OSDN Git Service

2009-03-29 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 static 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         {
1936           TREE_OVERFLOW (t) = 1;
1937           TREE_CONSTANT_OVERFLOW (t) = 1;
1938         }
1939       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1940         TREE_CONSTANT_OVERFLOW (t) = 1;
1941       return t;
1942     }
1943
1944   if (TREE_CODE (arg1) == COMPLEX_CST)
1945     {
1946       tree type = TREE_TYPE (arg1);
1947       tree r1 = TREE_REALPART (arg1);
1948       tree i1 = TREE_IMAGPART (arg1);
1949       tree r2 = TREE_REALPART (arg2);
1950       tree i2 = TREE_IMAGPART (arg2);
1951       tree real, imag;
1952
1953       switch (code)
1954         {
1955         case PLUS_EXPR:
1956         case MINUS_EXPR:
1957           real = const_binop (code, r1, r2, notrunc);
1958           imag = const_binop (code, i1, i2, notrunc);
1959           break;
1960
1961         case MULT_EXPR:
1962           real = const_binop (MINUS_EXPR,
1963                               const_binop (MULT_EXPR, r1, r2, notrunc),
1964                               const_binop (MULT_EXPR, i1, i2, notrunc),
1965                               notrunc);
1966           imag = const_binop (PLUS_EXPR,
1967                               const_binop (MULT_EXPR, r1, i2, notrunc),
1968                               const_binop (MULT_EXPR, i1, r2, notrunc),
1969                               notrunc);
1970           break;
1971
1972         case RDIV_EXPR:
1973           {
1974             tree magsquared
1975               = const_binop (PLUS_EXPR,
1976                              const_binop (MULT_EXPR, r2, r2, notrunc),
1977                              const_binop (MULT_EXPR, i2, i2, notrunc),
1978                              notrunc);
1979             tree t1
1980               = const_binop (PLUS_EXPR,
1981                              const_binop (MULT_EXPR, r1, r2, notrunc),
1982                              const_binop (MULT_EXPR, i1, i2, notrunc),
1983                              notrunc);
1984             tree t2
1985               = const_binop (MINUS_EXPR,
1986                              const_binop (MULT_EXPR, i1, r2, notrunc),
1987                              const_binop (MULT_EXPR, r1, i2, notrunc),
1988                              notrunc);
1989
1990             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1991               code = TRUNC_DIV_EXPR;
1992
1993             real = const_binop (code, t1, magsquared, notrunc);
1994             imag = const_binop (code, t2, magsquared, notrunc);
1995           }
1996           break;
1997
1998         default:
1999           return NULL_TREE;
2000         }
2001
2002       if (real && imag)
2003         return build_complex (type, real, imag);
2004     }
2005
2006   return NULL_TREE;
2007 }
2008
2009 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
2010    indicates which particular sizetype to create.  */
2011
2012 tree
2013 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2014 {
2015   return build_int_cst (sizetype_tab[(int) kind], number);
2016 }
2017 \f
2018 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
2019    is a tree code.  The type of the result is taken from the operands.
2020    Both must be equivalent integer types, ala int_binop_types_match_p.
2021    If the operands are constant, so is the result.  */
2022
2023 tree
2024 size_binop (enum tree_code code, tree arg0, tree arg1)
2025 {
2026   tree type = TREE_TYPE (arg0);
2027
2028   if (arg0 == error_mark_node || arg1 == error_mark_node)
2029     return error_mark_node;
2030
2031   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2032                                        TREE_TYPE (arg1)));
2033
2034   /* Handle the special case of two integer constants faster.  */
2035   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2036     {
2037       /* And some specific cases even faster than that.  */
2038       if (code == PLUS_EXPR)
2039         {
2040           if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2041             return arg1;
2042           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2043             return arg0;
2044         }
2045       else if (code == MINUS_EXPR)
2046         {
2047           if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2048             return arg0;
2049         }
2050       else if (code == MULT_EXPR)
2051         {
2052           if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2053             return arg1;
2054         }
2055
2056       /* Handle general case of two integer constants.  */
2057       return int_const_binop (code, arg0, arg1, 0);
2058     }
2059
2060   return fold_build2 (code, type, arg0, arg1);
2061 }
2062
2063 /* Given two values, either both of sizetype or both of bitsizetype,
2064    compute the difference between the two values.  Return the value
2065    in signed type corresponding to the type of the operands.  */
2066
2067 tree
2068 size_diffop (tree arg0, tree arg1)
2069 {
2070   tree type = TREE_TYPE (arg0);
2071   tree ctype;
2072
2073   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2074                                        TREE_TYPE (arg1)));
2075
2076   /* If the type is already signed, just do the simple thing.  */
2077   if (!TYPE_UNSIGNED (type))
2078     return size_binop (MINUS_EXPR, arg0, arg1);
2079
2080   if (type == sizetype)
2081     ctype = ssizetype;
2082   else if (type == bitsizetype)
2083     ctype = sbitsizetype;
2084   else
2085     ctype = signed_type_for (type);
2086
2087   /* If either operand is not a constant, do the conversions to the signed
2088      type and subtract.  The hardware will do the right thing with any
2089      overflow in the subtraction.  */
2090   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2091     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2092                        fold_convert (ctype, arg1));
2093
2094   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2095      Otherwise, subtract the other way, convert to CTYPE (we know that can't
2096      overflow) and negate (which can't either).  Special-case a result
2097      of zero while we're here.  */
2098   if (tree_int_cst_equal (arg0, arg1))
2099     return build_int_cst (ctype, 0);
2100   else if (tree_int_cst_lt (arg1, arg0))
2101     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2102   else
2103     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2104                        fold_convert (ctype, size_binop (MINUS_EXPR,
2105                                                         arg1, arg0)));
2106 }
2107 \f
2108 /* A subroutine of fold_convert_const handling conversions of an
2109    INTEGER_CST to another integer type.  */
2110
2111 static tree
2112 fold_convert_const_int_from_int (tree type, const_tree arg1)
2113 {
2114   tree t;
2115
2116   /* Given an integer constant, make new constant with new type,
2117      appropriately sign-extended or truncated.  */
2118   t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2119                              TREE_INT_CST_HIGH (arg1),
2120                              /* Don't set the overflow when
2121                                 converting from a pointer,  */
2122                              !POINTER_TYPE_P (TREE_TYPE (arg1))
2123                              /* or to a sizetype with same signedness
2124                                 and the precision is unchanged.
2125                                 ???  sizetype is always sign-extended,
2126                                 but its signedness depends on the
2127                                 frontend.  Thus we see spurious overflows
2128                                 here if we do not check this.  */
2129                              && !((TYPE_PRECISION (TREE_TYPE (arg1))
2130                                    == TYPE_PRECISION (type))
2131                                   && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2132                                       == TYPE_UNSIGNED (type))
2133                                   && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2134                                        && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2135                                       || (TREE_CODE (type) == INTEGER_TYPE
2136                                           && TYPE_IS_SIZETYPE (type)))),
2137                              (TREE_INT_CST_HIGH (arg1) < 0
2138                               && (TYPE_UNSIGNED (type)
2139                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2140                              | TREE_OVERFLOW (arg1));
2141
2142   return t;
2143 }
2144
2145 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2146    to an integer type.  */
2147
2148 static tree
2149 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2150 {
2151   int overflow = 0;
2152   tree t;
2153
2154   /* The following code implements the floating point to integer
2155      conversion rules required by the Java Language Specification,
2156      that IEEE NaNs are mapped to zero and values that overflow
2157      the target precision saturate, i.e. values greater than
2158      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2159      are mapped to INT_MIN.  These semantics are allowed by the
2160      C and C++ standards that simply state that the behavior of
2161      FP-to-integer conversion is unspecified upon overflow.  */
2162
2163   HOST_WIDE_INT high, low;
2164   REAL_VALUE_TYPE r;
2165   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2166
2167   switch (code)
2168     {
2169     case FIX_TRUNC_EXPR:
2170       real_trunc (&r, VOIDmode, &x);
2171       break;
2172
2173     default:
2174       gcc_unreachable ();
2175     }
2176
2177   /* If R is NaN, return zero and show we have an overflow.  */
2178   if (REAL_VALUE_ISNAN (r))
2179     {
2180       overflow = 1;
2181       high = 0;
2182       low = 0;
2183     }
2184
2185   /* See if R is less than the lower bound or greater than the
2186      upper bound.  */
2187
2188   if (! overflow)
2189     {
2190       tree lt = TYPE_MIN_VALUE (type);
2191       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2192       if (REAL_VALUES_LESS (r, l))
2193         {
2194           overflow = 1;
2195           high = TREE_INT_CST_HIGH (lt);
2196           low = TREE_INT_CST_LOW (lt);
2197         }
2198     }
2199
2200   if (! overflow)
2201     {
2202       tree ut = TYPE_MAX_VALUE (type);
2203       if (ut)
2204         {
2205           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2206           if (REAL_VALUES_LESS (u, r))
2207             {
2208               overflow = 1;
2209               high = TREE_INT_CST_HIGH (ut);
2210               low = TREE_INT_CST_LOW (ut);
2211             }
2212         }
2213     }
2214
2215   if (! overflow)
2216     REAL_VALUE_TO_INT (&low, &high, r);
2217
2218   t = force_fit_type_double (type, low, high, -1,
2219                              overflow | TREE_OVERFLOW (arg1));
2220   return t;
2221 }
2222
2223 /* A subroutine of fold_convert_const handling conversions of a
2224    FIXED_CST to an integer type.  */
2225
2226 static tree
2227 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2228 {
2229   tree t;
2230   double_int temp, temp_trunc;
2231   unsigned int mode;
2232
2233   /* Right shift FIXED_CST to temp by fbit.  */
2234   temp = TREE_FIXED_CST (arg1).data;
2235   mode = TREE_FIXED_CST (arg1).mode;
2236   if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2237     {
2238       lshift_double (temp.low, temp.high,
2239                      - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240                      &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2241
2242       /* Left shift temp to temp_trunc by fbit.  */
2243       lshift_double (temp.low, temp.high,
2244                      GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2245                      &temp_trunc.low, &temp_trunc.high,
2246                      SIGNED_FIXED_POINT_MODE_P (mode));
2247     }
2248   else
2249     {
2250       temp.low = 0;
2251       temp.high = 0;
2252       temp_trunc.low = 0;
2253       temp_trunc.high = 0;
2254     }
2255
2256   /* If FIXED_CST is negative, we need to round the value toward 0.
2257      By checking if the fractional bits are not zero to add 1 to temp.  */
2258   if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2259       && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2260     {
2261       double_int one;
2262       one.low = 1;
2263       one.high = 0;
2264       temp = double_int_add (temp, one);
2265     }
2266
2267   /* Given a fixed-point constant, make new constant with new type,
2268      appropriately sign-extended or truncated.  */
2269   t = force_fit_type_double (type, temp.low, temp.high, -1,
2270                              (temp.high < 0
2271                               && (TYPE_UNSIGNED (type)
2272                                   < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2273                              | TREE_OVERFLOW (arg1));
2274
2275   return t;
2276 }
2277
2278 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2279    to another floating point type.  */
2280
2281 static tree
2282 fold_convert_const_real_from_real (tree type, const_tree arg1)
2283 {
2284   REAL_VALUE_TYPE value;
2285   tree t;
2286
2287   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2288   t = build_real (type, value);
2289
2290   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2291   return t;
2292 }
2293
2294 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2295    to a floating point type.  */
2296
2297 static tree
2298 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2299 {
2300   REAL_VALUE_TYPE value;
2301   tree t;
2302
2303   real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2304   t = build_real (type, value);
2305
2306   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2307   TREE_CONSTANT_OVERFLOW (t)
2308     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2309   return t;
2310 }
2311
2312 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2313    to another fixed-point type.  */
2314
2315 static tree
2316 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2317 {
2318   FIXED_VALUE_TYPE value;
2319   tree t;
2320   bool overflow_p;
2321
2322   overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2323                               TYPE_SATURATING (type));
2324   t = build_fixed (type, value);
2325
2326   /* Propagate overflow flags.  */
2327   if (overflow_p | TREE_OVERFLOW (arg1))
2328     {
2329       TREE_OVERFLOW (t) = 1;
2330       TREE_CONSTANT_OVERFLOW (t) = 1;
2331     }
2332   else if (TREE_CONSTANT_OVERFLOW (arg1))
2333     TREE_CONSTANT_OVERFLOW (t) = 1;
2334   return t;
2335 }
2336
2337 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2338    to a fixed-point type.  */
2339
2340 static tree
2341 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2342 {
2343   FIXED_VALUE_TYPE value;
2344   tree t;
2345   bool overflow_p;
2346
2347   overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2348                                        TREE_INT_CST (arg1),
2349                                        TYPE_UNSIGNED (TREE_TYPE (arg1)),
2350                                        TYPE_SATURATING (type));
2351   t = build_fixed (type, value);
2352
2353   /* Propagate overflow flags.  */
2354   if (overflow_p | TREE_OVERFLOW (arg1))
2355     {
2356       TREE_OVERFLOW (t) = 1;
2357       TREE_CONSTANT_OVERFLOW (t) = 1;
2358     }
2359   else if (TREE_CONSTANT_OVERFLOW (arg1))
2360     TREE_CONSTANT_OVERFLOW (t) = 1;
2361   return t;
2362 }
2363
2364 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2365    to a fixed-point type.  */
2366
2367 static tree
2368 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2369 {
2370   FIXED_VALUE_TYPE value;
2371   tree t;
2372   bool overflow_p;
2373
2374   overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2375                                         &TREE_REAL_CST (arg1),
2376                                         TYPE_SATURATING (type));
2377   t = build_fixed (type, value);
2378
2379   /* Propagate overflow flags.  */
2380   if (overflow_p | TREE_OVERFLOW (arg1))
2381     {
2382       TREE_OVERFLOW (t) = 1;
2383       TREE_CONSTANT_OVERFLOW (t) = 1;
2384     }
2385   else if (TREE_CONSTANT_OVERFLOW (arg1))
2386     TREE_CONSTANT_OVERFLOW (t) = 1;
2387   return t;
2388 }
2389
2390 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2391    type TYPE.  If no simplification can be done return NULL_TREE.  */
2392
2393 static tree
2394 fold_convert_const (enum tree_code code, tree type, tree arg1)
2395 {
2396   if (TREE_TYPE (arg1) == type)
2397     return arg1;
2398
2399   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2400       || TREE_CODE (type) == OFFSET_TYPE)
2401     {
2402       if (TREE_CODE (arg1) == INTEGER_CST)
2403         return fold_convert_const_int_from_int (type, arg1);
2404       else if (TREE_CODE (arg1) == REAL_CST)
2405         return fold_convert_const_int_from_real (code, type, arg1);
2406       else if (TREE_CODE (arg1) == FIXED_CST)
2407         return fold_convert_const_int_from_fixed (type, arg1);
2408     }
2409   else if (TREE_CODE (type) == REAL_TYPE)
2410     {
2411       if (TREE_CODE (arg1) == INTEGER_CST)
2412         return build_real_from_int_cst (type, arg1);
2413       else if (TREE_CODE (arg1) == REAL_CST)
2414         return fold_convert_const_real_from_real (type, arg1);
2415       else if (TREE_CODE (arg1) == FIXED_CST)
2416         return fold_convert_const_real_from_fixed (type, arg1);
2417     }
2418   else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2419     {
2420       if (TREE_CODE (arg1) == FIXED_CST)
2421         return fold_convert_const_fixed_from_fixed (type, arg1);
2422       else if (TREE_CODE (arg1) == INTEGER_CST)
2423         return fold_convert_const_fixed_from_int (type, arg1);
2424       else if (TREE_CODE (arg1) == REAL_CST)
2425         return fold_convert_const_fixed_from_real (type, arg1);
2426     }
2427   return NULL_TREE;
2428 }
2429
2430 /* Construct a vector of zero elements of vector type TYPE.  */
2431
2432 static tree
2433 build_zero_vector (tree type)
2434 {
2435   tree elem, list;
2436   int i, units;
2437
2438   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2439   units = TYPE_VECTOR_SUBPARTS (type);
2440   
2441   list = NULL_TREE;
2442   for (i = 0; i < units; i++)
2443     list = tree_cons (NULL_TREE, elem, list);
2444   return build_vector (type, list);
2445 }
2446
2447 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR.  */
2448
2449 bool
2450 fold_convertible_p (const_tree type, const_tree arg)
2451 {
2452   tree orig = TREE_TYPE (arg);
2453
2454   if (type == orig)
2455     return true;
2456
2457   if (TREE_CODE (arg) == ERROR_MARK
2458       || TREE_CODE (type) == ERROR_MARK
2459       || TREE_CODE (orig) == ERROR_MARK)
2460     return false;
2461
2462   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2463     return true;
2464
2465   switch (TREE_CODE (type))
2466     {
2467     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2468     case POINTER_TYPE: case REFERENCE_TYPE:
2469     case OFFSET_TYPE:
2470       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2471           || TREE_CODE (orig) == OFFSET_TYPE)
2472         return true;
2473       return (TREE_CODE (orig) == VECTOR_TYPE
2474               && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2475
2476     case REAL_TYPE:
2477     case FIXED_POINT_TYPE:
2478     case COMPLEX_TYPE:
2479     case VECTOR_TYPE:
2480     case VOID_TYPE:
2481       return TREE_CODE (type) == TREE_CODE (orig);
2482
2483     default:
2484       return false;
2485     }
2486 }
2487
2488 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2489    simple conversions in preference to calling the front-end's convert.  */
2490
2491 tree
2492 fold_convert (tree type, tree arg)
2493 {
2494   tree orig = TREE_TYPE (arg);
2495   tree tem;
2496
2497   if (type == orig)
2498     return arg;
2499
2500   if (TREE_CODE (arg) == ERROR_MARK
2501       || TREE_CODE (type) == ERROR_MARK
2502       || TREE_CODE (orig) == ERROR_MARK)
2503     return error_mark_node;
2504
2505   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2506     return fold_build1 (NOP_EXPR, type, arg);
2507
2508   switch (TREE_CODE (type))
2509     {
2510     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2511     case POINTER_TYPE: case REFERENCE_TYPE:
2512     case OFFSET_TYPE:
2513       if (TREE_CODE (arg) == INTEGER_CST)
2514         {
2515           tem = fold_convert_const (NOP_EXPR, type, arg);
2516           if (tem != NULL_TREE)
2517             return tem;
2518         }
2519       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2520           || TREE_CODE (orig) == OFFSET_TYPE)
2521         return fold_build1 (NOP_EXPR, type, arg);
2522       if (TREE_CODE (orig) == COMPLEX_TYPE)
2523         {
2524           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2525           return fold_convert (type, tem);
2526         }
2527       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2528                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2529       return fold_build1 (NOP_EXPR, type, arg);
2530
2531     case REAL_TYPE:
2532       if (TREE_CODE (arg) == INTEGER_CST)
2533         {
2534           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2535           if (tem != NULL_TREE)
2536             return tem;
2537         }
2538       else if (TREE_CODE (arg) == REAL_CST)
2539         {
2540           tem = fold_convert_const (NOP_EXPR, type, arg);
2541           if (tem != NULL_TREE)
2542             return tem;
2543         }
2544       else if (TREE_CODE (arg) == FIXED_CST)
2545         {
2546           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2547           if (tem != NULL_TREE)
2548             return tem;
2549         }
2550
2551       switch (TREE_CODE (orig))
2552         {
2553         case INTEGER_TYPE:
2554         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2555         case POINTER_TYPE: case REFERENCE_TYPE:
2556           return fold_build1 (FLOAT_EXPR, type, arg);
2557
2558         case REAL_TYPE:
2559           return fold_build1 (NOP_EXPR, type, arg);
2560
2561         case FIXED_POINT_TYPE:
2562           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2563
2564         case COMPLEX_TYPE:
2565           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2566           return fold_convert (type, tem);
2567
2568         default:
2569           gcc_unreachable ();
2570         }
2571
2572     case FIXED_POINT_TYPE:
2573       if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2574           || TREE_CODE (arg) == REAL_CST)
2575         {
2576           tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2577           if (tem != NULL_TREE)
2578             return tem;
2579         }
2580
2581       switch (TREE_CODE (orig))
2582         {
2583         case FIXED_POINT_TYPE:
2584         case INTEGER_TYPE:
2585         case ENUMERAL_TYPE:
2586         case BOOLEAN_TYPE:
2587         case REAL_TYPE:
2588           return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2589
2590         case COMPLEX_TYPE:
2591           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2592           return fold_convert (type, tem);
2593
2594         default:
2595           gcc_unreachable ();
2596         }
2597
2598     case COMPLEX_TYPE:
2599       switch (TREE_CODE (orig))
2600         {
2601         case INTEGER_TYPE:
2602         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2603         case POINTER_TYPE: case REFERENCE_TYPE:
2604         case REAL_TYPE:
2605         case FIXED_POINT_TYPE:
2606           return build2 (COMPLEX_EXPR, type,
2607                          fold_convert (TREE_TYPE (type), arg),
2608                          fold_convert (TREE_TYPE (type), integer_zero_node));
2609         case COMPLEX_TYPE:
2610           {
2611             tree rpart, ipart;
2612
2613             if (TREE_CODE (arg) == COMPLEX_EXPR)
2614               {
2615                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2616                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2617                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2618               }
2619
2620             arg = save_expr (arg);
2621             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2622             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2623             rpart = fold_convert (TREE_TYPE (type), rpart);
2624             ipart = fold_convert (TREE_TYPE (type), ipart);
2625             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2626           }
2627
2628         default:
2629           gcc_unreachable ();
2630         }
2631
2632     case VECTOR_TYPE:
2633       if (integer_zerop (arg))
2634         return build_zero_vector (type);
2635       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2636       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2637                   || TREE_CODE (orig) == VECTOR_TYPE);
2638       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2639
2640     case VOID_TYPE:
2641       tem = fold_ignored_result (arg);
2642       if (TREE_CODE (tem) == MODIFY_EXPR)
2643         return tem;
2644       return fold_build1 (NOP_EXPR, type, tem);
2645
2646     default:
2647       gcc_unreachable ();
2648     }
2649 }
2650 \f
2651 /* Return false if expr can be assumed not to be an lvalue, true
2652    otherwise.  */
2653
2654 static bool
2655 maybe_lvalue_p (const_tree x)
2656 {
2657   /* We only need to wrap lvalue tree codes.  */
2658   switch (TREE_CODE (x))
2659   {
2660   case VAR_DECL:
2661   case PARM_DECL:
2662   case RESULT_DECL:
2663   case LABEL_DECL:
2664   case FUNCTION_DECL:
2665   case SSA_NAME:
2666
2667   case COMPONENT_REF:
2668   case INDIRECT_REF:
2669   case ALIGN_INDIRECT_REF:
2670   case MISALIGNED_INDIRECT_REF:
2671   case ARRAY_REF:
2672   case ARRAY_RANGE_REF:
2673   case BIT_FIELD_REF:
2674   case OBJ_TYPE_REF:
2675
2676   case REALPART_EXPR:
2677   case IMAGPART_EXPR:
2678   case PREINCREMENT_EXPR:
2679   case PREDECREMENT_EXPR:
2680   case SAVE_EXPR:
2681   case TRY_CATCH_EXPR:
2682   case WITH_CLEANUP_EXPR:
2683   case COMPOUND_EXPR:
2684   case MODIFY_EXPR:
2685   case TARGET_EXPR:
2686   case COND_EXPR:
2687   case BIND_EXPR:
2688   case MIN_EXPR:
2689   case MAX_EXPR:
2690     break;
2691
2692   default:
2693     /* Assume the worst for front-end tree codes.  */
2694     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2695       break;
2696     return false;
2697   }
2698
2699   return true;
2700 }
2701
2702 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2703
2704 tree
2705 non_lvalue (tree x)
2706 {
2707   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2708      us.  */
2709   if (in_gimple_form)
2710     return x;
2711
2712   if (! maybe_lvalue_p (x))
2713     return x;
2714   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2715 }
2716
2717 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2718    Zero means allow extended lvalues.  */
2719
2720 int pedantic_lvalues;
2721
2722 /* When pedantic, return an expr equal to X but certainly not valid as a
2723    pedantic lvalue.  Otherwise, return X.  */
2724
2725 static tree
2726 pedantic_non_lvalue (tree x)
2727 {
2728   if (pedantic_lvalues)
2729     return non_lvalue (x);
2730   else
2731     return x;
2732 }
2733 \f
2734 /* Given a tree comparison code, return the code that is the logical inverse
2735    of the given code.  It is not safe to do this for floating-point
2736    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2737    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2738
2739 enum tree_code
2740 invert_tree_comparison (enum tree_code code, bool honor_nans)
2741 {
2742   if (honor_nans && flag_trapping_math)
2743     return ERROR_MARK;
2744
2745   switch (code)
2746     {
2747     case EQ_EXPR:
2748       return NE_EXPR;
2749     case NE_EXPR:
2750       return EQ_EXPR;
2751     case GT_EXPR:
2752       return honor_nans ? UNLE_EXPR : LE_EXPR;
2753     case GE_EXPR:
2754       return honor_nans ? UNLT_EXPR : LT_EXPR;
2755     case LT_EXPR:
2756       return honor_nans ? UNGE_EXPR : GE_EXPR;
2757     case LE_EXPR:
2758       return honor_nans ? UNGT_EXPR : GT_EXPR;
2759     case LTGT_EXPR:
2760       return UNEQ_EXPR;
2761     case UNEQ_EXPR:
2762       return LTGT_EXPR;
2763     case UNGT_EXPR:
2764       return LE_EXPR;
2765     case UNGE_EXPR:
2766       return LT_EXPR;
2767     case UNLT_EXPR:
2768       return GE_EXPR;
2769     case UNLE_EXPR:
2770       return GT_EXPR;
2771     case ORDERED_EXPR:
2772       return UNORDERED_EXPR;
2773     case UNORDERED_EXPR:
2774       return ORDERED_EXPR;
2775     default:
2776       gcc_unreachable ();
2777     }
2778 }
2779
2780 /* Similar, but return the comparison that results if the operands are
2781    swapped.  This is safe for floating-point.  */
2782
2783 enum tree_code
2784 swap_tree_comparison (enum tree_code code)
2785 {
2786   switch (code)
2787     {
2788     case EQ_EXPR:
2789     case NE_EXPR:
2790     case ORDERED_EXPR:
2791     case UNORDERED_EXPR:
2792     case LTGT_EXPR:
2793     case UNEQ_EXPR:
2794       return code;
2795     case GT_EXPR:
2796       return LT_EXPR;
2797     case GE_EXPR:
2798       return LE_EXPR;
2799     case LT_EXPR:
2800       return GT_EXPR;
2801     case LE_EXPR:
2802       return GE_EXPR;
2803     case UNGT_EXPR:
2804       return UNLT_EXPR;
2805     case UNGE_EXPR:
2806       return UNLE_EXPR;
2807     case UNLT_EXPR:
2808       return UNGT_EXPR;
2809     case UNLE_EXPR:
2810       return UNGE_EXPR;
2811     default:
2812       gcc_unreachable ();
2813     }
2814 }
2815
2816
2817 /* Convert a comparison tree code from an enum tree_code representation
2818    into a compcode bit-based encoding.  This function is the inverse of
2819    compcode_to_comparison.  */
2820
2821 static enum comparison_code
2822 comparison_to_compcode (enum tree_code code)
2823 {
2824   switch (code)
2825     {
2826     case LT_EXPR:
2827       return COMPCODE_LT;
2828     case EQ_EXPR:
2829       return COMPCODE_EQ;
2830     case LE_EXPR:
2831       return COMPCODE_LE;
2832     case GT_EXPR:
2833       return COMPCODE_GT;
2834     case NE_EXPR:
2835       return COMPCODE_NE;
2836     case GE_EXPR:
2837       return COMPCODE_GE;
2838     case ORDERED_EXPR:
2839       return COMPCODE_ORD;
2840     case UNORDERED_EXPR:
2841       return COMPCODE_UNORD;
2842     case UNLT_EXPR:
2843       return COMPCODE_UNLT;
2844     case UNEQ_EXPR:
2845       return COMPCODE_UNEQ;
2846     case UNLE_EXPR:
2847       return COMPCODE_UNLE;
2848     case UNGT_EXPR:
2849       return COMPCODE_UNGT;
2850     case LTGT_EXPR:
2851       return COMPCODE_LTGT;
2852     case UNGE_EXPR:
2853       return COMPCODE_UNGE;
2854     default:
2855       gcc_unreachable ();
2856     }
2857 }
2858
2859 /* Convert a compcode bit-based encoding of a comparison operator back
2860    to GCC's enum tree_code representation.  This function is the
2861    inverse of comparison_to_compcode.  */
2862
2863 static enum tree_code
2864 compcode_to_comparison (enum comparison_code code)
2865 {
2866   switch (code)
2867     {
2868     case COMPCODE_LT:
2869       return LT_EXPR;
2870     case COMPCODE_EQ:
2871       return EQ_EXPR;
2872     case COMPCODE_LE:
2873       return LE_EXPR;
2874     case COMPCODE_GT:
2875       return GT_EXPR;
2876     case COMPCODE_NE:
2877       return NE_EXPR;
2878     case COMPCODE_GE:
2879       return GE_EXPR;
2880     case COMPCODE_ORD:
2881       return ORDERED_EXPR;
2882     case COMPCODE_UNORD:
2883       return UNORDERED_EXPR;
2884     case COMPCODE_UNLT:
2885       return UNLT_EXPR;
2886     case COMPCODE_UNEQ:
2887       return UNEQ_EXPR;
2888     case COMPCODE_UNLE:
2889       return UNLE_EXPR;
2890     case COMPCODE_UNGT:
2891       return UNGT_EXPR;
2892     case COMPCODE_LTGT:
2893       return LTGT_EXPR;
2894     case COMPCODE_UNGE:
2895       return UNGE_EXPR;
2896     default:
2897       gcc_unreachable ();
2898     }
2899 }
2900
2901 /* Return a tree for the comparison which is the combination of
2902    doing the AND or OR (depending on CODE) of the two operations LCODE
2903    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2904    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2905    if this makes the transformation invalid.  */
2906
2907 tree
2908 combine_comparisons (enum tree_code code, enum tree_code lcode,
2909                      enum tree_code rcode, tree truth_type,
2910                      tree ll_arg, tree lr_arg)
2911 {
2912   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2913   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2914   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2915   enum comparison_code compcode;
2916
2917   switch (code)
2918     {
2919     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2920       compcode = lcompcode & rcompcode;
2921       break;
2922
2923     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2924       compcode = lcompcode | rcompcode;
2925       break;
2926
2927     default:
2928       return NULL_TREE;
2929     }
2930
2931   if (!honor_nans)
2932     {
2933       /* Eliminate unordered comparisons, as well as LTGT and ORD
2934          which are not used unless the mode has NaNs.  */
2935       compcode &= ~COMPCODE_UNORD;
2936       if (compcode == COMPCODE_LTGT)
2937         compcode = COMPCODE_NE;
2938       else if (compcode == COMPCODE_ORD)
2939         compcode = COMPCODE_TRUE;
2940     }
2941    else if (flag_trapping_math)
2942      {
2943         /* Check that the original operation and the optimized ones will trap
2944            under the same condition.  */
2945         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2946                      && (lcompcode != COMPCODE_EQ)
2947                      && (lcompcode != COMPCODE_ORD);
2948         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2949                      && (rcompcode != COMPCODE_EQ)
2950                      && (rcompcode != COMPCODE_ORD);
2951         bool trap = (compcode & COMPCODE_UNORD) == 0
2952                     && (compcode != COMPCODE_EQ)
2953                     && (compcode != COMPCODE_ORD);
2954
2955         /* In a short-circuited boolean expression the LHS might be
2956            such that the RHS, if evaluated, will never trap.  For
2957            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2958            if neither x nor y is NaN.  (This is a mixed blessing: for
2959            example, the expression above will never trap, hence
2960            optimizing it to x < y would be invalid).  */
2961         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2962             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2963           rtrap = false;
2964
2965         /* If the comparison was short-circuited, and only the RHS
2966            trapped, we may now generate a spurious trap.  */
2967         if (rtrap && !ltrap
2968             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2969           return NULL_TREE;
2970
2971         /* If we changed the conditions that cause a trap, we lose.  */
2972         if ((ltrap || rtrap) != trap)
2973           return NULL_TREE;
2974       }
2975
2976   if (compcode == COMPCODE_TRUE)
2977     return constant_boolean_node (true, truth_type);
2978   else if (compcode == COMPCODE_FALSE)
2979     return constant_boolean_node (false, truth_type);
2980   else
2981     return fold_build2 (compcode_to_comparison (compcode),
2982                         truth_type, ll_arg, lr_arg);
2983 }
2984 \f
2985 /* Return nonzero if two operands (typically of the same tree node)
2986    are necessarily equal.  If either argument has side-effects this
2987    function returns zero.  FLAGS modifies behavior as follows:
2988
2989    If OEP_ONLY_CONST is set, only return nonzero for constants.
2990    This function tests whether the operands are indistinguishable;
2991    it does not test whether they are equal using C's == operation.
2992    The distinction is important for IEEE floating point, because
2993    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2994    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2995
2996    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2997    even though it may hold multiple values during a function.
2998    This is because a GCC tree node guarantees that nothing else is
2999    executed between the evaluation of its "operands" (which may often
3000    be evaluated in arbitrary order).  Hence if the operands themselves
3001    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3002    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
3003    unset means assuming isochronic (or instantaneous) tree equivalence.
3004    Unless comparing arbitrary expression trees, such as from different
3005    statements, this flag can usually be left unset.
3006
3007    If OEP_PURE_SAME is set, then pure functions with identical arguments
3008    are considered the same.  It is used when the caller has other ways
3009    to ensure that global memory is unchanged in between.  */
3010
3011 int
3012 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3013 {
3014   /* If either is ERROR_MARK, they aren't equal.  */
3015   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3016     return 0;
3017
3018   /* Check equality of integer constants before bailing out due to
3019      precision differences.  */
3020   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3021     return tree_int_cst_equal (arg0, arg1);
3022
3023   /* If both types don't have the same signedness, then we can't consider
3024      them equal.  We must check this before the STRIP_NOPS calls
3025      because they may change the signedness of the arguments.  As pointers
3026      strictly don't have a signedness, require either two pointers or
3027      two non-pointers as well.  */
3028   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3029       || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3030     return 0;
3031
3032   /* If both types don't have the same precision, then it is not safe
3033      to strip NOPs.  */
3034   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3035     return 0;
3036
3037   STRIP_NOPS (arg0);
3038   STRIP_NOPS (arg1);
3039
3040   /* In case both args are comparisons but with different comparison
3041      code, try to swap the comparison operands of one arg to produce
3042      a match and compare that variant.  */
3043   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3044       && COMPARISON_CLASS_P (arg0)
3045       && COMPARISON_CLASS_P (arg1))
3046     {
3047       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3048
3049       if (TREE_CODE (arg0) == swap_code)
3050         return operand_equal_p (TREE_OPERAND (arg0, 0),
3051                                 TREE_OPERAND (arg1, 1), flags)
3052                && operand_equal_p (TREE_OPERAND (arg0, 1),
3053                                    TREE_OPERAND (arg1, 0), flags);
3054     }
3055
3056   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3057       /* This is needed for conversions and for COMPONENT_REF.
3058          Might as well play it safe and always test this.  */
3059       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3060       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3061       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3062     return 0;
3063
3064   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3065      We don't care about side effects in that case because the SAVE_EXPR
3066      takes care of that for us. In all other cases, two expressions are
3067      equal if they have no side effects.  If we have two identical
3068      expressions with side effects that should be treated the same due
3069      to the only side effects being identical SAVE_EXPR's, that will
3070      be detected in the recursive calls below.  */
3071   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3072       && (TREE_CODE (arg0) == SAVE_EXPR
3073           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3074     return 1;
3075
3076   /* Next handle constant cases, those for which we can return 1 even
3077      if ONLY_CONST is set.  */
3078   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3079     switch (TREE_CODE (arg0))
3080       {
3081       case INTEGER_CST:
3082         return tree_int_cst_equal (arg0, arg1);
3083
3084       case FIXED_CST:
3085         return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3086                                        TREE_FIXED_CST (arg1));
3087
3088       case REAL_CST:
3089         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3090                                    TREE_REAL_CST (arg1)))
3091           return 1;
3092
3093         
3094         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3095           {
3096             /* If we do not distinguish between signed and unsigned zero,
3097                consider them equal.  */
3098             if (real_zerop (arg0) && real_zerop (arg1))
3099               return 1;
3100           }
3101         return 0;
3102
3103       case VECTOR_CST:
3104         {
3105           tree v1, v2;
3106
3107           v1 = TREE_VECTOR_CST_ELTS (arg0);
3108           v2 = TREE_VECTOR_CST_ELTS (arg1);
3109           while (v1 && v2)
3110             {
3111               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3112                                     flags))
3113                 return 0;
3114               v1 = TREE_CHAIN (v1);
3115               v2 = TREE_CHAIN (v2);
3116             }
3117
3118           return v1 == v2;
3119         }
3120
3121       case COMPLEX_CST:
3122         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3123                                  flags)
3124                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3125                                     flags));
3126
3127       case STRING_CST:
3128         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3129                 && ! memcmp (TREE_STRING_POINTER (arg0),
3130                               TREE_STRING_POINTER (arg1),
3131                               TREE_STRING_LENGTH (arg0)));
3132
3133       case ADDR_EXPR:
3134         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3135                                 0);
3136       default:
3137         break;
3138       }
3139
3140   if (flags & OEP_ONLY_CONST)
3141     return 0;
3142
3143 /* Define macros to test an operand from arg0 and arg1 for equality and a
3144    variant that allows null and views null as being different from any
3145    non-null value.  In the latter case, if either is null, the both
3146    must be; otherwise, do the normal comparison.  */
3147 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
3148                                     TREE_OPERAND (arg1, N), flags)
3149
3150 #define OP_SAME_WITH_NULL(N)                            \
3151   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3152    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3153
3154   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3155     {
3156     case tcc_unary:
3157       /* Two conversions are equal only if signedness and modes match.  */
3158       switch (TREE_CODE (arg0))
3159         {
3160         CASE_CONVERT:
3161         case FIX_TRUNC_EXPR:
3162           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3163               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3164             return 0;
3165           break;
3166         default:
3167           break;
3168         }
3169
3170       return OP_SAME (0);
3171
3172
3173     case tcc_comparison:
3174     case tcc_binary:
3175       if (OP_SAME (0) && OP_SAME (1))
3176         return 1;
3177
3178       /* For commutative ops, allow the other order.  */
3179       return (commutative_tree_code (TREE_CODE (arg0))
3180               && operand_equal_p (TREE_OPERAND (arg0, 0),
3181                                   TREE_OPERAND (arg1, 1), flags)
3182               && operand_equal_p (TREE_OPERAND (arg0, 1),
3183                                   TREE_OPERAND (arg1, 0), flags));
3184
3185     case tcc_reference:
3186       /* If either of the pointer (or reference) expressions we are
3187          dereferencing contain a side effect, these cannot be equal.  */
3188       if (TREE_SIDE_EFFECTS (arg0)
3189           || TREE_SIDE_EFFECTS (arg1))
3190         return 0;
3191
3192       switch (TREE_CODE (arg0))
3193         {
3194         case INDIRECT_REF:
3195         case ALIGN_INDIRECT_REF:
3196         case MISALIGNED_INDIRECT_REF:
3197         case REALPART_EXPR:
3198         case IMAGPART_EXPR:
3199           return OP_SAME (0);
3200
3201         case ARRAY_REF:
3202         case ARRAY_RANGE_REF:
3203           /* Operands 2 and 3 may be null.
3204              Compare the array index by value if it is constant first as we
3205              may have different types but same value here.  */
3206           return (OP_SAME (0)
3207                   && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3208                                           TREE_OPERAND (arg1, 1))
3209                       || OP_SAME (1))
3210                   && OP_SAME_WITH_NULL (2)
3211                   && OP_SAME_WITH_NULL (3));
3212
3213         case COMPONENT_REF:
3214           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
3215              may be NULL when we're called to compare MEM_EXPRs.  */
3216           return OP_SAME_WITH_NULL (0)
3217                  && OP_SAME (1)
3218                  && OP_SAME_WITH_NULL (2);
3219
3220         case BIT_FIELD_REF:
3221           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3222
3223         default:
3224           return 0;
3225         }
3226
3227     case tcc_expression:
3228       switch (TREE_CODE (arg0))
3229         {
3230         case ADDR_EXPR:
3231         case TRUTH_NOT_EXPR:
3232           return OP_SAME (0);
3233
3234         case TRUTH_ANDIF_EXPR:
3235         case TRUTH_ORIF_EXPR:
3236           return OP_SAME (0) && OP_SAME (1);
3237
3238         case TRUTH_AND_EXPR:
3239         case TRUTH_OR_EXPR:
3240         case TRUTH_XOR_EXPR:
3241           if (OP_SAME (0) && OP_SAME (1))
3242             return 1;
3243
3244           /* Otherwise take into account this is a commutative operation.  */
3245           return (operand_equal_p (TREE_OPERAND (arg0, 0),
3246                                    TREE_OPERAND (arg1, 1), flags)
3247                   && operand_equal_p (TREE_OPERAND (arg0, 1),
3248                                       TREE_OPERAND (arg1, 0), flags));
3249
3250         case COND_EXPR:
3251           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3252           
3253         default:
3254           return 0;
3255         }
3256
3257     case tcc_vl_exp:
3258       switch (TREE_CODE (arg0))
3259         {
3260         case CALL_EXPR:
3261           /* If the CALL_EXPRs call different functions, then they
3262              clearly can not be equal.  */
3263           if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3264                                  flags))
3265             return 0;
3266
3267           {
3268             unsigned int cef = call_expr_flags (arg0);
3269             if (flags & OEP_PURE_SAME)
3270               cef &= ECF_CONST | ECF_PURE;
3271             else
3272               cef &= ECF_CONST;
3273             if (!cef)
3274               return 0;
3275           }
3276
3277           /* Now see if all the arguments are the same.  */
3278           {
3279             const_call_expr_arg_iterator iter0, iter1;
3280             const_tree a0, a1;
3281             for (a0 = first_const_call_expr_arg (arg0, &iter0),
3282                    a1 = first_const_call_expr_arg (arg1, &iter1);
3283                  a0 && a1;
3284                  a0 = next_const_call_expr_arg (&iter0),
3285                    a1 = next_const_call_expr_arg (&iter1))
3286               if (! operand_equal_p (a0, a1, flags))
3287                 return 0;
3288
3289             /* If we get here and both argument lists are exhausted
3290                then the CALL_EXPRs are equal.  */
3291             return ! (a0 || a1);
3292           }
3293         default:
3294           return 0;
3295         }
3296
3297     case tcc_declaration:
3298       /* Consider __builtin_sqrt equal to sqrt.  */
3299       return (TREE_CODE (arg0) == FUNCTION_DECL
3300               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3301               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3302               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3303
3304     default:
3305       return 0;
3306     }
3307
3308 #undef OP_SAME
3309 #undef OP_SAME_WITH_NULL
3310 }
3311 \f
3312 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3313    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3314
3315    When in doubt, return 0.  */
3316
3317 static int
3318 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3319 {
3320   int unsignedp1, unsignedpo;
3321   tree primarg0, primarg1, primother;
3322   unsigned int correct_width;
3323
3324   if (operand_equal_p (arg0, arg1, 0))
3325     return 1;
3326
3327   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3328       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3329     return 0;
3330
3331   /* Discard any conversions that don't change the modes of ARG0 and ARG1
3332      and see if the inner values are the same.  This removes any
3333      signedness comparison, which doesn't matter here.  */
3334   primarg0 = arg0, primarg1 = arg1;
3335   STRIP_NOPS (primarg0);
3336   STRIP_NOPS (primarg1);
3337   if (operand_equal_p (primarg0, primarg1, 0))
3338     return 1;
3339
3340   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3341      actual comparison operand, ARG0.
3342
3343      First throw away any conversions to wider types
3344      already present in the operands.  */
3345
3346   primarg1 = get_narrower (arg1, &unsignedp1);
3347   primother = get_narrower (other, &unsignedpo);
3348
3349   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3350   if (unsignedp1 == unsignedpo
3351       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3352       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3353     {
3354       tree type = TREE_TYPE (arg0);
3355
3356       /* Make sure shorter operand is extended the right way
3357          to match the longer operand.  */
3358       primarg1 = fold_convert (signed_or_unsigned_type_for
3359                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3360
3361       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3362         return 1;
3363     }
3364
3365   return 0;
3366 }
3367 \f
3368 /* See if ARG is an expression that is either a comparison or is performing
3369    arithmetic on comparisons.  The comparisons must only be comparing
3370    two different values, which will be stored in *CVAL1 and *CVAL2; if
3371    they are nonzero it means that some operands have already been found.
3372    No variables may be used anywhere else in the expression except in the
3373    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
3374    the expression and save_expr needs to be called with CVAL1 and CVAL2.
3375
3376    If this is true, return 1.  Otherwise, return zero.  */
3377
3378 static int
3379 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3380 {
3381   enum tree_code code = TREE_CODE (arg);
3382   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3383
3384   /* We can handle some of the tcc_expression cases here.  */
3385   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3386     tclass = tcc_unary;
3387   else if (tclass == tcc_expression
3388            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3389                || code == COMPOUND_EXPR))
3390     tclass = tcc_binary;
3391
3392   else if (tclass == tcc_expression && code == SAVE_EXPR
3393            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3394     {
3395       /* If we've already found a CVAL1 or CVAL2, this expression is
3396          two complex to handle.  */
3397       if (*cval1 || *cval2)
3398         return 0;
3399
3400       tclass = tcc_unary;
3401       *save_p = 1;
3402     }
3403
3404   switch (tclass)
3405     {
3406     case tcc_unary:
3407       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3408
3409     case tcc_binary:
3410       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3411               && twoval_comparison_p (TREE_OPERAND (arg, 1),
3412                                       cval1, cval2, save_p));
3413
3414     case tcc_constant:
3415       return 1;
3416
3417     case tcc_expression:
3418       if (code == COND_EXPR)
3419         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3420                                      cval1, cval2, save_p)
3421                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3422                                         cval1, cval2, save_p)
3423                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3424                                         cval1, cval2, save_p));
3425       return 0;
3426
3427     case tcc_comparison:
3428       /* First see if we can handle the first operand, then the second.  For
3429          the second operand, we know *CVAL1 can't be zero.  It must be that
3430          one side of the comparison is each of the values; test for the
3431          case where this isn't true by failing if the two operands
3432          are the same.  */
3433
3434       if (operand_equal_p (TREE_OPERAND (arg, 0),
3435                            TREE_OPERAND (arg, 1), 0))
3436         return 0;
3437
3438       if (*cval1 == 0)
3439         *cval1 = TREE_OPERAND (arg, 0);
3440       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3441         ;
3442       else if (*cval2 == 0)
3443         *cval2 = TREE_OPERAND (arg, 0);
3444       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3445         ;
3446       else
3447         return 0;
3448
3449       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3450         ;
3451       else if (*cval2 == 0)
3452         *cval2 = TREE_OPERAND (arg, 1);
3453       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3454         ;
3455       else
3456         return 0;
3457
3458       return 1;
3459
3460     default:
3461       return 0;
3462     }
3463 }
3464 \f
3465 /* ARG is a tree that is known to contain just arithmetic operations and
3466    comparisons.  Evaluate the operations in the tree substituting NEW0 for
3467    any occurrence of OLD0 as an operand of a comparison and likewise for
3468    NEW1 and OLD1.  */
3469
3470 static tree
3471 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3472 {
3473   tree type = TREE_TYPE (arg);
3474   enum tree_code code = TREE_CODE (arg);
3475   enum tree_code_class tclass = TREE_CODE_CLASS (code);
3476
3477   /* We can handle some of the tcc_expression cases here.  */
3478   if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3479     tclass = tcc_unary;
3480   else if (tclass == tcc_expression
3481            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3482     tclass = tcc_binary;
3483
3484   switch (tclass)
3485     {
3486     case tcc_unary:
3487       return fold_build1 (code, type,
3488                           eval_subst (TREE_OPERAND (arg, 0),
3489                                       old0, new0, old1, new1));
3490
3491     case tcc_binary:
3492       return fold_build2 (code, type,
3493                           eval_subst (TREE_OPERAND (arg, 0),
3494                                       old0, new0, old1, new1),
3495                           eval_subst (TREE_OPERAND (arg, 1),
3496                                       old0, new0, old1, new1));
3497
3498     case tcc_expression:
3499       switch (code)
3500         {
3501         case SAVE_EXPR:
3502           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3503
3504         case COMPOUND_EXPR:
3505           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3506
3507         case COND_EXPR:
3508           return fold_build3 (code, type,
3509                               eval_subst (TREE_OPERAND (arg, 0),
3510                                           old0, new0, old1, new1),
3511                               eval_subst (TREE_OPERAND (arg, 1),
3512                                           old0, new0, old1, new1),
3513                               eval_subst (TREE_OPERAND (arg, 2),
3514                                           old0, new0, old1, new1));
3515         default:
3516           break;
3517         }
3518       /* Fall through - ???  */
3519
3520     case tcc_comparison:
3521       {
3522         tree arg0 = TREE_OPERAND (arg, 0);
3523         tree arg1 = TREE_OPERAND (arg, 1);
3524
3525         /* We need to check both for exact equality and tree equality.  The
3526            former will be true if the operand has a side-effect.  In that
3527            case, we know the operand occurred exactly once.  */
3528
3529         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3530           arg0 = new0;
3531         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3532           arg0 = new1;
3533
3534         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3535           arg1 = new0;
3536         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3537           arg1 = new1;
3538
3539         return fold_build2 (code, type, arg0, arg1);
3540       }
3541
3542     default:
3543       return arg;
3544     }
3545 }
3546 \f
3547 /* Return a tree for the case when the result of an expression is RESULT
3548    converted to TYPE and OMITTED was previously an operand of the expression
3549    but is now not needed (e.g., we folded OMITTED * 0).
3550
3551    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3552    the conversion of RESULT to TYPE.  */
3553
3554 tree
3555 omit_one_operand (tree type, tree result, tree omitted)
3556 {
3557   tree t = fold_convert (type, result);
3558
3559   /* If the resulting operand is an empty statement, just return the omitted
3560      statement casted to void. */
3561   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3562     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3563
3564   if (TREE_SIDE_EFFECTS (omitted))
3565     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3566
3567   return non_lvalue (t);
3568 }
3569
3570 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3571
3572 static tree
3573 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3574 {
3575   tree t = fold_convert (type, result);
3576
3577   /* If the resulting operand is an empty statement, just return the omitted
3578      statement casted to void. */
3579   if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3580     return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3581
3582   if (TREE_SIDE_EFFECTS (omitted))
3583     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3584
3585   return pedantic_non_lvalue (t);
3586 }
3587
3588 /* Return a tree for the case when the result of an expression is RESULT
3589    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3590    of the expression but are now not needed.
3591
3592    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3593    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3594    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3595    just do the conversion of RESULT to TYPE.  */
3596
3597 tree
3598 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3599 {
3600   tree t = fold_convert (type, result);
3601
3602   if (TREE_SIDE_EFFECTS (omitted2))
3603     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3604   if (TREE_SIDE_EFFECTS (omitted1))
3605     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3606
3607   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3608 }
3609
3610 \f
3611 /* Return a simplified tree node for the truth-negation of ARG.  This
3612    never alters ARG itself.  We assume that ARG is an operation that
3613    returns a truth value (0 or 1).
3614
3615    FIXME: one would think we would fold the result, but it causes
3616    problems with the dominator optimizer.  */
3617
3618 tree
3619 fold_truth_not_expr (tree arg)
3620 {
3621   tree type = TREE_TYPE (arg);
3622   enum tree_code code = TREE_CODE (arg);
3623
3624   /* If this is a comparison, we can simply invert it, except for
3625      floating-point non-equality comparisons, in which case we just
3626      enclose a TRUTH_NOT_EXPR around what we have.  */
3627
3628   if (TREE_CODE_CLASS (code) == tcc_comparison)
3629     {
3630       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3631       if (FLOAT_TYPE_P (op_type)
3632           && flag_trapping_math
3633           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3634           && code != NE_EXPR && code != EQ_EXPR)
3635         return NULL_TREE;
3636       else
3637         {
3638           code = invert_tree_comparison (code,
3639                                          HONOR_NANS (TYPE_MODE (op_type)));
3640           if (code == ERROR_MARK)
3641             return NULL_TREE;
3642           else
3643             return build2 (code, type,
3644                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3645         }
3646     }
3647
3648   switch (code)
3649     {
3650     case INTEGER_CST:
3651       return constant_boolean_node (integer_zerop (arg), type);
3652
3653     case TRUTH_AND_EXPR:
3654       return build2 (TRUTH_OR_EXPR, type,
3655                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3656                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3657
3658     case TRUTH_OR_EXPR:
3659       return build2 (TRUTH_AND_EXPR, type,
3660                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3661                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3662
3663     case TRUTH_XOR_EXPR:
3664       /* Here we can invert either operand.  We invert the first operand
3665          unless the second operand is a TRUTH_NOT_EXPR in which case our
3666          result is the XOR of the first operand with the inside of the
3667          negation of the second operand.  */
3668
3669       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3670         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3671                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3672       else
3673         return build2 (TRUTH_XOR_EXPR, type,
3674                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3675                        TREE_OPERAND (arg, 1));
3676
3677     case TRUTH_ANDIF_EXPR:
3678       return build2 (TRUTH_ORIF_EXPR, type,
3679                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3680                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3681
3682     case TRUTH_ORIF_EXPR:
3683       return build2 (TRUTH_ANDIF_EXPR, type,
3684                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3685                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3686
3687     case TRUTH_NOT_EXPR:
3688       return TREE_OPERAND (arg, 0);
3689
3690     case COND_EXPR:
3691       {
3692         tree arg1 = TREE_OPERAND (arg, 1);
3693         tree arg2 = TREE_OPERAND (arg, 2);
3694         /* A COND_EXPR may have a throw as one operand, which
3695            then has void type.  Just leave void operands
3696            as they are.  */
3697         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3698                        VOID_TYPE_P (TREE_TYPE (arg1))
3699                        ? arg1 : invert_truthvalue (arg1),
3700                        VOID_TYPE_P (TREE_TYPE (arg2))
3701                        ? arg2 : invert_truthvalue (arg2));
3702       }
3703
3704     case COMPOUND_EXPR:
3705       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3706                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3707
3708     case NON_LVALUE_EXPR:
3709       return invert_truthvalue (TREE_OPERAND (arg, 0));
3710
3711     case NOP_EXPR:
3712       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3713         return build1 (TRUTH_NOT_EXPR, type, arg);
3714
3715     case CONVERT_EXPR:
3716     case FLOAT_EXPR:
3717       return build1 (TREE_CODE (arg), type,
3718                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3719
3720     case BIT_AND_EXPR:
3721       if (!integer_onep (TREE_OPERAND (arg, 1)))
3722         break;
3723       return build2 (EQ_EXPR, type, arg,
3724                      build_int_cst (type, 0));
3725
3726     case SAVE_EXPR:
3727       return build1 (TRUTH_NOT_EXPR, type, arg);
3728
3729     case CLEANUP_POINT_EXPR:
3730       return build1 (CLEANUP_POINT_EXPR, type,
3731                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3732
3733     default:
3734       break;
3735     }
3736
3737   return NULL_TREE;
3738 }
3739
3740 /* Return a simplified tree node for the truth-negation of ARG.  This
3741    never alters ARG itself.  We assume that ARG is an operation that
3742    returns a truth value (0 or 1).
3743
3744    FIXME: one would think we would fold the result, but it causes
3745    problems with the dominator optimizer.  */
3746
3747 tree
3748 invert_truthvalue (tree arg)
3749 {
3750   tree tem;
3751
3752   if (TREE_CODE (arg) == ERROR_MARK)
3753     return arg;
3754
3755   tem = fold_truth_not_expr (arg);
3756   if (!tem)
3757     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3758
3759   return tem;
3760 }
3761
3762 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3763    operands are another bit-wise operation with a common input.  If so,
3764    distribute the bit operations to save an operation and possibly two if
3765    constants are involved.  For example, convert
3766         (A | B) & (A | C) into A | (B & C)
3767    Further simplification will occur if B and C are constants.
3768
3769    If this optimization cannot be done, 0 will be returned.  */
3770
3771 static tree
3772 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3773 {
3774   tree common;
3775   tree left, right;
3776
3777   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3778       || TREE_CODE (arg0) == code
3779       || (TREE_CODE (arg0) != BIT_AND_EXPR
3780           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3781     return 0;
3782
3783   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3784     {
3785       common = TREE_OPERAND (arg0, 0);
3786       left = TREE_OPERAND (arg0, 1);
3787       right = TREE_OPERAND (arg1, 1);
3788     }
3789   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3790     {
3791       common = TREE_OPERAND (arg0, 0);
3792       left = TREE_OPERAND (arg0, 1);
3793       right = TREE_OPERAND (arg1, 0);
3794     }
3795   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3796     {
3797       common = TREE_OPERAND (arg0, 1);
3798       left = TREE_OPERAND (arg0, 0);
3799       right = TREE_OPERAND (arg1, 1);
3800     }
3801   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3802     {
3803       common = TREE_OPERAND (arg0, 1);
3804       left = TREE_OPERAND (arg0, 0);
3805       right = TREE_OPERAND (arg1, 0);
3806     }
3807   else
3808     return 0;
3809
3810   common = fold_convert (type, common);
3811   left = fold_convert (type, left);
3812   right = fold_convert (type, right);
3813   return fold_build2 (TREE_CODE (arg0), type, common,
3814                       fold_build2 (code, type, left, right));
3815 }
3816
3817 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3818    with code CODE.  This optimization is unsafe.  */
3819 static tree
3820 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3821 {
3822   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3823   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3824
3825   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3826   if (mul0 == mul1
3827       && operand_equal_p (TREE_OPERAND (arg0, 1),
3828                        TREE_OPERAND (arg1, 1), 0))
3829     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3830                         fold_build2 (code, type,
3831                                      TREE_OPERAND (arg0, 0),
3832                                      TREE_OPERAND (arg1, 0)),
3833                         TREE_OPERAND (arg0, 1));
3834
3835   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3836   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3837                        TREE_OPERAND (arg1, 0), 0)
3838       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3839       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3840     {
3841       REAL_VALUE_TYPE r0, r1;
3842       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3843       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3844       if (!mul0)
3845         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3846       if (!mul1)
3847         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3848       real_arithmetic (&r0, code, &r0, &r1);
3849       return fold_build2 (MULT_EXPR, type,
3850                           TREE_OPERAND (arg0, 0),
3851                           build_real (type, r0));
3852     }
3853
3854   return NULL_TREE;
3855 }
3856 \f
3857 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3858    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3859
3860 static tree
3861 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3862                     HOST_WIDE_INT bitpos, int unsignedp)
3863 {
3864   tree result, bftype;
3865
3866   if (bitpos == 0)
3867     {
3868       tree size = TYPE_SIZE (TREE_TYPE (inner));
3869       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3870            || POINTER_TYPE_P (TREE_TYPE (inner)))
3871           && host_integerp (size, 0) 
3872           && tree_low_cst (size, 0) == bitsize)
3873         return fold_convert (type, inner);
3874     }
3875
3876   bftype = type;
3877   if (TYPE_PRECISION (bftype) != bitsize
3878       || TYPE_UNSIGNED (bftype) == !unsignedp)
3879     bftype = build_nonstandard_integer_type (bitsize, 0);
3880
3881   result = build3 (BIT_FIELD_REF, bftype, inner,
3882                    size_int (bitsize), bitsize_int (bitpos));
3883
3884   if (bftype != type)
3885     result = fold_convert (type, result);
3886
3887   return result;
3888 }
3889
3890 /* Optimize a bit-field compare.
3891
3892    There are two cases:  First is a compare against a constant and the
3893    second is a comparison of two items where the fields are at the same
3894    bit position relative to the start of a chunk (byte, halfword, word)
3895    large enough to contain it.  In these cases we can avoid the shift
3896    implicit in bitfield extractions.
3897
3898    For constants, we emit a compare of the shifted constant with the
3899    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3900    compared.  For two fields at the same position, we do the ANDs with the
3901    similar mask and compare the result of the ANDs.
3902
3903    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3904    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3905    are the left and right operands of the comparison, respectively.
3906
3907    If the optimization described above can be done, we return the resulting
3908    tree.  Otherwise we return zero.  */
3909
3910 static tree
3911 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3912                             tree lhs, tree rhs)
3913 {
3914   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3915   tree type = TREE_TYPE (lhs);
3916   tree signed_type, unsigned_type;
3917   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3918   enum machine_mode lmode, rmode, nmode;
3919   int lunsignedp, runsignedp;
3920   int lvolatilep = 0, rvolatilep = 0;
3921   tree linner, rinner = NULL_TREE;
3922   tree mask;
3923   tree offset;
3924
3925   /* Get all the information about the extractions being done.  If the bit size
3926      if the same as the size of the underlying object, we aren't doing an
3927      extraction at all and so can do nothing.  We also don't want to
3928      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3929      then will no longer be able to replace it.  */
3930   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3931                                 &lunsignedp, &lvolatilep, false);
3932   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3933       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3934     return 0;
3935
3936  if (!const_p)
3937    {
3938      /* If this is not a constant, we can only do something if bit positions,
3939         sizes, and signedness are the same.  */
3940      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3941                                    &runsignedp, &rvolatilep, false);
3942
3943      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3944          || lunsignedp != runsignedp || offset != 0
3945          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3946        return 0;
3947    }
3948
3949   /* See if we can find a mode to refer to this field.  We should be able to,
3950      but fail if we can't.  */
3951   nmode = get_best_mode (lbitsize, lbitpos,
3952                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3953                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3954                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3955                          word_mode, lvolatilep || rvolatilep);
3956   if (nmode == VOIDmode)
3957     return 0;
3958
3959   /* Set signed and unsigned types of the precision of this mode for the
3960      shifts below.  */
3961   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3962   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3963
3964   /* Compute the bit position and size for the new reference and our offset
3965      within it. If the new reference is the same size as the original, we
3966      won't optimize anything, so return zero.  */
3967   nbitsize = GET_MODE_BITSIZE (nmode);
3968   nbitpos = lbitpos & ~ (nbitsize - 1);
3969   lbitpos -= nbitpos;
3970   if (nbitsize == lbitsize)
3971     return 0;
3972
3973   if (BYTES_BIG_ENDIAN)
3974     lbitpos = nbitsize - lbitsize - lbitpos;
3975
3976   /* Make the mask to be used against the extracted field.  */
3977   mask = build_int_cst_type (unsigned_type, -1);
3978   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3979   mask = const_binop (RSHIFT_EXPR, mask,
3980                       size_int (nbitsize - lbitsize - lbitpos), 0);
3981
3982   if (! const_p)
3983     /* If not comparing with constant, just rework the comparison
3984        and return.  */
3985     return fold_build2 (code, compare_type,
3986                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3987                                      make_bit_field_ref (linner,
3988                                                          unsigned_type,
3989                                                          nbitsize, nbitpos,
3990                                                          1),
3991                                      mask),
3992                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3993                                      make_bit_field_ref (rinner,
3994                                                          unsigned_type,
3995                                                          nbitsize, nbitpos,
3996                                                          1),
3997                                      mask));
3998
3999   /* Otherwise, we are handling the constant case. See if the constant is too
4000      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
4001      this not only for its own sake, but to avoid having to test for this
4002      error case below.  If we didn't, we might generate wrong code.
4003
4004      For unsigned fields, the constant shifted right by the field length should
4005      be all zero.  For signed fields, the high-order bits should agree with
4006      the sign bit.  */
4007
4008   if (lunsignedp)
4009     {
4010       if (! integer_zerop (const_binop (RSHIFT_EXPR,
4011                                         fold_convert (unsigned_type, rhs),
4012                                         size_int (lbitsize), 0)))
4013         {
4014           warning (0, "comparison is always %d due to width of bit-field",
4015                    code == NE_EXPR);
4016           return constant_boolean_node (code == NE_EXPR, compare_type);
4017         }
4018     }
4019   else
4020     {
4021       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4022                               size_int (lbitsize - 1), 0);
4023       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4024         {
4025           warning (0, "comparison is always %d due to width of bit-field",
4026                    code == NE_EXPR);
4027           return constant_boolean_node (code == NE_EXPR, compare_type);
4028         }
4029     }
4030
4031   /* Single-bit compares should always be against zero.  */
4032   if (lbitsize == 1 && ! integer_zerop (rhs))
4033     {
4034       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4035       rhs = build_int_cst (type, 0);
4036     }
4037
4038   /* Make a new bitfield reference, shift the constant over the
4039      appropriate number of bits and mask it with the computed mask
4040      (in case this was a signed field).  If we changed it, make a new one.  */
4041   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4042   if (lvolatilep)
4043     {
4044       TREE_SIDE_EFFECTS (lhs) = 1;
4045       TREE_THIS_VOLATILE (lhs) = 1;
4046     }
4047
4048   rhs = const_binop (BIT_AND_EXPR,
4049                      const_binop (LSHIFT_EXPR,
4050                                   fold_convert (unsigned_type, rhs),
4051                                   size_int (lbitpos), 0),
4052                      mask, 0);
4053
4054   return build2 (code, compare_type,
4055                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4056                  rhs);
4057 }
4058 \f
4059 /* Subroutine for fold_truthop: decode a field reference.
4060
4061    If EXP is a comparison reference, we return the innermost reference.
4062
4063    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4064    set to the starting bit number.
4065
4066    If the innermost field can be completely contained in a mode-sized
4067    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
4068
4069    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4070    otherwise it is not changed.
4071
4072    *PUNSIGNEDP is set to the signedness of the field.
4073
4074    *PMASK is set to the mask used.  This is either contained in a
4075    BIT_AND_EXPR or derived from the width of the field.
4076
4077    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4078
4079    Return 0 if this is not a component reference or is one that we can't
4080    do anything with.  */
4081
4082 static tree
4083 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4084                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4085                         int *punsignedp, int *pvolatilep,
4086                         tree *pmask, tree *pand_mask)
4087 {
4088   tree outer_type = 0;
4089   tree and_mask = 0;
4090   tree mask, inner, offset;
4091   tree unsigned_type;
4092   unsigned int precision;
4093
4094   /* All the optimizations using this function assume integer fields.
4095      There are problems with FP fields since the type_for_size call
4096      below can fail for, e.g., XFmode.  */
4097   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4098     return 0;
4099
4100   /* We are interested in the bare arrangement of bits, so strip everything
4101      that doesn't affect the machine mode.  However, record the type of the
4102      outermost expression if it may matter below.  */
4103   if (CONVERT_EXPR_P (exp)
4104       || TREE_CODE (exp) == NON_LVALUE_EXPR)
4105     outer_type = TREE_TYPE (exp);
4106   STRIP_NOPS (exp);
4107
4108   if (TREE_CODE (exp) == BIT_AND_EXPR)
4109     {
4110       and_mask = TREE_OPERAND (exp, 1);
4111       exp = TREE_OPERAND (exp, 0);
4112       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4113       if (TREE_CODE (and_mask) != INTEGER_CST)
4114         return 0;
4115     }
4116
4117   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4118                                punsignedp, pvolatilep, false);
4119   if ((inner == exp && and_mask == 0)
4120       || *pbitsize < 0 || offset != 0
4121       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4122     return 0;
4123
4124   /* If the number of bits in the reference is the same as the bitsize of
4125      the outer type, then the outer type gives the signedness. Otherwise
4126      (in case of a small bitfield) the signedness is unchanged.  */
4127   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4128     *punsignedp = TYPE_UNSIGNED (outer_type);
4129
4130   /* Compute the mask to access the bitfield.  */
4131   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4132   precision = TYPE_PRECISION (unsigned_type);
4133
4134   mask = build_int_cst_type (unsigned_type, -1);
4135
4136   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4137   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4138
4139   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
4140   if (and_mask != 0)
4141     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4142                         fold_convert (unsigned_type, and_mask), mask);
4143
4144   *pmask = mask;
4145   *pand_mask = and_mask;
4146   return inner;
4147 }
4148
4149 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4150    bit positions.  */
4151
4152 static int
4153 all_ones_mask_p (const_tree mask, int size)
4154 {
4155   tree type = TREE_TYPE (mask);
4156   unsigned int precision = TYPE_PRECISION (type);
4157   tree tmask;
4158
4159   tmask = build_int_cst_type (signed_type_for (type), -1);
4160
4161   return
4162     tree_int_cst_equal (mask,
4163                         const_binop (RSHIFT_EXPR,
4164                                      const_binop (LSHIFT_EXPR, tmask,
4165                                                   size_int (precision - size),
4166                                                   0),
4167                                      size_int (precision - size), 0));
4168 }
4169
4170 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4171    represents the sign bit of EXP's type.  If EXP represents a sign
4172    or zero extension, also test VAL against the unextended type.
4173    The return value is the (sub)expression whose sign bit is VAL,
4174    or NULL_TREE otherwise.  */
4175
4176 static tree
4177 sign_bit_p (tree exp, const_tree val)
4178 {
4179   unsigned HOST_WIDE_INT mask_lo, lo;
4180   HOST_WIDE_INT mask_hi, hi;
4181   int width;
4182   tree t;
4183
4184   /* Tree EXP must have an integral type.  */
4185   t = TREE_TYPE (exp);
4186   if (! INTEGRAL_TYPE_P (t))
4187     return NULL_TREE;
4188
4189   /* Tree VAL must be an integer constant.  */
4190   if (TREE_CODE (val) != INTEGER_CST
4191       || TREE_OVERFLOW (val))
4192     return NULL_TREE;
4193
4194   width = TYPE_PRECISION (t);
4195   if (width > HOST_BITS_PER_WIDE_INT)
4196     {
4197       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4198       lo = 0;
4199
4200       mask_hi = ((unsigned HOST_WIDE_INT) -1
4201                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
4202       mask_lo = -1;
4203     }
4204   else
4205     {
4206       hi = 0;
4207       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4208
4209       mask_hi = 0;
4210       mask_lo = ((unsigned HOST_WIDE_INT) -1
4211                  >> (HOST_BITS_PER_WIDE_INT - width));
4212     }
4213
4214   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4215      treat VAL as if it were unsigned.  */
4216   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4217       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4218     return exp;
4219
4220   /* Handle extension from a narrower type.  */
4221   if (TREE_CODE (exp) == NOP_EXPR
4222       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4223     return sign_bit_p (TREE_OPERAND (exp, 0), val);
4224
4225   return NULL_TREE;
4226 }
4227
4228 /* Subroutine for fold_truthop: determine if an operand is simple enough
4229    to be evaluated unconditionally.  */
4230
4231 static int
4232 simple_operand_p (const_tree exp)
4233 {
4234   /* Strip any conversions that don't change the machine mode.  */
4235   STRIP_NOPS (exp);
4236
4237   return (CONSTANT_CLASS_P (exp)
4238           || TREE_CODE (exp) == SSA_NAME
4239           || (DECL_P (exp)
4240               && ! TREE_ADDRESSABLE (exp)
4241               && ! TREE_THIS_VOLATILE (exp)
4242               && ! DECL_NONLOCAL (exp)
4243               /* Don't regard global variables as simple.  They may be
4244                  allocated in ways unknown to the compiler (shared memory,
4245                  #pragma weak, etc).  */
4246               && ! TREE_PUBLIC (exp)
4247               && ! DECL_EXTERNAL (exp)
4248               /* Loading a static variable is unduly expensive, but global
4249                  registers aren't expensive.  */
4250               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4251 }
4252 \f
4253 /* The following functions are subroutines to fold_range_test and allow it to
4254    try to change a logical combination of comparisons into a range test.
4255
4256    For example, both
4257         X == 2 || X == 3 || X == 4 || X == 5
4258    and
4259         X >= 2 && X <= 5
4260    are converted to
4261         (unsigned) (X - 2) <= 3
4262
4263    We describe each set of comparisons as being either inside or outside
4264    a range, using a variable named like IN_P, and then describe the
4265    range with a lower and upper bound.  If one of the bounds is omitted,
4266    it represents either the highest or lowest value of the type.
4267
4268    In the comments below, we represent a range by two numbers in brackets
4269    preceded by a "+" to designate being inside that range, or a "-" to
4270    designate being outside that range, so the condition can be inverted by
4271    flipping the prefix.  An omitted bound is represented by a "-".  For
4272    example, "- [-, 10]" means being outside the range starting at the lowest
4273    possible value and ending at 10, in other words, being greater than 10.
4274    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4275    always false.
4276
4277    We set up things so that the missing bounds are handled in a consistent
4278    manner so neither a missing bound nor "true" and "false" need to be
4279    handled using a special case.  */
4280
4281 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4282    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4283    and UPPER1_P are nonzero if the respective argument is an upper bound
4284    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
4285    must be specified for a comparison.  ARG1 will be converted to ARG0's
4286    type if both are specified.  */
4287
4288 static tree
4289 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4290              tree arg1, int upper1_p)
4291 {
4292   tree tem;
4293   int result;
4294   int sgn0, sgn1;
4295
4296   /* If neither arg represents infinity, do the normal operation.
4297      Else, if not a comparison, return infinity.  Else handle the special
4298      comparison rules. Note that most of the cases below won't occur, but
4299      are handled for consistency.  */
4300
4301   if (arg0 != 0 && arg1 != 0)
4302     {
4303       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4304                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
4305       STRIP_NOPS (tem);
4306       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4307     }
4308
4309   if (TREE_CODE_CLASS (code) != tcc_comparison)
4310     return 0;
4311
4312   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4313      for neither.  In real maths, we cannot assume open ended ranges are
4314      the same. But, this is computer arithmetic, where numbers are finite.
4315      We can therefore make the transformation of any unbounded range with
4316      the value Z, Z being greater than any representable number. This permits
4317      us to treat unbounded ranges as equal.  */
4318   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4319   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4320   switch (code)
4321     {
4322     case EQ_EXPR:
4323       result = sgn0 == sgn1;
4324       break;
4325     case NE_EXPR:
4326       result = sgn0 != sgn1;
4327       break;
4328     case LT_EXPR:
4329       result = sgn0 < sgn1;
4330       break;
4331     case LE_EXPR:
4332       result = sgn0 <= sgn1;
4333       break;
4334     case GT_EXPR:
4335       result = sgn0 > sgn1;
4336       break;
4337     case GE_EXPR:
4338       result = sgn0 >= sgn1;
4339       break;
4340     default:
4341       gcc_unreachable ();
4342     }
4343
4344   return constant_boolean_node (result, type);
4345 }
4346 \f
4347 /* Given EXP, a logical expression, set the range it is testing into
4348    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
4349    actually being tested.  *PLOW and *PHIGH will be made of the same
4350    type as the returned expression.  If EXP is not a comparison, we
4351    will most likely not be returning a useful value and range.  Set
4352    *STRICT_OVERFLOW_P to true if the return value is only valid
4353    because signed overflow is undefined; otherwise, do not change
4354    *STRICT_OVERFLOW_P.  */
4355
4356 static tree
4357 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4358             bool *strict_overflow_p)
4359 {
4360   enum tree_code code;
4361   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4362   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4363   int in_p, n_in_p;
4364   tree low, high, n_low, n_high;
4365
4366   /* Start with simply saying "EXP != 0" and then look at the code of EXP
4367      and see if we can refine the range.  Some of the cases below may not
4368      happen, but it doesn't seem worth worrying about this.  We "continue"
4369      the outer loop when we've changed something; otherwise we "break"
4370      the switch, which will "break" the while.  */
4371
4372   in_p = 0;
4373   low = high = build_int_cst (TREE_TYPE (exp), 0);
4374
4375   while (1)
4376     {
4377       code = TREE_CODE (exp);
4378       exp_type = TREE_TYPE (exp);
4379
4380       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4381         {
4382           if (TREE_OPERAND_LENGTH (exp) > 0)
4383             arg0 = TREE_OPERAND (exp, 0);
4384           if (TREE_CODE_CLASS (code) == tcc_comparison
4385               || TREE_CODE_CLASS (code) == tcc_unary
4386               || TREE_CODE_CLASS (code) == tcc_binary)
4387             arg0_type = TREE_TYPE (arg0);
4388           if (TREE_CODE_CLASS (code) == tcc_binary
4389               || TREE_CODE_CLASS (code) == tcc_comparison
4390               || (TREE_CODE_CLASS (code) == tcc_expression
4391                   && TREE_OPERAND_LENGTH (exp) > 1))
4392             arg1 = TREE_OPERAND (exp, 1);
4393         }
4394
4395       switch (code)
4396         {
4397         case TRUTH_NOT_EXPR:
4398           in_p = ! in_p, exp = arg0;
4399           continue;
4400
4401         case EQ_EXPR: case NE_EXPR:
4402         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4403           /* We can only do something if the range is testing for zero
4404              and if the second operand is an integer constant.  Note that
4405              saying something is "in" the range we make is done by
4406              complementing IN_P since it will set in the initial case of
4407              being not equal to zero; "out" is leaving it alone.  */
4408           if (low == 0 || high == 0
4409               || ! integer_zerop (low) || ! integer_zerop (high)
4410               || TREE_CODE (arg1) != INTEGER_CST)
4411             break;
4412
4413           switch (code)
4414             {
4415             case NE_EXPR:  /* - [c, c]  */
4416               low = high = arg1;
4417               break;
4418             case EQ_EXPR:  /* + [c, c]  */
4419               in_p = ! in_p, low = high = arg1;
4420               break;
4421             case GT_EXPR:  /* - [-, c] */
4422               low = 0, high = arg1;
4423               break;
4424             case GE_EXPR:  /* + [c, -] */
4425               in_p = ! in_p, low = arg1, high = 0;
4426               break;
4427             case LT_EXPR:  /* - [c, -] */
4428               low = arg1, high = 0;
4429               break;
4430             case LE_EXPR:  /* + [-, c] */
4431               in_p = ! in_p, low = 0, high = arg1;
4432               break;
4433             default:
4434               gcc_unreachable ();
4435             }
4436
4437           /* If this is an unsigned comparison, we also know that EXP is
4438              greater than or equal to zero.  We base the range tests we make
4439              on that fact, so we record it here so we can parse existing
4440              range tests.  We test arg0_type since often the return type
4441              of, e.g. EQ_EXPR, is boolean.  */
4442           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4443             {
4444               if (! merge_ranges (&n_in_p, &n_low, &n_high,
4445                                   in_p, low, high, 1,
4446                                   build_int_cst (arg0_type, 0),
4447                                   NULL_TREE))
4448                 break;
4449
4450               in_p = n_in_p, low = n_low, high = n_high;
4451
4452               /* If the high bound is missing, but we have a nonzero low
4453                  bound, reverse the range so it goes from zero to the low bound
4454                  minus 1.  */
4455               if (high == 0 && low && ! integer_zerop (low))
4456                 {
4457                   in_p = ! in_p;
4458                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4459                                       integer_one_node, 0);
4460                   low = build_int_cst (arg0_type, 0);
4461                 }
4462             }
4463
4464           exp = arg0;
4465           continue;
4466
4467         case NEGATE_EXPR:
4468           /* (-x) IN [a,b] -> x in [-b, -a]  */
4469           n_low = range_binop (MINUS_EXPR, exp_type,
4470                                build_int_cst (exp_type, 0),
4471                                0, high, 1);
4472           n_high = range_binop (MINUS_EXPR, exp_type,
4473                                 build_int_cst (exp_type, 0),
4474                                 0, low, 0);
4475           low = n_low, high = n_high;
4476           exp = arg0;
4477           continue;
4478
4479         case BIT_NOT_EXPR:
4480           /* ~ X -> -X - 1  */
4481           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4482                         build_int_cst (exp_type, 1));
4483           continue;
4484
4485         case PLUS_EXPR:  case MINUS_EXPR:
4486           if (TREE_CODE (arg1) != INTEGER_CST)
4487             break;
4488
4489           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4490              move a constant to the other side.  */
4491           if (!TYPE_UNSIGNED (arg0_type)
4492               && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4493             break;
4494
4495           /* If EXP is signed, any overflow in the computation is undefined,
4496              so we don't worry about it so long as our computations on
4497              the bounds don't overflow.  For unsigned, overflow is defined
4498              and this is exactly the right thing.  */
4499           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4500                                arg0_type, low, 0, arg1, 0);
4501           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4502                                 arg0_type, high, 1, arg1, 0);
4503           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4504               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4505             break;
4506
4507           if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4508             *strict_overflow_p = true;
4509
4510           /* Check for an unsigned range which has wrapped around the maximum
4511              value thus making n_high < n_low, and normalize it.  */
4512           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4513             {
4514               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4515                                  integer_one_node, 0);
4516               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4517                                   integer_one_node, 0);
4518
4519               /* If the range is of the form +/- [ x+1, x ], we won't
4520                  be able to normalize it.  But then, it represents the
4521                  whole range or the empty set, so make it
4522                  +/- [ -, - ].  */
4523               if (tree_int_cst_equal (n_low, low)
4524                   && tree_int_cst_equal (n_high, high))
4525                 low = high = 0;
4526               else
4527                 in_p = ! in_p;
4528             }
4529           else
4530             low = n_low, high = n_high;
4531
4532           exp = arg0;
4533           continue;
4534
4535         CASE_CONVERT: case NON_LVALUE_EXPR:
4536           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4537             break;
4538
4539           if (! INTEGRAL_TYPE_P (arg0_type)
4540               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4541               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4542             break;
4543
4544           n_low = low, n_high = high;
4545
4546           if (n_low != 0)
4547             n_low = fold_convert (arg0_type, n_low);
4548
4549           if (n_high != 0)
4550             n_high = fold_convert (arg0_type, n_high);
4551
4552
4553           /* If we're converting arg0 from an unsigned type, to exp,
4554              a signed type,  we will be doing the comparison as unsigned.
4555              The tests above have already verified that LOW and HIGH
4556              are both positive.
4557
4558              So we have to ensure that we will handle large unsigned
4559              values the same way that the current signed bounds treat
4560              negative values.  */
4561
4562           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4563             {
4564               tree high_positive;
4565               tree equiv_type;
4566               /* For fixed-point modes, we need to pass the saturating flag
4567                  as the 2nd parameter.  */
4568               if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4569                 equiv_type = lang_hooks.types.type_for_mode
4570                              (TYPE_MODE (arg0_type),
4571                               TYPE_SATURATING (arg0_type));
4572               else
4573                 equiv_type = lang_hooks.types.type_for_mode
4574                              (TYPE_MODE (arg0_type), 1);
4575
4576               /* A range without an upper bound is, naturally, unbounded.
4577                  Since convert would have cropped a very large value, use
4578                  the max value for the destination type.  */
4579               high_positive
4580                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4581                 : TYPE_MAX_VALUE (arg0_type);
4582
4583               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4584                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4585                                              fold_convert (arg0_type,
4586                                                            high_positive),
4587                                              build_int_cst (arg0_type, 1));
4588
4589               /* If the low bound is specified, "and" the range with the
4590                  range for which the original unsigned value will be
4591                  positive.  */
4592               if (low != 0)
4593                 {
4594                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4595                                       1, n_low, n_high, 1,
4596                                       fold_convert (arg0_type,
4597                                                     integer_zero_node),
4598                                       high_positive))
4599                     break;
4600
4601                   in_p = (n_in_p == in_p);
4602                 }
4603               else
4604                 {
4605                   /* Otherwise, "or" the range with the range of the input
4606                      that will be interpreted as negative.  */
4607                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4608                                       0, n_low, n_high, 1,
4609                                       fold_convert (arg0_type,
4610                                                     integer_zero_node),
4611                                       high_positive))
4612                     break;
4613
4614                   in_p = (in_p != n_in_p);
4615                 }
4616             }
4617
4618           exp = arg0;
4619           low = n_low, high = n_high;
4620           continue;
4621
4622         default:
4623           break;
4624         }
4625
4626       break;
4627     }
4628
4629   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4630   if (TREE_CODE (exp) == INTEGER_CST)
4631     {
4632       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4633                                                  exp, 0, low, 0))
4634                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4635                                                     exp, 1, high, 1)));
4636       low = high = 0;
4637       exp = 0;
4638     }
4639
4640   *pin_p = in_p, *plow = low, *phigh = high;
4641   return exp;
4642 }
4643 \f
4644 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4645    type, TYPE, return an expression to test if EXP is in (or out of, depending
4646    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4647
4648 static tree
4649 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4650 {
4651   tree etype = TREE_TYPE (exp);
4652   tree value;
4653
4654 #ifdef HAVE_canonicalize_funcptr_for_compare
4655   /* Disable this optimization for function pointer expressions
4656      on targets that require function pointer canonicalization.  */
4657   if (HAVE_canonicalize_funcptr_for_compare
4658       && TREE_CODE (etype) == POINTER_TYPE
4659       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4660     return NULL_TREE;
4661 #endif
4662
4663   if (! in_p)
4664     {
4665       value = build_range_check (type, exp, 1, low, high);
4666       if (value != 0)
4667         return invert_truthvalue (value);
4668
4669       return 0;
4670     }
4671
4672   if (low == 0 && high == 0)
4673     return build_int_cst (type, 1);
4674
4675   if (low == 0)
4676     return fold_build2 (LE_EXPR, type, exp,
4677                         fold_convert (etype, high));
4678
4679   if (high == 0)
4680     return fold_build2 (GE_EXPR, type, exp,
4681                         fold_convert (etype, low));
4682
4683   if (operand_equal_p (low, high, 0))
4684     return fold_build2 (EQ_EXPR, type, exp,
4685                         fold_convert (etype, low));
4686
4687   if (integer_zerop (low))
4688     {
4689       if (! TYPE_UNSIGNED (etype))
4690         {
4691           etype = unsigned_type_for (etype);
4692           high = fold_convert (etype, high);
4693           exp = fold_convert (etype, exp);
4694         }
4695       return build_range_check (type, exp, 1, 0, high);
4696     }
4697
4698   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4699   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4700     {
4701       unsigned HOST_WIDE_INT lo;
4702       HOST_WIDE_INT hi;
4703       int prec;
4704
4705       prec = TYPE_PRECISION (etype);
4706       if (prec <= HOST_BITS_PER_WIDE_INT)
4707         {
4708           hi = 0;
4709           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4710         }
4711       else
4712         {
4713           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4714           lo = (unsigned HOST_WIDE_INT) -1;
4715         }
4716
4717       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4718         {
4719           if (TYPE_UNSIGNED (etype))
4720             {
4721               tree signed_etype = signed_type_for (etype);
4722               if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4723                 etype
4724                   = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4725               else
4726                 etype = signed_etype;
4727               exp = fold_convert (etype, exp);
4728             }
4729           return fold_build2 (GT_EXPR, type, exp,
4730                               build_int_cst (etype, 0));
4731         }
4732     }
4733
4734   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4735      This requires wrap-around arithmetics for the type of the expression.  */
4736   switch (TREE_CODE (etype))
4737     {
4738     case INTEGER_TYPE:
4739       /* There is no requirement that LOW be within the range of ETYPE
4740          if the latter is a subtype.  It must, however, be within the base
4741          type of ETYPE.  So be sure we do the subtraction in that type.  */
4742       if (TREE_TYPE (etype))
4743         etype = TREE_TYPE (etype);
4744       break;
4745
4746     case ENUMERAL_TYPE:
4747     case BOOLEAN_TYPE:
4748       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4749                                               TYPE_UNSIGNED (etype));
4750       break;
4751
4752     default:
4753       break;
4754     }
4755
4756   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4757   if (TREE_CODE (etype) == INTEGER_TYPE
4758       && !TYPE_OVERFLOW_WRAPS (etype))
4759     {
4760       tree utype, minv, maxv;
4761
4762       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4763          for the type in question, as we rely on this here.  */
4764       utype = unsigned_type_for (etype);
4765       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4766       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4767                           integer_one_node, 1);
4768       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4769
4770       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4771                                       minv, 1, maxv, 1)))
4772         etype = utype;
4773       else
4774         return 0;
4775     }
4776
4777   high = fold_convert (etype, high);
4778   low = fold_convert (etype, low);
4779   exp = fold_convert (etype, exp);
4780
4781   value = const_binop (MINUS_EXPR, high, low, 0);
4782
4783
4784   if (POINTER_TYPE_P (etype))
4785     {
4786       if (value != 0 && !TREE_OVERFLOW (value))
4787         {
4788           low = fold_convert (sizetype, low);
4789           low = fold_build1 (NEGATE_EXPR, sizetype, low);
4790           return build_range_check (type,
4791                                     fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4792                                     1, build_int_cst (etype, 0), value);
4793         }
4794       return 0;
4795     }
4796
4797   if (value != 0 && !TREE_OVERFLOW (value))
4798     return build_range_check (type,
4799                               fold_build2 (MINUS_EXPR, etype, exp, low),
4800                               1, build_int_cst (etype, 0), value);
4801
4802   return 0;
4803 }
4804 \f
4805 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4806
4807 static tree
4808 range_predecessor (tree val)
4809 {
4810   tree type = TREE_TYPE (val);
4811
4812   if (INTEGRAL_TYPE_P (type)
4813       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4814     return 0;
4815   else
4816     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4817 }
4818
4819 /* Return the successor of VAL in its type, handling the infinite case.  */
4820
4821 static tree
4822 range_successor (tree val)
4823 {
4824   tree type = TREE_TYPE (val);
4825
4826   if (INTEGRAL_TYPE_P (type)
4827       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4828     return 0;
4829   else
4830     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4831 }
4832
4833 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4834    can, 0 if we can't.  Set the output range into the specified parameters.  */
4835
4836 static int
4837 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4838               tree high0, int in1_p, tree low1, tree high1)
4839 {
4840   int no_overlap;
4841   int subset;
4842   int temp;
4843   tree tem;
4844   int in_p;
4845   tree low, high;
4846   int lowequal = ((low0 == 0 && low1 == 0)
4847                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4848                                                 low0, 0, low1, 0)));
4849   int highequal = ((high0 == 0 && high1 == 0)
4850                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4851                                                  high0, 1, high1, 1)));
4852
4853   /* Make range 0 be the range that starts first, or ends last if they
4854      start at the same value.  Swap them if it isn't.  */
4855   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4856                                  low0, 0, low1, 0))
4857       || (lowequal
4858           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4859                                         high1, 1, high0, 1))))
4860     {
4861       temp = in0_p, in0_p = in1_p, in1_p = temp;
4862       tem = low0, low0 = low1, low1 = tem;
4863       tem = high0, high0 = high1, high1 = tem;
4864     }
4865
4866   /* Now flag two cases, whether the ranges are disjoint or whether the
4867      second range is totally subsumed in the first.  Note that the tests
4868      below are simplified by the ones above.  */
4869   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4870                                           high0, 1, low1, 0));
4871   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4872                                       high1, 1, high0, 1));
4873
4874   /* We now have four cases, depending on whether we are including or
4875      excluding the two ranges.  */
4876   if (in0_p && in1_p)
4877     {
4878       /* If they don't overlap, the result is false.  If the second range
4879          is a subset it is the result.  Otherwise, the range is from the start
4880          of the second to the end of the first.  */
4881       if (no_overlap)
4882         in_p = 0, low = high = 0;
4883       else if (subset)
4884         in_p = 1, low = low1, high = high1;
4885       else
4886         in_p = 1, low = low1, high = high0;
4887     }
4888
4889   else if (in0_p && ! in1_p)
4890     {
4891       /* If they don't overlap, the result is the first range.  If they are
4892          equal, the result is false.  If the second range is a subset of the
4893          first, and the ranges begin at the same place, we go from just after
4894          the end of the second range to the end of the first.  If the second
4895          range is not a subset of the first, or if it is a subset and both
4896          ranges end at the same place, the range starts at the start of the
4897          first range and ends just before the second range.
4898          Otherwise, we can't describe this as a single range.  */
4899       if (no_overlap)
4900         in_p = 1, low = low0, high = high0;
4901       else if (lowequal && highequal)
4902         in_p = 0, low = high = 0;
4903       else if (subset && lowequal)
4904         {
4905           low = range_successor (high1);
4906           high = high0;
4907           in_p = 1;
4908           if (low == 0)
4909             {
4910               /* We are in the weird situation where high0 > high1 but
4911                  high1 has no successor.  Punt.  */
4912               return 0;
4913             }
4914         }
4915       else if (! subset || highequal)
4916         {
4917           low = low0;
4918           high = range_predecessor (low1);
4919           in_p = 1;
4920           if (high == 0)
4921             {
4922               /* low0 < low1 but low1 has no predecessor.  Punt.  */
4923               return 0;
4924             }
4925         }
4926       else
4927         return 0;
4928     }
4929
4930   else if (! in0_p && in1_p)
4931     {
4932       /* If they don't overlap, the result is the second range.  If the second
4933          is a subset of the first, the result is false.  Otherwise,
4934          the range starts just after the first range and ends at the
4935          end of the second.  */
4936       if (no_overlap)
4937         in_p = 1, low = low1, high = high1;
4938       else if (subset || highequal)
4939         in_p = 0, low = high = 0;
4940       else
4941         {
4942           low = range_successor (high0);
4943           high = high1;
4944           in_p = 1;
4945           if (low == 0)
4946             {
4947               /* high1 > high0 but high0 has no successor.  Punt.  */
4948               return 0;
4949             }
4950         }
4951     }
4952
4953   else
4954     {
4955       /* The case where we are excluding both ranges.  Here the complex case
4956          is if they don't overlap.  In that case, the only time we have a
4957          range is if they are adjacent.  If the second is a subset of the
4958          first, the result is the first.  Otherwise, the range to exclude
4959          starts at the beginning of the first range and ends at the end of the
4960          second.  */
4961       if (no_overlap)
4962         {
4963           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4964                                          range_successor (high0),
4965                                          1, low1, 0)))
4966             in_p = 0, low = low0, high = high1;
4967           else
4968             {
4969               /* Canonicalize - [min, x] into - [-, x].  */
4970               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4971                 switch (TREE_CODE (TREE_TYPE (low0)))
4972                   {
4973                   case ENUMERAL_TYPE:
4974                     if (TYPE_PRECISION (TREE_TYPE (low0))
4975                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4976                       break;
4977                     /* FALLTHROUGH */
4978                   case INTEGER_TYPE:
4979                     if (tree_int_cst_equal (low0,
4980                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4981                       low0 = 0;
4982                     break;
4983                   case POINTER_TYPE:
4984                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4985                         && integer_zerop (low0))
4986                       low0 = 0;
4987                     break;
4988                   default:
4989                     break;
4990                   }
4991
4992               /* Canonicalize - [x, max] into - [x, -].  */
4993               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4994                 switch (TREE_CODE (TREE_TYPE (high1)))
4995                   {
4996                   case ENUMERAL_TYPE:
4997                     if (TYPE_PRECISION (TREE_TYPE (high1))
4998                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4999                       break;
5000                     /* FALLTHROUGH */
5001                   case INTEGER_TYPE:
5002                     if (tree_int_cst_equal (high1,
5003                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
5004                       high1 = 0;
5005                     break;
5006                   case POINTER_TYPE:
5007                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
5008                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5009                                                        high1, 1,
5010                                                        integer_one_node, 1)))
5011                       high1 = 0;
5012                     break;
5013                   default:
5014                     break;
5015                   }
5016
5017               /* The ranges might be also adjacent between the maximum and
5018                  minimum values of the given type.  For
5019                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5020                  return + [x + 1, y - 1].  */
5021               if (low0 == 0 && high1 == 0)
5022                 {
5023                   low = range_successor (high0);
5024                   high = range_predecessor (low1);
5025                   if (low == 0 || high == 0)
5026                     return 0;
5027
5028                   in_p = 1;
5029                 }
5030               else
5031                 return 0;
5032             }
5033         }
5034       else if (subset)
5035         in_p = 0, low = low0, high = high0;
5036       else
5037         in_p = 0, low = low0, high = high1;
5038     }
5039
5040   *pin_p = in_p, *plow = low, *phigh = high;
5041   return 1;
5042 }
5043 \f
5044
5045 /* Subroutine of fold, looking inside expressions of the form
5046    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5047    of the COND_EXPR.  This function is being used also to optimize
5048    A op B ? C : A, by reversing the comparison first.
5049
5050    Return a folded expression whose code is not a COND_EXPR
5051    anymore, or NULL_TREE if no folding opportunity is found.  */
5052
5053 static tree
5054 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5055 {
5056   enum tree_code comp_code = TREE_CODE (arg0);
5057   tree arg00 = TREE_OPERAND (arg0, 0);
5058   tree arg01 = TREE_OPERAND (arg0, 1);
5059   tree arg1_type = TREE_TYPE (arg1);
5060   tree tem;
5061
5062   STRIP_NOPS (arg1);
5063   STRIP_NOPS (arg2);
5064
5065   /* If we have A op 0 ? A : -A, consider applying the following
5066      transformations:
5067
5068      A == 0? A : -A    same as -A
5069      A != 0? A : -A    same as A
5070      A >= 0? A : -A    same as abs (A)
5071      A > 0?  A : -A    same as abs (A)
5072      A <= 0? A : -A    same as -abs (A)
5073      A < 0?  A : -A    same as -abs (A)
5074
5075      None of these transformations work for modes with signed
5076      zeros.  If A is +/-0, the first two transformations will
5077      change the sign of the result (from +0 to -0, or vice
5078      versa).  The last four will fix the sign of the result,
5079      even though the original expressions could be positive or
5080      negative, depending on the sign of A.
5081
5082      Note that all these transformations are correct if A is
5083      NaN, since the two alternatives (A and -A) are also NaNs.  */
5084   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5085       && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5086           ? real_zerop (arg01)
5087           : integer_zerop (arg01))
5088       && ((TREE_CODE (arg2) == NEGATE_EXPR
5089            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5090              /* In the case that A is of the form X-Y, '-A' (arg2) may
5091                 have already been folded to Y-X, check for that. */
5092           || (TREE_CODE (arg1) == MINUS_EXPR
5093               && TREE_CODE (arg2) == MINUS_EXPR
5094               && operand_equal_p (TREE_OPERAND (arg1, 0),
5095                                   TREE_OPERAND (arg2, 1), 0)
5096               && operand_equal_p (TREE_OPERAND (arg1, 1),
5097                                   TREE_OPERAND (arg2, 0), 0))))
5098     switch (comp_code)
5099       {
5100       case EQ_EXPR:
5101       case UNEQ_EXPR:
5102         tem = fold_convert (arg1_type, arg1);
5103         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5104       case NE_EXPR:
5105       case LTGT_EXPR:
5106         return pedantic_non_lvalue (fold_convert (type, arg1));
5107       case UNGE_EXPR:
5108       case UNGT_EXPR:
5109         if (flag_trapping_math)
5110           break;
5111         /* Fall through.  */
5112       case GE_EXPR:
5113       case GT_EXPR:
5114         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5115           arg1 = fold_convert (signed_type_for
5116                                (TREE_TYPE (arg1)), arg1);
5117         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5118         return pedantic_non_lvalue (fold_convert (type, tem));
5119       case UNLE_EXPR:
5120       case UNLT_EXPR:
5121         if (flag_trapping_math)
5122           break;
5123       case LE_EXPR:
5124       case LT_EXPR:
5125         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5126           arg1 = fold_convert (signed_type_for
5127                                (TREE_TYPE (arg1)), arg1);
5128         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5129         return negate_expr (fold_convert (type, tem));
5130       default:
5131         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5132         break;
5133       }
5134
5135   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
5136      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
5137      both transformations are correct when A is NaN: A != 0
5138      is then true, and A == 0 is false.  */
5139
5140   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5141       && integer_zerop (arg01) && integer_zerop (arg2))
5142     {
5143       if (comp_code == NE_EXPR)
5144         return pedantic_non_lvalue (fold_convert (type, arg1));
5145       else if (comp_code == EQ_EXPR)
5146         return build_int_cst (type, 0);
5147     }
5148
5149   /* Try some transformations of A op B ? A : B.
5150
5151      A == B? A : B    same as B
5152      A != B? A : B    same as A
5153      A >= B? A : B    same as max (A, B)
5154      A > B?  A : B    same as max (B, A)
5155      A <= B? A : B    same as min (A, B)
5156      A < B?  A : B    same as min (B, A)
5157
5158      As above, these transformations don't work in the presence
5159      of signed zeros.  For example, if A and B are zeros of
5160      opposite sign, the first two transformations will change
5161      the sign of the result.  In the last four, the original
5162      expressions give different results for (A=+0, B=-0) and
5163      (A=-0, B=+0), but the transformed expressions do not.
5164
5165      The first two transformations are correct if either A or B
5166      is a NaN.  In the first transformation, the condition will
5167      be false, and B will indeed be chosen.  In the case of the
5168      second transformation, the condition A != B will be true,
5169      and A will be chosen.
5170
5171      The conversions to max() and min() are not correct if B is
5172      a number and A is not.  The conditions in the original
5173      expressions will be false, so all four give B.  The min()
5174      and max() versions would give a NaN instead.  */
5175   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5176       && operand_equal_for_comparison_p (arg01, arg2, arg00)
5177       /* Avoid these transformations if the COND_EXPR may be used
5178          as an lvalue in the C++ front-end.  PR c++/19199.  */
5179       && (in_gimple_form
5180           || (strcmp (lang_hooks.name, "GNU C++") != 0
5181               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5182           || ! maybe_lvalue_p (arg1)
5183           || ! maybe_lvalue_p (arg2)))
5184     {
5185       tree comp_op0 = arg00;
5186       tree comp_op1 = arg01;
5187       tree comp_type = TREE_TYPE (comp_op0);
5188
5189       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
5190       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5191         {
5192           comp_type = type;
5193           comp_op0 = arg1;
5194           comp_op1 = arg2;
5195         }
5196
5197       switch (comp_code)
5198         {
5199         case EQ_EXPR:
5200           return pedantic_non_lvalue (fold_convert (type, arg2));
5201         case NE_EXPR:
5202           return pedantic_non_lvalue (fold_convert (type, arg1));
5203         case LE_EXPR:
5204         case LT_EXPR:
5205         case UNLE_EXPR:
5206         case UNLT_EXPR:
5207           /* In C++ a ?: expression can be an lvalue, so put the
5208              operand which will be used if they are equal first
5209              so that we can convert this back to the
5210              corresponding COND_EXPR.  */
5211           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5212             {
5213               comp_op0 = fold_convert (comp_type, comp_op0);
5214               comp_op1 = fold_convert (comp_type, comp_op1);
5215               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5216                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5217                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5218               return pedantic_non_lvalue (fold_convert (type, tem));
5219             }
5220           break;
5221         case GE_EXPR:
5222         case GT_EXPR:
5223         case UNGE_EXPR:
5224         case UNGT_EXPR:
5225           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5226             {
5227               comp_op0 = fold_convert (comp_type, comp_op0);
5228               comp_op1 = fold_convert (comp_type, comp_op1);
5229               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5230                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5231                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5232               return pedantic_non_lvalue (fold_convert (type, tem));
5233             }
5234           break;
5235         case UNEQ_EXPR:
5236           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5237             return pedantic_non_lvalue (fold_convert (type, arg2));
5238           break;
5239         case LTGT_EXPR:
5240           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5241             return pedantic_non_lvalue (fold_convert (type, arg1));
5242           break;
5243         default:
5244           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5245           break;
5246         }
5247     }
5248
5249   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5250      we might still be able to simplify this.  For example,
5251      if C1 is one less or one more than C2, this might have started
5252      out as a MIN or MAX and been transformed by this function.
5253      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5254
5255   if (INTEGRAL_TYPE_P (type)
5256       && TREE_CODE (arg01) == INTEGER_CST
5257       && TREE_CODE (arg2) == INTEGER_CST)
5258     switch (comp_code)
5259       {
5260       case EQ_EXPR:
5261         /* We can replace A with C1 in this case.  */
5262         arg1 = fold_convert (type, arg01);
5263         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5264
5265       case LT_EXPR:
5266         /* If C1 is C2 + 1, this is min(A, C2).  */
5267         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5268                                OEP_ONLY_CONST)
5269             && operand_equal_p (arg01,
5270                                 const_binop (PLUS_EXPR, arg2,
5271                                              build_int_cst (type, 1), 0),
5272                                 OEP_ONLY_CONST))
5273           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5274                                                    type,
5275                                                    fold_convert (type, arg1),
5276                                                    arg2));
5277         break;
5278
5279       case LE_EXPR:
5280         /* If C1 is C2 - 1, this is min(A, C2).  */
5281         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5282                                OEP_ONLY_CONST)
5283             && operand_equal_p (arg01,
5284                                 const_binop (MINUS_EXPR, arg2,
5285                                              build_int_cst (type, 1), 0),
5286                                 OEP_ONLY_CONST))
5287           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5288                                                    type,
5289                                                    fold_convert (type, arg1),
5290                                                    arg2));
5291         break;
5292
5293       case GT_EXPR:
5294         /* If C1 is C2 - 1, this is max(A, C2).  */
5295         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5296                                OEP_ONLY_CONST)
5297             && operand_equal_p (arg01,
5298                                 const_binop (MINUS_EXPR, arg2,
5299                                              build_int_cst (type, 1), 0),
5300                                 OEP_ONLY_CONST))
5301           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5302                                                    type,
5303                                                    fold_convert (type, arg1),
5304                                                    arg2));
5305         break;
5306
5307       case GE_EXPR:
5308         /* If C1 is C2 + 1, this is max(A, C2).  */
5309         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5310                                OEP_ONLY_CONST)
5311             && operand_equal_p (arg01,
5312                                 const_binop (PLUS_EXPR, arg2,
5313                                              build_int_cst (type, 1), 0),
5314                                 OEP_ONLY_CONST))
5315           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5316                                                    type,
5317                                                    fold_convert (type, arg1),
5318                                                    arg2));
5319         break;
5320       case NE_EXPR:
5321         break;
5322       default:
5323         gcc_unreachable ();
5324       }
5325
5326   return NULL_TREE;
5327 }
5328
5329
5330 \f
5331 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5332 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5333   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5334                 false) >= 2)
5335 #endif
5336
5337 /* EXP is some logical combination of boolean tests.  See if we can
5338    merge it into some range test.  Return the new tree if so.  */
5339
5340 static tree
5341 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5342 {
5343   int or_op = (code == TRUTH_ORIF_EXPR
5344                || code == TRUTH_OR_EXPR);
5345   int in0_p, in1_p, in_p;
5346   tree low0, low1, low, high0, high1, high;
5347   bool strict_overflow_p = false;
5348   tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5349   tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5350   tree tem;
5351   const char * const warnmsg = G_("assuming signed overflow does not occur "
5352                                   "when simplifying range test");
5353
5354   /* If this is an OR operation, invert both sides; we will invert
5355      again at the end.  */
5356   if (or_op)
5357     in0_p = ! in0_p, in1_p = ! in1_p;
5358
5359   /* If both expressions are the same, if we can merge the ranges, and we
5360      can build the range test, return it or it inverted.  If one of the
5361      ranges is always true or always false, consider it to be the same
5362      expression as the other.  */
5363   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5364       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5365                        in1_p, low1, high1)
5366       && 0 != (tem = (build_range_check (type,
5367                                          lhs != 0 ? lhs
5368                                          : rhs != 0 ? rhs : integer_zero_node,
5369                                          in_p, low, high))))
5370     {
5371       if (strict_overflow_p)
5372         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5373       return or_op ? invert_truthvalue (tem) : tem;
5374     }
5375
5376   /* On machines where the branch cost is expensive, if this is a
5377      short-circuited branch and the underlying object on both sides
5378      is the same, make a non-short-circuit operation.  */
5379   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5380            && lhs != 0 && rhs != 0
5381            && (code == TRUTH_ANDIF_EXPR
5382                || code == TRUTH_ORIF_EXPR)
5383            && operand_equal_p (lhs, rhs, 0))
5384     {
5385       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
5386          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5387          which cases we can't do this.  */
5388       if (simple_operand_p (lhs))
5389         return build2 (code == TRUTH_ANDIF_EXPR
5390                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5391                        type, op0, op1);
5392
5393       else if (lang_hooks.decls.global_bindings_p () == 0
5394                && ! CONTAINS_PLACEHOLDER_P (lhs))
5395         {
5396           tree common = save_expr (lhs);
5397
5398           if (0 != (lhs = build_range_check (type, common,
5399                                              or_op ? ! in0_p : in0_p,
5400                                              low0, high0))
5401               && (0 != (rhs = build_range_check (type, common,
5402                                                  or_op ? ! in1_p : in1_p,
5403                                                  low1, high1))))
5404             {
5405               if (strict_overflow_p)
5406                 fold_overflow_warning (warnmsg,
5407                                        WARN_STRICT_OVERFLOW_COMPARISON);
5408               return build2 (code == TRUTH_ANDIF_EXPR
5409                              ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5410                              type, lhs, rhs);
5411             }
5412         }
5413     }
5414
5415   return 0;
5416 }
5417 \f
5418 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5419    bit value.  Arrange things so the extra bits will be set to zero if and
5420    only if C is signed-extended to its full width.  If MASK is nonzero,
5421    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
5422
5423 static tree
5424 unextend (tree c, int p, int unsignedp, tree mask)
5425 {
5426   tree type = TREE_TYPE (c);
5427   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5428   tree temp;
5429
5430   if (p == modesize || unsignedp)
5431     return c;
5432
5433   /* We work by getting just the sign bit into the low-order bit, then
5434      into the high-order bit, then sign-extend.  We then XOR that value
5435      with C.  */
5436   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5437   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5438
5439   /* We must use a signed type in order to get an arithmetic right shift.
5440      However, we must also avoid introducing accidental overflows, so that
5441      a subsequent call to integer_zerop will work.  Hence we must
5442      do the type conversion here.  At this point, the constant is either
5443      zero or one, and the conversion to a signed type can never overflow.
5444      We could get an overflow if this conversion is done anywhere else.  */
5445   if (TYPE_UNSIGNED (type))
5446     temp = fold_convert (signed_type_for (type), temp);
5447
5448   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5449   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5450   if (mask != 0)
5451     temp = const_binop (BIT_AND_EXPR, temp,
5452                         fold_convert (TREE_TYPE (c), mask), 0);
5453   /* If necessary, convert the type back to match the type of C.  */
5454   if (TYPE_UNSIGNED (type))
5455     temp = fold_convert (type, temp);
5456
5457   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5458 }
5459 \f
5460 /* Find ways of folding logical expressions of LHS and RHS:
5461    Try to merge two comparisons to the same innermost item.
5462    Look for range tests like "ch >= '0' && ch <= '9'".
5463    Look for combinations of simple terms on machines with expensive branches
5464    and evaluate the RHS unconditionally.
5465
5466    For example, if we have p->a == 2 && p->b == 4 and we can make an
5467    object large enough to span both A and B, we can do this with a comparison
5468    against the object ANDed with the a mask.
5469
5470    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5471    operations to do this with one comparison.
5472
5473    We check for both normal comparisons and the BIT_AND_EXPRs made this by
5474    function and the one above.
5475
5476    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
5477    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5478
5479    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5480    two operands.
5481
5482    We return the simplified tree or 0 if no optimization is possible.  */
5483
5484 static tree
5485 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5486 {
5487   /* If this is the "or" of two comparisons, we can do something if
5488      the comparisons are NE_EXPR.  If this is the "and", we can do something
5489      if the comparisons are EQ_EXPR.  I.e.,
5490         (a->b == 2 && a->c == 4) can become (a->new == NEW).
5491
5492      WANTED_CODE is this operation code.  For single bit fields, we can
5493      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5494      comparison for one-bit fields.  */
5495
5496   enum tree_code wanted_code;
5497   enum tree_code lcode, rcode;
5498   tree ll_arg, lr_arg, rl_arg, rr_arg;
5499   tree ll_inner, lr_inner, rl_inner, rr_inner;
5500   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5501   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5502   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5503   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5504   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5505   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5506   enum machine_mode lnmode, rnmode;
5507   tree ll_mask, lr_mask, rl_mask, rr_mask;
5508   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5509   tree l_const, r_const;
5510   tree lntype, rntype, result;
5511   HOST_WIDE_INT first_bit, end_bit;
5512   int volatilep;
5513   tree orig_lhs = lhs, orig_rhs = rhs;
5514   enum tree_code orig_code = code;
5515
5516   /* Start by getting the comparison codes.  Fail if anything is volatile.
5517      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5518      it were surrounded with a NE_EXPR.  */
5519
5520   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5521     return 0;
5522
5523   lcode = TREE_CODE (lhs);
5524   rcode = TREE_CODE (rhs);
5525
5526   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5527     {
5528       lhs = build2 (NE_EXPR, truth_type, lhs,
5529                     build_int_cst (TREE_TYPE (lhs), 0));
5530       lcode = NE_EXPR;
5531     }
5532
5533   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5534     {
5535       rhs = build2 (NE_EXPR, truth_type, rhs,
5536                     build_int_cst (TREE_TYPE (rhs), 0));
5537       rcode = NE_EXPR;
5538     }
5539
5540   if (TREE_CODE_CLASS (lcode) != tcc_comparison
5541       || TREE_CODE_CLASS (rcode) != tcc_comparison)
5542     return 0;
5543
5544   ll_arg = TREE_OPERAND (lhs, 0);
5545   lr_arg = TREE_OPERAND (lhs, 1);
5546   rl_arg = TREE_OPERAND (rhs, 0);
5547   rr_arg = TREE_OPERAND (rhs, 1);
5548
5549   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
5550   if (simple_operand_p (ll_arg)
5551       && simple_operand_p (lr_arg))
5552     {
5553       tree result;
5554       if (operand_equal_p (ll_arg, rl_arg, 0)
5555           && operand_equal_p (lr_arg, rr_arg, 0))
5556         {
5557           result = combine_comparisons (code, lcode, rcode,
5558                                         truth_type, ll_arg, lr_arg);
5559           if (result)
5560             return result;
5561         }
5562       else if (operand_equal_p (ll_arg, rr_arg, 0)
5563                && operand_equal_p (lr_arg, rl_arg, 0))
5564         {
5565           result = combine_comparisons (code, lcode,
5566                                         swap_tree_comparison (rcode),
5567                                         truth_type, ll_arg, lr_arg);
5568           if (result)
5569             return result;
5570         }
5571     }
5572
5573   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5574           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5575
5576   /* If the RHS can be evaluated unconditionally and its operands are
5577      simple, it wins to evaluate the RHS unconditionally on machines
5578      with expensive branches.  In this case, this isn't a comparison
5579      that can be merged.  Avoid doing this if the RHS is a floating-point
5580      comparison since those can trap.  */
5581
5582   if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5583                    false) >= 2
5584       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5585       && simple_operand_p (rl_arg)
5586       && simple_operand_p (rr_arg))
5587     {
5588       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5589       if (code == TRUTH_OR_EXPR
5590           && lcode == NE_EXPR && integer_zerop (lr_arg)
5591           && rcode == NE_EXPR && integer_zerop (rr_arg)
5592           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5593           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5594         return build2 (NE_EXPR, truth_type,
5595                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5596                                ll_arg, rl_arg),
5597                        build_int_cst (TREE_TYPE (ll_arg), 0));
5598
5599       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5600       if (code == TRUTH_AND_EXPR
5601           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5602           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5603           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5604           && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5605         return build2 (EQ_EXPR, truth_type,
5606                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5607                                ll_arg, rl_arg),
5608                        build_int_cst (TREE_TYPE (ll_arg), 0));
5609
5610       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5611         {
5612           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5613             return build2 (code, truth_type, lhs, rhs);
5614           return NULL_TREE;
5615         }
5616     }
5617
5618   /* See if the comparisons can be merged.  Then get all the parameters for
5619      each side.  */
5620
5621   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5622       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5623     return 0;
5624
5625   volatilep = 0;
5626   ll_inner = decode_field_reference (ll_arg,
5627                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5628                                      &ll_unsignedp, &volatilep, &ll_mask,
5629                                      &ll_and_mask);
5630   lr_inner = decode_field_reference (lr_arg,
5631                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5632                                      &lr_unsignedp, &volatilep, &lr_mask,
5633                                      &lr_and_mask);
5634   rl_inner = decode_field_reference (rl_arg,
5635                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5636                                      &rl_unsignedp, &volatilep, &rl_mask,
5637                                      &rl_and_mask);
5638   rr_inner = decode_field_reference (rr_arg,
5639                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5640                                      &rr_unsignedp, &volatilep, &rr_mask,
5641                                      &rr_and_mask);
5642
5643   /* It must be true that the inner operation on the lhs of each
5644      comparison must be the same if we are to be able to do anything.
5645      Then see if we have constants.  If not, the same must be true for
5646      the rhs's.  */
5647   if (volatilep || ll_inner == 0 || rl_inner == 0
5648       || ! operand_equal_p (ll_inner, rl_inner, 0))
5649     return 0;
5650
5651   if (TREE_CODE (lr_arg) == INTEGER_CST
5652       && TREE_CODE (rr_arg) == INTEGER_CST)
5653     l_const = lr_arg, r_const = rr_arg;
5654   else if (lr_inner == 0 || rr_inner == 0
5655            || ! operand_equal_p (lr_inner, rr_inner, 0))
5656     return 0;
5657   else
5658     l_const = r_const = 0;
5659
5660   /* If either comparison code is not correct for our logical operation,
5661      fail.  However, we can convert a one-bit comparison against zero into
5662      the opposite comparison against that bit being set in the field.  */
5663
5664   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5665   if (lcode != wanted_code)
5666     {
5667       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5668         {
5669           /* Make the left operand unsigned, since we are only interested
5670              in the value of one bit.  Otherwise we are doing the wrong
5671              thing below.  */
5672           ll_unsignedp = 1;
5673           l_const = ll_mask;
5674         }
5675       else
5676         return 0;
5677     }
5678
5679   /* This is analogous to the code for l_const above.  */
5680   if (rcode != wanted_code)
5681     {
5682       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5683         {
5684           rl_unsignedp = 1;
5685           r_const = rl_mask;
5686         }
5687       else
5688         return 0;
5689     }
5690
5691   /* See if we can find a mode that contains both fields being compared on
5692      the left.  If we can't, fail.  Otherwise, update all constants and masks
5693      to be relative to a field of that size.  */
5694   first_bit = MIN (ll_bitpos, rl_bitpos);
5695   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5696   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5697                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5698                           volatilep);
5699   if (lnmode == VOIDmode)
5700     return 0;
5701
5702   lnbitsize = GET_MODE_BITSIZE (lnmode);
5703   lnbitpos = first_bit & ~ (lnbitsize - 1);
5704   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5705   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5706
5707   if (BYTES_BIG_ENDIAN)
5708     {
5709       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5710       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5711     }
5712
5713   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5714                          size_int (xll_bitpos), 0);
5715   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5716                          size_int (xrl_bitpos), 0);
5717
5718   if (l_const)
5719     {
5720       l_const = fold_convert (lntype, l_const);
5721       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5722       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5723       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5724                                         fold_build1 (BIT_NOT_EXPR,
5725                                                      lntype, ll_mask),
5726                                         0)))
5727         {
5728           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5729
5730           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5731         }
5732     }
5733   if (r_const)
5734     {
5735       r_const = fold_convert (lntype, r_const);
5736       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5737       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5738       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5739                                         fold_build1 (BIT_NOT_EXPR,
5740                                                      lntype, rl_mask),
5741                                         0)))
5742         {
5743           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5744
5745           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5746         }
5747     }
5748
5749   /* If the right sides are not constant, do the same for it.  Also,
5750      disallow this optimization if a size or signedness mismatch occurs
5751      between the left and right sides.  */
5752   if (l_const == 0)
5753     {
5754       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5755           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5756           /* Make sure the two fields on the right
5757              correspond to the left without being swapped.  */
5758           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5759         return 0;
5760
5761       first_bit = MIN (lr_bitpos, rr_bitpos);
5762       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5763       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5764                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5765                               volatilep);
5766       if (rnmode == VOIDmode)
5767         return 0;
5768
5769       rnbitsize = GET_MODE_BITSIZE (rnmode);
5770       rnbitpos = first_bit & ~ (rnbitsize - 1);
5771       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5772       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5773
5774       if (BYTES_BIG_ENDIAN)
5775         {
5776           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5777           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5778         }
5779
5780       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5781                              size_int (xlr_bitpos), 0);
5782       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5783                              size_int (xrr_bitpos), 0);
5784
5785       /* Make a mask that corresponds to both fields being compared.
5786          Do this for both items being compared.  If the operands are the
5787          same size and the bits being compared are in the same position
5788          then we can do this by masking both and comparing the masked
5789          results.  */
5790       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5791       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5792       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5793         {
5794           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5795                                     ll_unsignedp || rl_unsignedp);
5796           if (! all_ones_mask_p (ll_mask, lnbitsize))
5797             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5798
5799           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5800                                     lr_unsignedp || rr_unsignedp);
5801           if (! all_ones_mask_p (lr_mask, rnbitsize))
5802             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5803
5804           return build2 (wanted_code, truth_type, lhs, rhs);
5805         }
5806
5807       /* There is still another way we can do something:  If both pairs of
5808          fields being compared are adjacent, we may be able to make a wider
5809          field containing them both.
5810
5811          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5812          the mask must be shifted to account for the shift done by
5813          make_bit_field_ref.  */
5814       if ((ll_bitsize + ll_bitpos == rl_bitpos
5815            && lr_bitsize + lr_bitpos == rr_bitpos)
5816           || (ll_bitpos == rl_bitpos + rl_bitsize
5817               && lr_bitpos == rr_bitpos + rr_bitsize))
5818         {
5819           tree type;
5820
5821           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5822                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5823           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5824                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5825
5826           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5827                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5828           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5829                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5830
5831           /* Convert to the smaller type before masking out unwanted bits.  */
5832           type = lntype;
5833           if (lntype != rntype)
5834             {
5835               if (lnbitsize > rnbitsize)
5836                 {
5837                   lhs = fold_convert (rntype, lhs);
5838                   ll_mask = fold_convert (rntype, ll_mask);
5839                   type = rntype;
5840                 }
5841               else if (lnbitsize < rnbitsize)
5842                 {
5843                   rhs = fold_convert (lntype, rhs);
5844                   lr_mask = fold_convert (lntype, lr_mask);
5845                   type = lntype;
5846                 }
5847             }
5848
5849           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5850             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5851
5852           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5853             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5854
5855           return build2 (wanted_code, truth_type, lhs, rhs);
5856         }
5857
5858       return 0;
5859     }
5860
5861   /* Handle the case of comparisons with constants.  If there is something in
5862      common between the masks, those bits of the constants must be the same.
5863      If not, the condition is always false.  Test for this to avoid generating
5864      incorrect code below.  */
5865   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5866   if (! integer_zerop (result)
5867       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5868                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5869     {
5870       if (wanted_code == NE_EXPR)
5871         {
5872           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5873           return constant_boolean_node (true, truth_type);
5874         }
5875       else
5876         {
5877           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5878           return constant_boolean_node (false, truth_type);
5879         }
5880     }
5881
5882   /* Construct the expression we will return.  First get the component
5883      reference we will make.  Unless the mask is all ones the width of
5884      that field, perform the mask operation.  Then compare with the
5885      merged constant.  */
5886   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5887                                ll_unsignedp || rl_unsignedp);
5888
5889   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5890   if (! all_ones_mask_p (ll_mask, lnbitsize))
5891     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5892
5893   return build2 (wanted_code, truth_type, result,
5894                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5895 }
5896 \f
5897 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5898    constant.  */
5899
5900 static tree
5901 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5902 {
5903   tree arg0 = op0;
5904   enum tree_code op_code;
5905   tree comp_const;
5906   tree minmax_const;
5907   int consts_equal, consts_lt;
5908   tree inner;
5909
5910   STRIP_SIGN_NOPS (arg0);
5911
5912   op_code = TREE_CODE (arg0);
5913   minmax_const = TREE_OPERAND (arg0, 1);
5914   comp_const = fold_convert (TREE_TYPE (arg0), op1);
5915   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5916   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5917   inner = TREE_OPERAND (arg0, 0);
5918
5919   /* If something does not permit us to optimize, return the original tree.  */
5920   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5921       || TREE_CODE (comp_const) != INTEGER_CST
5922       || TREE_OVERFLOW (comp_const)
5923       || TREE_CODE (minmax_const) != INTEGER_CST
5924       || TREE_OVERFLOW (minmax_const))
5925     return NULL_TREE;
5926
5927   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5928      and GT_EXPR, doing the rest with recursive calls using logical
5929      simplifications.  */
5930   switch (code)
5931     {
5932     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5933       {
5934         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5935                                           type, op0, op1);
5936         if (tem)
5937           return invert_truthvalue (tem);
5938         return NULL_TREE;
5939       }
5940
5941     case GE_EXPR:
5942       return
5943         fold_build2 (TRUTH_ORIF_EXPR, type,
5944                      optimize_minmax_comparison
5945                      (EQ_EXPR, type, arg0, comp_const),
5946                      optimize_minmax_comparison
5947                      (GT_EXPR, type, arg0, comp_const));
5948
5949     case EQ_EXPR:
5950       if (op_code == MAX_EXPR && consts_equal)
5951         /* MAX (X, 0) == 0  ->  X <= 0  */
5952         return fold_build2 (LE_EXPR, type, inner, comp_const);
5953
5954       else if (op_code == MAX_EXPR && consts_lt)
5955         /* MAX (X, 0) == 5  ->  X == 5   */
5956         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5957
5958       else if (op_code == MAX_EXPR)
5959         /* MAX (X, 0) == -1  ->  false  */
5960         return omit_one_operand (type, integer_zero_node, inner);
5961
5962       else if (consts_equal)
5963         /* MIN (X, 0) == 0  ->  X >= 0  */
5964         return fold_build2 (GE_EXPR, type, inner, comp_const);
5965
5966       else if (consts_lt)
5967         /* MIN (X, 0) == 5  ->  false  */
5968         return omit_one_operand (type, integer_zero_node, inner);
5969
5970       else
5971         /* MIN (X, 0) == -1  ->  X == -1  */
5972         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5973
5974     case GT_EXPR:
5975       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5976         /* MAX (X, 0) > 0  ->  X > 0
5977            MAX (X, 0) > 5  ->  X > 5  */
5978         return fold_build2 (GT_EXPR, type, inner, comp_const);
5979
5980       else if (op_code == MAX_EXPR)
5981         /* MAX (X, 0) > -1  ->  true  */
5982         return omit_one_operand (type, integer_one_node, inner);
5983
5984       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5985         /* MIN (X, 0) > 0  ->  false
5986            MIN (X, 0) > 5  ->  false  */
5987         return omit_one_operand (type, integer_zero_node, inner);
5988
5989       else
5990         /* MIN (X, 0) > -1  ->  X > -1  */
5991         return fold_build2 (GT_EXPR, type, inner, comp_const);
5992
5993     default:
5994       return NULL_TREE;
5995     }
5996 }
5997 \f
5998 /* T is an integer expression that is being multiplied, divided, or taken a
5999    modulus (CODE says which and what kind of divide or modulus) by a
6000    constant C.  See if we can eliminate that operation by folding it with
6001    other operations already in T.  WIDE_TYPE, if non-null, is a type that
6002    should be used for the computation if wider than our type.
6003
6004    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6005    (X * 2) + (Y * 4).  We must, however, be assured that either the original
6006    expression would not overflow or that overflow is undefined for the type
6007    in the language in question.
6008
6009    If we return a non-null expression, it is an equivalent form of the
6010    original computation, but need not be in the original type.
6011
6012    We set *STRICT_OVERFLOW_P to true if the return values depends on
6013    signed overflow being undefined.  Otherwise we do not change
6014    *STRICT_OVERFLOW_P.  */
6015
6016 static tree
6017 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6018                 bool *strict_overflow_p)
6019 {
6020   /* To avoid exponential search depth, refuse to allow recursion past
6021      three levels.  Beyond that (1) it's highly unlikely that we'll find
6022      something interesting and (2) we've probably processed it before
6023      when we built the inner expression.  */
6024
6025   static int depth;
6026   tree ret;
6027
6028   if (depth > 3)
6029     return NULL;
6030
6031   depth++;
6032   ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6033   depth--;
6034
6035   return ret;
6036 }
6037
6038 static tree
6039 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6040                   bool *strict_overflow_p)
6041 {
6042   tree type = TREE_TYPE (t);
6043   enum tree_code tcode = TREE_CODE (t);
6044   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6045                                    > GET_MODE_SIZE (TYPE_MODE (type)))
6046                 ? wide_type : type);
6047   tree t1, t2;
6048   int same_p = tcode == code;
6049   tree op0 = NULL_TREE, op1 = NULL_TREE;
6050   bool sub_strict_overflow_p;
6051
6052   /* Don't deal with constants of zero here; they confuse the code below.  */
6053   if (integer_zerop (c))
6054     return NULL_TREE;
6055
6056   if (TREE_CODE_CLASS (tcode) == tcc_unary)
6057     op0 = TREE_OPERAND (t, 0);
6058
6059   if (TREE_CODE_CLASS (tcode) == tcc_binary)
6060     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6061
6062   /* Note that we need not handle conditional operations here since fold
6063      already handles those cases.  So just do arithmetic here.  */
6064   switch (tcode)
6065     {
6066     case INTEGER_CST:
6067       /* For a constant, we can always simplify if we are a multiply
6068          or (for divide and modulus) if it is a multiple of our constant.  */
6069       if (code == MULT_EXPR
6070           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6071         return const_binop (code, fold_convert (ctype, t),
6072                             fold_convert (ctype, c), 0);
6073       break;
6074
6075     CASE_CONVERT: case NON_LVALUE_EXPR:
6076       /* If op0 is an expression ...  */
6077       if ((COMPARISON_CLASS_P (op0)
6078            || UNARY_CLASS_P (op0)
6079            || BINARY_CLASS_P (op0)
6080            || VL_EXP_CLASS_P (op0)
6081            || EXPRESSION_CLASS_P (op0))
6082           /* ... and has wrapping overflow, and its type is smaller
6083              than ctype, then we cannot pass through as widening.  */
6084           && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6085                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6086                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6087                && (TYPE_PRECISION (ctype)
6088                    > TYPE_PRECISION (TREE_TYPE (op0))))
6089               /* ... or this is a truncation (t is narrower than op0),
6090                  then we cannot pass through this narrowing.  */
6091               || (TYPE_PRECISION (type)
6092                   < TYPE_PRECISION (TREE_TYPE (op0)))
6093               /* ... or signedness changes for division or modulus,
6094                  then we cannot pass through this conversion.  */
6095               || (code != MULT_EXPR
6096                   && (TYPE_UNSIGNED (ctype)
6097                       != TYPE_UNSIGNED (TREE_TYPE (op0))))
6098               /* ... or has undefined overflow while the converted to
6099                  type has not, we cannot do the operation in the inner type
6100                  as that would introduce undefined overflow.  */
6101               || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6102                   && !TYPE_OVERFLOW_UNDEFINED (type))))
6103         break;
6104
6105       /* Pass the constant down and see if we can make a simplification.  If
6106          we can, replace this expression with the inner simplification for
6107          possible later conversion to our or some other type.  */
6108       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6109           && TREE_CODE (t2) == INTEGER_CST
6110           && !TREE_OVERFLOW (t2)
6111           && (0 != (t1 = extract_muldiv (op0, t2, code,
6112                                          code == MULT_EXPR
6113                                          ? ctype : NULL_TREE,
6114                                          strict_overflow_p))))
6115         return t1;
6116       break;
6117
6118     case ABS_EXPR:
6119       /* If widening the type changes it from signed to unsigned, then we
6120          must avoid building ABS_EXPR itself as unsigned.  */
6121       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6122         {
6123           tree cstype = (*signed_type_for) (ctype);
6124           if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6125               != 0)
6126             {
6127               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6128               return fold_convert (ctype, t1);
6129             }
6130           break;
6131         }
6132       /* If the constant is negative, we cannot simplify this.  */
6133       if (tree_int_cst_sgn (c) == -1)
6134         break;
6135       /* FALLTHROUGH */
6136     case NEGATE_EXPR:
6137       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6138           != 0)
6139         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6140       break;
6141
6142     case MIN_EXPR:  case MAX_EXPR:
6143       /* If widening the type changes the signedness, then we can't perform
6144          this optimization as that changes the result.  */
6145       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6146         break;
6147
6148       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
6149       sub_strict_overflow_p = false;
6150       if ((t1 = extract_muldiv (op0, c, code, wide_type,
6151                                 &sub_strict_overflow_p)) != 0
6152           && (t2 = extract_muldiv (op1, c, code, wide_type,
6153                                    &sub_strict_overflow_p)) != 0)
6154         {
6155           if (tree_int_cst_sgn (c) < 0)
6156             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6157           if (sub_strict_overflow_p)
6158             *strict_overflow_p = true;
6159           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6160                               fold_convert (ctype, t2));
6161         }
6162       break;
6163
6164     case LSHIFT_EXPR:  case RSHIFT_EXPR:
6165       /* If the second operand is constant, this is a multiplication
6166          or floor division, by a power of two, so we can treat it that
6167          way unless the multiplier or divisor overflows.  Signed
6168          left-shift overflow is implementation-defined rather than
6169          undefined in C90, so do not convert signed left shift into
6170          multiplication.  */
6171       if (TREE_CODE (op1) == INTEGER_CST
6172           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6173           /* const_binop may not detect overflow correctly,
6174              so check for it explicitly here.  */
6175           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6176           && TREE_INT_CST_HIGH (op1) == 0
6177           && 0 != (t1 = fold_convert (ctype,
6178                                       const_binop (LSHIFT_EXPR,
6179                                                    size_one_node,
6180                                                    op1, 0)))
6181           && !TREE_OVERFLOW (t1))
6182         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6183                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
6184                                        ctype, fold_convert (ctype, op0), t1),
6185                                c, code, wide_type, strict_overflow_p);
6186       break;
6187
6188     case PLUS_EXPR:  case MINUS_EXPR:
6189       /* See if we can eliminate the operation on both sides.  If we can, we
6190          can return a new PLUS or MINUS.  If we can't, the only remaining
6191          cases where we can do anything are if the second operand is a
6192          constant.  */
6193       sub_strict_overflow_p = false;
6194       t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6195       t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6196       if (t1 != 0 && t2 != 0
6197           && (code == MULT_EXPR
6198               /* If not multiplication, we can only do this if both operands
6199                  are divisible by c.  */
6200               || (multiple_of_p (ctype, op0, c)
6201                   && multiple_of_p (ctype, op1, c))))
6202         {
6203           if (sub_strict_overflow_p)
6204             *strict_overflow_p = true;
6205           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6206                               fold_convert (ctype, t2));
6207         }
6208
6209       /* If this was a subtraction, negate OP1 and set it to be an addition.
6210          This simplifies the logic below.  */
6211       if (tcode == MINUS_EXPR)
6212         tcode = PLUS_EXPR, op1 = negate_expr (op1);
6213
6214       if (TREE_CODE (op1) != INTEGER_CST)
6215         break;
6216
6217       /* If either OP1 or C are negative, this optimization is not safe for
6218          some of the division and remainder types while for others we need
6219          to change the code.  */
6220       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6221         {
6222           if (code == CEIL_DIV_EXPR)
6223             code = FLOOR_DIV_EXPR;
6224           else if (code == FLOOR_DIV_EXPR)
6225             code = CEIL_DIV_EXPR;
6226           else if (code != MULT_EXPR
6227                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6228             break;
6229         }
6230
6231       /* If it's a multiply or a division/modulus operation of a multiple
6232          of our constant, do the operation and verify it doesn't overflow.  */
6233       if (code == MULT_EXPR
6234           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6235         {
6236           op1 = const_binop (code, fold_convert (ctype, op1),
6237                              fold_convert (ctype, c), 0);
6238           /* We allow the constant to overflow with wrapping semantics.  */
6239           if (op1 == 0
6240               || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6241             break;
6242         }
6243       else
6244         break;
6245
6246       /* If we have an unsigned type is not a sizetype, we cannot widen
6247          the operation since it will change the result if the original
6248          computation overflowed.  */
6249       if (TYPE_UNSIGNED (ctype)
6250           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6251           && ctype != type)
6252         break;
6253
6254       /* If we were able to eliminate our operation from the first side,
6255          apply our operation to the second side and reform the PLUS.  */
6256       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6257         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6258
6259       /* The last case is if we are a multiply.  In that case, we can
6260          apply the distributive law to commute the multiply and addition
6261          if the multiplication of the constants doesn't overflow.  */
6262       if (code == MULT_EXPR)
6263         return fold_build2 (tcode, ctype,
6264                             fold_build2 (code, ctype,
6265                                          fold_convert (ctype, op0),
6266                                          fold_convert (ctype, c)),
6267                             op1);
6268
6269       break;
6270
6271     case MULT_EXPR:
6272       /* We have a special case here if we are doing something like
6273          (C * 8) % 4 since we know that's zero.  */
6274       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6275            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6276           /* If the multiplication can overflow we cannot optimize this.
6277              ???  Until we can properly mark individual operations as
6278              not overflowing we need to treat sizetype special here as
6279              stor-layout relies on this opimization to make
6280              DECL_FIELD_BIT_OFFSET always a constant.  */
6281           && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6282               || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6283                   && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6284           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6285           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6286         {
6287           *strict_overflow_p = true;
6288           return omit_one_operand (type, integer_zero_node, op0);
6289         }
6290
6291       /* ... fall through ...  */
6292
6293     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
6294     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
6295       /* If we can extract our operation from the LHS, do so and return a
6296          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
6297          do something only if the second operand is a constant.  */
6298       if (same_p
6299           && (t1 = extract_muldiv (op0, c, code, wide_type,
6300                                    strict_overflow_p)) != 0)
6301         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6302                             fold_convert (ctype, op1));
6303       else if (tcode == MULT_EXPR && code == MULT_EXPR
6304                && (t1 = extract_muldiv (op1, c, code, wide_type,
6305                                         strict_overflow_p)) != 0)
6306         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6307                             fold_convert (ctype, t1));
6308       else if (TREE_CODE (op1) != INTEGER_CST)
6309         return 0;
6310
6311       /* If these are the same operation types, we can associate them
6312          assuming no overflow.  */
6313       if (tcode == code
6314           && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6315                                          fold_convert (ctype, c), 1))
6316           && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6317                                                TREE_INT_CST_HIGH (t1),
6318                                                (TYPE_UNSIGNED (ctype)
6319                                                 && tcode != MULT_EXPR) ? -1 : 1,
6320                                                TREE_OVERFLOW (t1)))
6321           && !TREE_OVERFLOW (t1))
6322         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6323
6324       /* If these operations "cancel" each other, we have the main
6325          optimizations of this pass, which occur when either constant is a
6326          multiple of the other, in which case we replace this with either an
6327          operation or CODE or TCODE.
6328
6329          If we have an unsigned type that is not a sizetype, we cannot do
6330          this since it will change the result if the original computation
6331          overflowed.  */
6332       if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6333            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6334           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6335               || (tcode == MULT_EXPR
6336                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6337                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6338                   && code != MULT_EXPR)))
6339         {
6340           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6341             {
6342               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6343                 *strict_overflow_p = true;
6344               return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6345                                   fold_convert (ctype,
6346                                                 const_binop (TRUNC_DIV_EXPR,
6347                                                              op1, c, 0)));
6348             }
6349           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6350             {
6351               if (TYPE_OVERFLOW_UNDEFINED (ctype))
6352                 *strict_overflow_p = true;
6353               return fold_build2 (code, ctype, fold_convert (ctype, op0),
6354                                   fold_convert (ctype,
6355                                                 const_binop (TRUNC_DIV_EXPR,
6356                                                              c, op1, 0)));
6357             }
6358         }
6359       break;
6360
6361     default:
6362       break;
6363     }
6364
6365   return 0;
6366 }
6367 \f
6368 /* Return a node which has the indicated constant VALUE (either 0 or
6369    1), and is of the indicated TYPE.  */
6370
6371 tree
6372 constant_boolean_node (int value, tree type)
6373 {
6374   if (type == integer_type_node)
6375     return value ? integer_one_node : integer_zero_node;
6376   else if (type == boolean_type_node)
6377     return value ? boolean_true_node : boolean_false_node;
6378   else
6379     return build_int_cst (type, value);
6380 }
6381
6382
6383 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6384    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
6385    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6386    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
6387    COND is the first argument to CODE; otherwise (as in the example
6388    given here), it is the second argument.  TYPE is the type of the
6389    original expression.  Return NULL_TREE if no simplification is
6390    possible.  */
6391
6392 static tree
6393 fold_binary_op_with_conditional_arg (enum tree_code code,
6394                                      tree type, tree op0, tree op1,
6395                                      tree cond, tree arg, int cond_first_p)
6396 {
6397   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6398   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6399   tree test, true_value, false_value;
6400   tree lhs = NULL_TREE;
6401   tree rhs = NULL_TREE;
6402
6403   /* This transformation is only worthwhile if we don't have to wrap
6404      arg in a SAVE_EXPR, and the operation can be simplified on at least
6405      one of the branches once its pushed inside the COND_EXPR.  */
6406   if (!TREE_CONSTANT (arg))
6407     return NULL_TREE;
6408
6409   if (TREE_CODE (cond) == COND_EXPR)
6410     {
6411       test = TREE_OPERAND (cond, 0);
6412       true_value = TREE_OPERAND (cond, 1);
6413       false_value = TREE_OPERAND (cond, 2);
6414       /* If this operand throws an expression, then it does not make
6415          sense to try to perform a logical or arithmetic operation
6416          involving it.  */
6417       if (VOID_TYPE_P (TREE_TYPE (true_value)))
6418         lhs = true_value;
6419       if (VOID_TYPE_P (TREE_TYPE (false_value)))
6420         rhs = false_value;
6421     }
6422   else
6423     {
6424       tree testtype = TREE_TYPE (cond);
6425       test = cond;
6426       true_value = constant_boolean_node (true, testtype);
6427       false_value = constant_boolean_node (false, testtype);
6428     }
6429
6430   arg = fold_convert (arg_type, arg);
6431   if (lhs == 0)
6432     {
6433       true_value = fold_convert (cond_type, true_value);
6434       if (cond_first_p)
6435         lhs = fold_build2 (code, type, true_value, arg);
6436       else
6437         lhs = fold_build2 (code, type, arg, true_value);
6438     }
6439   if (rhs == 0)
6440     {
6441       false_value = fold_convert (cond_type, false_value);
6442       if (cond_first_p)
6443         rhs = fold_build2 (code, type, false_value, arg);
6444       else
6445         rhs = fold_build2 (code, type, arg, false_value);
6446     }
6447
6448   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6449   return fold_convert (type, test);
6450 }
6451
6452 \f
6453 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6454
6455    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6456    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
6457    ADDEND is the same as X.
6458
6459    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6460    and finite.  The problematic cases are when X is zero, and its mode
6461    has signed zeros.  In the case of rounding towards -infinity,
6462    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
6463    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
6464
6465 bool
6466 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6467 {
6468   if (!real_zerop (addend))
6469     return false;
6470
6471   /* Don't allow the fold with -fsignaling-nans.  */
6472   if (HONOR_SNANS (TYPE_MODE (type)))
6473     return false;
6474
6475   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
6476   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6477     return true;
6478
6479   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
6480   if (TREE_CODE (addend) == REAL_CST
6481       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6482     negate = !negate;
6483
6484   /* The mode has signed zeros, and we have to honor their sign.
6485      In this situation, there is only one case we can return true for.
6486      X - 0 is the same as X unless rounding towards -infinity is
6487      supported.  */
6488   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6489 }
6490
6491 /* Subroutine of fold() that checks comparisons of built-in math
6492    functions against real constants.
6493
6494    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6495    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
6496    is the type of the result and ARG0 and ARG1 are the operands of the
6497    comparison.  ARG1 must be a TREE_REAL_CST.
6498
6499    The function returns the constant folded tree if a simplification
6500    can be made, and NULL_TREE otherwise.  */
6501
6502 static tree
6503 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6504                      tree type, tree arg0, tree arg1)
6505 {
6506   REAL_VALUE_TYPE c;
6507
6508   if (BUILTIN_SQRT_P (fcode))
6509     {
6510       tree arg = CALL_EXPR_ARG (arg0, 0);
6511       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6512
6513       c = TREE_REAL_CST (arg1);
6514       if (REAL_VALUE_NEGATIVE (c))
6515         {
6516           /* sqrt(x) < y is always false, if y is negative.  */
6517           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6518             return omit_one_operand (type, integer_zero_node, arg);
6519
6520           /* sqrt(x) > y is always true, if y is negative and we
6521              don't care about NaNs, i.e. negative values of x.  */
6522           if (code == NE_EXPR || !HONOR_NANS (mode))
6523             return omit_one_operand (type, integer_one_node, arg);
6524
6525           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6526           return fold_build2 (GE_EXPR, type, arg,
6527                               build_real (TREE_TYPE (arg), dconst0));
6528         }
6529       else if (code == GT_EXPR || code == GE_EXPR)
6530         {
6531           REAL_VALUE_TYPE c2;
6532
6533           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6534           real_convert (&c2, mode, &c2);
6535
6536           if (REAL_VALUE_ISINF (c2))
6537             {
6538               /* sqrt(x) > y is x == +Inf, when y is very large.  */
6539               if (HONOR_INFINITIES (mode))
6540                 return fold_build2 (EQ_EXPR, type, arg,
6541                                     build_real (TREE_TYPE (arg), c2));
6542
6543               /* sqrt(x) > y is always false, when y is very large
6544                  and we don't care about infinities.  */
6545               return omit_one_operand (type, integer_zero_node, arg);
6546             }
6547
6548           /* sqrt(x) > c is the same as x > c*c.  */
6549           return fold_build2 (code, type, arg,
6550                               build_real (TREE_TYPE (arg), c2));
6551         }
6552       else if (code == LT_EXPR || code == LE_EXPR)
6553         {
6554           REAL_VALUE_TYPE c2;
6555
6556           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6557           real_convert (&c2, mode, &c2);
6558
6559           if (REAL_VALUE_ISINF (c2))
6560             {
6561               /* sqrt(x) < y is always true, when y is a very large
6562                  value and we don't care about NaNs or Infinities.  */
6563               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6564                 return omit_one_operand (type, integer_one_node, arg);
6565
6566               /* sqrt(x) < y is x != +Inf when y is very large and we
6567                  don't care about NaNs.  */
6568               if (! HONOR_NANS (mode))
6569                 return fold_build2 (NE_EXPR, type, arg,
6570                                     build_real (TREE_TYPE (arg), c2));
6571
6572               /* sqrt(x) < y is x >= 0 when y is very large and we
6573                  don't care about Infinities.  */
6574               if (! HONOR_INFINITIES (mode))
6575                 return fold_build2 (GE_EXPR, type, arg,
6576                                     build_real (TREE_TYPE (arg), dconst0));
6577
6578               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6579               if (lang_hooks.decls.global_bindings_p () != 0
6580                   || CONTAINS_PLACEHOLDER_P (arg))
6581                 return NULL_TREE;
6582
6583               arg = save_expr (arg);
6584               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6585                                   fold_build2 (GE_EXPR, type, arg,
6586                                                build_real (TREE_TYPE (arg),
6587                                                            dconst0)),
6588                                   fold_build2 (NE_EXPR, type, arg,
6589                                                build_real (TREE_TYPE (arg),
6590                                                            c2)));
6591             }
6592
6593           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6594           if (! HONOR_NANS (mode))
6595             return fold_build2 (code, type, arg,
6596                                 build_real (TREE_TYPE (arg), c2));
6597
6598           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6599           if (lang_hooks.decls.global_bindings_p () == 0
6600               && ! CONTAINS_PLACEHOLDER_P (arg))
6601             {
6602               arg = save_expr (arg);
6603               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6604                                   fold_build2 (GE_EXPR, type, arg,
6605                                                build_real (TREE_TYPE (arg),
6606                                                            dconst0)),
6607                                   fold_build2 (code, type, arg,
6608                                                build_real (TREE_TYPE (arg),
6609                                                            c2)));
6610             }
6611         }
6612     }
6613
6614   return NULL_TREE;
6615 }
6616
6617 /* Subroutine of fold() that optimizes comparisons against Infinities,
6618    either +Inf or -Inf.
6619
6620    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6621    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6622    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6623
6624    The function returns the constant folded tree if a simplification
6625    can be made, and NULL_TREE otherwise.  */
6626
6627 static tree
6628 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6629 {
6630   enum machine_mode mode;
6631   REAL_VALUE_TYPE max;
6632   tree temp;
6633   bool neg;
6634
6635   mode = TYPE_MODE (TREE_TYPE (arg0));
6636
6637   /* For negative infinity swap the sense of the comparison.  */
6638   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6639   if (neg)
6640     code = swap_tree_comparison (code);
6641
6642   switch (code)
6643     {
6644     case GT_EXPR:
6645       /* x > +Inf is always false, if with ignore sNANs.  */
6646       if (HONOR_SNANS (mode))
6647         return NULL_TREE;
6648       return omit_one_operand (type, integer_zero_node, arg0);
6649
6650     case LE_EXPR:
6651       /* x <= +Inf is always true, if we don't case about NaNs.  */
6652       if (! HONOR_NANS (mode))
6653         return omit_one_operand (type, integer_one_node, arg0);
6654
6655       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6656       if (lang_hooks.decls.global_bindings_p () == 0
6657           && ! CONTAINS_PLACEHOLDER_P (arg0))
6658         {
6659           arg0 = save_expr (arg0);
6660           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6661         }
6662       break;
6663
6664     case EQ_EXPR:
6665     case GE_EXPR:
6666       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6667       real_maxval (&max, neg, mode);
6668       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6669                           arg0, build_real (TREE_TYPE (arg0), max));
6670
6671     case LT_EXPR:
6672       /* x < +Inf is always equal to x <= DBL_MAX.  */
6673       real_maxval (&max, neg, mode);
6674       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6675                           arg0, build_real (TREE_TYPE (arg0), max));
6676
6677     case NE_EXPR:
6678       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6679       real_maxval (&max, neg, mode);
6680       if (! HONOR_NANS (mode))
6681         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6682                             arg0, build_real (TREE_TYPE (arg0), max));
6683
6684       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6685                           arg0, build_real (TREE_TYPE (arg0), max));
6686       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6687
6688     default:
6689       break;
6690     }
6691
6692   return NULL_TREE;
6693 }
6694
6695 /* Subroutine of fold() that optimizes comparisons of a division by
6696    a nonzero integer constant against an integer constant, i.e.
6697    X/C1 op C2.
6698
6699    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6700    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6701    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6702
6703    The function returns the constant folded tree if a simplification
6704    can be made, and NULL_TREE otherwise.  */
6705
6706 static tree
6707 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6708 {
6709   tree prod, tmp, hi, lo;
6710   tree arg00 = TREE_OPERAND (arg0, 0);
6711   tree arg01 = TREE_OPERAND (arg0, 1);
6712   unsigned HOST_WIDE_INT lpart;
6713   HOST_WIDE_INT hpart;
6714   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6715   bool neg_overflow;
6716   int overflow;
6717
6718   /* We have to do this the hard way to detect unsigned overflow.
6719      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6720   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6721                                    TREE_INT_CST_HIGH (arg01),
6722                                    TREE_INT_CST_LOW (arg1),
6723                                    TREE_INT_CST_HIGH (arg1),
6724                                    &lpart, &hpart, unsigned_p);
6725   prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6726                                 -1, overflow);
6727   neg_overflow = false;
6728
6729   if (unsigned_p)
6730     {
6731       tmp = int_const_binop (MINUS_EXPR, arg01,
6732                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6733       lo = prod;
6734
6735       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6736       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6737                                        TREE_INT_CST_HIGH (prod),
6738                                        TREE_INT_CST_LOW (tmp),
6739                                        TREE_INT_CST_HIGH (tmp),
6740                                        &lpart, &hpart, unsigned_p);
6741       hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6742                                   -1, overflow | TREE_OVERFLOW (prod));
6743     }
6744   else if (tree_int_cst_sgn (arg01) >= 0)
6745     {
6746       tmp = int_const_binop (MINUS_EXPR, arg01,
6747                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6748       switch (tree_int_cst_sgn (arg1))
6749         {
6750         case -1:
6751           neg_overflow = true;
6752           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6753           hi = prod;
6754           break;
6755
6756         case  0:
6757           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6758           hi = tmp;
6759           break;
6760
6761         case  1:
6762           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6763           lo = prod;
6764           break;
6765
6766         default:
6767           gcc_unreachable ();
6768         }
6769     }
6770   else
6771     {
6772       /* A negative divisor reverses the relational operators.  */
6773       code = swap_tree_comparison (code);
6774
6775       tmp = int_const_binop (PLUS_EXPR, arg01,
6776                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6777       switch (tree_int_cst_sgn (arg1))
6778         {
6779         case -1:
6780           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6781           lo = prod;
6782           break;
6783
6784         case  0:
6785           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6786           lo = tmp;
6787           break;
6788
6789         case  1:
6790           neg_overflow = true;
6791           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6792           hi = prod;
6793           break;
6794
6795         default:
6796           gcc_unreachable ();
6797         }
6798     }
6799
6800   switch (code)
6801     {
6802     case EQ_EXPR:
6803       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6804         return omit_one_operand (type, integer_zero_node, arg00);
6805       if (TREE_OVERFLOW (hi))
6806         return fold_build2 (GE_EXPR, type, arg00, lo);
6807       if (TREE_OVERFLOW (lo))
6808         return fold_build2 (LE_EXPR, type, arg00, hi);
6809       return build_range_check (type, arg00, 1, lo, hi);
6810
6811     case NE_EXPR:
6812       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6813         return omit_one_operand (type, integer_one_node, arg00);
6814       if (TREE_OVERFLOW (hi))
6815         return fold_build2 (LT_EXPR, type, arg00, lo);
6816       if (TREE_OVERFLOW (lo))
6817         return fold_build2 (GT_EXPR, type, arg00, hi);
6818       return build_range_check (type, arg00, 0, lo, hi);
6819
6820     case LT_EXPR:
6821       if (TREE_OVERFLOW (lo))
6822         {
6823           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6824           return omit_one_operand (type, tmp, arg00);
6825         }
6826       return fold_build2 (LT_EXPR, type, arg00, lo);
6827
6828     case LE_EXPR:
6829       if (TREE_OVERFLOW (hi))
6830         {
6831           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6832           return omit_one_operand (type, tmp, arg00);
6833         }
6834       return fold_build2 (LE_EXPR, type, arg00, hi);
6835
6836     case GT_EXPR:
6837       if (TREE_OVERFLOW (hi))
6838         {
6839           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6840           return omit_one_operand (type, tmp, arg00);
6841         }
6842       return fold_build2 (GT_EXPR, type, arg00, hi);
6843
6844     case GE_EXPR:
6845       if (TREE_OVERFLOW (lo))
6846         {
6847           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6848           return omit_one_operand (type, tmp, arg00);
6849         }
6850       return fold_build2 (GE_EXPR, type, arg00, lo);
6851
6852     default:
6853       break;
6854     }
6855
6856   return NULL_TREE;
6857 }
6858
6859
6860 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6861    equality/inequality test, then return a simplified form of the test
6862    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6863    result type.  */
6864
6865 static tree
6866 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6867                                      tree result_type)
6868 {
6869   /* If this is testing a single bit, we can optimize the test.  */
6870   if ((code == NE_EXPR || code == EQ_EXPR)
6871       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6872       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6873     {
6874       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6875          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6876       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6877
6878       if (arg00 != NULL_TREE
6879           /* This is only a win if casting to a signed type is cheap,
6880              i.e. when arg00's type is not a partial mode.  */
6881           && TYPE_PRECISION (TREE_TYPE (arg00))
6882              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6883         {
6884           tree stype = signed_type_for (TREE_TYPE (arg00));
6885           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6886                               result_type, fold_convert (stype, arg00),
6887                               build_int_cst (stype, 0));
6888         }
6889     }
6890
6891   return NULL_TREE;
6892 }
6893
6894 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6895    equality/inequality test, then return a simplified form of
6896    the test using shifts and logical operations.  Otherwise return
6897    NULL.  TYPE is the desired result type.  */
6898
6899 tree
6900 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6901                       tree result_type)
6902 {
6903   /* If this is testing a single bit, we can optimize the test.  */
6904   if ((code == NE_EXPR || code == EQ_EXPR)
6905       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6906       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6907     {
6908       tree inner = TREE_OPERAND (arg0, 0);
6909       tree type = TREE_TYPE (arg0);
6910       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6911       enum machine_mode operand_mode = TYPE_MODE (type);
6912       int ops_unsigned;
6913       tree signed_type, unsigned_type, intermediate_type;
6914       tree tem, one;
6915
6916       /* First, see if we can fold the single bit test into a sign-bit
6917          test.  */
6918       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6919                                                  result_type);
6920       if (tem)
6921         return tem;
6922
6923       /* Otherwise we have (A & C) != 0 where C is a single bit,
6924          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6925          Similarly for (A & C) == 0.  */
6926
6927       /* If INNER is a right shift of a constant and it plus BITNUM does
6928          not overflow, adjust BITNUM and INNER.  */
6929       if (TREE_CODE (inner) == RSHIFT_EXPR
6930           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6931           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6932           && bitnum < TYPE_PRECISION (type)
6933           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6934                                    bitnum - TYPE_PRECISION (type)))
6935         {
6936           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6937           inner = TREE_OPERAND (inner, 0);
6938         }
6939
6940       /* If we are going to be able to omit the AND below, we must do our
6941          operations as unsigned.  If we must use the AND, we have a choice.
6942          Normally unsigned is faster, but for some machines signed is.  */
6943 #ifdef LOAD_EXTEND_OP
6944       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6945                       && !flag_syntax_only) ? 0 : 1;
6946 #else
6947       ops_unsigned = 1;
6948 #endif
6949
6950       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6951       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6952       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6953       inner = fold_convert (intermediate_type, inner);
6954
6955       if (bitnum != 0)
6956         inner = build2 (RSHIFT_EXPR, intermediate_type,
6957                         inner, size_int (bitnum));
6958
6959       one = build_int_cst (intermediate_type, 1);
6960
6961       if (code == EQ_EXPR)
6962         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6963
6964       /* Put the AND last so it can combine with more things.  */
6965       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6966
6967       /* Make sure to return the proper type.  */
6968       inner = fold_convert (result_type, inner);
6969
6970       return inner;
6971     }
6972   return NULL_TREE;
6973 }
6974
6975 /* Check whether we are allowed to reorder operands arg0 and arg1,
6976    such that the evaluation of arg1 occurs before arg0.  */
6977
6978 static bool
6979 reorder_operands_p (const_tree arg0, const_tree arg1)
6980 {
6981   if (! flag_evaluation_order)
6982       return true;
6983   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6984     return true;
6985   return ! TREE_SIDE_EFFECTS (arg0)
6986          && ! TREE_SIDE_EFFECTS (arg1);
6987 }
6988
6989 /* Test whether it is preferable two swap two operands, ARG0 and
6990    ARG1, for example because ARG0 is an integer constant and ARG1
6991    isn't.  If REORDER is true, only recommend swapping if we can
6992    evaluate the operands in reverse order.  */
6993
6994 bool
6995 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6996 {
6997   STRIP_SIGN_NOPS (arg0);
6998   STRIP_SIGN_NOPS (arg1);
6999
7000   if (TREE_CODE (arg1) == INTEGER_CST)
7001     return 0;
7002   if (TREE_CODE (arg0) == INTEGER_CST)
7003     return 1;
7004
7005   if (TREE_CODE (arg1) == REAL_CST)
7006     return 0;
7007   if (TREE_CODE (arg0) == REAL_CST)
7008     return 1;
7009
7010   if (TREE_CODE (arg1) == FIXED_CST)
7011     return 0;
7012   if (TREE_CODE (arg0) == FIXED_CST)
7013     return 1;
7014
7015   if (TREE_CODE (arg1) == COMPLEX_CST)
7016     return 0;
7017   if (TREE_CODE (arg0) == COMPLEX_CST)
7018     return 1;
7019
7020   if (TREE_CONSTANT (arg1))
7021     return 0;
7022   if (TREE_CONSTANT (arg0))
7023     return 1;
7024
7025   if (optimize_function_for_size_p (cfun))
7026     return 0;
7027
7028   if (reorder && flag_evaluation_order
7029       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7030     return 0;
7031
7032   /* It is preferable to swap two SSA_NAME to ensure a canonical form
7033      for commutative and comparison operators.  Ensuring a canonical
7034      form allows the optimizers to find additional redundancies without
7035      having to explicitly check for both orderings.  */
7036   if (TREE_CODE (arg0) == SSA_NAME
7037       && TREE_CODE (arg1) == SSA_NAME
7038       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7039     return 1;
7040
7041   /* Put SSA_NAMEs last.  */
7042   if (TREE_CODE (arg1) == SSA_NAME)
7043     return 0;
7044   if (TREE_CODE (arg0) == SSA_NAME)
7045     return 1;
7046
7047   /* Put variables last.  */
7048   if (DECL_P (arg1))
7049     return 0;
7050   if (DECL_P (arg0))
7051     return 1;
7052
7053   return 0;
7054 }
7055
7056 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7057    ARG0 is extended to a wider type.  */
7058
7059 static tree
7060 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7061 {
7062   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7063   tree arg1_unw;
7064   tree shorter_type, outer_type;
7065   tree min, max;
7066   bool above, below;
7067
7068   if (arg0_unw == arg0)
7069     return NULL_TREE;
7070   shorter_type = TREE_TYPE (arg0_unw);
7071
7072 #ifdef HAVE_canonicalize_funcptr_for_compare
7073   /* Disable this optimization if we're casting a function pointer
7074      type on targets that require function pointer canonicalization.  */
7075   if (HAVE_canonicalize_funcptr_for_compare
7076       && TREE_CODE (shorter_type) == POINTER_TYPE
7077       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7078     return NULL_TREE;
7079 #endif
7080
7081   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7082     return NULL_TREE;
7083
7084   arg1_unw = get_unwidened (arg1, NULL_TREE);
7085
7086   /* If possible, express the comparison in the shorter mode.  */
7087   if ((code == EQ_EXPR || code == NE_EXPR
7088        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7089       && (TREE_TYPE (arg1_unw) == shorter_type
7090           || ((TYPE_PRECISION (shorter_type)
7091                >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7092               && (TYPE_UNSIGNED (shorter_type)
7093                   == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7094           || (TREE_CODE (arg1_unw) == INTEGER_CST
7095               && (TREE_CODE (shorter_type) == INTEGER_TYPE
7096                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7097               && int_fits_type_p (arg1_unw, shorter_type))))
7098     return fold_build2 (code, type, arg0_unw,
7099                        fold_convert (shorter_type, arg1_unw));
7100
7101   if (TREE_CODE (arg1_unw) != INTEGER_CST
7102       || TREE_CODE (shorter_type) != INTEGER_TYPE
7103       || !int_fits_type_p (arg1_unw, shorter_type))
7104     return NULL_TREE;
7105
7106   /* If we are comparing with the integer that does not fit into the range
7107      of the shorter type, the result is known.  */
7108   outer_type = TREE_TYPE (arg1_unw);
7109   min = lower_bound_in_type (outer_type, shorter_type);
7110   max = upper_bound_in_type (outer_type, shorter_type);
7111
7112   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7113                                                    max, arg1_unw));
7114   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7115                                                    arg1_unw, min));
7116
7117   switch (code)
7118     {
7119     case EQ_EXPR:
7120       if (above || below)
7121         return omit_one_operand (type, integer_zero_node, arg0);
7122       break;
7123
7124     case NE_EXPR:
7125       if (above || below)
7126         return omit_one_operand (type, integer_one_node, arg0);
7127       break;
7128
7129     case LT_EXPR:
7130     case LE_EXPR:
7131       if (above)
7132         return omit_one_operand (type, integer_one_node, arg0);
7133       else if (below)
7134         return omit_one_operand (type, integer_zero_node, arg0);
7135
7136     case GT_EXPR:
7137     case GE_EXPR:
7138       if (above)
7139         return omit_one_operand (type, integer_zero_node, arg0);
7140       else if (below)
7141         return omit_one_operand (type, integer_one_node, arg0);
7142
7143     default:
7144       break;
7145     }
7146
7147   return NULL_TREE;
7148 }
7149
7150 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7151    ARG0 just the signedness is changed.  */
7152
7153 static tree
7154 fold_sign_changed_comparison (enum tree_code code, tree type,
7155                               tree arg0, tree arg1)
7156 {
7157   tree arg0_inner;
7158   tree inner_type, outer_type;
7159
7160   if (!CONVERT_EXPR_P (arg0))
7161     return NULL_TREE;
7162
7163   outer_type = TREE_TYPE (arg0);
7164   arg0_inner = TREE_OPERAND (arg0, 0);
7165   inner_type = TREE_TYPE (arg0_inner);
7166
7167 #ifdef HAVE_canonicalize_funcptr_for_compare
7168   /* Disable this optimization if we're casting a function pointer
7169      type on targets that require function pointer canonicalization.  */
7170   if (HAVE_canonicalize_funcptr_for_compare
7171       && TREE_CODE (inner_type) == POINTER_TYPE
7172       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7173     return NULL_TREE;
7174 #endif
7175
7176   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7177     return NULL_TREE;
7178
7179   /* If the conversion is from an integral subtype to its basetype
7180      leave it alone.  */
7181   if (TREE_TYPE (inner_type) == outer_type)
7182     return NULL_TREE;
7183
7184   if (TREE_CODE (arg1) != INTEGER_CST
7185       && !(CONVERT_EXPR_P (arg1)
7186            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7187     return NULL_TREE;
7188
7189   if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7190        || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7191       && code != NE_EXPR
7192       && code != EQ_EXPR)
7193     return NULL_TREE;
7194
7195   if (TREE_CODE (arg1) == INTEGER_CST)
7196     arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7197                                   TREE_INT_CST_HIGH (arg1), 0,
7198                                   TREE_OVERFLOW (arg1));
7199   else
7200     arg1 = fold_convert (inner_type, arg1);
7201
7202   return fold_build2 (code, type, arg0_inner, arg1);
7203 }
7204
7205 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7206    step of the array.  Reconstructs s and delta in the case of s * delta
7207    being an integer constant (and thus already folded).
7208    ADDR is the address. MULT is the multiplicative expression.
7209    If the function succeeds, the new address expression is returned.  Otherwise
7210    NULL_TREE is returned.  */
7211
7212 static tree
7213 try_move_mult_to_index (tree addr, tree op1)
7214 {
7215   tree s, delta, step;
7216   tree ref = TREE_OPERAND (addr, 0), pref;
7217   tree ret, pos;
7218   tree itype;
7219   bool mdim = false;
7220
7221   /*  Strip the nops that might be added when converting op1 to sizetype. */
7222   STRIP_NOPS (op1);
7223
7224   /* Canonicalize op1 into a possibly non-constant delta
7225      and an INTEGER_CST s.  */
7226   if (TREE_CODE (op1) == MULT_EXPR)
7227     {
7228       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7229
7230       STRIP_NOPS (arg0);
7231       STRIP_NOPS (arg1);
7232   
7233       if (TREE_CODE (arg0) == INTEGER_CST)
7234         {
7235           s = arg0;
7236           delta = arg1;
7237         }
7238       else if (TREE_CODE (arg1) == INTEGER_CST)
7239         {
7240           s = arg1;
7241           delta = arg0;
7242         }
7243       else
7244         return NULL_TREE;
7245     }
7246   else if (TREE_CODE (op1) == INTEGER_CST)
7247     {
7248       delta = op1;
7249       s = NULL_TREE;
7250     }
7251   else
7252     {
7253       /* Simulate we are delta * 1.  */
7254       delta = op1;
7255       s = integer_one_node;
7256     }
7257
7258   for (;; ref = TREE_OPERAND (ref, 0))
7259     {
7260       if (TREE_CODE (ref) == ARRAY_REF)
7261         {
7262           /* Remember if this was a multi-dimensional array.  */
7263           if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7264             mdim = true;
7265
7266           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7267           if (! itype)
7268             continue;
7269
7270           step = array_ref_element_size (ref);
7271           if (TREE_CODE (step) != INTEGER_CST)
7272             continue;
7273
7274           if (s)
7275             {
7276               if (! tree_int_cst_equal (step, s))
7277                 continue;
7278             }
7279           else
7280             {
7281               /* Try if delta is a multiple of step.  */
7282               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7283               if (! tmp)
7284                 continue;
7285               delta = tmp;
7286             }
7287
7288           /* Only fold here if we can verify we do not overflow one
7289              dimension of a multi-dimensional array.  */
7290           if (mdim)
7291             {
7292               tree tmp;
7293
7294               if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7295                   || !INTEGRAL_TYPE_P (itype)
7296                   || !TYPE_MAX_VALUE (itype)
7297                   || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7298                 continue;
7299
7300               tmp = fold_binary (PLUS_EXPR, itype,
7301                                  fold_convert (itype,
7302                                                TREE_OPERAND (ref, 1)),
7303                                  fold_convert (itype, delta));
7304               if (!tmp
7305                   || TREE_CODE (tmp) != INTEGER_CST
7306                   || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7307                 continue;
7308             }
7309
7310           break;
7311         }
7312       else
7313         mdim = false;
7314
7315       if (!handled_component_p (ref))
7316         return NULL_TREE;
7317     }
7318
7319   /* We found the suitable array reference.  So copy everything up to it,
7320      and replace the index.  */
7321
7322   pref = TREE_OPERAND (addr, 0);
7323   ret = copy_node (pref);
7324   pos = ret;
7325
7326   while (pref != ref)
7327     {
7328       pref = TREE_OPERAND (pref, 0);
7329       TREE_OPERAND (pos, 0) = copy_node (pref);
7330       pos = TREE_OPERAND (pos, 0);
7331     }
7332
7333   TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7334                                        fold_convert (itype,
7335                                                      TREE_OPERAND (pos, 1)),
7336                                        fold_convert (itype, delta));
7337
7338   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7339 }
7340
7341
7342 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
7343    means A >= Y && A != MAX, but in this case we know that
7344    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
7345
7346 static tree
7347 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7348 {
7349   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7350
7351   if (TREE_CODE (bound) == LT_EXPR)
7352     a = TREE_OPERAND (bound, 0);
7353   else if (TREE_CODE (bound) == GT_EXPR)
7354     a = TREE_OPERAND (bound, 1);
7355   else
7356     return NULL_TREE;
7357
7358   typea = TREE_TYPE (a);
7359   if (!INTEGRAL_TYPE_P (typea)
7360       && !POINTER_TYPE_P (typea))
7361     return NULL_TREE;
7362
7363   if (TREE_CODE (ineq) == LT_EXPR)
7364     {
7365       a1 = TREE_OPERAND (ineq, 1);
7366       y = TREE_OPERAND (ineq, 0);
7367     }
7368   else if (TREE_CODE (ineq) == GT_EXPR)
7369     {
7370       a1 = TREE_OPERAND (ineq, 0);
7371       y = TREE_OPERAND (ineq, 1);
7372     }
7373   else
7374     return NULL_TREE;
7375
7376   if (TREE_TYPE (a1) != typea)
7377     return NULL_TREE;
7378
7379   if (POINTER_TYPE_P (typea))
7380     {
7381       /* Convert the pointer types into integer before taking the difference.  */
7382       tree ta = fold_convert (ssizetype, a);
7383       tree ta1 = fold_convert (ssizetype, a1);
7384       diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7385     }
7386   else
7387    diff = fold_binary (MINUS_EXPR, typea, a1, a);
7388
7389   if (!diff || !integer_onep (diff))
7390    return NULL_TREE;
7391
7392   return fold_build2 (GE_EXPR, type, a, y);
7393 }
7394
7395 /* Fold a sum or difference of at least one multiplication.
7396    Returns the folded tree or NULL if no simplification could be made.  */
7397
7398 static tree
7399 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7400 {
7401   tree arg00, arg01, arg10, arg11;
7402   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7403
7404   /* (A * C) +- (B * C) -> (A+-B) * C.
7405      (A * C) +- A -> A * (C+-1).
7406      We are most concerned about the case where C is a constant,
7407      but other combinations show up during loop reduction.  Since
7408      it is not difficult, try all four possibilities.  */
7409
7410   if (TREE_CODE (arg0) == MULT_EXPR)
7411     {
7412       arg00 = TREE_OPERAND (arg0, 0);
7413       arg01 = TREE_OPERAND (arg0, 1);
7414     }
7415   else if (TREE_CODE (arg0) == INTEGER_CST)
7416     {
7417       arg00 = build_one_cst (type);
7418       arg01 = arg0;
7419     }
7420   else
7421     {
7422       /* We cannot generate constant 1 for fract.  */
7423       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7424         return NULL_TREE;
7425       arg00 = arg0;
7426       arg01 = build_one_cst (type);
7427     }
7428   if (TREE_CODE (arg1) == MULT_EXPR)
7429     {
7430       arg10 = TREE_OPERAND (arg1, 0);
7431       arg11 = TREE_OPERAND (arg1, 1);
7432     }
7433   else if (TREE_CODE (arg1) == INTEGER_CST)
7434     {
7435       arg10 = build_one_cst (type);
7436       /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7437          the purpose of this canonicalization.  */
7438       if (TREE_INT_CST_HIGH (arg1) == -1
7439           && negate_expr_p (arg1)
7440           && code == PLUS_EXPR)
7441         {
7442           arg11 = negate_expr (arg1);
7443           code = MINUS_EXPR;
7444         }
7445       else
7446         arg11 = arg1;
7447     }
7448   else
7449     {
7450       /* We cannot generate constant 1 for fract.  */
7451       if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7452         return NULL_TREE;
7453       arg10 = arg1;
7454       arg11 = build_one_cst (type);
7455     }
7456   same = NULL_TREE;
7457
7458   if (operand_equal_p (arg01, arg11, 0))
7459     same = arg01, alt0 = arg00, alt1 = arg10;
7460   else if (operand_equal_p (arg00, arg10, 0))
7461     same = arg00, alt0 = arg01, alt1 = arg11;
7462   else if (operand_equal_p (arg00, arg11, 0))
7463     same = arg00, alt0 = arg01, alt1 = arg10;
7464   else if (operand_equal_p (arg01, arg10, 0))
7465     same = arg01, alt0 = arg00, alt1 = arg11;
7466
7467   /* No identical multiplicands; see if we can find a common
7468      power-of-two factor in non-power-of-two multiplies.  This
7469      can help in multi-dimensional array access.  */
7470   else if (host_integerp (arg01, 0)
7471            && host_integerp (arg11, 0))
7472     {
7473       HOST_WIDE_INT int01, int11, tmp;
7474       bool swap = false;
7475       tree maybe_same;
7476       int01 = TREE_INT_CST_LOW (arg01);
7477       int11 = TREE_INT_CST_LOW (arg11);
7478
7479       /* Move min of absolute values to int11.  */
7480       if ((int01 >= 0 ? int01 : -int01)
7481           < (int11 >= 0 ? int11 : -int11))
7482         {
7483           tmp = int01, int01 = int11, int11 = tmp;
7484           alt0 = arg00, arg00 = arg10, arg10 = alt0;
7485           maybe_same = arg01;
7486           swap = true;
7487         }
7488       else
7489         maybe_same = arg11;
7490
7491       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7492         {
7493           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7494                               build_int_cst (TREE_TYPE (arg00),
7495                                              int01 / int11));
7496           alt1 = arg10;
7497           same = maybe_same;
7498           if (swap)
7499             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7500         }
7501     }
7502
7503   if (same)
7504     return fold_build2 (MULT_EXPR, type,
7505                         fold_build2 (code, type,
7506                                      fold_convert (type, alt0),
7507                                      fold_convert (type, alt1)),
7508                         fold_convert (type, same));
7509
7510   return NULL_TREE;
7511 }
7512
7513 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
7514    specified by EXPR into the buffer PTR of length LEN bytes.
7515    Return the number of bytes placed in the buffer, or zero
7516    upon failure.  */
7517
7518 static int
7519 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7520 {
7521   tree type = TREE_TYPE (expr);
7522   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7523   int byte, offset, word, words;
7524   unsigned char value;
7525
7526   if (total_bytes > len)
7527     return 0;
7528   words = total_bytes / UNITS_PER_WORD;
7529
7530   for (byte = 0; byte < total_bytes; byte++)
7531     {
7532       int bitpos = byte * BITS_PER_UNIT;
7533       if (bitpos < HOST_BITS_PER_WIDE_INT)
7534         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7535       else
7536         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7537                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
7538
7539       if (total_bytes > UNITS_PER_WORD)
7540         {
7541           word = byte / UNITS_PER_WORD;
7542           if (WORDS_BIG_ENDIAN)
7543             word = (words - 1) - word;
7544           offset = word * UNITS_PER_WORD;
7545           if (BYTES_BIG_ENDIAN)
7546             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7547           else
7548             offset += byte % UNITS_PER_WORD;
7549         }
7550       else
7551         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7552       ptr[offset] = value;
7553     }
7554   return total_bytes;
7555 }
7556
7557
7558 /* Subroutine of native_encode_expr.  Encode the REAL_CST
7559    specified by EXPR into the buffer PTR of length LEN bytes.
7560    Return the number of bytes placed in the buffer, or zero
7561    upon failure.  */
7562
7563 static int
7564 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7565 {
7566   tree type = TREE_TYPE (expr);
7567   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7568   int byte, offset, word, words, bitpos;
7569   unsigned char value;
7570
7571   /* There are always 32 bits in each long, no matter the size of
7572      the hosts long.  We handle floating point representations with
7573      up to 192 bits.  */
7574   long tmp[6];
7575
7576   if (total_bytes > len)
7577     return 0;
7578   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7579
7580   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7581
7582   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7583        bitpos += BITS_PER_UNIT)
7584     {
7585       byte = (bitpos / BITS_PER_UNIT) & 3;
7586       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7587
7588       if (UNITS_PER_WORD < 4)
7589         {
7590           word = byte / UNITS_PER_WORD;
7591           if (WORDS_BIG_ENDIAN)
7592             word = (words - 1) - word;
7593           offset = word * UNITS_PER_WORD;
7594           if (BYTES_BIG_ENDIAN)
7595             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7596           else
7597             offset += byte % UNITS_PER_WORD;
7598         }
7599       else
7600         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7601       ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7602     }
7603   return total_bytes;
7604 }
7605
7606 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
7607    specified by EXPR into the buffer PTR of length LEN bytes.
7608    Return the number of bytes placed in the buffer, or zero
7609    upon failure.  */
7610
7611 static int
7612 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7613 {
7614   int rsize, isize;
7615   tree part;
7616
7617   part = TREE_REALPART (expr);
7618   rsize = native_encode_expr (part, ptr, len);
7619   if (rsize == 0)
7620     return 0;
7621   part = TREE_IMAGPART (expr);
7622   isize = native_encode_expr (part, ptr+rsize, len-rsize);
7623   if (isize != rsize)
7624     return 0;
7625   return rsize + isize;
7626 }
7627
7628
7629 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
7630    specified by EXPR into the buffer PTR of length LEN bytes.
7631    Return the number of bytes placed in the buffer, or zero
7632    upon failure.  */
7633
7634 static int
7635 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7636 {
7637   int i, size, offset, count;
7638   tree itype, elem, elements;
7639
7640   offset = 0;
7641   elements = TREE_VECTOR_CST_ELTS (expr);
7642   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7643   itype = TREE_TYPE (TREE_TYPE (expr));
7644   size = GET_MODE_SIZE (TYPE_MODE (itype));
7645   for (i = 0; i < count; i++)
7646     {
7647       if (elements)
7648         {
7649           elem = TREE_VALUE (elements);
7650           elements = TREE_CHAIN (elements);
7651         }
7652       else
7653         elem = NULL_TREE;
7654
7655       if (elem)
7656         {
7657           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7658             return 0;
7659         }
7660       else
7661         {
7662           if (offset + size > len)
7663             return 0;
7664           memset (ptr+offset, 0, size);
7665         }
7666       offset += size;
7667     }
7668   return offset;
7669 }
7670
7671
7672 /* Subroutine of native_encode_expr.  Encode the STRING_CST
7673    specified by EXPR into the buffer PTR of length LEN bytes.
7674    Return the number of bytes placed in the buffer, or zero
7675    upon failure.  */
7676
7677 static int
7678 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7679 {
7680   tree type = TREE_TYPE (expr);
7681   HOST_WIDE_INT total_bytes;
7682
7683   if (TREE_CODE (type) != ARRAY_TYPE
7684       || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7685       || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7686       || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7687     return 0;
7688   total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7689   if (total_bytes > len)
7690     return 0;
7691   if (TREE_STRING_LENGTH (expr) < total_bytes)
7692     {
7693       memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7694       memset (ptr + TREE_STRING_LENGTH (expr), 0,
7695               total_bytes - TREE_STRING_LENGTH (expr));
7696     }
7697   else
7698     memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7699   return total_bytes;
7700 }
7701
7702
7703 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7704    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7705    buffer PTR of length LEN bytes.  Return the number of bytes
7706    placed in the buffer, or zero upon failure.  */
7707
7708 int
7709 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7710 {
7711   switch (TREE_CODE (expr))
7712     {
7713     case INTEGER_CST:
7714       return native_encode_int (expr, ptr, len);
7715
7716     case REAL_CST:
7717       return native_encode_real (expr, ptr, len);
7718
7719     case COMPLEX_CST:
7720       return native_encode_complex (expr, ptr, len);
7721
7722     case VECTOR_CST:
7723       return native_encode_vector (expr, ptr, len);
7724
7725     case STRING_CST:
7726       return native_encode_string (expr, ptr, len);
7727
7728     default:
7729       return 0;
7730     }
7731 }
7732
7733
7734 /* Subroutine of native_interpret_expr.  Interpret the contents of
7735    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7736    If the buffer cannot be interpreted, return NULL_TREE.  */
7737
7738 static tree
7739 native_interpret_int (tree type, const unsigned char *ptr, int len)
7740 {
7741   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7742   int byte, offset, word, words;
7743   unsigned char value;
7744   unsigned int HOST_WIDE_INT lo = 0;
7745   HOST_WIDE_INT hi = 0;
7746
7747   if (total_bytes > len)
7748     return NULL_TREE;
7749   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7750     return NULL_TREE;
7751   words = total_bytes / UNITS_PER_WORD;
7752
7753   for (byte = 0; byte < total_bytes; byte++)
7754     {
7755       int bitpos = byte * BITS_PER_UNIT;
7756       if (total_bytes > UNITS_PER_WORD)
7757         {
7758           word = byte / UNITS_PER_WORD;
7759           if (WORDS_BIG_ENDIAN)
7760             word = (words - 1) - word;
7761           offset = word * UNITS_PER_WORD;
7762           if (BYTES_BIG_ENDIAN)
7763             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7764           else
7765             offset += byte % UNITS_PER_WORD;
7766         }
7767       else
7768         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7769       value = ptr[offset];
7770
7771       if (bitpos < HOST_BITS_PER_WIDE_INT)
7772         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7773       else
7774         hi |= (unsigned HOST_WIDE_INT) value
7775               << (bitpos - HOST_BITS_PER_WIDE_INT);
7776     }
7777
7778   return build_int_cst_wide_type (type, lo, hi);
7779 }
7780
7781
7782 /* Subroutine of native_interpret_expr.  Interpret the contents of
7783    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7784    If the buffer cannot be interpreted, return NULL_TREE.  */
7785
7786 static tree
7787 native_interpret_real (tree type, const unsigned char *ptr, int len)
7788 {
7789   enum machine_mode mode = TYPE_MODE (type);
7790   int total_bytes = GET_MODE_SIZE (mode);
7791   int byte, offset, word, words, bitpos;
7792   unsigned char value;
7793   /* There are always 32 bits in each long, no matter the size of
7794      the hosts long.  We handle floating point representations with
7795      up to 192 bits.  */
7796   REAL_VALUE_TYPE r;
7797   long tmp[6];
7798
7799   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7800   if (total_bytes > len || total_bytes > 24)
7801     return NULL_TREE;
7802   words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7803
7804   memset (tmp, 0, sizeof (tmp));
7805   for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7806        bitpos += BITS_PER_UNIT)
7807     {
7808       byte = (bitpos / BITS_PER_UNIT) & 3;
7809       if (UNITS_PER_WORD < 4)
7810         {
7811           word = byte / UNITS_PER_WORD;
7812           if (WORDS_BIG_ENDIAN)
7813             word = (words - 1) - word;
7814           offset = word * UNITS_PER_WORD;
7815           if (BYTES_BIG_ENDIAN)
7816             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7817           else
7818             offset += byte % UNITS_PER_WORD;
7819         }
7820       else
7821         offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7822       value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7823
7824       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7825     }
7826
7827   real_from_target (&r, tmp, mode);
7828   return build_real (type, r);
7829 }
7830
7831
7832 /* Subroutine of native_interpret_expr.  Interpret the contents of
7833    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7834    If the buffer cannot be interpreted, return NULL_TREE.  */
7835
7836 static tree
7837 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7838 {
7839   tree etype, rpart, ipart;
7840   int size;
7841
7842   etype = TREE_TYPE (type);
7843   size = GET_MODE_SIZE (TYPE_MODE (etype));
7844   if (size * 2 > len)
7845     return NULL_TREE;
7846   rpart = native_interpret_expr (etype, ptr, size);
7847   if (!rpart)
7848     return NULL_TREE;
7849   ipart = native_interpret_expr (etype, ptr+size, size);
7850   if (!ipart)
7851     return NULL_TREE;
7852   return build_complex (type, rpart, ipart);
7853 }
7854
7855
7856 /* Subroutine of native_interpret_expr.  Interpret the contents of
7857    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7858    If the buffer cannot be interpreted, return NULL_TREE.  */
7859
7860 static tree
7861 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7862 {
7863   tree etype, elem, elements;
7864   int i, size, count;
7865
7866   etype = TREE_TYPE (type);
7867   size = GET_MODE_SIZE (TYPE_MODE (etype));
7868   count = TYPE_VECTOR_SUBPARTS (type);
7869   if (size * count > len)
7870     return NULL_TREE;
7871
7872   elements = NULL_TREE;
7873   for (i = count - 1; i >= 0; i--)
7874     {
7875       elem = native_interpret_expr (etype, ptr+(i*size), size);
7876       if (!elem)
7877         return NULL_TREE;
7878       elements = tree_cons (NULL_TREE, elem, elements);
7879     }
7880   return build_vector (type, elements);
7881 }
7882
7883
7884 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7885    the buffer PTR of length LEN as a constant of type TYPE.  For
7886    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7887    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7888    return NULL_TREE.  */
7889
7890 tree
7891 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7892 {
7893   switch (TREE_CODE (type))
7894     {
7895     case INTEGER_TYPE:
7896     case ENUMERAL_TYPE:
7897     case BOOLEAN_TYPE:
7898       return native_interpret_int (type, ptr, len);
7899
7900     case REAL_TYPE:
7901       return native_interpret_real (type, ptr, len);
7902
7903     case COMPLEX_TYPE:
7904       return native_interpret_complex (type, ptr, len);
7905
7906     case VECTOR_TYPE:
7907       return native_interpret_vector (type, ptr, len);
7908
7909     default:
7910       return NULL_TREE;
7911     }
7912 }
7913
7914
7915 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7916    TYPE at compile-time.  If we're unable to perform the conversion
7917    return NULL_TREE.  */
7918
7919 static tree
7920 fold_view_convert_expr (tree type, tree expr)
7921 {
7922   /* We support up to 512-bit values (for V8DFmode).  */
7923   unsigned char buffer[64];
7924   int len;
7925
7926   /* Check that the host and target are sane.  */
7927   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7928     return NULL_TREE;
7929
7930   len = native_encode_expr (expr, buffer, sizeof (buffer));
7931   if (len == 0)
7932     return NULL_TREE;
7933
7934   return native_interpret_expr (type, buffer, len);
7935 }
7936
7937 /* Build an expression for the address of T.  Folds away INDIRECT_REF
7938    to avoid confusing the gimplify process.  */
7939
7940 tree
7941 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7942 {
7943   /* The size of the object is not relevant when talking about its address.  */
7944   if (TREE_CODE (t) == WITH_SIZE_EXPR)
7945     t = TREE_OPERAND (t, 0);
7946
7947   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7948   if (TREE_CODE (t) == INDIRECT_REF
7949       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7950     {
7951       t = TREE_OPERAND (t, 0);
7952
7953       if (TREE_TYPE (t) != ptrtype)
7954         t = build1 (NOP_EXPR, ptrtype, t);
7955     }
7956   else
7957     t = build1 (ADDR_EXPR, ptrtype, t);
7958
7959   return t;
7960 }
7961
7962 /* Build an expression for the address of T.  */
7963
7964 tree
7965 build_fold_addr_expr (tree t)
7966 {
7967   tree ptrtype = build_pointer_type (TREE_TYPE (t));
7968
7969   return build_fold_addr_expr_with_type (t, ptrtype);
7970 }
7971
7972 /* Fold a unary expression of code CODE and type TYPE with operand
7973    OP0.  Return the folded expression if folding is successful.
7974    Otherwise, return NULL_TREE.  */
7975
7976 tree
7977 fold_unary (enum tree_code code, tree type, tree op0)
7978 {
7979   tree tem;
7980   tree arg0;
7981   enum tree_code_class kind = TREE_CODE_CLASS (code);
7982
7983   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7984               && TREE_CODE_LENGTH (code) == 1);
7985
7986   arg0 = op0;
7987   if (arg0)
7988     {
7989       if (CONVERT_EXPR_CODE_P (code)
7990           || code == FLOAT_EXPR || code == ABS_EXPR)
7991         {
7992           /* Don't use STRIP_NOPS, because signedness of argument type
7993              matters.  */
7994           STRIP_SIGN_NOPS (arg0);
7995         }
7996       else
7997         {
7998           /* Strip any conversions that don't change the mode.  This
7999              is safe for every expression, except for a comparison
8000              expression because its signedness is derived from its
8001              operands.
8002
8003              Note that this is done as an internal manipulation within
8004              the constant folder, in order to find the simplest
8005              representation of the arguments so that their form can be
8006              studied.  In any cases, the appropriate type conversions
8007              should be put back in the tree that will get out of the
8008              constant folder.  */
8009           STRIP_NOPS (arg0);
8010         }
8011     }
8012
8013   if (TREE_CODE_CLASS (code) == tcc_unary)
8014     {
8015       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8016         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8017                        fold_build1 (code, type,
8018                                     fold_convert (TREE_TYPE (op0),
8019                                                   TREE_OPERAND (arg0, 1))));
8020       else if (TREE_CODE (arg0) == COND_EXPR)
8021         {
8022           tree arg01 = TREE_OPERAND (arg0, 1);
8023           tree arg02 = TREE_OPERAND (arg0, 2);
8024           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8025             arg01 = fold_build1 (code, type,
8026                                  fold_convert (TREE_TYPE (op0), arg01));
8027           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8028             arg02 = fold_build1 (code, type,
8029                                  fold_convert (TREE_TYPE (op0), arg02));
8030           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8031                              arg01, arg02);
8032
8033           /* If this was a conversion, and all we did was to move into
8034              inside the COND_EXPR, bring it back out.  But leave it if
8035              it is a conversion from integer to integer and the
8036              result precision is no wider than a word since such a
8037              conversion is cheap and may be optimized away by combine,
8038              while it couldn't if it were outside the COND_EXPR.  Then return
8039              so we don't get into an infinite recursion loop taking the
8040              conversion out and then back in.  */
8041
8042           if ((CONVERT_EXPR_CODE_P (code)
8043                || code == NON_LVALUE_EXPR)
8044               && TREE_CODE (tem) == COND_EXPR
8045               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8046               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8047               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8048               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8049               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8050                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8051               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8052                      && (INTEGRAL_TYPE_P
8053                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8054                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8055                   || flag_syntax_only))
8056             tem = build1 (code, type,
8057                           build3 (COND_EXPR,
8058                                   TREE_TYPE (TREE_OPERAND
8059                                              (TREE_OPERAND (tem, 1), 0)),
8060                                   TREE_OPERAND (tem, 0),
8061                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8062                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8063           return tem;
8064         }
8065       else if (COMPARISON_CLASS_P (arg0))
8066         {
8067           if (TREE_CODE (type) == BOOLEAN_TYPE)
8068             {
8069               arg0 = copy_node (arg0);
8070               TREE_TYPE (arg0) = type;
8071               return arg0;
8072             }
8073           else if (TREE_CODE (type) != INTEGER_TYPE)
8074             return fold_build3 (COND_EXPR, type, arg0,
8075                                 fold_build1 (code, type,
8076                                              integer_one_node),
8077                                 fold_build1 (code, type,
8078                                              integer_zero_node));
8079         }
8080    }
8081
8082   switch (code)
8083     {
8084     case PAREN_EXPR:
8085       /* Re-association barriers around constants and other re-association
8086          barriers can be removed.  */
8087       if (CONSTANT_CLASS_P (op0)
8088           || TREE_CODE (op0) == PAREN_EXPR)
8089         return fold_convert (type, op0);
8090       return NULL_TREE;
8091
8092     CASE_CONVERT:
8093     case FLOAT_EXPR:
8094     case FIX_TRUNC_EXPR:
8095       if (TREE_TYPE (op0) == type)
8096         return op0;
8097       
8098       /* If we have (type) (a CMP b) and type is an integral type, return
8099          new expression involving the new type.  */
8100       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8101         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8102                             TREE_OPERAND (op0, 1));
8103
8104       /* Handle cases of two conversions in a row.  */
8105       if (CONVERT_EXPR_P (op0))
8106         {
8107           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8108           tree inter_type = TREE_TYPE (op0);
8109           int inside_int = INTEGRAL_TYPE_P (inside_type);
8110           int inside_ptr = POINTER_TYPE_P (inside_type);
8111           int inside_float = FLOAT_TYPE_P (inside_type);
8112           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8113           unsigned int inside_prec = TYPE_PRECISION (inside_type);
8114           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8115           int inter_int = INTEGRAL_TYPE_P (inter_type);
8116           int inter_ptr = POINTER_TYPE_P (inter_type);
8117           int inter_float = FLOAT_TYPE_P (inter_type);
8118           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8119           unsigned int inter_prec = TYPE_PRECISION (inter_type);
8120           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8121           int final_int = INTEGRAL_TYPE_P (type);
8122           int final_ptr = POINTER_TYPE_P (type);
8123           int final_float = FLOAT_TYPE_P (type);
8124           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8125           unsigned int final_prec = TYPE_PRECISION (type);
8126           int final_unsignedp = TYPE_UNSIGNED (type);
8127
8128           /* In addition to the cases of two conversions in a row
8129              handled below, if we are converting something to its own
8130              type via an object of identical or wider precision, neither
8131              conversion is needed.  */
8132           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8133               && (((inter_int || inter_ptr) && final_int)
8134                   || (inter_float && final_float))
8135               && inter_prec >= final_prec)
8136             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8137
8138           /* Likewise, if the intermediate and initial types are either both
8139              float or both integer, we don't need the middle conversion if the
8140              former is wider than the latter and doesn't change the signedness
8141              (for integers).  Avoid this if the final type is a pointer since
8142              then we sometimes need the middle conversion.  Likewise if the
8143              final type has a precision not equal to the size of its mode.  */
8144           if (((inter_int && inside_int)
8145                || (inter_float && inside_float)
8146                || (inter_vec && inside_vec))
8147               && inter_prec >= inside_prec
8148               && (inter_float || inter_vec
8149                   || inter_unsignedp == inside_unsignedp)
8150               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8151                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
8152               && ! final_ptr
8153               && (! final_vec || inter_prec == inside_prec))
8154             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8155
8156           /* If we have a sign-extension of a zero-extended value, we can
8157              replace that by a single zero-extension.  */
8158           if (inside_int && inter_int && final_int
8159               && inside_prec < inter_prec && inter_prec < final_prec
8160               && inside_unsignedp && !inter_unsignedp)
8161             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8162
8163           /* Two conversions in a row are not needed unless:
8164              - some conversion is floating-point (overstrict for now), or
8165              - some conversion is a vector (overstrict for now), or
8166              - the intermediate type is narrower than both initial and
8167                final, or
8168              - the intermediate type and innermost type differ in signedness,
8169                and the outermost type is wider than the intermediate, or
8170              - the initial type is a pointer type and the precisions of the
8171                intermediate and final types differ, or
8172              - the final type is a pointer type and the precisions of the
8173                initial and intermediate types differ.  */
8174           if (! inside_float && ! inter_float && ! final_float
8175               && ! inside_vec && ! inter_vec && ! final_vec
8176               && (inter_prec >= inside_prec || inter_prec >= final_prec)
8177               && ! (inside_int && inter_int
8178                     && inter_unsignedp != inside_unsignedp
8179                     && inter_prec < final_prec)
8180               && ((inter_unsignedp && inter_prec > inside_prec)
8181                   == (final_unsignedp && final_prec > inter_prec))
8182               && ! (inside_ptr && inter_prec != final_prec)
8183               && ! (final_ptr && inside_prec != inter_prec)
8184               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8185                     && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8186             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8187         }
8188
8189       /* Handle (T *)&A.B.C for A being of type T and B and C
8190          living at offset zero.  This occurs frequently in
8191          C++ upcasting and then accessing the base.  */
8192       if (TREE_CODE (op0) == ADDR_EXPR
8193           && POINTER_TYPE_P (type)
8194           && handled_component_p (TREE_OPERAND (op0, 0)))
8195         {
8196           HOST_WIDE_INT bitsize, bitpos;
8197           tree offset;
8198           enum machine_mode mode;
8199           int unsignedp, volatilep;
8200           tree base = TREE_OPERAND (op0, 0);
8201           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8202                                       &mode, &unsignedp, &volatilep, false);
8203           /* If the reference was to a (constant) zero offset, we can use
8204              the address of the base if it has the same base type
8205              as the result type.  */
8206           if (! offset && bitpos == 0
8207               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8208                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8209             return fold_convert (type, build_fold_addr_expr (base));
8210         }
8211
8212       if (TREE_CODE (op0) == MODIFY_EXPR
8213           && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8214           /* Detect assigning a bitfield.  */
8215           && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8216                && DECL_BIT_FIELD
8217                (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8218         {
8219           /* Don't leave an assignment inside a conversion
8220              unless assigning a bitfield.  */
8221           tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8222           /* First do the assignment, then return converted constant.  */
8223           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8224           TREE_NO_WARNING (tem) = 1;
8225           TREE_USED (tem) = 1;
8226           return tem;
8227         }
8228
8229       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8230          constants (if x has signed type, the sign bit cannot be set
8231          in c).  This folds extension into the BIT_AND_EXPR.
8232          ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8233          very likely don't have maximal range for their precision and this
8234          transformation effectively doesn't preserve non-maximal ranges.  */
8235       if (TREE_CODE (type) == INTEGER_TYPE
8236           && TREE_CODE (op0) == BIT_AND_EXPR
8237           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8238           /* Not if the conversion is to the sub-type.  */
8239           && TREE_TYPE (type) != TREE_TYPE (op0))
8240         {
8241           tree and = op0;
8242           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8243           int change = 0;
8244
8245           if (TYPE_UNSIGNED (TREE_TYPE (and))
8246               || (TYPE_PRECISION (type)
8247                   <= TYPE_PRECISION (TREE_TYPE (and))))
8248             change = 1;
8249           else if (TYPE_PRECISION (TREE_TYPE (and1))
8250                    <= HOST_BITS_PER_WIDE_INT
8251                    && host_integerp (and1, 1))
8252             {
8253               unsigned HOST_WIDE_INT cst;
8254
8255               cst = tree_low_cst (and1, 1);
8256               cst &= (HOST_WIDE_INT) -1
8257                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8258               change = (cst == 0);
8259 #ifdef LOAD_EXTEND_OP
8260               if (change
8261                   && !flag_syntax_only
8262                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8263                       == ZERO_EXTEND))
8264                 {
8265                   tree uns = unsigned_type_for (TREE_TYPE (and0));
8266                   and0 = fold_convert (uns, and0);
8267                   and1 = fold_convert (uns, and1);
8268                 }
8269 #endif
8270             }
8271           if (change)
8272             {
8273               tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8274                                            TREE_INT_CST_HIGH (and1), 0,
8275                                            TREE_OVERFLOW (and1));
8276               return fold_build2 (BIT_AND_EXPR, type,
8277                                   fold_convert (type, and0), tem);
8278             }
8279         }
8280
8281       /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8282          when one of the new casts will fold away. Conservatively we assume
8283          that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8284       if (POINTER_TYPE_P (type)
8285           && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8286           && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8287               || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8288               || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8289         {
8290           tree arg00 = TREE_OPERAND (arg0, 0);
8291           tree arg01 = TREE_OPERAND (arg0, 1);
8292
8293           return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8294                               fold_convert (sizetype, arg01));
8295         }
8296
8297       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8298          of the same precision, and X is an integer type not narrower than
8299          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
8300       if (INTEGRAL_TYPE_P (type)
8301           && TREE_CODE (op0) == BIT_NOT_EXPR
8302           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8303           && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8304           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8305         {
8306           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8307           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8308               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8309             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8310         }
8311
8312       /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8313          type of X and Y (integer types only).  */
8314       if (INTEGRAL_TYPE_P (type)
8315           && TREE_CODE (op0) == MULT_EXPR
8316           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8317           && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8318         {
8319           /* Be careful not to introduce new overflows.  */
8320           tree mult_type;
8321           if (TYPE_OVERFLOW_WRAPS (type))
8322             mult_type = type;
8323           else
8324             mult_type = unsigned_type_for (type);
8325
8326           if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8327             {
8328               tem = fold_build2 (MULT_EXPR, mult_type,
8329                                  fold_convert (mult_type,
8330                                                TREE_OPERAND (op0, 0)),
8331                                  fold_convert (mult_type,
8332                                                TREE_OPERAND (op0, 1)));
8333               return fold_convert (type, tem);
8334             }
8335         }
8336
8337       tem = fold_convert_const (code, type, op0);
8338       return tem ? tem : NULL_TREE;
8339
8340     case FIXED_CONVERT_EXPR:
8341       tem = fold_convert_const (code, type, arg0);
8342       return tem ? tem : NULL_TREE;
8343
8344     case VIEW_CONVERT_EXPR:
8345       if (TREE_TYPE (op0) == type)
8346         return op0;
8347       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8348         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8349
8350       /* For integral conversions with the same precision or pointer
8351          conversions use a NOP_EXPR instead.  */
8352       if ((INTEGRAL_TYPE_P (type)
8353            || POINTER_TYPE_P (type))
8354           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8355               || POINTER_TYPE_P (TREE_TYPE (op0)))
8356           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8357           /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8358              a sub-type to its base type as generated by the Ada FE.  */
8359           && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8360                && TREE_TYPE (TREE_TYPE (op0))))
8361         return fold_convert (type, op0);
8362
8363       /* Strip inner integral conversions that do not change the precision.  */
8364       if (CONVERT_EXPR_P (op0)
8365           && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8366               || POINTER_TYPE_P (TREE_TYPE (op0)))
8367           && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8368               || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8369           && (TYPE_PRECISION (TREE_TYPE (op0))
8370               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8371         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8372
8373       return fold_view_convert_expr (type, op0);
8374
8375     case NEGATE_EXPR:
8376       tem = fold_negate_expr (arg0);
8377       if (tem)
8378         return fold_convert (type, tem);
8379       return NULL_TREE;
8380
8381     case ABS_EXPR:
8382       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8383         return fold_abs_const (arg0, type);
8384       else if (TREE_CODE (arg0) == NEGATE_EXPR)
8385         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8386       /* Convert fabs((double)float) into (double)fabsf(float).  */
8387       else if (TREE_CODE (arg0) == NOP_EXPR
8388                && TREE_CODE (type) == REAL_TYPE)
8389         {
8390           tree targ0 = strip_float_extensions (arg0);
8391           if (targ0 != arg0)
8392             return fold_convert (type, fold_build1 (ABS_EXPR,
8393                                                     TREE_TYPE (targ0),
8394                                                     targ0));
8395         }
8396       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
8397       else if (TREE_CODE (arg0) == ABS_EXPR)
8398         return arg0;
8399       else if (tree_expr_nonnegative_p (arg0))
8400         return arg0;
8401
8402       /* Strip sign ops from argument.  */
8403       if (TREE_CODE (type) == REAL_TYPE)
8404         {
8405           tem = fold_strip_sign_ops (arg0);
8406           if (tem)
8407             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8408         }
8409       return NULL_TREE;
8410
8411     case CONJ_EXPR:
8412       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8413         return fold_convert (type, arg0);
8414       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8415         {
8416           tree itype = TREE_TYPE (type);
8417           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8418           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8419           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8420         }
8421       if (TREE_CODE (arg0) == COMPLEX_CST)
8422         {
8423           tree itype = TREE_TYPE (type);
8424           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8425           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8426           return build_complex (type, rpart, negate_expr (ipart));
8427         }
8428       if (TREE_CODE (arg0) == CONJ_EXPR)
8429         return fold_convert (type, TREE_OPERAND (arg0, 0));
8430       return NULL_TREE;
8431
8432     case BIT_NOT_EXPR:
8433       if (TREE_CODE (arg0) == INTEGER_CST)
8434         return fold_not_const (arg0, type);
8435       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8436         return fold_convert (type, TREE_OPERAND (arg0, 0));
8437       /* Convert ~ (-A) to A - 1.  */
8438       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8439         return fold_build2 (MINUS_EXPR, type,
8440                             fold_convert (type, TREE_OPERAND (arg0, 0)),
8441                             build_int_cst (type, 1));
8442       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
8443       else if (INTEGRAL_TYPE_P (type)
8444                && ((TREE_CODE (arg0) == MINUS_EXPR
8445                     && integer_onep (TREE_OPERAND (arg0, 1)))
8446                    || (TREE_CODE (arg0) == PLUS_EXPR
8447                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8448         return fold_build1 (NEGATE_EXPR, type,
8449                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8450       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
8451       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8452                && (tem = fold_unary (BIT_NOT_EXPR, type,
8453                                      fold_convert (type,
8454                                                    TREE_OPERAND (arg0, 0)))))
8455         return fold_build2 (BIT_XOR_EXPR, type, tem,
8456                             fold_convert (type, TREE_OPERAND (arg0, 1)));
8457       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8458                && (tem = fold_unary (BIT_NOT_EXPR, type,
8459                                      fold_convert (type,
8460                                                    TREE_OPERAND (arg0, 1)))))
8461         return fold_build2 (BIT_XOR_EXPR, type,
8462                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8463       /* Perform BIT_NOT_EXPR on each element individually.  */
8464       else if (TREE_CODE (arg0) == VECTOR_CST)
8465         {
8466           tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8467           int count = TYPE_VECTOR_SUBPARTS (type), i;
8468
8469           for (i = 0; i < count; i++)
8470             {
8471               if (elements)
8472                 {
8473                   elem = TREE_VALUE (elements);
8474                   elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8475                   if (elem == NULL_TREE)
8476                     break;
8477                   elements = TREE_CHAIN (elements);
8478                 }
8479               else
8480                 elem = build_int_cst (TREE_TYPE (type), -1);
8481               list = tree_cons (NULL_TREE, elem, list);
8482             }
8483           if (i == count)
8484             return build_vector (type, nreverse (list));
8485         }
8486
8487       return NULL_TREE;
8488
8489     case TRUTH_NOT_EXPR:
8490       /* The argument to invert_truthvalue must have Boolean type.  */
8491       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8492           arg0 = fold_convert (boolean_type_node, arg0);
8493
8494       /* Note that the operand of this must be an int
8495          and its values must be 0 or 1.
8496          ("true" is a fixed value perhaps depending on the language,
8497          but we don't handle values other than 1 correctly yet.)  */
8498       tem = fold_truth_not_expr (arg0);
8499       if (!tem)
8500         return NULL_TREE;
8501       return fold_convert (type, tem);
8502
8503     case REALPART_EXPR:
8504       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8505         return fold_convert (type, arg0);
8506       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8507         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8508                                  TREE_OPERAND (arg0, 1));
8509       if (TREE_CODE (arg0) == COMPLEX_CST)
8510         return fold_convert (type, TREE_REALPART (arg0));
8511       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8512         {
8513           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8514           tem = fold_build2 (TREE_CODE (arg0), itype,
8515                              fold_build1 (REALPART_EXPR, itype,
8516                                           TREE_OPERAND (arg0, 0)),
8517                              fold_build1 (REALPART_EXPR, itype,
8518                                           TREE_OPERAND (arg0, 1)));
8519           return fold_convert (type, tem);
8520         }
8521       if (TREE_CODE (arg0) == CONJ_EXPR)
8522         {
8523           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8524           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8525           return fold_convert (type, tem);
8526         }
8527       if (TREE_CODE (arg0) == CALL_EXPR)
8528         {
8529           tree fn = get_callee_fndecl (arg0);
8530           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8531             switch (DECL_FUNCTION_CODE (fn))
8532               {
8533               CASE_FLT_FN (BUILT_IN_CEXPI):
8534                 fn = mathfn_built_in (type, BUILT_IN_COS);
8535                 if (fn)
8536                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8537                 break;
8538
8539               default:
8540                 break;
8541               }
8542         }
8543       return NULL_TREE;
8544
8545     case IMAGPART_EXPR:
8546       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8547         return fold_convert (type, integer_zero_node);
8548       if (TREE_CODE (arg0) == COMPLEX_EXPR)
8549         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8550                                  TREE_OPERAND (arg0, 0));
8551       if (TREE_CODE (arg0) == COMPLEX_CST)
8552         return fold_convert (type, TREE_IMAGPART (arg0));
8553       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8554         {
8555           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8556           tem = fold_build2 (TREE_CODE (arg0), itype,
8557                              fold_build1 (IMAGPART_EXPR, itype,
8558                                           TREE_OPERAND (arg0, 0)),
8559                              fold_build1 (IMAGPART_EXPR, itype,
8560                                           TREE_OPERAND (arg0, 1)));
8561           return fold_convert (type, tem);
8562         }
8563       if (TREE_CODE (arg0) == CONJ_EXPR)
8564         {
8565           tree itype = TREE_TYPE (TREE_TYPE (arg0));
8566           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8567           return fold_convert (type, negate_expr (tem));
8568         }
8569       if (TREE_CODE (arg0) == CALL_EXPR)
8570         {
8571           tree fn = get_callee_fndecl (arg0);
8572           if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8573             switch (DECL_FUNCTION_CODE (fn))
8574               {
8575               CASE_FLT_FN (BUILT_IN_CEXPI):
8576                 fn = mathfn_built_in (type, BUILT_IN_SIN);
8577                 if (fn)
8578                   return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8579                 break;
8580
8581               default:
8582                 break;
8583               }
8584         }
8585       return NULL_TREE;
8586
8587     default:
8588       return NULL_TREE;
8589     } /* switch (code) */
8590 }
8591
8592
8593 /* If the operation was a conversion do _not_ mark a resulting constant
8594    with TREE_OVERFLOW if the original constant was not.  These conversions
8595    have implementation defined behavior and retaining the TREE_OVERFLOW
8596    flag here would confuse later passes such as VRP.  */
8597 tree
8598 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8599 {
8600   tree res = fold_unary (code, type, op0);
8601   if (res
8602       && TREE_CODE (res) == INTEGER_CST
8603       && TREE_CODE (op0) == INTEGER_CST
8604       && CONVERT_EXPR_CODE_P (code))
8605     TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8606
8607   return res;
8608 }
8609
8610 /* Fold a binary expression of code CODE and type TYPE with operands
8611    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8612    Return the folded expression if folding is successful.  Otherwise,
8613    return NULL_TREE.  */
8614
8615 static tree
8616 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8617 {
8618   enum tree_code compl_code;
8619
8620   if (code == MIN_EXPR)
8621     compl_code = MAX_EXPR;
8622   else if (code == MAX_EXPR)
8623     compl_code = MIN_EXPR;
8624   else
8625     gcc_unreachable ();
8626
8627   /* MIN (MAX (a, b), b) == b.  */
8628   if (TREE_CODE (op0) == compl_code
8629       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8630     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8631
8632   /* MIN (MAX (b, a), b) == b.  */
8633   if (TREE_CODE (op0) == compl_code
8634       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8635       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8636     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8637
8638   /* MIN (a, MAX (a, b)) == a.  */
8639   if (TREE_CODE (op1) == compl_code
8640       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8641       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8642     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8643
8644   /* MIN (a, MAX (b, a)) == a.  */
8645   if (TREE_CODE (op1) == compl_code
8646       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8647       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8648     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8649
8650   return NULL_TREE;
8651 }
8652
8653 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8654    by changing CODE to reduce the magnitude of constants involved in
8655    ARG0 of the comparison.
8656    Returns a canonicalized comparison tree if a simplification was
8657    possible, otherwise returns NULL_TREE.
8658    Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8659    valid if signed overflow is undefined.  */
8660
8661 static tree
8662 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8663                                  tree arg0, tree arg1,
8664                                  bool *strict_overflow_p)
8665 {
8666   enum tree_code code0 = TREE_CODE (arg0);
8667   tree t, cst0 = NULL_TREE;
8668   int sgn0;
8669   bool swap = false;
8670
8671   /* Match A +- CST code arg1 and CST code arg1.  We can change the
8672      first form only if overflow is undefined.  */
8673   if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8674          /* In principle pointers also have undefined overflow behavior,
8675             but that causes problems elsewhere.  */
8676          && !POINTER_TYPE_P (TREE_TYPE (arg0))
8677          && (code0 == MINUS_EXPR
8678              || code0 == PLUS_EXPR)
8679          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8680         || code0 == INTEGER_CST))
8681     return NULL_TREE;
8682
8683   /* Identify the constant in arg0 and its sign.  */
8684   if (code0 == INTEGER_CST)
8685     cst0 = arg0;
8686   else
8687     cst0 = TREE_OPERAND (arg0, 1);
8688   sgn0 = tree_int_cst_sgn (cst0);
8689
8690   /* Overflowed constants and zero will cause problems.  */
8691   if (integer_zerop (cst0)
8692       || TREE_OVERFLOW (cst0))
8693     return NULL_TREE;
8694
8695   /* See if we can reduce the magnitude of the constant in
8696      arg0 by changing the comparison code.  */
8697   if (code0 == INTEGER_CST)
8698     {
8699       /* CST <= arg1  ->  CST-1 < arg1.  */
8700       if (code == LE_EXPR && sgn0 == 1)
8701         code = LT_EXPR;
8702       /* -CST < arg1  ->  -CST-1 <= arg1.  */
8703       else if (code == LT_EXPR && sgn0 == -1)
8704         code = LE_EXPR;
8705       /* CST > arg1  ->  CST-1 >= arg1.  */
8706       else if (code == GT_EXPR && sgn0 == 1)
8707         code = GE_EXPR;
8708       /* -CST >= arg1  ->  -CST-1 > arg1.  */
8709       else if (code == GE_EXPR && sgn0 == -1)
8710         code = GT_EXPR;
8711       else
8712         return NULL_TREE;
8713       /* arg1 code' CST' might be more canonical.  */
8714       swap = true;
8715     }
8716   else
8717     {
8718       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
8719       if (code == LT_EXPR
8720           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8721         code = LE_EXPR;
8722       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
8723       else if (code == GT_EXPR
8724                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8725         code = GE_EXPR;
8726       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
8727       else if (code == LE_EXPR
8728                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8729         code = LT_EXPR;
8730       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
8731       else if (code == GE_EXPR
8732                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8733         code = GT_EXPR;
8734       else
8735         return NULL_TREE;
8736       *strict_overflow_p = true;
8737     }
8738
8739   /* Now build the constant reduced in magnitude.  But not if that
8740      would produce one outside of its types range.  */
8741   if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8742       && ((sgn0 == 1
8743            && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8744            && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8745           || (sgn0 == -1
8746               && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8747               && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8748     /* We cannot swap the comparison here as that would cause us to
8749        endlessly recurse.  */
8750     return NULL_TREE;
8751
8752   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8753                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8754   if (code0 != INTEGER_CST)
8755     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8756
8757   /* If swapping might yield to a more canonical form, do so.  */
8758   if (swap)
8759     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8760   else
8761     return fold_build2 (code, type, t, arg1);
8762 }
8763
8764 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8765    overflow further.  Try to decrease the magnitude of constants involved
8766    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8767    and put sole constants at the second argument position.
8768    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
8769
8770 static tree
8771 maybe_canonicalize_comparison (enum tree_code code, tree type,
8772                                tree arg0, tree arg1)
8773 {
8774   tree t;
8775   bool strict_overflow_p;
8776   const char * const warnmsg = G_("assuming signed overflow does not occur "
8777                                   "when reducing constant in comparison");
8778
8779   /* Try canonicalization by simplifying arg0.  */
8780   strict_overflow_p = false;
8781   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8782                                        &strict_overflow_p);
8783   if (t)
8784     {
8785       if (strict_overflow_p)
8786         fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8787       return t;
8788     }
8789
8790   /* Try canonicalization by simplifying arg1 using the swapped
8791      comparison.  */
8792   code = swap_tree_comparison (code);
8793   strict_overflow_p = false;
8794   t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8795                                        &strict_overflow_p);
8796   if (t && strict_overflow_p)
8797     fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8798   return t;
8799 }
8800
8801 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8802    space.  This is used to avoid issuing overflow warnings for
8803    expressions like &p->x which can not wrap.  */
8804
8805 static bool
8806 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8807 {
8808   unsigned HOST_WIDE_INT offset_low, total_low;
8809   HOST_WIDE_INT size, offset_high, total_high;
8810
8811   if (!POINTER_TYPE_P (TREE_TYPE (base)))
8812     return true;
8813
8814   if (bitpos < 0)
8815     return true;
8816
8817   if (offset == NULL_TREE)
8818     {
8819       offset_low = 0;
8820       offset_high = 0;
8821     }
8822   else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8823     return true;
8824   else
8825     {
8826       offset_low = TREE_INT_CST_LOW (offset);
8827       offset_high = TREE_INT_CST_HIGH (offset);
8828     }
8829
8830   if (add_double_with_sign (offset_low, offset_high,
8831                             bitpos / BITS_PER_UNIT, 0,
8832                             &total_low, &total_high,
8833                             true))
8834     return true;
8835
8836   if (total_high != 0)
8837     return true;
8838
8839   size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8840   if (size <= 0)
8841     return true;
8842
8843   /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8844      array.  */
8845   if (TREE_CODE (base) == ADDR_EXPR)
8846     {
8847       HOST_WIDE_INT base_size;
8848
8849       base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8850       if (base_size > 0 && size < base_size)
8851         size = base_size;
8852     }
8853
8854   return total_low > (unsigned HOST_WIDE_INT) size;
8855 }
8856
8857 /* Subroutine of fold_binary.  This routine performs all of the
8858    transformations that are common to the equality/inequality
8859    operators (EQ_EXPR and NE_EXPR) and the ordering operators
8860    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
8861    fold_binary should call fold_binary.  Fold a comparison with
8862    tree code CODE and type TYPE with operands OP0 and OP1.  Return
8863    the folded comparison or NULL_TREE.  */
8864
8865 static tree
8866 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8867 {
8868   tree arg0, arg1, tem;
8869
8870   arg0 = op0;
8871   arg1 = op1;
8872
8873   STRIP_SIGN_NOPS (arg0);
8874   STRIP_SIGN_NOPS (arg1);
8875
8876   tem = fold_relational_const (code, type, arg0, arg1);
8877   if (tem != NULL_TREE)
8878     return tem;
8879
8880   /* If one arg is a real or integer constant, put it last.  */
8881   if (tree_swap_operands_p (arg0, arg1, true))
8882     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8883
8884   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
8885   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8886       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8887           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8888           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8889       && (TREE_CODE (arg1) == INTEGER_CST
8890           && !TREE_OVERFLOW (arg1)))
8891     {
8892       tree const1 = TREE_OPERAND (arg0, 1);
8893       tree const2 = arg1;
8894       tree variable = TREE_OPERAND (arg0, 0);
8895       tree lhs;
8896       int lhs_add;
8897       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8898
8899       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8900                          TREE_TYPE (arg1), const2, const1);
8901
8902       /* If the constant operation overflowed this can be
8903          simplified as a comparison against INT_MAX/INT_MIN.  */
8904       if (TREE_CODE (lhs) == INTEGER_CST
8905           && TREE_OVERFLOW (lhs))
8906         {
8907           int const1_sgn = tree_int_cst_sgn (const1);
8908           enum tree_code code2 = code;
8909
8910           /* Get the sign of the constant on the lhs if the
8911              operation were VARIABLE + CONST1.  */
8912           if (TREE_CODE (arg0) == MINUS_EXPR)
8913             const1_sgn = -const1_sgn;
8914
8915           /* The sign of the constant determines if we overflowed
8916              INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8917              Canonicalize to the INT_MIN overflow by swapping the comparison
8918              if necessary.  */
8919           if (const1_sgn == -1)
8920             code2 = swap_tree_comparison (code);
8921
8922           /* We now can look at the canonicalized case
8923                VARIABLE + 1  CODE2  INT_MIN
8924              and decide on the result.  */
8925           if (code2 == LT_EXPR
8926               || code2 == LE_EXPR
8927               || code2 == EQ_EXPR)
8928             return omit_one_operand (type, boolean_false_node, variable);
8929           else if (code2 == NE_EXPR
8930                    || code2 == GE_EXPR
8931                    || code2 == GT_EXPR)
8932             return omit_one_operand (type, boolean_true_node, variable);
8933         }
8934
8935       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8936           && (TREE_CODE (lhs) != INTEGER_CST
8937               || !TREE_OVERFLOW (lhs)))
8938         {
8939           fold_overflow_warning (("assuming signed overflow does not occur "
8940                                   "when changing X +- C1 cmp C2 to "
8941                                   "X cmp C1 +- C2"),
8942                                  WARN_STRICT_OVERFLOW_COMPARISON);
8943           return fold_build2 (code, type, variable, lhs);
8944         }
8945     }
8946
8947   /* For comparisons of pointers we can decompose it to a compile time
8948      comparison of the base objects and the offsets into the object.
8949      This requires at least one operand being an ADDR_EXPR or a
8950      POINTER_PLUS_EXPR to do more than the operand_equal_p test below.  */
8951   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8952       && (TREE_CODE (arg0) == ADDR_EXPR
8953           || TREE_CODE (arg1) == ADDR_EXPR
8954           || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8955           || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8956     {
8957       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8958       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8959       enum machine_mode mode;
8960       int volatilep, unsignedp;
8961       bool indirect_base0 = false, indirect_base1 = false;
8962
8963       /* Get base and offset for the access.  Strip ADDR_EXPR for
8964          get_inner_reference, but put it back by stripping INDIRECT_REF
8965          off the base object if possible.  indirect_baseN will be true
8966          if baseN is not an address but refers to the object itself.  */
8967       base0 = arg0;
8968       if (TREE_CODE (arg0) == ADDR_EXPR)
8969         {
8970           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8971                                        &bitsize, &bitpos0, &offset0, &mode,
8972                                        &unsignedp, &volatilep, false);
8973           if (TREE_CODE (base0) == INDIRECT_REF)
8974             base0 = TREE_OPERAND (base0, 0);
8975           else
8976             indirect_base0 = true;
8977         }
8978       else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8979         {
8980           base0 = TREE_OPERAND (arg0, 0);
8981           offset0 = TREE_OPERAND (arg0, 1);
8982         }
8983
8984       base1 = arg1;
8985       if (TREE_CODE (arg1) == ADDR_EXPR)
8986         {
8987           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8988                                        &bitsize, &bitpos1, &offset1, &mode,
8989                                        &unsignedp, &volatilep, false);
8990           if (TREE_CODE (base1) == INDIRECT_REF)
8991             base1 = TREE_OPERAND (base1, 0);
8992           else
8993             indirect_base1 = true;
8994         }
8995       else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8996         {
8997           base1 = TREE_OPERAND (arg1, 0);
8998           offset1 = TREE_OPERAND (arg1, 1);
8999         }
9000
9001       /* If we have equivalent bases we might be able to simplify.  */
9002       if (indirect_base0 == indirect_base1
9003           && operand_equal_p (base0, base1, 0))
9004         {
9005           /* We can fold this expression to a constant if the non-constant
9006              offset parts are equal.  */
9007           if ((offset0 == offset1
9008                || (offset0 && offset1
9009                    && operand_equal_p (offset0, offset1, 0)))
9010               && (code == EQ_EXPR
9011                   || code == NE_EXPR
9012                   || POINTER_TYPE_OVERFLOW_UNDEFINED))
9013                 
9014             {
9015               if (code != EQ_EXPR
9016                   && code != NE_EXPR
9017                   && bitpos0 != bitpos1
9018                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9019                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9020                 fold_overflow_warning (("assuming pointer wraparound does not "
9021                                         "occur when comparing P +- C1 with "
9022                                         "P +- C2"),
9023                                        WARN_STRICT_OVERFLOW_CONDITIONAL);
9024
9025               switch (code)
9026                 {
9027                 case EQ_EXPR:
9028                   return constant_boolean_node (bitpos0 == bitpos1, type);
9029                 case NE_EXPR:
9030                   return constant_boolean_node (bitpos0 != bitpos1, type);
9031                 case LT_EXPR:
9032                   return constant_boolean_node (bitpos0 < bitpos1, type);
9033                 case LE_EXPR:
9034                   return constant_boolean_node (bitpos0 <= bitpos1, type);
9035                 case GE_EXPR:
9036                   return constant_boolean_node (bitpos0 >= bitpos1, type);
9037                 case GT_EXPR:
9038                   return constant_boolean_node (bitpos0 > bitpos1, type);
9039                 default:;
9040                 }
9041             }
9042           /* We can simplify the comparison to a comparison of the variable
9043              offset parts if the constant offset parts are equal.
9044              Be careful to use signed size type here because otherwise we
9045              mess with array offsets in the wrong way.  This is possible
9046              because pointer arithmetic is restricted to retain within an
9047              object and overflow on pointer differences is undefined as of
9048              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
9049           else if (bitpos0 == bitpos1
9050                    && ((code == EQ_EXPR || code == NE_EXPR)
9051                        || POINTER_TYPE_OVERFLOW_UNDEFINED))
9052             {
9053               tree signed_size_type_node;
9054               signed_size_type_node = signed_type_for (size_type_node);
9055
9056               /* By converting to signed size type we cover middle-end pointer
9057                  arithmetic which operates on unsigned pointer types of size
9058                  type size and ARRAY_REF offsets which are properly sign or
9059                  zero extended from their type in case it is narrower than
9060                  size type.  */
9061               if (offset0 == NULL_TREE)
9062                 offset0 = build_int_cst (signed_size_type_node, 0);
9063               else
9064                 offset0 = fold_convert (signed_size_type_node, offset0);
9065               if (offset1 == NULL_TREE)
9066                 offset1 = build_int_cst (signed_size_type_node, 0);
9067               else
9068                 offset1 = fold_convert (signed_size_type_node, offset1);
9069
9070               if (code != EQ_EXPR
9071                   && code != NE_EXPR
9072                   && (pointer_may_wrap_p (base0, offset0, bitpos0)
9073                       || pointer_may_wrap_p (base1, offset1, bitpos1)))
9074                 fold_overflow_warning (("assuming pointer wraparound does not "
9075                                         "occur when comparing P +- C1 with "
9076                                         "P +- C2"),
9077                                        WARN_STRICT_OVERFLOW_COMPARISON);
9078
9079               return fold_build2 (code, type, offset0, offset1);
9080             }
9081         }
9082       /* For non-equal bases we can simplify if they are addresses
9083          of local binding decls or constants.  */
9084       else if (indirect_base0 && indirect_base1
9085                /* We know that !operand_equal_p (base0, base1, 0)
9086                   because the if condition was false.  But make
9087                   sure two decls are not the same.  */
9088                && base0 != base1
9089                && TREE_CODE (arg0) == ADDR_EXPR
9090                && TREE_CODE (arg1) == ADDR_EXPR
9091                && (((TREE_CODE (base0) == VAR_DECL
9092                      || TREE_CODE (base0) == PARM_DECL)
9093                     && (targetm.binds_local_p (base0)
9094                         || CONSTANT_CLASS_P (base1)))
9095                    || CONSTANT_CLASS_P (base0))
9096                && (((TREE_CODE (base1) == VAR_DECL
9097                      || TREE_CODE (base1) == PARM_DECL)
9098                     && (targetm.binds_local_p (base1)
9099                         || CONSTANT_CLASS_P (base0)))
9100                    || CONSTANT_CLASS_P (base1)))
9101         {
9102           if (code == EQ_EXPR)
9103             return omit_two_operands (type, boolean_false_node, arg0, arg1);
9104           else if (code == NE_EXPR)
9105             return omit_two_operands (type, boolean_true_node, arg0, arg1);
9106         }
9107       /* For equal offsets we can simplify to a comparison of the
9108          base addresses.  */
9109       else if (bitpos0 == bitpos1
9110                && (indirect_base0
9111                    ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9112                && (indirect_base1
9113                    ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9114                && ((offset0 == offset1)
9115                    || (offset0 && offset1
9116                        && operand_equal_p (offset0, offset1, 0))))
9117         {
9118           if (indirect_base0)
9119             base0 = build_fold_addr_expr (base0);
9120           if (indirect_base1)
9121             base1 = build_fold_addr_expr (base1);
9122           return fold_build2 (code, type, base0, base1);
9123         }
9124     }
9125
9126   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9127      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
9128      the resulting offset is smaller in absolute value than the
9129      original one.  */
9130   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9131       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9132       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9133           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9134       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9135       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9136           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9137     {
9138       tree const1 = TREE_OPERAND (arg0, 1);
9139       tree const2 = TREE_OPERAND (arg1, 1);
9140       tree variable1 = TREE_OPERAND (arg0, 0);
9141       tree variable2 = TREE_OPERAND (arg1, 0);
9142       tree cst;
9143       const char * const warnmsg = G_("assuming signed overflow does not "
9144                                       "occur when combining constants around "
9145                                       "a comparison");
9146
9147       /* Put the constant on the side where it doesn't overflow and is
9148          of lower absolute value than before.  */
9149       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9150                              ? MINUS_EXPR : PLUS_EXPR,
9151                              const2, const1, 0);
9152       if (!TREE_OVERFLOW (cst)
9153           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9154         {
9155           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9156           return fold_build2 (code, type,
9157                               variable1,
9158                               fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9159                                            variable2, cst));
9160         }
9161
9162       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9163                              ? MINUS_EXPR : PLUS_EXPR,
9164                              const1, const2, 0);
9165       if (!TREE_OVERFLOW (cst)
9166           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9167         {
9168           fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9169           return fold_build2 (code, type,
9170                               fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9171                                            variable1, cst),
9172                               variable2);
9173         }
9174     }
9175
9176   /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9177      signed arithmetic case.  That form is created by the compiler
9178      often enough for folding it to be of value.  One example is in
9179      computing loop trip counts after Operator Strength Reduction.  */
9180   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9181       && TREE_CODE (arg0) == MULT_EXPR
9182       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9183           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9184       && integer_zerop (arg1))
9185     {
9186       tree const1 = TREE_OPERAND (arg0, 1);
9187       tree const2 = arg1;                       /* zero */
9188       tree variable1 = TREE_OPERAND (arg0, 0);
9189       enum tree_code cmp_code = code;
9190
9191       gcc_assert (!integer_zerop (const1));
9192
9193       fold_overflow_warning (("assuming signed overflow does not occur when "
9194                               "eliminating multiplication in comparison "
9195                               "with zero"),
9196                              WARN_STRICT_OVERFLOW_COMPARISON);
9197
9198       /* If const1 is negative we swap the sense of the comparison.  */
9199       if (tree_int_cst_sgn (const1) < 0)
9200         cmp_code = swap_tree_comparison (cmp_code);
9201
9202       return fold_build2 (cmp_code, type, variable1, const2);
9203     }
9204
9205   tem = maybe_canonicalize_comparison (code, type, op0, op1);
9206   if (tem)
9207     return tem;
9208
9209   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9210     {
9211       tree targ0 = strip_float_extensions (arg0);
9212       tree targ1 = strip_float_extensions (arg1);
9213       tree newtype = TREE_TYPE (targ0);
9214
9215       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9216         newtype = TREE_TYPE (targ1);
9217
9218       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
9219       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9220         return fold_build2 (code, type, fold_convert (newtype, targ0),
9221                             fold_convert (newtype, targ1));
9222
9223       /* (-a) CMP (-b) -> b CMP a  */
9224       if (TREE_CODE (arg0) == NEGATE_EXPR
9225           && TREE_CODE (arg1) == NEGATE_EXPR)
9226         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9227                             TREE_OPERAND (arg0, 0));
9228
9229       if (TREE_CODE (arg1) == REAL_CST)
9230         {
9231           REAL_VALUE_TYPE cst;
9232           cst = TREE_REAL_CST (arg1);
9233
9234           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
9235           if (TREE_CODE (arg0) == NEGATE_EXPR)
9236             return fold_build2 (swap_tree_comparison (code), type,
9237                                 TREE_OPERAND (arg0, 0),
9238                                 build_real (TREE_TYPE (arg1),
9239                                             REAL_VALUE_NEGATE (cst)));
9240
9241           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
9242           /* a CMP (-0) -> a CMP 0  */
9243           if (REAL_VALUE_MINUS_ZERO (cst))
9244             return fold_build2 (code, type, arg0,
9245                                 build_real (TREE_TYPE (arg1), dconst0));
9246
9247           /* x != NaN is always true, other ops are always false.  */
9248           if (REAL_VALUE_ISNAN (cst)
9249               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9250             {
9251               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9252               return omit_one_operand (type, tem, arg0);
9253             }
9254
9255           /* Fold comparisons against infinity.  */
9256           if (REAL_VALUE_ISINF (cst)
9257               && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9258             {
9259               tem = fold_inf_compare (code, type, arg0, arg1);
9260               if (tem != NULL_TREE)
9261                 return tem;
9262             }
9263         }
9264
9265       /* If this is a comparison of a real constant with a PLUS_EXPR
9266          or a MINUS_EXPR of a real constant, we can convert it into a
9267          comparison with a revised real constant as long as no overflow
9268          occurs when unsafe_math_optimizations are enabled.  */
9269       if (flag_unsafe_math_optimizations
9270           && TREE_CODE (arg1) == REAL_CST
9271           && (TREE_CODE (arg0) == PLUS_EXPR
9272               || TREE_CODE (arg0) == MINUS_EXPR)
9273           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9274           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9275                                       ? MINUS_EXPR : PLUS_EXPR,
9276                                       arg1, TREE_OPERAND (arg0, 1), 0))
9277           && !TREE_OVERFLOW (tem))
9278         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9279
9280       /* Likewise, we can simplify a comparison of a real constant with
9281          a MINUS_EXPR whose first operand is also a real constant, i.e.
9282          (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on 
9283          floating-point types only if -fassociative-math is set.  */
9284       if (flag_associative_math
9285           && TREE_CODE (arg1) == REAL_CST
9286           && TREE_CODE (arg0) == MINUS_EXPR
9287           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9288           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9289                                       arg1, 0))
9290           && !TREE_OVERFLOW (tem))
9291         return fold_build2 (swap_tree_comparison (code), type,
9292                             TREE_OPERAND (arg0, 1), tem);
9293
9294       /* Fold comparisons against built-in math functions.  */
9295       if (TREE_CODE (arg1) == REAL_CST
9296           && flag_unsafe_math_optimizations
9297           && ! flag_errno_math)
9298         {
9299           enum built_in_function fcode = builtin_mathfn_code (arg0);
9300
9301           if (fcode != END_BUILTINS)
9302             {
9303               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9304               if (tem != NULL_TREE)
9305                 return tem;
9306             }
9307         }
9308     }
9309
9310   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9311       && CONVERT_EXPR_P (arg0))
9312     {
9313       /* If we are widening one operand of an integer comparison,
9314          see if the other operand is similarly being widened.  Perhaps we
9315          can do the comparison in the narrower type.  */
9316       tem = fold_widened_comparison (code, type, arg0, arg1);
9317       if (tem)
9318         return tem;
9319
9320       /* Or if we are changing signedness.  */
9321       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9322       if (tem)
9323         return tem;
9324     }
9325
9326   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9327      constant, we can simplify it.  */
9328   if (TREE_CODE (arg1) == INTEGER_CST
9329       && (TREE_CODE (arg0) == MIN_EXPR
9330           || TREE_CODE (arg0) == MAX_EXPR)
9331       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9332     {
9333       tem = optimize_minmax_comparison (code, type, op0, op1);
9334       if (tem)
9335         return tem;
9336     }
9337
9338   /* Simplify comparison of something with itself.  (For IEEE
9339      floating-point, we can only do some of these simplifications.)  */
9340   if (operand_equal_p (arg0, arg1, 0))
9341     {
9342       switch (code)
9343         {
9344         case EQ_EXPR:
9345           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9346               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9347             return constant_boolean_node (1, type);
9348           break;
9349
9350         case GE_EXPR:
9351         case LE_EXPR:
9352           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9353               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9354             return constant_boolean_node (1, type);
9355           return fold_build2 (EQ_EXPR, type, arg0, arg1);
9356
9357         case NE_EXPR:
9358           /* For NE, we can only do this simplification if integer
9359              or we don't honor IEEE floating point NaNs.  */
9360           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9361               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9362             break;
9363           /* ... fall through ...  */
9364         case GT_EXPR:
9365         case LT_EXPR:
9366           return constant_boolean_node (0, type);
9367         default:
9368           gcc_unreachable ();
9369         }
9370     }
9371
9372   /* If we are comparing an expression that just has comparisons
9373      of two integer values, arithmetic expressions of those comparisons,
9374      and constants, we can simplify it.  There are only three cases
9375      to check: the two values can either be equal, the first can be
9376      greater, or the second can be greater.  Fold the expression for
9377      those three values.  Since each value must be 0 or 1, we have
9378      eight possibilities, each of which corresponds to the constant 0
9379      or 1 or one of the six possible comparisons.
9380
9381      This handles common cases like (a > b) == 0 but also handles
9382      expressions like  ((x > y) - (y > x)) > 0, which supposedly
9383      occur in macroized code.  */
9384
9385   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9386     {
9387       tree cval1 = 0, cval2 = 0;
9388       int save_p = 0;
9389
9390       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9391           /* Don't handle degenerate cases here; they should already
9392              have been handled anyway.  */
9393           && cval1 != 0 && cval2 != 0
9394           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9395           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9396           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9397           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9398           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9399           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9400                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9401         {
9402           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9403           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9404
9405           /* We can't just pass T to eval_subst in case cval1 or cval2
9406              was the same as ARG1.  */
9407
9408           tree high_result
9409                 = fold_build2 (code, type,
9410                                eval_subst (arg0, cval1, maxval,
9411                                            cval2, minval),
9412                                arg1);
9413           tree equal_result
9414                 = fold_build2 (code, type,
9415                                eval_subst (arg0, cval1, maxval,
9416                                            cval2, maxval),
9417                                arg1);
9418           tree low_result
9419                 = fold_build2 (code, type,
9420                                eval_subst (arg0, cval1, minval,
9421                                            cval2, maxval),
9422                                arg1);
9423
9424           /* All three of these results should be 0 or 1.  Confirm they are.
9425              Then use those values to select the proper code to use.  */
9426
9427           if (TREE_CODE (high_result) == INTEGER_CST
9428               && TREE_CODE (equal_result) == INTEGER_CST
9429               && TREE_CODE (low_result) == INTEGER_CST)
9430             {
9431               /* Make a 3-bit mask with the high-order bit being the
9432                  value for `>', the next for '=', and the low for '<'.  */
9433               switch ((integer_onep (high_result) * 4)
9434                       + (integer_onep (equal_result) * 2)
9435                       + integer_onep (low_result))
9436                 {
9437                 case 0:
9438                   /* Always false.  */
9439                   return omit_one_operand (type, integer_zero_node, arg0);
9440                 case 1:
9441                   code = LT_EXPR;
9442                   break;
9443                 case 2:
9444                   code = EQ_EXPR;
9445                   break;
9446                 case 3:
9447                   code = LE_EXPR;
9448                   break;
9449                 case 4:
9450                   code = GT_EXPR;
9451                   break;
9452                 case 5:
9453                   code = NE_EXPR;
9454                   break;
9455                 case 6:
9456                   code = GE_EXPR;
9457                   break;
9458                 case 7:
9459                   /* Always true.  */
9460                   return omit_one_operand (type, integer_one_node, arg0);
9461                 }
9462
9463               if (save_p)
9464                 return save_expr (build2 (code, type, cval1, cval2));
9465               return fold_build2 (code, type, cval1, cval2);
9466             }
9467         }
9468     }
9469
9470   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9471      into a single range test.  */
9472   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9473        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9474       && TREE_CODE (arg1) == INTEGER_CST
9475       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9476       && !integer_zerop (TREE_OPERAND (arg0, 1))
9477       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9478       && !TREE_OVERFLOW (arg1))
9479     {
9480       tem = fold_div_compare (code, type, arg0, arg1);
9481       if (tem != NULL_TREE)
9482         return tem;
9483     }
9484
9485   /* Fold ~X op ~Y as Y op X.  */
9486   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9487       && TREE_CODE (arg1) == BIT_NOT_EXPR)
9488     {
9489       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9490       return fold_build2 (code, type,
9491                           fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9492                           TREE_OPERAND (arg0, 0));
9493     }
9494
9495   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
9496   if (TREE_CODE (arg0) == BIT_NOT_EXPR
9497       && TREE_CODE (arg1) == INTEGER_CST)
9498     {
9499       tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9500       return fold_build2 (swap_tree_comparison (code), type,
9501                           TREE_OPERAND (arg0, 0),
9502                           fold_build1 (BIT_NOT_EXPR, cmp_type,
9503                                        fold_convert (cmp_type, arg1)));
9504     }
9505
9506   return NULL_TREE;
9507 }
9508
9509
9510 /* Subroutine of fold_binary.  Optimize complex multiplications of the
9511    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
9512    argument EXPR represents the expression "z" of type TYPE.  */
9513
9514 static tree
9515 fold_mult_zconjz (tree type, tree expr)
9516 {
9517   tree itype = TREE_TYPE (type);
9518   tree rpart, ipart, tem;
9519
9520   if (TREE_CODE (expr) == COMPLEX_EXPR)
9521     {
9522       rpart = TREE_OPERAND (expr, 0);
9523       ipart = TREE_OPERAND (expr, 1);
9524     }
9525   else if (TREE_CODE (expr) == COMPLEX_CST)
9526     {
9527       rpart = TREE_REALPART (expr);
9528       ipart = TREE_IMAGPART (expr);
9529     }
9530   else
9531     {
9532       expr = save_expr (expr);
9533       rpart = fold_build1 (REALPART_EXPR, itype, expr);
9534       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9535     }
9536
9537   rpart = save_expr (rpart);
9538   ipart = save_expr (ipart);
9539   tem = fold_build2 (PLUS_EXPR, itype,
9540                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
9541                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
9542   return fold_build2 (COMPLEX_EXPR, type, tem,
9543                       fold_convert (itype, integer_zero_node));
9544 }
9545
9546
9547 /* Subroutine of fold_binary.  If P is the value of EXPR, computes
9548    power-of-two M and (arbitrary) N such that M divides (P-N).  This condition
9549    guarantees that P and N have the same least significant log2(M) bits.
9550    N is not otherwise constrained.  In particular, N is not normalized to
9551    0 <= N < M as is common.  In general, the precise value of P is unknown.
9552    M is chosen as large as possible such that constant N can be determined.
9553
9554    Returns M and sets *RESIDUE to N.
9555
9556    If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9557    account.  This is not always possible due to PR 35705.
9558  */
9559
9560 static unsigned HOST_WIDE_INT
9561 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9562                                  bool allow_func_align)
9563 {
9564   enum tree_code code;
9565
9566   *residue = 0;
9567
9568   code = TREE_CODE (expr);
9569   if (code == ADDR_EXPR)
9570     {
9571       expr = TREE_OPERAND (expr, 0);
9572       if (handled_component_p (expr))
9573         {
9574           HOST_WIDE_INT bitsize, bitpos;
9575           tree offset;
9576           enum machine_mode mode;
9577           int unsignedp, volatilep;
9578
9579           expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9580                                       &mode, &unsignedp, &volatilep, false);
9581           *residue = bitpos / BITS_PER_UNIT;
9582           if (offset)
9583             {
9584               if (TREE_CODE (offset) == INTEGER_CST)
9585                 *residue += TREE_INT_CST_LOW (offset);
9586               else
9587                 /* We don't handle more complicated offset expressions.  */
9588                 return 1;
9589             }
9590         }
9591
9592       if (DECL_P (expr)
9593           && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9594         return DECL_ALIGN_UNIT (expr);
9595     }
9596   else if (code == POINTER_PLUS_EXPR)
9597     {
9598       tree op0, op1;
9599       unsigned HOST_WIDE_INT modulus;
9600       enum tree_code inner_code;
9601       
9602       op0 = TREE_OPERAND (expr, 0);
9603       STRIP_NOPS (op0);
9604       modulus = get_pointer_modulus_and_residue (op0, residue,
9605                                                  allow_func_align);
9606
9607       op1 = TREE_OPERAND (expr, 1);
9608       STRIP_NOPS (op1);
9609       inner_code = TREE_CODE (op1);
9610       if (inner_code == INTEGER_CST)
9611         {
9612           *residue += TREE_INT_CST_LOW (op1);
9613           return modulus;
9614         }
9615       else if (inner_code == MULT_EXPR)
9616         {
9617           op1 = TREE_OPERAND (op1, 1);
9618           if (TREE_CODE (op1) == INTEGER_CST)
9619             {
9620               unsigned HOST_WIDE_INT align;
9621               
9622               /* Compute the greatest power-of-2 divisor of op1.  */
9623               align = TREE_INT_CST_LOW (op1);
9624               align &= -align;
9625
9626               /* If align is non-zero and less than *modulus, replace
9627                  *modulus with align., If align is 0, then either op1 is 0
9628                  or the greatest power-of-2 divisor of op1 doesn't fit in an
9629                  unsigned HOST_WIDE_INT.  In either case, no additional
9630                  constraint is imposed.  */
9631               if (align)
9632                 modulus = MIN (modulus, align);
9633
9634               return modulus;
9635             }
9636         }
9637     }
9638
9639     /* If we get here, we were unable to determine anything useful about the
9640        expression.  */
9641     return 1;
9642 }
9643
9644
9645 /* Fold a binary expression of code CODE and type TYPE with operands
9646    OP0 and OP1.  Return the folded expression if folding is
9647    successful.  Otherwise, return NULL_TREE.  */
9648
9649 tree
9650 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9651 {
9652   enum tree_code_class kind = TREE_CODE_CLASS (code);
9653   tree arg0, arg1, tem;
9654   tree t1 = NULL_TREE;
9655   bool strict_overflow_p;
9656
9657   gcc_assert (IS_EXPR_CODE_CLASS (kind)
9658               && TREE_CODE_LENGTH (code) == 2
9659               && op0 != NULL_TREE
9660               && op1 != NULL_TREE);
9661
9662   arg0 = op0;
9663   arg1 = op1;
9664
9665   /* Strip any conversions that don't change the mode.  This is
9666      safe for every expression, except for a comparison expression
9667      because its signedness is derived from its operands.  So, in
9668      the latter case, only strip conversions that don't change the
9669      signedness.  MIN_EXPR/MAX_EXPR also need signedness of arguments
9670      preserved.
9671
9672      Note that this is done as an internal manipulation within the
9673      constant folder, in order to find the simplest representation
9674      of the arguments so that their form can be studied.  In any
9675      cases, the appropriate type conversions should be put back in
9676      the tree that will get out of the constant folder.  */
9677
9678   if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9679     {
9680       STRIP_SIGN_NOPS (arg0);
9681       STRIP_SIGN_NOPS (arg1);
9682     }
9683   else
9684     {
9685       STRIP_NOPS (arg0);
9686       STRIP_NOPS (arg1);
9687     }
9688
9689   /* Note that TREE_CONSTANT isn't enough: static var addresses are
9690      constant but we can't do arithmetic on them.  */
9691   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9692       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9693       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9694       || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9695       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9696       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9697     {
9698       if (kind == tcc_binary)
9699         {
9700           /* Make sure type and arg0 have the same saturating flag.  */
9701           gcc_assert (TYPE_SATURATING (type)
9702                       == TYPE_SATURATING (TREE_TYPE (arg0)));
9703           tem = const_binop (code, arg0, arg1, 0);
9704         }
9705       else if (kind == tcc_comparison)
9706         tem = fold_relational_const (code, type, arg0, arg1);
9707       else
9708         tem = NULL_TREE;
9709
9710       if (tem != NULL_TREE)
9711         {
9712           if (TREE_TYPE (tem) != type)
9713             tem = fold_convert (type, tem);
9714           return tem;
9715         }
9716     }
9717
9718   /* If this is a commutative operation, and ARG0 is a constant, move it
9719      to ARG1 to reduce the number of tests below.  */
9720   if (commutative_tree_code (code)
9721       && tree_swap_operands_p (arg0, arg1, true))
9722     return fold_build2 (code, type, op1, op0);
9723
9724   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9725
9726      First check for cases where an arithmetic operation is applied to a
9727      compound, conditional, or comparison operation.  Push the arithmetic
9728      operation inside the compound or conditional to see if any folding
9729      can then be done.  Convert comparison to conditional for this purpose.
9730      The also optimizes non-constant cases that used to be done in
9731      expand_expr.
9732
9733      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9734      one of the operands is a comparison and the other is a comparison, a
9735      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
9736      code below would make the expression more complex.  Change it to a
9737      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
9738      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
9739
9740   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9741        || code == EQ_EXPR || code == NE_EXPR)
9742       && ((truth_value_p (TREE_CODE (arg0))
9743            && (truth_value_p (TREE_CODE (arg1))
9744                || (TREE_CODE (arg1) == BIT_AND_EXPR
9745                    && integer_onep (TREE_OPERAND (arg1, 1)))))
9746           || (truth_value_p (TREE_CODE (arg1))
9747               && (truth_value_p (TREE_CODE (arg0))
9748                   || (TREE_CODE (arg0) == BIT_AND_EXPR
9749                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
9750     {
9751       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9752                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9753                          : TRUTH_XOR_EXPR,
9754                          boolean_type_node,
9755                          fold_convert (boolean_type_node, arg0),
9756                          fold_convert (boolean_type_node, arg1));
9757
9758       if (code == EQ_EXPR)
9759         tem = invert_truthvalue (tem);
9760
9761       return fold_convert (type, tem);
9762     }
9763
9764   if (TREE_CODE_CLASS (code) == tcc_binary
9765       || TREE_CODE_CLASS (code) == tcc_comparison)
9766     {
9767       if (TREE_CODE (arg0) == COMPOUND_EXPR)
9768         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9769                        fold_build2 (code, type,
9770                                     fold_convert (TREE_TYPE (op0),
9771                                                   TREE_OPERAND (arg0, 1)),
9772                                     op1));
9773       if (TREE_CODE (arg1) == COMPOUND_EXPR
9774           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9775         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9776                        fold_build2 (code, type, op0,
9777                                     fold_convert (TREE_TYPE (op1),
9778                                                   TREE_OPERAND (arg1, 1))));
9779
9780       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9781         {
9782           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9783                                                      arg0, arg1, 
9784                                                      /*cond_first_p=*/1);
9785           if (tem != NULL_TREE)
9786             return tem;
9787         }
9788
9789       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9790         {
9791           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9792                                                      arg1, arg0, 
9793                                                      /*cond_first_p=*/0);
9794           if (tem != NULL_TREE)
9795             return tem;
9796         }
9797     }
9798
9799   switch (code)
9800     {
9801     case POINTER_PLUS_EXPR:
9802       /* 0 +p index -> (type)index */
9803       if (integer_zerop (arg0))
9804         return non_lvalue (fold_convert (type, arg1));
9805
9806       /* PTR +p 0 -> PTR */
9807       if (integer_zerop (arg1))
9808         return non_lvalue (fold_convert (type, arg0));
9809
9810       /* INT +p INT -> (PTR)(INT + INT).  Stripping types allows for this. */
9811       if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9812            && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9813         return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9814                                                 fold_convert (sizetype, arg1),
9815                                                 fold_convert (sizetype, arg0)));
9816
9817       /* index +p PTR -> PTR +p index */
9818       if (POINTER_TYPE_P (TREE_TYPE (arg1))
9819           && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9820         return fold_build2 (POINTER_PLUS_EXPR, type,
9821                             fold_convert (type, arg1),
9822                             fold_convert (sizetype, arg0));
9823
9824       /* (PTR +p B) +p A -> PTR +p (B + A) */
9825       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9826         {
9827           tree inner;
9828           tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9829           tree arg00 = TREE_OPERAND (arg0, 0);
9830           inner = fold_build2 (PLUS_EXPR, sizetype,
9831                                arg01, fold_convert (sizetype, arg1));
9832           return fold_convert (type,
9833                                fold_build2 (POINTER_PLUS_EXPR,
9834                                             TREE_TYPE (arg00), arg00, inner));
9835         }
9836
9837       /* PTR_CST +p CST -> CST1 */
9838       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9839         return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9840
9841      /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9842         of the array.  Loop optimizer sometimes produce this type of
9843         expressions.  */
9844       if (TREE_CODE (arg0) == ADDR_EXPR)
9845         {
9846           tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9847           if (tem)
9848             return fold_convert (type, tem);
9849         }
9850
9851       return NULL_TREE;
9852
9853     case PLUS_EXPR:
9854       /* A + (-B) -> A - B */
9855       if (TREE_CODE (arg1) == NEGATE_EXPR)
9856         return fold_build2 (MINUS_EXPR, type,
9857                             fold_convert (type, arg0),
9858                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9859       /* (-A) + B -> B - A */
9860       if (TREE_CODE (arg0) == NEGATE_EXPR
9861           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9862         return fold_build2 (MINUS_EXPR, type,
9863                             fold_convert (type, arg1),
9864                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9865
9866       if (INTEGRAL_TYPE_P (type))
9867         {
9868           /* Convert ~A + 1 to -A.  */
9869           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9870               && integer_onep (arg1))
9871             return fold_build1 (NEGATE_EXPR, type,
9872                                 fold_convert (type, TREE_OPERAND (arg0, 0)));
9873
9874           /* ~X + X is -1.  */
9875           if (TREE_CODE (arg0) == BIT_NOT_EXPR
9876               && !TYPE_OVERFLOW_TRAPS (type))
9877             {
9878               tree tem = TREE_OPERAND (arg0, 0);
9879
9880               STRIP_NOPS (tem);
9881               if (operand_equal_p (tem, arg1, 0))
9882                 {
9883                   t1 = build_int_cst_type (type, -1);
9884                   return omit_one_operand (type, t1, arg1);
9885                 }
9886             }
9887
9888           /* X + ~X is -1.  */
9889           if (TREE_CODE (arg1) == BIT_NOT_EXPR
9890               && !TYPE_OVERFLOW_TRAPS (type))
9891             {
9892               tree tem = TREE_OPERAND (arg1, 0);
9893
9894               STRIP_NOPS (tem);
9895               if (operand_equal_p (arg0, tem, 0))
9896                 {
9897                   t1 = build_int_cst_type (type, -1);
9898                   return omit_one_operand (type, t1, arg0);
9899                 }
9900             }
9901
9902           /* X + (X / CST) * -CST is X % CST.  */
9903           if (TREE_CODE (arg1) == MULT_EXPR
9904               && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9905               && operand_equal_p (arg0,
9906                                   TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9907             {
9908               tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9909               tree cst1 = TREE_OPERAND (arg1, 1);
9910               tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9911               if (sum && integer_zerop (sum))
9912                 return fold_convert (type,
9913                                      fold_build2 (TRUNC_MOD_EXPR,
9914                                                   TREE_TYPE (arg0), arg0, cst0));
9915             }
9916         }
9917
9918       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9919          same or one.  Make sure type is not saturating.
9920          fold_plusminus_mult_expr will re-associate.  */
9921       if ((TREE_CODE (arg0) == MULT_EXPR
9922            || TREE_CODE (arg1) == MULT_EXPR)
9923           && !TYPE_SATURATING (type)
9924           && (!FLOAT_TYPE_P (type) || flag_associative_math))
9925         {
9926           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9927           if (tem)
9928             return tem;
9929         }
9930
9931       if (! FLOAT_TYPE_P (type))
9932         {
9933           if (integer_zerop (arg1))
9934             return non_lvalue (fold_convert (type, arg0));
9935
9936           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9937              with a constant, and the two constants have no bits in common,
9938              we should treat this as a BIT_IOR_EXPR since this may produce more
9939              simplifications.  */
9940           if (TREE_CODE (arg0) == BIT_AND_EXPR
9941               && TREE_CODE (arg1) == BIT_AND_EXPR
9942               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9943               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9944               && integer_zerop (const_binop (BIT_AND_EXPR,
9945                                              TREE_OPERAND (arg0, 1),
9946                                              TREE_OPERAND (arg1, 1), 0)))
9947             {
9948               code = BIT_IOR_EXPR;
9949               goto bit_ior;
9950             }
9951
9952           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9953              (plus (plus (mult) (mult)) (foo)) so that we can
9954              take advantage of the factoring cases below.  */
9955           if (((TREE_CODE (arg0) == PLUS_EXPR
9956                 || TREE_CODE (arg0) == MINUS_EXPR)
9957                && TREE_CODE (arg1) == MULT_EXPR)
9958               || ((TREE_CODE (arg1) == PLUS_EXPR
9959                    || TREE_CODE (arg1) == MINUS_EXPR)
9960                   && TREE_CODE (arg0) == MULT_EXPR))
9961             {
9962               tree parg0, parg1, parg, marg;
9963               enum tree_code pcode;
9964
9965               if (TREE_CODE (arg1) == MULT_EXPR)
9966                 parg = arg0, marg = arg1;
9967               else
9968                 parg = arg1, marg = arg0;
9969               pcode = TREE_CODE (parg);
9970               parg0 = TREE_OPERAND (parg, 0);
9971               parg1 = TREE_OPERAND (parg, 1);
9972               STRIP_NOPS (parg0);
9973               STRIP_NOPS (parg1);
9974
9975               if (TREE_CODE (parg0) == MULT_EXPR
9976                   && TREE_CODE (parg1) != MULT_EXPR)
9977                 return fold_build2 (pcode, type,
9978                                     fold_build2 (PLUS_EXPR, type,
9979                                                  fold_convert (type, parg0),
9980                                                  fold_convert (type, marg)),
9981                                     fold_convert (type, parg1));
9982               if (TREE_CODE (parg0) != MULT_EXPR
9983                   && TREE_CODE (parg1) == MULT_EXPR)
9984                 return fold_build2 (PLUS_EXPR, type,
9985                                     fold_convert (type, parg0),
9986                                     fold_build2 (pcode, type,
9987                                                  fold_convert (type, marg),
9988                                                  fold_convert (type,
9989                                                                parg1)));
9990             }
9991         }
9992       else
9993         {
9994           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
9995           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9996             return non_lvalue (fold_convert (type, arg0));
9997
9998           /* Likewise if the operands are reversed.  */
9999           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10000             return non_lvalue (fold_convert (type, arg1));
10001
10002           /* Convert X + -C into X - C.  */
10003           if (TREE_CODE (arg1) == REAL_CST
10004               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10005             {
10006               tem = fold_negate_const (arg1, type);
10007               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10008                 return fold_build2 (MINUS_EXPR, type,
10009                                     fold_convert (type, arg0),
10010                                     fold_convert (type, tem));
10011             }
10012
10013           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10014              to __complex__ ( x, y ).  This is not the same for SNaNs or
10015              if signed zeros are involved.  */
10016           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10017               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10018               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10019             {
10020               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10021               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10022               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10023               bool arg0rz = false, arg0iz = false;
10024               if ((arg0r && (arg0rz = real_zerop (arg0r)))
10025                   || (arg0i && (arg0iz = real_zerop (arg0i))))
10026                 {
10027                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10028                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10029                   if (arg0rz && arg1i && real_zerop (arg1i))
10030                     {
10031                       tree rp = arg1r ? arg1r
10032                                   : build1 (REALPART_EXPR, rtype, arg1);
10033                       tree ip = arg0i ? arg0i
10034                                   : build1 (IMAGPART_EXPR, rtype, arg0);
10035                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10036                     }
10037                   else if (arg0iz && arg1r && real_zerop (arg1r))
10038                     {
10039                       tree rp = arg0r ? arg0r
10040                                   : build1 (REALPART_EXPR, rtype, arg0);
10041                       tree ip = arg1i ? arg1i
10042                                   : build1 (IMAGPART_EXPR, rtype, arg1);
10043                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10044                     }
10045                 }
10046             }
10047
10048           if (flag_unsafe_math_optimizations
10049               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10050               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10051               && (tem = distribute_real_division (code, type, arg0, arg1)))
10052             return tem;
10053
10054           /* Convert x+x into x*2.0.  */
10055           if (operand_equal_p (arg0, arg1, 0)
10056               && SCALAR_FLOAT_TYPE_P (type))
10057             return fold_build2 (MULT_EXPR, type, arg0,
10058                                 build_real (type, dconst2));
10059
10060           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  
10061              We associate floats only if the user has specified
10062              -fassociative-math.  */
10063           if (flag_associative_math
10064               && TREE_CODE (arg1) == PLUS_EXPR
10065               && TREE_CODE (arg0) != MULT_EXPR)
10066             {
10067               tree tree10 = TREE_OPERAND (arg1, 0);
10068               tree tree11 = TREE_OPERAND (arg1, 1);
10069               if (TREE_CODE (tree11) == MULT_EXPR
10070                   && TREE_CODE (tree10) == MULT_EXPR)
10071                 {
10072                   tree tree0;
10073                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10074                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10075                 }
10076             }
10077           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  
10078              We associate floats only if the user has specified
10079              -fassociative-math.  */
10080           if (flag_associative_math
10081               && TREE_CODE (arg0) == PLUS_EXPR
10082               && TREE_CODE (arg1) != MULT_EXPR)
10083             {
10084               tree tree00 = TREE_OPERAND (arg0, 0);
10085               tree tree01 = TREE_OPERAND (arg0, 1);
10086               if (TREE_CODE (tree01) == MULT_EXPR
10087                   && TREE_CODE (tree00) == MULT_EXPR)
10088                 {
10089                   tree tree0;
10090                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10091                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10092                 }
10093             }
10094         }
10095
10096      bit_rotate:
10097       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10098          is a rotate of A by C1 bits.  */
10099       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10100          is a rotate of A by B bits.  */
10101       {
10102         enum tree_code code0, code1;
10103         tree rtype;
10104         code0 = TREE_CODE (arg0);
10105         code1 = TREE_CODE (arg1);
10106         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10107              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10108             && operand_equal_p (TREE_OPERAND (arg0, 0),
10109                                 TREE_OPERAND (arg1, 0), 0)
10110             && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10111                 TYPE_UNSIGNED (rtype))
10112             /* Only create rotates in complete modes.  Other cases are not
10113                expanded properly.  */
10114             && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10115           {
10116             tree tree01, tree11;
10117             enum tree_code code01, code11;
10118
10119             tree01 = TREE_OPERAND (arg0, 1);
10120             tree11 = TREE_OPERAND (arg1, 1);
10121             STRIP_NOPS (tree01);
10122             STRIP_NOPS (tree11);
10123             code01 = TREE_CODE (tree01);
10124             code11 = TREE_CODE (tree11);
10125             if (code01 == INTEGER_CST
10126                 && code11 == INTEGER_CST
10127                 && TREE_INT_CST_HIGH (tree01) == 0
10128                 && TREE_INT_CST_HIGH (tree11) == 0
10129                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10130                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10131               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10132                              code0 == LSHIFT_EXPR ? tree01 : tree11);
10133             else if (code11 == MINUS_EXPR)
10134               {
10135                 tree tree110, tree111;
10136                 tree110 = TREE_OPERAND (tree11, 0);
10137                 tree111 = TREE_OPERAND (tree11, 1);
10138                 STRIP_NOPS (tree110);
10139                 STRIP_NOPS (tree111);
10140                 if (TREE_CODE (tree110) == INTEGER_CST
10141                     && 0 == compare_tree_int (tree110,
10142                                               TYPE_PRECISION
10143                                               (TREE_TYPE (TREE_OPERAND
10144                                                           (arg0, 0))))
10145                     && operand_equal_p (tree01, tree111, 0))
10146                   return build2 ((code0 == LSHIFT_EXPR
10147                                   ? LROTATE_EXPR
10148                                   : RROTATE_EXPR),
10149                                  type, TREE_OPERAND (arg0, 0), tree01);
10150               }
10151             else if (code01 == MINUS_EXPR)
10152               {
10153                 tree tree010, tree011;
10154                 tree010 = TREE_OPERAND (tree01, 0);
10155                 tree011 = TREE_OPERAND (tree01, 1);
10156                 STRIP_NOPS (tree010);
10157                 STRIP_NOPS (tree011);
10158                 if (TREE_CODE (tree010) == INTEGER_CST
10159                     && 0 == compare_tree_int (tree010,
10160                                               TYPE_PRECISION
10161                                               (TREE_TYPE (TREE_OPERAND
10162                                                           (arg0, 0))))
10163                     && operand_equal_p (tree11, tree011, 0))
10164                   return build2 ((code0 != LSHIFT_EXPR
10165                                   ? LROTATE_EXPR
10166                                   : RROTATE_EXPR),
10167                                  type, TREE_OPERAND (arg0, 0), tree11);
10168               }
10169           }
10170       }
10171
10172     associate:
10173       /* In most languages, can't associate operations on floats through
10174          parentheses.  Rather than remember where the parentheses were, we
10175          don't associate floats at all, unless the user has specified
10176          -fassociative-math.
10177          And, we need to make sure type is not saturating.  */
10178
10179       if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10180           && !TYPE_SATURATING (type))
10181         {
10182           tree var0, con0, lit0, minus_lit0;
10183           tree var1, con1, lit1, minus_lit1;
10184           bool ok = true;
10185
10186           /* Split both trees into variables, constants, and literals.  Then
10187              associate each group together, the constants with literals,
10188              then the result with variables.  This increases the chances of
10189              literals being recombined later and of generating relocatable
10190              expressions for the sum of a constant and literal.  */
10191           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10192           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10193                              code == MINUS_EXPR);
10194
10195           /* With undefined overflow we can only associate constants
10196              with one variable.  */
10197           if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10198                || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10199               && var0 && var1)
10200             {
10201               tree tmp0 = var0;
10202               tree tmp1 = var1;
10203
10204               if (TREE_CODE (tmp0) == NEGATE_EXPR)
10205                 tmp0 = TREE_OPERAND (tmp0, 0);
10206               if (TREE_CODE (tmp1) == NEGATE_EXPR)
10207                 tmp1 = TREE_OPERAND (tmp1, 0);
10208               /* The only case we can still associate with two variables
10209                  is if they are the same, modulo negation.  */
10210               if (!operand_equal_p (tmp0, tmp1, 0))
10211                 ok = false;
10212             }
10213
10214           /* Only do something if we found more than two objects.  Otherwise,
10215              nothing has changed and we risk infinite recursion.  */
10216           if (ok
10217               && (2 < ((var0 != 0) + (var1 != 0)
10218                        + (con0 != 0) + (con1 != 0)
10219                        + (lit0 != 0) + (lit1 != 0)
10220                        + (minus_lit0 != 0) + (minus_lit1 != 0))))
10221             {
10222               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
10223               if (code == MINUS_EXPR)
10224                 code = PLUS_EXPR;
10225
10226               var0 = associate_trees (var0, var1, code, type);
10227               con0 = associate_trees (con0, con1, code, type);
10228               lit0 = associate_trees (lit0, lit1, code, type);
10229               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10230
10231               /* Preserve the MINUS_EXPR if the negative part of the literal is
10232                  greater than the positive part.  Otherwise, the multiplicative
10233                  folding code (i.e extract_muldiv) may be fooled in case
10234                  unsigned constants are subtracted, like in the following
10235                  example: ((X*2 + 4) - 8U)/2.  */
10236               if (minus_lit0 && lit0)
10237                 {
10238                   if (TREE_CODE (lit0) == INTEGER_CST
10239                       && TREE_CODE (minus_lit0) == INTEGER_CST
10240                       && tree_int_cst_lt (lit0, minus_lit0))
10241                     {
10242                       minus_lit0 = associate_trees (minus_lit0, lit0,
10243                                                     MINUS_EXPR, type);
10244                       lit0 = 0;
10245                     }
10246                   else
10247                     {
10248                       lit0 = associate_trees (lit0, minus_lit0,
10249                                               MINUS_EXPR, type);
10250                       minus_lit0 = 0;
10251                     }
10252                 }
10253               if (minus_lit0)
10254                 {
10255                   if (con0 == 0)
10256                     return fold_convert (type,
10257                                          associate_trees (var0, minus_lit0,
10258                                                           MINUS_EXPR, type));
10259                   else
10260                     {
10261                       con0 = associate_trees (con0, minus_lit0,
10262                                               MINUS_EXPR, type);
10263                       return fold_convert (type,
10264                                            associate_trees (var0, con0,
10265                                                             PLUS_EXPR, type));
10266                     }
10267                 }
10268
10269               con0 = associate_trees (con0, lit0, code, type);
10270               return fold_convert (type, associate_trees (var0, con0,
10271                                                           code, type));
10272             }
10273         }
10274
10275       return NULL_TREE;
10276
10277     case MINUS_EXPR:
10278       /* Pointer simplifications for subtraction, simple reassociations. */
10279       if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10280         {
10281           /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10282           if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10283               && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10284             {
10285               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10286               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10287               tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10288               tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10289               return fold_build2 (PLUS_EXPR, type,
10290                                   fold_build2 (MINUS_EXPR, type, arg00, arg10),
10291                                   fold_build2 (MINUS_EXPR, type, arg01, arg11));
10292             }
10293           /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10294           else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10295             {
10296               tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10297               tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10298               tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10299               if (tmp)
10300                 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10301             }
10302         }
10303       /* A - (-B) -> A + B */
10304       if (TREE_CODE (arg1) == NEGATE_EXPR)
10305         return fold_build2 (PLUS_EXPR, type, op0,
10306                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10307       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
10308       if (TREE_CODE (arg0) == NEGATE_EXPR
10309           && (FLOAT_TYPE_P (type)
10310               || INTEGRAL_TYPE_P (type))
10311           && negate_expr_p (arg1)
10312           && reorder_operands_p (arg0, arg1))
10313         return fold_build2 (MINUS_EXPR, type,
10314                             fold_convert (type, negate_expr (arg1)),
10315                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10316       /* Convert -A - 1 to ~A.  */
10317       if (INTEGRAL_TYPE_P (type)
10318           && TREE_CODE (arg0) == NEGATE_EXPR
10319           && integer_onep (arg1)
10320           && !TYPE_OVERFLOW_TRAPS (type))
10321         return fold_build1 (BIT_NOT_EXPR, type,
10322                             fold_convert (type, TREE_OPERAND (arg0, 0)));
10323
10324       /* Convert -1 - A to ~A.  */
10325       if (INTEGRAL_TYPE_P (type)
10326           && integer_all_onesp (arg0))
10327         return fold_build1 (BIT_NOT_EXPR, type, op1);
10328
10329
10330       /* X - (X / CST) * CST is X % CST.  */
10331       if (INTEGRAL_TYPE_P (type)
10332           && TREE_CODE (arg1) == MULT_EXPR
10333           && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10334           && operand_equal_p (arg0,
10335                               TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10336           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10337                               TREE_OPERAND (arg1, 1), 0))
10338         return fold_convert (type,
10339                              fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10340                                           arg0, TREE_OPERAND (arg1, 1)));
10341
10342       if (! FLOAT_TYPE_P (type))
10343         {
10344           if (integer_zerop (arg0))
10345             return negate_expr (fold_convert (type, arg1));
10346           if (integer_zerop (arg1))
10347             return non_lvalue (fold_convert (type, arg0));
10348
10349           /* Fold A - (A & B) into ~B & A.  */
10350           if (!TREE_SIDE_EFFECTS (arg0)
10351               && TREE_CODE (arg1) == BIT_AND_EXPR)
10352             {
10353               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10354                 {
10355                   tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10356                   return fold_build2 (BIT_AND_EXPR, type,
10357                                       fold_build1 (BIT_NOT_EXPR, type, arg10),
10358                                       fold_convert (type, arg0));
10359                 }
10360               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10361                 {
10362                   tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10363                   return fold_build2 (BIT_AND_EXPR, type,
10364                                       fold_build1 (BIT_NOT_EXPR, type, arg11),
10365                                       fold_convert (type, arg0));
10366                 }
10367             }
10368
10369           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10370              any power of 2 minus 1.  */
10371           if (TREE_CODE (arg0) == BIT_AND_EXPR
10372               && TREE_CODE (arg1) == BIT_AND_EXPR
10373               && operand_equal_p (TREE_OPERAND (arg0, 0),
10374                                   TREE_OPERAND (arg1, 0), 0))
10375             {
10376               tree mask0 = TREE_OPERAND (arg0, 1);
10377               tree mask1 = TREE_OPERAND (arg1, 1);
10378               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10379
10380               if (operand_equal_p (tem, mask1, 0))
10381                 {
10382                   tem = fold_build2 (BIT_XOR_EXPR, type,
10383                                      TREE_OPERAND (arg0, 0), mask1);
10384                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
10385                 }
10386             }
10387         }
10388
10389       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
10390       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10391         return non_lvalue (fold_convert (type, arg0));
10392
10393       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
10394          ARG0 is zero and X + ARG0 reduces to X, since that would mean
10395          (-ARG1 + ARG0) reduces to -ARG1.  */
10396       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10397         return negate_expr (fold_convert (type, arg1));
10398
10399       /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10400          __complex__ ( x, -y ).  This is not the same for SNaNs or if
10401          signed zeros are involved.  */
10402       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10403           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10404           && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10405         {
10406           tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10407           tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10408           tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10409           bool arg0rz = false, arg0iz = false;
10410           if ((arg0r && (arg0rz = real_zerop (arg0r)))
10411               || (arg0i && (arg0iz = real_zerop (arg0i))))
10412             {
10413               tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10414               tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10415               if (arg0rz && arg1i && real_zerop (arg1i))
10416                 {
10417                   tree rp = fold_build1 (NEGATE_EXPR, rtype,
10418                                          arg1r ? arg1r
10419                                          : build1 (REALPART_EXPR, rtype, arg1));
10420                   tree ip = arg0i ? arg0i
10421                     : build1 (IMAGPART_EXPR, rtype, arg0);
10422                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10423                 }
10424               else if (arg0iz && arg1r && real_zerop (arg1r))
10425                 {
10426                   tree rp = arg0r ? arg0r
10427                     : build1 (REALPART_EXPR, rtype, arg0);
10428                   tree ip = fold_build1 (NEGATE_EXPR, rtype,
10429                                          arg1i ? arg1i
10430                                          : build1 (IMAGPART_EXPR, rtype, arg1));
10431                   return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10432                 }
10433             }
10434         }
10435
10436       /* Fold &x - &x.  This can happen from &x.foo - &x.
10437          This is unsafe for certain floats even in non-IEEE formats.
10438          In IEEE, it is unsafe because it does wrong for NaNs.
10439          Also note that operand_equal_p is always false if an operand
10440          is volatile.  */
10441
10442       if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10443           && operand_equal_p (arg0, arg1, 0))
10444         return fold_convert (type, integer_zero_node);
10445
10446       /* A - B -> A + (-B) if B is easily negatable.  */
10447       if (negate_expr_p (arg1)
10448           && ((FLOAT_TYPE_P (type)
10449                /* Avoid this transformation if B is a positive REAL_CST.  */
10450                && (TREE_CODE (arg1) != REAL_CST
10451                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10452               || INTEGRAL_TYPE_P (type)))
10453         return fold_build2 (PLUS_EXPR, type,
10454                             fold_convert (type, arg0),
10455                             fold_convert (type, negate_expr (arg1)));
10456
10457       /* Try folding difference of addresses.  */
10458       {
10459         HOST_WIDE_INT diff;
10460
10461         if ((TREE_CODE (arg0) == ADDR_EXPR
10462              || TREE_CODE (arg1) == ADDR_EXPR)
10463             && ptr_difference_const (arg0, arg1, &diff))
10464           return build_int_cst_type (type, diff);
10465       }
10466
10467       /* Fold &a[i] - &a[j] to i-j.  */
10468       if (TREE_CODE (arg0) == ADDR_EXPR
10469           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10470           && TREE_CODE (arg1) == ADDR_EXPR
10471           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10472         {
10473           tree aref0 = TREE_OPERAND (arg0, 0);
10474           tree aref1 = TREE_OPERAND (arg1, 0);
10475           if (operand_equal_p (TREE_OPERAND (aref0, 0),
10476                                TREE_OPERAND (aref1, 0), 0))
10477             {
10478               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10479               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10480               tree esz = array_ref_element_size (aref0);
10481               tree diff = build2 (MINUS_EXPR, type, op0, op1);
10482               return fold_build2 (MULT_EXPR, type, diff,
10483                                   fold_convert (type, esz));
10484                                   
10485             }
10486         }
10487
10488       if (flag_unsafe_math_optimizations
10489           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10490           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10491           && (tem = distribute_real_division (code, type, arg0, arg1)))
10492         return tem;
10493
10494       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10495          same or one.  Make sure type is not saturating.
10496          fold_plusminus_mult_expr will re-associate.  */
10497       if ((TREE_CODE (arg0) == MULT_EXPR
10498            || TREE_CODE (arg1) == MULT_EXPR)
10499           && !TYPE_SATURATING (type)
10500           && (!FLOAT_TYPE_P (type) || flag_associative_math))
10501         {
10502           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10503           if (tem)
10504             return tem;
10505         }
10506
10507       goto associate;
10508
10509     case MULT_EXPR:
10510       /* (-A) * (-B) -> A * B  */
10511       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10512         return fold_build2 (MULT_EXPR, type,
10513                             fold_convert (type, TREE_OPERAND (arg0, 0)),
10514                             fold_convert (type, negate_expr (arg1)));
10515       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10516         return fold_build2 (MULT_EXPR, type,
10517                             fold_convert (type, negate_expr (arg0)),
10518                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10519
10520       if (! FLOAT_TYPE_P (type))
10521         {
10522           if (integer_zerop (arg1))
10523             return omit_one_operand (type, arg1, arg0);
10524           if (integer_onep (arg1))
10525             return non_lvalue (fold_convert (type, arg0));
10526           /* Transform x * -1 into -x.  Make sure to do the negation
10527              on the original operand with conversions not stripped
10528              because we can only strip non-sign-changing conversions.  */
10529           if (integer_all_onesp (arg1))
10530             return fold_convert (type, negate_expr (op0));
10531           /* Transform x * -C into -x * C if x is easily negatable.  */
10532           if (TREE_CODE (arg1) == INTEGER_CST
10533               && tree_int_cst_sgn (arg1) == -1
10534               && negate_expr_p (arg0)
10535               && (tem = negate_expr (arg1)) != arg1
10536               && !TREE_OVERFLOW (tem))
10537             return fold_build2 (MULT_EXPR, type,
10538                                 fold_convert (type, negate_expr (arg0)), tem);
10539
10540           /* (a * (1 << b)) is (a << b)  */
10541           if (TREE_CODE (arg1) == LSHIFT_EXPR
10542               && integer_onep (TREE_OPERAND (arg1, 0)))
10543             return fold_build2 (LSHIFT_EXPR, type, op0,
10544                                 TREE_OPERAND (arg1, 1));
10545           if (TREE_CODE (arg0) == LSHIFT_EXPR
10546               && integer_onep (TREE_OPERAND (arg0, 0)))
10547             return fold_build2 (LSHIFT_EXPR, type, op1,
10548                                 TREE_OPERAND (arg0, 1));
10549
10550           /* (A + A) * C -> A * 2 * C  */
10551           if (TREE_CODE (arg0) == PLUS_EXPR
10552               && TREE_CODE (arg1) == INTEGER_CST
10553               && operand_equal_p (TREE_OPERAND (arg0, 0),
10554                                   TREE_OPERAND (arg0, 1), 0))
10555             return fold_build2 (MULT_EXPR, type,
10556                                 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10557                                                   TREE_OPERAND (arg0, 1)),
10558                                 fold_build2 (MULT_EXPR, type,
10559                                              build_int_cst (type, 2) , arg1));
10560
10561           strict_overflow_p = false;
10562           if (TREE_CODE (arg1) == INTEGER_CST
10563               && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10564                                              &strict_overflow_p)))
10565             {
10566               if (strict_overflow_p)
10567                 fold_overflow_warning (("assuming signed overflow does not "
10568                                         "occur when simplifying "
10569                                         "multiplication"),
10570                                        WARN_STRICT_OVERFLOW_MISC);
10571               return fold_convert (type, tem);
10572             }
10573
10574           /* Optimize z * conj(z) for integer complex numbers.  */
10575           if (TREE_CODE (arg0) == CONJ_EXPR
10576               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10577             return fold_mult_zconjz (type, arg1);
10578           if (TREE_CODE (arg1) == CONJ_EXPR
10579               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10580             return fold_mult_zconjz (type, arg0);
10581         }
10582       else
10583         {
10584           /* Maybe fold x * 0 to 0.  The expressions aren't the same
10585              when x is NaN, since x * 0 is also NaN.  Nor are they the
10586              same in modes with signed zeros, since multiplying a
10587              negative value by 0 gives -0, not +0.  */
10588           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10589               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10590               && real_zerop (arg1))
10591             return omit_one_operand (type, arg1, arg0);
10592           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
10593           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10594               && real_onep (arg1))
10595             return non_lvalue (fold_convert (type, arg0));
10596
10597           /* Transform x * -1.0 into -x.  */
10598           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10599               && real_minus_onep (arg1))
10600             return fold_convert (type, negate_expr (arg0));
10601
10602           /* Convert (C1/X)*C2 into (C1*C2)/X.  This transformation may change
10603              the result for floating point types due to rounding so it is applied
10604              only if -fassociative-math was specify.  */
10605           if (flag_associative_math
10606               && TREE_CODE (arg0) == RDIV_EXPR
10607               && TREE_CODE (arg1) == REAL_CST
10608               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10609             {
10610               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10611                                       arg1, 0);
10612               if (tem)
10613                 return fold_build2 (RDIV_EXPR, type, tem,
10614                                     TREE_OPERAND (arg0, 1));
10615             }
10616
10617           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
10618           if (operand_equal_p (arg0, arg1, 0))
10619             {
10620               tree tem = fold_strip_sign_ops (arg0);
10621               if (tem != NULL_TREE)
10622                 {
10623                   tem = fold_convert (type, tem);
10624                   return fold_build2 (MULT_EXPR, type, tem, tem);
10625                 }
10626             }
10627
10628           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10629              This is not the same for NaNs or if signed zeros are
10630              involved.  */
10631           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10632               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10633               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10634               && TREE_CODE (arg1) == COMPLEX_CST
10635               && real_zerop (TREE_REALPART (arg1)))
10636             {
10637               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10638               if (real_onep (TREE_IMAGPART (arg1)))
10639                 return fold_build2 (COMPLEX_EXPR, type,
10640                                     negate_expr (fold_build1 (IMAGPART_EXPR,
10641                                                               rtype, arg0)),
10642                                     fold_build1 (REALPART_EXPR, rtype, arg0));
10643               else if (real_minus_onep (TREE_IMAGPART (arg1)))
10644                 return fold_build2 (COMPLEX_EXPR, type,
10645                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
10646                                     negate_expr (fold_build1 (REALPART_EXPR,
10647                                                               rtype, arg0)));
10648             }
10649
10650           /* Optimize z * conj(z) for floating point complex numbers.
10651              Guarded by flag_unsafe_math_optimizations as non-finite
10652              imaginary components don't produce scalar results.  */
10653           if (flag_unsafe_math_optimizations
10654               && TREE_CODE (arg0) == CONJ_EXPR
10655               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10656             return fold_mult_zconjz (type, arg1);
10657           if (flag_unsafe_math_optimizations
10658               && TREE_CODE (arg1) == CONJ_EXPR
10659               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10660             return fold_mult_zconjz (type, arg0);
10661
10662           if (flag_unsafe_math_optimizations)
10663             {
10664               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10665               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10666
10667               /* Optimizations of root(...)*root(...).  */
10668               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10669                 {
10670                   tree rootfn, arg;
10671                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10672                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10673
10674                   /* Optimize sqrt(x)*sqrt(x) as x.  */
10675                   if (BUILTIN_SQRT_P (fcode0)
10676                       && operand_equal_p (arg00, arg10, 0)
10677                       && ! HONOR_SNANS (TYPE_MODE (type)))
10678                     return arg00;
10679
10680                   /* Optimize root(x)*root(y) as root(x*y).  */
10681                   rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10682                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10683                   return build_call_expr (rootfn, 1, arg);
10684                 }
10685
10686               /* Optimize expN(x)*expN(y) as expN(x+y).  */
10687               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10688                 {
10689                   tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10690                   tree arg = fold_build2 (PLUS_EXPR, type,
10691                                           CALL_EXPR_ARG (arg0, 0),
10692                                           CALL_EXPR_ARG (arg1, 0));
10693                   return build_call_expr (expfn, 1, arg);
10694                 }
10695
10696               /* Optimizations of pow(...)*pow(...).  */
10697               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10698                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10699                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10700                 {
10701                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10702                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10703                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10704                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10705
10706                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
10707                   if (operand_equal_p (arg01, arg11, 0))
10708                     {
10709                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10710                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10711                       return build_call_expr (powfn, 2, arg, arg01);
10712                     }
10713
10714                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
10715                   if (operand_equal_p (arg00, arg10, 0))
10716                     {
10717                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10718                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10719                       return build_call_expr (powfn, 2, arg00, arg);
10720                     }
10721                 }
10722
10723               /* Optimize tan(x)*cos(x) as sin(x).  */
10724               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10725                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10726                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10727                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10728                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10729                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10730                   && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10731                                       CALL_EXPR_ARG (arg1, 0), 0))
10732                 {
10733                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10734
10735                   if (sinfn != NULL_TREE)
10736                     return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10737                 }
10738
10739               /* Optimize x*pow(x,c) as pow(x,c+1).  */
10740               if (fcode1 == BUILT_IN_POW
10741                   || fcode1 == BUILT_IN_POWF
10742                   || fcode1 == BUILT_IN_POWL)
10743                 {
10744                   tree arg10 = CALL_EXPR_ARG (arg1, 0);
10745                   tree arg11 = CALL_EXPR_ARG (arg1, 1);
10746                   if (TREE_CODE (arg11) == REAL_CST
10747                       && !TREE_OVERFLOW (arg11)
10748                       && operand_equal_p (arg0, arg10, 0))
10749                     {
10750                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10751                       REAL_VALUE_TYPE c;
10752                       tree arg;
10753
10754                       c = TREE_REAL_CST (arg11);
10755                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10756                       arg = build_real (type, c);
10757                       return build_call_expr (powfn, 2, arg0, arg);
10758                     }
10759                 }
10760
10761               /* Optimize pow(x,c)*x as pow(x,c+1).  */
10762               if (fcode0 == BUILT_IN_POW
10763                   || fcode0 == BUILT_IN_POWF
10764                   || fcode0 == BUILT_IN_POWL)
10765                 {
10766                   tree arg00 = CALL_EXPR_ARG (arg0, 0);
10767                   tree arg01 = CALL_EXPR_ARG (arg0, 1);
10768                   if (TREE_CODE (arg01) == REAL_CST
10769                       && !TREE_OVERFLOW (arg01)
10770                       && operand_equal_p (arg1, arg00, 0))
10771                     {
10772                       tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10773                       REAL_VALUE_TYPE c;
10774                       tree arg;
10775
10776                       c = TREE_REAL_CST (arg01);
10777                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10778                       arg = build_real (type, c);
10779                       return build_call_expr (powfn, 2, arg1, arg);
10780                     }
10781                 }
10782
10783               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
10784               if (optimize_function_for_speed_p (cfun)
10785                   && operand_equal_p (arg0, arg1, 0))
10786                 {
10787                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10788
10789                   if (powfn)
10790                     {
10791                       tree arg = build_real (type, dconst2);
10792                       return build_call_expr (powfn, 2, arg0, arg);
10793                     }
10794                 }
10795             }
10796         }
10797       goto associate;
10798
10799     case BIT_IOR_EXPR:
10800     bit_ior:
10801       if (integer_all_onesp (arg1))
10802         return omit_one_operand (type, arg1, arg0);
10803       if (integer_zerop (arg1))
10804         return non_lvalue (fold_convert (type, arg0));
10805       if (operand_equal_p (arg0, arg1, 0))
10806         return non_lvalue (fold_convert (type, arg0));
10807
10808       /* ~X | X is -1.  */
10809       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10810           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10811         {
10812           t1 = fold_convert (type, integer_zero_node);
10813           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10814           return omit_one_operand (type, t1, arg1);
10815         }
10816
10817       /* X | ~X is -1.  */
10818       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10819           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10820         {
10821           t1 = fold_convert (type, integer_zero_node);
10822           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10823           return omit_one_operand (type, t1, arg0);
10824         }
10825
10826       /* Canonicalize (X & C1) | C2.  */
10827       if (TREE_CODE (arg0) == BIT_AND_EXPR
10828           && TREE_CODE (arg1) == INTEGER_CST
10829           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10830         {
10831           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10832           int width = TYPE_PRECISION (type), w;
10833           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10834           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10835           hi2 = TREE_INT_CST_HIGH (arg1);
10836           lo2 = TREE_INT_CST_LOW (arg1);
10837
10838           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
10839           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10840             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10841
10842           if (width > HOST_BITS_PER_WIDE_INT)
10843             {
10844               mhi = (unsigned HOST_WIDE_INT) -1 
10845                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
10846               mlo = -1;
10847             }
10848           else
10849             {
10850               mhi = 0;
10851               mlo = (unsigned HOST_WIDE_INT) -1
10852                     >> (HOST_BITS_PER_WIDE_INT - width);
10853             }
10854
10855           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
10856           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10857             return fold_build2 (BIT_IOR_EXPR, type,
10858                                 TREE_OPERAND (arg0, 0), arg1);
10859
10860           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10861              unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10862              mode which allows further optimizations.  */
10863           hi1 &= mhi;
10864           lo1 &= mlo;
10865           hi2 &= mhi;
10866           lo2 &= mlo;
10867           hi3 = hi1 & ~hi2;
10868           lo3 = lo1 & ~lo2;
10869           for (w = BITS_PER_UNIT;
10870                w <= width && w <= HOST_BITS_PER_WIDE_INT;
10871                w <<= 1)
10872             {
10873               unsigned HOST_WIDE_INT mask
10874                 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10875               if (((lo1 | lo2) & mask) == mask
10876                   && (lo1 & ~mask) == 0 && hi1 == 0)
10877                 {
10878                   hi3 = 0;
10879                   lo3 = mask;
10880                   break;
10881                 }
10882             }
10883           if (hi3 != hi1 || lo3 != lo1)
10884             return fold_build2 (BIT_IOR_EXPR, type,
10885                                 fold_build2 (BIT_AND_EXPR, type,
10886                                              TREE_OPERAND (arg0, 0),
10887                                              build_int_cst_wide (type,
10888                                                                  lo3, hi3)),
10889                                 arg1);
10890         }
10891
10892       /* (X & Y) | Y is (X, Y).  */
10893       if (TREE_CODE (arg0) == BIT_AND_EXPR
10894           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10895         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10896       /* (X & Y) | X is (Y, X).  */
10897       if (TREE_CODE (arg0) == BIT_AND_EXPR
10898           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10899           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10900         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10901       /* X | (X & Y) is (Y, X).  */
10902       if (TREE_CODE (arg1) == BIT_AND_EXPR
10903           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10904           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10905         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10906       /* X | (Y & X) is (Y, X).  */
10907       if (TREE_CODE (arg1) == BIT_AND_EXPR
10908           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10909           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10910         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10911
10912       t1 = distribute_bit_expr (code, type, arg0, arg1);
10913       if (t1 != NULL_TREE)
10914         return t1;
10915
10916       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10917
10918          This results in more efficient code for machines without a NAND
10919          instruction.  Combine will canonicalize to the first form
10920          which will allow use of NAND instructions provided by the
10921          backend if they exist.  */
10922       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10923           && TREE_CODE (arg1) == BIT_NOT_EXPR)
10924         {
10925           return fold_build1 (BIT_NOT_EXPR, type,
10926                               build2 (BIT_AND_EXPR, type,
10927                                       fold_convert (type,
10928                                                     TREE_OPERAND (arg0, 0)),
10929                                       fold_convert (type,
10930                                                     TREE_OPERAND (arg1, 0))));
10931         }
10932
10933       /* See if this can be simplified into a rotate first.  If that
10934          is unsuccessful continue in the association code.  */
10935       goto bit_rotate;
10936
10937     case BIT_XOR_EXPR:
10938       if (integer_zerop (arg1))
10939         return non_lvalue (fold_convert (type, arg0));
10940       if (integer_all_onesp (arg1))
10941         return fold_build1 (BIT_NOT_EXPR, type, op0);
10942       if (operand_equal_p (arg0, arg1, 0))
10943         return omit_one_operand (type, integer_zero_node, arg0);
10944
10945       /* ~X ^ X is -1.  */
10946       if (TREE_CODE (arg0) == BIT_NOT_EXPR
10947           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10948         {
10949           t1 = fold_convert (type, integer_zero_node);
10950           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10951           return omit_one_operand (type, t1, arg1);
10952         }
10953
10954       /* X ^ ~X is -1.  */
10955       if (TREE_CODE (arg1) == BIT_NOT_EXPR
10956           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10957         {
10958           t1 = fold_convert (type, integer_zero_node);
10959           t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10960           return omit_one_operand (type, t1, arg0);
10961         }
10962
10963       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10964          with a constant, and the two constants have no bits in common,
10965          we should treat this as a BIT_IOR_EXPR since this may produce more
10966          simplifications.  */
10967       if (TREE_CODE (arg0) == BIT_AND_EXPR
10968           && TREE_CODE (arg1) == BIT_AND_EXPR
10969           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10970           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10971           && integer_zerop (const_binop (BIT_AND_EXPR,
10972                                          TREE_OPERAND (arg0, 1),
10973                                          TREE_OPERAND (arg1, 1), 0)))
10974         {
10975           code = BIT_IOR_EXPR;
10976           goto bit_ior;
10977         }
10978
10979       /* (X | Y) ^ X -> Y & ~ X*/
10980       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10981           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10982         {
10983           tree t2 = TREE_OPERAND (arg0, 1);
10984           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10985                             arg1);
10986           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10987                             fold_convert (type, t1));
10988           return t1;
10989         }
10990
10991       /* (Y | X) ^ X -> Y & ~ X*/
10992       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10993           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10994         {
10995           tree t2 = TREE_OPERAND (arg0, 0);
10996           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10997                             arg1);
10998           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10999                             fold_convert (type, t1));
11000           return t1;
11001         }
11002
11003       /* X ^ (X | Y) -> Y & ~ X*/
11004       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11005           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11006         {
11007           tree t2 = TREE_OPERAND (arg1, 1);
11008           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11009                             arg0);
11010           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11011                             fold_convert (type, t1));
11012           return t1;
11013         }
11014
11015       /* X ^ (Y | X) -> Y & ~ X*/
11016       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11017           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11018         {
11019           tree t2 = TREE_OPERAND (arg1, 0);
11020           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11021                             arg0);
11022           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11023                             fold_convert (type, t1));
11024           return t1;
11025         }
11026         
11027       /* Convert ~X ^ ~Y to X ^ Y.  */
11028       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11029           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11030         return fold_build2 (code, type,
11031                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11032                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11033
11034       /* Convert ~X ^ C to X ^ ~C.  */
11035       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11036           && TREE_CODE (arg1) == INTEGER_CST)
11037         return fold_build2 (code, type,
11038                             fold_convert (type, TREE_OPERAND (arg0, 0)),
11039                             fold_build1 (BIT_NOT_EXPR, type, arg1));
11040
11041       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
11042       if (TREE_CODE (arg0) == BIT_AND_EXPR
11043           && integer_onep (TREE_OPERAND (arg0, 1))
11044           && integer_onep (arg1))
11045         return fold_build2 (EQ_EXPR, type, arg0,
11046                             build_int_cst (TREE_TYPE (arg0), 0));
11047
11048       /* Fold (X & Y) ^ Y as ~X & Y.  */
11049       if (TREE_CODE (arg0) == BIT_AND_EXPR
11050           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11051         {
11052           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11053           return fold_build2 (BIT_AND_EXPR, type, 
11054                               fold_build1 (BIT_NOT_EXPR, type, tem),
11055                               fold_convert (type, arg1));
11056         }
11057       /* Fold (X & Y) ^ X as ~Y & X.  */
11058       if (TREE_CODE (arg0) == BIT_AND_EXPR
11059           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11060           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11061         {
11062           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11063           return fold_build2 (BIT_AND_EXPR, type,
11064                               fold_build1 (BIT_NOT_EXPR, type, tem),
11065                               fold_convert (type, arg1));
11066         }
11067       /* Fold X ^ (X & Y) as X & ~Y.  */
11068       if (TREE_CODE (arg1) == BIT_AND_EXPR
11069           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11070         {
11071           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11072           return fold_build2 (BIT_AND_EXPR, type,
11073                               fold_convert (type, arg0),
11074                               fold_build1 (BIT_NOT_EXPR, type, tem));
11075         }
11076       /* Fold X ^ (Y & X) as ~Y & X.  */
11077       if (TREE_CODE (arg1) == BIT_AND_EXPR
11078           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11079           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11080         {
11081           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11082           return fold_build2 (BIT_AND_EXPR, type,
11083                               fold_build1 (BIT_NOT_EXPR, type, tem),
11084                               fold_convert (type, arg0));
11085         }
11086
11087       /* See if this can be simplified into a rotate first.  If that
11088          is unsuccessful continue in the association code.  */
11089       goto bit_rotate;
11090
11091     case BIT_AND_EXPR:
11092       if (integer_all_onesp (arg1))
11093         return non_lvalue (fold_convert (type, arg0));
11094       if (integer_zerop (arg1))
11095         return omit_one_operand (type, arg1, arg0);
11096       if (operand_equal_p (arg0, arg1, 0))
11097         return non_lvalue (fold_convert (type, arg0));
11098
11099       /* ~X & X is always zero.  */
11100       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11101           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11102         return omit_one_operand (type, integer_zero_node, arg1);
11103
11104       /* X & ~X is always zero.  */
11105       if (TREE_CODE (arg1) == BIT_NOT_EXPR
11106           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11107         return omit_one_operand (type, integer_zero_node, arg0);
11108
11109       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
11110       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11111           && TREE_CODE (arg1) == INTEGER_CST
11112           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11113         {
11114           tree tmp1 = fold_convert (type, arg1);
11115           tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11116           tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11117           tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11118           tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11119           return fold_convert (type,
11120                                fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11121         }
11122
11123       /* (X | Y) & Y is (X, Y).  */
11124       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11125           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11126         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11127       /* (X | Y) & X is (Y, X).  */
11128       if (TREE_CODE (arg0) == BIT_IOR_EXPR
11129           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11130           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11131         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11132       /* X & (X | Y) is (Y, X).  */
11133       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11134           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11135           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11136         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11137       /* X & (Y | X) is (Y, X).  */
11138       if (TREE_CODE (arg1) == BIT_IOR_EXPR
11139           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11140           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11141         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11142
11143       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
11144       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11145           && integer_onep (TREE_OPERAND (arg0, 1))
11146           && integer_onep (arg1))
11147         {
11148           tem = TREE_OPERAND (arg0, 0);
11149           return fold_build2 (EQ_EXPR, type,
11150                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11151                                            build_int_cst (TREE_TYPE (tem), 1)),
11152                               build_int_cst (TREE_TYPE (tem), 0));
11153         }
11154       /* Fold ~X & 1 as (X & 1) == 0.  */
11155       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11156           && integer_onep (arg1))
11157         {
11158           tem = TREE_OPERAND (arg0, 0);
11159           return fold_build2 (EQ_EXPR, type,
11160                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11161                                            build_int_cst (TREE_TYPE (tem), 1)),
11162                               build_int_cst (TREE_TYPE (tem), 0));
11163         }
11164
11165       /* Fold (X ^ Y) & Y as ~X & Y.  */
11166       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11167           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11168         {
11169           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11170           return fold_build2 (BIT_AND_EXPR, type, 
11171                               fold_build1 (BIT_NOT_EXPR, type, tem),
11172                               fold_convert (type, arg1));
11173         }
11174       /* Fold (X ^ Y) & X as ~Y & X.  */
11175       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11176           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11177           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11178         {
11179           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11180           return fold_build2 (BIT_AND_EXPR, type,
11181                               fold_build1 (BIT_NOT_EXPR, type, tem),
11182                               fold_convert (type, arg1));
11183         }
11184       /* Fold X & (X ^ Y) as X & ~Y.  */
11185       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11186           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11187         {
11188           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11189           return fold_build2 (BIT_AND_EXPR, type,
11190                               fold_convert (type, arg0),
11191                               fold_build1 (BIT_NOT_EXPR, type, tem));
11192         }
11193       /* Fold X & (Y ^ X) as ~Y & X.  */
11194       if (TREE_CODE (arg1) == BIT_XOR_EXPR
11195           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11196           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11197         {
11198           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11199           return fold_build2 (BIT_AND_EXPR, type,
11200                               fold_build1 (BIT_NOT_EXPR, type, tem),
11201                               fold_convert (type, arg0));
11202         }
11203
11204       t1 = distribute_bit_expr (code, type, arg0, arg1);
11205       if (t1 != NULL_TREE)
11206         return t1;
11207       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
11208       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11209           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11210         {
11211           unsigned int prec
11212             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11213
11214           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11215               && (~TREE_INT_CST_LOW (arg1)
11216                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11217             return fold_convert (type, TREE_OPERAND (arg0, 0));
11218         }
11219
11220       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11221
11222          This results in more efficient code for machines without a NOR
11223          instruction.  Combine will canonicalize to the first form
11224          which will allow use of NOR instructions provided by the
11225          backend if they exist.  */
11226       if (TREE_CODE (arg0) == BIT_NOT_EXPR
11227           && TREE_CODE (arg1) == BIT_NOT_EXPR)
11228         {
11229           return fold_build1 (BIT_NOT_EXPR, type,
11230                               build2 (BIT_IOR_EXPR, type,
11231                                       fold_convert (type,
11232                                                     TREE_OPERAND (arg0, 0)),
11233                                       fold_convert (type,
11234                                                     TREE_OPERAND (arg1, 0))));
11235         }
11236
11237       /* If arg0 is derived from the address of an object or function, we may
11238          be able to fold this expression using the object or function's
11239          alignment.  */
11240       if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11241         {
11242           unsigned HOST_WIDE_INT modulus, residue;
11243           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11244
11245           modulus = get_pointer_modulus_and_residue (arg0, &residue,
11246                                                      integer_onep (arg1));
11247
11248           /* This works because modulus is a power of 2.  If this weren't the
11249              case, we'd have to replace it by its greatest power-of-2
11250              divisor: modulus & -modulus.  */
11251           if (low < modulus)
11252             return build_int_cst (type, residue & low);
11253         }
11254
11255       /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11256               (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11257          if the new mask might be further optimized.  */
11258       if ((TREE_CODE (arg0) == LSHIFT_EXPR
11259            || TREE_CODE (arg0) == RSHIFT_EXPR)
11260           && host_integerp (TREE_OPERAND (arg0, 1), 1)
11261           && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11262           && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11263              < TYPE_PRECISION (TREE_TYPE (arg0))
11264           && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11265           && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11266         {
11267           unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11268           unsigned HOST_WIDE_INT mask
11269             = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11270           unsigned HOST_WIDE_INT newmask, zerobits = 0;
11271           tree shift_type = TREE_TYPE (arg0);
11272
11273           if (TREE_CODE (arg0) == LSHIFT_EXPR)
11274             zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11275           else if (TREE_CODE (arg0) == RSHIFT_EXPR
11276                    && TYPE_PRECISION (TREE_TYPE (arg0))
11277                       == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11278             {
11279               unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11280               tree arg00 = TREE_OPERAND (arg0, 0);
11281               /* See if more bits can be proven as zero because of
11282                  zero extension.  */
11283               if (TREE_CODE (arg00) == NOP_EXPR
11284                   && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11285                 {
11286                   tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11287                   if (TYPE_PRECISION (inner_type)
11288                       == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11289                       && TYPE_PRECISION (inner_type) < prec)
11290                     {
11291                       prec = TYPE_PRECISION (inner_type);
11292                       /* See if we can shorten the right shift.  */
11293                       if (shiftc < prec)
11294                         shift_type = inner_type;
11295                     }
11296                 }
11297               zerobits = ~(unsigned HOST_WIDE_INT) 0;
11298               zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11299               zerobits <<= prec - shiftc;
11300               /* For arithmetic shift if sign bit could be set, zerobits
11301                  can contain actually sign bits, so no transformation is
11302                  possible, unless MASK masks them all away.  In that
11303                  case the shift needs to be converted into logical shift.  */
11304               if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11305                   && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11306                 {
11307                   if ((mask & zerobits) == 0)
11308                     shift_type = unsigned_type_for (TREE_TYPE (arg0));
11309                   else
11310                     zerobits = 0;
11311                 }
11312             }
11313
11314           /* ((X << 16) & 0xff00) is (X, 0).  */
11315           if ((mask & zerobits) == mask)
11316             return omit_one_operand (type, build_int_cst (type, 0), arg0);
11317
11318           newmask = mask | zerobits;
11319           if (newmask != mask && (newmask & (newmask + 1)) == 0)
11320             {
11321               unsigned int prec;
11322
11323               /* Only do the transformation if NEWMASK is some integer
11324                  mode's mask.  */
11325               for (prec = BITS_PER_UNIT;
11326                    prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11327                 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11328                   break;
11329               if (prec < HOST_BITS_PER_WIDE_INT
11330                   || newmask == ~(unsigned HOST_WIDE_INT) 0)
11331                 {
11332                   if (shift_type != TREE_TYPE (arg0))
11333                     {
11334                       tem = fold_build2 (TREE_CODE (arg0), shift_type,
11335                                          fold_convert (shift_type,
11336                                                        TREE_OPERAND (arg0, 0)),
11337                                          TREE_OPERAND (arg0, 1));
11338                       tem = fold_convert (type, tem);
11339                     }
11340                   else
11341                     tem = op0;
11342                   return fold_build2 (BIT_AND_EXPR, type, tem,
11343                                       build_int_cst_type (TREE_TYPE (op1),
11344                                                           newmask));
11345                 }
11346             }
11347         }
11348
11349       goto associate;
11350
11351     case RDIV_EXPR:
11352       /* Don't touch a floating-point divide by zero unless the mode
11353          of the constant can represent infinity.  */
11354       if (TREE_CODE (arg1) == REAL_CST
11355           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11356           && real_zerop (arg1))
11357         return NULL_TREE;
11358
11359       /* Optimize A / A to 1.0 if we don't care about
11360          NaNs or Infinities.  Skip the transformation
11361          for non-real operands.  */
11362       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11363           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11364           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11365           && operand_equal_p (arg0, arg1, 0))
11366         {
11367           tree r = build_real (TREE_TYPE (arg0), dconst1);
11368
11369           return omit_two_operands (type, r, arg0, arg1);
11370         }
11371
11372       /* The complex version of the above A / A optimization.  */
11373       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11374           && operand_equal_p (arg0, arg1, 0))
11375         {
11376           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11377           if (! HONOR_NANS (TYPE_MODE (elem_type))
11378               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11379             {
11380               tree r = build_real (elem_type, dconst1);
11381               /* omit_two_operands will call fold_convert for us.  */
11382               return omit_two_operands (type, r, arg0, arg1);
11383             }
11384         }
11385
11386       /* (-A) / (-B) -> A / B  */
11387       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11388         return fold_build2 (RDIV_EXPR, type,
11389                             TREE_OPERAND (arg0, 0),
11390                             negate_expr (arg1));
11391       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11392         return fold_build2 (RDIV_EXPR, type,
11393                             negate_expr (arg0),
11394                             TREE_OPERAND (arg1, 0));
11395
11396       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
11397       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11398           && real_onep (arg1))
11399         return non_lvalue (fold_convert (type, arg0));
11400
11401       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
11402       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11403           && real_minus_onep (arg1))
11404         return non_lvalue (fold_convert (type, negate_expr (arg0)));
11405
11406       /* If ARG1 is a constant, we can convert this to a multiply by the
11407          reciprocal.  This does not have the same rounding properties,
11408          so only do this if -freciprocal-math.  We can actually
11409          always safely do it if ARG1 is a power of two, but it's hard to
11410          tell if it is or not in a portable manner.  */
11411       if (TREE_CODE (arg1) == REAL_CST)
11412         {
11413           if (flag_reciprocal_math
11414               && 0 != (tem = const_binop (code, build_real (type, dconst1),
11415                                           arg1, 0)))
11416             return fold_build2 (MULT_EXPR, type, arg0, tem);
11417           /* Find the reciprocal if optimizing and the result is exact.  */
11418           if (optimize)
11419             {
11420               REAL_VALUE_TYPE r;
11421               r = TREE_REAL_CST (arg1);
11422               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11423                 {
11424                   tem = build_real (type, r);
11425                   return fold_build2 (MULT_EXPR, type,
11426                                       fold_convert (type, arg0), tem);
11427                 }
11428             }
11429         }
11430       /* Convert A/B/C to A/(B*C).  */ 
11431       if (flag_reciprocal_math
11432           && TREE_CODE (arg0) == RDIV_EXPR)
11433         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11434                             fold_build2 (MULT_EXPR, type,
11435                                          TREE_OPERAND (arg0, 1), arg1));
11436
11437       /* Convert A/(B/C) to (A/B)*C.  */
11438       if (flag_reciprocal_math
11439           && TREE_CODE (arg1) == RDIV_EXPR)
11440         return fold_build2 (MULT_EXPR, type,
11441                             fold_build2 (RDIV_EXPR, type, arg0,
11442                                          TREE_OPERAND (arg1, 0)),
11443                             TREE_OPERAND (arg1, 1));
11444
11445       /* Convert C1/(X*C2) into (C1/C2)/X.  */
11446       if (flag_reciprocal_math
11447           && TREE_CODE (arg1) == MULT_EXPR
11448           && TREE_CODE (arg0) == REAL_CST
11449           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11450         {
11451           tree tem = const_binop (RDIV_EXPR, arg0,
11452                                   TREE_OPERAND (arg1, 1), 0);
11453           if (tem)
11454             return fold_build2 (RDIV_EXPR, type, tem,
11455                                 TREE_OPERAND (arg1, 0));
11456         }
11457
11458       if (flag_unsafe_math_optimizations)
11459         {
11460           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11461           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11462
11463           /* Optimize sin(x)/cos(x) as tan(x).  */
11464           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11465                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11466                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11467               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11468                                   CALL_EXPR_ARG (arg1, 0), 0))
11469             {
11470               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11471
11472               if (tanfn != NULL_TREE)
11473                 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11474             }
11475
11476           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
11477           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11478                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11479                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11480               && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11481                                   CALL_EXPR_ARG (arg1, 0), 0))
11482             {
11483               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11484
11485               if (tanfn != NULL_TREE)
11486                 {
11487                   tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11488                   return fold_build2 (RDIV_EXPR, type,
11489                                       build_real (type, dconst1), tmp);
11490                 }
11491             }
11492
11493           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11494              NaNs or Infinities.  */
11495           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11496                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11497                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11498             {
11499               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11500               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11501
11502               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11503                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11504                   && operand_equal_p (arg00, arg01, 0))
11505                 {
11506                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11507
11508                   if (cosfn != NULL_TREE)
11509                     return build_call_expr (cosfn, 1, arg00);
11510                 }
11511             }
11512
11513           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11514              NaNs or Infinities.  */
11515           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11516                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11517                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11518             {
11519               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11520               tree arg01 = CALL_EXPR_ARG (arg1, 0);
11521
11522               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11523                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11524                   && operand_equal_p (arg00, arg01, 0))
11525                 {
11526                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11527
11528                   if (cosfn != NULL_TREE)
11529                     {
11530                       tree tmp = build_call_expr (cosfn, 1, arg00);
11531                       return fold_build2 (RDIV_EXPR, type,
11532                                           build_real (type, dconst1),
11533                                           tmp);
11534                     }
11535                 }
11536             }
11537
11538           /* Optimize pow(x,c)/x as pow(x,c-1).  */
11539           if (fcode0 == BUILT_IN_POW
11540               || fcode0 == BUILT_IN_POWF
11541               || fcode0 == BUILT_IN_POWL)
11542             {
11543               tree arg00 = CALL_EXPR_ARG (arg0, 0);
11544               tree arg01 = CALL_EXPR_ARG (arg0, 1);
11545               if (TREE_CODE (arg01) == REAL_CST
11546                   && !TREE_OVERFLOW (arg01)
11547                   && operand_equal_p (arg1, arg00, 0))
11548                 {
11549                   tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11550                   REAL_VALUE_TYPE c;
11551                   tree arg;
11552
11553                   c = TREE_REAL_CST (arg01);
11554                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11555                   arg = build_real (type, c);
11556                   return build_call_expr (powfn, 2, arg1, arg);
11557                 }
11558             }
11559
11560           /* Optimize a/root(b/c) into a*root(c/b).  */
11561           if (BUILTIN_ROOT_P (fcode1))
11562             {
11563               tree rootarg = CALL_EXPR_ARG (arg1, 0);
11564
11565               if (TREE_CODE (rootarg) == RDIV_EXPR)
11566                 {
11567                   tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11568                   tree b = TREE_OPERAND (rootarg, 0);
11569                   tree c = TREE_OPERAND (rootarg, 1);
11570
11571                   tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11572
11573                   tmp = build_call_expr (rootfn, 1, tmp);
11574                   return fold_build2 (MULT_EXPR, type, arg0, tmp);
11575                 }
11576             }
11577
11578           /* Optimize x/expN(y) into x*expN(-y).  */
11579           if (BUILTIN_EXPONENT_P (fcode1))
11580             {
11581               tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11582               tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11583               arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11584               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11585             }
11586
11587           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
11588           if (fcode1 == BUILT_IN_POW
11589               || fcode1 == BUILT_IN_POWF
11590               || fcode1 == BUILT_IN_POWL)
11591             {
11592               tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11593               tree arg10 = CALL_EXPR_ARG (arg1, 0);
11594               tree arg11 = CALL_EXPR_ARG (arg1, 1);
11595               tree neg11 = fold_convert (type, negate_expr (arg11));
11596               arg1 = build_call_expr (powfn, 2, arg10, neg11);
11597               return fold_build2 (MULT_EXPR, type, arg0, arg1);
11598             }
11599         }
11600       return NULL_TREE;
11601
11602     case TRUNC_DIV_EXPR:
11603     case FLOOR_DIV_EXPR:
11604       /* Simplify A / (B << N) where A and B are positive and B is
11605          a power of 2, to A >> (N + log2(B)).  */
11606       strict_overflow_p = false;
11607       if (TREE_CODE (arg1) == LSHIFT_EXPR
11608           && (TYPE_UNSIGNED (type)
11609               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11610         {
11611           tree sval = TREE_OPERAND (arg1, 0);
11612           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11613             {
11614               tree sh_cnt = TREE_OPERAND (arg1, 1);
11615               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11616
11617               if (strict_overflow_p)
11618                 fold_overflow_warning (("assuming signed overflow does not "
11619                                         "occur when simplifying A / (B << N)"),
11620                                        WARN_STRICT_OVERFLOW_MISC);
11621
11622               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11623                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
11624               return fold_build2 (RSHIFT_EXPR, type,
11625                                   fold_convert (type, arg0), sh_cnt);
11626             }
11627         }
11628
11629       /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11630          TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
11631       if (INTEGRAL_TYPE_P (type)
11632           && TYPE_UNSIGNED (type)
11633           && code == FLOOR_DIV_EXPR)
11634         return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11635
11636       /* Fall thru */
11637
11638     case ROUND_DIV_EXPR:
11639     case CEIL_DIV_EXPR:
11640     case EXACT_DIV_EXPR:
11641       if (integer_onep (arg1))
11642         return non_lvalue (fold_convert (type, arg0));
11643       if (integer_zerop (arg1))
11644         return NULL_TREE;
11645       /* X / -1 is -X.  */
11646       if (!TYPE_UNSIGNED (type)
11647           && TREE_CODE (arg1) == INTEGER_CST
11648           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11649           && TREE_INT_CST_HIGH (arg1) == -1)
11650         return fold_convert (type, negate_expr (arg0));
11651
11652       /* Convert -A / -B to A / B when the type is signed and overflow is
11653          undefined.  */
11654       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11655           && TREE_CODE (arg0) == NEGATE_EXPR
11656           && negate_expr_p (arg1))
11657         {
11658           if (INTEGRAL_TYPE_P (type))
11659             fold_overflow_warning (("assuming signed overflow does not occur "
11660                                     "when distributing negation across "
11661                                     "division"),
11662                                    WARN_STRICT_OVERFLOW_MISC);
11663           return fold_build2 (code, type,
11664                               fold_convert (type, TREE_OPERAND (arg0, 0)),
11665                               fold_convert (type, negate_expr (arg1)));
11666         }
11667       if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11668           && TREE_CODE (arg1) == NEGATE_EXPR
11669           && negate_expr_p (arg0))
11670         {
11671           if (INTEGRAL_TYPE_P (type))
11672             fold_overflow_warning (("assuming signed overflow does not occur "
11673                                     "when distributing negation across "
11674                                     "division"),
11675                                    WARN_STRICT_OVERFLOW_MISC);
11676           return fold_build2 (code, type,
11677                               fold_convert (type, negate_expr (arg0)),
11678                               fold_convert (type, TREE_OPERAND (arg1, 0)));
11679         }
11680
11681       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11682          operation, EXACT_DIV_EXPR.
11683
11684          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11685          At one time others generated faster code, it's not clear if they do
11686          after the last round to changes to the DIV code in expmed.c.  */
11687       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11688           && multiple_of_p (type, arg0, arg1))
11689         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11690
11691       strict_overflow_p = false;
11692       if (TREE_CODE (arg1) == INTEGER_CST
11693           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11694                                          &strict_overflow_p)))
11695         {
11696           if (strict_overflow_p)
11697             fold_overflow_warning (("assuming signed overflow does not occur "
11698                                     "when simplifying division"),
11699                                    WARN_STRICT_OVERFLOW_MISC);
11700           return fold_convert (type, tem);
11701         }
11702
11703       return NULL_TREE;
11704
11705     case CEIL_MOD_EXPR:
11706     case FLOOR_MOD_EXPR:
11707     case ROUND_MOD_EXPR:
11708     case TRUNC_MOD_EXPR:
11709       /* X % 1 is always zero, but be sure to preserve any side
11710          effects in X.  */
11711       if (integer_onep (arg1))
11712         return omit_one_operand (type, integer_zero_node, arg0);
11713
11714       /* X % 0, return X % 0 unchanged so that we can get the
11715          proper warnings and errors.  */
11716       if (integer_zerop (arg1))
11717         return NULL_TREE;
11718
11719       /* 0 % X is always zero, but be sure to preserve any side
11720          effects in X.  Place this after checking for X == 0.  */
11721       if (integer_zerop (arg0))
11722         return omit_one_operand (type, integer_zero_node, arg1);
11723
11724       /* X % -1 is zero.  */
11725       if (!TYPE_UNSIGNED (type)
11726           && TREE_CODE (arg1) == INTEGER_CST
11727           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11728           && TREE_INT_CST_HIGH (arg1) == -1)
11729         return omit_one_operand (type, integer_zero_node, arg0);
11730
11731       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11732          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
11733       strict_overflow_p = false;
11734       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11735           && (TYPE_UNSIGNED (type)
11736               || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11737         {
11738           tree c = arg1;
11739           /* Also optimize A % (C << N)  where C is a power of 2,
11740              to A & ((C << N) - 1).  */
11741           if (TREE_CODE (arg1) == LSHIFT_EXPR)
11742             c = TREE_OPERAND (arg1, 0);
11743
11744           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11745             {
11746               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11747                                        build_int_cst (TREE_TYPE (arg1), 1));
11748               if (strict_overflow_p)
11749                 fold_overflow_warning (("assuming signed overflow does not "
11750                                         "occur when simplifying "
11751                                         "X % (power of two)"),
11752                                        WARN_STRICT_OVERFLOW_MISC);
11753               return fold_build2 (BIT_AND_EXPR, type,
11754                                   fold_convert (type, arg0),
11755                                   fold_convert (type, mask));
11756             }
11757         }
11758
11759       /* X % -C is the same as X % C.  */
11760       if (code == TRUNC_MOD_EXPR
11761           && !TYPE_UNSIGNED (type)
11762           && TREE_CODE (arg1) == INTEGER_CST
11763           && !TREE_OVERFLOW (arg1)
11764           && TREE_INT_CST_HIGH (arg1) < 0
11765           && !TYPE_OVERFLOW_TRAPS (type)
11766           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
11767           && !sign_bit_p (arg1, arg1))
11768         return fold_build2 (code, type, fold_convert (type, arg0),
11769                             fold_convert (type, negate_expr (arg1)));
11770
11771       /* X % -Y is the same as X % Y.  */
11772       if (code == TRUNC_MOD_EXPR
11773           && !TYPE_UNSIGNED (type)
11774           && TREE_CODE (arg1) == NEGATE_EXPR
11775           && !TYPE_OVERFLOW_TRAPS (type))
11776         return fold_build2 (code, type, fold_convert (type, arg0),
11777                             fold_convert (type, TREE_OPERAND (arg1, 0)));
11778
11779       if (TREE_CODE (arg1) == INTEGER_CST
11780           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11781                                          &strict_overflow_p)))
11782         {
11783           if (strict_overflow_p)
11784             fold_overflow_warning (("assuming signed overflow does not occur "
11785                                     "when simplifying modulus"),
11786                                    WARN_STRICT_OVERFLOW_MISC);
11787           return fold_convert (type, tem);
11788         }
11789
11790       return NULL_TREE;
11791
11792     case LROTATE_EXPR:
11793     case RROTATE_EXPR:
11794       if (integer_all_onesp (arg0))
11795         return omit_one_operand (type, arg0, arg1);
11796       goto shift;
11797
11798     case RSHIFT_EXPR:
11799       /* Optimize -1 >> x for arithmetic right shifts.  */
11800       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11801           && tree_expr_nonnegative_p (arg1))
11802         return omit_one_operand (type, arg0, arg1);
11803       /* ... fall through ...  */
11804
11805     case LSHIFT_EXPR:
11806     shift:
11807       if (integer_zerop (arg1))
11808         return non_lvalue (fold_convert (type, arg0));
11809       if (integer_zerop (arg0))
11810         return omit_one_operand (type, arg0, arg1);
11811
11812       /* Since negative shift count is not well-defined,
11813          don't try to compute it in the compiler.  */
11814       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11815         return NULL_TREE;
11816
11817       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
11818       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11819           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11820           && host_integerp (TREE_OPERAND (arg0, 1), false)
11821           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11822         {
11823           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11824                                + TREE_INT_CST_LOW (arg1));
11825
11826           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11827              being well defined.  */
11828           if (low >= TYPE_PRECISION (type))
11829             {
11830               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11831                 low = low % TYPE_PRECISION (type);
11832               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11833                 return build_int_cst (type, 0);
11834               else
11835                 low = TYPE_PRECISION (type) - 1;
11836             }
11837
11838           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11839                               build_int_cst (type, low));
11840         }
11841
11842       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11843          into x & ((unsigned)-1 >> c) for unsigned types.  */
11844       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11845            || (TYPE_UNSIGNED (type)
11846                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11847           && host_integerp (arg1, false)
11848           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11849           && host_integerp (TREE_OPERAND (arg0, 1), false)
11850           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11851         {
11852           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11853           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11854           tree lshift;
11855           tree arg00;
11856
11857           if (low0 == low1)
11858             {
11859               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11860
11861               lshift = build_int_cst (type, -1);
11862               lshift = int_const_binop (code, lshift, arg1, 0);
11863
11864               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11865             }
11866         }
11867
11868       /* Rewrite an LROTATE_EXPR by a constant into an
11869          RROTATE_EXPR by a new constant.  */
11870       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11871         {
11872           tree tem = build_int_cst (TREE_TYPE (arg1),
11873                                     TYPE_PRECISION (type));
11874           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11875           return fold_build2 (RROTATE_EXPR, type, op0, tem);
11876         }
11877
11878       /* If we have a rotate of a bit operation with the rotate count and
11879          the second operand of the bit operation both constant,
11880          permute the two operations.  */
11881       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11882           && (TREE_CODE (arg0) == BIT_AND_EXPR
11883               || TREE_CODE (arg0) == BIT_IOR_EXPR
11884               || TREE_CODE (arg0) == BIT_XOR_EXPR)
11885           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11886         return fold_build2 (TREE_CODE (arg0), type,
11887                             fold_build2 (code, type,
11888                                          TREE_OPERAND (arg0, 0), arg1),
11889                             fold_build2 (code, type,
11890                                          TREE_OPERAND (arg0, 1), arg1));
11891
11892       /* Two consecutive rotates adding up to the precision of the
11893          type can be ignored.  */
11894       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11895           && TREE_CODE (arg0) == RROTATE_EXPR
11896           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11897           && TREE_INT_CST_HIGH (arg1) == 0
11898           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11899           && ((TREE_INT_CST_LOW (arg1)
11900                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11901               == (unsigned int) TYPE_PRECISION (type)))
11902         return TREE_OPERAND (arg0, 0);
11903
11904       /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11905               (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11906          if the latter can be further optimized.  */
11907       if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11908           && TREE_CODE (arg0) == BIT_AND_EXPR
11909           && TREE_CODE (arg1) == INTEGER_CST
11910           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11911         {
11912           tree mask = fold_build2 (code, type,
11913                                    fold_convert (type, TREE_OPERAND (arg0, 1)),
11914                                    arg1);
11915           tree shift = fold_build2 (code, type,
11916                                     fold_convert (type, TREE_OPERAND (arg0, 0)),
11917                                     arg1);
11918           tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11919           if (tem)
11920             return tem;
11921         }
11922
11923       return NULL_TREE;
11924
11925     case MIN_EXPR:
11926       if (operand_equal_p (arg0, arg1, 0))
11927         return omit_one_operand (type, arg0, arg1);
11928       if (INTEGRAL_TYPE_P (type)
11929           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11930         return omit_one_operand (type, arg1, arg0);
11931       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11932       if (tem)
11933         return tem;
11934       goto associate;
11935
11936     case MAX_EXPR:
11937       if (operand_equal_p (arg0, arg1, 0))
11938         return omit_one_operand (type, arg0, arg1);
11939       if (INTEGRAL_TYPE_P (type)
11940           && TYPE_MAX_VALUE (type)
11941           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11942         return omit_one_operand (type, arg1, arg0);
11943       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11944       if (tem)
11945         return tem;
11946       goto associate;
11947
11948     case TRUTH_ANDIF_EXPR:
11949       /* Note that the operands of this must be ints
11950          and their values must be 0 or 1.
11951          ("true" is a fixed value perhaps depending on the language.)  */
11952       /* If first arg is constant zero, return it.  */
11953       if (integer_zerop (arg0))
11954         return fold_convert (type, arg0);
11955     case TRUTH_AND_EXPR:
11956       /* If either arg is constant true, drop it.  */
11957       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11958         return non_lvalue (fold_convert (type, arg1));
11959       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11960           /* Preserve sequence points.  */
11961           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11962         return non_lvalue (fold_convert (type, arg0));
11963       /* If second arg is constant zero, result is zero, but first arg
11964          must be evaluated.  */
11965       if (integer_zerop (arg1))
11966         return omit_one_operand (type, arg1, arg0);
11967       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11968          case will be handled here.  */
11969       if (integer_zerop (arg0))
11970         return omit_one_operand (type, arg0, arg1);
11971
11972       /* !X && X is always false.  */
11973       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11974           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11975         return omit_one_operand (type, integer_zero_node, arg1);
11976       /* X && !X is always false.  */
11977       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11978           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11979         return omit_one_operand (type, integer_zero_node, arg0);
11980
11981       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
11982          means A >= Y && A != MAX, but in this case we know that
11983          A < X <= MAX.  */
11984
11985       if (!TREE_SIDE_EFFECTS (arg0)
11986           && !TREE_SIDE_EFFECTS (arg1))
11987         {
11988           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11989           if (tem && !operand_equal_p (tem, arg0, 0))
11990             return fold_build2 (code, type, tem, arg1);
11991
11992           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11993           if (tem && !operand_equal_p (tem, arg1, 0))
11994             return fold_build2 (code, type, arg0, tem);
11995         }
11996
11997     truth_andor:
11998       /* We only do these simplifications if we are optimizing.  */
11999       if (!optimize)
12000         return NULL_TREE;
12001
12002       /* Check for things like (A || B) && (A || C).  We can convert this
12003          to A || (B && C).  Note that either operator can be any of the four
12004          truth and/or operations and the transformation will still be
12005          valid.   Also note that we only care about order for the
12006          ANDIF and ORIF operators.  If B contains side effects, this
12007          might change the truth-value of A.  */
12008       if (TREE_CODE (arg0) == TREE_CODE (arg1)
12009           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12010               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12011               || TREE_CODE (arg0) == TRUTH_AND_EXPR
12012               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12013           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12014         {
12015           tree a00 = TREE_OPERAND (arg0, 0);
12016           tree a01 = TREE_OPERAND (arg0, 1);
12017           tree a10 = TREE_OPERAND (arg1, 0);
12018           tree a11 = TREE_OPERAND (arg1, 1);
12019           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12020                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12021                              && (code == TRUTH_AND_EXPR
12022                                  || code == TRUTH_OR_EXPR));
12023
12024           if (operand_equal_p (a00, a10, 0))
12025             return fold_build2 (TREE_CODE (arg0), type, a00,
12026                                 fold_build2 (code, type, a01, a11));
12027           else if (commutative && operand_equal_p (a00, a11, 0))
12028             return fold_build2 (TREE_CODE (arg0), type, a00,
12029                                 fold_build2 (code, type, a01, a10));
12030           else if (commutative && operand_equal_p (a01, a10, 0))
12031             return fold_build2 (TREE_CODE (arg0), type, a01,
12032                                 fold_build2 (code, type, a00, a11));
12033
12034           /* This case if tricky because we must either have commutative
12035              operators or else A10 must not have side-effects.  */
12036
12037           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12038                    && operand_equal_p (a01, a11, 0))
12039             return fold_build2 (TREE_CODE (arg0), type,
12040                                 fold_build2 (code, type, a00, a10),
12041                                 a01);
12042         }
12043
12044       /* See if we can build a range comparison.  */
12045       if (0 != (tem = fold_range_test (code, type, op0, op1)))
12046         return tem;
12047
12048       /* Check for the possibility of merging component references.  If our
12049          lhs is another similar operation, try to merge its rhs with our
12050          rhs.  Then try to merge our lhs and rhs.  */
12051       if (TREE_CODE (arg0) == code
12052           && 0 != (tem = fold_truthop (code, type,
12053                                        TREE_OPERAND (arg0, 1), arg1)))
12054         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12055
12056       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12057         return tem;
12058
12059       return NULL_TREE;
12060
12061     case TRUTH_ORIF_EXPR:
12062       /* Note that the operands of this must be ints
12063          and their values must be 0 or true.
12064          ("true" is a fixed value perhaps depending on the language.)  */
12065       /* If first arg is constant true, return it.  */
12066       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12067         return fold_convert (type, arg0);
12068     case TRUTH_OR_EXPR:
12069       /* If either arg is constant zero, drop it.  */
12070       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12071         return non_lvalue (fold_convert (type, arg1));
12072       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12073           /* Preserve sequence points.  */
12074           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12075         return non_lvalue (fold_convert (type, arg0));
12076       /* If second arg is constant true, result is true, but we must
12077          evaluate first arg.  */
12078       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12079         return omit_one_operand (type, arg1, arg0);
12080       /* Likewise for first arg, but note this only occurs here for
12081          TRUTH_OR_EXPR.  */
12082       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12083         return omit_one_operand (type, arg0, arg1);
12084
12085       /* !X || X is always true.  */
12086       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12087           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12088         return omit_one_operand (type, integer_one_node, arg1);
12089       /* X || !X is always true.  */
12090       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12091           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12092         return omit_one_operand (type, integer_one_node, arg0);
12093
12094       goto truth_andor;
12095
12096     case TRUTH_XOR_EXPR:
12097       /* If the second arg is constant zero, drop it.  */
12098       if (integer_zerop (arg1))
12099         return non_lvalue (fold_convert (type, arg0));
12100       /* If the second arg is constant true, this is a logical inversion.  */
12101       if (integer_onep (arg1))
12102         {
12103           /* Only call invert_truthvalue if operand is a truth value.  */
12104           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12105             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12106           else
12107             tem = invert_truthvalue (arg0);
12108           return non_lvalue (fold_convert (type, tem));
12109         }
12110       /* Identical arguments cancel to zero.  */
12111       if (operand_equal_p (arg0, arg1, 0))
12112         return omit_one_operand (type, integer_zero_node, arg0);
12113
12114       /* !X ^ X is always true.  */
12115       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12116           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12117         return omit_one_operand (type, integer_one_node, arg1);
12118
12119       /* X ^ !X is always true.  */
12120       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12121           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12122         return omit_one_operand (type, integer_one_node, arg0);
12123
12124       return NULL_TREE;
12125
12126     case EQ_EXPR:
12127     case NE_EXPR:
12128       tem = fold_comparison (code, type, op0, op1);
12129       if (tem != NULL_TREE)
12130         return tem;
12131
12132       /* bool_var != 0 becomes bool_var. */
12133       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12134           && code == NE_EXPR)
12135         return non_lvalue (fold_convert (type, arg0));
12136
12137       /* bool_var == 1 becomes bool_var. */
12138       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12139           && code == EQ_EXPR)
12140         return non_lvalue (fold_convert (type, arg0));
12141
12142       /* bool_var != 1 becomes !bool_var. */
12143       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12144           && code == NE_EXPR)
12145         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12146
12147       /* bool_var == 0 becomes !bool_var. */
12148       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12149           && code == EQ_EXPR)
12150         return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12151
12152       /* If this is an equality comparison of the address of two non-weak,
12153          unaliased symbols neither of which are extern (since we do not
12154          have access to attributes for externs), then we know the result.  */
12155       if (TREE_CODE (arg0) == ADDR_EXPR
12156           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12157           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12158           && ! lookup_attribute ("alias",
12159                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12160           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12161           && TREE_CODE (arg1) == ADDR_EXPR
12162           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12163           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12164           && ! lookup_attribute ("alias",
12165                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12166           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12167         {
12168           /* We know that we're looking at the address of two
12169              non-weak, unaliased, static _DECL nodes.
12170
12171              It is both wasteful and incorrect to call operand_equal_p
12172              to compare the two ADDR_EXPR nodes.  It is wasteful in that
12173              all we need to do is test pointer equality for the arguments
12174              to the two ADDR_EXPR nodes.  It is incorrect to use
12175              operand_equal_p as that function is NOT equivalent to a
12176              C equality test.  It can in fact return false for two
12177              objects which would test as equal using the C equality
12178              operator.  */
12179           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12180           return constant_boolean_node (equal
12181                                         ? code == EQ_EXPR : code != EQ_EXPR,
12182                                         type);
12183         }
12184
12185       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12186          a MINUS_EXPR of a constant, we can convert it into a comparison with
12187          a revised constant as long as no overflow occurs.  */
12188       if (TREE_CODE (arg1) == INTEGER_CST
12189           && (TREE_CODE (arg0) == PLUS_EXPR
12190               || TREE_CODE (arg0) == MINUS_EXPR)
12191           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12192           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12193                                       ? MINUS_EXPR : PLUS_EXPR,
12194                                       fold_convert (TREE_TYPE (arg0), arg1),
12195                                       TREE_OPERAND (arg0, 1), 0))
12196           && !TREE_OVERFLOW (tem))
12197         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12198
12199       /* Similarly for a NEGATE_EXPR.  */
12200       if (TREE_CODE (arg0) == NEGATE_EXPR
12201           && TREE_CODE (arg1) == INTEGER_CST
12202           && 0 != (tem = negate_expr (arg1))
12203           && TREE_CODE (tem) == INTEGER_CST
12204           && !TREE_OVERFLOW (tem))
12205         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12206
12207       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
12208       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12209           && TREE_CODE (arg1) == INTEGER_CST
12210           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12211         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12212                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12213                                          fold_convert (TREE_TYPE (arg0), arg1),
12214                                          TREE_OPERAND (arg0, 1)));
12215
12216       /* Transform comparisons of the form X +- C CMP X.  */
12217       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12218           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12219           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12220           && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12221               || POINTER_TYPE_P (TREE_TYPE (arg0))))
12222         {
12223           tree cst = TREE_OPERAND (arg0, 1);
12224
12225           if (code == EQ_EXPR
12226               && !integer_zerop (cst))
12227             return omit_two_operands (type, boolean_false_node,
12228                                       TREE_OPERAND (arg0, 0), arg1);
12229           else
12230             return omit_two_operands (type, boolean_true_node,
12231                                       TREE_OPERAND (arg0, 0), arg1);
12232         }
12233
12234       /* If we have X - Y == 0, we can convert that to X == Y and similarly
12235          for !=.  Don't do this for ordered comparisons due to overflow.  */
12236       if (TREE_CODE (arg0) == MINUS_EXPR
12237           && integer_zerop (arg1))
12238         return fold_build2 (code, type,
12239                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12240
12241       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
12242       if (TREE_CODE (arg0) == ABS_EXPR
12243           && (integer_zerop (arg1) || real_zerop (arg1)))
12244         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12245
12246       /* If this is an EQ or NE comparison with zero and ARG0 is
12247          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
12248          two operations, but the latter can be done in one less insn
12249          on machines that have only two-operand insns or on which a
12250          constant cannot be the first operand.  */
12251       if (TREE_CODE (arg0) == BIT_AND_EXPR
12252           && integer_zerop (arg1))
12253         {
12254           tree arg00 = TREE_OPERAND (arg0, 0);
12255           tree arg01 = TREE_OPERAND (arg0, 1);
12256           if (TREE_CODE (arg00) == LSHIFT_EXPR
12257               && integer_onep (TREE_OPERAND (arg00, 0)))
12258             {
12259               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12260                                       arg01, TREE_OPERAND (arg00, 1));
12261               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12262                                  build_int_cst (TREE_TYPE (arg0), 1));
12263               return fold_build2 (code, type,
12264                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12265             }
12266           else if (TREE_CODE (arg01) == LSHIFT_EXPR
12267                    && integer_onep (TREE_OPERAND (arg01, 0)))
12268             {
12269               tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12270                                       arg00, TREE_OPERAND (arg01, 1));
12271               tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12272                                  build_int_cst (TREE_TYPE (arg0), 1));
12273               return fold_build2 (code, type,
12274                                   fold_convert (TREE_TYPE (arg1), tem), arg1);
12275             }
12276         }
12277
12278       /* If this is an NE or EQ comparison of zero against the result of a
12279          signed MOD operation whose second operand is a power of 2, make
12280          the MOD operation unsigned since it is simpler and equivalent.  */
12281       if (integer_zerop (arg1)
12282           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12283           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12284               || TREE_CODE (arg0) == CEIL_MOD_EXPR
12285               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12286               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12287           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12288         {
12289           tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12290           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12291                                      fold_convert (newtype,
12292                                                    TREE_OPERAND (arg0, 0)),
12293                                      fold_convert (newtype,
12294                                                    TREE_OPERAND (arg0, 1)));
12295
12296           return fold_build2 (code, type, newmod,
12297                               fold_convert (newtype, arg1));
12298         }
12299
12300       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12301          C1 is a valid shift constant, and C2 is a power of two, i.e.
12302          a single bit.  */
12303       if (TREE_CODE (arg0) == BIT_AND_EXPR
12304           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12305           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12306              == INTEGER_CST
12307           && integer_pow2p (TREE_OPERAND (arg0, 1))
12308           && integer_zerop (arg1))
12309         {
12310           tree itype = TREE_TYPE (arg0);
12311           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12312           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12313
12314           /* Check for a valid shift count.  */
12315           if (TREE_INT_CST_HIGH (arg001) == 0
12316               && TREE_INT_CST_LOW (arg001) < prec)
12317             {
12318               tree arg01 = TREE_OPERAND (arg0, 1);
12319               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12320               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12321               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12322                  can be rewritten as (X & (C2 << C1)) != 0.  */
12323               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12324                 {
12325                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12326                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12327                   return fold_build2 (code, type, tem, arg1);
12328                 }
12329               /* Otherwise, for signed (arithmetic) shifts,
12330                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12331                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
12332               else if (!TYPE_UNSIGNED (itype))
12333                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12334                                     arg000, build_int_cst (itype, 0));
12335               /* Otherwise, of unsigned (logical) shifts,
12336                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12337                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
12338               else
12339                 return omit_one_operand (type,
12340                                          code == EQ_EXPR ? integer_one_node
12341                                                          : integer_zero_node,
12342                                          arg000);
12343             }
12344         }
12345
12346       /* If this is an NE comparison of zero with an AND of one, remove the
12347          comparison since the AND will give the correct value.  */
12348       if (code == NE_EXPR
12349           && integer_zerop (arg1)
12350           && TREE_CODE (arg0) == BIT_AND_EXPR
12351           && integer_onep (TREE_OPERAND (arg0, 1)))
12352         return fold_convert (type, arg0);
12353
12354       /* If we have (A & C) == C where C is a power of 2, convert this into
12355          (A & C) != 0.  Similarly for NE_EXPR.  */
12356       if (TREE_CODE (arg0) == BIT_AND_EXPR
12357           && integer_pow2p (TREE_OPERAND (arg0, 1))
12358           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12359         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12360                             arg0, fold_convert (TREE_TYPE (arg0),
12361                                                 integer_zero_node));
12362
12363       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12364          bit, then fold the expression into A < 0 or A >= 0.  */
12365       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12366       if (tem)
12367         return tem;
12368
12369       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12370          Similarly for NE_EXPR.  */
12371       if (TREE_CODE (arg0) == BIT_AND_EXPR
12372           && TREE_CODE (arg1) == INTEGER_CST
12373           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12374         {
12375           tree notc = fold_build1 (BIT_NOT_EXPR,
12376                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
12377                                    TREE_OPERAND (arg0, 1));
12378           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12379                                        arg1, notc);
12380           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12381           if (integer_nonzerop (dandnotc))
12382             return omit_one_operand (type, rslt, arg0);
12383         }
12384
12385       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12386          Similarly for NE_EXPR.  */
12387       if (TREE_CODE (arg0) == BIT_IOR_EXPR
12388           && TREE_CODE (arg1) == INTEGER_CST
12389           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12390         {
12391           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12392           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12393                                        TREE_OPERAND (arg0, 1), notd);
12394           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12395           if (integer_nonzerop (candnotd))
12396             return omit_one_operand (type, rslt, arg0);
12397         }
12398
12399       /* If this is a comparison of a field, we may be able to simplify it.  */
12400       if ((TREE_CODE (arg0) == COMPONENT_REF
12401            || TREE_CODE (arg0) == BIT_FIELD_REF)
12402           /* Handle the constant case even without -O
12403              to make sure the warnings are given.  */
12404           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12405         {
12406           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12407           if (t1)
12408             return t1;
12409         }
12410
12411       /* Optimize comparisons of strlen vs zero to a compare of the
12412          first character of the string vs zero.  To wit,
12413                 strlen(ptr) == 0   =>  *ptr == 0
12414                 strlen(ptr) != 0   =>  *ptr != 0
12415          Other cases should reduce to one of these two (or a constant)
12416          due to the return value of strlen being unsigned.  */
12417       if (TREE_CODE (arg0) == CALL_EXPR
12418           && integer_zerop (arg1))
12419         {
12420           tree fndecl = get_callee_fndecl (arg0);
12421
12422           if (fndecl
12423               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12424               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12425               && call_expr_nargs (arg0) == 1
12426               && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12427             {
12428               tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12429               return fold_build2 (code, type, iref,
12430                                   build_int_cst (TREE_TYPE (iref), 0));
12431             }
12432         }
12433
12434       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12435          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
12436       if (TREE_CODE (arg0) == RSHIFT_EXPR
12437           && integer_zerop (arg1)
12438           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12439         {
12440           tree arg00 = TREE_OPERAND (arg0, 0);
12441           tree arg01 = TREE_OPERAND (arg0, 1);
12442           tree itype = TREE_TYPE (arg00);
12443           if (TREE_INT_CST_HIGH (arg01) == 0
12444               && TREE_INT_CST_LOW (arg01)
12445                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12446             {
12447               if (TYPE_UNSIGNED (itype))
12448                 {
12449                   itype = signed_type_for (itype);
12450                   arg00 = fold_convert (itype, arg00);
12451                 }
12452               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12453                                   type, arg00, build_int_cst (itype, 0));
12454             }
12455         }
12456
12457       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
12458       if (integer_zerop (arg1)
12459           && TREE_CODE (arg0) == BIT_XOR_EXPR)
12460         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12461                             TREE_OPERAND (arg0, 1));
12462
12463       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
12464       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12465           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12466         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12467                             build_int_cst (TREE_TYPE (arg1), 0));
12468       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
12469       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12470           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12471           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12472         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12473                             build_int_cst (TREE_TYPE (arg1), 0));
12474
12475       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
12476       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12477           && TREE_CODE (arg1) == INTEGER_CST
12478           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12479         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12480                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12481                                          TREE_OPERAND (arg0, 1), arg1));
12482
12483       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12484          (X & C) == 0 when C is a single bit.  */
12485       if (TREE_CODE (arg0) == BIT_AND_EXPR
12486           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12487           && integer_zerop (arg1)
12488           && integer_pow2p (TREE_OPERAND (arg0, 1)))
12489         {
12490           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12491                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12492                              TREE_OPERAND (arg0, 1));
12493           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12494                               type, tem, arg1);
12495         }
12496
12497       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12498          constant C is a power of two, i.e. a single bit.  */
12499       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12500           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12501           && integer_zerop (arg1)
12502           && integer_pow2p (TREE_OPERAND (arg0, 1))
12503           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12504                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12505         {
12506           tree arg00 = TREE_OPERAND (arg0, 0);
12507           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12508                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
12509         }
12510
12511       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12512          when is C is a power of two, i.e. a single bit.  */
12513       if (TREE_CODE (arg0) == BIT_AND_EXPR
12514           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12515           && integer_zerop (arg1)
12516           && integer_pow2p (TREE_OPERAND (arg0, 1))
12517           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12518                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12519         {
12520           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12521           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12522                              arg000, TREE_OPERAND (arg0, 1));
12523           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12524                               tem, build_int_cst (TREE_TYPE (tem), 0));
12525         }
12526
12527       if (integer_zerop (arg1)
12528           && tree_expr_nonzero_p (arg0))
12529         {
12530           tree res = constant_boolean_node (code==NE_EXPR, type);
12531           return omit_one_operand (type, res, arg0);
12532         }
12533
12534       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
12535       if (TREE_CODE (arg0) == NEGATE_EXPR
12536           && TREE_CODE (arg1) == NEGATE_EXPR)
12537         return fold_build2 (code, type,
12538                             TREE_OPERAND (arg0, 0),
12539                             TREE_OPERAND (arg1, 0));
12540
12541       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
12542       if (TREE_CODE (arg0) == BIT_AND_EXPR
12543           && TREE_CODE (arg1) == BIT_AND_EXPR)
12544         {
12545           tree arg00 = TREE_OPERAND (arg0, 0);
12546           tree arg01 = TREE_OPERAND (arg0, 1);
12547           tree arg10 = TREE_OPERAND (arg1, 0);
12548           tree arg11 = TREE_OPERAND (arg1, 1);
12549           tree itype = TREE_TYPE (arg0);
12550
12551           if (operand_equal_p (arg01, arg11, 0))
12552             return fold_build2 (code, type,
12553                                 fold_build2 (BIT_AND_EXPR, itype,
12554                                              fold_build2 (BIT_XOR_EXPR, itype,
12555                                                           arg00, arg10),
12556                                              arg01),
12557                                 build_int_cst (itype, 0));
12558
12559           if (operand_equal_p (arg01, arg10, 0))
12560             return fold_build2 (code, type,
12561                                 fold_build2 (BIT_AND_EXPR, itype,
12562                                              fold_build2 (BIT_XOR_EXPR, itype,
12563                                                           arg00, arg11),
12564                                              arg01),
12565                                 build_int_cst (itype, 0));
12566
12567           if (operand_equal_p (arg00, arg11, 0))
12568             return fold_build2 (code, type,
12569                                 fold_build2 (BIT_AND_EXPR, itype,
12570                                              fold_build2 (BIT_XOR_EXPR, itype,
12571                                                           arg01, arg10),
12572                                              arg00),
12573                                 build_int_cst (itype, 0));
12574
12575           if (operand_equal_p (arg00, arg10, 0))
12576             return fold_build2 (code, type,
12577                                 fold_build2 (BIT_AND_EXPR, itype,
12578                                              fold_build2 (BIT_XOR_EXPR, itype,
12579                                                           arg01, arg11),
12580                                              arg00),
12581                                 build_int_cst (itype, 0));
12582         }
12583
12584       if (TREE_CODE (arg0) == BIT_XOR_EXPR
12585           && TREE_CODE (arg1) == BIT_XOR_EXPR)
12586         {
12587           tree arg00 = TREE_OPERAND (arg0, 0);
12588           tree arg01 = TREE_OPERAND (arg0, 1);
12589           tree arg10 = TREE_OPERAND (arg1, 0);
12590           tree arg11 = TREE_OPERAND (arg1, 1);
12591           tree itype = TREE_TYPE (arg0);
12592
12593           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12594              operand_equal_p guarantees no side-effects so we don't need
12595              to use omit_one_operand on Z.  */
12596           if (operand_equal_p (arg01, arg11, 0))
12597             return fold_build2 (code, type, arg00, arg10);
12598           if (operand_equal_p (arg01, arg10, 0))
12599             return fold_build2 (code, type, arg00, arg11);
12600           if (operand_equal_p (arg00, arg11, 0))
12601             return fold_build2 (code, type, arg01, arg10);
12602           if (operand_equal_p (arg00, arg10, 0))
12603             return fold_build2 (code, type, arg01, arg11);
12604
12605           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
12606           if (TREE_CODE (arg01) == INTEGER_CST
12607               && TREE_CODE (arg11) == INTEGER_CST)
12608             return fold_build2 (code, type,
12609                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12610                                              fold_build2 (BIT_XOR_EXPR, itype,
12611                                                           arg01, arg11)),
12612                                 arg10);
12613         }
12614
12615       /* Attempt to simplify equality/inequality comparisons of complex
12616          values.  Only lower the comparison if the result is known or
12617          can be simplified to a single scalar comparison.  */
12618       if ((TREE_CODE (arg0) == COMPLEX_EXPR
12619            || TREE_CODE (arg0) == COMPLEX_CST)
12620           && (TREE_CODE (arg1) == COMPLEX_EXPR
12621               || TREE_CODE (arg1) == COMPLEX_CST))
12622         {
12623           tree real0, imag0, real1, imag1;
12624           tree rcond, icond;
12625
12626           if (TREE_CODE (arg0) == COMPLEX_EXPR)
12627             {
12628               real0 = TREE_OPERAND (arg0, 0);
12629               imag0 = TREE_OPERAND (arg0, 1);
12630             }
12631           else
12632             {
12633               real0 = TREE_REALPART (arg0);
12634               imag0 = TREE_IMAGPART (arg0);
12635             }
12636
12637           if (TREE_CODE (arg1) == COMPLEX_EXPR)
12638             {
12639               real1 = TREE_OPERAND (arg1, 0);
12640               imag1 = TREE_OPERAND (arg1, 1);
12641             }
12642           else
12643             {
12644               real1 = TREE_REALPART (arg1);
12645               imag1 = TREE_IMAGPART (arg1);
12646             }
12647
12648           rcond = fold_binary (code, type, real0, real1);
12649           if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12650             {
12651               if (integer_zerop (rcond))
12652                 {
12653                   if (code == EQ_EXPR)
12654                     return omit_two_operands (type, boolean_false_node,
12655                                               imag0, imag1);
12656                   return fold_build2 (NE_EXPR, type, imag0, imag1);
12657                 }
12658               else
12659                 {
12660                   if (code == NE_EXPR)
12661                     return omit_two_operands (type, boolean_true_node,
12662                                               imag0, imag1);
12663                   return fold_build2 (EQ_EXPR, type, imag0, imag1);
12664                 }
12665             }
12666
12667           icond = fold_binary (code, type, imag0, imag1);
12668           if (icond && TREE_CODE (icond) == INTEGER_CST)
12669             {
12670               if (integer_zerop (icond))
12671                 {
12672                   if (code == EQ_EXPR)
12673                     return omit_two_operands (type, boolean_false_node,
12674                                               real0, real1);
12675                   return fold_build2 (NE_EXPR, type, real0, real1);
12676                 }
12677               else
12678                 {
12679                   if (code == NE_EXPR)
12680                     return omit_two_operands (type, boolean_true_node,
12681                                               real0, real1);
12682                   return fold_build2 (EQ_EXPR, type, real0, real1);
12683                 }
12684             }
12685         }
12686
12687       return NULL_TREE;
12688
12689     case LT_EXPR:
12690     case GT_EXPR:
12691     case LE_EXPR:
12692     case GE_EXPR:
12693       tem = fold_comparison (code, type, op0, op1);
12694       if (tem != NULL_TREE)
12695         return tem;
12696
12697       /* Transform comparisons of the form X +- C CMP X.  */
12698       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12699           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12700           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12701                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12702               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12703                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12704         {
12705           tree arg01 = TREE_OPERAND (arg0, 1);
12706           enum tree_code code0 = TREE_CODE (arg0);
12707           int is_positive;
12708
12709           if (TREE_CODE (arg01) == REAL_CST)
12710             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12711           else
12712             is_positive = tree_int_cst_sgn (arg01);
12713
12714           /* (X - c) > X becomes false.  */
12715           if (code == GT_EXPR
12716               && ((code0 == MINUS_EXPR && is_positive >= 0)
12717                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12718             {
12719               if (TREE_CODE (arg01) == INTEGER_CST
12720                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12721                 fold_overflow_warning (("assuming signed overflow does not "
12722                                         "occur when assuming that (X - c) > X "
12723                                         "is always false"),
12724                                        WARN_STRICT_OVERFLOW_ALL);
12725               return constant_boolean_node (0, type);
12726             }
12727
12728           /* Likewise (X + c) < X becomes false.  */
12729           if (code == LT_EXPR
12730               && ((code0 == PLUS_EXPR && is_positive >= 0)
12731                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12732             {
12733               if (TREE_CODE (arg01) == INTEGER_CST
12734                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12735                 fold_overflow_warning (("assuming signed overflow does not "
12736                                         "occur when assuming that "
12737                                         "(X + c) < X is always false"),
12738                                        WARN_STRICT_OVERFLOW_ALL);
12739               return constant_boolean_node (0, type);
12740             }
12741
12742           /* Convert (X - c) <= X to true.  */
12743           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12744               && code == LE_EXPR
12745               && ((code0 == MINUS_EXPR && is_positive >= 0)
12746                   || (code0 == PLUS_EXPR && is_positive <= 0)))
12747             {
12748               if (TREE_CODE (arg01) == INTEGER_CST
12749                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12750                 fold_overflow_warning (("assuming signed overflow does not "
12751                                         "occur when assuming that "
12752                                         "(X - c) <= X is always true"),
12753                                        WARN_STRICT_OVERFLOW_ALL);
12754               return constant_boolean_node (1, type);
12755             }
12756
12757           /* Convert (X + c) >= X to true.  */
12758           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12759               && code == GE_EXPR
12760               && ((code0 == PLUS_EXPR && is_positive >= 0)
12761                   || (code0 == MINUS_EXPR && is_positive <= 0)))
12762             {
12763               if (TREE_CODE (arg01) == INTEGER_CST
12764                   && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12765                 fold_overflow_warning (("assuming signed overflow does not "
12766                                         "occur when assuming that "
12767                                         "(X + c) >= X is always true"),
12768                                        WARN_STRICT_OVERFLOW_ALL);
12769               return constant_boolean_node (1, type);
12770             }
12771
12772           if (TREE_CODE (arg01) == INTEGER_CST)
12773             {
12774               /* Convert X + c > X and X - c < X to true for integers.  */
12775               if (code == GT_EXPR
12776                   && ((code0 == PLUS_EXPR && is_positive > 0)
12777                       || (code0 == MINUS_EXPR && is_positive < 0)))
12778                 {
12779                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12780                     fold_overflow_warning (("assuming signed overflow does "
12781                                             "not occur when assuming that "
12782                                             "(X + c) > X is always true"),
12783                                            WARN_STRICT_OVERFLOW_ALL);
12784                   return constant_boolean_node (1, type);
12785                 }
12786
12787               if (code == LT_EXPR
12788                   && ((code0 == MINUS_EXPR && is_positive > 0)
12789                       || (code0 == PLUS_EXPR && is_positive < 0)))
12790                 {
12791                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12792                     fold_overflow_warning (("assuming signed overflow does "
12793                                             "not occur when assuming that "
12794                                             "(X - c) < X is always true"),
12795                                            WARN_STRICT_OVERFLOW_ALL);
12796                   return constant_boolean_node (1, type);
12797                 }
12798
12799               /* Convert X + c <= X and X - c >= X to false for integers.  */
12800               if (code == LE_EXPR
12801                   && ((code0 == PLUS_EXPR && is_positive > 0)
12802                       || (code0 == MINUS_EXPR && is_positive < 0)))
12803                 {
12804                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12805                     fold_overflow_warning (("assuming signed overflow does "
12806                                             "not occur when assuming that "
12807                                             "(X + c) <= X is always false"),
12808                                            WARN_STRICT_OVERFLOW_ALL);
12809                   return constant_boolean_node (0, type);
12810                 }
12811
12812               if (code == GE_EXPR
12813                   && ((code0 == MINUS_EXPR && is_positive > 0)
12814                       || (code0 == PLUS_EXPR && is_positive < 0)))
12815                 {
12816                   if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12817                     fold_overflow_warning (("assuming signed overflow does "
12818                                             "not occur when assuming that "
12819                                             "(X - c) >= X is always false"),
12820                                            WARN_STRICT_OVERFLOW_ALL);
12821                   return constant_boolean_node (0, type);
12822                 }
12823             }
12824         }
12825
12826       /* Comparisons with the highest or lowest possible integer of
12827          the specified precision will have known values.  */
12828       {
12829         tree arg1_type = TREE_TYPE (arg1);
12830         unsigned int width = TYPE_PRECISION (arg1_type);
12831
12832         if (TREE_CODE (arg1) == INTEGER_CST
12833             && width <= 2 * HOST_BITS_PER_WIDE_INT
12834             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12835           {
12836             HOST_WIDE_INT signed_max_hi;
12837             unsigned HOST_WIDE_INT signed_max_lo;
12838             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12839
12840             if (width <= HOST_BITS_PER_WIDE_INT)
12841               {
12842                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12843                                 - 1;
12844                 signed_max_hi = 0;
12845                 max_hi = 0;
12846
12847                 if (TYPE_UNSIGNED (arg1_type))
12848                   {
12849                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12850                     min_lo = 0;
12851                     min_hi = 0;
12852                   }
12853                 else
12854                   {
12855                     max_lo = signed_max_lo;
12856                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12857                     min_hi = -1;
12858                   }
12859               }
12860             else
12861               {
12862                 width -= HOST_BITS_PER_WIDE_INT;
12863                 signed_max_lo = -1;
12864                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12865                                 - 1;
12866                 max_lo = -1;
12867                 min_lo = 0;
12868
12869                 if (TYPE_UNSIGNED (arg1_type))
12870                   {
12871                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12872                     min_hi = 0;
12873                   }
12874                 else
12875                   {
12876                     max_hi = signed_max_hi;
12877                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12878                   }
12879               }
12880
12881             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12882                 && TREE_INT_CST_LOW (arg1) == max_lo)
12883               switch (code)
12884                 {
12885                 case GT_EXPR:
12886                   return omit_one_operand (type, integer_zero_node, arg0);
12887
12888                 case GE_EXPR:
12889                   return fold_build2 (EQ_EXPR, type, op0, op1);
12890
12891                 case LE_EXPR:
12892                   return omit_one_operand (type, integer_one_node, arg0);
12893
12894                 case LT_EXPR:
12895                   return fold_build2 (NE_EXPR, type, op0, op1);
12896
12897                 /* The GE_EXPR and LT_EXPR cases above are not normally
12898                    reached because of previous transformations.  */
12899
12900                 default:
12901                   break;
12902                 }
12903             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12904                      == max_hi
12905                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12906               switch (code)
12907                 {
12908                 case GT_EXPR:
12909                   arg1 = const_binop (PLUS_EXPR, arg1,
12910                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12911                   return fold_build2 (EQ_EXPR, type,
12912                                       fold_convert (TREE_TYPE (arg1), arg0),
12913                                       arg1);
12914                 case LE_EXPR:
12915                   arg1 = const_binop (PLUS_EXPR, arg1,
12916                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
12917                   return fold_build2 (NE_EXPR, type,
12918                                       fold_convert (TREE_TYPE (arg1), arg0),
12919                                       arg1);
12920                 default:
12921                   break;
12922                 }
12923             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12924                      == min_hi
12925                      && TREE_INT_CST_LOW (arg1) == min_lo)
12926               switch (code)
12927                 {
12928                 case LT_EXPR:
12929                   return omit_one_operand (type, integer_zero_node, arg0);
12930
12931                 case LE_EXPR:
12932                   return fold_build2 (EQ_EXPR, type, op0, op1);
12933
12934                 case GE_EXPR:
12935                   return omit_one_operand (type, integer_one_node, arg0);
12936
12937                 case GT_EXPR:
12938                   return fold_build2 (NE_EXPR, type, op0, op1);
12939
12940                 default:
12941                   break;
12942                 }
12943             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12944                      == min_hi
12945                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12946               switch (code)
12947                 {
12948                 case GE_EXPR:
12949                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12950                   return fold_build2 (NE_EXPR, type,
12951                                       fold_convert (TREE_TYPE (arg1), arg0),
12952                                       arg1);
12953                 case LT_EXPR:
12954                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12955                   return fold_build2 (EQ_EXPR, type,
12956                                       fold_convert (TREE_TYPE (arg1), arg0),
12957                                       arg1);
12958                 default:
12959                   break;
12960                 }
12961
12962             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12963                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
12964                      && TYPE_UNSIGNED (arg1_type)
12965                      /* We will flip the signedness of the comparison operator
12966                         associated with the mode of arg1, so the sign bit is
12967                         specified by this mode.  Check that arg1 is the signed
12968                         max associated with this sign bit.  */
12969                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12970                      /* signed_type does not work on pointer types.  */
12971                      && INTEGRAL_TYPE_P (arg1_type))
12972               {
12973                 /* The following case also applies to X < signed_max+1
12974                    and X >= signed_max+1 because previous transformations.  */
12975                 if (code == LE_EXPR || code == GT_EXPR)
12976                   {
12977                     tree st;
12978                     st = signed_type_for (TREE_TYPE (arg1));
12979                     return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12980                                         type, fold_convert (st, arg0),
12981                                         build_int_cst (st, 0));
12982                   }
12983               }
12984           }
12985       }
12986
12987       /* If we are comparing an ABS_EXPR with a constant, we can
12988          convert all the cases into explicit comparisons, but they may
12989          well not be faster than doing the ABS and one comparison.
12990          But ABS (X) <= C is a range comparison, which becomes a subtraction
12991          and a comparison, and is probably faster.  */
12992       if (code == LE_EXPR
12993           && TREE_CODE (arg1) == INTEGER_CST
12994           && TREE_CODE (arg0) == ABS_EXPR
12995           && ! TREE_SIDE_EFFECTS (arg0)
12996           && (0 != (tem = negate_expr (arg1)))
12997           && TREE_CODE (tem) == INTEGER_CST
12998           && !TREE_OVERFLOW (tem))
12999         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13000                             build2 (GE_EXPR, type,
13001                                     TREE_OPERAND (arg0, 0), tem),
13002                             build2 (LE_EXPR, type,
13003                                     TREE_OPERAND (arg0, 0), arg1));
13004
13005       /* Convert ABS_EXPR<x> >= 0 to true.  */
13006       strict_overflow_p = false;
13007       if (code == GE_EXPR
13008           && (integer_zerop (arg1)
13009               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13010                   && real_zerop (arg1)))
13011           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13012         {
13013           if (strict_overflow_p)
13014             fold_overflow_warning (("assuming signed overflow does not occur "
13015                                     "when simplifying comparison of "
13016                                     "absolute value and zero"),
13017                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13018           return omit_one_operand (type, integer_one_node, arg0);
13019         }
13020
13021       /* Convert ABS_EXPR<x> < 0 to false.  */
13022       strict_overflow_p = false;
13023       if (code == LT_EXPR
13024           && (integer_zerop (arg1) || real_zerop (arg1))
13025           && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13026         {
13027           if (strict_overflow_p)
13028             fold_overflow_warning (("assuming signed overflow does not occur "
13029                                     "when simplifying comparison of "
13030                                     "absolute value and zero"),
13031                                    WARN_STRICT_OVERFLOW_CONDITIONAL);
13032           return omit_one_operand (type, integer_zero_node, arg0);
13033         }
13034
13035       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13036          and similarly for >= into !=.  */
13037       if ((code == LT_EXPR || code == GE_EXPR)
13038           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13039           && TREE_CODE (arg1) == LSHIFT_EXPR
13040           && integer_onep (TREE_OPERAND (arg1, 0)))
13041         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13042                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13043                                TREE_OPERAND (arg1, 1)),
13044                        build_int_cst (TREE_TYPE (arg0), 0));
13045
13046       if ((code == LT_EXPR || code == GE_EXPR)
13047           && TYPE_UNSIGNED (TREE_TYPE (arg0))
13048           && CONVERT_EXPR_P (arg1)
13049           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13050           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13051         return
13052           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13053                   fold_convert (TREE_TYPE (arg0),
13054                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13055                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
13056                                                       1))),
13057                   build_int_cst (TREE_TYPE (arg0), 0));
13058
13059       return NULL_TREE;
13060
13061     case UNORDERED_EXPR:
13062     case ORDERED_EXPR:
13063     case UNLT_EXPR:
13064     case UNLE_EXPR:
13065     case UNGT_EXPR:
13066     case UNGE_EXPR:
13067     case UNEQ_EXPR:
13068     case LTGT_EXPR:
13069       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13070         {
13071           t1 = fold_relational_const (code, type, arg0, arg1);
13072           if (t1 != NULL_TREE)
13073             return t1;
13074         }
13075
13076       /* If the first operand is NaN, the result is constant.  */
13077       if (TREE_CODE (arg0) == REAL_CST
13078           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13079           && (code != LTGT_EXPR || ! flag_trapping_math))
13080         {
13081           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13082                ? integer_zero_node
13083                : integer_one_node;
13084           return omit_one_operand (type, t1, arg1);
13085         }
13086
13087       /* If the second operand is NaN, the result is constant.  */
13088       if (TREE_CODE (arg1) == REAL_CST
13089           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13090           && (code != LTGT_EXPR || ! flag_trapping_math))
13091         {
13092           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13093                ? integer_zero_node
13094                : integer_one_node;
13095           return omit_one_operand (type, t1, arg0);
13096         }
13097
13098       /* Simplify unordered comparison of something with itself.  */
13099       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13100           && operand_equal_p (arg0, arg1, 0))
13101         return constant_boolean_node (1, type);
13102
13103       if (code == LTGT_EXPR
13104           && !flag_trapping_math
13105           && operand_equal_p (arg0, arg1, 0))
13106         return constant_boolean_node (0, type);
13107
13108       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
13109       {
13110         tree targ0 = strip_float_extensions (arg0);
13111         tree targ1 = strip_float_extensions (arg1);
13112         tree newtype = TREE_TYPE (targ0);
13113
13114         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13115           newtype = TREE_TYPE (targ1);
13116
13117         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13118           return fold_build2 (code, type, fold_convert (newtype, targ0),
13119                               fold_convert (newtype, targ1));
13120       }
13121
13122       return NULL_TREE;
13123
13124     case COMPOUND_EXPR:
13125       /* When pedantic, a compound expression can be neither an lvalue
13126          nor an integer constant expression.  */
13127       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13128         return NULL_TREE;
13129       /* Don't let (0, 0) be null pointer constant.  */
13130       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13131                                  : fold_convert (type, arg1);
13132       return pedantic_non_lvalue (tem);
13133
13134     case COMPLEX_EXPR:
13135       if ((TREE_CODE (arg0) == REAL_CST
13136            && TREE_CODE (arg1) == REAL_CST)
13137           || (TREE_CODE (arg0) == INTEGER_CST
13138               && TREE_CODE (arg1) == INTEGER_CST))
13139         return build_complex (type, arg0, arg1);
13140       return NULL_TREE;
13141
13142     case ASSERT_EXPR:
13143       /* An ASSERT_EXPR should never be passed to fold_binary.  */
13144       gcc_unreachable ();
13145
13146     default:
13147       return NULL_TREE;
13148     } /* switch (code) */
13149 }
13150
13151 /* Callback for walk_tree, looking for LABEL_EXPR.
13152    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13153    Do not check the sub-tree of GOTO_EXPR.  */
13154
13155 static tree
13156 contains_label_1 (tree *tp,
13157                   int *walk_subtrees,
13158                   void *data ATTRIBUTE_UNUSED)
13159 {
13160   switch (TREE_CODE (*tp))
13161     {
13162     case LABEL_EXPR:
13163       return *tp;
13164     case GOTO_EXPR:
13165       *walk_subtrees = 0;
13166     /* no break */
13167     default:
13168       return NULL_TREE;
13169     }
13170 }
13171
13172 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13173    accessible from outside the sub-tree. Returns NULL_TREE if no
13174    addressable label is found.  */
13175
13176 static bool
13177 contains_label_p (tree st)
13178 {
13179   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13180 }
13181
13182 /* Fold a ternary expression of code CODE and type TYPE with operands
13183    OP0, OP1, and OP2.  Return the folded expression if folding is
13184    successful.  Otherwise, return NULL_TREE.  */
13185
13186 tree
13187 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13188 {
13189   tree tem;
13190   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13191   enum tree_code_class kind = TREE_CODE_CLASS (code);
13192
13193   gcc_assert (IS_EXPR_CODE_CLASS (kind)
13194               && TREE_CODE_LENGTH (code) == 3);
13195
13196   /* Strip any conversions that don't change the mode.  This is safe
13197      for every expression, except for a comparison expression because
13198      its signedness is derived from its operands.  So, in the latter
13199      case, only strip conversions that don't change the signedness.
13200
13201      Note that this is done as an internal manipulation within the
13202      constant folder, in order to find the simplest representation of
13203      the arguments so that their form can be studied.  In any cases,
13204      the appropriate type conversions should be put back in the tree
13205      that will get out of the constant folder.  */
13206   if (op0)
13207     {
13208       arg0 = op0;
13209       STRIP_NOPS (arg0);
13210     }
13211
13212   if (op1)
13213     {
13214       arg1 = op1;
13215       STRIP_NOPS (arg1);
13216     }
13217
13218   switch (code)
13219     {
13220     case COMPONENT_REF:
13221       if (TREE_CODE (arg0) == CONSTRUCTOR
13222           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13223         {
13224           unsigned HOST_WIDE_INT idx;
13225           tree field, value;
13226           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13227             if (field == arg1)
13228               return value;
13229         }
13230       return NULL_TREE;
13231
13232     case COND_EXPR:
13233       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13234          so all simple results must be passed through pedantic_non_lvalue.  */
13235       if (TREE_CODE (arg0) == INTEGER_CST)
13236         {
13237           tree unused_op = integer_zerop (arg0) ? op1 : op2;
13238           tem = integer_zerop (arg0) ? op2 : op1;
13239           /* Only optimize constant conditions when the selected branch
13240              has the same type as the COND_EXPR.  This avoids optimizing
13241              away "c ? x : throw", where the throw has a void type.
13242              Avoid throwing away that operand which contains label.  */
13243           if ((!TREE_SIDE_EFFECTS (unused_op)
13244                || !contains_label_p (unused_op))
13245               && (! VOID_TYPE_P (TREE_TYPE (tem))
13246                   || VOID_TYPE_P (type)))
13247             return pedantic_non_lvalue (tem);
13248           return NULL_TREE;
13249         }
13250       if (operand_equal_p (arg1, op2, 0))
13251         return pedantic_omit_one_operand (type, arg1, arg0);
13252
13253       /* If we have A op B ? A : C, we may be able to convert this to a
13254          simpler expression, depending on the operation and the values
13255          of B and C.  Signed zeros prevent all of these transformations,
13256          for reasons given above each one.
13257
13258          Also try swapping the arguments and inverting the conditional.  */
13259       if (COMPARISON_CLASS_P (arg0)
13260           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13261                                              arg1, TREE_OPERAND (arg0, 1))
13262           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13263         {
13264           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13265           if (tem)
13266             return tem;
13267         }
13268
13269       if (COMPARISON_CLASS_P (arg0)
13270           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13271                                              op2,
13272                                              TREE_OPERAND (arg0, 1))
13273           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13274         {
13275           tem = fold_truth_not_expr (arg0);
13276           if (tem && COMPARISON_CLASS_P (tem))
13277             {
13278               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13279               if (tem)
13280                 return tem;
13281             }
13282         }
13283
13284       /* If the second operand is simpler than the third, swap them
13285          since that produces better jump optimization results.  */
13286       if (truth_value_p (TREE_CODE (arg0))
13287           && tree_swap_operands_p (op1, op2, false))
13288         {
13289           /* See if this can be inverted.  If it can't, possibly because
13290              it was a floating-point inequality comparison, don't do
13291              anything.  */
13292           tem = fold_truth_not_expr (arg0);
13293           if (tem)
13294             return fold_build3 (code, type, tem, op2, op1);
13295         }
13296
13297       /* Convert A ? 1 : 0 to simply A.  */
13298       if (integer_onep (op1)
13299           && integer_zerop (op2)
13300           /* If we try to convert OP0 to our type, the
13301              call to fold will try to move the conversion inside
13302              a COND, which will recurse.  In that case, the COND_EXPR
13303              is probably the best choice, so leave it alone.  */
13304           && type == TREE_TYPE (arg0))
13305         return pedantic_non_lvalue (arg0);
13306
13307       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
13308          over COND_EXPR in cases such as floating point comparisons.  */
13309       if (integer_zerop (op1)
13310           && integer_onep (op2)
13311           && truth_value_p (TREE_CODE (arg0)))
13312         return pedantic_non_lvalue (fold_convert (type,
13313                                                   invert_truthvalue (arg0)));
13314
13315       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
13316       if (TREE_CODE (arg0) == LT_EXPR
13317           && integer_zerop (TREE_OPERAND (arg0, 1))
13318           && integer_zerop (op2)
13319           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13320         {
13321           /* sign_bit_p only checks ARG1 bits within A's precision.
13322              If <sign bit of A> has wider type than A, bits outside
13323              of A's precision in <sign bit of A> need to be checked.
13324              If they are all 0, this optimization needs to be done
13325              in unsigned A's type, if they are all 1 in signed A's type,
13326              otherwise this can't be done.  */
13327           if (TYPE_PRECISION (TREE_TYPE (tem))
13328               < TYPE_PRECISION (TREE_TYPE (arg1))
13329               && TYPE_PRECISION (TREE_TYPE (tem))
13330                  < TYPE_PRECISION (type))
13331             {
13332               unsigned HOST_WIDE_INT mask_lo;
13333               HOST_WIDE_INT mask_hi;
13334               int inner_width, outer_width;
13335               tree tem_type;
13336
13337               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13338               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13339               if (outer_width > TYPE_PRECISION (type))
13340                 outer_width = TYPE_PRECISION (type);
13341
13342               if (outer_width > HOST_BITS_PER_WIDE_INT)
13343                 {
13344                   mask_hi = ((unsigned HOST_WIDE_INT) -1
13345                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13346                   mask_lo = -1;
13347                 }
13348               else
13349                 {
13350                   mask_hi = 0;
13351                   mask_lo = ((unsigned HOST_WIDE_INT) -1
13352                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
13353                 }
13354               if (inner_width > HOST_BITS_PER_WIDE_INT)
13355                 {
13356                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13357                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
13358                   mask_lo = 0;
13359                 }
13360               else
13361                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13362                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
13363
13364               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13365                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13366                 {
13367                   tem_type = signed_type_for (TREE_TYPE (tem));
13368                   tem = fold_convert (tem_type, tem);
13369                 }
13370               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13371                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13372                 {
13373                   tem_type = unsigned_type_for (TREE_TYPE (tem));
13374                   tem = fold_convert (tem_type, tem);
13375                 }
13376               else
13377                 tem = NULL;
13378             }
13379
13380           if (tem)
13381             return fold_convert (type,
13382                                  fold_build2 (BIT_AND_EXPR,
13383                                               TREE_TYPE (tem), tem,
13384                                               fold_convert (TREE_TYPE (tem),
13385                                                             arg1)));
13386         }
13387
13388       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
13389          already handled above.  */
13390       if (TREE_CODE (arg0) == BIT_AND_EXPR
13391           && integer_onep (TREE_OPERAND (arg0, 1))
13392           && integer_zerop (op2)
13393           && integer_pow2p (arg1))
13394         {
13395           tree tem = TREE_OPERAND (arg0, 0);
13396           STRIP_NOPS (tem);
13397           if (TREE_CODE (tem) == RSHIFT_EXPR
13398               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13399               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13400                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13401             return fold_build2 (BIT_AND_EXPR, type,
13402                                 TREE_OPERAND (tem, 0), arg1);
13403         }
13404
13405       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
13406          is probably obsolete because the first operand should be a
13407          truth value (that's why we have the two cases above), but let's
13408          leave it in until we can confirm this for all front-ends.  */
13409       if (integer_zerop (op2)
13410           && TREE_CODE (arg0) == NE_EXPR
13411           && integer_zerop (TREE_OPERAND (arg0, 1))
13412           && integer_pow2p (arg1)
13413           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13414           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13415                               arg1, OEP_ONLY_CONST))
13416         return pedantic_non_lvalue (fold_convert (type,
13417                                                   TREE_OPERAND (arg0, 0)));
13418
13419       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
13420       if (integer_zerop (op2)
13421           && truth_value_p (TREE_CODE (arg0))
13422           && truth_value_p (TREE_CODE (arg1)))
13423         return fold_build2 (TRUTH_ANDIF_EXPR, type,
13424                             fold_convert (type, arg0),
13425                             arg1);
13426
13427       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
13428       if (integer_onep (op2)
13429           && truth_value_p (TREE_CODE (arg0))
13430           && truth_value_p (TREE_CODE (arg1)))
13431         {
13432           /* Only perform transformation if ARG0 is easily inverted.  */
13433           tem = fold_truth_not_expr (arg0);
13434           if (tem)
13435             return fold_build2 (TRUTH_ORIF_EXPR, type,
13436                                 fold_convert (type, tem),
13437                                 arg1);
13438         }
13439
13440       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
13441       if (integer_zerop (arg1)
13442           && truth_value_p (TREE_CODE (arg0))
13443           && truth_value_p (TREE_CODE (op2)))
13444         {
13445           /* Only perform transformation if ARG0 is easily inverted.  */
13446           tem = fold_truth_not_expr (arg0);
13447           if (tem)
13448             return fold_build2 (TRUTH_ANDIF_EXPR, type,
13449                                 fold_convert (type, tem),
13450                                 op2);
13451         }
13452
13453       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
13454       if (integer_onep (arg1)
13455           && truth_value_p (TREE_CODE (arg0))
13456           && truth_value_p (TREE_CODE (op2)))
13457         return fold_build2 (TRUTH_ORIF_EXPR, type,
13458                             fold_convert (type, arg0),
13459                             op2);
13460
13461       return NULL_TREE;
13462
13463     case CALL_EXPR:
13464       /* CALL_EXPRs used to be ternary exprs.  Catch any mistaken uses
13465          of fold_ternary on them.  */
13466       gcc_unreachable ();
13467
13468     case BIT_FIELD_REF:
13469       if ((TREE_CODE (arg0) == VECTOR_CST
13470            || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13471           && type == TREE_TYPE (TREE_TYPE (arg0)))
13472         {
13473           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13474           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13475
13476           if (width != 0
13477               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13478               && (idx % width) == 0
13479               && (idx = idx / width)
13480                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13481             {
13482               tree elements = NULL_TREE;
13483
13484               if (TREE_CODE (arg0) == VECTOR_CST)
13485                 elements = TREE_VECTOR_CST_ELTS (arg0);
13486               else
13487                 {
13488                   unsigned HOST_WIDE_INT idx;
13489                   tree value;
13490
13491                   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13492                     elements = tree_cons (NULL_TREE, value, elements);
13493                 }
13494               while (idx-- > 0 && elements)
13495                 elements = TREE_CHAIN (elements);
13496               if (elements)
13497                 return TREE_VALUE (elements);
13498               else
13499                 return fold_convert (type, integer_zero_node);
13500             }
13501         }
13502
13503       /* A bit-field-ref that referenced the full argument can be stripped.  */
13504       if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13505           && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13506           && integer_zerop (op2))
13507         return fold_convert (type, arg0);
13508
13509       return NULL_TREE;
13510
13511     default:
13512       return NULL_TREE;
13513     } /* switch (code) */
13514 }
13515
13516 /* Perform constant folding and related simplification of EXPR.
13517    The related simplifications include x*1 => x, x*0 => 0, etc.,
13518    and application of the associative law.
13519    NOP_EXPR conversions may be removed freely (as long as we
13520    are careful not to change the type of the overall expression).
13521    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13522    but we can constant-fold them if they have constant operands.  */
13523
13524 #ifdef ENABLE_FOLD_CHECKING
13525 # define fold(x) fold_1 (x)
13526 static tree fold_1 (tree);
13527 static
13528 #endif
13529 tree
13530 fold (tree expr)
13531 {
13532   const tree t = expr;
13533   enum tree_code code = TREE_CODE (t);
13534   enum tree_code_class kind = TREE_CODE_CLASS (code);
13535   tree tem;
13536
13537   /* Return right away if a constant.  */
13538   if (kind == tcc_constant)
13539     return t;
13540
13541   /* CALL_EXPR-like objects with variable numbers of operands are
13542      treated specially.  */
13543   if (kind == tcc_vl_exp)
13544     {
13545       if (code == CALL_EXPR)
13546         {
13547           tem = fold_call_expr (expr, false);
13548           return tem ? tem : expr;
13549         }
13550       return expr;
13551     }
13552
13553   if (IS_EXPR_CODE_CLASS (kind))
13554     {
13555       tree type = TREE_TYPE (t);
13556       tree op0, op1, op2;
13557
13558       switch (TREE_CODE_LENGTH (code))
13559         {
13560         case 1:
13561           op0 = TREE_OPERAND (t, 0);
13562           tem = fold_unary (code, type, op0);
13563           return tem ? tem : expr;
13564         case 2:
13565           op0 = TREE_OPERAND (t, 0);
13566           op1 = TREE_OPERAND (t, 1);
13567           tem = fold_binary (code, type, op0, op1);
13568           return tem ? tem : expr;
13569         case 3:
13570           op0 = TREE_OPERAND (t, 0);
13571           op1 = TREE_OPERAND (t, 1);
13572           op2 = TREE_OPERAND (t, 2);
13573           tem = fold_ternary (code, type, op0, op1, op2);
13574           return tem ? tem : expr;
13575         default:
13576           break;
13577         }
13578     }
13579
13580   switch (code)
13581     {
13582     case ARRAY_REF:
13583       {
13584         tree op0 = TREE_OPERAND (t, 0);
13585         tree op1 = TREE_OPERAND (t, 1);
13586
13587         if (TREE_CODE (op1) == INTEGER_CST
13588             && TREE_CODE (op0) == CONSTRUCTOR
13589             && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13590           {
13591             VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13592             unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13593             unsigned HOST_WIDE_INT begin = 0;
13594
13595             /* Find a matching index by means of a binary search.  */
13596             while (begin != end)
13597               {
13598                 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13599                 tree index = VEC_index (constructor_elt, elts, middle)->index;
13600
13601                 if (TREE_CODE (index) == INTEGER_CST
13602                     && tree_int_cst_lt (index, op1))
13603                   begin = middle + 1;
13604                 else if (TREE_CODE (index) == INTEGER_CST
13605                          && tree_int_cst_lt (op1, index))
13606                   end = middle;
13607                 else if (TREE_CODE (index) == RANGE_EXPR
13608                          && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13609                   begin = middle + 1;
13610                 else if (TREE_CODE (index) == RANGE_EXPR
13611                          && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13612                   end = middle;
13613                 else
13614                   return VEC_index (constructor_elt, elts, middle)->value;
13615               }
13616           }
13617
13618         return t;
13619       }
13620
13621     case CONST_DECL:
13622       return fold (DECL_INITIAL (t));
13623
13624     default:
13625       return t;
13626     } /* switch (code) */
13627 }
13628
13629 #ifdef ENABLE_FOLD_CHECKING
13630 #undef fold
13631
13632 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13633 static void fold_check_failed (const_tree, const_tree);
13634 void print_fold_checksum (const_tree);
13635
13636 /* When --enable-checking=fold, compute a digest of expr before
13637    and after actual fold call to see if fold did not accidentally
13638    change original expr.  */
13639
13640 tree
13641 fold (tree expr)
13642 {
13643   tree ret;
13644   struct md5_ctx ctx;
13645   unsigned char checksum_before[16], checksum_after[16];
13646   htab_t ht;
13647
13648   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13649   md5_init_ctx (&ctx);
13650   fold_checksum_tree (expr, &ctx, ht);
13651   md5_finish_ctx (&ctx, checksum_before);
13652   htab_empty (ht);
13653
13654   ret = fold_1 (expr);
13655
13656   md5_init_ctx (&ctx);
13657   fold_checksum_tree (expr, &ctx, ht);
13658   md5_finish_ctx (&ctx, checksum_after);
13659   htab_delete (ht);
13660
13661   if (memcmp (checksum_before, checksum_after, 16))
13662     fold_check_failed (expr, ret);
13663
13664   return ret;
13665 }
13666
13667 void
13668 print_fold_checksum (const_tree expr)
13669 {
13670   struct md5_ctx ctx;
13671   unsigned char checksum[16], cnt;
13672   htab_t ht;
13673
13674   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13675   md5_init_ctx (&ctx);
13676   fold_checksum_tree (expr, &ctx, ht);
13677   md5_finish_ctx (&ctx, checksum);
13678   htab_delete (ht);
13679   for (cnt = 0; cnt < 16; ++cnt)
13680     fprintf (stderr, "%02x", checksum[cnt]);
13681   putc ('\n', stderr);
13682 }
13683
13684 static void
13685 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13686 {
13687   internal_error ("fold check: original tree changed by fold");
13688 }
13689
13690 static void
13691 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13692 {
13693   const void **slot;
13694   enum tree_code code;
13695   union tree_node buf;
13696   int i, len;
13697   
13698 recursive_label:
13699
13700   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13701                <= sizeof (struct tree_function_decl))
13702               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13703   if (expr == NULL)
13704     return;
13705   slot = (const void **) htab_find_slot (ht, expr, INSERT);
13706   if (*slot != NULL)
13707     return;
13708   *slot = expr;
13709   code = TREE_CODE (expr);
13710   if (TREE_CODE_CLASS (code) == tcc_declaration
13711       && DECL_ASSEMBLER_NAME_SET_P (expr))
13712     {
13713       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
13714       memcpy ((char *) &buf, expr, tree_size (expr));
13715       SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13716       expr = (tree) &buf;
13717     }
13718   else if (TREE_CODE_CLASS (code) == tcc_type
13719            && (TYPE_POINTER_TO (expr)
13720                || TYPE_REFERENCE_TO (expr)
13721                || TYPE_CACHED_VALUES_P (expr)
13722                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13723                || TYPE_NEXT_VARIANT (expr)))
13724     {
13725       /* Allow these fields to be modified.  */
13726       tree tmp;
13727       memcpy ((char *) &buf, expr, tree_size (expr));
13728       expr = tmp = (tree) &buf;
13729       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13730       TYPE_POINTER_TO (tmp) = NULL;
13731       TYPE_REFERENCE_TO (tmp) = NULL;
13732       TYPE_NEXT_VARIANT (tmp) = NULL;
13733       if (TYPE_CACHED_VALUES_P (tmp))
13734         {
13735           TYPE_CACHED_VALUES_P (tmp) = 0;
13736           TYPE_CACHED_VALUES (tmp) = NULL;
13737         }
13738     }
13739   md5_process_bytes (expr, tree_size (expr), ctx);
13740   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13741   if (TREE_CODE_CLASS (code) != tcc_type
13742       && TREE_CODE_CLASS (code) != tcc_declaration
13743       && code != TREE_LIST
13744       && code != SSA_NAME)
13745     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13746   switch (TREE_CODE_CLASS (code))
13747     {
13748     case tcc_constant:
13749       switch (code)
13750         {
13751         case STRING_CST:
13752           md5_process_bytes (TREE_STRING_POINTER (expr),
13753                              TREE_STRING_LENGTH (expr), ctx);
13754           break;
13755         case COMPLEX_CST:
13756           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13757           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13758           break;
13759         case VECTOR_CST:
13760           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13761           break;
13762         default:
13763           break;
13764         }
13765       break;
13766     case tcc_exceptional:
13767       switch (code)
13768         {
13769         case TREE_LIST:
13770           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13771           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13772           expr = TREE_CHAIN (expr);
13773           goto recursive_label;
13774           break;
13775         case TREE_VEC:
13776           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13777             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13778           break;
13779         default:
13780           break;
13781         }
13782       break;
13783     case tcc_expression:
13784     case tcc_reference:
13785     case tcc_comparison:
13786     case tcc_unary:
13787     case tcc_binary:
13788     case tcc_statement:
13789     case tcc_vl_exp:
13790       len = TREE_OPERAND_LENGTH (expr);
13791       for (i = 0; i < len; ++i)
13792         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13793       break;
13794     case tcc_declaration:
13795       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13796       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13797       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13798         {
13799           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13800           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13801           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13802           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13803           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13804         }
13805       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13806         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13807           
13808       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13809         {
13810           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13811           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13812           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13813         }
13814       break;
13815     case tcc_type:
13816       if (TREE_CODE (expr) == ENUMERAL_TYPE)
13817         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13818       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13819       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13820       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13821       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13822       if (INTEGRAL_TYPE_P (expr)
13823           || SCALAR_FLOAT_TYPE_P (expr))
13824         {
13825           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13826           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13827         }
13828       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13829       if (TREE_CODE (expr) == RECORD_TYPE
13830           || TREE_CODE (expr) == UNION_TYPE
13831           || TREE_CODE (expr) == QUAL_UNION_TYPE)
13832         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13833       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13834       break;
13835     default:
13836       break;
13837     }
13838 }
13839
13840 /* Helper function for outputting the checksum of a tree T.  When
13841    debugging with gdb, you can "define mynext" to be "next" followed
13842    by "call debug_fold_checksum (op0)", then just trace down till the
13843    outputs differ.  */
13844
13845 void
13846 debug_fold_checksum (const_tree t)
13847 {
13848   int i;
13849   unsigned char checksum[16];
13850   struct md5_ctx ctx;
13851   htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13852   
13853   md5_init_ctx (&ctx);
13854   fold_checksum_tree (t, &ctx, ht);
13855   md5_finish_ctx (&ctx, checksum);
13856   htab_empty (ht);
13857
13858   for (i = 0; i < 16; i++)
13859     fprintf (stderr, "%d ", checksum[i]);
13860
13861   fprintf (stderr, "\n");
13862 }
13863
13864 #endif
13865
13866 /* Fold a unary tree expression with code CODE of type TYPE with an
13867    operand OP0.  Return a folded expression if successful.  Otherwise,
13868    return a tree expression with code CODE of type TYPE with an
13869    operand OP0.  */
13870
13871 tree
13872 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13873 {
13874   tree tem;
13875 #ifdef ENABLE_FOLD_CHECKING
13876   unsigned char checksum_before[16], checksum_after[16];
13877   struct md5_ctx ctx;
13878   htab_t ht;
13879
13880   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13881   md5_init_ctx (&ctx);
13882   fold_checksum_tree (op0, &ctx, ht);
13883   md5_finish_ctx (&ctx, checksum_before);
13884   htab_empty (ht);
13885 #endif
13886   
13887   tem = fold_unary (code, type, op0);
13888   if (!tem)
13889     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13890   
13891 #ifdef ENABLE_FOLD_CHECKING
13892   md5_init_ctx (&ctx);
13893   fold_checksum_tree (op0, &ctx, ht);
13894   md5_finish_ctx (&ctx, checksum_after);
13895   htab_delete (ht);
13896
13897   if (memcmp (checksum_before, checksum_after, 16))
13898     fold_check_failed (op0, tem);
13899 #endif
13900   return tem;
13901 }
13902
13903 /* Fold a binary tree expression with code CODE of type TYPE with
13904    operands OP0 and OP1.  Return a folded expression if successful.
13905    Otherwise, return a tree expression with code CODE of type TYPE
13906    with operands OP0 and OP1.  */
13907
13908 tree
13909 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13910                   MEM_STAT_DECL)
13911 {
13912   tree tem;
13913 #ifdef ENABLE_FOLD_CHECKING
13914   unsigned char checksum_before_op0[16],
13915                 checksum_before_op1[16],
13916                 checksum_after_op0[16],
13917                 checksum_after_op1[16];
13918   struct md5_ctx ctx;
13919   htab_t ht;
13920
13921   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13922   md5_init_ctx (&ctx);
13923   fold_checksum_tree (op0, &ctx, ht);
13924   md5_finish_ctx (&ctx, checksum_before_op0);
13925   htab_empty (ht);
13926
13927   md5_init_ctx (&ctx);
13928   fold_checksum_tree (op1, &ctx, ht);
13929   md5_finish_ctx (&ctx, checksum_before_op1);
13930   htab_empty (ht);
13931 #endif
13932
13933   tem = fold_binary (code, type, op0, op1);
13934   if (!tem)
13935     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13936   
13937 #ifdef ENABLE_FOLD_CHECKING
13938   md5_init_ctx (&ctx);
13939   fold_checksum_tree (op0, &ctx, ht);
13940   md5_finish_ctx (&ctx, checksum_after_op0);
13941   htab_empty (ht);
13942
13943   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13944     fold_check_failed (op0, tem);
13945   
13946   md5_init_ctx (&ctx);
13947   fold_checksum_tree (op1, &ctx, ht);
13948   md5_finish_ctx (&ctx, checksum_after_op1);
13949   htab_delete (ht);
13950
13951   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13952     fold_check_failed (op1, tem);
13953 #endif
13954   return tem;
13955 }
13956
13957 /* Fold a ternary tree expression with code CODE of type TYPE with
13958    operands OP0, OP1, and OP2.  Return a folded expression if
13959    successful.  Otherwise, return a tree expression with code CODE of
13960    type TYPE with operands OP0, OP1, and OP2.  */
13961
13962 tree
13963 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13964              MEM_STAT_DECL)
13965 {
13966   tree tem;
13967 #ifdef ENABLE_FOLD_CHECKING
13968   unsigned char checksum_before_op0[16],
13969                 checksum_before_op1[16],
13970                 checksum_before_op2[16],
13971                 checksum_after_op0[16],
13972                 checksum_after_op1[16],
13973                 checksum_after_op2[16];
13974   struct md5_ctx ctx;
13975   htab_t ht;
13976
13977   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13978   md5_init_ctx (&ctx);
13979   fold_checksum_tree (op0, &ctx, ht);
13980   md5_finish_ctx (&ctx, checksum_before_op0);
13981   htab_empty (ht);
13982
13983   md5_init_ctx (&ctx);
13984   fold_checksum_tree (op1, &ctx, ht);
13985   md5_finish_ctx (&ctx, checksum_before_op1);
13986   htab_empty (ht);
13987
13988   md5_init_ctx (&ctx);
13989   fold_checksum_tree (op2, &ctx, ht);
13990   md5_finish_ctx (&ctx, checksum_before_op2);
13991   htab_empty (ht);
13992 #endif
13993
13994   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13995   tem = fold_ternary (code, type, op0, op1, op2);
13996   if (!tem)
13997     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13998       
13999 #ifdef ENABLE_FOLD_CHECKING
14000   md5_init_ctx (&ctx);
14001   fold_checksum_tree (op0, &ctx, ht);
14002   md5_finish_ctx (&ctx, checksum_after_op0);
14003   htab_empty (ht);
14004
14005   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14006     fold_check_failed (op0, tem);
14007   
14008   md5_init_ctx (&ctx);
14009   fold_checksum_tree (op1, &ctx, ht);
14010   md5_finish_ctx (&ctx, checksum_after_op1);
14011   htab_empty (ht);
14012
14013   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14014     fold_check_failed (op1, tem);
14015   
14016   md5_init_ctx (&ctx);
14017   fold_checksum_tree (op2, &ctx, ht);
14018   md5_finish_ctx (&ctx, checksum_after_op2);
14019   htab_delete (ht);
14020
14021   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14022     fold_check_failed (op2, tem);
14023 #endif
14024   return tem;
14025 }
14026
14027 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14028    arguments in ARGARRAY, and a null static chain.
14029    Return a folded expression if successful.  Otherwise, return a CALL_EXPR
14030    of type TYPE from the given operands as constructed by build_call_array.  */
14031
14032 tree
14033 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14034 {
14035   tree tem;
14036 #ifdef ENABLE_FOLD_CHECKING
14037   unsigned char checksum_before_fn[16],
14038                 checksum_before_arglist[16],
14039                 checksum_after_fn[16],
14040                 checksum_after_arglist[16];
14041   struct md5_ctx ctx;
14042   htab_t ht;
14043   int i;
14044
14045   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14046   md5_init_ctx (&ctx);
14047   fold_checksum_tree (fn, &ctx, ht);
14048   md5_finish_ctx (&ctx, checksum_before_fn);
14049   htab_empty (ht);
14050
14051   md5_init_ctx (&ctx);
14052   for (i = 0; i < nargs; i++)
14053     fold_checksum_tree (argarray[i], &ctx, ht);
14054   md5_finish_ctx (&ctx, checksum_before_arglist);
14055   htab_empty (ht);
14056 #endif
14057
14058   tem = fold_builtin_call_array (type, fn, nargs, argarray);
14059       
14060 #ifdef ENABLE_FOLD_CHECKING
14061   md5_init_ctx (&ctx);
14062   fold_checksum_tree (fn, &ctx, ht);
14063   md5_finish_ctx (&ctx, checksum_after_fn);
14064   htab_empty (ht);
14065
14066   if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14067     fold_check_failed (fn, tem);
14068   
14069   md5_init_ctx (&ctx);
14070   for (i = 0; i < nargs; i++)
14071     fold_checksum_tree (argarray[i], &ctx, ht);
14072   md5_finish_ctx (&ctx, checksum_after_arglist);
14073   htab_delete (ht);
14074
14075   if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14076     fold_check_failed (NULL_TREE, tem);
14077 #endif
14078   return tem;
14079 }
14080
14081 /* Perform constant folding and related simplification of initializer
14082    expression EXPR.  These behave identically to "fold_buildN" but ignore
14083    potential run-time traps and exceptions that fold must preserve.  */
14084
14085 #define START_FOLD_INIT \
14086   int saved_signaling_nans = flag_signaling_nans;\
14087   int saved_trapping_math = flag_trapping_math;\
14088   int saved_rounding_math = flag_rounding_math;\
14089   int saved_trapv = flag_trapv;\
14090   int saved_folding_initializer = folding_initializer;\
14091   flag_signaling_nans = 0;\
14092   flag_trapping_math = 0;\
14093   flag_rounding_math = 0;\
14094   flag_trapv = 0;\
14095   folding_initializer = 1;
14096
14097 #define END_FOLD_INIT \
14098   flag_signaling_nans = saved_signaling_nans;\
14099   flag_trapping_math = saved_trapping_math;\
14100   flag_rounding_math = saved_rounding_math;\
14101   flag_trapv = saved_trapv;\
14102   folding_initializer = saved_folding_initializer;
14103
14104 tree
14105 fold_build1_initializer (enum tree_code code, tree type, tree op)
14106 {
14107   tree result;
14108   START_FOLD_INIT;
14109
14110   result = fold_build1 (code, type, op);
14111
14112   END_FOLD_INIT;
14113   return result;
14114 }
14115
14116 tree
14117 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14118 {
14119   tree result;
14120   START_FOLD_INIT;
14121
14122   result = fold_build2 (code, type, op0, op1);
14123
14124   END_FOLD_INIT;
14125   return result;
14126 }
14127
14128 tree
14129 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14130                          tree op2)
14131 {
14132   tree result;
14133   START_FOLD_INIT;
14134
14135   result = fold_build3 (code, type, op0, op1, op2);
14136
14137   END_FOLD_INIT;
14138   return result;
14139 }
14140
14141 tree
14142 fold_build_call_array_initializer (tree type, tree fn,
14143                                    int nargs, tree *argarray)
14144 {
14145   tree result;
14146   START_FOLD_INIT;
14147
14148   result = fold_build_call_array (type, fn, nargs, argarray);
14149
14150   END_FOLD_INIT;
14151   return result;
14152 }
14153
14154 #undef START_FOLD_INIT
14155 #undef END_FOLD_INIT
14156
14157 /* Determine if first argument is a multiple of second argument.  Return 0 if
14158    it is not, or we cannot easily determined it to be.
14159
14160    An example of the sort of thing we care about (at this point; this routine
14161    could surely be made more general, and expanded to do what the *_DIV_EXPR's
14162    fold cases do now) is discovering that
14163
14164      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14165
14166    is a multiple of
14167
14168      SAVE_EXPR (J * 8)
14169
14170    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14171
14172    This code also handles discovering that
14173
14174      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14175
14176    is a multiple of 8 so we don't have to worry about dealing with a
14177    possible remainder.
14178
14179    Note that we *look* inside a SAVE_EXPR only to determine how it was
14180    calculated; it is not safe for fold to do much of anything else with the
14181    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14182    at run time.  For example, the latter example above *cannot* be implemented
14183    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14184    evaluation time of the original SAVE_EXPR is not necessarily the same at
14185    the time the new expression is evaluated.  The only optimization of this
14186    sort that would be valid is changing
14187
14188      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14189
14190    divided by 8 to
14191
14192      SAVE_EXPR (I) * SAVE_EXPR (J)
14193
14194    (where the same SAVE_EXPR (J) is used in the original and the
14195    transformed version).  */
14196
14197 int
14198 multiple_of_p (tree type, const_tree top, const_tree bottom)
14199 {
14200   if (operand_equal_p (top, bottom, 0))
14201     return 1;
14202
14203   if (TREE_CODE (type) != INTEGER_TYPE)
14204     return 0;
14205
14206   switch (TREE_CODE (top))
14207     {
14208     case BIT_AND_EXPR:
14209       /* Bitwise and provides a power of two multiple.  If the mask is
14210          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
14211       if (!integer_pow2p (bottom))
14212         return 0;
14213       /* FALLTHRU */
14214
14215     case MULT_EXPR:
14216       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14217               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14218
14219     case PLUS_EXPR:
14220     case MINUS_EXPR:
14221       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14222               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14223
14224     case LSHIFT_EXPR:
14225       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14226         {
14227           tree op1, t1;
14228
14229           op1 = TREE_OPERAND (top, 1);
14230           /* const_binop may not detect overflow correctly,
14231              so check for it explicitly here.  */
14232           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14233               > TREE_INT_CST_LOW (op1)
14234               && TREE_INT_CST_HIGH (op1) == 0
14235               && 0 != (t1 = fold_convert (type,
14236                                           const_binop (LSHIFT_EXPR,
14237                                                        size_one_node,
14238                                                        op1, 0)))
14239               && !TREE_OVERFLOW (t1))
14240             return multiple_of_p (type, t1, bottom);
14241         }
14242       return 0;
14243
14244     case NOP_EXPR:
14245       /* Can't handle conversions from non-integral or wider integral type.  */
14246       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14247           || (TYPE_PRECISION (type)
14248               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14249         return 0;
14250
14251       /* .. fall through ...  */
14252
14253     case SAVE_EXPR:
14254       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14255
14256     case INTEGER_CST:
14257       if (TREE_CODE (bottom) != INTEGER_CST
14258           || integer_zerop (bottom)
14259           || (TYPE_UNSIGNED (type)
14260               && (tree_int_cst_sgn (top) < 0
14261                   || tree_int_cst_sgn (bottom) < 0)))
14262         return 0;
14263       return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14264                                              top, bottom, 0));
14265
14266     default:
14267       return 0;
14268     }
14269 }
14270
14271 /* Return true if CODE or TYPE is known to be non-negative. */
14272
14273 static bool
14274 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14275 {
14276   if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14277       && truth_value_p (code))
14278     /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14279        have a signed:1 type (where the value is -1 and 0).  */
14280     return true;
14281   return false;
14282 }
14283
14284 /* Return true if (CODE OP0) is known to be non-negative.  If the return
14285    value is based on the assumption that signed overflow is undefined,
14286    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14287    *STRICT_OVERFLOW_P.  */
14288
14289 bool
14290 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14291                                 bool *strict_overflow_p)
14292 {
14293   if (TYPE_UNSIGNED (type))
14294     return true;
14295
14296   switch (code)
14297     {
14298     case ABS_EXPR:
14299       /* We can't return 1 if flag_wrapv is set because
14300          ABS_EXPR<INT_MIN> = INT_MIN.  */
14301       if (!INTEGRAL_TYPE_P (type))
14302         return true;
14303       if (TYPE_OVERFLOW_UNDEFINED (type))
14304         {
14305           *strict_overflow_p = true;
14306           return true;
14307         }
14308       break;
14309
14310     case NON_LVALUE_EXPR:
14311     case FLOAT_EXPR:
14312     case FIX_TRUNC_EXPR:
14313       return tree_expr_nonnegative_warnv_p (op0,
14314                                             strict_overflow_p);
14315
14316     case NOP_EXPR:
14317       {
14318         tree inner_type = TREE_TYPE (op0);
14319         tree outer_type = type;
14320
14321         if (TREE_CODE (outer_type) == REAL_TYPE)
14322           {
14323             if (TREE_CODE (inner_type) == REAL_TYPE)
14324               return tree_expr_nonnegative_warnv_p (op0,
14325                                                     strict_overflow_p);
14326             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14327               {
14328                 if (TYPE_UNSIGNED (inner_type))
14329                   return true;
14330                 return tree_expr_nonnegative_warnv_p (op0,
14331                                                       strict_overflow_p);
14332               }
14333           }
14334         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14335           {
14336             if (TREE_CODE (inner_type) == REAL_TYPE)
14337               return tree_expr_nonnegative_warnv_p (op0,
14338                                                     strict_overflow_p);
14339             if (TREE_CODE (inner_type) == INTEGER_TYPE)
14340               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14341                       && TYPE_UNSIGNED (inner_type);
14342           }
14343       }
14344       break;
14345
14346     default:
14347       return tree_simple_nonnegative_warnv_p (code, type);
14348     }
14349
14350   /* We don't know sign of `t', so be conservative and return false.  */
14351   return false;
14352 }
14353
14354 /* Return true if (CODE OP0 OP1) is known to be non-negative.  If the return
14355    value is based on the assumption that signed overflow is undefined,
14356    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14357    *STRICT_OVERFLOW_P.  */
14358
14359 bool
14360 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14361                                       tree op1, bool *strict_overflow_p)
14362 {
14363   if (TYPE_UNSIGNED (type))
14364     return true;
14365
14366   switch (code)
14367     {
14368     case POINTER_PLUS_EXPR:
14369     case PLUS_EXPR:
14370       if (FLOAT_TYPE_P (type))
14371         return (tree_expr_nonnegative_warnv_p (op0,
14372                                                strict_overflow_p)
14373                 && tree_expr_nonnegative_warnv_p (op1,
14374                                                   strict_overflow_p));
14375
14376       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14377          both unsigned and at least 2 bits shorter than the result.  */
14378       if (TREE_CODE (type) == INTEGER_TYPE
14379           && TREE_CODE (op0) == NOP_EXPR
14380           && TREE_CODE (op1) == NOP_EXPR)
14381         {
14382           tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14383           tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14384           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14385               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14386             {
14387               unsigned int prec = MAX (TYPE_PRECISION (inner1),
14388                                        TYPE_PRECISION (inner2)) + 1;
14389               return prec < TYPE_PRECISION (type);
14390             }
14391         }
14392       break;
14393
14394     case MULT_EXPR:
14395       if (FLOAT_TYPE_P (type))
14396         {
14397           /* x * x for floating point x is always non-negative.  */
14398           if (operand_equal_p (op0, op1, 0))
14399             return true;
14400           return (tree_expr_nonnegative_warnv_p (op0,
14401                                                  strict_overflow_p)
14402                   && tree_expr_nonnegative_warnv_p (op1,
14403                                                     strict_overflow_p));
14404         }
14405
14406       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14407          both unsigned and their total bits is shorter than the result.  */
14408       if (TREE_CODE (type) == INTEGER_TYPE
14409           && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14410           && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14411         {
14412           tree inner0 = (TREE_CODE (op0) == NOP_EXPR) 
14413             ? TREE_TYPE (TREE_OPERAND (op0, 0))
14414             : TREE_TYPE (op0);
14415           tree inner1 = (TREE_CODE (op1) == NOP_EXPR) 
14416             ? TREE_TYPE (TREE_OPERAND (op1, 0))
14417             : TREE_TYPE (op1);
14418
14419           bool unsigned0 = TYPE_UNSIGNED (inner0);
14420           bool unsigned1 = TYPE_UNSIGNED (inner1);
14421
14422           if (TREE_CODE (op0) == INTEGER_CST)
14423             unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14424
14425           if (TREE_CODE (op1) == INTEGER_CST)
14426             unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14427
14428           if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14429               && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14430             {
14431               unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14432                 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14433                 : TYPE_PRECISION (inner0);
14434
14435               unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14436                 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14437                 : TYPE_PRECISION (inner1);
14438
14439               return precision0 + precision1 < TYPE_PRECISION (type);
14440             }
14441         }
14442       return false;
14443
14444     case BIT_AND_EXPR:
14445     case MAX_EXPR:
14446       return (tree_expr_nonnegative_warnv_p (op0,
14447                                              strict_overflow_p)
14448               || tree_expr_nonnegative_warnv_p (op1,
14449                                                 strict_overflow_p));
14450
14451     case BIT_IOR_EXPR:
14452     case BIT_XOR_EXPR:
14453     case MIN_EXPR:
14454     case RDIV_EXPR:
14455     case TRUNC_DIV_EXPR:
14456     case CEIL_DIV_EXPR:
14457     case FLOOR_DIV_EXPR:
14458     case ROUND_DIV_EXPR:
14459       return (tree_expr_nonnegative_warnv_p (op0,
14460                                              strict_overflow_p)
14461               && tree_expr_nonnegative_warnv_p (op1,
14462                                                 strict_overflow_p));
14463
14464     case TRUNC_MOD_EXPR:
14465     case CEIL_MOD_EXPR:
14466     case FLOOR_MOD_EXPR:
14467     case ROUND_MOD_EXPR:
14468       return tree_expr_nonnegative_warnv_p (op0,
14469                                             strict_overflow_p);
14470     default:
14471       return tree_simple_nonnegative_warnv_p (code, type);
14472     }
14473
14474   /* We don't know sign of `t', so be conservative and return false.  */
14475   return false;
14476 }
14477
14478 /* Return true if T is known to be non-negative.  If the return
14479    value is based on the assumption that signed overflow is undefined,
14480    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14481    *STRICT_OVERFLOW_P.  */
14482
14483 bool
14484 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14485 {
14486   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14487     return true;
14488
14489   switch (TREE_CODE (t))
14490     {
14491     case INTEGER_CST:
14492       return tree_int_cst_sgn (t) >= 0;
14493
14494     case REAL_CST:
14495       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14496
14497     case FIXED_CST:
14498       return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14499
14500     case COND_EXPR:
14501       return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14502                                              strict_overflow_p)
14503               && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14504                                                 strict_overflow_p));
14505     default:
14506       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14507                                                    TREE_TYPE (t));
14508     }
14509   /* We don't know sign of `t', so be conservative and return false.  */
14510   return false;
14511 }
14512
14513 /* Return true if T is known to be non-negative.  If the return
14514    value is based on the assumption that signed overflow is undefined,
14515    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14516    *STRICT_OVERFLOW_P.  */
14517
14518 bool
14519 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14520                                tree arg0, tree arg1, bool *strict_overflow_p)
14521 {
14522   if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14523     switch (DECL_FUNCTION_CODE (fndecl))
14524       {
14525         CASE_FLT_FN (BUILT_IN_ACOS):
14526         CASE_FLT_FN (BUILT_IN_ACOSH):
14527         CASE_FLT_FN (BUILT_IN_CABS):
14528         CASE_FLT_FN (BUILT_IN_COSH):
14529         CASE_FLT_FN (BUILT_IN_ERFC):
14530         CASE_FLT_FN (BUILT_IN_EXP):
14531         CASE_FLT_FN (BUILT_IN_EXP10):
14532         CASE_FLT_FN (BUILT_IN_EXP2):
14533         CASE_FLT_FN (BUILT_IN_FABS):
14534         CASE_FLT_FN (BUILT_IN_FDIM):
14535         CASE_FLT_FN (BUILT_IN_HYPOT):
14536         CASE_FLT_FN (BUILT_IN_POW10):
14537         CASE_INT_FN (BUILT_IN_FFS):
14538         CASE_INT_FN (BUILT_IN_PARITY):
14539         CASE_INT_FN (BUILT_IN_POPCOUNT):
14540       case BUILT_IN_BSWAP32:
14541       case BUILT_IN_BSWAP64:
14542         /* Always true.  */
14543         return true;
14544
14545         CASE_FLT_FN (BUILT_IN_SQRT):
14546         /* sqrt(-0.0) is -0.0.  */
14547         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14548           return true;
14549         return tree_expr_nonnegative_warnv_p (arg0,
14550                                               strict_overflow_p);
14551
14552         CASE_FLT_FN (BUILT_IN_ASINH):
14553         CASE_FLT_FN (BUILT_IN_ATAN):
14554         CASE_FLT_FN (BUILT_IN_ATANH):
14555         CASE_FLT_FN (BUILT_IN_CBRT):
14556         CASE_FLT_FN (BUILT_IN_CEIL):
14557         CASE_FLT_FN (BUILT_IN_ERF):
14558         CASE_FLT_FN (BUILT_IN_EXPM1):
14559         CASE_FLT_FN (BUILT_IN_FLOOR):
14560         CASE_FLT_FN (BUILT_IN_FMOD):
14561         CASE_FLT_FN (BUILT_IN_FREXP):
14562         CASE_FLT_FN (BUILT_IN_LCEIL):
14563         CASE_FLT_FN (BUILT_IN_LDEXP):
14564         CASE_FLT_FN (BUILT_IN_LFLOOR):
14565         CASE_FLT_FN (BUILT_IN_LLCEIL):
14566         CASE_FLT_FN (BUILT_IN_LLFLOOR):
14567         CASE_FLT_FN (BUILT_IN_LLRINT):
14568         CASE_FLT_FN (BUILT_IN_LLROUND):
14569         CASE_FLT_FN (BUILT_IN_LRINT):
14570         CASE_FLT_FN (BUILT_IN_LROUND):
14571         CASE_FLT_FN (BUILT_IN_MODF):
14572         CASE_FLT_FN (BUILT_IN_NEARBYINT):
14573         CASE_FLT_FN (BUILT_IN_RINT):
14574         CASE_FLT_FN (BUILT_IN_ROUND):
14575         CASE_FLT_FN (BUILT_IN_SCALB):
14576         CASE_FLT_FN (BUILT_IN_SCALBLN):
14577         CASE_FLT_FN (BUILT_IN_SCALBN):
14578         CASE_FLT_FN (BUILT_IN_SIGNBIT):
14579         CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14580         CASE_FLT_FN (BUILT_IN_SINH):
14581         CASE_FLT_FN (BUILT_IN_TANH):
14582         CASE_FLT_FN (BUILT_IN_TRUNC):
14583         /* True if the 1st argument is nonnegative.  */
14584         return tree_expr_nonnegative_warnv_p (arg0,
14585                                               strict_overflow_p);
14586
14587         CASE_FLT_FN (BUILT_IN_FMAX):
14588         /* True if the 1st OR 2nd arguments are nonnegative.  */
14589         return (tree_expr_nonnegative_warnv_p (arg0,
14590                                                strict_overflow_p)
14591                 || (tree_expr_nonnegative_warnv_p (arg1,
14592                                                    strict_overflow_p)));
14593
14594         CASE_FLT_FN (BUILT_IN_FMIN):
14595         /* True if the 1st AND 2nd arguments are nonnegative.  */
14596         return (tree_expr_nonnegative_warnv_p (arg0,
14597                                                strict_overflow_p)
14598                 && (tree_expr_nonnegative_warnv_p (arg1,
14599                                                    strict_overflow_p)));
14600
14601         CASE_FLT_FN (BUILT_IN_COPYSIGN):
14602         /* True if the 2nd argument is nonnegative.  */
14603         return tree_expr_nonnegative_warnv_p (arg1,
14604                                               strict_overflow_p);
14605
14606         CASE_FLT_FN (BUILT_IN_POWI):
14607         /* True if the 1st argument is nonnegative or the second
14608            argument is an even integer.  */
14609         if (TREE_CODE (arg1) == INTEGER_CST
14610             && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14611           return true;
14612         return tree_expr_nonnegative_warnv_p (arg0,
14613                                               strict_overflow_p);
14614
14615         CASE_FLT_FN (BUILT_IN_POW):
14616         /* True if the 1st argument is nonnegative or the second
14617            argument is an even integer valued real.  */
14618         if (TREE_CODE (arg1) == REAL_CST)
14619           {
14620             REAL_VALUE_TYPE c;
14621             HOST_WIDE_INT n;
14622
14623             c = TREE_REAL_CST (arg1);
14624             n = real_to_integer (&c);
14625             if ((n & 1) == 0)
14626               {
14627                 REAL_VALUE_TYPE cint;
14628                 real_from_integer (&cint, VOIDmode, n,
14629                                    n < 0 ? -1 : 0, 0);
14630                 if (real_identical (&c, &cint))
14631                   return true;
14632               }
14633           }
14634         return tree_expr_nonnegative_warnv_p (arg0,
14635                                               strict_overflow_p);
14636
14637       default:
14638         break;
14639       }
14640   return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14641                                           type);
14642 }
14643
14644 /* Return true if T is known to be non-negative.  If the return
14645    value is based on the assumption that signed overflow is undefined,
14646    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14647    *STRICT_OVERFLOW_P.  */
14648
14649 bool
14650 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14651 {
14652   enum tree_code code = TREE_CODE (t);
14653   if (TYPE_UNSIGNED (TREE_TYPE (t)))
14654     return true;
14655
14656   switch (code)
14657     {
14658     case TARGET_EXPR:
14659       {
14660         tree temp = TARGET_EXPR_SLOT (t);
14661         t = TARGET_EXPR_INITIAL (t);
14662
14663         /* If the initializer is non-void, then it's a normal expression
14664            that will be assigned to the slot.  */
14665         if (!VOID_TYPE_P (t))
14666           return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14667
14668         /* Otherwise, the initializer sets the slot in some way.  One common
14669            way is an assignment statement at the end of the initializer.  */
14670         while (1)
14671           {
14672             if (TREE_CODE (t) == BIND_EXPR)
14673               t = expr_last (BIND_EXPR_BODY (t));
14674             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14675                      || TREE_CODE (t) == TRY_CATCH_EXPR)
14676               t = expr_last (TREE_OPERAND (t, 0));
14677             else if (TREE_CODE (t) == STATEMENT_LIST)
14678               t = expr_last (t);
14679             else
14680               break;
14681           }
14682         if (TREE_CODE (t) == MODIFY_EXPR
14683             && TREE_OPERAND (t, 0) == temp)
14684           return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14685                                                 strict_overflow_p);
14686
14687         return false;
14688       }
14689
14690     case CALL_EXPR:
14691       {
14692         tree arg0 = call_expr_nargs (t) > 0 ?  CALL_EXPR_ARG (t, 0) : NULL_TREE;
14693         tree arg1 = call_expr_nargs (t) > 1 ?  CALL_EXPR_ARG (t, 1) : NULL_TREE;
14694
14695         return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14696                                               get_callee_fndecl (t),
14697                                               arg0,
14698                                               arg1,
14699                                               strict_overflow_p);
14700       }
14701     case COMPOUND_EXPR:
14702     case MODIFY_EXPR:
14703       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14704                                             strict_overflow_p);
14705     case BIND_EXPR:
14706       return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14707                                             strict_overflow_p);
14708     case SAVE_EXPR:
14709       return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14710                                             strict_overflow_p);
14711
14712     default:
14713       return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14714                                                    TREE_TYPE (t));
14715     }
14716
14717   /* We don't know sign of `t', so be conservative and return false.  */
14718   return false;
14719 }
14720
14721 /* Return true if T is known to be non-negative.  If the return
14722    value is based on the assumption that signed overflow is undefined,
14723    set *STRICT_OVERFLOW_P to true; otherwise, don't change
14724    *STRICT_OVERFLOW_P.  */
14725
14726 bool
14727 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14728 {
14729   enum tree_code code;
14730   if (t == error_mark_node)
14731     return false;
14732
14733   code = TREE_CODE (t);
14734   switch (TREE_CODE_CLASS (code))
14735     {
14736     case tcc_binary:
14737     case tcc_comparison:
14738       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14739                                               TREE_TYPE (t),
14740                                               TREE_OPERAND (t, 0),
14741                                               TREE_OPERAND (t, 1),
14742                                               strict_overflow_p);
14743
14744     case tcc_unary:
14745       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14746                                              TREE_TYPE (t),
14747                                              TREE_OPERAND (t, 0),
14748                                              strict_overflow_p);
14749
14750     case tcc_constant:
14751     case tcc_declaration:
14752     case tcc_reference:
14753       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14754
14755     default:
14756       break;
14757     }
14758
14759   switch (code)
14760     {
14761     case TRUTH_AND_EXPR:
14762     case TRUTH_OR_EXPR:
14763     case TRUTH_XOR_EXPR:
14764       return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14765                                               TREE_TYPE (t),
14766                                               TREE_OPERAND (t, 0),
14767                                               TREE_OPERAND (t, 1),
14768                                               strict_overflow_p);
14769     case TRUTH_NOT_EXPR:
14770       return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14771                                              TREE_TYPE (t),
14772                                              TREE_OPERAND (t, 0),
14773                                              strict_overflow_p);
14774
14775     case COND_EXPR:
14776     case CONSTRUCTOR:
14777     case OBJ_TYPE_REF:
14778     case ASSERT_EXPR:
14779     case ADDR_EXPR:
14780     case WITH_SIZE_EXPR:
14781     case EXC_PTR_EXPR:
14782     case SSA_NAME:
14783     case FILTER_EXPR:
14784       return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14785
14786     default:
14787       return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14788     }
14789 }
14790
14791 /* Return true if `t' is known to be non-negative.  Handle warnings
14792    about undefined signed overflow.  */
14793
14794 bool
14795 tree_expr_nonnegative_p (tree t)
14796 {
14797   bool ret, strict_overflow_p;
14798
14799   strict_overflow_p = false;
14800   ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14801   if (strict_overflow_p)
14802     fold_overflow_warning (("assuming signed overflow does not occur when "
14803                             "determining that expression is always "
14804                             "non-negative"),
14805                            WARN_STRICT_OVERFLOW_MISC);
14806   return ret;
14807 }
14808
14809
14810 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14811    For floating point we further ensure that T is not denormal.
14812    Similar logic is present in nonzero_address in rtlanal.h.
14813
14814    If the return value is based on the assumption that signed overflow
14815    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14816    change *STRICT_OVERFLOW_P.  */
14817
14818 bool
14819 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14820                                  bool *strict_overflow_p)
14821 {
14822   switch (code)
14823     {
14824     case ABS_EXPR:
14825       return tree_expr_nonzero_warnv_p (op0,
14826                                         strict_overflow_p);
14827
14828     case NOP_EXPR:
14829       {
14830         tree inner_type = TREE_TYPE (op0);
14831         tree outer_type = type;
14832
14833         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14834                 && tree_expr_nonzero_warnv_p (op0,
14835                                               strict_overflow_p));
14836       }
14837       break;
14838
14839     case NON_LVALUE_EXPR:
14840       return tree_expr_nonzero_warnv_p (op0,
14841                                         strict_overflow_p);
14842
14843     default:
14844       break;
14845   }
14846
14847   return false;
14848 }
14849
14850 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14851    For floating point we further ensure that T is not denormal.
14852    Similar logic is present in nonzero_address in rtlanal.h.
14853
14854    If the return value is based on the assumption that signed overflow
14855    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14856    change *STRICT_OVERFLOW_P.  */
14857
14858 bool
14859 tree_binary_nonzero_warnv_p (enum tree_code code,
14860                              tree type,
14861                              tree op0,
14862                              tree op1, bool *strict_overflow_p)
14863 {
14864   bool sub_strict_overflow_p;
14865   switch (code)
14866     {
14867     case POINTER_PLUS_EXPR:
14868     case PLUS_EXPR:
14869       if (TYPE_OVERFLOW_UNDEFINED (type))
14870         {
14871           /* With the presence of negative values it is hard
14872              to say something.  */
14873           sub_strict_overflow_p = false;
14874           if (!tree_expr_nonnegative_warnv_p (op0,
14875                                               &sub_strict_overflow_p)
14876               || !tree_expr_nonnegative_warnv_p (op1,
14877                                                  &sub_strict_overflow_p))
14878             return false;
14879           /* One of operands must be positive and the other non-negative.  */
14880           /* We don't set *STRICT_OVERFLOW_P here: even if this value
14881              overflows, on a twos-complement machine the sum of two
14882              nonnegative numbers can never be zero.  */
14883           return (tree_expr_nonzero_warnv_p (op0,
14884                                              strict_overflow_p)
14885                   || tree_expr_nonzero_warnv_p (op1,
14886                                                 strict_overflow_p));
14887         }
14888       break;
14889
14890     case MULT_EXPR:
14891       if (TYPE_OVERFLOW_UNDEFINED (type))
14892         {
14893           if (tree_expr_nonzero_warnv_p (op0,
14894                                          strict_overflow_p)
14895               && tree_expr_nonzero_warnv_p (op1,
14896                                             strict_overflow_p))
14897             {
14898               *strict_overflow_p = true;
14899               return true;
14900             }
14901         }
14902       break;
14903
14904     case MIN_EXPR:
14905       sub_strict_overflow_p = false;
14906       if (tree_expr_nonzero_warnv_p (op0,
14907                                      &sub_strict_overflow_p)
14908           && tree_expr_nonzero_warnv_p (op1,
14909                                         &sub_strict_overflow_p))
14910         {
14911           if (sub_strict_overflow_p)
14912             *strict_overflow_p = true;
14913         }
14914       break;
14915
14916     case MAX_EXPR:
14917       sub_strict_overflow_p = false;
14918       if (tree_expr_nonzero_warnv_p (op0,
14919                                      &sub_strict_overflow_p))
14920         {
14921           if (sub_strict_overflow_p)
14922             *strict_overflow_p = true;
14923
14924           /* When both operands are nonzero, then MAX must be too.  */
14925           if (tree_expr_nonzero_warnv_p (op1,
14926                                          strict_overflow_p))
14927             return true;
14928
14929           /* MAX where operand 0 is positive is positive.  */
14930           return tree_expr_nonnegative_warnv_p (op0,
14931                                                strict_overflow_p);
14932         }
14933       /* MAX where operand 1 is positive is positive.  */
14934       else if (tree_expr_nonzero_warnv_p (op1,
14935                                           &sub_strict_overflow_p)
14936                && tree_expr_nonnegative_warnv_p (op1,
14937                                                  &sub_strict_overflow_p))
14938         {
14939           if (sub_strict_overflow_p)
14940             *strict_overflow_p = true;
14941           return true;
14942         }
14943       break;
14944
14945     case BIT_IOR_EXPR:
14946       return (tree_expr_nonzero_warnv_p (op1,
14947                                          strict_overflow_p)
14948               || tree_expr_nonzero_warnv_p (op0,
14949                                             strict_overflow_p));
14950
14951     default:
14952       break;
14953   }
14954
14955   return false;
14956 }
14957
14958 /* Return true when T is an address and is known to be nonzero.
14959    For floating point we further ensure that T is not denormal.
14960    Similar logic is present in nonzero_address in rtlanal.h.
14961
14962    If the return value is based on the assumption that signed overflow
14963    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14964    change *STRICT_OVERFLOW_P.  */
14965
14966 bool
14967 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14968 {
14969   bool sub_strict_overflow_p;
14970   switch (TREE_CODE (t))
14971     {
14972     case INTEGER_CST:
14973       return !integer_zerop (t);
14974
14975     case ADDR_EXPR:
14976       {
14977         tree base = get_base_address (TREE_OPERAND (t, 0));
14978
14979         if (!base)
14980           return false;
14981
14982         /* Weak declarations may link to NULL.  */
14983         if (VAR_OR_FUNCTION_DECL_P (base))
14984           return !DECL_WEAK (base);
14985
14986         /* Constants are never weak.  */
14987         if (CONSTANT_CLASS_P (base))
14988           return true;
14989
14990         return false;
14991       }
14992
14993     case COND_EXPR:
14994       sub_strict_overflow_p = false;
14995       if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14996                                      &sub_strict_overflow_p)
14997           && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14998                                         &sub_strict_overflow_p))
14999         {
15000           if (sub_strict_overflow_p)
15001             *strict_overflow_p = true;
15002           return true;
15003         }
15004       break;
15005
15006     default:
15007       break;
15008     }
15009   return false;
15010 }
15011
15012 /* Return true when T is an address and is known to be nonzero.
15013    For floating point we further ensure that T is not denormal.
15014    Similar logic is present in nonzero_address in rtlanal.h.
15015
15016    If the return value is based on the assumption that signed overflow
15017    is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15018    change *STRICT_OVERFLOW_P.  */
15019
15020 bool
15021 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15022 {
15023   tree type = TREE_TYPE (t);
15024   enum tree_code code;
15025
15026   /* Doing something useful for floating point would need more work.  */
15027   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15028     return false;
15029
15030   code = TREE_CODE (t);
15031   switch (TREE_CODE_CLASS (code))
15032     {
15033     case tcc_unary:
15034       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15035                                               strict_overflow_p);
15036     case tcc_binary:
15037     case tcc_comparison:
15038       return tree_binary_nonzero_warnv_p (code, type,
15039                                                TREE_OPERAND (t, 0),
15040                                                TREE_OPERAND (t, 1),
15041                                                strict_overflow_p);
15042     case tcc_constant:
15043     case tcc_declaration:
15044     case tcc_reference:
15045       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15046
15047     default:
15048       break;
15049     }
15050
15051   switch (code)
15052     {
15053     case TRUTH_NOT_EXPR:
15054       return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15055                                               strict_overflow_p);
15056
15057     case TRUTH_AND_EXPR:
15058     case TRUTH_OR_EXPR:
15059     case TRUTH_XOR_EXPR:
15060       return tree_binary_nonzero_warnv_p (code, type,
15061                                                TREE_OPERAND (t, 0),
15062                                                TREE_OPERAND (t, 1),
15063                                                strict_overflow_p);
15064
15065     case COND_EXPR:
15066     case CONSTRUCTOR:
15067     case OBJ_TYPE_REF:
15068     case ASSERT_EXPR:
15069     case ADDR_EXPR:
15070     case WITH_SIZE_EXPR:
15071     case EXC_PTR_EXPR:
15072     case SSA_NAME:
15073     case FILTER_EXPR:
15074       return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15075
15076     case COMPOUND_EXPR:
15077     case MODIFY_EXPR:
15078     case BIND_EXPR:
15079       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15080                                         strict_overflow_p);
15081
15082     case SAVE_EXPR:
15083       return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15084                                         strict_overflow_p);
15085
15086     case CALL_EXPR:
15087       return alloca_call_p (t);
15088
15089     default:
15090       break;
15091     }
15092   return false;
15093 }
15094
15095 /* Return true when T is an address and is known to be nonzero.
15096    Handle warnings about undefined signed overflow.  */
15097
15098 bool
15099 tree_expr_nonzero_p (tree t)
15100 {
15101   bool ret, strict_overflow_p;
15102
15103   strict_overflow_p = false;
15104   ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15105   if (strict_overflow_p)
15106     fold_overflow_warning (("assuming signed overflow does not occur when "
15107                             "determining that expression is always "
15108                             "non-zero"),
15109                            WARN_STRICT_OVERFLOW_MISC);
15110   return ret;
15111 }
15112
15113 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15114    attempt to fold the expression to a constant without modifying TYPE,
15115    OP0 or OP1.
15116
15117    If the expression could be simplified to a constant, then return
15118    the constant.  If the expression would not be simplified to a
15119    constant, then return NULL_TREE.  */
15120
15121 tree
15122 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15123 {
15124   tree tem = fold_binary (code, type, op0, op1);
15125   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15126 }
15127
15128 /* Given the components of a unary expression CODE, TYPE and OP0,
15129    attempt to fold the expression to a constant without modifying
15130    TYPE or OP0.
15131
15132    If the expression could be simplified to a constant, then return
15133    the constant.  If the expression would not be simplified to a
15134    constant, then return NULL_TREE.  */
15135
15136 tree
15137 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15138 {
15139   tree tem = fold_unary (code, type, op0);
15140   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15141 }
15142
15143 /* If EXP represents referencing an element in a constant string
15144    (either via pointer arithmetic or array indexing), return the
15145    tree representing the value accessed, otherwise return NULL.  */
15146
15147 tree
15148 fold_read_from_constant_string (tree exp)
15149 {
15150   if ((TREE_CODE (exp) == INDIRECT_REF
15151        || TREE_CODE (exp) == ARRAY_REF)
15152       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15153     {
15154       tree exp1 = TREE_OPERAND (exp, 0);
15155       tree index;
15156       tree string;
15157
15158       if (TREE_CODE (exp) == INDIRECT_REF)
15159         string = string_constant (exp1, &index);
15160       else
15161         {
15162           tree low_bound = array_ref_low_bound (exp);
15163           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15164
15165           /* Optimize the special-case of a zero lower bound.
15166
15167              We convert the low_bound to sizetype to avoid some problems
15168              with constant folding.  (E.g. suppose the lower bound is 1,
15169              and its mode is QI.  Without the conversion,l (ARRAY
15170              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15171              +INDEX), which becomes (ARRAY+255+INDEX).  Oops!)  */
15172           if (! integer_zerop (low_bound))
15173             index = size_diffop (index, fold_convert (sizetype, low_bound));
15174
15175           string = exp1;
15176         }
15177
15178       if (string
15179           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15180           && TREE_CODE (string) == STRING_CST
15181           && TREE_CODE (index) == INTEGER_CST
15182           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15183           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15184               == MODE_INT)
15185           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15186         return build_int_cst_type (TREE_TYPE (exp),
15187                                    (TREE_STRING_POINTER (string)
15188                                     [TREE_INT_CST_LOW (index)]));
15189     }
15190   return NULL;
15191 }
15192
15193 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15194    an integer constant, real, or fixed-point constant.
15195
15196    TYPE is the type of the result.  */
15197
15198 static tree
15199 fold_negate_const (tree arg0, tree type)
15200 {
15201   tree t = NULL_TREE;
15202
15203   switch (TREE_CODE (arg0))
15204     {
15205     case INTEGER_CST:
15206       {
15207         unsigned HOST_WIDE_INT low;
15208         HOST_WIDE_INT high;
15209         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15210                                    TREE_INT_CST_HIGH (arg0),
15211                                    &low, &high);
15212         t = force_fit_type_double (type, low, high, 1,
15213                                    (overflow | TREE_OVERFLOW (arg0))
15214                                    && !TYPE_UNSIGNED (type));
15215         break;
15216       }
15217
15218     case REAL_CST:
15219       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15220       break;
15221
15222     case FIXED_CST:
15223       {
15224         FIXED_VALUE_TYPE f;
15225         bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15226                                             &(TREE_FIXED_CST (arg0)), NULL,
15227                                             TYPE_SATURATING (type));
15228         t = build_fixed (type, f);
15229         /* Propagate overflow flags.  */
15230         if (overflow_p | TREE_OVERFLOW (arg0))
15231           {
15232             TREE_OVERFLOW (t) = 1;
15233             TREE_CONSTANT_OVERFLOW (t) = 1;
15234           }
15235         else if (TREE_CONSTANT_OVERFLOW (arg0))
15236           TREE_CONSTANT_OVERFLOW (t) = 1;
15237         break;
15238       }
15239
15240     default:
15241       gcc_unreachable ();
15242     }
15243
15244   return t;
15245 }
15246
15247 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15248    an integer constant or real constant.
15249
15250    TYPE is the type of the result.  */
15251
15252 tree
15253 fold_abs_const (tree arg0, tree type)
15254 {
15255   tree t = NULL_TREE;
15256
15257   switch (TREE_CODE (arg0))
15258     {
15259     case INTEGER_CST:
15260       /* If the value is unsigned, then the absolute value is
15261          the same as the ordinary value.  */
15262       if (TYPE_UNSIGNED (type))
15263         t = arg0;
15264       /* Similarly, if the value is non-negative.  */
15265       else if (INT_CST_LT (integer_minus_one_node, arg0))
15266         t = arg0;
15267       /* If the value is negative, then the absolute value is
15268          its negation.  */
15269       else
15270         {
15271           unsigned HOST_WIDE_INT low;
15272           HOST_WIDE_INT high;
15273           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15274                                      TREE_INT_CST_HIGH (arg0),
15275                                      &low, &high);
15276           t = force_fit_type_double (type, low, high, -1,
15277                                      overflow | TREE_OVERFLOW (arg0));
15278         }
15279       break;
15280
15281     case REAL_CST:
15282       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15283         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15284       else
15285         t =  arg0;
15286       break;
15287
15288     default:
15289       gcc_unreachable ();
15290     }
15291
15292   return t;
15293 }
15294
15295 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15296    constant.  TYPE is the type of the result.  */
15297
15298 static tree
15299 fold_not_const (tree arg0, tree type)
15300 {
15301   tree t = NULL_TREE;
15302
15303   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15304
15305   t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15306                              ~TREE_INT_CST_HIGH (arg0), 0,
15307                              TREE_OVERFLOW (arg0));
15308
15309   return t;
15310 }
15311
15312 /* Given CODE, a relational operator, the target type, TYPE and two
15313    constant operands OP0 and OP1, return the result of the
15314    relational operation.  If the result is not a compile time
15315    constant, then return NULL_TREE.  */
15316
15317 static tree
15318 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15319 {
15320   int result, invert;
15321
15322   /* From here on, the only cases we handle are when the result is
15323      known to be a constant.  */
15324
15325   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15326     {
15327       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15328       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15329
15330       /* Handle the cases where either operand is a NaN.  */
15331       if (real_isnan (c0) || real_isnan (c1))
15332         {
15333           switch (code)
15334             {
15335             case EQ_EXPR:
15336             case ORDERED_EXPR:
15337               result = 0;
15338               break;
15339
15340             case NE_EXPR:
15341             case UNORDERED_EXPR:
15342             case UNLT_EXPR:
15343             case UNLE_EXPR:
15344             case UNGT_EXPR:
15345             case UNGE_EXPR:
15346             case UNEQ_EXPR:
15347               result = 1;
15348               break;
15349
15350             case LT_EXPR:
15351             case LE_EXPR:
15352             case GT_EXPR:
15353             case GE_EXPR:
15354             case LTGT_EXPR:
15355               if (flag_trapping_math)
15356                 return NULL_TREE;
15357               result = 0;
15358               break;
15359
15360             default:
15361               gcc_unreachable ();
15362             }
15363
15364           return constant_boolean_node (result, type);
15365         }
15366
15367       return constant_boolean_node (real_compare (code, c0, c1), type);
15368     }
15369
15370   if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15371     {
15372       const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15373       const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15374       return constant_boolean_node (fixed_compare (code, c0, c1), type);
15375     }
15376
15377   /* Handle equality/inequality of complex constants.  */
15378   if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15379     {
15380       tree rcond = fold_relational_const (code, type,
15381                                           TREE_REALPART (op0),
15382                                           TREE_REALPART (op1));
15383       tree icond = fold_relational_const (code, type,
15384                                           TREE_IMAGPART (op0),
15385                                           TREE_IMAGPART (op1));
15386       if (code == EQ_EXPR)
15387         return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15388       else if (code == NE_EXPR)
15389         return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15390       else
15391         return NULL_TREE;
15392     }
15393
15394   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15395
15396      To compute GT, swap the arguments and do LT.
15397      To compute GE, do LT and invert the result.
15398      To compute LE, swap the arguments, do LT and invert the result.
15399      To compute NE, do EQ and invert the result.
15400
15401      Therefore, the code below must handle only EQ and LT.  */
15402
15403   if (code == LE_EXPR || code == GT_EXPR)
15404     {
15405       tree tem = op0;
15406       op0 = op1;
15407       op1 = tem;
15408       code = swap_tree_comparison (code);
15409     }
15410
15411   /* Note that it is safe to invert for real values here because we
15412      have already handled the one case that it matters.  */
15413
15414   invert = 0;
15415   if (code == NE_EXPR || code == GE_EXPR)
15416     {
15417       invert = 1;
15418       code = invert_tree_comparison (code, false);
15419     }
15420
15421   /* Compute a result for LT or EQ if args permit;
15422      Otherwise return T.  */
15423   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15424     {
15425       if (code == EQ_EXPR)
15426         result = tree_int_cst_equal (op0, op1);
15427       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15428         result = INT_CST_LT_UNSIGNED (op0, op1);
15429       else
15430         result = INT_CST_LT (op0, op1);
15431     }
15432   else
15433     return NULL_TREE;
15434
15435   if (invert)
15436     result ^= 1;
15437   return constant_boolean_node (result, type);
15438 }
15439
15440 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15441    indicated TYPE.  If no CLEANUP_POINT_EXPR is necessary, return EXPR
15442    itself.  */
15443
15444 tree
15445 fold_build_cleanup_point_expr (tree type, tree expr)
15446 {
15447   /* If the expression does not have side effects then we don't have to wrap
15448      it with a cleanup point expression.  */
15449   if (!TREE_SIDE_EFFECTS (expr))
15450     return expr;
15451
15452   /* If the expression is a return, check to see if the expression inside the
15453      return has no side effects or the right hand side of the modify expression
15454      inside the return. If either don't have side effects set we don't need to
15455      wrap the expression in a cleanup point expression.  Note we don't check the
15456      left hand side of the modify because it should always be a return decl.  */
15457   if (TREE_CODE (expr) == RETURN_EXPR)
15458     {
15459       tree op = TREE_OPERAND (expr, 0);
15460       if (!op || !TREE_SIDE_EFFECTS (op))
15461         return expr;
15462       op = TREE_OPERAND (op, 1);
15463       if (!TREE_SIDE_EFFECTS (op))
15464         return expr;
15465     }
15466   
15467   return build1 (CLEANUP_POINT_EXPR, type, expr);
15468 }
15469
15470 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15471    of an indirection through OP0, or NULL_TREE if no simplification is
15472    possible.  */
15473
15474 tree
15475 fold_indirect_ref_1 (tree type, tree op0)
15476 {
15477   tree sub = op0;
15478   tree subtype;
15479
15480   STRIP_NOPS (sub);
15481   subtype = TREE_TYPE (sub);
15482   if (!POINTER_TYPE_P (subtype))
15483     return NULL_TREE;
15484
15485   if (TREE_CODE (sub) == ADDR_EXPR)
15486     {
15487       tree op = TREE_OPERAND (sub, 0);
15488       tree optype = TREE_TYPE (op);
15489       /* *&CONST_DECL -> to the value of the const decl.  */
15490       if (TREE_CODE (op) == CONST_DECL)
15491         return DECL_INITIAL (op);
15492       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
15493       if (type == optype)
15494         {
15495           tree fop = fold_read_from_constant_string (op);
15496           if (fop)
15497             return fop;
15498           else
15499             return op;
15500         }
15501       /* *(foo *)&fooarray => fooarray[0] */
15502       else if (TREE_CODE (optype) == ARRAY_TYPE
15503                && type == TREE_TYPE (optype))
15504         {
15505           tree type_domain = TYPE_DOMAIN (optype);
15506           tree min_val = size_zero_node;
15507           if (type_domain && TYPE_MIN_VALUE (type_domain))
15508             min_val = TYPE_MIN_VALUE (type_domain);
15509           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15510         }
15511       /* *(foo *)&complexfoo => __real__ complexfoo */
15512       else if (TREE_CODE (optype) == COMPLEX_TYPE
15513                && type == TREE_TYPE (optype))
15514         return fold_build1 (REALPART_EXPR, type, op);
15515       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15516       else if (TREE_CODE (optype) == VECTOR_TYPE
15517                && type == TREE_TYPE (optype))
15518         {
15519           tree part_width = TYPE_SIZE (type);
15520           tree index = bitsize_int (0);
15521           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15522         }
15523     }
15524
15525   /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15526   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15527       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15528     { 
15529       tree op00 = TREE_OPERAND (sub, 0);
15530       tree op01 = TREE_OPERAND (sub, 1);
15531       tree op00type;
15532       
15533       STRIP_NOPS (op00);
15534       op00type = TREE_TYPE (op00);
15535       if (TREE_CODE (op00) == ADDR_EXPR
15536           && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15537           && type == TREE_TYPE (TREE_TYPE (op00type)))
15538         { 
15539           HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15540           tree part_width = TYPE_SIZE (type);
15541           unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15542           unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15543           tree index = bitsize_int (indexi);
15544
15545           if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15546             return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15547                                 part_width, index);
15548         
15549         }
15550     }
15551
15552
15553   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15554   if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15555       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15556     {
15557       tree op00 = TREE_OPERAND (sub, 0);
15558       tree op01 = TREE_OPERAND (sub, 1);
15559       tree op00type;
15560
15561       STRIP_NOPS (op00);
15562       op00type = TREE_TYPE (op00);
15563       if (TREE_CODE (op00) == ADDR_EXPR
15564           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15565           && type == TREE_TYPE (TREE_TYPE (op00type)))
15566         {
15567           tree size = TYPE_SIZE_UNIT (type);
15568           if (tree_int_cst_equal (size, op01))
15569             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15570         }
15571     }
15572   
15573   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15574   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15575       && type == TREE_TYPE (TREE_TYPE (subtype)))
15576     {
15577       tree type_domain;
15578       tree min_val = size_zero_node;
15579       sub = build_fold_indirect_ref (sub);
15580       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15581       if (type_domain && TYPE_MIN_VALUE (type_domain))
15582         min_val = TYPE_MIN_VALUE (type_domain);
15583       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15584     }
15585
15586   return NULL_TREE;
15587 }
15588
15589 /* Builds an expression for an indirection through T, simplifying some
15590    cases.  */
15591
15592 tree
15593 build_fold_indirect_ref (tree t)
15594 {
15595   tree type = TREE_TYPE (TREE_TYPE (t));
15596   tree sub = fold_indirect_ref_1 (type, t);
15597
15598   if (sub)
15599     return sub;
15600   else
15601     return build1 (INDIRECT_REF, type, t);
15602 }
15603
15604 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
15605
15606 tree
15607 fold_indirect_ref (tree t)
15608 {
15609   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15610
15611   if (sub)
15612     return sub;
15613   else
15614     return t;
15615 }
15616
15617 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15618    whose result is ignored.  The type of the returned tree need not be
15619    the same as the original expression.  */
15620
15621 tree
15622 fold_ignored_result (tree t)
15623 {
15624   if (!TREE_SIDE_EFFECTS (t))
15625     return integer_zero_node;
15626
15627   for (;;)
15628     switch (TREE_CODE_CLASS (TREE_CODE (t)))
15629       {
15630       case tcc_unary:
15631         t = TREE_OPERAND (t, 0);
15632         break;
15633
15634       case tcc_binary:
15635       case tcc_comparison:
15636         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15637           t = TREE_OPERAND (t, 0);
15638         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15639           t = TREE_OPERAND (t, 1);
15640         else
15641           return t;
15642         break;
15643
15644       case tcc_expression:
15645         switch (TREE_CODE (t))
15646           {
15647           case COMPOUND_EXPR:
15648             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15649               return t;
15650             t = TREE_OPERAND (t, 0);
15651             break;
15652
15653           case COND_EXPR:
15654             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15655                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15656               return t;
15657             t = TREE_OPERAND (t, 0);
15658             break;
15659
15660           default:
15661             return t;
15662           }
15663         break;
15664
15665       default:
15666         return t;
15667       }
15668 }
15669
15670 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15671    This can only be applied to objects of a sizetype.  */
15672
15673 tree
15674 round_up (tree value, int divisor)
15675 {
15676   tree div = NULL_TREE;
15677
15678   gcc_assert (divisor > 0);
15679   if (divisor == 1)
15680     return value;
15681
15682   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15683      have to do anything.  Only do this when we are not given a const,
15684      because in that case, this check is more expensive than just
15685      doing it.  */
15686   if (TREE_CODE (value) != INTEGER_CST)
15687     {
15688       div = build_int_cst (TREE_TYPE (value), divisor);
15689
15690       if (multiple_of_p (TREE_TYPE (value), value, div))
15691         return value;
15692     }
15693
15694   /* If divisor is a power of two, simplify this to bit manipulation.  */
15695   if (divisor == (divisor & -divisor))
15696     {
15697       if (TREE_CODE (value) == INTEGER_CST)
15698         {
15699           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15700           unsigned HOST_WIDE_INT high;
15701           bool overflow_p;
15702
15703           if ((low & (divisor - 1)) == 0)
15704             return value;
15705
15706           overflow_p = TREE_OVERFLOW (value);
15707           high = TREE_INT_CST_HIGH (value);
15708           low &= ~(divisor - 1);
15709           low += divisor;
15710           if (low == 0)
15711             {
15712               high++;
15713               if (high == 0)
15714                 overflow_p = true;
15715             }
15716
15717           return force_fit_type_double (TREE_TYPE (value), low, high,
15718                                         -1, overflow_p);
15719         }
15720       else
15721         {
15722           tree t;
15723
15724           t = build_int_cst (TREE_TYPE (value), divisor - 1);
15725           value = size_binop (PLUS_EXPR, value, t);
15726           t = build_int_cst (TREE_TYPE (value), -divisor);
15727           value = size_binop (BIT_AND_EXPR, value, t);
15728         }
15729     }
15730   else
15731     {
15732       if (!div)
15733         div = build_int_cst (TREE_TYPE (value), divisor);
15734       value = size_binop (CEIL_DIV_EXPR, value, div);
15735       value = size_binop (MULT_EXPR, value, div);
15736     }
15737
15738   return value;
15739 }
15740
15741 /* Likewise, but round down.  */
15742
15743 tree
15744 round_down (tree value, int divisor)
15745 {
15746   tree div = NULL_TREE;
15747
15748   gcc_assert (divisor > 0);
15749   if (divisor == 1)
15750     return value;
15751
15752   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
15753      have to do anything.  Only do this when we are not given a const,
15754      because in that case, this check is more expensive than just
15755      doing it.  */
15756   if (TREE_CODE (value) != INTEGER_CST)
15757     {
15758       div = build_int_cst (TREE_TYPE (value), divisor);
15759
15760       if (multiple_of_p (TREE_TYPE (value), value, div))
15761         return value;
15762     }
15763
15764   /* If divisor is a power of two, simplify this to bit manipulation.  */
15765   if (divisor == (divisor & -divisor))
15766     {
15767       tree t;
15768
15769       t = build_int_cst (TREE_TYPE (value), -divisor);
15770       value = size_binop (BIT_AND_EXPR, value, t);
15771     }
15772   else
15773     {
15774       if (!div)
15775         div = build_int_cst (TREE_TYPE (value), divisor);
15776       value = size_binop (FLOOR_DIV_EXPR, value, div);
15777       value = size_binop (MULT_EXPR, value, div);
15778     }
15779
15780   return value;
15781 }
15782
15783 /* Returns the pointer to the base of the object addressed by EXP and
15784    extracts the information about the offset of the access, storing it
15785    to PBITPOS and POFFSET.  */
15786
15787 static tree
15788 split_address_to_core_and_offset (tree exp,
15789                                   HOST_WIDE_INT *pbitpos, tree *poffset)
15790 {
15791   tree core;
15792   enum machine_mode mode;
15793   int unsignedp, volatilep;
15794   HOST_WIDE_INT bitsize;
15795
15796   if (TREE_CODE (exp) == ADDR_EXPR)
15797     {
15798       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15799                                   poffset, &mode, &unsignedp, &volatilep,
15800                                   false);
15801       core = build_fold_addr_expr (core);
15802     }
15803   else
15804     {
15805       core = exp;
15806       *pbitpos = 0;
15807       *poffset = NULL_TREE;
15808     }
15809
15810   return core;
15811 }
15812
15813 /* Returns true if addresses of E1 and E2 differ by a constant, false
15814    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
15815
15816 bool
15817 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15818 {
15819   tree core1, core2;
15820   HOST_WIDE_INT bitpos1, bitpos2;
15821   tree toffset1, toffset2, tdiff, type;
15822
15823   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15824   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15825
15826   if (bitpos1 % BITS_PER_UNIT != 0
15827       || bitpos2 % BITS_PER_UNIT != 0
15828       || !operand_equal_p (core1, core2, 0))
15829     return false;
15830
15831   if (toffset1 && toffset2)
15832     {
15833       type = TREE_TYPE (toffset1);
15834       if (type != TREE_TYPE (toffset2))
15835         toffset2 = fold_convert (type, toffset2);
15836
15837       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15838       if (!cst_and_fits_in_hwi (tdiff))
15839         return false;
15840
15841       *diff = int_cst_value (tdiff);
15842     }
15843   else if (toffset1 || toffset2)
15844     {
15845       /* If only one of the offsets is non-constant, the difference cannot
15846          be a constant.  */
15847       return false;
15848     }
15849   else
15850     *diff = 0;
15851
15852   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15853   return true;
15854 }
15855
15856 /* Simplify the floating point expression EXP when the sign of the
15857    result is not significant.  Return NULL_TREE if no simplification
15858    is possible.  */
15859
15860 tree
15861 fold_strip_sign_ops (tree exp)
15862 {
15863   tree arg0, arg1;
15864
15865   switch (TREE_CODE (exp))
15866     {
15867     case ABS_EXPR:
15868     case NEGATE_EXPR:
15869       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15870       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15871
15872     case MULT_EXPR:
15873     case RDIV_EXPR:
15874       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15875         return NULL_TREE;
15876       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15877       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15878       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15879         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15880                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
15881                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
15882       break;
15883
15884     case COMPOUND_EXPR:
15885       arg0 = TREE_OPERAND (exp, 0);
15886       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15887       if (arg1)
15888         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15889       break;
15890       
15891     case COND_EXPR:
15892       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15893       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15894       if (arg0 || arg1)
15895         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15896                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
15897                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
15898       break;
15899       
15900     case CALL_EXPR:
15901       {
15902         const enum built_in_function fcode = builtin_mathfn_code (exp);
15903         switch (fcode)
15904         {
15905         CASE_FLT_FN (BUILT_IN_COPYSIGN):
15906           /* Strip copysign function call, return the 1st argument. */
15907           arg0 = CALL_EXPR_ARG (exp, 0);
15908           arg1 = CALL_EXPR_ARG (exp, 1);
15909           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15910
15911         default:
15912           /* Strip sign ops from the argument of "odd" math functions.  */
15913           if (negate_mathfn_p (fcode))
15914             {
15915               arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15916               if (arg0)
15917                 return build_call_expr (get_callee_fndecl (exp), 1, arg0);
15918             }
15919           break;
15920         }
15921       }
15922       break;
15923
15924     default:
15925       break;
15926     }
15927   return NULL_TREE;
15928 }