OSDN Git Service

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