OSDN Git Service

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