OSDN Git Service

2004-06-09 Daniel Berlin <dberlin@dberlin.org>
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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 and prior overflow indicator, and
43    forces the value to fit the type.  It returns an overflow indicator.  */
44
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "real.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "toplev.h"
56 #include "ggc.h"
57 #include "hashtab.h"
58 #include "langhooks.h"
59 #include "md5.h"
60
61 /* The following constants represent a bit based encoding of GCC's
62    comparison operators.  This encoding simplifies transformations
63    on relational comparison operators, such as AND and OR.  */
64 enum comparison_code {
65   COMPCODE_FALSE = 0,
66   COMPCODE_LT = 1,
67   COMPCODE_EQ = 2,
68   COMPCODE_LE = 3,
69   COMPCODE_GT = 4,
70   COMPCODE_LTGT = 5,
71   COMPCODE_GE = 6,
72   COMPCODE_ORD = 7,
73   COMPCODE_UNORD = 8,
74   COMPCODE_UNLT = 9,
75   COMPCODE_UNEQ = 10,
76   COMPCODE_UNLE = 11,
77   COMPCODE_UNGT = 12,
78   COMPCODE_NE = 13,
79   COMPCODE_UNGE = 14,
80   COMPCODE_TRUE = 15
81 };
82
83 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
84 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
85 static bool negate_mathfn_p (enum built_in_function);
86 static bool negate_expr_p (tree);
87 static tree negate_expr (tree);
88 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
89 static tree associate_trees (tree, tree, enum tree_code, tree);
90 static tree const_binop (enum tree_code, tree, tree, int);
91 static hashval_t size_htab_hash (const void *);
92 static int size_htab_eq (const void *, const void *);
93 static tree fold_convert_const (enum tree_code, tree, tree);
94 static enum tree_code invert_tree_comparison (enum tree_code, bool);
95 static enum tree_code swap_tree_comparison (enum tree_code);
96 static enum comparison_code comparison_to_compcode (enum tree_code);
97 static enum tree_code compcode_to_comparison (enum comparison_code);
98 static tree combine_comparisons (enum tree_code, enum tree_code,
99                                  enum tree_code, tree, tree, tree);
100 static int truth_value_p (enum tree_code);
101 static int operand_equal_for_comparison_p (tree, tree, tree);
102 static int twoval_comparison_p (tree, tree *, tree *, int *);
103 static tree eval_subst (tree, tree, tree, tree, tree);
104 static tree pedantic_omit_one_operand (tree, tree, tree);
105 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
106 static tree make_bit_field_ref (tree, tree, int, int, int);
107 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
108 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
109                                     enum machine_mode *, int *, int *,
110                                     tree *, tree *);
111 static int all_ones_mask_p (tree, int);
112 static tree sign_bit_p (tree, tree);
113 static int simple_operand_p (tree);
114 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
115 static tree make_range (tree, int *, tree *, tree *);
116 static tree build_range_check (tree, tree, int, tree, tree);
117 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
118                          tree);
119 static tree fold_range_test (tree);
120 static tree unextend (tree, int, int, tree);
121 static tree fold_truthop (enum tree_code, tree, tree, tree);
122 static tree optimize_minmax_comparison (tree);
123 static tree extract_muldiv (tree, tree, enum tree_code, tree);
124 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
125 static int multiple_of_p (tree, tree, tree);
126 static tree constant_boolean_node (int, tree);
127 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree, tree,
128                                                  tree, int);
129 static bool fold_real_zero_addition_p (tree, tree, int);
130 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
131                                  tree, tree, tree);
132 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
133 static tree fold_div_compare (enum tree_code, tree, tree, tree);
134 static bool reorder_operands_p (tree, tree);
135 static bool tree_swap_operands_p (tree, tree, bool);
136
137 static tree fold_negate_const (tree, tree);
138 static tree fold_abs_const (tree, tree);
139 static tree fold_not_const (tree, tree);
140 static tree fold_relational_const (enum tree_code, tree, tree, tree);
141 static tree fold_relational_hi_lo (enum tree_code *, const tree,
142                                    tree *, tree *);
143
144 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
145    overflow.  Suppose A, B and SUM have the same respective signs as A1, B1,
146    and SUM1.  Then this yields nonzero if overflow occurred during the
147    addition.
148
149    Overflow occurs if A and B have the same sign, but A and SUM differ in
150    sign.  Use `^' to test whether signs differ, and `< 0' to isolate the
151    sign.  */
152 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
153 \f
154 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
155    We do that by representing the two-word integer in 4 words, with only
156    HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
157    number.  The value of the word is LOWPART + HIGHPART * BASE.  */
158
159 #define LOWPART(x) \
160   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
161 #define HIGHPART(x) \
162   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
163 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
164
165 /* Unpack a two-word integer into 4 words.
166    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
167    WORDS points to the array of HOST_WIDE_INTs.  */
168
169 static void
170 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
171 {
172   words[0] = LOWPART (low);
173   words[1] = HIGHPART (low);
174   words[2] = LOWPART (hi);
175   words[3] = HIGHPART (hi);
176 }
177
178 /* Pack an array of 4 words into a two-word integer.
179    WORDS points to the array of words.
180    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
181
182 static void
183 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
184         HOST_WIDE_INT *hi)
185 {
186   *low = words[0] + words[1] * BASE;
187   *hi = words[2] + words[3] * BASE;
188 }
189 \f
190 /* Make the integer constant T valid for its type by setting to 0 or 1 all
191    the bits in the constant that don't belong in the type.
192
193    Return 1 if a signed overflow occurs, 0 otherwise.  If OVERFLOW is
194    nonzero, a signed overflow has already occurred in calculating T, so
195    propagate it.  */
196
197 int
198 force_fit_type (tree t, int overflow)
199 {
200   unsigned HOST_WIDE_INT low;
201   HOST_WIDE_INT high;
202   unsigned int prec;
203
204   if (TREE_CODE (t) == REAL_CST)
205     {
206       /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
207          Consider doing it via real_convert now.  */
208       return overflow;
209     }
210
211   else if (TREE_CODE (t) != INTEGER_CST)
212     return overflow;
213
214   low = TREE_INT_CST_LOW (t);
215   high = TREE_INT_CST_HIGH (t);
216
217   if (POINTER_TYPE_P (TREE_TYPE (t))
218       || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
219     prec = POINTER_SIZE;
220   else
221     prec = TYPE_PRECISION (TREE_TYPE (t));
222
223   /* First clear all bits that are beyond the type's precision.  */
224
225   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
226     ;
227   else if (prec > HOST_BITS_PER_WIDE_INT)
228     TREE_INT_CST_HIGH (t)
229       &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
230   else
231     {
232       TREE_INT_CST_HIGH (t) = 0;
233       if (prec < HOST_BITS_PER_WIDE_INT)
234         TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
235     }
236
237   /* Unsigned types do not suffer sign extension or overflow unless they
238      are a sizetype.  */
239   if (TYPE_UNSIGNED (TREE_TYPE (t))
240       && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
241             && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
242     return overflow;
243
244   /* If the value's sign bit is set, extend the sign.  */
245   if (prec != 2 * HOST_BITS_PER_WIDE_INT
246       && (prec > HOST_BITS_PER_WIDE_INT
247           ? 0 != (TREE_INT_CST_HIGH (t)
248                   & ((HOST_WIDE_INT) 1
249                      << (prec - HOST_BITS_PER_WIDE_INT - 1)))
250           : 0 != (TREE_INT_CST_LOW (t)
251                   & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
252     {
253       /* Value is negative:
254          set to 1 all the bits that are outside this type's precision.  */
255       if (prec > HOST_BITS_PER_WIDE_INT)
256         TREE_INT_CST_HIGH (t)
257           |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
258       else
259         {
260           TREE_INT_CST_HIGH (t) = -1;
261           if (prec < HOST_BITS_PER_WIDE_INT)
262             TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
263         }
264     }
265
266   /* Return nonzero if signed overflow occurred.  */
267   return
268     ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
269      != 0);
270 }
271 \f
272 /* Add two doubleword integers with doubleword result.
273    Each argument is given as two `HOST_WIDE_INT' pieces.
274    One argument is L1 and H1; the other, L2 and H2.
275    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
276
277 int
278 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
279             unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
280             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
281 {
282   unsigned HOST_WIDE_INT l;
283   HOST_WIDE_INT h;
284
285   l = l1 + l2;
286   h = h1 + h2 + (l < l1);
287
288   *lv = l;
289   *hv = h;
290   return OVERFLOW_SUM_SIGN (h1, h2, h);
291 }
292
293 /* Negate a doubleword integer with doubleword result.
294    Return nonzero if the operation overflows, assuming it's signed.
295    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
296    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
297
298 int
299 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
300             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
301 {
302   if (l1 == 0)
303     {
304       *lv = 0;
305       *hv = - h1;
306       return (*hv & h1) < 0;
307     }
308   else
309     {
310       *lv = -l1;
311       *hv = ~h1;
312       return 0;
313     }
314 }
315 \f
316 /* Multiply two doubleword integers with doubleword result.
317    Return nonzero if the operation overflows, assuming it's signed.
318    Each argument is given as two `HOST_WIDE_INT' pieces.
319    One argument is L1 and H1; the other, L2 and H2.
320    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
321
322 int
323 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
324             unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
325             unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
326 {
327   HOST_WIDE_INT arg1[4];
328   HOST_WIDE_INT arg2[4];
329   HOST_WIDE_INT prod[4 * 2];
330   unsigned HOST_WIDE_INT carry;
331   int i, j, k;
332   unsigned HOST_WIDE_INT toplow, neglow;
333   HOST_WIDE_INT tophigh, neghigh;
334
335   encode (arg1, l1, h1);
336   encode (arg2, l2, h2);
337
338   memset (prod, 0, sizeof prod);
339
340   for (i = 0; i < 4; i++)
341     {
342       carry = 0;
343       for (j = 0; j < 4; j++)
344         {
345           k = i + j;
346           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
347           carry += arg1[i] * arg2[j];
348           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
349           carry += prod[k];
350           prod[k] = LOWPART (carry);
351           carry = HIGHPART (carry);
352         }
353       prod[i + 4] = carry;
354     }
355
356   decode (prod, lv, hv);        /* This ignores prod[4] through prod[4*2-1] */
357
358   /* Check for overflow by calculating the top half of the answer in full;
359      it should agree with the low half's sign bit.  */
360   decode (prod + 4, &toplow, &tophigh);
361   if (h1 < 0)
362     {
363       neg_double (l2, h2, &neglow, &neghigh);
364       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
365     }
366   if (h2 < 0)
367     {
368       neg_double (l1, h1, &neglow, &neghigh);
369       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
370     }
371   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
372 }
373 \f
374 /* Shift the doubleword integer in L1, H1 left by COUNT places
375    keeping only PREC bits of result.
376    Shift right if COUNT is negative.
377    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
378    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
379
380 void
381 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
382                HOST_WIDE_INT count, unsigned int prec,
383                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
384 {
385   unsigned HOST_WIDE_INT signmask;
386
387   if (count < 0)
388     {
389       rshift_double (l1, h1, -count, prec, lv, hv, arith);
390       return;
391     }
392
393   if (SHIFT_COUNT_TRUNCATED)
394     count %= prec;
395
396   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
397     {
398       /* Shifting by the host word size is undefined according to the
399          ANSI standard, so we must handle this as a special case.  */
400       *hv = 0;
401       *lv = 0;
402     }
403   else if (count >= HOST_BITS_PER_WIDE_INT)
404     {
405       *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
406       *lv = 0;
407     }
408   else
409     {
410       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
411              | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
412       *lv = l1 << count;
413     }
414
415   /* Sign extend all bits that are beyond the precision.  */
416
417   signmask = -((prec > HOST_BITS_PER_WIDE_INT
418                 ? ((unsigned HOST_WIDE_INT) *hv
419                    >> (prec - HOST_BITS_PER_WIDE_INT - 1))
420                 : (*lv >> (prec - 1))) & 1);
421
422   if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
423     ;
424   else if (prec >= HOST_BITS_PER_WIDE_INT)
425     {
426       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
427       *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
428     }
429   else
430     {
431       *hv = signmask;
432       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
433       *lv |= signmask << prec;
434     }
435 }
436
437 /* Shift the doubleword integer in L1, H1 right by COUNT places
438    keeping only PREC bits of result.  COUNT must be positive.
439    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
440    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
441
442 void
443 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
444                HOST_WIDE_INT count, unsigned int prec,
445                unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
446                int arith)
447 {
448   unsigned HOST_WIDE_INT signmask;
449
450   signmask = (arith
451               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
452               : 0);
453
454   if (SHIFT_COUNT_TRUNCATED)
455     count %= prec;
456
457   if (count >= 2 * HOST_BITS_PER_WIDE_INT)
458     {
459       /* Shifting by the host word size is undefined according to the
460          ANSI standard, so we must handle this as a special case.  */
461       *hv = 0;
462       *lv = 0;
463     }
464   else if (count >= HOST_BITS_PER_WIDE_INT)
465     {
466       *hv = 0;
467       *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
468     }
469   else
470     {
471       *hv = (unsigned HOST_WIDE_INT) h1 >> count;
472       *lv = ((l1 >> count)
473              | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
474     }
475
476   /* Zero / sign extend all bits that are beyond the precision.  */
477
478   if (count >= (HOST_WIDE_INT)prec)
479     {
480       *hv = signmask;
481       *lv = signmask;
482     }
483   else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
484     ;
485   else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
486     {
487       *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
488       *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
489     }
490   else
491     {
492       *hv = signmask;
493       *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
494       *lv |= signmask << (prec - count);
495     }
496 }
497 \f
498 /* Rotate the doubleword integer in L1, H1 left by COUNT places
499    keeping only PREC bits of result.
500    Rotate right if COUNT is negative.
501    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
502
503 void
504 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
505                 HOST_WIDE_INT count, unsigned int prec,
506                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
507 {
508   unsigned HOST_WIDE_INT s1l, s2l;
509   HOST_WIDE_INT s1h, s2h;
510
511   count %= prec;
512   if (count < 0)
513     count += prec;
514
515   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
516   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
517   *lv = s1l | s2l;
518   *hv = s1h | s2h;
519 }
520
521 /* Rotate the doubleword integer in L1, H1 left by COUNT places
522    keeping only PREC bits of result.  COUNT must be positive.
523    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
524
525 void
526 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
527                 HOST_WIDE_INT count, unsigned int prec,
528                 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
529 {
530   unsigned HOST_WIDE_INT s1l, s2l;
531   HOST_WIDE_INT s1h, s2h;
532
533   count %= prec;
534   if (count < 0)
535     count += prec;
536
537   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
538   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
539   *lv = s1l | s2l;
540   *hv = s1h | s2h;
541 }
542 \f
543 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
544    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
545    CODE is a tree code for a kind of division, one of
546    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
547    or EXACT_DIV_EXPR
548    It controls how the quotient is rounded to an integer.
549    Return nonzero if the operation overflows.
550    UNS nonzero says do unsigned division.  */
551
552 int
553 div_and_round_double (enum tree_code code, int uns,
554                       unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
555                       HOST_WIDE_INT hnum_orig,
556                       unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
557                       HOST_WIDE_INT hden_orig,
558                       unsigned HOST_WIDE_INT *lquo,
559                       HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
560                       HOST_WIDE_INT *hrem)
561 {
562   int quo_neg = 0;
563   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
564   HOST_WIDE_INT den[4], quo[4];
565   int i, j;
566   unsigned HOST_WIDE_INT work;
567   unsigned HOST_WIDE_INT carry = 0;
568   unsigned HOST_WIDE_INT lnum = lnum_orig;
569   HOST_WIDE_INT hnum = hnum_orig;
570   unsigned HOST_WIDE_INT lden = lden_orig;
571   HOST_WIDE_INT hden = hden_orig;
572   int overflow = 0;
573
574   if (hden == 0 && lden == 0)
575     overflow = 1, lden = 1;
576
577   /* Calculate quotient sign and convert operands to unsigned.  */
578   if (!uns)
579     {
580       if (hnum < 0)
581         {
582           quo_neg = ~ quo_neg;
583           /* (minimum integer) / (-1) is the only overflow case.  */
584           if (neg_double (lnum, hnum, &lnum, &hnum)
585               && ((HOST_WIDE_INT) lden & hden) == -1)
586             overflow = 1;
587         }
588       if (hden < 0)
589         {
590           quo_neg = ~ quo_neg;
591           neg_double (lden, hden, &lden, &hden);
592         }
593     }
594
595   if (hnum == 0 && hden == 0)
596     {                           /* single precision */
597       *hquo = *hrem = 0;
598       /* This unsigned division rounds toward zero.  */
599       *lquo = lnum / lden;
600       goto finish_up;
601     }
602
603   if (hnum == 0)
604     {                           /* trivial case: dividend < divisor */
605       /* hden != 0 already checked.  */
606       *hquo = *lquo = 0;
607       *hrem = hnum;
608       *lrem = lnum;
609       goto finish_up;
610     }
611
612   memset (quo, 0, sizeof quo);
613
614   memset (num, 0, sizeof num);  /* to zero 9th element */
615   memset (den, 0, sizeof den);
616
617   encode (num, lnum, hnum);
618   encode (den, lden, hden);
619
620   /* Special code for when the divisor < BASE.  */
621   if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
622     {
623       /* hnum != 0 already checked.  */
624       for (i = 4 - 1; i >= 0; i--)
625         {
626           work = num[i] + carry * BASE;
627           quo[i] = work / lden;
628           carry = work % lden;
629         }
630     }
631   else
632     {
633       /* Full double precision division,
634          with thanks to Don Knuth's "Seminumerical Algorithms".  */
635       int num_hi_sig, den_hi_sig;
636       unsigned HOST_WIDE_INT quo_est, scale;
637
638       /* Find the highest nonzero divisor digit.  */
639       for (i = 4 - 1;; i--)
640         if (den[i] != 0)
641           {
642             den_hi_sig = i;
643             break;
644           }
645
646       /* Insure that the first digit of the divisor is at least BASE/2.
647          This is required by the quotient digit estimation algorithm.  */
648
649       scale = BASE / (den[den_hi_sig] + 1);
650       if (scale > 1)
651         {               /* scale divisor and dividend */
652           carry = 0;
653           for (i = 0; i <= 4 - 1; i++)
654             {
655               work = (num[i] * scale) + carry;
656               num[i] = LOWPART (work);
657               carry = HIGHPART (work);
658             }
659
660           num[4] = carry;
661           carry = 0;
662           for (i = 0; i <= 4 - 1; i++)
663             {
664               work = (den[i] * scale) + carry;
665               den[i] = LOWPART (work);
666               carry = HIGHPART (work);
667               if (den[i] != 0) den_hi_sig = i;
668             }
669         }
670
671       num_hi_sig = 4;
672
673       /* Main loop */
674       for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
675         {
676           /* Guess the next quotient digit, quo_est, by dividing the first
677              two remaining dividend digits by the high order quotient digit.
678              quo_est is never low and is at most 2 high.  */
679           unsigned HOST_WIDE_INT tmp;
680
681           num_hi_sig = i + den_hi_sig + 1;
682           work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
683           if (num[num_hi_sig] != den[den_hi_sig])
684             quo_est = work / den[den_hi_sig];
685           else
686             quo_est = BASE - 1;
687
688           /* Refine quo_est so it's usually correct, and at most one high.  */
689           tmp = work - quo_est * den[den_hi_sig];
690           if (tmp < BASE
691               && (den[den_hi_sig - 1] * quo_est
692                   > (tmp * BASE + num[num_hi_sig - 2])))
693             quo_est--;
694
695           /* Try QUO_EST as the quotient digit, by multiplying the
696              divisor by QUO_EST and subtracting from the remaining dividend.
697              Keep in mind that QUO_EST is the I - 1st digit.  */
698
699           carry = 0;
700           for (j = 0; j <= den_hi_sig; j++)
701             {
702               work = quo_est * den[j] + carry;
703               carry = HIGHPART (work);
704               work = num[i + j] - LOWPART (work);
705               num[i + j] = LOWPART (work);
706               carry += HIGHPART (work) != 0;
707             }
708
709           /* If quo_est was high by one, then num[i] went negative and
710              we need to correct things.  */
711           if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
712             {
713               quo_est--;
714               carry = 0;                /* add divisor back in */
715               for (j = 0; j <= den_hi_sig; j++)
716                 {
717                   work = num[i + j] + den[j] + carry;
718                   carry = HIGHPART (work);
719                   num[i + j] = LOWPART (work);
720                 }
721
722               num [num_hi_sig] += carry;
723             }
724
725           /* Store the quotient digit.  */
726           quo[i] = quo_est;
727         }
728     }
729
730   decode (quo, lquo, hquo);
731
732  finish_up:
733   /* If result is negative, make it so.  */
734   if (quo_neg)
735     neg_double (*lquo, *hquo, lquo, hquo);
736
737   /* Compute trial remainder:  rem = num - (quo * den)  */
738   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
739   neg_double (*lrem, *hrem, lrem, hrem);
740   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
741
742   switch (code)
743     {
744     case TRUNC_DIV_EXPR:
745     case TRUNC_MOD_EXPR:        /* round toward zero */
746     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
747       return overflow;
748
749     case FLOOR_DIV_EXPR:
750     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
751       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
752         {
753           /* quo = quo - 1;  */
754           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
755                       lquo, hquo);
756         }
757       else
758         return overflow;
759       break;
760
761     case CEIL_DIV_EXPR:
762     case CEIL_MOD_EXPR:         /* round toward positive infinity */
763       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
764         {
765           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
766                       lquo, hquo);
767         }
768       else
769         return overflow;
770       break;
771
772     case ROUND_DIV_EXPR:
773     case ROUND_MOD_EXPR:        /* round to closest integer */
774       {
775         unsigned HOST_WIDE_INT labs_rem = *lrem;
776         HOST_WIDE_INT habs_rem = *hrem;
777         unsigned HOST_WIDE_INT labs_den = lden, ltwice;
778         HOST_WIDE_INT habs_den = hden, htwice;
779
780         /* Get absolute values.  */
781         if (*hrem < 0)
782           neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
783         if (hden < 0)
784           neg_double (lden, hden, &labs_den, &habs_den);
785
786         /* If (2 * abs (lrem) >= abs (lden)) */
787         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
788                     labs_rem, habs_rem, &ltwice, &htwice);
789
790         if (((unsigned HOST_WIDE_INT) habs_den
791              < (unsigned HOST_WIDE_INT) htwice)
792             || (((unsigned HOST_WIDE_INT) habs_den
793                  == (unsigned HOST_WIDE_INT) htwice)
794                 && (labs_den < ltwice)))
795           {
796             if (*hquo < 0)
797               /* quo = quo - 1;  */
798               add_double (*lquo, *hquo,
799                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
800             else
801               /* quo = quo + 1; */
802               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
803                           lquo, hquo);
804           }
805         else
806           return overflow;
807       }
808       break;
809
810     default:
811       abort ();
812     }
813
814   /* Compute true remainder:  rem = num - (quo * den)  */
815   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
816   neg_double (*lrem, *hrem, lrem, hrem);
817   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
818   return overflow;
819 }
820 \f
821 /* Return true if built-in mathematical function specified by CODE
822    preserves the sign of it argument, i.e. -f(x) == f(-x).  */
823
824 static bool
825 negate_mathfn_p (enum built_in_function code)
826 {
827   switch (code)
828     {
829     case BUILT_IN_ASIN:
830     case BUILT_IN_ASINF:
831     case BUILT_IN_ASINL:
832     case BUILT_IN_ATAN:
833     case BUILT_IN_ATANF:
834     case BUILT_IN_ATANL:
835     case BUILT_IN_SIN:
836     case BUILT_IN_SINF:
837     case BUILT_IN_SINL:
838     case BUILT_IN_TAN:
839     case BUILT_IN_TANF:
840     case BUILT_IN_TANL:
841       return true;
842
843     default:
844       break;
845     }
846   return false;
847 }
848
849 /* Determine whether an expression T can be cheaply negated using
850    the function negate_expr.  */
851
852 static bool
853 negate_expr_p (tree t)
854 {
855   unsigned HOST_WIDE_INT val;
856   unsigned int prec;
857   tree type;
858
859   if (t == 0)
860     return false;
861
862   type = TREE_TYPE (t);
863
864   STRIP_SIGN_NOPS (t);
865   switch (TREE_CODE (t))
866     {
867     case INTEGER_CST:
868       if (TYPE_UNSIGNED (type) || ! flag_trapv)
869         return true;
870
871       /* Check that -CST will not overflow type.  */
872       prec = TYPE_PRECISION (type);
873       if (prec > HOST_BITS_PER_WIDE_INT)
874         {
875           if (TREE_INT_CST_LOW (t) != 0)
876             return true;
877           prec -= HOST_BITS_PER_WIDE_INT;
878           val = TREE_INT_CST_HIGH (t);
879         }
880       else
881         val = TREE_INT_CST_LOW (t);
882       if (prec < HOST_BITS_PER_WIDE_INT)
883         val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
884       return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
885
886     case REAL_CST:
887     case NEGATE_EXPR:
888       return true;
889
890     case COMPLEX_CST:
891       return negate_expr_p (TREE_REALPART (t))
892              && negate_expr_p (TREE_IMAGPART (t));
893
894     case PLUS_EXPR:
895       if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
896         return false;
897       /* -(A + B) -> (-B) - A.  */
898       if (negate_expr_p (TREE_OPERAND (t, 1))
899           && reorder_operands_p (TREE_OPERAND (t, 0),
900                                  TREE_OPERAND (t, 1)))
901         return true;
902       /* -(A + B) -> (-A) - B.  */
903       return negate_expr_p (TREE_OPERAND (t, 0));
904
905     case MINUS_EXPR:
906       /* We can't turn -(A-B) into B-A when we honor signed zeros.  */
907       return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
908              && reorder_operands_p (TREE_OPERAND (t, 0),
909                                     TREE_OPERAND (t, 1));
910
911     case MULT_EXPR:
912       if (TYPE_UNSIGNED (TREE_TYPE (t)))
913         break;
914
915       /* Fall through.  */
916
917     case RDIV_EXPR:
918       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
919         return negate_expr_p (TREE_OPERAND (t, 1))
920                || negate_expr_p (TREE_OPERAND (t, 0));
921       break;
922
923     case NOP_EXPR:
924       /* Negate -((double)float) as (double)(-float).  */
925       if (TREE_CODE (type) == REAL_TYPE)
926         {
927           tree tem = strip_float_extensions (t);
928           if (tem != t)
929             return negate_expr_p (tem);
930         }
931       break;
932
933     case CALL_EXPR:
934       /* Negate -f(x) as f(-x).  */
935       if (negate_mathfn_p (builtin_mathfn_code (t)))
936         return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
937       break;
938
939     case RSHIFT_EXPR:
940       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
941       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
942         {
943           tree op1 = TREE_OPERAND (t, 1);
944           if (TREE_INT_CST_HIGH (op1) == 0
945               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
946                  == TREE_INT_CST_LOW (op1))
947             return true;
948         }
949       break;
950
951     default:
952       break;
953     }
954   return false;
955 }
956
957 /* Given T, an expression, return the negation of T.  Allow for T to be
958    null, in which case return null.  */
959
960 static tree
961 negate_expr (tree t)
962 {
963   tree type;
964   tree tem;
965
966   if (t == 0)
967     return 0;
968
969   type = TREE_TYPE (t);
970   STRIP_SIGN_NOPS (t);
971
972   switch (TREE_CODE (t))
973     {
974     case INTEGER_CST:
975       tem = fold_negate_const (t, type);
976       if (! TREE_OVERFLOW (tem)
977           || TYPE_UNSIGNED (type)
978           || ! flag_trapv)
979         return tem;
980       break;
981
982     case REAL_CST:
983       tem = fold_negate_const (t, type);
984       /* Two's complement FP formats, such as c4x, may overflow.  */
985       if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
986         return fold_convert (type, tem);
987       break;
988
989     case COMPLEX_CST:
990       {
991         tree rpart = negate_expr (TREE_REALPART (t));
992         tree ipart = negate_expr (TREE_IMAGPART (t));
993
994         if ((TREE_CODE (rpart) == REAL_CST
995              && TREE_CODE (ipart) == REAL_CST)
996             || (TREE_CODE (rpart) == INTEGER_CST
997                 && TREE_CODE (ipart) == INTEGER_CST))
998           return build_complex (type, rpart, ipart);
999       }
1000       break;
1001
1002     case NEGATE_EXPR:
1003       return fold_convert (type, TREE_OPERAND (t, 0));
1004
1005     case PLUS_EXPR:
1006       if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1007         {
1008           /* -(A + B) -> (-B) - A.  */
1009           if (negate_expr_p (TREE_OPERAND (t, 1))
1010               && reorder_operands_p (TREE_OPERAND (t, 0),
1011                                      TREE_OPERAND (t, 1)))
1012             {
1013               tem = negate_expr (TREE_OPERAND (t, 1));
1014               tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1015                                   tem, TREE_OPERAND (t, 0)));
1016               return fold_convert (type, tem);
1017             }
1018
1019           /* -(A + B) -> (-A) - B.  */
1020           if (negate_expr_p (TREE_OPERAND (t, 0)))
1021             {
1022               tem = negate_expr (TREE_OPERAND (t, 0));
1023               tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1024                                   tem, TREE_OPERAND (t, 1)));
1025               return fold_convert (type, tem);
1026             }
1027         }
1028       break;
1029
1030     case MINUS_EXPR:
1031       /* - (A - B) -> B - A  */
1032       if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1033           && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1034         return fold_convert (type,
1035                              fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1036                                            TREE_OPERAND (t, 1),
1037                                            TREE_OPERAND (t, 0))));
1038       break;
1039
1040     case MULT_EXPR:
1041       if (TYPE_UNSIGNED (TREE_TYPE (t)))
1042         break;
1043
1044       /* Fall through.  */
1045
1046     case RDIV_EXPR:
1047       if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1048         {
1049           tem = TREE_OPERAND (t, 1);
1050           if (negate_expr_p (tem))
1051             return fold_convert (type,
1052                                  fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1053                                                TREE_OPERAND (t, 0),
1054                                                negate_expr (tem))));
1055           tem = TREE_OPERAND (t, 0);
1056           if (negate_expr_p (tem))
1057             return fold_convert (type,
1058                                  fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1059                                                negate_expr (tem),
1060                                                TREE_OPERAND (t, 1))));
1061         }
1062       break;
1063
1064     case NOP_EXPR:
1065       /* Convert -((double)float) into (double)(-float).  */
1066       if (TREE_CODE (type) == REAL_TYPE)
1067         {
1068           tem = strip_float_extensions (t);
1069           if (tem != t && negate_expr_p (tem))
1070             return fold_convert (type, negate_expr (tem));
1071         }
1072       break;
1073
1074     case CALL_EXPR:
1075       /* Negate -f(x) as f(-x).  */
1076       if (negate_mathfn_p (builtin_mathfn_code (t))
1077           && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1078         {
1079           tree fndecl, arg, arglist;
1080
1081           fndecl = get_callee_fndecl (t);
1082           arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1083           arglist = build_tree_list (NULL_TREE, arg);
1084           return build_function_call_expr (fndecl, arglist);
1085         }
1086       break;
1087
1088     case RSHIFT_EXPR:
1089       /* Optimize -((int)x >> 31) into (unsigned)x >> 31.  */
1090       if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1091         {
1092           tree op1 = TREE_OPERAND (t, 1);
1093           if (TREE_INT_CST_HIGH (op1) == 0
1094               && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1095                  == TREE_INT_CST_LOW (op1))
1096             {
1097               tree ntype = TYPE_UNSIGNED (type)
1098                            ? lang_hooks.types.signed_type (type)
1099                            : lang_hooks.types.unsigned_type (type);
1100               tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1101               temp = fold (build2 (RSHIFT_EXPR, ntype, temp, op1));
1102               return fold_convert (type, temp);
1103             }
1104         }
1105       break;
1106
1107     default:
1108       break;
1109     }
1110
1111   tem = fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1112   return fold_convert (type, tem);
1113 }
1114 \f
1115 /* Split a tree IN into a constant, literal and variable parts that could be
1116    combined with CODE to make IN.  "constant" means an expression with
1117    TREE_CONSTANT but that isn't an actual constant.  CODE must be a
1118    commutative arithmetic operation.  Store the constant part into *CONP,
1119    the literal in *LITP and return the variable part.  If a part isn't
1120    present, set it to null.  If the tree does not decompose in this way,
1121    return the entire tree as the variable part and the other parts as null.
1122
1123    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.  In that
1124    case, we negate an operand that was subtracted.  Except if it is a
1125    literal for which we use *MINUS_LITP instead.
1126
1127    If NEGATE_P is true, we are negating all of IN, again except a literal
1128    for which we use *MINUS_LITP instead.
1129
1130    If IN is itself a literal or constant, return it as appropriate.
1131
1132    Note that we do not guarantee that any of the three values will be the
1133    same type as IN, but they will have the same signedness and mode.  */
1134
1135 static tree
1136 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1137             tree *minus_litp, int negate_p)
1138 {
1139   tree var = 0;
1140
1141   *conp = 0;
1142   *litp = 0;
1143   *minus_litp = 0;
1144
1145   /* Strip any conversions that don't change the machine mode or signedness.  */
1146   STRIP_SIGN_NOPS (in);
1147
1148   if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1149     *litp = in;
1150   else if (TREE_CODE (in) == code
1151            || (! FLOAT_TYPE_P (TREE_TYPE (in))
1152                /* We can associate addition and subtraction together (even
1153                   though the C standard doesn't say so) for integers because
1154                   the value is not affected.  For reals, the value might be
1155                   affected, so we can't.  */
1156                && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1157                    || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1158     {
1159       tree op0 = TREE_OPERAND (in, 0);
1160       tree op1 = TREE_OPERAND (in, 1);
1161       int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1162       int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1163
1164       /* First see if either of the operands is a literal, then a constant.  */
1165       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1166         *litp = op0, op0 = 0;
1167       else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1168         *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1169
1170       if (op0 != 0 && TREE_CONSTANT (op0))
1171         *conp = op0, op0 = 0;
1172       else if (op1 != 0 && TREE_CONSTANT (op1))
1173         *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1174
1175       /* If we haven't dealt with either operand, this is not a case we can
1176          decompose.  Otherwise, VAR is either of the ones remaining, if any.  */
1177       if (op0 != 0 && op1 != 0)
1178         var = in;
1179       else if (op0 != 0)
1180         var = op0;
1181       else
1182         var = op1, neg_var_p = neg1_p;
1183
1184       /* Now do any needed negations.  */
1185       if (neg_litp_p)
1186         *minus_litp = *litp, *litp = 0;
1187       if (neg_conp_p)
1188         *conp = negate_expr (*conp);
1189       if (neg_var_p)
1190         var = negate_expr (var);
1191     }
1192   else if (TREE_CONSTANT (in))
1193     *conp = in;
1194   else
1195     var = in;
1196
1197   if (negate_p)
1198     {
1199       if (*litp)
1200         *minus_litp = *litp, *litp = 0;
1201       else if (*minus_litp)
1202         *litp = *minus_litp, *minus_litp = 0;
1203       *conp = negate_expr (*conp);
1204       var = negate_expr (var);
1205     }
1206
1207   return var;
1208 }
1209
1210 /* Re-associate trees split by the above function.  T1 and T2 are either
1211    expressions to associate or null.  Return the new expression, if any.  If
1212    we build an operation, do it in TYPE and with CODE.  */
1213
1214 static tree
1215 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1216 {
1217   if (t1 == 0)
1218     return t2;
1219   else if (t2 == 0)
1220     return t1;
1221
1222   /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1223      try to fold this since we will have infinite recursion.  But do
1224      deal with any NEGATE_EXPRs.  */
1225   if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1226       || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1227     {
1228       if (code == PLUS_EXPR)
1229         {
1230           if (TREE_CODE (t1) == NEGATE_EXPR)
1231             return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1232                            fold_convert (type, TREE_OPERAND (t1, 0)));
1233           else if (TREE_CODE (t2) == NEGATE_EXPR)
1234             return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1235                            fold_convert (type, TREE_OPERAND (t2, 0)));
1236         }
1237       return build2 (code, type, fold_convert (type, t1),
1238                      fold_convert (type, t2));
1239     }
1240
1241   return fold (build2 (code, type, fold_convert (type, t1),
1242                        fold_convert (type, t2)));
1243 }
1244 \f
1245 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1246    to produce a new constant.
1247
1248    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1249
1250 tree
1251 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1252 {
1253   unsigned HOST_WIDE_INT int1l, int2l;
1254   HOST_WIDE_INT int1h, int2h;
1255   unsigned HOST_WIDE_INT low;
1256   HOST_WIDE_INT hi;
1257   unsigned HOST_WIDE_INT garbagel;
1258   HOST_WIDE_INT garbageh;
1259   tree t;
1260   tree type = TREE_TYPE (arg1);
1261   int uns = TYPE_UNSIGNED (type);
1262   int is_sizetype
1263     = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1264   int overflow = 0;
1265   int no_overflow = 0;
1266
1267   int1l = TREE_INT_CST_LOW (arg1);
1268   int1h = TREE_INT_CST_HIGH (arg1);
1269   int2l = TREE_INT_CST_LOW (arg2);
1270   int2h = TREE_INT_CST_HIGH (arg2);
1271
1272   switch (code)
1273     {
1274     case BIT_IOR_EXPR:
1275       low = int1l | int2l, hi = int1h | int2h;
1276       break;
1277
1278     case BIT_XOR_EXPR:
1279       low = int1l ^ int2l, hi = int1h ^ int2h;
1280       break;
1281
1282     case BIT_AND_EXPR:
1283       low = int1l & int2l, hi = int1h & int2h;
1284       break;
1285
1286     case RSHIFT_EXPR:
1287       int2l = -int2l;
1288     case LSHIFT_EXPR:
1289       /* It's unclear from the C standard whether shifts can overflow.
1290          The following code ignores overflow; perhaps a C standard
1291          interpretation ruling is needed.  */
1292       lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1293                      &low, &hi, !uns);
1294       no_overflow = 1;
1295       break;
1296
1297     case RROTATE_EXPR:
1298       int2l = - int2l;
1299     case LROTATE_EXPR:
1300       lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1301                       &low, &hi);
1302       break;
1303
1304     case PLUS_EXPR:
1305       overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1306       break;
1307
1308     case MINUS_EXPR:
1309       neg_double (int2l, int2h, &low, &hi);
1310       add_double (int1l, int1h, low, hi, &low, &hi);
1311       overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1312       break;
1313
1314     case MULT_EXPR:
1315       overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1316       break;
1317
1318     case TRUNC_DIV_EXPR:
1319     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1320     case EXACT_DIV_EXPR:
1321       /* This is a shortcut for a common special case.  */
1322       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1323           && ! TREE_CONSTANT_OVERFLOW (arg1)
1324           && ! TREE_CONSTANT_OVERFLOW (arg2)
1325           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1326         {
1327           if (code == CEIL_DIV_EXPR)
1328             int1l += int2l - 1;
1329
1330           low = int1l / int2l, hi = 0;
1331           break;
1332         }
1333
1334       /* ... fall through ...  */
1335
1336     case ROUND_DIV_EXPR:
1337       if (int2h == 0 && int2l == 1)
1338         {
1339           low = int1l, hi = int1h;
1340           break;
1341         }
1342       if (int1l == int2l && int1h == int2h
1343           && ! (int1l == 0 && int1h == 0))
1344         {
1345           low = 1, hi = 0;
1346           break;
1347         }
1348       overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1349                                        &low, &hi, &garbagel, &garbageh);
1350       break;
1351
1352     case TRUNC_MOD_EXPR:
1353     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1354       /* This is a shortcut for a common special case.  */
1355       if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1356           && ! TREE_CONSTANT_OVERFLOW (arg1)
1357           && ! TREE_CONSTANT_OVERFLOW (arg2)
1358           && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1359         {
1360           if (code == CEIL_MOD_EXPR)
1361             int1l += int2l - 1;
1362           low = int1l % int2l, hi = 0;
1363           break;
1364         }
1365
1366       /* ... fall through ...  */
1367
1368     case ROUND_MOD_EXPR:
1369       overflow = div_and_round_double (code, uns,
1370                                        int1l, int1h, int2l, int2h,
1371                                        &garbagel, &garbageh, &low, &hi);
1372       break;
1373
1374     case MIN_EXPR:
1375     case MAX_EXPR:
1376       if (uns)
1377         low = (((unsigned HOST_WIDE_INT) int1h
1378                 < (unsigned HOST_WIDE_INT) int2h)
1379                || (((unsigned HOST_WIDE_INT) int1h
1380                     == (unsigned HOST_WIDE_INT) int2h)
1381                    && int1l < int2l));
1382       else
1383         low = (int1h < int2h
1384                || (int1h == int2h && int1l < int2l));
1385
1386       if (low == (code == MIN_EXPR))
1387         low = int1l, hi = int1h;
1388       else
1389         low = int2l, hi = int2h;
1390       break;
1391
1392     default:
1393       abort ();
1394     }
1395
1396   /* If this is for a sizetype, can be represented as one (signed)
1397      HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1398      constants.  */
1399   if (is_sizetype
1400       && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1401           || (hi == -1 && (HOST_WIDE_INT) low < 0))
1402       && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1403     return size_int_type_wide (low, type);
1404   else
1405     {
1406       t = build_int_2 (low, hi);
1407       TREE_TYPE (t) = TREE_TYPE (arg1);
1408     }
1409
1410   TREE_OVERFLOW (t)
1411     = ((notrunc
1412         ? (!uns || is_sizetype) && overflow
1413         : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1414            && ! no_overflow))
1415        | TREE_OVERFLOW (arg1)
1416        | TREE_OVERFLOW (arg2));
1417
1418   /* If we're doing a size calculation, unsigned arithmetic does overflow.
1419      So check if force_fit_type truncated the value.  */
1420   if (is_sizetype
1421       && ! TREE_OVERFLOW (t)
1422       && (TREE_INT_CST_HIGH (t) != hi
1423           || TREE_INT_CST_LOW (t) != low))
1424     TREE_OVERFLOW (t) = 1;
1425
1426   TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1427                                 | TREE_CONSTANT_OVERFLOW (arg1)
1428                                 | TREE_CONSTANT_OVERFLOW (arg2));
1429   return t;
1430 }
1431
1432 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1433    constant.  We assume ARG1 and ARG2 have the same data type, or at least
1434    are the same kind of constant and the same machine mode.
1435
1436    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1437
1438 static tree
1439 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1440 {
1441   STRIP_NOPS (arg1);
1442   STRIP_NOPS (arg2);
1443
1444   if (TREE_CODE (arg1) == INTEGER_CST)
1445     return int_const_binop (code, arg1, arg2, notrunc);
1446
1447   if (TREE_CODE (arg1) == REAL_CST)
1448     {
1449       enum machine_mode mode;
1450       REAL_VALUE_TYPE d1;
1451       REAL_VALUE_TYPE d2;
1452       REAL_VALUE_TYPE value;
1453       tree t, type;
1454
1455       d1 = TREE_REAL_CST (arg1);
1456       d2 = TREE_REAL_CST (arg2);
1457
1458       type = TREE_TYPE (arg1);
1459       mode = TYPE_MODE (type);
1460
1461       /* Don't perform operation if we honor signaling NaNs and
1462          either operand is a NaN.  */
1463       if (HONOR_SNANS (mode)
1464           && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1465         return NULL_TREE;
1466
1467       /* Don't perform operation if it would raise a division
1468          by zero exception.  */
1469       if (code == RDIV_EXPR
1470           && REAL_VALUES_EQUAL (d2, dconst0)
1471           && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1472         return NULL_TREE;
1473
1474       /* If either operand is a NaN, just return it.  Otherwise, set up
1475          for floating-point trap; we return an overflow.  */
1476       if (REAL_VALUE_ISNAN (d1))
1477         return arg1;
1478       else if (REAL_VALUE_ISNAN (d2))
1479         return arg2;
1480
1481       REAL_ARITHMETIC (value, code, d1, d2);
1482
1483       t = build_real (type, real_value_truncate (mode, value));
1484
1485       TREE_OVERFLOW (t)
1486         = (force_fit_type (t, 0)
1487            | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1488       TREE_CONSTANT_OVERFLOW (t)
1489         = TREE_OVERFLOW (t)
1490           | TREE_CONSTANT_OVERFLOW (arg1)
1491           | TREE_CONSTANT_OVERFLOW (arg2);
1492       return t;
1493     }
1494   if (TREE_CODE (arg1) == COMPLEX_CST)
1495     {
1496       tree type = TREE_TYPE (arg1);
1497       tree r1 = TREE_REALPART (arg1);
1498       tree i1 = TREE_IMAGPART (arg1);
1499       tree r2 = TREE_REALPART (arg2);
1500       tree i2 = TREE_IMAGPART (arg2);
1501       tree t;
1502
1503       switch (code)
1504         {
1505         case PLUS_EXPR:
1506           t = build_complex (type,
1507                              const_binop (PLUS_EXPR, r1, r2, notrunc),
1508                              const_binop (PLUS_EXPR, i1, i2, notrunc));
1509           break;
1510
1511         case MINUS_EXPR:
1512           t = build_complex (type,
1513                              const_binop (MINUS_EXPR, r1, r2, notrunc),
1514                              const_binop (MINUS_EXPR, i1, i2, notrunc));
1515           break;
1516
1517         case MULT_EXPR:
1518           t = build_complex (type,
1519                              const_binop (MINUS_EXPR,
1520                                           const_binop (MULT_EXPR,
1521                                                        r1, r2, notrunc),
1522                                           const_binop (MULT_EXPR,
1523                                                        i1, i2, notrunc),
1524                                           notrunc),
1525                              const_binop (PLUS_EXPR,
1526                                           const_binop (MULT_EXPR,
1527                                                        r1, i2, notrunc),
1528                                           const_binop (MULT_EXPR,
1529                                                        i1, r2, notrunc),
1530                                           notrunc));
1531           break;
1532
1533         case RDIV_EXPR:
1534           {
1535             tree magsquared
1536               = const_binop (PLUS_EXPR,
1537                              const_binop (MULT_EXPR, r2, r2, notrunc),
1538                              const_binop (MULT_EXPR, i2, i2, notrunc),
1539                              notrunc);
1540
1541             t = build_complex (type,
1542                                const_binop
1543                                (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1544                                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1545                                 const_binop (PLUS_EXPR,
1546                                              const_binop (MULT_EXPR, r1, r2,
1547                                                           notrunc),
1548                                              const_binop (MULT_EXPR, i1, i2,
1549                                                           notrunc),
1550                                              notrunc),
1551                                 magsquared, notrunc),
1552                                const_binop
1553                                (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1554                                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1555                                 const_binop (MINUS_EXPR,
1556                                              const_binop (MULT_EXPR, i1, r2,
1557                                                           notrunc),
1558                                              const_binop (MULT_EXPR, r1, i2,
1559                                                           notrunc),
1560                                              notrunc),
1561                                 magsquared, notrunc));
1562           }
1563           break;
1564
1565         default:
1566           abort ();
1567         }
1568       return t;
1569     }
1570   return 0;
1571 }
1572
1573 /* These are the hash table functions for the hash table of INTEGER_CST
1574    nodes of a sizetype.  */
1575
1576 /* Return the hash code code X, an INTEGER_CST.  */
1577
1578 static hashval_t
1579 size_htab_hash (const void *x)
1580 {
1581   tree t = (tree) x;
1582
1583   return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1584           ^ htab_hash_pointer (TREE_TYPE (t))
1585           ^ (TREE_OVERFLOW (t) << 20));
1586 }
1587
1588 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1589    is the same as that given by *Y, which is the same.  */
1590
1591 static int
1592 size_htab_eq (const void *x, const void *y)
1593 {
1594   tree xt = (tree) x;
1595   tree yt = (tree) y;
1596
1597   return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1598           && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1599           && TREE_TYPE (xt) == TREE_TYPE (yt)
1600           && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1601 }
1602 \f
1603 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1604    bits are given by NUMBER and of the sizetype represented by KIND.  */
1605
1606 tree
1607 size_int_wide (HOST_WIDE_INT number, enum size_type_kind kind)
1608 {
1609   return size_int_type_wide (number, sizetype_tab[(int) kind]);
1610 }
1611
1612 /* Likewise, but the desired type is specified explicitly.  */
1613
1614 static GTY (()) tree new_const;
1615 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1616      htab_t size_htab;
1617
1618 tree
1619 size_int_type_wide (HOST_WIDE_INT number, tree type)
1620 {
1621   void **slot;
1622
1623   if (size_htab == 0)
1624     {
1625       size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1626       new_const = make_node (INTEGER_CST);
1627     }
1628
1629   /* Adjust NEW_CONST to be the constant we want.  If it's already in the
1630      hash table, we return the value from the hash table.  Otherwise, we
1631      place that in the hash table and make a new node for the next time.  */
1632   TREE_INT_CST_LOW (new_const) = number;
1633   TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1634   TREE_TYPE (new_const) = type;
1635   TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1636     = force_fit_type (new_const, 0);
1637
1638   slot = htab_find_slot (size_htab, new_const, INSERT);
1639   if (*slot == 0)
1640     {
1641       tree t = new_const;
1642
1643       *slot = new_const;
1644       new_const = make_node (INTEGER_CST);
1645       return t;
1646     }
1647   else
1648     return (tree) *slot;
1649 }
1650
1651 /* Combine operands OP1 and OP2 with arithmetic operation CODE.  CODE
1652    is a tree code.  The type of the result is taken from the operands.
1653    Both must be the same type integer type and it must be a size type.
1654    If the operands are constant, so is the result.  */
1655
1656 tree
1657 size_binop (enum tree_code code, tree arg0, tree arg1)
1658 {
1659   tree type = TREE_TYPE (arg0);
1660
1661   if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1662       || type != TREE_TYPE (arg1))
1663     abort ();
1664
1665   /* Handle the special case of two integer constants faster.  */
1666   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1667     {
1668       /* And some specific cases even faster than that.  */
1669       if (code == PLUS_EXPR && integer_zerop (arg0))
1670         return arg1;
1671       else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1672                && integer_zerop (arg1))
1673         return arg0;
1674       else if (code == MULT_EXPR && integer_onep (arg0))
1675         return arg1;
1676
1677       /* Handle general case of two integer constants.  */
1678       return int_const_binop (code, arg0, arg1, 0);
1679     }
1680
1681   if (arg0 == error_mark_node || arg1 == error_mark_node)
1682     return error_mark_node;
1683
1684   return fold (build2 (code, type, arg0, arg1));
1685 }
1686
1687 /* Given two values, either both of sizetype or both of bitsizetype,
1688    compute the difference between the two values.  Return the value
1689    in signed type corresponding to the type of the operands.  */
1690
1691 tree
1692 size_diffop (tree arg0, tree arg1)
1693 {
1694   tree type = TREE_TYPE (arg0);
1695   tree ctype;
1696
1697   if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1698       || type != TREE_TYPE (arg1))
1699     abort ();
1700
1701   /* If the type is already signed, just do the simple thing.  */
1702   if (!TYPE_UNSIGNED (type))
1703     return size_binop (MINUS_EXPR, arg0, arg1);
1704
1705   ctype = (type == bitsizetype || type == ubitsizetype
1706            ? sbitsizetype : ssizetype);
1707
1708   /* If either operand is not a constant, do the conversions to the signed
1709      type and subtract.  The hardware will do the right thing with any
1710      overflow in the subtraction.  */
1711   if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1712     return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1713                        fold_convert (ctype, arg1));
1714
1715   /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1716      Otherwise, subtract the other way, convert to CTYPE (we know that can't
1717      overflow) and negate (which can't either).  Special-case a result
1718      of zero while we're here.  */
1719   if (tree_int_cst_equal (arg0, arg1))
1720     return fold_convert (ctype, integer_zero_node);
1721   else if (tree_int_cst_lt (arg1, arg0))
1722     return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1723   else
1724     return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1725                        fold_convert (ctype, size_binop (MINUS_EXPR,
1726                                                         arg1, arg0)));
1727 }
1728 \f
1729
1730 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1731    type TYPE.  If no simplification can be done return NULL_TREE.  */
1732
1733 static tree
1734 fold_convert_const (enum tree_code code, tree type, tree arg1)
1735 {
1736   int overflow = 0;
1737   tree t;
1738
1739   if (TREE_TYPE (arg1) == type)
1740     return arg1;
1741
1742   if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1743     {
1744       if (TREE_CODE (arg1) == INTEGER_CST)
1745         {
1746           /* If we would build a constant wider than GCC supports,
1747              leave the conversion unfolded.  */
1748           if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1749             return NULL_TREE;
1750
1751           /* If we are trying to make a sizetype for a small integer, use
1752              size_int to pick up cached types to reduce duplicate nodes.  */
1753           if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1754               && !TREE_CONSTANT_OVERFLOW (arg1)
1755               && compare_tree_int (arg1, 10000) < 0)
1756             return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
1757
1758           /* Given an integer constant, make new constant with new type,
1759              appropriately sign-extended or truncated.  */
1760           t = build_int_2 (TREE_INT_CST_LOW (arg1),
1761                            TREE_INT_CST_HIGH (arg1));
1762           TREE_TYPE (t) = type;
1763           /* Indicate an overflow if (1) ARG1 already overflowed,
1764              or (2) force_fit_type indicates an overflow.
1765              Tell force_fit_type that an overflow has already occurred
1766              if ARG1 is a too-large unsigned value and T is signed.
1767              But don't indicate an overflow if converting a pointer.  */
1768           TREE_OVERFLOW (t)
1769             = ((force_fit_type (t,
1770                                 (TREE_INT_CST_HIGH (arg1) < 0
1771                                  && (TYPE_UNSIGNED (type)
1772                                     < TYPE_UNSIGNED (TREE_TYPE (arg1)))))
1773                 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1774                || TREE_OVERFLOW (arg1));
1775           TREE_CONSTANT_OVERFLOW (t)
1776             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1777           return t;
1778         }
1779       else if (TREE_CODE (arg1) == REAL_CST)
1780         {
1781           /* The following code implements the floating point to integer
1782              conversion rules required by the Java Language Specification,
1783              that IEEE NaNs are mapped to zero and values that overflow
1784              the target precision saturate, i.e. values greater than
1785              INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1786              are mapped to INT_MIN.  These semantics are allowed by the
1787              C and C++ standards that simply state that the behavior of
1788              FP-to-integer conversion is unspecified upon overflow.  */
1789
1790           HOST_WIDE_INT high, low;
1791
1792           REAL_VALUE_TYPE r;
1793           REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1794
1795           switch (code)
1796             {
1797             case FIX_TRUNC_EXPR:
1798               real_trunc (&r, VOIDmode, &x);
1799               break;
1800
1801             case FIX_CEIL_EXPR:
1802               real_ceil (&r, VOIDmode, &x);
1803               break;
1804
1805             case FIX_FLOOR_EXPR:
1806               real_floor (&r, VOIDmode, &x);
1807               break;
1808
1809             case FIX_ROUND_EXPR:
1810               real_round (&r, VOIDmode, &x);
1811               break;
1812
1813             default:
1814               abort ();
1815             }
1816
1817           /* If R is NaN, return zero and show we have an overflow.  */
1818           if (REAL_VALUE_ISNAN (r))
1819             {
1820               overflow = 1;
1821               high = 0;
1822               low = 0;
1823             }
1824
1825           /* See if R is less than the lower bound or greater than the
1826              upper bound.  */
1827
1828           if (! overflow)
1829             {
1830               tree lt = TYPE_MIN_VALUE (type);
1831               REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1832               if (REAL_VALUES_LESS (r, l))
1833                 {
1834                   overflow = 1;
1835                   high = TREE_INT_CST_HIGH (lt);
1836                   low = TREE_INT_CST_LOW (lt);
1837                 }
1838             }
1839
1840           if (! overflow)
1841             {
1842               tree ut = TYPE_MAX_VALUE (type);
1843               if (ut)
1844                 {
1845                   REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1846                   if (REAL_VALUES_LESS (u, r))
1847                     {
1848                       overflow = 1;
1849                       high = TREE_INT_CST_HIGH (ut);
1850                       low = TREE_INT_CST_LOW (ut);
1851                     }
1852                 }
1853             }
1854
1855           if (! overflow)
1856             REAL_VALUE_TO_INT (&low, &high, r);
1857
1858           t = build_int_2 (low, high);
1859           TREE_TYPE (t) = type;
1860           TREE_OVERFLOW (t)
1861             = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1862           TREE_CONSTANT_OVERFLOW (t)
1863             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1864           return t;
1865         }
1866     }
1867   else if (TREE_CODE (type) == REAL_TYPE)
1868     {
1869       if (TREE_CODE (arg1) == INTEGER_CST)
1870         return build_real_from_int_cst (type, arg1);
1871       if (TREE_CODE (arg1) == REAL_CST)
1872         {
1873           if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1874             {
1875               /* We make a copy of ARG1 so that we don't modify an
1876                  existing constant tree.  */
1877               t = copy_node (arg1);
1878               TREE_TYPE (t) = type;
1879               return t;
1880             }
1881
1882           t = build_real (type,
1883                           real_value_truncate (TYPE_MODE (type),
1884                                                TREE_REAL_CST (arg1)));
1885
1886           TREE_OVERFLOW (t)
1887             = TREE_OVERFLOW (arg1) | force_fit_type (t, 0);
1888           TREE_CONSTANT_OVERFLOW (t)
1889             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1890           return t;
1891         }
1892     }
1893   return NULL_TREE;
1894 }
1895
1896 /* Convert expression ARG to type TYPE.  Used by the middle-end for
1897    simple conversions in preference to calling the front-end's convert.  */
1898
1899 tree
1900 fold_convert (tree type, tree arg)
1901 {
1902   tree orig = TREE_TYPE (arg);
1903   tree tem;
1904
1905   if (type == orig)
1906     return arg;
1907
1908   if (TREE_CODE (arg) == ERROR_MARK
1909       || TREE_CODE (type) == ERROR_MARK
1910       || TREE_CODE (orig) == ERROR_MARK)
1911     return error_mark_node;
1912
1913   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1914     return fold (build1 (NOP_EXPR, type, arg));
1915
1916   if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
1917       || TREE_CODE (type) == OFFSET_TYPE)
1918     {
1919       if (TREE_CODE (arg) == INTEGER_CST)
1920         {
1921           tem = fold_convert_const (NOP_EXPR, type, arg);
1922           if (tem != NULL_TREE)
1923             return tem;
1924         }
1925       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1926           || TREE_CODE (orig) == OFFSET_TYPE)
1927         return fold (build1 (NOP_EXPR, type, arg));
1928       if (TREE_CODE (orig) == COMPLEX_TYPE)
1929         {
1930           tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1931           return fold_convert (type, tem);
1932         }
1933       if (TREE_CODE (orig) == VECTOR_TYPE
1934           && GET_MODE_SIZE (TYPE_MODE (type))
1935              == GET_MODE_SIZE (TYPE_MODE (orig)))
1936         return fold (build1 (NOP_EXPR, type, arg));
1937     }
1938   else if (TREE_CODE (type) == REAL_TYPE)
1939     {
1940       if (TREE_CODE (arg) == INTEGER_CST)
1941         {
1942           tem = fold_convert_const (FLOAT_EXPR, type, arg);
1943           if (tem != NULL_TREE)
1944             return tem;
1945         }
1946       else if (TREE_CODE (arg) == REAL_CST)
1947         {
1948           tem = fold_convert_const (NOP_EXPR, type, arg);
1949           if (tem != NULL_TREE)
1950             return tem;
1951         }
1952
1953       if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1954         return fold (build1 (FLOAT_EXPR, type, arg));
1955       if (TREE_CODE (orig) == REAL_TYPE)
1956         return fold (build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1957                              type, arg));
1958       if (TREE_CODE (orig) == COMPLEX_TYPE)
1959         {
1960           tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1961           return fold_convert (type, tem);
1962         }
1963     }
1964   else if (TREE_CODE (type) == COMPLEX_TYPE)
1965     {
1966       if (INTEGRAL_TYPE_P (orig)
1967           || POINTER_TYPE_P (orig)
1968           || TREE_CODE (orig) == REAL_TYPE)
1969         return build2 (COMPLEX_EXPR, type,
1970                        fold_convert (TREE_TYPE (type), arg),
1971                        fold_convert (TREE_TYPE (type), integer_zero_node));
1972       if (TREE_CODE (orig) == COMPLEX_TYPE)
1973         {
1974           tree rpart, ipart;
1975
1976           if (TREE_CODE (arg) == COMPLEX_EXPR)
1977             {
1978               rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
1979               ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
1980               return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1981             }
1982
1983           arg = save_expr (arg);
1984           rpart = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1985           ipart = fold (build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg));
1986           rpart = fold_convert (TREE_TYPE (type), rpart);
1987           ipart = fold_convert (TREE_TYPE (type), ipart);
1988           return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1989         }
1990     }
1991   else if (TREE_CODE (type) == VECTOR_TYPE)
1992     {
1993       if ((INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1994           && GET_MODE_SIZE (TYPE_MODE (type))
1995              == GET_MODE_SIZE (TYPE_MODE (orig)))
1996         return fold (build1 (NOP_EXPR, type, arg));
1997       if (TREE_CODE (orig) == VECTOR_TYPE
1998           && GET_MODE_SIZE (TYPE_MODE (type))
1999              == GET_MODE_SIZE (TYPE_MODE (orig)))
2000         return fold (build1 (NOP_EXPR, type, arg));
2001     }
2002   else if (VOID_TYPE_P (type))
2003     return fold (build1 (CONVERT_EXPR, type, arg));
2004   abort ();
2005 }
2006 \f
2007 /* Return an expr equal to X but certainly not valid as an lvalue.  */
2008
2009 tree
2010 non_lvalue (tree x)
2011 {
2012   /* We only need to wrap lvalue tree codes.  */
2013   switch (TREE_CODE (x))
2014   {
2015   case VAR_DECL:
2016   case PARM_DECL:
2017   case RESULT_DECL:
2018   case LABEL_DECL:
2019   case FUNCTION_DECL:
2020   case SSA_NAME:
2021
2022   case COMPONENT_REF:
2023   case INDIRECT_REF:
2024   case ARRAY_REF:
2025   case BIT_FIELD_REF:
2026   case BUFFER_REF:
2027   case ARRAY_RANGE_REF:
2028   case VTABLE_REF:
2029
2030   case REALPART_EXPR:
2031   case IMAGPART_EXPR:
2032   case PREINCREMENT_EXPR:
2033   case PREDECREMENT_EXPR:
2034   case SAVE_EXPR:
2035   case UNSAVE_EXPR:
2036   case TRY_CATCH_EXPR:
2037   case WITH_CLEANUP_EXPR:
2038   case COMPOUND_EXPR:
2039   case MODIFY_EXPR:
2040   case TARGET_EXPR:
2041   case COND_EXPR:
2042   case BIND_EXPR:
2043   case MIN_EXPR:
2044   case MAX_EXPR:
2045   case RTL_EXPR:
2046     break;
2047
2048   default:
2049     /* Assume the worst for front-end tree codes.  */
2050     if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2051       break;
2052     return x;
2053   }
2054   return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2055 }
2056
2057 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2058    Zero means allow extended lvalues.  */
2059
2060 int pedantic_lvalues;
2061
2062 /* When pedantic, return an expr equal to X but certainly not valid as a
2063    pedantic lvalue.  Otherwise, return X.  */
2064
2065 tree
2066 pedantic_non_lvalue (tree x)
2067 {
2068   if (pedantic_lvalues)
2069     return non_lvalue (x);
2070   else
2071     return x;
2072 }
2073 \f
2074 /* Given a tree comparison code, return the code that is the logical inverse
2075    of the given code.  It is not safe to do this for floating-point
2076    comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2077    as well: if reversing the comparison is unsafe, return ERROR_MARK.  */
2078
2079 static enum tree_code
2080 invert_tree_comparison (enum tree_code code, bool honor_nans)
2081 {
2082   if (honor_nans && flag_trapping_math)
2083     return ERROR_MARK;
2084
2085   switch (code)
2086     {
2087     case EQ_EXPR:
2088       return NE_EXPR;
2089     case NE_EXPR:
2090       return EQ_EXPR;
2091     case GT_EXPR:
2092       return honor_nans ? UNLE_EXPR : LE_EXPR;
2093     case GE_EXPR:
2094       return honor_nans ? UNLT_EXPR : LT_EXPR;
2095     case LT_EXPR:
2096       return honor_nans ? UNGE_EXPR : GE_EXPR;
2097     case LE_EXPR:
2098       return honor_nans ? UNGT_EXPR : GT_EXPR;
2099     case LTGT_EXPR:
2100       return UNEQ_EXPR;
2101     case UNEQ_EXPR:
2102       return LTGT_EXPR;
2103     case UNGT_EXPR:
2104       return LE_EXPR;
2105     case UNGE_EXPR:
2106       return LT_EXPR;
2107     case UNLT_EXPR:
2108       return GE_EXPR;
2109     case UNLE_EXPR:
2110       return GT_EXPR;
2111     case ORDERED_EXPR:
2112       return UNORDERED_EXPR;
2113     case UNORDERED_EXPR:
2114       return ORDERED_EXPR;
2115     default:
2116       abort ();
2117     }
2118 }
2119
2120 /* Similar, but return the comparison that results if the operands are
2121    swapped.  This is safe for floating-point.  */
2122
2123 static enum tree_code
2124 swap_tree_comparison (enum tree_code code)
2125 {
2126   switch (code)
2127     {
2128     case EQ_EXPR:
2129     case NE_EXPR:
2130       return code;
2131     case GT_EXPR:
2132       return LT_EXPR;
2133     case GE_EXPR:
2134       return LE_EXPR;
2135     case LT_EXPR:
2136       return GT_EXPR;
2137     case LE_EXPR:
2138       return GE_EXPR;
2139     default:
2140       abort ();
2141     }
2142 }
2143
2144
2145 /* Convert a comparison tree code from an enum tree_code representation
2146    into a compcode bit-based encoding.  This function is the inverse of
2147    compcode_to_comparison.  */
2148
2149 static enum comparison_code
2150 comparison_to_compcode (enum tree_code code)
2151 {
2152   switch (code)
2153     {
2154     case LT_EXPR:
2155       return COMPCODE_LT;
2156     case EQ_EXPR:
2157       return COMPCODE_EQ;
2158     case LE_EXPR:
2159       return COMPCODE_LE;
2160     case GT_EXPR:
2161       return COMPCODE_GT;
2162     case NE_EXPR:
2163       return COMPCODE_NE;
2164     case GE_EXPR:
2165       return COMPCODE_GE;
2166     case ORDERED_EXPR:
2167       return COMPCODE_ORD;
2168     case UNORDERED_EXPR:
2169       return COMPCODE_UNORD;
2170     case UNLT_EXPR:
2171       return COMPCODE_UNLT;
2172     case UNEQ_EXPR:
2173       return COMPCODE_UNEQ;
2174     case UNLE_EXPR:
2175       return COMPCODE_UNLE;
2176     case UNGT_EXPR:
2177       return COMPCODE_UNGT;
2178     case LTGT_EXPR:
2179       return COMPCODE_LTGT;
2180     case UNGE_EXPR:
2181       return COMPCODE_UNGE;
2182     default:
2183       abort ();
2184     }
2185 }
2186
2187 /* Convert a compcode bit-based encoding of a comparison operator back
2188    to GCC's enum tree_code representation.  This function is the
2189    inverse of comparison_to_compcode.  */
2190
2191 static enum tree_code
2192 compcode_to_comparison (enum comparison_code code)
2193 {
2194   switch (code)
2195     {
2196     case COMPCODE_LT:
2197       return LT_EXPR;
2198     case COMPCODE_EQ:
2199       return EQ_EXPR;
2200     case COMPCODE_LE:
2201       return LE_EXPR;
2202     case COMPCODE_GT:
2203       return GT_EXPR;
2204     case COMPCODE_NE:
2205       return NE_EXPR;
2206     case COMPCODE_GE:
2207       return GE_EXPR;
2208     case COMPCODE_ORD:
2209       return ORDERED_EXPR;
2210     case COMPCODE_UNORD:
2211       return UNORDERED_EXPR;
2212     case COMPCODE_UNLT:
2213       return UNLT_EXPR;
2214     case COMPCODE_UNEQ:
2215       return UNEQ_EXPR;
2216     case COMPCODE_UNLE:
2217       return UNLE_EXPR;
2218     case COMPCODE_UNGT:
2219       return UNGT_EXPR;
2220     case COMPCODE_LTGT:
2221       return LTGT_EXPR;
2222     case COMPCODE_UNGE:
2223       return UNGE_EXPR;
2224     default:
2225       abort ();
2226     }
2227 }
2228
2229 /* Return a tree for the comparison which is the combination of
2230    doing the AND or OR (depending on CODE) of the two operations LCODE
2231    and RCODE on the identical operands LL_ARG and LR_ARG.  Take into account
2232    the possibility of trapping if the mode has NaNs, and return NULL_TREE
2233    if this makes the transformation invalid.  */
2234
2235 tree
2236 combine_comparisons (enum tree_code code, enum tree_code lcode,
2237                      enum tree_code rcode, tree truth_type,
2238                      tree ll_arg, tree lr_arg)
2239 {
2240   bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2241   enum comparison_code lcompcode = comparison_to_compcode (lcode);
2242   enum comparison_code rcompcode = comparison_to_compcode (rcode);
2243   enum comparison_code compcode;
2244
2245   switch (code)
2246     {
2247     case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2248       compcode = lcompcode & rcompcode;
2249       break;
2250
2251     case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2252       compcode = lcompcode | rcompcode;
2253       break;
2254
2255     default:
2256       return NULL_TREE;
2257     }
2258
2259   if (!honor_nans)
2260     {
2261       /* Eliminate unordered comparisons, as well as LTGT and ORD
2262          which are not used unless the mode has NaNs.  */
2263       compcode &= ~COMPCODE_UNORD;
2264       if (compcode == COMPCODE_LTGT)
2265         compcode = COMPCODE_NE;
2266       else if (compcode == COMPCODE_ORD)
2267         compcode = COMPCODE_TRUE;
2268     }
2269    else if (flag_trapping_math)
2270      {
2271         /* Check that the original operation and the optimized ones will trap 
2272            under the same condition.  */
2273         bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2274                      && (lcompcode != COMPCODE_EQ)
2275                      && (lcompcode != COMPCODE_ORD);
2276         bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2277                      && (rcompcode != COMPCODE_EQ)
2278                      && (rcompcode != COMPCODE_ORD);
2279         bool trap = (compcode & COMPCODE_UNORD) == 0
2280                     && (compcode != COMPCODE_EQ)
2281                     && (compcode != COMPCODE_ORD);
2282
2283         /* In a short-circuited boolean expression the LHS might be
2284            such that the RHS, if evaluated, will never trap.  For
2285            example, in ORD (x, y) && (x < y), we evaluate the RHS only
2286            if neither x nor y is NaN.  (This is a mixed blessing: for
2287            example, the expression above will never trap, hence
2288            optimizing it to x < y would be invalid).  */
2289         if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2290             || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2291           rtrap = false;
2292
2293         /* If the comparison was short-circuited, and only the RHS
2294            trapped, we may now generate a spurious trap.  */
2295         if (rtrap && !ltrap
2296             && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2297           return NULL_TREE;
2298
2299         /* If we changed the conditions that cause a trap, we lose.  */
2300         if ((ltrap || rtrap) != trap)
2301           return NULL_TREE;
2302       }
2303
2304   if (compcode == COMPCODE_TRUE)
2305     return constant_boolean_node (true, truth_type);
2306   else if (compcode == COMPCODE_FALSE)
2307     return constant_boolean_node (false, truth_type);
2308   else
2309     return fold (build2 (compcode_to_comparison (compcode),
2310                          truth_type, ll_arg, lr_arg));
2311 }
2312
2313 /* Return nonzero if CODE is a tree code that represents a truth value.  */
2314
2315 static int
2316 truth_value_p (enum tree_code code)
2317 {
2318   return (TREE_CODE_CLASS (code) == '<'
2319           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2320           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2321           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2322 }
2323 \f
2324 /* Return nonzero if two operands (typically of the same tree node)
2325    are necessarily equal.  If either argument has side-effects this
2326    function returns zero.  FLAGS modifies behavior as follows:
2327
2328    If OEP_ONLY_CONST is set, only return nonzero for constants.
2329    This function tests whether the operands are indistinguishable;
2330    it does not test whether they are equal using C's == operation.
2331    The distinction is important for IEEE floating point, because
2332    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2333    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2334
2335    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2336    even though it may hold multiple values during a function.
2337    This is because a GCC tree node guarantees that nothing else is
2338    executed between the evaluation of its "operands" (which may often
2339    be evaluated in arbitrary order).  Hence if the operands themselves
2340    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2341    same value in each operand/subexpression.  Hence a zero value for
2342    ONLY_CONST assumes isochronic (or instantaneous) tree equivalence.
2343    If comparing arbitrary expression trees, such as from different
2344    statements, ONLY_CONST must usually be nonzero.
2345
2346    If OEP_PURE_SAME is set, then pure functions with identical arguments
2347    are considered the same.  It is used when the caller has other ways
2348    to ensure that global memory is unchanged in between.  */
2349
2350 int
2351 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2352 {
2353   /* If either is ERROR_MARK, they aren't equal.  */
2354   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2355     return 0;
2356
2357   /* If both types don't have the same signedness, then we can't consider
2358      them equal.  We must check this before the STRIP_NOPS calls
2359      because they may change the signedness of the arguments.  */
2360   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2361     return 0;
2362
2363   STRIP_NOPS (arg0);
2364   STRIP_NOPS (arg1);
2365
2366   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2367       /* This is needed for conversions and for COMPONENT_REF.
2368          Might as well play it safe and always test this.  */
2369       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2370       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2371       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2372     return 0;
2373
2374   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2375      We don't care about side effects in that case because the SAVE_EXPR
2376      takes care of that for us. In all other cases, two expressions are
2377      equal if they have no side effects.  If we have two identical
2378      expressions with side effects that should be treated the same due
2379      to the only side effects being identical SAVE_EXPR's, that will
2380      be detected in the recursive calls below.  */
2381   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2382       && (TREE_CODE (arg0) == SAVE_EXPR
2383           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2384     return 1;
2385
2386   /* Next handle constant cases, those for which we can return 1 even
2387      if ONLY_CONST is set.  */
2388   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2389     switch (TREE_CODE (arg0))
2390       {
2391       case INTEGER_CST:
2392         return (! TREE_CONSTANT_OVERFLOW (arg0)
2393                 && ! TREE_CONSTANT_OVERFLOW (arg1)
2394                 && tree_int_cst_equal (arg0, arg1));
2395
2396       case REAL_CST:
2397         return (! TREE_CONSTANT_OVERFLOW (arg0)
2398                 && ! TREE_CONSTANT_OVERFLOW (arg1)
2399                 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2400                                           TREE_REAL_CST (arg1)));
2401
2402       case VECTOR_CST:
2403         {
2404           tree v1, v2;
2405
2406           if (TREE_CONSTANT_OVERFLOW (arg0)
2407               || TREE_CONSTANT_OVERFLOW (arg1))
2408             return 0;
2409
2410           v1 = TREE_VECTOR_CST_ELTS (arg0);
2411           v2 = TREE_VECTOR_CST_ELTS (arg1);
2412           while (v1 && v2)
2413             {
2414               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2415                                     flags))
2416                 return 0;
2417               v1 = TREE_CHAIN (v1);
2418               v2 = TREE_CHAIN (v2);
2419             }
2420
2421           return 1;
2422         }
2423
2424       case COMPLEX_CST:
2425         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2426                                  flags)
2427                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2428                                     flags));
2429
2430       case STRING_CST:
2431         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2432                 && ! memcmp (TREE_STRING_POINTER (arg0),
2433                               TREE_STRING_POINTER (arg1),
2434                               TREE_STRING_LENGTH (arg0)));
2435
2436       case ADDR_EXPR:
2437         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2438                                 0);
2439       default:
2440         break;
2441       }
2442
2443   if (flags & OEP_ONLY_CONST)
2444     return 0;
2445
2446   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2447     {
2448     case '1':
2449       /* Two conversions are equal only if signedness and modes match.  */
2450       if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2451           && (TYPE_UNSIGNED (TREE_TYPE (arg0))
2452               != TYPE_UNSIGNED (TREE_TYPE (arg1))))
2453         return 0;
2454
2455       return operand_equal_p (TREE_OPERAND (arg0, 0),
2456                               TREE_OPERAND (arg1, 0), flags);
2457
2458     case '<':
2459     case '2':
2460       if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2461           && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2462                               0))
2463         return 1;
2464
2465       /* For commutative ops, allow the other order.  */
2466       return (commutative_tree_code (TREE_CODE (arg0))
2467               && operand_equal_p (TREE_OPERAND (arg0, 0),
2468                                   TREE_OPERAND (arg1, 1), flags)
2469               && operand_equal_p (TREE_OPERAND (arg0, 1),
2470                                   TREE_OPERAND (arg1, 0), flags));
2471
2472     case 'r':
2473       /* If either of the pointer (or reference) expressions we are
2474          dereferencing contain a side effect, these cannot be equal.  */
2475       if (TREE_SIDE_EFFECTS (arg0)
2476           || TREE_SIDE_EFFECTS (arg1))
2477         return 0;
2478
2479       switch (TREE_CODE (arg0))
2480         {
2481         case INDIRECT_REF:
2482           return operand_equal_p (TREE_OPERAND (arg0, 0),
2483                                   TREE_OPERAND (arg1, 0), flags);
2484
2485         case COMPONENT_REF:
2486         case ARRAY_REF:
2487         case ARRAY_RANGE_REF:
2488           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2489                                    TREE_OPERAND (arg1, 0), flags)
2490                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2491                                       TREE_OPERAND (arg1, 1), flags));
2492
2493         case BIT_FIELD_REF:
2494           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2495                                    TREE_OPERAND (arg1, 0), flags)
2496                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2497                                       TREE_OPERAND (arg1, 1), flags)
2498                   && operand_equal_p (TREE_OPERAND (arg0, 2),
2499                                       TREE_OPERAND (arg1, 2), flags));
2500         default:
2501           return 0;
2502         }
2503
2504     case 'e':
2505       switch (TREE_CODE (arg0))
2506         {
2507         case ADDR_EXPR:
2508         case TRUTH_NOT_EXPR:
2509           return operand_equal_p (TREE_OPERAND (arg0, 0),
2510                                   TREE_OPERAND (arg1, 0), flags);
2511
2512         case RTL_EXPR:
2513           return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2514
2515         case CALL_EXPR:
2516           /* If the CALL_EXPRs call different functions, then they
2517              clearly can not be equal.  */
2518           if (! operand_equal_p (TREE_OPERAND (arg0, 0),
2519                                  TREE_OPERAND (arg1, 0), flags))
2520             return 0;
2521
2522           {
2523             unsigned int cef = call_expr_flags (arg0);
2524             if (flags & OEP_PURE_SAME)
2525               cef &= ECF_CONST | ECF_PURE;
2526             else
2527               cef &= ECF_CONST;
2528             if (!cef)
2529               return 0;
2530           }
2531
2532           /* Now see if all the arguments are the same.  operand_equal_p
2533              does not handle TREE_LIST, so we walk the operands here
2534              feeding them to operand_equal_p.  */
2535           arg0 = TREE_OPERAND (arg0, 1);
2536           arg1 = TREE_OPERAND (arg1, 1);
2537           while (arg0 && arg1)
2538             {
2539               if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2540                                      flags))
2541                 return 0;
2542
2543               arg0 = TREE_CHAIN (arg0);
2544               arg1 = TREE_CHAIN (arg1);
2545             }
2546
2547           /* If we get here and both argument lists are exhausted
2548              then the CALL_EXPRs are equal.  */
2549           return ! (arg0 || arg1);
2550
2551         default:
2552           return 0;
2553         }
2554
2555     case 'd':
2556       /* Consider __builtin_sqrt equal to sqrt.  */
2557       return (TREE_CODE (arg0) == FUNCTION_DECL
2558               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2559               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2560               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2561
2562     default:
2563       return 0;
2564     }
2565 }
2566 \f
2567 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2568    shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2569
2570    When in doubt, return 0.  */
2571
2572 static int
2573 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2574 {
2575   int unsignedp1, unsignedpo;
2576   tree primarg0, primarg1, primother;
2577   unsigned int correct_width;
2578
2579   if (operand_equal_p (arg0, arg1, 0))
2580     return 1;
2581
2582   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2583       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2584     return 0;
2585
2586   /* Discard any conversions that don't change the modes of ARG0 and ARG1
2587      and see if the inner values are the same.  This removes any
2588      signedness comparison, which doesn't matter here.  */
2589   primarg0 = arg0, primarg1 = arg1;
2590   STRIP_NOPS (primarg0);
2591   STRIP_NOPS (primarg1);
2592   if (operand_equal_p (primarg0, primarg1, 0))
2593     return 1;
2594
2595   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2596      actual comparison operand, ARG0.
2597
2598      First throw away any conversions to wider types
2599      already present in the operands.  */
2600
2601   primarg1 = get_narrower (arg1, &unsignedp1);
2602   primother = get_narrower (other, &unsignedpo);
2603
2604   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2605   if (unsignedp1 == unsignedpo
2606       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2607       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2608     {
2609       tree type = TREE_TYPE (arg0);
2610
2611       /* Make sure shorter operand is extended the right way
2612          to match the longer operand.  */
2613       primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2614                                (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2615
2616       if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2617         return 1;
2618     }
2619
2620   return 0;
2621 }
2622 \f
2623 /* See if ARG is an expression that is either a comparison or is performing
2624    arithmetic on comparisons.  The comparisons must only be comparing
2625    two different values, which will be stored in *CVAL1 and *CVAL2; if
2626    they are nonzero it means that some operands have already been found.
2627    No variables may be used anywhere else in the expression except in the
2628    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
2629    the expression and save_expr needs to be called with CVAL1 and CVAL2.
2630
2631    If this is true, return 1.  Otherwise, return zero.  */
2632
2633 static int
2634 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2635 {
2636   enum tree_code code = TREE_CODE (arg);
2637   char class = TREE_CODE_CLASS (code);
2638
2639   /* We can handle some of the 'e' cases here.  */
2640   if (class == 'e' && code == TRUTH_NOT_EXPR)
2641     class = '1';
2642   else if (class == 'e'
2643            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2644                || code == COMPOUND_EXPR))
2645     class = '2';
2646
2647   else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2648            && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2649     {
2650       /* If we've already found a CVAL1 or CVAL2, this expression is
2651          two complex to handle.  */
2652       if (*cval1 || *cval2)
2653         return 0;
2654
2655       class = '1';
2656       *save_p = 1;
2657     }
2658
2659   switch (class)
2660     {
2661     case '1':
2662       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2663
2664     case '2':
2665       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2666               && twoval_comparison_p (TREE_OPERAND (arg, 1),
2667                                       cval1, cval2, save_p));
2668
2669     case 'c':
2670       return 1;
2671
2672     case 'e':
2673       if (code == COND_EXPR)
2674         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2675                                      cval1, cval2, save_p)
2676                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2677                                         cval1, cval2, save_p)
2678                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2679                                         cval1, cval2, save_p));
2680       return 0;
2681
2682     case '<':
2683       /* First see if we can handle the first operand, then the second.  For
2684          the second operand, we know *CVAL1 can't be zero.  It must be that
2685          one side of the comparison is each of the values; test for the
2686          case where this isn't true by failing if the two operands
2687          are the same.  */
2688
2689       if (operand_equal_p (TREE_OPERAND (arg, 0),
2690                            TREE_OPERAND (arg, 1), 0))
2691         return 0;
2692
2693       if (*cval1 == 0)
2694         *cval1 = TREE_OPERAND (arg, 0);
2695       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2696         ;
2697       else if (*cval2 == 0)
2698         *cval2 = TREE_OPERAND (arg, 0);
2699       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2700         ;
2701       else
2702         return 0;
2703
2704       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2705         ;
2706       else if (*cval2 == 0)
2707         *cval2 = TREE_OPERAND (arg, 1);
2708       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2709         ;
2710       else
2711         return 0;
2712
2713       return 1;
2714
2715     default:
2716       return 0;
2717     }
2718 }
2719 \f
2720 /* ARG is a tree that is known to contain just arithmetic operations and
2721    comparisons.  Evaluate the operations in the tree substituting NEW0 for
2722    any occurrence of OLD0 as an operand of a comparison and likewise for
2723    NEW1 and OLD1.  */
2724
2725 static tree
2726 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2727 {
2728   tree type = TREE_TYPE (arg);
2729   enum tree_code code = TREE_CODE (arg);
2730   char class = TREE_CODE_CLASS (code);
2731
2732   /* We can handle some of the 'e' cases here.  */
2733   if (class == 'e' && code == TRUTH_NOT_EXPR)
2734     class = '1';
2735   else if (class == 'e'
2736            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2737     class = '2';
2738
2739   switch (class)
2740     {
2741     case '1':
2742       return fold (build1 (code, type,
2743                            eval_subst (TREE_OPERAND (arg, 0),
2744                                        old0, new0, old1, new1)));
2745
2746     case '2':
2747       return fold (build2 (code, type,
2748                            eval_subst (TREE_OPERAND (arg, 0),
2749                                        old0, new0, old1, new1),
2750                            eval_subst (TREE_OPERAND (arg, 1),
2751                                        old0, new0, old1, new1)));
2752
2753     case 'e':
2754       switch (code)
2755         {
2756         case SAVE_EXPR:
2757           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2758
2759         case COMPOUND_EXPR:
2760           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2761
2762         case COND_EXPR:
2763           return fold (build3 (code, type,
2764                                eval_subst (TREE_OPERAND (arg, 0),
2765                                            old0, new0, old1, new1),
2766                                eval_subst (TREE_OPERAND (arg, 1),
2767                                            old0, new0, old1, new1),
2768                                eval_subst (TREE_OPERAND (arg, 2),
2769                                            old0, new0, old1, new1)));
2770         default:
2771           break;
2772         }
2773       /* Fall through - ???  */
2774
2775     case '<':
2776       {
2777         tree arg0 = TREE_OPERAND (arg, 0);
2778         tree arg1 = TREE_OPERAND (arg, 1);
2779
2780         /* We need to check both for exact equality and tree equality.  The
2781            former will be true if the operand has a side-effect.  In that
2782            case, we know the operand occurred exactly once.  */
2783
2784         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2785           arg0 = new0;
2786         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2787           arg0 = new1;
2788
2789         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2790           arg1 = new0;
2791         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2792           arg1 = new1;
2793
2794         return fold (build2 (code, type, arg0, arg1));
2795       }
2796
2797     default:
2798       return arg;
2799     }
2800 }
2801 \f
2802 /* Return a tree for the case when the result of an expression is RESULT
2803    converted to TYPE and OMITTED was previously an operand of the expression
2804    but is now not needed (e.g., we folded OMITTED * 0).
2805
2806    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2807    the conversion of RESULT to TYPE.  */
2808
2809 tree
2810 omit_one_operand (tree type, tree result, tree omitted)
2811 {
2812   tree t = fold_convert (type, result);
2813
2814   if (TREE_SIDE_EFFECTS (omitted))
2815     return build2 (COMPOUND_EXPR, type, omitted, t);
2816
2817   return non_lvalue (t);
2818 }
2819
2820 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2821
2822 static tree
2823 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2824 {
2825   tree t = fold_convert (type, result);
2826
2827   if (TREE_SIDE_EFFECTS (omitted))
2828     return build2 (COMPOUND_EXPR, type, omitted, t);
2829
2830   return pedantic_non_lvalue (t);
2831 }
2832 \f
2833 /* Return a simplified tree node for the truth-negation of ARG.  This
2834    never alters ARG itself.  We assume that ARG is an operation that
2835    returns a truth value (0 or 1).
2836
2837    FIXME: one would think we would fold the result, but it causes
2838    problems with the dominator optimizer.  */
2839 tree
2840 invert_truthvalue (tree arg)
2841 {
2842   tree type = TREE_TYPE (arg);
2843   enum tree_code code = TREE_CODE (arg);
2844
2845   if (code == ERROR_MARK)
2846     return arg;
2847
2848   /* If this is a comparison, we can simply invert it, except for
2849      floating-point non-equality comparisons, in which case we just
2850      enclose a TRUTH_NOT_EXPR around what we have.  */
2851
2852   if (TREE_CODE_CLASS (code) == '<')
2853     {
2854       tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
2855       if (FLOAT_TYPE_P (op_type)
2856           && flag_trapping_math
2857           && code != ORDERED_EXPR && code != UNORDERED_EXPR
2858           && code != NE_EXPR && code != EQ_EXPR)
2859         return build1 (TRUTH_NOT_EXPR, type, arg);
2860       else
2861         {
2862           code = invert_tree_comparison (code,
2863                                          HONOR_NANS (TYPE_MODE (op_type)));
2864           if (code == ERROR_MARK)
2865             return build1 (TRUTH_NOT_EXPR, type, arg);
2866           else
2867             return build2 (code, type,
2868                            TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2869         }
2870     }
2871
2872   switch (code)
2873     {
2874     case INTEGER_CST:
2875       return fold_convert (type, build_int_2 (integer_zerop (arg), 0));
2876
2877     case TRUTH_AND_EXPR:
2878       return build2 (TRUTH_OR_EXPR, type,
2879                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2880                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2881
2882     case TRUTH_OR_EXPR:
2883       return build2 (TRUTH_AND_EXPR, type,
2884                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2885                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2886
2887     case TRUTH_XOR_EXPR:
2888       /* Here we can invert either operand.  We invert the first operand
2889          unless the second operand is a TRUTH_NOT_EXPR in which case our
2890          result is the XOR of the first operand with the inside of the
2891          negation of the second operand.  */
2892
2893       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2894         return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2895                        TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2896       else
2897         return build2 (TRUTH_XOR_EXPR, type,
2898                        invert_truthvalue (TREE_OPERAND (arg, 0)),
2899                        TREE_OPERAND (arg, 1));
2900
2901     case TRUTH_ANDIF_EXPR:
2902       return build2 (TRUTH_ORIF_EXPR, type,
2903                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2904                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2905
2906     case TRUTH_ORIF_EXPR:
2907       return build2 (TRUTH_ANDIF_EXPR, type,
2908                      invert_truthvalue (TREE_OPERAND (arg, 0)),
2909                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2910
2911     case TRUTH_NOT_EXPR:
2912       return TREE_OPERAND (arg, 0);
2913
2914     case COND_EXPR:
2915       return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
2916                      invert_truthvalue (TREE_OPERAND (arg, 1)),
2917                      invert_truthvalue (TREE_OPERAND (arg, 2)));
2918
2919     case COMPOUND_EXPR:
2920       return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2921                      invert_truthvalue (TREE_OPERAND (arg, 1)));
2922
2923     case NON_LVALUE_EXPR:
2924       return invert_truthvalue (TREE_OPERAND (arg, 0));
2925
2926     case NOP_EXPR:
2927       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2928         break;
2929
2930     case CONVERT_EXPR:
2931     case FLOAT_EXPR:
2932       return build1 (TREE_CODE (arg), type,
2933                      invert_truthvalue (TREE_OPERAND (arg, 0)));
2934
2935     case BIT_AND_EXPR:
2936       if (!integer_onep (TREE_OPERAND (arg, 1)))
2937         break;
2938       return build2 (EQ_EXPR, type, arg,
2939                      fold_convert (type, integer_zero_node));
2940
2941     case SAVE_EXPR:
2942       return build1 (TRUTH_NOT_EXPR, type, arg);
2943
2944     case CLEANUP_POINT_EXPR:
2945       return build1 (CLEANUP_POINT_EXPR, type,
2946                      invert_truthvalue (TREE_OPERAND (arg, 0)));
2947
2948     default:
2949       break;
2950     }
2951   if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2952     abort ();
2953   return build1 (TRUTH_NOT_EXPR, type, arg);
2954 }
2955
2956 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2957    operands are another bit-wise operation with a common input.  If so,
2958    distribute the bit operations to save an operation and possibly two if
2959    constants are involved.  For example, convert
2960         (A | B) & (A | C) into A | (B & C)
2961    Further simplification will occur if B and C are constants.
2962
2963    If this optimization cannot be done, 0 will be returned.  */
2964
2965 static tree
2966 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
2967 {
2968   tree common;
2969   tree left, right;
2970
2971   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2972       || TREE_CODE (arg0) == code
2973       || (TREE_CODE (arg0) != BIT_AND_EXPR
2974           && TREE_CODE (arg0) != BIT_IOR_EXPR))
2975     return 0;
2976
2977   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2978     {
2979       common = TREE_OPERAND (arg0, 0);
2980       left = TREE_OPERAND (arg0, 1);
2981       right = TREE_OPERAND (arg1, 1);
2982     }
2983   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2984     {
2985       common = TREE_OPERAND (arg0, 0);
2986       left = TREE_OPERAND (arg0, 1);
2987       right = TREE_OPERAND (arg1, 0);
2988     }
2989   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2990     {
2991       common = TREE_OPERAND (arg0, 1);
2992       left = TREE_OPERAND (arg0, 0);
2993       right = TREE_OPERAND (arg1, 1);
2994     }
2995   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2996     {
2997       common = TREE_OPERAND (arg0, 1);
2998       left = TREE_OPERAND (arg0, 0);
2999       right = TREE_OPERAND (arg1, 0);
3000     }
3001   else
3002     return 0;
3003
3004   return fold (build2 (TREE_CODE (arg0), type, common,
3005                        fold (build2 (code, type, left, right))));
3006 }
3007 \f
3008 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3009    starting at BITPOS.  The field is unsigned if UNSIGNEDP is nonzero.  */
3010
3011 static tree
3012 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3013                     int unsignedp)
3014 {
3015   tree result = build3 (BIT_FIELD_REF, type, inner,
3016                         size_int (bitsize), bitsize_int (bitpos));
3017
3018   BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3019
3020   return result;
3021 }
3022
3023 /* Optimize a bit-field compare.
3024
3025    There are two cases:  First is a compare against a constant and the
3026    second is a comparison of two items where the fields are at the same
3027    bit position relative to the start of a chunk (byte, halfword, word)
3028    large enough to contain it.  In these cases we can avoid the shift
3029    implicit in bitfield extractions.
3030
3031    For constants, we emit a compare of the shifted constant with the
3032    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3033    compared.  For two fields at the same position, we do the ANDs with the
3034    similar mask and compare the result of the ANDs.
3035
3036    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3037    COMPARE_TYPE is the type of the comparison, and LHS and RHS
3038    are the left and right operands of the comparison, respectively.
3039
3040    If the optimization described above can be done, we return the resulting
3041    tree.  Otherwise we return zero.  */
3042
3043 static tree
3044 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3045                             tree lhs, tree rhs)
3046 {
3047   HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3048   tree type = TREE_TYPE (lhs);
3049   tree signed_type, unsigned_type;
3050   int const_p = TREE_CODE (rhs) == INTEGER_CST;
3051   enum machine_mode lmode, rmode, nmode;
3052   int lunsignedp, runsignedp;
3053   int lvolatilep = 0, rvolatilep = 0;
3054   tree linner, rinner = NULL_TREE;
3055   tree mask;
3056   tree offset;
3057
3058   /* Get all the information about the extractions being done.  If the bit size
3059      if the same as the size of the underlying object, we aren't doing an
3060      extraction at all and so can do nothing.  We also don't want to
3061      do anything if the inner expression is a PLACEHOLDER_EXPR since we
3062      then will no longer be able to replace it.  */
3063   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3064                                 &lunsignedp, &lvolatilep);
3065   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3066       || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3067     return 0;
3068
3069  if (!const_p)
3070    {
3071      /* If this is not a constant, we can only do something if bit positions,
3072         sizes, and signedness are the same.  */
3073      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3074                                    &runsignedp, &rvolatilep);
3075
3076      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3077          || lunsignedp != runsignedp || offset != 0
3078          || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3079        return 0;
3080    }
3081
3082   /* See if we can find a mode to refer to this field.  We should be able to,
3083      but fail if we can't.  */
3084   nmode = get_best_mode (lbitsize, lbitpos,
3085                          const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3086                          : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3087                                 TYPE_ALIGN (TREE_TYPE (rinner))),
3088                          word_mode, lvolatilep || rvolatilep);
3089   if (nmode == VOIDmode)
3090     return 0;
3091
3092   /* Set signed and unsigned types of the precision of this mode for the
3093      shifts below.  */
3094   signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3095   unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3096
3097   /* Compute the bit position and size for the new reference and our offset
3098      within it. If the new reference is the same size as the original, we
3099      won't optimize anything, so return zero.  */
3100   nbitsize = GET_MODE_BITSIZE (nmode);
3101   nbitpos = lbitpos & ~ (nbitsize - 1);
3102   lbitpos -= nbitpos;
3103   if (nbitsize == lbitsize)
3104     return 0;
3105
3106   if (BYTES_BIG_ENDIAN)
3107     lbitpos = nbitsize - lbitsize - lbitpos;
3108
3109   /* Make the mask to be used against the extracted field.  */
3110   mask = build_int_2 (~0, ~0);
3111   TREE_TYPE (mask) = unsigned_type;
3112   force_fit_type (mask, 0);
3113   mask = fold_convert (unsigned_type, mask);
3114   mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3115   mask = const_binop (RSHIFT_EXPR, mask,
3116                       size_int (nbitsize - lbitsize - lbitpos), 0);
3117
3118   if (! const_p)
3119     /* If not comparing with constant, just rework the comparison
3120        and return.  */
3121     return build2 (code, compare_type,
3122                    build2 (BIT_AND_EXPR, unsigned_type,
3123                            make_bit_field_ref (linner, unsigned_type,
3124                                                nbitsize, nbitpos, 1),
3125                            mask),
3126                    build2 (BIT_AND_EXPR, unsigned_type,
3127                            make_bit_field_ref (rinner, unsigned_type,
3128                                                nbitsize, nbitpos, 1),
3129                            mask));
3130
3131   /* Otherwise, we are handling the constant case. See if the constant is too
3132      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
3133      this not only for its own sake, but to avoid having to test for this
3134      error case below.  If we didn't, we might generate wrong code.
3135
3136      For unsigned fields, the constant shifted right by the field length should
3137      be all zero.  For signed fields, the high-order bits should agree with
3138      the sign bit.  */
3139
3140   if (lunsignedp)
3141     {
3142       if (! integer_zerop (const_binop (RSHIFT_EXPR,
3143                                         fold_convert (unsigned_type, rhs),
3144                                         size_int (lbitsize), 0)))
3145         {
3146           warning ("comparison is always %d due to width of bit-field",
3147                    code == NE_EXPR);
3148           return constant_boolean_node (code == NE_EXPR, compare_type);
3149         }
3150     }
3151   else
3152     {
3153       tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3154                               size_int (lbitsize - 1), 0);
3155       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3156         {
3157           warning ("comparison is always %d due to width of bit-field",
3158                    code == NE_EXPR);
3159           return constant_boolean_node (code == NE_EXPR, compare_type);
3160         }
3161     }
3162
3163   /* Single-bit compares should always be against zero.  */
3164   if (lbitsize == 1 && ! integer_zerop (rhs))
3165     {
3166       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3167       rhs = fold_convert (type, integer_zero_node);
3168     }
3169
3170   /* Make a new bitfield reference, shift the constant over the
3171      appropriate number of bits and mask it with the computed mask
3172      (in case this was a signed field).  If we changed it, make a new one.  */
3173   lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3174   if (lvolatilep)
3175     {
3176       TREE_SIDE_EFFECTS (lhs) = 1;
3177       TREE_THIS_VOLATILE (lhs) = 1;
3178     }
3179
3180   rhs = fold (const_binop (BIT_AND_EXPR,
3181                            const_binop (LSHIFT_EXPR,
3182                                         fold_convert (unsigned_type, rhs),
3183                                         size_int (lbitpos), 0),
3184                            mask, 0));
3185
3186   return build2 (code, compare_type,
3187                  build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3188                  rhs);
3189 }
3190 \f
3191 /* Subroutine for fold_truthop: decode a field reference.
3192
3193    If EXP is a comparison reference, we return the innermost reference.
3194
3195    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3196    set to the starting bit number.
3197
3198    If the innermost field can be completely contained in a mode-sized
3199    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
3200
3201    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3202    otherwise it is not changed.
3203
3204    *PUNSIGNEDP is set to the signedness of the field.
3205
3206    *PMASK is set to the mask used.  This is either contained in a
3207    BIT_AND_EXPR or derived from the width of the field.
3208
3209    *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3210
3211    Return 0 if this is not a component reference or is one that we can't
3212    do anything with.  */
3213
3214 static tree
3215 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3216                         HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3217                         int *punsignedp, int *pvolatilep,
3218                         tree *pmask, tree *pand_mask)
3219 {
3220   tree outer_type = 0;
3221   tree and_mask = 0;
3222   tree mask, inner, offset;
3223   tree unsigned_type;
3224   unsigned int precision;
3225
3226   /* All the optimizations using this function assume integer fields.
3227      There are problems with FP fields since the type_for_size call
3228      below can fail for, e.g., XFmode.  */
3229   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3230     return 0;
3231
3232   /* We are interested in the bare arrangement of bits, so strip everything
3233      that doesn't affect the machine mode.  However, record the type of the
3234      outermost expression if it may matter below.  */
3235   if (TREE_CODE (exp) == NOP_EXPR
3236       || TREE_CODE (exp) == CONVERT_EXPR
3237       || TREE_CODE (exp) == NON_LVALUE_EXPR)
3238     outer_type = TREE_TYPE (exp);
3239   STRIP_NOPS (exp);
3240
3241   if (TREE_CODE (exp) == BIT_AND_EXPR)
3242     {
3243       and_mask = TREE_OPERAND (exp, 1);
3244       exp = TREE_OPERAND (exp, 0);
3245       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3246       if (TREE_CODE (and_mask) != INTEGER_CST)
3247         return 0;
3248     }
3249
3250   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3251                                punsignedp, pvolatilep);
3252   if ((inner == exp && and_mask == 0)
3253       || *pbitsize < 0 || offset != 0
3254       || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3255     return 0;
3256
3257   /* If the number of bits in the reference is the same as the bitsize of
3258      the outer type, then the outer type gives the signedness. Otherwise
3259      (in case of a small bitfield) the signedness is unchanged.  */
3260   if (outer_type && *pbitsize == tree_low_cst (TYPE_SIZE (outer_type), 1))
3261     *punsignedp = TYPE_UNSIGNED (outer_type);
3262
3263   /* Compute the mask to access the bitfield.  */
3264   unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3265   precision = TYPE_PRECISION (unsigned_type);
3266
3267   mask = build_int_2 (~0, ~0);
3268   TREE_TYPE (mask) = unsigned_type;
3269   force_fit_type (mask, 0);
3270   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3271   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3272
3273   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
3274   if (and_mask != 0)
3275     mask = fold (build2 (BIT_AND_EXPR, unsigned_type,
3276                          fold_convert (unsigned_type, and_mask), mask));
3277
3278   *pmask = mask;
3279   *pand_mask = and_mask;
3280   return inner;
3281 }
3282
3283 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3284    bit positions.  */
3285
3286 static int
3287 all_ones_mask_p (tree mask, int size)
3288 {
3289   tree type = TREE_TYPE (mask);
3290   unsigned int precision = TYPE_PRECISION (type);
3291   tree tmask;
3292
3293   tmask = build_int_2 (~0, ~0);
3294   TREE_TYPE (tmask) = lang_hooks.types.signed_type (type);
3295   force_fit_type (tmask, 0);
3296   return
3297     tree_int_cst_equal (mask,
3298                         const_binop (RSHIFT_EXPR,
3299                                      const_binop (LSHIFT_EXPR, tmask,
3300                                                   size_int (precision - size),
3301                                                   0),
3302                                      size_int (precision - size), 0));
3303 }
3304
3305 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3306    represents the sign bit of EXP's type.  If EXP represents a sign
3307    or zero extension, also test VAL against the unextended type.
3308    The return value is the (sub)expression whose sign bit is VAL,
3309    or NULL_TREE otherwise.  */
3310
3311 static tree
3312 sign_bit_p (tree exp, tree val)
3313 {
3314   unsigned HOST_WIDE_INT mask_lo, lo;
3315   HOST_WIDE_INT mask_hi, hi;
3316   int width;
3317   tree t;
3318
3319   /* Tree EXP must have an integral type.  */
3320   t = TREE_TYPE (exp);
3321   if (! INTEGRAL_TYPE_P (t))
3322     return NULL_TREE;
3323
3324   /* Tree VAL must be an integer constant.  */
3325   if (TREE_CODE (val) != INTEGER_CST
3326       || TREE_CONSTANT_OVERFLOW (val))
3327     return NULL_TREE;
3328
3329   width = TYPE_PRECISION (t);
3330   if (width > HOST_BITS_PER_WIDE_INT)
3331     {
3332       hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3333       lo = 0;
3334
3335       mask_hi = ((unsigned HOST_WIDE_INT) -1
3336                  >> (2 * HOST_BITS_PER_WIDE_INT - width));
3337       mask_lo = -1;
3338     }
3339   else
3340     {
3341       hi = 0;
3342       lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3343
3344       mask_hi = 0;
3345       mask_lo = ((unsigned HOST_WIDE_INT) -1
3346                  >> (HOST_BITS_PER_WIDE_INT - width));
3347     }
3348
3349   /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3350      treat VAL as if it were unsigned.  */
3351   if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3352       && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3353     return exp;
3354
3355   /* Handle extension from a narrower type.  */
3356   if (TREE_CODE (exp) == NOP_EXPR
3357       && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3358     return sign_bit_p (TREE_OPERAND (exp, 0), val);
3359
3360   return NULL_TREE;
3361 }
3362
3363 /* Subroutine for fold_truthop: determine if an operand is simple enough
3364    to be evaluated unconditionally.  */
3365
3366 static int
3367 simple_operand_p (tree exp)
3368 {
3369   /* Strip any conversions that don't change the machine mode.  */
3370   while ((TREE_CODE (exp) == NOP_EXPR
3371           || TREE_CODE (exp) == CONVERT_EXPR)
3372          && (TYPE_MODE (TREE_TYPE (exp))
3373              == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3374     exp = TREE_OPERAND (exp, 0);
3375
3376   return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3377           || (DECL_P (exp)
3378               && ! TREE_ADDRESSABLE (exp)
3379               && ! TREE_THIS_VOLATILE (exp)
3380               && ! DECL_NONLOCAL (exp)
3381               /* Don't regard global variables as simple.  They may be
3382                  allocated in ways unknown to the compiler (shared memory,
3383                  #pragma weak, etc).  */
3384               && ! TREE_PUBLIC (exp)
3385               && ! DECL_EXTERNAL (exp)
3386               /* Loading a static variable is unduly expensive, but global
3387                  registers aren't expensive.  */
3388               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3389 }
3390 \f
3391 /* The following functions are subroutines to fold_range_test and allow it to
3392    try to change a logical combination of comparisons into a range test.
3393
3394    For example, both
3395         X == 2 || X == 3 || X == 4 || X == 5
3396    and
3397         X >= 2 && X <= 5
3398    are converted to
3399         (unsigned) (X - 2) <= 3
3400
3401    We describe each set of comparisons as being either inside or outside
3402    a range, using a variable named like IN_P, and then describe the
3403    range with a lower and upper bound.  If one of the bounds is omitted,
3404    it represents either the highest or lowest value of the type.
3405
3406    In the comments below, we represent a range by two numbers in brackets
3407    preceded by a "+" to designate being inside that range, or a "-" to
3408    designate being outside that range, so the condition can be inverted by
3409    flipping the prefix.  An omitted bound is represented by a "-".  For
3410    example, "- [-, 10]" means being outside the range starting at the lowest
3411    possible value and ending at 10, in other words, being greater than 10.
3412    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3413    always false.
3414
3415    We set up things so that the missing bounds are handled in a consistent
3416    manner so neither a missing bound nor "true" and "false" need to be
3417    handled using a special case.  */
3418
3419 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3420    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3421    and UPPER1_P are nonzero if the respective argument is an upper bound
3422    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
3423    must be specified for a comparison.  ARG1 will be converted to ARG0's
3424    type if both are specified.  */
3425
3426 static tree
3427 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3428              tree arg1, int upper1_p)
3429 {
3430   tree tem;
3431   int result;
3432   int sgn0, sgn1;
3433
3434   /* If neither arg represents infinity, do the normal operation.
3435      Else, if not a comparison, return infinity.  Else handle the special
3436      comparison rules. Note that most of the cases below won't occur, but
3437      are handled for consistency.  */
3438
3439   if (arg0 != 0 && arg1 != 0)
3440     {
3441       tem = fold (build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3442                           arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3443       STRIP_NOPS (tem);
3444       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3445     }
3446
3447   if (TREE_CODE_CLASS (code) != '<')
3448     return 0;
3449
3450   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3451      for neither.  In real maths, we cannot assume open ended ranges are
3452      the same. But, this is computer arithmetic, where numbers are finite.
3453      We can therefore make the transformation of any unbounded range with
3454      the value Z, Z being greater than any representable number. This permits
3455      us to treat unbounded ranges as equal.  */
3456   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3457   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3458   switch (code)
3459     {
3460     case EQ_EXPR:
3461       result = sgn0 == sgn1;
3462       break;
3463     case NE_EXPR:
3464       result = sgn0 != sgn1;
3465       break;
3466     case LT_EXPR:
3467       result = sgn0 < sgn1;
3468       break;
3469     case LE_EXPR:
3470       result = sgn0 <= sgn1;
3471       break;
3472     case GT_EXPR:
3473       result = sgn0 > sgn1;
3474       break;
3475     case GE_EXPR:
3476       result = sgn0 >= sgn1;
3477       break;
3478     default:
3479       abort ();
3480     }
3481
3482   return constant_boolean_node (result, type);
3483 }
3484 \f
3485 /* Given EXP, a logical expression, set the range it is testing into
3486    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
3487    actually being tested.  *PLOW and *PHIGH will be made of the same type
3488    as the returned expression.  If EXP is not a comparison, we will most
3489    likely not be returning a useful value and range.  */
3490
3491 static tree
3492 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3493 {
3494   enum tree_code code;
3495   tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3496   tree orig_type = NULL_TREE;
3497   int in_p, n_in_p;
3498   tree low, high, n_low, n_high;
3499
3500   /* Start with simply saying "EXP != 0" and then look at the code of EXP
3501      and see if we can refine the range.  Some of the cases below may not
3502      happen, but it doesn't seem worth worrying about this.  We "continue"
3503      the outer loop when we've changed something; otherwise we "break"
3504      the switch, which will "break" the while.  */
3505
3506   in_p = 0;
3507   low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3508
3509   while (1)
3510     {
3511       code = TREE_CODE (exp);
3512
3513       if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3514         {
3515           if (first_rtl_op (code) > 0)
3516             arg0 = TREE_OPERAND (exp, 0);
3517           if (TREE_CODE_CLASS (code) == '<'
3518               || TREE_CODE_CLASS (code) == '1'
3519               || TREE_CODE_CLASS (code) == '2')
3520             type = TREE_TYPE (arg0);
3521           if (TREE_CODE_CLASS (code) == '2'
3522               || TREE_CODE_CLASS (code) == '<'
3523               || (TREE_CODE_CLASS (code) == 'e'
3524                   && TREE_CODE_LENGTH (code) > 1))
3525             arg1 = TREE_OPERAND (exp, 1);
3526         }
3527
3528       /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3529          lose a cast by accident.  */
3530       if (type != NULL_TREE && orig_type == NULL_TREE)
3531         orig_type = type;
3532
3533       switch (code)
3534         {
3535         case TRUTH_NOT_EXPR:
3536           in_p = ! in_p, exp = arg0;
3537           continue;
3538
3539         case EQ_EXPR: case NE_EXPR:
3540         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3541           /* We can only do something if the range is testing for zero
3542              and if the second operand is an integer constant.  Note that
3543              saying something is "in" the range we make is done by
3544              complementing IN_P since it will set in the initial case of
3545              being not equal to zero; "out" is leaving it alone.  */
3546           if (low == 0 || high == 0
3547               || ! integer_zerop (low) || ! integer_zerop (high)
3548               || TREE_CODE (arg1) != INTEGER_CST)
3549             break;
3550
3551           switch (code)
3552             {
3553             case NE_EXPR:  /* - [c, c]  */
3554               low = high = arg1;
3555               break;
3556             case EQ_EXPR:  /* + [c, c]  */
3557               in_p = ! in_p, low = high = arg1;
3558               break;
3559             case GT_EXPR:  /* - [-, c] */
3560               low = 0, high = arg1;
3561               break;
3562             case GE_EXPR:  /* + [c, -] */
3563               in_p = ! in_p, low = arg1, high = 0;
3564               break;
3565             case LT_EXPR:  /* - [c, -] */
3566               low = arg1, high = 0;
3567               break;
3568             case LE_EXPR:  /* + [-, c] */
3569               in_p = ! in_p, low = 0, high = arg1;
3570               break;
3571             default:
3572               abort ();
3573             }
3574
3575           exp = arg0;
3576
3577           /* If this is an unsigned comparison, we also know that EXP is
3578              greater than or equal to zero.  We base the range tests we make
3579              on that fact, so we record it here so we can parse existing
3580              range tests.  */
3581           if (TYPE_UNSIGNED (type) && (low == 0 || high == 0))
3582             {
3583               if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3584                                   1, fold_convert (type, integer_zero_node),
3585                                   NULL_TREE))
3586                 break;
3587
3588               in_p = n_in_p, low = n_low, high = n_high;
3589
3590               /* If the high bound is missing, but we have a nonzero low
3591                  bound, reverse the range so it goes from zero to the low bound
3592                  minus 1.  */
3593               if (high == 0 && low && ! integer_zerop (low))
3594                 {
3595                   in_p = ! in_p;
3596                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3597                                       integer_one_node, 0);
3598                   low = fold_convert (type, integer_zero_node);
3599                 }
3600             }
3601           continue;
3602
3603         case NEGATE_EXPR:
3604           /* (-x) IN [a,b] -> x in [-b, -a]  */
3605           n_low = range_binop (MINUS_EXPR, type,
3606                                fold_convert (type, integer_zero_node),
3607                                0, high, 1);
3608           n_high = range_binop (MINUS_EXPR, type,
3609                                 fold_convert (type, integer_zero_node),
3610                                 0, low, 0);
3611           low = n_low, high = n_high;
3612           exp = arg0;
3613           continue;
3614
3615         case BIT_NOT_EXPR:
3616           /* ~ X -> -X - 1  */
3617           exp = build2 (MINUS_EXPR, type, negate_expr (arg0),
3618                         fold_convert (type, integer_one_node));
3619           continue;
3620
3621         case PLUS_EXPR:  case MINUS_EXPR:
3622           if (TREE_CODE (arg1) != INTEGER_CST)
3623             break;
3624
3625           /* If EXP is signed, any overflow in the computation is undefined,
3626              so we don't worry about it so long as our computations on
3627              the bounds don't overflow.  For unsigned, overflow is defined
3628              and this is exactly the right thing.  */
3629           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3630                                type, low, 0, arg1, 0);
3631           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3632                                 type, high, 1, arg1, 0);
3633           if ((n_low != 0 && TREE_OVERFLOW (n_low))
3634               || (n_high != 0 && TREE_OVERFLOW (n_high)))
3635             break;
3636
3637           /* Check for an unsigned range which has wrapped around the maximum
3638              value thus making n_high < n_low, and normalize it.  */
3639           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3640             {
3641               low = range_binop (PLUS_EXPR, type, n_high, 0,
3642                                  integer_one_node, 0);
3643               high = range_binop (MINUS_EXPR, type, n_low, 0,
3644                                   integer_one_node, 0);
3645
3646               /* If the range is of the form +/- [ x+1, x ], we won't
3647                  be able to normalize it.  But then, it represents the
3648                  whole range or the empty set, so make it
3649                  +/- [ -, - ].  */
3650               if (tree_int_cst_equal (n_low, low)
3651                   && tree_int_cst_equal (n_high, high))
3652                 low = high = 0;
3653               else
3654                 in_p = ! in_p;
3655             }
3656           else
3657             low = n_low, high = n_high;
3658
3659           exp = arg0;
3660           continue;
3661
3662         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
3663           if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3664             break;
3665
3666           if (! INTEGRAL_TYPE_P (type)
3667               || (low != 0 && ! int_fits_type_p (low, type))
3668               || (high != 0 && ! int_fits_type_p (high, type)))
3669             break;
3670
3671           n_low = low, n_high = high;
3672
3673           if (n_low != 0)
3674             n_low = fold_convert (type, n_low);
3675
3676           if (n_high != 0)
3677             n_high = fold_convert (type, n_high);
3678
3679           /* If we're converting from an unsigned to a signed type,
3680              we will be doing the comparison as unsigned.  The tests above
3681              have already verified that LOW and HIGH are both positive.
3682
3683              So we have to make sure that the original unsigned value will
3684              be interpreted as positive.  */
3685           if (TYPE_UNSIGNED (type) && ! TYPE_UNSIGNED (TREE_TYPE (exp)))
3686             {
3687               tree equiv_type = lang_hooks.types.type_for_mode
3688                 (TYPE_MODE (type), 1);
3689               tree high_positive;
3690
3691               /* A range without an upper bound is, naturally, unbounded.
3692                  Since convert would have cropped a very large value, use
3693                  the max value for the destination type.  */
3694               high_positive
3695                 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3696                   : TYPE_MAX_VALUE (type);
3697
3698               if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (exp)))
3699                 high_positive = fold (build2 (RSHIFT_EXPR, type,
3700                                               fold_convert (type,
3701                                                             high_positive),
3702                                               fold_convert (type,
3703                                                             integer_one_node)));
3704
3705               /* If the low bound is specified, "and" the range with the
3706                  range for which the original unsigned value will be
3707                  positive.  */
3708               if (low != 0)
3709                 {
3710                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
3711                                       1, n_low, n_high, 1,
3712                                       fold_convert (type, integer_zero_node),
3713                                       high_positive))
3714                     break;
3715
3716                   in_p = (n_in_p == in_p);
3717                 }
3718               else
3719                 {
3720                   /* Otherwise, "or" the range with the range of the input
3721                      that will be interpreted as negative.  */
3722                   if (! merge_ranges (&n_in_p, &n_low, &n_high,
3723                                       0, n_low, n_high, 1,
3724                                       fold_convert (type, integer_zero_node),
3725                                       high_positive))
3726                     break;
3727
3728                   in_p = (in_p != n_in_p);
3729                 }
3730             }
3731
3732           exp = arg0;
3733           low = n_low, high = n_high;
3734           continue;
3735
3736         default:
3737           break;
3738         }
3739
3740       break;
3741     }
3742
3743   /* If EXP is a constant, we can evaluate whether this is true or false.  */
3744   if (TREE_CODE (exp) == INTEGER_CST)
3745     {
3746       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3747                                                  exp, 0, low, 0))
3748                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
3749                                                     exp, 1, high, 1)));
3750       low = high = 0;
3751       exp = 0;
3752     }
3753
3754   *pin_p = in_p, *plow = low, *phigh = high;
3755   return exp;
3756 }
3757 \f
3758 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3759    type, TYPE, return an expression to test if EXP is in (or out of, depending
3760    on IN_P) the range.  */
3761
3762 static tree
3763 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3764 {
3765   tree etype = TREE_TYPE (exp);
3766   tree value;
3767
3768   if (! in_p
3769       && (0 != (value = build_range_check (type, exp, 1, low, high))))
3770     return invert_truthvalue (value);
3771
3772   if (low == 0 && high == 0)
3773     return fold_convert (type, integer_one_node);
3774
3775   if (low == 0)
3776     return fold (build2 (LE_EXPR, type, exp, high));
3777
3778   if (high == 0)
3779     return fold (build2 (GE_EXPR, type, exp, low));
3780
3781   if (operand_equal_p (low, high, 0))
3782     return fold (build2 (EQ_EXPR, type, exp, low));
3783
3784   if (integer_zerop (low))
3785     {
3786       if (! TYPE_UNSIGNED (etype))
3787         {
3788           etype = lang_hooks.types.unsigned_type (etype);
3789           high = fold_convert (etype, high);
3790           exp = fold_convert (etype, exp);
3791         }
3792       return build_range_check (type, exp, 1, 0, high);
3793     }
3794
3795   /* Optimize (c>=1) && (c<=127) into (signed char)c > 0.  */
3796   if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3797     {
3798       unsigned HOST_WIDE_INT lo;
3799       HOST_WIDE_INT hi;
3800       int prec;
3801
3802       prec = TYPE_PRECISION (etype);
3803       if (prec <= HOST_BITS_PER_WIDE_INT)
3804         {
3805           hi = 0;
3806           lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3807         }
3808       else
3809         {
3810           hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3811           lo = (unsigned HOST_WIDE_INT) -1;
3812         }
3813
3814       if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3815         {
3816           if (TYPE_UNSIGNED (etype))
3817             {
3818               etype = lang_hooks.types.signed_type (etype);
3819               exp = fold_convert (etype, exp);
3820             }
3821           return fold (build2 (GT_EXPR, type, exp,
3822                                fold_convert (etype, integer_zero_node)));
3823         }
3824     }
3825
3826   if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3827       && ! TREE_OVERFLOW (value))
3828     return build_range_check (type,
3829                               fold (build2 (MINUS_EXPR, etype, exp, low)),
3830                               1, fold_convert (etype, integer_zero_node),
3831                               value);
3832
3833   return 0;
3834 }
3835 \f
3836 /* Given two ranges, see if we can merge them into one.  Return 1 if we
3837    can, 0 if we can't.  Set the output range into the specified parameters.  */
3838
3839 static int
3840 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3841               tree high0, int in1_p, tree low1, tree high1)
3842 {
3843   int no_overlap;
3844   int subset;
3845   int temp;
3846   tree tem;
3847   int in_p;
3848   tree low, high;
3849   int lowequal = ((low0 == 0 && low1 == 0)
3850                   || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3851                                                 low0, 0, low1, 0)));
3852   int highequal = ((high0 == 0 && high1 == 0)
3853                    || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3854                                                  high0, 1, high1, 1)));
3855
3856   /* Make range 0 be the range that starts first, or ends last if they
3857      start at the same value.  Swap them if it isn't.  */
3858   if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3859                                  low0, 0, low1, 0))
3860       || (lowequal
3861           && integer_onep (range_binop (GT_EXPR, integer_type_node,
3862                                         high1, 1, high0, 1))))
3863     {
3864       temp = in0_p, in0_p = in1_p, in1_p = temp;
3865       tem = low0, low0 = low1, low1 = tem;
3866       tem = high0, high0 = high1, high1 = tem;
3867     }
3868
3869   /* Now flag two cases, whether the ranges are disjoint or whether the
3870      second range is totally subsumed in the first.  Note that the tests
3871      below are simplified by the ones above.  */
3872   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3873                                           high0, 1, low1, 0));
3874   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3875                                       high1, 1, high0, 1));
3876
3877   /* We now have four cases, depending on whether we are including or
3878      excluding the two ranges.  */
3879   if (in0_p && in1_p)
3880     {
3881       /* If they don't overlap, the result is false.  If the second range
3882          is a subset it is the result.  Otherwise, the range is from the start
3883          of the second to the end of the first.  */
3884       if (no_overlap)
3885         in_p = 0, low = high = 0;
3886       else if (subset)
3887         in_p = 1, low = low1, high = high1;
3888       else
3889         in_p = 1, low = low1, high = high0;
3890     }
3891
3892   else if (in0_p && ! in1_p)
3893     {
3894       /* If they don't overlap, the result is the first range.  If they are
3895          equal, the result is false.  If the second range is a subset of the
3896          first, and the ranges begin at the same place, we go from just after
3897          the end of the first range to the end of the second.  If the second
3898          range is not a subset of the first, or if it is a subset and both
3899          ranges end at the same place, the range starts at the start of the
3900          first range and ends just before the second range.
3901          Otherwise, we can't describe this as a single range.  */
3902       if (no_overlap)
3903         in_p = 1, low = low0, high = high0;
3904       else if (lowequal && highequal)
3905         in_p = 0, low = high = 0;
3906       else if (subset && lowequal)
3907         {
3908           in_p = 1, high = high0;
3909           low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3910                              integer_one_node, 0);
3911         }
3912       else if (! subset || highequal)
3913         {
3914           in_p = 1, low = low0;
3915           high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3916                               integer_one_node, 0);
3917         }
3918       else
3919         return 0;
3920     }
3921
3922   else if (! in0_p && in1_p)
3923     {
3924       /* If they don't overlap, the result is the second range.  If the second
3925          is a subset of the first, the result is false.  Otherwise,
3926          the range starts just after the first range and ends at the
3927          end of the second.  */
3928       if (no_overlap)
3929         in_p = 1, low = low1, high = high1;
3930       else if (subset || highequal)
3931         in_p = 0, low = high = 0;
3932       else
3933         {
3934           in_p = 1, high = high1;
3935           low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3936                              integer_one_node, 0);
3937         }
3938     }
3939
3940   else
3941     {
3942       /* The case where we are excluding both ranges.  Here the complex case
3943          is if they don't overlap.  In that case, the only time we have a
3944          range is if they are adjacent.  If the second is a subset of the
3945          first, the result is the first.  Otherwise, the range to exclude
3946          starts at the beginning of the first range and ends at the end of the
3947          second.  */
3948       if (no_overlap)
3949         {
3950           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3951                                          range_binop (PLUS_EXPR, NULL_TREE,
3952                                                       high0, 1,
3953                                                       integer_one_node, 1),
3954                                          1, low1, 0)))
3955             in_p = 0, low = low0, high = high1;
3956           else
3957             return 0;
3958         }
3959       else if (subset)
3960         in_p = 0, low = low0, high = high0;
3961       else
3962         in_p = 0, low = low0, high = high1;
3963     }
3964
3965   *pin_p = in_p, *plow = low, *phigh = high;
3966   return 1;
3967 }
3968 \f
3969 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
3970 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
3971 #endif
3972
3973 /* EXP is some logical combination of boolean tests.  See if we can
3974    merge it into some range test.  Return the new tree if so.  */
3975
3976 static tree
3977 fold_range_test (tree exp)
3978 {
3979   int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3980                || TREE_CODE (exp) == TRUTH_OR_EXPR);
3981   int in0_p, in1_p, in_p;
3982   tree low0, low1, low, high0, high1, high;
3983   tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3984   tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3985   tree tem;
3986
3987   /* If this is an OR operation, invert both sides; we will invert
3988      again at the end.  */
3989   if (or_op)
3990     in0_p = ! in0_p, in1_p = ! in1_p;
3991
3992   /* If both expressions are the same, if we can merge the ranges, and we
3993      can build the range test, return it or it inverted.  If one of the
3994      ranges is always true or always false, consider it to be the same
3995      expression as the other.  */
3996   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3997       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3998                        in1_p, low1, high1)
3999       && 0 != (tem = (build_range_check (TREE_TYPE (exp),
4000                                          lhs != 0 ? lhs
4001                                          : rhs != 0 ? rhs : integer_zero_node,
4002                                          in_p, low, high))))
4003     return or_op ? invert_truthvalue (tem) : tem;
4004
4005   /* On machines where the branch cost is expensive, if this is a
4006      short-circuited branch and the underlying object on both sides
4007      is the same, make a non-short-circuit operation.  */
4008   else if (RANGE_TEST_NON_SHORT_CIRCUIT
4009            && lhs != 0 && rhs != 0
4010            && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4011                || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
4012            && operand_equal_p (lhs, rhs, 0))
4013     {
4014       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR
4015          unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4016          which cases we can't do this.  */
4017       if (simple_operand_p (lhs))
4018         return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4019                        ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4020                        TREE_TYPE (exp), TREE_OPERAND (exp, 0),
4021                        TREE_OPERAND (exp, 1));
4022
4023       else if (lang_hooks.decls.global_bindings_p () == 0
4024                && ! CONTAINS_PLACEHOLDER_P (lhs))
4025         {
4026           tree common = save_expr (lhs);
4027
4028           if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
4029                                              or_op ? ! in0_p : in0_p,
4030                                              low0, high0))
4031               && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
4032                                                  or_op ? ! in1_p : in1_p,
4033                                                  low1, high1))))
4034             return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4035                            ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4036                            TREE_TYPE (exp), lhs, rhs);
4037         }
4038     }
4039
4040   return 0;
4041 }
4042 \f
4043 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4044    bit value.  Arrange things so the extra bits will be set to zero if and
4045    only if C is signed-extended to its full width.  If MASK is nonzero,
4046    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
4047
4048 static tree
4049 unextend (tree c, int p, int unsignedp, tree mask)
4050 {
4051   tree type = TREE_TYPE (c);
4052   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4053   tree temp;
4054
4055   if (p == modesize || unsignedp)
4056     return c;
4057
4058   /* We work by getting just the sign bit into the low-order bit, then
4059      into the high-order bit, then sign-extend.  We then XOR that value
4060      with C.  */
4061   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4062   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4063
4064   /* We must use a signed type in order to get an arithmetic right shift.
4065      However, we must also avoid introducing accidental overflows, so that
4066      a subsequent call to integer_zerop will work.  Hence we must
4067      do the type conversion here.  At this point, the constant is either
4068      zero or one, and the conversion to a signed type can never overflow.
4069      We could get an overflow if this conversion is done anywhere else.  */
4070   if (TYPE_UNSIGNED (type))
4071     temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4072
4073   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4074   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4075   if (mask != 0)
4076     temp = const_binop (BIT_AND_EXPR, temp,
4077                         fold_convert (TREE_TYPE (c), mask), 0);
4078   /* If necessary, convert the type back to match the type of C.  */
4079   if (TYPE_UNSIGNED (type))
4080     temp = fold_convert (type, temp);
4081
4082   return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4083 }
4084 \f
4085 /* Find ways of folding logical expressions of LHS and RHS:
4086    Try to merge two comparisons to the same innermost item.
4087    Look for range tests like "ch >= '0' && ch <= '9'".
4088    Look for combinations of simple terms on machines with expensive branches
4089    and evaluate the RHS unconditionally.
4090
4091    For example, if we have p->a == 2 && p->b == 4 and we can make an
4092    object large enough to span both A and B, we can do this with a comparison
4093    against the object ANDed with the a mask.
4094
4095    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4096    operations to do this with one comparison.
4097
4098    We check for both normal comparisons and the BIT_AND_EXPRs made this by
4099    function and the one above.
4100
4101    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
4102    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4103
4104    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4105    two operands.
4106
4107    We return the simplified tree or 0 if no optimization is possible.  */
4108
4109 static tree
4110 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4111 {
4112   /* If this is the "or" of two comparisons, we can do something if
4113      the comparisons are NE_EXPR.  If this is the "and", we can do something
4114      if the comparisons are EQ_EXPR.  I.e.,
4115         (a->b == 2 && a->c == 4) can become (a->new == NEW).
4116
4117      WANTED_CODE is this operation code.  For single bit fields, we can
4118      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4119      comparison for one-bit fields.  */
4120
4121   enum tree_code wanted_code;
4122   enum tree_code lcode, rcode;
4123   tree ll_arg, lr_arg, rl_arg, rr_arg;
4124   tree ll_inner, lr_inner, rl_inner, rr_inner;
4125   HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4126   HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4127   HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4128   HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4129   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4130   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4131   enum machine_mode lnmode, rnmode;
4132   tree ll_mask, lr_mask, rl_mask, rr_mask;
4133   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4134   tree l_const, r_const;
4135   tree lntype, rntype, result;
4136   int first_bit, end_bit;
4137   int volatilep;
4138
4139   /* Start by getting the comparison codes.  Fail if anything is volatile.
4140      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4141      it were surrounded with a NE_EXPR.  */
4142
4143   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4144     return 0;
4145
4146   lcode = TREE_CODE (lhs);
4147   rcode = TREE_CODE (rhs);
4148
4149   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4150     {
4151       lhs = build2 (NE_EXPR, truth_type, lhs, integer_zero_node);
4152       lcode = NE_EXPR;
4153     }
4154
4155   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4156     {
4157       rhs = build2 (NE_EXPR, truth_type, rhs, integer_zero_node);
4158       rcode = NE_EXPR;
4159     }
4160
4161   if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
4162     return 0;
4163
4164   ll_arg = TREE_OPERAND (lhs, 0);
4165   lr_arg = TREE_OPERAND (lhs, 1);
4166   rl_arg = TREE_OPERAND (rhs, 0);
4167   rr_arg = TREE_OPERAND (rhs, 1);
4168
4169   /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations.  */
4170   if (simple_operand_p (ll_arg)
4171       && simple_operand_p (lr_arg))
4172     {
4173       tree result;
4174       if (operand_equal_p (ll_arg, rl_arg, 0)
4175           && operand_equal_p (lr_arg, rr_arg, 0))
4176         {
4177           result = combine_comparisons (code, lcode, rcode,
4178                                         truth_type, ll_arg, lr_arg);
4179           if (result)
4180             return result;
4181         }
4182       else if (operand_equal_p (ll_arg, rr_arg, 0)
4183                && operand_equal_p (lr_arg, rl_arg, 0))
4184         {
4185           result = combine_comparisons (code, lcode,
4186                                         swap_tree_comparison (rcode),
4187                                         truth_type, ll_arg, lr_arg);
4188           if (result)
4189             return result;
4190         }
4191     }
4192
4193   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4194           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4195
4196   /* If the RHS can be evaluated unconditionally and its operands are
4197      simple, it wins to evaluate the RHS unconditionally on machines
4198      with expensive branches.  In this case, this isn't a comparison
4199      that can be merged.  Avoid doing this if the RHS is a floating-point
4200      comparison since those can trap.  */
4201
4202   if (BRANCH_COST >= 2
4203       && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4204       && simple_operand_p (rl_arg)
4205       && simple_operand_p (rr_arg))
4206     {
4207       /* Convert (a != 0) || (b != 0) into (a | b) != 0.  */
4208       if (code == TRUTH_OR_EXPR
4209           && lcode == NE_EXPR && integer_zerop (lr_arg)
4210           && rcode == NE_EXPR && integer_zerop (rr_arg)
4211           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4212         return build2 (NE_EXPR, truth_type,
4213                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4214                                ll_arg, rl_arg),
4215                        fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4216
4217       /* Convert (a == 0) && (b == 0) into (a | b) == 0.  */
4218       if (code == TRUTH_AND_EXPR
4219           && lcode == EQ_EXPR && integer_zerop (lr_arg)
4220           && rcode == EQ_EXPR && integer_zerop (rr_arg)
4221           && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4222         return build2 (EQ_EXPR, truth_type,
4223                        build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4224                                ll_arg, rl_arg),
4225                        fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4226
4227       return build2 (code, truth_type, lhs, rhs);
4228     }
4229
4230   /* See if the comparisons can be merged.  Then get all the parameters for
4231      each side.  */
4232
4233   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4234       || (rcode != EQ_EXPR && rcode != NE_EXPR))
4235     return 0;
4236
4237   volatilep = 0;
4238   ll_inner = decode_field_reference (ll_arg,
4239                                      &ll_bitsize, &ll_bitpos, &ll_mode,
4240                                      &ll_unsignedp, &volatilep, &ll_mask,
4241                                      &ll_and_mask);
4242   lr_inner = decode_field_reference (lr_arg,
4243                                      &lr_bitsize, &lr_bitpos, &lr_mode,
4244                                      &lr_unsignedp, &volatilep, &lr_mask,
4245                                      &lr_and_mask);
4246   rl_inner = decode_field_reference (rl_arg,
4247                                      &rl_bitsize, &rl_bitpos, &rl_mode,
4248                                      &rl_unsignedp, &volatilep, &rl_mask,
4249                                      &rl_and_mask);
4250   rr_inner = decode_field_reference (rr_arg,
4251                                      &rr_bitsize, &rr_bitpos, &rr_mode,
4252                                      &rr_unsignedp, &volatilep, &rr_mask,
4253                                      &rr_and_mask);
4254
4255   /* It must be true that the inner operation on the lhs of each
4256      comparison must be the same if we are to be able to do anything.
4257      Then see if we have constants.  If not, the same must be true for
4258      the rhs's.  */
4259   if (volatilep || ll_inner == 0 || rl_inner == 0
4260       || ! operand_equal_p (ll_inner, rl_inner, 0))
4261     return 0;
4262
4263   if (TREE_CODE (lr_arg) == INTEGER_CST
4264       && TREE_CODE (rr_arg) == INTEGER_CST)
4265     l_const = lr_arg, r_const = rr_arg;
4266   else if (lr_inner == 0 || rr_inner == 0
4267            || ! operand_equal_p (lr_inner, rr_inner, 0))
4268     return 0;
4269   else
4270     l_const = r_const = 0;
4271
4272   /* If either comparison code is not correct for our logical operation,
4273      fail.  However, we can convert a one-bit comparison against zero into
4274      the opposite comparison against that bit being set in the field.  */
4275
4276   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4277   if (lcode != wanted_code)
4278     {
4279       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4280         {
4281           /* Make the left operand unsigned, since we are only interested
4282              in the value of one bit.  Otherwise we are doing the wrong
4283              thing below.  */
4284           ll_unsignedp = 1;
4285           l_const = ll_mask;
4286         }
4287       else
4288         return 0;
4289     }
4290
4291   /* This is analogous to the code for l_const above.  */
4292   if (rcode != wanted_code)
4293     {
4294       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4295         {
4296           rl_unsignedp = 1;
4297           r_const = rl_mask;
4298         }
4299       else
4300         return 0;
4301     }
4302
4303   /* After this point all optimizations will generate bit-field
4304      references, which we might not want.  */
4305   if (! lang_hooks.can_use_bit_fields_p ())
4306     return 0;
4307
4308   /* See if we can find a mode that contains both fields being compared on
4309      the left.  If we can't, fail.  Otherwise, update all constants and masks
4310      to be relative to a field of that size.  */
4311   first_bit = MIN (ll_bitpos, rl_bitpos);
4312   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4313   lnmode = get_best_mode (end_bit - first_bit, first_bit,
4314                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4315                           volatilep);
4316   if (lnmode == VOIDmode)
4317     return 0;
4318
4319   lnbitsize = GET_MODE_BITSIZE (lnmode);
4320   lnbitpos = first_bit & ~ (lnbitsize - 1);
4321   lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4322   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4323
4324   if (BYTES_BIG_ENDIAN)
4325     {
4326       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4327       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4328     }
4329
4330   ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4331                          size_int (xll_bitpos), 0);
4332   rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4333                          size_int (xrl_bitpos), 0);
4334
4335   if (l_const)
4336     {
4337       l_const = fold_convert (lntype, l_const);
4338       l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4339       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4340       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4341                                         fold (build1 (BIT_NOT_EXPR,
4342                                                       lntype, ll_mask)),
4343                                         0)))
4344         {
4345           warning ("comparison is always %d", wanted_code == NE_EXPR);
4346
4347           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4348         }
4349     }
4350   if (r_const)
4351     {
4352       r_const = fold_convert (lntype, r_const);
4353       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4354       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4355       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4356                                         fold (build1 (BIT_NOT_EXPR,
4357                                                       lntype, rl_mask)),
4358                                         0)))
4359         {
4360           warning ("comparison is always %d", wanted_code == NE_EXPR);
4361
4362           return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4363         }
4364     }
4365
4366   /* If the right sides are not constant, do the same for it.  Also,
4367      disallow this optimization if a size or signedness mismatch occurs
4368      between the left and right sides.  */
4369   if (l_const == 0)
4370     {
4371       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4372           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4373           /* Make sure the two fields on the right
4374              correspond to the left without being swapped.  */
4375           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4376         return 0;
4377
4378       first_bit = MIN (lr_bitpos, rr_bitpos);
4379       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4380       rnmode = get_best_mode (end_bit - first_bit, first_bit,
4381                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4382                               volatilep);
4383       if (rnmode == VOIDmode)
4384         return 0;
4385
4386       rnbitsize = GET_MODE_BITSIZE (rnmode);
4387       rnbitpos = first_bit & ~ (rnbitsize - 1);
4388       rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4389       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4390
4391       if (BYTES_BIG_ENDIAN)
4392         {
4393           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4394           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4395         }
4396
4397       lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4398                              size_int (xlr_bitpos), 0);
4399       rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4400                              size_int (xrr_bitpos), 0);
4401
4402       /* Make a mask that corresponds to both fields being compared.
4403          Do this for both items being compared.  If the operands are the
4404          same size and the bits being compared are in the same position
4405          then we can do this by masking both and comparing the masked
4406          results.  */
4407       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4408       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4409       if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4410         {
4411           lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4412                                     ll_unsignedp || rl_unsignedp);
4413           if (! all_ones_mask_p (ll_mask, lnbitsize))
4414             lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4415
4416           rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4417                                     lr_unsignedp || rr_unsignedp);
4418           if (! all_ones_mask_p (lr_mask, rnbitsize))
4419             rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4420
4421           return build2 (wanted_code, truth_type, lhs, rhs);
4422         }
4423
4424       /* There is still another way we can do something:  If both pairs of
4425          fields being compared are adjacent, we may be able to make a wider
4426          field containing them both.
4427
4428          Note that we still must mask the lhs/rhs expressions.  Furthermore,
4429          the mask must be shifted to account for the shift done by
4430          make_bit_field_ref.  */
4431       if ((ll_bitsize + ll_bitpos == rl_bitpos
4432            && lr_bitsize + lr_bitpos == rr_bitpos)
4433           || (ll_bitpos == rl_bitpos + rl_bitsize
4434               && lr_bitpos == rr_bitpos + rr_bitsize))
4435         {
4436           tree type;
4437
4438           lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4439                                     MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4440           rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4441                                     MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4442
4443           ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4444                                  size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4445           lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4446                                  size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4447
4448           /* Convert to the smaller type before masking out unwanted bits.  */
4449           type = lntype;
4450           if (lntype != rntype)
4451             {
4452               if (lnbitsize > rnbitsize)
4453                 {
4454                   lhs = fold_convert (rntype, lhs);
4455                   ll_mask = fold_convert (rntype, ll_mask);
4456                   type = rntype;
4457                 }
4458               else if (lnbitsize < rnbitsize)
4459                 {
4460                   rhs = fold_convert (lntype, rhs);
4461                   lr_mask = fold_convert (lntype, lr_mask);
4462                   type = lntype;
4463                 }
4464             }
4465
4466           if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4467             lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
4468
4469           if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4470             rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
4471
4472           return build2 (wanted_code, truth_type, lhs, rhs);
4473         }
4474
4475       return 0;
4476     }
4477
4478   /* Handle the case of comparisons with constants.  If there is something in
4479      common between the masks, those bits of the constants must be the same.
4480      If not, the condition is always false.  Test for this to avoid generating
4481      incorrect code below.  */
4482   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4483   if (! integer_zerop (result)
4484       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4485                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4486     {
4487       if (wanted_code == NE_EXPR)
4488         {
4489           warning ("`or' of unmatched not-equal tests is always 1");
4490           return constant_boolean_node (true, truth_type);
4491         }
4492       else
4493         {
4494           warning ("`and' of mutually exclusive equal-tests is always 0");
4495           return constant_boolean_node (false, truth_type);
4496         }
4497     }
4498
4499   /* Construct the expression we will return.  First get the component
4500      reference we will make.  Unless the mask is all ones the width of
4501      that field, perform the mask operation.  Then compare with the
4502      merged constant.  */
4503   result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4504                                ll_unsignedp || rl_unsignedp);
4505
4506   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4507   if (! all_ones_mask_p (ll_mask, lnbitsize))
4508     result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
4509
4510   return build2 (wanted_code, truth_type, result,
4511                  const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4512 }
4513 \f
4514 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4515    constant.  */
4516
4517 static tree
4518 optimize_minmax_comparison (tree t)
4519 {
4520   tree type = TREE_TYPE (t);
4521   tree arg0 = TREE_OPERAND (t, 0);
4522   enum tree_code op_code;
4523   tree comp_const = TREE_OPERAND (t, 1);
4524   tree minmax_const;
4525   int consts_equal, consts_lt;
4526   tree inner;
4527
4528   STRIP_SIGN_NOPS (arg0);
4529
4530   op_code = TREE_CODE (arg0);
4531   minmax_const = TREE_OPERAND (arg0, 1);
4532   consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4533   consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4534   inner = TREE_OPERAND (arg0, 0);
4535
4536   /* If something does not permit us to optimize, return the original tree.  */
4537   if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4538       || TREE_CODE (comp_const) != INTEGER_CST
4539       || TREE_CONSTANT_OVERFLOW (comp_const)
4540       || TREE_CODE (minmax_const) != INTEGER_CST
4541       || TREE_CONSTANT_OVERFLOW (minmax_const))
4542     return t;
4543
4544   /* Now handle all the various comparison codes.  We only handle EQ_EXPR
4545      and GT_EXPR, doing the rest with recursive calls using logical
4546      simplifications.  */
4547   switch (TREE_CODE (t))
4548     {
4549     case NE_EXPR:  case LT_EXPR:  case LE_EXPR:
4550       return
4551         invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4552
4553     case GE_EXPR:
4554       return
4555         fold (build2 (TRUTH_ORIF_EXPR, type,
4556                       optimize_minmax_comparison
4557                       (build2 (EQ_EXPR, type, arg0, comp_const)),
4558                       optimize_minmax_comparison
4559                       (build2 (GT_EXPR, type, arg0, comp_const))));
4560
4561     case EQ_EXPR:
4562       if (op_code == MAX_EXPR && consts_equal)
4563         /* MAX (X, 0) == 0  ->  X <= 0  */
4564         return fold (build2 (LE_EXPR, type, inner, comp_const));
4565
4566       else if (op_code == MAX_EXPR && consts_lt)
4567         /* MAX (X, 0) == 5  ->  X == 5   */
4568         return fold (build2 (EQ_EXPR, type, inner, comp_const));
4569
4570       else if (op_code == MAX_EXPR)
4571         /* MAX (X, 0) == -1  ->  false  */
4572         return omit_one_operand (type, integer_zero_node, inner);
4573
4574       else if (consts_equal)
4575         /* MIN (X, 0) == 0  ->  X >= 0  */
4576         return fold (build2 (GE_EXPR, type, inner, comp_const));
4577
4578       else if (consts_lt)
4579         /* MIN (X, 0) == 5  ->  false  */
4580         return omit_one_operand (type, integer_zero_node, inner);
4581
4582       else
4583         /* MIN (X, 0) == -1  ->  X == -1  */
4584         return fold (build2 (EQ_EXPR, type, inner, comp_const));
4585
4586     case GT_EXPR:
4587       if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4588         /* MAX (X, 0) > 0  ->  X > 0
4589            MAX (X, 0) > 5  ->  X > 5  */
4590         return fold (build2 (GT_EXPR, type, inner, comp_const));
4591
4592       else if (op_code == MAX_EXPR)
4593         /* MAX (X, 0) > -1  ->  true  */
4594         return omit_one_operand (type, integer_one_node, inner);
4595
4596       else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4597         /* MIN (X, 0) > 0  ->  false
4598            MIN (X, 0) > 5  ->  false  */
4599         return omit_one_operand (type, integer_zero_node, inner);
4600
4601       else
4602         /* MIN (X, 0) > -1  ->  X > -1  */
4603         return fold (build2 (GT_EXPR, type, inner, comp_const));
4604
4605     default:
4606       return t;
4607     }
4608 }
4609 \f
4610 /* T is an integer expression that is being multiplied, divided, or taken a
4611    modulus (CODE says which and what kind of divide or modulus) by a
4612    constant C.  See if we can eliminate that operation by folding it with
4613    other operations already in T.  WIDE_TYPE, if non-null, is a type that
4614    should be used for the computation if wider than our type.
4615
4616    For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4617    (X * 2) + (Y * 4).  We must, however, be assured that either the original
4618    expression would not overflow or that overflow is undefined for the type
4619    in the language in question.
4620
4621    We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4622    the machine has a multiply-accumulate insn or that this is part of an
4623    addressing calculation.
4624
4625    If we return a non-null expression, it is an equivalent form of the
4626    original computation, but need not be in the original type.  */
4627
4628 static tree
4629 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
4630 {
4631   /* To avoid exponential search depth, refuse to allow recursion past
4632      three levels.  Beyond that (1) it's highly unlikely that we'll find
4633      something interesting and (2) we've probably processed it before
4634      when we built the inner expression.  */
4635
4636   static int depth;
4637   tree ret;
4638
4639   if (depth > 3)
4640     return NULL;
4641
4642   depth++;
4643   ret = extract_muldiv_1 (t, c, code, wide_type);
4644   depth--;
4645
4646   return ret;
4647 }
4648
4649 static tree
4650 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
4651 {
4652   tree type = TREE_TYPE (t);
4653   enum tree_code tcode = TREE_CODE (t);
4654   tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4655                                    > GET_MODE_SIZE (TYPE_MODE (type)))
4656                 ? wide_type : type);
4657   tree t1, t2;
4658   int same_p = tcode == code;
4659   tree op0 = NULL_TREE, op1 = NULL_TREE;
4660
4661   /* Don't deal with constants of zero here; they confuse the code below.  */
4662   if (integer_zerop (c))
4663     return NULL_TREE;
4664
4665   if (TREE_CODE_CLASS (tcode) == '1')
4666     op0 = TREE_OPERAND (t, 0);
4667
4668   if (TREE_CODE_CLASS (tcode) == '2')
4669     op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4670
4671   /* Note that we need not handle conditional operations here since fold
4672      already handles those cases.  So just do arithmetic here.  */
4673   switch (tcode)
4674     {
4675     case INTEGER_CST:
4676       /* For a constant, we can always simplify if we are a multiply
4677          or (for divide and modulus) if it is a multiple of our constant.  */
4678       if (code == MULT_EXPR
4679           || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4680         return const_binop (code, fold_convert (ctype, t),
4681                             fold_convert (ctype, c), 0);
4682       break;
4683
4684     case CONVERT_EXPR:  case NON_LVALUE_EXPR:  case NOP_EXPR:
4685       /* If op0 is an expression ...  */
4686       if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4687            || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4688            || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4689            || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4690           /* ... and is unsigned, and its type is smaller than ctype,
4691              then we cannot pass through as widening.  */
4692           && ((TYPE_UNSIGNED (TREE_TYPE (op0))
4693                && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4694                      && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4695                && (GET_MODE_SIZE (TYPE_MODE (ctype))
4696                    > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4697               /* ... or its type is larger than ctype,
4698                  then we cannot pass through this truncation.  */
4699               || (GET_MODE_SIZE (TYPE_MODE (ctype))
4700                   < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
4701               /* ... or signedness changes for division or modulus,
4702                  then we cannot pass through this conversion.  */
4703               || (code != MULT_EXPR
4704                   && (TYPE_UNSIGNED (ctype)
4705                       != TYPE_UNSIGNED (TREE_TYPE (op0))))))
4706         break;
4707
4708       /* Pass the constant down and see if we can make a simplification.  If
4709          we can, replace this expression with the inner simplification for
4710          possible later conversion to our or some other type.  */
4711       if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
4712           && TREE_CODE (t2) == INTEGER_CST
4713           && ! TREE_CONSTANT_OVERFLOW (t2)
4714           && (0 != (t1 = extract_muldiv (op0, t2, code,
4715                                          code == MULT_EXPR
4716                                          ? ctype : NULL_TREE))))
4717         return t1;
4718       break;
4719
4720     case NEGATE_EXPR:  case ABS_EXPR:
4721       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4722         return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
4723       break;
4724
4725     case MIN_EXPR:  case MAX_EXPR:
4726       /* If widening the type changes the signedness, then we can't perform
4727          this optimization as that changes the result.  */
4728       if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
4729         break;
4730
4731       /* MIN (a, b) / 5 -> MIN (a / 5, b / 5)  */
4732       if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4733           && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4734         {
4735           if (tree_int_cst_sgn (c) < 0)
4736             tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4737
4738           return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4739                                fold_convert (ctype, t2)));
4740         }
4741       break;
4742
4743     case LSHIFT_EXPR:  case RSHIFT_EXPR:
4744       /* If the second operand is constant, this is a multiplication
4745          or floor division, by a power of two, so we can treat it that
4746          way unless the multiplier or divisor overflows.  */
4747       if (TREE_CODE (op1) == INTEGER_CST
4748           /* const_binop may not detect overflow correctly,
4749              so check for it explicitly here.  */
4750           && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4751           && TREE_INT_CST_HIGH (op1) == 0
4752           && 0 != (t1 = fold_convert (ctype,
4753                                       const_binop (LSHIFT_EXPR,
4754                                                    size_one_node,
4755                                                    op1, 0)))
4756           && ! TREE_OVERFLOW (t1))
4757         return extract_muldiv (build2 (tcode == LSHIFT_EXPR
4758                                        ? MULT_EXPR : FLOOR_DIV_EXPR,
4759                                        ctype, fold_convert (ctype, op0), t1),
4760                                c, code, wide_type);
4761       break;
4762
4763     case PLUS_EXPR:  case MINUS_EXPR:
4764       /* See if we can eliminate the operation on both sides.  If we can, we
4765          can return a new PLUS or MINUS.  If we can't, the only remaining
4766          cases where we can do anything are if the second operand is a
4767          constant.  */
4768       t1 = extract_muldiv (op0, c, code, wide_type);
4769       t2 = extract_muldiv (op1, c, code, wide_type);
4770       if (t1 != 0 && t2 != 0
4771           && (code == MULT_EXPR
4772               /* If not multiplication, we can only do this if both operands
4773                  are divisible by c.  */
4774               || (multiple_of_p (ctype, op0, c)
4775                   && multiple_of_p (ctype, op1, c))))
4776         return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4777                              fold_convert (ctype, t2)));
4778
4779       /* If this was a subtraction, negate OP1 and set it to be an addition.
4780          This simplifies the logic below.  */
4781       if (tcode == MINUS_EXPR)
4782         tcode = PLUS_EXPR, op1 = negate_expr (op1);
4783
4784       if (TREE_CODE (op1) != INTEGER_CST)
4785         break;
4786
4787       /* If either OP1 or C are negative, this optimization is not safe for
4788          some of the division and remainder types while for others we need
4789          to change the code.  */
4790       if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4791         {
4792           if (code == CEIL_DIV_EXPR)
4793             code = FLOOR_DIV_EXPR;
4794           else if (code == FLOOR_DIV_EXPR)
4795             code = CEIL_DIV_EXPR;
4796           else if (code != MULT_EXPR
4797                    && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4798             break;
4799         }
4800
4801       /* If it's a multiply or a division/modulus operation of a multiple
4802          of our constant, do the operation and verify it doesn't overflow.  */
4803       if (code == MULT_EXPR
4804           || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4805         {
4806           op1 = const_binop (code, fold_convert (ctype, op1),
4807                              fold_convert (ctype, c), 0);
4808           /* We allow the constant to overflow with wrapping semantics.  */
4809           if (op1 == 0
4810               || (TREE_OVERFLOW (op1) && ! flag_wrapv))
4811             break;
4812         }
4813       else
4814         break;
4815
4816       /* If we have an unsigned type is not a sizetype, we cannot widen
4817          the operation since it will change the result if the original
4818          computation overflowed.  */
4819       if (TYPE_UNSIGNED (ctype)
4820           && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4821           && ctype != type)
4822         break;
4823
4824       /* If we were able to eliminate our operation from the first side,
4825          apply our operation to the second side and reform the PLUS.  */
4826       if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4827         return fold (build2 (tcode, ctype, fold_convert (ctype, t1), op1));
4828
4829       /* The last case is if we are a multiply.  In that case, we can
4830          apply the distributive law to commute the multiply and addition
4831          if the multiplication of the constants doesn't overflow.  */
4832       if (code == MULT_EXPR)
4833         return fold (build2 (tcode, ctype,
4834                              fold (build2 (code, ctype,
4835                                            fold_convert (ctype, op0),
4836                                            fold_convert (ctype, c))),
4837                              op1));
4838
4839       break;
4840
4841     case MULT_EXPR:
4842       /* We have a special case here if we are doing something like
4843          (C * 8) % 4 since we know that's zero.  */
4844       if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4845            || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4846           && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4847           && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4848         return omit_one_operand (type, integer_zero_node, op0);
4849
4850       /* ... fall through ...  */
4851
4852     case TRUNC_DIV_EXPR:  case CEIL_DIV_EXPR:  case FLOOR_DIV_EXPR:
4853     case ROUND_DIV_EXPR:  case EXACT_DIV_EXPR:
4854       /* If we can extract our operation from the LHS, do so and return a
4855          new operation.  Likewise for the RHS from a MULT_EXPR.  Otherwise,
4856          do something only if the second operand is a constant.  */
4857       if (same_p
4858           && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4859         return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4860                              fold_convert (ctype, op1)));
4861       else if (tcode == MULT_EXPR && code == MULT_EXPR
4862                && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4863         return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
4864                              fold_convert (ctype, t1)));
4865       else if (TREE_CODE (op1) != INTEGER_CST)
4866         return 0;
4867
4868       /* If these are the same operation types, we can associate them
4869          assuming no overflow.  */
4870       if (tcode == code
4871           && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
4872                                      fold_convert (ctype, c), 0))
4873           && ! TREE_OVERFLOW (t1))
4874         return fold (build2 (tcode, ctype, fold_convert (ctype, op0), t1));
4875
4876       /* If these operations "cancel" each other, we have the main
4877          optimizations of this pass, which occur when either constant is a
4878          multiple of the other, in which case we replace this with either an
4879          operation or CODE or TCODE.
4880
4881          If we have an unsigned type that is not a sizetype, we cannot do
4882          this since it will change the result if the original computation
4883          overflowed.  */
4884       if ((! TYPE_UNSIGNED (ctype)
4885            || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4886           && ! flag_wrapv
4887           && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4888               || (tcode == MULT_EXPR
4889                   && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4890                   && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4891         {
4892           if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4893             return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
4894                                  fold_convert (ctype,
4895                                                const_binop (TRUNC_DIV_EXPR,
4896                                                             op1, c, 0))));
4897           else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4898             return fold (build2 (code, ctype, fold_convert (ctype, op0),
4899                                  fold_convert (ctype,
4900                                                const_binop (TRUNC_DIV_EXPR,
4901                                                             c, op1, 0))));
4902         }
4903       break;
4904
4905     default:
4906       break;
4907     }
4908
4909   return 0;
4910 }
4911 \f
4912 /* Return a node which has the indicated constant VALUE (either 0 or
4913    1), and is of the indicated TYPE.  */
4914
4915 static tree
4916 constant_boolean_node (int value, tree type)
4917 {
4918   if (type == integer_type_node)
4919     return value ? integer_one_node : integer_zero_node;
4920   else if (TREE_CODE (type) == BOOLEAN_TYPE)
4921     return lang_hooks.truthvalue_conversion (value ? integer_one_node
4922                                                    : integer_zero_node);
4923   else
4924     {
4925       tree t = build_int_2 (value, 0);
4926
4927       TREE_TYPE (t) = type;
4928       return t;
4929     }
4930 }
4931
4932 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4933    Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'.  Here
4934    CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4935    expression, and ARG to `a'.  If COND_FIRST_P is nonzero, then the
4936    COND is the first argument to CODE; otherwise (as in the example
4937    given here), it is the second argument.  TYPE is the type of the
4938    original expression.  Return NULL_TREE if no simplification is
4939    possible.  */
4940
4941 static tree
4942 fold_binary_op_with_conditional_arg (enum tree_code code, tree type,
4943                                      tree cond, tree arg, int cond_first_p)
4944 {
4945   tree test, true_value, false_value;
4946   tree lhs = NULL_TREE;
4947   tree rhs = NULL_TREE;
4948
4949   /* This transformation is only worthwhile if we don't have to wrap
4950      arg in a SAVE_EXPR, and the operation can be simplified on atleast
4951      one of the branches once its pushed inside the COND_EXPR.  */
4952   if (!TREE_CONSTANT (arg))
4953     return NULL_TREE;
4954
4955   if (TREE_CODE (cond) == COND_EXPR)
4956     {
4957       test = TREE_OPERAND (cond, 0);
4958       true_value = TREE_OPERAND (cond, 1);
4959       false_value = TREE_OPERAND (cond, 2);
4960       /* If this operand throws an expression, then it does not make
4961          sense to try to perform a logical or arithmetic operation
4962          involving it.  */
4963       if (VOID_TYPE_P (TREE_TYPE (true_value)))
4964         lhs = true_value;
4965       if (VOID_TYPE_P (TREE_TYPE (false_value)))
4966         rhs = false_value;
4967     }
4968   else
4969     {
4970       tree testtype = TREE_TYPE (cond);
4971       test = cond;
4972       true_value = constant_boolean_node (true, testtype);
4973       false_value = constant_boolean_node (false, testtype);
4974     }
4975
4976   if (lhs == 0)
4977     lhs = fold (cond_first_p ? build2 (code, type, true_value, arg)
4978                              : build2 (code, type, arg, true_value));
4979   if (rhs == 0)
4980     rhs = fold (cond_first_p ? build2 (code, type, false_value, arg)
4981                              : build2 (code, type, arg, false_value));
4982
4983   test = fold (build3 (COND_EXPR, type, test, lhs, rhs));
4984   return fold_convert (type, test);
4985 }
4986
4987 \f
4988 /* Subroutine of fold() that checks for the addition of +/- 0.0.
4989
4990    If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
4991    TYPE, X + ADDEND is the same as X.  If NEGATE, return true if X -
4992    ADDEND is the same as X.
4993
4994    X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
4995    and finite.  The problematic cases are when X is zero, and its mode
4996    has signed zeros.  In the case of rounding towards -infinity,
4997    X - 0 is not the same as X because 0 - 0 is -0.  In other rounding
4998    modes, X + 0 is not the same as X because -0 + 0 is 0.  */
4999
5000 static bool
5001 fold_real_zero_addition_p (tree type, tree addend, int negate)
5002 {
5003   if (!real_zerop (addend))
5004     return false;
5005
5006   /* Don't allow the fold with -fsignaling-nans.  */
5007   if (HONOR_SNANS (TYPE_MODE (type)))
5008     return false;
5009
5010   /* Allow the fold if zeros aren't signed, or their sign isn't important.  */
5011   if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5012     return true;
5013
5014   /* Treat x + -0 as x - 0 and x - -0 as x + 0.  */
5015   if (TREE_CODE (addend) == REAL_CST
5016       && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5017     negate = !negate;
5018
5019   /* The mode has signed zeros, and we have to honor their sign.
5020      In this situation, there is only one case we can return true for.
5021      X - 0 is the same as X unless rounding towards -infinity is
5022      supported.  */
5023   return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5024 }
5025
5026 /* Subroutine of fold() that checks comparisons of built-in math
5027    functions against real constants.
5028
5029    FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5030    operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR.  TYPE
5031    is the type of the result and ARG0 and ARG1 are the operands of the
5032    comparison.  ARG1 must be a TREE_REAL_CST.
5033
5034    The function returns the constant folded tree if a simplification
5035    can be made, and NULL_TREE otherwise.  */
5036
5037 static tree
5038 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5039                      tree type, tree arg0, tree arg1)
5040 {
5041   REAL_VALUE_TYPE c;
5042
5043   if (BUILTIN_SQRT_P (fcode))
5044     {
5045       tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5046       enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5047
5048       c = TREE_REAL_CST (arg1);
5049       if (REAL_VALUE_NEGATIVE (c))
5050         {
5051           /* sqrt(x) < y is always false, if y is negative.  */
5052           if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5053             return omit_one_operand (type, integer_zero_node, arg);
5054
5055           /* sqrt(x) > y is always true, if y is negative and we
5056              don't care about NaNs, i.e. negative values of x.  */
5057           if (code == NE_EXPR || !HONOR_NANS (mode))
5058             return omit_one_operand (type, integer_one_node, arg);
5059
5060           /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
5061           return fold (build2 (GE_EXPR, type, arg,
5062                                build_real (TREE_TYPE (arg), dconst0)));
5063         }
5064       else if (code == GT_EXPR || code == GE_EXPR)
5065         {
5066           REAL_VALUE_TYPE c2;
5067
5068           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5069           real_convert (&c2, mode, &c2);
5070
5071           if (REAL_VALUE_ISINF (c2))
5072             {
5073               /* sqrt(x) > y is x == +Inf, when y is very large.  */
5074               if (HONOR_INFINITIES (mode))
5075                 return fold (build2 (EQ_EXPR, type, arg,
5076                                      build_real (TREE_TYPE (arg), c2)));
5077
5078               /* sqrt(x) > y is always false, when y is very large
5079                  and we don't care about infinities.  */
5080               return omit_one_operand (type, integer_zero_node, arg);
5081             }
5082
5083           /* sqrt(x) > c is the same as x > c*c.  */
5084           return fold (build2 (code, type, arg,
5085                                build_real (TREE_TYPE (arg), c2)));
5086         }
5087       else if (code == LT_EXPR || code == LE_EXPR)
5088         {
5089           REAL_VALUE_TYPE c2;
5090
5091           REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5092           real_convert (&c2, mode, &c2);
5093
5094           if (REAL_VALUE_ISINF (c2))
5095             {
5096               /* sqrt(x) < y is always true, when y is a very large
5097                  value and we don't care about NaNs or Infinities.  */
5098               if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5099                 return omit_one_operand (type, integer_one_node, arg);
5100
5101               /* sqrt(x) < y is x != +Inf when y is very large and we
5102                  don't care about NaNs.  */
5103               if (! HONOR_NANS (mode))
5104                 return fold (build2 (NE_EXPR, type, arg,
5105                                      build_real (TREE_TYPE (arg), c2)));
5106
5107               /* sqrt(x) < y is x >= 0 when y is very large and we
5108                  don't care about Infinities.  */
5109               if (! HONOR_INFINITIES (mode))
5110                 return fold (build2 (GE_EXPR, type, arg,
5111                                      build_real (TREE_TYPE (arg), dconst0)));
5112
5113               /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
5114               if (lang_hooks.decls.global_bindings_p () != 0
5115                   || CONTAINS_PLACEHOLDER_P (arg))
5116                 return NULL_TREE;
5117
5118               arg = save_expr (arg);
5119               return fold (build2 (TRUTH_ANDIF_EXPR, type,
5120                                    fold (build2 (GE_EXPR, type, arg,
5121                                                  build_real (TREE_TYPE (arg),
5122                                                              dconst0))),
5123                                    fold (build2 (NE_EXPR, type, arg,
5124                                                  build_real (TREE_TYPE (arg),
5125                                                              c2)))));
5126             }
5127
5128           /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
5129           if (! HONOR_NANS (mode))
5130             return fold (build2 (code, type, arg,
5131                                  build_real (TREE_TYPE (arg), c2)));
5132
5133           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
5134           if (lang_hooks.decls.global_bindings_p () == 0
5135               && ! CONTAINS_PLACEHOLDER_P (arg))
5136             {
5137               arg = save_expr (arg);
5138               return fold (build2 (TRUTH_ANDIF_EXPR, type,
5139                                    fold (build2 (GE_EXPR, type, arg,
5140                                                  build_real (TREE_TYPE (arg),
5141                                                              dconst0))),
5142                                    fold (build2 (code, type, arg,
5143                                                  build_real (TREE_TYPE (arg),
5144                                                              c2)))));
5145             }
5146         }
5147     }
5148
5149   return NULL_TREE;
5150 }
5151
5152 /* Subroutine of fold() that optimizes comparisons against Infinities,
5153    either +Inf or -Inf.
5154
5155    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5156    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
5157    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
5158
5159    The function returns the constant folded tree if a simplification
5160    can be made, and NULL_TREE otherwise.  */
5161
5162 static tree
5163 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5164 {
5165   enum machine_mode mode;
5166   REAL_VALUE_TYPE max;
5167   tree temp;
5168   bool neg;
5169
5170   mode = TYPE_MODE (TREE_TYPE (arg0));
5171
5172   /* For negative infinity swap the sense of the comparison.  */
5173   neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5174   if (neg)
5175     code = swap_tree_comparison (code);
5176
5177   switch (code)
5178     {
5179     case GT_EXPR:
5180       /* x > +Inf is always false, if with ignore sNANs.  */
5181       if (HONOR_SNANS (mode))
5182         return NULL_TREE;
5183       return omit_one_operand (type, integer_zero_node, arg0);
5184
5185     case LE_EXPR:
5186       /* x <= +Inf is always true, if we don't case about NaNs.  */
5187       if (! HONOR_NANS (mode))
5188         return omit_one_operand (type, integer_one_node, arg0);
5189
5190       /* x <= +Inf is the same as x == x, i.e. isfinite(x).  */
5191       if (lang_hooks.decls.global_bindings_p () == 0
5192           && ! CONTAINS_PLACEHOLDER_P (arg0))
5193         {
5194           arg0 = save_expr (arg0);
5195           return fold (build2 (EQ_EXPR, type, arg0, arg0));
5196         }
5197       break;
5198
5199     case EQ_EXPR:
5200     case GE_EXPR:
5201       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
5202       real_maxval (&max, neg, mode);
5203       return fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5204                            arg0, build_real (TREE_TYPE (arg0), max)));
5205
5206     case LT_EXPR:
5207       /* x < +Inf is always equal to x <= DBL_MAX.  */
5208       real_maxval (&max, neg, mode);
5209       return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5210                            arg0, build_real (TREE_TYPE (arg0), max)));
5211
5212     case NE_EXPR:
5213       /* x != +Inf is always equal to !(x > DBL_MAX).  */
5214       real_maxval (&max, neg, mode);
5215       if (! HONOR_NANS (mode))
5216         return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5217                              arg0, build_real (TREE_TYPE (arg0), max)));
5218       temp = fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5219                            arg0, build_real (TREE_TYPE (arg0), max)));
5220       return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5221
5222     default:
5223       break;
5224     }
5225
5226   return NULL_TREE;
5227 }
5228
5229 /* Subroutine of fold() that optimizes comparisons of a division by
5230    a nonzero integer constant against an integer constant, i.e.
5231    X/C1 op C2.
5232
5233    CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5234    GE_EXPR or LE_EXPR.  TYPE is the type of the result and ARG0 and ARG1
5235    are the operands of the comparison.  ARG1 must be a TREE_REAL_CST.
5236
5237    The function returns the constant folded tree if a simplification
5238    can be made, and NULL_TREE otherwise.  */
5239
5240 static tree
5241 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5242 {
5243   tree prod, tmp, hi, lo;
5244   tree arg00 = TREE_OPERAND (arg0, 0);
5245   tree arg01 = TREE_OPERAND (arg0, 1);
5246   unsigned HOST_WIDE_INT lpart;
5247   HOST_WIDE_INT hpart;
5248   int overflow;
5249
5250   /* We have to do this the hard way to detect unsigned overflow.
5251      prod = int_const_binop (MULT_EXPR, arg01, arg1, 0);  */
5252   overflow = mul_double (TREE_INT_CST_LOW (arg01),
5253                          TREE_INT_CST_HIGH (arg01),
5254                          TREE_INT_CST_LOW (arg1),
5255                          TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5256   prod = build_int_2 (lpart, hpart);
5257   TREE_TYPE (prod) = TREE_TYPE (arg00);
5258   TREE_OVERFLOW (prod) = force_fit_type (prod, overflow)
5259                          || TREE_INT_CST_HIGH (prod) != hpart
5260                          || TREE_INT_CST_LOW (prod) != lpart;
5261   TREE_CONSTANT_OVERFLOW (prod) = TREE_OVERFLOW (prod);
5262
5263   if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5264     {
5265       tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5266       lo = prod;
5267
5268       /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0).  */
5269       overflow = add_double (TREE_INT_CST_LOW (prod),
5270                              TREE_INT_CST_HIGH (prod),
5271                              TREE_INT_CST_LOW (tmp),
5272                              TREE_INT_CST_HIGH (tmp),
5273                              &lpart, &hpart);
5274       hi = build_int_2 (lpart, hpart);
5275       TREE_TYPE (hi) = TREE_TYPE (arg00);
5276       TREE_OVERFLOW (hi) = force_fit_type (hi, overflow)
5277                            || TREE_INT_CST_HIGH (hi) != hpart
5278                            || TREE_INT_CST_LOW (hi) != lpart
5279                            || TREE_OVERFLOW (prod);
5280       TREE_CONSTANT_OVERFLOW (hi) = TREE_OVERFLOW (hi);
5281     }
5282   else if (tree_int_cst_sgn (arg01) >= 0)
5283     {
5284       tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5285       switch (tree_int_cst_sgn (arg1))
5286         {
5287         case -1:
5288           lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5289           hi = prod;
5290           break;
5291
5292         case  0:
5293           lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5294           hi = tmp;
5295           break;
5296
5297         case  1:
5298           hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5299           lo = prod;
5300           break;
5301
5302         default:
5303           abort ();
5304         }
5305     }
5306   else
5307     {
5308       tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5309       switch (tree_int_cst_sgn (arg1))
5310         {
5311         case -1:
5312           hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5313           lo = prod;
5314           break;
5315
5316         case  0:
5317           hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5318           lo = tmp;
5319           break;
5320
5321         case  1:
5322           lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5323           hi = prod;
5324           break;
5325
5326         default:
5327           abort ();
5328         }
5329     }
5330
5331   switch (code)
5332     {
5333     case EQ_EXPR:
5334       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5335         return omit_one_operand (type, integer_zero_node, arg00);
5336       if (TREE_OVERFLOW (hi))
5337         return fold (build2 (GE_EXPR, type, arg00, lo));
5338       if (TREE_OVERFLOW (lo))
5339         return fold (build2 (LE_EXPR, type, arg00, hi));
5340       return build_range_check (type, arg00, 1, lo, hi);
5341
5342     case NE_EXPR:
5343       if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5344         return omit_one_operand (type, integer_one_node, arg00);
5345       if (TREE_OVERFLOW (hi))
5346         return fold (build2 (LT_EXPR, type, arg00, lo));
5347       if (TREE_OVERFLOW (lo))
5348         return fold (build2 (GT_EXPR, type, arg00, hi));
5349       return build_range_check (type, arg00, 0, lo, hi);
5350
5351     case LT_EXPR:
5352       if (TREE_OVERFLOW (lo))
5353         return omit_one_operand (type, integer_zero_node, arg00);
5354       return fold (build2 (LT_EXPR, type, arg00, lo));
5355
5356     case LE_EXPR:
5357       if (TREE_OVERFLOW (hi))
5358         return omit_one_operand (type, integer_one_node, arg00);
5359       return fold (build2 (LE_EXPR, type, arg00, hi));
5360
5361     case GT_EXPR:
5362       if (TREE_OVERFLOW (hi))
5363         return omit_one_operand (type, integer_zero_node, arg00);
5364       return fold (build2 (GT_EXPR, type, arg00, hi));
5365
5366     case GE_EXPR:
5367       if (TREE_OVERFLOW (lo))
5368         return omit_one_operand (type, integer_one_node, arg00);
5369       return fold (build2 (GE_EXPR, type, arg00, lo));
5370
5371     default:
5372       break;
5373     }
5374
5375   return NULL_TREE;
5376 }
5377
5378
5379 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5380    equality/inequality test, then return a simplified form of
5381    the test using shifts and logical operations.  Otherwise return
5382    NULL.  TYPE is the desired result type.  */
5383  
5384 tree
5385 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5386                       tree result_type)
5387 {
5388   /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5389      operand 0.  */
5390   if (code == TRUTH_NOT_EXPR)
5391     {
5392       code = TREE_CODE (arg0);
5393       if (code != NE_EXPR && code != EQ_EXPR)
5394         return NULL_TREE;
5395
5396       /* Extract the arguments of the EQ/NE.  */
5397       arg1 = TREE_OPERAND (arg0, 1);
5398       arg0 = TREE_OPERAND (arg0, 0);
5399
5400       /* This requires us to invert the code.  */ 
5401       code = (code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
5402     }
5403
5404   /* If this is testing a single bit, we can optimize the test.  */
5405   if ((code == NE_EXPR || code == EQ_EXPR)
5406       && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5407       && integer_pow2p (TREE_OPERAND (arg0, 1)))
5408     {
5409       tree inner = TREE_OPERAND (arg0, 0);
5410       tree type = TREE_TYPE (arg0);
5411       int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5412       enum machine_mode operand_mode = TYPE_MODE (type);
5413       int ops_unsigned;
5414       tree signed_type, unsigned_type, intermediate_type;
5415       tree arg00;
5416   
5417       /* If we have (A & C) != 0 where C is the sign bit of A, convert
5418          this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
5419       arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5420       if (arg00 != NULL_TREE
5421           /* This is only a win if casting to a signed type is cheap,
5422              i.e. when arg00's type is not a partial mode.  */
5423           && TYPE_PRECISION (TREE_TYPE (arg00))
5424              == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
5425         {
5426           tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5427           return fold (build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
5428                                result_type, fold_convert (stype, arg00),
5429                                fold_convert (stype, integer_zero_node)));
5430         }
5431
5432       /* Otherwise we have (A & C) != 0 where C is a single bit, 
5433          convert that into ((A >> C2) & 1).  Where C2 = log2(C).
5434          Similarly for (A & C) == 0.  */
5435
5436       /* If INNER is a right shift of a constant and it plus BITNUM does
5437          not overflow, adjust BITNUM and INNER.  */
5438       if (TREE_CODE (inner) == RSHIFT_EXPR
5439           && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5440           && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5441           && bitnum < TYPE_PRECISION (type)
5442           && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5443                                    bitnum - TYPE_PRECISION (type)))
5444         {
5445           bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5446           inner = TREE_OPERAND (inner, 0);
5447         }
5448
5449       /* If we are going to be able to omit the AND below, we must do our
5450          operations as unsigned.  If we must use the AND, we have a choice.
5451          Normally unsigned is faster, but for some machines signed is.  */
5452 #ifdef LOAD_EXTEND_OP
5453       ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND ? 0 : 1);
5454 #else
5455       ops_unsigned = 1;
5456 #endif
5457
5458       signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5459       unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5460       intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5461       inner = fold_convert (intermediate_type, inner);
5462
5463       if (bitnum != 0)
5464         inner = build2 (RSHIFT_EXPR, intermediate_type,
5465                         inner, size_int (bitnum));
5466
5467       if (code == EQ_EXPR)
5468         inner = build2 (BIT_XOR_EXPR, intermediate_type,
5469                         inner, integer_one_node);
5470
5471       /* Put the AND last so it can combine with more things.  */
5472       inner = build2 (BIT_AND_EXPR, intermediate_type,
5473                       inner, integer_one_node);
5474
5475       /* Make sure to return the proper type.  */
5476       inner = fold_convert (result_type, inner);
5477
5478       return inner;
5479     }
5480   return NULL_TREE;
5481 }
5482
5483 /* Check whether we are allowed to reorder operands arg0 and arg1,
5484    such that the evaluation of arg1 occurs before arg0.  */
5485
5486 static bool
5487 reorder_operands_p (tree arg0, tree arg1)
5488 {
5489   if (! flag_evaluation_order)
5490     return true;
5491   if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
5492     return true;
5493   return ! TREE_SIDE_EFFECTS (arg0)
5494          && ! TREE_SIDE_EFFECTS (arg1);
5495 }
5496
5497 /* Test whether it is preferable two swap two operands, ARG0 and
5498    ARG1, for example because ARG0 is an integer constant and ARG1
5499    isn't.  If REORDER is true, only recommend swapping if we can
5500    evaluate the operands in reverse order.  */
5501
5502 static bool
5503 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
5504 {
5505   STRIP_SIGN_NOPS (arg0);
5506   STRIP_SIGN_NOPS (arg1);
5507
5508   if (TREE_CODE (arg1) == INTEGER_CST)
5509     return 0;
5510   if (TREE_CODE (arg0) == INTEGER_CST)
5511     return 1;
5512
5513   if (TREE_CODE (arg1) == REAL_CST)
5514     return 0;
5515   if (TREE_CODE (arg0) == REAL_CST)
5516     return 1;
5517
5518   if (TREE_CODE (arg1) == COMPLEX_CST)
5519     return 0;
5520   if (TREE_CODE (arg0) == COMPLEX_CST)
5521     return 1;
5522
5523   if (TREE_CONSTANT (arg1))
5524     return 0;
5525   if (TREE_CONSTANT (arg0))
5526     return 1;
5527     
5528   if (optimize_size)
5529     return 0;
5530
5531   if (reorder && flag_evaluation_order
5532       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5533     return 0;
5534
5535   if (DECL_P (arg1))
5536     return 0;
5537   if (DECL_P (arg0))
5538     return 1;
5539
5540   if (reorder && flag_evaluation_order
5541       && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5542     return 0;
5543
5544   if (DECL_P (arg1))
5545     return 0;
5546   if (DECL_P (arg0))
5547     return 1;
5548
5549   return 0;
5550 }
5551
5552 /* Perform constant folding and related simplification of EXPR.
5553    The related simplifications include x*1 => x, x*0 => 0, etc.,
5554    and application of the associative law.
5555    NOP_EXPR conversions may be removed freely (as long as we
5556    are careful not to change the type of the overall expression).
5557    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
5558    but we can constant-fold them if they have constant operands.  */
5559
5560 #ifdef ENABLE_FOLD_CHECKING
5561 # define fold(x) fold_1 (x)
5562 static tree fold_1 (tree);
5563 static
5564 #endif
5565 tree
5566 fold (tree expr)
5567 {
5568   const tree t = expr;
5569   const tree type = TREE_TYPE (expr);
5570   tree t1 = NULL_TREE;
5571   tree tem;
5572   tree arg0 = NULL_TREE, arg1 = NULL_TREE;
5573   enum tree_code code = TREE_CODE (t);
5574   int kind = TREE_CODE_CLASS (code);
5575
5576   /* WINS will be nonzero when the switch is done
5577      if all operands are constant.  */
5578   int wins = 1;
5579
5580   /* Don't try to process an RTL_EXPR since its operands aren't trees.
5581      Likewise for a SAVE_EXPR that's already been evaluated.  */
5582   if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
5583     return t;
5584
5585   /* Return right away if a constant.  */
5586   if (kind == 'c')
5587     return t;
5588
5589   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
5590     {
5591       tree subop;
5592
5593       /* Special case for conversion ops that can have fixed point args.  */
5594       arg0 = TREE_OPERAND (t, 0);
5595
5596       /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
5597       if (arg0 != 0)
5598         STRIP_SIGN_NOPS (arg0);
5599
5600       if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5601         subop = TREE_REALPART (arg0);
5602       else
5603         subop = arg0;
5604
5605       if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5606           && TREE_CODE (subop) != REAL_CST)
5607         /* Note that TREE_CONSTANT isn't enough:
5608            static var addresses are constant but we can't
5609            do arithmetic on them.  */
5610         wins = 0;
5611     }
5612   else if (IS_EXPR_CODE_CLASS (kind))
5613     {
5614       int len = first_rtl_op (code);
5615       int i;
5616       for (i = 0; i < len; i++)
5617         {
5618           tree op = TREE_OPERAND (t, i);
5619           tree subop;
5620
5621           if (op == 0)
5622             continue;           /* Valid for CALL_EXPR, at least.  */
5623
5624           /* Strip any conversions that don't change the mode.  This is
5625              safe for every expression, except for a comparison expression
5626              because its signedness is derived from its operands.  So, in
5627              the latter case, only strip conversions that don't change the
5628              signedness.
5629
5630              Note that this is done as an internal manipulation within the
5631              constant folder, in order to find the simplest representation
5632              of the arguments so that their form can be studied.  In any
5633              cases, the appropriate type conversions should be put back in
5634              the tree that will get out of the constant folder.  */
5635           if (kind == '<')
5636             STRIP_SIGN_NOPS (op);
5637           else
5638             STRIP_NOPS (op);
5639
5640           if (TREE_CODE (op) == COMPLEX_CST)
5641             subop = TREE_REALPART (op);
5642           else
5643             subop = op;
5644
5645           if (TREE_CODE (subop) != INTEGER_CST
5646               && TREE_CODE (subop) != REAL_CST)
5647             /* Note that TREE_CONSTANT isn't enough:
5648                static var addresses are constant but we can't
5649                do arithmetic on them.  */
5650             wins = 0;
5651
5652           if (i == 0)
5653             arg0 = op;
5654           else if (i == 1)
5655             arg1 = op;
5656         }
5657     }
5658
5659   /* If this is a commutative operation, and ARG0 is a constant, move it
5660      to ARG1 to reduce the number of tests below.  */
5661   if (commutative_tree_code (code)
5662       && tree_swap_operands_p (arg0, arg1, true))
5663     return fold (build2 (code, type, TREE_OPERAND (t, 1),
5664                          TREE_OPERAND (t, 0)));
5665
5666   /* Now WINS is set as described above,
5667      ARG0 is the first operand of EXPR,
5668      and ARG1 is the second operand (if it has more than one operand).
5669
5670      First check for cases where an arithmetic operation is applied to a
5671      compound, conditional, or comparison operation.  Push the arithmetic
5672      operation inside the compound or conditional to see if any folding
5673      can then be done.  Convert comparison to conditional for this purpose.
5674      The also optimizes non-constant cases that used to be done in
5675      expand_expr.
5676
5677      Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5678      one of the operands is a comparison and the other is a comparison, a
5679      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
5680      code below would make the expression more complex.  Change it to a
5681      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to
5682      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
5683
5684   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5685        || code == EQ_EXPR || code == NE_EXPR)
5686       && ((truth_value_p (TREE_CODE (arg0))
5687            && (truth_value_p (TREE_CODE (arg1))
5688                || (TREE_CODE (arg1) == BIT_AND_EXPR
5689                    && integer_onep (TREE_OPERAND (arg1, 1)))))
5690           || (truth_value_p (TREE_CODE (arg1))
5691               && (truth_value_p (TREE_CODE (arg0))
5692                   || (TREE_CODE (arg0) == BIT_AND_EXPR
5693                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
5694     {
5695       tem = fold (build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5696                           : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5697                           : TRUTH_XOR_EXPR,
5698                           type, fold_convert (boolean_type_node, arg0),
5699                           fold_convert (boolean_type_node, arg1)));
5700
5701       if (code == EQ_EXPR)
5702         tem = invert_truthvalue (tem);
5703
5704       return tem;
5705     }
5706
5707   if (TREE_CODE_CLASS (code) == '1')
5708     {
5709       if (TREE_CODE (arg0) == COMPOUND_EXPR)
5710         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5711                        fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5712       else if (TREE_CODE (arg0) == COND_EXPR)
5713         {
5714           tree arg01 = TREE_OPERAND (arg0, 1);
5715           tree arg02 = TREE_OPERAND (arg0, 2);
5716           if (! VOID_TYPE_P (TREE_TYPE (arg01)))
5717             arg01 = fold (build1 (code, type, arg01));
5718           if (! VOID_TYPE_P (TREE_TYPE (arg02)))
5719             arg02 = fold (build1 (code, type, arg02));
5720           tem = fold (build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5721                               arg01, arg02));
5722
5723           /* If this was a conversion, and all we did was to move into
5724              inside the COND_EXPR, bring it back out.  But leave it if
5725              it is a conversion from integer to integer and the
5726              result precision is no wider than a word since such a
5727              conversion is cheap and may be optimized away by combine,
5728              while it couldn't if it were outside the COND_EXPR.  Then return
5729              so we don't get into an infinite recursion loop taking the
5730              conversion out and then back in.  */
5731
5732           if ((code == NOP_EXPR || code == CONVERT_EXPR
5733                || code == NON_LVALUE_EXPR)
5734               && TREE_CODE (tem) == COND_EXPR
5735               && TREE_CODE (TREE_OPERAND (tem, 1)) == code
5736               && TREE_CODE (TREE_OPERAND (tem, 2)) == code
5737               && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
5738               && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
5739               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
5740                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
5741               && ! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
5742                     && (INTEGRAL_TYPE_P
5743                         (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
5744                     && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD))
5745             tem = build1 (code, type,
5746                           build3 (COND_EXPR,
5747                                   TREE_TYPE (TREE_OPERAND
5748                                              (TREE_OPERAND (tem, 1), 0)),
5749                                   TREE_OPERAND (tem, 0),
5750                                   TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
5751                                   TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
5752           return tem;
5753         }
5754       else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5755         {
5756           if (TREE_CODE (type) == BOOLEAN_TYPE)
5757             {
5758               arg0 = copy_node (arg0);
5759               TREE_TYPE (arg0) = type;
5760               return arg0;
5761             }
5762           else if (TREE_CODE (type) != INTEGER_TYPE)
5763             return fold (build3 (COND_EXPR, type, arg0,
5764                                  fold (build1 (code, type,
5765                                                integer_one_node)),
5766                                  fold (build1 (code, type,
5767                                                integer_zero_node))));
5768         }
5769    }
5770   else if (TREE_CODE_CLASS (code) == '<'
5771            && TREE_CODE (arg0) == COMPOUND_EXPR)
5772     return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5773                    fold (build2 (code, type, TREE_OPERAND (arg0, 1), arg1)));
5774   else if (TREE_CODE_CLASS (code) == '<'
5775            && TREE_CODE (arg1) == COMPOUND_EXPR)
5776     return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5777                    fold (build2 (code, type, arg0, TREE_OPERAND (arg1, 1))));
5778   else if (TREE_CODE_CLASS (code) == '2'
5779            || TREE_CODE_CLASS (code) == '<')
5780     {
5781       if (TREE_CODE (arg0) == COMPOUND_EXPR)
5782         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5783                        fold (build2 (code, type, TREE_OPERAND (arg0, 1),
5784                                      arg1)));
5785       if (TREE_CODE (arg1) == COMPOUND_EXPR
5786           && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
5787         return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5788                        fold (build2 (code, type,
5789                                      arg0, TREE_OPERAND (arg1, 1))));
5790
5791       if (TREE_CODE (arg0) == COND_EXPR
5792           || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5793         {
5794           tem = fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5795                                                      /*cond_first_p=*/1);
5796           if (tem != NULL_TREE)
5797             return tem;
5798         }
5799
5800       if (TREE_CODE (arg1) == COND_EXPR
5801           || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
5802         {
5803           tem = fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5804                                                      /*cond_first_p=*/0);
5805           if (tem != NULL_TREE)
5806             return tem;
5807         }
5808     }
5809
5810   switch (code)
5811     {
5812     case CONST_DECL:
5813       return fold (DECL_INITIAL (t));
5814
5815     case NOP_EXPR:
5816     case FLOAT_EXPR:
5817     case CONVERT_EXPR:
5818     case FIX_TRUNC_EXPR:
5819     case FIX_CEIL_EXPR:
5820     case FIX_FLOOR_EXPR:
5821     case FIX_ROUND_EXPR:
5822       if (TREE_TYPE (TREE_OPERAND (t, 0)) == type)
5823         return TREE_OPERAND (t, 0);
5824
5825       /* Handle cases of two conversions in a row.  */
5826       if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5827           || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5828         {
5829           tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5830           tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5831           int inside_int = INTEGRAL_TYPE_P (inside_type);
5832           int inside_ptr = POINTER_TYPE_P (inside_type);
5833           int inside_float = FLOAT_TYPE_P (inside_type);
5834           unsigned int inside_prec = TYPE_PRECISION (inside_type);
5835           int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5836           int inter_int = INTEGRAL_TYPE_P (inter_type);
5837           int inter_ptr = POINTER_TYPE_P (inter_type);
5838           int inter_float = FLOAT_TYPE_P (inter_type);
5839           unsigned int inter_prec = TYPE_PRECISION (inter_type);
5840           int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5841           int final_int = INTEGRAL_TYPE_P (type);
5842           int final_ptr = POINTER_TYPE_P (type);
5843           int final_float = FLOAT_TYPE_P (type);
5844           unsigned int final_prec = TYPE_PRECISION (type);
5845           int final_unsignedp = TYPE_UNSIGNED (type);
5846
5847           /* In addition to the cases of two conversions in a row
5848              handled below, if we are converting something to its own
5849              type via an object of identical or wider precision, neither
5850              conversion is needed.  */
5851           if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
5852               && ((inter_int && final_int) || (inter_float && final_float))
5853               && inter_prec >= final_prec)
5854             return fold (build1 (code, type,
5855                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5856
5857           /* Likewise, if the intermediate and final types are either both
5858              float or both integer, we don't need the middle conversion if
5859              it is wider than the final type and doesn't change the signedness
5860              (for integers).  Avoid this if the final type is a pointer
5861              since then we sometimes need the inner conversion.  Likewise if
5862              the outer has a precision not equal to the size of its mode.  */
5863           if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5864                || (inter_float && inside_float))
5865               && inter_prec >= inside_prec
5866               && (inter_float || inter_unsignedp == inside_unsignedp)
5867               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5868                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
5869               && ! final_ptr)
5870             return fold (build1 (code, type,
5871                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5872
5873           /* If we have a sign-extension of a zero-extended value, we can
5874              replace that by a single zero-extension.  */
5875           if (inside_int && inter_int && final_int
5876               && inside_prec < inter_prec && inter_prec < final_prec
5877               && inside_unsignedp && !inter_unsignedp)
5878             return fold (build1 (code, type,
5879                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5880
5881           /* Two conversions in a row are not needed unless:
5882              - some conversion is floating-point (overstrict for now), or
5883              - the intermediate type is narrower than both initial and
5884                final, or
5885              - the intermediate type and innermost type differ in signedness,
5886                and the outermost type is wider than the intermediate, or
5887              - the initial type is a pointer type and the precisions of the
5888                intermediate and final types differ, or
5889              - the final type is a pointer type and the precisions of the
5890                initial and intermediate types differ.  */
5891           if (! inside_float && ! inter_float && ! final_float
5892               && (inter_prec > inside_prec || inter_prec > final_prec)
5893               && ! (inside_int && inter_int
5894                     && inter_unsignedp != inside_unsignedp
5895                     && inter_prec < final_prec)
5896               && ((inter_unsignedp && inter_prec > inside_prec)
5897                   == (final_unsignedp && final_prec > inter_prec))
5898               && ! (inside_ptr && inter_prec != final_prec)
5899               && ! (final_ptr && inside_prec != inter_prec)
5900               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5901                     && TYPE_MODE (type) == TYPE_MODE (inter_type))
5902               && ! final_ptr)
5903             return fold (build1 (code, type,
5904                                  TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5905         }
5906
5907       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5908           && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5909           /* Detect assigning a bitfield.  */
5910           && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5911                && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5912         {
5913           /* Don't leave an assignment inside a conversion
5914              unless assigning a bitfield.  */
5915           tree prev = TREE_OPERAND (t, 0);
5916           tem = copy_node (t);
5917           TREE_OPERAND (tem, 0) = TREE_OPERAND (prev, 1);
5918           /* First do the assignment, then return converted constant.  */
5919           tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), prev, fold (tem));
5920           TREE_NO_WARNING (tem) = 1;
5921           TREE_USED (tem) = 1;
5922           return tem;
5923         }
5924
5925       /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5926          constants (if x has signed type, the sign bit cannot be set
5927          in c).  This folds extension into the BIT_AND_EXPR.  */
5928       if (INTEGRAL_TYPE_P (type)
5929           && TREE_CODE (type) != BOOLEAN_TYPE
5930           && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
5931           && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
5932         {
5933           tree and = TREE_OPERAND (t, 0);
5934           tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
5935           int change = 0;
5936
5937           if (TYPE_UNSIGNED (TREE_TYPE (and))
5938               || (TYPE_PRECISION (type)
5939                   <= TYPE_PRECISION (TREE_TYPE (and))))
5940             change = 1;
5941           else if (TYPE_PRECISION (TREE_TYPE (and1))
5942                    <= HOST_BITS_PER_WIDE_INT
5943                    && host_integerp (and1, 1))
5944             {
5945               unsigned HOST_WIDE_INT cst;
5946
5947               cst = tree_low_cst (and1, 1);
5948               cst &= (HOST_WIDE_INT) -1
5949                      << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
5950               change = (cst == 0);
5951 #ifdef LOAD_EXTEND_OP
5952               if (change
5953                   && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
5954                       == ZERO_EXTEND))
5955                 {
5956                   tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
5957                   and0 = fold_convert (uns, and0);
5958                   and1 = fold_convert (uns, and1);
5959                 }
5960 #endif
5961             }
5962           if (change)
5963             return fold (build2 (BIT_AND_EXPR, type,
5964                                  fold_convert (type, and0),
5965                                  fold_convert (type, and1)));
5966         }
5967
5968       /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
5969          T2 being pointers to types of the same size.  */
5970       if (POINTER_TYPE_P (TREE_TYPE (t))
5971           && TREE_CODE_CLASS (TREE_CODE (arg0)) == '2'
5972           && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
5973           && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5974         {
5975           tree arg00 = TREE_OPERAND (arg0, 0);
5976           tree t0 = TREE_TYPE (t);
5977           tree t1 = TREE_TYPE (arg00);
5978           tree tt0 = TREE_TYPE (t0);
5979           tree tt1 = TREE_TYPE (t1);
5980           tree s0 = TYPE_SIZE (tt0);
5981           tree s1 = TYPE_SIZE (tt1);
5982
5983           if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
5984             return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
5985                            TREE_OPERAND (arg0, 1));
5986         }
5987
5988       tem = fold_convert_const (code, type, arg0);
5989       return tem ? tem : t;
5990
5991     case VIEW_CONVERT_EXPR:
5992       if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5993         return build1 (VIEW_CONVERT_EXPR, type,
5994                        TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5995       return t;
5996
5997     case COMPONENT_REF:
5998       if (TREE_CODE (arg0) == CONSTRUCTOR
5999           && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
6000         {
6001           tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
6002           if (m)
6003             return TREE_VALUE (m);
6004         }
6005       return t;
6006
6007     case RANGE_EXPR:
6008       if (TREE_CONSTANT (t) != wins)
6009         {
6010           tem = copy_node (t);
6011           TREE_CONSTANT (tem) = wins;
6012           TREE_INVARIANT (tem) = wins;
6013           return tem;
6014         }
6015       return t;
6016
6017     case NEGATE_EXPR:
6018       if (negate_expr_p (arg0))
6019         return fold_convert (type, negate_expr (arg0));
6020       return t;
6021
6022     case ABS_EXPR:
6023       if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
6024         return fold_abs_const (arg0, type);
6025       else if (TREE_CODE (arg0) == NEGATE_EXPR)
6026         return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6027       /* Convert fabs((double)float) into (double)fabsf(float).  */
6028       else if (TREE_CODE (arg0) == NOP_EXPR
6029                && TREE_CODE (type) == REAL_TYPE)
6030         {
6031           tree targ0 = strip_float_extensions (arg0);
6032           if (targ0 != arg0)
6033             return fold_convert (type, fold (build1 (ABS_EXPR,
6034                                                      TREE_TYPE (targ0),
6035                                                      targ0)));
6036         }
6037       else if (tree_expr_nonnegative_p (arg0))
6038         return arg0;
6039       return t;
6040
6041     case CONJ_EXPR:
6042       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6043         return fold_convert (type, arg0);
6044       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6045         return build2 (COMPLEX_EXPR, type,
6046                        TREE_OPERAND (arg0, 0),
6047                        negate_expr (TREE_OPERAND (arg0, 1)));
6048       else if (TREE_CODE (arg0) == COMPLEX_CST)
6049         return build_complex (type, TREE_REALPART (arg0),
6050                               negate_expr (TREE_IMAGPART (arg0)));
6051       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6052         return fold (build2 (TREE_CODE (arg0), type,
6053                              fold (build1 (CONJ_EXPR, type,
6054                                            TREE_OPERAND (arg0, 0))),
6055                              fold (build1 (CONJ_EXPR, type,
6056                                            TREE_OPERAND (arg0, 1)))));
6057       else if (TREE_CODE (arg0) == CONJ_EXPR)
6058         return TREE_OPERAND (arg0, 0);
6059       return t;
6060
6061     case BIT_NOT_EXPR:
6062       if (TREE_CODE (arg0) == INTEGER_CST)
6063         return fold_not_const (arg0, type);
6064       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6065         return TREE_OPERAND (arg0, 0);
6066       return t;
6067
6068     case PLUS_EXPR:
6069       /* A + (-B) -> A - B */
6070       if (TREE_CODE (arg1) == NEGATE_EXPR)
6071         return fold (build2 (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6072       /* (-A) + B -> B - A */
6073       if (TREE_CODE (arg0) == NEGATE_EXPR
6074           && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
6075         return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
6076       if (! FLOAT_TYPE_P (type))
6077         {
6078           if (integer_zerop (arg1))
6079             return non_lvalue (fold_convert (type, arg0));
6080
6081           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
6082              with a constant, and the two constants have no bits in common,
6083              we should treat this as a BIT_IOR_EXPR since this may produce more
6084              simplifications.  */
6085           if (TREE_CODE (arg0) == BIT_AND_EXPR
6086               && TREE_CODE (arg1) == BIT_AND_EXPR
6087               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6088               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6089               && integer_zerop (const_binop (BIT_AND_EXPR,
6090                                              TREE_OPERAND (arg0, 1),
6091                                              TREE_OPERAND (arg1, 1), 0)))
6092             {
6093               code = BIT_IOR_EXPR;
6094               goto bit_ior;
6095             }
6096
6097           /* Reassociate (plus (plus (mult) (foo)) (mult)) as
6098              (plus (plus (mult) (mult)) (foo)) so that we can
6099              take advantage of the factoring cases below.  */
6100           if ((TREE_CODE (arg0) == PLUS_EXPR
6101                && TREE_CODE (arg1) == MULT_EXPR)
6102               || (TREE_CODE (arg1) == PLUS_EXPR
6103                   && TREE_CODE (arg0) == MULT_EXPR))
6104             {
6105               tree parg0, parg1, parg, marg;
6106
6107               if (TREE_CODE (arg0) == PLUS_EXPR)
6108                 parg = arg0, marg = arg1;
6109               else
6110                 parg = arg1, marg = arg0;
6111               parg0 = TREE_OPERAND (parg, 0);
6112               parg1 = TREE_OPERAND (parg, 1);
6113               STRIP_NOPS (parg0);
6114               STRIP_NOPS (parg1);
6115
6116               if (TREE_CODE (parg0) == MULT_EXPR
6117                   && TREE_CODE (parg1) != MULT_EXPR)
6118                 return fold (build2 (PLUS_EXPR, type,
6119                                      fold (build2 (PLUS_EXPR, type,
6120                                                    fold_convert (type, parg0),
6121                                                    fold_convert (type, marg))),
6122                                      fold_convert (type, parg1)));
6123               if (TREE_CODE (parg0) != MULT_EXPR
6124                   && TREE_CODE (parg1) == MULT_EXPR)
6125                 return fold (build2 (PLUS_EXPR, type,
6126                                      fold (build2 (PLUS_EXPR, type,
6127                                                    fold_convert (type, parg1),
6128                                                    fold_convert (type, marg))),
6129                                      fold_convert (type, parg0)));
6130             }
6131
6132           if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
6133             {
6134               tree arg00, arg01, arg10, arg11;
6135               tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6136
6137               /* (A * C) + (B * C) -> (A+B) * C.
6138                  We are most concerned about the case where C is a constant,
6139                  but other combinations show up during loop reduction.  Since
6140                  it is not difficult, try all four possibilities.  */
6141
6142               arg00 = TREE_OPERAND (arg0, 0);
6143               arg01 = TREE_OPERAND (arg0, 1);
6144               arg10 = TREE_OPERAND (arg1, 0);
6145               arg11 = TREE_OPERAND (arg1, 1);
6146               same = NULL_TREE;
6147
6148               if (operand_equal_p (arg01, arg11, 0))
6149                 same = arg01, alt0 = arg00, alt1 = arg10;
6150               else if (operand_equal_p (arg00, arg10, 0))
6151                 same = arg00, alt0 = arg01, alt1 = arg11;
6152               else if (operand_equal_p (arg00, arg11, 0))
6153                 same = arg00, alt0 = arg01, alt1 = arg10;
6154               else if (operand_equal_p (arg01, arg10, 0))
6155                 same = arg01, alt0 = arg00, alt1 = arg11;
6156
6157               /* No identical multiplicands; see if we can find a common
6158                  power-of-two factor in non-power-of-two multiplies.  This
6159                  can help in multi-dimensional array access.  */
6160               else if (TREE_CODE (arg01) == INTEGER_CST
6161                        && TREE_CODE (arg11) == INTEGER_CST
6162                        && TREE_INT_CST_HIGH (arg01) == 0
6163                        && TREE_INT_CST_HIGH (arg11) == 0)
6164                 {
6165                   HOST_WIDE_INT int01, int11, tmp;
6166                   int01 = TREE_INT_CST_LOW (arg01);
6167                   int11 = TREE_INT_CST_LOW (arg11);
6168
6169                   /* Move min of absolute values to int11.  */
6170                   if ((int01 >= 0 ? int01 : -int01)
6171                       < (int11 >= 0 ? int11 : -int11))
6172                     {
6173                       tmp = int01, int01 = int11, int11 = tmp;
6174                       alt0 = arg00, arg00 = arg10, arg10 = alt0;
6175                       alt0 = arg01, arg01 = arg11, arg11 = alt0;
6176                     }
6177
6178                   if (exact_log2 (int11) > 0 && int01 % int11 == 0)
6179                     {
6180                       alt0 = fold (build2 (MULT_EXPR, type, arg00,
6181                                            build_int_2 (int01 / int11, 0)));
6182                       alt1 = arg10;
6183                       same = arg11;
6184                     }
6185                 }
6186
6187               if (same)
6188                 return fold (build2 (MULT_EXPR, type,
6189                                      fold (build2 (PLUS_EXPR, type,
6190                                                    alt0, alt1)),
6191                                      same));
6192             }
6193         }
6194       else
6195         {
6196           /* See if ARG1 is zero and X + ARG1 reduces to X.  */
6197           if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
6198             return non_lvalue (fold_convert (type, arg0));
6199
6200           /* Likewise if the operands are reversed.  */
6201           if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6202             return non_lvalue (fold_convert (type, arg1));
6203
6204           /* Convert x+x into x*2.0.  */
6205           if (operand_equal_p (arg0, arg1, 0)
6206               && SCALAR_FLOAT_TYPE_P (type))
6207             return fold (build2 (MULT_EXPR, type, arg0,
6208                                  build_real (type, dconst2)));
6209
6210           /* Convert x*c+x into x*(c+1).  */
6211           if (flag_unsafe_math_optimizations
6212               && TREE_CODE (arg0) == MULT_EXPR
6213               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6214               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6215               && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
6216             {
6217               REAL_VALUE_TYPE c;
6218
6219               c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6220               real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6221               return fold (build2 (MULT_EXPR, type, arg1,
6222                                    build_real (type, c)));
6223             }
6224
6225           /* Convert x+x*c into x*(c+1).  */
6226           if (flag_unsafe_math_optimizations
6227               && TREE_CODE (arg1) == MULT_EXPR
6228               && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6229               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6230               && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
6231             {
6232               REAL_VALUE_TYPE c;
6233
6234               c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6235               real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6236               return fold (build2 (MULT_EXPR, type, arg0,
6237                                    build_real (type, c)));
6238             }
6239
6240           /* Convert x*c1+x*c2 into x*(c1+c2).  */
6241           if (flag_unsafe_math_optimizations
6242               && TREE_CODE (arg0) == MULT_EXPR
6243               && TREE_CODE (arg1) == MULT_EXPR
6244               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6245               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6246               && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6247               && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6248               && operand_equal_p (TREE_OPERAND (arg0, 0),
6249                                   TREE_OPERAND (arg1, 0), 0))
6250             {
6251               REAL_VALUE_TYPE c1, c2;
6252
6253               c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6254               c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6255               real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
6256               return fold (build2 (MULT_EXPR, type,
6257                                    TREE_OPERAND (arg0, 0),
6258                                    build_real (type, c1)));
6259             }
6260           /* Convert a + (b*c + d*e) into (a + b*c) + d*e */
6261           if (flag_unsafe_math_optimizations
6262               && TREE_CODE (arg1) == PLUS_EXPR
6263               && TREE_CODE (arg0) != MULT_EXPR)
6264             {
6265               tree tree10 = TREE_OPERAND (arg1, 0);
6266               tree tree11 = TREE_OPERAND (arg1, 1);
6267               if (TREE_CODE (tree11) == MULT_EXPR
6268                   && TREE_CODE (tree10) == MULT_EXPR)
6269                 {
6270                   tree tree0;
6271                   tree0 = fold (build2 (PLUS_EXPR, type, arg0, tree10));
6272                   return fold (build2 (PLUS_EXPR, type, tree0, tree11));
6273                 }
6274             }
6275           /* Convert (b*c + d*e) + a into b*c + (d*e +a) */
6276           if (flag_unsafe_math_optimizations
6277               && TREE_CODE (arg0) == PLUS_EXPR
6278               && TREE_CODE (arg1) != MULT_EXPR)
6279             {
6280               tree tree00 = TREE_OPERAND (arg0, 0);
6281               tree tree01 = TREE_OPERAND (arg0, 1);
6282               if (TREE_CODE (tree01) == MULT_EXPR
6283                   && TREE_CODE (tree00) == MULT_EXPR)
6284                 {
6285                   tree tree0;
6286                   tree0 = fold (build2 (PLUS_EXPR, type, tree01, arg1));
6287                   return fold (build2 (PLUS_EXPR, type, tree00, tree0));
6288                 }
6289             }
6290         }
6291
6292      bit_rotate:
6293       /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
6294          is a rotate of A by C1 bits.  */
6295       /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
6296          is a rotate of A by B bits.  */
6297       {
6298         enum tree_code code0, code1;
6299         code0 = TREE_CODE (arg0);
6300         code1 = TREE_CODE (arg1);
6301         if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
6302              || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
6303             && operand_equal_p (TREE_OPERAND (arg0, 0),
6304                                 TREE_OPERAND (arg1, 0), 0)
6305             && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6306           {
6307             tree tree01, tree11;
6308             enum tree_code code01, code11;
6309
6310             tree01 = TREE_OPERAND (arg0, 1);
6311             tree11 = TREE_OPERAND (arg1, 1);
6312             STRIP_NOPS (tree01);
6313             STRIP_NOPS (tree11);
6314             code01 = TREE_CODE (tree01);
6315             code11 = TREE_CODE (tree11);
6316             if (code01 == INTEGER_CST
6317                 && code11 == INTEGER_CST
6318                 && TREE_INT_CST_HIGH (tree01) == 0
6319                 && TREE_INT_CST_HIGH (tree11) == 0
6320                 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
6321                     == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
6322               return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
6323                              code0 == LSHIFT_EXPR ? tree01 : tree11);
6324             else if (code11 == MINUS_EXPR)
6325               {
6326                 tree tree110, tree111;
6327                 tree110 = TREE_OPERAND (tree11, 0);
6328                 tree111 = TREE_OPERAND (tree11, 1);
6329                 STRIP_NOPS (tree110);
6330                 STRIP_NOPS (tree111);
6331                 if (TREE_CODE (tree110) == INTEGER_CST
6332                     && 0 == compare_tree_int (tree110,
6333                                               TYPE_PRECISION
6334                                               (TREE_TYPE (TREE_OPERAND
6335                                                           (arg0, 0))))
6336                     && operand_equal_p (tree01, tree111, 0))
6337                   return build2 ((code0 == LSHIFT_EXPR
6338                                   ? LROTATE_EXPR
6339                                   : RROTATE_EXPR),
6340                                  type, TREE_OPERAND (arg0, 0), tree01);
6341               }
6342             else if (code01 == MINUS_EXPR)
6343               {
6344                 tree tree010, tree011;
6345                 tree010 = TREE_OPERAND (tree01, 0);
6346                 tree011 = TREE_OPERAND (tree01, 1);
6347                 STRIP_NOPS (tree010);
6348                 STRIP_NOPS (tree011);
6349                 if (TREE_CODE (tree010) == INTEGER_CST
6350                     && 0 == compare_tree_int (tree010,
6351                                               TYPE_PRECISION
6352                                               (TREE_TYPE (TREE_OPERAND
6353                                                           (arg0, 0))))
6354                     && operand_equal_p (tree11, tree011, 0))
6355                   return build2 ((code0 != LSHIFT_EXPR
6356                                   ? LROTATE_EXPR
6357                                   : RROTATE_EXPR),
6358                                  type, TREE_OPERAND (arg0, 0), tree11);
6359               }
6360           }
6361       }
6362
6363     associate:
6364       /* In most languages, can't associate operations on floats through
6365          parentheses.  Rather than remember where the parentheses were, we
6366          don't associate floats at all, unless the user has specified
6367          -funsafe-math-optimizations.  */
6368
6369       if (! wins
6370           && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
6371         {
6372           tree var0, con0, lit0, minus_lit0;
6373           tree var1, con1, lit1, minus_lit1;
6374
6375           /* Split both trees into variables, constants, and literals.  Then
6376              associate each group together, the constants with literals,
6377              then the result with variables.  This increases the chances of
6378              literals being recombined later and of generating relocatable
6379              expressions for the sum of a constant and literal.  */
6380           var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
6381           var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
6382                              code == MINUS_EXPR);
6383
6384           /* Only do something if we found more than two objects.  Otherwise,
6385              nothing has changed and we risk infinite recursion.  */
6386           if (2 < ((var0 != 0) + (var1 != 0)
6387                    + (con0 != 0) + (con1 != 0)
6388                    + (lit0 != 0) + (lit1 != 0)
6389                    + (minus_lit0 != 0) + (minus_lit1 != 0)))
6390             {
6391               /* Recombine MINUS_EXPR operands by using PLUS_EXPR.  */
6392               if (code == MINUS_EXPR)
6393                 code = PLUS_EXPR;
6394
6395               var0 = associate_trees (var0, var1, code, type);
6396               con0 = associate_trees (con0, con1, code, type);
6397               lit0 = associate_trees (lit0, lit1, code, type);
6398               minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
6399
6400               /* Preserve the MINUS_EXPR if the negative part of the literal is
6401                  greater than the positive part.  Otherwise, the multiplicative
6402                  folding code (i.e extract_muldiv) may be fooled in case
6403                  unsigned constants are subtracted, like in the following
6404                  example: ((X*2 + 4) - 8U)/2.  */
6405               if (minus_lit0 && lit0)
6406                 {
6407                   if (TREE_CODE (lit0) == INTEGER_CST
6408                       && TREE_CODE (minus_lit0) == INTEGER_CST
6409                       && tree_int_cst_lt (lit0, minus_lit0))
6410                     {
6411                       minus_lit0 = associate_trees (minus_lit0, lit0,
6412                                                     MINUS_EXPR, type);
6413                       lit0 = 0;
6414                     }
6415                   else
6416                     {
6417                       lit0 = associate_trees (lit0, minus_lit0,
6418                                               MINUS_EXPR, type);
6419                       minus_lit0 = 0;
6420                     }
6421                 }
6422               if (minus_lit0)
6423                 {
6424                   if (con0 == 0)
6425                     return fold_convert (type,
6426                                          associate_trees (var0, minus_lit0,
6427                                                           MINUS_EXPR, type));
6428                   else
6429                     {
6430                       con0 = associate_trees (con0, minus_lit0,
6431                                               MINUS_EXPR, type);
6432                       return fold_convert (type,
6433                                            associate_trees (var0, con0,
6434                                                             PLUS_EXPR, type));
6435                     }
6436                 }
6437
6438               con0 = associate_trees (con0, lit0, code, type);
6439               return fold_convert (type, associate_trees (var0, con0,
6440                                                           code, type));
6441             }
6442         }
6443
6444     binary:
6445       if (wins)
6446         t1 = const_binop (code, arg0, arg1, 0);
6447       if (t1 != NULL_TREE)
6448         {
6449           /* The return value should always have
6450              the same type as the original expression.  */
6451           if (TREE_TYPE (t1) != type)
6452             t1 = fold_convert (type, t1);
6453
6454           return t1;
6455         }
6456       return t;
6457
6458     case MINUS_EXPR:
6459       /* A - (-B) -> A + B */
6460       if (TREE_CODE (arg1) == NEGATE_EXPR)
6461         return fold (build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6462       /* (-A) - B -> (-B) - A  where B is easily negated and we can swap.  */
6463       if (TREE_CODE (arg0) == NEGATE_EXPR
6464           && (FLOAT_TYPE_P (type)
6465               || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
6466           && negate_expr_p (arg1)
6467           && reorder_operands_p (arg0, arg1))
6468         return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
6469                              TREE_OPERAND (arg0, 0)));
6470
6471       if (! FLOAT_TYPE_P (type))
6472         {
6473           if (! wins && integer_zerop (arg0))
6474             return negate_expr (fold_convert (type, arg1));
6475           if (integer_zerop (arg1))
6476             return non_lvalue (fold_convert (type, arg0));
6477
6478           /* Fold A - (A & B) into ~B & A.  */
6479           if (!TREE_SIDE_EFFECTS (arg0)
6480               && TREE_CODE (arg1) == BIT_AND_EXPR)
6481             {
6482               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
6483                 return fold (build2 (BIT_AND_EXPR, type,
6484                                      fold (build1 (BIT_NOT_EXPR, type,
6485                                                    TREE_OPERAND (arg1, 0))),
6486                                      arg0));
6487               if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
6488                 return fold (build2 (BIT_AND_EXPR, type,
6489                                      fold (build1 (BIT_NOT_EXPR, type,
6490                                                    TREE_OPERAND (arg1, 1))),
6491                                      arg0));
6492             }
6493
6494           /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6495              any power of 2 minus 1.  */
6496           if (TREE_CODE (arg0) == BIT_AND_EXPR
6497               && TREE_CODE (arg1) == BIT_AND_EXPR
6498               && operand_equal_p (TREE_OPERAND (arg0, 0),
6499                                   TREE_OPERAND (arg1, 0), 0))
6500             {
6501               tree mask0 = TREE_OPERAND (arg0, 1);
6502               tree mask1 = TREE_OPERAND (arg1, 1);
6503               tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
6504               
6505               if (operand_equal_p (tem, mask1, 0))
6506                 {
6507                   tem = fold (build2 (BIT_XOR_EXPR, type,
6508                                       TREE_OPERAND (arg0, 0), mask1));
6509                   return fold (build2 (MINUS_EXPR, type, tem, mask1));
6510                 }
6511             }
6512         }
6513
6514       /* See if ARG1 is zero and X - ARG1 reduces to X.  */
6515       else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
6516         return non_lvalue (fold_convert (type, arg0));
6517
6518       /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
6519          ARG0 is zero and X + ARG0 reduces to X, since that would mean
6520          (-ARG1 + ARG0) reduces to -ARG1.  */
6521       else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6522         return negate_expr (fold_convert (type, arg1));
6523
6524       /* Fold &x - &x.  This can happen from &x.foo - &x.
6525          This is unsafe for certain floats even in non-IEEE formats.
6526          In IEEE, it is unsafe because it does wrong for NaNs.
6527          Also note that operand_equal_p is always false if an operand
6528          is volatile.  */
6529
6530       if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
6531           && operand_equal_p (arg0, arg1, 0))
6532         return fold_convert (type, integer_zero_node);
6533
6534       /* A - B -> A + (-B) if B is easily negatable.  */
6535       if (!wins && negate_expr_p (arg1)
6536           && (FLOAT_TYPE_P (type)
6537               || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
6538         return fold (build2 (PLUS_EXPR, type, arg0, negate_expr (arg1)));
6539
6540       if (TREE_CODE (arg0) == MULT_EXPR
6541           && TREE_CODE (arg1) == MULT_EXPR
6542           && (INTEGRAL_TYPE_P (type) || flag_unsafe_math_optimizations))
6543         {
6544           /* (A * C) - (B * C) -> (A-B) * C.  */
6545           if (operand_equal_p (TREE_OPERAND (arg0, 1),
6546                                TREE_OPERAND (arg1, 1), 0))
6547             return fold (build2 (MULT_EXPR, type,
6548                                  fold (build2 (MINUS_EXPR, type,
6549                                                TREE_OPERAND (arg0, 0),
6550                                                TREE_OPERAND (arg1, 0))),
6551                                  TREE_OPERAND (arg0, 1)));
6552           /* (A * C1) - (A * C2) -> A * (C1-C2).  */
6553           if (operand_equal_p (TREE_OPERAND (arg0, 0),
6554                                TREE_OPERAND (arg1, 0), 0))
6555             return fold (build2 (MULT_EXPR, type,
6556                                  TREE_OPERAND (arg0, 0),
6557                                  fold (build2 (MINUS_EXPR, type,
6558                                                TREE_OPERAND (arg0, 1),
6559                                                TREE_OPERAND (arg1, 1)))));
6560         }
6561
6562       goto associate;
6563
6564     case MULT_EXPR:
6565       /* (-A) * (-B) -> A * B  */
6566       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6567         return fold (build2 (MULT_EXPR, type,
6568                              TREE_OPERAND (arg0, 0),
6569                              negate_expr (arg1)));
6570       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6571         return fold (build2 (MULT_EXPR, type,
6572                              negate_expr (arg0),
6573                              TREE_OPERAND (arg1, 0)));
6574
6575       if (! FLOAT_TYPE_P (type))
6576         {
6577           if (integer_zerop (arg1))
6578             return omit_one_operand (type, arg1, arg0);
6579           if (integer_onep (arg1))
6580             return non_lvalue (fold_convert (type, arg0));
6581
6582           /* (a * (1 << b)) is (a << b)  */
6583           if (TREE_CODE (arg1) == LSHIFT_EXPR
6584               && integer_onep (TREE_OPERAND (arg1, 0)))
6585             return fold (build2 (LSHIFT_EXPR, type, arg0,
6586                                  TREE_OPERAND (arg1, 1)));
6587           if (TREE_CODE (arg0) == LSHIFT_EXPR
6588               && integer_onep (TREE_OPERAND (arg0, 0)))
6589             return fold (build2 (LSHIFT_EXPR, type, arg1,
6590                                  TREE_OPERAND (arg0, 1)));
6591
6592           if (TREE_CODE (arg1) == INTEGER_CST
6593               && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
6594                                              fold_convert (type, arg1),
6595                                              code, NULL_TREE)))
6596             return fold_convert (type, tem);
6597
6598         }
6599       else
6600         {
6601           /* Maybe fold x * 0 to 0.  The expressions aren't the same
6602              when x is NaN, since x * 0 is also NaN.  Nor are they the
6603              same in modes with signed zeros, since multiplying a
6604              negative value by 0 gives -0, not +0.  */
6605           if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
6606               && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
6607               && real_zerop (arg1))
6608             return omit_one_operand (type, arg1, arg0);
6609           /* In IEEE floating point, x*1 is not equivalent to x for snans.  */
6610           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6611               && real_onep (arg1))
6612             return non_lvalue (fold_convert (type, arg0));
6613
6614           /* Transform x * -1.0 into -x.  */
6615           if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6616               && real_minus_onep (arg1))
6617             return fold_convert (type, negate_expr (arg0));
6618
6619           /* Convert (C1/X)*C2 into (C1*C2)/X.  */
6620           if (flag_unsafe_math_optimizations
6621               && TREE_CODE (arg0) == RDIV_EXPR
6622               && TREE_CODE (arg1) == REAL_CST
6623               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
6624             {
6625               tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
6626                                       arg1, 0);
6627               if (tem)
6628                 return fold (build2 (RDIV_EXPR, type, tem,
6629                                      TREE_OPERAND (arg0, 1)));
6630             }
6631
6632           if (flag_unsafe_math_optimizations)
6633             {
6634               enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6635               enum built_in_function fcode1 = builtin_mathfn_code (arg1);
6636
6637               /* Optimizations of root(...)*root(...).  */
6638               if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
6639                 {
6640                   tree rootfn, arg, arglist;
6641                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6642                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6643
6644                   /* Optimize sqrt(x)*sqrt(x) as x.  */
6645                   if (BUILTIN_SQRT_P (fcode0)
6646                       && operand_equal_p (arg00, arg10, 0)
6647                       && ! HONOR_SNANS (TYPE_MODE (type)))
6648                     return arg00;
6649
6650                   /* Optimize root(x)*root(y) as root(x*y).  */
6651                   rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6652                   arg = fold (build2 (MULT_EXPR, type, arg00, arg10));
6653                   arglist = build_tree_list (NULL_TREE, arg);
6654                   return build_function_call_expr (rootfn, arglist);
6655                 }
6656
6657               /* Optimize expN(x)*expN(y) as expN(x+y).  */
6658               if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
6659                 {
6660                   tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6661                   tree arg = build2 (PLUS_EXPR, type,
6662                                      TREE_VALUE (TREE_OPERAND (arg0, 1)),
6663                                      TREE_VALUE (TREE_OPERAND (arg1, 1)));
6664                   tree arglist = build_tree_list (NULL_TREE, fold (arg));
6665                   return build_function_call_expr (expfn, arglist);
6666                 }
6667
6668               /* Optimizations of pow(...)*pow(...).  */
6669               if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
6670                   || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
6671                   || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
6672                 {
6673                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6674                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6675                                                                      1)));
6676                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6677                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6678                                                                      1)));
6679
6680                   /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y).  */
6681                   if (operand_equal_p (arg01, arg11, 0))
6682                     {
6683                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6684                       tree arg = build2 (MULT_EXPR, type, arg00, arg10);
6685                       tree arglist = tree_cons (NULL_TREE, fold (arg),
6686                                                 build_tree_list (NULL_TREE,
6687                                                                  arg01));
6688                       return build_function_call_expr (powfn, arglist);
6689                     }
6690
6691                   /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
6692                   if (operand_equal_p (arg00, arg10, 0))
6693                     {
6694                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6695                       tree arg = fold (build2 (PLUS_EXPR, type, arg01, arg11));
6696                       tree arglist = tree_cons (NULL_TREE, arg00,
6697                                                 build_tree_list (NULL_TREE,
6698                                                                  arg));
6699                       return build_function_call_expr (powfn, arglist);
6700                     }
6701                 }
6702
6703               /* Optimize tan(x)*cos(x) as sin(x).  */
6704               if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
6705                    || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
6706                    || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
6707                    || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
6708                    || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
6709                    || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
6710                   && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6711                                       TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6712                 {
6713                   tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
6714
6715                   if (sinfn != NULL_TREE)
6716                     return build_function_call_expr (sinfn,
6717                                                      TREE_OPERAND (arg0, 1));
6718                 }
6719
6720               /* Optimize x*pow(x,c) as pow(x,c+1).  */
6721               if (fcode1 == BUILT_IN_POW
6722                   || fcode1 == BUILT_IN_POWF
6723                   || fcode1 == BUILT_IN_POWL)
6724                 {
6725                   tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6726                   tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6727                                                                      1)));
6728                   if (TREE_CODE (arg11) == REAL_CST
6729                       && ! TREE_CONSTANT_OVERFLOW (arg11)
6730                       && operand_equal_p (arg0, arg10, 0))
6731                     {
6732                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6733                       REAL_VALUE_TYPE c;
6734                       tree arg, arglist;
6735
6736                       c = TREE_REAL_CST (arg11);
6737                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6738                       arg = build_real (type, c);
6739                       arglist = build_tree_list (NULL_TREE, arg);
6740                       arglist = tree_cons (NULL_TREE, arg0, arglist);
6741                       return build_function_call_expr (powfn, arglist);
6742                     }
6743                 }
6744
6745               /* Optimize pow(x,c)*x as pow(x,c+1).  */
6746               if (fcode0 == BUILT_IN_POW
6747                   || fcode0 == BUILT_IN_POWF
6748                   || fcode0 == BUILT_IN_POWL)
6749                 {
6750                   tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6751                   tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6752                                                                      1)));
6753                   if (TREE_CODE (arg01) == REAL_CST
6754                       && ! TREE_CONSTANT_OVERFLOW (arg01)
6755                       && operand_equal_p (arg1, arg00, 0))
6756                     {
6757                       tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6758                       REAL_VALUE_TYPE c;
6759                       tree arg, arglist;
6760
6761                       c = TREE_REAL_CST (arg01);
6762                       real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6763                       arg = build_real (type, c);
6764                       arglist = build_tree_list (NULL_TREE, arg);
6765                       arglist = tree_cons (NULL_TREE, arg1, arglist);
6766                       return build_function_call_expr (powfn, arglist);
6767                     }
6768                 }
6769
6770               /* Optimize x*x as pow(x,2.0), which is expanded as x*x.  */
6771               if (! optimize_size
6772                   && operand_equal_p (arg0, arg1, 0))
6773                 {
6774                   tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6775
6776                   if (powfn)
6777                     {
6778                       tree arg = build_real (type, dconst2);
6779                       tree arglist = build_tree_list (NULL_TREE, arg);
6780                       arglist = tree_cons (NULL_TREE, arg0, arglist);
6781                       return build_function_call_expr (powfn, arglist);
6782                     }
6783                 }
6784             }
6785         }
6786       goto associate;
6787
6788     case BIT_IOR_EXPR:
6789     bit_ior:
6790       if (integer_all_onesp (arg1))
6791         return omit_one_operand (type, arg1, arg0);
6792       if (integer_zerop (arg1))
6793         return non_lvalue (fold_convert (type, arg0));
6794       if (operand_equal_p (arg0, arg1, 0))
6795         return non_lvalue (fold_convert (type, arg0));
6796       t1 = distribute_bit_expr (code, type, arg0, arg1);
6797       if (t1 != NULL_TREE)
6798         return t1;
6799
6800       /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
6801
6802          This results in more efficient code for machines without a NAND
6803          instruction.  Combine will canonicalize to the first form
6804          which will allow use of NAND instructions provided by the
6805          backend if they exist.  */
6806       if (TREE_CODE (arg0) == BIT_NOT_EXPR
6807           && TREE_CODE (arg1) == BIT_NOT_EXPR)
6808         {
6809           return fold (build1 (BIT_NOT_EXPR, type,
6810                                build2 (BIT_AND_EXPR, type,
6811                                        TREE_OPERAND (arg0, 0),
6812                                        TREE_OPERAND (arg1, 0))));
6813         }
6814
6815       /* See if this can be simplified into a rotate first.  If that
6816          is unsuccessful continue in the association code.  */
6817       goto bit_rotate;
6818
6819     case BIT_XOR_EXPR:
6820       if (integer_zerop (arg1))
6821         return non_lvalue (fold_convert (type, arg0));
6822       if (integer_all_onesp (arg1))
6823         return fold (build1 (BIT_NOT_EXPR, type, arg0));
6824       if (operand_equal_p (arg0, arg1, 0))
6825         return omit_one_operand (type, integer_zero_node, arg0);
6826
6827       /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
6828          with a constant, and the two constants have no bits in common,
6829          we should treat this as a BIT_IOR_EXPR since this may produce more
6830          simplifications.  */
6831       if (TREE_CODE (arg0) == BIT_AND_EXPR
6832           && TREE_CODE (arg1) == BIT_AND_EXPR
6833           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6834           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6835           && integer_zerop (const_binop (BIT_AND_EXPR,
6836                                          TREE_OPERAND (arg0, 1),
6837                                          TREE_OPERAND (arg1, 1), 0)))
6838         {
6839           code = BIT_IOR_EXPR;
6840           goto bit_ior;
6841         }
6842
6843       /* See if this can be simplified into a rotate first.  If that
6844          is unsuccessful continue in the association code.  */
6845       goto bit_rotate;
6846
6847     case BIT_AND_EXPR:
6848       if (integer_all_onesp (arg1))
6849         return non_lvalue (fold_convert (type, arg0));
6850       if (integer_zerop (arg1))
6851         return omit_one_operand (type, arg1, arg0);
6852       if (operand_equal_p (arg0, arg1, 0))
6853         return non_lvalue (fold_convert (type, arg0));
6854       t1 = distribute_bit_expr (code, type, arg0, arg1);
6855       if (t1 != NULL_TREE)
6856         return t1;
6857       /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char.  */
6858       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
6859           && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6860         {
6861           unsigned int prec
6862             = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
6863
6864           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
6865               && (~TREE_INT_CST_LOW (arg1)
6866                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6867             return fold_convert (type, TREE_OPERAND (arg0, 0));
6868         }
6869
6870       /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
6871
6872          This results in more efficient code for machines without a NOR
6873          instruction.  Combine will canonicalize to the first form
6874          which will allow use of NOR instructions provided by the
6875          backend if they exist.  */
6876       if (TREE_CODE (arg0) == BIT_NOT_EXPR
6877           && TREE_CODE (arg1) == BIT_NOT_EXPR)
6878         {
6879           return fold (build1 (BIT_NOT_EXPR, type,
6880                                build2 (BIT_IOR_EXPR, type,
6881                                        TREE_OPERAND (arg0, 0),
6882                                        TREE_OPERAND (arg1, 0))));
6883         }
6884
6885       goto associate;
6886
6887     case RDIV_EXPR:
6888       /* Don't touch a floating-point divide by zero unless the mode
6889          of the constant can represent infinity.  */
6890       if (TREE_CODE (arg1) == REAL_CST
6891           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
6892           && real_zerop (arg1))
6893         return t;
6894
6895       /* (-A) / (-B) -> A / B  */
6896       if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6897         return fold (build2 (RDIV_EXPR, type,
6898                              TREE_OPERAND (arg0, 0),
6899                              negate_expr (arg1)));
6900       if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6901         return fold (build2 (RDIV_EXPR, type,
6902                              negate_expr (arg0),
6903                              TREE_OPERAND (arg1, 0)));
6904
6905       /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
6906       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6907           && real_onep (arg1))
6908         return non_lvalue (fold_convert (type, arg0));
6909
6910       /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
6911       if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6912           && real_minus_onep (arg1))
6913         return non_lvalue (fold_convert (type, negate_expr (arg0)));
6914
6915       /* If ARG1 is a constant, we can convert this to a multiply by the
6916          reciprocal.  This does not have the same rounding properties,
6917          so only do this if -funsafe-math-optimizations.  We can actually
6918          always safely do it if ARG1 is a power of two, but it's hard to
6919          tell if it is or not in a portable manner.  */
6920       if (TREE_CODE (arg1) == REAL_CST)
6921         {
6922           if (flag_unsafe_math_optimizations
6923               && 0 != (tem = const_binop (code, build_real (type, dconst1),
6924                                           arg1, 0)))
6925             return fold (build2 (MULT_EXPR, type, arg0, tem));
6926           /* Find the reciprocal if optimizing and the result is exact.  */
6927           if (optimize)
6928             {
6929               REAL_VALUE_TYPE r;
6930               r = TREE_REAL_CST (arg1);
6931               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6932                 {
6933                   tem = build_real (type, r);
6934                   return fold (build2 (MULT_EXPR, type, arg0, tem));
6935                 }
6936             }
6937         }
6938       /* Convert A/B/C to A/(B*C).  */
6939       if (flag_unsafe_math_optimizations
6940           && TREE_CODE (arg0) == RDIV_EXPR)
6941         return fold (build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6942                              fold (build2 (MULT_EXPR, type,
6943                                            TREE_OPERAND (arg0, 1), arg1))));
6944
6945       /* Convert A/(B/C) to (A/B)*C.  */
6946       if (flag_unsafe_math_optimizations
6947           && TREE_CODE (arg1) == RDIV_EXPR)
6948         return fold (build2 (MULT_EXPR, type,
6949                              fold (build2 (RDIV_EXPR, type, arg0,
6950                                            TREE_OPERAND (arg1, 0))),
6951                              TREE_OPERAND (arg1, 1)));
6952
6953       /* Convert C1/(X*C2) into (C1/C2)/X.  */
6954       if (flag_unsafe_math_optimizations
6955           && TREE_CODE (arg1) == MULT_EXPR
6956           && TREE_CODE (arg0) == REAL_CST
6957           && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
6958         {
6959           tree tem = const_binop (RDIV_EXPR, arg0,
6960                                   TREE_OPERAND (arg1, 1), 0);
6961           if (tem)
6962             return fold (build2 (RDIV_EXPR, type, tem,
6963                                  TREE_OPERAND (arg1, 0)));
6964         }
6965
6966       if (flag_unsafe_math_optimizations)
6967         {
6968           enum built_in_function fcode = builtin_mathfn_code (arg1);
6969           /* Optimize x/expN(y) into x*expN(-y).  */
6970           if (BUILTIN_EXPONENT_P (fcode))
6971             {
6972               tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6973               tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
6974               tree arglist = build_tree_list (NULL_TREE,
6975                                               fold_convert (type, arg));
6976               arg1 = build_function_call_expr (expfn, arglist);
6977               return fold (build2 (MULT_EXPR, type, arg0, arg1));
6978             }
6979
6980           /* Optimize x/pow(y,z) into x*pow(y,-z).  */
6981           if (fcode == BUILT_IN_POW
6982               || fcode == BUILT_IN_POWF
6983               || fcode == BUILT_IN_POWL)
6984             {
6985               tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6986               tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6987               tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
6988               tree neg11 = fold_convert (type, negate_expr (arg11));
6989               tree arglist = tree_cons(NULL_TREE, arg10,
6990                                        build_tree_list (NULL_TREE, neg11));
6991               arg1 = build_function_call_expr (powfn, arglist);
6992               return fold (build2 (MULT_EXPR, type, arg0, arg1));
6993             }
6994         }
6995
6996       if (flag_unsafe_math_optimizations)
6997         {
6998           enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6999           enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7000
7001           /* Optimize sin(x)/cos(x) as tan(x).  */
7002           if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
7003                || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
7004                || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
7005               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7006                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7007             {
7008               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7009
7010               if (tanfn != NULL_TREE)
7011                 return build_function_call_expr (tanfn,
7012                                                  TREE_OPERAND (arg0, 1));
7013             }
7014
7015           /* Optimize cos(x)/sin(x) as 1.0/tan(x).  */
7016           if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
7017                || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
7018                || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
7019               && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7020                                   TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7021             {
7022               tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7023
7024               if (tanfn != NULL_TREE)
7025                 {
7026                   tree tmp = TREE_OPERAND (arg0, 1);
7027                   tmp = build_function_call_expr (tanfn, tmp);
7028                   return fold (build2 (RDIV_EXPR, type,
7029                                        build_real (type, dconst1), tmp));
7030                 }
7031             }
7032
7033           /* Optimize pow(x,c)/x as pow(x,c-1).  */
7034           if (fcode0 == BUILT_IN_POW
7035               || fcode0 == BUILT_IN_POWF
7036               || fcode0 == BUILT_IN_POWL)
7037             {
7038               tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7039               tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7040               if (TREE_CODE (arg01) == REAL_CST
7041                   && ! TREE_CONSTANT_OVERFLOW (arg01)
7042                   && operand_equal_p (arg1, arg00, 0))
7043                 {
7044                   tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7045                   REAL_VALUE_TYPE c;
7046                   tree arg, arglist;
7047
7048                   c = TREE_REAL_CST (arg01);
7049                   real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
7050                   arg = build_real (type, c);
7051                   arglist = build_tree_list (NULL_TREE, arg);
7052                   arglist = tree_cons (NULL_TREE, arg1, arglist);
7053                   return build_function_call_expr (powfn, arglist);
7054                 }
7055             }
7056         }
7057       goto binary;
7058
7059     case TRUNC_DIV_EXPR:
7060     case ROUND_DIV_EXPR:
7061     case FLOOR_DIV_EXPR:
7062     case CEIL_DIV_EXPR:
7063     case EXACT_DIV_EXPR:
7064       if (integer_onep (arg1))
7065         return non_lvalue (fold_convert (type, arg0));
7066       if (integer_zerop (arg1))
7067         return t;
7068       /* X / -1 is -X.  */
7069       if (!TYPE_UNSIGNED (type)
7070           && TREE_CODE (arg1) == INTEGER_CST
7071           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7072           && TREE_INT_CST_HIGH (arg1) == -1)
7073         return fold_convert (type, negate_expr (arg0));
7074
7075       /* If arg0 is a multiple of arg1, then rewrite to the fastest div
7076          operation, EXACT_DIV_EXPR.
7077
7078          Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
7079          At one time others generated faster code, it's not clear if they do
7080          after the last round to changes to the DIV code in expmed.c.  */
7081       if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
7082           && multiple_of_p (type, arg0, arg1))
7083         return fold (build2 (EXACT_DIV_EXPR, type, arg0, arg1));
7084
7085       if (TREE_CODE (arg1) == INTEGER_CST
7086           && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7087                                          code, NULL_TREE)))
7088         return fold_convert (type, tem);
7089
7090       goto binary;
7091
7092     case CEIL_MOD_EXPR:
7093     case FLOOR_MOD_EXPR:
7094     case ROUND_MOD_EXPR:
7095     case TRUNC_MOD_EXPR:
7096       if (integer_onep (arg1))
7097         return omit_one_operand (type, integer_zero_node, arg0);
7098       if (integer_zerop (arg1))
7099         return t;
7100       /* X % -1 is zero.  */
7101       if (!TYPE_UNSIGNED (type)
7102           && TREE_CODE (arg1) == INTEGER_CST
7103           && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7104           && TREE_INT_CST_HIGH (arg1) == -1)
7105         return omit_one_operand (type, integer_zero_node, arg0);
7106
7107       if (TREE_CODE (arg1) == INTEGER_CST
7108           && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7109                                          code, NULL_TREE)))
7110         return fold_convert (type, tem);
7111
7112       goto binary;
7113
7114     case LROTATE_EXPR:
7115     case RROTATE_EXPR:
7116       if (integer_all_onesp (arg0))
7117         return omit_one_operand (type, arg0, arg1);
7118       goto shift;
7119
7120     case RSHIFT_EXPR:
7121       /* Optimize -1 >> x for arithmetic right shifts.  */
7122       if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
7123         return omit_one_operand (type, arg0, arg1);
7124       /* ... fall through ...  */
7125
7126     case LSHIFT_EXPR:
7127     shift:
7128       if (integer_zerop (arg1))
7129         return non_lvalue (fold_convert (type, arg0));
7130       if (integer_zerop (arg0))
7131         return omit_one_operand (type, arg0, arg1);
7132
7133       /* Since negative shift count is not well-defined,
7134          don't try to compute it in the compiler.  */
7135       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
7136         return t;
7137       /* Rewrite an LROTATE_EXPR by a constant into an
7138          RROTATE_EXPR by a new constant.  */
7139       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
7140         {
7141           tree tem = build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0);
7142           tem = fold_convert (TREE_TYPE (arg1), tem);
7143           tem = const_binop (MINUS_EXPR, tem, arg1, 0);
7144           return fold (build2 (RROTATE_EXPR, type, arg0, tem));
7145         }
7146
7147       /* If we have a rotate of a bit operation with the rotate count and
7148          the second operand of the bit operation both constant,
7149          permute the two operations.  */
7150       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7151           && (TREE_CODE (arg0) == BIT_AND_EXPR
7152               || TREE_CODE (arg0) == BIT_IOR_EXPR
7153               || TREE_CODE (arg0) == BIT_XOR_EXPR)
7154           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7155         return fold (build2 (TREE_CODE (arg0), type,
7156                              fold (build2 (code, type,
7157                                            TREE_OPERAND (arg0, 0), arg1)),
7158                              fold (build2 (code, type,
7159                                            TREE_OPERAND (arg0, 1), arg1))));
7160
7161       /* Two consecutive rotates adding up to the width of the mode can
7162          be ignored.  */
7163       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7164           && TREE_CODE (arg0) == RROTATE_EXPR
7165           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7166           && TREE_INT_CST_HIGH (arg1) == 0
7167           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
7168           && ((TREE_INT_CST_LOW (arg1)
7169                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
7170               == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
7171         return TREE_OPERAND (arg0, 0);
7172
7173       goto binary;
7174
7175     case MIN_EXPR:
7176       if (operand_equal_p (arg0, arg1, 0))
7177         return omit_one_operand (type, arg0, arg1);
7178       if (INTEGRAL_TYPE_P (type)
7179           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
7180         return omit_one_operand (type, arg1, arg0);
7181       goto associate;
7182
7183     case MAX_EXPR:
7184       if (operand_equal_p (arg0, arg1, 0))
7185         return omit_one_operand (type, arg0, arg1);
7186       if (INTEGRAL_TYPE_P (type)
7187           && TYPE_MAX_VALUE (type)
7188           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
7189         return omit_one_operand (type, arg1, arg0);
7190       goto associate;
7191
7192     case TRUTH_NOT_EXPR:
7193       /* The argument to invert_truthvalue must have Boolean type.  */
7194       if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7195           arg0 = fold_convert (boolean_type_node, arg0);
7196       
7197       /* Note that the operand of this must be an int
7198          and its values must be 0 or 1.
7199          ("true" is a fixed value perhaps depending on the language,
7200          but we don't handle values other than 1 correctly yet.)  */
7201       tem = invert_truthvalue (arg0);
7202       /* Avoid infinite recursion.  */
7203       if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7204         {
7205           tem = fold_single_bit_test (code, arg0, arg1, type);
7206           if (tem)
7207             return tem;
7208           return t;
7209         }
7210       return fold_convert (type, tem);
7211
7212     case TRUTH_ANDIF_EXPR:
7213       /* Note that the operands of this must be ints
7214          and their values must be 0 or 1.
7215          ("true" is a fixed value perhaps depending on the language.)  */
7216       /* If first arg is constant zero, return it.  */
7217       if (integer_zerop (arg0))
7218         return fold_convert (type, arg0);
7219     case TRUTH_AND_EXPR:
7220       /* If either arg is constant true, drop it.  */
7221       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7222         return non_lvalue (fold_convert (type, arg1));
7223       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
7224           /* Preserve sequence points.  */
7225           && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7226         return non_lvalue (fold_convert (type, arg0));
7227       /* If second arg is constant zero, result is zero, but first arg
7228          must be evaluated.  */
7229       if (integer_zerop (arg1))
7230         return omit_one_operand (type, arg1, arg0);
7231       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
7232          case will be handled here.  */
7233       if (integer_zerop (arg0))
7234         return omit_one_operand (type, arg0, arg1);
7235
7236     truth_andor:
7237       /* We only do these simplifications if we are optimizing.  */
7238       if (!optimize)
7239         return t;
7240
7241       /* Check for things like (A || B) && (A || C).  We can convert this
7242          to A || (B && C).  Note that either operator can be any of the four
7243          truth and/or operations and the transformation will still be
7244          valid.   Also note that we only care about order for the
7245          ANDIF and ORIF operators.  If B contains side effects, this
7246          might change the truth-value of A.  */
7247       if (TREE_CODE (arg0) == TREE_CODE (arg1)
7248           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7249               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7250               || TREE_CODE (arg0) == TRUTH_AND_EXPR
7251               || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7252           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7253         {
7254           tree a00 = TREE_OPERAND (arg0, 0);
7255           tree a01 = TREE_OPERAND (arg0, 1);
7256           tree a10 = TREE_OPERAND (arg1, 0);
7257           tree a11 = TREE_OPERAND (arg1, 1);
7258           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7259                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7260                              && (code == TRUTH_AND_EXPR
7261                                  || code == TRUTH_OR_EXPR));
7262
7263           if (operand_equal_p (a00, a10, 0))
7264             return fold (build2 (TREE_CODE (arg0), type, a00,
7265                                  fold (build2 (code, type, a01, a11))));
7266           else if (commutative && operand_equal_p (a00, a11, 0))
7267             return fold (build2 (TREE_CODE (arg0), type, a00,
7268                                  fold (build2 (code, type, a01, a10))));
7269           else if (commutative && operand_equal_p (a01, a10, 0))
7270             return fold (build2 (TREE_CODE (arg0), type, a01,
7271                                  fold (build2 (code, type, a00, a11))));
7272
7273           /* This case if tricky because we must either have commutative
7274              operators or else A10 must not have side-effects.  */
7275
7276           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7277                    && operand_equal_p (a01, a11, 0))
7278             return fold (build2 (TREE_CODE (arg0), type,
7279                                  fold (build2 (code, type, a00, a10)),
7280                                  a01));
7281         }
7282
7283       /* See if we can build a range comparison.  */
7284       if (0 != (tem = fold_range_test (t)))
7285         return tem;
7286
7287       /* Check for the possibility of merging component references.  If our
7288          lhs is another similar operation, try to merge its rhs with our
7289          rhs.  Then try to merge our lhs and rhs.  */
7290       if (TREE_CODE (arg0) == code
7291           && 0 != (tem = fold_truthop (code, type,
7292                                        TREE_OPERAND (arg0, 1), arg1)))
7293         return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7294
7295       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
7296         return tem;
7297
7298       return t;
7299
7300     case TRUTH_ORIF_EXPR:
7301       /* Note that the operands of this must be ints
7302          and their values must be 0 or true.
7303          ("true" is a fixed value perhaps depending on the language.)  */
7304       /* If first arg is constant true, return it.  */
7305       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7306         return fold_convert (type, arg0);
7307     case TRUTH_OR_EXPR:
7308       /* If either arg is constant zero, drop it.  */
7309       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
7310         return non_lvalue (fold_convert (type, arg1));
7311       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
7312           /* Preserve sequence points.  */
7313           && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7314         return non_lvalue (fold_convert (type, arg0));
7315       /* If second arg is constant true, result is true, but we must
7316          evaluate first arg.  */
7317       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
7318         return omit_one_operand (type, arg1, arg0);
7319       /* Likewise for first arg, but note this only occurs here for
7320          TRUTH_OR_EXPR.  */
7321       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7322         return omit_one_operand (type, arg0, arg1);
7323       goto truth_andor;
7324
7325     case TRUTH_XOR_EXPR:
7326       /* If either arg is constant zero, drop it.  */
7327       if (integer_zerop (arg0))
7328         return non_lvalue (fold_convert (type, arg1));
7329       if (integer_zerop (arg1))
7330         return non_lvalue (fold_convert (type, arg0));
7331       /* If either arg is constant true, this is a logical inversion.  */
7332       if (integer_onep (arg0))
7333         return non_lvalue (fold_convert (type, invert_truthvalue (arg1)));
7334       if (integer_onep (arg1))
7335         return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
7336       /* Identical arguments cancel to zero.  */
7337       if (operand_equal_p (arg0, arg1, 0))
7338         return omit_one_operand (type, integer_zero_node, arg0);
7339       return t;
7340
7341     case EQ_EXPR:
7342     case NE_EXPR:
7343     case LT_EXPR:
7344     case GT_EXPR:
7345     case LE_EXPR:
7346     case GE_EXPR:
7347       /* If one arg is a real or integer constant, put it last.  */
7348       if (tree_swap_operands_p (arg0, arg1, true))
7349         return fold (build2 (swap_tree_comparison (code), type, arg1, arg0));
7350
7351       /* If this is an equality comparison of the address of a non-weak
7352          object against zero, then we know the result.  */
7353       if ((code == EQ_EXPR || code == NE_EXPR)
7354           && TREE_CODE (arg0) == ADDR_EXPR
7355           && DECL_P (TREE_OPERAND (arg0, 0))
7356           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7357           && integer_zerop (arg1))
7358         return constant_boolean_node (code != EQ_EXPR, type);
7359
7360       /* If this is an equality comparison of the address of two non-weak,
7361          unaliased symbols neither of which are extern (since we do not
7362          have access to attributes for externs), then we know the result.  */
7363       if ((code == EQ_EXPR || code == NE_EXPR)
7364           && TREE_CODE (arg0) == ADDR_EXPR
7365           && DECL_P (TREE_OPERAND (arg0, 0))
7366           && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7367           && ! lookup_attribute ("alias",
7368                                  DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
7369           && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
7370           && TREE_CODE (arg1) == ADDR_EXPR
7371           && DECL_P (TREE_OPERAND (arg1, 0))
7372           && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
7373           && ! lookup_attribute ("alias",
7374                                  DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
7375           && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
7376         return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
7377                                       ? code == EQ_EXPR : code != EQ_EXPR,
7378                                       type);
7379
7380       if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7381         {
7382           tree targ0 = strip_float_extensions (arg0);
7383           tree targ1 = strip_float_extensions (arg1);
7384           tree newtype = TREE_TYPE (targ0);
7385
7386           if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7387             newtype = TREE_TYPE (targ1);
7388
7389           /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
7390           if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7391             return fold (build2 (code, type, fold_convert (newtype, targ0),
7392                                  fold_convert (newtype, targ1)));
7393
7394           /* (-a) CMP (-b) -> b CMP a  */
7395           if (TREE_CODE (arg0) == NEGATE_EXPR
7396               && TREE_CODE (arg1) == NEGATE_EXPR)
7397             return fold (build2 (code, type, TREE_OPERAND (arg1, 0),
7398                                  TREE_OPERAND (arg0, 0)));
7399
7400           if (TREE_CODE (arg1) == REAL_CST)
7401           {
7402             REAL_VALUE_TYPE cst;
7403             cst = TREE_REAL_CST (arg1);
7404
7405             /* (-a) CMP CST -> a swap(CMP) (-CST)  */
7406             if (TREE_CODE (arg0) == NEGATE_EXPR)
7407               return
7408                 fold (build2 (swap_tree_comparison (code), type,
7409                               TREE_OPERAND (arg0, 0),
7410                               build_real (TREE_TYPE (arg1),
7411                                           REAL_VALUE_NEGATE (cst))));
7412
7413             /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
7414             /* a CMP (-0) -> a CMP 0  */
7415             if (REAL_VALUE_MINUS_ZERO (cst))
7416               return fold (build2 (code, type, arg0,
7417                                    build_real (TREE_TYPE (arg1), dconst0)));
7418
7419             /* x != NaN is always true, other ops are always false.  */
7420             if (REAL_VALUE_ISNAN (cst)
7421                 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7422               {
7423                 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7424                 return omit_one_operand (type, tem, arg0);
7425               }
7426
7427             /* Fold comparisons against infinity.  */
7428             if (REAL_VALUE_ISINF (cst))
7429               {
7430                 tem = fold_inf_compare (code, type, arg0, arg1);
7431                 if (tem != NULL_TREE)
7432                   return tem;
7433               }
7434           }
7435
7436           /* If this is a comparison of a real constant with a PLUS_EXPR
7437              or a MINUS_EXPR of a real constant, we can convert it into a
7438              comparison with a revised real constant as long as no overflow
7439              occurs when unsafe_math_optimizations are enabled.  */
7440           if (flag_unsafe_math_optimizations
7441               && TREE_CODE (arg1) == REAL_CST
7442               && (TREE_CODE (arg0) == PLUS_EXPR
7443                   || TREE_CODE (arg0) == MINUS_EXPR)
7444               && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7445               && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7446                                           ? MINUS_EXPR : PLUS_EXPR,
7447                                           arg1, TREE_OPERAND (arg0, 1), 0))
7448               && ! TREE_CONSTANT_OVERFLOW (tem))
7449             return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7450
7451           /* Likewise, we can simplify a comparison of a real constant with
7452              a MINUS_EXPR whose first operand is also a real constant, i.e.
7453              (c1 - x) < c2 becomes x > c1-c2.  */
7454           if (flag_unsafe_math_optimizations
7455               && TREE_CODE (arg1) == REAL_CST
7456               && TREE_CODE (arg0) == MINUS_EXPR
7457               && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7458               && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7459                                           arg1, 0))
7460               && ! TREE_CONSTANT_OVERFLOW (tem))
7461             return fold (build2 (swap_tree_comparison (code), type,
7462                                  TREE_OPERAND (arg0, 1), tem));
7463
7464           /* Fold comparisons against built-in math functions.  */
7465           if (TREE_CODE (arg1) == REAL_CST
7466               && flag_unsafe_math_optimizations
7467               && ! flag_errno_math)
7468             {
7469               enum built_in_function fcode = builtin_mathfn_code (arg0);
7470
7471               if (fcode != END_BUILTINS)
7472                 {
7473                   tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7474                   if (tem != NULL_TREE)
7475                     return tem;
7476                 }
7477             }
7478         }
7479
7480       /* Convert foo++ == CONST into ++foo == CONST + INCR.  */
7481       if (TREE_CONSTANT (arg1)
7482           && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7483               || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7484           /* This optimization is invalid for ordered comparisons
7485              if CONST+INCR overflows or if foo+incr might overflow.
7486              This optimization is invalid for floating point due to rounding.
7487              For pointer types we assume overflow doesn't happen.  */
7488           && (POINTER_TYPE_P (TREE_TYPE (arg0))
7489               || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7490                   && (code == EQ_EXPR || code == NE_EXPR))))
7491         {
7492           tree varop, newconst;
7493
7494           if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7495             {
7496               newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
7497                                        arg1, TREE_OPERAND (arg0, 1)));
7498               varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7499                               TREE_OPERAND (arg0, 0),
7500                               TREE_OPERAND (arg0, 1));
7501             }
7502           else
7503             {
7504               newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
7505                                        arg1, TREE_OPERAND (arg0, 1)));
7506               varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7507                               TREE_OPERAND (arg0, 0),
7508                               TREE_OPERAND (arg0, 1));
7509             }
7510
7511
7512           /* If VAROP is a reference to a bitfield, we must mask
7513              the constant by the width of the field.  */
7514           if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7515               && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1)))
7516             {
7517               tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7518               int size = TREE_INT_CST_LOW (DECL_SIZE (fielddecl));
7519               tree folded_compare, shift;
7520
7521               /* First check whether the comparison would come out
7522                  always the same.  If we don't do that we would
7523                  change the meaning with the masking.  */
7524               folded_compare = fold (build2 (code, type,
7525                                              TREE_OPERAND (varop, 0),
7526                                              arg1));
7527               if (integer_zerop (folded_compare)
7528                   || integer_onep (folded_compare))
7529                 return omit_one_operand (type, folded_compare, varop);
7530
7531               shift = build_int_2 (TYPE_PRECISION (TREE_TYPE (varop)) - size,
7532                                    0);
7533               newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7534                                        newconst, shift));
7535               newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7536                                        newconst, shift));
7537             }
7538
7539           return fold (build2 (code, type, varop, newconst));
7540         }
7541
7542       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
7543          This transformation affects the cases which are handled in later
7544          optimizations involving comparisons with non-negative constants.  */
7545       if (TREE_CODE (arg1) == INTEGER_CST
7546           && TREE_CODE (arg0) != INTEGER_CST
7547           && tree_int_cst_sgn (arg1) > 0)
7548         {
7549           switch (code)
7550             {
7551             case GE_EXPR:
7552               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7553               return fold (build2 (GT_EXPR, type, arg0, arg1));
7554
7555             case LT_EXPR:
7556               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7557               return fold (build2 (LE_EXPR, type, arg0, arg1));
7558
7559             default:
7560               break;
7561             }
7562         }
7563
7564       /* Comparisons with the highest or lowest possible integer of
7565          the specified size will have known values. 
7566
7567          This is quite similar to fold_relational_hi_lo; however, my
7568          attempts to share the code have been nothing but trouble.
7569          I give up for now.  */
7570       {
7571         int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
7572
7573         if (TREE_CODE (arg1) == INTEGER_CST
7574             && ! TREE_CONSTANT_OVERFLOW (arg1)
7575             && width <= HOST_BITS_PER_WIDE_INT
7576             && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
7577                 || POINTER_TYPE_P (TREE_TYPE (arg1))))
7578           {
7579             unsigned HOST_WIDE_INT signed_max;
7580             unsigned HOST_WIDE_INT max, min;
7581
7582             signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
7583
7584             if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
7585               {
7586                 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
7587                 min = 0;
7588               }
7589             else
7590               {
7591                 max = signed_max;
7592                 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
7593               }
7594
7595             if (TREE_INT_CST_HIGH (arg1) == 0
7596                 && TREE_INT_CST_LOW (arg1) == max)
7597               switch (code)
7598                 {
7599                 case GT_EXPR:
7600                   return omit_one_operand (type, integer_zero_node, arg0);
7601
7602                 case GE_EXPR:
7603                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
7604
7605                 case LE_EXPR:
7606                   return omit_one_operand (type, integer_one_node, arg0);
7607
7608                 case LT_EXPR:
7609                   return fold (build2 (NE_EXPR, type, arg0, arg1));
7610
7611                 /* The GE_EXPR and LT_EXPR cases above are not normally
7612                    reached because of previous transformations.  */
7613
7614                 default:
7615                   break;
7616                 }
7617             else if (TREE_INT_CST_HIGH (arg1) == 0
7618                      && TREE_INT_CST_LOW (arg1) == max - 1)
7619               switch (code)
7620                 {
7621                 case GT_EXPR:
7622                   arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7623                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
7624                 case LE_EXPR:
7625                   arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7626                   return fold (build2 (NE_EXPR, type, arg0, arg1));
7627                 default:
7628                   break;
7629                 }
7630             else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7631                      && TREE_INT_CST_LOW (arg1) == min)
7632               switch (code)
7633                 {
7634                 case LT_EXPR:
7635                   return omit_one_operand (type, integer_zero_node, arg0);
7636
7637                 case LE_EXPR:
7638                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
7639
7640                 case GE_EXPR:
7641                   return omit_one_operand (type, integer_one_node, arg0);
7642
7643                 case GT_EXPR:
7644                   return fold (build2 (NE_EXPR, type, arg0, arg1));
7645
7646                 default:
7647                   break;
7648                 }
7649             else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7650                      && TREE_INT_CST_LOW (arg1) == min + 1)
7651               switch (code)
7652                 {
7653                 case GE_EXPR:
7654                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7655                   return fold (build2 (NE_EXPR, type, arg0, arg1));
7656                 case LT_EXPR:
7657                   arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7658                   return fold (build2 (EQ_EXPR, type, arg0, arg1));
7659                 default:
7660                   break;
7661                 }
7662
7663             else if (!in_gimple_form
7664                      && TREE_INT_CST_HIGH (arg1) == 0
7665                      && TREE_INT_CST_LOW (arg1) == signed_max
7666                      && TYPE_UNSIGNED (TREE_TYPE (arg1))
7667                      /* signed_type does not work on pointer types.  */
7668                      && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
7669               {
7670                 /* The following case also applies to X < signed_max+1
7671                    and X >= signed_max+1 because previous transformations.  */
7672                 if (code == LE_EXPR || code == GT_EXPR)
7673                   {
7674                     tree st0, st1;
7675                     st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
7676                     st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
7677                     return fold
7678                       (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
7679                                type, fold_convert (st0, arg0),
7680                                fold_convert (st1, integer_zero_node)));
7681                   }
7682               }
7683           }
7684       }
7685
7686       /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
7687          a MINUS_EXPR of a constant, we can convert it into a comparison with
7688          a revised constant as long as no overflow occurs.  */
7689       if ((code == EQ_EXPR || code == NE_EXPR)
7690           && TREE_CODE (arg1) == INTEGER_CST
7691           && (TREE_CODE (arg0) == PLUS_EXPR
7692               || TREE_CODE (arg0) == MINUS_EXPR)
7693           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7694           && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7695                                       ? MINUS_EXPR : PLUS_EXPR,
7696                                       arg1, TREE_OPERAND (arg0, 1), 0))
7697           && ! TREE_CONSTANT_OVERFLOW (tem))
7698         return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7699
7700       /* Similarly for a NEGATE_EXPR.  */
7701       else if ((code == EQ_EXPR || code == NE_EXPR)
7702                && TREE_CODE (arg0) == NEGATE_EXPR
7703                && TREE_CODE (arg1) == INTEGER_CST
7704                && 0 != (tem = negate_expr (arg1))
7705                && TREE_CODE (tem) == INTEGER_CST
7706                && ! TREE_CONSTANT_OVERFLOW (tem))
7707         return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7708
7709       /* If we have X - Y == 0, we can convert that to X == Y and similarly
7710          for !=.  Don't do this for ordered comparisons due to overflow.  */
7711       else if ((code == NE_EXPR || code == EQ_EXPR)
7712                && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
7713         return fold (build2 (code, type,
7714                              TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
7715
7716       /* If we are widening one operand of an integer comparison,
7717          see if the other operand is similarly being widened.  Perhaps we
7718          can do the comparison in the narrower type.  */
7719       else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7720                && TREE_CODE (arg0) == NOP_EXPR
7721                && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
7722                && (code == EQ_EXPR || code == NE_EXPR
7723                    || TYPE_UNSIGNED (TREE_TYPE (arg0))
7724                       == TYPE_UNSIGNED (TREE_TYPE (tem)))
7725                && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
7726                && (TREE_TYPE (t1) == TREE_TYPE (tem)
7727                    || (TREE_CODE (t1) == INTEGER_CST
7728                        && int_fits_type_p (t1, TREE_TYPE (tem)))))
7729         return fold (build2 (code, type, tem,
7730                              fold_convert (TREE_TYPE (tem), t1)));
7731
7732       /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7733          constant, we can simplify it.  */
7734       else if (TREE_CODE (arg1) == INTEGER_CST
7735                && (TREE_CODE (arg0) == MIN_EXPR
7736                    || TREE_CODE (arg0) == MAX_EXPR)
7737                && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7738         return optimize_minmax_comparison (t);
7739
7740       /* If we are comparing an ABS_EXPR with a constant, we can
7741          convert all the cases into explicit comparisons, but they may
7742          well not be faster than doing the ABS and one comparison.
7743          But ABS (X) <= C is a range comparison, which becomes a subtraction
7744          and a comparison, and is probably faster.  */
7745       else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7746                && TREE_CODE (arg0) == ABS_EXPR
7747                && ! TREE_SIDE_EFFECTS (arg0)
7748                && (0 != (tem = negate_expr (arg1)))
7749                && TREE_CODE (tem) == INTEGER_CST
7750                && ! TREE_CONSTANT_OVERFLOW (tem))
7751         return fold (build2 (TRUTH_ANDIF_EXPR, type,
7752                              build2 (GE_EXPR, type,
7753                                      TREE_OPERAND (arg0, 0), tem),
7754                              build2 (LE_EXPR, type,
7755                                      TREE_OPERAND (arg0, 0), arg1)));
7756
7757       /* If this is an EQ or NE comparison with zero and ARG0 is
7758          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
7759          two operations, but the latter can be done in one less insn
7760          on machines that have only two-operand insns or on which a
7761          constant cannot be the first operand.  */
7762       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
7763           && TREE_CODE (arg0) == BIT_AND_EXPR)
7764         {
7765           tree arg00 = TREE_OPERAND (arg0, 0);
7766           tree arg01 = TREE_OPERAND (arg0, 1);
7767           if (TREE_CODE (arg00) == LSHIFT_EXPR
7768               && integer_onep (TREE_OPERAND (arg00, 0)))
7769             return
7770               fold (build2 (code, type,
7771                             build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7772                                     build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
7773                                             arg01, TREE_OPERAND (arg00, 1)),
7774                                     fold_convert (TREE_TYPE (arg0),
7775                                                   integer_one_node)),
7776                             arg1));
7777           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
7778                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
7779             return
7780               fold (build2 (code, type,
7781                             build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7782                                     build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
7783                                             arg00, TREE_OPERAND (arg01, 1)),
7784                                     fold_convert (TREE_TYPE (arg0),
7785                                                   integer_one_node)),
7786                             arg1));
7787         }
7788
7789       /* If this is an NE or EQ comparison of zero against the result of a
7790          signed MOD operation whose second operand is a power of 2, make
7791          the MOD operation unsigned since it is simpler and equivalent.  */
7792       if ((code == NE_EXPR || code == EQ_EXPR)
7793           && integer_zerop (arg1)
7794           && !TYPE_UNSIGNED (TREE_TYPE (arg0))
7795           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
7796               || TREE_CODE (arg0) == CEIL_MOD_EXPR
7797               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
7798               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
7799           && integer_pow2p (TREE_OPERAND (arg0, 1)))
7800         {
7801           tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
7802           tree newmod = build2 (TREE_CODE (arg0), newtype,
7803                                 fold_convert (newtype,
7804                                               TREE_OPERAND (arg0, 0)),
7805                                 fold_convert (newtype,
7806                                               TREE_OPERAND (arg0, 1)));
7807
7808           return build2 (code, type, newmod, fold_convert (newtype, arg1));
7809         }
7810
7811       /* If this is an NE comparison of zero with an AND of one, remove the
7812          comparison since the AND will give the correct value.  */
7813       if (code == NE_EXPR && integer_zerop (arg1)
7814           && TREE_CODE (arg0) == BIT_AND_EXPR
7815           && integer_onep (TREE_OPERAND (arg0, 1)))
7816         return fold_convert (type, arg0);
7817
7818       /* If we have (A & C) == C where C is a power of 2, convert this into
7819          (A & C) != 0.  Similarly for NE_EXPR.  */
7820       if ((code == EQ_EXPR || code == NE_EXPR)
7821           && TREE_CODE (arg0) == BIT_AND_EXPR
7822           && integer_pow2p (TREE_OPERAND (arg0, 1))
7823           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
7824         return fold (build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
7825                              arg0, integer_zero_node));
7826
7827       /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
7828          2, then fold the expression into shifts and logical operations.  */
7829       tem = fold_single_bit_test (code, arg0, arg1, type);
7830       if (tem)
7831         return tem;
7832
7833       /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
7834          Similarly for NE_EXPR.  */
7835       if ((code == EQ_EXPR || code == NE_EXPR)
7836           && TREE_CODE (arg0) == BIT_AND_EXPR
7837           && TREE_CODE (arg1) == INTEGER_CST
7838           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7839         {
7840           tree dandnotc
7841             = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7842                             arg1, build1 (BIT_NOT_EXPR,
7843                                           TREE_TYPE (TREE_OPERAND (arg0, 1)),
7844                                           TREE_OPERAND (arg0, 1))));
7845           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7846           if (integer_nonzerop (dandnotc))
7847             return omit_one_operand (type, rslt, arg0);
7848         }
7849
7850       /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7851          Similarly for NE_EXPR.  */
7852       if ((code == EQ_EXPR || code == NE_EXPR)
7853           && TREE_CODE (arg0) == BIT_IOR_EXPR
7854           && TREE_CODE (arg1) == INTEGER_CST
7855           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7856         {
7857           tree candnotd
7858             = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7859                             TREE_OPERAND (arg0, 1),
7860                             build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
7861           tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7862           if (integer_nonzerop (candnotd))
7863             return omit_one_operand (type, rslt, arg0);
7864         }
7865
7866       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
7867          and similarly for >= into !=.  */
7868       if ((code == LT_EXPR || code == GE_EXPR)
7869           && TYPE_UNSIGNED (TREE_TYPE (arg0))
7870           && TREE_CODE (arg1) == LSHIFT_EXPR
7871           && integer_onep (TREE_OPERAND (arg1, 0)))
7872         return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7873                        build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7874                                TREE_OPERAND (arg1, 1)),
7875                        fold_convert (TREE_TYPE (arg0), integer_zero_node));
7876
7877       else if ((code == LT_EXPR || code == GE_EXPR)
7878                && TYPE_UNSIGNED (TREE_TYPE (arg0))
7879                && (TREE_CODE (arg1) == NOP_EXPR
7880                    || TREE_CODE (arg1) == CONVERT_EXPR)
7881                && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
7882                && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
7883         return
7884           build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7885                   fold_convert (TREE_TYPE (arg0),
7886                                 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7887                                         TREE_OPERAND (TREE_OPERAND (arg1, 0),
7888                                                       1))),
7889                   fold_convert (TREE_TYPE (arg0), integer_zero_node));
7890
7891       /* Simplify comparison of something with itself.  (For IEEE
7892          floating-point, we can only do some of these simplifications.)  */
7893       if (operand_equal_p (arg0, arg1, 0))
7894         {
7895           switch (code)
7896             {
7897             case EQ_EXPR:
7898               if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7899                   || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7900                 return constant_boolean_node (1, type);
7901               break;
7902
7903             case GE_EXPR:
7904             case LE_EXPR:
7905               if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7906                   || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7907                 return constant_boolean_node (1, type);
7908               return fold (build2 (EQ_EXPR, type, arg0, arg1));
7909
7910             case NE_EXPR:
7911               /* For NE, we can only do this simplification if integer
7912                  or we don't honor IEEE floating point NaNs.  */
7913               if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7914                   && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7915                 break;
7916               /* ... fall through ...  */
7917             case GT_EXPR:
7918             case LT_EXPR:
7919               return constant_boolean_node (0, type);
7920             default:
7921               abort ();
7922             }
7923         }
7924
7925       /* If we are comparing an expression that just has comparisons
7926          of two integer values, arithmetic expressions of those comparisons,
7927          and constants, we can simplify it.  There are only three cases
7928          to check: the two values can either be equal, the first can be
7929          greater, or the second can be greater.  Fold the expression for
7930          those three values.  Since each value must be 0 or 1, we have
7931          eight possibilities, each of which corresponds to the constant 0
7932          or 1 or one of the six possible comparisons.
7933
7934          This handles common cases like (a > b) == 0 but also handles
7935          expressions like  ((x > y) - (y > x)) > 0, which supposedly
7936          occur in macroized code.  */
7937
7938       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7939         {
7940           tree cval1 = 0, cval2 = 0;
7941           int save_p = 0;
7942
7943           if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7944               /* Don't handle degenerate cases here; they should already
7945                  have been handled anyway.  */
7946               && cval1 != 0 && cval2 != 0
7947               && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7948               && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7949               && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7950               && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7951               && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7952               && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7953                                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7954             {
7955               tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7956               tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7957
7958               /* We can't just pass T to eval_subst in case cval1 or cval2
7959                  was the same as ARG1.  */
7960
7961               tree high_result
7962                 = fold (build2 (code, type,
7963                                 eval_subst (arg0, cval1, maxval,
7964                                             cval2, minval),
7965                                 arg1));
7966               tree equal_result
7967                 = fold (build2 (code, type,
7968                                 eval_subst (arg0, cval1, maxval,
7969                                             cval2, maxval),
7970                                 arg1));
7971               tree low_result
7972                 = fold (build2 (code, type,
7973                                 eval_subst (arg0, cval1, minval,
7974                                             cval2, maxval),
7975                                 arg1));
7976
7977               /* All three of these results should be 0 or 1.  Confirm they
7978                  are.  Then use those values to select the proper code
7979                  to use.  */
7980
7981               if ((integer_zerop (high_result)
7982                    || integer_onep (high_result))
7983                   && (integer_zerop (equal_result)
7984                       || integer_onep (equal_result))
7985                   && (integer_zerop (low_result)
7986                       || integer_onep (low_result)))
7987                 {
7988                   /* Make a 3-bit mask with the high-order bit being the
7989                      value for `>', the next for '=', and the low for '<'.  */
7990                   switch ((integer_onep (high_result) * 4)
7991                           + (integer_onep (equal_result) * 2)
7992                           + integer_onep (low_result))
7993                     {
7994                     case 0:
7995                       /* Always false.  */
7996                       return omit_one_operand (type, integer_zero_node, arg0);
7997                     case 1:
7998                       code = LT_EXPR;
7999                       break;
8000                     case 2:
8001                       code = EQ_EXPR;
8002                       break;
8003                     case 3:
8004                       code = LE_EXPR;
8005                       break;
8006                     case 4:
8007                       code = GT_EXPR;
8008                       break;
8009                     case 5:
8010                       code = NE_EXPR;
8011                       break;
8012                     case 6:
8013                       code = GE_EXPR;
8014                       break;
8015                     case 7:
8016                       /* Always true.  */
8017                       return omit_one_operand (type, integer_one_node, arg0);
8018                     }
8019
8020                   tem = build2 (code, type, cval1, cval2);
8021                   if (save_p)
8022                     return save_expr (tem);
8023                   else
8024                     return fold (tem);
8025                 }
8026             }
8027         }
8028
8029       /* If this is a comparison of a field, we may be able to simplify it.  */
8030       if (((TREE_CODE (arg0) == COMPONENT_REF
8031             && lang_hooks.can_use_bit_fields_p ())
8032            || TREE_CODE (arg0) == BIT_FIELD_REF)
8033           && (code == EQ_EXPR || code == NE_EXPR)
8034           /* Handle the constant case even without -O
8035              to make sure the warnings are given.  */
8036           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
8037         {
8038           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
8039           if (t1)
8040             return t1;
8041         }
8042
8043       /* If this is a comparison of complex values and either or both sides
8044          are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
8045          comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
8046          This may prevent needless evaluations.  */
8047       if ((code == EQ_EXPR || code == NE_EXPR)
8048           && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
8049           && (TREE_CODE (arg0) == COMPLEX_EXPR
8050               || TREE_CODE (arg1) == COMPLEX_EXPR
8051               || TREE_CODE (arg0) == COMPLEX_CST
8052               || TREE_CODE (arg1) == COMPLEX_CST))
8053         {
8054           tree subtype = TREE_TYPE (TREE_TYPE (arg0));
8055           tree real0, imag0, real1, imag1;
8056
8057           arg0 = save_expr (arg0);
8058           arg1 = save_expr (arg1);
8059           real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
8060           imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
8061           real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
8062           imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
8063
8064           return fold (build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
8065                                 : TRUTH_ORIF_EXPR),
8066                                type,
8067                                fold (build2 (code, type, real0, real1)),
8068                                fold (build2 (code, type, imag0, imag1))));
8069         }
8070
8071       /* Optimize comparisons of strlen vs zero to a compare of the
8072          first character of the string vs zero.  To wit,
8073                 strlen(ptr) == 0   =>  *ptr == 0
8074                 strlen(ptr) != 0   =>  *ptr != 0
8075          Other cases should reduce to one of these two (or a constant)
8076          due to the return value of strlen being unsigned.  */
8077       if ((code == EQ_EXPR || code == NE_EXPR)
8078           && integer_zerop (arg1)
8079           && TREE_CODE (arg0) == CALL_EXPR)
8080         {
8081           tree fndecl = get_callee_fndecl (arg0);
8082           tree arglist;
8083
8084           if (fndecl
8085               && DECL_BUILT_IN (fndecl)
8086               && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
8087               && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
8088               && (arglist = TREE_OPERAND (arg0, 1))
8089               && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
8090               && ! TREE_CHAIN (arglist))
8091             return fold (build2 (code, type,
8092                                  build1 (INDIRECT_REF, char_type_node,
8093                                          TREE_VALUE(arglist)),
8094                                  integer_zero_node));
8095         }
8096
8097       /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8098          into a single range test.  */
8099       if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8100           && TREE_CODE (arg1) == INTEGER_CST
8101           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8102           && !integer_zerop (TREE_OPERAND (arg0, 1))
8103           && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8104           && !TREE_OVERFLOW (arg1))
8105         {
8106           t1 = fold_div_compare (code, type, arg0, arg1);
8107           if (t1 != NULL_TREE)
8108             return t1;
8109         }
8110
8111       /* Both ARG0 and ARG1 are known to be constants at this point.  */
8112       t1 = fold_relational_const (code, type, arg0, arg1);
8113       return (t1 == NULL_TREE ? t : t1);
8114
8115     case COND_EXPR:
8116       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
8117          so all simple results must be passed through pedantic_non_lvalue.  */
8118       if (TREE_CODE (arg0) == INTEGER_CST)
8119         {
8120           tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
8121           /* Only optimize constant conditions when the selected branch
8122              has the same type as the COND_EXPR.  This avoids optimizing
8123              away "c ? x : throw", where the throw has a void type.  */
8124           if (! VOID_TYPE_P (TREE_TYPE (tem))
8125               || VOID_TYPE_P (type))
8126             return pedantic_non_lvalue (tem);
8127           return t;
8128         }
8129       if (operand_equal_p (arg1, TREE_OPERAND (t, 2), 0))
8130         return pedantic_omit_one_operand (type, arg1, arg0);
8131
8132       /* If we have A op B ? A : C, we may be able to convert this to a
8133          simpler expression, depending on the operation and the values
8134          of B and C.  Signed zeros prevent all of these transformations,
8135          for reasons given above each one.  */
8136
8137       if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
8138           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
8139                                              arg1, TREE_OPERAND (arg0, 1))
8140           && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
8141         {
8142           tree arg2 = TREE_OPERAND (t, 2);
8143           enum tree_code comp_code = TREE_CODE (arg0);
8144
8145           STRIP_NOPS (arg2);
8146
8147           /* If we have A op 0 ? A : -A, consider applying the following
8148              transformations:
8149
8150              A == 0? A : -A    same as -A
8151              A != 0? A : -A    same as A
8152              A >= 0? A : -A    same as abs (A)
8153              A > 0?  A : -A    same as abs (A)
8154              A <= 0? A : -A    same as -abs (A)
8155              A < 0?  A : -A    same as -abs (A)
8156
8157              None of these transformations work for modes with signed
8158              zeros.  If A is +/-0, the first two transformations will
8159              change the sign of the result (from +0 to -0, or vice
8160              versa).  The last four will fix the sign of the result,
8161              even though the original expressions could be positive or
8162              negative, depending on the sign of A.
8163
8164              Note that all these transformations are correct if A is
8165              NaN, since the two alternatives (A and -A) are also NaNs.  */
8166           if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
8167                ? real_zerop (TREE_OPERAND (arg0, 1))
8168                : integer_zerop (TREE_OPERAND (arg0, 1)))
8169               && TREE_CODE (arg2) == NEGATE_EXPR
8170               && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
8171             switch (comp_code)
8172               {
8173               case EQ_EXPR:
8174                 tem = fold_convert (TREE_TYPE (TREE_OPERAND (t, 1)), arg1);
8175                 tem = fold_convert (type, negate_expr (tem));
8176                 return pedantic_non_lvalue (tem);
8177               case NE_EXPR:
8178                 return pedantic_non_lvalue (fold_convert (type, arg1));
8179               case GE_EXPR:
8180               case GT_EXPR:
8181                 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8182                   arg1 = fold_convert (lang_hooks.types.signed_type
8183                                        (TREE_TYPE (arg1)), arg1);
8184                 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8185                 return pedantic_non_lvalue (fold_convert (type, arg1));
8186               case LE_EXPR:
8187               case LT_EXPR:
8188                 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8189                   arg1 = fold_convert (lang_hooks.types.signed_type
8190                                        (TREE_TYPE (arg1)), arg1);
8191                 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8192                 arg1 = negate_expr (fold_convert (type, arg1));
8193                 return pedantic_non_lvalue (arg1);
8194               default:
8195                 abort ();
8196               }
8197
8198           /* A != 0 ? A : 0 is simply A, unless A is -0.  Likewise
8199              A == 0 ? A : 0 is always 0 unless A is -0.  Note that
8200              both transformations are correct when A is NaN: A != 0
8201              is then true, and A == 0 is false.  */
8202
8203           if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
8204             {
8205               if (comp_code == NE_EXPR)
8206                 return pedantic_non_lvalue (fold_convert (type, arg1));
8207               else if (comp_code == EQ_EXPR)
8208                 return pedantic_non_lvalue (fold_convert (type, integer_zero_node));
8209             }
8210
8211           /* Try some transformations of A op B ? A : B.
8212
8213              A == B? A : B    same as B
8214              A != B? A : B    same as A
8215              A >= B? A : B    same as max (A, B)
8216              A > B?  A : B    same as max (B, A)
8217              A <= B? A : B    same as min (A, B)
8218              A < B?  A : B    same as min (B, A)
8219
8220              As above, these transformations don't work in the presence
8221              of signed zeros.  For example, if A and B are zeros of
8222              opposite sign, the first two transformations will change
8223              the sign of the result.  In the last four, the original
8224              expressions give different results for (A=+0, B=-0) and
8225              (A=-0, B=+0), but the transformed expressions do not.
8226
8227              The first two transformations are correct if either A or B
8228              is a NaN.  In the first transformation, the condition will
8229              be false, and B will indeed be chosen.  In the case of the
8230              second transformation, the condition A != B will be true,
8231              and A will be chosen.
8232
8233              The conversions to max() and min() are not correct if B is
8234              a number and A is not.  The conditions in the original
8235              expressions will be false, so all four give B.  The min()
8236              and max() versions would give a NaN instead.  */
8237           if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
8238                                               arg2, TREE_OPERAND (arg0, 0)))
8239             {
8240               tree comp_op0 = TREE_OPERAND (arg0, 0);
8241               tree comp_op1 = TREE_OPERAND (arg0, 1);
8242               tree comp_type = TREE_TYPE (comp_op0);
8243
8244               /* Avoid adding NOP_EXPRs in case this is an lvalue.  */
8245               if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
8246                 {
8247                   comp_type = type;
8248                   comp_op0 = arg1;
8249                   comp_op1 = arg2;
8250                 }
8251
8252               switch (comp_code)
8253                 {
8254                 case EQ_EXPR:
8255                   return pedantic_non_lvalue (fold_convert (type, arg2));
8256                 case NE_EXPR:
8257                   return pedantic_non_lvalue (fold_convert (type, arg1));
8258                 case LE_EXPR:
8259                 case LT_EXPR:
8260                   /* In C++ a ?: expression can be an lvalue, so put the
8261                      operand which will be used if they are equal first
8262                      so that we can convert this back to the
8263                      corresponding COND_EXPR.  */
8264                   if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8265                     return pedantic_non_lvalue (fold_convert
8266                       (type, fold (build2 (MIN_EXPR, comp_type,
8267                                            (comp_code == LE_EXPR
8268                                             ? comp_op0 : comp_op1),
8269                                            (comp_code == LE_EXPR
8270                                             ? comp_op1 : comp_op0)))));
8271                   break;
8272                 case GE_EXPR:
8273                 case GT_EXPR:
8274                   if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8275                     return pedantic_non_lvalue (fold_convert
8276                       (type, fold (build2 (MAX_EXPR, comp_type,
8277                                            (comp_code == GE_EXPR
8278                                             ? comp_op0 : comp_op1),
8279                                            (comp_code == GE_EXPR
8280                                             ? comp_op1 : comp_op0)))));
8281                   break;
8282                 default:
8283                   abort ();
8284                 }
8285             }
8286
8287           /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
8288              we might still be able to simplify this.  For example,
8289              if C1 is one less or one more than C2, this might have started
8290              out as a MIN or MAX and been transformed by this function.
8291              Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
8292
8293           if (INTEGRAL_TYPE_P (type)
8294               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8295               && TREE_CODE (arg2) == INTEGER_CST)
8296             switch (comp_code)
8297               {
8298               case EQ_EXPR:
8299                 /* We can replace A with C1 in this case.  */
8300                 arg1 = fold_convert (type, TREE_OPERAND (arg0, 1));
8301                 return fold (build3 (code, type, TREE_OPERAND (t, 0), arg1,
8302                                      TREE_OPERAND (t, 2)));
8303
8304               case LT_EXPR:
8305                 /* If C1 is C2 + 1, this is min(A, C2).  */
8306                 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8307                                        OEP_ONLY_CONST)
8308                     && operand_equal_p (TREE_OPERAND (arg0, 1),
8309                                         const_binop (PLUS_EXPR, arg2,
8310                                                      integer_one_node, 0),
8311                                         OEP_ONLY_CONST))
8312                   return pedantic_non_lvalue
8313                     (fold (build2 (MIN_EXPR, type, arg1, arg2)));
8314                 break;
8315
8316               case LE_EXPR:
8317                 /* If C1 is C2 - 1, this is min(A, C2).  */
8318                 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8319                                        OEP_ONLY_CONST)
8320                     && operand_equal_p (TREE_OPERAND (arg0, 1),
8321                                         const_binop (MINUS_EXPR, arg2,
8322                                                      integer_one_node, 0),
8323                                         OEP_ONLY_CONST))
8324                   return pedantic_non_lvalue
8325                     (fold (build2 (MIN_EXPR, type, arg1, arg2)));
8326                 break;
8327
8328               case GT_EXPR:
8329                 /* If C1 is C2 - 1, this is max(A, C2).  */
8330                 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8331                                        OEP_ONLY_CONST)
8332                     && operand_equal_p (TREE_OPERAND (arg0, 1),
8333                                         const_binop (MINUS_EXPR, arg2,
8334                                                      integer_one_node, 0),
8335                                         OEP_ONLY_CONST))
8336                   return pedantic_non_lvalue
8337                     (fold (build2 (MAX_EXPR, type, arg1, arg2)));
8338                 break;
8339
8340               case GE_EXPR:
8341                 /* If C1 is C2 + 1, this is max(A, C2).  */
8342                 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8343                                        OEP_ONLY_CONST)
8344                     && operand_equal_p (TREE_OPERAND (arg0, 1),
8345                                         const_binop (PLUS_EXPR, arg2,
8346                                                      integer_one_node, 0),
8347                                         OEP_ONLY_CONST))
8348                   return pedantic_non_lvalue
8349                     (fold (build2 (MAX_EXPR, type, arg1, arg2)));
8350                 break;
8351               case NE_EXPR:
8352                 break;
8353               default:
8354                 abort ();
8355               }
8356         }
8357
8358       /* If the second operand is simpler than the third, swap them
8359          since that produces better jump optimization results.  */
8360       if (tree_swap_operands_p (TREE_OPERAND (t, 1),
8361                                 TREE_OPERAND (t, 2), false))
8362         {
8363           /* See if this can be inverted.  If it can't, possibly because
8364              it was a floating-point inequality comparison, don't do
8365              anything.  */
8366           tem = invert_truthvalue (arg0);
8367
8368           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8369             return fold (build3 (code, type, tem,
8370                                  TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
8371         }
8372
8373       /* Convert A ? 1 : 0 to simply A.  */
8374       if (integer_onep (TREE_OPERAND (t, 1))
8375           && integer_zerop (TREE_OPERAND (t, 2))
8376           /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8377              call to fold will try to move the conversion inside
8378              a COND, which will recurse.  In that case, the COND_EXPR
8379              is probably the best choice, so leave it alone.  */
8380           && type == TREE_TYPE (arg0))
8381         return pedantic_non_lvalue (arg0);
8382
8383       /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
8384          over COND_EXPR in cases such as floating point comparisons.  */
8385       if (integer_zerop (TREE_OPERAND (t, 1))
8386           && integer_onep (TREE_OPERAND (t, 2))
8387           && truth_value_p (TREE_CODE (arg0)))
8388         return pedantic_non_lvalue (fold_convert (type,
8389                                                   invert_truthvalue (arg0)));
8390
8391       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
8392          operation is simply A & 2.  */
8393
8394       if (integer_zerop (TREE_OPERAND (t, 2))
8395           && TREE_CODE (arg0) == NE_EXPR
8396           && integer_zerop (TREE_OPERAND (arg0, 1))
8397           && integer_pow2p (arg1)
8398           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
8399           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
8400                               arg1, OEP_ONLY_CONST))
8401         return pedantic_non_lvalue (fold_convert (type,
8402                                                   TREE_OPERAND (arg0, 0)));
8403
8404       /* Convert A ? B : 0 into A && B if A and B are truth values.  */
8405       if (integer_zerop (TREE_OPERAND (t, 2))
8406           && truth_value_p (TREE_CODE (arg0))
8407           && truth_value_p (TREE_CODE (arg1)))
8408         return pedantic_non_lvalue (fold (build2 (TRUTH_ANDIF_EXPR, type,
8409                                                   arg0, arg1)));
8410
8411       /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
8412       if (integer_onep (TREE_OPERAND (t, 2))
8413           && truth_value_p (TREE_CODE (arg0))
8414           && truth_value_p (TREE_CODE (arg1)))
8415         {
8416           /* Only perform transformation if ARG0 is easily inverted.  */
8417           tem = invert_truthvalue (arg0);
8418           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8419             return pedantic_non_lvalue (fold (build2 (TRUTH_ORIF_EXPR, type,
8420                                                       tem, arg1)));
8421         }
8422
8423       return t;
8424
8425     case COMPOUND_EXPR:
8426       /* When pedantic, a compound expression can be neither an lvalue
8427          nor an integer constant expression.  */
8428       if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
8429         return t;
8430       /* Don't let (0, 0) be null pointer constant.  */
8431       tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
8432                                  : fold_convert (type, arg1);
8433       return pedantic_non_lvalue (tem);
8434
8435     case COMPLEX_EXPR:
8436       if (wins)
8437         return build_complex (type, arg0, arg1);
8438       return t;
8439
8440     case REALPART_EXPR:
8441       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8442         return t;
8443       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8444         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8445                                  TREE_OPERAND (arg0, 1));
8446       else if (TREE_CODE (arg0) == COMPLEX_CST)
8447         return TREE_REALPART (arg0);
8448       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8449         return fold (build2 (TREE_CODE (arg0), type,
8450                              fold (build1 (REALPART_EXPR, type,
8451                                            TREE_OPERAND (arg0, 0))),
8452                              fold (build1 (REALPART_EXPR, type,
8453                                            TREE_OPERAND (arg0, 1)))));
8454       return t;
8455
8456     case IMAGPART_EXPR:
8457       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8458         return fold_convert (type, integer_zero_node);
8459       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8460         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8461                                  TREE_OPERAND (arg0, 0));
8462       else if (TREE_CODE (arg0) == COMPLEX_CST)
8463         return TREE_IMAGPART (arg0);
8464       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8465         return fold (build2 (TREE_CODE (arg0), type,
8466                              fold (build1 (IMAGPART_EXPR, type,
8467                                            TREE_OPERAND (arg0, 0))),
8468                              fold (build1 (IMAGPART_EXPR, type,
8469                                            TREE_OPERAND (arg0, 1)))));
8470       return t;
8471
8472       /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8473          appropriate.  */
8474     case CLEANUP_POINT_EXPR:
8475       if (! has_cleanups (arg0))
8476         return TREE_OPERAND (t, 0);
8477
8478       {
8479         enum tree_code code0 = TREE_CODE (arg0);
8480         int kind0 = TREE_CODE_CLASS (code0);
8481         tree arg00 = TREE_OPERAND (arg0, 0);
8482         tree arg01;
8483
8484         if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
8485           return fold (build1 (code0, type,
8486                                fold (build1 (CLEANUP_POINT_EXPR,
8487                                              TREE_TYPE (arg00), arg00))));
8488
8489         if (kind0 == '<' || kind0 == '2'
8490             || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
8491             || code0 == TRUTH_AND_EXPR   || code0 == TRUTH_OR_EXPR
8492             || code0 == TRUTH_XOR_EXPR)
8493           {
8494             arg01 = TREE_OPERAND (arg0, 1);
8495
8496             if (TREE_CONSTANT (arg00)
8497                 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
8498                     && ! has_cleanups (arg00)))
8499               return fold (build2 (code0, type, arg00,
8500                                    fold (build1 (CLEANUP_POINT_EXPR,
8501                                                  TREE_TYPE (arg01), arg01))));
8502
8503             if (TREE_CONSTANT (arg01))
8504               return fold (build2 (code0, type,
8505                                    fold (build1 (CLEANUP_POINT_EXPR,
8506                                                  TREE_TYPE (arg00), arg00)),
8507                                    arg01));
8508           }
8509
8510         return t;
8511       }
8512
8513     case CALL_EXPR:
8514       /* Check for a built-in function.  */
8515       if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
8516           && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
8517               == FUNCTION_DECL)
8518           && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
8519         {
8520           tree tmp = fold_builtin (t);
8521           if (tmp)
8522             return tmp;
8523         }
8524       return t;
8525
8526     default:
8527       return t;
8528     } /* switch (code) */
8529 }
8530
8531 #ifdef ENABLE_FOLD_CHECKING
8532 #undef fold
8533
8534 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
8535 static void fold_check_failed (tree, tree);
8536 void print_fold_checksum (tree);
8537
8538 /* When --enable-checking=fold, compute a digest of expr before
8539    and after actual fold call to see if fold did not accidentally
8540    change original expr.  */
8541
8542 tree
8543 fold (tree expr)
8544 {
8545   tree ret;
8546   struct md5_ctx ctx;
8547   unsigned char checksum_before[16], checksum_after[16];
8548   htab_t ht;
8549
8550   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8551   md5_init_ctx (&ctx);
8552   fold_checksum_tree (expr, &ctx, ht);
8553   md5_finish_ctx (&ctx, checksum_before);
8554   htab_empty (ht);
8555
8556   ret = fold_1 (expr);
8557
8558   md5_init_ctx (&ctx);
8559   fold_checksum_tree (expr, &ctx, ht);
8560   md5_finish_ctx (&ctx, checksum_after);
8561   htab_delete (ht);
8562
8563   if (memcmp (checksum_before, checksum_after, 16))
8564     fold_check_failed (expr, ret);
8565
8566   return ret;
8567 }
8568
8569 void
8570 print_fold_checksum (tree expr)
8571 {
8572   struct md5_ctx ctx;
8573   unsigned char checksum[16], cnt;
8574   htab_t ht;
8575
8576   ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8577   md5_init_ctx (&ctx);
8578   fold_checksum_tree (expr, &ctx, ht);
8579   md5_finish_ctx (&ctx, checksum);
8580   htab_delete (ht);
8581   for (cnt = 0; cnt < 16; ++cnt)
8582     fprintf (stderr, "%02x", checksum[cnt]);
8583   putc ('\n', stderr);
8584 }
8585
8586 static void
8587 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
8588 {
8589   internal_error ("fold check: original tree changed by fold");
8590 }
8591
8592 static void
8593 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
8594 {
8595   void **slot;
8596   enum tree_code code;
8597   char buf[sizeof (struct tree_decl)];
8598   int i, len;
8599
8600   if (sizeof (struct tree_exp) + 5 * sizeof (tree)
8601       > sizeof (struct tree_decl)
8602       || sizeof (struct tree_type) > sizeof (struct tree_decl))
8603     abort ();
8604   if (expr == NULL)
8605     return;
8606   slot = htab_find_slot (ht, expr, INSERT);
8607   if (*slot != NULL)
8608     return;
8609   *slot = expr;
8610   code = TREE_CODE (expr);
8611   if (code == SAVE_EXPR && SAVE_EXPR_NOPLACEHOLDER (expr))
8612     {
8613       /* Allow SAVE_EXPR_NOPLACEHOLDER flag to be modified.  */
8614       memcpy (buf, expr, tree_size (expr));
8615       expr = (tree) buf;
8616       SAVE_EXPR_NOPLACEHOLDER (expr) = 0;
8617     }
8618   else if (TREE_CODE_CLASS (code) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr))
8619     {
8620       /* Allow DECL_ASSEMBLER_NAME to be modified.  */
8621       memcpy (buf, expr, tree_size (expr));
8622       expr = (tree) buf;
8623       SET_DECL_ASSEMBLER_NAME (expr, NULL);
8624     }
8625   else if (TREE_CODE_CLASS (code) == 't'
8626            && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)))
8627     {
8628       /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified.  */
8629       memcpy (buf, expr, tree_size (expr));
8630       expr = (tree) buf;
8631       TYPE_POINTER_TO (expr) = NULL;
8632       TYPE_REFERENCE_TO (expr) = NULL;
8633     }
8634   md5_process_bytes (expr, tree_size (expr), ctx);
8635   fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
8636   if (TREE_CODE_CLASS (code) != 't' && TREE_CODE_CLASS (code) != 'd')
8637     fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
8638   len = TREE_CODE_LENGTH (code);
8639   switch (TREE_CODE_CLASS (code))
8640     {
8641     case 'c':
8642       switch (code)
8643         {
8644         case STRING_CST:
8645           md5_process_bytes (TREE_STRING_POINTER (expr),
8646                              TREE_STRING_LENGTH (expr), ctx);
8647           break;
8648         case COMPLEX_CST:
8649           fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
8650           fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
8651           break;
8652         case VECTOR_CST:
8653           fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
8654           break;
8655         default:
8656           break;
8657         }
8658       break;
8659     case 'x':
8660       switch (code)
8661         {
8662         case TREE_LIST:
8663           fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
8664           fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
8665           break;
8666         case TREE_VEC:
8667           for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
8668             fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
8669           break;
8670         default:
8671           break;
8672         }
8673       break;
8674     case 'e':
8675       switch (code)
8676         {
8677         case SAVE_EXPR: len = 2; break;
8678         case GOTO_SUBROUTINE_EXPR: len = 0; break;
8679         case RTL_EXPR: len = 0; break;
8680         case WITH_CLEANUP_EXPR: len = 2; break;
8681         default: break;
8682         }
8683       /* Fall through.  */
8684     case 'r':
8685     case '<':
8686     case '1':
8687     case '2':
8688     case 's':
8689       for (i = 0; i < len; ++i)
8690         fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
8691       break;
8692     case 'd':
8693       fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
8694       fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
8695       fold_checksum_tree (DECL_NAME (expr), ctx, ht);
8696       fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
8697       fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
8698       fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
8699       fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
8700       fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
8701       fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
8702       fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
8703       fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
8704       break;
8705     case 't':
8706       fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
8707       fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
8708       fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
8709       fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
8710       fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
8711       fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
8712       fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
8713       fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
8714       fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
8715       fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
8716       break;
8717     default:
8718       break;
8719     }
8720 }
8721
8722 #endif
8723
8724 /* Perform constant folding and related simplification of initializer
8725    expression EXPR.  This behaves identically to "fold" but ignores
8726    potential run-time traps and exceptions that fold must preserve.  */
8727
8728 tree
8729 fold_initializer (tree expr)
8730 {
8731   int saved_signaling_nans = flag_signaling_nans;
8732   int saved_trapping_math = flag_trapping_math;
8733   int saved_trapv = flag_trapv;
8734   tree result;
8735
8736   flag_signaling_nans = 0;
8737   flag_trapping_math = 0;
8738   flag_trapv = 0;
8739
8740   result = fold (expr);
8741
8742   flag_signaling_nans = saved_signaling_nans;
8743   flag_trapping_math = saved_trapping_math;
8744   flag_trapv = saved_trapv;
8745
8746   return result;
8747 }
8748
8749 /* Determine if first argument is a multiple of second argument.  Return 0 if
8750    it is not, or we cannot easily determined it to be.
8751
8752    An example of the sort of thing we care about (at this point; this routine
8753    could surely be made more general, and expanded to do what the *_DIV_EXPR's
8754    fold cases do now) is discovering that
8755
8756      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8757
8758    is a multiple of
8759
8760      SAVE_EXPR (J * 8)
8761
8762    when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
8763
8764    This code also handles discovering that
8765
8766      SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8767
8768    is a multiple of 8 so we don't have to worry about dealing with a
8769    possible remainder.
8770
8771    Note that we *look* inside a SAVE_EXPR only to determine how it was
8772    calculated; it is not safe for fold to do much of anything else with the
8773    internals of a SAVE_EXPR, since it cannot know when it will be evaluated
8774    at run time.  For example, the latter example above *cannot* be implemented
8775    as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
8776    evaluation time of the original SAVE_EXPR is not necessarily the same at
8777    the time the new expression is evaluated.  The only optimization of this
8778    sort that would be valid is changing
8779
8780      SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
8781
8782    divided by 8 to
8783
8784      SAVE_EXPR (I) * SAVE_EXPR (J)
8785
8786    (where the same SAVE_EXPR (J) is used in the original and the
8787    transformed version).  */
8788
8789 static int
8790 multiple_of_p (tree type, tree top, tree bottom)
8791 {
8792   if (operand_equal_p (top, bottom, 0))
8793     return 1;
8794
8795   if (TREE_CODE (type) != INTEGER_TYPE)
8796     return 0;
8797
8798   switch (TREE_CODE (top))
8799     {
8800     case MULT_EXPR:
8801       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8802               || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8803
8804     case PLUS_EXPR:
8805     case MINUS_EXPR:
8806       return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8807               && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8808
8809     case LSHIFT_EXPR:
8810       if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
8811         {
8812           tree op1, t1;
8813
8814           op1 = TREE_OPERAND (top, 1);
8815           /* const_binop may not detect overflow correctly,
8816              so check for it explicitly here.  */
8817           if (TYPE_PRECISION (TREE_TYPE (size_one_node))
8818               > TREE_INT_CST_LOW (op1)
8819               && TREE_INT_CST_HIGH (op1) == 0
8820               && 0 != (t1 = fold_convert (type,
8821                                           const_binop (LSHIFT_EXPR,
8822                                                        size_one_node,
8823                                                        op1, 0)))
8824               && ! TREE_OVERFLOW (t1))
8825             return multiple_of_p (type, t1, bottom);
8826         }
8827       return 0;
8828
8829     case NOP_EXPR:
8830       /* Can't handle conversions from non-integral or wider integral type.  */
8831       if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
8832           || (TYPE_PRECISION (type)
8833               < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
8834         return 0;
8835
8836       /* .. fall through ...  */
8837
8838     case SAVE_EXPR:
8839       return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
8840
8841     case INTEGER_CST:
8842       if (TREE_CODE (bottom) != INTEGER_CST
8843           || (TYPE_UNSIGNED (type)
8844               && (tree_int_cst_sgn (top) < 0
8845                   || tree_int_cst_sgn (bottom) < 0)))
8846         return 0;
8847       return integer_zerop (const_binop (TRUNC_MOD_EXPR,
8848                                          top, bottom, 0));
8849
8850     default:
8851       return 0;
8852     }
8853 }
8854
8855 /* Return true if `t' is known to be non-negative.  */
8856
8857 int
8858 tree_expr_nonnegative_p (tree t)
8859 {
8860   switch (TREE_CODE (t))
8861     {
8862     case ABS_EXPR:
8863       return 1;
8864
8865     case INTEGER_CST:
8866       return tree_int_cst_sgn (t) >= 0;
8867
8868     case REAL_CST:
8869       return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
8870
8871     case PLUS_EXPR:
8872       if (FLOAT_TYPE_P (TREE_TYPE (t)))
8873         return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8874                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8875
8876       /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
8877          both unsigned and at least 2 bits shorter than the result.  */
8878       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8879           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8880           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8881         {
8882           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8883           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8884           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8885               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8886             {
8887               unsigned int prec = MAX (TYPE_PRECISION (inner1),
8888                                        TYPE_PRECISION (inner2)) + 1;
8889               return prec < TYPE_PRECISION (TREE_TYPE (t));
8890             }
8891         }
8892       break;
8893
8894     case MULT_EXPR:
8895       if (FLOAT_TYPE_P (TREE_TYPE (t)))
8896         {
8897           /* x * x for floating point x is always non-negative.  */
8898           if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
8899             return 1;
8900           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8901                  && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8902         }
8903
8904       /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
8905          both unsigned and their total bits is shorter than the result.  */
8906       if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8907           && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8908           && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8909         {
8910           tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8911           tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8912           if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8913               && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8914             return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
8915                    < TYPE_PRECISION (TREE_TYPE (t));
8916         }
8917       return 0;
8918
8919     case TRUNC_DIV_EXPR:
8920     case CEIL_DIV_EXPR:
8921     case FLOOR_DIV_EXPR:
8922     case ROUND_DIV_EXPR:
8923       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8924              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8925
8926     case TRUNC_MOD_EXPR:
8927     case CEIL_MOD_EXPR:
8928     case FLOOR_MOD_EXPR:
8929     case ROUND_MOD_EXPR:
8930       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8931
8932     case RDIV_EXPR:
8933       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8934              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8935
8936     case BIT_AND_EXPR:
8937       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8938              || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8939     case BIT_IOR_EXPR:
8940     case BIT_XOR_EXPR:
8941       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8942              && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8943
8944     case NOP_EXPR:
8945       {
8946         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
8947         tree outer_type = TREE_TYPE (t);
8948
8949         if (TREE_CODE (outer_type) == REAL_TYPE)
8950           {
8951             if (TREE_CODE (inner_type) == REAL_TYPE)
8952               return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8953             if (TREE_CODE (inner_type) == INTEGER_TYPE)
8954               {
8955                 if (TYPE_UNSIGNED (inner_type))
8956                   return 1;
8957                 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8958               }
8959           }
8960         else if (TREE_CODE (outer_type) == INTEGER_TYPE)
8961           {
8962             if (TREE_CODE (inner_type) == REAL_TYPE)
8963               return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
8964             if (TREE_CODE (inner_type) == INTEGER_TYPE)
8965               return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
8966                       && TYPE_UNSIGNED (inner_type);
8967           }
8968       }
8969       break;
8970
8971     case COND_EXPR:
8972       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8973         && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
8974     case COMPOUND_EXPR:
8975       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8976     case MIN_EXPR:
8977       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8978         && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8979     case MAX_EXPR:
8980       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8981         || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8982     case MODIFY_EXPR:
8983       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8984     case BIND_EXPR:
8985       return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8986     case SAVE_EXPR:
8987       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8988     case NON_LVALUE_EXPR:
8989       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8990     case FLOAT_EXPR:
8991       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8992     case RTL_EXPR:
8993       return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
8994
8995     case CALL_EXPR:
8996       {
8997         tree fndecl = get_callee_fndecl (t);
8998         tree arglist = TREE_OPERAND (t, 1);
8999         if (fndecl
9000             && DECL_BUILT_IN (fndecl)
9001             && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
9002           switch (DECL_FUNCTION_CODE (fndecl))
9003             {
9004 #define CASE_BUILTIN_F(BUILT_IN_FN) \
9005   case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
9006 #define CASE_BUILTIN_I(BUILT_IN_FN) \
9007   case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
9008
9009             CASE_BUILTIN_F (BUILT_IN_ACOS)
9010             CASE_BUILTIN_F (BUILT_IN_ACOSH)
9011             CASE_BUILTIN_F (BUILT_IN_CABS)
9012             CASE_BUILTIN_F (BUILT_IN_COSH)
9013             CASE_BUILTIN_F (BUILT_IN_ERFC)
9014             CASE_BUILTIN_F (BUILT_IN_EXP)
9015             CASE_BUILTIN_F (BUILT_IN_EXP10)
9016             CASE_BUILTIN_F (BUILT_IN_EXP2)
9017             CASE_BUILTIN_F (BUILT_IN_FABS)
9018             CASE_BUILTIN_F (BUILT_IN_FDIM)
9019             CASE_BUILTIN_F (BUILT_IN_FREXP)
9020             CASE_BUILTIN_F (BUILT_IN_HYPOT)
9021             CASE_BUILTIN_F (BUILT_IN_POW10)
9022             CASE_BUILTIN_I (BUILT_IN_FFS)
9023             CASE_BUILTIN_I (BUILT_IN_PARITY)
9024             CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
9025               /* Always true.  */
9026               return 1;
9027
9028             CASE_BUILTIN_F (BUILT_IN_SQRT)
9029               /* sqrt(-0.0) is -0.0.  */
9030               if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
9031                 return 1;
9032               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9033
9034             CASE_BUILTIN_F (BUILT_IN_ASINH)
9035             CASE_BUILTIN_F (BUILT_IN_ATAN)
9036             CASE_BUILTIN_F (BUILT_IN_ATANH)
9037             CASE_BUILTIN_F (BUILT_IN_CBRT)
9038             CASE_BUILTIN_F (BUILT_IN_CEIL)
9039             CASE_BUILTIN_F (BUILT_IN_ERF)
9040             CASE_BUILTIN_F (BUILT_IN_EXPM1)
9041             CASE_BUILTIN_F (BUILT_IN_FLOOR)
9042             CASE_BUILTIN_F (BUILT_IN_FMOD)
9043             CASE_BUILTIN_F (BUILT_IN_LDEXP)
9044             CASE_BUILTIN_F (BUILT_IN_LLRINT)
9045             CASE_BUILTIN_F (BUILT_IN_LLROUND)
9046             CASE_BUILTIN_F (BUILT_IN_LRINT)
9047             CASE_BUILTIN_F (BUILT_IN_LROUND)
9048             CASE_BUILTIN_F (BUILT_IN_MODF)
9049             CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
9050             CASE_BUILTIN_F (BUILT_IN_POW)
9051             CASE_BUILTIN_F (BUILT_IN_RINT)
9052             CASE_BUILTIN_F (BUILT_IN_ROUND)
9053             CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
9054             CASE_BUILTIN_F (BUILT_IN_SINH)
9055             CASE_BUILTIN_F (BUILT_IN_TANH)
9056             CASE_BUILTIN_F (BUILT_IN_TRUNC)
9057               /* True if the 1st argument is nonnegative.  */
9058               return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9059
9060             CASE_BUILTIN_F (BUILT_IN_FMAX)
9061               /* True if the 1st OR 2nd arguments are nonnegative.  */
9062               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9063                 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9064
9065             CASE_BUILTIN_F (BUILT_IN_FMIN)
9066               /* True if the 1st AND 2nd arguments are nonnegative.  */
9067               return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9068                 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9069
9070             CASE_BUILTIN_F (BUILT_IN_COPYSIGN)
9071               /* True if the 2nd argument is nonnegative.  */
9072               return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9073
9074             default:
9075               break;
9076 #undef CASE_BUILTIN_F
9077 #undef CASE_BUILTIN_I
9078             }
9079       }
9080
9081       /* ... fall through ...  */
9082
9083     default:
9084       if (truth_value_p (TREE_CODE (t)))
9085         /* Truth values evaluate to 0 or 1, which is nonnegative.  */
9086         return 1;
9087     }
9088
9089   /* We don't know sign of `t', so be conservative and return false.  */
9090   return 0;
9091 }
9092
9093 /* Return true when T is an address and is known to be nonzero.
9094    For floating point we further ensure that T is not denormal.
9095    Similar logic is present in nonzero_address in rtlanal.h  */
9096
9097 static bool
9098 tree_expr_nonzero_p (tree t)
9099 {
9100   tree type = TREE_TYPE (t);
9101
9102   /* Doing something useful for floating point would need more work.  */
9103   if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9104     return false;
9105
9106   switch (TREE_CODE (t))
9107     {
9108     case ABS_EXPR:
9109       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9110         return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9111
9112     case INTEGER_CST:
9113       return !integer_zerop (t);
9114
9115     case PLUS_EXPR:
9116       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9117         {
9118           /* With the presence of negative values it is hard
9119              to say something.  */
9120           if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9121               || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9122             return false;
9123           /* One of operands must be positive and the other non-negative.  */
9124           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9125                   || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9126         }
9127       break;
9128
9129     case MULT_EXPR:
9130       if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9131         {
9132           return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9133                   && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9134         }
9135       break;
9136
9137     case NOP_EXPR:
9138       {
9139         tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
9140         tree outer_type = TREE_TYPE (t);
9141
9142         return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
9143                 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
9144       }
9145       break;
9146
9147    case ADDR_EXPR:
9148       /* Weak declarations may link to NULL.  */
9149       if (DECL_P (TREE_OPERAND (t, 0)))
9150         return !DECL_WEAK (TREE_OPERAND (t, 0));
9151       /* Constants and all other cases are never weak.  */
9152       return true;
9153
9154     case COND_EXPR:
9155       return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9156               && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
9157
9158     case MIN_EXPR:
9159       return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9160               && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9161
9162     case MAX_EXPR:
9163       if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
9164         {
9165           /* When both operands are nonzero, then MAX must be too.  */
9166           if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
9167             return true;
9168
9169           /* MAX where operand 0 is positive is positive.  */
9170           return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9171         }
9172       /* MAX where operand 1 is positive is positive.  */
9173       else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9174                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9175         return true;
9176       break;
9177
9178     case COMPOUND_EXPR:
9179     case MODIFY_EXPR:
9180     case BIND_EXPR:
9181       return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
9182
9183     case SAVE_EXPR:
9184     case NON_LVALUE_EXPR:
9185       return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9186
9187     case BIT_IOR_EXPR:
9188       return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9189              || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9190
9191     default:
9192       break;
9193     }
9194   return false;
9195 }
9196
9197 /* Return true if `r' is known to be non-negative.
9198    Only handles constants at the moment.  */
9199
9200 int
9201 rtl_expr_nonnegative_p (rtx r)
9202 {
9203   switch (GET_CODE (r))
9204     {
9205     case CONST_INT:
9206       return INTVAL (r) >= 0;
9207
9208     case CONST_DOUBLE:
9209       if (GET_MODE (r) == VOIDmode)
9210         return CONST_DOUBLE_HIGH (r) >= 0;
9211       return 0;
9212
9213     case CONST_VECTOR:
9214       {
9215         int units, i;
9216         rtx elt;
9217
9218         units = CONST_VECTOR_NUNITS (r);
9219
9220         for (i = 0; i < units; ++i)
9221           {
9222             elt = CONST_VECTOR_ELT (r, i);
9223             if (!rtl_expr_nonnegative_p (elt))
9224               return 0;
9225           }
9226
9227         return 1;
9228       }
9229
9230     case SYMBOL_REF:
9231     case LABEL_REF:
9232       /* These are always nonnegative.  */
9233       return 1;
9234
9235     default:
9236       return 0;
9237     }
9238 }
9239
9240
9241 /* See if we are applying CODE, a relational to the highest or lowest
9242    possible integer of TYPE.  If so, then the result is a compile
9243    time constant.  */
9244
9245 static tree
9246 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
9247                        tree *op1_p)
9248 {
9249   tree op0 = *op0_p;
9250   tree op1 = *op1_p;
9251   enum tree_code code = *code_p;
9252   int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
9253
9254   if (TREE_CODE (op1) == INTEGER_CST
9255       && ! TREE_CONSTANT_OVERFLOW (op1)
9256       && width <= HOST_BITS_PER_WIDE_INT
9257       && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
9258           || POINTER_TYPE_P (TREE_TYPE (op1))))
9259     {
9260       unsigned HOST_WIDE_INT signed_max;
9261       unsigned HOST_WIDE_INT max, min;
9262
9263       signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
9264
9265       if (TYPE_UNSIGNED (TREE_TYPE (op1)))
9266         {
9267           max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9268           min = 0;
9269         }
9270       else
9271         {
9272           max = signed_max;
9273           min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9274         }
9275
9276       if (TREE_INT_CST_HIGH (op1) == 0
9277           && TREE_INT_CST_LOW (op1) == max)
9278         switch (code)
9279           {
9280           case GT_EXPR:
9281             return omit_one_operand (type, integer_zero_node, op0);
9282
9283           case GE_EXPR:
9284             *code_p = EQ_EXPR;
9285             break;
9286           case LE_EXPR:
9287             return omit_one_operand (type, integer_one_node, op0);
9288
9289           case LT_EXPR:
9290             *code_p = NE_EXPR;
9291             break;
9292
9293           /* The GE_EXPR and LT_EXPR cases above are not normally
9294              reached because of  previous transformations.  */
9295
9296           default:
9297             break;
9298           }
9299       else if (TREE_INT_CST_HIGH (op1) == 0
9300                && TREE_INT_CST_LOW (op1) == max - 1)
9301         switch (code)
9302           {
9303           case GT_EXPR:
9304             *code_p = EQ_EXPR;
9305             *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9306             break;
9307           case LE_EXPR:
9308             *code_p = NE_EXPR;
9309             *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9310             break;
9311           default:
9312             break;
9313           }
9314       else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9315                && TREE_INT_CST_LOW (op1) == min)
9316        switch (code)
9317           {
9318           case LT_EXPR:
9319             return omit_one_operand (type, integer_zero_node, op0);
9320
9321           case LE_EXPR:
9322             *code_p = EQ_EXPR;
9323             break;
9324
9325           case GE_EXPR:
9326             return omit_one_operand (type, integer_one_node, op0);
9327
9328           case GT_EXPR:
9329             *code_p = NE_EXPR;
9330             break;
9331
9332           default:
9333             break;
9334           }
9335       else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9336                && TREE_INT_CST_LOW (op1) == min + 1)
9337         switch (code)
9338           {
9339           case GE_EXPR:
9340             *code_p = NE_EXPR;
9341             *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9342             break;
9343           case LT_EXPR:
9344             *code_p = EQ_EXPR;
9345             *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9346             break;
9347           default:
9348             break;
9349           }
9350
9351       else if (TREE_INT_CST_HIGH (op1) == 0
9352                && TREE_INT_CST_LOW (op1) == signed_max
9353                && TYPE_UNSIGNED (TREE_TYPE (op1))
9354                /* signed_type does not work on pointer types.  */
9355                && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
9356         {
9357           /* The following case also applies to X < signed_max+1
9358              and X >= signed_max+1 because previous transformations.  */
9359           if (code == LE_EXPR || code == GT_EXPR)
9360             {
9361               tree st0, st1, exp, retval;
9362               st0 = lang_hooks.types.signed_type (TREE_TYPE (op0));
9363               st1 = lang_hooks.types.signed_type (TREE_TYPE (op1));
9364
9365               exp = build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9366                             type,
9367                             fold_convert (st0, op0),
9368                             fold_convert (st1, integer_zero_node));
9369
9370               retval
9371                 = nondestructive_fold_binary_to_constant (TREE_CODE (exp),
9372                                                           TREE_TYPE (exp),
9373                                                           TREE_OPERAND (exp, 0),
9374                                                           TREE_OPERAND (exp, 1));
9375
9376               /* If we are in gimple form, then returning EXP would create
9377                  non-gimple expressions.  Clearing it is safe and insures
9378                  we do not allow a non-gimple expression to escape.  */
9379               if (in_gimple_form)
9380                 exp = NULL;
9381
9382               return (retval ? retval : exp);
9383             }
9384         }
9385     }
9386
9387   return NULL_TREE;
9388 }
9389
9390
9391 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
9392    attempt to fold the expression to a constant without modifying TYPE,
9393    OP0 or OP1.
9394
9395    If the expression could be simplified to a constant, then return
9396    the constant.  If the expression would not be simplified to a
9397    constant, then return NULL_TREE.
9398
9399    Note this is primarily designed to be called after gimplification
9400    of the tree structures and when at least one operand is a constant.
9401    As a result of those simplifying assumptions this routine is far
9402    simpler than the generic fold routine.  */
9403
9404 tree
9405 nondestructive_fold_binary_to_constant (enum tree_code code, tree type,
9406                                         tree op0, tree op1)
9407 {
9408   int wins = 1;
9409   tree subop0;
9410   tree subop1;
9411   tree tem;
9412
9413   /* If this is a commutative operation, and ARG0 is a constant, move it
9414      to ARG1 to reduce the number of tests below.  */
9415   if (commutative_tree_code (code)
9416       && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
9417     {
9418       tem = op0;
9419       op0 = op1;
9420       op1 = tem;
9421     }
9422
9423   /* If either operand is a complex type, extract its real component.  */
9424   if (TREE_CODE (op0) == COMPLEX_CST)
9425     subop0 = TREE_REALPART (op0);
9426   else
9427     subop0 = op0;
9428
9429   if (TREE_CODE (op1) == COMPLEX_CST)
9430     subop1 = TREE_REALPART (op1);
9431   else
9432     subop1 = op1;
9433
9434   /* Note if either argument is not a real or integer constant.
9435      With a few exceptions, simplification is limited to cases
9436      where both arguments are constants.  */
9437   if ((TREE_CODE (subop0) != INTEGER_CST
9438        && TREE_CODE (subop0) != REAL_CST)
9439       || (TREE_CODE (subop1) != INTEGER_CST
9440           && TREE_CODE (subop1) != REAL_CST))
9441     wins = 0;
9442
9443   switch (code)
9444     {
9445     case PLUS_EXPR:
9446       /* (plus (address) (const_int)) is a constant.  */
9447       if (TREE_CODE (op0) == PLUS_EXPR
9448           && TREE_CODE (op1) == INTEGER_CST
9449           && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
9450               || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
9451                   && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
9452                       == ADDR_EXPR)))
9453           && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9454         {
9455           return build2 (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
9456                          const_binop (PLUS_EXPR, op1,
9457                                       TREE_OPERAND (op0, 1), 0));
9458         }
9459     case BIT_XOR_EXPR:
9460
9461     binary:
9462       if (!wins)
9463         return NULL_TREE;
9464
9465       /* Both arguments are constants.  Simplify.  */
9466       tem = const_binop (code, op0, op1, 0);
9467       if (tem != NULL_TREE)
9468         {
9469           /* The return value should always have the same type as
9470              the original expression.  */
9471           if (TREE_TYPE (tem) != type)
9472             tem = fold_convert (type, tem);
9473
9474           return tem;
9475         }
9476       return NULL_TREE;
9477       
9478     case MINUS_EXPR:
9479       /* Fold &x - &x.  This can happen from &x.foo - &x.
9480          This is unsafe for certain floats even in non-IEEE formats.
9481          In IEEE, it is unsafe because it does wrong for NaNs.
9482          Also note that operand_equal_p is always false if an
9483          operand is volatile.  */
9484       if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
9485         return fold_convert (type, integer_zero_node);
9486
9487       goto binary;
9488
9489     case MULT_EXPR:
9490     case BIT_AND_EXPR:
9491       /* Special case multiplication or bitwise AND where one argument
9492          is zero.  */
9493       if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
9494         return omit_one_operand (type, op1, op0);
9495       else
9496         if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
9497             && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
9498             && real_zerop (op1))
9499           return omit_one_operand (type, op1, op0);
9500
9501       goto binary;
9502
9503     case BIT_IOR_EXPR:
9504       /* Special case when we know the result will be all ones.  */
9505       if (integer_all_onesp (op1))
9506         return omit_one_operand (type, op1, op0);
9507
9508       goto binary;
9509         
9510     case TRUNC_DIV_EXPR:
9511     case ROUND_DIV_EXPR:
9512     case FLOOR_DIV_EXPR:
9513     case CEIL_DIV_EXPR:
9514     case EXACT_DIV_EXPR:
9515     case TRUNC_MOD_EXPR:
9516     case ROUND_MOD_EXPR:
9517     case FLOOR_MOD_EXPR:
9518     case CEIL_MOD_EXPR:
9519     case RDIV_EXPR:
9520       /* Division by zero is undefined.  */
9521       if (integer_zerop (op1))
9522         return NULL_TREE;
9523
9524       if (TREE_CODE (op1) == REAL_CST
9525           && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
9526           && real_zerop (op1))
9527         return NULL_TREE;
9528
9529       goto binary;
9530
9531     case MIN_EXPR:
9532       if (INTEGRAL_TYPE_P (type)
9533           && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9534         return omit_one_operand (type, op1, op0);
9535
9536       goto binary;
9537
9538     case MAX_EXPR:
9539       if (INTEGRAL_TYPE_P (type)
9540           && TYPE_MAX_VALUE (type)
9541           && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9542         return omit_one_operand (type, op1, op0);
9543
9544       goto binary;
9545
9546     case RSHIFT_EXPR:
9547       /* Optimize -1 >> x for arithmetic right shifts.  */
9548       if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
9549         return omit_one_operand (type, op0, op1);
9550       /* ... fall through ...  */
9551
9552     case LSHIFT_EXPR:
9553       if (integer_zerop (op0))
9554         return omit_one_operand (type, op0, op1);
9555
9556       /* Since negative shift count is not well-defined, don't
9557          try to compute it in the compiler.  */
9558       if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
9559         return NULL_TREE;
9560
9561       goto binary;
9562
9563     case LROTATE_EXPR:
9564     case RROTATE_EXPR:
9565       /* -1 rotated either direction by any amount is still -1.  */
9566       if (integer_all_onesp (op0))
9567         return omit_one_operand (type, op0, op1);
9568
9569       /* 0 rotated either direction by any amount is still zero.  */
9570       if (integer_zerop (op0))
9571         return omit_one_operand (type, op0, op1);
9572
9573       goto binary;
9574
9575     case COMPLEX_EXPR:
9576       if (wins)
9577         return build_complex (type, op0, op1);
9578       return NULL_TREE;
9579
9580     case LT_EXPR:
9581     case LE_EXPR:
9582     case GT_EXPR:
9583     case GE_EXPR:
9584     case EQ_EXPR:
9585     case NE_EXPR:
9586       /* If one arg is a real or integer constant, put it last.  */
9587       if ((TREE_CODE (op0) == INTEGER_CST
9588            && TREE_CODE (op1) != INTEGER_CST)
9589           || (TREE_CODE (op0) == REAL_CST
9590               && TREE_CODE (op0) != REAL_CST))
9591         {
9592           tree temp;
9593
9594           temp = op0;
9595           op0 = op1;
9596           op1 = temp;
9597           code = swap_tree_comparison (code);
9598         }
9599
9600       /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
9601          This transformation affects the cases which are handled in later
9602          optimizations involving comparisons with non-negative constants.  */
9603       if (TREE_CODE (op1) == INTEGER_CST
9604           && TREE_CODE (op0) != INTEGER_CST
9605           && tree_int_cst_sgn (op1) > 0)
9606         {
9607           switch (code)
9608             {
9609             case GE_EXPR:
9610               code = GT_EXPR;
9611               op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9612               break;
9613
9614             case LT_EXPR:
9615               code = LE_EXPR;
9616               op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9617               break;
9618
9619             default:
9620               break;
9621             }
9622         }
9623
9624       tem = fold_relational_hi_lo (&code, type, &op0, &op1);
9625       if (tem)
9626         return tem;
9627
9628       if (!wins)
9629         return NULL_TREE;
9630
9631       return fold_relational_const (code, type, op0, op1);
9632
9633     case RANGE_EXPR:
9634       /* This could probably be handled.  */
9635       return NULL_TREE;
9636
9637     case TRUTH_AND_EXPR:
9638       /* If second arg is constant zero, result is zero, but first arg
9639          must be evaluated.  */
9640       if (integer_zerop (op1))
9641         return omit_one_operand (type, op1, op0);
9642       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9643          case will be handled here.  */
9644       if (integer_zerop (op0))
9645         return omit_one_operand (type, op0, op1);
9646       if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9647         return constant_boolean_node (true, type);
9648       return NULL_TREE;
9649
9650     case TRUTH_OR_EXPR:
9651       /* If second arg is constant true, result is true, but we must
9652          evaluate first arg.  */
9653       if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
9654         return omit_one_operand (type, op1, op0);
9655       /* Likewise for first arg, but note this only occurs here for
9656          TRUTH_OR_EXPR.  */
9657       if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
9658         return omit_one_operand (type, op0, op1);
9659       if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9660         return constant_boolean_node (false, type);
9661       return NULL_TREE;
9662
9663     case TRUTH_XOR_EXPR:
9664       if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9665         {
9666           int x = ! integer_zerop (op0) ^ ! integer_zerop (op1);
9667           return constant_boolean_node (x, type);
9668         }
9669       return NULL_TREE;
9670
9671     default:
9672       return NULL_TREE;
9673     }
9674 }
9675
9676 /* Given the components of a unary expression CODE, TYPE and OP0,
9677    attempt to fold the expression to a constant without modifying
9678    TYPE or OP0. 
9679
9680    If the expression could be simplified to a constant, then return
9681    the constant.  If the expression would not be simplified to a
9682    constant, then return NULL_TREE.
9683
9684    Note this is primarily designed to be called after gimplification
9685    of the tree structures and when op0 is a constant.  As a result
9686    of those simplifying assumptions this routine is far simpler than
9687    the generic fold routine.  */
9688
9689 tree
9690 nondestructive_fold_unary_to_constant (enum tree_code code, tree type,
9691                                        tree op0)
9692 {
9693   /* Make sure we have a suitable constant argument.  */
9694   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
9695     {
9696       tree subop;
9697
9698       if (TREE_CODE (op0) == COMPLEX_CST)
9699         subop = TREE_REALPART (op0);
9700       else
9701         subop = op0;
9702
9703       if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
9704         return NULL_TREE;
9705     }
9706
9707   switch (code)
9708     {
9709     case NOP_EXPR:
9710     case FLOAT_EXPR:
9711     case CONVERT_EXPR:
9712     case FIX_TRUNC_EXPR:
9713     case FIX_FLOOR_EXPR:
9714     case FIX_CEIL_EXPR:
9715       return fold_convert_const (code, type, op0);
9716
9717     case NEGATE_EXPR:
9718       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9719         return fold_negate_const (op0, type);
9720       else
9721         return NULL_TREE;
9722
9723     case ABS_EXPR:
9724       if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9725         return fold_abs_const (op0, type);
9726       else
9727         return NULL_TREE;
9728
9729     case BIT_NOT_EXPR:
9730       if (TREE_CODE (op0) == INTEGER_CST)
9731         return fold_not_const (op0, type);
9732       else
9733         return NULL_TREE;
9734
9735     case REALPART_EXPR:
9736       if (TREE_CODE (op0) == COMPLEX_CST)
9737         return TREE_REALPART (op0);
9738       else
9739         return NULL_TREE;
9740
9741     case IMAGPART_EXPR:
9742       if (TREE_CODE (op0) == COMPLEX_CST)
9743         return TREE_IMAGPART (op0);
9744       else
9745         return NULL_TREE;
9746
9747     case CONJ_EXPR:
9748       if (TREE_CODE (op0) == COMPLEX_CST
9749           && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
9750         return build_complex (type, TREE_REALPART (op0),
9751                               negate_expr (TREE_IMAGPART (op0)));
9752       return NULL_TREE;
9753
9754     default:
9755       return NULL_TREE;
9756     }
9757 }
9758
9759 /* If EXP represents referencing an element in a constant string
9760    (either via pointer arithmetic or array indexing), return the
9761    tree representing the value accessed, otherwise return NULL.  */
9762
9763 tree
9764 fold_read_from_constant_string (tree exp)
9765 {
9766   if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
9767     {
9768       tree exp1 = TREE_OPERAND (exp, 0);
9769       tree index;
9770       tree string;
9771
9772       if (TREE_CODE (exp) == INDIRECT_REF)
9773         {
9774           string = string_constant (exp1, &index);
9775         }
9776       else
9777         {
9778           tree domain = TYPE_DOMAIN (TREE_TYPE (exp1));
9779           tree low_bound = domain ? TYPE_MIN_VALUE (domain) : integer_zero_node;
9780           index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
9781           
9782           /* Optimize the special-case of a zero lower bound.
9783
9784              We convert the low_bound to sizetype to avoid some problems
9785              with constant folding.  (E.g. suppose the lower bound is 1,
9786              and its mode is QI.  Without the conversion,l (ARRAY
9787              +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9788              +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
9789           if (! integer_zerop (low_bound))
9790             index = size_diffop (index, fold_convert (sizetype, low_bound));
9791
9792           string = exp1;
9793         }
9794
9795       if (string
9796           && TREE_CODE (string) == STRING_CST
9797           && TREE_CODE (index) == INTEGER_CST
9798           && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
9799           && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
9800               == MODE_INT)
9801           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
9802         return fold_convert (TREE_TYPE (exp),
9803                              build_int_2 ((TREE_STRING_POINTER (string)
9804                                           [TREE_INT_CST_LOW (index)]), 0));
9805     }
9806   return NULL;
9807 }
9808
9809 /* Return the tree for neg (ARG0) when ARG0 is known to be either
9810    an integer constant or real constant.
9811
9812    TYPE is the type of the result.  */
9813
9814 static tree
9815 fold_negate_const (tree arg0, tree type)
9816 {
9817   tree t = NULL_TREE;
9818
9819   if (TREE_CODE (arg0) == INTEGER_CST)
9820     {
9821       unsigned HOST_WIDE_INT low;
9822       HOST_WIDE_INT high;
9823       int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9824                                  TREE_INT_CST_HIGH (arg0),
9825                                  &low, &high);
9826       t = build_int_2 (low, high);
9827       TREE_TYPE (t) = type;
9828       TREE_OVERFLOW (t)
9829         = (TREE_OVERFLOW (arg0)
9830            | force_fit_type (t, overflow && !TYPE_UNSIGNED (type)));
9831       TREE_CONSTANT_OVERFLOW (t)
9832         = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9833     }
9834   else if (TREE_CODE (arg0) == REAL_CST)
9835     t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9836 #ifdef ENABLE_CHECKING
9837   else
9838     abort ();
9839 #endif
9840     
9841   return t;
9842 }
9843
9844 /* Return the tree for abs (ARG0) when ARG0 is known to be either
9845    an integer constant or real constant.
9846
9847    TYPE is the type of the result.  */
9848
9849 static tree
9850 fold_abs_const (tree arg0, tree type)
9851 {
9852   tree t = NULL_TREE;
9853
9854   if (TREE_CODE (arg0) == INTEGER_CST)
9855     {
9856       /* If the value is unsigned, then the absolute value is
9857          the same as the ordinary value.  */
9858       if (TYPE_UNSIGNED (type))
9859         return arg0;
9860       /* Similarly, if the value is non-negative.  */
9861       else if (INT_CST_LT (integer_minus_one_node, arg0))
9862         return arg0;
9863       /* If the value is negative, then the absolute value is
9864          its negation.  */
9865       else
9866         {
9867           unsigned HOST_WIDE_INT low;
9868           HOST_WIDE_INT high;
9869           int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9870                                      TREE_INT_CST_HIGH (arg0),
9871                                      &low, &high);
9872           t = build_int_2 (low, high);
9873           TREE_TYPE (t) = type;
9874           TREE_OVERFLOW (t)
9875             = (TREE_OVERFLOW (arg0)
9876                | force_fit_type (t, overflow));
9877           TREE_CONSTANT_OVERFLOW (t)
9878             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9879           return t;
9880         }
9881     }
9882   else if (TREE_CODE (arg0) == REAL_CST)
9883     {
9884       if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
9885         return build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9886       else
9887         return arg0;
9888     }
9889 #ifdef ENABLE_CHECKING
9890   else
9891     abort ();
9892 #endif
9893     
9894   return t;
9895 }
9896
9897 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
9898    constant.  TYPE is the type of the result.  */
9899
9900 static tree
9901 fold_not_const (tree arg0, tree type)
9902 {
9903   tree t = NULL_TREE;
9904
9905   if (TREE_CODE (arg0) == INTEGER_CST)
9906     {
9907       t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
9908                        ~ TREE_INT_CST_HIGH (arg0));
9909       TREE_TYPE (t) = type;
9910       force_fit_type (t, 0);
9911       TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
9912       TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
9913     }
9914 #ifdef ENABLE_CHECKING
9915   else
9916     abort ();
9917 #endif
9918     
9919   return t;
9920 }
9921
9922 /* Given CODE, a relational operator, the target type, TYPE and two
9923    constant operands OP0 and OP1, return the result of the
9924    relational operation.  If the result is not a compile time
9925    constant, then return NULL_TREE.  */
9926
9927 static tree
9928 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
9929 {
9930   tree tem;
9931   int invert;
9932
9933   /* From here on, the only cases we handle are when the result is
9934      known to be a constant.
9935
9936      To compute GT, swap the arguments and do LT.
9937      To compute GE, do LT and invert the result.
9938      To compute LE, swap the arguments, do LT and invert the result.
9939      To compute NE, do EQ and invert the result.
9940
9941      Therefore, the code below must handle only EQ and LT.  */
9942
9943   if (code == LE_EXPR || code == GT_EXPR)
9944     {
9945       tem = op0, op0 = op1, op1 = tem;
9946       code = swap_tree_comparison (code);
9947     }
9948
9949   /* Note that it is safe to invert for real values here because we
9950      will check below in the one case that it matters.  */
9951
9952   tem = NULL_TREE;
9953   invert = 0;
9954   if (code == NE_EXPR || code == GE_EXPR)
9955     {
9956       invert = 1;
9957       code = invert_tree_comparison (code, false);
9958     }
9959
9960   /* Compute a result for LT or EQ if args permit;
9961      Otherwise return T.  */
9962   if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9963     {
9964       if (code == EQ_EXPR)
9965         tem = build_int_2 (tree_int_cst_equal (op0, op1), 0);
9966       else
9967         tem = build_int_2 ((TYPE_UNSIGNED (TREE_TYPE (op0))
9968                             ? INT_CST_LT_UNSIGNED (op0, op1)
9969                             : INT_CST_LT (op0, op1)),
9970                            0);
9971     }
9972
9973   else if (code == EQ_EXPR && !TREE_SIDE_EFFECTS (op0)
9974            && integer_zerop (op1) && tree_expr_nonzero_p (op0))
9975     tem = build_int_2 (0, 0);
9976
9977   /* Two real constants can be compared explicitly.  */
9978   else if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
9979     {
9980       /* If either operand is a NaN, the result is false with two
9981          exceptions: First, an NE_EXPR is true on NaNs, but that case
9982          is already handled correctly since we will be inverting the
9983          result for NE_EXPR.  Second, if we had inverted a LE_EXPR
9984          or a GE_EXPR into a LT_EXPR, we must return true so that it
9985          will be inverted into false.  */
9986
9987       if (REAL_VALUE_ISNAN (TREE_REAL_CST (op0))
9988           || REAL_VALUE_ISNAN (TREE_REAL_CST (op1)))
9989         tem = build_int_2 (invert && code == LT_EXPR, 0);
9990
9991       else if (code == EQ_EXPR)
9992         tem = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (op0),
9993                                               TREE_REAL_CST (op1)),
9994                            0);
9995       else
9996         tem = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (op0),
9997                                              TREE_REAL_CST (op1)),
9998                            0);
9999     }
10000
10001   if (tem == NULL_TREE)
10002     return NULL_TREE;
10003
10004   if (invert)
10005     TREE_INT_CST_LOW (tem) ^= 1;
10006
10007   TREE_TYPE (tem) = type;
10008   if (TREE_CODE (type) == BOOLEAN_TYPE)
10009     return lang_hooks.truthvalue_conversion (tem);
10010   return tem;
10011 }
10012
10013 /* Build an expression for the address of T.  Folds away INDIRECT_REF to
10014    avoid confusing the gimplify process.  */
10015
10016 tree
10017 build_fold_addr_expr_with_type (tree t, tree ptrtype)
10018 {
10019   if (TREE_CODE (t) == INDIRECT_REF)
10020     {
10021       t = TREE_OPERAND (t, 0);
10022       if (TREE_TYPE (t) != ptrtype)
10023         t = build1 (NOP_EXPR, ptrtype, t);
10024     }
10025   else
10026     {
10027       tree base = t;
10028       while (TREE_CODE (base) == COMPONENT_REF
10029              || TREE_CODE (base) == ARRAY_REF)
10030         base = TREE_OPERAND (base, 0);
10031       if (DECL_P (base))
10032         TREE_ADDRESSABLE (base) = 1;
10033
10034       t = build1 (ADDR_EXPR, ptrtype, t);
10035     }
10036
10037   return t;
10038 }
10039
10040 tree
10041 build_fold_addr_expr (tree t)
10042 {
10043   return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
10044 }
10045
10046 /* Builds an expression for an indirection through T, simplifying some
10047    cases.  */
10048
10049 tree
10050 build_fold_indirect_ref (tree t)
10051 {
10052   tree type = TREE_TYPE (TREE_TYPE (t));
10053   tree sub = t;
10054   tree subtype;
10055
10056   STRIP_NOPS (sub);
10057   if (TREE_CODE (sub) == ADDR_EXPR)
10058     {
10059       tree op = TREE_OPERAND (sub, 0);
10060       tree optype = TREE_TYPE (op);
10061       /* *&p => p */
10062       if (lang_hooks.types_compatible_p (type, optype))
10063         return op;
10064       /* *(foo *)&fooarray => fooarray[0] */
10065       else if (TREE_CODE (optype) == ARRAY_TYPE
10066                && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
10067         return build2 (ARRAY_REF, type, op, size_zero_node);
10068     }
10069
10070   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10071   subtype = TREE_TYPE (sub);
10072   if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10073       && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10074     {
10075       sub = build_fold_indirect_ref (sub);
10076       return build2 (ARRAY_REF, type, sub, size_zero_node);
10077     }
10078
10079   return build1 (INDIRECT_REF, type, t);
10080 }
10081
10082 #include "gt-fold-const.h"