OSDN Git Service

* fold-const.c (pointer_may_wrap_p): New static function.
[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   if (fold_deferring_overflow_warnings > 0)
1016     {
1017       if (fold_deferred_overflow_warning == NULL
1018           || wc < fold_deferred_overflow_code)
1019         {
1020           fold_deferred_overflow_warning = gmsgid;
1021           fold_deferred_overflow_code = wc;
1022         }
1023     }
1024   else if (issue_strict_overflow_warning (wc))
1025     warning (OPT_Wstrict_overflow, gmsgid);
1026 }
1027 \f
1028 /* Return true if the built-in mathematical function specified by CODE
1029    is odd, i.e. -f(x) == f(-x).  */
1030
1031 static bool
1032 negate_mathfn_p (enum built_in_function code)
1033 {
1034   switch (code)
1035     {
1036     CASE_FLT_FN (BUILT_IN_ASIN):
1037     CASE_FLT_FN (BUILT_IN_ASINH):
1038     CASE_FLT_FN (BUILT_IN_ATAN):
1039     CASE_FLT_FN (BUILT_IN_ATANH):
1040     CASE_FLT_FN (BUILT_IN_CASIN):
1041     CASE_FLT_FN (BUILT_IN_CASINH):
1042     CASE_FLT_FN (BUILT_IN_CATAN):
1043     CASE_FLT_FN (BUILT_IN_CATANH):
1044     CASE_FLT_FN (BUILT_IN_CBRT):
1045     CASE_FLT_FN (BUILT_IN_CPROJ):
1046     CASE_FLT_FN (BUILT_IN_CSIN):
1047     CASE_FLT_FN (BUILT_IN_CSINH):
1048     CASE_FLT_FN (BUILT_IN_CTAN):
1049     CASE_FLT_FN (BUILT_IN_CTANH):
1050     CASE_FLT_FN (BUILT_IN_ERF):
1051     CASE_FLT_FN (BUILT_IN_LLROUND):
1052     CASE_FLT_FN (BUILT_IN_LROUND):
1053     CASE_FLT_FN (BUILT_IN_ROUND):
1054     CASE_FLT_FN (BUILT_IN_SIN):
1055     CASE_FLT_FN (BUILT_IN_SINH):
1056     CASE_FLT_FN (BUILT_IN_TAN):
1057     CASE_FLT_FN (BUILT_IN_TANH):
1058     CASE_FLT_FN (BUILT_IN_TRUNC):
1059       return true;
1060
1061     CASE_FLT_FN (BUILT_IN_LLRINT):
1062     CASE_FLT_FN (BUILT_IN_LRINT):
1063     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1064     CASE_FLT_FN (BUILT_IN_RINT):
1065       return !flag_rounding_math;
1066     
1067     default:
1068       break;
1069     }
1070   return false;
1071 }
1072
1073 /* Check whether we may negate an integer constant T without causing
1074    overflow.  */
1075
1076 bool
1077 may_negate_without_overflow_p (const_tree t)
1078 {
1079   unsigned HOST_WIDE_INT val;
1080   unsigned int prec;
1081   tree type;
1082
1083   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1084
1085   type = TREE_TYPE (t);
1086   if (TYPE_UNSIGNED (type))
1087     return false;
1088
1089   prec = TYPE_PRECISION (type);
1090   if (prec > HOST_BITS_PER_WIDE_INT)
1091     {
1092       if (TREE_INT_CST_LOW (t) != 0)
1093         return true;
1094       prec -= HOST_BITS_PER_WIDE_INT;
1095       val = TREE_INT_CST_HIGH (t);
1096     }
1097   else
1098     val = TREE_INT_CST_LOW (t);
1099   if (prec < HOST_BITS_PER_WIDE_INT)
1100     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1101   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1102 }
1103
1104 /* Determine whether an expression T can be cheaply negated using
1105    the function negate_expr without introducing undefined overflow.  */
1106
1107 static bool
1108 negate_expr_p (tree t)
1109 {
1110   tree type;
1111
1112   if (t == 0)
1113     return false;
1114
1115   type = TREE_TYPE (t);
1116
1117   STRIP_SIGN_NOPS (t);
1118   switch (TREE_CODE (t))
1119     {
1120     case INTEGER_CST:
1121       if (TYPE_OVERFLOW_WRAPS (type))
1122         return true;
1123
1124       /* Check that -CST will not overflow type.  */
1125       return may_negate_without_overflow_p (t);
1126     case BIT_NOT_EXPR:
1127       return (INTEGRAL_TYPE_P (type)
1128               && TYPE_OVERFLOW_WRAPS (type));
1129
1130     case FIXED_CST:
1131     case REAL_CST:
1132     case NEGATE_EXPR:
1133       return true;
1134
1135     case COMPLEX_CST:
1136       return negate_expr_p (TREE_REALPART (t))
1137              && negate_expr_p (TREE_IMAGPART (t));
1138
1139     case COMPLEX_EXPR:
1140       return negate_expr_p (TREE_OPERAND (t, 0))
1141              && negate_expr_p (TREE_OPERAND (t, 1));
1142
1143     case CONJ_EXPR:
1144       return negate_expr_p (TREE_OPERAND (t, 0));
1145
1146     case PLUS_EXPR:
1147       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1148           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1149         return false;
1150       /* -(A + B) -> (-B) - A.  */
1151       if (negate_expr_p (TREE_OPERAND (t, 1))
1152           && reorder_operands_p (TREE_OPERAND (t, 0),
1153                                  TREE_OPERAND (t, 1)))
1154         return true;
1155       /* -(A + B) -> (-A) - B.  */
1156       return negate_expr_p (TREE_OPERAND (t, 0));
1157
1158     case MINUS_EXPR:
1159       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1160       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1161              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1162              && reorder_operands_p (TREE_OPERAND (t, 0),
1163                                     TREE_OPERAND (t, 1));
1164
1165     case MULT_EXPR:
1166       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1167         break;
1168
1169       /* Fall through.  */
1170
1171     case RDIV_EXPR:
1172       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1173         return negate_expr_p (TREE_OPERAND (t, 1))
1174                || negate_expr_p (TREE_OPERAND (t, 0));
1175       break;
1176
1177     case TRUNC_DIV_EXPR:
1178     case ROUND_DIV_EXPR:
1179     case FLOOR_DIV_EXPR:
1180     case CEIL_DIV_EXPR:
1181     case EXACT_DIV_EXPR:
1182       /* In general we can't negate A / B, because if A is INT_MIN and
1183          B is 1, we may turn this into INT_MIN / -1 which is undefined
1184          and actually traps on some architectures.  But if overflow is
1185          undefined, we can negate, because - (INT_MIN / 1) is an
1186          overflow.  */
1187       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1188           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1189         break;
1190       return negate_expr_p (TREE_OPERAND (t, 1))
1191              || negate_expr_p (TREE_OPERAND (t, 0));
1192
1193     case NOP_EXPR:
1194       /* Negate -((double)float) as (double)(-float).  */
1195       if (TREE_CODE (type) == REAL_TYPE)
1196         {
1197           tree tem = strip_float_extensions (t);
1198           if (tem != t)
1199             return negate_expr_p (tem);
1200         }
1201       break;
1202
1203     case CALL_EXPR:
1204       /* Negate -f(x) as f(-x).  */
1205       if (negate_mathfn_p (builtin_mathfn_code (t)))
1206         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1207       break;
1208
1209     case RSHIFT_EXPR:
1210       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1211       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1212         {
1213           tree op1 = TREE_OPERAND (t, 1);
1214           if (TREE_INT_CST_HIGH (op1) == 0
1215               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1216                  == TREE_INT_CST_LOW (op1))
1217             return true;
1218         }
1219       break;
1220
1221     default:
1222       break;
1223     }
1224   return false;
1225 }
1226
1227 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1228    simplification is possible.
1229    If negate_expr_p would return true for T, NULL_TREE will never be
1230    returned.  */
1231
1232 static tree
1233 fold_negate_expr (tree t)
1234 {
1235   tree type = TREE_TYPE (t);
1236   tree tem;
1237
1238   switch (TREE_CODE (t))
1239     {
1240     /* Convert - (~A) to A + 1.  */
1241     case BIT_NOT_EXPR:
1242       if (INTEGRAL_TYPE_P (type))
1243         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1244                             build_int_cst (type, 1));
1245       break;
1246       
1247     case INTEGER_CST:
1248       tem = fold_negate_const (t, type);
1249       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1250           || !TYPE_OVERFLOW_TRAPS (type))
1251         return tem;
1252       break;
1253
1254     case REAL_CST:
1255       tem = fold_negate_const (t, type);
1256       /* Two's complement FP formats, such as c4x, may overflow.  */
1257       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1258         return tem;
1259       break;
1260
1261     case FIXED_CST:
1262       tem = fold_negate_const (t, type);
1263       return tem;
1264
1265     case COMPLEX_CST:
1266       {
1267         tree rpart = negate_expr (TREE_REALPART (t));
1268         tree ipart = negate_expr (TREE_IMAGPART (t));
1269
1270         if ((TREE_CODE (rpart) == REAL_CST
1271              && TREE_CODE (ipart) == REAL_CST)
1272             || (TREE_CODE (rpart) == INTEGER_CST
1273                 && TREE_CODE (ipart) == INTEGER_CST))
1274           return build_complex (type, rpart, ipart);
1275       }
1276       break;
1277
1278     case COMPLEX_EXPR:
1279       if (negate_expr_p (t))
1280         return fold_build2 (COMPLEX_EXPR, type,
1281                             fold_negate_expr (TREE_OPERAND (t, 0)),
1282                             fold_negate_expr (TREE_OPERAND (t, 1)));
1283       break;
1284       
1285     case CONJ_EXPR:
1286       if (negate_expr_p (t))
1287         return fold_build1 (CONJ_EXPR, type,
1288                             fold_negate_expr (TREE_OPERAND (t, 0)));
1289       break;
1290
1291     case NEGATE_EXPR:
1292       return TREE_OPERAND (t, 0);
1293
1294     case PLUS_EXPR:
1295       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1296           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1297         {
1298           /* -(A + B) -> (-B) - A.  */
1299           if (negate_expr_p (TREE_OPERAND (t, 1))
1300               && reorder_operands_p (TREE_OPERAND (t, 0),
1301                                      TREE_OPERAND (t, 1)))
1302             {
1303               tem = negate_expr (TREE_OPERAND (t, 1));
1304               return fold_build2 (MINUS_EXPR, type,
1305                                   tem, TREE_OPERAND (t, 0));
1306             }
1307
1308           /* -(A + B) -> (-A) - B.  */
1309           if (negate_expr_p (TREE_OPERAND (t, 0)))
1310             {
1311               tem = negate_expr (TREE_OPERAND (t, 0));
1312               return fold_build2 (MINUS_EXPR, type,
1313                                   tem, TREE_OPERAND (t, 1));
1314             }
1315         }
1316       break;
1317
1318     case MINUS_EXPR:
1319       /* - (A - B) -> B - A  */
1320       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1321           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1322           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1323         return fold_build2 (MINUS_EXPR, type,
1324                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1325       break;
1326
1327     case MULT_EXPR:
1328       if (TYPE_UNSIGNED (type))
1329         break;
1330
1331       /* Fall through.  */
1332
1333     case RDIV_EXPR:
1334       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1335         {
1336           tem = TREE_OPERAND (t, 1);
1337           if (negate_expr_p (tem))
1338             return fold_build2 (TREE_CODE (t), type,
1339                                 TREE_OPERAND (t, 0), negate_expr (tem));
1340           tem = TREE_OPERAND (t, 0);
1341           if (negate_expr_p (tem))
1342             return fold_build2 (TREE_CODE (t), type,
1343                                 negate_expr (tem), TREE_OPERAND (t, 1));
1344         }
1345       break;
1346
1347     case TRUNC_DIV_EXPR:
1348     case ROUND_DIV_EXPR:
1349     case FLOOR_DIV_EXPR:
1350     case CEIL_DIV_EXPR:
1351     case EXACT_DIV_EXPR:
1352       /* In general we can't negate A / B, because if A is INT_MIN and
1353          B is 1, we may turn this into INT_MIN / -1 which is undefined
1354          and actually traps on some architectures.  But if overflow is
1355          undefined, we can negate, because - (INT_MIN / 1) is an
1356          overflow.  */
1357       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1358         {
1359           const char * const warnmsg = G_("assuming signed overflow does not "
1360                                           "occur when negating a division");
1361           tem = TREE_OPERAND (t, 1);
1362           if (negate_expr_p (tem))
1363             {
1364               if (INTEGRAL_TYPE_P (type)
1365                   && (TREE_CODE (tem) != INTEGER_CST
1366                       || integer_onep (tem)))
1367                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1368               return fold_build2 (TREE_CODE (t), type,
1369                                   TREE_OPERAND (t, 0), negate_expr (tem));
1370             }
1371           tem = TREE_OPERAND (t, 0);
1372           if (negate_expr_p (tem))
1373             {
1374               if (INTEGRAL_TYPE_P (type)
1375                   && (TREE_CODE (tem) != INTEGER_CST
1376                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1377                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1378               return fold_build2 (TREE_CODE (t), type,
1379                                   negate_expr (tem), TREE_OPERAND (t, 1));
1380             }
1381         }
1382       break;
1383
1384     case NOP_EXPR:
1385       /* Convert -((double)float) into (double)(-float).  */
1386       if (TREE_CODE (type) == REAL_TYPE)
1387         {
1388           tem = strip_float_extensions (t);
1389           if (tem != t && negate_expr_p (tem))
1390             return fold_convert (type, negate_expr (tem));
1391         }
1392       break;
1393
1394     case CALL_EXPR:
1395       /* Negate -f(x) as f(-x).  */
1396       if (negate_mathfn_p (builtin_mathfn_code (t))
1397           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1398         {
1399           tree fndecl, arg;
1400
1401           fndecl = get_callee_fndecl (t);
1402           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1403           return build_call_expr (fndecl, 1, arg);
1404         }
1405       break;
1406
1407     case RSHIFT_EXPR:
1408       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1409       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1410         {
1411           tree op1 = TREE_OPERAND (t, 1);
1412           if (TREE_INT_CST_HIGH (op1) == 0
1413               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1414                  == TREE_INT_CST_LOW (op1))
1415             {
1416               tree ntype = TYPE_UNSIGNED (type)
1417                            ? signed_type_for (type)
1418                            : unsigned_type_for (type);
1419               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1420               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1421               return fold_convert (type, temp);
1422             }
1423         }
1424       break;
1425
1426     default:
1427       break;
1428     }
1429
1430   return NULL_TREE;
1431 }
1432
1433 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1434    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1435    return NULL_TREE. */
1436
1437 static tree
1438 negate_expr (tree t)
1439 {
1440   tree type, tem;
1441
1442   if (t == NULL_TREE)
1443     return NULL_TREE;
1444
1445   type = TREE_TYPE (t);
1446   STRIP_SIGN_NOPS (t);
1447
1448   tem = fold_negate_expr (t);
1449   if (!tem)
1450     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1451   return fold_convert (type, tem);
1452 }
1453 \f
1454 /* Split a tree IN into a constant, literal and variable parts that could be
1455    combined with CODE to make IN.  "constant" means an expression with
1456    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1457    commutative arithmetic operation.  Store the constant part into *CONP,
1458    the literal in *LITP and return the variable part.  If a part isn't
1459    present, set it to null.  If the tree does not decompose in this way,
1460    return the entire tree as the variable part and the other parts as null.
1461
1462    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1463    case, we negate an operand that was subtracted.  Except if it is a
1464    literal for which we use *MINUS_LITP instead.
1465
1466    If NEGATE_P is true, we are negating all of IN, again except a literal
1467    for which we use *MINUS_LITP instead.
1468
1469    If IN is itself a literal or constant, return it as appropriate.
1470
1471    Note that we do not guarantee that any of the three values will be the
1472    same type as IN, but they will have the same signedness and mode.  */
1473
1474 static tree
1475 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1476             tree *minus_litp, int negate_p)
1477 {
1478   tree var = 0;
1479
1480   *conp = 0;
1481   *litp = 0;
1482   *minus_litp = 0;
1483
1484   /* Strip any conversions that don't change the machine mode or signedness.  */
1485   STRIP_SIGN_NOPS (in);
1486
1487   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1488       || TREE_CODE (in) == FIXED_CST)
1489     *litp = in;
1490   else if (TREE_CODE (in) == code
1491            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1492                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1493                /* We can associate addition and subtraction together (even
1494                   though the C standard doesn't say so) for integers because
1495                   the value is not affected.  For reals, the value might be
1496                   affected, so we can't.  */
1497                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1498                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1499     {
1500       tree op0 = TREE_OPERAND (in, 0);
1501       tree op1 = TREE_OPERAND (in, 1);
1502       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1503       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1504
1505       /* First see if either of the operands is a literal, then a constant.  */
1506       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1507           || TREE_CODE (op0) == FIXED_CST)
1508         *litp = op0, op0 = 0;
1509       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1510                || TREE_CODE (op1) == FIXED_CST)
1511         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1512
1513       if (op0 != 0 && TREE_CONSTANT (op0))
1514         *conp = op0, op0 = 0;
1515       else if (op1 != 0 && TREE_CONSTANT (op1))
1516         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1517
1518       /* If we haven't dealt with either operand, this is not a case we can
1519          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1520       if (op0 != 0 && op1 != 0)
1521         var = in;
1522       else if (op0 != 0)
1523         var = op0;
1524       else
1525         var = op1, neg_var_p = neg1_p;
1526
1527       /* Now do any needed negations.  */
1528       if (neg_litp_p)
1529         *minus_litp = *litp, *litp = 0;
1530       if (neg_conp_p)
1531         *conp = negate_expr (*conp);
1532       if (neg_var_p)
1533         var = negate_expr (var);
1534     }
1535   else if (TREE_CONSTANT (in))
1536     *conp = in;
1537   else
1538     var = in;
1539
1540   if (negate_p)
1541     {
1542       if (*litp)
1543         *minus_litp = *litp, *litp = 0;
1544       else if (*minus_litp)
1545         *litp = *minus_litp, *minus_litp = 0;
1546       *conp = negate_expr (*conp);
1547       var = negate_expr (var);
1548     }
1549
1550   return var;
1551 }
1552
1553 /* Re-associate trees split by the above function.  T1 and T2 are either
1554    expressions to associate or null.  Return the new expression, if any.  If
1555    we build an operation, do it in TYPE and with CODE.  */
1556
1557 static tree
1558 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1559 {
1560   if (t1 == 0)
1561     return t2;
1562   else if (t2 == 0)
1563     return t1;
1564
1565   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1566      try to fold this since we will have infinite recursion.  But do
1567      deal with any NEGATE_EXPRs.  */
1568   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1569       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1570     {
1571       if (code == PLUS_EXPR)
1572         {
1573           if (TREE_CODE (t1) == NEGATE_EXPR)
1574             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1575                            fold_convert (type, TREE_OPERAND (t1, 0)));
1576           else if (TREE_CODE (t2) == NEGATE_EXPR)
1577             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1578                            fold_convert (type, TREE_OPERAND (t2, 0)));
1579           else if (integer_zerop (t2))
1580             return fold_convert (type, t1);
1581         }
1582       else if (code == MINUS_EXPR)
1583         {
1584           if (integer_zerop (t2))
1585             return fold_convert (type, t1);
1586         }
1587
1588       return build2 (code, type, fold_convert (type, t1),
1589                      fold_convert (type, t2));
1590     }
1591
1592   return fold_build2 (code, type, fold_convert (type, t1),
1593                       fold_convert (type, t2));
1594 }
1595 \f
1596 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1597    for use in int_const_binop, size_binop and size_diffop.  */
1598
1599 static bool
1600 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1601 {
1602   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1603     return false;
1604   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1605     return false;
1606
1607   switch (code)
1608     {
1609     case LSHIFT_EXPR:
1610     case RSHIFT_EXPR:
1611     case LROTATE_EXPR:
1612     case RROTATE_EXPR:
1613       return true;
1614
1615     default:
1616       break;
1617     }
1618
1619   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1620          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1621          && TYPE_MODE (type1) == TYPE_MODE (type2);
1622 }
1623
1624
1625 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1626    to produce a new constant.  Return NULL_TREE if we don't know how
1627    to evaluate CODE at compile-time.
1628
1629    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1630
1631 tree
1632 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1633 {
1634   unsigned HOST_WIDE_INT int1l, int2l;
1635   HOST_WIDE_INT int1h, int2h;
1636   unsigned HOST_WIDE_INT low;
1637   HOST_WIDE_INT hi;
1638   unsigned HOST_WIDE_INT garbagel;
1639   HOST_WIDE_INT garbageh;
1640   tree t;
1641   tree type = TREE_TYPE (arg1);
1642   int uns = TYPE_UNSIGNED (type);
1643   int is_sizetype
1644     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1645   int overflow = 0;
1646
1647   int1l = TREE_INT_CST_LOW (arg1);
1648   int1h = TREE_INT_CST_HIGH (arg1);
1649   int2l = TREE_INT_CST_LOW (arg2);
1650   int2h = TREE_INT_CST_HIGH (arg2);
1651
1652   switch (code)
1653     {
1654     case BIT_IOR_EXPR:
1655       low = int1l | int2l, hi = int1h | int2h;
1656       break;
1657
1658     case BIT_XOR_EXPR:
1659       low = int1l ^ int2l, hi = int1h ^ int2h;
1660       break;
1661
1662     case BIT_AND_EXPR:
1663       low = int1l & int2l, hi = int1h & int2h;
1664       break;
1665
1666     case RSHIFT_EXPR:
1667       int2l = -int2l;
1668     case LSHIFT_EXPR:
1669       /* It's unclear from the C standard whether shifts can overflow.
1670          The following code ignores overflow; perhaps a C standard
1671          interpretation ruling is needed.  */
1672       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1673                      &low, &hi, !uns);
1674       break;
1675
1676     case RROTATE_EXPR:
1677       int2l = - int2l;
1678     case LROTATE_EXPR:
1679       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1680                       &low, &hi);
1681       break;
1682
1683     case PLUS_EXPR:
1684       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1685       break;
1686
1687     case MINUS_EXPR:
1688       neg_double (int2l, int2h, &low, &hi);
1689       add_double (int1l, int1h, low, hi, &low, &hi);
1690       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1691       break;
1692
1693     case MULT_EXPR:
1694       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1695       break;
1696
1697     case TRUNC_DIV_EXPR:
1698     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1699     case EXACT_DIV_EXPR:
1700       /* This is a shortcut for a common special case.  */
1701       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1702           && !TREE_OVERFLOW (arg1)
1703           && !TREE_OVERFLOW (arg2)
1704           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1705         {
1706           if (code == CEIL_DIV_EXPR)
1707             int1l += int2l - 1;
1708
1709           low = int1l / int2l, hi = 0;
1710           break;
1711         }
1712
1713       /* ... fall through ...  */
1714
1715     case ROUND_DIV_EXPR:
1716       if (int2h == 0 && int2l == 0)
1717         return NULL_TREE;
1718       if (int2h == 0 && int2l == 1)
1719         {
1720           low = int1l, hi = int1h;
1721           break;
1722         }
1723       if (int1l == int2l && int1h == int2h
1724           && ! (int1l == 0 && int1h == 0))
1725         {
1726           low = 1, hi = 0;
1727           break;
1728         }
1729       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1730                                        &low, &hi, &garbagel, &garbageh);
1731       break;
1732
1733     case TRUNC_MOD_EXPR:
1734     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1735       /* This is a shortcut for a common special case.  */
1736       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1737           && !TREE_OVERFLOW (arg1)
1738           && !TREE_OVERFLOW (arg2)
1739           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1740         {
1741           if (code == CEIL_MOD_EXPR)
1742             int1l += int2l - 1;
1743           low = int1l % int2l, hi = 0;
1744           break;
1745         }
1746
1747       /* ... fall through ...  */
1748
1749     case ROUND_MOD_EXPR:
1750       if (int2h == 0 && int2l == 0)
1751         return NULL_TREE;
1752       overflow = div_and_round_double (code, uns,
1753                                        int1l, int1h, int2l, int2h,
1754                                        &garbagel, &garbageh, &low, &hi);
1755       break;
1756
1757     case MIN_EXPR:
1758     case MAX_EXPR:
1759       if (uns)
1760         low = (((unsigned HOST_WIDE_INT) int1h
1761                 < (unsigned HOST_WIDE_INT) int2h)
1762                || (((unsigned HOST_WIDE_INT) int1h
1763                     == (unsigned HOST_WIDE_INT) int2h)
1764                    && int1l < int2l));
1765       else
1766         low = (int1h < int2h
1767                || (int1h == int2h && int1l < int2l));
1768
1769       if (low == (code == MIN_EXPR))
1770         low = int1l, hi = int1h;
1771       else
1772         low = int2l, hi = int2h;
1773       break;
1774
1775     default:
1776       return NULL_TREE;
1777     }
1778
1779   if (notrunc)
1780     {
1781       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1782
1783       /* Propagate overflow flags ourselves.  */
1784       if (((!uns || is_sizetype) && overflow)
1785           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1786         {
1787           t = copy_node (t);
1788           TREE_OVERFLOW (t) = 1;
1789         }
1790     }
1791   else
1792     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1793                                ((!uns || is_sizetype) && overflow)
1794                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1795
1796   return t;
1797 }
1798
1799 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1800    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1801    are the same kind of constant and the same machine mode.  Return zero if
1802    combining the constants is not allowed in the current operating mode.
1803
1804    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1805
1806 static tree
1807 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1808 {
1809   /* Sanity check for the recursive cases.  */
1810   if (!arg1 || !arg2)
1811     return NULL_TREE;
1812
1813   STRIP_NOPS (arg1);
1814   STRIP_NOPS (arg2);
1815
1816   if (TREE_CODE (arg1) == INTEGER_CST)
1817     return int_const_binop (code, arg1, arg2, notrunc);
1818
1819   if (TREE_CODE (arg1) == REAL_CST)
1820     {
1821       enum machine_mode mode;
1822       REAL_VALUE_TYPE d1;
1823       REAL_VALUE_TYPE d2;
1824       REAL_VALUE_TYPE value;
1825       REAL_VALUE_TYPE result;
1826       bool inexact;
1827       tree t, type;
1828
1829       /* The following codes are handled by real_arithmetic.  */
1830       switch (code)
1831         {
1832         case PLUS_EXPR:
1833         case MINUS_EXPR:
1834         case MULT_EXPR:
1835         case RDIV_EXPR:
1836         case MIN_EXPR:
1837         case MAX_EXPR:
1838           break;
1839
1840         default:
1841           return NULL_TREE;
1842         }
1843
1844       d1 = TREE_REAL_CST (arg1);
1845       d2 = TREE_REAL_CST (arg2);
1846
1847       type = TREE_TYPE (arg1);
1848       mode = TYPE_MODE (type);
1849
1850       /* Don't perform operation if we honor signaling NaNs and
1851          either operand is a NaN.  */
1852       if (HONOR_SNANS (mode)
1853           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1854         return NULL_TREE;
1855
1856       /* Don't perform operation if it would raise a division
1857          by zero exception.  */
1858       if (code == RDIV_EXPR
1859           && REAL_VALUES_EQUAL (d2, dconst0)
1860           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1861         return NULL_TREE;
1862
1863       /* If either operand is a NaN, just return it.  Otherwise, set up
1864          for floating-point trap; we return an overflow.  */
1865       if (REAL_VALUE_ISNAN (d1))
1866         return arg1;
1867       else if (REAL_VALUE_ISNAN (d2))
1868         return arg2;
1869
1870       inexact = real_arithmetic (&value, code, &d1, &d2);
1871       real_convert (&result, mode, &value);
1872
1873       /* Don't constant fold this floating point operation if
1874          the result has overflowed and flag_trapping_math.  */
1875       if (flag_trapping_math
1876           && MODE_HAS_INFINITIES (mode)
1877           && REAL_VALUE_ISINF (result)
1878           && !REAL_VALUE_ISINF (d1)
1879           && !REAL_VALUE_ISINF (d2))
1880         return NULL_TREE;
1881
1882       /* Don't constant fold this floating point operation if the
1883          result may dependent upon the run-time rounding mode and
1884          flag_rounding_math is set, or if GCC's software emulation
1885          is unable to accurately represent the result.  */
1886       if ((flag_rounding_math
1887            || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1888                && !flag_unsafe_math_optimizations))
1889           && (inexact || !real_identical (&result, &value)))
1890         return NULL_TREE;
1891
1892       t = build_real (type, result);
1893
1894       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1895       return t;
1896     }
1897
1898   if (TREE_CODE (arg1) == FIXED_CST)
1899     {
1900       FIXED_VALUE_TYPE f1;
1901       FIXED_VALUE_TYPE f2;
1902       FIXED_VALUE_TYPE result;
1903       tree t, type;
1904       int sat_p;
1905       bool overflow_p;
1906
1907       /* The following codes are handled by fixed_arithmetic.  */
1908       switch (code)
1909         {
1910         case PLUS_EXPR:
1911         case MINUS_EXPR:
1912         case MULT_EXPR:
1913         case TRUNC_DIV_EXPR:
1914           f2 = TREE_FIXED_CST (arg2);
1915           break;
1916
1917         case LSHIFT_EXPR:
1918         case RSHIFT_EXPR:
1919           f2.data.high = TREE_INT_CST_HIGH (arg2);
1920           f2.data.low = TREE_INT_CST_LOW (arg2);
1921           f2.mode = SImode;
1922           break;
1923
1924         default:
1925           return NULL_TREE;
1926         }
1927
1928       f1 = TREE_FIXED_CST (arg1);
1929       type = TREE_TYPE (arg1);
1930       sat_p = TYPE_SATURATING (type);
1931       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1932       t = build_fixed (type, result);
1933       /* Propagate overflow flags.  */
1934       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1935         {
1936           TREE_OVERFLOW (t) = 1;
1937           TREE_CONSTANT_OVERFLOW (t) = 1;
1938         }
1939       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1940         TREE_CONSTANT_OVERFLOW (t) = 1;
1941       return t;
1942     }
1943
1944   if (TREE_CODE (arg1) == COMPLEX_CST)
1945     {
1946       tree type = TREE_TYPE (arg1);
1947       tree r1 = TREE_REALPART (arg1);
1948       tree i1 = TREE_IMAGPART (arg1);
1949       tree r2 = TREE_REALPART (arg2);
1950       tree i2 = TREE_IMAGPART (arg2);
1951       tree real, imag;
1952
1953       switch (code)
1954         {
1955         case PLUS_EXPR:
1956         case MINUS_EXPR:
1957           real = const_binop (code, r1, r2, notrunc);
1958           imag = const_binop (code, i1, i2, notrunc);
1959           break;
1960
1961         case MULT_EXPR:
1962           real = const_binop (MINUS_EXPR,
1963                               const_binop (MULT_EXPR, r1, r2, notrunc),
1964                               const_binop (MULT_EXPR, i1, i2, notrunc),
1965                               notrunc);
1966           imag = const_binop (PLUS_EXPR,
1967                               const_binop (MULT_EXPR, r1, i2, notrunc),
1968                               const_binop (MULT_EXPR, i1, r2, notrunc),
1969                               notrunc);
1970           break;
1971
1972         case RDIV_EXPR:
1973           {
1974             tree magsquared
1975               = const_binop (PLUS_EXPR,
1976                              const_binop (MULT_EXPR, r2, r2, notrunc),
1977                              const_binop (MULT_EXPR, i2, i2, notrunc),
1978                              notrunc);
1979             tree t1
1980               = const_binop (PLUS_EXPR,
1981                              const_binop (MULT_EXPR, r1, r2, notrunc),
1982                              const_binop (MULT_EXPR, i1, i2, notrunc),
1983                              notrunc);
1984             tree t2
1985               = const_binop (MINUS_EXPR,
1986                              const_binop (MULT_EXPR, i1, r2, notrunc),
1987                              const_binop (MULT_EXPR, r1, i2, notrunc),
1988                              notrunc);
1989
1990             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1991               code = TRUNC_DIV_EXPR;
1992
1993             real = const_binop (code, t1, magsquared, notrunc);
1994             imag = const_binop (code, t2, magsquared, notrunc);
1995           }
1996           break;
1997
1998         default:
1999           return NULL_TREE;
2000         }
2001
2002       if (real && imag)
2003         return build_complex (type, real, imag);
2004     }
2005
2006   return NULL_TREE;
2007 }
2008
2009 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2010    indicates which particular sizetype to create.  */
2011
2012 tree
2013 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2014 {
2015   return build_int_cst (sizetype_tab[(int) kind], number);
2016 }
2017 \f
2018 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2019    is a tree code.  The type of the result is taken from the operands.
2020    Both must be equivalent integer types, ala int_binop_types_match_p.
2021    If the operands are constant, so is the result.  */
2022
2023 tree
2024 size_binop (enum tree_code code, tree arg0, tree arg1)
2025 {
2026   tree type = TREE_TYPE (arg0);
2027
2028   if (arg0 == error_mark_node || arg1 == error_mark_node)
2029     return error_mark_node;
2030
2031   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2032                                        TREE_TYPE (arg1)));
2033
2034   /* Handle the special case of two integer constants faster.  */
2035   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2036     {
2037       /* And some specific cases even faster than that.  */
2038       if (code == PLUS_EXPR)
2039         {
2040           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2041             return arg1;
2042           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2043             return arg0;
2044         }
2045       else if (code == MINUS_EXPR)
2046         {
2047           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2048             return arg0;
2049         }
2050       else if (code == MULT_EXPR)
2051         {
2052           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2053             return arg1;
2054         }
2055
2056       /* Handle general case of two integer constants.  */
2057       return int_const_binop (code, arg0, arg1, 0);
2058     }
2059
2060   return fold_build2 (code, type, arg0, arg1);
2061 }
2062
2063 /* Given two values, either both of sizetype or both of bitsizetype,
2064    compute the difference between the two values.  Return the value
2065    in signed type corresponding to the type of the operands.  */
2066
2067 tree
2068 size_diffop (tree arg0, tree arg1)
2069 {
2070   tree type = TREE_TYPE (arg0);
2071   tree ctype;
2072
2073   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2074                                        TREE_TYPE (arg1)));
2075
2076   /* If the type is already signed, just do the simple thing.  */
2077   if (!TYPE_UNSIGNED (type))
2078     return size_binop (MINUS_EXPR, arg0, arg1);
2079
2080   if (type == sizetype)
2081     ctype = ssizetype;
2082   else if (type == bitsizetype)
2083     ctype = sbitsizetype;
2084   else
2085     ctype = signed_type_for (type);
2086
2087   /* If either operand is not a constant, do the conversions to the signed
2088      type and subtract.  The hardware will do the right thing with any
2089      overflow in the subtraction.  */
2090   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2091     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2092                        fold_convert (ctype, arg1));
2093
2094   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2095      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2096      overflow) and negate (which can't either).  Special-case a result
2097      of zero while we're here.  */
2098   if (tree_int_cst_equal (arg0, arg1))
2099     return build_int_cst (ctype, 0);
2100   else if (tree_int_cst_lt (arg1, arg0))
2101     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2102   else
2103     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2104                        fold_convert (ctype, size_binop (MINUS_EXPR,
2105                                                         arg1, arg0)));
2106 }
2107 \f
2108 /* A subroutine of fold_convert_const handling conversions of an
2109    INTEGER_CST to another integer type.  */
2110
2111 static tree
2112 fold_convert_const_int_from_int (tree type, const_tree arg1)
2113 {
2114   tree t;
2115
2116   /* Given an integer constant, make new constant with new type,
2117      appropriately sign-extended or truncated.  */
2118   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2119                              TREE_INT_CST_HIGH (arg1),
2120                              /* Don't set the overflow when
2121                                 converting from a pointer,  */
2122                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2123                              /* or to a sizetype with same signedness
2124                                 and the precision is unchanged.
2125                                 ???  sizetype is always sign-extended,
2126                                 but its signedness depends on the
2127                                 frontend.  Thus we see spurious overflows
2128                                 here if we do not check this.  */
2129                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2130                                    == TYPE_PRECISION (type))
2131                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2132                                       == TYPE_UNSIGNED (type))
2133                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2134                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2135                                       || (TREE_CODE (type) == INTEGER_TYPE
2136                                           && TYPE_IS_SIZETYPE (type)))),
2137                              (TREE_INT_CST_HIGH (arg1) < 0
2138                               && (TYPE_UNSIGNED (type)
2139                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2140                              | TREE_OVERFLOW (arg1));
2141
2142   return t;
2143 }
2144
2145 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2146    to an integer type.  */
2147
2148 static tree
2149 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2150 {
2151   int overflow = 0;
2152   tree t;
2153
2154   /* The following code implements the floating point to integer
2155      conversion rules required by the Java Language Specification,
2156      that IEEE NaNs are mapped to zero and values that overflow
2157      the target precision saturate, i.e. values greater than
2158      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2159      are mapped to INT_MIN.  These semantics are allowed by the
2160      C and C++ standards that simply state that the behavior of
2161      FP-to-integer conversion is unspecified upon overflow.  */
2162
2163   HOST_WIDE_INT high, low;
2164   REAL_VALUE_TYPE r;
2165   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2166
2167   switch (code)
2168     {
2169     case FIX_TRUNC_EXPR:
2170       real_trunc (&r, VOIDmode, &x);
2171       break;
2172
2173     default:
2174       gcc_unreachable ();
2175     }
2176
2177   /* If R is NaN, return zero and show we have an overflow.  */
2178   if (REAL_VALUE_ISNAN (r))
2179     {
2180       overflow = 1;
2181       high = 0;
2182       low = 0;
2183     }
2184
2185   /* See if R is less than the lower bound or greater than the
2186      upper bound.  */
2187
2188   if (! overflow)
2189     {
2190       tree lt = TYPE_MIN_VALUE (type);
2191       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2192       if (REAL_VALUES_LESS (r, l))
2193         {
2194           overflow = 1;
2195           high = TREE_INT_CST_HIGH (lt);
2196           low = TREE_INT_CST_LOW (lt);
2197         }
2198     }
2199
2200   if (! overflow)
2201     {
2202       tree ut = TYPE_MAX_VALUE (type);
2203       if (ut)
2204         {
2205           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2206           if (REAL_VALUES_LESS (u, r))
2207             {
2208               overflow = 1;
2209               high = TREE_INT_CST_HIGH (ut);
2210               low = TREE_INT_CST_LOW (ut);
2211             }
2212         }
2213     }
2214
2215   if (! overflow)
2216     REAL_VALUE_TO_INT (&low, &high, r);
2217
2218   t = force_fit_type_double (type, low, high, -1,
2219                              overflow | TREE_OVERFLOW (arg1));
2220   return t;
2221 }
2222
2223 /* A subroutine of fold_convert_const handling conversions of a
2224    FIXED_CST to an integer type.  */
2225
2226 static tree
2227 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2228 {
2229   tree t;
2230   double_int temp, temp_trunc;
2231   unsigned int mode;
2232
2233   /* Right shift FIXED_CST to temp by fbit.  */
2234   temp = TREE_FIXED_CST (arg1).data;
2235   mode = TREE_FIXED_CST (arg1).mode;
2236   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2237     {
2238       lshift_double (temp.low, temp.high,
2239                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2241
2242       /* Left shift temp to temp_trunc by fbit.  */
2243       lshift_double (temp.low, temp.high,
2244                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2245                      &temp_trunc.low, &temp_trunc.high,
2246                      SIGNED_FIXED_POINT_MODE_P (mode));
2247     }
2248   else
2249     {
2250       temp.low = 0;
2251       temp.high = 0;
2252       temp_trunc.low = 0;
2253       temp_trunc.high = 0;
2254     }
2255
2256   /* If FIXED_CST is negative, we need to round the value toward 0.
2257      By checking if the fractional bits are not zero to add 1 to temp.  */
2258   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2259       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2260     {
2261       double_int one;
2262       one.low = 1;
2263       one.high = 0;
2264       temp = double_int_add (temp, one);
2265     }
2266
2267   /* Given a fixed-point constant, make new constant with new type,
2268      appropriately sign-extended or truncated.  */
2269   t = force_fit_type_double (type, temp.low, temp.high, -1,
2270                              (temp.high < 0
2271                               && (TYPE_UNSIGNED (type)
2272                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2273                              | TREE_OVERFLOW (arg1));
2274
2275   return t;
2276 }
2277
2278 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2279    to another floating point type.  */
2280
2281 static tree
2282 fold_convert_const_real_from_real (tree type, const_tree arg1)
2283 {
2284   REAL_VALUE_TYPE value;
2285   tree t;
2286
2287   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2288   t = build_real (type, value);
2289
2290   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2291   return t;
2292 }
2293
2294 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2295    to a floating point type.  */
2296
2297 static tree
2298 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2299 {
2300   REAL_VALUE_TYPE value;
2301   tree t;
2302
2303   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2304   t = build_real (type, value);
2305
2306   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2307   TREE_CONSTANT_OVERFLOW (t)
2308     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2309   return t;
2310 }
2311
2312 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2313    to another fixed-point type.  */
2314
2315 static tree
2316 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2317 {
2318   FIXED_VALUE_TYPE value;
2319   tree t;
2320   bool overflow_p;
2321
2322   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2323                               TYPE_SATURATING (type));
2324   t = build_fixed (type, value);
2325
2326   /* Propagate overflow flags.  */
2327   if (overflow_p | TREE_OVERFLOW (arg1))
2328     {
2329       TREE_OVERFLOW (t) = 1;
2330       TREE_CONSTANT_OVERFLOW (t) = 1;
2331     }
2332   else if (TREE_CONSTANT_OVERFLOW (arg1))
2333     TREE_CONSTANT_OVERFLOW (t) = 1;
2334   return t;
2335 }
2336
2337 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2338    to a fixed-point type.  */
2339
2340 static tree
2341 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2342 {
2343   FIXED_VALUE_TYPE value;
2344   tree t;
2345   bool overflow_p;
2346
2347   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2348                                        TREE_INT_CST (arg1),
2349                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2350                                        TYPE_SATURATING (type));
2351   t = build_fixed (type, value);
2352
2353   /* Propagate overflow flags.  */
2354   if (overflow_p | TREE_OVERFLOW (arg1))
2355     {
2356       TREE_OVERFLOW (t) = 1;
2357       TREE_CONSTANT_OVERFLOW (t) = 1;
2358     }
2359   else if (TREE_CONSTANT_OVERFLOW (arg1))
2360     TREE_CONSTANT_OVERFLOW (t) = 1;
2361   return t;
2362 }
2363
2364 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2365    to a fixed-point type.  */
2366
2367 static tree
2368 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2369 {
2370   FIXED_VALUE_TYPE value;
2371   tree t;
2372   bool overflow_p;
2373
2374   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2375                                         &TREE_REAL_CST (arg1),
2376                                         TYPE_SATURATING (type));
2377   t = build_fixed (type, value);
2378
2379   /* Propagate overflow flags.  */
2380   if (overflow_p | TREE_OVERFLOW (arg1))
2381     {
2382       TREE_OVERFLOW (t) = 1;
2383       TREE_CONSTANT_OVERFLOW (t) = 1;
2384     }
2385   else if (TREE_CONSTANT_OVERFLOW (arg1))
2386     TREE_CONSTANT_OVERFLOW (t) = 1;
2387   return t;
2388 }
2389
2390 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2391    type TYPE.  If no simplification can be done return NULL_TREE.  */
2392
2393 static tree
2394 fold_convert_const (enum tree_code code, tree type, tree arg1)
2395 {
2396   if (TREE_TYPE (arg1) == type)
2397     return arg1;
2398
2399   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2400     {
2401       if (TREE_CODE (arg1) == INTEGER_CST)
2402         return fold_convert_const_int_from_int (type, arg1);
2403       else if (TREE_CODE (arg1) == REAL_CST)
2404         return fold_convert_const_int_from_real (code, type, arg1);
2405       else if (TREE_CODE (arg1) == FIXED_CST)
2406         return fold_convert_const_int_from_fixed (type, arg1);
2407     }
2408   else if (TREE_CODE (type) == REAL_TYPE)
2409     {
2410       if (TREE_CODE (arg1) == INTEGER_CST)
2411         return build_real_from_int_cst (type, arg1);
2412       else if (TREE_CODE (arg1) == REAL_CST)
2413         return fold_convert_const_real_from_real (type, arg1);
2414       else if (TREE_CODE (arg1) == FIXED_CST)
2415         return fold_convert_const_real_from_fixed (type, arg1);
2416     }
2417   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2418     {
2419       if (TREE_CODE (arg1) == FIXED_CST)
2420         return fold_convert_const_fixed_from_fixed (type, arg1);
2421       else if (TREE_CODE (arg1) == INTEGER_CST)
2422         return fold_convert_const_fixed_from_int (type, arg1);
2423       else if (TREE_CODE (arg1) == REAL_CST)
2424         return fold_convert_const_fixed_from_real (type, arg1);
2425     }
2426   return NULL_TREE;
2427 }
2428
2429 /* Construct a vector of zero elements of vector type TYPE.  */
2430
2431 static tree
2432 build_zero_vector (tree type)
2433 {
2434   tree elem, list;
2435   int i, units;
2436
2437   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2438   units = TYPE_VECTOR_SUBPARTS (type);
2439   
2440   list = NULL_TREE;
2441   for (i = 0; i < units; i++)
2442     list = tree_cons (NULL_TREE, elem, list);
2443   return build_vector (type, list);
2444 }
2445
2446 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2447
2448 bool
2449 fold_convertible_p (const_tree type, const_tree arg)
2450 {
2451   tree orig = TREE_TYPE (arg);
2452
2453   if (type == orig)
2454     return true;
2455
2456   if (TREE_CODE (arg) == ERROR_MARK
2457       || TREE_CODE (type) == ERROR_MARK
2458       || TREE_CODE (orig) == ERROR_MARK)
2459     return false;
2460
2461   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2462     return true;
2463
2464   switch (TREE_CODE (type))
2465     {
2466     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2467     case POINTER_TYPE: case REFERENCE_TYPE:
2468     case OFFSET_TYPE:
2469       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2470           || TREE_CODE (orig) == OFFSET_TYPE)
2471         return true;
2472       return (TREE_CODE (orig) == VECTOR_TYPE
2473               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2474
2475     case REAL_TYPE:
2476     case FIXED_POINT_TYPE:
2477     case COMPLEX_TYPE:
2478     case VECTOR_TYPE:
2479     case VOID_TYPE:
2480       return TREE_CODE (type) == TREE_CODE (orig);
2481
2482     default:
2483       return false;
2484     }
2485 }
2486
2487 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2488    simple conversions in preference to calling the front-end's convert.  */
2489
2490 tree
2491 fold_convert (tree type, tree arg)
2492 {
2493   tree orig = TREE_TYPE (arg);
2494   tree tem;
2495
2496   if (type == orig)
2497     return arg;
2498
2499   if (TREE_CODE (arg) == ERROR_MARK
2500       || TREE_CODE (type) == ERROR_MARK
2501       || TREE_CODE (orig) == ERROR_MARK)
2502     return error_mark_node;
2503
2504   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2505     return fold_build1 (NOP_EXPR, type, arg);
2506
2507   switch (TREE_CODE (type))
2508     {
2509     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2510     case POINTER_TYPE: case REFERENCE_TYPE:
2511     case OFFSET_TYPE:
2512       if (TREE_CODE (arg) == INTEGER_CST)
2513         {
2514           tem = fold_convert_const (NOP_EXPR, type, arg);
2515           if (tem != NULL_TREE)
2516             return tem;
2517         }
2518       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2519           || TREE_CODE (orig) == OFFSET_TYPE)
2520         return fold_build1 (NOP_EXPR, type, arg);
2521       if (TREE_CODE (orig) == COMPLEX_TYPE)
2522         {
2523           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2524           return fold_convert (type, tem);
2525         }
2526       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2527                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2528       return fold_build1 (NOP_EXPR, type, arg);
2529
2530     case REAL_TYPE:
2531       if (TREE_CODE (arg) == INTEGER_CST)
2532         {
2533           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2534           if (tem != NULL_TREE)
2535             return tem;
2536         }
2537       else if (TREE_CODE (arg) == REAL_CST)
2538         {
2539           tem = fold_convert_const (NOP_EXPR, type, arg);
2540           if (tem != NULL_TREE)
2541             return tem;
2542         }
2543       else if (TREE_CODE (arg) == FIXED_CST)
2544         {
2545           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2546           if (tem != NULL_TREE)
2547             return tem;
2548         }
2549
2550       switch (TREE_CODE (orig))
2551         {
2552         case INTEGER_TYPE:
2553         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2554         case POINTER_TYPE: case REFERENCE_TYPE:
2555           return fold_build1 (FLOAT_EXPR, type, arg);
2556
2557         case REAL_TYPE:
2558           return fold_build1 (NOP_EXPR, type, arg);
2559
2560         case FIXED_POINT_TYPE:
2561           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2562
2563         case COMPLEX_TYPE:
2564           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2565           return fold_convert (type, tem);
2566
2567         default:
2568           gcc_unreachable ();
2569         }
2570
2571     case FIXED_POINT_TYPE:
2572       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2573           || TREE_CODE (arg) == REAL_CST)
2574         {
2575           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2576           if (tem != NULL_TREE)
2577             return tem;
2578         }
2579
2580       switch (TREE_CODE (orig))
2581         {
2582         case FIXED_POINT_TYPE:
2583         case INTEGER_TYPE:
2584         case ENUMERAL_TYPE:
2585         case BOOLEAN_TYPE:
2586         case REAL_TYPE:
2587           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2588
2589         case COMPLEX_TYPE:
2590           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2591           return fold_convert (type, tem);
2592
2593         default:
2594           gcc_unreachable ();
2595         }
2596
2597     case COMPLEX_TYPE:
2598       switch (TREE_CODE (orig))
2599         {
2600         case INTEGER_TYPE:
2601         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2602         case POINTER_TYPE: case REFERENCE_TYPE:
2603         case REAL_TYPE:
2604         case FIXED_POINT_TYPE:
2605           return build2 (COMPLEX_EXPR, type,
2606                          fold_convert (TREE_TYPE (type), arg),
2607                          fold_convert (TREE_TYPE (type), integer_zero_node));
2608         case COMPLEX_TYPE:
2609           {
2610             tree rpart, ipart;
2611
2612             if (TREE_CODE (arg) == COMPLEX_EXPR)
2613               {
2614                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2615                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2616                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2617               }
2618
2619             arg = save_expr (arg);
2620             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2621             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2622             rpart = fold_convert (TREE_TYPE (type), rpart);
2623             ipart = fold_convert (TREE_TYPE (type), ipart);
2624             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2625           }
2626
2627         default:
2628           gcc_unreachable ();
2629         }
2630
2631     case VECTOR_TYPE:
2632       if (integer_zerop (arg))
2633         return build_zero_vector (type);
2634       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2635       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2636                   || TREE_CODE (orig) == VECTOR_TYPE);
2637       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2638
2639     case VOID_TYPE:
2640       tem = fold_ignored_result (arg);
2641       if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2642         return tem;
2643       return fold_build1 (NOP_EXPR, type, tem);
2644
2645     default:
2646       gcc_unreachable ();
2647     }
2648 }
2649 \f
2650 /* Return false if expr can be assumed not to be an lvalue, true
2651    otherwise.  */
2652
2653 static bool
2654 maybe_lvalue_p (const_tree x)
2655 {
2656   /* We only need to wrap lvalue tree codes.  */
2657   switch (TREE_CODE (x))
2658   {
2659   case VAR_DECL:
2660   case PARM_DECL:
2661   case RESULT_DECL:
2662   case LABEL_DECL:
2663   case FUNCTION_DECL:
2664   case SSA_NAME:
2665
2666   case COMPONENT_REF:
2667   case INDIRECT_REF:
2668   case ALIGN_INDIRECT_REF:
2669   case MISALIGNED_INDIRECT_REF:
2670   case ARRAY_REF:
2671   case ARRAY_RANGE_REF:
2672   case BIT_FIELD_REF:
2673   case OBJ_TYPE_REF:
2674
2675   case REALPART_EXPR:
2676   case IMAGPART_EXPR:
2677   case PREINCREMENT_EXPR:
2678   case PREDECREMENT_EXPR:
2679   case SAVE_EXPR:
2680   case TRY_CATCH_EXPR:
2681   case WITH_CLEANUP_EXPR:
2682   case COMPOUND_EXPR:
2683   case MODIFY_EXPR:
2684   case GIMPLE_MODIFY_STMT:
2685   case TARGET_EXPR:
2686   case COND_EXPR:
2687   case BIND_EXPR:
2688   case MIN_EXPR:
2689   case MAX_EXPR:
2690     break;
2691
2692   default:
2693     /* Assume the worst for front-end tree codes.  */
2694     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2695       break;
2696     return false;
2697   }
2698
2699   return true;
2700 }
2701
2702 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2703
2704 tree
2705 non_lvalue (tree x)
2706 {
2707   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2708      us.  */
2709   if (in_gimple_form)
2710     return x;
2711
2712   if (! maybe_lvalue_p (x))
2713     return x;
2714   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2715 }
2716
2717 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2718    Zero means allow extended lvalues.  */
2719
2720 int pedantic_lvalues;
2721
2722 /* When pedantic, return an expr equal to X but certainly not valid as a
2723    pedantic lvalue.  Otherwise, return X.  */
2724
2725 static tree
2726 pedantic_non_lvalue (tree x)
2727 {
2728   if (pedantic_lvalues)
2729     return non_lvalue (x);
2730   else
2731     return x;
2732 }
2733 \f
2734 /* Given a tree comparison code, return the code that is the logical inverse
2735    of the given code.  It is not safe to do this for floating-point
2736    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2737    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2738
2739 enum tree_code
2740 invert_tree_comparison (enum tree_code code, bool honor_nans)
2741 {
2742   if (honor_nans && flag_trapping_math)
2743     return ERROR_MARK;
2744
2745   switch (code)
2746     {
2747     case EQ_EXPR:
2748       return NE_EXPR;
2749     case NE_EXPR:
2750       return EQ_EXPR;
2751     case GT_EXPR:
2752       return honor_nans ? UNLE_EXPR : LE_EXPR;
2753     case GE_EXPR:
2754       return honor_nans ? UNLT_EXPR : LT_EXPR;
2755     case LT_EXPR:
2756       return honor_nans ? UNGE_EXPR : GE_EXPR;
2757     case LE_EXPR:
2758       return honor_nans ? UNGT_EXPR : GT_EXPR;
2759     case LTGT_EXPR:
2760       return UNEQ_EXPR;
2761     case UNEQ_EXPR:
2762       return LTGT_EXPR;
2763     case UNGT_EXPR:
2764       return LE_EXPR;
2765     case UNGE_EXPR:
2766       return LT_EXPR;
2767     case UNLT_EXPR:
2768       return GE_EXPR;
2769     case UNLE_EXPR:
2770       return GT_EXPR;
2771     case ORDERED_EXPR:
2772       return UNORDERED_EXPR;
2773     case UNORDERED_EXPR:
2774       return ORDERED_EXPR;
2775     default:
2776       gcc_unreachable ();
2777     }
2778 }
2779
2780 /* Similar, but return the comparison that results if the operands are
2781    swapped.  This is safe for floating-point.  */
2782
2783 enum tree_code
2784 swap_tree_comparison (enum tree_code code)
2785 {
2786   switch (code)
2787     {
2788     case EQ_EXPR:
2789     case NE_EXPR:
2790     case ORDERED_EXPR:
2791     case UNORDERED_EXPR:
2792     case LTGT_EXPR:
2793     case UNEQ_EXPR:
2794       return code;
2795     case GT_EXPR:
2796       return LT_EXPR;
2797     case GE_EXPR:
2798       return LE_EXPR;
2799     case LT_EXPR:
2800       return GT_EXPR;
2801     case LE_EXPR:
2802       return GE_EXPR;
2803     case UNGT_EXPR:
2804       return UNLT_EXPR;
2805     case UNGE_EXPR:
2806       return UNLE_EXPR;
2807     case UNLT_EXPR:
2808       return UNGT_EXPR;
2809     case UNLE_EXPR:
2810       return UNGE_EXPR;
2811     default:
2812       gcc_unreachable ();
2813     }
2814 }
2815
2816
2817 /* Convert a comparison tree code from an enum tree_code representation
2818    into a compcode bit-based encoding.  This function is the inverse of
2819    compcode_to_comparison.  */
2820
2821 static enum comparison_code
2822 comparison_to_compcode (enum tree_code code)
2823 {
2824   switch (code)
2825     {
2826     case LT_EXPR:
2827       return COMPCODE_LT;
2828     case EQ_EXPR:
2829       return COMPCODE_EQ;
2830     case LE_EXPR:
2831       return COMPCODE_LE;
2832     case GT_EXPR:
2833       return COMPCODE_GT;
2834     case NE_EXPR:
2835       return COMPCODE_NE;
2836     case GE_EXPR:
2837       return COMPCODE_GE;
2838     case ORDERED_EXPR:
2839       return COMPCODE_ORD;
2840     case UNORDERED_EXPR:
2841       return COMPCODE_UNORD;
2842     case UNLT_EXPR:
2843       return COMPCODE_UNLT;
2844     case UNEQ_EXPR:
2845       return COMPCODE_UNEQ;
2846     case UNLE_EXPR:
2847       return COMPCODE_UNLE;
2848     case UNGT_EXPR:
2849       return COMPCODE_UNGT;
2850     case LTGT_EXPR:
2851       return COMPCODE_LTGT;
2852     case UNGE_EXPR:
2853       return COMPCODE_UNGE;
2854     default:
2855       gcc_unreachable ();
2856     }
2857 }
2858
2859 /* Convert a compcode bit-based encoding of a comparison operator back
2860    to GCC's enum tree_code representation.  This function is the
2861    inverse of comparison_to_compcode.  */
2862
2863 static enum tree_code
2864 compcode_to_comparison (enum comparison_code code)
2865 {
2866   switch (code)
2867     {
2868     case COMPCODE_LT:
2869       return LT_EXPR;
2870     case COMPCODE_EQ:
2871       return EQ_EXPR;
2872     case COMPCODE_LE:
2873       return LE_EXPR;
2874     case COMPCODE_GT:
2875       return GT_EXPR;
2876     case COMPCODE_NE:
2877       return NE_EXPR;
2878     case COMPCODE_GE:
2879       return GE_EXPR;
2880     case COMPCODE_ORD:
2881       return ORDERED_EXPR;
2882     case COMPCODE_UNORD:
2883       return UNORDERED_EXPR;
2884     case COMPCODE_UNLT:
2885       return UNLT_EXPR;
2886     case COMPCODE_UNEQ:
2887       return UNEQ_EXPR;
2888     case COMPCODE_UNLE:
2889       return UNLE_EXPR;
2890     case COMPCODE_UNGT:
2891       return UNGT_EXPR;
2892     case COMPCODE_LTGT:
2893       return LTGT_EXPR;
2894     case COMPCODE_UNGE:
2895       return UNGE_EXPR;
2896     default:
2897       gcc_unreachable ();
2898     }
2899 }
2900
2901 /* Return a tree for the comparison which is the combination of
2902    doing the AND or OR (depending on CODE) of the two operations LCODE
2903    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2904    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2905    if this makes the transformation invalid.  */
2906
2907 tree
2908 combine_comparisons (enum tree_code code, enum tree_code lcode,
2909                      enum tree_code rcode, tree truth_type,
2910                      tree ll_arg, tree lr_arg)
2911 {
2912   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2913   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2914   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2915   enum comparison_code compcode;
2916
2917   switch (code)
2918     {
2919     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2920       compcode = lcompcode & rcompcode;
2921       break;
2922
2923     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2924       compcode = lcompcode | rcompcode;
2925       break;
2926
2927     default:
2928       return NULL_TREE;
2929     }
2930
2931   if (!honor_nans)
2932     {
2933       /* Eliminate unordered comparisons, as well as LTGT and ORD
2934          which are not used unless the mode has NaNs.  */
2935       compcode &= ~COMPCODE_UNORD;
2936       if (compcode == COMPCODE_LTGT)
2937         compcode = COMPCODE_NE;
2938       else if (compcode == COMPCODE_ORD)
2939         compcode = COMPCODE_TRUE;
2940     }
2941    else if (flag_trapping_math)
2942      {
2943         /* Check that the original operation and the optimized ones will trap
2944            under the same condition.  */
2945         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2946                      && (lcompcode != COMPCODE_EQ)
2947                      && (lcompcode != COMPCODE_ORD);
2948         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2949                      && (rcompcode != COMPCODE_EQ)
2950                      && (rcompcode != COMPCODE_ORD);
2951         bool trap = (compcode & COMPCODE_UNORD) == 0
2952                     && (compcode != COMPCODE_EQ)
2953                     && (compcode != COMPCODE_ORD);
2954
2955         /* In a short-circuited boolean expression the LHS might be
2956            such that the RHS, if evaluated, will never trap.  For
2957            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2958            if neither x nor y is NaN.  (This is a mixed blessing: for
2959            example, the expression above will never trap, hence
2960            optimizing it to x < y would be invalid).  */
2961         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2962             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2963           rtrap = false;
2964
2965         /* If the comparison was short-circuited, and only the RHS
2966            trapped, we may now generate a spurious trap.  */
2967         if (rtrap && !ltrap
2968             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2969           return NULL_TREE;
2970
2971         /* If we changed the conditions that cause a trap, we lose.  */
2972         if ((ltrap || rtrap) != trap)
2973           return NULL_TREE;
2974       }
2975
2976   if (compcode == COMPCODE_TRUE)
2977     return constant_boolean_node (true, truth_type);
2978   else if (compcode == COMPCODE_FALSE)
2979     return constant_boolean_node (false, truth_type);
2980   else
2981     return fold_build2 (compcode_to_comparison (compcode),
2982                         truth_type, ll_arg, lr_arg);
2983 }
2984
2985 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2986
2987 static int
2988 truth_value_p (enum tree_code code)
2989 {
2990   return (TREE_CODE_CLASS (code) == tcc_comparison
2991           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2992           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2993           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2994 }
2995 \f
2996 /* Return nonzero if two operands (typically of the same tree node)
2997    are necessarily equal.  If either argument has side-effects this
2998    function returns zero.  FLAGS modifies behavior as follows:
2999
3000    If OEP_ONLY_CONST is set, only return nonzero for constants.
3001    This function tests whether the operands are indistinguishable;
3002    it does not test whether they are equal using C's == operation.
3003    The distinction is important for IEEE floating point, because
3004    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3005    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3006
3007    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3008    even though it may hold multiple values during a function.
3009    This is because a GCC tree node guarantees that nothing else is
3010    executed between the evaluation of its "operands" (which may often
3011    be evaluated in arbitrary order).  Hence if the operands themselves
3012    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3013    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3014    unset means assuming isochronic (or instantaneous) tree equivalence.
3015    Unless comparing arbitrary expression trees, such as from different
3016    statements, this flag can usually be left unset.
3017
3018    If OEP_PURE_SAME is set, then pure functions with identical arguments
3019    are considered the same.  It is used when the caller has other ways
3020    to ensure that global memory is unchanged in between.  */
3021
3022 int
3023 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3024 {
3025   /* If either is ERROR_MARK, they aren't equal.  */
3026   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3027     return 0;
3028
3029   /* Check equality of integer constants before bailing out due to
3030      precision differences.  */
3031   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3032     return tree_int_cst_equal (arg0, arg1);
3033
3034   /* If both types don't have the same signedness, then we can't consider
3035      them equal.  We must check this before the STRIP_NOPS calls
3036      because they may change the signedness of the arguments.  */
3037   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3038     return 0;
3039
3040   /* If both types don't have the same precision, then it is not safe
3041      to strip NOPs.  */
3042   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3043     return 0;
3044
3045   STRIP_NOPS (arg0);
3046   STRIP_NOPS (arg1);
3047
3048   /* In case both args are comparisons but with different comparison
3049      code, try to swap the comparison operands of one arg to produce
3050      a match and compare that variant.  */
3051   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3052       && COMPARISON_CLASS_P (arg0)
3053       && COMPARISON_CLASS_P (arg1))
3054     {
3055       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3056
3057       if (TREE_CODE (arg0) == swap_code)
3058         return operand_equal_p (TREE_OPERAND (arg0, 0),
3059                                 TREE_OPERAND (arg1, 1), flags)
3060                && operand_equal_p (TREE_OPERAND (arg0, 1),
3061                                    TREE_OPERAND (arg1, 0), flags);
3062     }
3063
3064   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3065       /* This is needed for conversions and for COMPONENT_REF.
3066          Might as well play it safe and always test this.  */
3067       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3068       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3069       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3070     return 0;
3071
3072   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3073      We don't care about side effects in that case because the SAVE_EXPR
3074      takes care of that for us. In all other cases, two expressions are
3075      equal if they have no side effects.  If we have two identical
3076      expressions with side effects that should be treated the same due
3077      to the only side effects being identical SAVE_EXPR's, that will
3078      be detected in the recursive calls below.  */
3079   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3080       && (TREE_CODE (arg0) == SAVE_EXPR
3081           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3082     return 1;
3083
3084   /* Next handle constant cases, those for which we can return 1 even
3085      if ONLY_CONST is set.  */
3086   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3087     switch (TREE_CODE (arg0))
3088       {
3089       case INTEGER_CST:
3090         return tree_int_cst_equal (arg0, arg1);
3091
3092       case FIXED_CST:
3093         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3094                                        TREE_FIXED_CST (arg1));
3095
3096       case REAL_CST:
3097         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3098                                    TREE_REAL_CST (arg1)))
3099           return 1;
3100
3101         
3102         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3103           {
3104             /* If we do not distinguish between signed and unsigned zero,
3105                consider them equal.  */
3106             if (real_zerop (arg0) && real_zerop (arg1))
3107               return 1;
3108           }
3109         return 0;
3110
3111       case VECTOR_CST:
3112         {
3113           tree v1, v2;
3114
3115           v1 = TREE_VECTOR_CST_ELTS (arg0);
3116           v2 = TREE_VECTOR_CST_ELTS (arg1);
3117           while (v1 && v2)
3118             {
3119               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3120                                     flags))
3121                 return 0;
3122               v1 = TREE_CHAIN (v1);
3123               v2 = TREE_CHAIN (v2);
3124             }
3125
3126           return v1 == v2;
3127         }
3128
3129       case COMPLEX_CST:
3130         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3131                                  flags)
3132                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3133                                     flags));
3134
3135       case STRING_CST:
3136         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3137                 && ! memcmp (TREE_STRING_POINTER (arg0),
3138                               TREE_STRING_POINTER (arg1),
3139                               TREE_STRING_LENGTH (arg0)));
3140
3141       case ADDR_EXPR:
3142         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3143                                 0);
3144       default:
3145         break;
3146       }
3147
3148   if (flags & OEP_ONLY_CONST)
3149     return 0;
3150
3151 /* Define macros to test an operand from arg0 and arg1 for equality and a
3152    variant that allows null and views null as being different from any
3153    non-null value.  In the latter case, if either is null, the both
3154    must be; otherwise, do the normal comparison.  */
3155 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3156                                     TREE_OPERAND (arg1, N), flags)
3157
3158 #define OP_SAME_WITH_NULL(N)                            \
3159   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3160    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3161
3162   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3163     {
3164     case tcc_unary:
3165       /* Two conversions are equal only if signedness and modes match.  */
3166       switch (TREE_CODE (arg0))
3167         {
3168         case NOP_EXPR:
3169         case CONVERT_EXPR:
3170         case FIX_TRUNC_EXPR:
3171           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3172               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3173             return 0;
3174           break;
3175         default:
3176           break;
3177         }
3178
3179       return OP_SAME (0);
3180
3181
3182     case tcc_comparison:
3183     case tcc_binary:
3184       if (OP_SAME (0) && OP_SAME (1))
3185         return 1;
3186
3187       /* For commutative ops, allow the other order.  */
3188       return (commutative_tree_code (TREE_CODE (arg0))
3189               && operand_equal_p (TREE_OPERAND (arg0, 0),
3190                                   TREE_OPERAND (arg1, 1), flags)
3191               && operand_equal_p (TREE_OPERAND (arg0, 1),
3192                                   TREE_OPERAND (arg1, 0), flags));
3193
3194     case tcc_reference:
3195       /* If either of the pointer (or reference) expressions we are
3196          dereferencing contain a side effect, these cannot be equal.  */
3197       if (TREE_SIDE_EFFECTS (arg0)
3198           || TREE_SIDE_EFFECTS (arg1))
3199         return 0;
3200
3201       switch (TREE_CODE (arg0))
3202         {
3203         case INDIRECT_REF:
3204         case ALIGN_INDIRECT_REF:
3205         case MISALIGNED_INDIRECT_REF:
3206         case REALPART_EXPR:
3207         case IMAGPART_EXPR:
3208           return OP_SAME (0);
3209
3210         case ARRAY_REF:
3211         case ARRAY_RANGE_REF:
3212           /* Operands 2 and 3 may be null.
3213              Compare the array index by value if it is constant first as we
3214              may have different types but same value here.  */
3215           return (OP_SAME (0)
3216                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3217                                           TREE_OPERAND (arg1, 1))
3218                       || OP_SAME (1))
3219                   && OP_SAME_WITH_NULL (2)
3220                   && OP_SAME_WITH_NULL (3));
3221
3222         case COMPONENT_REF:
3223           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3224              may be NULL when we're called to compare MEM_EXPRs.  */
3225           return OP_SAME_WITH_NULL (0)
3226                  && OP_SAME (1)
3227                  && OP_SAME_WITH_NULL (2);
3228
3229         case BIT_FIELD_REF:
3230           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3231
3232         default:
3233           return 0;
3234         }
3235
3236     case tcc_expression:
3237       switch (TREE_CODE (arg0))
3238         {
3239         case ADDR_EXPR:
3240         case TRUTH_NOT_EXPR:
3241           return OP_SAME (0);
3242
3243         case TRUTH_ANDIF_EXPR:
3244         case TRUTH_ORIF_EXPR:
3245           return OP_SAME (0) && OP_SAME (1);
3246
3247         case TRUTH_AND_EXPR:
3248         case TRUTH_OR_EXPR:
3249         case TRUTH_XOR_EXPR:
3250           if (OP_SAME (0) && OP_SAME (1))
3251             return 1;
3252
3253           /* Otherwise take into account this is a commutative operation.  */
3254           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3255                                    TREE_OPERAND (arg1, 1), flags)
3256                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3257                                       TREE_OPERAND (arg1, 0), flags));
3258
3259         default:
3260           return 0;
3261         }
3262
3263     case tcc_vl_exp:
3264       switch (TREE_CODE (arg0))
3265         {
3266         case CALL_EXPR:
3267           /* If the CALL_EXPRs call different functions, then they
3268              clearly can not be equal.  */
3269           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3270                                  flags))
3271             return 0;
3272
3273           {
3274             unsigned int cef = call_expr_flags (arg0);
3275             if (flags & OEP_PURE_SAME)
3276               cef &= ECF_CONST | ECF_PURE;
3277             else
3278               cef &= ECF_CONST;
3279             if (!cef)
3280               return 0;
3281           }
3282
3283           /* Now see if all the arguments are the same.  */
3284           {
3285             const_call_expr_arg_iterator iter0, iter1;
3286             const_tree a0, a1;
3287             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3288                    a1 = first_const_call_expr_arg (arg1, &iter1);
3289                  a0 && a1;
3290                  a0 = next_const_call_expr_arg (&iter0),
3291                    a1 = next_const_call_expr_arg (&iter1))
3292               if (! operand_equal_p (a0, a1, flags))
3293                 return 0;
3294
3295             /* If we get here and both argument lists are exhausted
3296                then the CALL_EXPRs are equal.  */
3297             return ! (a0 || a1);
3298           }
3299         default:
3300           return 0;
3301         }
3302
3303     case tcc_declaration:
3304       /* Consider __builtin_sqrt equal to sqrt.  */
3305       return (TREE_CODE (arg0) == FUNCTION_DECL
3306               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3307               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3308               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3309
3310     default:
3311       return 0;
3312     }
3313
3314 #undef OP_SAME
3315 #undef OP_SAME_WITH_NULL
3316 }
3317 \f
3318 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3319    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3320
3321    When in doubt, return 0.  */
3322
3323 static int
3324 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3325 {
3326   int unsignedp1, unsignedpo;
3327   tree primarg0, primarg1, primother;
3328   unsigned int correct_width;
3329
3330   if (operand_equal_p (arg0, arg1, 0))
3331     return 1;
3332
3333   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3334       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3335     return 0;
3336
3337   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3338      and see if the inner values are the same.  This removes any
3339      signedness comparison, which doesn't matter here.  */
3340   primarg0 = arg0, primarg1 = arg1;
3341   STRIP_NOPS (primarg0);
3342   STRIP_NOPS (primarg1);
3343   if (operand_equal_p (primarg0, primarg1, 0))
3344     return 1;
3345
3346   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3347      actual comparison operand, ARG0.
3348
3349      First throw away any conversions to wider types
3350      already present in the operands.  */
3351
3352   primarg1 = get_narrower (arg1, &unsignedp1);
3353   primother = get_narrower (other, &unsignedpo);
3354
3355   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3356   if (unsignedp1 == unsignedpo
3357       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3358       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3359     {
3360       tree type = TREE_TYPE (arg0);
3361
3362       /* Make sure shorter operand is extended the right way
3363          to match the longer operand.  */
3364       primarg1 = fold_convert (signed_or_unsigned_type_for
3365                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3366
3367       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3368         return 1;
3369     }
3370
3371   return 0;
3372 }
3373 \f
3374 /* See if ARG is an expression that is either a comparison or is performing
3375    arithmetic on comparisons.  The comparisons must only be comparing
3376    two different values, which will be stored in *CVAL1 and *CVAL2; if
3377    they are nonzero it means that some operands have already been found.
3378    No variables may be used anywhere else in the expression except in the
3379    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3380    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3381
3382    If this is true, return 1.  Otherwise, return zero.  */
3383
3384 static int
3385 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3386 {
3387   enum tree_code code = TREE_CODE (arg);
3388   enum tree_code_class class = TREE_CODE_CLASS (code);
3389
3390   /* We can handle some of the tcc_expression cases here.  */
3391   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3392     class = tcc_unary;
3393   else if (class == tcc_expression
3394            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3395                || code == COMPOUND_EXPR))
3396     class = tcc_binary;
3397
3398   else if (class == tcc_expression && code == SAVE_EXPR
3399            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3400     {
3401       /* If we've already found a CVAL1 or CVAL2, this expression is
3402          two complex to handle.  */
3403       if (*cval1 || *cval2)
3404         return 0;
3405
3406       class = tcc_unary;
3407       *save_p = 1;
3408     }
3409
3410   switch (class)
3411     {
3412     case tcc_unary:
3413       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3414
3415     case tcc_binary:
3416       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3417               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3418                                       cval1, cval2, save_p));
3419
3420     case tcc_constant:
3421       return 1;
3422
3423     case tcc_expression:
3424       if (code == COND_EXPR)
3425         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3426                                      cval1, cval2, save_p)
3427                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3428                                         cval1, cval2, save_p)
3429                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3430                                         cval1, cval2, save_p));
3431       return 0;
3432
3433     case tcc_comparison:
3434       /* First see if we can handle the first operand, then the second.  For
3435          the second operand, we know *CVAL1 can't be zero.  It must be that
3436          one side of the comparison is each of the values; test for the
3437          case where this isn't true by failing if the two operands
3438          are the same.  */
3439
3440       if (operand_equal_p (TREE_OPERAND (arg, 0),
3441                            TREE_OPERAND (arg, 1), 0))
3442         return 0;
3443
3444       if (*cval1 == 0)
3445         *cval1 = TREE_OPERAND (arg, 0);
3446       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3447         ;
3448       else if (*cval2 == 0)
3449         *cval2 = TREE_OPERAND (arg, 0);
3450       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3451         ;
3452       else
3453         return 0;
3454
3455       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3456         ;
3457       else if (*cval2 == 0)
3458         *cval2 = TREE_OPERAND (arg, 1);
3459       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3460         ;
3461       else
3462         return 0;
3463
3464       return 1;
3465
3466     default:
3467       return 0;
3468     }
3469 }
3470 \f
3471 /* ARG is a tree that is known to contain just arithmetic operations and
3472    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3473    any occurrence of OLD0 as an operand of a comparison and likewise for
3474    NEW1 and OLD1.  */
3475
3476 static tree
3477 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3478 {
3479   tree type = TREE_TYPE (arg);
3480   enum tree_code code = TREE_CODE (arg);
3481   enum tree_code_class class = TREE_CODE_CLASS (code);
3482
3483   /* We can handle some of the tcc_expression cases here.  */
3484   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3485     class = tcc_unary;
3486   else if (class == tcc_expression
3487            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3488     class = tcc_binary;
3489
3490   switch (class)
3491     {
3492     case tcc_unary:
3493       return fold_build1 (code, type,
3494                           eval_subst (TREE_OPERAND (arg, 0),
3495                                       old0, new0, old1, new1));
3496
3497     case tcc_binary:
3498       return fold_build2 (code, type,
3499                           eval_subst (TREE_OPERAND (arg, 0),
3500                                       old0, new0, old1, new1),
3501                           eval_subst (TREE_OPERAND (arg, 1),
3502                                       old0, new0, old1, new1));
3503
3504     case tcc_expression:
3505       switch (code)
3506         {
3507         case SAVE_EXPR:
3508           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3509
3510         case COMPOUND_EXPR:
3511           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3512
3513         case COND_EXPR:
3514           return fold_build3 (code, type,
3515                               eval_subst (TREE_OPERAND (arg, 0),
3516                                           old0, new0, old1, new1),
3517                               eval_subst (TREE_OPERAND (arg, 1),
3518                                           old0, new0, old1, new1),
3519                               eval_subst (TREE_OPERAND (arg, 2),
3520                                           old0, new0, old1, new1));
3521         default:
3522           break;
3523         }
3524       /* Fall through - ???  */
3525
3526     case tcc_comparison:
3527       {
3528         tree arg0 = TREE_OPERAND (arg, 0);
3529         tree arg1 = TREE_OPERAND (arg, 1);
3530
3531         /* We need to check both for exact equality and tree equality.  The
3532            former will be true if the operand has a side-effect.  In that
3533            case, we know the operand occurred exactly once.  */
3534
3535         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3536           arg0 = new0;
3537         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3538           arg0 = new1;
3539
3540         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3541           arg1 = new0;
3542         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3543           arg1 = new1;
3544
3545         return fold_build2 (code, type, arg0, arg1);
3546       }
3547
3548     default:
3549       return arg;
3550     }
3551 }
3552 \f
3553 /* Return a tree for the case when the result of an expression is RESULT
3554    converted to TYPE and OMITTED was previously an operand of the expression
3555    but is now not needed (e.g., we folded OMITTED * 0).
3556
3557    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3558    the conversion of RESULT to TYPE.  */
3559
3560 tree
3561 omit_one_operand (tree type, tree result, tree omitted)
3562 {
3563   tree t = fold_convert (type, result);
3564
3565   /* If the resulting operand is an empty statement, just return the omitted
3566      statement casted to void. */
3567   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3568     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3569
3570   if (TREE_SIDE_EFFECTS (omitted))
3571     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3572
3573   return non_lvalue (t);
3574 }
3575
3576 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3577
3578 static tree
3579 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3580 {
3581   tree t = fold_convert (type, result);
3582
3583   /* If the resulting operand is an empty statement, just return the omitted
3584      statement casted to void. */
3585   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3586     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3587
3588   if (TREE_SIDE_EFFECTS (omitted))
3589     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3590
3591   return pedantic_non_lvalue (t);
3592 }
3593
3594 /* Return a tree for the case when the result of an expression is RESULT
3595    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3596    of the expression but are now not needed.
3597
3598    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3599    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3600    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3601    just do the conversion of RESULT to TYPE.  */
3602
3603 tree
3604 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3605 {
3606   tree t = fold_convert (type, result);
3607
3608   if (TREE_SIDE_EFFECTS (omitted2))
3609     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3610   if (TREE_SIDE_EFFECTS (omitted1))
3611     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3612
3613   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3614 }
3615
3616 \f
3617 /* Return a simplified tree node for the truth-negation of ARG.  This
3618    never alters ARG itself.  We assume that ARG is an operation that
3619    returns a truth value (0 or 1).
3620
3621    FIXME: one would think we would fold the result, but it causes
3622    problems with the dominator optimizer.  */
3623
3624 tree
3625 fold_truth_not_expr (tree arg)
3626 {
3627   tree type = TREE_TYPE (arg);
3628   enum tree_code code = TREE_CODE (arg);
3629
3630   /* If this is a comparison, we can simply invert it, except for
3631      floating-point non-equality comparisons, in which case we just
3632      enclose a TRUTH_NOT_EXPR around what we have.  */
3633
3634   if (TREE_CODE_CLASS (code) == tcc_comparison)
3635     {
3636       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3637       if (FLOAT_TYPE_P (op_type)
3638           && flag_trapping_math
3639           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3640           && code != NE_EXPR && code != EQ_EXPR)
3641         return NULL_TREE;
3642       else
3643         {
3644           code = invert_tree_comparison (code,
3645                                          HONOR_NANS (TYPE_MODE (op_type)));
3646           if (code == ERROR_MARK)
3647             return NULL_TREE;
3648           else
3649             return build2 (code, type,
3650                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3651         }
3652     }
3653
3654   switch (code)
3655     {
3656     case INTEGER_CST:
3657       return constant_boolean_node (integer_zerop (arg), type);
3658
3659     case TRUTH_AND_EXPR:
3660       return build2 (TRUTH_OR_EXPR, type,
3661                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3662                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3663
3664     case TRUTH_OR_EXPR:
3665       return build2 (TRUTH_AND_EXPR, type,
3666                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3667                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3668
3669     case TRUTH_XOR_EXPR:
3670       /* Here we can invert either operand.  We invert the first operand
3671          unless the second operand is a TRUTH_NOT_EXPR in which case our
3672          result is the XOR of the first operand with the inside of the
3673          negation of the second operand.  */
3674
3675       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3676         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3677                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3678       else
3679         return build2 (TRUTH_XOR_EXPR, type,
3680                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3681                        TREE_OPERAND (arg, 1));
3682
3683     case TRUTH_ANDIF_EXPR:
3684       return build2 (TRUTH_ORIF_EXPR, type,
3685                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3686                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3687
3688     case TRUTH_ORIF_EXPR:
3689       return build2 (TRUTH_ANDIF_EXPR, type,
3690                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3691                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3692
3693     case TRUTH_NOT_EXPR:
3694       return TREE_OPERAND (arg, 0);
3695
3696     case COND_EXPR:
3697       {
3698         tree arg1 = TREE_OPERAND (arg, 1);
3699         tree arg2 = TREE_OPERAND (arg, 2);
3700         /* A COND_EXPR may have a throw as one operand, which
3701            then has void type.  Just leave void operands
3702            as they are.  */
3703         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3704                        VOID_TYPE_P (TREE_TYPE (arg1))
3705                        ? arg1 : invert_truthvalue (arg1),
3706                        VOID_TYPE_P (TREE_TYPE (arg2))
3707                        ? arg2 : invert_truthvalue (arg2));
3708       }
3709
3710     case COMPOUND_EXPR:
3711       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3712                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3713
3714     case NON_LVALUE_EXPR:
3715       return invert_truthvalue (TREE_OPERAND (arg, 0));
3716
3717     case NOP_EXPR:
3718       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3719         return build1 (TRUTH_NOT_EXPR, type, arg);
3720
3721     case CONVERT_EXPR:
3722     case FLOAT_EXPR:
3723       return build1 (TREE_CODE (arg), type,
3724                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3725
3726     case BIT_AND_EXPR:
3727       if (!integer_onep (TREE_OPERAND (arg, 1)))
3728         break;
3729       return build2 (EQ_EXPR, type, arg,
3730                      build_int_cst (type, 0));
3731
3732     case SAVE_EXPR:
3733       return build1 (TRUTH_NOT_EXPR, type, arg);
3734
3735     case CLEANUP_POINT_EXPR:
3736       return build1 (CLEANUP_POINT_EXPR, type,
3737                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3738
3739     default:
3740       break;
3741     }
3742
3743   return NULL_TREE;
3744 }
3745
3746 /* Return a simplified tree node for the truth-negation of ARG.  This
3747    never alters ARG itself.  We assume that ARG is an operation that
3748    returns a truth value (0 or 1).
3749
3750    FIXME: one would think we would fold the result, but it causes
3751    problems with the dominator optimizer.  */
3752
3753 tree
3754 invert_truthvalue (tree arg)
3755 {
3756   tree tem;
3757
3758   if (TREE_CODE (arg) == ERROR_MARK)
3759     return arg;
3760
3761   tem = fold_truth_not_expr (arg);
3762   if (!tem)
3763     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3764
3765   return tem;
3766 }
3767
3768 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3769    operands are another bit-wise operation with a common input.  If so,
3770    distribute the bit operations to save an operation and possibly two if
3771    constants are involved.  For example, convert
3772         (A | B) & (A | C) into A | (B & C)
3773    Further simplification will occur if B and C are constants.
3774
3775    If this optimization cannot be done, 0 will be returned.  */
3776
3777 static tree
3778 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3779 {
3780   tree common;
3781   tree left, right;
3782
3783   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3784       || TREE_CODE (arg0) == code
3785       || (TREE_CODE (arg0) != BIT_AND_EXPR
3786           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3787     return 0;
3788
3789   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3790     {
3791       common = TREE_OPERAND (arg0, 0);
3792       left = TREE_OPERAND (arg0, 1);
3793       right = TREE_OPERAND (arg1, 1);
3794     }
3795   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3796     {
3797       common = TREE_OPERAND (arg0, 0);
3798       left = TREE_OPERAND (arg0, 1);
3799       right = TREE_OPERAND (arg1, 0);
3800     }
3801   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3802     {
3803       common = TREE_OPERAND (arg0, 1);
3804       left = TREE_OPERAND (arg0, 0);
3805       right = TREE_OPERAND (arg1, 1);
3806     }
3807   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3808     {
3809       common = TREE_OPERAND (arg0, 1);
3810       left = TREE_OPERAND (arg0, 0);
3811       right = TREE_OPERAND (arg1, 0);
3812     }
3813   else
3814     return 0;
3815
3816   return fold_build2 (TREE_CODE (arg0), type, common,
3817                       fold_build2 (code, type, left, right));
3818 }
3819
3820 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3821    with code CODE.  This optimization is unsafe.  */
3822 static tree
3823 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3824 {
3825   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3826   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3827
3828   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3829   if (mul0 == mul1
3830       && operand_equal_p (TREE_OPERAND (arg0, 1),
3831                        TREE_OPERAND (arg1, 1), 0))
3832     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3833                         fold_build2 (code, type,
3834                                      TREE_OPERAND (arg0, 0),
3835                                      TREE_OPERAND (arg1, 0)),
3836                         TREE_OPERAND (arg0, 1));
3837
3838   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3839   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3840                        TREE_OPERAND (arg1, 0), 0)
3841       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3842       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3843     {
3844       REAL_VALUE_TYPE r0, r1;
3845       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3846       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3847       if (!mul0)
3848         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3849       if (!mul1)
3850         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3851       real_arithmetic (&r0, code, &r0, &r1);
3852       return fold_build2 (MULT_EXPR, type,
3853                           TREE_OPERAND (arg0, 0),
3854                           build_real (type, r0));
3855     }
3856
3857   return NULL_TREE;
3858 }
3859 \f
3860 /* Subroutine for fold_truthop: decode a field reference.
3861
3862    If EXP is a comparison reference, we return the innermost reference.
3863
3864    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3865    set to the starting bit number.
3866
3867    If the innermost field can be completely contained in a mode-sized
3868    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3869
3870    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3871    otherwise it is not changed.
3872
3873    *PUNSIGNEDP is set to the signedness of the field.
3874
3875    *PMASK is set to the mask used.  This is either contained in a
3876    BIT_AND_EXPR or derived from the width of the field.
3877
3878    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3879
3880    Return 0 if this is not a component reference or is one that we can't
3881    do anything with.  */
3882
3883 static tree
3884 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3885                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3886                         int *punsignedp, int *pvolatilep,
3887                         tree *pmask, tree *pand_mask)
3888 {
3889   tree outer_type = 0;
3890   tree and_mask = 0;
3891   tree mask, inner, offset;
3892   tree unsigned_type;
3893   unsigned int precision;
3894
3895   /* All the optimizations using this function assume integer fields.
3896      There are problems with FP fields since the type_for_size call
3897      below can fail for, e.g., XFmode.  */
3898   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3899     return 0;
3900
3901   /* We are interested in the bare arrangement of bits, so strip everything
3902      that doesn't affect the machine mode.  However, record the type of the
3903      outermost expression if it may matter below.  */
3904   if (TREE_CODE (exp) == NOP_EXPR
3905       || TREE_CODE (exp) == CONVERT_EXPR
3906       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3907     outer_type = TREE_TYPE (exp);
3908   STRIP_NOPS (exp);
3909
3910   if (TREE_CODE (exp) == BIT_AND_EXPR)
3911     {
3912       and_mask = TREE_OPERAND (exp, 1);
3913       exp = TREE_OPERAND (exp, 0);
3914       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3915       if (TREE_CODE (and_mask) != INTEGER_CST)
3916         return 0;
3917     }
3918
3919   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3920                                punsignedp, pvolatilep, false);
3921   if ((inner == exp && and_mask == 0)
3922       || *pbitsize < 0 || offset != 0
3923       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3924     return 0;
3925
3926   /* If the number of bits in the reference is the same as the bitsize of
3927      the outer type, then the outer type gives the signedness. Otherwise
3928      (in case of a small bitfield) the signedness is unchanged.  */
3929   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3930     *punsignedp = TYPE_UNSIGNED (outer_type);
3931
3932   /* Compute the mask to access the bitfield.  */
3933   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3934   precision = TYPE_PRECISION (unsigned_type);
3935
3936   mask = build_int_cst_type (unsigned_type, -1);
3937
3938   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3939   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3940
3941   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3942   if (and_mask != 0)
3943     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3944                         fold_convert (unsigned_type, and_mask), mask);
3945
3946   *pmask = mask;
3947   *pand_mask = and_mask;
3948   return inner;
3949 }
3950
3951 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3952    represents the sign bit of EXP's type.  If EXP represents a sign
3953    or zero extension, also test VAL against the unextended type.
3954    The return value is the (sub)expression whose sign bit is VAL,
3955    or NULL_TREE otherwise.  */
3956
3957 static tree
3958 sign_bit_p (tree exp, const_tree val)
3959 {
3960   unsigned HOST_WIDE_INT mask_lo, lo;
3961   HOST_WIDE_INT mask_hi, hi;
3962   int width;
3963   tree t;
3964
3965   /* Tree EXP must have an integral type.  */
3966   t = TREE_TYPE (exp);
3967   if (! INTEGRAL_TYPE_P (t))
3968     return NULL_TREE;
3969
3970   /* Tree VAL must be an integer constant.  */
3971   if (TREE_CODE (val) != INTEGER_CST
3972       || TREE_OVERFLOW (val))
3973     return NULL_TREE;
3974
3975   width = TYPE_PRECISION (t);
3976   if (width > HOST_BITS_PER_WIDE_INT)
3977     {
3978       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3979       lo = 0;
3980
3981       mask_hi = ((unsigned HOST_WIDE_INT) -1
3982                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3983       mask_lo = -1;
3984     }
3985   else
3986     {
3987       hi = 0;
3988       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3989
3990       mask_hi = 0;
3991       mask_lo = ((unsigned HOST_WIDE_INT) -1
3992                  >> (HOST_BITS_PER_WIDE_INT - width));
3993     }
3994
3995   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3996      treat VAL as if it were unsigned.  */
3997   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3998       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3999     return exp;
4000
4001   /* Handle extension from a narrower type.  */
4002   if (TREE_CODE (exp) == NOP_EXPR
4003       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4004     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4005
4006   return NULL_TREE;
4007 }
4008
4009 /* Subroutine for fold_truthop: determine if an operand is simple enough
4010    to be evaluated unconditionally.  */
4011
4012 static int
4013 simple_operand_p (const_tree exp)
4014 {
4015   /* Strip any conversions that don't change the machine mode.  */
4016   STRIP_NOPS (exp);
4017
4018   return (CONSTANT_CLASS_P (exp)
4019           || TREE_CODE (exp) == SSA_NAME
4020           || (DECL_P (exp)
4021               && ! TREE_ADDRESSABLE (exp)
4022               && ! TREE_THIS_VOLATILE (exp)
4023               && ! DECL_NONLOCAL (exp)
4024               /* Don't regard global variables as simple.  They may be
4025                  allocated in ways unknown to the compiler (shared memory,
4026                  #pragma weak, etc).  */
4027               && ! TREE_PUBLIC (exp)
4028               && ! DECL_EXTERNAL (exp)
4029               /* Loading a static variable is unduly expensive, but global
4030                  registers aren't expensive.  */
4031               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4032 }
4033 \f
4034 /* The following functions are subroutines to fold_range_test and allow it to
4035    try to change a logical combination of comparisons into a range test.
4036
4037    For example, both
4038         X == 2 || X == 3 || X == 4 || X == 5
4039    and
4040         X >= 2 && X <= 5
4041    are converted to
4042         (unsigned) (X - 2) <= 3
4043
4044    We describe each set of comparisons as being either inside or outside
4045    a range, using a variable named like IN_P, and then describe the
4046    range with a lower and upper bound.  If one of the bounds is omitted,
4047    it represents either the highest or lowest value of the type.
4048
4049    In the comments below, we represent a range by two numbers in brackets
4050    preceded by a "+" to designate being inside that range, or a "-" to
4051    designate being outside that range, so the condition can be inverted by
4052    flipping the prefix.  An omitted bound is represented by a "-".  For
4053    example, "- [-, 10]" means being outside the range starting at the lowest
4054    possible value and ending at 10, in other words, being greater than 10.
4055    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4056    always false.
4057
4058    We set up things so that the missing bounds are handled in a consistent
4059    manner so neither a missing bound nor "true" and "false" need to be
4060    handled using a special case.  */
4061
4062 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4063    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4064    and UPPER1_P are nonzero if the respective argument is an upper bound
4065    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4066    must be specified for a comparison.  ARG1 will be converted to ARG0's
4067    type if both are specified.  */
4068
4069 static tree
4070 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4071              tree arg1, int upper1_p)
4072 {
4073   tree tem;
4074   int result;
4075   int sgn0, sgn1;
4076
4077   /* If neither arg represents infinity, do the normal operation.
4078      Else, if not a comparison, return infinity.  Else handle the special
4079      comparison rules. Note that most of the cases below won't occur, but
4080      are handled for consistency.  */
4081
4082   if (arg0 != 0 && arg1 != 0)
4083     {
4084       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4085                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4086       STRIP_NOPS (tem);
4087       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4088     }
4089
4090   if (TREE_CODE_CLASS (code) != tcc_comparison)
4091     return 0;
4092
4093   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4094      for neither.  In real maths, we cannot assume open ended ranges are
4095      the same. But, this is computer arithmetic, where numbers are finite.
4096      We can therefore make the transformation of any unbounded range with
4097      the value Z, Z being greater than any representable number. This permits
4098      us to treat unbounded ranges as equal.  */
4099   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4100   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4101   switch (code)
4102     {
4103     case EQ_EXPR:
4104       result = sgn0 == sgn1;
4105       break;
4106     case NE_EXPR:
4107       result = sgn0 != sgn1;
4108       break;
4109     case LT_EXPR:
4110       result = sgn0 < sgn1;
4111       break;
4112     case LE_EXPR:
4113       result = sgn0 <= sgn1;
4114       break;
4115     case GT_EXPR:
4116       result = sgn0 > sgn1;
4117       break;
4118     case GE_EXPR:
4119       result = sgn0 >= sgn1;
4120       break;
4121     default:
4122       gcc_unreachable ();
4123     }
4124
4125   return constant_boolean_node (result, type);
4126 }
4127 \f
4128 /* Given EXP, a logical expression, set the range it is testing into
4129    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4130    actually being tested.  *PLOW and *PHIGH will be made of the same
4131    type as the returned expression.  If EXP is not a comparison, we
4132    will most likely not be returning a useful value and range.  Set
4133    *STRICT_OVERFLOW_P to true if the return value is only valid
4134    because signed overflow is undefined; otherwise, do not change
4135    *STRICT_OVERFLOW_P.  */
4136
4137 static tree
4138 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4139             bool *strict_overflow_p)
4140 {
4141   enum tree_code code;
4142   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4143   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4144   int in_p, n_in_p;
4145   tree low, high, n_low, n_high;
4146
4147   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4148      and see if we can refine the range.  Some of the cases below may not
4149      happen, but it doesn't seem worth worrying about this.  We "continue"
4150      the outer loop when we've changed something; otherwise we "break"
4151      the switch, which will "break" the while.  */
4152
4153   in_p = 0;
4154   low = high = build_int_cst (TREE_TYPE (exp), 0);
4155
4156   while (1)
4157     {
4158       code = TREE_CODE (exp);
4159       exp_type = TREE_TYPE (exp);
4160
4161       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4162         {
4163           if (TREE_OPERAND_LENGTH (exp) > 0)
4164             arg0 = TREE_OPERAND (exp, 0);
4165           if (TREE_CODE_CLASS (code) == tcc_comparison
4166               || TREE_CODE_CLASS (code) == tcc_unary
4167               || TREE_CODE_CLASS (code) == tcc_binary)
4168             arg0_type = TREE_TYPE (arg0);
4169           if (TREE_CODE_CLASS (code) == tcc_binary
4170               || TREE_CODE_CLASS (code) == tcc_comparison
4171               || (TREE_CODE_CLASS (code) == tcc_expression
4172                   && TREE_OPERAND_LENGTH (exp) > 1))
4173             arg1 = TREE_OPERAND (exp, 1);
4174         }
4175
4176       switch (code)
4177         {
4178         case TRUTH_NOT_EXPR:
4179           in_p = ! in_p, exp = arg0;
4180           continue;
4181
4182         case EQ_EXPR: case NE_EXPR:
4183         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4184           /* We can only do something if the range is testing for zero
4185              and if the second operand is an integer constant.  Note that
4186              saying something is "in" the range we make is done by
4187              complementing IN_P since it will set in the initial case of
4188              being not equal to zero; "out" is leaving it alone.  */
4189           if (low == 0 || high == 0
4190               || ! integer_zerop (low) || ! integer_zerop (high)
4191               || TREE_CODE (arg1) != INTEGER_CST)
4192             break;
4193
4194           switch (code)
4195             {
4196             case NE_EXPR:  /* - [c, c]  */
4197               low = high = arg1;
4198               break;
4199             case EQ_EXPR:  /* + [c, c]  */
4200               in_p = ! in_p, low = high = arg1;
4201               break;
4202             case GT_EXPR:  /* - [-, c] */
4203               low = 0, high = arg1;
4204               break;
4205             case GE_EXPR:  /* + [c, -] */
4206               in_p = ! in_p, low = arg1, high = 0;
4207               break;
4208             case LT_EXPR:  /* - [c, -] */
4209               low = arg1, high = 0;
4210               break;
4211             case LE_EXPR:  /* + [-, c] */
4212               in_p = ! in_p, low = 0, high = arg1;
4213               break;
4214             default:
4215               gcc_unreachable ();
4216             }
4217
4218           /* If this is an unsigned comparison, we also know that EXP is
4219              greater than or equal to zero.  We base the range tests we make
4220              on that fact, so we record it here so we can parse existing
4221              range tests.  We test arg0_type since often the return type
4222              of, e.g. EQ_EXPR, is boolean.  */
4223           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4224             {
4225               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4226                                   in_p, low, high, 1,
4227                                   build_int_cst (arg0_type, 0),
4228                                   NULL_TREE))
4229                 break;
4230
4231               in_p = n_in_p, low = n_low, high = n_high;
4232
4233               /* If the high bound is missing, but we have a nonzero low
4234                  bound, reverse the range so it goes from zero to the low bound
4235                  minus 1.  */
4236               if (high == 0 && low && ! integer_zerop (low))
4237                 {
4238                   in_p = ! in_p;
4239                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4240                                       integer_one_node, 0);
4241                   low = build_int_cst (arg0_type, 0);
4242                 }
4243             }
4244
4245           exp = arg0;
4246           continue;
4247
4248         case NEGATE_EXPR:
4249           /* (-x) IN [a,b] -> x in [-b, -a]  */
4250           n_low = range_binop (MINUS_EXPR, exp_type,
4251                                build_int_cst (exp_type, 0),
4252                                0, high, 1);
4253           n_high = range_binop (MINUS_EXPR, exp_type,
4254                                 build_int_cst (exp_type, 0),
4255                                 0, low, 0);
4256           low = n_low, high = n_high;
4257           exp = arg0;
4258           continue;
4259
4260         case BIT_NOT_EXPR:
4261           /* ~ X -> -X - 1  */
4262           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4263                         build_int_cst (exp_type, 1));
4264           continue;
4265
4266         case PLUS_EXPR:  case MINUS_EXPR:
4267           if (TREE_CODE (arg1) != INTEGER_CST)
4268             break;
4269
4270           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4271              move a constant to the other side.  */
4272           if (!TYPE_UNSIGNED (arg0_type)
4273               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4274             break;
4275
4276           /* If EXP is signed, any overflow in the computation is undefined,
4277              so we don't worry about it so long as our computations on
4278              the bounds don't overflow.  For unsigned, overflow is defined
4279              and this is exactly the right thing.  */
4280           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4281                                arg0_type, low, 0, arg1, 0);
4282           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4283                                 arg0_type, high, 1, arg1, 0);
4284           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4285               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4286             break;
4287
4288           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4289             *strict_overflow_p = true;
4290
4291           /* Check for an unsigned range which has wrapped around the maximum
4292              value thus making n_high < n_low, and normalize it.  */
4293           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4294             {
4295               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4296                                  integer_one_node, 0);
4297               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4298                                   integer_one_node, 0);
4299
4300               /* If the range is of the form +/- [ x+1, x ], we won't
4301                  be able to normalize it.  But then, it represents the
4302                  whole range or the empty set, so make it
4303                  +/- [ -, - ].  */
4304               if (tree_int_cst_equal (n_low, low)
4305                   && tree_int_cst_equal (n_high, high))
4306                 low = high = 0;
4307               else
4308                 in_p = ! in_p;
4309             }
4310           else
4311             low = n_low, high = n_high;
4312
4313           exp = arg0;
4314           continue;
4315
4316         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
4317           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4318             break;
4319
4320           if (! INTEGRAL_TYPE_P (arg0_type)
4321               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4322               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4323             break;
4324
4325           n_low = low, n_high = high;
4326
4327           if (n_low != 0)
4328             n_low = fold_convert (arg0_type, n_low);
4329
4330           if (n_high != 0)
4331             n_high = fold_convert (arg0_type, n_high);
4332
4333
4334           /* If we're converting arg0 from an unsigned type, to exp,
4335              a signed type,  we will be doing the comparison as unsigned.
4336              The tests above have already verified that LOW and HIGH
4337              are both positive.
4338
4339              So we have to ensure that we will handle large unsigned
4340              values the same way that the current signed bounds treat
4341              negative values.  */
4342
4343           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4344             {
4345               tree high_positive;
4346               tree equiv_type;
4347               /* For fixed-point modes, we need to pass the saturating flag
4348                  as the 2nd parameter.  */
4349               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4350                 equiv_type = lang_hooks.types.type_for_mode
4351                              (TYPE_MODE (arg0_type),
4352                               TYPE_SATURATING (arg0_type));
4353               else
4354                 equiv_type = lang_hooks.types.type_for_mode
4355                              (TYPE_MODE (arg0_type), 1);
4356
4357               /* A range without an upper bound is, naturally, unbounded.
4358                  Since convert would have cropped a very large value, use
4359                  the max value for the destination type.  */
4360               high_positive
4361                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4362                 : TYPE_MAX_VALUE (arg0_type);
4363
4364               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4365                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4366                                              fold_convert (arg0_type,
4367                                                            high_positive),
4368                                              build_int_cst (arg0_type, 1));
4369
4370               /* If the low bound is specified, "and" the range with the
4371                  range for which the original unsigned value will be
4372                  positive.  */
4373               if (low != 0)
4374                 {
4375                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4376                                       1, n_low, n_high, 1,
4377                                       fold_convert (arg0_type,
4378                                                     integer_zero_node),
4379                                       high_positive))
4380                     break;
4381
4382                   in_p = (n_in_p == in_p);
4383                 }
4384               else
4385                 {
4386                   /* Otherwise, "or" the range with the range of the input
4387                      that will be interpreted as negative.  */
4388                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4389                                       0, n_low, n_high, 1,
4390                                       fold_convert (arg0_type,
4391                                                     integer_zero_node),
4392                                       high_positive))
4393                     break;
4394
4395                   in_p = (in_p != n_in_p);
4396                 }
4397             }
4398
4399           exp = arg0;
4400           low = n_low, high = n_high;
4401           continue;
4402
4403         default:
4404           break;
4405         }
4406
4407       break;
4408     }
4409
4410   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4411   if (TREE_CODE (exp) == INTEGER_CST)
4412     {
4413       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4414                                                  exp, 0, low, 0))
4415                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4416                                                     exp, 1, high, 1)));
4417       low = high = 0;
4418       exp = 0;
4419     }
4420
4421   *pin_p = in_p, *plow = low, *phigh = high;
4422   return exp;
4423 }
4424 \f
4425 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4426    type, TYPE, return an expression to test if EXP is in (or out of, depending
4427    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4428
4429 static tree
4430 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4431 {
4432   tree etype = TREE_TYPE (exp);
4433   tree value;
4434
4435 #ifdef HAVE_canonicalize_funcptr_for_compare
4436   /* Disable this optimization for function pointer expressions
4437      on targets that require function pointer canonicalization.  */
4438   if (HAVE_canonicalize_funcptr_for_compare
4439       && TREE_CODE (etype) == POINTER_TYPE
4440       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4441     return NULL_TREE;
4442 #endif
4443
4444   if (! in_p)
4445     {
4446       value = build_range_check (type, exp, 1, low, high);
4447       if (value != 0)
4448         return invert_truthvalue (value);
4449
4450       return 0;
4451     }
4452
4453   if (low == 0 && high == 0)
4454     return build_int_cst (type, 1);
4455
4456   if (low == 0)
4457     return fold_build2 (LE_EXPR, type, exp,
4458                         fold_convert (etype, high));
4459
4460   if (high == 0)
4461     return fold_build2 (GE_EXPR, type, exp,
4462                         fold_convert (etype, low));
4463
4464   if (operand_equal_p (low, high, 0))
4465     return fold_build2 (EQ_EXPR, type, exp,
4466                         fold_convert (etype, low));
4467
4468   if (integer_zerop (low))
4469     {
4470       if (! TYPE_UNSIGNED (etype))
4471         {
4472           etype = unsigned_type_for (etype);
4473           high = fold_convert (etype, high);
4474           exp = fold_convert (etype, exp);
4475         }
4476       return build_range_check (type, exp, 1, 0, high);
4477     }
4478
4479   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4480   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4481     {
4482       unsigned HOST_WIDE_INT lo;
4483       HOST_WIDE_INT hi;
4484       int prec;
4485
4486       prec = TYPE_PRECISION (etype);
4487       if (prec <= HOST_BITS_PER_WIDE_INT)
4488         {
4489           hi = 0;
4490           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4491         }
4492       else
4493         {
4494           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4495           lo = (unsigned HOST_WIDE_INT) -1;
4496         }
4497
4498       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4499         {
4500           if (TYPE_UNSIGNED (etype))
4501             {
4502               etype = signed_type_for (etype);
4503               exp = fold_convert (etype, exp);
4504             }
4505           return fold_build2 (GT_EXPR, type, exp,
4506                               build_int_cst (etype, 0));
4507         }
4508     }
4509
4510   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4511      This requires wrap-around arithmetics for the type of the expression.  */
4512   switch (TREE_CODE (etype))
4513     {
4514     case INTEGER_TYPE:
4515       /* There is no requirement that LOW be within the range of ETYPE
4516          if the latter is a subtype.  It must, however, be within the base
4517          type of ETYPE.  So be sure we do the subtraction in that type.  */
4518       if (TREE_TYPE (etype))
4519         etype = TREE_TYPE (etype);
4520       break;
4521
4522     case ENUMERAL_TYPE:
4523     case BOOLEAN_TYPE:
4524       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4525                                               TYPE_UNSIGNED (etype));
4526       break;
4527
4528     default:
4529       break;
4530     }
4531
4532   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4533   if (TREE_CODE (etype) == INTEGER_TYPE
4534       && !TYPE_OVERFLOW_WRAPS (etype))
4535     {
4536       tree utype, minv, maxv;
4537
4538       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4539          for the type in question, as we rely on this here.  */
4540       utype = unsigned_type_for (etype);
4541       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4542       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4543                           integer_one_node, 1);
4544       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4545
4546       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4547                                       minv, 1, maxv, 1)))
4548         etype = utype;
4549       else
4550         return 0;
4551     }
4552
4553   high = fold_convert (etype, high);
4554   low = fold_convert (etype, low);
4555   exp = fold_convert (etype, exp);
4556
4557   value = const_binop (MINUS_EXPR, high, low, 0);
4558
4559
4560   if (POINTER_TYPE_P (etype))
4561     {
4562       if (value != 0 && !TREE_OVERFLOW (value))
4563         {
4564           low = fold_convert (sizetype, low);
4565           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4566           return build_range_check (type,
4567                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4568                                     1, build_int_cst (etype, 0), value);
4569         }
4570       return 0;
4571     }
4572
4573   if (value != 0 && !TREE_OVERFLOW (value))
4574     return build_range_check (type,
4575                               fold_build2 (MINUS_EXPR, etype, exp, low),
4576                               1, build_int_cst (etype, 0), value);
4577
4578   return 0;
4579 }
4580 \f
4581 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4582
4583 static tree
4584 range_predecessor (tree val)
4585 {
4586   tree type = TREE_TYPE (val);
4587
4588   if (INTEGRAL_TYPE_P (type)
4589       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4590     return 0;
4591   else
4592     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4593 }
4594
4595 /* Return the successor of VAL in its type, handling the infinite case.  */
4596
4597 static tree
4598 range_successor (tree val)
4599 {
4600   tree type = TREE_TYPE (val);
4601
4602   if (INTEGRAL_TYPE_P (type)
4603       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4604     return 0;
4605   else
4606     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4607 }
4608
4609 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4610    can, 0 if we can't.  Set the output range into the specified parameters.  */
4611
4612 static int
4613 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4614               tree high0, int in1_p, tree low1, tree high1)
4615 {
4616   int no_overlap;
4617   int subset;
4618   int temp;
4619   tree tem;
4620   int in_p;
4621   tree low, high;
4622   int lowequal = ((low0 == 0 && low1 == 0)
4623                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4624                                                 low0, 0, low1, 0)));
4625   int highequal = ((high0 == 0 && high1 == 0)
4626                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4627                                                  high0, 1, high1, 1)));
4628
4629   /* Make range 0 be the range that starts first, or ends last if they
4630      start at the same value.  Swap them if it isn't.  */
4631   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4632                                  low0, 0, low1, 0))
4633       || (lowequal
4634           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4635                                         high1, 1, high0, 1))))
4636     {
4637       temp = in0_p, in0_p = in1_p, in1_p = temp;
4638       tem = low0, low0 = low1, low1 = tem;
4639       tem = high0, high0 = high1, high1 = tem;
4640     }
4641
4642   /* Now flag two cases, whether the ranges are disjoint or whether the
4643      second range is totally subsumed in the first.  Note that the tests
4644      below are simplified by the ones above.  */
4645   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4646                                           high0, 1, low1, 0));
4647   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4648                                       high1, 1, high0, 1));
4649
4650   /* We now have four cases, depending on whether we are including or
4651      excluding the two ranges.  */
4652   if (in0_p && in1_p)
4653     {
4654       /* If they don't overlap, the result is false.  If the second range
4655          is a subset it is the result.  Otherwise, the range is from the start
4656          of the second to the end of the first.  */
4657       if (no_overlap)
4658         in_p = 0, low = high = 0;
4659       else if (subset)
4660         in_p = 1, low = low1, high = high1;
4661       else
4662         in_p = 1, low = low1, high = high0;
4663     }
4664
4665   else if (in0_p && ! in1_p)
4666     {
4667       /* If they don't overlap, the result is the first range.  If they are
4668          equal, the result is false.  If the second range is a subset of the
4669          first, and the ranges begin at the same place, we go from just after
4670          the end of the second range to the end of the first.  If the second
4671          range is not a subset of the first, or if it is a subset and both
4672          ranges end at the same place, the range starts at the start of the
4673          first range and ends just before the second range.
4674          Otherwise, we can't describe this as a single range.  */
4675       if (no_overlap)
4676         in_p = 1, low = low0, high = high0;
4677       else if (lowequal && highequal)
4678         in_p = 0, low = high = 0;
4679       else if (subset && lowequal)
4680         {
4681           low = range_successor (high1);
4682           high = high0;
4683           in_p = 1;
4684           if (low == 0)
4685             {
4686               /* We are in the weird situation where high0 > high1 but
4687                  high1 has no successor.  Punt.  */
4688               return 0;
4689             }
4690         }
4691       else if (! subset || highequal)
4692         {
4693           low = low0;
4694           high = range_predecessor (low1);
4695           in_p = 1;
4696           if (high == 0)
4697             {
4698               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4699               return 0;
4700             }
4701         }
4702       else
4703         return 0;
4704     }
4705
4706   else if (! in0_p && in1_p)
4707     {
4708       /* If they don't overlap, the result is the second range.  If the second
4709          is a subset of the first, the result is false.  Otherwise,
4710          the range starts just after the first range and ends at the
4711          end of the second.  */
4712       if (no_overlap)
4713         in_p = 1, low = low1, high = high1;
4714       else if (subset || highequal)
4715         in_p = 0, low = high = 0;
4716       else
4717         {
4718           low = range_successor (high0);
4719           high = high1;
4720           in_p = 1;
4721           if (low == 0)
4722             {
4723               /* high1 > high0 but high0 has no successor.  Punt.  */
4724               return 0;
4725             }
4726         }
4727     }
4728
4729   else
4730     {
4731       /* The case where we are excluding both ranges.  Here the complex case
4732          is if they don't overlap.  In that case, the only time we have a
4733          range is if they are adjacent.  If the second is a subset of the
4734          first, the result is the first.  Otherwise, the range to exclude
4735          starts at the beginning of the first range and ends at the end of the
4736          second.  */
4737       if (no_overlap)
4738         {
4739           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4740                                          range_successor (high0),
4741                                          1, low1, 0)))
4742             in_p = 0, low = low0, high = high1;
4743           else
4744             {
4745               /* Canonicalize - [min, x] into - [-, x].  */
4746               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4747                 switch (TREE_CODE (TREE_TYPE (low0)))
4748                   {
4749                   case ENUMERAL_TYPE:
4750                     if (TYPE_PRECISION (TREE_TYPE (low0))
4751                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4752                       break;
4753                     /* FALLTHROUGH */
4754                   case INTEGER_TYPE:
4755                     if (tree_int_cst_equal (low0,
4756                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4757                       low0 = 0;
4758                     break;
4759                   case POINTER_TYPE:
4760                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4761                         && integer_zerop (low0))
4762                       low0 = 0;
4763                     break;
4764                   default:
4765                     break;
4766                   }
4767
4768               /* Canonicalize - [x, max] into - [x, -].  */
4769               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4770                 switch (TREE_CODE (TREE_TYPE (high1)))
4771                   {
4772                   case ENUMERAL_TYPE:
4773                     if (TYPE_PRECISION (TREE_TYPE (high1))
4774                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4775                       break;
4776                     /* FALLTHROUGH */
4777                   case INTEGER_TYPE:
4778                     if (tree_int_cst_equal (high1,
4779                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4780                       high1 = 0;
4781                     break;
4782                   case POINTER_TYPE:
4783                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4784                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4785                                                        high1, 1,
4786                                                        integer_one_node, 1)))
4787                       high1 = 0;
4788                     break;
4789                   default:
4790                     break;
4791                   }
4792
4793               /* The ranges might be also adjacent between the maximum and
4794                  minimum values of the given type.  For
4795                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4796                  return + [x + 1, y - 1].  */
4797               if (low0 == 0 && high1 == 0)
4798                 {
4799                   low = range_successor (high0);
4800                   high = range_predecessor (low1);
4801                   if (low == 0 || high == 0)
4802                     return 0;
4803
4804                   in_p = 1;
4805                 }
4806               else
4807                 return 0;
4808             }
4809         }
4810       else if (subset)
4811         in_p = 0, low = low0, high = high0;
4812       else
4813         in_p = 0, low = low0, high = high1;
4814     }
4815
4816   *pin_p = in_p, *plow = low, *phigh = high;
4817   return 1;
4818 }
4819 \f
4820
4821 /* Subroutine of fold, looking inside expressions of the form
4822    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4823    of the COND_EXPR.  This function is being used also to optimize
4824    A op B ? C : A, by reversing the comparison first.
4825
4826    Return a folded expression whose code is not a COND_EXPR
4827    anymore, or NULL_TREE if no folding opportunity is found.  */
4828
4829 static tree
4830 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4831 {
4832   enum tree_code comp_code = TREE_CODE (arg0);
4833   tree arg00 = TREE_OPERAND (arg0, 0);
4834   tree arg01 = TREE_OPERAND (arg0, 1);
4835   tree arg1_type = TREE_TYPE (arg1);
4836   tree tem;
4837
4838   STRIP_NOPS (arg1);
4839   STRIP_NOPS (arg2);
4840
4841   /* If we have A op 0 ? A : -A, consider applying the following
4842      transformations:
4843
4844      A == 0? A : -A    same as -A
4845      A != 0? A : -A    same as A
4846      A >= 0? A : -A    same as abs (A)
4847      A > 0?  A : -A    same as abs (A)
4848      A <= 0? A : -A    same as -abs (A)
4849      A < 0?  A : -A    same as -abs (A)
4850
4851      None of these transformations work for modes with signed
4852      zeros.  If A is +/-0, the first two transformations will
4853      change the sign of the result (from +0 to -0, or vice
4854      versa).  The last four will fix the sign of the result,
4855      even though the original expressions could be positive or
4856      negative, depending on the sign of A.
4857
4858      Note that all these transformations are correct if A is
4859      NaN, since the two alternatives (A and -A) are also NaNs.  */
4860   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4861       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4862           ? real_zerop (arg01)
4863           : integer_zerop (arg01))
4864       && ((TREE_CODE (arg2) == NEGATE_EXPR
4865            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4866              /* In the case that A is of the form X-Y, '-A' (arg2) may
4867                 have already been folded to Y-X, check for that. */
4868           || (TREE_CODE (arg1) == MINUS_EXPR
4869               && TREE_CODE (arg2) == MINUS_EXPR
4870               && operand_equal_p (TREE_OPERAND (arg1, 0),
4871                                   TREE_OPERAND (arg2, 1), 0)
4872               && operand_equal_p (TREE_OPERAND (arg1, 1),
4873                                   TREE_OPERAND (arg2, 0), 0))))
4874     switch (comp_code)
4875       {
4876       case EQ_EXPR:
4877       case UNEQ_EXPR:
4878         tem = fold_convert (arg1_type, arg1);
4879         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4880       case NE_EXPR:
4881       case LTGT_EXPR:
4882         return pedantic_non_lvalue (fold_convert (type, arg1));
4883       case UNGE_EXPR:
4884       case UNGT_EXPR:
4885         if (flag_trapping_math)
4886           break;
4887         /* Fall through.  */
4888       case GE_EXPR:
4889       case GT_EXPR:
4890         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4891           arg1 = fold_convert (signed_type_for
4892                                (TREE_TYPE (arg1)), arg1);
4893         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4894         return pedantic_non_lvalue (fold_convert (type, tem));
4895       case UNLE_EXPR:
4896       case UNLT_EXPR:
4897         if (flag_trapping_math)
4898           break;
4899       case LE_EXPR:
4900       case LT_EXPR:
4901         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4902           arg1 = fold_convert (signed_type_for
4903                                (TREE_TYPE (arg1)), arg1);
4904         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4905         return negate_expr (fold_convert (type, tem));
4906       default:
4907         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4908         break;
4909       }
4910
4911   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4912      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4913      both transformations are correct when A is NaN: A != 0
4914      is then true, and A == 0 is false.  */
4915
4916   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4917       && integer_zerop (arg01) && integer_zerop (arg2))
4918     {
4919       if (comp_code == NE_EXPR)
4920         return pedantic_non_lvalue (fold_convert (type, arg1));
4921       else if (comp_code == EQ_EXPR)
4922         return build_int_cst (type, 0);
4923     }
4924
4925   /* Try some transformations of A op B ? A : B.
4926
4927      A == B? A : B    same as B
4928      A != B? A : B    same as A
4929      A >= B? A : B    same as max (A, B)
4930      A > B?  A : B    same as max (B, A)
4931      A <= B? A : B    same as min (A, B)
4932      A < B?  A : B    same as min (B, A)
4933
4934      As above, these transformations don't work in the presence
4935      of signed zeros.  For example, if A and B are zeros of
4936      opposite sign, the first two transformations will change
4937      the sign of the result.  In the last four, the original
4938      expressions give different results for (A=+0, B=-0) and
4939      (A=-0, B=+0), but the transformed expressions do not.
4940
4941      The first two transformations are correct if either A or B
4942      is a NaN.  In the first transformation, the condition will
4943      be false, and B will indeed be chosen.  In the case of the
4944      second transformation, the condition A != B will be true,
4945      and A will be chosen.
4946
4947      The conversions to max() and min() are not correct if B is
4948      a number and A is not.  The conditions in the original
4949      expressions will be false, so all four give B.  The min()
4950      and max() versions would give a NaN instead.  */
4951   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4952       && operand_equal_for_comparison_p (arg01, arg2, arg00)
4953       /* Avoid these transformations if the COND_EXPR may be used
4954          as an lvalue in the C++ front-end.  PR c++/19199.  */
4955       && (in_gimple_form
4956           || (strcmp (lang_hooks.name, "GNU C++") != 0
4957               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4958           || ! maybe_lvalue_p (arg1)
4959           || ! maybe_lvalue_p (arg2)))
4960     {
4961       tree comp_op0 = arg00;
4962       tree comp_op1 = arg01;
4963       tree comp_type = TREE_TYPE (comp_op0);
4964
4965       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4966       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4967         {
4968           comp_type = type;
4969           comp_op0 = arg1;
4970           comp_op1 = arg2;
4971         }
4972
4973       switch (comp_code)
4974         {
4975         case EQ_EXPR:
4976           return pedantic_non_lvalue (fold_convert (type, arg2));
4977         case NE_EXPR:
4978           return pedantic_non_lvalue (fold_convert (type, arg1));
4979         case LE_EXPR:
4980         case LT_EXPR:
4981         case UNLE_EXPR:
4982         case UNLT_EXPR:
4983           /* In C++ a ?: expression can be an lvalue, so put the
4984              operand which will be used if they are equal first
4985              so that we can convert this back to the
4986              corresponding COND_EXPR.  */
4987           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4988             {
4989               comp_op0 = fold_convert (comp_type, comp_op0);
4990               comp_op1 = fold_convert (comp_type, comp_op1);
4991               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4992                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4993                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4994               return pedantic_non_lvalue (fold_convert (type, tem));
4995             }
4996           break;
4997         case GE_EXPR:
4998         case GT_EXPR:
4999         case UNGE_EXPR:
5000         case UNGT_EXPR:
5001           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5002             {
5003               comp_op0 = fold_convert (comp_type, comp_op0);
5004               comp_op1 = fold_convert (comp_type, comp_op1);
5005               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5006                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5007                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5008               return pedantic_non_lvalue (fold_convert (type, tem));
5009             }
5010           break;
5011         case UNEQ_EXPR:
5012           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5013             return pedantic_non_lvalue (fold_convert (type, arg2));
5014           break;
5015         case LTGT_EXPR:
5016           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5017             return pedantic_non_lvalue (fold_convert (type, arg1));
5018           break;
5019         default:
5020           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5021           break;
5022         }
5023     }
5024
5025   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5026      we might still be able to simplify this.  For example,
5027      if C1 is one less or one more than C2, this might have started
5028      out as a MIN or MAX and been transformed by this function.
5029      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5030
5031   if (INTEGRAL_TYPE_P (type)
5032       && TREE_CODE (arg01) == INTEGER_CST
5033       && TREE_CODE (arg2) == INTEGER_CST)
5034     switch (comp_code)
5035       {
5036       case EQ_EXPR:
5037         /* We can replace A with C1 in this case.  */
5038         arg1 = fold_convert (type, arg01);
5039         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5040
5041       case LT_EXPR:
5042         /* If C1 is C2 + 1, this is min(A, C2).  */
5043         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5044                                OEP_ONLY_CONST)
5045             && operand_equal_p (arg01,
5046                                 const_binop (PLUS_EXPR, arg2,
5047                                              build_int_cst (type, 1), 0),
5048                                 OEP_ONLY_CONST))
5049           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5050                                                    type,
5051                                                    fold_convert (type, arg1),
5052                                                    arg2));
5053         break;
5054
5055       case LE_EXPR:
5056         /* If C1 is C2 - 1, this is min(A, C2).  */
5057         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5058                                OEP_ONLY_CONST)
5059             && operand_equal_p (arg01,
5060                                 const_binop (MINUS_EXPR, arg2,
5061                                              build_int_cst (type, 1), 0),
5062                                 OEP_ONLY_CONST))
5063           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5064                                                    type,
5065                                                    fold_convert (type, arg1),
5066                                                    arg2));
5067         break;
5068
5069       case GT_EXPR:
5070         /* If C1 is C2 - 1, this is max(A, C2).  */
5071         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5072                                OEP_ONLY_CONST)
5073             && operand_equal_p (arg01,
5074                                 const_binop (MINUS_EXPR, arg2,
5075                                              build_int_cst (type, 1), 0),
5076                                 OEP_ONLY_CONST))
5077           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5078                                                    type,
5079                                                    fold_convert (type, arg1),
5080                                                    arg2));
5081         break;
5082
5083       case GE_EXPR:
5084         /* If C1 is C2 + 1, this is max(A, C2).  */
5085         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5086                                OEP_ONLY_CONST)
5087             && operand_equal_p (arg01,
5088                                 const_binop (PLUS_EXPR, arg2,
5089                                              build_int_cst (type, 1), 0),
5090                                 OEP_ONLY_CONST))
5091           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5092                                                    type,
5093                                                    fold_convert (type, arg1),
5094                                                    arg2));
5095         break;
5096       case NE_EXPR:
5097         break;
5098       default:
5099         gcc_unreachable ();
5100       }
5101
5102   return NULL_TREE;
5103 }
5104
5105
5106 \f
5107 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5108 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
5109 #endif
5110
5111 /* EXP is some logical combination of boolean tests.  See if we can
5112    merge it into some range test.  Return the new tree if so.  */
5113
5114 static tree
5115 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5116 {
5117   int or_op = (code == TRUTH_ORIF_EXPR
5118                || code == TRUTH_OR_EXPR);
5119   int in0_p, in1_p, in_p;
5120   tree low0, low1, low, high0, high1, high;
5121   bool strict_overflow_p = false;
5122   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5123   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5124   tree tem;
5125   const char * const warnmsg = G_("assuming signed overflow does not occur "
5126                                   "when simplifying range test");
5127
5128   /* If this is an OR operation, invert both sides; we will invert
5129      again at the end.  */
5130   if (or_op)
5131     in0_p = ! in0_p, in1_p = ! in1_p;
5132
5133   /* If both expressions are the same, if we can merge the ranges, and we
5134      can build the range test, return it or it inverted.  If one of the
5135      ranges is always true or always false, consider it to be the same
5136      expression as the other.  */
5137   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5138       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5139                        in1_p, low1, high1)
5140       && 0 != (tem = (build_range_check (type,
5141                                          lhs != 0 ? lhs
5142                                          : rhs != 0 ? rhs : integer_zero_node,
5143                                          in_p, low, high))))
5144     {
5145       if (strict_overflow_p)
5146         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5147       return or_op ? invert_truthvalue (tem) : tem;
5148     }
5149
5150   /* On machines where the branch cost is expensive, if this is a
5151      short-circuited branch and the underlying object on both sides
5152      is the same, make a non-short-circuit operation.  */
5153   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5154            && lhs != 0 && rhs != 0
5155            && (code == TRUTH_ANDIF_EXPR
5156                || code == TRUTH_ORIF_EXPR)
5157            && operand_equal_p (lhs, rhs, 0))
5158     {
5159       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5160          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5161          which cases we can't do this.  */
5162       if (simple_operand_p (lhs))
5163         return build2 (code == TRUTH_ANDIF_EXPR
5164                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5165                        type, op0, op1);
5166
5167       else if (lang_hooks.decls.global_bindings_p () == 0
5168                && ! CONTAINS_PLACEHOLDER_P (lhs))
5169         {
5170           tree common = save_expr (lhs);
5171
5172           if (0 != (lhs = build_range_check (type, common,
5173                                              or_op ? ! in0_p : in0_p,
5174                                              low0, high0))
5175               && (0 != (rhs = build_range_check (type, common,
5176                                                  or_op ? ! in1_p : in1_p,
5177                                                  low1, high1))))
5178             {
5179               if (strict_overflow_p)
5180                 fold_overflow_warning (warnmsg,
5181                                        WARN_STRICT_OVERFLOW_COMPARISON);
5182               return build2 (code == TRUTH_ANDIF_EXPR
5183                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5184                              type, lhs, rhs);
5185             }
5186         }
5187     }
5188
5189   return 0;
5190 }
5191 \f
5192 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5193    bit value.  Arrange things so the extra bits will be set to zero if and
5194    only if C is signed-extended to its full width.  If MASK is nonzero,
5195    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5196
5197 static tree
5198 unextend (tree c, int p, int unsignedp, tree mask)
5199 {
5200   tree type = TREE_TYPE (c);
5201   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5202   tree temp;
5203
5204   if (p == modesize || unsignedp)
5205     return c;
5206
5207   /* We work by getting just the sign bit into the low-order bit, then
5208      into the high-order bit, then sign-extend.  We then XOR that value
5209      with C.  */
5210   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5211   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5212
5213   /* We must use a signed type in order to get an arithmetic right shift.
5214      However, we must also avoid introducing accidental overflows, so that
5215      a subsequent call to integer_zerop will work.  Hence we must
5216      do the type conversion here.  At this point, the constant is either
5217      zero or one, and the conversion to a signed type can never overflow.
5218      We could get an overflow if this conversion is done anywhere else.  */
5219   if (TYPE_UNSIGNED (type))
5220     temp = fold_convert (signed_type_for (type), temp);
5221
5222   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5223   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5224   if (mask != 0)
5225     temp = const_binop (BIT_AND_EXPR, temp,
5226                         fold_convert (TREE_TYPE (c), mask), 0);
5227   /* If necessary, convert the type back to match the type of C.  */
5228   if (TYPE_UNSIGNED (type))
5229     temp = fold_convert (type, temp);
5230
5231   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5232 }
5233 \f
5234 /* Find ways of folding logical expressions of LHS and RHS:
5235    Try to merge two comparisons to the same innermost item.
5236    Look for range tests like "ch >= '0' && ch <= '9'".
5237    Look for combinations of simple terms on machines with expensive branches
5238    and evaluate the RHS unconditionally.
5239
5240    For example, if we have p->a == 2 && p->b == 4 and we can make an
5241    object large enough to span both A and B, we can do this with a comparison
5242    against the object ANDed with the a mask.
5243
5244    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5245    operations to do this with one comparison.
5246
5247    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5248    function and the one above.
5249
5250    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5251    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5252
5253    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5254    two operands.
5255
5256    We return the simplified tree or 0 if no optimization is possible.  */
5257
5258 static tree
5259 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5260 {
5261   /* If this is the "or" of two comparisons, we can do something if
5262      the comparisons are NE_EXPR.  If this is the "and", we can do something
5263      if the comparisons are EQ_EXPR.  I.e.,
5264         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5265
5266      WANTED_CODE is this operation code.  For single bit fields, we can
5267      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5268      comparison for one-bit fields.  */
5269
5270   enum tree_code wanted_code;
5271   enum tree_code lcode, rcode;
5272   tree ll_arg, lr_arg, rl_arg, rr_arg;
5273   tree ll_inner, lr_inner, rl_inner, rr_inner;
5274   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5275   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5276   HOST_WIDE_INT xll_bitpos, xrl_bitpos;
5277   HOST_WIDE_INT lnbitsize, lnbitpos;
5278   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5279   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5280   enum machine_mode lnmode;
5281   tree ll_mask, lr_mask, rl_mask, rr_mask;
5282   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5283   tree l_const, r_const;
5284   tree lntype, result;
5285   int first_bit, end_bit;
5286   int volatilep;
5287   tree orig_lhs = lhs, orig_rhs = rhs;
5288   enum tree_code orig_code = code;
5289
5290   /* Start by getting the comparison codes.  Fail if anything is volatile.
5291      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5292      it were surrounded with a NE_EXPR.  */
5293
5294   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5295     return 0;
5296
5297   lcode = TREE_CODE (lhs);
5298   rcode = TREE_CODE (rhs);
5299
5300   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5301     {
5302       lhs = build2 (NE_EXPR, truth_type, lhs,
5303                     build_int_cst (TREE_TYPE (lhs), 0));
5304       lcode = NE_EXPR;
5305     }
5306
5307   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5308     {
5309       rhs = build2 (NE_EXPR, truth_type, rhs,
5310                     build_int_cst (TREE_TYPE (rhs), 0));
5311       rcode = NE_EXPR;
5312     }
5313
5314   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5315       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5316     return 0;
5317
5318   ll_arg = TREE_OPERAND (lhs, 0);
5319   lr_arg = TREE_OPERAND (lhs, 1);
5320   rl_arg = TREE_OPERAND (rhs, 0);
5321   rr_arg = TREE_OPERAND (rhs, 1);
5322
5323   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5324   if (simple_operand_p (ll_arg)
5325       && simple_operand_p (lr_arg))
5326     {
5327       tree result;
5328       if (operand_equal_p (ll_arg, rl_arg, 0)
5329           && operand_equal_p (lr_arg, rr_arg, 0))
5330         {
5331           result = combine_comparisons (code, lcode, rcode,
5332                                         truth_type, ll_arg, lr_arg);
5333           if (result)
5334             return result;
5335         }
5336       else if (operand_equal_p (ll_arg, rr_arg, 0)
5337                && operand_equal_p (lr_arg, rl_arg, 0))
5338         {
5339           result = combine_comparisons (code, lcode,
5340                                         swap_tree_comparison (rcode),
5341                                         truth_type, ll_arg, lr_arg);
5342           if (result)
5343             return result;
5344         }
5345     }
5346
5347   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5348           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5349
5350   /* If the RHS can be evaluated unconditionally and its operands are
5351      simple, it wins to evaluate the RHS unconditionally on machines
5352      with expensive branches.  In this case, this isn't a comparison
5353      that can be merged.  Avoid doing this if the RHS is a floating-point
5354      comparison since those can trap.  */
5355
5356   if (BRANCH_COST >= 2
5357       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5358       && simple_operand_p (rl_arg)
5359       && simple_operand_p (rr_arg))
5360     {
5361       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5362       if (code == TRUTH_OR_EXPR
5363           && lcode == NE_EXPR && integer_zerop (lr_arg)
5364           && rcode == NE_EXPR && integer_zerop (rr_arg)
5365           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5366           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5367         return build2 (NE_EXPR, truth_type,
5368                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5369                                ll_arg, rl_arg),
5370                        build_int_cst (TREE_TYPE (ll_arg), 0));
5371
5372       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5373       if (code == TRUTH_AND_EXPR
5374           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5375           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5376           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5377           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5378         return build2 (EQ_EXPR, truth_type,
5379                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5380                                ll_arg, rl_arg),
5381                        build_int_cst (TREE_TYPE (ll_arg), 0));
5382
5383       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5384         {
5385           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5386             return build2 (code, truth_type, lhs, rhs);
5387           return NULL_TREE;
5388         }
5389     }
5390
5391   /* See if the comparisons can be merged.  Then get all the parameters for
5392      each side.  */
5393
5394   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5395       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5396     return 0;
5397
5398   volatilep = 0;
5399   ll_inner = decode_field_reference (ll_arg,
5400                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5401                                      &ll_unsignedp, &volatilep, &ll_mask,
5402                                      &ll_and_mask);
5403   lr_inner = decode_field_reference (lr_arg,
5404                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5405                                      &lr_unsignedp, &volatilep, &lr_mask,
5406                                      &lr_and_mask);
5407   rl_inner = decode_field_reference (rl_arg,
5408                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5409                                      &rl_unsignedp, &volatilep, &rl_mask,
5410                                      &rl_and_mask);
5411   rr_inner = decode_field_reference (rr_arg,
5412                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5413                                      &rr_unsignedp, &volatilep, &rr_mask,
5414                                      &rr_and_mask);
5415
5416   /* It must be true that the inner operation on the lhs of each
5417      comparison must be the same if we are to be able to do anything.
5418      Then see if we have constants.  If not, the same must be true for
5419      the rhs's.  */
5420   if (volatilep || ll_inner == 0 || rl_inner == 0
5421       || ! operand_equal_p (ll_inner, rl_inner, 0))
5422     return 0;
5423
5424   if (TREE_CODE (lr_arg) == INTEGER_CST
5425       && TREE_CODE (rr_arg) == INTEGER_CST)
5426     l_const = lr_arg, r_const = rr_arg;
5427   else if (lr_inner == 0 || rr_inner == 0
5428            || ! operand_equal_p (lr_inner, rr_inner, 0))
5429     return 0;
5430   else
5431     l_const = r_const = 0;
5432
5433   /* If either comparison code is not correct for our logical operation,
5434      fail.  However, we can convert a one-bit comparison against zero into
5435      the opposite comparison against that bit being set in the field.  */
5436
5437   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5438   if (lcode != wanted_code)
5439     {
5440       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5441         {
5442           /* Make the left operand unsigned, since we are only interested
5443              in the value of one bit.  Otherwise we are doing the wrong
5444              thing below.  */
5445           ll_unsignedp = 1;
5446           l_const = ll_mask;
5447         }
5448       else
5449         return 0;
5450     }
5451
5452   /* This is analogous to the code for l_const above.  */
5453   if (rcode != wanted_code)
5454     {
5455       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5456         {
5457           rl_unsignedp = 1;
5458           r_const = rl_mask;
5459         }
5460       else
5461         return 0;
5462     }
5463
5464   /* See if we can find a mode that contains both fields being compared on
5465      the left.  If we can't, fail.  Otherwise, update all constants and masks
5466      to be relative to a field of that size.  */
5467   first_bit = MIN (ll_bitpos, rl_bitpos);
5468   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5469   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5470                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5471                           volatilep);
5472   if (lnmode == VOIDmode)
5473     return 0;
5474
5475   lnbitsize = GET_MODE_BITSIZE (lnmode);
5476   lnbitpos = first_bit & ~ (lnbitsize - 1);
5477   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5478   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5479
5480   if (BYTES_BIG_ENDIAN)
5481     {
5482       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5483       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5484     }
5485
5486   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5487                          size_int (xll_bitpos), 0);
5488   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5489                          size_int (xrl_bitpos), 0);
5490
5491   if (l_const)
5492     {
5493       l_const = fold_convert (lntype, l_const);
5494       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5495       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5496       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5497                                         fold_build1 (BIT_NOT_EXPR,
5498                                                      lntype, ll_mask),
5499                                         0)))
5500         {
5501           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5502
5503           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5504         }
5505     }
5506   if (r_const)
5507     {
5508       r_const = fold_convert (lntype, r_const);
5509       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5510       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5511       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5512                                         fold_build1 (BIT_NOT_EXPR,
5513                                                      lntype, rl_mask),
5514                                         0)))
5515         {
5516           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5517
5518           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5519         }
5520     }
5521
5522   /* Handle the case of comparisons with constants.  If there is something in
5523      common between the masks, those bits of the constants must be the same.
5524      If not, the condition is always false.  Test for this to avoid generating
5525      incorrect code below.  */
5526   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5527   if (! integer_zerop (result)
5528       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5529                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5530     {
5531       if (wanted_code == NE_EXPR)
5532         {
5533           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5534           return constant_boolean_node (true, truth_type);
5535         }
5536       else
5537         {
5538           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5539           return constant_boolean_node (false, truth_type);
5540         }
5541     }
5542
5543   return NULL_TREE;
5544 }
5545 \f
5546 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5547    constant.  */
5548
5549 static tree
5550 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5551 {
5552   tree arg0 = op0;
5553   enum tree_code op_code;
5554   tree comp_const;
5555   tree minmax_const;
5556   int consts_equal, consts_lt;
5557   tree inner;
5558
5559   STRIP_SIGN_NOPS (arg0);
5560
5561   op_code = TREE_CODE (arg0);
5562   minmax_const = TREE_OPERAND (arg0, 1);
5563   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5564   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5565   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5566   inner = TREE_OPERAND (arg0, 0);
5567
5568   /* If something does not permit us to optimize, return the original tree.  */
5569   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5570       || TREE_CODE (comp_const) != INTEGER_CST
5571       || TREE_OVERFLOW (comp_const)
5572       || TREE_CODE (minmax_const) != INTEGER_CST
5573       || TREE_OVERFLOW (minmax_const))
5574     return NULL_TREE;
5575
5576   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5577      and GT_EXPR, doing the rest with recursive calls using logical
5578      simplifications.  */
5579   switch (code)
5580     {
5581     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5582       {
5583         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5584                                           type, op0, op1);
5585         if (tem)
5586           return invert_truthvalue (tem);
5587         return NULL_TREE;
5588       }
5589
5590     case GE_EXPR:
5591       return
5592         fold_build2 (TRUTH_ORIF_EXPR, type,
5593                      optimize_minmax_comparison
5594                      (EQ_EXPR, type, arg0, comp_const),
5595                      optimize_minmax_comparison
5596                      (GT_EXPR, type, arg0, comp_const));
5597
5598     case EQ_EXPR:
5599       if (op_code == MAX_EXPR && consts_equal)
5600         /* MAX (X, 0) == 0  ->  X <= 0  */
5601         return fold_build2 (LE_EXPR, type, inner, comp_const);
5602
5603       else if (op_code == MAX_EXPR && consts_lt)
5604         /* MAX (X, 0) == 5  ->  X == 5   */
5605         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5606
5607       else if (op_code == MAX_EXPR)
5608         /* MAX (X, 0) == -1  ->  false  */
5609         return omit_one_operand (type, integer_zero_node, inner);
5610
5611       else if (consts_equal)
5612         /* MIN (X, 0) == 0  ->  X >= 0  */
5613         return fold_build2 (GE_EXPR, type, inner, comp_const);
5614
5615       else if (consts_lt)
5616         /* MIN (X, 0) == 5  ->  false  */
5617         return omit_one_operand (type, integer_zero_node, inner);
5618
5619       else
5620         /* MIN (X, 0) == -1  ->  X == -1  */
5621         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5622
5623     case GT_EXPR:
5624       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5625         /* MAX (X, 0) > 0  ->  X > 0
5626            MAX (X, 0) > 5  ->  X > 5  */
5627         return fold_build2 (GT_EXPR, type, inner, comp_const);
5628
5629       else if (op_code == MAX_EXPR)
5630         /* MAX (X, 0) > -1  ->  true  */
5631         return omit_one_operand (type, integer_one_node, inner);
5632
5633       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5634         /* MIN (X, 0) > 0  ->  false
5635            MIN (X, 0) > 5  ->  false  */
5636         return omit_one_operand (type, integer_zero_node, inner);
5637
5638       else
5639         /* MIN (X, 0) > -1  ->  X > -1  */
5640         return fold_build2 (GT_EXPR, type, inner, comp_const);
5641
5642     default:
5643       return NULL_TREE;
5644     }
5645 }
5646 \f
5647 /* T is an integer expression that is being multiplied, divided, or taken a
5648    modulus (CODE says which and what kind of divide or modulus) by a
5649    constant C.  See if we can eliminate that operation by folding it with
5650    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5651    should be used for the computation if wider than our type.
5652
5653    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5654    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5655    expression would not overflow or that overflow is undefined for the type
5656    in the language in question.
5657
5658    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5659    the machine has a multiply-accumulate insn or that this is part of an
5660    addressing calculation.
5661
5662    If we return a non-null expression, it is an equivalent form of the
5663    original computation, but need not be in the original type.
5664
5665    We set *STRICT_OVERFLOW_P to true if the return values depends on
5666    signed overflow being undefined.  Otherwise we do not change
5667    *STRICT_OVERFLOW_P.  */
5668
5669 static tree
5670 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5671                 bool *strict_overflow_p)
5672 {
5673   /* To avoid exponential search depth, refuse to allow recursion past
5674      three levels.  Beyond that (1) it's highly unlikely that we'll find
5675      something interesting and (2) we've probably processed it before
5676      when we built the inner expression.  */
5677
5678   static int depth;
5679   tree ret;
5680
5681   if (depth > 3)
5682     return NULL;
5683
5684   depth++;
5685   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5686   depth--;
5687
5688   return ret;
5689 }
5690
5691 static tree
5692 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5693                   bool *strict_overflow_p)
5694 {
5695   tree type = TREE_TYPE (t);
5696   enum tree_code tcode = TREE_CODE (t);
5697   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5698                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5699                 ? wide_type : type);
5700   tree t1, t2;
5701   int same_p = tcode == code;
5702   tree op0 = NULL_TREE, op1 = NULL_TREE;
5703   bool sub_strict_overflow_p;
5704
5705   /* Don't deal with constants of zero here; they confuse the code below.  */
5706   if (integer_zerop (c))
5707     return NULL_TREE;
5708
5709   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5710     op0 = TREE_OPERAND (t, 0);
5711
5712   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5713     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5714
5715   /* Note that we need not handle conditional operations here since fold
5716      already handles those cases.  So just do arithmetic here.  */
5717   switch (tcode)
5718     {
5719     case INTEGER_CST:
5720       /* For a constant, we can always simplify if we are a multiply
5721          or (for divide and modulus) if it is a multiple of our constant.  */
5722       if (code == MULT_EXPR
5723           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5724         return const_binop (code, fold_convert (ctype, t),
5725                             fold_convert (ctype, c), 0);
5726       break;
5727
5728     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
5729       /* If op0 is an expression ...  */
5730       if ((COMPARISON_CLASS_P (op0)
5731            || UNARY_CLASS_P (op0)
5732            || BINARY_CLASS_P (op0)
5733            || VL_EXP_CLASS_P (op0)
5734            || EXPRESSION_CLASS_P (op0))
5735           /* ... and is unsigned, and its type is smaller than ctype,
5736              then we cannot pass through as widening.  */
5737           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5738                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5739                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5740                && (GET_MODE_SIZE (TYPE_MODE (ctype))
5741                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5742               /* ... or this is a truncation (t is narrower than op0),
5743                  then we cannot pass through this narrowing.  */
5744               || (GET_MODE_SIZE (TYPE_MODE (type))
5745                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5746               /* ... or signedness changes for division or modulus,
5747                  then we cannot pass through this conversion.  */
5748               || (code != MULT_EXPR
5749                   && (TYPE_UNSIGNED (ctype)
5750                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
5751               /* ... or has undefined overflow while the converted to
5752                  type has not, we cannot do the operation in the inner type
5753                  as that would introduce undefined overflow.  */
5754               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5755                   && !TYPE_OVERFLOW_UNDEFINED (type))))
5756         break;
5757
5758       /* Pass the constant down and see if we can make a simplification.  If
5759          we can, replace this expression with the inner simplification for
5760          possible later conversion to our or some other type.  */
5761       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5762           && TREE_CODE (t2) == INTEGER_CST
5763           && !TREE_OVERFLOW (t2)
5764           && (0 != (t1 = extract_muldiv (op0, t2, code,
5765                                          code == MULT_EXPR
5766                                          ? ctype : NULL_TREE,
5767                                          strict_overflow_p))))
5768         return t1;
5769       break;
5770
5771     case ABS_EXPR:
5772       /* If widening the type changes it from signed to unsigned, then we
5773          must avoid building ABS_EXPR itself as unsigned.  */
5774       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5775         {
5776           tree cstype = (*signed_type_for) (ctype);
5777           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5778               != 0)
5779             {
5780               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5781               return fold_convert (ctype, t1);
5782             }
5783           break;
5784         }
5785       /* If the constant is negative, we cannot simplify this.  */
5786       if (tree_int_cst_sgn (c) == -1)
5787         break;
5788       /* FALLTHROUGH */
5789     case NEGATE_EXPR:
5790       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5791           != 0)
5792         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5793       break;
5794
5795     case MIN_EXPR:  case MAX_EXPR:
5796       /* If widening the type changes the signedness, then we can't perform
5797          this optimization as that changes the result.  */
5798       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5799         break;
5800
5801       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5802       sub_strict_overflow_p = false;
5803       if ((t1 = extract_muldiv (op0, c, code, wide_type,
5804                                 &sub_strict_overflow_p)) != 0
5805           && (t2 = extract_muldiv (op1, c, code, wide_type,
5806                                    &sub_strict_overflow_p)) != 0)
5807         {
5808           if (tree_int_cst_sgn (c) < 0)
5809             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5810           if (sub_strict_overflow_p)
5811             *strict_overflow_p = true;
5812           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5813                               fold_convert (ctype, t2));
5814         }
5815       break;
5816
5817     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5818       /* If the second operand is constant, this is a multiplication
5819          or floor division, by a power of two, so we can treat it that
5820          way unless the multiplier or divisor overflows.  Signed
5821          left-shift overflow is implementation-defined rather than
5822          undefined in C90, so do not convert signed left shift into
5823          multiplication.  */
5824       if (TREE_CODE (op1) == INTEGER_CST
5825           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5826           /* const_binop may not detect overflow correctly,
5827              so check for it explicitly here.  */
5828           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5829           && TREE_INT_CST_HIGH (op1) == 0
5830           && 0 != (t1 = fold_convert (ctype,
5831                                       const_binop (LSHIFT_EXPR,
5832                                                    size_one_node,
5833                                                    op1, 0)))
5834           && !TREE_OVERFLOW (t1))
5835         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5836                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5837                                        ctype, fold_convert (ctype, op0), t1),
5838                                c, code, wide_type, strict_overflow_p);
5839       break;
5840
5841     case PLUS_EXPR:  case MINUS_EXPR:
5842       /* See if we can eliminate the operation on both sides.  If we can, we
5843          can return a new PLUS or MINUS.  If we can't, the only remaining
5844          cases where we can do anything are if the second operand is a
5845          constant.  */
5846       sub_strict_overflow_p = false;
5847       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5848       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5849       if (t1 != 0 && t2 != 0
5850           && (code == MULT_EXPR
5851               /* If not multiplication, we can only do this if both operands
5852                  are divisible by c.  */
5853               || (multiple_of_p (ctype, op0, c)
5854                   && multiple_of_p (ctype, op1, c))))
5855         {
5856           if (sub_strict_overflow_p)
5857             *strict_overflow_p = true;
5858           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5859                               fold_convert (ctype, t2));
5860         }
5861
5862       /* If this was a subtraction, negate OP1 and set it to be an addition.
5863          This simplifies the logic below.  */
5864       if (tcode == MINUS_EXPR)
5865         tcode = PLUS_EXPR, op1 = negate_expr (op1);
5866
5867       if (TREE_CODE (op1) != INTEGER_CST)
5868         break;
5869
5870       /* If either OP1 or C are negative, this optimization is not safe for
5871          some of the division and remainder types while for others we need
5872          to change the code.  */
5873       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5874         {
5875           if (code == CEIL_DIV_EXPR)
5876             code = FLOOR_DIV_EXPR;
5877           else if (code == FLOOR_DIV_EXPR)
5878             code = CEIL_DIV_EXPR;
5879           else if (code != MULT_EXPR
5880                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5881             break;
5882         }
5883
5884       /* If it's a multiply or a division/modulus operation of a multiple
5885          of our constant, do the operation and verify it doesn't overflow.  */
5886       if (code == MULT_EXPR
5887           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5888         {
5889           op1 = const_binop (code, fold_convert (ctype, op1),
5890                              fold_convert (ctype, c), 0);
5891           /* We allow the constant to overflow with wrapping semantics.  */
5892           if (op1 == 0
5893               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5894             break;
5895         }
5896       else
5897         break;
5898
5899       /* If we have an unsigned type is not a sizetype, we cannot widen
5900          the operation since it will change the result if the original
5901          computation overflowed.  */
5902       if (TYPE_UNSIGNED (ctype)
5903           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5904           && ctype != type)
5905         break;
5906
5907       /* If we were able to eliminate our operation from the first side,
5908          apply our operation to the second side and reform the PLUS.  */
5909       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5910         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5911
5912       /* The last case is if we are a multiply.  In that case, we can
5913          apply the distributive law to commute the multiply and addition
5914          if the multiplication of the constants doesn't overflow.  */
5915       if (code == MULT_EXPR)
5916         return fold_build2 (tcode, ctype,
5917                             fold_build2 (code, ctype,
5918                                          fold_convert (ctype, op0),
5919                                          fold_convert (ctype, c)),
5920                             op1);
5921
5922       break;
5923
5924     case MULT_EXPR:
5925       /* We have a special case here if we are doing something like
5926          (C * 8) % 4 since we know that's zero.  */
5927       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5928            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5929           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5930           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5931         return omit_one_operand (type, integer_zero_node, op0);
5932
5933       /* ... fall through ...  */
5934
5935     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5936     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5937       /* If we can extract our operation from the LHS, do so and return a
5938          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5939          do something only if the second operand is a constant.  */
5940       if (same_p
5941           && (t1 = extract_muldiv (op0, c, code, wide_type,
5942                                    strict_overflow_p)) != 0)
5943         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5944                             fold_convert (ctype, op1));
5945       else if (tcode == MULT_EXPR && code == MULT_EXPR
5946                && (t1 = extract_muldiv (op1, c, code, wide_type,
5947                                         strict_overflow_p)) != 0)
5948         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5949                             fold_convert (ctype, t1));
5950       else if (TREE_CODE (op1) != INTEGER_CST)
5951         return 0;
5952
5953       /* If these are the same operation types, we can associate them
5954          assuming no overflow.  */
5955       if (tcode == code
5956           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5957                                      fold_convert (ctype, c), 0))
5958           && !TREE_OVERFLOW (t1))
5959         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5960
5961       /* If these operations "cancel" each other, we have the main
5962          optimizations of this pass, which occur when either constant is a
5963          multiple of the other, in which case we replace this with either an
5964          operation or CODE or TCODE.
5965
5966          If we have an unsigned type that is not a sizetype, we cannot do
5967          this since it will change the result if the original computation
5968          overflowed.  */
5969       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5970            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5971           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5972               || (tcode == MULT_EXPR
5973                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5974                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5975                   && code != MULT_EXPR)))
5976         {
5977           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5978             {
5979               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5980                 *strict_overflow_p = true;
5981               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5982                                   fold_convert (ctype,
5983                                                 const_binop (TRUNC_DIV_EXPR,
5984                                                              op1, c, 0)));
5985             }
5986           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5987             {
5988               if (TYPE_OVERFLOW_UNDEFINED (ctype))
5989                 *strict_overflow_p = true;
5990               return fold_build2 (code, ctype, fold_convert (ctype, op0),
5991                                   fold_convert (ctype,
5992                                                 const_binop (TRUNC_DIV_EXPR,
5993                                                              c, op1, 0)));
5994             }
5995         }
5996       break;
5997
5998     default:
5999       break;
6000     }
6001
6002   return 0;
6003 }
6004 \f
6005 /* Return a node which has the indicated constant VALUE (either 0 or
6006    1), and is of the indicated TYPE.  */
6007
6008 tree
6009 constant_boolean_node (int value, tree type)
6010 {
6011   if (type == integer_type_node)
6012     return value ? integer_one_node : integer_zero_node;
6013   else if (type == boolean_type_node)
6014     return value ? boolean_true_node : boolean_false_node;
6015   else
6016     return build_int_cst (type, value);
6017 }
6018
6019
6020 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6021    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6022    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6023    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6024    COND is the first argument to CODE; otherwise (as in the example
6025    given here), it is the second argument.  TYPE is the type of the
6026    original expression.  Return NULL_TREE if no simplification is
6027    possible.  */
6028
6029 static tree
6030 fold_binary_op_with_conditional_arg (enum tree_code code,
6031                                      tree type, tree op0, tree op1,
6032                                      tree cond, tree arg, int cond_first_p)
6033 {
6034   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6035   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6036   tree test, true_value, false_value;
6037   tree lhs = NULL_TREE;
6038   tree rhs = NULL_TREE;
6039
6040   /* This transformation is only worthwhile if we don't have to wrap
6041      arg in a SAVE_EXPR, and the operation can be simplified on at least
6042      one of the branches once its pushed inside the COND_EXPR.  */
6043   if (!TREE_CONSTANT (arg))
6044     return NULL_TREE;
6045
6046   if (TREE_CODE (cond) == COND_EXPR)
6047     {
6048       test = TREE_OPERAND (cond, 0);
6049       true_value = TREE_OPERAND (cond, 1);
6050       false_value = TREE_OPERAND (cond, 2);
6051       /* If this operand throws an expression, then it does not make
6052          sense to try to perform a logical or arithmetic operation
6053          involving it.  */
6054       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6055         lhs = true_value;
6056       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6057         rhs = false_value;
6058     }
6059   else
6060     {
6061       tree testtype = TREE_TYPE (cond);
6062       test = cond;
6063       true_value = constant_boolean_node (true, testtype);
6064       false_value = constant_boolean_node (false, testtype);
6065     }
6066
6067   arg = fold_convert (arg_type, arg);
6068   if (lhs == 0)
6069     {
6070       true_value = fold_convert (cond_type, true_value);
6071       if (cond_first_p)
6072         lhs = fold_build2 (code, type, true_value, arg);
6073       else
6074         lhs = fold_build2 (code, type, arg, true_value);
6075     }
6076   if (rhs == 0)
6077     {
6078       false_value = fold_convert (cond_type, false_value);
6079       if (cond_first_p)
6080         rhs = fold_build2 (code, type, false_value, arg);
6081       else
6082         rhs = fold_build2 (code, type, arg, false_value);
6083     }
6084
6085   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6086   return fold_convert (type, test);
6087 }
6088
6089 \f
6090 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6091
6092    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6093    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6094    ADDEND is the same as X.
6095
6096    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6097    and finite.  The problematic cases are when X is zero, and its mode
6098    has signed zeros.  In the case of rounding towards -infinity,
6099    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6100    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6101
6102 bool
6103 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6104 {
6105   if (!real_zerop (addend))
6106     return false;
6107
6108   /* Don't allow the fold with -fsignaling-nans.  */
6109   if (HONOR_SNANS (TYPE_MODE (type)))
6110     return false;
6111
6112   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6113   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6114     return true;
6115
6116   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6117   if (TREE_CODE (addend) == REAL_CST
6118       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6119     negate = !negate;
6120
6121   /* The mode has signed zeros, and we have to honor their sign.
6122      In this situation, there is only one case we can return true for.
6123      X - 0 is the same as X unless rounding towards -infinity is
6124      supported.  */
6125   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6126 }
6127
6128 /* Subroutine of fold() that checks comparisons of built-in math
6129    functions against real constants.
6130
6131    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6132    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6133    is the type of the result and ARG0 and ARG1 are the operands of the
6134    comparison.  ARG1 must be a TREE_REAL_CST.
6135
6136    The function returns the constant folded tree if a simplification
6137    can be made, and NULL_TREE otherwise.  */
6138
6139 static tree
6140 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6141                      tree type, tree arg0, tree arg1)
6142 {
6143   REAL_VALUE_TYPE c;
6144
6145   if (BUILTIN_SQRT_P (fcode))
6146     {
6147       tree arg = CALL_EXPR_ARG (arg0, 0);
6148       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6149
6150       c = TREE_REAL_CST (arg1);
6151       if (REAL_VALUE_NEGATIVE (c))
6152         {
6153           /* sqrt(x) < y is always false, if y is negative.  */
6154           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6155             return omit_one_operand (type, integer_zero_node, arg);
6156
6157           /* sqrt(x) > y is always true, if y is negative and we
6158              don't care about NaNs, i.e. negative values of x.  */
6159           if (code == NE_EXPR || !HONOR_NANS (mode))
6160             return omit_one_operand (type, integer_one_node, arg);
6161
6162           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6163           return fold_build2 (GE_EXPR, type, arg,
6164                               build_real (TREE_TYPE (arg), dconst0));
6165         }
6166       else if (code == GT_EXPR || code == GE_EXPR)
6167         {
6168           REAL_VALUE_TYPE c2;
6169
6170           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6171           real_convert (&c2, mode, &c2);
6172
6173           if (REAL_VALUE_ISINF (c2))
6174             {
6175               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6176               if (HONOR_INFINITIES (mode))
6177                 return fold_build2 (EQ_EXPR, type, arg,
6178                                     build_real (TREE_TYPE (arg), c2));
6179
6180               /* sqrt(x) > y is always false, when y is very large
6181                  and we don't care about infinities.  */
6182               return omit_one_operand (type, integer_zero_node, arg);
6183             }
6184
6185           /* sqrt(x) > c is the same as x > c*c.  */
6186           return fold_build2 (code, type, arg,
6187                               build_real (TREE_TYPE (arg), c2));
6188         }
6189       else if (code == LT_EXPR || code == LE_EXPR)
6190         {
6191           REAL_VALUE_TYPE c2;
6192
6193           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6194           real_convert (&c2, mode, &c2);
6195
6196           if (REAL_VALUE_ISINF (c2))
6197             {
6198               /* sqrt(x) < y is always true, when y is a very large
6199                  value and we don't care about NaNs or Infinities.  */
6200               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6201                 return omit_one_operand (type, integer_one_node, arg);
6202
6203               /* sqrt(x) < y is x != +Inf when y is very large and we
6204                  don't care about NaNs.  */
6205               if (! HONOR_NANS (mode))
6206                 return fold_build2 (NE_EXPR, type, arg,
6207                                     build_real (TREE_TYPE (arg), c2));
6208
6209               /* sqrt(x) < y is x >= 0 when y is very large and we
6210                  don't care about Infinities.  */
6211               if (! HONOR_INFINITIES (mode))
6212                 return fold_build2 (GE_EXPR, type, arg,
6213                                     build_real (TREE_TYPE (arg), dconst0));
6214
6215               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6216               if (lang_hooks.decls.global_bindings_p () != 0
6217                   || CONTAINS_PLACEHOLDER_P (arg))
6218                 return NULL_TREE;
6219
6220               arg = save_expr (arg);
6221               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6222                                   fold_build2 (GE_EXPR, type, arg,
6223                                                build_real (TREE_TYPE (arg),
6224                                                            dconst0)),
6225                                   fold_build2 (NE_EXPR, type, arg,
6226                                                build_real (TREE_TYPE (arg),
6227                                                            c2)));
6228             }
6229
6230           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6231           if (! HONOR_NANS (mode))
6232             return fold_build2 (code, type, arg,
6233                                 build_real (TREE_TYPE (arg), c2));
6234
6235           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6236           if (lang_hooks.decls.global_bindings_p () == 0
6237               && ! CONTAINS_PLACEHOLDER_P (arg))
6238             {
6239               arg = save_expr (arg);
6240               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6241                                   fold_build2 (GE_EXPR, type, arg,
6242                                                build_real (TREE_TYPE (arg),
6243                                                            dconst0)),
6244                                   fold_build2 (code, type, arg,
6245                                                build_real (TREE_TYPE (arg),
6246                                                            c2)));
6247             }
6248         }
6249     }
6250
6251   return NULL_TREE;
6252 }
6253
6254 /* Subroutine of fold() that optimizes comparisons against Infinities,
6255    either +Inf or -Inf.
6256
6257    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6258    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6259    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6260
6261    The function returns the constant folded tree if a simplification
6262    can be made, and NULL_TREE otherwise.  */
6263
6264 static tree
6265 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6266 {
6267   enum machine_mode mode;
6268   REAL_VALUE_TYPE max;
6269   tree temp;
6270   bool neg;
6271
6272   mode = TYPE_MODE (TREE_TYPE (arg0));
6273
6274   /* For negative infinity swap the sense of the comparison.  */
6275   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6276   if (neg)
6277     code = swap_tree_comparison (code);
6278
6279   switch (code)
6280     {
6281     case GT_EXPR:
6282       /* x > +Inf is always false, if with ignore sNANs.  */
6283       if (HONOR_SNANS (mode))
6284         return NULL_TREE;
6285       return omit_one_operand (type, integer_zero_node, arg0);
6286
6287     case LE_EXPR:
6288       /* x <= +Inf is always true, if we don't case about NaNs.  */
6289       if (! HONOR_NANS (mode))
6290         return omit_one_operand (type, integer_one_node, arg0);
6291
6292       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6293       if (lang_hooks.decls.global_bindings_p () == 0
6294           && ! CONTAINS_PLACEHOLDER_P (arg0))
6295         {
6296           arg0 = save_expr (arg0);
6297           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6298         }
6299       break;
6300
6301     case EQ_EXPR:
6302     case GE_EXPR:
6303       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6304       real_maxval (&max, neg, mode);
6305       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6306                           arg0, build_real (TREE_TYPE (arg0), max));
6307
6308     case LT_EXPR:
6309       /* x < +Inf is always equal to x <= DBL_MAX.  */
6310       real_maxval (&max, neg, mode);
6311       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6312                           arg0, build_real (TREE_TYPE (arg0), max));
6313
6314     case NE_EXPR:
6315       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6316       real_maxval (&max, neg, mode);
6317       if (! HONOR_NANS (mode))
6318         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6319                             arg0, build_real (TREE_TYPE (arg0), max));
6320
6321       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6322                           arg0, build_real (TREE_TYPE (arg0), max));
6323       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6324
6325     default:
6326       break;
6327     }
6328
6329   return NULL_TREE;
6330 }
6331
6332 /* Subroutine of fold() that optimizes comparisons of a division by
6333    a nonzero integer constant against an integer constant, i.e.
6334    X/C1 op C2.
6335
6336    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6337    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6338    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6339
6340    The function returns the constant folded tree if a simplification
6341    can be made, and NULL_TREE otherwise.  */
6342
6343 static tree
6344 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6345 {
6346   tree prod, tmp, hi, lo;
6347   tree arg00 = TREE_OPERAND (arg0, 0);
6348   tree arg01 = TREE_OPERAND (arg0, 1);
6349   unsigned HOST_WIDE_INT lpart;
6350   HOST_WIDE_INT hpart;
6351   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6352   bool neg_overflow;
6353   int overflow;
6354
6355   /* We have to do this the hard way to detect unsigned overflow.
6356      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6357   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6358                                    TREE_INT_CST_HIGH (arg01),
6359                                    TREE_INT_CST_LOW (arg1),
6360                                    TREE_INT_CST_HIGH (arg1),
6361                                    &lpart, &hpart, unsigned_p);
6362   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6363                                 -1, overflow);
6364   neg_overflow = false;
6365
6366   if (unsigned_p)
6367     {
6368       tmp = int_const_binop (MINUS_EXPR, arg01,
6369                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6370       lo = prod;
6371
6372       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6373       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6374                                        TREE_INT_CST_HIGH (prod),
6375                                        TREE_INT_CST_LOW (tmp),
6376                                        TREE_INT_CST_HIGH (tmp),
6377                                        &lpart, &hpart, unsigned_p);
6378       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6379                                   -1, overflow | TREE_OVERFLOW (prod));
6380     }
6381   else if (tree_int_cst_sgn (arg01) >= 0)
6382     {
6383       tmp = int_const_binop (MINUS_EXPR, arg01,
6384                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6385       switch (tree_int_cst_sgn (arg1))
6386         {
6387         case -1:
6388           neg_overflow = true;
6389           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6390           hi = prod;
6391           break;
6392
6393         case  0:
6394           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6395           hi = tmp;
6396           break;
6397
6398         case  1:
6399           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6400           lo = prod;
6401           break;
6402
6403         default:
6404           gcc_unreachable ();
6405         }
6406     }
6407   else
6408     {
6409       /* A negative divisor reverses the relational operators.  */
6410       code = swap_tree_comparison (code);
6411
6412       tmp = int_const_binop (PLUS_EXPR, arg01,
6413                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6414       switch (tree_int_cst_sgn (arg1))
6415         {
6416         case -1:
6417           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6418           lo = prod;
6419           break;
6420
6421         case  0:
6422           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6423           lo = tmp;
6424           break;
6425
6426         case  1:
6427           neg_overflow = true;
6428           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6429           hi = prod;
6430           break;
6431
6432         default:
6433           gcc_unreachable ();
6434         }
6435     }
6436
6437   switch (code)
6438     {
6439     case EQ_EXPR:
6440       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6441         return omit_one_operand (type, integer_zero_node, arg00);
6442       if (TREE_OVERFLOW (hi))
6443         return fold_build2 (GE_EXPR, type, arg00, lo);
6444       if (TREE_OVERFLOW (lo))
6445         return fold_build2 (LE_EXPR, type, arg00, hi);
6446       return build_range_check (type, arg00, 1, lo, hi);
6447
6448     case NE_EXPR:
6449       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6450         return omit_one_operand (type, integer_one_node, arg00);
6451       if (TREE_OVERFLOW (hi))
6452         return fold_build2 (LT_EXPR, type, arg00, lo);
6453       if (TREE_OVERFLOW (lo))
6454         return fold_build2 (GT_EXPR, type, arg00, hi);
6455       return build_range_check (type, arg00, 0, lo, hi);
6456
6457     case LT_EXPR:
6458       if (TREE_OVERFLOW (lo))
6459         {
6460           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6461           return omit_one_operand (type, tmp, arg00);
6462         }
6463       return fold_build2 (LT_EXPR, type, arg00, lo);
6464
6465     case LE_EXPR:
6466       if (TREE_OVERFLOW (hi))
6467         {
6468           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6469           return omit_one_operand (type, tmp, arg00);
6470         }
6471       return fold_build2 (LE_EXPR, type, arg00, hi);
6472
6473     case GT_EXPR:
6474       if (TREE_OVERFLOW (hi))
6475         {
6476           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6477           return omit_one_operand (type, tmp, arg00);
6478         }
6479       return fold_build2 (GT_EXPR, type, arg00, hi);
6480
6481     case GE_EXPR:
6482       if (TREE_OVERFLOW (lo))
6483         {
6484           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6485           return omit_one_operand (type, tmp, arg00);
6486         }
6487       return fold_build2 (GE_EXPR, type, arg00, lo);
6488
6489     default:
6490       break;
6491     }
6492
6493   return NULL_TREE;
6494 }
6495
6496
6497 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6498    equality/inequality test, then return a simplified form of the test
6499    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6500    result type.  */
6501
6502 static tree
6503 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6504                                      tree result_type)
6505 {
6506   /* If this is testing a single bit, we can optimize the test.  */
6507   if ((code == NE_EXPR || code == EQ_EXPR)
6508       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6509       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6510     {
6511       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6512          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6513       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6514
6515       if (arg00 != NULL_TREE
6516           /* This is only a win if casting to a signed type is cheap,
6517              i.e. when arg00's type is not a partial mode.  */
6518           && TYPE_PRECISION (TREE_TYPE (arg00))
6519              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6520         {
6521           tree stype = signed_type_for (TREE_TYPE (arg00));
6522           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6523                               result_type, fold_convert (stype, arg00),
6524                               build_int_cst (stype, 0));
6525         }
6526     }
6527
6528   return NULL_TREE;
6529 }
6530
6531 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6532    equality/inequality test, then return a simplified form of
6533    the test using shifts and logical operations.  Otherwise return
6534    NULL.  TYPE is the desired result type.  */
6535
6536 tree
6537 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6538                       tree result_type)
6539 {
6540   /* If this is testing a single bit, we can optimize the test.  */
6541   if ((code == NE_EXPR || code == EQ_EXPR)
6542       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6543       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6544     {
6545       tree inner = TREE_OPERAND (arg0, 0);
6546       tree type = TREE_TYPE (arg0);
6547       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6548       enum machine_mode operand_mode = TYPE_MODE (type);
6549       int ops_unsigned;
6550       tree signed_type, unsigned_type, intermediate_type;
6551       tree tem, one;
6552
6553       /* First, see if we can fold the single bit test into a sign-bit
6554          test.  */
6555       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6556                                                  result_type);
6557       if (tem)
6558         return tem;
6559
6560       /* Otherwise we have (A & C) != 0 where C is a single bit,
6561          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6562          Similarly for (A & C) == 0.  */
6563
6564       /* If INNER is a right shift of a constant and it plus BITNUM does
6565          not overflow, adjust BITNUM and INNER.  */
6566       if (TREE_CODE (inner) == RSHIFT_EXPR
6567           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6568           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6569           && bitnum < TYPE_PRECISION (type)
6570           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6571                                    bitnum - TYPE_PRECISION (type)))
6572         {
6573           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6574           inner = TREE_OPERAND (inner, 0);
6575         }
6576
6577       /* If we are going to be able to omit the AND below, we must do our
6578          operations as unsigned.  If we must use the AND, we have a choice.
6579          Normally unsigned is faster, but for some machines signed is.  */
6580 #ifdef LOAD_EXTEND_OP
6581       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6582                       && !flag_syntax_only) ? 0 : 1;
6583 #else
6584       ops_unsigned = 1;
6585 #endif
6586
6587       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6588       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6589       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6590       inner = fold_convert (intermediate_type, inner);
6591
6592       if (bitnum != 0)
6593         inner = build2 (RSHIFT_EXPR, intermediate_type,
6594                         inner, size_int (bitnum));
6595
6596       one = build_int_cst (intermediate_type, 1);
6597
6598       if (code == EQ_EXPR)
6599         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6600
6601       /* Put the AND last so it can combine with more things.  */
6602       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6603
6604       /* Make sure to return the proper type.  */
6605       inner = fold_convert (result_type, inner);
6606
6607       return inner;
6608     }
6609   return NULL_TREE;
6610 }
6611
6612 /* Check whether we are allowed to reorder operands arg0 and arg1,
6613    such that the evaluation of arg1 occurs before arg0.  */
6614
6615 static bool
6616 reorder_operands_p (const_tree arg0, const_tree arg1)
6617 {
6618   if (! flag_evaluation_order)
6619       return true;
6620   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6621     return true;
6622   return ! TREE_SIDE_EFFECTS (arg0)
6623          && ! TREE_SIDE_EFFECTS (arg1);
6624 }
6625
6626 /* Test whether it is preferable two swap two operands, ARG0 and
6627    ARG1, for example because ARG0 is an integer constant and ARG1
6628    isn't.  If REORDER is true, only recommend swapping if we can
6629    evaluate the operands in reverse order.  */
6630
6631 bool
6632 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6633 {
6634   STRIP_SIGN_NOPS (arg0);
6635   STRIP_SIGN_NOPS (arg1);
6636
6637   if (TREE_CODE (arg1) == INTEGER_CST)
6638     return 0;
6639   if (TREE_CODE (arg0) == INTEGER_CST)
6640     return 1;
6641
6642   if (TREE_CODE (arg1) == REAL_CST)
6643     return 0;
6644   if (TREE_CODE (arg0) == REAL_CST)
6645     return 1;
6646
6647   if (TREE_CODE (arg1) == FIXED_CST)
6648     return 0;
6649   if (TREE_CODE (arg0) == FIXED_CST)
6650     return 1;
6651
6652   if (TREE_CODE (arg1) == COMPLEX_CST)
6653     return 0;
6654   if (TREE_CODE (arg0) == COMPLEX_CST)
6655     return 1;
6656
6657   if (TREE_CONSTANT (arg1))
6658     return 0;
6659   if (TREE_CONSTANT (arg0))
6660     return 1;
6661
6662   if (optimize_size)
6663     return 0;
6664
6665   if (reorder && flag_evaluation_order
6666       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6667     return 0;
6668
6669   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6670      for commutative and comparison operators.  Ensuring a canonical
6671      form allows the optimizers to find additional redundancies without
6672      having to explicitly check for both orderings.  */
6673   if (TREE_CODE (arg0) == SSA_NAME
6674       && TREE_CODE (arg1) == SSA_NAME
6675       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6676     return 1;
6677
6678   /* Put SSA_NAMEs last.  */
6679   if (TREE_CODE (arg1) == SSA_NAME)
6680     return 0;
6681   if (TREE_CODE (arg0) == SSA_NAME)
6682     return 1;
6683
6684   /* Put variables last.  */
6685   if (DECL_P (arg1))
6686     return 0;
6687   if (DECL_P (arg0))
6688     return 1;
6689
6690   return 0;
6691 }
6692
6693 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6694    ARG0 is extended to a wider type.  */
6695
6696 static tree
6697 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6698 {
6699   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6700   tree arg1_unw;
6701   tree shorter_type, outer_type;
6702   tree min, max;
6703   bool above, below;
6704
6705   if (arg0_unw == arg0)
6706     return NULL_TREE;
6707   shorter_type = TREE_TYPE (arg0_unw);
6708
6709 #ifdef HAVE_canonicalize_funcptr_for_compare
6710   /* Disable this optimization if we're casting a function pointer
6711      type on targets that require function pointer canonicalization.  */
6712   if (HAVE_canonicalize_funcptr_for_compare
6713       && TREE_CODE (shorter_type) == POINTER_TYPE
6714       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6715     return NULL_TREE;
6716 #endif
6717
6718   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6719     return NULL_TREE;
6720
6721   arg1_unw = get_unwidened (arg1, NULL_TREE);
6722
6723   /* If possible, express the comparison in the shorter mode.  */
6724   if ((code == EQ_EXPR || code == NE_EXPR
6725        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6726       && (TREE_TYPE (arg1_unw) == shorter_type
6727           || (TYPE_PRECISION (shorter_type)
6728               > TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6729           || ((TYPE_PRECISION (shorter_type)
6730                == TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6731               && (TYPE_UNSIGNED (shorter_type)
6732                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6733           || (TREE_CODE (arg1_unw) == INTEGER_CST
6734               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6735                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6736               && int_fits_type_p (arg1_unw, shorter_type))))
6737     return fold_build2 (code, type, arg0_unw,
6738                        fold_convert (shorter_type, arg1_unw));
6739
6740   if (TREE_CODE (arg1_unw) != INTEGER_CST
6741       || TREE_CODE (shorter_type) != INTEGER_TYPE
6742       || !int_fits_type_p (arg1_unw, shorter_type))
6743     return NULL_TREE;
6744
6745   /* If we are comparing with the integer that does not fit into the range
6746      of the shorter type, the result is known.  */
6747   outer_type = TREE_TYPE (arg1_unw);
6748   min = lower_bound_in_type (outer_type, shorter_type);
6749   max = upper_bound_in_type (outer_type, shorter_type);
6750
6751   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6752                                                    max, arg1_unw));
6753   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6754                                                    arg1_unw, min));
6755
6756   switch (code)
6757     {
6758     case EQ_EXPR:
6759       if (above || below)
6760         return omit_one_operand (type, integer_zero_node, arg0);
6761       break;
6762
6763     case NE_EXPR:
6764       if (above || below)
6765         return omit_one_operand (type, integer_one_node, arg0);
6766       break;
6767
6768     case LT_EXPR:
6769     case LE_EXPR:
6770       if (above)
6771         return omit_one_operand (type, integer_one_node, arg0);
6772       else if (below)
6773         return omit_one_operand (type, integer_zero_node, arg0);
6774
6775     case GT_EXPR:
6776     case GE_EXPR:
6777       if (above)
6778         return omit_one_operand (type, integer_zero_node, arg0);
6779       else if (below)
6780         return omit_one_operand (type, integer_one_node, arg0);
6781
6782     default:
6783       break;
6784     }
6785
6786   return NULL_TREE;
6787 }
6788
6789 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6790    ARG0 just the signedness is changed.  */
6791
6792 static tree
6793 fold_sign_changed_comparison (enum tree_code code, tree type,
6794                               tree arg0, tree arg1)
6795 {
6796   tree arg0_inner;
6797   tree inner_type, outer_type;
6798
6799   if (TREE_CODE (arg0) != NOP_EXPR
6800       && TREE_CODE (arg0) != CONVERT_EXPR)
6801     return NULL_TREE;
6802
6803   outer_type = TREE_TYPE (arg0);
6804   arg0_inner = TREE_OPERAND (arg0, 0);
6805   inner_type = TREE_TYPE (arg0_inner);
6806
6807 #ifdef HAVE_canonicalize_funcptr_for_compare
6808   /* Disable this optimization if we're casting a function pointer
6809      type on targets that require function pointer canonicalization.  */
6810   if (HAVE_canonicalize_funcptr_for_compare
6811       && TREE_CODE (inner_type) == POINTER_TYPE
6812       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6813     return NULL_TREE;
6814 #endif
6815
6816   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6817     return NULL_TREE;
6818
6819   /* If the conversion is from an integral subtype to its basetype
6820      leave it alone.  */
6821   if (TREE_TYPE (inner_type) == outer_type)
6822     return NULL_TREE;
6823
6824   if (TREE_CODE (arg1) != INTEGER_CST
6825       && !((TREE_CODE (arg1) == NOP_EXPR
6826             || TREE_CODE (arg1) == CONVERT_EXPR)
6827            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6828     return NULL_TREE;
6829
6830   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6831       && code != NE_EXPR
6832       && code != EQ_EXPR)
6833     return NULL_TREE;
6834
6835   if (TREE_CODE (arg1) == INTEGER_CST)
6836     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6837                                   TREE_INT_CST_HIGH (arg1), 0,
6838                                   TREE_OVERFLOW (arg1));
6839   else
6840     arg1 = fold_convert (inner_type, arg1);
6841
6842   return fold_build2 (code, type, arg0_inner, arg1);
6843 }
6844
6845 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6846    step of the array.  Reconstructs s and delta in the case of s * delta
6847    being an integer constant (and thus already folded).
6848    ADDR is the address. MULT is the multiplicative expression.
6849    If the function succeeds, the new address expression is returned.  Otherwise
6850    NULL_TREE is returned.  */
6851
6852 static tree
6853 try_move_mult_to_index (tree addr, tree op1)
6854 {
6855   tree s, delta, step;
6856   tree ref = TREE_OPERAND (addr, 0), pref;
6857   tree ret, pos;
6858   tree itype;
6859   bool mdim = false;
6860
6861   /*  Strip the nops that might be added when converting op1 to sizetype. */
6862   STRIP_NOPS (op1);
6863
6864   /* Canonicalize op1 into a possibly non-constant delta
6865      and an INTEGER_CST s.  */
6866   if (TREE_CODE (op1) == MULT_EXPR)
6867     {
6868       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6869
6870       STRIP_NOPS (arg0);
6871       STRIP_NOPS (arg1);
6872   
6873       if (TREE_CODE (arg0) == INTEGER_CST)
6874         {
6875           s = arg0;
6876           delta = arg1;
6877         }
6878       else if (TREE_CODE (arg1) == INTEGER_CST)
6879         {
6880           s = arg1;
6881           delta = arg0;
6882         }
6883       else
6884         return NULL_TREE;
6885     }
6886   else if (TREE_CODE (op1) == INTEGER_CST)
6887     {
6888       delta = op1;
6889       s = NULL_TREE;
6890     }
6891   else
6892     {
6893       /* Simulate we are delta * 1.  */
6894       delta = op1;
6895       s = integer_one_node;
6896     }
6897
6898   for (;; ref = TREE_OPERAND (ref, 0))
6899     {
6900       if (TREE_CODE (ref) == ARRAY_REF)
6901         {
6902           /* Remember if this was a multi-dimensional array.  */
6903           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6904             mdim = true;
6905
6906           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6907           if (! itype)
6908             continue;
6909
6910           step = array_ref_element_size (ref);
6911           if (TREE_CODE (step) != INTEGER_CST)
6912             continue;
6913
6914           if (s)
6915             {
6916               if (! tree_int_cst_equal (step, s))
6917                 continue;
6918             }
6919           else
6920             {
6921               /* Try if delta is a multiple of step.  */
6922               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6923               if (! tmp)
6924                 continue;
6925               delta = tmp;
6926             }
6927
6928           /* Only fold here if we can verify we do not overflow one
6929              dimension of a multi-dimensional array.  */
6930           if (mdim)
6931             {
6932               tree tmp;
6933
6934               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6935                   || !INTEGRAL_TYPE_P (itype)
6936                   || !TYPE_MAX_VALUE (itype)
6937                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
6938                 continue;
6939
6940               tmp = fold_binary (PLUS_EXPR, itype,
6941                                  fold_convert (itype,
6942                                                TREE_OPERAND (ref, 1)),
6943                                  fold_convert (itype, delta));
6944               if (!tmp
6945                   || TREE_CODE (tmp) != INTEGER_CST
6946                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
6947                 continue;
6948             }
6949
6950           break;
6951         }
6952       else
6953         mdim = false;
6954
6955       if (!handled_component_p (ref))
6956         return NULL_TREE;
6957     }
6958
6959   /* We found the suitable array reference.  So copy everything up to it,
6960      and replace the index.  */
6961
6962   pref = TREE_OPERAND (addr, 0);
6963   ret = copy_node (pref);
6964   pos = ret;
6965
6966   while (pref != ref)
6967     {
6968       pref = TREE_OPERAND (pref, 0);
6969       TREE_OPERAND (pos, 0) = copy_node (pref);
6970       pos = TREE_OPERAND (pos, 0);
6971     }
6972
6973   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
6974                                        fold_convert (itype,
6975                                                      TREE_OPERAND (pos, 1)),
6976                                        fold_convert (itype, delta));
6977
6978   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6979 }
6980
6981
6982 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6983    means A >= Y && A != MAX, but in this case we know that
6984    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6985
6986 static tree
6987 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6988 {
6989   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6990
6991   if (TREE_CODE (bound) == LT_EXPR)
6992     a = TREE_OPERAND (bound, 0);
6993   else if (TREE_CODE (bound) == GT_EXPR)
6994     a = TREE_OPERAND (bound, 1);
6995   else
6996     return NULL_TREE;
6997
6998   typea = TREE_TYPE (a);
6999   if (!INTEGRAL_TYPE_P (typea)
7000       && !POINTER_TYPE_P (typea))
7001     return NULL_TREE;
7002
7003   if (TREE_CODE (ineq) == LT_EXPR)
7004     {
7005       a1 = TREE_OPERAND (ineq, 1);
7006       y = TREE_OPERAND (ineq, 0);
7007     }
7008   else if (TREE_CODE (ineq) == GT_EXPR)
7009     {
7010       a1 = TREE_OPERAND (ineq, 0);
7011       y = TREE_OPERAND (ineq, 1);
7012     }
7013   else
7014     return NULL_TREE;
7015
7016   if (TREE_TYPE (a1) != typea)
7017     return NULL_TREE;
7018
7019   if (POINTER_TYPE_P (typea))
7020     {
7021       /* Convert the pointer types into integer before taking the difference.  */
7022       tree ta = fold_convert (ssizetype, a);
7023       tree ta1 = fold_convert (ssizetype, a1);
7024       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7025     }
7026   else
7027    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7028
7029   if (!diff || !integer_onep (diff))
7030    return NULL_TREE;
7031
7032   return fold_build2 (GE_EXPR, type, a, y);
7033 }
7034
7035 /* Fold a sum or difference of at least one multiplication.
7036    Returns the folded tree or NULL if no simplification could be made.  */
7037
7038 static tree
7039 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7040 {
7041   tree arg00, arg01, arg10, arg11;
7042   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7043
7044   /* (A * C) +- (B * C) -> (A+-B) * C.
7045      (A * C) +- A -> A * (C+-1).
7046      We are most concerned about the case where C is a constant,
7047      but other combinations show up during loop reduction.  Since
7048      it is not difficult, try all four possibilities.  */
7049
7050   if (TREE_CODE (arg0) == MULT_EXPR)
7051     {
7052       arg00 = TREE_OPERAND (arg0, 0);
7053       arg01 = TREE_OPERAND (arg0, 1);
7054     }
7055   else if (TREE_CODE (arg0) == INTEGER_CST)
7056     {
7057       arg00 = build_one_cst (type);
7058       arg01 = arg0;
7059     }
7060   else
7061     {
7062       /* We cannot generate constant 1 for fract.  */
7063       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7064         return NULL_TREE;
7065       arg00 = arg0;
7066       arg01 = build_one_cst (type);
7067     }
7068   if (TREE_CODE (arg1) == MULT_EXPR)
7069     {
7070       arg10 = TREE_OPERAND (arg1, 0);
7071       arg11 = TREE_OPERAND (arg1, 1);
7072     }
7073   else if (TREE_CODE (arg1) == INTEGER_CST)
7074     {
7075       arg10 = build_one_cst (type);
7076       arg11 = arg1;
7077     }
7078   else
7079     {
7080       /* We cannot generate constant 1 for fract.  */
7081       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7082         return NULL_TREE;
7083       arg10 = arg1;
7084       arg11 = build_one_cst (type);
7085     }
7086   same = NULL_TREE;
7087
7088   if (operand_equal_p (arg01, arg11, 0))
7089     same = arg01, alt0 = arg00, alt1 = arg10;
7090   else if (operand_equal_p (arg00, arg10, 0))
7091     same = arg00, alt0 = arg01, alt1 = arg11;
7092   else if (operand_equal_p (arg00, arg11, 0))
7093     same = arg00, alt0 = arg01, alt1 = arg10;
7094   else if (operand_equal_p (arg01, arg10, 0))
7095     same = arg01, alt0 = arg00, alt1 = arg11;
7096
7097   /* No identical multiplicands; see if we can find a common
7098      power-of-two factor in non-power-of-two multiplies.  This
7099      can help in multi-dimensional array access.  */
7100   else if (host_integerp (arg01, 0)
7101            && host_integerp (arg11, 0))
7102     {
7103       HOST_WIDE_INT int01, int11, tmp;
7104       bool swap = false;
7105       tree maybe_same;
7106       int01 = TREE_INT_CST_LOW (arg01);
7107       int11 = TREE_INT_CST_LOW (arg11);
7108
7109       /* Move min of absolute values to int11.  */
7110       if ((int01 >= 0 ? int01 : -int01)
7111           < (int11 >= 0 ? int11 : -int11))
7112         {
7113           tmp = int01, int01 = int11, int11 = tmp;
7114           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7115           maybe_same = arg01;
7116           swap = true;
7117         }
7118       else
7119         maybe_same = arg11;
7120
7121       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7122         {
7123           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7124                               build_int_cst (TREE_TYPE (arg00),
7125                                              int01 / int11));
7126           alt1 = arg10;
7127           same = maybe_same;
7128           if (swap)
7129             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7130         }
7131     }
7132
7133   if (same)
7134     return fold_build2 (MULT_EXPR, type,
7135                         fold_build2 (code, type,
7136                                      fold_convert (type, alt0),
7137                                      fold_convert (type, alt1)),
7138                         fold_convert (type, same));
7139
7140   return NULL_TREE;
7141 }
7142
7143 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7144    specified by EXPR into the buffer PTR of length LEN bytes.
7145    Return the number of bytes placed in the buffer, or zero
7146    upon failure.  */
7147
7148 static int
7149 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7150 {
7151   tree type = TREE_TYPE (expr);
7152   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7153   int byte, offset, word, words;
7154   unsigned char value;
7155
7156   if (total_bytes > len)
7157     return 0;
7158   words = total_bytes / UNITS_PER_WORD;
7159
7160   for (byte = 0; byte < total_bytes; byte++)
7161     {
7162       int bitpos = byte * BITS_PER_UNIT;
7163       if (bitpos < HOST_BITS_PER_WIDE_INT)
7164         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7165       else
7166         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7167                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7168
7169       if (total_bytes > UNITS_PER_WORD)
7170         {
7171           word = byte / UNITS_PER_WORD;
7172           if (WORDS_BIG_ENDIAN)
7173             word = (words - 1) - word;
7174           offset = word * UNITS_PER_WORD;
7175           if (BYTES_BIG_ENDIAN)
7176             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7177           else
7178             offset += byte % UNITS_PER_WORD;
7179         }
7180       else
7181         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7182       ptr[offset] = value;
7183     }
7184   return total_bytes;
7185 }
7186
7187
7188 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7189    specified by EXPR into the buffer PTR of length LEN bytes.
7190    Return the number of bytes placed in the buffer, or zero
7191    upon failure.  */
7192
7193 static int
7194 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7195 {
7196   tree type = TREE_TYPE (expr);
7197   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7198   int byte, offset, word, words, bitpos;
7199   unsigned char value;
7200
7201   /* There are always 32 bits in each long, no matter the size of
7202      the hosts long.  We handle floating point representations with
7203      up to 192 bits.  */
7204   long tmp[6];
7205
7206   if (total_bytes > len)
7207     return 0;
7208   words = 32 / UNITS_PER_WORD;
7209
7210   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7211
7212   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7213        bitpos += BITS_PER_UNIT)
7214     {
7215       byte = (bitpos / BITS_PER_UNIT) & 3;
7216       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7217
7218       if (UNITS_PER_WORD < 4)
7219         {
7220           word = byte / UNITS_PER_WORD;
7221           if (WORDS_BIG_ENDIAN)
7222             word = (words - 1) - word;
7223           offset = word * UNITS_PER_WORD;
7224           if (BYTES_BIG_ENDIAN)
7225             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7226           else
7227             offset += byte % UNITS_PER_WORD;
7228         }
7229       else
7230         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7231       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7232     }
7233   return total_bytes;
7234 }
7235
7236 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7237    specified by EXPR into the buffer PTR of length LEN bytes.
7238    Return the number of bytes placed in the buffer, or zero
7239    upon failure.  */
7240
7241 static int
7242 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7243 {
7244   int rsize, isize;
7245   tree part;
7246
7247   part = TREE_REALPART (expr);
7248   rsize = native_encode_expr (part, ptr, len);
7249   if (rsize == 0)
7250     return 0;
7251   part = TREE_IMAGPART (expr);
7252   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7253   if (isize != rsize)
7254     return 0;
7255   return rsize + isize;
7256 }
7257
7258
7259 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7260    specified by EXPR into the buffer PTR of length LEN bytes.
7261    Return the number of bytes placed in the buffer, or zero
7262    upon failure.  */
7263
7264 static int
7265 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7266 {
7267   int i, size, offset, count;
7268   tree itype, elem, elements;
7269
7270   offset = 0;
7271   elements = TREE_VECTOR_CST_ELTS (expr);
7272   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7273   itype = TREE_TYPE (TREE_TYPE (expr));
7274   size = GET_MODE_SIZE (TYPE_MODE (itype));
7275   for (i = 0; i < count; i++)
7276     {
7277       if (elements)
7278         {
7279           elem = TREE_VALUE (elements);
7280           elements = TREE_CHAIN (elements);
7281         }
7282       else
7283         elem = NULL_TREE;
7284
7285       if (elem)
7286         {
7287           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7288             return 0;
7289         }
7290       else
7291         {
7292           if (offset + size > len)
7293             return 0;
7294           memset (ptr+offset, 0, size);
7295         }
7296       offset += size;
7297     }
7298   return offset;
7299 }
7300
7301
7302 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7303    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7304    buffer PTR of length LEN bytes.  Return the number of bytes
7305    placed in the buffer, or zero upon failure.  */
7306
7307 int
7308 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7309 {
7310   switch (TREE_CODE (expr))
7311     {
7312     case INTEGER_CST:
7313       return native_encode_int (expr, ptr, len);
7314
7315     case REAL_CST:
7316       return native_encode_real (expr, ptr, len);
7317
7318     case COMPLEX_CST:
7319       return native_encode_complex (expr, ptr, len);
7320
7321     case VECTOR_CST:
7322       return native_encode_vector (expr, ptr, len);
7323
7324     default:
7325       return 0;
7326     }
7327 }
7328
7329
7330 /* Subroutine of native_interpret_expr.  Interpret the contents of
7331    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7332    If the buffer cannot be interpreted, return NULL_TREE.  */
7333
7334 static tree
7335 native_interpret_int (tree type, const unsigned char *ptr, int len)
7336 {
7337   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7338   int byte, offset, word, words;
7339   unsigned char value;
7340   unsigned int HOST_WIDE_INT lo = 0;
7341   HOST_WIDE_INT hi = 0;
7342
7343   if (total_bytes > len)
7344     return NULL_TREE;
7345   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7346     return NULL_TREE;
7347   words = total_bytes / UNITS_PER_WORD;
7348
7349   for (byte = 0; byte < total_bytes; byte++)
7350     {
7351       int bitpos = byte * BITS_PER_UNIT;
7352       if (total_bytes > UNITS_PER_WORD)
7353         {
7354           word = byte / UNITS_PER_WORD;
7355           if (WORDS_BIG_ENDIAN)
7356             word = (words - 1) - word;
7357           offset = word * UNITS_PER_WORD;
7358           if (BYTES_BIG_ENDIAN)
7359             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7360           else
7361             offset += byte % UNITS_PER_WORD;
7362         }
7363       else
7364         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7365       value = ptr[offset];
7366
7367       if (bitpos < HOST_BITS_PER_WIDE_INT)
7368         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7369       else
7370         hi |= (unsigned HOST_WIDE_INT) value
7371               << (bitpos - HOST_BITS_PER_WIDE_INT);
7372     }
7373
7374   return build_int_cst_wide_type (type, lo, hi);
7375 }
7376
7377
7378 /* Subroutine of native_interpret_expr.  Interpret the contents of
7379    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7380    If the buffer cannot be interpreted, return NULL_TREE.  */
7381
7382 static tree
7383 native_interpret_real (tree type, const unsigned char *ptr, int len)
7384 {
7385   enum machine_mode mode = TYPE_MODE (type);
7386   int total_bytes = GET_MODE_SIZE (mode);
7387   int byte, offset, word, words, bitpos;
7388   unsigned char value;
7389   /* There are always 32 bits in each long, no matter the size of
7390      the hosts long.  We handle floating point representations with
7391      up to 192 bits.  */
7392   REAL_VALUE_TYPE r;
7393   long tmp[6];
7394
7395   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7396   if (total_bytes > len || total_bytes > 24)
7397     return NULL_TREE;
7398   words = 32 / UNITS_PER_WORD;
7399
7400   memset (tmp, 0, sizeof (tmp));
7401   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7402        bitpos += BITS_PER_UNIT)
7403     {
7404       byte = (bitpos / BITS_PER_UNIT) & 3;
7405       if (UNITS_PER_WORD < 4)
7406         {
7407           word = byte / UNITS_PER_WORD;
7408           if (WORDS_BIG_ENDIAN)
7409             word = (words - 1) - word;
7410           offset = word * UNITS_PER_WORD;
7411           if (BYTES_BIG_ENDIAN)
7412             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7413           else
7414             offset += byte % UNITS_PER_WORD;
7415         }
7416       else
7417         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7418       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7419
7420       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7421     }
7422
7423   real_from_target (&r, tmp, mode);
7424   return build_real (type, r);
7425 }
7426
7427
7428 /* Subroutine of native_interpret_expr.  Interpret the contents of
7429    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7430    If the buffer cannot be interpreted, return NULL_TREE.  */
7431
7432 static tree
7433 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7434 {
7435   tree etype, rpart, ipart;
7436   int size;
7437
7438   etype = TREE_TYPE (type);
7439   size = GET_MODE_SIZE (TYPE_MODE (etype));
7440   if (size * 2 > len)
7441     return NULL_TREE;
7442   rpart = native_interpret_expr (etype, ptr, size);
7443   if (!rpart)
7444     return NULL_TREE;
7445   ipart = native_interpret_expr (etype, ptr+size, size);
7446   if (!ipart)
7447     return NULL_TREE;
7448   return build_complex (type, rpart, ipart);
7449 }
7450
7451
7452 /* Subroutine of native_interpret_expr.  Interpret the contents of
7453    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7454    If the buffer cannot be interpreted, return NULL_TREE.  */
7455
7456 static tree
7457 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7458 {
7459   tree etype, elem, elements;
7460   int i, size, count;
7461
7462   etype = TREE_TYPE (type);
7463   size = GET_MODE_SIZE (TYPE_MODE (etype));
7464   count = TYPE_VECTOR_SUBPARTS (type);
7465   if (size * count > len)
7466     return NULL_TREE;
7467
7468   elements = NULL_TREE;
7469   for (i = count - 1; i >= 0; i--)
7470     {
7471       elem = native_interpret_expr (etype, ptr+(i*size), size);
7472       if (!elem)
7473         return NULL_TREE;
7474       elements = tree_cons (NULL_TREE, elem, elements);
7475     }
7476   return build_vector (type, elements);
7477 }
7478
7479
7480 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7481    the buffer PTR of length LEN as a constant of type TYPE.  For
7482    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7483    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7484    return NULL_TREE.  */
7485
7486 tree
7487 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7488 {
7489   switch (TREE_CODE (type))
7490     {
7491     case INTEGER_TYPE:
7492     case ENUMERAL_TYPE:
7493     case BOOLEAN_TYPE:
7494       return native_interpret_int (type, ptr, len);
7495
7496     case REAL_TYPE:
7497       return native_interpret_real (type, ptr, len);
7498
7499     case COMPLEX_TYPE:
7500       return native_interpret_complex (type, ptr, len);
7501
7502     case VECTOR_TYPE:
7503       return native_interpret_vector (type, ptr, len);
7504
7505     default:
7506       return NULL_TREE;
7507     }
7508 }
7509
7510
7511 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7512    TYPE at compile-time.  If we're unable to perform the conversion
7513    return NULL_TREE.  */
7514
7515 static tree
7516 fold_view_convert_expr (tree type, tree expr)
7517 {
7518   /* We support up to 512-bit values (for V8DFmode).  */
7519   unsigned char buffer[64];
7520   int len;
7521
7522   /* Check that the host and target are sane.  */
7523   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7524     return NULL_TREE;
7525
7526   len = native_encode_expr (expr, buffer, sizeof (buffer));
7527   if (len == 0)
7528     return NULL_TREE;
7529
7530   return native_interpret_expr (type, buffer, len);
7531 }
7532
7533 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7534    to avoid confusing the gimplify process.  When IN_FOLD is true
7535    avoid modifications of T.  */
7536
7537 static tree
7538 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7539 {
7540   /* The size of the object is not relevant when talking about its address.  */
7541   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7542     t = TREE_OPERAND (t, 0);
7543
7544   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7545   if (TREE_CODE (t) == INDIRECT_REF
7546       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7547     {
7548       t = TREE_OPERAND (t, 0);
7549
7550       if (TREE_TYPE (t) != ptrtype)
7551         t = build1 (NOP_EXPR, ptrtype, t);
7552     }
7553   else if (!in_fold)
7554     {
7555       tree base = t;
7556
7557       while (handled_component_p (base))
7558         base = TREE_OPERAND (base, 0);
7559
7560       if (DECL_P (base))
7561         TREE_ADDRESSABLE (base) = 1;
7562
7563       t = build1 (ADDR_EXPR, ptrtype, t);
7564     }
7565   else
7566     t = build1 (ADDR_EXPR, ptrtype, t);
7567
7568   return t;
7569 }
7570
7571 /* Build an expression for the address of T with type PTRTYPE.  This
7572    function modifies the input parameter 'T' by sometimes setting the
7573    TREE_ADDRESSABLE flag.  */
7574
7575 tree
7576 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7577 {
7578   return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7579 }
7580
7581 /* Build an expression for the address of T.  This function modifies
7582    the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7583    flag.  When called from fold functions, use fold_addr_expr instead.  */
7584
7585 tree
7586 build_fold_addr_expr (tree t)
7587 {
7588   return build_fold_addr_expr_with_type_1 (t, 
7589                                            build_pointer_type (TREE_TYPE (t)),
7590                                            false);
7591 }
7592
7593 /* Same as build_fold_addr_expr, builds an expression for the address
7594    of T, but avoids touching the input node 't'.  Fold functions
7595    should use this version.  */
7596
7597 static tree
7598 fold_addr_expr (tree t)
7599 {
7600   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7601
7602   return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7603 }
7604
7605 /* Fold a unary expression of code CODE and type TYPE with operand
7606    OP0.  Return the folded expression if folding is successful.
7607    Otherwise, return NULL_TREE.  */
7608
7609 tree
7610 fold_unary (enum tree_code code, tree type, tree op0)
7611 {
7612   tree tem;
7613   tree arg0;
7614   enum tree_code_class kind = TREE_CODE_CLASS (code);
7615
7616   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7617               && TREE_CODE_LENGTH (code) == 1);
7618
7619   arg0 = op0;
7620   if (arg0)
7621     {
7622       if (code == NOP_EXPR || code == CONVERT_EXPR
7623           || code == FLOAT_EXPR || code == ABS_EXPR)
7624         {
7625           /* Don't use STRIP_NOPS, because signedness of argument type
7626              matters.  */
7627           STRIP_SIGN_NOPS (arg0);
7628         }
7629       else
7630         {
7631           /* Strip any conversions that don't change the mode.  This
7632              is safe for every expression, except for a comparison
7633              expression because its signedness is derived from its
7634              operands.
7635
7636              Note that this is done as an internal manipulation within
7637              the constant folder, in order to find the simplest
7638              representation of the arguments so that their form can be
7639              studied.  In any cases, the appropriate type conversions
7640              should be put back in the tree that will get out of the
7641              constant folder.  */
7642           STRIP_NOPS (arg0);
7643         }
7644     }
7645
7646   if (TREE_CODE_CLASS (code) == tcc_unary)
7647     {
7648       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7649         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7650                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
7651       else if (TREE_CODE (arg0) == COND_EXPR)
7652         {
7653           tree arg01 = TREE_OPERAND (arg0, 1);
7654           tree arg02 = TREE_OPERAND (arg0, 2);
7655           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7656             arg01 = fold_build1 (code, type, arg01);
7657           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7658             arg02 = fold_build1 (code, type, arg02);
7659           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7660                              arg01, arg02);
7661
7662           /* If this was a conversion, and all we did was to move into
7663              inside the COND_EXPR, bring it back out.  But leave it if
7664              it is a conversion from integer to integer and the
7665              result precision is no wider than a word since such a
7666              conversion is cheap and may be optimized away by combine,
7667              while it couldn't if it were outside the COND_EXPR.  Then return
7668              so we don't get into an infinite recursion loop taking the
7669              conversion out and then back in.  */
7670
7671           if ((code == NOP_EXPR || code == CONVERT_EXPR
7672                || code == NON_LVALUE_EXPR)
7673               && TREE_CODE (tem) == COND_EXPR
7674               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7675               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7676               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7677               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7678               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7679                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7680               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7681                      && (INTEGRAL_TYPE_P
7682                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7683                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7684                   || flag_syntax_only))
7685             tem = build1 (code, type,
7686                           build3 (COND_EXPR,
7687                                   TREE_TYPE (TREE_OPERAND
7688                                              (TREE_OPERAND (tem, 1), 0)),
7689                                   TREE_OPERAND (tem, 0),
7690                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7691                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7692           return tem;
7693         }
7694       else if (COMPARISON_CLASS_P (arg0))
7695         {
7696           if (TREE_CODE (type) == BOOLEAN_TYPE)
7697             {
7698               arg0 = copy_node (arg0);
7699               TREE_TYPE (arg0) = type;
7700               return arg0;
7701             }
7702           else if (TREE_CODE (type) != INTEGER_TYPE)
7703             return fold_build3 (COND_EXPR, type, arg0,
7704                                 fold_build1 (code, type,
7705                                              integer_one_node),
7706                                 fold_build1 (code, type,
7707                                              integer_zero_node));
7708         }
7709    }
7710
7711   switch (code)
7712     {
7713     case PAREN_EXPR:
7714       /* Re-association barriers around constants and other re-association
7715          barriers can be removed.  */
7716       if (CONSTANT_CLASS_P (op0)
7717           || TREE_CODE (op0) == PAREN_EXPR)
7718         return fold_convert (type, op0);
7719       return NULL_TREE;
7720
7721     case NOP_EXPR:
7722     case FLOAT_EXPR:
7723     case CONVERT_EXPR:
7724     case FIX_TRUNC_EXPR:
7725       if (TREE_TYPE (op0) == type)
7726         return op0;
7727       
7728       /* If we have (type) (a CMP b) and type is an integral type, return
7729          new expression involving the new type.  */
7730       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7731         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7732                             TREE_OPERAND (op0, 1));
7733
7734       /* Handle cases of two conversions in a row.  */
7735       if (TREE_CODE (op0) == NOP_EXPR
7736           || TREE_CODE (op0) == CONVERT_EXPR)
7737         {
7738           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7739           tree inter_type = TREE_TYPE (op0);
7740           int inside_int = INTEGRAL_TYPE_P (inside_type);
7741           int inside_ptr = POINTER_TYPE_P (inside_type);
7742           int inside_float = FLOAT_TYPE_P (inside_type);
7743           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7744           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7745           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7746           int inter_int = INTEGRAL_TYPE_P (inter_type);
7747           int inter_ptr = POINTER_TYPE_P (inter_type);
7748           int inter_float = FLOAT_TYPE_P (inter_type);
7749           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7750           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7751           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7752           int final_int = INTEGRAL_TYPE_P (type);
7753           int final_ptr = POINTER_TYPE_P (type);
7754           int final_float = FLOAT_TYPE_P (type);
7755           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7756           unsigned int final_prec = TYPE_PRECISION (type);
7757           int final_unsignedp = TYPE_UNSIGNED (type);
7758
7759           /* In addition to the cases of two conversions in a row
7760              handled below, if we are converting something to its own
7761              type via an object of identical or wider precision, neither
7762              conversion is needed.  */
7763           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7764               && (((inter_int || inter_ptr) && final_int)
7765                   || (inter_float && final_float))
7766               && inter_prec >= final_prec)
7767             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7768
7769           /* Likewise, if the intermediate and final types are either both
7770              float or both integer, we don't need the middle conversion if
7771              it is wider than the final type and doesn't change the signedness
7772              (for integers).  Avoid this if the final type is a pointer
7773              since then we sometimes need the inner conversion.  Likewise if
7774              the outer has a precision not equal to the size of its mode.  */
7775           if (((inter_int && inside_int)
7776                || (inter_float && inside_float)
7777                || (inter_vec && inside_vec))
7778               && inter_prec >= inside_prec
7779               && (inter_float || inter_vec
7780                   || inter_unsignedp == inside_unsignedp)
7781               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7782                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7783               && ! final_ptr
7784               && (! final_vec || inter_prec == inside_prec))
7785             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7786
7787           /* If we have a sign-extension of a zero-extended value, we can
7788              replace that by a single zero-extension.  */
7789           if (inside_int && inter_int && final_int
7790               && inside_prec < inter_prec && inter_prec < final_prec
7791               && inside_unsignedp && !inter_unsignedp)
7792             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7793
7794           /* Two conversions in a row are not needed unless:
7795              - some conversion is floating-point (overstrict for now), or
7796              - some conversion is a vector (overstrict for now), or
7797              - the intermediate type is narrower than both initial and
7798                final, or
7799              - the intermediate type and innermost type differ in signedness,
7800                and the outermost type is wider than the intermediate, or
7801              - the initial type is a pointer type and the precisions of the
7802                intermediate and final types differ, or
7803              - the final type is a pointer type and the precisions of the
7804                initial and intermediate types differ.  */
7805           if (! inside_float && ! inter_float && ! final_float
7806               && ! inside_vec && ! inter_vec && ! final_vec
7807               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7808               && ! (inside_int && inter_int
7809                     && inter_unsignedp != inside_unsignedp
7810                     && inter_prec < final_prec)
7811               && ((inter_unsignedp && inter_prec > inside_prec)
7812                   == (final_unsignedp && final_prec > inter_prec))
7813               && ! (inside_ptr && inter_prec != final_prec)
7814               && ! (final_ptr && inside_prec != inter_prec)
7815               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7816                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7817             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7818         }
7819
7820       /* Handle (T *)&A.B.C for A being of type T and B and C
7821          living at offset zero.  This occurs frequently in
7822          C++ upcasting and then accessing the base.  */
7823       if (TREE_CODE (op0) == ADDR_EXPR
7824           && POINTER_TYPE_P (type)
7825           && handled_component_p (TREE_OPERAND (op0, 0)))
7826         {
7827           HOST_WIDE_INT bitsize, bitpos;
7828           tree offset;
7829           enum machine_mode mode;
7830           int unsignedp, volatilep;
7831           tree base = TREE_OPERAND (op0, 0);
7832           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7833                                       &mode, &unsignedp, &volatilep, false);
7834           /* If the reference was to a (constant) zero offset, we can use
7835              the address of the base if it has the same base type
7836              as the result type.  */
7837           if (! offset && bitpos == 0
7838               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7839                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7840             return fold_convert (type, fold_addr_expr (base));
7841         }
7842
7843       if ((TREE_CODE (op0) == MODIFY_EXPR
7844            || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
7845           && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
7846           /* Detect assigning a bitfield.  */
7847           && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
7848                && DECL_BIT_FIELD
7849                (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
7850         {
7851           /* Don't leave an assignment inside a conversion
7852              unless assigning a bitfield.  */
7853           tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
7854           /* First do the assignment, then return converted constant.  */
7855           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7856           TREE_NO_WARNING (tem) = 1;
7857           TREE_USED (tem) = 1;
7858           return tem;
7859         }
7860
7861       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7862          constants (if x has signed type, the sign bit cannot be set
7863          in c).  This folds extension into the BIT_AND_EXPR.  */
7864       if (INTEGRAL_TYPE_P (type)
7865           && TREE_CODE (type) != BOOLEAN_TYPE
7866           && TREE_CODE (op0) == BIT_AND_EXPR
7867           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7868         {
7869           tree and = op0;
7870           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7871           int change = 0;
7872
7873           if (TYPE_UNSIGNED (TREE_TYPE (and))
7874               || (TYPE_PRECISION (type)
7875                   <= TYPE_PRECISION (TREE_TYPE (and))))
7876             change = 1;
7877           else if (TYPE_PRECISION (TREE_TYPE (and1))
7878                    <= HOST_BITS_PER_WIDE_INT
7879                    && host_integerp (and1, 1))
7880             {
7881               unsigned HOST_WIDE_INT cst;
7882
7883               cst = tree_low_cst (and1, 1);
7884               cst &= (HOST_WIDE_INT) -1
7885                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7886               change = (cst == 0);
7887 #ifdef LOAD_EXTEND_OP
7888               if (change
7889                   && !flag_syntax_only
7890                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7891                       == ZERO_EXTEND))
7892                 {
7893                   tree uns = unsigned_type_for (TREE_TYPE (and0));
7894                   and0 = fold_convert (uns, and0);
7895                   and1 = fold_convert (uns, and1);
7896                 }
7897 #endif
7898             }
7899           if (change)
7900             {
7901               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7902                                            TREE_INT_CST_HIGH (and1), 0,
7903                                            TREE_OVERFLOW (and1));
7904               return fold_build2 (BIT_AND_EXPR, type,
7905                                   fold_convert (type, and0), tem);
7906             }
7907         }
7908
7909       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7910          when one of the new casts will fold away. Conservatively we assume
7911          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7912       if (POINTER_TYPE_P (type)
7913           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7914           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7915               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7916               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7917         {
7918           tree arg00 = TREE_OPERAND (arg0, 0);
7919           tree arg01 = TREE_OPERAND (arg0, 1);
7920
7921           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
7922                               fold_convert (sizetype, arg01));
7923         }
7924
7925       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7926          of the same precision, and X is an integer type not narrower than
7927          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7928       if (INTEGRAL_TYPE_P (type)
7929           && TREE_CODE (op0) == BIT_NOT_EXPR
7930           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7931           && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
7932               || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
7933           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7934         {
7935           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7936           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7937               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7938             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7939         }
7940
7941       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7942          type of X and Y (integer types only).  */
7943       if (INTEGRAL_TYPE_P (type)
7944           && TREE_CODE (op0) == MULT_EXPR
7945           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7946           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7947         {
7948           /* Be careful not to introduce new overflows.  */
7949           tree mult_type;
7950           if (TYPE_OVERFLOW_WRAPS (type))
7951             mult_type = type;
7952           else
7953             mult_type = unsigned_type_for (type);
7954           
7955           tem = fold_build2 (MULT_EXPR, mult_type,
7956                              fold_convert (mult_type, TREE_OPERAND (op0, 0)),
7957                              fold_convert (mult_type, TREE_OPERAND (op0, 1)));
7958           return fold_convert (type, tem);
7959         }
7960
7961       tem = fold_convert_const (code, type, op0);
7962       return tem ? tem : NULL_TREE;
7963
7964     case FIXED_CONVERT_EXPR:
7965       tem = fold_convert_const (code, type, arg0);
7966       return tem ? tem : NULL_TREE;
7967
7968     case VIEW_CONVERT_EXPR:
7969       if (TREE_TYPE (op0) == type)
7970         return op0;
7971       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7972         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7973
7974       /* For integral conversions with the same precision or pointer
7975          conversions use a NOP_EXPR instead.  */
7976       if ((INTEGRAL_TYPE_P (type)
7977            || POINTER_TYPE_P (type))
7978           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7979               || POINTER_TYPE_P (TREE_TYPE (op0)))
7980           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
7981           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
7982              a sub-type to its base type as generated by the Ada FE.  */
7983           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
7984                && TREE_TYPE (TREE_TYPE (op0))))
7985         return fold_convert (type, op0);
7986
7987       /* Strip inner integral conversions that do not change the precision.  */
7988       if ((TREE_CODE (op0) == NOP_EXPR
7989            || TREE_CODE (op0) == CONVERT_EXPR)
7990           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7991               || POINTER_TYPE_P (TREE_TYPE (op0)))
7992           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7993               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7994           && (TYPE_PRECISION (TREE_TYPE (op0))
7995               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7996         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7997
7998       return fold_view_convert_expr (type, op0);
7999
8000     case NEGATE_EXPR:
8001       tem = fold_negate_expr (arg0);
8002       if (tem)
8003         return fold_convert (type, tem);
8004       return NULL_TREE;
8005
8006     case ABS_EXPR:
8007       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8008         return fold_abs_const (arg0, type);
8009       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8010         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8011       /* Convert fabs((double)float) into (double)fabsf(float).  */
8012       else if (TREE_CODE (arg0) == NOP_EXPR
8013                && TREE_CODE (type) == REAL_TYPE)
8014         {
8015           tree targ0 = strip_float_extensions (arg0);
8016           if (targ0 != arg0)
8017             return fold_convert (type, fold_build1 (ABS_EXPR,
8018                                                     TREE_TYPE (targ0),
8019                                                     targ0));
8020         }
8021       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8022       else if (TREE_CODE (arg0) == ABS_EXPR)
8023         return arg0;
8024       else if (tree_expr_nonnegative_p (arg0))
8025         return arg0;
8026
8027       /* Strip sign ops from argument.  */
8028       if (TREE_CODE (type) == REAL_TYPE)
8029         {
8030           tem = fold_strip_sign_ops (arg0);
8031           if (tem)
8032             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8033         }
8034       return NULL_TREE;
8035
8036     case CONJ_EXPR:
8037       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8038         return fold_convert (type, arg0);
8039       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8040         {
8041           tree itype = TREE_TYPE (type);
8042           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8043           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8044           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8045         }
8046       if (TREE_CODE (arg0) == COMPLEX_CST)
8047         {
8048           tree itype = TREE_TYPE (type);
8049           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8050           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8051           return build_complex (type, rpart, negate_expr (ipart));
8052         }
8053       if (TREE_CODE (arg0) == CONJ_EXPR)
8054         return fold_convert (type, TREE_OPERAND (arg0, 0));
8055       return NULL_TREE;
8056
8057     case BIT_NOT_EXPR:
8058       if (TREE_CODE (arg0) == INTEGER_CST)
8059         return fold_not_const (arg0, type);
8060       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8061         return fold_convert (type, TREE_OPERAND (arg0, 0));
8062       /* Convert ~ (-A) to A - 1.  */
8063       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8064         return fold_build2 (MINUS_EXPR, type,
8065                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8066                             build_int_cst (type, 1));
8067       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8068       else if (INTEGRAL_TYPE_P (type)
8069                && ((TREE_CODE (arg0) == MINUS_EXPR
8070                     && integer_onep (TREE_OPERAND (arg0, 1)))
8071                    || (TREE_CODE (arg0) == PLUS_EXPR
8072                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8073         return fold_build1 (NEGATE_EXPR, type,
8074                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8075       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8076       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8077                && (tem = fold_unary (BIT_NOT_EXPR, type,
8078                                      fold_convert (type,
8079                                                    TREE_OPERAND (arg0, 0)))))
8080         return fold_build2 (BIT_XOR_EXPR, type, tem,
8081                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8082       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8083                && (tem = fold_unary (BIT_NOT_EXPR, type,
8084                                      fold_convert (type,
8085                                                    TREE_OPERAND (arg0, 1)))))
8086         return fold_build2 (BIT_XOR_EXPR, type,
8087                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8088       /* Perform BIT_NOT_EXPR on each element individually.  */
8089       else if (TREE_CODE (arg0) == VECTOR_CST)
8090         {
8091           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8092           int count = TYPE_VECTOR_SUBPARTS (type), i;
8093
8094           for (i = 0; i < count; i++)
8095             {
8096               if (elements)
8097                 {
8098                   elem = TREE_VALUE (elements);
8099                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8100                   if (elem == NULL_TREE)
8101                     break;
8102                   elements = TREE_CHAIN (elements);
8103                 }
8104               else
8105                 elem = build_int_cst (TREE_TYPE (type), -1);
8106               list = tree_cons (NULL_TREE, elem, list);
8107             }
8108           if (i == count)
8109             return build_vector (type, nreverse (list));
8110         }
8111
8112       return NULL_TREE;
8113
8114     case TRUTH_NOT_EXPR:
8115       /* The argument to invert_truthvalue must have Boolean type.  */
8116       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8117           arg0 = fold_convert (boolean_type_node, arg0);
8118
8119       /* Note that the operand of this must be an int
8120          and its values must be 0 or 1.
8121          ("true" is a fixed value perhaps depending on the language,
8122          but we don't handle values other than 1 correctly yet.)  */
8123       tem = fold_truth_not_expr (arg0);
8124       if (!tem)
8125         return NULL_TREE;
8126       return fold_convert (type, tem);
8127
8128     case REALPART_EXPR:
8129       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8130         return fold_convert (type, arg0);
8131       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8132         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8133                                  TREE_OPERAND (arg0, 1));
8134       if (TREE_CODE (arg0) == COMPLEX_CST)
8135         return fold_convert (type, TREE_REALPART (arg0));
8136       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8137         {
8138           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8139           tem = fold_build2 (TREE_CODE (arg0), itype,
8140                              fold_build1 (REALPART_EXPR, itype,
8141                                           TREE_OPERAND (arg0, 0)),
8142                              fold_build1 (REALPART_EXPR, itype,
8143                                           TREE_OPERAND (arg0, 1)));
8144           return fold_convert (type, tem);
8145         }
8146       if (TREE_CODE (arg0) == CONJ_EXPR)
8147         {
8148           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8149           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8150           return fold_convert (type, tem);
8151         }
8152       if (TREE_CODE (arg0) == CALL_EXPR)
8153         {
8154           tree fn = get_callee_fndecl (arg0);
8155           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8156             switch (DECL_FUNCTION_CODE (fn))
8157               {
8158               CASE_FLT_FN (BUILT_IN_CEXPI):
8159                 fn = mathfn_built_in (type, BUILT_IN_COS);
8160                 if (fn)
8161                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8162                 break;
8163
8164               default:
8165                 break;
8166               }
8167         }
8168       return NULL_TREE;
8169
8170     case IMAGPART_EXPR:
8171       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8172         return fold_convert (type, integer_zero_node);
8173       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8174         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8175                                  TREE_OPERAND (arg0, 0));
8176       if (TREE_CODE (arg0) == COMPLEX_CST)
8177         return fold_convert (type, TREE_IMAGPART (arg0));
8178       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8179         {
8180           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8181           tem = fold_build2 (TREE_CODE (arg0), itype,
8182                              fold_build1 (IMAGPART_EXPR, itype,
8183                                           TREE_OPERAND (arg0, 0)),
8184                              fold_build1 (IMAGPART_EXPR, itype,
8185                                           TREE_OPERAND (arg0, 1)));
8186           return fold_convert (type, tem);
8187         }
8188       if (TREE_CODE (arg0) == CONJ_EXPR)
8189         {
8190           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8191           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8192           return fold_convert (type, negate_expr (tem));
8193         }
8194       if (TREE_CODE (arg0) == CALL_EXPR)
8195         {
8196           tree fn = get_callee_fndecl (arg0);
8197           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8198             switch (DECL_FUNCTION_CODE (fn))
8199               {
8200               CASE_FLT_FN (BUILT_IN_CEXPI):
8201                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8202                 if (fn)
8203                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8204                 break;
8205
8206               default:
8207                 break;
8208               }
8209         }
8210       return NULL_TREE;
8211
8212     default:
8213       return NULL_TREE;
8214     } /* switch (code) */
8215 }
8216
8217 /* Fold a binary expression of code CODE and type TYPE with operands
8218    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8219    Return the folded expression if folding is successful.  Otherwise,
8220    return NULL_TREE.  */
8221
8222 static tree
8223 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8224 {
8225   enum tree_code compl_code;
8226
8227   if (code == MIN_EXPR)
8228     compl_code = MAX_EXPR;
8229   else if (code == MAX_EXPR)
8230     compl_code = MIN_EXPR;
8231   else
8232     gcc_unreachable ();
8233
8234   /* MIN (MAX (a, b), b) == b.  */
8235   if (TREE_CODE (op0) == compl_code
8236       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8237     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8238
8239   /* MIN (MAX (b, a), b) == b.  */
8240   if (TREE_CODE (op0) == compl_code
8241       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8242       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8243     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8244
8245   /* MIN (a, MAX (a, b)) == a.  */
8246   if (TREE_CODE (op1) == compl_code
8247       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8248       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8249     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8250
8251   /* MIN (a, MAX (b, a)) == a.  */
8252   if (TREE_CODE (op1) == compl_code
8253       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8254       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8255     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8256
8257   return NULL_TREE;
8258 }
8259
8260 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8261    by changing CODE to reduce the magnitude of constants involved in
8262    ARG0 of the comparison.
8263    Returns a canonicalized comparison tree if a simplification was
8264    possible, otherwise returns NULL_TREE.
8265    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8266    valid if signed overflow is undefined.  */
8267
8268 static tree
8269 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8270                                  tree arg0, tree arg1,
8271                                  bool *strict_overflow_p)
8272 {
8273   enum tree_code code0 = TREE_CODE (arg0);
8274   tree t, cst0 = NULL_TREE;
8275   int sgn0;
8276   bool swap = false;
8277
8278   /* Match A +- CST code arg1 and CST code arg1.  */
8279   if (!(((code0 == MINUS_EXPR
8280           || code0 == PLUS_EXPR)
8281          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8282         || code0 == INTEGER_CST))
8283     return NULL_TREE;
8284
8285   /* Identify the constant in arg0 and its sign.  */
8286   if (code0 == INTEGER_CST)
8287     cst0 = arg0;
8288   else
8289     cst0 = TREE_OPERAND (arg0, 1);
8290   sgn0 = tree_int_cst_sgn (cst0);
8291
8292   /* Overflowed constants and zero will cause problems.  */
8293   if (integer_zerop (cst0)
8294       || TREE_OVERFLOW (cst0))
8295     return NULL_TREE;
8296
8297   /* See if we can reduce the magnitude of the constant in
8298      arg0 by changing the comparison code.  */
8299   if (code0 == INTEGER_CST)
8300     {
8301       /* CST <= arg1  ->  CST-1 < arg1.  */
8302       if (code == LE_EXPR && sgn0 == 1)
8303         code = LT_EXPR;
8304       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8305       else if (code == LT_EXPR && sgn0 == -1)
8306         code = LE_EXPR;
8307       /* CST > arg1  ->  CST-1 >= arg1.  */
8308       else if (code == GT_EXPR && sgn0 == 1)
8309         code = GE_EXPR;
8310       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8311       else if (code == GE_EXPR && sgn0 == -1)
8312         code = GT_EXPR;
8313       else
8314         return NULL_TREE;
8315       /* arg1 code' CST' might be more canonical.  */
8316       swap = true;
8317     }
8318   else
8319     {
8320       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8321       if (code == LT_EXPR
8322           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8323         code = LE_EXPR;
8324       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8325       else if (code == GT_EXPR
8326                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8327         code = GE_EXPR;
8328       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8329       else if (code == LE_EXPR
8330                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8331         code = LT_EXPR;
8332       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8333       else if (code == GE_EXPR
8334                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8335         code = GT_EXPR;
8336       else
8337         return NULL_TREE;
8338       *strict_overflow_p = true;
8339     }
8340
8341   /* Now build the constant reduced in magnitude.  */
8342   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8343                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8344   if (code0 != INTEGER_CST)
8345     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8346
8347   /* If swapping might yield to a more canonical form, do so.  */
8348   if (swap)
8349     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8350   else
8351     return fold_build2 (code, type, t, arg1);
8352 }
8353
8354 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8355    overflow further.  Try to decrease the magnitude of constants involved
8356    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8357    and put sole constants at the second argument position.
8358    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8359
8360 static tree
8361 maybe_canonicalize_comparison (enum tree_code code, tree type,
8362                                tree arg0, tree arg1)
8363 {
8364   tree t;
8365   bool strict_overflow_p;
8366   const char * const warnmsg = G_("assuming signed overflow does not occur "
8367                                   "when reducing constant in comparison");
8368
8369   /* In principle pointers also have undefined overflow behavior,
8370      but that causes problems elsewhere.  */
8371   if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8372       || POINTER_TYPE_P (TREE_TYPE (arg0)))
8373     return NULL_TREE;
8374
8375   /* Try canonicalization by simplifying arg0.  */
8376   strict_overflow_p = false;
8377   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8378                                        &strict_overflow_p);
8379   if (t)
8380     {
8381       if (strict_overflow_p)
8382         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8383       return t;
8384     }
8385
8386   /* Try canonicalization by simplifying arg1 using the swapped
8387      comparison.  */
8388   code = swap_tree_comparison (code);
8389   strict_overflow_p = false;
8390   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8391                                        &strict_overflow_p);
8392   if (t && strict_overflow_p)
8393     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8394   return t;
8395 }
8396
8397 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8398    space.  This is used to avoid issuing overflow warnings for
8399    expressions like &p->x which can not wrap.  */
8400
8401 static bool
8402 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8403 {
8404   tree size;
8405   unsigned HOST_WIDE_INT offset_low, total_low;
8406   HOST_WIDE_INT offset_high, total_high;
8407
8408   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8409     return true;
8410
8411   if (bitpos < 0)
8412     return true;
8413
8414   size = size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8415   if (size == NULL_TREE || TREE_CODE (size) != INTEGER_CST)
8416     return true;
8417
8418   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8419      array.  */
8420   if (TREE_CODE (base) == ADDR_EXPR)
8421     {
8422       tree base_size = size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8423       if (base_size != NULL_TREE
8424           && TREE_CODE (base_size) == INTEGER_CST
8425           && INT_CST_LT_UNSIGNED (size, base_size))
8426         size = base_size;
8427     }
8428
8429   if (offset == NULL_TREE)
8430     {
8431       offset_low = 0;
8432       offset_high = 0;
8433     }
8434   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8435     return true;
8436   else
8437     {
8438       offset_low = TREE_INT_CST_LOW (offset);
8439       offset_high = TREE_INT_CST_HIGH (offset);
8440     }
8441
8442   if (add_double_with_sign (offset_low, offset_high,
8443                             bitpos / BITS_PER_UNIT, 0,
8444                             &total_low, &total_high,
8445                             true))
8446     return true;
8447
8448   if ((unsigned HOST_WIDE_INT) total_high
8449       < (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (size))
8450     return false;
8451   if ((unsigned HOST_WIDE_INT) total_high
8452       > (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (size))
8453     return true;
8454   return total_low > TREE_INT_CST_LOW (size);
8455 }
8456
8457 /* Subroutine of fold_binary.  This routine performs all of the
8458    transformations that are common to the equality/inequality
8459    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8460    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8461    fold_binary should call fold_binary.  Fold a comparison with
8462    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8463    the folded comparison or NULL_TREE.  */
8464
8465 static tree
8466 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8467 {
8468   tree arg0, arg1, tem;
8469
8470   arg0 = op0;
8471   arg1 = op1;
8472
8473   STRIP_SIGN_NOPS (arg0);
8474   STRIP_SIGN_NOPS (arg1);
8475
8476   tem = fold_relational_const (code, type, arg0, arg1);
8477   if (tem != NULL_TREE)
8478     return tem;
8479
8480   /* If one arg is a real or integer constant, put it last.  */
8481   if (tree_swap_operands_p (arg0, arg1, true))
8482     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8483
8484   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8485   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8486       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8487           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8488           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8489       && (TREE_CODE (arg1) == INTEGER_CST
8490           && !TREE_OVERFLOW (arg1)))
8491     {
8492       tree const1 = TREE_OPERAND (arg0, 1);
8493       tree const2 = arg1;
8494       tree variable = TREE_OPERAND (arg0, 0);
8495       tree lhs;
8496       int lhs_add;
8497       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8498
8499       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8500                          TREE_TYPE (arg1), const2, const1);
8501
8502       /* If the constant operation overflowed this can be
8503          simplified as a comparison against INT_MAX/INT_MIN.  */
8504       if (TREE_CODE (lhs) == INTEGER_CST
8505           && TREE_OVERFLOW (lhs))
8506         {
8507           int const1_sgn = tree_int_cst_sgn (const1);
8508           enum tree_code code2 = code;
8509
8510           /* Get the sign of the constant on the lhs if the
8511              operation were VARIABLE + CONST1.  */
8512           if (TREE_CODE (arg0) == MINUS_EXPR)
8513             const1_sgn = -const1_sgn;
8514
8515           /* The sign of the constant determines if we overflowed
8516              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8517              Canonicalize to the INT_MIN overflow by swapping the comparison
8518              if necessary.  */
8519           if (const1_sgn == -1)
8520             code2 = swap_tree_comparison (code);
8521
8522           /* We now can look at the canonicalized case
8523                VARIABLE + 1  CODE2  INT_MIN
8524              and decide on the result.  */
8525           if (code2 == LT_EXPR
8526               || code2 == LE_EXPR
8527               || code2 == EQ_EXPR)
8528             return omit_one_operand (type, boolean_false_node, variable);
8529           else if (code2 == NE_EXPR
8530                    || code2 == GE_EXPR
8531                    || code2 == GT_EXPR)
8532             return omit_one_operand (type, boolean_true_node, variable);
8533         }
8534
8535       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8536           && (TREE_CODE (lhs) != INTEGER_CST
8537               || !TREE_OVERFLOW (lhs)))
8538         {
8539           fold_overflow_warning (("assuming signed overflow does not occur "
8540                                   "when changing X +- C1 cmp C2 to "
8541                                   "X cmp C1 +- C2"),
8542                                  WARN_STRICT_OVERFLOW_COMPARISON);
8543           return fold_build2 (code, type, variable, lhs);
8544         }
8545     }
8546
8547   /* For comparisons of pointers we can decompose it to a compile time
8548      comparison of the base objects and the offsets into the object.
8549      This requires at least one operand being an ADDR_EXPR or a
8550      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8551   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8552       && (TREE_CODE (arg0) == ADDR_EXPR
8553           || TREE_CODE (arg1) == ADDR_EXPR
8554           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8555           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8556     {
8557       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8558       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8559       enum machine_mode mode;
8560       int volatilep, unsignedp;
8561       bool indirect_base0 = false, indirect_base1 = false;
8562
8563       /* Get base and offset for the access.  Strip ADDR_EXPR for
8564          get_inner_reference, but put it back by stripping INDIRECT_REF
8565          off the base object if possible.  indirect_baseN will be true
8566          if baseN is not an address but refers to the object itself.  */
8567       base0 = arg0;
8568       if (TREE_CODE (arg0) == ADDR_EXPR)
8569         {
8570           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8571                                        &bitsize, &bitpos0, &offset0, &mode,
8572                                        &unsignedp, &volatilep, false);
8573           if (TREE_CODE (base0) == INDIRECT_REF)
8574             base0 = TREE_OPERAND (base0, 0);
8575           else
8576             indirect_base0 = true;
8577         }
8578       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8579         {
8580           base0 = TREE_OPERAND (arg0, 0);
8581           offset0 = TREE_OPERAND (arg0, 1);
8582         }
8583
8584       base1 = arg1;
8585       if (TREE_CODE (arg1) == ADDR_EXPR)
8586         {
8587           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8588                                        &bitsize, &bitpos1, &offset1, &mode,
8589                                        &unsignedp, &volatilep, false);
8590           if (TREE_CODE (base1) == INDIRECT_REF)
8591             base1 = TREE_OPERAND (base1, 0);
8592           else
8593             indirect_base1 = true;
8594         }
8595       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8596         {
8597           base1 = TREE_OPERAND (arg1, 0);
8598           offset1 = TREE_OPERAND (arg1, 1);
8599         }
8600
8601       /* If we have equivalent bases we might be able to simplify.  */
8602       if (indirect_base0 == indirect_base1
8603           && operand_equal_p (base0, base1, 0))
8604         {
8605           /* We can fold this expression to a constant if the non-constant
8606              offset parts are equal.  */
8607           if ((offset0 == offset1
8608                || (offset0 && offset1
8609                    && operand_equal_p (offset0, offset1, 0)))
8610               && (code == EQ_EXPR
8611                   || code == NE_EXPR
8612                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8613                 
8614             {
8615               if (code != EQ_EXPR
8616                   && code != NE_EXPR
8617                   && bitpos0 != bitpos1
8618                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8619                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8620                 fold_overflow_warning (("assuming pointer wraparound does not "
8621                                         "occur when comparing P +- C1 with "
8622                                         "P +- C2"),
8623                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
8624
8625               switch (code)
8626                 {
8627                 case EQ_EXPR:
8628                   return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8629                 case NE_EXPR:
8630                   return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8631                 case LT_EXPR:
8632                   return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8633                 case LE_EXPR:
8634                   return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8635                 case GE_EXPR:
8636                   return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8637                 case GT_EXPR:
8638                   return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8639                 default:;
8640                 }
8641             }
8642           /* We can simplify the comparison to a comparison of the variable
8643              offset parts if the constant offset parts are equal.
8644              Be careful to use signed size type here because otherwise we
8645              mess with array offsets in the wrong way.  This is possible
8646              because pointer arithmetic is restricted to retain within an
8647              object and overflow on pointer differences is undefined as of
8648              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8649           else if (bitpos0 == bitpos1
8650                    && ((code == EQ_EXPR || code == NE_EXPR)
8651                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
8652             {
8653               tree signed_size_type_node;
8654               signed_size_type_node = signed_type_for (size_type_node);
8655
8656               /* By converting to signed size type we cover middle-end pointer
8657                  arithmetic which operates on unsigned pointer types of size
8658                  type size and ARRAY_REF offsets which are properly sign or
8659                  zero extended from their type in case it is narrower than
8660                  size type.  */
8661               if (offset0 == NULL_TREE)
8662                 offset0 = build_int_cst (signed_size_type_node, 0);
8663               else
8664                 offset0 = fold_convert (signed_size_type_node, offset0);
8665               if (offset1 == NULL_TREE)
8666                 offset1 = build_int_cst (signed_size_type_node, 0);
8667               else
8668                 offset1 = fold_convert (signed_size_type_node, offset1);
8669
8670               if (code != EQ_EXPR
8671                   && code != NE_EXPR
8672                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8673                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8674                 fold_overflow_warning (("assuming pointer wraparound does not "
8675                                         "occur when comparing P +- C1 with "
8676                                         "P +- C2"),
8677                                        WARN_STRICT_OVERFLOW_COMPARISON);
8678
8679               return fold_build2 (code, type, offset0, offset1);
8680             }
8681         }
8682       /* For non-equal bases we can simplify if they are addresses
8683          of local binding decls or constants.  */
8684       else if (indirect_base0 && indirect_base1
8685                /* We know that !operand_equal_p (base0, base1, 0)
8686                   because the if condition was false.  But make
8687                   sure two decls are not the same.  */
8688                && base0 != base1
8689                && TREE_CODE (arg0) == ADDR_EXPR
8690                && TREE_CODE (arg1) == ADDR_EXPR
8691                && (((TREE_CODE (base0) == VAR_DECL
8692                      || TREE_CODE (base0) == PARM_DECL)
8693                     && (targetm.binds_local_p (base0)
8694                         || CONSTANT_CLASS_P (base1)))
8695                    || CONSTANT_CLASS_P (base0))
8696                && (((TREE_CODE (base1) == VAR_DECL
8697                      || TREE_CODE (base1) == PARM_DECL)
8698                     && (targetm.binds_local_p (base1)
8699                         || CONSTANT_CLASS_P (base0)))
8700                    || CONSTANT_CLASS_P (base1)))
8701         {
8702           if (code == EQ_EXPR)
8703             return omit_two_operands (type, boolean_false_node, arg0, arg1);
8704           else if (code == NE_EXPR)
8705             return omit_two_operands (type, boolean_true_node, arg0, arg1);
8706         }
8707       /* For equal offsets we can simplify to a comparison of the
8708          base addresses.  */
8709       else if (bitpos0 == bitpos1
8710                && (indirect_base0
8711                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8712                && (indirect_base1
8713                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8714                && ((offset0 == offset1)
8715                    || (offset0 && offset1
8716                        && operand_equal_p (offset0, offset1, 0))))
8717         {
8718           if (indirect_base0)
8719             base0 = fold_addr_expr (base0);
8720           if (indirect_base1)
8721             base1 = fold_addr_expr (base1);
8722           return fold_build2 (code, type, base0, base1);
8723         }
8724     }
8725
8726   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8727      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8728      the resulting offset is smaller in absolute value than the
8729      original one.  */
8730   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8731       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8732       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8733           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8734       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8735       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8736           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8737     {
8738       tree const1 = TREE_OPERAND (arg0, 1);
8739       tree const2 = TREE_OPERAND (arg1, 1);
8740       tree variable1 = TREE_OPERAND (arg0, 0);
8741       tree variable2 = TREE_OPERAND (arg1, 0);
8742       tree cst;
8743       const char * const warnmsg = G_("assuming signed overflow does not "
8744                                       "occur when combining constants around "
8745                                       "a comparison");
8746
8747       /* Put the constant on the side where it doesn't overflow and is
8748          of lower absolute value than before.  */
8749       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8750                              ? MINUS_EXPR : PLUS_EXPR,
8751                              const2, const1, 0);
8752       if (!TREE_OVERFLOW (cst)
8753           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8754         {
8755           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8756           return fold_build2 (code, type,
8757                               variable1,
8758                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8759                                            variable2, cst));
8760         }
8761
8762       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8763                              ? MINUS_EXPR : PLUS_EXPR,
8764                              const1, const2, 0);
8765       if (!TREE_OVERFLOW (cst)
8766           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8767         {
8768           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8769           return fold_build2 (code, type,
8770                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8771                                            variable1, cst),
8772                               variable2);
8773         }
8774     }
8775
8776   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8777      signed arithmetic case.  That form is created by the compiler
8778      often enough for folding it to be of value.  One example is in
8779      computing loop trip counts after Operator Strength Reduction.  */
8780   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8781       && TREE_CODE (arg0) == MULT_EXPR
8782       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8783           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8784       && integer_zerop (arg1))
8785     {
8786       tree const1 = TREE_OPERAND (arg0, 1);
8787       tree const2 = arg1;                       /* zero */
8788       tree variable1 = TREE_OPERAND (arg0, 0);
8789       enum tree_code cmp_code = code;
8790
8791       gcc_assert (!integer_zerop (const1));
8792
8793       fold_overflow_warning (("assuming signed overflow does not occur when "
8794                               "eliminating multiplication in comparison "
8795                               "with zero"),
8796                              WARN_STRICT_OVERFLOW_COMPARISON);
8797
8798       /* If const1 is negative we swap the sense of the comparison.  */
8799       if (tree_int_cst_sgn (const1) < 0)
8800         cmp_code = swap_tree_comparison (cmp_code);
8801
8802       return fold_build2 (cmp_code, type, variable1, const2);
8803     }
8804
8805   tem = maybe_canonicalize_comparison (code, type, op0, op1);
8806   if (tem)
8807     return tem;
8808
8809   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8810     {
8811       tree targ0 = strip_float_extensions (arg0);
8812       tree targ1 = strip_float_extensions (arg1);
8813       tree newtype = TREE_TYPE (targ0);
8814
8815       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8816         newtype = TREE_TYPE (targ1);
8817
8818       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8819       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8820         return fold_build2 (code, type, fold_convert (newtype, targ0),
8821                             fold_convert (newtype, targ1));
8822
8823       /* (-a) CMP (-b) -> b CMP a  */
8824       if (TREE_CODE (arg0) == NEGATE_EXPR
8825           && TREE_CODE (arg1) == NEGATE_EXPR)
8826         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8827                             TREE_OPERAND (arg0, 0));
8828
8829       if (TREE_CODE (arg1) == REAL_CST)
8830         {
8831           REAL_VALUE_TYPE cst;
8832           cst = TREE_REAL_CST (arg1);
8833
8834           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8835           if (TREE_CODE (arg0) == NEGATE_EXPR)
8836             return fold_build2 (swap_tree_comparison (code), type,
8837                                 TREE_OPERAND (arg0, 0),
8838                                 build_real (TREE_TYPE (arg1),
8839                                             REAL_VALUE_NEGATE (cst)));
8840
8841           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8842           /* a CMP (-0) -> a CMP 0  */
8843           if (REAL_VALUE_MINUS_ZERO (cst))
8844             return fold_build2 (code, type, arg0,
8845                                 build_real (TREE_TYPE (arg1), dconst0));
8846
8847           /* x != NaN is always true, other ops are always false.  */
8848           if (REAL_VALUE_ISNAN (cst)
8849               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8850             {
8851               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8852               return omit_one_operand (type, tem, arg0);
8853             }
8854
8855           /* Fold comparisons against infinity.  */
8856           if (REAL_VALUE_ISINF (cst))
8857             {
8858               tem = fold_inf_compare (code, type, arg0, arg1);
8859               if (tem != NULL_TREE)
8860                 return tem;
8861             }
8862         }
8863
8864       /* If this is a comparison of a real constant with a PLUS_EXPR
8865          or a MINUS_EXPR of a real constant, we can convert it into a
8866          comparison with a revised real constant as long as no overflow
8867          occurs when unsafe_math_optimizations are enabled.  */
8868       if (flag_unsafe_math_optimizations
8869           && TREE_CODE (arg1) == REAL_CST
8870           && (TREE_CODE (arg0) == PLUS_EXPR
8871               || TREE_CODE (arg0) == MINUS_EXPR)
8872           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8873           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8874                                       ? MINUS_EXPR : PLUS_EXPR,
8875                                       arg1, TREE_OPERAND (arg0, 1), 0))
8876           && !TREE_OVERFLOW (tem))
8877         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8878
8879       /* Likewise, we can simplify a comparison of a real constant with
8880          a MINUS_EXPR whose first operand is also a real constant, i.e.
8881          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
8882          floating-point types only if -fassociative-math is set.  */
8883       if (flag_associative_math
8884           && TREE_CODE (arg1) == REAL_CST
8885           && TREE_CODE (arg0) == MINUS_EXPR
8886           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8887           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8888                                       arg1, 0))
8889           && !TREE_OVERFLOW (tem))
8890         return fold_build2 (swap_tree_comparison (code), type,
8891                             TREE_OPERAND (arg0, 1), tem);
8892
8893       /* Fold comparisons against built-in math functions.  */
8894       if (TREE_CODE (arg1) == REAL_CST
8895           && flag_unsafe_math_optimizations
8896           && ! flag_errno_math)
8897         {
8898           enum built_in_function fcode = builtin_mathfn_code (arg0);
8899
8900           if (fcode != END_BUILTINS)
8901             {
8902               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8903               if (tem != NULL_TREE)
8904                 return tem;
8905             }
8906         }
8907     }
8908
8909   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8910       && (TREE_CODE (arg0) == NOP_EXPR
8911           || TREE_CODE (arg0) == CONVERT_EXPR))
8912     {
8913       /* If we are widening one operand of an integer comparison,
8914          see if the other operand is similarly being widened.  Perhaps we
8915          can do the comparison in the narrower type.  */
8916       tem = fold_widened_comparison (code, type, arg0, arg1);
8917       if (tem)
8918         return tem;
8919
8920       /* Or if we are changing signedness.  */
8921       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8922       if (tem)
8923         return tem;
8924     }
8925
8926   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8927      constant, we can simplify it.  */
8928   if (TREE_CODE (arg1) == INTEGER_CST
8929       && (TREE_CODE (arg0) == MIN_EXPR
8930           || TREE_CODE (arg0) == MAX_EXPR)
8931       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8932     {
8933       tem = optimize_minmax_comparison (code, type, op0, op1);
8934       if (tem)
8935         return tem;
8936     }
8937
8938   /* Simplify comparison of something with itself.  (For IEEE
8939      floating-point, we can only do some of these simplifications.)  */
8940   if (operand_equal_p (arg0, arg1, 0))
8941     {
8942       switch (code)
8943         {
8944         case EQ_EXPR:
8945           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8946               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8947             return constant_boolean_node (1, type);
8948           break;
8949
8950         case GE_EXPR:
8951         case LE_EXPR:
8952           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8953               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8954             return constant_boolean_node (1, type);
8955           return fold_build2 (EQ_EXPR, type, arg0, arg1);
8956
8957         case NE_EXPR:
8958           /* For NE, we can only do this simplification if integer
8959              or we don't honor IEEE floating point NaNs.  */
8960           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8961               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8962             break;
8963           /* ... fall through ...  */
8964         case GT_EXPR:
8965         case LT_EXPR:
8966           return constant_boolean_node (0, type);
8967         default:
8968           gcc_unreachable ();
8969         }
8970     }
8971
8972   /* If we are comparing an expression that just has comparisons
8973      of two integer values, arithmetic expressions of those comparisons,
8974      and constants, we can simplify it.  There are only three cases
8975      to check: the two values can either be equal, the first can be
8976      greater, or the second can be greater.  Fold the expression for
8977      those three values.  Since each value must be 0 or 1, we have
8978      eight possibilities, each of which corresponds to the constant 0
8979      or 1 or one of the six possible comparisons.
8980
8981      This handles common cases like (a > b) == 0 but also handles
8982      expressions like  ((x > y) - (y > x)) > 0, which supposedly
8983      occur in macroized code.  */
8984
8985   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8986     {
8987       tree cval1 = 0, cval2 = 0;
8988       int save_p = 0;
8989
8990       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8991           /* Don't handle degenerate cases here; they should already
8992              have been handled anyway.  */
8993           && cval1 != 0 && cval2 != 0
8994           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8995           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8996           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8997           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8998           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8999           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9000                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9001         {
9002           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9003           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9004
9005           /* We can't just pass T to eval_subst in case cval1 or cval2
9006              was the same as ARG1.  */
9007
9008           tree high_result
9009                 = fold_build2 (code, type,
9010                                eval_subst (arg0, cval1, maxval,
9011                                            cval2, minval),
9012                                arg1);
9013           tree equal_result
9014                 = fold_build2 (code, type,
9015                                eval_subst (arg0, cval1, maxval,
9016                                            cval2, maxval),
9017                                arg1);
9018           tree low_result
9019                 = fold_build2 (code, type,
9020                                eval_subst (arg0, cval1, minval,
9021                                            cval2, maxval),
9022                                arg1);
9023
9024           /* All three of these results should be 0 or 1.  Confirm they are.
9025              Then use those values to select the proper code to use.  */
9026
9027           if (TREE_CODE (high_result) == INTEGER_CST
9028               && TREE_CODE (equal_result) == INTEGER_CST
9029               && TREE_CODE (low_result) == INTEGER_CST)
9030             {
9031               /* Make a 3-bit mask with the high-order bit being the
9032                  value for `>', the next for '=', and the low for '<'.  */
9033               switch ((integer_onep (high_result) * 4)
9034                       + (integer_onep (equal_result) * 2)
9035                       + integer_onep (low_result))
9036                 {
9037                 case 0:
9038                   /* Always false.  */
9039                   return omit_one_operand (type, integer_zero_node, arg0);
9040                 case 1:
9041                   code = LT_EXPR;
9042                   break;
9043                 case 2:
9044                   code = EQ_EXPR;
9045                   break;
9046                 case 3:
9047                   code = LE_EXPR;
9048                   break;
9049                 case 4:
9050                   code = GT_EXPR;
9051                   break;
9052                 case 5:
9053                   code = NE_EXPR;
9054                   break;
9055                 case 6:
9056                   code = GE_EXPR;
9057                   break;
9058                 case 7:
9059                   /* Always true.  */
9060                   return omit_one_operand (type, integer_one_node, arg0);
9061                 }
9062
9063               if (save_p)
9064                 return save_expr (build2 (code, type, cval1, cval2));
9065               return fold_build2 (code, type, cval1, cval2);
9066             }
9067         }
9068     }
9069
9070   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9071      into a single range test.  */
9072   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9073        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9074       && TREE_CODE (arg1) == INTEGER_CST
9075       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9076       && !integer_zerop (TREE_OPERAND (arg0, 1))
9077       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9078       && !TREE_OVERFLOW (arg1))
9079     {
9080       tem = fold_div_compare (code, type, arg0, arg1);
9081       if (tem != NULL_TREE)
9082         return tem;
9083     }
9084
9085   /* Fold ~X op ~Y as Y op X.  */
9086   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9087       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9088     {
9089       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9090       return fold_build2 (code, type,
9091                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9092                           TREE_OPERAND (arg0, 0));
9093     }
9094
9095   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9096   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9097       && TREE_CODE (arg1) == INTEGER_CST)
9098     {
9099       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9100       return fold_build2 (swap_tree_comparison (code), type,
9101                           TREE_OPERAND (arg0, 0),
9102                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9103                                        fold_convert (cmp_type, arg1)));
9104     }
9105
9106   return NULL_TREE;
9107 }
9108
9109
9110 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9111    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9112    argument EXPR represents the expression "z" of type TYPE.  */
9113
9114 static tree
9115 fold_mult_zconjz (tree type, tree expr)
9116 {
9117   tree itype = TREE_TYPE (type);
9118   tree rpart, ipart, tem;
9119
9120   if (TREE_CODE (expr) == COMPLEX_EXPR)
9121     {
9122       rpart = TREE_OPERAND (expr, 0);
9123       ipart = TREE_OPERAND (expr, 1);
9124     }
9125   else if (TREE_CODE (expr) == COMPLEX_CST)
9126     {
9127       rpart = TREE_REALPART (expr);
9128       ipart = TREE_IMAGPART (expr);
9129     }
9130   else
9131     {
9132       expr = save_expr (expr);
9133       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9134       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9135     }
9136
9137   rpart = save_expr (rpart);
9138   ipart = save_expr (ipart);
9139   tem = fold_build2 (PLUS_EXPR, itype,
9140                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9141                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9142   return fold_build2 (COMPLEX_EXPR, type, tem,
9143                       fold_convert (itype, integer_zero_node));
9144 }
9145
9146
9147 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9148    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9149    guarantees that P and N have the same least significant log2(M) bits.
9150    N is not otherwise constrained.  In particular, N is not normalized to
9151    0 <= N < M as is common.  In general, the precise value of P is unknown.
9152    M is chosen as large as possible such that constant N can be determined.
9153
9154    Returns M and sets *RESIDUE to N.  */
9155
9156 static unsigned HOST_WIDE_INT
9157 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9158 {
9159   enum tree_code code;
9160
9161   *residue = 0;
9162
9163   code = TREE_CODE (expr);
9164   if (code == ADDR_EXPR)
9165     {
9166       expr = TREE_OPERAND (expr, 0);
9167       if (handled_component_p (expr))
9168         {
9169           HOST_WIDE_INT bitsize, bitpos;
9170           tree offset;
9171           enum machine_mode mode;
9172           int unsignedp, volatilep;
9173
9174           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9175                                       &mode, &unsignedp, &volatilep, false);
9176           *residue = bitpos / BITS_PER_UNIT;
9177           if (offset)
9178             {
9179               if (TREE_CODE (offset) == INTEGER_CST)
9180                 *residue += TREE_INT_CST_LOW (offset);
9181               else
9182                 /* We don't handle more complicated offset expressions.  */
9183                 return 1;
9184             }
9185         }
9186
9187       if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9188         return DECL_ALIGN_UNIT (expr);
9189     }
9190   else if (code == POINTER_PLUS_EXPR)
9191     {
9192       tree op0, op1;
9193       unsigned HOST_WIDE_INT modulus;
9194       enum tree_code inner_code;
9195       
9196       op0 = TREE_OPERAND (expr, 0);
9197       STRIP_NOPS (op0);
9198       modulus = get_pointer_modulus_and_residue (op0, residue);
9199
9200       op1 = TREE_OPERAND (expr, 1);
9201       STRIP_NOPS (op1);
9202       inner_code = TREE_CODE (op1);
9203       if (inner_code == INTEGER_CST)
9204         {
9205           *residue += TREE_INT_CST_LOW (op1);
9206           return modulus;
9207         }
9208       else if (inner_code == MULT_EXPR)
9209         {
9210           op1 = TREE_OPERAND (op1, 1);
9211           if (TREE_CODE (op1) == INTEGER_CST)
9212             {
9213               unsigned HOST_WIDE_INT align;
9214               
9215               /* Compute the greatest power-of-2 divisor of op1.  */
9216               align = TREE_INT_CST_LOW (op1);
9217               align &= -align;
9218
9219               /* If align is non-zero and less than *modulus, replace
9220                  *modulus with align., If align is 0, then either op1 is 0
9221                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9222                  unsigned HOST_WIDE_INT.  In either case, no additional
9223                  constraint is imposed.  */
9224               if (align)
9225                 modulus = MIN (modulus, align);
9226
9227               return modulus;
9228             }
9229         }
9230     }
9231
9232     /* If we get here, we were unable to determine anything useful about the
9233        expression.  */
9234     return 1;
9235 }
9236
9237
9238 /* Fold a binary expression of code CODE and type TYPE with operands
9239    OP0 and OP1.  Return the folded expression if folding is
9240    successful.  Otherwise, return NULL_TREE.  */
9241
9242 tree
9243 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9244 {
9245   enum tree_code_class kind = TREE_CODE_CLASS (code);
9246   tree arg0, arg1, tem;
9247   tree t1 = NULL_TREE;
9248   bool strict_overflow_p;
9249
9250   gcc_assert ((IS_EXPR_CODE_CLASS (kind)
9251                || IS_GIMPLE_STMT_CODE_CLASS (kind))
9252               && TREE_CODE_LENGTH (code) == 2
9253               && op0 != NULL_TREE
9254               && op1 != NULL_TREE);
9255
9256   arg0 = op0;
9257   arg1 = op1;
9258
9259   /* Strip any conversions that don't change the mode.  This is
9260      safe for every expression, except for a comparison expression
9261      because its signedness is derived from its operands.  So, in
9262      the latter case, only strip conversions that don't change the
9263      signedness.
9264
9265      Note that this is done as an internal manipulation within the
9266      constant folder, in order to find the simplest representation
9267      of the arguments so that their form can be studied.  In any
9268      cases, the appropriate type conversions should be put back in
9269      the tree that will get out of the constant folder.  */
9270
9271   if (kind == tcc_comparison)
9272     {
9273       STRIP_SIGN_NOPS (arg0);
9274       STRIP_SIGN_NOPS (arg1);
9275     }
9276   else
9277     {
9278       STRIP_NOPS (arg0);
9279       STRIP_NOPS (arg1);
9280     }
9281
9282   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9283      constant but we can't do arithmetic on them.  */
9284   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9285       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9286       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9287       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9288       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9289       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9290     {
9291       if (kind == tcc_binary)
9292         {
9293           /* Make sure type and arg0 have the same saturating flag.  */
9294           gcc_assert (TYPE_SATURATING (type)
9295                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9296           tem = const_binop (code, arg0, arg1, 0);
9297         }
9298       else if (kind == tcc_comparison)
9299         tem = fold_relational_const (code, type, arg0, arg1);
9300       else
9301         tem = NULL_TREE;
9302
9303       if (tem != NULL_TREE)
9304         {
9305           if (TREE_TYPE (tem) != type)
9306             tem = fold_convert (type, tem);
9307           return tem;
9308         }
9309     }
9310
9311   /* If this is a commutative operation, and ARG0 is a constant, move it
9312      to ARG1 to reduce the number of tests below.  */
9313   if (commutative_tree_code (code)
9314       && tree_swap_operands_p (arg0, arg1, true))
9315     return fold_build2 (code, type, op1, op0);
9316
9317   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9318
9319      First check for cases where an arithmetic operation is applied to a
9320      compound, conditional, or comparison operation.  Push the arithmetic
9321      operation inside the compound or conditional to see if any folding
9322      can then be done.  Convert comparison to conditional for this purpose.
9323      The also optimizes non-constant cases that used to be done in
9324      expand_expr.
9325
9326      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9327      one of the operands is a comparison and the other is a comparison, a
9328      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9329      code below would make the expression more complex.  Change it to a
9330      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9331      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9332
9333   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9334        || code == EQ_EXPR || code == NE_EXPR)
9335       && ((truth_value_p (TREE_CODE (arg0))
9336            && (truth_value_p (TREE_CODE (arg1))
9337                || (TREE_CODE (arg1) == BIT_AND_EXPR
9338                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9339           || (truth_value_p (TREE_CODE (arg1))
9340               && (truth_value_p (TREE_CODE (arg0))
9341                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9342                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9343     {
9344       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9345                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9346                          : TRUTH_XOR_EXPR,
9347                          boolean_type_node,
9348                          fold_convert (boolean_type_node, arg0),
9349                          fold_convert (boolean_type_node, arg1));
9350
9351       if (code == EQ_EXPR)
9352         tem = invert_truthvalue (tem);
9353
9354       return fold_convert (type, tem);
9355     }
9356
9357   if (TREE_CODE_CLASS (code) == tcc_binary
9358       || TREE_CODE_CLASS (code) == tcc_comparison)
9359     {
9360       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9361         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9362                        fold_build2 (code, type,
9363                                     fold_convert (TREE_TYPE (op0),
9364                                                   TREE_OPERAND (arg0, 1)),
9365                                     op1));
9366       if (TREE_CODE (arg1) == COMPOUND_EXPR
9367           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9368         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9369                        fold_build2 (code, type, op0,
9370                                     fold_convert (TREE_TYPE (op1),
9371                                                   TREE_OPERAND (arg1, 1))));
9372
9373       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9374         {
9375           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9376                                                      arg0, arg1, 
9377                                                      /*cond_first_p=*/1);
9378           if (tem != NULL_TREE)
9379             return tem;
9380         }
9381
9382       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9383         {
9384           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9385                                                      arg1, arg0, 
9386                                                      /*cond_first_p=*/0);
9387           if (tem != NULL_TREE)
9388             return tem;
9389         }
9390     }
9391
9392   switch (code)
9393     {
9394     case POINTER_PLUS_EXPR:
9395       /* 0 +p index -> (type)index */
9396       if (integer_zerop (arg0))
9397         return non_lvalue (fold_convert (type, arg1));
9398
9399       /* PTR +p 0 -> PTR */
9400       if (integer_zerop (arg1))
9401         return non_lvalue (fold_convert (type, arg0));
9402
9403       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9404       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9405            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9406         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9407                                                 fold_convert (sizetype, arg1),
9408                                                 fold_convert (sizetype, arg0)));
9409
9410       /* index +p PTR -> PTR +p index */
9411       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9412           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9413         return fold_build2 (POINTER_PLUS_EXPR, type,
9414                             fold_convert (type, arg1),
9415                             fold_convert (sizetype, arg0));
9416
9417       /* (PTR +p B) +p A -> PTR +p (B + A) */
9418       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9419         {
9420           tree inner;
9421           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9422           tree arg00 = TREE_OPERAND (arg0, 0);
9423           inner = fold_build2 (PLUS_EXPR, sizetype,
9424                                arg01, fold_convert (sizetype, arg1));
9425           return fold_convert (type,
9426                                fold_build2 (POINTER_PLUS_EXPR,
9427                                             TREE_TYPE (arg00), arg00, inner));
9428         }
9429
9430       /* PTR_CST +p CST -> CST1 */
9431       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9432         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9433
9434      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9435         of the array.  Loop optimizer sometimes produce this type of
9436         expressions.  */
9437       if (TREE_CODE (arg0) == ADDR_EXPR)
9438         {
9439           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9440           if (tem)
9441             return fold_convert (type, tem);
9442         }
9443
9444       return NULL_TREE;
9445
9446     case PLUS_EXPR:
9447       /* PTR + INT -> (INT)(PTR p+ INT) */
9448       if (POINTER_TYPE_P (TREE_TYPE (arg0))
9449           && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9450         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9451                                                 TREE_TYPE (arg0),
9452                                                 arg0,
9453                                                 fold_convert (sizetype, arg1)));
9454       /* INT + PTR -> (INT)(PTR p+ INT) */
9455       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9456           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9457         return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9458                                                 TREE_TYPE (arg1),
9459                                                 arg1,
9460                                                 fold_convert (sizetype, arg0)));
9461       /* A + (-B) -> A - B */
9462       if (TREE_CODE (arg1) == NEGATE_EXPR)
9463         return fold_build2 (MINUS_EXPR, type,
9464                             fold_convert (type, arg0),
9465                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9466       /* (-A) + B -> B - A */
9467       if (TREE_CODE (arg0) == NEGATE_EXPR
9468           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9469         return fold_build2 (MINUS_EXPR, type,
9470                             fold_convert (type, arg1),
9471                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9472
9473       if (INTEGRAL_TYPE_P (type))
9474         {
9475           /* Convert ~A + 1 to -A.  */
9476           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9477               && integer_onep (arg1))
9478             return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
9479
9480           /* ~X + X is -1.  */
9481           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9482               && !TYPE_OVERFLOW_TRAPS (type))
9483             {
9484               tree tem = TREE_OPERAND (arg0, 0);
9485
9486               STRIP_NOPS (tem);
9487               if (operand_equal_p (tem, arg1, 0))
9488                 {
9489                   t1 = build_int_cst_type (type, -1);
9490                   return omit_one_operand (type, t1, arg1);
9491                 }
9492             }
9493
9494           /* X + ~X is -1.  */
9495           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9496               && !TYPE_OVERFLOW_TRAPS (type))
9497             {
9498               tree tem = TREE_OPERAND (arg1, 0);
9499
9500               STRIP_NOPS (tem);
9501               if (operand_equal_p (arg0, tem, 0))
9502                 {
9503                   t1 = build_int_cst_type (type, -1);
9504                   return omit_one_operand (type, t1, arg0);
9505                 }
9506             }
9507
9508           /* X + (X / CST) * -CST is X % CST.  */
9509           if (TREE_CODE (arg1) == MULT_EXPR
9510               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9511               && operand_equal_p (arg0,
9512                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9513             {
9514               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9515               tree cst1 = TREE_OPERAND (arg1, 1);
9516               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9517               if (sum && integer_zerop (sum))
9518                 return fold_convert (type,
9519                                      fold_build2 (TRUNC_MOD_EXPR,
9520                                                   TREE_TYPE (arg0), arg0, cst0));
9521             }
9522         }
9523
9524       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9525          same or one.  Make sure type is not saturating.
9526          fold_plusminus_mult_expr will re-associate.  */
9527       if ((TREE_CODE (arg0) == MULT_EXPR
9528            || TREE_CODE (arg1) == MULT_EXPR)
9529           && !TYPE_SATURATING (type)
9530           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9531         {
9532           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9533           if (tem)
9534             return tem;
9535         }
9536
9537       if (! FLOAT_TYPE_P (type))
9538         {
9539           if (integer_zerop (arg1))
9540             return non_lvalue (fold_convert (type, arg0));
9541
9542           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9543              with a constant, and the two constants have no bits in common,
9544              we should treat this as a BIT_IOR_EXPR since this may produce more
9545              simplifications.  */
9546           if (TREE_CODE (arg0) == BIT_AND_EXPR
9547               && TREE_CODE (arg1) == BIT_AND_EXPR
9548               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9549               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9550               && integer_zerop (const_binop (BIT_AND_EXPR,
9551                                              TREE_OPERAND (arg0, 1),
9552                                              TREE_OPERAND (arg1, 1), 0)))
9553             {
9554               code = BIT_IOR_EXPR;
9555               goto bit_ior;
9556             }
9557
9558           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9559              (plus (plus (mult) (mult)) (foo)) so that we can
9560              take advantage of the factoring cases below.  */
9561           if (((TREE_CODE (arg0) == PLUS_EXPR
9562                 || TREE_CODE (arg0) == MINUS_EXPR)
9563                && TREE_CODE (arg1) == MULT_EXPR)
9564               || ((TREE_CODE (arg1) == PLUS_EXPR
9565                    || TREE_CODE (arg1) == MINUS_EXPR)
9566                   && TREE_CODE (arg0) == MULT_EXPR))
9567             {
9568               tree parg0, parg1, parg, marg;
9569               enum tree_code pcode;
9570
9571               if (TREE_CODE (arg1) == MULT_EXPR)
9572                 parg = arg0, marg = arg1;
9573               else
9574                 parg = arg1, marg = arg0;
9575               pcode = TREE_CODE (parg);
9576               parg0 = TREE_OPERAND (parg, 0);
9577               parg1 = TREE_OPERAND (parg, 1);
9578               STRIP_NOPS (parg0);
9579               STRIP_NOPS (parg1);
9580
9581               if (TREE_CODE (parg0) == MULT_EXPR
9582                   && TREE_CODE (parg1) != MULT_EXPR)
9583                 return fold_build2 (pcode, type,
9584                                     fold_build2 (PLUS_EXPR, type,
9585                                                  fold_convert (type, parg0),
9586                                                  fold_convert (type, marg)),
9587                                     fold_convert (type, parg1));
9588               if (TREE_CODE (parg0) != MULT_EXPR
9589                   && TREE_CODE (parg1) == MULT_EXPR)
9590                 return fold_build2 (PLUS_EXPR, type,
9591                                     fold_convert (type, parg0),
9592                                     fold_build2 (pcode, type,
9593                                                  fold_convert (type, marg),
9594                                                  fold_convert (type,
9595                                                                parg1)));
9596             }
9597         }
9598       else
9599         {
9600           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9601           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9602             return non_lvalue (fold_convert (type, arg0));
9603
9604           /* Likewise if the operands are reversed.  */
9605           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9606             return non_lvalue (fold_convert (type, arg1));
9607
9608           /* Convert X + -C into X - C.  */
9609           if (TREE_CODE (arg1) == REAL_CST
9610               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9611             {
9612               tem = fold_negate_const (arg1, type);
9613               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9614                 return fold_build2 (MINUS_EXPR, type,
9615                                     fold_convert (type, arg0),
9616                                     fold_convert (type, tem));
9617             }
9618
9619           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9620              to __complex__ ( x, y ).  This is not the same for SNaNs or
9621              if signed zeros are involved.  */
9622           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9623               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9624               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9625             {
9626               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9627               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9628               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9629               bool arg0rz = false, arg0iz = false;
9630               if ((arg0r && (arg0rz = real_zerop (arg0r)))
9631                   || (arg0i && (arg0iz = real_zerop (arg0i))))
9632                 {
9633                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9634                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9635                   if (arg0rz && arg1i && real_zerop (arg1i))
9636                     {
9637                       tree rp = arg1r ? arg1r
9638                                   : build1 (REALPART_EXPR, rtype, arg1);
9639                       tree ip = arg0i ? arg0i
9640                                   : build1 (IMAGPART_EXPR, rtype, arg0);
9641                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9642                     }
9643                   else if (arg0iz && arg1r && real_zerop (arg1r))
9644                     {
9645                       tree rp = arg0r ? arg0r
9646                                   : build1 (REALPART_EXPR, rtype, arg0);
9647                       tree ip = arg1i ? arg1i
9648                                   : build1 (IMAGPART_EXPR, rtype, arg1);
9649                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9650                     }
9651                 }
9652             }
9653
9654           if (flag_unsafe_math_optimizations
9655               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9656               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9657               && (tem = distribute_real_division (code, type, arg0, arg1)))
9658             return tem;
9659
9660           /* Convert x+x into x*2.0.  */
9661           if (operand_equal_p (arg0, arg1, 0)
9662               && SCALAR_FLOAT_TYPE_P (type))
9663             return fold_build2 (MULT_EXPR, type, arg0,
9664                                 build_real (type, dconst2));
9665
9666           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
9667              We associate floats only if the user has specified
9668              -fassociative-math.  */
9669           if (flag_associative_math
9670               && TREE_CODE (arg1) == PLUS_EXPR
9671               && TREE_CODE (arg0) != MULT_EXPR)
9672             {
9673               tree tree10 = TREE_OPERAND (arg1, 0);
9674               tree tree11 = TREE_OPERAND (arg1, 1);
9675               if (TREE_CODE (tree11) == MULT_EXPR
9676                   && TREE_CODE (tree10) == MULT_EXPR)
9677                 {
9678                   tree tree0;
9679                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9680                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9681                 }
9682             }
9683           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
9684              We associate floats only if the user has specified
9685              -fassociative-math.  */
9686           if (flag_associative_math
9687               && TREE_CODE (arg0) == PLUS_EXPR
9688               && TREE_CODE (arg1) != MULT_EXPR)
9689             {
9690               tree tree00 = TREE_OPERAND (arg0, 0);
9691               tree tree01 = TREE_OPERAND (arg0, 1);
9692               if (TREE_CODE (tree01) == MULT_EXPR
9693                   && TREE_CODE (tree00) == MULT_EXPR)
9694                 {
9695                   tree tree0;
9696                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9697                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9698                 }
9699             }
9700         }
9701
9702      bit_rotate:
9703       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9704          is a rotate of A by C1 bits.  */
9705       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9706          is a rotate of A by B bits.  */
9707       {
9708         enum tree_code code0, code1;
9709         tree rtype;
9710         code0 = TREE_CODE (arg0);
9711         code1 = TREE_CODE (arg1);
9712         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9713              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9714             && operand_equal_p (TREE_OPERAND (arg0, 0),
9715                                 TREE_OPERAND (arg1, 0), 0)
9716             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9717                 TYPE_UNSIGNED (rtype))
9718             /* Only create rotates in complete modes.  Other cases are not
9719                expanded properly.  */
9720             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9721           {
9722             tree tree01, tree11;
9723             enum tree_code code01, code11;
9724
9725             tree01 = TREE_OPERAND (arg0, 1);
9726             tree11 = TREE_OPERAND (arg1, 1);
9727             STRIP_NOPS (tree01);
9728             STRIP_NOPS (tree11);
9729             code01 = TREE_CODE (tree01);
9730             code11 = TREE_CODE (tree11);
9731             if (code01 == INTEGER_CST
9732                 && code11 == INTEGER_CST
9733                 && TREE_INT_CST_HIGH (tree01) == 0
9734                 && TREE_INT_CST_HIGH (tree11) == 0
9735                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9736                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9737               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9738                              code0 == LSHIFT_EXPR ? tree01 : tree11);
9739             else if (code11 == MINUS_EXPR)
9740               {
9741                 tree tree110, tree111;
9742                 tree110 = TREE_OPERAND (tree11, 0);
9743                 tree111 = TREE_OPERAND (tree11, 1);
9744                 STRIP_NOPS (tree110);
9745                 STRIP_NOPS (tree111);
9746                 if (TREE_CODE (tree110) == INTEGER_CST
9747                     && 0 == compare_tree_int (tree110,
9748                                               TYPE_PRECISION
9749                                               (TREE_TYPE (TREE_OPERAND
9750                                                           (arg0, 0))))
9751                     && operand_equal_p (tree01, tree111, 0))
9752                   return build2 ((code0 == LSHIFT_EXPR
9753                                   ? LROTATE_EXPR
9754                                   : RROTATE_EXPR),
9755                                  type, TREE_OPERAND (arg0, 0), tree01);
9756               }
9757             else if (code01 == MINUS_EXPR)
9758               {
9759                 tree tree010, tree011;
9760                 tree010 = TREE_OPERAND (tree01, 0);
9761                 tree011 = TREE_OPERAND (tree01, 1);
9762                 STRIP_NOPS (tree010);
9763                 STRIP_NOPS (tree011);
9764                 if (TREE_CODE (tree010) == INTEGER_CST
9765                     && 0 == compare_tree_int (tree010,
9766                                               TYPE_PRECISION
9767                                               (TREE_TYPE (TREE_OPERAND
9768                                                           (arg0, 0))))
9769                     && operand_equal_p (tree11, tree011, 0))
9770                   return build2 ((code0 != LSHIFT_EXPR
9771                                   ? LROTATE_EXPR
9772                                   : RROTATE_EXPR),
9773                                  type, TREE_OPERAND (arg0, 0), tree11);
9774               }
9775           }
9776       }
9777
9778     associate:
9779       /* In most languages, can't associate operations on floats through
9780          parentheses.  Rather than remember where the parentheses were, we
9781          don't associate floats at all, unless the user has specified
9782          -fassociative-math.
9783          And, we need to make sure type is not saturating.  */
9784
9785       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9786           && !TYPE_SATURATING (type))
9787         {
9788           tree var0, con0, lit0, minus_lit0;
9789           tree var1, con1, lit1, minus_lit1;
9790           bool ok = true;
9791
9792           /* Split both trees into variables, constants, and literals.  Then
9793              associate each group together, the constants with literals,
9794              then the result with variables.  This increases the chances of
9795              literals being recombined later and of generating relocatable
9796              expressions for the sum of a constant and literal.  */
9797           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9798           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9799                              code == MINUS_EXPR);
9800
9801           /* With undefined overflow we can only associate constants
9802              with one variable.  */
9803           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9804                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9805               && var0 && var1)
9806             {
9807               tree tmp0 = var0;
9808               tree tmp1 = var1;
9809
9810               if (TREE_CODE (tmp0) == NEGATE_EXPR)
9811                 tmp0 = TREE_OPERAND (tmp0, 0);
9812               if (TREE_CODE (tmp1) == NEGATE_EXPR)
9813                 tmp1 = TREE_OPERAND (tmp1, 0);
9814               /* The only case we can still associate with two variables
9815                  is if they are the same, modulo negation.  */
9816               if (!operand_equal_p (tmp0, tmp1, 0))
9817                 ok = false;
9818             }
9819
9820           /* Only do something if we found more than two objects.  Otherwise,
9821              nothing has changed and we risk infinite recursion.  */
9822           if (ok
9823               && (2 < ((var0 != 0) + (var1 != 0)
9824                        + (con0 != 0) + (con1 != 0)
9825                        + (lit0 != 0) + (lit1 != 0)
9826                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
9827             {
9828               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9829               if (code == MINUS_EXPR)
9830                 code = PLUS_EXPR;
9831
9832               var0 = associate_trees (var0, var1, code, type);
9833               con0 = associate_trees (con0, con1, code, type);
9834               lit0 = associate_trees (lit0, lit1, code, type);
9835               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9836
9837               /* Preserve the MINUS_EXPR if the negative part of the literal is
9838                  greater than the positive part.  Otherwise, the multiplicative
9839                  folding code (i.e extract_muldiv) may be fooled in case
9840                  unsigned constants are subtracted, like in the following
9841                  example: ((X*2 + 4) - 8U)/2.  */
9842               if (minus_lit0 && lit0)
9843                 {
9844                   if (TREE_CODE (lit0) == INTEGER_CST
9845                       && TREE_CODE (minus_lit0) == INTEGER_CST
9846                       && tree_int_cst_lt (lit0, minus_lit0))
9847                     {
9848                       minus_lit0 = associate_trees (minus_lit0, lit0,
9849                                                     MINUS_EXPR, type);
9850                       lit0 = 0;
9851                     }
9852                   else
9853                     {
9854                       lit0 = associate_trees (lit0, minus_lit0,
9855                                               MINUS_EXPR, type);
9856                       minus_lit0 = 0;
9857                     }
9858                 }
9859               if (minus_lit0)
9860                 {
9861                   if (con0 == 0)
9862                     return fold_convert (type,
9863                                          associate_trees (var0, minus_lit0,
9864                                                           MINUS_EXPR, type));
9865                   else
9866                     {
9867                       con0 = associate_trees (con0, minus_lit0,
9868                                               MINUS_EXPR, type);
9869                       return fold_convert (type,
9870                                            associate_trees (var0, con0,
9871                                                             PLUS_EXPR, type));
9872                     }
9873                 }
9874
9875               con0 = associate_trees (con0, lit0, code, type);
9876               return fold_convert (type, associate_trees (var0, con0,
9877                                                           code, type));
9878             }
9879         }
9880
9881       return NULL_TREE;
9882
9883     case MINUS_EXPR:
9884       /* Pointer simplifications for subtraction, simple reassociations. */
9885       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9886         {
9887           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9888           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9889               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9890             {
9891               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9892               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9893               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9894               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9895               return fold_build2 (PLUS_EXPR, type,
9896                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
9897                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
9898             }
9899           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9900           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9901             {
9902               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9903               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9904               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9905               if (tmp)
9906                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9907             }
9908         }
9909       /* A - (-B) -> A + B */
9910       if (TREE_CODE (arg1) == NEGATE_EXPR)
9911         return fold_build2 (PLUS_EXPR, type, op0,
9912                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9913       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
9914       if (TREE_CODE (arg0) == NEGATE_EXPR
9915           && (FLOAT_TYPE_P (type)
9916               || INTEGRAL_TYPE_P (type))
9917           && negate_expr_p (arg1)
9918           && reorder_operands_p (arg0, arg1))
9919         return fold_build2 (MINUS_EXPR, type,
9920                             fold_convert (type, negate_expr (arg1)),
9921                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9922       /* Convert -A - 1 to ~A.  */
9923       if (INTEGRAL_TYPE_P (type)
9924           && TREE_CODE (arg0) == NEGATE_EXPR
9925           && integer_onep (arg1)
9926           && !TYPE_OVERFLOW_TRAPS (type))
9927         return fold_build1 (BIT_NOT_EXPR, type,
9928                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9929
9930       /* Convert -1 - A to ~A.  */
9931       if (INTEGRAL_TYPE_P (type)
9932           && integer_all_onesp (arg0))
9933         return fold_build1 (BIT_NOT_EXPR, type, op1);
9934
9935
9936       /* X - (X / CST) * CST is X % CST.  */
9937       if (INTEGRAL_TYPE_P (type)
9938           && TREE_CODE (arg1) == MULT_EXPR
9939           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9940           && operand_equal_p (arg0,
9941                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
9942           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
9943                               TREE_OPERAND (arg1, 1), 0))
9944         return fold_convert (type,
9945                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
9946                                           arg0, TREE_OPERAND (arg1, 1)));
9947
9948       if (! FLOAT_TYPE_P (type))
9949         {
9950           if (integer_zerop (arg0))
9951             return negate_expr (fold_convert (type, arg1));
9952           if (integer_zerop (arg1))
9953             return non_lvalue (fold_convert (type, arg0));
9954
9955           /* Fold A - (A & B) into ~B & A.  */
9956           if (!TREE_SIDE_EFFECTS (arg0)
9957               && TREE_CODE (arg1) == BIT_AND_EXPR)
9958             {
9959               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9960                 {
9961                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9962                   return fold_build2 (BIT_AND_EXPR, type,
9963                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
9964                                       fold_convert (type, arg0));
9965                 }
9966               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9967                 {
9968                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9969                   return fold_build2 (BIT_AND_EXPR, type,
9970                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
9971                                       fold_convert (type, arg0));
9972                 }
9973             }
9974
9975           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9976              any power of 2 minus 1.  */
9977           if (TREE_CODE (arg0) == BIT_AND_EXPR
9978               && TREE_CODE (arg1) == BIT_AND_EXPR
9979               && operand_equal_p (TREE_OPERAND (arg0, 0),
9980                                   TREE_OPERAND (arg1, 0), 0))
9981             {
9982               tree mask0 = TREE_OPERAND (arg0, 1);
9983               tree mask1 = TREE_OPERAND (arg1, 1);
9984               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
9985
9986               if (operand_equal_p (tem, mask1, 0))
9987                 {
9988                   tem = fold_build2 (BIT_XOR_EXPR, type,
9989                                      TREE_OPERAND (arg0, 0), mask1);
9990                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
9991                 }
9992             }
9993         }
9994
9995       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
9996       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
9997         return non_lvalue (fold_convert (type, arg0));
9998
9999       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10000          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10001          (-ARG1 + ARG0) reduces to -ARG1.  */
10002       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10003         return negate_expr (fold_convert (type, arg1));
10004
10005       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10006          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10007          signed zeros are involved.  */
10008       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10009           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10010           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10011         {
10012           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10013           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10014           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10015           bool arg0rz = false, arg0iz = false;
10016           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10017               || (arg0i && (arg0iz = real_zerop (arg0i))))
10018             {
10019               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10020               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10021               if (arg0rz && arg1i && real_zerop (arg1i))
10022                 {
10023                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10024                                          arg1r ? arg1r
10025                                          : build1 (REALPART_EXPR, rtype, arg1));
10026                   tree ip = arg0i ? arg0i
10027                     : build1 (IMAGPART_EXPR, rtype, arg0);
10028                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10029                 }
10030               else if (arg0iz && arg1r && real_zerop (arg1r))
10031                 {
10032                   tree rp = arg0r ? arg0r
10033                     : build1 (REALPART_EXPR, rtype, arg0);
10034                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10035                                          arg1i ? arg1i
10036                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10037                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10038                 }
10039             }
10040         }
10041
10042       /* Fold &x - &x.  This can happen from &x.foo - &x.
10043          This is unsafe for certain floats even in non-IEEE formats.
10044          In IEEE, it is unsafe because it does wrong for NaNs.
10045          Also note that operand_equal_p is always false if an operand
10046          is volatile.  */
10047
10048       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10049           && operand_equal_p (arg0, arg1, 0))
10050         return fold_convert (type, integer_zero_node);
10051
10052       /* A - B -> A + (-B) if B is easily negatable.  */
10053       if (negate_expr_p (arg1)
10054           && ((FLOAT_TYPE_P (type)
10055                /* Avoid this transformation if B is a positive REAL_CST.  */
10056                && (TREE_CODE (arg1) != REAL_CST
10057                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10058               || INTEGRAL_TYPE_P (type)))
10059         return fold_build2 (PLUS_EXPR, type,
10060                             fold_convert (type, arg0),
10061                             fold_convert (type, negate_expr (arg1)));
10062
10063       /* Try folding difference of addresses.  */
10064       {
10065         HOST_WIDE_INT diff;
10066
10067         if ((TREE_CODE (arg0) == ADDR_EXPR
10068              || TREE_CODE (arg1) == ADDR_EXPR)
10069             && ptr_difference_const (arg0, arg1, &diff))
10070           return build_int_cst_type (type, diff);
10071       }
10072
10073       /* Fold &a[i] - &a[j] to i-j.  */
10074       if (TREE_CODE (arg0) == ADDR_EXPR
10075           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10076           && TREE_CODE (arg1) == ADDR_EXPR
10077           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10078         {
10079           tree aref0 = TREE_OPERAND (arg0, 0);
10080           tree aref1 = TREE_OPERAND (arg1, 0);
10081           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10082                                TREE_OPERAND (aref1, 0), 0))
10083             {
10084               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10085               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10086               tree esz = array_ref_element_size (aref0);
10087               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10088               return fold_build2 (MULT_EXPR, type, diff,
10089                                   fold_convert (type, esz));
10090                                   
10091             }
10092         }
10093
10094       if (flag_unsafe_math_optimizations
10095           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10096           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10097           && (tem = distribute_real_division (code, type, arg0, arg1)))
10098         return tem;
10099
10100       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10101          same or one.  Make sure type is not saturating.
10102          fold_plusminus_mult_expr will re-associate.  */
10103       if ((TREE_CODE (arg0) == MULT_EXPR
10104            || TREE_CODE (arg1) == MULT_EXPR)
10105           && !TYPE_SATURATING (type)
10106           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10107         {
10108           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10109           if (tem)
10110             return tem;
10111         }
10112
10113       goto associate;
10114
10115     case MULT_EXPR:
10116       /* (-A) * (-B) -> A * B  */
10117       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10118         return fold_build2 (MULT_EXPR, type,
10119                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10120                             fold_convert (type, negate_expr (arg1)));
10121       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10122         return fold_build2 (MULT_EXPR, type,
10123                             fold_convert (type, negate_expr (arg0)),
10124                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10125
10126       if (! FLOAT_TYPE_P (type))
10127         {
10128           if (integer_zerop (arg1))
10129             return omit_one_operand (type, arg1, arg0);
10130           if (integer_onep (arg1))
10131             return non_lvalue (fold_convert (type, arg0));
10132           /* Transform x * -1 into -x.  Make sure to do the negation
10133              on the original operand with conversions not stripped
10134              because we can only strip non-sign-changing conversions.  */
10135           if (integer_all_onesp (arg1))
10136             return fold_convert (type, negate_expr (op0));
10137           /* Transform x * -C into -x * C if x is easily negatable.  */
10138           if (TREE_CODE (arg1) == INTEGER_CST
10139               && tree_int_cst_sgn (arg1) == -1
10140               && negate_expr_p (arg0)
10141               && (tem = negate_expr (arg1)) != arg1
10142               && !TREE_OVERFLOW (tem))
10143             return fold_build2 (MULT_EXPR, type,
10144                                 fold_convert (type, negate_expr (arg0)), tem);
10145
10146           /* (a * (1 << b)) is (a << b)  */
10147           if (TREE_CODE (arg1) == LSHIFT_EXPR
10148               && integer_onep (TREE_OPERAND (arg1, 0)))
10149             return fold_build2 (LSHIFT_EXPR, type, op0,
10150                                 TREE_OPERAND (arg1, 1));
10151           if (TREE_CODE (arg0) == LSHIFT_EXPR
10152               && integer_onep (TREE_OPERAND (arg0, 0)))
10153             return fold_build2 (LSHIFT_EXPR, type, op1,
10154                                 TREE_OPERAND (arg0, 1));
10155
10156           strict_overflow_p = false;
10157           if (TREE_CODE (arg1) == INTEGER_CST
10158               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10159                                              &strict_overflow_p)))
10160             {
10161               if (strict_overflow_p)
10162                 fold_overflow_warning (("assuming signed overflow does not "
10163                                         "occur when simplifying "
10164                                         "multiplication"),
10165                                        WARN_STRICT_OVERFLOW_MISC);
10166               return fold_convert (type, tem);
10167             }
10168
10169           /* Optimize z * conj(z) for integer complex numbers.  */
10170           if (TREE_CODE (arg0) == CONJ_EXPR
10171               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10172             return fold_mult_zconjz (type, arg1);
10173           if (TREE_CODE (arg1) == CONJ_EXPR
10174               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10175             return fold_mult_zconjz (type, arg0);
10176         }
10177       else
10178         {
10179           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10180              when x is NaN, since x * 0 is also NaN.  Nor are they the
10181              same in modes with signed zeros, since multiplying a
10182              negative value by 0 gives -0, not +0.  */
10183           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10184               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10185               && real_zerop (arg1))
10186             return omit_one_operand (type, arg1, arg0);
10187           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10188           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10189               && real_onep (arg1))
10190             return non_lvalue (fold_convert (type, arg0));
10191
10192           /* Transform x * -1.0 into -x.  */
10193           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10194               && real_minus_onep (arg1))
10195             return fold_convert (type, negate_expr (arg0));
10196
10197           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10198              the result for floating point types due to rounding so it is applied
10199              only if -fassociative-math was specify.  */
10200           if (flag_associative_math
10201               && TREE_CODE (arg0) == RDIV_EXPR
10202               && TREE_CODE (arg1) == REAL_CST
10203               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10204             {
10205               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10206                                       arg1, 0);
10207               if (tem)
10208                 return fold_build2 (RDIV_EXPR, type, tem,
10209                                     TREE_OPERAND (arg0, 1));
10210             }
10211
10212           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10213           if (operand_equal_p (arg0, arg1, 0))
10214             {
10215               tree tem = fold_strip_sign_ops (arg0);
10216               if (tem != NULL_TREE)
10217                 {
10218                   tem = fold_convert (type, tem);
10219                   return fold_build2 (MULT_EXPR, type, tem, tem);
10220                 }
10221             }
10222
10223           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10224              This is not the same for NaNs or if signed zeros are
10225              involved.  */
10226           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10227               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10228               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10229               && TREE_CODE (arg1) == COMPLEX_CST
10230               && real_zerop (TREE_REALPART (arg1)))
10231             {
10232               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10233               if (real_onep (TREE_IMAGPART (arg1)))
10234                 return fold_build2 (COMPLEX_EXPR, type,
10235                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10236                                                               rtype, arg0)),
10237                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10238               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10239                 return fold_build2 (COMPLEX_EXPR, type,
10240                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10241                                     negate_expr (fold_build1 (REALPART_EXPR,
10242                                                               rtype, arg0)));
10243             }
10244
10245           /* Optimize z * conj(z) for floating point complex numbers.
10246              Guarded by flag_unsafe_math_optimizations as non-finite
10247              imaginary components don't produce scalar results.  */
10248           if (flag_unsafe_math_optimizations
10249               && TREE_CODE (arg0) == CONJ_EXPR
10250               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10251             return fold_mult_zconjz (type, arg1);
10252           if (flag_unsafe_math_optimizations
10253               && TREE_CODE (arg1) == CONJ_EXPR
10254               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10255             return fold_mult_zconjz (type, arg0);
10256
10257           if (flag_unsafe_math_optimizations)
10258             {
10259               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10260               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10261
10262               /* Optimizations of root(...)*root(...).  */
10263               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10264                 {
10265                   tree rootfn, arg;
10266                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10267                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10268
10269                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10270                   if (BUILTIN_SQRT_P (fcode0)
10271                       && operand_equal_p (arg00, arg10, 0)
10272                       && ! HONOR_SNANS (TYPE_MODE (type)))
10273                     return arg00;
10274
10275                   /* Optimize root(x)*root(y) as root(x*y).  */
10276                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10277                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10278                   return build_call_expr (rootfn, 1, arg);
10279                 }
10280
10281               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10282               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10283                 {
10284                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10285                   tree arg = fold_build2 (PLUS_EXPR, type,
10286                                           CALL_EXPR_ARG (arg0, 0),
10287                                           CALL_EXPR_ARG (arg1, 0));
10288                   return build_call_expr (expfn, 1, arg);
10289                 }
10290
10291               /* Optimizations of pow(...)*pow(...).  */
10292               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10293                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10294                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10295                 {
10296                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10297                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10298                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10299                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10300
10301                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10302                   if (operand_equal_p (arg01, arg11, 0))
10303                     {
10304                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10305                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10306                       return build_call_expr (powfn, 2, arg, arg01);
10307                     }
10308
10309                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10310                   if (operand_equal_p (arg00, arg10, 0))
10311                     {
10312                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10313                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10314                       return build_call_expr (powfn, 2, arg00, arg);
10315                     }
10316                 }
10317
10318               /* Optimize tan(x)*cos(x) as sin(x).  */
10319               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10320                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10321                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10322                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10323                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10324                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10325                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10326                                       CALL_EXPR_ARG (arg1, 0), 0))
10327                 {
10328                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10329
10330                   if (sinfn != NULL_TREE)
10331                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10332                 }
10333
10334               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10335               if (fcode1 == BUILT_IN_POW
10336                   || fcode1 == BUILT_IN_POWF
10337                   || fcode1 == BUILT_IN_POWL)
10338                 {
10339                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10340                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10341                   if (TREE_CODE (arg11) == REAL_CST
10342                       && !TREE_OVERFLOW (arg11)
10343                       && operand_equal_p (arg0, arg10, 0))
10344                     {
10345                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10346                       REAL_VALUE_TYPE c;
10347                       tree arg;
10348
10349                       c = TREE_REAL_CST (arg11);
10350                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10351                       arg = build_real (type, c);
10352                       return build_call_expr (powfn, 2, arg0, arg);
10353                     }
10354                 }
10355
10356               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10357               if (fcode0 == BUILT_IN_POW
10358                   || fcode0 == BUILT_IN_POWF
10359                   || fcode0 == BUILT_IN_POWL)
10360                 {
10361                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10362                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10363                   if (TREE_CODE (arg01) == REAL_CST
10364                       && !TREE_OVERFLOW (arg01)
10365                       && operand_equal_p (arg1, arg00, 0))
10366                     {
10367                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10368                       REAL_VALUE_TYPE c;
10369                       tree arg;
10370
10371                       c = TREE_REAL_CST (arg01);
10372                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10373                       arg = build_real (type, c);
10374                       return build_call_expr (powfn, 2, arg1, arg);
10375                     }
10376                 }
10377
10378               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10379               if (! optimize_size
10380                   && operand_equal_p (arg0, arg1, 0))
10381                 {
10382                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10383
10384                   if (powfn)
10385                     {
10386                       tree arg = build_real (type, dconst2);
10387                       return build_call_expr (powfn, 2, arg0, arg);
10388                     }
10389                 }
10390             }
10391         }
10392       goto associate;
10393
10394     case BIT_IOR_EXPR:
10395     bit_ior:
10396       if (integer_all_onesp (arg1))
10397         return omit_one_operand (type, arg1, arg0);
10398       if (integer_zerop (arg1))
10399         return non_lvalue (fold_convert (type, arg0));
10400       if (operand_equal_p (arg0, arg1, 0))
10401         return non_lvalue (fold_convert (type, arg0));
10402
10403       /* ~X | X is -1.  */
10404       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10405           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10406         {
10407           t1 = fold_convert (type, integer_zero_node);
10408           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10409           return omit_one_operand (type, t1, arg1);
10410         }
10411
10412       /* X | ~X is -1.  */
10413       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10414           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10415         {
10416           t1 = fold_convert (type, integer_zero_node);
10417           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10418           return omit_one_operand (type, t1, arg0);
10419         }
10420
10421       /* Canonicalize (X & C1) | C2.  */
10422       if (TREE_CODE (arg0) == BIT_AND_EXPR
10423           && TREE_CODE (arg1) == INTEGER_CST
10424           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10425         {
10426           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10427           int width = TYPE_PRECISION (type), w;
10428           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10429           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10430           hi2 = TREE_INT_CST_HIGH (arg1);
10431           lo2 = TREE_INT_CST_LOW (arg1);
10432
10433           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10434           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10435             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10436
10437           if (width > HOST_BITS_PER_WIDE_INT)
10438             {
10439               mhi = (unsigned HOST_WIDE_INT) -1 
10440                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10441               mlo = -1;
10442             }
10443           else
10444             {
10445               mhi = 0;
10446               mlo = (unsigned HOST_WIDE_INT) -1
10447                     >> (HOST_BITS_PER_WIDE_INT - width);
10448             }
10449
10450           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10451           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10452             return fold_build2 (BIT_IOR_EXPR, type,
10453                                 TREE_OPERAND (arg0, 0), arg1);
10454
10455           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10456              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10457              mode which allows further optimizations.  */
10458           hi1 &= mhi;
10459           lo1 &= mlo;
10460           hi2 &= mhi;
10461           lo2 &= mlo;
10462           hi3 = hi1 & ~hi2;
10463           lo3 = lo1 & ~lo2;
10464           for (w = BITS_PER_UNIT;
10465                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10466                w <<= 1)
10467             {
10468               unsigned HOST_WIDE_INT mask
10469                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10470               if (((lo1 | lo2) & mask) == mask
10471                   && (lo1 & ~mask) == 0 && hi1 == 0)
10472                 {
10473                   hi3 = 0;
10474                   lo3 = mask;
10475                   break;
10476                 }
10477             }
10478           if (hi3 != hi1 || lo3 != lo1)
10479             return fold_build2 (BIT_IOR_EXPR, type,
10480                                 fold_build2 (BIT_AND_EXPR, type,
10481                                              TREE_OPERAND (arg0, 0),
10482                                              build_int_cst_wide (type,
10483                                                                  lo3, hi3)),
10484                                 arg1);
10485         }
10486
10487       /* (X & Y) | Y is (X, Y).  */
10488       if (TREE_CODE (arg0) == BIT_AND_EXPR
10489           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10490         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10491       /* (X & Y) | X is (Y, X).  */
10492       if (TREE_CODE (arg0) == BIT_AND_EXPR
10493           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10494           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10495         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10496       /* X | (X & Y) is (Y, X).  */
10497       if (TREE_CODE (arg1) == BIT_AND_EXPR
10498           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10499           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10500         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10501       /* X | (Y & X) is (Y, X).  */
10502       if (TREE_CODE (arg1) == BIT_AND_EXPR
10503           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10504           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10505         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10506
10507       t1 = distribute_bit_expr (code, type, arg0, arg1);
10508       if (t1 != NULL_TREE)
10509         return t1;
10510
10511       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10512
10513          This results in more efficient code for machines without a NAND
10514          instruction.  Combine will canonicalize to the first form
10515          which will allow use of NAND instructions provided by the
10516          backend if they exist.  */
10517       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10518           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10519         {
10520           return fold_build1 (BIT_NOT_EXPR, type,
10521                               build2 (BIT_AND_EXPR, type,
10522                                       fold_convert (type,
10523                                                     TREE_OPERAND (arg0, 0)),
10524                                       fold_convert (type,
10525                                                     TREE_OPERAND (arg1, 0))));
10526         }
10527
10528       /* See if this can be simplified into a rotate first.  If that
10529          is unsuccessful continue in the association code.  */
10530       goto bit_rotate;
10531
10532     case BIT_XOR_EXPR:
10533       if (integer_zerop (arg1))
10534         return non_lvalue (fold_convert (type, arg0));
10535       if (integer_all_onesp (arg1))
10536         return fold_build1 (BIT_NOT_EXPR, type, op0);
10537       if (operand_equal_p (arg0, arg1, 0))
10538         return omit_one_operand (type, integer_zero_node, arg0);
10539
10540       /* ~X ^ X is -1.  */
10541       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10542           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10543         {
10544           t1 = fold_convert (type, integer_zero_node);
10545           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10546           return omit_one_operand (type, t1, arg1);
10547         }
10548
10549       /* X ^ ~X is -1.  */
10550       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10551           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10552         {
10553           t1 = fold_convert (type, integer_zero_node);
10554           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10555           return omit_one_operand (type, t1, arg0);
10556         }
10557
10558       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10559          with a constant, and the two constants have no bits in common,
10560          we should treat this as a BIT_IOR_EXPR since this may produce more
10561          simplifications.  */
10562       if (TREE_CODE (arg0) == BIT_AND_EXPR
10563           && TREE_CODE (arg1) == BIT_AND_EXPR
10564           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10565           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10566           && integer_zerop (const_binop (BIT_AND_EXPR,
10567                                          TREE_OPERAND (arg0, 1),
10568                                          TREE_OPERAND (arg1, 1), 0)))
10569         {
10570           code = BIT_IOR_EXPR;
10571           goto bit_ior;
10572         }
10573
10574       /* (X | Y) ^ X -> Y & ~ X*/
10575       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10576           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10577         {
10578           tree t2 = TREE_OPERAND (arg0, 1);
10579           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10580                             arg1);
10581           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10582                             fold_convert (type, t1));
10583           return t1;
10584         }
10585
10586       /* (Y | X) ^ X -> Y & ~ X*/
10587       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10588           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10589         {
10590           tree t2 = TREE_OPERAND (arg0, 0);
10591           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10592                             arg1);
10593           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10594                             fold_convert (type, t1));
10595           return t1;
10596         }
10597
10598       /* X ^ (X | Y) -> Y & ~ X*/
10599       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10600           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10601         {
10602           tree t2 = TREE_OPERAND (arg1, 1);
10603           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10604                             arg0);
10605           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10606                             fold_convert (type, t1));
10607           return t1;
10608         }
10609
10610       /* X ^ (Y | X) -> Y & ~ X*/
10611       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10612           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10613         {
10614           tree t2 = TREE_OPERAND (arg1, 0);
10615           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10616                             arg0);
10617           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10618                             fold_convert (type, t1));
10619           return t1;
10620         }
10621         
10622       /* Convert ~X ^ ~Y to X ^ Y.  */
10623       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10624           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10625         return fold_build2 (code, type,
10626                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10627                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10628
10629       /* Convert ~X ^ C to X ^ ~C.  */
10630       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10631           && TREE_CODE (arg1) == INTEGER_CST)
10632         return fold_build2 (code, type,
10633                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10634                             fold_build1 (BIT_NOT_EXPR, type, arg1));
10635
10636       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
10637       if (TREE_CODE (arg0) == BIT_AND_EXPR
10638           && integer_onep (TREE_OPERAND (arg0, 1))
10639           && integer_onep (arg1))
10640         return fold_build2 (EQ_EXPR, type, arg0,
10641                             build_int_cst (TREE_TYPE (arg0), 0));
10642
10643       /* Fold (X & Y) ^ Y as ~X & Y.  */
10644       if (TREE_CODE (arg0) == BIT_AND_EXPR
10645           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10646         {
10647           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10648           return fold_build2 (BIT_AND_EXPR, type, 
10649                               fold_build1 (BIT_NOT_EXPR, type, tem),
10650                               fold_convert (type, arg1));
10651         }
10652       /* Fold (X & Y) ^ X as ~Y & X.  */
10653       if (TREE_CODE (arg0) == BIT_AND_EXPR
10654           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10655           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10656         {
10657           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10658           return fold_build2 (BIT_AND_EXPR, type,
10659                               fold_build1 (BIT_NOT_EXPR, type, tem),
10660                               fold_convert (type, arg1));
10661         }
10662       /* Fold X ^ (X & Y) as X & ~Y.  */
10663       if (TREE_CODE (arg1) == BIT_AND_EXPR
10664           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10665         {
10666           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10667           return fold_build2 (BIT_AND_EXPR, type,
10668                               fold_convert (type, arg0),
10669                               fold_build1 (BIT_NOT_EXPR, type, tem));
10670         }
10671       /* Fold X ^ (Y & X) as ~Y & X.  */
10672       if (TREE_CODE (arg1) == BIT_AND_EXPR
10673           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10674           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10675         {
10676           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10677           return fold_build2 (BIT_AND_EXPR, type,
10678                               fold_build1 (BIT_NOT_EXPR, type, tem),
10679                               fold_convert (type, arg0));
10680         }
10681
10682       /* See if this can be simplified into a rotate first.  If that
10683          is unsuccessful continue in the association code.  */
10684       goto bit_rotate;
10685
10686     case BIT_AND_EXPR:
10687       if (integer_all_onesp (arg1))
10688         return non_lvalue (fold_convert (type, arg0));
10689       if (integer_zerop (arg1))
10690         return omit_one_operand (type, arg1, arg0);
10691       if (operand_equal_p (arg0, arg1, 0))
10692         return non_lvalue (fold_convert (type, arg0));
10693
10694       /* ~X & X is always zero.  */
10695       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10696           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10697         return omit_one_operand (type, integer_zero_node, arg1);
10698
10699       /* X & ~X is always zero.  */
10700       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10701           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10702         return omit_one_operand (type, integer_zero_node, arg0);
10703
10704       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
10705       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10706           && TREE_CODE (arg1) == INTEGER_CST
10707           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10708         {
10709           tree tmp1 = fold_convert (TREE_TYPE (arg0), arg1);
10710           tree tmp2 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10711                                    TREE_OPERAND (arg0, 0), tmp1);
10712           tree tmp3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10713                                    TREE_OPERAND (arg0, 1), tmp1);
10714           return fold_convert (type,
10715                                fold_build2 (BIT_IOR_EXPR, TREE_TYPE (arg0),
10716                                             tmp2, tmp3));
10717         }
10718
10719       /* (X | Y) & Y is (X, Y).  */
10720       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10721           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10722         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10723       /* (X | Y) & X is (Y, X).  */
10724       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10725           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10726           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10727         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10728       /* X & (X | Y) is (Y, X).  */
10729       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10730           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10731           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10732         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10733       /* X & (Y | X) is (Y, X).  */
10734       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10735           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10736           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10737         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10738
10739       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
10740       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10741           && integer_onep (TREE_OPERAND (arg0, 1))
10742           && integer_onep (arg1))
10743         {
10744           tem = TREE_OPERAND (arg0, 0);
10745           return fold_build2 (EQ_EXPR, type,
10746                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10747                                            build_int_cst (TREE_TYPE (tem), 1)),
10748                               build_int_cst (TREE_TYPE (tem), 0));
10749         }
10750       /* Fold ~X & 1 as (X & 1) == 0.  */
10751       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10752           && integer_onep (arg1))
10753         {
10754           tem = TREE_OPERAND (arg0, 0);
10755           return fold_build2 (EQ_EXPR, type,
10756                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10757                                            build_int_cst (TREE_TYPE (tem), 1)),
10758                               build_int_cst (TREE_TYPE (tem), 0));
10759         }
10760
10761       /* Fold (X ^ Y) & Y as ~X & Y.  */
10762       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10763           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10764         {
10765           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10766           return fold_build2 (BIT_AND_EXPR, type, 
10767                               fold_build1 (BIT_NOT_EXPR, type, tem),
10768                               fold_convert (type, arg1));
10769         }
10770       /* Fold (X ^ Y) & X as ~Y & X.  */
10771       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10772           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10773           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10774         {
10775           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10776           return fold_build2 (BIT_AND_EXPR, type,
10777                               fold_build1 (BIT_NOT_EXPR, type, tem),
10778                               fold_convert (type, arg1));
10779         }
10780       /* Fold X & (X ^ Y) as X & ~Y.  */
10781       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10782           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10783         {
10784           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10785           return fold_build2 (BIT_AND_EXPR, type,
10786                               fold_convert (type, arg0),
10787                               fold_build1 (BIT_NOT_EXPR, type, tem));
10788         }
10789       /* Fold X & (Y ^ X) as ~Y & X.  */
10790       if (TREE_CODE (arg1) == BIT_XOR_EXPR
10791           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10792           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10793         {
10794           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10795           return fold_build2 (BIT_AND_EXPR, type,
10796                               fold_build1 (BIT_NOT_EXPR, type, tem),
10797                               fold_convert (type, arg0));
10798         }
10799
10800       t1 = distribute_bit_expr (code, type, arg0, arg1);
10801       if (t1 != NULL_TREE)
10802         return t1;
10803       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
10804       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10805           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10806         {
10807           unsigned int prec
10808             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10809
10810           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10811               && (~TREE_INT_CST_LOW (arg1)
10812                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10813             return fold_convert (type, TREE_OPERAND (arg0, 0));
10814         }
10815
10816       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10817
10818          This results in more efficient code for machines without a NOR
10819          instruction.  Combine will canonicalize to the first form
10820          which will allow use of NOR instructions provided by the
10821          backend if they exist.  */
10822       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10823           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10824         {
10825           return fold_build1 (BIT_NOT_EXPR, type,
10826                               build2 (BIT_IOR_EXPR, type,
10827                                       fold_convert (type,
10828                                                     TREE_OPERAND (arg0, 0)),
10829                                       fold_convert (type,
10830                                                     TREE_OPERAND (arg1, 0))));
10831         }
10832
10833       /* If arg0 is derived from the address of an object or function, we may
10834          be able to fold this expression using the object or function's
10835          alignment.  */
10836       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10837         {
10838           unsigned HOST_WIDE_INT modulus, residue;
10839           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10840
10841           modulus = get_pointer_modulus_and_residue (arg0, &residue);
10842
10843           /* This works because modulus is a power of 2.  If this weren't the
10844              case, we'd have to replace it by its greatest power-of-2
10845              divisor: modulus & -modulus.  */
10846           if (low < modulus)
10847             return build_int_cst (type, residue & low);
10848         }
10849
10850       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
10851               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
10852          if the new mask might be further optimized.  */
10853       if ((TREE_CODE (arg0) == LSHIFT_EXPR
10854            || TREE_CODE (arg0) == RSHIFT_EXPR)
10855           && host_integerp (TREE_OPERAND (arg0, 1), 1)
10856           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
10857           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
10858              < TYPE_PRECISION (TREE_TYPE (arg0))
10859           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
10860           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
10861         {
10862           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
10863           unsigned HOST_WIDE_INT mask
10864             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
10865           unsigned HOST_WIDE_INT newmask, zerobits = 0;
10866           tree shift_type = TREE_TYPE (arg0);
10867
10868           if (TREE_CODE (arg0) == LSHIFT_EXPR)
10869             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
10870           else if (TREE_CODE (arg0) == RSHIFT_EXPR
10871                    && TYPE_PRECISION (TREE_TYPE (arg0))
10872                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
10873             {
10874               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
10875               tree arg00 = TREE_OPERAND (arg0, 0);
10876               /* See if more bits can be proven as zero because of
10877                  zero extension.  */
10878               if (TREE_CODE (arg00) == NOP_EXPR
10879                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
10880                 {
10881                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
10882                   if (TYPE_PRECISION (inner_type)
10883                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
10884                       && TYPE_PRECISION (inner_type) < prec)
10885                     {
10886                       prec = TYPE_PRECISION (inner_type);
10887                       /* See if we can shorten the right shift.  */
10888                       if (shiftc < prec)
10889                         shift_type = inner_type;
10890                     }
10891                 }
10892               zerobits = ~(unsigned HOST_WIDE_INT) 0;
10893               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
10894               zerobits <<= prec - shiftc;
10895               /* For arithmetic shift if sign bit could be set, zerobits
10896                  can contain actually sign bits, so no transformation is
10897                  possible, unless MASK masks them all away.  In that
10898                  case the shift needs to be converted into logical shift.  */
10899               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
10900                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
10901                 {
10902                   if ((mask & zerobits) == 0)
10903                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
10904                   else
10905                     zerobits = 0;
10906                 }
10907             }
10908
10909           /* ((X << 16) & 0xff00) is (X, 0).  */
10910           if ((mask & zerobits) == mask)
10911             return omit_one_operand (type, build_int_cst (type, 0), arg0);
10912
10913           newmask = mask | zerobits;
10914           if (newmask != mask && (newmask & (newmask + 1)) == 0)
10915             {
10916               unsigned int prec;
10917
10918               /* Only do the transformation if NEWMASK is some integer
10919                  mode's mask.  */
10920               for (prec = BITS_PER_UNIT;
10921                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
10922                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
10923                   break;
10924               if (prec < HOST_BITS_PER_WIDE_INT
10925                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
10926                 {
10927                   if (shift_type != TREE_TYPE (arg0))
10928                     {
10929                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
10930                                          fold_convert (shift_type,
10931                                                        TREE_OPERAND (arg0, 0)),
10932                                          TREE_OPERAND (arg0, 1));
10933                       tem = fold_convert (type, tem);
10934                     }
10935                   else
10936                     tem = op0;
10937                   return fold_build2 (BIT_AND_EXPR, type, tem,
10938                                       build_int_cst_type (TREE_TYPE (op1),
10939                                                           newmask));
10940                 }
10941             }
10942         }
10943
10944       goto associate;
10945
10946     case RDIV_EXPR:
10947       /* Don't touch a floating-point divide by zero unless the mode
10948          of the constant can represent infinity.  */
10949       if (TREE_CODE (arg1) == REAL_CST
10950           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10951           && real_zerop (arg1))
10952         return NULL_TREE;
10953
10954       /* Optimize A / A to 1.0 if we don't care about
10955          NaNs or Infinities.  Skip the transformation
10956          for non-real operands.  */
10957       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10958           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10959           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10960           && operand_equal_p (arg0, arg1, 0))
10961         {
10962           tree r = build_real (TREE_TYPE (arg0), dconst1);
10963
10964           return omit_two_operands (type, r, arg0, arg1);
10965         }
10966
10967       /* The complex version of the above A / A optimization.  */
10968       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10969           && operand_equal_p (arg0, arg1, 0))
10970         {
10971           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10972           if (! HONOR_NANS (TYPE_MODE (elem_type))
10973               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10974             {
10975               tree r = build_real (elem_type, dconst1);
10976               /* omit_two_operands will call fold_convert for us.  */
10977               return omit_two_operands (type, r, arg0, arg1);
10978             }
10979         }
10980
10981       /* (-A) / (-B) -> A / B  */
10982       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10983         return fold_build2 (RDIV_EXPR, type,
10984                             TREE_OPERAND (arg0, 0),
10985                             negate_expr (arg1));
10986       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10987         return fold_build2 (RDIV_EXPR, type,
10988                             negate_expr (arg0),
10989                             TREE_OPERAND (arg1, 0));
10990
10991       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
10992       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10993           && real_onep (arg1))
10994         return non_lvalue (fold_convert (type, arg0));
10995
10996       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
10997       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10998           && real_minus_onep (arg1))
10999         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11000
11001       /* If ARG1 is a constant, we can convert this to a multiply by the
11002          reciprocal.  This does not have the same rounding properties,
11003          so only do this if -freciprocal-math.  We can actually
11004          always safely do it if ARG1 is a power of two, but it's hard to
11005          tell if it is or not in a portable manner.  */
11006       if (TREE_CODE (arg1) == REAL_CST)
11007         {
11008           if (flag_reciprocal_math
11009               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11010                                           arg1, 0)))
11011             return fold_build2 (MULT_EXPR, type, arg0, tem);
11012           /* Find the reciprocal if optimizing and the result is exact.  */
11013           if (optimize)
11014             {
11015               REAL_VALUE_TYPE r;
11016               r = TREE_REAL_CST (arg1);
11017               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11018                 {
11019                   tem = build_real (type, r);
11020                   return fold_build2 (MULT_EXPR, type,
11021                                       fold_convert (type, arg0), tem);
11022                 }
11023             }
11024         }
11025       /* Convert A/B/C to A/(B*C).  */ 
11026       if (flag_reciprocal_math
11027           && TREE_CODE (arg0) == RDIV_EXPR)
11028         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11029                             fold_build2 (MULT_EXPR, type,
11030                                          TREE_OPERAND (arg0, 1), arg1));
11031
11032       /* Convert A/(B/C) to (A/B)*C.  */
11033       if (flag_reciprocal_math
11034           && TREE_CODE (arg1) == RDIV_EXPR)
11035         return fold_build2 (MULT_EXPR, type,
11036                             fold_build2 (RDIV_EXPR, type, arg0,
11037                                          TREE_OPERAND (arg1, 0)),
11038                             TREE_OPERAND (arg1, 1));
11039
11040       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11041       if (flag_reciprocal_math
11042           && TREE_CODE (arg1) == MULT_EXPR
11043           && TREE_CODE (arg0) == REAL_CST
11044           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11045         {
11046           tree tem = const_binop (RDIV_EXPR, arg0,
11047                                   TREE_OPERAND (arg1, 1), 0);
11048           if (tem)
11049             return fold_build2 (RDIV_EXPR, type, tem,
11050                                 TREE_OPERAND (arg1, 0));
11051         }
11052
11053       if (flag_unsafe_math_optimizations)
11054         {
11055           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11056           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11057
11058           /* Optimize sin(x)/cos(x) as tan(x).  */
11059           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11060                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11061                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11062               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11063                                   CALL_EXPR_ARG (arg1, 0), 0))
11064             {
11065               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11066
11067               if (tanfn != NULL_TREE)
11068                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11069             }
11070
11071           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11072           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11073                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11074                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11075               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11076                                   CALL_EXPR_ARG (arg1, 0), 0))
11077             {
11078               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11079
11080               if (tanfn != NULL_TREE)
11081                 {
11082                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11083                   return fold_build2 (RDIV_EXPR, type,
11084                                       build_real (type, dconst1), tmp);
11085                 }
11086             }
11087
11088           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11089              NaNs or Infinities.  */
11090           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11091                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11092                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11093             {
11094               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11095               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11096
11097               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11098                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11099                   && operand_equal_p (arg00, arg01, 0))
11100                 {
11101                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11102
11103                   if (cosfn != NULL_TREE)
11104                     return build_call_expr (cosfn, 1, arg00);
11105                 }
11106             }
11107
11108           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11109              NaNs or Infinities.  */
11110           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11111                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11112                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11113             {
11114               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11115               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11116
11117               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11118                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11119                   && operand_equal_p (arg00, arg01, 0))
11120                 {
11121                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11122
11123                   if (cosfn != NULL_TREE)
11124                     {
11125                       tree tmp = build_call_expr (cosfn, 1, arg00);
11126                       return fold_build2 (RDIV_EXPR, type,
11127                                           build_real (type, dconst1),
11128                                           tmp);
11129                     }
11130                 }
11131             }
11132
11133           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11134           if (fcode0 == BUILT_IN_POW
11135               || fcode0 == BUILT_IN_POWF
11136               || fcode0 == BUILT_IN_POWL)
11137             {
11138               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11139               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11140               if (TREE_CODE (arg01) == REAL_CST
11141                   && !TREE_OVERFLOW (arg01)
11142                   && operand_equal_p (arg1, arg00, 0))
11143                 {
11144                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11145                   REAL_VALUE_TYPE c;
11146                   tree arg;
11147
11148                   c = TREE_REAL_CST (arg01);
11149                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11150                   arg = build_real (type, c);
11151                   return build_call_expr (powfn, 2, arg1, arg);
11152                 }
11153             }
11154
11155           /* Optimize a/root(b/c) into a*root(c/b).  */
11156           if (BUILTIN_ROOT_P (fcode1))
11157             {
11158               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11159
11160               if (TREE_CODE (rootarg) == RDIV_EXPR)
11161                 {
11162                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11163                   tree b = TREE_OPERAND (rootarg, 0);
11164                   tree c = TREE_OPERAND (rootarg, 1);
11165
11166                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11167
11168                   tmp = build_call_expr (rootfn, 1, tmp);
11169                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11170                 }
11171             }
11172
11173           /* Optimize x/expN(y) into x*expN(-y).  */
11174           if (BUILTIN_EXPONENT_P (fcode1))
11175             {
11176               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11177               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11178               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11179               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11180             }
11181
11182           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11183           if (fcode1 == BUILT_IN_POW
11184               || fcode1 == BUILT_IN_POWF
11185               || fcode1 == BUILT_IN_POWL)
11186             {
11187               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11188               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11189               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11190               tree neg11 = fold_convert (type, negate_expr (arg11));
11191               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11192               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11193             }
11194         }
11195       return NULL_TREE;
11196
11197     case TRUNC_DIV_EXPR:
11198     case FLOOR_DIV_EXPR:
11199       /* Simplify A / (B << N) where A and B are positive and B is
11200          a power of 2, to A >> (N + log2(B)).  */
11201       strict_overflow_p = false;
11202       if (TREE_CODE (arg1) == LSHIFT_EXPR
11203           && (TYPE_UNSIGNED (type)
11204               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11205         {
11206           tree sval = TREE_OPERAND (arg1, 0);
11207           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11208             {
11209               tree sh_cnt = TREE_OPERAND (arg1, 1);
11210               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11211
11212               if (strict_overflow_p)
11213                 fold_overflow_warning (("assuming signed overflow does not "
11214                                         "occur when simplifying A / (B << N)"),
11215                                        WARN_STRICT_OVERFLOW_MISC);
11216
11217               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11218                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11219               return fold_build2 (RSHIFT_EXPR, type,
11220                                   fold_convert (type, arg0), sh_cnt);
11221             }
11222         }
11223
11224       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11225          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11226       if (INTEGRAL_TYPE_P (type)
11227           && TYPE_UNSIGNED (type)
11228           && code == FLOOR_DIV_EXPR)
11229         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11230
11231       /* Fall thru */
11232
11233     case ROUND_DIV_EXPR:
11234     case CEIL_DIV_EXPR:
11235     case EXACT_DIV_EXPR:
11236       if (integer_onep (arg1))
11237         return non_lvalue (fold_convert (type, arg0));
11238       if (integer_zerop (arg1))
11239         return NULL_TREE;
11240       /* X / -1 is -X.  */
11241       if (!TYPE_UNSIGNED (type)
11242           && TREE_CODE (arg1) == INTEGER_CST
11243           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11244           && TREE_INT_CST_HIGH (arg1) == -1)
11245         return fold_convert (type, negate_expr (arg0));
11246
11247       /* Convert -A / -B to A / B when the type is signed and overflow is
11248          undefined.  */
11249       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11250           && TREE_CODE (arg0) == NEGATE_EXPR
11251           && negate_expr_p (arg1))
11252         {
11253           if (INTEGRAL_TYPE_P (type))
11254             fold_overflow_warning (("assuming signed overflow does not occur "
11255                                     "when distributing negation across "
11256                                     "division"),
11257                                    WARN_STRICT_OVERFLOW_MISC);
11258           return fold_build2 (code, type,
11259                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11260                               negate_expr (arg1));
11261         }
11262       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11263           && TREE_CODE (arg1) == NEGATE_EXPR
11264           && negate_expr_p (arg0))
11265         {
11266           if (INTEGRAL_TYPE_P (type))
11267             fold_overflow_warning (("assuming signed overflow does not occur "
11268                                     "when distributing negation across "
11269                                     "division"),
11270                                    WARN_STRICT_OVERFLOW_MISC);
11271           return fold_build2 (code, type, negate_expr (arg0),
11272                               TREE_OPERAND (arg1, 0));
11273         }
11274
11275       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11276          operation, EXACT_DIV_EXPR.
11277
11278          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11279          At one time others generated faster code, it's not clear if they do
11280          after the last round to changes to the DIV code in expmed.c.  */
11281       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11282           && multiple_of_p (type, arg0, arg1))
11283         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11284
11285       strict_overflow_p = false;
11286       if (TREE_CODE (arg1) == INTEGER_CST
11287           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11288                                          &strict_overflow_p)))
11289         {
11290           if (strict_overflow_p)
11291             fold_overflow_warning (("assuming signed overflow does not occur "
11292                                     "when simplifying division"),
11293                                    WARN_STRICT_OVERFLOW_MISC);
11294           return fold_convert (type, tem);
11295         }
11296
11297       return NULL_TREE;
11298
11299     case CEIL_MOD_EXPR:
11300     case FLOOR_MOD_EXPR:
11301     case ROUND_MOD_EXPR:
11302     case TRUNC_MOD_EXPR:
11303       /* X % 1 is always zero, but be sure to preserve any side
11304          effects in X.  */
11305       if (integer_onep (arg1))
11306         return omit_one_operand (type, integer_zero_node, arg0);
11307
11308       /* X % 0, return X % 0 unchanged so that we can get the
11309          proper warnings and errors.  */
11310       if (integer_zerop (arg1))
11311         return NULL_TREE;
11312
11313       /* 0 % X is always zero, but be sure to preserve any side
11314          effects in X.  Place this after checking for X == 0.  */
11315       if (integer_zerop (arg0))
11316         return omit_one_operand (type, integer_zero_node, arg1);
11317
11318       /* X % -1 is zero.  */
11319       if (!TYPE_UNSIGNED (type)
11320           && TREE_CODE (arg1) == INTEGER_CST
11321           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11322           && TREE_INT_CST_HIGH (arg1) == -1)
11323         return omit_one_operand (type, integer_zero_node, arg0);
11324
11325       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11326          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11327       strict_overflow_p = false;
11328       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11329           && (TYPE_UNSIGNED (type)
11330               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11331         {
11332           tree c = arg1;
11333           /* Also optimize A % (C << N)  where C is a power of 2,
11334              to A & ((C << N) - 1).  */
11335           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11336             c = TREE_OPERAND (arg1, 0);
11337
11338           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11339             {
11340               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11341                                        build_int_cst (TREE_TYPE (arg1), 1));
11342               if (strict_overflow_p)
11343                 fold_overflow_warning (("assuming signed overflow does not "
11344                                         "occur when simplifying "
11345                                         "X % (power of two)"),
11346                                        WARN_STRICT_OVERFLOW_MISC);
11347               return fold_build2 (BIT_AND_EXPR, type,
11348                                   fold_convert (type, arg0),
11349                                   fold_convert (type, mask));
11350             }
11351         }
11352
11353       /* X % -C is the same as X % C.  */
11354       if (code == TRUNC_MOD_EXPR
11355           && !TYPE_UNSIGNED (type)
11356           && TREE_CODE (arg1) == INTEGER_CST
11357           && !TREE_OVERFLOW (arg1)
11358           && TREE_INT_CST_HIGH (arg1) < 0
11359           && !TYPE_OVERFLOW_TRAPS (type)
11360           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11361           && !sign_bit_p (arg1, arg1))
11362         return fold_build2 (code, type, fold_convert (type, arg0),
11363                             fold_convert (type, negate_expr (arg1)));
11364
11365       /* X % -Y is the same as X % Y.  */
11366       if (code == TRUNC_MOD_EXPR
11367           && !TYPE_UNSIGNED (type)
11368           && TREE_CODE (arg1) == NEGATE_EXPR
11369           && !TYPE_OVERFLOW_TRAPS (type))
11370         return fold_build2 (code, type, fold_convert (type, arg0),
11371                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11372
11373       if (TREE_CODE (arg1) == INTEGER_CST
11374           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11375                                          &strict_overflow_p)))
11376         {
11377           if (strict_overflow_p)
11378             fold_overflow_warning (("assuming signed overflow does not occur "
11379                                     "when simplifying modulos"),
11380                                    WARN_STRICT_OVERFLOW_MISC);
11381           return fold_convert (type, tem);
11382         }
11383
11384       return NULL_TREE;
11385
11386     case LROTATE_EXPR:
11387     case RROTATE_EXPR:
11388       if (integer_all_onesp (arg0))
11389         return omit_one_operand (type, arg0, arg1);
11390       goto shift;
11391
11392     case RSHIFT_EXPR:
11393       /* Optimize -1 >> x for arithmetic right shifts.  */
11394       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11395         return omit_one_operand (type, arg0, arg1);
11396       /* ... fall through ...  */
11397
11398     case LSHIFT_EXPR:
11399     shift:
11400       if (integer_zerop (arg1))
11401         return non_lvalue (fold_convert (type, arg0));
11402       if (integer_zerop (arg0))
11403         return omit_one_operand (type, arg0, arg1);
11404
11405       /* Since negative shift count is not well-defined,
11406          don't try to compute it in the compiler.  */
11407       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11408         return NULL_TREE;
11409
11410       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11411       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11412           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11413           && host_integerp (TREE_OPERAND (arg0, 1), false)
11414           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11415         {
11416           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11417                                + TREE_INT_CST_LOW (arg1));
11418
11419           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11420              being well defined.  */
11421           if (low >= TYPE_PRECISION (type))
11422             {
11423               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11424                 low = low % TYPE_PRECISION (type);
11425               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11426                 return build_int_cst (type, 0);
11427               else
11428                 low = TYPE_PRECISION (type) - 1;
11429             }
11430
11431           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11432                               build_int_cst (type, low));
11433         }
11434
11435       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11436          into x & ((unsigned)-1 >> c) for unsigned types.  */
11437       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11438            || (TYPE_UNSIGNED (type)
11439                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11440           && host_integerp (arg1, false)
11441           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11442           && host_integerp (TREE_OPERAND (arg0, 1), false)
11443           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11444         {
11445           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11446           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11447           tree lshift;
11448           tree arg00;
11449
11450           if (low0 == low1)
11451             {
11452               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11453
11454               lshift = build_int_cst (type, -1);
11455               lshift = int_const_binop (code, lshift, arg1, 0);
11456
11457               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11458             }
11459         }
11460
11461       /* Rewrite an LROTATE_EXPR by a constant into an
11462          RROTATE_EXPR by a new constant.  */
11463       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11464         {
11465           tree tem = build_int_cst (TREE_TYPE (arg1),
11466                                     TYPE_PRECISION (type));
11467           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11468           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11469         }
11470
11471       /* If we have a rotate of a bit operation with the rotate count and
11472          the second operand of the bit operation both constant,
11473          permute the two operations.  */
11474       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11475           && (TREE_CODE (arg0) == BIT_AND_EXPR
11476               || TREE_CODE (arg0) == BIT_IOR_EXPR
11477               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11478           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11479         return fold_build2 (TREE_CODE (arg0), type,
11480                             fold_build2 (code, type,
11481                                          TREE_OPERAND (arg0, 0), arg1),
11482                             fold_build2 (code, type,
11483                                          TREE_OPERAND (arg0, 1), arg1));
11484
11485       /* Two consecutive rotates adding up to the precision of the
11486          type can be ignored.  */
11487       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11488           && TREE_CODE (arg0) == RROTATE_EXPR
11489           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11490           && TREE_INT_CST_HIGH (arg1) == 0
11491           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11492           && ((TREE_INT_CST_LOW (arg1)
11493                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11494               == (unsigned int) TYPE_PRECISION (type)))
11495         return TREE_OPERAND (arg0, 0);
11496
11497       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11498               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11499          if the latter can be further optimized.  */
11500       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11501           && TREE_CODE (arg0) == BIT_AND_EXPR
11502           && TREE_CODE (arg1) == INTEGER_CST
11503           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11504         {
11505           tree mask = fold_build2 (code, type,
11506                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11507                                    arg1);
11508           tree shift = fold_build2 (code, type,
11509                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11510                                     arg1);
11511           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11512           if (tem)
11513             return tem;
11514         }
11515
11516       return NULL_TREE;
11517
11518     case MIN_EXPR:
11519       if (operand_equal_p (arg0, arg1, 0))
11520         return omit_one_operand (type, arg0, arg1);
11521       if (INTEGRAL_TYPE_P (type)
11522           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11523         return omit_one_operand (type, arg1, arg0);
11524       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11525       if (tem)
11526         return tem;
11527       goto associate;
11528
11529     case MAX_EXPR:
11530       if (operand_equal_p (arg0, arg1, 0))
11531         return omit_one_operand (type, arg0, arg1);
11532       if (INTEGRAL_TYPE_P (type)
11533           && TYPE_MAX_VALUE (type)
11534           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11535         return omit_one_operand (type, arg1, arg0);
11536       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11537       if (tem)
11538         return tem;
11539       goto associate;
11540
11541     case TRUTH_ANDIF_EXPR:
11542       /* Note that the operands of this must be ints
11543          and their values must be 0 or 1.
11544          ("true" is a fixed value perhaps depending on the language.)  */
11545       /* If first arg is constant zero, return it.  */
11546       if (integer_zerop (arg0))
11547         return fold_convert (type, arg0);
11548     case TRUTH_AND_EXPR:
11549       /* If either arg is constant true, drop it.  */
11550       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11551         return non_lvalue (fold_convert (type, arg1));
11552       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11553           /* Preserve sequence points.  */
11554           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11555         return non_lvalue (fold_convert (type, arg0));
11556       /* If second arg is constant zero, result is zero, but first arg
11557          must be evaluated.  */
11558       if (integer_zerop (arg1))
11559         return omit_one_operand (type, arg1, arg0);
11560       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11561          case will be handled here.  */
11562       if (integer_zerop (arg0))
11563         return omit_one_operand (type, arg0, arg1);
11564
11565       /* !X && X is always false.  */
11566       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11567           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11568         return omit_one_operand (type, integer_zero_node, arg1);
11569       /* X && !X is always false.  */
11570       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11571           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11572         return omit_one_operand (type, integer_zero_node, arg0);
11573
11574       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11575          means A >= Y && A != MAX, but in this case we know that
11576          A < X <= MAX.  */
11577
11578       if (!TREE_SIDE_EFFECTS (arg0)
11579           && !TREE_SIDE_EFFECTS (arg1))
11580         {
11581           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11582           if (tem && !operand_equal_p (tem, arg0, 0))
11583             return fold_build2 (code, type, tem, arg1);
11584
11585           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11586           if (tem && !operand_equal_p (tem, arg1, 0))
11587             return fold_build2 (code, type, arg0, tem);
11588         }
11589
11590     truth_andor:
11591       /* We only do these simplifications if we are optimizing.  */
11592       if (!optimize)
11593         return NULL_TREE;
11594
11595       /* Check for things like (A || B) && (A || C).  We can convert this
11596          to A || (B && C).  Note that either operator can be any of the four
11597          truth and/or operations and the transformation will still be
11598          valid.   Also note that we only care about order for the
11599          ANDIF and ORIF operators.  If B contains side effects, this
11600          might change the truth-value of A.  */
11601       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11602           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11603               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11604               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11605               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11606           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11607         {
11608           tree a00 = TREE_OPERAND (arg0, 0);
11609           tree a01 = TREE_OPERAND (arg0, 1);
11610           tree a10 = TREE_OPERAND (arg1, 0);
11611           tree a11 = TREE_OPERAND (arg1, 1);
11612           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11613                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11614                              && (code == TRUTH_AND_EXPR
11615                                  || code == TRUTH_OR_EXPR));
11616
11617           if (operand_equal_p (a00, a10, 0))
11618             return fold_build2 (TREE_CODE (arg0), type, a00,
11619                                 fold_build2 (code, type, a01, a11));
11620           else if (commutative && operand_equal_p (a00, a11, 0))
11621             return fold_build2 (TREE_CODE (arg0), type, a00,
11622                                 fold_build2 (code, type, a01, a10));
11623           else if (commutative && operand_equal_p (a01, a10, 0))
11624             return fold_build2 (TREE_CODE (arg0), type, a01,
11625                                 fold_build2 (code, type, a00, a11));
11626
11627           /* This case if tricky because we must either have commutative
11628              operators or else A10 must not have side-effects.  */
11629
11630           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11631                    && operand_equal_p (a01, a11, 0))
11632             return fold_build2 (TREE_CODE (arg0), type,
11633                                 fold_build2 (code, type, a00, a10),
11634                                 a01);
11635         }
11636
11637       /* See if we can build a range comparison.  */
11638       if (0 != (tem = fold_range_test (code, type, op0, op1)))
11639         return tem;
11640
11641       /* Check for the possibility of merging component references.  If our
11642          lhs is another similar operation, try to merge its rhs with our
11643          rhs.  Then try to merge our lhs and rhs.  */
11644       if (TREE_CODE (arg0) == code
11645           && 0 != (tem = fold_truthop (code, type,
11646                                        TREE_OPERAND (arg0, 1), arg1)))
11647         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11648
11649       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11650         return tem;
11651
11652       return NULL_TREE;
11653
11654     case TRUTH_ORIF_EXPR:
11655       /* Note that the operands of this must be ints
11656          and their values must be 0 or true.
11657          ("true" is a fixed value perhaps depending on the language.)  */
11658       /* If first arg is constant true, return it.  */
11659       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11660         return fold_convert (type, arg0);
11661     case TRUTH_OR_EXPR:
11662       /* If either arg is constant zero, drop it.  */
11663       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11664         return non_lvalue (fold_convert (type, arg1));
11665       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11666           /* Preserve sequence points.  */
11667           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11668         return non_lvalue (fold_convert (type, arg0));
11669       /* If second arg is constant true, result is true, but we must
11670          evaluate first arg.  */
11671       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11672         return omit_one_operand (type, arg1, arg0);
11673       /* Likewise for first arg, but note this only occurs here for
11674          TRUTH_OR_EXPR.  */
11675       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11676         return omit_one_operand (type, arg0, arg1);
11677
11678       /* !X || X is always true.  */
11679       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11680           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11681         return omit_one_operand (type, integer_one_node, arg1);
11682       /* X || !X is always true.  */
11683       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11684           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11685         return omit_one_operand (type, integer_one_node, arg0);
11686
11687       goto truth_andor;
11688
11689     case TRUTH_XOR_EXPR:
11690       /* If the second arg is constant zero, drop it.  */
11691       if (integer_zerop (arg1))
11692         return non_lvalue (fold_convert (type, arg0));
11693       /* If the second arg is constant true, this is a logical inversion.  */
11694       if (integer_onep (arg1))
11695         {
11696           /* Only call invert_truthvalue if operand is a truth value.  */
11697           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11698             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11699           else
11700             tem = invert_truthvalue (arg0);
11701           return non_lvalue (fold_convert (type, tem));
11702         }
11703       /* Identical arguments cancel to zero.  */
11704       if (operand_equal_p (arg0, arg1, 0))
11705         return omit_one_operand (type, integer_zero_node, arg0);
11706
11707       /* !X ^ X is always true.  */
11708       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11709           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11710         return omit_one_operand (type, integer_one_node, arg1);
11711
11712       /* X ^ !X is always true.  */
11713       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11714           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11715         return omit_one_operand (type, integer_one_node, arg0);
11716
11717       return NULL_TREE;
11718
11719     case EQ_EXPR:
11720     case NE_EXPR:
11721       tem = fold_comparison (code, type, op0, op1);
11722       if (tem != NULL_TREE)
11723         return tem;
11724
11725       /* bool_var != 0 becomes bool_var. */
11726       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11727           && code == NE_EXPR)
11728         return non_lvalue (fold_convert (type, arg0));
11729
11730       /* bool_var == 1 becomes bool_var. */
11731       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11732           && code == EQ_EXPR)
11733         return non_lvalue (fold_convert (type, arg0));
11734
11735       /* bool_var != 1 becomes !bool_var. */
11736       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11737           && code == NE_EXPR)
11738         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11739
11740       /* bool_var == 0 becomes !bool_var. */
11741       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11742           && code == EQ_EXPR)
11743         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11744
11745       /* If this is an equality comparison of the address of two non-weak,
11746          unaliased symbols neither of which are extern (since we do not
11747          have access to attributes for externs), then we know the result.  */
11748       if (TREE_CODE (arg0) == ADDR_EXPR
11749           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11750           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11751           && ! lookup_attribute ("alias",
11752                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11753           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11754           && TREE_CODE (arg1) == ADDR_EXPR
11755           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11756           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11757           && ! lookup_attribute ("alias",
11758                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11759           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11760         {
11761           /* We know that we're looking at the address of two
11762              non-weak, unaliased, static _DECL nodes.
11763
11764              It is both wasteful and incorrect to call operand_equal_p
11765              to compare the two ADDR_EXPR nodes.  It is wasteful in that
11766              all we need to do is test pointer equality for the arguments
11767              to the two ADDR_EXPR nodes.  It is incorrect to use
11768              operand_equal_p as that function is NOT equivalent to a
11769              C equality test.  It can in fact return false for two
11770              objects which would test as equal using the C equality
11771              operator.  */
11772           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11773           return constant_boolean_node (equal
11774                                         ? code == EQ_EXPR : code != EQ_EXPR,
11775                                         type);
11776         }
11777
11778       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11779          a MINUS_EXPR of a constant, we can convert it into a comparison with
11780          a revised constant as long as no overflow occurs.  */
11781       if (TREE_CODE (arg1) == INTEGER_CST
11782           && (TREE_CODE (arg0) == PLUS_EXPR
11783               || TREE_CODE (arg0) == MINUS_EXPR)
11784           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11785           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11786                                       ? MINUS_EXPR : PLUS_EXPR,
11787                                       fold_convert (TREE_TYPE (arg0), arg1),
11788                                       TREE_OPERAND (arg0, 1), 0))
11789           && !TREE_OVERFLOW (tem))
11790         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11791
11792       /* Similarly for a NEGATE_EXPR.  */
11793       if (TREE_CODE (arg0) == NEGATE_EXPR
11794           && TREE_CODE (arg1) == INTEGER_CST
11795           && 0 != (tem = negate_expr (arg1))
11796           && TREE_CODE (tem) == INTEGER_CST
11797           && !TREE_OVERFLOW (tem))
11798         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11799
11800       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
11801       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11802           && TREE_CODE (arg1) == INTEGER_CST
11803           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11804         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11805                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11806                                          fold_convert (TREE_TYPE (arg0), arg1),
11807                                          TREE_OPERAND (arg0, 1)));
11808
11809       /* Transform comparisons of the form X +- C CMP X.  */
11810       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11811           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11812           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11813           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11814               || POINTER_TYPE_P (TREE_TYPE (arg0))))
11815         {
11816           tree cst = TREE_OPERAND (arg0, 1);
11817
11818           if (code == EQ_EXPR
11819               && !integer_zerop (cst))
11820             return omit_two_operands (type, boolean_false_node,
11821                                       TREE_OPERAND (arg0, 0), arg1);
11822           else
11823             return omit_two_operands (type, boolean_true_node,
11824                                       TREE_OPERAND (arg0, 0), arg1);
11825         }
11826
11827       /* If we have X - Y == 0, we can convert that to X == Y and similarly
11828          for !=.  Don't do this for ordered comparisons due to overflow.  */
11829       if (TREE_CODE (arg0) == MINUS_EXPR
11830           && integer_zerop (arg1))
11831         return fold_build2 (code, type,
11832                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11833
11834       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
11835       if (TREE_CODE (arg0) == ABS_EXPR
11836           && (integer_zerop (arg1) || real_zerop (arg1)))
11837         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11838
11839       /* If this is an EQ or NE comparison with zero and ARG0 is
11840          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
11841          two operations, but the latter can be done in one less insn
11842          on machines that have only two-operand insns or on which a
11843          constant cannot be the first operand.  */
11844       if (TREE_CODE (arg0) == BIT_AND_EXPR
11845           && integer_zerop (arg1))
11846         {
11847           tree arg00 = TREE_OPERAND (arg0, 0);
11848           tree arg01 = TREE_OPERAND (arg0, 1);
11849           if (TREE_CODE (arg00) == LSHIFT_EXPR
11850               && integer_onep (TREE_OPERAND (arg00, 0)))
11851             {
11852               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11853                                       arg01, TREE_OPERAND (arg00, 1));
11854               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11855                                  build_int_cst (TREE_TYPE (arg0), 1));
11856               return fold_build2 (code, type,
11857                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
11858             }
11859           else if (TREE_CODE (arg01) == LSHIFT_EXPR
11860                    && integer_onep (TREE_OPERAND (arg01, 0)))
11861             {
11862               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11863                                       arg00, TREE_OPERAND (arg01, 1));
11864               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11865                                  build_int_cst (TREE_TYPE (arg0), 1));
11866               return fold_build2 (code, type,
11867                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
11868             }
11869         }
11870
11871       /* If this is an NE or EQ comparison of zero against the result of a
11872          signed MOD operation whose second operand is a power of 2, make
11873          the MOD operation unsigned since it is simpler and equivalent.  */
11874       if (integer_zerop (arg1)
11875           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11876           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11877               || TREE_CODE (arg0) == CEIL_MOD_EXPR
11878               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11879               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11880           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11881         {
11882           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11883           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11884                                      fold_convert (newtype,
11885                                                    TREE_OPERAND (arg0, 0)),
11886                                      fold_convert (newtype,
11887                                                    TREE_OPERAND (arg0, 1)));
11888
11889           return fold_build2 (code, type, newmod,
11890                               fold_convert (newtype, arg1));
11891         }
11892
11893       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11894          C1 is a valid shift constant, and C2 is a power of two, i.e.
11895          a single bit.  */
11896       if (TREE_CODE (arg0) == BIT_AND_EXPR
11897           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11898           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11899              == INTEGER_CST
11900           && integer_pow2p (TREE_OPERAND (arg0, 1))
11901           && integer_zerop (arg1))
11902         {
11903           tree itype = TREE_TYPE (arg0);
11904           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11905           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11906
11907           /* Check for a valid shift count.  */
11908           if (TREE_INT_CST_HIGH (arg001) == 0
11909               && TREE_INT_CST_LOW (arg001) < prec)
11910             {
11911               tree arg01 = TREE_OPERAND (arg0, 1);
11912               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11913               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11914               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11915                  can be rewritten as (X & (C2 << C1)) != 0.  */
11916               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11917                 {
11918                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11919                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11920                   return fold_build2 (code, type, tem, arg1);
11921                 }
11922               /* Otherwise, for signed (arithmetic) shifts,
11923                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11924                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
11925               else if (!TYPE_UNSIGNED (itype))
11926                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11927                                     arg000, build_int_cst (itype, 0));
11928               /* Otherwise, of unsigned (logical) shifts,
11929                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11930                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
11931               else
11932                 return omit_one_operand (type,
11933                                          code == EQ_EXPR ? integer_one_node
11934                                                          : integer_zero_node,
11935                                          arg000);
11936             }
11937         }
11938
11939       /* If this is an NE comparison of zero with an AND of one, remove the
11940          comparison since the AND will give the correct value.  */
11941       if (code == NE_EXPR
11942           && integer_zerop (arg1)
11943           && TREE_CODE (arg0) == BIT_AND_EXPR
11944           && integer_onep (TREE_OPERAND (arg0, 1)))
11945         return fold_convert (type, arg0);
11946
11947       /* If we have (A & C) == C where C is a power of 2, convert this into
11948          (A & C) != 0.  Similarly for NE_EXPR.  */
11949       if (TREE_CODE (arg0) == BIT_AND_EXPR
11950           && integer_pow2p (TREE_OPERAND (arg0, 1))
11951           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11952         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11953                             arg0, fold_convert (TREE_TYPE (arg0),
11954                                                 integer_zero_node));
11955
11956       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11957          bit, then fold the expression into A < 0 or A >= 0.  */
11958       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11959       if (tem)
11960         return tem;
11961
11962       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11963          Similarly for NE_EXPR.  */
11964       if (TREE_CODE (arg0) == BIT_AND_EXPR
11965           && TREE_CODE (arg1) == INTEGER_CST
11966           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11967         {
11968           tree notc = fold_build1 (BIT_NOT_EXPR,
11969                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
11970                                    TREE_OPERAND (arg0, 1));
11971           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11972                                        arg1, notc);
11973           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11974           if (integer_nonzerop (dandnotc))
11975             return omit_one_operand (type, rslt, arg0);
11976         }
11977
11978       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
11979          Similarly for NE_EXPR.  */
11980       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11981           && TREE_CODE (arg1) == INTEGER_CST
11982           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11983         {
11984           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
11985           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11986                                        TREE_OPERAND (arg0, 1), notd);
11987           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11988           if (integer_nonzerop (candnotd))
11989             return omit_one_operand (type, rslt, arg0);
11990         }
11991
11992       /* Optimize comparisons of strlen vs zero to a compare of the
11993          first character of the string vs zero.  To wit,
11994                 strlen(ptr) == 0   =>  *ptr == 0
11995                 strlen(ptr) != 0   =>  *ptr != 0
11996          Other cases should reduce to one of these two (or a constant)
11997          due to the return value of strlen being unsigned.  */
11998       if (TREE_CODE (arg0) == CALL_EXPR
11999           && integer_zerop (arg1))
12000         {
12001           tree fndecl = get_callee_fndecl (arg0);
12002
12003           if (fndecl
12004               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12005               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12006               && call_expr_nargs (arg0) == 1
12007               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12008             {
12009               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12010               return fold_build2 (code, type, iref,
12011                                   build_int_cst (TREE_TYPE (iref), 0));
12012             }
12013         }
12014
12015       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12016          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12017       if (TREE_CODE (arg0) == RSHIFT_EXPR
12018           && integer_zerop (arg1)
12019           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12020         {
12021           tree arg00 = TREE_OPERAND (arg0, 0);
12022           tree arg01 = TREE_OPERAND (arg0, 1);
12023           tree itype = TREE_TYPE (arg00);
12024           if (TREE_INT_CST_HIGH (arg01) == 0
12025               && TREE_INT_CST_LOW (arg01)
12026                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12027             {
12028               if (TYPE_UNSIGNED (itype))
12029                 {
12030                   itype = signed_type_for (itype);
12031                   arg00 = fold_convert (itype, arg00);
12032                 }
12033               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12034                                   type, arg00, build_int_cst (itype, 0));
12035             }
12036         }
12037
12038       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12039       if (integer_zerop (arg1)
12040           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12041         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12042                             TREE_OPERAND (arg0, 1));
12043
12044       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12045       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12046           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12047         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12048                             build_int_cst (TREE_TYPE (arg1), 0));
12049       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12050       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12051           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12052           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12053         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12054                             build_int_cst (TREE_TYPE (arg1), 0));
12055
12056       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12057       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12058           && TREE_CODE (arg1) == INTEGER_CST
12059           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12060         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12061                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12062                                          TREE_OPERAND (arg0, 1), arg1));
12063
12064       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12065          (X & C) == 0 when C is a single bit.  */
12066       if (TREE_CODE (arg0) == BIT_AND_EXPR
12067           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12068           && integer_zerop (arg1)
12069           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12070         {
12071           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12072                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12073                              TREE_OPERAND (arg0, 1));
12074           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12075                               type, tem, arg1);
12076         }
12077
12078       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12079          constant C is a power of two, i.e. a single bit.  */
12080       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12081           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12082           && integer_zerop (arg1)
12083           && integer_pow2p (TREE_OPERAND (arg0, 1))
12084           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12085                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12086         {
12087           tree arg00 = TREE_OPERAND (arg0, 0);
12088           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12089                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12090         }
12091
12092       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12093          when is C is a power of two, i.e. a single bit.  */
12094       if (TREE_CODE (arg0) == BIT_AND_EXPR
12095           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12096           && integer_zerop (arg1)
12097           && integer_pow2p (TREE_OPERAND (arg0, 1))
12098           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12099                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12100         {
12101           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12102           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12103                              arg000, TREE_OPERAND (arg0, 1));
12104           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12105                               tem, build_int_cst (TREE_TYPE (tem), 0));
12106         }
12107
12108       if (integer_zerop (arg1)
12109           && tree_expr_nonzero_p (arg0))
12110         {
12111           tree res = constant_boolean_node (code==NE_EXPR, type);
12112           return omit_one_operand (type, res, arg0);
12113         }
12114
12115       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12116       if (TREE_CODE (arg0) == NEGATE_EXPR
12117           && TREE_CODE (arg1) == NEGATE_EXPR)
12118         return fold_build2 (code, type,
12119                             TREE_OPERAND (arg0, 0),
12120                             TREE_OPERAND (arg1, 0));
12121
12122       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12123       if (TREE_CODE (arg0) == BIT_AND_EXPR
12124           && TREE_CODE (arg1) == BIT_AND_EXPR)
12125         {
12126           tree arg00 = TREE_OPERAND (arg0, 0);
12127           tree arg01 = TREE_OPERAND (arg0, 1);
12128           tree arg10 = TREE_OPERAND (arg1, 0);
12129           tree arg11 = TREE_OPERAND (arg1, 1);
12130           tree itype = TREE_TYPE (arg0);
12131
12132           if (operand_equal_p (arg01, arg11, 0))
12133             return fold_build2 (code, type,
12134                                 fold_build2 (BIT_AND_EXPR, itype,
12135                                              fold_build2 (BIT_XOR_EXPR, itype,
12136                                                           arg00, arg10),
12137                                              arg01),
12138                                 build_int_cst (itype, 0));
12139
12140           if (operand_equal_p (arg01, arg10, 0))
12141             return fold_build2 (code, type,
12142                                 fold_build2 (BIT_AND_EXPR, itype,
12143                                              fold_build2 (BIT_XOR_EXPR, itype,
12144                                                           arg00, arg11),
12145                                              arg01),
12146                                 build_int_cst (itype, 0));
12147
12148           if (operand_equal_p (arg00, arg11, 0))
12149             return fold_build2 (code, type,
12150                                 fold_build2 (BIT_AND_EXPR, itype,
12151                                              fold_build2 (BIT_XOR_EXPR, itype,
12152                                                           arg01, arg10),
12153                                              arg00),
12154                                 build_int_cst (itype, 0));
12155
12156           if (operand_equal_p (arg00, arg10, 0))
12157             return fold_build2 (code, type,
12158                                 fold_build2 (BIT_AND_EXPR, itype,
12159                                              fold_build2 (BIT_XOR_EXPR, itype,
12160                                                           arg01, arg11),
12161                                              arg00),
12162                                 build_int_cst (itype, 0));
12163         }
12164
12165       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12166           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12167         {
12168           tree arg00 = TREE_OPERAND (arg0, 0);
12169           tree arg01 = TREE_OPERAND (arg0, 1);
12170           tree arg10 = TREE_OPERAND (arg1, 0);
12171           tree arg11 = TREE_OPERAND (arg1, 1);
12172           tree itype = TREE_TYPE (arg0);
12173
12174           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12175              operand_equal_p guarantees no side-effects so we don't need
12176              to use omit_one_operand on Z.  */
12177           if (operand_equal_p (arg01, arg11, 0))
12178             return fold_build2 (code, type, arg00, arg10);
12179           if (operand_equal_p (arg01, arg10, 0))
12180             return fold_build2 (code, type, arg00, arg11);
12181           if (operand_equal_p (arg00, arg11, 0))
12182             return fold_build2 (code, type, arg01, arg10);
12183           if (operand_equal_p (arg00, arg10, 0))
12184             return fold_build2 (code, type, arg01, arg11);
12185
12186           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12187           if (TREE_CODE (arg01) == INTEGER_CST
12188               && TREE_CODE (arg11) == INTEGER_CST)
12189             return fold_build2 (code, type,
12190                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12191                                              fold_build2 (BIT_XOR_EXPR, itype,
12192                                                           arg01, arg11)),
12193                                 arg10);
12194         }
12195
12196       /* Attempt to simplify equality/inequality comparisons of complex
12197          values.  Only lower the comparison if the result is known or
12198          can be simplified to a single scalar comparison.  */
12199       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12200            || TREE_CODE (arg0) == COMPLEX_CST)
12201           && (TREE_CODE (arg1) == COMPLEX_EXPR
12202               || TREE_CODE (arg1) == COMPLEX_CST))
12203         {
12204           tree real0, imag0, real1, imag1;
12205           tree rcond, icond;
12206
12207           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12208             {
12209               real0 = TREE_OPERAND (arg0, 0);
12210               imag0 = TREE_OPERAND (arg0, 1);
12211             }
12212           else
12213             {
12214               real0 = TREE_REALPART (arg0);
12215               imag0 = TREE_IMAGPART (arg0);
12216             }
12217
12218           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12219             {
12220               real1 = TREE_OPERAND (arg1, 0);
12221               imag1 = TREE_OPERAND (arg1, 1);
12222             }
12223           else
12224             {
12225               real1 = TREE_REALPART (arg1);
12226               imag1 = TREE_IMAGPART (arg1);
12227             }
12228
12229           rcond = fold_binary (code, type, real0, real1);
12230           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12231             {
12232               if (integer_zerop (rcond))
12233                 {
12234                   if (code == EQ_EXPR)
12235                     return omit_two_operands (type, boolean_false_node,
12236                                               imag0, imag1);
12237                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12238                 }
12239               else
12240                 {
12241                   if (code == NE_EXPR)
12242                     return omit_two_operands (type, boolean_true_node,
12243                                               imag0, imag1);
12244                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12245                 }
12246             }
12247
12248           icond = fold_binary (code, type, imag0, imag1);
12249           if (icond && TREE_CODE (icond) == INTEGER_CST)
12250             {
12251               if (integer_zerop (icond))
12252                 {
12253                   if (code == EQ_EXPR)
12254                     return omit_two_operands (type, boolean_false_node,
12255                                               real0, real1);
12256                   return fold_build2 (NE_EXPR, type, real0, real1);
12257                 }
12258               else
12259                 {
12260                   if (code == NE_EXPR)
12261                     return omit_two_operands (type, boolean_true_node,
12262                                               real0, real1);
12263                   return fold_build2 (EQ_EXPR, type, real0, real1);
12264                 }
12265             }
12266         }
12267
12268       return NULL_TREE;
12269
12270     case LT_EXPR:
12271     case GT_EXPR:
12272     case LE_EXPR:
12273     case GE_EXPR:
12274       tem = fold_comparison (code, type, op0, op1);
12275       if (tem != NULL_TREE)
12276         return tem;
12277
12278       /* Transform comparisons of the form X +- C CMP X.  */
12279       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12280           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12281           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12282                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12283               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12284                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12285         {
12286           tree arg01 = TREE_OPERAND (arg0, 1);
12287           enum tree_code code0 = TREE_CODE (arg0);
12288           int is_positive;
12289
12290           if (TREE_CODE (arg01) == REAL_CST)
12291             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12292           else
12293             is_positive = tree_int_cst_sgn (arg01);
12294
12295           /* (X - c) > X becomes false.  */
12296           if (code == GT_EXPR
12297               && ((code0 == MINUS_EXPR && is_positive >= 0)
12298                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12299             {
12300               if (TREE_CODE (arg01) == INTEGER_CST
12301                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12302                 fold_overflow_warning (("assuming signed overflow does not "
12303                                         "occur when assuming that (X - c) > X "
12304                                         "is always false"),
12305                                        WARN_STRICT_OVERFLOW_ALL);
12306               return constant_boolean_node (0, type);
12307             }
12308
12309           /* Likewise (X + c) < X becomes false.  */
12310           if (code == LT_EXPR
12311               && ((code0 == PLUS_EXPR && is_positive >= 0)
12312                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12313             {
12314               if (TREE_CODE (arg01) == INTEGER_CST
12315                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12316                 fold_overflow_warning (("assuming signed overflow does not "
12317                                         "occur when assuming that "
12318                                         "(X + c) < X is always false"),
12319                                        WARN_STRICT_OVERFLOW_ALL);
12320               return constant_boolean_node (0, type);
12321             }
12322
12323           /* Convert (X - c) <= X to true.  */
12324           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12325               && code == LE_EXPR
12326               && ((code0 == MINUS_EXPR && is_positive >= 0)
12327                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12328             {
12329               if (TREE_CODE (arg01) == INTEGER_CST
12330                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12331                 fold_overflow_warning (("assuming signed overflow does not "
12332                                         "occur when assuming that "
12333                                         "(X - c) <= X is always true"),
12334                                        WARN_STRICT_OVERFLOW_ALL);
12335               return constant_boolean_node (1, type);
12336             }
12337
12338           /* Convert (X + c) >= X to true.  */
12339           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12340               && code == GE_EXPR
12341               && ((code0 == PLUS_EXPR && is_positive >= 0)
12342                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12343             {
12344               if (TREE_CODE (arg01) == INTEGER_CST
12345                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12346                 fold_overflow_warning (("assuming signed overflow does not "
12347                                         "occur when assuming that "
12348                                         "(X + c) >= X is always true"),
12349                                        WARN_STRICT_OVERFLOW_ALL);
12350               return constant_boolean_node (1, type);
12351             }
12352
12353           if (TREE_CODE (arg01) == INTEGER_CST)
12354             {
12355               /* Convert X + c > X and X - c < X to true for integers.  */
12356               if (code == GT_EXPR
12357                   && ((code0 == PLUS_EXPR && is_positive > 0)
12358                       || (code0 == MINUS_EXPR && is_positive < 0)))
12359                 {
12360                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12361                     fold_overflow_warning (("assuming signed overflow does "
12362                                             "not occur when assuming that "
12363                                             "(X + c) > X is always true"),
12364                                            WARN_STRICT_OVERFLOW_ALL);
12365                   return constant_boolean_node (1, type);
12366                 }
12367
12368               if (code == LT_EXPR
12369                   && ((code0 == MINUS_EXPR && is_positive > 0)
12370                       || (code0 == PLUS_EXPR && is_positive < 0)))
12371                 {
12372                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12373                     fold_overflow_warning (("assuming signed overflow does "
12374                                             "not occur when assuming that "
12375                                             "(X - c) < X is always true"),
12376                                            WARN_STRICT_OVERFLOW_ALL);
12377                   return constant_boolean_node (1, type);
12378                 }
12379
12380               /* Convert X + c <= X and X - c >= X to false for integers.  */
12381               if (code == LE_EXPR
12382                   && ((code0 == PLUS_EXPR && is_positive > 0)
12383                       || (code0 == MINUS_EXPR && is_positive < 0)))
12384                 {
12385                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12386                     fold_overflow_warning (("assuming signed overflow does "
12387                                             "not occur when assuming that "
12388                                             "(X + c) <= X is always false"),
12389                                            WARN_STRICT_OVERFLOW_ALL);
12390                   return constant_boolean_node (0, type);
12391                 }
12392
12393               if (code == GE_EXPR
12394                   && ((code0 == MINUS_EXPR && is_positive > 0)
12395                       || (code0 == PLUS_EXPR && is_positive < 0)))
12396                 {
12397                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12398                     fold_overflow_warning (("assuming signed overflow does "
12399                                             "not occur when assuming that "
12400                                             "(X - c) >= X is always false"),
12401                                            WARN_STRICT_OVERFLOW_ALL);
12402                   return constant_boolean_node (0, type);
12403                 }
12404             }
12405         }
12406
12407       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
12408          This transformation affects the cases which are handled in later
12409          optimizations involving comparisons with non-negative constants.  */
12410       if (TREE_CODE (arg1) == INTEGER_CST
12411           && TREE_CODE (arg0) != INTEGER_CST
12412           && tree_int_cst_sgn (arg1) > 0)
12413         {
12414           if (code == GE_EXPR)
12415             {
12416               arg1 = const_binop (MINUS_EXPR, arg1,
12417                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12418               return fold_build2 (GT_EXPR, type, arg0,
12419                                   fold_convert (TREE_TYPE (arg0), arg1));
12420             }
12421           if (code == LT_EXPR)
12422             {
12423               arg1 = const_binop (MINUS_EXPR, arg1,
12424                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
12425               return fold_build2 (LE_EXPR, type, arg0,
12426                                   fold_convert (TREE_TYPE (arg0), arg1));
12427             }
12428         }
12429
12430       /* Comparisons with the highest or lowest possible integer of
12431          the specified precision will have known values.  */
12432       {
12433         tree arg1_type = TREE_TYPE (arg1);
12434         unsigned int width = TYPE_PRECISION (arg1_type);
12435
12436         if (TREE_CODE (arg1) == INTEGER_CST
12437             && !TREE_OVERFLOW (arg1)
12438             && width <= 2 * HOST_BITS_PER_WIDE_INT
12439             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12440           {
12441             HOST_WIDE_INT signed_max_hi;
12442             unsigned HOST_WIDE_INT signed_max_lo;
12443             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12444
12445             if (width <= HOST_BITS_PER_WIDE_INT)
12446               {
12447                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12448                                 - 1;
12449                 signed_max_hi = 0;
12450                 max_hi = 0;
12451
12452                 if (TYPE_UNSIGNED (arg1_type))
12453                   {
12454                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12455                     min_lo = 0;
12456                     min_hi = 0;
12457                   }
12458                 else
12459                   {
12460                     max_lo = signed_max_lo;
12461                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12462                     min_hi = -1;
12463                   }
12464               }
12465             else
12466               {
12467                 width -= HOST_BITS_PER_WIDE_INT;
12468                 signed_max_lo = -1;
12469                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12470                                 - 1;
12471                 max_lo = -1;
12472                 min_lo = 0;
12473
12474                 if (TYPE_UNSIGNED (arg1_type))
12475                   {
12476                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12477                     min_hi = 0;
12478                   }
12479                 else
12480                   {
12481                     max_hi = signed_max_hi;
12482                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12483                   }
12484               }
12485
12486             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12487                 && TREE_INT_CST_LOW (arg1) == max_lo)
12488               switch (code)
12489                 {
12490                 case GT_EXPR:
12491                   return omit_one_operand (type, integer_zero_node, arg0);
12492
12493                 case GE_EXPR:
12494                   return fold_build2 (EQ_EXPR, type, op0, op1);
12495
12496                 case LE_EXPR:
12497                   return omit_one_operand (type, integer_one_node, arg0);
12498
12499                 case LT_EXPR:
12500                   return fold_build2 (NE_EXPR, type, op0, op1);
12501
12502                 /* The GE_EXPR and LT_EXPR cases above are not normally
12503                    reached because of previous transformations.  */
12504
12505                 default:
12506                   break;
12507                 }
12508             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12509                      == max_hi
12510                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12511               switch (code)
12512                 {
12513                 case GT_EXPR:
12514                   arg1 = const_binop (PLUS_EXPR, arg1,
12515                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12516                   return fold_build2 (EQ_EXPR, type,
12517                                       fold_convert (TREE_TYPE (arg1), arg0),
12518                                       arg1);
12519                 case LE_EXPR:
12520                   arg1 = const_binop (PLUS_EXPR, arg1,
12521                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12522                   return fold_build2 (NE_EXPR, type,
12523                                       fold_convert (TREE_TYPE (arg1), arg0),
12524                                       arg1);
12525                 default:
12526                   break;
12527                 }
12528             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12529                      == min_hi
12530                      && TREE_INT_CST_LOW (arg1) == min_lo)
12531               switch (code)
12532                 {
12533                 case LT_EXPR:
12534                   return omit_one_operand (type, integer_zero_node, arg0);
12535
12536                 case LE_EXPR:
12537                   return fold_build2 (EQ_EXPR, type, op0, op1);
12538
12539                 case GE_EXPR:
12540                   return omit_one_operand (type, integer_one_node, arg0);
12541
12542                 case GT_EXPR:
12543                   return fold_build2 (NE_EXPR, type, op0, op1);
12544
12545                 default:
12546                   break;
12547                 }
12548             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12549                      == min_hi
12550                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12551               switch (code)
12552                 {
12553                 case GE_EXPR:
12554                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12555                   return fold_build2 (NE_EXPR, type,
12556                                       fold_convert (TREE_TYPE (arg1), arg0),
12557                                       arg1);
12558                 case LT_EXPR:
12559                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12560                   return fold_build2 (EQ_EXPR, type,
12561                                       fold_convert (TREE_TYPE (arg1), arg0),
12562                                       arg1);
12563                 default:
12564                   break;
12565                 }
12566
12567             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12568                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12569                      && TYPE_UNSIGNED (arg1_type)
12570                      /* We will flip the signedness of the comparison operator
12571                         associated with the mode of arg1, so the sign bit is
12572                         specified by this mode.  Check that arg1 is the signed
12573                         max associated with this sign bit.  */
12574                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12575                      /* signed_type does not work on pointer types.  */
12576                      && INTEGRAL_TYPE_P (arg1_type))
12577               {
12578                 /* The following case also applies to X < signed_max+1
12579                    and X >= signed_max+1 because previous transformations.  */
12580                 if (code == LE_EXPR || code == GT_EXPR)
12581                   {
12582                     tree st;
12583                     st = signed_type_for (TREE_TYPE (arg1));
12584                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12585                                         type, fold_convert (st, arg0),
12586                                         build_int_cst (st, 0));
12587                   }
12588               }
12589           }
12590       }
12591
12592       /* If we are comparing an ABS_EXPR with a constant, we can
12593          convert all the cases into explicit comparisons, but they may
12594          well not be faster than doing the ABS and one comparison.
12595          But ABS (X) <= C is a range comparison, which becomes a subtraction
12596          and a comparison, and is probably faster.  */
12597       if (code == LE_EXPR
12598           && TREE_CODE (arg1) == INTEGER_CST
12599           && TREE_CODE (arg0) == ABS_EXPR
12600           && ! TREE_SIDE_EFFECTS (arg0)
12601           && (0 != (tem = negate_expr (arg1)))
12602           && TREE_CODE (tem) == INTEGER_CST
12603           && !TREE_OVERFLOW (tem))
12604         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12605                             build2 (GE_EXPR, type,
12606                                     TREE_OPERAND (arg0, 0), tem),
12607                             build2 (LE_EXPR, type,
12608                                     TREE_OPERAND (arg0, 0), arg1));
12609
12610       /* Convert ABS_EXPR<x> >= 0 to true.  */
12611       strict_overflow_p = false;
12612       if (code == GE_EXPR
12613           && (integer_zerop (arg1)
12614               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12615                   && real_zerop (arg1)))
12616           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12617         {
12618           if (strict_overflow_p)
12619             fold_overflow_warning (("assuming signed overflow does not occur "
12620                                     "when simplifying comparison of "
12621                                     "absolute value and zero"),
12622                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12623           return omit_one_operand (type, integer_one_node, arg0);
12624         }
12625
12626       /* Convert ABS_EXPR<x> < 0 to false.  */
12627       strict_overflow_p = false;
12628       if (code == LT_EXPR
12629           && (integer_zerop (arg1) || real_zerop (arg1))
12630           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12631         {
12632           if (strict_overflow_p)
12633             fold_overflow_warning (("assuming signed overflow does not occur "
12634                                     "when simplifying comparison of "
12635                                     "absolute value and zero"),
12636                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12637           return omit_one_operand (type, integer_zero_node, arg0);
12638         }
12639
12640       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12641          and similarly for >= into !=.  */
12642       if ((code == LT_EXPR || code == GE_EXPR)
12643           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12644           && TREE_CODE (arg1) == LSHIFT_EXPR
12645           && integer_onep (TREE_OPERAND (arg1, 0)))
12646         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12647                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12648                                TREE_OPERAND (arg1, 1)),
12649                        build_int_cst (TREE_TYPE (arg0), 0));
12650
12651       if ((code == LT_EXPR || code == GE_EXPR)
12652           && TYPE_UNSIGNED (TREE_TYPE (arg0))
12653           && (TREE_CODE (arg1) == NOP_EXPR
12654               || TREE_CODE (arg1) == CONVERT_EXPR)
12655           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12656           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12657         return
12658           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12659                   fold_convert (TREE_TYPE (arg0),
12660                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12661                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
12662                                                       1))),
12663                   build_int_cst (TREE_TYPE (arg0), 0));
12664
12665       return NULL_TREE;
12666
12667     case UNORDERED_EXPR:
12668     case ORDERED_EXPR:
12669     case UNLT_EXPR:
12670     case UNLE_EXPR:
12671     case UNGT_EXPR:
12672     case UNGE_EXPR:
12673     case UNEQ_EXPR:
12674     case LTGT_EXPR:
12675       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12676         {
12677           t1 = fold_relational_const (code, type, arg0, arg1);
12678           if (t1 != NULL_TREE)
12679             return t1;
12680         }
12681
12682       /* If the first operand is NaN, the result is constant.  */
12683       if (TREE_CODE (arg0) == REAL_CST
12684           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12685           && (code != LTGT_EXPR || ! flag_trapping_math))
12686         {
12687           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12688                ? integer_zero_node
12689                : integer_one_node;
12690           return omit_one_operand (type, t1, arg1);
12691         }
12692
12693       /* If the second operand is NaN, the result is constant.  */
12694       if (TREE_CODE (arg1) == REAL_CST
12695           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12696           && (code != LTGT_EXPR || ! flag_trapping_math))
12697         {
12698           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12699                ? integer_zero_node
12700                : integer_one_node;
12701           return omit_one_operand (type, t1, arg0);
12702         }
12703
12704       /* Simplify unordered comparison of something with itself.  */
12705       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12706           && operand_equal_p (arg0, arg1, 0))
12707         return constant_boolean_node (1, type);
12708
12709       if (code == LTGT_EXPR
12710           && !flag_trapping_math
12711           && operand_equal_p (arg0, arg1, 0))
12712         return constant_boolean_node (0, type);
12713
12714       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
12715       {
12716         tree targ0 = strip_float_extensions (arg0);
12717         tree targ1 = strip_float_extensions (arg1);
12718         tree newtype = TREE_TYPE (targ0);
12719
12720         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12721           newtype = TREE_TYPE (targ1);
12722
12723         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12724           return fold_build2 (code, type, fold_convert (newtype, targ0),
12725                               fold_convert (newtype, targ1));
12726       }
12727
12728       return NULL_TREE;
12729
12730     case COMPOUND_EXPR:
12731       /* When pedantic, a compound expression can be neither an lvalue
12732          nor an integer constant expression.  */
12733       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12734         return NULL_TREE;
12735       /* Don't let (0, 0) be null pointer constant.  */
12736       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12737                                  : fold_convert (type, arg1);
12738       return pedantic_non_lvalue (tem);
12739
12740     case COMPLEX_EXPR:
12741       if ((TREE_CODE (arg0) == REAL_CST
12742            && TREE_CODE (arg1) == REAL_CST)
12743           || (TREE_CODE (arg0) == INTEGER_CST
12744               && TREE_CODE (arg1) == INTEGER_CST))
12745         return build_complex (type, arg0, arg1);
12746       return NULL_TREE;
12747
12748     case ASSERT_EXPR:
12749       /* An ASSERT_EXPR should never be passed to fold_binary.  */
12750       gcc_unreachable ();
12751
12752     default:
12753       return NULL_TREE;
12754     } /* switch (code) */
12755 }
12756
12757 /* Callback for walk_tree, looking for LABEL_EXPR.
12758    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12759    Do not check the sub-tree of GOTO_EXPR.  */
12760
12761 static tree
12762 contains_label_1 (tree *tp,
12763                   int *walk_subtrees,
12764                   void *data ATTRIBUTE_UNUSED)
12765 {
12766   switch (TREE_CODE (*tp))
12767     {
12768     case LABEL_EXPR:
12769       return *tp;
12770     case GOTO_EXPR:
12771       *walk_subtrees = 0;
12772     /* no break */
12773     default:
12774       return NULL_TREE;
12775     }
12776 }
12777
12778 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12779    accessible from outside the sub-tree. Returns NULL_TREE if no
12780    addressable label is found.  */
12781
12782 static bool
12783 contains_label_p (tree st)
12784 {
12785   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12786 }
12787
12788 /* Fold a ternary expression of code CODE and type TYPE with operands
12789    OP0, OP1, and OP2.  Return the folded expression if folding is
12790    successful.  Otherwise, return NULL_TREE.  */
12791
12792 tree
12793 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12794 {
12795   tree tem;
12796   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12797   enum tree_code_class kind = TREE_CODE_CLASS (code);
12798
12799   gcc_assert (IS_EXPR_CODE_CLASS (kind)
12800               && TREE_CODE_LENGTH (code) == 3);
12801
12802   /* Strip any conversions that don't change the mode.  This is safe
12803      for every expression, except for a comparison expression because
12804      its signedness is derived from its operands.  So, in the latter
12805      case, only strip conversions that don't change the signedness.
12806
12807      Note that this is done as an internal manipulation within the
12808      constant folder, in order to find the simplest representation of
12809      the arguments so that their form can be studied.  In any cases,
12810      the appropriate type conversions should be put back in the tree
12811      that will get out of the constant folder.  */
12812   if (op0)
12813     {
12814       arg0 = op0;
12815       STRIP_NOPS (arg0);
12816     }
12817
12818   if (op1)
12819     {
12820       arg1 = op1;
12821       STRIP_NOPS (arg1);
12822     }
12823
12824   switch (code)
12825     {
12826     case COMPONENT_REF:
12827       if (TREE_CODE (arg0) == CONSTRUCTOR
12828           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12829         {
12830           unsigned HOST_WIDE_INT idx;
12831           tree field, value;
12832           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12833             if (field == arg1)
12834               return value;
12835         }
12836       return NULL_TREE;
12837
12838     case COND_EXPR:
12839       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12840          so all simple results must be passed through pedantic_non_lvalue.  */
12841       if (TREE_CODE (arg0) == INTEGER_CST)
12842         {
12843           tree unused_op = integer_zerop (arg0) ? op1 : op2;
12844           tem = integer_zerop (arg0) ? op2 : op1;
12845           /* Only optimize constant conditions when the selected branch
12846              has the same type as the COND_EXPR.  This avoids optimizing
12847              away "c ? x : throw", where the throw has a void type.
12848              Avoid throwing away that operand which contains label.  */
12849           if ((!TREE_SIDE_EFFECTS (unused_op)
12850                || !contains_label_p (unused_op))
12851               && (! VOID_TYPE_P (TREE_TYPE (tem))
12852                   || VOID_TYPE_P (type)))
12853             return pedantic_non_lvalue (tem);
12854           return NULL_TREE;
12855         }
12856       if (operand_equal_p (arg1, op2, 0))
12857         return pedantic_omit_one_operand (type, arg1, arg0);
12858
12859       /* If we have A op B ? A : C, we may be able to convert this to a
12860          simpler expression, depending on the operation and the values
12861          of B and C.  Signed zeros prevent all of these transformations,
12862          for reasons given above each one.
12863
12864          Also try swapping the arguments and inverting the conditional.  */
12865       if (COMPARISON_CLASS_P (arg0)
12866           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12867                                              arg1, TREE_OPERAND (arg0, 1))
12868           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12869         {
12870           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12871           if (tem)
12872             return tem;
12873         }
12874
12875       if (COMPARISON_CLASS_P (arg0)
12876           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12877                                              op2,
12878                                              TREE_OPERAND (arg0, 1))
12879           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12880         {
12881           tem = fold_truth_not_expr (arg0);
12882           if (tem && COMPARISON_CLASS_P (tem))
12883             {
12884               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12885               if (tem)
12886                 return tem;
12887             }
12888         }
12889
12890       /* If the second operand is simpler than the third, swap them
12891          since that produces better jump optimization results.  */
12892       if (truth_value_p (TREE_CODE (arg0))
12893           && tree_swap_operands_p (op1, op2, false))
12894         {
12895           /* See if this can be inverted.  If it can't, possibly because
12896              it was a floating-point inequality comparison, don't do
12897              anything.  */
12898           tem = fold_truth_not_expr (arg0);
12899           if (tem)
12900             return fold_build3 (code, type, tem, op2, op1);
12901         }
12902
12903       /* Convert A ? 1 : 0 to simply A.  */
12904       if (integer_onep (op1)
12905           && integer_zerop (op2)
12906           /* If we try to convert OP0 to our type, the
12907              call to fold will try to move the conversion inside
12908              a COND, which will recurse.  In that case, the COND_EXPR
12909              is probably the best choice, so leave it alone.  */
12910           && type == TREE_TYPE (arg0))
12911         return pedantic_non_lvalue (arg0);
12912
12913       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
12914          over COND_EXPR in cases such as floating point comparisons.  */
12915       if (integer_zerop (op1)
12916           && integer_onep (op2)
12917           && truth_value_p (TREE_CODE (arg0)))
12918         return pedantic_non_lvalue (fold_convert (type,
12919                                                   invert_truthvalue (arg0)));
12920
12921       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
12922       if (TREE_CODE (arg0) == LT_EXPR
12923           && integer_zerop (TREE_OPERAND (arg0, 1))
12924           && integer_zerop (op2)
12925           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12926         {
12927           /* sign_bit_p only checks ARG1 bits within A's precision.
12928              If <sign bit of A> has wider type than A, bits outside
12929              of A's precision in <sign bit of A> need to be checked.
12930              If they are all 0, this optimization needs to be done
12931              in unsigned A's type, if they are all 1 in signed A's type,
12932              otherwise this can't be done.  */
12933           if (TYPE_PRECISION (TREE_TYPE (tem))
12934               < TYPE_PRECISION (TREE_TYPE (arg1))
12935               && TYPE_PRECISION (TREE_TYPE (tem))
12936                  < TYPE_PRECISION (type))
12937             {
12938               unsigned HOST_WIDE_INT mask_lo;
12939               HOST_WIDE_INT mask_hi;
12940               int inner_width, outer_width;
12941               tree tem_type;
12942
12943               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12944               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12945               if (outer_width > TYPE_PRECISION (type))
12946                 outer_width = TYPE_PRECISION (type);
12947
12948               if (outer_width > HOST_BITS_PER_WIDE_INT)
12949                 {
12950                   mask_hi = ((unsigned HOST_WIDE_INT) -1
12951                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12952                   mask_lo = -1;
12953                 }
12954               else
12955                 {
12956                   mask_hi = 0;
12957                   mask_lo = ((unsigned HOST_WIDE_INT) -1
12958                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
12959                 }
12960               if (inner_width > HOST_BITS_PER_WIDE_INT)
12961                 {
12962                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12963                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
12964                   mask_lo = 0;
12965                 }
12966               else
12967                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12968                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
12969
12970               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12971                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12972                 {
12973                   tem_type = signed_type_for (TREE_TYPE (tem));
12974                   tem = fold_convert (tem_type, tem);
12975                 }
12976               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12977                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12978                 {
12979                   tem_type = unsigned_type_for (TREE_TYPE (tem));
12980                   tem = fold_convert (tem_type, tem);
12981                 }
12982               else
12983                 tem = NULL;
12984             }
12985
12986           if (tem)
12987             return fold_convert (type,
12988                                  fold_build2 (BIT_AND_EXPR,
12989                                               TREE_TYPE (tem), tem,
12990                                               fold_convert (TREE_TYPE (tem),
12991                                                             arg1)));
12992         }
12993
12994       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
12995          already handled above.  */
12996       if (TREE_CODE (arg0) == BIT_AND_EXPR
12997           && integer_onep (TREE_OPERAND (arg0, 1))
12998           && integer_zerop (op2)
12999           && integer_pow2p (arg1))
13000         {
13001           tree tem = TREE_OPERAND (arg0, 0);
13002           STRIP_NOPS (tem);
13003           if (TREE_CODE (tem) == RSHIFT_EXPR
13004               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13005               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13006                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13007             return fold_build2 (BIT_AND_EXPR, type,
13008                                 TREE_OPERAND (tem, 0), arg1);
13009         }
13010
13011       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13012          is probably obsolete because the first operand should be a
13013          truth value (that's why we have the two cases above), but let's
13014          leave it in until we can confirm this for all front-ends.  */
13015       if (integer_zerop (op2)
13016           && TREE_CODE (arg0) == NE_EXPR
13017           && integer_zerop (TREE_OPERAND (arg0, 1))
13018           && integer_pow2p (arg1)
13019           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13020           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13021                               arg1, OEP_ONLY_CONST))
13022         return pedantic_non_lvalue (fold_convert (type,
13023                                                   TREE_OPERAND (arg0, 0)));
13024
13025       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13026       if (integer_zerop (op2)
13027           && truth_value_p (TREE_CODE (arg0))
13028           && truth_value_p (TREE_CODE (arg1)))
13029         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13030                             fold_convert (type, arg0),
13031                             arg1);
13032
13033       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13034       if (integer_onep (op2)
13035           && truth_value_p (TREE_CODE (arg0))
13036           && truth_value_p (TREE_CODE (arg1)))
13037         {
13038           /* Only perform transformation if ARG0 is easily inverted.  */
13039           tem = fold_truth_not_expr (arg0);
13040           if (tem)
13041             return fold_build2 (TRUTH_ORIF_EXPR, type,
13042                                 fold_convert (type, tem),
13043                                 arg1);
13044         }
13045
13046       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13047       if (integer_zerop (arg1)
13048           && truth_value_p (TREE_CODE (arg0))
13049           && truth_value_p (TREE_CODE (op2)))
13050         {
13051           /* Only perform transformation if ARG0 is easily inverted.  */
13052           tem = fold_truth_not_expr (arg0);
13053           if (tem)
13054             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13055                                 fold_convert (type, tem),
13056                                 op2);
13057         }
13058
13059       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13060       if (integer_onep (arg1)
13061           && truth_value_p (TREE_CODE (arg0))
13062           && truth_value_p (TREE_CODE (op2)))
13063         return fold_build2 (TRUTH_ORIF_EXPR, type,
13064                             fold_convert (type, arg0),
13065                             op2);
13066
13067       return NULL_TREE;
13068
13069     case CALL_EXPR:
13070       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13071          of fold_ternary on them.  */
13072       gcc_unreachable ();
13073
13074     case BIT_FIELD_REF:
13075       if ((TREE_CODE (arg0) == VECTOR_CST
13076            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13077           && type == TREE_TYPE (TREE_TYPE (arg0)))
13078         {
13079           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13080           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13081
13082           if (width != 0
13083               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13084               && (idx % width) == 0
13085               && (idx = idx / width)
13086                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13087             {
13088               tree elements = NULL_TREE;
13089
13090               if (TREE_CODE (arg0) == VECTOR_CST)
13091                 elements = TREE_VECTOR_CST_ELTS (arg0);
13092               else
13093                 {
13094                   unsigned HOST_WIDE_INT idx;
13095                   tree value;
13096
13097                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13098                     elements = tree_cons (NULL_TREE, value, elements);
13099                 }
13100               while (idx-- > 0 && elements)
13101                 elements = TREE_CHAIN (elements);
13102               if (elements)
13103                 return TREE_VALUE (elements);
13104               else
13105                 return fold_convert (type, integer_zero_node);
13106             }
13107         }
13108       return NULL_TREE;
13109
13110     default:
13111       return NULL_TREE;
13112     } /* switch (code) */
13113 }
13114
13115 /* Perform constant folding and related simplification of EXPR.
13116    The related simplifications include x*1 => x, x*0 => 0, etc.,
13117    and application of the associative law.
13118    NOP_EXPR conversions may be removed freely (as long as we
13119    are careful not to change the type of the overall expression).
13120    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13121    but we can constant-fold them if they have constant operands.  */
13122
13123 #ifdef ENABLE_FOLD_CHECKING
13124 # define fold(x) fold_1 (x)
13125 static tree fold_1 (tree);
13126 static
13127 #endif
13128 tree
13129 fold (tree expr)
13130 {
13131   const tree t = expr;
13132   enum tree_code code = TREE_CODE (t);
13133   enum tree_code_class kind = TREE_CODE_CLASS (code);
13134   tree tem;
13135
13136   /* Return right away if a constant.  */
13137   if (kind == tcc_constant)
13138     return t;
13139
13140   /* CALL_EXPR-like objects with variable numbers of operands are
13141      treated specially.  */
13142   if (kind == tcc_vl_exp)
13143     {
13144       if (code == CALL_EXPR)
13145         {
13146           tem = fold_call_expr (expr, false);
13147           return tem ? tem : expr;
13148         }
13149       return expr;
13150     }
13151
13152   if (IS_EXPR_CODE_CLASS (kind)
13153       || IS_GIMPLE_STMT_CODE_CLASS (kind))
13154     {
13155       tree type = TREE_TYPE (t);
13156       tree op0, op1, op2;
13157
13158       switch (TREE_CODE_LENGTH (code))
13159         {
13160         case 1:
13161           op0 = TREE_OPERAND (t, 0);
13162           tem = fold_unary (code, type, op0);
13163           return tem ? tem : expr;
13164         case 2:
13165           op0 = TREE_OPERAND (t, 0);
13166           op1 = TREE_OPERAND (t, 1);
13167           tem = fold_binary (code, type, op0, op1);
13168           return tem ? tem : expr;
13169         case 3:
13170           op0 = TREE_OPERAND (t, 0);
13171           op1 = TREE_OPERAND (t, 1);
13172           op2 = TREE_OPERAND (t, 2);
13173           tem = fold_ternary (code, type, op0, op1, op2);
13174           return tem ? tem : expr;
13175         default:
13176           break;
13177         }
13178     }
13179
13180   switch (code)
13181     {
13182     case ARRAY_REF:
13183       {
13184         tree op0 = TREE_OPERAND (t, 0);
13185         tree op1 = TREE_OPERAND (t, 1);
13186
13187         if (TREE_CODE (op1) == INTEGER_CST
13188             && TREE_CODE (op0) == CONSTRUCTOR
13189             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13190           {
13191             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13192             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13193             unsigned HOST_WIDE_INT begin = 0;
13194
13195             /* Find a matching index by means of a binary search.  */
13196             while (begin != end)
13197               {
13198                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13199                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13200
13201                 if (TREE_CODE (index) == INTEGER_CST
13202                     && tree_int_cst_lt (index, op1))
13203                   begin = middle + 1;
13204                 else if (TREE_CODE (index) == INTEGER_CST
13205                          && tree_int_cst_lt (op1, index))
13206                   end = middle;
13207                 else if (TREE_CODE (index) == RANGE_EXPR
13208                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13209                   begin = middle + 1;
13210                 else if (TREE_CODE (index) == RANGE_EXPR
13211                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13212                   end = middle;
13213                 else
13214                   return VEC_index (constructor_elt, elts, middle)->value;
13215               }
13216           }
13217
13218         return t;
13219       }
13220
13221     case CONST_DECL:
13222       return fold (DECL_INITIAL (t));
13223
13224     default:
13225       return t;
13226     } /* switch (code) */
13227 }
13228
13229 #ifdef ENABLE_FOLD_CHECKING
13230 #undef fold
13231
13232 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13233 static void fold_check_failed (const_tree, const_tree);
13234 void print_fold_checksum (const_tree);
13235
13236 /* When --enable-checking=fold, compute a digest of expr before
13237    and after actual fold call to see if fold did not accidentally
13238    change original expr.  */
13239
13240 tree
13241 fold (tree expr)
13242 {
13243   tree ret;
13244   struct md5_ctx ctx;
13245   unsigned char checksum_before[16], checksum_after[16];
13246   htab_t ht;
13247
13248   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13249   md5_init_ctx (&ctx);
13250   fold_checksum_tree (expr, &ctx, ht);
13251   md5_finish_ctx (&ctx, checksum_before);
13252   htab_empty (ht);
13253
13254   ret = fold_1 (expr);
13255
13256   md5_init_ctx (&ctx);
13257   fold_checksum_tree (expr, &ctx, ht);
13258   md5_finish_ctx (&ctx, checksum_after);
13259   htab_delete (ht);
13260
13261   if (memcmp (checksum_before, checksum_after, 16))
13262     fold_check_failed (expr, ret);
13263
13264   return ret;
13265 }
13266
13267 void
13268 print_fold_checksum (const_tree expr)
13269 {
13270   struct md5_ctx ctx;
13271   unsigned char checksum[16], cnt;
13272   htab_t ht;
13273
13274   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13275   md5_init_ctx (&ctx);
13276   fold_checksum_tree (expr, &ctx, ht);
13277   md5_finish_ctx (&ctx, checksum);
13278   htab_delete (ht);
13279   for (cnt = 0; cnt < 16; ++cnt)
13280     fprintf (stderr, "%02x", checksum[cnt]);
13281   putc ('\n', stderr);
13282 }
13283
13284 static void
13285 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13286 {
13287   internal_error ("fold check: original tree changed by fold");
13288 }
13289
13290 static void
13291 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13292 {
13293   const void **slot;
13294   enum tree_code code;
13295   struct tree_function_decl buf;
13296   int i, len;
13297   
13298 recursive_label:
13299
13300   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13301                <= sizeof (struct tree_function_decl))
13302               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13303   if (expr == NULL)
13304     return;
13305   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13306   if (*slot != NULL)
13307     return;
13308   *slot = expr;
13309   code = TREE_CODE (expr);
13310   if (TREE_CODE_CLASS (code) == tcc_declaration
13311       && DECL_ASSEMBLER_NAME_SET_P (expr))
13312     {
13313       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13314       memcpy ((char *) &buf, expr, tree_size (expr));
13315       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13316       expr = (tree) &buf;
13317     }
13318   else if (TREE_CODE_CLASS (code) == tcc_type
13319            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13320                || TYPE_CACHED_VALUES_P (expr)
13321                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13322     {
13323       /* Allow these fields to be modified.  */
13324       tree tmp;
13325       memcpy ((char *) &buf, expr, tree_size (expr));
13326       expr = tmp = (tree) &buf;
13327       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13328       TYPE_POINTER_TO (tmp) = NULL;
13329       TYPE_REFERENCE_TO (tmp) = NULL;
13330       if (TYPE_CACHED_VALUES_P (tmp))
13331         {
13332           TYPE_CACHED_VALUES_P (tmp) = 0;
13333           TYPE_CACHED_VALUES (tmp) = NULL;
13334         }
13335     }
13336   md5_process_bytes (expr, tree_size (expr), ctx);
13337   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13338   if (TREE_CODE_CLASS (code) != tcc_type
13339       && TREE_CODE_CLASS (code) != tcc_declaration
13340       && code != TREE_LIST
13341       && code != SSA_NAME)
13342     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13343   switch (TREE_CODE_CLASS (code))
13344     {
13345     case tcc_constant:
13346       switch (code)
13347         {
13348         case STRING_CST:
13349           md5_process_bytes (TREE_STRING_POINTER (expr),
13350                              TREE_STRING_LENGTH (expr), ctx);
13351           break;
13352         case COMPLEX_CST:
13353           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13354           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13355           break;
13356         case VECTOR_CST:
13357           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13358           break;
13359         default:
13360           break;
13361         }
13362       break;
13363     case tcc_exceptional:
13364       switch (code)
13365         {
13366         case TREE_LIST:
13367           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13368           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13369           expr = TREE_CHAIN (expr);
13370           goto recursive_label;
13371           break;
13372         case TREE_VEC:
13373           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13374             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13375           break;
13376         default:
13377           break;
13378         }
13379       break;
13380     case tcc_expression:
13381     case tcc_reference:
13382     case tcc_comparison:
13383     case tcc_unary:
13384     case tcc_binary:
13385     case tcc_statement:
13386     case tcc_vl_exp:
13387       len = TREE_OPERAND_LENGTH (expr);
13388       for (i = 0; i < len; ++i)
13389         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13390       break;
13391     case tcc_declaration:
13392       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13393       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13394       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13395         {
13396           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13397           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13398           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13399           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13400           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13401         }
13402       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13403         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13404           
13405       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13406         {
13407           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13408           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13409           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13410         }
13411       break;
13412     case tcc_type:
13413       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13414         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13415       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13416       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13417       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13418       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13419       if (INTEGRAL_TYPE_P (expr)
13420           || SCALAR_FLOAT_TYPE_P (expr))
13421         {
13422           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13423           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13424         }
13425       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13426       if (TREE_CODE (expr) == RECORD_TYPE
13427           || TREE_CODE (expr) == UNION_TYPE
13428           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13429         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13430       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13431       break;
13432     default:
13433       break;
13434     }
13435 }
13436
13437 /* Helper function for outputting the checksum of a tree T.  When
13438    debugging with gdb, you can "define mynext" to be "next" followed
13439    by "call debug_fold_checksum (op0)", then just trace down till the
13440    outputs differ.  */
13441
13442 void
13443 debug_fold_checksum (const_tree t)
13444 {
13445   int i;
13446   unsigned char checksum[16];
13447   struct md5_ctx ctx;
13448   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13449   
13450   md5_init_ctx (&ctx);
13451   fold_checksum_tree (t, &ctx, ht);
13452   md5_finish_ctx (&ctx, checksum);
13453   htab_empty (ht);
13454
13455   for (i = 0; i < 16; i++)
13456     fprintf (stderr, "%d ", checksum[i]);
13457
13458   fprintf (stderr, "\n");
13459 }
13460
13461 #endif
13462
13463 /* Fold a unary tree expression with code CODE of type TYPE with an
13464    operand OP0.  Return a folded expression if successful.  Otherwise,
13465    return a tree expression with code CODE of type TYPE with an
13466    operand OP0.  */
13467
13468 tree
13469 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13470 {
13471   tree tem;
13472 #ifdef ENABLE_FOLD_CHECKING
13473   unsigned char checksum_before[16], checksum_after[16];
13474   struct md5_ctx ctx;
13475   htab_t ht;
13476
13477   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13478   md5_init_ctx (&ctx);
13479   fold_checksum_tree (op0, &ctx, ht);
13480   md5_finish_ctx (&ctx, checksum_before);
13481   htab_empty (ht);
13482 #endif
13483   
13484   tem = fold_unary (code, type, op0);
13485   if (!tem)
13486     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13487   
13488 #ifdef ENABLE_FOLD_CHECKING
13489   md5_init_ctx (&ctx);
13490   fold_checksum_tree (op0, &ctx, ht);
13491   md5_finish_ctx (&ctx, checksum_after);
13492   htab_delete (ht);
13493
13494   if (memcmp (checksum_before, checksum_after, 16))
13495     fold_check_failed (op0, tem);
13496 #endif
13497   return tem;
13498 }
13499
13500 /* Fold a binary tree expression with code CODE of type TYPE with
13501    operands OP0 and OP1.  Return a folded expression if successful.
13502    Otherwise, return a tree expression with code CODE of type TYPE
13503    with operands OP0 and OP1.  */
13504
13505 tree
13506 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13507                   MEM_STAT_DECL)
13508 {
13509   tree tem;
13510 #ifdef ENABLE_FOLD_CHECKING
13511   unsigned char checksum_before_op0[16],
13512                 checksum_before_op1[16],
13513                 checksum_after_op0[16],
13514                 checksum_after_op1[16];
13515   struct md5_ctx ctx;
13516   htab_t ht;
13517
13518   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13519   md5_init_ctx (&ctx);
13520   fold_checksum_tree (op0, &ctx, ht);
13521   md5_finish_ctx (&ctx, checksum_before_op0);
13522   htab_empty (ht);
13523
13524   md5_init_ctx (&ctx);
13525   fold_checksum_tree (op1, &ctx, ht);
13526   md5_finish_ctx (&ctx, checksum_before_op1);
13527   htab_empty (ht);
13528 #endif
13529
13530   tem = fold_binary (code, type, op0, op1);
13531   if (!tem)
13532     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13533   
13534 #ifdef ENABLE_FOLD_CHECKING
13535   md5_init_ctx (&ctx);
13536   fold_checksum_tree (op0, &ctx, ht);
13537   md5_finish_ctx (&ctx, checksum_after_op0);
13538   htab_empty (ht);
13539
13540   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13541     fold_check_failed (op0, tem);
13542   
13543   md5_init_ctx (&ctx);
13544   fold_checksum_tree (op1, &ctx, ht);
13545   md5_finish_ctx (&ctx, checksum_after_op1);
13546   htab_delete (ht);
13547
13548   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13549     fold_check_failed (op1, tem);
13550 #endif
13551   return tem;
13552 }
13553
13554 /* Fold a ternary tree expression with code CODE of type TYPE with
13555    operands OP0, OP1, and OP2.  Return a folded expression if
13556    successful.  Otherwise, return a tree expression with code CODE of
13557    type TYPE with operands OP0, OP1, and OP2.  */
13558
13559 tree
13560 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13561              MEM_STAT_DECL)
13562 {
13563   tree tem;
13564 #ifdef ENABLE_FOLD_CHECKING
13565   unsigned char checksum_before_op0[16],
13566                 checksum_before_op1[16],
13567                 checksum_before_op2[16],
13568                 checksum_after_op0[16],
13569                 checksum_after_op1[16],
13570                 checksum_after_op2[16];
13571   struct md5_ctx ctx;
13572   htab_t ht;
13573
13574   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13575   md5_init_ctx (&ctx);
13576   fold_checksum_tree (op0, &ctx, ht);
13577   md5_finish_ctx (&ctx, checksum_before_op0);
13578   htab_empty (ht);
13579
13580   md5_init_ctx (&ctx);
13581   fold_checksum_tree (op1, &ctx, ht);
13582   md5_finish_ctx (&ctx, checksum_before_op1);
13583   htab_empty (ht);
13584
13585   md5_init_ctx (&ctx);
13586   fold_checksum_tree (op2, &ctx, ht);
13587   md5_finish_ctx (&ctx, checksum_before_op2);
13588   htab_empty (ht);
13589 #endif
13590
13591   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13592   tem = fold_ternary (code, type, op0, op1, op2);
13593   if (!tem)
13594     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13595       
13596 #ifdef ENABLE_FOLD_CHECKING
13597   md5_init_ctx (&ctx);
13598   fold_checksum_tree (op0, &ctx, ht);
13599   md5_finish_ctx (&ctx, checksum_after_op0);
13600   htab_empty (ht);
13601
13602   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13603     fold_check_failed (op0, tem);
13604   
13605   md5_init_ctx (&ctx);
13606   fold_checksum_tree (op1, &ctx, ht);
13607   md5_finish_ctx (&ctx, checksum_after_op1);
13608   htab_empty (ht);
13609
13610   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13611     fold_check_failed (op1, tem);
13612   
13613   md5_init_ctx (&ctx);
13614   fold_checksum_tree (op2, &ctx, ht);
13615   md5_finish_ctx (&ctx, checksum_after_op2);
13616   htab_delete (ht);
13617
13618   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13619     fold_check_failed (op2, tem);
13620 #endif
13621   return tem;
13622 }
13623
13624 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13625    arguments in ARGARRAY, and a null static chain.
13626    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
13627    of type TYPE from the given operands as constructed by build_call_array.  */
13628
13629 tree
13630 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13631 {
13632   tree tem;
13633 #ifdef ENABLE_FOLD_CHECKING
13634   unsigned char checksum_before_fn[16],
13635                 checksum_before_arglist[16],
13636                 checksum_after_fn[16],
13637                 checksum_after_arglist[16];
13638   struct md5_ctx ctx;
13639   htab_t ht;
13640   int i;
13641
13642   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13643   md5_init_ctx (&ctx);
13644   fold_checksum_tree (fn, &ctx, ht);
13645   md5_finish_ctx (&ctx, checksum_before_fn);
13646   htab_empty (ht);
13647
13648   md5_init_ctx (&ctx);
13649   for (i = 0; i < nargs; i++)
13650     fold_checksum_tree (argarray[i], &ctx, ht);
13651   md5_finish_ctx (&ctx, checksum_before_arglist);
13652   htab_empty (ht);
13653 #endif
13654
13655   tem = fold_builtin_call_array (type, fn, nargs, argarray);
13656       
13657 #ifdef ENABLE_FOLD_CHECKING
13658   md5_init_ctx (&ctx);
13659   fold_checksum_tree (fn, &ctx, ht);
13660   md5_finish_ctx (&ctx, checksum_after_fn);
13661   htab_empty (ht);
13662
13663   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13664     fold_check_failed (fn, tem);
13665   
13666   md5_init_ctx (&ctx);
13667   for (i = 0; i < nargs; i++)
13668     fold_checksum_tree (argarray[i], &ctx, ht);
13669   md5_finish_ctx (&ctx, checksum_after_arglist);
13670   htab_delete (ht);
13671
13672   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13673     fold_check_failed (NULL_TREE, tem);
13674 #endif
13675   return tem;
13676 }
13677
13678 /* Perform constant folding and related simplification of initializer
13679    expression EXPR.  These behave identically to "fold_buildN" but ignore
13680    potential run-time traps and exceptions that fold must preserve.  */
13681
13682 #define START_FOLD_INIT \
13683   int saved_signaling_nans = flag_signaling_nans;\
13684   int saved_trapping_math = flag_trapping_math;\
13685   int saved_rounding_math = flag_rounding_math;\
13686   int saved_trapv = flag_trapv;\
13687   int saved_folding_initializer = folding_initializer;\
13688   flag_signaling_nans = 0;\
13689   flag_trapping_math = 0;\
13690   flag_rounding_math = 0;\
13691   flag_trapv = 0;\
13692   folding_initializer = 1;
13693
13694 #define END_FOLD_INIT \
13695   flag_signaling_nans = saved_signaling_nans;\
13696   flag_trapping_math = saved_trapping_math;\
13697   flag_rounding_math = saved_rounding_math;\
13698   flag_trapv = saved_trapv;\
13699   folding_initializer = saved_folding_initializer;
13700
13701 tree
13702 fold_build1_initializer (enum tree_code code, tree type, tree op)
13703 {
13704   tree result;
13705   START_FOLD_INIT;
13706
13707   result = fold_build1 (code, type, op);
13708
13709   END_FOLD_INIT;
13710   return result;
13711 }
13712
13713 tree
13714 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13715 {
13716   tree result;
13717   START_FOLD_INIT;
13718
13719   result = fold_build2 (code, type, op0, op1);
13720
13721   END_FOLD_INIT;
13722   return result;
13723 }
13724
13725 tree
13726 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13727                          tree op2)
13728 {
13729   tree result;
13730   START_FOLD_INIT;
13731
13732   result = fold_build3 (code, type, op0, op1, op2);
13733
13734   END_FOLD_INIT;
13735   return result;
13736 }
13737
13738 tree
13739 fold_build_call_array_initializer (tree type, tree fn,
13740                                    int nargs, tree *argarray)
13741 {
13742   tree result;
13743   START_FOLD_INIT;
13744
13745   result = fold_build_call_array (type, fn, nargs, argarray);
13746
13747   END_FOLD_INIT;
13748   return result;
13749 }
13750
13751 #undef START_FOLD_INIT
13752 #undef END_FOLD_INIT
13753
13754 /* Determine if first argument is a multiple of second argument.  Return 0 if
13755    it is not, or we cannot easily determined it to be.
13756
13757    An example of the sort of thing we care about (at this point; this routine
13758    could surely be made more general, and expanded to do what the *_DIV_EXPR's
13759    fold cases do now) is discovering that
13760
13761      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13762
13763    is a multiple of
13764
13765      SAVE_EXPR (J * 8)
13766
13767    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13768
13769    This code also handles discovering that
13770
13771      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13772
13773    is a multiple of 8 so we don't have to worry about dealing with a
13774    possible remainder.
13775
13776    Note that we *look* inside a SAVE_EXPR only to determine how it was
13777    calculated; it is not safe for fold to do much of anything else with the
13778    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13779    at run time.  For example, the latter example above *cannot* be implemented
13780    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13781    evaluation time of the original SAVE_EXPR is not necessarily the same at
13782    the time the new expression is evaluated.  The only optimization of this
13783    sort that would be valid is changing
13784
13785      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13786
13787    divided by 8 to
13788
13789      SAVE_EXPR (I) * SAVE_EXPR (J)
13790
13791    (where the same SAVE_EXPR (J) is used in the original and the
13792    transformed version).  */
13793
13794 int
13795 multiple_of_p (tree type, const_tree top, const_tree bottom)
13796 {
13797   if (operand_equal_p (top, bottom, 0))
13798     return 1;
13799
13800   if (TREE_CODE (type) != INTEGER_TYPE)
13801     return 0;
13802
13803   switch (TREE_CODE (top))
13804     {
13805     case BIT_AND_EXPR:
13806       /* Bitwise and provides a power of two multiple.  If the mask is
13807          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
13808       if (!integer_pow2p (bottom))
13809         return 0;
13810       /* FALLTHRU */
13811
13812     case MULT_EXPR:
13813       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13814               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13815
13816     case PLUS_EXPR:
13817     case MINUS_EXPR:
13818       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13819               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13820
13821     case LSHIFT_EXPR:
13822       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13823         {
13824           tree op1, t1;
13825
13826           op1 = TREE_OPERAND (top, 1);
13827           /* const_binop may not detect overflow correctly,
13828              so check for it explicitly here.  */
13829           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13830               > TREE_INT_CST_LOW (op1)
13831               && TREE_INT_CST_HIGH (op1) == 0
13832               && 0 != (t1 = fold_convert (type,
13833                                           const_binop (LSHIFT_EXPR,
13834                                                        size_one_node,
13835                                                        op1, 0)))
13836               && !TREE_OVERFLOW (t1))
13837             return multiple_of_p (type, t1, bottom);
13838         }
13839       return 0;
13840
13841     case NOP_EXPR:
13842       /* Can't handle conversions from non-integral or wider integral type.  */
13843       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13844           || (TYPE_PRECISION (type)
13845               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13846         return 0;
13847
13848       /* .. fall through ...  */
13849
13850     case SAVE_EXPR:
13851       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13852
13853     case INTEGER_CST:
13854       if (TREE_CODE (bottom) != INTEGER_CST
13855           || integer_zerop (bottom)
13856           || (TYPE_UNSIGNED (type)
13857               && (tree_int_cst_sgn (top) < 0
13858                   || tree_int_cst_sgn (bottom) < 0)))
13859         return 0;
13860       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13861                                              top, bottom, 0));
13862
13863     default:
13864       return 0;
13865     }
13866 }
13867
13868 /* Return true if CODE or TYPE is known to be non-negative. */
13869
13870 static bool
13871 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
13872 {
13873   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13874       && truth_value_p (code))
13875     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13876        have a signed:1 type (where the value is -1 and 0).  */
13877     return true;
13878   return false;
13879 }
13880
13881 /* Return true if (CODE OP0) is known to be non-negative.  If the return
13882    value is based on the assumption that signed overflow is undefined,
13883    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13884    *STRICT_OVERFLOW_P.  */
13885
13886 bool
13887 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13888                                 bool *strict_overflow_p)
13889 {
13890   if (TYPE_UNSIGNED (type))
13891     return true;
13892
13893   switch (code)
13894     {
13895     case ABS_EXPR:
13896       /* We can't return 1 if flag_wrapv is set because
13897          ABS_EXPR<INT_MIN> = INT_MIN.  */
13898       if (!INTEGRAL_TYPE_P (type))
13899         return true;
13900       if (TYPE_OVERFLOW_UNDEFINED (type))
13901         {
13902           *strict_overflow_p = true;
13903           return true;
13904         }
13905       break;
13906
13907     case NON_LVALUE_EXPR:
13908     case FLOAT_EXPR:
13909     case FIX_TRUNC_EXPR:
13910       return tree_expr_nonnegative_warnv_p (op0,
13911                                             strict_overflow_p);
13912
13913     case NOP_EXPR:
13914       {
13915         tree inner_type = TREE_TYPE (op0);
13916         tree outer_type = type;
13917
13918         if (TREE_CODE (outer_type) == REAL_TYPE)
13919           {
13920             if (TREE_CODE (inner_type) == REAL_TYPE)
13921               return tree_expr_nonnegative_warnv_p (op0,
13922                                                     strict_overflow_p);
13923             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13924               {
13925                 if (TYPE_UNSIGNED (inner_type))
13926                   return true;
13927                 return tree_expr_nonnegative_warnv_p (op0,
13928                                                       strict_overflow_p);
13929               }
13930           }
13931         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13932           {
13933             if (TREE_CODE (inner_type) == REAL_TYPE)
13934               return tree_expr_nonnegative_warnv_p (op0,
13935                                                     strict_overflow_p);
13936             if (TREE_CODE (inner_type) == INTEGER_TYPE)
13937               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13938                       && TYPE_UNSIGNED (inner_type);
13939           }
13940       }
13941       break;
13942
13943     default:
13944       return tree_simple_nonnegative_warnv_p (code, type);
13945     }
13946
13947   /* We don't know sign of `t', so be conservative and return false.  */
13948   return false;
13949 }
13950
13951 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
13952    value is based on the assumption that signed overflow is undefined,
13953    set *STRICT_OVERFLOW_P to true; otherwise, don't change
13954    *STRICT_OVERFLOW_P.  */
13955
13956 bool
13957 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13958                                       tree op1, bool *strict_overflow_p)
13959 {
13960   if (TYPE_UNSIGNED (type))
13961     return true;
13962
13963   switch (code)
13964     {
13965     case POINTER_PLUS_EXPR:
13966     case PLUS_EXPR:
13967       if (FLOAT_TYPE_P (type))
13968         return (tree_expr_nonnegative_warnv_p (op0,
13969                                                strict_overflow_p)
13970                 && tree_expr_nonnegative_warnv_p (op1,
13971                                                   strict_overflow_p));
13972
13973       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13974          both unsigned and at least 2 bits shorter than the result.  */
13975       if (TREE_CODE (type) == INTEGER_TYPE
13976           && TREE_CODE (op0) == NOP_EXPR
13977           && TREE_CODE (op1) == NOP_EXPR)
13978         {
13979           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13980           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13981           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13982               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13983             {
13984               unsigned int prec = MAX (TYPE_PRECISION (inner1),
13985                                        TYPE_PRECISION (inner2)) + 1;
13986               return prec < TYPE_PRECISION (type);
13987             }
13988         }
13989       break;
13990
13991     case MULT_EXPR:
13992       if (FLOAT_TYPE_P (type))
13993         {
13994           /* x * x for floating point x is always non-negative.  */
13995           if (operand_equal_p (op0, op1, 0))
13996             return true;
13997           return (tree_expr_nonnegative_warnv_p (op0,
13998                                                  strict_overflow_p)
13999                   && tree_expr_nonnegative_warnv_p (op1,
14000                                                     strict_overflow_p));
14001         }
14002
14003       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14004          both unsigned and their total bits is shorter than the result.  */
14005       if (TREE_CODE (type) == INTEGER_TYPE
14006           && TREE_CODE (op0) == NOP_EXPR
14007           && TREE_CODE (op1) == NOP_EXPR)
14008         {
14009           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14010           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14011           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14012               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14013             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
14014                    < TYPE_PRECISION (type);
14015         }
14016       return false;
14017
14018     case BIT_AND_EXPR:
14019     case MAX_EXPR:
14020       return (tree_expr_nonnegative_warnv_p (op0,
14021                                              strict_overflow_p)
14022               || tree_expr_nonnegative_warnv_p (op1,
14023                                                 strict_overflow_p));
14024
14025     case BIT_IOR_EXPR:
14026     case BIT_XOR_EXPR:
14027     case MIN_EXPR:
14028     case RDIV_EXPR:
14029     case TRUNC_DIV_EXPR:
14030     case CEIL_DIV_EXPR:
14031     case FLOOR_DIV_EXPR:
14032     case ROUND_DIV_EXPR:
14033       return (tree_expr_nonnegative_warnv_p (op0,
14034                                              strict_overflow_p)
14035               && tree_expr_nonnegative_warnv_p (op1,
14036                                                 strict_overflow_p));
14037
14038     case TRUNC_MOD_EXPR:
14039     case CEIL_MOD_EXPR:
14040     case FLOOR_MOD_EXPR:
14041     case ROUND_MOD_EXPR:
14042       return tree_expr_nonnegative_warnv_p (op0,
14043                                             strict_overflow_p);
14044     default:
14045       return tree_simple_nonnegative_warnv_p (code, type);
14046     }
14047
14048   /* We don't know sign of `t', so be conservative and return false.  */
14049   return false;
14050 }
14051
14052 /* Return true if T is known to be non-negative.  If the return
14053    value is based on the assumption that signed overflow is undefined,
14054    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14055    *STRICT_OVERFLOW_P.  */
14056
14057 bool
14058 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14059 {
14060   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14061     return true;
14062
14063   switch (TREE_CODE (t))
14064     {
14065     case SSA_NAME:
14066       /* Query VRP to see if it has recorded any information about
14067          the range of this object.  */
14068       return ssa_name_nonnegative_p (t);
14069
14070     case INTEGER_CST:
14071       return tree_int_cst_sgn (t) >= 0;
14072
14073     case REAL_CST:
14074       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14075
14076     case FIXED_CST:
14077       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14078
14079     case COND_EXPR:
14080       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14081                                              strict_overflow_p)
14082               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14083                                                 strict_overflow_p));
14084     default:
14085       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14086                                                    TREE_TYPE (t));
14087     }
14088   /* We don't know sign of `t', so be conservative and return false.  */
14089   return false;
14090 }
14091
14092 /* Return true if T is known to be non-negative.  If the return
14093    value is based on the assumption that signed overflow is undefined,
14094    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14095    *STRICT_OVERFLOW_P.  */
14096
14097 bool
14098 tree_call_nonnegative_warnv_p (enum tree_code code,  tree type, tree fndecl,
14099                                tree arg0, tree arg1, bool *strict_overflow_p)
14100 {
14101   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14102     switch (DECL_FUNCTION_CODE (fndecl))
14103       {
14104         CASE_FLT_FN (BUILT_IN_ACOS):
14105         CASE_FLT_FN (BUILT_IN_ACOSH):
14106         CASE_FLT_FN (BUILT_IN_CABS):
14107         CASE_FLT_FN (BUILT_IN_COSH):
14108         CASE_FLT_FN (BUILT_IN_ERFC):
14109         CASE_FLT_FN (BUILT_IN_EXP):
14110         CASE_FLT_FN (BUILT_IN_EXP10):
14111         CASE_FLT_FN (BUILT_IN_EXP2):
14112         CASE_FLT_FN (BUILT_IN_FABS):
14113         CASE_FLT_FN (BUILT_IN_FDIM):
14114         CASE_FLT_FN (BUILT_IN_HYPOT):
14115         CASE_FLT_FN (BUILT_IN_POW10):
14116         CASE_INT_FN (BUILT_IN_FFS):
14117         CASE_INT_FN (BUILT_IN_PARITY):
14118         CASE_INT_FN (BUILT_IN_POPCOUNT):
14119       case BUILT_IN_BSWAP32:
14120       case BUILT_IN_BSWAP64:
14121         /* Always true.  */
14122         return true;
14123
14124         CASE_FLT_FN (BUILT_IN_SQRT):
14125         /* sqrt(-0.0) is -0.0.  */
14126         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14127           return true;
14128         return tree_expr_nonnegative_warnv_p (arg0,
14129                                               strict_overflow_p);
14130
14131         CASE_FLT_FN (BUILT_IN_ASINH):
14132         CASE_FLT_FN (BUILT_IN_ATAN):
14133         CASE_FLT_FN (BUILT_IN_ATANH):
14134         CASE_FLT_FN (BUILT_IN_CBRT):
14135         CASE_FLT_FN (BUILT_IN_CEIL):
14136         CASE_FLT_FN (BUILT_IN_ERF):
14137         CASE_FLT_FN (BUILT_IN_EXPM1):
14138         CASE_FLT_FN (BUILT_IN_FLOOR):
14139         CASE_FLT_FN (BUILT_IN_FMOD):
14140         CASE_FLT_FN (BUILT_IN_FREXP):
14141         CASE_FLT_FN (BUILT_IN_LCEIL):
14142         CASE_FLT_FN (BUILT_IN_LDEXP):
14143         CASE_FLT_FN (BUILT_IN_LFLOOR):
14144         CASE_FLT_FN (BUILT_IN_LLCEIL):
14145         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14146         CASE_FLT_FN (BUILT_IN_LLRINT):
14147         CASE_FLT_FN (BUILT_IN_LLROUND):
14148         CASE_FLT_FN (BUILT_IN_LRINT):
14149         CASE_FLT_FN (BUILT_IN_LROUND):
14150         CASE_FLT_FN (BUILT_IN_MODF):
14151         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14152         CASE_FLT_FN (BUILT_IN_RINT):
14153         CASE_FLT_FN (BUILT_IN_ROUND):
14154         CASE_FLT_FN (BUILT_IN_SCALB):
14155         CASE_FLT_FN (BUILT_IN_SCALBLN):
14156         CASE_FLT_FN (BUILT_IN_SCALBN):
14157         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14158         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14159         CASE_FLT_FN (BUILT_IN_SINH):
14160         CASE_FLT_FN (BUILT_IN_TANH):
14161         CASE_FLT_FN (BUILT_IN_TRUNC):
14162         /* True if the 1st argument is nonnegative.  */
14163         return tree_expr_nonnegative_warnv_p (arg0,
14164                                               strict_overflow_p);
14165
14166         CASE_FLT_FN (BUILT_IN_FMAX):
14167         /* True if the 1st OR 2nd arguments are nonnegative.  */
14168         return (tree_expr_nonnegative_warnv_p (arg0,
14169                                                strict_overflow_p)
14170                 || (tree_expr_nonnegative_warnv_p (arg1,
14171                                                    strict_overflow_p)));
14172
14173         CASE_FLT_FN (BUILT_IN_FMIN):
14174         /* True if the 1st AND 2nd arguments are nonnegative.  */
14175         return (tree_expr_nonnegative_warnv_p (arg0,
14176                                                strict_overflow_p)
14177                 && (tree_expr_nonnegative_warnv_p (arg1,
14178                                                    strict_overflow_p)));
14179
14180         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14181         /* True if the 2nd argument is nonnegative.  */
14182         return tree_expr_nonnegative_warnv_p (arg1,
14183                                               strict_overflow_p);
14184
14185         CASE_FLT_FN (BUILT_IN_POWI):
14186         /* True if the 1st argument is nonnegative or the second
14187            argument is an even integer.  */
14188         if (TREE_CODE (arg1) == INTEGER_CST
14189             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14190           return true;
14191         return tree_expr_nonnegative_warnv_p (arg0,
14192                                               strict_overflow_p);
14193
14194         CASE_FLT_FN (BUILT_IN_POW):
14195         /* True if the 1st argument is nonnegative or the second
14196            argument is an even integer valued real.  */
14197         if (TREE_CODE (arg1) == REAL_CST)
14198           {
14199             REAL_VALUE_TYPE c;
14200             HOST_WIDE_INT n;
14201
14202             c = TREE_REAL_CST (arg1);
14203             n = real_to_integer (&c);
14204             if ((n & 1) == 0)
14205               {
14206                 REAL_VALUE_TYPE cint;
14207                 real_from_integer (&cint, VOIDmode, n,
14208                                    n < 0 ? -1 : 0, 0);
14209                 if (real_identical (&c, &cint))
14210                   return true;
14211               }
14212           }
14213         return tree_expr_nonnegative_warnv_p (arg0,
14214                                               strict_overflow_p);
14215
14216       default:
14217         break;
14218       }
14219   return tree_simple_nonnegative_warnv_p (code,
14220                                           type);
14221 }
14222
14223 /* Return true if T is known to be non-negative.  If the return
14224    value is based on the assumption that signed overflow is undefined,
14225    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14226    *STRICT_OVERFLOW_P.  */
14227
14228 bool
14229 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14230 {
14231   enum tree_code code = TREE_CODE (t);
14232   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14233     return true;
14234
14235   switch (code)
14236     {
14237     case TARGET_EXPR:
14238       {
14239         tree temp = TARGET_EXPR_SLOT (t);
14240         t = TARGET_EXPR_INITIAL (t);
14241
14242         /* If the initializer is non-void, then it's a normal expression
14243            that will be assigned to the slot.  */
14244         if (!VOID_TYPE_P (t))
14245           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14246
14247         /* Otherwise, the initializer sets the slot in some way.  One common
14248            way is an assignment statement at the end of the initializer.  */
14249         while (1)
14250           {
14251             if (TREE_CODE (t) == BIND_EXPR)
14252               t = expr_last (BIND_EXPR_BODY (t));
14253             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14254                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14255               t = expr_last (TREE_OPERAND (t, 0));
14256             else if (TREE_CODE (t) == STATEMENT_LIST)
14257               t = expr_last (t);
14258             else
14259               break;
14260           }
14261         if ((TREE_CODE (t) == MODIFY_EXPR
14262              || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
14263             && GENERIC_TREE_OPERAND (t, 0) == temp)
14264           return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14265                                                 strict_overflow_p);
14266
14267         return false;
14268       }
14269
14270     case CALL_EXPR:
14271       {
14272         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14273         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14274
14275         return tree_call_nonnegative_warnv_p (TREE_CODE (t),
14276                                               TREE_TYPE (t),
14277                                               get_callee_fndecl (t),
14278                                               arg0,
14279                                               arg1,
14280                                               strict_overflow_p);
14281       }
14282     case COMPOUND_EXPR:
14283     case MODIFY_EXPR:
14284     case GIMPLE_MODIFY_STMT:
14285       return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14286                                             strict_overflow_p);
14287     case BIND_EXPR:
14288       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14289                                             strict_overflow_p);
14290     case SAVE_EXPR:
14291       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14292                                             strict_overflow_p);
14293
14294     default:
14295       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14296                                                    TREE_TYPE (t));
14297     }
14298
14299   /* We don't know sign of `t', so be conservative and return false.  */
14300   return false;
14301 }
14302
14303 /* Return true if T is known to be non-negative.  If the return
14304    value is based on the assumption that signed overflow is undefined,
14305    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14306    *STRICT_OVERFLOW_P.  */
14307
14308 bool
14309 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14310 {
14311   enum tree_code code;
14312   if (t == error_mark_node)
14313     return false;
14314
14315   code = TREE_CODE (t);
14316   switch (TREE_CODE_CLASS (code))
14317     {
14318     case tcc_binary:
14319     case tcc_comparison:
14320       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14321                                               TREE_TYPE (t),
14322                                               TREE_OPERAND (t, 0),
14323                                               TREE_OPERAND (t, 1),
14324                                               strict_overflow_p);
14325
14326     case tcc_unary:
14327       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14328                                              TREE_TYPE (t),
14329                                              TREE_OPERAND (t, 0),
14330                                              strict_overflow_p);
14331
14332     case tcc_constant:
14333     case tcc_declaration:
14334     case tcc_reference:
14335       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14336
14337     default:
14338       break;
14339     }
14340
14341   switch (code)
14342     {
14343     case TRUTH_AND_EXPR:
14344     case TRUTH_OR_EXPR:
14345     case TRUTH_XOR_EXPR:
14346       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14347                                               TREE_TYPE (t),
14348                                               TREE_OPERAND (t, 0),
14349                                               TREE_OPERAND (t, 1),
14350                                               strict_overflow_p);
14351     case TRUTH_NOT_EXPR:
14352       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14353                                              TREE_TYPE (t),
14354                                              TREE_OPERAND (t, 0),
14355                                              strict_overflow_p);
14356
14357     case COND_EXPR:
14358     case CONSTRUCTOR:
14359     case OBJ_TYPE_REF:
14360     case ASSERT_EXPR:
14361     case ADDR_EXPR:
14362     case WITH_SIZE_EXPR:
14363     case EXC_PTR_EXPR:
14364     case SSA_NAME:
14365     case FILTER_EXPR:
14366       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14367
14368     default:
14369       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14370     }
14371 }
14372
14373 /* Return true if `t' is known to be non-negative.  Handle warnings
14374    about undefined signed overflow.  */
14375
14376 bool
14377 tree_expr_nonnegative_p (tree t)
14378 {
14379   bool ret, strict_overflow_p;
14380
14381   strict_overflow_p = false;
14382   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14383   if (strict_overflow_p)
14384     fold_overflow_warning (("assuming signed overflow does not occur when "
14385                             "determining that expression is always "
14386                             "non-negative"),
14387                            WARN_STRICT_OVERFLOW_MISC);
14388   return ret;
14389 }
14390
14391
14392 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14393    For floating point we further ensure that T is not denormal.
14394    Similar logic is present in nonzero_address in rtlanal.h.
14395
14396    If the return value is based on the assumption that signed overflow
14397    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14398    change *STRICT_OVERFLOW_P.  */
14399
14400 bool
14401 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14402                                  bool *strict_overflow_p)
14403 {
14404   switch (code)
14405     {
14406     case ABS_EXPR:
14407       return tree_expr_nonzero_warnv_p (op0,
14408                                         strict_overflow_p);
14409
14410     case NOP_EXPR:
14411       {
14412         tree inner_type = TREE_TYPE (op0);
14413         tree outer_type = type;
14414
14415         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14416                 && tree_expr_nonzero_warnv_p (op0,
14417                                               strict_overflow_p));
14418       }
14419       break;
14420
14421     case NON_LVALUE_EXPR:
14422       return tree_expr_nonzero_warnv_p (op0,
14423                                         strict_overflow_p);
14424
14425     default:
14426       break;
14427   }
14428
14429   return false;
14430 }
14431
14432 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14433    For floating point we further ensure that T is not denormal.
14434    Similar logic is present in nonzero_address in rtlanal.h.
14435
14436    If the return value is based on the assumption that signed overflow
14437    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14438    change *STRICT_OVERFLOW_P.  */
14439
14440 bool
14441 tree_binary_nonzero_warnv_p (enum tree_code code,
14442                              tree type,
14443                              tree op0,
14444                              tree op1, bool *strict_overflow_p)
14445 {
14446   bool sub_strict_overflow_p;
14447   switch (code)
14448     {
14449     case POINTER_PLUS_EXPR:
14450     case PLUS_EXPR:
14451       if (TYPE_OVERFLOW_UNDEFINED (type))
14452         {
14453           /* With the presence of negative values it is hard
14454              to say something.  */
14455           sub_strict_overflow_p = false;
14456           if (!tree_expr_nonnegative_warnv_p (op0,
14457                                               &sub_strict_overflow_p)
14458               || !tree_expr_nonnegative_warnv_p (op1,
14459                                                  &sub_strict_overflow_p))
14460             return false;
14461           /* One of operands must be positive and the other non-negative.  */
14462           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14463              overflows, on a twos-complement machine the sum of two
14464              nonnegative numbers can never be zero.  */
14465           return (tree_expr_nonzero_warnv_p (op0,
14466                                              strict_overflow_p)
14467                   || tree_expr_nonzero_warnv_p (op1,
14468                                                 strict_overflow_p));
14469         }
14470       break;
14471
14472     case MULT_EXPR:
14473       if (TYPE_OVERFLOW_UNDEFINED (type))
14474         {
14475           if (tree_expr_nonzero_warnv_p (op0,
14476                                          strict_overflow_p)
14477               && tree_expr_nonzero_warnv_p (op1,
14478                                             strict_overflow_p))
14479             {
14480               *strict_overflow_p = true;
14481               return true;
14482             }
14483         }
14484       break;
14485
14486     case MIN_EXPR:
14487       sub_strict_overflow_p = false;
14488       if (tree_expr_nonzero_warnv_p (op0,
14489                                      &sub_strict_overflow_p)
14490           && tree_expr_nonzero_warnv_p (op1,
14491                                         &sub_strict_overflow_p))
14492         {
14493           if (sub_strict_overflow_p)
14494             *strict_overflow_p = true;
14495         }
14496       break;
14497
14498     case MAX_EXPR:
14499       sub_strict_overflow_p = false;
14500       if (tree_expr_nonzero_warnv_p (op0,
14501                                      &sub_strict_overflow_p))
14502         {
14503           if (sub_strict_overflow_p)
14504             *strict_overflow_p = true;
14505
14506           /* When both operands are nonzero, then MAX must be too.  */
14507           if (tree_expr_nonzero_warnv_p (op1,
14508                                          strict_overflow_p))
14509             return true;
14510
14511           /* MAX where operand 0 is positive is positive.  */
14512           return tree_expr_nonnegative_warnv_p (op0,
14513                                                strict_overflow_p);
14514         }
14515       /* MAX where operand 1 is positive is positive.  */
14516       else if (tree_expr_nonzero_warnv_p (op1,
14517                                           &sub_strict_overflow_p)
14518                && tree_expr_nonnegative_warnv_p (op1,
14519                                                  &sub_strict_overflow_p))
14520         {
14521           if (sub_strict_overflow_p)
14522             *strict_overflow_p = true;
14523           return true;
14524         }
14525       break;
14526
14527     case BIT_IOR_EXPR:
14528       return (tree_expr_nonzero_warnv_p (op1,
14529                                          strict_overflow_p)
14530               || tree_expr_nonzero_warnv_p (op0,
14531                                             strict_overflow_p));
14532
14533     default:
14534       break;
14535   }
14536
14537   return false;
14538 }
14539
14540 /* Return true when T is an address and is known to be nonzero.
14541    For floating point we further ensure that T is not denormal.
14542    Similar logic is present in nonzero_address in rtlanal.h.
14543
14544    If the return value is based on the assumption that signed overflow
14545    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14546    change *STRICT_OVERFLOW_P.  */
14547
14548 bool
14549 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14550 {
14551   bool sub_strict_overflow_p;
14552   switch (TREE_CODE (t))
14553     {
14554     case SSA_NAME:
14555       /* Query VRP to see if it has recorded any information about
14556          the range of this object.  */
14557       return ssa_name_nonzero_p (t);
14558
14559     case INTEGER_CST:
14560       return !integer_zerop (t);
14561
14562     case ADDR_EXPR:
14563       {
14564         tree base = get_base_address (TREE_OPERAND (t, 0));
14565
14566         if (!base)
14567           return false;
14568
14569         /* Weak declarations may link to NULL.  */
14570         if (VAR_OR_FUNCTION_DECL_P (base))
14571           return !DECL_WEAK (base);
14572
14573         /* Constants are never weak.  */
14574         if (CONSTANT_CLASS_P (base))
14575           return true;
14576
14577         return false;
14578       }
14579
14580     case COND_EXPR:
14581       sub_strict_overflow_p = false;
14582       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14583                                      &sub_strict_overflow_p)
14584           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14585                                         &sub_strict_overflow_p))
14586         {
14587           if (sub_strict_overflow_p)
14588             *strict_overflow_p = true;
14589           return true;
14590         }
14591       break;
14592
14593     default:
14594       break;
14595     }
14596   return false;
14597 }
14598
14599 /* Return true when T is an address and is known to be nonzero.
14600    For floating point we further ensure that T is not denormal.
14601    Similar logic is present in nonzero_address in rtlanal.h.
14602
14603    If the return value is based on the assumption that signed overflow
14604    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14605    change *STRICT_OVERFLOW_P.  */
14606
14607 bool
14608 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14609 {
14610   tree type = TREE_TYPE (t);
14611   enum tree_code code;
14612
14613   /* Doing something useful for floating point would need more work.  */
14614   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14615     return false;
14616
14617   code = TREE_CODE (t);
14618   switch (TREE_CODE_CLASS (code))
14619     {
14620     case tcc_unary:
14621       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14622                                               strict_overflow_p);
14623     case tcc_binary:
14624     case tcc_comparison:
14625       return tree_binary_nonzero_warnv_p (code, type,
14626                                                TREE_OPERAND (t, 0),
14627                                                TREE_OPERAND (t, 1),
14628                                                strict_overflow_p);
14629     case tcc_constant:
14630     case tcc_declaration:
14631     case tcc_reference:
14632       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14633
14634     default:
14635       break;
14636     }
14637
14638   switch (code)
14639     {
14640     case TRUTH_NOT_EXPR:
14641       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14642                                               strict_overflow_p);
14643
14644     case TRUTH_AND_EXPR:
14645     case TRUTH_OR_EXPR:
14646     case TRUTH_XOR_EXPR:
14647       return tree_binary_nonzero_warnv_p (code, type,
14648                                                TREE_OPERAND (t, 0),
14649                                                TREE_OPERAND (t, 1),
14650                                                strict_overflow_p);
14651
14652     case COND_EXPR:
14653     case CONSTRUCTOR:
14654     case OBJ_TYPE_REF:
14655     case ASSERT_EXPR:
14656     case ADDR_EXPR:
14657     case WITH_SIZE_EXPR:
14658     case EXC_PTR_EXPR:
14659     case SSA_NAME:
14660     case FILTER_EXPR:
14661       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14662
14663     case COMPOUND_EXPR:
14664     case MODIFY_EXPR:
14665     case GIMPLE_MODIFY_STMT:
14666     case BIND_EXPR:
14667       return tree_expr_nonzero_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14668                                         strict_overflow_p);
14669
14670     case SAVE_EXPR:
14671       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14672                                         strict_overflow_p);
14673
14674     case CALL_EXPR:
14675       return alloca_call_p (t);
14676
14677     default:
14678       break;
14679     }
14680   return false;
14681 }
14682
14683 /* Return true when T is an address and is known to be nonzero.
14684    Handle warnings about undefined signed overflow.  */
14685
14686 bool
14687 tree_expr_nonzero_p (tree t)
14688 {
14689   bool ret, strict_overflow_p;
14690
14691   strict_overflow_p = false;
14692   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14693   if (strict_overflow_p)
14694     fold_overflow_warning (("assuming signed overflow does not occur when "
14695                             "determining that expression is always "
14696                             "non-zero"),
14697                            WARN_STRICT_OVERFLOW_MISC);
14698   return ret;
14699 }
14700
14701 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14702    attempt to fold the expression to a constant without modifying TYPE,
14703    OP0 or OP1.
14704
14705    If the expression could be simplified to a constant, then return
14706    the constant.  If the expression would not be simplified to a
14707    constant, then return NULL_TREE.  */
14708
14709 tree
14710 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14711 {
14712   tree tem = fold_binary (code, type, op0, op1);
14713   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14714 }
14715
14716 /* Given the components of a unary expression CODE, TYPE and OP0,
14717    attempt to fold the expression to a constant without modifying
14718    TYPE or OP0.
14719
14720    If the expression could be simplified to a constant, then return
14721    the constant.  If the expression would not be simplified to a
14722    constant, then return NULL_TREE.  */
14723
14724 tree
14725 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14726 {
14727   tree tem = fold_unary (code, type, op0);
14728   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14729 }
14730
14731 /* If EXP represents referencing an element in a constant string
14732    (either via pointer arithmetic or array indexing), return the
14733    tree representing the value accessed, otherwise return NULL.  */
14734
14735 tree
14736 fold_read_from_constant_string (tree exp)
14737 {
14738   if ((TREE_CODE (exp) == INDIRECT_REF
14739        || TREE_CODE (exp) == ARRAY_REF)
14740       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14741     {
14742       tree exp1 = TREE_OPERAND (exp, 0);
14743       tree index;
14744       tree string;
14745
14746       if (TREE_CODE (exp) == INDIRECT_REF)
14747         string = string_constant (exp1, &index);
14748       else
14749         {
14750           tree low_bound = array_ref_low_bound (exp);
14751           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14752
14753           /* Optimize the special-case of a zero lower bound.
14754
14755              We convert the low_bound to sizetype to avoid some problems
14756              with constant folding.  (E.g. suppose the lower bound is 1,
14757              and its mode is QI.  Without the conversion,l (ARRAY
14758              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14759              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
14760           if (! integer_zerop (low_bound))
14761             index = size_diffop (index, fold_convert (sizetype, low_bound));
14762
14763           string = exp1;
14764         }
14765
14766       if (string
14767           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14768           && TREE_CODE (string) == STRING_CST
14769           && TREE_CODE (index) == INTEGER_CST
14770           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14771           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14772               == MODE_INT)
14773           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14774         return build_int_cst_type (TREE_TYPE (exp),
14775                                    (TREE_STRING_POINTER (string)
14776                                     [TREE_INT_CST_LOW (index)]));
14777     }
14778   return NULL;
14779 }
14780
14781 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14782    an integer constant, real, or fixed-point constant.
14783
14784    TYPE is the type of the result.  */
14785
14786 static tree
14787 fold_negate_const (tree arg0, tree type)
14788 {
14789   tree t = NULL_TREE;
14790
14791   switch (TREE_CODE (arg0))
14792     {
14793     case INTEGER_CST:
14794       {
14795         unsigned HOST_WIDE_INT low;
14796         HOST_WIDE_INT high;
14797         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14798                                    TREE_INT_CST_HIGH (arg0),
14799                                    &low, &high);
14800         t = force_fit_type_double (type, low, high, 1,
14801                                    (overflow | TREE_OVERFLOW (arg0))
14802                                    && !TYPE_UNSIGNED (type));
14803         break;
14804       }
14805
14806     case REAL_CST:
14807       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14808       break;
14809
14810     case FIXED_CST:
14811       {
14812         FIXED_VALUE_TYPE f;
14813         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14814                                             &(TREE_FIXED_CST (arg0)), NULL,
14815                                             TYPE_SATURATING (type));
14816         t = build_fixed (type, f);
14817         /* Propagate overflow flags.  */
14818         if (overflow_p | TREE_OVERFLOW (arg0))
14819           {
14820             TREE_OVERFLOW (t) = 1;
14821             TREE_CONSTANT_OVERFLOW (t) = 1;
14822           }
14823         else if (TREE_CONSTANT_OVERFLOW (arg0))
14824           TREE_CONSTANT_OVERFLOW (t) = 1;
14825         break;
14826       }
14827
14828     default:
14829       gcc_unreachable ();
14830     }
14831
14832   return t;
14833 }
14834
14835 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14836    an integer constant or real constant.
14837
14838    TYPE is the type of the result.  */
14839
14840 tree
14841 fold_abs_const (tree arg0, tree type)
14842 {
14843   tree t = NULL_TREE;
14844
14845   switch (TREE_CODE (arg0))
14846     {
14847     case INTEGER_CST:
14848       /* If the value is unsigned, then the absolute value is
14849          the same as the ordinary value.  */
14850       if (TYPE_UNSIGNED (type))
14851         t = arg0;
14852       /* Similarly, if the value is non-negative.  */
14853       else if (INT_CST_LT (integer_minus_one_node, arg0))
14854         t = arg0;
14855       /* If the value is negative, then the absolute value is
14856          its negation.  */
14857       else
14858         {
14859           unsigned HOST_WIDE_INT low;
14860           HOST_WIDE_INT high;
14861           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14862                                      TREE_INT_CST_HIGH (arg0),
14863                                      &low, &high);
14864           t = force_fit_type_double (type, low, high, -1,
14865                                      overflow | TREE_OVERFLOW (arg0));
14866         }
14867       break;
14868
14869     case REAL_CST:
14870       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14871         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14872       else
14873         t =  arg0;
14874       break;
14875
14876     default:
14877       gcc_unreachable ();
14878     }
14879
14880   return t;
14881 }
14882
14883 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14884    constant.  TYPE is the type of the result.  */
14885
14886 static tree
14887 fold_not_const (tree arg0, tree type)
14888 {
14889   tree t = NULL_TREE;
14890
14891   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14892
14893   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14894                              ~TREE_INT_CST_HIGH (arg0), 0,
14895                              TREE_OVERFLOW (arg0));
14896
14897   return t;
14898 }
14899
14900 /* Given CODE, a relational operator, the target type, TYPE and two
14901    constant operands OP0 and OP1, return the result of the
14902    relational operation.  If the result is not a compile time
14903    constant, then return NULL_TREE.  */
14904
14905 static tree
14906 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14907 {
14908   int result, invert;
14909
14910   /* From here on, the only cases we handle are when the result is
14911      known to be a constant.  */
14912
14913   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14914     {
14915       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14916       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14917
14918       /* Handle the cases where either operand is a NaN.  */
14919       if (real_isnan (c0) || real_isnan (c1))
14920         {
14921           switch (code)
14922             {
14923             case EQ_EXPR:
14924             case ORDERED_EXPR:
14925               result = 0;
14926               break;
14927
14928             case NE_EXPR:
14929             case UNORDERED_EXPR:
14930             case UNLT_EXPR:
14931             case UNLE_EXPR:
14932             case UNGT_EXPR:
14933             case UNGE_EXPR:
14934             case UNEQ_EXPR:
14935               result = 1;
14936               break;
14937
14938             case LT_EXPR:
14939             case LE_EXPR:
14940             case GT_EXPR:
14941             case GE_EXPR:
14942             case LTGT_EXPR:
14943               if (flag_trapping_math)
14944                 return NULL_TREE;
14945               result = 0;
14946               break;
14947
14948             default:
14949               gcc_unreachable ();
14950             }
14951
14952           return constant_boolean_node (result, type);
14953         }
14954
14955       return constant_boolean_node (real_compare (code, c0, c1), type);
14956     }
14957
14958   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14959     {
14960       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14961       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14962       return constant_boolean_node (fixed_compare (code, c0, c1), type);
14963     }
14964
14965   /* Handle equality/inequality of complex constants.  */
14966   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14967     {
14968       tree rcond = fold_relational_const (code, type,
14969                                           TREE_REALPART (op0),
14970                                           TREE_REALPART (op1));
14971       tree icond = fold_relational_const (code, type,
14972                                           TREE_IMAGPART (op0),
14973                                           TREE_IMAGPART (op1));
14974       if (code == EQ_EXPR)
14975         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14976       else if (code == NE_EXPR)
14977         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14978       else
14979         return NULL_TREE;
14980     }
14981
14982   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14983
14984      To compute GT, swap the arguments and do LT.
14985      To compute GE, do LT and invert the result.
14986      To compute LE, swap the arguments, do LT and invert the result.
14987      To compute NE, do EQ and invert the result.
14988
14989      Therefore, the code below must handle only EQ and LT.  */
14990
14991   if (code == LE_EXPR || code == GT_EXPR)
14992     {
14993       tree tem = op0;
14994       op0 = op1;
14995       op1 = tem;
14996       code = swap_tree_comparison (code);
14997     }
14998
14999   /* Note that it is safe to invert for real values here because we
15000      have already handled the one case that it matters.  */
15001
15002   invert = 0;
15003   if (code == NE_EXPR || code == GE_EXPR)
15004     {
15005       invert = 1;
15006       code = invert_tree_comparison (code, false);
15007     }
15008
15009   /* Compute a result for LT or EQ if args permit;
15010      Otherwise return T.  */
15011   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15012     {
15013       if (code == EQ_EXPR)
15014         result = tree_int_cst_equal (op0, op1);
15015       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15016         result = INT_CST_LT_UNSIGNED (op0, op1);
15017       else
15018         result = INT_CST_LT (op0, op1);
15019     }
15020   else
15021     return NULL_TREE;
15022
15023   if (invert)
15024     result ^= 1;
15025   return constant_boolean_node (result, type);
15026 }
15027
15028 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15029    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15030    itself.  */
15031
15032 tree
15033 fold_build_cleanup_point_expr (tree type, tree expr)
15034 {
15035   /* If the expression does not have side effects then we don't have to wrap
15036      it with a cleanup point expression.  */
15037   if (!TREE_SIDE_EFFECTS (expr))
15038     return expr;
15039
15040   /* If the expression is a return, check to see if the expression inside the
15041      return has no side effects or the right hand side of the modify expression
15042      inside the return. If either don't have side effects set we don't need to
15043      wrap the expression in a cleanup point expression.  Note we don't check the
15044      left hand side of the modify because it should always be a return decl.  */
15045   if (TREE_CODE (expr) == RETURN_EXPR)
15046     {
15047       tree op = TREE_OPERAND (expr, 0);
15048       if (!op || !TREE_SIDE_EFFECTS (op))
15049         return expr;
15050       op = TREE_OPERAND (op, 1);
15051       if (!TREE_SIDE_EFFECTS (op))
15052         return expr;
15053     }
15054   
15055   return build1 (CLEANUP_POINT_EXPR, type, expr);
15056 }
15057
15058 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15059    of an indirection through OP0, or NULL_TREE if no simplification is
15060    possible.  */
15061
15062 tree
15063 fold_indirect_ref_1 (tree type, tree op0)
15064 {
15065   tree sub = op0;
15066   tree subtype;
15067
15068   STRIP_NOPS (sub);
15069   subtype = TREE_TYPE (sub);
15070   if (!POINTER_TYPE_P (subtype))
15071     return NULL_TREE;
15072
15073   if (TREE_CODE (sub) == ADDR_EXPR)
15074     {
15075       tree op = TREE_OPERAND (sub, 0);
15076       tree optype = TREE_TYPE (op);
15077       /* *&CONST_DECL -> to the value of the const decl.  */
15078       if (TREE_CODE (op) == CONST_DECL)
15079         return DECL_INITIAL (op);
15080       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15081       if (type == optype)
15082         {
15083           tree fop = fold_read_from_constant_string (op);
15084           if (fop)
15085             return fop;
15086           else
15087             return op;
15088         }
15089       /* *(foo *)&fooarray => fooarray[0] */
15090       else if (TREE_CODE (optype) == ARRAY_TYPE
15091                && type == TREE_TYPE (optype))
15092         {
15093           tree type_domain = TYPE_DOMAIN (optype);
15094           tree min_val = size_zero_node;
15095           if (type_domain && TYPE_MIN_VALUE (type_domain))
15096             min_val = TYPE_MIN_VALUE (type_domain);
15097           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15098         }
15099       /* *(foo *)&complexfoo => __real__ complexfoo */
15100       else if (TREE_CODE (optype) == COMPLEX_TYPE
15101                && type == TREE_TYPE (optype))
15102         return fold_build1 (REALPART_EXPR, type, op);
15103       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15104       else if (TREE_CODE (optype) == VECTOR_TYPE
15105                && type == TREE_TYPE (optype))
15106         {
15107           tree part_width = TYPE_SIZE (type);
15108           tree index = bitsize_int (0);
15109           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15110         }
15111     }
15112
15113   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15114   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15115       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15116     { 
15117       tree op00 = TREE_OPERAND (sub, 0);
15118       tree op01 = TREE_OPERAND (sub, 1);
15119       tree op00type;
15120       
15121       STRIP_NOPS (op00);
15122       op00type = TREE_TYPE (op00);
15123       if (TREE_CODE (op00) == ADDR_EXPR
15124           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15125           && type == TREE_TYPE (TREE_TYPE (op00type)))
15126         { 
15127           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15128           tree part_width = TYPE_SIZE (type);
15129           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15130           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15131           tree index = bitsize_int (indexi);
15132
15133           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15134             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15135                                 part_width, index);
15136         
15137         }
15138     }
15139
15140
15141   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15142   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15143       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15144     {
15145       tree op00 = TREE_OPERAND (sub, 0);
15146       tree op01 = TREE_OPERAND (sub, 1);
15147       tree op00type;
15148
15149       STRIP_NOPS (op00);
15150       op00type = TREE_TYPE (op00);
15151       if (TREE_CODE (op00) == ADDR_EXPR
15152           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15153           && type == TREE_TYPE (TREE_TYPE (op00type)))
15154         {
15155           tree size = TYPE_SIZE_UNIT (type);
15156           if (tree_int_cst_equal (size, op01))
15157             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15158         }
15159     }
15160   
15161   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15162   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15163       && type == TREE_TYPE (TREE_TYPE (subtype)))
15164     {
15165       tree type_domain;
15166       tree min_val = size_zero_node;
15167       sub = build_fold_indirect_ref (sub);
15168       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15169       if (type_domain && TYPE_MIN_VALUE (type_domain))
15170         min_val = TYPE_MIN_VALUE (type_domain);
15171       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15172     }
15173
15174   return NULL_TREE;
15175 }
15176
15177 /* Builds an expression for an indirection through T, simplifying some
15178    cases.  */
15179
15180 tree
15181 build_fold_indirect_ref (tree t)
15182 {
15183   tree type = TREE_TYPE (TREE_TYPE (t));
15184   tree sub = fold_indirect_ref_1 (type, t);
15185
15186   if (sub)
15187     return sub;
15188   else
15189     return build1 (INDIRECT_REF, type, t);
15190 }
15191
15192 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15193
15194 tree
15195 fold_indirect_ref (tree t)
15196 {
15197   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15198
15199   if (sub)
15200     return sub;
15201   else
15202     return t;
15203 }
15204
15205 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15206    whose result is ignored.  The type of the returned tree need not be
15207    the same as the original expression.  */
15208
15209 tree
15210 fold_ignored_result (tree t)
15211 {
15212   if (!TREE_SIDE_EFFECTS (t))
15213     return integer_zero_node;
15214
15215   for (;;)
15216     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15217       {
15218       case tcc_unary:
15219         t = TREE_OPERAND (t, 0);
15220         break;
15221
15222       case tcc_binary:
15223       case tcc_comparison:
15224         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15225           t = TREE_OPERAND (t, 0);
15226         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15227           t = TREE_OPERAND (t, 1);
15228         else
15229           return t;
15230         break;
15231
15232       case tcc_expression:
15233         switch (TREE_CODE (t))
15234           {
15235           case COMPOUND_EXPR:
15236             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15237               return t;
15238             t = TREE_OPERAND (t, 0);
15239             break;
15240
15241           case COND_EXPR:
15242             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15243                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15244               return t;
15245             t = TREE_OPERAND (t, 0);
15246             break;
15247
15248           default:
15249             return t;
15250           }
15251         break;
15252
15253       default:
15254         return t;
15255       }
15256 }
15257
15258 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15259    This can only be applied to objects of a sizetype.  */
15260
15261 tree
15262 round_up (tree value, int divisor)
15263 {
15264   tree div = NULL_TREE;
15265
15266   gcc_assert (divisor > 0);
15267   if (divisor == 1)
15268     return value;
15269
15270   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15271      have to do anything.  Only do this when we are not given a const,
15272      because in that case, this check is more expensive than just
15273      doing it.  */
15274   if (TREE_CODE (value) != INTEGER_CST)
15275     {
15276       div = build_int_cst (TREE_TYPE (value), divisor);
15277
15278       if (multiple_of_p (TREE_TYPE (value), value, div))
15279         return value;
15280     }
15281
15282   /* If divisor is a power of two, simplify this to bit manipulation.  */
15283   if (divisor == (divisor & -divisor))
15284     {
15285       if (TREE_CODE (value) == INTEGER_CST)
15286         {
15287           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15288           unsigned HOST_WIDE_INT high;
15289           bool overflow_p;
15290
15291           if ((low & (divisor - 1)) == 0)
15292             return value;
15293
15294           overflow_p = TREE_OVERFLOW (value);
15295           high = TREE_INT_CST_HIGH (value);
15296           low &= ~(divisor - 1);
15297           low += divisor;
15298           if (low == 0)
15299             {
15300               high++;
15301               if (high == 0)
15302                 overflow_p = true;
15303             }
15304
15305           return force_fit_type_double (TREE_TYPE (value), low, high,
15306                                         -1, overflow_p);
15307         }
15308       else
15309         {
15310           tree t;
15311
15312           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15313           value = size_binop (PLUS_EXPR, value, t);
15314           t = build_int_cst (TREE_TYPE (value), -divisor);
15315           value = size_binop (BIT_AND_EXPR, value, t);
15316         }
15317     }
15318   else
15319     {
15320       if (!div)
15321         div = build_int_cst (TREE_TYPE (value), divisor);
15322       value = size_binop (CEIL_DIV_EXPR, value, div);
15323       value = size_binop (MULT_EXPR, value, div);
15324     }
15325
15326   return value;
15327 }
15328
15329 /* Likewise, but round down.  */
15330
15331 tree
15332 round_down (tree value, int divisor)
15333 {
15334   tree div = NULL_TREE;
15335
15336   gcc_assert (divisor > 0);
15337   if (divisor == 1)
15338     return value;
15339
15340   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15341      have to do anything.  Only do this when we are not given a const,
15342      because in that case, this check is more expensive than just
15343      doing it.  */
15344   if (TREE_CODE (value) != INTEGER_CST)
15345     {
15346       div = build_int_cst (TREE_TYPE (value), divisor);
15347
15348       if (multiple_of_p (TREE_TYPE (value), value, div))
15349         return value;
15350     }
15351
15352   /* If divisor is a power of two, simplify this to bit manipulation.  */
15353   if (divisor == (divisor & -divisor))
15354     {
15355       tree t;
15356
15357       t = build_int_cst (TREE_TYPE (value), -divisor);
15358       value = size_binop (BIT_AND_EXPR, value, t);
15359     }
15360   else
15361     {
15362       if (!div)
15363         div = build_int_cst (TREE_TYPE (value), divisor);
15364       value = size_binop (FLOOR_DIV_EXPR, value, div);
15365       value = size_binop (MULT_EXPR, value, div);
15366     }
15367
15368   return value;
15369 }
15370
15371 /* Returns the pointer to the base of the object addressed by EXP and
15372    extracts the information about the offset of the access, storing it
15373    to PBITPOS and POFFSET.  */
15374
15375 static tree
15376 split_address_to_core_and_offset (tree exp,
15377                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15378 {
15379   tree core;
15380   enum machine_mode mode;
15381   int unsignedp, volatilep;
15382   HOST_WIDE_INT bitsize;
15383
15384   if (TREE_CODE (exp) == ADDR_EXPR)
15385     {
15386       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15387                                   poffset, &mode, &unsignedp, &volatilep,
15388                                   false);
15389       core = fold_addr_expr (core);
15390     }
15391   else
15392     {
15393       core = exp;
15394       *pbitpos = 0;
15395       *poffset = NULL_TREE;
15396     }
15397
15398   return core;
15399 }
15400
15401 /* Returns true if addresses of E1 and E2 differ by a constant, false
15402    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15403
15404 bool
15405 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15406 {
15407   tree core1, core2;
15408   HOST_WIDE_INT bitpos1, bitpos2;
15409   tree toffset1, toffset2, tdiff, type;
15410
15411   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15412   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15413
15414   if (bitpos1 % BITS_PER_UNIT != 0
15415       || bitpos2 % BITS_PER_UNIT != 0
15416       || !operand_equal_p (core1, core2, 0))
15417     return false;
15418
15419   if (toffset1 && toffset2)
15420     {
15421       type = TREE_TYPE (toffset1);
15422       if (type != TREE_TYPE (toffset2))
15423         toffset2 = fold_convert (type, toffset2);
15424
15425       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15426       if (!cst_and_fits_in_hwi (tdiff))
15427         return false;
15428
15429       *diff = int_cst_value (tdiff);
15430     }
15431   else if (toffset1 || toffset2)
15432     {
15433       /* If only one of the offsets is non-constant, the difference cannot
15434          be a constant.  */
15435       return false;
15436     }
15437   else
15438     *diff = 0;
15439
15440   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15441   return true;
15442 }
15443
15444 /* Simplify the floating point expression EXP when the sign of the
15445    result is not significant.  Return NULL_TREE if no simplification
15446    is possible.  */
15447
15448 tree
15449 fold_strip_sign_ops (tree exp)
15450 {
15451   tree arg0, arg1;
15452
15453   switch (TREE_CODE (exp))
15454     {
15455     case ABS_EXPR:
15456     case NEGATE_EXPR:
15457       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15458       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15459
15460     case MULT_EXPR:
15461     case RDIV_EXPR:
15462       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15463         return NULL_TREE;
15464       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15465       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15466       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15467         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15468                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15469                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15470       break;
15471
15472     case COMPOUND_EXPR:
15473       arg0 = TREE_OPERAND (exp, 0);
15474       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15475       if (arg1)
15476         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15477       break;
15478       
15479     case COND_EXPR:
15480       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15481       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15482       if (arg0 || arg1)
15483         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15484                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15485                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15486       break;
15487       
15488     case CALL_EXPR:
15489       {
15490         const enum built_in_function fcode = builtin_mathfn_code (exp);
15491         switch (fcode)
15492         {
15493         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15494           /* Strip copysign function call, return the 1st argument. */
15495           arg0 = CALL_EXPR_ARG (exp, 0);
15496           arg1 = CALL_EXPR_ARG (exp, 1);
15497           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15498
15499         default:
15500           /* Strip sign ops from the argument of "odd" math functions.  */
15501           if (negate_mathfn_p (fcode))
15502             {
15503               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15504               if (arg0)
15505                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15506             }
15507           break;
15508         }
15509       }
15510       break;
15511
15512     default:
15513       break;
15514     }
15515   return NULL_TREE;
15516 }