OSDN Git Service

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