OSDN Git Service

(operand_equal_p): Do real comparison with REAL_VALUES_EQUAL.
[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, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /*@@ This file should be rewritten to use an arbitrary precision
22   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
23   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
24   @@ The routines that translate from the ap rep should
25   @@ warn if precision et. al. is lost.
26   @@ This would also make life easier when this technology is used
27   @@ for cross-compilers.  */
28
29
30 /* The entry points in this file are fold, size_int and size_binop.
31
32    fold takes a tree as argument and returns a simplified tree.
33
34    size_binop takes a tree code for an arithmetic operation
35    and two operands that are trees, and produces a tree for the
36    result, assuming the type comes from `sizetype'.
37
38    size_int takes an integer value, and creates a tree constant
39    with type from `sizetype'.  */
40    
41 #include <stdio.h>
42 #include <setjmp.h>
43 #include "config.h"
44 #include "flags.h"
45 #include "tree.h"
46
47 /* Handle floating overflow for `const_binop'.  */
48 static jmp_buf float_error;
49
50 static void encode              PROTO((HOST_WIDE_INT *,
51                                        HOST_WIDE_INT, HOST_WIDE_INT));
52 static void decode              PROTO((HOST_WIDE_INT *,
53                                        HOST_WIDE_INT *, HOST_WIDE_INT *));
54 int div_and_round_double        PROTO((enum tree_code, int, HOST_WIDE_INT,
55                                        HOST_WIDE_INT, HOST_WIDE_INT,
56                                        HOST_WIDE_INT, HOST_WIDE_INT *,
57                                        HOST_WIDE_INT *, HOST_WIDE_INT *,
58                                        HOST_WIDE_INT *));
59 static int split_tree           PROTO((tree, enum tree_code, tree *,
60                                        tree *, int *));
61 static tree const_binop         PROTO((enum tree_code, tree, tree, int));
62 static tree fold_convert        PROTO((tree, tree));
63 static enum tree_code invert_tree_comparison PROTO((enum tree_code));
64 static enum tree_code swap_tree_comparison PROTO((enum tree_code));
65 static int truth_value_p        PROTO((enum tree_code));
66 static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
67 static int twoval_comparison_p  PROTO((tree, tree *, tree *, int *));
68 static tree eval_subst          PROTO((tree, tree, tree, tree, tree));
69 static tree omit_one_operand    PROTO((tree, tree, tree));
70 static tree pedantic_omit_one_operand PROTO((tree, tree, tree));
71 static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
72 static tree make_bit_field_ref  PROTO((tree, tree, int, int, int));
73 static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
74                                               tree, tree));
75 static tree decode_field_reference PROTO((tree, int *, int *,
76                                           enum machine_mode *, int *,
77                                           int *, tree *, tree *));
78 static int all_ones_mask_p      PROTO((tree, int));
79 static int simple_operand_p     PROTO((tree));
80 static tree range_binop         PROTO((enum tree_code, tree, tree, int,
81                                        tree, int));
82 static tree make_range          PROTO((tree, int *, tree *, tree *));
83 static tree build_range_check   PROTO((tree, tree, int, tree, tree));
84 static int merge_ranges         PROTO((int *, tree *, tree *, int, tree, tree,
85                                        int, tree, tree));
86 static tree fold_range_test     PROTO((tree));
87 static tree unextend            PROTO((tree, int, int, tree));
88 static tree fold_truthop        PROTO((enum tree_code, tree, tree, tree));
89 static tree strip_compound_expr PROTO((tree, tree));
90
91 #ifndef BRANCH_COST
92 #define BRANCH_COST 1
93 #endif
94
95 /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
96    Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
97    Then this yields nonzero if overflow occurred during the addition.
98    Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
99    Use `^' to test whether signs differ, and `< 0' to isolate the sign.  */
100 #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
101 \f
102 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
103    We do that by representing the two-word integer in 4 words, with only
104    HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number.  */
105
106 #define LOWPART(x) \
107   ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1))
108 #define HIGHPART(x) \
109   ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2)
110 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2)
111
112 /* Unpack a two-word integer into 4 words.
113    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
114    WORDS points to the array of HOST_WIDE_INTs.  */
115
116 static void
117 encode (words, low, hi)
118      HOST_WIDE_INT *words;
119      HOST_WIDE_INT low, hi;
120 {
121   words[0] = LOWPART (low);
122   words[1] = HIGHPART (low);
123   words[2] = LOWPART (hi);
124   words[3] = HIGHPART (hi);
125 }
126
127 /* Pack an array of 4 words into a two-word integer.
128    WORDS points to the array of words.
129    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
130
131 static void
132 decode (words, low, hi)
133      HOST_WIDE_INT *words;
134      HOST_WIDE_INT *low, *hi;
135 {
136   *low = words[0] | words[1] * BASE;
137   *hi = words[2] | words[3] * BASE;
138 }
139 \f
140 /* Make the integer constant T valid for its type
141    by setting to 0 or 1 all the bits in the constant
142    that don't belong in the type.
143    Yield 1 if a signed overflow occurs, 0 otherwise.
144    If OVERFLOW is nonzero, a signed overflow has already occurred
145    in calculating T, so propagate it.
146
147    Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
148    if it exists.  */
149
150 int
151 force_fit_type (t, overflow)
152      tree t;
153      int overflow;
154 {
155   HOST_WIDE_INT low, high;
156   register int prec;
157
158   if (TREE_CODE (t) == REAL_CST)
159     {
160 #ifdef CHECK_FLOAT_VALUE
161       CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
162                          overflow);
163 #endif
164       return overflow;
165     }
166
167   else if (TREE_CODE (t) != INTEGER_CST)
168     return overflow;
169
170   low = TREE_INT_CST_LOW (t);
171   high = TREE_INT_CST_HIGH (t);
172
173   if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
174     prec = POINTER_SIZE;
175   else
176     prec = TYPE_PRECISION (TREE_TYPE (t));
177
178   /* First clear all bits that are beyond the type's precision.  */
179
180   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
181     ;
182   else if (prec > HOST_BITS_PER_WIDE_INT)
183     {
184       TREE_INT_CST_HIGH (t)
185         &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
186     }
187   else
188     {
189       TREE_INT_CST_HIGH (t) = 0;
190       if (prec < HOST_BITS_PER_WIDE_INT)
191         TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
192     }
193
194   /* Unsigned types do not suffer sign extension or overflow.  */
195   if (TREE_UNSIGNED (TREE_TYPE (t)))
196     return overflow;
197
198   /* If the value's sign bit is set, extend the sign.  */
199   if (prec != 2 * HOST_BITS_PER_WIDE_INT
200       && (prec > HOST_BITS_PER_WIDE_INT
201           ? (TREE_INT_CST_HIGH (t)
202              & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
203           : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
204     {
205       /* Value is negative:
206          set to 1 all the bits that are outside this type's precision.  */
207       if (prec > HOST_BITS_PER_WIDE_INT)
208         {
209           TREE_INT_CST_HIGH (t)
210             |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
211         }
212       else
213         {
214           TREE_INT_CST_HIGH (t) = -1;
215           if (prec < HOST_BITS_PER_WIDE_INT)
216             TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
217         }
218     }
219
220   /* Yield nonzero if signed overflow occurred.  */
221   return
222     ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
223      != 0);
224 }
225 \f
226 /* Add two doubleword integers with doubleword result.
227    Each argument is given as two `HOST_WIDE_INT' pieces.
228    One argument is L1 and H1; the other, L2 and H2.
229    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
230
231 int
232 add_double (l1, h1, l2, h2, lv, hv)
233      HOST_WIDE_INT l1, h1, l2, h2;
234      HOST_WIDE_INT *lv, *hv;
235 {
236   HOST_WIDE_INT l, h;
237
238   l = l1 + l2;
239   h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1);
240
241   *lv = l;
242   *hv = h;
243   return overflow_sum_sign (h1, h2, h);
244 }
245
246 /* Negate a doubleword integer with doubleword result.
247    Return nonzero if the operation overflows, assuming it's signed.
248    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
249    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
250
251 int
252 neg_double (l1, h1, lv, hv)
253      HOST_WIDE_INT l1, h1;
254      HOST_WIDE_INT *lv, *hv;
255 {
256   if (l1 == 0)
257     {
258       *lv = 0;
259       *hv = - h1;
260       return (*hv & h1) < 0;
261     }
262   else
263     {
264       *lv = - l1;
265       *hv = ~ h1;
266       return 0;
267     }
268 }
269 \f
270 /* Multiply two doubleword integers with doubleword result.
271    Return nonzero if the operation overflows, assuming it's signed.
272    Each argument is given as two `HOST_WIDE_INT' pieces.
273    One argument is L1 and H1; the other, L2 and H2.
274    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
275
276 int
277 mul_double (l1, h1, l2, h2, lv, hv)
278      HOST_WIDE_INT l1, h1, l2, h2;
279      HOST_WIDE_INT *lv, *hv;
280 {
281   HOST_WIDE_INT arg1[4];
282   HOST_WIDE_INT arg2[4];
283   HOST_WIDE_INT prod[4 * 2];
284   register unsigned HOST_WIDE_INT carry;
285   register int i, j, k;
286   HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
287
288   encode (arg1, l1, h1);
289   encode (arg2, l2, h2);
290
291   bzero ((char *) prod, sizeof prod);
292
293   for (i = 0; i < 4; i++)
294     {
295       carry = 0;
296       for (j = 0; j < 4; j++)
297         {
298           k = i + j;
299           /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000.  */
300           carry += arg1[i] * arg2[j];
301           /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF.  */
302           carry += prod[k];
303           prod[k] = LOWPART (carry);
304           carry = HIGHPART (carry);
305         }
306       prod[i + 4] = carry;
307     }
308
309   decode (prod, lv, hv);        /* This ignores prod[4] through prod[4*2-1] */
310
311   /* Check for overflow by calculating the top half of the answer in full;
312      it should agree with the low half's sign bit.  */
313   decode (prod+4, &toplow, &tophigh);
314   if (h1 < 0)
315     {
316       neg_double (l2, h2, &neglow, &neghigh);
317       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
318     }
319   if (h2 < 0)
320     {
321       neg_double (l1, h1, &neglow, &neghigh);
322       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
323     }
324   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
325 }
326 \f
327 /* Shift the doubleword integer in L1, H1 left by COUNT places
328    keeping only PREC bits of result.
329    Shift right if COUNT is negative.
330    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
331    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
332
333 void
334 lshift_double (l1, h1, count, prec, lv, hv, arith)
335      HOST_WIDE_INT l1, h1, count;
336      int prec;
337      HOST_WIDE_INT *lv, *hv;
338      int arith;
339 {
340   if (count < 0)
341     {
342       rshift_double (l1, h1, - count, prec, lv, hv, arith);
343       return;
344     }
345   
346 #ifdef SHIFT_COUNT_TRUNCATED
347   if (SHIFT_COUNT_TRUNCATED)
348     count %= prec;
349 #endif
350
351   if (count >= HOST_BITS_PER_WIDE_INT)
352     {
353       *hv = (unsigned HOST_WIDE_INT) l1 << count - HOST_BITS_PER_WIDE_INT;
354       *lv = 0;
355     }
356   else
357     {
358       *hv = (((unsigned HOST_WIDE_INT) h1 << count)
359              | ((unsigned HOST_WIDE_INT) l1 >> HOST_BITS_PER_WIDE_INT - count - 1 >> 1));
360       *lv = (unsigned HOST_WIDE_INT) l1 << count;
361     }
362 }
363
364 /* Shift the doubleword integer in L1, H1 right by COUNT places
365    keeping only PREC bits of result.  COUNT must be positive.
366    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
367    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
368
369 void
370 rshift_double (l1, h1, count, prec, lv, hv, arith)
371      HOST_WIDE_INT l1, h1, count;
372      int prec;
373      HOST_WIDE_INT *lv, *hv;
374      int arith;
375 {
376   unsigned HOST_WIDE_INT signmask;
377   signmask = (arith
378               ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
379               : 0);
380
381 #ifdef SHIFT_COUNT_TRUNCATED
382   if (SHIFT_COUNT_TRUNCATED)
383     count %= prec;
384 #endif
385
386   if (count >= HOST_BITS_PER_WIDE_INT)
387     {
388       *hv = signmask;
389       *lv = ((signmask << 2 * HOST_BITS_PER_WIDE_INT - count - 1 << 1)
390              | ((unsigned HOST_WIDE_INT) h1 >> count - HOST_BITS_PER_WIDE_INT));
391     }
392   else
393     {
394       *lv = (((unsigned HOST_WIDE_INT) l1 >> count)
395              | ((unsigned HOST_WIDE_INT) h1 << HOST_BITS_PER_WIDE_INT - count - 1 << 1));
396       *hv = ((signmask << HOST_BITS_PER_WIDE_INT - count)
397              | ((unsigned HOST_WIDE_INT) h1 >> count));
398     }
399 }
400 \f
401 /* Rotate the doubleword integer in L1, H1 left by COUNT places
402    keeping only PREC bits of result.
403    Rotate right if COUNT is negative.
404    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
405
406 void
407 lrotate_double (l1, h1, count, prec, lv, hv)
408      HOST_WIDE_INT l1, h1, count;
409      int prec;
410      HOST_WIDE_INT *lv, *hv;
411 {
412   HOST_WIDE_INT s1l, s1h, s2l, s2h;
413
414   count %= prec;
415   if (count < 0)
416     count += prec;
417
418   lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
419   rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
420   *lv = s1l | s2l;
421   *hv = s1h | s2h;
422 }
423
424 /* Rotate the doubleword integer in L1, H1 left by COUNT places
425    keeping only PREC bits of result.  COUNT must be positive.
426    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
427
428 void
429 rrotate_double (l1, h1, count, prec, lv, hv)
430      HOST_WIDE_INT l1, h1, count;
431      int prec;
432      HOST_WIDE_INT *lv, *hv;
433 {
434   HOST_WIDE_INT s1l, s1h, s2l, s2h;
435
436   count %= prec;
437   if (count < 0)
438     count += prec;
439
440   rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
441   lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
442   *lv = s1l | s2l;
443   *hv = s1h | s2h;
444 }
445 \f
446 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
447    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
448    CODE is a tree code for a kind of division, one of
449    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
450    or EXACT_DIV_EXPR
451    It controls how the quotient is rounded to a integer.
452    Return nonzero if the operation overflows.
453    UNS nonzero says do unsigned division.  */
454
455 int
456 div_and_round_double (code, uns,
457                       lnum_orig, hnum_orig, lden_orig, hden_orig,
458                       lquo, hquo, lrem, hrem)
459      enum tree_code code;
460      int uns;
461      HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
462      HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
463      HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
464 {
465   int quo_neg = 0;
466   HOST_WIDE_INT num[4 + 1];     /* extra element for scaling.  */
467   HOST_WIDE_INT den[4], quo[4];
468   register int i, j;
469   unsigned HOST_WIDE_INT work;
470   register unsigned HOST_WIDE_INT carry = 0;
471   HOST_WIDE_INT lnum = lnum_orig;
472   HOST_WIDE_INT hnum = hnum_orig;
473   HOST_WIDE_INT lden = lden_orig;
474   HOST_WIDE_INT hden = hden_orig;
475   int overflow = 0;
476
477   if ((hden == 0) && (lden == 0))
478     abort ();
479
480   /* calculate quotient sign and convert operands to unsigned.  */
481   if (!uns) 
482     {
483       if (hnum < 0)
484         {
485           quo_neg = ~ quo_neg;
486           /* (minimum integer) / (-1) is the only overflow case.  */
487           if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
488             overflow = 1;
489         }
490       if (hden < 0) 
491         {
492           quo_neg = ~ quo_neg;
493           neg_double (lden, hden, &lden, &hden);
494         }
495     }
496
497   if (hnum == 0 && hden == 0)
498     {                           /* single precision */
499       *hquo = *hrem = 0;
500       /* This unsigned division rounds toward zero.  */
501       *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
502       goto finish_up;
503     }
504
505   if (hnum == 0)
506     {                           /* trivial case: dividend < divisor */
507       /* hden != 0 already checked.  */
508       *hquo = *lquo = 0;
509       *hrem = hnum;
510       *lrem = lnum;
511       goto finish_up;
512     }
513
514   bzero ((char *) quo, sizeof quo);
515
516   bzero ((char *) num, sizeof num);     /* to zero 9th element */
517   bzero ((char *) den, sizeof den);
518
519   encode (num, lnum, hnum); 
520   encode (den, lden, hden);
521
522   /* Special code for when the divisor < BASE.  */
523   if (hden == 0 && lden < BASE)
524     {
525       /* hnum != 0 already checked.  */
526       for (i = 4 - 1; i >= 0; i--)
527         {
528           work = num[i] + carry * BASE;
529           quo[i] = work / (unsigned HOST_WIDE_INT) lden;
530           carry = work % (unsigned HOST_WIDE_INT) lden;
531         }
532     }
533   else
534     {
535       /* Full double precision division,
536          with thanks to Don Knuth's "Seminumerical Algorithms".  */
537     int num_hi_sig, den_hi_sig;
538     unsigned HOST_WIDE_INT quo_est, scale;
539
540     /* Find the highest non-zero divisor digit.  */
541     for (i = 4 - 1; ; i--)
542       if (den[i] != 0) {
543         den_hi_sig = i;
544         break;
545       }
546
547     /* Insure that the first digit of the divisor is at least BASE/2.
548        This is required by the quotient digit estimation algorithm.  */
549
550     scale = BASE / (den[den_hi_sig] + 1);
551     if (scale > 1) {            /* scale divisor and dividend */
552       carry = 0;
553       for (i = 0; i <= 4 - 1; i++) {
554         work = (num[i] * scale) + carry;
555         num[i] = LOWPART (work);
556         carry = HIGHPART (work);
557       } num[4] = carry;
558       carry = 0;
559       for (i = 0; i <= 4 - 1; i++) {
560         work = (den[i] * scale) + carry;
561         den[i] = LOWPART (work);
562         carry = HIGHPART (work);
563         if (den[i] != 0) den_hi_sig = i;
564       }
565     }
566
567     num_hi_sig = 4;
568
569     /* Main loop */
570     for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) {
571       /* guess the next quotient digit, quo_est, by dividing the first
572          two remaining dividend digits by the high order quotient digit.
573          quo_est is never low and is at most 2 high.  */
574       unsigned HOST_WIDE_INT tmp;
575
576       num_hi_sig = i + den_hi_sig + 1;
577       work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
578       if (num[num_hi_sig] != den[den_hi_sig])
579         quo_est = work / den[den_hi_sig];
580       else
581         quo_est = BASE - 1;
582
583       /* refine quo_est so it's usually correct, and at most one high.   */
584       tmp = work - quo_est * den[den_hi_sig];
585       if (tmp < BASE
586           && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2]))
587         quo_est--;
588
589       /* Try QUO_EST as the quotient digit, by multiplying the
590          divisor by QUO_EST and subtracting from the remaining dividend.
591          Keep in mind that QUO_EST is the I - 1st digit.  */
592
593       carry = 0;
594       for (j = 0; j <= den_hi_sig; j++)
595         {
596           work = quo_est * den[j] + carry;
597           carry = HIGHPART (work);
598           work = num[i + j] - LOWPART (work);
599           num[i + j] = LOWPART (work);
600           carry += HIGHPART (work) != 0;
601         }
602
603       /* if quo_est was high by one, then num[i] went negative and
604          we need to correct things.  */
605
606       if (num[num_hi_sig] < carry)
607         {
608           quo_est--;
609           carry = 0;            /* add divisor back in */
610           for (j = 0; j <= den_hi_sig; j++)
611             {
612               work = num[i + j] + den[j] + carry;
613               carry = HIGHPART (work);
614               num[i + j] = LOWPART (work);
615             }
616           num [num_hi_sig] += carry;
617         }
618
619       /* store the quotient digit.  */
620       quo[i] = quo_est;
621     }
622   }
623
624   decode (quo, lquo, hquo);
625
626  finish_up:
627   /* if result is negative, make it so.  */
628   if (quo_neg)
629     neg_double (*lquo, *hquo, lquo, hquo);
630
631   /* compute trial remainder:  rem = num - (quo * den)  */
632   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
633   neg_double (*lrem, *hrem, lrem, hrem);
634   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
635
636   switch (code)
637     {
638     case TRUNC_DIV_EXPR:
639     case TRUNC_MOD_EXPR:        /* round toward zero */
640     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
641       return overflow;
642
643     case FLOOR_DIV_EXPR:
644     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
645       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
646         {
647           /* quo = quo - 1;  */
648           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
649                       lquo, hquo);
650         }
651       else return overflow;
652       break;
653
654     case CEIL_DIV_EXPR:
655     case CEIL_MOD_EXPR:         /* round toward positive infinity */
656       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
657         {
658           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
659                       lquo, hquo);
660         }
661       else return overflow;
662       break;
663     
664     case ROUND_DIV_EXPR:
665     case ROUND_MOD_EXPR:        /* round to closest integer */
666       {
667         HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
668         HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
669
670         /* get absolute values */
671         if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
672         if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
673
674         /* if (2 * abs (lrem) >= abs (lden)) */
675         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
676                     labs_rem, habs_rem, &ltwice, &htwice);
677         if (((unsigned HOST_WIDE_INT) habs_den
678              < (unsigned HOST_WIDE_INT) htwice)
679             || (((unsigned HOST_WIDE_INT) habs_den
680                  == (unsigned HOST_WIDE_INT) htwice)
681                 && ((HOST_WIDE_INT unsigned) labs_den
682                     < (unsigned HOST_WIDE_INT) ltwice)))
683           {
684             if (*hquo < 0)
685               /* quo = quo - 1;  */
686               add_double (*lquo, *hquo,
687                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
688             else
689               /* quo = quo + 1; */
690               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
691                           lquo, hquo);
692           }
693         else return overflow;
694       }
695       break;
696
697     default:
698       abort ();
699     }
700
701   /* compute true remainder:  rem = num - (quo * den)  */
702   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
703   neg_double (*lrem, *hrem, lrem, hrem);
704   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
705   return overflow;
706 }
707 \f
708 #ifndef REAL_ARITHMETIC
709 /* Effectively truncate a real value to represent the nearest possible value
710    in a narrower mode.  The result is actually represented in the same data
711    type as the argument, but its value is usually different.
712
713    A trap may occur during the FP operations and it is the responsibility
714    of the calling function to have a handler established.  */
715
716 REAL_VALUE_TYPE
717 real_value_truncate (mode, arg)
718      enum machine_mode mode;
719      REAL_VALUE_TYPE arg;
720 {
721   return REAL_VALUE_TRUNCATE (mode, arg);
722 }
723
724 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
725
726 /* Check for infinity in an IEEE double precision number.  */
727
728 int
729 target_isinf (x)
730      REAL_VALUE_TYPE x;
731 {
732   /* The IEEE 64-bit double format.  */
733   union {
734     REAL_VALUE_TYPE d;
735     struct {
736       unsigned sign      :  1;
737       unsigned exponent  : 11;
738       unsigned mantissa1 : 20;
739       unsigned mantissa2;
740     } little_endian;
741     struct {
742       unsigned mantissa2;
743       unsigned mantissa1 : 20;
744       unsigned exponent  : 11;
745       unsigned sign      :  1;
746     } big_endian;    
747   } u;
748
749   u.d = dconstm1;
750   if (u.big_endian.sign == 1)
751     {
752       u.d = x;
753       return (u.big_endian.exponent == 2047
754               && u.big_endian.mantissa1 == 0
755               && u.big_endian.mantissa2 == 0);
756     }
757   else
758     {
759       u.d = x;
760       return (u.little_endian.exponent == 2047
761               && u.little_endian.mantissa1 == 0
762               && u.little_endian.mantissa2 == 0);
763     }
764 }
765
766 /* Check whether an IEEE double precision number is a NaN.  */
767
768 int
769 target_isnan (x)
770      REAL_VALUE_TYPE x;
771 {
772   /* The IEEE 64-bit double format.  */
773   union {
774     REAL_VALUE_TYPE d;
775     struct {
776       unsigned sign      :  1;
777       unsigned exponent  : 11;
778       unsigned mantissa1 : 20;
779       unsigned mantissa2;
780     } little_endian;
781     struct {
782       unsigned mantissa2;
783       unsigned mantissa1 : 20;
784       unsigned exponent  : 11;
785       unsigned sign      :  1;
786     } big_endian;    
787   } u;
788
789   u.d = dconstm1;
790   if (u.big_endian.sign == 1)
791     {
792       u.d = x;
793       return (u.big_endian.exponent == 2047
794               && (u.big_endian.mantissa1 != 0
795                   || u.big_endian.mantissa2 != 0));
796     }
797   else
798     {
799       u.d = x;
800       return (u.little_endian.exponent == 2047
801               && (u.little_endian.mantissa1 != 0
802                   || u.little_endian.mantissa2 != 0));
803     }
804 }
805
806 /* Check for a negative IEEE double precision number.  */
807
808 int
809 target_negative (x)
810      REAL_VALUE_TYPE x;
811 {
812   /* The IEEE 64-bit double format.  */
813   union {
814     REAL_VALUE_TYPE d;
815     struct {
816       unsigned sign      :  1;
817       unsigned exponent  : 11;
818       unsigned mantissa1 : 20;
819       unsigned mantissa2;
820     } little_endian;
821     struct {
822       unsigned mantissa2;
823       unsigned mantissa1 : 20;
824       unsigned exponent  : 11;
825       unsigned sign      :  1;
826     } big_endian;    
827   } u;
828
829   u.d = dconstm1;
830   if (u.big_endian.sign == 1)
831     {
832       u.d = x;
833       return u.big_endian.sign;
834     }
835   else
836     {
837       u.d = x;
838       return u.little_endian.sign;
839     }
840 }
841 #else /* Target not IEEE */
842
843 /* Let's assume other float formats don't have infinity.
844    (This can be overridden by redefining REAL_VALUE_ISINF.)  */
845
846 target_isinf (x)
847      REAL_VALUE_TYPE x;
848 {
849   return 0;
850 }
851
852 /* Let's assume other float formats don't have NaNs.
853    (This can be overridden by redefining REAL_VALUE_ISNAN.)  */
854
855 target_isnan (x)
856      REAL_VALUE_TYPE x;
857 {
858   return 0;
859 }
860
861 /* Let's assume other float formats don't have minus zero.
862    (This can be overridden by redefining REAL_VALUE_NEGATIVE.)  */
863
864 target_negative (x)
865      REAL_VALUE_TYPE x;
866 {
867   return x < 0;
868 }
869 #endif /* Target not IEEE */
870
871 /* Try to change R into its exact multiplicative inverse in machine mode
872    MODE.  Return nonzero function value if successful.  */
873
874 int
875 exact_real_inverse (mode, r)
876      enum machine_mode mode;
877      REAL_VALUE_TYPE *r;
878 {
879   union
880     {
881       double d;
882       unsigned short i[4];
883     }x, t, y;
884   int i;
885
886   /* Usually disable if bounds checks are not reliable.  */
887   if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
888     return 0;
889
890   /* Set array index to the less significant bits in the unions, depending
891      on the endian-ness of the host doubles.
892      Disable if insufficient information on the data structure.  */
893 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
894   return 0;
895 #else
896 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
897 #define K 2
898 #else
899 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
900 #define K 2
901 #else
902 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
903 #endif
904 #endif
905 #endif
906
907   if (setjmp (float_error))
908     {
909       /* Don't do the optimization if there was an arithmetic error.  */
910 fail:
911       set_float_handler (NULL_PTR);
912       return 0;
913     }
914   set_float_handler (float_error);
915
916   /* Domain check the argument.  */
917   x.d = *r;
918   if (x.d == 0.0)
919     goto fail;
920
921 #ifdef REAL_INFINITY
922   if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
923     goto fail;
924 #endif
925
926   /* Compute the reciprocal and check for numerical exactness.
927      It is unnecessary to check all the significand bits to determine
928      whether X is a power of 2.  If X is not, then it is impossible for
929      the bottom half significand of both X and 1/X to be all zero bits.
930      Hence we ignore the data structure of the top half and examine only
931      the low order bits of the two significands.  */
932   t.d = 1.0 / x.d;
933   if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
934     goto fail;
935
936   /* Truncate to the required mode and range-check the result.  */
937   y.d = REAL_VALUE_TRUNCATE (mode, t.d);
938 #ifdef CHECK_FLOAT_VALUE
939   i = 0;
940   if (CHECK_FLOAT_VALUE (mode, y.d, i))
941     goto fail;
942 #endif
943
944   /* Fail if truncation changed the value.  */
945   if (y.d != t.d || y.d == 0.0)
946     goto fail;
947
948 #ifdef REAL_INFINITY
949   if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
950     goto fail;
951 #endif
952
953   /* Output the reciprocal and return success flag.  */
954   set_float_handler (NULL_PTR);
955   *r = y.d;
956   return 1;
957 }
958 #endif /* no REAL_ARITHMETIC */
959 \f
960 /* Split a tree IN into a constant and a variable part
961    that could be combined with CODE to make IN.
962    CODE must be a commutative arithmetic operation.
963    Store the constant part into *CONP and the variable in &VARP.
964    Return 1 if this was done; zero means the tree IN did not decompose
965    this way.
966
967    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
968    Therefore, we must tell the caller whether the variable part
969    was subtracted.  We do this by storing 1 or -1 into *VARSIGNP.
970    The value stored is the coefficient for the variable term.
971    The constant term we return should always be added;
972    we negate it if necessary.  */
973
974 static int
975 split_tree (in, code, varp, conp, varsignp)
976      tree in;
977      enum tree_code code;
978      tree *varp, *conp;
979      int *varsignp;
980 {
981   register tree outtype = TREE_TYPE (in);
982   *varp = 0;
983   *conp = 0;
984
985   /* Strip any conversions that don't change the machine mode.  */
986   while ((TREE_CODE (in) == NOP_EXPR
987           || TREE_CODE (in) == CONVERT_EXPR)
988          && (TYPE_MODE (TREE_TYPE (in))
989              == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
990     in = TREE_OPERAND (in, 0);
991
992   if (TREE_CODE (in) == code
993       || (! FLOAT_TYPE_P (TREE_TYPE (in))
994           /* We can associate addition and subtraction together
995              (even though the C standard doesn't say so)
996              for integers because the value is not affected.
997              For reals, the value might be affected, so we can't.  */
998           && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
999               || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1000     {
1001       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
1002       if (code == INTEGER_CST)
1003         {
1004           *conp = TREE_OPERAND (in, 0);
1005           *varp = TREE_OPERAND (in, 1);
1006           if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1007               && TREE_TYPE (*varp) != outtype)
1008             *varp = convert (outtype, *varp);
1009           *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1010           return 1;
1011         }
1012       if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
1013         {
1014           *conp = TREE_OPERAND (in, 1);
1015           *varp = TREE_OPERAND (in, 0);
1016           *varsignp = 1;
1017           if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1018               && TREE_TYPE (*varp) != outtype)
1019             *varp = convert (outtype, *varp);
1020           if (TREE_CODE (in) == MINUS_EXPR)
1021             {
1022               /* If operation is subtraction and constant is second,
1023                  must negate it to get an additive constant.
1024                  And this cannot be done unless it is a manifest constant.
1025                  It could also be the address of a static variable.
1026                  We cannot negate that, so give up.  */
1027               if (TREE_CODE (*conp) == INTEGER_CST)
1028                 /* Subtracting from integer_zero_node loses for long long.  */
1029                 *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
1030               else
1031                 return 0;
1032             }
1033           return 1;
1034         }
1035       if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
1036         {
1037           *conp = TREE_OPERAND (in, 0);
1038           *varp = TREE_OPERAND (in, 1);
1039           if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1040               && TREE_TYPE (*varp) != outtype)
1041             *varp = convert (outtype, *varp);
1042           *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1043           return 1;
1044         }
1045     }
1046   return 0;
1047 }
1048 \f
1049 /* Combine two constants ARG1 and ARG2 under operation CODE
1050    to produce a new constant.
1051    We assume ARG1 and ARG2 have the same data type,
1052    or at least are the same kind of constant and the same machine mode.
1053
1054    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1055
1056 static tree
1057 const_binop (code, arg1, arg2, notrunc)
1058      enum tree_code code;
1059      register tree arg1, arg2;
1060      int notrunc;
1061 {
1062   STRIP_NOPS (arg1); STRIP_NOPS (arg2);
1063
1064   if (TREE_CODE (arg1) == INTEGER_CST)
1065     {
1066       register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
1067       register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
1068       HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
1069       HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
1070       HOST_WIDE_INT low, hi;
1071       HOST_WIDE_INT garbagel, garbageh;
1072       register tree t;
1073       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1074       int overflow = 0;
1075
1076       switch (code)
1077         {
1078         case BIT_IOR_EXPR:
1079           t = build_int_2 (int1l | int2l, int1h | int2h);
1080           break;
1081
1082         case BIT_XOR_EXPR:
1083           t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
1084           break;
1085
1086         case BIT_AND_EXPR:
1087           t = build_int_2 (int1l & int2l, int1h & int2h);
1088           break;
1089
1090         case BIT_ANDTC_EXPR:
1091           t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
1092           break;
1093
1094         case RSHIFT_EXPR:
1095           int2l = - int2l;
1096         case LSHIFT_EXPR:
1097           /* It's unclear from the C standard whether shifts can overflow.
1098              The following code ignores overflow; perhaps a C standard
1099              interpretation ruling is needed.  */
1100           lshift_double (int1l, int1h, int2l,
1101                          TYPE_PRECISION (TREE_TYPE (arg1)),
1102                          &low, &hi,
1103                          !uns);
1104           t = build_int_2 (low, hi);
1105           TREE_TYPE (t) = TREE_TYPE (arg1);
1106           if (!notrunc)
1107             force_fit_type (t, 0);
1108           TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1109           TREE_CONSTANT_OVERFLOW (t)
1110             = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
1111           return t;
1112
1113         case RROTATE_EXPR:
1114           int2l = - int2l;
1115         case LROTATE_EXPR:
1116           lrotate_double (int1l, int1h, int2l,
1117                           TYPE_PRECISION (TREE_TYPE (arg1)),
1118                           &low, &hi);
1119           t = build_int_2 (low, hi);
1120           break;
1121
1122         case PLUS_EXPR:
1123           if (int1h == 0)
1124             {
1125               int2l += int1l;
1126               if ((unsigned HOST_WIDE_INT) int2l < int1l)
1127                 {
1128                   hi = int2h++;
1129                   overflow = int2h < hi;
1130                 }
1131               t = build_int_2 (int2l, int2h);
1132               break;
1133             }
1134           if (int2h == 0)
1135             {
1136               int1l += int2l;
1137               if ((unsigned HOST_WIDE_INT) int1l < int2l)
1138                 {
1139                   hi = int1h++;
1140                   overflow = int1h < hi;
1141                 }
1142               t = build_int_2 (int1l, int1h);
1143               break;
1144             }
1145           overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1146           t = build_int_2 (low, hi);
1147           break;
1148
1149         case MINUS_EXPR:
1150           if (int2h == 0 && int2l == 0)
1151             {
1152               t = build_int_2 (int1l, int1h);
1153               break;
1154             }
1155           neg_double (int2l, int2h, &low, &hi);
1156           add_double (int1l, int1h, low, hi, &low, &hi);
1157           overflow = overflow_sum_sign (hi, int2h, int1h);
1158           t = build_int_2 (low, hi);
1159           break;
1160
1161         case MULT_EXPR:
1162           overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1163           t = build_int_2 (low, hi);
1164           break;
1165
1166         case TRUNC_DIV_EXPR:
1167         case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1168         case EXACT_DIV_EXPR:
1169           /* This is a shortcut for a common special case.
1170              It reduces the number of tree nodes generated
1171              and saves time.  */
1172           if (int2h == 0 && int2l > 0
1173               && TREE_TYPE (arg1) == sizetype
1174               && ! TREE_CONSTANT_OVERFLOW (arg1)
1175               && ! TREE_CONSTANT_OVERFLOW (arg2)
1176               && int1h == 0 && int1l >= 0)
1177             {
1178               if (code == CEIL_DIV_EXPR)
1179                 int1l += int2l-1;
1180               return size_int (int1l / int2l);
1181             }
1182         case ROUND_DIV_EXPR: 
1183           if (int2h == 0 && int2l == 1)
1184             {
1185               t = build_int_2 (int1l, int1h);
1186               break;
1187             }
1188           if (int1l == int2l && int1h == int2h)
1189             {
1190               if ((int1l | int1h) == 0)
1191                 abort ();
1192               t = build_int_2 (1, 0);
1193               break;
1194             }
1195           overflow = div_and_round_double (code, uns,
1196                                            int1l, int1h, int2l, int2h,
1197                                            &low, &hi, &garbagel, &garbageh);
1198           t = build_int_2 (low, hi);
1199           break;
1200
1201         case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
1202         case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1203           overflow = div_and_round_double (code, uns,
1204                                            int1l, int1h, int2l, int2h,
1205                                            &garbagel, &garbageh, &low, &hi);
1206           t = build_int_2 (low, hi);
1207           break;
1208
1209         case MIN_EXPR:
1210         case MAX_EXPR:
1211           if (uns)
1212             {
1213               low = (((unsigned HOST_WIDE_INT) int1h
1214                       < (unsigned HOST_WIDE_INT) int2h)
1215                      || (((unsigned HOST_WIDE_INT) int1h
1216                           == (unsigned HOST_WIDE_INT) int2h)
1217                          && ((unsigned HOST_WIDE_INT) int1l
1218                              < (unsigned HOST_WIDE_INT) int2l)));
1219             }
1220           else
1221             {
1222               low = ((int1h < int2h)
1223                      || ((int1h == int2h)
1224                          && ((unsigned HOST_WIDE_INT) int1l
1225                              < (unsigned HOST_WIDE_INT) int2l)));
1226             }
1227           if (low == (code == MIN_EXPR))
1228             t = build_int_2 (int1l, int1h);
1229           else
1230             t = build_int_2 (int2l, int2h);
1231           break;
1232
1233         default:
1234           abort ();
1235         }
1236     got_it:
1237       TREE_TYPE (t) = TREE_TYPE (arg1);
1238       TREE_OVERFLOW (t)
1239         = ((notrunc ? !uns && overflow : force_fit_type (t, overflow && !uns))
1240            | TREE_OVERFLOW (arg1)
1241            | TREE_OVERFLOW (arg2));
1242       TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1243                                     | TREE_CONSTANT_OVERFLOW (arg1)
1244                                     | TREE_CONSTANT_OVERFLOW (arg2));
1245       return t;
1246     }
1247 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1248   if (TREE_CODE (arg1) == REAL_CST)
1249     {
1250       REAL_VALUE_TYPE d1;
1251       REAL_VALUE_TYPE d2;
1252       int overflow = 0;
1253       REAL_VALUE_TYPE value;
1254       tree t;
1255
1256       d1 = TREE_REAL_CST (arg1);
1257       d2 = TREE_REAL_CST (arg2);
1258
1259       /* If either operand is a NaN, just return it.  Otherwise, set up
1260          for floating-point trap; we return an overflow.  */
1261       if (REAL_VALUE_ISNAN (d1))
1262         return arg1;
1263       else if (REAL_VALUE_ISNAN (d2))
1264         return arg2;
1265       else if (setjmp (float_error))
1266         {
1267           t = copy_node (arg1);
1268           overflow = 1;
1269           goto got_float;
1270         }
1271
1272       set_float_handler (float_error);
1273
1274 #ifdef REAL_ARITHMETIC
1275       REAL_ARITHMETIC (value, code, d1, d2);
1276 #else
1277       switch (code)
1278         {
1279         case PLUS_EXPR:
1280           value = d1 + d2;
1281           break;
1282
1283         case MINUS_EXPR:
1284           value = d1 - d2;
1285           break;
1286
1287         case MULT_EXPR:
1288           value = d1 * d2;
1289           break;
1290
1291         case RDIV_EXPR:
1292 #ifndef REAL_INFINITY
1293           if (d2 == 0)
1294             abort ();
1295 #endif
1296
1297           value = d1 / d2;
1298           break;
1299
1300         case MIN_EXPR:
1301           value = MIN (d1, d2);
1302           break;
1303
1304         case MAX_EXPR:
1305           value = MAX (d1, d2);
1306           break;
1307
1308         default:
1309           abort ();
1310         }
1311 #endif /* no REAL_ARITHMETIC */
1312       t = build_real (TREE_TYPE (arg1),
1313                       real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
1314     got_float:
1315       set_float_handler (NULL_PTR);
1316
1317       TREE_OVERFLOW (t)
1318         = (force_fit_type (t, overflow)
1319            | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1320       TREE_CONSTANT_OVERFLOW (t)
1321         = TREE_OVERFLOW (t)
1322           | TREE_CONSTANT_OVERFLOW (arg1)
1323           | TREE_CONSTANT_OVERFLOW (arg2);
1324       return t;
1325     }
1326 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1327   if (TREE_CODE (arg1) == COMPLEX_CST)
1328     {
1329       register tree type = TREE_TYPE (arg1);
1330       register tree r1 = TREE_REALPART (arg1);
1331       register tree i1 = TREE_IMAGPART (arg1);
1332       register tree r2 = TREE_REALPART (arg2);
1333       register tree i2 = TREE_IMAGPART (arg2);
1334       register tree t;
1335
1336       switch (code)
1337         {
1338         case PLUS_EXPR:
1339           t = build_complex (type,
1340                              const_binop (PLUS_EXPR, r1, r2, notrunc),
1341                              const_binop (PLUS_EXPR, i1, i2, notrunc));
1342           break;
1343
1344         case MINUS_EXPR:
1345           t = build_complex (type,
1346                              const_binop (MINUS_EXPR, r1, r2, notrunc),
1347                              const_binop (MINUS_EXPR, i1, i2, notrunc));
1348           break;
1349
1350         case MULT_EXPR:
1351           t = build_complex (type,
1352                              const_binop (MINUS_EXPR,
1353                                           const_binop (MULT_EXPR,
1354                                                        r1, r2, notrunc),
1355                                           const_binop (MULT_EXPR,
1356                                                        i1, i2, notrunc),
1357                                           notrunc),
1358                              const_binop (PLUS_EXPR,
1359                                           const_binop (MULT_EXPR,
1360                                                        r1, i2, notrunc),
1361                                           const_binop (MULT_EXPR,
1362                                                        i1, r2, notrunc),
1363                                           notrunc));
1364           break;
1365
1366         case RDIV_EXPR:
1367           {
1368             register tree magsquared
1369               = const_binop (PLUS_EXPR,
1370                              const_binop (MULT_EXPR, r2, r2, notrunc),
1371                              const_binop (MULT_EXPR, i2, i2, notrunc),
1372                              notrunc);
1373
1374             t = build_complex (type,
1375                                const_binop
1376                                (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1377                                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1378                                 const_binop (PLUS_EXPR,
1379                                              const_binop (MULT_EXPR, r1, r2,
1380                                                           notrunc),
1381                                              const_binop (MULT_EXPR, i1, i2,
1382                                                           notrunc),
1383                                              notrunc),
1384                                 magsquared, notrunc),
1385                                const_binop
1386                                (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1387                                 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1388                                 const_binop (MINUS_EXPR,
1389                                              const_binop (MULT_EXPR, i1, r2,
1390                                                           notrunc),
1391                                              const_binop (MULT_EXPR, r1, i2,
1392                                                           notrunc),
1393                                              notrunc),
1394                                 magsquared, notrunc));
1395           }
1396           break;
1397
1398         default:
1399           abort ();
1400         }
1401       return t;
1402     }
1403   return 0;
1404 }
1405 \f
1406 /* Return an INTEGER_CST with value V and type from `sizetype'.  */
1407
1408 tree
1409 size_int (number)
1410      unsigned HOST_WIDE_INT number;
1411 {
1412   register tree t;
1413   /* Type-size nodes already made for small sizes.  */
1414   static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
1415
1416   if (number < 2*HOST_BITS_PER_WIDE_INT + 1
1417       && size_table[number] != 0)
1418     return size_table[number];
1419   if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
1420     {
1421       push_obstacks_nochange ();
1422       /* Make this a permanent node.  */
1423       end_temporary_allocation ();
1424       t = build_int_2 (number, 0);
1425       TREE_TYPE (t) = sizetype;
1426       size_table[number] = t;
1427       pop_obstacks ();
1428     }
1429   else
1430     {
1431       t = build_int_2 (number, 0);
1432       TREE_TYPE (t) = sizetype;
1433     }
1434   return t;
1435 }
1436
1437 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1438    CODE is a tree code.  Data type is taken from `sizetype',
1439    If the operands are constant, so is the result.  */
1440
1441 tree
1442 size_binop (code, arg0, arg1)
1443      enum tree_code code;
1444      tree arg0, arg1;
1445 {
1446   /* Handle the special case of two integer constants faster.  */
1447   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1448     {
1449       /* And some specific cases even faster than that.  */
1450       if (code == PLUS_EXPR && integer_zerop (arg0))
1451         return arg1;
1452       else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1453                && integer_zerop (arg1))
1454         return arg0;
1455       else if (code == MULT_EXPR && integer_onep (arg0))
1456         return arg1;
1457
1458       /* Handle general case of two integer constants.  */
1459       return const_binop (code, arg0, arg1, 0);
1460     }
1461
1462   if (arg0 == error_mark_node || arg1 == error_mark_node)
1463     return error_mark_node;
1464
1465   return fold (build (code, sizetype, arg0, arg1));
1466 }
1467 \f
1468 /* Given T, a tree representing type conversion of ARG1, a constant,
1469    return a constant tree representing the result of conversion.  */
1470
1471 static tree
1472 fold_convert (t, arg1)
1473      register tree t;
1474      register tree arg1;
1475 {
1476   register tree type = TREE_TYPE (t);
1477   int overflow = 0;
1478
1479   if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
1480     {
1481       if (TREE_CODE (arg1) == INTEGER_CST)
1482         {
1483           /* If we would build a constant wider than GCC supports,
1484              leave the conversion unfolded.  */
1485           if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1486             return t;
1487
1488           /* Given an integer constant, make new constant with new type,
1489              appropriately sign-extended or truncated.  */
1490           t = build_int_2 (TREE_INT_CST_LOW (arg1),
1491                            TREE_INT_CST_HIGH (arg1));
1492           TREE_TYPE (t) = type;
1493           /* Indicate an overflow if (1) ARG1 already overflowed,
1494              or (2) force_fit_type indicates an overflow.
1495              Tell force_fit_type that an overflow has already occurred
1496              if ARG1 is a too-large unsigned value and T is signed.  */
1497           TREE_OVERFLOW (t)
1498             = (TREE_OVERFLOW (arg1)
1499                | force_fit_type (t,
1500                                  (TREE_INT_CST_HIGH (arg1) < 0
1501                                   & (TREE_UNSIGNED (type)
1502                                      < TREE_UNSIGNED (TREE_TYPE (arg1))))));
1503           TREE_CONSTANT_OVERFLOW (t)
1504             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1505         }
1506 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1507       else if (TREE_CODE (arg1) == REAL_CST)
1508         {
1509           /* Don't initialize these, use assignments.
1510              Initialized local aggregates don't work on old compilers.  */
1511           REAL_VALUE_TYPE x;
1512           REAL_VALUE_TYPE l;
1513           REAL_VALUE_TYPE u;
1514           tree type1 = TREE_TYPE (arg1);
1515
1516           x = TREE_REAL_CST (arg1);
1517           l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
1518           u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
1519           /* See if X will be in range after truncation towards 0.
1520              To compensate for truncation, move the bounds away from 0,
1521              but reject if X exactly equals the adjusted bounds.  */
1522 #ifdef REAL_ARITHMETIC
1523           REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1524           REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1525 #else
1526           l--;
1527           u++;
1528 #endif
1529           /* If X is a NaN, use zero instead and show we have an overflow.
1530              Otherwise, range check.  */
1531           if (REAL_VALUE_ISNAN (x))
1532             overflow = 1, x = dconst0;
1533           else if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
1534             overflow = 1;
1535
1536 #ifndef REAL_ARITHMETIC
1537           {
1538             HOST_WIDE_INT low, high;
1539             HOST_WIDE_INT half_word
1540               = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
1541
1542             if (x < 0)
1543               x = -x;
1544
1545             high = (HOST_WIDE_INT) (x / half_word / half_word);
1546             x -= (REAL_VALUE_TYPE) high * half_word * half_word;
1547             if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
1548               {
1549                 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
1550                 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
1551               }
1552             else
1553               low = (HOST_WIDE_INT) x;
1554             if (TREE_REAL_CST (arg1) < 0)
1555               neg_double (low, high, &low, &high);
1556             t = build_int_2 (low, high);
1557           }
1558 #else
1559           {
1560             HOST_WIDE_INT low, high;
1561             REAL_VALUE_TO_INT (&low, &high, x);
1562             t = build_int_2 (low, high);
1563           }
1564 #endif
1565           TREE_TYPE (t) = type;
1566           TREE_OVERFLOW (t)
1567             = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1568           TREE_CONSTANT_OVERFLOW (t)
1569             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1570         }
1571 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1572       TREE_TYPE (t) = type;
1573     }
1574   else if (TREE_CODE (type) == REAL_TYPE)
1575     {
1576 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1577       if (TREE_CODE (arg1) == INTEGER_CST)
1578         return build_real_from_int_cst (type, arg1);
1579 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1580       if (TREE_CODE (arg1) == REAL_CST)
1581         {
1582           if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1583             {
1584               t = arg1;
1585               TREE_TYPE (arg1) = type;
1586               return t;
1587             }
1588           else if (setjmp (float_error))
1589             {
1590               overflow = 1;
1591               t = copy_node (arg1);
1592               goto got_it;
1593             }
1594           set_float_handler (float_error);
1595
1596           t = build_real (type, real_value_truncate (TYPE_MODE (type),
1597                                                      TREE_REAL_CST (arg1)));
1598           set_float_handler (NULL_PTR);
1599
1600         got_it:
1601           TREE_OVERFLOW (t)
1602             = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1603           TREE_CONSTANT_OVERFLOW (t)
1604             = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1605           return t;
1606         }
1607     }
1608   TREE_CONSTANT (t) = 1;
1609   return t;
1610 }
1611 \f
1612 /* Return an expr equal to X but certainly not valid as an lvalue.
1613    Also make sure it is not valid as an null pointer constant.  */
1614
1615 tree
1616 non_lvalue (x)
1617      tree x;
1618 {
1619   tree result;
1620
1621   /* These things are certainly not lvalues.  */
1622   if (TREE_CODE (x) == NON_LVALUE_EXPR
1623       || TREE_CODE (x) == INTEGER_CST
1624       || TREE_CODE (x) == REAL_CST
1625       || TREE_CODE (x) == STRING_CST
1626       || TREE_CODE (x) == ADDR_EXPR)
1627     {
1628       if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
1629         {
1630           /* Use NOP_EXPR instead of NON_LVALUE_EXPR
1631              so convert_for_assignment won't strip it.
1632              This is so this 0 won't be treated as a null pointer constant.  */
1633           result = build1 (NOP_EXPR, TREE_TYPE (x), x);
1634           TREE_CONSTANT (result) = TREE_CONSTANT (x);
1635           return result;
1636         }
1637       return x;
1638     }
1639
1640   result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1641   TREE_CONSTANT (result) = TREE_CONSTANT (x);
1642   return result;
1643 }
1644
1645 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
1646    Zero means allow extended lvalues.  */
1647
1648 int pedantic_lvalues;
1649
1650 /* When pedantic, return an expr equal to X but certainly not valid as a
1651    pedantic lvalue.  Otherwise, return X.  */
1652
1653 tree
1654 pedantic_non_lvalue (x)
1655      tree x;
1656 {
1657   if (pedantic_lvalues)
1658     return non_lvalue (x);
1659   else
1660     return x;
1661 }
1662 \f
1663 /* Given a tree comparison code, return the code that is the logical inverse
1664    of the given code.  It is not safe to do this for floating-point
1665    comparisons, except for NE_EXPR and EQ_EXPR.  */
1666
1667 static enum tree_code
1668 invert_tree_comparison (code)
1669      enum tree_code code;
1670 {
1671   switch (code)
1672     {
1673     case EQ_EXPR:
1674       return NE_EXPR;
1675     case NE_EXPR:
1676       return EQ_EXPR;
1677     case GT_EXPR:
1678       return LE_EXPR;
1679     case GE_EXPR:
1680       return LT_EXPR;
1681     case LT_EXPR:
1682       return GE_EXPR;
1683     case LE_EXPR:
1684       return GT_EXPR;
1685     default:
1686       abort ();
1687     }
1688 }
1689
1690 /* Similar, but return the comparison that results if the operands are
1691    swapped.  This is safe for floating-point.  */
1692
1693 static enum tree_code
1694 swap_tree_comparison (code)
1695      enum tree_code code;
1696 {
1697   switch (code)
1698     {
1699     case EQ_EXPR:
1700     case NE_EXPR:
1701       return code;
1702     case GT_EXPR:
1703       return LT_EXPR;
1704     case GE_EXPR:
1705       return LE_EXPR;
1706     case LT_EXPR:
1707       return GT_EXPR;
1708     case LE_EXPR:
1709       return GE_EXPR;
1710     default:
1711       abort ();
1712     }
1713 }
1714
1715 /* Return nonzero if CODE is a tree code that represents a truth value.  */
1716
1717 static int
1718 truth_value_p (code)
1719      enum tree_code code;
1720 {
1721   return (TREE_CODE_CLASS (code) == '<'
1722           || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
1723           || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
1724           || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
1725 }
1726 \f
1727 /* Return nonzero if two operands are necessarily equal.
1728    If ONLY_CONST is non-zero, only return non-zero for constants.
1729    This function tests whether the operands are indistinguishable;
1730    it does not test whether they are equal using C's == operation.
1731    The distinction is important for IEEE floating point, because
1732    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1733    (2) two NaNs may be indistinguishable, but NaN!=NaN.  */
1734
1735 int
1736 operand_equal_p (arg0, arg1, only_const)
1737      tree arg0, arg1;
1738      int only_const;
1739 {
1740   /* If both types don't have the same signedness, then we can't consider
1741      them equal.  We must check this before the STRIP_NOPS calls
1742      because they may change the signedness of the arguments.  */
1743   if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
1744     return 0;
1745
1746   STRIP_NOPS (arg0);
1747   STRIP_NOPS (arg1);
1748
1749   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1750      We don't care about side effects in that case because the SAVE_EXPR
1751      takes care of that for us.  */
1752   if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
1753     return ! only_const;
1754
1755   if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
1756     return 0;
1757
1758   if (TREE_CODE (arg0) == TREE_CODE (arg1)
1759       && TREE_CODE (arg0) == ADDR_EXPR
1760       && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
1761     return 1;
1762
1763   if (TREE_CODE (arg0) == TREE_CODE (arg1)
1764       && TREE_CODE (arg0) == INTEGER_CST
1765       && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
1766       && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
1767     return 1;
1768
1769   /* Detect when real constants are equal.  */
1770   if (TREE_CODE (arg0) == TREE_CODE (arg1)
1771       && TREE_CODE (arg0) == REAL_CST)
1772     return REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), TREE_REAL_CST (arg1));
1773
1774   if (only_const)
1775     return 0;
1776
1777   if (arg0 == arg1)
1778     return 1;
1779
1780   if (TREE_CODE (arg0) != TREE_CODE (arg1))
1781     return 0;
1782   /* This is needed for conversions and for COMPONENT_REF.
1783      Might as well play it safe and always test this.  */
1784   if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
1785     return 0;
1786
1787   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
1788     {
1789     case '1':
1790       /* Two conversions are equal only if signedness and modes match.  */
1791       if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
1792           && (TREE_UNSIGNED (TREE_TYPE (arg0))
1793               != TREE_UNSIGNED (TREE_TYPE (arg1))))
1794         return 0;
1795
1796       return operand_equal_p (TREE_OPERAND (arg0, 0),
1797                               TREE_OPERAND (arg1, 0), 0);
1798
1799     case '<':
1800     case '2':
1801       return (operand_equal_p (TREE_OPERAND (arg0, 0),
1802                                TREE_OPERAND (arg1, 0), 0)
1803               && operand_equal_p (TREE_OPERAND (arg0, 1),
1804                                   TREE_OPERAND (arg1, 1), 0));
1805
1806     case 'r':
1807       switch (TREE_CODE (arg0))
1808         {
1809         case INDIRECT_REF:
1810           return operand_equal_p (TREE_OPERAND (arg0, 0),
1811                                   TREE_OPERAND (arg1, 0), 0);
1812
1813         case COMPONENT_REF:
1814         case ARRAY_REF:
1815           return (operand_equal_p (TREE_OPERAND (arg0, 0),
1816                                    TREE_OPERAND (arg1, 0), 0)
1817                   && operand_equal_p (TREE_OPERAND (arg0, 1),
1818                                       TREE_OPERAND (arg1, 1), 0));
1819
1820         case BIT_FIELD_REF:
1821           return (operand_equal_p (TREE_OPERAND (arg0, 0),
1822                                    TREE_OPERAND (arg1, 0), 0)
1823                   && operand_equal_p (TREE_OPERAND (arg0, 1),
1824                                       TREE_OPERAND (arg1, 1), 0)
1825                   && operand_equal_p (TREE_OPERAND (arg0, 2),
1826                                       TREE_OPERAND (arg1, 2), 0));
1827         }
1828       break;
1829     }
1830
1831   return 0;
1832 }
1833 \f
1834 /* Similar to operand_equal_p, but see if ARG0 might have been made by
1835    shorten_compare from ARG1 when ARG1 was being compared with OTHER. 
1836
1837    When in doubt, return 0.  */
1838
1839 static int 
1840 operand_equal_for_comparison_p (arg0, arg1, other)
1841      tree arg0, arg1;
1842      tree other;
1843 {
1844   int unsignedp1, unsignedpo;
1845   tree primarg1, primother;
1846   unsigned correct_width;
1847
1848   if (operand_equal_p (arg0, arg1, 0))
1849     return 1;
1850
1851   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
1852       || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
1853     return 0;
1854
1855   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
1856      actual comparison operand, ARG0.
1857
1858      First throw away any conversions to wider types
1859      already present in the operands.  */
1860
1861   primarg1 = get_narrower (arg1, &unsignedp1);
1862   primother = get_narrower (other, &unsignedpo);
1863
1864   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
1865   if (unsignedp1 == unsignedpo
1866       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
1867       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
1868     {
1869       tree type = TREE_TYPE (arg0);
1870
1871       /* Make sure shorter operand is extended the right way
1872          to match the longer operand.  */
1873       primarg1 = convert (signed_or_unsigned_type (unsignedp1,
1874                                                   TREE_TYPE (primarg1)),
1875                          primarg1);
1876
1877       if (operand_equal_p (arg0, convert (type, primarg1), 0))
1878         return 1;
1879     }
1880
1881   return 0;
1882 }
1883 \f
1884 /* See if ARG is an expression that is either a comparison or is performing
1885    arithmetic on comparisons.  The comparisons must only be comparing
1886    two different values, which will be stored in *CVAL1 and *CVAL2; if
1887    they are non-zero it means that some operands have already been found.
1888    No variables may be used anywhere else in the expression except in the
1889    comparisons.  If SAVE_P is true it means we removed a SAVE_EXPR around
1890    the expression and save_expr needs to be called with CVAL1 and CVAL2.
1891
1892    If this is true, return 1.  Otherwise, return zero.  */
1893
1894 static int
1895 twoval_comparison_p (arg, cval1, cval2, save_p)
1896      tree arg;
1897      tree *cval1, *cval2;
1898      int *save_p;
1899 {
1900   enum tree_code code = TREE_CODE (arg);
1901   char class = TREE_CODE_CLASS (code);
1902
1903   /* We can handle some of the 'e' cases here.  */
1904   if (class == 'e' && code == TRUTH_NOT_EXPR)
1905     class = '1';
1906   else if (class == 'e'
1907            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
1908                || code == COMPOUND_EXPR))
1909     class = '2';
1910
1911   /* ??? Disable this since the SAVE_EXPR might already be in use outside
1912      the expression.  There may be no way to make this work, but it needs
1913      to be looked at again for 2.6.  */
1914 #if 0
1915   else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)
1916     {
1917       /* If we've already found a CVAL1 or CVAL2, this expression is
1918          two complex to handle.  */
1919       if (*cval1 || *cval2)
1920         return 0;
1921
1922       class = '1';
1923       *save_p = 1;
1924     }
1925 #endif
1926
1927   switch (class)
1928     {
1929     case '1':
1930       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
1931
1932     case '2':
1933       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
1934               && twoval_comparison_p (TREE_OPERAND (arg, 1),
1935                                       cval1, cval2, save_p));
1936
1937     case 'c':
1938       return 1;
1939
1940     case 'e':
1941       if (code == COND_EXPR)
1942         return (twoval_comparison_p (TREE_OPERAND (arg, 0),
1943                                      cval1, cval2, save_p)
1944                 && twoval_comparison_p (TREE_OPERAND (arg, 1),
1945                                         cval1, cval2, save_p)
1946                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
1947                                         cval1, cval2, save_p));
1948       return 0;
1949           
1950     case '<':
1951       /* First see if we can handle the first operand, then the second.  For
1952          the second operand, we know *CVAL1 can't be zero.  It must be that
1953          one side of the comparison is each of the values; test for the
1954          case where this isn't true by failing if the two operands
1955          are the same.  */
1956
1957       if (operand_equal_p (TREE_OPERAND (arg, 0),
1958                            TREE_OPERAND (arg, 1), 0))
1959         return 0;
1960
1961       if (*cval1 == 0)
1962         *cval1 = TREE_OPERAND (arg, 0);
1963       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
1964         ;
1965       else if (*cval2 == 0)
1966         *cval2 = TREE_OPERAND (arg, 0);
1967       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
1968         ;
1969       else
1970         return 0;
1971
1972       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
1973         ;
1974       else if (*cval2 == 0)
1975         *cval2 = TREE_OPERAND (arg, 1);
1976       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
1977         ;
1978       else
1979         return 0;
1980
1981       return 1;
1982     }
1983
1984   return 0;
1985 }
1986 \f
1987 /* ARG is a tree that is known to contain just arithmetic operations and
1988    comparisons.  Evaluate the operations in the tree substituting NEW0 for
1989    any occurrence of OLD0 as an operand of a comparison and likewise for
1990    NEW1 and OLD1.  */
1991
1992 static tree
1993 eval_subst (arg, old0, new0, old1, new1)
1994      tree arg;
1995      tree old0, new0, old1, new1;
1996 {
1997   tree type = TREE_TYPE (arg);
1998   enum tree_code code = TREE_CODE (arg);
1999   char class = TREE_CODE_CLASS (code);
2000
2001   /* We can handle some of the 'e' cases here.  */
2002   if (class == 'e' && code == TRUTH_NOT_EXPR)
2003     class = '1';
2004   else if (class == 'e'
2005            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2006     class = '2';
2007
2008   switch (class)
2009     {
2010     case '1':
2011       return fold (build1 (code, type,
2012                            eval_subst (TREE_OPERAND (arg, 0),
2013                                        old0, new0, old1, new1)));
2014
2015     case '2':
2016       return fold (build (code, type,
2017                           eval_subst (TREE_OPERAND (arg, 0),
2018                                       old0, new0, old1, new1),
2019                           eval_subst (TREE_OPERAND (arg, 1),
2020                                       old0, new0, old1, new1)));
2021
2022     case 'e':
2023       switch (code)
2024         {
2025         case SAVE_EXPR:
2026           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2027
2028         case COMPOUND_EXPR:
2029           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2030
2031         case COND_EXPR:
2032           return fold (build (code, type,
2033                               eval_subst (TREE_OPERAND (arg, 0),
2034                                           old0, new0, old1, new1),
2035                               eval_subst (TREE_OPERAND (arg, 1),
2036                                           old0, new0, old1, new1),
2037                               eval_subst (TREE_OPERAND (arg, 2),
2038                                           old0, new0, old1, new1)));
2039         }
2040
2041     case '<':
2042       {
2043         tree arg0 = TREE_OPERAND (arg, 0);
2044         tree arg1 = TREE_OPERAND (arg, 1);
2045
2046         /* We need to check both for exact equality and tree equality.  The
2047            former will be true if the operand has a side-effect.  In that
2048            case, we know the operand occurred exactly once.  */
2049
2050         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2051           arg0 = new0;
2052         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2053           arg0 = new1;
2054
2055         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2056           arg1 = new0;
2057         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2058           arg1 = new1;
2059
2060         return fold (build (code, type, arg0, arg1));
2061       }
2062     }
2063
2064   return arg;
2065 }
2066 \f
2067 /* Return a tree for the case when the result of an expression is RESULT
2068    converted to TYPE and OMITTED was previously an operand of the expression
2069    but is now not needed (e.g., we folded OMITTED * 0).
2070
2071    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2072    the conversion of RESULT to TYPE.  */
2073
2074 static tree
2075 omit_one_operand (type, result, omitted)
2076      tree type, result, omitted;
2077 {
2078   tree t = convert (type, result);
2079
2080   if (TREE_SIDE_EFFECTS (omitted))
2081     return build (COMPOUND_EXPR, type, omitted, t);
2082
2083   return non_lvalue (t);
2084 }
2085
2086 /* Similar, but call pedantic_non_lvalue instead of non_lvalue.  */
2087
2088 static tree
2089 pedantic_omit_one_operand (type, result, omitted)
2090      tree type, result, omitted;
2091 {
2092   tree t = convert (type, result);
2093
2094   if (TREE_SIDE_EFFECTS (omitted))
2095     return build (COMPOUND_EXPR, type, omitted, t);
2096
2097   return pedantic_non_lvalue (t);
2098 }
2099
2100
2101 \f
2102 /* Return a simplified tree node for the truth-negation of ARG.  This
2103    never alters ARG itself.  We assume that ARG is an operation that
2104    returns a truth value (0 or 1).  */
2105
2106 tree
2107 invert_truthvalue (arg)
2108      tree arg;
2109 {
2110   tree type = TREE_TYPE (arg);
2111   enum tree_code code = TREE_CODE (arg);
2112
2113   if (code == ERROR_MARK)
2114     return arg;
2115
2116   /* If this is a comparison, we can simply invert it, except for
2117      floating-point non-equality comparisons, in which case we just
2118      enclose a TRUTH_NOT_EXPR around what we have.  */
2119
2120   if (TREE_CODE_CLASS (code) == '<')
2121     {
2122       if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2123           && code != NE_EXPR && code != EQ_EXPR)
2124         return build1 (TRUTH_NOT_EXPR, type, arg);
2125       else
2126         return build (invert_tree_comparison (code), type,
2127                       TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2128     }
2129
2130   switch (code)
2131     {
2132     case INTEGER_CST:
2133       return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
2134                                          && TREE_INT_CST_HIGH (arg) == 0, 0));
2135
2136     case TRUTH_AND_EXPR:
2137       return build (TRUTH_OR_EXPR, type,
2138                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2139                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2140
2141     case TRUTH_OR_EXPR:
2142       return build (TRUTH_AND_EXPR, type,
2143                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2144                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2145
2146     case TRUTH_XOR_EXPR:
2147       /* Here we can invert either operand.  We invert the first operand
2148          unless the second operand is a TRUTH_NOT_EXPR in which case our
2149          result is the XOR of the first operand with the inside of the
2150          negation of the second operand.  */
2151
2152       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2153         return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2154                       TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2155       else
2156         return build (TRUTH_XOR_EXPR, type,
2157                       invert_truthvalue (TREE_OPERAND (arg, 0)),
2158                       TREE_OPERAND (arg, 1));
2159
2160     case TRUTH_ANDIF_EXPR:
2161       return build (TRUTH_ORIF_EXPR, type,
2162                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2163                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2164
2165     case TRUTH_ORIF_EXPR:
2166       return build (TRUTH_ANDIF_EXPR, type,
2167                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2168                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2169
2170     case TRUTH_NOT_EXPR:
2171       return TREE_OPERAND (arg, 0);
2172
2173     case COND_EXPR:
2174       return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2175                     invert_truthvalue (TREE_OPERAND (arg, 1)),
2176                     invert_truthvalue (TREE_OPERAND (arg, 2)));
2177
2178     case COMPOUND_EXPR:
2179       return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2180                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2181
2182     case NON_LVALUE_EXPR:
2183       return invert_truthvalue (TREE_OPERAND (arg, 0));
2184
2185     case NOP_EXPR:
2186     case CONVERT_EXPR:
2187     case FLOAT_EXPR:
2188       return build1 (TREE_CODE (arg), type,
2189                      invert_truthvalue (TREE_OPERAND (arg, 0)));
2190
2191     case BIT_AND_EXPR:
2192       if (!integer_onep (TREE_OPERAND (arg, 1)))
2193         break;
2194       return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2195
2196     case SAVE_EXPR:
2197       return build1 (TRUTH_NOT_EXPR, type, arg);
2198
2199     case CLEANUP_POINT_EXPR:
2200       return build1 (CLEANUP_POINT_EXPR, type,
2201                      invert_truthvalue (TREE_OPERAND (arg, 0)));
2202     }
2203   if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2204     abort ();
2205   return build1 (TRUTH_NOT_EXPR, type, arg);
2206 }
2207
2208 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2209    operands are another bit-wise operation with a common input.  If so,
2210    distribute the bit operations to save an operation and possibly two if
2211    constants are involved.  For example, convert
2212         (A | B) & (A | C) into A | (B & C)
2213    Further simplification will occur if B and C are constants.
2214
2215    If this optimization cannot be done, 0 will be returned.  */
2216
2217 static tree
2218 distribute_bit_expr (code, type, arg0, arg1)
2219      enum tree_code code;
2220      tree type;
2221      tree arg0, arg1;
2222 {
2223   tree common;
2224   tree left, right;
2225
2226   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2227       || TREE_CODE (arg0) == code
2228       || (TREE_CODE (arg0) != BIT_AND_EXPR
2229           && TREE_CODE (arg0) != BIT_IOR_EXPR))
2230     return 0;
2231
2232   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2233     {
2234       common = TREE_OPERAND (arg0, 0);
2235       left = TREE_OPERAND (arg0, 1);
2236       right = TREE_OPERAND (arg1, 1);
2237     }
2238   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2239     {
2240       common = TREE_OPERAND (arg0, 0);
2241       left = TREE_OPERAND (arg0, 1);
2242       right = TREE_OPERAND (arg1, 0);
2243     }
2244   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2245     {
2246       common = TREE_OPERAND (arg0, 1);
2247       left = TREE_OPERAND (arg0, 0);
2248       right = TREE_OPERAND (arg1, 1);
2249     }
2250   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2251     {
2252       common = TREE_OPERAND (arg0, 1);
2253       left = TREE_OPERAND (arg0, 0);
2254       right = TREE_OPERAND (arg1, 0);
2255     }
2256   else
2257     return 0;
2258
2259   return fold (build (TREE_CODE (arg0), type, common,
2260                       fold (build (code, type, left, right))));
2261 }
2262 \f
2263 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2264    starting at BITPOS.  The field is unsigned if UNSIGNEDP is non-zero.  */
2265
2266 static tree
2267 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2268      tree inner;
2269      tree type;
2270      int bitsize, bitpos;
2271      int unsignedp;
2272 {
2273   tree result = build (BIT_FIELD_REF, type, inner,
2274                        size_int (bitsize), size_int (bitpos));
2275
2276   TREE_UNSIGNED (result) = unsignedp;
2277
2278   return result;
2279 }
2280
2281 /* Optimize a bit-field compare.
2282
2283    There are two cases:  First is a compare against a constant and the
2284    second is a comparison of two items where the fields are at the same
2285    bit position relative to the start of a chunk (byte, halfword, word)
2286    large enough to contain it.  In these cases we can avoid the shift
2287    implicit in bitfield extractions.
2288
2289    For constants, we emit a compare of the shifted constant with the
2290    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2291    compared.  For two fields at the same position, we do the ANDs with the
2292    similar mask and compare the result of the ANDs.
2293
2294    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2295    COMPARE_TYPE is the type of the comparison, and LHS and RHS
2296    are the left and right operands of the comparison, respectively.
2297
2298    If the optimization described above can be done, we return the resulting
2299    tree.  Otherwise we return zero.  */
2300
2301 static tree
2302 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2303      enum tree_code code;
2304      tree compare_type;
2305      tree lhs, rhs;
2306 {
2307   int lbitpos, lbitsize, rbitpos, rbitsize;
2308   int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
2309   tree type = TREE_TYPE (lhs);
2310   tree signed_type, unsigned_type;
2311   int const_p = TREE_CODE (rhs) == INTEGER_CST;
2312   enum machine_mode lmode, rmode, lnmode, rnmode;
2313   int lunsignedp, runsignedp;
2314   int lvolatilep = 0, rvolatilep = 0;
2315   tree linner, rinner;
2316   tree mask;
2317   tree offset;
2318
2319   /* Get all the information about the extractions being done.  If the bit size
2320      if the same as the size of the underlying object, we aren't doing an
2321      extraction at all and so can do nothing.  */
2322   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2323                                 &lunsignedp, &lvolatilep);
2324   if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2325       || offset != 0)
2326     return 0;
2327
2328  if (!const_p)
2329    {
2330      /* If this is not a constant, we can only do something if bit positions,
2331         sizes, and signedness are the same.   */
2332      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
2333                                    &rmode, &runsignedp, &rvolatilep);
2334
2335      if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2336          || lunsignedp != runsignedp || offset != 0)
2337        return 0;
2338    }
2339
2340   /* See if we can find a mode to refer to this field.  We should be able to,
2341      but fail if we can't.  */
2342   lnmode = get_best_mode (lbitsize, lbitpos,
2343                           TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
2344                           lvolatilep);
2345   if (lnmode == VOIDmode)
2346     return 0;
2347
2348   /* Set signed and unsigned types of the precision of this mode for the
2349      shifts below.  */
2350   signed_type = type_for_mode (lnmode, 0);
2351   unsigned_type = type_for_mode (lnmode, 1);
2352
2353   if (! const_p)
2354     {
2355       rnmode = get_best_mode (rbitsize, rbitpos, 
2356                               TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
2357                               rvolatilep);
2358       if (rnmode == VOIDmode)
2359         return 0;
2360     }
2361     
2362   /* Compute the bit position and size for the new reference and our offset
2363      within it. If the new reference is the same size as the original, we
2364      won't optimize anything, so return zero.  */
2365   lnbitsize = GET_MODE_BITSIZE (lnmode);
2366   lnbitpos = lbitpos & ~ (lnbitsize - 1);
2367   lbitpos -= lnbitpos;
2368   if (lnbitsize == lbitsize)
2369     return 0;
2370
2371   if (! const_p)
2372     {
2373       rnbitsize = GET_MODE_BITSIZE (rnmode);
2374       rnbitpos = rbitpos & ~ (rnbitsize - 1);
2375       rbitpos -= rnbitpos;
2376       if (rnbitsize == rbitsize)
2377         return 0;
2378     }
2379
2380   if (BYTES_BIG_ENDIAN)
2381     lbitpos = lnbitsize - lbitsize - lbitpos;
2382
2383   /* Make the mask to be used against the extracted field.  */
2384   mask = build_int_2 (~0, ~0);
2385   TREE_TYPE (mask) = unsigned_type;
2386   force_fit_type (mask, 0);
2387   mask = convert (unsigned_type, mask);
2388   mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
2389   mask = const_binop (RSHIFT_EXPR, mask,
2390                       size_int (lnbitsize - lbitsize - lbitpos), 0);
2391
2392   if (! const_p)
2393     /* If not comparing with constant, just rework the comparison
2394        and return.  */
2395     return build (code, compare_type,
2396                   build (BIT_AND_EXPR, unsigned_type,
2397                          make_bit_field_ref (linner, unsigned_type,
2398                                              lnbitsize, lnbitpos, 1),
2399                          mask),
2400                   build (BIT_AND_EXPR, unsigned_type,
2401                          make_bit_field_ref (rinner, unsigned_type,
2402                                              rnbitsize, rnbitpos, 1),
2403                          mask));
2404
2405   /* Otherwise, we are handling the constant case. See if the constant is too
2406      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
2407      this not only for its own sake, but to avoid having to test for this
2408      error case below.  If we didn't, we might generate wrong code.
2409
2410      For unsigned fields, the constant shifted right by the field length should
2411      be all zero.  For signed fields, the high-order bits should agree with 
2412      the sign bit.  */
2413
2414   if (lunsignedp)
2415     {
2416       if (! integer_zerop (const_binop (RSHIFT_EXPR,
2417                                         convert (unsigned_type, rhs),
2418                                         size_int (lbitsize), 0)))
2419         {
2420           warning ("comparison is always %s due to width of bitfield",
2421                    code == NE_EXPR ? "one" : "zero");
2422           return convert (compare_type,
2423                           (code == NE_EXPR
2424                            ? integer_one_node : integer_zero_node));
2425         }
2426     }
2427   else
2428     {
2429       tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2430                               size_int (lbitsize - 1), 0);
2431       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2432         {
2433           warning ("comparison is always %s due to width of bitfield",
2434                    code == NE_EXPR ? "one" : "zero");
2435           return convert (compare_type,
2436                           (code == NE_EXPR
2437                            ? integer_one_node : integer_zero_node));
2438         }
2439     }
2440
2441   /* Single-bit compares should always be against zero.  */
2442   if (lbitsize == 1 && ! integer_zerop (rhs))
2443     {
2444       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2445       rhs = convert (type, integer_zero_node);
2446     }
2447
2448   /* Make a new bitfield reference, shift the constant over the
2449      appropriate number of bits and mask it with the computed mask
2450      (in case this was a signed field).  If we changed it, make a new one.  */
2451   lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
2452   if (lvolatilep)
2453     {
2454       TREE_SIDE_EFFECTS (lhs) = 1;
2455       TREE_THIS_VOLATILE (lhs) = 1;
2456     }
2457
2458   rhs = fold (const_binop (BIT_AND_EXPR,
2459                            const_binop (LSHIFT_EXPR,
2460                                         convert (unsigned_type, rhs),
2461                                         size_int (lbitpos), 0),
2462                            mask, 0));
2463
2464   return build (code, compare_type,
2465                 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2466                 rhs);
2467 }
2468 \f
2469 /* Subroutine for fold_truthop: decode a field reference.
2470
2471    If EXP is a comparison reference, we return the innermost reference.
2472
2473    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2474    set to the starting bit number.
2475
2476    If the innermost field can be completely contained in a mode-sized
2477    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
2478
2479    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2480    otherwise it is not changed.
2481
2482    *PUNSIGNEDP is set to the signedness of the field.
2483
2484    *PMASK is set to the mask used.  This is either contained in a
2485    BIT_AND_EXPR or derived from the width of the field.
2486
2487    *PAND_MASK is set the the mask found in a BIT_AND_EXPR, if any.
2488
2489    Return 0 if this is not a component reference or is one that we can't
2490    do anything with.  */
2491
2492 static tree
2493 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2494                         pvolatilep, pmask, pand_mask)
2495      tree exp;
2496      int *pbitsize, *pbitpos;
2497      enum machine_mode *pmode;
2498      int *punsignedp, *pvolatilep;
2499      tree *pmask;
2500      tree *pand_mask;
2501 {
2502   tree and_mask = 0;
2503   tree mask, inner, offset;
2504   tree unsigned_type;
2505   int precision;
2506
2507   /* All the optimizations using this function assume integer fields.  
2508      There are problems with FP fields since the type_for_size call
2509      below can fail for, e.g., XFmode.  */
2510   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2511     return 0;
2512
2513   STRIP_NOPS (exp);
2514
2515   if (TREE_CODE (exp) == BIT_AND_EXPR)
2516     {
2517       and_mask = TREE_OPERAND (exp, 1);
2518       exp = TREE_OPERAND (exp, 0);
2519       STRIP_NOPS (exp); STRIP_NOPS (and_mask);
2520       if (TREE_CODE (and_mask) != INTEGER_CST)
2521         return 0;
2522     }
2523
2524
2525   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2526                                punsignedp, pvolatilep);
2527   if ((inner == exp && and_mask == 0)
2528       || *pbitsize < 0 || offset != 0)
2529     return 0;
2530   
2531   /* Compute the mask to access the bitfield.  */
2532   unsigned_type = type_for_size (*pbitsize, 1);
2533   precision = TYPE_PRECISION (unsigned_type);
2534
2535   mask = build_int_2 (~0, ~0);
2536   TREE_TYPE (mask) = unsigned_type;
2537   force_fit_type (mask, 0);
2538   mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2539   mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2540
2541   /* Merge it with the mask we found in the BIT_AND_EXPR, if any.  */
2542   if (and_mask != 0)
2543     mask = fold (build (BIT_AND_EXPR, unsigned_type,
2544                         convert (unsigned_type, and_mask), mask));
2545
2546   *pmask = mask;
2547   *pand_mask = and_mask;
2548   return inner;
2549 }
2550
2551 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
2552    bit positions.  */
2553
2554 static int
2555 all_ones_mask_p (mask, size)
2556      tree mask;
2557      int size;
2558 {
2559   tree type = TREE_TYPE (mask);
2560   int precision = TYPE_PRECISION (type);
2561   tree tmask;
2562
2563   tmask = build_int_2 (~0, ~0);
2564   TREE_TYPE (tmask) = signed_type (type);
2565   force_fit_type (tmask, 0);
2566   return
2567     tree_int_cst_equal (mask, 
2568                         const_binop (RSHIFT_EXPR,
2569                                      const_binop (LSHIFT_EXPR, tmask,
2570                                                   size_int (precision - size),
2571                                                   0),
2572                                      size_int (precision - size), 0));
2573 }
2574
2575 /* Subroutine for fold_truthop: determine if an operand is simple enough
2576    to be evaluated unconditionally.  */
2577
2578 static int 
2579 simple_operand_p (exp)
2580      tree exp;
2581 {
2582   /* Strip any conversions that don't change the machine mode.  */
2583   while ((TREE_CODE (exp) == NOP_EXPR
2584           || TREE_CODE (exp) == CONVERT_EXPR)
2585          && (TYPE_MODE (TREE_TYPE (exp))
2586              == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2587     exp = TREE_OPERAND (exp, 0);
2588
2589   return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2590           || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
2591               && ! TREE_ADDRESSABLE (exp)
2592               && ! TREE_THIS_VOLATILE (exp)
2593               && ! DECL_NONLOCAL (exp)
2594               /* Don't regard global variables as simple.  They may be
2595                  allocated in ways unknown to the compiler (shared memory,
2596                  #pragma weak, etc).  */
2597               && ! TREE_PUBLIC (exp)
2598               && ! DECL_EXTERNAL (exp)
2599               /* Loading a static variable is unduly expensive, but global
2600                  registers aren't expensive.  */
2601               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
2602 }
2603 \f
2604 /* The following functions are subroutines to fold_range_test and allow it to
2605    try to change a logical combination of comparisons into a range test.
2606
2607    For example, both
2608         X == 2 && X == 3 && X == 4 && X == 5
2609    and
2610         X >= 2 && X <= 5
2611    are converted to
2612         (unsigned) (X - 2) <= 3
2613
2614    We decribe each set of comparisons as being either inside or outside
2615    a range, using a variable named like IN_P, and then describe the
2616    range with a lower and upper bound.  If one of the bounds is omitted,
2617    it represents either the highest or lowest value of the type.
2618
2619    In the comments below, we represent a range by two numbers in brackets
2620    preceeded by a "+" to designate being inside that range, or a "-" to
2621    designate being outside that range, so the condition can be inverted by
2622    flipping the prefix.  An omitted bound is represented by a "-".  For
2623    example, "- [-, 10]" means being outside the range starting at the lowest
2624    possible value and ending at 10, in other words, being greater than 10.
2625    The range "+ [-, -]" is always true and hence the range "- [-, -]" is
2626    always false.
2627
2628    We set up things so that the missing bounds are handled in a consistent
2629    manner so neither a missing bound nor "true" and "false" need to be
2630    handled using a special case.  */
2631
2632 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
2633    of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
2634    and UPPER1_P are nonzero if the respective argument is an upper bound
2635    and zero for a lower.  TYPE, if nonzero, is the type of the result; it
2636    must be specified for a comparison.  ARG1 will be converted to ARG0's
2637    type if both are specified.  */
2638
2639 static tree
2640 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
2641      enum tree_code code;
2642      tree type;
2643      tree arg0, arg1;
2644      int upper0_p, upper1_p;
2645 {
2646   tree tem;
2647   int result;
2648   int sgn0, sgn1;
2649
2650   /* If neither arg represents infinity, do the normal operation.
2651      Else, if not a comparison, return infinity.  Else handle the special
2652      comparison rules. Note that most of the cases below won't occur, but
2653      are handled for consistency.  */
2654
2655   if (arg0 != 0 && arg1 != 0)
2656     {
2657       tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
2658                          arg0, convert (TREE_TYPE (arg0), arg1)));
2659       STRIP_NOPS (tem);
2660       return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
2661     }
2662
2663   if (TREE_CODE_CLASS (code) != '<')
2664     return 0;
2665
2666   /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
2667      for neither.  Then compute our result treating them as never equal
2668      and comparing bounds to non-bounds as above.  */
2669   sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
2670   sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
2671   switch (code)
2672     {
2673     case EQ_EXPR:  case NE_EXPR:
2674       result = (code == NE_EXPR);
2675       break;
2676     case LT_EXPR:  case LE_EXPR:
2677       result = sgn0 < sgn1;
2678       break;
2679     case GT_EXPR:  case GE_EXPR:
2680       result = sgn0 > sgn1;
2681       break;
2682     }
2683
2684   return convert (type, result ? integer_one_node : integer_zero_node);
2685 }
2686 \f      
2687 /* Given EXP, a logical expression, set the range it is testing into
2688    variables denoted by PIN_P, PLOW, and PHIGH.  Return the expression
2689    actually being tested.  *PLOW and *PHIGH will have be made the same type
2690    as the returned expression.  If EXP is not a comparison, we will most
2691    likely not be returning a useful value and range.  */
2692
2693 static tree
2694 make_range (exp, pin_p, plow, phigh)
2695      tree exp;
2696      int *pin_p;
2697      tree *plow, *phigh;
2698 {
2699   enum tree_code code;
2700   tree arg0, arg1, type;
2701   int in_p, n_in_p;
2702   tree low, high, n_low, n_high;
2703
2704   /* Start with simply saying "EXP != 0" and then look at the code of EXP
2705      and see if we can refine the range.  Some of the cases below may not
2706      happen, but it doesn't seem worth worrying about this.  We "continue"
2707      the outer loop when we've changed something; otherwise we "break"
2708      the switch, which will "break" the while.  */
2709
2710   in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
2711
2712   while (1)
2713     {
2714       code = TREE_CODE (exp);
2715       arg0 = TREE_OPERAND (exp, 0), arg1 = TREE_OPERAND (exp, 1);
2716       if (TREE_CODE_CLASS (code) == '<' || TREE_CODE_CLASS (code) == '1'
2717           || TREE_CODE_CLASS (code) == '2')
2718         type = TREE_TYPE (arg0);
2719
2720       switch (code)
2721         {
2722         case TRUTH_NOT_EXPR:
2723           in_p = ! in_p, exp = arg0;
2724           continue;
2725
2726         case EQ_EXPR: case NE_EXPR:
2727         case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
2728           /* We can only do something if the range is testing for zero
2729              and if the second operand is an integer constant.  Note that
2730              saying something is "in" the range we make is done by
2731              complementing IN_P since it will set in the initial case of
2732              being not equal to zero; "out" is leaving it alone.  */
2733           if (low == 0 || high == 0
2734               || ! integer_zerop (low) || ! integer_zerop (high)
2735               || TREE_CODE (arg1) != INTEGER_CST)
2736             break;
2737
2738           switch (code)
2739             {
2740             case NE_EXPR:  /* - [c, c]  */
2741               low = high = arg1;
2742               break;
2743             case EQ_EXPR:  /* + [c, c]  */
2744               in_p = ! in_p, low = high = arg1;
2745               break;
2746             case GT_EXPR:  /* - [-, c] */
2747               low = 0, high = arg1;
2748               break;
2749             case GE_EXPR:  /* + [c, -] */
2750               in_p = ! in_p, low = arg1, high = 0;
2751               break;
2752             case LT_EXPR:  /* - [c, -] */
2753               low = arg1, high = 0;
2754               break;
2755             case LE_EXPR:  /* + [-, c] */
2756               in_p = ! in_p, low = 0, high = arg1;
2757               break;
2758             }
2759
2760           exp = arg0;
2761
2762           /* If this is an unsigned comparison, we also know that EXP is
2763              greater than or equal to zero.  We base the range tests we make
2764              on that fact, so we record it here so we can parse existing
2765              range tests.  */
2766           if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
2767             {
2768               if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
2769                                   1, convert (type, integer_zero_node),
2770                                   NULL_TREE))
2771                 break;
2772
2773               in_p = n_in_p, low = n_low, high = n_high;
2774
2775               /* If the high bound is missing, reverse the range so it
2776                  goes from zero to the low bound minus 1.  */
2777               if (high == 0)
2778                 {
2779                   in_p = ! in_p;
2780                   high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
2781                                       integer_one_node, 0);
2782                   low = convert (type, integer_zero_node);
2783                 }
2784             }
2785           continue;
2786
2787         case NEGATE_EXPR:
2788           /* (-x) IN [a,b] -> x in [-b, -a]  */
2789           n_low = range_binop (MINUS_EXPR, type,
2790                                convert (type, integer_zero_node), 0, high, 1);
2791           n_high = range_binop (MINUS_EXPR, type,
2792                                 convert (type, integer_zero_node), 0, low, 0);
2793           low = n_low, high = n_high;
2794           exp = arg0;
2795           continue;
2796
2797         case BIT_NOT_EXPR:
2798           /* ~ X -> -X - 1  */
2799           exp = build (MINUS_EXPR, type, build1 (NEGATE_EXPR, type, arg0),
2800                        convert (type, integer_one_node));
2801           continue;
2802
2803         case PLUS_EXPR:  case MINUS_EXPR:
2804           if (TREE_CODE (arg1) != INTEGER_CST)
2805             break;
2806
2807           /* If EXP is signed, any overflow in the computation is undefined,
2808              so we don't worry about it so long as our computations on
2809              the bounds don't overflow.  For unsigned, overflow is defined
2810              and this is exactly the right thing.  */
2811           n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
2812                                type, low, 0, arg1, 0);
2813           n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
2814                                 type, high, 1, arg1, 0);
2815           if ((n_low != 0 && TREE_OVERFLOW (n_low))
2816               || (n_high != 0 && TREE_OVERFLOW (n_high)))
2817             break;
2818
2819           /* Check for an unsigned range which has wrapped around the maximum
2820              value thus making n_high < n_low, and normalize it.  */
2821           if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
2822             {
2823               low = range_binop (PLUS_EXPR, type, n_high, 0,
2824                                  integer_one_node, 0);
2825               high = range_binop (MINUS_EXPR, type, n_low, 0,
2826                                  integer_one_node, 0);
2827               in_p = ! in_p;
2828             }
2829           else
2830             low = n_low, high = n_high;
2831
2832           exp = arg0;
2833           continue;
2834
2835         case NOP_EXPR:  case NON_LVALUE_EXPR:  case CONVERT_EXPR:
2836           if (! INTEGRAL_TYPE_P (type)
2837               || (low != 0 && ! int_fits_type_p (low, type))
2838               || (high != 0 && ! int_fits_type_p (high, type)))
2839             break;
2840
2841           if (low != 0)
2842             low = convert (type, low);
2843
2844           if (high != 0)
2845             high = convert (type, high);
2846
2847           exp = arg0;
2848           continue;
2849         }
2850
2851       break;
2852     }
2853
2854   /* If EXP is a constant, we can evaluate whether this is true or false.  */
2855   if (TREE_CODE (exp) == INTEGER_CST)
2856     {
2857       in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
2858                                                  exp, 0, low, 0))
2859                       && integer_onep (range_binop (LE_EXPR, integer_type_node,
2860                                                     exp, 1, high, 1)));
2861       low = high = 0;
2862       exp = 0;
2863     }
2864
2865   *pin_p = in_p, *plow = low, *phigh = high;
2866   return exp;
2867 }
2868 \f
2869 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
2870    type, TYPE, return an expression to test if EXP is in (or out of, depending
2871    on IN_P) the range.  */
2872
2873 static tree
2874 build_range_check (type, exp, in_p, low, high)
2875      tree type;
2876      tree exp;
2877      int in_p;
2878      tree low, high;
2879 {
2880   tree etype = TREE_TYPE (exp);
2881   tree utype, value;
2882
2883   if (! in_p
2884       && (0 != (value = build_range_check (type, exp, 1, low, high))))
2885     return invert_truthvalue (value);
2886
2887   else if (low == 0 && high == 0)
2888     return convert (type, integer_one_node);
2889
2890   else if (low == 0)
2891     return fold (build (LE_EXPR, type, exp, high));
2892
2893   else if (high == 0)
2894     return fold (build (GE_EXPR, type, exp, low));
2895
2896   else if (operand_equal_p (low, high, 0))
2897     return fold (build (EQ_EXPR, type, exp, low));
2898
2899   else if (TREE_UNSIGNED (etype) && integer_zerop (low))
2900     return build_range_check (type, exp, 1, 0, high);
2901
2902   else if (integer_zerop (low))
2903     {
2904       utype = unsigned_type (etype);
2905       return build_range_check (type, convert (utype, exp), 1, 0,
2906                                 convert (utype, high));
2907     }
2908
2909   else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
2910            && ! TREE_OVERFLOW (value))
2911     return build_range_check (type,
2912                               fold (build (MINUS_EXPR, etype, exp, low)),
2913                               1, convert (etype, integer_zero_node), value);
2914   else
2915     return 0;
2916 }
2917 \f
2918 /* Given two ranges, see if we can merge them into one.  Return 1 if we 
2919    can, 0 if we can't.  Set the output range into the specified parameters.  */
2920
2921 static int
2922 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
2923      int *pin_p;
2924      tree *plow, *phigh;
2925      int in0_p, in1_p;
2926      tree low0, high0, low1, high1;
2927 {
2928   int no_overlap;
2929   int subset;
2930   int temp;
2931   tree tem;
2932   int in_p;
2933   tree low, high;
2934
2935   /* Make range 0 be the range that starts first.  Swap them if it isn't.  */
2936   if (integer_onep (range_binop (GT_EXPR, integer_type_node, 
2937                                  low0, 0, low1, 0))
2938       || (((low0 == 0 && low1 == 0)
2939            || integer_onep (range_binop (EQ_EXPR, integer_type_node,
2940                                          low0, 0, low1, 0)))
2941           && integer_onep (range_binop (GT_EXPR, integer_type_node,
2942                                         high0, 1, high1, 1))))
2943     {
2944       temp = in0_p, in0_p = in1_p, in1_p = temp;
2945       tem = low0, low0 = low1, low1 = tem;
2946       tem = high0, high0 = high1, high1 = tem;
2947     }
2948
2949   /* Now flag two cases, whether the ranges are disjoint or whether the
2950      second range is totally subsumed in the first.  Note that the tests
2951      below are simplified by the ones above.  */
2952   no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
2953                                           high0, 1, low1, 0));
2954   subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
2955                                       high1, 1, high0, 1));
2956
2957   /* We now have four cases, depending on whether we are including or
2958      excluding the two ranges.  */
2959   if (in0_p && in1_p)
2960     {
2961       /* If they don't overlap, the result is false.  If the second range
2962          is a subset it is the result.  Otherwise, the range is from the start
2963          of the second to the end of the first.  */
2964       if (no_overlap)
2965         in_p = 0, low = high = 0;
2966       else if (subset)
2967         in_p = 1, low = low1, high = high1;
2968       else
2969         in_p = 1, low = low1, high = high0;
2970     }
2971
2972   else if (in0_p && ! in1_p)
2973     {
2974       /* If they don't overlap, the result is the first range.  If the
2975          second range is a subset of the first, we can't describe this as
2976          a single range unless both ranges end at the same place.  If both
2977          ranges also start in the same place, then the result is false.
2978          Otherwise, we go from the start of the first range to just before
2979          the start of the second.  */
2980       if (no_overlap)
2981         in_p = 1, low = low0, high = high0;
2982       else if (subset
2983                && integer_zerop (range_binop (EQ_EXPR, integer_type_node,
2984                                               high0, 1, high1, 0)))
2985         return 0;
2986       else if (subset
2987                && integer_onep (range_binop (EQ_EXPR, integer_type_node,
2988                                              low0, 0, low1, 0)))
2989         in_p = 0, low = high = 0;
2990       else
2991         {
2992           in_p = 1, low = low0;
2993           high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
2994                               integer_one_node, 0);
2995         }
2996     }
2997
2998   else if (! in0_p && in1_p)
2999     {
3000       /* If they don't overlap, the result is the second range.  If the second
3001          is a subset of the first, the result is false.  Otherwise,
3002          the range starts just after the first range and ends at the
3003          end of the second.  */
3004       if (no_overlap)
3005         in_p = 1, low = low1, high = high1;
3006       else if (subset)
3007         in_p = 0, low = high = 0;
3008       else
3009         {
3010           in_p = 1, high = high1;
3011           low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3012                              integer_one_node, 0);
3013         }
3014     }
3015
3016   else
3017     {
3018       /* The case where we are excluding both ranges.  Here the complex case
3019          is if they don't overlap.  In that case, the only time we have a
3020          range is if they are adjacent.  If the second is a subset of the
3021          first, the result is the first.  Otherwise, the range to exclude
3022          starts at the beginning of the first range and ends at the end of the
3023          second.  */
3024       if (no_overlap)
3025         {
3026           if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3027                                          range_binop (PLUS_EXPR, NULL_TREE,
3028                                                       high0, 1,
3029                                                       integer_one_node, 1),
3030                                          1, low1, 0)))
3031             in_p = 0, low = low0, high = high1;
3032           else
3033             return 0;
3034         }
3035       else if (subset)
3036         in_p = 0, low = low0, high = high0;
3037       else
3038         in_p = 0, low = low0, high = high1;
3039     }
3040
3041   *pin_p = in_p, *plow = low, *phigh = high;
3042   return 1;
3043 }
3044 \f
3045 /* EXP is some logical combination of boolean tests.  See if we can
3046    merge it into some range test.  Return the new tree if so.  */
3047
3048 static tree
3049 fold_range_test (exp)
3050      tree exp;
3051 {
3052   int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3053                || TREE_CODE (exp) == TRUTH_OR_EXPR);
3054   int in0_p, in1_p, in_p;
3055   tree low0, low1, low, high0, high1, high;
3056   tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3057   tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3058   tree tem;
3059
3060   /* If this is an OR operation, invert both sides; we will invert
3061      again at the end.  */
3062   if (or_op)
3063     in0_p = ! in0_p, in1_p = ! in1_p;
3064
3065   /* If both expressions are the same, if we can merge the ranges, and we
3066      can build the range test, return it or it inverted.  If one of the
3067      ranges is always true or always false, consider it to be the same
3068      expression as the other.  */
3069   if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3070       && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3071                        in1_p, low1, high1)
3072       && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3073                                          lhs != 0 ? lhs
3074                                          : rhs != 0 ? rhs : integer_zero_node,
3075                                          in_p, low, high))))
3076     return or_op ? invert_truthvalue (tem) : tem;
3077
3078   /* On machines where the branch cost is expensive, if this is a
3079      short-circuited branch and the underlying object on both sides
3080      is the same, make a non-short-circuit operation.  */
3081   else if (BRANCH_COST >= 2
3082            && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3083                || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3084            && operand_equal_p (lhs, rhs, 0))
3085     {
3086       /* If simple enough, just rewrite.  Otherwise, make a SAVE_EXPR.  */
3087       if (simple_operand_p (lhs))
3088         return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3089                       ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3090                       TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3091                       TREE_OPERAND (exp, 1));
3092       else
3093         {
3094           tree common = save_expr (lhs);
3095
3096           if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3097                                              or_op ? ! in0_p : in0_p,
3098                                              low0, high0))
3099               && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3100                                                  or_op ? ! in1_p : in1_p,
3101                                                  low1, high1))))
3102             return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3103                           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3104                           TREE_TYPE (exp), lhs, rhs);
3105         }
3106     }
3107   else
3108     return 0;
3109 }
3110 \f
3111 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3112    bit value.  Arrange things so the extra bits will be set to zero if and
3113    only if C is signed-extended to its full width.  If MASK is nonzero,
3114    it is an INTEGER_CST that should be AND'ed with the extra bits.  */
3115
3116 static tree
3117 unextend (c, p, unsignedp, mask)
3118      tree c;
3119      int p;
3120      int unsignedp;
3121      tree mask;
3122 {
3123   tree type = TREE_TYPE (c);
3124   int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3125   tree temp;
3126
3127   if (p == modesize || unsignedp)
3128     return c;
3129
3130   if (TREE_UNSIGNED (type))
3131     c = convert (signed_type (type), c);
3132
3133   /* We work by getting just the sign bit into the low-order bit, then
3134      into the high-order bit, then sign-extend.  We then XOR that value
3135      with C.  */
3136   temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3137   temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3138   temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3139   temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3140   if (mask != 0)
3141     temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3142
3143   return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3144 }
3145 \f
3146 /* Find ways of folding logical expressions of LHS and RHS:
3147    Try to merge two comparisons to the same innermost item.
3148    Look for range tests like "ch >= '0' && ch <= '9'".
3149    Look for combinations of simple terms on machines with expensive branches
3150    and evaluate the RHS unconditionally.
3151
3152    For example, if we have p->a == 2 && p->b == 4 and we can make an
3153    object large enough to span both A and B, we can do this with a comparison
3154    against the object ANDed with the a mask.
3155
3156    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3157    operations to do this with one comparison.
3158
3159    We check for both normal comparisons and the BIT_AND_EXPRs made this by
3160    function and the one above.
3161
3162    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
3163    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3164
3165    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3166    two operands.
3167
3168    We return the simplified tree or 0 if no optimization is possible.  */
3169
3170 static tree
3171 fold_truthop (code, truth_type, lhs, rhs)
3172      enum tree_code code;
3173      tree truth_type, lhs, rhs;
3174 {
3175   /* If this is the "or" of two comparisons, we can do something if we
3176      the comparisons are NE_EXPR.  If this is the "and", we can do something
3177      if the comparisons are EQ_EXPR.  I.e., 
3178         (a->b == 2 && a->c == 4) can become (a->new == NEW).
3179
3180      WANTED_CODE is this operation code.  For single bit fields, we can
3181      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3182      comparison for one-bit fields.  */
3183
3184   enum tree_code wanted_code;
3185   enum tree_code lcode, rcode;
3186   tree ll_arg, lr_arg, rl_arg, rr_arg;
3187   tree ll_inner, lr_inner, rl_inner, rr_inner;
3188   int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3189   int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3190   int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3191   int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3192   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3193   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3194   enum machine_mode lnmode, rnmode;
3195   tree ll_mask, lr_mask, rl_mask, rr_mask;
3196   tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3197   tree l_const, r_const;
3198   tree type, result;
3199   int first_bit, end_bit;
3200   int volatilep;
3201
3202   /* Start by getting the comparison codes.  Fail if anything is volatile.
3203      If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3204      it were surrounded with a NE_EXPR.  */
3205
3206   if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3207     return 0;
3208
3209   lcode = TREE_CODE (lhs);
3210   rcode = TREE_CODE (rhs);
3211
3212   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3213     lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3214
3215   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3216     rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3217
3218   if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3219     return 0;
3220
3221   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3222           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3223
3224   ll_arg = TREE_OPERAND (lhs, 0);
3225   lr_arg = TREE_OPERAND (lhs, 1);
3226   rl_arg = TREE_OPERAND (rhs, 0);
3227   rr_arg = TREE_OPERAND (rhs, 1);
3228   
3229   /* If the RHS can be evaluated unconditionally and its operands are
3230      simple, it wins to evaluate the RHS unconditionally on machines
3231      with expensive branches.  In this case, this isn't a comparison
3232      that can be merged.  */
3233
3234   /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
3235      are with zero (tmw).  */
3236
3237   if (BRANCH_COST >= 2
3238       && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
3239       && simple_operand_p (rl_arg)
3240       && simple_operand_p (rr_arg))
3241     return build (code, truth_type, lhs, rhs);
3242
3243   /* See if the comparisons can be merged.  Then get all the parameters for
3244      each side.  */
3245
3246   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3247       || (rcode != EQ_EXPR && rcode != NE_EXPR))
3248     return 0;
3249
3250   volatilep = 0;
3251   ll_inner = decode_field_reference (ll_arg,
3252                                      &ll_bitsize, &ll_bitpos, &ll_mode,
3253                                      &ll_unsignedp, &volatilep, &ll_mask,
3254                                      &ll_and_mask);
3255   lr_inner = decode_field_reference (lr_arg,
3256                                      &lr_bitsize, &lr_bitpos, &lr_mode,
3257                                      &lr_unsignedp, &volatilep, &lr_mask,
3258                                      &lr_and_mask);
3259   rl_inner = decode_field_reference (rl_arg,
3260                                      &rl_bitsize, &rl_bitpos, &rl_mode,
3261                                      &rl_unsignedp, &volatilep, &rl_mask,
3262                                      &rl_and_mask);
3263   rr_inner = decode_field_reference (rr_arg,
3264                                      &rr_bitsize, &rr_bitpos, &rr_mode,
3265                                      &rr_unsignedp, &volatilep, &rr_mask,
3266                                      &rr_and_mask);
3267
3268   /* It must be true that the inner operation on the lhs of each
3269      comparison must be the same if we are to be able to do anything.
3270      Then see if we have constants.  If not, the same must be true for
3271      the rhs's.  */
3272   if (volatilep || ll_inner == 0 || rl_inner == 0
3273       || ! operand_equal_p (ll_inner, rl_inner, 0))
3274     return 0;
3275
3276   if (TREE_CODE (lr_arg) == INTEGER_CST
3277       && TREE_CODE (rr_arg) == INTEGER_CST)
3278     l_const = lr_arg, r_const = rr_arg;
3279   else if (lr_inner == 0 || rr_inner == 0
3280            || ! operand_equal_p (lr_inner, rr_inner, 0))
3281     return 0;
3282   else
3283     l_const = r_const = 0;
3284
3285   /* If either comparison code is not correct for our logical operation,
3286      fail.  However, we can convert a one-bit comparison against zero into
3287      the opposite comparison against that bit being set in the field.  */
3288
3289   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
3290   if (lcode != wanted_code)
3291     {
3292       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
3293         l_const = ll_mask;
3294       else
3295         return 0;
3296     }
3297
3298   if (rcode != wanted_code)
3299     {
3300       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
3301         r_const = rl_mask;
3302       else
3303         return 0;
3304     }
3305
3306   /* See if we can find a mode that contains both fields being compared on
3307      the left.  If we can't, fail.  Otherwise, update all constants and masks
3308      to be relative to a field of that size.  */
3309   first_bit = MIN (ll_bitpos, rl_bitpos);
3310   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
3311   lnmode = get_best_mode (end_bit - first_bit, first_bit,
3312                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
3313                           volatilep);
3314   if (lnmode == VOIDmode)
3315     return 0;
3316
3317   lnbitsize = GET_MODE_BITSIZE (lnmode);
3318   lnbitpos = first_bit & ~ (lnbitsize - 1);
3319   type = type_for_size (lnbitsize, 1);
3320   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
3321
3322   if (BYTES_BIG_ENDIAN)
3323     {
3324       xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
3325       xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
3326     }
3327
3328   ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
3329                          size_int (xll_bitpos), 0);
3330   rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
3331                          size_int (xrl_bitpos), 0);
3332
3333   if (l_const)
3334     {
3335       l_const = convert (type, l_const);
3336       l_const = unextend (l_const,  ll_bitsize, ll_unsignedp, ll_and_mask);
3337       l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
3338       if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
3339                                         fold (build1 (BIT_NOT_EXPR,
3340                                                       type, ll_mask)),
3341                                         0)))
3342         {
3343           warning ("comparison is always %s",
3344                    wanted_code == NE_EXPR ? "one" : "zero");
3345           
3346           return convert (truth_type,
3347                           wanted_code == NE_EXPR
3348                           ? integer_one_node : integer_zero_node);
3349         }
3350     }
3351   if (r_const)
3352     {
3353       r_const = convert (type, r_const);
3354       r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
3355       r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
3356       if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
3357                                         fold (build1 (BIT_NOT_EXPR,
3358                                                       type, rl_mask)),
3359                                         0)))
3360         {
3361           warning ("comparison is always %s",
3362                    wanted_code == NE_EXPR ? "one" : "zero");
3363           
3364           return convert (truth_type,
3365                           wanted_code == NE_EXPR
3366                           ? integer_one_node : integer_zero_node);
3367         }
3368     }
3369
3370   /* If the right sides are not constant, do the same for it.  Also,
3371      disallow this optimization if a size or signedness mismatch occurs
3372      between the left and right sides.  */
3373   if (l_const == 0)
3374     {
3375       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
3376           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
3377           /* Make sure the two fields on the right
3378              correspond to the left without being swapped.  */
3379           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
3380         return 0;
3381
3382       first_bit = MIN (lr_bitpos, rr_bitpos);
3383       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
3384       rnmode = get_best_mode (end_bit - first_bit, first_bit,
3385                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
3386                               volatilep);
3387       if (rnmode == VOIDmode)
3388         return 0;
3389
3390       rnbitsize = GET_MODE_BITSIZE (rnmode);
3391       rnbitpos = first_bit & ~ (rnbitsize - 1);
3392       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
3393
3394       if (BYTES_BIG_ENDIAN)
3395         {
3396           xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
3397           xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
3398         }
3399
3400       lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
3401                              size_int (xlr_bitpos), 0);
3402       rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
3403                              size_int (xrr_bitpos), 0);
3404
3405       /* Make a mask that corresponds to both fields being compared.
3406          Do this for both items being compared.  If the masks agree,
3407          we can do this by masking both and comparing the masked
3408          results.  */
3409       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3410       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
3411       if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
3412         {
3413           lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3414                                     ll_unsignedp || rl_unsignedp);
3415           rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
3416                                     lr_unsignedp || rr_unsignedp);
3417           if (! all_ones_mask_p (ll_mask, lnbitsize))
3418             {
3419               lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
3420               rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
3421             }
3422           return build (wanted_code, truth_type, lhs, rhs);
3423         }
3424
3425       /* There is still another way we can do something:  If both pairs of
3426          fields being compared are adjacent, we may be able to make a wider
3427          field containing them both.  */
3428       if ((ll_bitsize + ll_bitpos == rl_bitpos
3429            && lr_bitsize + lr_bitpos == rr_bitpos)
3430           || (ll_bitpos == rl_bitpos + rl_bitsize
3431               && lr_bitpos == rr_bitpos + rr_bitsize))
3432         return build (wanted_code, truth_type,
3433                       make_bit_field_ref (ll_inner, type,
3434                                           ll_bitsize + rl_bitsize,
3435                                           MIN (ll_bitpos, rl_bitpos),
3436                                           ll_unsignedp),
3437                       make_bit_field_ref (lr_inner, type,
3438                                           lr_bitsize + rr_bitsize,
3439                                           MIN (lr_bitpos, rr_bitpos),
3440                                           lr_unsignedp));
3441
3442       return 0;
3443     }
3444
3445   /* Handle the case of comparisons with constants.  If there is something in
3446      common between the masks, those bits of the constants must be the same.
3447      If not, the condition is always false.  Test for this to avoid generating
3448      incorrect code below.  */
3449   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
3450   if (! integer_zerop (result)
3451       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
3452                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
3453     {
3454       if (wanted_code == NE_EXPR)
3455         {
3456           warning ("`or' of unmatched not-equal tests is always 1");
3457           return convert (truth_type, integer_one_node);
3458         }
3459       else
3460         {
3461           warning ("`and' of mutually exclusive equal-tests is always zero");
3462           return convert (truth_type, integer_zero_node);
3463         }
3464     }
3465
3466   /* Construct the expression we will return.  First get the component
3467      reference we will make.  Unless the mask is all ones the width of
3468      that field, perform the mask operation.  Then compare with the
3469      merged constant.  */
3470   result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
3471                                ll_unsignedp || rl_unsignedp);
3472
3473   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
3474   if (! all_ones_mask_p (ll_mask, lnbitsize))
3475     result = build (BIT_AND_EXPR, type, result, ll_mask);
3476
3477   return build (wanted_code, truth_type, result,
3478                 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
3479 }
3480 \f
3481 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
3482    S, a SAVE_EXPR, return the expression actually being evaluated.   Note
3483    that we may sometimes modify the tree.  */
3484
3485 static tree
3486 strip_compound_expr (t, s)
3487      tree t;
3488      tree s;
3489 {
3490   tree type = TREE_TYPE (t);
3491   enum tree_code code = TREE_CODE (t);
3492
3493   /* See if this is the COMPOUND_EXPR we want to eliminate.  */
3494   if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
3495       && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
3496     return TREE_OPERAND (t, 1);
3497
3498   /* See if this is a COND_EXPR or a simple arithmetic operator.   We
3499      don't bother handling any other types.  */
3500   else if (code == COND_EXPR)
3501     {
3502       TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3503       TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
3504       TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
3505     }
3506   else if (TREE_CODE_CLASS (code) == '1')
3507     TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3508   else if (TREE_CODE_CLASS (code) == '<'
3509            || TREE_CODE_CLASS (code) == '2')
3510     {
3511       TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
3512       TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
3513     }
3514
3515   return t;
3516 }
3517 \f
3518 /* Perform constant folding and related simplification of EXPR.
3519    The related simplifications include x*1 => x, x*0 => 0, etc.,
3520    and application of the associative law.
3521    NOP_EXPR conversions may be removed freely (as long as we
3522    are careful not to change the C type of the overall expression)
3523    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
3524    but we can constant-fold them if they have constant operands.  */
3525
3526 tree
3527 fold (expr) 
3528      tree expr;
3529 {
3530   register tree t = expr;
3531   tree t1 = NULL_TREE;
3532   tree tem;
3533   tree type = TREE_TYPE (expr);
3534   register tree arg0, arg1;
3535   register enum tree_code code = TREE_CODE (t);
3536   register int kind;
3537   int invert;
3538
3539   /* WINS will be nonzero when the switch is done
3540      if all operands are constant.  */
3541
3542   int wins = 1;
3543
3544   /* Don't try to process an RTL_EXPR since its operands aren't trees. 
3545      Likewise for a SAVE_EXPR that's already been evaluated.  */
3546   if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0)
3547     return t;
3548
3549   /* Return right away if already constant.  */
3550   if (TREE_CONSTANT (t))
3551     {
3552       if (code == CONST_DECL)
3553         return DECL_INITIAL (t);
3554       return t;
3555     }
3556   
3557   kind = TREE_CODE_CLASS (code);
3558   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
3559     {
3560       tree subop;
3561
3562       /* Special case for conversion ops that can have fixed point args.  */
3563       arg0 = TREE_OPERAND (t, 0);
3564
3565       /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
3566       if (arg0 != 0)
3567         STRIP_TYPE_NOPS (arg0);
3568
3569       if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
3570         subop = TREE_REALPART (arg0);
3571       else
3572         subop = arg0;
3573
3574       if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
3575 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3576           && TREE_CODE (subop) != REAL_CST
3577 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3578           )
3579         /* Note that TREE_CONSTANT isn't enough:
3580            static var addresses are constant but we can't
3581            do arithmetic on them.  */
3582         wins = 0;
3583     }
3584   else if (kind == 'e' || kind == '<'
3585            || kind == '1' || kind == '2' || kind == 'r')
3586     {
3587       register int len = tree_code_length[(int) code];
3588       register int i;
3589       for (i = 0; i < len; i++)
3590         {
3591           tree op = TREE_OPERAND (t, i);
3592           tree subop;
3593
3594           if (op == 0)
3595             continue;           /* Valid for CALL_EXPR, at least.  */
3596
3597           if (kind == '<' || code == RSHIFT_EXPR)
3598             {
3599               /* Signedness matters here.  Perhaps we can refine this
3600                  later.  */
3601               STRIP_TYPE_NOPS (op);
3602             }
3603           else
3604             {
3605               /* Strip any conversions that don't change the mode.  */
3606               STRIP_NOPS (op);
3607             }
3608           
3609           if (TREE_CODE (op) == COMPLEX_CST)
3610             subop = TREE_REALPART (op);
3611           else
3612             subop = op;
3613
3614           if (TREE_CODE (subop) != INTEGER_CST
3615 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3616               && TREE_CODE (subop) != REAL_CST
3617 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3618               )
3619             /* Note that TREE_CONSTANT isn't enough:
3620                static var addresses are constant but we can't
3621                do arithmetic on them.  */
3622             wins = 0;
3623
3624           if (i == 0)
3625             arg0 = op;
3626           else if (i == 1)
3627             arg1 = op;
3628         }
3629     }
3630
3631   /* If this is a commutative operation, and ARG0 is a constant, move it
3632      to ARG1 to reduce the number of tests below.  */
3633   if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
3634        || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
3635        || code == BIT_AND_EXPR)
3636       && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
3637     {
3638       tem = arg0; arg0 = arg1; arg1 = tem;
3639
3640       tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
3641       TREE_OPERAND (t, 1) = tem;
3642     }
3643
3644   /* Now WINS is set as described above,
3645      ARG0 is the first operand of EXPR,
3646      and ARG1 is the second operand (if it has more than one operand).
3647
3648      First check for cases where an arithmetic operation is applied to a
3649      compound, conditional, or comparison operation.  Push the arithmetic
3650      operation inside the compound or conditional to see if any folding
3651      can then be done.  Convert comparison to conditional for this purpose.
3652      The also optimizes non-constant cases that used to be done in
3653      expand_expr.
3654
3655      Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
3656      one of the operands is a comparison and the other is a comparison, a
3657      BIT_AND_EXPR with the constant 1, or a truth value.  In that case, the
3658      code below would make the expression more complex.  Change it to a
3659      TRUTH_{AND,OR}_EXPR.  Likewise, convert a similar NE_EXPR to 
3660      TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR.  */
3661
3662   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
3663        || code == EQ_EXPR || code == NE_EXPR)
3664       && ((truth_value_p (TREE_CODE (arg0))
3665            && (truth_value_p (TREE_CODE (arg1))
3666                || (TREE_CODE (arg1) == BIT_AND_EXPR
3667                    && integer_onep (TREE_OPERAND (arg1, 1)))))
3668           || (truth_value_p (TREE_CODE (arg1))
3669               && (truth_value_p (TREE_CODE (arg0))
3670                   || (TREE_CODE (arg0) == BIT_AND_EXPR
3671                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
3672     {
3673       t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
3674                        : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
3675                        : TRUTH_XOR_EXPR,
3676                        type, arg0, arg1));
3677
3678       if (code == EQ_EXPR)
3679         t = invert_truthvalue (t);
3680
3681       return t;
3682     }
3683
3684   if (TREE_CODE_CLASS (code) == '1')
3685     {
3686       if (TREE_CODE (arg0) == COMPOUND_EXPR)
3687         return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3688                       fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
3689       else if (TREE_CODE (arg0) == COND_EXPR)
3690         {
3691           t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
3692                            fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
3693                            fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
3694
3695           /* If this was a conversion, and all we did was to move into
3696              inside the COND_EXPR, bring it back out.  But leave it if
3697              it is a conversion from integer to integer and the
3698              result precision is no wider than a word since such a
3699              conversion is cheap and may be optimized away by combine,
3700              while it couldn't if it were outside the COND_EXPR.  Then return
3701              so we don't get into an infinite recursion loop taking the
3702              conversion out and then back in.  */
3703
3704           if ((code == NOP_EXPR || code == CONVERT_EXPR
3705                || code == NON_LVALUE_EXPR)
3706               && TREE_CODE (t) == COND_EXPR
3707               && TREE_CODE (TREE_OPERAND (t, 1)) == code
3708               && TREE_CODE (TREE_OPERAND (t, 2)) == code
3709               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
3710                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
3711               && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
3712                     && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)))
3713                     && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
3714             t = build1 (code, type,
3715                         build (COND_EXPR,
3716                                TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
3717                                TREE_OPERAND (t, 0),
3718                                TREE_OPERAND (TREE_OPERAND (t, 1), 0),
3719                                TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
3720           return t;
3721         }
3722       else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') 
3723         return fold (build (COND_EXPR, type, arg0,
3724                             fold (build1 (code, type, integer_one_node)),
3725                             fold (build1 (code, type, integer_zero_node))));
3726    }
3727   else if (TREE_CODE_CLASS (code) == '2'
3728            || TREE_CODE_CLASS (code) == '<')
3729     {
3730       if (TREE_CODE (arg1) == COMPOUND_EXPR)
3731         return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3732                       fold (build (code, type,
3733                                    arg0, TREE_OPERAND (arg1, 1))));
3734       else if (TREE_CODE (arg1) == COND_EXPR
3735                || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
3736         {
3737           tree test, true_value, false_value;
3738
3739           if (TREE_CODE (arg1) == COND_EXPR)
3740             {
3741               test = TREE_OPERAND (arg1, 0);
3742               true_value = TREE_OPERAND (arg1, 1);
3743               false_value = TREE_OPERAND (arg1, 2);
3744             }
3745           else
3746             {
3747               tree testtype = TREE_TYPE (arg1);
3748               test = arg1;
3749               true_value = convert (testtype, integer_one_node);
3750               false_value = convert (testtype, integer_zero_node);
3751             }
3752
3753           /* If ARG0 is complex we want to make sure we only evaluate
3754              it once.  Though this is only required if it is volatile, it
3755              might be more efficient even if it is not.  However, if we
3756              succeed in folding one part to a constant, we do not need
3757              to make this SAVE_EXPR.  Since we do this optimization
3758              primarily to see if we do end up with constant and this
3759              SAVE_EXPR interferes with later optimizations, suppressing
3760              it when we can is important.  */
3761
3762           if (TREE_CODE (arg0) != SAVE_EXPR
3763               && ((TREE_CODE (arg0) != VAR_DECL
3764                    && TREE_CODE (arg0) != PARM_DECL)
3765                   || TREE_SIDE_EFFECTS (arg0)))
3766             {
3767               tree lhs = fold (build (code, type, arg0, true_value));
3768               tree rhs = fold (build (code, type, arg0, false_value));
3769
3770               if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
3771                 return fold (build (COND_EXPR, type, test, lhs, rhs));
3772
3773               arg0 = save_expr (arg0);
3774             }
3775
3776           test = fold (build (COND_EXPR, type, test,
3777                               fold (build (code, type, arg0, true_value)),
3778                               fold (build (code, type, arg0, false_value))));
3779           if (TREE_CODE (arg0) == SAVE_EXPR)
3780             return build (COMPOUND_EXPR, type,
3781                           convert (void_type_node, arg0),
3782                           strip_compound_expr (test, arg0));
3783           else
3784             return convert (type, test);
3785         }
3786
3787       else if (TREE_CODE (arg0) == COMPOUND_EXPR)
3788         return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3789                       fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3790       else if (TREE_CODE (arg0) == COND_EXPR
3791                || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3792         {
3793           tree test, true_value, false_value;
3794
3795           if (TREE_CODE (arg0) == COND_EXPR)
3796             {
3797               test = TREE_OPERAND (arg0, 0);
3798               true_value = TREE_OPERAND (arg0, 1);
3799               false_value = TREE_OPERAND (arg0, 2);
3800             }
3801           else
3802             {
3803               tree testtype = TREE_TYPE (arg0);
3804               test = arg0;
3805               true_value = convert (testtype, integer_one_node);
3806               false_value = convert (testtype, integer_zero_node);
3807             }
3808
3809           if (TREE_CODE (arg1) != SAVE_EXPR
3810               && ((TREE_CODE (arg1) != VAR_DECL
3811                    && TREE_CODE (arg1) != PARM_DECL)
3812                   || TREE_SIDE_EFFECTS (arg1)))
3813             {
3814               tree lhs = fold (build (code, type, true_value, arg1));
3815               tree rhs = fold (build (code, type, false_value, arg1));
3816
3817               if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs)
3818                   || TREE_CONSTANT (arg1))
3819                 return fold (build (COND_EXPR, type, test, lhs, rhs));
3820
3821               arg1 = save_expr (arg1);
3822             }
3823
3824           test = fold (build (COND_EXPR, type, test,
3825                               fold (build (code, type, true_value, arg1)),
3826                               fold (build (code, type, false_value, arg1))));
3827           if (TREE_CODE (arg1) == SAVE_EXPR)
3828             return build (COMPOUND_EXPR, type,
3829                           convert (void_type_node, arg1),
3830                           strip_compound_expr (test, arg1));
3831           else
3832             return convert (type, test);
3833         }
3834     }
3835   else if (TREE_CODE_CLASS (code) == '<'
3836            && TREE_CODE (arg0) == COMPOUND_EXPR)
3837     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3838                   fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3839   else if (TREE_CODE_CLASS (code) == '<'
3840            && TREE_CODE (arg1) == COMPOUND_EXPR)
3841     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3842                   fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
3843           
3844   switch (code)
3845     {
3846     case INTEGER_CST:
3847     case REAL_CST:
3848     case STRING_CST:
3849     case COMPLEX_CST:
3850     case CONSTRUCTOR:
3851       return t;
3852
3853     case CONST_DECL:
3854       return fold (DECL_INITIAL (t));
3855
3856     case NOP_EXPR:
3857     case FLOAT_EXPR:
3858     case CONVERT_EXPR:
3859     case FIX_TRUNC_EXPR:
3860       /* Other kinds of FIX are not handled properly by fold_convert.  */
3861
3862       if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
3863         return TREE_OPERAND (t, 0);
3864
3865       /* Handle cases of two conversions in a row.  */
3866       if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
3867           || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
3868         {
3869           tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3870           tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
3871           tree final_type = TREE_TYPE (t);
3872           int inside_int = INTEGRAL_TYPE_P (inside_type);
3873           int inside_ptr = POINTER_TYPE_P (inside_type);
3874           int inside_float = FLOAT_TYPE_P (inside_type);
3875           int inside_prec = TYPE_PRECISION (inside_type);
3876           int inside_unsignedp = TREE_UNSIGNED (inside_type);
3877           int inter_int = INTEGRAL_TYPE_P (inter_type);
3878           int inter_ptr = POINTER_TYPE_P (inter_type);
3879           int inter_float = FLOAT_TYPE_P (inter_type);
3880           int inter_prec = TYPE_PRECISION (inter_type);
3881           int inter_unsignedp = TREE_UNSIGNED (inter_type);
3882           int final_int = INTEGRAL_TYPE_P (final_type);
3883           int final_ptr = POINTER_TYPE_P (final_type);
3884           int final_float = FLOAT_TYPE_P (final_type);
3885           int final_prec = TYPE_PRECISION (final_type);
3886           int final_unsignedp = TREE_UNSIGNED (final_type);
3887
3888           /* In addition to the cases of two conversions in a row 
3889              handled below, if we are converting something to its own
3890              type via an object of identical or wider precision, neither
3891              conversion is needed.  */
3892           if (inside_type == final_type
3893               && ((inter_int && final_int) || (inter_float && final_float))
3894               && inter_prec >= final_prec)
3895             return TREE_OPERAND (TREE_OPERAND (t, 0), 0);
3896
3897           /* Likewise, if the intermediate and final types are either both
3898              float or both integer, we don't need the middle conversion if
3899              it is wider than the final type and doesn't change the signedness
3900              (for integers).  Avoid this if the final type is a pointer
3901              since then we sometimes need the inner conversion.  Likewise if
3902              the outer has a precision not equal to the size of its mode.  */
3903           if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
3904                || (inter_float && inside_float))
3905               && inter_prec >= inside_prec
3906               && (inter_float || inter_unsignedp == inside_unsignedp)
3907               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
3908                     && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
3909               && ! final_ptr)
3910             return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3911
3912           /* Two conversions in a row are not needed unless:
3913              - some conversion is floating-point (overstrict for now), or
3914              - the intermediate type is narrower than both initial and
3915                final, or
3916              - the intermediate type and innermost type differ in signedness,
3917                and the outermost type is wider than the intermediate, or
3918              - the initial type is a pointer type and the precisions of the
3919                intermediate and final types differ, or
3920              - the final type is a pointer type and the precisions of the 
3921                initial and intermediate types differ.  */
3922           if (! inside_float && ! inter_float && ! final_float
3923               && (inter_prec > inside_prec || inter_prec > final_prec)
3924               && ! (inside_int && inter_int
3925                     && inter_unsignedp != inside_unsignedp
3926                     && inter_prec < final_prec)
3927               && ((inter_unsignedp && inter_prec > inside_prec)
3928                   == (final_unsignedp && final_prec > inter_prec))
3929               && ! (inside_ptr && inter_prec != final_prec)
3930               && ! (final_ptr && inside_prec != inter_prec)
3931               && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
3932                     && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
3933               && ! final_ptr)
3934             return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3935         }
3936
3937       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
3938           && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
3939           /* Detect assigning a bitfield.  */
3940           && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
3941                && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
3942         {
3943           /* Don't leave an assignment inside a conversion
3944              unless assigning a bitfield.  */
3945           tree prev = TREE_OPERAND (t, 0);
3946           TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
3947           /* First do the assignment, then return converted constant.  */
3948           t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
3949           TREE_USED (t) = 1;
3950           return t;
3951         }
3952       if (!wins)
3953         {
3954           TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
3955           return t;
3956         }
3957       return fold_convert (t, arg0);
3958
3959 #if 0  /* This loses on &"foo"[0].  */
3960     case ARRAY_REF:
3961         {
3962           int i;
3963
3964           /* Fold an expression like: "foo"[2] */
3965           if (TREE_CODE (arg0) == STRING_CST
3966               && TREE_CODE (arg1) == INTEGER_CST
3967               && !TREE_INT_CST_HIGH (arg1)
3968               && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
3969             {
3970               t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
3971               TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
3972               force_fit_type (t, 0);
3973             }
3974         }
3975       return t;
3976 #endif /* 0 */
3977
3978     case COMPONENT_REF:
3979       if (TREE_CODE (arg0) == CONSTRUCTOR)
3980         {
3981           tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
3982           if (m)
3983             t = TREE_VALUE (m);
3984         }
3985       return t;
3986
3987     case RANGE_EXPR:
3988       TREE_CONSTANT (t) = wins;
3989       return t;
3990
3991     case NEGATE_EXPR:
3992       if (wins)
3993         {
3994           if (TREE_CODE (arg0) == INTEGER_CST)
3995             {
3996               HOST_WIDE_INT low, high;
3997               int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3998                                          TREE_INT_CST_HIGH (arg0),
3999                                          &low, &high);
4000               t = build_int_2 (low, high);
4001               TREE_TYPE (t) = type;
4002               TREE_OVERFLOW (t)
4003                 = (TREE_OVERFLOW (arg0)
4004                    | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
4005               TREE_CONSTANT_OVERFLOW (t)
4006                 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
4007             }
4008           else if (TREE_CODE (arg0) == REAL_CST)
4009             t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
4010           TREE_TYPE (t) = type;
4011         }
4012       else if (TREE_CODE (arg0) == NEGATE_EXPR)
4013         return TREE_OPERAND (arg0, 0);
4014
4015       /* Convert - (a - b) to (b - a) for non-floating-point.  */
4016       else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
4017         return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
4018                       TREE_OPERAND (arg0, 0));
4019
4020       return t;
4021
4022     case ABS_EXPR:
4023       if (wins)
4024         {
4025           if (TREE_CODE (arg0) == INTEGER_CST)
4026             {
4027               if (! TREE_UNSIGNED (type)
4028                   && TREE_INT_CST_HIGH (arg0) < 0)
4029                 {
4030                   HOST_WIDE_INT low, high;
4031                   int overflow = neg_double (TREE_INT_CST_LOW (arg0),
4032                                              TREE_INT_CST_HIGH (arg0),
4033                                              &low, &high);
4034                   t = build_int_2 (low, high);
4035                   TREE_TYPE (t) = type;
4036                   TREE_OVERFLOW (t)
4037                     = (TREE_OVERFLOW (arg0)
4038                        | force_fit_type (t, overflow));
4039                   TREE_CONSTANT_OVERFLOW (t)
4040                     = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
4041                 }
4042             }
4043           else if (TREE_CODE (arg0) == REAL_CST)
4044             {
4045               if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
4046                 t = build_real (type,
4047                                 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
4048             }
4049           TREE_TYPE (t) = type;
4050         }
4051       else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
4052         return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
4053       return t;
4054
4055     case CONJ_EXPR:
4056       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4057         return arg0;
4058       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4059         return build (COMPLEX_EXPR, TREE_TYPE (arg0),
4060                       TREE_OPERAND (arg0, 0),
4061                       fold (build1 (NEGATE_EXPR,
4062                                     TREE_TYPE (TREE_TYPE (arg0)),
4063                                     TREE_OPERAND (arg0, 1))));
4064       else if (TREE_CODE (arg0) == COMPLEX_CST)
4065         return build_complex (type, TREE_OPERAND (arg0, 0),
4066                               fold (build1 (NEGATE_EXPR,
4067                                             TREE_TYPE (TREE_TYPE (arg0)),
4068                                             TREE_OPERAND (arg0, 1))));
4069       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4070         return fold (build (TREE_CODE (arg0), type,
4071                             fold (build1 (CONJ_EXPR, type,
4072                                           TREE_OPERAND (arg0, 0))),
4073                             fold (build1 (CONJ_EXPR,
4074                                           type, TREE_OPERAND (arg0, 1)))));
4075       else if (TREE_CODE (arg0) == CONJ_EXPR)
4076         return TREE_OPERAND (arg0, 0);
4077       return t;
4078
4079     case BIT_NOT_EXPR:
4080       if (wins)
4081         {
4082           if (TREE_CODE (arg0) == INTEGER_CST)
4083             t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
4084                              ~ TREE_INT_CST_HIGH (arg0));
4085           TREE_TYPE (t) = type;
4086           force_fit_type (t, 0);
4087           TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
4088           TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
4089         }
4090       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
4091         return TREE_OPERAND (arg0, 0);
4092       return t;
4093
4094     case PLUS_EXPR:
4095       /* A + (-B) -> A - B */
4096       if (TREE_CODE (arg1) == NEGATE_EXPR)
4097         return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
4098       else if (! FLOAT_TYPE_P (type))
4099         {
4100           if (integer_zerop (arg1))
4101             return non_lvalue (convert (type, arg0));
4102
4103           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
4104              with a constant, and the two constants have no bits in common,
4105              we should treat this as a BIT_IOR_EXPR since this may produce more
4106              simplifications.  */
4107           if (TREE_CODE (arg0) == BIT_AND_EXPR
4108               && TREE_CODE (arg1) == BIT_AND_EXPR
4109               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4110               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
4111               && integer_zerop (const_binop (BIT_AND_EXPR,
4112                                              TREE_OPERAND (arg0, 1),
4113                                              TREE_OPERAND (arg1, 1), 0)))
4114             {
4115               code = BIT_IOR_EXPR;
4116               goto bit_ior;
4117             }
4118
4119           /* (A * C) + (B * C) -> (A+B) * C.  Since we are most concerned
4120              about the case where C is a constant, just try one of the
4121              four possibilities.  */
4122
4123           if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
4124               && operand_equal_p (TREE_OPERAND (arg0, 1),
4125                                   TREE_OPERAND (arg1, 1), 0))
4126             return fold (build (MULT_EXPR, type,
4127                                 fold (build (PLUS_EXPR, type,
4128                                              TREE_OPERAND (arg0, 0),
4129                                              TREE_OPERAND (arg1, 0))),
4130                                 TREE_OPERAND (arg0, 1)));
4131         }
4132       /* In IEEE floating point, x+0 may not equal x.  */
4133       else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4134                 || flag_fast_math)
4135                && real_zerop (arg1))
4136         return non_lvalue (convert (type, arg0));
4137     associate:
4138       /* In most languages, can't associate operations on floats
4139          through parentheses.  Rather than remember where the parentheses
4140          were, we don't associate floats at all.  It shouldn't matter much.
4141          However, associating multiplications is only very slightly
4142          inaccurate, so do that if -ffast-math is specified.  */
4143       if (FLOAT_TYPE_P (type)
4144           && ! (flag_fast_math && code == MULT_EXPR))
4145         goto binary;
4146
4147       /* The varsign == -1 cases happen only for addition and subtraction.
4148          It says that the arg that was split was really CON minus VAR.
4149          The rest of the code applies to all associative operations.  */
4150       if (!wins)
4151         {
4152           tree var, con;
4153           int varsign;
4154
4155           if (split_tree (arg0, code, &var, &con, &varsign))
4156             {
4157               if (varsign == -1)
4158                 {
4159                   /* EXPR is (CON-VAR) +- ARG1.  */
4160                   /* If it is + and VAR==ARG1, return just CONST.  */
4161                   if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
4162                     return convert (TREE_TYPE (t), con);
4163                     
4164                   /* If ARG0 is a constant, don't change things around;
4165                      instead keep all the constant computations together.  */
4166
4167                   if (TREE_CONSTANT (arg0))
4168                     return t;
4169
4170                   /* Otherwise return (CON +- ARG1) - VAR.  */
4171                   t = build (MINUS_EXPR, type,
4172                              fold (build (code, type, con, arg1)), var);
4173                 }
4174               else
4175                 {
4176                   /* EXPR is (VAR+CON) +- ARG1.  */
4177                   /* If it is - and VAR==ARG1, return just CONST.  */
4178                   if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
4179                     return convert (TREE_TYPE (t), con);
4180                     
4181                   /* If ARG0 is a constant, don't change things around;
4182                      instead keep all the constant computations together.  */
4183
4184                   if (TREE_CONSTANT (arg0))
4185                     return t;
4186
4187                   /* Otherwise return VAR +- (ARG1 +- CON).  */
4188                   tem = fold (build (code, type, arg1, con));
4189                   t = build (code, type, var, tem);
4190
4191                   if (integer_zerop (tem)
4192                       && (code == PLUS_EXPR || code == MINUS_EXPR))
4193                     return convert (type, var);
4194                   /* If we have x +/- (c - d) [c an explicit integer]
4195                      change it to x -/+ (d - c) since if d is relocatable
4196                      then the latter can be a single immediate insn
4197                      and the former cannot.  */
4198                   if (TREE_CODE (tem) == MINUS_EXPR
4199                       && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
4200                     {
4201                       tree tem1 = TREE_OPERAND (tem, 1);
4202                       TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
4203                       TREE_OPERAND (tem, 0) = tem1;
4204                       TREE_SET_CODE (t,
4205                                      (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
4206                     }
4207                 }
4208               return t;
4209             }
4210
4211           if (split_tree (arg1, code, &var, &con, &varsign))
4212             {
4213               if (TREE_CONSTANT (arg1))
4214                 return t;
4215
4216               if (varsign == -1)
4217                 TREE_SET_CODE (t,
4218                                (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
4219
4220               /* EXPR is ARG0 +- (CON +- VAR).  */
4221               if (TREE_CODE (t) == MINUS_EXPR
4222                   && operand_equal_p (var, arg0, 0))
4223                 {
4224                   /* If VAR and ARG0 cancel, return just CON or -CON.  */
4225                   if (code == PLUS_EXPR)
4226                     return convert (TREE_TYPE (t), con);
4227                   return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
4228                                        convert (TREE_TYPE (t), con)));
4229                 }
4230
4231               t = build (TREE_CODE (t), type,
4232                          fold (build (code, TREE_TYPE (t), arg0, con)), var);
4233
4234               if (integer_zerop (TREE_OPERAND (t, 0))
4235                   && TREE_CODE (t) == PLUS_EXPR)
4236                 return convert (TREE_TYPE (t), var);
4237               return t;
4238             }
4239         }
4240     binary:
4241 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
4242       if (TREE_CODE (arg1) == REAL_CST)
4243         return t;
4244 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
4245       if (wins)
4246         t1 = const_binop (code, arg0, arg1, 0);
4247       if (t1 != NULL_TREE)
4248         {
4249           /* The return value should always have
4250              the same type as the original expression.  */
4251           TREE_TYPE (t1) = TREE_TYPE (t);
4252           return t1;
4253         }
4254       return t;
4255
4256     case MINUS_EXPR:
4257       if (! FLOAT_TYPE_P (type))
4258         {
4259           if (! wins && integer_zerop (arg0))
4260             return build1 (NEGATE_EXPR, type, arg1);
4261           if (integer_zerop (arg1))
4262             return non_lvalue (convert (type, arg0));
4263
4264           /* (A * C) - (B * C) -> (A-B) * C.  Since we are most concerned
4265              about the case where C is a constant, just try one of the
4266              four possibilities.  */
4267
4268           if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
4269               && operand_equal_p (TREE_OPERAND (arg0, 1),
4270                                   TREE_OPERAND (arg1, 1), 0))
4271             return fold (build (MULT_EXPR, type,
4272                                 fold (build (MINUS_EXPR, type,
4273                                              TREE_OPERAND (arg0, 0),
4274                                              TREE_OPERAND (arg1, 0))),
4275                                 TREE_OPERAND (arg0, 1)));
4276         }
4277       /* Convert A - (-B) to A + B.  */
4278       else if (TREE_CODE (arg1) == NEGATE_EXPR)
4279         return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
4280
4281       else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4282                || flag_fast_math)
4283         {
4284           /* Except with IEEE floating point, 0-x equals -x.  */
4285           if (! wins && real_zerop (arg0))
4286             return build1 (NEGATE_EXPR, type, arg1);
4287           /* Except with IEEE floating point, x-0 equals x.  */
4288           if (real_zerop (arg1))
4289             return non_lvalue (convert (type, arg0));
4290         }
4291
4292       /* Fold &x - &x.  This can happen from &x.foo - &x. 
4293          This is unsafe for certain floats even in non-IEEE formats.
4294          In IEEE, it is unsafe because it does wrong for NaNs.
4295          Also note that operand_equal_p is always false if an operand
4296          is volatile.  */
4297
4298       if ((! FLOAT_TYPE_P (type) || flag_fast_math)
4299           && operand_equal_p (arg0, arg1, 0))
4300         return convert (type, integer_zero_node);
4301
4302       goto associate;
4303
4304     case MULT_EXPR:
4305       if (! FLOAT_TYPE_P (type))
4306         {
4307           if (integer_zerop (arg1))
4308             return omit_one_operand (type, arg1, arg0);
4309           if (integer_onep (arg1))
4310             return non_lvalue (convert (type, arg0));
4311
4312           /* ((A / C) * C) is A if the division is an
4313              EXACT_DIV_EXPR.   Since C is normally a constant,
4314              just check for one of the four possibilities.  */
4315
4316           if (TREE_CODE (arg0) == EXACT_DIV_EXPR
4317               && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
4318             return TREE_OPERAND (arg0, 0);
4319
4320           /* (a * (1 << b)) is (a << b)  */
4321           if (TREE_CODE (arg1) == LSHIFT_EXPR
4322               && integer_onep (TREE_OPERAND (arg1, 0)))
4323             return fold (build (LSHIFT_EXPR, type, arg0,
4324                                 TREE_OPERAND (arg1, 1)));
4325           if (TREE_CODE (arg0) == LSHIFT_EXPR
4326               && integer_onep (TREE_OPERAND (arg0, 0)))
4327             return fold (build (LSHIFT_EXPR, type, arg1,
4328                                 TREE_OPERAND (arg0, 1)));
4329         }
4330       else
4331         {
4332           /* x*0 is 0, except for IEEE floating point.  */
4333           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4334                || flag_fast_math)
4335               && real_zerop (arg1))
4336             return omit_one_operand (type, arg1, arg0);
4337           /* In IEEE floating point, x*1 is not equivalent to x for snans.
4338              However, ANSI says we can drop signals,
4339              so we can do this anyway.  */
4340           if (real_onep (arg1))
4341             return non_lvalue (convert (type, arg0));
4342           /* x*2 is x+x */
4343           if (! wins && real_twop (arg1))
4344             {
4345               tree arg = save_expr (arg0);
4346               return build (PLUS_EXPR, type, arg, arg);
4347             }
4348         }
4349       goto associate;
4350
4351     case BIT_IOR_EXPR:
4352     bit_ior:
4353       {
4354       register enum tree_code code0, code1;
4355
4356       if (integer_all_onesp (arg1))
4357         return omit_one_operand (type, arg1, arg0);
4358       if (integer_zerop (arg1))
4359         return non_lvalue (convert (type, arg0));
4360       t1 = distribute_bit_expr (code, type, arg0, arg1);
4361       if (t1 != NULL_TREE)
4362         return t1;
4363
4364       /* (A << C1) | (A >> C2) if A is unsigned and C1+C2 is the size of A
4365          is a rotate of A by C1 bits.  */
4366       /* (A << B) | (A >> (Z - B)) if A is unsigned and Z is the size of A
4367          is a rotate of A by B bits.  */
4368
4369       code0 = TREE_CODE (arg0);
4370       code1 = TREE_CODE (arg1);
4371       if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
4372           || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
4373           && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
4374           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4375         {
4376           register tree tree01, tree11;
4377           register enum tree_code code01, code11;
4378
4379           tree01 = TREE_OPERAND (arg0, 1);
4380           tree11 = TREE_OPERAND (arg1, 1);
4381           code01 = TREE_CODE (tree01);
4382           code11 = TREE_CODE (tree11);
4383           if (code01 == INTEGER_CST
4384             && code11 == INTEGER_CST
4385             && TREE_INT_CST_HIGH (tree01) == 0
4386             && TREE_INT_CST_HIGH (tree11) == 0
4387             && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
4388               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
4389             return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
4390                       code0 == LSHIFT_EXPR ? tree01 : tree11);
4391           else if (code11 == MINUS_EXPR
4392                 && TREE_CODE (TREE_OPERAND (tree11, 0)) == INTEGER_CST
4393                 && TREE_INT_CST_HIGH (TREE_OPERAND (tree11, 0)) == 0
4394                 && TREE_INT_CST_LOW (TREE_OPERAND (tree11, 0))
4395                   == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))
4396                 && operand_equal_p (tree01, TREE_OPERAND (tree11, 1), 0))
4397             return build (code0 == LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR,
4398                         type, TREE_OPERAND (arg0, 0), tree01);
4399           else if (code01 == MINUS_EXPR
4400                 && TREE_CODE (TREE_OPERAND (tree01, 0)) == INTEGER_CST
4401                 && TREE_INT_CST_HIGH (TREE_OPERAND (tree01, 0)) == 0
4402                 && TREE_INT_CST_LOW (TREE_OPERAND (tree01, 0))
4403                   == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))
4404                 && operand_equal_p (tree11, TREE_OPERAND (tree01, 1), 0))
4405             return build (code0 != LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR,
4406                         type, TREE_OPERAND (arg0, 0), tree11);
4407         }
4408
4409       goto associate;
4410       }
4411
4412     case BIT_XOR_EXPR:
4413       if (integer_zerop (arg1))
4414         return non_lvalue (convert (type, arg0));
4415       if (integer_all_onesp (arg1))
4416         return fold (build1 (BIT_NOT_EXPR, type, arg0));
4417       goto associate;
4418
4419     case BIT_AND_EXPR:
4420     bit_and:
4421       if (integer_all_onesp (arg1))
4422         return non_lvalue (convert (type, arg0));
4423       if (integer_zerop (arg1))
4424         return omit_one_operand (type, arg1, arg0);
4425       t1 = distribute_bit_expr (code, type, arg0, arg1);
4426       if (t1 != NULL_TREE)
4427         return t1;
4428       /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char.  */
4429       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
4430           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
4431         {
4432           int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
4433           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
4434               && (~TREE_INT_CST_LOW (arg0)
4435                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
4436             return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
4437         }
4438       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
4439           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4440         {
4441           int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
4442           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
4443               && (~TREE_INT_CST_LOW (arg1)
4444                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
4445             return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
4446         }
4447       goto associate;
4448
4449     case BIT_ANDTC_EXPR:
4450       if (integer_all_onesp (arg0))
4451         return non_lvalue (convert (type, arg1));
4452       if (integer_zerop (arg0))
4453         return omit_one_operand (type, arg0, arg1);
4454       if (TREE_CODE (arg1) == INTEGER_CST)
4455         {
4456           arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
4457           code = BIT_AND_EXPR;
4458           goto bit_and;
4459         }
4460       goto binary;
4461
4462     case RDIV_EXPR:
4463       /* In most cases, do nothing with a divide by zero.  */
4464 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4465 #ifndef REAL_INFINITY
4466       if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
4467         return t;
4468 #endif
4469 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4470
4471       /* In IEEE floating point, x/1 is not equivalent to x for snans.
4472          However, ANSI says we can drop signals, so we can do this anyway.  */
4473       if (real_onep (arg1))
4474         return non_lvalue (convert (type, arg0));
4475
4476       /* If ARG1 is a constant, we can convert this to a multiply by the
4477          reciprocal.  This does not have the same rounding properties,
4478          so only do this if -ffast-math.  We can actually always safely
4479          do it if ARG1 is a power of two, but it's hard to tell if it is
4480          or not in a portable manner.  */
4481       if (TREE_CODE (arg1) == REAL_CST)
4482         {
4483           if (flag_fast_math
4484               && 0 != (tem = const_binop (code, build_real (type, dconst1),
4485                                           arg1, 0)))
4486             return fold (build (MULT_EXPR, type, arg0, tem));
4487           /* Find the reciprocal if optimizing and the result is exact. */
4488           else if (optimize)
4489             {
4490               REAL_VALUE_TYPE r;
4491               r = TREE_REAL_CST (arg1);
4492               if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
4493                   {
4494                     tem = build_real (type, r);
4495                     return fold (build (MULT_EXPR, type, arg0, tem));
4496                   }
4497             }
4498         }
4499       goto binary;
4500
4501     case TRUNC_DIV_EXPR:
4502     case ROUND_DIV_EXPR:
4503     case FLOOR_DIV_EXPR:
4504     case CEIL_DIV_EXPR:
4505     case EXACT_DIV_EXPR:
4506       if (integer_onep (arg1))
4507         return non_lvalue (convert (type, arg0));
4508       if (integer_zerop (arg1))
4509         return t;
4510
4511       /* If we have ((a / C1) / C2) where both division are the same type, try
4512          to simplify.  First see if C1 * C2 overflows or not.  */
4513       if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST
4514           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
4515         {
4516           tree new_divisor;
4517
4518           new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0);
4519           tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0);
4520
4521           if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem)
4522               && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem))
4523             {
4524               /* If no overflow, divide by C1*C2.  */
4525               return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor));
4526             }
4527         }
4528
4529       /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3),
4530          where C1 % C3 == 0 or C3 % C1 == 0.  We can simplify these
4531          expressions, which often appear in the offsets or sizes of
4532          objects with a varying size.  Only deal with positive divisors
4533          and multiplicands.   If C2 is negative, we must have C2 % C3 == 0.
4534
4535          Look for NOPs and SAVE_EXPRs inside.  */
4536
4537       if (TREE_CODE (arg1) == INTEGER_CST
4538           && tree_int_cst_sgn (arg1) >= 0)
4539         {
4540           int have_save_expr = 0;
4541           tree c2 = integer_zero_node;
4542           tree xarg0 = arg0;
4543
4544           if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0)
4545             have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
4546
4547           STRIP_NOPS (xarg0);
4548
4549           if (TREE_CODE (xarg0) == PLUS_EXPR
4550               && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
4551             c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
4552           else if (TREE_CODE (xarg0) == MINUS_EXPR
4553                    && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4554                    /* If we are doing this computation unsigned, the negate
4555                       is incorrect.  */
4556                    && ! TREE_UNSIGNED (type))
4557             {
4558               c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
4559               xarg0 = TREE_OPERAND (xarg0, 0);
4560             }
4561
4562           if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0)
4563             have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0);
4564
4565           STRIP_NOPS (xarg0);
4566
4567           if (TREE_CODE (xarg0) == MULT_EXPR
4568               && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4569               && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0
4570               && (integer_zerop (const_binop (TRUNC_MOD_EXPR,
4571                                               TREE_OPERAND (xarg0, 1), arg1, 1))
4572                   || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1,
4573                                                  TREE_OPERAND (xarg0, 1), 1)))
4574               && (tree_int_cst_sgn (c2) >= 0
4575                   || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2,
4576                                                  arg1, 1))))
4577             {
4578               tree outer_div = integer_one_node;
4579               tree c1 = TREE_OPERAND (xarg0, 1);
4580               tree c3 = arg1;
4581
4582               /* If C3 > C1, set them equal and do a divide by
4583                  C3/C1 at the end of the operation.  */
4584               if (tree_int_cst_lt (c1, c3))
4585                 outer_div = const_binop (code, c3, c1, 0), c3 = c1;
4586                 
4587               /* The result is A * (C1/C3) + (C2/C3).  */
4588               t = fold (build (PLUS_EXPR, type,
4589                                fold (build (MULT_EXPR, type,
4590                                             TREE_OPERAND (xarg0, 0),
4591                                             const_binop (code, c1, c3, 1))),
4592                                const_binop (code, c2, c3, 1)));
4593
4594               if (! integer_onep (outer_div))
4595                 t = fold (build (code, type, t, convert (type, outer_div)));
4596
4597               if (have_save_expr)
4598                 t = save_expr (t);
4599
4600               return t;
4601             }
4602         }
4603
4604       goto binary;
4605
4606     case CEIL_MOD_EXPR:
4607     case FLOOR_MOD_EXPR:
4608     case ROUND_MOD_EXPR:
4609     case TRUNC_MOD_EXPR:
4610       if (integer_onep (arg1))
4611         return omit_one_operand (type, integer_zero_node, arg0);
4612       if (integer_zerop (arg1))
4613         return t;
4614
4615       /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3),
4616          where C1 % C3 == 0.  Handle similarly to the division case,
4617          but don't bother with SAVE_EXPRs.  */
4618
4619       if (TREE_CODE (arg1) == INTEGER_CST
4620           && ! integer_zerop (arg1))
4621         {
4622           tree c2 = integer_zero_node;
4623           tree xarg0 = arg0;
4624
4625           if (TREE_CODE (xarg0) == PLUS_EXPR
4626               && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST)
4627             c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0);
4628           else if (TREE_CODE (xarg0) == MINUS_EXPR
4629                    && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4630                    && ! TREE_UNSIGNED (type))
4631             {
4632               c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1)));
4633               xarg0 = TREE_OPERAND (xarg0, 0);
4634             }
4635
4636           STRIP_NOPS (xarg0);
4637
4638           if (TREE_CODE (xarg0) == MULT_EXPR
4639               && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST
4640               && integer_zerop (const_binop (TRUNC_MOD_EXPR,
4641                                              TREE_OPERAND (xarg0, 1),
4642                                              arg1, 1))
4643               && tree_int_cst_sgn (c2) >= 0)
4644             /* The result is (C2%C3).  */
4645             return omit_one_operand (type, const_binop (code, c2, arg1, 1),
4646                                      TREE_OPERAND (xarg0, 0));
4647         }
4648
4649       goto binary;
4650
4651     case LSHIFT_EXPR:
4652     case RSHIFT_EXPR:
4653     case LROTATE_EXPR:
4654     case RROTATE_EXPR:
4655       if (integer_zerop (arg1))
4656         return non_lvalue (convert (type, arg0));
4657       /* Since negative shift count is not well-defined,
4658          don't try to compute it in the compiler.  */
4659       if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
4660         return t;
4661       /* Rewrite an LROTATE_EXPR by a constant into an
4662          RROTATE_EXPR by a new constant.  */
4663       if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
4664         {
4665           TREE_SET_CODE (t, RROTATE_EXPR);
4666           code = RROTATE_EXPR;
4667           TREE_OPERAND (t, 1) = arg1
4668             = const_binop
4669               (MINUS_EXPR,
4670                convert (TREE_TYPE (arg1),
4671                         build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
4672                arg1, 0);
4673           if (tree_int_cst_sgn (arg1) < 0)
4674             return t;
4675         }
4676
4677       /* If we have a rotate of a bit operation with the rotate count and
4678          the second operand of the bit operation both constant,
4679          permute the two operations.  */
4680       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
4681           && (TREE_CODE (arg0) == BIT_AND_EXPR
4682               || TREE_CODE (arg0) == BIT_ANDTC_EXPR
4683               || TREE_CODE (arg0) == BIT_IOR_EXPR
4684               || TREE_CODE (arg0) == BIT_XOR_EXPR)
4685           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
4686         return fold (build (TREE_CODE (arg0), type,
4687                             fold (build (code, type,
4688                                          TREE_OPERAND (arg0, 0), arg1)),
4689                             fold (build (code, type,
4690                                          TREE_OPERAND (arg0, 1), arg1))));
4691
4692       /* Two consecutive rotates adding up to the width of the mode can
4693          be ignored.  */
4694       if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
4695           && TREE_CODE (arg0) == RROTATE_EXPR
4696           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4697           && TREE_INT_CST_HIGH (arg1) == 0
4698           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
4699           && ((TREE_INT_CST_LOW (arg1)
4700                + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
4701               == GET_MODE_BITSIZE (TYPE_MODE (type))))
4702         return TREE_OPERAND (arg0, 0);
4703
4704       goto binary;
4705
4706     case MIN_EXPR:
4707       if (operand_equal_p (arg0, arg1, 0))
4708         return arg0;
4709       if (INTEGRAL_TYPE_P (type)
4710           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
4711         return omit_one_operand (type, arg1, arg0);
4712       goto associate;
4713
4714     case MAX_EXPR:
4715       if (operand_equal_p (arg0, arg1, 0))
4716         return arg0;
4717       if (INTEGRAL_TYPE_P (type)
4718           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
4719         return omit_one_operand (type, arg1, arg0);
4720       goto associate;
4721
4722     case TRUTH_NOT_EXPR:
4723       /* Note that the operand of this must be an int
4724          and its values must be 0 or 1.
4725          ("true" is a fixed value perhaps depending on the language,
4726          but we don't handle values other than 1 correctly yet.)  */
4727       tem = invert_truthvalue (arg0);
4728       /* Avoid infinite recursion.  */
4729       if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
4730         return t;
4731       return convert (type, tem);
4732
4733     case TRUTH_ANDIF_EXPR:
4734       /* Note that the operands of this must be ints
4735          and their values must be 0 or 1.
4736          ("true" is a fixed value perhaps depending on the language.)  */
4737       /* If first arg is constant zero, return it.  */
4738       if (integer_zerop (arg0))
4739         return arg0;
4740     case TRUTH_AND_EXPR:
4741       /* If either arg is constant true, drop it.  */
4742       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4743         return non_lvalue (arg1);
4744       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
4745         return non_lvalue (arg0);
4746       /* If second arg is constant zero, result is zero, but first arg
4747          must be evaluated.  */
4748       if (integer_zerop (arg1))
4749         return omit_one_operand (type, arg1, arg0);
4750       /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
4751          case will be handled here.  */
4752       if (integer_zerop (arg0))
4753         return omit_one_operand (type, arg0, arg1);
4754
4755     truth_andor:
4756       /* We only do these simplifications if we are optimizing.  */
4757       if (!optimize)
4758         return t;
4759
4760       /* Check for things like (A || B) && (A || C).  We can convert this
4761          to A || (B && C).  Note that either operator can be any of the four
4762          truth and/or operations and the transformation will still be
4763          valid.   Also note that we only care about order for the
4764          ANDIF and ORIF operators.  */
4765       if (TREE_CODE (arg0) == TREE_CODE (arg1)
4766           && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
4767               || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
4768               || TREE_CODE (arg0) == TRUTH_AND_EXPR
4769               || TREE_CODE (arg0) == TRUTH_OR_EXPR))
4770         {
4771           tree a00 = TREE_OPERAND (arg0, 0);
4772           tree a01 = TREE_OPERAND (arg0, 1);
4773           tree a10 = TREE_OPERAND (arg1, 0);
4774           tree a11 = TREE_OPERAND (arg1, 1);
4775           int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
4776                               || TREE_CODE (arg0) == TRUTH_AND_EXPR)
4777                              && (code == TRUTH_AND_EXPR
4778                                  || code == TRUTH_OR_EXPR));
4779
4780           if (operand_equal_p (a00, a10, 0))
4781             return fold (build (TREE_CODE (arg0), type, a00,
4782                                 fold (build (code, type, a01, a11))));
4783           else if (commutative && operand_equal_p (a00, a11, 0))
4784             return fold (build (TREE_CODE (arg0), type, a00,
4785                                 fold (build (code, type, a01, a10))));
4786           else if (commutative && operand_equal_p (a01, a10, 0))
4787             return fold (build (TREE_CODE (arg0), type, a01,
4788                                 fold (build (code, type, a00, a11))));
4789
4790           /* This case if tricky because we must either have commutative
4791              operators or else A10 must not have side-effects.  */
4792
4793           else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
4794                    && operand_equal_p (a01, a11, 0))
4795             return fold (build (TREE_CODE (arg0), type,
4796                                 fold (build (code, type, a00, a10)),
4797                                 a01));
4798         }
4799
4800       /* See if we can build a range comparison.  */
4801       if (0 != (tem = fold_range_test (t)))
4802         return tem;
4803
4804       /* Check for the possibility of merging component references.  If our
4805          lhs is another similar operation, try to merge its rhs with our
4806          rhs.  Then try to merge our lhs and rhs.  */
4807       if (TREE_CODE (arg0) == code
4808           && 0 != (tem = fold_truthop (code, type,
4809                                        TREE_OPERAND (arg0, 1), arg1)))
4810         return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
4811
4812       if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
4813         return tem;
4814
4815       return t;
4816
4817     case TRUTH_ORIF_EXPR:
4818       /* Note that the operands of this must be ints
4819          and their values must be 0 or true.
4820          ("true" is a fixed value perhaps depending on the language.)  */
4821       /* If first arg is constant true, return it.  */
4822       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4823         return arg0;
4824     case TRUTH_OR_EXPR:
4825       /* If either arg is constant zero, drop it.  */
4826       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
4827         return non_lvalue (arg1);
4828       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
4829         return non_lvalue (arg0);
4830       /* If second arg is constant true, result is true, but we must
4831          evaluate first arg.  */
4832       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
4833         return omit_one_operand (type, arg1, arg0);
4834       /* Likewise for first arg, but note this only occurs here for
4835          TRUTH_OR_EXPR.  */
4836       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
4837         return omit_one_operand (type, arg0, arg1);
4838       goto truth_andor;
4839
4840     case TRUTH_XOR_EXPR:
4841       /* If either arg is constant zero, drop it.  */
4842       if (integer_zerop (arg0))
4843         return non_lvalue (arg1);
4844       if (integer_zerop (arg1))
4845         return non_lvalue (arg0);
4846       /* If either arg is constant true, this is a logical inversion.  */
4847       if (integer_onep (arg0))
4848         return non_lvalue (invert_truthvalue (arg1));
4849       if (integer_onep (arg1))
4850         return non_lvalue (invert_truthvalue (arg0));
4851       return t;
4852
4853     case EQ_EXPR:
4854     case NE_EXPR:
4855     case LT_EXPR:
4856     case GT_EXPR:
4857     case LE_EXPR:
4858     case GE_EXPR:
4859       /* If one arg is a constant integer, put it last.  */
4860       if (TREE_CODE (arg0) == INTEGER_CST
4861           && TREE_CODE (arg1) != INTEGER_CST)
4862         {
4863           TREE_OPERAND (t, 0) = arg1;
4864           TREE_OPERAND (t, 1) = arg0;
4865           arg0 = TREE_OPERAND (t, 0);
4866           arg1 = TREE_OPERAND (t, 1);
4867           code = swap_tree_comparison (code);
4868           TREE_SET_CODE (t, code);
4869         }
4870
4871       /* Convert foo++ == CONST into ++foo == CONST + INCR.
4872          First, see if one arg is constant; find the constant arg
4873          and the other one.  */
4874       {
4875         tree constop = 0, varop;
4876         int constopnum = -1;
4877
4878         if (TREE_CONSTANT (arg1))
4879           constopnum = 1, constop = arg1, varop = arg0;
4880         if (TREE_CONSTANT (arg0))
4881           constopnum = 0, constop = arg0, varop = arg1;
4882
4883         if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
4884           {
4885             /* This optimization is invalid for ordered comparisons
4886                if CONST+INCR overflows or if foo+incr might overflow.
4887                This optimization is invalid for floating point due to rounding.
4888                For pointer types we assume overflow doesn't happen.  */
4889             if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
4890                 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4891                     && (code == EQ_EXPR || code == NE_EXPR)))
4892               {
4893                 tree newconst
4894                   = fold (build (PLUS_EXPR, TREE_TYPE (varop),
4895                                  constop, TREE_OPERAND (varop, 1)));
4896                 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
4897
4898                 /* If VAROP is a reference to a bitfield, we must mask
4899                    the constant by the width of the field.  */
4900                 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
4901                     && DECL_BIT_FIELD(TREE_OPERAND
4902                                       (TREE_OPERAND (varop, 0), 1)))
4903                   {
4904                     int size
4905                       = TREE_INT_CST_LOW (DECL_SIZE
4906                                           (TREE_OPERAND
4907                                            (TREE_OPERAND (varop, 0), 1)));
4908
4909                     newconst = fold (build (BIT_AND_EXPR,
4910                                             TREE_TYPE (varop), newconst,
4911                                             convert (TREE_TYPE (varop),
4912                                                      build_int_2 (size, 0))));
4913                   }
4914                                                          
4915
4916                 t = build (code, type, TREE_OPERAND (t, 0),
4917                            TREE_OPERAND (t, 1));
4918                 TREE_OPERAND (t, constopnum) = newconst;
4919                 return t;
4920               }
4921           }
4922         else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
4923           {
4924             if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
4925                 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4926                     && (code == EQ_EXPR || code == NE_EXPR)))
4927               {
4928                 tree newconst
4929                   = fold (build (MINUS_EXPR, TREE_TYPE (varop),
4930                                  constop, TREE_OPERAND (varop, 1)));
4931                 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
4932
4933                 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
4934                     && DECL_BIT_FIELD(TREE_OPERAND
4935                                       (TREE_OPERAND (varop, 0), 1)))
4936                   {
4937                     int size
4938                       = TREE_INT_CST_LOW (DECL_SIZE
4939                                           (TREE_OPERAND
4940                                            (TREE_OPERAND (varop, 0), 1)));
4941
4942                     newconst = fold (build (BIT_AND_EXPR,
4943                                             TREE_TYPE (varop), newconst,
4944                                             convert (TREE_TYPE (varop),
4945                                                      build_int_2 (size, 0))));
4946                   }
4947                                                          
4948
4949                 t = build (code, type, TREE_OPERAND (t, 0),
4950                            TREE_OPERAND (t, 1));
4951                 TREE_OPERAND (t, constopnum) = newconst;
4952                 return t;
4953               }
4954           }
4955       }
4956
4957       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
4958       if (TREE_CODE (arg1) == INTEGER_CST
4959           && TREE_CODE (arg0) != INTEGER_CST
4960           && tree_int_cst_sgn (arg1) > 0)
4961         {
4962           switch (TREE_CODE (t))
4963             {
4964             case GE_EXPR:
4965               code = GT_EXPR;
4966               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4967               t = build (code, type, TREE_OPERAND (t, 0), arg1);
4968               break;
4969
4970             case LT_EXPR:
4971               code = LE_EXPR;
4972               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4973               t = build (code, type, TREE_OPERAND (t, 0), arg1);
4974               break;
4975             }
4976         }
4977
4978       /* If this is an EQ or NE comparison with zero and ARG0 is
4979          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
4980          two operations, but the latter can be done in one less insn
4981          one machine that have only two-operand insns or on which a
4982          constant cannot be the first operand.  */
4983       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
4984           && TREE_CODE (arg0) == BIT_AND_EXPR)
4985         {
4986           if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
4987               && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
4988             return
4989               fold (build (code, type,
4990                            build (BIT_AND_EXPR, TREE_TYPE (arg0),
4991                                   build (RSHIFT_EXPR,
4992                                          TREE_TYPE (TREE_OPERAND (arg0, 0)),
4993                                          TREE_OPERAND (arg0, 1),
4994                                          TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
4995                                   convert (TREE_TYPE (arg0),
4996                                            integer_one_node)),
4997                            arg1));
4998           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
4999                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
5000             return
5001               fold (build (code, type,
5002                            build (BIT_AND_EXPR, TREE_TYPE (arg0),
5003                                   build (RSHIFT_EXPR,
5004                                          TREE_TYPE (TREE_OPERAND (arg0, 1)),
5005                                          TREE_OPERAND (arg0, 0),
5006                                          TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
5007                                   convert (TREE_TYPE (arg0),
5008                                            integer_one_node)),
5009                            arg1));
5010         }
5011
5012       /* If this is an NE or EQ comparison of zero against the result of a
5013          signed MOD operation whose second operand is a power of 2, make
5014          the MOD operation unsigned since it is simpler and equivalent.  */
5015       if ((code == NE_EXPR || code == EQ_EXPR)
5016           && integer_zerop (arg1)
5017           && ! TREE_UNSIGNED (TREE_TYPE (arg0))
5018           && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
5019               || TREE_CODE (arg0) == CEIL_MOD_EXPR
5020               || TREE_CODE (arg0) == FLOOR_MOD_EXPR
5021               || TREE_CODE (arg0) == ROUND_MOD_EXPR)
5022           && integer_pow2p (TREE_OPERAND (arg0, 1)))
5023         {
5024           tree newtype = unsigned_type (TREE_TYPE (arg0));
5025           tree newmod = build (TREE_CODE (arg0), newtype,
5026                                convert (newtype, TREE_OPERAND (arg0, 0)),
5027                                convert (newtype, TREE_OPERAND (arg0, 1)));
5028
5029           return build (code, type, newmod, convert (newtype, arg1));
5030         }
5031
5032       /* If this is an NE comparison of zero with an AND of one, remove the
5033          comparison since the AND will give the correct value.  */
5034       if (code == NE_EXPR && integer_zerop (arg1)
5035           && TREE_CODE (arg0) == BIT_AND_EXPR
5036           && integer_onep (TREE_OPERAND (arg0, 1)))
5037         return convert (type, arg0);
5038
5039       /* If we have (A & C) == C where C is a power of 2, convert this into
5040          (A & C) != 0.  Similarly for NE_EXPR.  */
5041       if ((code == EQ_EXPR || code == NE_EXPR)
5042           && TREE_CODE (arg0) == BIT_AND_EXPR
5043           && integer_pow2p (TREE_OPERAND (arg0, 1))
5044           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
5045         return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
5046                       arg0, integer_zero_node);
5047
5048       /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
5049          and similarly for >= into !=.  */
5050       if ((code == LT_EXPR || code == GE_EXPR)
5051           && TREE_UNSIGNED (TREE_TYPE (arg0))
5052           && TREE_CODE (arg1) == LSHIFT_EXPR
5053           && integer_onep (TREE_OPERAND (arg1, 0)))
5054         return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, 
5055                       build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
5056                              TREE_OPERAND (arg1, 1)),
5057                       convert (TREE_TYPE (arg0), integer_zero_node));
5058
5059       else if ((code == LT_EXPR || code == GE_EXPR)
5060                && TREE_UNSIGNED (TREE_TYPE (arg0))
5061                && (TREE_CODE (arg1) == NOP_EXPR
5062                    || TREE_CODE (arg1) == CONVERT_EXPR)
5063                && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
5064                && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
5065         return
5066           build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
5067                  convert (TREE_TYPE (arg0),
5068                           build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
5069                                  TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
5070                  convert (TREE_TYPE (arg0), integer_zero_node));
5071
5072       /* Simplify comparison of something with itself.  (For IEEE
5073          floating-point, we can only do some of these simplifications.)  */
5074       if (operand_equal_p (arg0, arg1, 0))
5075         {
5076           switch (code)
5077             {
5078             case EQ_EXPR:
5079             case GE_EXPR:
5080             case LE_EXPR:
5081               if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5082                 {
5083                   t = build_int_2 (1, 0);
5084                   TREE_TYPE (t) = type;
5085                   return t;
5086                 }
5087               code = EQ_EXPR;
5088               TREE_SET_CODE (t, code);
5089               break;
5090
5091             case NE_EXPR:
5092               /* For NE, we can only do this simplification if integer.  */
5093               if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5094                 break;
5095               /* ... fall through ...  */
5096             case GT_EXPR:
5097             case LT_EXPR:
5098               t = build_int_2 (0, 0);
5099               TREE_TYPE (t) = type;
5100               return t;
5101             }
5102         }
5103
5104       /* An unsigned comparison against 0 can be simplified.  */
5105       if (integer_zerop (arg1)
5106           && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
5107               || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
5108           && TREE_UNSIGNED (TREE_TYPE (arg1)))
5109         {
5110           switch (TREE_CODE (t))
5111             {
5112             case GT_EXPR:
5113               code = NE_EXPR;
5114               TREE_SET_CODE (t, NE_EXPR);
5115               break;
5116             case LE_EXPR:
5117               code = EQ_EXPR;
5118               TREE_SET_CODE (t, EQ_EXPR);
5119               break;
5120             case GE_EXPR:
5121               return omit_one_operand (type,
5122                                        convert (type, integer_one_node),
5123                                        arg0);
5124             case LT_EXPR:
5125               return omit_one_operand (type,
5126                                        convert (type, integer_zero_node),
5127                                        arg0);
5128             }
5129         }
5130
5131       /* If we are comparing an expression that just has comparisons
5132          of two integer values, arithmetic expressions of those comparisons,
5133          and constants, we can simplify it.  There are only three cases
5134          to check: the two values can either be equal, the first can be
5135          greater, or the second can be greater.  Fold the expression for
5136          those three values.  Since each value must be 0 or 1, we have
5137          eight possibilities, each of which corresponds to the constant 0
5138          or 1 or one of the six possible comparisons.
5139
5140          This handles common cases like (a > b) == 0 but also handles
5141          expressions like  ((x > y) - (y > x)) > 0, which supposedly
5142          occur in macroized code.  */
5143
5144       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
5145         {
5146           tree cval1 = 0, cval2 = 0;
5147           int save_p = 0;
5148
5149           if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
5150               /* Don't handle degenerate cases here; they should already
5151                  have been handled anyway.  */
5152               && cval1 != 0 && cval2 != 0
5153               && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
5154               && TREE_TYPE (cval1) == TREE_TYPE (cval2)
5155               && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
5156               && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
5157                                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
5158             {
5159               tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
5160               tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
5161
5162               /* We can't just pass T to eval_subst in case cval1 or cval2
5163                  was the same as ARG1.  */
5164
5165               tree high_result
5166                 = fold (build (code, type,
5167                                eval_subst (arg0, cval1, maxval, cval2, minval),
5168                                arg1));
5169               tree equal_result
5170                 = fold (build (code, type,
5171                                eval_subst (arg0, cval1, maxval, cval2, maxval),
5172                                arg1));
5173               tree low_result
5174                 = fold (build (code, type,
5175                                eval_subst (arg0, cval1, minval, cval2, maxval),
5176                                arg1));
5177
5178               /* All three of these results should be 0 or 1.  Confirm they
5179                  are.  Then use those values to select the proper code
5180                  to use.  */
5181
5182               if ((integer_zerop (high_result)
5183                    || integer_onep (high_result))
5184                   && (integer_zerop (equal_result)
5185                       || integer_onep (equal_result))
5186                   && (integer_zerop (low_result)
5187                       || integer_onep (low_result)))
5188                 {
5189                   /* Make a 3-bit mask with the high-order bit being the
5190                      value for `>', the next for '=', and the low for '<'.  */
5191                   switch ((integer_onep (high_result) * 4)
5192                           + (integer_onep (equal_result) * 2)
5193                           + integer_onep (low_result))
5194                     {
5195                     case 0:
5196                       /* Always false.  */
5197                       return omit_one_operand (type, integer_zero_node, arg0);
5198                     case 1:
5199                       code = LT_EXPR;
5200                       break;
5201                     case 2:
5202                       code = EQ_EXPR;
5203                       break;
5204                     case 3:
5205                       code = LE_EXPR;
5206                       break;
5207                     case 4:
5208                       code = GT_EXPR;
5209                       break;
5210                     case 5:
5211                       code = NE_EXPR;
5212                       break;
5213                     case 6:
5214                       code = GE_EXPR;
5215                       break;
5216                     case 7:
5217                       /* Always true.  */
5218                       return omit_one_operand (type, integer_one_node, arg0);
5219                     }
5220
5221                   t = build (code, type, cval1, cval2);
5222                   if (save_p)
5223                     return save_expr (t);
5224                   else
5225                     return fold (t);
5226                 }
5227             }
5228         }
5229
5230       /* If this is a comparison of a field, we may be able to simplify it.  */
5231       if ((TREE_CODE (arg0) == COMPONENT_REF
5232            || TREE_CODE (arg0) == BIT_FIELD_REF)
5233           && (code == EQ_EXPR || code == NE_EXPR)
5234           /* Handle the constant case even without -O
5235              to make sure the warnings are given.  */
5236           && (optimize || TREE_CODE (arg1) == INTEGER_CST))
5237         {
5238           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
5239           return t1 ? t1 : t;
5240         }
5241
5242       /* If this is a comparison of complex values and either or both
5243          sizes are a COMPLEX_EXPR, it is best to split up the comparisons
5244          and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.  This
5245          may prevent needless evaluations.  */
5246       if ((code == EQ_EXPR || code == NE_EXPR)
5247           && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
5248           && (TREE_CODE (arg0) == COMPLEX_EXPR
5249               || TREE_CODE (arg1) == COMPLEX_EXPR))
5250         {
5251           tree subtype = TREE_TYPE (TREE_TYPE (arg0));
5252           tree real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
5253           tree imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
5254           tree real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
5255           tree imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
5256
5257           return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
5258                                : TRUTH_ORIF_EXPR),
5259                               type,
5260                               fold (build (code, type, real0, real1)),
5261                               fold (build (code, type, imag0, imag1))));
5262         }
5263
5264       /* From here on, the only cases we handle are when the result is
5265          known to be a constant.
5266
5267          To compute GT, swap the arguments and do LT.
5268          To compute GE, do LT and invert the result.
5269          To compute LE, swap the arguments, do LT and invert the result.
5270          To compute NE, do EQ and invert the result.
5271
5272          Therefore, the code below must handle only EQ and LT.  */
5273
5274       if (code == LE_EXPR || code == GT_EXPR)
5275         {
5276           tem = arg0, arg0 = arg1, arg1 = tem;
5277           code = swap_tree_comparison (code);
5278         }
5279
5280       /* Note that it is safe to invert for real values here because we
5281          will check below in the one case that it matters.  */
5282
5283       invert = 0;
5284       if (code == NE_EXPR || code == GE_EXPR)
5285         {
5286           invert = 1;
5287           code = invert_tree_comparison (code);
5288         }
5289
5290       /* Compute a result for LT or EQ if args permit;
5291          otherwise return T.  */
5292       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
5293         {
5294           if (code == EQ_EXPR)
5295             t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
5296                                == TREE_INT_CST_LOW (arg1))
5297                               && (TREE_INT_CST_HIGH (arg0)
5298                                   == TREE_INT_CST_HIGH (arg1)),
5299                               0);
5300           else
5301             t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
5302                                ? INT_CST_LT_UNSIGNED (arg0, arg1)
5303                                : INT_CST_LT (arg0, arg1)),
5304                               0);
5305         }
5306
5307       /* Assume a nonexplicit constant cannot equal an explicit one,
5308          since such code would be undefined anyway.
5309          Exception: on sysvr4, using #pragma weak,
5310          a label can come out as 0.  */
5311       else if (TREE_CODE (arg1) == INTEGER_CST
5312                && !integer_zerop (arg1)
5313                && TREE_CONSTANT (arg0)
5314                && TREE_CODE (arg0) == ADDR_EXPR
5315                && code == EQ_EXPR)
5316         t1 = build_int_2 (0, 0);
5317
5318       /* Two real constants can be compared explicitly.  */
5319       else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
5320         {
5321           /* If either operand is a NaN, the result is false with two
5322              exceptions: First, an NE_EXPR is true on NaNs, but that case
5323              is already handled correctly since we will be inverting the
5324              result for NE_EXPR.  Second, if we had inverted a LE_EXPR
5325              or a GE_EXPR into a LT_EXPR, we must return true so that it
5326              will be inverted into false.  */
5327
5328           if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
5329               || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
5330             t1 = build_int_2 (invert && code == LT_EXPR, 0);
5331
5332           else if (code == EQ_EXPR)
5333             t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
5334                                                  TREE_REAL_CST (arg1)),
5335                               0);
5336           else
5337             t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
5338                                                 TREE_REAL_CST (arg1)),
5339                               0);
5340         }
5341
5342       if (t1 == NULL_TREE)
5343         return t;
5344
5345       if (invert)
5346         TREE_INT_CST_LOW (t1) ^= 1;
5347
5348       TREE_TYPE (t1) = type;
5349       return t1;
5350
5351     case COND_EXPR:
5352       /* Pedantic ANSI C says that a conditional expression is never an lvalue,
5353          so all simple results must be passed through pedantic_non_lvalue.  */
5354       if (TREE_CODE (arg0) == INTEGER_CST)
5355         return pedantic_non_lvalue
5356           (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
5357       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
5358         return pedantic_omit_one_operand (type, arg1, arg0);
5359
5360       /* If the second operand is zero, invert the comparison and swap
5361          the second and third operands.  Likewise if the second operand
5362          is constant and the third is not or if the third operand is
5363          equivalent to the first operand of the comparison.  */
5364
5365       if (integer_zerop (arg1)
5366           || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
5367           || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5368               && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
5369                                                  TREE_OPERAND (t, 2),
5370                                                  TREE_OPERAND (arg0, 1))))
5371         {
5372           /* See if this can be inverted.  If it can't, possibly because
5373              it was a floating-point inequality comparison, don't do
5374              anything.  */
5375           tem = invert_truthvalue (arg0);
5376
5377           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
5378             {
5379               t = build (code, type, tem,
5380                          TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
5381               arg0 = tem;
5382               arg1 = TREE_OPERAND (t, 2);
5383               STRIP_NOPS (arg1);
5384             }
5385         }
5386
5387       /* If we have A op B ? A : C, we may be able to convert this to a
5388          simpler expression, depending on the operation and the values
5389          of B and C.  IEEE floating point prevents this though,
5390          because A or B might be -0.0 or a NaN.  */
5391
5392       if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5393           && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5394               || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
5395               || flag_fast_math)
5396           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
5397                                              arg1, TREE_OPERAND (arg0, 1)))
5398         {
5399           tree arg2 = TREE_OPERAND (t, 2);
5400           enum tree_code comp_code = TREE_CODE (arg0);
5401
5402           STRIP_NOPS (arg2);
5403
5404           /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
5405              depending on the comparison operation.  */
5406           if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
5407                ? real_zerop (TREE_OPERAND (arg0, 1))
5408                : integer_zerop (TREE_OPERAND (arg0, 1)))
5409               && TREE_CODE (arg2) == NEGATE_EXPR
5410               && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5411             switch (comp_code)
5412               {
5413               case EQ_EXPR:
5414                 return pedantic_non_lvalue
5415                   (fold (build1 (NEGATE_EXPR, type, arg1)));
5416               case NE_EXPR:
5417                 return pedantic_non_lvalue (convert (type, arg1));
5418               case GE_EXPR:
5419               case GT_EXPR:
5420                 return pedantic_non_lvalue
5421                   (convert (type, fold (build1 (ABS_EXPR,
5422                                                 TREE_TYPE (arg1), arg1))));
5423               case LE_EXPR:
5424               case LT_EXPR:
5425                 return pedantic_non_lvalue
5426                   (fold (build1 (NEGATE_EXPR, type,
5427                                  convert (type,
5428                                           fold (build1 (ABS_EXPR,
5429                                                         TREE_TYPE (arg1),
5430                                                         arg1))))));
5431               }
5432
5433           /* If this is A != 0 ? A : 0, this is simply A.  For ==, it is
5434              always zero.  */
5435
5436           if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
5437             {
5438               if (comp_code == NE_EXPR)
5439                 return pedantic_non_lvalue (convert (type, arg1));
5440               else if (comp_code == EQ_EXPR)
5441                 return pedantic_non_lvalue (convert (type, integer_zero_node));
5442             }
5443
5444           /* If this is A op B ? A : B, this is either A, B, min (A, B),
5445              or max (A, B), depending on the operation.  */
5446
5447           if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
5448                                               arg2, TREE_OPERAND (arg0, 0)))
5449             {
5450               tree comp_op0 = TREE_OPERAND (arg0, 0);
5451               tree comp_op1 = TREE_OPERAND (arg0, 1);
5452               tree comp_type = TREE_TYPE (comp_op0);
5453
5454               switch (comp_code)
5455                 {
5456                 case EQ_EXPR:
5457                   return pedantic_non_lvalue (convert (type, arg2));
5458                 case NE_EXPR:
5459                   return pedantic_non_lvalue (convert (type, arg1));
5460                 case LE_EXPR:
5461                 case LT_EXPR:
5462                   return pedantic_non_lvalue
5463                     (convert (type, (fold (build (MIN_EXPR, comp_type,
5464                                                   comp_op0, comp_op1)))));
5465                 case GE_EXPR:
5466                 case GT_EXPR:
5467                   return pedantic_non_lvalue
5468                     (convert (type, fold (build (MAX_EXPR, comp_type,
5469                                                  comp_op0, comp_op1))));
5470                 }
5471             }
5472
5473           /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5474              we might still be able to simplify this.  For example,
5475              if C1 is one less or one more than C2, this might have started
5476              out as a MIN or MAX and been transformed by this function.
5477              Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
5478
5479           if (INTEGRAL_TYPE_P (type)
5480               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5481               && TREE_CODE (arg2) == INTEGER_CST)
5482             switch (comp_code)
5483               {
5484               case EQ_EXPR:
5485                 /* We can replace A with C1 in this case.  */
5486                 arg1 = convert (type, TREE_OPERAND (arg0, 1));
5487                 t = build (code, type, TREE_OPERAND (t, 0), arg1,
5488                            TREE_OPERAND (t, 2));
5489                 break;
5490
5491               case LT_EXPR:
5492                 /* If C1 is C2 + 1, this is min(A, C2).  */
5493                 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
5494                     && operand_equal_p (TREE_OPERAND (arg0, 1),
5495                                         const_binop (PLUS_EXPR, arg2,
5496                                                      integer_one_node, 0), 1))
5497                   return pedantic_non_lvalue
5498                     (fold (build (MIN_EXPR, type, arg1, arg2)));
5499                 break;
5500
5501               case LE_EXPR:
5502                 /* If C1 is C2 - 1, this is min(A, C2).  */
5503                 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
5504                     && operand_equal_p (TREE_OPERAND (arg0, 1),
5505                                         const_binop (MINUS_EXPR, arg2,
5506                                                      integer_one_node, 0), 1))
5507                   return pedantic_non_lvalue
5508                     (fold (build (MIN_EXPR, type, arg1, arg2)));
5509                 break;
5510
5511               case GT_EXPR:
5512                 /* If C1 is C2 - 1, this is max(A, C2).  */
5513                 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
5514                     && operand_equal_p (TREE_OPERAND (arg0, 1),
5515                                         const_binop (MINUS_EXPR, arg2,
5516                                                      integer_one_node, 0), 1))
5517                   return pedantic_non_lvalue
5518                     (fold (build (MAX_EXPR, type, arg1, arg2)));
5519                 break;
5520
5521               case GE_EXPR:
5522                 /* If C1 is C2 + 1, this is max(A, C2).  */
5523                 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
5524                     && operand_equal_p (TREE_OPERAND (arg0, 1),
5525                                         const_binop (PLUS_EXPR, arg2,
5526                                                      integer_one_node, 0), 1))
5527                   return pedantic_non_lvalue
5528                     (fold (build (MAX_EXPR, type, arg1, arg2)));
5529                 break;
5530               }
5531         }
5532
5533       /* If the second operand is simpler than the third, swap them
5534          since that produces better jump optimization results.  */
5535       if ((TREE_CONSTANT (arg1) || TREE_CODE_CLASS (TREE_CODE (arg1)) == 'd'
5536            || TREE_CODE (arg1) == SAVE_EXPR)
5537           && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
5538                 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 2))) == 'd'
5539                 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
5540         {
5541           /* See if this can be inverted.  If it can't, possibly because
5542              it was a floating-point inequality comparison, don't do
5543              anything.  */
5544           tem = invert_truthvalue (arg0);
5545
5546           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
5547             {
5548               t = build (code, type, tem,
5549                          TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
5550               arg0 = tem;
5551               arg1 = TREE_OPERAND (t, 2);
5552               STRIP_NOPS (arg1);
5553             }
5554         }
5555
5556       /* Convert A ? 1 : 0 to simply A.  */
5557       if (integer_onep (TREE_OPERAND (t, 1))
5558           && integer_zerop (TREE_OPERAND (t, 2))
5559           /* If we try to convert TREE_OPERAND (t, 0) to our type, the
5560              call to fold will try to move the conversion inside 
5561              a COND, which will recurse.  In that case, the COND_EXPR
5562              is probably the best choice, so leave it alone.  */
5563           && type == TREE_TYPE (arg0))
5564         return pedantic_non_lvalue (arg0);
5565
5566       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
5567          operation is simply A & 2.  */
5568
5569       if (integer_zerop (TREE_OPERAND (t, 2))
5570           && TREE_CODE (arg0) == NE_EXPR
5571           && integer_zerop (TREE_OPERAND (arg0, 1))
5572           && integer_pow2p (arg1)
5573           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
5574           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
5575                               arg1, 1))
5576         return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
5577
5578       return t;
5579
5580     case COMPOUND_EXPR:
5581       /* When pedantic, a compound expression can be neither an lvalue
5582          nor an integer constant expression.  */
5583       if (TREE_SIDE_EFFECTS (arg0) || pedantic)
5584         return t;
5585       /* Don't let (0, 0) be null pointer constant.  */
5586       if (integer_zerop (arg1))
5587         return non_lvalue (arg1);
5588       return arg1;
5589
5590     case COMPLEX_EXPR:
5591       if (wins)
5592         return build_complex (type, arg0, arg1);
5593       return t;
5594
5595     case REALPART_EXPR:
5596       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5597         return t;
5598       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5599         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
5600                                  TREE_OPERAND (arg0, 1));
5601       else if (TREE_CODE (arg0) == COMPLEX_CST)
5602         return TREE_REALPART (arg0);
5603       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5604         return fold (build (TREE_CODE (arg0), type,
5605                             fold (build1 (REALPART_EXPR, type,
5606                                           TREE_OPERAND (arg0, 0))),
5607                             fold (build1 (REALPART_EXPR,
5608                                           type, TREE_OPERAND (arg0, 1)))));
5609       return t;
5610
5611     case IMAGPART_EXPR:
5612       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5613         return convert (type, integer_zero_node);
5614       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5615         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
5616                                  TREE_OPERAND (arg0, 0));
5617       else if (TREE_CODE (arg0) == COMPLEX_CST)
5618         return TREE_IMAGPART (arg0);
5619       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5620         return fold (build (TREE_CODE (arg0), type,
5621                             fold (build1 (IMAGPART_EXPR, type,
5622                                           TREE_OPERAND (arg0, 0))),
5623                             fold (build1 (IMAGPART_EXPR, type,
5624                                           TREE_OPERAND (arg0, 1)))));
5625       return t;
5626
5627       /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
5628          appropriate.  */
5629     case CLEANUP_POINT_EXPR:
5630       if (! TREE_SIDE_EFFECTS (arg0))
5631         return TREE_OPERAND (t, 0);
5632
5633       {
5634         enum tree_code code0 = TREE_CODE (arg0);
5635         int kind0 = TREE_CODE_CLASS (code0);
5636         tree arg00 = TREE_OPERAND (arg0, 0);
5637         tree arg01;
5638
5639         if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
5640           return fold (build1 (code0, type, 
5641                                fold (build1 (CLEANUP_POINT_EXPR,
5642                                              TREE_TYPE (arg00), arg00))));
5643
5644         if (kind0 == '<' || kind0 == '2'
5645             || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
5646             || code0 == TRUTH_AND_EXPR   || code0 == TRUTH_OR_EXPR
5647             || code0 == TRUTH_XOR_EXPR)
5648           {
5649             arg01 = TREE_OPERAND (arg0, 1);
5650
5651             if (! TREE_SIDE_EFFECTS (arg00))
5652               return fold (build (code0, type, arg00,
5653                                   fold (build1 (CLEANUP_POINT_EXPR,
5654                                                 TREE_TYPE (arg01), arg01))));
5655
5656             if (! TREE_SIDE_EFFECTS (arg01))
5657               return fold (build (code0, type,
5658                                   fold (build1 (CLEANUP_POINT_EXPR,
5659                                                 TREE_TYPE (arg00), arg00)),
5660                                   arg01));
5661           }
5662
5663         return t;
5664       }
5665
5666     default:
5667       return t;
5668     } /* switch (code) */
5669 }