OSDN Git Service

2007-01-08 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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
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.
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 takes a constant, an overflowable flag and prior
43    overflow indicators.  It forces the value to fit the type and sets
44    TREE_OVERFLOW and TREE_CONSTANT_OVERFLOW as appropriate.
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 "rtl.h"
58 #include "expr.h"
59 #include "tm_p.h"
60 #include "toplev.h"
61 #include "ggc.h"
62 #include "hashtab.h"
63 #include "langhooks.h"
64 #include "md5.h"
65
66 /* Non-zero if we are folding constants inside an initializer; zero
67    otherwise.  */
68 int folding_initializer = 0;
69
70 /* The following constants represent a bit based encoding of GCC's
71    comparison operators.  This encoding simplifies transformations
72    on relational comparison operators, such as AND and OR.  */
73 enum comparison_code {
74   COMPCODE_FALSE = 0,
75   COMPCODE_LT = 1,
76   COMPCODE_EQ = 2,
77   COMPCODE_LE = 3,
78   COMPCODE_GT = 4,
79   COMPCODE_LTGT = 5,
80   COMPCODE_GE = 6,
81   COMPCODE_ORD = 7,
82   COMPCODE_UNORD = 8,
83   COMPCODE_UNLT = 9,
84   COMPCODE_UNEQ = 10,
85   COMPCODE_UNLE = 11,
86   COMPCODE_UNGT = 12,
87   COMPCODE_NE = 13,
88   COMPCODE_UNGE = 14,
89   COMPCODE_TRUE = 15
90 };
91
92 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
93 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
94 static bool negate_mathfn_p (enum built_in_function);
95 static bool negate_expr_p (tree);
96 static tree negate_expr (tree);
97 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
98 static tree associate_trees (tree, tree, enum tree_code, tree);
99 static tree const_binop (enum tree_code, tree, tree, int);
100 static enum comparison_code comparison_to_compcode (enum tree_code);
101 static enum tree_code compcode_to_comparison (enum comparison_code);
102 static tree combine_comparisons (enum tree_code, enum tree_code,
103                                  enum tree_code, tree, tree, tree);
104 static int truth_value_p (enum tree_code);
105 static int operand_equal_for_comparison_p (tree, tree, tree);
106 static int twoval_comparison_p (tree, tree *, tree *, int *);
107 static tree eval_subst (tree, tree, tree, tree, tree);
108 static tree pedantic_omit_one_operand (tree, tree, tree);
109 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
110 static tree make_bit_field_ref (tree, tree, int, int, int);
111 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
112 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
113                                     enum machine_mode *, int *, int *,
114                                     tree *, tree *);
115 static int all_ones_mask_p (tree, int);
116 static tree sign_bit_p (tree, tree);
117 static int simple_operand_p (tree);
118 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
119 static tree range_predecessor (tree);
120 static tree range_successor (tree);
121 static tree make_range (tree, int *, tree *, tree *);
122 static tree build_range_check (tree, tree, int, tree, tree);
123 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
124                          tree);
125 static tree fold_range_test (enum tree_code, tree, tree, tree);
126 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
127 static tree unextend (tree, int, int, tree);
128 static tree fold_truthop (enum tree_code, tree, tree, tree);
129 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
132 static int multiple_of_p (tree, tree, tree);
133 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
134                                                  tree, tree,
135                                                  tree, tree, int);
136 static bool fold_real_zero_addition_p (tree, tree, int);
137 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
138                                  tree, tree, tree);
139 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
140 static tree fold_div_compare (enum tree_code, tree, tree, tree);
141 static bool reorder_operands_p (tree, tree);
142 static tree fold_negate_const (tree, tree);
143 static tree fold_not_const (tree, tree);
144 static tree fold_relational_const (enum tree_code, tree, tree, tree);
145 static int native_encode_expr (tree, unsigned char *, int);
146 static tree native_interpret_expr (tree, unsigned char *, int);
147
148
149 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
150    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
151    and SUM1.  Then this yields nonzero if overflow occurred during the
152    addition.
153
154    Overflow occurs if A and B have the same sign, but A and SUM differ in
155    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
156    sign.  */
157 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
158 \f
159 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
160    We do that by representing the two-word integer in 4 words, with only
161    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
162    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
163
164 #define LOWPART(x) \
165   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
166 #define HIGHPART(x) \
167   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
168 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
169
170 /* Unpack a two-word integer into 4 words.
171    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
172    WORDS points to the array of HOST_WIDE_INTs.  */
173
174 static void
175 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
176 {
177   words[0] = LOWPART (low);
178   words[1] = HIGHPART (low);
179   words[2] = LOWPART (hi);
180   words[3] = HIGHPART (hi);
181 }
182
183 /* Pack an array of 4 words into a two-word integer.
184    WORDS points to the array of words.
185    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
186
187 static void
188 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
189         HOST_WIDE_INT *hi)
190 {
191   *low = words[0] + words[1] * BASE;
192   *hi = words[2] + words[3] * BASE;
193 }
194 \f
195 /* Force the double-word integer L1, H1 to be within the range of the
196    integer type TYPE.  Stores the properly truncated and sign-extended
197    double-word integer in *LV, *HV.  Returns true if the operation
198    overflows, that is, argument and result are different.  */
199
200 int
201 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
202                  unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, tree type)
203 {
204   unsigned HOST_WIDE_INT low0 = l1;
205   HOST_WIDE_INT high0 = h1;
206   unsigned int prec;
207   int sign_extended_type;
208
209   if (POINTER_TYPE_P (type)
210       || TREE_CODE (type) == OFFSET_TYPE)
211     prec = POINTER_SIZE;
212   else
213     prec = TYPE_PRECISION (type);
214
215   /* Size types *are* sign extended.  */
216   sign_extended_type = (!TYPE_UNSIGNED (type)
217                         || (TREE_CODE (type) == INTEGER_TYPE
218                             && TYPE_IS_SIZETYPE (type)));
219
220   /* First clear all bits that are beyond the type's precision.  */
221   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
222     ;
223   else if (prec > HOST_BITS_PER_WIDE_INT)
224     h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
225   else
226     {
227       h1 = 0;
228       if (prec < HOST_BITS_PER_WIDE_INT)
229         l1 &= ~((HOST_WIDE_INT) (-1) << prec);
230     }
231
232   /* Then do sign extension if necessary.  */
233   if (!sign_extended_type)
234     /* No sign extension */;
235   else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
236     /* Correct width already.  */;
237   else if (prec > HOST_BITS_PER_WIDE_INT)
238     {
239       /* Sign extend top half? */
240       if (h1 & ((unsigned HOST_WIDE_INT)1
241                 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
242         h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
243     }
244   else if (prec == HOST_BITS_PER_WIDE_INT)
245     {
246       if ((HOST_WIDE_INT)l1 < 0)
247         h1 = -1;
248     }
249   else
250     {
251       /* Sign extend bottom half? */
252       if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
253         {
254           h1 = -1;
255           l1 |= (HOST_WIDE_INT)(-1) << prec;
256         }
257     }
258
259   *lv = l1;
260   *hv = h1;
261
262   /* If the value didn't fit, signal overflow.  */
263   return l1 != low0 || h1 != high0;
264 }
265
266 /* T is an INT_CST node.  OVERFLOWABLE indicates if we are interested
267    in overflow of the value, when >0 we are only interested in signed
268    overflow, for <0 we are interested in any overflow.  OVERFLOWED
269    indicates whether overflow has already occurred.  CONST_OVERFLOWED
270    indicates whether constant overflow has already occurred.  We force
271    T's value to be within range of T's type (by setting to 0 or 1 all
272    the bits outside the type's range).  We set TREE_OVERFLOWED if,
273         OVERFLOWED is nonzero,
274         or OVERFLOWABLE is >0 and signed overflow occurs
275         or OVERFLOWABLE is <0 and any overflow occurs
276    We set TREE_CONSTANT_OVERFLOWED if,
277         CONST_OVERFLOWED is nonzero
278         or we set TREE_OVERFLOWED.
279   We return either the original T, or a copy.  */
280
281 tree
282 force_fit_type (tree t, int overflowable,
283                 bool overflowed, bool overflowed_const)
284 {
285   unsigned HOST_WIDE_INT low;
286   HOST_WIDE_INT high;
287   int sign_extended_type;
288   bool overflow;
289
290   gcc_assert (TREE_CODE (t) == INTEGER_CST);
291
292   /* Size types *are* sign extended.  */
293   sign_extended_type = (!TYPE_UNSIGNED (TREE_TYPE (t))
294                         || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
295                             && TYPE_IS_SIZETYPE (TREE_TYPE (t))));
296
297   low = TREE_INT_CST_LOW (t);
298   high = TREE_INT_CST_HIGH (t);
299
300   overflow = fit_double_type (low, high, &low, &high, TREE_TYPE (t));
301
302   /* If the value changed, return a new node.  */
303   if (overflowed || overflowed_const || overflow)
304     {
305       t = build_int_cst_wide (TREE_TYPE (t), low, high);
306
307       if (overflowed
308           || overflowable < 0
309           || (overflowable > 0 && sign_extended_type))
310         {
311           t = copy_node (t);
312           TREE_OVERFLOW (t) = 1;
313           TREE_CONSTANT_OVERFLOW (t) = 1;
314         }
315       else if (overflowed_const)
316         {
317           t = copy_node (t);
318           TREE_CONSTANT_OVERFLOW (t) = 1;
319         }
320     }
321
322   return t;
323 }
324 \f
325 /* Add two doubleword integers with doubleword result.
326    Return nonzero if the operation overflows according to UNSIGNED_P.
327    Each argument is given as two `HOST_WIDE_INT' pieces.
328    One argument is L1 and H1; the other, L2 and H2.
329    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
330
331 int
332 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
333                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
334                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
335                       bool unsigned_p)
336 {
337   unsigned HOST_WIDE_INT l;
338   HOST_WIDE_INT h;
339
340   l = l1 + l2;
341   h = h1 + h2 + (l < l1);
342
343   *lv = l;
344   *hv = h;
345
346   if (unsigned_p)
347     return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
348   else
349     return OVERFLOW_SUM_SIGN (h1, h2, h);
350 }
351
352 /* Negate a doubleword integer with doubleword result.
353    Return nonzero if the operation overflows, assuming it's signed.
354    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
355    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
356
357 int
358 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
359             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
360 {
361   if (l1 == 0)
362     {
363       *lv = 0;
364       *hv = - h1;
365       return (*hv & h1) < 0;
366     }
367   else
368     {
369       *lv = -l1;
370       *hv = ~h1;
371       return 0;
372     }
373 }
374 \f
375 /* Multiply two doubleword integers with doubleword result.
376    Return nonzero if the operation overflows according to UNSIGNED_P.
377    Each argument is given as two `HOST_WIDE_INT' pieces.
378    One argument is L1 and H1; the other, L2 and H2.
379    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
380
381 int
382 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
383                       unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
384                       unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
385                       bool unsigned_p)
386 {
387   HOST_WIDE_INT arg1[4];
388   HOST_WIDE_INT arg2[4];
389   HOST_WIDE_INT prod[4 * 2];
390   unsigned HOST_WIDE_INT carry;
391   int i, j, k;
392   unsigned HOST_WIDE_INT toplow, neglow;
393   HOST_WIDE_INT tophigh, neghigh;
394
395   encode (arg1, l1, h1);
396   encode (arg2, l2, h2);
397
398   memset (prod, 0, sizeof prod);
399
400   for (i = 0; i < 4; i++)
401     {
402       carry = 0;
403       for (j = 0; j < 4; j++)
404         {
405           k = i + j;
406           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
407           carry += arg1[i] * arg2[j];
408           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
409           carry += prod[k];
410           prod[k] = LOWPART (carry);
411           carry = HIGHPART (carry);
412         }
413       prod[i + 4] = carry;
414     }
415
416   decode (prod, lv, hv);
417   decode (prod + 4, &toplow, &tophigh);
418
419   /* Unsigned overflow is immediate.  */
420   if (unsigned_p)
421     return (toplow | tophigh) != 0;
422
423   /* Check for signed overflow by calculating the signed representation of the
424      top half of the result; it should agree with the low half's sign bit.  */
425   if (h1 < 0)
426     {
427       neg_double (l2, h2, &neglow, &neghigh);
428       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
429     }
430   if (h2 < 0)
431     {
432       neg_double (l1, h1, &neglow, &neghigh);
433       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
434     }
435   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
436 }
437 \f
438 /* Shift the doubleword integer in L1, H1 left by COUNT places
439    keeping only PREC bits of result.
440    Shift right if COUNT is negative.
441    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
442    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
443
444 void
445 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
446                HOST_WIDE_INT count, unsigned int prec,
447                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
448 {
449   unsigned HOST_WIDE_INT signmask;
450
451   if (count < 0)
452     {
453       rshift_double (l1, h1, -count, prec, lv, hv, arith);
454       return;
455     }
456
457   if (SHIFT_COUNT_TRUNCATED)
458     count %= prec;
459
460   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
461     {
462       /* Shifting by the host word size is undefined according to the
463          ANSI standard, so we must handle this as a special case.  */
464       *hv = 0;
465       *lv = 0;
466     }
467   else if (count >= HOST_BITS_PER_WIDE_INT)
468     {
469       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
470       *lv = 0;
471     }
472   else
473     {
474       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
475              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
476       *lv = l1 << count;
477     }
478
479   /* Sign extend all bits that are beyond the precision.  */
480
481   signmask = -((prec > HOST_BITS_PER_WIDE_INT
482                 ? ((unsigned HOST_WIDE_INT) *hv
483                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
484                 : (*lv >> (prec - 1))) & 1);
485
486   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
487     ;
488   else if (prec >= HOST_BITS_PER_WIDE_INT)
489     {
490       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
491       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
492     }
493   else
494     {
495       *hv = signmask;
496       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
497       *lv |= signmask << prec;
498     }
499 }
500
501 /* Shift the doubleword integer in L1, H1 right by COUNT places
502    keeping only PREC bits of result.  COUNT must be positive.
503    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
504    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
505
506 void
507 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
508                HOST_WIDE_INT count, unsigned int prec,
509                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
510                int arith)
511 {
512   unsigned HOST_WIDE_INT signmask;
513
514   signmask = (arith
515               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
516               : 0);
517
518   if (SHIFT_COUNT_TRUNCATED)
519     count %= prec;
520
521   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
522     {
523       /* Shifting by the host word size is undefined according to the
524          ANSI standard, so we must handle this as a special case.  */
525       *hv = 0;
526       *lv = 0;
527     }
528   else if (count >= HOST_BITS_PER_WIDE_INT)
529     {
530       *hv = 0;
531       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
532     }
533   else
534     {
535       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
536       *lv = ((l1 >> count)
537              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
538     }
539
540   /* Zero / sign extend all bits that are beyond the precision.  */
541
542   if (count >= (HOST_WIDE_INT)prec)
543     {
544       *hv = signmask;
545       *lv = signmask;
546     }
547   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
548     ;
549   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
550     {
551       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
552       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
553     }
554   else
555     {
556       *hv = signmask;
557       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
558       *lv |= signmask << (prec - count);
559     }
560 }
561 \f
562 /* Rotate the doubleword integer in L1, H1 left by COUNT places
563    keeping only PREC bits of result.
564    Rotate right if COUNT is negative.
565    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
566
567 void
568 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
569                 HOST_WIDE_INT count, unsigned int prec,
570                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
571 {
572   unsigned HOST_WIDE_INT s1l, s2l;
573   HOST_WIDE_INT s1h, s2h;
574
575   count %= prec;
576   if (count < 0)
577     count += prec;
578
579   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
580   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
581   *lv = s1l | s2l;
582   *hv = s1h | s2h;
583 }
584
585 /* Rotate the doubleword integer in L1, H1 left by COUNT places
586    keeping only PREC bits of result.  COUNT must be positive.
587    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
588
589 void
590 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
591                 HOST_WIDE_INT count, unsigned int prec,
592                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
593 {
594   unsigned HOST_WIDE_INT s1l, s2l;
595   HOST_WIDE_INT s1h, s2h;
596
597   count %= prec;
598   if (count < 0)
599     count += prec;
600
601   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
602   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
603   *lv = s1l | s2l;
604   *hv = s1h | s2h;
605 }
606 \f
607 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
608    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
609    CODE is a tree code for a kind of division, one of
610    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
611    or EXACT_DIV_EXPR
612    It controls how the quotient is rounded to an integer.
613    Return nonzero if the operation overflows.
614    UNS nonzero says do unsigned division.  */
615
616 int
617 div_and_round_double (enum tree_code code, int uns,
618                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
619                       HOST_WIDE_INT hnum_orig,
620                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
621                       HOST_WIDE_INT hden_orig,
622                       unsigned HOST_WIDE_INT *lquo,
623                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
624                       HOST_WIDE_INT *hrem)
625 {
626   int quo_neg = 0;
627   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
628   HOST_WIDE_INT den[4], quo[4];
629   int i, j;
630   unsigned HOST_WIDE_INT work;
631   unsigned HOST_WIDE_INT carry = 0;
632   unsigned HOST_WIDE_INT lnum = lnum_orig;
633   HOST_WIDE_INT hnum = hnum_orig;
634   unsigned HOST_WIDE_INT lden = lden_orig;
635   HOST_WIDE_INT hden = hden_orig;
636   int overflow = 0;
637
638   if (hden == 0 && lden == 0)
639     overflow = 1, lden = 1;
640
641   /* Calculate quotient sign and convert operands to unsigned.  */
642   if (!uns)
643     {
644       if (hnum < 0)
645         {
646           quo_neg = ~ quo_neg;
647           /* (minimum integer) / (-1) is the only overflow case.  */
648           if (neg_double (lnum, hnum, &lnum, &hnum)
649               && ((HOST_WIDE_INT) lden & hden) == -1)
650             overflow = 1;
651         }
652       if (hden < 0)
653         {
654           quo_neg = ~ quo_neg;
655           neg_double (lden, hden, &lden, &hden);
656         }
657     }
658
659   if (hnum == 0 && hden == 0)
660     {                           /* single precision */
661       *hquo = *hrem = 0;
662       /* This unsigned division rounds toward zero.  */
663       *lquo = lnum / lden;
664       goto finish_up;
665     }
666
667   if (hnum == 0)
668     {                           /* trivial case: dividend < divisor */
669       /* hden != 0 already checked.  */
670       *hquo = *lquo = 0;
671       *hrem = hnum;
672       *lrem = lnum;
673       goto finish_up;
674     }
675
676   memset (quo, 0, sizeof quo);
677
678   memset (num, 0, sizeof num);  /* to zero 9th element */
679   memset (den, 0, sizeof den);
680
681   encode (num, lnum, hnum);
682   encode (den, lden, hden);
683
684   /* Special code for when the divisor < BASE.  */
685   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
686     {
687       /* hnum != 0 already checked.  */
688       for (i = 4 - 1; i >= 0; i--)
689         {
690           work = num[i] + carry * BASE;
691           quo[i] = work / lden;
692           carry = work % lden;
693         }
694     }
695   else
696     {
697       /* Full double precision division,
698          with thanks to Don Knuth's "Seminumerical Algorithms".  */
699       int num_hi_sig, den_hi_sig;
700       unsigned HOST_WIDE_INT quo_est, scale;
701
702       /* Find the highest nonzero divisor digit.  */
703       for (i = 4 - 1;; i--)
704         if (den[i] != 0)
705           {
706             den_hi_sig = i;
707             break;
708           }
709
710       /* Insure that the first digit of the divisor is at least BASE/2.
711          This is required by the quotient digit estimation algorithm.  */
712
713       scale = BASE / (den[den_hi_sig] + 1);
714       if (scale > 1)
715         {               /* scale divisor and dividend */
716           carry = 0;
717           for (i = 0; i <= 4 - 1; i++)
718             {
719               work = (num[i] * scale) + carry;
720               num[i] = LOWPART (work);
721               carry = HIGHPART (work);
722             }
723
724           num[4] = carry;
725           carry = 0;
726           for (i = 0; i <= 4 - 1; i++)
727             {
728               work = (den[i] * scale) + carry;
729               den[i] = LOWPART (work);
730               carry = HIGHPART (work);
731               if (den[i] != 0) den_hi_sig = i;
732             }
733         }
734
735       num_hi_sig = 4;
736
737       /* Main loop */
738       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
739         {
740           /* Guess the next quotient digit, quo_est, by dividing the first
741              two remaining dividend digits by the high order quotient digit.
742              quo_est is never low and is at most 2 high.  */
743           unsigned HOST_WIDE_INT tmp;
744
745           num_hi_sig = i + den_hi_sig + 1;
746           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
747           if (num[num_hi_sig] != den[den_hi_sig])
748             quo_est = work / den[den_hi_sig];
749           else
750             quo_est = BASE - 1;
751
752           /* Refine quo_est so it's usually correct, and at most one high.  */
753           tmp = work - quo_est * den[den_hi_sig];
754           if (tmp < BASE
755               && (den[den_hi_sig - 1] * quo_est
756                   > (tmp * BASE + num[num_hi_sig - 2])))
757             quo_est--;
758
759           /* Try QUO_EST as the quotient digit, by multiplying the
760              divisor by QUO_EST and subtracting from the remaining dividend.
761              Keep in mind that QUO_EST is the I - 1st digit.  */
762
763           carry = 0;
764           for (j = 0; j <= den_hi_sig; j++)
765             {
766               work = quo_est * den[j] + carry;
767               carry = HIGHPART (work);
768               work = num[i + j] - LOWPART (work);
769               num[i + j] = LOWPART (work);
770               carry += HIGHPART (work) != 0;
771             }
772
773           /* If quo_est was high by one, then num[i] went negative and
774              we need to correct things.  */
775           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
776             {
777               quo_est--;
778               carry = 0;                /* add divisor back in */
779               for (j = 0; j <= den_hi_sig; j++)
780                 {
781                   work = num[i + j] + den[j] + carry;
782                   carry = HIGHPART (work);
783                   num[i + j] = LOWPART (work);
784                 }
785
786               num [num_hi_sig] += carry;
787             }
788
789           /* Store the quotient digit.  */
790           quo[i] = quo_est;
791         }
792     }
793
794   decode (quo, lquo, hquo);
795
796  finish_up:
797   /* If result is negative, make it so.  */
798   if (quo_neg)
799     neg_double (*lquo, *hquo, lquo, hquo);
800
801   /* Compute trial remainder:  rem = num - (quo * den)  */
802   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
803   neg_double (*lrem, *hrem, lrem, hrem);
804   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
805
806   switch (code)
807     {
808     case TRUNC_DIV_EXPR:
809     case TRUNC_MOD_EXPR:        /* round toward zero */
810     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
811       return overflow;
812
813     case FLOOR_DIV_EXPR:
814     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
815       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
816         {
817           /* quo = quo - 1;  */
818           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
819                       lquo, hquo);
820         }
821       else
822         return overflow;
823       break;
824
825     case CEIL_DIV_EXPR:
826     case CEIL_MOD_EXPR:         /* round toward positive infinity */
827       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
828         {
829           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
830                       lquo, hquo);
831         }
832       else
833         return overflow;
834       break;
835
836     case ROUND_DIV_EXPR:
837     case ROUND_MOD_EXPR:        /* round to closest integer */
838       {
839         unsigned HOST_WIDE_INT labs_rem = *lrem;
840         HOST_WIDE_INT habs_rem = *hrem;
841         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
842         HOST_WIDE_INT habs_den = hden, htwice;
843
844         /* Get absolute values.  */
845         if (*hrem < 0)
846           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
847         if (hden < 0)
848           neg_double (lden, hden, &labs_den, &habs_den);
849
850         /* If (2 * abs (lrem) >= abs (lden)) */
851         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
852                     labs_rem, habs_rem, &ltwice, &htwice);
853
854         if (((unsigned HOST_WIDE_INT) habs_den
855              < (unsigned HOST_WIDE_INT) htwice)
856             || (((unsigned HOST_WIDE_INT) habs_den
857                  == (unsigned HOST_WIDE_INT) htwice)
858                 && (labs_den < ltwice)))
859           {
860             if (*hquo < 0)
861               /* quo = quo - 1;  */
862               add_double (*lquo, *hquo,
863                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
864             else
865               /* quo = quo + 1; */
866               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
867                           lquo, hquo);
868           }
869         else
870           return overflow;
871       }
872       break;
873
874     default:
875       gcc_unreachable ();
876     }
877
878   /* Compute true remainder:  rem = num - (quo * den)  */
879   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
880   neg_double (*lrem, *hrem, lrem, hrem);
881   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
882   return overflow;
883 }
884
885 /* If ARG2 divides ARG1 with zero remainder, carries out the division
886    of type CODE and returns the quotient.
887    Otherwise returns NULL_TREE.  */
888
889 static tree
890 div_if_zero_remainder (enum tree_code code, tree arg1, tree arg2)
891 {
892   unsigned HOST_WIDE_INT int1l, int2l;
893   HOST_WIDE_INT int1h, int2h;
894   unsigned HOST_WIDE_INT quol, reml;
895   HOST_WIDE_INT quoh, remh;
896   tree type = TREE_TYPE (arg1);
897   int uns = TYPE_UNSIGNED (type);
898
899   int1l = TREE_INT_CST_LOW (arg1);
900   int1h = TREE_INT_CST_HIGH (arg1);
901   int2l = TREE_INT_CST_LOW (arg2);
902   int2h = TREE_INT_CST_HIGH (arg2);
903
904   div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
905                         &quol, &quoh, &reml, &remh);
906   if (remh != 0 || reml != 0)
907     return NULL_TREE;
908
909   return build_int_cst_wide (type, quol, quoh);
910 }
911 \f
912 /* Return true if the built-in mathematical function specified by CODE
913    is odd, i.e. -f(x) == f(-x).  */
914
915 static bool
916 negate_mathfn_p (enum built_in_function code)
917 {
918   switch (code)
919     {
920     CASE_FLT_FN (BUILT_IN_ASIN):
921     CASE_FLT_FN (BUILT_IN_ASINH):
922     CASE_FLT_FN (BUILT_IN_ATAN):
923     CASE_FLT_FN (BUILT_IN_ATANH):
924     CASE_FLT_FN (BUILT_IN_CBRT):
925     CASE_FLT_FN (BUILT_IN_ERF):
926     CASE_FLT_FN (BUILT_IN_LLROUND):
927     CASE_FLT_FN (BUILT_IN_LROUND):
928     CASE_FLT_FN (BUILT_IN_ROUND):
929     CASE_FLT_FN (BUILT_IN_SIN):
930     CASE_FLT_FN (BUILT_IN_SINH):
931     CASE_FLT_FN (BUILT_IN_TAN):
932     CASE_FLT_FN (BUILT_IN_TANH):
933     CASE_FLT_FN (BUILT_IN_TRUNC):
934       return true;
935
936     CASE_FLT_FN (BUILT_IN_LLRINT):
937     CASE_FLT_FN (BUILT_IN_LRINT):
938     CASE_FLT_FN (BUILT_IN_NEARBYINT):
939     CASE_FLT_FN (BUILT_IN_RINT):
940       return !flag_rounding_math;
941     
942     default:
943       break;
944     }
945   return false;
946 }
947
948 /* Check whether we may negate an integer constant T without causing
949    overflow.  */
950
951 bool
952 may_negate_without_overflow_p (tree t)
953 {
954   unsigned HOST_WIDE_INT val;
955   unsigned int prec;
956   tree type;
957
958   gcc_assert (TREE_CODE (t) == INTEGER_CST);
959
960   type = TREE_TYPE (t);
961   if (TYPE_UNSIGNED (type))
962     return false;
963
964   prec = TYPE_PRECISION (type);
965   if (prec > HOST_BITS_PER_WIDE_INT)
966     {
967       if (TREE_INT_CST_LOW (t) != 0)
968         return true;
969       prec -= HOST_BITS_PER_WIDE_INT;
970       val = TREE_INT_CST_HIGH (t);
971     }
972   else
973     val = TREE_INT_CST_LOW (t);
974   if (prec < HOST_BITS_PER_WIDE_INT)
975     val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
976   return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
977 }
978
979 /* Determine whether an expression T can be cheaply negated using
980    the function negate_expr without introducing undefined overflow.  */
981
982 static bool
983 negate_expr_p (tree t)
984 {
985   tree type;
986
987   if (t == 0)
988     return false;
989
990   type = TREE_TYPE (t);
991
992   STRIP_SIGN_NOPS (t);
993   switch (TREE_CODE (t))
994     {
995     case INTEGER_CST:
996       if (TYPE_UNSIGNED (type)
997           || (flag_wrapv && ! flag_trapv))
998         return true;
999
1000       /* Check that -CST will not overflow type.  */
1001       return may_negate_without_overflow_p (t);
1002     case BIT_NOT_EXPR:
1003        return INTEGRAL_TYPE_P (type)
1004               && (TYPE_UNSIGNED (type)
1005                   || (flag_wrapv && !flag_trapv));
1006
1007     case REAL_CST:
1008     case NEGATE_EXPR:
1009       return true;
1010
1011     case COMPLEX_CST:
1012       return negate_expr_p (TREE_REALPART (t))
1013              && negate_expr_p (TREE_IMAGPART (t));
1014
1015     case PLUS_EXPR:
1016       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1017           || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1018         return false;
1019       /* -(A + B) -> (-B) - A.  */
1020       if (negate_expr_p (TREE_OPERAND (t, 1))
1021           && reorder_operands_p (TREE_OPERAND (t, 0),
1022                                  TREE_OPERAND (t, 1)))
1023         return true;
1024       /* -(A + B) -> (-A) - B.  */
1025       return negate_expr_p (TREE_OPERAND (t, 0));
1026
1027     case MINUS_EXPR:
1028       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
1029       return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1030              && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1031              && reorder_operands_p (TREE_OPERAND (t, 0),
1032                                     TREE_OPERAND (t, 1));
1033
1034     case MULT_EXPR:
1035       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1036         break;
1037
1038       /* Fall through.  */
1039
1040     case RDIV_EXPR:
1041       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1042         return negate_expr_p (TREE_OPERAND (t, 1))
1043                || negate_expr_p (TREE_OPERAND (t, 0));
1044       break;
1045
1046     case TRUNC_DIV_EXPR:
1047     case ROUND_DIV_EXPR:
1048     case FLOOR_DIV_EXPR:
1049     case CEIL_DIV_EXPR:
1050     case EXACT_DIV_EXPR:
1051       if (TYPE_UNSIGNED (TREE_TYPE (t)) || flag_wrapv)
1052         break;
1053       return negate_expr_p (TREE_OPERAND (t, 1))
1054              || negate_expr_p (TREE_OPERAND (t, 0));
1055
1056     case NOP_EXPR:
1057       /* Negate -((double)float) as (double)(-float).  */
1058       if (TREE_CODE (type) == REAL_TYPE)
1059         {
1060           tree tem = strip_float_extensions (t);
1061           if (tem != t)
1062             return negate_expr_p (tem);
1063         }
1064       break;
1065
1066     case CALL_EXPR:
1067       /* Negate -f(x) as f(-x).  */
1068       if (negate_mathfn_p (builtin_mathfn_code (t)))
1069         return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
1070       break;
1071
1072     case RSHIFT_EXPR:
1073       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1074       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1075         {
1076           tree op1 = TREE_OPERAND (t, 1);
1077           if (TREE_INT_CST_HIGH (op1) == 0
1078               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1079                  == TREE_INT_CST_LOW (op1))
1080             return true;
1081         }
1082       break;
1083
1084     default:
1085       break;
1086     }
1087   return false;
1088 }
1089
1090 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1091    simplification is possible.
1092    If negate_expr_p would return true for T, NULL_TREE will never be
1093    returned.  */
1094
1095 static tree
1096 fold_negate_expr (tree t)
1097 {
1098   tree type = TREE_TYPE (t);
1099   tree tem;
1100
1101   switch (TREE_CODE (t))
1102     {
1103     /* Convert - (~A) to A + 1.  */
1104     case BIT_NOT_EXPR:
1105       if (INTEGRAL_TYPE_P (type))
1106         return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1107                             build_int_cst (type, 1));
1108       break;
1109       
1110     case INTEGER_CST:
1111       tem = fold_negate_const (t, type);
1112       if (! TREE_OVERFLOW (tem)
1113           || TYPE_UNSIGNED (type)
1114           || ! flag_trapv)
1115         return tem;
1116       break;
1117
1118     case REAL_CST:
1119       tem = fold_negate_const (t, type);
1120       /* Two's complement FP formats, such as c4x, may overflow.  */
1121       if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
1122         return tem;
1123       break;
1124
1125     case COMPLEX_CST:
1126       {
1127         tree rpart = negate_expr (TREE_REALPART (t));
1128         tree ipart = negate_expr (TREE_IMAGPART (t));
1129
1130         if ((TREE_CODE (rpart) == REAL_CST
1131              && TREE_CODE (ipart) == REAL_CST)
1132             || (TREE_CODE (rpart) == INTEGER_CST
1133                 && TREE_CODE (ipart) == INTEGER_CST))
1134           return build_complex (type, rpart, ipart);
1135       }
1136       break;
1137
1138     case NEGATE_EXPR:
1139       return TREE_OPERAND (t, 0);
1140
1141     case PLUS_EXPR:
1142       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1143           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1144         {
1145           /* -(A + B) -> (-B) - A.  */
1146           if (negate_expr_p (TREE_OPERAND (t, 1))
1147               && reorder_operands_p (TREE_OPERAND (t, 0),
1148                                      TREE_OPERAND (t, 1)))
1149             {
1150               tem = negate_expr (TREE_OPERAND (t, 1));
1151               return fold_build2 (MINUS_EXPR, type,
1152                                   tem, TREE_OPERAND (t, 0));
1153             }
1154
1155           /* -(A + B) -> (-A) - B.  */
1156           if (negate_expr_p (TREE_OPERAND (t, 0)))
1157             {
1158               tem = negate_expr (TREE_OPERAND (t, 0));
1159               return fold_build2 (MINUS_EXPR, type,
1160                                   tem, TREE_OPERAND (t, 1));
1161             }
1162         }
1163       break;
1164
1165     case MINUS_EXPR:
1166       /* - (A - B) -> B - A  */
1167       if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1168           && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1169           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1170         return fold_build2 (MINUS_EXPR, type,
1171                             TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1172       break;
1173
1174     case MULT_EXPR:
1175       if (TYPE_UNSIGNED (type))
1176         break;
1177
1178       /* Fall through.  */
1179
1180     case RDIV_EXPR:
1181       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1182         {
1183           tem = TREE_OPERAND (t, 1);
1184           if (negate_expr_p (tem))
1185             return fold_build2 (TREE_CODE (t), type,
1186                                 TREE_OPERAND (t, 0), negate_expr (tem));
1187           tem = TREE_OPERAND (t, 0);
1188           if (negate_expr_p (tem))
1189             return fold_build2 (TREE_CODE (t), type,
1190                                 negate_expr (tem), TREE_OPERAND (t, 1));
1191         }
1192       break;
1193
1194     case TRUNC_DIV_EXPR:
1195     case ROUND_DIV_EXPR:
1196     case FLOOR_DIV_EXPR:
1197     case CEIL_DIV_EXPR:
1198     case EXACT_DIV_EXPR:
1199       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
1200         {
1201           tem = TREE_OPERAND (t, 1);
1202           if (negate_expr_p (tem))
1203             return fold_build2 (TREE_CODE (t), type,
1204                                 TREE_OPERAND (t, 0), negate_expr (tem));
1205           tem = TREE_OPERAND (t, 0);
1206           if (negate_expr_p (tem))
1207             return fold_build2 (TREE_CODE (t), type,
1208                                 negate_expr (tem), TREE_OPERAND (t, 1));
1209         }
1210       break;
1211
1212     case NOP_EXPR:
1213       /* Convert -((double)float) into (double)(-float).  */
1214       if (TREE_CODE (type) == REAL_TYPE)
1215         {
1216           tem = strip_float_extensions (t);
1217           if (tem != t && negate_expr_p (tem))
1218             return negate_expr (tem);
1219         }
1220       break;
1221
1222     case CALL_EXPR:
1223       /* Negate -f(x) as f(-x).  */
1224       if (negate_mathfn_p (builtin_mathfn_code (t))
1225           && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1226         {
1227           tree fndecl, arg, arglist;
1228
1229           fndecl = get_callee_fndecl (t);
1230           arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1231           arglist = build_tree_list (NULL_TREE, arg);
1232           return build_function_call_expr (fndecl, arglist);
1233         }
1234       break;
1235
1236     case RSHIFT_EXPR:
1237       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1238       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1239         {
1240           tree op1 = TREE_OPERAND (t, 1);
1241           if (TREE_INT_CST_HIGH (op1) == 0
1242               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1243                  == TREE_INT_CST_LOW (op1))
1244             {
1245               tree ntype = TYPE_UNSIGNED (type)
1246                            ? lang_hooks.types.signed_type (type)
1247                            : lang_hooks.types.unsigned_type (type);
1248               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1249               temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1250               return fold_convert (type, temp);
1251             }
1252         }
1253       break;
1254
1255     default:
1256       break;
1257     }
1258
1259   return NULL_TREE;
1260 }
1261
1262 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1263    negated in a simpler way.  Also allow for T to be NULL_TREE, in which case
1264    return NULL_TREE. */
1265
1266 static tree
1267 negate_expr (tree t)
1268 {
1269   tree type, tem;
1270
1271   if (t == NULL_TREE)
1272     return NULL_TREE;
1273
1274   type = TREE_TYPE (t);
1275   STRIP_SIGN_NOPS (t);
1276
1277   tem = fold_negate_expr (t);
1278   if (!tem)
1279     tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1280   return fold_convert (type, tem);
1281 }
1282 \f
1283 /* Split a tree IN into a constant, literal and variable parts that could be
1284    combined with CODE to make IN.  "constant" means an expression with
1285    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1286    commutative arithmetic operation.  Store the constant part into *CONP,
1287    the literal in *LITP and return the variable part.  If a part isn't
1288    present, set it to null.  If the tree does not decompose in this way,
1289    return the entire tree as the variable part and the other parts as null.
1290
1291    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1292    case, we negate an operand that was subtracted.  Except if it is a
1293    literal for which we use *MINUS_LITP instead.
1294
1295    If NEGATE_P is true, we are negating all of IN, again except a literal
1296    for which we use *MINUS_LITP instead.
1297
1298    If IN is itself a literal or constant, return it as appropriate.
1299
1300    Note that we do not guarantee that any of the three values will be the
1301    same type as IN, but they will have the same signedness and mode.  */
1302
1303 static tree
1304 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1305             tree *minus_litp, int negate_p)
1306 {
1307   tree var = 0;
1308
1309   *conp = 0;
1310   *litp = 0;
1311   *minus_litp = 0;
1312
1313   /* Strip any conversions that don't change the machine mode or signedness.  */
1314   STRIP_SIGN_NOPS (in);
1315
1316   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1317     *litp = in;
1318   else if (TREE_CODE (in) == code
1319            || (! FLOAT_TYPE_P (TREE_TYPE (in))
1320                /* We can associate addition and subtraction together (even
1321                   though the C standard doesn't say so) for integers because
1322                   the value is not affected.  For reals, the value might be
1323                   affected, so we can't.  */
1324                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1325                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1326     {
1327       tree op0 = TREE_OPERAND (in, 0);
1328       tree op1 = TREE_OPERAND (in, 1);
1329       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1330       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1331
1332       /* First see if either of the operands is a literal, then a constant.  */
1333       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1334         *litp = op0, op0 = 0;
1335       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1336         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1337
1338       if (op0 != 0 && TREE_CONSTANT (op0))
1339         *conp = op0, op0 = 0;
1340       else if (op1 != 0 && TREE_CONSTANT (op1))
1341         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1342
1343       /* If we haven't dealt with either operand, this is not a case we can
1344          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1345       if (op0 != 0 && op1 != 0)
1346         var = in;
1347       else if (op0 != 0)
1348         var = op0;
1349       else
1350         var = op1, neg_var_p = neg1_p;
1351
1352       /* Now do any needed negations.  */
1353       if (neg_litp_p)
1354         *minus_litp = *litp, *litp = 0;
1355       if (neg_conp_p)
1356         *conp = negate_expr (*conp);
1357       if (neg_var_p)
1358         var = negate_expr (var);
1359     }
1360   else if (TREE_CONSTANT (in))
1361     *conp = in;
1362   else
1363     var = in;
1364
1365   if (negate_p)
1366     {
1367       if (*litp)
1368         *minus_litp = *litp, *litp = 0;
1369       else if (*minus_litp)
1370         *litp = *minus_litp, *minus_litp = 0;
1371       *conp = negate_expr (*conp);
1372       var = negate_expr (var);
1373     }
1374
1375   return var;
1376 }
1377
1378 /* Re-associate trees split by the above function.  T1 and T2 are either
1379    expressions to associate or null.  Return the new expression, if any.  If
1380    we build an operation, do it in TYPE and with CODE.  */
1381
1382 static tree
1383 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1384 {
1385   if (t1 == 0)
1386     return t2;
1387   else if (t2 == 0)
1388     return t1;
1389
1390   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1391      try to fold this since we will have infinite recursion.  But do
1392      deal with any NEGATE_EXPRs.  */
1393   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1394       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1395     {
1396       if (code == PLUS_EXPR)
1397         {
1398           if (TREE_CODE (t1) == NEGATE_EXPR)
1399             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1400                            fold_convert (type, TREE_OPERAND (t1, 0)));
1401           else if (TREE_CODE (t2) == NEGATE_EXPR)
1402             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1403                            fold_convert (type, TREE_OPERAND (t2, 0)));
1404           else if (integer_zerop (t2))
1405             return fold_convert (type, t1);
1406         }
1407       else if (code == MINUS_EXPR)
1408         {
1409           if (integer_zerop (t2))
1410             return fold_convert (type, t1);
1411         }
1412
1413       return build2 (code, type, fold_convert (type, t1),
1414                      fold_convert (type, t2));
1415     }
1416
1417   return fold_build2 (code, type, fold_convert (type, t1),
1418                       fold_convert (type, t2));
1419 }
1420 \f
1421 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1422    for use in int_const_binop, size_binop and size_diffop.  */
1423
1424 static bool
1425 int_binop_types_match_p (enum tree_code code, tree type1, tree type2)
1426 {
1427   if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1428     return false;
1429   if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1430     return false;
1431
1432   switch (code)
1433     {
1434     case LSHIFT_EXPR:
1435     case RSHIFT_EXPR:
1436     case LROTATE_EXPR:
1437     case RROTATE_EXPR:
1438       return true;
1439
1440     default:
1441       break;
1442     }
1443
1444   return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1445          && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1446          && TYPE_MODE (type1) == TYPE_MODE (type2);
1447 }
1448
1449
1450 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1451    to produce a new constant.  Return NULL_TREE if we don't know how
1452    to evaluate CODE at compile-time.
1453
1454    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1455
1456 tree
1457 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1458 {
1459   unsigned HOST_WIDE_INT int1l, int2l;
1460   HOST_WIDE_INT int1h, int2h;
1461   unsigned HOST_WIDE_INT low;
1462   HOST_WIDE_INT hi;
1463   unsigned HOST_WIDE_INT garbagel;
1464   HOST_WIDE_INT garbageh;
1465   tree t;
1466   tree type = TREE_TYPE (arg1);
1467   int uns = TYPE_UNSIGNED (type);
1468   int is_sizetype
1469     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1470   int overflow = 0;
1471
1472   int1l = TREE_INT_CST_LOW (arg1);
1473   int1h = TREE_INT_CST_HIGH (arg1);
1474   int2l = TREE_INT_CST_LOW (arg2);
1475   int2h = TREE_INT_CST_HIGH (arg2);
1476
1477   switch (code)
1478     {
1479     case BIT_IOR_EXPR:
1480       low = int1l | int2l, hi = int1h | int2h;
1481       break;
1482
1483     case BIT_XOR_EXPR:
1484       low = int1l ^ int2l, hi = int1h ^ int2h;
1485       break;
1486
1487     case BIT_AND_EXPR:
1488       low = int1l & int2l, hi = int1h & int2h;
1489       break;
1490
1491     case RSHIFT_EXPR:
1492       int2l = -int2l;
1493     case LSHIFT_EXPR:
1494       /* It's unclear from the C standard whether shifts can overflow.
1495          The following code ignores overflow; perhaps a C standard
1496          interpretation ruling is needed.  */
1497       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1498                      &low, &hi, !uns);
1499       break;
1500
1501     case RROTATE_EXPR:
1502       int2l = - int2l;
1503     case LROTATE_EXPR:
1504       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1505                       &low, &hi);
1506       break;
1507
1508     case PLUS_EXPR:
1509       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1510       break;
1511
1512     case MINUS_EXPR:
1513       neg_double (int2l, int2h, &low, &hi);
1514       add_double (int1l, int1h, low, hi, &low, &hi);
1515       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1516       break;
1517
1518     case MULT_EXPR:
1519       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1520       break;
1521
1522     case TRUNC_DIV_EXPR:
1523     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1524     case EXACT_DIV_EXPR:
1525       /* This is a shortcut for a common special case.  */
1526       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1527           && ! TREE_CONSTANT_OVERFLOW (arg1)
1528           && ! TREE_CONSTANT_OVERFLOW (arg2)
1529           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1530         {
1531           if (code == CEIL_DIV_EXPR)
1532             int1l += int2l - 1;
1533
1534           low = int1l / int2l, hi = 0;
1535           break;
1536         }
1537
1538       /* ... fall through ...  */
1539
1540     case ROUND_DIV_EXPR:
1541       if (int2h == 0 && int2l == 0)
1542         return NULL_TREE;
1543       if (int2h == 0 && int2l == 1)
1544         {
1545           low = int1l, hi = int1h;
1546           break;
1547         }
1548       if (int1l == int2l && int1h == int2h
1549           && ! (int1l == 0 && int1h == 0))
1550         {
1551           low = 1, hi = 0;
1552           break;
1553         }
1554       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1555                                        &low, &hi, &garbagel, &garbageh);
1556       break;
1557
1558     case TRUNC_MOD_EXPR:
1559     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1560       /* This is a shortcut for a common special case.  */
1561       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1562           && ! TREE_CONSTANT_OVERFLOW (arg1)
1563           && ! TREE_CONSTANT_OVERFLOW (arg2)
1564           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1565         {
1566           if (code == CEIL_MOD_EXPR)
1567             int1l += int2l - 1;
1568           low = int1l % int2l, hi = 0;
1569           break;
1570         }
1571
1572       /* ... fall through ...  */
1573
1574     case ROUND_MOD_EXPR:
1575       if (int2h == 0 && int2l == 0)
1576         return NULL_TREE;
1577       overflow = div_and_round_double (code, uns,
1578                                        int1l, int1h, int2l, int2h,
1579                                        &garbagel, &garbageh, &low, &hi);
1580       break;
1581
1582     case MIN_EXPR:
1583     case MAX_EXPR:
1584       if (uns)
1585         low = (((unsigned HOST_WIDE_INT) int1h
1586                 < (unsigned HOST_WIDE_INT) int2h)
1587                || (((unsigned HOST_WIDE_INT) int1h
1588                     == (unsigned HOST_WIDE_INT) int2h)
1589                    && int1l < int2l));
1590       else
1591         low = (int1h < int2h
1592                || (int1h == int2h && int1l < int2l));
1593
1594       if (low == (code == MIN_EXPR))
1595         low = int1l, hi = int1h;
1596       else
1597         low = int2l, hi = int2h;
1598       break;
1599
1600     default:
1601       return NULL_TREE;
1602     }
1603
1604   t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1605
1606   if (notrunc)
1607     {
1608       /* Propagate overflow flags ourselves.  */
1609       if (((!uns || is_sizetype) && overflow)
1610           | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1611         {
1612           t = copy_node (t);
1613           TREE_OVERFLOW (t) = 1;
1614           TREE_CONSTANT_OVERFLOW (t) = 1;
1615         }
1616       else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1617         {
1618           t = copy_node (t);
1619           TREE_CONSTANT_OVERFLOW (t) = 1;
1620         }
1621     }
1622   else
1623     t = force_fit_type (t, 1,
1624                         ((!uns || is_sizetype) && overflow)
1625                         | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2),
1626                         TREE_CONSTANT_OVERFLOW (arg1)
1627                         | TREE_CONSTANT_OVERFLOW (arg2));
1628
1629   return t;
1630 }
1631
1632 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1633    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1634    are the same kind of constant and the same machine mode.  Return zero if
1635    combining the constants is not allowed in the current operating mode.
1636
1637    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1638
1639 static tree
1640 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1641 {
1642   /* Sanity check for the recursive cases.  */
1643   if (!arg1 || !arg2)
1644     return NULL_TREE;
1645
1646   STRIP_NOPS (arg1);
1647   STRIP_NOPS (arg2);
1648
1649   if (TREE_CODE (arg1) == INTEGER_CST)
1650     return int_const_binop (code, arg1, arg2, notrunc);
1651
1652   if (TREE_CODE (arg1) == REAL_CST)
1653     {
1654       enum machine_mode mode;
1655       REAL_VALUE_TYPE d1;
1656       REAL_VALUE_TYPE d2;
1657       REAL_VALUE_TYPE value;
1658       REAL_VALUE_TYPE result;
1659       bool inexact;
1660       tree t, type;
1661
1662       /* The following codes are handled by real_arithmetic.  */
1663       switch (code)
1664         {
1665         case PLUS_EXPR:
1666         case MINUS_EXPR:
1667         case MULT_EXPR:
1668         case RDIV_EXPR:
1669         case MIN_EXPR:
1670         case MAX_EXPR:
1671           break;
1672
1673         default:
1674           return NULL_TREE;
1675         }
1676
1677       d1 = TREE_REAL_CST (arg1);
1678       d2 = TREE_REAL_CST (arg2);
1679
1680       type = TREE_TYPE (arg1);
1681       mode = TYPE_MODE (type);
1682
1683       /* Don't perform operation if we honor signaling NaNs and
1684          either operand is a NaN.  */
1685       if (HONOR_SNANS (mode)
1686           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1687         return NULL_TREE;
1688
1689       /* Don't perform operation if it would raise a division
1690          by zero exception.  */
1691       if (code == RDIV_EXPR
1692           && REAL_VALUES_EQUAL (d2, dconst0)
1693           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1694         return NULL_TREE;
1695
1696       /* If either operand is a NaN, just return it.  Otherwise, set up
1697          for floating-point trap; we return an overflow.  */
1698       if (REAL_VALUE_ISNAN (d1))
1699         return arg1;
1700       else if (REAL_VALUE_ISNAN (d2))
1701         return arg2;
1702
1703       inexact = real_arithmetic (&value, code, &d1, &d2);
1704       real_convert (&result, mode, &value);
1705
1706       /* Don't constant fold this floating point operation if
1707          the result has overflowed and flag_trapping_math.  */
1708       if (flag_trapping_math
1709           && MODE_HAS_INFINITIES (mode)
1710           && REAL_VALUE_ISINF (result)
1711           && !REAL_VALUE_ISINF (d1)
1712           && !REAL_VALUE_ISINF (d2))
1713         return NULL_TREE;
1714
1715       /* Don't constant fold this floating point operation if the
1716          result may dependent upon the run-time rounding mode and
1717          flag_rounding_math is set, or if GCC's software emulation
1718          is unable to accurately represent the result.  */
1719       if ((flag_rounding_math
1720            || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1721                && !flag_unsafe_math_optimizations))
1722           && (inexact || !real_identical (&result, &value)))
1723         return NULL_TREE;
1724
1725       t = build_real (type, result);
1726
1727       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1728       TREE_CONSTANT_OVERFLOW (t)
1729         = TREE_OVERFLOW (t)
1730           | TREE_CONSTANT_OVERFLOW (arg1)
1731           | TREE_CONSTANT_OVERFLOW (arg2);
1732       return t;
1733     }
1734
1735   if (TREE_CODE (arg1) == COMPLEX_CST)
1736     {
1737       tree type = TREE_TYPE (arg1);
1738       tree r1 = TREE_REALPART (arg1);
1739       tree i1 = TREE_IMAGPART (arg1);
1740       tree r2 = TREE_REALPART (arg2);
1741       tree i2 = TREE_IMAGPART (arg2);
1742       tree real, imag;
1743
1744       switch (code)
1745         {
1746         case PLUS_EXPR:
1747         case MINUS_EXPR:
1748           real = const_binop (code, r1, r2, notrunc);
1749           imag = const_binop (code, i1, i2, notrunc);
1750           break;
1751
1752         case MULT_EXPR:
1753           real = const_binop (MINUS_EXPR,
1754                               const_binop (MULT_EXPR, r1, r2, notrunc),
1755                               const_binop (MULT_EXPR, i1, i2, notrunc),
1756                               notrunc);
1757           imag = const_binop (PLUS_EXPR,
1758                               const_binop (MULT_EXPR, r1, i2, notrunc),
1759                               const_binop (MULT_EXPR, i1, r2, notrunc),
1760                               notrunc);
1761           break;
1762
1763         case RDIV_EXPR:
1764           {
1765             tree magsquared
1766               = const_binop (PLUS_EXPR,
1767                              const_binop (MULT_EXPR, r2, r2, notrunc),
1768                              const_binop (MULT_EXPR, i2, i2, notrunc),
1769                              notrunc);
1770             tree t1
1771               = const_binop (PLUS_EXPR,
1772                              const_binop (MULT_EXPR, r1, r2, notrunc),
1773                              const_binop (MULT_EXPR, i1, i2, notrunc),
1774                              notrunc);
1775             tree t2
1776               = const_binop (MINUS_EXPR,
1777                              const_binop (MULT_EXPR, i1, r2, notrunc),
1778                              const_binop (MULT_EXPR, r1, i2, notrunc),
1779                              notrunc);
1780
1781             if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1782               code = TRUNC_DIV_EXPR;
1783
1784             real = const_binop (code, t1, magsquared, notrunc);
1785             imag = const_binop (code, t2, magsquared, notrunc);
1786           }
1787           break;
1788
1789         default:
1790           return NULL_TREE;
1791         }
1792
1793       if (real && imag)
1794         return build_complex (type, real, imag);
1795     }
1796
1797   return NULL_TREE;
1798 }
1799
1800 /* Create a size type INT_CST node with NUMBER sign extended.  KIND
1801    indicates which particular sizetype to create.  */
1802
1803 tree
1804 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1805 {
1806   return build_int_cst (sizetype_tab[(int) kind], number);
1807 }
1808 \f
1809 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1810    is a tree code.  The type of the result is taken from the operands.
1811    Both must be equivalent integer types, ala int_binop_types_match_p.
1812    If the operands are constant, so is the result.  */
1813
1814 tree
1815 size_binop (enum tree_code code, tree arg0, tree arg1)
1816 {
1817   tree type = TREE_TYPE (arg0);
1818
1819   if (arg0 == error_mark_node || arg1 == error_mark_node)
1820     return error_mark_node;
1821
1822   gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1823                                        TREE_TYPE (arg1)));
1824
1825   /* Handle the special case of two integer constants faster.  */
1826   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1827     {
1828       /* And some specific cases even faster than that.  */
1829       if (code == PLUS_EXPR && integer_zerop (arg0))
1830         return arg1;
1831       else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1832                && integer_zerop (arg1))
1833         return arg0;
1834       else if (code == MULT_EXPR && integer_onep (arg0))
1835         return arg1;
1836
1837       /* Handle general case of two integer constants.  */
1838       return int_const_binop (code, arg0, arg1, 0);
1839     }
1840
1841   return fold_build2 (code, type, arg0, arg1);
1842 }
1843
1844 /* Given two values, either both of sizetype or both of bitsizetype,
1845    compute the difference between the two values.  Return the value
1846    in signed type corresponding to the type of the operands.  */
1847
1848 tree
1849 size_diffop (tree arg0, tree arg1)
1850 {
1851   tree type = TREE_TYPE (arg0);
1852   tree ctype;
1853
1854   gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1855                                        TREE_TYPE (arg1)));
1856
1857   /* If the type is already signed, just do the simple thing.  */
1858   if (!TYPE_UNSIGNED (type))
1859     return size_binop (MINUS_EXPR, arg0, arg1);
1860
1861   if (type == sizetype)
1862     ctype = ssizetype;
1863   else if (type == bitsizetype)
1864     ctype = sbitsizetype;
1865   else
1866     ctype = lang_hooks.types.signed_type (type);
1867
1868   /* If either operand is not a constant, do the conversions to the signed
1869      type and subtract.  The hardware will do the right thing with any
1870      overflow in the subtraction.  */
1871   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1872     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1873                        fold_convert (ctype, arg1));
1874
1875   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1876      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1877      overflow) and negate (which can't either).  Special-case a result
1878      of zero while we're here.  */
1879   if (tree_int_cst_equal (arg0, arg1))
1880     return build_int_cst (ctype, 0);
1881   else if (tree_int_cst_lt (arg1, arg0))
1882     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1883   else
1884     return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
1885                        fold_convert (ctype, size_binop (MINUS_EXPR,
1886                                                         arg1, arg0)));
1887 }
1888 \f
1889 /* A subroutine of fold_convert_const handling conversions of an
1890    INTEGER_CST to another integer type.  */
1891
1892 static tree
1893 fold_convert_const_int_from_int (tree type, tree arg1)
1894 {
1895   tree t;
1896
1897   /* Given an integer constant, make new constant with new type,
1898      appropriately sign-extended or truncated.  */
1899   t = build_int_cst_wide (type, TREE_INT_CST_LOW (arg1),
1900                           TREE_INT_CST_HIGH (arg1));
1901
1902   t = force_fit_type (t,
1903                       /* Don't set the overflow when
1904                          converting a pointer  */
1905                       !POINTER_TYPE_P (TREE_TYPE (arg1)),
1906                       (TREE_INT_CST_HIGH (arg1) < 0
1907                        && (TYPE_UNSIGNED (type)
1908                            < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1909                       | TREE_OVERFLOW (arg1),
1910                       TREE_CONSTANT_OVERFLOW (arg1));
1911
1912   return t;
1913 }
1914
1915 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1916    to an integer type.  */
1917
1918 static tree
1919 fold_convert_const_int_from_real (enum tree_code code, tree type, tree arg1)
1920 {
1921   int overflow = 0;
1922   tree t;
1923
1924   /* The following code implements the floating point to integer
1925      conversion rules required by the Java Language Specification,
1926      that IEEE NaNs are mapped to zero and values that overflow
1927      the target precision saturate, i.e. values greater than
1928      INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1929      are mapped to INT_MIN.  These semantics are allowed by the
1930      C and C++ standards that simply state that the behavior of
1931      FP-to-integer conversion is unspecified upon overflow.  */
1932
1933   HOST_WIDE_INT high, low;
1934   REAL_VALUE_TYPE r;
1935   REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1936
1937   switch (code)
1938     {
1939     case FIX_TRUNC_EXPR:
1940       real_trunc (&r, VOIDmode, &x);
1941       break;
1942
1943     default:
1944       gcc_unreachable ();
1945     }
1946
1947   /* If R is NaN, return zero and show we have an overflow.  */
1948   if (REAL_VALUE_ISNAN (r))
1949     {
1950       overflow = 1;
1951       high = 0;
1952       low = 0;
1953     }
1954
1955   /* See if R is less than the lower bound or greater than the
1956      upper bound.  */
1957
1958   if (! overflow)
1959     {
1960       tree lt = TYPE_MIN_VALUE (type);
1961       REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1962       if (REAL_VALUES_LESS (r, l))
1963         {
1964           overflow = 1;
1965           high = TREE_INT_CST_HIGH (lt);
1966           low = TREE_INT_CST_LOW (lt);
1967         }
1968     }
1969
1970   if (! overflow)
1971     {
1972       tree ut = TYPE_MAX_VALUE (type);
1973       if (ut)
1974         {
1975           REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1976           if (REAL_VALUES_LESS (u, r))
1977             {
1978               overflow = 1;
1979               high = TREE_INT_CST_HIGH (ut);
1980               low = TREE_INT_CST_LOW (ut);
1981             }
1982         }
1983     }
1984
1985   if (! overflow)
1986     REAL_VALUE_TO_INT (&low, &high, r);
1987
1988   t = build_int_cst_wide (type, low, high);
1989
1990   t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg1),
1991                       TREE_CONSTANT_OVERFLOW (arg1));
1992   return t;
1993 }
1994
1995 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1996    to another floating point type.  */
1997
1998 static tree
1999 fold_convert_const_real_from_real (tree type, tree arg1)
2000 {
2001   REAL_VALUE_TYPE value;
2002   tree t;
2003
2004   real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2005   t = build_real (type, value);
2006
2007   TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2008   TREE_CONSTANT_OVERFLOW (t)
2009     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2010   return t;
2011 }
2012
2013 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2014    type TYPE.  If no simplification can be done return NULL_TREE.  */
2015
2016 static tree
2017 fold_convert_const (enum tree_code code, tree type, tree arg1)
2018 {
2019   if (TREE_TYPE (arg1) == type)
2020     return arg1;
2021
2022   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2023     {
2024       if (TREE_CODE (arg1) == INTEGER_CST)
2025         return fold_convert_const_int_from_int (type, arg1);
2026       else if (TREE_CODE (arg1) == REAL_CST)
2027         return fold_convert_const_int_from_real (code, type, arg1);
2028     }
2029   else if (TREE_CODE (type) == REAL_TYPE)
2030     {
2031       if (TREE_CODE (arg1) == INTEGER_CST)
2032         return build_real_from_int_cst (type, arg1);
2033       if (TREE_CODE (arg1) == REAL_CST)
2034         return fold_convert_const_real_from_real (type, arg1);
2035     }
2036   return NULL_TREE;
2037 }
2038
2039 /* Construct a vector of zero elements of vector type TYPE.  */
2040
2041 static tree
2042 build_zero_vector (tree type)
2043 {
2044   tree elem, list;
2045   int i, units;
2046
2047   elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2048   units = TYPE_VECTOR_SUBPARTS (type);
2049   
2050   list = NULL_TREE;
2051   for (i = 0; i < units; i++)
2052     list = tree_cons (NULL_TREE, elem, list);
2053   return build_vector (type, list);
2054 }
2055
2056 /* Convert expression ARG to type TYPE.  Used by the middle-end for
2057    simple conversions in preference to calling the front-end's convert.  */
2058
2059 tree
2060 fold_convert (tree type, tree arg)
2061 {
2062   tree orig = TREE_TYPE (arg);
2063   tree tem;
2064
2065   if (type == orig)
2066     return arg;
2067
2068   if (TREE_CODE (arg) == ERROR_MARK
2069       || TREE_CODE (type) == ERROR_MARK
2070       || TREE_CODE (orig) == ERROR_MARK)
2071     return error_mark_node;
2072
2073   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)
2074       || lang_hooks.types_compatible_p (TYPE_MAIN_VARIANT (type),
2075                                         TYPE_MAIN_VARIANT (orig)))
2076     return fold_build1 (NOP_EXPR, type, arg);
2077
2078   switch (TREE_CODE (type))
2079     {
2080     case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2081     case POINTER_TYPE: case REFERENCE_TYPE:
2082     case OFFSET_TYPE:
2083       if (TREE_CODE (arg) == INTEGER_CST)
2084         {
2085           tem = fold_convert_const (NOP_EXPR, type, arg);
2086           if (tem != NULL_TREE)
2087             return tem;
2088         }
2089       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2090           || TREE_CODE (orig) == OFFSET_TYPE)
2091         return fold_build1 (NOP_EXPR, type, arg);
2092       if (TREE_CODE (orig) == COMPLEX_TYPE)
2093         {
2094           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2095           return fold_convert (type, tem);
2096         }
2097       gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2098                   && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2099       return fold_build1 (NOP_EXPR, type, arg);
2100
2101     case REAL_TYPE:
2102       if (TREE_CODE (arg) == INTEGER_CST)
2103         {
2104           tem = fold_convert_const (FLOAT_EXPR, type, arg);
2105           if (tem != NULL_TREE)
2106             return tem;
2107         }
2108       else if (TREE_CODE (arg) == REAL_CST)
2109         {
2110           tem = fold_convert_const (NOP_EXPR, type, arg);
2111           if (tem != NULL_TREE)
2112             return tem;
2113         }
2114
2115       switch (TREE_CODE (orig))
2116         {
2117         case INTEGER_TYPE:
2118         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2119         case POINTER_TYPE: case REFERENCE_TYPE:
2120           return fold_build1 (FLOAT_EXPR, type, arg);
2121
2122         case REAL_TYPE:
2123           return fold_build1 (NOP_EXPR, type, arg);
2124
2125         case COMPLEX_TYPE:
2126           tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2127           return fold_convert (type, tem);
2128
2129         default:
2130           gcc_unreachable ();
2131         }
2132
2133     case COMPLEX_TYPE:
2134       switch (TREE_CODE (orig))
2135         {
2136         case INTEGER_TYPE:
2137         case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2138         case POINTER_TYPE: case REFERENCE_TYPE:
2139         case REAL_TYPE:
2140           return build2 (COMPLEX_EXPR, type,
2141                          fold_convert (TREE_TYPE (type), arg),
2142                          fold_convert (TREE_TYPE (type), integer_zero_node));
2143         case COMPLEX_TYPE:
2144           {
2145             tree rpart, ipart;
2146
2147             if (TREE_CODE (arg) == COMPLEX_EXPR)
2148               {
2149                 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2150                 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2151                 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2152               }
2153
2154             arg = save_expr (arg);
2155             rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2156             ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2157             rpart = fold_convert (TREE_TYPE (type), rpart);
2158             ipart = fold_convert (TREE_TYPE (type), ipart);
2159             return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2160           }
2161
2162         default:
2163           gcc_unreachable ();
2164         }
2165
2166     case VECTOR_TYPE:
2167       if (integer_zerop (arg))
2168         return build_zero_vector (type);
2169       gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2170       gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2171                   || TREE_CODE (orig) == VECTOR_TYPE);
2172       return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2173
2174     case VOID_TYPE:
2175       tem = fold_ignored_result (arg);
2176       if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2177         return tem;
2178       return fold_build1 (NOP_EXPR, type, tem);
2179
2180     default:
2181       gcc_unreachable ();
2182     }
2183 }
2184 \f
2185 /* Return false if expr can be assumed not to be an lvalue, true
2186    otherwise.  */
2187
2188 static bool
2189 maybe_lvalue_p (tree x)
2190 {
2191   /* We only need to wrap lvalue tree codes.  */
2192   switch (TREE_CODE (x))
2193   {
2194   case VAR_DECL:
2195   case PARM_DECL:
2196   case RESULT_DECL:
2197   case LABEL_DECL:
2198   case FUNCTION_DECL:
2199   case SSA_NAME:
2200
2201   case COMPONENT_REF:
2202   case INDIRECT_REF:
2203   case ALIGN_INDIRECT_REF:
2204   case MISALIGNED_INDIRECT_REF:
2205   case ARRAY_REF:
2206   case ARRAY_RANGE_REF:
2207   case BIT_FIELD_REF:
2208   case OBJ_TYPE_REF:
2209
2210   case REALPART_EXPR:
2211   case IMAGPART_EXPR:
2212   case PREINCREMENT_EXPR:
2213   case PREDECREMENT_EXPR:
2214   case SAVE_EXPR:
2215   case TRY_CATCH_EXPR:
2216   case WITH_CLEANUP_EXPR:
2217   case COMPOUND_EXPR:
2218   case MODIFY_EXPR:
2219   case GIMPLE_MODIFY_STMT:
2220   case TARGET_EXPR:
2221   case COND_EXPR:
2222   case BIND_EXPR:
2223   case MIN_EXPR:
2224   case MAX_EXPR:
2225     break;
2226
2227   default:
2228     /* Assume the worst for front-end tree codes.  */
2229     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2230       break;
2231     return false;
2232   }
2233
2234   return true;
2235 }
2236
2237 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2238
2239 tree
2240 non_lvalue (tree x)
2241 {
2242   /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2243      us.  */
2244   if (in_gimple_form)
2245     return x;
2246
2247   if (! maybe_lvalue_p (x))
2248     return x;
2249   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2250 }
2251
2252 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2253    Zero means allow extended lvalues.  */
2254
2255 int pedantic_lvalues;
2256
2257 /* When pedantic, return an expr equal to X but certainly not valid as a
2258    pedantic lvalue.  Otherwise, return X.  */
2259
2260 static tree
2261 pedantic_non_lvalue (tree x)
2262 {
2263   if (pedantic_lvalues)
2264     return non_lvalue (x);
2265   else
2266     return x;
2267 }
2268 \f
2269 /* Given a tree comparison code, return the code that is the logical inverse
2270    of the given code.  It is not safe to do this for floating-point
2271    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2272    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2273
2274 enum tree_code
2275 invert_tree_comparison (enum tree_code code, bool honor_nans)
2276 {
2277   if (honor_nans && flag_trapping_math)
2278     return ERROR_MARK;
2279
2280   switch (code)
2281     {
2282     case EQ_EXPR:
2283       return NE_EXPR;
2284     case NE_EXPR:
2285       return EQ_EXPR;
2286     case GT_EXPR:
2287       return honor_nans ? UNLE_EXPR : LE_EXPR;
2288     case GE_EXPR:
2289       return honor_nans ? UNLT_EXPR : LT_EXPR;
2290     case LT_EXPR:
2291       return honor_nans ? UNGE_EXPR : GE_EXPR;
2292     case LE_EXPR:
2293       return honor_nans ? UNGT_EXPR : GT_EXPR;
2294     case LTGT_EXPR:
2295       return UNEQ_EXPR;
2296     case UNEQ_EXPR:
2297       return LTGT_EXPR;
2298     case UNGT_EXPR:
2299       return LE_EXPR;
2300     case UNGE_EXPR:
2301       return LT_EXPR;
2302     case UNLT_EXPR:
2303       return GE_EXPR;
2304     case UNLE_EXPR:
2305       return GT_EXPR;
2306     case ORDERED_EXPR:
2307       return UNORDERED_EXPR;
2308     case UNORDERED_EXPR:
2309       return ORDERED_EXPR;
2310     default:
2311       gcc_unreachable ();
2312     }
2313 }
2314
2315 /* Similar, but return the comparison that results if the operands are
2316    swapped.  This is safe for floating-point.  */
2317
2318 enum tree_code
2319 swap_tree_comparison (enum tree_code code)
2320 {
2321   switch (code)
2322     {
2323     case EQ_EXPR:
2324     case NE_EXPR:
2325     case ORDERED_EXPR:
2326     case UNORDERED_EXPR:
2327     case LTGT_EXPR:
2328     case UNEQ_EXPR:
2329       return code;
2330     case GT_EXPR:
2331       return LT_EXPR;
2332     case GE_EXPR:
2333       return LE_EXPR;
2334     case LT_EXPR:
2335       return GT_EXPR;
2336     case LE_EXPR:
2337       return GE_EXPR;
2338     case UNGT_EXPR:
2339       return UNLT_EXPR;
2340     case UNGE_EXPR:
2341       return UNLE_EXPR;
2342     case UNLT_EXPR:
2343       return UNGT_EXPR;
2344     case UNLE_EXPR:
2345       return UNGE_EXPR;
2346     default:
2347       gcc_unreachable ();
2348     }
2349 }
2350
2351
2352 /* Convert a comparison tree code from an enum tree_code representation
2353    into a compcode bit-based encoding.  This function is the inverse of
2354    compcode_to_comparison.  */
2355
2356 static enum comparison_code
2357 comparison_to_compcode (enum tree_code code)
2358 {
2359   switch (code)
2360     {
2361     case LT_EXPR:
2362       return COMPCODE_LT;
2363     case EQ_EXPR:
2364       return COMPCODE_EQ;
2365     case LE_EXPR:
2366       return COMPCODE_LE;
2367     case GT_EXPR:
2368       return COMPCODE_GT;
2369     case NE_EXPR:
2370       return COMPCODE_NE;
2371     case GE_EXPR:
2372       return COMPCODE_GE;
2373     case ORDERED_EXPR:
2374       return COMPCODE_ORD;
2375     case UNORDERED_EXPR:
2376       return COMPCODE_UNORD;
2377     case UNLT_EXPR:
2378       return COMPCODE_UNLT;
2379     case UNEQ_EXPR:
2380       return COMPCODE_UNEQ;
2381     case UNLE_EXPR:
2382       return COMPCODE_UNLE;
2383     case UNGT_EXPR:
2384       return COMPCODE_UNGT;
2385     case LTGT_EXPR:
2386       return COMPCODE_LTGT;
2387     case UNGE_EXPR:
2388       return COMPCODE_UNGE;
2389     default:
2390       gcc_unreachable ();
2391     }
2392 }
2393
2394 /* Convert a compcode bit-based encoding of a comparison operator back
2395    to GCC's enum tree_code representation.  This function is the
2396    inverse of comparison_to_compcode.  */
2397
2398 static enum tree_code
2399 compcode_to_comparison (enum comparison_code code)
2400 {
2401   switch (code)
2402     {
2403     case COMPCODE_LT:
2404       return LT_EXPR;
2405     case COMPCODE_EQ:
2406       return EQ_EXPR;
2407     case COMPCODE_LE:
2408       return LE_EXPR;
2409     case COMPCODE_GT:
2410       return GT_EXPR;
2411     case COMPCODE_NE:
2412       return NE_EXPR;
2413     case COMPCODE_GE:
2414       return GE_EXPR;
2415     case COMPCODE_ORD:
2416       return ORDERED_EXPR;
2417     case COMPCODE_UNORD:
2418       return UNORDERED_EXPR;
2419     case COMPCODE_UNLT:
2420       return UNLT_EXPR;
2421     case COMPCODE_UNEQ:
2422       return UNEQ_EXPR;
2423     case COMPCODE_UNLE:
2424       return UNLE_EXPR;
2425     case COMPCODE_UNGT:
2426       return UNGT_EXPR;
2427     case COMPCODE_LTGT:
2428       return LTGT_EXPR;
2429     case COMPCODE_UNGE:
2430       return UNGE_EXPR;
2431     default:
2432       gcc_unreachable ();
2433     }
2434 }
2435
2436 /* Return a tree for the comparison which is the combination of
2437    doing the AND or OR (depending on CODE) of the two operations LCODE
2438    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2439    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2440    if this makes the transformation invalid.  */
2441
2442 tree
2443 combine_comparisons (enum tree_code code, enum tree_code lcode,
2444                      enum tree_code rcode, tree truth_type,
2445                      tree ll_arg, tree lr_arg)
2446 {
2447   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2448   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2449   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2450   enum comparison_code compcode;
2451
2452   switch (code)
2453     {
2454     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2455       compcode = lcompcode & rcompcode;
2456       break;
2457
2458     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2459       compcode = lcompcode | rcompcode;
2460       break;
2461
2462     default:
2463       return NULL_TREE;
2464     }
2465
2466   if (!honor_nans)
2467     {
2468       /* Eliminate unordered comparisons, as well as LTGT and ORD
2469          which are not used unless the mode has NaNs.  */
2470       compcode &= ~COMPCODE_UNORD;
2471       if (compcode == COMPCODE_LTGT)
2472         compcode = COMPCODE_NE;
2473       else if (compcode == COMPCODE_ORD)
2474         compcode = COMPCODE_TRUE;
2475     }
2476    else if (flag_trapping_math)
2477      {
2478         /* Check that the original operation and the optimized ones will trap
2479            under the same condition.  */
2480         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2481                      && (lcompcode != COMPCODE_EQ)
2482                      && (lcompcode != COMPCODE_ORD);
2483         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2484                      && (rcompcode != COMPCODE_EQ)
2485                      && (rcompcode != COMPCODE_ORD);
2486         bool trap = (compcode & COMPCODE_UNORD) == 0
2487                     && (compcode != COMPCODE_EQ)
2488                     && (compcode != COMPCODE_ORD);
2489
2490         /* In a short-circuited boolean expression the LHS might be
2491            such that the RHS, if evaluated, will never trap.  For
2492            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2493            if neither x nor y is NaN.  (This is a mixed blessing: for
2494            example, the expression above will never trap, hence
2495            optimizing it to x < y would be invalid).  */
2496         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2497             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2498           rtrap = false;
2499
2500         /* If the comparison was short-circuited, and only the RHS
2501            trapped, we may now generate a spurious trap.  */
2502         if (rtrap && !ltrap
2503             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2504           return NULL_TREE;
2505
2506         /* If we changed the conditions that cause a trap, we lose.  */
2507         if ((ltrap || rtrap) != trap)
2508           return NULL_TREE;
2509       }
2510
2511   if (compcode == COMPCODE_TRUE)
2512     return constant_boolean_node (true, truth_type);
2513   else if (compcode == COMPCODE_FALSE)
2514     return constant_boolean_node (false, truth_type);
2515   else
2516     return fold_build2 (compcode_to_comparison (compcode),
2517                         truth_type, ll_arg, lr_arg);
2518 }
2519
2520 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2521
2522 static int
2523 truth_value_p (enum tree_code code)
2524 {
2525   return (TREE_CODE_CLASS (code) == tcc_comparison
2526           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2527           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2528           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2529 }
2530 \f
2531 /* Return nonzero if two operands (typically of the same tree node)
2532    are necessarily equal.  If either argument has side-effects this
2533    function returns zero.  FLAGS modifies behavior as follows:
2534
2535    If OEP_ONLY_CONST is set, only return nonzero for constants.
2536    This function tests whether the operands are indistinguishable;
2537    it does not test whether they are equal using C's == operation.
2538    The distinction is important for IEEE floating point, because
2539    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2540    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2541
2542    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2543    even though it may hold multiple values during a function.
2544    This is because a GCC tree node guarantees that nothing else is
2545    executed between the evaluation of its "operands" (which may often
2546    be evaluated in arbitrary order).  Hence if the operands themselves
2547    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2548    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2549    unset means assuming isochronic (or instantaneous) tree equivalence.
2550    Unless comparing arbitrary expression trees, such as from different
2551    statements, this flag can usually be left unset.
2552
2553    If OEP_PURE_SAME is set, then pure functions with identical arguments
2554    are considered the same.  It is used when the caller has other ways
2555    to ensure that global memory is unchanged in between.  */
2556
2557 int
2558 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2559 {
2560   /* If either is ERROR_MARK, they aren't equal.  */
2561   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2562     return 0;
2563
2564   /* If both types don't have the same signedness, then we can't consider
2565      them equal.  We must check this before the STRIP_NOPS calls
2566      because they may change the signedness of the arguments.  */
2567   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2568     return 0;
2569
2570   /* If both types don't have the same precision, then it is not safe
2571      to strip NOPs.  */
2572   if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2573     return 0;
2574
2575   STRIP_NOPS (arg0);
2576   STRIP_NOPS (arg1);
2577
2578   /* In case both args are comparisons but with different comparison
2579      code, try to swap the comparison operands of one arg to produce
2580      a match and compare that variant.  */
2581   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2582       && COMPARISON_CLASS_P (arg0)
2583       && COMPARISON_CLASS_P (arg1))
2584     {
2585       enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2586
2587       if (TREE_CODE (arg0) == swap_code)
2588         return operand_equal_p (TREE_OPERAND (arg0, 0),
2589                                 TREE_OPERAND (arg1, 1), flags)
2590                && operand_equal_p (TREE_OPERAND (arg0, 1),
2591                                    TREE_OPERAND (arg1, 0), flags);
2592     }
2593
2594   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2595       /* This is needed for conversions and for COMPONENT_REF.
2596          Might as well play it safe and always test this.  */
2597       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2598       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2599       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2600     return 0;
2601
2602   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2603      We don't care about side effects in that case because the SAVE_EXPR
2604      takes care of that for us. In all other cases, two expressions are
2605      equal if they have no side effects.  If we have two identical
2606      expressions with side effects that should be treated the same due
2607      to the only side effects being identical SAVE_EXPR's, that will
2608      be detected in the recursive calls below.  */
2609   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2610       && (TREE_CODE (arg0) == SAVE_EXPR
2611           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2612     return 1;
2613
2614   /* Next handle constant cases, those for which we can return 1 even
2615      if ONLY_CONST is set.  */
2616   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2617     switch (TREE_CODE (arg0))
2618       {
2619       case INTEGER_CST:
2620         return tree_int_cst_equal (arg0, arg1);
2621
2622       case REAL_CST:
2623         if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2624                                    TREE_REAL_CST (arg1)))
2625           return 1;
2626
2627         
2628         if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2629           {
2630             /* If we do not distinguish between signed and unsigned zero,
2631                consider them equal.  */
2632             if (real_zerop (arg0) && real_zerop (arg1))
2633               return 1;
2634           }
2635         return 0;
2636
2637       case VECTOR_CST:
2638         {
2639           tree v1, v2;
2640
2641           v1 = TREE_VECTOR_CST_ELTS (arg0);
2642           v2 = TREE_VECTOR_CST_ELTS (arg1);
2643           while (v1 && v2)
2644             {
2645               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2646                                     flags))
2647                 return 0;
2648               v1 = TREE_CHAIN (v1);
2649               v2 = TREE_CHAIN (v2);
2650             }
2651
2652           return v1 == v2;
2653         }
2654
2655       case COMPLEX_CST:
2656         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2657                                  flags)
2658                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2659                                     flags));
2660
2661       case STRING_CST:
2662         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2663                 && ! memcmp (TREE_STRING_POINTER (arg0),
2664                               TREE_STRING_POINTER (arg1),
2665                               TREE_STRING_LENGTH (arg0)));
2666
2667       case ADDR_EXPR:
2668         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2669                                 0);
2670       default:
2671         break;
2672       }
2673
2674   if (flags & OEP_ONLY_CONST)
2675     return 0;
2676
2677 /* Define macros to test an operand from arg0 and arg1 for equality and a
2678    variant that allows null and views null as being different from any
2679    non-null value.  In the latter case, if either is null, the both
2680    must be; otherwise, do the normal comparison.  */
2681 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2682                                     TREE_OPERAND (arg1, N), flags)
2683
2684 #define OP_SAME_WITH_NULL(N)                            \
2685   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2686    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2687
2688   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2689     {
2690     case tcc_unary:
2691       /* Two conversions are equal only if signedness and modes match.  */
2692       switch (TREE_CODE (arg0))
2693         {
2694         case NOP_EXPR:
2695         case CONVERT_EXPR:
2696         case FIX_TRUNC_EXPR:
2697           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2698               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2699             return 0;
2700           break;
2701         default:
2702           break;
2703         }
2704
2705       return OP_SAME (0);
2706
2707
2708     case tcc_comparison:
2709     case tcc_binary:
2710       if (OP_SAME (0) && OP_SAME (1))
2711         return 1;
2712
2713       /* For commutative ops, allow the other order.  */
2714       return (commutative_tree_code (TREE_CODE (arg0))
2715               && operand_equal_p (TREE_OPERAND (arg0, 0),
2716                                   TREE_OPERAND (arg1, 1), flags)
2717               && operand_equal_p (TREE_OPERAND (arg0, 1),
2718                                   TREE_OPERAND (arg1, 0), flags));
2719
2720     case tcc_reference:
2721       /* If either of the pointer (or reference) expressions we are
2722          dereferencing contain a side effect, these cannot be equal.  */
2723       if (TREE_SIDE_EFFECTS (arg0)
2724           || TREE_SIDE_EFFECTS (arg1))
2725         return 0;
2726
2727       switch (TREE_CODE (arg0))
2728         {
2729         case INDIRECT_REF:
2730         case ALIGN_INDIRECT_REF:
2731         case MISALIGNED_INDIRECT_REF:
2732         case REALPART_EXPR:
2733         case IMAGPART_EXPR:
2734           return OP_SAME (0);
2735
2736         case ARRAY_REF:
2737         case ARRAY_RANGE_REF:
2738           /* Operands 2 and 3 may be null.  */
2739           return (OP_SAME (0)
2740                   && OP_SAME (1)
2741                   && OP_SAME_WITH_NULL (2)
2742                   && OP_SAME_WITH_NULL (3));
2743
2744         case COMPONENT_REF:
2745           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2746              may be NULL when we're called to compare MEM_EXPRs.  */
2747           return OP_SAME_WITH_NULL (0)
2748                  && OP_SAME (1)
2749                  && OP_SAME_WITH_NULL (2);
2750
2751         case BIT_FIELD_REF:
2752           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2753
2754         default:
2755           return 0;
2756         }
2757
2758     case tcc_expression:
2759       switch (TREE_CODE (arg0))
2760         {
2761         case ADDR_EXPR:
2762         case TRUTH_NOT_EXPR:
2763           return OP_SAME (0);
2764
2765         case TRUTH_ANDIF_EXPR:
2766         case TRUTH_ORIF_EXPR:
2767           return OP_SAME (0) && OP_SAME (1);
2768
2769         case TRUTH_AND_EXPR:
2770         case TRUTH_OR_EXPR:
2771         case TRUTH_XOR_EXPR:
2772           if (OP_SAME (0) && OP_SAME (1))
2773             return 1;
2774
2775           /* Otherwise take into account this is a commutative operation.  */
2776           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2777                                    TREE_OPERAND (arg1, 1), flags)
2778                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2779                                       TREE_OPERAND (arg1, 0), flags));
2780
2781         case CALL_EXPR:
2782           /* If the CALL_EXPRs call different functions, then they
2783              clearly can not be equal.  */
2784           if (!OP_SAME (0))
2785             return 0;
2786
2787           {
2788             unsigned int cef = call_expr_flags (arg0);
2789             if (flags & OEP_PURE_SAME)
2790               cef &= ECF_CONST | ECF_PURE;
2791             else
2792               cef &= ECF_CONST;
2793             if (!cef)
2794               return 0;
2795           }
2796
2797           /* Now see if all the arguments are the same.  operand_equal_p
2798              does not handle TREE_LIST, so we walk the operands here
2799              feeding them to operand_equal_p.  */
2800           arg0 = TREE_OPERAND (arg0, 1);
2801           arg1 = TREE_OPERAND (arg1, 1);
2802           while (arg0 && arg1)
2803             {
2804               if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2805                                      flags))
2806                 return 0;
2807
2808               arg0 = TREE_CHAIN (arg0);
2809               arg1 = TREE_CHAIN (arg1);
2810             }
2811
2812           /* If we get here and both argument lists are exhausted
2813              then the CALL_EXPRs are equal.  */
2814           return ! (arg0 || arg1);
2815
2816         default:
2817           return 0;
2818         }
2819
2820     case tcc_declaration:
2821       /* Consider __builtin_sqrt equal to sqrt.  */
2822       return (TREE_CODE (arg0) == FUNCTION_DECL
2823               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2824               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2825               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2826
2827     default:
2828       return 0;
2829     }
2830
2831 #undef OP_SAME
2832 #undef OP_SAME_WITH_NULL
2833 }
2834 \f
2835 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2836    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2837
2838    When in doubt, return 0.  */
2839
2840 static int
2841 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2842 {
2843   int unsignedp1, unsignedpo;
2844   tree primarg0, primarg1, primother;
2845   unsigned int correct_width;
2846
2847   if (operand_equal_p (arg0, arg1, 0))
2848     return 1;
2849
2850   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2851       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2852     return 0;
2853
2854   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2855      and see if the inner values are the same.  This removes any
2856      signedness comparison, which doesn't matter here.  */
2857   primarg0 = arg0, primarg1 = arg1;
2858   STRIP_NOPS (primarg0);
2859   STRIP_NOPS (primarg1);
2860   if (operand_equal_p (primarg0, primarg1, 0))
2861     return 1;
2862
2863   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2864      actual comparison operand, ARG0.
2865
2866      First throw away any conversions to wider types
2867      already present in the operands.  */
2868
2869   primarg1 = get_narrower (arg1, &unsignedp1);
2870   primother = get_narrower (other, &unsignedpo);
2871
2872   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2873   if (unsignedp1 == unsignedpo
2874       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2875       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2876     {
2877       tree type = TREE_TYPE (arg0);
2878
2879       /* Make sure shorter operand is extended the right way
2880          to match the longer operand.  */
2881       primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2882                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2883
2884       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2885         return 1;
2886     }
2887
2888   return 0;
2889 }
2890 \f
2891 /* See if ARG is an expression that is either a comparison or is performing
2892    arithmetic on comparisons.  The comparisons must only be comparing
2893    two different values, which will be stored in *CVAL1 and *CVAL2; if
2894    they are nonzero it means that some operands have already been found.
2895    No variables may be used anywhere else in the expression except in the
2896    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2897    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2898
2899    If this is true, return 1.  Otherwise, return zero.  */
2900
2901 static int
2902 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2903 {
2904   enum tree_code code = TREE_CODE (arg);
2905   enum tree_code_class class = TREE_CODE_CLASS (code);
2906
2907   /* We can handle some of the tcc_expression cases here.  */
2908   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2909     class = tcc_unary;
2910   else if (class == tcc_expression
2911            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2912                || code == COMPOUND_EXPR))
2913     class = tcc_binary;
2914
2915   else if (class == tcc_expression && code == SAVE_EXPR
2916            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2917     {
2918       /* If we've already found a CVAL1 or CVAL2, this expression is
2919          two complex to handle.  */
2920       if (*cval1 || *cval2)
2921         return 0;
2922
2923       class = tcc_unary;
2924       *save_p = 1;
2925     }
2926
2927   switch (class)
2928     {
2929     case tcc_unary:
2930       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2931
2932     case tcc_binary:
2933       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2934               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2935                                       cval1, cval2, save_p));
2936
2937     case tcc_constant:
2938       return 1;
2939
2940     case tcc_expression:
2941       if (code == COND_EXPR)
2942         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2943                                      cval1, cval2, save_p)
2944                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2945                                         cval1, cval2, save_p)
2946                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2947                                         cval1, cval2, save_p));
2948       return 0;
2949
2950     case tcc_comparison:
2951       /* First see if we can handle the first operand, then the second.  For
2952          the second operand, we know *CVAL1 can't be zero.  It must be that
2953          one side of the comparison is each of the values; test for the
2954          case where this isn't true by failing if the two operands
2955          are the same.  */
2956
2957       if (operand_equal_p (TREE_OPERAND (arg, 0),
2958                            TREE_OPERAND (arg, 1), 0))
2959         return 0;
2960
2961       if (*cval1 == 0)
2962         *cval1 = TREE_OPERAND (arg, 0);
2963       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2964         ;
2965       else if (*cval2 == 0)
2966         *cval2 = TREE_OPERAND (arg, 0);
2967       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2968         ;
2969       else
2970         return 0;
2971
2972       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2973         ;
2974       else if (*cval2 == 0)
2975         *cval2 = TREE_OPERAND (arg, 1);
2976       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2977         ;
2978       else
2979         return 0;
2980
2981       return 1;
2982
2983     default:
2984       return 0;
2985     }
2986 }
2987 \f
2988 /* ARG is a tree that is known to contain just arithmetic operations and
2989    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2990    any occurrence of OLD0 as an operand of a comparison and likewise for
2991    NEW1 and OLD1.  */
2992
2993 static tree
2994 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2995 {
2996   tree type = TREE_TYPE (arg);
2997   enum tree_code code = TREE_CODE (arg);
2998   enum tree_code_class class = TREE_CODE_CLASS (code);
2999
3000   /* We can handle some of the tcc_expression cases here.  */
3001   if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3002     class = tcc_unary;
3003   else if (class == tcc_expression
3004            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3005     class = tcc_binary;
3006
3007   switch (class)
3008     {
3009     case tcc_unary:
3010       return fold_build1 (code, type,
3011                           eval_subst (TREE_OPERAND (arg, 0),
3012                                       old0, new0, old1, new1));
3013
3014     case tcc_binary:
3015       return fold_build2 (code, type,
3016                           eval_subst (TREE_OPERAND (arg, 0),
3017                                       old0, new0, old1, new1),
3018                           eval_subst (TREE_OPERAND (arg, 1),
3019                                       old0, new0, old1, new1));
3020
3021     case tcc_expression:
3022       switch (code)
3023         {
3024         case SAVE_EXPR:
3025           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3026
3027         case COMPOUND_EXPR:
3028           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3029
3030         case COND_EXPR:
3031           return fold_build3 (code, type,
3032                               eval_subst (TREE_OPERAND (arg, 0),
3033                                           old0, new0, old1, new1),
3034                               eval_subst (TREE_OPERAND (arg, 1),
3035                                           old0, new0, old1, new1),
3036                               eval_subst (TREE_OPERAND (arg, 2),
3037                                           old0, new0, old1, new1));
3038         default:
3039           break;
3040         }
3041       /* Fall through - ???  */
3042
3043     case tcc_comparison:
3044       {
3045         tree arg0 = TREE_OPERAND (arg, 0);
3046         tree arg1 = TREE_OPERAND (arg, 1);
3047
3048         /* We need to check both for exact equality and tree equality.  The
3049            former will be true if the operand has a side-effect.  In that
3050            case, we know the operand occurred exactly once.  */
3051
3052         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3053           arg0 = new0;
3054         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3055           arg0 = new1;
3056
3057         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3058           arg1 = new0;
3059         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3060           arg1 = new1;
3061
3062         return fold_build2 (code, type, arg0, arg1);
3063       }
3064
3065     default:
3066       return arg;
3067     }
3068 }
3069 \f
3070 /* Return a tree for the case when the result of an expression is RESULT
3071    converted to TYPE and OMITTED was previously an operand of the expression
3072    but is now not needed (e.g., we folded OMITTED * 0).
3073
3074    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
3075    the conversion of RESULT to TYPE.  */
3076
3077 tree
3078 omit_one_operand (tree type, tree result, tree omitted)
3079 {
3080   tree t = fold_convert (type, result);
3081
3082   if (TREE_SIDE_EFFECTS (omitted))
3083     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3084
3085   return non_lvalue (t);
3086 }
3087
3088 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
3089
3090 static tree
3091 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3092 {
3093   tree t = fold_convert (type, result);
3094
3095   if (TREE_SIDE_EFFECTS (omitted))
3096     return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3097
3098   return pedantic_non_lvalue (t);
3099 }
3100
3101 /* Return a tree for the case when the result of an expression is RESULT
3102    converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3103    of the expression but are now not needed.
3104
3105    If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3106    If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3107    evaluated before OMITTED2.  Otherwise, if neither has side effects,
3108    just do the conversion of RESULT to TYPE.  */
3109
3110 tree
3111 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3112 {
3113   tree t = fold_convert (type, result);
3114
3115   if (TREE_SIDE_EFFECTS (omitted2))
3116     t = build2 (COMPOUND_EXPR, type, omitted2, t);
3117   if (TREE_SIDE_EFFECTS (omitted1))
3118     t = build2 (COMPOUND_EXPR, type, omitted1, t);
3119
3120   return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3121 }
3122
3123 \f
3124 /* Return a simplified tree node for the truth-negation of ARG.  This
3125    never alters ARG itself.  We assume that ARG is an operation that
3126    returns a truth value (0 or 1).
3127
3128    FIXME: one would think we would fold the result, but it causes
3129    problems with the dominator optimizer.  */
3130
3131 tree
3132 fold_truth_not_expr (tree arg)
3133 {
3134   tree type = TREE_TYPE (arg);
3135   enum tree_code code = TREE_CODE (arg);
3136
3137   /* If this is a comparison, we can simply invert it, except for
3138      floating-point non-equality comparisons, in which case we just
3139      enclose a TRUTH_NOT_EXPR around what we have.  */
3140
3141   if (TREE_CODE_CLASS (code) == tcc_comparison)
3142     {
3143       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3144       if (FLOAT_TYPE_P (op_type)
3145           && flag_trapping_math
3146           && code != ORDERED_EXPR && code != UNORDERED_EXPR
3147           && code != NE_EXPR && code != EQ_EXPR)
3148         return NULL_TREE;
3149       else
3150         {
3151           code = invert_tree_comparison (code,
3152                                          HONOR_NANS (TYPE_MODE (op_type)));
3153           if (code == ERROR_MARK)
3154             return NULL_TREE;
3155           else
3156             return build2 (code, type,
3157                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3158         }
3159     }
3160
3161   switch (code)
3162     {
3163     case INTEGER_CST:
3164       return constant_boolean_node (integer_zerop (arg), type);
3165
3166     case TRUTH_AND_EXPR:
3167       return build2 (TRUTH_OR_EXPR, type,
3168                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3169                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3170
3171     case TRUTH_OR_EXPR:
3172       return build2 (TRUTH_AND_EXPR, type,
3173                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3174                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3175
3176     case TRUTH_XOR_EXPR:
3177       /* Here we can invert either operand.  We invert the first operand
3178          unless the second operand is a TRUTH_NOT_EXPR in which case our
3179          result is the XOR of the first operand with the inside of the
3180          negation of the second operand.  */
3181
3182       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3183         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3184                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3185       else
3186         return build2 (TRUTH_XOR_EXPR, type,
3187                        invert_truthvalue (TREE_OPERAND (arg, 0)),
3188                        TREE_OPERAND (arg, 1));
3189
3190     case TRUTH_ANDIF_EXPR:
3191       return build2 (TRUTH_ORIF_EXPR, type,
3192                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3193                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3194
3195     case TRUTH_ORIF_EXPR:
3196       return build2 (TRUTH_ANDIF_EXPR, type,
3197                      invert_truthvalue (TREE_OPERAND (arg, 0)),
3198                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3199
3200     case TRUTH_NOT_EXPR:
3201       return TREE_OPERAND (arg, 0);
3202
3203     case COND_EXPR:
3204       {
3205         tree arg1 = TREE_OPERAND (arg, 1);
3206         tree arg2 = TREE_OPERAND (arg, 2);
3207         /* A COND_EXPR may have a throw as one operand, which
3208            then has void type.  Just leave void operands
3209            as they are.  */
3210         return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3211                        VOID_TYPE_P (TREE_TYPE (arg1))
3212                        ? arg1 : invert_truthvalue (arg1),
3213                        VOID_TYPE_P (TREE_TYPE (arg2))
3214                        ? arg2 : invert_truthvalue (arg2));
3215       }
3216
3217     case COMPOUND_EXPR:
3218       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3219                      invert_truthvalue (TREE_OPERAND (arg, 1)));
3220
3221     case NON_LVALUE_EXPR:
3222       return invert_truthvalue (TREE_OPERAND (arg, 0));
3223
3224     case NOP_EXPR:
3225       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3226         return build1 (TRUTH_NOT_EXPR, type, arg);
3227
3228     case CONVERT_EXPR:
3229     case FLOAT_EXPR:
3230       return build1 (TREE_CODE (arg), type,
3231                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3232
3233     case BIT_AND_EXPR:
3234       if (!integer_onep (TREE_OPERAND (arg, 1)))
3235         break;
3236       return build2 (EQ_EXPR, type, arg,
3237                      build_int_cst (type, 0));
3238
3239     case SAVE_EXPR:
3240       return build1 (TRUTH_NOT_EXPR, type, arg);
3241
3242     case CLEANUP_POINT_EXPR:
3243       return build1 (CLEANUP_POINT_EXPR, type,
3244                      invert_truthvalue (TREE_OPERAND (arg, 0)));
3245
3246     default:
3247       break;
3248     }
3249
3250   return NULL_TREE;
3251 }
3252
3253 /* Return a simplified tree node for the truth-negation of ARG.  This
3254    never alters ARG itself.  We assume that ARG is an operation that
3255    returns a truth value (0 or 1).
3256
3257    FIXME: one would think we would fold the result, but it causes
3258    problems with the dominator optimizer.  */
3259
3260 tree
3261 invert_truthvalue (tree arg)
3262 {
3263   tree tem;
3264
3265   if (TREE_CODE (arg) == ERROR_MARK)
3266     return arg;
3267
3268   tem = fold_truth_not_expr (arg);
3269   if (!tem)
3270     tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3271
3272   return tem;
3273 }
3274
3275 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3276    operands are another bit-wise operation with a common input.  If so,
3277    distribute the bit operations to save an operation and possibly two if
3278    constants are involved.  For example, convert
3279         (A | B) & (A | C) into A | (B & C)
3280    Further simplification will occur if B and C are constants.
3281
3282    If this optimization cannot be done, 0 will be returned.  */
3283
3284 static tree
3285 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3286 {
3287   tree common;
3288   tree left, right;
3289
3290   if (TREE_CODE (arg0) != TREE_CODE (arg1)
3291       || TREE_CODE (arg0) == code
3292       || (TREE_CODE (arg0) != BIT_AND_EXPR
3293           && TREE_CODE (arg0) != BIT_IOR_EXPR))
3294     return 0;
3295
3296   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3297     {
3298       common = TREE_OPERAND (arg0, 0);
3299       left = TREE_OPERAND (arg0, 1);
3300       right = TREE_OPERAND (arg1, 1);
3301     }
3302   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3303     {
3304       common = TREE_OPERAND (arg0, 0);
3305       left = TREE_OPERAND (arg0, 1);
3306       right = TREE_OPERAND (arg1, 0);
3307     }
3308   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3309     {
3310       common = TREE_OPERAND (arg0, 1);
3311       left = TREE_OPERAND (arg0, 0);
3312       right = TREE_OPERAND (arg1, 1);
3313     }
3314   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3315     {
3316       common = TREE_OPERAND (arg0, 1);
3317       left = TREE_OPERAND (arg0, 0);
3318       right = TREE_OPERAND (arg1, 0);
3319     }
3320   else
3321     return 0;
3322
3323   return fold_build2 (TREE_CODE (arg0), type, common,
3324                       fold_build2 (code, type, left, right));
3325 }
3326
3327 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3328    with code CODE.  This optimization is unsafe.  */
3329 static tree
3330 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3331 {
3332   bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3333   bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3334
3335   /* (A / C) +- (B / C) -> (A +- B) / C.  */
3336   if (mul0 == mul1
3337       && operand_equal_p (TREE_OPERAND (arg0, 1),
3338                        TREE_OPERAND (arg1, 1), 0))
3339     return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3340                         fold_build2 (code, type,
3341                                      TREE_OPERAND (arg0, 0),
3342                                      TREE_OPERAND (arg1, 0)),
3343                         TREE_OPERAND (arg0, 1));
3344
3345   /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2).  */
3346   if (operand_equal_p (TREE_OPERAND (arg0, 0),
3347                        TREE_OPERAND (arg1, 0), 0)
3348       && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3349       && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3350     {
3351       REAL_VALUE_TYPE r0, r1;
3352       r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3353       r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3354       if (!mul0)
3355         real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3356       if (!mul1)
3357         real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3358       real_arithmetic (&r0, code, &r0, &r1);
3359       return fold_build2 (MULT_EXPR, type,
3360                           TREE_OPERAND (arg0, 0),
3361                           build_real (type, r0));
3362     }
3363
3364   return NULL_TREE;
3365 }
3366 \f
3367 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3368    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3369
3370 static tree
3371 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3372                     int unsignedp)
3373 {
3374   tree result;
3375
3376   if (bitpos == 0)
3377     {
3378       tree size = TYPE_SIZE (TREE_TYPE (inner));
3379       if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3380            || POINTER_TYPE_P (TREE_TYPE (inner)))
3381           && host_integerp (size, 0) 
3382           && tree_low_cst (size, 0) == bitsize)
3383         return fold_convert (type, inner);
3384     }
3385
3386   result = build3 (BIT_FIELD_REF, type, inner,
3387                    size_int (bitsize), bitsize_int (bitpos));
3388
3389   BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3390
3391   return result;
3392 }
3393
3394 /* Optimize a bit-field compare.
3395
3396    There are two cases:  First is a compare against a constant and the
3397    second is a comparison of two items where the fields are at the same
3398    bit position relative to the start of a chunk (byte, halfword, word)
3399    large enough to contain it.  In these cases we can avoid the shift
3400    implicit in bitfield extractions.
3401
3402    For constants, we emit a compare of the shifted constant with the
3403    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3404    compared.  For two fields at the same position, we do the ANDs with the
3405    similar mask and compare the result of the ANDs.
3406
3407    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3408    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3409    are the left and right operands of the comparison, respectively.
3410
3411    If the optimization described above can be done, we return the resulting
3412    tree.  Otherwise we return zero.  */
3413
3414 static tree
3415 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3416                             tree lhs, tree rhs)
3417 {
3418   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3419   tree type = TREE_TYPE (lhs);
3420   tree signed_type, unsigned_type;
3421   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3422   enum machine_mode lmode, rmode, nmode;
3423   int lunsignedp, runsignedp;
3424   int lvolatilep = 0, rvolatilep = 0;
3425   tree linner, rinner = NULL_TREE;
3426   tree mask;
3427   tree offset;
3428
3429   /* Get all the information about the extractions being done.  If the bit size
3430      if the same as the size of the underlying object, we aren't doing an
3431      extraction at all and so can do nothing.  We also don't want to
3432      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3433      then will no longer be able to replace it.  */
3434   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3435                                 &lunsignedp, &lvolatilep, false);
3436   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3437       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3438     return 0;
3439
3440  if (!const_p)
3441    {
3442      /* If this is not a constant, we can only do something if bit positions,
3443         sizes, and signedness are the same.  */
3444      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3445                                    &runsignedp, &rvolatilep, false);
3446
3447      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3448          || lunsignedp != runsignedp || offset != 0
3449          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3450        return 0;
3451    }
3452
3453   /* See if we can find a mode to refer to this field.  We should be able to,
3454      but fail if we can't.  */
3455   nmode = get_best_mode (lbitsize, lbitpos,
3456                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3457                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3458                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3459                          word_mode, lvolatilep || rvolatilep);
3460   if (nmode == VOIDmode)
3461     return 0;
3462
3463   /* Set signed and unsigned types of the precision of this mode for the
3464      shifts below.  */
3465   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3466   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3467
3468   /* Compute the bit position and size for the new reference and our offset
3469      within it. If the new reference is the same size as the original, we
3470      won't optimize anything, so return zero.  */
3471   nbitsize = GET_MODE_BITSIZE (nmode);
3472   nbitpos = lbitpos & ~ (nbitsize - 1);
3473   lbitpos -= nbitpos;
3474   if (nbitsize == lbitsize)
3475     return 0;
3476
3477   if (BYTES_BIG_ENDIAN)
3478     lbitpos = nbitsize - lbitsize - lbitpos;
3479
3480   /* Make the mask to be used against the extracted field.  */
3481   mask = build_int_cst_type (unsigned_type, -1);
3482   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3483   mask = const_binop (RSHIFT_EXPR, mask,
3484                       size_int (nbitsize - lbitsize - lbitpos), 0);
3485
3486   if (! const_p)
3487     /* If not comparing with constant, just rework the comparison
3488        and return.  */
3489     return fold_build2 (code, compare_type,
3490                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3491                                      make_bit_field_ref (linner,
3492                                                          unsigned_type,
3493                                                          nbitsize, nbitpos,
3494                                                          1),
3495                                      mask),
3496                         fold_build2 (BIT_AND_EXPR, unsigned_type,
3497                                      make_bit_field_ref (rinner,
3498                                                          unsigned_type,
3499                                                          nbitsize, nbitpos,
3500                                                          1),
3501                                      mask));
3502
3503   /* Otherwise, we are handling the constant case. See if the constant is too
3504      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3505      this not only for its own sake, but to avoid having to test for this
3506      error case below.  If we didn't, we might generate wrong code.
3507
3508      For unsigned fields, the constant shifted right by the field length should
3509      be all zero.  For signed fields, the high-order bits should agree with
3510      the sign bit.  */
3511
3512   if (lunsignedp)
3513     {
3514       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3515                                         fold_convert (unsigned_type, rhs),
3516                                         size_int (lbitsize), 0)))
3517         {
3518           warning (0, "comparison is always %d due to width of bit-field",
3519                    code == NE_EXPR);
3520           return constant_boolean_node (code == NE_EXPR, compare_type);
3521         }
3522     }
3523   else
3524     {
3525       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3526                               size_int (lbitsize - 1), 0);
3527       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3528         {
3529           warning (0, "comparison is always %d due to width of bit-field",
3530                    code == NE_EXPR);
3531           return constant_boolean_node (code == NE_EXPR, compare_type);
3532         }
3533     }
3534
3535   /* Single-bit compares should always be against zero.  */
3536   if (lbitsize == 1 && ! integer_zerop (rhs))
3537     {
3538       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3539       rhs = build_int_cst (type, 0);
3540     }
3541
3542   /* Make a new bitfield reference, shift the constant over the
3543      appropriate number of bits and mask it with the computed mask
3544      (in case this was a signed field).  If we changed it, make a new one.  */
3545   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3546   if (lvolatilep)
3547     {
3548       TREE_SIDE_EFFECTS (lhs) = 1;
3549       TREE_THIS_VOLATILE (lhs) = 1;
3550     }
3551
3552   rhs = const_binop (BIT_AND_EXPR,
3553                      const_binop (LSHIFT_EXPR,
3554                                   fold_convert (unsigned_type, rhs),
3555                                   size_int (lbitpos), 0),
3556                      mask, 0);
3557
3558   return build2 (code, compare_type,
3559                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3560                  rhs);
3561 }
3562 \f
3563 /* Subroutine for fold_truthop: decode a field reference.
3564
3565    If EXP is a comparison reference, we return the innermost reference.
3566
3567    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3568    set to the starting bit number.
3569
3570    If the innermost field can be completely contained in a mode-sized
3571    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3572
3573    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3574    otherwise it is not changed.
3575
3576    *PUNSIGNEDP is set to the signedness of the field.
3577
3578    *PMASK is set to the mask used.  This is either contained in a
3579    BIT_AND_EXPR or derived from the width of the field.
3580
3581    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3582
3583    Return 0 if this is not a component reference or is one that we can't
3584    do anything with.  */
3585
3586 static tree
3587 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3588                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3589                         int *punsignedp, int *pvolatilep,
3590                         tree *pmask, tree *pand_mask)
3591 {
3592   tree outer_type = 0;
3593   tree and_mask = 0;
3594   tree mask, inner, offset;
3595   tree unsigned_type;
3596   unsigned int precision;
3597
3598   /* All the optimizations using this function assume integer fields.
3599      There are problems with FP fields since the type_for_size call
3600      below can fail for, e.g., XFmode.  */
3601   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3602     return 0;
3603
3604   /* We are interested in the bare arrangement of bits, so strip everything
3605      that doesn't affect the machine mode.  However, record the type of the
3606      outermost expression if it may matter below.  */
3607   if (TREE_CODE (exp) == NOP_EXPR
3608       || TREE_CODE (exp) == CONVERT_EXPR
3609       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3610     outer_type = TREE_TYPE (exp);
3611   STRIP_NOPS (exp);
3612
3613   if (TREE_CODE (exp) == BIT_AND_EXPR)
3614     {
3615       and_mask = TREE_OPERAND (exp, 1);
3616       exp = TREE_OPERAND (exp, 0);
3617       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3618       if (TREE_CODE (and_mask) != INTEGER_CST)
3619         return 0;
3620     }
3621
3622   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3623                                punsignedp, pvolatilep, false);
3624   if ((inner == exp && and_mask == 0)
3625       || *pbitsize < 0 || offset != 0
3626       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3627     return 0;
3628
3629   /* If the number of bits in the reference is the same as the bitsize of
3630      the outer type, then the outer type gives the signedness. Otherwise
3631      (in case of a small bitfield) the signedness is unchanged.  */
3632   if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3633     *punsignedp = TYPE_UNSIGNED (outer_type);
3634
3635   /* Compute the mask to access the bitfield.  */
3636   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3637   precision = TYPE_PRECISION (unsigned_type);
3638
3639   mask = build_int_cst_type (unsigned_type, -1);
3640
3641   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3642   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3643
3644   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3645   if (and_mask != 0)
3646     mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3647                         fold_convert (unsigned_type, and_mask), mask);
3648
3649   *pmask = mask;
3650   *pand_mask = and_mask;
3651   return inner;
3652 }
3653
3654 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3655    bit positions.  */
3656
3657 static int
3658 all_ones_mask_p (tree mask, int size)
3659 {
3660   tree type = TREE_TYPE (mask);
3661   unsigned int precision = TYPE_PRECISION (type);
3662   tree tmask;
3663
3664   tmask = build_int_cst_type (lang_hooks.types.signed_type (type), -1);
3665
3666   return
3667     tree_int_cst_equal (mask,
3668                         const_binop (RSHIFT_EXPR,
3669                                      const_binop (LSHIFT_EXPR, tmask,
3670                                                   size_int (precision - size),
3671                                                   0),
3672                                      size_int (precision - size), 0));
3673 }
3674
3675 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3676    represents the sign bit of EXP's type.  If EXP represents a sign
3677    or zero extension, also test VAL against the unextended type.
3678    The return value is the (sub)expression whose sign bit is VAL,
3679    or NULL_TREE otherwise.  */
3680
3681 static tree
3682 sign_bit_p (tree exp, tree val)
3683 {
3684   unsigned HOST_WIDE_INT mask_lo, lo;
3685   HOST_WIDE_INT mask_hi, hi;
3686   int width;
3687   tree t;
3688
3689   /* Tree EXP must have an integral type.  */
3690   t = TREE_TYPE (exp);
3691   if (! INTEGRAL_TYPE_P (t))
3692     return NULL_TREE;
3693
3694   /* Tree VAL must be an integer constant.  */
3695   if (TREE_CODE (val) != INTEGER_CST
3696       || TREE_CONSTANT_OVERFLOW (val))
3697     return NULL_TREE;
3698
3699   width = TYPE_PRECISION (t);
3700   if (width > HOST_BITS_PER_WIDE_INT)
3701     {
3702       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3703       lo = 0;
3704
3705       mask_hi = ((unsigned HOST_WIDE_INT) -1
3706                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3707       mask_lo = -1;
3708     }
3709   else
3710     {
3711       hi = 0;
3712       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3713
3714       mask_hi = 0;
3715       mask_lo = ((unsigned HOST_WIDE_INT) -1
3716                  >> (HOST_BITS_PER_WIDE_INT - width));
3717     }
3718
3719   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3720      treat VAL as if it were unsigned.  */
3721   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3722       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3723     return exp;
3724
3725   /* Handle extension from a narrower type.  */
3726   if (TREE_CODE (exp) == NOP_EXPR
3727       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3728     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3729
3730   return NULL_TREE;
3731 }
3732
3733 /* Subroutine for fold_truthop: determine if an operand is simple enough
3734    to be evaluated unconditionally.  */
3735
3736 static int
3737 simple_operand_p (tree exp)
3738 {
3739   /* Strip any conversions that don't change the machine mode.  */
3740   STRIP_NOPS (exp);
3741
3742   return (CONSTANT_CLASS_P (exp)
3743           || TREE_CODE (exp) == SSA_NAME
3744           || (DECL_P (exp)
3745               && ! TREE_ADDRESSABLE (exp)
3746               && ! TREE_THIS_VOLATILE (exp)
3747               && ! DECL_NONLOCAL (exp)
3748               /* Don't regard global variables as simple.  They may be
3749                  allocated in ways unknown to the compiler (shared memory,
3750                  #pragma weak, etc).  */
3751               && ! TREE_PUBLIC (exp)
3752               && ! DECL_EXTERNAL (exp)
3753               /* Loading a static variable is unduly expensive, but global
3754                  registers aren't expensive.  */
3755               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3756 }
3757 \f
3758 /* The following functions are subroutines to fold_range_test and allow it to
3759    try to change a logical combination of comparisons into a range test.
3760
3761    For example, both
3762         X == 2 || X == 3 || X == 4 || X == 5
3763    and
3764         X >= 2 && X <= 5
3765    are converted to
3766         (unsigned) (X - 2) <= 3
3767
3768    We describe each set of comparisons as being either inside or outside
3769    a range, using a variable named like IN_P, and then describe the
3770    range with a lower and upper bound.  If one of the bounds is omitted,
3771    it represents either the highest or lowest value of the type.
3772
3773    In the comments below, we represent a range by two numbers in brackets
3774    preceded by a "+" to designate being inside that range, or a "-" to
3775    designate being outside that range, so the condition can be inverted by
3776    flipping the prefix.  An omitted bound is represented by a "-".  For
3777    example, "- [-, 10]" means being outside the range starting at the lowest
3778    possible value and ending at 10, in other words, being greater than 10.
3779    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3780    always false.
3781
3782    We set up things so that the missing bounds are handled in a consistent
3783    manner so neither a missing bound nor "true" and "false" need to be
3784    handled using a special case.  */
3785
3786 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3787    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3788    and UPPER1_P are nonzero if the respective argument is an upper bound
3789    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3790    must be specified for a comparison.  ARG1 will be converted to ARG0's
3791    type if both are specified.  */
3792
3793 static tree
3794 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3795              tree arg1, int upper1_p)
3796 {
3797   tree tem;
3798   int result;
3799   int sgn0, sgn1;
3800
3801   /* If neither arg represents infinity, do the normal operation.
3802      Else, if not a comparison, return infinity.  Else handle the special
3803      comparison rules. Note that most of the cases below won't occur, but
3804      are handled for consistency.  */
3805
3806   if (arg0 != 0 && arg1 != 0)
3807     {
3808       tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3809                          arg0, fold_convert (TREE_TYPE (arg0), arg1));
3810       STRIP_NOPS (tem);
3811       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3812     }
3813
3814   if (TREE_CODE_CLASS (code) != tcc_comparison)
3815     return 0;
3816
3817   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3818      for neither.  In real maths, we cannot assume open ended ranges are
3819      the same. But, this is computer arithmetic, where numbers are finite.
3820      We can therefore make the transformation of any unbounded range with
3821      the value Z, Z being greater than any representable number. This permits
3822      us to treat unbounded ranges as equal.  */
3823   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3824   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3825   switch (code)
3826     {
3827     case EQ_EXPR:
3828       result = sgn0 == sgn1;
3829       break;
3830     case NE_EXPR:
3831       result = sgn0 != sgn1;
3832       break;
3833     case LT_EXPR:
3834       result = sgn0 < sgn1;
3835       break;
3836     case LE_EXPR:
3837       result = sgn0 <= sgn1;
3838       break;
3839     case GT_EXPR:
3840       result = sgn0 > sgn1;
3841       break;
3842     case GE_EXPR:
3843       result = sgn0 >= sgn1;
3844       break;
3845     default:
3846       gcc_unreachable ();
3847     }
3848
3849   return constant_boolean_node (result, type);
3850 }
3851 \f
3852 /* Given EXP, a logical expression, set the range it is testing into
3853    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3854    actually being tested.  *PLOW and *PHIGH will be made of the same type
3855    as the returned expression.  If EXP is not a comparison, we will most
3856    likely not be returning a useful value and range.  */
3857
3858 static tree
3859 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3860 {
3861   enum tree_code code;
3862   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3863   tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3864   int in_p, n_in_p;
3865   tree low, high, n_low, n_high;
3866
3867   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3868      and see if we can refine the range.  Some of the cases below may not
3869      happen, but it doesn't seem worth worrying about this.  We "continue"
3870      the outer loop when we've changed something; otherwise we "break"
3871      the switch, which will "break" the while.  */
3872
3873   in_p = 0;
3874   low = high = build_int_cst (TREE_TYPE (exp), 0);
3875
3876   while (1)
3877     {
3878       code = TREE_CODE (exp);
3879       exp_type = TREE_TYPE (exp);
3880
3881       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3882         {
3883           if (TREE_CODE_LENGTH (code) > 0)
3884             arg0 = TREE_OPERAND (exp, 0);
3885           if (TREE_CODE_CLASS (code) == tcc_comparison
3886               || TREE_CODE_CLASS (code) == tcc_unary
3887               || TREE_CODE_CLASS (code) == tcc_binary)
3888             arg0_type = TREE_TYPE (arg0);
3889           if (TREE_CODE_CLASS (code) == tcc_binary
3890               || TREE_CODE_CLASS (code) == tcc_comparison
3891               || (TREE_CODE_CLASS (code) == tcc_expression
3892                   && TREE_CODE_LENGTH (code) > 1))
3893             arg1 = TREE_OPERAND (exp, 1);
3894         }
3895
3896       switch (code)
3897         {
3898         case TRUTH_NOT_EXPR:
3899           in_p = ! in_p, exp = arg0;
3900           continue;
3901
3902         case EQ_EXPR: case NE_EXPR:
3903         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3904           /* We can only do something if the range is testing for zero
3905              and if the second operand is an integer constant.  Note that
3906              saying something is "in" the range we make is done by
3907              complementing IN_P since it will set in the initial case of
3908              being not equal to zero; "out" is leaving it alone.  */
3909           if (low == 0 || high == 0
3910               || ! integer_zerop (low) || ! integer_zerop (high)
3911               || TREE_CODE (arg1) != INTEGER_CST)
3912             break;
3913
3914           switch (code)
3915             {
3916             case NE_EXPR:  /* - [c, c]  */
3917               low = high = arg1;
3918               break;
3919             case EQ_EXPR:  /* + [c, c]  */
3920               in_p = ! in_p, low = high = arg1;
3921               break;
3922             case GT_EXPR:  /* - [-, c] */
3923               low = 0, high = arg1;
3924               break;
3925             case GE_EXPR:  /* + [c, -] */
3926               in_p = ! in_p, low = arg1, high = 0;
3927               break;
3928             case LT_EXPR:  /* - [c, -] */
3929               low = arg1, high = 0;
3930               break;
3931             case LE_EXPR:  /* + [-, c] */
3932               in_p = ! in_p, low = 0, high = arg1;
3933               break;
3934             default:
3935               gcc_unreachable ();
3936             }
3937
3938           /* If this is an unsigned comparison, we also know that EXP is
3939              greater than or equal to zero.  We base the range tests we make
3940              on that fact, so we record it here so we can parse existing
3941              range tests.  We test arg0_type since often the return type
3942              of, e.g. EQ_EXPR, is boolean.  */
3943           if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3944             {
3945               if (! merge_ranges (&n_in_p, &n_low, &n_high,
3946                                   in_p, low, high, 1,
3947                                   build_int_cst (arg0_type, 0),
3948                                   NULL_TREE))
3949                 break;
3950
3951               in_p = n_in_p, low = n_low, high = n_high;
3952
3953               /* If the high bound is missing, but we have a nonzero low
3954                  bound, reverse the range so it goes from zero to the low bound
3955                  minus 1.  */
3956               if (high == 0 && low && ! integer_zerop (low))
3957                 {
3958                   in_p = ! in_p;
3959                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3960                                       integer_one_node, 0);
3961                   low = build_int_cst (arg0_type, 0);
3962                 }
3963             }
3964
3965           exp = arg0;
3966           continue;
3967
3968         case NEGATE_EXPR:
3969           /* (-x) IN [a,b] -> x in [-b, -a]  */
3970           n_low = range_binop (MINUS_EXPR, exp_type,
3971                                build_int_cst (exp_type, 0),
3972                                0, high, 1);
3973           n_high = range_binop (MINUS_EXPR, exp_type,
3974                                 build_int_cst (exp_type, 0),
3975                                 0, low, 0);
3976           low = n_low, high = n_high;
3977           exp = arg0;
3978           continue;
3979
3980         case BIT_NOT_EXPR:
3981           /* ~ X -> -X - 1  */
3982           exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3983                         build_int_cst (exp_type, 1));
3984           continue;
3985
3986         case PLUS_EXPR:  case MINUS_EXPR:
3987           if (TREE_CODE (arg1) != INTEGER_CST)
3988             break;
3989
3990           /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3991              move a constant to the other side.  */
3992           if (flag_wrapv && !TYPE_UNSIGNED (arg0_type))
3993             break;
3994
3995           /* If EXP is signed, any overflow in the computation is undefined,
3996              so we don't worry about it so long as our computations on
3997              the bounds don't overflow.  For unsigned, overflow is defined
3998              and this is exactly the right thing.  */
3999           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4000                                arg0_type, low, 0, arg1, 0);
4001           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4002                                 arg0_type, high, 1, arg1, 0);
4003           if ((n_low != 0 && TREE_OVERFLOW (n_low))
4004               || (n_high != 0 && TREE_OVERFLOW (n_high)))
4005             break;
4006
4007           /* Check for an unsigned range which has wrapped around the maximum
4008              value thus making n_high < n_low, and normalize it.  */
4009           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4010             {
4011               low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4012                                  integer_one_node, 0);
4013               high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4014                                   integer_one_node, 0);
4015
4016               /* If the range is of the form +/- [ x+1, x ], we won't
4017                  be able to normalize it.  But then, it represents the
4018                  whole range or the empty set, so make it
4019                  +/- [ -, - ].  */
4020               if (tree_int_cst_equal (n_low, low)
4021                   && tree_int_cst_equal (n_high, high))
4022                 low = high = 0;
4023               else
4024                 in_p = ! in_p;
4025             }
4026           else
4027             low = n_low, high = n_high;
4028
4029           exp = arg0;
4030           continue;
4031
4032         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
4033           if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4034             break;
4035
4036           if (! INTEGRAL_TYPE_P (arg0_type)
4037               || (low != 0 && ! int_fits_type_p (low, arg0_type))
4038               || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4039             break;
4040
4041           n_low = low, n_high = high;
4042
4043           if (n_low != 0)
4044             n_low = fold_convert (arg0_type, n_low);
4045
4046           if (n_high != 0)
4047             n_high = fold_convert (arg0_type, n_high);
4048
4049
4050           /* If we're converting arg0 from an unsigned type, to exp,
4051              a signed type,  we will be doing the comparison as unsigned.
4052              The tests above have already verified that LOW and HIGH
4053              are both positive.
4054
4055              So we have to ensure that we will handle large unsigned
4056              values the same way that the current signed bounds treat
4057              negative values.  */
4058
4059           if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4060             {
4061               tree high_positive;
4062               tree equiv_type = lang_hooks.types.type_for_mode
4063                 (TYPE_MODE (arg0_type), 1);
4064
4065               /* A range without an upper bound is, naturally, unbounded.
4066                  Since convert would have cropped a very large value, use
4067                  the max value for the destination type.  */
4068               high_positive
4069                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4070                 : TYPE_MAX_VALUE (arg0_type);
4071
4072               if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4073                 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4074                                              fold_convert (arg0_type,
4075                                                            high_positive),
4076                                              build_int_cst (arg0_type, 1));
4077
4078               /* If the low bound is specified, "and" the range with the
4079                  range for which the original unsigned value will be
4080                  positive.  */
4081               if (low != 0)
4082                 {
4083                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4084                                       1, n_low, n_high, 1,
4085                                       fold_convert (arg0_type,
4086                                                     integer_zero_node),
4087                                       high_positive))
4088                     break;
4089
4090                   in_p = (n_in_p == in_p);
4091                 }
4092               else
4093                 {
4094                   /* Otherwise, "or" the range with the range of the input
4095                      that will be interpreted as negative.  */
4096                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
4097                                       0, n_low, n_high, 1,
4098                                       fold_convert (arg0_type,
4099                                                     integer_zero_node),
4100                                       high_positive))
4101                     break;
4102
4103                   in_p = (in_p != n_in_p);
4104                 }
4105             }
4106
4107           exp = arg0;
4108           low = n_low, high = n_high;
4109           continue;
4110
4111         default:
4112           break;
4113         }
4114
4115       break;
4116     }
4117
4118   /* If EXP is a constant, we can evaluate whether this is true or false.  */
4119   if (TREE_CODE (exp) == INTEGER_CST)
4120     {
4121       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4122                                                  exp, 0, low, 0))
4123                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
4124                                                     exp, 1, high, 1)));
4125       low = high = 0;
4126       exp = 0;
4127     }
4128
4129   *pin_p = in_p, *plow = low, *phigh = high;
4130   return exp;
4131 }
4132 \f
4133 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4134    type, TYPE, return an expression to test if EXP is in (or out of, depending
4135    on IN_P) the range.  Return 0 if the test couldn't be created.  */
4136
4137 static tree
4138 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4139 {
4140   tree etype = TREE_TYPE (exp);
4141   tree value;
4142
4143 #ifdef HAVE_canonicalize_funcptr_for_compare
4144   /* Disable this optimization for function pointer expressions
4145      on targets that require function pointer canonicalization.  */
4146   if (HAVE_canonicalize_funcptr_for_compare
4147       && TREE_CODE (etype) == POINTER_TYPE
4148       && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4149     return NULL_TREE;
4150 #endif
4151
4152   if (! in_p)
4153     {
4154       value = build_range_check (type, exp, 1, low, high);
4155       if (value != 0)
4156         return invert_truthvalue (value);
4157
4158       return 0;
4159     }
4160
4161   if (low == 0 && high == 0)
4162     return build_int_cst (type, 1);
4163
4164   if (low == 0)
4165     return fold_build2 (LE_EXPR, type, exp,
4166                         fold_convert (etype, high));
4167
4168   if (high == 0)
4169     return fold_build2 (GE_EXPR, type, exp,
4170                         fold_convert (etype, low));
4171
4172   if (operand_equal_p (low, high, 0))
4173     return fold_build2 (EQ_EXPR, type, exp,
4174                         fold_convert (etype, low));
4175
4176   if (integer_zerop (low))
4177     {
4178       if (! TYPE_UNSIGNED (etype))
4179         {
4180           etype = lang_hooks.types.unsigned_type (etype);
4181           high = fold_convert (etype, high);
4182           exp = fold_convert (etype, exp);
4183         }
4184       return build_range_check (type, exp, 1, 0, high);
4185     }
4186
4187   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
4188   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4189     {
4190       unsigned HOST_WIDE_INT lo;
4191       HOST_WIDE_INT hi;
4192       int prec;
4193
4194       prec = TYPE_PRECISION (etype);
4195       if (prec <= HOST_BITS_PER_WIDE_INT)
4196         {
4197           hi = 0;
4198           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4199         }
4200       else
4201         {
4202           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4203           lo = (unsigned HOST_WIDE_INT) -1;
4204         }
4205
4206       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4207         {
4208           if (TYPE_UNSIGNED (etype))
4209             {
4210               etype = lang_hooks.types.signed_type (etype);
4211               exp = fold_convert (etype, exp);
4212             }
4213           return fold_build2 (GT_EXPR, type, exp,
4214                               build_int_cst (etype, 0));
4215         }
4216     }
4217
4218   /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4219      This requires wrap-around arithmetics for the type of the expression.  */
4220   switch (TREE_CODE (etype))
4221     {
4222     case INTEGER_TYPE:
4223       /* There is no requirement that LOW be within the range of ETYPE
4224          if the latter is a subtype.  It must, however, be within the base
4225          type of ETYPE.  So be sure we do the subtraction in that type.  */
4226       if (TREE_TYPE (etype))
4227         etype = TREE_TYPE (etype);
4228       break;
4229
4230     case ENUMERAL_TYPE:
4231     case BOOLEAN_TYPE:
4232       etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4233                                               TYPE_UNSIGNED (etype));
4234       break;
4235
4236     default:
4237       break;
4238     }
4239
4240   /* If we don't have wrap-around arithmetics upfront, try to force it.  */
4241   if (TREE_CODE (etype) == INTEGER_TYPE
4242       && !TYPE_UNSIGNED (etype) && !flag_wrapv)
4243     {
4244       tree utype, minv, maxv;
4245
4246       /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4247          for the type in question, as we rely on this here.  */
4248       utype = lang_hooks.types.unsigned_type (etype);
4249       maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4250       maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4251                           integer_one_node, 1);
4252       minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4253
4254       if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4255                                       minv, 1, maxv, 1)))
4256         etype = utype;
4257       else
4258         return 0;
4259     }
4260
4261   high = fold_convert (etype, high);
4262   low = fold_convert (etype, low);
4263   exp = fold_convert (etype, exp);
4264
4265   value = const_binop (MINUS_EXPR, high, low, 0);
4266
4267   if (value != 0 && !TREE_OVERFLOW (value))
4268     return build_range_check (type,
4269                               fold_build2 (MINUS_EXPR, etype, exp, low),
4270                               1, build_int_cst (etype, 0), value);
4271
4272   return 0;
4273 }
4274 \f
4275 /* Return the predecessor of VAL in its type, handling the infinite case.  */
4276
4277 static tree
4278 range_predecessor (tree val)
4279 {
4280   tree type = TREE_TYPE (val);
4281
4282   if (INTEGRAL_TYPE_P (type)
4283       && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4284     return 0;
4285   else
4286     return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4287 }
4288
4289 /* Return the successor of VAL in its type, handling the infinite case.  */
4290
4291 static tree
4292 range_successor (tree val)
4293 {
4294   tree type = TREE_TYPE (val);
4295
4296   if (INTEGRAL_TYPE_P (type)
4297       && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4298     return 0;
4299   else
4300     return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4301 }
4302
4303 /* Given two ranges, see if we can merge them into one.  Return 1 if we
4304    can, 0 if we can't.  Set the output range into the specified parameters.  */
4305
4306 static int
4307 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4308               tree high0, int in1_p, tree low1, tree high1)
4309 {
4310   int no_overlap;
4311   int subset;
4312   int temp;
4313   tree tem;
4314   int in_p;
4315   tree low, high;
4316   int lowequal = ((low0 == 0 && low1 == 0)
4317                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4318                                                 low0, 0, low1, 0)));
4319   int highequal = ((high0 == 0 && high1 == 0)
4320                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4321                                                  high0, 1, high1, 1)));
4322
4323   /* Make range 0 be the range that starts first, or ends last if they
4324      start at the same value.  Swap them if it isn't.  */
4325   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4326                                  low0, 0, low1, 0))
4327       || (lowequal
4328           && integer_onep (range_binop (GT_EXPR, integer_type_node,
4329                                         high1, 1, high0, 1))))
4330     {
4331       temp = in0_p, in0_p = in1_p, in1_p = temp;
4332       tem = low0, low0 = low1, low1 = tem;
4333       tem = high0, high0 = high1, high1 = tem;
4334     }
4335
4336   /* Now flag two cases, whether the ranges are disjoint or whether the
4337      second range is totally subsumed in the first.  Note that the tests
4338      below are simplified by the ones above.  */
4339   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4340                                           high0, 1, low1, 0));
4341   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4342                                       high1, 1, high0, 1));
4343
4344   /* We now have four cases, depending on whether we are including or
4345      excluding the two ranges.  */
4346   if (in0_p && in1_p)
4347     {
4348       /* If they don't overlap, the result is false.  If the second range
4349          is a subset it is the result.  Otherwise, the range is from the start
4350          of the second to the end of the first.  */
4351       if (no_overlap)
4352         in_p = 0, low = high = 0;
4353       else if (subset)
4354         in_p = 1, low = low1, high = high1;
4355       else
4356         in_p = 1, low = low1, high = high0;
4357     }
4358
4359   else if (in0_p && ! in1_p)
4360     {
4361       /* If they don't overlap, the result is the first range.  If they are
4362          equal, the result is false.  If the second range is a subset of the
4363          first, and the ranges begin at the same place, we go from just after
4364          the end of the second range to the end of the first.  If the second
4365          range is not a subset of the first, or if it is a subset and both
4366          ranges end at the same place, the range starts at the start of the
4367          first range and ends just before the second range.
4368          Otherwise, we can't describe this as a single range.  */
4369       if (no_overlap)
4370         in_p = 1, low = low0, high = high0;
4371       else if (lowequal && highequal)
4372         in_p = 0, low = high = 0;
4373       else if (subset && lowequal)
4374         {
4375           low = range_successor (high1);
4376           high = high0;
4377           in_p = (low != 0);
4378         }
4379       else if (! subset || highequal)
4380         {
4381           low = low0;
4382           high = range_predecessor (low1);
4383           in_p = (high != 0);
4384         }
4385       else
4386         return 0;
4387     }
4388
4389   else if (! in0_p && in1_p)
4390     {
4391       /* If they don't overlap, the result is the second range.  If the second
4392          is a subset of the first, the result is false.  Otherwise,
4393          the range starts just after the first range and ends at the
4394          end of the second.  */
4395       if (no_overlap)
4396         in_p = 1, low = low1, high = high1;
4397       else if (subset || highequal)
4398         in_p = 0, low = high = 0;
4399       else
4400         {
4401           low = range_successor (high0);
4402           high = high1;
4403           in_p = (low != 0);
4404         }
4405     }
4406
4407   else
4408     {
4409       /* The case where we are excluding both ranges.  Here the complex case
4410          is if they don't overlap.  In that case, the only time we have a
4411          range is if they are adjacent.  If the second is a subset of the
4412          first, the result is the first.  Otherwise, the range to exclude
4413          starts at the beginning of the first range and ends at the end of the
4414          second.  */
4415       if (no_overlap)
4416         {
4417           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4418                                          range_successor (high0),
4419                                          1, low1, 0)))
4420             in_p = 0, low = low0, high = high1;
4421           else
4422             {
4423               /* Canonicalize - [min, x] into - [-, x].  */
4424               if (low0 && TREE_CODE (low0) == INTEGER_CST)
4425                 switch (TREE_CODE (TREE_TYPE (low0)))
4426                   {
4427                   case ENUMERAL_TYPE:
4428                     if (TYPE_PRECISION (TREE_TYPE (low0))
4429                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4430                       break;
4431                     /* FALLTHROUGH */
4432                   case INTEGER_TYPE:
4433                     if (tree_int_cst_equal (low0,
4434                                             TYPE_MIN_VALUE (TREE_TYPE (low0))))
4435                       low0 = 0;
4436                     break;
4437                   case POINTER_TYPE:
4438                     if (TYPE_UNSIGNED (TREE_TYPE (low0))
4439                         && integer_zerop (low0))
4440                       low0 = 0;
4441                     break;
4442                   default:
4443                     break;
4444                   }
4445
4446               /* Canonicalize - [x, max] into - [x, -].  */
4447               if (high1 && TREE_CODE (high1) == INTEGER_CST)
4448                 switch (TREE_CODE (TREE_TYPE (high1)))
4449                   {
4450                   case ENUMERAL_TYPE:
4451                     if (TYPE_PRECISION (TREE_TYPE (high1))
4452                         != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4453                       break;
4454                     /* FALLTHROUGH */
4455                   case INTEGER_TYPE:
4456                     if (tree_int_cst_equal (high1,
4457                                             TYPE_MAX_VALUE (TREE_TYPE (high1))))
4458                       high1 = 0;
4459                     break;
4460                   case POINTER_TYPE:
4461                     if (TYPE_UNSIGNED (TREE_TYPE (high1))
4462                         && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4463                                                        high1, 1,
4464                                                        integer_one_node, 1)))
4465                       high1 = 0;
4466                     break;
4467                   default:
4468                     break;
4469                   }
4470
4471               /* The ranges might be also adjacent between the maximum and
4472                  minimum values of the given type.  For
4473                  - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4474                  return + [x + 1, y - 1].  */
4475               if (low0 == 0 && high1 == 0)
4476                 {
4477                   low = range_successor (high0);
4478                   high = range_predecessor (low1);
4479                   if (low == 0 || high == 0)
4480                     return 0;
4481
4482                   in_p = 1;
4483                 }
4484               else
4485                 return 0;
4486             }
4487         }
4488       else if (subset)
4489         in_p = 0, low = low0, high = high0;
4490       else
4491         in_p = 0, low = low0, high = high1;
4492     }
4493
4494   *pin_p = in_p, *plow = low, *phigh = high;
4495   return 1;
4496 }
4497 \f
4498
4499 /* Subroutine of fold, looking inside expressions of the form
4500    A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4501    of the COND_EXPR.  This function is being used also to optimize
4502    A op B ? C : A, by reversing the comparison first.
4503
4504    Return a folded expression whose code is not a COND_EXPR
4505    anymore, or NULL_TREE if no folding opportunity is found.  */
4506
4507 static tree
4508 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4509 {
4510   enum tree_code comp_code = TREE_CODE (arg0);
4511   tree arg00 = TREE_OPERAND (arg0, 0);
4512   tree arg01 = TREE_OPERAND (arg0, 1);
4513   tree arg1_type = TREE_TYPE (arg1);
4514   tree tem;
4515
4516   STRIP_NOPS (arg1);
4517   STRIP_NOPS (arg2);
4518
4519   /* If we have A op 0 ? A : -A, consider applying the following
4520      transformations:
4521
4522      A == 0? A : -A    same as -A
4523      A != 0? A : -A    same as A
4524      A >= 0? A : -A    same as abs (A)
4525      A > 0?  A : -A    same as abs (A)
4526      A <= 0? A : -A    same as -abs (A)
4527      A < 0?  A : -A    same as -abs (A)
4528
4529      None of these transformations work for modes with signed
4530      zeros.  If A is +/-0, the first two transformations will
4531      change the sign of the result (from +0 to -0, or vice
4532      versa).  The last four will fix the sign of the result,
4533      even though the original expressions could be positive or
4534      negative, depending on the sign of A.
4535
4536      Note that all these transformations are correct if A is
4537      NaN, since the two alternatives (A and -A) are also NaNs.  */
4538   if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4539        ? real_zerop (arg01)
4540        : integer_zerop (arg01))
4541       && ((TREE_CODE (arg2) == NEGATE_EXPR
4542            && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4543              /* In the case that A is of the form X-Y, '-A' (arg2) may
4544                 have already been folded to Y-X, check for that. */
4545           || (TREE_CODE (arg1) == MINUS_EXPR
4546               && TREE_CODE (arg2) == MINUS_EXPR
4547               && operand_equal_p (TREE_OPERAND (arg1, 0),
4548                                   TREE_OPERAND (arg2, 1), 0)
4549               && operand_equal_p (TREE_OPERAND (arg1, 1),
4550                                   TREE_OPERAND (arg2, 0), 0))))
4551     switch (comp_code)
4552       {
4553       case EQ_EXPR:
4554       case UNEQ_EXPR:
4555         tem = fold_convert (arg1_type, arg1);
4556         return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4557       case NE_EXPR:
4558       case LTGT_EXPR:
4559         return pedantic_non_lvalue (fold_convert (type, arg1));
4560       case UNGE_EXPR:
4561       case UNGT_EXPR:
4562         if (flag_trapping_math)
4563           break;
4564         /* Fall through.  */
4565       case GE_EXPR:
4566       case GT_EXPR:
4567         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4568           arg1 = fold_convert (lang_hooks.types.signed_type
4569                                (TREE_TYPE (arg1)), arg1);
4570         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4571         return pedantic_non_lvalue (fold_convert (type, tem));
4572       case UNLE_EXPR:
4573       case UNLT_EXPR:
4574         if (flag_trapping_math)
4575           break;
4576       case LE_EXPR:
4577       case LT_EXPR:
4578         if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4579           arg1 = fold_convert (lang_hooks.types.signed_type
4580                                (TREE_TYPE (arg1)), arg1);
4581         tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4582         return negate_expr (fold_convert (type, tem));
4583       default:
4584         gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4585         break;
4586       }
4587
4588   /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
4589      A == 0 ? A : 0 is always 0 unless A is -0.  Note that
4590      both transformations are correct when A is NaN: A != 0
4591      is then true, and A == 0 is false.  */
4592
4593   if (integer_zerop (arg01) && integer_zerop (arg2))
4594     {
4595       if (comp_code == NE_EXPR)
4596         return pedantic_non_lvalue (fold_convert (type, arg1));
4597       else if (comp_code == EQ_EXPR)
4598         return build_int_cst (type, 0);
4599     }
4600
4601   /* Try some transformations of A op B ? A : B.
4602
4603      A == B? A : B    same as B
4604      A != B? A : B    same as A
4605      A >= B? A : B    same as max (A, B)
4606      A > B?  A : B    same as max (B, A)
4607      A <= B? A : B    same as min (A, B)
4608      A < B?  A : B    same as min (B, A)
4609
4610      As above, these transformations don't work in the presence
4611      of signed zeros.  For example, if A and B are zeros of
4612      opposite sign, the first two transformations will change
4613      the sign of the result.  In the last four, the original
4614      expressions give different results for (A=+0, B=-0) and
4615      (A=-0, B=+0), but the transformed expressions do not.
4616
4617      The first two transformations are correct if either A or B
4618      is a NaN.  In the first transformation, the condition will
4619      be false, and B will indeed be chosen.  In the case of the
4620      second transformation, the condition A != B will be true,
4621      and A will be chosen.
4622
4623      The conversions to max() and min() are not correct if B is
4624      a number and A is not.  The conditions in the original
4625      expressions will be false, so all four give B.  The min()
4626      and max() versions would give a NaN instead.  */
4627   if (operand_equal_for_comparison_p (arg01, arg2, arg00)
4628       /* Avoid these transformations if the COND_EXPR may be used
4629          as an lvalue in the C++ front-end.  PR c++/19199.  */
4630       && (in_gimple_form
4631           || (strcmp (lang_hooks.name, "GNU C++") != 0
4632               && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4633           || ! maybe_lvalue_p (arg1)
4634           || ! maybe_lvalue_p (arg2)))
4635     {
4636       tree comp_op0 = arg00;
4637       tree comp_op1 = arg01;
4638       tree comp_type = TREE_TYPE (comp_op0);
4639
4640       /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
4641       if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4642         {
4643           comp_type = type;
4644           comp_op0 = arg1;
4645           comp_op1 = arg2;
4646         }
4647
4648       switch (comp_code)
4649         {
4650         case EQ_EXPR:
4651           return pedantic_non_lvalue (fold_convert (type, arg2));
4652         case NE_EXPR:
4653           return pedantic_non_lvalue (fold_convert (type, arg1));
4654         case LE_EXPR:
4655         case LT_EXPR:
4656         case UNLE_EXPR:
4657         case UNLT_EXPR:
4658           /* In C++ a ?: expression can be an lvalue, so put the
4659              operand which will be used if they are equal first
4660              so that we can convert this back to the
4661              corresponding COND_EXPR.  */
4662           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4663             {
4664               comp_op0 = fold_convert (comp_type, comp_op0);
4665               comp_op1 = fold_convert (comp_type, comp_op1);
4666               tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4667                     ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4668                     : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4669               return pedantic_non_lvalue (fold_convert (type, tem));
4670             }
4671           break;
4672         case GE_EXPR:
4673         case GT_EXPR:
4674         case UNGE_EXPR:
4675         case UNGT_EXPR:
4676           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4677             {
4678               comp_op0 = fold_convert (comp_type, comp_op0);
4679               comp_op1 = fold_convert (comp_type, comp_op1);
4680               tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4681                     ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
4682                     : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
4683               return pedantic_non_lvalue (fold_convert (type, tem));
4684             }
4685           break;
4686         case UNEQ_EXPR:
4687           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4688             return pedantic_non_lvalue (fold_convert (type, arg2));
4689           break;
4690         case LTGT_EXPR:
4691           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4692             return pedantic_non_lvalue (fold_convert (type, arg1));
4693           break;
4694         default:
4695           gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4696           break;
4697         }
4698     }
4699
4700   /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4701      we might still be able to simplify this.  For example,
4702      if C1 is one less or one more than C2, this might have started
4703      out as a MIN or MAX and been transformed by this function.
4704      Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4705
4706   if (INTEGRAL_TYPE_P (type)
4707       && TREE_CODE (arg01) == INTEGER_CST
4708       && TREE_CODE (arg2) == INTEGER_CST)
4709     switch (comp_code)
4710       {
4711       case EQ_EXPR:
4712         /* We can replace A with C1 in this case.  */
4713         arg1 = fold_convert (type, arg01);
4714         return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
4715
4716       case LT_EXPR:
4717         /* If C1 is C2 + 1, this is min(A, C2).  */
4718         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4719                                OEP_ONLY_CONST)
4720             && operand_equal_p (arg01,
4721                                 const_binop (PLUS_EXPR, arg2,
4722                                              build_int_cst (type, 1), 0),
4723                                 OEP_ONLY_CONST))
4724           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4725                                                    type, arg1, arg2));
4726         break;
4727
4728       case LE_EXPR:
4729         /* If C1 is C2 - 1, this is min(A, C2).  */
4730         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4731                                OEP_ONLY_CONST)
4732             && operand_equal_p (arg01,
4733                                 const_binop (MINUS_EXPR, arg2,
4734                                              build_int_cst (type, 1), 0),
4735                                 OEP_ONLY_CONST))
4736           return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
4737                                                    type, arg1, arg2));
4738         break;
4739
4740       case GT_EXPR:
4741         /* If C1 is C2 - 1, this is max(A, C2).  */
4742         if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4743                                OEP_ONLY_CONST)
4744             && operand_equal_p (arg01,
4745                                 const_binop (MINUS_EXPR, arg2,
4746                                              build_int_cst (type, 1), 0),
4747                                 OEP_ONLY_CONST))
4748           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4749                                                    type, arg1, arg2));
4750         break;
4751
4752       case GE_EXPR:
4753         /* If C1 is C2 + 1, this is max(A, C2).  */
4754         if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4755                                OEP_ONLY_CONST)
4756             && operand_equal_p (arg01,
4757                                 const_binop (PLUS_EXPR, arg2,
4758                                              build_int_cst (type, 1), 0),
4759                                 OEP_ONLY_CONST))
4760           return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
4761                                                    type, arg1, arg2));
4762         break;
4763       case NE_EXPR:
4764         break;
4765       default:
4766         gcc_unreachable ();
4767       }
4768
4769   return NULL_TREE;
4770 }
4771
4772
4773 \f
4774 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4775 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
4776 #endif
4777
4778 /* EXP is some logical combination of boolean tests.  See if we can
4779    merge it into some range test.  Return the new tree if so.  */
4780
4781 static tree
4782 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
4783 {
4784   int or_op = (code == TRUTH_ORIF_EXPR
4785                || code == TRUTH_OR_EXPR);
4786   int in0_p, in1_p, in_p;
4787   tree low0, low1, low, high0, high1, high;
4788   tree lhs = make_range (op0, &in0_p, &low0, &high0);
4789   tree rhs = make_range (op1, &in1_p, &low1, &high1);
4790   tree tem;
4791
4792   /* If this is an OR operation, invert both sides; we will invert
4793      again at the end.  */
4794   if (or_op)
4795     in0_p = ! in0_p, in1_p = ! in1_p;
4796
4797   /* If both expressions are the same, if we can merge the ranges, and we
4798      can build the range test, return it or it inverted.  If one of the
4799      ranges is always true or always false, consider it to be the same
4800      expression as the other.  */
4801   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4802       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4803                        in1_p, low1, high1)
4804       && 0 != (tem = (build_range_check (type,
4805                                          lhs != 0 ? lhs
4806                                          : rhs != 0 ? rhs : integer_zero_node,
4807                                          in_p, low, high))))
4808     return or_op ? invert_truthvalue (tem) : tem;
4809
4810   /* On machines where the branch cost is expensive, if this is a
4811      short-circuited branch and the underlying object on both sides
4812      is the same, make a non-short-circuit operation.  */
4813   else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4814            && lhs != 0 && rhs != 0
4815            && (code == TRUTH_ANDIF_EXPR
4816                || code == TRUTH_ORIF_EXPR)
4817            && operand_equal_p (lhs, rhs, 0))
4818     {
4819       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4820          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4821          which cases we can't do this.  */
4822       if (simple_operand_p (lhs))
4823         return build2 (code == TRUTH_ANDIF_EXPR
4824                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4825                        type, op0, op1);
4826
4827       else if (lang_hooks.decls.global_bindings_p () == 0
4828                && ! CONTAINS_PLACEHOLDER_P (lhs))
4829         {
4830           tree common = save_expr (lhs);
4831
4832           if (0 != (lhs = build_range_check (type, common,
4833                                              or_op ? ! in0_p : in0_p,
4834                                              low0, high0))
4835               && (0 != (rhs = build_range_check (type, common,
4836                                                  or_op ? ! in1_p : in1_p,
4837                                                  low1, high1))))
4838             return build2 (code == TRUTH_ANDIF_EXPR
4839                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4840                            type, lhs, rhs);
4841         }
4842     }
4843
4844   return 0;
4845 }
4846 \f
4847 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4848    bit value.  Arrange things so the extra bits will be set to zero if and
4849    only if C is signed-extended to its full width.  If MASK is nonzero,
4850    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4851
4852 static tree
4853 unextend (tree c, int p, int unsignedp, tree mask)
4854 {
4855   tree type = TREE_TYPE (c);
4856   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4857   tree temp;
4858
4859   if (p == modesize || unsignedp)
4860     return c;
4861
4862   /* We work by getting just the sign bit into the low-order bit, then
4863      into the high-order bit, then sign-extend.  We then XOR that value
4864      with C.  */
4865   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4866   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4867
4868   /* We must use a signed type in order to get an arithmetic right shift.
4869      However, we must also avoid introducing accidental overflows, so that
4870      a subsequent call to integer_zerop will work.  Hence we must
4871      do the type conversion here.  At this point, the constant is either
4872      zero or one, and the conversion to a signed type can never overflow.
4873      We could get an overflow if this conversion is done anywhere else.  */
4874   if (TYPE_UNSIGNED (type))
4875     temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4876
4877   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4878   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4879   if (mask != 0)
4880     temp = const_binop (BIT_AND_EXPR, temp,
4881                         fold_convert (TREE_TYPE (c), mask), 0);
4882   /* If necessary, convert the type back to match the type of C.  */
4883   if (TYPE_UNSIGNED (type))
4884     temp = fold_convert (type, temp);
4885
4886   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4887 }
4888 \f
4889 /* Find ways of folding logical expressions of LHS and RHS:
4890    Try to merge two comparisons to the same innermost item.
4891    Look for range tests like "ch >= '0' && ch <= '9'".
4892    Look for combinations of simple terms on machines with expensive branches
4893    and evaluate the RHS unconditionally.
4894
4895    For example, if we have p->a == 2 && p->b == 4 and we can make an
4896    object large enough to span both A and B, we can do this with a comparison
4897    against the object ANDed with the a mask.
4898
4899    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4900    operations to do this with one comparison.
4901
4902    We check for both normal comparisons and the BIT_AND_EXPRs made this by
4903    function and the one above.
4904
4905    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
4906    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4907
4908    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4909    two operands.
4910
4911    We return the simplified tree or 0 if no optimization is possible.  */
4912
4913 static tree
4914 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4915 {
4916   /* If this is the "or" of two comparisons, we can do something if
4917      the comparisons are NE_EXPR.  If this is the "and", we can do something
4918      if the comparisons are EQ_EXPR.  I.e.,
4919         (a->b == 2 && a->c == 4) can become (a->new == NEW).
4920
4921      WANTED_CODE is this operation code.  For single bit fields, we can
4922      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4923      comparison for one-bit fields.  */
4924
4925   enum tree_code wanted_code;
4926   enum tree_code lcode, rcode;
4927   tree ll_arg, lr_arg, rl_arg, rr_arg;
4928   tree ll_inner, lr_inner, rl_inner, rr_inner;
4929   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4930   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4931   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4932   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4933   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4934   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4935   enum machine_mode lnmode, rnmode;
4936   tree ll_mask, lr_mask, rl_mask, rr_mask;
4937   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4938   tree l_const, r_const;
4939   tree lntype, rntype, result;
4940   int first_bit, end_bit;
4941   int volatilep;
4942   tree orig_lhs = lhs, orig_rhs = rhs;
4943   enum tree_code orig_code = code;
4944
4945   /* Start by getting the comparison codes.  Fail if anything is volatile.
4946      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4947      it were surrounded with a NE_EXPR.  */
4948
4949   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4950     return 0;
4951
4952   lcode = TREE_CODE (lhs);
4953   rcode = TREE_CODE (rhs);
4954
4955   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4956     {
4957       lhs = build2 (NE_EXPR, truth_type, lhs,
4958                     build_int_cst (TREE_TYPE (lhs), 0));
4959       lcode = NE_EXPR;
4960     }
4961
4962   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4963     {
4964       rhs = build2 (NE_EXPR, truth_type, rhs,
4965                     build_int_cst (TREE_TYPE (rhs), 0));
4966       rcode = NE_EXPR;
4967     }
4968
4969   if (TREE_CODE_CLASS (lcode) != tcc_comparison
4970       || TREE_CODE_CLASS (rcode) != tcc_comparison)
4971     return 0;
4972
4973   ll_arg = TREE_OPERAND (lhs, 0);
4974   lr_arg = TREE_OPERAND (lhs, 1);
4975   rl_arg = TREE_OPERAND (rhs, 0);
4976   rr_arg = TREE_OPERAND (rhs, 1);
4977
4978   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
4979   if (simple_operand_p (ll_arg)
4980       && simple_operand_p (lr_arg))
4981     {
4982       tree result;
4983       if (operand_equal_p (ll_arg, rl_arg, 0)
4984           && operand_equal_p (lr_arg, rr_arg, 0))
4985         {
4986           result = combine_comparisons (code, lcode, rcode,
4987                                         truth_type, ll_arg, lr_arg);
4988           if (result)
4989             return result;
4990         }
4991       else if (operand_equal_p (ll_arg, rr_arg, 0)
4992                && operand_equal_p (lr_arg, rl_arg, 0))
4993         {
4994           result = combine_comparisons (code, lcode,
4995                                         swap_tree_comparison (rcode),
4996                                         truth_type, ll_arg, lr_arg);
4997           if (result)
4998             return result;
4999         }
5000     }
5001
5002   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5003           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5004
5005   /* If the RHS can be evaluated unconditionally and its operands are
5006      simple, it wins to evaluate the RHS unconditionally on machines
5007      with expensive branches.  In this case, this isn't a comparison
5008      that can be merged.  Avoid doing this if the RHS is a floating-point
5009      comparison since those can trap.  */
5010
5011   if (BRANCH_COST >= 2
5012       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5013       && simple_operand_p (rl_arg)
5014       && simple_operand_p (rr_arg))
5015     {
5016       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
5017       if (code == TRUTH_OR_EXPR
5018           && lcode == NE_EXPR && integer_zerop (lr_arg)
5019           && rcode == NE_EXPR && integer_zerop (rr_arg)
5020           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5021         return build2 (NE_EXPR, truth_type,
5022                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5023                                ll_arg, rl_arg),
5024                        build_int_cst (TREE_TYPE (ll_arg), 0));
5025
5026       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
5027       if (code == TRUTH_AND_EXPR
5028           && lcode == EQ_EXPR && integer_zerop (lr_arg)
5029           && rcode == EQ_EXPR && integer_zerop (rr_arg)
5030           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5031         return build2 (EQ_EXPR, truth_type,
5032                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5033                                ll_arg, rl_arg),
5034                        build_int_cst (TREE_TYPE (ll_arg), 0));
5035
5036       if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5037         {
5038           if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5039             return build2 (code, truth_type, lhs, rhs);
5040           return NULL_TREE;
5041         }
5042     }
5043
5044   /* See if the comparisons can be merged.  Then get all the parameters for
5045      each side.  */
5046
5047   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5048       || (rcode != EQ_EXPR && rcode != NE_EXPR))
5049     return 0;
5050
5051   volatilep = 0;
5052   ll_inner = decode_field_reference (ll_arg,
5053                                      &ll_bitsize, &ll_bitpos, &ll_mode,
5054                                      &ll_unsignedp, &volatilep, &ll_mask,
5055                                      &ll_and_mask);
5056   lr_inner = decode_field_reference (lr_arg,
5057                                      &lr_bitsize, &lr_bitpos, &lr_mode,
5058                                      &lr_unsignedp, &volatilep, &lr_mask,
5059                                      &lr_and_mask);
5060   rl_inner = decode_field_reference (rl_arg,
5061                                      &rl_bitsize, &rl_bitpos, &rl_mode,
5062                                      &rl_unsignedp, &volatilep, &rl_mask,
5063                                      &rl_and_mask);
5064   rr_inner = decode_field_reference (rr_arg,
5065                                      &rr_bitsize, &rr_bitpos, &rr_mode,
5066                                      &rr_unsignedp, &volatilep, &rr_mask,
5067                                      &rr_and_mask);
5068
5069   /* It must be true that the inner operation on the lhs of each
5070      comparison must be the same if we are to be able to do anything.
5071      Then see if we have constants.  If not, the same must be true for
5072      the rhs's.  */
5073   if (volatilep || ll_inner == 0 || rl_inner == 0
5074       || ! operand_equal_p (ll_inner, rl_inner, 0))
5075     return 0;
5076
5077   if (TREE_CODE (lr_arg) == INTEGER_CST
5078       && TREE_CODE (rr_arg) == INTEGER_CST)
5079     l_const = lr_arg, r_const = rr_arg;
5080   else if (lr_inner == 0 || rr_inner == 0
5081            || ! operand_equal_p (lr_inner, rr_inner, 0))
5082     return 0;
5083   else
5084     l_const = r_const = 0;
5085
5086   /* If either comparison code is not correct for our logical operation,
5087      fail.  However, we can convert a one-bit comparison against zero into
5088      the opposite comparison against that bit being set in the field.  */
5089
5090   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5091   if (lcode != wanted_code)
5092     {
5093       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5094         {
5095           /* Make the left operand unsigned, since we are only interested
5096              in the value of one bit.  Otherwise we are doing the wrong
5097              thing below.  */
5098           ll_unsignedp = 1;
5099           l_const = ll_mask;
5100         }
5101       else
5102         return 0;
5103     }
5104
5105   /* This is analogous to the code for l_const above.  */
5106   if (rcode != wanted_code)
5107     {
5108       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5109         {
5110           rl_unsignedp = 1;
5111           r_const = rl_mask;
5112         }
5113       else
5114         return 0;
5115     }
5116
5117   /* After this point all optimizations will generate bit-field
5118      references, which we might not want.  */
5119   if (! lang_hooks.can_use_bit_fields_p ())
5120     return 0;
5121
5122   /* See if we can find a mode that contains both fields being compared on
5123      the left.  If we can't, fail.  Otherwise, update all constants and masks
5124      to be relative to a field of that size.  */
5125   first_bit = MIN (ll_bitpos, rl_bitpos);
5126   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5127   lnmode = get_best_mode (end_bit - first_bit, first_bit,
5128                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5129                           volatilep);
5130   if (lnmode == VOIDmode)
5131     return 0;
5132
5133   lnbitsize = GET_MODE_BITSIZE (lnmode);
5134   lnbitpos = first_bit & ~ (lnbitsize - 1);
5135   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5136   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5137
5138   if (BYTES_BIG_ENDIAN)
5139     {
5140       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5141       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5142     }
5143
5144   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5145                          size_int (xll_bitpos), 0);
5146   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5147                          size_int (xrl_bitpos), 0);
5148
5149   if (l_const)
5150     {
5151       l_const = fold_convert (lntype, l_const);
5152       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5153       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5154       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5155                                         fold_build1 (BIT_NOT_EXPR,
5156                                                      lntype, ll_mask),
5157                                         0)))
5158         {
5159           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5160
5161           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5162         }
5163     }
5164   if (r_const)
5165     {
5166       r_const = fold_convert (lntype, r_const);
5167       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5168       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5169       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5170                                         fold_build1 (BIT_NOT_EXPR,
5171                                                      lntype, rl_mask),
5172                                         0)))
5173         {
5174           warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5175
5176           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5177         }
5178     }
5179
5180   /* If the right sides are not constant, do the same for it.  Also,
5181      disallow this optimization if a size or signedness mismatch occurs
5182      between the left and right sides.  */
5183   if (l_const == 0)
5184     {
5185       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5186           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5187           /* Make sure the two fields on the right
5188              correspond to the left without being swapped.  */
5189           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5190         return 0;
5191
5192       first_bit = MIN (lr_bitpos, rr_bitpos);
5193       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5194       rnmode = get_best_mode (end_bit - first_bit, first_bit,
5195                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5196                               volatilep);
5197       if (rnmode == VOIDmode)
5198         return 0;
5199
5200       rnbitsize = GET_MODE_BITSIZE (rnmode);
5201       rnbitpos = first_bit & ~ (rnbitsize - 1);
5202       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5203       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5204
5205       if (BYTES_BIG_ENDIAN)
5206         {
5207           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5208           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5209         }
5210
5211       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5212                              size_int (xlr_bitpos), 0);
5213       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5214                              size_int (xrr_bitpos), 0);
5215
5216       /* Make a mask that corresponds to both fields being compared.
5217          Do this for both items being compared.  If the operands are the
5218          same size and the bits being compared are in the same position
5219          then we can do this by masking both and comparing the masked
5220          results.  */
5221       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5222       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5223       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5224         {
5225           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5226                                     ll_unsignedp || rl_unsignedp);
5227           if (! all_ones_mask_p (ll_mask, lnbitsize))
5228             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5229
5230           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5231                                     lr_unsignedp || rr_unsignedp);
5232           if (! all_ones_mask_p (lr_mask, rnbitsize))
5233             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5234
5235           return build2 (wanted_code, truth_type, lhs, rhs);
5236         }
5237
5238       /* There is still another way we can do something:  If both pairs of
5239          fields being compared are adjacent, we may be able to make a wider
5240          field containing them both.
5241
5242          Note that we still must mask the lhs/rhs expressions.  Furthermore,
5243          the mask must be shifted to account for the shift done by
5244          make_bit_field_ref.  */
5245       if ((ll_bitsize + ll_bitpos == rl_bitpos
5246            && lr_bitsize + lr_bitpos == rr_bitpos)
5247           || (ll_bitpos == rl_bitpos + rl_bitsize
5248               && lr_bitpos == rr_bitpos + rr_bitsize))
5249         {
5250           tree type;
5251
5252           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5253                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5254           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5255                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5256
5257           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5258                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5259           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5260                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5261
5262           /* Convert to the smaller type before masking out unwanted bits.  */
5263           type = lntype;
5264           if (lntype != rntype)
5265             {
5266               if (lnbitsize > rnbitsize)
5267                 {
5268                   lhs = fold_convert (rntype, lhs);
5269                   ll_mask = fold_convert (rntype, ll_mask);
5270                   type = rntype;
5271                 }
5272               else if (lnbitsize < rnbitsize)
5273                 {
5274                   rhs = fold_convert (lntype, rhs);
5275                   lr_mask = fold_convert (lntype, lr_mask);
5276                   type = lntype;
5277                 }
5278             }
5279
5280           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5281             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5282
5283           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5284             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5285
5286           return build2 (wanted_code, truth_type, lhs, rhs);
5287         }
5288
5289       return 0;
5290     }
5291
5292   /* Handle the case of comparisons with constants.  If there is something in
5293      common between the masks, those bits of the constants must be the same.
5294      If not, the condition is always false.  Test for this to avoid generating
5295      incorrect code below.  */
5296   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5297   if (! integer_zerop (result)
5298       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5299                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5300     {
5301       if (wanted_code == NE_EXPR)
5302         {
5303           warning (0, "%<or%> of unmatched not-equal tests is always 1");
5304           return constant_boolean_node (true, truth_type);
5305         }
5306       else
5307         {
5308           warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5309           return constant_boolean_node (false, truth_type);
5310         }
5311     }
5312
5313   /* Construct the expression we will return.  First get the component
5314      reference we will make.  Unless the mask is all ones the width of
5315      that field, perform the mask operation.  Then compare with the
5316      merged constant.  */
5317   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5318                                ll_unsignedp || rl_unsignedp);
5319
5320   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5321   if (! all_ones_mask_p (ll_mask, lnbitsize))
5322     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5323
5324   return build2 (wanted_code, truth_type, result,
5325                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5326 }
5327 \f
5328 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5329    constant.  */
5330
5331 static tree
5332 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5333 {
5334   tree arg0 = op0;
5335   enum tree_code op_code;
5336   tree comp_const = op1;
5337   tree minmax_const;
5338   int consts_equal, consts_lt;
5339   tree inner;
5340
5341   STRIP_SIGN_NOPS (arg0);
5342
5343   op_code = TREE_CODE (arg0);
5344   minmax_const = TREE_OPERAND (arg0, 1);
5345   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5346   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5347   inner = TREE_OPERAND (arg0, 0);
5348
5349   /* If something does not permit us to optimize, return the original tree.  */
5350   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5351       || TREE_CODE (comp_const) != INTEGER_CST
5352       || TREE_CONSTANT_OVERFLOW (comp_const)
5353       || TREE_CODE (minmax_const) != INTEGER_CST
5354       || TREE_CONSTANT_OVERFLOW (minmax_const))
5355     return NULL_TREE;
5356
5357   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
5358      and GT_EXPR, doing the rest with recursive calls using logical
5359      simplifications.  */
5360   switch (code)
5361     {
5362     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
5363       {
5364         tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5365                                           type, op0, op1);
5366         if (tem)
5367           return invert_truthvalue (tem);
5368         return NULL_TREE;
5369       }
5370
5371     case GE_EXPR:
5372       return
5373         fold_build2 (TRUTH_ORIF_EXPR, type,
5374                      optimize_minmax_comparison
5375                      (EQ_EXPR, type, arg0, comp_const),
5376                      optimize_minmax_comparison
5377                      (GT_EXPR, type, arg0, comp_const));
5378
5379     case EQ_EXPR:
5380       if (op_code == MAX_EXPR && consts_equal)
5381         /* MAX (X, 0) == 0  ->  X <= 0  */
5382         return fold_build2 (LE_EXPR, type, inner, comp_const);
5383
5384       else if (op_code == MAX_EXPR && consts_lt)
5385         /* MAX (X, 0) == 5  ->  X == 5   */
5386         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5387
5388       else if (op_code == MAX_EXPR)
5389         /* MAX (X, 0) == -1  ->  false  */
5390         return omit_one_operand (type, integer_zero_node, inner);
5391
5392       else if (consts_equal)
5393         /* MIN (X, 0) == 0  ->  X >= 0  */
5394         return fold_build2 (GE_EXPR, type, inner, comp_const);
5395
5396       else if (consts_lt)
5397         /* MIN (X, 0) == 5  ->  false  */
5398         return omit_one_operand (type, integer_zero_node, inner);
5399
5400       else
5401         /* MIN (X, 0) == -1  ->  X == -1  */
5402         return fold_build2 (EQ_EXPR, type, inner, comp_const);
5403
5404     case GT_EXPR:
5405       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5406         /* MAX (X, 0) > 0  ->  X > 0
5407            MAX (X, 0) > 5  ->  X > 5  */
5408         return fold_build2 (GT_EXPR, type, inner, comp_const);
5409
5410       else if (op_code == MAX_EXPR)
5411         /* MAX (X, 0) > -1  ->  true  */
5412         return omit_one_operand (type, integer_one_node, inner);
5413
5414       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5415         /* MIN (X, 0) > 0  ->  false
5416            MIN (X, 0) > 5  ->  false  */
5417         return omit_one_operand (type, integer_zero_node, inner);
5418
5419       else
5420         /* MIN (X, 0) > -1  ->  X > -1  */
5421         return fold_build2 (GT_EXPR, type, inner, comp_const);
5422
5423     default:
5424       return NULL_TREE;
5425     }
5426 }
5427 \f
5428 /* T is an integer expression that is being multiplied, divided, or taken a
5429    modulus (CODE says which and what kind of divide or modulus) by a
5430    constant C.  See if we can eliminate that operation by folding it with
5431    other operations already in T.  WIDE_TYPE, if non-null, is a type that
5432    should be used for the computation if wider than our type.
5433
5434    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5435    (X * 2) + (Y * 4).  We must, however, be assured that either the original
5436    expression would not overflow or that overflow is undefined for the type
5437    in the language in question.
5438
5439    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5440    the machine has a multiply-accumulate insn or that this is part of an
5441    addressing calculation.
5442
5443    If we return a non-null expression, it is an equivalent form of the
5444    original computation, but need not be in the original type.  */
5445
5446 static tree
5447 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
5448 {
5449   /* To avoid exponential search depth, refuse to allow recursion past
5450      three levels.  Beyond that (1) it's highly unlikely that we'll find
5451      something interesting and (2) we've probably processed it before
5452      when we built the inner expression.  */
5453
5454   static int depth;
5455   tree ret;
5456
5457   if (depth > 3)
5458     return NULL;
5459
5460   depth++;
5461   ret = extract_muldiv_1 (t, c, code, wide_type);
5462   depth--;
5463
5464   return ret;
5465 }
5466
5467 static tree
5468 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
5469 {
5470   tree type = TREE_TYPE (t);
5471   enum tree_code tcode = TREE_CODE (t);
5472   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5473                                    > GET_MODE_SIZE (TYPE_MODE (type)))
5474                 ? wide_type : type);
5475   tree t1, t2;
5476   int same_p = tcode == code;
5477   tree op0 = NULL_TREE, op1 = NULL_TREE;
5478
5479   /* Don't deal with constants of zero here; they confuse the code below.  */
5480   if (integer_zerop (c))
5481     return NULL_TREE;
5482
5483   if (TREE_CODE_CLASS (tcode) == tcc_unary)
5484     op0 = TREE_OPERAND (t, 0);
5485
5486   if (TREE_CODE_CLASS (tcode) == tcc_binary)
5487     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5488
5489   /* Note that we need not handle conditional operations here since fold
5490      already handles those cases.  So just do arithmetic here.  */
5491   switch (tcode)
5492     {
5493     case INTEGER_CST:
5494       /* For a constant, we can always simplify if we are a multiply
5495          or (for divide and modulus) if it is a multiple of our constant.  */
5496       if (code == MULT_EXPR
5497           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5498         return const_binop (code, fold_convert (ctype, t),
5499                             fold_convert (ctype, c), 0);
5500       break;
5501
5502     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
5503       /* If op0 is an expression ...  */
5504       if ((COMPARISON_CLASS_P (op0)
5505            || UNARY_CLASS_P (op0)
5506            || BINARY_CLASS_P (op0)
5507            || EXPRESSION_CLASS_P (op0))
5508           /* ... and is unsigned, and its type is smaller than ctype,
5509              then we cannot pass through as widening.  */
5510           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5511                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5512                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5513                && (GET_MODE_SIZE (TYPE_MODE (ctype))
5514                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5515               /* ... or this is a truncation (t is narrower than op0),
5516                  then we cannot pass through this narrowing.  */
5517               || (GET_MODE_SIZE (TYPE_MODE (type))
5518                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5519               /* ... or signedness changes for division or modulus,
5520                  then we cannot pass through this conversion.  */
5521               || (code != MULT_EXPR
5522                   && (TYPE_UNSIGNED (ctype)
5523                       != TYPE_UNSIGNED (TREE_TYPE (op0))))))
5524         break;
5525
5526       /* Pass the constant down and see if we can make a simplification.  If
5527          we can, replace this expression with the inner simplification for
5528          possible later conversion to our or some other type.  */
5529       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5530           && TREE_CODE (t2) == INTEGER_CST
5531           && ! TREE_CONSTANT_OVERFLOW (t2)
5532           && (0 != (t1 = extract_muldiv (op0, t2, code,
5533                                          code == MULT_EXPR
5534                                          ? ctype : NULL_TREE))))
5535         return t1;
5536       break;
5537
5538     case ABS_EXPR:
5539       /* If widening the type changes it from signed to unsigned, then we
5540          must avoid building ABS_EXPR itself as unsigned.  */
5541       if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5542         {
5543           tree cstype = (*lang_hooks.types.signed_type) (ctype);
5544           if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
5545             {
5546               t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5547               return fold_convert (ctype, t1);
5548             }
5549           break;
5550         }
5551       /* FALLTHROUGH */
5552     case NEGATE_EXPR:
5553       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5554         return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5555       break;
5556
5557     case MIN_EXPR:  case MAX_EXPR:
5558       /* If widening the type changes the signedness, then we can't perform
5559          this optimization as that changes the result.  */
5560       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5561         break;
5562
5563       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
5564       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
5565           && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
5566         {
5567           if (tree_int_cst_sgn (c) < 0)
5568             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5569
5570           return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5571                               fold_convert (ctype, t2));
5572         }
5573       break;
5574
5575     case LSHIFT_EXPR:  case RSHIFT_EXPR:
5576       /* If the second operand is constant, this is a multiplication
5577          or floor division, by a power of two, so we can treat it that
5578          way unless the multiplier or divisor overflows.  Signed
5579          left-shift overflow is implementation-defined rather than
5580          undefined in C90, so do not convert signed left shift into
5581          multiplication.  */
5582       if (TREE_CODE (op1) == INTEGER_CST
5583           && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5584           /* const_binop may not detect overflow correctly,
5585              so check for it explicitly here.  */
5586           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5587           && TREE_INT_CST_HIGH (op1) == 0
5588           && 0 != (t1 = fold_convert (ctype,
5589                                       const_binop (LSHIFT_EXPR,
5590                                                    size_one_node,
5591                                                    op1, 0)))
5592           && ! TREE_OVERFLOW (t1))
5593         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5594                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
5595                                        ctype, fold_convert (ctype, op0), t1),
5596                                c, code, wide_type);
5597       break;
5598
5599     case PLUS_EXPR:  case MINUS_EXPR:
5600       /* See if we can eliminate the operation on both sides.  If we can, we
5601          can return a new PLUS or MINUS.  If we can't, the only remaining
5602          cases where we can do anything are if the second operand is a
5603          constant.  */
5604       t1 = extract_muldiv (op0, c, code, wide_type);
5605       t2 = extract_muldiv (op1, c, code, wide_type);
5606       if (t1 != 0 && t2 != 0
5607           && (code == MULT_EXPR
5608               /* If not multiplication, we can only do this if both operands
5609                  are divisible by c.  */
5610               || (multiple_of_p (ctype, op0, c)
5611                   && multiple_of_p (ctype, op1, c))))
5612         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5613                             fold_convert (ctype, t2));
5614
5615       /* If this was a subtraction, negate OP1 and set it to be an addition.
5616          This simplifies the logic below.  */
5617       if (tcode == MINUS_EXPR)
5618         tcode = PLUS_EXPR, op1 = negate_expr (op1);
5619
5620       if (TREE_CODE (op1) != INTEGER_CST)
5621         break;
5622
5623       /* If either OP1 or C are negative, this optimization is not safe for
5624          some of the division and remainder types while for others we need
5625          to change the code.  */
5626       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5627         {
5628           if (code == CEIL_DIV_EXPR)
5629             code = FLOOR_DIV_EXPR;
5630           else if (code == FLOOR_DIV_EXPR)
5631             code = CEIL_DIV_EXPR;
5632           else if (code != MULT_EXPR
5633                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5634             break;
5635         }
5636
5637       /* If it's a multiply or a division/modulus operation of a multiple
5638          of our constant, do the operation and verify it doesn't overflow.  */
5639       if (code == MULT_EXPR
5640           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5641         {
5642           op1 = const_binop (code, fold_convert (ctype, op1),
5643                              fold_convert (ctype, c), 0);
5644           /* We allow the constant to overflow with wrapping semantics.  */
5645           if (op1 == 0
5646               || (TREE_OVERFLOW (op1) && ! flag_wrapv))
5647             break;
5648         }
5649       else
5650         break;
5651
5652       /* If we have an unsigned type is not a sizetype, we cannot widen
5653          the operation since it will change the result if the original
5654          computation overflowed.  */
5655       if (TYPE_UNSIGNED (ctype)
5656           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5657           && ctype != type)
5658         break;
5659
5660       /* If we were able to eliminate our operation from the first side,
5661          apply our operation to the second side and reform the PLUS.  */
5662       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5663         return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5664
5665       /* The last case is if we are a multiply.  In that case, we can
5666          apply the distributive law to commute the multiply and addition
5667          if the multiplication of the constants doesn't overflow.  */
5668       if (code == MULT_EXPR)
5669         return fold_build2 (tcode, ctype,
5670                             fold_build2 (code, ctype,
5671                                          fold_convert (ctype, op0),
5672                                          fold_convert (ctype, c)),
5673                             op1);
5674
5675       break;
5676
5677     case MULT_EXPR:
5678       /* We have a special case here if we are doing something like
5679          (C * 8) % 4 since we know that's zero.  */
5680       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5681            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5682           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5683           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5684         return omit_one_operand (type, integer_zero_node, op0);
5685
5686       /* ... fall through ...  */
5687
5688     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
5689     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
5690       /* If we can extract our operation from the LHS, do so and return a
5691          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
5692          do something only if the second operand is a constant.  */
5693       if (same_p
5694           && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5695         return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5696                             fold_convert (ctype, op1));
5697       else if (tcode == MULT_EXPR && code == MULT_EXPR
5698                && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
5699         return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5700                             fold_convert (ctype, t1));
5701       else if (TREE_CODE (op1) != INTEGER_CST)
5702         return 0;
5703
5704       /* If these are the same operation types, we can associate them
5705          assuming no overflow.  */
5706       if (tcode == code
5707           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5708                                      fold_convert (ctype, c), 0))
5709           && ! TREE_OVERFLOW (t1))
5710         return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5711
5712       /* If these operations "cancel" each other, we have the main
5713          optimizations of this pass, which occur when either constant is a
5714          multiple of the other, in which case we replace this with either an
5715          operation or CODE or TCODE.
5716
5717          If we have an unsigned type that is not a sizetype, we cannot do
5718          this since it will change the result if the original computation
5719          overflowed.  */
5720       if ((! TYPE_UNSIGNED (ctype)
5721            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5722           && ! flag_wrapv
5723           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5724               || (tcode == MULT_EXPR
5725                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5726                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
5727         {
5728           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5729             return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5730                                 fold_convert (ctype,
5731                                               const_binop (TRUNC_DIV_EXPR,
5732                                                            op1, c, 0)));
5733           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5734             return fold_build2 (code, ctype, fold_convert (ctype, op0),
5735                                 fold_convert (ctype,
5736                                               const_binop (TRUNC_DIV_EXPR,
5737                                                            c, op1, 0)));
5738         }
5739       break;
5740
5741     default:
5742       break;
5743     }
5744
5745   return 0;
5746 }
5747 \f
5748 /* Return a node which has the indicated constant VALUE (either 0 or
5749    1), and is of the indicated TYPE.  */
5750
5751 tree
5752 constant_boolean_node (int value, tree type)
5753 {
5754   if (type == integer_type_node)
5755     return value ? integer_one_node : integer_zero_node;
5756   else if (type == boolean_type_node)
5757     return value ? boolean_true_node : boolean_false_node;
5758   else
5759     return build_int_cst (type, value);
5760 }
5761
5762
5763 /* Return true if expr looks like an ARRAY_REF and set base and
5764    offset to the appropriate trees.  If there is no offset,
5765    offset is set to NULL_TREE.  Base will be canonicalized to
5766    something you can get the element type from using
5767    TREE_TYPE (TREE_TYPE (base)).  Offset will be the offset
5768    in bytes to the base.  */
5769
5770 static bool
5771 extract_array_ref (tree expr, tree *base, tree *offset)
5772 {
5773   /* One canonical form is a PLUS_EXPR with the first
5774      argument being an ADDR_EXPR with a possible NOP_EXPR
5775      attached.  */
5776   if (TREE_CODE (expr) == PLUS_EXPR)
5777     {
5778       tree op0 = TREE_OPERAND (expr, 0);
5779       tree inner_base, dummy1;
5780       /* Strip NOP_EXPRs here because the C frontends and/or
5781          folders present us (int *)&x.a + 4B possibly.  */
5782       STRIP_NOPS (op0);
5783       if (extract_array_ref (op0, &inner_base, &dummy1))
5784         {
5785           *base = inner_base;
5786           if (dummy1 == NULL_TREE)
5787             *offset = TREE_OPERAND (expr, 1);
5788           else
5789             *offset = fold_build2 (PLUS_EXPR, TREE_TYPE (expr),
5790                                    dummy1, TREE_OPERAND (expr, 1));
5791           return true;
5792         }
5793     }
5794   /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
5795      which we transform into an ADDR_EXPR with appropriate
5796      offset.  For other arguments to the ADDR_EXPR we assume
5797      zero offset and as such do not care about the ADDR_EXPR
5798      type and strip possible nops from it.  */
5799   else if (TREE_CODE (expr) == ADDR_EXPR)
5800     {
5801       tree op0 = TREE_OPERAND (expr, 0);
5802       if (TREE_CODE (op0) == ARRAY_REF)
5803         {
5804           tree idx = TREE_OPERAND (op0, 1);
5805           *base = TREE_OPERAND (op0, 0);
5806           *offset = fold_build2 (MULT_EXPR, TREE_TYPE (idx), idx,
5807                                  array_ref_element_size (op0)); 
5808         }
5809       else
5810         {
5811           /* Handle array-to-pointer decay as &a.  */
5812           if (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE)
5813             *base = TREE_OPERAND (expr, 0);
5814           else
5815             *base = expr;
5816           *offset = NULL_TREE;
5817         }
5818       return true;
5819     }
5820   /* The next canonical form is a VAR_DECL with POINTER_TYPE.  */
5821   else if (SSA_VAR_P (expr)
5822            && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5823     {
5824       *base = expr;
5825       *offset = NULL_TREE;
5826       return true;
5827     }
5828
5829   return false;
5830 }
5831
5832
5833 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5834    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
5835    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5836    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
5837    COND is the first argument to CODE; otherwise (as in the example
5838    given here), it is the second argument.  TYPE is the type of the
5839    original expression.  Return NULL_TREE if no simplification is
5840    possible.  */
5841
5842 static tree
5843 fold_binary_op_with_conditional_arg (enum tree_code code,
5844                                      tree type, tree op0, tree op1,
5845                                      tree cond, tree arg, int cond_first_p)
5846 {
5847   tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5848   tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5849   tree test, true_value, false_value;
5850   tree lhs = NULL_TREE;
5851   tree rhs = NULL_TREE;
5852
5853   /* This transformation is only worthwhile if we don't have to wrap
5854      arg in a SAVE_EXPR, and the operation can be simplified on at least
5855      one of the branches once its pushed inside the COND_EXPR.  */
5856   if (!TREE_CONSTANT (arg))
5857     return NULL_TREE;
5858
5859   if (TREE_CODE (cond) == COND_EXPR)
5860     {
5861       test = TREE_OPERAND (cond, 0);
5862       true_value = TREE_OPERAND (cond, 1);
5863       false_value = TREE_OPERAND (cond, 2);
5864       /* If this operand throws an expression, then it does not make
5865          sense to try to perform a logical or arithmetic operation
5866          involving it.  */
5867       if (VOID_TYPE_P (TREE_TYPE (true_value)))
5868         lhs = true_value;
5869       if (VOID_TYPE_P (TREE_TYPE (false_value)))
5870         rhs = false_value;
5871     }
5872   else
5873     {
5874       tree testtype = TREE_TYPE (cond);
5875       test = cond;
5876       true_value = constant_boolean_node (true, testtype);
5877       false_value = constant_boolean_node (false, testtype);
5878     }
5879
5880   arg = fold_convert (arg_type, arg);
5881   if (lhs == 0)
5882     {
5883       true_value = fold_convert (cond_type, true_value);
5884       if (cond_first_p)
5885         lhs = fold_build2 (code, type, true_value, arg);
5886       else
5887         lhs = fold_build2 (code, type, arg, true_value);
5888     }
5889   if (rhs == 0)
5890     {
5891       false_value = fold_convert (cond_type, false_value);
5892       if (cond_first_p)
5893         rhs = fold_build2 (code, type, false_value, arg);
5894       else
5895         rhs = fold_build2 (code, type, arg, false_value);
5896     }
5897
5898   test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
5899   return fold_convert (type, test);
5900 }
5901
5902 \f
5903 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5904
5905    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5906    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
5907    ADDEND is the same as X.
5908
5909    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5910    and finite.  The problematic cases are when X is zero, and its mode
5911    has signed zeros.  In the case of rounding towards -infinity,
5912    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
5913    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
5914
5915 static bool
5916 fold_real_zero_addition_p (tree type, tree addend, int negate)
5917 {
5918   if (!real_zerop (addend))
5919     return false;
5920
5921   /* Don't allow the fold with -fsignaling-nans.  */
5922   if (HONOR_SNANS (TYPE_MODE (type)))
5923     return false;
5924
5925   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
5926   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5927     return true;
5928
5929   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
5930   if (TREE_CODE (addend) == REAL_CST
5931       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5932     negate = !negate;
5933
5934   /* The mode has signed zeros, and we have to honor their sign.
5935      In this situation, there is only one case we can return true for.
5936      X - 0 is the same as X unless rounding towards -infinity is
5937      supported.  */
5938   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5939 }
5940
5941 /* Subroutine of fold() that checks comparisons of built-in math
5942    functions against real constants.
5943
5944    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5945    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
5946    is the type of the result and ARG0 and ARG1 are the operands of the
5947    comparison.  ARG1 must be a TREE_REAL_CST.
5948
5949    The function returns the constant folded tree if a simplification
5950    can be made, and NULL_TREE otherwise.  */
5951
5952 static tree
5953 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5954                      tree type, tree arg0, tree arg1)
5955 {
5956   REAL_VALUE_TYPE c;
5957
5958   if (BUILTIN_SQRT_P (fcode))
5959     {
5960       tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5961       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5962
5963       c = TREE_REAL_CST (arg1);
5964       if (REAL_VALUE_NEGATIVE (c))
5965         {
5966           /* sqrt(x) < y is always false, if y is negative.  */
5967           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5968             return omit_one_operand (type, integer_zero_node, arg);
5969
5970           /* sqrt(x) > y is always true, if y is negative and we
5971              don't care about NaNs, i.e. negative values of x.  */
5972           if (code == NE_EXPR || !HONOR_NANS (mode))
5973             return omit_one_operand (type, integer_one_node, arg);
5974
5975           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5976           return fold_build2 (GE_EXPR, type, arg,
5977                               build_real (TREE_TYPE (arg), dconst0));
5978         }
5979       else if (code == GT_EXPR || code == GE_EXPR)
5980         {
5981           REAL_VALUE_TYPE c2;
5982
5983           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5984           real_convert (&c2, mode, &c2);
5985
5986           if (REAL_VALUE_ISINF (c2))
5987             {
5988               /* sqrt(x) > y is x == +Inf, when y is very large.  */
5989               if (HONOR_INFINITIES (mode))
5990                 return fold_build2 (EQ_EXPR, type, arg,
5991                                     build_real (TREE_TYPE (arg), c2));
5992
5993               /* sqrt(x) > y is always false, when y is very large
5994                  and we don't care about infinities.  */
5995               return omit_one_operand (type, integer_zero_node, arg);
5996             }
5997
5998           /* sqrt(x) > c is the same as x > c*c.  */
5999           return fold_build2 (code, type, arg,
6000                               build_real (TREE_TYPE (arg), c2));
6001         }
6002       else if (code == LT_EXPR || code == LE_EXPR)
6003         {
6004           REAL_VALUE_TYPE c2;
6005
6006           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6007           real_convert (&c2, mode, &c2);
6008
6009           if (REAL_VALUE_ISINF (c2))
6010             {
6011               /* sqrt(x) < y is always true, when y is a very large
6012                  value and we don't care about NaNs or Infinities.  */
6013               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6014                 return omit_one_operand (type, integer_one_node, arg);
6015
6016               /* sqrt(x) < y is x != +Inf when y is very large and we
6017                  don't care about NaNs.  */
6018               if (! HONOR_NANS (mode))
6019                 return fold_build2 (NE_EXPR, type, arg,
6020                                     build_real (TREE_TYPE (arg), c2));
6021
6022               /* sqrt(x) < y is x >= 0 when y is very large and we
6023                  don't care about Infinities.  */
6024               if (! HONOR_INFINITIES (mode))
6025                 return fold_build2 (GE_EXPR, type, arg,
6026                                     build_real (TREE_TYPE (arg), dconst0));
6027
6028               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6029               if (lang_hooks.decls.global_bindings_p () != 0
6030                   || CONTAINS_PLACEHOLDER_P (arg))
6031                 return NULL_TREE;
6032
6033               arg = save_expr (arg);
6034               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6035                                   fold_build2 (GE_EXPR, type, arg,
6036                                                build_real (TREE_TYPE (arg),
6037                                                            dconst0)),
6038                                   fold_build2 (NE_EXPR, type, arg,
6039                                                build_real (TREE_TYPE (arg),
6040                                                            c2)));
6041             }
6042
6043           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6044           if (! HONOR_NANS (mode))
6045             return fold_build2 (code, type, arg,
6046                                 build_real (TREE_TYPE (arg), c2));
6047
6048           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6049           if (lang_hooks.decls.global_bindings_p () == 0
6050               && ! CONTAINS_PLACEHOLDER_P (arg))
6051             {
6052               arg = save_expr (arg);
6053               return fold_build2 (TRUTH_ANDIF_EXPR, type,
6054                                   fold_build2 (GE_EXPR, type, arg,
6055                                                build_real (TREE_TYPE (arg),
6056                                                            dconst0)),
6057                                   fold_build2 (code, type, arg,
6058                                                build_real (TREE_TYPE (arg),
6059                                                            c2)));
6060             }
6061         }
6062     }
6063
6064   return NULL_TREE;
6065 }
6066
6067 /* Subroutine of fold() that optimizes comparisons against Infinities,
6068    either +Inf or -Inf.
6069
6070    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6071    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6072    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6073
6074    The function returns the constant folded tree if a simplification
6075    can be made, and NULL_TREE otherwise.  */
6076
6077 static tree
6078 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6079 {
6080   enum machine_mode mode;
6081   REAL_VALUE_TYPE max;
6082   tree temp;
6083   bool neg;
6084
6085   mode = TYPE_MODE (TREE_TYPE (arg0));
6086
6087   /* For negative infinity swap the sense of the comparison.  */
6088   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6089   if (neg)
6090     code = swap_tree_comparison (code);
6091
6092   switch (code)
6093     {
6094     case GT_EXPR:
6095       /* x > +Inf is always false, if with ignore sNANs.  */
6096       if (HONOR_SNANS (mode))
6097         return NULL_TREE;
6098       return omit_one_operand (type, integer_zero_node, arg0);
6099
6100     case LE_EXPR:
6101       /* x <= +Inf is always true, if we don't case about NaNs.  */
6102       if (! HONOR_NANS (mode))
6103         return omit_one_operand (type, integer_one_node, arg0);
6104
6105       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
6106       if (lang_hooks.decls.global_bindings_p () == 0
6107           && ! CONTAINS_PLACEHOLDER_P (arg0))
6108         {
6109           arg0 = save_expr (arg0);
6110           return fold_build2 (EQ_EXPR, type, arg0, arg0);
6111         }
6112       break;
6113
6114     case EQ_EXPR:
6115     case GE_EXPR:
6116       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
6117       real_maxval (&max, neg, mode);
6118       return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6119                           arg0, build_real (TREE_TYPE (arg0), max));
6120
6121     case LT_EXPR:
6122       /* x < +Inf is always equal to x <= DBL_MAX.  */
6123       real_maxval (&max, neg, mode);
6124       return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6125                           arg0, build_real (TREE_TYPE (arg0), max));
6126
6127     case NE_EXPR:
6128       /* x != +Inf is always equal to !(x > DBL_MAX).  */
6129       real_maxval (&max, neg, mode);
6130       if (! HONOR_NANS (mode))
6131         return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6132                             arg0, build_real (TREE_TYPE (arg0), max));
6133
6134       temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6135                           arg0, build_real (TREE_TYPE (arg0), max));
6136       return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6137
6138     default:
6139       break;
6140     }
6141
6142   return NULL_TREE;
6143 }
6144
6145 /* Subroutine of fold() that optimizes comparisons of a division by
6146    a nonzero integer constant against an integer constant, i.e.
6147    X/C1 op C2.
6148
6149    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6150    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
6151    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
6152
6153    The function returns the constant folded tree if a simplification
6154    can be made, and NULL_TREE otherwise.  */
6155
6156 static tree
6157 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6158 {
6159   tree prod, tmp, hi, lo;
6160   tree arg00 = TREE_OPERAND (arg0, 0);
6161   tree arg01 = TREE_OPERAND (arg0, 1);
6162   unsigned HOST_WIDE_INT lpart;
6163   HOST_WIDE_INT hpart;
6164   bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6165   bool neg_overflow;
6166   int overflow;
6167
6168   /* We have to do this the hard way to detect unsigned overflow.
6169      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
6170   overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6171                                    TREE_INT_CST_HIGH (arg01),
6172                                    TREE_INT_CST_LOW (arg1),
6173                                    TREE_INT_CST_HIGH (arg1),
6174                                    &lpart, &hpart, unsigned_p);
6175   prod = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
6176   prod = force_fit_type (prod, -1, overflow, false);
6177   neg_overflow = false;
6178
6179   if (unsigned_p)
6180     {
6181       tmp = int_const_binop (MINUS_EXPR, arg01,
6182                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6183       lo = prod;
6184
6185       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
6186       overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6187                                        TREE_INT_CST_HIGH (prod),
6188                                        TREE_INT_CST_LOW (tmp),
6189                                        TREE_INT_CST_HIGH (tmp),
6190                                        &lpart, &hpart, unsigned_p);
6191       hi = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
6192       hi = force_fit_type (hi, -1, overflow | TREE_OVERFLOW (prod),
6193                            TREE_CONSTANT_OVERFLOW (prod));
6194     }
6195   else if (tree_int_cst_sgn (arg01) >= 0)
6196     {
6197       tmp = int_const_binop (MINUS_EXPR, arg01,
6198                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6199       switch (tree_int_cst_sgn (arg1))
6200         {
6201         case -1:
6202           neg_overflow = true;
6203           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6204           hi = prod;
6205           break;
6206
6207         case  0:
6208           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6209           hi = tmp;
6210           break;
6211
6212         case  1:
6213           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6214           lo = prod;
6215           break;
6216
6217         default:
6218           gcc_unreachable ();
6219         }
6220     }
6221   else
6222     {
6223       /* A negative divisor reverses the relational operators.  */
6224       code = swap_tree_comparison (code);
6225
6226       tmp = int_const_binop (PLUS_EXPR, arg01,
6227                              build_int_cst (TREE_TYPE (arg01), 1), 0);
6228       switch (tree_int_cst_sgn (arg1))
6229         {
6230         case -1:
6231           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6232           lo = prod;
6233           break;
6234
6235         case  0:
6236           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6237           lo = tmp;
6238           break;
6239
6240         case  1:
6241           neg_overflow = true;
6242           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6243           hi = prod;
6244           break;
6245
6246         default:
6247           gcc_unreachable ();
6248         }
6249     }
6250
6251   switch (code)
6252     {
6253     case EQ_EXPR:
6254       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6255         return omit_one_operand (type, integer_zero_node, arg00);
6256       if (TREE_OVERFLOW (hi))
6257         return fold_build2 (GE_EXPR, type, arg00, lo);
6258       if (TREE_OVERFLOW (lo))
6259         return fold_build2 (LE_EXPR, type, arg00, hi);
6260       return build_range_check (type, arg00, 1, lo, hi);
6261
6262     case NE_EXPR:
6263       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6264         return omit_one_operand (type, integer_one_node, arg00);
6265       if (TREE_OVERFLOW (hi))
6266         return fold_build2 (LT_EXPR, type, arg00, lo);
6267       if (TREE_OVERFLOW (lo))
6268         return fold_build2 (GT_EXPR, type, arg00, hi);
6269       return build_range_check (type, arg00, 0, lo, hi);
6270
6271     case LT_EXPR:
6272       if (TREE_OVERFLOW (lo))
6273         {
6274           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6275           return omit_one_operand (type, tmp, arg00);
6276         }
6277       return fold_build2 (LT_EXPR, type, arg00, lo);
6278
6279     case LE_EXPR:
6280       if (TREE_OVERFLOW (hi))
6281         {
6282           tmp = neg_overflow ? integer_zero_node : integer_one_node;
6283           return omit_one_operand (type, tmp, arg00);
6284         }
6285       return fold_build2 (LE_EXPR, type, arg00, hi);
6286
6287     case GT_EXPR:
6288       if (TREE_OVERFLOW (hi))
6289         {
6290           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6291           return omit_one_operand (type, tmp, arg00);
6292         }
6293       return fold_build2 (GT_EXPR, type, arg00, hi);
6294
6295     case GE_EXPR:
6296       if (TREE_OVERFLOW (lo))
6297         {
6298           tmp = neg_overflow ? integer_one_node : integer_zero_node;
6299           return omit_one_operand (type, tmp, arg00);
6300         }
6301       return fold_build2 (GE_EXPR, type, arg00, lo);
6302
6303     default:
6304       break;
6305     }
6306
6307   return NULL_TREE;
6308 }
6309
6310
6311 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6312    equality/inequality test, then return a simplified form of the test
6313    using a sign testing.  Otherwise return NULL.  TYPE is the desired
6314    result type.  */
6315
6316 static tree
6317 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6318                                      tree result_type)
6319 {
6320   /* If this is testing a single bit, we can optimize the test.  */
6321   if ((code == NE_EXPR || code == EQ_EXPR)
6322       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6323       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6324     {
6325       /* If we have (A & C) != 0 where C is the sign bit of A, convert
6326          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6327       tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6328
6329       if (arg00 != NULL_TREE
6330           /* This is only a win if casting to a signed type is cheap,
6331              i.e. when arg00's type is not a partial mode.  */
6332           && TYPE_PRECISION (TREE_TYPE (arg00))
6333              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6334         {
6335           tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
6336           return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6337                               result_type, fold_convert (stype, arg00),
6338                               build_int_cst (stype, 0));
6339         }
6340     }
6341
6342   return NULL_TREE;
6343 }
6344
6345 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6346    equality/inequality test, then return a simplified form of
6347    the test using shifts and logical operations.  Otherwise return
6348    NULL.  TYPE is the desired result type.  */
6349
6350 tree
6351 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6352                       tree result_type)
6353 {
6354   /* If this is testing a single bit, we can optimize the test.  */
6355   if ((code == NE_EXPR || code == EQ_EXPR)
6356       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6357       && integer_pow2p (TREE_OPERAND (arg0, 1)))
6358     {
6359       tree inner = TREE_OPERAND (arg0, 0);
6360       tree type = TREE_TYPE (arg0);
6361       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6362       enum machine_mode operand_mode = TYPE_MODE (type);
6363       int ops_unsigned;
6364       tree signed_type, unsigned_type, intermediate_type;
6365       tree tem, one;
6366
6367       /* First, see if we can fold the single bit test into a sign-bit
6368          test.  */
6369       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6370                                                  result_type);
6371       if (tem)
6372         return tem;
6373
6374       /* Otherwise we have (A & C) != 0 where C is a single bit,
6375          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
6376          Similarly for (A & C) == 0.  */
6377
6378       /* If INNER is a right shift of a constant and it plus BITNUM does
6379          not overflow, adjust BITNUM and INNER.  */
6380       if (TREE_CODE (inner) == RSHIFT_EXPR
6381           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6382           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6383           && bitnum < TYPE_PRECISION (type)
6384           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6385                                    bitnum - TYPE_PRECISION (type)))
6386         {
6387           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6388           inner = TREE_OPERAND (inner, 0);
6389         }
6390
6391       /* If we are going to be able to omit the AND below, we must do our
6392          operations as unsigned.  If we must use the AND, we have a choice.
6393          Normally unsigned is faster, but for some machines signed is.  */
6394 #ifdef LOAD_EXTEND_OP
6395       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND 
6396                       && !flag_syntax_only) ? 0 : 1;
6397 #else
6398       ops_unsigned = 1;
6399 #endif
6400
6401       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6402       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6403       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6404       inner = fold_convert (intermediate_type, inner);
6405
6406       if (bitnum != 0)
6407         inner = build2 (RSHIFT_EXPR, intermediate_type,
6408                         inner, size_int (bitnum));
6409
6410       one = build_int_cst (intermediate_type, 1);
6411
6412       if (code == EQ_EXPR)
6413         inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6414
6415       /* Put the AND last so it can combine with more things.  */
6416       inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6417
6418       /* Make sure to return the proper type.  */
6419       inner = fold_convert (result_type, inner);
6420
6421       return inner;
6422     }
6423   return NULL_TREE;
6424 }
6425
6426 /* Check whether we are allowed to reorder operands arg0 and arg1,
6427    such that the evaluation of arg1 occurs before arg0.  */
6428
6429 static bool
6430 reorder_operands_p (tree arg0, tree arg1)
6431 {
6432   if (! flag_evaluation_order)
6433       return true;
6434   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6435     return true;
6436   return ! TREE_SIDE_EFFECTS (arg0)
6437          && ! TREE_SIDE_EFFECTS (arg1);
6438 }
6439
6440 /* Test whether it is preferable two swap two operands, ARG0 and
6441    ARG1, for example because ARG0 is an integer constant and ARG1
6442    isn't.  If REORDER is true, only recommend swapping if we can
6443    evaluate the operands in reverse order.  */
6444
6445 bool
6446 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
6447 {
6448   STRIP_SIGN_NOPS (arg0);
6449   STRIP_SIGN_NOPS (arg1);
6450
6451   if (TREE_CODE (arg1) == INTEGER_CST)
6452     return 0;
6453   if (TREE_CODE (arg0) == INTEGER_CST)
6454     return 1;
6455
6456   if (TREE_CODE (arg1) == REAL_CST)
6457     return 0;
6458   if (TREE_CODE (arg0) == REAL_CST)
6459     return 1;
6460
6461   if (TREE_CODE (arg1) == COMPLEX_CST)
6462     return 0;
6463   if (TREE_CODE (arg0) == COMPLEX_CST)
6464     return 1;
6465
6466   if (TREE_CONSTANT (arg1))
6467     return 0;
6468   if (TREE_CONSTANT (arg0))
6469     return 1;
6470
6471   if (optimize_size)
6472     return 0;
6473
6474   if (reorder && flag_evaluation_order
6475       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6476     return 0;
6477
6478   if (DECL_P (arg1))
6479     return 0;
6480   if (DECL_P (arg0))
6481     return 1;
6482
6483   /* It is preferable to swap two SSA_NAME to ensure a canonical form
6484      for commutative and comparison operators.  Ensuring a canonical
6485      form allows the optimizers to find additional redundancies without
6486      having to explicitly check for both orderings.  */
6487   if (TREE_CODE (arg0) == SSA_NAME
6488       && TREE_CODE (arg1) == SSA_NAME
6489       && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6490     return 1;
6491
6492   return 0;
6493 }
6494
6495 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6496    ARG0 is extended to a wider type.  */
6497
6498 static tree
6499 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6500 {
6501   tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6502   tree arg1_unw;
6503   tree shorter_type, outer_type;
6504   tree min, max;
6505   bool above, below;
6506
6507   if (arg0_unw == arg0)
6508     return NULL_TREE;
6509   shorter_type = TREE_TYPE (arg0_unw);
6510
6511 #ifdef HAVE_canonicalize_funcptr_for_compare
6512   /* Disable this optimization if we're casting a function pointer
6513      type on targets that require function pointer canonicalization.  */
6514   if (HAVE_canonicalize_funcptr_for_compare
6515       && TREE_CODE (shorter_type) == POINTER_TYPE
6516       && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6517     return NULL_TREE;
6518 #endif
6519
6520   if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6521     return NULL_TREE;
6522
6523   arg1_unw = get_unwidened (arg1, shorter_type);
6524
6525   /* If possible, express the comparison in the shorter mode.  */
6526   if ((code == EQ_EXPR || code == NE_EXPR
6527        || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6528       && (TREE_TYPE (arg1_unw) == shorter_type
6529           || (TREE_CODE (arg1_unw) == INTEGER_CST
6530               && (TREE_CODE (shorter_type) == INTEGER_TYPE
6531                   || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6532               && int_fits_type_p (arg1_unw, shorter_type))))
6533     return fold_build2 (code, type, arg0_unw,
6534                        fold_convert (shorter_type, arg1_unw));
6535
6536   if (TREE_CODE (arg1_unw) != INTEGER_CST
6537       || TREE_CODE (shorter_type) != INTEGER_TYPE
6538       || !int_fits_type_p (arg1_unw, shorter_type))
6539     return NULL_TREE;
6540
6541   /* If we are comparing with the integer that does not fit into the range
6542      of the shorter type, the result is known.  */
6543   outer_type = TREE_TYPE (arg1_unw);
6544   min = lower_bound_in_type (outer_type, shorter_type);
6545   max = upper_bound_in_type (outer_type, shorter_type);
6546
6547   above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6548                                                    max, arg1_unw));
6549   below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6550                                                    arg1_unw, min));
6551
6552   switch (code)
6553     {
6554     case EQ_EXPR:
6555       if (above || below)
6556         return omit_one_operand (type, integer_zero_node, arg0);
6557       break;
6558
6559     case NE_EXPR:
6560       if (above || below)
6561         return omit_one_operand (type, integer_one_node, arg0);
6562       break;
6563
6564     case LT_EXPR:
6565     case LE_EXPR:
6566       if (above)
6567         return omit_one_operand (type, integer_one_node, arg0);
6568       else if (below)
6569         return omit_one_operand (type, integer_zero_node, arg0);
6570
6571     case GT_EXPR:
6572     case GE_EXPR:
6573       if (above)
6574         return omit_one_operand (type, integer_zero_node, arg0);
6575       else if (below)
6576         return omit_one_operand (type, integer_one_node, arg0);
6577
6578     default:
6579       break;
6580     }
6581
6582   return NULL_TREE;
6583 }
6584
6585 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6586    ARG0 just the signedness is changed.  */
6587
6588 static tree
6589 fold_sign_changed_comparison (enum tree_code code, tree type,
6590                               tree arg0, tree arg1)
6591 {
6592   tree arg0_inner, tmp;
6593   tree inner_type, outer_type;
6594
6595   if (TREE_CODE (arg0) != NOP_EXPR
6596       && TREE_CODE (arg0) != CONVERT_EXPR)
6597     return NULL_TREE;
6598
6599   outer_type = TREE_TYPE (arg0);
6600   arg0_inner = TREE_OPERAND (arg0, 0);
6601   inner_type = TREE_TYPE (arg0_inner);
6602
6603 #ifdef HAVE_canonicalize_funcptr_for_compare
6604   /* Disable this optimization if we're casting a function pointer
6605      type on targets that require function pointer canonicalization.  */
6606   if (HAVE_canonicalize_funcptr_for_compare
6607       && TREE_CODE (inner_type) == POINTER_TYPE
6608       && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6609     return NULL_TREE;
6610 #endif
6611
6612   if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6613     return NULL_TREE;
6614
6615   if (TREE_CODE (arg1) != INTEGER_CST
6616       && !((TREE_CODE (arg1) == NOP_EXPR
6617             || TREE_CODE (arg1) == CONVERT_EXPR)
6618            && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6619     return NULL_TREE;
6620
6621   if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6622       && code != NE_EXPR
6623       && code != EQ_EXPR)
6624     return NULL_TREE;
6625
6626   if (TREE_CODE (arg1) == INTEGER_CST)
6627     {
6628       tmp = build_int_cst_wide (inner_type,
6629                                 TREE_INT_CST_LOW (arg1),
6630                                 TREE_INT_CST_HIGH (arg1));
6631       arg1 = force_fit_type (tmp, 0,
6632                              TREE_OVERFLOW (arg1),
6633                              TREE_CONSTANT_OVERFLOW (arg1));
6634     }
6635   else
6636     arg1 = fold_convert (inner_type, arg1);
6637
6638   return fold_build2 (code, type, arg0_inner, arg1);
6639 }
6640
6641 /* Tries to replace &a[idx] CODE s * delta with &a[idx CODE delta], if s is
6642    step of the array.  Reconstructs s and delta in the case of s * delta
6643    being an integer constant (and thus already folded).
6644    ADDR is the address. MULT is the multiplicative expression.
6645    If the function succeeds, the new address expression is returned.  Otherwise
6646    NULL_TREE is returned.  */
6647
6648 static tree
6649 try_move_mult_to_index (enum tree_code code, tree addr, tree op1)
6650 {
6651   tree s, delta, step;
6652   tree ref = TREE_OPERAND (addr, 0), pref;
6653   tree ret, pos;
6654   tree itype;
6655
6656   /* Canonicalize op1 into a possibly non-constant delta
6657      and an INTEGER_CST s.  */
6658   if (TREE_CODE (op1) == MULT_EXPR)
6659     {
6660       tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6661
6662       STRIP_NOPS (arg0);
6663       STRIP_NOPS (arg1);
6664   
6665       if (TREE_CODE (arg0) == INTEGER_CST)
6666         {
6667           s = arg0;
6668           delta = arg1;
6669         }
6670       else if (TREE_CODE (arg1) == INTEGER_CST)
6671         {
6672           s = arg1;
6673           delta = arg0;
6674         }
6675       else
6676         return NULL_TREE;
6677     }
6678   else if (TREE_CODE (op1) == INTEGER_CST)
6679     {
6680       delta = op1;
6681       s = NULL_TREE;
6682     }
6683   else
6684     {
6685       /* Simulate we are delta * 1.  */
6686       delta = op1;
6687       s = integer_one_node;
6688     }
6689
6690   for (;; ref = TREE_OPERAND (ref, 0))
6691     {
6692       if (TREE_CODE (ref) == ARRAY_REF)
6693         {
6694           itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6695           if (! itype)
6696             continue;
6697
6698           step = array_ref_element_size (ref);
6699           if (TREE_CODE (step) != INTEGER_CST)
6700             continue;
6701
6702           if (s)
6703             {
6704               if (! tree_int_cst_equal (step, s))
6705                 continue;
6706             }
6707           else
6708             {
6709               /* Try if delta is a multiple of step.  */
6710               tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
6711               if (! tmp)
6712                 continue;
6713               delta = tmp;
6714             }
6715
6716           break;
6717         }
6718
6719       if (!handled_component_p (ref))
6720         return NULL_TREE;
6721     }
6722
6723   /* We found the suitable array reference.  So copy everything up to it,
6724      and replace the index.  */
6725
6726   pref = TREE_OPERAND (addr, 0);
6727   ret = copy_node (pref);
6728   pos = ret;
6729
6730   while (pref != ref)
6731     {
6732       pref = TREE_OPERAND (pref, 0);
6733       TREE_OPERAND (pos, 0) = copy_node (pref);
6734       pos = TREE_OPERAND (pos, 0);
6735     }
6736
6737   TREE_OPERAND (pos, 1) = fold_build2 (code, itype,
6738                                        fold_convert (itype,
6739                                                      TREE_OPERAND (pos, 1)),
6740                                        fold_convert (itype, delta));
6741
6742   return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6743 }
6744
6745
6746 /* Fold A < X && A + 1 > Y to A < X && A >= Y.  Normally A + 1 > Y
6747    means A >= Y && A != MAX, but in this case we know that
6748    A < X <= MAX.  INEQ is A + 1 > Y, BOUND is A < X.  */
6749
6750 static tree
6751 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6752 {
6753   tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6754
6755   if (TREE_CODE (bound) == LT_EXPR)
6756     a = TREE_OPERAND (bound, 0);
6757   else if (TREE_CODE (bound) == GT_EXPR)
6758     a = TREE_OPERAND (bound, 1);
6759   else
6760     return NULL_TREE;
6761
6762   typea = TREE_TYPE (a);
6763   if (!INTEGRAL_TYPE_P (typea)
6764       && !POINTER_TYPE_P (typea))
6765     return NULL_TREE;
6766
6767   if (TREE_CODE (ineq) == LT_EXPR)
6768     {
6769       a1 = TREE_OPERAND (ineq, 1);
6770       y = TREE_OPERAND (ineq, 0);
6771     }
6772   else if (TREE_CODE (ineq) == GT_EXPR)
6773     {
6774       a1 = TREE_OPERAND (ineq, 0);
6775       y = TREE_OPERAND (ineq, 1);
6776     }
6777   else
6778     return NULL_TREE;
6779
6780   if (TREE_TYPE (a1) != typea)
6781     return NULL_TREE;
6782
6783   diff = fold_build2 (MINUS_EXPR, typea, a1, a);
6784   if (!integer_onep (diff))
6785     return NULL_TREE;
6786
6787   return fold_build2 (GE_EXPR, type, a, y);
6788 }
6789
6790 /* Fold a sum or difference of at least one multiplication.
6791    Returns the folded tree or NULL if no simplification could be made.  */
6792
6793 static tree
6794 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
6795 {
6796   tree arg00, arg01, arg10, arg11;
6797   tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6798
6799   /* (A * C) +- (B * C) -> (A+-B) * C.
6800      (A * C) +- A -> A * (C+-1).
6801      We are most concerned about the case where C is a constant,
6802      but other combinations show up during loop reduction.  Since
6803      it is not difficult, try all four possibilities.  */
6804
6805   if (TREE_CODE (arg0) == MULT_EXPR)
6806     {
6807       arg00 = TREE_OPERAND (arg0, 0);
6808       arg01 = TREE_OPERAND (arg0, 1);
6809     }
6810   else
6811     {
6812       arg00 = arg0;
6813       arg01 = build_one_cst (type);
6814     }
6815   if (TREE_CODE (arg1) == MULT_EXPR)
6816     {
6817       arg10 = TREE_OPERAND (arg1, 0);
6818       arg11 = TREE_OPERAND (arg1, 1);
6819     }
6820   else
6821     {
6822       arg10 = arg1;
6823       arg11 = build_one_cst (type);
6824     }
6825   same = NULL_TREE;
6826
6827   if (operand_equal_p (arg01, arg11, 0))
6828     same = arg01, alt0 = arg00, alt1 = arg10;
6829   else if (operand_equal_p (arg00, arg10, 0))
6830     same = arg00, alt0 = arg01, alt1 = arg11;
6831   else if (operand_equal_p (arg00, arg11, 0))
6832     same = arg00, alt0 = arg01, alt1 = arg10;
6833   else if (operand_equal_p (arg01, arg10, 0))
6834     same = arg01, alt0 = arg00, alt1 = arg11;
6835
6836   /* No identical multiplicands; see if we can find a common
6837      power-of-two factor in non-power-of-two multiplies.  This
6838      can help in multi-dimensional array access.  */
6839   else if (host_integerp (arg01, 0)
6840            && host_integerp (arg11, 0))
6841     {
6842       HOST_WIDE_INT int01, int11, tmp;
6843       bool swap = false;
6844       tree maybe_same;
6845       int01 = TREE_INT_CST_LOW (arg01);
6846       int11 = TREE_INT_CST_LOW (arg11);
6847
6848       /* Move min of absolute values to int11.  */
6849       if ((int01 >= 0 ? int01 : -int01)
6850           < (int11 >= 0 ? int11 : -int11))
6851         {
6852           tmp = int01, int01 = int11, int11 = tmp;
6853           alt0 = arg00, arg00 = arg10, arg10 = alt0;
6854           maybe_same = arg01;
6855           swap = true;
6856         }
6857       else
6858         maybe_same = arg11;
6859
6860       if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
6861         {
6862           alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
6863                               build_int_cst (TREE_TYPE (arg00),
6864                                              int01 / int11));
6865           alt1 = arg10;
6866           same = maybe_same;
6867           if (swap)
6868             maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6869         }
6870     }
6871
6872   if (same)
6873     return fold_build2 (MULT_EXPR, type,
6874                         fold_build2 (code, type,
6875                                      fold_convert (type, alt0),
6876                                      fold_convert (type, alt1)),
6877                         fold_convert (type, same));
6878
6879   return NULL_TREE;
6880 }
6881
6882 /* Subroutine of native_encode_expr.  Encode the INTEGER_CST
6883    specified by EXPR into the buffer PTR of length LEN bytes.
6884    Return the number of bytes placed in the buffer, or zero
6885    upon failure.  */
6886
6887 static int
6888 native_encode_int (tree expr, unsigned char *ptr, int len)
6889 {
6890   tree type = TREE_TYPE (expr);
6891   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6892   int byte, offset, word, words;
6893   unsigned char value;
6894
6895   if (total_bytes > len)
6896     return 0;
6897   words = total_bytes / UNITS_PER_WORD;
6898
6899   for (byte = 0; byte < total_bytes; byte++)
6900     {
6901       int bitpos = byte * BITS_PER_UNIT;
6902       if (bitpos < HOST_BITS_PER_WIDE_INT)
6903         value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
6904       else
6905         value = (unsigned char) (TREE_INT_CST_HIGH (expr)
6906                                  >> (bitpos - HOST_BITS_PER_WIDE_INT));
6907
6908       if (total_bytes > UNITS_PER_WORD)
6909         {
6910           word = byte / UNITS_PER_WORD;
6911           if (WORDS_BIG_ENDIAN)
6912             word = (words - 1) - word;
6913           offset = word * UNITS_PER_WORD;
6914           if (BYTES_BIG_ENDIAN)
6915             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6916           else
6917             offset += byte % UNITS_PER_WORD;
6918         }
6919       else
6920         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
6921       ptr[offset] = value;
6922     }
6923   return total_bytes;
6924 }
6925
6926
6927 /* Subroutine of native_encode_expr.  Encode the REAL_CST
6928    specified by EXPR into the buffer PTR of length LEN bytes.
6929    Return the number of bytes placed in the buffer, or zero
6930    upon failure.  */
6931
6932 static int
6933 native_encode_real (tree expr, unsigned char *ptr, int len)
6934 {
6935   tree type = TREE_TYPE (expr);
6936   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6937   int byte, offset, word, words;
6938   unsigned char value;
6939
6940   /* There are always 32 bits in each long, no matter the size of
6941      the hosts long.  We handle floating point representations with
6942      up to 192 bits.  */
6943   long tmp[6];
6944
6945   if (total_bytes > len)
6946     return 0;
6947   words = total_bytes / UNITS_PER_WORD;
6948
6949   real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
6950
6951   for (byte = 0; byte < total_bytes; byte++)
6952     {
6953       int bitpos = byte * BITS_PER_UNIT;
6954       value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
6955
6956       if (total_bytes > UNITS_PER_WORD)
6957         {
6958           word = byte / UNITS_PER_WORD;
6959           if (FLOAT_WORDS_BIG_ENDIAN)
6960             word = (words - 1) - word;
6961           offset = word * UNITS_PER_WORD;
6962           if (BYTES_BIG_ENDIAN)
6963             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6964           else
6965             offset += byte % UNITS_PER_WORD;
6966         }
6967       else
6968         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
6969       ptr[offset] = value;
6970     }
6971   return total_bytes;
6972 }
6973
6974 /* Subroutine of native_encode_expr.  Encode the COMPLEX_CST
6975    specified by EXPR into the buffer PTR of length LEN bytes.
6976    Return the number of bytes placed in the buffer, or zero
6977    upon failure.  */
6978
6979 static int
6980 native_encode_complex (tree expr, unsigned char *ptr, int len)
6981 {
6982   int rsize, isize;
6983   tree part;
6984
6985   part = TREE_REALPART (expr);
6986   rsize = native_encode_expr (part, ptr, len);
6987   if (rsize == 0)
6988     return 0;
6989   part = TREE_IMAGPART (expr);
6990   isize = native_encode_expr (part, ptr+rsize, len-rsize);
6991   if (isize != rsize)
6992     return 0;
6993   return rsize + isize;
6994 }
6995
6996
6997 /* Subroutine of native_encode_expr.  Encode the VECTOR_CST
6998    specified by EXPR into the buffer PTR of length LEN bytes.
6999    Return the number of bytes placed in the buffer, or zero
7000    upon failure.  */
7001
7002 static int
7003 native_encode_vector (tree expr, unsigned char *ptr, int len)
7004 {
7005   int i, size, offset, count;
7006   tree itype, elem, elements;
7007
7008   offset = 0;
7009   elements = TREE_VECTOR_CST_ELTS (expr);
7010   count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7011   itype = TREE_TYPE (TREE_TYPE (expr));
7012   size = GET_MODE_SIZE (TYPE_MODE (itype));
7013   for (i = 0; i < count; i++)
7014     {
7015       if (elements)
7016         {
7017           elem = TREE_VALUE (elements);
7018           elements = TREE_CHAIN (elements);
7019         }
7020       else
7021         elem = NULL_TREE;
7022
7023       if (elem)
7024         {
7025           if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7026             return 0;
7027         }
7028       else
7029         {
7030           if (offset + size > len)
7031             return 0;
7032           memset (ptr+offset, 0, size);
7033         }
7034       offset += size;
7035     }
7036   return offset;
7037 }
7038
7039
7040 /* Subroutine of fold_view_convert_expr.  Encode the INTEGER_CST,
7041    REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7042    buffer PTR of length LEN bytes.  Return the number of bytes
7043    placed in the buffer, or zero upon failure.  */
7044
7045 static int
7046 native_encode_expr (tree expr, unsigned char *ptr, int len)
7047 {
7048   switch (TREE_CODE (expr))
7049     {
7050     case INTEGER_CST:
7051       return native_encode_int (expr, ptr, len);
7052
7053     case REAL_CST:
7054       return native_encode_real (expr, ptr, len);
7055
7056     case COMPLEX_CST:
7057       return native_encode_complex (expr, ptr, len);
7058
7059     case VECTOR_CST:
7060       return native_encode_vector (expr, ptr, len);
7061
7062     default:
7063       return 0;
7064     }
7065 }
7066
7067
7068 /* Subroutine of native_interpret_expr.  Interpret the contents of
7069    the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7070    If the buffer cannot be interpreted, return NULL_TREE.  */
7071
7072 static tree
7073 native_interpret_int (tree type, unsigned char *ptr, int len)
7074 {
7075   int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7076   int byte, offset, word, words;
7077   unsigned char value;
7078   unsigned int HOST_WIDE_INT lo = 0;
7079   HOST_WIDE_INT hi = 0;
7080
7081   if (total_bytes > len)
7082     return NULL_TREE;
7083   if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7084     return NULL_TREE;
7085   words = total_bytes / UNITS_PER_WORD;
7086
7087   for (byte = 0; byte < total_bytes; byte++)
7088     {
7089       int bitpos = byte * BITS_PER_UNIT;
7090       if (total_bytes > UNITS_PER_WORD)
7091         {
7092           word = byte / UNITS_PER_WORD;
7093           if (WORDS_BIG_ENDIAN)
7094             word = (words - 1) - word;
7095           offset = word * UNITS_PER_WORD;
7096           if (BYTES_BIG_ENDIAN)
7097             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7098           else
7099             offset += byte % UNITS_PER_WORD;
7100         }
7101       else
7102         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7103       value = ptr[offset];
7104
7105       if (bitpos < HOST_BITS_PER_WIDE_INT)
7106         lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7107       else
7108         hi |= (unsigned HOST_WIDE_INT) value
7109               << (bitpos - HOST_BITS_PER_WIDE_INT);
7110     }
7111
7112   return build_int_cst_wide_type (type, lo, hi);
7113 }
7114
7115
7116 /* Subroutine of native_interpret_expr.  Interpret the contents of
7117    the buffer PTR of length LEN as a REAL_CST of type TYPE.
7118    If the buffer cannot be interpreted, return NULL_TREE.  */
7119
7120 static tree
7121 native_interpret_real (tree type, unsigned char *ptr, int len)
7122 {
7123   enum machine_mode mode = TYPE_MODE (type);
7124   int total_bytes = GET_MODE_SIZE (mode);
7125   int byte, offset, word, words;
7126   unsigned char value;
7127   /* There are always 32 bits in each long, no matter the size of
7128      the hosts long.  We handle floating point representations with
7129      up to 192 bits.  */
7130   REAL_VALUE_TYPE r;
7131   long tmp[6];
7132
7133   total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7134   if (total_bytes > len || total_bytes > 24)
7135     return NULL_TREE;
7136   words = total_bytes / UNITS_PER_WORD;
7137
7138   memset (tmp, 0, sizeof (tmp));
7139   for (byte = 0; byte < total_bytes; byte++)
7140     {
7141       int bitpos = byte * BITS_PER_UNIT;
7142       if (total_bytes > UNITS_PER_WORD)
7143         {
7144           word = byte / UNITS_PER_WORD;
7145           if (FLOAT_WORDS_BIG_ENDIAN)
7146             word = (words - 1) - word;
7147           offset = word * UNITS_PER_WORD;
7148           if (BYTES_BIG_ENDIAN)
7149             offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7150           else
7151             offset += byte % UNITS_PER_WORD;
7152         }
7153       else
7154         offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7155       value = ptr[offset];
7156
7157       tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7158     }
7159
7160   real_from_target (&r, tmp, mode);
7161   return build_real (type, r);
7162 }
7163
7164
7165 /* Subroutine of native_interpret_expr.  Interpret the contents of
7166    the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7167    If the buffer cannot be interpreted, return NULL_TREE.  */
7168
7169 static tree
7170 native_interpret_complex (tree type, unsigned char *ptr, int len)
7171 {
7172   tree etype, rpart, ipart;
7173   int size;
7174
7175   etype = TREE_TYPE (type);
7176   size = GET_MODE_SIZE (TYPE_MODE (etype));
7177   if (size * 2 > len)
7178     return NULL_TREE;
7179   rpart = native_interpret_expr (etype, ptr, size);
7180   if (!rpart)
7181     return NULL_TREE;
7182   ipart = native_interpret_expr (etype, ptr+size, size);
7183   if (!ipart)
7184     return NULL_TREE;
7185   return build_complex (type, rpart, ipart);
7186 }
7187
7188
7189 /* Subroutine of native_interpret_expr.  Interpret the contents of
7190    the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7191    If the buffer cannot be interpreted, return NULL_TREE.  */
7192
7193 static tree
7194 native_interpret_vector (tree type, unsigned char *ptr, int len)
7195 {
7196   tree etype, elem, elements;
7197   int i, size, count;
7198
7199   etype = TREE_TYPE (type);
7200   size = GET_MODE_SIZE (TYPE_MODE (etype));
7201   count = TYPE_VECTOR_SUBPARTS (type);
7202   if (size * count > len)
7203     return NULL_TREE;
7204
7205   elements = NULL_TREE;
7206   for (i = count - 1; i >= 0; i--)
7207     {
7208       elem = native_interpret_expr (etype, ptr+(i*size), size);
7209       if (!elem)
7210         return NULL_TREE;
7211       elements = tree_cons (NULL_TREE, elem, elements);
7212     }
7213   return build_vector (type, elements);
7214 }
7215
7216
7217 /* Subroutine of fold_view_convert_expr.  Interpret the contents of
7218    the buffer PTR of length LEN as a constant of type TYPE.  For
7219    INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7220    we return a REAL_CST, etc...  If the buffer cannot be interpreted,
7221    return NULL_TREE.  */
7222
7223 static tree
7224 native_interpret_expr (tree type, unsigned char *ptr, int len)
7225 {
7226   switch (TREE_CODE (type))
7227     {
7228     case INTEGER_TYPE:
7229     case ENUMERAL_TYPE:
7230     case BOOLEAN_TYPE:
7231       return native_interpret_int (type, ptr, len);
7232
7233     case REAL_TYPE:
7234       return native_interpret_real (type, ptr, len);
7235
7236     case COMPLEX_TYPE:
7237       return native_interpret_complex (type, ptr, len);
7238
7239     case VECTOR_TYPE:
7240       return native_interpret_vector (type, ptr, len);
7241
7242     default:
7243       return NULL_TREE;
7244     }
7245 }
7246
7247
7248 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7249    TYPE at compile-time.  If we're unable to perform the conversion
7250    return NULL_TREE.  */
7251
7252 static tree
7253 fold_view_convert_expr (tree type, tree expr)
7254 {
7255   /* We support up to 512-bit values (for V8DFmode).  */
7256   unsigned char buffer[64];
7257   int len;
7258
7259   /* Check that the host and target are sane.  */
7260   if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7261     return NULL_TREE;
7262
7263   len = native_encode_expr (expr, buffer, sizeof (buffer));
7264   if (len == 0)
7265     return NULL_TREE;
7266
7267   return native_interpret_expr (type, buffer, len);
7268 }
7269
7270
7271 /* Fold a unary expression of code CODE and type TYPE with operand
7272    OP0.  Return the folded expression if folding is successful.
7273    Otherwise, return NULL_TREE.  */
7274
7275 tree
7276 fold_unary (enum tree_code code, tree type, tree op0)
7277 {
7278   tree tem;
7279   tree arg0;
7280   enum tree_code_class kind = TREE_CODE_CLASS (code);
7281
7282   gcc_assert (IS_EXPR_CODE_CLASS (kind)
7283               && TREE_CODE_LENGTH (code) == 1);
7284
7285   arg0 = op0;
7286   if (arg0)
7287     {
7288       if (code == NOP_EXPR || code == CONVERT_EXPR
7289           || code == FLOAT_EXPR || code == ABS_EXPR)
7290         {
7291           /* Don't use STRIP_NOPS, because signedness of argument type
7292              matters.  */
7293           STRIP_SIGN_NOPS (arg0);
7294         }
7295       else
7296         {
7297           /* Strip any conversions that don't change the mode.  This
7298              is safe for every expression, except for a comparison
7299              expression because its signedness is derived from its
7300              operands.
7301
7302              Note that this is done as an internal manipulation within
7303              the constant folder, in order to find the simplest
7304              representation of the arguments so that their form can be
7305              studied.  In any cases, the appropriate type conversions
7306              should be put back in the tree that will get out of the
7307              constant folder.  */
7308           STRIP_NOPS (arg0);
7309         }
7310     }
7311
7312   if (TREE_CODE_CLASS (code) == tcc_unary)
7313     {
7314       if (TREE_CODE (arg0) == COMPOUND_EXPR)
7315         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7316                        fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
7317       else if (TREE_CODE (arg0) == COND_EXPR)
7318         {
7319           tree arg01 = TREE_OPERAND (arg0, 1);
7320           tree arg02 = TREE_OPERAND (arg0, 2);
7321           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7322             arg01 = fold_build1 (code, type, arg01);
7323           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7324             arg02 = fold_build1 (code, type, arg02);
7325           tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7326                              arg01, arg02);
7327
7328           /* If this was a conversion, and all we did was to move into
7329              inside the COND_EXPR, bring it back out.  But leave it if
7330              it is a conversion from integer to integer and the
7331              result precision is no wider than a word since such a
7332              conversion is cheap and may be optimized away by combine,
7333              while it couldn't if it were outside the COND_EXPR.  Then return
7334              so we don't get into an infinite recursion loop taking the
7335              conversion out and then back in.  */
7336
7337           if ((code == NOP_EXPR || code == CONVERT_EXPR
7338                || code == NON_LVALUE_EXPR)
7339               && TREE_CODE (tem) == COND_EXPR
7340               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7341               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7342               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7343               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7344               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7345                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7346               && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7347                      && (INTEGRAL_TYPE_P
7348                          (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7349                      && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7350                   || flag_syntax_only))
7351             tem = build1 (code, type,
7352                           build3 (COND_EXPR,
7353                                   TREE_TYPE (TREE_OPERAND
7354                                              (TREE_OPERAND (tem, 1), 0)),
7355                                   TREE_OPERAND (tem, 0),
7356                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7357                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7358           return tem;
7359         }
7360       else if (COMPARISON_CLASS_P (arg0))
7361         {
7362           if (TREE_CODE (type) == BOOLEAN_TYPE)
7363             {
7364               arg0 = copy_node (arg0);
7365               TREE_TYPE (arg0) = type;
7366               return arg0;
7367             }
7368           else if (TREE_CODE (type) != INTEGER_TYPE)
7369             return fold_build3 (COND_EXPR, type, arg0,
7370                                 fold_build1 (code, type,
7371                                              integer_one_node),
7372                                 fold_build1 (code, type,
7373                                              integer_zero_node));
7374         }
7375    }
7376
7377   switch (code)
7378     {
7379     case NOP_EXPR:
7380     case FLOAT_EXPR:
7381     case CONVERT_EXPR:
7382     case FIX_TRUNC_EXPR:
7383       if (TREE_TYPE (op0) == type)
7384         return op0;
7385       
7386       /* If we have (type) (a CMP b) and type is an integral type, return
7387          new expression involving the new type.  */
7388       if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7389         return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7390                             TREE_OPERAND (op0, 1));
7391
7392       /* Handle cases of two conversions in a row.  */
7393       if (TREE_CODE (op0) == NOP_EXPR
7394           || TREE_CODE (op0) == CONVERT_EXPR)
7395         {
7396           tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7397           tree inter_type = TREE_TYPE (op0);
7398           int inside_int = INTEGRAL_TYPE_P (inside_type);
7399           int inside_ptr = POINTER_TYPE_P (inside_type);
7400           int inside_float = FLOAT_TYPE_P (inside_type);
7401           int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7402           unsigned int inside_prec = TYPE_PRECISION (inside_type);
7403           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7404           int inter_int = INTEGRAL_TYPE_P (inter_type);
7405           int inter_ptr = POINTER_TYPE_P (inter_type);
7406           int inter_float = FLOAT_TYPE_P (inter_type);
7407           int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7408           unsigned int inter_prec = TYPE_PRECISION (inter_type);
7409           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7410           int final_int = INTEGRAL_TYPE_P (type);
7411           int final_ptr = POINTER_TYPE_P (type);
7412           int final_float = FLOAT_TYPE_P (type);
7413           int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7414           unsigned int final_prec = TYPE_PRECISION (type);
7415           int final_unsignedp = TYPE_UNSIGNED (type);
7416
7417           /* In addition to the cases of two conversions in a row
7418              handled below, if we are converting something to its own
7419              type via an object of identical or wider precision, neither
7420              conversion is needed.  */
7421           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7422               && (((inter_int || inter_ptr) && final_int)
7423                   || (inter_float && final_float))
7424               && inter_prec >= final_prec)
7425             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7426
7427           /* Likewise, if the intermediate and final types are either both
7428              float or both integer, we don't need the middle conversion if
7429              it is wider than the final type and doesn't change the signedness
7430              (for integers).  Avoid this if the final type is a pointer
7431              since then we sometimes need the inner conversion.  Likewise if
7432              the outer has a precision not equal to the size of its mode.  */
7433           if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
7434                || (inter_float && inside_float)
7435                || (inter_vec && inside_vec))
7436               && inter_prec >= inside_prec
7437               && (inter_float || inter_vec
7438                   || inter_unsignedp == inside_unsignedp)
7439               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7440                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7441               && ! final_ptr
7442               && (! final_vec || inter_prec == inside_prec))
7443             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7444
7445           /* If we have a sign-extension of a zero-extended value, we can
7446              replace that by a single zero-extension.  */
7447           if (inside_int && inter_int && final_int
7448               && inside_prec < inter_prec && inter_prec < final_prec
7449               && inside_unsignedp && !inter_unsignedp)
7450             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7451
7452           /* Two conversions in a row are not needed unless:
7453              - some conversion is floating-point (overstrict for now), or
7454              - some conversion is a vector (overstrict for now), or
7455              - the intermediate type is narrower than both initial and
7456                final, or
7457              - the intermediate type and innermost type differ in signedness,
7458                and the outermost type is wider than the intermediate, or
7459              - the initial type is a pointer type and the precisions of the
7460                intermediate and final types differ, or
7461              - the final type is a pointer type and the precisions of the
7462                initial and intermediate types differ.
7463              - the final type is a pointer type and the initial type not
7464              - the initial type is a pointer to an array and the final type
7465                not.  */
7466           if (! inside_float && ! inter_float && ! final_float
7467               && ! inside_vec && ! inter_vec && ! final_vec
7468               && (inter_prec >= inside_prec || inter_prec >= final_prec)
7469               && ! (inside_int && inter_int
7470                     && inter_unsignedp != inside_unsignedp
7471                     && inter_prec < final_prec)
7472               && ((inter_unsignedp && inter_prec > inside_prec)
7473                   == (final_unsignedp && final_prec > inter_prec))
7474               && ! (inside_ptr && inter_prec != final_prec)
7475               && ! (final_ptr && inside_prec != inter_prec)
7476               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7477                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
7478               && final_ptr == inside_ptr
7479               && ! (inside_ptr
7480                     && TREE_CODE (TREE_TYPE (inside_type)) == ARRAY_TYPE
7481                     && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7482             return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7483         }
7484
7485       /* Handle (T *)&A.B.C for A being of type T and B and C
7486          living at offset zero.  This occurs frequently in
7487          C++ upcasting and then accessing the base.  */
7488       if (TREE_CODE (op0) == ADDR_EXPR
7489           && POINTER_TYPE_P (type)
7490           && handled_component_p (TREE_OPERAND (op0, 0)))
7491         {
7492           HOST_WIDE_INT bitsize, bitpos;
7493           tree offset;
7494           enum machine_mode mode;
7495           int unsignedp, volatilep;
7496           tree base = TREE_OPERAND (op0, 0);
7497           base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7498                                       &mode, &unsignedp, &volatilep, false);
7499           /* If the reference was to a (constant) zero offset, we can use
7500              the address of the base if it has the same base type
7501              as the result type.  */
7502           if (! offset && bitpos == 0
7503               && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7504                   == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7505             return fold_convert (type, build_fold_addr_expr (base));
7506         }
7507
7508       if ((TREE_CODE (op0) == MODIFY_EXPR
7509            || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
7510           && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
7511           /* Detect assigning a bitfield.  */
7512           && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
7513                && DECL_BIT_FIELD
7514                (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
7515         {
7516           /* Don't leave an assignment inside a conversion
7517              unless assigning a bitfield.  */
7518           tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
7519           /* First do the assignment, then return converted constant.  */
7520           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7521           TREE_NO_WARNING (tem) = 1;
7522           TREE_USED (tem) = 1;
7523           return tem;
7524         }
7525
7526       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7527          constants (if x has signed type, the sign bit cannot be set
7528          in c).  This folds extension into the BIT_AND_EXPR.  */
7529       if (INTEGRAL_TYPE_P (type)
7530           && TREE_CODE (type) != BOOLEAN_TYPE
7531           && TREE_CODE (op0) == BIT_AND_EXPR
7532           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7533         {
7534           tree and = op0;
7535           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7536           int change = 0;
7537
7538           if (TYPE_UNSIGNED (TREE_TYPE (and))
7539               || (TYPE_PRECISION (type)
7540                   <= TYPE_PRECISION (TREE_TYPE (and))))
7541             change = 1;
7542           else if (TYPE_PRECISION (TREE_TYPE (and1))
7543                    <= HOST_BITS_PER_WIDE_INT
7544                    && host_integerp (and1, 1))
7545             {
7546               unsigned HOST_WIDE_INT cst;
7547
7548               cst = tree_low_cst (and1, 1);
7549               cst &= (HOST_WIDE_INT) -1
7550                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7551               change = (cst == 0);
7552 #ifdef LOAD_EXTEND_OP
7553               if (change
7554                   && !flag_syntax_only
7555                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7556                       == ZERO_EXTEND))
7557                 {
7558                   tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
7559                   and0 = fold_convert (uns, and0);
7560                   and1 = fold_convert (uns, and1);
7561                 }
7562 #endif
7563             }
7564           if (change)
7565             {
7566               tem = build_int_cst_wide (type, TREE_INT_CST_LOW (and1),
7567                                         TREE_INT_CST_HIGH (and1));
7568               tem = force_fit_type (tem, 0, TREE_OVERFLOW (and1),
7569                                     TREE_CONSTANT_OVERFLOW (and1));
7570               return fold_build2 (BIT_AND_EXPR, type,
7571                                   fold_convert (type, and0), tem);
7572             }
7573         }
7574
7575       /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
7576          T2 being pointers to types of the same size.  */
7577       if (POINTER_TYPE_P (type)
7578           && BINARY_CLASS_P (arg0)
7579           && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7580           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7581         {
7582           tree arg00 = TREE_OPERAND (arg0, 0);
7583           tree t0 = type;
7584           tree t1 = TREE_TYPE (arg00);
7585           tree tt0 = TREE_TYPE (t0);
7586           tree tt1 = TREE_TYPE (t1);
7587           tree s0 = TYPE_SIZE (tt0);
7588           tree s1 = TYPE_SIZE (tt1);
7589
7590           if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
7591             return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
7592                            TREE_OPERAND (arg0, 1));
7593         }
7594
7595       /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7596          of the same precision, and X is a integer type not narrower than
7597          types T1 or T2, i.e. the cast (T2)X isn't an extension.  */
7598       if (INTEGRAL_TYPE_P (type)
7599           && TREE_CODE (op0) == BIT_NOT_EXPR
7600           && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7601           && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
7602               || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
7603           && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7604         {
7605           tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7606           if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7607               && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7608             return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7609         }
7610
7611       tem = fold_convert_const (code, type, arg0);
7612       return tem ? tem : NULL_TREE;
7613
7614     case VIEW_CONVERT_EXPR:
7615       if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7616         return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7617       return fold_view_convert_expr (type, op0);
7618
7619     case NEGATE_EXPR:
7620       tem = fold_negate_expr (arg0);
7621       if (tem)
7622         return fold_convert (type, tem);
7623       return NULL_TREE;
7624
7625     case ABS_EXPR:
7626       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7627         return fold_abs_const (arg0, type);
7628       else if (TREE_CODE (arg0) == NEGATE_EXPR)
7629         return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
7630       /* Convert fabs((double)float) into (double)fabsf(float).  */
7631       else if (TREE_CODE (arg0) == NOP_EXPR
7632                && TREE_CODE (type) == REAL_TYPE)
7633         {
7634           tree targ0 = strip_float_extensions (arg0);
7635           if (targ0 != arg0)
7636             return fold_convert (type, fold_build1 (ABS_EXPR,
7637                                                     TREE_TYPE (targ0),
7638                                                     targ0));
7639         }
7640       /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on.  */
7641       else if (tree_expr_nonnegative_p (arg0) || TREE_CODE (arg0) == ABS_EXPR)
7642         return arg0;
7643
7644       /* Strip sign ops from argument.  */
7645       if (TREE_CODE (type) == REAL_TYPE)
7646         {
7647           tem = fold_strip_sign_ops (arg0);
7648           if (tem)
7649             return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
7650         }
7651       return NULL_TREE;
7652
7653     case CONJ_EXPR:
7654       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7655         return fold_convert (type, arg0);
7656       if (TREE_CODE (arg0) == COMPLEX_EXPR)
7657         {
7658           tree itype = TREE_TYPE (type);
7659           tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
7660           tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
7661           return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
7662         }
7663       if (TREE_CODE (arg0) == COMPLEX_CST)
7664         {
7665           tree itype = TREE_TYPE (type);
7666           tree rpart = fold_convert (itype, TREE_REALPART (arg0));
7667           tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
7668           return build_complex (type, rpart, negate_expr (ipart));
7669         }
7670       if (TREE_CODE (arg0) == CONJ_EXPR)
7671         return fold_convert (type, TREE_OPERAND (arg0, 0));
7672       return NULL_TREE;
7673
7674     case BIT_NOT_EXPR:
7675       if (TREE_CODE (arg0) == INTEGER_CST)
7676         return fold_not_const (arg0, type);
7677       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
7678         return TREE_OPERAND (arg0, 0);
7679       /* Convert ~ (-A) to A - 1.  */
7680       else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
7681         return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
7682                             build_int_cst (type, 1));
7683       /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
7684       else if (INTEGRAL_TYPE_P (type)
7685                && ((TREE_CODE (arg0) == MINUS_EXPR
7686                     && integer_onep (TREE_OPERAND (arg0, 1)))
7687                    || (TREE_CODE (arg0) == PLUS_EXPR
7688                        && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
7689         return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
7690       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
7691       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7692                && (tem = fold_unary (BIT_NOT_EXPR, type,
7693                                      fold_convert (type,
7694                                                    TREE_OPERAND (arg0, 0)))))
7695         return fold_build2 (BIT_XOR_EXPR, type, tem,
7696                             fold_convert (type, TREE_OPERAND (arg0, 1)));
7697       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7698                && (tem = fold_unary (BIT_NOT_EXPR, type,
7699                                      fold_convert (type,
7700                                                    TREE_OPERAND (arg0, 1)))))
7701         return fold_build2 (BIT_XOR_EXPR, type,
7702                             fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
7703
7704       return NULL_TREE;
7705
7706     case TRUTH_NOT_EXPR:
7707       /* The argument to invert_truthvalue must have Boolean type.  */
7708       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7709           arg0 = fold_convert (boolean_type_node, arg0);
7710
7711       /* Note that the operand of this must be an int
7712          and its values must be 0 or 1.
7713          ("true" is a fixed value perhaps depending on the language,
7714          but we don't handle values other than 1 correctly yet.)  */
7715       tem = fold_truth_not_expr (arg0);
7716       if (!tem)
7717         return NULL_TREE;
7718       return fold_convert (type, tem);
7719
7720     case REALPART_EXPR:
7721       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7722         return fold_convert (type, arg0);
7723       if (TREE_CODE (arg0) == COMPLEX_EXPR)
7724         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7725                                  TREE_OPERAND (arg0, 1));
7726       if (TREE_CODE (arg0) == COMPLEX_CST)
7727         return fold_convert (type, TREE_REALPART (arg0));
7728       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7729         {
7730           tree itype = TREE_TYPE (TREE_TYPE (arg0));
7731           tem = fold_build2 (TREE_CODE (arg0), itype,
7732                              fold_build1 (REALPART_EXPR, itype,
7733                                           TREE_OPERAND (arg0, 0)),
7734                              fold_build1 (REALPART_EXPR, itype,
7735                                           TREE_OPERAND (arg0, 1)));
7736           return fold_convert (type, tem);
7737         }
7738       if (TREE_CODE (arg0) == CONJ_EXPR)
7739         {
7740           tree itype = TREE_TYPE (TREE_TYPE (arg0));
7741           tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
7742           return fold_convert (type, tem);
7743         }
7744       if (TREE_CODE (arg0) == CALL_EXPR)
7745         {
7746           tree fn = get_callee_fndecl (arg0);
7747           if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7748             switch (DECL_FUNCTION_CODE (fn))
7749               {
7750               CASE_FLT_FN (BUILT_IN_CEXPI):
7751                 fn = mathfn_built_in (type, BUILT_IN_COS);
7752                 return build_function_call_expr (fn, TREE_OPERAND (arg0, 1));
7753
7754               default:;
7755               }
7756         }
7757       return NULL_TREE;
7758
7759     case IMAGPART_EXPR:
7760       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7761         return fold_convert (type, integer_zero_node);
7762       if (TREE_CODE (arg0) == COMPLEX_EXPR)
7763         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7764                                  TREE_OPERAND (arg0, 0));
7765       if (TREE_CODE (arg0) == COMPLEX_CST)
7766         return fold_convert (type, TREE_IMAGPART (arg0));
7767       if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7768         {
7769           tree itype = TREE_TYPE (TREE_TYPE (arg0));
7770           tem = fold_build2 (TREE_CODE (arg0), itype,
7771                              fold_build1 (IMAGPART_EXPR, itype,
7772                                           TREE_OPERAND (arg0, 0)),
7773                              fold_build1 (IMAGPART_EXPR, itype,
7774                                           TREE_OPERAND (arg0, 1)));
7775           return fold_convert (type, tem);
7776         }
7777       if (TREE_CODE (arg0) == CONJ_EXPR)
7778         {
7779           tree itype = TREE_TYPE (TREE_TYPE (arg0));
7780           tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
7781           return fold_convert (type, negate_expr (tem));
7782         }
7783       if (TREE_CODE (arg0) == CALL_EXPR)
7784         {
7785           tree fn = get_callee_fndecl (arg0);
7786           if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
7787             switch (DECL_FUNCTION_CODE (fn))
7788               {
7789               CASE_FLT_FN (BUILT_IN_CEXPI):
7790                 fn = mathfn_built_in (type, BUILT_IN_SIN);
7791                 return build_function_call_expr (fn, TREE_OPERAND (arg0, 1));
7792
7793               default:;
7794               }
7795         }
7796       return NULL_TREE;
7797
7798     default:
7799       return NULL_TREE;
7800     } /* switch (code) */
7801 }
7802
7803 /* Fold a binary expression of code CODE and type TYPE with operands
7804    OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
7805    Return the folded expression if folding is successful.  Otherwise,
7806    return NULL_TREE.  */
7807
7808 static tree
7809 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
7810 {
7811   enum tree_code compl_code;
7812
7813   if (code == MIN_EXPR)
7814     compl_code = MAX_EXPR;
7815   else if (code == MAX_EXPR)
7816     compl_code = MIN_EXPR;
7817   else
7818     gcc_unreachable ();
7819
7820   /* MIN (MAX (a, b), b) == b.  */
7821   if (TREE_CODE (op0) == compl_code
7822       && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
7823     return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
7824
7825   /* MIN (MAX (b, a), b) == b.  */
7826   if (TREE_CODE (op0) == compl_code
7827       && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
7828       && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
7829     return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
7830
7831   /* MIN (a, MAX (a, b)) == a.  */
7832   if (TREE_CODE (op1) == compl_code
7833       && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
7834       && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
7835     return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
7836
7837   /* MIN (a, MAX (b, a)) == a.  */
7838   if (TREE_CODE (op1) == compl_code
7839       && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
7840       && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
7841     return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
7842
7843   return NULL_TREE;
7844 }
7845
7846 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
7847    by changing CODE to reduce the magnitude of constants involved in
7848    ARG0 of the comparison.
7849    Returns a canonicalized comparison tree if a simplification was
7850    possible, otherwise returns NULL_TREE.  */
7851
7852 static tree
7853 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
7854                                  tree arg0, tree arg1)
7855 {
7856   enum tree_code code0 = TREE_CODE (arg0);
7857   tree t, cst0 = NULL_TREE;
7858   int sgn0;
7859   bool swap = false;
7860
7861   /* Match A +- CST code arg1 and CST code arg1.  */
7862   if (!(((code0 == MINUS_EXPR
7863           || code0 == PLUS_EXPR)
7864          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7865         || code0 == INTEGER_CST))
7866     return NULL_TREE;
7867
7868   /* Identify the constant in arg0 and its sign.  */
7869   if (code0 == INTEGER_CST)
7870     cst0 = arg0;
7871   else
7872     cst0 = TREE_OPERAND (arg0, 1);
7873   sgn0 = tree_int_cst_sgn (cst0);
7874
7875   /* Overflowed constants and zero will cause problems.  */
7876   if (integer_zerop (cst0)
7877       || TREE_OVERFLOW (cst0))
7878     return NULL_TREE;
7879
7880   /* See if we can reduce the magnitude of the constant in
7881      arg0 by changing the comparison code.  */
7882   if (code0 == INTEGER_CST)
7883     {
7884       /* CST <= arg1  ->  CST-1 < arg1.  */
7885       if (code == LE_EXPR && sgn0 == 1)
7886         code = LT_EXPR;
7887       /* -CST < arg1  ->  -CST-1 <= arg1.  */
7888       else if (code == LT_EXPR && sgn0 == -1)
7889         code = LE_EXPR;
7890       /* CST > arg1  ->  CST-1 >= arg1.  */
7891       else if (code == GT_EXPR && sgn0 == 1)
7892         code = GE_EXPR;
7893       /* -CST >= arg1  ->  -CST-1 > arg1.  */
7894       else if (code == GE_EXPR && sgn0 == -1)
7895         code = GT_EXPR;
7896       else
7897         return NULL_TREE;
7898       /* arg1 code' CST' might be more canonical.  */
7899       swap = true;
7900     }
7901   else
7902     {
7903       /* A - CST < arg1  ->  A - CST-1 <= arg1.  */
7904       if (code == LT_EXPR
7905           && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
7906         code = LE_EXPR;
7907       /* A + CST > arg1  ->  A + CST-1 >= arg1.  */
7908       else if (code == GT_EXPR
7909                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
7910         code = GE_EXPR;
7911       /* A + CST <= arg1  ->  A + CST-1 < arg1.  */
7912       else if (code == LE_EXPR
7913                && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
7914         code = LT_EXPR;
7915       /* A - CST >= arg1  ->  A - CST-1 > arg1.  */
7916       else if (code == GE_EXPR
7917                && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
7918         code = GT_EXPR;
7919       else
7920         return NULL_TREE;
7921     }
7922
7923   /* Now build the constant reduced in magnitude.  */
7924   t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
7925                        cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
7926   if (code0 != INTEGER_CST)
7927     t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
7928
7929   /* If swapping might yield to a more canonical form, do so.  */
7930   if (swap)
7931     return fold_build2 (swap_tree_comparison (code), type, arg1, t);
7932   else
7933     return fold_build2 (code, type, t, arg1);
7934 }
7935
7936 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
7937    overflow further.  Try to decrease the magnitude of constants involved
7938    by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
7939    and put sole constants at the second argument position.
7940    Returns the canonicalized tree if changed, otherwise NULL_TREE.  */
7941
7942 static tree
7943 maybe_canonicalize_comparison (enum tree_code code, tree type,
7944                                tree arg0, tree arg1)
7945 {
7946   tree t;
7947
7948   /* In principle pointers also have undefined overflow behavior,
7949      but that causes problems elsewhere.  */
7950   if ((flag_wrapv || flag_trapv)
7951       || (TYPE_UNSIGNED (TREE_TYPE (arg0))
7952           || POINTER_TYPE_P (TREE_TYPE (arg0))))
7953     return NULL_TREE;
7954
7955   /* Try canonicalization by simplifying arg0.  */
7956   t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1);
7957   if (t)
7958     return t;
7959
7960   /* Try canonicalization by simplifying arg1 using the swapped
7961      comparison.  */
7962   code = swap_tree_comparison (code);
7963   return maybe_canonicalize_comparison_1 (code, type, arg1, arg0);
7964 }
7965
7966 /* Subroutine of fold_binary.  This routine performs all of the
7967    transformations that are common to the equality/inequality
7968    operators (EQ_EXPR and NE_EXPR) and the ordering operators
7969    (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR).  Callers other than
7970    fold_binary should call fold_binary.  Fold a comparison with
7971    tree code CODE and type TYPE with operands OP0 and OP1.  Return
7972    the folded comparison or NULL_TREE.  */
7973
7974 static tree
7975 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
7976 {
7977   tree arg0, arg1, tem;
7978
7979   arg0 = op0;
7980   arg1 = op1;
7981
7982   STRIP_SIGN_NOPS (arg0);
7983   STRIP_SIGN_NOPS (arg1);
7984
7985   tem = fold_relational_const (code, type, arg0, arg1);
7986   if (tem != NULL_TREE)
7987     return tem;
7988
7989   /* If one arg is a real or integer constant, put it last.  */
7990   if (tree_swap_operands_p (arg0, arg1, true))
7991     return fold_build2 (swap_tree_comparison (code), type, op1, op0);
7992
7993   /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
7994   if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7995       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7996           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
7997           && !TYPE_UNSIGNED (TREE_TYPE (arg1))
7998           && !(flag_wrapv || flag_trapv))
7999       && (TREE_CODE (arg1) == INTEGER_CST
8000           && !TREE_OVERFLOW (arg1)))
8001     {
8002       tree const1 = TREE_OPERAND (arg0, 1);
8003       tree const2 = arg1;
8004       tree variable = TREE_OPERAND (arg0, 0);
8005       tree lhs;
8006       int lhs_add;
8007       lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8008
8009       lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8010                          TREE_TYPE (arg1), const2, const1);
8011       if (TREE_CODE (lhs) == TREE_CODE (arg1)
8012           && (TREE_CODE (lhs) != INTEGER_CST
8013               || !TREE_OVERFLOW (lhs)))
8014         return fold_build2 (code, type, variable, lhs);
8015     }
8016
8017   /* For comparisons of pointers we can decompose it to a compile time
8018      comparison of the base objects and the offsets into the object.
8019      This requires at least one operand being an ADDR_EXPR to do more
8020      than the operand_equal_p test below.  */
8021   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8022       && (TREE_CODE (arg0) == ADDR_EXPR
8023           || TREE_CODE (arg1) == ADDR_EXPR))
8024     {
8025       tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8026       HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8027       enum machine_mode mode;
8028       int volatilep, unsignedp;
8029       bool indirect_base0 = false;
8030
8031       /* Get base and offset for the access.  Strip ADDR_EXPR for
8032          get_inner_reference, but put it back by stripping INDIRECT_REF
8033          off the base object if possible.  */
8034       base0 = arg0;
8035       if (TREE_CODE (arg0) == ADDR_EXPR)
8036         {
8037           base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8038                                        &bitsize, &bitpos0, &offset0, &mode,
8039                                        &unsignedp, &volatilep, false);
8040           if (TREE_CODE (base0) == INDIRECT_REF)
8041             base0 = TREE_OPERAND (base0, 0);
8042           else
8043             indirect_base0 = true;
8044         }
8045
8046       base1 = arg1;
8047       if (TREE_CODE (arg1) == ADDR_EXPR)
8048         {
8049           base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8050                                        &bitsize, &bitpos1, &offset1, &mode,
8051                                        &unsignedp, &volatilep, false);
8052           /* We have to make sure to have an indirect/non-indirect base1
8053              just the same as we did for base0.  */
8054           if (TREE_CODE (base1) == INDIRECT_REF
8055               && !indirect_base0)
8056             base1 = TREE_OPERAND (base1, 0);
8057           else if (!indirect_base0)
8058             base1 = NULL_TREE;
8059         }
8060       else if (indirect_base0)
8061         base1 = NULL_TREE;
8062
8063       /* If we have equivalent bases we might be able to simplify.  */
8064       if (base0 && base1
8065           && operand_equal_p (base0, base1, 0))
8066         {
8067           /* We can fold this expression to a constant if the non-constant
8068              offset parts are equal.  */
8069           if (offset0 == offset1
8070               || (offset0 && offset1
8071                   && operand_equal_p (offset0, offset1, 0)))
8072             {
8073               switch (code)
8074                 {
8075                 case EQ_EXPR:
8076                   return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8077                 case NE_EXPR:
8078                   return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8079                 case LT_EXPR:
8080                   return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8081                 case LE_EXPR:
8082                   return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8083                 case GE_EXPR:
8084                   return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8085                 case GT_EXPR:
8086                   return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8087                 default:;
8088                 }
8089             }
8090           /* We can simplify the comparison to a comparison of the variable
8091              offset parts if the constant offset parts are equal.
8092              Be careful to use signed size type here because otherwise we
8093              mess with array offsets in the wrong way.  This is possible
8094              because pointer arithmetic is restricted to retain within an
8095              object and overflow on pointer differences is undefined as of
8096              6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8097           else if (bitpos0 == bitpos1)
8098             {
8099               tree signed_size_type_node;
8100               signed_size_type_node = signed_type_for (size_type_node);
8101
8102               /* By converting to signed size type we cover middle-end pointer
8103                  arithmetic which operates on unsigned pointer types of size
8104                  type size and ARRAY_REF offsets which are properly sign or
8105                  zero extended from their type in case it is narrower than
8106                  size type.  */
8107               if (offset0 == NULL_TREE)
8108                 offset0 = build_int_cst (signed_size_type_node, 0);
8109               else
8110                 offset0 = fold_convert (signed_size_type_node, offset0);
8111               if (offset1 == NULL_TREE)
8112                 offset1 = build_int_cst (signed_size_type_node, 0);
8113               else
8114                 offset1 = fold_convert (signed_size_type_node, offset1);
8115
8116               return fold_build2 (code, type, offset0, offset1);
8117             }
8118         }
8119     }
8120
8121   /* If this is a comparison of two exprs that look like an ARRAY_REF of the
8122      same object, then we can fold this to a comparison of the two offsets in
8123      signed size type.  This is possible because pointer arithmetic is
8124      restricted to retain within an object and overflow on pointer differences
8125      is undefined as of 6.5.6/8 and /9 with respect to the signed ptrdiff_t.  */
8126   if (POINTER_TYPE_P (TREE_TYPE (arg0))
8127       && !flag_wrapv && !flag_trapv)
8128     {
8129       tree base0, offset0, base1, offset1;
8130
8131       if (extract_array_ref (arg0, &base0, &offset0)
8132           && extract_array_ref (arg1, &base1, &offset1)
8133           && operand_equal_p (base0, base1, 0))
8134         {
8135           tree signed_size_type_node;
8136           signed_size_type_node = signed_type_for (size_type_node);
8137
8138           /* By converting to signed size type we cover middle-end pointer
8139              arithmetic which operates on unsigned pointer types of size
8140              type size and ARRAY_REF offsets which are properly sign or
8141              zero extended from their type in case it is narrower than
8142              size type.  */
8143           if (offset0 == NULL_TREE)
8144             offset0 = build_int_cst (signed_size_type_node, 0);
8145           else
8146             offset0 = fold_convert (signed_size_type_node, offset0);
8147           if (offset1 == NULL_TREE)
8148             offset1 = build_int_cst (signed_size_type_node, 0);
8149           else
8150             offset1 = fold_convert (signed_size_type_node, offset1);
8151
8152           return fold_build2 (code, type, offset0, offset1);
8153         }
8154     }
8155
8156   /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8157      X CMP Y +- C2 +- C1 for signed X, Y.  This is valid if
8158      the resulting offset is smaller in absolute value than the
8159      original one.  */
8160   if (!(flag_wrapv || flag_trapv)
8161       && !TYPE_UNSIGNED (TREE_TYPE (arg0))
8162       && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8163       && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8164           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8165       && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8166       && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8167           && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8168     {
8169       tree const1 = TREE_OPERAND (arg0, 1);
8170       tree const2 = TREE_OPERAND (arg1, 1);
8171       tree variable1 = TREE_OPERAND (arg0, 0);
8172       tree variable2 = TREE_OPERAND (arg1, 0);
8173       tree cst;
8174
8175       /* Put the constant on the side where it doesn't overflow and is
8176          of lower absolute value than before.  */
8177       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8178                              ? MINUS_EXPR : PLUS_EXPR,
8179                              const2, const1, 0);
8180       if (!TREE_OVERFLOW (cst)
8181           && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8182         return fold_build2 (code, type,
8183                             variable1,
8184                             fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8185                                          variable2, cst));
8186
8187       cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8188                              ? MINUS_EXPR : PLUS_EXPR,
8189                              const1, const2, 0);
8190       if (!TREE_OVERFLOW (cst)
8191           && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8192         return fold_build2 (code, type,
8193                             fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8194                                          variable1, cst),
8195                             variable2);
8196     }
8197
8198   tem = maybe_canonicalize_comparison (code, type, arg0, arg1);
8199   if (tem)
8200     return tem;
8201
8202   if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8203     {
8204       tree targ0 = strip_float_extensions (arg0);
8205       tree targ1 = strip_float_extensions (arg1);
8206       tree newtype = TREE_TYPE (targ0);
8207
8208       if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8209         newtype = TREE_TYPE (targ1);
8210
8211       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
8212       if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8213         return fold_build2 (code, type, fold_convert (newtype, targ0),
8214                             fold_convert (newtype, targ1));
8215
8216       /* (-a) CMP (-b) -> b CMP a  */
8217       if (TREE_CODE (arg0) == NEGATE_EXPR
8218           && TREE_CODE (arg1) == NEGATE_EXPR)
8219         return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8220                             TREE_OPERAND (arg0, 0));
8221
8222       if (TREE_CODE (arg1) == REAL_CST)
8223         {
8224           REAL_VALUE_TYPE cst;
8225           cst = TREE_REAL_CST (arg1);
8226
8227           /* (-a) CMP CST -> a swap(CMP) (-CST)  */
8228           if (TREE_CODE (arg0) == NEGATE_EXPR)
8229             return fold_build2 (swap_tree_comparison (code), type,
8230                                 TREE_OPERAND (arg0, 0),
8231                                 build_real (TREE_TYPE (arg1),
8232                                             REAL_VALUE_NEGATE (cst)));
8233
8234           /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
8235           /* a CMP (-0) -> a CMP 0  */
8236           if (REAL_VALUE_MINUS_ZERO (cst))
8237             return fold_build2 (code, type, arg0,
8238                                 build_real (TREE_TYPE (arg1), dconst0));
8239
8240           /* x != NaN is always true, other ops are always false.  */
8241           if (REAL_VALUE_ISNAN (cst)
8242               && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8243             {
8244               tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8245               return omit_one_operand (type, tem, arg0);
8246             }
8247
8248           /* Fold comparisons against infinity.  */
8249           if (REAL_VALUE_ISINF (cst))
8250             {
8251               tem = fold_inf_compare (code, type, arg0, arg1);
8252               if (tem != NULL_TREE)
8253                 return tem;
8254             }
8255         }
8256
8257       /* If this is a comparison of a real constant with a PLUS_EXPR
8258          or a MINUS_EXPR of a real constant, we can convert it into a
8259          comparison with a revised real constant as long as no overflow
8260          occurs when unsafe_math_optimizations are enabled.  */
8261       if (flag_unsafe_math_optimizations
8262           && TREE_CODE (arg1) == REAL_CST
8263           && (TREE_CODE (arg0) == PLUS_EXPR
8264               || TREE_CODE (arg0) == MINUS_EXPR)
8265           && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8266           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8267                                       ? MINUS_EXPR : PLUS_EXPR,
8268                                       arg1, TREE_OPERAND (arg0, 1), 0))
8269           && ! TREE_CONSTANT_OVERFLOW (tem))
8270         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8271
8272       /* Likewise, we can simplify a comparison of a real constant with
8273          a MINUS_EXPR whose first operand is also a real constant, i.e.
8274          (c1 - x) < c2 becomes x > c1-c2.  */
8275       if (flag_unsafe_math_optimizations
8276           && TREE_CODE (arg1) == REAL_CST
8277           && TREE_CODE (arg0) == MINUS_EXPR
8278           && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8279           && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8280                                       arg1, 0))
8281           && ! TREE_CONSTANT_OVERFLOW (tem))
8282         return fold_build2 (swap_tree_comparison (code), type,
8283                             TREE_OPERAND (arg0, 1), tem);
8284
8285       /* Fold comparisons against built-in math functions.  */
8286       if (TREE_CODE (arg1) == REAL_CST
8287           && flag_unsafe_math_optimizations
8288           && ! flag_errno_math)
8289         {
8290           enum built_in_function fcode = builtin_mathfn_code (arg0);
8291
8292           if (fcode != END_BUILTINS)
8293             {
8294               tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8295               if (tem != NULL_TREE)
8296                 return tem;
8297             }
8298         }
8299     }
8300
8301   /* Convert foo++ == CONST into ++foo == CONST + INCR.  */
8302   if (TREE_CONSTANT (arg1)
8303       && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
8304           || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
8305       /* This optimization is invalid for ordered comparisons
8306          if CONST+INCR overflows or if foo+incr might overflow.
8307          This optimization is invalid for floating point due to rounding.
8308          For pointer types we assume overflow doesn't happen.  */
8309       && (POINTER_TYPE_P (TREE_TYPE (arg0))
8310           || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8311               && (code == EQ_EXPR || code == NE_EXPR))))
8312     {
8313       tree varop, newconst;
8314
8315       if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
8316         {
8317           newconst = fold_build2 (PLUS_EXPR, TREE_TYPE (arg0),
8318                                   arg1, TREE_OPERAND (arg0, 1));
8319           varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
8320                           TREE_OPERAND (arg0, 0),
8321                           TREE_OPERAND (arg0, 1));
8322         }
8323       else
8324         {
8325           newconst = fold_build2 (MINUS_EXPR, TREE_TYPE (arg0),
8326                                   arg1, TREE_OPERAND (arg0, 1));
8327           varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
8328                           TREE_OPERAND (arg0, 0),
8329                           TREE_OPERAND (arg0, 1));
8330         }
8331
8332
8333       /* If VAROP is a reference to a bitfield, we must mask
8334          the constant by the width of the field.  */
8335       if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
8336           && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
8337           && host_integerp (DECL_SIZE (TREE_OPERAND
8338                                          (TREE_OPERAND (varop, 0), 1)), 1))
8339         {
8340           tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
8341           HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
8342           tree folded_compare, shift;
8343
8344           /* First check whether the comparison would come out
8345              always the same.  If we don't do that we would
8346              change the meaning with the masking.  */
8347           folded_compare = fold_build2 (code, type,
8348                                         TREE_OPERAND (varop, 0), arg1);
8349           if (TREE_CODE (folded_compare) == INTEGER_CST)
8350             return omit_one_operand (type, folded_compare, varop);
8351
8352           shift = build_int_cst (NULL_TREE,
8353                                  TYPE_PRECISION (TREE_TYPE (varop)) - size);
8354           shift = fold_convert (TREE_TYPE (varop), shift);
8355           newconst = fold_build2 (LSHIFT_EXPR, TREE_TYPE (varop),
8356                                   newconst, shift);
8357           newconst = fold_build2 (RSHIFT_EXPR, TREE_TYPE (varop),
8358                                   newconst, shift);
8359         }
8360
8361       return fold_build2 (code, type, varop, newconst);
8362     }
8363
8364   if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8365       && (TREE_CODE (arg0) == NOP_EXPR
8366           || TREE_CODE (arg0) == CONVERT_EXPR))
8367     {
8368       /* If we are widening one operand of an integer comparison,
8369          see if the other operand is similarly being widened.  Perhaps we
8370          can do the comparison in the narrower type.  */
8371       tem = fold_widened_comparison (code, type, arg0, arg1);
8372       if (tem)
8373         return tem;
8374
8375       /* Or if we are changing signedness.  */
8376       tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8377       if (tem)
8378         return tem;
8379     }
8380
8381   /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8382      constant, we can simplify it.  */
8383   if (TREE_CODE (arg1) == INTEGER_CST
8384       && (TREE_CODE (arg0) == MIN_EXPR
8385           || TREE_CODE (arg0) == MAX_EXPR)
8386       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8387     {
8388       tem = optimize_minmax_comparison (code, type, op0, op1);
8389       if (tem)
8390         return tem;
8391     }
8392
8393   /* Simplify comparison of something with itself.  (For IEEE
8394      floating-point, we can only do some of these simplifications.)  */
8395   if (operand_equal_p (arg0, arg1, 0))
8396     {
8397       switch (code)
8398         {
8399         case EQ_EXPR:
8400           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8401               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8402             return constant_boolean_node (1, type);
8403           break;
8404
8405         case GE_EXPR:
8406         case LE_EXPR:
8407           if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8408               || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8409             return constant_boolean_node (1, type);
8410           return fold_build2 (EQ_EXPR, type, arg0, arg1);
8411
8412         case NE_EXPR:
8413           /* For NE, we can only do this simplification if integer
8414              or we don't honor IEEE floating point NaNs.  */
8415           if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8416               && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8417             break;
8418           /* ... fall through ...  */
8419         case GT_EXPR:
8420         case LT_EXPR:
8421           return constant_boolean_node (0, type);
8422         default:
8423           gcc_unreachable ();
8424         }
8425     }
8426
8427   /* If we are comparing an expression that just has comparisons
8428      of two integer values, arithmetic expressions of those comparisons,
8429      and constants, we can simplify it.  There are only three cases
8430      to check: the two values can either be equal, the first can be
8431      greater, or the second can be greater.  Fold the expression for
8432      those three values.  Since each value must be 0 or 1, we have
8433      eight possibilities, each of which corresponds to the constant 0
8434      or 1 or one of the six possible comparisons.
8435
8436      This handles common cases like (a > b) == 0 but also handles
8437      expressions like  ((x > y) - (y > x)) > 0, which supposedly
8438      occur in macroized code.  */
8439
8440   if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8441     {
8442       tree cval1 = 0, cval2 = 0;
8443       int save_p = 0;
8444
8445       if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8446           /* Don't handle degenerate cases here; they should already
8447              have been handled anyway.  */
8448           && cval1 != 0 && cval2 != 0
8449           && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8450           && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8451           && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8452           && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8453           && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8454           && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8455                                 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8456         {
8457           tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8458           tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8459
8460           /* We can't just pass T to eval_subst in case cval1 or cval2
8461              was the same as ARG1.  */
8462
8463           tree high_result
8464                 = fold_build2 (code, type,
8465                                eval_subst (arg0, cval1, maxval,
8466                                            cval2, minval),
8467                                arg1);
8468           tree equal_result
8469                 = fold_build2 (code, type,
8470                                eval_subst (arg0, cval1, maxval,
8471                                            cval2, maxval),
8472                                arg1);
8473           tree low_result
8474                 = fold_build2 (code, type,
8475                                eval_subst (arg0, cval1, minval,
8476                                            cval2, maxval),
8477                                arg1);
8478
8479           /* All three of these results should be 0 or 1.  Confirm they are.
8480              Then use those values to select the proper code to use.  */
8481
8482           if (TREE_CODE (high_result) == INTEGER_CST
8483               && TREE_CODE (equal_result) == INTEGER_CST
8484               && TREE_CODE (low_result) == INTEGER_CST)
8485             {
8486               /* Make a 3-bit mask with the high-order bit being the
8487                  value for `>', the next for '=', and the low for '<'.  */
8488               switch ((integer_onep (high_result) * 4)
8489                       + (integer_onep (equal_result) * 2)
8490                       + integer_onep (low_result))
8491                 {
8492                 case 0:
8493                   /* Always false.  */
8494                   return omit_one_operand (type, integer_zero_node, arg0);
8495                 case 1:
8496                   code = LT_EXPR;
8497                   break;
8498                 case 2:
8499                   code = EQ_EXPR;
8500                   break;
8501                 case 3:
8502                   code = LE_EXPR;
8503                   break;
8504                 case 4:
8505                   code = GT_EXPR;
8506                   break;
8507                 case 5:
8508                   code = NE_EXPR;
8509                   break;
8510                 case 6:
8511                   code = GE_EXPR;
8512                   break;
8513                 case 7:
8514                   /* Always true.  */
8515                   return omit_one_operand (type, integer_one_node, arg0);
8516                 }
8517
8518               if (save_p)
8519                 return save_expr (build2 (code, type, cval1, cval2));
8520               return fold_build2 (code, type, cval1, cval2);
8521             }
8522         }
8523     }
8524
8525   /* Fold a comparison of the address of COMPONENT_REFs with the same
8526      type and component to a comparison of the address of the base
8527      object.  In short, &x->a OP &y->a to x OP y and
8528      &x->a OP &y.a to x OP &y  */
8529   if (TREE_CODE (arg0) == ADDR_EXPR
8530       && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
8531       && TREE_CODE (arg1) == ADDR_EXPR
8532       && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
8533     {
8534       tree cref0 = TREE_OPERAND (arg0, 0);
8535       tree cref1 = TREE_OPERAND (arg1, 0);
8536       if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
8537         {
8538           tree op0 = TREE_OPERAND (cref0, 0);
8539           tree op1 = TREE_OPERAND (cref1, 0);
8540           return fold_build2 (code, type,
8541                               build_fold_addr_expr (op0),
8542                               build_fold_addr_expr (op1));
8543         }
8544     }
8545
8546   /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8547      into a single range test.  */
8548   if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8549        || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8550       && TREE_CODE (arg1) == INTEGER_CST
8551       && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8552       && !integer_zerop (TREE_OPERAND (arg0, 1))
8553       && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8554       && !TREE_OVERFLOW (arg1))
8555     {
8556       tem = fold_div_compare (code, type, arg0, arg1);
8557       if (tem != NULL_TREE)
8558         return tem;
8559     }
8560
8561   /* Fold ~X op ~Y as Y op X.  */
8562   if (TREE_CODE (arg0) == BIT_NOT_EXPR
8563       && TREE_CODE (arg1) == BIT_NOT_EXPR)
8564     return fold_build2 (code, type,
8565                         TREE_OPERAND (arg1, 0),
8566                         TREE_OPERAND (arg0, 0));
8567
8568   /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
8569   if (TREE_CODE (arg0) == BIT_NOT_EXPR
8570       && TREE_CODE (arg1) == INTEGER_CST)
8571     return fold_build2 (swap_tree_comparison (code), type,
8572                         TREE_OPERAND (arg0, 0),
8573                         fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1));
8574
8575   return NULL_TREE;
8576 }
8577
8578
8579 /* Subroutine of fold_binary.  Optimize complex multiplications of the
8580    form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2).  The
8581    argument EXPR represents the expression "z" of type TYPE.  */
8582
8583 static tree
8584 fold_mult_zconjz (tree type, tree expr)
8585 {
8586   tree itype = TREE_TYPE (type);
8587   tree rpart, ipart, tem;
8588
8589   if (TREE_CODE (expr) == COMPLEX_EXPR)
8590     {
8591       rpart = TREE_OPERAND (expr, 0);
8592       ipart = TREE_OPERAND (expr, 1);
8593     }
8594   else if (TREE_CODE (expr) == COMPLEX_CST)
8595     {
8596       rpart = TREE_REALPART (expr);
8597       ipart = TREE_IMAGPART (expr);
8598     }
8599   else
8600     {
8601       expr = save_expr (expr);
8602       rpart = fold_build1 (REALPART_EXPR, itype, expr);
8603       ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
8604     }
8605
8606   rpart = save_expr (rpart);
8607   ipart = save_expr (ipart);
8608   tem = fold_build2 (PLUS_EXPR, itype,
8609                      fold_build2 (MULT_EXPR, itype, rpart, rpart),
8610                      fold_build2 (MULT_EXPR, itype, ipart, ipart));
8611   return fold_build2 (COMPLEX_EXPR, type, tem,
8612                       fold_convert (itype, integer_zero_node));
8613 }
8614
8615
8616 /* Fold a binary expression of code CODE and type TYPE with operands
8617    OP0 and OP1.  Return the folded expression if folding is
8618    successful.  Otherwise, return NULL_TREE.  */
8619
8620 tree
8621 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
8622 {
8623   enum tree_code_class kind = TREE_CODE_CLASS (code);
8624   tree arg0, arg1, tem;
8625   tree t1 = NULL_TREE;
8626
8627   gcc_assert ((IS_EXPR_CODE_CLASS (kind)
8628                || IS_GIMPLE_STMT_CODE_CLASS (kind))
8629               && TREE_CODE_LENGTH (code) == 2
8630               && op0 != NULL_TREE
8631               && op1 != NULL_TREE);
8632
8633   arg0 = op0;
8634   arg1 = op1;
8635
8636   /* Strip any conversions that don't change the mode.  This is
8637      safe for every expression, except for a comparison expression
8638      because its signedness is derived from its operands.  So, in
8639      the latter case, only strip conversions that don't change the
8640      signedness.
8641
8642      Note that this is done as an internal manipulation within the
8643      constant folder, in order to find the simplest representation
8644      of the arguments so that their form can be studied.  In any
8645      cases, the appropriate type conversions should be put back in
8646      the tree that will get out of the constant folder.  */
8647
8648   if (kind == tcc_comparison)
8649     {
8650       STRIP_SIGN_NOPS (arg0);
8651       STRIP_SIGN_NOPS (arg1);
8652     }
8653   else
8654     {
8655       STRIP_NOPS (arg0);
8656       STRIP_NOPS (arg1);
8657     }
8658
8659   /* Note that TREE_CONSTANT isn't enough: static var addresses are
8660      constant but we can't do arithmetic on them.  */
8661   if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
8662       || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
8663       || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
8664       || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
8665     {
8666       if (kind == tcc_binary)
8667         tem = const_binop (code, arg0, arg1, 0);
8668       else if (kind == tcc_comparison)
8669         tem = fold_relational_const (code, type, arg0, arg1);
8670       else
8671         tem = NULL_TREE;
8672
8673       if (tem != NULL_TREE)
8674         {
8675           if (TREE_TYPE (tem) != type)
8676             tem = fold_convert (type, tem);
8677           return tem;
8678         }
8679     }
8680
8681   /* If this is a commutative operation, and ARG0 is a constant, move it
8682      to ARG1 to reduce the number of tests below.  */
8683   if (commutative_tree_code (code)
8684       && tree_swap_operands_p (arg0, arg1, true))
8685     return fold_build2 (code, type, op1, op0);
8686
8687   /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
8688
8689      First check for cases where an arithmetic operation is applied to a
8690      compound, conditional, or comparison operation.  Push the arithmetic
8691      operation inside the compound or conditional to see if any folding
8692      can then be done.  Convert comparison to conditional for this purpose.
8693      The also optimizes non-constant cases that used to be done in
8694      expand_expr.
8695
8696      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
8697      one of the operands is a comparison and the other is a comparison, a
8698      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
8699      code below would make the expression more complex.  Change it to a
8700      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
8701      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
8702
8703   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
8704        || code == EQ_EXPR || code == NE_EXPR)
8705       && ((truth_value_p (TREE_CODE (arg0))
8706            && (truth_value_p (TREE_CODE (arg1))
8707                || (TREE_CODE (arg1) == BIT_AND_EXPR
8708                    && integer_onep (TREE_OPERAND (arg1, 1)))))
8709           || (truth_value_p (TREE_CODE (arg1))
8710               && (truth_value_p (TREE_CODE (arg0))
8711                   || (TREE_CODE (arg0) == BIT_AND_EXPR
8712                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
8713     {
8714       tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
8715                          : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
8716                          : TRUTH_XOR_EXPR,
8717                          boolean_type_node,
8718                          fold_convert (boolean_type_node, arg0),
8719                          fold_convert (boolean_type_node, arg1));
8720
8721       if (code == EQ_EXPR)
8722         tem = invert_truthvalue (tem);
8723
8724       return fold_convert (type, tem);
8725     }
8726
8727   if (TREE_CODE_CLASS (code) == tcc_binary
8728       || TREE_CODE_CLASS (code) == tcc_comparison)
8729     {
8730       if (TREE_CODE (arg0) == COMPOUND_EXPR)
8731         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8732                        fold_build2 (code, type,
8733                                     TREE_OPERAND (arg0, 1), op1));
8734       if (TREE_CODE (arg1) == COMPOUND_EXPR
8735           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
8736         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
8737                        fold_build2 (code, type,
8738                                     op0, TREE_OPERAND (arg1, 1)));
8739
8740       if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
8741         {
8742           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
8743                                                      arg0, arg1, 
8744                                                      /*cond_first_p=*/1);
8745           if (tem != NULL_TREE)
8746             return tem;
8747         }
8748
8749       if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
8750         {
8751           tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
8752                                                      arg1, arg0, 
8753                                                      /*cond_first_p=*/0);
8754           if (tem != NULL_TREE)
8755             return tem;
8756         }
8757     }
8758
8759   switch (code)
8760     {
8761     case PLUS_EXPR:
8762       /* A + (-B) -> A - B */
8763       if (TREE_CODE (arg1) == NEGATE_EXPR)
8764         return fold_build2 (MINUS_EXPR, type,
8765                             fold_convert (type, arg0),
8766                             fold_convert (type, TREE_OPERAND (arg1, 0)));
8767       /* (-A) + B -> B - A */
8768       if (TREE_CODE (arg0) == NEGATE_EXPR
8769           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
8770         return fold_build2 (MINUS_EXPR, type,
8771                             fold_convert (type, arg1),
8772                             fold_convert (type, TREE_OPERAND (arg0, 0)));
8773       /* Convert ~A + 1 to -A.  */
8774       if (INTEGRAL_TYPE_P (type)
8775           && TREE_CODE (arg0) == BIT_NOT_EXPR
8776           && integer_onep (arg1))
8777         return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
8778
8779       /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
8780          same or one.  */
8781       if ((TREE_CODE (arg0) == MULT_EXPR
8782            || TREE_CODE (arg1) == MULT_EXPR)
8783           && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
8784         {
8785           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
8786           if (tem)
8787             return tem;
8788         }
8789
8790       if (! FLOAT_TYPE_P (type))
8791         {
8792           if (integer_zerop (arg1))
8793             return non_lvalue (fold_convert (type, arg0));
8794
8795           /* ~X + X is -1.  */
8796           if (TREE_CODE (arg0) == BIT_NOT_EXPR
8797               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
8798               && !TYPE_TRAP_SIGNED (type))
8799             {
8800               t1 = build_int_cst_type (type, -1);
8801               return omit_one_operand (type, t1, arg1);
8802             }
8803
8804           /* X + ~X is -1.  */
8805           if (TREE_CODE (arg1) == BIT_NOT_EXPR
8806               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
8807               && !TYPE_TRAP_SIGNED (type))
8808             {
8809               t1 = build_int_cst_type (type, -1);
8810               return omit_one_operand (type, t1, arg0);
8811           }
8812
8813           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
8814              with a constant, and the two constants have no bits in common,
8815              we should treat this as a BIT_IOR_EXPR since this may produce more
8816              simplifications.  */
8817           if (TREE_CODE (arg0) == BIT_AND_EXPR
8818               && TREE_CODE (arg1) == BIT_AND_EXPR
8819               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8820               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8821               && integer_zerop (const_binop (BIT_AND_EXPR,
8822                                              TREE_OPERAND (arg0, 1),
8823                                              TREE_OPERAND (arg1, 1), 0)))
8824             {
8825               code = BIT_IOR_EXPR;
8826               goto bit_ior;
8827             }
8828
8829           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
8830              (plus (plus (mult) (mult)) (foo)) so that we can
8831              take advantage of the factoring cases below.  */
8832           if (((TREE_CODE (arg0) == PLUS_EXPR
8833                 || TREE_CODE (arg0) == MINUS_EXPR)
8834                && TREE_CODE (arg1) == MULT_EXPR)
8835               || ((TREE_CODE (arg1) == PLUS_EXPR
8836                    || TREE_CODE (arg1) == MINUS_EXPR)
8837                   && TREE_CODE (arg0) == MULT_EXPR))
8838             {
8839               tree parg0, parg1, parg, marg;
8840               enum tree_code pcode;
8841
8842               if (TREE_CODE (arg1) == MULT_EXPR)
8843                 parg = arg0, marg = arg1;
8844               else
8845                 parg = arg1, marg = arg0;
8846               pcode = TREE_CODE (parg);
8847               parg0 = TREE_OPERAND (parg, 0);
8848               parg1 = TREE_OPERAND (parg, 1);
8849               STRIP_NOPS (parg0);
8850               STRIP_NOPS (parg1);
8851
8852               if (TREE_CODE (parg0) == MULT_EXPR
8853                   && TREE_CODE (parg1) != MULT_EXPR)
8854                 return fold_build2 (pcode, type,
8855                                     fold_build2 (PLUS_EXPR, type,
8856                                                  fold_convert (type, parg0),
8857                                                  fold_convert (type, marg)),
8858                                     fold_convert (type, parg1));
8859               if (TREE_CODE (parg0) != MULT_EXPR
8860                   && TREE_CODE (parg1) == MULT_EXPR)
8861                 return fold_build2 (PLUS_EXPR, type,
8862                                     fold_convert (type, parg0),
8863                                     fold_build2 (pcode, type,
8864                                                  fold_convert (type, marg),
8865                                                  fold_convert (type,
8866                                                                parg1)));
8867             }
8868
8869           /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
8870              of the array.  Loop optimizer sometimes produce this type of
8871              expressions.  */
8872           if (TREE_CODE (arg0) == ADDR_EXPR)
8873             {
8874               tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
8875               if (tem)
8876                 return fold_convert (type, tem);
8877             }
8878           else if (TREE_CODE (arg1) == ADDR_EXPR)
8879             {
8880               tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
8881               if (tem)
8882                 return fold_convert (type, tem);
8883             }
8884         }
8885       else
8886         {
8887           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
8888           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
8889             return non_lvalue (fold_convert (type, arg0));
8890
8891           /* Likewise if the operands are reversed.  */
8892           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
8893             return non_lvalue (fold_convert (type, arg1));
8894
8895           /* Convert X + -C into X - C.  */
8896           if (TREE_CODE (arg1) == REAL_CST
8897               && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
8898             {
8899               tem = fold_negate_const (arg1, type);
8900               if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
8901                 return fold_build2 (MINUS_EXPR, type,
8902                                     fold_convert (type, arg0),
8903                                     fold_convert (type, tem));
8904             }
8905
8906           /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
8907              to __complex__ ( x, y ).  This is not the same for SNaNs or
8908              if singed zeros are involved.  */
8909           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8910               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
8911               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
8912             {
8913               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
8914               tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
8915               tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
8916               bool arg0rz = false, arg0iz = false;
8917               if ((arg0r && (arg0rz = real_zerop (arg0r)))
8918                   || (arg0i && (arg0iz = real_zerop (arg0i))))
8919                 {
8920                   tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
8921                   tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
8922                   if (arg0rz && arg1i && real_zerop (arg1i))
8923                     {
8924                       tree rp = arg1r ? arg1r
8925                                   : build1 (REALPART_EXPR, rtype, arg1);
8926                       tree ip = arg0i ? arg0i
8927                                   : build1 (IMAGPART_EXPR, rtype, arg0);
8928                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
8929                     }
8930                   else if (arg0iz && arg1r && real_zerop (arg1r))
8931                     {
8932                       tree rp = arg0r ? arg0r
8933                                   : build1 (REALPART_EXPR, rtype, arg0);
8934                       tree ip = arg1i ? arg1i
8935                                   : build1 (IMAGPART_EXPR, rtype, arg1);
8936                       return fold_build2 (COMPLEX_EXPR, type, rp, ip);
8937                     }
8938                 }
8939             }
8940
8941           if (flag_unsafe_math_optimizations
8942               && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
8943               && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
8944               && (tem = distribute_real_division (code, type, arg0, arg1)))
8945             return tem;
8946
8947           /* Convert x+x into x*2.0.  */
8948           if (operand_equal_p (arg0, arg1, 0)
8949               && SCALAR_FLOAT_TYPE_P (type))
8950             return fold_build2 (MULT_EXPR, type, arg0,
8951                                 build_real (type, dconst2));
8952
8953           /* Convert a + (b*c + d*e) into (a + b*c) + d*e.  */
8954           if (flag_unsafe_math_optimizations
8955               && TREE_CODE (arg1) == PLUS_EXPR
8956               && TREE_CODE (arg0) != MULT_EXPR)
8957             {
8958               tree tree10 = TREE_OPERAND (arg1, 0);
8959               tree tree11 = TREE_OPERAND (arg1, 1);
8960               if (TREE_CODE (tree11) == MULT_EXPR
8961                   && TREE_CODE (tree10) == MULT_EXPR)
8962                 {
8963                   tree tree0;
8964                   tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
8965                   return fold_build2 (PLUS_EXPR, type, tree0, tree11);
8966                 }
8967             }
8968           /* Convert (b*c + d*e) + a into b*c + (d*e +a).  */
8969           if (flag_unsafe_math_optimizations
8970               && TREE_CODE (arg0) == PLUS_EXPR
8971               && TREE_CODE (arg1) != MULT_EXPR)
8972             {
8973               tree tree00 = TREE_OPERAND (arg0, 0);
8974               tree tree01 = TREE_OPERAND (arg0, 1);
8975               if (TREE_CODE (tree01) == MULT_EXPR
8976                   && TREE_CODE (tree00) == MULT_EXPR)
8977                 {
8978                   tree tree0;
8979                   tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
8980                   return fold_build2 (PLUS_EXPR, type, tree00, tree0);
8981                 }
8982             }
8983         }
8984
8985      bit_rotate:
8986       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
8987          is a rotate of A by C1 bits.  */
8988       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
8989          is a rotate of A by B bits.  */
8990       {
8991         enum tree_code code0, code1;
8992         code0 = TREE_CODE (arg0);
8993         code1 = TREE_CODE (arg1);
8994         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
8995              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
8996             && operand_equal_p (TREE_OPERAND (arg0, 0),
8997                                 TREE_OPERAND (arg1, 0), 0)
8998             && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8999           {
9000             tree tree01, tree11;
9001             enum tree_code code01, code11;
9002
9003             tree01 = TREE_OPERAND (arg0, 1);
9004             tree11 = TREE_OPERAND (arg1, 1);
9005             STRIP_NOPS (tree01);
9006             STRIP_NOPS (tree11);
9007             code01 = TREE_CODE (tree01);
9008             code11 = TREE_CODE (tree11);
9009             if (code01 == INTEGER_CST
9010                 && code11 == INTEGER_CST
9011                 && TREE_INT_CST_HIGH (tree01) == 0
9012                 && TREE_INT_CST_HIGH (tree11) == 0
9013                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9014                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9015               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9016                              code0 == LSHIFT_EXPR ? tree01 : tree11);
9017             else if (code11 == MINUS_EXPR)
9018               {
9019                 tree tree110, tree111;
9020                 tree110 = TREE_OPERAND (tree11, 0);
9021                 tree111 = TREE_OPERAND (tree11, 1);
9022                 STRIP_NOPS (tree110);
9023                 STRIP_NOPS (tree111);
9024                 if (TREE_CODE (tree110) == INTEGER_CST
9025                     && 0 == compare_tree_int (tree110,
9026                                               TYPE_PRECISION
9027                                               (TREE_TYPE (TREE_OPERAND
9028                                                           (arg0, 0))))
9029                     && operand_equal_p (tree01, tree111, 0))
9030                   return build2 ((code0 == LSHIFT_EXPR
9031                                   ? LROTATE_EXPR
9032                                   : RROTATE_EXPR),
9033                                  type, TREE_OPERAND (arg0, 0), tree01);
9034               }
9035             else if (code01 == MINUS_EXPR)
9036               {
9037                 tree tree010, tree011;
9038                 tree010 = TREE_OPERAND (tree01, 0);
9039                 tree011 = TREE_OPERAND (tree01, 1);
9040                 STRIP_NOPS (tree010);
9041                 STRIP_NOPS (tree011);
9042                 if (TREE_CODE (tree010) == INTEGER_CST
9043                     && 0 == compare_tree_int (tree010,
9044                                               TYPE_PRECISION
9045                                               (TREE_TYPE (TREE_OPERAND
9046                                                           (arg0, 0))))
9047                     && operand_equal_p (tree11, tree011, 0))
9048                   return build2 ((code0 != LSHIFT_EXPR
9049                                   ? LROTATE_EXPR
9050                                   : RROTATE_EXPR),
9051                                  type, TREE_OPERAND (arg0, 0), tree11);
9052               }
9053           }
9054       }
9055
9056     associate:
9057       /* In most languages, can't associate operations on floats through
9058          parentheses.  Rather than remember where the parentheses were, we
9059          don't associate floats at all, unless the user has specified
9060          -funsafe-math-optimizations.  */
9061
9062       if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
9063         {
9064           tree var0, con0, lit0, minus_lit0;
9065           tree var1, con1, lit1, minus_lit1;
9066
9067           /* Split both trees into variables, constants, and literals.  Then
9068              associate each group together, the constants with literals,
9069              then the result with variables.  This increases the chances of
9070              literals being recombined later and of generating relocatable
9071              expressions for the sum of a constant and literal.  */
9072           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9073           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9074                              code == MINUS_EXPR);
9075
9076           /* Only do something if we found more than two objects.  Otherwise,
9077              nothing has changed and we risk infinite recursion.  */
9078           if (2 < ((var0 != 0) + (var1 != 0)
9079                    + (con0 != 0) + (con1 != 0)
9080                    + (lit0 != 0) + (lit1 != 0)
9081                    + (minus_lit0 != 0) + (minus_lit1 != 0)))
9082             {
9083               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
9084               if (code == MINUS_EXPR)
9085                 code = PLUS_EXPR;
9086
9087               var0 = associate_trees (var0, var1, code, type);
9088               con0 = associate_trees (con0, con1, code, type);
9089               lit0 = associate_trees (lit0, lit1, code, type);
9090               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9091
9092               /* Preserve the MINUS_EXPR if the negative part of the literal is
9093                  greater than the positive part.  Otherwise, the multiplicative
9094                  folding code (i.e extract_muldiv) may be fooled in case
9095                  unsigned constants are subtracted, like in the following
9096                  example: ((X*2 + 4) - 8U)/2.  */
9097               if (minus_lit0 && lit0)
9098                 {
9099                   if (TREE_CODE (lit0) == INTEGER_CST
9100                       && TREE_CODE (minus_lit0) == INTEGER_CST
9101                       && tree_int_cst_lt (lit0, minus_lit0))
9102                     {
9103                       minus_lit0 = associate_trees (minus_lit0, lit0,
9104                                                     MINUS_EXPR, type);
9105                       lit0 = 0;
9106                     }
9107                   else
9108                     {
9109                       lit0 = associate_trees (lit0, minus_lit0,
9110                                               MINUS_EXPR, type);
9111                       minus_lit0 = 0;
9112                     }
9113                 }
9114               if (minus_lit0)
9115                 {
9116                   if (con0 == 0)
9117                     return fold_convert (type,
9118                                          associate_trees (var0, minus_lit0,
9119                                                           MINUS_EXPR, type));
9120                   else
9121                     {
9122                       con0 = associate_trees (con0, minus_lit0,
9123                                               MINUS_EXPR, type);
9124                       return fold_convert (type,
9125                                            associate_trees (var0, con0,
9126                                                             PLUS_EXPR, type));
9127                     }
9128                 }
9129
9130               con0 = associate_trees (con0, lit0, code, type);
9131               return fold_convert (type, associate_trees (var0, con0,
9132                                                           code, type));
9133             }
9134         }
9135
9136       return NULL_TREE;
9137
9138     case MINUS_EXPR:
9139       /* A - (-B) -> A + B */
9140       if (TREE_CODE (arg1) == NEGATE_EXPR)
9141         return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
9142       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
9143       if (TREE_CODE (arg0) == NEGATE_EXPR
9144           && (FLOAT_TYPE_P (type)
9145               || INTEGRAL_TYPE_P (type))
9146           && negate_expr_p (arg1)
9147           && reorder_operands_p (arg0, arg1))
9148         return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
9149                             TREE_OPERAND (arg0, 0));
9150       /* Convert -A - 1 to ~A.  */
9151       if (INTEGRAL_TYPE_P (type)
9152           && TREE_CODE (arg0) == NEGATE_EXPR
9153           && integer_onep (arg1)
9154           && !TYPE_TRAP_SIGNED (type))
9155         return fold_build1 (BIT_NOT_EXPR, type,
9156                             fold_convert (type, TREE_OPERAND (arg0, 0)));
9157
9158       /* Convert -1 - A to ~A.  */
9159       if (INTEGRAL_TYPE_P (type)
9160           && integer_all_onesp (arg0))
9161         return fold_build1 (BIT_NOT_EXPR, type, op1);
9162
9163       if (! FLOAT_TYPE_P (type))
9164         {
9165           if (integer_zerop (arg0))
9166             return negate_expr (fold_convert (type, arg1));
9167           if (integer_zerop (arg1))
9168             return non_lvalue (fold_convert (type, arg0));
9169
9170           /* Fold A - (A & B) into ~B & A.  */
9171           if (!TREE_SIDE_EFFECTS (arg0)
9172               && TREE_CODE (arg1) == BIT_AND_EXPR)
9173             {
9174               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9175                 return fold_build2 (BIT_AND_EXPR, type,
9176                                     fold_build1 (BIT_NOT_EXPR, type,
9177                                                  TREE_OPERAND (arg1, 0)),
9178                                     arg0);
9179               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9180                 return fold_build2 (BIT_AND_EXPR, type,
9181                                     fold_build1 (BIT_NOT_EXPR, type,
9182                                                  TREE_OPERAND (arg1, 1)),
9183                                     arg0);
9184             }
9185
9186           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9187              any power of 2 minus 1.  */
9188           if (TREE_CODE (arg0) == BIT_AND_EXPR
9189               && TREE_CODE (arg1) == BIT_AND_EXPR
9190               && operand_equal_p (TREE_OPERAND (arg0, 0),
9191                                   TREE_OPERAND (arg1, 0), 0))
9192             {
9193               tree mask0 = TREE_OPERAND (arg0, 1);
9194               tree mask1 = TREE_OPERAND (arg1, 1);
9195               tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
9196
9197               if (operand_equal_p (tem, mask1, 0))
9198                 {
9199                   tem = fold_build2 (BIT_XOR_EXPR, type,
9200                                      TREE_OPERAND (arg0, 0), mask1);
9201                   return fold_build2 (MINUS_EXPR, type, tem, mask1);
9202                 }
9203             }
9204         }
9205
9206       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
9207       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
9208         return non_lvalue (fold_convert (type, arg0));
9209
9210       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
9211          ARG0 is zero and X + ARG0 reduces to X, since that would mean
9212          (-ARG1 + ARG0) reduces to -ARG1.  */
9213       else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9214         return negate_expr (fold_convert (type, arg1));
9215
9216       /* Fold &x - &x.  This can happen from &x.foo - &x.
9217          This is unsafe for certain floats even in non-IEEE formats.
9218          In IEEE, it is unsafe because it does wrong for NaNs.
9219          Also note that operand_equal_p is always false if an operand
9220          is volatile.  */
9221
9222       if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
9223           && operand_equal_p (arg0, arg1, 0))
9224         return fold_convert (type, integer_zero_node);
9225
9226       /* A - B -> A + (-B) if B is easily negatable.  */
9227       if (negate_expr_p (arg1)
9228           && ((FLOAT_TYPE_P (type)
9229                /* Avoid this transformation if B is a positive REAL_CST.  */
9230                && (TREE_CODE (arg1) != REAL_CST
9231                    ||  REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
9232               || INTEGRAL_TYPE_P (type)))
9233         return fold_build2 (PLUS_EXPR, type,
9234                             fold_convert (type, arg0),
9235                             fold_convert (type, negate_expr (arg1)));
9236
9237       /* Try folding difference of addresses.  */
9238       {
9239         HOST_WIDE_INT diff;
9240
9241         if ((TREE_CODE (arg0) == ADDR_EXPR
9242              || TREE_CODE (arg1) == ADDR_EXPR)
9243             && ptr_difference_const (arg0, arg1, &diff))
9244           return build_int_cst_type (type, diff);
9245       }
9246
9247       /* Fold &a[i] - &a[j] to i-j.  */
9248       if (TREE_CODE (arg0) == ADDR_EXPR
9249           && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9250           && TREE_CODE (arg1) == ADDR_EXPR
9251           && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9252         {
9253           tree aref0 = TREE_OPERAND (arg0, 0);
9254           tree aref1 = TREE_OPERAND (arg1, 0);
9255           if (operand_equal_p (TREE_OPERAND (aref0, 0),
9256                                TREE_OPERAND (aref1, 0), 0))
9257             {
9258               tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
9259               tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
9260               tree esz = array_ref_element_size (aref0);
9261               tree diff = build2 (MINUS_EXPR, type, op0, op1);
9262               return fold_build2 (MULT_EXPR, type, diff,
9263                                   fold_convert (type, esz));
9264                                   
9265             }
9266         }
9267
9268       /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
9269          of the array.  Loop optimizer sometimes produce this type of
9270          expressions.  */
9271       if (TREE_CODE (arg0) == ADDR_EXPR)
9272         {
9273           tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
9274           if (tem)
9275             return fold_convert (type, tem);
9276         }
9277
9278       if (flag_unsafe_math_optimizations
9279           && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9280           && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9281           && (tem = distribute_real_division (code, type, arg0, arg1)))
9282         return tem;
9283
9284       /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
9285          same or one.  */
9286       if ((TREE_CODE (arg0) == MULT_EXPR
9287            || TREE_CODE (arg1) == MULT_EXPR)
9288           && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
9289         {
9290           tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9291           if (tem)
9292             return tem;
9293         }
9294
9295       goto associate;
9296
9297     case MULT_EXPR:
9298       /* (-A) * (-B) -> A * B  */
9299       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9300         return fold_build2 (MULT_EXPR, type,
9301                             fold_convert (type, TREE_OPERAND (arg0, 0)),
9302                             fold_convert (type, negate_expr (arg1)));
9303       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
9304         return fold_build2 (MULT_EXPR, type,
9305                             fold_convert (type, negate_expr (arg0)),
9306                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9307
9308       if (! FLOAT_TYPE_P (type))
9309         {
9310           if (integer_zerop (arg1))
9311             return omit_one_operand (type, arg1, arg0);
9312           if (integer_onep (arg1))
9313             return non_lvalue (fold_convert (type, arg0));
9314           /* Transform x * -1 into -x.  */
9315           if (integer_all_onesp (arg1))
9316             return fold_convert (type, negate_expr (arg0));
9317           /* Transform x * -C into -x * C if x is easily negatable.  */
9318           if (TREE_CODE (arg1) == INTEGER_CST
9319               && tree_int_cst_sgn (arg1) == -1
9320               && negate_expr_p (arg0)
9321               && (tem = negate_expr (arg1)) != arg1
9322               && !TREE_OVERFLOW (tem))
9323             return fold_build2 (MULT_EXPR, type,
9324                                 negate_expr (arg0), tem);
9325
9326           /* (a * (1 << b)) is (a << b)  */
9327           if (TREE_CODE (arg1) == LSHIFT_EXPR
9328               && integer_onep (TREE_OPERAND (arg1, 0)))
9329             return fold_build2 (LSHIFT_EXPR, type, arg0,
9330                                 TREE_OPERAND (arg1, 1));
9331           if (TREE_CODE (arg0) == LSHIFT_EXPR
9332               && integer_onep (TREE_OPERAND (arg0, 0)))
9333             return fold_build2 (LSHIFT_EXPR, type, arg1,
9334                                 TREE_OPERAND (arg0, 1));
9335
9336           if (TREE_CODE (arg1) == INTEGER_CST
9337               && 0 != (tem = extract_muldiv (op0,
9338                                              fold_convert (type, arg1),
9339                                              code, NULL_TREE)))
9340             return fold_convert (type, tem);
9341
9342           /* Optimize z * conj(z) for integer complex numbers.  */
9343           if (TREE_CODE (arg0) == CONJ_EXPR
9344               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9345             return fold_mult_zconjz (type, arg1);
9346           if (TREE_CODE (arg1) == CONJ_EXPR
9347               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9348             return fold_mult_zconjz (type, arg0);
9349         }
9350       else
9351         {
9352           /* Maybe fold x * 0 to 0.  The expressions aren't the same
9353              when x is NaN, since x * 0 is also NaN.  Nor are they the
9354              same in modes with signed zeros, since multiplying a
9355              negative value by 0 gives -0, not +0.  */
9356           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9357               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9358               && real_zerop (arg1))
9359             return omit_one_operand (type, arg1, arg0);
9360           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
9361           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9362               && real_onep (arg1))
9363             return non_lvalue (fold_convert (type, arg0));
9364
9365           /* Transform x * -1.0 into -x.  */
9366           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9367               && real_minus_onep (arg1))
9368             return fold_convert (type, negate_expr (arg0));
9369
9370           /* Convert (C1/X)*C2 into (C1*C2)/X.  */
9371           if (flag_unsafe_math_optimizations
9372               && TREE_CODE (arg0) == RDIV_EXPR
9373               && TREE_CODE (arg1) == REAL_CST
9374               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
9375             {
9376               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
9377                                       arg1, 0);
9378               if (tem)
9379                 return fold_build2 (RDIV_EXPR, type, tem,
9380                                     TREE_OPERAND (arg0, 1));
9381             }
9382
9383           /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y.  */
9384           if (operand_equal_p (arg0, arg1, 0))
9385             {
9386               tree tem = fold_strip_sign_ops (arg0);
9387               if (tem != NULL_TREE)
9388                 {
9389                   tem = fold_convert (type, tem);
9390                   return fold_build2 (MULT_EXPR, type, tem, tem);
9391                 }
9392             }
9393
9394           /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9395              This is not the same for NaNs or if singed zeros are
9396              involved.  */
9397           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9398               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9399               && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9400               && TREE_CODE (arg1) == COMPLEX_CST
9401               && real_zerop (TREE_REALPART (arg1)))
9402             {
9403               tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9404               if (real_onep (TREE_IMAGPART (arg1)))
9405                 return fold_build2 (COMPLEX_EXPR, type,
9406                                     negate_expr (fold_build1 (IMAGPART_EXPR,
9407                                                               rtype, arg0)),
9408                                     fold_build1 (REALPART_EXPR, rtype, arg0));
9409               else if (real_minus_onep (TREE_IMAGPART (arg1)))
9410                 return fold_build2 (COMPLEX_EXPR, type,
9411                                     fold_build1 (IMAGPART_EXPR, rtype, arg0),
9412                                     negate_expr (fold_build1 (REALPART_EXPR,
9413                                                               rtype, arg0)));
9414             }
9415
9416           /* Optimize z * conj(z) for floating point complex numbers.
9417              Guarded by flag_unsafe_math_optimizations as non-finite
9418              imaginary components don't produce scalar results.  */
9419           if (flag_unsafe_math_optimizations
9420               && TREE_CODE (arg0) == CONJ_EXPR
9421               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9422             return fold_mult_zconjz (type, arg1);
9423           if (flag_unsafe_math_optimizations
9424               && TREE_CODE (arg1) == CONJ_EXPR
9425               && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9426             return fold_mult_zconjz (type, arg0);
9427
9428           if (flag_unsafe_math_optimizations)
9429             {
9430               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
9431               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
9432
9433               /* Optimizations of root(...)*root(...).  */
9434               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
9435                 {
9436                   tree rootfn, arg, arglist;
9437                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9438                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9439
9440                   /* Optimize sqrt(x)*sqrt(x) as x.  */
9441                   if (BUILTIN_SQRT_P (fcode0)
9442                       && operand_equal_p (arg00, arg10, 0)
9443                       && ! HONOR_SNANS (TYPE_MODE (type)))
9444                     return arg00;
9445
9446                   /* Optimize root(x)*root(y) as root(x*y).  */
9447                   rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9448                   arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
9449                   arglist = build_tree_list (NULL_TREE, arg);
9450                   return build_function_call_expr (rootfn, arglist);
9451                 }
9452
9453               /* Optimize expN(x)*expN(y) as expN(x+y).  */
9454               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
9455                 {
9456                   tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9457                   tree arg = fold_build2 (PLUS_EXPR, type,
9458                                           TREE_VALUE (TREE_OPERAND (arg0, 1)),
9459                                           TREE_VALUE (TREE_OPERAND (arg1, 1)));
9460                   tree arglist = build_tree_list (NULL_TREE, arg);
9461                   return build_function_call_expr (expfn, arglist);
9462                 }
9463
9464               /* Optimizations of pow(...)*pow(...).  */
9465               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
9466                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
9467                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
9468                 {
9469                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9470                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
9471                                                                      1)));
9472                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9473                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
9474                                                                      1)));
9475
9476                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
9477                   if (operand_equal_p (arg01, arg11, 0))
9478                     {
9479                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9480                       tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
9481                       tree arglist = tree_cons (NULL_TREE, arg,
9482                                                 build_tree_list (NULL_TREE,
9483                                                                  arg01));
9484                       return build_function_call_expr (powfn, arglist);
9485                     }
9486
9487                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
9488                   if (operand_equal_p (arg00, arg10, 0))
9489                     {
9490                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9491                       tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
9492                       tree arglist = tree_cons (NULL_TREE, arg00,
9493                                                 build_tree_list (NULL_TREE,
9494                                                                  arg));
9495                       return build_function_call_expr (powfn, arglist);
9496                     }
9497                 }
9498
9499               /* Optimize tan(x)*cos(x) as sin(x).  */
9500               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
9501                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
9502                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
9503                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
9504                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
9505                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
9506                   && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
9507                                       TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
9508                 {
9509                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
9510
9511                   if (sinfn != NULL_TREE)
9512                     return build_function_call_expr (sinfn,
9513                                                      TREE_OPERAND (arg0, 1));
9514                 }
9515
9516               /* Optimize x*pow(x,c) as pow(x,c+1).  */
9517               if (fcode1 == BUILT_IN_POW
9518                   || fcode1 == BUILT_IN_POWF
9519                   || fcode1 == BUILT_IN_POWL)
9520                 {
9521                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
9522                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
9523                                                                      1)));
9524                   if (TREE_CODE (arg11) == REAL_CST
9525                       && ! TREE_CONSTANT_OVERFLOW (arg11)
9526                       && operand_equal_p (arg0, arg10, 0))
9527                     {
9528                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
9529                       REAL_VALUE_TYPE c;
9530                       tree arg, arglist;
9531
9532                       c = TREE_REAL_CST (arg11);
9533                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
9534                       arg = build_real (type, c);
9535                       arglist = build_tree_list (NULL_TREE, arg);
9536                       arglist = tree_cons (NULL_TREE, arg0, arglist);
9537                       return build_function_call_expr (powfn, arglist);
9538                     }
9539                 }
9540
9541               /* Optimize pow(x,c)*x as pow(x,c+1).  */
9542               if (fcode0 == BUILT_IN_POW
9543                   || fcode0 == BUILT_IN_POWF
9544                   || fcode0 == BUILT_IN_POWL)
9545                 {
9546                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
9547                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
9548                                                                      1)));
9549                   if (TREE_CODE (arg01) == REAL_CST
9550                       && ! TREE_CONSTANT_OVERFLOW (arg01)
9551                       && operand_equal_p (arg1, arg00, 0))
9552                     {
9553                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
9554                       REAL_VALUE_TYPE c;
9555                       tree arg, arglist;
9556
9557                       c = TREE_REAL_CST (arg01);
9558                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
9559                       arg = build_real (type, c);
9560                       arglist = build_tree_list (NULL_TREE, arg);
9561                       arglist = tree_cons (NULL_TREE, arg1, arglist);
9562                       return build_function_call_expr (powfn, arglist);
9563                     }
9564                 }
9565
9566               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
9567               if (! optimize_size
9568                   && operand_equal_p (arg0, arg1, 0))
9569                 {
9570                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
9571
9572                   if (powfn)
9573                     {
9574                       tree arg = build_real (type, dconst2);
9575                       tree arglist = build_tree_list (NULL_TREE, arg);
9576                       arglist = tree_cons (NULL_TREE, arg0, arglist);
9577                       return build_function_call_expr (powfn, arglist);
9578                     }
9579                 }
9580             }
9581         }
9582       goto associate;
9583
9584     case BIT_IOR_EXPR:
9585     bit_ior:
9586       if (integer_all_onesp (arg1))
9587         return omit_one_operand (type, arg1, arg0);
9588       if (integer_zerop (arg1))
9589         return non_lvalue (fold_convert (type, arg0));
9590       if (operand_equal_p (arg0, arg1, 0))
9591         return non_lvalue (fold_convert (type, arg0));
9592
9593       /* ~X | X is -1.  */
9594       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9595           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9596         {
9597           t1 = build_int_cst_type (type, -1);
9598           return omit_one_operand (type, t1, arg1);
9599         }
9600
9601       /* X | ~X is -1.  */
9602       if (TREE_CODE (arg1) == BIT_NOT_EXPR
9603           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9604         {
9605           t1 = build_int_cst_type (type, -1);
9606           return omit_one_operand (type, t1, arg0);
9607         }
9608
9609       /* Canonicalize (X & C1) | C2.  */
9610       if (TREE_CODE (arg0) == BIT_AND_EXPR
9611           && TREE_CODE (arg1) == INTEGER_CST
9612           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9613         {
9614           unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
9615           int width = TYPE_PRECISION (type);
9616           hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
9617           lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
9618           hi2 = TREE_INT_CST_HIGH (arg1);
9619           lo2 = TREE_INT_CST_LOW (arg1);
9620
9621           /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2).  */
9622           if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
9623             return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
9624
9625           if (width > HOST_BITS_PER_WIDE_INT)
9626             {
9627               mhi = (unsigned HOST_WIDE_INT) -1 
9628                     >> (2 * HOST_BITS_PER_WIDE_INT - width);
9629               mlo = -1;
9630             }
9631           else
9632             {
9633               mhi = 0;
9634               mlo = (unsigned HOST_WIDE_INT) -1
9635                     >> (HOST_BITS_PER_WIDE_INT - width);
9636             }
9637
9638           /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2.  */
9639           if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
9640             return fold_build2 (BIT_IOR_EXPR, type,
9641                                 TREE_OPERAND (arg0, 0), arg1);
9642
9643           /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2.  */
9644           hi1 &= mhi;
9645           lo1 &= mlo;
9646           if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
9647             return fold_build2 (BIT_IOR_EXPR, type,
9648                                 fold_build2 (BIT_AND_EXPR, type,
9649                                              TREE_OPERAND (arg0, 0),
9650                                              build_int_cst_wide (type,
9651                                                                  lo1 & ~lo2,
9652                                                                  hi1 & ~hi2)),
9653                                 arg1);
9654         }
9655
9656       /* (X & Y) | Y is (X, Y).  */
9657       if (TREE_CODE (arg0) == BIT_AND_EXPR
9658           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9659         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
9660       /* (X & Y) | X is (Y, X).  */
9661       if (TREE_CODE (arg0) == BIT_AND_EXPR
9662           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9663           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
9664         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
9665       /* X | (X & Y) is (Y, X).  */
9666       if (TREE_CODE (arg1) == BIT_AND_EXPR
9667           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
9668           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
9669         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
9670       /* X | (Y & X) is (Y, X).  */
9671       if (TREE_CODE (arg1) == BIT_AND_EXPR
9672           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
9673           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9674         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
9675
9676       t1 = distribute_bit_expr (code, type, arg0, arg1);
9677       if (t1 != NULL_TREE)
9678         return t1;
9679
9680       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
9681
9682          This results in more efficient code for machines without a NAND
9683          instruction.  Combine will canonicalize to the first form
9684          which will allow use of NAND instructions provided by the
9685          backend if they exist.  */
9686       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9687           && TREE_CODE (arg1) == BIT_NOT_EXPR)
9688         {
9689           return fold_build1 (BIT_NOT_EXPR, type,
9690                               build2 (BIT_AND_EXPR, type,
9691                                       TREE_OPERAND (arg0, 0),
9692                                       TREE_OPERAND (arg1, 0)));
9693         }
9694
9695       /* See if this can be simplified into a rotate first.  If that
9696          is unsuccessful continue in the association code.  */
9697       goto bit_rotate;
9698
9699     case BIT_XOR_EXPR:
9700       if (integer_zerop (arg1))
9701         return non_lvalue (fold_convert (type, arg0));
9702       if (integer_all_onesp (arg1))
9703         return fold_build1 (BIT_NOT_EXPR, type, arg0);
9704       if (operand_equal_p (arg0, arg1, 0))
9705         return omit_one_operand (type, integer_zero_node, arg0);
9706
9707       /* ~X ^ X is -1.  */
9708       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9709           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9710         {
9711           t1 = build_int_cst_type (type, -1);
9712           return omit_one_operand (type, t1, arg1);
9713         }
9714
9715       /* X ^ ~X is -1.  */
9716       if (TREE_CODE (arg1) == BIT_NOT_EXPR
9717           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9718         {
9719           t1 = build_int_cst_type (type, -1);
9720           return omit_one_operand (type, t1, arg0);
9721         }
9722
9723       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
9724          with a constant, and the two constants have no bits in common,
9725          we should treat this as a BIT_IOR_EXPR since this may produce more
9726          simplifications.  */
9727       if (TREE_CODE (arg0) == BIT_AND_EXPR
9728           && TREE_CODE (arg1) == BIT_AND_EXPR
9729           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9730           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9731           && integer_zerop (const_binop (BIT_AND_EXPR,
9732                                          TREE_OPERAND (arg0, 1),
9733                                          TREE_OPERAND (arg1, 1), 0)))
9734         {
9735           code = BIT_IOR_EXPR;
9736           goto bit_ior;
9737         }
9738
9739       /* (X | Y) ^ X -> Y & ~ X*/
9740       if (TREE_CODE (arg0) == BIT_IOR_EXPR
9741           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9742         {
9743           tree t2 = TREE_OPERAND (arg0, 1);
9744           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
9745                             arg1);
9746           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
9747                             fold_convert (type, t1));
9748           return t1;
9749         }
9750
9751       /* (Y | X) ^ X -> Y & ~ X*/
9752       if (TREE_CODE (arg0) == BIT_IOR_EXPR
9753           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9754         {
9755           tree t2 = TREE_OPERAND (arg0, 0);
9756           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
9757                             arg1);
9758           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
9759                             fold_convert (type, t1));
9760           return t1;
9761         }
9762
9763       /* X ^ (X | Y) -> Y & ~ X*/
9764       if (TREE_CODE (arg1) == BIT_IOR_EXPR
9765           && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
9766         {
9767           tree t2 = TREE_OPERAND (arg1, 1);
9768           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
9769                             arg0);
9770           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
9771                             fold_convert (type, t1));
9772           return t1;
9773         }
9774
9775       /* X ^ (Y | X) -> Y & ~ X*/
9776       if (TREE_CODE (arg1) == BIT_IOR_EXPR
9777           && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
9778         {
9779           tree t2 = TREE_OPERAND (arg1, 0);
9780           t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
9781                             arg0);
9782           t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
9783                             fold_convert (type, t1));
9784           return t1;
9785         }
9786         
9787       /* Convert ~X ^ ~Y to X ^ Y.  */
9788       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9789           && TREE_CODE (arg1) == BIT_NOT_EXPR)
9790         return fold_build2 (code, type,
9791                             fold_convert (type, TREE_OPERAND (arg0, 0)),
9792                             fold_convert (type, TREE_OPERAND (arg1, 0)));
9793
9794       /* Convert ~X ^ C to X ^ ~C.  */
9795       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9796           && TREE_CODE (arg1) == INTEGER_CST)
9797         return fold_build2 (code, type,
9798                             fold_convert (type, TREE_OPERAND (arg0, 0)),
9799                             fold_build1 (BIT_NOT_EXPR, type, arg1));
9800
9801       /* Fold (X & 1) ^ 1 as (X & 1) == 0.  */
9802       if (TREE_CODE (arg0) == BIT_AND_EXPR
9803           && integer_onep (TREE_OPERAND (arg0, 1))
9804           && integer_onep (arg1))
9805         return fold_build2 (EQ_EXPR, type, arg0,
9806                             build_int_cst (TREE_TYPE (arg0), 0));
9807
9808       /* Fold (X & Y) ^ Y as ~X & Y.  */
9809       if (TREE_CODE (arg0) == BIT_AND_EXPR
9810           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9811         {
9812           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
9813           return fold_build2 (BIT_AND_EXPR, type, 
9814                               fold_build1 (BIT_NOT_EXPR, type, tem),
9815                               fold_convert (type, arg1));
9816         }
9817       /* Fold (X & Y) ^ X as ~Y & X.  */
9818       if (TREE_CODE (arg0) == BIT_AND_EXPR
9819           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9820           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
9821         {
9822           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
9823           return fold_build2 (BIT_AND_EXPR, type,
9824                               fold_build1 (BIT_NOT_EXPR, type, tem),
9825                               fold_convert (type, arg1));
9826         }
9827       /* Fold X ^ (X & Y) as X & ~Y.  */
9828       if (TREE_CODE (arg1) == BIT_AND_EXPR
9829           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9830         {
9831           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
9832           return fold_build2 (BIT_AND_EXPR, type,
9833                               fold_convert (type, arg0),
9834                               fold_build1 (BIT_NOT_EXPR, type, tem));
9835         }
9836       /* Fold X ^ (Y & X) as ~Y & X.  */
9837       if (TREE_CODE (arg1) == BIT_AND_EXPR
9838           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
9839           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9840         {
9841           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
9842           return fold_build2 (BIT_AND_EXPR, type,
9843                               fold_build1 (BIT_NOT_EXPR, type, tem),
9844                               fold_convert (type, arg0));
9845         }
9846
9847       /* See if this can be simplified into a rotate first.  If that
9848          is unsuccessful continue in the association code.  */
9849       goto bit_rotate;
9850
9851     case BIT_AND_EXPR:
9852       if (integer_all_onesp (arg1))
9853         return non_lvalue (fold_convert (type, arg0));
9854       if (integer_zerop (arg1))
9855         return omit_one_operand (type, arg1, arg0);
9856       if (operand_equal_p (arg0, arg1, 0))
9857         return non_lvalue (fold_convert (type, arg0));
9858
9859       /* ~X & X is always zero.  */
9860       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9861           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9862         return omit_one_operand (type, integer_zero_node, arg1);
9863
9864       /* X & ~X is always zero.  */
9865       if (TREE_CODE (arg1) == BIT_NOT_EXPR
9866           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9867         return omit_one_operand (type, integer_zero_node, arg0);
9868
9869       /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2).  */
9870       if (TREE_CODE (arg0) == BIT_IOR_EXPR
9871           && TREE_CODE (arg1) == INTEGER_CST
9872           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9873         return fold_build2 (BIT_IOR_EXPR, type,
9874                             fold_build2 (BIT_AND_EXPR, type,
9875                                          TREE_OPERAND (arg0, 0), arg1),
9876                             fold_build2 (BIT_AND_EXPR, type,
9877                                          TREE_OPERAND (arg0, 1), arg1));
9878
9879       /* (X | Y) & Y is (X, Y).  */
9880       if (TREE_CODE (arg0) == BIT_IOR_EXPR
9881           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9882         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
9883       /* (X | Y) & X is (Y, X).  */
9884       if (TREE_CODE (arg0) == BIT_IOR_EXPR
9885           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9886           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
9887         return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
9888       /* X & (X | Y) is (Y, X).  */
9889       if (TREE_CODE (arg1) == BIT_IOR_EXPR
9890           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
9891           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
9892         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
9893       /* X & (Y | X) is (Y, X).  */
9894       if (TREE_CODE (arg1) == BIT_IOR_EXPR
9895           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
9896           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9897         return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
9898
9899       /* Fold (X ^ 1) & 1 as (X & 1) == 0.  */
9900       if (TREE_CODE (arg0) == BIT_XOR_EXPR
9901           && integer_onep (TREE_OPERAND (arg0, 1))
9902           && integer_onep (arg1))
9903         {
9904           tem = TREE_OPERAND (arg0, 0);
9905           return fold_build2 (EQ_EXPR, type,
9906                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
9907                                            build_int_cst (TREE_TYPE (tem), 1)),
9908                               build_int_cst (TREE_TYPE (tem), 0));
9909         }
9910       /* Fold ~X & 1 as (X & 1) == 0.  */
9911       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9912           && integer_onep (arg1))
9913         {
9914           tem = TREE_OPERAND (arg0, 0);
9915           return fold_build2 (EQ_EXPR, type,
9916                               fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
9917                                            build_int_cst (TREE_TYPE (tem), 1)),
9918                               build_int_cst (TREE_TYPE (tem), 0));
9919         }
9920
9921       /* Fold (X ^ Y) & Y as ~X & Y.  */
9922       if (TREE_CODE (arg0) == BIT_XOR_EXPR
9923           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9924         {
9925           tem = fold_convert (type, TREE_OPERAND (arg0, 0));
9926           return fold_build2 (BIT_AND_EXPR, type, 
9927                               fold_build1 (BIT_NOT_EXPR, type, tem),
9928                               fold_convert (type, arg1));
9929         }
9930       /* Fold (X ^ Y) & X as ~Y & X.  */
9931       if (TREE_CODE (arg0) == BIT_XOR_EXPR
9932           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
9933           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
9934         {
9935           tem = fold_convert (type, TREE_OPERAND (arg0, 1));
9936           return fold_build2 (BIT_AND_EXPR, type,
9937                               fold_build1 (BIT_NOT_EXPR, type, tem),
9938                               fold_convert (type, arg1));
9939         }
9940       /* Fold X & (X ^ Y) as X & ~Y.  */
9941       if (TREE_CODE (arg1) == BIT_XOR_EXPR
9942           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9943         {
9944           tem = fold_convert (type, TREE_OPERAND (arg1, 1));
9945           return fold_build2 (BIT_AND_EXPR, type,
9946                               fold_convert (type, arg0),
9947                               fold_build1 (BIT_NOT_EXPR, type, tem));
9948         }
9949       /* Fold X & (Y ^ X) as ~Y & X.  */
9950       if (TREE_CODE (arg1) == BIT_XOR_EXPR
9951           && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
9952           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9953         {
9954           tem = fold_convert (type, TREE_OPERAND (arg1, 0));
9955           return fold_build2 (BIT_AND_EXPR, type,
9956                               fold_build1 (BIT_NOT_EXPR, type, tem),
9957                               fold_convert (type, arg0));
9958         }
9959
9960       t1 = distribute_bit_expr (code, type, arg0, arg1);
9961       if (t1 != NULL_TREE)
9962         return t1;
9963       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
9964       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
9965           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
9966         {
9967           unsigned int prec
9968             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
9969
9970           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
9971               && (~TREE_INT_CST_LOW (arg1)
9972                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
9973             return fold_convert (type, TREE_OPERAND (arg0, 0));
9974         }
9975
9976       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
9977
9978          This results in more efficient code for machines without a NOR
9979          instruction.  Combine will canonicalize to the first form
9980          which will allow use of NOR instructions provided by the
9981          backend if they exist.  */
9982       if (TREE_CODE (arg0) == BIT_NOT_EXPR
9983           && TREE_CODE (arg1) == BIT_NOT_EXPR)
9984         {
9985           return fold_build1 (BIT_NOT_EXPR, type,
9986                               build2 (BIT_IOR_EXPR, type,
9987                                       TREE_OPERAND (arg0, 0),
9988                                       TREE_OPERAND (arg1, 0)));
9989         }
9990
9991       goto associate;
9992
9993     case RDIV_EXPR:
9994       /* Don't touch a floating-point divide by zero unless the mode
9995          of the constant can represent infinity.  */
9996       if (TREE_CODE (arg1) == REAL_CST
9997           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
9998           && real_zerop (arg1))
9999         return NULL_TREE;
10000
10001       /* Optimize A / A to 1.0 if we don't care about
10002          NaNs or Infinities.  Skip the transformation
10003          for non-real operands.  */
10004       if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10005           && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10006           && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10007           && operand_equal_p (arg0, arg1, 0))
10008         {
10009           tree r = build_real (TREE_TYPE (arg0), dconst1);
10010
10011           return omit_two_operands (type, r, arg0, arg1);
10012         }
10013
10014       /* The complex version of the above A / A optimization.  */
10015       if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10016           && operand_equal_p (arg0, arg1, 0))
10017         {
10018           tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10019           if (! HONOR_NANS (TYPE_MODE (elem_type))
10020               && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10021             {
10022               tree r = build_real (elem_type, dconst1);
10023               /* omit_two_operands will call fold_convert for us.  */
10024               return omit_two_operands (type, r, arg0, arg1);
10025             }
10026         }
10027
10028       /* (-A) / (-B) -> A / B  */
10029       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10030         return fold_build2 (RDIV_EXPR, type,
10031                             TREE_OPERAND (arg0, 0),
10032                             negate_expr (arg1));
10033       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10034         return fold_build2 (RDIV_EXPR, type,
10035                             negate_expr (arg0),
10036                             TREE_OPERAND (arg1, 0));
10037
10038       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
10039       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10040           && real_onep (arg1))
10041         return non_lvalue (fold_convert (type, arg0));
10042
10043       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
10044       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10045           && real_minus_onep (arg1))
10046         return non_lvalue (fold_convert (type, negate_expr (arg0)));
10047
10048       /* If ARG1 is a constant, we can convert this to a multiply by the
10049          reciprocal.  This does not have the same rounding properties,
10050          so only do this if -funsafe-math-optimizations.  We can actually
10051          always safely do it if ARG1 is a power of two, but it's hard to
10052          tell if it is or not in a portable manner.  */
10053       if (TREE_CODE (arg1) == REAL_CST)
10054         {
10055           if (flag_unsafe_math_optimizations
10056               && 0 != (tem = const_binop (code, build_real (type, dconst1),
10057                                           arg1, 0)))
10058             return fold_build2 (MULT_EXPR, type, arg0, tem);
10059           /* Find the reciprocal if optimizing and the result is exact.  */
10060           if (optimize)
10061             {
10062               REAL_VALUE_TYPE r;
10063               r = TREE_REAL_CST (arg1);
10064               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
10065                 {
10066                   tem = build_real (type, r);
10067                   return fold_build2 (MULT_EXPR, type,
10068                                       fold_convert (type, arg0), tem);
10069                 }
10070             }
10071         }
10072       /* Convert A/B/C to A/(B*C).  */
10073       if (flag_unsafe_math_optimizations
10074           && TREE_CODE (arg0) == RDIV_EXPR)
10075         return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10076                             fold_build2 (MULT_EXPR, type,
10077                                          TREE_OPERAND (arg0, 1), arg1));
10078
10079       /* Convert A/(B/C) to (A/B)*C.  */
10080       if (flag_unsafe_math_optimizations
10081           && TREE_CODE (arg1) == RDIV_EXPR)
10082         return fold_build2 (MULT_EXPR, type,
10083                             fold_build2 (RDIV_EXPR, type, arg0,
10084                                          TREE_OPERAND (arg1, 0)),
10085                             TREE_OPERAND (arg1, 1));
10086
10087       /* Convert C1/(X*C2) into (C1/C2)/X.  */
10088       if (flag_unsafe_math_optimizations
10089           && TREE_CODE (arg1) == MULT_EXPR
10090           && TREE_CODE (arg0) == REAL_CST
10091           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10092         {
10093           tree tem = const_binop (RDIV_EXPR, arg0,
10094                                   TREE_OPERAND (arg1, 1), 0);
10095           if (tem)
10096             return fold_build2 (RDIV_EXPR, type, tem,
10097                                 TREE_OPERAND (arg1, 0));
10098         }
10099
10100       if (flag_unsafe_math_optimizations)
10101         {
10102           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10103           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10104
10105           /* Optimize sin(x)/cos(x) as tan(x).  */
10106           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
10107                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
10108                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
10109               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
10110                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
10111             {
10112               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10113
10114               if (tanfn != NULL_TREE)
10115                 return build_function_call_expr (tanfn,
10116                                                  TREE_OPERAND (arg0, 1));
10117             }
10118
10119           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
10120           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
10121                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
10122                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
10123               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
10124                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
10125             {
10126               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10127
10128               if (tanfn != NULL_TREE)
10129                 {
10130                   tree tmp = TREE_OPERAND (arg0, 1);
10131                   tmp = build_function_call_expr (tanfn, tmp);
10132                   return fold_build2 (RDIV_EXPR, type,
10133                                       build_real (type, dconst1), tmp);
10134                 }
10135             }
10136
10137           /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
10138              NaNs or Infinities.  */
10139           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
10140                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
10141                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
10142             {
10143               tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
10144               tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
10145
10146               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
10147                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
10148                   && operand_equal_p (arg00, arg01, 0))
10149                 {
10150                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10151
10152                   if (cosfn != NULL_TREE)
10153                     return build_function_call_expr (cosfn,
10154                                                      TREE_OPERAND (arg0, 1));
10155                 }
10156             }
10157
10158           /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
10159              NaNs or Infinities.  */
10160           if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
10161                || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
10162                || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
10163             {
10164               tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
10165               tree arg01 = TREE_VALUE (TREE_OPERAND (arg1, 1));
10166
10167               if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
10168                   && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
10169                   && operand_equal_p (arg00, arg01, 0))
10170                 {
10171                   tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
10172
10173                   if (cosfn != NULL_TREE)
10174                     {
10175                       tree tmp = TREE_OPERAND (arg0, 1);
10176                       tmp = build_function_call_expr (cosfn, tmp);
10177                       return fold_build2 (RDIV_EXPR, type,
10178                                           build_real (type, dconst1),
10179                                           tmp);
10180                     }
10181                 }
10182             }
10183
10184           /* Optimize pow(x,c)/x as pow(x,c-1).  */
10185           if (fcode0 == BUILT_IN_POW
10186               || fcode0 == BUILT_IN_POWF
10187               || fcode0 == BUILT_IN_POWL)
10188             {
10189               tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
10190               tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
10191               if (TREE_CODE (arg01) == REAL_CST
10192                   && ! TREE_CONSTANT_OVERFLOW (arg01)
10193                   && operand_equal_p (arg1, arg00, 0))
10194                 {
10195                   tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10196                   REAL_VALUE_TYPE c;
10197                   tree arg, arglist;
10198
10199                   c = TREE_REAL_CST (arg01);
10200                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
10201                   arg = build_real (type, c);
10202                   arglist = build_tree_list (NULL_TREE, arg);
10203                   arglist = tree_cons (NULL_TREE, arg1, arglist);
10204                   return build_function_call_expr (powfn, arglist);
10205                 }
10206             }
10207
10208           /* Optimize x/expN(y) into x*expN(-y).  */
10209           if (BUILTIN_EXPONENT_P (fcode1))
10210             {
10211               tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
10212               tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
10213               tree arglist = build_tree_list (NULL_TREE,
10214                                               fold_convert (type, arg));
10215               arg1 = build_function_call_expr (expfn, arglist);
10216               return fold_build2 (MULT_EXPR, type, arg0, arg1);
10217             }
10218
10219           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
10220           if (fcode1 == BUILT_IN_POW
10221               || fcode1 == BUILT_IN_POWF
10222               || fcode1 == BUILT_IN_POWL)
10223             {
10224               tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
10225               tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
10226               tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
10227               tree neg11 = fold_convert (type, negate_expr (arg11));
10228               tree arglist = tree_cons(NULL_TREE, arg10,
10229                                        build_tree_list (NULL_TREE, neg11));
10230               arg1 = build_function_call_expr (powfn, arglist);
10231               return fold_build2 (MULT_EXPR, type, arg0, arg1);
10232             }
10233         }
10234       return NULL_TREE;
10235
10236     case TRUNC_DIV_EXPR:
10237     case FLOOR_DIV_EXPR:
10238       /* Simplify A / (B << N) where A and B are positive and B is
10239          a power of 2, to A >> (N + log2(B)).  */
10240       if (TREE_CODE (arg1) == LSHIFT_EXPR
10241           && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0)))
10242         {
10243           tree sval = TREE_OPERAND (arg1, 0);
10244           if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10245             {
10246               tree sh_cnt = TREE_OPERAND (arg1, 1);
10247               unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
10248
10249               sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
10250                                     sh_cnt, build_int_cst (NULL_TREE, pow2));
10251               return fold_build2 (RSHIFT_EXPR, type,
10252                                   fold_convert (type, arg0), sh_cnt);
10253             }
10254         }
10255       /* Fall thru */
10256
10257     case ROUND_DIV_EXPR:
10258     case CEIL_DIV_EXPR:
10259     case EXACT_DIV_EXPR:
10260       if (integer_onep (arg1))
10261         return non_lvalue (fold_convert (type, arg0));
10262       if (integer_zerop (arg1))
10263         return NULL_TREE;
10264       /* X / -1 is -X.  */
10265       if (!TYPE_UNSIGNED (type)
10266           && TREE_CODE (arg1) == INTEGER_CST
10267           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
10268           && TREE_INT_CST_HIGH (arg1) == -1)
10269         return fold_convert (type, negate_expr (arg0));
10270
10271       /* Convert -A / -B to A / B when the type is signed and overflow is
10272          undefined.  */
10273       if (!TYPE_UNSIGNED (type) && !flag_wrapv
10274           && TREE_CODE (arg0) == NEGATE_EXPR
10275           && negate_expr_p (arg1))
10276         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
10277                             negate_expr (arg1));
10278       if (!TYPE_UNSIGNED (type) && !flag_wrapv
10279           && TREE_CODE (arg1) == NEGATE_EXPR
10280           && negate_expr_p (arg0))
10281         return fold_build2 (code, type, negate_expr (arg0),
10282                             TREE_OPERAND (arg1, 0));
10283
10284       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10285          operation, EXACT_DIV_EXPR.
10286
10287          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10288          At one time others generated faster code, it's not clear if they do
10289          after the last round to changes to the DIV code in expmed.c.  */
10290       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10291           && multiple_of_p (type, arg0, arg1))
10292         return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
10293
10294       if (TREE_CODE (arg1) == INTEGER_CST
10295           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
10296         return fold_convert (type, tem);
10297
10298       return NULL_TREE;
10299
10300     case CEIL_MOD_EXPR:
10301     case FLOOR_MOD_EXPR:
10302     case ROUND_MOD_EXPR:
10303     case TRUNC_MOD_EXPR:
10304       /* X % 1 is always zero, but be sure to preserve any side
10305          effects in X.  */
10306       if (integer_onep (arg1))
10307         return omit_one_operand (type, integer_zero_node, arg0);
10308
10309       /* X % 0, return X % 0 unchanged so that we can get the
10310          proper warnings and errors.  */
10311       if (integer_zerop (arg1))
10312         return NULL_TREE;
10313
10314       /* 0 % X is always zero, but be sure to preserve any side
10315          effects in X.  Place this after checking for X == 0.  */
10316       if (integer_zerop (arg0))
10317         return omit_one_operand (type, integer_zero_node, arg1);
10318
10319       /* X % -1 is zero.  */
10320       if (!TYPE_UNSIGNED (type)
10321           && TREE_CODE (arg1) == INTEGER_CST
10322           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
10323           && TREE_INT_CST_HIGH (arg1) == -1)
10324         return omit_one_operand (type, integer_zero_node, arg0);
10325
10326       /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
10327          i.e. "X % C" into "X & (C - 1)", if X and C are positive.  */
10328       if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
10329           && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0)))
10330         {
10331           tree c = arg1;
10332           /* Also optimize A % (C << N)  where C is a power of 2,
10333              to A & ((C << N) - 1).  */
10334           if (TREE_CODE (arg1) == LSHIFT_EXPR)
10335             c = TREE_OPERAND (arg1, 0);
10336
10337           if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
10338             {
10339               tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
10340                                        build_int_cst (TREE_TYPE (arg1), 1));
10341               return fold_build2 (BIT_AND_EXPR, type,
10342                                   fold_convert (type, arg0),
10343                                   fold_convert (type, mask));
10344             }
10345         }
10346
10347       /* X % -C is the same as X % C.  */
10348       if (code == TRUNC_MOD_EXPR
10349           && !TYPE_UNSIGNED (type)
10350           && TREE_CODE (arg1) == INTEGER_CST
10351           && !TREE_CONSTANT_OVERFLOW (arg1)
10352           && TREE_INT_CST_HIGH (arg1) < 0
10353           && !flag_trapv
10354           /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
10355           && !sign_bit_p (arg1, arg1))
10356         return fold_build2 (code, type, fold_convert (type, arg0),
10357                             fold_convert (type, negate_expr (arg1)));
10358
10359       /* X % -Y is the same as X % Y.  */
10360       if (code == TRUNC_MOD_EXPR
10361           && !TYPE_UNSIGNED (type)
10362           && TREE_CODE (arg1) == NEGATE_EXPR
10363           && !flag_trapv)
10364         return fold_build2 (code, type, fold_convert (type, arg0),
10365                             fold_convert (type, TREE_OPERAND (arg1, 0)));
10366
10367       if (TREE_CODE (arg1) == INTEGER_CST
10368           && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
10369         return fold_convert (type, tem);
10370
10371       return NULL_TREE;
10372
10373     case LROTATE_EXPR:
10374     case RROTATE_EXPR:
10375       if (integer_all_onesp (arg0))
10376         return omit_one_operand (type, arg0, arg1);
10377       goto shift;
10378
10379     case RSHIFT_EXPR:
10380       /* Optimize -1 >> x for arithmetic right shifts.  */
10381       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
10382         return omit_one_operand (type, arg0, arg1);
10383       /* ... fall through ...  */
10384
10385     case LSHIFT_EXPR:
10386     shift:
10387       if (integer_zerop (arg1))
10388         return non_lvalue (fold_convert (type, arg0));
10389       if (integer_zerop (arg0))
10390         return omit_one_operand (type, arg0, arg1);
10391
10392       /* Since negative shift count is not well-defined,
10393          don't try to compute it in the compiler.  */
10394       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10395         return NULL_TREE;
10396
10397       /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
10398       if (TREE_CODE (op0) == code && host_integerp (arg1, false)
10399           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
10400           && host_integerp (TREE_OPERAND (arg0, 1), false)
10401           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
10402         {
10403           HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
10404                                + TREE_INT_CST_LOW (arg1));
10405
10406           /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
10407              being well defined.  */
10408           if (low >= TYPE_PRECISION (type))
10409             {
10410               if (code == LROTATE_EXPR || code == RROTATE_EXPR)
10411                 low = low % TYPE_PRECISION (type);
10412               else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
10413                 return build_int_cst (type, 0);
10414               else
10415                 low = TYPE_PRECISION (type) - 1;
10416             }
10417
10418           return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
10419                               build_int_cst (type, low));
10420         }
10421
10422       /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
10423          into x & ((unsigned)-1 >> c) for unsigned types.  */
10424       if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
10425            || (TYPE_UNSIGNED (type)
10426                && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
10427           && host_integerp (arg1, false)
10428           && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
10429           && host_integerp (TREE_OPERAND (arg0, 1), false)
10430           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
10431         {
10432           HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10433           HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
10434           tree lshift;
10435           tree arg00;
10436
10437           if (low0 == low1)
10438             {
10439               arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10440
10441               lshift = build_int_cst (type, -1);
10442               lshift = int_const_binop (code, lshift, arg1, 0);
10443
10444               return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
10445             }
10446         }
10447
10448       /* Rewrite an LROTATE_EXPR by a constant into an
10449          RROTATE_EXPR by a new constant.  */
10450       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
10451         {
10452           tree tem = build_int_cst (TREE_TYPE (arg1),
10453                                     GET_MODE_BITSIZE (TYPE_MODE (type)));
10454           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
10455           return fold_build2 (RROTATE_EXPR, type, arg0, tem);
10456         }
10457
10458       /* If we have a rotate of a bit operation with the rotate count and
10459          the second operand of the bit operation both constant,
10460          permute the two operations.  */
10461       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10462           && (TREE_CODE (arg0) == BIT_AND_EXPR
10463               || TREE_CODE (arg0) == BIT_IOR_EXPR
10464               || TREE_CODE (arg0) == BIT_XOR_EXPR)
10465           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10466         return fold_build2 (TREE_CODE (arg0), type,
10467                             fold_build2 (code, type,
10468                                          TREE_OPERAND (arg0, 0), arg1),
10469                             fold_build2 (code, type,
10470                                          TREE_OPERAND (arg0, 1), arg1));
10471
10472       /* Two consecutive rotates adding up to the width of the mode can
10473          be ignored.  */
10474       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10475           && TREE_CODE (arg0) == RROTATE_EXPR
10476           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10477           && TREE_INT_CST_HIGH (arg1) == 0
10478           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
10479           && ((TREE_INT_CST_LOW (arg1)
10480                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
10481               == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
10482         return TREE_OPERAND (arg0, 0);
10483
10484       return NULL_TREE;
10485
10486     case MIN_EXPR:
10487       if (operand_equal_p (arg0, arg1, 0))
10488         return omit_one_operand (type, arg0, arg1);
10489       if (INTEGRAL_TYPE_P (type)
10490           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
10491         return omit_one_operand (type, arg1, arg0);
10492       tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
10493       if (tem)
10494         return tem;
10495       goto associate;
10496
10497     case MAX_EXPR:
10498       if (operand_equal_p (arg0, arg1, 0))
10499         return omit_one_operand (type, arg0, arg1);
10500       if (INTEGRAL_TYPE_P (type)
10501           && TYPE_MAX_VALUE (type)
10502           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
10503         return omit_one_operand (type, arg1, arg0);
10504       tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
10505       if (tem)
10506         return tem;
10507       goto associate;
10508
10509     case TRUTH_ANDIF_EXPR:
10510       /* Note that the operands of this must be ints
10511          and their values must be 0 or 1.
10512          ("true" is a fixed value perhaps depending on the language.)  */
10513       /* If first arg is constant zero, return it.  */
10514       if (integer_zerop (arg0))
10515         return fold_convert (type, arg0);
10516     case TRUTH_AND_EXPR:
10517       /* If either arg is constant true, drop it.  */
10518       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10519         return non_lvalue (fold_convert (type, arg1));
10520       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10521           /* Preserve sequence points.  */
10522           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10523         return non_lvalue (fold_convert (type, arg0));
10524       /* If second arg is constant zero, result is zero, but first arg
10525          must be evaluated.  */
10526       if (integer_zerop (arg1))
10527         return omit_one_operand (type, arg1, arg0);
10528       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10529          case will be handled here.  */
10530       if (integer_zerop (arg0))
10531         return omit_one_operand (type, arg0, arg1);
10532
10533       /* !X && X is always false.  */
10534       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10535           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10536         return omit_one_operand (type, integer_zero_node, arg1);
10537       /* X && !X is always false.  */
10538       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10539           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10540         return omit_one_operand (type, integer_zero_node, arg0);
10541
10542       /* A < X && A + 1 > Y ==> A < X && A >= Y.  Normally A + 1 > Y
10543          means A >= Y && A != MAX, but in this case we know that
10544          A < X <= MAX.  */
10545
10546       if (!TREE_SIDE_EFFECTS (arg0)
10547           && !TREE_SIDE_EFFECTS (arg1))
10548         {
10549           tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
10550           if (tem && !operand_equal_p (tem, arg0, 0))
10551             return fold_build2 (code, type, tem, arg1);
10552
10553           tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
10554           if (tem && !operand_equal_p (tem, arg1, 0))
10555             return fold_build2 (code, type, arg0, tem);
10556         }
10557
10558     truth_andor:
10559       /* We only do these simplifications if we are optimizing.  */
10560       if (!optimize)
10561         return NULL_TREE;
10562
10563       /* Check for things like (A || B) && (A || C).  We can convert this
10564          to A || (B && C).  Note that either operator can be any of the four
10565          truth and/or operations and the transformation will still be
10566          valid.   Also note that we only care about order for the
10567          ANDIF and ORIF operators.  If B contains side effects, this
10568          might change the truth-value of A.  */
10569       if (TREE_CODE (arg0) == TREE_CODE (arg1)
10570           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
10571               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
10572               || TREE_CODE (arg0) == TRUTH_AND_EXPR
10573               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
10574           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
10575         {
10576           tree a00 = TREE_OPERAND (arg0, 0);
10577           tree a01 = TREE_OPERAND (arg0, 1);
10578           tree a10 = TREE_OPERAND (arg1, 0);
10579           tree a11 = TREE_OPERAND (arg1, 1);
10580           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
10581                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
10582                              && (code == TRUTH_AND_EXPR
10583                                  || code == TRUTH_OR_EXPR));
10584
10585           if (operand_equal_p (a00, a10, 0))
10586             return fold_build2 (TREE_CODE (arg0), type, a00,
10587                                 fold_build2 (code, type, a01, a11));
10588           else if (commutative && operand_equal_p (a00, a11, 0))
10589             return fold_build2 (TREE_CODE (arg0), type, a00,
10590                                 fold_build2 (code, type, a01, a10));
10591           else if (commutative && operand_equal_p (a01, a10, 0))
10592             return fold_build2 (TREE_CODE (arg0), type, a01,
10593                                 fold_build2 (code, type, a00, a11));
10594
10595           /* This case if tricky because we must either have commutative
10596              operators or else A10 must not have side-effects.  */
10597
10598           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
10599                    && operand_equal_p (a01, a11, 0))
10600             return fold_build2 (TREE_CODE (arg0), type,
10601                                 fold_build2 (code, type, a00, a10),
10602                                 a01);
10603         }
10604
10605       /* See if we can build a range comparison.  */
10606       if (0 != (tem = fold_range_test (code, type, op0, op1)))
10607         return tem;
10608
10609       /* Check for the possibility of merging component references.  If our
10610          lhs is another similar operation, try to merge its rhs with our
10611          rhs.  Then try to merge our lhs and rhs.  */
10612       if (TREE_CODE (arg0) == code
10613           && 0 != (tem = fold_truthop (code, type,
10614                                        TREE_OPERAND (arg0, 1), arg1)))
10615         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
10616
10617       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
10618         return tem;
10619
10620       return NULL_TREE;
10621
10622     case TRUTH_ORIF_EXPR:
10623       /* Note that the operands of this must be ints
10624          and their values must be 0 or true.
10625          ("true" is a fixed value perhaps depending on the language.)  */
10626       /* If first arg is constant true, return it.  */
10627       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10628         return fold_convert (type, arg0);
10629     case TRUTH_OR_EXPR:
10630       /* If either arg is constant zero, drop it.  */
10631       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10632         return non_lvalue (fold_convert (type, arg1));
10633       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10634           /* Preserve sequence points.  */
10635           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10636         return non_lvalue (fold_convert (type, arg0));
10637       /* If second arg is constant true, result is true, but we must
10638          evaluate first arg.  */
10639       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10640         return omit_one_operand (type, arg1, arg0);
10641       /* Likewise for first arg, but note this only occurs here for
10642          TRUTH_OR_EXPR.  */
10643       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10644         return omit_one_operand (type, arg0, arg1);
10645
10646       /* !X || X is always true.  */
10647       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10648           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10649         return omit_one_operand (type, integer_one_node, arg1);
10650       /* X || !X is always true.  */
10651       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10652           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10653         return omit_one_operand (type, integer_one_node, arg0);
10654
10655       goto truth_andor;
10656
10657     case TRUTH_XOR_EXPR:
10658       /* If the second arg is constant zero, drop it.  */
10659       if (integer_zerop (arg1))
10660         return non_lvalue (fold_convert (type, arg0));
10661       /* If the second arg is constant true, this is a logical inversion.  */
10662       if (integer_onep (arg1))
10663         {
10664           /* Only call invert_truthvalue if operand is a truth value.  */
10665           if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
10666             tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
10667           else
10668             tem = invert_truthvalue (arg0);
10669           return non_lvalue (fold_convert (type, tem));
10670         }
10671       /* Identical arguments cancel to zero.  */
10672       if (operand_equal_p (arg0, arg1, 0))
10673         return omit_one_operand (type, integer_zero_node, arg0);
10674
10675       /* !X ^ X is always true.  */
10676       if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10677           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10678         return omit_one_operand (type, integer_one_node, arg1);
10679
10680       /* X ^ !X is always true.  */
10681       if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10682           && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10683         return omit_one_operand (type, integer_one_node, arg0);
10684
10685       return NULL_TREE;
10686
10687     case EQ_EXPR:
10688     case NE_EXPR:
10689       tem = fold_comparison (code, type, op0, op1);
10690       if (tem != NULL_TREE)
10691         return tem;
10692
10693       /* bool_var != 0 becomes bool_var. */
10694       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10695           && code == NE_EXPR)
10696         return non_lvalue (fold_convert (type, arg0));
10697
10698       /* bool_var == 1 becomes bool_var. */
10699       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10700           && code == EQ_EXPR)
10701         return non_lvalue (fold_convert (type, arg0));
10702
10703       /* bool_var != 1 becomes !bool_var. */
10704       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10705           && code == NE_EXPR)
10706         return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
10707
10708       /* bool_var == 0 becomes !bool_var. */
10709       if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10710           && code == EQ_EXPR)
10711         return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
10712
10713       /* If this is an equality comparison of the address of a non-weak
10714          object against zero, then we know the result.  */
10715       if (TREE_CODE (arg0) == ADDR_EXPR
10716           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
10717           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
10718           && integer_zerop (arg1))
10719         return constant_boolean_node (code != EQ_EXPR, type);
10720
10721       /* If this is an equality comparison of the address of two non-weak,
10722          unaliased symbols neither of which are extern (since we do not
10723          have access to attributes for externs), then we know the result.  */
10724       if (TREE_CODE (arg0) == ADDR_EXPR
10725           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
10726           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
10727           && ! lookup_attribute ("alias",
10728                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
10729           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
10730           && TREE_CODE (arg1) == ADDR_EXPR
10731           && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
10732           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
10733           && ! lookup_attribute ("alias",
10734                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
10735           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
10736         {
10737           /* We know that we're looking at the address of two
10738              non-weak, unaliased, static _DECL nodes.
10739
10740              It is both wasteful and incorrect to call operand_equal_p
10741              to compare the two ADDR_EXPR nodes.  It is wasteful in that
10742              all we need to do is test pointer equality for the arguments
10743              to the two ADDR_EXPR nodes.  It is incorrect to use
10744              operand_equal_p as that function is NOT equivalent to a
10745              C equality test.  It can in fact return false for two
10746              objects which would test as equal using the C equality
10747              operator.  */
10748           bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
10749           return constant_boolean_node (equal
10750                                         ? code == EQ_EXPR : code != EQ_EXPR,
10751                                         type);
10752         }
10753
10754       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
10755          a MINUS_EXPR of a constant, we can convert it into a comparison with
10756          a revised constant as long as no overflow occurs.  */
10757       if (TREE_CODE (arg1) == INTEGER_CST
10758           && (TREE_CODE (arg0) == PLUS_EXPR
10759               || TREE_CODE (arg0) == MINUS_EXPR)
10760           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10761           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
10762                                       ? MINUS_EXPR : PLUS_EXPR,
10763                                       fold_convert (TREE_TYPE (arg0), arg1),
10764                                       TREE_OPERAND (arg0, 1), 0))
10765           && ! TREE_CONSTANT_OVERFLOW (tem))
10766         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
10767
10768       /* Similarly for a NEGATE_EXPR.  */
10769       if (TREE_CODE (arg0) == NEGATE_EXPR
10770           && TREE_CODE (arg1) == INTEGER_CST
10771           && 0 != (tem = negate_expr (arg1))
10772           && TREE_CODE (tem) == INTEGER_CST
10773           && ! TREE_CONSTANT_OVERFLOW (tem))
10774         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
10775
10776       /* Similarly for a BIT_XOR_EXPR;  X ^ C1 == C2 is X == (C1 ^ C2).  */
10777       if (TREE_CODE (arg0) == BIT_XOR_EXPR
10778           && TREE_CODE (arg1) == INTEGER_CST
10779           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10780         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
10781                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
10782                                          fold_convert (TREE_TYPE (arg0), arg1),
10783                                          TREE_OPERAND (arg0, 1)));
10784
10785       /* If we have X - Y == 0, we can convert that to X == Y and similarly
10786          for !=.  Don't do this for ordered comparisons due to overflow.  */
10787       if (TREE_CODE (arg0) == MINUS_EXPR
10788           && integer_zerop (arg1))
10789         return fold_build2 (code, type,
10790                             TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
10791
10792       /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
10793       if (TREE_CODE (arg0) == ABS_EXPR
10794           && (integer_zerop (arg1) || real_zerop (arg1)))
10795         return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
10796
10797       /* If this is an EQ or NE comparison with zero and ARG0 is
10798          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
10799          two operations, but the latter can be done in one less insn
10800          on machines that have only two-operand insns or on which a
10801          constant cannot be the first operand.  */
10802       if (TREE_CODE (arg0) == BIT_AND_EXPR
10803           && integer_zerop (arg1))
10804         {
10805           tree arg00 = TREE_OPERAND (arg0, 0);
10806           tree arg01 = TREE_OPERAND (arg0, 1);
10807           if (TREE_CODE (arg00) == LSHIFT_EXPR
10808               && integer_onep (TREE_OPERAND (arg00, 0)))
10809             return
10810               fold_build2 (code, type,
10811                            build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10812                                    build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
10813                                            arg01, TREE_OPERAND (arg00, 1)),
10814                                    fold_convert (TREE_TYPE (arg0),
10815                                                  integer_one_node)),
10816                            arg1);
10817           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
10818                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
10819             return
10820               fold_build2 (code, type,
10821                            build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10822                                    build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
10823                                            arg00, TREE_OPERAND (arg01, 1)),
10824                                    fold_convert (TREE_TYPE (arg0),
10825                                                  integer_one_node)),
10826                            arg1);
10827         }
10828
10829       /* If this is an NE or EQ comparison of zero against the result of a
10830          signed MOD operation whose second operand is a power of 2, make
10831          the MOD operation unsigned since it is simpler and equivalent.  */
10832       if (integer_zerop (arg1)
10833           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10834           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10835               || TREE_CODE (arg0) == CEIL_MOD_EXPR
10836               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10837               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10838           && integer_pow2p (TREE_OPERAND (arg0, 1)))
10839         {
10840           tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
10841           tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
10842                                      fold_convert (newtype,
10843                                                    TREE_OPERAND (arg0, 0)),
10844                                      fold_convert (newtype,
10845                                                    TREE_OPERAND (arg0, 1)));
10846
10847           return fold_build2 (code, type, newmod,
10848                               fold_convert (newtype, arg1));
10849         }
10850
10851       /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10852          C1 is a valid shift constant, and C2 is a power of two, i.e.
10853          a single bit.  */
10854       if (TREE_CODE (arg0) == BIT_AND_EXPR
10855           && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10856           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10857              == INTEGER_CST
10858           && integer_pow2p (TREE_OPERAND (arg0, 1))
10859           && integer_zerop (arg1))
10860         {
10861           tree itype = TREE_TYPE (arg0);
10862           unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
10863           tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10864
10865           /* Check for a valid shift count.  */
10866           if (TREE_INT_CST_HIGH (arg001) == 0
10867               && TREE_INT_CST_LOW (arg001) < prec)
10868             {
10869               tree arg01 = TREE_OPERAND (arg0, 1);
10870               tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10871               unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10872               /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10873                  can be rewritten as (X & (C2 << C1)) != 0.  */
10874               if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10875                 {
10876                   tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
10877                   tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
10878                   return fold_build2 (code, type, tem, arg1);
10879                 }
10880               /* Otherwise, for signed (arithmetic) shifts,
10881                  ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10882                  ((X >> C1) & C2) == 0 is rewritten as X >= 0.  */
10883               else if (!TYPE_UNSIGNED (itype))
10884                 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10885                                     arg000, build_int_cst (itype, 0));
10886               /* Otherwise, of unsigned (logical) shifts,
10887                  ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10888                  ((X >> C1) & C2) == 0 is rewritten as (X,true).  */
10889               else
10890                 return omit_one_operand (type,
10891                                          code == EQ_EXPR ? integer_one_node
10892                                                          : integer_zero_node,
10893                                          arg000);
10894             }
10895         }
10896
10897       /* If this is an NE comparison of zero with an AND of one, remove the
10898          comparison since the AND will give the correct value.  */
10899       if (code == NE_EXPR
10900           && integer_zerop (arg1)
10901           && TREE_CODE (arg0) == BIT_AND_EXPR
10902           && integer_onep (TREE_OPERAND (arg0, 1)))
10903         return fold_convert (type, arg0);
10904
10905       /* If we have (A & C) == C where C is a power of 2, convert this into
10906          (A & C) != 0.  Similarly for NE_EXPR.  */
10907       if (TREE_CODE (arg0) == BIT_AND_EXPR
10908           && integer_pow2p (TREE_OPERAND (arg0, 1))
10909           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10910         return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10911                             arg0, fold_convert (TREE_TYPE (arg0),
10912                                                 integer_zero_node));
10913
10914       /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
10915          bit, then fold the expression into A < 0 or A >= 0.  */
10916       tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
10917       if (tem)
10918         return tem;
10919
10920       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10921          Similarly for NE_EXPR.  */
10922       if (TREE_CODE (arg0) == BIT_AND_EXPR
10923           && TREE_CODE (arg1) == INTEGER_CST
10924           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10925         {
10926           tree notc = fold_build1 (BIT_NOT_EXPR,
10927                                    TREE_TYPE (TREE_OPERAND (arg0, 1)),
10928                                    TREE_OPERAND (arg0, 1));
10929           tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10930                                        arg1, notc);
10931           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10932           if (integer_nonzerop (dandnotc))
10933             return omit_one_operand (type, rslt, arg0);
10934         }
10935
10936       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
10937          Similarly for NE_EXPR.  */
10938       if (TREE_CODE (arg0) == BIT_IOR_EXPR
10939           && TREE_CODE (arg1) == INTEGER_CST
10940           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10941         {
10942           tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
10943           tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
10944                                        TREE_OPERAND (arg0, 1), notd);
10945           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10946           if (integer_nonzerop (candnotd))
10947             return omit_one_operand (type, rslt, arg0);
10948         }
10949
10950       /* If this is a comparison of a field, we may be able to simplify it.  */
10951       if (((TREE_CODE (arg0) == COMPONENT_REF
10952             && lang_hooks.can_use_bit_fields_p ())
10953            || TREE_CODE (arg0) == BIT_FIELD_REF)
10954           /* Handle the constant case even without -O
10955              to make sure the warnings are given.  */
10956           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10957         {
10958           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
10959           if (t1)
10960             return t1;
10961         }
10962
10963       /* Optimize comparisons of strlen vs zero to a compare of the
10964          first character of the string vs zero.  To wit,
10965                 strlen(ptr) == 0   =>  *ptr == 0
10966                 strlen(ptr) != 0   =>  *ptr != 0
10967          Other cases should reduce to one of these two (or a constant)
10968          due to the return value of strlen being unsigned.  */
10969       if (TREE_CODE (arg0) == CALL_EXPR
10970           && integer_zerop (arg1))
10971         {
10972           tree fndecl = get_callee_fndecl (arg0);
10973           tree arglist;
10974
10975           if (fndecl
10976               && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10977               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10978               && (arglist = TREE_OPERAND (arg0, 1))
10979               && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
10980               && ! TREE_CHAIN (arglist))
10981             {
10982               tree iref = build_fold_indirect_ref (TREE_VALUE (arglist));
10983               return fold_build2 (code, type, iref,
10984                                   build_int_cst (TREE_TYPE (iref), 0));
10985             }
10986         }
10987
10988       /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10989          of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
10990       if (TREE_CODE (arg0) == RSHIFT_EXPR
10991           && integer_zerop (arg1)
10992           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10993         {
10994           tree arg00 = TREE_OPERAND (arg0, 0);
10995           tree arg01 = TREE_OPERAND (arg0, 1);
10996           tree itype = TREE_TYPE (arg00);
10997           if (TREE_INT_CST_HIGH (arg01) == 0
10998               && TREE_INT_CST_LOW (arg01)
10999                  == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
11000             {
11001               if (TYPE_UNSIGNED (itype))
11002                 {
11003                   itype = lang_hooks.types.signed_type (itype);
11004                   arg00 = fold_convert (itype, arg00);
11005                 }
11006               return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11007                                   type, arg00, build_int_cst (itype, 0));
11008             }
11009         }
11010
11011       /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
11012       if (integer_zerop (arg1)
11013           && TREE_CODE (arg0) == BIT_XOR_EXPR)
11014         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11015                             TREE_OPERAND (arg0, 1));
11016
11017       /* (X ^ Y) == Y becomes X == 0.  We know that Y has no side-effects.  */
11018       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11019           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11020         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11021                             build_int_cst (TREE_TYPE (arg1), 0));
11022       /* Likewise (X ^ Y) == X becomes Y == 0.  X has no side-effects.  */
11023       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11024           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11025           && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11026         return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
11027                             build_int_cst (TREE_TYPE (arg1), 0));
11028
11029       /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
11030       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11031           && TREE_CODE (arg1) == INTEGER_CST
11032           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11033         return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11034                             fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
11035                                          TREE_OPERAND (arg0, 1), arg1));
11036
11037       /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
11038          (X & C) == 0 when C is a single bit.  */
11039       if (TREE_CODE (arg0) == BIT_AND_EXPR
11040           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
11041           && integer_zerop (arg1)
11042           && integer_pow2p (TREE_OPERAND (arg0, 1)))
11043         {
11044           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11045                              TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
11046                              TREE_OPERAND (arg0, 1));
11047           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
11048                               type, tem, arg1);
11049         }
11050
11051       /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
11052          constant C is a power of two, i.e. a single bit.  */
11053       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11054           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11055           && integer_zerop (arg1)
11056           && integer_pow2p (TREE_OPERAND (arg0, 1))
11057           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11058                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11059         {
11060           tree arg00 = TREE_OPERAND (arg0, 0);
11061           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11062                               arg00, build_int_cst (TREE_TYPE (arg00), 0));
11063         }
11064
11065       /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
11066          when is C is a power of two, i.e. a single bit.  */
11067       if (TREE_CODE (arg0) == BIT_AND_EXPR
11068           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
11069           && integer_zerop (arg1)
11070           && integer_pow2p (TREE_OPERAND (arg0, 1))
11071           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11072                               TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
11073         {
11074           tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11075           tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
11076                              arg000, TREE_OPERAND (arg0, 1));
11077           return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11078                               tem, build_int_cst (TREE_TYPE (tem), 0));
11079         }
11080
11081       if (integer_zerop (arg1)
11082           && tree_expr_nonzero_p (arg0))
11083         {
11084           tree res = constant_boolean_node (code==NE_EXPR, type);
11085           return omit_one_operand (type, res, arg0);
11086         }
11087
11088       /* Fold -X op -Y as X op Y, where op is eq/ne.  */
11089       if (TREE_CODE (arg0) == NEGATE_EXPR
11090           && TREE_CODE (arg1) == NEGATE_EXPR)
11091         return fold_build2 (code, type,
11092                             TREE_OPERAND (arg0, 0),
11093                             TREE_OPERAND (arg1, 0));
11094
11095       /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries.  */
11096       if (TREE_CODE (arg0) == BIT_AND_EXPR
11097           && TREE_CODE (arg1) == BIT_AND_EXPR)
11098         {
11099           tree arg00 = TREE_OPERAND (arg0, 0);
11100           tree arg01 = TREE_OPERAND (arg0, 1);
11101           tree arg10 = TREE_OPERAND (arg1, 0);
11102           tree arg11 = TREE_OPERAND (arg1, 1);
11103           tree itype = TREE_TYPE (arg0);
11104
11105           if (operand_equal_p (arg01, arg11, 0))
11106             return fold_build2 (code, type,
11107                                 fold_build2 (BIT_AND_EXPR, itype,
11108                                              fold_build2 (BIT_XOR_EXPR, itype,
11109                                                           arg00, arg10),
11110                                              arg01),
11111                                 build_int_cst (itype, 0));
11112
11113           if (operand_equal_p (arg01, arg10, 0))
11114             return fold_build2 (code, type,
11115                                 fold_build2 (BIT_AND_EXPR, itype,
11116                                              fold_build2 (BIT_XOR_EXPR, itype,
11117                                                           arg00, arg11),
11118                                              arg01),
11119                                 build_int_cst (itype, 0));
11120
11121           if (operand_equal_p (arg00, arg11, 0))
11122             return fold_build2 (code, type,
11123                                 fold_build2 (BIT_AND_EXPR, itype,
11124                                              fold_build2 (BIT_XOR_EXPR, itype,
11125                                                           arg01, arg10),
11126                                              arg00),
11127                                 build_int_cst (itype, 0));
11128
11129           if (operand_equal_p (arg00, arg10, 0))
11130             return fold_build2 (code, type,
11131                                 fold_build2 (BIT_AND_EXPR, itype,
11132                                              fold_build2 (BIT_XOR_EXPR, itype,
11133                                                           arg01, arg11),
11134                                              arg00),
11135                                 build_int_cst (itype, 0));
11136         }
11137
11138       if (TREE_CODE (arg0) == BIT_XOR_EXPR
11139           && TREE_CODE (arg1) == BIT_XOR_EXPR)
11140         {
11141           tree arg00 = TREE_OPERAND (arg0, 0);
11142           tree arg01 = TREE_OPERAND (arg0, 1);
11143           tree arg10 = TREE_OPERAND (arg1, 0);
11144           tree arg11 = TREE_OPERAND (arg1, 1);
11145           tree itype = TREE_TYPE (arg0);
11146
11147           /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
11148              operand_equal_p guarantees no side-effects so we don't need
11149              to use omit_one_operand on Z.  */
11150           if (operand_equal_p (arg01, arg11, 0))
11151             return fold_build2 (code, type, arg00, arg10);
11152           if (operand_equal_p (arg01, arg10, 0))
11153             return fold_build2 (code, type, arg00, arg11);
11154           if (operand_equal_p (arg00, arg11, 0))
11155             return fold_build2 (code, type, arg01, arg10);
11156           if (operand_equal_p (arg00, arg10, 0))
11157             return fold_build2 (code, type, arg01, arg11);
11158
11159           /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y.  */
11160           if (TREE_CODE (arg01) == INTEGER_CST
11161               && TREE_CODE (arg11) == INTEGER_CST)
11162             return fold_build2 (code, type,
11163                                 fold_build2 (BIT_XOR_EXPR, itype, arg00,
11164                                              fold_build2 (BIT_XOR_EXPR, itype,
11165                                                           arg01, arg11)),
11166                                 arg10);
11167         }
11168       return NULL_TREE;
11169
11170     case LT_EXPR:
11171     case GT_EXPR:
11172     case LE_EXPR:
11173     case GE_EXPR:
11174       tem = fold_comparison (code, type, op0, op1);
11175       if (tem != NULL_TREE)
11176         return tem;
11177
11178       /* Transform comparisons of the form X +- C CMP X.  */
11179       if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11180           && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11181           && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11182                && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
11183               || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11184                   && !TYPE_UNSIGNED (TREE_TYPE (arg1))
11185                   && !(flag_wrapv || flag_trapv))))
11186         {
11187           tree arg01 = TREE_OPERAND (arg0, 1);
11188           enum tree_code code0 = TREE_CODE (arg0);
11189           int is_positive;
11190
11191           if (TREE_CODE (arg01) == REAL_CST)
11192             is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11193           else
11194             is_positive = tree_int_cst_sgn (arg01);
11195
11196           /* (X - c) > X becomes false.  */
11197           if (code == GT_EXPR
11198               && ((code0 == MINUS_EXPR && is_positive >= 0)
11199                   || (code0 == PLUS_EXPR && is_positive <= 0)))
11200             return constant_boolean_node (0, type);
11201
11202           /* Likewise (X + c) < X becomes false.  */
11203           if (code == LT_EXPR
11204               && ((code0 == PLUS_EXPR && is_positive >= 0)
11205                   || (code0 == MINUS_EXPR && is_positive <= 0)))
11206             return constant_boolean_node (0, type);
11207
11208           /* Convert (X - c) <= X to true.  */
11209           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
11210               && code == LE_EXPR
11211               && ((code0 == MINUS_EXPR && is_positive >= 0)
11212                   || (code0 == PLUS_EXPR && is_positive <= 0)))
11213             return constant_boolean_node (1, type);
11214
11215           /* Convert (X + c) >= X to true.  */
11216           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
11217               && code == GE_EXPR
11218               && ((code0 == PLUS_EXPR && is_positive >= 0)
11219                   || (code0 == MINUS_EXPR && is_positive <= 0)))
11220             return constant_boolean_node (1, type);
11221
11222           if (TREE_CODE (arg01) == INTEGER_CST)
11223             {
11224               /* Convert X + c > X and X - c < X to true for integers.  */
11225               if (code == GT_EXPR
11226                   && ((code0 == PLUS_EXPR && is_positive > 0)
11227                       || (code0 == MINUS_EXPR && is_positive < 0)))
11228                 return constant_boolean_node (1, type);
11229
11230               if (code == LT_EXPR
11231                   && ((code0 == MINUS_EXPR && is_positive > 0)
11232                       || (code0 == PLUS_EXPR && is_positive < 0)))
11233                 return constant_boolean_node (1, type);
11234
11235               /* Convert X + c <= X and X - c >= X to false for integers.  */
11236               if (code == LE_EXPR
11237                   && ((code0 == PLUS_EXPR && is_positive > 0)
11238                       || (code0 == MINUS_EXPR && is_positive < 0)))
11239                 return constant_boolean_node (0, type);
11240
11241               if (code == GE_EXPR
11242                   && ((code0 == MINUS_EXPR && is_positive > 0)
11243                       || (code0 == PLUS_EXPR && is_positive < 0)))
11244                 return constant_boolean_node (0, type);
11245             }
11246         }
11247
11248       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
11249          This transformation affects the cases which are handled in later
11250          optimizations involving comparisons with non-negative constants.  */
11251       if (TREE_CODE (arg1) == INTEGER_CST
11252           && TREE_CODE (arg0) != INTEGER_CST
11253           && tree_int_cst_sgn (arg1) > 0)
11254         {
11255           if (code == GE_EXPR)
11256             {
11257               arg1 = const_binop (MINUS_EXPR, arg1,
11258                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
11259               return fold_build2 (GT_EXPR, type, arg0,
11260                                   fold_convert (TREE_TYPE (arg0), arg1));
11261             }
11262           if (code == LT_EXPR)
11263             {
11264               arg1 = const_binop (MINUS_EXPR, arg1,
11265                                   build_int_cst (TREE_TYPE (arg1), 1), 0);
11266               return fold_build2 (LE_EXPR, type, arg0,
11267                                   fold_convert (TREE_TYPE (arg0), arg1));
11268             }
11269         }
11270
11271       /* Comparisons with the highest or lowest possible integer of
11272          the specified precision will have known values.  */
11273       {
11274         tree arg1_type = TREE_TYPE (arg1);
11275         unsigned int width = TYPE_PRECISION (arg1_type);
11276
11277         if (TREE_CODE (arg1) == INTEGER_CST
11278             && ! TREE_CONSTANT_OVERFLOW (arg1)
11279             && width <= 2 * HOST_BITS_PER_WIDE_INT
11280             && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
11281           {
11282             HOST_WIDE_INT signed_max_hi;
11283             unsigned HOST_WIDE_INT signed_max_lo;
11284             unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
11285
11286             if (width <= HOST_BITS_PER_WIDE_INT)
11287               {
11288                 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
11289                                 - 1;
11290                 signed_max_hi = 0;
11291                 max_hi = 0;
11292
11293                 if (TYPE_UNSIGNED (arg1_type))
11294                   {
11295                     max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
11296                     min_lo = 0;
11297                     min_hi = 0;
11298                   }
11299                 else
11300                   {
11301                     max_lo = signed_max_lo;
11302                     min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
11303                     min_hi = -1;
11304                   }
11305               }
11306             else
11307               {
11308                 width -= HOST_BITS_PER_WIDE_INT;
11309                 signed_max_lo = -1;
11310                 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
11311                                 - 1;
11312                 max_lo = -1;
11313                 min_lo = 0;
11314
11315                 if (TYPE_UNSIGNED (arg1_type))
11316                   {
11317                     max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
11318                     min_hi = 0;
11319                   }
11320                 else
11321                   {
11322                     max_hi = signed_max_hi;
11323                     min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
11324                   }
11325               }
11326
11327             if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
11328                 && TREE_INT_CST_LOW (arg1) == max_lo)
11329               switch (code)
11330                 {
11331                 case GT_EXPR:
11332                   return omit_one_operand (type, integer_zero_node, arg0);
11333
11334                 case GE_EXPR:
11335                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
11336
11337                 case LE_EXPR:
11338                   return omit_one_operand (type, integer_one_node, arg0);
11339
11340                 case LT_EXPR:
11341                   return fold_build2 (NE_EXPR, type, arg0, arg1);
11342
11343                 /* The GE_EXPR and LT_EXPR cases above are not normally
11344                    reached because of previous transformations.  */
11345
11346                 default:
11347                   break;
11348                 }
11349             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
11350                      == max_hi
11351                      && TREE_INT_CST_LOW (arg1) == max_lo - 1)
11352               switch (code)
11353                 {
11354                 case GT_EXPR:
11355                   arg1 = const_binop (PLUS_EXPR, arg1,
11356                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
11357                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
11358                 case LE_EXPR:
11359                   arg1 = const_binop (PLUS_EXPR, arg1,
11360                                       build_int_cst (TREE_TYPE (arg1), 1), 0);
11361                   return fold_build2 (NE_EXPR, type, arg0, arg1);
11362                 default:
11363                   break;
11364                 }
11365             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
11366                      == min_hi
11367                      && TREE_INT_CST_LOW (arg1) == min_lo)
11368               switch (code)
11369                 {
11370                 case LT_EXPR:
11371                   return omit_one_operand (type, integer_zero_node, arg0);
11372
11373                 case LE_EXPR:
11374                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
11375
11376                 case GE_EXPR:
11377                   return omit_one_operand (type, integer_one_node, arg0);
11378
11379                 case GT_EXPR:
11380                   return fold_build2 (NE_EXPR, type, op0, op1);
11381
11382                 default:
11383                   break;
11384                 }
11385             else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
11386                      == min_hi
11387                      && TREE_INT_CST_LOW (arg1) == min_lo + 1)
11388               switch (code)
11389                 {
11390                 case GE_EXPR:
11391                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
11392                   return fold_build2 (NE_EXPR, type, arg0, arg1);
11393                 case LT_EXPR:
11394                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
11395                   return fold_build2 (EQ_EXPR, type, arg0, arg1);
11396                 default:
11397                   break;
11398                 }
11399
11400             else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
11401                      && TREE_INT_CST_LOW (arg1) == signed_max_lo
11402                      && TYPE_UNSIGNED (arg1_type)
11403                      /* We will flip the signedness of the comparison operator
11404                         associated with the mode of arg1, so the sign bit is
11405                         specified by this mode.  Check that arg1 is the signed
11406                         max associated with this sign bit.  */
11407                      && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
11408                      /* signed_type does not work on pointer types.  */
11409                      && INTEGRAL_TYPE_P (arg1_type))
11410               {
11411                 /* The following case also applies to X < signed_max+1
11412                    and X >= signed_max+1 because previous transformations.  */
11413                 if (code == LE_EXPR || code == GT_EXPR)
11414                   {
11415                     tree st0, st1;
11416                     st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
11417                     st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
11418                     return fold_build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
11419                                         type, fold_convert (st0, arg0),
11420                                         build_int_cst (st1, 0));
11421                   }
11422               }
11423           }
11424       }
11425
11426       /* If we are comparing an ABS_EXPR with a constant, we can
11427          convert all the cases into explicit comparisons, but they may
11428          well not be faster than doing the ABS and one comparison.
11429          But ABS (X) <= C is a range comparison, which becomes a subtraction
11430          and a comparison, and is probably faster.  */
11431       if (code == LE_EXPR
11432           && TREE_CODE (arg1) == INTEGER_CST
11433           && TREE_CODE (arg0) == ABS_EXPR
11434           && ! TREE_SIDE_EFFECTS (arg0)
11435           && (0 != (tem = negate_expr (arg1)))
11436           && TREE_CODE (tem) == INTEGER_CST
11437           && ! TREE_CONSTANT_OVERFLOW (tem))
11438         return fold_build2 (TRUTH_ANDIF_EXPR, type,
11439                             build2 (GE_EXPR, type,
11440                                     TREE_OPERAND (arg0, 0), tem),
11441                             build2 (LE_EXPR, type,
11442                                     TREE_OPERAND (arg0, 0), arg1));
11443
11444       /* Convert ABS_EXPR<x> >= 0 to true.  */
11445       if (code == GE_EXPR
11446           && tree_expr_nonnegative_p (arg0)
11447           && (integer_zerop (arg1)
11448               || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11449                   && real_zerop (arg1))))
11450         return omit_one_operand (type, integer_one_node, arg0);
11451
11452       /* Convert ABS_EXPR<x> < 0 to false.  */
11453       if (code == LT_EXPR
11454           && tree_expr_nonnegative_p (arg0)
11455           && (integer_zerop (arg1) || real_zerop (arg1)))
11456         return omit_one_operand (type, integer_zero_node, arg0);
11457
11458       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11459          and similarly for >= into !=.  */
11460       if ((code == LT_EXPR || code == GE_EXPR)
11461           && TYPE_UNSIGNED (TREE_TYPE (arg0))
11462           && TREE_CODE (arg1) == LSHIFT_EXPR
11463           && integer_onep (TREE_OPERAND (arg1, 0)))
11464         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11465                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11466                                TREE_OPERAND (arg1, 1)),
11467                        build_int_cst (TREE_TYPE (arg0), 0));
11468
11469       if ((code == LT_EXPR || code == GE_EXPR)
11470           && TYPE_UNSIGNED (TREE_TYPE (arg0))
11471           && (TREE_CODE (arg1) == NOP_EXPR
11472               || TREE_CODE (arg1) == CONVERT_EXPR)
11473           && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11474           && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11475         return
11476           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11477                   fold_convert (TREE_TYPE (arg0),
11478                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11479                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
11480                                                       1))),
11481                   build_int_cst (TREE_TYPE (arg0), 0));
11482
11483       return NULL_TREE;
11484
11485     case UNORDERED_EXPR:
11486     case ORDERED_EXPR:
11487     case UNLT_EXPR:
11488     case UNLE_EXPR:
11489     case UNGT_EXPR:
11490     case UNGE_EXPR:
11491     case UNEQ_EXPR:
11492     case LTGT_EXPR:
11493       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
11494         {
11495           t1 = fold_relational_const (code, type, arg0, arg1);
11496           if (t1 != NULL_TREE)
11497             return t1;
11498         }
11499
11500       /* If the first operand is NaN, the result is constant.  */
11501       if (TREE_CODE (arg0) == REAL_CST
11502           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
11503           && (code != LTGT_EXPR || ! flag_trapping_math))
11504         {
11505           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
11506                ? integer_zero_node
11507                : integer_one_node;
11508           return omit_one_operand (type, t1, arg1);
11509         }
11510
11511       /* If the second operand is NaN, the result is constant.  */
11512       if (TREE_CODE (arg1) == REAL_CST
11513           && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
11514           && (code != LTGT_EXPR || ! flag_trapping_math))
11515         {
11516           t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
11517                ? integer_zero_node
11518                : integer_one_node;
11519           return omit_one_operand (type, t1, arg0);
11520         }
11521
11522       /* Simplify unordered comparison of something with itself.  */
11523       if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
11524           && operand_equal_p (arg0, arg1, 0))
11525         return constant_boolean_node (1, type);
11526
11527       if (code == LTGT_EXPR
11528           && !flag_trapping_math
11529           && operand_equal_p (arg0, arg1, 0))
11530         return constant_boolean_node (0, type);
11531
11532       /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
11533       {
11534         tree targ0 = strip_float_extensions (arg0);
11535         tree targ1 = strip_float_extensions (arg1);
11536         tree newtype = TREE_TYPE (targ0);
11537
11538         if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11539           newtype = TREE_TYPE (targ1);
11540
11541         if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11542           return fold_build2 (code, type, fold_convert (newtype, targ0),
11543                               fold_convert (newtype, targ1));
11544       }
11545
11546       return NULL_TREE;
11547
11548     case COMPOUND_EXPR:
11549       /* When pedantic, a compound expression can be neither an lvalue
11550          nor an integer constant expression.  */
11551       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11552         return NULL_TREE;
11553       /* Don't let (0, 0) be null pointer constant.  */
11554       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11555                                  : fold_convert (type, arg1);
11556       return pedantic_non_lvalue (tem);
11557
11558     case COMPLEX_EXPR:
11559       if ((TREE_CODE (arg0) == REAL_CST
11560            && TREE_CODE (arg1) == REAL_CST)
11561           || (TREE_CODE (arg0) == INTEGER_CST
11562               && TREE_CODE (arg1) == INTEGER_CST))
11563         return build_complex (type, arg0, arg1);
11564       return NULL_TREE;
11565
11566     case ASSERT_EXPR:
11567       /* An ASSERT_EXPR should never be passed to fold_binary.  */
11568       gcc_unreachable ();
11569
11570     default:
11571       return NULL_TREE;
11572     } /* switch (code) */
11573 }
11574
11575 /* Callback for walk_tree, looking for LABEL_EXPR.
11576    Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
11577    Do not check the sub-tree of GOTO_EXPR.  */
11578
11579 static tree
11580 contains_label_1 (tree *tp,
11581                   int *walk_subtrees,
11582                   void *data ATTRIBUTE_UNUSED)
11583 {
11584   switch (TREE_CODE (*tp))
11585     {
11586     case LABEL_EXPR:
11587       return *tp;
11588     case GOTO_EXPR:
11589       *walk_subtrees = 0;
11590     /* no break */
11591     default:
11592       return NULL_TREE;
11593     }
11594 }
11595
11596 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
11597    accessible from outside the sub-tree. Returns NULL_TREE if no
11598    addressable label is found.  */
11599
11600 static bool
11601 contains_label_p (tree st)
11602 {
11603   return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
11604 }
11605
11606 /* Fold a ternary expression of code CODE and type TYPE with operands
11607    OP0, OP1, and OP2.  Return the folded expression if folding is
11608    successful.  Otherwise, return NULL_TREE.  */
11609
11610 tree
11611 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
11612 {
11613   tree tem;
11614   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
11615   enum tree_code_class kind = TREE_CODE_CLASS (code);
11616
11617   gcc_assert (IS_EXPR_CODE_CLASS (kind)
11618               && TREE_CODE_LENGTH (code) == 3);
11619
11620   /* Strip any conversions that don't change the mode.  This is safe
11621      for every expression, except for a comparison expression because
11622      its signedness is derived from its operands.  So, in the latter
11623      case, only strip conversions that don't change the signedness.
11624
11625      Note that this is done as an internal manipulation within the
11626      constant folder, in order to find the simplest representation of
11627      the arguments so that their form can be studied.  In any cases,
11628      the appropriate type conversions should be put back in the tree
11629      that will get out of the constant folder.  */
11630   if (op0)
11631     {
11632       arg0 = op0;
11633       STRIP_NOPS (arg0);
11634     }
11635
11636   if (op1)
11637     {
11638       arg1 = op1;
11639       STRIP_NOPS (arg1);
11640     }
11641
11642   switch (code)
11643     {
11644     case COMPONENT_REF:
11645       if (TREE_CODE (arg0) == CONSTRUCTOR
11646           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11647         {
11648           unsigned HOST_WIDE_INT idx;
11649           tree field, value;
11650           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11651             if (field == arg1)
11652               return value;
11653         }
11654       return NULL_TREE;
11655
11656     case COND_EXPR:
11657       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11658          so all simple results must be passed through pedantic_non_lvalue.  */
11659       if (TREE_CODE (arg0) == INTEGER_CST)
11660         {
11661           tree unused_op = integer_zerop (arg0) ? op1 : op2;
11662           tem = integer_zerop (arg0) ? op2 : op1;
11663           /* Only optimize constant conditions when the selected branch
11664              has the same type as the COND_EXPR.  This avoids optimizing
11665              away "c ? x : throw", where the throw has a void type.
11666              Avoid throwing away that operand which contains label.  */
11667           if ((!TREE_SIDE_EFFECTS (unused_op)
11668                || !contains_label_p (unused_op))
11669               && (! VOID_TYPE_P (TREE_TYPE (tem))
11670                   || VOID_TYPE_P (type)))
11671             return pedantic_non_lvalue (tem);
11672           return NULL_TREE;
11673         }
11674       if (operand_equal_p (arg1, op2, 0))
11675         return pedantic_omit_one_operand (type, arg1, arg0);
11676
11677       /* If we have A op B ? A : C, we may be able to convert this to a
11678          simpler expression, depending on the operation and the values
11679          of B and C.  Signed zeros prevent all of these transformations,
11680          for reasons given above each one.
11681
11682          Also try swapping the arguments and inverting the conditional.  */
11683       if (COMPARISON_CLASS_P (arg0)
11684           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11685                                              arg1, TREE_OPERAND (arg0, 1))
11686           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
11687         {
11688           tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
11689           if (tem)
11690             return tem;
11691         }
11692
11693       if (COMPARISON_CLASS_P (arg0)
11694           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11695                                              op2,
11696                                              TREE_OPERAND (arg0, 1))
11697           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
11698         {
11699           tem = fold_truth_not_expr (arg0);
11700           if (tem && COMPARISON_CLASS_P (tem))
11701             {
11702               tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
11703               if (tem)
11704                 return tem;
11705             }
11706         }
11707
11708       /* If the second operand is simpler than the third, swap them
11709          since that produces better jump optimization results.  */
11710       if (truth_value_p (TREE_CODE (arg0))
11711           && tree_swap_operands_p (op1, op2, false))
11712         {
11713           /* See if this can be inverted.  If it can't, possibly because
11714              it was a floating-point inequality comparison, don't do
11715              anything.  */
11716           tem = fold_truth_not_expr (arg0);
11717           if (tem)
11718             return fold_build3 (code, type, tem, op2, op1);
11719         }
11720
11721       /* Convert A ? 1 : 0 to simply A.  */
11722       if (integer_onep (op1)
11723           && integer_zerop (op2)
11724           /* If we try to convert OP0 to our type, the
11725              call to fold will try to move the conversion inside
11726              a COND, which will recurse.  In that case, the COND_EXPR
11727              is probably the best choice, so leave it alone.  */
11728           && type == TREE_TYPE (arg0))
11729         return pedantic_non_lvalue (arg0);
11730
11731       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
11732          over COND_EXPR in cases such as floating point comparisons.  */
11733       if (integer_zerop (op1)
11734           && integer_onep (op2)
11735           && truth_value_p (TREE_CODE (arg0)))
11736         return pedantic_non_lvalue (fold_convert (type,
11737                                                   invert_truthvalue (arg0)));
11738
11739       /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>).  */
11740       if (TREE_CODE (arg0) == LT_EXPR
11741           && integer_zerop (TREE_OPERAND (arg0, 1))
11742           && integer_zerop (op2)
11743           && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11744         {
11745           /* sign_bit_p only checks ARG1 bits within A's precision.
11746              If <sign bit of A> has wider type than A, bits outside
11747              of A's precision in <sign bit of A> need to be checked.
11748              If they are all 0, this optimization needs to be done
11749              in unsigned A's type, if they are all 1 in signed A's type,
11750              otherwise this can't be done.  */
11751           if (TYPE_PRECISION (TREE_TYPE (tem))
11752               < TYPE_PRECISION (TREE_TYPE (arg1))
11753               && TYPE_PRECISION (TREE_TYPE (tem))
11754                  < TYPE_PRECISION (type))
11755             {
11756               unsigned HOST_WIDE_INT mask_lo;
11757               HOST_WIDE_INT mask_hi;
11758               int inner_width, outer_width;
11759               tree tem_type;
11760
11761               inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11762               outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11763               if (outer_width > TYPE_PRECISION (type))
11764                 outer_width = TYPE_PRECISION (type);
11765
11766               if (outer_width > HOST_BITS_PER_WIDE_INT)
11767                 {
11768                   mask_hi = ((unsigned HOST_WIDE_INT) -1
11769                              >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
11770                   mask_lo = -1;
11771                 }
11772               else
11773                 {
11774                   mask_hi = 0;
11775                   mask_lo = ((unsigned HOST_WIDE_INT) -1
11776                              >> (HOST_BITS_PER_WIDE_INT - outer_width));
11777                 }
11778               if (inner_width > HOST_BITS_PER_WIDE_INT)
11779                 {
11780                   mask_hi &= ~((unsigned HOST_WIDE_INT) -1
11781                                >> (HOST_BITS_PER_WIDE_INT - inner_width));
11782                   mask_lo = 0;
11783                 }
11784               else
11785                 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
11786                              >> (HOST_BITS_PER_WIDE_INT - inner_width));
11787
11788               if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
11789                   && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
11790                 {
11791                   tem_type = lang_hooks.types.signed_type (TREE_TYPE (tem));
11792                   tem = fold_convert (tem_type, tem);
11793                 }
11794               else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
11795                        && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
11796                 {
11797                   tem_type = lang_hooks.types.unsigned_type (TREE_TYPE (tem));
11798                   tem = fold_convert (tem_type, tem);
11799                 }
11800               else
11801                 tem = NULL;
11802             }
11803
11804           if (tem)
11805             return fold_convert (type,
11806                                  fold_build2 (BIT_AND_EXPR,
11807                                               TREE_TYPE (tem), tem,
11808                                               fold_convert (TREE_TYPE (tem),
11809                                                             arg1)));
11810         }
11811
11812       /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N).  A & 1 was
11813          already handled above.  */
11814       if (TREE_CODE (arg0) == BIT_AND_EXPR
11815           && integer_onep (TREE_OPERAND (arg0, 1))
11816           && integer_zerop (op2)
11817           && integer_pow2p (arg1))
11818         {
11819           tree tem = TREE_OPERAND (arg0, 0);
11820           STRIP_NOPS (tem);
11821           if (TREE_CODE (tem) == RSHIFT_EXPR
11822               && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
11823               && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11824                  TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
11825             return fold_build2 (BIT_AND_EXPR, type,
11826                                 TREE_OPERAND (tem, 0), arg1);
11827         }
11828
11829       /* A & N ? N : 0 is simply A & N if N is a power of two.  This
11830          is probably obsolete because the first operand should be a
11831          truth value (that's why we have the two cases above), but let's
11832          leave it in until we can confirm this for all front-ends.  */
11833       if (integer_zerop (op2)
11834           && TREE_CODE (arg0) == NE_EXPR
11835           && integer_zerop (TREE_OPERAND (arg0, 1))
11836           && integer_pow2p (arg1)
11837           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11838           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11839                               arg1, OEP_ONLY_CONST))
11840         return pedantic_non_lvalue (fold_convert (type,
11841                                                   TREE_OPERAND (arg0, 0)));
11842
11843       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
11844       if (integer_zerop (op2)
11845           && truth_value_p (TREE_CODE (arg0))
11846           && truth_value_p (TREE_CODE (arg1)))
11847         return fold_build2 (TRUTH_ANDIF_EXPR, type,
11848                             fold_convert (type, arg0),
11849                             arg1);
11850
11851       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
11852       if (integer_onep (op2)
11853           && truth_value_p (TREE_CODE (arg0))
11854           && truth_value_p (TREE_CODE (arg1)))
11855         {
11856           /* Only perform transformation if ARG0 is easily inverted.  */
11857           tem = fold_truth_not_expr (arg0);
11858           if (tem)
11859             return fold_build2 (TRUTH_ORIF_EXPR, type,
11860                                 fold_convert (type, tem),
11861                                 arg1);
11862         }
11863
11864       /* Convert A ? 0 : B into !A && B if A and B are truth values.  */
11865       if (integer_zerop (arg1)
11866           && truth_value_p (TREE_CODE (arg0))
11867           && truth_value_p (TREE_CODE (op2)))
11868         {
11869           /* Only perform transformation if ARG0 is easily inverted.  */
11870           tem = fold_truth_not_expr (arg0);
11871           if (tem)
11872             return fold_build2 (TRUTH_ANDIF_EXPR, type,
11873                                 fold_convert (type, tem),
11874                                 op2);
11875         }
11876
11877       /* Convert A ? 1 : B into A || B if A and B are truth values.  */
11878       if (integer_onep (arg1)
11879           && truth_value_p (TREE_CODE (arg0))
11880           && truth_value_p (TREE_CODE (op2)))
11881         return fold_build2 (TRUTH_ORIF_EXPR, type,
11882                             fold_convert (type, arg0),
11883                             op2);
11884
11885       return NULL_TREE;
11886
11887     case CALL_EXPR:
11888       /* Check for a built-in function.  */
11889       if (TREE_CODE (op0) == ADDR_EXPR
11890           && TREE_CODE (TREE_OPERAND (op0, 0)) == FUNCTION_DECL
11891           && DECL_BUILT_IN (TREE_OPERAND (op0, 0)))
11892         return fold_builtin (TREE_OPERAND (op0, 0), op1, false);
11893       return NULL_TREE;
11894
11895     case BIT_FIELD_REF:
11896       if (TREE_CODE (arg0) == VECTOR_CST
11897           && type == TREE_TYPE (TREE_TYPE (arg0))
11898           && host_integerp (arg1, 1)
11899           && host_integerp (op2, 1))
11900         {
11901           unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
11902           unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
11903
11904           if (width != 0
11905               && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
11906               && (idx % width) == 0
11907               && (idx = idx / width)
11908                  < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11909             {
11910               tree elements = TREE_VECTOR_CST_ELTS (arg0);
11911               while (idx-- > 0 && elements)
11912                 elements = TREE_CHAIN (elements);
11913               if (elements)
11914                 return TREE_VALUE (elements);
11915               else
11916                 return fold_convert (type, integer_zero_node);
11917             }
11918         }
11919       return NULL_TREE;
11920
11921     default:
11922       return NULL_TREE;
11923     } /* switch (code) */
11924 }
11925
11926 /* Perform constant folding and related simplification of EXPR.
11927    The related simplifications include x*1 => x, x*0 => 0, etc.,
11928    and application of the associative law.
11929    NOP_EXPR conversions may be removed freely (as long as we
11930    are careful not to change the type of the overall expression).
11931    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11932    but we can constant-fold them if they have constant operands.  */
11933
11934 #ifdef ENABLE_FOLD_CHECKING
11935 # define fold(x) fold_1 (x)
11936 static tree fold_1 (tree);
11937 static
11938 #endif
11939 tree
11940 fold (tree expr)
11941 {
11942   const tree t = expr;
11943   enum tree_code code = TREE_CODE (t);
11944   enum tree_code_class kind = TREE_CODE_CLASS (code);
11945   tree tem;
11946
11947   /* Return right away if a constant.  */
11948   if (kind == tcc_constant)
11949     return t;
11950
11951   if (IS_EXPR_CODE_CLASS (kind)
11952       || IS_GIMPLE_STMT_CODE_CLASS (kind))
11953     {
11954       tree type = TREE_TYPE (t);
11955       tree op0, op1, op2;
11956
11957       switch (TREE_CODE_LENGTH (code))
11958         {
11959         case 1:
11960           op0 = TREE_OPERAND (t, 0);
11961           tem = fold_unary (code, type, op0);
11962           return tem ? tem : expr;
11963         case 2:
11964           op0 = TREE_OPERAND (t, 0);
11965           op1 = TREE_OPERAND (t, 1);
11966           tem = fold_binary (code, type, op0, op1);
11967           return tem ? tem : expr;
11968         case 3:
11969           op0 = TREE_OPERAND (t, 0);
11970           op1 = TREE_OPERAND (t, 1);
11971           op2 = TREE_OPERAND (t, 2);
11972           tem = fold_ternary (code, type, op0, op1, op2);
11973           return tem ? tem : expr;
11974         default:
11975           break;
11976         }
11977     }
11978
11979   switch (code)
11980     {
11981     case CONST_DECL:
11982       return fold (DECL_INITIAL (t));
11983
11984     default:
11985       return t;
11986     } /* switch (code) */
11987 }
11988
11989 #ifdef ENABLE_FOLD_CHECKING
11990 #undef fold
11991
11992 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
11993 static void fold_check_failed (tree, tree);
11994 void print_fold_checksum (tree);
11995
11996 /* When --enable-checking=fold, compute a digest of expr before
11997    and after actual fold call to see if fold did not accidentally
11998    change original expr.  */
11999
12000 tree
12001 fold (tree expr)
12002 {
12003   tree ret;
12004   struct md5_ctx ctx;
12005   unsigned char checksum_before[16], checksum_after[16];
12006   htab_t ht;
12007
12008   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12009   md5_init_ctx (&ctx);
12010   fold_checksum_tree (expr, &ctx, ht);
12011   md5_finish_ctx (&ctx, checksum_before);
12012   htab_empty (ht);
12013
12014   ret = fold_1 (expr);
12015
12016   md5_init_ctx (&ctx);
12017   fold_checksum_tree (expr, &ctx, ht);
12018   md5_finish_ctx (&ctx, checksum_after);
12019   htab_delete (ht);
12020
12021   if (memcmp (checksum_before, checksum_after, 16))
12022     fold_check_failed (expr, ret);
12023
12024   return ret;
12025 }
12026
12027 void
12028 print_fold_checksum (tree expr)
12029 {
12030   struct md5_ctx ctx;
12031   unsigned char checksum[16], cnt;
12032   htab_t ht;
12033
12034   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12035   md5_init_ctx (&ctx);
12036   fold_checksum_tree (expr, &ctx, ht);
12037   md5_finish_ctx (&ctx, checksum);
12038   htab_delete (ht);
12039   for (cnt = 0; cnt < 16; ++cnt)
12040     fprintf (stderr, "%02x", checksum[cnt]);
12041   putc ('\n', stderr);
12042 }
12043
12044 static void
12045 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
12046 {
12047   internal_error ("fold check: original tree changed by fold");
12048 }
12049
12050 static void
12051 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
12052 {
12053   void **slot;
12054   enum tree_code code;
12055   struct tree_function_decl buf;
12056   int i, len;
12057   
12058 recursive_label:
12059
12060   gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
12061                <= sizeof (struct tree_function_decl))
12062               && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
12063   if (expr == NULL)
12064     return;
12065   slot = htab_find_slot (ht, expr, INSERT);
12066   if (*slot != NULL)
12067     return;
12068   *slot = expr;
12069   code = TREE_CODE (expr);
12070   if (TREE_CODE_CLASS (code) == tcc_declaration
12071       && DECL_ASSEMBLER_NAME_SET_P (expr))
12072     {
12073       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
12074       memcpy ((char *) &buf, expr, tree_size (expr));
12075       expr = (tree) &buf;
12076       SET_DECL_ASSEMBLER_NAME (expr, NULL);
12077     }
12078   else if (TREE_CODE_CLASS (code) == tcc_type
12079            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
12080                || TYPE_CACHED_VALUES_P (expr)
12081                || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
12082     {
12083       /* Allow these fields to be modified.  */
12084       memcpy ((char *) &buf, expr, tree_size (expr));
12085       expr = (tree) &buf;
12086       TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) = 0;
12087       TYPE_POINTER_TO (expr) = NULL;
12088       TYPE_REFERENCE_TO (expr) = NULL;
12089       if (TYPE_CACHED_VALUES_P (expr))
12090         {
12091           TYPE_CACHED_VALUES_P (expr) = 0;
12092           TYPE_CACHED_VALUES (expr) = NULL;
12093         }
12094     }
12095   md5_process_bytes (expr, tree_size (expr), ctx);
12096   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12097   if (TREE_CODE_CLASS (code) != tcc_type
12098       && TREE_CODE_CLASS (code) != tcc_declaration
12099       && code != TREE_LIST)
12100     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12101   switch (TREE_CODE_CLASS (code))
12102     {
12103     case tcc_constant:
12104       switch (code)
12105         {
12106         case STRING_CST:
12107           md5_process_bytes (TREE_STRING_POINTER (expr),
12108                              TREE_STRING_LENGTH (expr), ctx);
12109           break;
12110         case COMPLEX_CST:
12111           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12112           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12113           break;
12114         case VECTOR_CST:
12115           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
12116           break;
12117         default:
12118           break;
12119         }
12120       break;
12121     case tcc_exceptional:
12122       switch (code)
12123         {
12124         case TREE_LIST:
12125           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12126           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12127           expr = TREE_CHAIN (expr);
12128           goto recursive_label;
12129           break;
12130         case TREE_VEC:
12131           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12132             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12133           break;
12134         default:
12135           break;
12136         }
12137       break;
12138     case tcc_expression:
12139     case tcc_reference:
12140     case tcc_comparison:
12141     case tcc_unary:
12142     case tcc_binary:
12143     case tcc_statement:
12144       len = TREE_CODE_LENGTH (code);
12145       for (i = 0; i < len; ++i)
12146         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12147       break;
12148     case tcc_declaration:
12149       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12150       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12151       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12152         {
12153           fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12154           fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12155           fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12156           fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12157           fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12158         }
12159       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
12160         fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
12161           
12162       if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12163         {
12164           fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12165           fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12166           fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
12167         }
12168       break;
12169     case tcc_type:
12170       if (TREE_CODE (expr) == ENUMERAL_TYPE)
12171         fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12172       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12173       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12174       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12175       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12176       if (INTEGRAL_TYPE_P (expr)
12177           || SCALAR_FLOAT_TYPE_P (expr))
12178         {
12179           fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12180           fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12181         }
12182       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12183       if (TREE_CODE (expr) == RECORD_TYPE
12184           || TREE_CODE (expr) == UNION_TYPE
12185           || TREE_CODE (expr) == QUAL_UNION_TYPE)
12186         fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12187       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12188       break;
12189     default:
12190       break;
12191     }
12192 }
12193
12194 #endif
12195
12196 /* Fold a unary tree expression with code CODE of type TYPE with an
12197    operand OP0.  Return a folded expression if successful.  Otherwise,
12198    return a tree expression with code CODE of type TYPE with an
12199    operand OP0.  */
12200
12201 tree
12202 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12203 {
12204   tree tem;
12205 #ifdef ENABLE_FOLD_CHECKING
12206   unsigned char checksum_before[16], checksum_after[16];
12207   struct md5_ctx ctx;
12208   htab_t ht;
12209
12210   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12211   md5_init_ctx (&ctx);
12212   fold_checksum_tree (op0, &ctx, ht);
12213   md5_finish_ctx (&ctx, checksum_before);
12214   htab_empty (ht);
12215 #endif
12216   
12217   tem = fold_unary (code, type, op0);
12218   if (!tem)
12219     tem = build1_stat (code, type, op0 PASS_MEM_STAT);
12220   
12221 #ifdef ENABLE_FOLD_CHECKING
12222   md5_init_ctx (&ctx);
12223   fold_checksum_tree (op0, &ctx, ht);
12224   md5_finish_ctx (&ctx, checksum_after);
12225   htab_delete (ht);
12226
12227   if (memcmp (checksum_before, checksum_after, 16))
12228     fold_check_failed (op0, tem);
12229 #endif
12230   return tem;
12231 }
12232
12233 /* Fold a binary tree expression with code CODE of type TYPE with
12234    operands OP0 and OP1.  Return a folded expression if successful.
12235    Otherwise, return a tree expression with code CODE of type TYPE
12236    with operands OP0 and OP1.  */
12237
12238 tree
12239 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
12240                   MEM_STAT_DECL)
12241 {
12242   tree tem;
12243 #ifdef ENABLE_FOLD_CHECKING
12244   unsigned char checksum_before_op0[16],
12245                 checksum_before_op1[16],
12246                 checksum_after_op0[16],
12247                 checksum_after_op1[16];
12248   struct md5_ctx ctx;
12249   htab_t ht;
12250
12251   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12252   md5_init_ctx (&ctx);
12253   fold_checksum_tree (op0, &ctx, ht);
12254   md5_finish_ctx (&ctx, checksum_before_op0);
12255   htab_empty (ht);
12256
12257   md5_init_ctx (&ctx);
12258   fold_checksum_tree (op1, &ctx, ht);
12259   md5_finish_ctx (&ctx, checksum_before_op1);
12260   htab_empty (ht);
12261 #endif
12262
12263   tem = fold_binary (code, type, op0, op1);
12264   if (!tem)
12265     tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
12266   
12267 #ifdef ENABLE_FOLD_CHECKING
12268   md5_init_ctx (&ctx);
12269   fold_checksum_tree (op0, &ctx, ht);
12270   md5_finish_ctx (&ctx, checksum_after_op0);
12271   htab_empty (ht);
12272
12273   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12274     fold_check_failed (op0, tem);
12275   
12276   md5_init_ctx (&ctx);
12277   fold_checksum_tree (op1, &ctx, ht);
12278   md5_finish_ctx (&ctx, checksum_after_op1);
12279   htab_delete (ht);
12280
12281   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12282     fold_check_failed (op1, tem);
12283 #endif
12284   return tem;
12285 }
12286
12287 /* Fold a ternary tree expression with code CODE of type TYPE with
12288    operands OP0, OP1, and OP2.  Return a folded expression if
12289    successful.  Otherwise, return a tree expression with code CODE of
12290    type TYPE with operands OP0, OP1, and OP2.  */
12291
12292 tree
12293 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
12294              MEM_STAT_DECL)
12295 {
12296   tree tem;
12297 #ifdef ENABLE_FOLD_CHECKING
12298   unsigned char checksum_before_op0[16],
12299                 checksum_before_op1[16],
12300                 checksum_before_op2[16],
12301                 checksum_after_op0[16],
12302                 checksum_after_op1[16],
12303                 checksum_after_op2[16];
12304   struct md5_ctx ctx;
12305   htab_t ht;
12306
12307   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
12308   md5_init_ctx (&ctx);
12309   fold_checksum_tree (op0, &ctx, ht);
12310   md5_finish_ctx (&ctx, checksum_before_op0);
12311   htab_empty (ht);
12312
12313   md5_init_ctx (&ctx);
12314   fold_checksum_tree (op1, &ctx, ht);
12315   md5_finish_ctx (&ctx, checksum_before_op1);
12316   htab_empty (ht);
12317
12318   md5_init_ctx (&ctx);
12319   fold_checksum_tree (op2, &ctx, ht);
12320   md5_finish_ctx (&ctx, checksum_before_op2);
12321   htab_empty (ht);
12322 #endif
12323   
12324   tem = fold_ternary (code, type, op0, op1, op2);
12325   if (!tem)
12326     tem =  build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
12327       
12328 #ifdef ENABLE_FOLD_CHECKING
12329   md5_init_ctx (&ctx);
12330   fold_checksum_tree (op0, &ctx, ht);
12331   md5_finish_ctx (&ctx, checksum_after_op0);
12332   htab_empty (ht);
12333
12334   if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12335     fold_check_failed (op0, tem);
12336   
12337   md5_init_ctx (&ctx);
12338   fold_checksum_tree (op1, &ctx, ht);
12339   md5_finish_ctx (&ctx, checksum_after_op1);
12340   htab_empty (ht);
12341
12342   if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12343     fold_check_failed (op1, tem);
12344   
12345   md5_init_ctx (&ctx);
12346   fold_checksum_tree (op2, &ctx, ht);
12347   md5_finish_ctx (&ctx, checksum_after_op2);
12348   htab_delete (ht);
12349
12350   if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12351     fold_check_failed (op2, tem);
12352 #endif
12353   return tem;
12354 }
12355
12356 /* Perform constant folding and related simplification of initializer
12357    expression EXPR.  These behave identically to "fold_buildN" but ignore
12358    potential run-time traps and exceptions that fold must preserve.  */
12359
12360 #define START_FOLD_INIT \
12361   int saved_signaling_nans = flag_signaling_nans;\
12362   int saved_trapping_math = flag_trapping_math;\
12363   int saved_rounding_math = flag_rounding_math;\
12364   int saved_trapv = flag_trapv;\
12365   int saved_folding_initializer = folding_initializer;\
12366   flag_signaling_nans = 0;\
12367   flag_trapping_math = 0;\
12368   flag_rounding_math = 0;\
12369   flag_trapv = 0;\
12370   folding_initializer = 1;
12371
12372 #define END_FOLD_INIT \
12373   flag_signaling_nans = saved_signaling_nans;\
12374   flag_trapping_math = saved_trapping_math;\
12375   flag_rounding_math = saved_rounding_math;\
12376   flag_trapv = saved_trapv;\
12377   folding_initializer = saved_folding_initializer;
12378
12379 tree
12380 fold_build1_initializer (enum tree_code code, tree type, tree op)
12381 {
12382   tree result;
12383   START_FOLD_INIT;
12384
12385   result = fold_build1 (code, type, op);
12386
12387   END_FOLD_INIT;
12388   return result;
12389 }
12390
12391 tree
12392 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
12393 {
12394   tree result;
12395   START_FOLD_INIT;
12396
12397   result = fold_build2 (code, type, op0, op1);
12398
12399   END_FOLD_INIT;
12400   return result;
12401 }
12402
12403 tree
12404 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
12405                          tree op2)
12406 {
12407   tree result;
12408   START_FOLD_INIT;
12409
12410   result = fold_build3 (code, type, op0, op1, op2);
12411
12412   END_FOLD_INIT;
12413   return result;
12414 }
12415
12416 #undef START_FOLD_INIT
12417 #undef END_FOLD_INIT
12418
12419 /* Determine if first argument is a multiple of second argument.  Return 0 if
12420    it is not, or we cannot easily determined it to be.
12421
12422    An example of the sort of thing we care about (at this point; this routine
12423    could surely be made more general, and expanded to do what the *_DIV_EXPR's
12424    fold cases do now) is discovering that
12425
12426      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12427
12428    is a multiple of
12429
12430      SAVE_EXPR (J * 8)
12431
12432    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12433
12434    This code also handles discovering that
12435
12436      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12437
12438    is a multiple of 8 so we don't have to worry about dealing with a
12439    possible remainder.
12440
12441    Note that we *look* inside a SAVE_EXPR only to determine how it was
12442    calculated; it is not safe for fold to do much of anything else with the
12443    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12444    at run time.  For example, the latter example above *cannot* be implemented
12445    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12446    evaluation time of the original SAVE_EXPR is not necessarily the same at
12447    the time the new expression is evaluated.  The only optimization of this
12448    sort that would be valid is changing
12449
12450      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12451
12452    divided by 8 to
12453
12454      SAVE_EXPR (I) * SAVE_EXPR (J)
12455
12456    (where the same SAVE_EXPR (J) is used in the original and the
12457    transformed version).  */
12458
12459 static int
12460 multiple_of_p (tree type, tree top, tree bottom)
12461 {
12462   if (operand_equal_p (top, bottom, 0))
12463     return 1;
12464
12465   if (TREE_CODE (type) != INTEGER_TYPE)
12466     return 0;
12467
12468   switch (TREE_CODE (top))
12469     {
12470     case BIT_AND_EXPR:
12471       /* Bitwise and provides a power of two multiple.  If the mask is
12472          a multiple of BOTTOM then TOP is a multiple of BOTTOM.  */
12473       if (!integer_pow2p (bottom))
12474         return 0;
12475       /* FALLTHRU */
12476
12477     case MULT_EXPR:
12478       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12479               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12480
12481     case PLUS_EXPR:
12482     case MINUS_EXPR:
12483       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12484               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12485
12486     case LSHIFT_EXPR:
12487       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12488         {
12489           tree op1, t1;
12490
12491           op1 = TREE_OPERAND (top, 1);
12492           /* const_binop may not detect overflow correctly,
12493              so check for it explicitly here.  */
12494           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
12495               > TREE_INT_CST_LOW (op1)
12496               && TREE_INT_CST_HIGH (op1) == 0
12497               && 0 != (t1 = fold_convert (type,
12498                                           const_binop (LSHIFT_EXPR,
12499                                                        size_one_node,
12500                                                        op1, 0)))
12501               && ! TREE_OVERFLOW (t1))
12502             return multiple_of_p (type, t1, bottom);
12503         }
12504       return 0;
12505
12506     case NOP_EXPR:
12507       /* Can't handle conversions from non-integral or wider integral type.  */
12508       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12509           || (TYPE_PRECISION (type)
12510               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12511         return 0;
12512
12513       /* .. fall through ...  */
12514
12515     case SAVE_EXPR:
12516       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12517
12518     case INTEGER_CST:
12519       if (TREE_CODE (bottom) != INTEGER_CST
12520           || (TYPE_UNSIGNED (type)
12521               && (tree_int_cst_sgn (top) < 0
12522                   || tree_int_cst_sgn (bottom) < 0)))
12523         return 0;
12524       return integer_zerop (const_binop (TRUNC_MOD_EXPR,
12525                                          top, bottom, 0));
12526
12527     default:
12528       return 0;
12529     }
12530 }
12531
12532 /* Return true if `t' is known to be non-negative.  */
12533
12534 bool
12535 tree_expr_nonnegative_p (tree t)
12536 {
12537   if (t == error_mark_node)
12538     return false;
12539
12540   if (TYPE_UNSIGNED (TREE_TYPE (t)))
12541     return true;
12542
12543   switch (TREE_CODE (t))
12544     {
12545     case SSA_NAME:
12546       /* Query VRP to see if it has recorded any information about
12547          the range of this object.  */
12548       return ssa_name_nonnegative_p (t);
12549
12550     case ABS_EXPR:
12551       /* We can't return 1 if flag_wrapv is set because
12552          ABS_EXPR<INT_MIN> = INT_MIN.  */
12553       if (!(flag_wrapv && INTEGRAL_TYPE_P (TREE_TYPE (t))))
12554         return true;
12555       break;
12556
12557     case INTEGER_CST:
12558       return tree_int_cst_sgn (t) >= 0;
12559
12560     case REAL_CST:
12561       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12562
12563     case PLUS_EXPR:
12564       if (FLOAT_TYPE_P (TREE_TYPE (t)))
12565         return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
12566                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
12567
12568       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12569          both unsigned and at least 2 bits shorter than the result.  */
12570       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
12571           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
12572           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
12573         {
12574           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
12575           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
12576           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12577               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12578             {
12579               unsigned int prec = MAX (TYPE_PRECISION (inner1),
12580                                        TYPE_PRECISION (inner2)) + 1;
12581               return prec < TYPE_PRECISION (TREE_TYPE (t));
12582             }
12583         }
12584       break;
12585
12586     case MULT_EXPR:
12587       if (FLOAT_TYPE_P (TREE_TYPE (t)))
12588         {
12589           /* x * x for floating point x is always non-negative.  */
12590           if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
12591             return true;
12592           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
12593                  && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
12594         }
12595
12596       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12597          both unsigned and their total bits is shorter than the result.  */
12598       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
12599           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
12600           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
12601         {
12602           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
12603           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
12604           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12605               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12606             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
12607                    < TYPE_PRECISION (TREE_TYPE (t));
12608         }
12609       return false;
12610
12611     case BIT_AND_EXPR:
12612     case MAX_EXPR:
12613       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
12614              || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
12615
12616     case BIT_IOR_EXPR:
12617     case BIT_XOR_EXPR:
12618     case MIN_EXPR:
12619     case RDIV_EXPR:
12620     case TRUNC_DIV_EXPR:
12621     case CEIL_DIV_EXPR:
12622     case FLOOR_DIV_EXPR:
12623     case ROUND_DIV_EXPR:
12624       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
12625              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
12626
12627     case TRUNC_MOD_EXPR:
12628     case CEIL_MOD_EXPR:
12629     case FLOOR_MOD_EXPR:
12630     case ROUND_MOD_EXPR:
12631     case SAVE_EXPR:
12632     case NON_LVALUE_EXPR:
12633     case FLOAT_EXPR:
12634       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
12635
12636     case COMPOUND_EXPR:
12637     case MODIFY_EXPR:
12638     case GIMPLE_MODIFY_STMT:
12639       return tree_expr_nonnegative_p (GENERIC_TREE_OPERAND (t, 1));
12640
12641     case BIND_EXPR:
12642       return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
12643
12644     case COND_EXPR:
12645       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
12646              && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
12647
12648     case NOP_EXPR:
12649       {
12650         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
12651         tree outer_type = TREE_TYPE (t);
12652
12653         if (TREE_CODE (outer_type) == REAL_TYPE)
12654           {
12655             if (TREE_CODE (inner_type) == REAL_TYPE)
12656               return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
12657             if (TREE_CODE (inner_type) == INTEGER_TYPE)
12658               {
12659                 if (TYPE_UNSIGNED (inner_type))
12660                   return true;
12661                 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
12662               }
12663           }
12664         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
12665           {
12666             if (TREE_CODE (inner_type) == REAL_TYPE)
12667               return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
12668             if (TREE_CODE (inner_type) == INTEGER_TYPE)
12669               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12670                       && TYPE_UNSIGNED (inner_type);
12671           }
12672       }
12673       break;
12674
12675     case TARGET_EXPR:
12676       {
12677         tree temp = TARGET_EXPR_SLOT (t);
12678         t = TARGET_EXPR_INITIAL (t);
12679
12680         /* If the initializer is non-void, then it's a normal expression
12681            that will be assigned to the slot.  */
12682         if (!VOID_TYPE_P (t))
12683           return tree_expr_nonnegative_p (t);
12684
12685         /* Otherwise, the initializer sets the slot in some way.  One common
12686            way is an assignment statement at the end of the initializer.  */
12687         while (1)
12688           {
12689             if (TREE_CODE (t) == BIND_EXPR)
12690               t = expr_last (BIND_EXPR_BODY (t));
12691             else if (TREE_CODE (t) == TRY_FINALLY_EXPR
12692                      || TREE_CODE (t) == TRY_CATCH_EXPR)
12693               t = expr_last (TREE_OPERAND (t, 0));
12694             else if (TREE_CODE (t) == STATEMENT_LIST)
12695               t = expr_last (t);
12696             else
12697               break;
12698           }
12699         if ((TREE_CODE (t) == MODIFY_EXPR
12700              || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
12701             && GENERIC_TREE_OPERAND (t, 0) == temp)
12702           return tree_expr_nonnegative_p (GENERIC_TREE_OPERAND (t, 1));
12703
12704         return false;
12705       }
12706
12707     case CALL_EXPR:
12708       {
12709         tree fndecl = get_callee_fndecl (t);
12710         tree arglist = TREE_OPERAND (t, 1);
12711         if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
12712           switch (DECL_FUNCTION_CODE (fndecl))
12713             {
12714             CASE_FLT_FN (BUILT_IN_ACOS):
12715             CASE_FLT_FN (BUILT_IN_ACOSH):
12716             CASE_FLT_FN (BUILT_IN_CABS):
12717             CASE_FLT_FN (BUILT_IN_COSH):
12718             CASE_FLT_FN (BUILT_IN_ERFC):
12719             CASE_FLT_FN (BUILT_IN_EXP):
12720             CASE_FLT_FN (BUILT_IN_EXP10):
12721             CASE_FLT_FN (BUILT_IN_EXP2):
12722             CASE_FLT_FN (BUILT_IN_FABS):
12723             CASE_FLT_FN (BUILT_IN_FDIM):
12724             CASE_FLT_FN (BUILT_IN_HYPOT):
12725             CASE_FLT_FN (BUILT_IN_POW10):
12726             CASE_INT_FN (BUILT_IN_FFS):
12727             CASE_INT_FN (BUILT_IN_PARITY):
12728             CASE_INT_FN (BUILT_IN_POPCOUNT):
12729             case BUILT_IN_BSWAP32:
12730             case BUILT_IN_BSWAP64:
12731               /* Always true.  */
12732               return true;
12733
12734             CASE_FLT_FN (BUILT_IN_SQRT):
12735               /* sqrt(-0.0) is -0.0.  */
12736               if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
12737                 return true;
12738               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
12739
12740             CASE_FLT_FN (BUILT_IN_ASINH):
12741             CASE_FLT_FN (BUILT_IN_ATAN):
12742             CASE_FLT_FN (BUILT_IN_ATANH):
12743             CASE_FLT_FN (BUILT_IN_CBRT):
12744             CASE_FLT_FN (BUILT_IN_CEIL):
12745             CASE_FLT_FN (BUILT_IN_ERF):
12746             CASE_FLT_FN (BUILT_IN_EXPM1):
12747             CASE_FLT_FN (BUILT_IN_FLOOR):
12748             CASE_FLT_FN (BUILT_IN_FMOD):
12749             CASE_FLT_FN (BUILT_IN_FREXP):
12750             CASE_FLT_FN (BUILT_IN_LCEIL):
12751             CASE_FLT_FN (BUILT_IN_LDEXP):
12752             CASE_FLT_FN (BUILT_IN_LFLOOR):
12753             CASE_FLT_FN (BUILT_IN_LLCEIL):
12754             CASE_FLT_FN (BUILT_IN_LLFLOOR):
12755             CASE_FLT_FN (BUILT_IN_LLRINT):
12756             CASE_FLT_FN (BUILT_IN_LLROUND):
12757             CASE_FLT_FN (BUILT_IN_LRINT):
12758             CASE_FLT_FN (BUILT_IN_LROUND):
12759             CASE_FLT_FN (BUILT_IN_MODF):
12760             CASE_FLT_FN (BUILT_IN_NEARBYINT):
12761             CASE_FLT_FN (BUILT_IN_RINT):
12762             CASE_FLT_FN (BUILT_IN_ROUND):
12763             CASE_FLT_FN (BUILT_IN_SIGNBIT):
12764             CASE_FLT_FN (BUILT_IN_SINH):
12765             CASE_FLT_FN (BUILT_IN_TANH):
12766             CASE_FLT_FN (BUILT_IN_TRUNC):
12767               /* True if the 1st argument is nonnegative.  */
12768               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
12769
12770             CASE_FLT_FN (BUILT_IN_FMAX):
12771               /* True if the 1st OR 2nd arguments are nonnegative.  */
12772               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
12773                 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
12774
12775             CASE_FLT_FN (BUILT_IN_FMIN):
12776               /* True if the 1st AND 2nd arguments are nonnegative.  */
12777               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
12778                 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
12779
12780             CASE_FLT_FN (BUILT_IN_COPYSIGN):
12781               /* True if the 2nd argument is nonnegative.  */
12782               return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
12783
12784             CASE_FLT_FN (BUILT_IN_POWI):
12785               /* True if the 1st argument is nonnegative or the second
12786                  argument is an even integer.  */
12787               if (TREE_CODE (TREE_VALUE (TREE_CHAIN (arglist))) == INTEGER_CST)
12788                 {
12789                   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12790                   if ((TREE_INT_CST_LOW (arg1) & 1) == 0)
12791                     return true;
12792                 }
12793               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
12794
12795             CASE_FLT_FN (BUILT_IN_POW):
12796               /* True if the 1st argument is nonnegative or the second
12797                  argument is an even integer valued real.  */
12798               if (TREE_CODE (TREE_VALUE (TREE_CHAIN (arglist))) == REAL_CST)
12799                 {
12800                   REAL_VALUE_TYPE c;
12801                   HOST_WIDE_INT n;
12802
12803                   c = TREE_REAL_CST (TREE_VALUE (TREE_CHAIN (arglist)));
12804                   n = real_to_integer (&c);
12805                   if ((n & 1) == 0)
12806                     {
12807                       REAL_VALUE_TYPE cint;
12808                       real_from_integer (&cint, VOIDmode, n,
12809                                          n < 0 ? -1 : 0, 0);
12810                       if (real_identical (&c, &cint))
12811                         return true;
12812                     }
12813                 }
12814               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
12815
12816             default:
12817               break;
12818             }
12819       }
12820
12821       /* ... fall through ...  */
12822
12823     default:
12824       if (truth_value_p (TREE_CODE (t)))
12825         /* Truth values evaluate to 0 or 1, which is nonnegative.  */
12826         return true;
12827     }
12828
12829   /* We don't know sign of `t', so be conservative and return false.  */
12830   return false;
12831 }
12832
12833 /* Return true when T is an address and is known to be nonzero.
12834    For floating point we further ensure that T is not denormal.
12835    Similar logic is present in nonzero_address in rtlanal.h.  */
12836
12837 bool
12838 tree_expr_nonzero_p (tree t)
12839 {
12840   tree type = TREE_TYPE (t);
12841
12842   /* Doing something useful for floating point would need more work.  */
12843   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
12844     return false;
12845
12846   switch (TREE_CODE (t))
12847     {
12848     case SSA_NAME:
12849       /* Query VRP to see if it has recorded any information about
12850          the range of this object.  */
12851       return ssa_name_nonzero_p (t);
12852
12853     case ABS_EXPR:
12854       return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
12855
12856     case INTEGER_CST:
12857       /* We used to test for !integer_zerop here.  This does not work correctly
12858          if TREE_CONSTANT_OVERFLOW (t).  */
12859       return (TREE_INT_CST_LOW (t) != 0
12860               || TREE_INT_CST_HIGH (t) != 0);
12861
12862     case PLUS_EXPR:
12863       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
12864         {
12865           /* With the presence of negative values it is hard
12866              to say something.  */
12867           if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
12868               || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
12869             return false;
12870           /* One of operands must be positive and the other non-negative.  */
12871           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
12872                   || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
12873         }
12874       break;
12875
12876     case MULT_EXPR:
12877       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
12878         {
12879           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
12880                   && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
12881         }
12882       break;
12883
12884     case NOP_EXPR:
12885       {
12886         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
12887         tree outer_type = TREE_TYPE (t);
12888
12889         return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
12890                 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
12891       }
12892       break;
12893
12894    case ADDR_EXPR:
12895       {
12896         tree base = get_base_address (TREE_OPERAND (t, 0));
12897
12898         if (!base)
12899           return false;
12900
12901         /* Weak declarations may link to NULL.  */
12902         if (VAR_OR_FUNCTION_DECL_P (base))
12903           return !DECL_WEAK (base);
12904
12905         /* Constants are never weak.  */
12906         if (CONSTANT_CLASS_P (base))
12907           return true;
12908
12909         return false;
12910       }
12911
12912     case COND_EXPR:
12913       return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
12914               && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
12915
12916     case MIN_EXPR:
12917       return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
12918               && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
12919
12920     case MAX_EXPR:
12921       if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
12922         {
12923           /* When both operands are nonzero, then MAX must be too.  */
12924           if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
12925             return true;
12926
12927           /* MAX where operand 0 is positive is positive.  */
12928           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
12929         }
12930       /* MAX where operand 1 is positive is positive.  */
12931       else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
12932                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
12933         return true;
12934       break;
12935
12936     case COMPOUND_EXPR:
12937     case MODIFY_EXPR:
12938     case GIMPLE_MODIFY_STMT:
12939     case BIND_EXPR:
12940       return tree_expr_nonzero_p (GENERIC_TREE_OPERAND (t, 1));
12941
12942     case SAVE_EXPR:
12943     case NON_LVALUE_EXPR:
12944       return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
12945
12946     case BIT_IOR_EXPR:
12947       return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
12948              || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
12949
12950     case CALL_EXPR:
12951       return alloca_call_p (t);
12952
12953     default:
12954       break;
12955     }
12956   return false;
12957 }
12958
12959 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
12960    attempt to fold the expression to a constant without modifying TYPE,
12961    OP0 or OP1.
12962
12963    If the expression could be simplified to a constant, then return
12964    the constant.  If the expression would not be simplified to a
12965    constant, then return NULL_TREE.  */
12966
12967 tree
12968 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
12969 {
12970   tree tem = fold_binary (code, type, op0, op1);
12971   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
12972 }
12973
12974 /* Given the components of a unary expression CODE, TYPE and OP0,
12975    attempt to fold the expression to a constant without modifying
12976    TYPE or OP0.
12977
12978    If the expression could be simplified to a constant, then return
12979    the constant.  If the expression would not be simplified to a
12980    constant, then return NULL_TREE.  */
12981
12982 tree
12983 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
12984 {
12985   tree tem = fold_unary (code, type, op0);
12986   return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
12987 }
12988
12989 /* If EXP represents referencing an element in a constant string
12990    (either via pointer arithmetic or array indexing), return the
12991    tree representing the value accessed, otherwise return NULL.  */
12992
12993 tree
12994 fold_read_from_constant_string (tree exp)
12995 {
12996   if ((TREE_CODE (exp) == INDIRECT_REF
12997        || TREE_CODE (exp) == ARRAY_REF)
12998       && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
12999     {
13000       tree exp1 = TREE_OPERAND (exp, 0);
13001       tree index;
13002       tree string;
13003
13004       if (TREE_CODE (exp) == INDIRECT_REF)
13005         string = string_constant (exp1, &index);
13006       else
13007         {
13008           tree low_bound = array_ref_low_bound (exp);
13009           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
13010
13011           /* Optimize the special-case of a zero lower bound.
13012
13013              We convert the low_bound to sizetype to avoid some problems
13014              with constant folding.  (E.g. suppose the lower bound is 1,
13015              and its mode is QI.  Without the conversion,l (ARRAY
13016              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13017              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
13018           if (! integer_zerop (low_bound))
13019             index = size_diffop (index, fold_convert (sizetype, low_bound));
13020
13021           string = exp1;
13022         }
13023
13024       if (string
13025           && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13026           && TREE_CODE (string) == STRING_CST
13027           && TREE_CODE (index) == INTEGER_CST
13028           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13029           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13030               == MODE_INT)
13031           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13032         return fold_convert (TREE_TYPE (exp),
13033                              build_int_cst (NULL_TREE,
13034                                             (TREE_STRING_POINTER (string)
13035                                              [TREE_INT_CST_LOW (index)])));
13036     }
13037   return NULL;
13038 }
13039
13040 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13041    an integer constant or real constant.
13042
13043    TYPE is the type of the result.  */
13044
13045 static tree
13046 fold_negate_const (tree arg0, tree type)
13047 {
13048   tree t = NULL_TREE;
13049
13050   switch (TREE_CODE (arg0))
13051     {
13052     case INTEGER_CST:
13053       {
13054         unsigned HOST_WIDE_INT low;
13055         HOST_WIDE_INT high;
13056         int overflow = neg_double (TREE_INT_CST_LOW (arg0),
13057                                    TREE_INT_CST_HIGH (arg0),
13058                                    &low, &high);
13059         t = build_int_cst_wide (type, low, high);
13060         t = force_fit_type (t, 1,
13061                             (overflow | TREE_OVERFLOW (arg0))
13062                             && !TYPE_UNSIGNED (type),
13063                             TREE_CONSTANT_OVERFLOW (arg0));
13064         break;
13065       }
13066
13067     case REAL_CST:
13068       t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
13069       break;
13070
13071     default:
13072       gcc_unreachable ();
13073     }
13074
13075   return t;
13076 }
13077
13078 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13079    an integer constant or real constant.
13080
13081    TYPE is the type of the result.  */
13082
13083 tree
13084 fold_abs_const (tree arg0, tree type)
13085 {
13086   tree t = NULL_TREE;
13087
13088   switch (TREE_CODE (arg0))
13089     {
13090     case INTEGER_CST:
13091       /* If the value is unsigned, then the absolute value is
13092          the same as the ordinary value.  */
13093       if (TYPE_UNSIGNED (type))
13094         t = arg0;
13095       /* Similarly, if the value is non-negative.  */
13096       else if (INT_CST_LT (integer_minus_one_node, arg0))
13097         t = arg0;
13098       /* If the value is negative, then the absolute value is
13099          its negation.  */
13100       else
13101         {
13102           unsigned HOST_WIDE_INT low;
13103           HOST_WIDE_INT high;
13104           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
13105                                      TREE_INT_CST_HIGH (arg0),
13106                                      &low, &high);
13107           t = build_int_cst_wide (type, low, high);
13108           t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
13109                               TREE_CONSTANT_OVERFLOW (arg0));
13110         }
13111       break;
13112
13113     case REAL_CST:
13114       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13115         t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
13116       else
13117         t =  arg0;
13118       break;
13119
13120     default:
13121       gcc_unreachable ();
13122     }
13123
13124   return t;
13125 }
13126
13127 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13128    constant.  TYPE is the type of the result.  */
13129
13130 static tree
13131 fold_not_const (tree arg0, tree type)
13132 {
13133   tree t = NULL_TREE;
13134
13135   gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13136
13137   t = build_int_cst_wide (type,
13138                           ~ TREE_INT_CST_LOW (arg0),
13139                           ~ TREE_INT_CST_HIGH (arg0));
13140   t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
13141                       TREE_CONSTANT_OVERFLOW (arg0));
13142
13143   return t;
13144 }
13145
13146 /* Given CODE, a relational operator, the target type, TYPE and two
13147    constant operands OP0 and OP1, return the result of the
13148    relational operation.  If the result is not a compile time
13149    constant, then return NULL_TREE.  */
13150
13151 static tree
13152 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13153 {
13154   int result, invert;
13155
13156   /* From here on, the only cases we handle are when the result is
13157      known to be a constant.  */
13158
13159   if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13160     {
13161       const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13162       const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13163
13164       /* Handle the cases where either operand is a NaN.  */
13165       if (real_isnan (c0) || real_isnan (c1))
13166         {
13167           switch (code)
13168             {
13169             case EQ_EXPR:
13170             case ORDERED_EXPR:
13171               result = 0;
13172               break;
13173
13174             case NE_EXPR:
13175             case UNORDERED_EXPR:
13176             case UNLT_EXPR:
13177             case UNLE_EXPR:
13178             case UNGT_EXPR:
13179             case UNGE_EXPR:
13180             case UNEQ_EXPR:
13181               result = 1;
13182               break;
13183
13184             case LT_EXPR:
13185             case LE_EXPR:
13186             case GT_EXPR:
13187             case GE_EXPR:
13188             case LTGT_EXPR:
13189               if (flag_trapping_math)
13190                 return NULL_TREE;
13191               result = 0;
13192               break;
13193
13194             default:
13195               gcc_unreachable ();
13196             }
13197
13198           return constant_boolean_node (result, type);
13199         }
13200
13201       return constant_boolean_node (real_compare (code, c0, c1), type);
13202     }
13203
13204   /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13205
13206      To compute GT, swap the arguments and do LT.
13207      To compute GE, do LT and invert the result.
13208      To compute LE, swap the arguments, do LT and invert the result.
13209      To compute NE, do EQ and invert the result.
13210
13211      Therefore, the code below must handle only EQ and LT.  */
13212
13213   if (code == LE_EXPR || code == GT_EXPR)
13214     {
13215       tree tem = op0;
13216       op0 = op1;
13217       op1 = tem;
13218       code = swap_tree_comparison (code);
13219     }
13220
13221   /* Note that it is safe to invert for real values here because we
13222      have already handled the one case that it matters.  */
13223
13224   invert = 0;
13225   if (code == NE_EXPR || code == GE_EXPR)
13226     {
13227       invert = 1;
13228       code = invert_tree_comparison (code, false);
13229     }
13230
13231   /* Compute a result for LT or EQ if args permit;
13232      Otherwise return T.  */
13233   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13234     {
13235       if (code == EQ_EXPR)
13236         result = tree_int_cst_equal (op0, op1);
13237       else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
13238         result = INT_CST_LT_UNSIGNED (op0, op1);
13239       else
13240         result = INT_CST_LT (op0, op1);
13241     }
13242   else
13243     return NULL_TREE;
13244
13245   if (invert)
13246     result ^= 1;
13247   return constant_boolean_node (result, type);
13248 }
13249
13250 /* Build an expression for the a clean point containing EXPR with type TYPE.
13251    Don't build a cleanup point expression for EXPR which don't have side
13252    effects.  */
13253
13254 tree
13255 fold_build_cleanup_point_expr (tree type, tree expr)
13256 {
13257   /* If the expression does not have side effects then we don't have to wrap
13258      it with a cleanup point expression.  */
13259   if (!TREE_SIDE_EFFECTS (expr))
13260     return expr;
13261
13262   /* If the expression is a return, check to see if the expression inside the
13263      return has no side effects or the right hand side of the modify expression
13264      inside the return. If either don't have side effects set we don't need to
13265      wrap the expression in a cleanup point expression.  Note we don't check the
13266      left hand side of the modify because it should always be a return decl.  */
13267   if (TREE_CODE (expr) == RETURN_EXPR)
13268     {
13269       tree op = TREE_OPERAND (expr, 0);
13270       if (!op || !TREE_SIDE_EFFECTS (op))
13271         return expr;
13272       op = TREE_OPERAND (op, 1);
13273       if (!TREE_SIDE_EFFECTS (op))
13274         return expr;
13275     }
13276   
13277   return build1 (CLEANUP_POINT_EXPR, type, expr);
13278 }
13279
13280 /* Build an expression for the address of T.  Folds away INDIRECT_REF to
13281    avoid confusing the gimplify process.  */
13282
13283 tree
13284 build_fold_addr_expr_with_type (tree t, tree ptrtype)
13285 {
13286   /* The size of the object is not relevant when talking about its address.  */
13287   if (TREE_CODE (t) == WITH_SIZE_EXPR)
13288     t = TREE_OPERAND (t, 0);
13289
13290   /* Note: doesn't apply to ALIGN_INDIRECT_REF */
13291   if (TREE_CODE (t) == INDIRECT_REF
13292       || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
13293     {
13294       t = TREE_OPERAND (t, 0);
13295       if (TREE_TYPE (t) != ptrtype)
13296         t = build1 (NOP_EXPR, ptrtype, t);
13297     }
13298   else
13299     {
13300       tree base = t;
13301
13302       while (handled_component_p (base))
13303         base = TREE_OPERAND (base, 0);
13304       if (DECL_P (base))
13305         TREE_ADDRESSABLE (base) = 1;
13306
13307       t = build1 (ADDR_EXPR, ptrtype, t);
13308     }
13309
13310   return t;
13311 }
13312
13313 tree
13314 build_fold_addr_expr (tree t)
13315 {
13316   return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
13317 }
13318
13319 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13320    of an indirection through OP0, or NULL_TREE if no simplification is
13321    possible.  */
13322
13323 tree
13324 fold_indirect_ref_1 (tree type, tree op0)
13325 {
13326   tree sub = op0;
13327   tree subtype;
13328
13329   STRIP_NOPS (sub);
13330   subtype = TREE_TYPE (sub);
13331   if (!POINTER_TYPE_P (subtype))
13332     return NULL_TREE;
13333
13334   if (TREE_CODE (sub) == ADDR_EXPR)
13335     {
13336       tree op = TREE_OPERAND (sub, 0);
13337       tree optype = TREE_TYPE (op);
13338       /* *&CONST_DECL -> to the value of the const decl.  */
13339       if (TREE_CODE (op) == CONST_DECL)
13340         return DECL_INITIAL (op);
13341       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
13342       if (type == optype)
13343         {
13344           tree fop = fold_read_from_constant_string (op);
13345           if (fop)
13346             return fop;
13347           else
13348             return op;
13349         }
13350       /* *(foo *)&fooarray => fooarray[0] */
13351       else if (TREE_CODE (optype) == ARRAY_TYPE
13352                && type == TREE_TYPE (optype))
13353         {
13354           tree type_domain = TYPE_DOMAIN (optype);
13355           tree min_val = size_zero_node;
13356           if (type_domain && TYPE_MIN_VALUE (type_domain))
13357             min_val = TYPE_MIN_VALUE (type_domain);
13358           return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
13359         }
13360       /* *(foo *)&complexfoo => __real__ complexfoo */
13361       else if (TREE_CODE (optype) == COMPLEX_TYPE
13362                && type == TREE_TYPE (optype))
13363         return fold_build1 (REALPART_EXPR, type, op);
13364       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
13365       else if (TREE_CODE (optype) == VECTOR_TYPE
13366                && type == TREE_TYPE (optype))
13367         {
13368           tree part_width = TYPE_SIZE (type);
13369           tree index = bitsize_int (0);
13370           return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
13371         }
13372     }
13373
13374   /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
13375   if (TREE_CODE (sub) == PLUS_EXPR
13376       && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
13377     {
13378       tree op00 = TREE_OPERAND (sub, 0);
13379       tree op01 = TREE_OPERAND (sub, 1);
13380       tree op00type;
13381
13382       STRIP_NOPS (op00);
13383       op00type = TREE_TYPE (op00);
13384       if (TREE_CODE (op00) == ADDR_EXPR
13385           && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
13386           && type == TREE_TYPE (TREE_TYPE (op00type)))
13387         {
13388           tree size = TYPE_SIZE_UNIT (type);
13389           if (tree_int_cst_equal (size, op01))
13390             return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
13391         }
13392     }
13393   
13394   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
13395   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
13396       && type == TREE_TYPE (TREE_TYPE (subtype)))
13397     {
13398       tree type_domain;
13399       tree min_val = size_zero_node;
13400       sub = build_fold_indirect_ref (sub);
13401       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
13402       if (type_domain && TYPE_MIN_VALUE (type_domain))
13403         min_val = TYPE_MIN_VALUE (type_domain);
13404       return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
13405     }
13406
13407   return NULL_TREE;
13408 }
13409
13410 /* Builds an expression for an indirection through T, simplifying some
13411    cases.  */
13412
13413 tree
13414 build_fold_indirect_ref (tree t)
13415 {
13416   tree type = TREE_TYPE (TREE_TYPE (t));
13417   tree sub = fold_indirect_ref_1 (type, t);
13418
13419   if (sub)
13420     return sub;
13421   else
13422     return build1 (INDIRECT_REF, type, t);
13423 }
13424
13425 /* Given an INDIRECT_REF T, return either T or a simplified version.  */
13426
13427 tree
13428 fold_indirect_ref (tree t)
13429 {
13430   tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
13431
13432   if (sub)
13433     return sub;
13434   else
13435     return t;
13436 }
13437
13438 /* Strip non-trapping, non-side-effecting tree nodes from an expression
13439    whose result is ignored.  The type of the returned tree need not be
13440    the same as the original expression.  */
13441
13442 tree
13443 fold_ignored_result (tree t)
13444 {
13445   if (!TREE_SIDE_EFFECTS (t))
13446     return integer_zero_node;
13447
13448   for (;;)
13449     switch (TREE_CODE_CLASS (TREE_CODE (t)))
13450       {
13451       case tcc_unary:
13452         t = TREE_OPERAND (t, 0);
13453         break;
13454
13455       case tcc_binary:
13456       case tcc_comparison:
13457         if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
13458           t = TREE_OPERAND (t, 0);
13459         else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
13460           t = TREE_OPERAND (t, 1);
13461         else
13462           return t;
13463         break;
13464
13465       case tcc_expression:
13466         switch (TREE_CODE (t))
13467           {
13468           case COMPOUND_EXPR:
13469             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
13470               return t;
13471             t = TREE_OPERAND (t, 0);
13472             break;
13473
13474           case COND_EXPR:
13475             if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
13476                 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
13477               return t;
13478             t = TREE_OPERAND (t, 0);
13479             break;
13480
13481           default:
13482             return t;
13483           }
13484         break;
13485
13486       default:
13487         return t;
13488       }
13489 }
13490
13491 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
13492    This can only be applied to objects of a sizetype.  */
13493
13494 tree
13495 round_up (tree value, int divisor)
13496 {
13497   tree div = NULL_TREE;
13498
13499   gcc_assert (divisor > 0);
13500   if (divisor == 1)
13501     return value;
13502
13503   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
13504      have to do anything.  Only do this when we are not given a const,
13505      because in that case, this check is more expensive than just
13506      doing it.  */
13507   if (TREE_CODE (value) != INTEGER_CST)
13508     {
13509       div = build_int_cst (TREE_TYPE (value), divisor);
13510
13511       if (multiple_of_p (TREE_TYPE (value), value, div))
13512         return value;
13513     }
13514
13515   /* If divisor is a power of two, simplify this to bit manipulation.  */
13516   if (divisor == (divisor & -divisor))
13517     {
13518       tree t;
13519
13520       t = build_int_cst (TREE_TYPE (value), divisor - 1);
13521       value = size_binop (PLUS_EXPR, value, t);
13522       t = build_int_cst (TREE_TYPE (value), -divisor);
13523       value = size_binop (BIT_AND_EXPR, value, t);
13524     }
13525   else
13526     {
13527       if (!div)
13528         div = build_int_cst (TREE_TYPE (value), divisor);
13529       value = size_binop (CEIL_DIV_EXPR, value, div);
13530       value = size_binop (MULT_EXPR, value, div);
13531     }
13532
13533   return value;
13534 }
13535
13536 /* Likewise, but round down.  */
13537
13538 tree
13539 round_down (tree value, int divisor)
13540 {
13541   tree div = NULL_TREE;
13542
13543   gcc_assert (divisor > 0);
13544   if (divisor == 1)
13545     return value;
13546
13547   /* See if VALUE is already a multiple of DIVISOR.  If so, we don't
13548      have to do anything.  Only do this when we are not given a const,
13549      because in that case, this check is more expensive than just
13550      doing it.  */
13551   if (TREE_CODE (value) != INTEGER_CST)
13552     {
13553       div = build_int_cst (TREE_TYPE (value), divisor);
13554
13555       if (multiple_of_p (TREE_TYPE (value), value, div))
13556         return value;
13557     }
13558
13559   /* If divisor is a power of two, simplify this to bit manipulation.  */
13560   if (divisor == (divisor & -divisor))
13561     {
13562       tree t;
13563
13564       t = build_int_cst (TREE_TYPE (value), -divisor);
13565       value = size_binop (BIT_AND_EXPR, value, t);
13566     }
13567   else
13568     {
13569       if (!div)
13570         div = build_int_cst (TREE_TYPE (value), divisor);
13571       value = size_binop (FLOOR_DIV_EXPR, value, div);
13572       value = size_binop (MULT_EXPR, value, div);
13573     }
13574
13575   return value;
13576 }
13577
13578 /* Returns the pointer to the base of the object addressed by EXP and
13579    extracts the information about the offset of the access, storing it
13580    to PBITPOS and POFFSET.  */
13581
13582 static tree
13583 split_address_to_core_and_offset (tree exp,
13584                                   HOST_WIDE_INT *pbitpos, tree *poffset)
13585 {
13586   tree core;
13587   enum machine_mode mode;
13588   int unsignedp, volatilep;
13589   HOST_WIDE_INT bitsize;
13590
13591   if (TREE_CODE (exp) == ADDR_EXPR)
13592     {
13593       core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
13594                                   poffset, &mode, &unsignedp, &volatilep,
13595                                   false);
13596       core = build_fold_addr_expr (core);
13597     }
13598   else
13599     {
13600       core = exp;
13601       *pbitpos = 0;
13602       *poffset = NULL_TREE;
13603     }
13604
13605   return core;
13606 }
13607
13608 /* Returns true if addresses of E1 and E2 differ by a constant, false
13609    otherwise.  If they do, E1 - E2 is stored in *DIFF.  */
13610
13611 bool
13612 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
13613 {
13614   tree core1, core2;
13615   HOST_WIDE_INT bitpos1, bitpos2;
13616   tree toffset1, toffset2, tdiff, type;
13617
13618   core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
13619   core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
13620
13621   if (bitpos1 % BITS_PER_UNIT != 0
13622       || bitpos2 % BITS_PER_UNIT != 0
13623       || !operand_equal_p (core1, core2, 0))
13624     return false;
13625
13626   if (toffset1 && toffset2)
13627     {
13628       type = TREE_TYPE (toffset1);
13629       if (type != TREE_TYPE (toffset2))
13630         toffset2 = fold_convert (type, toffset2);
13631
13632       tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
13633       if (!cst_and_fits_in_hwi (tdiff))
13634         return false;
13635
13636       *diff = int_cst_value (tdiff);
13637     }
13638   else if (toffset1 || toffset2)
13639     {
13640       /* If only one of the offsets is non-constant, the difference cannot
13641          be a constant.  */
13642       return false;
13643     }
13644   else
13645     *diff = 0;
13646
13647   *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
13648   return true;
13649 }
13650
13651 /* Simplify the floating point expression EXP when the sign of the
13652    result is not significant.  Return NULL_TREE if no simplification
13653    is possible.  */
13654
13655 tree
13656 fold_strip_sign_ops (tree exp)
13657 {
13658   tree arg0, arg1;
13659
13660   switch (TREE_CODE (exp))
13661     {
13662     case ABS_EXPR:
13663     case NEGATE_EXPR:
13664       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
13665       return arg0 ? arg0 : TREE_OPERAND (exp, 0);
13666
13667     case MULT_EXPR:
13668     case RDIV_EXPR:
13669       if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
13670         return NULL_TREE;
13671       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
13672       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
13673       if (arg0 != NULL_TREE || arg1 != NULL_TREE)
13674         return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
13675                             arg0 ? arg0 : TREE_OPERAND (exp, 0),
13676                             arg1 ? arg1 : TREE_OPERAND (exp, 1));
13677       break;
13678
13679     case COMPOUND_EXPR:
13680       arg0 = TREE_OPERAND (exp, 0);
13681       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
13682       if (arg1)
13683         return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
13684       break;
13685       
13686     case COND_EXPR:
13687       arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
13688       arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
13689       if (arg0 || arg1)
13690         return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
13691                             arg0 ? arg0 : TREE_OPERAND (exp, 1),
13692                             arg1 ? arg1 : TREE_OPERAND (exp, 2));
13693       break;
13694       
13695     case CALL_EXPR:
13696       {
13697         const enum built_in_function fcode = builtin_mathfn_code (exp);
13698         switch (fcode)
13699         {
13700         CASE_FLT_FN (BUILT_IN_COPYSIGN):
13701           /* Strip copysign function call, return the 1st argument. */
13702           arg0 = TREE_VALUE (TREE_OPERAND (exp, 1));
13703           arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (exp, 1)));
13704           return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
13705
13706         default:
13707           /* Strip sign ops from the argument of "odd" math functions.  */
13708           if (negate_mathfn_p (fcode))
13709             {
13710               arg0 = fold_strip_sign_ops (TREE_VALUE (TREE_OPERAND (exp, 1)));
13711               if (arg0)
13712                 return build_function_call_expr (get_callee_fndecl (exp),
13713                                                  build_tree_list (NULL_TREE,
13714                                                                   arg0));
13715             }
13716           break;
13717         }
13718       }
13719       break;
13720
13721     default:
13722       break;
13723     }
13724   return NULL_TREE;
13725 }