OSDN Git Service

2009-05-19 Manuel López-Ibáñez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30 /* The entry points in this file are fold, size_int_wide, size_binop
31    and force_fit_type_double.
32
33    fold takes a tree as argument and returns a simplified tree.
34
35    size_binop takes a tree code for an arithmetic operation
36    and two operands that are trees, and produces a tree for the
37    result, assuming the type comes from `sizetype'.
38
39    size_int takes an integer value, and creates a tree constant
40    with type from `sizetype'.
41
42    force_fit_type_double takes a constant, an overflowable flag and a
43    prior overflow indicator.  It forces the value to fit the type and
44    sets TREE_OVERFLOW.
45
46    Note: Since the folders get called on non-gimple code as well as
47    gimple code, we need to handle GIMPLE tuples as well as their
48    corresponding tree equivalents.  */
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "rtl.h"
59 #include "expr.h"
60 #include "tm_p.h"
61 #include "target.h"
62 #include "toplev.h"
63 #include "intl.h"
64 #include "ggc.h"
65 #include "hashtab.h"
66 #include "langhooks.h"
67 #include "md5.h"
68 #include "gimple.h"
69
70 /* Nonzero if we are folding constants inside an initializer; zero
71    otherwise.  */
72 int folding_initializer = 0;
73
74 /* The following constants represent a bit based encoding of GCC's
75    comparison operators.  This encoding simplifies transformations
76    on relational comparison operators, such as AND and OR.  */
77 enum comparison_code {
78   COMPCODE_FALSE = 0,
79   COMPCODE_LT = 1,
80   COMPCODE_EQ = 2,
81   COMPCODE_LE = 3,
82   COMPCODE_GT = 4,
83   COMPCODE_LTGT = 5,
84   COMPCODE_GE = 6,
85   COMPCODE_ORD = 7,
86   COMPCODE_UNORD = 8,
87   COMPCODE_UNLT = 9,
88   COMPCODE_UNEQ = 10,
89   COMPCODE_UNLE = 11,
90   COMPCODE_UNGT = 12,
91   COMPCODE_NE = 13,
92   COMPCODE_UNGE = 14,
93   COMPCODE_TRUE = 15
94 };
95
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static int operand_equal_for_comparison_p (tree, tree, tree);
107 static int twoval_comparison_p (tree, tree *, tree *, int *);
108 static tree eval_subst (tree, tree, tree, tree, tree);
109 static tree pedantic_omit_one_operand (tree, tree, tree);
110 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
111 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
112 static tree optimize_bit_field_compare (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 int all_ones_mask_p (const_tree, int);
117 static tree sign_bit_p (tree, const_tree);
118 static int simple_operand_p (const_tree);
119 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
120 static tree range_predecessor (tree);
121 static tree range_successor (tree);
122 extern tree make_range (tree, int *, tree *, tree *, bool *);
123 extern tree build_range_check (tree, tree, int, tree, tree);
124 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
125                           tree, tree);
126 static tree fold_range_test (enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree fold_truthop (enum tree_code, tree, tree, tree);
130 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
131 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
133 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
134                                                  tree, tree,
135                                                  tree, tree, int);
136 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
137                                  tree, tree, tree);
138 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
139 static tree fold_div_compare (enum tree_code, tree, tree, tree);
140 static bool reorder_operands_p (const_tree, const_tree);
141 static tree fold_negate_const (tree, tree);
142 static tree fold_not_const (tree, tree);
143 static tree fold_relational_const (enum tree_code, tree, tree, tree);
144 static tree fold_convert_const (enum tree_code, tree, tree);
145
146
147 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
148    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
149    and SUM1.  Then this yields nonzero if overflow occurred during the
150    addition.
151
152    Overflow occurs if A and B have the same sign, but A and SUM differ in
153    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
154    sign.  */
155 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
156 \f
157 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
158    We do that by representing the two-word integer in 4 words, with only
159    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
160    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
161
162 #define LOWPART(x) \
163   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
164 #define HIGHPART(x) \
165   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
166 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
167
168 /* Unpack a two-word integer into 4 words.
169    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
170    WORDS points to the array of HOST_WIDE_INTs.  */
171
172 static void
173 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
174 {
175   words[0] = LOWPART (low);
176   words[1] = HIGHPART (low);
177   words[2] = LOWPART (hi);
178   words[3] = HIGHPART (hi);
179 }
180
181 /* Pack an array of 4 words into a two-word integer.
182    WORDS points to the array of words.
183    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
184
185 static void
186 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
187         HOST_WIDE_INT *hi)
188 {
189   *low = words[0] + words[1] * BASE;
190   *hi = words[2] + words[3] * BASE;
191 }
192 \f
193 /* Force the double-word integer L1, H1 to be within the range of the
194    integer type TYPE.  Stores the properly truncated and sign-extended
195    double-word integer in *LV, *HV.  Returns true if the operation
196    overflows, that is, argument and result are different.  */
197
198 int
199 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
200                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
201 {
202   unsigned HOST_WIDE_INT low0 = l1;
203   HOST_WIDE_INT high0 = h1;
204   unsigned int prec;
205   int sign_extended_type;
206
207   if (POINTER_TYPE_P (type)
208       || TREE_CODE (type) == OFFSET_TYPE)
209     prec = POINTER_SIZE;
210   else
211     prec = TYPE_PRECISION (type);
212
213   /* Size types *are* sign extended.  */
214   sign_extended_type = (!TYPE_UNSIGNED (type)
215                         || (TREE_CODE (type) == INTEGER_TYPE
216                             && TYPE_IS_SIZETYPE (type)));
217
218   /* First clear all bits that are beyond the type's precision.  */
219   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
220     ;
221   else if (prec > HOST_BITS_PER_WIDE_INT)
222     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
223   else
224     {
225       h1 = 0;
226       if (prec < HOST_BITS_PER_WIDE_INT)
227         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
228     }
229
230   /* Then do sign extension if necessary.  */
231   if (!sign_extended_type)
232     /* No sign extension */;
233   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
234     /* Correct width already.  */;
235   else if (prec > HOST_BITS_PER_WIDE_INT)
236     {
237       /* Sign extend top half? */
238       if (h1 & ((unsigned HOST_WIDE_INT)1
239                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
240         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
241     }
242   else if (prec == HOST_BITS_PER_WIDE_INT)
243     {
244       if ((HOST_WIDE_INT)l1 < 0)
245         h1 = -1;
246     }
247   else
248     {
249       /* Sign extend bottom half? */
250       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
251         {
252           h1 = -1;
253           l1 |= (HOST_WIDE_INT)(-1) << prec;
254         }
255     }
256
257   *lv = l1;
258   *hv = h1;
259
260   /* If the value didn't fit, signal overflow.  */
261   return l1 != low0 || h1 != high0;
262 }
263
264 /* We force the double-int HIGH:LOW to the range of the type TYPE by
265    sign or zero extending it.
266    OVERFLOWABLE indicates if we are interested
267    in overflow of the value, when >0 we are only interested in signed
268    overflow, for <0 we are interested in any overflow.  OVERFLOWED
269    indicates whether overflow has already occurred.  CONST_OVERFLOWED
270    indicates whether constant overflow has already occurred.  We force
271    T's value to be within range of T's type (by setting to 0 or 1 all
272    the bits outside the type's range).  We set TREE_OVERFLOWED if,
273         OVERFLOWED is nonzero,
274         or OVERFLOWABLE is >0 and signed overflow occurs
275         or OVERFLOWABLE is <0 and any overflow occurs
276    We return a new tree node for the extended double-int.  The node
277    is shared if no overflow flags are set.  */
278
279 tree
280 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
281                        HOST_WIDE_INT high, int overflowable,
282                        bool overflowed)
283 {
284   int sign_extended_type;
285   bool overflow;
286
287   /* Size types *are* sign extended.  */
288   sign_extended_type = (!TYPE_UNSIGNED (type)
289                         || (TREE_CODE (type) == INTEGER_TYPE
290                             && TYPE_IS_SIZETYPE (type)));
291
292   overflow = fit_double_type (low, high, &low, &high, type);
293
294   /* If we need to set overflow flags, return a new unshared node.  */
295   if (overflowed || overflow)
296     {
297       if (overflowed
298           || overflowable < 0
299           || (overflowable > 0 && sign_extended_type))
300         {
301           tree t = make_node (INTEGER_CST);
302           TREE_INT_CST_LOW (t) = low;
303           TREE_INT_CST_HIGH (t) = high;
304           TREE_TYPE (t) = type;
305           TREE_OVERFLOW (t) = 1;
306           return t;
307         }
308     }
309
310   /* Else build a shared node.  */
311   return build_int_cst_wide (type, low, high);
312 }
313 \f
314 /* Add two doubleword integers with doubleword result.
315    Return nonzero if the operation overflows according to UNSIGNED_P.
316    Each argument is given as two `HOST_WIDE_INT' pieces.
317    One argument is L1 and H1; the other, L2 and H2.
318    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
319
320 int
321 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
322                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
323                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
324                       bool unsigned_p)
325 {
326   unsigned HOST_WIDE_INT l;
327   HOST_WIDE_INT h;
328
329   l = l1 + l2;
330   h = h1 + h2 + (l < l1);
331
332   *lv = l;
333   *hv = h;
334
335   if (unsigned_p)
336     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
337   else
338     return OVERFLOW_SUM_SIGN (h1, h2, h);
339 }
340
341 /* Negate a doubleword integer with doubleword result.
342    Return nonzero if the operation overflows, assuming it's signed.
343    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
344    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
345
346 int
347 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
348             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
349 {
350   if (l1 == 0)
351     {
352       *lv = 0;
353       *hv = - h1;
354       return (*hv & h1) < 0;
355     }
356   else
357     {
358       *lv = -l1;
359       *hv = ~h1;
360       return 0;
361     }
362 }
363 \f
364 /* Multiply two doubleword integers with doubleword result.
365    Return nonzero if the operation overflows according to UNSIGNED_P.
366    Each argument is given as two `HOST_WIDE_INT' pieces.
367    One argument is L1 and H1; the other, L2 and H2.
368    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
369
370 int
371 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
372                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
373                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
374                       bool unsigned_p)
375 {
376   HOST_WIDE_INT arg1[4];
377   HOST_WIDE_INT arg2[4];
378   HOST_WIDE_INT prod[4 * 2];
379   unsigned HOST_WIDE_INT carry;
380   int i, j, k;
381   unsigned HOST_WIDE_INT toplow, neglow;
382   HOST_WIDE_INT tophigh, neghigh;
383
384   encode (arg1, l1, h1);
385   encode (arg2, l2, h2);
386
387   memset (prod, 0, sizeof prod);
388
389   for (i = 0; i < 4; i++)
390     {
391       carry = 0;
392       for (j = 0; j < 4; j++)
393         {
394           k = i + j;
395           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
396           carry += arg1[i] * arg2[j];
397           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
398           carry += prod[k];
399           prod[k] = LOWPART (carry);
400           carry = HIGHPART (carry);
401         }
402       prod[i + 4] = carry;
403     }
404
405   decode (prod, lv, hv);
406   decode (prod + 4, &toplow, &tophigh);
407
408   /* Unsigned overflow is immediate.  */
409   if (unsigned_p)
410     return (toplow | tophigh) != 0;
411
412   /* Check for signed overflow by calculating the signed representation of the
413      top half of the result; it should agree with the low half's sign bit.  */
414   if (h1 < 0)
415     {
416       neg_double (l2, h2, &neglow, &neghigh);
417       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
418     }
419   if (h2 < 0)
420     {
421       neg_double (l1, h1, &neglow, &neghigh);
422       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
423     }
424   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
425 }
426 \f
427 /* Shift the doubleword integer in L1, H1 left by COUNT places
428    keeping only PREC bits of result.
429    Shift right if COUNT is negative.
430    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
431    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
432
433 void
434 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
435                HOST_WIDE_INT count, unsigned int prec,
436                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
437 {
438   unsigned HOST_WIDE_INT signmask;
439
440   if (count < 0)
441     {
442       rshift_double (l1, h1, -count, prec, lv, hv, arith);
443       return;
444     }
445
446   if (SHIFT_COUNT_TRUNCATED)
447     count %= prec;
448
449   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
450     {
451       /* Shifting by the host word size is undefined according to the
452          ANSI standard, so we must handle this as a special case.  */
453       *hv = 0;
454       *lv = 0;
455     }
456   else if (count >= HOST_BITS_PER_WIDE_INT)
457     {
458       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
459       *lv = 0;
460     }
461   else
462     {
463       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
464              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
465       *lv = l1 << count;
466     }
467
468   /* Sign extend all bits that are beyond the precision.  */
469
470   signmask = -((prec > HOST_BITS_PER_WIDE_INT
471                 ? ((unsigned HOST_WIDE_INT) *hv
472                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
473                 : (*lv >> (prec - 1))) & 1);
474
475   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
476     ;
477   else if (prec >= HOST_BITS_PER_WIDE_INT)
478     {
479       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
480       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
481     }
482   else
483     {
484       *hv = signmask;
485       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
486       *lv |= signmask << prec;
487     }
488 }
489
490 /* Shift the doubleword integer in L1, H1 right by COUNT places
491    keeping only PREC bits of result.  COUNT must be positive.
492    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
493    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
494
495 void
496 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
497                HOST_WIDE_INT count, unsigned int prec,
498                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
499                int arith)
500 {
501   unsigned HOST_WIDE_INT signmask;
502
503   signmask = (arith
504               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
505               : 0);
506
507   if (SHIFT_COUNT_TRUNCATED)
508     count %= prec;
509
510   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
511     {
512       /* Shifting by the host word size is undefined according to the
513          ANSI standard, so we must handle this as a special case.  */
514       *hv = 0;
515       *lv = 0;
516     }
517   else if (count >= HOST_BITS_PER_WIDE_INT)
518     {
519       *hv = 0;
520       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
521     }
522   else
523     {
524       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
525       *lv = ((l1 >> count)
526              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
527     }
528
529   /* Zero / sign extend all bits that are beyond the precision.  */
530
531   if (count >= (HOST_WIDE_INT)prec)
532     {
533       *hv = signmask;
534       *lv = signmask;
535     }
536   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
537     ;
538   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
539     {
540       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
541       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
542     }
543   else
544     {
545       *hv = signmask;
546       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
547       *lv |= signmask << (prec - count);
548     }
549 }
550 \f
551 /* Rotate the doubleword integer in L1, H1 left by COUNT places
552    keeping only PREC bits of result.
553    Rotate right if COUNT is negative.
554    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
555
556 void
557 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
558                 HOST_WIDE_INT count, unsigned int prec,
559                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
560 {
561   unsigned HOST_WIDE_INT s1l, s2l;
562   HOST_WIDE_INT s1h, s2h;
563
564   count %= prec;
565   if (count < 0)
566     count += prec;
567
568   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
569   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
570   *lv = s1l | s2l;
571   *hv = s1h | s2h;
572 }
573
574 /* Rotate the doubleword integer in L1, H1 left by COUNT places
575    keeping only PREC bits of result.  COUNT must be positive.
576    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
577
578 void
579 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
580                 HOST_WIDE_INT count, unsigned int prec,
581                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
582 {
583   unsigned HOST_WIDE_INT s1l, s2l;
584   HOST_WIDE_INT s1h, s2h;
585
586   count %= prec;
587   if (count < 0)
588     count += prec;
589
590   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
591   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
592   *lv = s1l | s2l;
593   *hv = s1h | s2h;
594 }
595 \f
596 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
597    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
598    CODE is a tree code for a kind of division, one of
599    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
600    or EXACT_DIV_EXPR
601    It controls how the quotient is rounded to an integer.
602    Return nonzero if the operation overflows.
603    UNS nonzero says do unsigned division.  */
604
605 int
606 div_and_round_double (enum tree_code code, int uns,
607                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
608                       HOST_WIDE_INT hnum_orig,
609                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
610                       HOST_WIDE_INT hden_orig,
611                       unsigned HOST_WIDE_INT *lquo,
612                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
613                       HOST_WIDE_INT *hrem)
614 {
615   int quo_neg = 0;
616   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
617   HOST_WIDE_INT den[4], quo[4];
618   int i, j;
619   unsigned HOST_WIDE_INT work;
620   unsigned HOST_WIDE_INT carry = 0;
621   unsigned HOST_WIDE_INT lnum = lnum_orig;
622   HOST_WIDE_INT hnum = hnum_orig;
623   unsigned HOST_WIDE_INT lden = lden_orig;
624   HOST_WIDE_INT hden = hden_orig;
625   int overflow = 0;
626
627   if (hden == 0 && lden == 0)
628     overflow = 1, lden = 1;
629
630   /* Calculate quotient sign and convert operands to unsigned.  */
631   if (!uns)
632     {
633       if (hnum < 0)
634         {
635           quo_neg = ~ quo_neg;
636           /* (minimum integer) / (-1) is the only overflow case.  */
637           if (neg_double (lnum, hnum, &lnum, &hnum)
638               && ((HOST_WIDE_INT) lden & hden) == -1)
639             overflow = 1;
640         }
641       if (hden < 0)
642         {
643           quo_neg = ~ quo_neg;
644           neg_double (lden, hden, &lden, &hden);
645         }
646     }
647
648   if (hnum == 0 && hden == 0)
649     {                           /* single precision */
650       *hquo = *hrem = 0;
651       /* This unsigned division rounds toward zero.  */
652       *lquo = lnum / lden;
653       goto finish_up;
654     }
655
656   if (hnum == 0)
657     {                           /* trivial case: dividend < divisor */
658       /* hden != 0 already checked.  */
659       *hquo = *lquo = 0;
660       *hrem = hnum;
661       *lrem = lnum;
662       goto finish_up;
663     }
664
665   memset (quo, 0, sizeof quo);
666
667   memset (num, 0, sizeof num);  /* to zero 9th element */
668   memset (den, 0, sizeof den);
669
670   encode (num, lnum, hnum);
671   encode (den, lden, hden);
672
673   /* Special code for when the divisor < BASE.  */
674   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
675     {
676       /* hnum != 0 already checked.  */
677       for (i = 4 - 1; i >= 0; i--)
678         {
679           work = num[i] + carry * BASE;
680           quo[i] = work / lden;
681           carry = work % lden;
682         }
683     }
684   else
685     {
686       /* Full double precision division,
687          with thanks to Don Knuth's "Seminumerical Algorithms".  */
688       int num_hi_sig, den_hi_sig;
689       unsigned HOST_WIDE_INT quo_est, scale;
690
691       /* Find the highest nonzero divisor digit.  */
692       for (i = 4 - 1;; i--)
693         if (den[i] != 0)
694           {
695             den_hi_sig = i;
696             break;
697           }
698
699       /* Insure that the first digit of the divisor is at least BASE/2.
700          This is required by the quotient digit estimation algorithm.  */
701
702       scale = BASE / (den[den_hi_sig] + 1);
703       if (scale > 1)
704         {               /* scale divisor and dividend */
705           carry = 0;
706           for (i = 0; i <= 4 - 1; i++)
707             {
708               work = (num[i] * scale) + carry;
709               num[i] = LOWPART (work);
710               carry = HIGHPART (work);
711             }
712
713           num[4] = carry;
714           carry = 0;
715           for (i = 0; i <= 4 - 1; i++)
716             {
717               work = (den[i] * scale) + carry;
718               den[i] = LOWPART (work);
719               carry = HIGHPART (work);
720               if (den[i] != 0) den_hi_sig = i;
721             }
722         }
723
724       num_hi_sig = 4;
725
726       /* Main loop */
727       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
728         {
729           /* Guess the next quotient digit, quo_est, by dividing the first
730              two remaining dividend digits by the high order quotient digit.
731              quo_est is never low and is at most 2 high.  */
732           unsigned HOST_WIDE_INT tmp;
733
734           num_hi_sig = i + den_hi_sig + 1;
735           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
736           if (num[num_hi_sig] != den[den_hi_sig])
737             quo_est = work / den[den_hi_sig];
738           else
739             quo_est = BASE - 1;
740
741           /* Refine quo_est so it's usually correct, and at most one high.  */
742           tmp = work - quo_est * den[den_hi_sig];
743           if (tmp < BASE
744               && (den[den_hi_sig - 1] * quo_est
745                   > (tmp * BASE + num[num_hi_sig - 2])))
746             quo_est--;
747
748           /* Try QUO_EST as the quotient digit, by multiplying the
749              divisor by QUO_EST and subtracting from the remaining dividend.
750              Keep in mind that QUO_EST is the I - 1st digit.  */
751
752           carry = 0;
753           for (j = 0; j <= den_hi_sig; j++)
754             {
755               work = quo_est * den[j] + carry;
756               carry = HIGHPART (work);
757               work = num[i + j] - LOWPART (work);
758               num[i + j] = LOWPART (work);
759               carry += HIGHPART (work) != 0;
760             }
761
762           /* If quo_est was high by one, then num[i] went negative and
763              we need to correct things.  */
764           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
765             {
766               quo_est--;
767               carry = 0;                /* add divisor back in */
768               for (j = 0; j <= den_hi_sig; j++)
769                 {
770                   work = num[i + j] + den[j] + carry;
771                   carry = HIGHPART (work);
772                   num[i + j] = LOWPART (work);
773                 }
774
775               num [num_hi_sig] += carry;
776             }
777
778           /* Store the quotient digit.  */
779           quo[i] = quo_est;
780         }
781     }
782
783   decode (quo, lquo, hquo);
784
785  finish_up:
786   /* If result is negative, make it so.  */
787   if (quo_neg)
788     neg_double (*lquo, *hquo, lquo, hquo);
789
790   /* Compute trial remainder:  rem = num - (quo * den)  */
791   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
792   neg_double (*lrem, *hrem, lrem, hrem);
793   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
794
795   switch (code)
796     {
797     case TRUNC_DIV_EXPR:
798     case TRUNC_MOD_EXPR:        /* round toward zero */
799     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
800       return overflow;
801
802     case FLOOR_DIV_EXPR:
803     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
804       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
805         {
806           /* quo = quo - 1;  */
807           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
808                       lquo, hquo);
809         }
810       else
811         return overflow;
812       break;
813
814     case CEIL_DIV_EXPR:
815     case CEIL_MOD_EXPR:         /* round toward positive infinity */
816       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
817         {
818           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
819                       lquo, hquo);
820         }
821       else
822         return overflow;
823       break;
824
825     case ROUND_DIV_EXPR:
826     case ROUND_MOD_EXPR:        /* round to closest integer */
827       {
828         unsigned HOST_WIDE_INT labs_rem = *lrem;
829         HOST_WIDE_INT habs_rem = *hrem;
830         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
831         HOST_WIDE_INT habs_den = hden, htwice;
832
833         /* Get absolute values.  */
834         if (*hrem < 0)
835           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
836         if (hden < 0)
837           neg_double (lden, hden, &labs_den, &habs_den);
838
839         /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient.  */
840         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
841                     labs_rem, habs_rem, &ltwice, &htwice);
842
843         if (((unsigned HOST_WIDE_INT) habs_den
844              < (unsigned HOST_WIDE_INT) htwice)
845             || (((unsigned HOST_WIDE_INT) habs_den
846                  == (unsigned HOST_WIDE_INT) htwice)
847                 && (labs_den <= ltwice)))
848           {
849             if (*hquo < 0)
850               /* quo = quo - 1;  */
851               add_double (*lquo, *hquo,
852                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
853             else
854               /* quo = quo + 1; */
855               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
856                           lquo, hquo);
857           }
858         else
859           return overflow;
860       }
861       break;
862
863     default:
864       gcc_unreachable ();
865     }
866
867   /* Compute true remainder:  rem = num - (quo * den)  */
868   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
869   neg_double (*lrem, *hrem, lrem, hrem);
870   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
871   return overflow;
872 }
873
874 /* If ARG2 divides ARG1 with zero remainder, carries out the division
875    of type CODE and returns the quotient.
876    Otherwise returns NULL_TREE.  */
877
878 tree
879 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
880 {
881   unsigned HOST_WIDE_INT int1l, int2l;
882   HOST_WIDE_INT int1h, int2h;
883   unsigned HOST_WIDE_INT quol, reml;
884   HOST_WIDE_INT quoh, remh;
885   tree type = TREE_TYPE (arg1);
886   int uns = TYPE_UNSIGNED (type);
887
888   int1l = TREE_INT_CST_LOW (arg1);
889   int1h = TREE_INT_CST_HIGH (arg1);
890   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
891      &obj[some_exotic_number].  */
892   if (POINTER_TYPE_P (type))
893     {
894       uns = false;
895       type = signed_type_for (type);
896       fit_double_type (int1l, int1h, &int1l, &int1h,
897                        type);
898     }
899   else
900     fit_double_type (int1l, int1h, &int1l, &int1h, type);
901   int2l = TREE_INT_CST_LOW (arg2);
902   int2h = TREE_INT_CST_HIGH (arg2);
903
904   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
905                         &quol, &quoh, &reml, &remh);
906   if (remh != 0 || reml != 0)
907     return NULL_TREE;
908
909   return build_int_cst_wide (type, quol, quoh);
910 }
911 \f
912 /* This is nonzero if we should defer warnings about undefined
913    overflow.  This facility exists because these warnings are a
914    special case.  The code to estimate loop iterations does not want
915    to issue any warnings, since it works with expressions which do not
916    occur in user code.  Various bits of cleanup code call fold(), but
917    only use the result if it has certain characteristics (e.g., is a
918    constant); that code only wants to issue a warning if the result is
919    used.  */
920
921 static int fold_deferring_overflow_warnings;
922
923 /* If a warning about undefined overflow is deferred, this is the
924    warning.  Note that this may cause us to turn two warnings into
925    one, but that is fine since it is sufficient to only give one
926    warning per expression.  */
927
928 static const char* fold_deferred_overflow_warning;
929
930 /* If a warning about undefined overflow is deferred, this is the
931    level at which the warning should be emitted.  */
932
933 static enum warn_strict_overflow_code fold_deferred_overflow_code;
934
935 /* Start deferring overflow warnings.  We could use a stack here to
936    permit nested calls, but at present it is not necessary.  */
937
938 void
939 fold_defer_overflow_warnings (void)
940 {
941   ++fold_deferring_overflow_warnings;
942 }
943
944 /* Stop deferring overflow warnings.  If there is a pending warning,
945    and ISSUE is true, then issue the warning if appropriate.  STMT is
946    the statement with which the warning should be associated (used for
947    location information); STMT may be NULL.  CODE is the level of the
948    warning--a warn_strict_overflow_code value.  This function will use
949    the smaller of CODE and the deferred code when deciding whether to
950    issue the warning.  CODE may be zero to mean to always use the
951    deferred code.  */
952
953 void
954 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
955 {
956   const char *warnmsg;
957   location_t locus;
958
959   gcc_assert (fold_deferring_overflow_warnings > 0);
960   --fold_deferring_overflow_warnings;
961   if (fold_deferring_overflow_warnings > 0)
962     {
963       if (fold_deferred_overflow_warning != NULL
964           && code != 0
965           && code < (int) fold_deferred_overflow_code)
966         fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
967       return;
968     }
969
970   warnmsg = fold_deferred_overflow_warning;
971   fold_deferred_overflow_warning = NULL;
972
973   if (!issue || warnmsg == NULL)
974     return;
975
976   if (gimple_no_warning_p (stmt))
977     return;
978
979   /* Use the smallest code level when deciding to issue the
980      warning.  */
981   if (code == 0 || code > (int) fold_deferred_overflow_code)
982     code = fold_deferred_overflow_code;
983
984   if (!issue_strict_overflow_warning (code))
985     return;
986
987   if (stmt == NULL)
988     locus = input_location;
989   else
990     locus = gimple_location (stmt);
991   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
992 }
993
994 /* Stop deferring overflow warnings, ignoring any deferred
995    warnings.  */
996
997 void
998 fold_undefer_and_ignore_overflow_warnings (void)
999 {
1000   fold_undefer_overflow_warnings (false, NULL, 0);
1001 }
1002
1003 /* Whether we are deferring overflow warnings.  */
1004
1005 bool
1006 fold_deferring_overflow_warnings_p (void)
1007 {
1008   return fold_deferring_overflow_warnings > 0;
1009 }
1010
1011 /* This is called when we fold something based on the fact that signed
1012    overflow is undefined.  */
1013
1014 static void
1015 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1016 {
1017   if (fold_deferring_overflow_warnings > 0)
1018     {
1019       if (fold_deferred_overflow_warning == NULL
1020           || wc < fold_deferred_overflow_code)
1021         {
1022           fold_deferred_overflow_warning = gmsgid;
1023           fold_deferred_overflow_code = wc;
1024         }
1025     }
1026   else if (issue_strict_overflow_warning (wc))
1027     warning (OPT_Wstrict_overflow, gmsgid);
1028 }
1029 \f
1030 /* Return true if the built-in mathematical function specified by CODE
1031    is odd, i.e. -f(x) == f(-x).  */
1032
1033 static bool
1034 negate_mathfn_p (enum built_in_function code)
1035 {
1036   switch (code)
1037     {
1038     CASE_FLT_FN (BUILT_IN_ASIN):
1039     CASE_FLT_FN (BUILT_IN_ASINH):
1040     CASE_FLT_FN (BUILT_IN_ATAN):
1041     CASE_FLT_FN (BUILT_IN_ATANH):
1042     CASE_FLT_FN (BUILT_IN_CASIN):
1043     CASE_FLT_FN (BUILT_IN_CASINH):
1044     CASE_FLT_FN (BUILT_IN_CATAN):
1045     CASE_FLT_FN (BUILT_IN_CATANH):
1046     CASE_FLT_FN (BUILT_IN_CBRT):
1047     CASE_FLT_FN (BUILT_IN_CPROJ):
1048     CASE_FLT_FN (BUILT_IN_CSIN):
1049     CASE_FLT_FN (BUILT_IN_CSINH):
1050     CASE_FLT_FN (BUILT_IN_CTAN):
1051     CASE_FLT_FN (BUILT_IN_CTANH):
1052     CASE_FLT_FN (BUILT_IN_ERF):
1053     CASE_FLT_FN (BUILT_IN_LLROUND):
1054     CASE_FLT_FN (BUILT_IN_LROUND):
1055     CASE_FLT_FN (BUILT_IN_ROUND):
1056     CASE_FLT_FN (BUILT_IN_SIN):
1057     CASE_FLT_FN (BUILT_IN_SINH):
1058     CASE_FLT_FN (BUILT_IN_TAN):
1059     CASE_FLT_FN (BUILT_IN_TANH):
1060     CASE_FLT_FN (BUILT_IN_TRUNC):
1061       return true;
1062
1063     CASE_FLT_FN (BUILT_IN_LLRINT):
1064     CASE_FLT_FN (BUILT_IN_LRINT):
1065     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1066     CASE_FLT_FN (BUILT_IN_RINT):
1067       return !flag_rounding_math;
1068     
1069     default:
1070       break;
1071     }
1072   return false;
1073 }
1074
1075 /* Check whether we may negate an integer constant T without causing
1076    overflow.  */
1077
1078 bool
1079 may_negate_without_overflow_p (const_tree t)
1080 {
1081   unsigned HOST_WIDE_INT val;
1082   unsigned int prec;
1083   tree type;
1084
1085   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1086
1087   type = TREE_TYPE (t);
1088   if (TYPE_UNSIGNED (type))
1089     return false;
1090
1091   prec = TYPE_PRECISION (type);
1092   if (prec > HOST_BITS_PER_WIDE_INT)
1093     {
1094       if (TREE_INT_CST_LOW (t) != 0)
1095         return true;
1096       prec -= HOST_BITS_PER_WIDE_INT;
1097       val = TREE_INT_CST_HIGH (t);
1098     }
1099   else
1100     val = TREE_INT_CST_LOW (t);
1101   if (prec < HOST_BITS_PER_WIDE_INT)
1102     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1103   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1104 }
1105
1106 /* Determine whether an expression T can be cheaply negated using
1107    the function negate_expr without introducing undefined overflow.  */
1108
1109 static bool
1110 negate_expr_p (tree t)
1111 {
1112   tree type;
1113
1114   if (t == 0)
1115     return false;
1116
1117   type = TREE_TYPE (t);
1118
1119   STRIP_SIGN_NOPS (t);
1120   switch (TREE_CODE (t))
1121     {
1122     case INTEGER_CST:
1123       if (TYPE_OVERFLOW_WRAPS (type))
1124         return true;
1125
1126       /* Check that -CST will not overflow type.  */
1127       return may_negate_without_overflow_p (t);
1128     case BIT_NOT_EXPR:
1129       return (INTEGRAL_TYPE_P (type)
1130               && TYPE_OVERFLOW_WRAPS (type));
1131
1132     case FIXED_CST:
1133     case REAL_CST:
1134     case NEGATE_EXPR:
1135       return true;
1136
1137     case COMPLEX_CST:
1138       return negate_expr_p (TREE_REALPART (t))
1139              && negate_expr_p (TREE_IMAGPART (t));
1140
1141     case COMPLEX_EXPR:
1142       return negate_expr_p (TREE_OPERAND (t, 0))
1143              && negate_expr_p (TREE_OPERAND (t, 1));
1144
1145     case CONJ_EXPR:
1146       return negate_expr_p (TREE_OPERAND (t, 0));
1147
1148     case PLUS_EXPR:
1149       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1150           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1151         return false;
1152       /* -(A + B) -> (-B) - A.  */
1153       if (negate_expr_p (TREE_OPERAND (t, 1))
1154           && reorder_operands_p (TREE_OPERAND (t, 0),
1155                                  TREE_OPERAND (t, 1)))
1156         return true;
1157       /* -(A + B) -> (-A) - B.  */
1158       return negate_expr_p (TREE_OPERAND (t, 0));
1159
1160     case MINUS_EXPR:
1161       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1162       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1163              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1164              && reorder_operands_p (TREE_OPERAND (t, 0),
1165                                     TREE_OPERAND (t, 1));
1166
1167     case MULT_EXPR:
1168       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1169         break;
1170
1171       /* Fall through.  */
1172
1173     case RDIV_EXPR:
1174       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1175         return negate_expr_p (TREE_OPERAND (t, 1))
1176                || negate_expr_p (TREE_OPERAND (t, 0));
1177       break;
1178
1179     case TRUNC_DIV_EXPR:
1180     case ROUND_DIV_EXPR:
1181     case FLOOR_DIV_EXPR:
1182     case CEIL_DIV_EXPR:
1183     case EXACT_DIV_EXPR:
1184       /* In general we can't negate A / B, because if A is INT_MIN and
1185          B is 1, we may turn this into INT_MIN / -1 which is undefined
1186          and actually traps on some architectures.  But if overflow is
1187          undefined, we can negate, because - (INT_MIN / 1) is an
1188          overflow.  */
1189       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1190           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1191         break;
1192       return negate_expr_p (TREE_OPERAND (t, 1))
1193              || negate_expr_p (TREE_OPERAND (t, 0));
1194
1195     case NOP_EXPR:
1196       /* Negate -((double)float) as (double)(-float).  */
1197       if (TREE_CODE (type) == REAL_TYPE)
1198         {
1199           tree tem = strip_float_extensions (t);
1200           if (tem != t)
1201             return negate_expr_p (tem);
1202         }
1203       break;
1204
1205     case CALL_EXPR:
1206       /* Negate -f(x) as f(-x).  */
1207       if (negate_mathfn_p (builtin_mathfn_code (t)))
1208         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1209       break;
1210
1211     case RSHIFT_EXPR:
1212       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1213       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1214         {
1215           tree op1 = TREE_OPERAND (t, 1);
1216           if (TREE_INT_CST_HIGH (op1) == 0
1217               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1218                  == TREE_INT_CST_LOW (op1))
1219             return true;
1220         }
1221       break;
1222
1223     default:
1224       break;
1225     }
1226   return false;
1227 }
1228
1229 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1230    simplification is possible.
1231    If negate_expr_p would return true for T, NULL_TREE will never be
1232    returned.  */
1233
1234 static tree
1235 fold_negate_expr (tree t)
1236 {
1237   tree type = TREE_TYPE (t);
1238   tree tem;
1239
1240   switch (TREE_CODE (t))
1241     {
1242     /* Convert - (~A) to A + 1.  */
1243     case BIT_NOT_EXPR:
1244       if (INTEGRAL_TYPE_P (type))
1245         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1246                             build_int_cst (type, 1));
1247       break;
1248       
1249     case INTEGER_CST:
1250       tem = fold_negate_const (t, type);
1251       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1252           || !TYPE_OVERFLOW_TRAPS (type))
1253         return tem;
1254       break;
1255
1256     case REAL_CST:
1257       tem = fold_negate_const (t, type);
1258       /* Two's complement FP formats, such as c4x, may overflow.  */
1259       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1260         return tem;
1261       break;
1262
1263     case FIXED_CST:
1264       tem = fold_negate_const (t, type);
1265       return tem;
1266
1267     case COMPLEX_CST:
1268       {
1269         tree rpart = negate_expr (TREE_REALPART (t));
1270         tree ipart = negate_expr (TREE_IMAGPART (t));
1271
1272         if ((TREE_CODE (rpart) == REAL_CST
1273              && TREE_CODE (ipart) == REAL_CST)
1274             || (TREE_CODE (rpart) == INTEGER_CST
1275                 && TREE_CODE (ipart) == INTEGER_CST))
1276           return build_complex (type, rpart, ipart);
1277       }
1278       break;
1279
1280     case COMPLEX_EXPR:
1281       if (negate_expr_p (t))
1282         return fold_build2 (COMPLEX_EXPR, type,
1283                             fold_negate_expr (TREE_OPERAND (t, 0)),
1284                             fold_negate_expr (TREE_OPERAND (t, 1)));
1285       break;
1286       
1287     case CONJ_EXPR:
1288       if (negate_expr_p (t))
1289         return fold_build1 (CONJ_EXPR, type,
1290                             fold_negate_expr (TREE_OPERAND (t, 0)));
1291       break;
1292
1293     case NEGATE_EXPR:
1294       return TREE_OPERAND (t, 0);
1295
1296     case PLUS_EXPR:
1297       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1298           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1299         {
1300           /* -(A + B) -> (-B) - A.  */
1301           if (negate_expr_p (TREE_OPERAND (t, 1))
1302               && reorder_operands_p (TREE_OPERAND (t, 0),
1303                                      TREE_OPERAND (t, 1)))
1304             {
1305               tem = negate_expr (TREE_OPERAND (t, 1));
1306               return fold_build2 (MINUS_EXPR, type,
1307                                   tem, TREE_OPERAND (t, 0));
1308             }
1309
1310           /* -(A + B) -> (-A) - B.  */
1311           if (negate_expr_p (TREE_OPERAND (t, 0)))
1312             {
1313               tem = negate_expr (TREE_OPERAND (t, 0));
1314               return fold_build2 (MINUS_EXPR, type,
1315                                   tem, TREE_OPERAND (t, 1));
1316             }
1317         }
1318       break;
1319
1320     case MINUS_EXPR:
1321       /* - (A - B) -> B - A  */
1322       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1323           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1324           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1325         return fold_build2 (MINUS_EXPR, type,
1326                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1327       break;
1328
1329     case MULT_EXPR:
1330       if (TYPE_UNSIGNED (type))
1331         break;
1332
1333       /* Fall through.  */
1334
1335     case RDIV_EXPR:
1336       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1337         {
1338           tem = TREE_OPERAND (t, 1);
1339           if (negate_expr_p (tem))
1340             return fold_build2 (TREE_CODE (t), type,
1341                                 TREE_OPERAND (t, 0), negate_expr (tem));
1342           tem = TREE_OPERAND (t, 0);
1343           if (negate_expr_p (tem))
1344             return fold_build2 (TREE_CODE (t), type,
1345                                 negate_expr (tem), TREE_OPERAND (t, 1));
1346         }
1347       break;
1348
1349     case TRUNC_DIV_EXPR:
1350     case ROUND_DIV_EXPR:
1351     case FLOOR_DIV_EXPR:
1352     case CEIL_DIV_EXPR:
1353     case EXACT_DIV_EXPR:
1354       /* In general we can't negate A / B, because if A is INT_MIN and
1355          B is 1, we may turn this into INT_MIN / -1 which is undefined
1356          and actually traps on some architectures.  But if overflow is
1357          undefined, we can negate, because - (INT_MIN / 1) is an
1358          overflow.  */
1359       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1360         {
1361           const char * const warnmsg = G_("assuming signed overflow does not "
1362                                           "occur when negating a division");
1363           tem = TREE_OPERAND (t, 1);
1364           if (negate_expr_p (tem))
1365             {
1366               if (INTEGRAL_TYPE_P (type)
1367                   && (TREE_CODE (tem) != INTEGER_CST
1368                       || integer_onep (tem)))
1369                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1370               return fold_build2 (TREE_CODE (t), type,
1371                                   TREE_OPERAND (t, 0), negate_expr (tem));
1372             }
1373           tem = TREE_OPERAND (t, 0);
1374           if (negate_expr_p (tem))
1375             {
1376               if (INTEGRAL_TYPE_P (type)
1377                   && (TREE_CODE (tem) != INTEGER_CST
1378                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1379                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1380               return fold_build2 (TREE_CODE (t), type,
1381                                   negate_expr (tem), TREE_OPERAND (t, 1));
1382             }
1383         }
1384       break;
1385
1386     case NOP_EXPR:
1387       /* Convert -((double)float) into (double)(-float).  */
1388       if (TREE_CODE (type) == REAL_TYPE)
1389         {
1390           tem = strip_float_extensions (t);
1391           if (tem != t && negate_expr_p (tem))
1392             return fold_convert (type, negate_expr (tem));
1393         }
1394       break;
1395
1396     case CALL_EXPR:
1397       /* Negate -f(x) as f(-x).  */
1398       if (negate_mathfn_p (builtin_mathfn_code (t))
1399           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1400         {
1401           tree fndecl, arg;
1402
1403           fndecl = get_callee_fndecl (t);
1404           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1405           return build_call_expr (fndecl, 1, arg);
1406         }
1407       break;
1408
1409     case RSHIFT_EXPR:
1410       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1411       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1412         {
1413           tree op1 = TREE_OPERAND (t, 1);
1414           if (TREE_INT_CST_HIGH (op1) == 0
1415               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1416                  == TREE_INT_CST_LOW (op1))
1417             {
1418               tree ntype = TYPE_UNSIGNED (type)
1419                            ? signed_type_for (type)
1420                            : unsigned_type_for (type);
1421               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1422               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1423               return fold_convert (type, temp);
1424             }
1425         }
1426       break;
1427
1428     default:
1429       break;
1430     }
1431
1432   return NULL_TREE;
1433 }
1434
1435 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1436    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1437    return NULL_TREE. */
1438
1439 static tree
1440 negate_expr (tree t)
1441 {
1442   tree type, tem;
1443
1444   if (t == NULL_TREE)
1445     return NULL_TREE;
1446
1447   type = TREE_TYPE (t);
1448   STRIP_SIGN_NOPS (t);
1449
1450   tem = fold_negate_expr (t);
1451   if (!tem)
1452     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1453   return fold_convert (type, tem);
1454 }
1455 \f
1456 /* Split a tree IN into a constant, literal and variable parts that could be
1457    combined with CODE to make IN.  "constant" means an expression with
1458    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1459    commutative arithmetic operation.  Store the constant part into *CONP,
1460    the literal in *LITP and return the variable part.  If a part isn't
1461    present, set it to null.  If the tree does not decompose in this way,
1462    return the entire tree as the variable part and the other parts as null.
1463
1464    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1465    case, we negate an operand that was subtracted.  Except if it is a
1466    literal for which we use *MINUS_LITP instead.
1467
1468    If NEGATE_P is true, we are negating all of IN, again except a literal
1469    for which we use *MINUS_LITP instead.
1470
1471    If IN is itself a literal or constant, return it as appropriate.
1472
1473    Note that we do not guarantee that any of the three values will be the
1474    same type as IN, but they will have the same signedness and mode.  */
1475
1476 static tree
1477 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1478             tree *minus_litp, int negate_p)
1479 {
1480   tree var = 0;
1481
1482   *conp = 0;
1483   *litp = 0;
1484   *minus_litp = 0;
1485
1486   /* Strip any conversions that don't change the machine mode or signedness.  */
1487   STRIP_SIGN_NOPS (in);
1488
1489   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1490       || TREE_CODE (in) == FIXED_CST)
1491     *litp = in;
1492   else if (TREE_CODE (in) == code
1493            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1494                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1495                /* We can associate addition and subtraction together (even
1496                   though the C standard doesn't say so) for integers because
1497                   the value is not affected.  For reals, the value might be
1498                   affected, so we can't.  */
1499                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1500                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1501     {
1502       tree op0 = TREE_OPERAND (in, 0);
1503       tree op1 = TREE_OPERAND (in, 1);
1504       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1505       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1506
1507       /* First see if either of the operands is a literal, then a constant.  */
1508       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1509           || TREE_CODE (op0) == FIXED_CST)
1510         *litp = op0, op0 = 0;
1511       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1512                || TREE_CODE (op1) == FIXED_CST)
1513         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1514
1515       if (op0 != 0 && TREE_CONSTANT (op0))
1516         *conp = op0, op0 = 0;
1517       else if (op1 != 0 && TREE_CONSTANT (op1))
1518         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1519
1520       /* If we haven't dealt with either operand, this is not a case we can
1521          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1522       if (op0 != 0 && op1 != 0)
1523         var = in;
1524       else if (op0 != 0)
1525         var = op0;
1526       else
1527         var = op1, neg_var_p = neg1_p;
1528
1529       /* Now do any needed negations.  */
1530       if (neg_litp_p)
1531         *minus_litp = *litp, *litp = 0;
1532       if (neg_conp_p)
1533         *conp = negate_expr (*conp);
1534       if (neg_var_p)
1535         var = negate_expr (var);
1536     }
1537   else if (TREE_CONSTANT (in))
1538     *conp = in;
1539   else
1540     var = in;
1541
1542   if (negate_p)
1543     {
1544       if (*litp)
1545         *minus_litp = *litp, *litp = 0;
1546       else if (*minus_litp)
1547         *litp = *minus_litp, *minus_litp = 0;
1548       *conp = negate_expr (*conp);
1549       var = negate_expr (var);
1550     }
1551
1552   return var;
1553 }
1554
1555 /* Re-associate trees split by the above function.  T1 and T2 are either
1556    expressions to associate or null.  Return the new expression, if any.  If
1557    we build an operation, do it in TYPE and with CODE.  */
1558
1559 static tree
1560 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1561 {
1562   if (t1 == 0)
1563     return t2;
1564   else if (t2 == 0)
1565     return t1;
1566
1567   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1568      try to fold this since we will have infinite recursion.  But do
1569      deal with any NEGATE_EXPRs.  */
1570   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1571       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1572     {
1573       if (code == PLUS_EXPR)
1574         {
1575           if (TREE_CODE (t1) == NEGATE_EXPR)
1576             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1577                            fold_convert (type, TREE_OPERAND (t1, 0)));
1578           else if (TREE_CODE (t2) == NEGATE_EXPR)
1579             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1580                            fold_convert (type, TREE_OPERAND (t2, 0)));
1581           else if (integer_zerop (t2))
1582             return fold_convert (type, t1);
1583         }
1584       else if (code == MINUS_EXPR)
1585         {
1586           if (integer_zerop (t2))
1587             return fold_convert (type, t1);
1588         }
1589
1590       return build2 (code, type, fold_convert (type, t1),
1591                      fold_convert (type, t2));
1592     }
1593
1594   return fold_build2 (code, type, fold_convert (type, t1),
1595                       fold_convert (type, t2));
1596 }
1597 \f
1598 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1599    for use in int_const_binop, size_binop and size_diffop.  */
1600
1601 static bool
1602 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1603 {
1604   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1605     return false;
1606   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1607     return false;
1608
1609   switch (code)
1610     {
1611     case LSHIFT_EXPR:
1612     case RSHIFT_EXPR:
1613     case LROTATE_EXPR:
1614     case RROTATE_EXPR:
1615       return true;
1616
1617     default:
1618       break;
1619     }
1620
1621   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1622          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1623          && TYPE_MODE (type1) == TYPE_MODE (type2);
1624 }
1625
1626
1627 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1628    to produce a new constant.  Return NULL_TREE if we don't know how
1629    to evaluate CODE at compile-time.
1630
1631    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1632
1633 tree
1634 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1635 {
1636   unsigned HOST_WIDE_INT int1l, int2l;
1637   HOST_WIDE_INT int1h, int2h;
1638   unsigned HOST_WIDE_INT low;
1639   HOST_WIDE_INT hi;
1640   unsigned HOST_WIDE_INT garbagel;
1641   HOST_WIDE_INT garbageh;
1642   tree t;
1643   tree type = TREE_TYPE (arg1);
1644   int uns = TYPE_UNSIGNED (type);
1645   int is_sizetype
1646     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1647   int overflow = 0;
1648
1649   int1l = TREE_INT_CST_LOW (arg1);
1650   int1h = TREE_INT_CST_HIGH (arg1);
1651   int2l = TREE_INT_CST_LOW (arg2);
1652   int2h = TREE_INT_CST_HIGH (arg2);
1653
1654   switch (code)
1655     {
1656     case BIT_IOR_EXPR:
1657       low = int1l | int2l, hi = int1h | int2h;
1658       break;
1659
1660     case BIT_XOR_EXPR:
1661       low = int1l ^ int2l, hi = int1h ^ int2h;
1662       break;
1663
1664     case BIT_AND_EXPR:
1665       low = int1l & int2l, hi = int1h & int2h;
1666       break;
1667
1668     case RSHIFT_EXPR:
1669       int2l = -int2l;
1670     case LSHIFT_EXPR:
1671       /* It's unclear from the C standard whether shifts can overflow.
1672          The following code ignores overflow; perhaps a C standard
1673          interpretation ruling is needed.  */
1674       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1675                      &low, &hi, !uns);
1676       break;
1677
1678     case RROTATE_EXPR:
1679       int2l = - int2l;
1680     case LROTATE_EXPR:
1681       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1682                       &low, &hi);
1683       break;
1684
1685     case PLUS_EXPR:
1686       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1687       break;
1688
1689     case MINUS_EXPR:
1690       neg_double (int2l, int2h, &low, &hi);
1691       add_double (int1l, int1h, low, hi, &low, &hi);
1692       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1693       break;
1694
1695     case MULT_EXPR:
1696       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1697       break;
1698
1699     case TRUNC_DIV_EXPR:
1700     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1701     case EXACT_DIV_EXPR:
1702       /* This is a shortcut for a common special case.  */
1703       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1704           && !TREE_OVERFLOW (arg1)
1705           && !TREE_OVERFLOW (arg2)
1706           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1707         {
1708           if (code == CEIL_DIV_EXPR)
1709             int1l += int2l - 1;
1710
1711           low = int1l / int2l, hi = 0;
1712           break;
1713         }
1714
1715       /* ... fall through ...  */
1716
1717     case ROUND_DIV_EXPR:
1718       if (int2h == 0 && int2l == 0)
1719         return NULL_TREE;
1720       if (int2h == 0 && int2l == 1)
1721         {
1722           low = int1l, hi = int1h;
1723           break;
1724         }
1725       if (int1l == int2l && int1h == int2h
1726           && ! (int1l == 0 && int1h == 0))
1727         {
1728           low = 1, hi = 0;
1729           break;
1730         }
1731       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1732                                        &low, &hi, &garbagel, &garbageh);
1733       break;
1734
1735     case TRUNC_MOD_EXPR:
1736     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1737       /* This is a shortcut for a common special case.  */
1738       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1739           && !TREE_OVERFLOW (arg1)
1740           && !TREE_OVERFLOW (arg2)
1741           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1742         {
1743           if (code == CEIL_MOD_EXPR)
1744             int1l += int2l - 1;
1745           low = int1l % int2l, hi = 0;
1746           break;
1747         }
1748
1749       /* ... fall through ...  */
1750
1751     case ROUND_MOD_EXPR:
1752       if (int2h == 0 && int2l == 0)
1753         return NULL_TREE;
1754       overflow = div_and_round_double (code, uns,
1755                                        int1l, int1h, int2l, int2h,
1756                                        &garbagel, &garbageh, &low, &hi);
1757       break;
1758
1759     case MIN_EXPR:
1760     case MAX_EXPR:
1761       if (uns)
1762         low = (((unsigned HOST_WIDE_INT) int1h
1763                 < (unsigned HOST_WIDE_INT) int2h)
1764                || (((unsigned HOST_WIDE_INT) int1h
1765                     == (unsigned HOST_WIDE_INT) int2h)
1766                    && int1l < int2l));
1767       else
1768         low = (int1h < int2h
1769                || (int1h == int2h && int1l < int2l));
1770
1771       if (low == (code == MIN_EXPR))
1772         low = int1l, hi = int1h;
1773       else
1774         low = int2l, hi = int2h;
1775       break;
1776
1777     default:
1778       return NULL_TREE;
1779     }
1780
1781   if (notrunc)
1782     {
1783       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1784
1785       /* Propagate overflow flags ourselves.  */
1786       if (((!uns || is_sizetype) && overflow)
1787           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1788         {
1789           t = copy_node (t);
1790           TREE_OVERFLOW (t) = 1;
1791         }
1792     }
1793   else
1794     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1795                                ((!uns || is_sizetype) && overflow)
1796                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1797
1798   return t;
1799 }
1800
1801 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1802    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1803    are the same kind of constant and the same machine mode.  Return zero if
1804    combining the constants is not allowed in the current operating mode.
1805
1806    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1807
1808 static tree
1809 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1810 {
1811   /* Sanity check for the recursive cases.  */
1812   if (!arg1 || !arg2)
1813     return NULL_TREE;
1814
1815   STRIP_NOPS (arg1);
1816   STRIP_NOPS (arg2);
1817
1818   if (TREE_CODE (arg1) == INTEGER_CST)
1819     return int_const_binop (code, arg1, arg2, notrunc);
1820
1821   if (TREE_CODE (arg1) == REAL_CST)
1822     {
1823       enum machine_mode mode;
1824       REAL_VALUE_TYPE d1;
1825       REAL_VALUE_TYPE d2;
1826       REAL_VALUE_TYPE value;
1827       REAL_VALUE_TYPE result;
1828       bool inexact;
1829       tree t, type;
1830
1831       /* The following codes are handled by real_arithmetic.  */
1832       switch (code)
1833         {
1834         case PLUS_EXPR:
1835         case MINUS_EXPR:
1836         case MULT_EXPR:
1837         case RDIV_EXPR:
1838         case MIN_EXPR:
1839         case MAX_EXPR:
1840           break;
1841
1842         default:
1843           return NULL_TREE;
1844         }
1845
1846       d1 = TREE_REAL_CST (arg1);
1847       d2 = TREE_REAL_CST (arg2);
1848
1849       type = TREE_TYPE (arg1);
1850       mode = TYPE_MODE (type);
1851
1852       /* Don't perform operation if we honor signaling NaNs and
1853          either operand is a NaN.  */
1854       if (HONOR_SNANS (mode)
1855           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1856         return NULL_TREE;
1857
1858       /* Don't perform operation if it would raise a division
1859          by zero exception.  */
1860       if (code == RDIV_EXPR
1861           && REAL_VALUES_EQUAL (d2, dconst0)
1862           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1863         return NULL_TREE;
1864
1865       /* If either operand is a NaN, just return it.  Otherwise, set up
1866          for floating-point trap; we return an overflow.  */
1867       if (REAL_VALUE_ISNAN (d1))
1868         return arg1;
1869       else if (REAL_VALUE_ISNAN (d2))
1870         return arg2;
1871
1872       inexact = real_arithmetic (&value, code, &d1, &d2);
1873       real_convert (&result, mode, &value);
1874
1875       /* Don't constant fold this floating point operation if
1876          the result has overflowed and flag_trapping_math.  */
1877       if (flag_trapping_math
1878           && MODE_HAS_INFINITIES (mode)
1879           && REAL_VALUE_ISINF (result)
1880           && !REAL_VALUE_ISINF (d1)
1881           && !REAL_VALUE_ISINF (d2))
1882         return NULL_TREE;
1883
1884       /* Don't constant fold this floating point operation if the
1885          result may dependent upon the run-time rounding mode and
1886          flag_rounding_math is set, or if GCC's software emulation
1887          is unable to accurately represent the result.  */
1888       if ((flag_rounding_math
1889            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1890           && (inexact || !real_identical (&result, &value)))
1891         return NULL_TREE;
1892
1893       t = build_real (type, result);
1894
1895       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1896       return t;
1897     }
1898
1899   if (TREE_CODE (arg1) == FIXED_CST)
1900     {
1901       FIXED_VALUE_TYPE f1;
1902       FIXED_VALUE_TYPE f2;
1903       FIXED_VALUE_TYPE result;
1904       tree t, type;
1905       int sat_p;
1906       bool overflow_p;
1907
1908       /* The following codes are handled by fixed_arithmetic.  */
1909       switch (code)
1910         {
1911         case PLUS_EXPR:
1912         case MINUS_EXPR:
1913         case MULT_EXPR:
1914         case TRUNC_DIV_EXPR:
1915           f2 = TREE_FIXED_CST (arg2);
1916           break;
1917
1918         case LSHIFT_EXPR:
1919         case RSHIFT_EXPR:
1920           f2.data.high = TREE_INT_CST_HIGH (arg2);
1921           f2.data.low = TREE_INT_CST_LOW (arg2);
1922           f2.mode = SImode;
1923           break;
1924
1925         default:
1926           return NULL_TREE;
1927         }
1928
1929       f1 = TREE_FIXED_CST (arg1);
1930       type = TREE_TYPE (arg1);
1931       sat_p = TYPE_SATURATING (type);
1932       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1933       t = build_fixed (type, result);
1934       /* Propagate overflow flags.  */
1935       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1936         TREE_OVERFLOW (t) = 1;
1937       return t;
1938     }
1939
1940   if (TREE_CODE (arg1) == COMPLEX_CST)
1941     {
1942       tree type = TREE_TYPE (arg1);
1943       tree r1 = TREE_REALPART (arg1);
1944       tree i1 = TREE_IMAGPART (arg1);
1945       tree r2 = TREE_REALPART (arg2);
1946       tree i2 = TREE_IMAGPART (arg2);
1947       tree real, imag;
1948
1949       switch (code)
1950         {
1951         case PLUS_EXPR:
1952         case MINUS_EXPR:
1953           real = const_binop (code, r1, r2, notrunc);
1954           imag = const_binop (code, i1, i2, notrunc);
1955           break;
1956
1957         case MULT_EXPR:
1958           real = const_binop (MINUS_EXPR,
1959                               const_binop (MULT_EXPR, r1, r2, notrunc),
1960                               const_binop (MULT_EXPR, i1, i2, notrunc),
1961                               notrunc);
1962           imag = const_binop (PLUS_EXPR,
1963                               const_binop (MULT_EXPR, r1, i2, notrunc),
1964                               const_binop (MULT_EXPR, i1, r2, notrunc),
1965                               notrunc);
1966           break;
1967
1968         case RDIV_EXPR:
1969           {
1970             tree magsquared
1971               = const_binop (PLUS_EXPR,
1972                              const_binop (MULT_EXPR, r2, r2, notrunc),
1973                              const_binop (MULT_EXPR, i2, i2, notrunc),
1974                              notrunc);
1975             tree t1
1976               = const_binop (PLUS_EXPR,
1977                              const_binop (MULT_EXPR, r1, r2, notrunc),
1978                              const_binop (MULT_EXPR, i1, i2, notrunc),
1979                              notrunc);
1980             tree t2
1981               = const_binop (MINUS_EXPR,
1982                              const_binop (MULT_EXPR, i1, r2, notrunc),
1983                              const_binop (MULT_EXPR, r1, i2, notrunc),
1984                              notrunc);
1985
1986             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1987               code = TRUNC_DIV_EXPR;
1988
1989             real = const_binop (code, t1, magsquared, notrunc);
1990             imag = const_binop (code, t2, magsquared, notrunc);
1991           }
1992           break;
1993
1994         default:
1995           return NULL_TREE;
1996         }
1997
1998       if (real && imag)
1999         return build_complex (type, real, imag);
2000     }
2001
2002   if (TREE_CODE (arg1) == VECTOR_CST)
2003     {
2004       tree type = TREE_TYPE(arg1);
2005       int count = TYPE_VECTOR_SUBPARTS (type), i;
2006       tree elements1, elements2, list = NULL_TREE;
2007       
2008       if(TREE_CODE(arg2) != VECTOR_CST)
2009         return NULL_TREE;
2010         
2011       elements1 = TREE_VECTOR_CST_ELTS (arg1);
2012       elements2 = TREE_VECTOR_CST_ELTS (arg2);
2013
2014       for (i = 0; i < count; i++)
2015         {
2016           tree elem1, elem2, elem;
2017           
2018           /* The trailing elements can be empty and should be treated as 0 */
2019           if(!elements1)
2020             elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2021           else
2022             {
2023               elem1 = TREE_VALUE(elements1);
2024               elements1 = TREE_CHAIN (elements1);
2025             }  
2026             
2027           if(!elements2)
2028             elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2029           else
2030             {
2031               elem2 = TREE_VALUE(elements2);
2032               elements2 = TREE_CHAIN (elements2);
2033             }
2034               
2035           elem = const_binop (code, elem1, elem2, notrunc);
2036           
2037           /* It is possible that const_binop cannot handle the given
2038             code and return NULL_TREE */
2039           if(elem == NULL_TREE)
2040             return NULL_TREE;
2041           
2042           list = tree_cons (NULL_TREE, elem, list);
2043         }
2044       return build_vector(type, nreverse(list));  
2045     }
2046   return NULL_TREE;
2047 }
2048
2049 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2050    indicates which particular sizetype to create.  */
2051
2052 tree
2053 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2054 {
2055   return build_int_cst (sizetype_tab[(int) kind], number);
2056 }
2057 \f
2058 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2059    is a tree code.  The type of the result is taken from the operands.
2060    Both must be equivalent integer types, ala int_binop_types_match_p.
2061    If the operands are constant, so is the result.  */
2062
2063 tree
2064 size_binop (enum tree_code code, tree arg0, tree arg1)
2065 {
2066   tree type = TREE_TYPE (arg0);
2067
2068   if (arg0 == error_mark_node || arg1 == error_mark_node)
2069     return error_mark_node;
2070
2071   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2072                                        TREE_TYPE (arg1)));
2073
2074   /* Handle the special case of two integer constants faster.  */
2075   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2076     {
2077       /* And some specific cases even faster than that.  */
2078       if (code == PLUS_EXPR)
2079         {
2080           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2081             return arg1;
2082           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2083             return arg0;
2084         }
2085       else if (code == MINUS_EXPR)
2086         {
2087           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2088             return arg0;
2089         }
2090       else if (code == MULT_EXPR)
2091         {
2092           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2093             return arg1;
2094         }
2095
2096       /* Handle general case of two integer constants.  */
2097       return int_const_binop (code, arg0, arg1, 0);
2098     }
2099
2100   return fold_build2 (code, type, arg0, arg1);
2101 }
2102
2103 /* Given two values, either both of sizetype or both of bitsizetype,
2104    compute the difference between the two values.  Return the value
2105    in signed type corresponding to the type of the operands.  */
2106
2107 tree
2108 size_diffop (tree arg0, tree arg1)
2109 {
2110   tree type = TREE_TYPE (arg0);
2111   tree ctype;
2112
2113   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2114                                        TREE_TYPE (arg1)));
2115
2116   /* If the type is already signed, just do the simple thing.  */
2117   if (!TYPE_UNSIGNED (type))
2118     return size_binop (MINUS_EXPR, arg0, arg1);
2119
2120   if (type == sizetype)
2121     ctype = ssizetype;
2122   else if (type == bitsizetype)
2123     ctype = sbitsizetype;
2124   else
2125     ctype = signed_type_for (type);
2126
2127   /* If either operand is not a constant, do the conversions to the signed
2128      type and subtract.  The hardware will do the right thing with any
2129      overflow in the subtraction.  */
2130   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2131     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2132                        fold_convert (ctype, arg1));
2133
2134   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2135      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2136      overflow) and negate (which can't either).  Special-case a result
2137      of zero while we're here.  */
2138   if (tree_int_cst_equal (arg0, arg1))
2139     return build_int_cst (ctype, 0);
2140   else if (tree_int_cst_lt (arg1, arg0))
2141     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2142   else
2143     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2144                        fold_convert (ctype, size_binop (MINUS_EXPR,
2145                                                         arg1, arg0)));
2146 }
2147 \f
2148 /* A subroutine of fold_convert_const handling conversions of an
2149    INTEGER_CST to another integer type.  */
2150
2151 static tree
2152 fold_convert_const_int_from_int (tree type, const_tree arg1)
2153 {
2154   tree t;
2155
2156   /* Given an integer constant, make new constant with new type,
2157      appropriately sign-extended or truncated.  */
2158   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2159                              TREE_INT_CST_HIGH (arg1),
2160                              /* Don't set the overflow when
2161                                 converting from a pointer,  */
2162                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2163                              /* or to a sizetype with same signedness
2164                                 and the precision is unchanged.
2165                                 ???  sizetype is always sign-extended,
2166                                 but its signedness depends on the
2167                                 frontend.  Thus we see spurious overflows
2168                                 here if we do not check this.  */
2169                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2170                                    == TYPE_PRECISION (type))
2171                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2172                                       == TYPE_UNSIGNED (type))
2173                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2174                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2175                                       || (TREE_CODE (type) == INTEGER_TYPE
2176                                           && TYPE_IS_SIZETYPE (type)))),
2177                              (TREE_INT_CST_HIGH (arg1) < 0
2178                               && (TYPE_UNSIGNED (type)
2179                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2180                              | TREE_OVERFLOW (arg1));
2181
2182   return t;
2183 }
2184
2185 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2186    to an integer type.  */
2187
2188 static tree
2189 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2190 {
2191   int overflow = 0;
2192   tree t;
2193
2194   /* The following code implements the floating point to integer
2195      conversion rules required by the Java Language Specification,
2196      that IEEE NaNs are mapped to zero and values that overflow
2197      the target precision saturate, i.e. values greater than
2198      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2199      are mapped to INT_MIN.  These semantics are allowed by the
2200      C and C++ standards that simply state that the behavior of
2201      FP-to-integer conversion is unspecified upon overflow.  */
2202
2203   HOST_WIDE_INT high, low;
2204   REAL_VALUE_TYPE r;
2205   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2206
2207   switch (code)
2208     {
2209     case FIX_TRUNC_EXPR:
2210       real_trunc (&r, VOIDmode, &x);
2211       break;
2212
2213     default:
2214       gcc_unreachable ();
2215     }
2216
2217   /* If R is NaN, return zero and show we have an overflow.  */
2218   if (REAL_VALUE_ISNAN (r))
2219     {
2220       overflow = 1;
2221       high = 0;
2222       low = 0;
2223     }
2224
2225   /* See if R is less than the lower bound or greater than the
2226      upper bound.  */
2227
2228   if (! overflow)
2229     {
2230       tree lt = TYPE_MIN_VALUE (type);
2231       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2232       if (REAL_VALUES_LESS (r, l))
2233         {
2234           overflow = 1;
2235           high = TREE_INT_CST_HIGH (lt);
2236           low = TREE_INT_CST_LOW (lt);
2237         }
2238     }
2239
2240   if (! overflow)
2241     {
2242       tree ut = TYPE_MAX_VALUE (type);
2243       if (ut)
2244         {
2245           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2246           if (REAL_VALUES_LESS (u, r))
2247             {
2248               overflow = 1;
2249               high = TREE_INT_CST_HIGH (ut);
2250               low = TREE_INT_CST_LOW (ut);
2251             }
2252         }
2253     }
2254
2255   if (! overflow)
2256     REAL_VALUE_TO_INT (&low, &high, r);
2257
2258   t = force_fit_type_double (type, low, high, -1,
2259                              overflow | TREE_OVERFLOW (arg1));
2260   return t;
2261 }
2262
2263 /* A subroutine of fold_convert_const handling conversions of a
2264    FIXED_CST to an integer type.  */
2265
2266 static tree
2267 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2268 {
2269   tree t;
2270   double_int temp, temp_trunc;
2271   unsigned int mode;
2272
2273   /* Right shift FIXED_CST to temp by fbit.  */
2274   temp = TREE_FIXED_CST (arg1).data;
2275   mode = TREE_FIXED_CST (arg1).mode;
2276   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2277     {
2278       lshift_double (temp.low, temp.high,
2279                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2280                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2281
2282       /* Left shift temp to temp_trunc by fbit.  */
2283       lshift_double (temp.low, temp.high,
2284                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2285                      &temp_trunc.low, &temp_trunc.high,
2286                      SIGNED_FIXED_POINT_MODE_P (mode));
2287     }
2288   else
2289     {
2290       temp.low = 0;
2291       temp.high = 0;
2292       temp_trunc.low = 0;
2293       temp_trunc.high = 0;
2294     }
2295
2296   /* If FIXED_CST is negative, we need to round the value toward 0.
2297      By checking if the fractional bits are not zero to add 1 to temp.  */
2298   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2299       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2300     {
2301       double_int one;
2302       one.low = 1;
2303       one.high = 0;
2304       temp = double_int_add (temp, one);
2305     }
2306
2307   /* Given a fixed-point constant, make new constant with new type,
2308      appropriately sign-extended or truncated.  */
2309   t = force_fit_type_double (type, temp.low, temp.high, -1,
2310                              (temp.high < 0
2311                               && (TYPE_UNSIGNED (type)
2312                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2313                              | TREE_OVERFLOW (arg1));
2314
2315   return t;
2316 }
2317
2318 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2319    to another floating point type.  */
2320
2321 static tree
2322 fold_convert_const_real_from_real (tree type, const_tree arg1)
2323 {
2324   REAL_VALUE_TYPE value;
2325   tree t;
2326
2327   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2328   t = build_real (type, value);
2329
2330   /* If converting an infinity or NAN to a representation that doesn't
2331      have one, set the overflow bit so that we can produce some kind of
2332      error message at the appropriate point if necessary.  It's not the
2333      most user-friendly message, but it's better than nothing.  */
2334   if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2335       && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2336     TREE_OVERFLOW (t) = 1;
2337   else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2338            && !MODE_HAS_NANS (TYPE_MODE (type)))
2339     TREE_OVERFLOW (t) = 1;
2340   /* Regular overflow, conversion produced an infinity in a mode that
2341      can't represent them.  */
2342   else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2343            && REAL_VALUE_ISINF (value)
2344            && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2345     TREE_OVERFLOW (t) = 1;
2346   else
2347     TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2348   return t;
2349 }
2350
2351 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2352    to a floating point type.  */
2353
2354 static tree
2355 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2356 {
2357   REAL_VALUE_TYPE value;
2358   tree t;
2359
2360   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2361   t = build_real (type, value);
2362
2363   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2364   return t;
2365 }
2366
2367 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2368    to another fixed-point type.  */
2369
2370 static tree
2371 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2372 {
2373   FIXED_VALUE_TYPE value;
2374   tree t;
2375   bool overflow_p;
2376
2377   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2378                               TYPE_SATURATING (type));
2379   t = build_fixed (type, value);
2380
2381   /* Propagate overflow flags.  */
2382   if (overflow_p | TREE_OVERFLOW (arg1))
2383     TREE_OVERFLOW (t) = 1;
2384   return t;
2385 }
2386
2387 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2388    to a fixed-point type.  */
2389
2390 static tree
2391 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2392 {
2393   FIXED_VALUE_TYPE value;
2394   tree t;
2395   bool overflow_p;
2396
2397   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2398                                        TREE_INT_CST (arg1),
2399                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2400                                        TYPE_SATURATING (type));
2401   t = build_fixed (type, value);
2402
2403   /* Propagate overflow flags.  */
2404   if (overflow_p | TREE_OVERFLOW (arg1))
2405     TREE_OVERFLOW (t) = 1;
2406   return t;
2407 }
2408
2409 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2410    to a fixed-point type.  */
2411
2412 static tree
2413 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2414 {
2415   FIXED_VALUE_TYPE value;
2416   tree t;
2417   bool overflow_p;
2418
2419   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2420                                         &TREE_REAL_CST (arg1),
2421                                         TYPE_SATURATING (type));
2422   t = build_fixed (type, value);
2423
2424   /* Propagate overflow flags.  */
2425   if (overflow_p | TREE_OVERFLOW (arg1))
2426     TREE_OVERFLOW (t) = 1;
2427   return t;
2428 }
2429
2430 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2431    type TYPE.  If no simplification can be done return NULL_TREE.  */
2432
2433 static tree
2434 fold_convert_const (enum tree_code code, tree type, tree arg1)
2435 {
2436   if (TREE_TYPE (arg1) == type)
2437     return arg1;
2438
2439   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2440       || TREE_CODE (type) == OFFSET_TYPE)
2441     {
2442       if (TREE_CODE (arg1) == INTEGER_CST)
2443         return fold_convert_const_int_from_int (type, arg1);
2444       else if (TREE_CODE (arg1) == REAL_CST)
2445         return fold_convert_const_int_from_real (code, type, arg1);
2446       else if (TREE_CODE (arg1) == FIXED_CST)
2447         return fold_convert_const_int_from_fixed (type, arg1);
2448     }
2449   else if (TREE_CODE (type) == REAL_TYPE)
2450     {
2451       if (TREE_CODE (arg1) == INTEGER_CST)
2452         return build_real_from_int_cst (type, arg1);
2453       else if (TREE_CODE (arg1) == REAL_CST)
2454         return fold_convert_const_real_from_real (type, arg1);
2455       else if (TREE_CODE (arg1) == FIXED_CST)
2456         return fold_convert_const_real_from_fixed (type, arg1);
2457     }
2458   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2459     {
2460       if (TREE_CODE (arg1) == FIXED_CST)
2461         return fold_convert_const_fixed_from_fixed (type, arg1);
2462       else if (TREE_CODE (arg1) == INTEGER_CST)
2463         return fold_convert_const_fixed_from_int (type, arg1);
2464       else if (TREE_CODE (arg1) == REAL_CST)
2465         return fold_convert_const_fixed_from_real (type, arg1);
2466     }
2467   return NULL_TREE;
2468 }
2469
2470 /* Construct a vector of zero elements of vector type TYPE.  */
2471
2472 static tree
2473 build_zero_vector (tree type)
2474 {
2475   tree elem, list;
2476   int i, units;
2477
2478   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2479   units = TYPE_VECTOR_SUBPARTS (type);
2480   
2481   list = NULL_TREE;
2482   for (i = 0; i < units; i++)
2483     list = tree_cons (NULL_TREE, elem, list);
2484   return build_vector (type, list);
2485 }
2486
2487 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2488
2489 bool
2490 fold_convertible_p (const_tree type, const_tree arg)
2491 {
2492   tree orig = TREE_TYPE (arg);
2493
2494   if (type == orig)
2495     return true;
2496
2497   if (TREE_CODE (arg) == ERROR_MARK
2498       || TREE_CODE (type) == ERROR_MARK
2499       || TREE_CODE (orig) == ERROR_MARK)
2500     return false;
2501
2502   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2503     return true;
2504
2505   switch (TREE_CODE (type))
2506     {
2507     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2508     case POINTER_TYPE: case REFERENCE_TYPE:
2509     case OFFSET_TYPE:
2510       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2511           || TREE_CODE (orig) == OFFSET_TYPE)
2512         return true;
2513       return (TREE_CODE (orig) == VECTOR_TYPE
2514               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2515
2516     case REAL_TYPE:
2517     case FIXED_POINT_TYPE:
2518     case COMPLEX_TYPE:
2519     case VECTOR_TYPE:
2520     case VOID_TYPE:
2521       return TREE_CODE (type) == TREE_CODE (orig);
2522
2523     default:
2524       return false;
2525     }
2526 }
2527
2528 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2529    simple conversions in preference to calling the front-end's convert.  */
2530
2531 tree
2532 fold_convert (tree type, tree arg)
2533 {
2534   tree orig = TREE_TYPE (arg);
2535   tree tem;
2536
2537   if (type == orig)
2538     return arg;
2539
2540   if (TREE_CODE (arg) == ERROR_MARK
2541       || TREE_CODE (type) == ERROR_MARK
2542       || TREE_CODE (orig) == ERROR_MARK)
2543     return error_mark_node;
2544
2545   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2546     return fold_build1 (NOP_EXPR, type, arg);
2547
2548   switch (TREE_CODE (type))
2549     {
2550     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2551     case POINTER_TYPE: case REFERENCE_TYPE:
2552     case OFFSET_TYPE:
2553       if (TREE_CODE (arg) == INTEGER_CST)
2554         {
2555           tem = fold_convert_const (NOP_EXPR, type, arg);
2556           if (tem != NULL_TREE)
2557             return tem;
2558         }
2559       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2560           || TREE_CODE (orig) == OFFSET_TYPE)
2561         return fold_build1 (NOP_EXPR, type, arg);
2562       if (TREE_CODE (orig) == COMPLEX_TYPE)
2563         {
2564           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2565           return fold_convert (type, tem);
2566         }
2567       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2568                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2569       return fold_build1 (NOP_EXPR, type, arg);
2570
2571     case REAL_TYPE:
2572       if (TREE_CODE (arg) == INTEGER_CST)
2573         {
2574           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2575           if (tem != NULL_TREE)
2576             return tem;
2577         }
2578       else if (TREE_CODE (arg) == REAL_CST)
2579         {
2580           tem = fold_convert_const (NOP_EXPR, type, arg);
2581           if (tem != NULL_TREE)
2582             return tem;
2583         }
2584       else if (TREE_CODE (arg) == FIXED_CST)
2585         {
2586           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2587           if (tem != NULL_TREE)
2588             return tem;
2589         }
2590
2591       switch (TREE_CODE (orig))
2592         {
2593         case INTEGER_TYPE:
2594         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2595         case POINTER_TYPE: case REFERENCE_TYPE:
2596           return fold_build1 (FLOAT_EXPR, type, arg);
2597
2598         case REAL_TYPE:
2599           return fold_build1 (NOP_EXPR, type, arg);
2600
2601         case FIXED_POINT_TYPE:
2602           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2603
2604         case COMPLEX_TYPE:
2605           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2606           return fold_convert (type, tem);
2607
2608         default:
2609           gcc_unreachable ();
2610         }
2611
2612     case FIXED_POINT_TYPE:
2613       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2614           || TREE_CODE (arg) == REAL_CST)
2615         {
2616           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2617           if (tem != NULL_TREE)
2618             return tem;
2619         }
2620
2621       switch (TREE_CODE (orig))
2622         {
2623         case FIXED_POINT_TYPE:
2624         case INTEGER_TYPE:
2625         case ENUMERAL_TYPE:
2626         case BOOLEAN_TYPE:
2627         case REAL_TYPE:
2628           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2629
2630         case COMPLEX_TYPE:
2631           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2632           return fold_convert (type, tem);
2633
2634         default:
2635           gcc_unreachable ();
2636         }
2637
2638     case COMPLEX_TYPE:
2639       switch (TREE_CODE (orig))
2640         {
2641         case INTEGER_TYPE:
2642         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2643         case POINTER_TYPE: case REFERENCE_TYPE:
2644         case REAL_TYPE:
2645         case FIXED_POINT_TYPE:
2646           return build2 (COMPLEX_EXPR, type,
2647                          fold_convert (TREE_TYPE (type), arg),
2648                          fold_convert (TREE_TYPE (type), integer_zero_node));
2649         case COMPLEX_TYPE:
2650           {
2651             tree rpart, ipart;
2652
2653             if (TREE_CODE (arg) == COMPLEX_EXPR)
2654               {
2655                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2656                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2657                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2658               }
2659
2660             arg = save_expr (arg);
2661             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2662             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2663             rpart = fold_convert (TREE_TYPE (type), rpart);
2664             ipart = fold_convert (TREE_TYPE (type), ipart);
2665             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2666           }
2667
2668         default:
2669           gcc_unreachable ();
2670         }
2671
2672     case VECTOR_TYPE:
2673       if (integer_zerop (arg))
2674         return build_zero_vector (type);
2675       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2676       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2677                   || TREE_CODE (orig) == VECTOR_TYPE);
2678       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2679
2680     case VOID_TYPE:
2681       tem = fold_ignored_result (arg);
2682       if (TREE_CODE (tem) == MODIFY_EXPR)
2683         return tem;
2684       return fold_build1 (NOP_EXPR, type, tem);
2685
2686     default:
2687       gcc_unreachable ();
2688     }
2689 }
2690 \f
2691 /* Return false if expr can be assumed not to be an lvalue, true
2692    otherwise.  */
2693
2694 static bool
2695 maybe_lvalue_p (const_tree x)
2696 {
2697   /* We only need to wrap lvalue tree codes.  */
2698   switch (TREE_CODE (x))
2699   {
2700   case VAR_DECL:
2701   case PARM_DECL:
2702   case RESULT_DECL:
2703   case LABEL_DECL:
2704   case FUNCTION_DECL:
2705   case SSA_NAME:
2706
2707   case COMPONENT_REF:
2708   case INDIRECT_REF:
2709   case ALIGN_INDIRECT_REF:
2710   case MISALIGNED_INDIRECT_REF:
2711   case ARRAY_REF:
2712   case ARRAY_RANGE_REF:
2713   case BIT_FIELD_REF:
2714   case OBJ_TYPE_REF:
2715
2716   case REALPART_EXPR:
2717   case IMAGPART_EXPR:
2718   case PREINCREMENT_EXPR:
2719   case PREDECREMENT_EXPR:
2720   case SAVE_EXPR:
2721   case TRY_CATCH_EXPR:
2722   case WITH_CLEANUP_EXPR:
2723   case COMPOUND_EXPR:
2724   case MODIFY_EXPR:
2725   case TARGET_EXPR:
2726   case COND_EXPR:
2727   case BIND_EXPR:
2728   case MIN_EXPR:
2729   case MAX_EXPR:
2730     break;
2731
2732   default:
2733     /* Assume the worst for front-end tree codes.  */
2734     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2735       break;
2736     return false;
2737   }
2738
2739   return true;
2740 }
2741
2742 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2743
2744 tree
2745 non_lvalue (tree x)
2746 {
2747   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2748      us.  */
2749   if (in_gimple_form)
2750     return x;
2751
2752   if (! maybe_lvalue_p (x))
2753     return x;
2754   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2755 }
2756
2757 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2758    Zero means allow extended lvalues.  */
2759
2760 int pedantic_lvalues;
2761
2762 /* When pedantic, return an expr equal to X but certainly not valid as a
2763    pedantic lvalue.  Otherwise, return X.  */
2764
2765 static tree
2766 pedantic_non_lvalue (tree x)
2767 {
2768   if (pedantic_lvalues)
2769     return non_lvalue (x);
2770   else
2771     return x;
2772 }
2773 \f
2774 /* Given a tree comparison code, return the code that is the logical inverse
2775    of the given code.  It is not safe to do this for floating-point
2776    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2777    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2778
2779 enum tree_code
2780 invert_tree_comparison (enum tree_code code, bool honor_nans)
2781 {
2782   if (honor_nans && flag_trapping_math)
2783     return ERROR_MARK;
2784
2785   switch (code)
2786     {
2787     case EQ_EXPR:
2788       return NE_EXPR;
2789     case NE_EXPR:
2790       return EQ_EXPR;
2791     case GT_EXPR:
2792       return honor_nans ? UNLE_EXPR : LE_EXPR;
2793     case GE_EXPR:
2794       return honor_nans ? UNLT_EXPR : LT_EXPR;
2795     case LT_EXPR:
2796       return honor_nans ? UNGE_EXPR : GE_EXPR;
2797     case LE_EXPR:
2798       return honor_nans ? UNGT_EXPR : GT_EXPR;
2799     case LTGT_EXPR:
2800       return UNEQ_EXPR;
2801     case UNEQ_EXPR:
2802       return LTGT_EXPR;
2803     case UNGT_EXPR:
2804       return LE_EXPR;
2805     case UNGE_EXPR:
2806       return LT_EXPR;
2807     case UNLT_EXPR:
2808       return GE_EXPR;
2809     case UNLE_EXPR:
2810       return GT_EXPR;
2811     case ORDERED_EXPR:
2812       return UNORDERED_EXPR;
2813     case UNORDERED_EXPR:
2814       return ORDERED_EXPR;
2815     default:
2816       gcc_unreachable ();
2817     }
2818 }
2819
2820 /* Similar, but return the comparison that results if the operands are
2821    swapped.  This is safe for floating-point.  */
2822
2823 enum tree_code
2824 swap_tree_comparison (enum tree_code code)
2825 {
2826   switch (code)
2827     {
2828     case EQ_EXPR:
2829     case NE_EXPR:
2830     case ORDERED_EXPR:
2831     case UNORDERED_EXPR:
2832     case LTGT_EXPR:
2833     case UNEQ_EXPR:
2834       return code;
2835     case GT_EXPR:
2836       return LT_EXPR;
2837     case GE_EXPR:
2838       return LE_EXPR;
2839     case LT_EXPR:
2840       return GT_EXPR;
2841     case LE_EXPR:
2842       return GE_EXPR;
2843     case UNGT_EXPR:
2844       return UNLT_EXPR;
2845     case UNGE_EXPR:
2846       return UNLE_EXPR;
2847     case UNLT_EXPR:
2848       return UNGT_EXPR;
2849     case UNLE_EXPR:
2850       return UNGE_EXPR;
2851     default:
2852       gcc_unreachable ();
2853     }
2854 }
2855
2856
2857 /* Convert a comparison tree code from an enum tree_code representation
2858    into a compcode bit-based encoding.  This function is the inverse of
2859    compcode_to_comparison.  */
2860
2861 static enum comparison_code
2862 comparison_to_compcode (enum tree_code code)
2863 {
2864   switch (code)
2865     {
2866     case LT_EXPR:
2867       return COMPCODE_LT;
2868     case EQ_EXPR:
2869       return COMPCODE_EQ;
2870     case LE_EXPR:
2871       return COMPCODE_LE;
2872     case GT_EXPR:
2873       return COMPCODE_GT;
2874     case NE_EXPR:
2875       return COMPCODE_NE;
2876     case GE_EXPR:
2877       return COMPCODE_GE;
2878     case ORDERED_EXPR:
2879       return COMPCODE_ORD;
2880     case UNORDERED_EXPR:
2881       return COMPCODE_UNORD;
2882     case UNLT_EXPR:
2883       return COMPCODE_UNLT;
2884     case UNEQ_EXPR:
2885       return COMPCODE_UNEQ;
2886     case UNLE_EXPR:
2887       return COMPCODE_UNLE;
2888     case UNGT_EXPR:
2889       return COMPCODE_UNGT;
2890     case LTGT_EXPR:
2891       return COMPCODE_LTGT;
2892     case UNGE_EXPR:
2893       return COMPCODE_UNGE;
2894     default:
2895       gcc_unreachable ();
2896     }
2897 }
2898
2899 /* Convert a compcode bit-based encoding of a comparison operator back
2900    to GCC's enum tree_code representation.  This function is the
2901    inverse of comparison_to_compcode.  */
2902
2903 static enum tree_code
2904 compcode_to_comparison (enum comparison_code code)
2905 {
2906   switch (code)
2907     {
2908     case COMPCODE_LT:
2909       return LT_EXPR;
2910     case COMPCODE_EQ:
2911       return EQ_EXPR;
2912     case COMPCODE_LE:
2913       return LE_EXPR;
2914     case COMPCODE_GT:
2915       return GT_EXPR;
2916     case COMPCODE_NE:
2917       return NE_EXPR;
2918     case COMPCODE_GE:
2919       return GE_EXPR;
2920     case COMPCODE_ORD:
2921       return ORDERED_EXPR;
2922     case COMPCODE_UNORD:
2923       return UNORDERED_EXPR;
2924     case COMPCODE_UNLT:
2925       return UNLT_EXPR;
2926     case COMPCODE_UNEQ:
2927       return UNEQ_EXPR;
2928     case COMPCODE_UNLE:
2929       return UNLE_EXPR;
2930     case COMPCODE_UNGT:
2931       return UNGT_EXPR;
2932     case COMPCODE_LTGT:
2933       return LTGT_EXPR;
2934     case COMPCODE_UNGE:
2935       return UNGE_EXPR;
2936     default:
2937       gcc_unreachable ();
2938     }
2939 }
2940
2941 /* Return a tree for the comparison which is the combination of
2942    doing the AND or OR (depending on CODE) of the two operations LCODE
2943    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2944    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2945    if this makes the transformation invalid.  */
2946
2947 tree
2948 combine_comparisons (enum tree_code code, enum tree_code lcode,
2949                      enum tree_code rcode, tree truth_type,
2950                      tree ll_arg, tree lr_arg)
2951 {
2952   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2953   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2954   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2955   int compcode;
2956
2957   switch (code)
2958     {
2959     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2960       compcode = lcompcode & rcompcode;
2961       break;
2962
2963     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2964       compcode = lcompcode | rcompcode;
2965       break;
2966
2967     default:
2968       return NULL_TREE;
2969     }
2970
2971   if (!honor_nans)
2972     {
2973       /* Eliminate unordered comparisons, as well as LTGT and ORD
2974          which are not used unless the mode has NaNs.  */
2975       compcode &= ~COMPCODE_UNORD;
2976       if (compcode == COMPCODE_LTGT)
2977         compcode = COMPCODE_NE;
2978       else if (compcode == COMPCODE_ORD)
2979         compcode = COMPCODE_TRUE;
2980     }
2981    else if (flag_trapping_math)
2982      {
2983         /* Check that the original operation and the optimized ones will trap
2984            under the same condition.  */
2985         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2986                      && (lcompcode != COMPCODE_EQ)
2987                      && (lcompcode != COMPCODE_ORD);
2988         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2989                      && (rcompcode != COMPCODE_EQ)
2990                      && (rcompcode != COMPCODE_ORD);
2991         bool trap = (compcode & COMPCODE_UNORD) == 0
2992                     && (compcode != COMPCODE_EQ)
2993                     && (compcode != COMPCODE_ORD);
2994
2995         /* In a short-circuited boolean expression the LHS might be
2996            such that the RHS, if evaluated, will never trap.  For
2997            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2998            if neither x nor y is NaN.  (This is a mixed blessing: for
2999            example, the expression above will never trap, hence
3000            optimizing it to x < y would be invalid).  */
3001         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3002             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3003           rtrap = false;
3004
3005         /* If the comparison was short-circuited, and only the RHS
3006            trapped, we may now generate a spurious trap.  */
3007         if (rtrap && !ltrap
3008             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3009           return NULL_TREE;
3010
3011         /* If we changed the conditions that cause a trap, we lose.  */
3012         if ((ltrap || rtrap) != trap)
3013           return NULL_TREE;
3014       }
3015
3016   if (compcode == COMPCODE_TRUE)
3017     return constant_boolean_node (true, truth_type);
3018   else if (compcode == COMPCODE_FALSE)
3019     return constant_boolean_node (false, truth_type);
3020   else
3021     {
3022       enum tree_code tcode;
3023
3024       tcode = compcode_to_comparison ((enum comparison_code) compcode);
3025       return fold_build2 (tcode, truth_type, ll_arg, lr_arg);
3026     }
3027 }
3028 \f
3029 /* Return nonzero if two operands (typically of the same tree node)
3030    are necessarily equal.  If either argument has side-effects this
3031    function returns zero.  FLAGS modifies behavior as follows:
3032
3033    If OEP_ONLY_CONST is set, only return nonzero for constants.
3034    This function tests whether the operands are indistinguishable;
3035    it does not test whether they are equal using C's == operation.
3036    The distinction is important for IEEE floating point, because
3037    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3038    (2) two NaNs may be indistinguishable, but NaN!=NaN.
3039
3040    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3041    even though it may hold multiple values during a function.
3042    This is because a GCC tree node guarantees that nothing else is
3043    executed between the evaluation of its "operands" (which may often
3044    be evaluated in arbitrary order).  Hence if the operands themselves
3045    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3046    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3047    unset means assuming isochronic (or instantaneous) tree equivalence.
3048    Unless comparing arbitrary expression trees, such as from different
3049    statements, this flag can usually be left unset.
3050
3051    If OEP_PURE_SAME is set, then pure functions with identical arguments
3052    are considered the same.  It is used when the caller has other ways
3053    to ensure that global memory is unchanged in between.  */
3054
3055 int
3056 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3057 {
3058   /* If either is ERROR_MARK, they aren't equal.  */
3059   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3060     return 0;
3061
3062   /* Check equality of integer constants before bailing out due to
3063      precision differences.  */
3064   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3065     return tree_int_cst_equal (arg0, arg1);
3066
3067   /* If both types don't have the same signedness, then we can't consider
3068      them equal.  We must check this before the STRIP_NOPS calls
3069      because they may change the signedness of the arguments.  As pointers
3070      strictly don't have a signedness, require either two pointers or
3071      two non-pointers as well.  */
3072   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3073       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3074     return 0;
3075
3076   /* If both types don't have the same precision, then it is not safe
3077      to strip NOPs.  */
3078   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3079     return 0;
3080
3081   STRIP_NOPS (arg0);
3082   STRIP_NOPS (arg1);
3083
3084   /* In case both args are comparisons but with different comparison
3085      code, try to swap the comparison operands of one arg to produce
3086      a match and compare that variant.  */
3087   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3088       && COMPARISON_CLASS_P (arg0)
3089       && COMPARISON_CLASS_P (arg1))
3090     {
3091       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3092
3093       if (TREE_CODE (arg0) == swap_code)
3094         return operand_equal_p (TREE_OPERAND (arg0, 0),
3095                                 TREE_OPERAND (arg1, 1), flags)
3096                && operand_equal_p (TREE_OPERAND (arg0, 1),
3097                                    TREE_OPERAND (arg1, 0), flags);
3098     }
3099
3100   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3101       /* This is needed for conversions and for COMPONENT_REF.
3102          Might as well play it safe and always test this.  */
3103       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3104       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3105       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3106     return 0;
3107
3108   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3109      We don't care about side effects in that case because the SAVE_EXPR
3110      takes care of that for us. In all other cases, two expressions are
3111      equal if they have no side effects.  If we have two identical
3112      expressions with side effects that should be treated the same due
3113      to the only side effects being identical SAVE_EXPR's, that will
3114      be detected in the recursive calls below.  */
3115   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3116       && (TREE_CODE (arg0) == SAVE_EXPR
3117           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3118     return 1;
3119
3120   /* Next handle constant cases, those for which we can return 1 even
3121      if ONLY_CONST is set.  */
3122   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3123     switch (TREE_CODE (arg0))
3124       {
3125       case INTEGER_CST:
3126         return tree_int_cst_equal (arg0, arg1);
3127
3128       case FIXED_CST:
3129         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3130                                        TREE_FIXED_CST (arg1));
3131
3132       case REAL_CST:
3133         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3134                                    TREE_REAL_CST (arg1)))
3135           return 1;
3136
3137         
3138         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3139           {
3140             /* If we do not distinguish between signed and unsigned zero,
3141                consider them equal.  */
3142             if (real_zerop (arg0) && real_zerop (arg1))
3143               return 1;
3144           }
3145         return 0;
3146
3147       case VECTOR_CST:
3148         {
3149           tree v1, v2;
3150
3151           v1 = TREE_VECTOR_CST_ELTS (arg0);
3152           v2 = TREE_VECTOR_CST_ELTS (arg1);
3153           while (v1 && v2)
3154             {
3155               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3156                                     flags))
3157                 return 0;
3158               v1 = TREE_CHAIN (v1);
3159               v2 = TREE_CHAIN (v2);
3160             }
3161
3162           return v1 == v2;
3163         }
3164
3165       case COMPLEX_CST:
3166         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3167                                  flags)
3168                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3169                                     flags));
3170
3171       case STRING_CST:
3172         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3173                 && ! memcmp (TREE_STRING_POINTER (arg0),
3174                               TREE_STRING_POINTER (arg1),
3175                               TREE_STRING_LENGTH (arg0)));
3176
3177       case ADDR_EXPR:
3178         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3179                                 0);
3180       default:
3181         break;
3182       }
3183
3184   if (flags & OEP_ONLY_CONST)
3185     return 0;
3186
3187 /* Define macros to test an operand from arg0 and arg1 for equality and a
3188    variant that allows null and views null as being different from any
3189    non-null value.  In the latter case, if either is null, the both
3190    must be; otherwise, do the normal comparison.  */
3191 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3192                                     TREE_OPERAND (arg1, N), flags)
3193
3194 #define OP_SAME_WITH_NULL(N)                            \
3195   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3196    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3197
3198   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3199     {
3200     case tcc_unary:
3201       /* Two conversions are equal only if signedness and modes match.  */
3202       switch (TREE_CODE (arg0))
3203         {
3204         CASE_CONVERT:
3205         case FIX_TRUNC_EXPR:
3206           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3207               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3208             return 0;
3209           break;
3210         default:
3211           break;
3212         }
3213
3214       return OP_SAME (0);
3215
3216
3217     case tcc_comparison:
3218     case tcc_binary:
3219       if (OP_SAME (0) && OP_SAME (1))
3220         return 1;
3221
3222       /* For commutative ops, allow the other order.  */
3223       return (commutative_tree_code (TREE_CODE (arg0))
3224               && operand_equal_p (TREE_OPERAND (arg0, 0),
3225                                   TREE_OPERAND (arg1, 1), flags)
3226               && operand_equal_p (TREE_OPERAND (arg0, 1),
3227                                   TREE_OPERAND (arg1, 0), flags));
3228
3229     case tcc_reference:
3230       /* If either of the pointer (or reference) expressions we are
3231          dereferencing contain a side effect, these cannot be equal.  */
3232       if (TREE_SIDE_EFFECTS (arg0)
3233           || TREE_SIDE_EFFECTS (arg1))
3234         return 0;
3235
3236       switch (TREE_CODE (arg0))
3237         {
3238         case INDIRECT_REF:
3239         case ALIGN_INDIRECT_REF:
3240         case MISALIGNED_INDIRECT_REF:
3241         case REALPART_EXPR:
3242         case IMAGPART_EXPR:
3243           return OP_SAME (0);
3244
3245         case ARRAY_REF:
3246         case ARRAY_RANGE_REF:
3247           /* Operands 2 and 3 may be null.
3248              Compare the array index by value if it is constant first as we
3249              may have different types but same value here.  */
3250           return (OP_SAME (0)
3251                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3252                                           TREE_OPERAND (arg1, 1))
3253                       || OP_SAME (1))
3254                   && OP_SAME_WITH_NULL (2)
3255                   && OP_SAME_WITH_NULL (3));
3256
3257         case COMPONENT_REF:
3258           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3259              may be NULL when we're called to compare MEM_EXPRs.  */
3260           return OP_SAME_WITH_NULL (0)
3261                  && OP_SAME (1)
3262                  && OP_SAME_WITH_NULL (2);
3263
3264         case BIT_FIELD_REF:
3265           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3266
3267         default:
3268           return 0;
3269         }
3270
3271     case tcc_expression:
3272       switch (TREE_CODE (arg0))
3273         {
3274         case ADDR_EXPR:
3275         case TRUTH_NOT_EXPR:
3276           return OP_SAME (0);
3277
3278         case TRUTH_ANDIF_EXPR:
3279         case TRUTH_ORIF_EXPR:
3280           return OP_SAME (0) && OP_SAME (1);
3281
3282         case TRUTH_AND_EXPR:
3283         case TRUTH_OR_EXPR:
3284         case TRUTH_XOR_EXPR:
3285           if (OP_SAME (0) && OP_SAME (1))
3286             return 1;
3287
3288           /* Otherwise take into account this is a commutative operation.  */
3289           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3290                                    TREE_OPERAND (arg1, 1), flags)
3291                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3292                                       TREE_OPERAND (arg1, 0), flags));
3293
3294         case COND_EXPR:
3295           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3296           
3297         default:
3298           return 0;
3299         }
3300
3301     case tcc_vl_exp:
3302       switch (TREE_CODE (arg0))
3303         {
3304         case CALL_EXPR:
3305           /* If the CALL_EXPRs call different functions, then they
3306              clearly can not be equal.  */
3307           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3308                                  flags))
3309             return 0;
3310
3311           {
3312             unsigned int cef = call_expr_flags (arg0);
3313             if (flags & OEP_PURE_SAME)
3314               cef &= ECF_CONST | ECF_PURE;
3315             else
3316               cef &= ECF_CONST;
3317             if (!cef)
3318               return 0;
3319           }
3320
3321           /* Now see if all the arguments are the same.  */
3322           {
3323             const_call_expr_arg_iterator iter0, iter1;
3324             const_tree a0, a1;
3325             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3326                    a1 = first_const_call_expr_arg (arg1, &iter1);
3327                  a0 && a1;
3328                  a0 = next_const_call_expr_arg (&iter0),
3329                    a1 = next_const_call_expr_arg (&iter1))
3330               if (! operand_equal_p (a0, a1, flags))
3331                 return 0;
3332
3333             /* If we get here and both argument lists are exhausted
3334                then the CALL_EXPRs are equal.  */
3335             return ! (a0 || a1);
3336           }
3337         default:
3338           return 0;
3339         }
3340
3341     case tcc_declaration:
3342       /* Consider __builtin_sqrt equal to sqrt.  */
3343       return (TREE_CODE (arg0) == FUNCTION_DECL
3344               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3345               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3346               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3347
3348     default:
3349       return 0;
3350     }
3351
3352 #undef OP_SAME
3353 #undef OP_SAME_WITH_NULL
3354 }
3355 \f
3356 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3357    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3358
3359    When in doubt, return 0.  */
3360
3361 static int
3362 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3363 {
3364   int unsignedp1, unsignedpo;
3365   tree primarg0, primarg1, primother;
3366   unsigned int correct_width;
3367
3368   if (operand_equal_p (arg0, arg1, 0))
3369     return 1;
3370
3371   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3372       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3373     return 0;
3374
3375   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3376      and see if the inner values are the same.  This removes any
3377      signedness comparison, which doesn't matter here.  */
3378   primarg0 = arg0, primarg1 = arg1;
3379   STRIP_NOPS (primarg0);
3380   STRIP_NOPS (primarg1);
3381   if (operand_equal_p (primarg0, primarg1, 0))
3382     return 1;
3383
3384   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3385      actual comparison operand, ARG0.
3386
3387      First throw away any conversions to wider types
3388      already present in the operands.  */
3389
3390   primarg1 = get_narrower (arg1, &unsignedp1);
3391   primother = get_narrower (other, &unsignedpo);
3392
3393   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3394   if (unsignedp1 == unsignedpo
3395       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3396       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3397     {
3398       tree type = TREE_TYPE (arg0);
3399
3400       /* Make sure shorter operand is extended the right way
3401          to match the longer operand.  */
3402       primarg1 = fold_convert (signed_or_unsigned_type_for
3403                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3404
3405       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3406         return 1;
3407     }
3408
3409   return 0;
3410 }
3411 \f
3412 /* See if ARG is an expression that is either a comparison or is performing
3413    arithmetic on comparisons.  The comparisons must only be comparing
3414    two different values, which will be stored in *CVAL1 and *CVAL2; if
3415    they are nonzero it means that some operands have already been found.
3416    No variables may be used anywhere else in the expression except in the
3417    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3418    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3419
3420    If this is true, return 1.  Otherwise, return zero.  */
3421
3422 static int
3423 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3424 {
3425   enum tree_code code = TREE_CODE (arg);
3426   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3427
3428   /* We can handle some of the tcc_expression cases here.  */
3429   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3430     tclass = tcc_unary;
3431   else if (tclass == tcc_expression
3432            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3433                || code == COMPOUND_EXPR))
3434     tclass = tcc_binary;
3435
3436   else if (tclass == tcc_expression && code == SAVE_EXPR
3437            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3438     {
3439       /* If we've already found a CVAL1 or CVAL2, this expression is
3440          two complex to handle.  */
3441       if (*cval1 || *cval2)
3442         return 0;
3443
3444       tclass = tcc_unary;
3445       *save_p = 1;
3446     }
3447
3448   switch (tclass)
3449     {
3450     case tcc_unary:
3451       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3452
3453     case tcc_binary:
3454       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3455               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3456                                       cval1, cval2, save_p));
3457
3458     case tcc_constant:
3459       return 1;
3460
3461     case tcc_expression:
3462       if (code == COND_EXPR)
3463         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3464                                      cval1, cval2, save_p)
3465                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3466                                         cval1, cval2, save_p)
3467                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3468                                         cval1, cval2, save_p));
3469       return 0;
3470
3471     case tcc_comparison:
3472       /* First see if we can handle the first operand, then the second.  For
3473          the second operand, we know *CVAL1 can't be zero.  It must be that
3474          one side of the comparison is each of the values; test for the
3475          case where this isn't true by failing if the two operands
3476          are the same.  */
3477
3478       if (operand_equal_p (TREE_OPERAND (arg, 0),
3479                            TREE_OPERAND (arg, 1), 0))
3480         return 0;
3481
3482       if (*cval1 == 0)
3483         *cval1 = TREE_OPERAND (arg, 0);
3484       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3485         ;
3486       else if (*cval2 == 0)
3487         *cval2 = TREE_OPERAND (arg, 0);
3488       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3489         ;
3490       else
3491         return 0;
3492
3493       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3494         ;
3495       else if (*cval2 == 0)
3496         *cval2 = TREE_OPERAND (arg, 1);
3497       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3498         ;
3499       else
3500         return 0;
3501
3502       return 1;
3503
3504     default:
3505       return 0;
3506     }
3507 }
3508 \f
3509 /* ARG is a tree that is known to contain just arithmetic operations and
3510    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3511    any occurrence of OLD0 as an operand of a comparison and likewise for
3512    NEW1 and OLD1.  */
3513
3514 static tree
3515 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3516 {
3517   tree type = TREE_TYPE (arg);
3518   enum tree_code code = TREE_CODE (arg);
3519   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3520
3521   /* We can handle some of the tcc_expression cases here.  */
3522   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3523     tclass = tcc_unary;
3524   else if (tclass == tcc_expression
3525            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3526     tclass = tcc_binary;
3527
3528   switch (tclass)
3529     {
3530     case tcc_unary:
3531       return fold_build1 (code, type,
3532                           eval_subst (TREE_OPERAND (arg, 0),
3533                                       old0, new0, old1, new1));
3534
3535     case tcc_binary:
3536       return fold_build2 (code, type,
3537                           eval_subst (TREE_OPERAND (arg, 0),
3538                                       old0, new0, old1, new1),
3539                           eval_subst (TREE_OPERAND (arg, 1),
3540                                       old0, new0, old1, new1));
3541
3542     case tcc_expression:
3543       switch (code)
3544         {
3545         case SAVE_EXPR:
3546           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3547
3548         case COMPOUND_EXPR:
3549           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3550
3551         case COND_EXPR:
3552           return fold_build3 (code, type,
3553                               eval_subst (TREE_OPERAND (arg, 0),
3554                                           old0, new0, old1, new1),
3555                               eval_subst (TREE_OPERAND (arg, 1),
3556                                           old0, new0, old1, new1),
3557                               eval_subst (TREE_OPERAND (arg, 2),
3558                                           old0, new0, old1, new1));
3559         default:
3560           break;
3561         }
3562       /* Fall through - ???  */
3563
3564     case tcc_comparison:
3565       {
3566         tree arg0 = TREE_OPERAND (arg, 0);
3567         tree arg1 = TREE_OPERAND (arg, 1);
3568
3569         /* We need to check both for exact equality and tree equality.  The
3570            former will be true if the operand has a side-effect.  In that
3571            case, we know the operand occurred exactly once.  */
3572
3573         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3574           arg0 = new0;
3575         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3576           arg0 = new1;
3577
3578         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3579           arg1 = new0;
3580         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3581           arg1 = new1;
3582
3583         return fold_build2 (code, type, arg0, arg1);
3584       }
3585
3586     default:
3587       return arg;
3588     }
3589 }
3590 \f
3591 /* Return a tree for the case when the result of an expression is RESULT
3592    converted to TYPE and OMITTED was previously an operand of the expression
3593    but is now not needed (e.g., we folded OMITTED * 0).
3594
3595    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3596    the conversion of RESULT to TYPE.  */
3597
3598 tree
3599 omit_one_operand (tree type, tree result, tree omitted)
3600 {
3601   tree t = fold_convert (type, result);
3602
3603   /* If the resulting operand is an empty statement, just return the omitted
3604      statement casted to void. */
3605   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3606     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3607
3608   if (TREE_SIDE_EFFECTS (omitted))
3609     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3610
3611   return non_lvalue (t);
3612 }
3613
3614 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3615
3616 static tree
3617 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3618 {
3619   tree t = fold_convert (type, result);
3620
3621   /* If the resulting operand is an empty statement, just return the omitted
3622      statement casted to void. */
3623   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3624     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3625
3626   if (TREE_SIDE_EFFECTS (omitted))
3627     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3628
3629   return pedantic_non_lvalue (t);
3630 }
3631
3632 /* Return a tree for the case when the result of an expression is RESULT
3633    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3634    of the expression but are now not needed.
3635
3636    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3637    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3638    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3639    just do the conversion of RESULT to TYPE.  */
3640
3641 tree
3642 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3643 {
3644   tree t = fold_convert (type, result);
3645
3646   if (TREE_SIDE_EFFECTS (omitted2))
3647     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3648   if (TREE_SIDE_EFFECTS (omitted1))
3649     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3650
3651   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3652 }
3653
3654 \f
3655 /* Return a simplified tree node for the truth-negation of ARG.  This
3656    never alters ARG itself.  We assume that ARG is an operation that
3657    returns a truth value (0 or 1).
3658
3659    FIXME: one would think we would fold the result, but it causes
3660    problems with the dominator optimizer.  */
3661
3662 tree
3663 fold_truth_not_expr (tree arg)
3664 {
3665   tree t, type = TREE_TYPE (arg);
3666   enum tree_code code = TREE_CODE (arg);
3667
3668   /* If this is a comparison, we can simply invert it, except for
3669      floating-point non-equality comparisons, in which case we just
3670      enclose a TRUTH_NOT_EXPR around what we have.  */
3671
3672   if (TREE_CODE_CLASS (code) == tcc_comparison)
3673     {
3674       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3675       if (FLOAT_TYPE_P (op_type)
3676           && flag_trapping_math
3677           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3678           && code != NE_EXPR && code != EQ_EXPR)
3679         return NULL_TREE;
3680
3681       code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3682       if (code == ERROR_MARK)
3683         return NULL_TREE;
3684
3685       t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3686       if (EXPR_HAS_LOCATION (arg))
3687         SET_EXPR_LOCATION (t, EXPR_LOCATION (arg));
3688       return t;
3689     }
3690
3691   switch (code)
3692     {
3693     case INTEGER_CST:
3694       return constant_boolean_node (integer_zerop (arg), type);
3695
3696     case TRUTH_AND_EXPR:
3697       t = build2 (TRUTH_OR_EXPR, type,
3698                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3699                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3700       break;
3701
3702     case TRUTH_OR_EXPR:
3703       t = build2 (TRUTH_AND_EXPR, type,
3704                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3705                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3706       break;
3707
3708     case TRUTH_XOR_EXPR:
3709       /* Here we can invert either operand.  We invert the first operand
3710          unless the second operand is a TRUTH_NOT_EXPR in which case our
3711          result is the XOR of the first operand with the inside of the
3712          negation of the second operand.  */
3713
3714       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3715         t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3716                     TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3717       else
3718         t = build2 (TRUTH_XOR_EXPR, type,
3719                     invert_truthvalue (TREE_OPERAND (arg, 0)),
3720                     TREE_OPERAND (arg, 1));
3721       break;
3722
3723     case TRUTH_ANDIF_EXPR:
3724       t = build2 (TRUTH_ORIF_EXPR, type,
3725                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3726                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3727       break;
3728
3729     case TRUTH_ORIF_EXPR:
3730       t = build2 (TRUTH_ANDIF_EXPR, type,
3731                   invert_truthvalue (TREE_OPERAND (arg, 0)),
3732                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3733       break;
3734
3735     case TRUTH_NOT_EXPR:
3736       return TREE_OPERAND (arg, 0);
3737
3738     case COND_EXPR:
3739       {
3740         tree arg1 = TREE_OPERAND (arg, 1);
3741         tree arg2 = TREE_OPERAND (arg, 2);
3742         /* A COND_EXPR may have a throw as one operand, which
3743            then has void type.  Just leave void operands
3744            as they are.  */
3745         t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3746                     VOID_TYPE_P (TREE_TYPE (arg1))
3747                     ? arg1 : invert_truthvalue (arg1),
3748                     VOID_TYPE_P (TREE_TYPE (arg2))
3749                     ? arg2 : invert_truthvalue (arg2));
3750         break;
3751       }
3752
3753     case COMPOUND_EXPR:
3754       t = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3755                   invert_truthvalue (TREE_OPERAND (arg, 1)));
3756       break;
3757
3758     case NON_LVALUE_EXPR:
3759       return invert_truthvalue (TREE_OPERAND (arg, 0));
3760
3761     CASE_CONVERT:
3762       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3763         {
3764           t = build1 (TRUTH_NOT_EXPR, type, arg);
3765           break;
3766         }
3767
3768       /* ... fall through ...  */
3769
3770     case FLOAT_EXPR:
3771       t = build1 (TREE_CODE (arg), type,
3772                   invert_truthvalue (TREE_OPERAND (arg, 0)));
3773       break;
3774
3775     case BIT_AND_EXPR:
3776       if (!integer_onep (TREE_OPERAND (arg, 1)))
3777         return NULL_TREE;
3778       t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3779       break;
3780
3781     case SAVE_EXPR:
3782       t = build1 (TRUTH_NOT_EXPR, type, arg);
3783       break;
3784
3785     case CLEANUP_POINT_EXPR:
3786       t = build1 (CLEANUP_POINT_EXPR, type,
3787                   invert_truthvalue (TREE_OPERAND (arg, 0)));
3788       break;
3789
3790     default:
3791       t = NULL_TREE;
3792       break;
3793     }
3794
3795   if (t && EXPR_HAS_LOCATION (arg))
3796     SET_EXPR_LOCATION (t, EXPR_LOCATION (arg));
3797
3798   return t;
3799 }
3800
3801 /* Return a simplified tree node for the truth-negation of ARG.  This
3802    never alters ARG itself.  We assume that ARG is an operation that
3803    returns a truth value (0 or 1).
3804
3805    FIXME: one would think we would fold the result, but it causes
3806    problems with the dominator optimizer.  */
3807
3808 tree
3809 invert_truthvalue (tree arg)
3810 {
3811   tree tem;
3812
3813   if (TREE_CODE (arg) == ERROR_MARK)
3814     return arg;
3815
3816   tem = fold_truth_not_expr (arg);
3817   if (!tem)
3818     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3819
3820   return tem;
3821 }
3822
3823 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3824    operands are another bit-wise operation with a common input.  If so,
3825    distribute the bit operations to save an operation and possibly two if
3826    constants are involved.  For example, convert
3827         (A | B) & (A | C) into A | (B & C)
3828    Further simplification will occur if B and C are constants.
3829
3830    If this optimization cannot be done, 0 will be returned.  */
3831
3832 static tree
3833 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3834 {
3835   tree common;
3836   tree left, right;
3837
3838   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3839       || TREE_CODE (arg0) == code
3840       || (TREE_CODE (arg0) != BIT_AND_EXPR
3841           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3842     return 0;
3843
3844   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3845     {
3846       common = TREE_OPERAND (arg0, 0);
3847       left = TREE_OPERAND (arg0, 1);
3848       right = TREE_OPERAND (arg1, 1);
3849     }
3850   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3851     {
3852       common = TREE_OPERAND (arg0, 0);
3853       left = TREE_OPERAND (arg0, 1);
3854       right = TREE_OPERAND (arg1, 0);
3855     }
3856   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3857     {
3858       common = TREE_OPERAND (arg0, 1);
3859       left = TREE_OPERAND (arg0, 0);
3860       right = TREE_OPERAND (arg1, 1);
3861     }
3862   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3863     {
3864       common = TREE_OPERAND (arg0, 1);
3865       left = TREE_OPERAND (arg0, 0);
3866       right = TREE_OPERAND (arg1, 0);
3867     }
3868   else
3869     return 0;
3870
3871   common = fold_convert (type, common);
3872   left = fold_convert (type, left);
3873   right = fold_convert (type, right);
3874   return fold_build2 (TREE_CODE (arg0), type, common,
3875                       fold_build2 (code, type, left, right));
3876 }
3877
3878 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3879    with code CODE.  This optimization is unsafe.  */
3880 static tree
3881 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3882 {
3883   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3884   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3885
3886   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3887   if (mul0 == mul1
3888       && operand_equal_p (TREE_OPERAND (arg0, 1),
3889                        TREE_OPERAND (arg1, 1), 0))
3890     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3891                         fold_build2 (code, type,
3892                                      TREE_OPERAND (arg0, 0),
3893                                      TREE_OPERAND (arg1, 0)),
3894                         TREE_OPERAND (arg0, 1));
3895
3896   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3897   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3898                        TREE_OPERAND (arg1, 0), 0)
3899       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3900       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3901     {
3902       REAL_VALUE_TYPE r0, r1;
3903       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3904       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3905       if (!mul0)
3906         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3907       if (!mul1)
3908         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3909       real_arithmetic (&r0, code, &r0, &r1);
3910       return fold_build2 (MULT_EXPR, type,
3911                           TREE_OPERAND (arg0, 0),
3912                           build_real (type, r0));
3913     }
3914
3915   return NULL_TREE;
3916 }
3917 \f
3918 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3919    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3920
3921 static tree
3922 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3923                     HOST_WIDE_INT bitpos, int unsignedp)
3924 {
3925   tree result, bftype;
3926
3927   if (bitpos == 0)
3928     {
3929       tree size = TYPE_SIZE (TREE_TYPE (inner));
3930       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3931            || POINTER_TYPE_P (TREE_TYPE (inner)))
3932           && host_integerp (size, 0) 
3933           && tree_low_cst (size, 0) == bitsize)
3934         return fold_convert (type, inner);
3935     }
3936
3937   bftype = type;
3938   if (TYPE_PRECISION (bftype) != bitsize
3939       || TYPE_UNSIGNED (bftype) == !unsignedp)
3940     bftype = build_nonstandard_integer_type (bitsize, 0);
3941
3942   result = build3 (BIT_FIELD_REF, bftype, inner,
3943                    size_int (bitsize), bitsize_int (bitpos));
3944
3945   if (bftype != type)
3946     result = fold_convert (type, result);
3947
3948   return result;
3949 }
3950
3951 /* Optimize a bit-field compare.
3952
3953    There are two cases:  First is a compare against a constant and the
3954    second is a comparison of two items where the fields are at the same
3955    bit position relative to the start of a chunk (byte, halfword, word)
3956    large enough to contain it.  In these cases we can avoid the shift
3957    implicit in bitfield extractions.
3958
3959    For constants, we emit a compare of the shifted constant with the
3960    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3961    compared.  For two fields at the same position, we do the ANDs with the
3962    similar mask and compare the result of the ANDs.
3963
3964    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3965    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3966    are the left and right operands of the comparison, respectively.
3967
3968    If the optimization described above can be done, we return the resulting
3969    tree.  Otherwise we return zero.  */
3970
3971 static tree
3972 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3973                             tree lhs, tree rhs)
3974 {
3975   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3976   tree type = TREE_TYPE (lhs);
3977   tree signed_type, unsigned_type;
3978   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3979   enum machine_mode lmode, rmode, nmode;
3980   int lunsignedp, runsignedp;
3981   int lvolatilep = 0, rvolatilep = 0;
3982   tree linner, rinner = NULL_TREE;
3983   tree mask;
3984   tree offset;
3985
3986   /* Get all the information about the extractions being done.  If the bit size
3987      if the same as the size of the underlying object, we aren't doing an
3988      extraction at all and so can do nothing.  We also don't want to
3989      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3990      then will no longer be able to replace it.  */
3991   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3992                                 &lunsignedp, &lvolatilep, false);
3993   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3994       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3995     return 0;
3996
3997  if (!const_p)
3998    {
3999      /* If this is not a constant, we can only do something if bit positions,
4000         sizes, and signedness are the same.  */
4001      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4002                                    &runsignedp, &rvolatilep, false);
4003
4004      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
4005          || lunsignedp != runsignedp || offset != 0
4006          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
4007        return 0;
4008    }
4009
4010   /* See if we can find a mode to refer to this field.  We should be able to,
4011      but fail if we can't.  */
4012   nmode = get_best_mode (lbitsize, lbitpos,
4013                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4014                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4015                                 TYPE_ALIGN (TREE_TYPE (rinner))),
4016                          word_mode, lvolatilep || rvolatilep);
4017   if (nmode == VOIDmode)
4018     return 0;
4019
4020   /* Set signed and unsigned types of the precision of this mode for the
4021      shifts below.  */
4022   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4023   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4024
4025   /* Compute the bit position and size for the new reference and our offset
4026      within it. If the new reference is the same size as the original, we
4027      won't optimize anything, so return zero.  */
4028   nbitsize = GET_MODE_BITSIZE (nmode);
4029   nbitpos = lbitpos & ~ (nbitsize - 1);
4030   lbitpos -= nbitpos;
4031   if (nbitsize == lbitsize)
4032     return 0;
4033
4034   if (BYTES_BIG_ENDIAN)
4035     lbitpos = nbitsize - lbitsize - lbitpos;
4036
4037   /* Make the mask to be used against the extracted field.  */
4038   mask = build_int_cst_type (unsigned_type, -1);
4039   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4040   mask = const_binop (RSHIFT_EXPR, mask,
4041                       size_int (nbitsize - lbitsize - lbitpos), 0);
4042
4043   if (! const_p)
4044     /* If not comparing with constant, just rework the comparison
4045        and return.  */
4046     return fold_build2 (code, compare_type,
4047                         fold_build2 (BIT_AND_EXPR, unsigned_type,
4048                                      make_bit_field_ref (linner,
4049                                                          unsigned_type,
4050                                                          nbitsize, nbitpos,
4051                                                          1),
4052                                      mask),
4053                         fold_build2 (BIT_AND_EXPR, unsigned_type,
4054                                      make_bit_field_ref (rinner,
4055                                                          unsigned_type,
4056                                                          nbitsize, nbitpos,
4057                                                          1),
4058                                      mask));
4059
4060   /* Otherwise, we are handling the constant case. See if the constant is too
4061      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4062      this not only for its own sake, but to avoid having to test for this
4063      error case below.  If we didn't, we might generate wrong code.
4064
4065      For unsigned fields, the constant shifted right by the field length should
4066      be all zero.  For signed fields, the high-order bits should agree with
4067      the sign bit.  */
4068
4069   if (lunsignedp)
4070     {
4071       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4072                                         fold_convert (unsigned_type, rhs),
4073                                         size_int (lbitsize), 0)))
4074         {
4075           warning (0, "comparison is always %d due to width of bit-field",
4076                    code == NE_EXPR);
4077           return constant_boolean_node (code == NE_EXPR, compare_type);
4078         }
4079     }
4080   else
4081     {
4082       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4083                               size_int (lbitsize - 1), 0);
4084       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4085         {
4086           warning (0, "comparison is always %d due to width of bit-field",
4087                    code == NE_EXPR);
4088           return constant_boolean_node (code == NE_EXPR, compare_type);
4089         }
4090     }
4091
4092   /* Single-bit compares should always be against zero.  */
4093   if (lbitsize == 1 && ! integer_zerop (rhs))
4094     {
4095       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4096       rhs = build_int_cst (type, 0);
4097     }
4098
4099   /* Make a new bitfield reference, shift the constant over the
4100      appropriate number of bits and mask it with the computed mask
4101      (in case this was a signed field).  If we changed it, make a new one.  */
4102   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4103   if (lvolatilep)
4104     {
4105       TREE_SIDE_EFFECTS (lhs) = 1;
4106       TREE_THIS_VOLATILE (lhs) = 1;
4107     }
4108
4109   rhs = const_binop (BIT_AND_EXPR,
4110                      const_binop (LSHIFT_EXPR,
4111                                   fold_convert (unsigned_type, rhs),
4112                                   size_int (lbitpos), 0),
4113                      mask, 0);
4114
4115   return build2 (code, compare_type,
4116                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4117                  rhs);
4118 }
4119 \f
4120 /* Subroutine for fold_truthop: decode a field reference.
4121
4122    If EXP is a comparison reference, we return the innermost reference.
4123
4124    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4125    set to the starting bit number.
4126
4127    If the innermost field can be completely contained in a mode-sized
4128    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4129
4130    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4131    otherwise it is not changed.
4132
4133    *PUNSIGNEDP is set to the signedness of the field.
4134
4135    *PMASK is set to the mask used.  This is either contained in a
4136    BIT_AND_EXPR or derived from the width of the field.
4137
4138    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4139
4140    Return 0 if this is not a component reference or is one that we can't
4141    do anything with.  */
4142
4143 static tree
4144 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4145                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4146                         int *punsignedp, int *pvolatilep,
4147                         tree *pmask, tree *pand_mask)
4148 {
4149   tree outer_type = 0;
4150   tree and_mask = 0;
4151   tree mask, inner, offset;
4152   tree unsigned_type;
4153   unsigned int precision;
4154
4155   /* All the optimizations using this function assume integer fields.
4156      There are problems with FP fields since the type_for_size call
4157      below can fail for, e.g., XFmode.  */
4158   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4159     return 0;
4160
4161   /* We are interested in the bare arrangement of bits, so strip everything
4162      that doesn't affect the machine mode.  However, record the type of the
4163      outermost expression if it may matter below.  */
4164   if (CONVERT_EXPR_P (exp)
4165       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4166     outer_type = TREE_TYPE (exp);
4167   STRIP_NOPS (exp);
4168
4169   if (TREE_CODE (exp) == BIT_AND_EXPR)
4170     {
4171       and_mask = TREE_OPERAND (exp, 1);
4172       exp = TREE_OPERAND (exp, 0);
4173       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4174       if (TREE_CODE (and_mask) != INTEGER_CST)
4175         return 0;
4176     }
4177
4178   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4179                                punsignedp, pvolatilep, false);
4180   if ((inner == exp && and_mask == 0)
4181       || *pbitsize < 0 || offset != 0
4182       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4183     return 0;
4184
4185   /* If the number of bits in the reference is the same as the bitsize of
4186      the outer type, then the outer type gives the signedness. Otherwise
4187      (in case of a small bitfield) the signedness is unchanged.  */
4188   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4189     *punsignedp = TYPE_UNSIGNED (outer_type);
4190
4191   /* Compute the mask to access the bitfield.  */
4192   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4193   precision = TYPE_PRECISION (unsigned_type);
4194
4195   mask = build_int_cst_type (unsigned_type, -1);
4196
4197   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4198   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4199
4200   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4201   if (and_mask != 0)
4202     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4203                         fold_convert (unsigned_type, and_mask), mask);
4204
4205   *pmask = mask;
4206   *pand_mask = and_mask;
4207   return inner;
4208 }
4209
4210 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4211    bit positions.  */
4212
4213 static int
4214 all_ones_mask_p (const_tree mask, int size)
4215 {
4216   tree type = TREE_TYPE (mask);
4217   unsigned int precision = TYPE_PRECISION (type);
4218   tree tmask;
4219
4220   tmask = build_int_cst_type (signed_type_for (type), -1);
4221
4222   return
4223     tree_int_cst_equal (mask,
4224                         const_binop (RSHIFT_EXPR,
4225                                      const_binop (LSHIFT_EXPR, tmask,
4226                                                   size_int (precision - size),
4227                                                   0),
4228                                      size_int (precision - size), 0));
4229 }
4230
4231 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4232    represents the sign bit of EXP's type.  If EXP represents a sign
4233    or zero extension, also test VAL against the unextended type.
4234    The return value is the (sub)expression whose sign bit is VAL,
4235    or NULL_TREE otherwise.  */
4236
4237 static tree
4238 sign_bit_p (tree exp, const_tree val)
4239 {
4240   unsigned HOST_WIDE_INT mask_lo, lo;
4241   HOST_WIDE_INT mask_hi, hi;
4242   int width;
4243   tree t;
4244
4245   /* Tree EXP must have an integral type.  */
4246   t = TREE_TYPE (exp);
4247   if (! INTEGRAL_TYPE_P (t))
4248     return NULL_TREE;
4249
4250   /* Tree VAL must be an integer constant.  */
4251   if (TREE_CODE (val) != INTEGER_CST
4252       || TREE_OVERFLOW (val))
4253     return NULL_TREE;
4254
4255   width = TYPE_PRECISION (t);
4256   if (width > HOST_BITS_PER_WIDE_INT)
4257     {
4258       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4259       lo = 0;
4260
4261       mask_hi = ((unsigned HOST_WIDE_INT) -1
4262                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4263       mask_lo = -1;
4264     }
4265   else
4266     {
4267       hi = 0;
4268       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4269
4270       mask_hi = 0;
4271       mask_lo = ((unsigned HOST_WIDE_INT) -1
4272                  >> (HOST_BITS_PER_WIDE_INT - width));
4273     }
4274
4275   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4276      treat VAL as if it were unsigned.  */
4277   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4278       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4279     return exp;
4280
4281   /* Handle extension from a narrower type.  */
4282   if (TREE_CODE (exp) == NOP_EXPR
4283       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4284     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4285
4286   return NULL_TREE;
4287 }
4288
4289 /* Subroutine for fold_truthop: determine if an operand is simple enough
4290    to be evaluated unconditionally.  */
4291
4292 static int
4293 simple_operand_p (const_tree exp)
4294 {
4295   /* Strip any conversions that don't change the machine mode.  */
4296   STRIP_NOPS (exp);
4297
4298   return (CONSTANT_CLASS_P (exp)
4299           || TREE_CODE (exp) == SSA_NAME
4300           || (DECL_P (exp)
4301               && ! TREE_ADDRESSABLE (exp)
4302               && ! TREE_THIS_VOLATILE (exp)
4303               && ! DECL_NONLOCAL (exp)
4304               /* Don't regard global variables as simple.  They may be
4305                  allocated in ways unknown to the compiler (shared memory,
4306                  #pragma weak, etc).  */
4307               && ! TREE_PUBLIC (exp)
4308               && ! DECL_EXTERNAL (exp)
4309               /* Loading a static variable is unduly expensive, but global
4310                  registers aren't expensive.  */
4311               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4312 }
4313 \f
4314 /* The following functions are subroutines to fold_range_test and allow it to
4315    try to change a logical combination of comparisons into a range test.
4316
4317    For example, both
4318         X == 2 || X == 3 || X == 4 || X == 5
4319    and
4320         X >= 2 && X <= 5
4321    are converted to
4322         (unsigned) (X - 2) <= 3
4323
4324    We describe each set of comparisons as being either inside or outside
4325    a range, using a variable named like IN_P, and then describe the
4326    range with a lower and upper bound.  If one of the bounds is omitted,
4327    it represents either the highest or lowest value of the type.
4328
4329    In the comments below, we represent a range by two numbers in brackets
4330    preceded by a "+" to designate being inside that range, or a "-" to
4331    designate being outside that range, so the condition can be inverted by
4332    flipping the prefix.  An omitted bound is represented by a "-".  For
4333    example, "- [-, 10]" means being outside the range starting at the lowest
4334    possible value and ending at 10, in other words, being greater than 10.
4335    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4336    always false.
4337
4338    We set up things so that the missing bounds are handled in a consistent
4339    manner so neither a missing bound nor "true" and "false" need to be
4340    handled using a special case.  */
4341
4342 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4343    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4344    and UPPER1_P are nonzero if the respective argument is an upper bound
4345    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4346    must be specified for a comparison.  ARG1 will be converted to ARG0's
4347    type if both are specified.  */
4348
4349 static tree
4350 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4351              tree arg1, int upper1_p)
4352 {
4353   tree tem;
4354   int result;
4355   int sgn0, sgn1;
4356
4357   /* If neither arg represents infinity, do the normal operation.
4358      Else, if not a comparison, return infinity.  Else handle the special
4359      comparison rules. Note that most of the cases below won't occur, but
4360      are handled for consistency.  */
4361
4362   if (arg0 != 0 && arg1 != 0)
4363     {
4364       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4365                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4366       STRIP_NOPS (tem);
4367       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4368     }
4369
4370   if (TREE_CODE_CLASS (code) != tcc_comparison)
4371     return 0;
4372
4373   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4374      for neither.  In real maths, we cannot assume open ended ranges are
4375      the same. But, this is computer arithmetic, where numbers are finite.
4376      We can therefore make the transformation of any unbounded range with
4377      the value Z, Z being greater than any representable number. This permits
4378      us to treat unbounded ranges as equal.  */
4379   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4380   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4381   switch (code)
4382     {
4383     case EQ_EXPR:
4384       result = sgn0 == sgn1;
4385       break;
4386     case NE_EXPR:
4387       result = sgn0 != sgn1;
4388       break;
4389     case LT_EXPR:
4390       result = sgn0 < sgn1;
4391       break;
4392     case LE_EXPR:
4393       result = sgn0 <= sgn1;
4394       break;
4395     case GT_EXPR:
4396       result = sgn0 > sgn1;
4397       break;
4398     case GE_EXPR:
4399       result = sgn0 >= sgn1;
4400       break;
4401     default:
4402       gcc_unreachable ();
4403     }
4404
4405   return constant_boolean_node (result, type);
4406 }
4407 \f
4408 /* Given EXP, a logical expression, set the range it is testing into
4409    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4410    actually being tested.  *PLOW and *PHIGH will be made of the same
4411    type as the returned expression.  If EXP is not a comparison, we
4412    will most likely not be returning a useful value and range.  Set
4413    *STRICT_OVERFLOW_P to true if the return value is only valid
4414    because signed overflow is undefined; otherwise, do not change
4415    *STRICT_OVERFLOW_P.  */
4416
4417 tree
4418 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4419             bool *strict_overflow_p)
4420 {
4421   enum tree_code code;
4422   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4423   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4424   int in_p, n_in_p;
4425   tree low, high, n_low, n_high;
4426
4427   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4428      and see if we can refine the range.  Some of the cases below may not
4429      happen, but it doesn't seem worth worrying about this.  We "continue"
4430      the outer loop when we've changed something; otherwise we "break"
4431      the switch, which will "break" the while.  */
4432
4433   in_p = 0;
4434   low = high = build_int_cst (TREE_TYPE (exp), 0);
4435
4436   while (1)
4437     {
4438       code = TREE_CODE (exp);
4439       exp_type = TREE_TYPE (exp);
4440
4441       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4442         {
4443           if (TREE_OPERAND_LENGTH (exp) > 0)
4444             arg0 = TREE_OPERAND (exp, 0);
4445           if (TREE_CODE_CLASS (code) == tcc_comparison
4446               || TREE_CODE_CLASS (code) == tcc_unary
4447               || TREE_CODE_CLASS (code) == tcc_binary)
4448             arg0_type = TREE_TYPE (arg0);
4449           if (TREE_CODE_CLASS (code) == tcc_binary
4450               || TREE_CODE_CLASS (code) == tcc_comparison
4451               || (TREE_CODE_CLASS (code) == tcc_expression
4452                   && TREE_OPERAND_LENGTH (exp) > 1))
4453             arg1 = TREE_OPERAND (exp, 1);
4454         }
4455
4456       switch (code)
4457         {
4458         case TRUTH_NOT_EXPR:
4459           in_p = ! in_p, exp = arg0;
4460           continue;
4461
4462         case EQ_EXPR: case NE_EXPR:
4463         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4464           /* We can only do something if the range is testing for zero
4465              and if the second operand is an integer constant.  Note that
4466              saying something is "in" the range we make is done by
4467              complementing IN_P since it will set in the initial case of
4468              being not equal to zero; "out" is leaving it alone.  */
4469           if (low == 0 || high == 0
4470               || ! integer_zerop (low) || ! integer_zerop (high)
4471               || TREE_CODE (arg1) != INTEGER_CST)
4472             break;
4473
4474           switch (code)
4475             {
4476             case NE_EXPR:  /* - [c, c]  */
4477               low = high = arg1;
4478               break;
4479             case EQ_EXPR:  /* + [c, c]  */
4480               in_p = ! in_p, low = high = arg1;
4481               break;
4482             case GT_EXPR:  /* - [-, c] */
4483               low = 0, high = arg1;
4484               break;
4485             case GE_EXPR:  /* + [c, -] */
4486               in_p = ! in_p, low = arg1, high = 0;
4487               break;
4488             case LT_EXPR:  /* - [c, -] */
4489               low = arg1, high = 0;
4490               break;
4491             case LE_EXPR:  /* + [-, c] */
4492               in_p = ! in_p, low = 0, high = arg1;
4493               break;
4494             default:
4495               gcc_unreachable ();
4496             }
4497
4498           /* If this is an unsigned comparison, we also know that EXP is
4499              greater than or equal to zero.  We base the range tests we make
4500              on that fact, so we record it here so we can parse existing
4501              range tests.  We test arg0_type since often the return type
4502              of, e.g. EQ_EXPR, is boolean.  */
4503           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4504             {
4505               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4506                                   in_p, low, high, 1,
4507                                   build_int_cst (arg0_type, 0),
4508                                   NULL_TREE))
4509                 break;
4510
4511               in_p = n_in_p, low = n_low, high = n_high;
4512
4513               /* If the high bound is missing, but we have a nonzero low
4514                  bound, reverse the range so it goes from zero to the low bound
4515                  minus 1.  */
4516               if (high == 0 && low && ! integer_zerop (low))
4517                 {
4518                   in_p = ! in_p;
4519                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4520                                       integer_one_node, 0);
4521                   low = build_int_cst (arg0_type, 0);
4522                 }
4523             }
4524
4525           exp = arg0;
4526           continue;
4527
4528         case NEGATE_EXPR:
4529           /* (-x) IN [a,b] -> x in [-b, -a]  */
4530           n_low = range_binop (MINUS_EXPR, exp_type,
4531                                build_int_cst (exp_type, 0),
4532                                0, high, 1);
4533           n_high = range_binop (MINUS_EXPR, exp_type,
4534                                 build_int_cst (exp_type, 0),
4535                                 0, low, 0);
4536           low = n_low, high = n_high;
4537           exp = arg0;
4538           continue;
4539
4540         case BIT_NOT_EXPR:
4541           /* ~ X -> -X - 1  */
4542           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4543                         build_int_cst (exp_type, 1));
4544           continue;
4545
4546         case PLUS_EXPR:  case MINUS_EXPR:
4547           if (TREE_CODE (arg1) != INTEGER_CST)
4548             break;
4549
4550           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4551              move a constant to the other side.  */
4552           if (!TYPE_UNSIGNED (arg0_type)
4553               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4554             break;
4555
4556           /* If EXP is signed, any overflow in the computation is undefined,
4557              so we don't worry about it so long as our computations on
4558              the bounds don't overflow.  For unsigned, overflow is defined
4559              and this is exactly the right thing.  */
4560           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4561                                arg0_type, low, 0, arg1, 0);
4562           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4563                                 arg0_type, high, 1, arg1, 0);
4564           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4565               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4566             break;
4567
4568           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4569             *strict_overflow_p = true;
4570
4571           /* Check for an unsigned range which has wrapped around the maximum
4572              value thus making n_high < n_low, and normalize it.  */
4573           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4574             {
4575               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4576                                  integer_one_node, 0);
4577               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4578                                   integer_one_node, 0);
4579
4580               /* If the range is of the form +/- [ x+1, x ], we won't
4581                  be able to normalize it.  But then, it represents the
4582                  whole range or the empty set, so make it
4583                  +/- [ -, - ].  */
4584               if (tree_int_cst_equal (n_low, low)
4585                   && tree_int_cst_equal (n_high, high))
4586                 low = high = 0;
4587               else
4588                 in_p = ! in_p;
4589             }
4590           else
4591             low = n_low, high = n_high;
4592
4593           exp = arg0;
4594           continue;
4595
4596         CASE_CONVERT: case NON_LVALUE_EXPR:
4597           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4598             break;
4599
4600           if (! INTEGRAL_TYPE_P (arg0_type)
4601               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4602               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4603             break;
4604
4605           n_low = low, n_high = high;
4606
4607           if (n_low != 0)
4608             n_low = fold_convert (arg0_type, n_low);
4609
4610           if (n_high != 0)
4611             n_high = fold_convert (arg0_type, n_high);
4612
4613
4614           /* If we're converting arg0 from an unsigned type, to exp,
4615              a signed type,  we will be doing the comparison as unsigned.
4616              The tests above have already verified that LOW and HIGH
4617              are both positive.
4618
4619              So we have to ensure that we will handle large unsigned
4620              values the same way that the current signed bounds treat
4621              negative values.  */
4622
4623           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4624             {
4625               tree high_positive;
4626               tree equiv_type;
4627               /* For fixed-point modes, we need to pass the saturating flag
4628                  as the 2nd parameter.  */
4629               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4630                 equiv_type = lang_hooks.types.type_for_mode
4631                              (TYPE_MODE (arg0_type),
4632                               TYPE_SATURATING (arg0_type));
4633               else
4634                 equiv_type = lang_hooks.types.type_for_mode
4635                              (TYPE_MODE (arg0_type), 1);
4636
4637               /* A range without an upper bound is, naturally, unbounded.
4638                  Since convert would have cropped a very large value, use
4639                  the max value for the destination type.  */
4640               high_positive
4641                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4642                 : TYPE_MAX_VALUE (arg0_type);
4643
4644               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4645                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4646                                              fold_convert (arg0_type,
4647                                                            high_positive),
4648                                              build_int_cst (arg0_type, 1));
4649
4650               /* If the low bound is specified, "and" the range with the
4651                  range for which the original unsigned value will be
4652                  positive.  */
4653               if (low != 0)
4654                 {
4655                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4656                                       1, n_low, n_high, 1,
4657                                       fold_convert (arg0_type,
4658                                                     integer_zero_node),
4659                                       high_positive))
4660                     break;
4661
4662                   in_p = (n_in_p == in_p);
4663                 }
4664               else
4665                 {
4666                   /* Otherwise, "or" the range with the range of the input
4667                      that will be interpreted as negative.  */
4668                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4669                                       0, n_low, n_high, 1,
4670                                       fold_convert (arg0_type,
4671                                                     integer_zero_node),
4672                                       high_positive))
4673                     break;
4674
4675                   in_p = (in_p != n_in_p);
4676                 }
4677             }
4678
4679           exp = arg0;
4680           low = n_low, high = n_high;
4681           continue;
4682
4683         default:
4684           break;
4685         }
4686
4687       break;
4688     }
4689
4690   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4691   if (TREE_CODE (exp) == INTEGER_CST)
4692     {
4693       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4694                                                  exp, 0, low, 0))
4695                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4696                                                     exp, 1, high, 1)));
4697       low = high = 0;
4698       exp = 0;
4699     }
4700
4701   *pin_p = in_p, *plow = low, *phigh = high;
4702   return exp;
4703 }
4704 \f
4705 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4706    type, TYPE, return an expression to test if EXP is in (or out of, depending
4707    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4708
4709 tree
4710 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4711 {
4712   tree etype = TREE_TYPE (exp), value;
4713
4714 #ifdef HAVE_canonicalize_funcptr_for_compare
4715   /* Disable this optimization for function pointer expressions
4716      on targets that require function pointer canonicalization.  */
4717   if (HAVE_canonicalize_funcptr_for_compare
4718       && TREE_CODE (etype) == POINTER_TYPE
4719       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4720     return NULL_TREE;
4721 #endif
4722
4723   if (! in_p)
4724     {
4725       value = build_range_check (type, exp, 1, low, high);
4726       if (value != 0)
4727         return invert_truthvalue (value);
4728
4729       return 0;
4730     }
4731
4732   if (low == 0 && high == 0)
4733     return build_int_cst (type, 1);
4734
4735   if (low == 0)
4736     return fold_build2 (LE_EXPR, type, exp,
4737                         fold_convert (etype, high));
4738
4739   if (high == 0)
4740     return fold_build2 (GE_EXPR, type, exp,
4741                         fold_convert (etype, low));
4742
4743   if (operand_equal_p (low, high, 0))
4744     return fold_build2 (EQ_EXPR, type, exp,
4745                         fold_convert (etype, low));
4746
4747   if (integer_zerop (low))
4748     {
4749       if (! TYPE_UNSIGNED (etype))
4750         {
4751           etype = unsigned_type_for (etype);
4752           high = fold_convert (etype, high);
4753           exp = fold_convert (etype, exp);
4754         }
4755       return build_range_check (type, exp, 1, 0, high);
4756     }
4757
4758   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4759   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4760     {
4761       unsigned HOST_WIDE_INT lo;
4762       HOST_WIDE_INT hi;
4763       int prec;
4764
4765       prec = TYPE_PRECISION (etype);
4766       if (prec <= HOST_BITS_PER_WIDE_INT)
4767         {
4768           hi = 0;
4769           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4770         }
4771       else
4772         {
4773           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4774           lo = (unsigned HOST_WIDE_INT) -1;
4775         }
4776
4777       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4778         {
4779           if (TYPE_UNSIGNED (etype))
4780             {
4781               tree signed_etype = signed_type_for (etype);
4782               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4783                 etype
4784                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4785               else
4786                 etype = signed_etype;
4787               exp = fold_convert (etype, exp);
4788             }
4789           return fold_build2 (GT_EXPR, type, exp,
4790                               build_int_cst (etype, 0));
4791         }
4792     }
4793
4794   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4795      This requires wrap-around arithmetics for the type of the expression.
4796      First make sure that arithmetics in this type is valid, then make sure
4797      that it wraps around.  */
4798   if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4799     etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4800                                             TYPE_UNSIGNED (etype));
4801
4802   if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4803     {
4804       tree utype, minv, maxv;
4805
4806       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4807          for the type in question, as we rely on this here.  */
4808       utype = unsigned_type_for (etype);
4809       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4810       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4811                           integer_one_node, 1);
4812       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4813
4814       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4815                                       minv, 1, maxv, 1)))
4816         etype = utype;
4817       else
4818         return 0;
4819     }
4820
4821   high = fold_convert (etype, high);
4822   low = fold_convert (etype, low);
4823   exp = fold_convert (etype, exp);
4824
4825   value = const_binop (MINUS_EXPR, high, low, 0);
4826
4827
4828   if (POINTER_TYPE_P (etype))
4829     {
4830       if (value != 0 && !TREE_OVERFLOW (value))
4831         {
4832           low = fold_convert (sizetype, low);
4833           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4834           return build_range_check (type,
4835                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4836                                     1, build_int_cst (etype, 0), value);
4837         }
4838       return 0;
4839     }
4840
4841   if (value != 0 && !TREE_OVERFLOW (value))
4842     return build_range_check (type,
4843                               fold_build2 (MINUS_EXPR, etype, exp, low),
4844                               1, build_int_cst (etype, 0), value);
4845
4846   return 0;
4847 }
4848 \f
4849 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4850
4851 static tree
4852 range_predecessor (tree val)
4853 {
4854   tree type = TREE_TYPE (val);
4855
4856   if (INTEGRAL_TYPE_P (type)
4857       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4858     return 0;
4859   else
4860     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4861 }
4862
4863 /* Return the successor of VAL in its type, handling the infinite case.  */
4864
4865 static tree
4866 range_successor (tree val)
4867 {
4868   tree type = TREE_TYPE (val);
4869
4870   if (INTEGRAL_TYPE_P (type)
4871       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4872     return 0;
4873   else
4874     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4875 }
4876
4877 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4878    can, 0 if we can't.  Set the output range into the specified parameters.  */
4879
4880 bool
4881 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4882               tree high0, int in1_p, tree low1, tree high1)
4883 {
4884   int no_overlap;
4885   int subset;
4886   int temp;
4887   tree tem;
4888   int in_p;
4889   tree low, high;
4890   int lowequal = ((low0 == 0 && low1 == 0)
4891                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4892                                                 low0, 0, low1, 0)));
4893   int highequal = ((high0 == 0 && high1 == 0)
4894                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4895                                                  high0, 1, high1, 1)));
4896
4897   /* Make range 0 be the range that starts first, or ends last if they
4898      start at the same value.  Swap them if it isn't.  */
4899   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4900                                  low0, 0, low1, 0))
4901       || (lowequal
4902           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4903                                         high1, 1, high0, 1))))
4904     {
4905       temp = in0_p, in0_p = in1_p, in1_p = temp;
4906       tem = low0, low0 = low1, low1 = tem;
4907       tem = high0, high0 = high1, high1 = tem;
4908     }
4909
4910   /* Now flag two cases, whether the ranges are disjoint or whether the
4911      second range is totally subsumed in the first.  Note that the tests
4912      below are simplified by the ones above.  */
4913   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4914                                           high0, 1, low1, 0));
4915   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4916                                       high1, 1, high0, 1));
4917
4918   /* We now have four cases, depending on whether we are including or
4919      excluding the two ranges.  */
4920   if (in0_p && in1_p)
4921     {
4922       /* If they don't overlap, the result is false.  If the second range
4923          is a subset it is the result.  Otherwise, the range is from the start
4924          of the second to the end of the first.  */
4925       if (no_overlap)
4926         in_p = 0, low = high = 0;
4927       else if (subset)
4928         in_p = 1, low = low1, high = high1;
4929       else
4930         in_p = 1, low = low1, high = high0;
4931     }
4932
4933   else if (in0_p && ! in1_p)
4934     {
4935       /* If they don't overlap, the result is the first range.  If they are
4936          equal, the result is false.  If the second range is a subset of the
4937          first, and the ranges begin at the same place, we go from just after
4938          the end of the second range to the end of the first.  If the second
4939          range is not a subset of the first, or if it is a subset and both
4940          ranges end at the same place, the range starts at the start of the
4941          first range and ends just before the second range.
4942          Otherwise, we can't describe this as a single range.  */
4943       if (no_overlap)
4944         in_p = 1, low = low0, high = high0;
4945       else if (lowequal && highequal)
4946         in_p = 0, low = high = 0;
4947       else if (subset && lowequal)
4948         {
4949           low = range_successor (high1);
4950           high = high0;
4951           in_p = 1;
4952           if (low == 0)
4953             {
4954               /* We are in the weird situation where high0 > high1 but
4955                  high1 has no successor.  Punt.  */
4956               return 0;
4957             }
4958         }
4959       else if (! subset || highequal)
4960         {
4961           low = low0;
4962           high = range_predecessor (low1);
4963           in_p = 1;
4964           if (high == 0)
4965             {
4966               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4967               return 0;
4968             }
4969         }
4970       else
4971         return 0;
4972     }
4973
4974   else if (! in0_p && in1_p)
4975     {
4976       /* If they don't overlap, the result is the second range.  If the second
4977          is a subset of the first, the result is false.  Otherwise,
4978          the range starts just after the first range and ends at the
4979          end of the second.  */
4980       if (no_overlap)
4981         in_p = 1, low = low1, high = high1;
4982       else if (subset || highequal)
4983         in_p = 0, low = high = 0;
4984       else
4985         {
4986           low = range_successor (high0);
4987           high = high1;
4988           in_p = 1;
4989           if (low == 0)
4990             {
4991               /* high1 > high0 but high0 has no successor.  Punt.  */
4992               return 0;
4993             }
4994         }
4995     }
4996
4997   else
4998     {
4999       /* The case where we are excluding both ranges.  Here the complex case
5000          is if they don't overlap.  In that case, the only time we have a
5001          range is if they are adjacent.  If the second is a subset of the
5002          first, the result is the first.  Otherwise, the range to exclude
5003          starts at the beginning of the first range and ends at the end of the
5004          second.  */
5005       if (no_overlap)
5006         {
5007           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5008                                          range_successor (high0),
5009                                          1, low1, 0)))
5010             in_p = 0, low = low0, high = high1;
5011           else
5012             {
5013               /* Canonicalize - [min, x] into - [-, x].  */
5014               if (low0 && TREE_CODE (low0) == INTEGER_CST)
5015                 switch (TREE_CODE (TREE_TYPE (low0)))
5016                   {
5017                   case ENUMERAL_TYPE:
5018                     if (TYPE_PRECISION (TREE_TYPE (low0))
5019                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5020                       break;
5021                     /* FALLTHROUGH */
5022                   case INTEGER_TYPE:
5023                     if (tree_int_cst_equal (low0,
5024                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
5025                       low0 = 0;
5026                     break;
5027                   case POINTER_TYPE:
5028                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
5029                         && integer_zerop (low0))
5030                       low0 = 0;
5031                     break;
5032                   default:
5033                     break;
5034                   }
5035
5036               /* Canonicalize - [x, max] into - [x, -].  */
5037               if (high1 && TREE_CODE (high1) == INTEGER_CST)
5038                 switch (TREE_CODE (TREE_TYPE (high1)))
5039                   {
5040                   case ENUMERAL_TYPE:
5041                     if (TYPE_PRECISION (TREE_TYPE (high1))
5042                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5043                       break;
5044                     /* FALLTHROUGH */
5045                   case INTEGER_TYPE:
5046                     if (tree_int_cst_equal (high1,
5047                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5048                       high1 = 0;
5049                     break;
5050                   case POINTER_TYPE:
5051                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5052                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5053                                                        high1, 1,
5054                                                        integer_one_node, 1)))
5055                       high1 = 0;
5056                     break;
5057                   default:
5058                     break;
5059                   }
5060
5061               /* The ranges might be also adjacent between the maximum and
5062                  minimum values of the given type.  For
5063                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5064                  return + [x + 1, y - 1].  */
5065               if (low0 == 0 && high1 == 0)
5066                 {
5067                   low = range_successor (high0);
5068                   high = range_predecessor (low1);
5069                   if (low == 0 || high == 0)
5070                     return 0;
5071
5072                   in_p = 1;
5073                 }
5074               else
5075                 return 0;
5076             }
5077         }
5078       else if (subset)
5079         in_p = 0, low = low0, high = high0;
5080       else
5081         in_p = 0, low = low0, high = high1;
5082     }
5083
5084   *pin_p = in_p, *plow = low, *phigh = high;
5085   return 1;
5086 }
5087 \f
5088
5089 /* Subroutine of fold, looking inside expressions of the form
5090    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5091    of the COND_EXPR.  This function is being used also to optimize
5092    A op B ? C : A, by reversing the comparison first.
5093
5094    Return a folded expression whose code is not a COND_EXPR
5095    anymore, or NULL_TREE if no folding opportunity is found.  */
5096
5097 static tree
5098 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5099 {
5100   enum tree_code comp_code = TREE_CODE (arg0);
5101   tree arg00 = TREE_OPERAND (arg0, 0);
5102   tree arg01 = TREE_OPERAND (arg0, 1);
5103   tree arg1_type = TREE_TYPE (arg1);
5104   tree tem;
5105
5106   STRIP_NOPS (arg1);
5107   STRIP_NOPS (arg2);
5108
5109   /* If we have A op 0 ? A : -A, consider applying the following
5110      transformations:
5111
5112      A == 0? A : -A    same as -A
5113      A != 0? A : -A    same as A
5114      A >= 0? A : -A    same as abs (A)
5115      A > 0?  A : -A    same as abs (A)
5116      A <= 0? A : -A    same as -abs (A)
5117      A < 0?  A : -A    same as -abs (A)
5118
5119      None of these transformations work for modes with signed
5120      zeros.  If A is +/-0, the first two transformations will
5121      change the sign of the result (from +0 to -0, or vice
5122      versa).  The last four will fix the sign of the result,
5123      even though the original expressions could be positive or
5124      negative, depending on the sign of A.
5125
5126      Note that all these transformations are correct if A is
5127      NaN, since the two alternatives (A and -A) are also NaNs.  */
5128   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5129       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5130           ? real_zerop (arg01)
5131           : integer_zerop (arg01))
5132       && ((TREE_CODE (arg2) == NEGATE_EXPR
5133            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5134              /* In the case that A is of the form X-Y, '-A' (arg2) may
5135                 have already been folded to Y-X, check for that. */
5136           || (TREE_CODE (arg1) == MINUS_EXPR
5137               && TREE_CODE (arg2) == MINUS_EXPR
5138               && operand_equal_p (TREE_OPERAND (arg1, 0),
5139                                   TREE_OPERAND (arg2, 1), 0)
5140               && operand_equal_p (TREE_OPERAND (arg1, 1),
5141                                   TREE_OPERAND (arg2, 0), 0))))
5142     switch (comp_code)
5143       {
5144       case EQ_EXPR:
5145       case UNEQ_EXPR:
5146         tem = fold_convert (arg1_type, arg1);
5147         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5148       case NE_EXPR:
5149       case LTGT_EXPR:
5150         return pedantic_non_lvalue (fold_convert (type, arg1));
5151       case UNGE_EXPR:
5152       case UNGT_EXPR:
5153         if (flag_trapping_math)
5154           break;
5155         /* Fall through.  */
5156       case GE_EXPR:
5157       case GT_EXPR:
5158         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5159           arg1 = fold_convert (signed_type_for
5160                                (TREE_TYPE (arg1)), arg1);
5161         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5162         return pedantic_non_lvalue (fold_convert (type, tem));
5163       case UNLE_EXPR:
5164       case UNLT_EXPR:
5165         if (flag_trapping_math)
5166           break;
5167       case LE_EXPR:
5168       case LT_EXPR:
5169         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5170           arg1 = fold_convert (signed_type_for
5171                                (TREE_TYPE (arg1)), arg1);
5172         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5173         return negate_expr (fold_convert (type, tem));
5174       default:
5175         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5176         break;
5177       }
5178
5179   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5180      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5181      both transformations are correct when A is NaN: A != 0
5182      is then true, and A == 0 is false.  */
5183
5184   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5185       && integer_zerop (arg01) && integer_zerop (arg2))
5186     {
5187       if (comp_code == NE_EXPR)
5188         return pedantic_non_lvalue (fold_convert (type, arg1));
5189       else if (comp_code == EQ_EXPR)
5190         return build_int_cst (type, 0);
5191     }
5192
5193   /* Try some transformations of A op B ? A : B.
5194
5195      A == B? A : B    same as B
5196      A != B? A : B    same as A
5197      A >= B? A : B    same as max (A, B)
5198      A > B?  A : B    same as max (B, A)
5199      A <= B? A : B    same as min (A, B)
5200      A < B?  A : B    same as min (B, A)
5201
5202      As above, these transformations don't work in the presence
5203      of signed zeros.  For example, if A and B are zeros of
5204      opposite sign, the first two transformations will change
5205      the sign of the result.  In the last four, the original
5206      expressions give different results for (A=+0, B=-0) and
5207      (A=-0, B=+0), but the transformed expressions do not.
5208
5209      The first two transformations are correct if either A or B
5210      is a NaN.  In the first transformation, the condition will
5211      be false, and B will indeed be chosen.  In the case of the
5212      second transformation, the condition A != B will be true,
5213      and A will be chosen.
5214
5215      The conversions to max() and min() are not correct if B is
5216      a number and A is not.  The conditions in the original
5217      expressions will be false, so all four give B.  The min()
5218      and max() versions would give a NaN instead.  */
5219   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5220       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5221       /* Avoid these transformations if the COND_EXPR may be used
5222          as an lvalue in the C++ front-end.  PR c++/19199.  */
5223       && (in_gimple_form
5224           || (strcmp (lang_hooks.name, "GNU C++") != 0
5225               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5226           || ! maybe_lvalue_p (arg1)
5227           || ! maybe_lvalue_p (arg2)))
5228     {
5229       tree comp_op0 = arg00;
5230       tree comp_op1 = arg01;
5231       tree comp_type = TREE_TYPE (comp_op0);
5232
5233       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5234       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5235         {
5236           comp_type = type;
5237           comp_op0 = arg1;
5238           comp_op1 = arg2;
5239         }
5240
5241       switch (comp_code)
5242         {
5243         case EQ_EXPR:
5244           return pedantic_non_lvalue (fold_convert (type, arg2));
5245         case NE_EXPR:
5246           return pedantic_non_lvalue (fold_convert (type, arg1));
5247         case LE_EXPR:
5248         case LT_EXPR:
5249         case UNLE_EXPR:
5250         case UNLT_EXPR:
5251           /* In C++ a ?: expression can be an lvalue, so put the
5252              operand which will be used if they are equal first
5253              so that we can convert this back to the
5254              corresponding COND_EXPR.  */
5255           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5256             {
5257               comp_op0 = fold_convert (comp_type, comp_op0);
5258               comp_op1 = fold_convert (comp_type, comp_op1);
5259               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5260                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5261                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5262               return pedantic_non_lvalue (fold_convert (type, tem));
5263             }
5264           break;
5265         case GE_EXPR:
5266         case GT_EXPR:
5267         case UNGE_EXPR:
5268         case UNGT_EXPR:
5269           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5270             {
5271               comp_op0 = fold_convert (comp_type, comp_op0);
5272               comp_op1 = fold_convert (comp_type, comp_op1);
5273               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5274                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5275                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5276               return pedantic_non_lvalue (fold_convert (type, tem));
5277             }
5278           break;
5279         case UNEQ_EXPR:
5280           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5281             return pedantic_non_lvalue (fold_convert (type, arg2));
5282           break;
5283         case LTGT_EXPR:
5284           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5285             return pedantic_non_lvalue (fold_convert (type, arg1));
5286           break;
5287         default:
5288           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5289           break;
5290         }
5291     }
5292
5293   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5294      we might still be able to simplify this.  For example,
5295      if C1 is one less or one more than C2, this might have started
5296      out as a MIN or MAX and been transformed by this function.
5297      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5298
5299   if (INTEGRAL_TYPE_P (type)
5300       && TREE_CODE (arg01) == INTEGER_CST
5301       && TREE_CODE (arg2) == INTEGER_CST)
5302     switch (comp_code)
5303       {
5304       case EQ_EXPR:
5305         /* We can replace A with C1 in this case.  */
5306         arg1 = fold_convert (type, arg01);
5307         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5308
5309       case LT_EXPR:
5310         /* If C1 is C2 + 1, this is min(A, C2).  */
5311         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5312                                OEP_ONLY_CONST)
5313             && operand_equal_p (arg01,
5314                                 const_binop (PLUS_EXPR, arg2,
5315                                              build_int_cst (type, 1), 0),
5316                                 OEP_ONLY_CONST))
5317           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5318                                                    type,
5319                                                    fold_convert (type, arg1),
5320                                                    arg2));
5321         break;
5322
5323       case LE_EXPR:
5324         /* If C1 is C2 - 1, this is min(A, C2).  */
5325         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5326                                OEP_ONLY_CONST)
5327             && operand_equal_p (arg01,
5328                                 const_binop (MINUS_EXPR, arg2,
5329                                              build_int_cst (type, 1), 0),
5330                                 OEP_ONLY_CONST))
5331           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5332                                                    type,
5333                                                    fold_convert (type, arg1),
5334                                                    arg2));
5335         break;
5336
5337       case GT_EXPR:
5338         /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5339            MAX_EXPR, to preserve the signedness of the comparison.  */
5340         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5341                                OEP_ONLY_CONST)
5342             && operand_equal_p (arg01,
5343                                 const_binop (MINUS_EXPR, arg2,
5344                                              build_int_cst (type, 1), 0),
5345                                 OEP_ONLY_CONST))
5346           return pedantic_non_lvalue (fold_convert (type,
5347                                       fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
5348                                                    arg00,
5349                                                    fold_convert (TREE_TYPE (arg00),
5350                                                                  arg2))));
5351         break;
5352
5353       case GE_EXPR:
5354         /* If C1 is C2 + 1, this is max(A, C2), with the same care as above.  */
5355         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5356                                OEP_ONLY_CONST)
5357             && operand_equal_p (arg01,
5358                                 const_binop (PLUS_EXPR, arg2,
5359                                              build_int_cst (type, 1), 0),
5360                                 OEP_ONLY_CONST))
5361           return pedantic_non_lvalue (fold_convert (type,
5362                                       fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
5363                                                    arg00,
5364                                                    fold_convert (TREE_TYPE (arg00),
5365                                                                  arg2))));
5366         break;
5367       case NE_EXPR:
5368         break;
5369       default:
5370         gcc_unreachable ();
5371       }
5372
5373   return NULL_TREE;
5374 }
5375
5376
5377 \f
5378 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5379 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5380   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5381                 false) >= 2)
5382 #endif
5383
5384 /* EXP is some logical combination of boolean tests.  See if we can
5385    merge it into some range test.  Return the new tree if so.  */
5386
5387 static tree
5388 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5389 {
5390   int or_op = (code == TRUTH_ORIF_EXPR
5391                || code == TRUTH_OR_EXPR);
5392   int in0_p, in1_p, in_p;
5393   tree low0, low1, low, high0, high1, high;
5394   bool strict_overflow_p = false;
5395   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5396   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5397   tree tem;
5398   const char * const warnmsg = G_("assuming signed overflow does not occur "
5399                                   "when simplifying range test");
5400
5401   /* If this is an OR operation, invert both sides; we will invert
5402      again at the end.  */
5403   if (or_op)
5404     in0_p = ! in0_p, in1_p = ! in1_p;
5405
5406   /* If both expressions are the same, if we can merge the ranges, and we
5407      can build the range test, return it or it inverted.  If one of the
5408      ranges is always true or always false, consider it to be the same
5409      expression as the other.  */
5410   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5411       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5412                        in1_p, low1, high1)
5413       && 0 != (tem = (build_range_check (type,
5414                                          lhs != 0 ? lhs
5415                                          : rhs != 0 ? rhs : integer_zero_node,
5416                                          in_p, low, high))))
5417     {
5418       if (strict_overflow_p)
5419         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5420       return or_op ? invert_truthvalue (tem) : tem;
5421     }
5422
5423   /* On machines where the branch cost is expensive, if this is a
5424      short-circuited branch and the underlying object on both sides
5425      is the same, make a non-short-circuit operation.  */
5426   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5427            && lhs != 0 && rhs != 0
5428            && (code == TRUTH_ANDIF_EXPR
5429                || code == TRUTH_ORIF_EXPR)
5430            && operand_equal_p (lhs, rhs, 0))
5431     {
5432       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5433          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5434          which cases we can't do this.  */
5435       if (simple_operand_p (lhs))
5436         return build2 (code == TRUTH_ANDIF_EXPR
5437                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5438                        type, op0, op1);
5439
5440       else if (lang_hooks.decls.global_bindings_p () == 0
5441                && ! CONTAINS_PLACEHOLDER_P (lhs))
5442         {
5443           tree common = save_expr (lhs);
5444
5445           if (0 != (lhs = build_range_check (type, common,
5446                                              or_op ? ! in0_p : in0_p,
5447                                              low0, high0))
5448               && (0 != (rhs = build_range_check (type, common,
5449                                                  or_op ? ! in1_p : in1_p,
5450                                                  low1, high1))))
5451             {
5452               if (strict_overflow_p)
5453                 fold_overflow_warning (warnmsg,
5454                                        WARN_STRICT_OVERFLOW_COMPARISON);
5455               return build2 (code == TRUTH_ANDIF_EXPR
5456                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5457                              type, lhs, rhs);
5458             }
5459         }
5460     }
5461
5462   return 0;
5463 }
5464 \f
5465 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5466    bit value.  Arrange things so the extra bits will be set to zero if and
5467    only if C is signed-extended to its full width.  If MASK is nonzero,
5468    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5469
5470 static tree
5471 unextend (tree c, int p, int unsignedp, tree mask)
5472 {
5473   tree type = TREE_TYPE (c);
5474   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5475   tree temp;
5476
5477   if (p == modesize || unsignedp)
5478     return c;
5479
5480   /* We work by getting just the sign bit into the low-order bit, then
5481      into the high-order bit, then sign-extend.  We then XOR that value
5482      with C.  */
5483   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5484   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5485
5486   /* We must use a signed type in order to get an arithmetic right shift.
5487      However, we must also avoid introducing accidental overflows, so that
5488      a subsequent call to integer_zerop will work.  Hence we must
5489      do the type conversion here.  At this point, the constant is either
5490      zero or one, and the conversion to a signed type can never overflow.
5491      We could get an overflow if this conversion is done anywhere else.  */
5492   if (TYPE_UNSIGNED (type))
5493     temp = fold_convert (signed_type_for (type), temp);
5494
5495   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5496   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5497   if (mask != 0)
5498     temp = const_binop (BIT_AND_EXPR, temp,
5499                         fold_convert (TREE_TYPE (c), mask), 0);
5500   /* If necessary, convert the type back to match the type of C.  */
5501   if (TYPE_UNSIGNED (type))
5502     temp = fold_convert (type, temp);
5503
5504   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5505 }
5506 \f
5507 /* Find ways of folding logical expressions of LHS and RHS:
5508    Try to merge two comparisons to the same innermost item.
5509    Look for range tests like "ch >= '0' && ch <= '9'".
5510    Look for combinations of simple terms on machines with expensive branches
5511    and evaluate the RHS unconditionally.
5512
5513    For example, if we have p->a == 2 && p->b == 4 and we can make an
5514    object large enough to span both A and B, we can do this with a comparison
5515    against the object ANDed with the a mask.
5516
5517    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5518    operations to do this with one comparison.
5519
5520    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5521    function and the one above.
5522
5523    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5524    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5525
5526    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5527    two operands.
5528
5529    We return the simplified tree or 0 if no optimization is possible.  */
5530
5531 static tree
5532 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5533 {
5534   /* If this is the "or" of two comparisons, we can do something if
5535      the comparisons are NE_EXPR.  If this is the "and", we can do something
5536      if the comparisons are EQ_EXPR.  I.e.,
5537         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5538
5539      WANTED_CODE is this operation code.  For single bit fields, we can
5540      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5541      comparison for one-bit fields.  */
5542
5543   enum tree_code wanted_code;
5544   enum tree_code lcode, rcode;
5545   tree ll_arg, lr_arg, rl_arg, rr_arg;
5546   tree ll_inner, lr_inner, rl_inner, rr_inner;
5547   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5548   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5549   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5550   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5551   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5552   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5553   enum machine_mode lnmode, rnmode;
5554   tree ll_mask, lr_mask, rl_mask, rr_mask;
5555   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5556   tree l_const, r_const;
5557   tree lntype, rntype, result;
5558   HOST_WIDE_INT first_bit, end_bit;
5559   int volatilep;
5560   tree orig_lhs = lhs, orig_rhs = rhs;
5561   enum tree_code orig_code = code;
5562
5563   /* Start by getting the comparison codes.  Fail if anything is volatile.
5564      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5565      it were surrounded with a NE_EXPR.  */
5566
5567   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5568     return 0;
5569
5570   lcode = TREE_CODE (lhs);
5571   rcode = TREE_CODE (rhs);
5572
5573   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5574     {
5575       lhs = build2 (NE_EXPR, truth_type, lhs,
5576                     build_int_cst (TREE_TYPE (lhs), 0));
5577       lcode = NE_EXPR;
5578     }
5579
5580   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5581     {
5582       rhs = build2 (NE_EXPR, truth_type, rhs,
5583                     build_int_cst (TREE_TYPE (rhs), 0));
5584       rcode = NE_EXPR;
5585     }
5586
5587   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5588       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5589     return 0;
5590
5591   ll_arg = TREE_OPERAND (lhs, 0);
5592   lr_arg = TREE_OPERAND (lhs, 1);
5593   rl_arg = TREE_OPERAND (rhs, 0);
5594   rr_arg = TREE_OPERAND (rhs, 1);
5595
5596   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5597   if (simple_operand_p (ll_arg)
5598       && simple_operand_p (lr_arg))
5599     {
5600       tree result;
5601       if (operand_equal_p (ll_arg, rl_arg, 0)
5602           && operand_equal_p (lr_arg, rr_arg, 0))
5603         {
5604           result = combine_comparisons (code, lcode, rcode,
5605                                         truth_type, ll_arg, lr_arg);
5606           if (result)
5607             return result;
5608         }
5609       else if (operand_equal_p (ll_arg, rr_arg, 0)
5610                && operand_equal_p (lr_arg, rl_arg, 0))
5611         {
5612           result = combine_comparisons (code, lcode,
5613                                         swap_tree_comparison (rcode),
5614                                         truth_type, ll_arg, lr_arg);
5615           if (result)
5616             return result;
5617         }
5618     }
5619
5620   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5621           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5622
5623   /* If the RHS can be evaluated unconditionally and its operands are
5624      simple, it wins to evaluate the RHS unconditionally on machines
5625      with expensive branches.  In this case, this isn't a comparison
5626      that can be merged.  Avoid doing this if the RHS is a floating-point
5627      comparison since those can trap.  */
5628
5629   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5630                    false) >= 2
5631       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5632       && simple_operand_p (rl_arg)
5633       && simple_operand_p (rr_arg))
5634     {
5635       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5636       if (code == TRUTH_OR_EXPR
5637           && lcode == NE_EXPR && integer_zerop (lr_arg)
5638           && rcode == NE_EXPR && integer_zerop (rr_arg)
5639           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5640           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5641         return build2 (NE_EXPR, truth_type,
5642                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5643                                ll_arg, rl_arg),
5644                        build_int_cst (TREE_TYPE (ll_arg), 0));
5645
5646       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5647       if (code == TRUTH_AND_EXPR
5648           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5649           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5650           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5651           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5652         return build2 (EQ_EXPR, truth_type,
5653                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5654                                ll_arg, rl_arg),
5655                        build_int_cst (TREE_TYPE (ll_arg), 0));
5656
5657       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5658         {
5659           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5660             return build2 (code, truth_type, lhs, rhs);
5661           return NULL_TREE;
5662         }
5663     }
5664
5665   /* See if the comparisons can be merged.  Then get all the parameters for
5666      each side.  */
5667
5668   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5669       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5670     return 0;
5671
5672   volatilep = 0;
5673   ll_inner = decode_field_reference (ll_arg,
5674                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5675                                      &ll_unsignedp, &volatilep, &ll_mask,
5676                                      &ll_and_mask);
5677   lr_inner = decode_field_reference (lr_arg,
5678                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5679                                      &lr_unsignedp, &volatilep, &lr_mask,
5680                                      &lr_and_mask);
5681   rl_inner = decode_field_reference (rl_arg,
5682                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5683                                      &rl_unsignedp, &volatilep, &rl_mask,
5684                                      &rl_and_mask);
5685   rr_inner = decode_field_reference (rr_arg,
5686                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5687                                      &rr_unsignedp, &volatilep, &rr_mask,
5688                                      &rr_and_mask);
5689
5690   /* It must be true that the inner operation on the lhs of each
5691      comparison must be the same if we are to be able to do anything.
5692      Then see if we have constants.  If not, the same must be true for
5693      the rhs's.  */
5694   if (volatilep || ll_inner == 0 || rl_inner == 0
5695       || ! operand_equal_p (ll_inner, rl_inner, 0))
5696     return 0;
5697
5698   if (TREE_CODE (lr_arg) == INTEGER_CST
5699       && TREE_CODE (rr_arg) == INTEGER_CST)
5700     l_const = lr_arg, r_const = rr_arg;
5701   else if (lr_inner == 0 || rr_inner == 0
5702            || ! operand_equal_p (lr_inner, rr_inner, 0))
5703     return 0;
5704   else
5705     l_const = r_const = 0;
5706
5707   /* If either comparison code is not correct for our logical operation,
5708      fail.  However, we can convert a one-bit comparison against zero into
5709      the opposite comparison against that bit being set in the field.  */
5710
5711   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5712   if (lcode != wanted_code)
5713     {
5714       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5715         {
5716           /* Make the left operand unsigned, since we are only interested
5717              in the value of one bit.  Otherwise we are doing the wrong
5718              thing below.  */
5719           ll_unsignedp = 1;
5720           l_const = ll_mask;
5721         }
5722       else
5723         return 0;
5724     }
5725
5726   /* This is analogous to the code for l_const above.  */
5727   if (rcode != wanted_code)
5728     {
5729       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5730         {
5731           rl_unsignedp = 1;
5732           r_const = rl_mask;
5733         }
5734       else
5735         return 0;
5736     }
5737
5738   /* See if we can find a mode that contains both fields being compared on
5739      the left.  If we can't, fail.  Otherwise, update all constants and masks
5740      to be relative to a field of that size.  */
5741   first_bit = MIN (ll_bitpos, rl_bitpos);
5742   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5743   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5744                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5745                           volatilep);
5746   if (lnmode == VOIDmode)
5747     return 0;
5748
5749   lnbitsize = GET_MODE_BITSIZE (lnmode);
5750   lnbitpos = first_bit & ~ (lnbitsize - 1);
5751   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5752   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5753
5754   if (BYTES_BIG_ENDIAN)
5755     {
5756       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5757       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5758     }
5759
5760   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5761                          size_int (xll_bitpos), 0);
5762   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5763                          size_int (xrl_bitpos), 0);
5764
5765   if (l_const)
5766     {
5767       l_const = fold_convert (lntype, l_const);
5768       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5769       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5770       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5771                                         fold_build1 (BIT_NOT_EXPR,
5772                                                      lntype, ll_mask),
5773                                         0)))
5774         {
5775           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5776
5777           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5778         }
5779     }
5780   if (r_const)
5781     {
5782       r_const = fold_convert (lntype, r_const);
5783       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5784       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5785       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5786                                         fold_build1 (BIT_NOT_EXPR,
5787                                                      lntype, rl_mask),
5788                                         0)))
5789         {
5790           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5791
5792           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5793         }
5794     }
5795
5796   /* If the right sides are not constant, do the same for it.  Also,
5797      disallow this optimization if a size or signedness mismatch occurs
5798      between the left and right sides.  */
5799   if (l_const == 0)
5800     {
5801       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5802           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5803           /* Make sure the two fields on the right
5804              correspond to the left without being swapped.  */
5805           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5806         return 0;
5807
5808       first_bit = MIN (lr_bitpos, rr_bitpos);
5809       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5810       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5811                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5812                               volatilep);
5813       if (rnmode == VOIDmode)
5814         return 0;
5815
5816       rnbitsize = GET_MODE_BITSIZE (rnmode);
5817       rnbitpos = first_bit & ~ (rnbitsize - 1);
5818       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5819       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5820
5821       if (BYTES_BIG_ENDIAN)
5822         {
5823           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5824           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5825         }
5826
5827       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5828                              size_int (xlr_bitpos), 0);
5829       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5830                              size_int (xrr_bitpos), 0);
5831
5832       /* Make a mask that corresponds to both fields being compared.
5833          Do this for both items being compared.  If the operands are the
5834          same size and the bits being compared are in the same position
5835          then we can do this by masking both and comparing the masked
5836          results.  */
5837       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5838       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5839       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5840         {
5841           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5842                                     ll_unsignedp || rl_unsignedp);
5843           if (! all_ones_mask_p (ll_mask, lnbitsize))
5844             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5845
5846           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5847                                     lr_unsignedp || rr_unsignedp);
5848           if (! all_ones_mask_p (lr_mask, rnbitsize))
5849             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5850
5851           return build2 (wanted_code, truth_type, lhs, rhs);
5852         }
5853
5854       /* There is still another way we can do something:  If both pairs of
5855          fields being compared are adjacent, we may be able to make a wider
5856          field containing them both.
5857
5858          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5859          the mask must be shifted to account for the shift done by
5860          make_bit_field_ref.  */
5861       if ((ll_bitsize + ll_bitpos == rl_bitpos
5862            && lr_bitsize + lr_bitpos == rr_bitpos)
5863           || (ll_bitpos == rl_bitpos + rl_bitsize
5864               && lr_bitpos == rr_bitpos + rr_bitsize))
5865         {
5866           tree type;
5867
5868           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5869                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5870           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5871                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5872
5873           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5874                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5875           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5876                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5877
5878           /* Convert to the smaller type before masking out unwanted bits.  */
5879           type = lntype;
5880           if (lntype != rntype)
5881             {
5882               if (lnbitsize > rnbitsize)
5883                 {
5884                   lhs = fold_convert (rntype, lhs);
5885                   ll_mask = fold_convert (rntype, ll_mask);
5886                   type = rntype;
5887                 }
5888               else if (lnbitsize < rnbitsize)
5889                 {
5890                   rhs = fold_convert (lntype, rhs);
5891                   lr_mask = fold_convert (lntype, lr_mask);
5892                   type = lntype;
5893                 }
5894             }
5895
5896           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5897             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5898
5899           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5900             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5901
5902           return build2 (wanted_code, truth_type, lhs, rhs);
5903         }
5904
5905       return 0;
5906     }
5907
5908   /* Handle the case of comparisons with constants.  If there is something in
5909      common between the masks, those bits of the constants must be the same.
5910      If not, the condition is always false.  Test for this to avoid generating
5911      incorrect code below.  */
5912   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5913   if (! integer_zerop (result)
5914       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5915                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5916     {
5917       if (wanted_code == NE_EXPR)
5918         {
5919           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5920           return constant_boolean_node (true, truth_type);
5921         }
5922       else
5923         {
5924           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5925           return constant_boolean_node (false, truth_type);
5926         }
5927     }
5928
5929   /* Construct the expression we will return.  First get the component
5930      reference we will make.  Unless the mask is all ones the width of
5931      that field, perform the mask operation.  Then compare with the
5932      merged constant.  */
5933   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5934                                ll_unsignedp || rl_unsignedp);
5935
5936   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5937   if (! all_ones_mask_p (ll_mask, lnbitsize))
5938     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5939
5940   return build2 (wanted_code, truth_type, result,
5941                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5942 }
5943 \f
5944 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5945    constant.  */
5946
5947 static tree
5948 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5949 {
5950   tree arg0 = op0;
5951   enum tree_code op_code;
5952   tree comp_const;
5953   tree minmax_const;
5954   int consts_equal, consts_lt;
5955   tree inner;
5956
5957   STRIP_SIGN_NOPS (arg0);
5958
5959   op_code = TREE_CODE (arg0);
5960   minmax_const = TREE_OPERAND (arg0, 1);
5961   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5962   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5963   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5964   inner = TREE_OPERAND (arg0, 0);
5965
5966   /* If something does not permit us to optimize, return the original tree.  */
5967   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5968       || TREE_CODE (comp_const) != INTEGER_CST
5969       || TREE_OVERFLOW (comp_const)
5970       || TREE_CODE (minmax_const) != INTEGER_CST
5971       || TREE_OVERFLOW (minmax_const))
5972     return NULL_TREE;
5973
5974   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5975      and GT_EXPR, doing the rest with recursive calls using logical
5976      simplifications.  */
5977   switch (code)
5978     {
5979     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5980       {
5981         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5982                                           type, op0, op1);
5983         if (tem)
5984           return invert_truthvalue (tem);
5985         return NULL_TREE;
5986       }
5987
5988     case GE_EXPR:
5989       return
5990         fold_build2 (TRUTH_ORIF_EXPR, type,
5991                      optimize_minmax_comparison
5992                      (EQ_EXPR, type, arg0, comp_const),
5993                      optimize_minmax_comparison
5994                      (GT_EXPR, type, arg0, comp_const));
5995
5996     case EQ_EXPR:
5997       if (op_code == MAX_EXPR && consts_equal)
5998         /* MAX (X, 0) == 0  ->  X <= 0  */
5999         return fold_build2 (LE_EXPR, type, inner, comp_const);
6000
6001       else if (op_code == MAX_EXPR && consts_lt)
6002         /* MAX (X, 0) == 5  ->  X == 5   */
6003         return fold_build2 (EQ_EXPR, type, inner, comp_const);
6004
6005       else if (op_code == MAX_EXPR)
6006         /* MAX (X, 0) == -1  ->  false  */
6007         return omit_one_operand (type, integer_zero_node, inner);
6008
6009       else if (consts_equal)
6010         /* MIN (X, 0) == 0  ->  X >= 0  */
6011         return fold_build2 (GE_EXPR, type, inner, comp_const);
6012
6013       else if (consts_lt)
6014         /* MIN (X, 0) == 5  ->  false  */
6015         return omit_one_operand (type, integer_zero_node, inner);
6016
6017       else
6018         /* MIN (X, 0) == -1  ->  X == -1  */
6019         return fold_build2 (EQ_EXPR, type, inner, comp_const);
6020
6021     case GT_EXPR:
6022       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6023         /* MAX (X, 0) > 0  ->  X > 0
6024            MAX (X, 0) > 5  ->  X > 5  */
6025         return fold_build2 (GT_EXPR, type, inner, comp_const);
6026
6027       else if (op_code == MAX_EXPR)
6028         /* MAX (X, 0) > -1  ->  true  */
6029         return omit_one_operand (type, integer_one_node, inner);
6030
6031       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6032         /* MIN (X, 0) > 0  ->  false
6033            MIN (X, 0) > 5  ->  false  */
6034         return omit_one_operand (type, integer_zero_node, inner);
6035
6036       else
6037         /* MIN (X, 0) > -1  ->  X > -1  */
6038         return fold_build2 (GT_EXPR, type, inner, comp_const);
6039
6040     default:
6041       return NULL_TREE;
6042     }
6043 }
6044 \f
6045 /* T is an integer expression that is being multiplied, divided, or taken a
6046    modulus (CODE says which and what kind of divide or modulus) by a
6047    constant C.  See if we can eliminate that operation by folding it with
6048    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6049    should be used for the computation if wider than our type.
6050
6051    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6052    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6053    expression would not overflow or that overflow is undefined for the type
6054    in the language in question.
6055
6056    If we return a non-null expression, it is an equivalent form of the
6057    original computation, but need not be in the original type.
6058
6059    We set *STRICT_OVERFLOW_P to true if the return values depends on
6060    signed overflow being undefined.  Otherwise we do not change
6061    *STRICT_OVERFLOW_P.  */
6062
6063 static tree
6064 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6065                 bool *strict_overflow_p)
6066 {
6067   /* To avoid exponential search depth, refuse to allow recursion past
6068      three levels.  Beyond that (1) it's highly unlikely that we'll find
6069      something interesting and (2) we've probably processed it before
6070      when we built the inner expression.  */
6071
6072   static int depth;
6073   tree ret;
6074
6075   if (depth > 3)
6076     return NULL;
6077
6078   depth++;
6079   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6080   depth--;
6081
6082   return ret;
6083 }
6084
6085 static tree
6086 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6087                   bool *strict_overflow_p)
6088 {
6089   tree type = TREE_TYPE (t);
6090   enum tree_code tcode = TREE_CODE (t);
6091   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6092                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6093                 ? wide_type : type);
6094   tree t1, t2;
6095   int same_p = tcode == code;
6096   tree op0 = NULL_TREE, op1 = NULL_TREE;
6097   bool sub_strict_overflow_p;
6098
6099   /* Don't deal with constants of zero here; they confuse the code below.  */
6100   if (integer_zerop (c))
6101     return NULL_TREE;
6102
6103   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6104     op0 = TREE_OPERAND (t, 0);
6105
6106   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6107     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6108
6109   /* Note that we need not handle conditional operations here since fold
6110      already handles those cases.  So just do arithmetic here.  */
6111   switch (tcode)
6112     {
6113     case INTEGER_CST:
6114       /* For a constant, we can always simplify if we are a multiply
6115          or (for divide and modulus) if it is a multiple of our constant.  */
6116       if (code == MULT_EXPR
6117           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6118         return const_binop (code, fold_convert (ctype, t),
6119                             fold_convert (ctype, c), 0);
6120       break;
6121
6122     CASE_CONVERT: case NON_LVALUE_EXPR:
6123       /* If op0 is an expression ...  */
6124       if ((COMPARISON_CLASS_P (op0)
6125            || UNARY_CLASS_P (op0)
6126            || BINARY_CLASS_P (op0)
6127            || VL_EXP_CLASS_P (op0)
6128            || EXPRESSION_CLASS_P (op0))
6129           /* ... and has wrapping overflow, and its type is smaller
6130              than ctype, then we cannot pass through as widening.  */
6131           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6132                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6133                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6134                && (TYPE_PRECISION (ctype)
6135                    > TYPE_PRECISION (TREE_TYPE (op0))))
6136               /* ... or this is a truncation (t is narrower than op0),
6137                  then we cannot pass through this narrowing.  */
6138               || (TYPE_PRECISION (type)
6139                   < TYPE_PRECISION (TREE_TYPE (op0)))
6140               /* ... or signedness changes for division or modulus,
6141                  then we cannot pass through this conversion.  */
6142               || (code != MULT_EXPR
6143                   && (TYPE_UNSIGNED (ctype)
6144                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6145               /* ... or has undefined overflow while the converted to
6146                  type has not, we cannot do the operation in the inner type
6147                  as that would introduce undefined overflow.  */
6148               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6149                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6150         break;
6151
6152       /* Pass the constant down and see if we can make a simplification.  If
6153          we can, replace this expression with the inner simplification for
6154          possible later conversion to our or some other type.  */
6155       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6156           && TREE_CODE (t2) == INTEGER_CST
6157           && !TREE_OVERFLOW (t2)
6158           && (0 != (t1 = extract_muldiv (op0, t2, code,
6159                                          code == MULT_EXPR
6160                                          ? ctype : NULL_TREE,
6161                                          strict_overflow_p))))
6162         return t1;
6163       break;
6164
6165     case ABS_EXPR:
6166       /* If widening the type changes it from signed to unsigned, then we
6167          must avoid building ABS_EXPR itself as unsigned.  */
6168       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6169         {
6170           tree cstype = (*signed_type_for) (ctype);
6171           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6172               != 0)
6173             {
6174               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6175               return fold_convert (ctype, t1);
6176             }
6177           break;
6178         }
6179       /* If the constant is negative, we cannot simplify this.  */
6180       if (tree_int_cst_sgn (c) == -1)
6181         break;
6182       /* FALLTHROUGH */
6183     case NEGATE_EXPR:
6184       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6185           != 0)
6186         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6187       break;
6188
6189     case MIN_EXPR:  case MAX_EXPR:
6190       /* If widening the type changes the signedness, then we can't perform
6191          this optimization as that changes the result.  */
6192       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6193         break;
6194
6195       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6196       sub_strict_overflow_p = false;
6197       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6198                                 &sub_strict_overflow_p)) != 0
6199           && (t2 = extract_muldiv (op1, c, code, wide_type,
6200                                    &sub_strict_overflow_p)) != 0)
6201         {
6202           if (tree_int_cst_sgn (c) < 0)
6203             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6204           if (sub_strict_overflow_p)
6205             *strict_overflow_p = true;
6206           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6207                               fold_convert (ctype, t2));
6208         }
6209       break;
6210
6211     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6212       /* If the second operand is constant, this is a multiplication
6213          or floor division, by a power of two, so we can treat it that
6214          way unless the multiplier or divisor overflows.  Signed
6215          left-shift overflow is implementation-defined rather than
6216          undefined in C90, so do not convert signed left shift into
6217          multiplication.  */
6218       if (TREE_CODE (op1) == INTEGER_CST
6219           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6220           /* const_binop may not detect overflow correctly,
6221              so check for it explicitly here.  */
6222           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6223           && TREE_INT_CST_HIGH (op1) == 0
6224           && 0 != (t1 = fold_convert (ctype,
6225                                       const_binop (LSHIFT_EXPR,
6226                                                    size_one_node,
6227                                                    op1, 0)))
6228           && !TREE_OVERFLOW (t1))
6229         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6230                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6231                                        ctype, fold_convert (ctype, op0), t1),
6232                                c, code, wide_type, strict_overflow_p);
6233       break;
6234
6235     case PLUS_EXPR:  case MINUS_EXPR:
6236       /* See if we can eliminate the operation on both sides.  If we can, we
6237          can return a new PLUS or MINUS.  If we can't, the only remaining
6238          cases where we can do anything are if the second operand is a
6239          constant.  */
6240       sub_strict_overflow_p = false;
6241       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6242       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6243       if (t1 != 0 && t2 != 0
6244           && (code == MULT_EXPR
6245               /* If not multiplication, we can only do this if both operands
6246                  are divisible by c.  */
6247               || (multiple_of_p (ctype, op0, c)
6248                   && multiple_of_p (ctype, op1, c))))
6249         {
6250           if (sub_strict_overflow_p)
6251             *strict_overflow_p = true;
6252           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6253                               fold_convert (ctype, t2));
6254         }
6255
6256       /* If this was a subtraction, negate OP1 and set it to be an addition.
6257          This simplifies the logic below.  */
6258       if (tcode == MINUS_EXPR)
6259         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6260
6261       if (TREE_CODE (op1) != INTEGER_CST)
6262         break;
6263
6264       /* If either OP1 or C are negative, this optimization is not safe for
6265          some of the division and remainder types while for others we need
6266          to change the code.  */
6267       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6268         {
6269           if (code == CEIL_DIV_EXPR)
6270             code = FLOOR_DIV_EXPR;
6271           else if (code == FLOOR_DIV_EXPR)
6272             code = CEIL_DIV_EXPR;
6273           else if (code != MULT_EXPR
6274                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6275             break;
6276         }
6277
6278       /* If it's a multiply or a division/modulus operation of a multiple
6279          of our constant, do the operation and verify it doesn't overflow.  */
6280       if (code == MULT_EXPR
6281           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6282         {
6283           op1 = const_binop (code, fold_convert (ctype, op1),
6284                              fold_convert (ctype, c), 0);
6285           /* We allow the constant to overflow with wrapping semantics.  */
6286           if (op1 == 0
6287               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6288             break;
6289         }
6290       else
6291         break;
6292
6293       /* If we have an unsigned type is not a sizetype, we cannot widen
6294          the operation since it will change the result if the original
6295          computation overflowed.  */
6296       if (TYPE_UNSIGNED (ctype)
6297           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6298           && ctype != type)
6299         break;
6300
6301       /* If we were able to eliminate our operation from the first side,
6302          apply our operation to the second side and reform the PLUS.  */
6303       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6304         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6305
6306       /* The last case is if we are a multiply.  In that case, we can
6307          apply the distributive law to commute the multiply and addition
6308          if the multiplication of the constants doesn't overflow.  */
6309       if (code == MULT_EXPR)
6310         return fold_build2 (tcode, ctype,
6311                             fold_build2 (code, ctype,
6312                                          fold_convert (ctype, op0),
6313                                          fold_convert (ctype, c)),
6314                             op1);
6315
6316       break;
6317
6318     case MULT_EXPR:
6319       /* We have a special case here if we are doing something like
6320          (C * 8) % 4 since we know that's zero.  */
6321       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6322            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6323           /* If the multiplication can overflow we cannot optimize this.
6324              ???  Until we can properly mark individual operations as
6325              not overflowing we need to treat sizetype special here as
6326              stor-layout relies on this opimization to make
6327              DECL_FIELD_BIT_OFFSET always a constant.  */
6328           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6329               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6330                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6331           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6332           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6333         {
6334           *strict_overflow_p = true;
6335           return omit_one_operand (type, integer_zero_node, op0);
6336         }
6337
6338       /* ... fall through ...  */
6339
6340     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6341     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6342       /* If we can extract our operation from the LHS, do so and return a
6343          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6344          do something only if the second operand is a constant.  */
6345       if (same_p
6346           && (t1 = extract_muldiv (op0, c, code, wide_type,
6347                                    strict_overflow_p)) != 0)
6348         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6349                             fold_convert (ctype, op1));
6350       else if (tcode == MULT_EXPR && code == MULT_EXPR
6351                && (t1 = extract_muldiv (op1, c, code, wide_type,
6352                                         strict_overflow_p)) != 0)
6353         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6354                             fold_convert (ctype, t1));
6355       else if (TREE_CODE (op1) != INTEGER_CST)
6356         return 0;
6357
6358       /* If these are the same operation types, we can associate them
6359          assuming no overflow.  */
6360       if (tcode == code
6361           && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6362                                          fold_convert (ctype, c), 1))
6363           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6364                                                TREE_INT_CST_HIGH (t1),
6365                                                (TYPE_UNSIGNED (ctype)
6366                                                 && tcode != MULT_EXPR) ? -1 : 1,
6367                                                TREE_OVERFLOW (t1)))
6368           && !TREE_OVERFLOW (t1))
6369         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6370
6371       /* If these operations "cancel" each other, we have the main
6372          optimizations of this pass, which occur when either constant is a
6373          multiple of the other, in which case we replace this with either an
6374          operation or CODE or TCODE.
6375
6376          If we have an unsigned type that is not a sizetype, we cannot do
6377          this since it will change the result if the original computation
6378          overflowed.  */
6379       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6380            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6381           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6382               || (tcode == MULT_EXPR
6383                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6384                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6385                   && code != MULT_EXPR)))
6386         {
6387           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6388             {
6389               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6390                 *strict_overflow_p = true;
6391               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6392                                   fold_convert (ctype,
6393                                                 const_binop (TRUNC_DIV_EXPR,
6394                                                              op1, c, 0)));
6395             }
6396           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6397             {
6398               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6399                 *strict_overflow_p = true;
6400               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6401                                   fold_convert (ctype,
6402                                                 const_binop (TRUNC_DIV_EXPR,
6403                                                              c, op1, 0)));
6404             }
6405         }
6406       break;
6407
6408     default:
6409       break;
6410     }
6411
6412   return 0;
6413 }
6414 \f
6415 /* Return a node which has the indicated constant VALUE (either 0 or
6416    1), and is of the indicated TYPE.  */
6417
6418 tree
6419 constant_boolean_node (int value, tree type)
6420 {
6421   if (type == integer_type_node)
6422     return value ? integer_one_node : integer_zero_node;
6423   else if (type == boolean_type_node)
6424     return value ? boolean_true_node : boolean_false_node;
6425   else
6426     return build_int_cst (type, value);
6427 }
6428
6429
6430 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6431    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6432    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6433    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6434    COND is the first argument to CODE; otherwise (as in the example
6435    given here), it is the second argument.  TYPE is the type of the
6436    original expression.  Return NULL_TREE if no simplification is
6437    possible.  */
6438
6439 static tree
6440 fold_binary_op_with_conditional_arg (enum tree_code code,
6441                                      tree type, tree op0, tree op1,
6442                                      tree cond, tree arg, int cond_first_p)
6443 {
6444   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6445   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6446   tree test, true_value, false_value;
6447   tree lhs = NULL_TREE;
6448   tree rhs = NULL_TREE;
6449
6450   /* This transformation is only worthwhile if we don't have to wrap
6451      arg in a SAVE_EXPR, and the operation can be simplified on at least
6452      one of the branches once its pushed inside the COND_EXPR.  */
6453   if (!TREE_CONSTANT (arg))
6454     return NULL_TREE;
6455
6456   if (TREE_CODE (cond) == COND_EXPR)
6457     {
6458       test = TREE_OPERAND (cond, 0);
6459       true_value = TREE_OPERAND (cond, 1);
6460       false_value = TREE_OPERAND (cond, 2);
6461       /* If this operand throws an expression, then it does not make
6462          sense to try to perform a logical or arithmetic operation
6463          involving it.  */
6464       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6465         lhs = true_value;
6466       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6467         rhs = false_value;
6468     }
6469   else
6470     {
6471       tree testtype = TREE_TYPE (cond);
6472       test = cond;
6473       true_value = constant_boolean_node (true, testtype);
6474       false_value = constant_boolean_node (false, testtype);
6475     }
6476
6477   arg = fold_convert (arg_type, arg);
6478   if (lhs == 0)
6479     {
6480       true_value = fold_convert (cond_type, true_value);
6481       if (cond_first_p)
6482         lhs = fold_build2 (code, type, true_value, arg);
6483       else
6484         lhs = fold_build2 (code, type, arg, true_value);
6485     }
6486   if (rhs == 0)
6487     {
6488       false_value = fold_convert (cond_type, false_value);
6489       if (cond_first_p)
6490         rhs = fold_build2 (code, type, false_value, arg);
6491       else
6492         rhs = fold_build2 (code, type, arg, false_value);
6493     }
6494
6495   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6496   return fold_convert (type, test);
6497 }
6498
6499 \f
6500 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6501
6502    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6503    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6504    ADDEND is the same as X.
6505
6506    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6507    and finite.  The problematic cases are when X is zero, and its mode
6508    has signed zeros.  In the case of rounding towards -infinity,
6509    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6510    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6511
6512 bool
6513 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6514 {
6515   if (!real_zerop (addend))
6516     return false;
6517
6518   /* Don't allow the fold with -fsignaling-nans.  */
6519   if (HONOR_SNANS (TYPE_MODE (type)))
6520     return false;
6521
6522   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6523   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6524     return true;
6525
6526   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6527   if (TREE_CODE (addend) == REAL_CST
6528       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6529     negate = !negate;
6530
6531   /* The mode has signed zeros, and we have to honor their sign.
6532      In this situation, there is only one case we can return true for.
6533      X - 0 is the same as X unless rounding towards -infinity is
6534      supported.  */
6535   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6536 }
6537
6538 /* Subroutine of fold() that checks comparisons of built-in math
6539    functions against real constants.
6540
6541    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6542    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6543    is the type of the result and ARG0 and ARG1 are the operands of the
6544    comparison.  ARG1 must be a TREE_REAL_CST.
6545
6546    The function returns the constant folded tree if a simplification
6547    can be made, and NULL_TREE otherwise.  */
6548
6549 static tree
6550 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6551                      tree type, tree arg0, tree arg1)
6552 {
6553   REAL_VALUE_TYPE c;
6554
6555   if (BUILTIN_SQRT_P (fcode))
6556     {
6557       tree arg = CALL_EXPR_ARG (arg0, 0);
6558       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6559
6560       c = TREE_REAL_CST (arg1);
6561       if (REAL_VALUE_NEGATIVE (c))
6562         {
6563           /* sqrt(x) < y is always false, if y is negative.  */
6564           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6565             return omit_one_operand (type, integer_zero_node, arg);
6566
6567           /* sqrt(x) > y is always true, if y is negative and we
6568              don't care about NaNs, i.e. negative values of x.  */
6569           if (code == NE_EXPR || !HONOR_NANS (mode))
6570             return omit_one_operand (type, integer_one_node, arg);
6571
6572           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6573           return fold_build2 (GE_EXPR, type, arg,
6574                               build_real (TREE_TYPE (arg), dconst0));
6575         }
6576       else if (code == GT_EXPR || code == GE_EXPR)
6577         {
6578           REAL_VALUE_TYPE c2;
6579
6580           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6581           real_convert (&c2, mode, &c2);
6582
6583           if (REAL_VALUE_ISINF (c2))
6584             {
6585               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6586               if (HONOR_INFINITIES (mode))
6587                 return fold_build2 (EQ_EXPR, type, arg,
6588                                     build_real (TREE_TYPE (arg), c2));
6589
6590               /* sqrt(x) > y is always false, when y is very large
6591                  and we don't care about infinities.  */
6592               return omit_one_operand (type, integer_zero_node, arg);
6593             }
6594
6595           /* sqrt(x) > c is the same as x > c*c.  */
6596           return fold_build2 (code, type, arg,
6597                               build_real (TREE_TYPE (arg), c2));
6598         }
6599       else if (code == LT_EXPR || code == LE_EXPR)
6600         {
6601           REAL_VALUE_TYPE c2;
6602
6603           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6604           real_convert (&c2, mode, &c2);
6605
6606           if (REAL_VALUE_ISINF (c2))
6607             {
6608               /* sqrt(x) < y is always true, when y is a very large
6609                  value and we don't care about NaNs or Infinities.  */
6610               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6611                 return omit_one_operand (type, integer_one_node, arg);
6612
6613               /* sqrt(x) < y is x != +Inf when y is very large and we
6614                  don't care about NaNs.  */
6615               if (! HONOR_NANS (mode))
6616                 return fold_build2 (NE_EXPR, type, arg,
6617                                     build_real (TREE_TYPE (arg), c2));
6618
6619               /* sqrt(x) < y is x >= 0 when y is very large and we
6620                  don't care about Infinities.  */
6621               if (! HONOR_INFINITIES (mode))
6622                 return fold_build2 (GE_EXPR, type, arg,
6623                                     build_real (TREE_TYPE (arg), dconst0));
6624
6625               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6626               if (lang_hooks.decls.global_bindings_p () != 0
6627                   || CONTAINS_PLACEHOLDER_P (arg))
6628                 return NULL_TREE;
6629
6630               arg = save_expr (arg);
6631               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6632                                   fold_build2 (GE_EXPR, type, arg,
6633                                                build_real (TREE_TYPE (arg),
6634                                                            dconst0)),
6635                                   fold_build2 (NE_EXPR, type, arg,
6636                                                build_real (TREE_TYPE (arg),
6637                                                            c2)));
6638             }
6639
6640           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6641           if (! HONOR_NANS (mode))
6642             return fold_build2 (code, type, arg,
6643                                 build_real (TREE_TYPE (arg), c2));
6644
6645           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6646           if (lang_hooks.decls.global_bindings_p () == 0
6647               && ! CONTAINS_PLACEHOLDER_P (arg))
6648             {
6649               arg = save_expr (arg);
6650               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6651                                   fold_build2 (GE_EXPR, type, arg,
6652                                                build_real (TREE_TYPE (arg),
6653                                                            dconst0)),
6654                                   fold_build2 (code, type, arg,
6655                                                build_real (TREE_TYPE (arg),
6656                                                            c2)));
6657             }
6658         }
6659     }
6660
6661   return NULL_TREE;
6662 }
6663
6664 /* Subroutine of fold() that optimizes comparisons against Infinities,
6665    either +Inf or -Inf.
6666
6667    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6668    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6669    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6670
6671    The function returns the constant folded tree if a simplification
6672    can be made, and NULL_TREE otherwise.  */
6673
6674 static tree
6675 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6676 {
6677   enum machine_mode mode;
6678   REAL_VALUE_TYPE max;
6679   tree temp;
6680   bool neg;
6681
6682   mode = TYPE_MODE (TREE_TYPE (arg0));
6683
6684   /* For negative infinity swap the sense of the comparison.  */
6685   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6686   if (neg)
6687     code = swap_tree_comparison (code);
6688
6689   switch (code)
6690     {
6691     case GT_EXPR:
6692       /* x > +Inf is always false, if with ignore sNANs.  */
6693       if (HONOR_SNANS (mode))
6694         return NULL_TREE;
6695       return omit_one_operand (type, integer_zero_node, arg0);
6696
6697     case LE_EXPR:
6698       /* x <= +Inf is always true, if we don't case about NaNs.  */
6699       if (! HONOR_NANS (mode))
6700         return omit_one_operand (type, integer_one_node, arg0);
6701
6702       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6703       if (lang_hooks.decls.global_bindings_p () == 0
6704           && ! CONTAINS_PLACEHOLDER_P (arg0))
6705         {
6706           arg0 = save_expr (arg0);
6707           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6708         }
6709       break;
6710
6711     case EQ_EXPR:
6712     case GE_EXPR:
6713       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6714       real_maxval (&max, neg, mode);
6715       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6716                           arg0, build_real (TREE_TYPE (arg0), max));
6717
6718     case LT_EXPR:
6719       /* x < +Inf is always equal to x <= DBL_MAX.  */
6720       real_maxval (&max, neg, mode);
6721       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6722                           arg0, build_real (TREE_TYPE (arg0), max));
6723
6724     case NE_EXPR:
6725       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6726       real_maxval (&max, neg, mode);
6727       if (! HONOR_NANS (mode))
6728         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6729                             arg0, build_real (TREE_TYPE (arg0), max));
6730
6731       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6732                           arg0, build_real (TREE_TYPE (arg0), max));
6733       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6734
6735     default:
6736       break;
6737     }
6738
6739   return NULL_TREE;
6740 }
6741
6742 /* Subroutine of fold() that optimizes comparisons of a division by
6743    a nonzero integer constant against an integer constant, i.e.
6744    X/C1 op C2.
6745
6746    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6747    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6748    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6749
6750    The function returns the constant folded tree if a simplification
6751    can be made, and NULL_TREE otherwise.  */
6752
6753 static tree
6754 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6755 {
6756   tree prod, tmp, hi, lo;
6757   tree arg00 = TREE_OPERAND (arg0, 0);
6758   tree arg01 = TREE_OPERAND (arg0, 1);
6759   unsigned HOST_WIDE_INT lpart;
6760   HOST_WIDE_INT hpart;
6761   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6762   bool neg_overflow;
6763   int overflow;
6764
6765   /* We have to do this the hard way to detect unsigned overflow.
6766      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6767   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6768                                    TREE_INT_CST_HIGH (arg01),
6769                                    TREE_INT_CST_LOW (arg1),
6770                                    TREE_INT_CST_HIGH (arg1),
6771                                    &lpart, &hpart, unsigned_p);
6772   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6773                                 -1, overflow);
6774   neg_overflow = false;
6775
6776   if (unsigned_p)
6777     {
6778       tmp = int_const_binop (MINUS_EXPR, arg01,
6779                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6780       lo = prod;
6781
6782       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6783       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6784                                        TREE_INT_CST_HIGH (prod),
6785                                        TREE_INT_CST_LOW (tmp),
6786                                        TREE_INT_CST_HIGH (tmp),
6787                                        &lpart, &hpart, unsigned_p);
6788       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6789                                   -1, overflow | TREE_OVERFLOW (prod));
6790     }
6791   else if (tree_int_cst_sgn (arg01) >= 0)
6792     {
6793       tmp = int_const_binop (MINUS_EXPR, arg01,
6794                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6795       switch (tree_int_cst_sgn (arg1))
6796         {
6797         case -1:
6798           neg_overflow = true;
6799           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6800           hi = prod;
6801           break;
6802
6803         case  0:
6804           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6805           hi = tmp;
6806           break;
6807
6808         case  1:
6809           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6810           lo = prod;
6811           break;
6812
6813         default:
6814           gcc_unreachable ();
6815         }
6816     }
6817   else
6818     {
6819       /* A negative divisor reverses the relational operators.  */
6820       code = swap_tree_comparison (code);
6821
6822       tmp = int_const_binop (PLUS_EXPR, arg01,
6823                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6824       switch (tree_int_cst_sgn (arg1))
6825         {
6826         case -1:
6827           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6828           lo = prod;
6829           break;
6830
6831         case  0:
6832           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6833           lo = tmp;
6834           break;
6835
6836         case  1:
6837           neg_overflow = true;
6838           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6839           hi = prod;
6840           break;
6841
6842         default:
6843           gcc_unreachable ();
6844         }
6845     }
6846
6847   switch (code)
6848     {
6849     case EQ_EXPR:
6850       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6851         return omit_one_operand (type, integer_zero_node, arg00);
6852       if (TREE_OVERFLOW (hi))
6853         return fold_build2 (GE_EXPR, type, arg00, lo);
6854       if (TREE_OVERFLOW (lo))
6855         return fold_build2 (LE_EXPR, type, arg00, hi);
6856       return build_range_check (type, arg00, 1, lo, hi);
6857
6858     case NE_EXPR:
6859       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6860         return omit_one_operand (type, integer_one_node, arg00);
6861       if (TREE_OVERFLOW (hi))
6862         return fold_build2 (LT_EXPR, type, arg00, lo);
6863       if (TREE_OVERFLOW (lo))
6864         return fold_build2 (GT_EXPR, type, arg00, hi);
6865       return build_range_check (type, arg00, 0, lo, hi);
6866
6867     case LT_EXPR:
6868       if (TREE_OVERFLOW (lo))
6869         {
6870           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6871           return omit_one_operand (type, tmp, arg00);
6872         }
6873       return fold_build2 (LT_EXPR, type, arg00, lo);
6874
6875     case LE_EXPR:
6876       if (TREE_OVERFLOW (hi))
6877         {
6878           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6879           return omit_one_operand (type, tmp, arg00);
6880         }
6881       return fold_build2 (LE_EXPR, type, arg00, hi);
6882
6883     case GT_EXPR:
6884       if (TREE_OVERFLOW (hi))
6885         {
6886           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6887           return omit_one_operand (type, tmp, arg00);
6888         }
6889       return fold_build2 (GT_EXPR, type, arg00, hi);
6890
6891     case GE_EXPR:
6892       if (TREE_OVERFLOW (lo))
6893         {
6894           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6895           return omit_one_operand (type, tmp, arg00);
6896         }
6897       return fold_build2 (GE_EXPR, type, arg00, lo);
6898
6899     default:
6900       break;
6901     }
6902
6903   return NULL_TREE;
6904 }
6905
6906
6907 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6908    equality/inequality test, then return a simplified form of the test
6909    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6910    result type.  */
6911
6912 static tree
6913 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6914                                      tree result_type)
6915 {
6916   /* If this is testing a single bit, we can optimize the test.  */
6917   if ((code == NE_EXPR || code == EQ_EXPR)
6918       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6919       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6920     {
6921       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6922          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6923       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6924
6925       if (arg00 != NULL_TREE
6926           /* This is only a win if casting to a signed type is cheap,
6927              i.e. when arg00's type is not a partial mode.  */
6928           && TYPE_PRECISION (TREE_TYPE (arg00))
6929              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6930         {
6931           tree stype = signed_type_for (TREE_TYPE (arg00));
6932           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6933                               result_type, fold_convert (stype, arg00),
6934                               build_int_cst (stype, 0));
6935         }
6936     }
6937
6938   return NULL_TREE;
6939 }
6940
6941 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6942    equality/inequality test, then return a simplified form of
6943    the test using shifts and logical operations.  Otherwise return
6944    NULL.  TYPE is the desired result type.  */
6945
6946 tree
6947 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6948                       tree result_type)
6949 {
6950   /* If this is testing a single bit, we can optimize the test.  */
6951   if ((code == NE_EXPR || code == EQ_EXPR)
6952       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6953       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6954     {
6955       tree inner = TREE_OPERAND (arg0, 0);
6956       tree type = TREE_TYPE (arg0);
6957       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6958       enum machine_mode operand_mode = TYPE_MODE (type);
6959       int ops_unsigned;
6960       tree signed_type, unsigned_type, intermediate_type;
6961       tree tem, one;
6962
6963       /* First, see if we can fold the single bit test into a sign-bit
6964          test.  */
6965       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6966                                                  result_type);
6967       if (tem)
6968         return tem;
6969
6970       /* Otherwise we have (A & C) != 0 where C is a single bit,
6971          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6972          Similarly for (A & C) == 0.  */
6973
6974       /* If INNER is a right shift of a constant and it plus BITNUM does
6975          not overflow, adjust BITNUM and INNER.  */
6976       if (TREE_CODE (inner) == RSHIFT_EXPR
6977           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6978           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6979           && bitnum < TYPE_PRECISION (type)
6980           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6981                                    bitnum - TYPE_PRECISION (type)))
6982         {
6983           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6984           inner = TREE_OPERAND (inner, 0);
6985         }
6986
6987       /* If we are going to be able to omit the AND below, we must do our
6988          operations as unsigned.  If we must use the AND, we have a choice.
6989          Normally unsigned is faster, but for some machines signed is.  */
6990 #ifdef LOAD_EXTEND_OP
6991       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6992                       && !flag_syntax_only) ? 0 : 1;
6993 #else
6994       ops_unsigned = 1;
6995 #endif
6996
6997       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6998       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6999       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7000       inner = fold_convert (intermediate_type, inner);
7001
7002       if (bitnum != 0)
7003         inner = build2 (RSHIFT_EXPR, intermediate_type,
7004                         inner, size_int (bitnum));
7005
7006       one = build_int_cst (intermediate_type, 1);
7007
7008       if (code == EQ_EXPR)
7009         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
7010
7011       /* Put the AND last so it can combine with more things.  */
7012       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7013
7014       /* Make sure to return the proper type.  */
7015       inner = fold_convert (result_type, inner);
7016
7017       return inner;
7018     }
7019   return NULL_TREE;
7020 }
7021
7022 /* Check whether we are allowed to reorder operands arg0 and arg1,
7023    such that the evaluation of arg1 occurs before arg0.  */
7024
7025 static bool
7026 reorder_operands_p (const_tree arg0, const_tree arg1)
7027 {
7028   if (! flag_evaluation_order)
7029       return true;
7030   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7031     return true;
7032   return ! TREE_SIDE_EFFECTS (arg0)
7033          && ! TREE_SIDE_EFFECTS (arg1);
7034 }
7035
7036 /* Test whether it is preferable two swap two operands, ARG0 and
7037    ARG1, for example because ARG0 is an integer constant and ARG1
7038    isn't.  If REORDER is true, only recommend swapping if we can
7039    evaluate the operands in reverse order.  */
7040
7041 bool
7042 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7043 {
7044   STRIP_SIGN_NOPS (arg0);
7045   STRIP_SIGN_NOPS (arg1);
7046
7047   if (TREE_CODE (arg1) == INTEGER_CST)
7048     return 0;
7049   if (TREE_CODE (arg0) == INTEGER_CST)
7050     return 1;
7051
7052   if (TREE_CODE (arg1) == REAL_CST)
7053     return 0;
7054   if (TREE_CODE (arg0) == REAL_CST)
7055     return 1;
7056
7057   if (TREE_CODE (arg1) == FIXED_CST)
7058     return 0;
7059   if (TREE_CODE (arg0) == FIXED_CST)
7060     return 1;
7061
7062   if (TREE_CODE (arg1) == COMPLEX_CST)
7063     return 0;
7064   if (TREE_CODE (arg0) == COMPLEX_CST)
7065     return 1;
7066
7067   if (TREE_CONSTANT (arg1))
7068     return 0;
7069   if (TREE_CONSTANT (arg0))
7070     return 1;
7071
7072   if (optimize_function_for_size_p (cfun))
7073     return 0;
7074
7075   if (reorder && flag_evaluation_order
7076       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7077     return 0;
7078
7079   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7080      for commutative and comparison operators.  Ensuring a canonical
7081      form allows the optimizers to find additional redundancies without
7082      having to explicitly check for both orderings.  */
7083   if (TREE_CODE (arg0) == SSA_NAME
7084       && TREE_CODE (arg1) == SSA_NAME
7085       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7086     return 1;
7087
7088   /* Put SSA_NAMEs last.  */
7089   if (TREE_CODE (arg1) == SSA_NAME)
7090     return 0;
7091   if (TREE_CODE (arg0) == SSA_NAME)
7092     return 1;
7093
7094   /* Put variables last.  */
7095   if (DECL_P (arg1))
7096     return 0;
7097   if (DECL_P (arg0))
7098     return 1;
7099
7100   return 0;
7101 }
7102
7103 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7104    ARG0 is extended to a wider type.  */
7105
7106 static tree
7107 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7108 {
7109   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7110   tree arg1_unw;
7111   tree shorter_type, outer_type;
7112   tree min, max;
7113   bool above, below;
7114
7115   if (arg0_unw == arg0)
7116     return NULL_TREE;
7117   shorter_type = TREE_TYPE (arg0_unw);
7118
7119 #ifdef HAVE_canonicalize_funcptr_for_compare
7120   /* Disable this optimization if we're casting a function pointer
7121      type on targets that require function pointer canonicalization.  */
7122   if (HAVE_canonicalize_funcptr_for_compare
7123       && TREE_CODE (shorter_type) == POINTER_TYPE
7124       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7125     return NULL_TREE;
7126 #endif
7127
7128   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7129     return NULL_TREE;
7130
7131   arg1_unw = get_unwidened (arg1, NULL_TREE);
7132
7133   /* If possible, express the comparison in the shorter mode.  */
7134   if ((code == EQ_EXPR || code == NE_EXPR
7135        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7136       && (TREE_TYPE (arg1_unw) == shorter_type
7137           || ((TYPE_PRECISION (shorter_type)
7138                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7139               && (TYPE_UNSIGNED (shorter_type)
7140                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7141           || (TREE_CODE (arg1_unw) == INTEGER_CST
7142               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7143                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7144               && int_fits_type_p (arg1_unw, shorter_type))))
7145     return fold_build2 (code, type, arg0_unw,
7146                        fold_convert (shorter_type, arg1_unw));
7147
7148   if (TREE_CODE (arg1_unw) != INTEGER_CST
7149       || TREE_CODE (shorter_type) != INTEGER_TYPE
7150       || !int_fits_type_p (arg1_unw, shorter_type))
7151     return NULL_TREE;
7152
7153   /* If we are comparing with the integer that does not fit into the range
7154      of the shorter type, the result is known.  */
7155   outer_type = TREE_TYPE (arg1_unw);
7156   min = lower_bound_in_type (outer_type, shorter_type);
7157   max = upper_bound_in_type (outer_type, shorter_type);
7158
7159   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7160                                                    max, arg1_unw));
7161   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7162                                                    arg1_unw, min));
7163
7164   switch (code)
7165     {
7166     case EQ_EXPR:
7167       if (above || below)
7168         return omit_one_operand (type, integer_zero_node, arg0);
7169       break;
7170
7171     case NE_EXPR:
7172       if (above || below)
7173         return omit_one_operand (type, integer_one_node, arg0);
7174       break;
7175
7176     case LT_EXPR:
7177     case LE_EXPR:
7178       if (above)
7179         return omit_one_operand (type, integer_one_node, arg0);
7180       else if (below)
7181         return omit_one_operand (type, integer_zero_node, arg0);
7182
7183     case GT_EXPR:
7184     case GE_EXPR:
7185       if (above)
7186         return omit_one_operand (type, integer_zero_node, arg0);
7187       else if (below)
7188         return omit_one_operand (type, integer_one_node, arg0);
7189
7190     default:
7191       break;
7192     }
7193
7194   return NULL_TREE;
7195 }
7196
7197 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7198    ARG0 just the signedness is changed.  */
7199
7200 static tree
7201 fold_sign_changed_comparison (enum tree_code code, tree type,
7202                               tree arg0, tree arg1)
7203 {
7204   tree arg0_inner;
7205   tree inner_type, outer_type;
7206
7207   if (!CONVERT_EXPR_P (arg0))
7208     return NULL_TREE;
7209
7210   outer_type = TREE_TYPE (arg0);
7211   arg0_inner = TREE_OPERAND (arg0, 0);
7212   inner_type = TREE_TYPE (arg0_inner);
7213
7214 #ifdef HAVE_canonicalize_funcptr_for_compare
7215   /* Disable this optimization if we're casting a function pointer
7216      type on targets that require function pointer canonicalization.  */
7217   if (HAVE_canonicalize_funcptr_for_compare
7218       && TREE_CODE (inner_type) == POINTER_TYPE
7219       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7220     return NULL_TREE;
7221 #endif
7222
7223   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7224     return NULL_TREE;
7225
7226   if (TREE_CODE (arg1) != INTEGER_CST
7227       && !(CONVERT_EXPR_P (arg1)
7228            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7229     return NULL_TREE;
7230
7231   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7232        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7233       && code != NE_EXPR
7234       && code != EQ_EXPR)
7235     return NULL_TREE;
7236
7237   if (TREE_CODE (arg1) == INTEGER_CST)
7238     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7239                                   TREE_INT_CST_HIGH (arg1), 0,
7240                                   TREE_OVERFLOW (arg1));
7241   else
7242     arg1 = fold_convert (inner_type, arg1);
7243
7244   return fold_build2 (code, type, arg0_inner, arg1);
7245 }
7246
7247 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7248    step of the array.  Reconstructs s and delta in the case of s * delta
7249    being an integer constant (and thus already folded).
7250    ADDR is the address. MULT is the multiplicative expression.
7251    If the function succeeds, the new address expression is returned.  Otherwise
7252    NULL_TREE is returned.  */
7253
7254 static tree
7255 try_move_mult_to_index (tree addr, tree op1)
7256 {
7257   tree s, delta, step;
7258   tree ref = TREE_OPERAND (addr, 0), pref;
7259   tree ret, pos;
7260   tree itype;
7261   bool mdim = false;
7262
7263   /*  Strip the nops that might be added when converting op1 to sizetype. */
7264   STRIP_NOPS (op1);
7265
7266   /* Canonicalize op1 into a possibly non-constant delta
7267      and an INTEGER_CST s.  */
7268   if (TREE_CODE (op1) == MULT_EXPR)
7269     {
7270       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7271
7272       STRIP_NOPS (arg0);
7273       STRIP_NOPS (arg1);
7274   
7275       if (TREE_CODE (arg0) == INTEGER_CST)
7276         {
7277           s = arg0;
7278           delta = arg1;
7279         }
7280       else if (TREE_CODE (arg1) == INTEGER_CST)
7281         {
7282           s = arg1;
7283           delta = arg0;
7284         }
7285       else
7286         return NULL_TREE;
7287     }
7288   else if (TREE_CODE (op1) == INTEGER_CST)
7289     {
7290       delta = op1;
7291       s = NULL_TREE;
7292     }
7293   else
7294     {
7295       /* Simulate we are delta * 1.  */
7296       delta = op1;
7297       s = integer_one_node;
7298     }
7299
7300   for (;; ref = TREE_OPERAND (ref, 0))
7301     {
7302       if (TREE_CODE (ref) == ARRAY_REF)
7303         {
7304           /* Remember if this was a multi-dimensional array.  */
7305           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7306             mdim = true;
7307
7308           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7309           if (! itype)
7310             continue;
7311
7312           step = array_ref_element_size (ref);
7313           if (TREE_CODE (step) != INTEGER_CST)
7314             continue;
7315
7316           if (s)
7317             {
7318               if (! tree_int_cst_equal (step, s))
7319                 continue;
7320             }
7321           else
7322             {
7323               /* Try if delta is a multiple of step.  */
7324               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7325               if (! tmp)
7326                 continue;
7327               delta = tmp;
7328             }
7329
7330           /* Only fold here if we can verify we do not overflow one
7331              dimension of a multi-dimensional array.  */
7332           if (mdim)
7333             {
7334               tree tmp;
7335
7336               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7337                   || !INTEGRAL_TYPE_P (itype)
7338                   || !TYPE_MAX_VALUE (itype)
7339                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7340                 continue;
7341
7342               tmp = fold_binary (PLUS_EXPR, itype,
7343                                  fold_convert (itype,
7344                                                TREE_OPERAND (ref, 1)),
7345                                  fold_convert (itype, delta));
7346               if (!tmp
7347                   || TREE_CODE (tmp) != INTEGER_CST
7348                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7349                 continue;
7350             }
7351
7352           break;
7353         }
7354       else
7355         mdim = false;
7356
7357       if (!handled_component_p (ref))
7358         return NULL_TREE;
7359     }
7360
7361   /* We found the suitable array reference.  So copy everything up to it,
7362      and replace the index.  */
7363
7364   pref = TREE_OPERAND (addr, 0);
7365   ret = copy_node (pref);
7366   pos = ret;
7367
7368   while (pref != ref)
7369     {
7370       pref = TREE_OPERAND (pref, 0);
7371       TREE_OPERAND (pos, 0) = copy_node (pref);
7372       pos = TREE_OPERAND (pos, 0);
7373     }
7374
7375   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7376                                        fold_convert (itype,
7377                                                      TREE_OPERAND (pos, 1)),
7378                                        fold_convert (itype, delta));
7379
7380   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7381 }
7382
7383
7384 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7385    means A >= Y && A != MAX, but in this case we know that
7386    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7387
7388 static tree
7389 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7390 {
7391   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7392
7393   if (TREE_CODE (bound) == LT_EXPR)
7394     a = TREE_OPERAND (bound, 0);
7395   else if (TREE_CODE (bound) == GT_EXPR)
7396     a = TREE_OPERAND (bound, 1);
7397   else
7398     return NULL_TREE;
7399
7400   typea = TREE_TYPE (a);
7401   if (!INTEGRAL_TYPE_P (typea)
7402       && !POINTER_TYPE_P (typea))
7403     return NULL_TREE;
7404
7405   if (TREE_CODE (ineq) == LT_EXPR)
7406     {
7407       a1 = TREE_OPERAND (ineq, 1);
7408       y = TREE_OPERAND (ineq, 0);
7409     }
7410   else if (TREE_CODE (ineq) == GT_EXPR)
7411     {
7412       a1 = TREE_OPERAND (ineq, 0);
7413       y = TREE_OPERAND (ineq, 1);
7414     }
7415   else
7416     return NULL_TREE;
7417
7418   if (TREE_TYPE (a1) != typea)
7419     return NULL_TREE;
7420
7421   if (POINTER_TYPE_P (typea))
7422     {
7423       /* Convert the pointer types into integer before taking the difference.  */
7424       tree ta = fold_convert (ssizetype, a);
7425       tree ta1 = fold_convert (ssizetype, a1);
7426       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7427     }
7428   else
7429    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7430
7431   if (!diff || !integer_onep (diff))
7432    return NULL_TREE;
7433
7434   return fold_build2 (GE_EXPR, type, a, y);
7435 }
7436
7437 /* Fold a sum or difference of at least one multiplication.
7438    Returns the folded tree or NULL if no simplification could be made.  */
7439
7440 static tree
7441 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7442 {
7443   tree arg00, arg01, arg10, arg11;
7444   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7445
7446   /* (A * C) +- (B * C) -> (A+-B) * C.
7447      (A * C) +- A -> A * (C+-1).
7448      We are most concerned about the case where C is a constant,
7449      but other combinations show up during loop reduction.  Since
7450      it is not difficult, try all four possibilities.  */
7451
7452   if (TREE_CODE (arg0) == MULT_EXPR)
7453     {
7454       arg00 = TREE_OPERAND (arg0, 0);
7455       arg01 = TREE_OPERAND (arg0, 1);
7456     }
7457   else if (TREE_CODE (arg0) == INTEGER_CST)
7458     {
7459       arg00 = build_one_cst (type);
7460       arg01 = arg0;
7461     }
7462   else
7463     {
7464       /* We cannot generate constant 1 for fract.  */
7465       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7466         return NULL_TREE;
7467       arg00 = arg0;
7468       arg01 = build_one_cst (type);
7469     }
7470   if (TREE_CODE (arg1) == MULT_EXPR)
7471     {
7472       arg10 = TREE_OPERAND (arg1, 0);
7473       arg11 = TREE_OPERAND (arg1, 1);
7474     }
7475   else if (TREE_CODE (arg1) == INTEGER_CST)
7476     {
7477       arg10 = build_one_cst (type);
7478       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7479          the purpose of this canonicalization.  */
7480       if (TREE_INT_CST_HIGH (arg1) == -1
7481           && negate_expr_p (arg1)
7482           && code == PLUS_EXPR)
7483         {
7484           arg11 = negate_expr (arg1);
7485           code = MINUS_EXPR;
7486         }
7487       else
7488         arg11 = arg1;
7489     }
7490   else
7491     {
7492       /* We cannot generate constant 1 for fract.  */
7493       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7494         return NULL_TREE;
7495       arg10 = arg1;
7496       arg11 = build_one_cst (type);
7497     }
7498   same = NULL_TREE;
7499
7500   if (operand_equal_p (arg01, arg11, 0))
7501     same = arg01, alt0 = arg00, alt1 = arg10;
7502   else if (operand_equal_p (arg00, arg10, 0))
7503     same = arg00, alt0 = arg01, alt1 = arg11;
7504   else if (operand_equal_p (arg00, arg11, 0))
7505     same = arg00, alt0 = arg01, alt1 = arg10;
7506   else if (operand_equal_p (arg01, arg10, 0))
7507     same = arg01, alt0 = arg00, alt1 = arg11;
7508
7509   /* No identical multiplicands; see if we can find a common
7510      power-of-two factor in non-power-of-two multiplies.  This
7511      can help in multi-dimensional array access.  */
7512   else if (host_integerp (arg01, 0)
7513            && host_integerp (arg11, 0))
7514     {
7515       HOST_WIDE_INT int01, int11, tmp;
7516       bool swap = false;
7517       tree maybe_same;
7518       int01 = TREE_INT_CST_LOW (arg01);
7519       int11 = TREE_INT_CST_LOW (arg11);
7520
7521       /* Move min of absolute values to int11.  */
7522       if ((int01 >= 0 ? int01 : -int01)
7523           < (int11 >= 0 ? int11 : -int11))
7524         {
7525           tmp = int01, int01 = int11, int11 = tmp;
7526           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7527           maybe_same = arg01;
7528           swap = true;
7529         }
7530       else
7531         maybe_same = arg11;
7532
7533       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7534           /* The remainder should not be a constant, otherwise we
7535              end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7536              increased the number of multiplications necessary.  */
7537           && TREE_CODE (arg10) != INTEGER_CST)
7538         {
7539           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7540                               build_int_cst (TREE_TYPE (arg00),
7541                                              int01 / int11));
7542           alt1 = arg10;
7543           same = maybe_same;
7544           if (swap)
7545             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7546         }
7547     }
7548
7549   if (same)
7550     return fold_build2 (MULT_EXPR, type,
7551                         fold_build2 (code, type,
7552                                      fold_convert (type, alt0),
7553                                      fold_convert (type, alt1)),
7554                         fold_convert (type, same));
7555
7556   return NULL_TREE;
7557 }
7558
7559 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7560    specified by EXPR into the buffer PTR of length LEN bytes.
7561    Return the number of bytes placed in the buffer, or zero
7562    upon failure.  */
7563
7564 static int
7565 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7566 {
7567   tree type = TREE_TYPE (expr);
7568   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7569   int byte, offset, word, words;
7570   unsigned char value;
7571
7572   if (total_bytes > len)
7573     return 0;
7574   words = total_bytes / UNITS_PER_WORD;
7575
7576   for (byte = 0; byte < total_bytes; byte++)
7577     {
7578       int bitpos = byte * BITS_PER_UNIT;
7579       if (bitpos < HOST_BITS_PER_WIDE_INT)
7580         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7581       else
7582         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7583                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7584
7585       if (total_bytes > UNITS_PER_WORD)
7586         {
7587           word = byte / UNITS_PER_WORD;
7588           if (WORDS_BIG_ENDIAN)
7589             word = (words - 1) - word;
7590           offset = word * UNITS_PER_WORD;
7591           if (BYTES_BIG_ENDIAN)
7592             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7593           else
7594             offset += byte % UNITS_PER_WORD;
7595         }
7596       else
7597         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7598       ptr[offset] = value;
7599     }
7600   return total_bytes;
7601 }
7602
7603
7604 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7605    specified by EXPR into the buffer PTR of length LEN bytes.
7606    Return the number of bytes placed in the buffer, or zero
7607    upon failure.  */
7608
7609 static int
7610 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7611 {
7612   tree type = TREE_TYPE (expr);
7613   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7614   int byte, offset, word, words, bitpos;
7615   unsigned char value;
7616
7617   /* There are always 32 bits in each long, no matter the size of
7618      the hosts long.  We handle floating point representations with
7619      up to 192 bits.  */
7620   long tmp[6];
7621
7622   if (total_bytes > len)
7623     return 0;
7624   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7625
7626   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7627
7628   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7629        bitpos += BITS_PER_UNIT)
7630     {
7631       byte = (bitpos / BITS_PER_UNIT) & 3;
7632       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7633
7634       if (UNITS_PER_WORD < 4)
7635         {
7636           word = byte / UNITS_PER_WORD;
7637           if (WORDS_BIG_ENDIAN)
7638             word = (words - 1) - word;
7639           offset = word * UNITS_PER_WORD;
7640           if (BYTES_BIG_ENDIAN)
7641             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7642           else
7643             offset += byte % UNITS_PER_WORD;
7644         }
7645       else
7646         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7647       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7648     }
7649   return total_bytes;
7650 }
7651
7652 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7653    specified by EXPR into the buffer PTR of length LEN bytes.
7654    Return the number of bytes placed in the buffer, or zero
7655    upon failure.  */
7656
7657 static int
7658 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7659 {
7660   int rsize, isize;
7661   tree part;
7662
7663   part = TREE_REALPART (expr);
7664   rsize = native_encode_expr (part, ptr, len);
7665   if (rsize == 0)
7666     return 0;
7667   part = TREE_IMAGPART (expr);
7668   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7669   if (isize != rsize)
7670     return 0;
7671   return rsize + isize;
7672 }
7673
7674
7675 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7676    specified by EXPR into the buffer PTR of length LEN bytes.
7677    Return the number of bytes placed in the buffer, or zero
7678    upon failure.  */
7679
7680 static int
7681 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7682 {
7683   int i, size, offset, count;
7684   tree itype, elem, elements;
7685
7686   offset = 0;
7687   elements = TREE_VECTOR_CST_ELTS (expr);
7688   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7689   itype = TREE_TYPE (TREE_TYPE (expr));
7690   size = GET_MODE_SIZE (TYPE_MODE (itype));
7691   for (i = 0; i < count; i++)
7692     {
7693       if (elements)
7694         {
7695           elem = TREE_VALUE (elements);
7696           elements = TREE_CHAIN (elements);
7697         }
7698       else
7699         elem = NULL_TREE;
7700
7701       if (elem)
7702         {
7703           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7704             return 0;
7705         }
7706       else
7707         {
7708           if (offset + size > len)
7709             return 0;
7710           memset (ptr+offset, 0, size);
7711         }
7712       offset += size;
7713     }
7714   return offset;
7715 }
7716
7717
7718 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7719    specified by EXPR into the buffer PTR of length LEN bytes.
7720    Return the number of bytes placed in the buffer, or zero
7721    upon failure.  */
7722
7723 static int
7724 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7725 {
7726   tree type = TREE_TYPE (expr);
7727   HOST_WIDE_INT total_bytes;
7728
7729   if (TREE_CODE (type) != ARRAY_TYPE
7730       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7731       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7732       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7733     return 0;
7734   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7735   if (total_bytes > len)
7736     return 0;
7737   if (TREE_STRING_LENGTH (expr) < total_bytes)
7738     {
7739       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7740       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7741               total_bytes - TREE_STRING_LENGTH (expr));
7742     }
7743   else
7744     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7745   return total_bytes;
7746 }
7747
7748
7749 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7750    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7751    buffer PTR of length LEN bytes.  Return the number of bytes
7752    placed in the buffer, or zero upon failure.  */
7753
7754 int
7755 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7756 {
7757   switch (TREE_CODE (expr))
7758     {
7759     case INTEGER_CST:
7760       return native_encode_int (expr, ptr, len);
7761
7762     case REAL_CST:
7763       return native_encode_real (expr, ptr, len);
7764
7765     case COMPLEX_CST:
7766       return native_encode_complex (expr, ptr, len);
7767
7768     case VECTOR_CST:
7769       return native_encode_vector (expr, ptr, len);
7770
7771     case STRING_CST:
7772       return native_encode_string (expr, ptr, len);
7773
7774     default:
7775       return 0;
7776     }
7777 }
7778
7779
7780 /* Subroutine of native_interpret_expr.  Interpret the contents of
7781    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7782    If the buffer cannot be interpreted, return NULL_TREE.  */
7783
7784 static tree
7785 native_interpret_int (tree type, const unsigned char *ptr, int len)
7786 {
7787   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7788   int byte, offset, word, words;
7789   unsigned char value;
7790   unsigned int HOST_WIDE_INT lo = 0;
7791   HOST_WIDE_INT hi = 0;
7792
7793   if (total_bytes > len)
7794     return NULL_TREE;
7795   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7796     return NULL_TREE;
7797   words = total_bytes / UNITS_PER_WORD;
7798
7799   for (byte = 0; byte < total_bytes; byte++)
7800     {
7801       int bitpos = byte * BITS_PER_UNIT;
7802       if (total_bytes > UNITS_PER_WORD)
7803         {
7804           word = byte / UNITS_PER_WORD;
7805           if (WORDS_BIG_ENDIAN)
7806             word = (words - 1) - word;
7807           offset = word * UNITS_PER_WORD;
7808           if (BYTES_BIG_ENDIAN)
7809             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7810           else
7811             offset += byte % UNITS_PER_WORD;
7812         }
7813       else
7814         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7815       value = ptr[offset];
7816
7817       if (bitpos < HOST_BITS_PER_WIDE_INT)
7818         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7819       else
7820         hi |= (unsigned HOST_WIDE_INT) value
7821               << (bitpos - HOST_BITS_PER_WIDE_INT);
7822     }
7823
7824   return build_int_cst_wide_type (type, lo, hi);
7825 }
7826
7827
7828 /* Subroutine of native_interpret_expr.  Interpret the contents of
7829    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7830    If the buffer cannot be interpreted, return NULL_TREE.  */
7831
7832 static tree
7833 native_interpret_real (tree type, const unsigned char *ptr, int len)
7834 {
7835   enum machine_mode mode = TYPE_MODE (type);
7836   int total_bytes = GET_MODE_SIZE (mode);
7837   int byte, offset, word, words, bitpos;
7838   unsigned char value;
7839   /* There are always 32 bits in each long, no matter the size of
7840      the hosts long.  We handle floating point representations with
7841      up to 192 bits.  */
7842   REAL_VALUE_TYPE r;
7843   long tmp[6];
7844
7845   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7846   if (total_bytes > len || total_bytes > 24)
7847     return NULL_TREE;
7848   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7849
7850   memset (tmp, 0, sizeof (tmp));
7851   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7852        bitpos += BITS_PER_UNIT)
7853     {
7854       byte = (bitpos / BITS_PER_UNIT) & 3;
7855       if (UNITS_PER_WORD < 4)
7856         {
7857           word = byte / UNITS_PER_WORD;
7858           if (WORDS_BIG_ENDIAN)
7859             word = (words - 1) - word;
7860           offset = word * UNITS_PER_WORD;
7861           if (BYTES_BIG_ENDIAN)
7862             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7863           else
7864             offset += byte % UNITS_PER_WORD;
7865         }
7866       else
7867         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7868       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7869
7870       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7871     }
7872
7873   real_from_target (&r, tmp, mode);
7874   return build_real (type, r);
7875 }
7876
7877
7878 /* Subroutine of native_interpret_expr.  Interpret the contents of
7879    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7880    If the buffer cannot be interpreted, return NULL_TREE.  */
7881
7882 static tree
7883 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7884 {
7885   tree etype, rpart, ipart;
7886   int size;
7887
7888   etype = TREE_TYPE (type);
7889   size = GET_MODE_SIZE (TYPE_MODE (etype));
7890   if (size * 2 > len)
7891     return NULL_TREE;
7892   rpart = native_interpret_expr (etype, ptr, size);
7893   if (!rpart)
7894     return NULL_TREE;
7895   ipart = native_interpret_expr (etype, ptr+size, size);
7896   if (!ipart)
7897     return NULL_TREE;
7898   return build_complex (type, rpart, ipart);
7899 }
7900
7901
7902 /* Subroutine of native_interpret_expr.  Interpret the contents of
7903    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7904    If the buffer cannot be interpreted, return NULL_TREE.  */
7905
7906 static tree
7907 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7908 {
7909   tree etype, elem, elements;
7910   int i, size, count;
7911
7912   etype = TREE_TYPE (type);
7913   size = GET_MODE_SIZE (TYPE_MODE (etype));
7914   count = TYPE_VECTOR_SUBPARTS (type);
7915   if (size * count > len)
7916     return NULL_TREE;
7917
7918   elements = NULL_TREE;
7919   for (i = count - 1; i >= 0; i--)
7920     {
7921       elem = native_interpret_expr (etype, ptr+(i*size), size);
7922       if (!elem)
7923         return NULL_TREE;
7924       elements = tree_cons (NULL_TREE, elem, elements);
7925     }
7926   return build_vector (type, elements);
7927 }
7928
7929
7930 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7931    the buffer PTR of length LEN as a constant of type TYPE.  For
7932    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7933    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7934    return NULL_TREE.  */
7935
7936 tree
7937 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7938 {
7939   switch (TREE_CODE (type))
7940     {
7941     case INTEGER_TYPE:
7942     case ENUMERAL_TYPE:
7943     case BOOLEAN_TYPE:
7944       return native_interpret_int (type, ptr, len);
7945
7946     case REAL_TYPE:
7947       return native_interpret_real (type, ptr, len);
7948
7949     case COMPLEX_TYPE:
7950       return native_interpret_complex (type, ptr, len);
7951
7952     case VECTOR_TYPE:
7953       return native_interpret_vector (type, ptr, len);
7954
7955     default:
7956       return NULL_TREE;
7957     }
7958 }
7959
7960
7961 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7962    TYPE at compile-time.  If we're unable to perform the conversion
7963    return NULL_TREE.  */
7964
7965 static tree
7966 fold_view_convert_expr (tree type, tree expr)
7967 {
7968   /* We support up to 512-bit values (for V8DFmode).  */
7969   unsigned char buffer[64];
7970   int len;
7971
7972   /* Check that the host and target are sane.  */
7973   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7974     return NULL_TREE;
7975
7976   len = native_encode_expr (expr, buffer, sizeof (buffer));
7977   if (len == 0)
7978     return NULL_TREE;
7979
7980   return native_interpret_expr (type, buffer, len);
7981 }
7982
7983 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7984    to avoid confusing the gimplify process.  */
7985
7986 tree
7987 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7988 {
7989   /* The size of the object is not relevant when talking about its address.  */
7990   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7991     t = TREE_OPERAND (t, 0);
7992
7993   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7994   if (TREE_CODE (t) == INDIRECT_REF
7995       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7996     {
7997       t = TREE_OPERAND (t, 0);
7998
7999       if (TREE_TYPE (t) != ptrtype)
8000         t = build1 (NOP_EXPR, ptrtype, t);
8001     }
8002   else
8003     t = build1 (ADDR_EXPR, ptrtype, t);
8004
8005   return t;
8006 }
8007
8008 /* Build an expression for the address of T.  */
8009
8010 tree
8011 build_fold_addr_expr (tree t)
8012 {
8013   tree ptrtype = build_pointer_type (TREE_TYPE (t));
8014
8015   return build_fold_addr_expr_with_type (t, ptrtype);
8016 }
8017
8018 /* Fold a unary expression of code CODE and type TYPE with operand
8019    OP0.  Return the folded expression if folding is successful.
8020    Otherwise, return NULL_TREE.  */
8021
8022 tree
8023 fold_unary (enum tree_code code, tree type, tree op0)
8024 {
8025   tree tem;
8026   tree arg0;
8027   enum tree_code_class kind = TREE_CODE_CLASS (code);
8028
8029   gcc_assert (IS_EXPR_CODE_CLASS (kind)
8030               && TREE_CODE_LENGTH (code) == 1);
8031
8032   arg0 = op0;
8033   if (arg0)
8034     {
8035       if (CONVERT_EXPR_CODE_P (code)
8036           || code == FLOAT_EXPR || code == ABS_EXPR)
8037         {
8038           /* Don't use STRIP_NOPS, because signedness of argument type
8039              matters.  */
8040           STRIP_SIGN_NOPS (arg0);
8041         }
8042       else
8043         {
8044           /* Strip any conversions that don't change the mode.  This
8045              is safe for every expression, except for a comparison
8046              expression because its signedness is derived from its
8047              operands.
8048
8049              Note that this is done as an internal manipulation within
8050              the constant folder, in order to find the simplest
8051              representation of the arguments so that their form can be
8052              studied.  In any cases, the appropriate type conversions
8053              should be put back in the tree that will get out of the
8054              constant folder.  */
8055           STRIP_NOPS (arg0);
8056         }
8057     }
8058
8059   if (TREE_CODE_CLASS (code) == tcc_unary)
8060     {
8061       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8062         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8063                        fold_build1 (code, type,
8064                                     fold_convert (TREE_TYPE (op0),
8065                                                   TREE_OPERAND (arg0, 1))));
8066       else if (TREE_CODE (arg0) == COND_EXPR)
8067         {
8068           tree arg01 = TREE_OPERAND (arg0, 1);
8069           tree arg02 = TREE_OPERAND (arg0, 2);
8070           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8071             arg01 = fold_build1 (code, type,
8072                                  fold_convert (TREE_TYPE (op0), arg01));
8073           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8074             arg02 = fold_build1 (code, type,
8075                                  fold_convert (TREE_TYPE (op0), arg02));
8076           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8077                              arg01, arg02);
8078
8079           /* If this was a conversion, and all we did was to move into
8080              inside the COND_EXPR, bring it back out.  But leave it if
8081              it is a conversion from integer to integer and the
8082              result precision is no wider than a word since such a
8083              conversion is cheap and may be optimized away by combine,
8084              while it couldn't if it were outside the COND_EXPR.  Then return
8085              so we don't get into an infinite recursion loop taking the
8086              conversion out and then back in.  */
8087
8088           if ((CONVERT_EXPR_CODE_P (code)
8089                || code == NON_LVALUE_EXPR)
8090               && TREE_CODE (tem) == COND_EXPR
8091               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8092               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8093               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8094               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8095               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8096                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8097               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8098                      && (INTEGRAL_TYPE_P
8099                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8100                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8101                   || flag_syntax_only))
8102             tem = build1 (code, type,
8103                           build3 (COND_EXPR,
8104                                   TREE_TYPE (TREE_OPERAND
8105                                              (TREE_OPERAND (tem, 1), 0)),
8106                                   TREE_OPERAND (tem, 0),
8107                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8108                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8109           return tem;
8110         }
8111       else if (COMPARISON_CLASS_P (arg0))
8112         {
8113           if (TREE_CODE (type) == BOOLEAN_TYPE)
8114             {
8115               arg0 = copy_node (arg0);
8116               TREE_TYPE (arg0) = type;
8117               return arg0;
8118             }
8119           else if (TREE_CODE (type) != INTEGER_TYPE)
8120             return fold_build3 (COND_EXPR, type, arg0,
8121                                 fold_build1 (code, type,
8122                                              integer_one_node),
8123                                 fold_build1 (code, type,
8124                                              integer_zero_node));
8125         }
8126    }
8127
8128   switch (code)
8129     {
8130     case PAREN_EXPR:
8131       /* Re-association barriers around constants and other re-association
8132          barriers can be removed.  */
8133       if (CONSTANT_CLASS_P (op0)
8134           || TREE_CODE (op0) == PAREN_EXPR)
8135         return fold_convert (type, op0);
8136       return NULL_TREE;
8137
8138     CASE_CONVERT:
8139     case FLOAT_EXPR:
8140     case FIX_TRUNC_EXPR:
8141       if (TREE_TYPE (op0) == type)
8142         return op0;
8143       
8144       /* If we have (type) (a CMP b) and type is an integral type, return
8145          new expression involving the new type.  */
8146       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8147         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8148                             TREE_OPERAND (op0, 1));
8149
8150       /* Handle cases of two conversions in a row.  */
8151       if (CONVERT_EXPR_P (op0))
8152         {
8153           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8154           tree inter_type = TREE_TYPE (op0);
8155           int inside_int = INTEGRAL_TYPE_P (inside_type);
8156           int inside_ptr = POINTER_TYPE_P (inside_type);
8157           int inside_float = FLOAT_TYPE_P (inside_type);
8158           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8159           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8160           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8161           int inter_int = INTEGRAL_TYPE_P (inter_type);
8162           int inter_ptr = POINTER_TYPE_P (inter_type);
8163           int inter_float = FLOAT_TYPE_P (inter_type);
8164           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8165           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8166           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8167           int final_int = INTEGRAL_TYPE_P (type);
8168           int final_ptr = POINTER_TYPE_P (type);
8169           int final_float = FLOAT_TYPE_P (type);
8170           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8171           unsigned int final_prec = TYPE_PRECISION (type);
8172           int final_unsignedp = TYPE_UNSIGNED (type);
8173
8174           /* In addition to the cases of two conversions in a row
8175              handled below, if we are converting something to its own
8176              type via an object of identical or wider precision, neither
8177              conversion is needed.  */
8178           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8179               && (((inter_int || inter_ptr) && final_int)
8180                   || (inter_float && final_float))
8181               && inter_prec >= final_prec)
8182             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8183
8184           /* Likewise, if the intermediate and initial types are either both
8185              float or both integer, we don't need the middle conversion if the
8186              former is wider than the latter and doesn't change the signedness
8187              (for integers).  Avoid this if the final type is a pointer since
8188              then we sometimes need the middle conversion.  Likewise if the
8189              final type has a precision not equal to the size of its mode.  */
8190           if (((inter_int && inside_int)
8191                || (inter_float && inside_float)
8192                || (inter_vec && inside_vec))
8193               && inter_prec >= inside_prec
8194               && (inter_float || inter_vec
8195                   || inter_unsignedp == inside_unsignedp)
8196               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8197                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8198               && ! final_ptr
8199               && (! final_vec || inter_prec == inside_prec))
8200             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8201
8202           /* If we have a sign-extension of a zero-extended value, we can
8203              replace that by a single zero-extension.  */
8204           if (inside_int && inter_int && final_int
8205               && inside_prec < inter_prec && inter_prec < final_prec
8206               && inside_unsignedp && !inter_unsignedp)
8207             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8208
8209           /* Two conversions in a row are not needed unless:
8210              - some conversion is floating-point (overstrict for now), or
8211              - some conversion is a vector (overstrict for now), or
8212              - the intermediate type is narrower than both initial and
8213                final, or
8214              - the intermediate type and innermost type differ in signedness,
8215                and the outermost type is wider than the intermediate, or
8216              - the initial type is a pointer type and the precisions of the
8217                intermediate and final types differ, or
8218              - the final type is a pointer type and the precisions of the
8219                initial and intermediate types differ.  */
8220           if (! inside_float && ! inter_float && ! final_float
8221               && ! inside_vec && ! inter_vec && ! final_vec
8222               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8223               && ! (inside_int && inter_int
8224                     && inter_unsignedp != inside_unsignedp
8225                     && inter_prec < final_prec)
8226               && ((inter_unsignedp && inter_prec > inside_prec)
8227                   == (final_unsignedp && final_prec > inter_prec))
8228               && ! (inside_ptr && inter_prec != final_prec)
8229               && ! (final_ptr && inside_prec != inter_prec)
8230               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8231                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8232             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8233         }
8234
8235       /* Handle (T *)&A.B.C for A being of type T and B and C
8236          living at offset zero.  This occurs frequently in
8237          C++ upcasting and then accessing the base.  */
8238       if (TREE_CODE (op0) == ADDR_EXPR
8239           && POINTER_TYPE_P (type)
8240           && handled_component_p (TREE_OPERAND (op0, 0)))
8241         {
8242           HOST_WIDE_INT bitsize, bitpos;
8243           tree offset;
8244           enum machine_mode mode;
8245           int unsignedp, volatilep;
8246           tree base = TREE_OPERAND (op0, 0);
8247           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8248                                       &mode, &unsignedp, &volatilep, false);
8249           /* If the reference was to a (constant) zero offset, we can use
8250              the address of the base if it has the same base type
8251              as the result type.  */
8252           if (! offset && bitpos == 0
8253               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8254                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8255             return fold_convert (type, build_fold_addr_expr (base));
8256         }
8257
8258       if (TREE_CODE (op0) == MODIFY_EXPR
8259           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8260           /* Detect assigning a bitfield.  */
8261           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8262                && DECL_BIT_FIELD
8263                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8264         {
8265           /* Don't leave an assignment inside a conversion
8266              unless assigning a bitfield.  */
8267           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8268           /* First do the assignment, then return converted constant.  */
8269           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8270           TREE_NO_WARNING (tem) = 1;
8271           TREE_USED (tem) = 1;
8272           return tem;
8273         }
8274
8275       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8276          constants (if x has signed type, the sign bit cannot be set
8277          in c).  This folds extension into the BIT_AND_EXPR.
8278          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8279          very likely don't have maximal range for their precision and this
8280          transformation effectively doesn't preserve non-maximal ranges.  */
8281       if (TREE_CODE (type) == INTEGER_TYPE
8282           && TREE_CODE (op0) == BIT_AND_EXPR
8283           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8284         {
8285           tree and = op0;
8286           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8287           int change = 0;
8288
8289           if (TYPE_UNSIGNED (TREE_TYPE (and))
8290               || (TYPE_PRECISION (type)
8291                   <= TYPE_PRECISION (TREE_TYPE (and))))
8292             change = 1;
8293           else if (TYPE_PRECISION (TREE_TYPE (and1))
8294                    <= HOST_BITS_PER_WIDE_INT
8295                    && host_integerp (and1, 1))
8296             {
8297               unsigned HOST_WIDE_INT cst;
8298
8299               cst = tree_low_cst (and1, 1);
8300               cst &= (HOST_WIDE_INT) -1
8301                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8302               change = (cst == 0);
8303 #ifdef LOAD_EXTEND_OP
8304               if (change
8305                   && !flag_syntax_only
8306                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8307                       == ZERO_EXTEND))
8308                 {
8309                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8310                   and0 = fold_convert (uns, and0);
8311                   and1 = fold_convert (uns, and1);
8312                 }
8313 #endif
8314             }
8315           if (change)
8316             {
8317               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8318                                            TREE_INT_CST_HIGH (and1), 0,
8319                                            TREE_OVERFLOW (and1));
8320               return fold_build2 (BIT_AND_EXPR, type,
8321                                   fold_convert (type, and0), tem);
8322             }
8323         }
8324
8325       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8326          when one of the new casts will fold away. Conservatively we assume
8327          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8328       if (POINTER_TYPE_P (type)
8329           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8330           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8331               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8332               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8333         {
8334           tree arg00 = TREE_OPERAND (arg0, 0);
8335           tree arg01 = TREE_OPERAND (arg0, 1);
8336
8337           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8338                               fold_convert (sizetype, arg01));
8339         }
8340
8341       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8342          of the same precision, and X is an integer type not narrower than
8343          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8344       if (INTEGRAL_TYPE_P (type)
8345           && TREE_CODE (op0) == BIT_NOT_EXPR
8346           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8347           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8348           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8349         {
8350           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8351           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8352               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8353             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8354         }
8355
8356       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8357          type of X and Y (integer types only).  */
8358       if (INTEGRAL_TYPE_P (type)
8359           && TREE_CODE (op0) == MULT_EXPR
8360           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8361           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8362         {
8363           /* Be careful not to introduce new overflows.  */
8364           tree mult_type;
8365           if (TYPE_OVERFLOW_WRAPS (type))
8366             mult_type = type;
8367           else
8368             mult_type = unsigned_type_for (type);
8369
8370           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8371             {
8372               tem = fold_build2 (MULT_EXPR, mult_type,
8373                                  fold_convert (mult_type,
8374                                                TREE_OPERAND (op0, 0)),
8375                                  fold_convert (mult_type,
8376                                                TREE_OPERAND (op0, 1)));
8377               return fold_convert (type, tem);
8378             }
8379         }
8380
8381       tem = fold_convert_const (code, type, op0);
8382       return tem ? tem : NULL_TREE;
8383
8384     case FIXED_CONVERT_EXPR:
8385       tem = fold_convert_const (code, type, arg0);
8386       return tem ? tem : NULL_TREE;
8387
8388     case VIEW_CONVERT_EXPR:
8389       if (TREE_TYPE (op0) == type)
8390         return op0;
8391       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8392         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8393
8394       /* For integral conversions with the same precision or pointer
8395          conversions use a NOP_EXPR instead.  */
8396       if ((INTEGRAL_TYPE_P (type)
8397            || POINTER_TYPE_P (type))
8398           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8399               || POINTER_TYPE_P (TREE_TYPE (op0)))
8400           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8401         return fold_convert (type, op0);
8402
8403       /* Strip inner integral conversions that do not change the precision.  */
8404       if (CONVERT_EXPR_P (op0)
8405           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8406               || POINTER_TYPE_P (TREE_TYPE (op0)))
8407           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8408               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8409           && (TYPE_PRECISION (TREE_TYPE (op0))
8410               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8411         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8412
8413       return fold_view_convert_expr (type, op0);
8414
8415     case NEGATE_EXPR:
8416       tem = fold_negate_expr (arg0);
8417       if (tem)
8418         return fold_convert (type, tem);
8419       return NULL_TREE;
8420
8421     case ABS_EXPR:
8422       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8423         return fold_abs_const (arg0, type);
8424       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8425         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8426       /* Convert fabs((double)float) into (double)fabsf(float).  */
8427       else if (TREE_CODE (arg0) == NOP_EXPR
8428                && TREE_CODE (type) == REAL_TYPE)
8429         {
8430           tree targ0 = strip_float_extensions (arg0);
8431           if (targ0 != arg0)
8432             return fold_convert (type, fold_build1 (ABS_EXPR,
8433                                                     TREE_TYPE (targ0),
8434                                                     targ0));
8435         }
8436       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8437       else if (TREE_CODE (arg0) == ABS_EXPR)
8438         return arg0;
8439       else if (tree_expr_nonnegative_p (arg0))
8440         return arg0;
8441
8442       /* Strip sign ops from argument.  */
8443       if (TREE_CODE (type) == REAL_TYPE)
8444         {
8445           tem = fold_strip_sign_ops (arg0);
8446           if (tem)
8447             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8448         }
8449       return NULL_TREE;
8450
8451     case CONJ_EXPR:
8452       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8453         return fold_convert (type, arg0);
8454       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8455         {
8456           tree itype = TREE_TYPE (type);
8457           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8458           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8459           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8460         }
8461       if (TREE_CODE (arg0) == COMPLEX_CST)
8462         {
8463           tree itype = TREE_TYPE (type);
8464           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8465           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8466           return build_complex (type, rpart, negate_expr (ipart));
8467         }
8468       if (TREE_CODE (arg0) == CONJ_EXPR)
8469         return fold_convert (type, TREE_OPERAND (arg0, 0));
8470       return NULL_TREE;
8471
8472     case BIT_NOT_EXPR:
8473       if (TREE_CODE (arg0) == INTEGER_CST)
8474         return fold_not_const (arg0, type);
8475       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8476         return fold_convert (type, TREE_OPERAND (arg0, 0));
8477       /* Convert ~ (-A) to A - 1.  */
8478       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8479         return fold_build2 (MINUS_EXPR, type,
8480                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8481                             build_int_cst (type, 1));
8482       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8483       else if (INTEGRAL_TYPE_P (type)
8484                && ((TREE_CODE (arg0) == MINUS_EXPR
8485                     && integer_onep (TREE_OPERAND (arg0, 1)))
8486                    || (TREE_CODE (arg0) == PLUS_EXPR
8487                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8488         return fold_build1 (NEGATE_EXPR, type,
8489                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8490       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8491       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8492                && (tem = fold_unary (BIT_NOT_EXPR, type,
8493                                      fold_convert (type,
8494                                                    TREE_OPERAND (arg0, 0)))))
8495         return fold_build2 (BIT_XOR_EXPR, type, tem,
8496                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8497       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8498                && (tem = fold_unary (BIT_NOT_EXPR, type,
8499                                      fold_convert (type,
8500                                                    TREE_OPERAND (arg0, 1)))))
8501         return fold_build2 (BIT_XOR_EXPR, type,
8502                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8503       /* Perform BIT_NOT_EXPR on each element individually.  */
8504       else if (TREE_CODE (arg0) == VECTOR_CST)
8505         {
8506           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8507           int count = TYPE_VECTOR_SUBPARTS (type), i;
8508
8509           for (i = 0; i < count; i++)
8510             {
8511               if (elements)
8512                 {
8513                   elem = TREE_VALUE (elements);
8514                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8515                   if (elem == NULL_TREE)
8516                     break;
8517                   elements = TREE_CHAIN (elements);
8518                 }
8519               else
8520                 elem = build_int_cst (TREE_TYPE (type), -1);
8521               list = tree_cons (NULL_TREE, elem, list);
8522             }
8523           if (i == count)
8524             return build_vector (type, nreverse (list));
8525         }
8526
8527       return NULL_TREE;
8528
8529     case TRUTH_NOT_EXPR:
8530       /* The argument to invert_truthvalue must have Boolean type.  */
8531       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8532           arg0 = fold_convert (boolean_type_node, arg0);
8533
8534       /* Note that the operand of this must be an int
8535          and its values must be 0 or 1.
8536          ("true" is a fixed value perhaps depending on the language,
8537          but we don't handle values other than 1 correctly yet.)  */
8538       tem = fold_truth_not_expr (arg0);
8539       if (!tem)
8540         return NULL_TREE;
8541       return fold_convert (type, tem);
8542
8543     case REALPART_EXPR:
8544       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8545         return fold_convert (type, arg0);
8546       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8547         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8548                                  TREE_OPERAND (arg0, 1));
8549       if (TREE_CODE (arg0) == COMPLEX_CST)
8550         return fold_convert (type, TREE_REALPART (arg0));
8551       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8552         {
8553           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8554           tem = fold_build2 (TREE_CODE (arg0), itype,
8555                              fold_build1 (REALPART_EXPR, itype,
8556                                           TREE_OPERAND (arg0, 0)),
8557                              fold_build1 (REALPART_EXPR, itype,
8558                                           TREE_OPERAND (arg0, 1)));
8559           return fold_convert (type, tem);
8560         }
8561       if (TREE_CODE (arg0) == CONJ_EXPR)
8562         {
8563           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8564           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8565           return fold_convert (type, tem);
8566         }
8567       if (TREE_CODE (arg0) == CALL_EXPR)
8568         {
8569           tree fn = get_callee_fndecl (arg0);
8570           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8571             switch (DECL_FUNCTION_CODE (fn))
8572               {
8573               CASE_FLT_FN (BUILT_IN_CEXPI):
8574                 fn = mathfn_built_in (type, BUILT_IN_COS);
8575                 if (fn)
8576                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8577                 break;
8578
8579               default:
8580                 break;
8581               }
8582         }
8583       return NULL_TREE;
8584
8585     case IMAGPART_EXPR:
8586       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8587         return fold_convert (type, integer_zero_node);
8588       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8589         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8590                                  TREE_OPERAND (arg0, 0));
8591       if (TREE_CODE (arg0) == COMPLEX_CST)
8592         return fold_convert (type, TREE_IMAGPART (arg0));
8593       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8594         {
8595           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8596           tem = fold_build2 (TREE_CODE (arg0), itype,
8597                              fold_build1 (IMAGPART_EXPR, itype,
8598                                           TREE_OPERAND (arg0, 0)),
8599                              fold_build1 (IMAGPART_EXPR, itype,
8600                                           TREE_OPERAND (arg0, 1)));
8601           return fold_convert (type, tem);
8602         }
8603       if (TREE_CODE (arg0) == CONJ_EXPR)
8604         {
8605           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8606           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8607           return fold_convert (type, negate_expr (tem));
8608         }
8609       if (TREE_CODE (arg0) == CALL_EXPR)
8610         {
8611           tree fn = get_callee_fndecl (arg0);
8612           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8613             switch (DECL_FUNCTION_CODE (fn))
8614               {
8615               CASE_FLT_FN (BUILT_IN_CEXPI):
8616                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8617                 if (fn)
8618                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8619                 break;
8620
8621               default:
8622                 break;
8623               }
8624         }
8625       return NULL_TREE;
8626
8627     default:
8628       return NULL_TREE;
8629     } /* switch (code) */
8630 }
8631
8632
8633 /* If the operation was a conversion do _not_ mark a resulting constant
8634    with TREE_OVERFLOW if the original constant was not.  These conversions
8635    have implementation defined behavior and retaining the TREE_OVERFLOW
8636    flag here would confuse later passes such as VRP.  */
8637 tree
8638 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8639 {
8640   tree res = fold_unary (code, type, op0);
8641   if (res
8642       && TREE_CODE (res) == INTEGER_CST
8643       && TREE_CODE (op0) == INTEGER_CST
8644       && CONVERT_EXPR_CODE_P (code))
8645     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8646
8647   return res;
8648 }
8649
8650 /* Fold a binary expression of code CODE and type TYPE with operands
8651    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8652    Return the folded expression if folding is successful.  Otherwise,
8653    return NULL_TREE.  */
8654
8655 static tree
8656 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8657 {
8658   enum tree_code compl_code;
8659
8660   if (code == MIN_EXPR)
8661     compl_code = MAX_EXPR;
8662   else if (code == MAX_EXPR)
8663     compl_code = MIN_EXPR;
8664   else
8665     gcc_unreachable ();
8666
8667   /* MIN (MAX (a, b), b) == b.  */
8668   if (TREE_CODE (op0) == compl_code
8669       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8670     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8671
8672   /* MIN (MAX (b, a), b) == b.  */
8673   if (TREE_CODE (op0) == compl_code
8674       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8675       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8676     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8677
8678   /* MIN (a, MAX (a, b)) == a.  */
8679   if (TREE_CODE (op1) == compl_code
8680       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8681       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8682     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8683
8684   /* MIN (a, MAX (b, a)) == a.  */
8685   if (TREE_CODE (op1) == compl_code
8686       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8687       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8688     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8689
8690   return NULL_TREE;
8691 }
8692
8693 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8694    by changing CODE to reduce the magnitude of constants involved in
8695    ARG0 of the comparison.
8696    Returns a canonicalized comparison tree if a simplification was
8697    possible, otherwise returns NULL_TREE.
8698    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8699    valid if signed overflow is undefined.  */
8700
8701 static tree
8702 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8703                                  tree arg0, tree arg1,
8704                                  bool *strict_overflow_p)
8705 {
8706   enum tree_code code0 = TREE_CODE (arg0);
8707   tree t, cst0 = NULL_TREE;
8708   int sgn0;
8709   bool swap = false;
8710
8711   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8712      first form only if overflow is undefined.  */
8713   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8714          /* In principle pointers also have undefined overflow behavior,
8715             but that causes problems elsewhere.  */
8716          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8717          && (code0 == MINUS_EXPR
8718              || code0 == PLUS_EXPR)
8719          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8720         || code0 == INTEGER_CST))
8721     return NULL_TREE;
8722
8723   /* Identify the constant in arg0 and its sign.  */
8724   if (code0 == INTEGER_CST)
8725     cst0 = arg0;
8726   else
8727     cst0 = TREE_OPERAND (arg0, 1);
8728   sgn0 = tree_int_cst_sgn (cst0);
8729
8730   /* Overflowed constants and zero will cause problems.  */
8731   if (integer_zerop (cst0)
8732       || TREE_OVERFLOW (cst0))
8733     return NULL_TREE;
8734
8735   /* See if we can reduce the magnitude of the constant in
8736      arg0 by changing the comparison code.  */
8737   if (code0 == INTEGER_CST)
8738     {
8739       /* CST <= arg1  ->  CST-1 < arg1.  */
8740       if (code == LE_EXPR && sgn0 == 1)
8741         code = LT_EXPR;
8742       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8743       else if (code == LT_EXPR && sgn0 == -1)
8744         code = LE_EXPR;
8745       /* CST > arg1  ->  CST-1 >= arg1.  */
8746       else if (code == GT_EXPR && sgn0 == 1)
8747         code = GE_EXPR;
8748       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8749       else if (code == GE_EXPR && sgn0 == -1)
8750         code = GT_EXPR;
8751       else
8752         return NULL_TREE;
8753       /* arg1 code' CST' might be more canonical.  */
8754       swap = true;
8755     }
8756   else
8757     {
8758       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8759       if (code == LT_EXPR
8760           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8761         code = LE_EXPR;
8762       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8763       else if (code == GT_EXPR
8764                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8765         code = GE_EXPR;
8766       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8767       else if (code == LE_EXPR
8768                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8769         code = LT_EXPR;
8770       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8771       else if (code == GE_EXPR
8772                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8773         code = GT_EXPR;
8774       else
8775         return NULL_TREE;
8776       *strict_overflow_p = true;
8777     }
8778
8779   /* Now build the constant reduced in magnitude.  But not if that
8780      would produce one outside of its types range.  */
8781   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8782       && ((sgn0 == 1
8783            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8784            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8785           || (sgn0 == -1
8786               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8787               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8788     /* We cannot swap the comparison here as that would cause us to
8789        endlessly recurse.  */
8790     return NULL_TREE;
8791
8792   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8793                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8794   if (code0 != INTEGER_CST)
8795     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8796
8797   /* If swapping might yield to a more canonical form, do so.  */
8798   if (swap)
8799     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8800   else
8801     return fold_build2 (code, type, t, arg1);
8802 }
8803
8804 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8805    overflow further.  Try to decrease the magnitude of constants involved
8806    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8807    and put sole constants at the second argument position.
8808    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8809
8810 static tree
8811 maybe_canonicalize_comparison (enum tree_code code, tree type,
8812                                tree arg0, tree arg1)
8813 {
8814   tree t;
8815   bool strict_overflow_p;
8816   const char * const warnmsg = G_("assuming signed overflow does not occur "
8817                                   "when reducing constant in comparison");
8818
8819   /* Try canonicalization by simplifying arg0.  */
8820   strict_overflow_p = false;
8821   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8822                                        &strict_overflow_p);
8823   if (t)
8824     {
8825       if (strict_overflow_p)
8826         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8827       return t;
8828     }
8829
8830   /* Try canonicalization by simplifying arg1 using the swapped
8831      comparison.  */
8832   code = swap_tree_comparison (code);
8833   strict_overflow_p = false;
8834   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8835                                        &strict_overflow_p);
8836   if (t && strict_overflow_p)
8837     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8838   return t;
8839 }
8840
8841 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8842    space.  This is used to avoid issuing overflow warnings for
8843    expressions like &p->x which can not wrap.  */
8844
8845 static bool
8846 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8847 {
8848   unsigned HOST_WIDE_INT offset_low, total_low;
8849   HOST_WIDE_INT size, offset_high, total_high;
8850
8851   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8852     return true;
8853
8854   if (bitpos < 0)
8855     return true;
8856
8857   if (offset == NULL_TREE)
8858     {
8859       offset_low = 0;
8860       offset_high = 0;
8861     }
8862   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8863     return true;
8864   else
8865     {
8866       offset_low = TREE_INT_CST_LOW (offset);
8867       offset_high = TREE_INT_CST_HIGH (offset);
8868     }
8869
8870   if (add_double_with_sign (offset_low, offset_high,
8871                             bitpos / BITS_PER_UNIT, 0,
8872                             &total_low, &total_high,
8873                             true))
8874     return true;
8875
8876   if (total_high != 0)
8877     return true;
8878
8879   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8880   if (size <= 0)
8881     return true;
8882
8883   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8884      array.  */
8885   if (TREE_CODE (base) == ADDR_EXPR)
8886     {
8887       HOST_WIDE_INT base_size;
8888
8889       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8890       if (base_size > 0 && size < base_size)
8891         size = base_size;
8892     }
8893
8894   return total_low > (unsigned HOST_WIDE_INT) size;
8895 }
8896
8897 /* Subroutine of fold_binary.  This routine performs all of the
8898    transformations that are common to the equality/inequality
8899    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8900    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8901    fold_binary should call fold_binary.  Fold a comparison with
8902    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8903    the folded comparison or NULL_TREE.  */
8904
8905 static tree
8906 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8907 {
8908   tree arg0, arg1, tem;
8909
8910   arg0 = op0;
8911   arg1 = op1;
8912
8913   STRIP_SIGN_NOPS (arg0);
8914   STRIP_SIGN_NOPS (arg1);
8915
8916   tem = fold_relational_const (code, type, arg0, arg1);
8917   if (tem != NULL_TREE)
8918     return tem;
8919
8920   /* If one arg is a real or integer constant, put it last.  */
8921   if (tree_swap_operands_p (arg0, arg1, true))
8922     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8923
8924   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8925   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8926       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8927           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8928           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8929       && (TREE_CODE (arg1) == INTEGER_CST
8930           && !TREE_OVERFLOW (arg1)))
8931     {
8932       tree const1 = TREE_OPERAND (arg0, 1);
8933       tree const2 = arg1;
8934       tree variable = TREE_OPERAND (arg0, 0);
8935       tree lhs;
8936       int lhs_add;
8937       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8938
8939       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8940                          TREE_TYPE (arg1), const2, const1);
8941
8942       /* If the constant operation overflowed this can be
8943          simplified as a comparison against INT_MAX/INT_MIN.  */
8944       if (TREE_CODE (lhs) == INTEGER_CST
8945           && TREE_OVERFLOW (lhs))
8946         {
8947           int const1_sgn = tree_int_cst_sgn (const1);
8948           enum tree_code code2 = code;
8949
8950           /* Get the sign of the constant on the lhs if the
8951              operation were VARIABLE + CONST1.  */
8952           if (TREE_CODE (arg0) == MINUS_EXPR)
8953             const1_sgn = -const1_sgn;
8954
8955           /* The sign of the constant determines if we overflowed
8956              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8957              Canonicalize to the INT_MIN overflow by swapping the comparison
8958              if necessary.  */
8959           if (const1_sgn == -1)
8960             code2 = swap_tree_comparison (code);
8961
8962           /* We now can look at the canonicalized case
8963                VARIABLE + 1  CODE2  INT_MIN
8964              and decide on the result.  */
8965           if (code2 == LT_EXPR
8966               || code2 == LE_EXPR
8967               || code2 == EQ_EXPR)
8968             return omit_one_operand (type, boolean_false_node, variable);
8969           else if (code2 == NE_EXPR
8970                    || code2 == GE_EXPR
8971                    || code2 == GT_EXPR)
8972             return omit_one_operand (type, boolean_true_node, variable);
8973         }
8974
8975       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8976           && (TREE_CODE (lhs) != INTEGER_CST
8977               || !TREE_OVERFLOW (lhs)))
8978         {
8979           fold_overflow_warning (("assuming signed overflow does not occur "
8980                                   "when changing X +- C1 cmp C2 to "
8981                                   "X cmp C1 +- C2"),
8982                                  WARN_STRICT_OVERFLOW_COMPARISON);
8983           return fold_build2 (code, type, variable, lhs);
8984         }
8985     }
8986
8987   /* For comparisons of pointers we can decompose it to a compile time
8988      comparison of the base objects and the offsets into the object.
8989      This requires at least one operand being an ADDR_EXPR or a
8990      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8991   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8992       && (TREE_CODE (arg0) == ADDR_EXPR
8993           || TREE_CODE (arg1) == ADDR_EXPR
8994           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8995           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8996     {
8997       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8998       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8999       enum machine_mode mode;
9000       int volatilep, unsignedp;
9001       bool indirect_base0 = false, indirect_base1 = false;
9002
9003       /* Get base and offset for the access.  Strip ADDR_EXPR for
9004          get_inner_reference, but put it back by stripping INDIRECT_REF
9005          off the base object if possible.  indirect_baseN will be true
9006          if baseN is not an address but refers to the object itself.  */
9007       base0 = arg0;
9008       if (TREE_CODE (arg0) == ADDR_EXPR)
9009         {
9010           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9011                                        &bitsize, &bitpos0, &offset0, &mode,
9012                                        &unsignedp, &volatilep, false);
9013           if (TREE_CODE (base0) == INDIRECT_REF)
9014             base0 = TREE_OPERAND (base0, 0);
9015           else
9016             indirect_base0 = true;
9017         }
9018       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9019         {
9020           base0 = TREE_OPERAND (arg0, 0);
9021           offset0 = TREE_OPERAND (arg0, 1);
9022         }
9023
9024       base1 = arg1;
9025       if (TREE_CODE (arg1) == ADDR_EXPR)
9026         {
9027           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9028                                        &bitsize, &bitpos1, &offset1, &mode,
9029                                        &unsignedp, &volatilep, false);
9030           if (TREE_CODE (base1) == INDIRECT_REF)
9031             base1 = TREE_OPERAND (base1, 0);
9032           else
9033             indirect_base1 = true;
9034         }
9035       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9036         {
9037           base1 = TREE_OPERAND (arg1, 0);
9038           offset1 = TREE_OPERAND (arg1, 1);
9039         }
9040
9041       /* If we have equivalent bases we might be able to simplify.  */
9042       if (indirect_base0 == indirect_base1
9043           && operand_equal_p (base0, base1, 0))
9044         {
9045           /* We can fold this expression to a constant if the non-constant
9046              offset parts are equal.  */
9047           if ((offset0 == offset1
9048                || (offset0 && offset1
9049                    && operand_equal_p (offset0, offset1, 0)))
9050               && (code == EQ_EXPR
9051                   || code == NE_EXPR
9052                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9053                 
9054             {
9055               if (code != EQ_EXPR
9056                   && code != NE_EXPR
9057                   && bitpos0 != bitpos1
9058                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9059                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9060                 fold_overflow_warning (("assuming pointer wraparound does not "
9061                                         "occur when comparing P +- C1 with "
9062                                         "P +- C2"),
9063                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9064
9065               switch (code)
9066                 {
9067                 case EQ_EXPR:
9068                   return constant_boolean_node (bitpos0 == bitpos1, type);
9069                 case NE_EXPR:
9070                   return constant_boolean_node (bitpos0 != bitpos1, type);
9071                 case LT_EXPR:
9072                   return constant_boolean_node (bitpos0 < bitpos1, type);
9073                 case LE_EXPR:
9074                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9075                 case GE_EXPR:
9076                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9077                 case GT_EXPR:
9078                   return constant_boolean_node (bitpos0 > bitpos1, type);
9079                 default:;
9080                 }
9081             }
9082           /* We can simplify the comparison to a comparison of the variable
9083              offset parts if the constant offset parts are equal.
9084              Be careful to use signed size type here because otherwise we
9085              mess with array offsets in the wrong way.  This is possible
9086              because pointer arithmetic is restricted to retain within an
9087              object and overflow on pointer differences is undefined as of
9088              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9089           else if (bitpos0 == bitpos1
9090                    && ((code == EQ_EXPR || code == NE_EXPR)
9091                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9092             {
9093               tree signed_size_type_node;
9094               signed_size_type_node = signed_type_for (size_type_node);
9095
9096               /* By converting to signed size type we cover middle-end pointer
9097                  arithmetic which operates on unsigned pointer types of size
9098                  type size and ARRAY_REF offsets which are properly sign or
9099                  zero extended from their type in case it is narrower than
9100                  size type.  */
9101               if (offset0 == NULL_TREE)
9102                 offset0 = build_int_cst (signed_size_type_node, 0);
9103               else
9104                 offset0 = fold_convert (signed_size_type_node, offset0);
9105               if (offset1 == NULL_TREE)
9106                 offset1 = build_int_cst (signed_size_type_node, 0);
9107               else
9108                 offset1 = fold_convert (signed_size_type_node, offset1);
9109
9110               if (code != EQ_EXPR
9111                   && code != NE_EXPR
9112                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9113                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9114                 fold_overflow_warning (("assuming pointer wraparound does not "
9115                                         "occur when comparing P +- C1 with "
9116                                         "P +- C2"),
9117                                        WARN_STRICT_OVERFLOW_COMPARISON);
9118
9119               return fold_build2 (code, type, offset0, offset1);
9120             }
9121         }
9122       /* For non-equal bases we can simplify if they are addresses
9123          of local binding decls or constants.  */
9124       else if (indirect_base0 && indirect_base1
9125                /* We know that !operand_equal_p (base0, base1, 0)
9126                   because the if condition was false.  But make
9127                   sure two decls are not the same.  */
9128                && base0 != base1
9129                && TREE_CODE (arg0) == ADDR_EXPR
9130                && TREE_CODE (arg1) == ADDR_EXPR
9131                && (((TREE_CODE (base0) == VAR_DECL
9132                      || TREE_CODE (base0) == PARM_DECL)
9133                     && (targetm.binds_local_p (base0)
9134                         || CONSTANT_CLASS_P (base1)))
9135                    || CONSTANT_CLASS_P (base0))
9136                && (((TREE_CODE (base1) == VAR_DECL
9137                      || TREE_CODE (base1) == PARM_DECL)
9138                     && (targetm.binds_local_p (base1)
9139                         || CONSTANT_CLASS_P (base0)))
9140                    || CONSTANT_CLASS_P (base1)))
9141         {
9142           if (code == EQ_EXPR)
9143             return omit_two_operands (type, boolean_false_node, arg0, arg1);
9144           else if (code == NE_EXPR)
9145             return omit_two_operands (type, boolean_true_node, arg0, arg1);
9146         }
9147       /* For equal offsets we can simplify to a comparison of the
9148          base addresses.  */
9149       else if (bitpos0 == bitpos1
9150                && (indirect_base0
9151                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9152                && (indirect_base1
9153                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9154                && ((offset0 == offset1)
9155                    || (offset0 && offset1
9156                        && operand_equal_p (offset0, offset1, 0))))
9157         {
9158           if (indirect_base0)
9159             base0 = build_fold_addr_expr (base0);
9160           if (indirect_base1)
9161             base1 = build_fold_addr_expr (base1);
9162           return fold_build2 (code, type, base0, base1);
9163         }
9164     }
9165
9166   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9167      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9168      the resulting offset is smaller in absolute value than the
9169      original one.  */
9170   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9171       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9172       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9173           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9174       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9175       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9176           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9177     {
9178       tree const1 = TREE_OPERAND (arg0, 1);
9179       tree const2 = TREE_OPERAND (arg1, 1);
9180       tree variable1 = TREE_OPERAND (arg0, 0);
9181       tree variable2 = TREE_OPERAND (arg1, 0);
9182       tree cst;
9183       const char * const warnmsg = G_("assuming signed overflow does not "
9184                                       "occur when combining constants around "
9185                                       "a comparison");
9186
9187       /* Put the constant on the side where it doesn't overflow and is
9188          of lower absolute value than before.  */
9189       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9190                              ? MINUS_EXPR : PLUS_EXPR,
9191                              const2, const1, 0);
9192       if (!TREE_OVERFLOW (cst)
9193           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9194         {
9195           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9196           return fold_build2 (code, type,
9197                               variable1,
9198                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9199                                            variable2, cst));
9200         }
9201
9202       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9203                              ? MINUS_EXPR : PLUS_EXPR,
9204                              const1, const2, 0);
9205       if (!TREE_OVERFLOW (cst)
9206           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9207         {
9208           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9209           return fold_build2 (code, type,
9210                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9211                                            variable1, cst),
9212                               variable2);
9213         }
9214     }
9215
9216   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9217      signed arithmetic case.  That form is created by the compiler
9218      often enough for folding it to be of value.  One example is in
9219      computing loop trip counts after Operator Strength Reduction.  */
9220   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9221       && TREE_CODE (arg0) == MULT_EXPR
9222       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9223           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9224       && integer_zerop (arg1))
9225     {
9226       tree const1 = TREE_OPERAND (arg0, 1);
9227       tree const2 = arg1;                       /* zero */
9228       tree variable1 = TREE_OPERAND (arg0, 0);
9229       enum tree_code cmp_code = code;
9230
9231       gcc_assert (!integer_zerop (const1));
9232
9233       fold_overflow_warning (("assuming signed overflow does not occur when "
9234                               "eliminating multiplication in comparison "
9235                               "with zero"),
9236                              WARN_STRICT_OVERFLOW_COMPARISON);
9237
9238       /* If const1 is negative we swap the sense of the comparison.  */
9239       if (tree_int_cst_sgn (const1) < 0)
9240         cmp_code = swap_tree_comparison (cmp_code);
9241
9242       return fold_build2 (cmp_code, type, variable1, const2);
9243     }
9244
9245   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9246   if (tem)
9247     return tem;
9248
9249   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9250     {
9251       tree targ0 = strip_float_extensions (arg0);
9252       tree targ1 = strip_float_extensions (arg1);
9253       tree newtype = TREE_TYPE (targ0);
9254
9255       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9256         newtype = TREE_TYPE (targ1);
9257
9258       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9259       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9260         return fold_build2 (code, type, fold_convert (newtype, targ0),
9261                             fold_convert (newtype, targ1));
9262
9263       /* (-a) CMP (-b) -> b CMP a  */
9264       if (TREE_CODE (arg0) == NEGATE_EXPR
9265           && TREE_CODE (arg1) == NEGATE_EXPR)
9266         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9267                             TREE_OPERAND (arg0, 0));
9268
9269       if (TREE_CODE (arg1) == REAL_CST)
9270         {
9271           REAL_VALUE_TYPE cst;
9272           cst = TREE_REAL_CST (arg1);
9273
9274           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9275           if (TREE_CODE (arg0) == NEGATE_EXPR)
9276             return fold_build2 (swap_tree_comparison (code), type,
9277                                 TREE_OPERAND (arg0, 0),
9278                                 build_real (TREE_TYPE (arg1),
9279                                             REAL_VALUE_NEGATE (cst)));
9280
9281           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9282           /* a CMP (-0) -> a CMP 0  */
9283           if (REAL_VALUE_MINUS_ZERO (cst))
9284             return fold_build2 (code, type, arg0,
9285                                 build_real (TREE_TYPE (arg1), dconst0));
9286
9287           /* x != NaN is always true, other ops are always false.  */
9288           if (REAL_VALUE_ISNAN (cst)
9289               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9290             {
9291               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9292               return omit_one_operand (type, tem, arg0);
9293             }
9294
9295           /* Fold comparisons against infinity.  */
9296           if (REAL_VALUE_ISINF (cst)
9297               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9298             {
9299               tem = fold_inf_compare (code, type, arg0, arg1);
9300               if (tem != NULL_TREE)
9301                 return tem;
9302             }
9303         }
9304
9305       /* If this is a comparison of a real constant with a PLUS_EXPR
9306          or a MINUS_EXPR of a real constant, we can convert it into a
9307          comparison with a revised real constant as long as no overflow
9308          occurs when unsafe_math_optimizations are enabled.  */
9309       if (flag_unsafe_math_optimizations
9310           && TREE_CODE (arg1) == REAL_CST
9311           && (TREE_CODE (arg0) == PLUS_EXPR
9312               || TREE_CODE (arg0) == MINUS_EXPR)
9313           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9314           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9315                                       ? MINUS_EXPR : PLUS_EXPR,
9316                                       arg1, TREE_OPERAND (arg0, 1), 0))
9317           && !TREE_OVERFLOW (tem))
9318         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9319
9320       /* Likewise, we can simplify a comparison of a real constant with
9321          a MINUS_EXPR whose first operand is also a real constant, i.e.
9322          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9323          floating-point types only if -fassociative-math is set.  */
9324       if (flag_associative_math
9325           && TREE_CODE (arg1) == REAL_CST
9326           && TREE_CODE (arg0) == MINUS_EXPR
9327           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9328           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9329                                       arg1, 0))
9330           && !TREE_OVERFLOW (tem))
9331         return fold_build2 (swap_tree_comparison (code), type,
9332                             TREE_OPERAND (arg0, 1), tem);
9333
9334       /* Fold comparisons against built-in math functions.  */
9335       if (TREE_CODE (arg1) == REAL_CST
9336           && flag_unsafe_math_optimizations
9337           && ! flag_errno_math)
9338         {
9339           enum built_in_function fcode = builtin_mathfn_code (arg0);
9340
9341           if (fcode != END_BUILTINS)
9342             {
9343               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9344               if (tem != NULL_TREE)
9345                 return tem;
9346             }
9347         }
9348     }
9349
9350   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9351       && CONVERT_EXPR_P (arg0))
9352     {
9353       /* If we are widening one operand of an integer comparison,
9354          see if the other operand is similarly being widened.  Perhaps we
9355          can do the comparison in the narrower type.  */
9356       tem = fold_widened_comparison (code, type, arg0, arg1);
9357       if (tem)
9358         return tem;
9359
9360       /* Or if we are changing signedness.  */
9361       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9362       if (tem)
9363         return tem;
9364     }
9365
9366   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9367      constant, we can simplify it.  */
9368   if (TREE_CODE (arg1) == INTEGER_CST
9369       && (TREE_CODE (arg0) == MIN_EXPR
9370           || TREE_CODE (arg0) == MAX_EXPR)
9371       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9372     {
9373       tem = optimize_minmax_comparison (code, type, op0, op1);
9374       if (tem)
9375         return tem;
9376     }
9377
9378   /* Simplify comparison of something with itself.  (For IEEE
9379      floating-point, we can only do some of these simplifications.)  */
9380   if (operand_equal_p (arg0, arg1, 0))
9381     {
9382       switch (code)
9383         {
9384         case EQ_EXPR:
9385           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9386               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9387             return constant_boolean_node (1, type);
9388           break;
9389
9390         case GE_EXPR:
9391         case LE_EXPR:
9392           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9393               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9394             return constant_boolean_node (1, type);
9395           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9396
9397         case NE_EXPR:
9398           /* For NE, we can only do this simplification if integer
9399              or we don't honor IEEE floating point NaNs.  */
9400           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9401               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9402             break;
9403           /* ... fall through ...  */
9404         case GT_EXPR:
9405         case LT_EXPR:
9406           return constant_boolean_node (0, type);
9407         default:
9408           gcc_unreachable ();
9409         }
9410     }
9411
9412   /* If we are comparing an expression that just has comparisons
9413      of two integer values, arithmetic expressions of those comparisons,
9414      and constants, we can simplify it.  There are only three cases
9415      to check: the two values can either be equal, the first can be
9416      greater, or the second can be greater.  Fold the expression for
9417      those three values.  Since each value must be 0 or 1, we have
9418      eight possibilities, each of which corresponds to the constant 0
9419      or 1 or one of the six possible comparisons.
9420
9421      This handles common cases like (a > b) == 0 but also handles
9422      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9423      occur in macroized code.  */
9424
9425   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9426     {
9427       tree cval1 = 0, cval2 = 0;
9428       int save_p = 0;
9429
9430       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9431           /* Don't handle degenerate cases here; they should already
9432              have been handled anyway.  */
9433           && cval1 != 0 && cval2 != 0
9434           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9435           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9436           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9437           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9438           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9439           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9440                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9441         {
9442           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9443           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9444
9445           /* We can't just pass T to eval_subst in case cval1 or cval2
9446              was the same as ARG1.  */
9447
9448           tree high_result
9449                 = fold_build2 (code, type,
9450                                eval_subst (arg0, cval1, maxval,
9451                                            cval2, minval),
9452                                arg1);
9453           tree equal_result
9454                 = fold_build2 (code, type,
9455                                eval_subst (arg0, cval1, maxval,
9456                                            cval2, maxval),
9457                                arg1);
9458           tree low_result
9459                 = fold_build2 (code, type,
9460                                eval_subst (arg0, cval1, minval,
9461                                            cval2, maxval),
9462                                arg1);
9463
9464           /* All three of these results should be 0 or 1.  Confirm they are.
9465              Then use those values to select the proper code to use.  */
9466
9467           if (TREE_CODE (high_result) == INTEGER_CST
9468               && TREE_CODE (equal_result) == INTEGER_CST
9469               && TREE_CODE (low_result) == INTEGER_CST)
9470             {
9471               /* Make a 3-bit mask with the high-order bit being the
9472                  value for `>', the next for '=', and the low for '<'.  */
9473               switch ((integer_onep (high_result) * 4)
9474                       + (integer_onep (equal_result) * 2)
9475                       + integer_onep (low_result))
9476                 {
9477                 case 0:
9478                   /* Always false.  */
9479                   return omit_one_operand (type, integer_zero_node, arg0);
9480                 case 1:
9481                   code = LT_EXPR;
9482                   break;
9483                 case 2:
9484                   code = EQ_EXPR;
9485                   break;
9486                 case 3:
9487                   code = LE_EXPR;
9488                   break;
9489                 case 4:
9490                   code = GT_EXPR;
9491                   break;
9492                 case 5:
9493                   code = NE_EXPR;
9494                   break;
9495                 case 6:
9496                   code = GE_EXPR;
9497                   break;
9498                 case 7:
9499                   /* Always true.  */
9500                   return omit_one_operand (type, integer_one_node, arg0);
9501                 }
9502
9503               if (save_p)
9504                 return save_expr (build2 (code, type, cval1, cval2));
9505               return fold_build2 (code, type, cval1, cval2);
9506             }
9507         }
9508     }
9509
9510   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9511      into a single range test.  */
9512   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9513        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9514       && TREE_CODE (arg1) == INTEGER_CST
9515       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9516       && !integer_zerop (TREE_OPERAND (arg0, 1))
9517       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9518       && !TREE_OVERFLOW (arg1))
9519     {
9520       tem = fold_div_compare (code, type, arg0, arg1);
9521       if (tem != NULL_TREE)
9522         return tem;
9523     }
9524
9525   /* Fold ~X op ~Y as Y op X.  */
9526   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9527       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9528     {
9529       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9530       return fold_build2 (code, type,
9531                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9532                           TREE_OPERAND (arg0, 0));
9533     }
9534
9535   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9536   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9537       && TREE_CODE (arg1) == INTEGER_CST)
9538     {
9539       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9540       return fold_build2 (swap_tree_comparison (code), type,
9541                           TREE_OPERAND (arg0, 0),
9542                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9543                                        fold_convert (cmp_type, arg1)));
9544     }
9545
9546   return NULL_TREE;
9547 }
9548
9549
9550 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9551    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9552    argument EXPR represents the expression "z" of type TYPE.  */
9553
9554 static tree
9555 fold_mult_zconjz (tree type, tree expr)
9556 {
9557   tree itype = TREE_TYPE (type);
9558   tree rpart, ipart, tem;
9559
9560   if (TREE_CODE (expr) == COMPLEX_EXPR)
9561     {
9562       rpart = TREE_OPERAND (expr, 0);
9563       ipart = TREE_OPERAND (expr, 1);
9564     }
9565   else if (TREE_CODE (expr) == COMPLEX_CST)
9566     {
9567       rpart = TREE_REALPART (expr);
9568       ipart = TREE_IMAGPART (expr);
9569     }
9570   else
9571     {
9572       expr = save_expr (expr);
9573       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9574       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9575     }
9576
9577   rpart = save_expr (rpart);
9578   ipart = save_expr (ipart);
9579   tem = fold_build2 (PLUS_EXPR, itype,
9580                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9581                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9582   return fold_build2 (COMPLEX_EXPR, type, tem,
9583                       fold_convert (itype, integer_zero_node));
9584 }
9585
9586
9587 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9588    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9589    guarantees that P and N have the same least significant log2(M) bits.
9590    N is not otherwise constrained.  In particular, N is not normalized to
9591    0 <= N < M as is common.  In general, the precise value of P is unknown.
9592    M is chosen as large as possible such that constant N can be determined.
9593
9594    Returns M and sets *RESIDUE to N.
9595
9596    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9597    account.  This is not always possible due to PR 35705.
9598  */
9599
9600 static unsigned HOST_WIDE_INT
9601 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9602                                  bool allow_func_align)
9603 {
9604   enum tree_code code;
9605
9606   *residue = 0;
9607
9608   code = TREE_CODE (expr);
9609   if (code == ADDR_EXPR)
9610     {
9611       expr = TREE_OPERAND (expr, 0);
9612       if (handled_component_p (expr))
9613         {
9614           HOST_WIDE_INT bitsize, bitpos;
9615           tree offset;
9616           enum machine_mode mode;
9617           int unsignedp, volatilep;
9618
9619           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9620                                       &mode, &unsignedp, &volatilep, false);
9621           *residue = bitpos / BITS_PER_UNIT;
9622           if (offset)
9623             {
9624               if (TREE_CODE (offset) == INTEGER_CST)
9625                 *residue += TREE_INT_CST_LOW (offset);
9626               else
9627                 /* We don't handle more complicated offset expressions.  */
9628                 return 1;
9629             }
9630         }
9631
9632       if (DECL_P (expr)
9633           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9634         return DECL_ALIGN_UNIT (expr);
9635     }
9636   else if (code == POINTER_PLUS_EXPR)
9637     {
9638       tree op0, op1;
9639       unsigned HOST_WIDE_INT modulus;
9640       enum tree_code inner_code;
9641       
9642       op0 = TREE_OPERAND (expr, 0);
9643       STRIP_NOPS (op0);
9644       modulus = get_pointer_modulus_and_residue (op0, residue,
9645                                                  allow_func_align);
9646
9647       op1 = TREE_OPERAND (expr, 1);
9648       STRIP_NOPS (op1);
9649       inner_code = TREE_CODE (op1);
9650       if (inner_code == INTEGER_CST)
9651         {
9652           *residue += TREE_INT_CST_LOW (op1);
9653           return modulus;
9654         }
9655       else if (inner_code == MULT_EXPR)
9656         {
9657           op1 = TREE_OPERAND (op1, 1);
9658           if (TREE_CODE (op1) == INTEGER_CST)
9659             {
9660               unsigned HOST_WIDE_INT align;
9661               
9662               /* Compute the greatest power-of-2 divisor of op1.  */
9663               align = TREE_INT_CST_LOW (op1);
9664               align &= -align;
9665
9666               /* If align is non-zero and less than *modulus, replace
9667                  *modulus with align., If align is 0, then either op1 is 0
9668                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9669                  unsigned HOST_WIDE_INT.  In either case, no additional
9670                  constraint is imposed.  */
9671               if (align)
9672                 modulus = MIN (modulus, align);
9673
9674               return modulus;
9675             }
9676         }
9677     }
9678
9679     /* If we get here, we were unable to determine anything useful about the
9680        expression.  */
9681     return 1;
9682 }
9683
9684
9685 /* Fold a binary expression of code CODE and type TYPE with operands
9686    OP0 and OP1.  Return the folded expression if folding is
9687    successful.  Otherwise, return NULL_TREE.  */
9688
9689 tree
9690 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9691 {
9692   enum tree_code_class kind = TREE_CODE_CLASS (code);
9693   tree arg0, arg1, tem;
9694   tree t1 = NULL_TREE;
9695   bool strict_overflow_p;
9696
9697   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9698               && TREE_CODE_LENGTH (code) == 2
9699               && op0 != NULL_TREE
9700               && op1 != NULL_TREE);
9701
9702   arg0 = op0;
9703   arg1 = op1;
9704
9705   /* Strip any conversions that don't change the mode.  This is
9706      safe for every expression, except for a comparison expression
9707      because its signedness is derived from its operands.  So, in
9708      the latter case, only strip conversions that don't change the
9709      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9710      preserved.
9711
9712      Note that this is done as an internal manipulation within the
9713      constant folder, in order to find the simplest representation
9714      of the arguments so that their form can be studied.  In any
9715      cases, the appropriate type conversions should be put back in
9716      the tree that will get out of the constant folder.  */
9717
9718   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9719     {
9720       STRIP_SIGN_NOPS (arg0);
9721       STRIP_SIGN_NOPS (arg1);
9722     }
9723   else
9724     {
9725       STRIP_NOPS (arg0);
9726       STRIP_NOPS (arg1);
9727     }
9728
9729   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9730      constant but we can't do arithmetic on them.  */
9731   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9732       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9733       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9734       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9735       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9736       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9737     {
9738       if (kind == tcc_binary)
9739         {
9740           /* Make sure type and arg0 have the same saturating flag.  */
9741           gcc_assert (TYPE_SATURATING (type)
9742                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9743           tem = const_binop (code, arg0, arg1, 0);
9744         }
9745       else if (kind == tcc_comparison)
9746         tem = fold_relational_const (code, type, arg0, arg1);
9747       else
9748         tem = NULL_TREE;
9749
9750       if (tem != NULL_TREE)
9751         {
9752           if (TREE_TYPE (tem) != type)
9753             tem = fold_convert (type, tem);
9754           return tem;
9755         }
9756     }
9757
9758   /* If this is a commutative operation, and ARG0 is a constant, move it
9759      to ARG1 to reduce the number of tests below.  */
9760   if (commutative_tree_code (code)
9761       && tree_swap_operands_p (arg0, arg1, true))
9762     return fold_build2 (code, type, op1, op0);
9763
9764   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9765
9766      First check for cases where an arithmetic operation is applied to a
9767      compound, conditional, or comparison operation.  Push the arithmetic
9768      operation inside the compound or conditional to see if any folding
9769      can then be done.  Convert comparison to conditional for this purpose.
9770      The also optimizes non-constant cases that used to be done in
9771      expand_expr.
9772
9773      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9774      one of the operands is a comparison and the other is a comparison, a
9775      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9776      code below would make the expression more complex.  Change it to a
9777      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9778      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9779
9780   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9781        || code == EQ_EXPR || code == NE_EXPR)
9782       && ((truth_value_p (TREE_CODE (arg0))
9783            && (truth_value_p (TREE_CODE (arg1))
9784                || (TREE_CODE (arg1) == BIT_AND_EXPR
9785                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9786           || (truth_value_p (TREE_CODE (arg1))
9787               && (truth_value_p (TREE_CODE (arg0))
9788                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9789                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9790     {
9791       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9792                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9793                          : TRUTH_XOR_EXPR,
9794                          boolean_type_node,
9795                          fold_convert (boolean_type_node, arg0),
9796                          fold_convert (boolean_type_node, arg1));
9797
9798       if (code == EQ_EXPR)
9799         tem = invert_truthvalue (tem);
9800
9801       return fold_convert (type, tem);
9802     }
9803
9804   if (TREE_CODE_CLASS (code) == tcc_binary
9805       || TREE_CODE_CLASS (code) == tcc_comparison)
9806     {
9807       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9808         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9809                        fold_build2 (code, type,
9810                                     fold_convert (TREE_TYPE (op0),
9811                                                   TREE_OPERAND (arg0, 1)),
9812                                     op1));
9813       if (TREE_CODE (arg1) == COMPOUND_EXPR
9814           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9815         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9816                        fold_build2 (code, type, op0,
9817                                     fold_convert (TREE_TYPE (op1),
9818                                                   TREE_OPERAND (arg1, 1))));
9819
9820       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9821         {
9822           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9823                                                      arg0, arg1, 
9824                                                      /*cond_first_p=*/1);
9825           if (tem != NULL_TREE)
9826             return tem;
9827         }
9828
9829       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9830         {
9831           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9832                                                      arg1, arg0, 
9833                                                      /*cond_first_p=*/0);
9834           if (tem != NULL_TREE)
9835             return tem;
9836         }
9837     }
9838
9839   switch (code)
9840     {
9841     case POINTER_PLUS_EXPR:
9842       /* 0 +p index -> (type)index */
9843       if (integer_zerop (arg0))
9844         return non_lvalue (fold_convert (type, arg1));
9845
9846       /* PTR +p 0 -> PTR */
9847       if (integer_zerop (arg1))
9848         return non_lvalue (fold_convert (type, arg0));
9849
9850       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9851       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9852            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9853         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9854                                                 fold_convert (sizetype, arg1),
9855                                                 fold_convert (sizetype, arg0)));
9856
9857       /* index +p PTR -> PTR +p index */
9858       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9859           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9860         return fold_build2 (POINTER_PLUS_EXPR, type,
9861                             fold_convert (type, arg1),
9862                             fold_convert (sizetype, arg0));
9863
9864       /* (PTR +p B) +p A -> PTR +p (B + A) */
9865       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9866         {
9867           tree inner;
9868           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9869           tree arg00 = TREE_OPERAND (arg0, 0);
9870           inner = fold_build2 (PLUS_EXPR, sizetype,
9871                                arg01, fold_convert (sizetype, arg1));
9872           return fold_convert (type,
9873                                fold_build2 (POINTER_PLUS_EXPR,
9874                                             TREE_TYPE (arg00), arg00, inner));
9875         }
9876
9877       /* PTR_CST +p CST -> CST1 */
9878       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9879         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9880
9881      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9882         of the array.  Loop optimizer sometimes produce this type of
9883         expressions.  */
9884       if (TREE_CODE (arg0) == ADDR_EXPR)
9885         {
9886           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9887           if (tem)
9888             return fold_convert (type, tem);
9889         }
9890
9891       return NULL_TREE;
9892
9893     case PLUS_EXPR:
9894       /* A + (-B) -> A - B */
9895       if (TREE_CODE (arg1) == NEGATE_EXPR)
9896         return fold_build2 (MINUS_EXPR, type,
9897                             fold_convert (type, arg0),
9898                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9899       /* (-A) + B -> B - A */
9900       if (TREE_CODE (arg0) == NEGATE_EXPR
9901           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9902         return fold_build2 (MINUS_EXPR, type,
9903                             fold_convert (type, arg1),
9904                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9905
9906       if (INTEGRAL_TYPE_P (type))
9907         {
9908           /* Convert ~A + 1 to -A.  */
9909           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9910               && integer_onep (arg1))
9911             return fold_build1 (NEGATE_EXPR, type,
9912                                 fold_convert (type, TREE_OPERAND (arg0, 0)));
9913
9914           /* ~X + X is -1.  */
9915           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9916               && !TYPE_OVERFLOW_TRAPS (type))
9917             {
9918               tree tem = TREE_OPERAND (arg0, 0);
9919
9920               STRIP_NOPS (tem);
9921               if (operand_equal_p (tem, arg1, 0))
9922                 {
9923                   t1 = build_int_cst_type (type, -1);
9924                   return omit_one_operand (type, t1, arg1);
9925                 }
9926             }
9927
9928           /* X + ~X is -1.  */
9929           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9930               && !TYPE_OVERFLOW_TRAPS (type))
9931             {
9932               tree tem = TREE_OPERAND (arg1, 0);
9933
9934               STRIP_NOPS (tem);
9935               if (operand_equal_p (arg0, tem, 0))
9936                 {
9937                   t1 = build_int_cst_type (type, -1);
9938                   return omit_one_operand (type, t1, arg0);
9939                 }
9940             }
9941
9942           /* X + (X / CST) * -CST is X % CST.  */
9943           if (TREE_CODE (arg1) == MULT_EXPR
9944               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9945               && operand_equal_p (arg0,
9946                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9947             {
9948               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9949               tree cst1 = TREE_OPERAND (arg1, 1);
9950               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9951               if (sum && integer_zerop (sum))
9952                 return fold_convert (type,
9953                                      fold_build2 (TRUNC_MOD_EXPR,
9954                                                   TREE_TYPE (arg0), arg0, cst0));
9955             }
9956         }
9957
9958       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9959          same or one.  Make sure type is not saturating.
9960          fold_plusminus_mult_expr will re-associate.  */
9961       if ((TREE_CODE (arg0) == MULT_EXPR
9962            || TREE_CODE (arg1) == MULT_EXPR)
9963           && !TYPE_SATURATING (type)
9964           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9965         {
9966           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9967           if (tem)
9968             return tem;
9969         }
9970
9971       if (! FLOAT_TYPE_P (type))
9972         {
9973           if (integer_zerop (arg1))
9974             return non_lvalue (fold_convert (type, arg0));
9975
9976           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9977              with a constant, and the two constants have no bits in common,
9978              we should treat this as a BIT_IOR_EXPR since this may produce more
9979              simplifications.  */
9980           if (TREE_CODE (arg0) == BIT_AND_EXPR
9981               && TREE_CODE (arg1) == BIT_AND_EXPR
9982               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9983               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9984               && integer_zerop (const_binop (BIT_AND_EXPR,
9985                                              TREE_OPERAND (arg0, 1),
9986                                              TREE_OPERAND (arg1, 1), 0)))
9987             {
9988               code = BIT_IOR_EXPR;
9989               goto bit_ior;
9990             }
9991
9992           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9993              (plus (plus (mult) (mult)) (foo)) so that we can
9994              take advantage of the factoring cases below.  */
9995           if (((TREE_CODE (arg0) == PLUS_EXPR
9996                 || TREE_CODE (arg0) == MINUS_EXPR)
9997                && TREE_CODE (arg1) == MULT_EXPR)
9998               || ((TREE_CODE (arg1) == PLUS_EXPR
9999                    || TREE_CODE (arg1) == MINUS_EXPR)
10000                   && TREE_CODE (arg0) == MULT_EXPR))
10001             {
10002               tree parg0, parg1, parg, marg;
10003               enum tree_code pcode;
10004
10005               if (TREE_CODE (arg1) == MULT_EXPR)
10006                 parg = arg0, marg = arg1;
10007               else
10008                 parg = arg1, marg = arg0;
10009               pcode = TREE_CODE (parg);
10010               parg0 = TREE_OPERAND (parg, 0);
10011               parg1 = TREE_OPERAND (parg, 1);
10012               STRIP_NOPS (parg0);
10013               STRIP_NOPS (parg1);
10014
10015               if (TREE_CODE (parg0) == MULT_EXPR
10016                   && TREE_CODE (parg1) != MULT_EXPR)
10017                 return fold_build2 (pcode, type,
10018                                     fold_build2 (PLUS_EXPR, type,
10019                                                  fold_convert (type, parg0),
10020                                                  fold_convert (type, marg)),
10021                                     fold_convert (type, parg1));
10022               if (TREE_CODE (parg0) != MULT_EXPR
10023                   && TREE_CODE (parg1) == MULT_EXPR)
10024                 return fold_build2 (PLUS_EXPR, type,
10025                                     fold_convert (type, parg0),
10026                                     fold_build2 (pcode, type,
10027                                                  fold_convert (type, marg),
10028                                                  fold_convert (type,
10029                                                                parg1)));
10030             }
10031         }
10032       else
10033         {
10034           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
10035           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10036             return non_lvalue (fold_convert (type, arg0));
10037
10038           /* Likewise if the operands are reversed.  */
10039           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10040             return non_lvalue (fold_convert (type, arg1));
10041
10042           /* Convert X + -C into X - C.  */
10043           if (TREE_CODE (arg1) == REAL_CST
10044               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10045             {
10046               tem = fold_negate_const (arg1, type);
10047               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10048                 return fold_build2 (MINUS_EXPR, type,
10049                                     fold_convert (type, arg0),
10050                                     fold_convert (type, tem));
10051             }
10052
10053           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10054              to __complex__ ( x, y ).  This is not the same for SNaNs or
10055              if signed zeros are involved.  */
10056           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10057               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10058               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10059             {
10060               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10061               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10062               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10063               bool arg0rz = false, arg0iz = false;
10064               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10065                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10066                 {
10067                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10068                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10069                   if (arg0rz && arg1i && real_zerop (arg1i))
10070                     {
10071                       tree rp = arg1r ? arg1r
10072                                   : build1 (REALPART_EXPR, rtype, arg1);
10073                       tree ip = arg0i ? arg0i
10074                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10075                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10076                     }
10077                   else if (arg0iz && arg1r && real_zerop (arg1r))
10078                     {
10079                       tree rp = arg0r ? arg0r
10080                                   : build1 (REALPART_EXPR, rtype, arg0);
10081                       tree ip = arg1i ? arg1i
10082                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10083                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10084                     }
10085                 }
10086             }
10087
10088           if (flag_unsafe_math_optimizations
10089               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10090               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10091               && (tem = distribute_real_division (code, type, arg0, arg1)))
10092             return tem;
10093
10094           /* Convert x+x into x*2.0.  */
10095           if (operand_equal_p (arg0, arg1, 0)
10096               && SCALAR_FLOAT_TYPE_P (type))
10097             return fold_build2 (MULT_EXPR, type, arg0,
10098                                 build_real (type, dconst2));
10099
10100           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10101              We associate floats only if the user has specified
10102              -fassociative-math.  */
10103           if (flag_associative_math
10104               && TREE_CODE (arg1) == PLUS_EXPR
10105               && TREE_CODE (arg0) != MULT_EXPR)
10106             {
10107               tree tree10 = TREE_OPERAND (arg1, 0);
10108               tree tree11 = TREE_OPERAND (arg1, 1);
10109               if (TREE_CODE (tree11) == MULT_EXPR
10110                   && TREE_CODE (tree10) == MULT_EXPR)
10111                 {
10112                   tree tree0;
10113                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10114                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10115                 }
10116             }
10117           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10118              We associate floats only if the user has specified
10119              -fassociative-math.  */
10120           if (flag_associative_math
10121               && TREE_CODE (arg0) == PLUS_EXPR
10122               && TREE_CODE (arg1) != MULT_EXPR)
10123             {
10124               tree tree00 = TREE_OPERAND (arg0, 0);
10125               tree tree01 = TREE_OPERAND (arg0, 1);
10126               if (TREE_CODE (tree01) == MULT_EXPR
10127                   && TREE_CODE (tree00) == MULT_EXPR)
10128                 {
10129                   tree tree0;
10130                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10131                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10132                 }
10133             }
10134         }
10135
10136      bit_rotate:
10137       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10138          is a rotate of A by C1 bits.  */
10139       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10140          is a rotate of A by B bits.  */
10141       {
10142         enum tree_code code0, code1;
10143         tree rtype;
10144         code0 = TREE_CODE (arg0);
10145         code1 = TREE_CODE (arg1);
10146         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10147              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10148             && operand_equal_p (TREE_OPERAND (arg0, 0),
10149                                 TREE_OPERAND (arg1, 0), 0)
10150             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10151                 TYPE_UNSIGNED (rtype))
10152             /* Only create rotates in complete modes.  Other cases are not
10153                expanded properly.  */
10154             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10155           {
10156             tree tree01, tree11;
10157             enum tree_code code01, code11;
10158
10159             tree01 = TREE_OPERAND (arg0, 1);
10160             tree11 = TREE_OPERAND (arg1, 1);
10161             STRIP_NOPS (tree01);
10162             STRIP_NOPS (tree11);
10163             code01 = TREE_CODE (tree01);
10164             code11 = TREE_CODE (tree11);
10165             if (code01 == INTEGER_CST
10166                 && code11 == INTEGER_CST
10167                 && TREE_INT_CST_HIGH (tree01) == 0
10168                 && TREE_INT_CST_HIGH (tree11) == 0
10169                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10170                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10171               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10172                              code0 == LSHIFT_EXPR ? tree01 : tree11);
10173             else if (code11 == MINUS_EXPR)
10174               {
10175                 tree tree110, tree111;
10176                 tree110 = TREE_OPERAND (tree11, 0);
10177                 tree111 = TREE_OPERAND (tree11, 1);
10178                 STRIP_NOPS (tree110);
10179                 STRIP_NOPS (tree111);
10180                 if (TREE_CODE (tree110) == INTEGER_CST
10181                     && 0 == compare_tree_int (tree110,
10182                                               TYPE_PRECISION
10183                                               (TREE_TYPE (TREE_OPERAND
10184                                                           (arg0, 0))))
10185                     && operand_equal_p (tree01, tree111, 0))
10186                   return build2 ((code0 == LSHIFT_EXPR
10187                                   ? LROTATE_EXPR
10188                                   : RROTATE_EXPR),
10189                                  type, TREE_OPERAND (arg0, 0), tree01);
10190               }
10191             else if (code01 == MINUS_EXPR)
10192               {
10193                 tree tree010, tree011;
10194                 tree010 = TREE_OPERAND (tree01, 0);
10195                 tree011 = TREE_OPERAND (tree01, 1);
10196                 STRIP_NOPS (tree010);
10197                 STRIP_NOPS (tree011);
10198                 if (TREE_CODE (tree010) == INTEGER_CST
10199                     && 0 == compare_tree_int (tree010,
10200                                               TYPE_PRECISION
10201                                               (TREE_TYPE (TREE_OPERAND
10202                                                           (arg0, 0))))
10203                     && operand_equal_p (tree11, tree011, 0))
10204                   return build2 ((code0 != LSHIFT_EXPR
10205                                   ? LROTATE_EXPR
10206                                   : RROTATE_EXPR),
10207                                  type, TREE_OPERAND (arg0, 0), tree11);
10208               }
10209           }
10210       }
10211
10212     associate:
10213       /* In most languages, can't associate operations on floats through
10214          parentheses.  Rather than remember where the parentheses were, we
10215          don't associate floats at all, unless the user has specified
10216          -fassociative-math.
10217          And, we need to make sure type is not saturating.  */
10218
10219       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10220           && !TYPE_SATURATING (type))
10221         {
10222           tree var0, con0, lit0, minus_lit0;
10223           tree var1, con1, lit1, minus_lit1;
10224           bool ok = true;
10225
10226           /* Split both trees into variables, constants, and literals.  Then
10227              associate each group together, the constants with literals,
10228              then the result with variables.  This increases the chances of
10229              literals being recombined later and of generating relocatable
10230              expressions for the sum of a constant and literal.  */
10231           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10232           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10233                              code == MINUS_EXPR);
10234
10235           /* With undefined overflow we can only associate constants
10236              with one variable.  */
10237           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10238                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10239               && var0 && var1)
10240             {
10241               tree tmp0 = var0;
10242               tree tmp1 = var1;
10243
10244               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10245                 tmp0 = TREE_OPERAND (tmp0, 0);
10246               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10247                 tmp1 = TREE_OPERAND (tmp1, 0);
10248               /* The only case we can still associate with two variables
10249                  is if they are the same, modulo negation.  */
10250               if (!operand_equal_p (tmp0, tmp1, 0))
10251                 ok = false;
10252             }
10253
10254           /* Only do something if we found more than two objects.  Otherwise,
10255              nothing has changed and we risk infinite recursion.  */
10256           if (ok
10257               && (2 < ((var0 != 0) + (var1 != 0)
10258                        + (con0 != 0) + (con1 != 0)
10259                        + (lit0 != 0) + (lit1 != 0)
10260                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10261             {
10262               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10263               if (code == MINUS_EXPR)
10264                 code = PLUS_EXPR;
10265
10266               var0 = associate_trees (var0, var1, code, type);
10267               con0 = associate_trees (con0, con1, code, type);
10268               lit0 = associate_trees (lit0, lit1, code, type);
10269               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10270
10271               /* Preserve the MINUS_EXPR if the negative part of the literal is
10272                  greater than the positive part.  Otherwise, the multiplicative
10273                  folding code (i.e extract_muldiv) may be fooled in case
10274                  unsigned constants are subtracted, like in the following
10275                  example: ((X*2 + 4) - 8U)/2.  */
10276               if (minus_lit0 && lit0)
10277                 {
10278                   if (TREE_CODE (lit0) == INTEGER_CST
10279                       && TREE_CODE (minus_lit0) == INTEGER_CST
10280                       && tree_int_cst_lt (lit0, minus_lit0))
10281                     {
10282                       minus_lit0 = associate_trees (minus_lit0, lit0,
10283                                                     MINUS_EXPR, type);
10284                       lit0 = 0;
10285                     }
10286                   else
10287                     {
10288                       lit0 = associate_trees (lit0, minus_lit0,
10289                                               MINUS_EXPR, type);
10290                       minus_lit0 = 0;
10291                     }
10292                 }
10293               if (minus_lit0)
10294                 {
10295                   if (con0 == 0)
10296                     return fold_convert (type,
10297                                          associate_trees (var0, minus_lit0,
10298                                                           MINUS_EXPR, type));
10299                   else
10300                     {
10301                       con0 = associate_trees (con0, minus_lit0,
10302                                               MINUS_EXPR, type);
10303                       return fold_convert (type,
10304                                            associate_trees (var0, con0,
10305                                                             PLUS_EXPR, type));
10306                     }
10307                 }
10308
10309               con0 = associate_trees (con0, lit0, code, type);
10310               return fold_convert (type, associate_trees (var0, con0,
10311                                                           code, type));
10312             }
10313         }
10314
10315       return NULL_TREE;
10316
10317     case MINUS_EXPR:
10318       /* Pointer simplifications for subtraction, simple reassociations. */
10319       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10320         {
10321           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10322           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10323               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10324             {
10325               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10326               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10327               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10328               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10329               return fold_build2 (PLUS_EXPR, type,
10330                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10331                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10332             }
10333           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10334           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10335             {
10336               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10337               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10338               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10339               if (tmp)
10340                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10341             }
10342         }
10343       /* A - (-B) -> A + B */
10344       if (TREE_CODE (arg1) == NEGATE_EXPR)
10345         return fold_build2 (PLUS_EXPR, type, op0,
10346                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10347       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10348       if (TREE_CODE (arg0) == NEGATE_EXPR
10349           && (FLOAT_TYPE_P (type)
10350               || INTEGRAL_TYPE_P (type))
10351           && negate_expr_p (arg1)
10352           && reorder_operands_p (arg0, arg1))
10353         return fold_build2 (MINUS_EXPR, type,
10354                             fold_convert (type, negate_expr (arg1)),
10355                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10356       /* Convert -A - 1 to ~A.  */
10357       if (INTEGRAL_TYPE_P (type)
10358           && TREE_CODE (arg0) == NEGATE_EXPR
10359           && integer_onep (arg1)
10360           && !TYPE_OVERFLOW_TRAPS (type))
10361         return fold_build1 (BIT_NOT_EXPR, type,
10362                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10363
10364       /* Convert -1 - A to ~A.  */
10365       if (INTEGRAL_TYPE_P (type)
10366           && integer_all_onesp (arg0))
10367         return fold_build1 (BIT_NOT_EXPR, type, op1);
10368
10369
10370       /* X - (X / CST) * CST is X % CST.  */
10371       if (INTEGRAL_TYPE_P (type)
10372           && TREE_CODE (arg1) == MULT_EXPR
10373           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10374           && operand_equal_p (arg0,
10375                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10376           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10377                               TREE_OPERAND (arg1, 1), 0))
10378         return fold_convert (type,
10379                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10380                                           arg0, TREE_OPERAND (arg1, 1)));
10381
10382       if (! FLOAT_TYPE_P (type))
10383         {
10384           if (integer_zerop (arg0))
10385             return negate_expr (fold_convert (type, arg1));
10386           if (integer_zerop (arg1))
10387             return non_lvalue (fold_convert (type, arg0));
10388
10389           /* Fold A - (A & B) into ~B & A.  */
10390           if (!TREE_SIDE_EFFECTS (arg0)
10391               && TREE_CODE (arg1) == BIT_AND_EXPR)
10392             {
10393               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10394                 {
10395                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10396                   return fold_build2 (BIT_AND_EXPR, type,
10397                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10398                                       fold_convert (type, arg0));
10399                 }
10400               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10401                 {
10402                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10403                   return fold_build2 (BIT_AND_EXPR, type,
10404                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10405                                       fold_convert (type, arg0));
10406                 }
10407             }
10408
10409           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10410              any power of 2 minus 1.  */
10411           if (TREE_CODE (arg0) == BIT_AND_EXPR
10412               && TREE_CODE (arg1) == BIT_AND_EXPR
10413               && operand_equal_p (TREE_OPERAND (arg0, 0),
10414                                   TREE_OPERAND (arg1, 0), 0))
10415             {
10416               tree mask0 = TREE_OPERAND (arg0, 1);
10417               tree mask1 = TREE_OPERAND (arg1, 1);
10418               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10419
10420               if (operand_equal_p (tem, mask1, 0))
10421                 {
10422                   tem = fold_build2 (BIT_XOR_EXPR, type,
10423                                      TREE_OPERAND (arg0, 0), mask1);
10424                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10425                 }
10426             }
10427         }
10428
10429       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10430       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10431         return non_lvalue (fold_convert (type, arg0));
10432
10433       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10434          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10435          (-ARG1 + ARG0) reduces to -ARG1.  */
10436       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10437         return negate_expr (fold_convert (type, arg1));
10438
10439       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10440          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10441          signed zeros are involved.  */
10442       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10443           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10444           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10445         {
10446           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10447           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10448           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10449           bool arg0rz = false, arg0iz = false;
10450           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10451               || (arg0i && (arg0iz = real_zerop (arg0i))))
10452             {
10453               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10454               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10455               if (arg0rz && arg1i && real_zerop (arg1i))
10456                 {
10457                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10458                                          arg1r ? arg1r
10459                                          : build1 (REALPART_EXPR, rtype, arg1));
10460                   tree ip = arg0i ? arg0i
10461                     : build1 (IMAGPART_EXPR, rtype, arg0);
10462                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10463                 }
10464               else if (arg0iz && arg1r && real_zerop (arg1r))
10465                 {
10466                   tree rp = arg0r ? arg0r
10467                     : build1 (REALPART_EXPR, rtype, arg0);
10468                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10469                                          arg1i ? arg1i
10470                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10471                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10472                 }
10473             }
10474         }
10475
10476       /* Fold &x - &x.  This can happen from &x.foo - &x.
10477          This is unsafe for certain floats even in non-IEEE formats.
10478          In IEEE, it is unsafe because it does wrong for NaNs.
10479          Also note that operand_equal_p is always false if an operand
10480          is volatile.  */
10481
10482       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10483           && operand_equal_p (arg0, arg1, 0))
10484         return fold_convert (type, integer_zero_node);
10485
10486       /* A - B -> A + (-B) if B is easily negatable.  */
10487       if (negate_expr_p (arg1)
10488           && ((FLOAT_TYPE_P (type)
10489                /* Avoid this transformation if B is a positive REAL_CST.  */
10490                && (TREE_CODE (arg1) != REAL_CST
10491                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10492               || INTEGRAL_TYPE_P (type)))
10493         return fold_build2 (PLUS_EXPR, type,
10494                             fold_convert (type, arg0),
10495                             fold_convert (type, negate_expr (arg1)));
10496
10497       /* Try folding difference of addresses.  */
10498       {
10499         HOST_WIDE_INT diff;
10500
10501         if ((TREE_CODE (arg0) == ADDR_EXPR
10502              || TREE_CODE (arg1) == ADDR_EXPR)
10503             && ptr_difference_const (arg0, arg1, &diff))
10504           return build_int_cst_type (type, diff);
10505       }
10506
10507       /* Fold &a[i] - &a[j] to i-j.  */
10508       if (TREE_CODE (arg0) == ADDR_EXPR
10509           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10510           && TREE_CODE (arg1) == ADDR_EXPR
10511           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10512         {
10513           tree aref0 = TREE_OPERAND (arg0, 0);
10514           tree aref1 = TREE_OPERAND (arg1, 0);
10515           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10516                                TREE_OPERAND (aref1, 0), 0))
10517             {
10518               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10519               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10520               tree esz = array_ref_element_size (aref0);
10521               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10522               return fold_build2 (MULT_EXPR, type, diff,
10523                                   fold_convert (type, esz));
10524                                   
10525             }
10526         }
10527
10528       if (FLOAT_TYPE_P (type)
10529           && flag_unsafe_math_optimizations
10530           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10531           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10532           && (tem = distribute_real_division (code, type, arg0, arg1)))
10533         return tem;
10534
10535       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10536          same or one.  Make sure type is not saturating.
10537          fold_plusminus_mult_expr will re-associate.  */
10538       if ((TREE_CODE (arg0) == MULT_EXPR
10539            || TREE_CODE (arg1) == MULT_EXPR)
10540           && !TYPE_SATURATING (type)
10541           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10542         {
10543           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10544           if (tem)
10545             return tem;
10546         }
10547
10548       goto associate;
10549
10550     case MULT_EXPR:
10551       /* (-A) * (-B) -> A * B  */
10552       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10553         return fold_build2 (MULT_EXPR, type,
10554                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10555                             fold_convert (type, negate_expr (arg1)));
10556       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10557         return fold_build2 (MULT_EXPR, type,
10558                             fold_convert (type, negate_expr (arg0)),
10559                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10560
10561       if (! FLOAT_TYPE_P (type))
10562         {
10563           if (integer_zerop (arg1))
10564             return omit_one_operand (type, arg1, arg0);
10565           if (integer_onep (arg1))
10566             return non_lvalue (fold_convert (type, arg0));
10567           /* Transform x * -1 into -x.  Make sure to do the negation
10568              on the original operand with conversions not stripped
10569              because we can only strip non-sign-changing conversions.  */
10570           if (integer_all_onesp (arg1))
10571             return fold_convert (type, negate_expr (op0));
10572           /* Transform x * -C into -x * C if x is easily negatable.  */
10573           if (TREE_CODE (arg1) == INTEGER_CST
10574               && tree_int_cst_sgn (arg1) == -1
10575               && negate_expr_p (arg0)
10576               && (tem = negate_expr (arg1)) != arg1
10577               && !TREE_OVERFLOW (tem))
10578             return fold_build2 (MULT_EXPR, type,
10579                                 fold_convert (type, negate_expr (arg0)), tem);
10580
10581           /* (a * (1 << b)) is (a << b)  */
10582           if (TREE_CODE (arg1) == LSHIFT_EXPR
10583               && integer_onep (TREE_OPERAND (arg1, 0)))
10584             return fold_build2 (LSHIFT_EXPR, type, op0,
10585                                 TREE_OPERAND (arg1, 1));
10586           if (TREE_CODE (arg0) == LSHIFT_EXPR
10587               && integer_onep (TREE_OPERAND (arg0, 0)))
10588             return fold_build2 (LSHIFT_EXPR, type, op1,
10589                                 TREE_OPERAND (arg0, 1));
10590
10591           /* (A + A) * C -> A * 2 * C  */
10592           if (TREE_CODE (arg0) == PLUS_EXPR
10593               && TREE_CODE (arg1) == INTEGER_CST
10594               && operand_equal_p (TREE_OPERAND (arg0, 0),
10595                                   TREE_OPERAND (arg0, 1), 0))
10596             return fold_build2 (MULT_EXPR, type,
10597                                 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10598                                                   TREE_OPERAND (arg0, 1)),
10599                                 fold_build2 (MULT_EXPR, type,
10600                                              build_int_cst (type, 2) , arg1));
10601
10602           strict_overflow_p = false;
10603           if (TREE_CODE (arg1) == INTEGER_CST
10604               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10605                                              &strict_overflow_p)))
10606             {
10607               if (strict_overflow_p)
10608                 fold_overflow_warning (("assuming signed overflow does not "
10609                                         "occur when simplifying "
10610                                         "multiplication"),
10611                                        WARN_STRICT_OVERFLOW_MISC);
10612               return fold_convert (type, tem);
10613             }
10614
10615           /* Optimize z * conj(z) for integer complex numbers.  */
10616           if (TREE_CODE (arg0) == CONJ_EXPR
10617               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10618             return fold_mult_zconjz (type, arg1);
10619           if (TREE_CODE (arg1) == CONJ_EXPR
10620               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10621             return fold_mult_zconjz (type, arg0);
10622         }
10623       else
10624         {
10625           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10626              when x is NaN, since x * 0 is also NaN.  Nor are they the
10627              same in modes with signed zeros, since multiplying a
10628              negative value by 0 gives -0, not +0.  */
10629           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10630               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10631               && real_zerop (arg1))
10632             return omit_one_operand (type, arg1, arg0);
10633           /* In IEEE floating point, x*1 is not equivalent to x for snans.
10634              Likewise for complex arithmetic with signed zeros.  */
10635           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10636               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10637                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10638               && real_onep (arg1))
10639             return non_lvalue (fold_convert (type, arg0));
10640
10641           /* Transform x * -1.0 into -x.  */
10642           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10643               && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10644                   || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10645               && real_minus_onep (arg1))
10646             return fold_convert (type, negate_expr (arg0));
10647
10648           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10649              the result for floating point types due to rounding so it is applied
10650              only if -fassociative-math was specify.  */
10651           if (flag_associative_math
10652               && TREE_CODE (arg0) == RDIV_EXPR
10653               && TREE_CODE (arg1) == REAL_CST
10654               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10655             {
10656               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10657                                       arg1, 0);
10658               if (tem)
10659                 return fold_build2 (RDIV_EXPR, type, tem,
10660                                     TREE_OPERAND (arg0, 1));
10661             }
10662
10663           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10664           if (operand_equal_p (arg0, arg1, 0))
10665             {
10666               tree tem = fold_strip_sign_ops (arg0);
10667               if (tem != NULL_TREE)
10668                 {
10669                   tem = fold_convert (type, tem);
10670                   return fold_build2 (MULT_EXPR, type, tem, tem);
10671                 }
10672             }
10673
10674           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10675              This is not the same for NaNs or if signed zeros are
10676              involved.  */
10677           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10678               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10679               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10680               && TREE_CODE (arg1) == COMPLEX_CST
10681               && real_zerop (TREE_REALPART (arg1)))
10682             {
10683               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10684               if (real_onep (TREE_IMAGPART (arg1)))
10685                 return fold_build2 (COMPLEX_EXPR, type,
10686                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10687                                                               rtype, arg0)),
10688                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10689               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10690                 return fold_build2 (COMPLEX_EXPR, type,
10691                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10692                                     negate_expr (fold_build1 (REALPART_EXPR,
10693                                                               rtype, arg0)));
10694             }
10695
10696           /* Optimize z * conj(z) for floating point complex numbers.
10697              Guarded by flag_unsafe_math_optimizations as non-finite
10698              imaginary components don't produce scalar results.  */
10699           if (flag_unsafe_math_optimizations
10700               && TREE_CODE (arg0) == CONJ_EXPR
10701               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10702             return fold_mult_zconjz (type, arg1);
10703           if (flag_unsafe_math_optimizations
10704               && TREE_CODE (arg1) == CONJ_EXPR
10705               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10706             return fold_mult_zconjz (type, arg0);
10707
10708           if (flag_unsafe_math_optimizations)
10709             {
10710               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10711               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10712
10713               /* Optimizations of root(...)*root(...).  */
10714               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10715                 {
10716                   tree rootfn, arg;
10717                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10718                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10719
10720                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10721                   if (BUILTIN_SQRT_P (fcode0)
10722                       && operand_equal_p (arg00, arg10, 0)
10723                       && ! HONOR_SNANS (TYPE_MODE (type)))
10724                     return arg00;
10725
10726                   /* Optimize root(x)*root(y) as root(x*y).  */
10727                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10728                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10729                   return build_call_expr (rootfn, 1, arg);
10730                 }
10731
10732               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10733               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10734                 {
10735                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10736                   tree arg = fold_build2 (PLUS_EXPR, type,
10737                                           CALL_EXPR_ARG (arg0, 0),
10738                                           CALL_EXPR_ARG (arg1, 0));
10739                   return build_call_expr (expfn, 1, arg);
10740                 }
10741
10742               /* Optimizations of pow(...)*pow(...).  */
10743               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10744                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10745                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10746                 {
10747                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10748                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10749                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10750                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10751
10752                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10753                   if (operand_equal_p (arg01, arg11, 0))
10754                     {
10755                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10756                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10757                       return build_call_expr (powfn, 2, arg, arg01);
10758                     }
10759
10760                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10761                   if (operand_equal_p (arg00, arg10, 0))
10762                     {
10763                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10764                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10765                       return build_call_expr (powfn, 2, arg00, arg);
10766                     }
10767                 }
10768
10769               /* Optimize tan(x)*cos(x) as sin(x).  */
10770               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10771                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10772                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10773                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10774                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10775                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10776                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10777                                       CALL_EXPR_ARG (arg1, 0), 0))
10778                 {
10779                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10780
10781                   if (sinfn != NULL_TREE)
10782                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10783                 }
10784
10785               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10786               if (fcode1 == BUILT_IN_POW
10787                   || fcode1 == BUILT_IN_POWF
10788                   || fcode1 == BUILT_IN_POWL)
10789                 {
10790                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10791                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10792                   if (TREE_CODE (arg11) == REAL_CST
10793                       && !TREE_OVERFLOW (arg11)
10794                       && operand_equal_p (arg0, arg10, 0))
10795                     {
10796                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10797                       REAL_VALUE_TYPE c;
10798                       tree arg;
10799
10800                       c = TREE_REAL_CST (arg11);
10801                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10802                       arg = build_real (type, c);
10803                       return build_call_expr (powfn, 2, arg0, arg);
10804                     }
10805                 }
10806
10807               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10808               if (fcode0 == BUILT_IN_POW
10809                   || fcode0 == BUILT_IN_POWF
10810                   || fcode0 == BUILT_IN_POWL)
10811                 {
10812                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10813                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10814                   if (TREE_CODE (arg01) == REAL_CST
10815                       && !TREE_OVERFLOW (arg01)
10816                       && operand_equal_p (arg1, arg00, 0))
10817                     {
10818                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10819                       REAL_VALUE_TYPE c;
10820                       tree arg;
10821
10822                       c = TREE_REAL_CST (arg01);
10823                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10824                       arg = build_real (type, c);
10825                       return build_call_expr (powfn, 2, arg1, arg);
10826                     }
10827                 }
10828
10829               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10830               if (optimize_function_for_speed_p (cfun)
10831                   && operand_equal_p (arg0, arg1, 0))
10832                 {
10833                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10834
10835                   if (powfn)
10836                     {
10837                       tree arg = build_real (type, dconst2);
10838                       return build_call_expr (powfn, 2, arg0, arg);
10839                     }
10840                 }
10841             }
10842         }
10843       goto associate;
10844
10845     case BIT_IOR_EXPR:
10846     bit_ior:
10847       if (integer_all_onesp (arg1))
10848         return omit_one_operand (type, arg1, arg0);
10849       if (integer_zerop (arg1))
10850         return non_lvalue (fold_convert (type, arg0));
10851       if (operand_equal_p (arg0, arg1, 0))
10852         return non_lvalue (fold_convert (type, arg0));
10853
10854       /* ~X | X is -1.  */
10855       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10856           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10857         {
10858           t1 = fold_convert (type, integer_zero_node);
10859           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10860           return omit_one_operand (type, t1, arg1);
10861         }
10862
10863       /* X | ~X is -1.  */
10864       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10865           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10866         {
10867           t1 = fold_convert (type, integer_zero_node);
10868           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10869           return omit_one_operand (type, t1, arg0);
10870         }
10871
10872       /* Canonicalize (X & C1) | C2.  */
10873       if (TREE_CODE (arg0) == BIT_AND_EXPR
10874           && TREE_CODE (arg1) == INTEGER_CST
10875           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10876         {
10877           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10878           int width = TYPE_PRECISION (type), w;
10879           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10880           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10881           hi2 = TREE_INT_CST_HIGH (arg1);
10882           lo2 = TREE_INT_CST_LOW (arg1);
10883
10884           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10885           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10886             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10887
10888           if (width > HOST_BITS_PER_WIDE_INT)
10889             {
10890               mhi = (unsigned HOST_WIDE_INT) -1 
10891                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10892               mlo = -1;
10893             }
10894           else
10895             {
10896               mhi = 0;
10897               mlo = (unsigned HOST_WIDE_INT) -1
10898                     >> (HOST_BITS_PER_WIDE_INT - width);
10899             }
10900
10901           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10902           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10903             return fold_build2 (BIT_IOR_EXPR, type,
10904                                 TREE_OPERAND (arg0, 0), arg1);
10905
10906           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10907              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10908              mode which allows further optimizations.  */
10909           hi1 &= mhi;
10910           lo1 &= mlo;
10911           hi2 &= mhi;
10912           lo2 &= mlo;
10913           hi3 = hi1 & ~hi2;
10914           lo3 = lo1 & ~lo2;
10915           for (w = BITS_PER_UNIT;
10916                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10917                w <<= 1)
10918             {
10919               unsigned HOST_WIDE_INT mask
10920                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10921               if (((lo1 | lo2) & mask) == mask
10922                   && (lo1 & ~mask) == 0 && hi1 == 0)
10923                 {
10924                   hi3 = 0;
10925                   lo3 = mask;
10926                   break;
10927                 }
10928             }
10929           if (hi3 != hi1 || lo3 != lo1)
10930             return fold_build2 (BIT_IOR_EXPR, type,
10931                                 fold_build2 (BIT_AND_EXPR, type,
10932                                              TREE_OPERAND (arg0, 0),
10933                                              build_int_cst_wide (type,
10934                                                                  lo3, hi3)),
10935                                 arg1);
10936         }
10937
10938       /* (X & Y) | Y is (X, Y).  */
10939       if (TREE_CODE (arg0) == BIT_AND_EXPR
10940           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10941         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10942       /* (X & Y) | X is (Y, X).  */
10943       if (TREE_CODE (arg0) == BIT_AND_EXPR
10944           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10945           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10946         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10947       /* X | (X & Y) is (Y, X).  */
10948       if (TREE_CODE (arg1) == BIT_AND_EXPR
10949           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10950           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10951         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10952       /* X | (Y & X) is (Y, X).  */
10953       if (TREE_CODE (arg1) == BIT_AND_EXPR
10954           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10955           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10956         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10957
10958       t1 = distribute_bit_expr (code, type, arg0, arg1);
10959       if (t1 != NULL_TREE)
10960         return t1;
10961
10962       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10963
10964          This results in more efficient code for machines without a NAND
10965          instruction.  Combine will canonicalize to the first form
10966          which will allow use of NAND instructions provided by the
10967          backend if they exist.  */
10968       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10969           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10970         {
10971           return fold_build1 (BIT_NOT_EXPR, type,
10972                               build2 (BIT_AND_EXPR, type,
10973                                       fold_convert (type,
10974                                                     TREE_OPERAND (arg0, 0)),
10975                                       fold_convert (type,
10976                                                     TREE_OPERAND (arg1, 0))));
10977         }
10978
10979       /* See if this can be simplified into a rotate first.  If that
10980          is unsuccessful continue in the association code.  */
10981       goto bit_rotate;
10982
10983     case BIT_XOR_EXPR:
10984       if (integer_zerop (arg1))
10985         return non_lvalue (fold_convert (type, arg0));
10986       if (integer_all_onesp (arg1))
10987         return fold_build1 (BIT_NOT_EXPR, type, op0);
10988       if (operand_equal_p (arg0, arg1, 0))
10989         return omit_one_operand (type, integer_zero_node, arg0);
10990
10991       /* ~X ^ X is -1.  */
10992       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10993           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10994         {
10995           t1 = fold_convert (type, integer_zero_node);
10996           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10997           return omit_one_operand (type, t1, arg1);
10998         }
10999
11000       /* X ^ ~X is -1.  */
11001       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11002           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11003         {
11004           t1 = fold_convert (type, integer_zero_node);
11005           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
11006           return omit_one_operand (type, t1, arg0);
11007         }
11008
11009       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11010          with a constant, and the two constants have no bits in common,
11011          we should treat this as a BIT_IOR_EXPR since this may produce more
11012          simplifications.  */
11013       if (TREE_CODE (arg0) == BIT_AND_EXPR
11014           && TREE_CODE (arg1) == BIT_AND_EXPR
11015           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11016           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11017           && integer_zerop (const_binop (BIT_AND_EXPR,
11018                                          TREE_OPERAND (arg0, 1),
11019                                          TREE_OPERAND (arg1, 1), 0)))
11020         {
11021           code = BIT_IOR_EXPR;
11022           goto bit_ior;
11023         }
11024
11025       /* (X | Y) ^ X -> Y & ~ X*/
11026       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11027           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11028         {
11029           tree t2 = TREE_OPERAND (arg0, 1);
11030           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11031                             arg1);
11032           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11033                             fold_convert (type, t1));
11034           return t1;
11035         }
11036
11037       /* (Y | X) ^ X -> Y & ~ X*/
11038       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11039           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11040         {
11041           tree t2 = TREE_OPERAND (arg0, 0);
11042           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11043                             arg1);
11044           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11045                             fold_convert (type, t1));
11046           return t1;
11047         }
11048
11049       /* X ^ (X | Y) -> Y & ~ X*/
11050       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11051           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11052         {
11053           tree t2 = TREE_OPERAND (arg1, 1);
11054           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11055                             arg0);
11056           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11057                             fold_convert (type, t1));
11058           return t1;
11059         }
11060
11061       /* X ^ (Y | X) -> Y & ~ X*/
11062       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11063           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11064         {
11065           tree t2 = TREE_OPERAND (arg1, 0);
11066           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11067                             arg0);
11068           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11069                             fold_convert (type, t1));
11070           return t1;
11071         }
11072         
11073       /* Convert ~X ^ ~Y to X ^ Y.  */
11074       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11075           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11076         return fold_build2 (code, type,
11077                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11078                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11079
11080       /* Convert ~X ^ C to X ^ ~C.  */
11081       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11082           && TREE_CODE (arg1) == INTEGER_CST)
11083         return fold_build2 (code, type,
11084                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11085                             fold_build1 (BIT_NOT_EXPR, type, arg1));
11086
11087       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11088       if (TREE_CODE (arg0) == BIT_AND_EXPR
11089           && integer_onep (TREE_OPERAND (arg0, 1))
11090           && integer_onep (arg1))
11091         return fold_build2 (EQ_EXPR, type, arg0,
11092                             build_int_cst (TREE_TYPE (arg0), 0));
11093
11094       /* Fold (X & Y) ^ Y as ~X & Y.  */
11095       if (TREE_CODE (arg0) == BIT_AND_EXPR
11096           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11097         {
11098           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11099           return fold_build2 (BIT_AND_EXPR, type, 
11100                               fold_build1 (BIT_NOT_EXPR, type, tem),
11101                               fold_convert (type, arg1));
11102         }
11103       /* Fold (X & Y) ^ X as ~Y & X.  */
11104       if (TREE_CODE (arg0) == BIT_AND_EXPR
11105           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11106           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11107         {
11108           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11109           return fold_build2 (BIT_AND_EXPR, type,
11110                               fold_build1 (BIT_NOT_EXPR, type, tem),
11111                               fold_convert (type, arg1));
11112         }
11113       /* Fold X ^ (X & Y) as X & ~Y.  */
11114       if (TREE_CODE (arg1) == BIT_AND_EXPR
11115           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11116         {
11117           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11118           return fold_build2 (BIT_AND_EXPR, type,
11119                               fold_convert (type, arg0),
11120                               fold_build1 (BIT_NOT_EXPR, type, tem));
11121         }
11122       /* Fold X ^ (Y & X) as ~Y & X.  */
11123       if (TREE_CODE (arg1) == BIT_AND_EXPR
11124           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11125           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11126         {
11127           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11128           return fold_build2 (BIT_AND_EXPR, type,
11129                               fold_build1 (BIT_NOT_EXPR, type, tem),
11130                               fold_convert (type, arg0));
11131         }
11132
11133       /* See if this can be simplified into a rotate first.  If that
11134          is unsuccessful continue in the association code.  */
11135       goto bit_rotate;
11136
11137     case BIT_AND_EXPR:
11138       if (integer_all_onesp (arg1))
11139         return non_lvalue (fold_convert (type, arg0));
11140       if (integer_zerop (arg1))
11141         return omit_one_operand (type, arg1, arg0);
11142       if (operand_equal_p (arg0, arg1, 0))
11143         return non_lvalue (fold_convert (type, arg0));
11144
11145       /* ~X & X is always zero.  */
11146       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11147           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11148         return omit_one_operand (type, integer_zero_node, arg1);
11149
11150       /* X & ~X is always zero.  */
11151       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11152           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11153         return omit_one_operand (type, integer_zero_node, arg0);
11154
11155       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11156       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11157           && TREE_CODE (arg1) == INTEGER_CST
11158           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11159         {
11160           tree tmp1 = fold_convert (type, arg1);
11161           tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11162           tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11163           tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11164           tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11165           return fold_convert (type,
11166                                fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11167         }
11168
11169       /* (X | Y) & Y is (X, Y).  */
11170       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11171           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11172         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11173       /* (X | Y) & X is (Y, X).  */
11174       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11175           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11176           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11177         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11178       /* X & (X | Y) is (Y, X).  */
11179       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11180           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11181           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11182         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11183       /* X & (Y | X) is (Y, X).  */
11184       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11185           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11186           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11187         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11188
11189       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11190       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11191           && integer_onep (TREE_OPERAND (arg0, 1))
11192           && integer_onep (arg1))
11193         {
11194           tem = TREE_OPERAND (arg0, 0);
11195           return fold_build2 (EQ_EXPR, type,
11196                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11197                                            build_int_cst (TREE_TYPE (tem), 1)),
11198                               build_int_cst (TREE_TYPE (tem), 0));
11199         }
11200       /* Fold ~X & 1 as (X & 1) == 0.  */
11201       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11202           && integer_onep (arg1))
11203         {
11204           tem = TREE_OPERAND (arg0, 0);
11205           return fold_build2 (EQ_EXPR, type,
11206                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11207                                            build_int_cst (TREE_TYPE (tem), 1)),
11208                               build_int_cst (TREE_TYPE (tem), 0));
11209         }
11210
11211       /* Fold (X ^ Y) & Y as ~X & Y.  */
11212       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11213           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11214         {
11215           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11216           return fold_build2 (BIT_AND_EXPR, type, 
11217                               fold_build1 (BIT_NOT_EXPR, type, tem),
11218                               fold_convert (type, arg1));
11219         }
11220       /* Fold (X ^ Y) & X as ~Y & X.  */
11221       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11222           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11223           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11224         {
11225           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11226           return fold_build2 (BIT_AND_EXPR, type,
11227                               fold_build1 (BIT_NOT_EXPR, type, tem),
11228                               fold_convert (type, arg1));
11229         }
11230       /* Fold X & (X ^ Y) as X & ~Y.  */
11231       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11232           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11233         {
11234           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11235           return fold_build2 (BIT_AND_EXPR, type,
11236                               fold_convert (type, arg0),
11237                               fold_build1 (BIT_NOT_EXPR, type, tem));
11238         }
11239       /* Fold X & (Y ^ X) as ~Y & X.  */
11240       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11241           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11242           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11243         {
11244           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11245           return fold_build2 (BIT_AND_EXPR, type,
11246                               fold_build1 (BIT_NOT_EXPR, type, tem),
11247                               fold_convert (type, arg0));
11248         }
11249
11250       t1 = distribute_bit_expr (code, type, arg0, arg1);
11251       if (t1 != NULL_TREE)
11252         return t1;
11253       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11254       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11255           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11256         {
11257           unsigned int prec
11258             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11259
11260           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11261               && (~TREE_INT_CST_LOW (arg1)
11262                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11263             return fold_convert (type, TREE_OPERAND (arg0, 0));
11264         }
11265
11266       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11267
11268          This results in more efficient code for machines without a NOR
11269          instruction.  Combine will canonicalize to the first form
11270          which will allow use of NOR instructions provided by the
11271          backend if they exist.  */
11272       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11273           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11274         {
11275           return fold_build1 (BIT_NOT_EXPR, type,
11276                               build2 (BIT_IOR_EXPR, type,
11277                                       fold_convert (type,
11278                                                     TREE_OPERAND (arg0, 0)),
11279                                       fold_convert (type,
11280                                                     TREE_OPERAND (arg1, 0))));
11281         }
11282
11283       /* If arg0 is derived from the address of an object or function, we may
11284          be able to fold this expression using the object or function's
11285          alignment.  */
11286       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11287         {
11288           unsigned HOST_WIDE_INT modulus, residue;
11289           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11290
11291           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11292                                                      integer_onep (arg1));
11293
11294           /* This works because modulus is a power of 2.  If this weren't the
11295              case, we'd have to replace it by its greatest power-of-2
11296              divisor: modulus & -modulus.  */
11297           if (low < modulus)
11298             return build_int_cst (type, residue & low);
11299         }
11300
11301       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11302               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11303          if the new mask might be further optimized.  */
11304       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11305            || TREE_CODE (arg0) == RSHIFT_EXPR)
11306           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11307           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11308           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11309              < TYPE_PRECISION (TREE_TYPE (arg0))
11310           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11311           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11312         {
11313           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11314           unsigned HOST_WIDE_INT mask
11315             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11316           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11317           tree shift_type = TREE_TYPE (arg0);
11318
11319           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11320             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11321           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11322                    && TYPE_PRECISION (TREE_TYPE (arg0))
11323                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11324             {
11325               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11326               tree arg00 = TREE_OPERAND (arg0, 0);
11327               /* See if more bits can be proven as zero because of
11328                  zero extension.  */
11329               if (TREE_CODE (arg00) == NOP_EXPR
11330                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11331                 {
11332                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11333                   if (TYPE_PRECISION (inner_type)
11334                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11335                       && TYPE_PRECISION (inner_type) < prec)
11336                     {
11337                       prec = TYPE_PRECISION (inner_type);
11338                       /* See if we can shorten the right shift.  */
11339                       if (shiftc < prec)
11340                         shift_type = inner_type;
11341                     }
11342                 }
11343               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11344               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11345               zerobits <<= prec - shiftc;
11346               /* For arithmetic shift if sign bit could be set, zerobits
11347                  can contain actually sign bits, so no transformation is
11348                  possible, unless MASK masks them all away.  In that
11349                  case the shift needs to be converted into logical shift.  */
11350               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11351                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11352                 {
11353                   if ((mask & zerobits) == 0)
11354                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11355                   else
11356                     zerobits = 0;
11357                 }
11358             }
11359
11360           /* ((X << 16) & 0xff00) is (X, 0).  */
11361           if ((mask & zerobits) == mask)
11362             return omit_one_operand (type, build_int_cst (type, 0), arg0);
11363
11364           newmask = mask | zerobits;
11365           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11366             {
11367               unsigned int prec;
11368
11369               /* Only do the transformation if NEWMASK is some integer
11370                  mode's mask.  */
11371               for (prec = BITS_PER_UNIT;
11372                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11373                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11374                   break;
11375               if (prec < HOST_BITS_PER_WIDE_INT
11376                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11377                 {
11378                   if (shift_type != TREE_TYPE (arg0))
11379                     {
11380                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
11381                                          fold_convert (shift_type,
11382                                                        TREE_OPERAND (arg0, 0)),
11383                                          TREE_OPERAND (arg0, 1));
11384                       tem = fold_convert (type, tem);
11385                     }
11386                   else
11387                     tem = op0;
11388                   return fold_build2 (BIT_AND_EXPR, type, tem,
11389                                       build_int_cst_type (TREE_TYPE (op1),
11390                                                           newmask));
11391                 }
11392             }
11393         }
11394
11395       goto associate;
11396
11397     case RDIV_EXPR:
11398       /* Don't touch a floating-point divide by zero unless the mode
11399          of the constant can represent infinity.  */
11400       if (TREE_CODE (arg1) == REAL_CST
11401           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11402           && real_zerop (arg1))
11403         return NULL_TREE;
11404
11405       /* Optimize A / A to 1.0 if we don't care about
11406          NaNs or Infinities.  Skip the transformation
11407          for non-real operands.  */
11408       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11409           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11410           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11411           && operand_equal_p (arg0, arg1, 0))
11412         {
11413           tree r = build_real (TREE_TYPE (arg0), dconst1);
11414
11415           return omit_two_operands (type, r, arg0, arg1);
11416         }
11417
11418       /* The complex version of the above A / A optimization.  */
11419       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11420           && operand_equal_p (arg0, arg1, 0))
11421         {
11422           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11423           if (! HONOR_NANS (TYPE_MODE (elem_type))
11424               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11425             {
11426               tree r = build_real (elem_type, dconst1);
11427               /* omit_two_operands will call fold_convert for us.  */
11428               return omit_two_operands (type, r, arg0, arg1);
11429             }
11430         }
11431
11432       /* (-A) / (-B) -> A / B  */
11433       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11434         return fold_build2 (RDIV_EXPR, type,
11435                             TREE_OPERAND (arg0, 0),
11436                             negate_expr (arg1));
11437       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11438         return fold_build2 (RDIV_EXPR, type,
11439                             negate_expr (arg0),
11440                             TREE_OPERAND (arg1, 0));
11441
11442       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11443       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11444           && real_onep (arg1))
11445         return non_lvalue (fold_convert (type, arg0));
11446
11447       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11448       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11449           && real_minus_onep (arg1))
11450         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11451
11452       /* If ARG1 is a constant, we can convert this to a multiply by the
11453          reciprocal.  This does not have the same rounding properties,
11454          so only do this if -freciprocal-math.  We can actually
11455          always safely do it if ARG1 is a power of two, but it's hard to
11456          tell if it is or not in a portable manner.  */
11457       if (TREE_CODE (arg1) == REAL_CST)
11458         {
11459           if (flag_reciprocal_math
11460               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11461                                           arg1, 0)))
11462             return fold_build2 (MULT_EXPR, type, arg0, tem);
11463           /* Find the reciprocal if optimizing and the result is exact.  */
11464           if (optimize)
11465             {
11466               REAL_VALUE_TYPE r;
11467               r = TREE_REAL_CST (arg1);
11468               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11469                 {
11470                   tem = build_real (type, r);
11471                   return fold_build2 (MULT_EXPR, type,
11472                                       fold_convert (type, arg0), tem);
11473                 }
11474             }
11475         }
11476       /* Convert A/B/C to A/(B*C).  */ 
11477       if (flag_reciprocal_math
11478           && TREE_CODE (arg0) == RDIV_EXPR)
11479         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11480                             fold_build2 (MULT_EXPR, type,
11481                                          TREE_OPERAND (arg0, 1), arg1));
11482
11483       /* Convert A/(B/C) to (A/B)*C.  */
11484       if (flag_reciprocal_math
11485           && TREE_CODE (arg1) == RDIV_EXPR)
11486         return fold_build2 (MULT_EXPR, type,
11487                             fold_build2 (RDIV_EXPR, type, arg0,
11488                                          TREE_OPERAND (arg1, 0)),
11489                             TREE_OPERAND (arg1, 1));
11490
11491       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11492       if (flag_reciprocal_math
11493           && TREE_CODE (arg1) == MULT_EXPR
11494           && TREE_CODE (arg0) == REAL_CST
11495           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11496         {
11497           tree tem = const_binop (RDIV_EXPR, arg0,
11498                                   TREE_OPERAND (arg1, 1), 0);
11499           if (tem)
11500             return fold_build2 (RDIV_EXPR, type, tem,
11501                                 TREE_OPERAND (arg1, 0));
11502         }
11503
11504       if (flag_unsafe_math_optimizations)
11505         {
11506           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11507           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11508
11509           /* Optimize sin(x)/cos(x) as tan(x).  */
11510           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11511                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11512                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11513               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11514                                   CALL_EXPR_ARG (arg1, 0), 0))
11515             {
11516               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11517
11518               if (tanfn != NULL_TREE)
11519                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11520             }
11521
11522           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11523           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11524                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11525                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11526               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11527                                   CALL_EXPR_ARG (arg1, 0), 0))
11528             {
11529               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11530
11531               if (tanfn != NULL_TREE)
11532                 {
11533                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11534                   return fold_build2 (RDIV_EXPR, type,
11535                                       build_real (type, dconst1), tmp);
11536                 }
11537             }
11538
11539           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11540              NaNs or Infinities.  */
11541           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11542                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11543                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11544             {
11545               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11546               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11547
11548               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11549                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11550                   && operand_equal_p (arg00, arg01, 0))
11551                 {
11552                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11553
11554                   if (cosfn != NULL_TREE)
11555                     return build_call_expr (cosfn, 1, arg00);
11556                 }
11557             }
11558
11559           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11560              NaNs or Infinities.  */
11561           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11562                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11563                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11564             {
11565               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11566               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11567
11568               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11569                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11570                   && operand_equal_p (arg00, arg01, 0))
11571                 {
11572                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11573
11574                   if (cosfn != NULL_TREE)
11575                     {
11576                       tree tmp = build_call_expr (cosfn, 1, arg00);
11577                       return fold_build2 (RDIV_EXPR, type,
11578                                           build_real (type, dconst1),
11579                                           tmp);
11580                     }
11581                 }
11582             }
11583
11584           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11585           if (fcode0 == BUILT_IN_POW
11586               || fcode0 == BUILT_IN_POWF
11587               || fcode0 == BUILT_IN_POWL)
11588             {
11589               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11590               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11591               if (TREE_CODE (arg01) == REAL_CST
11592                   && !TREE_OVERFLOW (arg01)
11593                   && operand_equal_p (arg1, arg00, 0))
11594                 {
11595                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11596                   REAL_VALUE_TYPE c;
11597                   tree arg;
11598
11599                   c = TREE_REAL_CST (arg01);
11600                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11601                   arg = build_real (type, c);
11602                   return build_call_expr (powfn, 2, arg1, arg);
11603                 }
11604             }
11605
11606           /* Optimize a/root(b/c) into a*root(c/b).  */
11607           if (BUILTIN_ROOT_P (fcode1))
11608             {
11609               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11610
11611               if (TREE_CODE (rootarg) == RDIV_EXPR)
11612                 {
11613                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11614                   tree b = TREE_OPERAND (rootarg, 0);
11615                   tree c = TREE_OPERAND (rootarg, 1);
11616
11617                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11618
11619                   tmp = build_call_expr (rootfn, 1, tmp);
11620                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11621                 }
11622             }
11623
11624           /* Optimize x/expN(y) into x*expN(-y).  */
11625           if (BUILTIN_EXPONENT_P (fcode1))
11626             {
11627               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11628               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11629               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11630               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11631             }
11632
11633           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11634           if (fcode1 == BUILT_IN_POW
11635               || fcode1 == BUILT_IN_POWF
11636               || fcode1 == BUILT_IN_POWL)
11637             {
11638               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11639               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11640               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11641               tree neg11 = fold_convert (type, negate_expr (arg11));
11642               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11643               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11644             }
11645         }
11646       return NULL_TREE;
11647
11648     case TRUNC_DIV_EXPR:
11649     case FLOOR_DIV_EXPR:
11650       /* Simplify A / (B << N) where A and B are positive and B is
11651          a power of 2, to A >> (N + log2(B)).  */
11652       strict_overflow_p = false;
11653       if (TREE_CODE (arg1) == LSHIFT_EXPR
11654           && (TYPE_UNSIGNED (type)
11655               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11656         {
11657           tree sval = TREE_OPERAND (arg1, 0);
11658           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11659             {
11660               tree sh_cnt = TREE_OPERAND (arg1, 1);
11661               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11662
11663               if (strict_overflow_p)
11664                 fold_overflow_warning (("assuming signed overflow does not "
11665                                         "occur when simplifying A / (B << N)"),
11666                                        WARN_STRICT_OVERFLOW_MISC);
11667
11668               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11669                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11670               return fold_build2 (RSHIFT_EXPR, type,
11671                                   fold_convert (type, arg0), sh_cnt);
11672             }
11673         }
11674
11675       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11676          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11677       if (INTEGRAL_TYPE_P (type)
11678           && TYPE_UNSIGNED (type)
11679           && code == FLOOR_DIV_EXPR)
11680         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11681
11682       /* Fall thru */
11683
11684     case ROUND_DIV_EXPR:
11685     case CEIL_DIV_EXPR:
11686     case EXACT_DIV_EXPR:
11687       if (integer_onep (arg1))
11688         return non_lvalue (fold_convert (type, arg0));
11689       if (integer_zerop (arg1))
11690         return NULL_TREE;
11691       /* X / -1 is -X.  */
11692       if (!TYPE_UNSIGNED (type)
11693           && TREE_CODE (arg1) == INTEGER_CST
11694           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11695           && TREE_INT_CST_HIGH (arg1) == -1)
11696         return fold_convert (type, negate_expr (arg0));
11697
11698       /* Convert -A / -B to A / B when the type is signed and overflow is
11699          undefined.  */
11700       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11701           && TREE_CODE (arg0) == NEGATE_EXPR
11702           && negate_expr_p (arg1))
11703         {
11704           if (INTEGRAL_TYPE_P (type))
11705             fold_overflow_warning (("assuming signed overflow does not occur "
11706                                     "when distributing negation across "
11707                                     "division"),
11708                                    WARN_STRICT_OVERFLOW_MISC);
11709           return fold_build2 (code, type,
11710                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11711                               fold_convert (type, negate_expr (arg1)));
11712         }
11713       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11714           && TREE_CODE (arg1) == NEGATE_EXPR
11715           && negate_expr_p (arg0))
11716         {
11717           if (INTEGRAL_TYPE_P (type))
11718             fold_overflow_warning (("assuming signed overflow does not occur "
11719                                     "when distributing negation across "
11720                                     "division"),
11721                                    WARN_STRICT_OVERFLOW_MISC);
11722           return fold_build2 (code, type,
11723                               fold_convert (type, negate_expr (arg0)),
11724                               fold_convert (type, TREE_OPERAND (arg1, 0)));
11725         }
11726
11727       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11728          operation, EXACT_DIV_EXPR.
11729
11730          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11731          At one time others generated faster code, it's not clear if they do
11732          after the last round to changes to the DIV code in expmed.c.  */
11733       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11734           && multiple_of_p (type, arg0, arg1))
11735         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11736
11737       strict_overflow_p = false;
11738       if (TREE_CODE (arg1) == INTEGER_CST
11739           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11740                                          &strict_overflow_p)))
11741         {
11742           if (strict_overflow_p)
11743             fold_overflow_warning (("assuming signed overflow does not occur "
11744                                     "when simplifying division"),
11745                                    WARN_STRICT_OVERFLOW_MISC);
11746           return fold_convert (type, tem);
11747         }
11748
11749       return NULL_TREE;
11750
11751     case CEIL_MOD_EXPR:
11752     case FLOOR_MOD_EXPR:
11753     case ROUND_MOD_EXPR:
11754     case TRUNC_MOD_EXPR:
11755       /* X % 1 is always zero, but be sure to preserve any side
11756          effects in X.  */
11757       if (integer_onep (arg1))
11758         return omit_one_operand (type, integer_zero_node, arg0);
11759
11760       /* X % 0, return X % 0 unchanged so that we can get the
11761          proper warnings and errors.  */
11762       if (integer_zerop (arg1))
11763         return NULL_TREE;
11764
11765       /* 0 % X is always zero, but be sure to preserve any side
11766          effects in X.  Place this after checking for X == 0.  */
11767       if (integer_zerop (arg0))
11768         return omit_one_operand (type, integer_zero_node, arg1);
11769
11770       /* X % -1 is zero.  */
11771       if (!TYPE_UNSIGNED (type)
11772           && TREE_CODE (arg1) == INTEGER_CST
11773           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11774           && TREE_INT_CST_HIGH (arg1) == -1)
11775         return omit_one_operand (type, integer_zero_node, arg0);
11776
11777       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11778          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11779       strict_overflow_p = false;
11780       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11781           && (TYPE_UNSIGNED (type)
11782               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11783         {
11784           tree c = arg1;
11785           /* Also optimize A % (C << N)  where C is a power of 2,
11786              to A & ((C << N) - 1).  */
11787           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11788             c = TREE_OPERAND (arg1, 0);
11789
11790           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11791             {
11792               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11793                                        build_int_cst (TREE_TYPE (arg1), 1));
11794               if (strict_overflow_p)
11795                 fold_overflow_warning (("assuming signed overflow does not "
11796                                         "occur when simplifying "
11797                                         "X % (power of two)"),
11798                                        WARN_STRICT_OVERFLOW_MISC);
11799               return fold_build2 (BIT_AND_EXPR, type,
11800                                   fold_convert (type, arg0),
11801                                   fold_convert (type, mask));
11802             }
11803         }
11804
11805       /* X % -C is the same as X % C.  */
11806       if (code == TRUNC_MOD_EXPR
11807           && !TYPE_UNSIGNED (type)
11808           && TREE_CODE (arg1) == INTEGER_CST
11809           && !TREE_OVERFLOW (arg1)
11810           && TREE_INT_CST_HIGH (arg1) < 0
11811           && !TYPE_OVERFLOW_TRAPS (type)
11812           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11813           && !sign_bit_p (arg1, arg1))
11814         return fold_build2 (code, type, fold_convert (type, arg0),
11815                             fold_convert (type, negate_expr (arg1)));
11816
11817       /* X % -Y is the same as X % Y.  */
11818       if (code == TRUNC_MOD_EXPR
11819           && !TYPE_UNSIGNED (type)
11820           && TREE_CODE (arg1) == NEGATE_EXPR
11821           && !TYPE_OVERFLOW_TRAPS (type))
11822         return fold_build2 (code, type, fold_convert (type, arg0),
11823                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11824
11825       if (TREE_CODE (arg1) == INTEGER_CST
11826           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11827                                          &strict_overflow_p)))
11828         {
11829           if (strict_overflow_p)
11830             fold_overflow_warning (("assuming signed overflow does not occur "
11831                                     "when simplifying modulus"),
11832                                    WARN_STRICT_OVERFLOW_MISC);
11833           return fold_convert (type, tem);
11834         }
11835
11836       return NULL_TREE;
11837
11838     case LROTATE_EXPR:
11839     case RROTATE_EXPR:
11840       if (integer_all_onesp (arg0))
11841         return omit_one_operand (type, arg0, arg1);
11842       goto shift;
11843
11844     case RSHIFT_EXPR:
11845       /* Optimize -1 >> x for arithmetic right shifts.  */
11846       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11847           && tree_expr_nonnegative_p (arg1))
11848         return omit_one_operand (type, arg0, arg1);
11849       /* ... fall through ...  */
11850
11851     case LSHIFT_EXPR:
11852     shift:
11853       if (integer_zerop (arg1))
11854         return non_lvalue (fold_convert (type, arg0));
11855       if (integer_zerop (arg0))
11856         return omit_one_operand (type, arg0, arg1);
11857
11858       /* Since negative shift count is not well-defined,
11859          don't try to compute it in the compiler.  */
11860       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11861         return NULL_TREE;
11862
11863       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11864       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11865           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11866           && host_integerp (TREE_OPERAND (arg0, 1), false)
11867           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11868         {
11869           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11870                                + TREE_INT_CST_LOW (arg1));
11871
11872           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11873              being well defined.  */
11874           if (low >= TYPE_PRECISION (type))
11875             {
11876               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11877                 low = low % TYPE_PRECISION (type);
11878               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11879                 return omit_one_operand (type, build_int_cst (type, 0),
11880                                          TREE_OPERAND (arg0, 0));
11881               else
11882                 low = TYPE_PRECISION (type) - 1;
11883             }
11884
11885           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11886                               build_int_cst (type, low));
11887         }
11888
11889       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11890          into x & ((unsigned)-1 >> c) for unsigned types.  */
11891       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11892            || (TYPE_UNSIGNED (type)
11893                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11894           && host_integerp (arg1, false)
11895           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11896           && host_integerp (TREE_OPERAND (arg0, 1), false)
11897           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11898         {
11899           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11900           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11901           tree lshift;
11902           tree arg00;
11903
11904           if (low0 == low1)
11905             {
11906               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11907
11908               lshift = build_int_cst (type, -1);
11909               lshift = int_const_binop (code, lshift, arg1, 0);
11910
11911               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11912             }
11913         }
11914
11915       /* Rewrite an LROTATE_EXPR by a constant into an
11916          RROTATE_EXPR by a new constant.  */
11917       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11918         {
11919           tree tem = build_int_cst (TREE_TYPE (arg1),
11920                                     TYPE_PRECISION (type));
11921           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11922           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11923         }
11924
11925       /* If we have a rotate of a bit operation with the rotate count and
11926          the second operand of the bit operation both constant,
11927          permute the two operations.  */
11928       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11929           && (TREE_CODE (arg0) == BIT_AND_EXPR
11930               || TREE_CODE (arg0) == BIT_IOR_EXPR
11931               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11932           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11933         return fold_build2 (TREE_CODE (arg0), type,
11934                             fold_build2 (code, type,
11935                                          TREE_OPERAND (arg0, 0), arg1),
11936                             fold_build2 (code, type,
11937                                          TREE_OPERAND (arg0, 1), arg1));
11938
11939       /* Two consecutive rotates adding up to the precision of the
11940          type can be ignored.  */
11941       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11942           && TREE_CODE (arg0) == RROTATE_EXPR
11943           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11944           && TREE_INT_CST_HIGH (arg1) == 0
11945           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11946           && ((TREE_INT_CST_LOW (arg1)
11947                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11948               == (unsigned int) TYPE_PRECISION (type)))
11949         return TREE_OPERAND (arg0, 0);
11950
11951       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11952               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11953          if the latter can be further optimized.  */
11954       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11955           && TREE_CODE (arg0) == BIT_AND_EXPR
11956           && TREE_CODE (arg1) == INTEGER_CST
11957           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11958         {
11959           tree mask = fold_build2 (code, type,
11960                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11961                                    arg1);
11962           tree shift = fold_build2 (code, type,
11963                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11964                                     arg1);
11965           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11966           if (tem)
11967             return tem;
11968         }
11969
11970       return NULL_TREE;
11971
11972     case MIN_EXPR:
11973       if (operand_equal_p (arg0, arg1, 0))
11974         return omit_one_operand (type, arg0, arg1);
11975       if (INTEGRAL_TYPE_P (type)
11976           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11977         return omit_one_operand (type, arg1, arg0);
11978       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11979       if (tem)
11980         return tem;
11981       goto associate;
11982
11983     case MAX_EXPR:
11984       if (operand_equal_p (arg0, arg1, 0))
11985         return omit_one_operand (type, arg0, arg1);
11986       if (INTEGRAL_TYPE_P (type)
11987           && TYPE_MAX_VALUE (type)
11988           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11989         return omit_one_operand (type, arg1, arg0);
11990       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11991       if (tem)
11992         return tem;
11993       goto associate;
11994
11995     case TRUTH_ANDIF_EXPR:
11996       /* Note that the operands of this must be ints
11997          and their values must be 0 or 1.
11998          ("true" is a fixed value perhaps depending on the language.)  */
11999       /* If first arg is constant zero, return it.  */
12000       if (integer_zerop (arg0))
12001         return fold_convert (type, arg0);
12002     case TRUTH_AND_EXPR:
12003       /* If either arg is constant true, drop it.  */
12004       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12005         return non_lvalue (fold_convert (type, arg1));
12006       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12007           /* Preserve sequence points.  */
12008           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12009         return non_lvalue (fold_convert (type, arg0));
12010       /* If second arg is constant zero, result is zero, but first arg
12011          must be evaluated.  */
12012       if (integer_zerop (arg1))
12013         return omit_one_operand (type, arg1, arg0);
12014       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12015          case will be handled here.  */
12016       if (integer_zerop (arg0))
12017         return omit_one_operand (type, arg0, arg1);
12018
12019       /* !X && X is always false.  */
12020       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12021           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12022         return omit_one_operand (type, integer_zero_node, arg1);
12023       /* X && !X is always false.  */
12024       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12025           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12026         return omit_one_operand (type, integer_zero_node, arg0);
12027
12028       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
12029          means A >= Y && A != MAX, but in this case we know that
12030          A < X <= MAX.  */
12031
12032       if (!TREE_SIDE_EFFECTS (arg0)
12033           && !TREE_SIDE_EFFECTS (arg1))
12034         {
12035           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
12036           if (tem && !operand_equal_p (tem, arg0, 0))
12037             return fold_build2 (code, type, tem, arg1);
12038
12039           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
12040           if (tem && !operand_equal_p (tem, arg1, 0))
12041             return fold_build2 (code, type, arg0, tem);
12042         }
12043
12044     truth_andor:
12045       /* We only do these simplifications if we are optimizing.  */
12046       if (!optimize)
12047         return NULL_TREE;
12048
12049       /* Check for things like (A || B) && (A || C).  We can convert this
12050          to A || (B && C).  Note that either operator can be any of the four
12051          truth and/or operations and the transformation will still be
12052          valid.   Also note that we only care about order for the
12053          ANDIF and ORIF operators.  If B contains side effects, this
12054          might change the truth-value of A.  */
12055       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12056           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12057               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12058               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12059               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12060           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12061         {
12062           tree a00 = TREE_OPERAND (arg0, 0);
12063           tree a01 = TREE_OPERAND (arg0, 1);
12064           tree a10 = TREE_OPERAND (arg1, 0);
12065           tree a11 = TREE_OPERAND (arg1, 1);
12066           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12067                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12068                              && (code == TRUTH_AND_EXPR
12069                                  || code == TRUTH_OR_EXPR));
12070
12071           if (operand_equal_p (a00, a10, 0))
12072             return fold_build2 (TREE_CODE (arg0), type, a00,
12073                                 fold_build2 (code, type, a01, a11));
12074           else if (commutative && operand_equal_p (a00, a11, 0))
12075             return fold_build2 (TREE_CODE (arg0), type, a00,
12076                                 fold_build2 (code, type, a01, a10));
12077           else if (commutative && operand_equal_p (a01, a10, 0))
12078             return fold_build2 (TREE_CODE (arg0), type, a01,
12079                                 fold_build2 (code, type, a00, a11));
12080
12081           /* This case if tricky because we must either have commutative
12082              operators or else A10 must not have side-effects.  */
12083
12084           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12085                    && operand_equal_p (a01, a11, 0))
12086             return fold_build2 (TREE_CODE (arg0), type,
12087                                 fold_build2 (code, type, a00, a10),
12088                                 a01);
12089         }
12090
12091       /* See if we can build a range comparison.  */
12092       if (0 != (tem = fold_range_test (code, type, op0, op1)))
12093         return tem;
12094
12095       /* Check for the possibility of merging component references.  If our
12096          lhs is another similar operation, try to merge its rhs with our
12097          rhs.  Then try to merge our lhs and rhs.  */
12098       if (TREE_CODE (arg0) == code
12099           && 0 != (tem = fold_truthop (code, type,
12100                                        TREE_OPERAND (arg0, 1), arg1)))
12101         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12102
12103       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12104         return tem;
12105
12106       return NULL_TREE;
12107
12108     case TRUTH_ORIF_EXPR:
12109       /* Note that the operands of this must be ints
12110          and their values must be 0 or true.
12111          ("true" is a fixed value perhaps depending on the language.)  */
12112       /* If first arg is constant true, return it.  */
12113       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12114         return fold_convert (type, arg0);
12115     case TRUTH_OR_EXPR:
12116       /* If either arg is constant zero, drop it.  */
12117       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12118         return non_lvalue (fold_convert (type, arg1));
12119       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12120           /* Preserve sequence points.  */
12121           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12122         return non_lvalue (fold_convert (type, arg0));
12123       /* If second arg is constant true, result is true, but we must
12124          evaluate first arg.  */
12125       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12126         return omit_one_operand (type, arg1, arg0);
12127       /* Likewise for first arg, but note this only occurs here for
12128          TRUTH_OR_EXPR.  */
12129       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12130         return omit_one_operand (type, arg0, arg1);
12131
12132       /* !X || X is always true.  */
12133       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12134           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12135         return omit_one_operand (type, integer_one_node, arg1);
12136       /* X || !X is always true.  */
12137       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12138           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12139         return omit_one_operand (type, integer_one_node, arg0);
12140
12141       goto truth_andor;
12142
12143     case TRUTH_XOR_EXPR:
12144       /* If the second arg is constant zero, drop it.  */
12145       if (integer_zerop (arg1))
12146         return non_lvalue (fold_convert (type, arg0));
12147       /* If the second arg is constant true, this is a logical inversion.  */
12148       if (integer_onep (arg1))
12149         {
12150           /* Only call invert_truthvalue if operand is a truth value.  */
12151           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12152             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12153           else
12154             tem = invert_truthvalue (arg0);
12155           return non_lvalue (fold_convert (type, tem));
12156         }
12157       /* Identical arguments cancel to zero.  */
12158       if (operand_equal_p (arg0, arg1, 0))
12159         return omit_one_operand (type, integer_zero_node, arg0);
12160
12161       /* !X ^ X is always true.  */
12162       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12163           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12164         return omit_one_operand (type, integer_one_node, arg1);
12165
12166       /* X ^ !X is always true.  */
12167       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12168           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12169         return omit_one_operand (type, integer_one_node, arg0);
12170
12171       return NULL_TREE;
12172
12173     case EQ_EXPR:
12174     case NE_EXPR:
12175       tem = fold_comparison (code, type, op0, op1);
12176       if (tem != NULL_TREE)
12177         return tem;
12178
12179       /* bool_var != 0 becomes bool_var. */
12180       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12181           && code == NE_EXPR)
12182         return non_lvalue (fold_convert (type, arg0));
12183
12184       /* bool_var == 1 becomes bool_var. */
12185       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12186           && code == EQ_EXPR)
12187         return non_lvalue (fold_convert (type, arg0));
12188
12189       /* bool_var != 1 becomes !bool_var. */
12190       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12191           && code == NE_EXPR)
12192         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12193
12194       /* bool_var == 0 becomes !bool_var. */
12195       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12196           && code == EQ_EXPR)
12197         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12198
12199       /* If this is an equality comparison of the address of two non-weak,
12200          unaliased symbols neither of which are extern (since we do not
12201          have access to attributes for externs), then we know the result.  */
12202       if (TREE_CODE (arg0) == ADDR_EXPR
12203           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12204           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12205           && ! lookup_attribute ("alias",
12206                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12207           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12208           && TREE_CODE (arg1) == ADDR_EXPR
12209           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12210           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12211           && ! lookup_attribute ("alias",
12212                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12213           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12214         {
12215           /* We know that we're looking at the address of two
12216              non-weak, unaliased, static _DECL nodes.
12217
12218              It is both wasteful and incorrect to call operand_equal_p
12219              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12220              all we need to do is test pointer equality for the arguments
12221              to the two ADDR_EXPR nodes.  It is incorrect to use
12222              operand_equal_p as that function is NOT equivalent to a
12223              C equality test.  It can in fact return false for two
12224              objects which would test as equal using the C equality
12225              operator.  */
12226           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12227           return constant_boolean_node (equal
12228                                         ? code == EQ_EXPR : code != EQ_EXPR,
12229                                         type);
12230         }
12231
12232       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12233          a MINUS_EXPR of a constant, we can convert it into a comparison with
12234          a revised constant as long as no overflow occurs.  */
12235       if (TREE_CODE (arg1) == INTEGER_CST
12236           && (TREE_CODE (arg0) == PLUS_EXPR
12237               || TREE_CODE (arg0) == MINUS_EXPR)
12238           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12239           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12240                                       ? MINUS_EXPR : PLUS_EXPR,
12241                                       fold_convert (TREE_TYPE (arg0), arg1),
12242                                       TREE_OPERAND (arg0, 1), 0))
12243           && !TREE_OVERFLOW (tem))
12244         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12245
12246       /* Similarly for a NEGATE_EXPR.  */
12247       if (TREE_CODE (arg0) == NEGATE_EXPR
12248           && TREE_CODE (arg1) == INTEGER_CST
12249           && 0 != (tem = negate_expr (arg1))
12250           && TREE_CODE (tem) == INTEGER_CST
12251           && !TREE_OVERFLOW (tem))
12252         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12253
12254       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12255       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12256           && TREE_CODE (arg1) == INTEGER_CST
12257           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12258         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12259                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12260                                          fold_convert (TREE_TYPE (arg0), arg1),
12261                                          TREE_OPERAND (arg0, 1)));
12262
12263       /* Transform comparisons of the form X +- Y CMP X to Y CMP 0.  */
12264       if ((TREE_CODE (arg0) == PLUS_EXPR
12265            || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12266            || TREE_CODE (arg0) == MINUS_EXPR)
12267           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12268           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12269               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12270         {
12271           tree val = TREE_OPERAND (arg0, 1);
12272           return omit_two_operands (type,
12273                                     fold_build2 (code, type,
12274                                                  val,
12275                                                  build_int_cst (TREE_TYPE (val),
12276                                                                 0)),
12277                                     TREE_OPERAND (arg0, 0), arg1);
12278         }
12279
12280       /* Transform comparisons of the form C - X CMP X if C % 2 == 1.  */
12281       if (TREE_CODE (arg0) == MINUS_EXPR
12282           && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12283           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12284           && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12285         {
12286           return omit_two_operands (type,
12287                                     code == NE_EXPR
12288                                     ? boolean_true_node : boolean_false_node,
12289                                     TREE_OPERAND (arg0, 1), arg1);
12290         }
12291
12292       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12293          for !=.  Don't do this for ordered comparisons due to overflow.  */
12294       if (TREE_CODE (arg0) == MINUS_EXPR
12295           && integer_zerop (arg1))
12296         return fold_build2 (code, type,
12297                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12298
12299       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12300       if (TREE_CODE (arg0) == ABS_EXPR
12301           && (integer_zerop (arg1) || real_zerop (arg1)))
12302         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12303
12304       /* If this is an EQ or NE comparison with zero and ARG0 is
12305          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12306          two operations, but the latter can be done in one less insn
12307          on machines that have only two-operand insns or on which a
12308          constant cannot be the first operand.  */
12309       if (TREE_CODE (arg0) == BIT_AND_EXPR
12310           && integer_zerop (arg1))
12311         {
12312           tree arg00 = TREE_OPERAND (arg0, 0);
12313           tree arg01 = TREE_OPERAND (arg0, 1);
12314           if (TREE_CODE (arg00) == LSHIFT_EXPR
12315               && integer_onep (TREE_OPERAND (arg00, 0)))
12316             {
12317               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12318                                       arg01, TREE_OPERAND (arg00, 1));
12319               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12320                                  build_int_cst (TREE_TYPE (arg0), 1));
12321               return fold_build2 (code, type,
12322                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12323             }
12324           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12325                    && integer_onep (TREE_OPERAND (arg01, 0)))
12326             {
12327               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12328                                       arg00, TREE_OPERAND (arg01, 1));
12329               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12330                                  build_int_cst (TREE_TYPE (arg0), 1));
12331               return fold_build2 (code, type,
12332                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12333             }
12334         }
12335
12336       /* If this is an NE or EQ comparison of zero against the result of a
12337          signed MOD operation whose second operand is a power of 2, make
12338          the MOD operation unsigned since it is simpler and equivalent.  */
12339       if (integer_zerop (arg1)
12340           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12341           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12342               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12343               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12344               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12345           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12346         {
12347           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12348           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12349                                      fold_convert (newtype,
12350                                                    TREE_OPERAND (arg0, 0)),
12351                                      fold_convert (newtype,
12352                                                    TREE_OPERAND (arg0, 1)));
12353
12354           return fold_build2 (code, type, newmod,
12355                               fold_convert (newtype, arg1));
12356         }
12357
12358       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12359          C1 is a valid shift constant, and C2 is a power of two, i.e.
12360          a single bit.  */
12361       if (TREE_CODE (arg0) == BIT_AND_EXPR
12362           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12363           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12364              == INTEGER_CST
12365           && integer_pow2p (TREE_OPERAND (arg0, 1))
12366           && integer_zerop (arg1))
12367         {
12368           tree itype = TREE_TYPE (arg0);
12369           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12370           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12371
12372           /* Check for a valid shift count.  */
12373           if (TREE_INT_CST_HIGH (arg001) == 0
12374               && TREE_INT_CST_LOW (arg001) < prec)
12375             {
12376               tree arg01 = TREE_OPERAND (arg0, 1);
12377               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12378               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12379               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12380                  can be rewritten as (X & (C2 << C1)) != 0.  */
12381               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12382                 {
12383                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12384                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12385                   return fold_build2 (code, type, tem, arg1);
12386                 }
12387               /* Otherwise, for signed (arithmetic) shifts,
12388                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12389                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12390               else if (!TYPE_UNSIGNED (itype))
12391                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12392                                     arg000, build_int_cst (itype, 0));
12393               /* Otherwise, of unsigned (logical) shifts,
12394                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12395                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12396               else
12397                 return omit_one_operand (type,
12398                                          code == EQ_EXPR ? integer_one_node
12399                                                          : integer_zero_node,
12400                                          arg000);
12401             }
12402         }
12403
12404       /* If this is an NE comparison of zero with an AND of one, remove the
12405          comparison since the AND will give the correct value.  */
12406       if (code == NE_EXPR
12407           && integer_zerop (arg1)
12408           && TREE_CODE (arg0) == BIT_AND_EXPR
12409           && integer_onep (TREE_OPERAND (arg0, 1)))
12410         return fold_convert (type, arg0);
12411
12412       /* If we have (A & C) == C where C is a power of 2, convert this into
12413          (A & C) != 0.  Similarly for NE_EXPR.  */
12414       if (TREE_CODE (arg0) == BIT_AND_EXPR
12415           && integer_pow2p (TREE_OPERAND (arg0, 1))
12416           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12417         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12418                             arg0, fold_convert (TREE_TYPE (arg0),
12419                                                 integer_zero_node));
12420
12421       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12422          bit, then fold the expression into A < 0 or A >= 0.  */
12423       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12424       if (tem)
12425         return tem;
12426
12427       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12428          Similarly for NE_EXPR.  */
12429       if (TREE_CODE (arg0) == BIT_AND_EXPR
12430           && TREE_CODE (arg1) == INTEGER_CST
12431           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12432         {
12433           tree notc = fold_build1 (BIT_NOT_EXPR,
12434                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12435                                    TREE_OPERAND (arg0, 1));
12436           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12437                                        arg1, notc);
12438           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12439           if (integer_nonzerop (dandnotc))
12440             return omit_one_operand (type, rslt, arg0);
12441         }
12442
12443       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12444          Similarly for NE_EXPR.  */
12445       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12446           && TREE_CODE (arg1) == INTEGER_CST
12447           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12448         {
12449           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12450           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12451                                        TREE_OPERAND (arg0, 1), notd);
12452           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12453           if (integer_nonzerop (candnotd))
12454             return omit_one_operand (type, rslt, arg0);
12455         }
12456
12457       /* If this is a comparison of a field, we may be able to simplify it.  */
12458       if ((TREE_CODE (arg0) == COMPONENT_REF
12459            || TREE_CODE (arg0) == BIT_FIELD_REF)
12460           /* Handle the constant case even without -O
12461              to make sure the warnings are given.  */
12462           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12463         {
12464           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12465           if (t1)
12466             return t1;
12467         }
12468
12469       /* Optimize comparisons of strlen vs zero to a compare of the
12470          first character of the string vs zero.  To wit,
12471                 strlen(ptr) == 0   =>  *ptr == 0
12472                 strlen(ptr) != 0   =>  *ptr != 0
12473          Other cases should reduce to one of these two (or a constant)
12474          due to the return value of strlen being unsigned.  */
12475       if (TREE_CODE (arg0) == CALL_EXPR
12476           && integer_zerop (arg1))
12477         {
12478           tree fndecl = get_callee_fndecl (arg0);
12479
12480           if (fndecl
12481               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12482               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12483               && call_expr_nargs (arg0) == 1
12484               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12485             {
12486               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12487               return fold_build2 (code, type, iref,
12488                                   build_int_cst (TREE_TYPE (iref), 0));
12489             }
12490         }
12491
12492       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12493          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12494       if (TREE_CODE (arg0) == RSHIFT_EXPR
12495           && integer_zerop (arg1)
12496           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12497         {
12498           tree arg00 = TREE_OPERAND (arg0, 0);
12499           tree arg01 = TREE_OPERAND (arg0, 1);
12500           tree itype = TREE_TYPE (arg00);
12501           if (TREE_INT_CST_HIGH (arg01) == 0
12502               && TREE_INT_CST_LOW (arg01)
12503                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12504             {
12505               if (TYPE_UNSIGNED (itype))
12506                 {
12507                   itype = signed_type_for (itype);
12508                   arg00 = fold_convert (itype, arg00);
12509                 }
12510               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12511                                   type, arg00, build_int_cst (itype, 0));
12512             }
12513         }
12514
12515       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12516       if (integer_zerop (arg1)
12517           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12518         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12519                             TREE_OPERAND (arg0, 1));
12520
12521       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12522       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12523           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12524         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12525                             build_int_cst (TREE_TYPE (arg1), 0));
12526       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12527       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12528           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12529           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12530         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12531                             build_int_cst (TREE_TYPE (arg1), 0));
12532
12533       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12534       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12535           && TREE_CODE (arg1) == INTEGER_CST
12536           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12537         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12538                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12539                                          TREE_OPERAND (arg0, 1), arg1));
12540
12541       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12542          (X & C) == 0 when C is a single bit.  */
12543       if (TREE_CODE (arg0) == BIT_AND_EXPR
12544           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12545           && integer_zerop (arg1)
12546           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12547         {
12548           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12549                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12550                              TREE_OPERAND (arg0, 1));
12551           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12552                               type, tem, arg1);
12553         }
12554
12555       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12556          constant C is a power of two, i.e. a single bit.  */
12557       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12558           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12559           && integer_zerop (arg1)
12560           && integer_pow2p (TREE_OPERAND (arg0, 1))
12561           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12562                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12563         {
12564           tree arg00 = TREE_OPERAND (arg0, 0);
12565           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12566                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12567         }
12568
12569       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12570          when is C is a power of two, i.e. a single bit.  */
12571       if (TREE_CODE (arg0) == BIT_AND_EXPR
12572           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12573           && integer_zerop (arg1)
12574           && integer_pow2p (TREE_OPERAND (arg0, 1))
12575           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12576                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12577         {
12578           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12579           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12580                              arg000, TREE_OPERAND (arg0, 1));
12581           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12582                               tem, build_int_cst (TREE_TYPE (tem), 0));
12583         }
12584
12585       if (integer_zerop (arg1)
12586           && tree_expr_nonzero_p (arg0))
12587         {
12588           tree res = constant_boolean_node (code==NE_EXPR, type);
12589           return omit_one_operand (type, res, arg0);
12590         }
12591
12592       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12593       if (TREE_CODE (arg0) == NEGATE_EXPR
12594           && TREE_CODE (arg1) == NEGATE_EXPR)
12595         return fold_build2 (code, type,
12596                             TREE_OPERAND (arg0, 0),
12597                             TREE_OPERAND (arg1, 0));
12598
12599       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12600       if (TREE_CODE (arg0) == BIT_AND_EXPR
12601           && TREE_CODE (arg1) == BIT_AND_EXPR)
12602         {
12603           tree arg00 = TREE_OPERAND (arg0, 0);
12604           tree arg01 = TREE_OPERAND (arg0, 1);
12605           tree arg10 = TREE_OPERAND (arg1, 0);
12606           tree arg11 = TREE_OPERAND (arg1, 1);
12607           tree itype = TREE_TYPE (arg0);
12608
12609           if (operand_equal_p (arg01, arg11, 0))
12610             return fold_build2 (code, type,
12611                                 fold_build2 (BIT_AND_EXPR, itype,
12612                                              fold_build2 (BIT_XOR_EXPR, itype,
12613                                                           arg00, arg10),
12614                                              arg01),
12615                                 build_int_cst (itype, 0));
12616
12617           if (operand_equal_p (arg01, arg10, 0))
12618             return fold_build2 (code, type,
12619                                 fold_build2 (BIT_AND_EXPR, itype,
12620                                              fold_build2 (BIT_XOR_EXPR, itype,
12621                                                           arg00, arg11),
12622                                              arg01),
12623                                 build_int_cst (itype, 0));
12624
12625           if (operand_equal_p (arg00, arg11, 0))
12626             return fold_build2 (code, type,
12627                                 fold_build2 (BIT_AND_EXPR, itype,
12628                                              fold_build2 (BIT_XOR_EXPR, itype,
12629                                                           arg01, arg10),
12630                                              arg00),
12631                                 build_int_cst (itype, 0));
12632
12633           if (operand_equal_p (arg00, arg10, 0))
12634             return fold_build2 (code, type,
12635                                 fold_build2 (BIT_AND_EXPR, itype,
12636                                              fold_build2 (BIT_XOR_EXPR, itype,
12637                                                           arg01, arg11),
12638                                              arg00),
12639                                 build_int_cst (itype, 0));
12640         }
12641
12642       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12643           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12644         {
12645           tree arg00 = TREE_OPERAND (arg0, 0);
12646           tree arg01 = TREE_OPERAND (arg0, 1);
12647           tree arg10 = TREE_OPERAND (arg1, 0);
12648           tree arg11 = TREE_OPERAND (arg1, 1);
12649           tree itype = TREE_TYPE (arg0);
12650
12651           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12652              operand_equal_p guarantees no side-effects so we don't need
12653              to use omit_one_operand on Z.  */
12654           if (operand_equal_p (arg01, arg11, 0))
12655             return fold_build2 (code, type, arg00, arg10);
12656           if (operand_equal_p (arg01, arg10, 0))
12657             return fold_build2 (code, type, arg00, arg11);
12658           if (operand_equal_p (arg00, arg11, 0))
12659             return fold_build2 (code, type, arg01, arg10);
12660           if (operand_equal_p (arg00, arg10, 0))
12661             return fold_build2 (code, type, arg01, arg11);
12662
12663           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12664           if (TREE_CODE (arg01) == INTEGER_CST
12665               && TREE_CODE (arg11) == INTEGER_CST)
12666             return fold_build2 (code, type,
12667                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12668                                              fold_build2 (BIT_XOR_EXPR, itype,
12669                                                           arg01, arg11)),
12670                                 arg10);
12671         }
12672
12673       /* Attempt to simplify equality/inequality comparisons of complex
12674          values.  Only lower the comparison if the result is known or
12675          can be simplified to a single scalar comparison.  */
12676       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12677            || TREE_CODE (arg0) == COMPLEX_CST)
12678           && (TREE_CODE (arg1) == COMPLEX_EXPR
12679               || TREE_CODE (arg1) == COMPLEX_CST))
12680         {
12681           tree real0, imag0, real1, imag1;
12682           tree rcond, icond;
12683
12684           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12685             {
12686               real0 = TREE_OPERAND (arg0, 0);
12687               imag0 = TREE_OPERAND (arg0, 1);
12688             }
12689           else
12690             {
12691               real0 = TREE_REALPART (arg0);
12692               imag0 = TREE_IMAGPART (arg0);
12693             }
12694
12695           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12696             {
12697               real1 = TREE_OPERAND (arg1, 0);
12698               imag1 = TREE_OPERAND (arg1, 1);
12699             }
12700           else
12701             {
12702               real1 = TREE_REALPART (arg1);
12703               imag1 = TREE_IMAGPART (arg1);
12704             }
12705
12706           rcond = fold_binary (code, type, real0, real1);
12707           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12708             {
12709               if (integer_zerop (rcond))
12710                 {
12711                   if (code == EQ_EXPR)
12712                     return omit_two_operands (type, boolean_false_node,
12713                                               imag0, imag1);
12714                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12715                 }
12716               else
12717                 {
12718                   if (code == NE_EXPR)
12719                     return omit_two_operands (type, boolean_true_node,
12720                                               imag0, imag1);
12721                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12722                 }
12723             }
12724
12725           icond = fold_binary (code, type, imag0, imag1);
12726           if (icond && TREE_CODE (icond) == INTEGER_CST)
12727             {
12728               if (integer_zerop (icond))
12729                 {
12730                   if (code == EQ_EXPR)
12731                     return omit_two_operands (type, boolean_false_node,
12732                                               real0, real1);
12733                   return fold_build2 (NE_EXPR, type, real0, real1);
12734                 }
12735               else
12736                 {
12737                   if (code == NE_EXPR)
12738                     return omit_two_operands (type, boolean_true_node,
12739                                               real0, real1);
12740                   return fold_build2 (EQ_EXPR, type, real0, real1);
12741                 }
12742             }
12743         }
12744
12745       return NULL_TREE;
12746
12747     case LT_EXPR:
12748     case GT_EXPR:
12749     case LE_EXPR:
12750     case GE_EXPR:
12751       tem = fold_comparison (code, type, op0, op1);
12752       if (tem != NULL_TREE)
12753         return tem;
12754
12755       /* Transform comparisons of the form X +- C CMP X.  */
12756       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12757           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12758           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12759                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12760               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12761                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12762         {
12763           tree arg01 = TREE_OPERAND (arg0, 1);
12764           enum tree_code code0 = TREE_CODE (arg0);
12765           int is_positive;
12766
12767           if (TREE_CODE (arg01) == REAL_CST)
12768             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12769           else
12770             is_positive = tree_int_cst_sgn (arg01);
12771
12772           /* (X - c) > X becomes false.  */
12773           if (code == GT_EXPR
12774               && ((code0 == MINUS_EXPR && is_positive >= 0)
12775                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12776             {
12777               if (TREE_CODE (arg01) == INTEGER_CST
12778                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12779                 fold_overflow_warning (("assuming signed overflow does not "
12780                                         "occur when assuming that (X - c) > X "
12781                                         "is always false"),
12782                                        WARN_STRICT_OVERFLOW_ALL);
12783               return constant_boolean_node (0, type);
12784             }
12785
12786           /* Likewise (X + c) < X becomes false.  */
12787           if (code == LT_EXPR
12788               && ((code0 == PLUS_EXPR && is_positive >= 0)
12789                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12790             {
12791               if (TREE_CODE (arg01) == INTEGER_CST
12792                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12793                 fold_overflow_warning (("assuming signed overflow does not "
12794                                         "occur when assuming that "
12795                                         "(X + c) < X is always false"),
12796                                        WARN_STRICT_OVERFLOW_ALL);
12797               return constant_boolean_node (0, type);
12798             }
12799
12800           /* Convert (X - c) <= X to true.  */
12801           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12802               && code == LE_EXPR
12803               && ((code0 == MINUS_EXPR && is_positive >= 0)
12804                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12805             {
12806               if (TREE_CODE (arg01) == INTEGER_CST
12807                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12808                 fold_overflow_warning (("assuming signed overflow does not "
12809                                         "occur when assuming that "
12810                                         "(X - c) <= X is always true"),
12811                                        WARN_STRICT_OVERFLOW_ALL);
12812               return constant_boolean_node (1, type);
12813             }
12814
12815           /* Convert (X + c) >= X to true.  */
12816           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12817               && code == GE_EXPR
12818               && ((code0 == PLUS_EXPR && is_positive >= 0)
12819                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12820             {
12821               if (TREE_CODE (arg01) == INTEGER_CST
12822                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12823                 fold_overflow_warning (("assuming signed overflow does not "
12824                                         "occur when assuming that "
12825                                         "(X + c) >= X is always true"),
12826                                        WARN_STRICT_OVERFLOW_ALL);
12827               return constant_boolean_node (1, type);
12828             }
12829
12830           if (TREE_CODE (arg01) == INTEGER_CST)
12831             {
12832               /* Convert X + c > X and X - c < X to true for integers.  */
12833               if (code == GT_EXPR
12834                   && ((code0 == PLUS_EXPR && is_positive > 0)
12835                       || (code0 == MINUS_EXPR && is_positive < 0)))
12836                 {
12837                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12838                     fold_overflow_warning (("assuming signed overflow does "
12839                                             "not occur when assuming that "
12840                                             "(X + c) > X is always true"),
12841                                            WARN_STRICT_OVERFLOW_ALL);
12842                   return constant_boolean_node (1, type);
12843                 }
12844
12845               if (code == LT_EXPR
12846                   && ((code0 == MINUS_EXPR && is_positive > 0)
12847                       || (code0 == PLUS_EXPR && is_positive < 0)))
12848                 {
12849                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12850                     fold_overflow_warning (("assuming signed overflow does "
12851                                             "not occur when assuming that "
12852                                             "(X - c) < X is always true"),
12853                                            WARN_STRICT_OVERFLOW_ALL);
12854                   return constant_boolean_node (1, type);
12855                 }
12856
12857               /* Convert X + c <= X and X - c >= X to false for integers.  */
12858               if (code == LE_EXPR
12859                   && ((code0 == PLUS_EXPR && is_positive > 0)
12860                       || (code0 == MINUS_EXPR && is_positive < 0)))
12861                 {
12862                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12863                     fold_overflow_warning (("assuming signed overflow does "
12864                                             "not occur when assuming that "
12865                                             "(X + c) <= X is always false"),
12866                                            WARN_STRICT_OVERFLOW_ALL);
12867                   return constant_boolean_node (0, type);
12868                 }
12869
12870               if (code == GE_EXPR
12871                   && ((code0 == MINUS_EXPR && is_positive > 0)
12872                       || (code0 == PLUS_EXPR && is_positive < 0)))
12873                 {
12874                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12875                     fold_overflow_warning (("assuming signed overflow does "
12876                                             "not occur when assuming that "
12877                                             "(X - c) >= X is always false"),
12878                                            WARN_STRICT_OVERFLOW_ALL);
12879                   return constant_boolean_node (0, type);
12880                 }
12881             }
12882         }
12883
12884       /* Comparisons with the highest or lowest possible integer of
12885          the specified precision will have known values.  */
12886       {
12887         tree arg1_type = TREE_TYPE (arg1);
12888         unsigned int width = TYPE_PRECISION (arg1_type);
12889
12890         if (TREE_CODE (arg1) == INTEGER_CST
12891             && width <= 2 * HOST_BITS_PER_WIDE_INT
12892             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12893           {
12894             HOST_WIDE_INT signed_max_hi;
12895             unsigned HOST_WIDE_INT signed_max_lo;
12896             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12897
12898             if (width <= HOST_BITS_PER_WIDE_INT)
12899               {
12900                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12901                                 - 1;
12902                 signed_max_hi = 0;
12903                 max_hi = 0;
12904
12905                 if (TYPE_UNSIGNED (arg1_type))
12906                   {
12907                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12908                     min_lo = 0;
12909                     min_hi = 0;
12910                   }
12911                 else
12912                   {
12913                     max_lo = signed_max_lo;
12914                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12915                     min_hi = -1;
12916                   }
12917               }
12918             else
12919               {
12920                 width -= HOST_BITS_PER_WIDE_INT;
12921                 signed_max_lo = -1;
12922                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12923                                 - 1;
12924                 max_lo = -1;
12925                 min_lo = 0;
12926
12927                 if (TYPE_UNSIGNED (arg1_type))
12928                   {
12929                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12930                     min_hi = 0;
12931                   }
12932                 else
12933                   {
12934                     max_hi = signed_max_hi;
12935                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12936                   }
12937               }
12938
12939             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12940                 && TREE_INT_CST_LOW (arg1) == max_lo)
12941               switch (code)
12942                 {
12943                 case GT_EXPR:
12944                   return omit_one_operand (type, integer_zero_node, arg0);
12945
12946                 case GE_EXPR:
12947                   return fold_build2 (EQ_EXPR, type, op0, op1);
12948
12949                 case LE_EXPR:
12950                   return omit_one_operand (type, integer_one_node, arg0);
12951
12952                 case LT_EXPR:
12953                   return fold_build2 (NE_EXPR, type, op0, op1);
12954
12955                 /* The GE_EXPR and LT_EXPR cases above are not normally
12956                    reached because of previous transformations.  */
12957
12958                 default:
12959                   break;
12960                 }
12961             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12962                      == max_hi
12963                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12964               switch (code)
12965                 {
12966                 case GT_EXPR:
12967                   arg1 = const_binop (PLUS_EXPR, arg1,
12968                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12969                   return fold_build2 (EQ_EXPR, type,
12970                                       fold_convert (TREE_TYPE (arg1), arg0),
12971                                       arg1);
12972                 case LE_EXPR:
12973                   arg1 = const_binop (PLUS_EXPR, arg1,
12974                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12975                   return fold_build2 (NE_EXPR, type,
12976                                       fold_convert (TREE_TYPE (arg1), arg0),
12977                                       arg1);
12978                 default:
12979                   break;
12980                 }
12981             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12982                      == min_hi
12983                      && TREE_INT_CST_LOW (arg1) == min_lo)
12984               switch (code)
12985                 {
12986                 case LT_EXPR:
12987                   return omit_one_operand (type, integer_zero_node, arg0);
12988
12989                 case LE_EXPR:
12990                   return fold_build2 (EQ_EXPR, type, op0, op1);
12991
12992                 case GE_EXPR:
12993                   return omit_one_operand (type, integer_one_node, arg0);
12994
12995                 case GT_EXPR:
12996                   return fold_build2 (NE_EXPR, type, op0, op1);
12997
12998                 default:
12999                   break;
13000                 }
13001             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13002                      == min_hi
13003                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13004               switch (code)
13005                 {
13006                 case GE_EXPR:
13007                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13008                   return fold_build2 (NE_EXPR, type,
13009                                       fold_convert (TREE_TYPE (arg1), arg0),
13010                                       arg1);
13011                 case LT_EXPR:
13012                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13013                   return fold_build2 (EQ_EXPR, type,
13014                                       fold_convert (TREE_TYPE (arg1), arg0),
13015                                       arg1);
13016                 default:
13017                   break;
13018                 }
13019
13020             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13021                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
13022                      && TYPE_UNSIGNED (arg1_type)
13023                      /* We will flip the signedness of the comparison operator
13024                         associated with the mode of arg1, so the sign bit is
13025                         specified by this mode.  Check that arg1 is the signed
13026                         max associated with this sign bit.  */
13027                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13028                      /* signed_type does not work on pointer types.  */
13029                      && INTEGRAL_TYPE_P (arg1_type))
13030               {
13031                 /* The following case also applies to X < signed_max+1
13032                    and X >= signed_max+1 because previous transformations.  */
13033                 if (code == LE_EXPR || code == GT_EXPR)
13034                   {
13035                     tree st;
13036                     st = signed_type_for (TREE_TYPE (arg1));
13037                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
13038                                         type, fold_convert (st, arg0),
13039                                         build_int_cst (st, 0));
13040                   }
13041               }
13042           }
13043       }
13044
13045       /* If we are comparing an ABS_EXPR with a constant, we can
13046          convert all the cases into explicit comparisons, but they may
13047          well not be faster than doing the ABS and one comparison.
13048          But ABS (X) <= C is a range comparison, which becomes a subtraction
13049          and a comparison, and is probably faster.  */
13050       if (code == LE_EXPR
13051           && TREE_CODE (arg1) == INTEGER_CST
13052           && TREE_CODE (arg0) == ABS_EXPR
13053           && ! TREE_SIDE_EFFECTS (arg0)
13054           && (0 != (tem = negate_expr (arg1)))
13055           && TREE_CODE (tem) == INTEGER_CST
13056           && !TREE_OVERFLOW (tem))
13057         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13058                             build2 (GE_EXPR, type,
13059                                     TREE_OPERAND (arg0, 0), tem),
13060                             build2 (LE_EXPR, type,
13061                                     TREE_OPERAND (arg0, 0), arg1));
13062
13063       /* Convert ABS_EXPR<x> >= 0 to true.  */
13064       strict_overflow_p = false;
13065       if (code == GE_EXPR
13066           && (integer_zerop (arg1)
13067               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13068                   && real_zerop (arg1)))
13069           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13070         {
13071           if (strict_overflow_p)
13072             fold_overflow_warning (("assuming signed overflow does not occur "
13073                                     "when simplifying comparison of "
13074                                     "absolute value and zero"),
13075                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13076           return omit_one_operand (type, integer_one_node, arg0);
13077         }
13078
13079       /* Convert ABS_EXPR<x> < 0 to false.  */
13080       strict_overflow_p = false;
13081       if (code == LT_EXPR
13082           && (integer_zerop (arg1) || real_zerop (arg1))
13083           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13084         {
13085           if (strict_overflow_p)
13086             fold_overflow_warning (("assuming signed overflow does not occur "
13087                                     "when simplifying comparison of "
13088                                     "absolute value and zero"),
13089                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13090           return omit_one_operand (type, integer_zero_node, arg0);
13091         }
13092
13093       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13094          and similarly for >= into !=.  */
13095       if ((code == LT_EXPR || code == GE_EXPR)
13096           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13097           && TREE_CODE (arg1) == LSHIFT_EXPR
13098           && integer_onep (TREE_OPERAND (arg1, 0)))
13099         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13100                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13101                                TREE_OPERAND (arg1, 1)),
13102                        build_int_cst (TREE_TYPE (arg0), 0));
13103
13104       if ((code == LT_EXPR || code == GE_EXPR)
13105           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13106           && CONVERT_EXPR_P (arg1)
13107           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13108           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13109         return
13110           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13111                   fold_convert (TREE_TYPE (arg0),
13112                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13113                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
13114                                                       1))),
13115                   build_int_cst (TREE_TYPE (arg0), 0));
13116
13117       return NULL_TREE;
13118
13119     case UNORDERED_EXPR:
13120     case ORDERED_EXPR:
13121     case UNLT_EXPR:
13122     case UNLE_EXPR:
13123     case UNGT_EXPR:
13124     case UNGE_EXPR:
13125     case UNEQ_EXPR:
13126     case LTGT_EXPR:
13127       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13128         {
13129           t1 = fold_relational_const (code, type, arg0, arg1);
13130           if (t1 != NULL_TREE)
13131             return t1;
13132         }
13133
13134       /* If the first operand is NaN, the result is constant.  */
13135       if (TREE_CODE (arg0) == REAL_CST
13136           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13137           && (code != LTGT_EXPR || ! flag_trapping_math))
13138         {
13139           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13140                ? integer_zero_node
13141                : integer_one_node;
13142           return omit_one_operand (type, t1, arg1);
13143         }
13144
13145       /* If the second operand is NaN, the result is constant.  */
13146       if (TREE_CODE (arg1) == REAL_CST
13147           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13148           && (code != LTGT_EXPR || ! flag_trapping_math))
13149         {
13150           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13151                ? integer_zero_node
13152                : integer_one_node;
13153           return omit_one_operand (type, t1, arg0);
13154         }
13155
13156       /* Simplify unordered comparison of something with itself.  */
13157       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13158           && operand_equal_p (arg0, arg1, 0))
13159         return constant_boolean_node (1, type);
13160
13161       if (code == LTGT_EXPR
13162           && !flag_trapping_math
13163           && operand_equal_p (arg0, arg1, 0))
13164         return constant_boolean_node (0, type);
13165
13166       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13167       {
13168         tree targ0 = strip_float_extensions (arg0);
13169         tree targ1 = strip_float_extensions (arg1);
13170         tree newtype = TREE_TYPE (targ0);
13171
13172         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13173           newtype = TREE_TYPE (targ1);
13174
13175         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13176           return fold_build2 (code, type, fold_convert (newtype, targ0),
13177                               fold_convert (newtype, targ1));
13178       }
13179
13180       return NULL_TREE;
13181
13182     case COMPOUND_EXPR:
13183       /* When pedantic, a compound expression can be neither an lvalue
13184          nor an integer constant expression.  */
13185       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13186         return NULL_TREE;
13187       /* Don't let (0, 0) be null pointer constant.  */
13188       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13189                                  : fold_convert (type, arg1);
13190       return pedantic_non_lvalue (tem);
13191
13192     case COMPLEX_EXPR:
13193       if ((TREE_CODE (arg0) == REAL_CST
13194            && TREE_CODE (arg1) == REAL_CST)
13195           || (TREE_CODE (arg0) == INTEGER_CST
13196               && TREE_CODE (arg1) == INTEGER_CST))
13197         return build_complex (type, arg0, arg1);
13198       return NULL_TREE;
13199
13200     case ASSERT_EXPR:
13201       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13202       gcc_unreachable ();
13203
13204     default:
13205       return NULL_TREE;
13206     } /* switch (code) */
13207 }
13208
13209 /* Callback for walk_tree, looking for LABEL_EXPR.
13210    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13211    Do not check the sub-tree of GOTO_EXPR.  */
13212
13213 static tree
13214 contains_label_1 (tree *tp,
13215                   int *walk_subtrees,
13216                   void *data ATTRIBUTE_UNUSED)
13217 {
13218   switch (TREE_CODE (*tp))
13219     {
13220     case LABEL_EXPR:
13221       return *tp;
13222     case GOTO_EXPR:
13223       *walk_subtrees = 0;
13224     /* no break */
13225     default:
13226       return NULL_TREE;
13227     }
13228 }
13229
13230 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13231    accessible from outside the sub-tree. Returns NULL_TREE if no
13232    addressable label is found.  */
13233
13234 static bool
13235 contains_label_p (tree st)
13236 {
13237   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13238 }
13239
13240 /* Fold a ternary expression of code CODE and type TYPE with operands
13241    OP0, OP1, and OP2.  Return the folded expression if folding is
13242    successful.  Otherwise, return NULL_TREE.  */
13243
13244 tree
13245 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13246 {
13247   tree tem;
13248   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13249   enum tree_code_class kind = TREE_CODE_CLASS (code);
13250
13251   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13252               && TREE_CODE_LENGTH (code) == 3);
13253
13254   /* Strip any conversions that don't change the mode.  This is safe
13255      for every expression, except for a comparison expression because
13256      its signedness is derived from its operands.  So, in the latter
13257      case, only strip conversions that don't change the signedness.
13258
13259      Note that this is done as an internal manipulation within the
13260      constant folder, in order to find the simplest representation of
13261      the arguments so that their form can be studied.  In any cases,
13262      the appropriate type conversions should be put back in the tree
13263      that will get out of the constant folder.  */
13264   if (op0)
13265     {
13266       arg0 = op0;
13267       STRIP_NOPS (arg0);
13268     }
13269
13270   if (op1)
13271     {
13272       arg1 = op1;
13273       STRIP_NOPS (arg1);
13274     }
13275
13276   switch (code)
13277     {
13278     case COMPONENT_REF:
13279       if (TREE_CODE (arg0) == CONSTRUCTOR
13280           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13281         {
13282           unsigned HOST_WIDE_INT idx;
13283           tree field, value;
13284           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13285             if (field == arg1)
13286               return value;
13287         }
13288       return NULL_TREE;
13289
13290     case COND_EXPR:
13291       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13292          so all simple results must be passed through pedantic_non_lvalue.  */
13293       if (TREE_CODE (arg0) == INTEGER_CST)
13294         {
13295           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13296           tem = integer_zerop (arg0) ? op2 : op1;
13297           /* Only optimize constant conditions when the selected branch
13298              has the same type as the COND_EXPR.  This avoids optimizing
13299              away "c ? x : throw", where the throw has a void type.
13300              Avoid throwing away that operand which contains label.  */
13301           if ((!TREE_SIDE_EFFECTS (unused_op)
13302                || !contains_label_p (unused_op))
13303               && (! VOID_TYPE_P (TREE_TYPE (tem))
13304                   || VOID_TYPE_P (type)))
13305             return pedantic_non_lvalue (tem);
13306           return NULL_TREE;
13307         }
13308       if (operand_equal_p (arg1, op2, 0))
13309         return pedantic_omit_one_operand (type, arg1, arg0);
13310
13311       /* If we have A op B ? A : C, we may be able to convert this to a
13312          simpler expression, depending on the operation and the values
13313          of B and C.  Signed zeros prevent all of these transformations,
13314          for reasons given above each one.
13315
13316          Also try swapping the arguments and inverting the conditional.  */
13317       if (COMPARISON_CLASS_P (arg0)
13318           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13319                                              arg1, TREE_OPERAND (arg0, 1))
13320           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13321         {
13322           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13323           if (tem)
13324             return tem;
13325         }
13326
13327       if (COMPARISON_CLASS_P (arg0)
13328           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13329                                              op2,
13330                                              TREE_OPERAND (arg0, 1))
13331           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13332         {
13333           tem = fold_truth_not_expr (arg0);
13334           if (tem && COMPARISON_CLASS_P (tem))
13335             {
13336               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13337               if (tem)
13338                 return tem;
13339             }
13340         }
13341
13342       /* If the second operand is simpler than the third, swap them
13343          since that produces better jump optimization results.  */
13344       if (truth_value_p (TREE_CODE (arg0))
13345           && tree_swap_operands_p (op1, op2, false))
13346         {
13347           /* See if this can be inverted.  If it can't, possibly because
13348              it was a floating-point inequality comparison, don't do
13349              anything.  */
13350           tem = fold_truth_not_expr (arg0);
13351           if (tem)
13352             return fold_build3 (code, type, tem, op2, op1);
13353         }
13354
13355       /* Convert A ? 1 : 0 to simply A.  */
13356       if (integer_onep (op1)
13357           && integer_zerop (op2)
13358           /* If we try to convert OP0 to our type, the
13359              call to fold will try to move the conversion inside
13360              a COND, which will recurse.  In that case, the COND_EXPR
13361              is probably the best choice, so leave it alone.  */
13362           && type == TREE_TYPE (arg0))
13363         return pedantic_non_lvalue (arg0);
13364
13365       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13366          over COND_EXPR in cases such as floating point comparisons.  */
13367       if (integer_zerop (op1)
13368           && integer_onep (op2)
13369           && truth_value_p (TREE_CODE (arg0)))
13370         return pedantic_non_lvalue (fold_convert (type,
13371                                                   invert_truthvalue (arg0)));
13372
13373       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13374       if (TREE_CODE (arg0) == LT_EXPR
13375           && integer_zerop (TREE_OPERAND (arg0, 1))
13376           && integer_zerop (op2)
13377           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13378         {
13379           /* sign_bit_p only checks ARG1 bits within A's precision.
13380              If <sign bit of A> has wider type than A, bits outside
13381              of A's precision in <sign bit of A> need to be checked.
13382              If they are all 0, this optimization needs to be done
13383              in unsigned A's type, if they are all 1 in signed A's type,
13384              otherwise this can't be done.  */
13385           if (TYPE_PRECISION (TREE_TYPE (tem))
13386               < TYPE_PRECISION (TREE_TYPE (arg1))
13387               && TYPE_PRECISION (TREE_TYPE (tem))
13388                  < TYPE_PRECISION (type))
13389             {
13390               unsigned HOST_WIDE_INT mask_lo;
13391               HOST_WIDE_INT mask_hi;
13392               int inner_width, outer_width;
13393               tree tem_type;
13394
13395               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13396               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13397               if (outer_width > TYPE_PRECISION (type))
13398                 outer_width = TYPE_PRECISION (type);
13399
13400               if (outer_width > HOST_BITS_PER_WIDE_INT)
13401                 {
13402                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13403                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13404                   mask_lo = -1;
13405                 }
13406               else
13407                 {
13408                   mask_hi = 0;
13409                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13410                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13411                 }
13412               if (inner_width > HOST_BITS_PER_WIDE_INT)
13413                 {
13414                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13415                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13416                   mask_lo = 0;
13417                 }
13418               else
13419                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13420                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13421
13422               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13423                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13424                 {
13425                   tem_type = signed_type_for (TREE_TYPE (tem));
13426                   tem = fold_convert (tem_type, tem);
13427                 }
13428               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13429                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13430                 {
13431                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13432                   tem = fold_convert (tem_type, tem);
13433                 }
13434               else
13435                 tem = NULL;
13436             }
13437
13438           if (tem)
13439             return fold_convert (type,
13440                                  fold_build2 (BIT_AND_EXPR,
13441                                               TREE_TYPE (tem), tem,
13442                                               fold_convert (TREE_TYPE (tem),
13443                                                             arg1)));
13444         }
13445
13446       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13447          already handled above.  */
13448       if (TREE_CODE (arg0) == BIT_AND_EXPR
13449           && integer_onep (TREE_OPERAND (arg0, 1))
13450           && integer_zerop (op2)
13451           && integer_pow2p (arg1))
13452         {
13453           tree tem = TREE_OPERAND (arg0, 0);
13454           STRIP_NOPS (tem);
13455           if (TREE_CODE (tem) == RSHIFT_EXPR
13456               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13457               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13458                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13459             return fold_build2 (BIT_AND_EXPR, type,
13460                                 TREE_OPERAND (tem, 0), arg1);
13461         }
13462
13463       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13464          is probably obsolete because the first operand should be a
13465          truth value (that's why we have the two cases above), but let's
13466          leave it in until we can confirm this for all front-ends.  */
13467       if (integer_zerop (op2)
13468           && TREE_CODE (arg0) == NE_EXPR
13469           && integer_zerop (TREE_OPERAND (arg0, 1))
13470           && integer_pow2p (arg1)
13471           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13472           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13473                               arg1, OEP_ONLY_CONST))
13474         return pedantic_non_lvalue (fold_convert (type,
13475                                                   TREE_OPERAND (arg0, 0)));
13476
13477       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13478       if (integer_zerop (op2)
13479           && truth_value_p (TREE_CODE (arg0))
13480           && truth_value_p (TREE_CODE (arg1)))
13481         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13482                             fold_convert (type, arg0),
13483                             arg1);
13484
13485       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13486       if (integer_onep (op2)
13487           && truth_value_p (TREE_CODE (arg0))
13488           && truth_value_p (TREE_CODE (arg1)))
13489         {
13490           /* Only perform transformation if ARG0 is easily inverted.  */
13491           tem = fold_truth_not_expr (arg0);
13492           if (tem)
13493             return fold_build2 (TRUTH_ORIF_EXPR, type,
13494                                 fold_convert (type, tem),
13495                                 arg1);
13496         }
13497
13498       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13499       if (integer_zerop (arg1)
13500           && truth_value_p (TREE_CODE (arg0))
13501           && truth_value_p (TREE_CODE (op2)))
13502         {
13503           /* Only perform transformation if ARG0 is easily inverted.  */
13504           tem = fold_truth_not_expr (arg0);
13505           if (tem)
13506             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13507                                 fold_convert (type, tem),
13508                                 op2);
13509         }
13510
13511       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13512       if (integer_onep (arg1)
13513           && truth_value_p (TREE_CODE (arg0))
13514           && truth_value_p (TREE_CODE (op2)))
13515         return fold_build2 (TRUTH_ORIF_EXPR, type,
13516                             fold_convert (type, arg0),
13517                             op2);
13518
13519       return NULL_TREE;
13520
13521     case CALL_EXPR:
13522       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13523          of fold_ternary on them.  */
13524       gcc_unreachable ();
13525
13526     case BIT_FIELD_REF:
13527       if ((TREE_CODE (arg0) == VECTOR_CST
13528            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13529           && type == TREE_TYPE (TREE_TYPE (arg0)))
13530         {
13531           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13532           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13533
13534           if (width != 0
13535               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13536               && (idx % width) == 0
13537               && (idx = idx / width)
13538                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13539             {
13540               tree elements = NULL_TREE;
13541
13542               if (TREE_CODE (arg0) == VECTOR_CST)
13543                 elements = TREE_VECTOR_CST_ELTS (arg0);
13544               else
13545                 {
13546                   unsigned HOST_WIDE_INT idx;
13547                   tree value;
13548
13549                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13550                     elements = tree_cons (NULL_TREE, value, elements);
13551                 }
13552               while (idx-- > 0 && elements)
13553                 elements = TREE_CHAIN (elements);
13554               if (elements)
13555                 return TREE_VALUE (elements);
13556               else
13557                 return fold_convert (type, integer_zero_node);
13558             }
13559         }
13560
13561       /* A bit-field-ref that referenced the full argument can be stripped.  */
13562       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13563           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13564           && integer_zerop (op2))
13565         return fold_convert (type, arg0);
13566
13567       return NULL_TREE;
13568
13569     default:
13570       return NULL_TREE;
13571     } /* switch (code) */
13572 }
13573
13574 /* Perform constant folding and related simplification of EXPR.
13575    The related simplifications include x*1 => x, x*0 => 0, etc.,
13576    and application of the associative law.
13577    NOP_EXPR conversions may be removed freely (as long as we
13578    are careful not to change the type of the overall expression).
13579    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13580    but we can constant-fold them if they have constant operands.  */
13581
13582 #ifdef ENABLE_FOLD_CHECKING
13583 # define fold(x) fold_1 (x)
13584 static tree fold_1 (tree);
13585 static
13586 #endif
13587 tree
13588 fold (tree expr)
13589 {
13590   const tree t = expr;
13591   enum tree_code code = TREE_CODE (t);
13592   enum tree_code_class kind = TREE_CODE_CLASS (code);
13593   tree tem;
13594
13595   /* Return right away if a constant.  */
13596   if (kind == tcc_constant)
13597     return t;
13598
13599   /* CALL_EXPR-like objects with variable numbers of operands are
13600      treated specially.  */
13601   if (kind == tcc_vl_exp)
13602     {
13603       if (code == CALL_EXPR)
13604         {
13605           tem = fold_call_expr (expr, false);
13606           return tem ? tem : expr;
13607         }
13608       return expr;
13609     }
13610
13611   if (IS_EXPR_CODE_CLASS (kind))
13612     {
13613       tree type = TREE_TYPE (t);
13614       tree op0, op1, op2;
13615
13616       switch (TREE_CODE_LENGTH (code))
13617         {
13618         case 1:
13619           op0 = TREE_OPERAND (t, 0);
13620           tem = fold_unary (code, type, op0);
13621           return tem ? tem : expr;
13622         case 2:
13623           op0 = TREE_OPERAND (t, 0);
13624           op1 = TREE_OPERAND (t, 1);
13625           tem = fold_binary (code, type, op0, op1);
13626           return tem ? tem : expr;
13627         case 3:
13628           op0 = TREE_OPERAND (t, 0);
13629           op1 = TREE_OPERAND (t, 1);
13630           op2 = TREE_OPERAND (t, 2);
13631           tem = fold_ternary (code, type, op0, op1, op2);
13632           return tem ? tem : expr;
13633         default:
13634           break;
13635         }
13636     }
13637
13638   switch (code)
13639     {
13640     case ARRAY_REF:
13641       {
13642         tree op0 = TREE_OPERAND (t, 0);
13643         tree op1 = TREE_OPERAND (t, 1);
13644
13645         if (TREE_CODE (op1) == INTEGER_CST
13646             && TREE_CODE (op0) == CONSTRUCTOR
13647             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13648           {
13649             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13650             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13651             unsigned HOST_WIDE_INT begin = 0;
13652
13653             /* Find a matching index by means of a binary search.  */
13654             while (begin != end)
13655               {
13656                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13657                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13658
13659                 if (TREE_CODE (index) == INTEGER_CST
13660                     && tree_int_cst_lt (index, op1))
13661                   begin = middle + 1;
13662                 else if (TREE_CODE (index) == INTEGER_CST
13663                          && tree_int_cst_lt (op1, index))
13664                   end = middle;
13665                 else if (TREE_CODE (index) == RANGE_EXPR
13666                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13667                   begin = middle + 1;
13668                 else if (TREE_CODE (index) == RANGE_EXPR
13669                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13670                   end = middle;
13671                 else
13672                   return VEC_index (constructor_elt, elts, middle)->value;
13673               }
13674           }
13675
13676         return t;
13677       }
13678
13679     case CONST_DECL:
13680       return fold (DECL_INITIAL (t));
13681
13682     default:
13683       return t;
13684     } /* switch (code) */
13685 }
13686
13687 #ifdef ENABLE_FOLD_CHECKING
13688 #undef fold
13689
13690 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13691 static void fold_check_failed (const_tree, const_tree);
13692 void print_fold_checksum (const_tree);
13693
13694 /* When --enable-checking=fold, compute a digest of expr before
13695    and after actual fold call to see if fold did not accidentally
13696    change original expr.  */
13697
13698 tree
13699 fold (tree expr)
13700 {
13701   tree ret;
13702   struct md5_ctx ctx;
13703   unsigned char checksum_before[16], checksum_after[16];
13704   htab_t ht;
13705
13706   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13707   md5_init_ctx (&ctx);
13708   fold_checksum_tree (expr, &ctx, ht);
13709   md5_finish_ctx (&ctx, checksum_before);
13710   htab_empty (ht);
13711
13712   ret = fold_1 (expr);
13713
13714   md5_init_ctx (&ctx);
13715   fold_checksum_tree (expr, &ctx, ht);
13716   md5_finish_ctx (&ctx, checksum_after);
13717   htab_delete (ht);
13718
13719   if (memcmp (checksum_before, checksum_after, 16))
13720     fold_check_failed (expr, ret);
13721
13722   return ret;
13723 }
13724
13725 void
13726 print_fold_checksum (const_tree expr)
13727 {
13728   struct md5_ctx ctx;
13729   unsigned char checksum[16], cnt;
13730   htab_t ht;
13731
13732   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13733   md5_init_ctx (&ctx);
13734   fold_checksum_tree (expr, &ctx, ht);
13735   md5_finish_ctx (&ctx, checksum);
13736   htab_delete (ht);
13737   for (cnt = 0; cnt < 16; ++cnt)
13738     fprintf (stderr, "%02x", checksum[cnt]);
13739   putc ('\n', stderr);
13740 }
13741
13742 static void
13743 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13744 {
13745   internal_error ("fold check: original tree changed by fold");
13746 }
13747
13748 static void
13749 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13750 {
13751   const void **slot;
13752   enum tree_code code;
13753   union tree_node buf;
13754   int i, len;
13755   
13756 recursive_label:
13757
13758   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13759                <= sizeof (struct tree_function_decl))
13760               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13761   if (expr == NULL)
13762     return;
13763   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13764   if (*slot != NULL)
13765     return;
13766   *slot = expr;
13767   code = TREE_CODE (expr);
13768   if (TREE_CODE_CLASS (code) == tcc_declaration
13769       && DECL_ASSEMBLER_NAME_SET_P (expr))
13770     {
13771       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13772       memcpy ((char *) &buf, expr, tree_size (expr));
13773       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13774       expr = (tree) &buf;
13775     }
13776   else if (TREE_CODE_CLASS (code) == tcc_type
13777            && (TYPE_POINTER_TO (expr)
13778                || TYPE_REFERENCE_TO (expr)
13779                || TYPE_CACHED_VALUES_P (expr)
13780                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13781                || TYPE_NEXT_VARIANT (expr)))
13782     {
13783       /* Allow these fields to be modified.  */
13784       tree tmp;
13785       memcpy ((char *) &buf, expr, tree_size (expr));
13786       expr = tmp = (tree) &buf;
13787       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13788       TYPE_POINTER_TO (tmp) = NULL;
13789       TYPE_REFERENCE_TO (tmp) = NULL;
13790       TYPE_NEXT_VARIANT (tmp) = NULL;
13791       if (TYPE_CACHED_VALUES_P (tmp))
13792         {
13793           TYPE_CACHED_VALUES_P (tmp) = 0;
13794           TYPE_CACHED_VALUES (tmp) = NULL;
13795         }
13796     }
13797   md5_process_bytes (expr, tree_size (expr), ctx);
13798   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13799   if (TREE_CODE_CLASS (code) != tcc_type
13800       && TREE_CODE_CLASS (code) != tcc_declaration
13801       && code != TREE_LIST
13802       && code != SSA_NAME)
13803     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13804   switch (TREE_CODE_CLASS (code))
13805     {
13806     case tcc_constant:
13807       switch (code)
13808         {
13809         case STRING_CST:
13810           md5_process_bytes (TREE_STRING_POINTER (expr),
13811                              TREE_STRING_LENGTH (expr), ctx);
13812           break;
13813         case COMPLEX_CST:
13814           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13815           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13816           break;
13817         case VECTOR_CST:
13818           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13819           break;
13820         default:
13821           break;
13822         }
13823       break;
13824     case tcc_exceptional:
13825       switch (code)
13826         {
13827         case TREE_LIST:
13828           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13829           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13830           expr = TREE_CHAIN (expr);
13831           goto recursive_label;
13832           break;
13833         case TREE_VEC:
13834           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13835             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13836           break;
13837         default:
13838           break;
13839         }
13840       break;
13841     case tcc_expression:
13842     case tcc_reference:
13843     case tcc_comparison:
13844     case tcc_unary:
13845     case tcc_binary:
13846     case tcc_statement:
13847     case tcc_vl_exp:
13848       len = TREE_OPERAND_LENGTH (expr);
13849       for (i = 0; i < len; ++i)
13850         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13851       break;
13852     case tcc_declaration:
13853       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13854       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13855       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13856         {
13857           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13858           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13859           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13860           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13861           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13862         }
13863       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13864         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13865           
13866       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13867         {
13868           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13869           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13870           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13871         }
13872       break;
13873     case tcc_type:
13874       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13875         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13876       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13877       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13878       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13879       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13880       if (INTEGRAL_TYPE_P (expr)
13881           || SCALAR_FLOAT_TYPE_P (expr))
13882         {
13883           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13884           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13885         }
13886       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13887       if (TREE_CODE (expr) == RECORD_TYPE
13888           || TREE_CODE (expr) == UNION_TYPE
13889           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13890         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13891       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13892       break;
13893     default:
13894       break;
13895     }
13896 }
13897
13898 /* Helper function for outputting the checksum of a tree T.  When
13899    debugging with gdb, you can "define mynext" to be "next" followed
13900    by "call debug_fold_checksum (op0)", then just trace down till the
13901    outputs differ.  */
13902
13903 void
13904 debug_fold_checksum (const_tree t)
13905 {
13906   int i;
13907   unsigned char checksum[16];
13908   struct md5_ctx ctx;
13909   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13910   
13911   md5_init_ctx (&ctx);
13912   fold_checksum_tree (t, &ctx, ht);
13913   md5_finish_ctx (&ctx, checksum);
13914   htab_empty (ht);
13915
13916   for (i = 0; i < 16; i++)
13917     fprintf (stderr, "%d ", checksum[i]);
13918
13919   fprintf (stderr, "\n");
13920 }
13921
13922 #endif
13923
13924 /* Fold a unary tree expression with code CODE of type TYPE with an
13925    operand OP0.  Return a folded expression if successful.  Otherwise,
13926    return a tree expression with code CODE of type TYPE with an
13927    operand OP0.  */
13928
13929 tree
13930 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13931 {
13932   tree tem;
13933 #ifdef ENABLE_FOLD_CHECKING
13934   unsigned char checksum_before[16], checksum_after[16];
13935   struct md5_ctx ctx;
13936   htab_t ht;
13937
13938   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13939   md5_init_ctx (&ctx);
13940   fold_checksum_tree (op0, &ctx, ht);
13941   md5_finish_ctx (&ctx, checksum_before);
13942   htab_empty (ht);
13943 #endif
13944   
13945   tem = fold_unary (code, type, op0);
13946   if (!tem)
13947     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13948   
13949 #ifdef ENABLE_FOLD_CHECKING
13950   md5_init_ctx (&ctx);
13951   fold_checksum_tree (op0, &ctx, ht);
13952   md5_finish_ctx (&ctx, checksum_after);
13953   htab_delete (ht);
13954
13955   if (memcmp (checksum_before, checksum_after, 16))
13956     fold_check_failed (op0, tem);
13957 #endif
13958   return tem;
13959 }
13960
13961 /* Fold a binary tree expression with code CODE of type TYPE with
13962    operands OP0 and OP1.  Return a folded expression if successful.
13963    Otherwise, return a tree expression with code CODE of type TYPE
13964    with operands OP0 and OP1.  */
13965
13966 tree
13967 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13968                   MEM_STAT_DECL)
13969 {
13970   tree tem;
13971 #ifdef ENABLE_FOLD_CHECKING
13972   unsigned char checksum_before_op0[16],
13973                 checksum_before_op1[16],
13974                 checksum_after_op0[16],
13975                 checksum_after_op1[16];
13976   struct md5_ctx ctx;
13977   htab_t ht;
13978
13979   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13980   md5_init_ctx (&ctx);
13981   fold_checksum_tree (op0, &ctx, ht);
13982   md5_finish_ctx (&ctx, checksum_before_op0);
13983   htab_empty (ht);
13984
13985   md5_init_ctx (&ctx);
13986   fold_checksum_tree (op1, &ctx, ht);
13987   md5_finish_ctx (&ctx, checksum_before_op1);
13988   htab_empty (ht);
13989 #endif
13990
13991   tem = fold_binary (code, type, op0, op1);
13992   if (!tem)
13993     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13994   
13995 #ifdef ENABLE_FOLD_CHECKING
13996   md5_init_ctx (&ctx);
13997   fold_checksum_tree (op0, &ctx, ht);
13998   md5_finish_ctx (&ctx, checksum_after_op0);
13999   htab_empty (ht);
14000
14001   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14002     fold_check_failed (op0, tem);
14003   
14004   md5_init_ctx (&ctx);
14005   fold_checksum_tree (op1, &ctx, ht);
14006   md5_finish_ctx (&ctx, checksum_after_op1);
14007   htab_delete (ht);
14008
14009   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14010     fold_check_failed (op1, tem);
14011 #endif
14012   return tem;
14013 }
14014
14015 /* Fold a ternary tree expression with code CODE of type TYPE with
14016    operands OP0, OP1, and OP2.  Return a folded expression if
14017    successful.  Otherwise, return a tree expression with code CODE of
14018    type TYPE with operands OP0, OP1, and OP2.  */
14019
14020 tree
14021 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
14022              MEM_STAT_DECL)
14023 {
14024   tree tem;
14025 #ifdef ENABLE_FOLD_CHECKING
14026   unsigned char checksum_before_op0[16],
14027                 checksum_before_op1[16],
14028                 checksum_before_op2[16],
14029                 checksum_after_op0[16],
14030                 checksum_after_op1[16],
14031                 checksum_after_op2[16];
14032   struct md5_ctx ctx;
14033   htab_t ht;
14034
14035   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14036   md5_init_ctx (&ctx);
14037   fold_checksum_tree (op0, &ctx, ht);
14038   md5_finish_ctx (&ctx, checksum_before_op0);
14039   htab_empty (ht);
14040
14041   md5_init_ctx (&ctx);
14042   fold_checksum_tree (op1, &ctx, ht);
14043   md5_finish_ctx (&ctx, checksum_before_op1);
14044   htab_empty (ht);
14045
14046   md5_init_ctx (&ctx);
14047   fold_checksum_tree (op2, &ctx, ht);
14048   md5_finish_ctx (&ctx, checksum_before_op2);
14049   htab_empty (ht);
14050 #endif
14051
14052   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14053   tem = fold_ternary (code, type, op0, op1, op2);
14054   if (!tem)
14055     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14056       
14057 #ifdef ENABLE_FOLD_CHECKING
14058   md5_init_ctx (&ctx);
14059   fold_checksum_tree (op0, &ctx, ht);
14060   md5_finish_ctx (&ctx, checksum_after_op0);
14061   htab_empty (ht);
14062
14063   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14064     fold_check_failed (op0, tem);
14065   
14066   md5_init_ctx (&ctx);
14067   fold_checksum_tree (op1, &ctx, ht);
14068   md5_finish_ctx (&ctx, checksum_after_op1);
14069   htab_empty (ht);
14070
14071   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14072     fold_check_failed (op1, tem);
14073   
14074   md5_init_ctx (&ctx);
14075   fold_checksum_tree (op2, &ctx, ht);
14076   md5_finish_ctx (&ctx, checksum_after_op2);
14077   htab_delete (ht);
14078
14079   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14080     fold_check_failed (op2, tem);
14081 #endif
14082   return tem;
14083 }
14084
14085 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14086    arguments in ARGARRAY, and a null static chain.
14087    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14088    of type TYPE from the given operands as constructed by build_call_array.  */
14089
14090 tree
14091 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14092 {
14093   tree tem;
14094 #ifdef ENABLE_FOLD_CHECKING
14095   unsigned char checksum_before_fn[16],
14096                 checksum_before_arglist[16],
14097                 checksum_after_fn[16],
14098                 checksum_after_arglist[16];
14099   struct md5_ctx ctx;
14100   htab_t ht;
14101   int i;
14102
14103   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14104   md5_init_ctx (&ctx);
14105   fold_checksum_tree (fn, &ctx, ht);
14106   md5_finish_ctx (&ctx, checksum_before_fn);
14107   htab_empty (ht);
14108
14109   md5_init_ctx (&ctx);
14110   for (i = 0; i < nargs; i++)
14111     fold_checksum_tree (argarray[i], &ctx, ht);
14112   md5_finish_ctx (&ctx, checksum_before_arglist);
14113   htab_empty (ht);
14114 #endif
14115
14116   tem = fold_builtin_call_array (type, fn, nargs, argarray);
14117       
14118 #ifdef ENABLE_FOLD_CHECKING
14119   md5_init_ctx (&ctx);
14120   fold_checksum_tree (fn, &ctx, ht);
14121   md5_finish_ctx (&ctx, checksum_after_fn);
14122   htab_empty (ht);
14123
14124   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14125     fold_check_failed (fn, tem);
14126   
14127   md5_init_ctx (&ctx);
14128   for (i = 0; i < nargs; i++)
14129     fold_checksum_tree (argarray[i], &ctx, ht);
14130   md5_finish_ctx (&ctx, checksum_after_arglist);
14131   htab_delete (ht);
14132
14133   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14134     fold_check_failed (NULL_TREE, tem);
14135 #endif
14136   return tem;
14137 }
14138
14139 /* Perform constant folding and related simplification of initializer
14140    expression EXPR.  These behave identically to "fold_buildN" but ignore
14141    potential run-time traps and exceptions that fold must preserve.  */
14142
14143 #define START_FOLD_INIT \
14144   int saved_signaling_nans = flag_signaling_nans;\
14145   int saved_trapping_math = flag_trapping_math;\
14146   int saved_rounding_math = flag_rounding_math;\
14147   int saved_trapv = flag_trapv;\
14148   int saved_folding_initializer = folding_initializer;\
14149   flag_signaling_nans = 0;\
14150   flag_trapping_math = 0;\
14151   flag_rounding_math = 0;\
14152   flag_trapv = 0;\
14153   folding_initializer = 1;
14154
14155 #define END_FOLD_INIT \
14156   flag_signaling_nans = saved_signaling_nans;\
14157   flag_trapping_math = saved_trapping_math;\
14158   flag_rounding_math = saved_rounding_math;\
14159   flag_trapv = saved_trapv;\
14160   folding_initializer = saved_folding_initializer;
14161
14162 tree
14163 fold_build1_initializer (enum tree_code code, tree type, tree op)
14164 {
14165   tree result;
14166   START_FOLD_INIT;
14167
14168   result = fold_build1 (code, type, op);
14169
14170   END_FOLD_INIT;
14171   return result;
14172 }
14173
14174 tree
14175 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14176 {
14177   tree result;
14178   START_FOLD_INIT;
14179
14180   result = fold_build2 (code, type, op0, op1);
14181
14182   END_FOLD_INIT;
14183   return result;
14184 }
14185
14186 tree
14187 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14188                          tree op2)
14189 {
14190   tree result;
14191   START_FOLD_INIT;
14192
14193   result = fold_build3 (code, type, op0, op1, op2);
14194
14195   END_FOLD_INIT;
14196   return result;
14197 }
14198
14199 tree
14200 fold_build_call_array_initializer (tree type, tree fn,
14201                                    int nargs, tree *argarray)
14202 {
14203   tree result;
14204   START_FOLD_INIT;
14205
14206   result = fold_build_call_array (type, fn, nargs, argarray);
14207
14208   END_FOLD_INIT;
14209   return result;
14210 }
14211
14212 #undef START_FOLD_INIT
14213 #undef END_FOLD_INIT
14214
14215 /* Determine if first argument is a multiple of second argument.  Return 0 if
14216    it is not, or we cannot easily determined it to be.
14217
14218    An example of the sort of thing we care about (at this point; this routine
14219    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14220    fold cases do now) is discovering that
14221
14222      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14223
14224    is a multiple of
14225
14226      SAVE_EXPR (J * 8)
14227
14228    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14229
14230    This code also handles discovering that
14231
14232      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14233
14234    is a multiple of 8 so we don't have to worry about dealing with a
14235    possible remainder.
14236
14237    Note that we *look* inside a SAVE_EXPR only to determine how it was
14238    calculated; it is not safe for fold to do much of anything else with the
14239    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14240    at run time.  For example, the latter example above *cannot* be implemented
14241    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14242    evaluation time of the original SAVE_EXPR is not necessarily the same at
14243    the time the new expression is evaluated.  The only optimization of this
14244    sort that would be valid is changing
14245
14246      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14247
14248    divided by 8 to
14249
14250      SAVE_EXPR (I) * SAVE_EXPR (J)
14251
14252    (where the same SAVE_EXPR (J) is used in the original and the
14253    transformed version).  */
14254
14255 int
14256 multiple_of_p (tree type, const_tree top, const_tree bottom)
14257 {
14258   if (operand_equal_p (top, bottom, 0))
14259     return 1;
14260
14261   if (TREE_CODE (type) != INTEGER_TYPE)
14262     return 0;
14263
14264   switch (TREE_CODE (top))
14265     {
14266     case BIT_AND_EXPR:
14267       /* Bitwise and provides a power of two multiple.  If the mask is
14268          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14269       if (!integer_pow2p (bottom))
14270         return 0;
14271       /* FALLTHRU */
14272
14273     case MULT_EXPR:
14274       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14275               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14276
14277     case PLUS_EXPR:
14278     case MINUS_EXPR:
14279       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14280               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14281
14282     case LSHIFT_EXPR:
14283       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14284         {
14285           tree op1, t1;
14286
14287           op1 = TREE_OPERAND (top, 1);
14288           /* const_binop may not detect overflow correctly,
14289              so check for it explicitly here.  */
14290           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14291               > TREE_INT_CST_LOW (op1)
14292               && TREE_INT_CST_HIGH (op1) == 0
14293               && 0 != (t1 = fold_convert (type,
14294                                           const_binop (LSHIFT_EXPR,
14295                                                        size_one_node,
14296                                                        op1, 0)))
14297               && !TREE_OVERFLOW (t1))
14298             return multiple_of_p (type, t1, bottom);
14299         }
14300       return 0;
14301
14302     case NOP_EXPR:
14303       /* Can't handle conversions from non-integral or wider integral type.  */
14304       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14305           || (TYPE_PRECISION (type)
14306               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14307         return 0;
14308
14309       /* .. fall through ...  */
14310
14311     case SAVE_EXPR:
14312       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14313
14314     case INTEGER_CST:
14315       if (TREE_CODE (bottom) != INTEGER_CST
14316           || integer_zerop (bottom)
14317           || (TYPE_UNSIGNED (type)
14318               && (tree_int_cst_sgn (top) < 0
14319                   || tree_int_cst_sgn (bottom) < 0)))
14320         return 0;
14321       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14322                                              top, bottom, 0));
14323
14324     default:
14325       return 0;
14326     }
14327 }
14328
14329 /* Return true if CODE or TYPE is known to be non-negative. */
14330
14331 static bool
14332 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14333 {
14334   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14335       && truth_value_p (code))
14336     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14337        have a signed:1 type (where the value is -1 and 0).  */
14338     return true;
14339   return false;
14340 }
14341
14342 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14343    value is based on the assumption that signed overflow is undefined,
14344    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14345    *STRICT_OVERFLOW_P.  */
14346
14347 bool
14348 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14349                                 bool *strict_overflow_p)
14350 {
14351   if (TYPE_UNSIGNED (type))
14352     return true;
14353
14354   switch (code)
14355     {
14356     case ABS_EXPR:
14357       /* We can't return 1 if flag_wrapv is set because
14358          ABS_EXPR<INT_MIN> = INT_MIN.  */
14359       if (!INTEGRAL_TYPE_P (type))
14360         return true;
14361       if (TYPE_OVERFLOW_UNDEFINED (type))
14362         {
14363           *strict_overflow_p = true;
14364           return true;
14365         }
14366       break;
14367
14368     case NON_LVALUE_EXPR:
14369     case FLOAT_EXPR:
14370     case FIX_TRUNC_EXPR:
14371       return tree_expr_nonnegative_warnv_p (op0,
14372                                             strict_overflow_p);
14373
14374     case NOP_EXPR:
14375       {
14376         tree inner_type = TREE_TYPE (op0);
14377         tree outer_type = type;
14378
14379         if (TREE_CODE (outer_type) == REAL_TYPE)
14380           {
14381             if (TREE_CODE (inner_type) == REAL_TYPE)
14382               return tree_expr_nonnegative_warnv_p (op0,
14383                                                     strict_overflow_p);
14384             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14385               {
14386                 if (TYPE_UNSIGNED (inner_type))
14387                   return true;
14388                 return tree_expr_nonnegative_warnv_p (op0,
14389                                                       strict_overflow_p);
14390               }
14391           }
14392         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14393           {
14394             if (TREE_CODE (inner_type) == REAL_TYPE)
14395               return tree_expr_nonnegative_warnv_p (op0,
14396                                                     strict_overflow_p);
14397             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14398               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14399                       && TYPE_UNSIGNED (inner_type);
14400           }
14401       }
14402       break;
14403
14404     default:
14405       return tree_simple_nonnegative_warnv_p (code, type);
14406     }
14407
14408   /* We don't know sign of `t', so be conservative and return false.  */
14409   return false;
14410 }
14411
14412 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14413    value is based on the assumption that signed overflow is undefined,
14414    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14415    *STRICT_OVERFLOW_P.  */
14416
14417 bool
14418 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14419                                       tree op1, bool *strict_overflow_p)
14420 {
14421   if (TYPE_UNSIGNED (type))
14422     return true;
14423
14424   switch (code)
14425     {
14426     case POINTER_PLUS_EXPR:
14427     case PLUS_EXPR:
14428       if (FLOAT_TYPE_P (type))
14429         return (tree_expr_nonnegative_warnv_p (op0,
14430                                                strict_overflow_p)
14431                 && tree_expr_nonnegative_warnv_p (op1,
14432                                                   strict_overflow_p));
14433
14434       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14435          both unsigned and at least 2 bits shorter than the result.  */
14436       if (TREE_CODE (type) == INTEGER_TYPE
14437           && TREE_CODE (op0) == NOP_EXPR
14438           && TREE_CODE (op1) == NOP_EXPR)
14439         {
14440           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14441           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14442           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14443               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14444             {
14445               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14446                                        TYPE_PRECISION (inner2)) + 1;
14447               return prec < TYPE_PRECISION (type);
14448             }
14449         }
14450       break;
14451
14452     case MULT_EXPR:
14453       if (FLOAT_TYPE_P (type))
14454         {
14455           /* x * x for floating point x is always non-negative.  */
14456           if (operand_equal_p (op0, op1, 0))
14457             return true;
14458           return (tree_expr_nonnegative_warnv_p (op0,
14459                                                  strict_overflow_p)
14460                   && tree_expr_nonnegative_warnv_p (op1,
14461                                                     strict_overflow_p));
14462         }
14463
14464       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14465          both unsigned and their total bits is shorter than the result.  */
14466       if (TREE_CODE (type) == INTEGER_TYPE
14467           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14468           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14469         {
14470           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14471             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14472             : TREE_TYPE (op0);
14473           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14474             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14475             : TREE_TYPE (op1);
14476
14477           bool unsigned0 = TYPE_UNSIGNED (inner0);
14478           bool unsigned1 = TYPE_UNSIGNED (inner1);
14479
14480           if (TREE_CODE (op0) == INTEGER_CST)
14481             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14482
14483           if (TREE_CODE (op1) == INTEGER_CST)
14484             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14485
14486           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14487               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14488             {
14489               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14490                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14491                 : TYPE_PRECISION (inner0);
14492
14493               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14494                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14495                 : TYPE_PRECISION (inner1);
14496
14497               return precision0 + precision1 < TYPE_PRECISION (type);
14498             }
14499         }
14500       return false;
14501
14502     case BIT_AND_EXPR:
14503     case MAX_EXPR:
14504       return (tree_expr_nonnegative_warnv_p (op0,
14505                                              strict_overflow_p)
14506               || tree_expr_nonnegative_warnv_p (op1,
14507                                                 strict_overflow_p));
14508
14509     case BIT_IOR_EXPR:
14510     case BIT_XOR_EXPR:
14511     case MIN_EXPR:
14512     case RDIV_EXPR:
14513     case TRUNC_DIV_EXPR:
14514     case CEIL_DIV_EXPR:
14515     case FLOOR_DIV_EXPR:
14516     case ROUND_DIV_EXPR:
14517       return (tree_expr_nonnegative_warnv_p (op0,
14518                                              strict_overflow_p)
14519               && tree_expr_nonnegative_warnv_p (op1,
14520                                                 strict_overflow_p));
14521
14522     case TRUNC_MOD_EXPR:
14523     case CEIL_MOD_EXPR:
14524     case FLOOR_MOD_EXPR:
14525     case ROUND_MOD_EXPR:
14526       return tree_expr_nonnegative_warnv_p (op0,
14527                                             strict_overflow_p);
14528     default:
14529       return tree_simple_nonnegative_warnv_p (code, type);
14530     }
14531
14532   /* We don't know sign of `t', so be conservative and return false.  */
14533   return false;
14534 }
14535
14536 /* Return true if T is known to be non-negative.  If the return
14537    value is based on the assumption that signed overflow is undefined,
14538    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14539    *STRICT_OVERFLOW_P.  */
14540
14541 bool
14542 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14543 {
14544   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14545     return true;
14546
14547   switch (TREE_CODE (t))
14548     {
14549     case INTEGER_CST:
14550       return tree_int_cst_sgn (t) >= 0;
14551
14552     case REAL_CST:
14553       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14554
14555     case FIXED_CST:
14556       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14557
14558     case COND_EXPR:
14559       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14560                                              strict_overflow_p)
14561               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14562                                                 strict_overflow_p));
14563     default:
14564       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14565                                                    TREE_TYPE (t));
14566     }
14567   /* We don't know sign of `t', so be conservative and return false.  */
14568   return false;
14569 }
14570
14571 /* Return true if T is known to be non-negative.  If the return
14572    value is based on the assumption that signed overflow is undefined,
14573    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14574    *STRICT_OVERFLOW_P.  */
14575
14576 bool
14577 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14578                                tree arg0, tree arg1, bool *strict_overflow_p)
14579 {
14580   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14581     switch (DECL_FUNCTION_CODE (fndecl))
14582       {
14583         CASE_FLT_FN (BUILT_IN_ACOS):
14584         CASE_FLT_FN (BUILT_IN_ACOSH):
14585         CASE_FLT_FN (BUILT_IN_CABS):
14586         CASE_FLT_FN (BUILT_IN_COSH):
14587         CASE_FLT_FN (BUILT_IN_ERFC):
14588         CASE_FLT_FN (BUILT_IN_EXP):
14589         CASE_FLT_FN (BUILT_IN_EXP10):
14590         CASE_FLT_FN (BUILT_IN_EXP2):
14591         CASE_FLT_FN (BUILT_IN_FABS):
14592         CASE_FLT_FN (BUILT_IN_FDIM):
14593         CASE_FLT_FN (BUILT_IN_HYPOT):
14594         CASE_FLT_FN (BUILT_IN_POW10):
14595         CASE_INT_FN (BUILT_IN_FFS):
14596         CASE_INT_FN (BUILT_IN_PARITY):
14597         CASE_INT_FN (BUILT_IN_POPCOUNT):
14598       case BUILT_IN_BSWAP32:
14599       case BUILT_IN_BSWAP64:
14600         /* Always true.  */
14601         return true;
14602
14603         CASE_FLT_FN (BUILT_IN_SQRT):
14604         /* sqrt(-0.0) is -0.0.  */
14605         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14606           return true;
14607         return tree_expr_nonnegative_warnv_p (arg0,
14608                                               strict_overflow_p);
14609
14610         CASE_FLT_FN (BUILT_IN_ASINH):
14611         CASE_FLT_FN (BUILT_IN_ATAN):
14612         CASE_FLT_FN (BUILT_IN_ATANH):
14613         CASE_FLT_FN (BUILT_IN_CBRT):
14614         CASE_FLT_FN (BUILT_IN_CEIL):
14615         CASE_FLT_FN (BUILT_IN_ERF):
14616         CASE_FLT_FN (BUILT_IN_EXPM1):
14617         CASE_FLT_FN (BUILT_IN_FLOOR):
14618         CASE_FLT_FN (BUILT_IN_FMOD):
14619         CASE_FLT_FN (BUILT_IN_FREXP):
14620         CASE_FLT_FN (BUILT_IN_LCEIL):
14621         CASE_FLT_FN (BUILT_IN_LDEXP):
14622         CASE_FLT_FN (BUILT_IN_LFLOOR):
14623         CASE_FLT_FN (BUILT_IN_LLCEIL):
14624         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14625         CASE_FLT_FN (BUILT_IN_LLRINT):
14626         CASE_FLT_FN (BUILT_IN_LLROUND):
14627         CASE_FLT_FN (BUILT_IN_LRINT):
14628         CASE_FLT_FN (BUILT_IN_LROUND):
14629         CASE_FLT_FN (BUILT_IN_MODF):
14630         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14631         CASE_FLT_FN (BUILT_IN_RINT):
14632         CASE_FLT_FN (BUILT_IN_ROUND):
14633         CASE_FLT_FN (BUILT_IN_SCALB):
14634         CASE_FLT_FN (BUILT_IN_SCALBLN):
14635         CASE_FLT_FN (BUILT_IN_SCALBN):
14636         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14637         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14638         CASE_FLT_FN (BUILT_IN_SINH):
14639         CASE_FLT_FN (BUILT_IN_TANH):
14640         CASE_FLT_FN (BUILT_IN_TRUNC):
14641         /* True if the 1st argument is nonnegative.  */
14642         return tree_expr_nonnegative_warnv_p (arg0,
14643                                               strict_overflow_p);
14644
14645         CASE_FLT_FN (BUILT_IN_FMAX):
14646         /* True if the 1st OR 2nd arguments are nonnegative.  */
14647         return (tree_expr_nonnegative_warnv_p (arg0,
14648                                                strict_overflow_p)
14649                 || (tree_expr_nonnegative_warnv_p (arg1,
14650                                                    strict_overflow_p)));
14651
14652         CASE_FLT_FN (BUILT_IN_FMIN):
14653         /* True if the 1st AND 2nd arguments are nonnegative.  */
14654         return (tree_expr_nonnegative_warnv_p (arg0,
14655                                                strict_overflow_p)
14656                 && (tree_expr_nonnegative_warnv_p (arg1,
14657                                                    strict_overflow_p)));
14658
14659         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14660         /* True if the 2nd argument is nonnegative.  */
14661         return tree_expr_nonnegative_warnv_p (arg1,
14662                                               strict_overflow_p);
14663
14664         CASE_FLT_FN (BUILT_IN_POWI):
14665         /* True if the 1st argument is nonnegative or the second
14666            argument is an even integer.  */
14667         if (TREE_CODE (arg1) == INTEGER_CST
14668             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14669           return true;
14670         return tree_expr_nonnegative_warnv_p (arg0,
14671                                               strict_overflow_p);
14672
14673         CASE_FLT_FN (BUILT_IN_POW):
14674         /* True if the 1st argument is nonnegative or the second
14675            argument is an even integer valued real.  */
14676         if (TREE_CODE (arg1) == REAL_CST)
14677           {
14678             REAL_VALUE_TYPE c;
14679             HOST_WIDE_INT n;
14680
14681             c = TREE_REAL_CST (arg1);
14682             n = real_to_integer (&c);
14683             if ((n & 1) == 0)
14684               {
14685                 REAL_VALUE_TYPE cint;
14686                 real_from_integer (&cint, VOIDmode, n,
14687                                    n < 0 ? -1 : 0, 0);
14688                 if (real_identical (&c, &cint))
14689                   return true;
14690               }
14691           }
14692         return tree_expr_nonnegative_warnv_p (arg0,
14693                                               strict_overflow_p);
14694
14695       default:
14696         break;
14697       }
14698   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14699                                           type);
14700 }
14701
14702 /* Return true if T is known to be non-negative.  If the return
14703    value is based on the assumption that signed overflow is undefined,
14704    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14705    *STRICT_OVERFLOW_P.  */
14706
14707 bool
14708 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14709 {
14710   enum tree_code code = TREE_CODE (t);
14711   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14712     return true;
14713
14714   switch (code)
14715     {
14716     case TARGET_EXPR:
14717       {
14718         tree temp = TARGET_EXPR_SLOT (t);
14719         t = TARGET_EXPR_INITIAL (t);
14720
14721         /* If the initializer is non-void, then it's a normal expression
14722            that will be assigned to the slot.  */
14723         if (!VOID_TYPE_P (t))
14724           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14725
14726         /* Otherwise, the initializer sets the slot in some way.  One common
14727            way is an assignment statement at the end of the initializer.  */
14728         while (1)
14729           {
14730             if (TREE_CODE (t) == BIND_EXPR)
14731               t = expr_last (BIND_EXPR_BODY (t));
14732             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14733                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14734               t = expr_last (TREE_OPERAND (t, 0));
14735             else if (TREE_CODE (t) == STATEMENT_LIST)
14736               t = expr_last (t);
14737             else
14738               break;
14739           }
14740         if (TREE_CODE (t) == MODIFY_EXPR
14741             && TREE_OPERAND (t, 0) == temp)
14742           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14743                                                 strict_overflow_p);
14744
14745         return false;
14746       }
14747
14748     case CALL_EXPR:
14749       {
14750         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14751         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14752
14753         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14754                                               get_callee_fndecl (t),
14755                                               arg0,
14756                                               arg1,
14757                                               strict_overflow_p);
14758       }
14759     case COMPOUND_EXPR:
14760     case MODIFY_EXPR:
14761       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14762                                             strict_overflow_p);
14763     case BIND_EXPR:
14764       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14765                                             strict_overflow_p);
14766     case SAVE_EXPR:
14767       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14768                                             strict_overflow_p);
14769
14770     default:
14771       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14772                                                    TREE_TYPE (t));
14773     }
14774
14775   /* We don't know sign of `t', so be conservative and return false.  */
14776   return false;
14777 }
14778
14779 /* Return true if T is known to be non-negative.  If the return
14780    value is based on the assumption that signed overflow is undefined,
14781    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14782    *STRICT_OVERFLOW_P.  */
14783
14784 bool
14785 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14786 {
14787   enum tree_code code;
14788   if (t == error_mark_node)
14789     return false;
14790
14791   code = TREE_CODE (t);
14792   switch (TREE_CODE_CLASS (code))
14793     {
14794     case tcc_binary:
14795     case tcc_comparison:
14796       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14797                                               TREE_TYPE (t),
14798                                               TREE_OPERAND (t, 0),
14799                                               TREE_OPERAND (t, 1),
14800                                               strict_overflow_p);
14801
14802     case tcc_unary:
14803       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14804                                              TREE_TYPE (t),
14805                                              TREE_OPERAND (t, 0),
14806                                              strict_overflow_p);
14807
14808     case tcc_constant:
14809     case tcc_declaration:
14810     case tcc_reference:
14811       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14812
14813     default:
14814       break;
14815     }
14816
14817   switch (code)
14818     {
14819     case TRUTH_AND_EXPR:
14820     case TRUTH_OR_EXPR:
14821     case TRUTH_XOR_EXPR:
14822       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14823                                               TREE_TYPE (t),
14824                                               TREE_OPERAND (t, 0),
14825                                               TREE_OPERAND (t, 1),
14826                                               strict_overflow_p);
14827     case TRUTH_NOT_EXPR:
14828       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14829                                              TREE_TYPE (t),
14830                                              TREE_OPERAND (t, 0),
14831                                              strict_overflow_p);
14832
14833     case COND_EXPR:
14834     case CONSTRUCTOR:
14835     case OBJ_TYPE_REF:
14836     case ASSERT_EXPR:
14837     case ADDR_EXPR:
14838     case WITH_SIZE_EXPR:
14839     case EXC_PTR_EXPR:
14840     case SSA_NAME:
14841     case FILTER_EXPR:
14842       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14843
14844     default:
14845       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14846     }
14847 }
14848
14849 /* Return true if `t' is known to be non-negative.  Handle warnings
14850    about undefined signed overflow.  */
14851
14852 bool
14853 tree_expr_nonnegative_p (tree t)
14854 {
14855   bool ret, strict_overflow_p;
14856
14857   strict_overflow_p = false;
14858   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14859   if (strict_overflow_p)
14860     fold_overflow_warning (("assuming signed overflow does not occur when "
14861                             "determining that expression is always "
14862                             "non-negative"),
14863                            WARN_STRICT_OVERFLOW_MISC);
14864   return ret;
14865 }
14866
14867
14868 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14869    For floating point we further ensure that T is not denormal.
14870    Similar logic is present in nonzero_address in rtlanal.h.
14871
14872    If the return value is based on the assumption that signed overflow
14873    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14874    change *STRICT_OVERFLOW_P.  */
14875
14876 bool
14877 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14878                                  bool *strict_overflow_p)
14879 {
14880   switch (code)
14881     {
14882     case ABS_EXPR:
14883       return tree_expr_nonzero_warnv_p (op0,
14884                                         strict_overflow_p);
14885
14886     case NOP_EXPR:
14887       {
14888         tree inner_type = TREE_TYPE (op0);
14889         tree outer_type = type;
14890
14891         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14892                 && tree_expr_nonzero_warnv_p (op0,
14893                                               strict_overflow_p));
14894       }
14895       break;
14896
14897     case NON_LVALUE_EXPR:
14898       return tree_expr_nonzero_warnv_p (op0,
14899                                         strict_overflow_p);
14900
14901     default:
14902       break;
14903   }
14904
14905   return false;
14906 }
14907
14908 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14909    For floating point we further ensure that T is not denormal.
14910    Similar logic is present in nonzero_address in rtlanal.h.
14911
14912    If the return value is based on the assumption that signed overflow
14913    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14914    change *STRICT_OVERFLOW_P.  */
14915
14916 bool
14917 tree_binary_nonzero_warnv_p (enum tree_code code,
14918                              tree type,
14919                              tree op0,
14920                              tree op1, bool *strict_overflow_p)
14921 {
14922   bool sub_strict_overflow_p;
14923   switch (code)
14924     {
14925     case POINTER_PLUS_EXPR:
14926     case PLUS_EXPR:
14927       if (TYPE_OVERFLOW_UNDEFINED (type))
14928         {
14929           /* With the presence of negative values it is hard
14930              to say something.  */
14931           sub_strict_overflow_p = false;
14932           if (!tree_expr_nonnegative_warnv_p (op0,
14933                                               &sub_strict_overflow_p)
14934               || !tree_expr_nonnegative_warnv_p (op1,
14935                                                  &sub_strict_overflow_p))
14936             return false;
14937           /* One of operands must be positive and the other non-negative.  */
14938           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14939              overflows, on a twos-complement machine the sum of two
14940              nonnegative numbers can never be zero.  */
14941           return (tree_expr_nonzero_warnv_p (op0,
14942                                              strict_overflow_p)
14943                   || tree_expr_nonzero_warnv_p (op1,
14944                                                 strict_overflow_p));
14945         }
14946       break;
14947
14948     case MULT_EXPR:
14949       if (TYPE_OVERFLOW_UNDEFINED (type))
14950         {
14951           if (tree_expr_nonzero_warnv_p (op0,
14952                                          strict_overflow_p)
14953               && tree_expr_nonzero_warnv_p (op1,
14954                                             strict_overflow_p))
14955             {
14956               *strict_overflow_p = true;
14957               return true;
14958             }
14959         }
14960       break;
14961
14962     case MIN_EXPR:
14963       sub_strict_overflow_p = false;
14964       if (tree_expr_nonzero_warnv_p (op0,
14965                                      &sub_strict_overflow_p)
14966           && tree_expr_nonzero_warnv_p (op1,
14967                                         &sub_strict_overflow_p))
14968         {
14969           if (sub_strict_overflow_p)
14970             *strict_overflow_p = true;
14971         }
14972       break;
14973
14974     case MAX_EXPR:
14975       sub_strict_overflow_p = false;
14976       if (tree_expr_nonzero_warnv_p (op0,
14977                                      &sub_strict_overflow_p))
14978         {
14979           if (sub_strict_overflow_p)
14980             *strict_overflow_p = true;
14981
14982           /* When both operands are nonzero, then MAX must be too.  */
14983           if (tree_expr_nonzero_warnv_p (op1,
14984                                          strict_overflow_p))
14985             return true;
14986
14987           /* MAX where operand 0 is positive is positive.  */
14988           return tree_expr_nonnegative_warnv_p (op0,
14989                                                strict_overflow_p);
14990         }
14991       /* MAX where operand 1 is positive is positive.  */
14992       else if (tree_expr_nonzero_warnv_p (op1,
14993                                           &sub_strict_overflow_p)
14994                && tree_expr_nonnegative_warnv_p (op1,
14995                                                  &sub_strict_overflow_p))
14996         {
14997           if (sub_strict_overflow_p)
14998             *strict_overflow_p = true;
14999           return true;
15000         }
15001       break;
15002
15003     case BIT_IOR_EXPR:
15004       return (tree_expr_nonzero_warnv_p (op1,
15005                                          strict_overflow_p)
15006               || tree_expr_nonzero_warnv_p (op0,
15007                                             strict_overflow_p));
15008
15009     default:
15010       break;
15011   }
15012
15013   return false;
15014 }
15015
15016 /* Return true when T is an address and is known to be nonzero.
15017    For floating point we further ensure that T is not denormal.
15018    Similar logic is present in nonzero_address in rtlanal.h.
15019
15020    If the return value is based on the assumption that signed overflow
15021    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15022    change *STRICT_OVERFLOW_P.  */
15023
15024 bool
15025 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15026 {
15027   bool sub_strict_overflow_p;
15028   switch (TREE_CODE (t))
15029     {
15030     case INTEGER_CST:
15031       return !integer_zerop (t);
15032
15033     case ADDR_EXPR:
15034       {
15035         tree base = get_base_address (TREE_OPERAND (t, 0));
15036
15037         if (!base)
15038           return false;
15039
15040         /* Weak declarations may link to NULL.  Other things may also be NULL
15041            so protect with -fdelete-null-pointer-checks; but not variables
15042            allocated on the stack.  */
15043         if (DECL_P (base)
15044             && (flag_delete_null_pointer_checks
15045                 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15046           return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15047
15048         /* Constants are never weak.  */
15049         if (CONSTANT_CLASS_P (base))
15050           return true;
15051
15052         return false;
15053       }
15054
15055     case COND_EXPR:
15056       sub_strict_overflow_p = false;
15057       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15058                                      &sub_strict_overflow_p)
15059           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15060                                         &sub_strict_overflow_p))
15061         {
15062           if (sub_strict_overflow_p)
15063             *strict_overflow_p = true;
15064           return true;
15065         }
15066       break;
15067
15068     default:
15069       break;
15070     }
15071   return false;
15072 }
15073
15074 /* Return true when T is an address and is known to be nonzero.
15075    For floating point we further ensure that T is not denormal.
15076    Similar logic is present in nonzero_address in rtlanal.h.
15077
15078    If the return value is based on the assumption that signed overflow
15079    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15080    change *STRICT_OVERFLOW_P.  */
15081
15082 bool
15083 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15084 {
15085   tree type = TREE_TYPE (t);
15086   enum tree_code code;
15087
15088   /* Doing something useful for floating point would need more work.  */
15089   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15090     return false;
15091
15092   code = TREE_CODE (t);
15093   switch (TREE_CODE_CLASS (code))
15094     {
15095     case tcc_unary:
15096       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15097                                               strict_overflow_p);
15098     case tcc_binary:
15099     case tcc_comparison:
15100       return tree_binary_nonzero_warnv_p (code, type,
15101                                                TREE_OPERAND (t, 0),
15102                                                TREE_OPERAND (t, 1),
15103                                                strict_overflow_p);
15104     case tcc_constant:
15105     case tcc_declaration:
15106     case tcc_reference:
15107       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15108
15109     default:
15110       break;
15111     }
15112
15113   switch (code)
15114     {
15115     case TRUTH_NOT_EXPR:
15116       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15117                                               strict_overflow_p);
15118
15119     case TRUTH_AND_EXPR:
15120     case TRUTH_OR_EXPR:
15121     case TRUTH_XOR_EXPR:
15122       return tree_binary_nonzero_warnv_p (code, type,
15123                                                TREE_OPERAND (t, 0),
15124                                                TREE_OPERAND (t, 1),
15125                                                strict_overflow_p);
15126
15127     case COND_EXPR:
15128     case CONSTRUCTOR:
15129     case OBJ_TYPE_REF:
15130     case ASSERT_EXPR:
15131     case ADDR_EXPR:
15132     case WITH_SIZE_EXPR:
15133     case EXC_PTR_EXPR:
15134     case SSA_NAME:
15135     case FILTER_EXPR:
15136       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15137
15138     case COMPOUND_EXPR:
15139     case MODIFY_EXPR:
15140     case BIND_EXPR:
15141       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15142                                         strict_overflow_p);
15143
15144     case SAVE_EXPR:
15145       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15146                                         strict_overflow_p);
15147
15148     case CALL_EXPR:
15149       return alloca_call_p (t);
15150
15151     default:
15152       break;
15153     }
15154   return false;
15155 }
15156
15157 /* Return true when T is an address and is known to be nonzero.
15158    Handle warnings about undefined signed overflow.  */
15159
15160 bool
15161 tree_expr_nonzero_p (tree t)
15162 {
15163   bool ret, strict_overflow_p;
15164
15165   strict_overflow_p = false;
15166   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15167   if (strict_overflow_p)
15168     fold_overflow_warning (("assuming signed overflow does not occur when "
15169                             "determining that expression is always "
15170                             "non-zero"),
15171                            WARN_STRICT_OVERFLOW_MISC);
15172   return ret;
15173 }
15174
15175 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15176    attempt to fold the expression to a constant without modifying TYPE,
15177    OP0 or OP1.
15178
15179    If the expression could be simplified to a constant, then return
15180    the constant.  If the expression would not be simplified to a
15181    constant, then return NULL_TREE.  */
15182
15183 tree
15184 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15185 {
15186   tree tem = fold_binary (code, type, op0, op1);
15187   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15188 }
15189
15190 /* Given the components of a unary expression CODE, TYPE and OP0,
15191    attempt to fold the expression to a constant without modifying
15192    TYPE or OP0.
15193
15194    If the expression could be simplified to a constant, then return
15195    the constant.  If the expression would not be simplified to a
15196    constant, then return NULL_TREE.  */
15197
15198 tree
15199 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15200 {
15201   tree tem = fold_unary (code, type, op0);
15202   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15203 }
15204
15205 /* If EXP represents referencing an element in a constant string
15206    (either via pointer arithmetic or array indexing), return the
15207    tree representing the value accessed, otherwise return NULL.  */
15208
15209 tree
15210 fold_read_from_constant_string (tree exp)
15211 {
15212   if ((TREE_CODE (exp) == INDIRECT_REF
15213        || TREE_CODE (exp) == ARRAY_REF)
15214       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15215     {
15216       tree exp1 = TREE_OPERAND (exp, 0);
15217       tree index;
15218       tree string;
15219
15220       if (TREE_CODE (exp) == INDIRECT_REF)
15221         string = string_constant (exp1, &index);
15222       else
15223         {
15224           tree low_bound = array_ref_low_bound (exp);
15225           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15226
15227           /* Optimize the special-case of a zero lower bound.
15228
15229              We convert the low_bound to sizetype to avoid some problems
15230              with constant folding.  (E.g. suppose the lower bound is 1,
15231              and its mode is QI.  Without the conversion,l (ARRAY
15232              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15233              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15234           if (! integer_zerop (low_bound))
15235             index = size_diffop (index, fold_convert (sizetype, low_bound));
15236
15237           string = exp1;
15238         }
15239
15240       if (string
15241           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15242           && TREE_CODE (string) == STRING_CST
15243           && TREE_CODE (index) == INTEGER_CST
15244           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15245           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15246               == MODE_INT)
15247           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15248         return build_int_cst_type (TREE_TYPE (exp),
15249                                    (TREE_STRING_POINTER (string)
15250                                     [TREE_INT_CST_LOW (index)]));
15251     }
15252   return NULL;
15253 }
15254
15255 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15256    an integer constant, real, or fixed-point constant.
15257
15258    TYPE is the type of the result.  */
15259
15260 static tree
15261 fold_negate_const (tree arg0, tree type)
15262 {
15263   tree t = NULL_TREE;
15264
15265   switch (TREE_CODE (arg0))
15266     {
15267     case INTEGER_CST:
15268       {
15269         unsigned HOST_WIDE_INT low;
15270         HOST_WIDE_INT high;
15271         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15272                                    TREE_INT_CST_HIGH (arg0),
15273                                    &low, &high);
15274         t = force_fit_type_double (type, low, high, 1,
15275                                    (overflow | TREE_OVERFLOW (arg0))
15276                                    && !TYPE_UNSIGNED (type));
15277         break;
15278       }
15279
15280     case REAL_CST:
15281       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15282       break;
15283
15284     case FIXED_CST:
15285       {
15286         FIXED_VALUE_TYPE f;
15287         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15288                                             &(TREE_FIXED_CST (arg0)), NULL,
15289                                             TYPE_SATURATING (type));
15290         t = build_fixed (type, f);
15291         /* Propagate overflow flags.  */
15292         if (overflow_p | TREE_OVERFLOW (arg0))
15293           TREE_OVERFLOW (t) = 1;
15294         break;
15295       }
15296
15297     default:
15298       gcc_unreachable ();
15299     }
15300
15301   return t;
15302 }
15303
15304 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15305    an integer constant or real constant.
15306
15307    TYPE is the type of the result.  */
15308
15309 tree
15310 fold_abs_const (tree arg0, tree type)
15311 {
15312   tree t = NULL_TREE;
15313
15314   switch (TREE_CODE (arg0))
15315     {
15316     case INTEGER_CST:
15317       /* If the value is unsigned, then the absolute value is
15318          the same as the ordinary value.  */
15319       if (TYPE_UNSIGNED (type))
15320         t = arg0;
15321       /* Similarly, if the value is non-negative.  */
15322       else if (INT_CST_LT (integer_minus_one_node, arg0))
15323         t = arg0;
15324       /* If the value is negative, then the absolute value is
15325          its negation.  */
15326       else
15327         {
15328           unsigned HOST_WIDE_INT low;
15329           HOST_WIDE_INT high;
15330           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15331                                      TREE_INT_CST_HIGH (arg0),
15332                                      &low, &high);
15333           t = force_fit_type_double (type, low, high, -1,
15334                                      overflow | TREE_OVERFLOW (arg0));
15335         }
15336       break;
15337
15338     case REAL_CST:
15339       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15340         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15341       else
15342         t =  arg0;
15343       break;
15344
15345     default:
15346       gcc_unreachable ();
15347     }
15348
15349   return t;
15350 }
15351
15352 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15353    constant.  TYPE is the type of the result.  */
15354
15355 static tree
15356 fold_not_const (tree arg0, tree type)
15357 {
15358   tree t = NULL_TREE;
15359
15360   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15361
15362   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15363                              ~TREE_INT_CST_HIGH (arg0), 0,
15364                              TREE_OVERFLOW (arg0));
15365
15366   return t;
15367 }
15368
15369 /* Given CODE, a relational operator, the target type, TYPE and two
15370    constant operands OP0 and OP1, return the result of the
15371    relational operation.  If the result is not a compile time
15372    constant, then return NULL_TREE.  */
15373
15374 static tree
15375 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15376 {
15377   int result, invert;
15378
15379   /* From here on, the only cases we handle are when the result is
15380      known to be a constant.  */
15381
15382   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15383     {
15384       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15385       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15386
15387       /* Handle the cases where either operand is a NaN.  */
15388       if (real_isnan (c0) || real_isnan (c1))
15389         {
15390           switch (code)
15391             {
15392             case EQ_EXPR:
15393             case ORDERED_EXPR:
15394               result = 0;
15395               break;
15396
15397             case NE_EXPR:
15398             case UNORDERED_EXPR:
15399             case UNLT_EXPR:
15400             case UNLE_EXPR:
15401             case UNGT_EXPR:
15402             case UNGE_EXPR:
15403             case UNEQ_EXPR:
15404               result = 1;
15405               break;
15406
15407             case LT_EXPR:
15408             case LE_EXPR:
15409             case GT_EXPR:
15410             case GE_EXPR:
15411             case LTGT_EXPR:
15412               if (flag_trapping_math)
15413                 return NULL_TREE;
15414               result = 0;
15415               break;
15416
15417             default:
15418               gcc_unreachable ();
15419             }
15420
15421           return constant_boolean_node (result, type);
15422         }
15423
15424       return constant_boolean_node (real_compare (code, c0, c1), type);
15425     }
15426
15427   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15428     {
15429       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15430       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15431       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15432     }
15433
15434   /* Handle equality/inequality of complex constants.  */
15435   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15436     {
15437       tree rcond = fold_relational_const (code, type,
15438                                           TREE_REALPART (op0),
15439                                           TREE_REALPART (op1));
15440       tree icond = fold_relational_const (code, type,
15441                                           TREE_IMAGPART (op0),
15442                                           TREE_IMAGPART (op1));
15443       if (code == EQ_EXPR)
15444         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15445       else if (code == NE_EXPR)
15446         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15447       else
15448         return NULL_TREE;
15449     }
15450
15451   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15452
15453      To compute GT, swap the arguments and do LT.
15454      To compute GE, do LT and invert the result.
15455      To compute LE, swap the arguments, do LT and invert the result.
15456      To compute NE, do EQ and invert the result.
15457
15458      Therefore, the code below must handle only EQ and LT.  */
15459
15460   if (code == LE_EXPR || code == GT_EXPR)
15461     {
15462       tree tem = op0;
15463       op0 = op1;
15464       op1 = tem;
15465       code = swap_tree_comparison (code);
15466     }
15467
15468   /* Note that it is safe to invert for real values here because we
15469      have already handled the one case that it matters.  */
15470
15471   invert = 0;
15472   if (code == NE_EXPR || code == GE_EXPR)
15473     {
15474       invert = 1;
15475       code = invert_tree_comparison (code, false);
15476     }
15477
15478   /* Compute a result for LT or EQ if args permit;
15479      Otherwise return T.  */
15480   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15481     {
15482       if (code == EQ_EXPR)
15483         result = tree_int_cst_equal (op0, op1);
15484       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15485         result = INT_CST_LT_UNSIGNED (op0, op1);
15486       else
15487         result = INT_CST_LT (op0, op1);
15488     }
15489   else
15490     return NULL_TREE;
15491
15492   if (invert)
15493     result ^= 1;
15494   return constant_boolean_node (result, type);
15495 }
15496
15497 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15498    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15499    itself.  */
15500
15501 tree
15502 fold_build_cleanup_point_expr (tree type, tree expr)
15503 {
15504   /* If the expression does not have side effects then we don't have to wrap
15505      it with a cleanup point expression.  */
15506   if (!TREE_SIDE_EFFECTS (expr))
15507     return expr;
15508
15509   /* If the expression is a return, check to see if the expression inside the
15510      return has no side effects or the right hand side of the modify expression
15511      inside the return. If either don't have side effects set we don't need to
15512      wrap the expression in a cleanup point expression.  Note we don't check the
15513      left hand side of the modify because it should always be a return decl.  */
15514   if (TREE_CODE (expr) == RETURN_EXPR)
15515     {
15516       tree op = TREE_OPERAND (expr, 0);
15517       if (!op || !TREE_SIDE_EFFECTS (op))
15518         return expr;
15519       op = TREE_OPERAND (op, 1);
15520       if (!TREE_SIDE_EFFECTS (op))
15521         return expr;
15522     }
15523   
15524   return build1 (CLEANUP_POINT_EXPR, type, expr);
15525 }
15526
15527 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15528    of an indirection through OP0, or NULL_TREE if no simplification is
15529    possible.  */
15530
15531 tree
15532 fold_indirect_ref_1 (tree type, tree op0)
15533 {
15534   tree sub = op0;
15535   tree subtype;
15536
15537   STRIP_NOPS (sub);
15538   subtype = TREE_TYPE (sub);
15539   if (!POINTER_TYPE_P (subtype))
15540     return NULL_TREE;
15541
15542   if (TREE_CODE (sub) == ADDR_EXPR)
15543     {
15544       tree op = TREE_OPERAND (sub, 0);
15545       tree optype = TREE_TYPE (op);
15546       /* *&CONST_DECL -> to the value of the const decl.  */
15547       if (TREE_CODE (op) == CONST_DECL)
15548         return DECL_INITIAL (op);
15549       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15550       if (type == optype)
15551         {
15552           tree fop = fold_read_from_constant_string (op);
15553           if (fop)
15554             return fop;
15555           else
15556             return op;
15557         }
15558       /* *(foo *)&fooarray => fooarray[0] */
15559       else if (TREE_CODE (optype) == ARRAY_TYPE
15560                && type == TREE_TYPE (optype))
15561         {
15562           tree type_domain = TYPE_DOMAIN (optype);
15563           tree min_val = size_zero_node;
15564           if (type_domain && TYPE_MIN_VALUE (type_domain))
15565             min_val = TYPE_MIN_VALUE (type_domain);
15566           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15567         }
15568       /* *(foo *)&complexfoo => __real__ complexfoo */
15569       else if (TREE_CODE (optype) == COMPLEX_TYPE
15570                && type == TREE_TYPE (optype))
15571         return fold_build1 (REALPART_EXPR, type, op);
15572       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15573       else if (TREE_CODE (optype) == VECTOR_TYPE
15574                && type == TREE_TYPE (optype))
15575         {
15576           tree part_width = TYPE_SIZE (type);
15577           tree index = bitsize_int (0);
15578           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15579         }
15580     }
15581
15582   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15583   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15584       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15585     { 
15586       tree op00 = TREE_OPERAND (sub, 0);
15587       tree op01 = TREE_OPERAND (sub, 1);
15588       tree op00type;
15589       
15590       STRIP_NOPS (op00);
15591       op00type = TREE_TYPE (op00);
15592       if (TREE_CODE (op00) == ADDR_EXPR
15593           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15594           && type == TREE_TYPE (TREE_TYPE (op00type)))
15595         { 
15596           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15597           tree part_width = TYPE_SIZE (type);
15598           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15599           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15600           tree index = bitsize_int (indexi);
15601
15602           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15603             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15604                                 part_width, index);
15605         
15606         }
15607     }
15608
15609
15610   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15611   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15612       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15613     {
15614       tree op00 = TREE_OPERAND (sub, 0);
15615       tree op01 = TREE_OPERAND (sub, 1);
15616       tree op00type;
15617
15618       STRIP_NOPS (op00);
15619       op00type = TREE_TYPE (op00);
15620       if (TREE_CODE (op00) == ADDR_EXPR
15621           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15622           && type == TREE_TYPE (TREE_TYPE (op00type)))
15623         {
15624           tree size = TYPE_SIZE_UNIT (type);
15625           if (tree_int_cst_equal (size, op01))
15626             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15627         }
15628     }
15629   
15630   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15631   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15632       && type == TREE_TYPE (TREE_TYPE (subtype)))
15633     {
15634       tree type_domain;
15635       tree min_val = size_zero_node;
15636       sub = build_fold_indirect_ref (sub);
15637       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15638       if (type_domain && TYPE_MIN_VALUE (type_domain))
15639         min_val = TYPE_MIN_VALUE (type_domain);
15640       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15641     }
15642
15643   return NULL_TREE;
15644 }
15645
15646 /* Builds an expression for an indirection through T, simplifying some
15647    cases.  */
15648
15649 tree
15650 build_fold_indirect_ref (tree t)
15651 {
15652   tree type = TREE_TYPE (TREE_TYPE (t));
15653   tree sub = fold_indirect_ref_1 (type, t);
15654
15655   if (sub)
15656     return sub;
15657   else
15658     return build1 (INDIRECT_REF, type, t);
15659 }
15660
15661 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15662
15663 tree
15664 fold_indirect_ref (tree t)
15665 {
15666   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15667
15668   if (sub)
15669     return sub;
15670   else
15671     return t;
15672 }
15673
15674 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15675    whose result is ignored.  The type of the returned tree need not be
15676    the same as the original expression.  */
15677
15678 tree
15679 fold_ignored_result (tree t)
15680 {
15681   if (!TREE_SIDE_EFFECTS (t))
15682     return integer_zero_node;
15683
15684   for (;;)
15685     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15686       {
15687       case tcc_unary:
15688         t = TREE_OPERAND (t, 0);
15689         break;
15690
15691       case tcc_binary:
15692       case tcc_comparison:
15693         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15694           t = TREE_OPERAND (t, 0);
15695         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15696           t = TREE_OPERAND (t, 1);
15697         else
15698           return t;
15699         break;
15700
15701       case tcc_expression:
15702         switch (TREE_CODE (t))
15703           {
15704           case COMPOUND_EXPR:
15705             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15706               return t;
15707             t = TREE_OPERAND (t, 0);
15708             break;
15709
15710           case COND_EXPR:
15711             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15712                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15713               return t;
15714             t = TREE_OPERAND (t, 0);
15715             break;
15716
15717           default:
15718             return t;
15719           }
15720         break;
15721
15722       default:
15723         return t;
15724       }
15725 }
15726
15727 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15728    This can only be applied to objects of a sizetype.  */
15729
15730 tree
15731 round_up (tree value, int divisor)
15732 {
15733   tree div = NULL_TREE;
15734
15735   gcc_assert (divisor > 0);
15736   if (divisor == 1)
15737     return value;
15738
15739   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15740      have to do anything.  Only do this when we are not given a const,
15741      because in that case, this check is more expensive than just
15742      doing it.  */
15743   if (TREE_CODE (value) != INTEGER_CST)
15744     {
15745       div = build_int_cst (TREE_TYPE (value), divisor);
15746
15747       if (multiple_of_p (TREE_TYPE (value), value, div))
15748         return value;
15749     }
15750
15751   /* If divisor is a power of two, simplify this to bit manipulation.  */
15752   if (divisor == (divisor & -divisor))
15753     {
15754       if (TREE_CODE (value) == INTEGER_CST)
15755         {
15756           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15757           unsigned HOST_WIDE_INT high;
15758           bool overflow_p;
15759
15760           if ((low & (divisor - 1)) == 0)
15761             return value;
15762
15763           overflow_p = TREE_OVERFLOW (value);
15764           high = TREE_INT_CST_HIGH (value);
15765           low &= ~(divisor - 1);
15766           low += divisor;
15767           if (low == 0)
15768             {
15769               high++;
15770               if (high == 0)
15771                 overflow_p = true;
15772             }
15773
15774           return force_fit_type_double (TREE_TYPE (value), low, high,
15775                                         -1, overflow_p);
15776         }
15777       else
15778         {
15779           tree t;
15780
15781           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15782           value = size_binop (PLUS_EXPR, value, t);
15783           t = build_int_cst (TREE_TYPE (value), -divisor);
15784           value = size_binop (BIT_AND_EXPR, value, t);
15785         }
15786     }
15787   else
15788     {
15789       if (!div)
15790         div = build_int_cst (TREE_TYPE (value), divisor);
15791       value = size_binop (CEIL_DIV_EXPR, value, div);
15792       value = size_binop (MULT_EXPR, value, div);
15793     }
15794
15795   return value;
15796 }
15797
15798 /* Likewise, but round down.  */
15799
15800 tree
15801 round_down (tree value, int divisor)
15802 {
15803   tree div = NULL_TREE;
15804
15805   gcc_assert (divisor > 0);
15806   if (divisor == 1)
15807     return value;
15808
15809   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15810      have to do anything.  Only do this when we are not given a const,
15811      because in that case, this check is more expensive than just
15812      doing it.  */
15813   if (TREE_CODE (value) != INTEGER_CST)
15814     {
15815       div = build_int_cst (TREE_TYPE (value), divisor);
15816
15817       if (multiple_of_p (TREE_TYPE (value), value, div))
15818         return value;
15819     }
15820
15821   /* If divisor is a power of two, simplify this to bit manipulation.  */
15822   if (divisor == (divisor & -divisor))
15823     {
15824       tree t;
15825
15826       t = build_int_cst (TREE_TYPE (value), -divisor);
15827       value = size_binop (BIT_AND_EXPR, value, t);
15828     }
15829   else
15830     {
15831       if (!div)
15832         div = build_int_cst (TREE_TYPE (value), divisor);
15833       value = size_binop (FLOOR_DIV_EXPR, value, div);
15834       value = size_binop (MULT_EXPR, value, div);
15835     }
15836
15837   return value;
15838 }
15839
15840 /* Returns the pointer to the base of the object addressed by EXP and
15841    extracts the information about the offset of the access, storing it
15842    to PBITPOS and POFFSET.  */
15843
15844 static tree
15845 split_address_to_core_and_offset (tree exp,
15846                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15847 {
15848   tree core;
15849   enum machine_mode mode;
15850   int unsignedp, volatilep;
15851   HOST_WIDE_INT bitsize;
15852
15853   if (TREE_CODE (exp) == ADDR_EXPR)
15854     {
15855       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15856                                   poffset, &mode, &unsignedp, &volatilep,
15857                                   false);
15858       core = build_fold_addr_expr (core);
15859     }
15860   else
15861     {
15862       core = exp;
15863       *pbitpos = 0;
15864       *poffset = NULL_TREE;
15865     }
15866
15867   return core;
15868 }
15869
15870 /* Returns true if addresses of E1 and E2 differ by a constant, false
15871    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15872
15873 bool
15874 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15875 {
15876   tree core1, core2;
15877   HOST_WIDE_INT bitpos1, bitpos2;
15878   tree toffset1, toffset2, tdiff, type;
15879
15880   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15881   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15882
15883   if (bitpos1 % BITS_PER_UNIT != 0
15884       || bitpos2 % BITS_PER_UNIT != 0
15885       || !operand_equal_p (core1, core2, 0))
15886     return false;
15887
15888   if (toffset1 && toffset2)
15889     {
15890       type = TREE_TYPE (toffset1);
15891       if (type != TREE_TYPE (toffset2))
15892         toffset2 = fold_convert (type, toffset2);
15893
15894       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15895       if (!cst_and_fits_in_hwi (tdiff))
15896         return false;
15897
15898       *diff = int_cst_value (tdiff);
15899     }
15900   else if (toffset1 || toffset2)
15901     {
15902       /* If only one of the offsets is non-constant, the difference cannot
15903          be a constant.  */
15904       return false;
15905     }
15906   else
15907     *diff = 0;
15908
15909   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15910   return true;
15911 }
15912
15913 /* Simplify the floating point expression EXP when the sign of the
15914    result is not significant.  Return NULL_TREE if no simplification
15915    is possible.  */
15916
15917 tree
15918 fold_strip_sign_ops (tree exp)
15919 {
15920   tree arg0, arg1;
15921
15922   switch (TREE_CODE (exp))
15923     {
15924     case ABS_EXPR:
15925     case NEGATE_EXPR:
15926       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15927       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15928
15929     case MULT_EXPR:
15930     case RDIV_EXPR:
15931       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15932         return NULL_TREE;
15933       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15934       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15935       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15936         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15937                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15938                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15939       break;
15940
15941     case COMPOUND_EXPR:
15942       arg0 = TREE_OPERAND (exp, 0);
15943       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15944       if (arg1)
15945         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15946       break;
15947       
15948     case COND_EXPR:
15949       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15950       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15951       if (arg0 || arg1)
15952         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15953                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15954                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15955       break;
15956       
15957     case CALL_EXPR:
15958       {
15959         const enum built_in_function fcode = builtin_mathfn_code (exp);
15960         switch (fcode)
15961         {
15962         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15963           /* Strip copysign function call, return the 1st argument. */
15964           arg0 = CALL_EXPR_ARG (exp, 0);
15965           arg1 = CALL_EXPR_ARG (exp, 1);
15966           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15967
15968         default:
15969           /* Strip sign ops from the argument of "odd" math functions.  */
15970           if (negate_mathfn_p (fcode))
15971             {
15972               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15973               if (arg0)
15974                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15975             }
15976           break;
15977         }
15978       }
15979       break;
15980
15981     default:
15982       break;
15983     }
15984   return NULL_TREE;
15985 }