OSDN Git Service

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