OSDN Git Service

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