OSDN Git Service

Add prototypes for static functions.
[pf3gnuchains/gcc-fork.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2    Copyright (C) 1987, 1988, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /*@@ Fix lossage on folding division of big integers.  */
21
22 /*@@ This file should be rewritten to use an arbitrary precision
23   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25   @@ The routines that translate from the ap rep should
26   @@ warn if precision et. al. is lost.
27   @@ This would also make life easier when this technology is used
28   @@ for cross-compilers.  */
29
30
31 /* The entry points in this file are fold, size_int and size_binop.
32
33    fold takes a tree as argument and returns a simplified tree.
34
35    size_binop takes a tree code for an arithmetic operation
36    and two operands that are trees, and produces a tree for the
37    result, assuming the type comes from `sizetype'.
38
39    size_int takes an integer value, and creates a tree constant
40    with type from `sizetype'.  */
41    
42 #include <stdio.h>
43 #include <setjmp.h>
44 #include "config.h"
45 #include "flags.h"
46 #include "tree.h"
47
48 /* Handle floating overflow for `const_binop'.  */
49 static jmp_buf float_error;
50
51 static void encode      PROTO((short *, HOST_WIDE_INT, HOST_WIDE_INT));
52 static void decode      PROTO((short *, HOST_WIDE_INT *, HOST_WIDE_INT *));
53 static int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT,
54                                        HOST_WIDE_INT, HOST_WIDE_INT,
55                                        HOST_WIDE_INT, HOST_WIDE_INT *,
56                                        HOST_WIDE_INT *, HOST_WIDE_INT *,
57                                        HOST_WIDE_INT *));
58 static int split_tree   PROTO((tree, enum tree_code, tree *, tree *, int *));
59 static tree const_binop PROTO((enum tree_code, tree, tree, int));
60 static tree fold_convert PROTO((tree, tree));
61 static enum tree_code invert_tree_comparison PROTO((enum tree_code));
62 static enum tree_code swap_tree_comparison PROTO((enum tree_code));
63 static int operand_equal_for_comparison_p PROTO((tree, tree, tree));
64 static int twoval_comparison_p PROTO((tree, tree *, tree *));
65 static tree eval_subst  PROTO((tree, tree, tree, tree, tree));
66 static tree omit_one_operand PROTO((tree, tree, tree));
67 static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree));
68 static tree make_bit_field_ref PROTO((tree, tree, int, int, int));
69 static tree optimize_bit_field_compare PROTO((enum tree_code, tree,
70                                               tree, tree));
71 static tree decode_field_reference PROTO((tree, int *, int *,
72                                           enum machine_mode *, int *,
73                                           int *, tree *));
74 static int all_ones_mask_p PROTO((tree, int));
75 static int simple_operand_p PROTO((tree));
76 static tree range_test  PROTO((enum tree_code, tree, enum tree_code,
77                                enum tree_code, tree, tree, tree));
78 static tree fold_truthop PROTO((enum tree_code, tree, tree, tree));
79
80 #ifndef BRANCH_COST
81 #define BRANCH_COST 1
82 #endif
83
84 /* Yield nonzero if a signed left shift of A by B bits overflows.  */
85 #define left_shift_overflows(a, b)  ((a)  !=  ((a) << (b)) >> (b))
86
87 /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow.
88    Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1.
89    Then this yields nonzero if overflow occurred during the addition.
90    Overflow occurs if A and B have the same sign, but A and SUM differ in sign.
91    Use `^' to test whether signs differ, and `< 0' to isolate the sign.  */
92 #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
93 \f
94 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
95    We do that by representing the two-word integer as MAX_SHORTS shorts,
96    with only 8 bits stored in each short, as a positive number.  */
97
98 /* Unpack a two-word integer into MAX_SHORTS shorts.
99    LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
100    SHORTS points to the array of shorts.  */
101
102 static void
103 encode (shorts, low, hi)
104      short *shorts;
105      HOST_WIDE_INT low, hi;
106 {
107   register int i;
108
109   for (i = 0; i < MAX_SHORTS / 2; i++)
110     {
111       shorts[i] = (low >> (i * 8)) & 0xff;
112       shorts[i + MAX_SHORTS / 2] = (hi >> (i * 8) & 0xff);
113     }
114 }
115
116 /* Pack an array of MAX_SHORTS shorts into a two-word integer.
117    SHORTS points to the array of shorts.
118    The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces.  */
119
120 static void
121 decode (shorts, low, hi)
122      short *shorts;
123      HOST_WIDE_INT *low, *hi;
124 {
125   register int i;
126   HOST_WIDE_INT lv = 0, hv = 0;
127
128   for (i = 0; i < MAX_SHORTS / 2; i++)
129     {
130       lv |= (HOST_WIDE_INT) shorts[i] << (i * 8);
131       hv |= (HOST_WIDE_INT) shorts[i + MAX_SHORTS / 2] << (i * 8);
132     }
133
134   *low = lv, *hi = hv;
135 }
136 \f
137 /* Make the integer constant T valid for its type
138    by setting to 0 or 1 all the bits in the constant
139    that don't belong in the type.
140    Yield 1 if a signed overflow occurs, 0 otherwise.
141    If OVERFLOW is nonzero, a signed overflow has already occurred
142    in calculating T, so propagate it.  */
143
144 int
145 force_fit_type (t, overflow)
146      tree t;
147      int overflow;
148 {
149   HOST_WIDE_INT low, high;
150   register int prec;
151
152   if (TREE_CODE (t) != INTEGER_CST)
153     return overflow;
154
155   low = TREE_INT_CST_LOW (t);
156   high = TREE_INT_CST_HIGH (t);
157
158   if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
159     prec = POINTER_SIZE;
160   else
161     prec = TYPE_PRECISION (TREE_TYPE (t));
162
163   /* First clear all bits that are beyond the type's precision.  */
164
165   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
166     ;
167   else if (prec > HOST_BITS_PER_WIDE_INT)
168     {
169       TREE_INT_CST_HIGH (t)
170         &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
171     }
172   else
173     {
174       TREE_INT_CST_HIGH (t) = 0;
175       if (prec < HOST_BITS_PER_WIDE_INT)
176         TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec);
177     }
178
179   /* Unsigned types do not suffer sign extension or overflow.  */
180   if (TREE_UNSIGNED (TREE_TYPE (t)))
181     return 0;
182
183   /* If the value's sign bit is set, extend the sign.  */
184   if (prec != 2 * HOST_BITS_PER_WIDE_INT
185       && (prec > HOST_BITS_PER_WIDE_INT
186           ? (TREE_INT_CST_HIGH (t)
187              & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
188           : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1))))
189     {
190       /* Value is negative:
191          set to 1 all the bits that are outside this type's precision.  */
192       if (prec > HOST_BITS_PER_WIDE_INT)
193         {
194           TREE_INT_CST_HIGH (t)
195             |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
196         }
197       else
198         {
199           TREE_INT_CST_HIGH (t) = -1;
200           if (prec < HOST_BITS_PER_WIDE_INT)
201             TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec);
202         }
203     }
204
205   /* Yield nonzero if signed overflow occurred.  */
206   return
207     ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
208      != 0);
209 }
210 \f
211 /* Add two doubleword integers with doubleword result.
212    Each argument is given as two `HOST_WIDE_INT' pieces.
213    One argument is L1 and H1; the other, L2 and H2.
214    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
215    We use the 8-shorts representation internally.  */
216
217 int
218 add_double (l1, h1, l2, h2, lv, hv)
219      HOST_WIDE_INT l1, h1, l2, h2;
220      HOST_WIDE_INT *lv, *hv;
221 {
222   short arg1[MAX_SHORTS];
223   short arg2[MAX_SHORTS];
224   register int carry = 0;
225   register int i;
226
227   encode (arg1, l1, h1);
228   encode (arg2, l2, h2);
229
230   for (i = 0; i < MAX_SHORTS; i++)
231     {
232       carry += arg1[i] + arg2[i];
233       arg1[i] = carry & 0xff;
234       carry >>= 8;
235     }
236
237   decode (arg1, lv, hv);
238   return overflow_sum_sign (h1, h2, *hv);
239 }
240
241 /* Negate a doubleword integer with doubleword result.
242    Return nonzero if the operation overflows, assuming it's signed.
243    The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
244    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
245    We use the 8-shorts representation internally.  */
246
247 int
248 neg_double (l1, h1, lv, hv)
249      HOST_WIDE_INT l1, h1;
250      HOST_WIDE_INT *lv, *hv;
251 {
252   if (l1 == 0)
253     {
254       *lv = 0;
255       *hv = - h1;
256       return (*hv & h1) < 0;
257     }
258   else
259     {
260       *lv = - l1;
261       *hv = ~ h1;
262       return 0;
263     }
264 }
265 \f
266 /* Multiply two doubleword integers with doubleword result.
267    Return nonzero if the operation overflows, assuming it's signed.
268    Each argument is given as two `HOST_WIDE_INT' pieces.
269    One argument is L1 and H1; the other, L2 and H2.
270    The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV.
271    We use the 8-shorts representation internally.  */
272
273 int
274 mul_double (l1, h1, l2, h2, lv, hv)
275      HOST_WIDE_INT l1, h1, l2, h2;
276      HOST_WIDE_INT *lv, *hv;
277 {
278   short arg1[MAX_SHORTS];
279   short arg2[MAX_SHORTS];
280   short prod[MAX_SHORTS * 2];
281   register int carry = 0;
282   register int i, j, k;
283   HOST_WIDE_INT toplow, tophigh, neglow, neghigh;
284
285   /* These cases are used extensively, arising from pointer combinations.  */
286   if (h2 == 0)
287     {
288       if (l2 == 2)
289         {
290           int overflow = left_shift_overflows (h1, 1);
291           unsigned HOST_WIDE_INT temp = l1 + l1;
292           *hv = (h1 << 1) + (temp < l1);
293           *lv = temp;
294           return overflow;
295         }
296       if (l2 == 4)
297         {
298           int overflow = left_shift_overflows (h1, 2);
299           unsigned HOST_WIDE_INT temp = l1 + l1;
300           h1 = (h1 << 2) + ((temp < l1) << 1);
301           l1 = temp;
302           temp += temp;
303           h1 += (temp < l1);
304           *lv = temp;
305           *hv = h1;
306           return overflow;
307         }
308       if (l2 == 8)
309         {
310           int overflow = left_shift_overflows (h1, 3);
311           unsigned HOST_WIDE_INT temp = l1 + l1;
312           h1 = (h1 << 3) + ((temp < l1) << 2);
313           l1 = temp;
314           temp += temp;
315           h1 += (temp < l1) << 1;
316           l1 = temp;
317           temp += temp;
318           h1 += (temp < l1);
319           *lv = temp;
320           *hv = h1;
321           return overflow;
322         }
323     }
324
325   encode (arg1, l1, h1);
326   encode (arg2, l2, h2);
327
328   bzero (prod, sizeof prod);
329
330   for (i = 0; i < MAX_SHORTS; i++)
331     for (j = 0; j < MAX_SHORTS; j++)
332       {
333         k = i + j;
334         carry = arg1[i] * arg2[j];
335         while (carry)
336           {
337             carry += prod[k];
338             prod[k] = carry & 0xff;
339             carry >>= 8;
340             k++;
341           }
342       }
343
344   decode (prod, lv, hv);        /* This ignores
345                                    prod[MAX_SHORTS] -> prod[MAX_SHORTS*2-1] */
346
347   /* Check for overflow by calculating the top half of the answer in full;
348      it should agree with the low half's sign bit.  */
349   decode (prod+MAX_SHORTS, &toplow, &tophigh);
350   if (h1 < 0)
351     {
352       neg_double (l2, h2, &neglow, &neghigh);
353       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
354     }
355   if (h2 < 0)
356     {
357       neg_double (l1, h1, &neglow, &neghigh);
358       add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
359     }
360   return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
361 }
362 \f
363 /* Shift the doubleword integer in L1, H1 left by COUNT places
364    keeping only PREC bits of result.
365    Shift right if COUNT is negative.
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 lshift_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   short arg1[MAX_SHORTS];
377   register int i;
378   register int carry;
379
380   if (count < 0)
381     {
382       rshift_double (l1, h1, - count, prec, lv, hv, arith);
383       return;
384     }
385
386   encode (arg1, l1, h1);
387
388   if (count > prec)
389     count = prec;
390
391   while (count > 0)
392     {
393       carry = 0;
394       for (i = 0; i < MAX_SHORTS; i++)
395         {
396           carry += arg1[i] << 1;
397           arg1[i] = carry & 0xff;
398           carry >>= 8;
399         }
400       count--;
401     }
402
403   decode (arg1, lv, hv);
404 }
405
406 /* Shift the doubleword integer in L1, H1 right by COUNT places
407    keeping only PREC bits of result.  COUNT must be positive.
408    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
409    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
410
411 void
412 rshift_double (l1, h1, count, prec, lv, hv, arith)
413      HOST_WIDE_INT l1, h1, count;
414      int prec;
415      HOST_WIDE_INT *lv, *hv;
416      int arith;
417 {
418   short arg1[MAX_SHORTS];
419   register int i;
420   register int carry;
421
422   encode (arg1, l1, h1);
423
424   if (count > prec)
425     count = prec;
426
427   while (count > 0)
428     {
429       carry = arith && arg1[7] >> 7; 
430       for (i = MAX_SHORTS - 1; i >= 0; i--)
431         {
432           carry <<= 8;
433           carry += arg1[i];
434           arg1[i] = (carry >> 1) & 0xff;
435         }
436       count--;
437     }
438
439   decode (arg1, lv, hv);
440 }
441 \f
442 /* Rotate the doubldword integer in L1, H1 left by COUNT places
443    keeping only PREC bits of result.
444    Rotate right if COUNT is negative.
445    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
446
447 void
448 lrotate_double (l1, h1, count, prec, lv, hv)
449      HOST_WIDE_INT l1, h1, count;
450      int prec;
451      HOST_WIDE_INT *lv, *hv;
452 {
453   short arg1[MAX_SHORTS];
454   register int i;
455   register int carry;
456
457   if (count < 0)
458     {
459       rrotate_double (l1, h1, - count, prec, lv, hv);
460       return;
461     }
462
463   encode (arg1, l1, h1);
464
465   if (count > prec)
466     count = prec;
467
468   carry = arg1[MAX_SHORTS - 1] >> 7;
469   while (count > 0)
470     {
471       for (i = 0; i < MAX_SHORTS; i++)
472         {
473           carry += arg1[i] << 1;
474           arg1[i] = carry & 0xff;
475           carry >>= 8;
476         }
477       count--;
478     }
479
480   decode (arg1, lv, hv);
481 }
482
483 /* Rotate the doubleword integer in L1, H1 left by COUNT places
484    keeping only PREC bits of result.  COUNT must be positive.
485    Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV.  */
486
487 void
488 rrotate_double (l1, h1, count, prec, lv, hv)
489      HOST_WIDE_INT l1, h1, count;
490      int prec;
491      HOST_WIDE_INT *lv, *hv;
492 {
493   short arg1[MAX_SHORTS];
494   register int i;
495   register int carry;
496
497   encode (arg1, l1, h1);
498
499   if (count > prec)
500     count = prec;
501
502   carry = arg1[0] & 1;
503   while (count > 0)
504     {
505       for (i = MAX_SHORTS - 1; i >= 0; i--)
506         {
507           carry <<= 8;
508           carry += arg1[i];
509           arg1[i] = (carry >> 1) & 0xff;
510         }
511       count--;
512     }
513
514   decode (arg1, lv, hv);
515 }
516 \f
517 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
518    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
519    CODE is a tree code for a kind of division, one of
520    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
521    or EXACT_DIV_EXPR
522    It controls how the quotient is rounded to a integer.
523    Return nonzero if the operation overflows.
524    UNS nonzero says do unsigned division.  */
525
526 static int
527 div_and_round_double (code, uns,
528                       lnum_orig, hnum_orig, lden_orig, hden_orig,
529                       lquo, hquo, lrem, hrem)
530      enum tree_code code;
531      int uns;
532      HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */
533      HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */
534      HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem;
535 {
536   int quo_neg = 0;
537   short num[MAX_SHORTS + 1];    /* extra element for scaling.  */
538   short den[MAX_SHORTS], quo[MAX_SHORTS];
539   register int i, j, work;
540   register int carry = 0;
541   HOST_WIDE_INT lnum = lnum_orig;
542   HOST_WIDE_INT hnum = hnum_orig;
543   HOST_WIDE_INT lden = lden_orig;
544   HOST_WIDE_INT hden = hden_orig;
545   int overflow = 0;
546
547   if ((hden == 0) && (lden == 0))
548     abort ();
549
550   /* calculate quotient sign and convert operands to unsigned.  */
551   if (!uns) 
552     {
553       if (hnum < 0)
554         {
555           quo_neg = ~ quo_neg;
556           /* (minimum integer) / (-1) is the only overflow case.  */
557           if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1)
558             overflow = 1;
559         }
560       if (hden < 0) 
561         {
562           quo_neg = ~ quo_neg;
563           neg_double (lden, hden, &lden, &hden);
564         }
565     }
566
567   if (hnum == 0 && hden == 0)
568     {                           /* single precision */
569       *hquo = *hrem = 0;
570       /* This unsigned division rounds toward zero.  */
571       *lquo = lnum / (unsigned HOST_WIDE_INT) lden;
572       goto finish_up;
573     }
574
575   if (hnum == 0)
576     {                           /* trivial case: dividend < divisor */
577       /* hden != 0 already checked.  */
578       *hquo = *lquo = 0;
579       *hrem = hnum;
580       *lrem = lnum;
581       goto finish_up;
582     }
583
584   bzero (quo, sizeof quo);
585
586   bzero (num, sizeof num);      /* to zero 9th element */
587   bzero (den, sizeof den);
588
589   encode (num, lnum, hnum); 
590   encode (den, lden, hden);
591
592   /* This code requires more than just hden == 0.
593      We also have to require that we don't need more than three bytes
594      to hold CARRY.  If we ever did need four bytes to hold it, we
595      would lose part of it when computing WORK on the next round.  */
596   if (hden == 0 && (((unsigned HOST_WIDE_INT) lden << 8) >> 8) == lden)
597     {                           /* simpler algorithm */
598       /* hnum != 0 already checked.  */
599       for (i = MAX_SHORTS - 1; i >= 0; i--)
600         {
601           work = num[i] + (carry << 8);
602           quo[i] = work / (unsigned HOST_WIDE_INT) lden;
603           carry = work % (unsigned HOST_WIDE_INT) lden;
604         }
605     }
606   else {                        /* full double precision,
607                                    with thanks to Don Knuth's
608                                    "Seminumerical Algorithms".  */
609 #define BASE 256
610     int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
611
612     /* Find the highest non-zero divisor digit.  */
613     for (i = MAX_SHORTS - 1; ; i--)
614       if (den[i] != 0) {
615         den_hi_sig = i;
616         break;
617       }
618     for (i = MAX_SHORTS - 1; ; i--)
619       if (num[i] != 0) {
620         num_hi_sig = i;
621         break;
622       }
623     quo_hi_sig = num_hi_sig - den_hi_sig + 1;
624
625     /* Insure that the first digit of the divisor is at least BASE/2.
626        This is required by the quotient digit estimation algorithm.  */
627
628     scale = BASE / (den[den_hi_sig] + 1);
629     if (scale > 1) {            /* scale divisor and dividend */
630       carry = 0;
631       for (i = 0; i <= MAX_SHORTS - 1; i++) {
632         work = (num[i] * scale) + carry;
633         num[i] = work & 0xff;
634         carry = work >> 8;
635         if (num[i] != 0) num_hi_sig = i;
636       }
637       carry = 0;
638       for (i = 0; i <= MAX_SHORTS - 1; i++) {
639         work = (den[i] * scale) + carry;
640         den[i] = work & 0xff;
641         carry = work >> 8;
642         if (den[i] != 0) den_hi_sig = i;
643       }
644     }
645
646     /* Main loop */
647     for (i = quo_hi_sig; i > 0; i--) {
648       /* guess the next quotient digit, quo_est, by dividing the first
649          two remaining dividend digits by the high order quotient digit.
650          quo_est is never low and is at most 2 high.  */
651
652       int num_hi;               /* index of highest remaining dividend digit */
653
654       num_hi = i + den_hi_sig;
655
656       work = (num[num_hi] * BASE) + (num_hi > 0 ? num[num_hi - 1] : 0);
657       if (num[num_hi] != den[den_hi_sig]) {
658         quo_est = work / den[den_hi_sig];
659       }
660       else {
661         quo_est = BASE - 1;
662       }
663
664       /* refine quo_est so it's usually correct, and at most one high.   */
665       while ((den[den_hi_sig - 1] * quo_est)
666              > (((work - (quo_est * den[den_hi_sig])) * BASE)
667                  + ((num_hi - 1) > 0 ? num[num_hi - 2] : 0)))
668         quo_est--;
669
670       /* Try QUO_EST as the quotient digit, by multiplying the
671          divisor by QUO_EST and subtracting from the remaining dividend.
672          Keep in mind that QUO_EST is the I - 1st digit.  */
673
674       carry = 0;
675
676       for (j = 0; j <= den_hi_sig; j++)
677         {
678           int digit;
679
680           work = num[i + j - 1] - (quo_est * den[j]) + carry;
681           digit = work & 0xff;
682           carry = work >> 8;
683           if (digit < 0)
684             {
685               digit += BASE;
686               carry--;
687             }
688           num[i + j - 1] = digit;
689         }
690
691       /* if quo_est was high by one, then num[i] went negative and
692          we need to correct things.  */
693
694       if (num[num_hi] < 0)
695         {
696           quo_est--;
697           carry = 0;            /* add divisor back in */
698           for (j = 0; j <= den_hi_sig; j++)
699             {
700               work = num[i + j - 1] + den[j] + carry;
701               if (work > BASE)
702                 {
703                   work -= BASE;
704                   carry = 1;
705                 }
706               else
707                 {
708                   carry = 0;
709                 }
710               num[i + j - 1] = work;
711             }
712           num [num_hi] += carry;
713         }
714
715       /* store the quotient digit.  */
716       quo[i - 1] = quo_est;
717     }
718   }
719
720   decode (quo, lquo, hquo);
721
722  finish_up:
723   /* if result is negative, make it so.  */
724   if (quo_neg)
725     neg_double (*lquo, *hquo, lquo, hquo);
726
727   /* compute trial remainder:  rem = num - (quo * den)  */
728   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
729   neg_double (*lrem, *hrem, lrem, hrem);
730   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
731
732   switch (code)
733     {
734     case TRUNC_DIV_EXPR:
735     case TRUNC_MOD_EXPR:        /* round toward zero */
736     case EXACT_DIV_EXPR:        /* for this one, it shouldn't matter */
737       return overflow;
738
739     case FLOOR_DIV_EXPR:
740     case FLOOR_MOD_EXPR:        /* round toward negative infinity */
741       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
742         {
743           /* quo = quo - 1;  */
744           add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT)  -1,
745                       lquo, hquo);
746         }
747       else return overflow;
748       break;
749
750     case CEIL_DIV_EXPR:
751     case CEIL_MOD_EXPR:         /* round toward positive infinity */
752       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
753         {
754           add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
755                       lquo, hquo);
756         }
757       else return overflow;
758       break;
759     
760     case ROUND_DIV_EXPR:
761     case ROUND_MOD_EXPR:        /* round to closest integer */
762       {
763         HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem;
764         HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice;
765
766         /* get absolute values */
767         if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
768         if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den);
769
770         /* if (2 * abs (lrem) >= abs (lden)) */
771         mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
772                     labs_rem, habs_rem, &ltwice, &htwice);
773         if (((unsigned HOST_WIDE_INT) habs_den
774              < (unsigned HOST_WIDE_INT) htwice)
775             || (((unsigned HOST_WIDE_INT) habs_den
776                  == (unsigned HOST_WIDE_INT) htwice)
777                 && ((HOST_WIDE_INT unsigned) labs_den
778                     < (unsigned HOST_WIDE_INT) ltwice)))
779           {
780             if (*hquo < 0)
781               /* quo = quo - 1;  */
782               add_double (*lquo, *hquo,
783                           (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
784             else
785               /* quo = quo + 1; */
786               add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
787                           lquo, hquo);
788           }
789         else return overflow;
790       }
791       break;
792
793     default:
794       abort ();
795     }
796
797   /* compute true remainder:  rem = num - (quo * den)  */
798   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
799   neg_double (*lrem, *hrem, lrem, hrem);
800   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
801   return overflow;
802 }
803 \f
804 #ifndef REAL_ARITHMETIC
805 /* Effectively truncate a real value to represent
806    the nearest possible value in a narrower mode.
807    The result is actually represented in the same data type as the argument,
808    but its value is usually different.  */
809
810 REAL_VALUE_TYPE
811 real_value_truncate (mode, arg)
812      enum machine_mode mode;
813      REAL_VALUE_TYPE arg;
814 {
815 #ifdef __STDC__
816   /* Make sure the value is actually stored in memory before we turn off
817      the handler.  */
818   volatile
819 #endif
820     REAL_VALUE_TYPE value;
821   jmp_buf handler, old_handler;
822   int handled;
823
824   if (setjmp (handler))
825     {
826       error ("floating overflow");
827       return dconst0;
828     }
829   handled = push_float_handler (handler, old_handler);
830   value = REAL_VALUE_TRUNCATE (mode, arg);
831   pop_float_handler (handled, old_handler);
832   return value;
833 }
834
835 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
836
837 /* Check for infinity in an IEEE double precision number.  */
838
839 int
840 target_isinf (x)
841      REAL_VALUE_TYPE x;
842 {
843   /* The IEEE 64-bit double format.  */
844   union {
845     REAL_VALUE_TYPE d;
846     struct {
847       unsigned sign      :  1;
848       unsigned exponent  : 11;
849       unsigned mantissa1 : 20;
850       unsigned mantissa2;
851     } little_endian;
852     struct {
853       unsigned mantissa2;
854       unsigned mantissa1 : 20;
855       unsigned exponent  : 11;
856       unsigned sign      :  1;
857     } big_endian;    
858   } u;
859
860   u.d = dconstm1;
861   if (u.big_endian.sign == 1)
862     {
863       u.d = x;
864       return (u.big_endian.exponent == 2047
865               && u.big_endian.mantissa1 == 0
866               && u.big_endian.mantissa2 == 0);
867     }
868   else
869     {
870       u.d = x;
871       return (u.little_endian.exponent == 2047
872               && u.little_endian.mantissa1 == 0
873               && u.little_endian.mantissa2 == 0);
874     }
875 }
876
877 /* Check whether an IEEE double precision number is a NaN.  */
878
879 int
880 target_isnan (x)
881      REAL_VALUE_TYPE x;
882 {
883   /* The IEEE 64-bit double format.  */
884   union {
885     REAL_VALUE_TYPE d;
886     struct {
887       unsigned sign      :  1;
888       unsigned exponent  : 11;
889       unsigned mantissa1 : 20;
890       unsigned mantissa2;
891     } little_endian;
892     struct {
893       unsigned mantissa2;
894       unsigned mantissa1 : 20;
895       unsigned exponent  : 11;
896       unsigned sign      :  1;
897     } big_endian;    
898   } u;
899
900   u.d = dconstm1;
901   if (u.big_endian.sign == 1)
902     {
903       u.d = x;
904       return (u.big_endian.exponent == 2047
905               && (u.big_endian.mantissa1 != 0
906                   || u.big_endian.mantissa2 != 0));
907     }
908   else
909     {
910       u.d = x;
911       return (u.little_endian.exponent == 2047
912               && (u.little_endian.mantissa1 != 0
913                   || u.little_endian.mantissa2 != 0));
914     }
915 }
916
917 /* Check for a negative IEEE double precision number.  */
918
919 int
920 target_negative (x)
921      REAL_VALUE_TYPE x;
922 {
923   /* The IEEE 64-bit double format.  */
924   union {
925     REAL_VALUE_TYPE d;
926     struct {
927       unsigned sign      :  1;
928       unsigned exponent  : 11;
929       unsigned mantissa1 : 20;
930       unsigned mantissa2;
931     } little_endian;
932     struct {
933       unsigned mantissa2;
934       unsigned mantissa1 : 20;
935       unsigned exponent  : 11;
936       unsigned sign      :  1;
937     } big_endian;    
938   } u;
939
940   u.d = dconstm1;
941   if (u.big_endian.sign == 1)
942     {
943       u.d = x;
944       return u.big_endian.sign;
945     }
946   else
947     {
948       u.d = x;
949       return u.little_endian.sign;
950     }
951 }
952 #else /* Target not IEEE */
953
954 /* Let's assume other float formats don't have infinity.
955    (This can be overridden by redefining REAL_VALUE_ISINF.)  */
956
957 target_isinf (x)
958      REAL_VALUE_TYPE x;
959 {
960   return 0;
961 }
962
963 /* Let's assume other float formats don't have NaNs.
964    (This can be overridden by redefining REAL_VALUE_ISNAN.)  */
965
966 target_isnan (x)
967      REAL_VALUE_TYPE x;
968 {
969   return 0;
970 }
971
972 /* Let's assume other float formats don't have minus zero.
973    (This can be overridden by redefining REAL_VALUE_NEGATIVE.)  */
974
975 target_negative (x)
976      REAL_VALUE_TYPE x;
977 {
978   return x < 0;
979 }
980 #endif /* Target not IEEE */
981 #endif /* no REAL_ARITHMETIC */
982 \f
983 /* Split a tree IN into a constant and a variable part
984    that could be combined with CODE to make IN.
985    CODE must be a commutative arithmetic operation.
986    Store the constant part into *CONP and the variable in &VARP.
987    Return 1 if this was done; zero means the tree IN did not decompose
988    this way.
989
990    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
991    Therefore, we must tell the caller whether the variable part
992    was subtracted.  We do this by storing 1 or -1 into *VARSIGNP.
993    The value stored is the coefficient for the variable term.
994    The constant term we return should always be added;
995    we negate it if necessary.  */
996
997 static int
998 split_tree (in, code, varp, conp, varsignp)
999      tree in;
1000      enum tree_code code;
1001      tree *varp, *conp;
1002      int *varsignp;
1003 {
1004   register tree outtype = TREE_TYPE (in);
1005   *varp = 0;
1006   *conp = 0;
1007
1008   /* Strip any conversions that don't change the machine mode.  */
1009   while ((TREE_CODE (in) == NOP_EXPR
1010           || TREE_CODE (in) == CONVERT_EXPR)
1011          && (TYPE_MODE (TREE_TYPE (in))
1012              == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
1013     in = TREE_OPERAND (in, 0);
1014
1015   if (TREE_CODE (in) == code
1016       || (! FLOAT_TYPE_P (TREE_TYPE (in))
1017           /* We can associate addition and subtraction together
1018              (even though the C standard doesn't say so)
1019              for integers because the value is not affected.
1020              For reals, the value might be affected, so we can't.  */
1021           && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1022               || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1023     {
1024       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
1025       if (code == INTEGER_CST)
1026         {
1027           *conp = TREE_OPERAND (in, 0);
1028           *varp = TREE_OPERAND (in, 1);
1029           if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1030               && TREE_TYPE (*varp) != outtype)
1031             *varp = convert (outtype, *varp);
1032           *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1033           return 1;
1034         }
1035       if (TREE_CONSTANT (TREE_OPERAND (in, 1)))
1036         {
1037           *conp = TREE_OPERAND (in, 1);
1038           *varp = TREE_OPERAND (in, 0);
1039           *varsignp = 1;
1040           if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1041               && TREE_TYPE (*varp) != outtype)
1042             *varp = convert (outtype, *varp);
1043           if (TREE_CODE (in) == MINUS_EXPR)
1044             {
1045               /* If operation is subtraction and constant is second,
1046                  must negate it to get an additive constant.
1047                  And this cannot be done unless it is a manifest constant.
1048                  It could also be the address of a static variable.
1049                  We cannot negate that, so give up.  */
1050               if (TREE_CODE (*conp) == INTEGER_CST)
1051                 /* Subtracting from integer_zero_node loses for long long.  */
1052                 *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp));
1053               else
1054                 return 0;
1055             }
1056           return 1;
1057         }
1058       if (TREE_CONSTANT (TREE_OPERAND (in, 0)))
1059         {
1060           *conp = TREE_OPERAND (in, 0);
1061           *varp = TREE_OPERAND (in, 1);
1062           if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype)
1063               && TREE_TYPE (*varp) != outtype)
1064             *varp = convert (outtype, *varp);
1065           *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
1066           return 1;
1067         }
1068     }
1069   return 0;
1070 }
1071 \f
1072 /* Combine two constants NUM and ARG2 under operation CODE
1073    to produce a new constant.
1074    We assume ARG1 and ARG2 have the same data type,
1075    or at least are the same kind of constant and the same machine mode.
1076
1077    If NOTRUNC is nonzero, do not truncate the result to fit the data type.  */
1078
1079 static tree
1080 const_binop (code, arg1, arg2, notrunc)
1081      enum tree_code code;
1082      register tree arg1, arg2;
1083      int notrunc;
1084 {
1085   if (TREE_CODE (arg1) == INTEGER_CST)
1086     {
1087       register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1);
1088       register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1);
1089       HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2);
1090       HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2);
1091       HOST_WIDE_INT low, hi;
1092       HOST_WIDE_INT garbagel, garbageh;
1093       register tree t;
1094       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
1095       int overflow = 0;
1096
1097       switch (code)
1098         {
1099         case BIT_IOR_EXPR:
1100           t = build_int_2 (int1l | int2l, int1h | int2h);
1101           break;
1102
1103         case BIT_XOR_EXPR:
1104           t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
1105           break;
1106
1107         case BIT_AND_EXPR:
1108           t = build_int_2 (int1l & int2l, int1h & int2h);
1109           break;
1110
1111         case BIT_ANDTC_EXPR:
1112           t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
1113           break;
1114
1115         case RSHIFT_EXPR:
1116           int2l = - int2l;
1117         case LSHIFT_EXPR:
1118           /* It's unclear from the C standard whether shifts can overflow.
1119              The following code ignores overflow; perhaps a C standard
1120              interpretation ruling is needed.  */
1121           lshift_double (int1l, int1h, int2l,
1122                          TYPE_PRECISION (TREE_TYPE (arg1)),
1123                          &low, &hi,
1124                          !uns);
1125           t = build_int_2 (low, hi);
1126           TREE_TYPE (t) = TREE_TYPE (arg1);
1127           if (!notrunc)
1128             force_fit_type (t, 0);
1129           TREE_CONSTANT_OVERFLOW (t)
1130             = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2);
1131           return t;
1132
1133         case RROTATE_EXPR:
1134           int2l = - int2l;
1135         case LROTATE_EXPR:
1136           lrotate_double (int1l, int1h, int2l,
1137                           TYPE_PRECISION (TREE_TYPE (arg1)),
1138                           &low, &hi);
1139           t = build_int_2 (low, hi);
1140           break;
1141
1142         case PLUS_EXPR:
1143           if (int1h == 0)
1144             {
1145               int2l += int1l;
1146               if ((unsigned HOST_WIDE_INT) int2l < int1l)
1147                 {
1148                   hi = int2h++;
1149                   overflow = int2h < hi;
1150                 }
1151               t = build_int_2 (int2l, int2h);
1152               break;
1153             }
1154           if (int2h == 0)
1155             {
1156               int1l += int2l;
1157               if ((unsigned HOST_WIDE_INT) int1l < int2l)
1158                 {
1159                   hi = int1h++;
1160                   overflow = int1h < hi;
1161                 }
1162               t = build_int_2 (int1l, int1h);
1163               break;
1164             }
1165           overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1166           t = build_int_2 (low, hi);
1167           break;
1168
1169         case MINUS_EXPR:
1170           if (int2h == 0 && int2l == 0)
1171             {
1172               t = build_int_2 (int1l, int1h);
1173               break;
1174             }
1175           neg_double (int2l, int2h, &low, &hi);
1176           add_double (int1l, int1h, low, hi, &low, &hi);
1177           overflow = overflow_sum_sign (hi, int2h, int1h);
1178           t = build_int_2 (low, hi);
1179           break;
1180
1181         case MULT_EXPR:
1182           /* Optimize simple cases.  */
1183           if (int1h == 0)
1184             {
1185               unsigned HOST_WIDE_INT temp;
1186
1187               switch (int1l)
1188                 {
1189                 case 0:
1190                   t = build_int_2 (0, 0);
1191                   goto got_it;
1192                 case 1:
1193                   t = build_int_2 (int2l, int2h);
1194                   goto got_it;
1195                 case 2:
1196                   overflow = left_shift_overflows (int2h, 1);
1197                   temp = int2l + int2l;
1198                   int2h = (int2h << 1) + (temp < int2l);
1199                   t = build_int_2 (temp, int2h);
1200                   goto got_it;
1201 #if 0 /* This code can lose carries.  */
1202                 case 3:
1203                   temp = int2l + int2l + int2l;
1204                   int2h = int2h * 3 + (temp < int2l);
1205                   t = build_int_2 (temp, int2h);
1206                   goto got_it;
1207 #endif
1208                 case 4:
1209                   overflow = left_shift_overflows (int2h, 2);
1210                   temp = int2l + int2l;
1211                   int2h = (int2h << 2) + ((temp < int2l) << 1);
1212                   int2l = temp;
1213                   temp += temp;
1214                   int2h += (temp < int2l);
1215                   t = build_int_2 (temp, int2h);
1216                   goto got_it;
1217                 case 8:
1218                   overflow = left_shift_overflows (int2h, 3);
1219                   temp = int2l + int2l;
1220                   int2h = (int2h << 3) + ((temp < int2l) << 2);
1221                   int2l = temp;
1222                   temp += temp;
1223                   int2h += (temp < int2l) << 1;
1224                   int2l = temp;
1225                   temp += temp;
1226                   int2h += (temp < int2l);
1227                   t = build_int_2 (temp, int2h);
1228                   goto got_it;
1229                 default:
1230                   break;
1231                 }
1232             }
1233
1234           if (int2h == 0)
1235             {
1236               if (int2l == 0)
1237                 {
1238                   t = build_int_2 (0, 0);
1239                   break;
1240                 }
1241               if (int2l == 1)
1242                 {
1243                   t = build_int_2 (int1l, int1h);
1244                   break;
1245                 }
1246             }
1247
1248           overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1249           t = build_int_2 (low, hi);
1250           break;
1251
1252         case TRUNC_DIV_EXPR:
1253         case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1254         case EXACT_DIV_EXPR:
1255           /* This is a shortcut for a common special case.
1256              It reduces the number of tree nodes generated
1257              and saves time.  */
1258           if (int2h == 0 && int2l > 0
1259               && TREE_TYPE (arg1) == sizetype
1260               && int1h == 0 && int1l >= 0)
1261             {
1262               if (code == CEIL_DIV_EXPR)
1263                 int1l += int2l-1;
1264               return size_int (int1l / int2l);
1265             }
1266         case ROUND_DIV_EXPR: 
1267           if (int2h == 0 && int2l == 1)
1268             {
1269               t = build_int_2 (int1l, int1h);
1270               break;
1271             }
1272           if (int1l == int2l && int1h == int2h)
1273             {
1274               if ((int1l | int1h) == 0)
1275                 abort ();
1276               t = build_int_2 (1, 0);
1277               break;
1278             }
1279           overflow = div_and_round_double (code, uns,
1280                                            int1l, int1h, int2l, int2h,
1281                                            &low, &hi, &garbagel, &garbageh);
1282           t = build_int_2 (low, hi);
1283           break;
1284
1285         case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
1286         case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1287           overflow = div_and_round_double (code, uns,
1288                                            int1l, int1h, int2l, int2h,
1289                                            &garbagel, &garbageh, &low, &hi);
1290           t = build_int_2 (low, hi);
1291           break;
1292
1293         case MIN_EXPR:
1294         case MAX_EXPR:
1295           if (uns)
1296             {
1297               low = (((unsigned HOST_WIDE_INT) int1h
1298                       < (unsigned HOST_WIDE_INT) int2h)
1299                      || (((unsigned HOST_WIDE_INT) int1h
1300                           == (unsigned HOST_WIDE_INT) int2h)
1301                          && ((unsigned HOST_WIDE_INT) int1l
1302                              < (unsigned HOST_WIDE_INT) int2l)));
1303             }
1304           else
1305             {
1306               low = ((int1h < int2h)
1307                      || ((int1h == int2h)
1308                          && ((unsigned HOST_WIDE_INT) int1l
1309                              < (unsigned HOST_WIDE_INT) int2l)));
1310             }
1311           if (low == (code == MIN_EXPR))
1312             t = build_int_2 (int1l, int1h);
1313           else
1314             t = build_int_2 (int2l, int2h);
1315           break;
1316
1317         default:
1318           abort ();
1319         }
1320     got_it:
1321       TREE_TYPE (t) = TREE_TYPE (arg1);
1322       TREE_CONSTANT_OVERFLOW (t)
1323         = ((notrunc ? !uns && overflow : force_fit_type (t, overflow))
1324            | TREE_CONSTANT_OVERFLOW (arg1)
1325            | TREE_CONSTANT_OVERFLOW (arg2));
1326       return t;
1327     }
1328 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1329   if (TREE_CODE (arg1) == REAL_CST)
1330     {
1331       REAL_VALUE_TYPE d1;
1332       REAL_VALUE_TYPE d2;
1333       REAL_VALUE_TYPE value;
1334       tree t;
1335
1336       d1 = TREE_REAL_CST (arg1);
1337       d2 = TREE_REAL_CST (arg2);
1338       if (setjmp (float_error))
1339         {
1340           pedwarn ("floating overflow in constant expression");
1341           return build (code, TREE_TYPE (arg1), arg1, arg2);
1342         }
1343       set_float_handler (float_error);
1344
1345 #ifdef REAL_ARITHMETIC
1346       REAL_ARITHMETIC (value, code, d1, d2);
1347 #else
1348       switch (code)
1349         {
1350         case PLUS_EXPR:
1351           value = d1 + d2;
1352           break;
1353
1354         case MINUS_EXPR:
1355           value = d1 - d2;
1356           break;
1357
1358         case MULT_EXPR:
1359           value = d1 * d2;
1360           break;
1361
1362         case RDIV_EXPR:
1363 #ifndef REAL_INFINITY
1364           if (d2 == 0)
1365             abort ();
1366 #endif
1367
1368           value = d1 / d2;
1369           break;
1370
1371         case MIN_EXPR:
1372           value = MIN (d1, d2);
1373           break;
1374
1375         case MAX_EXPR:
1376           value = MAX (d1, d2);
1377           break;
1378
1379         default:
1380           abort ();
1381         }
1382 #endif /* no REAL_ARITHMETIC */
1383       t = build_real (TREE_TYPE (arg1),
1384                       real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value));
1385       set_float_handler (NULL_PTR);
1386       return t;
1387     }
1388 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1389   if (TREE_CODE (arg1) == COMPLEX_CST)
1390     {
1391       register tree r1 = TREE_REALPART (arg1);
1392       register tree i1 = TREE_IMAGPART (arg1);
1393       register tree r2 = TREE_REALPART (arg2);
1394       register tree i2 = TREE_IMAGPART (arg2);
1395       register tree t;
1396
1397       switch (code)
1398         {
1399         case PLUS_EXPR:
1400           t = build_complex (const_binop (PLUS_EXPR, r1, r2, notrunc),
1401                              const_binop (PLUS_EXPR, i1, i2, notrunc));
1402           break;
1403
1404         case MINUS_EXPR:
1405           t = build_complex (const_binop (MINUS_EXPR, r1, r2, notrunc),
1406                              const_binop (MINUS_EXPR, i1, i2, notrunc));
1407           break;
1408
1409         case MULT_EXPR:
1410           t = build_complex (const_binop (MINUS_EXPR,
1411                                           const_binop (MULT_EXPR,
1412                                                        r1, r2, notrunc),
1413                                           const_binop (MULT_EXPR,
1414                                                        i1, i2, notrunc),
1415                                           notrunc),
1416                              const_binop (PLUS_EXPR,
1417                                           const_binop (MULT_EXPR,
1418                                                        r1, i2, notrunc),
1419                                           const_binop (MULT_EXPR,
1420                                                        i1, r2, notrunc),
1421                                           notrunc));
1422           break;
1423
1424         case RDIV_EXPR:
1425           {
1426             register tree magsquared
1427               = const_binop (PLUS_EXPR,
1428                              const_binop (MULT_EXPR, r2, r2, notrunc),
1429                              const_binop (MULT_EXPR, i2, i2, notrunc),
1430                              notrunc);
1431             t = build_complex (const_binop (RDIV_EXPR,
1432                                             const_binop (PLUS_EXPR,
1433                                                          const_binop (MULT_EXPR, r1, r2, notrunc),
1434                                                          const_binop (MULT_EXPR, i1, i2, notrunc),
1435                                                          notrunc),
1436                                             magsquared, notrunc),
1437                                const_binop (RDIV_EXPR,
1438                                             const_binop (MINUS_EXPR,
1439                                                          const_binop (MULT_EXPR, i1, r2, notrunc),
1440                                                          const_binop (MULT_EXPR, r1, i2, notrunc),
1441                                                          notrunc),
1442                                             magsquared, notrunc));
1443           }
1444           break;
1445
1446         default:
1447           abort ();
1448         }
1449       TREE_TYPE (t) = TREE_TYPE (arg1);
1450       return t;
1451     }
1452   return 0;
1453 }
1454 \f
1455 /* Return an INTEGER_CST with value V and type from `sizetype'.  */
1456
1457 tree
1458 size_int (number)
1459      unsigned int number;
1460 {
1461   register tree t;
1462   /* Type-size nodes already made for small sizes.  */
1463   static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1];
1464
1465   if (number < 2*HOST_BITS_PER_WIDE_INT + 1
1466       && size_table[number] != 0)
1467     return size_table[number];
1468   if (number < 2*HOST_BITS_PER_WIDE_INT + 1)
1469     {
1470       push_obstacks_nochange ();
1471       /* Make this a permanent node.  */
1472       end_temporary_allocation ();
1473       t = build_int_2 (number, 0);
1474       TREE_TYPE (t) = sizetype;
1475       size_table[number] = t;
1476       pop_obstacks ();
1477     }
1478   else
1479     {
1480       t = build_int_2 (number, 0);
1481       TREE_TYPE (t) = sizetype;
1482     }
1483   return t;
1484 }
1485
1486 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1487    CODE is a tree code.  Data type is taken from `sizetype',
1488    If the operands are constant, so is the result.  */
1489
1490 tree
1491 size_binop (code, arg0, arg1)
1492      enum tree_code code;
1493      tree arg0, arg1;
1494 {
1495   /* Handle the special case of two integer constants faster.  */
1496   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1497     {
1498       /* And some specific cases even faster than that.  */
1499       if (code == PLUS_EXPR
1500           && TREE_INT_CST_LOW (arg0) == 0
1501           && TREE_INT_CST_HIGH (arg0) == 0)
1502         return arg1;
1503       if (code == MINUS_EXPR
1504           && TREE_INT_CST_LOW (arg1) == 0
1505           && TREE_INT_CST_HIGH (arg1) == 0)
1506         return arg0;
1507       if (code == MULT_EXPR
1508           && TREE_INT_CST_LOW (arg0) == 1
1509           && TREE_INT_CST_HIGH (arg0) == 0)
1510         return arg1;
1511       /* Handle general case of two integer constants.  */
1512       return const_binop (code, arg0, arg1, 1);
1513     }
1514
1515   if (arg0 == error_mark_node || arg1 == error_mark_node)
1516     return error_mark_node;
1517
1518   return fold (build (code, sizetype, arg0, arg1));
1519 }
1520 \f
1521 /* Given T, a tree representing type conversion of ARG1, a constant,
1522    return a constant tree representing the result of conversion.  */
1523
1524 static tree
1525 fold_convert (t, arg1)
1526      register tree t;
1527      register tree arg1;
1528 {
1529   register tree type = TREE_TYPE (t);
1530
1531   if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type))
1532     {
1533       if (TREE_CODE (arg1) == INTEGER_CST)
1534         {
1535           /* Given an integer constant, make new constant with new type,
1536              appropriately sign-extended or truncated.  */
1537           t = build_int_2 (TREE_INT_CST_LOW (arg1),
1538                            TREE_INT_CST_HIGH (arg1));
1539           TREE_TYPE (t) = type;
1540           /* Indicate an overflow if (1) ARG1 already overflowed,
1541              or (2) ARG1 is a too-large unsigned value and T is signed,
1542              or (3) force_fit_type indicates an overflow.
1543              force_fit_type can't detect (2), since it sees only T's type.  */
1544           TREE_CONSTANT_OVERFLOW (t) =
1545             (TREE_CONSTANT_OVERFLOW (arg1)
1546              | (TREE_INT_CST_HIGH (arg1) < 0
1547                 & TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (arg1)))
1548              | force_fit_type (t, 0));
1549         }
1550 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1551       else if (TREE_CODE (arg1) == REAL_CST)
1552         {
1553           REAL_VALUE_TYPE l, x, u;
1554
1555           l = real_value_from_int_cst (TYPE_MIN_VALUE (type));
1556           x = TREE_REAL_CST (arg1);
1557           u = real_value_from_int_cst (TYPE_MAX_VALUE (type));
1558
1559           /* See if X will be in range after truncation towards 0.
1560              To compensate for truncation, move the bounds away from 0,
1561              but reject if X exactly equals the adjusted bounds.  */
1562 #ifdef REAL_ARITHMETIC
1563           REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
1564           REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
1565 #else
1566           l--;
1567           u++;
1568 #endif
1569           if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u)))
1570             {
1571               pedwarn ("real constant out of range for integer conversion");
1572               return t;
1573             }
1574 #ifndef REAL_ARITHMETIC
1575           {
1576             REAL_VALUE_TYPE d;
1577             HOST_WIDE_INT low, high;
1578             HOST_WIDE_INT half_word
1579               = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
1580
1581             d = TREE_REAL_CST (arg1);
1582             if (d < 0)
1583               d = -d;
1584
1585             high = (HOST_WIDE_INT) (d / half_word / half_word);
1586             d -= (REAL_VALUE_TYPE) high * half_word * half_word;
1587             if (d >= (REAL_VALUE_TYPE) half_word * half_word / 2)
1588               {
1589                 low = d - (REAL_VALUE_TYPE) half_word * half_word / 2;
1590                 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
1591               }
1592             else
1593               low = (HOST_WIDE_INT) d;
1594             if (TREE_REAL_CST (arg1) < 0)
1595               neg_double (low, high, &low, &high);
1596             t = build_int_2 (low, high);
1597           }
1598 #else
1599           {
1600             HOST_WIDE_INT low, high;
1601             REAL_VALUE_TO_INT (&low, &high, (TREE_REAL_CST (arg1)));
1602             t = build_int_2 (low, high);
1603           }
1604 #endif
1605           TREE_TYPE (t) = type;
1606           force_fit_type (t, 0);
1607         }
1608 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1609       TREE_TYPE (t) = type;
1610     }
1611   else if (TREE_CODE (type) == REAL_TYPE)
1612     {
1613 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1614       if (TREE_CODE (arg1) == INTEGER_CST)
1615         return build_real_from_int_cst (type, arg1);
1616 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1617       if (TREE_CODE (arg1) == REAL_CST)
1618         {
1619           if (setjmp (float_error))
1620             {
1621               pedwarn ("floating overflow in constant expression");
1622               return t;
1623             }
1624           set_float_handler (float_error);
1625
1626           t = build_real (type, real_value_truncate (TYPE_MODE (type),
1627                                                      TREE_REAL_CST (arg1)));
1628           set_float_handler (NULL_PTR);
1629           return t;
1630         }
1631     }
1632   TREE_CONSTANT (t) = 1;
1633   return t;
1634 }
1635 \f
1636 /* Return an expr equal to X but certainly not valid as an lvalue.
1637    Also make sure it is not valid as an null pointer constant.  */
1638
1639 tree
1640 non_lvalue (x)
1641      tree x;
1642 {
1643   tree result;
1644
1645   /* These things are certainly not lvalues.  */
1646   if (TREE_CODE (x) == NON_LVALUE_EXPR
1647       || TREE_CODE (x) == INTEGER_CST
1648       || TREE_CODE (x) == REAL_CST
1649       || TREE_CODE (x) == STRING_CST
1650       || TREE_CODE (x) == ADDR_EXPR)
1651     {
1652       if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x))
1653         {
1654           /* Use NOP_EXPR instead of NON_LVALUE_EXPR
1655              so convert_for_assignment won't strip it.
1656              This is so this 0 won't be treated as a null pointer constant.  */
1657           result = build1 (NOP_EXPR, TREE_TYPE (x), x);
1658           TREE_CONSTANT (result) = TREE_CONSTANT (x);
1659           return result;
1660         }
1661       return x;
1662     }
1663
1664   result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1665   TREE_CONSTANT (result) = TREE_CONSTANT (x);
1666   return result;
1667 }
1668 \f
1669 /* Given a tree comparison code, return the code that is the logical inverse
1670    of the given code.  It is not safe to do this for floating-point
1671    comparisons, except for NE_EXPR and EQ_EXPR.  */
1672
1673 static enum tree_code
1674 invert_tree_comparison (code)
1675      enum tree_code code;
1676 {
1677   switch (code)
1678     {
1679     case EQ_EXPR:
1680       return NE_EXPR;
1681     case NE_EXPR:
1682       return EQ_EXPR;
1683     case GT_EXPR:
1684       return LE_EXPR;
1685     case GE_EXPR:
1686       return LT_EXPR;
1687     case LT_EXPR:
1688       return GE_EXPR;
1689     case LE_EXPR:
1690       return GT_EXPR;
1691     default:
1692       abort ();
1693     }
1694 }
1695
1696 /* Similar, but return the comparison that results if the operands are
1697    swapped.  This is safe for floating-point.  */
1698
1699 static enum tree_code
1700 swap_tree_comparison (code)
1701      enum tree_code code;
1702 {
1703   switch (code)
1704     {
1705     case EQ_EXPR:
1706     case NE_EXPR:
1707       return code;
1708     case GT_EXPR:
1709       return LT_EXPR;
1710     case GE_EXPR:
1711       return LE_EXPR;
1712     case LT_EXPR:
1713       return GT_EXPR;
1714     case LE_EXPR:
1715       return GE_EXPR;
1716     default:
1717       abort ();
1718     }
1719 }
1720 \f
1721 /* Return nonzero if two operands are necessarily equal.
1722    If ONLY_CONST is non-zero, only return non-zero for constants.
1723    This function tests whether the operands are indistinguishable;
1724    it does not test whether they are equal using C's == operation.
1725    The distinction is important for IEEE floating point, because
1726    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1727    (2) two NaNs may be indistinguishable, but NaN!=NaN.  */
1728
1729 int
1730 operand_equal_p (arg0, arg1, only_const)
1731      tree arg0, arg1;
1732      int only_const;
1733 {
1734   /* If both types don't have the same signedness, then we can't consider
1735      them equal.  We must check this before the STRIP_NOPS calls
1736      because they may change the signedness of the arguments.  */
1737   if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
1738     return 0;
1739
1740   STRIP_NOPS (arg0);
1741   STRIP_NOPS (arg1);
1742
1743   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1744      We don't care about side effects in that case because the SAVE_EXPR
1745      takes care of that for us.  */
1746   if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1)
1747     return ! only_const;
1748
1749   if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))
1750     return 0;
1751
1752   if (TREE_CODE (arg0) == TREE_CODE (arg1)
1753       && TREE_CODE (arg0) == ADDR_EXPR
1754       && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
1755     return 1;
1756
1757   if (TREE_CODE (arg0) == TREE_CODE (arg1)
1758       && TREE_CODE (arg0) == INTEGER_CST
1759       && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
1760       && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1))
1761     return 1;
1762
1763   /* Detect when real constants are equal.  */
1764   if (TREE_CODE (arg0) == TREE_CODE (arg1)
1765       && TREE_CODE (arg0) == REAL_CST)
1766     return !bcmp (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1),
1767                   sizeof (REAL_VALUE_TYPE));
1768
1769   if (only_const)
1770     return 0;
1771
1772   if (arg0 == arg1)
1773     return 1;
1774
1775   if (TREE_CODE (arg0) != TREE_CODE (arg1))
1776     return 0;
1777   /* This is needed for conversions and for COMPONENT_REF.
1778      Might as well play it safe and always test this.  */
1779   if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
1780     return 0;
1781
1782   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
1783     {
1784     case '1':
1785       /* Two conversions are equal only if signedness and modes match.  */
1786       if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
1787           && (TREE_UNSIGNED (TREE_TYPE (arg0))
1788               != TREE_UNSIGNED (TREE_TYPE (arg1))))
1789         return 0;
1790
1791       return operand_equal_p (TREE_OPERAND (arg0, 0),
1792                               TREE_OPERAND (arg1, 0), 0);
1793
1794     case '<':
1795     case '2':
1796       return (operand_equal_p (TREE_OPERAND (arg0, 0),
1797                                TREE_OPERAND (arg1, 0), 0)
1798               && operand_equal_p (TREE_OPERAND (arg0, 1),
1799                                   TREE_OPERAND (arg1, 1), 0));
1800
1801     case 'r':
1802       switch (TREE_CODE (arg0))
1803         {
1804         case INDIRECT_REF:
1805           return operand_equal_p (TREE_OPERAND (arg0, 0),
1806                                   TREE_OPERAND (arg1, 0), 0);
1807
1808         case COMPONENT_REF:
1809         case ARRAY_REF:
1810           return (operand_equal_p (TREE_OPERAND (arg0, 0),
1811                                    TREE_OPERAND (arg1, 0), 0)
1812                   && operand_equal_p (TREE_OPERAND (arg0, 1),
1813                                       TREE_OPERAND (arg1, 1), 0));
1814
1815         case BIT_FIELD_REF:
1816           return (operand_equal_p (TREE_OPERAND (arg0, 0),
1817                                    TREE_OPERAND (arg1, 0), 0)
1818                   && operand_equal_p (TREE_OPERAND (arg0, 1),
1819                                       TREE_OPERAND (arg1, 1), 0)
1820                   && operand_equal_p (TREE_OPERAND (arg0, 2),
1821                                       TREE_OPERAND (arg1, 2), 0));
1822         }
1823       break;
1824     }
1825
1826   return 0;
1827 }
1828 \f
1829 /* Similar to operand_equal_p, but see if ARG0 might have been made by
1830    shorten_compare from ARG1 when ARG1 was being compared with OTHER. 
1831
1832    When in doubt, return 0.  */
1833
1834 static int 
1835 operand_equal_for_comparison_p (arg0, arg1, other)
1836      tree arg0, arg1;
1837      tree other;
1838 {
1839   int unsignedp1, unsignedpo;
1840   tree primarg1, primother;
1841   int correct_width;
1842
1843   if (operand_equal_p (arg0, arg1, 0))
1844     return 1;
1845
1846   if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
1847     return 0;
1848
1849   /* Duplicate what shorten_compare does to ARG1 and see if that gives the
1850      actual comparison operand, ARG0.
1851
1852      First throw away any conversions to wider types
1853      already present in the operands.  */
1854
1855   primarg1 = get_narrower (arg1, &unsignedp1);
1856   primother = get_narrower (other, &unsignedpo);
1857
1858   correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
1859   if (unsignedp1 == unsignedpo
1860       && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
1861       && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
1862     {
1863       tree type = TREE_TYPE (arg0);
1864
1865       /* Make sure shorter operand is extended the right way
1866          to match the longer operand.  */
1867       primarg1 = convert (signed_or_unsigned_type (unsignedp1,
1868                                                   TREE_TYPE (primarg1)),
1869                          primarg1);
1870
1871       if (operand_equal_p (arg0, convert (type, primarg1), 0))
1872         return 1;
1873     }
1874
1875   return 0;
1876 }
1877 \f
1878 /* See if ARG is an expression that is either a comparison or is performing
1879    arithmetic on comparisons.  The comparisons must only be comparing
1880    two different values, which will be stored in *CVAL1 and *CVAL2; if
1881    they are non-zero it means that some operands have already been found.
1882    No variables may be used anywhere else in the expression except in the
1883    comparisons.
1884
1885    If this is true, return 1.  Otherwise, return zero.  */
1886
1887 static int
1888 twoval_comparison_p (arg, cval1, cval2)
1889      tree arg;
1890      tree *cval1, *cval2;
1891 {
1892   enum tree_code code = TREE_CODE (arg);
1893   char class = TREE_CODE_CLASS (code);
1894
1895   /* We can handle some of the 'e' cases here.  */
1896   if (class == 'e'
1897       && (code == TRUTH_NOT_EXPR
1898           || (code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0)))
1899     class = '1';
1900   else if (class == 'e'
1901            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
1902                || code == COMPOUND_EXPR))
1903     class = '2';
1904
1905   switch (class)
1906     {
1907     case '1':
1908       return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
1909
1910     case '2':
1911       return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
1912               && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
1913
1914     case 'c':
1915       return 1;
1916
1917     case 'e':
1918       if (code == COND_EXPR)
1919         return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
1920                 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
1921                 && twoval_comparison_p (TREE_OPERAND (arg, 2),
1922                                         cval1, cval2));
1923       return 0;
1924           
1925     case '<':
1926       /* First see if we can handle the first operand, then the second.  For
1927          the second operand, we know *CVAL1 can't be zero.  It must be that
1928          one side of the comparison is each of the values; test for the
1929          case where this isn't true by failing if the two operands
1930          are the same.  */
1931
1932       if (operand_equal_p (TREE_OPERAND (arg, 0),
1933                            TREE_OPERAND (arg, 1), 0))
1934         return 0;
1935
1936       if (*cval1 == 0)
1937         *cval1 = TREE_OPERAND (arg, 0);
1938       else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
1939         ;
1940       else if (*cval2 == 0)
1941         *cval2 = TREE_OPERAND (arg, 0);
1942       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
1943         ;
1944       else
1945         return 0;
1946
1947       if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
1948         ;
1949       else if (*cval2 == 0)
1950         *cval2 = TREE_OPERAND (arg, 1);
1951       else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
1952         ;
1953       else
1954         return 0;
1955
1956       return 1;
1957     }
1958
1959   return 0;
1960 }
1961 \f
1962 /* ARG is a tree that is known to contain just arithmetic operations and
1963    comparisons.  Evaluate the operations in the tree substituting NEW0 for
1964    any occurrence of OLD0 as an operand of a comparison and likewise for
1965    NEW1 and OLD1.  */
1966
1967 static tree
1968 eval_subst (arg, old0, new0, old1, new1)
1969      tree arg;
1970      tree old0, new0, old1, new1;
1971 {
1972   tree type = TREE_TYPE (arg);
1973   enum tree_code code = TREE_CODE (arg);
1974   char class = TREE_CODE_CLASS (code);
1975
1976   /* We can handle some of the 'e' cases here.  */
1977   if (class == 'e' && code == TRUTH_NOT_EXPR)
1978     class = '1';
1979   else if (class == 'e'
1980            && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
1981     class = '2';
1982
1983   switch (class)
1984     {
1985     case '1':
1986       return fold (build1 (code, type,
1987                            eval_subst (TREE_OPERAND (arg, 0),
1988                                        old0, new0, old1, new1)));
1989
1990     case '2':
1991       return fold (build (code, type,
1992                           eval_subst (TREE_OPERAND (arg, 0),
1993                                       old0, new0, old1, new1),
1994                           eval_subst (TREE_OPERAND (arg, 1),
1995                                       old0, new0, old1, new1)));
1996
1997     case 'e':
1998       switch (code)
1999         {
2000         case SAVE_EXPR:
2001           return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2002
2003         case COMPOUND_EXPR:
2004           return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2005
2006         case COND_EXPR:
2007           return fold (build (code, type,
2008                               eval_subst (TREE_OPERAND (arg, 0),
2009                                           old0, new0, old1, new1),
2010                               eval_subst (TREE_OPERAND (arg, 1),
2011                                           old0, new0, old1, new1),
2012                               eval_subst (TREE_OPERAND (arg, 2),
2013                                           old0, new0, old1, new1)));
2014         }
2015
2016     case '<':
2017       {
2018         tree arg0 = TREE_OPERAND (arg, 0);
2019         tree arg1 = TREE_OPERAND (arg, 1);
2020
2021         /* We need to check both for exact equality and tree equality.  The
2022            former will be true if the operand has a side-effect.  In that
2023            case, we know the operand occurred exactly once.  */
2024
2025         if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2026           arg0 = new0;
2027         else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2028           arg0 = new1;
2029
2030         if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2031           arg1 = new0;
2032         else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2033           arg1 = new1;
2034
2035         return fold (build (code, type, arg0, arg1));
2036       }
2037     }
2038
2039   return arg;
2040 }
2041 \f
2042 /* Return a tree for the case when the result of an expression is RESULT
2043    converted to TYPE and OMITTED was previously an operand of the expression
2044    but is now not needed (e.g., we folded OMITTED * 0).
2045
2046    If OMITTED has side effects, we must evaluate it.  Otherwise, just do
2047    the conversion of RESULT to TYPE.  */
2048
2049 static tree
2050 omit_one_operand (type, result, omitted)
2051      tree type, result, omitted;
2052 {
2053   tree t = convert (type, result);
2054
2055   if (TREE_SIDE_EFFECTS (omitted))
2056     return build (COMPOUND_EXPR, type, omitted, t);
2057
2058   return non_lvalue (t);
2059 }
2060 \f
2061 /* Return a simplified tree node for the truth-negation of ARG.  This
2062    never alters ARG itself.  We assume that ARG is an operation that
2063    returns a truth value (0 or 1).  */
2064
2065 tree
2066 invert_truthvalue (arg)
2067      tree arg;
2068 {
2069   tree type = TREE_TYPE (arg);
2070   enum tree_code code = TREE_CODE (arg);
2071
2072   /* If this is a comparison, we can simply invert it, except for
2073      floating-point non-equality comparisons, in which case we just
2074      enclose a TRUTH_NOT_EXPR around what we have.  */
2075
2076   if (TREE_CODE_CLASS (code) == '<')
2077     {
2078       if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2079           && code != NE_EXPR && code != EQ_EXPR)
2080         return build1 (TRUTH_NOT_EXPR, type, arg);
2081       else
2082         return build (invert_tree_comparison (code), type,
2083                       TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2084     }
2085
2086   switch (code)
2087     {
2088     case INTEGER_CST:
2089       return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0
2090                                          && TREE_INT_CST_HIGH (arg) == 0, 0));
2091
2092     case TRUTH_AND_EXPR:
2093       return build (TRUTH_OR_EXPR, type,
2094                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2095                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2096
2097     case TRUTH_OR_EXPR:
2098       return build (TRUTH_AND_EXPR, type,
2099                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2100                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2101
2102     case TRUTH_XOR_EXPR:
2103       /* Here we can invert either operand.  We invert the first operand
2104          unless the second operand is a TRUTH_NOT_EXPR in which case our
2105          result is the XOR of the first operand with the inside of the
2106          negation of the second operand.  */
2107
2108       if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2109         return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2110                       TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2111       else
2112         return build (TRUTH_XOR_EXPR, type,
2113                       invert_truthvalue (TREE_OPERAND (arg, 0)),
2114                       TREE_OPERAND (arg, 1));
2115
2116     case TRUTH_ANDIF_EXPR:
2117       return build (TRUTH_ORIF_EXPR, type,
2118                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2119                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2120
2121     case TRUTH_ORIF_EXPR:
2122       return build (TRUTH_ANDIF_EXPR, type,
2123                     invert_truthvalue (TREE_OPERAND (arg, 0)),
2124                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2125
2126     case TRUTH_NOT_EXPR:
2127       return TREE_OPERAND (arg, 0);
2128
2129     case COND_EXPR:
2130       return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2131                     invert_truthvalue (TREE_OPERAND (arg, 1)),
2132                     invert_truthvalue (TREE_OPERAND (arg, 2)));
2133
2134     case COMPOUND_EXPR:
2135       return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2136                     invert_truthvalue (TREE_OPERAND (arg, 1)));
2137
2138     case NON_LVALUE_EXPR:
2139       return invert_truthvalue (TREE_OPERAND (arg, 0));
2140
2141     case NOP_EXPR:
2142     case CONVERT_EXPR:
2143     case FLOAT_EXPR:
2144       return build1 (TREE_CODE (arg), type,
2145                      invert_truthvalue (TREE_OPERAND (arg, 0)));
2146
2147     case BIT_AND_EXPR:
2148       if (! integer_onep (TREE_OPERAND (arg, 1)))
2149         abort ();
2150       return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2151     }
2152
2153   abort ();
2154 }
2155
2156 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2157    operands are another bit-wise operation with a common input.  If so,
2158    distribute the bit operations to save an operation and possibly two if
2159    constants are involved.  For example, convert
2160         (A | B) & (A | C) into A | (B & C)
2161    Further simplification will occur if B and C are constants.
2162
2163    If this optimization cannot be done, 0 will be returned.  */
2164
2165 static tree
2166 distribute_bit_expr (code, type, arg0, arg1)
2167      enum tree_code code;
2168      tree type;
2169      tree arg0, arg1;
2170 {
2171   tree common;
2172   tree left, right;
2173
2174   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2175       || TREE_CODE (arg0) == code
2176       || (TREE_CODE (arg0) != BIT_AND_EXPR
2177           && TREE_CODE (arg0) != BIT_IOR_EXPR))
2178     return 0;
2179
2180   if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2181     {
2182       common = TREE_OPERAND (arg0, 0);
2183       left = TREE_OPERAND (arg0, 1);
2184       right = TREE_OPERAND (arg1, 1);
2185     }
2186   else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2187     {
2188       common = TREE_OPERAND (arg0, 0);
2189       left = TREE_OPERAND (arg0, 1);
2190       right = TREE_OPERAND (arg1, 0);
2191     }
2192   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2193     {
2194       common = TREE_OPERAND (arg0, 1);
2195       left = TREE_OPERAND (arg0, 0);
2196       right = TREE_OPERAND (arg1, 1);
2197     }
2198   else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2199     {
2200       common = TREE_OPERAND (arg0, 1);
2201       left = TREE_OPERAND (arg0, 0);
2202       right = TREE_OPERAND (arg1, 0);
2203     }
2204   else
2205     return 0;
2206
2207   return fold (build (TREE_CODE (arg0), type, common,
2208                       fold (build (code, type, left, right))));
2209 }
2210 \f
2211 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2212    starting at BITPOS.  The field is unsigned if UNSIGNEDP is non-zero.  */
2213
2214 static tree
2215 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
2216      tree inner;
2217      tree type;
2218      int bitsize, bitpos;
2219      int unsignedp;
2220 {
2221   tree result = build (BIT_FIELD_REF, type, inner,
2222                        size_int (bitsize), size_int (bitpos));
2223
2224   TREE_UNSIGNED (result) = unsignedp;
2225
2226   return result;
2227 }
2228
2229 /* Optimize a bit-field compare.
2230
2231    There are two cases:  First is a compare against a constant and the
2232    second is a comparison of two items where the fields are at the same
2233    bit position relative to the start of a chunk (byte, halfword, word)
2234    large enough to contain it.  In these cases we can avoid the shift
2235    implicit in bitfield extractions.
2236
2237    For constants, we emit a compare of the shifted constant with the
2238    BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2239    compared.  For two fields at the same position, we do the ANDs with the
2240    similar mask and compare the result of the ANDs.
2241
2242    CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2243    COMPARE_TYPE is the type of the comparison, and LHS and RHS
2244    are the left and right operands of the comparison, respectively.
2245
2246    If the optimization described above can be done, we return the resulting
2247    tree.  Otherwise we return zero.  */
2248
2249 static tree
2250 optimize_bit_field_compare (code, compare_type, lhs, rhs)
2251      enum tree_code code;
2252      tree compare_type;
2253      tree lhs, rhs;
2254 {
2255   int lbitpos, lbitsize, rbitpos, rbitsize;
2256   int lnbitpos, lnbitsize, rnbitpos, rnbitsize;
2257   tree type = TREE_TYPE (lhs);
2258   tree signed_type, unsigned_type;
2259   int const_p = TREE_CODE (rhs) == INTEGER_CST;
2260   enum machine_mode lmode, rmode, lnmode, rnmode;
2261   int lunsignedp, runsignedp;
2262   int lvolatilep = 0, rvolatilep = 0;
2263   tree linner, rinner;
2264   tree mask;
2265   tree offset;
2266
2267   /* Get all the information about the extractions being done.  If the bit size
2268      if the same as the size of the underlying object, we aren't doing an
2269      extraction at all and so can do nothing.  */
2270   linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2271                                 &lunsignedp, &lvolatilep);
2272   if (lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2273       || offset != 0)
2274     return 0;
2275
2276  if (!const_p)
2277    {
2278      /* If this is not a constant, we can only do something if bit positions,
2279         sizes, and signedness are the same.   */
2280      rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset,
2281                                    &rmode, &runsignedp, &rvolatilep);
2282
2283      if (lbitpos != rbitpos || lbitsize != rbitsize
2284          || lunsignedp != runsignedp || offset != 0)
2285        return 0;
2286    }
2287
2288   /* See if we can find a mode to refer to this field.  We should be able to,
2289      but fail if we can't.  */
2290   lnmode = get_best_mode (lbitsize, lbitpos,
2291                           TYPE_ALIGN (TREE_TYPE (linner)), word_mode,
2292                           lvolatilep);
2293   if (lnmode == VOIDmode)
2294     return 0;
2295
2296   /* Set signed and unsigned types of the precision of this mode for the
2297      shifts below.  */
2298   signed_type = type_for_mode (lnmode, 0);
2299   unsigned_type = type_for_mode (lnmode, 1);
2300
2301   if (! const_p)
2302     {
2303       rnmode = get_best_mode (rbitsize, rbitpos, 
2304                               TYPE_ALIGN (TREE_TYPE (rinner)), word_mode,
2305                               rvolatilep);
2306       if (rnmode == VOIDmode)
2307         return 0;
2308     }
2309     
2310   /* Compute the bit position and size for the new reference and our offset
2311      within it. If the new reference is the same size as the original, we
2312      won't optimize anything, so return zero.  */
2313   lnbitsize = GET_MODE_BITSIZE (lnmode);
2314   lnbitpos = lbitpos & ~ (lnbitsize - 1);
2315   lbitpos -= lnbitpos;
2316   if (lnbitsize == lbitsize)
2317     return 0;
2318
2319   if (! const_p)
2320     {
2321       rnbitsize = GET_MODE_BITSIZE (rnmode);
2322       rnbitpos = rbitpos & ~ (rnbitsize - 1);
2323       rbitpos -= rnbitpos;
2324       if (rnbitsize == rbitsize)
2325         return 0;
2326     }
2327
2328 #if BYTES_BIG_ENDIAN
2329   lbitpos = lnbitsize - lbitsize - lbitpos;
2330 #endif
2331
2332   /* Make the mask to be used against the extracted field.  */
2333   mask = build_int_2 (~0, ~0);
2334   TREE_TYPE (mask) = unsigned_type;
2335   force_fit_type (mask, 0);
2336   mask = convert (unsigned_type, mask);
2337   mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0);
2338   mask = const_binop (RSHIFT_EXPR, mask,
2339                       size_int (lnbitsize - lbitsize - lbitpos), 0);
2340
2341   if (! const_p)
2342     /* If not comparing with constant, just rework the comparison
2343        and return.  */
2344     return build (code, compare_type,
2345                   build (BIT_AND_EXPR, unsigned_type,
2346                          make_bit_field_ref (linner, unsigned_type,
2347                                              lnbitsize, lnbitpos, 1),
2348                          mask),
2349                   build (BIT_AND_EXPR, unsigned_type,
2350                          make_bit_field_ref (rinner, unsigned_type,
2351                                              rnbitsize, rnbitpos, 1),
2352                          mask));
2353
2354   /* Otherwise, we are handling the constant case. See if the constant is too
2355      big for the field.  Warn and return a tree of for 0 (false) if so.  We do
2356      this not only for its own sake, but to avoid having to test for this
2357      error case below.  If we didn't, we might generate wrong code.
2358
2359      For unsigned fields, the constant shifted right by the field length should
2360      be all zero.  For signed fields, the high-order bits should agree with 
2361      the sign bit.  */
2362
2363   if (lunsignedp)
2364     {
2365       if (! integer_zerop (const_binop (RSHIFT_EXPR,
2366                                         convert (unsigned_type, rhs),
2367                                         size_int (lbitsize), 0)))
2368         {
2369           warning ("comparison is always %s due to width of bitfield",
2370                    code == NE_EXPR ? "one" : "zero");
2371           return convert (compare_type,
2372                           (code == NE_EXPR
2373                            ? integer_one_node : integer_zero_node));
2374         }
2375     }
2376   else
2377     {
2378       tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2379                               size_int (lbitsize - 1), 0);
2380       if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2381         {
2382           warning ("comparison is always %s due to width of bitfield",
2383                    code == NE_EXPR ? "one" : "zero");
2384           return convert (compare_type,
2385                           (code == NE_EXPR
2386                            ? integer_one_node : integer_zero_node));
2387         }
2388     }
2389
2390   /* Single-bit compares should always be against zero.  */
2391   if (lbitsize == 1 && ! integer_zerop (rhs))
2392     {
2393       code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2394       rhs = convert (type, integer_zero_node);
2395     }
2396
2397   /* Make a new bitfield reference, shift the constant over the
2398      appropriate number of bits and mask it with the computed mask
2399      (in case this was a signed field).  If we changed it, make a new one.  */
2400   lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1);
2401
2402   rhs = fold (const_binop (BIT_AND_EXPR,
2403                            const_binop (LSHIFT_EXPR,
2404                                         convert (unsigned_type, rhs),
2405                                         size_int (lbitpos), 0),
2406                            mask, 0));
2407
2408   return build (code, compare_type,
2409                 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2410                 rhs);
2411 }
2412 \f
2413 /* Subroutine for fold_truthop: decode a field reference.
2414
2415    If EXP is a comparison reference, we return the innermost reference.
2416
2417    *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2418    set to the starting bit number.
2419
2420    If the innermost field can be completely contained in a mode-sized
2421    unit, *PMODE is set to that mode.  Otherwise, it is set to VOIDmode.
2422
2423    *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2424    otherwise it is not changed.
2425
2426    *PUNSIGNEDP is set to the signedness of the field.
2427
2428    *PMASK is set to the mask used.  This is either contained in a
2429    BIT_AND_EXPR or derived from the width of the field.
2430
2431    Return 0 if this is not a component reference or is one that we can't
2432    do anything with.  */
2433
2434 static tree
2435 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
2436                         pvolatilep, pmask)
2437      tree exp;
2438      int *pbitsize, *pbitpos;
2439      enum machine_mode *pmode;
2440      int *punsignedp, *pvolatilep;
2441      tree *pmask;
2442 {
2443   tree mask = 0;
2444   tree inner;
2445   tree offset;
2446
2447   /* All the optimizations using this function assume integer fields.  
2448      There are problems with FP fields since the type_for_size call
2449      below can fail for, e.g., XFmode.  */
2450   if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2451     return 0;
2452
2453   STRIP_NOPS (exp);
2454
2455   if (TREE_CODE (exp) == BIT_AND_EXPR)
2456     {
2457       mask = TREE_OPERAND (exp, 1);
2458       exp = TREE_OPERAND (exp, 0);
2459       STRIP_NOPS (exp); STRIP_NOPS (mask);
2460       if (TREE_CODE (mask) != INTEGER_CST)
2461         return 0;
2462     }
2463
2464   if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF
2465       && TREE_CODE (exp) != BIT_FIELD_REF)
2466     return 0;
2467
2468   inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2469                                punsignedp, pvolatilep);
2470   if (*pbitsize < 0 || offset != 0)
2471     return 0;
2472   
2473   if (mask == 0)
2474     {
2475       tree unsigned_type = type_for_size (*pbitsize, 1);
2476       int precision = TYPE_PRECISION (unsigned_type);
2477
2478       mask = build_int_2 (~0, ~0);
2479       TREE_TYPE (mask) = unsigned_type;
2480       force_fit_type (mask, 0);
2481       mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2482       mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2483     }
2484
2485   *pmask = mask;
2486   return inner;
2487 }
2488
2489 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
2490    bit positions.  */
2491
2492 static int
2493 all_ones_mask_p (mask, size)
2494      tree mask;
2495      int size;
2496 {
2497   tree type = TREE_TYPE (mask);
2498   int precision = TYPE_PRECISION (type);
2499   tree tmask;
2500
2501   tmask = build_int_2 (~0, ~0);
2502   TREE_TYPE (tmask) = signed_type (type);
2503   force_fit_type (tmask, 0);
2504   return
2505     operand_equal_p (mask, 
2506                      const_binop (RSHIFT_EXPR,
2507                                   const_binop (LSHIFT_EXPR, tmask,
2508                                                size_int (precision - size), 0),
2509                                   size_int (precision - size), 0),
2510                      0);
2511 }
2512
2513 /* Subroutine for fold_truthop: determine if an operand is simple enough
2514    to be evaluated unconditionally.  */
2515
2516 static int 
2517 simple_operand_p (exp)
2518      tree exp;
2519 {
2520   /* Strip any conversions that don't change the machine mode.  */
2521   while ((TREE_CODE (exp) == NOP_EXPR
2522           || TREE_CODE (exp) == CONVERT_EXPR)
2523          && (TYPE_MODE (TREE_TYPE (exp))
2524              == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
2525     exp = TREE_OPERAND (exp, 0);
2526
2527   return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
2528           || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
2529               && ! TREE_ADDRESSABLE (exp)
2530               && ! TREE_THIS_VOLATILE (exp)
2531               && ! DECL_NONLOCAL (exp)
2532               /* Don't regard global variables as simple.  They may be
2533                  allocated in ways unknown to the compiler (shared memory,
2534                  #pragma weak, etc).  */
2535               && ! TREE_PUBLIC (exp)
2536               && ! DECL_EXTERNAL (exp)
2537               /* Loading a static variable is unduly expensive, but global
2538                  registers aren't expensive.  */
2539               && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
2540 }
2541 \f
2542 /* Subroutine for fold_truthop: try to optimize a range test.
2543
2544    For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7".
2545
2546    JCODE is the logical combination of the two terms.  It is TRUTH_AND_EXPR
2547    (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR
2548    (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR).  TYPE is the type of
2549    the result.
2550
2551    VAR is the value being tested.  LO_CODE and HI_CODE are the comparison
2552    operators comparing VAR to LO_CST and HI_CST.  LO_CST is known to be no
2553    larger than HI_CST (they may be equal).
2554
2555    We return the simplified tree or 0 if no optimization is possible.  */
2556
2557 static tree
2558 range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst)
2559      enum tree_code jcode, lo_code, hi_code;
2560      tree type, var, lo_cst, hi_cst;
2561 {
2562   tree utype;
2563   enum tree_code rcode;
2564
2565   /* See if this is a range test and normalize the constant terms.  */
2566
2567   if (jcode == TRUTH_AND_EXPR)
2568     {
2569       switch (lo_code)
2570         {
2571         case NE_EXPR:
2572           /* See if we have VAR != CST && VAR != CST+1.  */
2573           if (! (hi_code == NE_EXPR
2574                  && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
2575                  && tree_int_cst_equal (integer_one_node,
2576                                         const_binop (MINUS_EXPR,
2577                                                      hi_cst, lo_cst, 0))))
2578             return 0;
2579
2580           rcode = GT_EXPR;
2581           break;
2582
2583         case GT_EXPR:
2584         case GE_EXPR:
2585           if (hi_code == LT_EXPR)
2586             hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
2587           else if (hi_code != LE_EXPR)
2588             return 0;
2589
2590           if (lo_code == GT_EXPR)
2591             lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
2592
2593           /* We now have VAR >= LO_CST && VAR <= HI_CST.  */
2594           rcode = LE_EXPR;
2595           break;
2596
2597         default:
2598           return 0;
2599         }
2600     }
2601   else
2602     {
2603       switch (lo_code)
2604         {
2605         case EQ_EXPR:
2606           /* See if we have VAR == CST || VAR == CST+1.  */
2607           if (! (hi_code == EQ_EXPR
2608                  && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1
2609                  && tree_int_cst_equal (integer_one_node,
2610                                         const_binop (MINUS_EXPR,
2611                                                      hi_cst, lo_cst, 0))))
2612             return 0;
2613
2614           rcode = LE_EXPR;
2615           break;
2616
2617         case LE_EXPR:
2618         case LT_EXPR:
2619           if (hi_code == GE_EXPR)
2620             hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node, 0);
2621           else if (hi_code != GT_EXPR)
2622             return 0;
2623
2624           if (lo_code == LE_EXPR)
2625             lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node, 0);
2626
2627           /* We now have VAR < LO_CST || VAR > HI_CST.  */
2628           rcode = GT_EXPR;
2629           break;
2630
2631         default:
2632           return 0;
2633         }
2634     }
2635
2636   /* When normalizing, it is possible to both increment the smaller constant
2637      and decrement the larger constant.  See if they are still ordered.  */
2638   if (tree_int_cst_lt (hi_cst, lo_cst))
2639     return 0;
2640
2641   /* Fail if VAR isn't an integer.  */
2642   utype = TREE_TYPE (var);
2643   if (! INTEGRAL_TYPE_P (utype))
2644     return 0;
2645
2646   /* The range test is invalid if subtracting the two constants results
2647      in overflow.  This can happen in traditional mode.  */
2648   if (! int_fits_type_p (hi_cst, TREE_TYPE (var))
2649       || ! int_fits_type_p (lo_cst, TREE_TYPE (var)))
2650     return 0;
2651
2652   if (! TREE_UNSIGNED (utype))
2653     {
2654       utype = unsigned_type (utype);
2655       var = convert (utype, var);
2656       lo_cst = convert (utype, lo_cst);
2657       hi_cst = convert (utype, hi_cst);
2658     }
2659
2660   return fold (convert (type,
2661                         build (rcode, utype,
2662                                build (MINUS_EXPR, utype, var, lo_cst),
2663                                const_binop (MINUS_EXPR, hi_cst, lo_cst, 0))));
2664 }
2665 \f
2666 /* Find ways of folding logical expressions of LHS and RHS:
2667    Try to merge two comparisons to the same innermost item.
2668    Look for range tests like "ch >= '0' && ch <= '9'".
2669    Look for combinations of simple terms on machines with expensive branches
2670    and evaluate the RHS unconditionally.
2671
2672    For example, if we have p->a == 2 && p->b == 4 and we can make an
2673    object large enough to span both A and B, we can do this with a comparison
2674    against the object ANDed with the a mask.
2675
2676    If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
2677    operations to do this with one comparison.
2678
2679    We check for both normal comparisons and the BIT_AND_EXPRs made this by
2680    function and the one above.
2681
2682    CODE is the logical operation being done.  It can be TRUTH_ANDIF_EXPR,
2683    TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
2684
2685    TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
2686    two operands.
2687
2688    We return the simplified tree or 0 if no optimization is possible.  */
2689
2690 static tree
2691 fold_truthop (code, truth_type, lhs, rhs)
2692      enum tree_code code;
2693      tree truth_type, lhs, rhs;
2694 {
2695   /* If this is the "or" of two comparisons, we can do something if we
2696      the comparisons are NE_EXPR.  If this is the "and", we can do something
2697      if the comparisons are EQ_EXPR.  I.e., 
2698         (a->b == 2 && a->c == 4) can become (a->new == NEW).
2699
2700      WANTED_CODE is this operation code.  For single bit fields, we can
2701      convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
2702      comparison for one-bit fields.  */
2703
2704   enum tree_code wanted_code;
2705   enum tree_code lcode, rcode;
2706   tree ll_arg, lr_arg, rl_arg, rr_arg;
2707   tree ll_inner, lr_inner, rl_inner, rr_inner;
2708   int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
2709   int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
2710   int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
2711   int lnbitsize, lnbitpos, rnbitsize, rnbitpos;
2712   int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
2713   enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
2714   enum machine_mode lnmode, rnmode;
2715   tree ll_mask, lr_mask, rl_mask, rr_mask;
2716   tree l_const, r_const;
2717   tree type, result;
2718   int first_bit, end_bit;
2719   int volatilep;
2720
2721   /* Start by getting the comparison codes and seeing if this looks like
2722      a range test.  Fail if anything is volatile.  If one operand is a
2723      BIT_AND_EXPR with the constant one, treat it as if it were surrounded
2724      with a NE_EXPR.  */
2725
2726   if (TREE_SIDE_EFFECTS (lhs)
2727       || TREE_SIDE_EFFECTS (rhs))
2728     return 0;
2729
2730   lcode = TREE_CODE (lhs);
2731   rcode = TREE_CODE (rhs);
2732
2733   if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
2734     lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
2735
2736   if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
2737     rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
2738
2739   if (TREE_CODE_CLASS (lcode) != '<'
2740       || TREE_CODE_CLASS (rcode) != '<')
2741     return 0;
2742
2743   code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
2744           ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
2745
2746   ll_arg = TREE_OPERAND (lhs, 0);
2747   lr_arg = TREE_OPERAND (lhs, 1);
2748   rl_arg = TREE_OPERAND (rhs, 0);
2749   rr_arg = TREE_OPERAND (rhs, 1);
2750   
2751   if (TREE_CODE (lr_arg) == INTEGER_CST
2752       && TREE_CODE (rr_arg) == INTEGER_CST
2753       && operand_equal_p (ll_arg, rl_arg, 0))
2754     {
2755       if (tree_int_cst_lt (lr_arg, rr_arg))
2756         result = range_test (code, truth_type, lcode, rcode,
2757                              ll_arg, lr_arg, rr_arg);
2758       else
2759         result = range_test (code, truth_type, rcode, lcode,
2760                              ll_arg, rr_arg, lr_arg);
2761
2762       /* If this isn't a range test, it also isn't a comparison that
2763          can be merged.  However, it wins to evaluate the RHS unconditionally
2764          on machines with expensive branches.   */
2765
2766       if (result == 0 && BRANCH_COST >= 2)
2767         {
2768           if (TREE_CODE (ll_arg) != VAR_DECL
2769               && TREE_CODE (ll_arg) != PARM_DECL)
2770             {
2771               /* Avoid evaluating the variable part twice.  */
2772               ll_arg = save_expr (ll_arg);
2773               lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg);
2774               rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg);
2775             }
2776           return build (code, truth_type, lhs, rhs);
2777         }
2778       return result;
2779     }
2780
2781   /* If the RHS can be evaluated unconditionally and its operands are
2782      simple, it wins to evaluate the RHS unconditionally on machines
2783      with expensive branches.  In this case, this isn't a comparison
2784      that can be merged.  */
2785
2786   /* @@ I'm not sure it wins on the m88110 to do this if the comparisons
2787      are with zero (tmw).  */
2788
2789   if (BRANCH_COST >= 2
2790       && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
2791       && simple_operand_p (rl_arg)
2792       && simple_operand_p (rr_arg))
2793     return build (code, truth_type, lhs, rhs);
2794
2795   /* See if the comparisons can be merged.  Then get all the parameters for
2796      each side.  */
2797
2798   if ((lcode != EQ_EXPR && lcode != NE_EXPR)
2799       || (rcode != EQ_EXPR && rcode != NE_EXPR))
2800     return 0;
2801
2802   volatilep = 0;
2803   ll_inner = decode_field_reference (ll_arg,
2804                                      &ll_bitsize, &ll_bitpos, &ll_mode,
2805                                      &ll_unsignedp, &volatilep, &ll_mask);
2806   lr_inner = decode_field_reference (lr_arg,
2807                                      &lr_bitsize, &lr_bitpos, &lr_mode,
2808                                      &lr_unsignedp, &volatilep, &lr_mask);
2809   rl_inner = decode_field_reference (rl_arg,
2810                                      &rl_bitsize, &rl_bitpos, &rl_mode,
2811                                      &rl_unsignedp, &volatilep, &rl_mask);
2812   rr_inner = decode_field_reference (rr_arg,
2813                                      &rr_bitsize, &rr_bitpos, &rr_mode,
2814                                      &rr_unsignedp, &volatilep, &rr_mask);
2815
2816   /* It must be true that the inner operation on the lhs of each
2817      comparison must be the same if we are to be able to do anything.
2818      Then see if we have constants.  If not, the same must be true for
2819      the rhs's.  */
2820   if (volatilep || ll_inner == 0 || rl_inner == 0
2821       || ! operand_equal_p (ll_inner, rl_inner, 0))
2822     return 0;
2823
2824   if (TREE_CODE (lr_arg) == INTEGER_CST
2825       && TREE_CODE (rr_arg) == INTEGER_CST)
2826     l_const = lr_arg, r_const = rr_arg;
2827   else if (lr_inner == 0 || rr_inner == 0
2828            || ! operand_equal_p (lr_inner, rr_inner, 0))
2829     return 0;
2830   else
2831     l_const = r_const = 0;
2832
2833   /* If either comparison code is not correct for our logical operation,
2834      fail.  However, we can convert a one-bit comparison against zero into
2835      the opposite comparison against that bit being set in the field.  */
2836
2837   wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
2838   if (lcode != wanted_code)
2839     {
2840       if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
2841         l_const = ll_mask;
2842       else
2843         return 0;
2844     }
2845
2846   if (rcode != wanted_code)
2847     {
2848       if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
2849         r_const = rl_mask;
2850       else
2851         return 0;
2852     }
2853
2854   /* See if we can find a mode that contains both fields being compared on
2855      the left.  If we can't, fail.  Otherwise, update all constants and masks
2856      to be relative to a field of that size.  */
2857   first_bit = MIN (ll_bitpos, rl_bitpos);
2858   end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
2859   lnmode = get_best_mode (end_bit - first_bit, first_bit,
2860                           TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
2861                           volatilep);
2862   if (lnmode == VOIDmode)
2863     return 0;
2864
2865   lnbitsize = GET_MODE_BITSIZE (lnmode);
2866   lnbitpos = first_bit & ~ (lnbitsize - 1);
2867   type = type_for_size (lnbitsize, 1);
2868   xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
2869
2870 #if BYTES_BIG_ENDIAN
2871   xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
2872   xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
2873 #endif
2874
2875   ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask),
2876                          size_int (xll_bitpos), 0);
2877   rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask),
2878                          size_int (xrl_bitpos), 0);
2879
2880   /* Make sure the constants are interpreted as unsigned, so we
2881      don't have sign bits outside the range of their type.  */
2882
2883   if (l_const)
2884     {
2885       l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const);
2886       l_const = const_binop (LSHIFT_EXPR, convert (type, l_const),
2887                              size_int (xll_bitpos), 0);
2888     }
2889   if (r_const)
2890     {
2891       r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const);
2892       r_const = const_binop (LSHIFT_EXPR, convert (type, r_const),
2893                              size_int (xrl_bitpos), 0);
2894     }
2895
2896   /* If the right sides are not constant, do the same for it.  Also,
2897      disallow this optimization if a size or signedness mismatch occurs
2898      between the left and right sides.  */
2899   if (l_const == 0)
2900     {
2901       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
2902           || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
2903           /* Make sure the two fields on the right
2904              correspond to the left without being swapped.  */
2905           || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
2906         return 0;
2907
2908       first_bit = MIN (lr_bitpos, rr_bitpos);
2909       end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
2910       rnmode = get_best_mode (end_bit - first_bit, first_bit,
2911                               TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
2912                               volatilep);
2913       if (rnmode == VOIDmode)
2914         return 0;
2915
2916       rnbitsize = GET_MODE_BITSIZE (rnmode);
2917       rnbitpos = first_bit & ~ (rnbitsize - 1);
2918       xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
2919
2920 #if BYTES_BIG_ENDIAN
2921       xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
2922       xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
2923 #endif
2924
2925       lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask),
2926                              size_int (xlr_bitpos), 0);
2927       rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask),
2928                              size_int (xrr_bitpos), 0);
2929
2930       /* Make a mask that corresponds to both fields being compared.
2931          Do this for both items being compared.  If the masks agree,
2932          we can do this by masking both and comparing the masked
2933          results.  */
2934       ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
2935       lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
2936       if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize)
2937         {
2938           lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
2939                                     ll_unsignedp || rl_unsignedp);
2940           rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos,
2941                                     lr_unsignedp || rr_unsignedp);
2942           if (! all_ones_mask_p (ll_mask, lnbitsize))
2943             {
2944               lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
2945               rhs = build (BIT_AND_EXPR, type, rhs, ll_mask);
2946             }
2947           return build (wanted_code, truth_type, lhs, rhs);
2948         }
2949
2950       /* There is still another way we can do something:  If both pairs of
2951          fields being compared are adjacent, we may be able to make a wider
2952          field containing them both.  */
2953       if ((ll_bitsize + ll_bitpos == rl_bitpos
2954            && lr_bitsize + lr_bitpos == rr_bitpos)
2955           || (ll_bitpos == rl_bitpos + rl_bitsize
2956               && lr_bitpos == rr_bitpos + rr_bitsize))
2957         return build (wanted_code, truth_type,
2958                       make_bit_field_ref (ll_inner, type,
2959                                           ll_bitsize + rl_bitsize,
2960                                           MIN (ll_bitpos, rl_bitpos),
2961                                           ll_unsignedp),
2962                       make_bit_field_ref (lr_inner, type,
2963                                           lr_bitsize + rr_bitsize,
2964                                           MIN (lr_bitpos, rr_bitpos),
2965                                           lr_unsignedp));
2966
2967       return 0;
2968     }
2969
2970   /* Handle the case of comparisons with constants.  If there is something in
2971      common between the masks, those bits of the constants must be the same.
2972      If not, the condition is always false.  Test for this to avoid generating
2973      incorrect code below.  */
2974   result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
2975   if (! integer_zerop (result)
2976       && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
2977                            const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
2978     {
2979       if (wanted_code == NE_EXPR)
2980         {
2981           warning ("`or' of unmatched not-equal tests is always 1");
2982           return convert (truth_type, integer_one_node);
2983         }
2984       else
2985         {
2986           warning ("`and' of mutually exclusive equal-tests is always zero");
2987           return convert (truth_type, integer_zero_node);
2988         }
2989     }
2990
2991   /* Construct the expression we will return.  First get the component
2992      reference we will make.  Unless the mask is all ones the width of
2993      that field, perform the mask operation.  Then compare with the
2994      merged constant.  */
2995   result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos,
2996                                ll_unsignedp || rl_unsignedp);
2997
2998   ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
2999   if (! all_ones_mask_p (ll_mask, lnbitsize))
3000     result = build (BIT_AND_EXPR, type, result, ll_mask);
3001
3002   return build (wanted_code, truth_type, result,
3003                 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
3004 }
3005 \f
3006 /* Perform constant folding and related simplification of EXPR.
3007    The related simplifications include x*1 => x, x*0 => 0, etc.,
3008    and application of the associative law.
3009    NOP_EXPR conversions may be removed freely (as long as we
3010    are careful not to change the C type of the overall expression)
3011    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
3012    but we can constant-fold them if they have constant operands.  */
3013
3014 tree
3015 fold (expr) 
3016      tree expr;
3017 {
3018   register tree t = expr;
3019   tree t1 = NULL_TREE;
3020   tree tem;
3021   tree type = TREE_TYPE (expr);
3022   register tree arg0, arg1;
3023   register enum tree_code code = TREE_CODE (t);
3024   register int kind;
3025   int invert;
3026
3027   /* WINS will be nonzero when the switch is done
3028      if all operands are constant.  */
3029
3030   int wins = 1;
3031
3032   /* Return right away if already constant.  */
3033   if (TREE_CONSTANT (t))
3034     {
3035       if (code == CONST_DECL)
3036         return DECL_INITIAL (t);
3037       return t;
3038     }
3039   
3040   kind = TREE_CODE_CLASS (code);
3041   if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
3042     {
3043       tree subop;
3044
3045       /* Special case for conversion ops that can have fixed point args.  */
3046       arg0 = TREE_OPERAND (t, 0);
3047
3048       /* Don't use STRIP_NOPS, because signedness of argument type matters.  */
3049       if (arg0 != 0)
3050         STRIP_TYPE_NOPS (arg0);
3051
3052       if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
3053         subop = TREE_REALPART (arg0);
3054       else
3055         subop = arg0;
3056
3057       if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
3058 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3059           && TREE_CODE (subop) != REAL_CST
3060 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3061           )
3062         /* Note that TREE_CONSTANT isn't enough:
3063            static var addresses are constant but we can't
3064            do arithmetic on them.  */
3065         wins = 0;
3066     }
3067   else if (kind == 'e' || kind == '<'
3068            || kind == '1' || kind == '2' || kind == 'r')
3069     {
3070       register int len = tree_code_length[(int) code];
3071       register int i;
3072       for (i = 0; i < len; i++)
3073         {
3074           tree op = TREE_OPERAND (t, i);
3075           tree subop;
3076
3077           if (op == 0)
3078             continue;           /* Valid for CALL_EXPR, at least.  */
3079
3080           if (kind == '<' || code == RSHIFT_EXPR)
3081             {
3082               /* Signedness matters here.  Perhaps we can refine this
3083                  later.  */
3084               STRIP_TYPE_NOPS (op);
3085             }
3086           else
3087             {
3088               /* Strip any conversions that don't change the mode.  */
3089               STRIP_NOPS (op);
3090             }
3091           
3092           if (TREE_CODE (op) == COMPLEX_CST)
3093             subop = TREE_REALPART (op);
3094           else
3095             subop = op;
3096
3097           if (TREE_CODE (subop) != INTEGER_CST
3098 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3099               && TREE_CODE (subop) != REAL_CST
3100 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3101               )
3102             /* Note that TREE_CONSTANT isn't enough:
3103                static var addresses are constant but we can't
3104                do arithmetic on them.  */
3105             wins = 0;
3106
3107           if (i == 0)
3108             arg0 = op;
3109           else if (i == 1)
3110             arg1 = op;
3111         }
3112     }
3113
3114   /* If this is a commutative operation, and ARG0 is a constant, move it
3115      to ARG1 to reduce the number of tests below.  */
3116   if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
3117        || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
3118        || code == BIT_AND_EXPR)
3119       && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
3120     {
3121       tem = arg0; arg0 = arg1; arg1 = tem;
3122
3123       tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
3124       TREE_OPERAND (t, 1) = tem;
3125     }
3126
3127   /* Now WINS is set as described above,
3128      ARG0 is the first operand of EXPR,
3129      and ARG1 is the second operand (if it has more than one operand).
3130
3131      First check for cases where an arithmetic operation is applied to a
3132      compound, conditional, or comparison operation.  Push the arithmetic
3133      operation inside the compound or conditional to see if any folding
3134      can then be done.  Convert comparison to conditional for this purpose.
3135      The also optimizes non-constant cases that used to be done in
3136      expand_expr.
3137
3138      Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR,
3139      one of the operands is a comparison and the other is either a comparison
3140      or a BIT_AND_EXPR with the constant 1.  In that case, the code below
3141      would make the expression more complex.  Change it to a
3142      TRUTH_{AND,OR}_EXPR.  */
3143
3144   if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
3145       && ((TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
3146            && (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
3147                || (TREE_CODE (arg1) == BIT_AND_EXPR
3148                    && integer_onep (TREE_OPERAND (arg1, 1)))))
3149           || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
3150               && (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
3151                   || (TREE_CODE (arg0) == BIT_AND_EXPR
3152                       && integer_onep (TREE_OPERAND (arg0, 1)))))))
3153     return fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3154                         type, arg0, arg1));
3155
3156   if (TREE_CODE_CLASS (code) == '1')
3157     {
3158       if (TREE_CODE (arg0) == COMPOUND_EXPR)
3159         return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3160                       fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
3161       else if (TREE_CODE (arg0) == COND_EXPR)
3162         {
3163           t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
3164                            fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
3165                            fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
3166
3167           /* If this was a conversion, and all we did was to move into
3168              inside the COND_EXPR, bring it back out.  Then return so we
3169              don't get into an infinite recursion loop taking the conversion
3170              out and then back in.  */
3171
3172           if ((code == NOP_EXPR || code == CONVERT_EXPR
3173                || code == NON_LVALUE_EXPR)
3174               && TREE_CODE (t) == COND_EXPR
3175               && TREE_CODE (TREE_OPERAND (t, 1)) == code
3176               && TREE_CODE (TREE_OPERAND (t, 2)) == code
3177               && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
3178                   == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0))))
3179             t = build1 (code, type,
3180                         build (COND_EXPR,
3181                                TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)),
3182                                TREE_OPERAND (t, 0),
3183                                TREE_OPERAND (TREE_OPERAND (t, 1), 0),
3184                                TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
3185           return t;
3186         }
3187       else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') 
3188         return fold (build (COND_EXPR, type, arg0,
3189                             fold (build1 (code, type, integer_one_node)),
3190                             fold (build1 (code, type, integer_zero_node))));
3191    }
3192   else if (TREE_CODE_CLASS (code) == '2'
3193            || TREE_CODE_CLASS (code) == '<')
3194     {
3195       if (TREE_CODE (arg1) == COMPOUND_EXPR)
3196         return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3197                       fold (build (code, type,
3198                                    arg0, TREE_OPERAND (arg1, 1))));
3199       else if (TREE_CODE (arg1) == COND_EXPR
3200                || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
3201         {
3202           tree test, true_value, false_value;
3203
3204           if (TREE_CODE (arg1) == COND_EXPR)
3205             {
3206               test = TREE_OPERAND (arg1, 0);
3207               true_value = TREE_OPERAND (arg1, 1);
3208               false_value = TREE_OPERAND (arg1, 2);
3209             }
3210           else
3211             {
3212               test = arg1;
3213               true_value = integer_one_node;
3214               false_value = integer_zero_node;
3215             }
3216
3217           /* If ARG0 is complex we want to make sure we only evaluate
3218              it once.  Though this is only required if it is volatile, it
3219              might be more efficient even if it is not.  However, if we
3220              succeed in folding one part to a constant, we do not need
3221              to make this SAVE_EXPR.  Since we do this optimization
3222              primarily to see if we do end up with constant and this
3223              SAVE_EXPR interfers with later optimizations, suppressing
3224              it when we can is important.  */
3225
3226           if ((TREE_CODE (arg0) != VAR_DECL && TREE_CODE (arg0) != PARM_DECL)
3227               || TREE_SIDE_EFFECTS (arg0))
3228             {
3229               tree lhs = fold (build (code, type, arg0, true_value));
3230               tree rhs = fold (build (code, type, arg0, false_value));
3231
3232               if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
3233                 return fold (build (COND_EXPR, type, test, lhs, rhs));
3234
3235               arg0 = save_expr (arg0);
3236             }
3237
3238           test = fold (build (COND_EXPR, type, test,
3239                               fold (build (code, type, arg0, true_value)),
3240                               fold (build (code, type, arg0, false_value))));
3241           if (TREE_CODE (arg0) == SAVE_EXPR)
3242             return build (COMPOUND_EXPR, type,
3243                           convert (void_type_node, arg0), test);
3244           else
3245             return convert (type, test);
3246         }
3247
3248       else if (TREE_CODE (arg0) == COMPOUND_EXPR)
3249         return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3250                       fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3251       else if (TREE_CODE (arg0) == COND_EXPR
3252                || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
3253         {
3254           tree test, true_value, false_value;
3255
3256           if (TREE_CODE (arg0) == COND_EXPR)
3257             {
3258               test = TREE_OPERAND (arg0, 0);
3259               true_value = TREE_OPERAND (arg0, 1);
3260               false_value = TREE_OPERAND (arg0, 2);
3261             }
3262           else
3263             {
3264               test = arg0;
3265               true_value = integer_one_node;
3266               false_value = integer_zero_node;
3267             }
3268
3269           if ((TREE_CODE (arg1) != VAR_DECL && TREE_CODE (arg1) != PARM_DECL)
3270               || TREE_SIDE_EFFECTS (arg1))
3271             {
3272               tree lhs = fold (build (code, type, true_value, arg1));
3273               tree rhs = fold (build (code, type, false_value, arg1));
3274
3275               if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs))
3276                 return fold (build (COND_EXPR, type, test, lhs, rhs));
3277
3278               arg1 = save_expr (arg1);
3279             }
3280
3281           test = fold (build (COND_EXPR, type, test,
3282                               fold (build (code, type, true_value, arg1)),
3283                               fold (build (code, type, false_value, arg1))));
3284           if (TREE_CODE (arg1) == SAVE_EXPR)
3285             return build (COMPOUND_EXPR, type,
3286                           convert (void_type_node, arg1), test);
3287           else
3288             return convert (type, test);
3289         }
3290     }
3291   else if (TREE_CODE_CLASS (code) == '<'
3292            && TREE_CODE (arg0) == COMPOUND_EXPR)
3293     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
3294                   fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
3295   else if (TREE_CODE_CLASS (code) == '<'
3296            && TREE_CODE (arg1) == COMPOUND_EXPR)
3297     return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
3298                   fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
3299           
3300   switch (code)
3301     {
3302     case INTEGER_CST:
3303     case REAL_CST:
3304     case STRING_CST:
3305     case COMPLEX_CST:
3306     case CONSTRUCTOR:
3307       return t;
3308
3309     case CONST_DECL:
3310       return fold (DECL_INITIAL (t));
3311
3312     case NOP_EXPR:
3313     case FLOAT_EXPR:
3314     case CONVERT_EXPR:
3315     case FIX_TRUNC_EXPR:
3316       /* Other kinds of FIX are not handled properly by fold_convert.  */
3317       /* Two conversions in a row are not needed unless:
3318          - the intermediate type is narrower than both initial and final, or
3319          - the intermediate type and innermost type differ in signedness,
3320            and the outermost type is wider than the intermediate, or
3321          - the initial type is a pointer type and the precisions of the
3322            intermediate and final types differ, or
3323          - the final type is a pointer type and the precisions of the 
3324           initial and intermediate types differ.  */
3325       if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
3326            || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
3327           && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3328               > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3329               ||
3330               TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3331               > TYPE_PRECISION (TREE_TYPE (t)))
3332           && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3333                  == INTEGER_TYPE)
3334                 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
3335                     == INTEGER_TYPE)
3336                 && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
3337                     != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3338                 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3339                     < TYPE_PRECISION (TREE_TYPE (t))))
3340           && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0)))
3341                && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3342                    > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))))
3343               ==
3344               (TREE_UNSIGNED (TREE_TYPE (t))
3345                && (TYPE_PRECISION (TREE_TYPE (t))
3346                    > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
3347           && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3348                  == POINTER_TYPE)
3349                 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3350                     != TYPE_PRECISION (TREE_TYPE (t))))
3351           && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
3352                 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
3353                     != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))))))
3354         return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0));
3355
3356       if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
3357           && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
3358           /* Detect assigning a bitfield.  */
3359           && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
3360                && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
3361         {
3362           /* Don't leave an assignment inside a conversion
3363              unless assigning a bitfield.  */
3364           tree prev = TREE_OPERAND (t, 0);
3365           TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
3366           /* First do the assignment, then return converted constant.  */
3367           t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
3368           TREE_USED (t) = 1;
3369           return t;
3370         }
3371       if (!wins)
3372         {
3373           TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
3374           return t;
3375         }
3376       return fold_convert (t, arg0);
3377
3378 #if 0  /* This loses on &"foo"[0].  */
3379     case ARRAY_REF:
3380         {
3381           int i;
3382
3383           /* Fold an expression like: "foo"[2] */
3384           if (TREE_CODE (arg0) == STRING_CST
3385               && TREE_CODE (arg1) == INTEGER_CST
3386               && !TREE_INT_CST_HIGH (arg1)
3387               && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0))
3388             {
3389               t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0);
3390               TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
3391               force_fit_type (t, 0);
3392             }
3393         }
3394       return t;
3395 #endif /* 0 */
3396
3397     case RANGE_EXPR:
3398       TREE_CONSTANT (t) = wins;
3399       return t;
3400
3401     case NEGATE_EXPR:
3402       if (wins)
3403         {
3404           if (TREE_CODE (arg0) == INTEGER_CST)
3405             {
3406               HOST_WIDE_INT low, high;
3407               int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3408                                          TREE_INT_CST_HIGH (arg0),
3409                                          &low, &high);
3410               t = build_int_2 (low, high);
3411               TREE_TYPE (t) = type;
3412               TREE_CONSTANT_OVERFLOW (t)
3413                 = (TREE_CONSTANT_OVERFLOW (arg0)
3414                    | force_fit_type (t, overflow));
3415             }
3416           else if (TREE_CODE (arg0) == REAL_CST)
3417             t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3418           TREE_TYPE (t) = type;
3419         }
3420       else if (TREE_CODE (arg0) == NEGATE_EXPR)
3421         return TREE_OPERAND (arg0, 0);
3422
3423       /* Convert - (a - b) to (b - a) for non-floating-point.  */
3424       else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type))
3425         return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
3426                       TREE_OPERAND (arg0, 0));
3427
3428       return t;
3429
3430     case ABS_EXPR:
3431       if (wins)
3432         {
3433           if (TREE_CODE (arg0) == INTEGER_CST)
3434             {
3435               if (! TREE_UNSIGNED (type)
3436                   && TREE_INT_CST_HIGH (arg0) < 0)
3437                 {
3438                   HOST_WIDE_INT low, high;
3439                   int overflow = neg_double (TREE_INT_CST_LOW (arg0),
3440                                              TREE_INT_CST_HIGH (arg0),
3441                                              &low, &high);
3442                   t = build_int_2 (low, high);
3443                   TREE_TYPE (t) = type;
3444                   TREE_CONSTANT_OVERFLOW (t)
3445                     = (TREE_CONSTANT_OVERFLOW (arg0)
3446                        | force_fit_type (t, overflow));
3447                 }
3448             }
3449           else if (TREE_CODE (arg0) == REAL_CST)
3450             {
3451               if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
3452                 t = build_real (type,
3453                                 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
3454             }
3455           TREE_TYPE (t) = type;
3456         }
3457       else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
3458         return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
3459       return t;
3460
3461     case BIT_NOT_EXPR:
3462       if (wins)
3463         {
3464           if (TREE_CODE (arg0) == INTEGER_CST)
3465             t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
3466                              ~ TREE_INT_CST_HIGH (arg0));
3467           TREE_TYPE (t) = type;
3468           force_fit_type (t, 0);
3469           TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
3470         }
3471       else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
3472         return TREE_OPERAND (arg0, 0);
3473       return t;
3474
3475     case PLUS_EXPR:
3476       /* A + (-B) -> A - B */
3477       if (TREE_CODE (arg1) == NEGATE_EXPR)
3478         return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
3479       else if (! FLOAT_TYPE_P (type))
3480         {
3481           if (integer_zerop (arg1))
3482             return non_lvalue (convert (type, arg0));
3483
3484           /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
3485              with a constant, and the two constants have no bits in common,
3486              we should treat this as a BIT_IOR_EXPR since this may produce more
3487              simplifications.  */
3488           if (TREE_CODE (arg0) == BIT_AND_EXPR
3489               && TREE_CODE (arg1) == BIT_AND_EXPR
3490               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3491               && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3492               && integer_zerop (const_binop (BIT_AND_EXPR,
3493                                              TREE_OPERAND (arg0, 1),
3494                                              TREE_OPERAND (arg1, 1), 0)))
3495             {
3496               code = BIT_IOR_EXPR;
3497               goto bit_ior;
3498             }
3499         }
3500       /* In IEEE floating point, x+0 may not equal x.  */
3501       else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3502                && real_zerop (arg1))
3503         return non_lvalue (convert (type, arg0));
3504     associate:
3505       /* In most languages, can't associate operations on floats
3506          through parentheses.  Rather than remember where the parentheses
3507          were, we don't associate floats at all.  It shouldn't matter much.  */
3508       if (FLOAT_TYPE_P (type))
3509         goto binary;
3510       /* The varsign == -1 cases happen only for addition and subtraction.
3511          It says that the arg that was split was really CON minus VAR.
3512          The rest of the code applies to all associative operations.  */
3513       if (!wins)
3514         {
3515           tree var, con;
3516           int varsign;
3517
3518           if (split_tree (arg0, code, &var, &con, &varsign))
3519             {
3520               if (varsign == -1)
3521                 {
3522                   /* EXPR is (CON-VAR) +- ARG1.  */
3523                   /* If it is + and VAR==ARG1, return just CONST.  */
3524                   if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0))
3525                     return convert (TREE_TYPE (t), con);
3526                     
3527                   /* If ARG0 is a constant, don't change things around;
3528                      instead keep all the constant computations together.  */
3529
3530                   if (TREE_CONSTANT (arg0))
3531                     return t;
3532
3533                   /* Otherwise return (CON +- ARG1) - VAR.  */
3534                   TREE_SET_CODE (t, MINUS_EXPR);
3535                   TREE_OPERAND (t, 1) = var;
3536                   TREE_OPERAND (t, 0)
3537                     = fold (build (code, TREE_TYPE (t), con, arg1));
3538                 }
3539               else
3540                 {
3541                   /* EXPR is (VAR+CON) +- ARG1.  */
3542                   /* If it is - and VAR==ARG1, return just CONST.  */
3543                   if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0))
3544                     return convert (TREE_TYPE (t), con);
3545                     
3546                   /* If ARG0 is a constant, don't change things around;
3547                      instead keep all the constant computations together.  */
3548
3549                   if (TREE_CONSTANT (arg0))
3550                     return t;
3551
3552                   /* Otherwise return VAR +- (ARG1 +- CON).  */
3553                   TREE_OPERAND (t, 1) = tem
3554                     = fold (build (code, TREE_TYPE (t), arg1, con));
3555                   TREE_OPERAND (t, 0) = var;
3556                   if (integer_zerop (tem)
3557                       && (code == PLUS_EXPR || code == MINUS_EXPR))
3558                     return convert (type, var);
3559                   /* If we have x +/- (c - d) [c an explicit integer]
3560                      change it to x -/+ (d - c) since if d is relocatable
3561                      then the latter can be a single immediate insn
3562                      and the former cannot.  */
3563                   if (TREE_CODE (tem) == MINUS_EXPR
3564                       && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
3565                     {
3566                       tree tem1 = TREE_OPERAND (tem, 1);
3567                       TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
3568                       TREE_OPERAND (tem, 0) = tem1;
3569                       TREE_SET_CODE (t,
3570                                      (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
3571                     }
3572                 }
3573               return t;
3574             }
3575
3576           if (split_tree (arg1, code, &var, &con, &varsign))
3577             {
3578               /* EXPR is ARG0 +- (CON +- VAR).  */
3579               if (TREE_CODE (t) == MINUS_EXPR
3580                   && operand_equal_p (var, arg0, 0))
3581                 {
3582                   /* If VAR and ARG0 cancel, return just CON or -CON.  */
3583                   if (code == PLUS_EXPR)
3584                     return convert (TREE_TYPE (t), con);
3585                   return fold (build1 (NEGATE_EXPR, TREE_TYPE (t),
3586                                        convert (TREE_TYPE (t), con)));
3587                 }
3588               if (TREE_CONSTANT (arg1))
3589                 return t;
3590               if (varsign == -1)
3591                 TREE_SET_CODE (t,
3592                                (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
3593               TREE_OPERAND (t, 0)
3594                 = fold (build (code, TREE_TYPE (t), arg0, con));
3595               TREE_OPERAND (t, 1) = var;
3596               if (integer_zerop (TREE_OPERAND (t, 0))
3597                   && TREE_CODE (t) == PLUS_EXPR)
3598                 return convert (TREE_TYPE (t), var);
3599               return t;
3600             }
3601         }
3602     binary:
3603 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
3604       if (TREE_CODE (arg1) == REAL_CST)
3605         return t;
3606 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
3607       if (wins)
3608         t1 = const_binop (code, arg0, arg1, 0);
3609       if (t1 != NULL_TREE)
3610         {
3611           /* The return value should always have
3612              the same type as the original expression.  */
3613           TREE_TYPE (t1) = TREE_TYPE (t);
3614           return t1;
3615         }
3616       return t;
3617
3618     case MINUS_EXPR:
3619       if (! FLOAT_TYPE_P (type))
3620         {
3621           if (! wins && integer_zerop (arg0))
3622             return build1 (NEGATE_EXPR, type, arg1);
3623           if (integer_zerop (arg1))
3624             return non_lvalue (convert (type, arg0));
3625         }
3626       /* Convert A - (-B) to A + B.  */
3627       else if (TREE_CODE (arg1) == NEGATE_EXPR)
3628         return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
3629       else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3630         {
3631           /* Except with IEEE floating point, 0-x equals -x.  */
3632           if (! wins && real_zerop (arg0))
3633             return build1 (NEGATE_EXPR, type, arg1);
3634           /* Except with IEEE floating point, x-0 equals x.  */
3635           if (real_zerop (arg1))
3636             return non_lvalue (convert (type, arg0));
3637
3638           /* Fold &x - &x.  This can happen from &x.foo - &x. 
3639              This is unsafe for certain floats even in non-IEEE formats.
3640              In IEEE, it is unsafe because it does wrong for NaNs.
3641              Also note that operand_equal_p is always false if an operand
3642              is volatile.  */
3643
3644           if (operand_equal_p (arg0, arg1, FLOAT_TYPE_P (type)))
3645             return convert (type, integer_zero_node);
3646         }
3647       goto associate;
3648
3649     case MULT_EXPR:
3650       if (! FLOAT_TYPE_P (type))
3651         {
3652           if (integer_zerop (arg1))
3653             return omit_one_operand (type, arg1, arg0);
3654           if (integer_onep (arg1))
3655             return non_lvalue (convert (type, arg0));
3656
3657           /* (a * (1 << b)) is (a << b)  */
3658           if (TREE_CODE (arg1) == LSHIFT_EXPR
3659               && integer_onep (TREE_OPERAND (arg1, 0)))
3660             return fold (build (LSHIFT_EXPR, type, arg0,
3661                                 TREE_OPERAND (arg1, 1)));
3662           if (TREE_CODE (arg0) == LSHIFT_EXPR
3663               && integer_onep (TREE_OPERAND (arg0, 0)))
3664             return fold (build (LSHIFT_EXPR, type, arg1,
3665                                 TREE_OPERAND (arg0, 1)));
3666         }
3667       else
3668         {
3669           /* x*0 is 0, except for IEEE floating point.  */
3670           if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3671               && real_zerop (arg1))
3672             return omit_one_operand (type, arg1, arg0);
3673           /* In IEEE floating point, x*1 is not equivalent to x for snans.
3674              However, ANSI says we can drop signals,
3675              so we can do this anyway.  */
3676           if (real_onep (arg1))
3677             return non_lvalue (convert (type, arg0));
3678           /* x*2 is x+x */
3679           if (! wins && real_twop (arg1))
3680             {
3681               tree arg = save_expr (arg0);
3682               return build (PLUS_EXPR, type, arg, arg);
3683             }
3684         }
3685       goto associate;
3686
3687     case BIT_IOR_EXPR:
3688     bit_ior:
3689       if (integer_all_onesp (arg1))
3690         return omit_one_operand (type, arg1, arg0);
3691       if (integer_zerop (arg1))
3692         return non_lvalue (convert (type, arg0));
3693       t1 = distribute_bit_expr (code, type, arg0, arg1);
3694       if (t1 != NULL_TREE)
3695         return t1;
3696
3697       /* (a << C1) | (a >> C2) if A is unsigned and C1+C2 is the size of A
3698          is a rotate of A by C1 bits.  */
3699
3700       if ((TREE_CODE (arg0) == RSHIFT_EXPR
3701            || TREE_CODE (arg0) == LSHIFT_EXPR)
3702           && (TREE_CODE (arg1) == RSHIFT_EXPR
3703               || TREE_CODE (arg1) == LSHIFT_EXPR)
3704           && TREE_CODE (arg0) != TREE_CODE (arg1)
3705           && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0)
3706           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))
3707           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3708           && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3709           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
3710           && TREE_INT_CST_HIGH (TREE_OPERAND (arg1, 1)) == 0
3711           && ((TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
3712                + TREE_INT_CST_LOW (TREE_OPERAND (arg1, 1)))
3713               == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
3714         return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
3715                       TREE_CODE (arg0) == LSHIFT_EXPR
3716                       ? TREE_OPERAND (arg0, 1) : TREE_OPERAND (arg1, 1));
3717
3718       goto associate;
3719
3720     case BIT_XOR_EXPR:
3721       if (integer_zerop (arg1))
3722         return non_lvalue (convert (type, arg0));
3723       if (integer_all_onesp (arg1))
3724         return fold (build1 (BIT_NOT_EXPR, type, arg0));
3725       goto associate;
3726
3727     case BIT_AND_EXPR:
3728     bit_and:
3729       if (integer_all_onesp (arg1))
3730         return non_lvalue (convert (type, arg0));
3731       if (integer_zerop (arg1))
3732         return omit_one_operand (type, arg1, arg0);
3733       t1 = distribute_bit_expr (code, type, arg0, arg1);
3734       if (t1 != NULL_TREE)
3735         return t1;
3736       /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char.  */
3737       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
3738           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
3739         {
3740           int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
3741           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
3742               && (~TREE_INT_CST_LOW (arg0)
3743                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
3744             return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
3745         }
3746       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
3747           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
3748         {
3749           int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
3750           if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
3751               && (~TREE_INT_CST_LOW (arg1)
3752                   & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
3753             return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
3754         }
3755       goto associate;
3756
3757     case BIT_ANDTC_EXPR:
3758       if (integer_all_onesp (arg0))
3759         return non_lvalue (convert (type, arg1));
3760       if (integer_zerop (arg0))
3761         return omit_one_operand (type, arg0, arg1);
3762       if (TREE_CODE (arg1) == INTEGER_CST)
3763         {
3764           arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
3765           code = BIT_AND_EXPR;
3766           goto bit_and;
3767         }
3768       goto binary;
3769
3770     case TRUNC_DIV_EXPR:
3771     case ROUND_DIV_EXPR:
3772     case FLOOR_DIV_EXPR:
3773     case CEIL_DIV_EXPR:
3774     case EXACT_DIV_EXPR:
3775     case RDIV_EXPR:
3776       if (integer_onep (arg1))
3777         return non_lvalue (convert (type, arg0));
3778       if (integer_zerop (arg1))
3779         return t;
3780
3781       /* If we have ((a * C1) / C2) and C1 % C2 == 0, we can replace this with
3782          (a * (C1/C2).  Also look for when we have a SAVE_EXPR in
3783          between.  */
3784       if (TREE_CODE (arg1) == INTEGER_CST
3785           && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0
3786           && TREE_CODE (arg0) == MULT_EXPR
3787           && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3788           && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) > 0
3789           && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
3790           && 0 == (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
3791                    % TREE_INT_CST_LOW (arg1)))
3792         {
3793           tree new_op
3794             = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
3795                            / TREE_INT_CST_LOW (arg1), 0);
3796
3797           TREE_TYPE (new_op) = type;
3798           return build (MULT_EXPR, type, TREE_OPERAND (arg0, 0), new_op);
3799         }
3800
3801       else if (TREE_CODE (arg1) == INTEGER_CST
3802                && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0
3803                && TREE_CODE (arg0) == SAVE_EXPR
3804                && TREE_CODE (TREE_OPERAND (arg0, 0)) == MULT_EXPR
3805                && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3806                    == INTEGER_CST)
3807                && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3808                    > 0)
3809                && (TREE_INT_CST_HIGH (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3810                    == 0)
3811                && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3812                    % TREE_INT_CST_LOW (arg1)) == 0)
3813         {
3814           tree new_op
3815             = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
3816                            / TREE_INT_CST_LOW (arg1), 0);
3817           
3818           TREE_TYPE (new_op) = type;
3819           return build (MULT_EXPR, type,
3820                         TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), new_op);
3821         }
3822
3823 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3824 #ifndef REAL_INFINITY
3825       if (TREE_CODE (arg1) == REAL_CST
3826           && real_zerop (arg1))
3827         return t;
3828 #endif
3829 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3830
3831       goto binary;
3832
3833     case CEIL_MOD_EXPR:
3834     case FLOOR_MOD_EXPR:
3835     case ROUND_MOD_EXPR:
3836     case TRUNC_MOD_EXPR:
3837       if (integer_onep (arg1))
3838         return omit_one_operand (type, integer_zero_node, arg0);
3839       if (integer_zerop (arg1))
3840         return t;
3841       goto binary;
3842
3843     case LSHIFT_EXPR:
3844     case RSHIFT_EXPR:
3845     case LROTATE_EXPR:
3846     case RROTATE_EXPR:
3847       if (integer_zerop (arg1))
3848         return non_lvalue (convert (type, arg0));
3849       /* Since negative shift count is not well-defined,
3850          don't try to compute it in the compiler.  */
3851       if (tree_int_cst_lt (arg1, integer_zero_node))
3852         return t;
3853       goto binary;
3854
3855     case MIN_EXPR:
3856       if (operand_equal_p (arg0, arg1, 0))
3857         return arg0;
3858       if (INTEGRAL_TYPE_P (type)
3859           && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
3860         return omit_one_operand (type, arg1, arg0);
3861       goto associate;
3862
3863     case MAX_EXPR:
3864       if (operand_equal_p (arg0, arg1, 0))
3865         return arg0;
3866       if (INTEGRAL_TYPE_P (type)
3867           && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
3868         return omit_one_operand (type, arg1, arg0);
3869       goto associate;
3870
3871     case TRUTH_NOT_EXPR:
3872       /* Note that the operand of this must be an int
3873          and its values must be 0 or 1.
3874          ("true" is a fixed value perhaps depending on the language,
3875          but we don't handle values other than 1 correctly yet.)  */
3876       return invert_truthvalue (arg0);
3877
3878     case TRUTH_ANDIF_EXPR:
3879       /* Note that the operands of this must be ints
3880          and their values must be 0 or 1.
3881          ("true" is a fixed value perhaps depending on the language.)  */
3882       /* If first arg is constant zero, return it.  */
3883       if (integer_zerop (arg0))
3884         return arg0;
3885     case TRUTH_AND_EXPR:
3886       /* If either arg is constant true, drop it.  */
3887       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
3888         return non_lvalue (arg1);
3889       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
3890         return non_lvalue (arg0);
3891       /* If second arg is constant zero, result is zero, but first arg
3892          must be evaluated.  */
3893       if (integer_zerop (arg1))
3894         return omit_one_operand (type, arg1, arg0);
3895
3896     truth_andor:
3897       /* Check for the possibility of merging component references.  If our
3898          lhs is another similar operation, try to merge its rhs with our
3899          rhs.  Then try to merge our lhs and rhs.  */
3900       if (optimize)
3901         {
3902           if (TREE_CODE (arg0) == code)
3903             {
3904               tem = fold_truthop (code, type,
3905                                   TREE_OPERAND (arg0, 1), arg1);
3906               if (tem)
3907                 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
3908             }
3909
3910           tem = fold_truthop (code, type, arg0, arg1);
3911           if (tem)
3912             return tem;
3913         }
3914       return t;
3915
3916     case TRUTH_ORIF_EXPR:
3917       /* Note that the operands of this must be ints
3918          and their values must be 0 or true.
3919          ("true" is a fixed value perhaps depending on the language.)  */
3920       /* If first arg is constant true, return it.  */
3921       if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
3922         return arg0;
3923     case TRUTH_OR_EXPR:
3924       /* If either arg is constant zero, drop it.  */
3925       if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
3926         return non_lvalue (arg1);
3927       if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1))
3928         return non_lvalue (arg0);
3929       /* If second arg is constant true, result is true, but we must
3930          evaluate first arg.  */
3931       if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
3932         return omit_one_operand (type, arg1, arg0);
3933       goto truth_andor;
3934
3935     case TRUTH_XOR_EXPR:
3936       /* If either arg is constant zero, drop it.  */
3937       if (integer_zerop (arg0))
3938         return non_lvalue (arg1);
3939       if (integer_zerop (arg1))
3940         return non_lvalue (arg0);
3941       /* If either arg is constant true, this is a logical inversion.  */
3942       if (integer_onep (arg0))
3943         return non_lvalue (invert_truthvalue (arg1));
3944       if (integer_onep (arg1))
3945         return non_lvalue (invert_truthvalue (arg0));
3946       break;
3947
3948     case EQ_EXPR:
3949     case NE_EXPR:
3950     case LT_EXPR:
3951     case GT_EXPR:
3952     case LE_EXPR:
3953     case GE_EXPR:
3954       /* If one arg is a constant integer, put it last.  */
3955       if (TREE_CODE (arg0) == INTEGER_CST
3956           && TREE_CODE (arg1) != INTEGER_CST)
3957         {
3958           TREE_OPERAND (t, 0) = arg1;
3959           TREE_OPERAND (t, 1) = arg0;
3960           arg0 = TREE_OPERAND (t, 0);
3961           arg1 = TREE_OPERAND (t, 1);
3962           code = swap_tree_comparison (code);
3963           TREE_SET_CODE (t, code);
3964         }
3965
3966       /* Convert foo++ == CONST into ++foo == CONST + INCR.
3967          First, see if one arg is constant; find the constant arg
3968          and the other one.  */
3969       {
3970         tree constop = 0, varop;
3971         tree *constoploc;
3972
3973         if (TREE_CONSTANT (arg1))
3974           constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
3975         if (TREE_CONSTANT (arg0))
3976           constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
3977
3978         if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
3979           {
3980             /* This optimization is invalid for ordered comparisons
3981                if CONST+INCR overflows or if foo+incr might overflow.
3982                This optimization is invalid for floating point due to rounding.
3983                For pointer types we assume overflow doesn't happen.  */
3984             if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
3985                 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
3986                     && (code == EQ_EXPR || code == NE_EXPR)))
3987               {
3988                 tree newconst
3989                   = fold (build (PLUS_EXPR, TREE_TYPE (varop),
3990                                  constop, TREE_OPERAND (varop, 1)));
3991                 TREE_SET_CODE (varop, PREINCREMENT_EXPR);
3992                 *constoploc = newconst;
3993                 return t;
3994               }
3995           }
3996         else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
3997           {
3998             if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE
3999                 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
4000                     && (code == EQ_EXPR || code == NE_EXPR)))
4001               {
4002                 tree newconst
4003                   = fold (build (MINUS_EXPR, TREE_TYPE (varop),
4004                                  constop, TREE_OPERAND (varop, 1)));
4005                 TREE_SET_CODE (varop, PREDECREMENT_EXPR);
4006                 *constoploc = newconst;
4007                 return t;
4008               }
4009           }
4010       }
4011
4012       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
4013       if (TREE_CODE (arg1) == INTEGER_CST
4014           && TREE_CODE (arg0) != INTEGER_CST
4015           && ! tree_int_cst_lt (arg1, integer_one_node))
4016         {
4017           switch (TREE_CODE (t))
4018             {
4019             case GE_EXPR:
4020               code = GT_EXPR;
4021               TREE_SET_CODE (t, code);
4022               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4023               TREE_OPERAND (t, 1) = arg1;
4024               break;
4025
4026             case LT_EXPR:
4027               code = LE_EXPR;
4028               TREE_SET_CODE (t, code);
4029               arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
4030               TREE_OPERAND (t, 1) = arg1;
4031             }
4032         }
4033
4034       /* If this is an EQ or NE comparison with zero and ARG0 is
4035          (1 << foo) & bar, convert it to (bar >> foo) & 1.  Both require
4036          two operations, but the latter can be done in one less insn
4037          one machine that have only two-operand insns or on which a
4038          constant cannot be the first operand.  */
4039       if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
4040           && TREE_CODE (arg0) == BIT_AND_EXPR)
4041         {
4042           if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
4043               && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
4044             return
4045               fold (build (code, type,
4046                            build (BIT_AND_EXPR, TREE_TYPE (arg0),
4047                                   build (RSHIFT_EXPR,
4048                                          TREE_TYPE (TREE_OPERAND (arg0, 0)),
4049                                          TREE_OPERAND (arg0, 1),
4050                                          TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
4051                                   convert (TREE_TYPE (arg0),
4052                                            integer_one_node)),
4053                            arg1));
4054           else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
4055                    && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
4056             return
4057               fold (build (code, type,
4058                            build (BIT_AND_EXPR, TREE_TYPE (arg0),
4059                                   build (RSHIFT_EXPR,
4060                                          TREE_TYPE (TREE_OPERAND (arg0, 1)),
4061                                          TREE_OPERAND (arg0, 0),
4062                                          TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
4063                                   convert (TREE_TYPE (arg0),
4064                                            integer_one_node)),
4065                            arg1));
4066         }
4067
4068       /* If this is an NE comparison of zero with an AND of one, remove the
4069          comparison since the AND will give the correct value.  */
4070       if (code == NE_EXPR && integer_zerop (arg1)
4071           && TREE_CODE (arg0) == BIT_AND_EXPR
4072           && integer_onep (TREE_OPERAND (arg0, 1)))
4073         return convert (type, arg0);
4074
4075       /* If we have (A & C) == C where C is a power of 2, convert this into
4076          (A & C) != 0.  Similarly for NE_EXPR.  */
4077       if ((code == EQ_EXPR || code == NE_EXPR)
4078           && TREE_CODE (arg0) == BIT_AND_EXPR
4079           && integer_pow2p (TREE_OPERAND (arg0, 1))
4080           && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
4081         return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
4082                       arg0, integer_zero_node);
4083
4084       /* Simplify comparison of something with itself.  (For IEEE
4085          floating-point, we can only do some of these simplifications.)  */
4086       if (operand_equal_p (arg0, arg1, 0))
4087         {
4088           switch (code)
4089             {
4090             case EQ_EXPR:
4091             case GE_EXPR:
4092             case LE_EXPR:
4093               if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
4094                 {
4095                   t = build_int_2 (1, 0);
4096                   TREE_TYPE (t) = type;
4097                   return t;
4098                 }
4099               code = EQ_EXPR;
4100               TREE_SET_CODE (t, code);
4101               break;
4102
4103             case NE_EXPR:
4104               /* For NE, we can only do this simplification if integer.  */
4105               if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
4106                 break;
4107               /* ... fall through ... */
4108             case GT_EXPR:
4109             case LT_EXPR:
4110               t = build_int_2 (0, 0);
4111               TREE_TYPE (t) = type;
4112               return t;
4113             }
4114         }
4115
4116       /* An unsigned comparison against 0 can be simplified.  */
4117       if (integer_zerop (arg1)
4118           && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
4119               || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
4120           && TREE_UNSIGNED (TREE_TYPE (arg1)))
4121         {
4122           switch (TREE_CODE (t))
4123             {
4124             case GT_EXPR:
4125               code = NE_EXPR;
4126               TREE_SET_CODE (t, NE_EXPR);
4127               break;
4128             case LE_EXPR:
4129               code = EQ_EXPR;
4130               TREE_SET_CODE (t, EQ_EXPR);
4131               break;
4132             case GE_EXPR:
4133               return omit_one_operand (integer_type_node,
4134                                        integer_one_node, arg0);
4135             case LT_EXPR:
4136               return omit_one_operand (integer_type_node,
4137                                        integer_zero_node, arg0);
4138             }
4139         }
4140
4141       /* If we are comparing an expression that just has comparisons
4142          of two integer values, arithmetic expressions of those comparisons,
4143          and constants, we can simplify it.  There are only three cases
4144          to check: the two values can either be equal, the first can be
4145          greater, or the second can be greater.  Fold the expression for
4146          those three values.  Since each value must be 0 or 1, we have
4147          eight possibilities, each of which corresponds to the constant 0
4148          or 1 or one of the six possible comparisons.
4149
4150          This handles common cases like (a > b) == 0 but also handles
4151          expressions like  ((x > y) - (y > x)) > 0, which supposedly
4152          occur in macroized code.  */
4153
4154       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
4155         {
4156           tree cval1 = 0, cval2 = 0;
4157
4158           if (twoval_comparison_p (arg0, &cval1, &cval2)
4159               /* Don't handle degenerate cases here; they should already
4160                  have been handled anyway.  */
4161               && cval1 != 0 && cval2 != 0
4162               && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
4163               && TREE_TYPE (cval1) == TREE_TYPE (cval2)
4164               && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
4165               && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
4166                                     TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
4167             {
4168               tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
4169               tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
4170
4171               /* We can't just pass T to eval_subst in case cval1 or cval2
4172                  was the same as ARG1.  */
4173
4174               tree high_result
4175                 = fold (build (code, type,
4176                                eval_subst (arg0, cval1, maxval, cval2, minval),
4177                                arg1));
4178               tree equal_result
4179                 = fold (build (code, type,
4180                                eval_subst (arg0, cval1, maxval, cval2, maxval),
4181                                arg1));
4182               tree low_result
4183                 = fold (build (code, type,
4184                                eval_subst (arg0, cval1, minval, cval2, maxval),
4185                                arg1));
4186
4187               /* All three of these results should be 0 or 1.  Confirm they
4188                  are.  Then use those values to select the proper code
4189                  to use.  */
4190
4191               if ((integer_zerop (high_result)
4192                    || integer_onep (high_result))
4193                   && (integer_zerop (equal_result)
4194                       || integer_onep (equal_result))
4195                   && (integer_zerop (low_result)
4196                       || integer_onep (low_result)))
4197                 {
4198                   /* Make a 3-bit mask with the high-order bit being the
4199                      value for `>', the next for '=', and the low for '<'.  */
4200                   switch ((integer_onep (high_result) * 4)
4201                           + (integer_onep (equal_result) * 2)
4202                           + integer_onep (low_result))
4203                     {
4204                     case 0:
4205                       /* Always false.  */
4206                       return omit_one_operand (type, integer_zero_node, arg0);
4207                     case 1:
4208                       code = LT_EXPR;
4209                       break;
4210                     case 2:
4211                       code = EQ_EXPR;
4212                       break;
4213                     case 3:
4214                       code = LE_EXPR;
4215                       break;
4216                     case 4:
4217                       code = GT_EXPR;
4218                       break;
4219                     case 5:
4220                       code = NE_EXPR;
4221                       break;
4222                     case 6:
4223                       code = GE_EXPR;
4224                       break;
4225                     case 7:
4226                       /* Always true.  */
4227                       return omit_one_operand (type, integer_one_node, arg0);
4228                     }
4229
4230                   return fold (build (code, type, cval1, cval2));
4231                 }
4232             }
4233         }
4234
4235       /* If this is a comparison of a field, we may be able to simplify it.  */
4236       if ((TREE_CODE (arg0) == COMPONENT_REF
4237                 || TREE_CODE (arg0) == BIT_FIELD_REF)
4238                && (code == EQ_EXPR || code == NE_EXPR)
4239                /* Handle the constant case even without -O
4240                   to make sure the warnings are given.  */
4241                && (optimize || TREE_CODE (arg1) == INTEGER_CST))
4242         {
4243           t1 = optimize_bit_field_compare (code, type, arg0, arg1);
4244           return t1 ? t1 : t;
4245         }
4246
4247       /* From here on, the only cases we handle are when the result is
4248          known to be a constant.
4249
4250          To compute GT, swap the arguments and do LT.
4251          To compute GE, do LT and invert the result.
4252          To compute LE, swap the arguments, do LT and invert the result.
4253          To compute NE, do EQ and invert the result.
4254
4255          Therefore, the code below must handle only EQ and LT.  */
4256
4257       if (code == LE_EXPR || code == GT_EXPR)
4258         {
4259           tem = arg0, arg0 = arg1, arg1 = tem;
4260           code = swap_tree_comparison (code);
4261         }
4262
4263       /* Note that it is safe to invert for real values here because we
4264          will check below in the one case that it matters.  */
4265
4266       invert = 0;
4267       if (code == NE_EXPR || code == GE_EXPR)
4268         {
4269           invert = 1;
4270           code = invert_tree_comparison (code);
4271         }
4272
4273       /* Compute a result for LT or EQ if args permit;
4274          otherwise return T.  */
4275       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
4276         {
4277           if (code == EQ_EXPR)
4278             t1 = build_int_2 ((TREE_INT_CST_LOW (arg0)
4279                                == TREE_INT_CST_LOW (arg1))
4280                               && (TREE_INT_CST_HIGH (arg0)
4281                                   == TREE_INT_CST_HIGH (arg1)),
4282                               0);
4283           else
4284             t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
4285                                ? INT_CST_LT_UNSIGNED (arg0, arg1)
4286                                : INT_CST_LT (arg0, arg1)),
4287                               0);
4288         }
4289
4290       /* Assume a nonexplicit constant cannot equal an explicit one,
4291          since such code would be undefined anyway.
4292          Exception: on sysvr4, using #pragma weak,
4293          a label can come out as 0.  */
4294       else if (TREE_CODE (arg1) == INTEGER_CST
4295                && !integer_zerop (arg1)
4296                && TREE_CONSTANT (arg0)
4297                && TREE_CODE (arg0) == ADDR_EXPR
4298                && code == EQ_EXPR)
4299         t1 = build_int_2 (0, 0);
4300
4301       /* Two real constants can be compared explicitly.  */
4302       else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
4303         {
4304           /* If either operand is a NaN, the result is false with two
4305              exceptions: First, an NE_EXPR is true on NaNs, but that case
4306              is already handled correctly since we will be inverting the
4307              result for NE_EXPR.  Second, if we had inverted a LE_EXPR
4308              or a GE_EXPR into a LT_EXPR, we must return true so that it
4309              will be inverted into false.  */
4310
4311           if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
4312               || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
4313             t1 = build_int_2 (invert && code == LT_EXPR, 0);
4314
4315           else if (code == EQ_EXPR)
4316             t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
4317                                                  TREE_REAL_CST (arg1)),
4318                               0);
4319           else
4320             t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
4321                                                 TREE_REAL_CST (arg1)),
4322                               0);
4323         }
4324
4325       if (t1 == NULL_TREE)
4326         return t;
4327
4328       if (invert)
4329         TREE_INT_CST_LOW (t1) ^= 1;
4330
4331       TREE_TYPE (t1) = type;
4332       return t1;
4333
4334     case COND_EXPR:
4335       if (TREE_CODE (arg0) == INTEGER_CST)
4336         return TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
4337       else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
4338         return omit_one_operand (type, arg1, arg0);
4339
4340       /* If the second operand is zero, invert the comparison and swap
4341          the second and third operands.  Likewise if the second operand
4342          is constant and the third is not or if the third operand is
4343          equivalent to the first operand of the comparison.  */
4344
4345       if (integer_zerop (arg1)
4346           || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
4347           || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4348               && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
4349                                                  TREE_OPERAND (t, 2),
4350                                                  TREE_OPERAND (arg0, 1))))
4351         {
4352           /* See if this can be inverted.  If it can't, possibly because
4353              it was a floating-point inequality comparison, don't do
4354              anything.  */
4355           tem = invert_truthvalue (arg0);
4356
4357           if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
4358             {
4359               arg0 = TREE_OPERAND (t, 0) = tem;
4360               TREE_OPERAND (t, 1) = TREE_OPERAND (t, 2);
4361               TREE_OPERAND (t, 2) = arg1;
4362               arg1 = TREE_OPERAND (t, 1);
4363             }
4364         }
4365
4366       /* If we have A op B ? A : C, we may be able to convert this to a
4367          simpler expression, depending on the operation and the values
4368          of B and C.  IEEE floating point prevents this though,
4369          because A or B might be -0.0 or a NaN.  */
4370
4371       if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
4372           && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4373               || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
4374           && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
4375                                              arg1, TREE_OPERAND (arg0, 1)))
4376         {
4377           tree arg2 = TREE_OPERAND (t, 2);
4378           enum tree_code comp_code = TREE_CODE (arg0);
4379
4380           /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
4381              depending on the comparison operation.  */
4382           if (integer_zerop (TREE_OPERAND (arg0, 1))
4383               && TREE_CODE (arg2) == NEGATE_EXPR
4384               && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4385             switch (comp_code)
4386               {
4387               case EQ_EXPR:
4388                 return fold (build1 (NEGATE_EXPR, type, arg1));
4389               case NE_EXPR:
4390                 return convert (type, arg1);
4391               case GE_EXPR:
4392               case GT_EXPR:
4393                 return fold (build1 (ABS_EXPR, type, arg1));
4394               case LE_EXPR:
4395               case LT_EXPR:
4396                 return fold (build1 (NEGATE_EXPR, type,
4397                                      fold (build1 (ABS_EXPR, type, arg1))));
4398               }
4399
4400           /* If this is A != 0 ? A : 0, this is simply A.  For ==, it is
4401              always zero.  */
4402
4403           if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
4404             {
4405               if (comp_code == NE_EXPR)
4406                 return convert (type, arg1);
4407               else if (comp_code == EQ_EXPR)
4408                 return convert (type, integer_zero_node);
4409             }
4410
4411           /* If this is A op B ? A : B, this is either A, B, min (A, B),
4412              or max (A, B), depending on the operation.  */
4413
4414           if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
4415                                               arg2, TREE_OPERAND (arg0, 0)))
4416             switch (comp_code)
4417               {
4418               case EQ_EXPR:
4419                 return convert (type, arg2);
4420               case NE_EXPR:
4421                 return convert (type, arg1);
4422               case LE_EXPR:
4423               case LT_EXPR:
4424                 return fold (build (MIN_EXPR, type, arg1, arg2));
4425               case GE_EXPR:
4426               case GT_EXPR:
4427                 return fold (build (MAX_EXPR, type, arg1, arg2));
4428               }
4429
4430           /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4431              we might still be able to simplify this.  For example,
4432              if C1 is one less or one more than C2, this might have started
4433              out as a MIN or MAX and been transformed by this function.
4434              Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE.  */
4435
4436           if (INTEGRAL_TYPE_P (type)
4437               && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
4438               && TREE_CODE (arg2) == INTEGER_CST)
4439             switch (comp_code)
4440               {
4441               case EQ_EXPR:
4442                 /* We can replace A with C1 in this case.  */
4443                 arg1 = TREE_OPERAND (t, 1)
4444                   = convert (type, TREE_OPERAND (arg0, 1));
4445                 break;
4446
4447               case LT_EXPR:
4448                 /* If C1 is C2 + 1, this is min(A, C2).  */
4449                 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
4450                     && operand_equal_p (TREE_OPERAND (arg0, 1),
4451                                         const_binop (PLUS_EXPR, arg2,
4452                                                      integer_one_node, 0), 1))
4453                   return fold (build (MIN_EXPR, type, arg1, arg2));
4454                 break;
4455
4456               case LE_EXPR:
4457                 /* If C1 is C2 - 1, this is min(A, C2).  */
4458                 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
4459                     && operand_equal_p (TREE_OPERAND (arg0, 1),
4460                                         const_binop (MINUS_EXPR, arg2,
4461                                                      integer_one_node, 0), 1))
4462                   return fold (build (MIN_EXPR, type, arg1, arg2));
4463                 break;
4464
4465               case GT_EXPR:
4466                 /* If C1 is C2 - 1, this is max(A, C2).  */
4467                 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
4468                     && operand_equal_p (TREE_OPERAND (arg0, 1),
4469                                         const_binop (MINUS_EXPR, arg2,
4470                                                      integer_one_node, 0), 1))
4471                   return fold (build (MAX_EXPR, type, arg1, arg2));
4472                 break;
4473
4474               case GE_EXPR:
4475                 /* If C1 is C2 + 1, this is max(A, C2).  */
4476                 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
4477                     && operand_equal_p (TREE_OPERAND (arg0, 1),
4478                                         const_binop (PLUS_EXPR, arg2,
4479                                                      integer_one_node, 0), 1))
4480                   return fold (build (MAX_EXPR, type, arg1, arg2));
4481                 break;
4482               }
4483         }
4484
4485       /* Convert A ? 1 : 0 to simply A.  */
4486       if (integer_onep (TREE_OPERAND (t, 1))
4487           && integer_zerop (TREE_OPERAND (t, 2))
4488           /* If we try to convert TREE_OPERAND (t, 0) to our type, the
4489              call to fold will try to move the conversion inside 
4490              a COND, which will recurse.  In that case, the COND_EXPR
4491              is probably the best choice, so leave it alone.  */
4492           && type == TREE_TYPE (arg0))
4493         return arg0;
4494
4495
4496       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
4497          operation is simply A & 2.  */
4498
4499       if (integer_zerop (TREE_OPERAND (t, 2))
4500           && TREE_CODE (arg0) == NE_EXPR
4501           && integer_zerop (TREE_OPERAND (arg0, 1))
4502           && integer_pow2p (arg1)
4503           && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
4504           && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
4505                               arg1, 1))
4506         return convert (type, TREE_OPERAND (arg0, 0));
4507
4508       return t;
4509
4510     case COMPOUND_EXPR:
4511       /* When pedantic, a compound expression can be neither an lvalue
4512          nor an integer constant expression.  */
4513       if (TREE_SIDE_EFFECTS (arg0) || pedantic)
4514         return t;
4515       /* Don't let (0, 0) be null pointer constant.  */
4516       if (integer_zerop (arg1))
4517         return non_lvalue (arg1);
4518       return arg1;
4519
4520     case COMPLEX_EXPR:
4521       if (wins)
4522         return build_complex (arg0, arg1);
4523       return t;
4524
4525     case REALPART_EXPR:
4526       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4527         return t;
4528       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4529         return omit_one_operand (type, TREE_OPERAND (arg0, 0),
4530                                  TREE_OPERAND (arg0, 1));
4531       else if (TREE_CODE (arg0) == COMPLEX_CST)
4532         return TREE_REALPART (arg0);
4533       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4534         return fold (build (TREE_CODE (arg0), type,
4535                             fold (build1 (REALPART_EXPR, type,
4536                                           TREE_OPERAND (arg0, 0))),
4537                             fold (build1 (REALPART_EXPR,
4538                                           type, TREE_OPERAND (arg0, 1)))));
4539       return t;
4540
4541     case IMAGPART_EXPR:
4542       if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
4543         return convert (type, integer_zero_node);
4544       else if (TREE_CODE (arg0) == COMPLEX_EXPR)
4545         return omit_one_operand (type, TREE_OPERAND (arg0, 1),
4546                                  TREE_OPERAND (arg0, 0));
4547       else if (TREE_CODE (arg0) == COMPLEX_CST)
4548         return TREE_IMAGPART (arg0);
4549       else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
4550         return fold (build (TREE_CODE (arg0), type,
4551                             fold (build1 (IMAGPART_EXPR, type,
4552                                           TREE_OPERAND (arg0, 0))),
4553                             fold (build1 (IMAGPART_EXPR, type,
4554                                           TREE_OPERAND (arg0, 1)))));
4555       return t;
4556
4557     default:
4558       return t;
4559     } /* switch (code) */
4560 }