OSDN Git Service

2009-03-31 Richard Guenther <rguenther@suse.de>
[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 static tree make_range (tree, int *, tree *, tree *, bool *);
123 static tree build_range_check (tree, tree, int, tree, tree);
124 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
125                          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
145
146 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
147    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
148    and SUM1.  Then this yields nonzero if overflow occurred during the
149    addition.
150
151    Overflow occurs if A and B have the same sign, but A and SUM differ in
152    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
153    sign.  */
154 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
155 \f
156 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
157    We do that by representing the two-word integer in 4 words, with only
158    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
159    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
160
161 #define LOWPART(x) \
162   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
163 #define HIGHPART(x) \
164   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
165 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
166
167 /* Unpack a two-word integer into 4 words.
168    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
169    WORDS points to the array of HOST_WIDE_INTs.  */
170
171 static void
172 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
173 {
174   words[0] = LOWPART (low);
175   words[1] = HIGHPART (low);
176   words[2] = LOWPART (hi);
177   words[3] = HIGHPART (hi);
178 }
179
180 /* Pack an array of 4 words into a two-word integer.
181    WORDS points to the array of words.
182    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
183
184 static void
185 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
186         HOST_WIDE_INT *hi)
187 {
188   *low = words[0] + words[1] * BASE;
189   *hi = words[2] + words[3] * BASE;
190 }
191 \f
192 /* Force the double-word integer L1, H1 to be within the range of the
193    integer type TYPE.  Stores the properly truncated and sign-extended
194    double-word integer in *LV, *HV.  Returns true if the operation
195    overflows, that is, argument and result are different.  */
196
197 int
198 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
199                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
200 {
201   unsigned HOST_WIDE_INT low0 = l1;
202   HOST_WIDE_INT high0 = h1;
203   unsigned int prec;
204   int sign_extended_type;
205
206   if (POINTER_TYPE_P (type)
207       || TREE_CODE (type) == OFFSET_TYPE)
208     prec = POINTER_SIZE;
209   else
210     prec = TYPE_PRECISION (type);
211
212   /* Size types *are* sign extended.  */
213   sign_extended_type = (!TYPE_UNSIGNED (type)
214                         || (TREE_CODE (type) == INTEGER_TYPE
215                             && TYPE_IS_SIZETYPE (type)));
216
217   /* First clear all bits that are beyond the type's precision.  */
218   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
219     ;
220   else if (prec > HOST_BITS_PER_WIDE_INT)
221     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
222   else
223     {
224       h1 = 0;
225       if (prec < HOST_BITS_PER_WIDE_INT)
226         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
227     }
228
229   /* Then do sign extension if necessary.  */
230   if (!sign_extended_type)
231     /* No sign extension */;
232   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
233     /* Correct width already.  */;
234   else if (prec > HOST_BITS_PER_WIDE_INT)
235     {
236       /* Sign extend top half? */
237       if (h1 & ((unsigned HOST_WIDE_INT)1
238                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
239         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
240     }
241   else if (prec == HOST_BITS_PER_WIDE_INT)
242     {
243       if ((HOST_WIDE_INT)l1 < 0)
244         h1 = -1;
245     }
246   else
247     {
248       /* Sign extend bottom half? */
249       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
250         {
251           h1 = -1;
252           l1 |= (HOST_WIDE_INT)(-1) << prec;
253         }
254     }
255
256   *lv = l1;
257   *hv = h1;
258
259   /* If the value didn't fit, signal overflow.  */
260   return l1 != low0 || h1 != high0;
261 }
262
263 /* We force the double-int HIGH:LOW to the range of the type TYPE by
264    sign or zero extending it.
265    OVERFLOWABLE indicates if we are interested
266    in overflow of the value, when >0 we are only interested in signed
267    overflow, for <0 we are interested in any overflow.  OVERFLOWED
268    indicates whether overflow has already occurred.  CONST_OVERFLOWED
269    indicates whether constant overflow has already occurred.  We force
270    T's value to be within range of T's type (by setting to 0 or 1 all
271    the bits outside the type's range).  We set TREE_OVERFLOWED if,
272         OVERFLOWED is nonzero,
273         or OVERFLOWABLE is >0 and signed overflow occurs
274         or OVERFLOWABLE is <0 and any overflow occurs
275    We return a new tree node for the extended double-int.  The node
276    is shared if no overflow flags are set.  */
277
278 tree
279 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
280                        HOST_WIDE_INT high, int overflowable,
281                        bool overflowed)
282 {
283   int sign_extended_type;
284   bool overflow;
285
286   /* Size types *are* sign extended.  */
287   sign_extended_type = (!TYPE_UNSIGNED (type)
288                         || (TREE_CODE (type) == INTEGER_TYPE
289                             && TYPE_IS_SIZETYPE (type)));
290
291   overflow = fit_double_type (low, high, &low, &high, type);
292
293   /* If we need to set overflow flags, return a new unshared node.  */
294   if (overflowed || overflow)
295     {
296       if (overflowed
297           || overflowable < 0
298           || (overflowable > 0 && sign_extended_type))
299         {
300           tree t = make_node (INTEGER_CST);
301           TREE_INT_CST_LOW (t) = low;
302           TREE_INT_CST_HIGH (t) = high;
303           TREE_TYPE (t) = type;
304           TREE_OVERFLOW (t) = 1;
305           return t;
306         }
307     }
308
309   /* Else build a shared node.  */
310   return build_int_cst_wide (type, low, high);
311 }
312 \f
313 /* Add two doubleword integers with doubleword result.
314    Return nonzero if the operation overflows according to UNSIGNED_P.
315    Each argument is given as two `HOST_WIDE_INT' pieces.
316    One argument is L1 and H1; the other, L2 and H2.
317    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
318
319 int
320 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
321                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
322                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
323                       bool unsigned_p)
324 {
325   unsigned HOST_WIDE_INT l;
326   HOST_WIDE_INT h;
327
328   l = l1 + l2;
329   h = h1 + h2 + (l < l1);
330
331   *lv = l;
332   *hv = h;
333
334   if (unsigned_p)
335     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
336   else
337     return OVERFLOW_SUM_SIGN (h1, h2, h);
338 }
339
340 /* Negate a doubleword integer with doubleword result.
341    Return nonzero if the operation overflows, assuming it's signed.
342    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
343    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
344
345 int
346 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
347             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
348 {
349   if (l1 == 0)
350     {
351       *lv = 0;
352       *hv = - h1;
353       return (*hv & h1) < 0;
354     }
355   else
356     {
357       *lv = -l1;
358       *hv = ~h1;
359       return 0;
360     }
361 }
362 \f
363 /* Multiply two doubleword integers with doubleword result.
364    Return nonzero if the operation overflows according to UNSIGNED_P.
365    Each argument is given as two `HOST_WIDE_INT' pieces.
366    One argument is L1 and H1; the other, L2 and H2.
367    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
368
369 int
370 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
371                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
372                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
373                       bool unsigned_p)
374 {
375   HOST_WIDE_INT arg1[4];
376   HOST_WIDE_INT arg2[4];
377   HOST_WIDE_INT prod[4 * 2];
378   unsigned HOST_WIDE_INT carry;
379   int i, j, k;
380   unsigned HOST_WIDE_INT toplow, neglow;
381   HOST_WIDE_INT tophigh, neghigh;
382
383   encode (arg1, l1, h1);
384   encode (arg2, l2, h2);
385
386   memset (prod, 0, sizeof prod);
387
388   for (i = 0; i < 4; i++)
389     {
390       carry = 0;
391       for (j = 0; j < 4; j++)
392         {
393           k = i + j;
394           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
395           carry += arg1[i] * arg2[j];
396           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
397           carry += prod[k];
398           prod[k] = LOWPART (carry);
399           carry = HIGHPART (carry);
400         }
401       prod[i + 4] = carry;
402     }
403
404   decode (prod, lv, hv);
405   decode (prod + 4, &toplow, &tophigh);
406
407   /* Unsigned overflow is immediate.  */
408   if (unsigned_p)
409     return (toplow | tophigh) != 0;
410
411   /* Check for signed overflow by calculating the signed representation of the
412      top half of the result; it should agree with the low half's sign bit.  */
413   if (h1 < 0)
414     {
415       neg_double (l2, h2, &neglow, &neghigh);
416       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
417     }
418   if (h2 < 0)
419     {
420       neg_double (l1, h1, &neglow, &neghigh);
421       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
422     }
423   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
424 }
425 \f
426 /* Shift the doubleword integer in L1, H1 left by COUNT places
427    keeping only PREC bits of result.
428    Shift right if COUNT is negative.
429    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
430    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
431
432 void
433 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
434                HOST_WIDE_INT count, unsigned int prec,
435                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
436 {
437   unsigned HOST_WIDE_INT signmask;
438
439   if (count < 0)
440     {
441       rshift_double (l1, h1, -count, prec, lv, hv, arith);
442       return;
443     }
444
445   if (SHIFT_COUNT_TRUNCATED)
446     count %= prec;
447
448   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
449     {
450       /* Shifting by the host word size is undefined according to the
451          ANSI standard, so we must handle this as a special case.  */
452       *hv = 0;
453       *lv = 0;
454     }
455   else if (count >= HOST_BITS_PER_WIDE_INT)
456     {
457       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
458       *lv = 0;
459     }
460   else
461     {
462       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
463              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
464       *lv = l1 << count;
465     }
466
467   /* Sign extend all bits that are beyond the precision.  */
468
469   signmask = -((prec > HOST_BITS_PER_WIDE_INT
470                 ? ((unsigned HOST_WIDE_INT) *hv
471                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
472                 : (*lv >> (prec - 1))) & 1);
473
474   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
475     ;
476   else if (prec >= HOST_BITS_PER_WIDE_INT)
477     {
478       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
479       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
480     }
481   else
482     {
483       *hv = signmask;
484       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
485       *lv |= signmask << prec;
486     }
487 }
488
489 /* Shift the doubleword integer in L1, H1 right by COUNT places
490    keeping only PREC bits of result.  COUNT must be positive.
491    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
492    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
493
494 void
495 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
496                HOST_WIDE_INT count, unsigned int prec,
497                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
498                int arith)
499 {
500   unsigned HOST_WIDE_INT signmask;
501
502   signmask = (arith
503               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
504               : 0);
505
506   if (SHIFT_COUNT_TRUNCATED)
507     count %= prec;
508
509   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
510     {
511       /* Shifting by the host word size is undefined according to the
512          ANSI standard, so we must handle this as a special case.  */
513       *hv = 0;
514       *lv = 0;
515     }
516   else if (count >= HOST_BITS_PER_WIDE_INT)
517     {
518       *hv = 0;
519       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
520     }
521   else
522     {
523       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
524       *lv = ((l1 >> count)
525              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
526     }
527
528   /* Zero / sign extend all bits that are beyond the precision.  */
529
530   if (count >= (HOST_WIDE_INT)prec)
531     {
532       *hv = signmask;
533       *lv = signmask;
534     }
535   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
536     ;
537   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
538     {
539       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
540       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
541     }
542   else
543     {
544       *hv = signmask;
545       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
546       *lv |= signmask << (prec - count);
547     }
548 }
549 \f
550 /* Rotate the doubleword integer in L1, H1 left by COUNT places
551    keeping only PREC bits of result.
552    Rotate right if COUNT is negative.
553    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
554
555 void
556 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
557                 HOST_WIDE_INT count, unsigned int prec,
558                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
559 {
560   unsigned HOST_WIDE_INT s1l, s2l;
561   HOST_WIDE_INT s1h, s2h;
562
563   count %= prec;
564   if (count < 0)
565     count += prec;
566
567   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
568   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
569   *lv = s1l | s2l;
570   *hv = s1h | s2h;
571 }
572
573 /* Rotate the doubleword integer in L1, H1 left by COUNT places
574    keeping only PREC bits of result.  COUNT must be positive.
575    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
576
577 void
578 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
579                 HOST_WIDE_INT count, unsigned int prec,
580                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
581 {
582   unsigned HOST_WIDE_INT s1l, s2l;
583   HOST_WIDE_INT s1h, s2h;
584
585   count %= prec;
586   if (count < 0)
587     count += prec;
588
589   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
590   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
591   *lv = s1l | s2l;
592   *hv = s1h | s2h;
593 }
594 \f
595 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
596    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
597    CODE is a tree code for a kind of division, one of
598    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
599    or EXACT_DIV_EXPR
600    It controls how the quotient is rounded to an integer.
601    Return nonzero if the operation overflows.
602    UNS nonzero says do unsigned division.  */
603
604 int
605 div_and_round_double (enum tree_code code, int uns,
606                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
607                       HOST_WIDE_INT hnum_orig,
608                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
609                       HOST_WIDE_INT hden_orig,
610                       unsigned HOST_WIDE_INT *lquo,
611                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
612                       HOST_WIDE_INT *hrem)
613 {
614   int quo_neg = 0;
615   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
616   HOST_WIDE_INT den[4], quo[4];
617   int i, j;
618   unsigned HOST_WIDE_INT work;
619   unsigned HOST_WIDE_INT carry = 0;
620   unsigned HOST_WIDE_INT lnum = lnum_orig;
621   HOST_WIDE_INT hnum = hnum_orig;
622   unsigned HOST_WIDE_INT lden = lden_orig;
623   HOST_WIDE_INT hden = hden_orig;
624   int overflow = 0;
625
626   if (hden == 0 && lden == 0)
627     overflow = 1, lden = 1;
628
629   /* Calculate quotient sign and convert operands to unsigned.  */
630   if (!uns)
631     {
632       if (hnum < 0)
633         {
634           quo_neg = ~ quo_neg;
635           /* (minimum integer) / (-1) is the only overflow case.  */
636           if (neg_double (lnum, hnum, &lnum, &hnum)
637               && ((HOST_WIDE_INT) lden & hden) == -1)
638             overflow = 1;
639         }
640       if (hden < 0)
641         {
642           quo_neg = ~ quo_neg;
643           neg_double (lden, hden, &lden, &hden);
644         }
645     }
646
647   if (hnum == 0 && hden == 0)
648     {                           /* single precision */
649       *hquo = *hrem = 0;
650       /* This unsigned division rounds toward zero.  */
651       *lquo = lnum / lden;
652       goto finish_up;
653     }
654
655   if (hnum == 0)
656     {                           /* trivial case: dividend < divisor */
657       /* hden != 0 already checked.  */
658       *hquo = *lquo = 0;
659       *hrem = hnum;
660       *lrem = lnum;
661       goto finish_up;
662     }
663
664   memset (quo, 0, sizeof quo);
665
666   memset (num, 0, sizeof num);  /* to zero 9th element */
667   memset (den, 0, sizeof den);
668
669   encode (num, lnum, hnum);
670   encode (den, lden, hden);
671
672   /* Special code for when the divisor < BASE.  */
673   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
674     {
675       /* hnum != 0 already checked.  */
676       for (i = 4 - 1; i >= 0; i--)
677         {
678           work = num[i] + carry * BASE;
679           quo[i] = work / lden;
680           carry = work % lden;
681         }
682     }
683   else
684     {
685       /* Full double precision division,
686          with thanks to Don Knuth's "Seminumerical Algorithms".  */
687       int num_hi_sig, den_hi_sig;
688       unsigned HOST_WIDE_INT quo_est, scale;
689
690       /* Find the highest nonzero divisor digit.  */
691       for (i = 4 - 1;; i--)
692         if (den[i] != 0)
693           {
694             den_hi_sig = i;
695             break;
696           }
697
698       /* Insure that the first digit of the divisor is at least BASE/2.
699          This is required by the quotient digit estimation algorithm.  */
700
701       scale = BASE / (den[den_hi_sig] + 1);
702       if (scale > 1)
703         {               /* scale divisor and dividend */
704           carry = 0;
705           for (i = 0; i <= 4 - 1; i++)
706             {
707               work = (num[i] * scale) + carry;
708               num[i] = LOWPART (work);
709               carry = HIGHPART (work);
710             }
711
712           num[4] = carry;
713           carry = 0;
714           for (i = 0; i <= 4 - 1; i++)
715             {
716               work = (den[i] * scale) + carry;
717               den[i] = LOWPART (work);
718               carry = HIGHPART (work);
719               if (den[i] != 0) den_hi_sig = i;
720             }
721         }
722
723       num_hi_sig = 4;
724
725       /* Main loop */
726       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
727         {
728           /* Guess the next quotient digit, quo_est, by dividing the first
729              two remaining dividend digits by the high order quotient digit.
730              quo_est is never low and is at most 2 high.  */
731           unsigned HOST_WIDE_INT tmp;
732
733           num_hi_sig = i + den_hi_sig + 1;
734           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
735           if (num[num_hi_sig] != den[den_hi_sig])
736             quo_est = work / den[den_hi_sig];
737           else
738             quo_est = BASE - 1;
739
740           /* Refine quo_est so it's usually correct, and at most one high.  */
741           tmp = work - quo_est * den[den_hi_sig];
742           if (tmp < BASE
743               && (den[den_hi_sig - 1] * quo_est
744                   > (tmp * BASE + num[num_hi_sig - 2])))
745             quo_est--;
746
747           /* Try QUO_EST as the quotient digit, by multiplying the
748              divisor by QUO_EST and subtracting from the remaining dividend.
749              Keep in mind that QUO_EST is the I - 1st digit.  */
750
751           carry = 0;
752           for (j = 0; j <= den_hi_sig; j++)
753             {
754               work = quo_est * den[j] + carry;
755               carry = HIGHPART (work);
756               work = num[i + j] - LOWPART (work);
757               num[i + j] = LOWPART (work);
758               carry += HIGHPART (work) != 0;
759             }
760
761           /* If quo_est was high by one, then num[i] went negative and
762              we need to correct things.  */
763           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
764             {
765               quo_est--;
766               carry = 0;                /* add divisor back in */
767               for (j = 0; j <= den_hi_sig; j++)
768                 {
769                   work = num[i + j] + den[j] + carry;
770                   carry = HIGHPART (work);
771                   num[i + j] = LOWPART (work);
772                 }
773
774               num [num_hi_sig] += carry;
775             }
776
777           /* Store the quotient digit.  */
778           quo[i] = quo_est;
779         }
780     }
781
782   decode (quo, lquo, hquo);
783
784  finish_up:
785   /* If result is negative, make it so.  */
786   if (quo_neg)
787     neg_double (*lquo, *hquo, lquo, hquo);
788
789   /* Compute trial remainder:  rem = num - (quo * den)  */
790   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
791   neg_double (*lrem, *hrem, lrem, hrem);
792   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
793
794   switch (code)
795     {
796     case TRUNC_DIV_EXPR:
797     case TRUNC_MOD_EXPR:        /* round toward zero */
798     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
799       return overflow;
800
801     case FLOOR_DIV_EXPR:
802     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
803       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
804         {
805           /* quo = quo - 1;  */
806           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
807                       lquo, hquo);
808         }
809       else
810         return overflow;
811       break;
812
813     case CEIL_DIV_EXPR:
814     case CEIL_MOD_EXPR:         /* round toward positive infinity */
815       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
816         {
817           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
818                       lquo, hquo);
819         }
820       else
821         return overflow;
822       break;
823
824     case ROUND_DIV_EXPR:
825     case ROUND_MOD_EXPR:        /* round to closest integer */
826       {
827         unsigned HOST_WIDE_INT labs_rem = *lrem;
828         HOST_WIDE_INT habs_rem = *hrem;
829         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
830         HOST_WIDE_INT habs_den = hden, htwice;
831
832         /* Get absolute values.  */
833         if (*hrem < 0)
834           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
835         if (hden < 0)
836           neg_double (lden, hden, &labs_den, &habs_den);
837
838         /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient.  */
839         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
840                     labs_rem, habs_rem, &ltwice, &htwice);
841
842         if (((unsigned HOST_WIDE_INT) habs_den
843              < (unsigned HOST_WIDE_INT) htwice)
844             || (((unsigned HOST_WIDE_INT) habs_den
845                  == (unsigned HOST_WIDE_INT) htwice)
846                 && (labs_den <= ltwice)))
847           {
848             if (*hquo < 0)
849               /* quo = quo - 1;  */
850               add_double (*lquo, *hquo,
851                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
852             else
853               /* quo = quo + 1; */
854               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
855                           lquo, hquo);
856           }
857         else
858           return overflow;
859       }
860       break;
861
862     default:
863       gcc_unreachable ();
864     }
865
866   /* Compute true remainder:  rem = num - (quo * den)  */
867   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
868   neg_double (*lrem, *hrem, lrem, hrem);
869   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
870   return overflow;
871 }
872
873 /* If ARG2 divides ARG1 with zero remainder, carries out the division
874    of type CODE and returns the quotient.
875    Otherwise returns NULL_TREE.  */
876
877 tree
878 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
879 {
880   unsigned HOST_WIDE_INT int1l, int2l;
881   HOST_WIDE_INT int1h, int2h;
882   unsigned HOST_WIDE_INT quol, reml;
883   HOST_WIDE_INT quoh, remh;
884   tree type = TREE_TYPE (arg1);
885   int uns = TYPE_UNSIGNED (type);
886
887   int1l = TREE_INT_CST_LOW (arg1);
888   int1h = TREE_INT_CST_HIGH (arg1);
889   /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
890      &obj[some_exotic_number].  */
891   if (POINTER_TYPE_P (type))
892     {
893       uns = false;
894       type = signed_type_for (type);
895       fit_double_type (int1l, int1h, &int1l, &int1h,
896                        type);
897     }
898   else
899     fit_double_type (int1l, int1h, &int1l, &int1h, type);
900   int2l = TREE_INT_CST_LOW (arg2);
901   int2h = TREE_INT_CST_HIGH (arg2);
902
903   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
904                         &quol, &quoh, &reml, &remh);
905   if (remh != 0 || reml != 0)
906     return NULL_TREE;
907
908   return build_int_cst_wide (type, quol, quoh);
909 }
910 \f
911 /* This is nonzero if we should defer warnings about undefined
912    overflow.  This facility exists because these warnings are a
913    special case.  The code to estimate loop iterations does not want
914    to issue any warnings, since it works with expressions which do not
915    occur in user code.  Various bits of cleanup code call fold(), but
916    only use the result if it has certain characteristics (e.g., is a
917    constant); that code only wants to issue a warning if the result is
918    used.  */
919
920 static int fold_deferring_overflow_warnings;
921
922 /* If a warning about undefined overflow is deferred, this is the
923    warning.  Note that this may cause us to turn two warnings into
924    one, but that is fine since it is sufficient to only give one
925    warning per expression.  */
926
927 static const char* fold_deferred_overflow_warning;
928
929 /* If a warning about undefined overflow is deferred, this is the
930    level at which the warning should be emitted.  */
931
932 static enum warn_strict_overflow_code fold_deferred_overflow_code;
933
934 /* Start deferring overflow warnings.  We could use a stack here to
935    permit nested calls, but at present it is not necessary.  */
936
937 void
938 fold_defer_overflow_warnings (void)
939 {
940   ++fold_deferring_overflow_warnings;
941 }
942
943 /* Stop deferring overflow warnings.  If there is a pending warning,
944    and ISSUE is true, then issue the warning if appropriate.  STMT is
945    the statement with which the warning should be associated (used for
946    location information); STMT may be NULL.  CODE is the level of the
947    warning--a warn_strict_overflow_code value.  This function will use
948    the smaller of CODE and the deferred code when deciding whether to
949    issue the warning.  CODE may be zero to mean to always use the
950    deferred code.  */
951
952 void
953 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
954 {
955   const char *warnmsg;
956   location_t locus;
957
958   gcc_assert (fold_deferring_overflow_warnings > 0);
959   --fold_deferring_overflow_warnings;
960   if (fold_deferring_overflow_warnings > 0)
961     {
962       if (fold_deferred_overflow_warning != NULL
963           && code != 0
964           && code < (int) fold_deferred_overflow_code)
965         fold_deferred_overflow_code = code;
966       return;
967     }
968
969   warnmsg = fold_deferred_overflow_warning;
970   fold_deferred_overflow_warning = NULL;
971
972   if (!issue || warnmsg == NULL)
973     return;
974
975   if (gimple_no_warning_p (stmt))
976     return;
977
978   /* Use the smallest code level when deciding to issue the
979      warning.  */
980   if (code == 0 || code > (int) fold_deferred_overflow_code)
981     code = fold_deferred_overflow_code;
982
983   if (!issue_strict_overflow_warning (code))
984     return;
985
986   if (stmt == NULL)
987     locus = input_location;
988   else
989     locus = gimple_location (stmt);
990   warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
991 }
992
993 /* Stop deferring overflow warnings, ignoring any deferred
994    warnings.  */
995
996 void
997 fold_undefer_and_ignore_overflow_warnings (void)
998 {
999   fold_undefer_overflow_warnings (false, NULL, 0);
1000 }
1001
1002 /* Whether we are deferring overflow warnings.  */
1003
1004 bool
1005 fold_deferring_overflow_warnings_p (void)
1006 {
1007   return fold_deferring_overflow_warnings > 0;
1008 }
1009
1010 /* This is called when we fold something based on the fact that signed
1011    overflow is undefined.  */
1012
1013 static void
1014 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1015 {
1016   if (fold_deferring_overflow_warnings > 0)
1017     {
1018       if (fold_deferred_overflow_warning == NULL
1019           || wc < fold_deferred_overflow_code)
1020         {
1021           fold_deferred_overflow_warning = gmsgid;
1022           fold_deferred_overflow_code = wc;
1023         }
1024     }
1025   else if (issue_strict_overflow_warning (wc))
1026     warning (OPT_Wstrict_overflow, gmsgid);
1027 }
1028 \f
1029 /* Return true if the built-in mathematical function specified by CODE
1030    is odd, i.e. -f(x) == f(-x).  */
1031
1032 static bool
1033 negate_mathfn_p (enum built_in_function code)
1034 {
1035   switch (code)
1036     {
1037     CASE_FLT_FN (BUILT_IN_ASIN):
1038     CASE_FLT_FN (BUILT_IN_ASINH):
1039     CASE_FLT_FN (BUILT_IN_ATAN):
1040     CASE_FLT_FN (BUILT_IN_ATANH):
1041     CASE_FLT_FN (BUILT_IN_CASIN):
1042     CASE_FLT_FN (BUILT_IN_CASINH):
1043     CASE_FLT_FN (BUILT_IN_CATAN):
1044     CASE_FLT_FN (BUILT_IN_CATANH):
1045     CASE_FLT_FN (BUILT_IN_CBRT):
1046     CASE_FLT_FN (BUILT_IN_CPROJ):
1047     CASE_FLT_FN (BUILT_IN_CSIN):
1048     CASE_FLT_FN (BUILT_IN_CSINH):
1049     CASE_FLT_FN (BUILT_IN_CTAN):
1050     CASE_FLT_FN (BUILT_IN_CTANH):
1051     CASE_FLT_FN (BUILT_IN_ERF):
1052     CASE_FLT_FN (BUILT_IN_LLROUND):
1053     CASE_FLT_FN (BUILT_IN_LROUND):
1054     CASE_FLT_FN (BUILT_IN_ROUND):
1055     CASE_FLT_FN (BUILT_IN_SIN):
1056     CASE_FLT_FN (BUILT_IN_SINH):
1057     CASE_FLT_FN (BUILT_IN_TAN):
1058     CASE_FLT_FN (BUILT_IN_TANH):
1059     CASE_FLT_FN (BUILT_IN_TRUNC):
1060       return true;
1061
1062     CASE_FLT_FN (BUILT_IN_LLRINT):
1063     CASE_FLT_FN (BUILT_IN_LRINT):
1064     CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065     CASE_FLT_FN (BUILT_IN_RINT):
1066       return !flag_rounding_math;
1067     
1068     default:
1069       break;
1070     }
1071   return false;
1072 }
1073
1074 /* Check whether we may negate an integer constant T without causing
1075    overflow.  */
1076
1077 bool
1078 may_negate_without_overflow_p (const_tree t)
1079 {
1080   unsigned HOST_WIDE_INT val;
1081   unsigned int prec;
1082   tree type;
1083
1084   gcc_assert (TREE_CODE (t) == INTEGER_CST);
1085
1086   type = TREE_TYPE (t);
1087   if (TYPE_UNSIGNED (type))
1088     return false;
1089
1090   prec = TYPE_PRECISION (type);
1091   if (prec > HOST_BITS_PER_WIDE_INT)
1092     {
1093       if (TREE_INT_CST_LOW (t) != 0)
1094         return true;
1095       prec -= HOST_BITS_PER_WIDE_INT;
1096       val = TREE_INT_CST_HIGH (t);
1097     }
1098   else
1099     val = TREE_INT_CST_LOW (t);
1100   if (prec < HOST_BITS_PER_WIDE_INT)
1101     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1103 }
1104
1105 /* Determine whether an expression T can be cheaply negated using
1106    the function negate_expr without introducing undefined overflow.  */
1107
1108 static bool
1109 negate_expr_p (tree t)
1110 {
1111   tree type;
1112
1113   if (t == 0)
1114     return false;
1115
1116   type = TREE_TYPE (t);
1117
1118   STRIP_SIGN_NOPS (t);
1119   switch (TREE_CODE (t))
1120     {
1121     case INTEGER_CST:
1122       if (TYPE_OVERFLOW_WRAPS (type))
1123         return true;
1124
1125       /* Check that -CST will not overflow type.  */
1126       return may_negate_without_overflow_p (t);
1127     case BIT_NOT_EXPR:
1128       return (INTEGRAL_TYPE_P (type)
1129               && TYPE_OVERFLOW_WRAPS (type));
1130
1131     case FIXED_CST:
1132     case REAL_CST:
1133     case NEGATE_EXPR:
1134       return true;
1135
1136     case COMPLEX_CST:
1137       return negate_expr_p (TREE_REALPART (t))
1138              && negate_expr_p (TREE_IMAGPART (t));
1139
1140     case COMPLEX_EXPR:
1141       return negate_expr_p (TREE_OPERAND (t, 0))
1142              && negate_expr_p (TREE_OPERAND (t, 1));
1143
1144     case CONJ_EXPR:
1145       return negate_expr_p (TREE_OPERAND (t, 0));
1146
1147     case PLUS_EXPR:
1148       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1150         return false;
1151       /* -(A + B) -> (-B) - A.  */
1152       if (negate_expr_p (TREE_OPERAND (t, 1))
1153           && reorder_operands_p (TREE_OPERAND (t, 0),
1154                                  TREE_OPERAND (t, 1)))
1155         return true;
1156       /* -(A + B) -> (-A) - B.  */
1157       return negate_expr_p (TREE_OPERAND (t, 0));
1158
1159     case MINUS_EXPR:
1160       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1161       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163              && reorder_operands_p (TREE_OPERAND (t, 0),
1164                                     TREE_OPERAND (t, 1));
1165
1166     case MULT_EXPR:
1167       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1168         break;
1169
1170       /* Fall through.  */
1171
1172     case RDIV_EXPR:
1173       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174         return negate_expr_p (TREE_OPERAND (t, 1))
1175                || negate_expr_p (TREE_OPERAND (t, 0));
1176       break;
1177
1178     case TRUNC_DIV_EXPR:
1179     case ROUND_DIV_EXPR:
1180     case FLOOR_DIV_EXPR:
1181     case CEIL_DIV_EXPR:
1182     case EXACT_DIV_EXPR:
1183       /* In general we can't negate A / B, because if A is INT_MIN and
1184          B is 1, we may turn this into INT_MIN / -1 which is undefined
1185          and actually traps on some architectures.  But if overflow is
1186          undefined, we can negate, because - (INT_MIN / 1) is an
1187          overflow.  */
1188       if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189           && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1190         break;
1191       return negate_expr_p (TREE_OPERAND (t, 1))
1192              || negate_expr_p (TREE_OPERAND (t, 0));
1193
1194     case NOP_EXPR:
1195       /* Negate -((double)float) as (double)(-float).  */
1196       if (TREE_CODE (type) == REAL_TYPE)
1197         {
1198           tree tem = strip_float_extensions (t);
1199           if (tem != t)
1200             return negate_expr_p (tem);
1201         }
1202       break;
1203
1204     case CALL_EXPR:
1205       /* Negate -f(x) as f(-x).  */
1206       if (negate_mathfn_p (builtin_mathfn_code (t)))
1207         return negate_expr_p (CALL_EXPR_ARG (t, 0));
1208       break;
1209
1210     case RSHIFT_EXPR:
1211       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1212       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1213         {
1214           tree op1 = TREE_OPERAND (t, 1);
1215           if (TREE_INT_CST_HIGH (op1) == 0
1216               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217                  == TREE_INT_CST_LOW (op1))
1218             return true;
1219         }
1220       break;
1221
1222     default:
1223       break;
1224     }
1225   return false;
1226 }
1227
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229    simplification is possible.
1230    If negate_expr_p would return true for T, NULL_TREE will never be
1231    returned.  */
1232
1233 static tree
1234 fold_negate_expr (tree t)
1235 {
1236   tree type = TREE_TYPE (t);
1237   tree tem;
1238
1239   switch (TREE_CODE (t))
1240     {
1241     /* Convert - (~A) to A + 1.  */
1242     case BIT_NOT_EXPR:
1243       if (INTEGRAL_TYPE_P (type))
1244         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1245                             build_int_cst (type, 1));
1246       break;
1247       
1248     case INTEGER_CST:
1249       tem = fold_negate_const (t, type);
1250       if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1251           || !TYPE_OVERFLOW_TRAPS (type))
1252         return tem;
1253       break;
1254
1255     case REAL_CST:
1256       tem = fold_negate_const (t, type);
1257       /* Two's complement FP formats, such as c4x, may overflow.  */
1258       if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1259         return tem;
1260       break;
1261
1262     case FIXED_CST:
1263       tem = fold_negate_const (t, type);
1264       return tem;
1265
1266     case COMPLEX_CST:
1267       {
1268         tree rpart = negate_expr (TREE_REALPART (t));
1269         tree ipart = negate_expr (TREE_IMAGPART (t));
1270
1271         if ((TREE_CODE (rpart) == REAL_CST
1272              && TREE_CODE (ipart) == REAL_CST)
1273             || (TREE_CODE (rpart) == INTEGER_CST
1274                 && TREE_CODE (ipart) == INTEGER_CST))
1275           return build_complex (type, rpart, ipart);
1276       }
1277       break;
1278
1279     case COMPLEX_EXPR:
1280       if (negate_expr_p (t))
1281         return fold_build2 (COMPLEX_EXPR, type,
1282                             fold_negate_expr (TREE_OPERAND (t, 0)),
1283                             fold_negate_expr (TREE_OPERAND (t, 1)));
1284       break;
1285       
1286     case CONJ_EXPR:
1287       if (negate_expr_p (t))
1288         return fold_build1 (CONJ_EXPR, type,
1289                             fold_negate_expr (TREE_OPERAND (t, 0)));
1290       break;
1291
1292     case NEGATE_EXPR:
1293       return TREE_OPERAND (t, 0);
1294
1295     case PLUS_EXPR:
1296       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1297           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1298         {
1299           /* -(A + B) -> (-B) - A.  */
1300           if (negate_expr_p (TREE_OPERAND (t, 1))
1301               && reorder_operands_p (TREE_OPERAND (t, 0),
1302                                      TREE_OPERAND (t, 1)))
1303             {
1304               tem = negate_expr (TREE_OPERAND (t, 1));
1305               return fold_build2 (MINUS_EXPR, type,
1306                                   tem, TREE_OPERAND (t, 0));
1307             }
1308
1309           /* -(A + B) -> (-A) - B.  */
1310           if (negate_expr_p (TREE_OPERAND (t, 0)))
1311             {
1312               tem = negate_expr (TREE_OPERAND (t, 0));
1313               return fold_build2 (MINUS_EXPR, type,
1314                                   tem, TREE_OPERAND (t, 1));
1315             }
1316         }
1317       break;
1318
1319     case MINUS_EXPR:
1320       /* - (A - B) -> B - A  */
1321       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1322           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1323           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1324         return fold_build2 (MINUS_EXPR, type,
1325                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1326       break;
1327
1328     case MULT_EXPR:
1329       if (TYPE_UNSIGNED (type))
1330         break;
1331
1332       /* Fall through.  */
1333
1334     case RDIV_EXPR:
1335       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1336         {
1337           tem = TREE_OPERAND (t, 1);
1338           if (negate_expr_p (tem))
1339             return fold_build2 (TREE_CODE (t), type,
1340                                 TREE_OPERAND (t, 0), negate_expr (tem));
1341           tem = TREE_OPERAND (t, 0);
1342           if (negate_expr_p (tem))
1343             return fold_build2 (TREE_CODE (t), type,
1344                                 negate_expr (tem), TREE_OPERAND (t, 1));
1345         }
1346       break;
1347
1348     case TRUNC_DIV_EXPR:
1349     case ROUND_DIV_EXPR:
1350     case FLOOR_DIV_EXPR:
1351     case CEIL_DIV_EXPR:
1352     case EXACT_DIV_EXPR:
1353       /* In general we can't negate A / B, because if A is INT_MIN and
1354          B is 1, we may turn this into INT_MIN / -1 which is undefined
1355          and actually traps on some architectures.  But if overflow is
1356          undefined, we can negate, because - (INT_MIN / 1) is an
1357          overflow.  */
1358       if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1359         {
1360           const char * const warnmsg = G_("assuming signed overflow does not "
1361                                           "occur when negating a division");
1362           tem = TREE_OPERAND (t, 1);
1363           if (negate_expr_p (tem))
1364             {
1365               if (INTEGRAL_TYPE_P (type)
1366                   && (TREE_CODE (tem) != INTEGER_CST
1367                       || integer_onep (tem)))
1368                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1369               return fold_build2 (TREE_CODE (t), type,
1370                                   TREE_OPERAND (t, 0), negate_expr (tem));
1371             }
1372           tem = TREE_OPERAND (t, 0);
1373           if (negate_expr_p (tem))
1374             {
1375               if (INTEGRAL_TYPE_P (type)
1376                   && (TREE_CODE (tem) != INTEGER_CST
1377                       || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1378                 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1379               return fold_build2 (TREE_CODE (t), type,
1380                                   negate_expr (tem), TREE_OPERAND (t, 1));
1381             }
1382         }
1383       break;
1384
1385     case NOP_EXPR:
1386       /* Convert -((double)float) into (double)(-float).  */
1387       if (TREE_CODE (type) == REAL_TYPE)
1388         {
1389           tem = strip_float_extensions (t);
1390           if (tem != t && negate_expr_p (tem))
1391             return fold_convert (type, negate_expr (tem));
1392         }
1393       break;
1394
1395     case CALL_EXPR:
1396       /* Negate -f(x) as f(-x).  */
1397       if (negate_mathfn_p (builtin_mathfn_code (t))
1398           && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1399         {
1400           tree fndecl, arg;
1401
1402           fndecl = get_callee_fndecl (t);
1403           arg = negate_expr (CALL_EXPR_ARG (t, 0));
1404           return build_call_expr (fndecl, 1, arg);
1405         }
1406       break;
1407
1408     case RSHIFT_EXPR:
1409       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1410       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1411         {
1412           tree op1 = TREE_OPERAND (t, 1);
1413           if (TREE_INT_CST_HIGH (op1) == 0
1414               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1415                  == TREE_INT_CST_LOW (op1))
1416             {
1417               tree ntype = TYPE_UNSIGNED (type)
1418                            ? signed_type_for (type)
1419                            : unsigned_type_for (type);
1420               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1421               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1422               return fold_convert (type, temp);
1423             }
1424         }
1425       break;
1426
1427     default:
1428       break;
1429     }
1430
1431   return NULL_TREE;
1432 }
1433
1434 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1435    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1436    return NULL_TREE. */
1437
1438 static tree
1439 negate_expr (tree t)
1440 {
1441   tree type, tem;
1442
1443   if (t == NULL_TREE)
1444     return NULL_TREE;
1445
1446   type = TREE_TYPE (t);
1447   STRIP_SIGN_NOPS (t);
1448
1449   tem = fold_negate_expr (t);
1450   if (!tem)
1451     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1452   return fold_convert (type, tem);
1453 }
1454 \f
1455 /* Split a tree IN into a constant, literal and variable parts that could be
1456    combined with CODE to make IN.  "constant" means an expression with
1457    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1458    commutative arithmetic operation.  Store the constant part into *CONP,
1459    the literal in *LITP and return the variable part.  If a part isn't
1460    present, set it to null.  If the tree does not decompose in this way,
1461    return the entire tree as the variable part and the other parts as null.
1462
1463    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1464    case, we negate an operand that was subtracted.  Except if it is a
1465    literal for which we use *MINUS_LITP instead.
1466
1467    If NEGATE_P is true, we are negating all of IN, again except a literal
1468    for which we use *MINUS_LITP instead.
1469
1470    If IN is itself a literal or constant, return it as appropriate.
1471
1472    Note that we do not guarantee that any of the three values will be the
1473    same type as IN, but they will have the same signedness and mode.  */
1474
1475 static tree
1476 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1477             tree *minus_litp, int negate_p)
1478 {
1479   tree var = 0;
1480
1481   *conp = 0;
1482   *litp = 0;
1483   *minus_litp = 0;
1484
1485   /* Strip any conversions that don't change the machine mode or signedness.  */
1486   STRIP_SIGN_NOPS (in);
1487
1488   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1489       || TREE_CODE (in) == FIXED_CST)
1490     *litp = in;
1491   else if (TREE_CODE (in) == code
1492            || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1493                && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1494                /* We can associate addition and subtraction together (even
1495                   though the C standard doesn't say so) for integers because
1496                   the value is not affected.  For reals, the value might be
1497                   affected, so we can't.  */
1498                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1499                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1500     {
1501       tree op0 = TREE_OPERAND (in, 0);
1502       tree op1 = TREE_OPERAND (in, 1);
1503       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1504       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1505
1506       /* First see if either of the operands is a literal, then a constant.  */
1507       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1508           || TREE_CODE (op0) == FIXED_CST)
1509         *litp = op0, op0 = 0;
1510       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1511                || TREE_CODE (op1) == FIXED_CST)
1512         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1513
1514       if (op0 != 0 && TREE_CONSTANT (op0))
1515         *conp = op0, op0 = 0;
1516       else if (op1 != 0 && TREE_CONSTANT (op1))
1517         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1518
1519       /* If we haven't dealt with either operand, this is not a case we can
1520          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1521       if (op0 != 0 && op1 != 0)
1522         var = in;
1523       else if (op0 != 0)
1524         var = op0;
1525       else
1526         var = op1, neg_var_p = neg1_p;
1527
1528       /* Now do any needed negations.  */
1529       if (neg_litp_p)
1530         *minus_litp = *litp, *litp = 0;
1531       if (neg_conp_p)
1532         *conp = negate_expr (*conp);
1533       if (neg_var_p)
1534         var = negate_expr (var);
1535     }
1536   else if (TREE_CONSTANT (in))
1537     *conp = in;
1538   else
1539     var = in;
1540
1541   if (negate_p)
1542     {
1543       if (*litp)
1544         *minus_litp = *litp, *litp = 0;
1545       else if (*minus_litp)
1546         *litp = *minus_litp, *minus_litp = 0;
1547       *conp = negate_expr (*conp);
1548       var = negate_expr (var);
1549     }
1550
1551   return var;
1552 }
1553
1554 /* Re-associate trees split by the above function.  T1 and T2 are either
1555    expressions to associate or null.  Return the new expression, if any.  If
1556    we build an operation, do it in TYPE and with CODE.  */
1557
1558 static tree
1559 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1560 {
1561   if (t1 == 0)
1562     return t2;
1563   else if (t2 == 0)
1564     return t1;
1565
1566   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1567      try to fold this since we will have infinite recursion.  But do
1568      deal with any NEGATE_EXPRs.  */
1569   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1570       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1571     {
1572       if (code == PLUS_EXPR)
1573         {
1574           if (TREE_CODE (t1) == NEGATE_EXPR)
1575             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1576                            fold_convert (type, TREE_OPERAND (t1, 0)));
1577           else if (TREE_CODE (t2) == NEGATE_EXPR)
1578             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1579                            fold_convert (type, TREE_OPERAND (t2, 0)));
1580           else if (integer_zerop (t2))
1581             return fold_convert (type, t1);
1582         }
1583       else if (code == MINUS_EXPR)
1584         {
1585           if (integer_zerop (t2))
1586             return fold_convert (type, t1);
1587         }
1588
1589       return build2 (code, type, fold_convert (type, t1),
1590                      fold_convert (type, t2));
1591     }
1592
1593   return fold_build2 (code, type, fold_convert (type, t1),
1594                       fold_convert (type, t2));
1595 }
1596 \f
1597 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1598    for use in int_const_binop, size_binop and size_diffop.  */
1599
1600 static bool
1601 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1602 {
1603   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1604     return false;
1605   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1606     return false;
1607
1608   switch (code)
1609     {
1610     case LSHIFT_EXPR:
1611     case RSHIFT_EXPR:
1612     case LROTATE_EXPR:
1613     case RROTATE_EXPR:
1614       return true;
1615
1616     default:
1617       break;
1618     }
1619
1620   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1621          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1622          && TYPE_MODE (type1) == TYPE_MODE (type2);
1623 }
1624
1625
1626 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1627    to produce a new constant.  Return NULL_TREE if we don't know how
1628    to evaluate CODE at compile-time.
1629
1630    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1631
1632 tree
1633 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1634 {
1635   unsigned HOST_WIDE_INT int1l, int2l;
1636   HOST_WIDE_INT int1h, int2h;
1637   unsigned HOST_WIDE_INT low;
1638   HOST_WIDE_INT hi;
1639   unsigned HOST_WIDE_INT garbagel;
1640   HOST_WIDE_INT garbageh;
1641   tree t;
1642   tree type = TREE_TYPE (arg1);
1643   int uns = TYPE_UNSIGNED (type);
1644   int is_sizetype
1645     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1646   int overflow = 0;
1647
1648   int1l = TREE_INT_CST_LOW (arg1);
1649   int1h = TREE_INT_CST_HIGH (arg1);
1650   int2l = TREE_INT_CST_LOW (arg2);
1651   int2h = TREE_INT_CST_HIGH (arg2);
1652
1653   switch (code)
1654     {
1655     case BIT_IOR_EXPR:
1656       low = int1l | int2l, hi = int1h | int2h;
1657       break;
1658
1659     case BIT_XOR_EXPR:
1660       low = int1l ^ int2l, hi = int1h ^ int2h;
1661       break;
1662
1663     case BIT_AND_EXPR:
1664       low = int1l & int2l, hi = int1h & int2h;
1665       break;
1666
1667     case RSHIFT_EXPR:
1668       int2l = -int2l;
1669     case LSHIFT_EXPR:
1670       /* It's unclear from the C standard whether shifts can overflow.
1671          The following code ignores overflow; perhaps a C standard
1672          interpretation ruling is needed.  */
1673       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1674                      &low, &hi, !uns);
1675       break;
1676
1677     case RROTATE_EXPR:
1678       int2l = - int2l;
1679     case LROTATE_EXPR:
1680       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1681                       &low, &hi);
1682       break;
1683
1684     case PLUS_EXPR:
1685       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1686       break;
1687
1688     case MINUS_EXPR:
1689       neg_double (int2l, int2h, &low, &hi);
1690       add_double (int1l, int1h, low, hi, &low, &hi);
1691       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1692       break;
1693
1694     case MULT_EXPR:
1695       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1696       break;
1697
1698     case TRUNC_DIV_EXPR:
1699     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1700     case EXACT_DIV_EXPR:
1701       /* This is a shortcut for a common special case.  */
1702       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1703           && !TREE_OVERFLOW (arg1)
1704           && !TREE_OVERFLOW (arg2)
1705           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1706         {
1707           if (code == CEIL_DIV_EXPR)
1708             int1l += int2l - 1;
1709
1710           low = int1l / int2l, hi = 0;
1711           break;
1712         }
1713
1714       /* ... fall through ...  */
1715
1716     case ROUND_DIV_EXPR:
1717       if (int2h == 0 && int2l == 0)
1718         return NULL_TREE;
1719       if (int2h == 0 && int2l == 1)
1720         {
1721           low = int1l, hi = int1h;
1722           break;
1723         }
1724       if (int1l == int2l && int1h == int2h
1725           && ! (int1l == 0 && int1h == 0))
1726         {
1727           low = 1, hi = 0;
1728           break;
1729         }
1730       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1731                                        &low, &hi, &garbagel, &garbageh);
1732       break;
1733
1734     case TRUNC_MOD_EXPR:
1735     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1736       /* This is a shortcut for a common special case.  */
1737       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1738           && !TREE_OVERFLOW (arg1)
1739           && !TREE_OVERFLOW (arg2)
1740           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1741         {
1742           if (code == CEIL_MOD_EXPR)
1743             int1l += int2l - 1;
1744           low = int1l % int2l, hi = 0;
1745           break;
1746         }
1747
1748       /* ... fall through ...  */
1749
1750     case ROUND_MOD_EXPR:
1751       if (int2h == 0 && int2l == 0)
1752         return NULL_TREE;
1753       overflow = div_and_round_double (code, uns,
1754                                        int1l, int1h, int2l, int2h,
1755                                        &garbagel, &garbageh, &low, &hi);
1756       break;
1757
1758     case MIN_EXPR:
1759     case MAX_EXPR:
1760       if (uns)
1761         low = (((unsigned HOST_WIDE_INT) int1h
1762                 < (unsigned HOST_WIDE_INT) int2h)
1763                || (((unsigned HOST_WIDE_INT) int1h
1764                     == (unsigned HOST_WIDE_INT) int2h)
1765                    && int1l < int2l));
1766       else
1767         low = (int1h < int2h
1768                || (int1h == int2h && int1l < int2l));
1769
1770       if (low == (code == MIN_EXPR))
1771         low = int1l, hi = int1h;
1772       else
1773         low = int2l, hi = int2h;
1774       break;
1775
1776     default:
1777       return NULL_TREE;
1778     }
1779
1780   if (notrunc)
1781     {
1782       t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1783
1784       /* Propagate overflow flags ourselves.  */
1785       if (((!uns || is_sizetype) && overflow)
1786           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1787         {
1788           t = copy_node (t);
1789           TREE_OVERFLOW (t) = 1;
1790         }
1791     }
1792   else
1793     t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1794                                ((!uns || is_sizetype) && overflow)
1795                                | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1796
1797   return t;
1798 }
1799
1800 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1801    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1802    are the same kind of constant and the same machine mode.  Return zero if
1803    combining the constants is not allowed in the current operating mode.
1804
1805    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1806
1807 static tree
1808 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1809 {
1810   /* Sanity check for the recursive cases.  */
1811   if (!arg1 || !arg2)
1812     return NULL_TREE;
1813
1814   STRIP_NOPS (arg1);
1815   STRIP_NOPS (arg2);
1816
1817   if (TREE_CODE (arg1) == INTEGER_CST)
1818     return int_const_binop (code, arg1, arg2, notrunc);
1819
1820   if (TREE_CODE (arg1) == REAL_CST)
1821     {
1822       enum machine_mode mode;
1823       REAL_VALUE_TYPE d1;
1824       REAL_VALUE_TYPE d2;
1825       REAL_VALUE_TYPE value;
1826       REAL_VALUE_TYPE result;
1827       bool inexact;
1828       tree t, type;
1829
1830       /* The following codes are handled by real_arithmetic.  */
1831       switch (code)
1832         {
1833         case PLUS_EXPR:
1834         case MINUS_EXPR:
1835         case MULT_EXPR:
1836         case RDIV_EXPR:
1837         case MIN_EXPR:
1838         case MAX_EXPR:
1839           break;
1840
1841         default:
1842           return NULL_TREE;
1843         }
1844
1845       d1 = TREE_REAL_CST (arg1);
1846       d2 = TREE_REAL_CST (arg2);
1847
1848       type = TREE_TYPE (arg1);
1849       mode = TYPE_MODE (type);
1850
1851       /* Don't perform operation if we honor signaling NaNs and
1852          either operand is a NaN.  */
1853       if (HONOR_SNANS (mode)
1854           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1855         return NULL_TREE;
1856
1857       /* Don't perform operation if it would raise a division
1858          by zero exception.  */
1859       if (code == RDIV_EXPR
1860           && REAL_VALUES_EQUAL (d2, dconst0)
1861           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1862         return NULL_TREE;
1863
1864       /* If either operand is a NaN, just return it.  Otherwise, set up
1865          for floating-point trap; we return an overflow.  */
1866       if (REAL_VALUE_ISNAN (d1))
1867         return arg1;
1868       else if (REAL_VALUE_ISNAN (d2))
1869         return arg2;
1870
1871       inexact = real_arithmetic (&value, code, &d1, &d2);
1872       real_convert (&result, mode, &value);
1873
1874       /* Don't constant fold this floating point operation if
1875          the result has overflowed and flag_trapping_math.  */
1876       if (flag_trapping_math
1877           && MODE_HAS_INFINITIES (mode)
1878           && REAL_VALUE_ISINF (result)
1879           && !REAL_VALUE_ISINF (d1)
1880           && !REAL_VALUE_ISINF (d2))
1881         return NULL_TREE;
1882
1883       /* Don't constant fold this floating point operation if the
1884          result may dependent upon the run-time rounding mode and
1885          flag_rounding_math is set, or if GCC's software emulation
1886          is unable to accurately represent the result.  */
1887       if ((flag_rounding_math
1888            || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1889           && (inexact || !real_identical (&result, &value)))
1890         return NULL_TREE;
1891
1892       t = build_real (type, result);
1893
1894       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1895       return t;
1896     }
1897
1898   if (TREE_CODE (arg1) == FIXED_CST)
1899     {
1900       FIXED_VALUE_TYPE f1;
1901       FIXED_VALUE_TYPE f2;
1902       FIXED_VALUE_TYPE result;
1903       tree t, type;
1904       int sat_p;
1905       bool overflow_p;
1906
1907       /* The following codes are handled by fixed_arithmetic.  */
1908       switch (code)
1909         {
1910         case PLUS_EXPR:
1911         case MINUS_EXPR:
1912         case MULT_EXPR:
1913         case TRUNC_DIV_EXPR:
1914           f2 = TREE_FIXED_CST (arg2);
1915           break;
1916
1917         case LSHIFT_EXPR:
1918         case RSHIFT_EXPR:
1919           f2.data.high = TREE_INT_CST_HIGH (arg2);
1920           f2.data.low = TREE_INT_CST_LOW (arg2);
1921           f2.mode = SImode;
1922           break;
1923
1924         default:
1925           return NULL_TREE;
1926         }
1927
1928       f1 = TREE_FIXED_CST (arg1);
1929       type = TREE_TYPE (arg1);
1930       sat_p = TYPE_SATURATING (type);
1931       overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1932       t = build_fixed (type, result);
1933       /* Propagate overflow flags.  */
1934       if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1935         TREE_OVERFLOW (t) = 1;
1936       return t;
1937     }
1938
1939   if (TREE_CODE (arg1) == COMPLEX_CST)
1940     {
1941       tree type = TREE_TYPE (arg1);
1942       tree r1 = TREE_REALPART (arg1);
1943       tree i1 = TREE_IMAGPART (arg1);
1944       tree r2 = TREE_REALPART (arg2);
1945       tree i2 = TREE_IMAGPART (arg2);
1946       tree real, imag;
1947
1948       switch (code)
1949         {
1950         case PLUS_EXPR:
1951         case MINUS_EXPR:
1952           real = const_binop (code, r1, r2, notrunc);
1953           imag = const_binop (code, i1, i2, notrunc);
1954           break;
1955
1956         case MULT_EXPR:
1957           real = const_binop (MINUS_EXPR,
1958                               const_binop (MULT_EXPR, r1, r2, notrunc),
1959                               const_binop (MULT_EXPR, i1, i2, notrunc),
1960                               notrunc);
1961           imag = const_binop (PLUS_EXPR,
1962                               const_binop (MULT_EXPR, r1, i2, notrunc),
1963                               const_binop (MULT_EXPR, i1, r2, notrunc),
1964                               notrunc);
1965           break;
1966
1967         case RDIV_EXPR:
1968           {
1969             tree magsquared
1970               = const_binop (PLUS_EXPR,
1971                              const_binop (MULT_EXPR, r2, r2, notrunc),
1972                              const_binop (MULT_EXPR, i2, i2, notrunc),
1973                              notrunc);
1974             tree t1
1975               = const_binop (PLUS_EXPR,
1976                              const_binop (MULT_EXPR, r1, r2, notrunc),
1977                              const_binop (MULT_EXPR, i1, i2, notrunc),
1978                              notrunc);
1979             tree t2
1980               = const_binop (MINUS_EXPR,
1981                              const_binop (MULT_EXPR, i1, r2, notrunc),
1982                              const_binop (MULT_EXPR, r1, i2, notrunc),
1983                              notrunc);
1984
1985             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1986               code = TRUNC_DIV_EXPR;
1987
1988             real = const_binop (code, t1, magsquared, notrunc);
1989             imag = const_binop (code, t2, magsquared, notrunc);
1990           }
1991           break;
1992
1993         default:
1994           return NULL_TREE;
1995         }
1996
1997       if (real && imag)
1998         return build_complex (type, real, imag);
1999     }
2000
2001   return NULL_TREE;
2002 }
2003
2004 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2005    indicates which particular sizetype to create.  */
2006
2007 tree
2008 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2009 {
2010   return build_int_cst (sizetype_tab[(int) kind], number);
2011 }
2012 \f
2013 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2014    is a tree code.  The type of the result is taken from the operands.
2015    Both must be equivalent integer types, ala int_binop_types_match_p.
2016    If the operands are constant, so is the result.  */
2017
2018 tree
2019 size_binop (enum tree_code code, tree arg0, tree arg1)
2020 {
2021   tree type = TREE_TYPE (arg0);
2022
2023   if (arg0 == error_mark_node || arg1 == error_mark_node)
2024     return error_mark_node;
2025
2026   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2027                                        TREE_TYPE (arg1)));
2028
2029   /* Handle the special case of two integer constants faster.  */
2030   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2031     {
2032       /* And some specific cases even faster than that.  */
2033       if (code == PLUS_EXPR)
2034         {
2035           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2036             return arg1;
2037           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2038             return arg0;
2039         }
2040       else if (code == MINUS_EXPR)
2041         {
2042           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2043             return arg0;
2044         }
2045       else if (code == MULT_EXPR)
2046         {
2047           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2048             return arg1;
2049         }
2050
2051       /* Handle general case of two integer constants.  */
2052       return int_const_binop (code, arg0, arg1, 0);
2053     }
2054
2055   return fold_build2 (code, type, arg0, arg1);
2056 }
2057
2058 /* Given two values, either both of sizetype or both of bitsizetype,
2059    compute the difference between the two values.  Return the value
2060    in signed type corresponding to the type of the operands.  */
2061
2062 tree
2063 size_diffop (tree arg0, tree arg1)
2064 {
2065   tree type = TREE_TYPE (arg0);
2066   tree ctype;
2067
2068   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2069                                        TREE_TYPE (arg1)));
2070
2071   /* If the type is already signed, just do the simple thing.  */
2072   if (!TYPE_UNSIGNED (type))
2073     return size_binop (MINUS_EXPR, arg0, arg1);
2074
2075   if (type == sizetype)
2076     ctype = ssizetype;
2077   else if (type == bitsizetype)
2078     ctype = sbitsizetype;
2079   else
2080     ctype = signed_type_for (type);
2081
2082   /* If either operand is not a constant, do the conversions to the signed
2083      type and subtract.  The hardware will do the right thing with any
2084      overflow in the subtraction.  */
2085   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2086     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2087                        fold_convert (ctype, arg1));
2088
2089   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2090      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2091      overflow) and negate (which can't either).  Special-case a result
2092      of zero while we're here.  */
2093   if (tree_int_cst_equal (arg0, arg1))
2094     return build_int_cst (ctype, 0);
2095   else if (tree_int_cst_lt (arg1, arg0))
2096     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2097   else
2098     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2099                        fold_convert (ctype, size_binop (MINUS_EXPR,
2100                                                         arg1, arg0)));
2101 }
2102 \f
2103 /* A subroutine of fold_convert_const handling conversions of an
2104    INTEGER_CST to another integer type.  */
2105
2106 static tree
2107 fold_convert_const_int_from_int (tree type, const_tree arg1)
2108 {
2109   tree t;
2110
2111   /* Given an integer constant, make new constant with new type,
2112      appropriately sign-extended or truncated.  */
2113   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2114                              TREE_INT_CST_HIGH (arg1),
2115                              /* Don't set the overflow when
2116                                 converting from a pointer,  */
2117                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2118                              /* or to a sizetype with same signedness
2119                                 and the precision is unchanged.
2120                                 ???  sizetype is always sign-extended,
2121                                 but its signedness depends on the
2122                                 frontend.  Thus we see spurious overflows
2123                                 here if we do not check this.  */
2124                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2125                                    == TYPE_PRECISION (type))
2126                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2127                                       == TYPE_UNSIGNED (type))
2128                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2129                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2130                                       || (TREE_CODE (type) == INTEGER_TYPE
2131                                           && TYPE_IS_SIZETYPE (type)))),
2132                              (TREE_INT_CST_HIGH (arg1) < 0
2133                               && (TYPE_UNSIGNED (type)
2134                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2135                              | TREE_OVERFLOW (arg1));
2136
2137   return t;
2138 }
2139
2140 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2141    to an integer type.  */
2142
2143 static tree
2144 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2145 {
2146   int overflow = 0;
2147   tree t;
2148
2149   /* The following code implements the floating point to integer
2150      conversion rules required by the Java Language Specification,
2151      that IEEE NaNs are mapped to zero and values that overflow
2152      the target precision saturate, i.e. values greater than
2153      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2154      are mapped to INT_MIN.  These semantics are allowed by the
2155      C and C++ standards that simply state that the behavior of
2156      FP-to-integer conversion is unspecified upon overflow.  */
2157
2158   HOST_WIDE_INT high, low;
2159   REAL_VALUE_TYPE r;
2160   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2161
2162   switch (code)
2163     {
2164     case FIX_TRUNC_EXPR:
2165       real_trunc (&r, VOIDmode, &x);
2166       break;
2167
2168     default:
2169       gcc_unreachable ();
2170     }
2171
2172   /* If R is NaN, return zero and show we have an overflow.  */
2173   if (REAL_VALUE_ISNAN (r))
2174     {
2175       overflow = 1;
2176       high = 0;
2177       low = 0;
2178     }
2179
2180   /* See if R is less than the lower bound or greater than the
2181      upper bound.  */
2182
2183   if (! overflow)
2184     {
2185       tree lt = TYPE_MIN_VALUE (type);
2186       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2187       if (REAL_VALUES_LESS (r, l))
2188         {
2189           overflow = 1;
2190           high = TREE_INT_CST_HIGH (lt);
2191           low = TREE_INT_CST_LOW (lt);
2192         }
2193     }
2194
2195   if (! overflow)
2196     {
2197       tree ut = TYPE_MAX_VALUE (type);
2198       if (ut)
2199         {
2200           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2201           if (REAL_VALUES_LESS (u, r))
2202             {
2203               overflow = 1;
2204               high = TREE_INT_CST_HIGH (ut);
2205               low = TREE_INT_CST_LOW (ut);
2206             }
2207         }
2208     }
2209
2210   if (! overflow)
2211     REAL_VALUE_TO_INT (&low, &high, r);
2212
2213   t = force_fit_type_double (type, low, high, -1,
2214                              overflow | TREE_OVERFLOW (arg1));
2215   return t;
2216 }
2217
2218 /* A subroutine of fold_convert_const handling conversions of a
2219    FIXED_CST to an integer type.  */
2220
2221 static tree
2222 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2223 {
2224   tree t;
2225   double_int temp, temp_trunc;
2226   unsigned int mode;
2227
2228   /* Right shift FIXED_CST to temp by fbit.  */
2229   temp = TREE_FIXED_CST (arg1).data;
2230   mode = TREE_FIXED_CST (arg1).mode;
2231   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2232     {
2233       lshift_double (temp.low, temp.high,
2234                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2235                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2236
2237       /* Left shift temp to temp_trunc by fbit.  */
2238       lshift_double (temp.low, temp.high,
2239                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240                      &temp_trunc.low, &temp_trunc.high,
2241                      SIGNED_FIXED_POINT_MODE_P (mode));
2242     }
2243   else
2244     {
2245       temp.low = 0;
2246       temp.high = 0;
2247       temp_trunc.low = 0;
2248       temp_trunc.high = 0;
2249     }
2250
2251   /* If FIXED_CST is negative, we need to round the value toward 0.
2252      By checking if the fractional bits are not zero to add 1 to temp.  */
2253   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2254       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2255     {
2256       double_int one;
2257       one.low = 1;
2258       one.high = 0;
2259       temp = double_int_add (temp, one);
2260     }
2261
2262   /* Given a fixed-point constant, make new constant with new type,
2263      appropriately sign-extended or truncated.  */
2264   t = force_fit_type_double (type, temp.low, temp.high, -1,
2265                              (temp.high < 0
2266                               && (TYPE_UNSIGNED (type)
2267                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2268                              | TREE_OVERFLOW (arg1));
2269
2270   return t;
2271 }
2272
2273 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2274    to another floating point type.  */
2275
2276 static tree
2277 fold_convert_const_real_from_real (tree type, const_tree arg1)
2278 {
2279   REAL_VALUE_TYPE value;
2280   tree t;
2281
2282   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2283   t = build_real (type, value);
2284
2285   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2286   return t;
2287 }
2288
2289 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2290    to a floating point type.  */
2291
2292 static tree
2293 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2294 {
2295   REAL_VALUE_TYPE value;
2296   tree t;
2297
2298   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2299   t = build_real (type, value);
2300
2301   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2302   return t;
2303 }
2304
2305 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2306    to another fixed-point type.  */
2307
2308 static tree
2309 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2310 {
2311   FIXED_VALUE_TYPE value;
2312   tree t;
2313   bool overflow_p;
2314
2315   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2316                               TYPE_SATURATING (type));
2317   t = build_fixed (type, value);
2318
2319   /* Propagate overflow flags.  */
2320   if (overflow_p | TREE_OVERFLOW (arg1))
2321     TREE_OVERFLOW (t) = 1;
2322   return t;
2323 }
2324
2325 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2326    to a fixed-point type.  */
2327
2328 static tree
2329 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2330 {
2331   FIXED_VALUE_TYPE value;
2332   tree t;
2333   bool overflow_p;
2334
2335   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2336                                        TREE_INT_CST (arg1),
2337                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2338                                        TYPE_SATURATING (type));
2339   t = build_fixed (type, value);
2340
2341   /* Propagate overflow flags.  */
2342   if (overflow_p | TREE_OVERFLOW (arg1))
2343     TREE_OVERFLOW (t) = 1;
2344   return t;
2345 }
2346
2347 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2348    to a fixed-point type.  */
2349
2350 static tree
2351 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2352 {
2353   FIXED_VALUE_TYPE value;
2354   tree t;
2355   bool overflow_p;
2356
2357   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2358                                         &TREE_REAL_CST (arg1),
2359                                         TYPE_SATURATING (type));
2360   t = build_fixed (type, value);
2361
2362   /* Propagate overflow flags.  */
2363   if (overflow_p | TREE_OVERFLOW (arg1))
2364     TREE_OVERFLOW (t) = 1;
2365   return t;
2366 }
2367
2368 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2369    type TYPE.  If no simplification can be done return NULL_TREE.  */
2370
2371 static tree
2372 fold_convert_const (enum tree_code code, tree type, tree arg1)
2373 {
2374   if (TREE_TYPE (arg1) == type)
2375     return arg1;
2376
2377   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2378       || TREE_CODE (type) == OFFSET_TYPE)
2379     {
2380       if (TREE_CODE (arg1) == INTEGER_CST)
2381         return fold_convert_const_int_from_int (type, arg1);
2382       else if (TREE_CODE (arg1) == REAL_CST)
2383         return fold_convert_const_int_from_real (code, type, arg1);
2384       else if (TREE_CODE (arg1) == FIXED_CST)
2385         return fold_convert_const_int_from_fixed (type, arg1);
2386     }
2387   else if (TREE_CODE (type) == REAL_TYPE)
2388     {
2389       if (TREE_CODE (arg1) == INTEGER_CST)
2390         return build_real_from_int_cst (type, arg1);
2391       else if (TREE_CODE (arg1) == REAL_CST)
2392         return fold_convert_const_real_from_real (type, arg1);
2393       else if (TREE_CODE (arg1) == FIXED_CST)
2394         return fold_convert_const_real_from_fixed (type, arg1);
2395     }
2396   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2397     {
2398       if (TREE_CODE (arg1) == FIXED_CST)
2399         return fold_convert_const_fixed_from_fixed (type, arg1);
2400       else if (TREE_CODE (arg1) == INTEGER_CST)
2401         return fold_convert_const_fixed_from_int (type, arg1);
2402       else if (TREE_CODE (arg1) == REAL_CST)
2403         return fold_convert_const_fixed_from_real (type, arg1);
2404     }
2405   return NULL_TREE;
2406 }
2407
2408 /* Construct a vector of zero elements of vector type TYPE.  */
2409
2410 static tree
2411 build_zero_vector (tree type)
2412 {
2413   tree elem, list;
2414   int i, units;
2415
2416   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2417   units = TYPE_VECTOR_SUBPARTS (type);
2418   
2419   list = NULL_TREE;
2420   for (i = 0; i < units; i++)
2421     list = tree_cons (NULL_TREE, elem, list);
2422   return build_vector (type, list);
2423 }
2424
2425 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2426
2427 bool
2428 fold_convertible_p (const_tree type, const_tree arg)
2429 {
2430   tree orig = TREE_TYPE (arg);
2431
2432   if (type == orig)
2433     return true;
2434
2435   if (TREE_CODE (arg) == ERROR_MARK
2436       || TREE_CODE (type) == ERROR_MARK
2437       || TREE_CODE (orig) == ERROR_MARK)
2438     return false;
2439
2440   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2441     return true;
2442
2443   switch (TREE_CODE (type))
2444     {
2445     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2446     case POINTER_TYPE: case REFERENCE_TYPE:
2447     case OFFSET_TYPE:
2448       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2449           || TREE_CODE (orig) == OFFSET_TYPE)
2450         return true;
2451       return (TREE_CODE (orig) == VECTOR_TYPE
2452               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2453
2454     case REAL_TYPE:
2455     case FIXED_POINT_TYPE:
2456     case COMPLEX_TYPE:
2457     case VECTOR_TYPE:
2458     case VOID_TYPE:
2459       return TREE_CODE (type) == TREE_CODE (orig);
2460
2461     default:
2462       return false;
2463     }
2464 }
2465
2466 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2467    simple conversions in preference to calling the front-end's convert.  */
2468
2469 tree
2470 fold_convert (tree type, tree arg)
2471 {
2472   tree orig = TREE_TYPE (arg);
2473   tree tem;
2474
2475   if (type == orig)
2476     return arg;
2477
2478   if (TREE_CODE (arg) == ERROR_MARK
2479       || TREE_CODE (type) == ERROR_MARK
2480       || TREE_CODE (orig) == ERROR_MARK)
2481     return error_mark_node;
2482
2483   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2484     return fold_build1 (NOP_EXPR, type, arg);
2485
2486   switch (TREE_CODE (type))
2487     {
2488     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2489     case POINTER_TYPE: case REFERENCE_TYPE:
2490     case OFFSET_TYPE:
2491       if (TREE_CODE (arg) == INTEGER_CST)
2492         {
2493           tem = fold_convert_const (NOP_EXPR, type, arg);
2494           if (tem != NULL_TREE)
2495             return tem;
2496         }
2497       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2498           || TREE_CODE (orig) == OFFSET_TYPE)
2499         return fold_build1 (NOP_EXPR, type, arg);
2500       if (TREE_CODE (orig) == COMPLEX_TYPE)
2501         {
2502           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2503           return fold_convert (type, tem);
2504         }
2505       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2506                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2507       return fold_build1 (NOP_EXPR, type, arg);
2508
2509     case REAL_TYPE:
2510       if (TREE_CODE (arg) == INTEGER_CST)
2511         {
2512           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2513           if (tem != NULL_TREE)
2514             return tem;
2515         }
2516       else if (TREE_CODE (arg) == REAL_CST)
2517         {
2518           tem = fold_convert_const (NOP_EXPR, type, arg);
2519           if (tem != NULL_TREE)
2520             return tem;
2521         }
2522       else if (TREE_CODE (arg) == FIXED_CST)
2523         {
2524           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2525           if (tem != NULL_TREE)
2526             return tem;
2527         }
2528
2529       switch (TREE_CODE (orig))
2530         {
2531         case INTEGER_TYPE:
2532         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2533         case POINTER_TYPE: case REFERENCE_TYPE:
2534           return fold_build1 (FLOAT_EXPR, type, arg);
2535
2536         case REAL_TYPE:
2537           return fold_build1 (NOP_EXPR, type, arg);
2538
2539         case FIXED_POINT_TYPE:
2540           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2541
2542         case COMPLEX_TYPE:
2543           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2544           return fold_convert (type, tem);
2545
2546         default:
2547           gcc_unreachable ();
2548         }
2549
2550     case FIXED_POINT_TYPE:
2551       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2552           || TREE_CODE (arg) == REAL_CST)
2553         {
2554           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2555           if (tem != NULL_TREE)
2556             return tem;
2557         }
2558
2559       switch (TREE_CODE (orig))
2560         {
2561         case FIXED_POINT_TYPE:
2562         case INTEGER_TYPE:
2563         case ENUMERAL_TYPE:
2564         case BOOLEAN_TYPE:
2565         case REAL_TYPE:
2566           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2567
2568         case COMPLEX_TYPE:
2569           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2570           return fold_convert (type, tem);
2571
2572         default:
2573           gcc_unreachable ();
2574         }
2575
2576     case COMPLEX_TYPE:
2577       switch (TREE_CODE (orig))
2578         {
2579         case INTEGER_TYPE:
2580         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2581         case POINTER_TYPE: case REFERENCE_TYPE:
2582         case REAL_TYPE:
2583         case FIXED_POINT_TYPE:
2584           return build2 (COMPLEX_EXPR, type,
2585                          fold_convert (TREE_TYPE (type), arg),
2586                          fold_convert (TREE_TYPE (type), integer_zero_node));
2587         case COMPLEX_TYPE:
2588           {
2589             tree rpart, ipart;
2590
2591             if (TREE_CODE (arg) == COMPLEX_EXPR)
2592               {
2593                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2594                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2595                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2596               }
2597
2598             arg = save_expr (arg);
2599             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2600             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2601             rpart = fold_convert (TREE_TYPE (type), rpart);
2602             ipart = fold_convert (TREE_TYPE (type), ipart);
2603             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2604           }
2605
2606         default:
2607           gcc_unreachable ();
2608         }
2609
2610     case VECTOR_TYPE:
2611       if (integer_zerop (arg))
2612         return build_zero_vector (type);
2613       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2614       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2615                   || TREE_CODE (orig) == VECTOR_TYPE);
2616       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2617
2618     case VOID_TYPE:
2619       tem = fold_ignored_result (arg);
2620       if (TREE_CODE (tem) == MODIFY_EXPR)
2621         return tem;
2622       return fold_build1 (NOP_EXPR, type, tem);
2623
2624     default:
2625       gcc_unreachable ();
2626     }
2627 }
2628 \f
2629 /* Return false if expr can be assumed not to be an lvalue, true
2630    otherwise.  */
2631
2632 static bool
2633 maybe_lvalue_p (const_tree x)
2634 {
2635   /* We only need to wrap lvalue tree codes.  */
2636   switch (TREE_CODE (x))
2637   {
2638   case VAR_DECL:
2639   case PARM_DECL:
2640   case RESULT_DECL:
2641   case LABEL_DECL:
2642   case FUNCTION_DECL:
2643   case SSA_NAME:
2644
2645   case COMPONENT_REF:
2646   case INDIRECT_REF:
2647   case ALIGN_INDIRECT_REF:
2648   case MISALIGNED_INDIRECT_REF:
2649   case ARRAY_REF:
2650   case ARRAY_RANGE_REF:
2651   case BIT_FIELD_REF:
2652   case OBJ_TYPE_REF:
2653
2654   case REALPART_EXPR:
2655   case IMAGPART_EXPR:
2656   case PREINCREMENT_EXPR:
2657   case PREDECREMENT_EXPR:
2658   case SAVE_EXPR:
2659   case TRY_CATCH_EXPR:
2660   case WITH_CLEANUP_EXPR:
2661   case COMPOUND_EXPR:
2662   case MODIFY_EXPR:
2663   case TARGET_EXPR:
2664   case COND_EXPR:
2665   case BIND_EXPR:
2666   case MIN_EXPR:
2667   case MAX_EXPR:
2668     break;
2669
2670   default:
2671     /* Assume the worst for front-end tree codes.  */
2672     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2673       break;
2674     return false;
2675   }
2676
2677   return true;
2678 }
2679
2680 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2681
2682 tree
2683 non_lvalue (tree x)
2684 {
2685   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2686      us.  */
2687   if (in_gimple_form)
2688     return x;
2689
2690   if (! maybe_lvalue_p (x))
2691     return x;
2692   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2693 }
2694
2695 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2696    Zero means allow extended lvalues.  */
2697
2698 int pedantic_lvalues;
2699
2700 /* When pedantic, return an expr equal to X but certainly not valid as a
2701    pedantic lvalue.  Otherwise, return X.  */
2702
2703 static tree
2704 pedantic_non_lvalue (tree x)
2705 {
2706   if (pedantic_lvalues)
2707     return non_lvalue (x);
2708   else
2709     return x;
2710 }
2711 \f
2712 /* Given a tree comparison code, return the code that is the logical inverse
2713    of the given code.  It is not safe to do this for floating-point
2714    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2715    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2716
2717 enum tree_code
2718 invert_tree_comparison (enum tree_code code, bool honor_nans)
2719 {
2720   if (honor_nans && flag_trapping_math)
2721     return ERROR_MARK;
2722
2723   switch (code)
2724     {
2725     case EQ_EXPR:
2726       return NE_EXPR;
2727     case NE_EXPR:
2728       return EQ_EXPR;
2729     case GT_EXPR:
2730       return honor_nans ? UNLE_EXPR : LE_EXPR;
2731     case GE_EXPR:
2732       return honor_nans ? UNLT_EXPR : LT_EXPR;
2733     case LT_EXPR:
2734       return honor_nans ? UNGE_EXPR : GE_EXPR;
2735     case LE_EXPR:
2736       return honor_nans ? UNGT_EXPR : GT_EXPR;
2737     case LTGT_EXPR:
2738       return UNEQ_EXPR;
2739     case UNEQ_EXPR:
2740       return LTGT_EXPR;
2741     case UNGT_EXPR:
2742       return LE_EXPR;
2743     case UNGE_EXPR:
2744       return LT_EXPR;
2745     case UNLT_EXPR:
2746       return GE_EXPR;
2747     case UNLE_EXPR:
2748       return GT_EXPR;
2749     case ORDERED_EXPR:
2750       return UNORDERED_EXPR;
2751     case UNORDERED_EXPR:
2752       return ORDERED_EXPR;
2753     default:
2754       gcc_unreachable ();
2755     }
2756 }
2757
2758 /* Similar, but return the comparison that results if the operands are
2759    swapped.  This is safe for floating-point.  */
2760
2761 enum tree_code
2762 swap_tree_comparison (enum tree_code code)
2763 {
2764   switch (code)
2765     {
2766     case EQ_EXPR:
2767     case NE_EXPR:
2768     case ORDERED_EXPR:
2769     case UNORDERED_EXPR:
2770     case LTGT_EXPR:
2771     case UNEQ_EXPR:
2772       return code;
2773     case GT_EXPR:
2774       return LT_EXPR;
2775     case GE_EXPR:
2776       return LE_EXPR;
2777     case LT_EXPR:
2778       return GT_EXPR;
2779     case LE_EXPR:
2780       return GE_EXPR;
2781     case UNGT_EXPR:
2782       return UNLT_EXPR;
2783     case UNGE_EXPR:
2784       return UNLE_EXPR;
2785     case UNLT_EXPR:
2786       return UNGT_EXPR;
2787     case UNLE_EXPR:
2788       return UNGE_EXPR;
2789     default:
2790       gcc_unreachable ();
2791     }
2792 }
2793
2794
2795 /* Convert a comparison tree code from an enum tree_code representation
2796    into a compcode bit-based encoding.  This function is the inverse of
2797    compcode_to_comparison.  */
2798
2799 static enum comparison_code
2800 comparison_to_compcode (enum tree_code code)
2801 {
2802   switch (code)
2803     {
2804     case LT_EXPR:
2805       return COMPCODE_LT;
2806     case EQ_EXPR:
2807       return COMPCODE_EQ;
2808     case LE_EXPR:
2809       return COMPCODE_LE;
2810     case GT_EXPR:
2811       return COMPCODE_GT;
2812     case NE_EXPR:
2813       return COMPCODE_NE;
2814     case GE_EXPR:
2815       return COMPCODE_GE;
2816     case ORDERED_EXPR:
2817       return COMPCODE_ORD;
2818     case UNORDERED_EXPR:
2819       return COMPCODE_UNORD;
2820     case UNLT_EXPR:
2821       return COMPCODE_UNLT;
2822     case UNEQ_EXPR:
2823       return COMPCODE_UNEQ;
2824     case UNLE_EXPR:
2825       return COMPCODE_UNLE;
2826     case UNGT_EXPR:
2827       return COMPCODE_UNGT;
2828     case LTGT_EXPR:
2829       return COMPCODE_LTGT;
2830     case UNGE_EXPR:
2831       return COMPCODE_UNGE;
2832     default:
2833       gcc_unreachable ();
2834     }
2835 }
2836
2837 /* Convert a compcode bit-based encoding of a comparison operator back
2838    to GCC's enum tree_code representation.  This function is the
2839    inverse of comparison_to_compcode.  */
2840
2841 static enum tree_code
2842 compcode_to_comparison (enum comparison_code code)
2843 {
2844   switch (code)
2845     {
2846     case COMPCODE_LT:
2847       return LT_EXPR;
2848     case COMPCODE_EQ:
2849       return EQ_EXPR;
2850     case COMPCODE_LE:
2851       return LE_EXPR;
2852     case COMPCODE_GT:
2853       return GT_EXPR;
2854     case COMPCODE_NE:
2855       return NE_EXPR;
2856     case COMPCODE_GE:
2857       return GE_EXPR;
2858     case COMPCODE_ORD:
2859       return ORDERED_EXPR;
2860     case COMPCODE_UNORD:
2861       return UNORDERED_EXPR;
2862     case COMPCODE_UNLT:
2863       return UNLT_EXPR;
2864     case COMPCODE_UNEQ:
2865       return UNEQ_EXPR;
2866     case COMPCODE_UNLE:
2867       return UNLE_EXPR;
2868     case COMPCODE_UNGT:
2869       return UNGT_EXPR;
2870     case COMPCODE_LTGT:
2871       return LTGT_EXPR;
2872     case COMPCODE_UNGE:
2873       return UNGE_EXPR;
2874     default:
2875       gcc_unreachable ();
2876     }
2877 }
2878
2879 /* Return a tree for the comparison which is the combination of
2880    doing the AND or OR (depending on CODE) of the two operations LCODE
2881    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2882    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2883    if this makes the transformation invalid.  */
2884
2885 tree
2886 combine_comparisons (enum tree_code code, enum tree_code lcode,
2887                      enum tree_code rcode, tree truth_type,
2888                      tree ll_arg, tree lr_arg)
2889 {
2890   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2891   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2892   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2893   enum comparison_code compcode;
2894
2895   switch (code)
2896     {
2897     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2898       compcode = lcompcode & rcompcode;
2899       break;
2900
2901     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2902       compcode = lcompcode | rcompcode;
2903       break;
2904
2905     default:
2906       return NULL_TREE;
2907     }
2908
2909   if (!honor_nans)
2910     {
2911       /* Eliminate unordered comparisons, as well as LTGT and ORD
2912          which are not used unless the mode has NaNs.  */
2913       compcode &= ~COMPCODE_UNORD;
2914       if (compcode == COMPCODE_LTGT)
2915         compcode = COMPCODE_NE;
2916       else if (compcode == COMPCODE_ORD)
2917         compcode = COMPCODE_TRUE;
2918     }
2919    else if (flag_trapping_math)
2920      {
2921         /* Check that the original operation and the optimized ones will trap
2922            under the same condition.  */
2923         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2924                      && (lcompcode != COMPCODE_EQ)
2925                      && (lcompcode != COMPCODE_ORD);
2926         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2927                      && (rcompcode != COMPCODE_EQ)
2928                      && (rcompcode != COMPCODE_ORD);
2929         bool trap = (compcode & COMPCODE_UNORD) == 0
2930                     && (compcode != COMPCODE_EQ)
2931                     && (compcode != COMPCODE_ORD);
2932
2933         /* In a short-circuited boolean expression the LHS might be
2934            such that the RHS, if evaluated, will never trap.  For
2935            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2936            if neither x nor y is NaN.  (This is a mixed blessing: for
2937            example, the expression above will never trap, hence
2938            optimizing it to x < y would be invalid).  */
2939         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2940             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2941           rtrap = false;
2942
2943         /* If the comparison was short-circuited, and only the RHS
2944            trapped, we may now generate a spurious trap.  */
2945         if (rtrap && !ltrap
2946             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2947           return NULL_TREE;
2948
2949         /* If we changed the conditions that cause a trap, we lose.  */
2950         if ((ltrap || rtrap) != trap)
2951           return NULL_TREE;
2952       }
2953
2954   if (compcode == COMPCODE_TRUE)
2955     return constant_boolean_node (true, truth_type);
2956   else if (compcode == COMPCODE_FALSE)
2957     return constant_boolean_node (false, truth_type);
2958   else
2959     return fold_build2 (compcode_to_comparison (compcode),
2960                         truth_type, ll_arg, lr_arg);
2961 }
2962 \f
2963 /* Return nonzero if two operands (typically of the same tree node)
2964    are necessarily equal.  If either argument has side-effects this
2965    function returns zero.  FLAGS modifies behavior as follows:
2966
2967    If OEP_ONLY_CONST is set, only return nonzero for constants.
2968    This function tests whether the operands are indistinguishable;
2969    it does not test whether they are equal using C's == operation.
2970    The distinction is important for IEEE floating point, because
2971    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2972    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2973
2974    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2975    even though it may hold multiple values during a function.
2976    This is because a GCC tree node guarantees that nothing else is
2977    executed between the evaluation of its "operands" (which may often
2978    be evaluated in arbitrary order).  Hence if the operands themselves
2979    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2980    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2981    unset means assuming isochronic (or instantaneous) tree equivalence.
2982    Unless comparing arbitrary expression trees, such as from different
2983    statements, this flag can usually be left unset.
2984
2985    If OEP_PURE_SAME is set, then pure functions with identical arguments
2986    are considered the same.  It is used when the caller has other ways
2987    to ensure that global memory is unchanged in between.  */
2988
2989 int
2990 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2991 {
2992   /* If either is ERROR_MARK, they aren't equal.  */
2993   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2994     return 0;
2995
2996   /* Check equality of integer constants before bailing out due to
2997      precision differences.  */
2998   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2999     return tree_int_cst_equal (arg0, arg1);
3000
3001   /* If both types don't have the same signedness, then we can't consider
3002      them equal.  We must check this before the STRIP_NOPS calls
3003      because they may change the signedness of the arguments.  As pointers
3004      strictly don't have a signedness, require either two pointers or
3005      two non-pointers as well.  */
3006   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3007       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3008     return 0;
3009
3010   /* If both types don't have the same precision, then it is not safe
3011      to strip NOPs.  */
3012   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3013     return 0;
3014
3015   STRIP_NOPS (arg0);
3016   STRIP_NOPS (arg1);
3017
3018   /* In case both args are comparisons but with different comparison
3019      code, try to swap the comparison operands of one arg to produce
3020      a match and compare that variant.  */
3021   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3022       && COMPARISON_CLASS_P (arg0)
3023       && COMPARISON_CLASS_P (arg1))
3024     {
3025       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3026
3027       if (TREE_CODE (arg0) == swap_code)
3028         return operand_equal_p (TREE_OPERAND (arg0, 0),
3029                                 TREE_OPERAND (arg1, 1), flags)
3030                && operand_equal_p (TREE_OPERAND (arg0, 1),
3031                                    TREE_OPERAND (arg1, 0), flags);
3032     }
3033
3034   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3035       /* This is needed for conversions and for COMPONENT_REF.
3036          Might as well play it safe and always test this.  */
3037       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3038       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3039       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3040     return 0;
3041
3042   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3043      We don't care about side effects in that case because the SAVE_EXPR
3044      takes care of that for us. In all other cases, two expressions are
3045      equal if they have no side effects.  If we have two identical
3046      expressions with side effects that should be treated the same due
3047      to the only side effects being identical SAVE_EXPR's, that will
3048      be detected in the recursive calls below.  */
3049   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3050       && (TREE_CODE (arg0) == SAVE_EXPR
3051           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3052     return 1;
3053
3054   /* Next handle constant cases, those for which we can return 1 even
3055      if ONLY_CONST is set.  */
3056   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3057     switch (TREE_CODE (arg0))
3058       {
3059       case INTEGER_CST:
3060         return tree_int_cst_equal (arg0, arg1);
3061
3062       case FIXED_CST:
3063         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3064                                        TREE_FIXED_CST (arg1));
3065
3066       case REAL_CST:
3067         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3068                                    TREE_REAL_CST (arg1)))
3069           return 1;
3070
3071         
3072         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3073           {
3074             /* If we do not distinguish between signed and unsigned zero,
3075                consider them equal.  */
3076             if (real_zerop (arg0) && real_zerop (arg1))
3077               return 1;
3078           }
3079         return 0;
3080
3081       case VECTOR_CST:
3082         {
3083           tree v1, v2;
3084
3085           v1 = TREE_VECTOR_CST_ELTS (arg0);
3086           v2 = TREE_VECTOR_CST_ELTS (arg1);
3087           while (v1 && v2)
3088             {
3089               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3090                                     flags))
3091                 return 0;
3092               v1 = TREE_CHAIN (v1);
3093               v2 = TREE_CHAIN (v2);
3094             }
3095
3096           return v1 == v2;
3097         }
3098
3099       case COMPLEX_CST:
3100         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3101                                  flags)
3102                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3103                                     flags));
3104
3105       case STRING_CST:
3106         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3107                 && ! memcmp (TREE_STRING_POINTER (arg0),
3108                               TREE_STRING_POINTER (arg1),
3109                               TREE_STRING_LENGTH (arg0)));
3110
3111       case ADDR_EXPR:
3112         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3113                                 0);
3114       default:
3115         break;
3116       }
3117
3118   if (flags & OEP_ONLY_CONST)
3119     return 0;
3120
3121 /* Define macros to test an operand from arg0 and arg1 for equality and a
3122    variant that allows null and views null as being different from any
3123    non-null value.  In the latter case, if either is null, the both
3124    must be; otherwise, do the normal comparison.  */
3125 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3126                                     TREE_OPERAND (arg1, N), flags)
3127
3128 #define OP_SAME_WITH_NULL(N)                            \
3129   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3130    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3131
3132   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3133     {
3134     case tcc_unary:
3135       /* Two conversions are equal only if signedness and modes match.  */
3136       switch (TREE_CODE (arg0))
3137         {
3138         CASE_CONVERT:
3139         case FIX_TRUNC_EXPR:
3140           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3141               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3142             return 0;
3143           break;
3144         default:
3145           break;
3146         }
3147
3148       return OP_SAME (0);
3149
3150
3151     case tcc_comparison:
3152     case tcc_binary:
3153       if (OP_SAME (0) && OP_SAME (1))
3154         return 1;
3155
3156       /* For commutative ops, allow the other order.  */
3157       return (commutative_tree_code (TREE_CODE (arg0))
3158               && operand_equal_p (TREE_OPERAND (arg0, 0),
3159                                   TREE_OPERAND (arg1, 1), flags)
3160               && operand_equal_p (TREE_OPERAND (arg0, 1),
3161                                   TREE_OPERAND (arg1, 0), flags));
3162
3163     case tcc_reference:
3164       /* If either of the pointer (or reference) expressions we are
3165          dereferencing contain a side effect, these cannot be equal.  */
3166       if (TREE_SIDE_EFFECTS (arg0)
3167           || TREE_SIDE_EFFECTS (arg1))
3168         return 0;
3169
3170       switch (TREE_CODE (arg0))
3171         {
3172         case INDIRECT_REF:
3173         case ALIGN_INDIRECT_REF:
3174         case MISALIGNED_INDIRECT_REF:
3175         case REALPART_EXPR:
3176         case IMAGPART_EXPR:
3177           return OP_SAME (0);
3178
3179         case ARRAY_REF:
3180         case ARRAY_RANGE_REF:
3181           /* Operands 2 and 3 may be null.
3182              Compare the array index by value if it is constant first as we
3183              may have different types but same value here.  */
3184           return (OP_SAME (0)
3185                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3186                                           TREE_OPERAND (arg1, 1))
3187                       || OP_SAME (1))
3188                   && OP_SAME_WITH_NULL (2)
3189                   && OP_SAME_WITH_NULL (3));
3190
3191         case COMPONENT_REF:
3192           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3193              may be NULL when we're called to compare MEM_EXPRs.  */
3194           return OP_SAME_WITH_NULL (0)
3195                  && OP_SAME (1)
3196                  && OP_SAME_WITH_NULL (2);
3197
3198         case BIT_FIELD_REF:
3199           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3200
3201         default:
3202           return 0;
3203         }
3204
3205     case tcc_expression:
3206       switch (TREE_CODE (arg0))
3207         {
3208         case ADDR_EXPR:
3209         case TRUTH_NOT_EXPR:
3210           return OP_SAME (0);
3211
3212         case TRUTH_ANDIF_EXPR:
3213         case TRUTH_ORIF_EXPR:
3214           return OP_SAME (0) && OP_SAME (1);
3215
3216         case TRUTH_AND_EXPR:
3217         case TRUTH_OR_EXPR:
3218         case TRUTH_XOR_EXPR:
3219           if (OP_SAME (0) && OP_SAME (1))
3220             return 1;
3221
3222           /* Otherwise take into account this is a commutative operation.  */
3223           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3224                                    TREE_OPERAND (arg1, 1), flags)
3225                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3226                                       TREE_OPERAND (arg1, 0), flags));
3227
3228         case COND_EXPR:
3229           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3230           
3231         default:
3232           return 0;
3233         }
3234
3235     case tcc_vl_exp:
3236       switch (TREE_CODE (arg0))
3237         {
3238         case CALL_EXPR:
3239           /* If the CALL_EXPRs call different functions, then they
3240              clearly can not be equal.  */
3241           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3242                                  flags))
3243             return 0;
3244
3245           {
3246             unsigned int cef = call_expr_flags (arg0);
3247             if (flags & OEP_PURE_SAME)
3248               cef &= ECF_CONST | ECF_PURE;
3249             else
3250               cef &= ECF_CONST;
3251             if (!cef)
3252               return 0;
3253           }
3254
3255           /* Now see if all the arguments are the same.  */
3256           {
3257             const_call_expr_arg_iterator iter0, iter1;
3258             const_tree a0, a1;
3259             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3260                    a1 = first_const_call_expr_arg (arg1, &iter1);
3261                  a0 && a1;
3262                  a0 = next_const_call_expr_arg (&iter0),
3263                    a1 = next_const_call_expr_arg (&iter1))
3264               if (! operand_equal_p (a0, a1, flags))
3265                 return 0;
3266
3267             /* If we get here and both argument lists are exhausted
3268                then the CALL_EXPRs are equal.  */
3269             return ! (a0 || a1);
3270           }
3271         default:
3272           return 0;
3273         }
3274
3275     case tcc_declaration:
3276       /* Consider __builtin_sqrt equal to sqrt.  */
3277       return (TREE_CODE (arg0) == FUNCTION_DECL
3278               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3279               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3280               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3281
3282     default:
3283       return 0;
3284     }
3285
3286 #undef OP_SAME
3287 #undef OP_SAME_WITH_NULL
3288 }
3289 \f
3290 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3291    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3292
3293    When in doubt, return 0.  */
3294
3295 static int
3296 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3297 {
3298   int unsignedp1, unsignedpo;
3299   tree primarg0, primarg1, primother;
3300   unsigned int correct_width;
3301
3302   if (operand_equal_p (arg0, arg1, 0))
3303     return 1;
3304
3305   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3306       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3307     return 0;
3308
3309   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3310      and see if the inner values are the same.  This removes any
3311      signedness comparison, which doesn't matter here.  */
3312   primarg0 = arg0, primarg1 = arg1;
3313   STRIP_NOPS (primarg0);
3314   STRIP_NOPS (primarg1);
3315   if (operand_equal_p (primarg0, primarg1, 0))
3316     return 1;
3317
3318   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3319      actual comparison operand, ARG0.
3320
3321      First throw away any conversions to wider types
3322      already present in the operands.  */
3323
3324   primarg1 = get_narrower (arg1, &unsignedp1);
3325   primother = get_narrower (other, &unsignedpo);
3326
3327   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3328   if (unsignedp1 == unsignedpo
3329       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3330       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3331     {
3332       tree type = TREE_TYPE (arg0);
3333
3334       /* Make sure shorter operand is extended the right way
3335          to match the longer operand.  */
3336       primarg1 = fold_convert (signed_or_unsigned_type_for
3337                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3338
3339       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3340         return 1;
3341     }
3342
3343   return 0;
3344 }
3345 \f
3346 /* See if ARG is an expression that is either a comparison or is performing
3347    arithmetic on comparisons.  The comparisons must only be comparing
3348    two different values, which will be stored in *CVAL1 and *CVAL2; if
3349    they are nonzero it means that some operands have already been found.
3350    No variables may be used anywhere else in the expression except in the
3351    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3352    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3353
3354    If this is true, return 1.  Otherwise, return zero.  */
3355
3356 static int
3357 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3358 {
3359   enum tree_code code = TREE_CODE (arg);
3360   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3361
3362   /* We can handle some of the tcc_expression cases here.  */
3363   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3364     tclass = tcc_unary;
3365   else if (tclass == tcc_expression
3366            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3367                || code == COMPOUND_EXPR))
3368     tclass = tcc_binary;
3369
3370   else if (tclass == tcc_expression && code == SAVE_EXPR
3371            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3372     {
3373       /* If we've already found a CVAL1 or CVAL2, this expression is
3374          two complex to handle.  */
3375       if (*cval1 || *cval2)
3376         return 0;
3377
3378       tclass = tcc_unary;
3379       *save_p = 1;
3380     }
3381
3382   switch (tclass)
3383     {
3384     case tcc_unary:
3385       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3386
3387     case tcc_binary:
3388       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3389               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3390                                       cval1, cval2, save_p));
3391
3392     case tcc_constant:
3393       return 1;
3394
3395     case tcc_expression:
3396       if (code == COND_EXPR)
3397         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3398                                      cval1, cval2, save_p)
3399                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3400                                         cval1, cval2, save_p)
3401                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3402                                         cval1, cval2, save_p));
3403       return 0;
3404
3405     case tcc_comparison:
3406       /* First see if we can handle the first operand, then the second.  For
3407          the second operand, we know *CVAL1 can't be zero.  It must be that
3408          one side of the comparison is each of the values; test for the
3409          case where this isn't true by failing if the two operands
3410          are the same.  */
3411
3412       if (operand_equal_p (TREE_OPERAND (arg, 0),
3413                            TREE_OPERAND (arg, 1), 0))
3414         return 0;
3415
3416       if (*cval1 == 0)
3417         *cval1 = TREE_OPERAND (arg, 0);
3418       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3419         ;
3420       else if (*cval2 == 0)
3421         *cval2 = TREE_OPERAND (arg, 0);
3422       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3423         ;
3424       else
3425         return 0;
3426
3427       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3428         ;
3429       else if (*cval2 == 0)
3430         *cval2 = TREE_OPERAND (arg, 1);
3431       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3432         ;
3433       else
3434         return 0;
3435
3436       return 1;
3437
3438     default:
3439       return 0;
3440     }
3441 }
3442 \f
3443 /* ARG is a tree that is known to contain just arithmetic operations and
3444    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3445    any occurrence of OLD0 as an operand of a comparison and likewise for
3446    NEW1 and OLD1.  */
3447
3448 static tree
3449 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3450 {
3451   tree type = TREE_TYPE (arg);
3452   enum tree_code code = TREE_CODE (arg);
3453   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3454
3455   /* We can handle some of the tcc_expression cases here.  */
3456   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3457     tclass = tcc_unary;
3458   else if (tclass == tcc_expression
3459            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3460     tclass = tcc_binary;
3461
3462   switch (tclass)
3463     {
3464     case tcc_unary:
3465       return fold_build1 (code, type,
3466                           eval_subst (TREE_OPERAND (arg, 0),
3467                                       old0, new0, old1, new1));
3468
3469     case tcc_binary:
3470       return fold_build2 (code, type,
3471                           eval_subst (TREE_OPERAND (arg, 0),
3472                                       old0, new0, old1, new1),
3473                           eval_subst (TREE_OPERAND (arg, 1),
3474                                       old0, new0, old1, new1));
3475
3476     case tcc_expression:
3477       switch (code)
3478         {
3479         case SAVE_EXPR:
3480           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3481
3482         case COMPOUND_EXPR:
3483           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3484
3485         case COND_EXPR:
3486           return fold_build3 (code, type,
3487                               eval_subst (TREE_OPERAND (arg, 0),
3488                                           old0, new0, old1, new1),
3489                               eval_subst (TREE_OPERAND (arg, 1),
3490                                           old0, new0, old1, new1),
3491                               eval_subst (TREE_OPERAND (arg, 2),
3492                                           old0, new0, old1, new1));
3493         default:
3494           break;
3495         }
3496       /* Fall through - ???  */
3497
3498     case tcc_comparison:
3499       {
3500         tree arg0 = TREE_OPERAND (arg, 0);
3501         tree arg1 = TREE_OPERAND (arg, 1);
3502
3503         /* We need to check both for exact equality and tree equality.  The
3504            former will be true if the operand has a side-effect.  In that
3505            case, we know the operand occurred exactly once.  */
3506
3507         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3508           arg0 = new0;
3509         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3510           arg0 = new1;
3511
3512         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3513           arg1 = new0;
3514         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3515           arg1 = new1;
3516
3517         return fold_build2 (code, type, arg0, arg1);
3518       }
3519
3520     default:
3521       return arg;
3522     }
3523 }
3524 \f
3525 /* Return a tree for the case when the result of an expression is RESULT
3526    converted to TYPE and OMITTED was previously an operand of the expression
3527    but is now not needed (e.g., we folded OMITTED * 0).
3528
3529    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3530    the conversion of RESULT to TYPE.  */
3531
3532 tree
3533 omit_one_operand (tree type, tree result, tree omitted)
3534 {
3535   tree t = fold_convert (type, result);
3536
3537   /* If the resulting operand is an empty statement, just return the omitted
3538      statement casted to void. */
3539   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3540     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3541
3542   if (TREE_SIDE_EFFECTS (omitted))
3543     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3544
3545   return non_lvalue (t);
3546 }
3547
3548 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3549
3550 static tree
3551 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3552 {
3553   tree t = fold_convert (type, result);
3554
3555   /* If the resulting operand is an empty statement, just return the omitted
3556      statement casted to void. */
3557   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3558     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3559
3560   if (TREE_SIDE_EFFECTS (omitted))
3561     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3562
3563   return pedantic_non_lvalue (t);
3564 }
3565
3566 /* Return a tree for the case when the result of an expression is RESULT
3567    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3568    of the expression but are now not needed.
3569
3570    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3571    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3572    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3573    just do the conversion of RESULT to TYPE.  */
3574
3575 tree
3576 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3577 {
3578   tree t = fold_convert (type, result);
3579
3580   if (TREE_SIDE_EFFECTS (omitted2))
3581     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3582   if (TREE_SIDE_EFFECTS (omitted1))
3583     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3584
3585   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3586 }
3587
3588 \f
3589 /* Return a simplified tree node for the truth-negation of ARG.  This
3590    never alters ARG itself.  We assume that ARG is an operation that
3591    returns a truth value (0 or 1).
3592
3593    FIXME: one would think we would fold the result, but it causes
3594    problems with the dominator optimizer.  */
3595
3596 tree
3597 fold_truth_not_expr (tree arg)
3598 {
3599   tree type = TREE_TYPE (arg);
3600   enum tree_code code = TREE_CODE (arg);
3601
3602   /* If this is a comparison, we can simply invert it, except for
3603      floating-point non-equality comparisons, in which case we just
3604      enclose a TRUTH_NOT_EXPR around what we have.  */
3605
3606   if (TREE_CODE_CLASS (code) == tcc_comparison)
3607     {
3608       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3609       if (FLOAT_TYPE_P (op_type)
3610           && flag_trapping_math
3611           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3612           && code != NE_EXPR && code != EQ_EXPR)
3613         return NULL_TREE;
3614       else
3615         {
3616           code = invert_tree_comparison (code,
3617                                          HONOR_NANS (TYPE_MODE (op_type)));
3618           if (code == ERROR_MARK)
3619             return NULL_TREE;
3620           else
3621             return build2 (code, type,
3622                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3623         }
3624     }
3625
3626   switch (code)
3627     {
3628     case INTEGER_CST:
3629       return constant_boolean_node (integer_zerop (arg), type);
3630
3631     case TRUTH_AND_EXPR:
3632       return build2 (TRUTH_OR_EXPR, type,
3633                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3634                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3635
3636     case TRUTH_OR_EXPR:
3637       return build2 (TRUTH_AND_EXPR, type,
3638                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3639                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3640
3641     case TRUTH_XOR_EXPR:
3642       /* Here we can invert either operand.  We invert the first operand
3643          unless the second operand is a TRUTH_NOT_EXPR in which case our
3644          result is the XOR of the first operand with the inside of the
3645          negation of the second operand.  */
3646
3647       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3648         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3649                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3650       else
3651         return build2 (TRUTH_XOR_EXPR, type,
3652                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3653                        TREE_OPERAND (arg, 1));
3654
3655     case TRUTH_ANDIF_EXPR:
3656       return build2 (TRUTH_ORIF_EXPR, type,
3657                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3658                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3659
3660     case TRUTH_ORIF_EXPR:
3661       return build2 (TRUTH_ANDIF_EXPR, type,
3662                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3663                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3664
3665     case TRUTH_NOT_EXPR:
3666       return TREE_OPERAND (arg, 0);
3667
3668     case COND_EXPR:
3669       {
3670         tree arg1 = TREE_OPERAND (arg, 1);
3671         tree arg2 = TREE_OPERAND (arg, 2);
3672         /* A COND_EXPR may have a throw as one operand, which
3673            then has void type.  Just leave void operands
3674            as they are.  */
3675         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3676                        VOID_TYPE_P (TREE_TYPE (arg1))
3677                        ? arg1 : invert_truthvalue (arg1),
3678                        VOID_TYPE_P (TREE_TYPE (arg2))
3679                        ? arg2 : invert_truthvalue (arg2));
3680       }
3681
3682     case COMPOUND_EXPR:
3683       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3684                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3685
3686     case NON_LVALUE_EXPR:
3687       return invert_truthvalue (TREE_OPERAND (arg, 0));
3688
3689     case NOP_EXPR:
3690       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3691         return build1 (TRUTH_NOT_EXPR, type, arg);
3692
3693     case CONVERT_EXPR:
3694     case FLOAT_EXPR:
3695       return build1 (TREE_CODE (arg), type,
3696                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3697
3698     case BIT_AND_EXPR:
3699       if (!integer_onep (TREE_OPERAND (arg, 1)))
3700         break;
3701       return build2 (EQ_EXPR, type, arg,
3702                      build_int_cst (type, 0));
3703
3704     case SAVE_EXPR:
3705       return build1 (TRUTH_NOT_EXPR, type, arg);
3706
3707     case CLEANUP_POINT_EXPR:
3708       return build1 (CLEANUP_POINT_EXPR, type,
3709                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3710
3711     default:
3712       break;
3713     }
3714
3715   return NULL_TREE;
3716 }
3717
3718 /* Return a simplified tree node for the truth-negation of ARG.  This
3719    never alters ARG itself.  We assume that ARG is an operation that
3720    returns a truth value (0 or 1).
3721
3722    FIXME: one would think we would fold the result, but it causes
3723    problems with the dominator optimizer.  */
3724
3725 tree
3726 invert_truthvalue (tree arg)
3727 {
3728   tree tem;
3729
3730   if (TREE_CODE (arg) == ERROR_MARK)
3731     return arg;
3732
3733   tem = fold_truth_not_expr (arg);
3734   if (!tem)
3735     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3736
3737   return tem;
3738 }
3739
3740 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3741    operands are another bit-wise operation with a common input.  If so,
3742    distribute the bit operations to save an operation and possibly two if
3743    constants are involved.  For example, convert
3744         (A | B) & (A | C) into A | (B & C)
3745    Further simplification will occur if B and C are constants.
3746
3747    If this optimization cannot be done, 0 will be returned.  */
3748
3749 static tree
3750 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3751 {
3752   tree common;
3753   tree left, right;
3754
3755   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3756       || TREE_CODE (arg0) == code
3757       || (TREE_CODE (arg0) != BIT_AND_EXPR
3758           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3759     return 0;
3760
3761   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3762     {
3763       common = TREE_OPERAND (arg0, 0);
3764       left = TREE_OPERAND (arg0, 1);
3765       right = TREE_OPERAND (arg1, 1);
3766     }
3767   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3768     {
3769       common = TREE_OPERAND (arg0, 0);
3770       left = TREE_OPERAND (arg0, 1);
3771       right = TREE_OPERAND (arg1, 0);
3772     }
3773   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3774     {
3775       common = TREE_OPERAND (arg0, 1);
3776       left = TREE_OPERAND (arg0, 0);
3777       right = TREE_OPERAND (arg1, 1);
3778     }
3779   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3780     {
3781       common = TREE_OPERAND (arg0, 1);
3782       left = TREE_OPERAND (arg0, 0);
3783       right = TREE_OPERAND (arg1, 0);
3784     }
3785   else
3786     return 0;
3787
3788   common = fold_convert (type, common);
3789   left = fold_convert (type, left);
3790   right = fold_convert (type, right);
3791   return fold_build2 (TREE_CODE (arg0), type, common,
3792                       fold_build2 (code, type, left, right));
3793 }
3794
3795 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3796    with code CODE.  This optimization is unsafe.  */
3797 static tree
3798 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3799 {
3800   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3801   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3802
3803   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3804   if (mul0 == mul1
3805       && operand_equal_p (TREE_OPERAND (arg0, 1),
3806                        TREE_OPERAND (arg1, 1), 0))
3807     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3808                         fold_build2 (code, type,
3809                                      TREE_OPERAND (arg0, 0),
3810                                      TREE_OPERAND (arg1, 0)),
3811                         TREE_OPERAND (arg0, 1));
3812
3813   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3814   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3815                        TREE_OPERAND (arg1, 0), 0)
3816       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3817       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3818     {
3819       REAL_VALUE_TYPE r0, r1;
3820       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3821       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3822       if (!mul0)
3823         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3824       if (!mul1)
3825         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3826       real_arithmetic (&r0, code, &r0, &r1);
3827       return fold_build2 (MULT_EXPR, type,
3828                           TREE_OPERAND (arg0, 0),
3829                           build_real (type, r0));
3830     }
3831
3832   return NULL_TREE;
3833 }
3834 \f
3835 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3836    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3837
3838 static tree
3839 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3840                     HOST_WIDE_INT bitpos, int unsignedp)
3841 {
3842   tree result, bftype;
3843
3844   if (bitpos == 0)
3845     {
3846       tree size = TYPE_SIZE (TREE_TYPE (inner));
3847       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3848            || POINTER_TYPE_P (TREE_TYPE (inner)))
3849           && host_integerp (size, 0) 
3850           && tree_low_cst (size, 0) == bitsize)
3851         return fold_convert (type, inner);
3852     }
3853
3854   bftype = type;
3855   if (TYPE_PRECISION (bftype) != bitsize
3856       || TYPE_UNSIGNED (bftype) == !unsignedp)
3857     bftype = build_nonstandard_integer_type (bitsize, 0);
3858
3859   result = build3 (BIT_FIELD_REF, bftype, inner,
3860                    size_int (bitsize), bitsize_int (bitpos));
3861
3862   if (bftype != type)
3863     result = fold_convert (type, result);
3864
3865   return result;
3866 }
3867
3868 /* Optimize a bit-field compare.
3869
3870    There are two cases:  First is a compare against a constant and the
3871    second is a comparison of two items where the fields are at the same
3872    bit position relative to the start of a chunk (byte, halfword, word)
3873    large enough to contain it.  In these cases we can avoid the shift
3874    implicit in bitfield extractions.
3875
3876    For constants, we emit a compare of the shifted constant with the
3877    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3878    compared.  For two fields at the same position, we do the ANDs with the
3879    similar mask and compare the result of the ANDs.
3880
3881    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3882    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3883    are the left and right operands of the comparison, respectively.
3884
3885    If the optimization described above can be done, we return the resulting
3886    tree.  Otherwise we return zero.  */
3887
3888 static tree
3889 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3890                             tree lhs, tree rhs)
3891 {
3892   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3893   tree type = TREE_TYPE (lhs);
3894   tree signed_type, unsigned_type;
3895   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3896   enum machine_mode lmode, rmode, nmode;
3897   int lunsignedp, runsignedp;
3898   int lvolatilep = 0, rvolatilep = 0;
3899   tree linner, rinner = NULL_TREE;
3900   tree mask;
3901   tree offset;
3902
3903   /* Get all the information about the extractions being done.  If the bit size
3904      if the same as the size of the underlying object, we aren't doing an
3905      extraction at all and so can do nothing.  We also don't want to
3906      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3907      then will no longer be able to replace it.  */
3908   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3909                                 &lunsignedp, &lvolatilep, false);
3910   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3911       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3912     return 0;
3913
3914  if (!const_p)
3915    {
3916      /* If this is not a constant, we can only do something if bit positions,
3917         sizes, and signedness are the same.  */
3918      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3919                                    &runsignedp, &rvolatilep, false);
3920
3921      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3922          || lunsignedp != runsignedp || offset != 0
3923          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3924        return 0;
3925    }
3926
3927   /* See if we can find a mode to refer to this field.  We should be able to,
3928      but fail if we can't.  */
3929   nmode = get_best_mode (lbitsize, lbitpos,
3930                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3931                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3932                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3933                          word_mode, lvolatilep || rvolatilep);
3934   if (nmode == VOIDmode)
3935     return 0;
3936
3937   /* Set signed and unsigned types of the precision of this mode for the
3938      shifts below.  */
3939   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3940   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3941
3942   /* Compute the bit position and size for the new reference and our offset
3943      within it. If the new reference is the same size as the original, we
3944      won't optimize anything, so return zero.  */
3945   nbitsize = GET_MODE_BITSIZE (nmode);
3946   nbitpos = lbitpos & ~ (nbitsize - 1);
3947   lbitpos -= nbitpos;
3948   if (nbitsize == lbitsize)
3949     return 0;
3950
3951   if (BYTES_BIG_ENDIAN)
3952     lbitpos = nbitsize - lbitsize - lbitpos;
3953
3954   /* Make the mask to be used against the extracted field.  */
3955   mask = build_int_cst_type (unsigned_type, -1);
3956   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3957   mask = const_binop (RSHIFT_EXPR, mask,
3958                       size_int (nbitsize - lbitsize - lbitpos), 0);
3959
3960   if (! const_p)
3961     /* If not comparing with constant, just rework the comparison
3962        and return.  */
3963     return fold_build2 (code, compare_type,
3964                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3965                                      make_bit_field_ref (linner,
3966                                                          unsigned_type,
3967                                                          nbitsize, nbitpos,
3968                                                          1),
3969                                      mask),
3970                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3971                                      make_bit_field_ref (rinner,
3972                                                          unsigned_type,
3973                                                          nbitsize, nbitpos,
3974                                                          1),
3975                                      mask));
3976
3977   /* Otherwise, we are handling the constant case. See if the constant is too
3978      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3979      this not only for its own sake, but to avoid having to test for this
3980      error case below.  If we didn't, we might generate wrong code.
3981
3982      For unsigned fields, the constant shifted right by the field length should
3983      be all zero.  For signed fields, the high-order bits should agree with
3984      the sign bit.  */
3985
3986   if (lunsignedp)
3987     {
3988       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3989                                         fold_convert (unsigned_type, rhs),
3990                                         size_int (lbitsize), 0)))
3991         {
3992           warning (0, "comparison is always %d due to width of bit-field",
3993                    code == NE_EXPR);
3994           return constant_boolean_node (code == NE_EXPR, compare_type);
3995         }
3996     }
3997   else
3998     {
3999       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4000                               size_int (lbitsize - 1), 0);
4001       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4002         {
4003           warning (0, "comparison is always %d due to width of bit-field",
4004                    code == NE_EXPR);
4005           return constant_boolean_node (code == NE_EXPR, compare_type);
4006         }
4007     }
4008
4009   /* Single-bit compares should always be against zero.  */
4010   if (lbitsize == 1 && ! integer_zerop (rhs))
4011     {
4012       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4013       rhs = build_int_cst (type, 0);
4014     }
4015
4016   /* Make a new bitfield reference, shift the constant over the
4017      appropriate number of bits and mask it with the computed mask
4018      (in case this was a signed field).  If we changed it, make a new one.  */
4019   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4020   if (lvolatilep)
4021     {
4022       TREE_SIDE_EFFECTS (lhs) = 1;
4023       TREE_THIS_VOLATILE (lhs) = 1;
4024     }
4025
4026   rhs = const_binop (BIT_AND_EXPR,
4027                      const_binop (LSHIFT_EXPR,
4028                                   fold_convert (unsigned_type, rhs),
4029                                   size_int (lbitpos), 0),
4030                      mask, 0);
4031
4032   return build2 (code, compare_type,
4033                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4034                  rhs);
4035 }
4036 \f
4037 /* Subroutine for fold_truthop: decode a field reference.
4038
4039    If EXP is a comparison reference, we return the innermost reference.
4040
4041    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4042    set to the starting bit number.
4043
4044    If the innermost field can be completely contained in a mode-sized
4045    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4046
4047    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4048    otherwise it is not changed.
4049
4050    *PUNSIGNEDP is set to the signedness of the field.
4051
4052    *PMASK is set to the mask used.  This is either contained in a
4053    BIT_AND_EXPR or derived from the width of the field.
4054
4055    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4056
4057    Return 0 if this is not a component reference or is one that we can't
4058    do anything with.  */
4059
4060 static tree
4061 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4062                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4063                         int *punsignedp, int *pvolatilep,
4064                         tree *pmask, tree *pand_mask)
4065 {
4066   tree outer_type = 0;
4067   tree and_mask = 0;
4068   tree mask, inner, offset;
4069   tree unsigned_type;
4070   unsigned int precision;
4071
4072   /* All the optimizations using this function assume integer fields.
4073      There are problems with FP fields since the type_for_size call
4074      below can fail for, e.g., XFmode.  */
4075   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4076     return 0;
4077
4078   /* We are interested in the bare arrangement of bits, so strip everything
4079      that doesn't affect the machine mode.  However, record the type of the
4080      outermost expression if it may matter below.  */
4081   if (CONVERT_EXPR_P (exp)
4082       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4083     outer_type = TREE_TYPE (exp);
4084   STRIP_NOPS (exp);
4085
4086   if (TREE_CODE (exp) == BIT_AND_EXPR)
4087     {
4088       and_mask = TREE_OPERAND (exp, 1);
4089       exp = TREE_OPERAND (exp, 0);
4090       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4091       if (TREE_CODE (and_mask) != INTEGER_CST)
4092         return 0;
4093     }
4094
4095   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4096                                punsignedp, pvolatilep, false);
4097   if ((inner == exp && and_mask == 0)
4098       || *pbitsize < 0 || offset != 0
4099       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4100     return 0;
4101
4102   /* If the number of bits in the reference is the same as the bitsize of
4103      the outer type, then the outer type gives the signedness. Otherwise
4104      (in case of a small bitfield) the signedness is unchanged.  */
4105   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4106     *punsignedp = TYPE_UNSIGNED (outer_type);
4107
4108   /* Compute the mask to access the bitfield.  */
4109   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4110   precision = TYPE_PRECISION (unsigned_type);
4111
4112   mask = build_int_cst_type (unsigned_type, -1);
4113
4114   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4115   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4116
4117   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4118   if (and_mask != 0)
4119     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4120                         fold_convert (unsigned_type, and_mask), mask);
4121
4122   *pmask = mask;
4123   *pand_mask = and_mask;
4124   return inner;
4125 }
4126
4127 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4128    bit positions.  */
4129
4130 static int
4131 all_ones_mask_p (const_tree mask, int size)
4132 {
4133   tree type = TREE_TYPE (mask);
4134   unsigned int precision = TYPE_PRECISION (type);
4135   tree tmask;
4136
4137   tmask = build_int_cst_type (signed_type_for (type), -1);
4138
4139   return
4140     tree_int_cst_equal (mask,
4141                         const_binop (RSHIFT_EXPR,
4142                                      const_binop (LSHIFT_EXPR, tmask,
4143                                                   size_int (precision - size),
4144                                                   0),
4145                                      size_int (precision - size), 0));
4146 }
4147
4148 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4149    represents the sign bit of EXP's type.  If EXP represents a sign
4150    or zero extension, also test VAL against the unextended type.
4151    The return value is the (sub)expression whose sign bit is VAL,
4152    or NULL_TREE otherwise.  */
4153
4154 static tree
4155 sign_bit_p (tree exp, const_tree val)
4156 {
4157   unsigned HOST_WIDE_INT mask_lo, lo;
4158   HOST_WIDE_INT mask_hi, hi;
4159   int width;
4160   tree t;
4161
4162   /* Tree EXP must have an integral type.  */
4163   t = TREE_TYPE (exp);
4164   if (! INTEGRAL_TYPE_P (t))
4165     return NULL_TREE;
4166
4167   /* Tree VAL must be an integer constant.  */
4168   if (TREE_CODE (val) != INTEGER_CST
4169       || TREE_OVERFLOW (val))
4170     return NULL_TREE;
4171
4172   width = TYPE_PRECISION (t);
4173   if (width > HOST_BITS_PER_WIDE_INT)
4174     {
4175       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4176       lo = 0;
4177
4178       mask_hi = ((unsigned HOST_WIDE_INT) -1
4179                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4180       mask_lo = -1;
4181     }
4182   else
4183     {
4184       hi = 0;
4185       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4186
4187       mask_hi = 0;
4188       mask_lo = ((unsigned HOST_WIDE_INT) -1
4189                  >> (HOST_BITS_PER_WIDE_INT - width));
4190     }
4191
4192   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4193      treat VAL as if it were unsigned.  */
4194   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4195       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4196     return exp;
4197
4198   /* Handle extension from a narrower type.  */
4199   if (TREE_CODE (exp) == NOP_EXPR
4200       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4201     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4202
4203   return NULL_TREE;
4204 }
4205
4206 /* Subroutine for fold_truthop: determine if an operand is simple enough
4207    to be evaluated unconditionally.  */
4208
4209 static int
4210 simple_operand_p (const_tree exp)
4211 {
4212   /* Strip any conversions that don't change the machine mode.  */
4213   STRIP_NOPS (exp);
4214
4215   return (CONSTANT_CLASS_P (exp)
4216           || TREE_CODE (exp) == SSA_NAME
4217           || (DECL_P (exp)
4218               && ! TREE_ADDRESSABLE (exp)
4219               && ! TREE_THIS_VOLATILE (exp)
4220               && ! DECL_NONLOCAL (exp)
4221               /* Don't regard global variables as simple.  They may be
4222                  allocated in ways unknown to the compiler (shared memory,
4223                  #pragma weak, etc).  */
4224               && ! TREE_PUBLIC (exp)
4225               && ! DECL_EXTERNAL (exp)
4226               /* Loading a static variable is unduly expensive, but global
4227                  registers aren't expensive.  */
4228               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4229 }
4230 \f
4231 /* The following functions are subroutines to fold_range_test and allow it to
4232    try to change a logical combination of comparisons into a range test.
4233
4234    For example, both
4235         X == 2 || X == 3 || X == 4 || X == 5
4236    and
4237         X >= 2 && X <= 5
4238    are converted to
4239         (unsigned) (X - 2) <= 3
4240
4241    We describe each set of comparisons as being either inside or outside
4242    a range, using a variable named like IN_P, and then describe the
4243    range with a lower and upper bound.  If one of the bounds is omitted,
4244    it represents either the highest or lowest value of the type.
4245
4246    In the comments below, we represent a range by two numbers in brackets
4247    preceded by a "+" to designate being inside that range, or a "-" to
4248    designate being outside that range, so the condition can be inverted by
4249    flipping the prefix.  An omitted bound is represented by a "-".  For
4250    example, "- [-, 10]" means being outside the range starting at the lowest
4251    possible value and ending at 10, in other words, being greater than 10.
4252    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4253    always false.
4254
4255    We set up things so that the missing bounds are handled in a consistent
4256    manner so neither a missing bound nor "true" and "false" need to be
4257    handled using a special case.  */
4258
4259 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4260    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4261    and UPPER1_P are nonzero if the respective argument is an upper bound
4262    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4263    must be specified for a comparison.  ARG1 will be converted to ARG0's
4264    type if both are specified.  */
4265
4266 static tree
4267 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4268              tree arg1, int upper1_p)
4269 {
4270   tree tem;
4271   int result;
4272   int sgn0, sgn1;
4273
4274   /* If neither arg represents infinity, do the normal operation.
4275      Else, if not a comparison, return infinity.  Else handle the special
4276      comparison rules. Note that most of the cases below won't occur, but
4277      are handled for consistency.  */
4278
4279   if (arg0 != 0 && arg1 != 0)
4280     {
4281       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4282                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4283       STRIP_NOPS (tem);
4284       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4285     }
4286
4287   if (TREE_CODE_CLASS (code) != tcc_comparison)
4288     return 0;
4289
4290   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4291      for neither.  In real maths, we cannot assume open ended ranges are
4292      the same. But, this is computer arithmetic, where numbers are finite.
4293      We can therefore make the transformation of any unbounded range with
4294      the value Z, Z being greater than any representable number. This permits
4295      us to treat unbounded ranges as equal.  */
4296   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4297   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4298   switch (code)
4299     {
4300     case EQ_EXPR:
4301       result = sgn0 == sgn1;
4302       break;
4303     case NE_EXPR:
4304       result = sgn0 != sgn1;
4305       break;
4306     case LT_EXPR:
4307       result = sgn0 < sgn1;
4308       break;
4309     case LE_EXPR:
4310       result = sgn0 <= sgn1;
4311       break;
4312     case GT_EXPR:
4313       result = sgn0 > sgn1;
4314       break;
4315     case GE_EXPR:
4316       result = sgn0 >= sgn1;
4317       break;
4318     default:
4319       gcc_unreachable ();
4320     }
4321
4322   return constant_boolean_node (result, type);
4323 }
4324 \f
4325 /* Given EXP, a logical expression, set the range it is testing into
4326    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4327    actually being tested.  *PLOW and *PHIGH will be made of the same
4328    type as the returned expression.  If EXP is not a comparison, we
4329    will most likely not be returning a useful value and range.  Set
4330    *STRICT_OVERFLOW_P to true if the return value is only valid
4331    because signed overflow is undefined; otherwise, do not change
4332    *STRICT_OVERFLOW_P.  */
4333
4334 static tree
4335 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4336             bool *strict_overflow_p)
4337 {
4338   enum tree_code code;
4339   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4340   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4341   int in_p, n_in_p;
4342   tree low, high, n_low, n_high;
4343
4344   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4345      and see if we can refine the range.  Some of the cases below may not
4346      happen, but it doesn't seem worth worrying about this.  We "continue"
4347      the outer loop when we've changed something; otherwise we "break"
4348      the switch, which will "break" the while.  */
4349
4350   in_p = 0;
4351   low = high = build_int_cst (TREE_TYPE (exp), 0);
4352
4353   while (1)
4354     {
4355       code = TREE_CODE (exp);
4356       exp_type = TREE_TYPE (exp);
4357
4358       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4359         {
4360           if (TREE_OPERAND_LENGTH (exp) > 0)
4361             arg0 = TREE_OPERAND (exp, 0);
4362           if (TREE_CODE_CLASS (code) == tcc_comparison
4363               || TREE_CODE_CLASS (code) == tcc_unary
4364               || TREE_CODE_CLASS (code) == tcc_binary)
4365             arg0_type = TREE_TYPE (arg0);
4366           if (TREE_CODE_CLASS (code) == tcc_binary
4367               || TREE_CODE_CLASS (code) == tcc_comparison
4368               || (TREE_CODE_CLASS (code) == tcc_expression
4369                   && TREE_OPERAND_LENGTH (exp) > 1))
4370             arg1 = TREE_OPERAND (exp, 1);
4371         }
4372
4373       switch (code)
4374         {
4375         case TRUTH_NOT_EXPR:
4376           in_p = ! in_p, exp = arg0;
4377           continue;
4378
4379         case EQ_EXPR: case NE_EXPR:
4380         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4381           /* We can only do something if the range is testing for zero
4382              and if the second operand is an integer constant.  Note that
4383              saying something is "in" the range we make is done by
4384              complementing IN_P since it will set in the initial case of
4385              being not equal to zero; "out" is leaving it alone.  */
4386           if (low == 0 || high == 0
4387               || ! integer_zerop (low) || ! integer_zerop (high)
4388               || TREE_CODE (arg1) != INTEGER_CST)
4389             break;
4390
4391           switch (code)
4392             {
4393             case NE_EXPR:  /* - [c, c]  */
4394               low = high = arg1;
4395               break;
4396             case EQ_EXPR:  /* + [c, c]  */
4397               in_p = ! in_p, low = high = arg1;
4398               break;
4399             case GT_EXPR:  /* - [-, c] */
4400               low = 0, high = arg1;
4401               break;
4402             case GE_EXPR:  /* + [c, -] */
4403               in_p = ! in_p, low = arg1, high = 0;
4404               break;
4405             case LT_EXPR:  /* - [c, -] */
4406               low = arg1, high = 0;
4407               break;
4408             case LE_EXPR:  /* + [-, c] */
4409               in_p = ! in_p, low = 0, high = arg1;
4410               break;
4411             default:
4412               gcc_unreachable ();
4413             }
4414
4415           /* If this is an unsigned comparison, we also know that EXP is
4416              greater than or equal to zero.  We base the range tests we make
4417              on that fact, so we record it here so we can parse existing
4418              range tests.  We test arg0_type since often the return type
4419              of, e.g. EQ_EXPR, is boolean.  */
4420           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4421             {
4422               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4423                                   in_p, low, high, 1,
4424                                   build_int_cst (arg0_type, 0),
4425                                   NULL_TREE))
4426                 break;
4427
4428               in_p = n_in_p, low = n_low, high = n_high;
4429
4430               /* If the high bound is missing, but we have a nonzero low
4431                  bound, reverse the range so it goes from zero to the low bound
4432                  minus 1.  */
4433               if (high == 0 && low && ! integer_zerop (low))
4434                 {
4435                   in_p = ! in_p;
4436                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4437                                       integer_one_node, 0);
4438                   low = build_int_cst (arg0_type, 0);
4439                 }
4440             }
4441
4442           exp = arg0;
4443           continue;
4444
4445         case NEGATE_EXPR:
4446           /* (-x) IN [a,b] -> x in [-b, -a]  */
4447           n_low = range_binop (MINUS_EXPR, exp_type,
4448                                build_int_cst (exp_type, 0),
4449                                0, high, 1);
4450           n_high = range_binop (MINUS_EXPR, exp_type,
4451                                 build_int_cst (exp_type, 0),
4452                                 0, low, 0);
4453           low = n_low, high = n_high;
4454           exp = arg0;
4455           continue;
4456
4457         case BIT_NOT_EXPR:
4458           /* ~ X -> -X - 1  */
4459           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4460                         build_int_cst (exp_type, 1));
4461           continue;
4462
4463         case PLUS_EXPR:  case MINUS_EXPR:
4464           if (TREE_CODE (arg1) != INTEGER_CST)
4465             break;
4466
4467           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4468              move a constant to the other side.  */
4469           if (!TYPE_UNSIGNED (arg0_type)
4470               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4471             break;
4472
4473           /* If EXP is signed, any overflow in the computation is undefined,
4474              so we don't worry about it so long as our computations on
4475              the bounds don't overflow.  For unsigned, overflow is defined
4476              and this is exactly the right thing.  */
4477           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4478                                arg0_type, low, 0, arg1, 0);
4479           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4480                                 arg0_type, high, 1, arg1, 0);
4481           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4482               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4483             break;
4484
4485           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4486             *strict_overflow_p = true;
4487
4488           /* Check for an unsigned range which has wrapped around the maximum
4489              value thus making n_high < n_low, and normalize it.  */
4490           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4491             {
4492               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4493                                  integer_one_node, 0);
4494               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4495                                   integer_one_node, 0);
4496
4497               /* If the range is of the form +/- [ x+1, x ], we won't
4498                  be able to normalize it.  But then, it represents the
4499                  whole range or the empty set, so make it
4500                  +/- [ -, - ].  */
4501               if (tree_int_cst_equal (n_low, low)
4502                   && tree_int_cst_equal (n_high, high))
4503                 low = high = 0;
4504               else
4505                 in_p = ! in_p;
4506             }
4507           else
4508             low = n_low, high = n_high;
4509
4510           exp = arg0;
4511           continue;
4512
4513         CASE_CONVERT: case NON_LVALUE_EXPR:
4514           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4515             break;
4516
4517           if (! INTEGRAL_TYPE_P (arg0_type)
4518               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4519               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4520             break;
4521
4522           n_low = low, n_high = high;
4523
4524           if (n_low != 0)
4525             n_low = fold_convert (arg0_type, n_low);
4526
4527           if (n_high != 0)
4528             n_high = fold_convert (arg0_type, n_high);
4529
4530
4531           /* If we're converting arg0 from an unsigned type, to exp,
4532              a signed type,  we will be doing the comparison as unsigned.
4533              The tests above have already verified that LOW and HIGH
4534              are both positive.
4535
4536              So we have to ensure that we will handle large unsigned
4537              values the same way that the current signed bounds treat
4538              negative values.  */
4539
4540           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4541             {
4542               tree high_positive;
4543               tree equiv_type;
4544               /* For fixed-point modes, we need to pass the saturating flag
4545                  as the 2nd parameter.  */
4546               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4547                 equiv_type = lang_hooks.types.type_for_mode
4548                              (TYPE_MODE (arg0_type),
4549                               TYPE_SATURATING (arg0_type));
4550               else
4551                 equiv_type = lang_hooks.types.type_for_mode
4552                              (TYPE_MODE (arg0_type), 1);
4553
4554               /* A range without an upper bound is, naturally, unbounded.
4555                  Since convert would have cropped a very large value, use
4556                  the max value for the destination type.  */
4557               high_positive
4558                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4559                 : TYPE_MAX_VALUE (arg0_type);
4560
4561               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4562                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4563                                              fold_convert (arg0_type,
4564                                                            high_positive),
4565                                              build_int_cst (arg0_type, 1));
4566
4567               /* If the low bound is specified, "and" the range with the
4568                  range for which the original unsigned value will be
4569                  positive.  */
4570               if (low != 0)
4571                 {
4572                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4573                                       1, n_low, n_high, 1,
4574                                       fold_convert (arg0_type,
4575                                                     integer_zero_node),
4576                                       high_positive))
4577                     break;
4578
4579                   in_p = (n_in_p == in_p);
4580                 }
4581               else
4582                 {
4583                   /* Otherwise, "or" the range with the range of the input
4584                      that will be interpreted as negative.  */
4585                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4586                                       0, n_low, n_high, 1,
4587                                       fold_convert (arg0_type,
4588                                                     integer_zero_node),
4589                                       high_positive))
4590                     break;
4591
4592                   in_p = (in_p != n_in_p);
4593                 }
4594             }
4595
4596           exp = arg0;
4597           low = n_low, high = n_high;
4598           continue;
4599
4600         default:
4601           break;
4602         }
4603
4604       break;
4605     }
4606
4607   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4608   if (TREE_CODE (exp) == INTEGER_CST)
4609     {
4610       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4611                                                  exp, 0, low, 0))
4612                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4613                                                     exp, 1, high, 1)));
4614       low = high = 0;
4615       exp = 0;
4616     }
4617
4618   *pin_p = in_p, *plow = low, *phigh = high;
4619   return exp;
4620 }
4621 \f
4622 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4623    type, TYPE, return an expression to test if EXP is in (or out of, depending
4624    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4625
4626 static tree
4627 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4628 {
4629   tree etype = TREE_TYPE (exp);
4630   tree value;
4631
4632 #ifdef HAVE_canonicalize_funcptr_for_compare
4633   /* Disable this optimization for function pointer expressions
4634      on targets that require function pointer canonicalization.  */
4635   if (HAVE_canonicalize_funcptr_for_compare
4636       && TREE_CODE (etype) == POINTER_TYPE
4637       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4638     return NULL_TREE;
4639 #endif
4640
4641   if (! in_p)
4642     {
4643       value = build_range_check (type, exp, 1, low, high);
4644       if (value != 0)
4645         return invert_truthvalue (value);
4646
4647       return 0;
4648     }
4649
4650   if (low == 0 && high == 0)
4651     return build_int_cst (type, 1);
4652
4653   if (low == 0)
4654     return fold_build2 (LE_EXPR, type, exp,
4655                         fold_convert (etype, high));
4656
4657   if (high == 0)
4658     return fold_build2 (GE_EXPR, type, exp,
4659                         fold_convert (etype, low));
4660
4661   if (operand_equal_p (low, high, 0))
4662     return fold_build2 (EQ_EXPR, type, exp,
4663                         fold_convert (etype, low));
4664
4665   if (integer_zerop (low))
4666     {
4667       if (! TYPE_UNSIGNED (etype))
4668         {
4669           etype = unsigned_type_for (etype);
4670           high = fold_convert (etype, high);
4671           exp = fold_convert (etype, exp);
4672         }
4673       return build_range_check (type, exp, 1, 0, high);
4674     }
4675
4676   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4677   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4678     {
4679       unsigned HOST_WIDE_INT lo;
4680       HOST_WIDE_INT hi;
4681       int prec;
4682
4683       prec = TYPE_PRECISION (etype);
4684       if (prec <= HOST_BITS_PER_WIDE_INT)
4685         {
4686           hi = 0;
4687           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4688         }
4689       else
4690         {
4691           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4692           lo = (unsigned HOST_WIDE_INT) -1;
4693         }
4694
4695       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4696         {
4697           if (TYPE_UNSIGNED (etype))
4698             {
4699               tree signed_etype = signed_type_for (etype);
4700               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4701                 etype
4702                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4703               else
4704                 etype = signed_etype;
4705               exp = fold_convert (etype, exp);
4706             }
4707           return fold_build2 (GT_EXPR, type, exp,
4708                               build_int_cst (etype, 0));
4709         }
4710     }
4711
4712   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4713      This requires wrap-around arithmetics for the type of the expression.  */
4714   switch (TREE_CODE (etype))
4715     {
4716     case INTEGER_TYPE:
4717       /* There is no requirement that LOW be within the range of ETYPE
4718          if the latter is a subtype.  It must, however, be within the base
4719          type of ETYPE.  So be sure we do the subtraction in that type.  */
4720       if (TREE_TYPE (etype))
4721         etype = TREE_TYPE (etype);
4722       break;
4723
4724     case ENUMERAL_TYPE:
4725     case BOOLEAN_TYPE:
4726       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4727                                               TYPE_UNSIGNED (etype));
4728       break;
4729
4730     default:
4731       break;
4732     }
4733
4734   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4735   if (TREE_CODE (etype) == INTEGER_TYPE
4736       && !TYPE_OVERFLOW_WRAPS (etype))
4737     {
4738       tree utype, minv, maxv;
4739
4740       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4741          for the type in question, as we rely on this here.  */
4742       utype = unsigned_type_for (etype);
4743       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4744       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4745                           integer_one_node, 1);
4746       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4747
4748       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4749                                       minv, 1, maxv, 1)))
4750         etype = utype;
4751       else
4752         return 0;
4753     }
4754
4755   high = fold_convert (etype, high);
4756   low = fold_convert (etype, low);
4757   exp = fold_convert (etype, exp);
4758
4759   value = const_binop (MINUS_EXPR, high, low, 0);
4760
4761
4762   if (POINTER_TYPE_P (etype))
4763     {
4764       if (value != 0 && !TREE_OVERFLOW (value))
4765         {
4766           low = fold_convert (sizetype, low);
4767           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4768           return build_range_check (type,
4769                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4770                                     1, build_int_cst (etype, 0), value);
4771         }
4772       return 0;
4773     }
4774
4775   if (value != 0 && !TREE_OVERFLOW (value))
4776     return build_range_check (type,
4777                               fold_build2 (MINUS_EXPR, etype, exp, low),
4778                               1, build_int_cst (etype, 0), value);
4779
4780   return 0;
4781 }
4782 \f
4783 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4784
4785 static tree
4786 range_predecessor (tree val)
4787 {
4788   tree type = TREE_TYPE (val);
4789
4790   if (INTEGRAL_TYPE_P (type)
4791       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4792     return 0;
4793   else
4794     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4795 }
4796
4797 /* Return the successor of VAL in its type, handling the infinite case.  */
4798
4799 static tree
4800 range_successor (tree val)
4801 {
4802   tree type = TREE_TYPE (val);
4803
4804   if (INTEGRAL_TYPE_P (type)
4805       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4806     return 0;
4807   else
4808     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4809 }
4810
4811 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4812    can, 0 if we can't.  Set the output range into the specified parameters.  */
4813
4814 static int
4815 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4816               tree high0, int in1_p, tree low1, tree high1)
4817 {
4818   int no_overlap;
4819   int subset;
4820   int temp;
4821   tree tem;
4822   int in_p;
4823   tree low, high;
4824   int lowequal = ((low0 == 0 && low1 == 0)
4825                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4826                                                 low0, 0, low1, 0)));
4827   int highequal = ((high0 == 0 && high1 == 0)
4828                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4829                                                  high0, 1, high1, 1)));
4830
4831   /* Make range 0 be the range that starts first, or ends last if they
4832      start at the same value.  Swap them if it isn't.  */
4833   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4834                                  low0, 0, low1, 0))
4835       || (lowequal
4836           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4837                                         high1, 1, high0, 1))))
4838     {
4839       temp = in0_p, in0_p = in1_p, in1_p = temp;
4840       tem = low0, low0 = low1, low1 = tem;
4841       tem = high0, high0 = high1, high1 = tem;
4842     }
4843
4844   /* Now flag two cases, whether the ranges are disjoint or whether the
4845      second range is totally subsumed in the first.  Note that the tests
4846      below are simplified by the ones above.  */
4847   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4848                                           high0, 1, low1, 0));
4849   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4850                                       high1, 1, high0, 1));
4851
4852   /* We now have four cases, depending on whether we are including or
4853      excluding the two ranges.  */
4854   if (in0_p && in1_p)
4855     {
4856       /* If they don't overlap, the result is false.  If the second range
4857          is a subset it is the result.  Otherwise, the range is from the start
4858          of the second to the end of the first.  */
4859       if (no_overlap)
4860         in_p = 0, low = high = 0;
4861       else if (subset)
4862         in_p = 1, low = low1, high = high1;
4863       else
4864         in_p = 1, low = low1, high = high0;
4865     }
4866
4867   else if (in0_p && ! in1_p)
4868     {
4869       /* If they don't overlap, the result is the first range.  If they are
4870          equal, the result is false.  If the second range is a subset of the
4871          first, and the ranges begin at the same place, we go from just after
4872          the end of the second range to the end of the first.  If the second
4873          range is not a subset of the first, or if it is a subset and both
4874          ranges end at the same place, the range starts at the start of the
4875          first range and ends just before the second range.
4876          Otherwise, we can't describe this as a single range.  */
4877       if (no_overlap)
4878         in_p = 1, low = low0, high = high0;
4879       else if (lowequal && highequal)
4880         in_p = 0, low = high = 0;
4881       else if (subset && lowequal)
4882         {
4883           low = range_successor (high1);
4884           high = high0;
4885           in_p = 1;
4886           if (low == 0)
4887             {
4888               /* We are in the weird situation where high0 > high1 but
4889                  high1 has no successor.  Punt.  */
4890               return 0;
4891             }
4892         }
4893       else if (! subset || highequal)
4894         {
4895           low = low0;
4896           high = range_predecessor (low1);
4897           in_p = 1;
4898           if (high == 0)
4899             {
4900               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4901               return 0;
4902             }
4903         }
4904       else
4905         return 0;
4906     }
4907
4908   else if (! in0_p && in1_p)
4909     {
4910       /* If they don't overlap, the result is the second range.  If the second
4911          is a subset of the first, the result is false.  Otherwise,
4912          the range starts just after the first range and ends at the
4913          end of the second.  */
4914       if (no_overlap)
4915         in_p = 1, low = low1, high = high1;
4916       else if (subset || highequal)
4917         in_p = 0, low = high = 0;
4918       else
4919         {
4920           low = range_successor (high0);
4921           high = high1;
4922           in_p = 1;
4923           if (low == 0)
4924             {
4925               /* high1 > high0 but high0 has no successor.  Punt.  */
4926               return 0;
4927             }
4928         }
4929     }
4930
4931   else
4932     {
4933       /* The case where we are excluding both ranges.  Here the complex case
4934          is if they don't overlap.  In that case, the only time we have a
4935          range is if they are adjacent.  If the second is a subset of the
4936          first, the result is the first.  Otherwise, the range to exclude
4937          starts at the beginning of the first range and ends at the end of the
4938          second.  */
4939       if (no_overlap)
4940         {
4941           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4942                                          range_successor (high0),
4943                                          1, low1, 0)))
4944             in_p = 0, low = low0, high = high1;
4945           else
4946             {
4947               /* Canonicalize - [min, x] into - [-, x].  */
4948               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4949                 switch (TREE_CODE (TREE_TYPE (low0)))
4950                   {
4951                   case ENUMERAL_TYPE:
4952                     if (TYPE_PRECISION (TREE_TYPE (low0))
4953                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4954                       break;
4955                     /* FALLTHROUGH */
4956                   case INTEGER_TYPE:
4957                     if (tree_int_cst_equal (low0,
4958                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4959                       low0 = 0;
4960                     break;
4961                   case POINTER_TYPE:
4962                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4963                         && integer_zerop (low0))
4964                       low0 = 0;
4965                     break;
4966                   default:
4967                     break;
4968                   }
4969
4970               /* Canonicalize - [x, max] into - [x, -].  */
4971               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4972                 switch (TREE_CODE (TREE_TYPE (high1)))
4973                   {
4974                   case ENUMERAL_TYPE:
4975                     if (TYPE_PRECISION (TREE_TYPE (high1))
4976                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4977                       break;
4978                     /* FALLTHROUGH */
4979                   case INTEGER_TYPE:
4980                     if (tree_int_cst_equal (high1,
4981                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4982                       high1 = 0;
4983                     break;
4984                   case POINTER_TYPE:
4985                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4986                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4987                                                        high1, 1,
4988                                                        integer_one_node, 1)))
4989                       high1 = 0;
4990                     break;
4991                   default:
4992                     break;
4993                   }
4994
4995               /* The ranges might be also adjacent between the maximum and
4996                  minimum values of the given type.  For
4997                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4998                  return + [x + 1, y - 1].  */
4999               if (low0 == 0 && high1 == 0)
5000                 {
5001                   low = range_successor (high0);
5002                   high = range_predecessor (low1);
5003                   if (low == 0 || high == 0)
5004                     return 0;
5005
5006                   in_p = 1;
5007                 }
5008               else
5009                 return 0;
5010             }
5011         }
5012       else if (subset)
5013         in_p = 0, low = low0, high = high0;
5014       else
5015         in_p = 0, low = low0, high = high1;
5016     }
5017
5018   *pin_p = in_p, *plow = low, *phigh = high;
5019   return 1;
5020 }
5021 \f
5022
5023 /* Subroutine of fold, looking inside expressions of the form
5024    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5025    of the COND_EXPR.  This function is being used also to optimize
5026    A op B ? C : A, by reversing the comparison first.
5027
5028    Return a folded expression whose code is not a COND_EXPR
5029    anymore, or NULL_TREE if no folding opportunity is found.  */
5030
5031 static tree
5032 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5033 {
5034   enum tree_code comp_code = TREE_CODE (arg0);
5035   tree arg00 = TREE_OPERAND (arg0, 0);
5036   tree arg01 = TREE_OPERAND (arg0, 1);
5037   tree arg1_type = TREE_TYPE (arg1);
5038   tree tem;
5039
5040   STRIP_NOPS (arg1);
5041   STRIP_NOPS (arg2);
5042
5043   /* If we have A op 0 ? A : -A, consider applying the following
5044      transformations:
5045
5046      A == 0? A : -A    same as -A
5047      A != 0? A : -A    same as A
5048      A >= 0? A : -A    same as abs (A)
5049      A > 0?  A : -A    same as abs (A)
5050      A <= 0? A : -A    same as -abs (A)
5051      A < 0?  A : -A    same as -abs (A)
5052
5053      None of these transformations work for modes with signed
5054      zeros.  If A is +/-0, the first two transformations will
5055      change the sign of the result (from +0 to -0, or vice
5056      versa).  The last four will fix the sign of the result,
5057      even though the original expressions could be positive or
5058      negative, depending on the sign of A.
5059
5060      Note that all these transformations are correct if A is
5061      NaN, since the two alternatives (A and -A) are also NaNs.  */
5062   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5063       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5064           ? real_zerop (arg01)
5065           : integer_zerop (arg01))
5066       && ((TREE_CODE (arg2) == NEGATE_EXPR
5067            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5068              /* In the case that A is of the form X-Y, '-A' (arg2) may
5069                 have already been folded to Y-X, check for that. */
5070           || (TREE_CODE (arg1) == MINUS_EXPR
5071               && TREE_CODE (arg2) == MINUS_EXPR
5072               && operand_equal_p (TREE_OPERAND (arg1, 0),
5073                                   TREE_OPERAND (arg2, 1), 0)
5074               && operand_equal_p (TREE_OPERAND (arg1, 1),
5075                                   TREE_OPERAND (arg2, 0), 0))))
5076     switch (comp_code)
5077       {
5078       case EQ_EXPR:
5079       case UNEQ_EXPR:
5080         tem = fold_convert (arg1_type, arg1);
5081         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5082       case NE_EXPR:
5083       case LTGT_EXPR:
5084         return pedantic_non_lvalue (fold_convert (type, arg1));
5085       case UNGE_EXPR:
5086       case UNGT_EXPR:
5087         if (flag_trapping_math)
5088           break;
5089         /* Fall through.  */
5090       case GE_EXPR:
5091       case GT_EXPR:
5092         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5093           arg1 = fold_convert (signed_type_for
5094                                (TREE_TYPE (arg1)), arg1);
5095         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5096         return pedantic_non_lvalue (fold_convert (type, tem));
5097       case UNLE_EXPR:
5098       case UNLT_EXPR:
5099         if (flag_trapping_math)
5100           break;
5101       case LE_EXPR:
5102       case LT_EXPR:
5103         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5104           arg1 = fold_convert (signed_type_for
5105                                (TREE_TYPE (arg1)), arg1);
5106         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5107         return negate_expr (fold_convert (type, tem));
5108       default:
5109         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5110         break;
5111       }
5112
5113   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5114      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5115      both transformations are correct when A is NaN: A != 0
5116      is then true, and A == 0 is false.  */
5117
5118   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5119       && integer_zerop (arg01) && integer_zerop (arg2))
5120     {
5121       if (comp_code == NE_EXPR)
5122         return pedantic_non_lvalue (fold_convert (type, arg1));
5123       else if (comp_code == EQ_EXPR)
5124         return build_int_cst (type, 0);
5125     }
5126
5127   /* Try some transformations of A op B ? A : B.
5128
5129      A == B? A : B    same as B
5130      A != B? A : B    same as A
5131      A >= B? A : B    same as max (A, B)
5132      A > B?  A : B    same as max (B, A)
5133      A <= B? A : B    same as min (A, B)
5134      A < B?  A : B    same as min (B, A)
5135
5136      As above, these transformations don't work in the presence
5137      of signed zeros.  For example, if A and B are zeros of
5138      opposite sign, the first two transformations will change
5139      the sign of the result.  In the last four, the original
5140      expressions give different results for (A=+0, B=-0) and
5141      (A=-0, B=+0), but the transformed expressions do not.
5142
5143      The first two transformations are correct if either A or B
5144      is a NaN.  In the first transformation, the condition will
5145      be false, and B will indeed be chosen.  In the case of the
5146      second transformation, the condition A != B will be true,
5147      and A will be chosen.
5148
5149      The conversions to max() and min() are not correct if B is
5150      a number and A is not.  The conditions in the original
5151      expressions will be false, so all four give B.  The min()
5152      and max() versions would give a NaN instead.  */
5153   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5154       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5155       /* Avoid these transformations if the COND_EXPR may be used
5156          as an lvalue in the C++ front-end.  PR c++/19199.  */
5157       && (in_gimple_form
5158           || (strcmp (lang_hooks.name, "GNU C++") != 0
5159               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5160           || ! maybe_lvalue_p (arg1)
5161           || ! maybe_lvalue_p (arg2)))
5162     {
5163       tree comp_op0 = arg00;
5164       tree comp_op1 = arg01;
5165       tree comp_type = TREE_TYPE (comp_op0);
5166
5167       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5168       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5169         {
5170           comp_type = type;
5171           comp_op0 = arg1;
5172           comp_op1 = arg2;
5173         }
5174
5175       switch (comp_code)
5176         {
5177         case EQ_EXPR:
5178           return pedantic_non_lvalue (fold_convert (type, arg2));
5179         case NE_EXPR:
5180           return pedantic_non_lvalue (fold_convert (type, arg1));
5181         case LE_EXPR:
5182         case LT_EXPR:
5183         case UNLE_EXPR:
5184         case UNLT_EXPR:
5185           /* In C++ a ?: expression can be an lvalue, so put the
5186              operand which will be used if they are equal first
5187              so that we can convert this back to the
5188              corresponding COND_EXPR.  */
5189           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5190             {
5191               comp_op0 = fold_convert (comp_type, comp_op0);
5192               comp_op1 = fold_convert (comp_type, comp_op1);
5193               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5194                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5195                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5196               return pedantic_non_lvalue (fold_convert (type, tem));
5197             }
5198           break;
5199         case GE_EXPR:
5200         case GT_EXPR:
5201         case UNGE_EXPR:
5202         case UNGT_EXPR:
5203           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5204             {
5205               comp_op0 = fold_convert (comp_type, comp_op0);
5206               comp_op1 = fold_convert (comp_type, comp_op1);
5207               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5208                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5209                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5210               return pedantic_non_lvalue (fold_convert (type, tem));
5211             }
5212           break;
5213         case UNEQ_EXPR:
5214           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5215             return pedantic_non_lvalue (fold_convert (type, arg2));
5216           break;
5217         case LTGT_EXPR:
5218           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5219             return pedantic_non_lvalue (fold_convert (type, arg1));
5220           break;
5221         default:
5222           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5223           break;
5224         }
5225     }
5226
5227   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5228      we might still be able to simplify this.  For example,
5229      if C1 is one less or one more than C2, this might have started
5230      out as a MIN or MAX and been transformed by this function.
5231      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5232
5233   if (INTEGRAL_TYPE_P (type)
5234       && TREE_CODE (arg01) == INTEGER_CST
5235       && TREE_CODE (arg2) == INTEGER_CST)
5236     switch (comp_code)
5237       {
5238       case EQ_EXPR:
5239         /* We can replace A with C1 in this case.  */
5240         arg1 = fold_convert (type, arg01);
5241         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5242
5243       case LT_EXPR:
5244         /* If C1 is C2 + 1, this is min(A, C2).  */
5245         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5246                                OEP_ONLY_CONST)
5247             && operand_equal_p (arg01,
5248                                 const_binop (PLUS_EXPR, arg2,
5249                                              build_int_cst (type, 1), 0),
5250                                 OEP_ONLY_CONST))
5251           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5252                                                    type,
5253                                                    fold_convert (type, arg1),
5254                                                    arg2));
5255         break;
5256
5257       case LE_EXPR:
5258         /* If C1 is C2 - 1, this is min(A, C2).  */
5259         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5260                                OEP_ONLY_CONST)
5261             && operand_equal_p (arg01,
5262                                 const_binop (MINUS_EXPR, arg2,
5263                                              build_int_cst (type, 1), 0),
5264                                 OEP_ONLY_CONST))
5265           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5266                                                    type,
5267                                                    fold_convert (type, arg1),
5268                                                    arg2));
5269         break;
5270
5271       case GT_EXPR:
5272         /* If C1 is C2 - 1, this is max(A, C2).  */
5273         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5274                                OEP_ONLY_CONST)
5275             && operand_equal_p (arg01,
5276                                 const_binop (MINUS_EXPR, arg2,
5277                                              build_int_cst (type, 1), 0),
5278                                 OEP_ONLY_CONST))
5279           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5280                                                    type,
5281                                                    fold_convert (type, arg1),
5282                                                    arg2));
5283         break;
5284
5285       case GE_EXPR:
5286         /* If C1 is C2 + 1, this is max(A, C2).  */
5287         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5288                                OEP_ONLY_CONST)
5289             && operand_equal_p (arg01,
5290                                 const_binop (PLUS_EXPR, arg2,
5291                                              build_int_cst (type, 1), 0),
5292                                 OEP_ONLY_CONST))
5293           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5294                                                    type,
5295                                                    fold_convert (type, arg1),
5296                                                    arg2));
5297         break;
5298       case NE_EXPR:
5299         break;
5300       default:
5301         gcc_unreachable ();
5302       }
5303
5304   return NULL_TREE;
5305 }
5306
5307
5308 \f
5309 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5310 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5311   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5312                 false) >= 2)
5313 #endif
5314
5315 /* EXP is some logical combination of boolean tests.  See if we can
5316    merge it into some range test.  Return the new tree if so.  */
5317
5318 static tree
5319 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5320 {
5321   int or_op = (code == TRUTH_ORIF_EXPR
5322                || code == TRUTH_OR_EXPR);
5323   int in0_p, in1_p, in_p;
5324   tree low0, low1, low, high0, high1, high;
5325   bool strict_overflow_p = false;
5326   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5327   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5328   tree tem;
5329   const char * const warnmsg = G_("assuming signed overflow does not occur "
5330                                   "when simplifying range test");
5331
5332   /* If this is an OR operation, invert both sides; we will invert
5333      again at the end.  */
5334   if (or_op)
5335     in0_p = ! in0_p, in1_p = ! in1_p;
5336
5337   /* If both expressions are the same, if we can merge the ranges, and we
5338      can build the range test, return it or it inverted.  If one of the
5339      ranges is always true or always false, consider it to be the same
5340      expression as the other.  */
5341   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5342       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5343                        in1_p, low1, high1)
5344       && 0 != (tem = (build_range_check (type,
5345                                          lhs != 0 ? lhs
5346                                          : rhs != 0 ? rhs : integer_zero_node,
5347                                          in_p, low, high))))
5348     {
5349       if (strict_overflow_p)
5350         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5351       return or_op ? invert_truthvalue (tem) : tem;
5352     }
5353
5354   /* On machines where the branch cost is expensive, if this is a
5355      short-circuited branch and the underlying object on both sides
5356      is the same, make a non-short-circuit operation.  */
5357   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5358            && lhs != 0 && rhs != 0
5359            && (code == TRUTH_ANDIF_EXPR
5360                || code == TRUTH_ORIF_EXPR)
5361            && operand_equal_p (lhs, rhs, 0))
5362     {
5363       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5364          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5365          which cases we can't do this.  */
5366       if (simple_operand_p (lhs))
5367         return build2 (code == TRUTH_ANDIF_EXPR
5368                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5369                        type, op0, op1);
5370
5371       else if (lang_hooks.decls.global_bindings_p () == 0
5372                && ! CONTAINS_PLACEHOLDER_P (lhs))
5373         {
5374           tree common = save_expr (lhs);
5375
5376           if (0 != (lhs = build_range_check (type, common,
5377                                              or_op ? ! in0_p : in0_p,
5378                                              low0, high0))
5379               && (0 != (rhs = build_range_check (type, common,
5380                                                  or_op ? ! in1_p : in1_p,
5381                                                  low1, high1))))
5382             {
5383               if (strict_overflow_p)
5384                 fold_overflow_warning (warnmsg,
5385                                        WARN_STRICT_OVERFLOW_COMPARISON);
5386               return build2 (code == TRUTH_ANDIF_EXPR
5387                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5388                              type, lhs, rhs);
5389             }
5390         }
5391     }
5392
5393   return 0;
5394 }
5395 \f
5396 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5397    bit value.  Arrange things so the extra bits will be set to zero if and
5398    only if C is signed-extended to its full width.  If MASK is nonzero,
5399    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5400
5401 static tree
5402 unextend (tree c, int p, int unsignedp, tree mask)
5403 {
5404   tree type = TREE_TYPE (c);
5405   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5406   tree temp;
5407
5408   if (p == modesize || unsignedp)
5409     return c;
5410
5411   /* We work by getting just the sign bit into the low-order bit, then
5412      into the high-order bit, then sign-extend.  We then XOR that value
5413      with C.  */
5414   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5415   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5416
5417   /* We must use a signed type in order to get an arithmetic right shift.
5418      However, we must also avoid introducing accidental overflows, so that
5419      a subsequent call to integer_zerop will work.  Hence we must
5420      do the type conversion here.  At this point, the constant is either
5421      zero or one, and the conversion to a signed type can never overflow.
5422      We could get an overflow if this conversion is done anywhere else.  */
5423   if (TYPE_UNSIGNED (type))
5424     temp = fold_convert (signed_type_for (type), temp);
5425
5426   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5427   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5428   if (mask != 0)
5429     temp = const_binop (BIT_AND_EXPR, temp,
5430                         fold_convert (TREE_TYPE (c), mask), 0);
5431   /* If necessary, convert the type back to match the type of C.  */
5432   if (TYPE_UNSIGNED (type))
5433     temp = fold_convert (type, temp);
5434
5435   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5436 }
5437 \f
5438 /* Find ways of folding logical expressions of LHS and RHS:
5439    Try to merge two comparisons to the same innermost item.
5440    Look for range tests like "ch >= '0' && ch <= '9'".
5441    Look for combinations of simple terms on machines with expensive branches
5442    and evaluate the RHS unconditionally.
5443
5444    For example, if we have p->a == 2 && p->b == 4 and we can make an
5445    object large enough to span both A and B, we can do this with a comparison
5446    against the object ANDed with the a mask.
5447
5448    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5449    operations to do this with one comparison.
5450
5451    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5452    function and the one above.
5453
5454    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5455    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5456
5457    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5458    two operands.
5459
5460    We return the simplified tree or 0 if no optimization is possible.  */
5461
5462 static tree
5463 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5464 {
5465   /* If this is the "or" of two comparisons, we can do something if
5466      the comparisons are NE_EXPR.  If this is the "and", we can do something
5467      if the comparisons are EQ_EXPR.  I.e.,
5468         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5469
5470      WANTED_CODE is this operation code.  For single bit fields, we can
5471      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5472      comparison for one-bit fields.  */
5473
5474   enum tree_code wanted_code;
5475   enum tree_code lcode, rcode;
5476   tree ll_arg, lr_arg, rl_arg, rr_arg;
5477   tree ll_inner, lr_inner, rl_inner, rr_inner;
5478   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5479   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5480   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5481   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5482   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5483   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5484   enum machine_mode lnmode, rnmode;
5485   tree ll_mask, lr_mask, rl_mask, rr_mask;
5486   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5487   tree l_const, r_const;
5488   tree lntype, rntype, result;
5489   HOST_WIDE_INT first_bit, end_bit;
5490   int volatilep;
5491   tree orig_lhs = lhs, orig_rhs = rhs;
5492   enum tree_code orig_code = code;
5493
5494   /* Start by getting the comparison codes.  Fail if anything is volatile.
5495      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5496      it were surrounded with a NE_EXPR.  */
5497
5498   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5499     return 0;
5500
5501   lcode = TREE_CODE (lhs);
5502   rcode = TREE_CODE (rhs);
5503
5504   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5505     {
5506       lhs = build2 (NE_EXPR, truth_type, lhs,
5507                     build_int_cst (TREE_TYPE (lhs), 0));
5508       lcode = NE_EXPR;
5509     }
5510
5511   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5512     {
5513       rhs = build2 (NE_EXPR, truth_type, rhs,
5514                     build_int_cst (TREE_TYPE (rhs), 0));
5515       rcode = NE_EXPR;
5516     }
5517
5518   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5519       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5520     return 0;
5521
5522   ll_arg = TREE_OPERAND (lhs, 0);
5523   lr_arg = TREE_OPERAND (lhs, 1);
5524   rl_arg = TREE_OPERAND (rhs, 0);
5525   rr_arg = TREE_OPERAND (rhs, 1);
5526
5527   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5528   if (simple_operand_p (ll_arg)
5529       && simple_operand_p (lr_arg))
5530     {
5531       tree result;
5532       if (operand_equal_p (ll_arg, rl_arg, 0)
5533           && operand_equal_p (lr_arg, rr_arg, 0))
5534         {
5535           result = combine_comparisons (code, lcode, rcode,
5536                                         truth_type, ll_arg, lr_arg);
5537           if (result)
5538             return result;
5539         }
5540       else if (operand_equal_p (ll_arg, rr_arg, 0)
5541                && operand_equal_p (lr_arg, rl_arg, 0))
5542         {
5543           result = combine_comparisons (code, lcode,
5544                                         swap_tree_comparison (rcode),
5545                                         truth_type, ll_arg, lr_arg);
5546           if (result)
5547             return result;
5548         }
5549     }
5550
5551   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5552           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5553
5554   /* If the RHS can be evaluated unconditionally and its operands are
5555      simple, it wins to evaluate the RHS unconditionally on machines
5556      with expensive branches.  In this case, this isn't a comparison
5557      that can be merged.  Avoid doing this if the RHS is a floating-point
5558      comparison since those can trap.  */
5559
5560   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5561                    false) >= 2
5562       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5563       && simple_operand_p (rl_arg)
5564       && simple_operand_p (rr_arg))
5565     {
5566       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5567       if (code == TRUTH_OR_EXPR
5568           && lcode == NE_EXPR && integer_zerop (lr_arg)
5569           && rcode == NE_EXPR && integer_zerop (rr_arg)
5570           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5571           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5572         return build2 (NE_EXPR, truth_type,
5573                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5574                                ll_arg, rl_arg),
5575                        build_int_cst (TREE_TYPE (ll_arg), 0));
5576
5577       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5578       if (code == TRUTH_AND_EXPR
5579           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5580           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5581           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5582           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5583         return build2 (EQ_EXPR, truth_type,
5584                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5585                                ll_arg, rl_arg),
5586                        build_int_cst (TREE_TYPE (ll_arg), 0));
5587
5588       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5589         {
5590           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5591             return build2 (code, truth_type, lhs, rhs);
5592           return NULL_TREE;
5593         }
5594     }
5595
5596   /* See if the comparisons can be merged.  Then get all the parameters for
5597      each side.  */
5598
5599   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5600       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5601     return 0;
5602
5603   volatilep = 0;
5604   ll_inner = decode_field_reference (ll_arg,
5605                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5606                                      &ll_unsignedp, &volatilep, &ll_mask,
5607                                      &ll_and_mask);
5608   lr_inner = decode_field_reference (lr_arg,
5609                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5610                                      &lr_unsignedp, &volatilep, &lr_mask,
5611                                      &lr_and_mask);
5612   rl_inner = decode_field_reference (rl_arg,
5613                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5614                                      &rl_unsignedp, &volatilep, &rl_mask,
5615                                      &rl_and_mask);
5616   rr_inner = decode_field_reference (rr_arg,
5617                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5618                                      &rr_unsignedp, &volatilep, &rr_mask,
5619                                      &rr_and_mask);
5620
5621   /* It must be true that the inner operation on the lhs of each
5622      comparison must be the same if we are to be able to do anything.
5623      Then see if we have constants.  If not, the same must be true for
5624      the rhs's.  */
5625   if (volatilep || ll_inner == 0 || rl_inner == 0
5626       || ! operand_equal_p (ll_inner, rl_inner, 0))
5627     return 0;
5628
5629   if (TREE_CODE (lr_arg) == INTEGER_CST
5630       && TREE_CODE (rr_arg) == INTEGER_CST)
5631     l_const = lr_arg, r_const = rr_arg;
5632   else if (lr_inner == 0 || rr_inner == 0
5633            || ! operand_equal_p (lr_inner, rr_inner, 0))
5634     return 0;
5635   else
5636     l_const = r_const = 0;
5637
5638   /* If either comparison code is not correct for our logical operation,
5639      fail.  However, we can convert a one-bit comparison against zero into
5640      the opposite comparison against that bit being set in the field.  */
5641
5642   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5643   if (lcode != wanted_code)
5644     {
5645       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5646         {
5647           /* Make the left operand unsigned, since we are only interested
5648              in the value of one bit.  Otherwise we are doing the wrong
5649              thing below.  */
5650           ll_unsignedp = 1;
5651           l_const = ll_mask;
5652         }
5653       else
5654         return 0;
5655     }
5656
5657   /* This is analogous to the code for l_const above.  */
5658   if (rcode != wanted_code)
5659     {
5660       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5661         {
5662           rl_unsignedp = 1;
5663           r_const = rl_mask;
5664         }
5665       else
5666         return 0;
5667     }
5668
5669   /* See if we can find a mode that contains both fields being compared on
5670      the left.  If we can't, fail.  Otherwise, update all constants and masks
5671      to be relative to a field of that size.  */
5672   first_bit = MIN (ll_bitpos, rl_bitpos);
5673   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5674   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5675                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5676                           volatilep);
5677   if (lnmode == VOIDmode)
5678     return 0;
5679
5680   lnbitsize = GET_MODE_BITSIZE (lnmode);
5681   lnbitpos = first_bit & ~ (lnbitsize - 1);
5682   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5683   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5684
5685   if (BYTES_BIG_ENDIAN)
5686     {
5687       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5688       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5689     }
5690
5691   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5692                          size_int (xll_bitpos), 0);
5693   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5694                          size_int (xrl_bitpos), 0);
5695
5696   if (l_const)
5697     {
5698       l_const = fold_convert (lntype, l_const);
5699       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5700       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5701       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5702                                         fold_build1 (BIT_NOT_EXPR,
5703                                                      lntype, ll_mask),
5704                                         0)))
5705         {
5706           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5707
5708           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5709         }
5710     }
5711   if (r_const)
5712     {
5713       r_const = fold_convert (lntype, r_const);
5714       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5715       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5716       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5717                                         fold_build1 (BIT_NOT_EXPR,
5718                                                      lntype, rl_mask),
5719                                         0)))
5720         {
5721           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5722
5723           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5724         }
5725     }
5726
5727   /* If the right sides are not constant, do the same for it.  Also,
5728      disallow this optimization if a size or signedness mismatch occurs
5729      between the left and right sides.  */
5730   if (l_const == 0)
5731     {
5732       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5733           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5734           /* Make sure the two fields on the right
5735              correspond to the left without being swapped.  */
5736           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5737         return 0;
5738
5739       first_bit = MIN (lr_bitpos, rr_bitpos);
5740       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5741       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5742                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5743                               volatilep);
5744       if (rnmode == VOIDmode)
5745         return 0;
5746
5747       rnbitsize = GET_MODE_BITSIZE (rnmode);
5748       rnbitpos = first_bit & ~ (rnbitsize - 1);
5749       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5750       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5751
5752       if (BYTES_BIG_ENDIAN)
5753         {
5754           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5755           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5756         }
5757
5758       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5759                              size_int (xlr_bitpos), 0);
5760       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5761                              size_int (xrr_bitpos), 0);
5762
5763       /* Make a mask that corresponds to both fields being compared.
5764          Do this for both items being compared.  If the operands are the
5765          same size and the bits being compared are in the same position
5766          then we can do this by masking both and comparing the masked
5767          results.  */
5768       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5769       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5770       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5771         {
5772           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5773                                     ll_unsignedp || rl_unsignedp);
5774           if (! all_ones_mask_p (ll_mask, lnbitsize))
5775             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5776
5777           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5778                                     lr_unsignedp || rr_unsignedp);
5779           if (! all_ones_mask_p (lr_mask, rnbitsize))
5780             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5781
5782           return build2 (wanted_code, truth_type, lhs, rhs);
5783         }
5784
5785       /* There is still another way we can do something:  If both pairs of
5786          fields being compared are adjacent, we may be able to make a wider
5787          field containing them both.
5788
5789          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5790          the mask must be shifted to account for the shift done by
5791          make_bit_field_ref.  */
5792       if ((ll_bitsize + ll_bitpos == rl_bitpos
5793            && lr_bitsize + lr_bitpos == rr_bitpos)
5794           || (ll_bitpos == rl_bitpos + rl_bitsize
5795               && lr_bitpos == rr_bitpos + rr_bitsize))
5796         {
5797           tree type;
5798
5799           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5800                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5801           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5802                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5803
5804           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5805                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5806           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5807                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5808
5809           /* Convert to the smaller type before masking out unwanted bits.  */
5810           type = lntype;
5811           if (lntype != rntype)
5812             {
5813               if (lnbitsize > rnbitsize)
5814                 {
5815                   lhs = fold_convert (rntype, lhs);
5816                   ll_mask = fold_convert (rntype, ll_mask);
5817                   type = rntype;
5818                 }
5819               else if (lnbitsize < rnbitsize)
5820                 {
5821                   rhs = fold_convert (lntype, rhs);
5822                   lr_mask = fold_convert (lntype, lr_mask);
5823                   type = lntype;
5824                 }
5825             }
5826
5827           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5828             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5829
5830           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5831             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5832
5833           return build2 (wanted_code, truth_type, lhs, rhs);
5834         }
5835
5836       return 0;
5837     }
5838
5839   /* Handle the case of comparisons with constants.  If there is something in
5840      common between the masks, those bits of the constants must be the same.
5841      If not, the condition is always false.  Test for this to avoid generating
5842      incorrect code below.  */
5843   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5844   if (! integer_zerop (result)
5845       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5846                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5847     {
5848       if (wanted_code == NE_EXPR)
5849         {
5850           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5851           return constant_boolean_node (true, truth_type);
5852         }
5853       else
5854         {
5855           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5856           return constant_boolean_node (false, truth_type);
5857         }
5858     }
5859
5860   /* Construct the expression we will return.  First get the component
5861      reference we will make.  Unless the mask is all ones the width of
5862      that field, perform the mask operation.  Then compare with the
5863      merged constant.  */
5864   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5865                                ll_unsignedp || rl_unsignedp);
5866
5867   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5868   if (! all_ones_mask_p (ll_mask, lnbitsize))
5869     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5870
5871   return build2 (wanted_code, truth_type, result,
5872                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5873 }
5874 \f
5875 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5876    constant.  */
5877
5878 static tree
5879 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5880 {
5881   tree arg0 = op0;
5882   enum tree_code op_code;
5883   tree comp_const;
5884   tree minmax_const;
5885   int consts_equal, consts_lt;
5886   tree inner;
5887
5888   STRIP_SIGN_NOPS (arg0);
5889
5890   op_code = TREE_CODE (arg0);
5891   minmax_const = TREE_OPERAND (arg0, 1);
5892   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5893   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5894   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5895   inner = TREE_OPERAND (arg0, 0);
5896
5897   /* If something does not permit us to optimize, return the original tree.  */
5898   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5899       || TREE_CODE (comp_const) != INTEGER_CST
5900       || TREE_OVERFLOW (comp_const)
5901       || TREE_CODE (minmax_const) != INTEGER_CST
5902       || TREE_OVERFLOW (minmax_const))
5903     return NULL_TREE;
5904
5905   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5906      and GT_EXPR, doing the rest with recursive calls using logical
5907      simplifications.  */
5908   switch (code)
5909     {
5910     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5911       {
5912         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5913                                           type, op0, op1);
5914         if (tem)
5915           return invert_truthvalue (tem);
5916         return NULL_TREE;
5917       }
5918
5919     case GE_EXPR:
5920       return
5921         fold_build2 (TRUTH_ORIF_EXPR, type,
5922                      optimize_minmax_comparison
5923                      (EQ_EXPR, type, arg0, comp_const),
5924                      optimize_minmax_comparison
5925                      (GT_EXPR, type, arg0, comp_const));
5926
5927     case EQ_EXPR:
5928       if (op_code == MAX_EXPR && consts_equal)
5929         /* MAX (X, 0) == 0  ->  X <= 0  */
5930         return fold_build2 (LE_EXPR, type, inner, comp_const);
5931
5932       else if (op_code == MAX_EXPR && consts_lt)
5933         /* MAX (X, 0) == 5  ->  X == 5   */
5934         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5935
5936       else if (op_code == MAX_EXPR)
5937         /* MAX (X, 0) == -1  ->  false  */
5938         return omit_one_operand (type, integer_zero_node, inner);
5939
5940       else if (consts_equal)
5941         /* MIN (X, 0) == 0  ->  X >= 0  */
5942         return fold_build2 (GE_EXPR, type, inner, comp_const);
5943
5944       else if (consts_lt)
5945         /* MIN (X, 0) == 5  ->  false  */
5946         return omit_one_operand (type, integer_zero_node, inner);
5947
5948       else
5949         /* MIN (X, 0) == -1  ->  X == -1  */
5950         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5951
5952     case GT_EXPR:
5953       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5954         /* MAX (X, 0) > 0  ->  X > 0
5955            MAX (X, 0) > 5  ->  X > 5  */
5956         return fold_build2 (GT_EXPR, type, inner, comp_const);
5957
5958       else if (op_code == MAX_EXPR)
5959         /* MAX (X, 0) > -1  ->  true  */
5960         return omit_one_operand (type, integer_one_node, inner);
5961
5962       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5963         /* MIN (X, 0) > 0  ->  false
5964            MIN (X, 0) > 5  ->  false  */
5965         return omit_one_operand (type, integer_zero_node, inner);
5966
5967       else
5968         /* MIN (X, 0) > -1  ->  X > -1  */
5969         return fold_build2 (GT_EXPR, type, inner, comp_const);
5970
5971     default:
5972       return NULL_TREE;
5973     }
5974 }
5975 \f
5976 /* T is an integer expression that is being multiplied, divided, or taken a
5977    modulus (CODE says which and what kind of divide or modulus) by a
5978    constant C.  See if we can eliminate that operation by folding it with
5979    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5980    should be used for the computation if wider than our type.
5981
5982    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5983    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5984    expression would not overflow or that overflow is undefined for the type
5985    in the language in question.
5986
5987    If we return a non-null expression, it is an equivalent form of the
5988    original computation, but need not be in the original type.
5989
5990    We set *STRICT_OVERFLOW_P to true if the return values depends on
5991    signed overflow being undefined.  Otherwise we do not change
5992    *STRICT_OVERFLOW_P.  */
5993
5994 static tree
5995 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5996                 bool *strict_overflow_p)
5997 {
5998   /* To avoid exponential search depth, refuse to allow recursion past
5999      three levels.  Beyond that (1) it's highly unlikely that we'll find
6000      something interesting and (2) we've probably processed it before
6001      when we built the inner expression.  */
6002
6003   static int depth;
6004   tree ret;
6005
6006   if (depth > 3)
6007     return NULL;
6008
6009   depth++;
6010   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6011   depth--;
6012
6013   return ret;
6014 }
6015
6016 static tree
6017 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6018                   bool *strict_overflow_p)
6019 {
6020   tree type = TREE_TYPE (t);
6021   enum tree_code tcode = TREE_CODE (t);
6022   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6023                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6024                 ? wide_type : type);
6025   tree t1, t2;
6026   int same_p = tcode == code;
6027   tree op0 = NULL_TREE, op1 = NULL_TREE;
6028   bool sub_strict_overflow_p;
6029
6030   /* Don't deal with constants of zero here; they confuse the code below.  */
6031   if (integer_zerop (c))
6032     return NULL_TREE;
6033
6034   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6035     op0 = TREE_OPERAND (t, 0);
6036
6037   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6038     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6039
6040   /* Note that we need not handle conditional operations here since fold
6041      already handles those cases.  So just do arithmetic here.  */
6042   switch (tcode)
6043     {
6044     case INTEGER_CST:
6045       /* For a constant, we can always simplify if we are a multiply
6046          or (for divide and modulus) if it is a multiple of our constant.  */
6047       if (code == MULT_EXPR
6048           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6049         return const_binop (code, fold_convert (ctype, t),
6050                             fold_convert (ctype, c), 0);
6051       break;
6052
6053     CASE_CONVERT: case NON_LVALUE_EXPR:
6054       /* If op0 is an expression ...  */
6055       if ((COMPARISON_CLASS_P (op0)
6056            || UNARY_CLASS_P (op0)
6057            || BINARY_CLASS_P (op0)
6058            || VL_EXP_CLASS_P (op0)
6059            || EXPRESSION_CLASS_P (op0))
6060           /* ... and has wrapping overflow, and its type is smaller
6061              than ctype, then we cannot pass through as widening.  */
6062           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6063                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6064                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6065                && (TYPE_PRECISION (ctype)
6066                    > TYPE_PRECISION (TREE_TYPE (op0))))
6067               /* ... or this is a truncation (t is narrower than op0),
6068                  then we cannot pass through this narrowing.  */
6069               || (TYPE_PRECISION (type)
6070                   < TYPE_PRECISION (TREE_TYPE (op0)))
6071               /* ... or signedness changes for division or modulus,
6072                  then we cannot pass through this conversion.  */
6073               || (code != MULT_EXPR
6074                   && (TYPE_UNSIGNED (ctype)
6075                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6076               /* ... or has undefined overflow while the converted to
6077                  type has not, we cannot do the operation in the inner type
6078                  as that would introduce undefined overflow.  */
6079               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6080                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6081         break;
6082
6083       /* Pass the constant down and see if we can make a simplification.  If
6084          we can, replace this expression with the inner simplification for
6085          possible later conversion to our or some other type.  */
6086       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6087           && TREE_CODE (t2) == INTEGER_CST
6088           && !TREE_OVERFLOW (t2)
6089           && (0 != (t1 = extract_muldiv (op0, t2, code,
6090                                          code == MULT_EXPR
6091                                          ? ctype : NULL_TREE,
6092                                          strict_overflow_p))))
6093         return t1;
6094       break;
6095
6096     case ABS_EXPR:
6097       /* If widening the type changes it from signed to unsigned, then we
6098          must avoid building ABS_EXPR itself as unsigned.  */
6099       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6100         {
6101           tree cstype = (*signed_type_for) (ctype);
6102           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6103               != 0)
6104             {
6105               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6106               return fold_convert (ctype, t1);
6107             }
6108           break;
6109         }
6110       /* If the constant is negative, we cannot simplify this.  */
6111       if (tree_int_cst_sgn (c) == -1)
6112         break;
6113       /* FALLTHROUGH */
6114     case NEGATE_EXPR:
6115       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6116           != 0)
6117         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6118       break;
6119
6120     case MIN_EXPR:  case MAX_EXPR:
6121       /* If widening the type changes the signedness, then we can't perform
6122          this optimization as that changes the result.  */
6123       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6124         break;
6125
6126       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6127       sub_strict_overflow_p = false;
6128       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6129                                 &sub_strict_overflow_p)) != 0
6130           && (t2 = extract_muldiv (op1, c, code, wide_type,
6131                                    &sub_strict_overflow_p)) != 0)
6132         {
6133           if (tree_int_cst_sgn (c) < 0)
6134             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6135           if (sub_strict_overflow_p)
6136             *strict_overflow_p = true;
6137           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6138                               fold_convert (ctype, t2));
6139         }
6140       break;
6141
6142     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6143       /* If the second operand is constant, this is a multiplication
6144          or floor division, by a power of two, so we can treat it that
6145          way unless the multiplier or divisor overflows.  Signed
6146          left-shift overflow is implementation-defined rather than
6147          undefined in C90, so do not convert signed left shift into
6148          multiplication.  */
6149       if (TREE_CODE (op1) == INTEGER_CST
6150           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6151           /* const_binop may not detect overflow correctly,
6152              so check for it explicitly here.  */
6153           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6154           && TREE_INT_CST_HIGH (op1) == 0
6155           && 0 != (t1 = fold_convert (ctype,
6156                                       const_binop (LSHIFT_EXPR,
6157                                                    size_one_node,
6158                                                    op1, 0)))
6159           && !TREE_OVERFLOW (t1))
6160         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6161                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6162                                        ctype, fold_convert (ctype, op0), t1),
6163                                c, code, wide_type, strict_overflow_p);
6164       break;
6165
6166     case PLUS_EXPR:  case MINUS_EXPR:
6167       /* See if we can eliminate the operation on both sides.  If we can, we
6168          can return a new PLUS or MINUS.  If we can't, the only remaining
6169          cases where we can do anything are if the second operand is a
6170          constant.  */
6171       sub_strict_overflow_p = false;
6172       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6173       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6174       if (t1 != 0 && t2 != 0
6175           && (code == MULT_EXPR
6176               /* If not multiplication, we can only do this if both operands
6177                  are divisible by c.  */
6178               || (multiple_of_p (ctype, op0, c)
6179                   && multiple_of_p (ctype, op1, c))))
6180         {
6181           if (sub_strict_overflow_p)
6182             *strict_overflow_p = true;
6183           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6184                               fold_convert (ctype, t2));
6185         }
6186
6187       /* If this was a subtraction, negate OP1 and set it to be an addition.
6188          This simplifies the logic below.  */
6189       if (tcode == MINUS_EXPR)
6190         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6191
6192       if (TREE_CODE (op1) != INTEGER_CST)
6193         break;
6194
6195       /* If either OP1 or C are negative, this optimization is not safe for
6196          some of the division and remainder types while for others we need
6197          to change the code.  */
6198       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6199         {
6200           if (code == CEIL_DIV_EXPR)
6201             code = FLOOR_DIV_EXPR;
6202           else if (code == FLOOR_DIV_EXPR)
6203             code = CEIL_DIV_EXPR;
6204           else if (code != MULT_EXPR
6205                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6206             break;
6207         }
6208
6209       /* If it's a multiply or a division/modulus operation of a multiple
6210          of our constant, do the operation and verify it doesn't overflow.  */
6211       if (code == MULT_EXPR
6212           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6213         {
6214           op1 = const_binop (code, fold_convert (ctype, op1),
6215                              fold_convert (ctype, c), 0);
6216           /* We allow the constant to overflow with wrapping semantics.  */
6217           if (op1 == 0
6218               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6219             break;
6220         }
6221       else
6222         break;
6223
6224       /* If we have an unsigned type is not a sizetype, we cannot widen
6225          the operation since it will change the result if the original
6226          computation overflowed.  */
6227       if (TYPE_UNSIGNED (ctype)
6228           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6229           && ctype != type)
6230         break;
6231
6232       /* If we were able to eliminate our operation from the first side,
6233          apply our operation to the second side and reform the PLUS.  */
6234       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6235         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6236
6237       /* The last case is if we are a multiply.  In that case, we can
6238          apply the distributive law to commute the multiply and addition
6239          if the multiplication of the constants doesn't overflow.  */
6240       if (code == MULT_EXPR)
6241         return fold_build2 (tcode, ctype,
6242                             fold_build2 (code, ctype,
6243                                          fold_convert (ctype, op0),
6244                                          fold_convert (ctype, c)),
6245                             op1);
6246
6247       break;
6248
6249     case MULT_EXPR:
6250       /* We have a special case here if we are doing something like
6251          (C * 8) % 4 since we know that's zero.  */
6252       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6253            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6254           /* If the multiplication can overflow we cannot optimize this.
6255              ???  Until we can properly mark individual operations as
6256              not overflowing we need to treat sizetype special here as
6257              stor-layout relies on this opimization to make
6258              DECL_FIELD_BIT_OFFSET always a constant.  */
6259           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6260               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6261                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6262           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6263           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6264         {
6265           *strict_overflow_p = true;
6266           return omit_one_operand (type, integer_zero_node, op0);
6267         }
6268
6269       /* ... fall through ...  */
6270
6271     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6272     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6273       /* If we can extract our operation from the LHS, do so and return a
6274          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6275          do something only if the second operand is a constant.  */
6276       if (same_p
6277           && (t1 = extract_muldiv (op0, c, code, wide_type,
6278                                    strict_overflow_p)) != 0)
6279         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6280                             fold_convert (ctype, op1));
6281       else if (tcode == MULT_EXPR && code == MULT_EXPR
6282                && (t1 = extract_muldiv (op1, c, code, wide_type,
6283                                         strict_overflow_p)) != 0)
6284         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6285                             fold_convert (ctype, t1));
6286       else if (TREE_CODE (op1) != INTEGER_CST)
6287         return 0;
6288
6289       /* If these are the same operation types, we can associate them
6290          assuming no overflow.  */
6291       if (tcode == code
6292           && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6293                                          fold_convert (ctype, c), 1))
6294           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6295                                                TREE_INT_CST_HIGH (t1),
6296                                                (TYPE_UNSIGNED (ctype)
6297                                                 && tcode != MULT_EXPR) ? -1 : 1,
6298                                                TREE_OVERFLOW (t1)))
6299           && !TREE_OVERFLOW (t1))
6300         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6301
6302       /* If these operations "cancel" each other, we have the main
6303          optimizations of this pass, which occur when either constant is a
6304          multiple of the other, in which case we replace this with either an
6305          operation or CODE or TCODE.
6306
6307          If we have an unsigned type that is not a sizetype, we cannot do
6308          this since it will change the result if the original computation
6309          overflowed.  */
6310       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6311            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6312           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6313               || (tcode == MULT_EXPR
6314                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6315                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6316                   && code != MULT_EXPR)))
6317         {
6318           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6319             {
6320               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6321                 *strict_overflow_p = true;
6322               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6323                                   fold_convert (ctype,
6324                                                 const_binop (TRUNC_DIV_EXPR,
6325                                                              op1, c, 0)));
6326             }
6327           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6328             {
6329               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6330                 *strict_overflow_p = true;
6331               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6332                                   fold_convert (ctype,
6333                                                 const_binop (TRUNC_DIV_EXPR,
6334                                                              c, op1, 0)));
6335             }
6336         }
6337       break;
6338
6339     default:
6340       break;
6341     }
6342
6343   return 0;
6344 }
6345 \f
6346 /* Return a node which has the indicated constant VALUE (either 0 or
6347    1), and is of the indicated TYPE.  */
6348
6349 tree
6350 constant_boolean_node (int value, tree type)
6351 {
6352   if (type == integer_type_node)
6353     return value ? integer_one_node : integer_zero_node;
6354   else if (type == boolean_type_node)
6355     return value ? boolean_true_node : boolean_false_node;
6356   else
6357     return build_int_cst (type, value);
6358 }
6359
6360
6361 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6362    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6363    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6364    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6365    COND is the first argument to CODE; otherwise (as in the example
6366    given here), it is the second argument.  TYPE is the type of the
6367    original expression.  Return NULL_TREE if no simplification is
6368    possible.  */
6369
6370 static tree
6371 fold_binary_op_with_conditional_arg (enum tree_code code,
6372                                      tree type, tree op0, tree op1,
6373                                      tree cond, tree arg, int cond_first_p)
6374 {
6375   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6376   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6377   tree test, true_value, false_value;
6378   tree lhs = NULL_TREE;
6379   tree rhs = NULL_TREE;
6380
6381   /* This transformation is only worthwhile if we don't have to wrap
6382      arg in a SAVE_EXPR, and the operation can be simplified on at least
6383      one of the branches once its pushed inside the COND_EXPR.  */
6384   if (!TREE_CONSTANT (arg))
6385     return NULL_TREE;
6386
6387   if (TREE_CODE (cond) == COND_EXPR)
6388     {
6389       test = TREE_OPERAND (cond, 0);
6390       true_value = TREE_OPERAND (cond, 1);
6391       false_value = TREE_OPERAND (cond, 2);
6392       /* If this operand throws an expression, then it does not make
6393          sense to try to perform a logical or arithmetic operation
6394          involving it.  */
6395       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6396         lhs = true_value;
6397       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6398         rhs = false_value;
6399     }
6400   else
6401     {
6402       tree testtype = TREE_TYPE (cond);
6403       test = cond;
6404       true_value = constant_boolean_node (true, testtype);
6405       false_value = constant_boolean_node (false, testtype);
6406     }
6407
6408   arg = fold_convert (arg_type, arg);
6409   if (lhs == 0)
6410     {
6411       true_value = fold_convert (cond_type, true_value);
6412       if (cond_first_p)
6413         lhs = fold_build2 (code, type, true_value, arg);
6414       else
6415         lhs = fold_build2 (code, type, arg, true_value);
6416     }
6417   if (rhs == 0)
6418     {
6419       false_value = fold_convert (cond_type, false_value);
6420       if (cond_first_p)
6421         rhs = fold_build2 (code, type, false_value, arg);
6422       else
6423         rhs = fold_build2 (code, type, arg, false_value);
6424     }
6425
6426   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6427   return fold_convert (type, test);
6428 }
6429
6430 \f
6431 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6432
6433    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6434    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6435    ADDEND is the same as X.
6436
6437    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6438    and finite.  The problematic cases are when X is zero, and its mode
6439    has signed zeros.  In the case of rounding towards -infinity,
6440    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6441    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6442
6443 bool
6444 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6445 {
6446   if (!real_zerop (addend))
6447     return false;
6448
6449   /* Don't allow the fold with -fsignaling-nans.  */
6450   if (HONOR_SNANS (TYPE_MODE (type)))
6451     return false;
6452
6453   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6454   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6455     return true;
6456
6457   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6458   if (TREE_CODE (addend) == REAL_CST
6459       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6460     negate = !negate;
6461
6462   /* The mode has signed zeros, and we have to honor their sign.
6463      In this situation, there is only one case we can return true for.
6464      X - 0 is the same as X unless rounding towards -infinity is
6465      supported.  */
6466   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6467 }
6468
6469 /* Subroutine of fold() that checks comparisons of built-in math
6470    functions against real constants.
6471
6472    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6473    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6474    is the type of the result and ARG0 and ARG1 are the operands of the
6475    comparison.  ARG1 must be a TREE_REAL_CST.
6476
6477    The function returns the constant folded tree if a simplification
6478    can be made, and NULL_TREE otherwise.  */
6479
6480 static tree
6481 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6482                      tree type, tree arg0, tree arg1)
6483 {
6484   REAL_VALUE_TYPE c;
6485
6486   if (BUILTIN_SQRT_P (fcode))
6487     {
6488       tree arg = CALL_EXPR_ARG (arg0, 0);
6489       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6490
6491       c = TREE_REAL_CST (arg1);
6492       if (REAL_VALUE_NEGATIVE (c))
6493         {
6494           /* sqrt(x) < y is always false, if y is negative.  */
6495           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6496             return omit_one_operand (type, integer_zero_node, arg);
6497
6498           /* sqrt(x) > y is always true, if y is negative and we
6499              don't care about NaNs, i.e. negative values of x.  */
6500           if (code == NE_EXPR || !HONOR_NANS (mode))
6501             return omit_one_operand (type, integer_one_node, arg);
6502
6503           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6504           return fold_build2 (GE_EXPR, type, arg,
6505                               build_real (TREE_TYPE (arg), dconst0));
6506         }
6507       else if (code == GT_EXPR || code == GE_EXPR)
6508         {
6509           REAL_VALUE_TYPE c2;
6510
6511           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6512           real_convert (&c2, mode, &c2);
6513
6514           if (REAL_VALUE_ISINF (c2))
6515             {
6516               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6517               if (HONOR_INFINITIES (mode))
6518                 return fold_build2 (EQ_EXPR, type, arg,
6519                                     build_real (TREE_TYPE (arg), c2));
6520
6521               /* sqrt(x) > y is always false, when y is very large
6522                  and we don't care about infinities.  */
6523               return omit_one_operand (type, integer_zero_node, arg);
6524             }
6525
6526           /* sqrt(x) > c is the same as x > c*c.  */
6527           return fold_build2 (code, type, arg,
6528                               build_real (TREE_TYPE (arg), c2));
6529         }
6530       else if (code == LT_EXPR || code == LE_EXPR)
6531         {
6532           REAL_VALUE_TYPE c2;
6533
6534           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6535           real_convert (&c2, mode, &c2);
6536
6537           if (REAL_VALUE_ISINF (c2))
6538             {
6539               /* sqrt(x) < y is always true, when y is a very large
6540                  value and we don't care about NaNs or Infinities.  */
6541               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6542                 return omit_one_operand (type, integer_one_node, arg);
6543
6544               /* sqrt(x) < y is x != +Inf when y is very large and we
6545                  don't care about NaNs.  */
6546               if (! HONOR_NANS (mode))
6547                 return fold_build2 (NE_EXPR, type, arg,
6548                                     build_real (TREE_TYPE (arg), c2));
6549
6550               /* sqrt(x) < y is x >= 0 when y is very large and we
6551                  don't care about Infinities.  */
6552               if (! HONOR_INFINITIES (mode))
6553                 return fold_build2 (GE_EXPR, type, arg,
6554                                     build_real (TREE_TYPE (arg), dconst0));
6555
6556               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6557               if (lang_hooks.decls.global_bindings_p () != 0
6558                   || CONTAINS_PLACEHOLDER_P (arg))
6559                 return NULL_TREE;
6560
6561               arg = save_expr (arg);
6562               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6563                                   fold_build2 (GE_EXPR, type, arg,
6564                                                build_real (TREE_TYPE (arg),
6565                                                            dconst0)),
6566                                   fold_build2 (NE_EXPR, type, arg,
6567                                                build_real (TREE_TYPE (arg),
6568                                                            c2)));
6569             }
6570
6571           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6572           if (! HONOR_NANS (mode))
6573             return fold_build2 (code, type, arg,
6574                                 build_real (TREE_TYPE (arg), c2));
6575
6576           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6577           if (lang_hooks.decls.global_bindings_p () == 0
6578               && ! CONTAINS_PLACEHOLDER_P (arg))
6579             {
6580               arg = save_expr (arg);
6581               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6582                                   fold_build2 (GE_EXPR, type, arg,
6583                                                build_real (TREE_TYPE (arg),
6584                                                            dconst0)),
6585                                   fold_build2 (code, type, arg,
6586                                                build_real (TREE_TYPE (arg),
6587                                                            c2)));
6588             }
6589         }
6590     }
6591
6592   return NULL_TREE;
6593 }
6594
6595 /* Subroutine of fold() that optimizes comparisons against Infinities,
6596    either +Inf or -Inf.
6597
6598    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6599    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6600    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6601
6602    The function returns the constant folded tree if a simplification
6603    can be made, and NULL_TREE otherwise.  */
6604
6605 static tree
6606 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6607 {
6608   enum machine_mode mode;
6609   REAL_VALUE_TYPE max;
6610   tree temp;
6611   bool neg;
6612
6613   mode = TYPE_MODE (TREE_TYPE (arg0));
6614
6615   /* For negative infinity swap the sense of the comparison.  */
6616   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6617   if (neg)
6618     code = swap_tree_comparison (code);
6619
6620   switch (code)
6621     {
6622     case GT_EXPR:
6623       /* x > +Inf is always false, if with ignore sNANs.  */
6624       if (HONOR_SNANS (mode))
6625         return NULL_TREE;
6626       return omit_one_operand (type, integer_zero_node, arg0);
6627
6628     case LE_EXPR:
6629       /* x <= +Inf is always true, if we don't case about NaNs.  */
6630       if (! HONOR_NANS (mode))
6631         return omit_one_operand (type, integer_one_node, arg0);
6632
6633       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6634       if (lang_hooks.decls.global_bindings_p () == 0
6635           && ! CONTAINS_PLACEHOLDER_P (arg0))
6636         {
6637           arg0 = save_expr (arg0);
6638           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6639         }
6640       break;
6641
6642     case EQ_EXPR:
6643     case GE_EXPR:
6644       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6645       real_maxval (&max, neg, mode);
6646       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6647                           arg0, build_real (TREE_TYPE (arg0), max));
6648
6649     case LT_EXPR:
6650       /* x < +Inf is always equal to x <= DBL_MAX.  */
6651       real_maxval (&max, neg, mode);
6652       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6653                           arg0, build_real (TREE_TYPE (arg0), max));
6654
6655     case NE_EXPR:
6656       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6657       real_maxval (&max, neg, mode);
6658       if (! HONOR_NANS (mode))
6659         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6660                             arg0, build_real (TREE_TYPE (arg0), max));
6661
6662       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6663                           arg0, build_real (TREE_TYPE (arg0), max));
6664       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6665
6666     default:
6667       break;
6668     }
6669
6670   return NULL_TREE;
6671 }
6672
6673 /* Subroutine of fold() that optimizes comparisons of a division by
6674    a nonzero integer constant against an integer constant, i.e.
6675    X/C1 op C2.
6676
6677    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6678    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6679    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6680
6681    The function returns the constant folded tree if a simplification
6682    can be made, and NULL_TREE otherwise.  */
6683
6684 static tree
6685 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6686 {
6687   tree prod, tmp, hi, lo;
6688   tree arg00 = TREE_OPERAND (arg0, 0);
6689   tree arg01 = TREE_OPERAND (arg0, 1);
6690   unsigned HOST_WIDE_INT lpart;
6691   HOST_WIDE_INT hpart;
6692   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6693   bool neg_overflow;
6694   int overflow;
6695
6696   /* We have to do this the hard way to detect unsigned overflow.
6697      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6698   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6699                                    TREE_INT_CST_HIGH (arg01),
6700                                    TREE_INT_CST_LOW (arg1),
6701                                    TREE_INT_CST_HIGH (arg1),
6702                                    &lpart, &hpart, unsigned_p);
6703   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6704                                 -1, overflow);
6705   neg_overflow = false;
6706
6707   if (unsigned_p)
6708     {
6709       tmp = int_const_binop (MINUS_EXPR, arg01,
6710                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6711       lo = prod;
6712
6713       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6714       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6715                                        TREE_INT_CST_HIGH (prod),
6716                                        TREE_INT_CST_LOW (tmp),
6717                                        TREE_INT_CST_HIGH (tmp),
6718                                        &lpart, &hpart, unsigned_p);
6719       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6720                                   -1, overflow | TREE_OVERFLOW (prod));
6721     }
6722   else if (tree_int_cst_sgn (arg01) >= 0)
6723     {
6724       tmp = int_const_binop (MINUS_EXPR, arg01,
6725                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6726       switch (tree_int_cst_sgn (arg1))
6727         {
6728         case -1:
6729           neg_overflow = true;
6730           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6731           hi = prod;
6732           break;
6733
6734         case  0:
6735           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6736           hi = tmp;
6737           break;
6738
6739         case  1:
6740           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6741           lo = prod;
6742           break;
6743
6744         default:
6745           gcc_unreachable ();
6746         }
6747     }
6748   else
6749     {
6750       /* A negative divisor reverses the relational operators.  */
6751       code = swap_tree_comparison (code);
6752
6753       tmp = int_const_binop (PLUS_EXPR, arg01,
6754                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6755       switch (tree_int_cst_sgn (arg1))
6756         {
6757         case -1:
6758           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6759           lo = prod;
6760           break;
6761
6762         case  0:
6763           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6764           lo = tmp;
6765           break;
6766
6767         case  1:
6768           neg_overflow = true;
6769           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6770           hi = prod;
6771           break;
6772
6773         default:
6774           gcc_unreachable ();
6775         }
6776     }
6777
6778   switch (code)
6779     {
6780     case EQ_EXPR:
6781       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6782         return omit_one_operand (type, integer_zero_node, arg00);
6783       if (TREE_OVERFLOW (hi))
6784         return fold_build2 (GE_EXPR, type, arg00, lo);
6785       if (TREE_OVERFLOW (lo))
6786         return fold_build2 (LE_EXPR, type, arg00, hi);
6787       return build_range_check (type, arg00, 1, lo, hi);
6788
6789     case NE_EXPR:
6790       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6791         return omit_one_operand (type, integer_one_node, arg00);
6792       if (TREE_OVERFLOW (hi))
6793         return fold_build2 (LT_EXPR, type, arg00, lo);
6794       if (TREE_OVERFLOW (lo))
6795         return fold_build2 (GT_EXPR, type, arg00, hi);
6796       return build_range_check (type, arg00, 0, lo, hi);
6797
6798     case LT_EXPR:
6799       if (TREE_OVERFLOW (lo))
6800         {
6801           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6802           return omit_one_operand (type, tmp, arg00);
6803         }
6804       return fold_build2 (LT_EXPR, type, arg00, lo);
6805
6806     case LE_EXPR:
6807       if (TREE_OVERFLOW (hi))
6808         {
6809           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6810           return omit_one_operand (type, tmp, arg00);
6811         }
6812       return fold_build2 (LE_EXPR, type, arg00, hi);
6813
6814     case GT_EXPR:
6815       if (TREE_OVERFLOW (hi))
6816         {
6817           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6818           return omit_one_operand (type, tmp, arg00);
6819         }
6820       return fold_build2 (GT_EXPR, type, arg00, hi);
6821
6822     case GE_EXPR:
6823       if (TREE_OVERFLOW (lo))
6824         {
6825           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6826           return omit_one_operand (type, tmp, arg00);
6827         }
6828       return fold_build2 (GE_EXPR, type, arg00, lo);
6829
6830     default:
6831       break;
6832     }
6833
6834   return NULL_TREE;
6835 }
6836
6837
6838 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6839    equality/inequality test, then return a simplified form of the test
6840    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6841    result type.  */
6842
6843 static tree
6844 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6845                                      tree result_type)
6846 {
6847   /* If this is testing a single bit, we can optimize the test.  */
6848   if ((code == NE_EXPR || code == EQ_EXPR)
6849       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6850       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6851     {
6852       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6853          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6854       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6855
6856       if (arg00 != NULL_TREE
6857           /* This is only a win if casting to a signed type is cheap,
6858              i.e. when arg00's type is not a partial mode.  */
6859           && TYPE_PRECISION (TREE_TYPE (arg00))
6860              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6861         {
6862           tree stype = signed_type_for (TREE_TYPE (arg00));
6863           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6864                               result_type, fold_convert (stype, arg00),
6865                               build_int_cst (stype, 0));
6866         }
6867     }
6868
6869   return NULL_TREE;
6870 }
6871
6872 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6873    equality/inequality test, then return a simplified form of
6874    the test using shifts and logical operations.  Otherwise return
6875    NULL.  TYPE is the desired result type.  */
6876
6877 tree
6878 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6879                       tree result_type)
6880 {
6881   /* If this is testing a single bit, we can optimize the test.  */
6882   if ((code == NE_EXPR || code == EQ_EXPR)
6883       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6884       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6885     {
6886       tree inner = TREE_OPERAND (arg0, 0);
6887       tree type = TREE_TYPE (arg0);
6888       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6889       enum machine_mode operand_mode = TYPE_MODE (type);
6890       int ops_unsigned;
6891       tree signed_type, unsigned_type, intermediate_type;
6892       tree tem, one;
6893
6894       /* First, see if we can fold the single bit test into a sign-bit
6895          test.  */
6896       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6897                                                  result_type);
6898       if (tem)
6899         return tem;
6900
6901       /* Otherwise we have (A & C) != 0 where C is a single bit,
6902          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6903          Similarly for (A & C) == 0.  */
6904
6905       /* If INNER is a right shift of a constant and it plus BITNUM does
6906          not overflow, adjust BITNUM and INNER.  */
6907       if (TREE_CODE (inner) == RSHIFT_EXPR
6908           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6909           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6910           && bitnum < TYPE_PRECISION (type)
6911           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6912                                    bitnum - TYPE_PRECISION (type)))
6913         {
6914           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6915           inner = TREE_OPERAND (inner, 0);
6916         }
6917
6918       /* If we are going to be able to omit the AND below, we must do our
6919          operations as unsigned.  If we must use the AND, we have a choice.
6920          Normally unsigned is faster, but for some machines signed is.  */
6921 #ifdef LOAD_EXTEND_OP
6922       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6923                       && !flag_syntax_only) ? 0 : 1;
6924 #else
6925       ops_unsigned = 1;
6926 #endif
6927
6928       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6929       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6930       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6931       inner = fold_convert (intermediate_type, inner);
6932
6933       if (bitnum != 0)
6934         inner = build2 (RSHIFT_EXPR, intermediate_type,
6935                         inner, size_int (bitnum));
6936
6937       one = build_int_cst (intermediate_type, 1);
6938
6939       if (code == EQ_EXPR)
6940         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6941
6942       /* Put the AND last so it can combine with more things.  */
6943       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6944
6945       /* Make sure to return the proper type.  */
6946       inner = fold_convert (result_type, inner);
6947
6948       return inner;
6949     }
6950   return NULL_TREE;
6951 }
6952
6953 /* Check whether we are allowed to reorder operands arg0 and arg1,
6954    such that the evaluation of arg1 occurs before arg0.  */
6955
6956 static bool
6957 reorder_operands_p (const_tree arg0, const_tree arg1)
6958 {
6959   if (! flag_evaluation_order)
6960       return true;
6961   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6962     return true;
6963   return ! TREE_SIDE_EFFECTS (arg0)
6964          && ! TREE_SIDE_EFFECTS (arg1);
6965 }
6966
6967 /* Test whether it is preferable two swap two operands, ARG0 and
6968    ARG1, for example because ARG0 is an integer constant and ARG1
6969    isn't.  If REORDER is true, only recommend swapping if we can
6970    evaluate the operands in reverse order.  */
6971
6972 bool
6973 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6974 {
6975   STRIP_SIGN_NOPS (arg0);
6976   STRIP_SIGN_NOPS (arg1);
6977
6978   if (TREE_CODE (arg1) == INTEGER_CST)
6979     return 0;
6980   if (TREE_CODE (arg0) == INTEGER_CST)
6981     return 1;
6982
6983   if (TREE_CODE (arg1) == REAL_CST)
6984     return 0;
6985   if (TREE_CODE (arg0) == REAL_CST)
6986     return 1;
6987
6988   if (TREE_CODE (arg1) == FIXED_CST)
6989     return 0;
6990   if (TREE_CODE (arg0) == FIXED_CST)
6991     return 1;
6992
6993   if (TREE_CODE (arg1) == COMPLEX_CST)
6994     return 0;
6995   if (TREE_CODE (arg0) == COMPLEX_CST)
6996     return 1;
6997
6998   if (TREE_CONSTANT (arg1))
6999     return 0;
7000   if (TREE_CONSTANT (arg0))
7001     return 1;
7002
7003   if (optimize_function_for_size_p (cfun))
7004     return 0;
7005
7006   if (reorder && flag_evaluation_order
7007       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7008     return 0;
7009
7010   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7011      for commutative and comparison operators.  Ensuring a canonical
7012      form allows the optimizers to find additional redundancies without
7013      having to explicitly check for both orderings.  */
7014   if (TREE_CODE (arg0) == SSA_NAME
7015       && TREE_CODE (arg1) == SSA_NAME
7016       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7017     return 1;
7018
7019   /* Put SSA_NAMEs last.  */
7020   if (TREE_CODE (arg1) == SSA_NAME)
7021     return 0;
7022   if (TREE_CODE (arg0) == SSA_NAME)
7023     return 1;
7024
7025   /* Put variables last.  */
7026   if (DECL_P (arg1))
7027     return 0;
7028   if (DECL_P (arg0))
7029     return 1;
7030
7031   return 0;
7032 }
7033
7034 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7035    ARG0 is extended to a wider type.  */
7036
7037 static tree
7038 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7039 {
7040   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7041   tree arg1_unw;
7042   tree shorter_type, outer_type;
7043   tree min, max;
7044   bool above, below;
7045
7046   if (arg0_unw == arg0)
7047     return NULL_TREE;
7048   shorter_type = TREE_TYPE (arg0_unw);
7049
7050 #ifdef HAVE_canonicalize_funcptr_for_compare
7051   /* Disable this optimization if we're casting a function pointer
7052      type on targets that require function pointer canonicalization.  */
7053   if (HAVE_canonicalize_funcptr_for_compare
7054       && TREE_CODE (shorter_type) == POINTER_TYPE
7055       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7056     return NULL_TREE;
7057 #endif
7058
7059   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7060     return NULL_TREE;
7061
7062   arg1_unw = get_unwidened (arg1, NULL_TREE);
7063
7064   /* If possible, express the comparison in the shorter mode.  */
7065   if ((code == EQ_EXPR || code == NE_EXPR
7066        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7067       && (TREE_TYPE (arg1_unw) == shorter_type
7068           || ((TYPE_PRECISION (shorter_type)
7069                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7070               && (TYPE_UNSIGNED (shorter_type)
7071                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7072           || (TREE_CODE (arg1_unw) == INTEGER_CST
7073               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7074                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7075               && int_fits_type_p (arg1_unw, shorter_type))))
7076     return fold_build2 (code, type, arg0_unw,
7077                        fold_convert (shorter_type, arg1_unw));
7078
7079   if (TREE_CODE (arg1_unw) != INTEGER_CST
7080       || TREE_CODE (shorter_type) != INTEGER_TYPE
7081       || !int_fits_type_p (arg1_unw, shorter_type))
7082     return NULL_TREE;
7083
7084   /* If we are comparing with the integer that does not fit into the range
7085      of the shorter type, the result is known.  */
7086   outer_type = TREE_TYPE (arg1_unw);
7087   min = lower_bound_in_type (outer_type, shorter_type);
7088   max = upper_bound_in_type (outer_type, shorter_type);
7089
7090   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7091                                                    max, arg1_unw));
7092   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7093                                                    arg1_unw, min));
7094
7095   switch (code)
7096     {
7097     case EQ_EXPR:
7098       if (above || below)
7099         return omit_one_operand (type, integer_zero_node, arg0);
7100       break;
7101
7102     case NE_EXPR:
7103       if (above || below)
7104         return omit_one_operand (type, integer_one_node, arg0);
7105       break;
7106
7107     case LT_EXPR:
7108     case LE_EXPR:
7109       if (above)
7110         return omit_one_operand (type, integer_one_node, arg0);
7111       else if (below)
7112         return omit_one_operand (type, integer_zero_node, arg0);
7113
7114     case GT_EXPR:
7115     case GE_EXPR:
7116       if (above)
7117         return omit_one_operand (type, integer_zero_node, arg0);
7118       else if (below)
7119         return omit_one_operand (type, integer_one_node, arg0);
7120
7121     default:
7122       break;
7123     }
7124
7125   return NULL_TREE;
7126 }
7127
7128 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7129    ARG0 just the signedness is changed.  */
7130
7131 static tree
7132 fold_sign_changed_comparison (enum tree_code code, tree type,
7133                               tree arg0, tree arg1)
7134 {
7135   tree arg0_inner;
7136   tree inner_type, outer_type;
7137
7138   if (!CONVERT_EXPR_P (arg0))
7139     return NULL_TREE;
7140
7141   outer_type = TREE_TYPE (arg0);
7142   arg0_inner = TREE_OPERAND (arg0, 0);
7143   inner_type = TREE_TYPE (arg0_inner);
7144
7145 #ifdef HAVE_canonicalize_funcptr_for_compare
7146   /* Disable this optimization if we're casting a function pointer
7147      type on targets that require function pointer canonicalization.  */
7148   if (HAVE_canonicalize_funcptr_for_compare
7149       && TREE_CODE (inner_type) == POINTER_TYPE
7150       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7151     return NULL_TREE;
7152 #endif
7153
7154   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7155     return NULL_TREE;
7156
7157   /* If the conversion is from an integral subtype to its basetype
7158      leave it alone.  */
7159   if (TREE_TYPE (inner_type) == outer_type)
7160     return NULL_TREE;
7161
7162   if (TREE_CODE (arg1) != INTEGER_CST
7163       && !(CONVERT_EXPR_P (arg1)
7164            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7165     return NULL_TREE;
7166
7167   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7168        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7169       && code != NE_EXPR
7170       && code != EQ_EXPR)
7171     return NULL_TREE;
7172
7173   if (TREE_CODE (arg1) == INTEGER_CST)
7174     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7175                                   TREE_INT_CST_HIGH (arg1), 0,
7176                                   TREE_OVERFLOW (arg1));
7177   else
7178     arg1 = fold_convert (inner_type, arg1);
7179
7180   return fold_build2 (code, type, arg0_inner, arg1);
7181 }
7182
7183 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7184    step of the array.  Reconstructs s and delta in the case of s * delta
7185    being an integer constant (and thus already folded).
7186    ADDR is the address. MULT is the multiplicative expression.
7187    If the function succeeds, the new address expression is returned.  Otherwise
7188    NULL_TREE is returned.  */
7189
7190 static tree
7191 try_move_mult_to_index (tree addr, tree op1)
7192 {
7193   tree s, delta, step;
7194   tree ref = TREE_OPERAND (addr, 0), pref;
7195   tree ret, pos;
7196   tree itype;
7197   bool mdim = false;
7198
7199   /*  Strip the nops that might be added when converting op1 to sizetype. */
7200   STRIP_NOPS (op1);
7201
7202   /* Canonicalize op1 into a possibly non-constant delta
7203      and an INTEGER_CST s.  */
7204   if (TREE_CODE (op1) == MULT_EXPR)
7205     {
7206       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7207
7208       STRIP_NOPS (arg0);
7209       STRIP_NOPS (arg1);
7210   
7211       if (TREE_CODE (arg0) == INTEGER_CST)
7212         {
7213           s = arg0;
7214           delta = arg1;
7215         }
7216       else if (TREE_CODE (arg1) == INTEGER_CST)
7217         {
7218           s = arg1;
7219           delta = arg0;
7220         }
7221       else
7222         return NULL_TREE;
7223     }
7224   else if (TREE_CODE (op1) == INTEGER_CST)
7225     {
7226       delta = op1;
7227       s = NULL_TREE;
7228     }
7229   else
7230     {
7231       /* Simulate we are delta * 1.  */
7232       delta = op1;
7233       s = integer_one_node;
7234     }
7235
7236   for (;; ref = TREE_OPERAND (ref, 0))
7237     {
7238       if (TREE_CODE (ref) == ARRAY_REF)
7239         {
7240           /* Remember if this was a multi-dimensional array.  */
7241           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7242             mdim = true;
7243
7244           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7245           if (! itype)
7246             continue;
7247
7248           step = array_ref_element_size (ref);
7249           if (TREE_CODE (step) != INTEGER_CST)
7250             continue;
7251
7252           if (s)
7253             {
7254               if (! tree_int_cst_equal (step, s))
7255                 continue;
7256             }
7257           else
7258             {
7259               /* Try if delta is a multiple of step.  */
7260               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7261               if (! tmp)
7262                 continue;
7263               delta = tmp;
7264             }
7265
7266           /* Only fold here if we can verify we do not overflow one
7267              dimension of a multi-dimensional array.  */
7268           if (mdim)
7269             {
7270               tree tmp;
7271
7272               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7273                   || !INTEGRAL_TYPE_P (itype)
7274                   || !TYPE_MAX_VALUE (itype)
7275                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7276                 continue;
7277
7278               tmp = fold_binary (PLUS_EXPR, itype,
7279                                  fold_convert (itype,
7280                                                TREE_OPERAND (ref, 1)),
7281                                  fold_convert (itype, delta));
7282               if (!tmp
7283                   || TREE_CODE (tmp) != INTEGER_CST
7284                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7285                 continue;
7286             }
7287
7288           break;
7289         }
7290       else
7291         mdim = false;
7292
7293       if (!handled_component_p (ref))
7294         return NULL_TREE;
7295     }
7296
7297   /* We found the suitable array reference.  So copy everything up to it,
7298      and replace the index.  */
7299
7300   pref = TREE_OPERAND (addr, 0);
7301   ret = copy_node (pref);
7302   pos = ret;
7303
7304   while (pref != ref)
7305     {
7306       pref = TREE_OPERAND (pref, 0);
7307       TREE_OPERAND (pos, 0) = copy_node (pref);
7308       pos = TREE_OPERAND (pos, 0);
7309     }
7310
7311   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7312                                        fold_convert (itype,
7313                                                      TREE_OPERAND (pos, 1)),
7314                                        fold_convert (itype, delta));
7315
7316   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7317 }
7318
7319
7320 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7321    means A >= Y && A != MAX, but in this case we know that
7322    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7323
7324 static tree
7325 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7326 {
7327   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7328
7329   if (TREE_CODE (bound) == LT_EXPR)
7330     a = TREE_OPERAND (bound, 0);
7331   else if (TREE_CODE (bound) == GT_EXPR)
7332     a = TREE_OPERAND (bound, 1);
7333   else
7334     return NULL_TREE;
7335
7336   typea = TREE_TYPE (a);
7337   if (!INTEGRAL_TYPE_P (typea)
7338       && !POINTER_TYPE_P (typea))
7339     return NULL_TREE;
7340
7341   if (TREE_CODE (ineq) == LT_EXPR)
7342     {
7343       a1 = TREE_OPERAND (ineq, 1);
7344       y = TREE_OPERAND (ineq, 0);
7345     }
7346   else if (TREE_CODE (ineq) == GT_EXPR)
7347     {
7348       a1 = TREE_OPERAND (ineq, 0);
7349       y = TREE_OPERAND (ineq, 1);
7350     }
7351   else
7352     return NULL_TREE;
7353
7354   if (TREE_TYPE (a1) != typea)
7355     return NULL_TREE;
7356
7357   if (POINTER_TYPE_P (typea))
7358     {
7359       /* Convert the pointer types into integer before taking the difference.  */
7360       tree ta = fold_convert (ssizetype, a);
7361       tree ta1 = fold_convert (ssizetype, a1);
7362       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7363     }
7364   else
7365    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7366
7367   if (!diff || !integer_onep (diff))
7368    return NULL_TREE;
7369
7370   return fold_build2 (GE_EXPR, type, a, y);
7371 }
7372
7373 /* Fold a sum or difference of at least one multiplication.
7374    Returns the folded tree or NULL if no simplification could be made.  */
7375
7376 static tree
7377 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7378 {
7379   tree arg00, arg01, arg10, arg11;
7380   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7381
7382   /* (A * C) +- (B * C) -> (A+-B) * C.
7383      (A * C) +- A -> A * (C+-1).
7384      We are most concerned about the case where C is a constant,
7385      but other combinations show up during loop reduction.  Since
7386      it is not difficult, try all four possibilities.  */
7387
7388   if (TREE_CODE (arg0) == MULT_EXPR)
7389     {
7390       arg00 = TREE_OPERAND (arg0, 0);
7391       arg01 = TREE_OPERAND (arg0, 1);
7392     }
7393   else if (TREE_CODE (arg0) == INTEGER_CST)
7394     {
7395       arg00 = build_one_cst (type);
7396       arg01 = arg0;
7397     }
7398   else
7399     {
7400       /* We cannot generate constant 1 for fract.  */
7401       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7402         return NULL_TREE;
7403       arg00 = arg0;
7404       arg01 = build_one_cst (type);
7405     }
7406   if (TREE_CODE (arg1) == MULT_EXPR)
7407     {
7408       arg10 = TREE_OPERAND (arg1, 0);
7409       arg11 = TREE_OPERAND (arg1, 1);
7410     }
7411   else if (TREE_CODE (arg1) == INTEGER_CST)
7412     {
7413       arg10 = build_one_cst (type);
7414       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7415          the purpose of this canonicalization.  */
7416       if (TREE_INT_CST_HIGH (arg1) == -1
7417           && negate_expr_p (arg1)
7418           && code == PLUS_EXPR)
7419         {
7420           arg11 = negate_expr (arg1);
7421           code = MINUS_EXPR;
7422         }
7423       else
7424         arg11 = arg1;
7425     }
7426   else
7427     {
7428       /* We cannot generate constant 1 for fract.  */
7429       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7430         return NULL_TREE;
7431       arg10 = arg1;
7432       arg11 = build_one_cst (type);
7433     }
7434   same = NULL_TREE;
7435
7436   if (operand_equal_p (arg01, arg11, 0))
7437     same = arg01, alt0 = arg00, alt1 = arg10;
7438   else if (operand_equal_p (arg00, arg10, 0))
7439     same = arg00, alt0 = arg01, alt1 = arg11;
7440   else if (operand_equal_p (arg00, arg11, 0))
7441     same = arg00, alt0 = arg01, alt1 = arg10;
7442   else if (operand_equal_p (arg01, arg10, 0))
7443     same = arg01, alt0 = arg00, alt1 = arg11;
7444
7445   /* No identical multiplicands; see if we can find a common
7446      power-of-two factor in non-power-of-two multiplies.  This
7447      can help in multi-dimensional array access.  */
7448   else if (host_integerp (arg01, 0)
7449            && host_integerp (arg11, 0))
7450     {
7451       HOST_WIDE_INT int01, int11, tmp;
7452       bool swap = false;
7453       tree maybe_same;
7454       int01 = TREE_INT_CST_LOW (arg01);
7455       int11 = TREE_INT_CST_LOW (arg11);
7456
7457       /* Move min of absolute values to int11.  */
7458       if ((int01 >= 0 ? int01 : -int01)
7459           < (int11 >= 0 ? int11 : -int11))
7460         {
7461           tmp = int01, int01 = int11, int11 = tmp;
7462           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7463           maybe_same = arg01;
7464           swap = true;
7465         }
7466       else
7467         maybe_same = arg11;
7468
7469       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7470         {
7471           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7472                               build_int_cst (TREE_TYPE (arg00),
7473                                              int01 / int11));
7474           alt1 = arg10;
7475           same = maybe_same;
7476           if (swap)
7477             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7478         }
7479     }
7480
7481   if (same)
7482     return fold_build2 (MULT_EXPR, type,
7483                         fold_build2 (code, type,
7484                                      fold_convert (type, alt0),
7485                                      fold_convert (type, alt1)),
7486                         fold_convert (type, same));
7487
7488   return NULL_TREE;
7489 }
7490
7491 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7492    specified by EXPR into the buffer PTR of length LEN bytes.
7493    Return the number of bytes placed in the buffer, or zero
7494    upon failure.  */
7495
7496 static int
7497 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7498 {
7499   tree type = TREE_TYPE (expr);
7500   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7501   int byte, offset, word, words;
7502   unsigned char value;
7503
7504   if (total_bytes > len)
7505     return 0;
7506   words = total_bytes / UNITS_PER_WORD;
7507
7508   for (byte = 0; byte < total_bytes; byte++)
7509     {
7510       int bitpos = byte * BITS_PER_UNIT;
7511       if (bitpos < HOST_BITS_PER_WIDE_INT)
7512         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7513       else
7514         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7515                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7516
7517       if (total_bytes > UNITS_PER_WORD)
7518         {
7519           word = byte / UNITS_PER_WORD;
7520           if (WORDS_BIG_ENDIAN)
7521             word = (words - 1) - word;
7522           offset = word * UNITS_PER_WORD;
7523           if (BYTES_BIG_ENDIAN)
7524             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7525           else
7526             offset += byte % UNITS_PER_WORD;
7527         }
7528       else
7529         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7530       ptr[offset] = value;
7531     }
7532   return total_bytes;
7533 }
7534
7535
7536 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7537    specified by EXPR into the buffer PTR of length LEN bytes.
7538    Return the number of bytes placed in the buffer, or zero
7539    upon failure.  */
7540
7541 static int
7542 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7543 {
7544   tree type = TREE_TYPE (expr);
7545   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7546   int byte, offset, word, words, bitpos;
7547   unsigned char value;
7548
7549   /* There are always 32 bits in each long, no matter the size of
7550      the hosts long.  We handle floating point representations with
7551      up to 192 bits.  */
7552   long tmp[6];
7553
7554   if (total_bytes > len)
7555     return 0;
7556   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7557
7558   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7559
7560   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7561        bitpos += BITS_PER_UNIT)
7562     {
7563       byte = (bitpos / BITS_PER_UNIT) & 3;
7564       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7565
7566       if (UNITS_PER_WORD < 4)
7567         {
7568           word = byte / UNITS_PER_WORD;
7569           if (WORDS_BIG_ENDIAN)
7570             word = (words - 1) - word;
7571           offset = word * UNITS_PER_WORD;
7572           if (BYTES_BIG_ENDIAN)
7573             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7574           else
7575             offset += byte % UNITS_PER_WORD;
7576         }
7577       else
7578         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7579       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7580     }
7581   return total_bytes;
7582 }
7583
7584 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7585    specified by EXPR into the buffer PTR of length LEN bytes.
7586    Return the number of bytes placed in the buffer, or zero
7587    upon failure.  */
7588
7589 static int
7590 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7591 {
7592   int rsize, isize;
7593   tree part;
7594
7595   part = TREE_REALPART (expr);
7596   rsize = native_encode_expr (part, ptr, len);
7597   if (rsize == 0)
7598     return 0;
7599   part = TREE_IMAGPART (expr);
7600   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7601   if (isize != rsize)
7602     return 0;
7603   return rsize + isize;
7604 }
7605
7606
7607 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7608    specified by EXPR into the buffer PTR of length LEN bytes.
7609    Return the number of bytes placed in the buffer, or zero
7610    upon failure.  */
7611
7612 static int
7613 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7614 {
7615   int i, size, offset, count;
7616   tree itype, elem, elements;
7617
7618   offset = 0;
7619   elements = TREE_VECTOR_CST_ELTS (expr);
7620   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7621   itype = TREE_TYPE (TREE_TYPE (expr));
7622   size = GET_MODE_SIZE (TYPE_MODE (itype));
7623   for (i = 0; i < count; i++)
7624     {
7625       if (elements)
7626         {
7627           elem = TREE_VALUE (elements);
7628           elements = TREE_CHAIN (elements);
7629         }
7630       else
7631         elem = NULL_TREE;
7632
7633       if (elem)
7634         {
7635           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7636             return 0;
7637         }
7638       else
7639         {
7640           if (offset + size > len)
7641             return 0;
7642           memset (ptr+offset, 0, size);
7643         }
7644       offset += size;
7645     }
7646   return offset;
7647 }
7648
7649
7650 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7651    specified by EXPR into the buffer PTR of length LEN bytes.
7652    Return the number of bytes placed in the buffer, or zero
7653    upon failure.  */
7654
7655 static int
7656 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7657 {
7658   tree type = TREE_TYPE (expr);
7659   HOST_WIDE_INT total_bytes;
7660
7661   if (TREE_CODE (type) != ARRAY_TYPE
7662       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7663       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7664       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7665     return 0;
7666   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7667   if (total_bytes > len)
7668     return 0;
7669   if (TREE_STRING_LENGTH (expr) < total_bytes)
7670     {
7671       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7672       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7673               total_bytes - TREE_STRING_LENGTH (expr));
7674     }
7675   else
7676     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7677   return total_bytes;
7678 }
7679
7680
7681 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7682    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7683    buffer PTR of length LEN bytes.  Return the number of bytes
7684    placed in the buffer, or zero upon failure.  */
7685
7686 int
7687 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7688 {
7689   switch (TREE_CODE (expr))
7690     {
7691     case INTEGER_CST:
7692       return native_encode_int (expr, ptr, len);
7693
7694     case REAL_CST:
7695       return native_encode_real (expr, ptr, len);
7696
7697     case COMPLEX_CST:
7698       return native_encode_complex (expr, ptr, len);
7699
7700     case VECTOR_CST:
7701       return native_encode_vector (expr, ptr, len);
7702
7703     case STRING_CST:
7704       return native_encode_string (expr, ptr, len);
7705
7706     default:
7707       return 0;
7708     }
7709 }
7710
7711
7712 /* Subroutine of native_interpret_expr.  Interpret the contents of
7713    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7714    If the buffer cannot be interpreted, return NULL_TREE.  */
7715
7716 static tree
7717 native_interpret_int (tree type, const unsigned char *ptr, int len)
7718 {
7719   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7720   int byte, offset, word, words;
7721   unsigned char value;
7722   unsigned int HOST_WIDE_INT lo = 0;
7723   HOST_WIDE_INT hi = 0;
7724
7725   if (total_bytes > len)
7726     return NULL_TREE;
7727   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7728     return NULL_TREE;
7729   words = total_bytes / UNITS_PER_WORD;
7730
7731   for (byte = 0; byte < total_bytes; byte++)
7732     {
7733       int bitpos = byte * BITS_PER_UNIT;
7734       if (total_bytes > UNITS_PER_WORD)
7735         {
7736           word = byte / UNITS_PER_WORD;
7737           if (WORDS_BIG_ENDIAN)
7738             word = (words - 1) - word;
7739           offset = word * UNITS_PER_WORD;
7740           if (BYTES_BIG_ENDIAN)
7741             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7742           else
7743             offset += byte % UNITS_PER_WORD;
7744         }
7745       else
7746         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7747       value = ptr[offset];
7748
7749       if (bitpos < HOST_BITS_PER_WIDE_INT)
7750         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7751       else
7752         hi |= (unsigned HOST_WIDE_INT) value
7753               << (bitpos - HOST_BITS_PER_WIDE_INT);
7754     }
7755
7756   return build_int_cst_wide_type (type, lo, hi);
7757 }
7758
7759
7760 /* Subroutine of native_interpret_expr.  Interpret the contents of
7761    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7762    If the buffer cannot be interpreted, return NULL_TREE.  */
7763
7764 static tree
7765 native_interpret_real (tree type, const unsigned char *ptr, int len)
7766 {
7767   enum machine_mode mode = TYPE_MODE (type);
7768   int total_bytes = GET_MODE_SIZE (mode);
7769   int byte, offset, word, words, bitpos;
7770   unsigned char value;
7771   /* There are always 32 bits in each long, no matter the size of
7772      the hosts long.  We handle floating point representations with
7773      up to 192 bits.  */
7774   REAL_VALUE_TYPE r;
7775   long tmp[6];
7776
7777   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7778   if (total_bytes > len || total_bytes > 24)
7779     return NULL_TREE;
7780   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7781
7782   memset (tmp, 0, sizeof (tmp));
7783   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7784        bitpos += BITS_PER_UNIT)
7785     {
7786       byte = (bitpos / BITS_PER_UNIT) & 3;
7787       if (UNITS_PER_WORD < 4)
7788         {
7789           word = byte / UNITS_PER_WORD;
7790           if (WORDS_BIG_ENDIAN)
7791             word = (words - 1) - word;
7792           offset = word * UNITS_PER_WORD;
7793           if (BYTES_BIG_ENDIAN)
7794             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7795           else
7796             offset += byte % UNITS_PER_WORD;
7797         }
7798       else
7799         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7800       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7801
7802       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7803     }
7804
7805   real_from_target (&r, tmp, mode);
7806   return build_real (type, r);
7807 }
7808
7809
7810 /* Subroutine of native_interpret_expr.  Interpret the contents of
7811    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7812    If the buffer cannot be interpreted, return NULL_TREE.  */
7813
7814 static tree
7815 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7816 {
7817   tree etype, rpart, ipart;
7818   int size;
7819
7820   etype = TREE_TYPE (type);
7821   size = GET_MODE_SIZE (TYPE_MODE (etype));
7822   if (size * 2 > len)
7823     return NULL_TREE;
7824   rpart = native_interpret_expr (etype, ptr, size);
7825   if (!rpart)
7826     return NULL_TREE;
7827   ipart = native_interpret_expr (etype, ptr+size, size);
7828   if (!ipart)
7829     return NULL_TREE;
7830   return build_complex (type, rpart, ipart);
7831 }
7832
7833
7834 /* Subroutine of native_interpret_expr.  Interpret the contents of
7835    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7836    If the buffer cannot be interpreted, return NULL_TREE.  */
7837
7838 static tree
7839 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7840 {
7841   tree etype, elem, elements;
7842   int i, size, count;
7843
7844   etype = TREE_TYPE (type);
7845   size = GET_MODE_SIZE (TYPE_MODE (etype));
7846   count = TYPE_VECTOR_SUBPARTS (type);
7847   if (size * count > len)
7848     return NULL_TREE;
7849
7850   elements = NULL_TREE;
7851   for (i = count - 1; i >= 0; i--)
7852     {
7853       elem = native_interpret_expr (etype, ptr+(i*size), size);
7854       if (!elem)
7855         return NULL_TREE;
7856       elements = tree_cons (NULL_TREE, elem, elements);
7857     }
7858   return build_vector (type, elements);
7859 }
7860
7861
7862 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7863    the buffer PTR of length LEN as a constant of type TYPE.  For
7864    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7865    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7866    return NULL_TREE.  */
7867
7868 tree
7869 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7870 {
7871   switch (TREE_CODE (type))
7872     {
7873     case INTEGER_TYPE:
7874     case ENUMERAL_TYPE:
7875     case BOOLEAN_TYPE:
7876       return native_interpret_int (type, ptr, len);
7877
7878     case REAL_TYPE:
7879       return native_interpret_real (type, ptr, len);
7880
7881     case COMPLEX_TYPE:
7882       return native_interpret_complex (type, ptr, len);
7883
7884     case VECTOR_TYPE:
7885       return native_interpret_vector (type, ptr, len);
7886
7887     default:
7888       return NULL_TREE;
7889     }
7890 }
7891
7892
7893 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7894    TYPE at compile-time.  If we're unable to perform the conversion
7895    return NULL_TREE.  */
7896
7897 static tree
7898 fold_view_convert_expr (tree type, tree expr)
7899 {
7900   /* We support up to 512-bit values (for V8DFmode).  */
7901   unsigned char buffer[64];
7902   int len;
7903
7904   /* Check that the host and target are sane.  */
7905   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7906     return NULL_TREE;
7907
7908   len = native_encode_expr (expr, buffer, sizeof (buffer));
7909   if (len == 0)
7910     return NULL_TREE;
7911
7912   return native_interpret_expr (type, buffer, len);
7913 }
7914
7915 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7916    to avoid confusing the gimplify process.  */
7917
7918 tree
7919 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7920 {
7921   /* The size of the object is not relevant when talking about its address.  */
7922   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7923     t = TREE_OPERAND (t, 0);
7924
7925   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7926   if (TREE_CODE (t) == INDIRECT_REF
7927       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7928     {
7929       t = TREE_OPERAND (t, 0);
7930
7931       if (TREE_TYPE (t) != ptrtype)
7932         t = build1 (NOP_EXPR, ptrtype, t);
7933     }
7934   else
7935     t = build1 (ADDR_EXPR, ptrtype, t);
7936
7937   return t;
7938 }
7939
7940 /* Build an expression for the address of T.  */
7941
7942 tree
7943 build_fold_addr_expr (tree t)
7944 {
7945   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7946
7947   return build_fold_addr_expr_with_type (t, ptrtype);
7948 }
7949
7950 /* Fold a unary expression of code CODE and type TYPE with operand
7951    OP0.  Return the folded expression if folding is successful.
7952    Otherwise, return NULL_TREE.  */
7953
7954 tree
7955 fold_unary (enum tree_code code, tree type, tree op0)
7956 {
7957   tree tem;
7958   tree arg0;
7959   enum tree_code_class kind = TREE_CODE_CLASS (code);
7960
7961   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7962               && TREE_CODE_LENGTH (code) == 1);
7963
7964   arg0 = op0;
7965   if (arg0)
7966     {
7967       if (CONVERT_EXPR_CODE_P (code)
7968           || code == FLOAT_EXPR || code == ABS_EXPR)
7969         {
7970           /* Don't use STRIP_NOPS, because signedness of argument type
7971              matters.  */
7972           STRIP_SIGN_NOPS (arg0);
7973         }
7974       else
7975         {
7976           /* Strip any conversions that don't change the mode.  This
7977              is safe for every expression, except for a comparison
7978              expression because its signedness is derived from its
7979              operands.
7980
7981              Note that this is done as an internal manipulation within
7982              the constant folder, in order to find the simplest
7983              representation of the arguments so that their form can be
7984              studied.  In any cases, the appropriate type conversions
7985              should be put back in the tree that will get out of the
7986              constant folder.  */
7987           STRIP_NOPS (arg0);
7988         }
7989     }
7990
7991   if (TREE_CODE_CLASS (code) == tcc_unary)
7992     {
7993       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7994         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7995                        fold_build1 (code, type,
7996                                     fold_convert (TREE_TYPE (op0),
7997                                                   TREE_OPERAND (arg0, 1))));
7998       else if (TREE_CODE (arg0) == COND_EXPR)
7999         {
8000           tree arg01 = TREE_OPERAND (arg0, 1);
8001           tree arg02 = TREE_OPERAND (arg0, 2);
8002           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8003             arg01 = fold_build1 (code, type,
8004                                  fold_convert (TREE_TYPE (op0), arg01));
8005           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8006             arg02 = fold_build1 (code, type,
8007                                  fold_convert (TREE_TYPE (op0), arg02));
8008           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8009                              arg01, arg02);
8010
8011           /* If this was a conversion, and all we did was to move into
8012              inside the COND_EXPR, bring it back out.  But leave it if
8013              it is a conversion from integer to integer and the
8014              result precision is no wider than a word since such a
8015              conversion is cheap and may be optimized away by combine,
8016              while it couldn't if it were outside the COND_EXPR.  Then return
8017              so we don't get into an infinite recursion loop taking the
8018              conversion out and then back in.  */
8019
8020           if ((CONVERT_EXPR_CODE_P (code)
8021                || code == NON_LVALUE_EXPR)
8022               && TREE_CODE (tem) == COND_EXPR
8023               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8024               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8025               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8026               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8027               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8028                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8029               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8030                      && (INTEGRAL_TYPE_P
8031                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8032                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8033                   || flag_syntax_only))
8034             tem = build1 (code, type,
8035                           build3 (COND_EXPR,
8036                                   TREE_TYPE (TREE_OPERAND
8037                                              (TREE_OPERAND (tem, 1), 0)),
8038                                   TREE_OPERAND (tem, 0),
8039                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8040                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8041           return tem;
8042         }
8043       else if (COMPARISON_CLASS_P (arg0))
8044         {
8045           if (TREE_CODE (type) == BOOLEAN_TYPE)
8046             {
8047               arg0 = copy_node (arg0);
8048               TREE_TYPE (arg0) = type;
8049               return arg0;
8050             }
8051           else if (TREE_CODE (type) != INTEGER_TYPE)
8052             return fold_build3 (COND_EXPR, type, arg0,
8053                                 fold_build1 (code, type,
8054                                              integer_one_node),
8055                                 fold_build1 (code, type,
8056                                              integer_zero_node));
8057         }
8058    }
8059
8060   switch (code)
8061     {
8062     case PAREN_EXPR:
8063       /* Re-association barriers around constants and other re-association
8064          barriers can be removed.  */
8065       if (CONSTANT_CLASS_P (op0)
8066           || TREE_CODE (op0) == PAREN_EXPR)
8067         return fold_convert (type, op0);
8068       return NULL_TREE;
8069
8070     CASE_CONVERT:
8071     case FLOAT_EXPR:
8072     case FIX_TRUNC_EXPR:
8073       if (TREE_TYPE (op0) == type)
8074         return op0;
8075       
8076       /* If we have (type) (a CMP b) and type is an integral type, return
8077          new expression involving the new type.  */
8078       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8079         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8080                             TREE_OPERAND (op0, 1));
8081
8082       /* Handle cases of two conversions in a row.  */
8083       if (CONVERT_EXPR_P (op0))
8084         {
8085           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8086           tree inter_type = TREE_TYPE (op0);
8087           int inside_int = INTEGRAL_TYPE_P (inside_type);
8088           int inside_ptr = POINTER_TYPE_P (inside_type);
8089           int inside_float = FLOAT_TYPE_P (inside_type);
8090           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8091           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8092           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8093           int inter_int = INTEGRAL_TYPE_P (inter_type);
8094           int inter_ptr = POINTER_TYPE_P (inter_type);
8095           int inter_float = FLOAT_TYPE_P (inter_type);
8096           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8097           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8098           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8099           int final_int = INTEGRAL_TYPE_P (type);
8100           int final_ptr = POINTER_TYPE_P (type);
8101           int final_float = FLOAT_TYPE_P (type);
8102           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8103           unsigned int final_prec = TYPE_PRECISION (type);
8104           int final_unsignedp = TYPE_UNSIGNED (type);
8105
8106           /* In addition to the cases of two conversions in a row
8107              handled below, if we are converting something to its own
8108              type via an object of identical or wider precision, neither
8109              conversion is needed.  */
8110           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8111               && (((inter_int || inter_ptr) && final_int)
8112                   || (inter_float && final_float))
8113               && inter_prec >= final_prec)
8114             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8115
8116           /* Likewise, if the intermediate and initial types are either both
8117              float or both integer, we don't need the middle conversion if the
8118              former is wider than the latter and doesn't change the signedness
8119              (for integers).  Avoid this if the final type is a pointer since
8120              then we sometimes need the middle conversion.  Likewise if the
8121              final type has a precision not equal to the size of its mode.  */
8122           if (((inter_int && inside_int)
8123                || (inter_float && inside_float)
8124                || (inter_vec && inside_vec))
8125               && inter_prec >= inside_prec
8126               && (inter_float || inter_vec
8127                   || inter_unsignedp == inside_unsignedp)
8128               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8129                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8130               && ! final_ptr
8131               && (! final_vec || inter_prec == inside_prec))
8132             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8133
8134           /* If we have a sign-extension of a zero-extended value, we can
8135              replace that by a single zero-extension.  */
8136           if (inside_int && inter_int && final_int
8137               && inside_prec < inter_prec && inter_prec < final_prec
8138               && inside_unsignedp && !inter_unsignedp)
8139             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8140
8141           /* Two conversions in a row are not needed unless:
8142              - some conversion is floating-point (overstrict for now), or
8143              - some conversion is a vector (overstrict for now), or
8144              - the intermediate type is narrower than both initial and
8145                final, or
8146              - the intermediate type and innermost type differ in signedness,
8147                and the outermost type is wider than the intermediate, or
8148              - the initial type is a pointer type and the precisions of the
8149                intermediate and final types differ, or
8150              - the final type is a pointer type and the precisions of the
8151                initial and intermediate types differ.  */
8152           if (! inside_float && ! inter_float && ! final_float
8153               && ! inside_vec && ! inter_vec && ! final_vec
8154               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8155               && ! (inside_int && inter_int
8156                     && inter_unsignedp != inside_unsignedp
8157                     && inter_prec < final_prec)
8158               && ((inter_unsignedp && inter_prec > inside_prec)
8159                   == (final_unsignedp && final_prec > inter_prec))
8160               && ! (inside_ptr && inter_prec != final_prec)
8161               && ! (final_ptr && inside_prec != inter_prec)
8162               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8163                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8164             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8165         }
8166
8167       /* Handle (T *)&A.B.C for A being of type T and B and C
8168          living at offset zero.  This occurs frequently in
8169          C++ upcasting and then accessing the base.  */
8170       if (TREE_CODE (op0) == ADDR_EXPR
8171           && POINTER_TYPE_P (type)
8172           && handled_component_p (TREE_OPERAND (op0, 0)))
8173         {
8174           HOST_WIDE_INT bitsize, bitpos;
8175           tree offset;
8176           enum machine_mode mode;
8177           int unsignedp, volatilep;
8178           tree base = TREE_OPERAND (op0, 0);
8179           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8180                                       &mode, &unsignedp, &volatilep, false);
8181           /* If the reference was to a (constant) zero offset, we can use
8182              the address of the base if it has the same base type
8183              as the result type.  */
8184           if (! offset && bitpos == 0
8185               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8186                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8187             return fold_convert (type, build_fold_addr_expr (base));
8188         }
8189
8190       if (TREE_CODE (op0) == MODIFY_EXPR
8191           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8192           /* Detect assigning a bitfield.  */
8193           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8194                && DECL_BIT_FIELD
8195                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8196         {
8197           /* Don't leave an assignment inside a conversion
8198              unless assigning a bitfield.  */
8199           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8200           /* First do the assignment, then return converted constant.  */
8201           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8202           TREE_NO_WARNING (tem) = 1;
8203           TREE_USED (tem) = 1;
8204           return tem;
8205         }
8206
8207       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8208          constants (if x has signed type, the sign bit cannot be set
8209          in c).  This folds extension into the BIT_AND_EXPR.
8210          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8211          very likely don't have maximal range for their precision and this
8212          transformation effectively doesn't preserve non-maximal ranges.  */
8213       if (TREE_CODE (type) == INTEGER_TYPE
8214           && TREE_CODE (op0) == BIT_AND_EXPR
8215           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8216           /* Not if the conversion is to the sub-type.  */
8217           && TREE_TYPE (type) != TREE_TYPE (op0))
8218         {
8219           tree and = op0;
8220           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8221           int change = 0;
8222
8223           if (TYPE_UNSIGNED (TREE_TYPE (and))
8224               || (TYPE_PRECISION (type)
8225                   <= TYPE_PRECISION (TREE_TYPE (and))))
8226             change = 1;
8227           else if (TYPE_PRECISION (TREE_TYPE (and1))
8228                    <= HOST_BITS_PER_WIDE_INT
8229                    && host_integerp (and1, 1))
8230             {
8231               unsigned HOST_WIDE_INT cst;
8232
8233               cst = tree_low_cst (and1, 1);
8234               cst &= (HOST_WIDE_INT) -1
8235                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8236               change = (cst == 0);
8237 #ifdef LOAD_EXTEND_OP
8238               if (change
8239                   && !flag_syntax_only
8240                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8241                       == ZERO_EXTEND))
8242                 {
8243                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8244                   and0 = fold_convert (uns, and0);
8245                   and1 = fold_convert (uns, and1);
8246                 }
8247 #endif
8248             }
8249           if (change)
8250             {
8251               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8252                                            TREE_INT_CST_HIGH (and1), 0,
8253                                            TREE_OVERFLOW (and1));
8254               return fold_build2 (BIT_AND_EXPR, type,
8255                                   fold_convert (type, and0), tem);
8256             }
8257         }
8258
8259       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8260          when one of the new casts will fold away. Conservatively we assume
8261          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8262       if (POINTER_TYPE_P (type)
8263           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8264           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8265               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8266               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8267         {
8268           tree arg00 = TREE_OPERAND (arg0, 0);
8269           tree arg01 = TREE_OPERAND (arg0, 1);
8270
8271           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8272                               fold_convert (sizetype, arg01));
8273         }
8274
8275       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8276          of the same precision, and X is an integer type not narrower than
8277          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8278       if (INTEGRAL_TYPE_P (type)
8279           && TREE_CODE (op0) == BIT_NOT_EXPR
8280           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8281           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8282           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8283         {
8284           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8285           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8286               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8287             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8288         }
8289
8290       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8291          type of X and Y (integer types only).  */
8292       if (INTEGRAL_TYPE_P (type)
8293           && TREE_CODE (op0) == MULT_EXPR
8294           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8295           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8296         {
8297           /* Be careful not to introduce new overflows.  */
8298           tree mult_type;
8299           if (TYPE_OVERFLOW_WRAPS (type))
8300             mult_type = type;
8301           else
8302             mult_type = unsigned_type_for (type);
8303
8304           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8305             {
8306               tem = fold_build2 (MULT_EXPR, mult_type,
8307                                  fold_convert (mult_type,
8308                                                TREE_OPERAND (op0, 0)),
8309                                  fold_convert (mult_type,
8310                                                TREE_OPERAND (op0, 1)));
8311               return fold_convert (type, tem);
8312             }
8313         }
8314
8315       tem = fold_convert_const (code, type, op0);
8316       return tem ? tem : NULL_TREE;
8317
8318     case FIXED_CONVERT_EXPR:
8319       tem = fold_convert_const (code, type, arg0);
8320       return tem ? tem : NULL_TREE;
8321
8322     case VIEW_CONVERT_EXPR:
8323       if (TREE_TYPE (op0) == type)
8324         return op0;
8325       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8326         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8327
8328       /* For integral conversions with the same precision or pointer
8329          conversions use a NOP_EXPR instead.  */
8330       if ((INTEGRAL_TYPE_P (type)
8331            || POINTER_TYPE_P (type))
8332           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8333               || POINTER_TYPE_P (TREE_TYPE (op0)))
8334           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8335           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8336              a sub-type to its base type as generated by the Ada FE.  */
8337           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8338                && TREE_TYPE (TREE_TYPE (op0))))
8339         return fold_convert (type, op0);
8340
8341       /* Strip inner integral conversions that do not change the precision.  */
8342       if (CONVERT_EXPR_P (op0)
8343           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8344               || POINTER_TYPE_P (TREE_TYPE (op0)))
8345           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8346               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8347           && (TYPE_PRECISION (TREE_TYPE (op0))
8348               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8349         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8350
8351       return fold_view_convert_expr (type, op0);
8352
8353     case NEGATE_EXPR:
8354       tem = fold_negate_expr (arg0);
8355       if (tem)
8356         return fold_convert (type, tem);
8357       return NULL_TREE;
8358
8359     case ABS_EXPR:
8360       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8361         return fold_abs_const (arg0, type);
8362       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8363         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8364       /* Convert fabs((double)float) into (double)fabsf(float).  */
8365       else if (TREE_CODE (arg0) == NOP_EXPR
8366                && TREE_CODE (type) == REAL_TYPE)
8367         {
8368           tree targ0 = strip_float_extensions (arg0);
8369           if (targ0 != arg0)
8370             return fold_convert (type, fold_build1 (ABS_EXPR,
8371                                                     TREE_TYPE (targ0),
8372                                                     targ0));
8373         }
8374       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8375       else if (TREE_CODE (arg0) == ABS_EXPR)
8376         return arg0;
8377       else if (tree_expr_nonnegative_p (arg0))
8378         return arg0;
8379
8380       /* Strip sign ops from argument.  */
8381       if (TREE_CODE (type) == REAL_TYPE)
8382         {
8383           tem = fold_strip_sign_ops (arg0);
8384           if (tem)
8385             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8386         }
8387       return NULL_TREE;
8388
8389     case CONJ_EXPR:
8390       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8391         return fold_convert (type, arg0);
8392       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8393         {
8394           tree itype = TREE_TYPE (type);
8395           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8396           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8397           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8398         }
8399       if (TREE_CODE (arg0) == COMPLEX_CST)
8400         {
8401           tree itype = TREE_TYPE (type);
8402           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8403           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8404           return build_complex (type, rpart, negate_expr (ipart));
8405         }
8406       if (TREE_CODE (arg0) == CONJ_EXPR)
8407         return fold_convert (type, TREE_OPERAND (arg0, 0));
8408       return NULL_TREE;
8409
8410     case BIT_NOT_EXPR:
8411       if (TREE_CODE (arg0) == INTEGER_CST)
8412         return fold_not_const (arg0, type);
8413       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8414         return fold_convert (type, TREE_OPERAND (arg0, 0));
8415       /* Convert ~ (-A) to A - 1.  */
8416       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8417         return fold_build2 (MINUS_EXPR, type,
8418                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8419                             build_int_cst (type, 1));
8420       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8421       else if (INTEGRAL_TYPE_P (type)
8422                && ((TREE_CODE (arg0) == MINUS_EXPR
8423                     && integer_onep (TREE_OPERAND (arg0, 1)))
8424                    || (TREE_CODE (arg0) == PLUS_EXPR
8425                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8426         return fold_build1 (NEGATE_EXPR, type,
8427                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8428       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8429       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8430                && (tem = fold_unary (BIT_NOT_EXPR, type,
8431                                      fold_convert (type,
8432                                                    TREE_OPERAND (arg0, 0)))))
8433         return fold_build2 (BIT_XOR_EXPR, type, tem,
8434                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8435       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8436                && (tem = fold_unary (BIT_NOT_EXPR, type,
8437                                      fold_convert (type,
8438                                                    TREE_OPERAND (arg0, 1)))))
8439         return fold_build2 (BIT_XOR_EXPR, type,
8440                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8441       /* Perform BIT_NOT_EXPR on each element individually.  */
8442       else if (TREE_CODE (arg0) == VECTOR_CST)
8443         {
8444           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8445           int count = TYPE_VECTOR_SUBPARTS (type), i;
8446
8447           for (i = 0; i < count; i++)
8448             {
8449               if (elements)
8450                 {
8451                   elem = TREE_VALUE (elements);
8452                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8453                   if (elem == NULL_TREE)
8454                     break;
8455                   elements = TREE_CHAIN (elements);
8456                 }
8457               else
8458                 elem = build_int_cst (TREE_TYPE (type), -1);
8459               list = tree_cons (NULL_TREE, elem, list);
8460             }
8461           if (i == count)
8462             return build_vector (type, nreverse (list));
8463         }
8464
8465       return NULL_TREE;
8466
8467     case TRUTH_NOT_EXPR:
8468       /* The argument to invert_truthvalue must have Boolean type.  */
8469       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8470           arg0 = fold_convert (boolean_type_node, arg0);
8471
8472       /* Note that the operand of this must be an int
8473          and its values must be 0 or 1.
8474          ("true" is a fixed value perhaps depending on the language,
8475          but we don't handle values other than 1 correctly yet.)  */
8476       tem = fold_truth_not_expr (arg0);
8477       if (!tem)
8478         return NULL_TREE;
8479       return fold_convert (type, tem);
8480
8481     case REALPART_EXPR:
8482       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8483         return fold_convert (type, arg0);
8484       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8485         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8486                                  TREE_OPERAND (arg0, 1));
8487       if (TREE_CODE (arg0) == COMPLEX_CST)
8488         return fold_convert (type, TREE_REALPART (arg0));
8489       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8490         {
8491           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8492           tem = fold_build2 (TREE_CODE (arg0), itype,
8493                              fold_build1 (REALPART_EXPR, itype,
8494                                           TREE_OPERAND (arg0, 0)),
8495                              fold_build1 (REALPART_EXPR, itype,
8496                                           TREE_OPERAND (arg0, 1)));
8497           return fold_convert (type, tem);
8498         }
8499       if (TREE_CODE (arg0) == CONJ_EXPR)
8500         {
8501           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8502           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8503           return fold_convert (type, tem);
8504         }
8505       if (TREE_CODE (arg0) == CALL_EXPR)
8506         {
8507           tree fn = get_callee_fndecl (arg0);
8508           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8509             switch (DECL_FUNCTION_CODE (fn))
8510               {
8511               CASE_FLT_FN (BUILT_IN_CEXPI):
8512                 fn = mathfn_built_in (type, BUILT_IN_COS);
8513                 if (fn)
8514                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8515                 break;
8516
8517               default:
8518                 break;
8519               }
8520         }
8521       return NULL_TREE;
8522
8523     case IMAGPART_EXPR:
8524       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8525         return fold_convert (type, integer_zero_node);
8526       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8527         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8528                                  TREE_OPERAND (arg0, 0));
8529       if (TREE_CODE (arg0) == COMPLEX_CST)
8530         return fold_convert (type, TREE_IMAGPART (arg0));
8531       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8532         {
8533           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8534           tem = fold_build2 (TREE_CODE (arg0), itype,
8535                              fold_build1 (IMAGPART_EXPR, itype,
8536                                           TREE_OPERAND (arg0, 0)),
8537                              fold_build1 (IMAGPART_EXPR, itype,
8538                                           TREE_OPERAND (arg0, 1)));
8539           return fold_convert (type, tem);
8540         }
8541       if (TREE_CODE (arg0) == CONJ_EXPR)
8542         {
8543           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8544           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8545           return fold_convert (type, negate_expr (tem));
8546         }
8547       if (TREE_CODE (arg0) == CALL_EXPR)
8548         {
8549           tree fn = get_callee_fndecl (arg0);
8550           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8551             switch (DECL_FUNCTION_CODE (fn))
8552               {
8553               CASE_FLT_FN (BUILT_IN_CEXPI):
8554                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8555                 if (fn)
8556                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8557                 break;
8558
8559               default:
8560                 break;
8561               }
8562         }
8563       return NULL_TREE;
8564
8565     default:
8566       return NULL_TREE;
8567     } /* switch (code) */
8568 }
8569
8570
8571 /* If the operation was a conversion do _not_ mark a resulting constant
8572    with TREE_OVERFLOW if the original constant was not.  These conversions
8573    have implementation defined behavior and retaining the TREE_OVERFLOW
8574    flag here would confuse later passes such as VRP.  */
8575 tree
8576 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8577 {
8578   tree res = fold_unary (code, type, op0);
8579   if (res
8580       && TREE_CODE (res) == INTEGER_CST
8581       && TREE_CODE (op0) == INTEGER_CST
8582       && CONVERT_EXPR_CODE_P (code))
8583     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8584
8585   return res;
8586 }
8587
8588 /* Fold a binary expression of code CODE and type TYPE with operands
8589    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8590    Return the folded expression if folding is successful.  Otherwise,
8591    return NULL_TREE.  */
8592
8593 static tree
8594 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8595 {
8596   enum tree_code compl_code;
8597
8598   if (code == MIN_EXPR)
8599     compl_code = MAX_EXPR;
8600   else if (code == MAX_EXPR)
8601     compl_code = MIN_EXPR;
8602   else
8603     gcc_unreachable ();
8604
8605   /* MIN (MAX (a, b), b) == b.  */
8606   if (TREE_CODE (op0) == compl_code
8607       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8608     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8609
8610   /* MIN (MAX (b, a), b) == b.  */
8611   if (TREE_CODE (op0) == compl_code
8612       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8613       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8614     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8615
8616   /* MIN (a, MAX (a, b)) == a.  */
8617   if (TREE_CODE (op1) == compl_code
8618       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8619       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8620     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8621
8622   /* MIN (a, MAX (b, a)) == a.  */
8623   if (TREE_CODE (op1) == compl_code
8624       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8625       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8626     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8627
8628   return NULL_TREE;
8629 }
8630
8631 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8632    by changing CODE to reduce the magnitude of constants involved in
8633    ARG0 of the comparison.
8634    Returns a canonicalized comparison tree if a simplification was
8635    possible, otherwise returns NULL_TREE.
8636    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8637    valid if signed overflow is undefined.  */
8638
8639 static tree
8640 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8641                                  tree arg0, tree arg1,
8642                                  bool *strict_overflow_p)
8643 {
8644   enum tree_code code0 = TREE_CODE (arg0);
8645   tree t, cst0 = NULL_TREE;
8646   int sgn0;
8647   bool swap = false;
8648
8649   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8650      first form only if overflow is undefined.  */
8651   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8652          /* In principle pointers also have undefined overflow behavior,
8653             but that causes problems elsewhere.  */
8654          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8655          && (code0 == MINUS_EXPR
8656              || code0 == PLUS_EXPR)
8657          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8658         || code0 == INTEGER_CST))
8659     return NULL_TREE;
8660
8661   /* Identify the constant in arg0 and its sign.  */
8662   if (code0 == INTEGER_CST)
8663     cst0 = arg0;
8664   else
8665     cst0 = TREE_OPERAND (arg0, 1);
8666   sgn0 = tree_int_cst_sgn (cst0);
8667
8668   /* Overflowed constants and zero will cause problems.  */
8669   if (integer_zerop (cst0)
8670       || TREE_OVERFLOW (cst0))
8671     return NULL_TREE;
8672
8673   /* See if we can reduce the magnitude of the constant in
8674      arg0 by changing the comparison code.  */
8675   if (code0 == INTEGER_CST)
8676     {
8677       /* CST <= arg1  ->  CST-1 < arg1.  */
8678       if (code == LE_EXPR && sgn0 == 1)
8679         code = LT_EXPR;
8680       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8681       else if (code == LT_EXPR && sgn0 == -1)
8682         code = LE_EXPR;
8683       /* CST > arg1  ->  CST-1 >= arg1.  */
8684       else if (code == GT_EXPR && sgn0 == 1)
8685         code = GE_EXPR;
8686       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8687       else if (code == GE_EXPR && sgn0 == -1)
8688         code = GT_EXPR;
8689       else
8690         return NULL_TREE;
8691       /* arg1 code' CST' might be more canonical.  */
8692       swap = true;
8693     }
8694   else
8695     {
8696       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8697       if (code == LT_EXPR
8698           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8699         code = LE_EXPR;
8700       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8701       else if (code == GT_EXPR
8702                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8703         code = GE_EXPR;
8704       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8705       else if (code == LE_EXPR
8706                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8707         code = LT_EXPR;
8708       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8709       else if (code == GE_EXPR
8710                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8711         code = GT_EXPR;
8712       else
8713         return NULL_TREE;
8714       *strict_overflow_p = true;
8715     }
8716
8717   /* Now build the constant reduced in magnitude.  But not if that
8718      would produce one outside of its types range.  */
8719   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8720       && ((sgn0 == 1
8721            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8722            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8723           || (sgn0 == -1
8724               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8725               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8726     /* We cannot swap the comparison here as that would cause us to
8727        endlessly recurse.  */
8728     return NULL_TREE;
8729
8730   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8731                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8732   if (code0 != INTEGER_CST)
8733     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8734
8735   /* If swapping might yield to a more canonical form, do so.  */
8736   if (swap)
8737     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8738   else
8739     return fold_build2 (code, type, t, arg1);
8740 }
8741
8742 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8743    overflow further.  Try to decrease the magnitude of constants involved
8744    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8745    and put sole constants at the second argument position.
8746    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8747
8748 static tree
8749 maybe_canonicalize_comparison (enum tree_code code, tree type,
8750                                tree arg0, tree arg1)
8751 {
8752   tree t;
8753   bool strict_overflow_p;
8754   const char * const warnmsg = G_("assuming signed overflow does not occur "
8755                                   "when reducing constant in comparison");
8756
8757   /* Try canonicalization by simplifying arg0.  */
8758   strict_overflow_p = false;
8759   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8760                                        &strict_overflow_p);
8761   if (t)
8762     {
8763       if (strict_overflow_p)
8764         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8765       return t;
8766     }
8767
8768   /* Try canonicalization by simplifying arg1 using the swapped
8769      comparison.  */
8770   code = swap_tree_comparison (code);
8771   strict_overflow_p = false;
8772   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8773                                        &strict_overflow_p);
8774   if (t && strict_overflow_p)
8775     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8776   return t;
8777 }
8778
8779 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8780    space.  This is used to avoid issuing overflow warnings for
8781    expressions like &p->x which can not wrap.  */
8782
8783 static bool
8784 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8785 {
8786   unsigned HOST_WIDE_INT offset_low, total_low;
8787   HOST_WIDE_INT size, offset_high, total_high;
8788
8789   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8790     return true;
8791
8792   if (bitpos < 0)
8793     return true;
8794
8795   if (offset == NULL_TREE)
8796     {
8797       offset_low = 0;
8798       offset_high = 0;
8799     }
8800   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8801     return true;
8802   else
8803     {
8804       offset_low = TREE_INT_CST_LOW (offset);
8805       offset_high = TREE_INT_CST_HIGH (offset);
8806     }
8807
8808   if (add_double_with_sign (offset_low, offset_high,
8809                             bitpos / BITS_PER_UNIT, 0,
8810                             &total_low, &total_high,
8811                             true))
8812     return true;
8813
8814   if (total_high != 0)
8815     return true;
8816
8817   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8818   if (size <= 0)
8819     return true;
8820
8821   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8822      array.  */
8823   if (TREE_CODE (base) == ADDR_EXPR)
8824     {
8825       HOST_WIDE_INT base_size;
8826
8827       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8828       if (base_size > 0 && size < base_size)
8829         size = base_size;
8830     }
8831
8832   return total_low > (unsigned HOST_WIDE_INT) size;
8833 }
8834
8835 /* Subroutine of fold_binary.  This routine performs all of the
8836    transformations that are common to the equality/inequality
8837    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8838    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8839    fold_binary should call fold_binary.  Fold a comparison with
8840    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8841    the folded comparison or NULL_TREE.  */
8842
8843 static tree
8844 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8845 {
8846   tree arg0, arg1, tem;
8847
8848   arg0 = op0;
8849   arg1 = op1;
8850
8851   STRIP_SIGN_NOPS (arg0);
8852   STRIP_SIGN_NOPS (arg1);
8853
8854   tem = fold_relational_const (code, type, arg0, arg1);
8855   if (tem != NULL_TREE)
8856     return tem;
8857
8858   /* If one arg is a real or integer constant, put it last.  */
8859   if (tree_swap_operands_p (arg0, arg1, true))
8860     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8861
8862   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8863   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8864       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8865           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8866           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8867       && (TREE_CODE (arg1) == INTEGER_CST
8868           && !TREE_OVERFLOW (arg1)))
8869     {
8870       tree const1 = TREE_OPERAND (arg0, 1);
8871       tree const2 = arg1;
8872       tree variable = TREE_OPERAND (arg0, 0);
8873       tree lhs;
8874       int lhs_add;
8875       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8876
8877       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8878                          TREE_TYPE (arg1), const2, const1);
8879
8880       /* If the constant operation overflowed this can be
8881          simplified as a comparison against INT_MAX/INT_MIN.  */
8882       if (TREE_CODE (lhs) == INTEGER_CST
8883           && TREE_OVERFLOW (lhs))
8884         {
8885           int const1_sgn = tree_int_cst_sgn (const1);
8886           enum tree_code code2 = code;
8887
8888           /* Get the sign of the constant on the lhs if the
8889              operation were VARIABLE + CONST1.  */
8890           if (TREE_CODE (arg0) == MINUS_EXPR)
8891             const1_sgn = -const1_sgn;
8892
8893           /* The sign of the constant determines if we overflowed
8894              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8895              Canonicalize to the INT_MIN overflow by swapping the comparison
8896              if necessary.  */
8897           if (const1_sgn == -1)
8898             code2 = swap_tree_comparison (code);
8899
8900           /* We now can look at the canonicalized case
8901                VARIABLE + 1  CODE2  INT_MIN
8902              and decide on the result.  */
8903           if (code2 == LT_EXPR
8904               || code2 == LE_EXPR
8905               || code2 == EQ_EXPR)
8906             return omit_one_operand (type, boolean_false_node, variable);
8907           else if (code2 == NE_EXPR
8908                    || code2 == GE_EXPR
8909                    || code2 == GT_EXPR)
8910             return omit_one_operand (type, boolean_true_node, variable);
8911         }
8912
8913       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8914           && (TREE_CODE (lhs) != INTEGER_CST
8915               || !TREE_OVERFLOW (lhs)))
8916         {
8917           fold_overflow_warning (("assuming signed overflow does not occur "
8918                                   "when changing X +- C1 cmp C2 to "
8919                                   "X cmp C1 +- C2"),
8920                                  WARN_STRICT_OVERFLOW_COMPARISON);
8921           return fold_build2 (code, type, variable, lhs);
8922         }
8923     }
8924
8925   /* For comparisons of pointers we can decompose it to a compile time
8926      comparison of the base objects and the offsets into the object.
8927      This requires at least one operand being an ADDR_EXPR or a
8928      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8929   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8930       && (TREE_CODE (arg0) == ADDR_EXPR
8931           || TREE_CODE (arg1) == ADDR_EXPR
8932           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8933           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8934     {
8935       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8936       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8937       enum machine_mode mode;
8938       int volatilep, unsignedp;
8939       bool indirect_base0 = false, indirect_base1 = false;
8940
8941       /* Get base and offset for the access.  Strip ADDR_EXPR for
8942          get_inner_reference, but put it back by stripping INDIRECT_REF
8943          off the base object if possible.  indirect_baseN will be true
8944          if baseN is not an address but refers to the object itself.  */
8945       base0 = arg0;
8946       if (TREE_CODE (arg0) == ADDR_EXPR)
8947         {
8948           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8949                                        &bitsize, &bitpos0, &offset0, &mode,
8950                                        &unsignedp, &volatilep, false);
8951           if (TREE_CODE (base0) == INDIRECT_REF)
8952             base0 = TREE_OPERAND (base0, 0);
8953           else
8954             indirect_base0 = true;
8955         }
8956       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8957         {
8958           base0 = TREE_OPERAND (arg0, 0);
8959           offset0 = TREE_OPERAND (arg0, 1);
8960         }
8961
8962       base1 = arg1;
8963       if (TREE_CODE (arg1) == ADDR_EXPR)
8964         {
8965           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8966                                        &bitsize, &bitpos1, &offset1, &mode,
8967                                        &unsignedp, &volatilep, false);
8968           if (TREE_CODE (base1) == INDIRECT_REF)
8969             base1 = TREE_OPERAND (base1, 0);
8970           else
8971             indirect_base1 = true;
8972         }
8973       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8974         {
8975           base1 = TREE_OPERAND (arg1, 0);
8976           offset1 = TREE_OPERAND (arg1, 1);
8977         }
8978
8979       /* If we have equivalent bases we might be able to simplify.  */
8980       if (indirect_base0 == indirect_base1
8981           && operand_equal_p (base0, base1, 0))
8982         {
8983           /* We can fold this expression to a constant if the non-constant
8984              offset parts are equal.  */
8985           if ((offset0 == offset1
8986                || (offset0 && offset1
8987                    && operand_equal_p (offset0, offset1, 0)))
8988               && (code == EQ_EXPR
8989                   || code == NE_EXPR
8990                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
8991                 
8992             {
8993               if (code != EQ_EXPR
8994                   && code != NE_EXPR
8995                   && bitpos0 != bitpos1
8996                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
8997                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
8998                 fold_overflow_warning (("assuming pointer wraparound does not "
8999                                         "occur when comparing P +- C1 with "
9000                                         "P +- C2"),
9001                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9002
9003               switch (code)
9004                 {
9005                 case EQ_EXPR:
9006                   return constant_boolean_node (bitpos0 == bitpos1, type);
9007                 case NE_EXPR:
9008                   return constant_boolean_node (bitpos0 != bitpos1, type);
9009                 case LT_EXPR:
9010                   return constant_boolean_node (bitpos0 < bitpos1, type);
9011                 case LE_EXPR:
9012                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9013                 case GE_EXPR:
9014                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9015                 case GT_EXPR:
9016                   return constant_boolean_node (bitpos0 > bitpos1, type);
9017                 default:;
9018                 }
9019             }
9020           /* We can simplify the comparison to a comparison of the variable
9021              offset parts if the constant offset parts are equal.
9022              Be careful to use signed size type here because otherwise we
9023              mess with array offsets in the wrong way.  This is possible
9024              because pointer arithmetic is restricted to retain within an
9025              object and overflow on pointer differences is undefined as of
9026              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9027           else if (bitpos0 == bitpos1
9028                    && ((code == EQ_EXPR || code == NE_EXPR)
9029                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9030             {
9031               tree signed_size_type_node;
9032               signed_size_type_node = signed_type_for (size_type_node);
9033
9034               /* By converting to signed size type we cover middle-end pointer
9035                  arithmetic which operates on unsigned pointer types of size
9036                  type size and ARRAY_REF offsets which are properly sign or
9037                  zero extended from their type in case it is narrower than
9038                  size type.  */
9039               if (offset0 == NULL_TREE)
9040                 offset0 = build_int_cst (signed_size_type_node, 0);
9041               else
9042                 offset0 = fold_convert (signed_size_type_node, offset0);
9043               if (offset1 == NULL_TREE)
9044                 offset1 = build_int_cst (signed_size_type_node, 0);
9045               else
9046                 offset1 = fold_convert (signed_size_type_node, offset1);
9047
9048               if (code != EQ_EXPR
9049                   && code != NE_EXPR
9050                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9051                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9052                 fold_overflow_warning (("assuming pointer wraparound does not "
9053                                         "occur when comparing P +- C1 with "
9054                                         "P +- C2"),
9055                                        WARN_STRICT_OVERFLOW_COMPARISON);
9056
9057               return fold_build2 (code, type, offset0, offset1);
9058             }
9059         }
9060       /* For non-equal bases we can simplify if they are addresses
9061          of local binding decls or constants.  */
9062       else if (indirect_base0 && indirect_base1
9063                /* We know that !operand_equal_p (base0, base1, 0)
9064                   because the if condition was false.  But make
9065                   sure two decls are not the same.  */
9066                && base0 != base1
9067                && TREE_CODE (arg0) == ADDR_EXPR
9068                && TREE_CODE (arg1) == ADDR_EXPR
9069                && (((TREE_CODE (base0) == VAR_DECL
9070                      || TREE_CODE (base0) == PARM_DECL)
9071                     && (targetm.binds_local_p (base0)
9072                         || CONSTANT_CLASS_P (base1)))
9073                    || CONSTANT_CLASS_P (base0))
9074                && (((TREE_CODE (base1) == VAR_DECL
9075                      || TREE_CODE (base1) == PARM_DECL)
9076                     && (targetm.binds_local_p (base1)
9077                         || CONSTANT_CLASS_P (base0)))
9078                    || CONSTANT_CLASS_P (base1)))
9079         {
9080           if (code == EQ_EXPR)
9081             return omit_two_operands (type, boolean_false_node, arg0, arg1);
9082           else if (code == NE_EXPR)
9083             return omit_two_operands (type, boolean_true_node, arg0, arg1);
9084         }
9085       /* For equal offsets we can simplify to a comparison of the
9086          base addresses.  */
9087       else if (bitpos0 == bitpos1
9088                && (indirect_base0
9089                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9090                && (indirect_base1
9091                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9092                && ((offset0 == offset1)
9093                    || (offset0 && offset1
9094                        && operand_equal_p (offset0, offset1, 0))))
9095         {
9096           if (indirect_base0)
9097             base0 = build_fold_addr_expr (base0);
9098           if (indirect_base1)
9099             base1 = build_fold_addr_expr (base1);
9100           return fold_build2 (code, type, base0, base1);
9101         }
9102     }
9103
9104   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9105      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9106      the resulting offset is smaller in absolute value than the
9107      original one.  */
9108   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9109       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9110       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9111           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9112       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9113       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9114           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9115     {
9116       tree const1 = TREE_OPERAND (arg0, 1);
9117       tree const2 = TREE_OPERAND (arg1, 1);
9118       tree variable1 = TREE_OPERAND (arg0, 0);
9119       tree variable2 = TREE_OPERAND (arg1, 0);
9120       tree cst;
9121       const char * const warnmsg = G_("assuming signed overflow does not "
9122                                       "occur when combining constants around "
9123                                       "a comparison");
9124
9125       /* Put the constant on the side where it doesn't overflow and is
9126          of lower absolute value than before.  */
9127       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9128                              ? MINUS_EXPR : PLUS_EXPR,
9129                              const2, const1, 0);
9130       if (!TREE_OVERFLOW (cst)
9131           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9132         {
9133           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9134           return fold_build2 (code, type,
9135                               variable1,
9136                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9137                                            variable2, cst));
9138         }
9139
9140       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9141                              ? MINUS_EXPR : PLUS_EXPR,
9142                              const1, const2, 0);
9143       if (!TREE_OVERFLOW (cst)
9144           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9145         {
9146           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9147           return fold_build2 (code, type,
9148                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9149                                            variable1, cst),
9150                               variable2);
9151         }
9152     }
9153
9154   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9155      signed arithmetic case.  That form is created by the compiler
9156      often enough for folding it to be of value.  One example is in
9157      computing loop trip counts after Operator Strength Reduction.  */
9158   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9159       && TREE_CODE (arg0) == MULT_EXPR
9160       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9161           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9162       && integer_zerop (arg1))
9163     {
9164       tree const1 = TREE_OPERAND (arg0, 1);
9165       tree const2 = arg1;                       /* zero */
9166       tree variable1 = TREE_OPERAND (arg0, 0);
9167       enum tree_code cmp_code = code;
9168
9169       gcc_assert (!integer_zerop (const1));
9170
9171       fold_overflow_warning (("assuming signed overflow does not occur when "
9172                               "eliminating multiplication in comparison "
9173                               "with zero"),
9174                              WARN_STRICT_OVERFLOW_COMPARISON);
9175
9176       /* If const1 is negative we swap the sense of the comparison.  */
9177       if (tree_int_cst_sgn (const1) < 0)
9178         cmp_code = swap_tree_comparison (cmp_code);
9179
9180       return fold_build2 (cmp_code, type, variable1, const2);
9181     }
9182
9183   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9184   if (tem)
9185     return tem;
9186
9187   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9188     {
9189       tree targ0 = strip_float_extensions (arg0);
9190       tree targ1 = strip_float_extensions (arg1);
9191       tree newtype = TREE_TYPE (targ0);
9192
9193       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9194         newtype = TREE_TYPE (targ1);
9195
9196       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9197       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9198         return fold_build2 (code, type, fold_convert (newtype, targ0),
9199                             fold_convert (newtype, targ1));
9200
9201       /* (-a) CMP (-b) -> b CMP a  */
9202       if (TREE_CODE (arg0) == NEGATE_EXPR
9203           && TREE_CODE (arg1) == NEGATE_EXPR)
9204         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9205                             TREE_OPERAND (arg0, 0));
9206
9207       if (TREE_CODE (arg1) == REAL_CST)
9208         {
9209           REAL_VALUE_TYPE cst;
9210           cst = TREE_REAL_CST (arg1);
9211
9212           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9213           if (TREE_CODE (arg0) == NEGATE_EXPR)
9214             return fold_build2 (swap_tree_comparison (code), type,
9215                                 TREE_OPERAND (arg0, 0),
9216                                 build_real (TREE_TYPE (arg1),
9217                                             REAL_VALUE_NEGATE (cst)));
9218
9219           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9220           /* a CMP (-0) -> a CMP 0  */
9221           if (REAL_VALUE_MINUS_ZERO (cst))
9222             return fold_build2 (code, type, arg0,
9223                                 build_real (TREE_TYPE (arg1), dconst0));
9224
9225           /* x != NaN is always true, other ops are always false.  */
9226           if (REAL_VALUE_ISNAN (cst)
9227               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9228             {
9229               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9230               return omit_one_operand (type, tem, arg0);
9231             }
9232
9233           /* Fold comparisons against infinity.  */
9234           if (REAL_VALUE_ISINF (cst)
9235               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9236             {
9237               tem = fold_inf_compare (code, type, arg0, arg1);
9238               if (tem != NULL_TREE)
9239                 return tem;
9240             }
9241         }
9242
9243       /* If this is a comparison of a real constant with a PLUS_EXPR
9244          or a MINUS_EXPR of a real constant, we can convert it into a
9245          comparison with a revised real constant as long as no overflow
9246          occurs when unsafe_math_optimizations are enabled.  */
9247       if (flag_unsafe_math_optimizations
9248           && TREE_CODE (arg1) == REAL_CST
9249           && (TREE_CODE (arg0) == PLUS_EXPR
9250               || TREE_CODE (arg0) == MINUS_EXPR)
9251           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9252           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9253                                       ? MINUS_EXPR : PLUS_EXPR,
9254                                       arg1, TREE_OPERAND (arg0, 1), 0))
9255           && !TREE_OVERFLOW (tem))
9256         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9257
9258       /* Likewise, we can simplify a comparison of a real constant with
9259          a MINUS_EXPR whose first operand is also a real constant, i.e.
9260          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9261          floating-point types only if -fassociative-math is set.  */
9262       if (flag_associative_math
9263           && TREE_CODE (arg1) == REAL_CST
9264           && TREE_CODE (arg0) == MINUS_EXPR
9265           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9266           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9267                                       arg1, 0))
9268           && !TREE_OVERFLOW (tem))
9269         return fold_build2 (swap_tree_comparison (code), type,
9270                             TREE_OPERAND (arg0, 1), tem);
9271
9272       /* Fold comparisons against built-in math functions.  */
9273       if (TREE_CODE (arg1) == REAL_CST
9274           && flag_unsafe_math_optimizations
9275           && ! flag_errno_math)
9276         {
9277           enum built_in_function fcode = builtin_mathfn_code (arg0);
9278
9279           if (fcode != END_BUILTINS)
9280             {
9281               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9282               if (tem != NULL_TREE)
9283                 return tem;
9284             }
9285         }
9286     }
9287
9288   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9289       && CONVERT_EXPR_P (arg0))
9290     {
9291       /* If we are widening one operand of an integer comparison,
9292          see if the other operand is similarly being widened.  Perhaps we
9293          can do the comparison in the narrower type.  */
9294       tem = fold_widened_comparison (code, type, arg0, arg1);
9295       if (tem)
9296         return tem;
9297
9298       /* Or if we are changing signedness.  */
9299       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9300       if (tem)
9301         return tem;
9302     }
9303
9304   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9305      constant, we can simplify it.  */
9306   if (TREE_CODE (arg1) == INTEGER_CST
9307       && (TREE_CODE (arg0) == MIN_EXPR
9308           || TREE_CODE (arg0) == MAX_EXPR)
9309       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9310     {
9311       tem = optimize_minmax_comparison (code, type, op0, op1);
9312       if (tem)
9313         return tem;
9314     }
9315
9316   /* Simplify comparison of something with itself.  (For IEEE
9317      floating-point, we can only do some of these simplifications.)  */
9318   if (operand_equal_p (arg0, arg1, 0))
9319     {
9320       switch (code)
9321         {
9322         case EQ_EXPR:
9323           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9324               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9325             return constant_boolean_node (1, type);
9326           break;
9327
9328         case GE_EXPR:
9329         case LE_EXPR:
9330           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9331               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9332             return constant_boolean_node (1, type);
9333           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9334
9335         case NE_EXPR:
9336           /* For NE, we can only do this simplification if integer
9337              or we don't honor IEEE floating point NaNs.  */
9338           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9339               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9340             break;
9341           /* ... fall through ...  */
9342         case GT_EXPR:
9343         case LT_EXPR:
9344           return constant_boolean_node (0, type);
9345         default:
9346           gcc_unreachable ();
9347         }
9348     }
9349
9350   /* If we are comparing an expression that just has comparisons
9351      of two integer values, arithmetic expressions of those comparisons,
9352      and constants, we can simplify it.  There are only three cases
9353      to check: the two values can either be equal, the first can be
9354      greater, or the second can be greater.  Fold the expression for
9355      those three values.  Since each value must be 0 or 1, we have
9356      eight possibilities, each of which corresponds to the constant 0
9357      or 1 or one of the six possible comparisons.
9358
9359      This handles common cases like (a > b) == 0 but also handles
9360      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9361      occur in macroized code.  */
9362
9363   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9364     {
9365       tree cval1 = 0, cval2 = 0;
9366       int save_p = 0;
9367
9368       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9369           /* Don't handle degenerate cases here; they should already
9370              have been handled anyway.  */
9371           && cval1 != 0 && cval2 != 0
9372           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9373           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9374           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9375           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9376           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9377           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9378                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9379         {
9380           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9381           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9382
9383           /* We can't just pass T to eval_subst in case cval1 or cval2
9384              was the same as ARG1.  */
9385
9386           tree high_result
9387                 = fold_build2 (code, type,
9388                                eval_subst (arg0, cval1, maxval,
9389                                            cval2, minval),
9390                                arg1);
9391           tree equal_result
9392                 = fold_build2 (code, type,
9393                                eval_subst (arg0, cval1, maxval,
9394                                            cval2, maxval),
9395                                arg1);
9396           tree low_result
9397                 = fold_build2 (code, type,
9398                                eval_subst (arg0, cval1, minval,
9399                                            cval2, maxval),
9400                                arg1);
9401
9402           /* All three of these results should be 0 or 1.  Confirm they are.
9403              Then use those values to select the proper code to use.  */
9404
9405           if (TREE_CODE (high_result) == INTEGER_CST
9406               && TREE_CODE (equal_result) == INTEGER_CST
9407               && TREE_CODE (low_result) == INTEGER_CST)
9408             {
9409               /* Make a 3-bit mask with the high-order bit being the
9410                  value for `>', the next for '=', and the low for '<'.  */
9411               switch ((integer_onep (high_result) * 4)
9412                       + (integer_onep (equal_result) * 2)
9413                       + integer_onep (low_result))
9414                 {
9415                 case 0:
9416                   /* Always false.  */
9417                   return omit_one_operand (type, integer_zero_node, arg0);
9418                 case 1:
9419                   code = LT_EXPR;
9420                   break;
9421                 case 2:
9422                   code = EQ_EXPR;
9423                   break;
9424                 case 3:
9425                   code = LE_EXPR;
9426                   break;
9427                 case 4:
9428                   code = GT_EXPR;
9429                   break;
9430                 case 5:
9431                   code = NE_EXPR;
9432                   break;
9433                 case 6:
9434                   code = GE_EXPR;
9435                   break;
9436                 case 7:
9437                   /* Always true.  */
9438                   return omit_one_operand (type, integer_one_node, arg0);
9439                 }
9440
9441               if (save_p)
9442                 return save_expr (build2 (code, type, cval1, cval2));
9443               return fold_build2 (code, type, cval1, cval2);
9444             }
9445         }
9446     }
9447
9448   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9449      into a single range test.  */
9450   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9451        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9452       && TREE_CODE (arg1) == INTEGER_CST
9453       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9454       && !integer_zerop (TREE_OPERAND (arg0, 1))
9455       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9456       && !TREE_OVERFLOW (arg1))
9457     {
9458       tem = fold_div_compare (code, type, arg0, arg1);
9459       if (tem != NULL_TREE)
9460         return tem;
9461     }
9462
9463   /* Fold ~X op ~Y as Y op X.  */
9464   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9465       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9466     {
9467       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9468       return fold_build2 (code, type,
9469                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9470                           TREE_OPERAND (arg0, 0));
9471     }
9472
9473   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9474   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9475       && TREE_CODE (arg1) == INTEGER_CST)
9476     {
9477       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9478       return fold_build2 (swap_tree_comparison (code), type,
9479                           TREE_OPERAND (arg0, 0),
9480                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9481                                        fold_convert (cmp_type, arg1)));
9482     }
9483
9484   return NULL_TREE;
9485 }
9486
9487
9488 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9489    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9490    argument EXPR represents the expression "z" of type TYPE.  */
9491
9492 static tree
9493 fold_mult_zconjz (tree type, tree expr)
9494 {
9495   tree itype = TREE_TYPE (type);
9496   tree rpart, ipart, tem;
9497
9498   if (TREE_CODE (expr) == COMPLEX_EXPR)
9499     {
9500       rpart = TREE_OPERAND (expr, 0);
9501       ipart = TREE_OPERAND (expr, 1);
9502     }
9503   else if (TREE_CODE (expr) == COMPLEX_CST)
9504     {
9505       rpart = TREE_REALPART (expr);
9506       ipart = TREE_IMAGPART (expr);
9507     }
9508   else
9509     {
9510       expr = save_expr (expr);
9511       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9512       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9513     }
9514
9515   rpart = save_expr (rpart);
9516   ipart = save_expr (ipart);
9517   tem = fold_build2 (PLUS_EXPR, itype,
9518                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9519                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9520   return fold_build2 (COMPLEX_EXPR, type, tem,
9521                       fold_convert (itype, integer_zero_node));
9522 }
9523
9524
9525 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9526    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9527    guarantees that P and N have the same least significant log2(M) bits.
9528    N is not otherwise constrained.  In particular, N is not normalized to
9529    0 <= N < M as is common.  In general, the precise value of P is unknown.
9530    M is chosen as large as possible such that constant N can be determined.
9531
9532    Returns M and sets *RESIDUE to N.
9533
9534    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9535    account.  This is not always possible due to PR 35705.
9536  */
9537
9538 static unsigned HOST_WIDE_INT
9539 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9540                                  bool allow_func_align)
9541 {
9542   enum tree_code code;
9543
9544   *residue = 0;
9545
9546   code = TREE_CODE (expr);
9547   if (code == ADDR_EXPR)
9548     {
9549       expr = TREE_OPERAND (expr, 0);
9550       if (handled_component_p (expr))
9551         {
9552           HOST_WIDE_INT bitsize, bitpos;
9553           tree offset;
9554           enum machine_mode mode;
9555           int unsignedp, volatilep;
9556
9557           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9558                                       &mode, &unsignedp, &volatilep, false);
9559           *residue = bitpos / BITS_PER_UNIT;
9560           if (offset)
9561             {
9562               if (TREE_CODE (offset) == INTEGER_CST)
9563                 *residue += TREE_INT_CST_LOW (offset);
9564               else
9565                 /* We don't handle more complicated offset expressions.  */
9566                 return 1;
9567             }
9568         }
9569
9570       if (DECL_P (expr)
9571           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9572         return DECL_ALIGN_UNIT (expr);
9573     }
9574   else if (code == POINTER_PLUS_EXPR)
9575     {
9576       tree op0, op1;
9577       unsigned HOST_WIDE_INT modulus;
9578       enum tree_code inner_code;
9579       
9580       op0 = TREE_OPERAND (expr, 0);
9581       STRIP_NOPS (op0);
9582       modulus = get_pointer_modulus_and_residue (op0, residue,
9583                                                  allow_func_align);
9584
9585       op1 = TREE_OPERAND (expr, 1);
9586       STRIP_NOPS (op1);
9587       inner_code = TREE_CODE (op1);
9588       if (inner_code == INTEGER_CST)
9589         {
9590           *residue += TREE_INT_CST_LOW (op1);
9591           return modulus;
9592         }
9593       else if (inner_code == MULT_EXPR)
9594         {
9595           op1 = TREE_OPERAND (op1, 1);
9596           if (TREE_CODE (op1) == INTEGER_CST)
9597             {
9598               unsigned HOST_WIDE_INT align;
9599               
9600               /* Compute the greatest power-of-2 divisor of op1.  */
9601               align = TREE_INT_CST_LOW (op1);
9602               align &= -align;
9603
9604               /* If align is non-zero and less than *modulus, replace
9605                  *modulus with align., If align is 0, then either op1 is 0
9606                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9607                  unsigned HOST_WIDE_INT.  In either case, no additional
9608                  constraint is imposed.  */
9609               if (align)
9610                 modulus = MIN (modulus, align);
9611
9612               return modulus;
9613             }
9614         }
9615     }
9616
9617     /* If we get here, we were unable to determine anything useful about the
9618        expression.  */
9619     return 1;
9620 }
9621
9622
9623 /* Fold a binary expression of code CODE and type TYPE with operands
9624    OP0 and OP1.  Return the folded expression if folding is
9625    successful.  Otherwise, return NULL_TREE.  */
9626
9627 tree
9628 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9629 {
9630   enum tree_code_class kind = TREE_CODE_CLASS (code);
9631   tree arg0, arg1, tem;
9632   tree t1 = NULL_TREE;
9633   bool strict_overflow_p;
9634
9635   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9636               && TREE_CODE_LENGTH (code) == 2
9637               && op0 != NULL_TREE
9638               && op1 != NULL_TREE);
9639
9640   arg0 = op0;
9641   arg1 = op1;
9642
9643   /* Strip any conversions that don't change the mode.  This is
9644      safe for every expression, except for a comparison expression
9645      because its signedness is derived from its operands.  So, in
9646      the latter case, only strip conversions that don't change the
9647      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9648      preserved.
9649
9650      Note that this is done as an internal manipulation within the
9651      constant folder, in order to find the simplest representation
9652      of the arguments so that their form can be studied.  In any
9653      cases, the appropriate type conversions should be put back in
9654      the tree that will get out of the constant folder.  */
9655
9656   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9657     {
9658       STRIP_SIGN_NOPS (arg0);
9659       STRIP_SIGN_NOPS (arg1);
9660     }
9661   else
9662     {
9663       STRIP_NOPS (arg0);
9664       STRIP_NOPS (arg1);
9665     }
9666
9667   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9668      constant but we can't do arithmetic on them.  */
9669   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9670       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9671       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9672       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9673       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9674       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9675     {
9676       if (kind == tcc_binary)
9677         {
9678           /* Make sure type and arg0 have the same saturating flag.  */
9679           gcc_assert (TYPE_SATURATING (type)
9680                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9681           tem = const_binop (code, arg0, arg1, 0);
9682         }
9683       else if (kind == tcc_comparison)
9684         tem = fold_relational_const (code, type, arg0, arg1);
9685       else
9686         tem = NULL_TREE;
9687
9688       if (tem != NULL_TREE)
9689         {
9690           if (TREE_TYPE (tem) != type)
9691             tem = fold_convert (type, tem);
9692           return tem;
9693         }
9694     }
9695
9696   /* If this is a commutative operation, and ARG0 is a constant, move it
9697      to ARG1 to reduce the number of tests below.  */
9698   if (commutative_tree_code (code)
9699       && tree_swap_operands_p (arg0, arg1, true))
9700     return fold_build2 (code, type, op1, op0);
9701
9702   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9703
9704      First check for cases where an arithmetic operation is applied to a
9705      compound, conditional, or comparison operation.  Push the arithmetic
9706      operation inside the compound or conditional to see if any folding
9707      can then be done.  Convert comparison to conditional for this purpose.
9708      The also optimizes non-constant cases that used to be done in
9709      expand_expr.
9710
9711      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9712      one of the operands is a comparison and the other is a comparison, a
9713      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9714      code below would make the expression more complex.  Change it to a
9715      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9716      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9717
9718   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9719        || code == EQ_EXPR || code == NE_EXPR)
9720       && ((truth_value_p (TREE_CODE (arg0))
9721            && (truth_value_p (TREE_CODE (arg1))
9722                || (TREE_CODE (arg1) == BIT_AND_EXPR
9723                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9724           || (truth_value_p (TREE_CODE (arg1))
9725               && (truth_value_p (TREE_CODE (arg0))
9726                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9727                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9728     {
9729       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9730                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9731                          : TRUTH_XOR_EXPR,
9732                          boolean_type_node,
9733                          fold_convert (boolean_type_node, arg0),
9734                          fold_convert (boolean_type_node, arg1));
9735
9736       if (code == EQ_EXPR)
9737         tem = invert_truthvalue (tem);
9738
9739       return fold_convert (type, tem);
9740     }
9741
9742   if (TREE_CODE_CLASS (code) == tcc_binary
9743       || TREE_CODE_CLASS (code) == tcc_comparison)
9744     {
9745       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9746         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9747                        fold_build2 (code, type,
9748                                     fold_convert (TREE_TYPE (op0),
9749                                                   TREE_OPERAND (arg0, 1)),
9750                                     op1));
9751       if (TREE_CODE (arg1) == COMPOUND_EXPR
9752           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9753         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9754                        fold_build2 (code, type, op0,
9755                                     fold_convert (TREE_TYPE (op1),
9756                                                   TREE_OPERAND (arg1, 1))));
9757
9758       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9759         {
9760           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9761                                                      arg0, arg1, 
9762                                                      /*cond_first_p=*/1);
9763           if (tem != NULL_TREE)
9764             return tem;
9765         }
9766
9767       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9768         {
9769           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9770                                                      arg1, arg0, 
9771                                                      /*cond_first_p=*/0);
9772           if (tem != NULL_TREE)
9773             return tem;
9774         }
9775     }
9776
9777   switch (code)
9778     {
9779     case POINTER_PLUS_EXPR:
9780       /* 0 +p index -> (type)index */
9781       if (integer_zerop (arg0))
9782         return non_lvalue (fold_convert (type, arg1));
9783
9784       /* PTR +p 0 -> PTR */
9785       if (integer_zerop (arg1))
9786         return non_lvalue (fold_convert (type, arg0));
9787
9788       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9789       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9790            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9791         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9792                                                 fold_convert (sizetype, arg1),
9793                                                 fold_convert (sizetype, arg0)));
9794
9795       /* index +p PTR -> PTR +p index */
9796       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9797           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9798         return fold_build2 (POINTER_PLUS_EXPR, type,
9799                             fold_convert (type, arg1),
9800                             fold_convert (sizetype, arg0));
9801
9802       /* (PTR +p B) +p A -> PTR +p (B + A) */
9803       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9804         {
9805           tree inner;
9806           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9807           tree arg00 = TREE_OPERAND (arg0, 0);
9808           inner = fold_build2 (PLUS_EXPR, sizetype,
9809                                arg01, fold_convert (sizetype, arg1));
9810           return fold_convert (type,
9811                                fold_build2 (POINTER_PLUS_EXPR,
9812                                             TREE_TYPE (arg00), arg00, inner));
9813         }
9814
9815       /* PTR_CST +p CST -> CST1 */
9816       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9817         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9818
9819      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9820         of the array.  Loop optimizer sometimes produce this type of
9821         expressions.  */
9822       if (TREE_CODE (arg0) == ADDR_EXPR)
9823         {
9824           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9825           if (tem)
9826             return fold_convert (type, tem);
9827         }
9828
9829       return NULL_TREE;
9830
9831     case PLUS_EXPR:
9832       /* A + (-B) -> A - B */
9833       if (TREE_CODE (arg1) == NEGATE_EXPR)
9834         return fold_build2 (MINUS_EXPR, type,
9835                             fold_convert (type, arg0),
9836                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9837       /* (-A) + B -> B - A */
9838       if (TREE_CODE (arg0) == NEGATE_EXPR
9839           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9840         return fold_build2 (MINUS_EXPR, type,
9841                             fold_convert (type, arg1),
9842                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9843
9844       if (INTEGRAL_TYPE_P (type))
9845         {
9846           /* Convert ~A + 1 to -A.  */
9847           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9848               && integer_onep (arg1))
9849             return fold_build1 (NEGATE_EXPR, type,
9850                                 fold_convert (type, TREE_OPERAND (arg0, 0)));
9851
9852           /* ~X + X is -1.  */
9853           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9854               && !TYPE_OVERFLOW_TRAPS (type))
9855             {
9856               tree tem = TREE_OPERAND (arg0, 0);
9857
9858               STRIP_NOPS (tem);
9859               if (operand_equal_p (tem, arg1, 0))
9860                 {
9861                   t1 = build_int_cst_type (type, -1);
9862                   return omit_one_operand (type, t1, arg1);
9863                 }
9864             }
9865
9866           /* X + ~X is -1.  */
9867           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9868               && !TYPE_OVERFLOW_TRAPS (type))
9869             {
9870               tree tem = TREE_OPERAND (arg1, 0);
9871
9872               STRIP_NOPS (tem);
9873               if (operand_equal_p (arg0, tem, 0))
9874                 {
9875                   t1 = build_int_cst_type (type, -1);
9876                   return omit_one_operand (type, t1, arg0);
9877                 }
9878             }
9879
9880           /* X + (X / CST) * -CST is X % CST.  */
9881           if (TREE_CODE (arg1) == MULT_EXPR
9882               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9883               && operand_equal_p (arg0,
9884                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9885             {
9886               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9887               tree cst1 = TREE_OPERAND (arg1, 1);
9888               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9889               if (sum && integer_zerop (sum))
9890                 return fold_convert (type,
9891                                      fold_build2 (TRUNC_MOD_EXPR,
9892                                                   TREE_TYPE (arg0), arg0, cst0));
9893             }
9894         }
9895
9896       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9897          same or one.  Make sure type is not saturating.
9898          fold_plusminus_mult_expr will re-associate.  */
9899       if ((TREE_CODE (arg0) == MULT_EXPR
9900            || TREE_CODE (arg1) == MULT_EXPR)
9901           && !TYPE_SATURATING (type)
9902           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9903         {
9904           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9905           if (tem)
9906             return tem;
9907         }
9908
9909       if (! FLOAT_TYPE_P (type))
9910         {
9911           if (integer_zerop (arg1))
9912             return non_lvalue (fold_convert (type, arg0));
9913
9914           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9915              with a constant, and the two constants have no bits in common,
9916              we should treat this as a BIT_IOR_EXPR since this may produce more
9917              simplifications.  */
9918           if (TREE_CODE (arg0) == BIT_AND_EXPR
9919               && TREE_CODE (arg1) == BIT_AND_EXPR
9920               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9921               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9922               && integer_zerop (const_binop (BIT_AND_EXPR,
9923                                              TREE_OPERAND (arg0, 1),
9924                                              TREE_OPERAND (arg1, 1), 0)))
9925             {
9926               code = BIT_IOR_EXPR;
9927               goto bit_ior;
9928             }
9929
9930           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9931              (plus (plus (mult) (mult)) (foo)) so that we can
9932              take advantage of the factoring cases below.  */
9933           if (((TREE_CODE (arg0) == PLUS_EXPR
9934                 || TREE_CODE (arg0) == MINUS_EXPR)
9935                && TREE_CODE (arg1) == MULT_EXPR)
9936               || ((TREE_CODE (arg1) == PLUS_EXPR
9937                    || TREE_CODE (arg1) == MINUS_EXPR)
9938                   && TREE_CODE (arg0) == MULT_EXPR))
9939             {
9940               tree parg0, parg1, parg, marg;
9941               enum tree_code pcode;
9942
9943               if (TREE_CODE (arg1) == MULT_EXPR)
9944                 parg = arg0, marg = arg1;
9945               else
9946                 parg = arg1, marg = arg0;
9947               pcode = TREE_CODE (parg);
9948               parg0 = TREE_OPERAND (parg, 0);
9949               parg1 = TREE_OPERAND (parg, 1);
9950               STRIP_NOPS (parg0);
9951               STRIP_NOPS (parg1);
9952
9953               if (TREE_CODE (parg0) == MULT_EXPR
9954                   && TREE_CODE (parg1) != MULT_EXPR)
9955                 return fold_build2 (pcode, type,
9956                                     fold_build2 (PLUS_EXPR, type,
9957                                                  fold_convert (type, parg0),
9958                                                  fold_convert (type, marg)),
9959                                     fold_convert (type, parg1));
9960               if (TREE_CODE (parg0) != MULT_EXPR
9961                   && TREE_CODE (parg1) == MULT_EXPR)
9962                 return fold_build2 (PLUS_EXPR, type,
9963                                     fold_convert (type, parg0),
9964                                     fold_build2 (pcode, type,
9965                                                  fold_convert (type, marg),
9966                                                  fold_convert (type,
9967                                                                parg1)));
9968             }
9969         }
9970       else
9971         {
9972           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9973           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9974             return non_lvalue (fold_convert (type, arg0));
9975
9976           /* Likewise if the operands are reversed.  */
9977           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9978             return non_lvalue (fold_convert (type, arg1));
9979
9980           /* Convert X + -C into X - C.  */
9981           if (TREE_CODE (arg1) == REAL_CST
9982               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9983             {
9984               tem = fold_negate_const (arg1, type);
9985               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9986                 return fold_build2 (MINUS_EXPR, type,
9987                                     fold_convert (type, arg0),
9988                                     fold_convert (type, tem));
9989             }
9990
9991           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9992              to __complex__ ( x, y ).  This is not the same for SNaNs or
9993              if signed zeros are involved.  */
9994           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9995               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9996               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9997             {
9998               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9999               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10000               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10001               bool arg0rz = false, arg0iz = false;
10002               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10003                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10004                 {
10005                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10006                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10007                   if (arg0rz && arg1i && real_zerop (arg1i))
10008                     {
10009                       tree rp = arg1r ? arg1r
10010                                   : build1 (REALPART_EXPR, rtype, arg1);
10011                       tree ip = arg0i ? arg0i
10012                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10013                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10014                     }
10015                   else if (arg0iz && arg1r && real_zerop (arg1r))
10016                     {
10017                       tree rp = arg0r ? arg0r
10018                                   : build1 (REALPART_EXPR, rtype, arg0);
10019                       tree ip = arg1i ? arg1i
10020                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10021                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10022                     }
10023                 }
10024             }
10025
10026           if (flag_unsafe_math_optimizations
10027               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10028               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10029               && (tem = distribute_real_division (code, type, arg0, arg1)))
10030             return tem;
10031
10032           /* Convert x+x into x*2.0.  */
10033           if (operand_equal_p (arg0, arg1, 0)
10034               && SCALAR_FLOAT_TYPE_P (type))
10035             return fold_build2 (MULT_EXPR, type, arg0,
10036                                 build_real (type, dconst2));
10037
10038           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10039              We associate floats only if the user has specified
10040              -fassociative-math.  */
10041           if (flag_associative_math
10042               && TREE_CODE (arg1) == PLUS_EXPR
10043               && TREE_CODE (arg0) != MULT_EXPR)
10044             {
10045               tree tree10 = TREE_OPERAND (arg1, 0);
10046               tree tree11 = TREE_OPERAND (arg1, 1);
10047               if (TREE_CODE (tree11) == MULT_EXPR
10048                   && TREE_CODE (tree10) == MULT_EXPR)
10049                 {
10050                   tree tree0;
10051                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10052                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10053                 }
10054             }
10055           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10056              We associate floats only if the user has specified
10057              -fassociative-math.  */
10058           if (flag_associative_math
10059               && TREE_CODE (arg0) == PLUS_EXPR
10060               && TREE_CODE (arg1) != MULT_EXPR)
10061             {
10062               tree tree00 = TREE_OPERAND (arg0, 0);
10063               tree tree01 = TREE_OPERAND (arg0, 1);
10064               if (TREE_CODE (tree01) == MULT_EXPR
10065                   && TREE_CODE (tree00) == MULT_EXPR)
10066                 {
10067                   tree tree0;
10068                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10069                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10070                 }
10071             }
10072         }
10073
10074      bit_rotate:
10075       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10076          is a rotate of A by C1 bits.  */
10077       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10078          is a rotate of A by B bits.  */
10079       {
10080         enum tree_code code0, code1;
10081         tree rtype;
10082         code0 = TREE_CODE (arg0);
10083         code1 = TREE_CODE (arg1);
10084         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10085              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10086             && operand_equal_p (TREE_OPERAND (arg0, 0),
10087                                 TREE_OPERAND (arg1, 0), 0)
10088             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10089                 TYPE_UNSIGNED (rtype))
10090             /* Only create rotates in complete modes.  Other cases are not
10091                expanded properly.  */
10092             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10093           {
10094             tree tree01, tree11;
10095             enum tree_code code01, code11;
10096
10097             tree01 = TREE_OPERAND (arg0, 1);
10098             tree11 = TREE_OPERAND (arg1, 1);
10099             STRIP_NOPS (tree01);
10100             STRIP_NOPS (tree11);
10101             code01 = TREE_CODE (tree01);
10102             code11 = TREE_CODE (tree11);
10103             if (code01 == INTEGER_CST
10104                 && code11 == INTEGER_CST
10105                 && TREE_INT_CST_HIGH (tree01) == 0
10106                 && TREE_INT_CST_HIGH (tree11) == 0
10107                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10108                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10109               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10110                              code0 == LSHIFT_EXPR ? tree01 : tree11);
10111             else if (code11 == MINUS_EXPR)
10112               {
10113                 tree tree110, tree111;
10114                 tree110 = TREE_OPERAND (tree11, 0);
10115                 tree111 = TREE_OPERAND (tree11, 1);
10116                 STRIP_NOPS (tree110);
10117                 STRIP_NOPS (tree111);
10118                 if (TREE_CODE (tree110) == INTEGER_CST
10119                     && 0 == compare_tree_int (tree110,
10120                                               TYPE_PRECISION
10121                                               (TREE_TYPE (TREE_OPERAND
10122                                                           (arg0, 0))))
10123                     && operand_equal_p (tree01, tree111, 0))
10124                   return build2 ((code0 == LSHIFT_EXPR
10125                                   ? LROTATE_EXPR
10126                                   : RROTATE_EXPR),
10127                                  type, TREE_OPERAND (arg0, 0), tree01);
10128               }
10129             else if (code01 == MINUS_EXPR)
10130               {
10131                 tree tree010, tree011;
10132                 tree010 = TREE_OPERAND (tree01, 0);
10133                 tree011 = TREE_OPERAND (tree01, 1);
10134                 STRIP_NOPS (tree010);
10135                 STRIP_NOPS (tree011);
10136                 if (TREE_CODE (tree010) == INTEGER_CST
10137                     && 0 == compare_tree_int (tree010,
10138                                               TYPE_PRECISION
10139                                               (TREE_TYPE (TREE_OPERAND
10140                                                           (arg0, 0))))
10141                     && operand_equal_p (tree11, tree011, 0))
10142                   return build2 ((code0 != LSHIFT_EXPR
10143                                   ? LROTATE_EXPR
10144                                   : RROTATE_EXPR),
10145                                  type, TREE_OPERAND (arg0, 0), tree11);
10146               }
10147           }
10148       }
10149
10150     associate:
10151       /* In most languages, can't associate operations on floats through
10152          parentheses.  Rather than remember where the parentheses were, we
10153          don't associate floats at all, unless the user has specified
10154          -fassociative-math.
10155          And, we need to make sure type is not saturating.  */
10156
10157       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10158           && !TYPE_SATURATING (type))
10159         {
10160           tree var0, con0, lit0, minus_lit0;
10161           tree var1, con1, lit1, minus_lit1;
10162           bool ok = true;
10163
10164           /* Split both trees into variables, constants, and literals.  Then
10165              associate each group together, the constants with literals,
10166              then the result with variables.  This increases the chances of
10167              literals being recombined later and of generating relocatable
10168              expressions for the sum of a constant and literal.  */
10169           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10170           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10171                              code == MINUS_EXPR);
10172
10173           /* With undefined overflow we can only associate constants
10174              with one variable.  */
10175           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10176                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10177               && var0 && var1)
10178             {
10179               tree tmp0 = var0;
10180               tree tmp1 = var1;
10181
10182               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10183                 tmp0 = TREE_OPERAND (tmp0, 0);
10184               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10185                 tmp1 = TREE_OPERAND (tmp1, 0);
10186               /* The only case we can still associate with two variables
10187                  is if they are the same, modulo negation.  */
10188               if (!operand_equal_p (tmp0, tmp1, 0))
10189                 ok = false;
10190             }
10191
10192           /* Only do something if we found more than two objects.  Otherwise,
10193              nothing has changed and we risk infinite recursion.  */
10194           if (ok
10195               && (2 < ((var0 != 0) + (var1 != 0)
10196                        + (con0 != 0) + (con1 != 0)
10197                        + (lit0 != 0) + (lit1 != 0)
10198                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10199             {
10200               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10201               if (code == MINUS_EXPR)
10202                 code = PLUS_EXPR;
10203
10204               var0 = associate_trees (var0, var1, code, type);
10205               con0 = associate_trees (con0, con1, code, type);
10206               lit0 = associate_trees (lit0, lit1, code, type);
10207               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10208
10209               /* Preserve the MINUS_EXPR if the negative part of the literal is
10210                  greater than the positive part.  Otherwise, the multiplicative
10211                  folding code (i.e extract_muldiv) may be fooled in case
10212                  unsigned constants are subtracted, like in the following
10213                  example: ((X*2 + 4) - 8U)/2.  */
10214               if (minus_lit0 && lit0)
10215                 {
10216                   if (TREE_CODE (lit0) == INTEGER_CST
10217                       && TREE_CODE (minus_lit0) == INTEGER_CST
10218                       && tree_int_cst_lt (lit0, minus_lit0))
10219                     {
10220                       minus_lit0 = associate_trees (minus_lit0, lit0,
10221                                                     MINUS_EXPR, type);
10222                       lit0 = 0;
10223                     }
10224                   else
10225                     {
10226                       lit0 = associate_trees (lit0, minus_lit0,
10227                                               MINUS_EXPR, type);
10228                       minus_lit0 = 0;
10229                     }
10230                 }
10231               if (minus_lit0)
10232                 {
10233                   if (con0 == 0)
10234                     return fold_convert (type,
10235                                          associate_trees (var0, minus_lit0,
10236                                                           MINUS_EXPR, type));
10237                   else
10238                     {
10239                       con0 = associate_trees (con0, minus_lit0,
10240                                               MINUS_EXPR, type);
10241                       return fold_convert (type,
10242                                            associate_trees (var0, con0,
10243                                                             PLUS_EXPR, type));
10244                     }
10245                 }
10246
10247               con0 = associate_trees (con0, lit0, code, type);
10248               return fold_convert (type, associate_trees (var0, con0,
10249                                                           code, type));
10250             }
10251         }
10252
10253       return NULL_TREE;
10254
10255     case MINUS_EXPR:
10256       /* Pointer simplifications for subtraction, simple reassociations. */
10257       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10258         {
10259           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10260           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10261               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10262             {
10263               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10264               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10265               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10266               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10267               return fold_build2 (PLUS_EXPR, type,
10268                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10269                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10270             }
10271           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10272           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10273             {
10274               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10275               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10276               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10277               if (tmp)
10278                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10279             }
10280         }
10281       /* A - (-B) -> A + B */
10282       if (TREE_CODE (arg1) == NEGATE_EXPR)
10283         return fold_build2 (PLUS_EXPR, type, op0,
10284                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10285       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10286       if (TREE_CODE (arg0) == NEGATE_EXPR
10287           && (FLOAT_TYPE_P (type)
10288               || INTEGRAL_TYPE_P (type))
10289           && negate_expr_p (arg1)
10290           && reorder_operands_p (arg0, arg1))
10291         return fold_build2 (MINUS_EXPR, type,
10292                             fold_convert (type, negate_expr (arg1)),
10293                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10294       /* Convert -A - 1 to ~A.  */
10295       if (INTEGRAL_TYPE_P (type)
10296           && TREE_CODE (arg0) == NEGATE_EXPR
10297           && integer_onep (arg1)
10298           && !TYPE_OVERFLOW_TRAPS (type))
10299         return fold_build1 (BIT_NOT_EXPR, type,
10300                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10301
10302       /* Convert -1 - A to ~A.  */
10303       if (INTEGRAL_TYPE_P (type)
10304           && integer_all_onesp (arg0))
10305         return fold_build1 (BIT_NOT_EXPR, type, op1);
10306
10307
10308       /* X - (X / CST) * CST is X % CST.  */
10309       if (INTEGRAL_TYPE_P (type)
10310           && TREE_CODE (arg1) == MULT_EXPR
10311           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10312           && operand_equal_p (arg0,
10313                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10314           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10315                               TREE_OPERAND (arg1, 1), 0))
10316         return fold_convert (type,
10317                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10318                                           arg0, TREE_OPERAND (arg1, 1)));
10319
10320       if (! FLOAT_TYPE_P (type))
10321         {
10322           if (integer_zerop (arg0))
10323             return negate_expr (fold_convert (type, arg1));
10324           if (integer_zerop (arg1))
10325             return non_lvalue (fold_convert (type, arg0));
10326
10327           /* Fold A - (A & B) into ~B & A.  */
10328           if (!TREE_SIDE_EFFECTS (arg0)
10329               && TREE_CODE (arg1) == BIT_AND_EXPR)
10330             {
10331               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10332                 {
10333                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10334                   return fold_build2 (BIT_AND_EXPR, type,
10335                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10336                                       fold_convert (type, arg0));
10337                 }
10338               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10339                 {
10340                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10341                   return fold_build2 (BIT_AND_EXPR, type,
10342                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10343                                       fold_convert (type, arg0));
10344                 }
10345             }
10346
10347           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10348              any power of 2 minus 1.  */
10349           if (TREE_CODE (arg0) == BIT_AND_EXPR
10350               && TREE_CODE (arg1) == BIT_AND_EXPR
10351               && operand_equal_p (TREE_OPERAND (arg0, 0),
10352                                   TREE_OPERAND (arg1, 0), 0))
10353             {
10354               tree mask0 = TREE_OPERAND (arg0, 1);
10355               tree mask1 = TREE_OPERAND (arg1, 1);
10356               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10357
10358               if (operand_equal_p (tem, mask1, 0))
10359                 {
10360                   tem = fold_build2 (BIT_XOR_EXPR, type,
10361                                      TREE_OPERAND (arg0, 0), mask1);
10362                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10363                 }
10364             }
10365         }
10366
10367       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10368       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10369         return non_lvalue (fold_convert (type, arg0));
10370
10371       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10372          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10373          (-ARG1 + ARG0) reduces to -ARG1.  */
10374       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10375         return negate_expr (fold_convert (type, arg1));
10376
10377       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10378          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10379          signed zeros are involved.  */
10380       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10381           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10382           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10383         {
10384           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10385           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10386           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10387           bool arg0rz = false, arg0iz = false;
10388           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10389               || (arg0i && (arg0iz = real_zerop (arg0i))))
10390             {
10391               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10392               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10393               if (arg0rz && arg1i && real_zerop (arg1i))
10394                 {
10395                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10396                                          arg1r ? arg1r
10397                                          : build1 (REALPART_EXPR, rtype, arg1));
10398                   tree ip = arg0i ? arg0i
10399                     : build1 (IMAGPART_EXPR, rtype, arg0);
10400                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10401                 }
10402               else if (arg0iz && arg1r && real_zerop (arg1r))
10403                 {
10404                   tree rp = arg0r ? arg0r
10405                     : build1 (REALPART_EXPR, rtype, arg0);
10406                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10407                                          arg1i ? arg1i
10408                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10409                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10410                 }
10411             }
10412         }
10413
10414       /* Fold &x - &x.  This can happen from &x.foo - &x.
10415          This is unsafe for certain floats even in non-IEEE formats.
10416          In IEEE, it is unsafe because it does wrong for NaNs.
10417          Also note that operand_equal_p is always false if an operand
10418          is volatile.  */
10419
10420       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10421           && operand_equal_p (arg0, arg1, 0))
10422         return fold_convert (type, integer_zero_node);
10423
10424       /* A - B -> A + (-B) if B is easily negatable.  */
10425       if (negate_expr_p (arg1)
10426           && ((FLOAT_TYPE_P (type)
10427                /* Avoid this transformation if B is a positive REAL_CST.  */
10428                && (TREE_CODE (arg1) != REAL_CST
10429                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10430               || INTEGRAL_TYPE_P (type)))
10431         return fold_build2 (PLUS_EXPR, type,
10432                             fold_convert (type, arg0),
10433                             fold_convert (type, negate_expr (arg1)));
10434
10435       /* Try folding difference of addresses.  */
10436       {
10437         HOST_WIDE_INT diff;
10438
10439         if ((TREE_CODE (arg0) == ADDR_EXPR
10440              || TREE_CODE (arg1) == ADDR_EXPR)
10441             && ptr_difference_const (arg0, arg1, &diff))
10442           return build_int_cst_type (type, diff);
10443       }
10444
10445       /* Fold &a[i] - &a[j] to i-j.  */
10446       if (TREE_CODE (arg0) == ADDR_EXPR
10447           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10448           && TREE_CODE (arg1) == ADDR_EXPR
10449           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10450         {
10451           tree aref0 = TREE_OPERAND (arg0, 0);
10452           tree aref1 = TREE_OPERAND (arg1, 0);
10453           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10454                                TREE_OPERAND (aref1, 0), 0))
10455             {
10456               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10457               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10458               tree esz = array_ref_element_size (aref0);
10459               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10460               return fold_build2 (MULT_EXPR, type, diff,
10461                                   fold_convert (type, esz));
10462                                   
10463             }
10464         }
10465
10466       if (flag_unsafe_math_optimizations
10467           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10468           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10469           && (tem = distribute_real_division (code, type, arg0, arg1)))
10470         return tem;
10471
10472       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10473          same or one.  Make sure type is not saturating.
10474          fold_plusminus_mult_expr will re-associate.  */
10475       if ((TREE_CODE (arg0) == MULT_EXPR
10476            || TREE_CODE (arg1) == MULT_EXPR)
10477           && !TYPE_SATURATING (type)
10478           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10479         {
10480           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10481           if (tem)
10482             return tem;
10483         }
10484
10485       goto associate;
10486
10487     case MULT_EXPR:
10488       /* (-A) * (-B) -> A * B  */
10489       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10490         return fold_build2 (MULT_EXPR, type,
10491                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10492                             fold_convert (type, negate_expr (arg1)));
10493       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10494         return fold_build2 (MULT_EXPR, type,
10495                             fold_convert (type, negate_expr (arg0)),
10496                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10497
10498       if (! FLOAT_TYPE_P (type))
10499         {
10500           if (integer_zerop (arg1))
10501             return omit_one_operand (type, arg1, arg0);
10502           if (integer_onep (arg1))
10503             return non_lvalue (fold_convert (type, arg0));
10504           /* Transform x * -1 into -x.  Make sure to do the negation
10505              on the original operand with conversions not stripped
10506              because we can only strip non-sign-changing conversions.  */
10507           if (integer_all_onesp (arg1))
10508             return fold_convert (type, negate_expr (op0));
10509           /* Transform x * -C into -x * C if x is easily negatable.  */
10510           if (TREE_CODE (arg1) == INTEGER_CST
10511               && tree_int_cst_sgn (arg1) == -1
10512               && negate_expr_p (arg0)
10513               && (tem = negate_expr (arg1)) != arg1
10514               && !TREE_OVERFLOW (tem))
10515             return fold_build2 (MULT_EXPR, type,
10516                                 fold_convert (type, negate_expr (arg0)), tem);
10517
10518           /* (a * (1 << b)) is (a << b)  */
10519           if (TREE_CODE (arg1) == LSHIFT_EXPR
10520               && integer_onep (TREE_OPERAND (arg1, 0)))
10521             return fold_build2 (LSHIFT_EXPR, type, op0,
10522                                 TREE_OPERAND (arg1, 1));
10523           if (TREE_CODE (arg0) == LSHIFT_EXPR
10524               && integer_onep (TREE_OPERAND (arg0, 0)))
10525             return fold_build2 (LSHIFT_EXPR, type, op1,
10526                                 TREE_OPERAND (arg0, 1));
10527
10528           /* (A + A) * C -> A * 2 * C  */
10529           if (TREE_CODE (arg0) == PLUS_EXPR
10530               && TREE_CODE (arg1) == INTEGER_CST
10531               && operand_equal_p (TREE_OPERAND (arg0, 0),
10532                                   TREE_OPERAND (arg0, 1), 0))
10533             return fold_build2 (MULT_EXPR, type,
10534                                 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10535                                                   TREE_OPERAND (arg0, 1)),
10536                                 fold_build2 (MULT_EXPR, type,
10537                                              build_int_cst (type, 2) , arg1));
10538
10539           strict_overflow_p = false;
10540           if (TREE_CODE (arg1) == INTEGER_CST
10541               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10542                                              &strict_overflow_p)))
10543             {
10544               if (strict_overflow_p)
10545                 fold_overflow_warning (("assuming signed overflow does not "
10546                                         "occur when simplifying "
10547                                         "multiplication"),
10548                                        WARN_STRICT_OVERFLOW_MISC);
10549               return fold_convert (type, tem);
10550             }
10551
10552           /* Optimize z * conj(z) for integer complex numbers.  */
10553           if (TREE_CODE (arg0) == CONJ_EXPR
10554               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10555             return fold_mult_zconjz (type, arg1);
10556           if (TREE_CODE (arg1) == CONJ_EXPR
10557               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10558             return fold_mult_zconjz (type, arg0);
10559         }
10560       else
10561         {
10562           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10563              when x is NaN, since x * 0 is also NaN.  Nor are they the
10564              same in modes with signed zeros, since multiplying a
10565              negative value by 0 gives -0, not +0.  */
10566           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10567               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10568               && real_zerop (arg1))
10569             return omit_one_operand (type, arg1, arg0);
10570           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10571           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10572               && real_onep (arg1))
10573             return non_lvalue (fold_convert (type, arg0));
10574
10575           /* Transform x * -1.0 into -x.  */
10576           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10577               && real_minus_onep (arg1))
10578             return fold_convert (type, negate_expr (arg0));
10579
10580           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10581              the result for floating point types due to rounding so it is applied
10582              only if -fassociative-math was specify.  */
10583           if (flag_associative_math
10584               && TREE_CODE (arg0) == RDIV_EXPR
10585               && TREE_CODE (arg1) == REAL_CST
10586               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10587             {
10588               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10589                                       arg1, 0);
10590               if (tem)
10591                 return fold_build2 (RDIV_EXPR, type, tem,
10592                                     TREE_OPERAND (arg0, 1));
10593             }
10594
10595           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10596           if (operand_equal_p (arg0, arg1, 0))
10597             {
10598               tree tem = fold_strip_sign_ops (arg0);
10599               if (tem != NULL_TREE)
10600                 {
10601                   tem = fold_convert (type, tem);
10602                   return fold_build2 (MULT_EXPR, type, tem, tem);
10603                 }
10604             }
10605
10606           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10607              This is not the same for NaNs or if signed zeros are
10608              involved.  */
10609           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10610               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10611               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10612               && TREE_CODE (arg1) == COMPLEX_CST
10613               && real_zerop (TREE_REALPART (arg1)))
10614             {
10615               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10616               if (real_onep (TREE_IMAGPART (arg1)))
10617                 return fold_build2 (COMPLEX_EXPR, type,
10618                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10619                                                               rtype, arg0)),
10620                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10621               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10622                 return fold_build2 (COMPLEX_EXPR, type,
10623                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10624                                     negate_expr (fold_build1 (REALPART_EXPR,
10625                                                               rtype, arg0)));
10626             }
10627
10628           /* Optimize z * conj(z) for floating point complex numbers.
10629              Guarded by flag_unsafe_math_optimizations as non-finite
10630              imaginary components don't produce scalar results.  */
10631           if (flag_unsafe_math_optimizations
10632               && TREE_CODE (arg0) == CONJ_EXPR
10633               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10634             return fold_mult_zconjz (type, arg1);
10635           if (flag_unsafe_math_optimizations
10636               && TREE_CODE (arg1) == CONJ_EXPR
10637               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10638             return fold_mult_zconjz (type, arg0);
10639
10640           if (flag_unsafe_math_optimizations)
10641             {
10642               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10643               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10644
10645               /* Optimizations of root(...)*root(...).  */
10646               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10647                 {
10648                   tree rootfn, arg;
10649                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10650                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10651
10652                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10653                   if (BUILTIN_SQRT_P (fcode0)
10654                       && operand_equal_p (arg00, arg10, 0)
10655                       && ! HONOR_SNANS (TYPE_MODE (type)))
10656                     return arg00;
10657
10658                   /* Optimize root(x)*root(y) as root(x*y).  */
10659                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10660                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10661                   return build_call_expr (rootfn, 1, arg);
10662                 }
10663
10664               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10665               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10666                 {
10667                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10668                   tree arg = fold_build2 (PLUS_EXPR, type,
10669                                           CALL_EXPR_ARG (arg0, 0),
10670                                           CALL_EXPR_ARG (arg1, 0));
10671                   return build_call_expr (expfn, 1, arg);
10672                 }
10673
10674               /* Optimizations of pow(...)*pow(...).  */
10675               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10676                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10677                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10678                 {
10679                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10680                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10681                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10682                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10683
10684                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10685                   if (operand_equal_p (arg01, arg11, 0))
10686                     {
10687                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10688                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10689                       return build_call_expr (powfn, 2, arg, arg01);
10690                     }
10691
10692                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10693                   if (operand_equal_p (arg00, arg10, 0))
10694                     {
10695                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10696                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10697                       return build_call_expr (powfn, 2, arg00, arg);
10698                     }
10699                 }
10700
10701               /* Optimize tan(x)*cos(x) as sin(x).  */
10702               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10703                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10704                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10705                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10706                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10707                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10708                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10709                                       CALL_EXPR_ARG (arg1, 0), 0))
10710                 {
10711                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10712
10713                   if (sinfn != NULL_TREE)
10714                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10715                 }
10716
10717               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10718               if (fcode1 == BUILT_IN_POW
10719                   || fcode1 == BUILT_IN_POWF
10720                   || fcode1 == BUILT_IN_POWL)
10721                 {
10722                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10723                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10724                   if (TREE_CODE (arg11) == REAL_CST
10725                       && !TREE_OVERFLOW (arg11)
10726                       && operand_equal_p (arg0, arg10, 0))
10727                     {
10728                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10729                       REAL_VALUE_TYPE c;
10730                       tree arg;
10731
10732                       c = TREE_REAL_CST (arg11);
10733                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10734                       arg = build_real (type, c);
10735                       return build_call_expr (powfn, 2, arg0, arg);
10736                     }
10737                 }
10738
10739               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10740               if (fcode0 == BUILT_IN_POW
10741                   || fcode0 == BUILT_IN_POWF
10742                   || fcode0 == BUILT_IN_POWL)
10743                 {
10744                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10745                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10746                   if (TREE_CODE (arg01) == REAL_CST
10747                       && !TREE_OVERFLOW (arg01)
10748                       && operand_equal_p (arg1, arg00, 0))
10749                     {
10750                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10751                       REAL_VALUE_TYPE c;
10752                       tree arg;
10753
10754                       c = TREE_REAL_CST (arg01);
10755                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10756                       arg = build_real (type, c);
10757                       return build_call_expr (powfn, 2, arg1, arg);
10758                     }
10759                 }
10760
10761               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10762               if (optimize_function_for_speed_p (cfun)
10763                   && operand_equal_p (arg0, arg1, 0))
10764                 {
10765                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10766
10767                   if (powfn)
10768                     {
10769                       tree arg = build_real (type, dconst2);
10770                       return build_call_expr (powfn, 2, arg0, arg);
10771                     }
10772                 }
10773             }
10774         }
10775       goto associate;
10776
10777     case BIT_IOR_EXPR:
10778     bit_ior:
10779       if (integer_all_onesp (arg1))
10780         return omit_one_operand (type, arg1, arg0);
10781       if (integer_zerop (arg1))
10782         return non_lvalue (fold_convert (type, arg0));
10783       if (operand_equal_p (arg0, arg1, 0))
10784         return non_lvalue (fold_convert (type, arg0));
10785
10786       /* ~X | X is -1.  */
10787       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10788           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10789         {
10790           t1 = fold_convert (type, integer_zero_node);
10791           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10792           return omit_one_operand (type, t1, arg1);
10793         }
10794
10795       /* X | ~X is -1.  */
10796       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10797           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10798         {
10799           t1 = fold_convert (type, integer_zero_node);
10800           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10801           return omit_one_operand (type, t1, arg0);
10802         }
10803
10804       /* Canonicalize (X & C1) | C2.  */
10805       if (TREE_CODE (arg0) == BIT_AND_EXPR
10806           && TREE_CODE (arg1) == INTEGER_CST
10807           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10808         {
10809           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10810           int width = TYPE_PRECISION (type), w;
10811           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10812           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10813           hi2 = TREE_INT_CST_HIGH (arg1);
10814           lo2 = TREE_INT_CST_LOW (arg1);
10815
10816           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10817           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10818             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10819
10820           if (width > HOST_BITS_PER_WIDE_INT)
10821             {
10822               mhi = (unsigned HOST_WIDE_INT) -1 
10823                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10824               mlo = -1;
10825             }
10826           else
10827             {
10828               mhi = 0;
10829               mlo = (unsigned HOST_WIDE_INT) -1
10830                     >> (HOST_BITS_PER_WIDE_INT - width);
10831             }
10832
10833           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10834           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10835             return fold_build2 (BIT_IOR_EXPR, type,
10836                                 TREE_OPERAND (arg0, 0), arg1);
10837
10838           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10839              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10840              mode which allows further optimizations.  */
10841           hi1 &= mhi;
10842           lo1 &= mlo;
10843           hi2 &= mhi;
10844           lo2 &= mlo;
10845           hi3 = hi1 & ~hi2;
10846           lo3 = lo1 & ~lo2;
10847           for (w = BITS_PER_UNIT;
10848                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10849                w <<= 1)
10850             {
10851               unsigned HOST_WIDE_INT mask
10852                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10853               if (((lo1 | lo2) & mask) == mask
10854                   && (lo1 & ~mask) == 0 && hi1 == 0)
10855                 {
10856                   hi3 = 0;
10857                   lo3 = mask;
10858                   break;
10859                 }
10860             }
10861           if (hi3 != hi1 || lo3 != lo1)
10862             return fold_build2 (BIT_IOR_EXPR, type,
10863                                 fold_build2 (BIT_AND_EXPR, type,
10864                                              TREE_OPERAND (arg0, 0),
10865                                              build_int_cst_wide (type,
10866                                                                  lo3, hi3)),
10867                                 arg1);
10868         }
10869
10870       /* (X & Y) | Y is (X, Y).  */
10871       if (TREE_CODE (arg0) == BIT_AND_EXPR
10872           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10873         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10874       /* (X & Y) | X is (Y, X).  */
10875       if (TREE_CODE (arg0) == BIT_AND_EXPR
10876           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10877           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10878         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10879       /* X | (X & Y) is (Y, X).  */
10880       if (TREE_CODE (arg1) == BIT_AND_EXPR
10881           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10882           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10883         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10884       /* X | (Y & X) is (Y, X).  */
10885       if (TREE_CODE (arg1) == BIT_AND_EXPR
10886           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10887           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10888         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10889
10890       t1 = distribute_bit_expr (code, type, arg0, arg1);
10891       if (t1 != NULL_TREE)
10892         return t1;
10893
10894       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10895
10896          This results in more efficient code for machines without a NAND
10897          instruction.  Combine will canonicalize to the first form
10898          which will allow use of NAND instructions provided by the
10899          backend if they exist.  */
10900       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10901           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10902         {
10903           return fold_build1 (BIT_NOT_EXPR, type,
10904                               build2 (BIT_AND_EXPR, type,
10905                                       fold_convert (type,
10906                                                     TREE_OPERAND (arg0, 0)),
10907                                       fold_convert (type,
10908                                                     TREE_OPERAND (arg1, 0))));
10909         }
10910
10911       /* See if this can be simplified into a rotate first.  If that
10912          is unsuccessful continue in the association code.  */
10913       goto bit_rotate;
10914
10915     case BIT_XOR_EXPR:
10916       if (integer_zerop (arg1))
10917         return non_lvalue (fold_convert (type, arg0));
10918       if (integer_all_onesp (arg1))
10919         return fold_build1 (BIT_NOT_EXPR, type, op0);
10920       if (operand_equal_p (arg0, arg1, 0))
10921         return omit_one_operand (type, integer_zero_node, arg0);
10922
10923       /* ~X ^ X is -1.  */
10924       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10925           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10926         {
10927           t1 = fold_convert (type, integer_zero_node);
10928           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10929           return omit_one_operand (type, t1, arg1);
10930         }
10931
10932       /* X ^ ~X is -1.  */
10933       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10934           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10935         {
10936           t1 = fold_convert (type, integer_zero_node);
10937           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10938           return omit_one_operand (type, t1, arg0);
10939         }
10940
10941       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10942          with a constant, and the two constants have no bits in common,
10943          we should treat this as a BIT_IOR_EXPR since this may produce more
10944          simplifications.  */
10945       if (TREE_CODE (arg0) == BIT_AND_EXPR
10946           && TREE_CODE (arg1) == BIT_AND_EXPR
10947           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10948           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10949           && integer_zerop (const_binop (BIT_AND_EXPR,
10950                                          TREE_OPERAND (arg0, 1),
10951                                          TREE_OPERAND (arg1, 1), 0)))
10952         {
10953           code = BIT_IOR_EXPR;
10954           goto bit_ior;
10955         }
10956
10957       /* (X | Y) ^ X -> Y & ~ X*/
10958       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10959           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10960         {
10961           tree t2 = TREE_OPERAND (arg0, 1);
10962           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10963                             arg1);
10964           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10965                             fold_convert (type, t1));
10966           return t1;
10967         }
10968
10969       /* (Y | X) ^ X -> Y & ~ X*/
10970       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10971           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10972         {
10973           tree t2 = TREE_OPERAND (arg0, 0);
10974           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10975                             arg1);
10976           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10977                             fold_convert (type, t1));
10978           return t1;
10979         }
10980
10981       /* X ^ (X | Y) -> Y & ~ X*/
10982       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10983           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10984         {
10985           tree t2 = TREE_OPERAND (arg1, 1);
10986           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10987                             arg0);
10988           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10989                             fold_convert (type, t1));
10990           return t1;
10991         }
10992
10993       /* X ^ (Y | X) -> Y & ~ X*/
10994       if (TREE_CODE (arg1) == BIT_IOR_EXPR
10995           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10996         {
10997           tree t2 = TREE_OPERAND (arg1, 0);
10998           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10999                             arg0);
11000           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11001                             fold_convert (type, t1));
11002           return t1;
11003         }
11004         
11005       /* Convert ~X ^ ~Y to X ^ Y.  */
11006       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11007           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11008         return fold_build2 (code, type,
11009                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11010                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11011
11012       /* Convert ~X ^ C to X ^ ~C.  */
11013       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11014           && TREE_CODE (arg1) == INTEGER_CST)
11015         return fold_build2 (code, type,
11016                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11017                             fold_build1 (BIT_NOT_EXPR, type, arg1));
11018
11019       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11020       if (TREE_CODE (arg0) == BIT_AND_EXPR
11021           && integer_onep (TREE_OPERAND (arg0, 1))
11022           && integer_onep (arg1))
11023         return fold_build2 (EQ_EXPR, type, arg0,
11024                             build_int_cst (TREE_TYPE (arg0), 0));
11025
11026       /* Fold (X & Y) ^ Y as ~X & Y.  */
11027       if (TREE_CODE (arg0) == BIT_AND_EXPR
11028           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11029         {
11030           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11031           return fold_build2 (BIT_AND_EXPR, type, 
11032                               fold_build1 (BIT_NOT_EXPR, type, tem),
11033                               fold_convert (type, arg1));
11034         }
11035       /* Fold (X & Y) ^ X as ~Y & X.  */
11036       if (TREE_CODE (arg0) == BIT_AND_EXPR
11037           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11038           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11039         {
11040           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11041           return fold_build2 (BIT_AND_EXPR, type,
11042                               fold_build1 (BIT_NOT_EXPR, type, tem),
11043                               fold_convert (type, arg1));
11044         }
11045       /* Fold X ^ (X & Y) as X & ~Y.  */
11046       if (TREE_CODE (arg1) == BIT_AND_EXPR
11047           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11048         {
11049           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11050           return fold_build2 (BIT_AND_EXPR, type,
11051                               fold_convert (type, arg0),
11052                               fold_build1 (BIT_NOT_EXPR, type, tem));
11053         }
11054       /* Fold X ^ (Y & X) as ~Y & X.  */
11055       if (TREE_CODE (arg1) == BIT_AND_EXPR
11056           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11057           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11058         {
11059           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11060           return fold_build2 (BIT_AND_EXPR, type,
11061                               fold_build1 (BIT_NOT_EXPR, type, tem),
11062                               fold_convert (type, arg0));
11063         }
11064
11065       /* See if this can be simplified into a rotate first.  If that
11066          is unsuccessful continue in the association code.  */
11067       goto bit_rotate;
11068
11069     case BIT_AND_EXPR:
11070       if (integer_all_onesp (arg1))
11071         return non_lvalue (fold_convert (type, arg0));
11072       if (integer_zerop (arg1))
11073         return omit_one_operand (type, arg1, arg0);
11074       if (operand_equal_p (arg0, arg1, 0))
11075         return non_lvalue (fold_convert (type, arg0));
11076
11077       /* ~X & X is always zero.  */
11078       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11079           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11080         return omit_one_operand (type, integer_zero_node, arg1);
11081
11082       /* X & ~X is always zero.  */
11083       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11084           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11085         return omit_one_operand (type, integer_zero_node, arg0);
11086
11087       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11088       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11089           && TREE_CODE (arg1) == INTEGER_CST
11090           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11091         {
11092           tree tmp1 = fold_convert (type, arg1);
11093           tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11094           tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11095           tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11096           tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11097           return fold_convert (type,
11098                                fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11099         }
11100
11101       /* (X | Y) & Y is (X, Y).  */
11102       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11103           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11104         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11105       /* (X | Y) & X is (Y, X).  */
11106       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11107           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11108           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11109         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11110       /* X & (X | Y) is (Y, X).  */
11111       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11112           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11113           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11114         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11115       /* X & (Y | X) is (Y, X).  */
11116       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11117           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11118           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11119         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11120
11121       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11122       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11123           && integer_onep (TREE_OPERAND (arg0, 1))
11124           && integer_onep (arg1))
11125         {
11126           tem = TREE_OPERAND (arg0, 0);
11127           return fold_build2 (EQ_EXPR, type,
11128                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11129                                            build_int_cst (TREE_TYPE (tem), 1)),
11130                               build_int_cst (TREE_TYPE (tem), 0));
11131         }
11132       /* Fold ~X & 1 as (X & 1) == 0.  */
11133       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11134           && integer_onep (arg1))
11135         {
11136           tem = TREE_OPERAND (arg0, 0);
11137           return fold_build2 (EQ_EXPR, type,
11138                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11139                                            build_int_cst (TREE_TYPE (tem), 1)),
11140                               build_int_cst (TREE_TYPE (tem), 0));
11141         }
11142
11143       /* Fold (X ^ Y) & Y as ~X & Y.  */
11144       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11145           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11146         {
11147           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11148           return fold_build2 (BIT_AND_EXPR, type, 
11149                               fold_build1 (BIT_NOT_EXPR, type, tem),
11150                               fold_convert (type, arg1));
11151         }
11152       /* Fold (X ^ Y) & X as ~Y & X.  */
11153       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11154           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11155           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11156         {
11157           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11158           return fold_build2 (BIT_AND_EXPR, type,
11159                               fold_build1 (BIT_NOT_EXPR, type, tem),
11160                               fold_convert (type, arg1));
11161         }
11162       /* Fold X & (X ^ Y) as X & ~Y.  */
11163       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11164           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11165         {
11166           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11167           return fold_build2 (BIT_AND_EXPR, type,
11168                               fold_convert (type, arg0),
11169                               fold_build1 (BIT_NOT_EXPR, type, tem));
11170         }
11171       /* Fold X & (Y ^ X) as ~Y & X.  */
11172       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11173           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11174           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11175         {
11176           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11177           return fold_build2 (BIT_AND_EXPR, type,
11178                               fold_build1 (BIT_NOT_EXPR, type, tem),
11179                               fold_convert (type, arg0));
11180         }
11181
11182       t1 = distribute_bit_expr (code, type, arg0, arg1);
11183       if (t1 != NULL_TREE)
11184         return t1;
11185       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11186       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11187           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11188         {
11189           unsigned int prec
11190             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11191
11192           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11193               && (~TREE_INT_CST_LOW (arg1)
11194                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11195             return fold_convert (type, TREE_OPERAND (arg0, 0));
11196         }
11197
11198       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11199
11200          This results in more efficient code for machines without a NOR
11201          instruction.  Combine will canonicalize to the first form
11202          which will allow use of NOR instructions provided by the
11203          backend if they exist.  */
11204       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11205           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11206         {
11207           return fold_build1 (BIT_NOT_EXPR, type,
11208                               build2 (BIT_IOR_EXPR, type,
11209                                       fold_convert (type,
11210                                                     TREE_OPERAND (arg0, 0)),
11211                                       fold_convert (type,
11212                                                     TREE_OPERAND (arg1, 0))));
11213         }
11214
11215       /* If arg0 is derived from the address of an object or function, we may
11216          be able to fold this expression using the object or function's
11217          alignment.  */
11218       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11219         {
11220           unsigned HOST_WIDE_INT modulus, residue;
11221           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11222
11223           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11224                                                      integer_onep (arg1));
11225
11226           /* This works because modulus is a power of 2.  If this weren't the
11227              case, we'd have to replace it by its greatest power-of-2
11228              divisor: modulus & -modulus.  */
11229           if (low < modulus)
11230             return build_int_cst (type, residue & low);
11231         }
11232
11233       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11234               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11235          if the new mask might be further optimized.  */
11236       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11237            || TREE_CODE (arg0) == RSHIFT_EXPR)
11238           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11239           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11240           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11241              < TYPE_PRECISION (TREE_TYPE (arg0))
11242           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11243           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11244         {
11245           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11246           unsigned HOST_WIDE_INT mask
11247             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11248           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11249           tree shift_type = TREE_TYPE (arg0);
11250
11251           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11252             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11253           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11254                    && TYPE_PRECISION (TREE_TYPE (arg0))
11255                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11256             {
11257               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11258               tree arg00 = TREE_OPERAND (arg0, 0);
11259               /* See if more bits can be proven as zero because of
11260                  zero extension.  */
11261               if (TREE_CODE (arg00) == NOP_EXPR
11262                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11263                 {
11264                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11265                   if (TYPE_PRECISION (inner_type)
11266                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11267                       && TYPE_PRECISION (inner_type) < prec)
11268                     {
11269                       prec = TYPE_PRECISION (inner_type);
11270                       /* See if we can shorten the right shift.  */
11271                       if (shiftc < prec)
11272                         shift_type = inner_type;
11273                     }
11274                 }
11275               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11276               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11277               zerobits <<= prec - shiftc;
11278               /* For arithmetic shift if sign bit could be set, zerobits
11279                  can contain actually sign bits, so no transformation is
11280                  possible, unless MASK masks them all away.  In that
11281                  case the shift needs to be converted into logical shift.  */
11282               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11283                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11284                 {
11285                   if ((mask & zerobits) == 0)
11286                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11287                   else
11288                     zerobits = 0;
11289                 }
11290             }
11291
11292           /* ((X << 16) & 0xff00) is (X, 0).  */
11293           if ((mask & zerobits) == mask)
11294             return omit_one_operand (type, build_int_cst (type, 0), arg0);
11295
11296           newmask = mask | zerobits;
11297           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11298             {
11299               unsigned int prec;
11300
11301               /* Only do the transformation if NEWMASK is some integer
11302                  mode's mask.  */
11303               for (prec = BITS_PER_UNIT;
11304                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11305                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11306                   break;
11307               if (prec < HOST_BITS_PER_WIDE_INT
11308                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11309                 {
11310                   if (shift_type != TREE_TYPE (arg0))
11311                     {
11312                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
11313                                          fold_convert (shift_type,
11314                                                        TREE_OPERAND (arg0, 0)),
11315                                          TREE_OPERAND (arg0, 1));
11316                       tem = fold_convert (type, tem);
11317                     }
11318                   else
11319                     tem = op0;
11320                   return fold_build2 (BIT_AND_EXPR, type, tem,
11321                                       build_int_cst_type (TREE_TYPE (op1),
11322                                                           newmask));
11323                 }
11324             }
11325         }
11326
11327       goto associate;
11328
11329     case RDIV_EXPR:
11330       /* Don't touch a floating-point divide by zero unless the mode
11331          of the constant can represent infinity.  */
11332       if (TREE_CODE (arg1) == REAL_CST
11333           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11334           && real_zerop (arg1))
11335         return NULL_TREE;
11336
11337       /* Optimize A / A to 1.0 if we don't care about
11338          NaNs or Infinities.  Skip the transformation
11339          for non-real operands.  */
11340       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11341           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11342           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11343           && operand_equal_p (arg0, arg1, 0))
11344         {
11345           tree r = build_real (TREE_TYPE (arg0), dconst1);
11346
11347           return omit_two_operands (type, r, arg0, arg1);
11348         }
11349
11350       /* The complex version of the above A / A optimization.  */
11351       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11352           && operand_equal_p (arg0, arg1, 0))
11353         {
11354           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11355           if (! HONOR_NANS (TYPE_MODE (elem_type))
11356               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11357             {
11358               tree r = build_real (elem_type, dconst1);
11359               /* omit_two_operands will call fold_convert for us.  */
11360               return omit_two_operands (type, r, arg0, arg1);
11361             }
11362         }
11363
11364       /* (-A) / (-B) -> A / B  */
11365       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11366         return fold_build2 (RDIV_EXPR, type,
11367                             TREE_OPERAND (arg0, 0),
11368                             negate_expr (arg1));
11369       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11370         return fold_build2 (RDIV_EXPR, type,
11371                             negate_expr (arg0),
11372                             TREE_OPERAND (arg1, 0));
11373
11374       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11375       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11376           && real_onep (arg1))
11377         return non_lvalue (fold_convert (type, arg0));
11378
11379       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11380       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11381           && real_minus_onep (arg1))
11382         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11383
11384       /* If ARG1 is a constant, we can convert this to a multiply by the
11385          reciprocal.  This does not have the same rounding properties,
11386          so only do this if -freciprocal-math.  We can actually
11387          always safely do it if ARG1 is a power of two, but it's hard to
11388          tell if it is or not in a portable manner.  */
11389       if (TREE_CODE (arg1) == REAL_CST)
11390         {
11391           if (flag_reciprocal_math
11392               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11393                                           arg1, 0)))
11394             return fold_build2 (MULT_EXPR, type, arg0, tem);
11395           /* Find the reciprocal if optimizing and the result is exact.  */
11396           if (optimize)
11397             {
11398               REAL_VALUE_TYPE r;
11399               r = TREE_REAL_CST (arg1);
11400               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11401                 {
11402                   tem = build_real (type, r);
11403                   return fold_build2 (MULT_EXPR, type,
11404                                       fold_convert (type, arg0), tem);
11405                 }
11406             }
11407         }
11408       /* Convert A/B/C to A/(B*C).  */ 
11409       if (flag_reciprocal_math
11410           && TREE_CODE (arg0) == RDIV_EXPR)
11411         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11412                             fold_build2 (MULT_EXPR, type,
11413                                          TREE_OPERAND (arg0, 1), arg1));
11414
11415       /* Convert A/(B/C) to (A/B)*C.  */
11416       if (flag_reciprocal_math
11417           && TREE_CODE (arg1) == RDIV_EXPR)
11418         return fold_build2 (MULT_EXPR, type,
11419                             fold_build2 (RDIV_EXPR, type, arg0,
11420                                          TREE_OPERAND (arg1, 0)),
11421                             TREE_OPERAND (arg1, 1));
11422
11423       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11424       if (flag_reciprocal_math
11425           && TREE_CODE (arg1) == MULT_EXPR
11426           && TREE_CODE (arg0) == REAL_CST
11427           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11428         {
11429           tree tem = const_binop (RDIV_EXPR, arg0,
11430                                   TREE_OPERAND (arg1, 1), 0);
11431           if (tem)
11432             return fold_build2 (RDIV_EXPR, type, tem,
11433                                 TREE_OPERAND (arg1, 0));
11434         }
11435
11436       if (flag_unsafe_math_optimizations)
11437         {
11438           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11439           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11440
11441           /* Optimize sin(x)/cos(x) as tan(x).  */
11442           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11443                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11444                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11445               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11446                                   CALL_EXPR_ARG (arg1, 0), 0))
11447             {
11448               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11449
11450               if (tanfn != NULL_TREE)
11451                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11452             }
11453
11454           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11455           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11456                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11457                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11458               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11459                                   CALL_EXPR_ARG (arg1, 0), 0))
11460             {
11461               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11462
11463               if (tanfn != NULL_TREE)
11464                 {
11465                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11466                   return fold_build2 (RDIV_EXPR, type,
11467                                       build_real (type, dconst1), tmp);
11468                 }
11469             }
11470
11471           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11472              NaNs or Infinities.  */
11473           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11474                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11475                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11476             {
11477               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11478               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11479
11480               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11481                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11482                   && operand_equal_p (arg00, arg01, 0))
11483                 {
11484                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11485
11486                   if (cosfn != NULL_TREE)
11487                     return build_call_expr (cosfn, 1, arg00);
11488                 }
11489             }
11490
11491           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11492              NaNs or Infinities.  */
11493           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11494                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11495                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11496             {
11497               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11498               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11499
11500               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11501                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11502                   && operand_equal_p (arg00, arg01, 0))
11503                 {
11504                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11505
11506                   if (cosfn != NULL_TREE)
11507                     {
11508                       tree tmp = build_call_expr (cosfn, 1, arg00);
11509                       return fold_build2 (RDIV_EXPR, type,
11510                                           build_real (type, dconst1),
11511                                           tmp);
11512                     }
11513                 }
11514             }
11515
11516           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11517           if (fcode0 == BUILT_IN_POW
11518               || fcode0 == BUILT_IN_POWF
11519               || fcode0 == BUILT_IN_POWL)
11520             {
11521               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11522               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11523               if (TREE_CODE (arg01) == REAL_CST
11524                   && !TREE_OVERFLOW (arg01)
11525                   && operand_equal_p (arg1, arg00, 0))
11526                 {
11527                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11528                   REAL_VALUE_TYPE c;
11529                   tree arg;
11530
11531                   c = TREE_REAL_CST (arg01);
11532                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11533                   arg = build_real (type, c);
11534                   return build_call_expr (powfn, 2, arg1, arg);
11535                 }
11536             }
11537
11538           /* Optimize a/root(b/c) into a*root(c/b).  */
11539           if (BUILTIN_ROOT_P (fcode1))
11540             {
11541               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11542
11543               if (TREE_CODE (rootarg) == RDIV_EXPR)
11544                 {
11545                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11546                   tree b = TREE_OPERAND (rootarg, 0);
11547                   tree c = TREE_OPERAND (rootarg, 1);
11548
11549                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11550
11551                   tmp = build_call_expr (rootfn, 1, tmp);
11552                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11553                 }
11554             }
11555
11556           /* Optimize x/expN(y) into x*expN(-y).  */
11557           if (BUILTIN_EXPONENT_P (fcode1))
11558             {
11559               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11560               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11561               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11562               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11563             }
11564
11565           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11566           if (fcode1 == BUILT_IN_POW
11567               || fcode1 == BUILT_IN_POWF
11568               || fcode1 == BUILT_IN_POWL)
11569             {
11570               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11571               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11572               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11573               tree neg11 = fold_convert (type, negate_expr (arg11));
11574               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11575               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11576             }
11577         }
11578       return NULL_TREE;
11579
11580     case TRUNC_DIV_EXPR:
11581     case FLOOR_DIV_EXPR:
11582       /* Simplify A / (B << N) where A and B are positive and B is
11583          a power of 2, to A >> (N + log2(B)).  */
11584       strict_overflow_p = false;
11585       if (TREE_CODE (arg1) == LSHIFT_EXPR
11586           && (TYPE_UNSIGNED (type)
11587               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11588         {
11589           tree sval = TREE_OPERAND (arg1, 0);
11590           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11591             {
11592               tree sh_cnt = TREE_OPERAND (arg1, 1);
11593               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11594
11595               if (strict_overflow_p)
11596                 fold_overflow_warning (("assuming signed overflow does not "
11597                                         "occur when simplifying A / (B << N)"),
11598                                        WARN_STRICT_OVERFLOW_MISC);
11599
11600               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11601                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11602               return fold_build2 (RSHIFT_EXPR, type,
11603                                   fold_convert (type, arg0), sh_cnt);
11604             }
11605         }
11606
11607       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11608          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11609       if (INTEGRAL_TYPE_P (type)
11610           && TYPE_UNSIGNED (type)
11611           && code == FLOOR_DIV_EXPR)
11612         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11613
11614       /* Fall thru */
11615
11616     case ROUND_DIV_EXPR:
11617     case CEIL_DIV_EXPR:
11618     case EXACT_DIV_EXPR:
11619       if (integer_onep (arg1))
11620         return non_lvalue (fold_convert (type, arg0));
11621       if (integer_zerop (arg1))
11622         return NULL_TREE;
11623       /* X / -1 is -X.  */
11624       if (!TYPE_UNSIGNED (type)
11625           && TREE_CODE (arg1) == INTEGER_CST
11626           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11627           && TREE_INT_CST_HIGH (arg1) == -1)
11628         return fold_convert (type, negate_expr (arg0));
11629
11630       /* Convert -A / -B to A / B when the type is signed and overflow is
11631          undefined.  */
11632       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11633           && TREE_CODE (arg0) == NEGATE_EXPR
11634           && negate_expr_p (arg1))
11635         {
11636           if (INTEGRAL_TYPE_P (type))
11637             fold_overflow_warning (("assuming signed overflow does not occur "
11638                                     "when distributing negation across "
11639                                     "division"),
11640                                    WARN_STRICT_OVERFLOW_MISC);
11641           return fold_build2 (code, type,
11642                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11643                               fold_convert (type, negate_expr (arg1)));
11644         }
11645       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11646           && TREE_CODE (arg1) == NEGATE_EXPR
11647           && negate_expr_p (arg0))
11648         {
11649           if (INTEGRAL_TYPE_P (type))
11650             fold_overflow_warning (("assuming signed overflow does not occur "
11651                                     "when distributing negation across "
11652                                     "division"),
11653                                    WARN_STRICT_OVERFLOW_MISC);
11654           return fold_build2 (code, type,
11655                               fold_convert (type, negate_expr (arg0)),
11656                               fold_convert (type, TREE_OPERAND (arg1, 0)));
11657         }
11658
11659       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11660          operation, EXACT_DIV_EXPR.
11661
11662          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11663          At one time others generated faster code, it's not clear if they do
11664          after the last round to changes to the DIV code in expmed.c.  */
11665       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11666           && multiple_of_p (type, arg0, arg1))
11667         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11668
11669       strict_overflow_p = false;
11670       if (TREE_CODE (arg1) == INTEGER_CST
11671           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11672                                          &strict_overflow_p)))
11673         {
11674           if (strict_overflow_p)
11675             fold_overflow_warning (("assuming signed overflow does not occur "
11676                                     "when simplifying division"),
11677                                    WARN_STRICT_OVERFLOW_MISC);
11678           return fold_convert (type, tem);
11679         }
11680
11681       return NULL_TREE;
11682
11683     case CEIL_MOD_EXPR:
11684     case FLOOR_MOD_EXPR:
11685     case ROUND_MOD_EXPR:
11686     case TRUNC_MOD_EXPR:
11687       /* X % 1 is always zero, but be sure to preserve any side
11688          effects in X.  */
11689       if (integer_onep (arg1))
11690         return omit_one_operand (type, integer_zero_node, arg0);
11691
11692       /* X % 0, return X % 0 unchanged so that we can get the
11693          proper warnings and errors.  */
11694       if (integer_zerop (arg1))
11695         return NULL_TREE;
11696
11697       /* 0 % X is always zero, but be sure to preserve any side
11698          effects in X.  Place this after checking for X == 0.  */
11699       if (integer_zerop (arg0))
11700         return omit_one_operand (type, integer_zero_node, arg1);
11701
11702       /* X % -1 is zero.  */
11703       if (!TYPE_UNSIGNED (type)
11704           && TREE_CODE (arg1) == INTEGER_CST
11705           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11706           && TREE_INT_CST_HIGH (arg1) == -1)
11707         return omit_one_operand (type, integer_zero_node, arg0);
11708
11709       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11710          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11711       strict_overflow_p = false;
11712       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11713           && (TYPE_UNSIGNED (type)
11714               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11715         {
11716           tree c = arg1;
11717           /* Also optimize A % (C << N)  where C is a power of 2,
11718              to A & ((C << N) - 1).  */
11719           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11720             c = TREE_OPERAND (arg1, 0);
11721
11722           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11723             {
11724               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11725                                        build_int_cst (TREE_TYPE (arg1), 1));
11726               if (strict_overflow_p)
11727                 fold_overflow_warning (("assuming signed overflow does not "
11728                                         "occur when simplifying "
11729                                         "X % (power of two)"),
11730                                        WARN_STRICT_OVERFLOW_MISC);
11731               return fold_build2 (BIT_AND_EXPR, type,
11732                                   fold_convert (type, arg0),
11733                                   fold_convert (type, mask));
11734             }
11735         }
11736
11737       /* X % -C is the same as X % C.  */
11738       if (code == TRUNC_MOD_EXPR
11739           && !TYPE_UNSIGNED (type)
11740           && TREE_CODE (arg1) == INTEGER_CST
11741           && !TREE_OVERFLOW (arg1)
11742           && TREE_INT_CST_HIGH (arg1) < 0
11743           && !TYPE_OVERFLOW_TRAPS (type)
11744           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11745           && !sign_bit_p (arg1, arg1))
11746         return fold_build2 (code, type, fold_convert (type, arg0),
11747                             fold_convert (type, negate_expr (arg1)));
11748
11749       /* X % -Y is the same as X % Y.  */
11750       if (code == TRUNC_MOD_EXPR
11751           && !TYPE_UNSIGNED (type)
11752           && TREE_CODE (arg1) == NEGATE_EXPR
11753           && !TYPE_OVERFLOW_TRAPS (type))
11754         return fold_build2 (code, type, fold_convert (type, arg0),
11755                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11756
11757       if (TREE_CODE (arg1) == INTEGER_CST
11758           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11759                                          &strict_overflow_p)))
11760         {
11761           if (strict_overflow_p)
11762             fold_overflow_warning (("assuming signed overflow does not occur "
11763                                     "when simplifying modulus"),
11764                                    WARN_STRICT_OVERFLOW_MISC);
11765           return fold_convert (type, tem);
11766         }
11767
11768       return NULL_TREE;
11769
11770     case LROTATE_EXPR:
11771     case RROTATE_EXPR:
11772       if (integer_all_onesp (arg0))
11773         return omit_one_operand (type, arg0, arg1);
11774       goto shift;
11775
11776     case RSHIFT_EXPR:
11777       /* Optimize -1 >> x for arithmetic right shifts.  */
11778       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11779           && tree_expr_nonnegative_p (arg1))
11780         return omit_one_operand (type, arg0, arg1);
11781       /* ... fall through ...  */
11782
11783     case LSHIFT_EXPR:
11784     shift:
11785       if (integer_zerop (arg1))
11786         return non_lvalue (fold_convert (type, arg0));
11787       if (integer_zerop (arg0))
11788         return omit_one_operand (type, arg0, arg1);
11789
11790       /* Since negative shift count is not well-defined,
11791          don't try to compute it in the compiler.  */
11792       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11793         return NULL_TREE;
11794
11795       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11796       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11797           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11798           && host_integerp (TREE_OPERAND (arg0, 1), false)
11799           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11800         {
11801           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11802                                + TREE_INT_CST_LOW (arg1));
11803
11804           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11805              being well defined.  */
11806           if (low >= TYPE_PRECISION (type))
11807             {
11808               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11809                 low = low % TYPE_PRECISION (type);
11810               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11811                 return build_int_cst (type, 0);
11812               else
11813                 low = TYPE_PRECISION (type) - 1;
11814             }
11815
11816           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11817                               build_int_cst (type, low));
11818         }
11819
11820       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11821          into x & ((unsigned)-1 >> c) for unsigned types.  */
11822       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11823            || (TYPE_UNSIGNED (type)
11824                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11825           && host_integerp (arg1, false)
11826           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11827           && host_integerp (TREE_OPERAND (arg0, 1), false)
11828           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11829         {
11830           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11831           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11832           tree lshift;
11833           tree arg00;
11834
11835           if (low0 == low1)
11836             {
11837               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11838
11839               lshift = build_int_cst (type, -1);
11840               lshift = int_const_binop (code, lshift, arg1, 0);
11841
11842               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11843             }
11844         }
11845
11846       /* Rewrite an LROTATE_EXPR by a constant into an
11847          RROTATE_EXPR by a new constant.  */
11848       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11849         {
11850           tree tem = build_int_cst (TREE_TYPE (arg1),
11851                                     TYPE_PRECISION (type));
11852           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11853           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11854         }
11855
11856       /* If we have a rotate of a bit operation with the rotate count and
11857          the second operand of the bit operation both constant,
11858          permute the two operations.  */
11859       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11860           && (TREE_CODE (arg0) == BIT_AND_EXPR
11861               || TREE_CODE (arg0) == BIT_IOR_EXPR
11862               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11863           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11864         return fold_build2 (TREE_CODE (arg0), type,
11865                             fold_build2 (code, type,
11866                                          TREE_OPERAND (arg0, 0), arg1),
11867                             fold_build2 (code, type,
11868                                          TREE_OPERAND (arg0, 1), arg1));
11869
11870       /* Two consecutive rotates adding up to the precision of the
11871          type can be ignored.  */
11872       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11873           && TREE_CODE (arg0) == RROTATE_EXPR
11874           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11875           && TREE_INT_CST_HIGH (arg1) == 0
11876           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11877           && ((TREE_INT_CST_LOW (arg1)
11878                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11879               == (unsigned int) TYPE_PRECISION (type)))
11880         return TREE_OPERAND (arg0, 0);
11881
11882       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11883               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11884          if the latter can be further optimized.  */
11885       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11886           && TREE_CODE (arg0) == BIT_AND_EXPR
11887           && TREE_CODE (arg1) == INTEGER_CST
11888           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11889         {
11890           tree mask = fold_build2 (code, type,
11891                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11892                                    arg1);
11893           tree shift = fold_build2 (code, type,
11894                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11895                                     arg1);
11896           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11897           if (tem)
11898             return tem;
11899         }
11900
11901       return NULL_TREE;
11902
11903     case MIN_EXPR:
11904       if (operand_equal_p (arg0, arg1, 0))
11905         return omit_one_operand (type, arg0, arg1);
11906       if (INTEGRAL_TYPE_P (type)
11907           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11908         return omit_one_operand (type, arg1, arg0);
11909       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11910       if (tem)
11911         return tem;
11912       goto associate;
11913
11914     case MAX_EXPR:
11915       if (operand_equal_p (arg0, arg1, 0))
11916         return omit_one_operand (type, arg0, arg1);
11917       if (INTEGRAL_TYPE_P (type)
11918           && TYPE_MAX_VALUE (type)
11919           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11920         return omit_one_operand (type, arg1, arg0);
11921       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11922       if (tem)
11923         return tem;
11924       goto associate;
11925
11926     case TRUTH_ANDIF_EXPR:
11927       /* Note that the operands of this must be ints
11928          and their values must be 0 or 1.
11929          ("true" is a fixed value perhaps depending on the language.)  */
11930       /* If first arg is constant zero, return it.  */
11931       if (integer_zerop (arg0))
11932         return fold_convert (type, arg0);
11933     case TRUTH_AND_EXPR:
11934       /* If either arg is constant true, drop it.  */
11935       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11936         return non_lvalue (fold_convert (type, arg1));
11937       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11938           /* Preserve sequence points.  */
11939           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11940         return non_lvalue (fold_convert (type, arg0));
11941       /* If second arg is constant zero, result is zero, but first arg
11942          must be evaluated.  */
11943       if (integer_zerop (arg1))
11944         return omit_one_operand (type, arg1, arg0);
11945       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11946          case will be handled here.  */
11947       if (integer_zerop (arg0))
11948         return omit_one_operand (type, arg0, arg1);
11949
11950       /* !X && X is always false.  */
11951       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11952           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11953         return omit_one_operand (type, integer_zero_node, arg1);
11954       /* X && !X is always false.  */
11955       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11956           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11957         return omit_one_operand (type, integer_zero_node, arg0);
11958
11959       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11960          means A >= Y && A != MAX, but in this case we know that
11961          A < X <= MAX.  */
11962
11963       if (!TREE_SIDE_EFFECTS (arg0)
11964           && !TREE_SIDE_EFFECTS (arg1))
11965         {
11966           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11967           if (tem && !operand_equal_p (tem, arg0, 0))
11968             return fold_build2 (code, type, tem, arg1);
11969
11970           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11971           if (tem && !operand_equal_p (tem, arg1, 0))
11972             return fold_build2 (code, type, arg0, tem);
11973         }
11974
11975     truth_andor:
11976       /* We only do these simplifications if we are optimizing.  */
11977       if (!optimize)
11978         return NULL_TREE;
11979
11980       /* Check for things like (A || B) && (A || C).  We can convert this
11981          to A || (B && C).  Note that either operator can be any of the four
11982          truth and/or operations and the transformation will still be
11983          valid.   Also note that we only care about order for the
11984          ANDIF and ORIF operators.  If B contains side effects, this
11985          might change the truth-value of A.  */
11986       if (TREE_CODE (arg0) == TREE_CODE (arg1)
11987           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11988               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11989               || TREE_CODE (arg0) == TRUTH_AND_EXPR
11990               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11991           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11992         {
11993           tree a00 = TREE_OPERAND (arg0, 0);
11994           tree a01 = TREE_OPERAND (arg0, 1);
11995           tree a10 = TREE_OPERAND (arg1, 0);
11996           tree a11 = TREE_OPERAND (arg1, 1);
11997           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11998                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11999                              && (code == TRUTH_AND_EXPR
12000                                  || code == TRUTH_OR_EXPR));
12001
12002           if (operand_equal_p (a00, a10, 0))
12003             return fold_build2 (TREE_CODE (arg0), type, a00,
12004                                 fold_build2 (code, type, a01, a11));
12005           else if (commutative && operand_equal_p (a00, a11, 0))
12006             return fold_build2 (TREE_CODE (arg0), type, a00,
12007                                 fold_build2 (code, type, a01, a10));
12008           else if (commutative && operand_equal_p (a01, a10, 0))
12009             return fold_build2 (TREE_CODE (arg0), type, a01,
12010                                 fold_build2 (code, type, a00, a11));
12011
12012           /* This case if tricky because we must either have commutative
12013              operators or else A10 must not have side-effects.  */
12014
12015           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12016                    && operand_equal_p (a01, a11, 0))
12017             return fold_build2 (TREE_CODE (arg0), type,
12018                                 fold_build2 (code, type, a00, a10),
12019                                 a01);
12020         }
12021
12022       /* See if we can build a range comparison.  */
12023       if (0 != (tem = fold_range_test (code, type, op0, op1)))
12024         return tem;
12025
12026       /* Check for the possibility of merging component references.  If our
12027          lhs is another similar operation, try to merge its rhs with our
12028          rhs.  Then try to merge our lhs and rhs.  */
12029       if (TREE_CODE (arg0) == code
12030           && 0 != (tem = fold_truthop (code, type,
12031                                        TREE_OPERAND (arg0, 1), arg1)))
12032         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12033
12034       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12035         return tem;
12036
12037       return NULL_TREE;
12038
12039     case TRUTH_ORIF_EXPR:
12040       /* Note that the operands of this must be ints
12041          and their values must be 0 or true.
12042          ("true" is a fixed value perhaps depending on the language.)  */
12043       /* If first arg is constant true, return it.  */
12044       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12045         return fold_convert (type, arg0);
12046     case TRUTH_OR_EXPR:
12047       /* If either arg is constant zero, drop it.  */
12048       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12049         return non_lvalue (fold_convert (type, arg1));
12050       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12051           /* Preserve sequence points.  */
12052           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12053         return non_lvalue (fold_convert (type, arg0));
12054       /* If second arg is constant true, result is true, but we must
12055          evaluate first arg.  */
12056       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12057         return omit_one_operand (type, arg1, arg0);
12058       /* Likewise for first arg, but note this only occurs here for
12059          TRUTH_OR_EXPR.  */
12060       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12061         return omit_one_operand (type, arg0, arg1);
12062
12063       /* !X || X is always true.  */
12064       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12065           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12066         return omit_one_operand (type, integer_one_node, arg1);
12067       /* X || !X is always true.  */
12068       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12069           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12070         return omit_one_operand (type, integer_one_node, arg0);
12071
12072       goto truth_andor;
12073
12074     case TRUTH_XOR_EXPR:
12075       /* If the second arg is constant zero, drop it.  */
12076       if (integer_zerop (arg1))
12077         return non_lvalue (fold_convert (type, arg0));
12078       /* If the second arg is constant true, this is a logical inversion.  */
12079       if (integer_onep (arg1))
12080         {
12081           /* Only call invert_truthvalue if operand is a truth value.  */
12082           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12083             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12084           else
12085             tem = invert_truthvalue (arg0);
12086           return non_lvalue (fold_convert (type, tem));
12087         }
12088       /* Identical arguments cancel to zero.  */
12089       if (operand_equal_p (arg0, arg1, 0))
12090         return omit_one_operand (type, integer_zero_node, arg0);
12091
12092       /* !X ^ X is always true.  */
12093       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12094           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12095         return omit_one_operand (type, integer_one_node, arg1);
12096
12097       /* X ^ !X is always true.  */
12098       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12099           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12100         return omit_one_operand (type, integer_one_node, arg0);
12101
12102       return NULL_TREE;
12103
12104     case EQ_EXPR:
12105     case NE_EXPR:
12106       tem = fold_comparison (code, type, op0, op1);
12107       if (tem != NULL_TREE)
12108         return tem;
12109
12110       /* bool_var != 0 becomes bool_var. */
12111       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12112           && code == NE_EXPR)
12113         return non_lvalue (fold_convert (type, arg0));
12114
12115       /* bool_var == 1 becomes bool_var. */
12116       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12117           && code == EQ_EXPR)
12118         return non_lvalue (fold_convert (type, arg0));
12119
12120       /* bool_var != 1 becomes !bool_var. */
12121       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12122           && code == NE_EXPR)
12123         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12124
12125       /* bool_var == 0 becomes !bool_var. */
12126       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12127           && code == EQ_EXPR)
12128         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12129
12130       /* If this is an equality comparison of the address of two non-weak,
12131          unaliased symbols neither of which are extern (since we do not
12132          have access to attributes for externs), then we know the result.  */
12133       if (TREE_CODE (arg0) == ADDR_EXPR
12134           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12135           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12136           && ! lookup_attribute ("alias",
12137                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12138           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12139           && TREE_CODE (arg1) == ADDR_EXPR
12140           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12141           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12142           && ! lookup_attribute ("alias",
12143                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12144           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12145         {
12146           /* We know that we're looking at the address of two
12147              non-weak, unaliased, static _DECL nodes.
12148
12149              It is both wasteful and incorrect to call operand_equal_p
12150              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12151              all we need to do is test pointer equality for the arguments
12152              to the two ADDR_EXPR nodes.  It is incorrect to use
12153              operand_equal_p as that function is NOT equivalent to a
12154              C equality test.  It can in fact return false for two
12155              objects which would test as equal using the C equality
12156              operator.  */
12157           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12158           return constant_boolean_node (equal
12159                                         ? code == EQ_EXPR : code != EQ_EXPR,
12160                                         type);
12161         }
12162
12163       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12164          a MINUS_EXPR of a constant, we can convert it into a comparison with
12165          a revised constant as long as no overflow occurs.  */
12166       if (TREE_CODE (arg1) == INTEGER_CST
12167           && (TREE_CODE (arg0) == PLUS_EXPR
12168               || TREE_CODE (arg0) == MINUS_EXPR)
12169           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12170           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12171                                       ? MINUS_EXPR : PLUS_EXPR,
12172                                       fold_convert (TREE_TYPE (arg0), arg1),
12173                                       TREE_OPERAND (arg0, 1), 0))
12174           && !TREE_OVERFLOW (tem))
12175         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12176
12177       /* Similarly for a NEGATE_EXPR.  */
12178       if (TREE_CODE (arg0) == NEGATE_EXPR
12179           && TREE_CODE (arg1) == INTEGER_CST
12180           && 0 != (tem = negate_expr (arg1))
12181           && TREE_CODE (tem) == INTEGER_CST
12182           && !TREE_OVERFLOW (tem))
12183         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12184
12185       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12186       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12187           && TREE_CODE (arg1) == INTEGER_CST
12188           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12189         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12190                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12191                                          fold_convert (TREE_TYPE (arg0), arg1),
12192                                          TREE_OPERAND (arg0, 1)));
12193
12194       /* Transform comparisons of the form X +- C CMP X.  */
12195       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12196           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12197           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12198           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12199               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12200         {
12201           tree cst = TREE_OPERAND (arg0, 1);
12202
12203           if (code == EQ_EXPR
12204               && !integer_zerop (cst))
12205             return omit_two_operands (type, boolean_false_node,
12206                                       TREE_OPERAND (arg0, 0), arg1);
12207           else
12208             return omit_two_operands (type, boolean_true_node,
12209                                       TREE_OPERAND (arg0, 0), arg1);
12210         }
12211
12212       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12213          for !=.  Don't do this for ordered comparisons due to overflow.  */
12214       if (TREE_CODE (arg0) == MINUS_EXPR
12215           && integer_zerop (arg1))
12216         return fold_build2 (code, type,
12217                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12218
12219       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12220       if (TREE_CODE (arg0) == ABS_EXPR
12221           && (integer_zerop (arg1) || real_zerop (arg1)))
12222         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12223
12224       /* If this is an EQ or NE comparison with zero and ARG0 is
12225          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12226          two operations, but the latter can be done in one less insn
12227          on machines that have only two-operand insns or on which a
12228          constant cannot be the first operand.  */
12229       if (TREE_CODE (arg0) == BIT_AND_EXPR
12230           && integer_zerop (arg1))
12231         {
12232           tree arg00 = TREE_OPERAND (arg0, 0);
12233           tree arg01 = TREE_OPERAND (arg0, 1);
12234           if (TREE_CODE (arg00) == LSHIFT_EXPR
12235               && integer_onep (TREE_OPERAND (arg00, 0)))
12236             {
12237               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12238                                       arg01, TREE_OPERAND (arg00, 1));
12239               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12240                                  build_int_cst (TREE_TYPE (arg0), 1));
12241               return fold_build2 (code, type,
12242                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12243             }
12244           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12245                    && integer_onep (TREE_OPERAND (arg01, 0)))
12246             {
12247               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12248                                       arg00, TREE_OPERAND (arg01, 1));
12249               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12250                                  build_int_cst (TREE_TYPE (arg0), 1));
12251               return fold_build2 (code, type,
12252                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12253             }
12254         }
12255
12256       /* If this is an NE or EQ comparison of zero against the result of a
12257          signed MOD operation whose second operand is a power of 2, make
12258          the MOD operation unsigned since it is simpler and equivalent.  */
12259       if (integer_zerop (arg1)
12260           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12261           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12262               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12263               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12264               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12265           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12266         {
12267           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12268           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12269                                      fold_convert (newtype,
12270                                                    TREE_OPERAND (arg0, 0)),
12271                                      fold_convert (newtype,
12272                                                    TREE_OPERAND (arg0, 1)));
12273
12274           return fold_build2 (code, type, newmod,
12275                               fold_convert (newtype, arg1));
12276         }
12277
12278       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12279          C1 is a valid shift constant, and C2 is a power of two, i.e.
12280          a single bit.  */
12281       if (TREE_CODE (arg0) == BIT_AND_EXPR
12282           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12283           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12284              == INTEGER_CST
12285           && integer_pow2p (TREE_OPERAND (arg0, 1))
12286           && integer_zerop (arg1))
12287         {
12288           tree itype = TREE_TYPE (arg0);
12289           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12290           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12291
12292           /* Check for a valid shift count.  */
12293           if (TREE_INT_CST_HIGH (arg001) == 0
12294               && TREE_INT_CST_LOW (arg001) < prec)
12295             {
12296               tree arg01 = TREE_OPERAND (arg0, 1);
12297               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12298               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12299               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12300                  can be rewritten as (X & (C2 << C1)) != 0.  */
12301               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12302                 {
12303                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12304                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12305                   return fold_build2 (code, type, tem, arg1);
12306                 }
12307               /* Otherwise, for signed (arithmetic) shifts,
12308                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12309                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12310               else if (!TYPE_UNSIGNED (itype))
12311                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12312                                     arg000, build_int_cst (itype, 0));
12313               /* Otherwise, of unsigned (logical) shifts,
12314                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12315                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12316               else
12317                 return omit_one_operand (type,
12318                                          code == EQ_EXPR ? integer_one_node
12319                                                          : integer_zero_node,
12320                                          arg000);
12321             }
12322         }
12323
12324       /* If this is an NE comparison of zero with an AND of one, remove the
12325          comparison since the AND will give the correct value.  */
12326       if (code == NE_EXPR
12327           && integer_zerop (arg1)
12328           && TREE_CODE (arg0) == BIT_AND_EXPR
12329           && integer_onep (TREE_OPERAND (arg0, 1)))
12330         return fold_convert (type, arg0);
12331
12332       /* If we have (A & C) == C where C is a power of 2, convert this into
12333          (A & C) != 0.  Similarly for NE_EXPR.  */
12334       if (TREE_CODE (arg0) == BIT_AND_EXPR
12335           && integer_pow2p (TREE_OPERAND (arg0, 1))
12336           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12337         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12338                             arg0, fold_convert (TREE_TYPE (arg0),
12339                                                 integer_zero_node));
12340
12341       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12342          bit, then fold the expression into A < 0 or A >= 0.  */
12343       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12344       if (tem)
12345         return tem;
12346
12347       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12348          Similarly for NE_EXPR.  */
12349       if (TREE_CODE (arg0) == BIT_AND_EXPR
12350           && TREE_CODE (arg1) == INTEGER_CST
12351           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12352         {
12353           tree notc = fold_build1 (BIT_NOT_EXPR,
12354                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12355                                    TREE_OPERAND (arg0, 1));
12356           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12357                                        arg1, notc);
12358           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12359           if (integer_nonzerop (dandnotc))
12360             return omit_one_operand (type, rslt, arg0);
12361         }
12362
12363       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12364          Similarly for NE_EXPR.  */
12365       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12366           && TREE_CODE (arg1) == INTEGER_CST
12367           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12368         {
12369           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12370           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12371                                        TREE_OPERAND (arg0, 1), notd);
12372           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12373           if (integer_nonzerop (candnotd))
12374             return omit_one_operand (type, rslt, arg0);
12375         }
12376
12377       /* If this is a comparison of a field, we may be able to simplify it.  */
12378       if ((TREE_CODE (arg0) == COMPONENT_REF
12379            || TREE_CODE (arg0) == BIT_FIELD_REF)
12380           /* Handle the constant case even without -O
12381              to make sure the warnings are given.  */
12382           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12383         {
12384           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12385           if (t1)
12386             return t1;
12387         }
12388
12389       /* Optimize comparisons of strlen vs zero to a compare of the
12390          first character of the string vs zero.  To wit,
12391                 strlen(ptr) == 0   =>  *ptr == 0
12392                 strlen(ptr) != 0   =>  *ptr != 0
12393          Other cases should reduce to one of these two (or a constant)
12394          due to the return value of strlen being unsigned.  */
12395       if (TREE_CODE (arg0) == CALL_EXPR
12396           && integer_zerop (arg1))
12397         {
12398           tree fndecl = get_callee_fndecl (arg0);
12399
12400           if (fndecl
12401               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12402               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12403               && call_expr_nargs (arg0) == 1
12404               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12405             {
12406               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12407               return fold_build2 (code, type, iref,
12408                                   build_int_cst (TREE_TYPE (iref), 0));
12409             }
12410         }
12411
12412       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12413          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12414       if (TREE_CODE (arg0) == RSHIFT_EXPR
12415           && integer_zerop (arg1)
12416           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12417         {
12418           tree arg00 = TREE_OPERAND (arg0, 0);
12419           tree arg01 = TREE_OPERAND (arg0, 1);
12420           tree itype = TREE_TYPE (arg00);
12421           if (TREE_INT_CST_HIGH (arg01) == 0
12422               && TREE_INT_CST_LOW (arg01)
12423                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12424             {
12425               if (TYPE_UNSIGNED (itype))
12426                 {
12427                   itype = signed_type_for (itype);
12428                   arg00 = fold_convert (itype, arg00);
12429                 }
12430               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12431                                   type, arg00, build_int_cst (itype, 0));
12432             }
12433         }
12434
12435       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12436       if (integer_zerop (arg1)
12437           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12438         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12439                             TREE_OPERAND (arg0, 1));
12440
12441       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12442       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12443           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12444         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12445                             build_int_cst (TREE_TYPE (arg1), 0));
12446       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12447       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12448           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12449           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12450         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12451                             build_int_cst (TREE_TYPE (arg1), 0));
12452
12453       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12454       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12455           && TREE_CODE (arg1) == INTEGER_CST
12456           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12457         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12458                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12459                                          TREE_OPERAND (arg0, 1), arg1));
12460
12461       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12462          (X & C) == 0 when C is a single bit.  */
12463       if (TREE_CODE (arg0) == BIT_AND_EXPR
12464           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12465           && integer_zerop (arg1)
12466           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12467         {
12468           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12469                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12470                              TREE_OPERAND (arg0, 1));
12471           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12472                               type, tem, arg1);
12473         }
12474
12475       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12476          constant C is a power of two, i.e. a single bit.  */
12477       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12478           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12479           && integer_zerop (arg1)
12480           && integer_pow2p (TREE_OPERAND (arg0, 1))
12481           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12482                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12483         {
12484           tree arg00 = TREE_OPERAND (arg0, 0);
12485           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12486                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12487         }
12488
12489       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12490          when is C is a power of two, i.e. a single bit.  */
12491       if (TREE_CODE (arg0) == BIT_AND_EXPR
12492           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12493           && integer_zerop (arg1)
12494           && integer_pow2p (TREE_OPERAND (arg0, 1))
12495           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12496                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12497         {
12498           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12499           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12500                              arg000, TREE_OPERAND (arg0, 1));
12501           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12502                               tem, build_int_cst (TREE_TYPE (tem), 0));
12503         }
12504
12505       if (integer_zerop (arg1)
12506           && tree_expr_nonzero_p (arg0))
12507         {
12508           tree res = constant_boolean_node (code==NE_EXPR, type);
12509           return omit_one_operand (type, res, arg0);
12510         }
12511
12512       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12513       if (TREE_CODE (arg0) == NEGATE_EXPR
12514           && TREE_CODE (arg1) == NEGATE_EXPR)
12515         return fold_build2 (code, type,
12516                             TREE_OPERAND (arg0, 0),
12517                             TREE_OPERAND (arg1, 0));
12518
12519       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12520       if (TREE_CODE (arg0) == BIT_AND_EXPR
12521           && TREE_CODE (arg1) == BIT_AND_EXPR)
12522         {
12523           tree arg00 = TREE_OPERAND (arg0, 0);
12524           tree arg01 = TREE_OPERAND (arg0, 1);
12525           tree arg10 = TREE_OPERAND (arg1, 0);
12526           tree arg11 = TREE_OPERAND (arg1, 1);
12527           tree itype = TREE_TYPE (arg0);
12528
12529           if (operand_equal_p (arg01, arg11, 0))
12530             return fold_build2 (code, type,
12531                                 fold_build2 (BIT_AND_EXPR, itype,
12532                                              fold_build2 (BIT_XOR_EXPR, itype,
12533                                                           arg00, arg10),
12534                                              arg01),
12535                                 build_int_cst (itype, 0));
12536
12537           if (operand_equal_p (arg01, arg10, 0))
12538             return fold_build2 (code, type,
12539                                 fold_build2 (BIT_AND_EXPR, itype,
12540                                              fold_build2 (BIT_XOR_EXPR, itype,
12541                                                           arg00, arg11),
12542                                              arg01),
12543                                 build_int_cst (itype, 0));
12544
12545           if (operand_equal_p (arg00, arg11, 0))
12546             return fold_build2 (code, type,
12547                                 fold_build2 (BIT_AND_EXPR, itype,
12548                                              fold_build2 (BIT_XOR_EXPR, itype,
12549                                                           arg01, arg10),
12550                                              arg00),
12551                                 build_int_cst (itype, 0));
12552
12553           if (operand_equal_p (arg00, arg10, 0))
12554             return fold_build2 (code, type,
12555                                 fold_build2 (BIT_AND_EXPR, itype,
12556                                              fold_build2 (BIT_XOR_EXPR, itype,
12557                                                           arg01, arg11),
12558                                              arg00),
12559                                 build_int_cst (itype, 0));
12560         }
12561
12562       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12563           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12564         {
12565           tree arg00 = TREE_OPERAND (arg0, 0);
12566           tree arg01 = TREE_OPERAND (arg0, 1);
12567           tree arg10 = TREE_OPERAND (arg1, 0);
12568           tree arg11 = TREE_OPERAND (arg1, 1);
12569           tree itype = TREE_TYPE (arg0);
12570
12571           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12572              operand_equal_p guarantees no side-effects so we don't need
12573              to use omit_one_operand on Z.  */
12574           if (operand_equal_p (arg01, arg11, 0))
12575             return fold_build2 (code, type, arg00, arg10);
12576           if (operand_equal_p (arg01, arg10, 0))
12577             return fold_build2 (code, type, arg00, arg11);
12578           if (operand_equal_p (arg00, arg11, 0))
12579             return fold_build2 (code, type, arg01, arg10);
12580           if (operand_equal_p (arg00, arg10, 0))
12581             return fold_build2 (code, type, arg01, arg11);
12582
12583           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12584           if (TREE_CODE (arg01) == INTEGER_CST
12585               && TREE_CODE (arg11) == INTEGER_CST)
12586             return fold_build2 (code, type,
12587                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12588                                              fold_build2 (BIT_XOR_EXPR, itype,
12589                                                           arg01, arg11)),
12590                                 arg10);
12591         }
12592
12593       /* Attempt to simplify equality/inequality comparisons of complex
12594          values.  Only lower the comparison if the result is known or
12595          can be simplified to a single scalar comparison.  */
12596       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12597            || TREE_CODE (arg0) == COMPLEX_CST)
12598           && (TREE_CODE (arg1) == COMPLEX_EXPR
12599               || TREE_CODE (arg1) == COMPLEX_CST))
12600         {
12601           tree real0, imag0, real1, imag1;
12602           tree rcond, icond;
12603
12604           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12605             {
12606               real0 = TREE_OPERAND (arg0, 0);
12607               imag0 = TREE_OPERAND (arg0, 1);
12608             }
12609           else
12610             {
12611               real0 = TREE_REALPART (arg0);
12612               imag0 = TREE_IMAGPART (arg0);
12613             }
12614
12615           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12616             {
12617               real1 = TREE_OPERAND (arg1, 0);
12618               imag1 = TREE_OPERAND (arg1, 1);
12619             }
12620           else
12621             {
12622               real1 = TREE_REALPART (arg1);
12623               imag1 = TREE_IMAGPART (arg1);
12624             }
12625
12626           rcond = fold_binary (code, type, real0, real1);
12627           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12628             {
12629               if (integer_zerop (rcond))
12630                 {
12631                   if (code == EQ_EXPR)
12632                     return omit_two_operands (type, boolean_false_node,
12633                                               imag0, imag1);
12634                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12635                 }
12636               else
12637                 {
12638                   if (code == NE_EXPR)
12639                     return omit_two_operands (type, boolean_true_node,
12640                                               imag0, imag1);
12641                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12642                 }
12643             }
12644
12645           icond = fold_binary (code, type, imag0, imag1);
12646           if (icond && TREE_CODE (icond) == INTEGER_CST)
12647             {
12648               if (integer_zerop (icond))
12649                 {
12650                   if (code == EQ_EXPR)
12651                     return omit_two_operands (type, boolean_false_node,
12652                                               real0, real1);
12653                   return fold_build2 (NE_EXPR, type, real0, real1);
12654                 }
12655               else
12656                 {
12657                   if (code == NE_EXPR)
12658                     return omit_two_operands (type, boolean_true_node,
12659                                               real0, real1);
12660                   return fold_build2 (EQ_EXPR, type, real0, real1);
12661                 }
12662             }
12663         }
12664
12665       return NULL_TREE;
12666
12667     case LT_EXPR:
12668     case GT_EXPR:
12669     case LE_EXPR:
12670     case GE_EXPR:
12671       tem = fold_comparison (code, type, op0, op1);
12672       if (tem != NULL_TREE)
12673         return tem;
12674
12675       /* Transform comparisons of the form X +- C CMP X.  */
12676       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12677           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12678           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12679                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12680               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12681                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12682         {
12683           tree arg01 = TREE_OPERAND (arg0, 1);
12684           enum tree_code code0 = TREE_CODE (arg0);
12685           int is_positive;
12686
12687           if (TREE_CODE (arg01) == REAL_CST)
12688             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12689           else
12690             is_positive = tree_int_cst_sgn (arg01);
12691
12692           /* (X - c) > X becomes false.  */
12693           if (code == GT_EXPR
12694               && ((code0 == MINUS_EXPR && is_positive >= 0)
12695                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12696             {
12697               if (TREE_CODE (arg01) == INTEGER_CST
12698                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12699                 fold_overflow_warning (("assuming signed overflow does not "
12700                                         "occur when assuming that (X - c) > X "
12701                                         "is always false"),
12702                                        WARN_STRICT_OVERFLOW_ALL);
12703               return constant_boolean_node (0, type);
12704             }
12705
12706           /* Likewise (X + c) < X becomes false.  */
12707           if (code == LT_EXPR
12708               && ((code0 == PLUS_EXPR && is_positive >= 0)
12709                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12710             {
12711               if (TREE_CODE (arg01) == INTEGER_CST
12712                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12713                 fold_overflow_warning (("assuming signed overflow does not "
12714                                         "occur when assuming that "
12715                                         "(X + c) < X is always false"),
12716                                        WARN_STRICT_OVERFLOW_ALL);
12717               return constant_boolean_node (0, type);
12718             }
12719
12720           /* Convert (X - c) <= X to true.  */
12721           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12722               && code == LE_EXPR
12723               && ((code0 == MINUS_EXPR && is_positive >= 0)
12724                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12725             {
12726               if (TREE_CODE (arg01) == INTEGER_CST
12727                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12728                 fold_overflow_warning (("assuming signed overflow does not "
12729                                         "occur when assuming that "
12730                                         "(X - c) <= X is always true"),
12731                                        WARN_STRICT_OVERFLOW_ALL);
12732               return constant_boolean_node (1, type);
12733             }
12734
12735           /* Convert (X + c) >= X to true.  */
12736           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12737               && code == GE_EXPR
12738               && ((code0 == PLUS_EXPR && is_positive >= 0)
12739                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12740             {
12741               if (TREE_CODE (arg01) == INTEGER_CST
12742                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12743                 fold_overflow_warning (("assuming signed overflow does not "
12744                                         "occur when assuming that "
12745                                         "(X + c) >= X is always true"),
12746                                        WARN_STRICT_OVERFLOW_ALL);
12747               return constant_boolean_node (1, type);
12748             }
12749
12750           if (TREE_CODE (arg01) == INTEGER_CST)
12751             {
12752               /* Convert X + c > X and X - c < X to true for integers.  */
12753               if (code == GT_EXPR
12754                   && ((code0 == PLUS_EXPR && is_positive > 0)
12755                       || (code0 == MINUS_EXPR && is_positive < 0)))
12756                 {
12757                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12758                     fold_overflow_warning (("assuming signed overflow does "
12759                                             "not occur when assuming that "
12760                                             "(X + c) > X is always true"),
12761                                            WARN_STRICT_OVERFLOW_ALL);
12762                   return constant_boolean_node (1, type);
12763                 }
12764
12765               if (code == LT_EXPR
12766                   && ((code0 == MINUS_EXPR && is_positive > 0)
12767                       || (code0 == PLUS_EXPR && is_positive < 0)))
12768                 {
12769                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12770                     fold_overflow_warning (("assuming signed overflow does "
12771                                             "not occur when assuming that "
12772                                             "(X - c) < X is always true"),
12773                                            WARN_STRICT_OVERFLOW_ALL);
12774                   return constant_boolean_node (1, type);
12775                 }
12776
12777               /* Convert X + c <= X and X - c >= X to false for integers.  */
12778               if (code == LE_EXPR
12779                   && ((code0 == PLUS_EXPR && is_positive > 0)
12780                       || (code0 == MINUS_EXPR && is_positive < 0)))
12781                 {
12782                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12783                     fold_overflow_warning (("assuming signed overflow does "
12784                                             "not occur when assuming that "
12785                                             "(X + c) <= X is always false"),
12786                                            WARN_STRICT_OVERFLOW_ALL);
12787                   return constant_boolean_node (0, type);
12788                 }
12789
12790               if (code == GE_EXPR
12791                   && ((code0 == MINUS_EXPR && is_positive > 0)
12792                       || (code0 == PLUS_EXPR && is_positive < 0)))
12793                 {
12794                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12795                     fold_overflow_warning (("assuming signed overflow does "
12796                                             "not occur when assuming that "
12797                                             "(X - c) >= X is always false"),
12798                                            WARN_STRICT_OVERFLOW_ALL);
12799                   return constant_boolean_node (0, type);
12800                 }
12801             }
12802         }
12803
12804       /* Comparisons with the highest or lowest possible integer of
12805          the specified precision will have known values.  */
12806       {
12807         tree arg1_type = TREE_TYPE (arg1);
12808         unsigned int width = TYPE_PRECISION (arg1_type);
12809
12810         if (TREE_CODE (arg1) == INTEGER_CST
12811             && width <= 2 * HOST_BITS_PER_WIDE_INT
12812             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12813           {
12814             HOST_WIDE_INT signed_max_hi;
12815             unsigned HOST_WIDE_INT signed_max_lo;
12816             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12817
12818             if (width <= HOST_BITS_PER_WIDE_INT)
12819               {
12820                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12821                                 - 1;
12822                 signed_max_hi = 0;
12823                 max_hi = 0;
12824
12825                 if (TYPE_UNSIGNED (arg1_type))
12826                   {
12827                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12828                     min_lo = 0;
12829                     min_hi = 0;
12830                   }
12831                 else
12832                   {
12833                     max_lo = signed_max_lo;
12834                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12835                     min_hi = -1;
12836                   }
12837               }
12838             else
12839               {
12840                 width -= HOST_BITS_PER_WIDE_INT;
12841                 signed_max_lo = -1;
12842                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12843                                 - 1;
12844                 max_lo = -1;
12845                 min_lo = 0;
12846
12847                 if (TYPE_UNSIGNED (arg1_type))
12848                   {
12849                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12850                     min_hi = 0;
12851                   }
12852                 else
12853                   {
12854                     max_hi = signed_max_hi;
12855                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12856                   }
12857               }
12858
12859             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12860                 && TREE_INT_CST_LOW (arg1) == max_lo)
12861               switch (code)
12862                 {
12863                 case GT_EXPR:
12864                   return omit_one_operand (type, integer_zero_node, arg0);
12865
12866                 case GE_EXPR:
12867                   return fold_build2 (EQ_EXPR, type, op0, op1);
12868
12869                 case LE_EXPR:
12870                   return omit_one_operand (type, integer_one_node, arg0);
12871
12872                 case LT_EXPR:
12873                   return fold_build2 (NE_EXPR, type, op0, op1);
12874
12875                 /* The GE_EXPR and LT_EXPR cases above are not normally
12876                    reached because of previous transformations.  */
12877
12878                 default:
12879                   break;
12880                 }
12881             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12882                      == max_hi
12883                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12884               switch (code)
12885                 {
12886                 case GT_EXPR:
12887                   arg1 = const_binop (PLUS_EXPR, arg1,
12888                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12889                   return fold_build2 (EQ_EXPR, type,
12890                                       fold_convert (TREE_TYPE (arg1), arg0),
12891                                       arg1);
12892                 case LE_EXPR:
12893                   arg1 = const_binop (PLUS_EXPR, arg1,
12894                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12895                   return fold_build2 (NE_EXPR, type,
12896                                       fold_convert (TREE_TYPE (arg1), arg0),
12897                                       arg1);
12898                 default:
12899                   break;
12900                 }
12901             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12902                      == min_hi
12903                      && TREE_INT_CST_LOW (arg1) == min_lo)
12904               switch (code)
12905                 {
12906                 case LT_EXPR:
12907                   return omit_one_operand (type, integer_zero_node, arg0);
12908
12909                 case LE_EXPR:
12910                   return fold_build2 (EQ_EXPR, type, op0, op1);
12911
12912                 case GE_EXPR:
12913                   return omit_one_operand (type, integer_one_node, arg0);
12914
12915                 case GT_EXPR:
12916                   return fold_build2 (NE_EXPR, type, op0, op1);
12917
12918                 default:
12919                   break;
12920                 }
12921             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12922                      == min_hi
12923                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12924               switch (code)
12925                 {
12926                 case GE_EXPR:
12927                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12928                   return fold_build2 (NE_EXPR, type,
12929                                       fold_convert (TREE_TYPE (arg1), arg0),
12930                                       arg1);
12931                 case LT_EXPR:
12932                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12933                   return fold_build2 (EQ_EXPR, type,
12934                                       fold_convert (TREE_TYPE (arg1), arg0),
12935                                       arg1);
12936                 default:
12937                   break;
12938                 }
12939
12940             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12941                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12942                      && TYPE_UNSIGNED (arg1_type)
12943                      /* We will flip the signedness of the comparison operator
12944                         associated with the mode of arg1, so the sign bit is
12945                         specified by this mode.  Check that arg1 is the signed
12946                         max associated with this sign bit.  */
12947                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12948                      /* signed_type does not work on pointer types.  */
12949                      && INTEGRAL_TYPE_P (arg1_type))
12950               {
12951                 /* The following case also applies to X < signed_max+1
12952                    and X >= signed_max+1 because previous transformations.  */
12953                 if (code == LE_EXPR || code == GT_EXPR)
12954                   {
12955                     tree st;
12956                     st = signed_type_for (TREE_TYPE (arg1));
12957                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12958                                         type, fold_convert (st, arg0),
12959                                         build_int_cst (st, 0));
12960                   }
12961               }
12962           }
12963       }
12964
12965       /* If we are comparing an ABS_EXPR with a constant, we can
12966          convert all the cases into explicit comparisons, but they may
12967          well not be faster than doing the ABS and one comparison.
12968          But ABS (X) <= C is a range comparison, which becomes a subtraction
12969          and a comparison, and is probably faster.  */
12970       if (code == LE_EXPR
12971           && TREE_CODE (arg1) == INTEGER_CST
12972           && TREE_CODE (arg0) == ABS_EXPR
12973           && ! TREE_SIDE_EFFECTS (arg0)
12974           && (0 != (tem = negate_expr (arg1)))
12975           && TREE_CODE (tem) == INTEGER_CST
12976           && !TREE_OVERFLOW (tem))
12977         return fold_build2 (TRUTH_ANDIF_EXPR, type,
12978                             build2 (GE_EXPR, type,
12979                                     TREE_OPERAND (arg0, 0), tem),
12980                             build2 (LE_EXPR, type,
12981                                     TREE_OPERAND (arg0, 0), arg1));
12982
12983       /* Convert ABS_EXPR<x> >= 0 to true.  */
12984       strict_overflow_p = false;
12985       if (code == GE_EXPR
12986           && (integer_zerop (arg1)
12987               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12988                   && real_zerop (arg1)))
12989           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12990         {
12991           if (strict_overflow_p)
12992             fold_overflow_warning (("assuming signed overflow does not occur "
12993                                     "when simplifying comparison of "
12994                                     "absolute value and zero"),
12995                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
12996           return omit_one_operand (type, integer_one_node, arg0);
12997         }
12998
12999       /* Convert ABS_EXPR<x> < 0 to false.  */
13000       strict_overflow_p = false;
13001       if (code == LT_EXPR
13002           && (integer_zerop (arg1) || real_zerop (arg1))
13003           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13004         {
13005           if (strict_overflow_p)
13006             fold_overflow_warning (("assuming signed overflow does not occur "
13007                                     "when simplifying comparison of "
13008                                     "absolute value and zero"),
13009                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13010           return omit_one_operand (type, integer_zero_node, arg0);
13011         }
13012
13013       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13014          and similarly for >= into !=.  */
13015       if ((code == LT_EXPR || code == GE_EXPR)
13016           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13017           && TREE_CODE (arg1) == LSHIFT_EXPR
13018           && integer_onep (TREE_OPERAND (arg1, 0)))
13019         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13020                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13021                                TREE_OPERAND (arg1, 1)),
13022                        build_int_cst (TREE_TYPE (arg0), 0));
13023
13024       if ((code == LT_EXPR || code == GE_EXPR)
13025           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13026           && CONVERT_EXPR_P (arg1)
13027           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13028           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13029         return
13030           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13031                   fold_convert (TREE_TYPE (arg0),
13032                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13033                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
13034                                                       1))),
13035                   build_int_cst (TREE_TYPE (arg0), 0));
13036
13037       return NULL_TREE;
13038
13039     case UNORDERED_EXPR:
13040     case ORDERED_EXPR:
13041     case UNLT_EXPR:
13042     case UNLE_EXPR:
13043     case UNGT_EXPR:
13044     case UNGE_EXPR:
13045     case UNEQ_EXPR:
13046     case LTGT_EXPR:
13047       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13048         {
13049           t1 = fold_relational_const (code, type, arg0, arg1);
13050           if (t1 != NULL_TREE)
13051             return t1;
13052         }
13053
13054       /* If the first operand is NaN, the result is constant.  */
13055       if (TREE_CODE (arg0) == REAL_CST
13056           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13057           && (code != LTGT_EXPR || ! flag_trapping_math))
13058         {
13059           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13060                ? integer_zero_node
13061                : integer_one_node;
13062           return omit_one_operand (type, t1, arg1);
13063         }
13064
13065       /* If the second operand is NaN, the result is constant.  */
13066       if (TREE_CODE (arg1) == REAL_CST
13067           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13068           && (code != LTGT_EXPR || ! flag_trapping_math))
13069         {
13070           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13071                ? integer_zero_node
13072                : integer_one_node;
13073           return omit_one_operand (type, t1, arg0);
13074         }
13075
13076       /* Simplify unordered comparison of something with itself.  */
13077       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13078           && operand_equal_p (arg0, arg1, 0))
13079         return constant_boolean_node (1, type);
13080
13081       if (code == LTGT_EXPR
13082           && !flag_trapping_math
13083           && operand_equal_p (arg0, arg1, 0))
13084         return constant_boolean_node (0, type);
13085
13086       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13087       {
13088         tree targ0 = strip_float_extensions (arg0);
13089         tree targ1 = strip_float_extensions (arg1);
13090         tree newtype = TREE_TYPE (targ0);
13091
13092         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13093           newtype = TREE_TYPE (targ1);
13094
13095         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13096           return fold_build2 (code, type, fold_convert (newtype, targ0),
13097                               fold_convert (newtype, targ1));
13098       }
13099
13100       return NULL_TREE;
13101
13102     case COMPOUND_EXPR:
13103       /* When pedantic, a compound expression can be neither an lvalue
13104          nor an integer constant expression.  */
13105       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13106         return NULL_TREE;
13107       /* Don't let (0, 0) be null pointer constant.  */
13108       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13109                                  : fold_convert (type, arg1);
13110       return pedantic_non_lvalue (tem);
13111
13112     case COMPLEX_EXPR:
13113       if ((TREE_CODE (arg0) == REAL_CST
13114            && TREE_CODE (arg1) == REAL_CST)
13115           || (TREE_CODE (arg0) == INTEGER_CST
13116               && TREE_CODE (arg1) == INTEGER_CST))
13117         return build_complex (type, arg0, arg1);
13118       return NULL_TREE;
13119
13120     case ASSERT_EXPR:
13121       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13122       gcc_unreachable ();
13123
13124     default:
13125       return NULL_TREE;
13126     } /* switch (code) */
13127 }
13128
13129 /* Callback for walk_tree, looking for LABEL_EXPR.
13130    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13131    Do not check the sub-tree of GOTO_EXPR.  */
13132
13133 static tree
13134 contains_label_1 (tree *tp,
13135                   int *walk_subtrees,
13136                   void *data ATTRIBUTE_UNUSED)
13137 {
13138   switch (TREE_CODE (*tp))
13139     {
13140     case LABEL_EXPR:
13141       return *tp;
13142     case GOTO_EXPR:
13143       *walk_subtrees = 0;
13144     /* no break */
13145     default:
13146       return NULL_TREE;
13147     }
13148 }
13149
13150 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13151    accessible from outside the sub-tree. Returns NULL_TREE if no
13152    addressable label is found.  */
13153
13154 static bool
13155 contains_label_p (tree st)
13156 {
13157   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13158 }
13159
13160 /* Fold a ternary expression of code CODE and type TYPE with operands
13161    OP0, OP1, and OP2.  Return the folded expression if folding is
13162    successful.  Otherwise, return NULL_TREE.  */
13163
13164 tree
13165 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13166 {
13167   tree tem;
13168   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13169   enum tree_code_class kind = TREE_CODE_CLASS (code);
13170
13171   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13172               && TREE_CODE_LENGTH (code) == 3);
13173
13174   /* Strip any conversions that don't change the mode.  This is safe
13175      for every expression, except for a comparison expression because
13176      its signedness is derived from its operands.  So, in the latter
13177      case, only strip conversions that don't change the signedness.
13178
13179      Note that this is done as an internal manipulation within the
13180      constant folder, in order to find the simplest representation of
13181      the arguments so that their form can be studied.  In any cases,
13182      the appropriate type conversions should be put back in the tree
13183      that will get out of the constant folder.  */
13184   if (op0)
13185     {
13186       arg0 = op0;
13187       STRIP_NOPS (arg0);
13188     }
13189
13190   if (op1)
13191     {
13192       arg1 = op1;
13193       STRIP_NOPS (arg1);
13194     }
13195
13196   switch (code)
13197     {
13198     case COMPONENT_REF:
13199       if (TREE_CODE (arg0) == CONSTRUCTOR
13200           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13201         {
13202           unsigned HOST_WIDE_INT idx;
13203           tree field, value;
13204           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13205             if (field == arg1)
13206               return value;
13207         }
13208       return NULL_TREE;
13209
13210     case COND_EXPR:
13211       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13212          so all simple results must be passed through pedantic_non_lvalue.  */
13213       if (TREE_CODE (arg0) == INTEGER_CST)
13214         {
13215           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13216           tem = integer_zerop (arg0) ? op2 : op1;
13217           /* Only optimize constant conditions when the selected branch
13218              has the same type as the COND_EXPR.  This avoids optimizing
13219              away "c ? x : throw", where the throw has a void type.
13220              Avoid throwing away that operand which contains label.  */
13221           if ((!TREE_SIDE_EFFECTS (unused_op)
13222                || !contains_label_p (unused_op))
13223               && (! VOID_TYPE_P (TREE_TYPE (tem))
13224                   || VOID_TYPE_P (type)))
13225             return pedantic_non_lvalue (tem);
13226           return NULL_TREE;
13227         }
13228       if (operand_equal_p (arg1, op2, 0))
13229         return pedantic_omit_one_operand (type, arg1, arg0);
13230
13231       /* If we have A op B ? A : C, we may be able to convert this to a
13232          simpler expression, depending on the operation and the values
13233          of B and C.  Signed zeros prevent all of these transformations,
13234          for reasons given above each one.
13235
13236          Also try swapping the arguments and inverting the conditional.  */
13237       if (COMPARISON_CLASS_P (arg0)
13238           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13239                                              arg1, TREE_OPERAND (arg0, 1))
13240           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13241         {
13242           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13243           if (tem)
13244             return tem;
13245         }
13246
13247       if (COMPARISON_CLASS_P (arg0)
13248           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13249                                              op2,
13250                                              TREE_OPERAND (arg0, 1))
13251           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13252         {
13253           tem = fold_truth_not_expr (arg0);
13254           if (tem && COMPARISON_CLASS_P (tem))
13255             {
13256               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13257               if (tem)
13258                 return tem;
13259             }
13260         }
13261
13262       /* If the second operand is simpler than the third, swap them
13263          since that produces better jump optimization results.  */
13264       if (truth_value_p (TREE_CODE (arg0))
13265           && tree_swap_operands_p (op1, op2, false))
13266         {
13267           /* See if this can be inverted.  If it can't, possibly because
13268              it was a floating-point inequality comparison, don't do
13269              anything.  */
13270           tem = fold_truth_not_expr (arg0);
13271           if (tem)
13272             return fold_build3 (code, type, tem, op2, op1);
13273         }
13274
13275       /* Convert A ? 1 : 0 to simply A.  */
13276       if (integer_onep (op1)
13277           && integer_zerop (op2)
13278           /* If we try to convert OP0 to our type, the
13279              call to fold will try to move the conversion inside
13280              a COND, which will recurse.  In that case, the COND_EXPR
13281              is probably the best choice, so leave it alone.  */
13282           && type == TREE_TYPE (arg0))
13283         return pedantic_non_lvalue (arg0);
13284
13285       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13286          over COND_EXPR in cases such as floating point comparisons.  */
13287       if (integer_zerop (op1)
13288           && integer_onep (op2)
13289           && truth_value_p (TREE_CODE (arg0)))
13290         return pedantic_non_lvalue (fold_convert (type,
13291                                                   invert_truthvalue (arg0)));
13292
13293       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13294       if (TREE_CODE (arg0) == LT_EXPR
13295           && integer_zerop (TREE_OPERAND (arg0, 1))
13296           && integer_zerop (op2)
13297           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13298         {
13299           /* sign_bit_p only checks ARG1 bits within A's precision.
13300              If <sign bit of A> has wider type than A, bits outside
13301              of A's precision in <sign bit of A> need to be checked.
13302              If they are all 0, this optimization needs to be done
13303              in unsigned A's type, if they are all 1 in signed A's type,
13304              otherwise this can't be done.  */
13305           if (TYPE_PRECISION (TREE_TYPE (tem))
13306               < TYPE_PRECISION (TREE_TYPE (arg1))
13307               && TYPE_PRECISION (TREE_TYPE (tem))
13308                  < TYPE_PRECISION (type))
13309             {
13310               unsigned HOST_WIDE_INT mask_lo;
13311               HOST_WIDE_INT mask_hi;
13312               int inner_width, outer_width;
13313               tree tem_type;
13314
13315               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13316               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13317               if (outer_width > TYPE_PRECISION (type))
13318                 outer_width = TYPE_PRECISION (type);
13319
13320               if (outer_width > HOST_BITS_PER_WIDE_INT)
13321                 {
13322                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13323                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13324                   mask_lo = -1;
13325                 }
13326               else
13327                 {
13328                   mask_hi = 0;
13329                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13330                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13331                 }
13332               if (inner_width > HOST_BITS_PER_WIDE_INT)
13333                 {
13334                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13335                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13336                   mask_lo = 0;
13337                 }
13338               else
13339                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13340                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13341
13342               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13343                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13344                 {
13345                   tem_type = signed_type_for (TREE_TYPE (tem));
13346                   tem = fold_convert (tem_type, tem);
13347                 }
13348               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13349                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13350                 {
13351                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13352                   tem = fold_convert (tem_type, tem);
13353                 }
13354               else
13355                 tem = NULL;
13356             }
13357
13358           if (tem)
13359             return fold_convert (type,
13360                                  fold_build2 (BIT_AND_EXPR,
13361                                               TREE_TYPE (tem), tem,
13362                                               fold_convert (TREE_TYPE (tem),
13363                                                             arg1)));
13364         }
13365
13366       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13367          already handled above.  */
13368       if (TREE_CODE (arg0) == BIT_AND_EXPR
13369           && integer_onep (TREE_OPERAND (arg0, 1))
13370           && integer_zerop (op2)
13371           && integer_pow2p (arg1))
13372         {
13373           tree tem = TREE_OPERAND (arg0, 0);
13374           STRIP_NOPS (tem);
13375           if (TREE_CODE (tem) == RSHIFT_EXPR
13376               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13377               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13378                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13379             return fold_build2 (BIT_AND_EXPR, type,
13380                                 TREE_OPERAND (tem, 0), arg1);
13381         }
13382
13383       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13384          is probably obsolete because the first operand should be a
13385          truth value (that's why we have the two cases above), but let's
13386          leave it in until we can confirm this for all front-ends.  */
13387       if (integer_zerop (op2)
13388           && TREE_CODE (arg0) == NE_EXPR
13389           && integer_zerop (TREE_OPERAND (arg0, 1))
13390           && integer_pow2p (arg1)
13391           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13392           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13393                               arg1, OEP_ONLY_CONST))
13394         return pedantic_non_lvalue (fold_convert (type,
13395                                                   TREE_OPERAND (arg0, 0)));
13396
13397       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13398       if (integer_zerop (op2)
13399           && truth_value_p (TREE_CODE (arg0))
13400           && truth_value_p (TREE_CODE (arg1)))
13401         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13402                             fold_convert (type, arg0),
13403                             arg1);
13404
13405       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13406       if (integer_onep (op2)
13407           && truth_value_p (TREE_CODE (arg0))
13408           && truth_value_p (TREE_CODE (arg1)))
13409         {
13410           /* Only perform transformation if ARG0 is easily inverted.  */
13411           tem = fold_truth_not_expr (arg0);
13412           if (tem)
13413             return fold_build2 (TRUTH_ORIF_EXPR, type,
13414                                 fold_convert (type, tem),
13415                                 arg1);
13416         }
13417
13418       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13419       if (integer_zerop (arg1)
13420           && truth_value_p (TREE_CODE (arg0))
13421           && truth_value_p (TREE_CODE (op2)))
13422         {
13423           /* Only perform transformation if ARG0 is easily inverted.  */
13424           tem = fold_truth_not_expr (arg0);
13425           if (tem)
13426             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13427                                 fold_convert (type, tem),
13428                                 op2);
13429         }
13430
13431       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13432       if (integer_onep (arg1)
13433           && truth_value_p (TREE_CODE (arg0))
13434           && truth_value_p (TREE_CODE (op2)))
13435         return fold_build2 (TRUTH_ORIF_EXPR, type,
13436                             fold_convert (type, arg0),
13437                             op2);
13438
13439       return NULL_TREE;
13440
13441     case CALL_EXPR:
13442       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13443          of fold_ternary on them.  */
13444       gcc_unreachable ();
13445
13446     case BIT_FIELD_REF:
13447       if ((TREE_CODE (arg0) == VECTOR_CST
13448            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13449           && type == TREE_TYPE (TREE_TYPE (arg0)))
13450         {
13451           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13452           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13453
13454           if (width != 0
13455               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13456               && (idx % width) == 0
13457               && (idx = idx / width)
13458                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13459             {
13460               tree elements = NULL_TREE;
13461
13462               if (TREE_CODE (arg0) == VECTOR_CST)
13463                 elements = TREE_VECTOR_CST_ELTS (arg0);
13464               else
13465                 {
13466                   unsigned HOST_WIDE_INT idx;
13467                   tree value;
13468
13469                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13470                     elements = tree_cons (NULL_TREE, value, elements);
13471                 }
13472               while (idx-- > 0 && elements)
13473                 elements = TREE_CHAIN (elements);
13474               if (elements)
13475                 return TREE_VALUE (elements);
13476               else
13477                 return fold_convert (type, integer_zero_node);
13478             }
13479         }
13480
13481       /* A bit-field-ref that referenced the full argument can be stripped.  */
13482       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13483           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13484           && integer_zerop (op2))
13485         return fold_convert (type, arg0);
13486
13487       return NULL_TREE;
13488
13489     default:
13490       return NULL_TREE;
13491     } /* switch (code) */
13492 }
13493
13494 /* Perform constant folding and related simplification of EXPR.
13495    The related simplifications include x*1 => x, x*0 => 0, etc.,
13496    and application of the associative law.
13497    NOP_EXPR conversions may be removed freely (as long as we
13498    are careful not to change the type of the overall expression).
13499    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13500    but we can constant-fold them if they have constant operands.  */
13501
13502 #ifdef ENABLE_FOLD_CHECKING
13503 # define fold(x) fold_1 (x)
13504 static tree fold_1 (tree);
13505 static
13506 #endif
13507 tree
13508 fold (tree expr)
13509 {
13510   const tree t = expr;
13511   enum tree_code code = TREE_CODE (t);
13512   enum tree_code_class kind = TREE_CODE_CLASS (code);
13513   tree tem;
13514
13515   /* Return right away if a constant.  */
13516   if (kind == tcc_constant)
13517     return t;
13518
13519   /* CALL_EXPR-like objects with variable numbers of operands are
13520      treated specially.  */
13521   if (kind == tcc_vl_exp)
13522     {
13523       if (code == CALL_EXPR)
13524         {
13525           tem = fold_call_expr (expr, false);
13526           return tem ? tem : expr;
13527         }
13528       return expr;
13529     }
13530
13531   if (IS_EXPR_CODE_CLASS (kind))
13532     {
13533       tree type = TREE_TYPE (t);
13534       tree op0, op1, op2;
13535
13536       switch (TREE_CODE_LENGTH (code))
13537         {
13538         case 1:
13539           op0 = TREE_OPERAND (t, 0);
13540           tem = fold_unary (code, type, op0);
13541           return tem ? tem : expr;
13542         case 2:
13543           op0 = TREE_OPERAND (t, 0);
13544           op1 = TREE_OPERAND (t, 1);
13545           tem = fold_binary (code, type, op0, op1);
13546           return tem ? tem : expr;
13547         case 3:
13548           op0 = TREE_OPERAND (t, 0);
13549           op1 = TREE_OPERAND (t, 1);
13550           op2 = TREE_OPERAND (t, 2);
13551           tem = fold_ternary (code, type, op0, op1, op2);
13552           return tem ? tem : expr;
13553         default:
13554           break;
13555         }
13556     }
13557
13558   switch (code)
13559     {
13560     case ARRAY_REF:
13561       {
13562         tree op0 = TREE_OPERAND (t, 0);
13563         tree op1 = TREE_OPERAND (t, 1);
13564
13565         if (TREE_CODE (op1) == INTEGER_CST
13566             && TREE_CODE (op0) == CONSTRUCTOR
13567             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13568           {
13569             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13570             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13571             unsigned HOST_WIDE_INT begin = 0;
13572
13573             /* Find a matching index by means of a binary search.  */
13574             while (begin != end)
13575               {
13576                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13577                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13578
13579                 if (TREE_CODE (index) == INTEGER_CST
13580                     && tree_int_cst_lt (index, op1))
13581                   begin = middle + 1;
13582                 else if (TREE_CODE (index) == INTEGER_CST
13583                          && tree_int_cst_lt (op1, index))
13584                   end = middle;
13585                 else if (TREE_CODE (index) == RANGE_EXPR
13586                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13587                   begin = middle + 1;
13588                 else if (TREE_CODE (index) == RANGE_EXPR
13589                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13590                   end = middle;
13591                 else
13592                   return VEC_index (constructor_elt, elts, middle)->value;
13593               }
13594           }
13595
13596         return t;
13597       }
13598
13599     case CONST_DECL:
13600       return fold (DECL_INITIAL (t));
13601
13602     default:
13603       return t;
13604     } /* switch (code) */
13605 }
13606
13607 #ifdef ENABLE_FOLD_CHECKING
13608 #undef fold
13609
13610 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13611 static void fold_check_failed (const_tree, const_tree);
13612 void print_fold_checksum (const_tree);
13613
13614 /* When --enable-checking=fold, compute a digest of expr before
13615    and after actual fold call to see if fold did not accidentally
13616    change original expr.  */
13617
13618 tree
13619 fold (tree expr)
13620 {
13621   tree ret;
13622   struct md5_ctx ctx;
13623   unsigned char checksum_before[16], checksum_after[16];
13624   htab_t ht;
13625
13626   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13627   md5_init_ctx (&ctx);
13628   fold_checksum_tree (expr, &ctx, ht);
13629   md5_finish_ctx (&ctx, checksum_before);
13630   htab_empty (ht);
13631
13632   ret = fold_1 (expr);
13633
13634   md5_init_ctx (&ctx);
13635   fold_checksum_tree (expr, &ctx, ht);
13636   md5_finish_ctx (&ctx, checksum_after);
13637   htab_delete (ht);
13638
13639   if (memcmp (checksum_before, checksum_after, 16))
13640     fold_check_failed (expr, ret);
13641
13642   return ret;
13643 }
13644
13645 void
13646 print_fold_checksum (const_tree expr)
13647 {
13648   struct md5_ctx ctx;
13649   unsigned char checksum[16], cnt;
13650   htab_t ht;
13651
13652   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13653   md5_init_ctx (&ctx);
13654   fold_checksum_tree (expr, &ctx, ht);
13655   md5_finish_ctx (&ctx, checksum);
13656   htab_delete (ht);
13657   for (cnt = 0; cnt < 16; ++cnt)
13658     fprintf (stderr, "%02x", checksum[cnt]);
13659   putc ('\n', stderr);
13660 }
13661
13662 static void
13663 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13664 {
13665   internal_error ("fold check: original tree changed by fold");
13666 }
13667
13668 static void
13669 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13670 {
13671   const void **slot;
13672   enum tree_code code;
13673   union tree_node buf;
13674   int i, len;
13675   
13676 recursive_label:
13677
13678   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13679                <= sizeof (struct tree_function_decl))
13680               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13681   if (expr == NULL)
13682     return;
13683   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13684   if (*slot != NULL)
13685     return;
13686   *slot = expr;
13687   code = TREE_CODE (expr);
13688   if (TREE_CODE_CLASS (code) == tcc_declaration
13689       && DECL_ASSEMBLER_NAME_SET_P (expr))
13690     {
13691       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13692       memcpy ((char *) &buf, expr, tree_size (expr));
13693       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13694       expr = (tree) &buf;
13695     }
13696   else if (TREE_CODE_CLASS (code) == tcc_type
13697            && (TYPE_POINTER_TO (expr)
13698                || TYPE_REFERENCE_TO (expr)
13699                || TYPE_CACHED_VALUES_P (expr)
13700                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13701                || TYPE_NEXT_VARIANT (expr)))
13702     {
13703       /* Allow these fields to be modified.  */
13704       tree tmp;
13705       memcpy ((char *) &buf, expr, tree_size (expr));
13706       expr = tmp = (tree) &buf;
13707       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13708       TYPE_POINTER_TO (tmp) = NULL;
13709       TYPE_REFERENCE_TO (tmp) = NULL;
13710       TYPE_NEXT_VARIANT (tmp) = NULL;
13711       if (TYPE_CACHED_VALUES_P (tmp))
13712         {
13713           TYPE_CACHED_VALUES_P (tmp) = 0;
13714           TYPE_CACHED_VALUES (tmp) = NULL;
13715         }
13716     }
13717   md5_process_bytes (expr, tree_size (expr), ctx);
13718   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13719   if (TREE_CODE_CLASS (code) != tcc_type
13720       && TREE_CODE_CLASS (code) != tcc_declaration
13721       && code != TREE_LIST
13722       && code != SSA_NAME)
13723     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13724   switch (TREE_CODE_CLASS (code))
13725     {
13726     case tcc_constant:
13727       switch (code)
13728         {
13729         case STRING_CST:
13730           md5_process_bytes (TREE_STRING_POINTER (expr),
13731                              TREE_STRING_LENGTH (expr), ctx);
13732           break;
13733         case COMPLEX_CST:
13734           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13735           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13736           break;
13737         case VECTOR_CST:
13738           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13739           break;
13740         default:
13741           break;
13742         }
13743       break;
13744     case tcc_exceptional:
13745       switch (code)
13746         {
13747         case TREE_LIST:
13748           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13749           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13750           expr = TREE_CHAIN (expr);
13751           goto recursive_label;
13752           break;
13753         case TREE_VEC:
13754           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13755             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13756           break;
13757         default:
13758           break;
13759         }
13760       break;
13761     case tcc_expression:
13762     case tcc_reference:
13763     case tcc_comparison:
13764     case tcc_unary:
13765     case tcc_binary:
13766     case tcc_statement:
13767     case tcc_vl_exp:
13768       len = TREE_OPERAND_LENGTH (expr);
13769       for (i = 0; i < len; ++i)
13770         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13771       break;
13772     case tcc_declaration:
13773       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13774       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13775       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13776         {
13777           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13778           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13779           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13780           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13781           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13782         }
13783       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13784         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13785           
13786       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13787         {
13788           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13789           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13790           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13791         }
13792       break;
13793     case tcc_type:
13794       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13795         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13796       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13797       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13798       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13799       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13800       if (INTEGRAL_TYPE_P (expr)
13801           || SCALAR_FLOAT_TYPE_P (expr))
13802         {
13803           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13804           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13805         }
13806       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13807       if (TREE_CODE (expr) == RECORD_TYPE
13808           || TREE_CODE (expr) == UNION_TYPE
13809           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13810         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13811       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13812       break;
13813     default:
13814       break;
13815     }
13816 }
13817
13818 /* Helper function for outputting the checksum of a tree T.  When
13819    debugging with gdb, you can "define mynext" to be "next" followed
13820    by "call debug_fold_checksum (op0)", then just trace down till the
13821    outputs differ.  */
13822
13823 void
13824 debug_fold_checksum (const_tree t)
13825 {
13826   int i;
13827   unsigned char checksum[16];
13828   struct md5_ctx ctx;
13829   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13830   
13831   md5_init_ctx (&ctx);
13832   fold_checksum_tree (t, &ctx, ht);
13833   md5_finish_ctx (&ctx, checksum);
13834   htab_empty (ht);
13835
13836   for (i = 0; i < 16; i++)
13837     fprintf (stderr, "%d ", checksum[i]);
13838
13839   fprintf (stderr, "\n");
13840 }
13841
13842 #endif
13843
13844 /* Fold a unary tree expression with code CODE of type TYPE with an
13845    operand OP0.  Return a folded expression if successful.  Otherwise,
13846    return a tree expression with code CODE of type TYPE with an
13847    operand OP0.  */
13848
13849 tree
13850 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13851 {
13852   tree tem;
13853 #ifdef ENABLE_FOLD_CHECKING
13854   unsigned char checksum_before[16], checksum_after[16];
13855   struct md5_ctx ctx;
13856   htab_t ht;
13857
13858   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13859   md5_init_ctx (&ctx);
13860   fold_checksum_tree (op0, &ctx, ht);
13861   md5_finish_ctx (&ctx, checksum_before);
13862   htab_empty (ht);
13863 #endif
13864   
13865   tem = fold_unary (code, type, op0);
13866   if (!tem)
13867     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13868   
13869 #ifdef ENABLE_FOLD_CHECKING
13870   md5_init_ctx (&ctx);
13871   fold_checksum_tree (op0, &ctx, ht);
13872   md5_finish_ctx (&ctx, checksum_after);
13873   htab_delete (ht);
13874
13875   if (memcmp (checksum_before, checksum_after, 16))
13876     fold_check_failed (op0, tem);
13877 #endif
13878   return tem;
13879 }
13880
13881 /* Fold a binary tree expression with code CODE of type TYPE with
13882    operands OP0 and OP1.  Return a folded expression if successful.
13883    Otherwise, return a tree expression with code CODE of type TYPE
13884    with operands OP0 and OP1.  */
13885
13886 tree
13887 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13888                   MEM_STAT_DECL)
13889 {
13890   tree tem;
13891 #ifdef ENABLE_FOLD_CHECKING
13892   unsigned char checksum_before_op0[16],
13893                 checksum_before_op1[16],
13894                 checksum_after_op0[16],
13895                 checksum_after_op1[16];
13896   struct md5_ctx ctx;
13897   htab_t ht;
13898
13899   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13900   md5_init_ctx (&ctx);
13901   fold_checksum_tree (op0, &ctx, ht);
13902   md5_finish_ctx (&ctx, checksum_before_op0);
13903   htab_empty (ht);
13904
13905   md5_init_ctx (&ctx);
13906   fold_checksum_tree (op1, &ctx, ht);
13907   md5_finish_ctx (&ctx, checksum_before_op1);
13908   htab_empty (ht);
13909 #endif
13910
13911   tem = fold_binary (code, type, op0, op1);
13912   if (!tem)
13913     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13914   
13915 #ifdef ENABLE_FOLD_CHECKING
13916   md5_init_ctx (&ctx);
13917   fold_checksum_tree (op0, &ctx, ht);
13918   md5_finish_ctx (&ctx, checksum_after_op0);
13919   htab_empty (ht);
13920
13921   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13922     fold_check_failed (op0, tem);
13923   
13924   md5_init_ctx (&ctx);
13925   fold_checksum_tree (op1, &ctx, ht);
13926   md5_finish_ctx (&ctx, checksum_after_op1);
13927   htab_delete (ht);
13928
13929   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13930     fold_check_failed (op1, tem);
13931 #endif
13932   return tem;
13933 }
13934
13935 /* Fold a ternary tree expression with code CODE of type TYPE with
13936    operands OP0, OP1, and OP2.  Return a folded expression if
13937    successful.  Otherwise, return a tree expression with code CODE of
13938    type TYPE with operands OP0, OP1, and OP2.  */
13939
13940 tree
13941 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13942              MEM_STAT_DECL)
13943 {
13944   tree tem;
13945 #ifdef ENABLE_FOLD_CHECKING
13946   unsigned char checksum_before_op0[16],
13947                 checksum_before_op1[16],
13948                 checksum_before_op2[16],
13949                 checksum_after_op0[16],
13950                 checksum_after_op1[16],
13951                 checksum_after_op2[16];
13952   struct md5_ctx ctx;
13953   htab_t ht;
13954
13955   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13956   md5_init_ctx (&ctx);
13957   fold_checksum_tree (op0, &ctx, ht);
13958   md5_finish_ctx (&ctx, checksum_before_op0);
13959   htab_empty (ht);
13960
13961   md5_init_ctx (&ctx);
13962   fold_checksum_tree (op1, &ctx, ht);
13963   md5_finish_ctx (&ctx, checksum_before_op1);
13964   htab_empty (ht);
13965
13966   md5_init_ctx (&ctx);
13967   fold_checksum_tree (op2, &ctx, ht);
13968   md5_finish_ctx (&ctx, checksum_before_op2);
13969   htab_empty (ht);
13970 #endif
13971
13972   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13973   tem = fold_ternary (code, type, op0, op1, op2);
13974   if (!tem)
13975     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13976       
13977 #ifdef ENABLE_FOLD_CHECKING
13978   md5_init_ctx (&ctx);
13979   fold_checksum_tree (op0, &ctx, ht);
13980   md5_finish_ctx (&ctx, checksum_after_op0);
13981   htab_empty (ht);
13982
13983   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13984     fold_check_failed (op0, tem);
13985   
13986   md5_init_ctx (&ctx);
13987   fold_checksum_tree (op1, &ctx, ht);
13988   md5_finish_ctx (&ctx, checksum_after_op1);
13989   htab_empty (ht);
13990
13991   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13992     fold_check_failed (op1, tem);
13993   
13994   md5_init_ctx (&ctx);
13995   fold_checksum_tree (op2, &ctx, ht);
13996   md5_finish_ctx (&ctx, checksum_after_op2);
13997   htab_delete (ht);
13998
13999   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14000     fold_check_failed (op2, tem);
14001 #endif
14002   return tem;
14003 }
14004
14005 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14006    arguments in ARGARRAY, and a null static chain.
14007    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14008    of type TYPE from the given operands as constructed by build_call_array.  */
14009
14010 tree
14011 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14012 {
14013   tree tem;
14014 #ifdef ENABLE_FOLD_CHECKING
14015   unsigned char checksum_before_fn[16],
14016                 checksum_before_arglist[16],
14017                 checksum_after_fn[16],
14018                 checksum_after_arglist[16];
14019   struct md5_ctx ctx;
14020   htab_t ht;
14021   int i;
14022
14023   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14024   md5_init_ctx (&ctx);
14025   fold_checksum_tree (fn, &ctx, ht);
14026   md5_finish_ctx (&ctx, checksum_before_fn);
14027   htab_empty (ht);
14028
14029   md5_init_ctx (&ctx);
14030   for (i = 0; i < nargs; i++)
14031     fold_checksum_tree (argarray[i], &ctx, ht);
14032   md5_finish_ctx (&ctx, checksum_before_arglist);
14033   htab_empty (ht);
14034 #endif
14035
14036   tem = fold_builtin_call_array (type, fn, nargs, argarray);
14037       
14038 #ifdef ENABLE_FOLD_CHECKING
14039   md5_init_ctx (&ctx);
14040   fold_checksum_tree (fn, &ctx, ht);
14041   md5_finish_ctx (&ctx, checksum_after_fn);
14042   htab_empty (ht);
14043
14044   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14045     fold_check_failed (fn, tem);
14046   
14047   md5_init_ctx (&ctx);
14048   for (i = 0; i < nargs; i++)
14049     fold_checksum_tree (argarray[i], &ctx, ht);
14050   md5_finish_ctx (&ctx, checksum_after_arglist);
14051   htab_delete (ht);
14052
14053   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14054     fold_check_failed (NULL_TREE, tem);
14055 #endif
14056   return tem;
14057 }
14058
14059 /* Perform constant folding and related simplification of initializer
14060    expression EXPR.  These behave identically to "fold_buildN" but ignore
14061    potential run-time traps and exceptions that fold must preserve.  */
14062
14063 #define START_FOLD_INIT \
14064   int saved_signaling_nans = flag_signaling_nans;\
14065   int saved_trapping_math = flag_trapping_math;\
14066   int saved_rounding_math = flag_rounding_math;\
14067   int saved_trapv = flag_trapv;\
14068   int saved_folding_initializer = folding_initializer;\
14069   flag_signaling_nans = 0;\
14070   flag_trapping_math = 0;\
14071   flag_rounding_math = 0;\
14072   flag_trapv = 0;\
14073   folding_initializer = 1;
14074
14075 #define END_FOLD_INIT \
14076   flag_signaling_nans = saved_signaling_nans;\
14077   flag_trapping_math = saved_trapping_math;\
14078   flag_rounding_math = saved_rounding_math;\
14079   flag_trapv = saved_trapv;\
14080   folding_initializer = saved_folding_initializer;
14081
14082 tree
14083 fold_build1_initializer (enum tree_code code, tree type, tree op)
14084 {
14085   tree result;
14086   START_FOLD_INIT;
14087
14088   result = fold_build1 (code, type, op);
14089
14090   END_FOLD_INIT;
14091   return result;
14092 }
14093
14094 tree
14095 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14096 {
14097   tree result;
14098   START_FOLD_INIT;
14099
14100   result = fold_build2 (code, type, op0, op1);
14101
14102   END_FOLD_INIT;
14103   return result;
14104 }
14105
14106 tree
14107 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14108                          tree op2)
14109 {
14110   tree result;
14111   START_FOLD_INIT;
14112
14113   result = fold_build3 (code, type, op0, op1, op2);
14114
14115   END_FOLD_INIT;
14116   return result;
14117 }
14118
14119 tree
14120 fold_build_call_array_initializer (tree type, tree fn,
14121                                    int nargs, tree *argarray)
14122 {
14123   tree result;
14124   START_FOLD_INIT;
14125
14126   result = fold_build_call_array (type, fn, nargs, argarray);
14127
14128   END_FOLD_INIT;
14129   return result;
14130 }
14131
14132 #undef START_FOLD_INIT
14133 #undef END_FOLD_INIT
14134
14135 /* Determine if first argument is a multiple of second argument.  Return 0 if
14136    it is not, or we cannot easily determined it to be.
14137
14138    An example of the sort of thing we care about (at this point; this routine
14139    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14140    fold cases do now) is discovering that
14141
14142      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14143
14144    is a multiple of
14145
14146      SAVE_EXPR (J * 8)
14147
14148    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14149
14150    This code also handles discovering that
14151
14152      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14153
14154    is a multiple of 8 so we don't have to worry about dealing with a
14155    possible remainder.
14156
14157    Note that we *look* inside a SAVE_EXPR only to determine how it was
14158    calculated; it is not safe for fold to do much of anything else with the
14159    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14160    at run time.  For example, the latter example above *cannot* be implemented
14161    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14162    evaluation time of the original SAVE_EXPR is not necessarily the same at
14163    the time the new expression is evaluated.  The only optimization of this
14164    sort that would be valid is changing
14165
14166      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14167
14168    divided by 8 to
14169
14170      SAVE_EXPR (I) * SAVE_EXPR (J)
14171
14172    (where the same SAVE_EXPR (J) is used in the original and the
14173    transformed version).  */
14174
14175 int
14176 multiple_of_p (tree type, const_tree top, const_tree bottom)
14177 {
14178   if (operand_equal_p (top, bottom, 0))
14179     return 1;
14180
14181   if (TREE_CODE (type) != INTEGER_TYPE)
14182     return 0;
14183
14184   switch (TREE_CODE (top))
14185     {
14186     case BIT_AND_EXPR:
14187       /* Bitwise and provides a power of two multiple.  If the mask is
14188          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14189       if (!integer_pow2p (bottom))
14190         return 0;
14191       /* FALLTHRU */
14192
14193     case MULT_EXPR:
14194       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14195               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14196
14197     case PLUS_EXPR:
14198     case MINUS_EXPR:
14199       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14200               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14201
14202     case LSHIFT_EXPR:
14203       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14204         {
14205           tree op1, t1;
14206
14207           op1 = TREE_OPERAND (top, 1);
14208           /* const_binop may not detect overflow correctly,
14209              so check for it explicitly here.  */
14210           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14211               > TREE_INT_CST_LOW (op1)
14212               && TREE_INT_CST_HIGH (op1) == 0
14213               && 0 != (t1 = fold_convert (type,
14214                                           const_binop (LSHIFT_EXPR,
14215                                                        size_one_node,
14216                                                        op1, 0)))
14217               && !TREE_OVERFLOW (t1))
14218             return multiple_of_p (type, t1, bottom);
14219         }
14220       return 0;
14221
14222     case NOP_EXPR:
14223       /* Can't handle conversions from non-integral or wider integral type.  */
14224       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14225           || (TYPE_PRECISION (type)
14226               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14227         return 0;
14228
14229       /* .. fall through ...  */
14230
14231     case SAVE_EXPR:
14232       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14233
14234     case INTEGER_CST:
14235       if (TREE_CODE (bottom) != INTEGER_CST
14236           || integer_zerop (bottom)
14237           || (TYPE_UNSIGNED (type)
14238               && (tree_int_cst_sgn (top) < 0
14239                   || tree_int_cst_sgn (bottom) < 0)))
14240         return 0;
14241       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14242                                              top, bottom, 0));
14243
14244     default:
14245       return 0;
14246     }
14247 }
14248
14249 /* Return true if CODE or TYPE is known to be non-negative. */
14250
14251 static bool
14252 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14253 {
14254   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14255       && truth_value_p (code))
14256     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14257        have a signed:1 type (where the value is -1 and 0).  */
14258     return true;
14259   return false;
14260 }
14261
14262 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14263    value is based on the assumption that signed overflow is undefined,
14264    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14265    *STRICT_OVERFLOW_P.  */
14266
14267 bool
14268 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14269                                 bool *strict_overflow_p)
14270 {
14271   if (TYPE_UNSIGNED (type))
14272     return true;
14273
14274   switch (code)
14275     {
14276     case ABS_EXPR:
14277       /* We can't return 1 if flag_wrapv is set because
14278          ABS_EXPR<INT_MIN> = INT_MIN.  */
14279       if (!INTEGRAL_TYPE_P (type))
14280         return true;
14281       if (TYPE_OVERFLOW_UNDEFINED (type))
14282         {
14283           *strict_overflow_p = true;
14284           return true;
14285         }
14286       break;
14287
14288     case NON_LVALUE_EXPR:
14289     case FLOAT_EXPR:
14290     case FIX_TRUNC_EXPR:
14291       return tree_expr_nonnegative_warnv_p (op0,
14292                                             strict_overflow_p);
14293
14294     case NOP_EXPR:
14295       {
14296         tree inner_type = TREE_TYPE (op0);
14297         tree outer_type = type;
14298
14299         if (TREE_CODE (outer_type) == REAL_TYPE)
14300           {
14301             if (TREE_CODE (inner_type) == REAL_TYPE)
14302               return tree_expr_nonnegative_warnv_p (op0,
14303                                                     strict_overflow_p);
14304             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14305               {
14306                 if (TYPE_UNSIGNED (inner_type))
14307                   return true;
14308                 return tree_expr_nonnegative_warnv_p (op0,
14309                                                       strict_overflow_p);
14310               }
14311           }
14312         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14313           {
14314             if (TREE_CODE (inner_type) == REAL_TYPE)
14315               return tree_expr_nonnegative_warnv_p (op0,
14316                                                     strict_overflow_p);
14317             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14318               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14319                       && TYPE_UNSIGNED (inner_type);
14320           }
14321       }
14322       break;
14323
14324     default:
14325       return tree_simple_nonnegative_warnv_p (code, type);
14326     }
14327
14328   /* We don't know sign of `t', so be conservative and return false.  */
14329   return false;
14330 }
14331
14332 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14333    value is based on the assumption that signed overflow is undefined,
14334    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14335    *STRICT_OVERFLOW_P.  */
14336
14337 bool
14338 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14339                                       tree op1, bool *strict_overflow_p)
14340 {
14341   if (TYPE_UNSIGNED (type))
14342     return true;
14343
14344   switch (code)
14345     {
14346     case POINTER_PLUS_EXPR:
14347     case PLUS_EXPR:
14348       if (FLOAT_TYPE_P (type))
14349         return (tree_expr_nonnegative_warnv_p (op0,
14350                                                strict_overflow_p)
14351                 && tree_expr_nonnegative_warnv_p (op1,
14352                                                   strict_overflow_p));
14353
14354       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14355          both unsigned and at least 2 bits shorter than the result.  */
14356       if (TREE_CODE (type) == INTEGER_TYPE
14357           && TREE_CODE (op0) == NOP_EXPR
14358           && TREE_CODE (op1) == NOP_EXPR)
14359         {
14360           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14361           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14362           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14363               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14364             {
14365               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14366                                        TYPE_PRECISION (inner2)) + 1;
14367               return prec < TYPE_PRECISION (type);
14368             }
14369         }
14370       break;
14371
14372     case MULT_EXPR:
14373       if (FLOAT_TYPE_P (type))
14374         {
14375           /* x * x for floating point x is always non-negative.  */
14376           if (operand_equal_p (op0, op1, 0))
14377             return true;
14378           return (tree_expr_nonnegative_warnv_p (op0,
14379                                                  strict_overflow_p)
14380                   && tree_expr_nonnegative_warnv_p (op1,
14381                                                     strict_overflow_p));
14382         }
14383
14384       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14385          both unsigned and their total bits is shorter than the result.  */
14386       if (TREE_CODE (type) == INTEGER_TYPE
14387           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14388           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14389         {
14390           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14391             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14392             : TREE_TYPE (op0);
14393           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14394             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14395             : TREE_TYPE (op1);
14396
14397           bool unsigned0 = TYPE_UNSIGNED (inner0);
14398           bool unsigned1 = TYPE_UNSIGNED (inner1);
14399
14400           if (TREE_CODE (op0) == INTEGER_CST)
14401             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14402
14403           if (TREE_CODE (op1) == INTEGER_CST)
14404             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14405
14406           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14407               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14408             {
14409               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14410                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14411                 : TYPE_PRECISION (inner0);
14412
14413               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14414                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14415                 : TYPE_PRECISION (inner1);
14416
14417               return precision0 + precision1 < TYPE_PRECISION (type);
14418             }
14419         }
14420       return false;
14421
14422     case BIT_AND_EXPR:
14423     case MAX_EXPR:
14424       return (tree_expr_nonnegative_warnv_p (op0,
14425                                              strict_overflow_p)
14426               || tree_expr_nonnegative_warnv_p (op1,
14427                                                 strict_overflow_p));
14428
14429     case BIT_IOR_EXPR:
14430     case BIT_XOR_EXPR:
14431     case MIN_EXPR:
14432     case RDIV_EXPR:
14433     case TRUNC_DIV_EXPR:
14434     case CEIL_DIV_EXPR:
14435     case FLOOR_DIV_EXPR:
14436     case ROUND_DIV_EXPR:
14437       return (tree_expr_nonnegative_warnv_p (op0,
14438                                              strict_overflow_p)
14439               && tree_expr_nonnegative_warnv_p (op1,
14440                                                 strict_overflow_p));
14441
14442     case TRUNC_MOD_EXPR:
14443     case CEIL_MOD_EXPR:
14444     case FLOOR_MOD_EXPR:
14445     case ROUND_MOD_EXPR:
14446       return tree_expr_nonnegative_warnv_p (op0,
14447                                             strict_overflow_p);
14448     default:
14449       return tree_simple_nonnegative_warnv_p (code, type);
14450     }
14451
14452   /* We don't know sign of `t', so be conservative and return false.  */
14453   return false;
14454 }
14455
14456 /* Return true if T is known to be non-negative.  If the return
14457    value is based on the assumption that signed overflow is undefined,
14458    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14459    *STRICT_OVERFLOW_P.  */
14460
14461 bool
14462 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14463 {
14464   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14465     return true;
14466
14467   switch (TREE_CODE (t))
14468     {
14469     case INTEGER_CST:
14470       return tree_int_cst_sgn (t) >= 0;
14471
14472     case REAL_CST:
14473       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14474
14475     case FIXED_CST:
14476       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14477
14478     case COND_EXPR:
14479       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14480                                              strict_overflow_p)
14481               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14482                                                 strict_overflow_p));
14483     default:
14484       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14485                                                    TREE_TYPE (t));
14486     }
14487   /* We don't know sign of `t', so be conservative and return false.  */
14488   return false;
14489 }
14490
14491 /* Return true if T is known to be non-negative.  If the return
14492    value is based on the assumption that signed overflow is undefined,
14493    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14494    *STRICT_OVERFLOW_P.  */
14495
14496 bool
14497 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14498                                tree arg0, tree arg1, bool *strict_overflow_p)
14499 {
14500   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14501     switch (DECL_FUNCTION_CODE (fndecl))
14502       {
14503         CASE_FLT_FN (BUILT_IN_ACOS):
14504         CASE_FLT_FN (BUILT_IN_ACOSH):
14505         CASE_FLT_FN (BUILT_IN_CABS):
14506         CASE_FLT_FN (BUILT_IN_COSH):
14507         CASE_FLT_FN (BUILT_IN_ERFC):
14508         CASE_FLT_FN (BUILT_IN_EXP):
14509         CASE_FLT_FN (BUILT_IN_EXP10):
14510         CASE_FLT_FN (BUILT_IN_EXP2):
14511         CASE_FLT_FN (BUILT_IN_FABS):
14512         CASE_FLT_FN (BUILT_IN_FDIM):
14513         CASE_FLT_FN (BUILT_IN_HYPOT):
14514         CASE_FLT_FN (BUILT_IN_POW10):
14515         CASE_INT_FN (BUILT_IN_FFS):
14516         CASE_INT_FN (BUILT_IN_PARITY):
14517         CASE_INT_FN (BUILT_IN_POPCOUNT):
14518       case BUILT_IN_BSWAP32:
14519       case BUILT_IN_BSWAP64:
14520         /* Always true.  */
14521         return true;
14522
14523         CASE_FLT_FN (BUILT_IN_SQRT):
14524         /* sqrt(-0.0) is -0.0.  */
14525         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14526           return true;
14527         return tree_expr_nonnegative_warnv_p (arg0,
14528                                               strict_overflow_p);
14529
14530         CASE_FLT_FN (BUILT_IN_ASINH):
14531         CASE_FLT_FN (BUILT_IN_ATAN):
14532         CASE_FLT_FN (BUILT_IN_ATANH):
14533         CASE_FLT_FN (BUILT_IN_CBRT):
14534         CASE_FLT_FN (BUILT_IN_CEIL):
14535         CASE_FLT_FN (BUILT_IN_ERF):
14536         CASE_FLT_FN (BUILT_IN_EXPM1):
14537         CASE_FLT_FN (BUILT_IN_FLOOR):
14538         CASE_FLT_FN (BUILT_IN_FMOD):
14539         CASE_FLT_FN (BUILT_IN_FREXP):
14540         CASE_FLT_FN (BUILT_IN_LCEIL):
14541         CASE_FLT_FN (BUILT_IN_LDEXP):
14542         CASE_FLT_FN (BUILT_IN_LFLOOR):
14543         CASE_FLT_FN (BUILT_IN_LLCEIL):
14544         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14545         CASE_FLT_FN (BUILT_IN_LLRINT):
14546         CASE_FLT_FN (BUILT_IN_LLROUND):
14547         CASE_FLT_FN (BUILT_IN_LRINT):
14548         CASE_FLT_FN (BUILT_IN_LROUND):
14549         CASE_FLT_FN (BUILT_IN_MODF):
14550         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14551         CASE_FLT_FN (BUILT_IN_RINT):
14552         CASE_FLT_FN (BUILT_IN_ROUND):
14553         CASE_FLT_FN (BUILT_IN_SCALB):
14554         CASE_FLT_FN (BUILT_IN_SCALBLN):
14555         CASE_FLT_FN (BUILT_IN_SCALBN):
14556         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14557         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14558         CASE_FLT_FN (BUILT_IN_SINH):
14559         CASE_FLT_FN (BUILT_IN_TANH):
14560         CASE_FLT_FN (BUILT_IN_TRUNC):
14561         /* True if the 1st argument is nonnegative.  */
14562         return tree_expr_nonnegative_warnv_p (arg0,
14563                                               strict_overflow_p);
14564
14565         CASE_FLT_FN (BUILT_IN_FMAX):
14566         /* True if the 1st OR 2nd arguments are nonnegative.  */
14567         return (tree_expr_nonnegative_warnv_p (arg0,
14568                                                strict_overflow_p)
14569                 || (tree_expr_nonnegative_warnv_p (arg1,
14570                                                    strict_overflow_p)));
14571
14572         CASE_FLT_FN (BUILT_IN_FMIN):
14573         /* True if the 1st AND 2nd arguments are nonnegative.  */
14574         return (tree_expr_nonnegative_warnv_p (arg0,
14575                                                strict_overflow_p)
14576                 && (tree_expr_nonnegative_warnv_p (arg1,
14577                                                    strict_overflow_p)));
14578
14579         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14580         /* True if the 2nd argument is nonnegative.  */
14581         return tree_expr_nonnegative_warnv_p (arg1,
14582                                               strict_overflow_p);
14583
14584         CASE_FLT_FN (BUILT_IN_POWI):
14585         /* True if the 1st argument is nonnegative or the second
14586            argument is an even integer.  */
14587         if (TREE_CODE (arg1) == INTEGER_CST
14588             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14589           return true;
14590         return tree_expr_nonnegative_warnv_p (arg0,
14591                                               strict_overflow_p);
14592
14593         CASE_FLT_FN (BUILT_IN_POW):
14594         /* True if the 1st argument is nonnegative or the second
14595            argument is an even integer valued real.  */
14596         if (TREE_CODE (arg1) == REAL_CST)
14597           {
14598             REAL_VALUE_TYPE c;
14599             HOST_WIDE_INT n;
14600
14601             c = TREE_REAL_CST (arg1);
14602             n = real_to_integer (&c);
14603             if ((n & 1) == 0)
14604               {
14605                 REAL_VALUE_TYPE cint;
14606                 real_from_integer (&cint, VOIDmode, n,
14607                                    n < 0 ? -1 : 0, 0);
14608                 if (real_identical (&c, &cint))
14609                   return true;
14610               }
14611           }
14612         return tree_expr_nonnegative_warnv_p (arg0,
14613                                               strict_overflow_p);
14614
14615       default:
14616         break;
14617       }
14618   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14619                                           type);
14620 }
14621
14622 /* Return true if T is known to be non-negative.  If the return
14623    value is based on the assumption that signed overflow is undefined,
14624    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14625    *STRICT_OVERFLOW_P.  */
14626
14627 bool
14628 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14629 {
14630   enum tree_code code = TREE_CODE (t);
14631   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14632     return true;
14633
14634   switch (code)
14635     {
14636     case TARGET_EXPR:
14637       {
14638         tree temp = TARGET_EXPR_SLOT (t);
14639         t = TARGET_EXPR_INITIAL (t);
14640
14641         /* If the initializer is non-void, then it's a normal expression
14642            that will be assigned to the slot.  */
14643         if (!VOID_TYPE_P (t))
14644           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14645
14646         /* Otherwise, the initializer sets the slot in some way.  One common
14647            way is an assignment statement at the end of the initializer.  */
14648         while (1)
14649           {
14650             if (TREE_CODE (t) == BIND_EXPR)
14651               t = expr_last (BIND_EXPR_BODY (t));
14652             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14653                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14654               t = expr_last (TREE_OPERAND (t, 0));
14655             else if (TREE_CODE (t) == STATEMENT_LIST)
14656               t = expr_last (t);
14657             else
14658               break;
14659           }
14660         if (TREE_CODE (t) == MODIFY_EXPR
14661             && TREE_OPERAND (t, 0) == temp)
14662           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14663                                                 strict_overflow_p);
14664
14665         return false;
14666       }
14667
14668     case CALL_EXPR:
14669       {
14670         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14671         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14672
14673         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14674                                               get_callee_fndecl (t),
14675                                               arg0,
14676                                               arg1,
14677                                               strict_overflow_p);
14678       }
14679     case COMPOUND_EXPR:
14680     case MODIFY_EXPR:
14681       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14682                                             strict_overflow_p);
14683     case BIND_EXPR:
14684       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14685                                             strict_overflow_p);
14686     case SAVE_EXPR:
14687       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14688                                             strict_overflow_p);
14689
14690     default:
14691       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14692                                                    TREE_TYPE (t));
14693     }
14694
14695   /* We don't know sign of `t', so be conservative and return false.  */
14696   return false;
14697 }
14698
14699 /* Return true if T is known to be non-negative.  If the return
14700    value is based on the assumption that signed overflow is undefined,
14701    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14702    *STRICT_OVERFLOW_P.  */
14703
14704 bool
14705 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14706 {
14707   enum tree_code code;
14708   if (t == error_mark_node)
14709     return false;
14710
14711   code = TREE_CODE (t);
14712   switch (TREE_CODE_CLASS (code))
14713     {
14714     case tcc_binary:
14715     case tcc_comparison:
14716       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14717                                               TREE_TYPE (t),
14718                                               TREE_OPERAND (t, 0),
14719                                               TREE_OPERAND (t, 1),
14720                                               strict_overflow_p);
14721
14722     case tcc_unary:
14723       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14724                                              TREE_TYPE (t),
14725                                              TREE_OPERAND (t, 0),
14726                                              strict_overflow_p);
14727
14728     case tcc_constant:
14729     case tcc_declaration:
14730     case tcc_reference:
14731       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14732
14733     default:
14734       break;
14735     }
14736
14737   switch (code)
14738     {
14739     case TRUTH_AND_EXPR:
14740     case TRUTH_OR_EXPR:
14741     case TRUTH_XOR_EXPR:
14742       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14743                                               TREE_TYPE (t),
14744                                               TREE_OPERAND (t, 0),
14745                                               TREE_OPERAND (t, 1),
14746                                               strict_overflow_p);
14747     case TRUTH_NOT_EXPR:
14748       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14749                                              TREE_TYPE (t),
14750                                              TREE_OPERAND (t, 0),
14751                                              strict_overflow_p);
14752
14753     case COND_EXPR:
14754     case CONSTRUCTOR:
14755     case OBJ_TYPE_REF:
14756     case ASSERT_EXPR:
14757     case ADDR_EXPR:
14758     case WITH_SIZE_EXPR:
14759     case EXC_PTR_EXPR:
14760     case SSA_NAME:
14761     case FILTER_EXPR:
14762       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14763
14764     default:
14765       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14766     }
14767 }
14768
14769 /* Return true if `t' is known to be non-negative.  Handle warnings
14770    about undefined signed overflow.  */
14771
14772 bool
14773 tree_expr_nonnegative_p (tree t)
14774 {
14775   bool ret, strict_overflow_p;
14776
14777   strict_overflow_p = false;
14778   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14779   if (strict_overflow_p)
14780     fold_overflow_warning (("assuming signed overflow does not occur when "
14781                             "determining that expression is always "
14782                             "non-negative"),
14783                            WARN_STRICT_OVERFLOW_MISC);
14784   return ret;
14785 }
14786
14787
14788 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14789    For floating point we further ensure that T is not denormal.
14790    Similar logic is present in nonzero_address in rtlanal.h.
14791
14792    If the return value is based on the assumption that signed overflow
14793    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14794    change *STRICT_OVERFLOW_P.  */
14795
14796 bool
14797 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14798                                  bool *strict_overflow_p)
14799 {
14800   switch (code)
14801     {
14802     case ABS_EXPR:
14803       return tree_expr_nonzero_warnv_p (op0,
14804                                         strict_overflow_p);
14805
14806     case NOP_EXPR:
14807       {
14808         tree inner_type = TREE_TYPE (op0);
14809         tree outer_type = type;
14810
14811         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14812                 && tree_expr_nonzero_warnv_p (op0,
14813                                               strict_overflow_p));
14814       }
14815       break;
14816
14817     case NON_LVALUE_EXPR:
14818       return tree_expr_nonzero_warnv_p (op0,
14819                                         strict_overflow_p);
14820
14821     default:
14822       break;
14823   }
14824
14825   return false;
14826 }
14827
14828 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14829    For floating point we further ensure that T is not denormal.
14830    Similar logic is present in nonzero_address in rtlanal.h.
14831
14832    If the return value is based on the assumption that signed overflow
14833    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14834    change *STRICT_OVERFLOW_P.  */
14835
14836 bool
14837 tree_binary_nonzero_warnv_p (enum tree_code code,
14838                              tree type,
14839                              tree op0,
14840                              tree op1, bool *strict_overflow_p)
14841 {
14842   bool sub_strict_overflow_p;
14843   switch (code)
14844     {
14845     case POINTER_PLUS_EXPR:
14846     case PLUS_EXPR:
14847       if (TYPE_OVERFLOW_UNDEFINED (type))
14848         {
14849           /* With the presence of negative values it is hard
14850              to say something.  */
14851           sub_strict_overflow_p = false;
14852           if (!tree_expr_nonnegative_warnv_p (op0,
14853                                               &sub_strict_overflow_p)
14854               || !tree_expr_nonnegative_warnv_p (op1,
14855                                                  &sub_strict_overflow_p))
14856             return false;
14857           /* One of operands must be positive and the other non-negative.  */
14858           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14859              overflows, on a twos-complement machine the sum of two
14860              nonnegative numbers can never be zero.  */
14861           return (tree_expr_nonzero_warnv_p (op0,
14862                                              strict_overflow_p)
14863                   || tree_expr_nonzero_warnv_p (op1,
14864                                                 strict_overflow_p));
14865         }
14866       break;
14867
14868     case MULT_EXPR:
14869       if (TYPE_OVERFLOW_UNDEFINED (type))
14870         {
14871           if (tree_expr_nonzero_warnv_p (op0,
14872                                          strict_overflow_p)
14873               && tree_expr_nonzero_warnv_p (op1,
14874                                             strict_overflow_p))
14875             {
14876               *strict_overflow_p = true;
14877               return true;
14878             }
14879         }
14880       break;
14881
14882     case MIN_EXPR:
14883       sub_strict_overflow_p = false;
14884       if (tree_expr_nonzero_warnv_p (op0,
14885                                      &sub_strict_overflow_p)
14886           && tree_expr_nonzero_warnv_p (op1,
14887                                         &sub_strict_overflow_p))
14888         {
14889           if (sub_strict_overflow_p)
14890             *strict_overflow_p = true;
14891         }
14892       break;
14893
14894     case MAX_EXPR:
14895       sub_strict_overflow_p = false;
14896       if (tree_expr_nonzero_warnv_p (op0,
14897                                      &sub_strict_overflow_p))
14898         {
14899           if (sub_strict_overflow_p)
14900             *strict_overflow_p = true;
14901
14902           /* When both operands are nonzero, then MAX must be too.  */
14903           if (tree_expr_nonzero_warnv_p (op1,
14904                                          strict_overflow_p))
14905             return true;
14906
14907           /* MAX where operand 0 is positive is positive.  */
14908           return tree_expr_nonnegative_warnv_p (op0,
14909                                                strict_overflow_p);
14910         }
14911       /* MAX where operand 1 is positive is positive.  */
14912       else if (tree_expr_nonzero_warnv_p (op1,
14913                                           &sub_strict_overflow_p)
14914                && tree_expr_nonnegative_warnv_p (op1,
14915                                                  &sub_strict_overflow_p))
14916         {
14917           if (sub_strict_overflow_p)
14918             *strict_overflow_p = true;
14919           return true;
14920         }
14921       break;
14922
14923     case BIT_IOR_EXPR:
14924       return (tree_expr_nonzero_warnv_p (op1,
14925                                          strict_overflow_p)
14926               || tree_expr_nonzero_warnv_p (op0,
14927                                             strict_overflow_p));
14928
14929     default:
14930       break;
14931   }
14932
14933   return false;
14934 }
14935
14936 /* Return true when T is an address and is known to be nonzero.
14937    For floating point we further ensure that T is not denormal.
14938    Similar logic is present in nonzero_address in rtlanal.h.
14939
14940    If the return value is based on the assumption that signed overflow
14941    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14942    change *STRICT_OVERFLOW_P.  */
14943
14944 bool
14945 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14946 {
14947   bool sub_strict_overflow_p;
14948   switch (TREE_CODE (t))
14949     {
14950     case INTEGER_CST:
14951       return !integer_zerop (t);
14952
14953     case ADDR_EXPR:
14954       {
14955         tree base = get_base_address (TREE_OPERAND (t, 0));
14956
14957         if (!base)
14958           return false;
14959
14960         /* Weak declarations may link to NULL.  */
14961         if (VAR_OR_FUNCTION_DECL_P (base))
14962           return !DECL_WEAK (base);
14963
14964         /* Constants are never weak.  */
14965         if (CONSTANT_CLASS_P (base))
14966           return true;
14967
14968         return false;
14969       }
14970
14971     case COND_EXPR:
14972       sub_strict_overflow_p = false;
14973       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14974                                      &sub_strict_overflow_p)
14975           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14976                                         &sub_strict_overflow_p))
14977         {
14978           if (sub_strict_overflow_p)
14979             *strict_overflow_p = true;
14980           return true;
14981         }
14982       break;
14983
14984     default:
14985       break;
14986     }
14987   return false;
14988 }
14989
14990 /* Return true when T is an address and is known to be nonzero.
14991    For floating point we further ensure that T is not denormal.
14992    Similar logic is present in nonzero_address in rtlanal.h.
14993
14994    If the return value is based on the assumption that signed overflow
14995    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14996    change *STRICT_OVERFLOW_P.  */
14997
14998 bool
14999 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15000 {
15001   tree type = TREE_TYPE (t);
15002   enum tree_code code;
15003
15004   /* Doing something useful for floating point would need more work.  */
15005   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15006     return false;
15007
15008   code = TREE_CODE (t);
15009   switch (TREE_CODE_CLASS (code))
15010     {
15011     case tcc_unary:
15012       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15013                                               strict_overflow_p);
15014     case tcc_binary:
15015     case tcc_comparison:
15016       return tree_binary_nonzero_warnv_p (code, type,
15017                                                TREE_OPERAND (t, 0),
15018                                                TREE_OPERAND (t, 1),
15019                                                strict_overflow_p);
15020     case tcc_constant:
15021     case tcc_declaration:
15022     case tcc_reference:
15023       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15024
15025     default:
15026       break;
15027     }
15028
15029   switch (code)
15030     {
15031     case TRUTH_NOT_EXPR:
15032       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15033                                               strict_overflow_p);
15034
15035     case TRUTH_AND_EXPR:
15036     case TRUTH_OR_EXPR:
15037     case TRUTH_XOR_EXPR:
15038       return tree_binary_nonzero_warnv_p (code, type,
15039                                                TREE_OPERAND (t, 0),
15040                                                TREE_OPERAND (t, 1),
15041                                                strict_overflow_p);
15042
15043     case COND_EXPR:
15044     case CONSTRUCTOR:
15045     case OBJ_TYPE_REF:
15046     case ASSERT_EXPR:
15047     case ADDR_EXPR:
15048     case WITH_SIZE_EXPR:
15049     case EXC_PTR_EXPR:
15050     case SSA_NAME:
15051     case FILTER_EXPR:
15052       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15053
15054     case COMPOUND_EXPR:
15055     case MODIFY_EXPR:
15056     case BIND_EXPR:
15057       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15058                                         strict_overflow_p);
15059
15060     case SAVE_EXPR:
15061       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15062                                         strict_overflow_p);
15063
15064     case CALL_EXPR:
15065       return alloca_call_p (t);
15066
15067     default:
15068       break;
15069     }
15070   return false;
15071 }
15072
15073 /* Return true when T is an address and is known to be nonzero.
15074    Handle warnings about undefined signed overflow.  */
15075
15076 bool
15077 tree_expr_nonzero_p (tree t)
15078 {
15079   bool ret, strict_overflow_p;
15080
15081   strict_overflow_p = false;
15082   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15083   if (strict_overflow_p)
15084     fold_overflow_warning (("assuming signed overflow does not occur when "
15085                             "determining that expression is always "
15086                             "non-zero"),
15087                            WARN_STRICT_OVERFLOW_MISC);
15088   return ret;
15089 }
15090
15091 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15092    attempt to fold the expression to a constant without modifying TYPE,
15093    OP0 or OP1.
15094
15095    If the expression could be simplified to a constant, then return
15096    the constant.  If the expression would not be simplified to a
15097    constant, then return NULL_TREE.  */
15098
15099 tree
15100 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15101 {
15102   tree tem = fold_binary (code, type, op0, op1);
15103   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15104 }
15105
15106 /* Given the components of a unary expression CODE, TYPE and OP0,
15107    attempt to fold the expression to a constant without modifying
15108    TYPE or OP0.
15109
15110    If the expression could be simplified to a constant, then return
15111    the constant.  If the expression would not be simplified to a
15112    constant, then return NULL_TREE.  */
15113
15114 tree
15115 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15116 {
15117   tree tem = fold_unary (code, type, op0);
15118   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15119 }
15120
15121 /* If EXP represents referencing an element in a constant string
15122    (either via pointer arithmetic or array indexing), return the
15123    tree representing the value accessed, otherwise return NULL.  */
15124
15125 tree
15126 fold_read_from_constant_string (tree exp)
15127 {
15128   if ((TREE_CODE (exp) == INDIRECT_REF
15129        || TREE_CODE (exp) == ARRAY_REF)
15130       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15131     {
15132       tree exp1 = TREE_OPERAND (exp, 0);
15133       tree index;
15134       tree string;
15135
15136       if (TREE_CODE (exp) == INDIRECT_REF)
15137         string = string_constant (exp1, &index);
15138       else
15139         {
15140           tree low_bound = array_ref_low_bound (exp);
15141           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15142
15143           /* Optimize the special-case of a zero lower bound.
15144
15145              We convert the low_bound to sizetype to avoid some problems
15146              with constant folding.  (E.g. suppose the lower bound is 1,
15147              and its mode is QI.  Without the conversion,l (ARRAY
15148              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15149              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15150           if (! integer_zerop (low_bound))
15151             index = size_diffop (index, fold_convert (sizetype, low_bound));
15152
15153           string = exp1;
15154         }
15155
15156       if (string
15157           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15158           && TREE_CODE (string) == STRING_CST
15159           && TREE_CODE (index) == INTEGER_CST
15160           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15161           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15162               == MODE_INT)
15163           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15164         return build_int_cst_type (TREE_TYPE (exp),
15165                                    (TREE_STRING_POINTER (string)
15166                                     [TREE_INT_CST_LOW (index)]));
15167     }
15168   return NULL;
15169 }
15170
15171 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15172    an integer constant, real, or fixed-point constant.
15173
15174    TYPE is the type of the result.  */
15175
15176 static tree
15177 fold_negate_const (tree arg0, tree type)
15178 {
15179   tree t = NULL_TREE;
15180
15181   switch (TREE_CODE (arg0))
15182     {
15183     case INTEGER_CST:
15184       {
15185         unsigned HOST_WIDE_INT low;
15186         HOST_WIDE_INT high;
15187         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15188                                    TREE_INT_CST_HIGH (arg0),
15189                                    &low, &high);
15190         t = force_fit_type_double (type, low, high, 1,
15191                                    (overflow | TREE_OVERFLOW (arg0))
15192                                    && !TYPE_UNSIGNED (type));
15193         break;
15194       }
15195
15196     case REAL_CST:
15197       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15198       break;
15199
15200     case FIXED_CST:
15201       {
15202         FIXED_VALUE_TYPE f;
15203         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15204                                             &(TREE_FIXED_CST (arg0)), NULL,
15205                                             TYPE_SATURATING (type));
15206         t = build_fixed (type, f);
15207         /* Propagate overflow flags.  */
15208         if (overflow_p | TREE_OVERFLOW (arg0))
15209           TREE_OVERFLOW (t) = 1;
15210         break;
15211       }
15212
15213     default:
15214       gcc_unreachable ();
15215     }
15216
15217   return t;
15218 }
15219
15220 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15221    an integer constant or real constant.
15222
15223    TYPE is the type of the result.  */
15224
15225 tree
15226 fold_abs_const (tree arg0, tree type)
15227 {
15228   tree t = NULL_TREE;
15229
15230   switch (TREE_CODE (arg0))
15231     {
15232     case INTEGER_CST:
15233       /* If the value is unsigned, then the absolute value is
15234          the same as the ordinary value.  */
15235       if (TYPE_UNSIGNED (type))
15236         t = arg0;
15237       /* Similarly, if the value is non-negative.  */
15238       else if (INT_CST_LT (integer_minus_one_node, arg0))
15239         t = arg0;
15240       /* If the value is negative, then the absolute value is
15241          its negation.  */
15242       else
15243         {
15244           unsigned HOST_WIDE_INT low;
15245           HOST_WIDE_INT high;
15246           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15247                                      TREE_INT_CST_HIGH (arg0),
15248                                      &low, &high);
15249           t = force_fit_type_double (type, low, high, -1,
15250                                      overflow | TREE_OVERFLOW (arg0));
15251         }
15252       break;
15253
15254     case REAL_CST:
15255       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15256         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15257       else
15258         t =  arg0;
15259       break;
15260
15261     default:
15262       gcc_unreachable ();
15263     }
15264
15265   return t;
15266 }
15267
15268 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15269    constant.  TYPE is the type of the result.  */
15270
15271 static tree
15272 fold_not_const (tree arg0, tree type)
15273 {
15274   tree t = NULL_TREE;
15275
15276   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15277
15278   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15279                              ~TREE_INT_CST_HIGH (arg0), 0,
15280                              TREE_OVERFLOW (arg0));
15281
15282   return t;
15283 }
15284
15285 /* Given CODE, a relational operator, the target type, TYPE and two
15286    constant operands OP0 and OP1, return the result of the
15287    relational operation.  If the result is not a compile time
15288    constant, then return NULL_TREE.  */
15289
15290 static tree
15291 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15292 {
15293   int result, invert;
15294
15295   /* From here on, the only cases we handle are when the result is
15296      known to be a constant.  */
15297
15298   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15299     {
15300       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15301       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15302
15303       /* Handle the cases where either operand is a NaN.  */
15304       if (real_isnan (c0) || real_isnan (c1))
15305         {
15306           switch (code)
15307             {
15308             case EQ_EXPR:
15309             case ORDERED_EXPR:
15310               result = 0;
15311               break;
15312
15313             case NE_EXPR:
15314             case UNORDERED_EXPR:
15315             case UNLT_EXPR:
15316             case UNLE_EXPR:
15317             case UNGT_EXPR:
15318             case UNGE_EXPR:
15319             case UNEQ_EXPR:
15320               result = 1;
15321               break;
15322
15323             case LT_EXPR:
15324             case LE_EXPR:
15325             case GT_EXPR:
15326             case GE_EXPR:
15327             case LTGT_EXPR:
15328               if (flag_trapping_math)
15329                 return NULL_TREE;
15330               result = 0;
15331               break;
15332
15333             default:
15334               gcc_unreachable ();
15335             }
15336
15337           return constant_boolean_node (result, type);
15338         }
15339
15340       return constant_boolean_node (real_compare (code, c0, c1), type);
15341     }
15342
15343   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15344     {
15345       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15346       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15347       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15348     }
15349
15350   /* Handle equality/inequality of complex constants.  */
15351   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15352     {
15353       tree rcond = fold_relational_const (code, type,
15354                                           TREE_REALPART (op0),
15355                                           TREE_REALPART (op1));
15356       tree icond = fold_relational_const (code, type,
15357                                           TREE_IMAGPART (op0),
15358                                           TREE_IMAGPART (op1));
15359       if (code == EQ_EXPR)
15360         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15361       else if (code == NE_EXPR)
15362         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15363       else
15364         return NULL_TREE;
15365     }
15366
15367   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15368
15369      To compute GT, swap the arguments and do LT.
15370      To compute GE, do LT and invert the result.
15371      To compute LE, swap the arguments, do LT and invert the result.
15372      To compute NE, do EQ and invert the result.
15373
15374      Therefore, the code below must handle only EQ and LT.  */
15375
15376   if (code == LE_EXPR || code == GT_EXPR)
15377     {
15378       tree tem = op0;
15379       op0 = op1;
15380       op1 = tem;
15381       code = swap_tree_comparison (code);
15382     }
15383
15384   /* Note that it is safe to invert for real values here because we
15385      have already handled the one case that it matters.  */
15386
15387   invert = 0;
15388   if (code == NE_EXPR || code == GE_EXPR)
15389     {
15390       invert = 1;
15391       code = invert_tree_comparison (code, false);
15392     }
15393
15394   /* Compute a result for LT or EQ if args permit;
15395      Otherwise return T.  */
15396   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15397     {
15398       if (code == EQ_EXPR)
15399         result = tree_int_cst_equal (op0, op1);
15400       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15401         result = INT_CST_LT_UNSIGNED (op0, op1);
15402       else
15403         result = INT_CST_LT (op0, op1);
15404     }
15405   else
15406     return NULL_TREE;
15407
15408   if (invert)
15409     result ^= 1;
15410   return constant_boolean_node (result, type);
15411 }
15412
15413 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15414    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15415    itself.  */
15416
15417 tree
15418 fold_build_cleanup_point_expr (tree type, tree expr)
15419 {
15420   /* If the expression does not have side effects then we don't have to wrap
15421      it with a cleanup point expression.  */
15422   if (!TREE_SIDE_EFFECTS (expr))
15423     return expr;
15424
15425   /* If the expression is a return, check to see if the expression inside the
15426      return has no side effects or the right hand side of the modify expression
15427      inside the return. If either don't have side effects set we don't need to
15428      wrap the expression in a cleanup point expression.  Note we don't check the
15429      left hand side of the modify because it should always be a return decl.  */
15430   if (TREE_CODE (expr) == RETURN_EXPR)
15431     {
15432       tree op = TREE_OPERAND (expr, 0);
15433       if (!op || !TREE_SIDE_EFFECTS (op))
15434         return expr;
15435       op = TREE_OPERAND (op, 1);
15436       if (!TREE_SIDE_EFFECTS (op))
15437         return expr;
15438     }
15439   
15440   return build1 (CLEANUP_POINT_EXPR, type, expr);
15441 }
15442
15443 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15444    of an indirection through OP0, or NULL_TREE if no simplification is
15445    possible.  */
15446
15447 tree
15448 fold_indirect_ref_1 (tree type, tree op0)
15449 {
15450   tree sub = op0;
15451   tree subtype;
15452
15453   STRIP_NOPS (sub);
15454   subtype = TREE_TYPE (sub);
15455   if (!POINTER_TYPE_P (subtype))
15456     return NULL_TREE;
15457
15458   if (TREE_CODE (sub) == ADDR_EXPR)
15459     {
15460       tree op = TREE_OPERAND (sub, 0);
15461       tree optype = TREE_TYPE (op);
15462       /* *&CONST_DECL -> to the value of the const decl.  */
15463       if (TREE_CODE (op) == CONST_DECL)
15464         return DECL_INITIAL (op);
15465       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15466       if (type == optype)
15467         {
15468           tree fop = fold_read_from_constant_string (op);
15469           if (fop)
15470             return fop;
15471           else
15472             return op;
15473         }
15474       /* *(foo *)&fooarray => fooarray[0] */
15475       else if (TREE_CODE (optype) == ARRAY_TYPE
15476                && type == TREE_TYPE (optype))
15477         {
15478           tree type_domain = TYPE_DOMAIN (optype);
15479           tree min_val = size_zero_node;
15480           if (type_domain && TYPE_MIN_VALUE (type_domain))
15481             min_val = TYPE_MIN_VALUE (type_domain);
15482           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15483         }
15484       /* *(foo *)&complexfoo => __real__ complexfoo */
15485       else if (TREE_CODE (optype) == COMPLEX_TYPE
15486                && type == TREE_TYPE (optype))
15487         return fold_build1 (REALPART_EXPR, type, op);
15488       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15489       else if (TREE_CODE (optype) == VECTOR_TYPE
15490                && type == TREE_TYPE (optype))
15491         {
15492           tree part_width = TYPE_SIZE (type);
15493           tree index = bitsize_int (0);
15494           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15495         }
15496     }
15497
15498   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15499   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15500       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15501     { 
15502       tree op00 = TREE_OPERAND (sub, 0);
15503       tree op01 = TREE_OPERAND (sub, 1);
15504       tree op00type;
15505       
15506       STRIP_NOPS (op00);
15507       op00type = TREE_TYPE (op00);
15508       if (TREE_CODE (op00) == ADDR_EXPR
15509           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15510           && type == TREE_TYPE (TREE_TYPE (op00type)))
15511         { 
15512           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15513           tree part_width = TYPE_SIZE (type);
15514           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15515           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15516           tree index = bitsize_int (indexi);
15517
15518           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15519             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15520                                 part_width, index);
15521         
15522         }
15523     }
15524
15525
15526   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15527   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15528       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15529     {
15530       tree op00 = TREE_OPERAND (sub, 0);
15531       tree op01 = TREE_OPERAND (sub, 1);
15532       tree op00type;
15533
15534       STRIP_NOPS (op00);
15535       op00type = TREE_TYPE (op00);
15536       if (TREE_CODE (op00) == ADDR_EXPR
15537           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15538           && type == TREE_TYPE (TREE_TYPE (op00type)))
15539         {
15540           tree size = TYPE_SIZE_UNIT (type);
15541           if (tree_int_cst_equal (size, op01))
15542             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15543         }
15544     }
15545   
15546   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15547   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15548       && type == TREE_TYPE (TREE_TYPE (subtype)))
15549     {
15550       tree type_domain;
15551       tree min_val = size_zero_node;
15552       sub = build_fold_indirect_ref (sub);
15553       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15554       if (type_domain && TYPE_MIN_VALUE (type_domain))
15555         min_val = TYPE_MIN_VALUE (type_domain);
15556       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15557     }
15558
15559   return NULL_TREE;
15560 }
15561
15562 /* Builds an expression for an indirection through T, simplifying some
15563    cases.  */
15564
15565 tree
15566 build_fold_indirect_ref (tree t)
15567 {
15568   tree type = TREE_TYPE (TREE_TYPE (t));
15569   tree sub = fold_indirect_ref_1 (type, t);
15570
15571   if (sub)
15572     return sub;
15573   else
15574     return build1 (INDIRECT_REF, type, t);
15575 }
15576
15577 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15578
15579 tree
15580 fold_indirect_ref (tree t)
15581 {
15582   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15583
15584   if (sub)
15585     return sub;
15586   else
15587     return t;
15588 }
15589
15590 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15591    whose result is ignored.  The type of the returned tree need not be
15592    the same as the original expression.  */
15593
15594 tree
15595 fold_ignored_result (tree t)
15596 {
15597   if (!TREE_SIDE_EFFECTS (t))
15598     return integer_zero_node;
15599
15600   for (;;)
15601     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15602       {
15603       case tcc_unary:
15604         t = TREE_OPERAND (t, 0);
15605         break;
15606
15607       case tcc_binary:
15608       case tcc_comparison:
15609         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15610           t = TREE_OPERAND (t, 0);
15611         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15612           t = TREE_OPERAND (t, 1);
15613         else
15614           return t;
15615         break;
15616
15617       case tcc_expression:
15618         switch (TREE_CODE (t))
15619           {
15620           case COMPOUND_EXPR:
15621             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15622               return t;
15623             t = TREE_OPERAND (t, 0);
15624             break;
15625
15626           case COND_EXPR:
15627             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15628                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15629               return t;
15630             t = TREE_OPERAND (t, 0);
15631             break;
15632
15633           default:
15634             return t;
15635           }
15636         break;
15637
15638       default:
15639         return t;
15640       }
15641 }
15642
15643 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15644    This can only be applied to objects of a sizetype.  */
15645
15646 tree
15647 round_up (tree value, int divisor)
15648 {
15649   tree div = NULL_TREE;
15650
15651   gcc_assert (divisor > 0);
15652   if (divisor == 1)
15653     return value;
15654
15655   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15656      have to do anything.  Only do this when we are not given a const,
15657      because in that case, this check is more expensive than just
15658      doing it.  */
15659   if (TREE_CODE (value) != INTEGER_CST)
15660     {
15661       div = build_int_cst (TREE_TYPE (value), divisor);
15662
15663       if (multiple_of_p (TREE_TYPE (value), value, div))
15664         return value;
15665     }
15666
15667   /* If divisor is a power of two, simplify this to bit manipulation.  */
15668   if (divisor == (divisor & -divisor))
15669     {
15670       if (TREE_CODE (value) == INTEGER_CST)
15671         {
15672           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15673           unsigned HOST_WIDE_INT high;
15674           bool overflow_p;
15675
15676           if ((low & (divisor - 1)) == 0)
15677             return value;
15678
15679           overflow_p = TREE_OVERFLOW (value);
15680           high = TREE_INT_CST_HIGH (value);
15681           low &= ~(divisor - 1);
15682           low += divisor;
15683           if (low == 0)
15684             {
15685               high++;
15686               if (high == 0)
15687                 overflow_p = true;
15688             }
15689
15690           return force_fit_type_double (TREE_TYPE (value), low, high,
15691                                         -1, overflow_p);
15692         }
15693       else
15694         {
15695           tree t;
15696
15697           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15698           value = size_binop (PLUS_EXPR, value, t);
15699           t = build_int_cst (TREE_TYPE (value), -divisor);
15700           value = size_binop (BIT_AND_EXPR, value, t);
15701         }
15702     }
15703   else
15704     {
15705       if (!div)
15706         div = build_int_cst (TREE_TYPE (value), divisor);
15707       value = size_binop (CEIL_DIV_EXPR, value, div);
15708       value = size_binop (MULT_EXPR, value, div);
15709     }
15710
15711   return value;
15712 }
15713
15714 /* Likewise, but round down.  */
15715
15716 tree
15717 round_down (tree value, int divisor)
15718 {
15719   tree div = NULL_TREE;
15720
15721   gcc_assert (divisor > 0);
15722   if (divisor == 1)
15723     return value;
15724
15725   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15726      have to do anything.  Only do this when we are not given a const,
15727      because in that case, this check is more expensive than just
15728      doing it.  */
15729   if (TREE_CODE (value) != INTEGER_CST)
15730     {
15731       div = build_int_cst (TREE_TYPE (value), divisor);
15732
15733       if (multiple_of_p (TREE_TYPE (value), value, div))
15734         return value;
15735     }
15736
15737   /* If divisor is a power of two, simplify this to bit manipulation.  */
15738   if (divisor == (divisor & -divisor))
15739     {
15740       tree t;
15741
15742       t = build_int_cst (TREE_TYPE (value), -divisor);
15743       value = size_binop (BIT_AND_EXPR, value, t);
15744     }
15745   else
15746     {
15747       if (!div)
15748         div = build_int_cst (TREE_TYPE (value), divisor);
15749       value = size_binop (FLOOR_DIV_EXPR, value, div);
15750       value = size_binop (MULT_EXPR, value, div);
15751     }
15752
15753   return value;
15754 }
15755
15756 /* Returns the pointer to the base of the object addressed by EXP and
15757    extracts the information about the offset of the access, storing it
15758    to PBITPOS and POFFSET.  */
15759
15760 static tree
15761 split_address_to_core_and_offset (tree exp,
15762                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15763 {
15764   tree core;
15765   enum machine_mode mode;
15766   int unsignedp, volatilep;
15767   HOST_WIDE_INT bitsize;
15768
15769   if (TREE_CODE (exp) == ADDR_EXPR)
15770     {
15771       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15772                                   poffset, &mode, &unsignedp, &volatilep,
15773                                   false);
15774       core = build_fold_addr_expr (core);
15775     }
15776   else
15777     {
15778       core = exp;
15779       *pbitpos = 0;
15780       *poffset = NULL_TREE;
15781     }
15782
15783   return core;
15784 }
15785
15786 /* Returns true if addresses of E1 and E2 differ by a constant, false
15787    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15788
15789 bool
15790 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15791 {
15792   tree core1, core2;
15793   HOST_WIDE_INT bitpos1, bitpos2;
15794   tree toffset1, toffset2, tdiff, type;
15795
15796   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15797   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15798
15799   if (bitpos1 % BITS_PER_UNIT != 0
15800       || bitpos2 % BITS_PER_UNIT != 0
15801       || !operand_equal_p (core1, core2, 0))
15802     return false;
15803
15804   if (toffset1 && toffset2)
15805     {
15806       type = TREE_TYPE (toffset1);
15807       if (type != TREE_TYPE (toffset2))
15808         toffset2 = fold_convert (type, toffset2);
15809
15810       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15811       if (!cst_and_fits_in_hwi (tdiff))
15812         return false;
15813
15814       *diff = int_cst_value (tdiff);
15815     }
15816   else if (toffset1 || toffset2)
15817     {
15818       /* If only one of the offsets is non-constant, the difference cannot
15819          be a constant.  */
15820       return false;
15821     }
15822   else
15823     *diff = 0;
15824
15825   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15826   return true;
15827 }
15828
15829 /* Simplify the floating point expression EXP when the sign of the
15830    result is not significant.  Return NULL_TREE if no simplification
15831    is possible.  */
15832
15833 tree
15834 fold_strip_sign_ops (tree exp)
15835 {
15836   tree arg0, arg1;
15837
15838   switch (TREE_CODE (exp))
15839     {
15840     case ABS_EXPR:
15841     case NEGATE_EXPR:
15842       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15843       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15844
15845     case MULT_EXPR:
15846     case RDIV_EXPR:
15847       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15848         return NULL_TREE;
15849       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15850       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15851       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15852         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15853                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15854                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15855       break;
15856
15857     case COMPOUND_EXPR:
15858       arg0 = TREE_OPERAND (exp, 0);
15859       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15860       if (arg1)
15861         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15862       break;
15863       
15864     case COND_EXPR:
15865       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15866       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15867       if (arg0 || arg1)
15868         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15869                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15870                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15871       break;
15872       
15873     case CALL_EXPR:
15874       {
15875         const enum built_in_function fcode = builtin_mathfn_code (exp);
15876         switch (fcode)
15877         {
15878         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15879           /* Strip copysign function call, return the 1st argument. */
15880           arg0 = CALL_EXPR_ARG (exp, 0);
15881           arg1 = CALL_EXPR_ARG (exp, 1);
15882           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15883
15884         default:
15885           /* Strip sign ops from the argument of "odd" math functions.  */
15886           if (negate_mathfn_p (fcode))
15887             {
15888               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15889               if (arg0)
15890                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15891             }
15892           break;
15893         }
15894       }
15895       break;
15896
15897     default:
15898       break;
15899     }
15900   return NULL_TREE;
15901 }