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