]>
gcc.gnu.org Git - gcc.git/blob - gcc/fold-const.c
dd269861d556f34b930f56c3902b47a650cb46ea
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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. */
20 /*@@ Fix lossage on folding division of big integers. */
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. */
31 /* The entry points in this file are fold, size_int and size_binop.
33 fold takes a tree as argument and returns a simplified tree.
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'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'. */
48 void lshift_double ();
49 void rshift_double ();
50 void lrotate_double ();
51 void rrotate_double ();
52 static tree
const_binop ();
54 /* To do constant folding on INTEGER_CST nodes requires 64-bit arithmetic.
55 We do that by representing the 64-bit integer as 8 shorts,
56 with only 8 bits stored in each short, as a positive number. */
58 /* Unpack a 64-bit integer into 8 shorts.
59 LOW and HI are the integer, as two `int' pieces.
60 SHORTS points to the array of shorts. */
63 encode (shorts
, low
, hi
)
67 shorts
[0] = low
& 0xff;
68 shorts
[1] = (low
>> 8) & 0xff;
69 shorts
[2] = (low
>> 16) & 0xff;
70 shorts
[3] = (low
>> 24) & 0xff;
71 shorts
[4] = hi
& 0xff;
72 shorts
[5] = (hi
>> 8) & 0xff;
73 shorts
[6] = (hi
>> 16) & 0xff;
74 shorts
[7] = (hi
>> 24) & 0xff;
77 /* Pack an array of 8 shorts into a 64-bit integer.
78 SHORTS points to the array of shorts.
79 The integer is stored into *LOW and *HI as two `int' pieces. */
82 decode (shorts
, low
, hi
)
86 /* The casts in the following statement should not be
87 needed, but they get around bugs in some C compilers. */
88 *low
= (((long)shorts
[3] << 24) | ((long)shorts
[2] << 16)
89 | ((long)shorts
[1] << 8) | (long)shorts
[0]);
90 *hi
= (((long)shorts
[7] << 24) | ((long)shorts
[6] << 16)
91 | ((long)shorts
[5] << 8) | (long)shorts
[4]);
94 /* Make the integer constant T valid for its type
95 by setting to 0 or 1 all the bits in the constant
96 that don't belong in the type. */
102 register int prec
= TYPE_PRECISION (TREE_TYPE (t
));
104 if (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
)
107 /* First clear all bits that are beyond the type's precision. */
109 if (prec
== 2 * HOST_BITS_PER_INT
)
111 else if (prec
> HOST_BITS_PER_INT
)
113 TREE_INT_CST_HIGH (t
)
114 &= ~((-1) << (prec
- HOST_BITS_PER_INT
));
118 TREE_INT_CST_HIGH (t
) = 0;
119 if (prec
< HOST_BITS_PER_INT
)
124 /* If it's a signed type and value's sign bit is set, extend the sign. */
126 if (! TREE_UNSIGNED (TREE_TYPE (t
))
127 && prec
!= 2 * HOST_BITS_PER_INT
128 && (prec
> HOST_BITS_PER_INT
129 ? TREE_INT_CST_HIGH (t
) & (1 << (prec
- HOST_BITS_PER_INT
- 1))
130 : TREE_INT_CST_LOW (t
) & (1 << (prec
- 1))))
132 /* Value is negative:
133 set to 1 all the bits that are outside this type's precision. */
134 if (prec
> HOST_BITS_PER_INT
)
136 TREE_INT_CST_HIGH (t
)
137 |= ((-1) << (prec
- HOST_BITS_PER_INT
));
141 TREE_INT_CST_HIGH (t
) = -1;
142 if (prec
< HOST_BITS_PER_INT
)
149 /* Add two 64-bit integers with 64-bit result.
150 Each argument is given as two `int' pieces.
151 One argument is L1 and H1; the other, L2 and H2.
152 The value is stored as two `int' pieces in *LV and *HV.
153 We use the 8-shorts representation internally. */
156 add_double (l1
, h1
, l2
, h2
, lv
, hv
)
162 register int carry
= 0;
165 encode (arg1
, l1
, h1
);
166 encode (arg2
, l2
, h2
);
168 for (i
= 0; i
< 8; i
++)
170 carry
+= arg1
[i
] + arg2
[i
];
171 arg1
[i
] = carry
& 0xff;
175 decode (arg1
, lv
, hv
);
178 /* Negate a 64-bit integers with 64-bit result.
179 The argument is given as two `int' pieces in L1 and H1.
180 The value is stored as two `int' pieces in *LV and *HV.
181 We use the 8-shorts representation internally. */
184 neg_double (l1
, h1
, lv
, hv
)
200 /* Multiply two 64-bit integers with 64-bit result.
201 Each argument is given as two `int' pieces.
202 One argument is L1 and H1; the other, L2 and H2.
203 The value is stored as two `int' pieces in *LV and *HV.
204 We use the 8-shorts representation internally. */
207 mul_double (l1
, h1
, l2
, h2
, lv
, hv
)
214 register int carry
= 0;
215 register int i
, j
, k
;
217 /* These two cases are used extensively, arising from pointer
223 unsigned temp
= l1
+ l1
;
224 *hv
= h1
* 2 + (temp
< l1
);
230 unsigned temp
= l1
+ l1
;
231 h1
= h1
* 4 + ((temp
< l1
) << 1);
241 unsigned temp
= l1
+ l1
;
242 h1
= h1
* 8 + ((temp
< l1
) << 2);
245 h1
+= (temp
< l1
) << 1;
255 encode (arg1
, l1
, h1
);
256 encode (arg2
, l2
, h2
);
258 bzero (prod
, sizeof prod
);
260 for (i
= 0; i
< 8; i
++)
261 for (j
= 0; j
< 8; j
++)
264 carry
= arg1
[i
] * arg2
[j
];
268 prod
[k
] = carry
& 0xff;
274 decode (prod
, lv
, hv
); /* @@decode ignores prod[8] -> prod[15] */
277 /* Shift the 64-bit integer in L1, H1 left by COUNT places
278 keeping only PREC bits of result.
279 Shift right if COUNT is negative.
280 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
281 Store the value as two `int' pieces in *LV and *HV. */
284 lshift_double (l1
, h1
, count
, prec
, lv
, hv
, arith
)
285 int l1
, h1
, count
, prec
;
295 rshift_double (l1
, h1
, - count
, prec
, lv
, hv
, arith
);
299 encode (arg1
, l1
, h1
);
307 for (i
= 0; i
< 8; i
++)
309 carry
+= arg1
[i
] << 1;
310 arg1
[i
] = carry
& 0xff;
316 decode (arg1
, lv
, hv
);
319 /* Shift the 64-bit integer in L1, H1 right by COUNT places
320 keeping only PREC bits of result. COUNT must be positive.
321 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
322 Store the value as two `int' pieces in *LV and *HV. */
325 rshift_double (l1
, h1
, count
, prec
, lv
, hv
, arith
)
326 int l1
, h1
, count
, prec
;
334 encode (arg1
, l1
, h1
);
341 carry
= arith
&& arg1
[7] >> 7;
342 for (i
= 7; i
>= 0; i
--)
346 arg1
[i
] = (carry
>> 1) & 0xff;
351 decode (arg1
, lv
, hv
);
354 /* Rotate the 64-bit integer in L1, H1 left by COUNT places
355 keeping only PREC bits of result.
356 Rotate right if COUNT is negative.
357 Store the value as two `int' pieces in *LV and *HV. */
360 lrotate_double (l1
, h1
, count
, prec
, lv
, hv
)
361 int l1
, h1
, count
, prec
;
370 rrotate_double (l1
, h1
, - count
, prec
, lv
, hv
);
374 encode (arg1
, l1
, h1
);
379 carry
= arg1
[7] >> 7;
382 for (i
= 0; i
< 8; i
++)
384 carry
+= arg1
[i
] << 1;
385 arg1
[i
] = carry
& 0xff;
391 decode (arg1
, lv
, hv
);
394 /* Rotate the 64-bit integer in L1, H1 left by COUNT places
395 keeping only PREC bits of result. COUNT must be positive.
396 Store the value as two `int' pieces in *LV and *HV. */
399 rrotate_double (l1
, h1
, count
, prec
, lv
, hv
)
400 int l1
, h1
, count
, prec
;
407 encode (arg1
, l1
, h1
);
415 for (i
= 7; i
>= 0; i
--)
419 arg1
[i
] = (carry
>> 1) & 0xff;
424 decode (arg1
, lv
, hv
);
427 /* Divide 64 bit integer LNUM, HNUM by 64 bit integer LDEN, HDEN
428 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
429 CODE is a tree code for a kind of division, one of
430 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
432 It controls how the quotient is rounded to a integer.
433 UNS nonzero says do unsigned division. */
436 div_and_round_double (code
, uns
,
437 lnum_orig
, hnum_orig
, lden_orig
, hden_orig
,
438 lquo
, hquo
, lrem
, hrem
)
441 int lnum_orig
, hnum_orig
; /* num == numerator == dividend */
442 int lden_orig
, hden_orig
; /* den == denominator == divisor */
443 int *lquo
, *hquo
, *lrem
, *hrem
;
446 short num
[9], den
[8], quo
[8]; /* extra element for scaling. */
447 register int i
, j
, work
;
448 register int carry
= 0;
449 unsigned int lnum
= lnum_orig
;
450 int hnum
= hnum_orig
;
451 unsigned int lden
= lden_orig
;
452 int hden
= hden_orig
;
454 if ((hden
== 0) && (lden
== 0))
457 /* calculate quotient sign and convert operands to unsigned. */
463 neg_double (lden
, hden
, &lden
, &hden
);
468 neg_double (lnum
, hnum
, &lnum
, &hnum
);
472 if (hnum
== 0 && hden
== 0)
473 { /* single precision */
475 *lquo
= lnum
/ lden
; /* rounds toward zero since positive args */
480 { /* trivial case: dividend < divisor */
481 /* hden != 0 already checked. */
488 bzero (quo
, sizeof quo
);
490 bzero (num
, sizeof num
); /* to zero 9th element */
491 bzero (den
, sizeof den
);
493 encode (num
, lnum
, hnum
);
494 encode (den
, lden
, hden
);
496 /* This code requires more than just hden == 0.
497 We also have to require that we don't need more than three bytes
498 to hold CARRY. If we ever did need four bytes to hold it, we
499 would lose part of it when computing WORK on the next round. */
500 if (hden
== 0 && ((lden
<< 8) >> 8) == lden
)
501 { /* simpler algorithm */
502 /* hnum != 0 already checked. */
503 for (i
= 7; i
>= 0; i
--)
505 work
= num
[i
] + (carry
<< 8);
506 quo
[i
] = work
/ lden
;
510 else { /* full double precision,
511 with thanks to Don Knuth's
512 "Seminumerical Algorithms". */
514 int quo_est
, scale
, num_hi_sig
, den_hi_sig
, quo_hi_sig
;
516 /* Find the highest non-zero divisor digit. */
527 quo_hi_sig
= num_hi_sig
- den_hi_sig
+ 1;
529 /* Insure that the first digit of the divisor is at least BASE/2.
530 This is required by the quotient digit estimation algorithm. */
532 scale
= BASE
/ (den
[den_hi_sig
] + 1);
533 if (scale
> 1) { /* scale divisor and dividend */
535 for (i
= 0; i
<= 8; i
++) {
536 work
= (num
[i
] * scale
) + carry
;
537 num
[i
] = work
& 0xff;
539 if (num
[i
] != 0) num_hi_sig
= i
;
542 for (i
= 0; i
<= 7; i
++) {
543 work
= (den
[i
] * scale
) + carry
;
544 den
[i
] = work
& 0xff;
546 if (den
[i
] != 0) den_hi_sig
= i
;
551 for (i
= quo_hi_sig
; i
> 0; i
--) {
552 /* guess the next quotient digit, quo_est, by dividing the first
553 two remaining dividend digits by the high order quotient digit.
554 quo_est is never low and is at most 2 high. */
556 int num_hi
; /* index of highest remaining dividend digit */
558 num_hi
= i
+ den_hi_sig
;
560 work
= (num
[num_hi
] * BASE
) + (num_hi
> 0 ? num
[num_hi
- 1] : 0);
561 if (num
[num_hi
] != den
[den_hi_sig
]) {
562 quo_est
= work
/ den
[den_hi_sig
];
568 /* refine quo_est so it's usually correct, and at most one high. */
569 while ((den
[den_hi_sig
- 1] * quo_est
)
570 > (((work
- (quo_est
* den
[den_hi_sig
])) * BASE
)
571 + ((num_hi
- 1) > 0 ? num
[num_hi
- 2] : 0)))
574 /* Try QUO_EST as the quotient digit, by multiplying the
575 divisor by QUO_EST and subtracting from the remaining dividend.
576 Keep in mind that QUO_EST is the I - 1st digit. */
580 for (j
= 0; j
<= den_hi_sig
; j
++)
584 work
= num
[i
+ j
- 1] - (quo_est
* den
[j
]) + carry
;
592 num
[i
+ j
- 1] = digit
;
595 /* if quo_est was high by one, then num[i] went negative and
596 we need to correct things. */
601 carry
= 0; /* add divisor back in */
602 for (j
= 0; j
<= den_hi_sig
; j
++)
604 work
= num
[i
+ j
- 1] + den
[j
] + carry
;
614 num
[i
+ j
- 1] = work
;
616 num
[num_hi
] += carry
;
619 /* store the quotient digit. */
620 quo
[i
- 1] = quo_est
;
624 decode (quo
, lquo
, hquo
);
627 /* if result is negative, make it so. */
629 neg_double (*lquo
, *hquo
, lquo
, hquo
);
631 /* compute trial remainder: rem = num - (quo * den) */
632 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
633 neg_double (*lrem
, *hrem
, lrem
, hrem
);
634 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
639 case TRUNC_MOD_EXPR
: /* round toward zero */
640 case EXACT_DIV_EXPR
: /* for this one, it shouldn't matter */
644 case FLOOR_MOD_EXPR
: /* round toward negative infinity */
645 if (quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio < 0 && rem != 0 */
648 add_double (*lquo
, *hquo
, -1, -1, lquo
, hquo
);
654 case CEIL_MOD_EXPR
: /* round toward positive infinity */
655 if (!quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio > 0 && rem != 0 */
657 add_double (*lquo
, *hquo
, 1, 0, lquo
, hquo
);
663 case ROUND_MOD_EXPR
: /* round to closest integer */
665 int labs_rem
= *lrem
, habs_rem
= *hrem
;
666 int labs_den
= lden
, habs_den
= hden
, ltwice
, htwice
;
668 /* get absolute values */
669 if (*hrem
< 0) neg_double (*lrem
, *hrem
, &labs_rem
, &habs_rem
);
670 if (hden
< 0) neg_double (lden
, hden
, &labs_den
, &habs_den
);
672 /* if (2 * abs (lrem) >= abs (lden)) */
673 mul_double (2, 0, labs_rem
, habs_rem
, <wice
, &htwice
);
674 if (((unsigned) habs_den
< (unsigned) htwice
)
675 || (((unsigned) habs_den
== (unsigned) htwice
)
676 && ((unsigned) labs_den
< (unsigned) ltwice
)))
680 add_double (*lquo
, *hquo
, -1, -1, lquo
, hquo
);
683 add_double (*lquo
, *hquo
, 1, 0, lquo
, hquo
);
693 /* compute true remainder: rem = num - (quo * den) */
694 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
695 neg_double (*lrem
, *hrem
, lrem
, hrem
);
696 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
699 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
701 /* Check for infinity in an IEEE double precision number. */
707 /* The IEEE 64-bit double format. */
712 unsigned exponent
: 11;
713 unsigned mantissa1
: 20;
718 unsigned mantissa1
: 20;
719 unsigned exponent
: 11;
725 if (u
.big_endian
.sign
== 1)
728 return (u
.big_endian
.exponent
== 2047
729 && u
.big_endian
.mantissa1
== 0
730 && u
.big_endian
.mantissa2
== 0);
735 return (u
.little_endian
.exponent
== 2047
736 && u
.little_endian
.mantissa1
== 0
737 && u
.little_endian
.mantissa2
== 0);
741 /* Check whether an IEEE double precision number is a NaN. */
747 /* The IEEE 64-bit double format. */
752 unsigned exponent
: 11;
753 unsigned mantissa1
: 20;
758 unsigned mantissa1
: 20;
759 unsigned exponent
: 11;
765 if (u
.big_endian
.sign
== 1)
768 return (u
.big_endian
.exponent
== 2047
769 && (u
.big_endian
.mantissa1
!= 0
770 || u
.big_endian
.mantissa2
!= 0));
775 return (u
.little_endian
.exponent
== 2047
776 && (u
.little_endian
.mantissa1
!= 0
777 || u
.little_endian
.mantissa2
!= 0));
781 /* Check for a negative IEEE double precision number. */
787 /* The IEEE 64-bit double format. */
792 unsigned exponent
: 11;
793 unsigned mantissa1
: 20;
798 unsigned mantissa1
: 20;
799 unsigned exponent
: 11;
805 if (u
.big_endian
.sign
== 1)
808 return u
.big_endian
.sign
;
813 return u
.little_endian
.sign
;
816 #else /* Target not IEEE */
818 /* Let's assume other float formats don't have infinity.
819 (This can be overridden by redefining REAL_VALUE_ISINF.) */
827 /* Let's assume other float formats don't have NaNs.
828 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
836 /* Let's assume other float formats don't have minus zero.
837 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
844 #endif /* Target not IEEE */
846 /* Split a tree IN into a constant and a variable part
847 that could be combined with CODE to make IN.
848 CODE must be a commutative arithmetic operation.
849 Store the constant part into *CONP and the variable in &VARP.
850 Return 1 if this was done; zero means the tree IN did not decompose
853 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
854 Therefore, we must tell the caller whether the variable part
855 was subtracted. We do this by storing 1 or -1 into *VARSIGNP.
856 The value stored is the coefficient for the variable term.
857 The constant term we return should always be added;
858 we negate it if necessary. */
861 split_tree (in
, code
, varp
, conp
, varsignp
)
867 register tree outtype
= TREE_TYPE (in
);
871 /* Strip any conversions that don't change the machine mode. */
872 while ((TREE_CODE (in
) == NOP_EXPR
873 || TREE_CODE (in
) == CONVERT_EXPR
)
874 && (TYPE_MODE (TREE_TYPE (in
))
875 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in
, 0)))))
876 in
= TREE_OPERAND (in
, 0);
878 if (TREE_CODE (in
) == code
879 || (TREE_CODE (TREE_TYPE (in
)) != REAL_TYPE
880 /* We can associate addition and subtraction together
881 (even though the C standard doesn't say so)
882 for integers because the value is not affected.
883 For reals, the value might be affected, so we can't. */
885 ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
886 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
888 enum tree_code code
= TREE_CODE (TREE_OPERAND (in
, 0));
889 if (code
== INTEGER_CST
)
891 *conp
= TREE_OPERAND (in
, 0);
892 *varp
= TREE_OPERAND (in
, 1);
893 if (TYPE_MODE (TREE_TYPE (*varp
)) != TYPE_MODE (outtype
)
894 && TREE_TYPE (*varp
) != outtype
)
895 *varp
= convert (outtype
, *varp
);
896 *varsignp
= (TREE_CODE (in
) == MINUS_EXPR
) ? -1 : 1;
899 if (TREE_CONSTANT (TREE_OPERAND (in
, 1)))
901 *conp
= TREE_OPERAND (in
, 1);
902 *varp
= TREE_OPERAND (in
, 0);
904 if (TYPE_MODE (TREE_TYPE (*varp
)) != TYPE_MODE (outtype
)
905 && TREE_TYPE (*varp
) != outtype
)
906 *varp
= convert (outtype
, *varp
);
907 if (TREE_CODE (in
) == MINUS_EXPR
)
909 /* If operation is subtraction and constant is second,
910 must negate it to get an additive constant.
911 And this cannot be done unless it is a manifest constant.
912 It could also be the address of a static variable.
913 We cannot negate that, so give up. */
914 if (TREE_CODE (*conp
) == INTEGER_CST
)
915 /* Subtracting from integer_zero_node loses for long long. */
916 *conp
= fold (build1 (NEGATE_EXPR
, TREE_TYPE (*conp
), *conp
));
922 if (TREE_CONSTANT (TREE_OPERAND (in
, 0)))
924 *conp
= TREE_OPERAND (in
, 0);
925 *varp
= TREE_OPERAND (in
, 1);
926 if (TYPE_MODE (TREE_TYPE (*varp
)) != TYPE_MODE (outtype
)
927 && TREE_TYPE (*varp
) != outtype
)
928 *varp
= convert (outtype
, *varp
);
929 *varsignp
= (TREE_CODE (in
) == MINUS_EXPR
) ? -1 : 1;
936 /* Combine two constants NUM and ARG2 under operation CODE
937 to produce a new constant.
938 We assume ARG1 and ARG2 have the same data type,
939 or at least are the same kind of constant and the same machine mode. */
941 /* Handle floating overflow for `const_binop'. */
942 static jmp_buf const_binop_error
;
945 const_binop (code
, arg1
, arg2
)
947 register tree arg1
, arg2
;
949 if (TREE_CODE (arg1
) == INTEGER_CST
)
951 register int int1l
= TREE_INT_CST_LOW (arg1
);
952 register int int1h
= TREE_INT_CST_HIGH (arg1
);
953 int int2l
= TREE_INT_CST_LOW (arg2
);
954 int int2h
= TREE_INT_CST_HIGH (arg2
);
956 int garbagel
, garbageh
;
958 int uns
= TREE_UNSIGNED (TREE_TYPE (arg1
));
963 t
= build_int_2 (int1l
| int2l
, int1h
| int2h
);
967 t
= build_int_2 (int1l
^ int2l
, int1h
^ int2h
);
971 t
= build_int_2 (int1l
& int2l
, int1h
& int2h
);
975 t
= build_int_2 (int1l
& ~int2l
, int1h
& ~int2h
);
981 lshift_double (int1l
, int1h
, int2l
,
982 TYPE_PRECISION (TREE_TYPE (arg1
)),
985 t
= build_int_2 (low
, hi
);
991 lrotate_double (int1l
, int1h
, int2l
,
992 TYPE_PRECISION (TREE_TYPE (arg1
)),
994 t
= build_int_2 (low
, hi
);
1001 if ((unsigned) int2l
< int1l
)
1003 t
= build_int_2 (int2l
, int2h
);
1009 if ((unsigned) int1l
< int2l
)
1011 t
= build_int_2 (int1l
, int1h
);
1014 add_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1015 t
= build_int_2 (low
, hi
);
1019 if (int2h
== 0 && int2l
== 0)
1021 t
= build_int_2 (int1l
, int1h
);
1024 neg_double (int2l
, int2h
, &int2l
, &int2h
);
1025 add_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1026 t
= build_int_2 (low
, hi
);
1030 /* Optimize simple cases. */
1038 t
= build_int_2 (0, 0);
1041 t
= build_int_2 (int2l
, int2h
);
1044 temp
= int2l
+ int2l
;
1045 int2h
= int2h
* 2 + (temp
< int2l
);
1046 t
= build_int_2 (temp
, int2h
);
1049 temp
= int2l
+ int2l
+ int2l
;
1050 int2h
= int2h
* 3 + (temp
< int2l
);
1051 t
= build_int_2 (temp
, int2h
);
1054 temp
= int2l
+ int2l
;
1055 int2h
= int2h
* 4 + ((temp
< int2l
) << 1);
1058 int2h
+= (temp
< int2l
);
1059 t
= build_int_2 (temp
, int2h
);
1062 temp
= int2l
+ int2l
;
1063 int2h
= int2h
* 8 + ((temp
< int2l
) << 2);
1066 int2h
+= (temp
< int2l
) << 1;
1069 int2h
+= (temp
< int2l
);
1070 t
= build_int_2 (temp
, int2h
);
1081 t
= build_int_2 (0, 0);
1086 t
= build_int_2 (int1l
, int1h
);
1091 mul_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1092 t
= build_int_2 (low
, hi
);
1095 case TRUNC_DIV_EXPR
:
1096 case FLOOR_DIV_EXPR
: case CEIL_DIV_EXPR
:
1097 case EXACT_DIV_EXPR
:
1098 /* This is a shortcut for a common special case.
1099 It reduces the number of tree nodes generated
1101 if (int2h
== 0 && int2l
> 0
1102 && TREE_TYPE (arg1
) == sizetype
1103 && int1h
== 0 && int1l
>= 0)
1105 if (code
== CEIL_DIV_EXPR
)
1107 return size_int (int1l
/ int2l
);
1109 case ROUND_DIV_EXPR
:
1110 if (int2h
== 0 && int2l
== 1)
1112 t
= build_int_2 (int1l
, int1h
);
1115 if (int1l
== int2l
&& int1h
== int2h
)
1117 if ((int1l
| int1h
) == 0)
1119 t
= build_int_2 (1, 0);
1122 div_and_round_double (code
, uns
, int1l
, int1h
, int2l
, int2h
,
1123 &low
, &hi
, &garbagel
, &garbageh
);
1124 t
= build_int_2 (low
, hi
);
1127 case TRUNC_MOD_EXPR
: case ROUND_MOD_EXPR
:
1128 case FLOOR_MOD_EXPR
: case CEIL_MOD_EXPR
:
1129 div_and_round_double (code
, uns
, int1l
, int1h
, int2l
, int2h
,
1130 &garbagel
, &garbageh
, &low
, &hi
);
1131 t
= build_int_2 (low
, hi
);
1138 low
= (((unsigned) int1h
< (unsigned) int2h
)
1139 || (((unsigned) int1h
== (unsigned) int2h
)
1140 && ((unsigned) int1l
< (unsigned) int2l
)));
1144 low
= ((int1h
< int2h
)
1145 || ((int1h
== int2h
)
1146 && ((unsigned) int1l
< (unsigned) int2l
)));
1148 if (low
== (code
== MIN_EXPR
))
1149 t
= build_int_2 (int1l
, int1h
);
1151 t
= build_int_2 (int2l
, int2h
);
1158 TREE_TYPE (t
) = TREE_TYPE (arg1
);
1162 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1163 if (TREE_CODE (arg1
) == REAL_CST
)
1165 register REAL_VALUE_TYPE d1
;
1166 register REAL_VALUE_TYPE d2
;
1167 register REAL_VALUE_TYPE value
;
1169 d1
= TREE_REAL_CST (arg1
);
1170 d2
= TREE_REAL_CST (arg2
);
1171 if (setjmp (const_binop_error
))
1173 warning ("floating overflow in constant folding");
1174 return build (code
, TREE_TYPE (arg1
), arg1
, arg2
);
1176 set_float_handler (const_binop_error
);
1178 #ifdef REAL_ARITHMETIC
1179 REAL_ARITHMETIC (value
, code
, d1
, d2
);
1196 #ifndef REAL_INFINITY
1205 value
= MIN (d1
, d2
);
1209 value
= MAX (d1
, d2
);
1215 #endif /* no REAL_ARITHMETIC */
1216 set_float_handler (0);
1217 return build_real (TREE_TYPE (arg1
),
1218 REAL_VALUE_TRUNCATE (TYPE_MODE (TREE_TYPE (arg1
)), value
));
1220 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1221 if (TREE_CODE (arg1
) == COMPLEX_CST
)
1223 register tree r1
= TREE_REALPART (arg1
);
1224 register tree i1
= TREE_IMAGPART (arg1
);
1225 register tree r2
= TREE_REALPART (arg2
);
1226 register tree i2
= TREE_IMAGPART (arg2
);
1232 t
= build_complex (const_binop (PLUS_EXPR
, r1
, r2
),
1233 const_binop (PLUS_EXPR
, i1
, i2
));
1237 t
= build_complex (const_binop (MINUS_EXPR
, r1
, r2
),
1238 const_binop (MINUS_EXPR
, i1
, i2
));
1242 t
= build_complex (const_binop (MINUS_EXPR
,
1243 const_binop (MULT_EXPR
, r1
, r2
),
1244 const_binop (MULT_EXPR
, i1
, i2
)),
1245 const_binop (PLUS_EXPR
,
1246 const_binop (MULT_EXPR
, r1
, i2
),
1247 const_binop (MULT_EXPR
, i1
, r2
)));
1252 register tree magsquared
1253 = const_binop (PLUS_EXPR
,
1254 const_binop (MULT_EXPR
, r2
, r2
),
1255 const_binop (MULT_EXPR
, i2
, i2
));
1256 t
= build_complex (const_binop (RDIV_EXPR
,
1257 const_binop (PLUS_EXPR
,
1258 const_binop (MULT_EXPR
, r1
, r2
),
1259 const_binop (MULT_EXPR
, i1
, i2
)),
1261 const_binop (RDIV_EXPR
,
1262 const_binop (MINUS_EXPR
,
1263 const_binop (MULT_EXPR
, i1
, r2
),
1264 const_binop (MULT_EXPR
, r1
, i2
)),
1272 TREE_TYPE (t
) = TREE_TYPE (arg1
);
1278 /* Return an INTEGER_CST with value V and type from `sizetype'. */
1282 unsigned int number
;
1285 /* Type-size nodes already made for small sizes. */
1286 static tree size_table
[2*HOST_BITS_PER_INT
+1];
1288 if (number
>= 0 && number
< 2*HOST_BITS_PER_INT
+1 && size_table
[number
] != 0)
1289 return size_table
[number
];
1290 if (number
>= 0 && number
< 2*HOST_BITS_PER_INT
+1)
1292 push_obstacks_nochange ();
1293 /* Make this a permanent node. */
1294 end_temporary_allocation ();
1295 t
= build_int_2 (number
, 0);
1296 TREE_TYPE (t
) = sizetype
;
1297 size_table
[number
] = t
;
1302 t
= build_int_2 (number
, 0);
1303 TREE_TYPE (t
) = sizetype
;
1308 /* Combine operands OP1 and OP2 with arithmetic operation CODE.
1309 CODE is a tree code. Data type is taken from `sizetype',
1310 If the operands are constant, so is the result. */
1313 size_binop (code
, arg0
, arg1
)
1314 enum tree_code code
;
1317 /* Handle the special case of two integer constants faster. */
1318 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1320 /* And some specific cases even faster than that. */
1321 if (code
== PLUS_EXPR
1322 && TREE_INT_CST_LOW (arg0
) == 0
1323 && TREE_INT_CST_HIGH (arg0
) == 0)
1325 if (code
== MINUS_EXPR
1326 && TREE_INT_CST_LOW (arg1
) == 0
1327 && TREE_INT_CST_HIGH (arg1
) == 0)
1329 if (code
== MULT_EXPR
1330 && TREE_INT_CST_LOW (arg0
) == 1
1331 && TREE_INT_CST_HIGH (arg0
) == 0)
1333 /* Handle general case of two integer constants. */
1334 return const_binop (code
, arg0
, arg1
);
1337 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1338 return error_mark_node
;
1340 return fold (build (code
, sizetype
, arg0
, arg1
));
1343 /* Given T, a tree representing type conversion of ARG1, a constant,
1344 return a constant tree representing the result of conversion. */
1347 fold_convert (t
, arg1
)
1351 register tree type
= TREE_TYPE (t
);
1353 if (TREE_CODE (type
) == POINTER_TYPE
1354 || TREE_CODE (type
) == INTEGER_TYPE
1355 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1357 if (TREE_CODE (arg1
) == INTEGER_CST
)
1359 /* Given an integer constant, make new constant with new type,
1360 appropriately sign-extended or truncated. */
1361 t
= build_int_2 (TREE_INT_CST_LOW (arg1
),
1362 TREE_INT_CST_HIGH (arg1
));
1363 TREE_TYPE (t
) = type
;
1366 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1367 else if (TREE_CODE (arg1
) == REAL_CST
)
1370 l
= real_value_from_int_cst (TYPE_MIN_VALUE (type
)),
1371 x
= TREE_REAL_CST (arg1
),
1372 u
= real_value_from_int_cst (TYPE_MAX_VALUE (type
));
1373 if (! ((REAL_VALUES_LESS (l
, x
) || REAL_VALUES_EQUAL (l
, x
))
1374 && (REAL_VALUES_LESS (x
, u
) || REAL_VALUES_EQUAL (x
, u
))))
1376 warning ("real constant out of range for integer conversion");
1379 #ifndef REAL_ARITHMETIC
1383 int half_word
= 1 << (HOST_BITS_PER_INT
/ 2);
1385 d
= TREE_REAL_CST (arg1
);
1389 high
= (int) (d
/ half_word
/ half_word
);
1390 d
-= (REAL_VALUE_TYPE
) high
* half_word
* half_word
;
1392 if (TREE_REAL_CST (arg1
) < 0)
1393 neg_double (low
, high
, &low
, &high
);
1394 t
= build_int_2 (low
, high
);
1399 REAL_VALUE_TO_INT (low
, high
, TREE_REAL_CST (arg1
));
1400 t
= build_int_2 (low
, high
);
1403 TREE_TYPE (t
) = type
;
1406 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1407 TREE_TYPE (t
) = type
;
1409 else if (TREE_CODE (type
) == REAL_TYPE
)
1411 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1412 if (TREE_CODE (arg1
) == INTEGER_CST
)
1413 return build_real_from_int_cst (type
, arg1
);
1414 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1415 if (TREE_CODE (arg1
) == REAL_CST
)
1416 return build_real (type
, REAL_VALUE_TRUNCATE (TYPE_MODE (type
),
1417 TREE_REAL_CST (arg1
)));
1419 TREE_CONSTANT (t
) = 1;
1423 /* Return an expr equal to X but certainly not valid as an lvalue. */
1431 /* These things are certainly not lvalues. */
1432 if (TREE_CODE (x
) == NON_LVALUE_EXPR
1433 || TREE_CODE (x
) == INTEGER_CST
1434 || TREE_CODE (x
) == REAL_CST
1435 || TREE_CODE (x
) == STRING_CST
1436 || TREE_CODE (x
) == ADDR_EXPR
)
1439 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
1440 TREE_CONSTANT (result
) = TREE_CONSTANT (x
);
1444 /* Given a tree comparison code, return the code that is the logical inverse
1445 of the given code. It is not safe to do this for floating-point
1446 comparisons, except for NE_EXPR and EQ_EXPR. */
1448 static enum tree_code
1449 invert_tree_comparison (code
)
1450 enum tree_code code
;
1471 /* Similar, but return the comparison that results if the operands are
1472 swapped. This is safe for floating-point. */
1474 static enum tree_code
1475 swap_tree_comparison (code
)
1476 enum tree_code code
;
1496 /* Return nonzero if two operands are necessarily equal.
1497 If ONLY_CONST is non-zero, only return non-zero for constants. */
1500 operand_equal_p (arg0
, arg1
, only_const
)
1504 /* If both types don't have the same signedness, then we can't consider
1505 them equal. We must check this before the STRIP_NOPS calls
1506 because they may change the signedness of the arguments. */
1507 if (TREE_UNSIGNED (TREE_TYPE (arg0
)) != TREE_UNSIGNED (TREE_TYPE (arg1
)))
1513 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
1514 We don't care about side effects in that case because the SAVE_EXPR
1515 takes care of that for us. */
1516 if (TREE_CODE (arg0
) == SAVE_EXPR
&& arg0
== arg1
)
1517 return ! only_const
;
1519 if (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
))
1522 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
1523 && TREE_CODE (arg0
) == ADDR_EXPR
1524 && TREE_OPERAND (arg0
, 0) == TREE_OPERAND (arg1
, 0))
1527 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
1528 && TREE_CODE (arg0
) == INTEGER_CST
1529 && TREE_INT_CST_LOW (arg0
) == TREE_INT_CST_LOW (arg1
)
1530 && TREE_INT_CST_HIGH (arg0
) == TREE_INT_CST_HIGH (arg1
))
1533 /* Detect when real constants are equal.
1534 But reject weird values because we can't be sure what to do with them. */
1535 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
1536 && TREE_CODE (arg0
) == REAL_CST
1537 && !bcmp (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
),
1538 sizeof (REAL_VALUE_TYPE
))
1539 /* Some people say these are not necessary.
1540 But they do little harm, and taking them out would be risky.
1541 So leave them and let's not spend any more time on them--rms. */
1542 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg0
))
1543 && !REAL_VALUE_ISNAN (TREE_REAL_CST (arg0
)))
1552 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
1554 /* This is needed for conversions and for COMPONENT_REF.
1555 Might as well play it safe and always test this. */
1556 if (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
)))
1559 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
1562 /* Two conversions are equal only if signedness and modes match. */
1563 if ((TREE_CODE (arg0
) == NOP_EXPR
|| TREE_CODE (arg0
) == CONVERT_EXPR
)
1564 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
1565 != TREE_UNSIGNED (TREE_TYPE (arg1
))))
1568 return operand_equal_p (TREE_OPERAND (arg0
, 0),
1569 TREE_OPERAND (arg1
, 0), 0);
1573 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
1574 TREE_OPERAND (arg1
, 0), 0)
1575 && operand_equal_p (TREE_OPERAND (arg0
, 1),
1576 TREE_OPERAND (arg1
, 1), 0));
1579 switch (TREE_CODE (arg0
))
1582 return operand_equal_p (TREE_OPERAND (arg0
, 0),
1583 TREE_OPERAND (arg1
, 0), 0);
1587 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
1588 TREE_OPERAND (arg1
, 0), 0)
1589 && operand_equal_p (TREE_OPERAND (arg0
, 1),
1590 TREE_OPERAND (arg1
, 1), 0));
1593 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
1594 TREE_OPERAND (arg1
, 0), 0)
1595 && operand_equal_p (TREE_OPERAND (arg0
, 1),
1596 TREE_OPERAND (arg1
, 1), 0)
1597 && operand_equal_p (TREE_OPERAND (arg0
, 2),
1598 TREE_OPERAND (arg1
, 2), 0));
1606 /* Similar to operand_equal_p, but see if ARG0 might have been made by
1607 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
1609 When in doubt, return 0. */
1612 operand_equal_for_comparison_p (arg0
, arg1
, other
)
1616 int unsignedp1
, unsignedpo
;
1617 tree primarg1
, primother
;
1620 if (operand_equal_p (arg0
, arg1
, 0))
1623 if (TREE_CODE (TREE_TYPE (arg0
)) != INTEGER_TYPE
)
1626 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
1627 actual comparison operand, ARG0.
1629 First throw away any conversions to wider types
1630 already present in the operands. */
1632 primarg1
= get_narrower (arg1
, &unsignedp1
);
1633 primother
= get_narrower (other
, &unsignedpo
);
1635 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
1636 if (unsignedp1
== unsignedpo
1637 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
1638 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
1640 tree type
= TREE_TYPE (arg0
);
1642 /* Make sure shorter operand is extended the right way
1643 to match the longer operand. */
1644 primarg1
= convert (signed_or_unsigned_type (unsignedp1
,
1645 TREE_TYPE (primarg1
)),
1648 if (operand_equal_p (arg0
, convert (type
, primarg1
), 0))
1655 /* See if ARG is an expression is either a comparison or is peforming
1656 arithmetic on comparisons. The comparisons must only be comparing
1657 two different values, which will be stored in *CVAL1 and *CVAL2; if
1658 they are non-zero it means that some operands have already been found.
1659 No variables may be used anywhere else in the expression except in the
1662 If this is true, return 1. Otherwise, return zero. */
1665 twoval_comparison_p (arg
, cval1
, cval2
)
1667 tree
*cval1
, *cval2
;
1669 enum tree_code code
= TREE_CODE (arg
);
1670 char class = TREE_CODE_CLASS (code
);
1672 /* We can handle some of the 'e' cases here. */
1674 && (code
== TRUTH_NOT_EXPR
1675 || (code
== SAVE_EXPR
&& SAVE_EXPR_RTL (arg
) == 0)))
1677 else if (class == 'e'
1678 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
1679 || code
== COMPOUND_EXPR
))
1685 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
);
1688 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
1689 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
));
1695 if (code
== COND_EXPR
)
1696 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
)
1697 && twoval_comparison_p (TREE_OPERAND (arg
, 1), cval1
, cval2
)
1698 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
1703 /* First see if we can handle the first operand, then the second. For
1704 the second operand, we know *CVAL1 can't be zero. It must be that
1705 one side of the comparison is each of the values; test for the
1706 case where this isn't true by failing if the two operands
1709 if (operand_equal_p (TREE_OPERAND (arg
, 0),
1710 TREE_OPERAND (arg
, 1), 0))
1714 *cval1
= TREE_OPERAND (arg
, 0);
1715 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
1717 else if (*cval2
== 0)
1718 *cval2
= TREE_OPERAND (arg
, 0);
1719 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
1724 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
1726 else if (*cval2
== 0)
1727 *cval2
= TREE_OPERAND (arg
, 1);
1728 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
1739 /* ARG is a tree that is known to contain just arithmetic operations and
1740 comparisons. Evaluate the operations in the tree substituting NEW0 for
1741 any occurrance of OLD0 as an operand of a comparison and likewise for
1745 eval_subst (arg
, old0
, new0
, old1
, new1
)
1747 tree old0
, new0
, old1
, new1
;
1749 tree type
= TREE_TYPE (arg
);
1750 enum tree_code code
= TREE_CODE (arg
);
1751 char class = TREE_CODE_CLASS (code
);
1753 /* We can handle some of the 'e' cases here. */
1754 if (class == 'e' && code
== TRUTH_NOT_EXPR
)
1756 else if (class == 'e'
1757 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
1763 return fold (build1 (code
, type
,
1764 eval_subst (TREE_OPERAND (arg
, 0),
1765 old0
, new0
, old1
, new1
)));
1768 return fold (build (code
, type
,
1769 eval_subst (TREE_OPERAND (arg
, 0),
1770 old0
, new0
, old1
, new1
),
1771 eval_subst (TREE_OPERAND (arg
, 1),
1772 old0
, new0
, old1
, new1
)));
1778 return eval_subst (TREE_OPERAND (arg
, 0), old0
, new0
, old1
, new1
);
1781 return eval_subst (TREE_OPERAND (arg
, 1), old0
, new0
, old1
, new1
);
1784 return fold (build (code
, type
,
1785 eval_subst (TREE_OPERAND (arg
, 0),
1786 old0
, new0
, old1
, new1
),
1787 eval_subst (TREE_OPERAND (arg
, 1),
1788 old0
, new0
, old1
, new1
),
1789 eval_subst (TREE_OPERAND (arg
, 2),
1790 old0
, new0
, old1
, new1
)));
1795 tree arg0
= TREE_OPERAND (arg
, 0);
1796 tree arg1
= TREE_OPERAND (arg
, 1);
1798 /* We need to check both for exact equality and tree equality. The
1799 former will be true if the operand has a side-effect. In that
1800 case, we know the operand occurred exactly once. */
1802 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
1804 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
1807 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
1809 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
1812 return fold (build (code
, type
, arg0
, arg1
));
1819 /* Return a tree for the case when the result of an expression is RESULT
1820 converted to TYPE and OMITTED was previously an operand of the expression
1821 but is now not needed (e.g., we folded OMITTED * 0).
1823 If OMITTED has side effects, we must evaluate it. Otherwise, just do
1824 the conversion of RESULT to TYPE. */
1827 omit_one_operand (type
, result
, omitted
)
1828 tree type
, result
, omitted
;
1830 tree t
= convert (type
, result
);
1832 if (TREE_SIDE_EFFECTS (omitted
))
1833 return build (COMPOUND_EXPR
, type
, omitted
, t
);
1838 /* Return a simplified tree node for the truth-negation of ARG
1839 (perhaps by altering ARG). It is known that ARG is an operation that
1840 returns a truth value (0 or 1). */
1843 invert_truthvalue (arg
)
1846 tree type
= TREE_TYPE (arg
);
1847 enum tree_code code
= TREE_CODE (arg
);
1849 /* If this is a comparison, we can simply invert it, except for
1850 floating-point non-equality comparisons, in which case we just
1851 enclose a TRUTH_NOT_EXPR around what we have. */
1853 if (TREE_CODE_CLASS (code
) == '<')
1855 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REAL_TYPE
1856 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
1857 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
1860 TREE_SET_CODE (arg
, invert_tree_comparison (code
));
1868 return convert (type
, build_int_2 (TREE_INT_CST_LOW (arg
) == 0
1869 && TREE_INT_CST_HIGH (arg
) == 0, 0));
1871 case TRUTH_AND_EXPR
:
1872 return build (TRUTH_OR_EXPR
, type
,
1873 invert_truthvalue (TREE_OPERAND (arg
, 0)),
1874 invert_truthvalue (TREE_OPERAND (arg
, 1)));
1877 return build (TRUTH_AND_EXPR
, type
,
1878 invert_truthvalue (TREE_OPERAND (arg
, 0)),
1879 invert_truthvalue (TREE_OPERAND (arg
, 1)));
1881 case TRUTH_ANDIF_EXPR
:
1882 return build (TRUTH_ORIF_EXPR
, type
,
1883 invert_truthvalue (TREE_OPERAND (arg
, 0)),
1884 invert_truthvalue (TREE_OPERAND (arg
, 1)));
1886 case TRUTH_ORIF_EXPR
:
1887 return build (TRUTH_ANDIF_EXPR
, type
,
1888 invert_truthvalue (TREE_OPERAND (arg
, 0)),
1889 invert_truthvalue (TREE_OPERAND (arg
, 1)));
1891 case TRUTH_NOT_EXPR
:
1892 return TREE_OPERAND (arg
, 0);
1895 return build (COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
1896 invert_truthvalue (TREE_OPERAND (arg
, 1)),
1897 invert_truthvalue (TREE_OPERAND (arg
, 2)));
1900 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg
, 0),
1901 invert_truthvalue (TREE_OPERAND (arg
, 1)));
1903 case NON_LVALUE_EXPR
:
1904 return invert_truthvalue (TREE_OPERAND (arg
, 0));
1909 return build1 (TREE_CODE (arg
), type
,
1910 invert_truthvalue (TREE_OPERAND (arg
, 0)));
1913 if (! integer_onep (TREE_OPERAND (arg
, 1)))
1915 return build (EQ_EXPR
, type
, arg
, convert (type
, integer_zero_node
));
1921 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1922 operands are another bit-wise operation with a common input. If so,
1923 distribute the bit operations to save an operation and possibly two if
1924 constants are involved. For example, convert
1925 (A | B) & (A | C) into A | (B & C)
1926 Further simplification will occur if B and C are constants.
1928 If this optimization cannot be done, 0 will be returned. */
1931 distribute_bit_expr (code
, type
, arg0
, arg1
)
1932 enum tree_code code
;
1939 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
1940 || TREE_CODE (arg0
) == code
1941 || (TREE_CODE (arg0
) != BIT_AND_EXPR
1942 && TREE_CODE (arg0
) != BIT_IOR_EXPR
))
1945 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0))
1947 common
= TREE_OPERAND (arg0
, 0);
1948 left
= TREE_OPERAND (arg0
, 1);
1949 right
= TREE_OPERAND (arg1
, 1);
1951 else if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1), 0))
1953 common
= TREE_OPERAND (arg0
, 0);
1954 left
= TREE_OPERAND (arg0
, 1);
1955 right
= TREE_OPERAND (arg1
, 0);
1957 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 0), 0))
1959 common
= TREE_OPERAND (arg0
, 1);
1960 left
= TREE_OPERAND (arg0
, 0);
1961 right
= TREE_OPERAND (arg1
, 1);
1963 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1), 0))
1965 common
= TREE_OPERAND (arg0
, 1);
1966 left
= TREE_OPERAND (arg0
, 0);
1967 right
= TREE_OPERAND (arg1
, 0);
1972 return fold (build (TREE_CODE (arg0
), type
, common
,
1973 fold (build (code
, type
, left
, right
))));
1976 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
1977 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
1980 make_bit_field_ref (inner
, type
, bitsize
, bitpos
, unsignedp
)
1983 int bitsize
, bitpos
;
1986 tree result
= build (BIT_FIELD_REF
, type
, inner
,
1987 size_int (bitsize
), size_int (bitpos
));
1989 TREE_UNSIGNED (result
) = unsignedp
;
1994 /* Optimize a bit-field compare.
1996 There are two cases: First is a compare against a constant and the
1997 second is a comparison of two items where the fields are at the same
1998 bit position relative to the start of a chunk (byte, halfword, word)
1999 large enough to contain it. In these cases we can avoid the shift
2000 implicit in bitfield extractions.
2002 For constants, we emit a compare of the shifted constant with the
2003 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2004 compared. For two fields at the same position, we do the ANDs with the
2005 similar mask and compare the result of the ANDs.
2007 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2008 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2009 are the left and right operands of the comparison, respectively.
2011 If the optimization described above can be done, we return the resulting
2012 tree. Otherwise we return zero. */
2015 optimize_bit_field_compare (code
, compare_type
, lhs
, rhs
)
2016 enum tree_code code
;
2020 int lbitpos
, lbitsize
, rbitpos
, rbitsize
;
2021 int lnbitpos
, lnbitsize
, rnbitpos
, rnbitsize
;
2022 tree type
= TREE_TYPE (lhs
);
2023 tree signed_type
, unsigned_type
;
2024 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
2025 enum machine_mode lmode
, rmode
, lnmode
, rnmode
;
2026 int lunsignedp
, runsignedp
;
2027 int lvolatilep
= 0, rvolatilep
= 0;
2028 tree linner
, rinner
;
2032 /* Get all the information about the extractions being done. If the bit size
2033 if the same as the size of the underlying object, we aren't doing an
2034 extraction at all and so can do nothing. */
2035 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
2036 &lunsignedp
, &lvolatilep
);
2037 if (lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
2043 /* If this is not a constant, we can only do something if bit positions,
2044 sizes, and signedness are the same. */
2045 rinner
= get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
,
2046 &rmode
, &runsignedp
, &rvolatilep
);
2048 if (lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
2049 || lunsignedp
!= runsignedp
|| offset
!= 0)
2053 /* See if we can find a mode to refer to this field. We should be able to,
2054 but fail if we can't. */
2055 lnmode
= get_best_mode (lbitsize
, lbitpos
,
2056 TYPE_ALIGN (TREE_TYPE (linner
)), word_mode
,
2058 if (lnmode
== VOIDmode
)
2061 /* Set signed and unsigned types of the precision of this mode for the
2063 signed_type
= type_for_mode (lnmode
, 0);
2064 unsigned_type
= type_for_mode (lnmode
, 1);
2068 rnmode
= get_best_mode (rbitsize
, rbitpos
,
2069 TYPE_ALIGN (TREE_TYPE (rinner
)), word_mode
,
2071 if (rnmode
== VOIDmode
)
2075 /* Compute the bit position and size for the new reference and our offset
2076 within it. If the new reference is the same size as the original, we
2077 won't optimize anything, so return zero. */
2078 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
2079 lnbitpos
= lbitpos
& ~ (lnbitsize
- 1);
2080 lbitpos
-= lnbitpos
;
2081 if (lnbitsize
== lbitsize
)
2086 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
2087 rnbitpos
= rbitpos
& ~ (rnbitsize
- 1);
2088 rbitpos
-= rnbitpos
;
2089 if (rnbitsize
== rbitsize
)
2093 #if BYTES_BIG_ENDIAN
2094 lbitpos
= lnbitsize
- lbitsize
- lbitpos
;
2095 rbitpos
= rnbitsize
- rbitsize
- rbitpos
;
2098 /* Make the mask to be used against the extracted field. */
2099 mask
= convert (unsigned_type
, build_int_2 (~0, ~0));
2100 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (lnbitsize
- lbitsize
));
2101 mask
= const_binop (RSHIFT_EXPR
, mask
,
2102 size_int (lnbitsize
- lbitsize
- lbitpos
));
2105 /* If not comparing with constant, just rework the comparison
2107 return build (code
, compare_type
,
2108 build (BIT_AND_EXPR
, type
,
2109 make_bit_field_ref (linner
, type
,
2110 lnbitsize
, lnbitpos
, lunsignedp
),
2112 build (BIT_AND_EXPR
, type
,
2113 make_bit_field_ref (rinner
, type
,
2114 rnbitsize
, rnbitpos
, runsignedp
),
2117 /* Otherwise, we are handling the constant case. See if the constant is too
2118 big for the field. Warn and return a tree of for 0 (false) if so. We do
2119 this not only for its own sake, but to avoid having to test for this
2120 error case below. If we didn't, we might generate wrong code.
2122 For unsigned fields, the constant shifted right by the field length should
2123 be all zero. For signed fields, the high-order bits should agree with
2128 if (! integer_zerop (const_binop (RSHIFT_EXPR
,
2129 convert (unsigned_type
, rhs
),
2130 size_int (lbitsize
))))
2132 warning ("comparison is always %s due to width of bitfield",
2133 code
== NE_EXPR
? "one" : "zero");
2134 return convert (compare_type
,
2136 ? integer_one_node
: integer_zero_node
));
2141 tree tem
= const_binop (RSHIFT_EXPR
, convert (signed_type
, rhs
),
2142 size_int (lbitsize
- 1));
2143 if (! integer_zerop (tem
) && ! integer_all_onesp (tem
))
2145 warning ("comparison is always %s due to width of bitfield",
2146 code
== NE_EXPR
? "one" : "zero");
2147 return convert (compare_type
,
2149 ? integer_one_node
: integer_zero_node
));
2153 /* Single-bit compares should always be against zero. */
2154 if (lbitsize
== 1 && ! integer_zerop (rhs
))
2156 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
2157 rhs
= convert (type
, integer_zero_node
);
2160 /* Make a new bitfield reference, shift the constant over the
2161 appropriate number of bits and mask it with the computed mask
2162 (in case this was a signed field). If we changed it, make a new one. */
2163 lhs
= make_bit_field_ref (linner
, TREE_TYPE (lhs
), lnbitsize
, lnbitpos
,
2166 rhs
= fold (build1 (NOP_EXPR
, type
,
2167 const_binop (BIT_AND_EXPR
,
2168 const_binop (LSHIFT_EXPR
,
2169 convert (unsigned_type
, rhs
),
2170 size_int (lbitpos
)), mask
)));
2172 return build (code
, compare_type
,
2173 build (BIT_AND_EXPR
, type
, lhs
, mask
),
2177 /* Subroutine for the following routine: decode a field reference.
2179 If EXP is a comparison reference, we return the innermost reference.
2181 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2182 set to the starting bit number.
2184 If the innermost field can be completely contained in a mode-sized
2185 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2187 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2188 otherwise it is not changed.
2190 *PUNSIGNEDP is set to the signedness of the field.
2192 *PMASK is set to the mask used. This is either contained in a
2193 BIT_AND_EXPR or derived from the width of the field.
2195 Return 0 if this is not a component reference or is one that we can't
2196 do anything with. */
2199 decode_field_reference (exp
, pbitsize
, pbitpos
, pmode
, punsignedp
,
2202 int *pbitsize
, *pbitpos
;
2203 enum machine_mode
*pmode
;
2204 int *punsignedp
, *pvolatilep
;
2213 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
2215 mask
= TREE_OPERAND (exp
, 1);
2216 exp
= TREE_OPERAND (exp
, 0);
2217 STRIP_NOPS (exp
); STRIP_NOPS (mask
);
2218 if (TREE_CODE (mask
) != INTEGER_CST
)
2222 if (TREE_CODE (exp
) != COMPONENT_REF
&& TREE_CODE (exp
) != ARRAY_REF
2223 && TREE_CODE (exp
) != BIT_FIELD_REF
)
2226 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
2227 punsignedp
, pvolatilep
);
2228 if (*pbitsize
< 0 || offset
!= 0)
2233 tree unsigned_type
= type_for_size (*pbitsize
, 1);
2234 int precision
= TYPE_PRECISION (unsigned_type
);
2236 mask
= convert (unsigned_type
, build_int_2 (~0, ~0));
2237 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
2238 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
2245 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
2249 all_ones_mask_p (mask
, size
)
2253 tree type
= TREE_TYPE (mask
);
2254 int precision
= TYPE_PRECISION (type
);
2257 operand_equal_p (mask
,
2258 const_binop (RSHIFT_EXPR
,
2259 const_binop (LSHIFT_EXPR
,
2260 convert (signed_type (type
),
2261 build_int_2 (~0, ~0)),
2262 size_int (precision
- size
)),
2263 size_int (precision
- size
)), 0);
2266 /* Try to merge two comparisons to the same innermost item.
2268 For example, if we have p->a == 2 && p->b == 4 and we can make an
2269 object large enough to span both A and B, we can do this with a comparison
2270 against the object ANDed with the a mask.
2272 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
2273 operations to do this with one comparison.
2275 We check for both normal comparisons and the BIT_AND_EXPRs made this by
2276 function and the one above.
2278 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
2279 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
2281 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
2284 We return the simplified tree or 0 if no optimization is possible. */
2287 merge_component_references (code
, truth_type
, lhs
, rhs
)
2288 enum tree_code code
;
2289 tree truth_type
, lhs
, rhs
;
2291 /* If this is the "or" of two comparisons, we can do something if we
2292 the comparisons are NE_EXPR. If this is the "and", we can do something
2293 if the comparisons are EQ_EXPR. I.e.,
2294 (a->b == 2 && a->c == 4) can become (a->new == NEW).
2296 WANTED_CODE is this operation code. For single bit fields, we can
2297 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
2298 comparison for one-bit fields. */
2300 enum tree_code wanted_code
2301 = (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2302 enum tree_code lcode
, rcode
;
2303 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
2304 int ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
2305 int rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
2306 int xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
2307 int lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
2308 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
2309 enum machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
2310 enum machine_mode lnmode
, rnmode
;
2311 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
2312 tree l_const
= 0, r_const
= 0;
2314 int first_bit
, end_bit
;
2317 /* Start by getting the comparison codes and seeing if we may be able
2318 to do something. Then get all the parameters for each side. Fail
2319 if anything is volatile. */
2321 lcode
= TREE_CODE (lhs
);
2322 rcode
= TREE_CODE (rhs
);
2323 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
2324 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
)
2325 || TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
2328 ll_inner
= decode_field_reference (TREE_OPERAND (lhs
, 0),
2329 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
2330 &ll_unsignedp
, &volatilep
, &ll_mask
);
2331 lr_inner
= decode_field_reference (TREE_OPERAND (lhs
, 1),
2332 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
2333 &lr_unsignedp
, &volatilep
, &lr_mask
);
2334 rl_inner
= decode_field_reference (TREE_OPERAND (rhs
, 0),
2335 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
2336 &rl_unsignedp
, &volatilep
, &rl_mask
);
2337 rr_inner
= decode_field_reference (TREE_OPERAND (rhs
, 1),
2338 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
2339 &rr_unsignedp
, &volatilep
, &rr_mask
);
2341 /* It must be true that the inner operation on the lhs of each
2342 comparison must be the same if we are to be able to do anything.
2343 Then see if we have constants. If not, the same must be true for
2345 if (volatilep
|| ll_inner
== 0 || rl_inner
== 0
2346 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
2349 if (TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
2350 && TREE_CODE (TREE_OPERAND (rhs
, 1)) == INTEGER_CST
)
2351 l_const
= TREE_OPERAND (lhs
, 1), r_const
= TREE_OPERAND (rhs
, 1);
2352 else if (lr_inner
== 0 || rr_inner
== 0
2353 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
2356 /* If either comparison code is not correct for our logical operation,
2357 fail. However, we can convert a one-bit comparison against zero into
2358 the opposite comparison against that bit being set in the field. */
2359 if (lcode
!= wanted_code
)
2361 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
2367 if (rcode
!= wanted_code
)
2369 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
2375 /* See if we can find a mode that contains both fields being compared on
2376 the left. If we can't, fail. Otherwise, update all constants and masks
2377 to be relative to a field of that size. */
2378 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
2379 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
2380 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
,
2381 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
2383 if (lnmode
== VOIDmode
)
2386 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
2387 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
2388 type
= type_for_size (lnbitsize
, 1);
2389 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
2391 #if BYTES_BIG_ENDIAN
2392 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
2393 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
2396 ll_mask
= const_binop (LSHIFT_EXPR
, convert (type
, ll_mask
),
2397 size_int (xll_bitpos
));
2398 rl_mask
= const_binop (LSHIFT_EXPR
, convert (type
, rl_mask
),
2399 size_int (xrl_bitpos
));
2401 /* Make sure the constants are interpreted as unsigned, so we
2402 don't have sign bits outside the range of their type. */
2406 l_const
= convert (unsigned_type (TREE_TYPE (l_const
)), l_const
);
2407 l_const
= const_binop (LSHIFT_EXPR
, convert (type
, l_const
),
2408 size_int (xll_bitpos
));
2412 r_const
= convert (unsigned_type (TREE_TYPE (r_const
)), r_const
);
2413 r_const
= const_binop (LSHIFT_EXPR
, convert (type
, r_const
),
2414 size_int (xrl_bitpos
));
2417 /* If the right sides are not constant, do the same for it. Also,
2418 disallow this optimization if a size or signedness mismatch occurs
2419 between the left and right sides. */
2422 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
2423 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
)
2426 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
2427 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
2428 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
,
2429 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
2431 if (rnmode
== VOIDmode
)
2434 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
2435 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
2436 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
2438 #if BYTES_BIG_ENDIAN
2439 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
2440 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
2443 lr_mask
= const_binop (LSHIFT_EXPR
, convert (type
, lr_mask
),
2444 size_int (xlr_bitpos
));
2445 rr_mask
= const_binop (LSHIFT_EXPR
, convert (type
, rr_mask
),
2446 size_int (xrr_bitpos
));
2448 /* Make a mask that corresponds to both fields being compared.
2449 Do this for both items being compared. If the masks agree,
2450 we can do this by masking both and comparing the masked
2452 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
2453 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
2454 if (operand_equal_p (ll_mask
, lr_mask
, 0) && lnbitsize
== rnbitsize
)
2456 lhs
= make_bit_field_ref (ll_inner
, type
, lnbitsize
, lnbitpos
,
2457 ll_unsignedp
|| rl_unsignedp
);
2458 rhs
= make_bit_field_ref (lr_inner
, type
, rnbitsize
, rnbitpos
,
2459 lr_unsignedp
|| rr_unsignedp
);
2460 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
2462 lhs
= build (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
2463 rhs
= build (BIT_AND_EXPR
, type
, rhs
, ll_mask
);
2465 return build (wanted_code
, truth_type
, lhs
, rhs
);
2468 /* There is still another way we can do something: If both pairs of
2469 fields being compared are adjacent, we may be able to make a wider
2470 field containing them both. */
2471 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
2472 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
2473 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
2474 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
2475 return build (wanted_code
, truth_type
,
2476 make_bit_field_ref (ll_inner
, type
,
2477 ll_bitsize
+ rl_bitsize
,
2478 MIN (ll_bitpos
, rl_bitpos
),
2480 make_bit_field_ref (lr_inner
, type
,
2481 lr_bitsize
+ rr_bitsize
,
2482 MIN (lr_bitpos
, rr_bitpos
),
2488 /* Handle the case of comparisons with constants. If there is something in
2489 common between the masks, those bits of the constants must be the same.
2490 If not, the condition is always false. Test for this to avoid generating
2491 incorrect code below. */
2492 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
2493 if (! integer_zerop (result
)
2494 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
2495 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
2497 if (wanted_code
== NE_EXPR
)
2499 warning ("`or' of unmatched not-equal tests is always 1");
2500 return convert (truth_type
, integer_one_node
);
2504 warning ("`and' of mutually exclusive equal-tests is always zero");
2505 return convert (truth_type
, integer_zero_node
);
2509 /* Construct the expression we will return. First get the component
2510 reference we will make. Unless the mask is all ones the width of
2511 that field, perform the mask operation. Then compare with the
2513 result
= make_bit_field_ref (ll_inner
, type
, lnbitsize
, lnbitpos
,
2514 ll_unsignedp
|| rl_unsignedp
);
2516 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
2517 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
2518 result
= build (BIT_AND_EXPR
, type
, result
, ll_mask
);
2520 return build (wanted_code
, truth_type
, result
,
2521 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
2524 /* Perform constant folding and related simplification of EXPR.
2525 The related simplifications include x*1 => x, x*0 => 0, etc.,
2526 and application of the associative law.
2527 NOP_EXPR conversions may be removed freely (as long as we
2528 are careful not to change the C type of the overall expression)
2529 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
2530 but we can constant-fold them if they have constant operands. */
2536 register tree t
= expr
;
2537 tree t1
= NULL_TREE
;
2539 tree type
= TREE_TYPE (expr
);
2540 register tree arg0
, arg1
;
2541 register enum tree_code code
= TREE_CODE (t
);
2545 /* WINS will be nonzero when the switch is done
2546 if all operands are constant. */
2550 /* Return right away if already constant. */
2551 if (TREE_CONSTANT (t
))
2553 if (code
== CONST_DECL
)
2554 return DECL_INITIAL (t
);
2558 kind
= TREE_CODE_CLASS (code
);
2559 if (kind
== 'e' || kind
== '<' || kind
== '1' || kind
== '2' || kind
== 'r')
2561 register int len
= tree_code_length
[(int) code
];
2563 for (i
= 0; i
< len
; i
++)
2565 tree op
= TREE_OPERAND (t
, i
);
2568 continue; /* Valid for CALL_EXPR, at least. */
2570 /* Strip any conversions that don't change the mode. */
2573 if (TREE_CODE (op
) != INTEGER_CST
2574 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2575 && TREE_CODE (op
) != REAL_CST
2576 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2578 /* Note that TREE_CONSTANT isn't enough:
2579 static var addresses are constant but we can't
2580 do arithmetic on them. */
2590 /* If this is a commutative operation, and ARG0 is a constant, move it
2591 to ARG1 to reduce the number of tests below. */
2592 if ((code
== PLUS_EXPR
|| code
== MULT_EXPR
|| code
== MIN_EXPR
2593 || code
== MAX_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
2594 || code
== BIT_AND_EXPR
)
2595 && (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
))
2597 tem
= arg0
; arg0
= arg1
; arg1
= tem
;
2599 tem
= TREE_OPERAND (t
, 0); TREE_OPERAND (t
, 0) = TREE_OPERAND (t
, 1);
2600 TREE_OPERAND (t
, 1) = tem
;
2603 /* Now WINS is set as described above,
2604 ARG0 is the first operand of EXPR,
2605 and ARG1 is the second operand (if it has more than one operand).
2607 First check for cases where an arithmetic operation is applied to a
2608 compound, conditional, or comparison operation. Push the arithmetic
2609 operation inside the compound or conditional to see if any folding
2610 can then be done. Convert comparison to conditional for this purpose.
2611 The also optimizes non-constant cases that used to be done in
2613 if (TREE_CODE_CLASS (code
) == '1')
2615 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
2616 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
2617 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 1))));
2618 else if (TREE_CODE (arg0
) == COND_EXPR
)
2619 return fold (build (COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
2620 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 1))),
2621 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 2)))));
2622 else if (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<')
2623 return fold (build (COND_EXPR
, type
, arg0
,
2624 fold (build1 (code
, type
, integer_one_node
)),
2625 fold (build1 (code
, type
, integer_zero_node
))));
2627 else if (TREE_CODE_CLASS (code
) == '2')
2629 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
2630 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
2631 fold (build (code
, type
, arg0
, TREE_OPERAND (arg1
, 1))));
2632 else if (TREE_CODE (arg1
) == COND_EXPR
2633 || TREE_CODE_CLASS (TREE_CODE (arg1
)) == '<')
2635 tree test
, true_value
, false_value
;
2637 if (TREE_CODE (arg1
) == COND_EXPR
)
2639 test
= TREE_OPERAND (arg1
, 0);
2640 true_value
= TREE_OPERAND (arg1
, 1);
2641 false_value
= TREE_OPERAND (arg1
, 2);
2646 true_value
= integer_one_node
;
2647 false_value
= integer_zero_node
;
2650 if (TREE_CODE (arg0
) != VAR_DECL
&& TREE_CODE (arg0
) != PARM_DECL
)
2651 arg0
= save_expr (arg0
);
2652 test
= fold (build (COND_EXPR
, type
, test
,
2653 fold (build (code
, type
, arg0
, true_value
)),
2654 fold (build (code
, type
, arg0
, false_value
))));
2655 if (TREE_CODE (arg0
) == SAVE_EXPR
)
2656 return build (COMPOUND_EXPR
, type
,
2657 convert (void_type_node
, arg0
), test
);
2659 return convert (type
, test
);
2662 else if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
2663 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
2664 fold (build (code
, type
, TREE_OPERAND (arg0
, 1), arg1
)));
2665 else if (TREE_CODE (arg0
) == COND_EXPR
2666 || TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<')
2668 tree test
, true_value
, false_value
;
2670 if (TREE_CODE (arg0
) == COND_EXPR
)
2672 test
= TREE_OPERAND (arg0
, 0);
2673 true_value
= TREE_OPERAND (arg0
, 1);
2674 false_value
= TREE_OPERAND (arg0
, 2);
2679 true_value
= integer_one_node
;
2680 false_value
= integer_zero_node
;
2683 if (TREE_CODE (arg1
) != VAR_DECL
&& TREE_CODE (arg1
) != PARM_DECL
)
2684 arg1
= save_expr (arg1
);
2685 test
= fold (build (COND_EXPR
, type
, test
,
2686 fold (build (code
, type
, true_value
, arg1
)),
2687 fold (build (code
, type
, false_value
, arg1
))));
2688 if (TREE_CODE (arg1
) == SAVE_EXPR
)
2689 return build (COMPOUND_EXPR
, type
,
2690 convert (void_type_node
, arg1
), test
);
2692 return convert (type
, test
);
2695 else if (TREE_CODE_CLASS (code
) == '<'
2696 && TREE_CODE (arg0
) == COMPOUND_EXPR
)
2697 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
2698 fold (build (code
, type
, TREE_OPERAND (arg0
, 1), arg1
)));
2699 else if (TREE_CODE_CLASS (code
) == '<'
2700 && TREE_CODE (arg1
) == COMPOUND_EXPR
)
2701 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
2702 fold (build (code
, type
, arg0
, TREE_OPERAND (arg1
, 1))));
2714 return fold (DECL_INITIAL (t
));
2719 case FIX_TRUNC_EXPR
:
2720 /* Other kinds of FIX are not handled properly by fold_convert. */
2721 /* Two conversions in a row are not needed unless:
2722 - the intermediate type is narrower than both initial and final, or
2723 - the initial type is a pointer type and the precisions of the
2724 intermediate and final types differ, or
2725 - the final type is a pointer type and the precisions of the
2726 initial and intermediate types differ. */
2727 if ((TREE_CODE (TREE_OPERAND (t
, 0)) == NOP_EXPR
2728 || TREE_CODE (TREE_OPERAND (t
, 0)) == CONVERT_EXPR
)
2729 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t
, 0)))
2730 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
2732 TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t
, 0)))
2733 > TYPE_PRECISION (TREE_TYPE (t
)))
2734 && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t
, 0)))
2735 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t
, 0)))
2736 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))))
2738 (TREE_UNSIGNED (TREE_TYPE (t
))
2739 && (TYPE_PRECISION (TREE_TYPE (t
))
2740 > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t
, 0))))))
2741 && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
2743 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t
, 0)))
2744 != TYPE_PRECISION (TREE_TYPE (t
))))
2745 && ! (TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
2746 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)))
2747 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t
, 0))))))
2748 return convert (TREE_TYPE (t
), TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
2750 if (TREE_CODE (TREE_OPERAND (t
, 0)) == MODIFY_EXPR
2751 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t
, 0), 1))
2752 /* Detect assigning a bitfield. */
2753 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == COMPONENT_REF
2754 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 1))))
2756 /* Don't leave an assignment inside a conversion
2757 unless assiging a bitfield. */
2758 tree prev
= TREE_OPERAND (t
, 0);
2759 TREE_OPERAND (t
, 0) = TREE_OPERAND (prev
, 1);
2760 /* First do the assignment, then return converted constant. */
2761 t
= build (COMPOUND_EXPR
, TREE_TYPE (t
), prev
, fold (t
));
2767 TREE_CONSTANT (t
) = TREE_CONSTANT (arg0
);
2770 return fold_convert (t
, arg0
);
2772 #if 0 /* This loses on &"foo"[0]. */
2777 /* Fold an expression like: "foo"[2] */
2778 if (TREE_CODE (arg0
) == STRING_CST
2779 && TREE_CODE (arg1
) == INTEGER_CST
2780 && !TREE_INT_CST_HIGH (arg1
)
2781 && (i
= TREE_INT_CST_LOW (arg1
)) < TREE_STRING_LENGTH (arg0
))
2783 t
= build_int_2 (TREE_STRING_POINTER (arg0
)[i
], 0);
2784 TREE_TYPE (t
) = TREE_TYPE (TREE_TYPE (arg0
));
2792 TREE_CONSTANT (t
) = wins
;
2798 if (TREE_CODE (arg0
) == INTEGER_CST
)
2800 if (TREE_INT_CST_LOW (arg0
) == 0)
2801 t
= build_int_2 (0, - TREE_INT_CST_HIGH (arg0
));
2803 t
= build_int_2 (- TREE_INT_CST_LOW (arg0
),
2804 ~ TREE_INT_CST_HIGH (arg0
));
2805 TREE_TYPE (t
) = type
;
2808 else if (TREE_CODE (arg0
) == REAL_CST
)
2809 t
= build_real (type
, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0
)));
2810 TREE_TYPE (t
) = type
;
2812 else if (TREE_CODE (arg0
) == NEGATE_EXPR
)
2813 return TREE_OPERAND (arg0
, 0);
2815 /* Convert - (a - b) to (b - a) for non-floating-point. */
2816 else if (TREE_CODE (arg0
) == MINUS_EXPR
&& TREE_CODE (type
) != REAL_TYPE
)
2817 return build (MINUS_EXPR
, type
, TREE_OPERAND (arg0
, 1),
2818 TREE_OPERAND (arg0
, 0));
2825 if (TREE_CODE (arg0
) == INTEGER_CST
)
2827 if (! TREE_UNSIGNED (type
)
2828 && TREE_INT_CST_HIGH (arg0
) < 0)
2830 if (TREE_INT_CST_LOW (arg0
) == 0)
2831 t
= build_int_2 (0, - TREE_INT_CST_HIGH (arg0
));
2833 t
= build_int_2 (- TREE_INT_CST_LOW (arg0
),
2834 ~ TREE_INT_CST_HIGH (arg0
));
2837 else if (TREE_CODE (arg0
) == REAL_CST
)
2839 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
2840 t
= build_real (type
,
2841 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0
)));
2843 TREE_TYPE (t
) = type
;
2845 else if (TREE_CODE (arg0
) == ABS_EXPR
|| TREE_CODE (arg0
) == NEGATE_EXPR
)
2846 return build1 (ABS_EXPR
, type
, TREE_OPERAND (arg0
, 0));
2852 if (TREE_CODE (arg0
) == INTEGER_CST
)
2853 t
= build_int_2 (~ TREE_INT_CST_LOW (arg0
),
2854 ~ TREE_INT_CST_HIGH (arg0
));
2855 TREE_TYPE (t
) = type
;
2858 else if (TREE_CODE (arg0
) == BIT_NOT_EXPR
)
2859 return TREE_OPERAND (arg0
, 0);
2863 /* A + (-B) -> A - B */
2864 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
2865 return fold (build (MINUS_EXPR
, type
, arg0
, TREE_OPERAND (arg1
, 0)));
2866 else if (TREE_CODE (type
) != REAL_TYPE
)
2868 if (integer_zerop (arg1
))
2869 return non_lvalue (convert (type
, arg0
));
2871 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
2872 with a constant, and the two constants have no bits in common,
2873 we should treat this as a BIT_IOR_EXPR since this may produce more
2875 if (TREE_CODE (arg0
) == BIT_AND_EXPR
2876 && TREE_CODE (arg1
) == BIT_AND_EXPR
2877 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
2878 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
2879 && integer_zerop (const_binop (BIT_AND_EXPR
,
2880 TREE_OPERAND (arg0
, 1),
2881 TREE_OPERAND (arg1
, 1))))
2883 code
= BIT_IOR_EXPR
;
2887 /* In IEEE floating point, x+0 may not equal x. */
2888 else if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
2889 && real_zerop (arg1
))
2890 return non_lvalue (convert (type
, arg0
));
2892 /* In most languages, can't associate operations on floats
2893 through parentheses. Rather than remember where the parentheses
2894 were, we don't associate floats at all. It shouldn't matter much. */
2895 if (TREE_CODE (type
) == REAL_TYPE
)
2897 /* The varsign == -1 cases happen only for addition and subtraction.
2898 It says that the arg that was split was really CON minus VAR.
2899 The rest of the code applies to all associative operations. */
2905 if (split_tree (arg0
, code
, &var
, &con
, &varsign
))
2909 /* EXPR is (CON-VAR) +- ARG1. */
2910 /* If it is + and VAR==ARG1, return just CONST. */
2911 if (code
== PLUS_EXPR
&& operand_equal_p (var
, arg1
, 0))
2912 return convert (TREE_TYPE (t
), con
);
2914 /* Otherwise return (CON +- ARG1) - VAR. */
2915 TREE_SET_CODE (t
, MINUS_EXPR
);
2916 TREE_OPERAND (t
, 1) = var
;
2918 = fold (build (code
, TREE_TYPE (t
), con
, arg1
));
2922 /* EXPR is (VAR+CON) +- ARG1. */
2923 /* If it is - and VAR==ARG1, return just CONST. */
2924 if (code
== MINUS_EXPR
&& operand_equal_p (var
, arg1
, 0))
2925 return convert (TREE_TYPE (t
), con
);
2927 /* Otherwise return VAR +- (ARG1 +- CON). */
2928 TREE_OPERAND (t
, 1) = tem
2929 = fold (build (code
, TREE_TYPE (t
), arg1
, con
));
2930 TREE_OPERAND (t
, 0) = var
;
2931 if (integer_zerop (tem
)
2932 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
))
2933 return convert (type
, var
);
2934 /* If we have x +/- (c - d) [c an explicit integer]
2935 change it to x -/+ (d - c) since if d is relocatable
2936 then the latter can be a single immediate insn
2937 and the former cannot. */
2938 if (TREE_CODE (tem
) == MINUS_EXPR
2939 && TREE_CODE (TREE_OPERAND (tem
, 0)) == INTEGER_CST
)
2941 tree tem1
= TREE_OPERAND (tem
, 1);
2942 TREE_OPERAND (tem
, 1) = TREE_OPERAND (tem
, 0);
2943 TREE_OPERAND (tem
, 0) = tem1
;
2945 (code
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
));
2951 if (split_tree (arg1
, code
, &var
, &con
, &varsign
))
2953 /* EXPR is ARG0 +- (CON +- VAR). */
2956 (code
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
));
2957 if (TREE_CODE (t
) == MINUS_EXPR
2958 && operand_equal_p (var
, arg0
, 0))
2960 /* If VAR and ARG0 cancel, return just CON or -CON. */
2961 if (code
== PLUS_EXPR
)
2962 return convert (TREE_TYPE (t
), con
);
2963 return fold (build1 (NEGATE_EXPR
, TREE_TYPE (t
),
2964 convert (TREE_TYPE (t
), con
)));
2967 = fold (build (code
, TREE_TYPE (t
), arg0
, con
));
2968 TREE_OPERAND (t
, 1) = var
;
2969 if (integer_zerop (TREE_OPERAND (t
, 0))
2970 && TREE_CODE (t
) == PLUS_EXPR
)
2971 return convert (TREE_TYPE (t
), var
);
2976 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
2977 if (TREE_CODE (arg1
) == REAL_CST
)
2979 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
2981 t1
= const_binop (code
, arg0
, arg1
);
2982 if (t1
!= NULL_TREE
)
2984 /* The return value should always have
2985 the same type as the original expression. */
2986 TREE_TYPE (t1
) = TREE_TYPE (t
);
2992 if (TREE_CODE (type
) != REAL_TYPE
)
2994 if (! wins
&& integer_zerop (arg0
))
2995 return build1 (NEGATE_EXPR
, type
, arg1
);
2996 if (integer_zerop (arg1
))
2997 return non_lvalue (convert (type
, arg0
));
2999 /* Convert A - (-B) to A + B. */
3000 else if (TREE_CODE (arg1
) == NEGATE_EXPR
)
3001 return fold (build (PLUS_EXPR
, type
, arg0
, TREE_OPERAND (arg1
, 0)));
3002 else if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
)
3004 /* Except with IEEE floating point, 0-x equals -x. */
3005 if (! wins
&& real_zerop (arg0
))
3006 return build1 (NEGATE_EXPR
, type
, arg1
);
3007 /* Except with IEEE floating point, x-0 equals x. */
3008 if (real_zerop (arg1
))
3009 return non_lvalue (convert (type
, arg0
));
3011 /* Fold &x - &x. This can happen from &x.foo - &x.
3012 This is unsafe for certain floats even in non-IEEE formats.
3013 In IEEE, it is unsafe because it does wrong for NaNs.
3014 Also note that operand_equal_p is always false is an operand
3017 if (operand_equal_p (arg0
, arg1
,
3018 TREE_CODE (type
) == REAL_TYPE
))
3019 return convert (type
, integer_zero_node
);
3024 if (TREE_CODE (type
) != REAL_TYPE
)
3026 if (integer_zerop (arg1
))
3027 return omit_one_operand (type
, arg1
, arg0
);
3028 if (integer_onep (arg1
))
3029 return non_lvalue (convert (type
, arg0
));
3031 /* (a * (1 << b)) is (a << b) */
3032 if (TREE_CODE (arg1
) == LSHIFT_EXPR
3033 && integer_onep (TREE_OPERAND (arg1
, 0)))
3034 return fold (build (LSHIFT_EXPR
, type
, arg0
,
3035 TREE_OPERAND (arg1
, 1)));
3036 if (TREE_CODE (arg0
) == LSHIFT_EXPR
3037 && integer_onep (TREE_OPERAND (arg0
, 0)))
3038 return fold (build (LSHIFT_EXPR
, type
, arg1
,
3039 TREE_OPERAND (arg0
, 1)));
3043 /* x*0 is 0, except for IEEE floating point. */
3044 if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
3045 && real_zerop (arg1
))
3046 return omit_one_operand (type
, arg1
, arg0
);
3047 /* In IEEE floating point, x*1 is not equivalent to x for snans.
3048 However, ANSI says we can drop signals,
3049 so we can do this anyway. */
3050 if (real_onep (arg1
))
3051 return non_lvalue (convert (type
, arg0
));
3053 if (! wins
&& real_twop (arg1
))
3055 tree arg
= save_expr (arg0
);
3056 return build (PLUS_EXPR
, type
, arg
, arg
);
3063 if (integer_all_onesp (arg1
))
3064 return omit_one_operand (type
, arg1
, arg0
);
3065 if (integer_zerop (arg1
))
3066 return non_lvalue (convert (type
, arg0
));
3067 t1
= distribute_bit_expr (code
, type
, arg0
, arg1
);
3068 if (t1
!= NULL_TREE
)
3073 if (integer_zerop (arg1
))
3074 return non_lvalue (convert (type
, arg0
));
3075 if (integer_all_onesp (arg1
))
3076 return fold (build1 (BIT_NOT_EXPR
, type
, arg0
));
3081 if (integer_all_onesp (arg1
))
3082 return non_lvalue (convert (type
, arg0
));
3083 if (integer_zerop (arg1
))
3084 return omit_one_operand (type
, arg1
, arg0
);
3085 t1
= distribute_bit_expr (code
, type
, arg0
, arg1
);
3086 if (t1
!= NULL_TREE
)
3088 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
3089 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == NOP_EXPR
3090 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
3092 int prec
= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)));
3093 if (prec
< BITS_PER_WORD
&& prec
< HOST_BITS_PER_INT
3094 && (~TREE_INT_CST_LOW (arg0
) & ((1 << prec
) - 1)) == 0)
3095 return build1 (NOP_EXPR
, type
, TREE_OPERAND (arg1
, 0));
3097 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
3098 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
3100 int prec
= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
3101 if (prec
< BITS_PER_WORD
&& prec
< HOST_BITS_PER_INT
3102 && (~TREE_INT_CST_LOW (arg1
) & ((1 << prec
) - 1)) == 0)
3103 return build1 (NOP_EXPR
, type
, TREE_OPERAND (arg0
, 0));
3107 case BIT_ANDTC_EXPR
:
3108 if (integer_all_onesp (arg0
))
3109 return non_lvalue (convert (type
, arg1
));
3110 if (integer_zerop (arg0
))
3111 return omit_one_operand (type
, arg0
, arg1
);
3112 if (TREE_CODE (arg1
) == INTEGER_CST
)
3114 arg1
= fold (build1 (BIT_NOT_EXPR
, type
, arg1
));
3115 code
= BIT_AND_EXPR
;
3120 case TRUNC_DIV_EXPR
:
3121 case ROUND_DIV_EXPR
:
3122 case FLOOR_DIV_EXPR
:
3124 case EXACT_DIV_EXPR
:
3126 if (integer_onep (arg1
))
3127 return non_lvalue (convert (type
, arg0
));
3128 if (integer_zerop (arg1
))
3130 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3131 #ifndef REAL_INFINITY
3132 if (TREE_CODE (arg1
) == REAL_CST
3133 && real_zerop (arg1
))
3136 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3141 case FLOOR_MOD_EXPR
:
3142 case ROUND_MOD_EXPR
:
3143 case TRUNC_MOD_EXPR
:
3144 if (integer_onep (arg1
))
3145 return omit_one_operand (type
, integer_zero_node
, arg0
);
3146 if (integer_zerop (arg1
))
3154 if (integer_zerop (arg1
))
3155 return non_lvalue (convert (type
, arg0
));
3156 /* Since negative shift count is not well-defined,
3157 don't try to compute it in the compiler. */
3158 if (tree_int_cst_lt (arg1
, integer_zero_node
))
3163 if (operand_equal_p (arg0
, arg1
, 0))
3165 if (TREE_CODE (type
) == INTEGER_TYPE
3166 && operand_equal_p (arg1
, TYPE_MIN_VALUE (type
), 1))
3167 return omit_one_operand (type
, arg1
, arg0
);
3171 if (operand_equal_p (arg0
, arg1
, 0))
3173 if (TREE_CODE (type
) == INTEGER_TYPE
3174 && operand_equal_p (arg1
, TYPE_MAX_VALUE (type
), 1))
3175 return omit_one_operand (type
, arg1
, arg0
);
3178 case TRUTH_NOT_EXPR
:
3179 /* Note that the operand of this must be an int
3180 and its values must be 0 or 1.
3181 ("true" is a fixed value perhaps depending on the language,
3182 but we don't handle values other than 1 correctly yet.) */
3183 return invert_truthvalue (arg0
);
3185 case TRUTH_ANDIF_EXPR
:
3186 /* Note that the operands of this must be ints
3187 and their values must be 0 or 1.
3188 ("true" is a fixed value perhaps depending on the language.) */
3189 /* If first arg is constant zero, return it. */
3190 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
3192 case TRUTH_AND_EXPR
:
3193 /* If either arg is constant true, drop it. */
3194 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
3195 return non_lvalue (arg1
);
3196 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
3197 return non_lvalue (arg0
);
3198 /* Both known to be zero => return zero. */
3199 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3203 /* Check for the possibility of merging component references. If our
3204 lhs is another similar operation, try to merge its rhs with our
3205 rhs. Then try to merge our lhs and rhs. */
3208 if (TREE_CODE (arg0
) == code
)
3210 tem
= merge_component_references (code
, type
,
3211 TREE_OPERAND (arg0
, 1), arg1
);
3213 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
3216 tem
= merge_component_references (code
, type
, arg0
, arg1
);
3222 case TRUTH_ORIF_EXPR
:
3223 /* Note that the operands of this must be ints
3224 and their values must be 0 or true.
3225 ("true" is a fixed value perhaps depending on the language.) */
3226 /* If first arg is constant true, return it. */
3227 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
3230 /* If either arg is constant zero, drop it. */
3231 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
3232 return non_lvalue (arg1
);
3233 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
))
3234 return non_lvalue (arg0
);
3235 /* Both known to be true => return true. */
3236 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3246 /* If one arg is a constant integer, put it last. */
3247 if (TREE_CODE (arg0
) == INTEGER_CST
3248 && TREE_CODE (arg1
) != INTEGER_CST
)
3250 TREE_OPERAND (t
, 0) = arg1
;
3251 TREE_OPERAND (t
, 1) = arg0
;
3252 arg0
= TREE_OPERAND (t
, 0);
3253 arg1
= TREE_OPERAND (t
, 1);
3254 code
= swap_tree_comparison (code
);
3255 TREE_SET_CODE (t
, code
);
3258 /* Convert foo++ == CONST into ++foo == CONST + INCR.
3259 First, see if one arg is constant; find the constant arg
3260 and the other one. */
3262 tree constop
= 0, varop
;
3265 if (TREE_CONSTANT (arg1
))
3266 constoploc
= &TREE_OPERAND (t
, 1), constop
= arg1
, varop
= arg0
;
3267 if (TREE_CONSTANT (arg0
))
3268 constoploc
= &TREE_OPERAND (t
, 0), constop
= arg0
, varop
= arg1
;
3270 if (constop
&& TREE_CODE (varop
) == POSTINCREMENT_EXPR
)
3272 /* This optimization is invalid for ordered comparisons
3273 if CONST+INCR overflows or if foo+incr might overflow.
3274 This optimization is invalid for floating point due to rounding.
3275 For pointer types we assume overflow doesn't happen. */
3276 if (TREE_CODE (TREE_TYPE (varop
)) == POINTER_TYPE
3277 || (TREE_CODE (TREE_TYPE (varop
)) != REAL_TYPE
3278 && (code
== EQ_EXPR
|| code
== NE_EXPR
)))
3281 = fold (build (PLUS_EXPR
, TREE_TYPE (varop
),
3282 constop
, TREE_OPERAND (varop
, 1)));
3283 TREE_SET_CODE (varop
, PREINCREMENT_EXPR
);
3284 *constoploc
= newconst
;
3288 else if (constop
&& TREE_CODE (varop
) == POSTDECREMENT_EXPR
)
3290 if (TREE_CODE (TREE_TYPE (varop
)) == POINTER_TYPE
3291 || (TREE_CODE (TREE_TYPE (varop
)) != REAL_TYPE
3292 && (code
== EQ_EXPR
|| code
== NE_EXPR
)))
3295 = fold (build (MINUS_EXPR
, TREE_TYPE (varop
),
3296 constop
, TREE_OPERAND (varop
, 1)));
3297 TREE_SET_CODE (varop
, PREDECREMENT_EXPR
);
3298 *constoploc
= newconst
;
3304 /* Change X >= CST to X > (CST - 1) if CST is positive. */
3305 if (TREE_CODE (arg1
) == INTEGER_CST
3306 && TREE_CODE (arg0
) != INTEGER_CST
3307 && ! tree_int_cst_lt (arg1
, integer_one_node
))
3309 switch (TREE_CODE (t
))
3313 TREE_SET_CODE (t
, code
);
3314 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
);
3315 TREE_OPERAND (t
, 1) = arg1
;
3320 TREE_SET_CODE (t
, code
);
3321 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
);
3322 TREE_OPERAND (t
, 1) = arg1
;
3326 /* If this is an EQ or NE comparison with zero and ARG0 is
3327 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
3328 two operations, but the latter can be done in one less insn
3329 one machine that have only two-operand insns or on which a
3330 constant cannot be the first operand. */
3331 if (integer_zerop (arg1
) && (code
== EQ_EXPR
|| code
== NE_EXPR
)
3332 && TREE_CODE (arg0
) == BIT_AND_EXPR
)
3334 if (TREE_CODE (TREE_OPERAND (arg0
, 0)) == LSHIFT_EXPR
3335 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0)))
3337 fold (build (code
, type
,
3338 build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
3340 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
3341 TREE_OPERAND (arg0
, 1),
3342 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)),
3343 convert (TREE_TYPE (arg0
),
3346 else if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == LSHIFT_EXPR
3347 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0
, 1), 0)))
3349 fold (build (code
, type
,
3350 build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
3352 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
3353 TREE_OPERAND (arg0
, 0),
3354 TREE_OPERAND (TREE_OPERAND (arg0
, 1), 1)),
3355 convert (TREE_TYPE (arg0
),
3360 /* If this is an NE comparison of zero with an AND of one, remove the
3361 comparison since the AND will give the correct value. */
3362 if (code
== NE_EXPR
&& integer_zerop (arg1
)
3363 && TREE_CODE (arg0
) == BIT_AND_EXPR
3364 && integer_onep (TREE_OPERAND (arg0
, 1)))
3365 return convert (type
, arg0
);
3367 /* If we have (A & C) == C where C is a power of 2, convert this into
3368 (A & C) != 0. Similarly for NE_EXPR. */
3369 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
3370 && TREE_CODE (arg0
) == BIT_AND_EXPR
3371 && integer_pow2p (TREE_OPERAND (arg0
, 1))
3372 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
3373 return build (code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
3374 arg0
, integer_zero_node
);
3376 /* Simplify comparison of something with itself. (For IEEE
3377 floating-point, we can only do some of these simplifications.) */
3378 if (operand_equal_p (arg0
, arg1
, 0))
3385 if (TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
)
3387 t
= build_int_2 (1, 0);
3388 TREE_TYPE (t
) = type
;
3392 TREE_SET_CODE (t
, code
);
3396 /* For NE, we can only do this simplification if integer. */
3397 if (TREE_CODE (TREE_TYPE (arg0
)) != INTEGER_TYPE
)
3399 /* ... fall through ... */
3402 t
= build_int_2 (0, 0);
3403 TREE_TYPE (t
) = type
;
3408 /* An unsigned comparison against 0 can be simplified. */
3409 if (integer_zerop (arg1
)
3410 && (TREE_CODE (TREE_TYPE (arg1
)) == INTEGER_TYPE
3411 || TREE_CODE (TREE_TYPE (arg1
)) == POINTER_TYPE
)
3412 && TREE_UNSIGNED (TREE_TYPE (arg1
)))
3414 switch (TREE_CODE (t
))
3418 TREE_SET_CODE (t
, NE_EXPR
);
3422 TREE_SET_CODE (t
, EQ_EXPR
);
3425 return omit_one_operand (integer_type_node
,
3426 integer_one_node
, arg0
);
3428 return omit_one_operand (integer_type_node
,
3429 integer_zero_node
, arg0
);
3433 /* If we are comparing an expression that just has comparisons
3434 of two integer values, arithmetic expressions of those comparisons,
3435 and constants, we can simplify it. There are only three cases
3436 to check: the two values can either be equal, the first can be
3437 greater, or the second can be greater. Fold the expression for
3438 those three values. Since each value must be 0 or 1, we have
3439 eight possibilities, each of which corresponds to the constant 0
3440 or 1 or one of the six possible comparisons.
3442 This handles common cases like (a > b) == 0 but also handles
3443 expressions like ((x > y) - (y > x)) > 0, which supposedly
3444 occur in macroized code. */
3446 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
3448 tree cval1
= 0, cval2
= 0;
3450 if (twoval_comparison_p (arg0
, &cval1
, &cval2
)
3451 /* Don't handle degenerate cases here; they should already
3452 have been handled anyway. */
3453 && cval1
!= 0 && cval2
!= 0
3454 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
3455 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
3456 && TREE_CODE (TREE_TYPE (cval1
)) == INTEGER_TYPE
3457 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
3458 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
3460 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
3461 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
3463 /* We can't just pass T to eval_subst in case cval1 or cval2
3464 was the same as ARG1. */
3467 = fold (build (code
, type
,
3468 eval_subst (arg0
, cval1
, maxval
, cval2
, minval
),
3471 = fold (build (code
, type
,
3472 eval_subst (arg0
, cval1
, maxval
, cval2
, maxval
),
3475 = fold (build (code
, type
,
3476 eval_subst (arg0
, cval1
, minval
, cval2
, maxval
),
3479 /* All three of these results should be 0 or 1. Confirm they
3480 are. Then use those values to select the proper code
3483 if ((integer_zerop (high_result
)
3484 || integer_onep (high_result
))
3485 && (integer_zerop (equal_result
)
3486 || integer_onep (equal_result
))
3487 && (integer_zerop (low_result
)
3488 || integer_onep (low_result
)))
3490 /* Make a 3-bit mask with the high-order bit being the
3491 value for `>', the next for '=', and the low for '<'. */
3492 switch ((integer_onep (high_result
) * 4)
3493 + (integer_onep (equal_result
) * 2)
3494 + integer_onep (low_result
))
3498 return omit_one_operand (type
, integer_zero_node
, arg0
);
3519 return omit_one_operand (type
, integer_one_node
, arg0
);
3522 return fold (build (code
, type
, cval1
, cval2
));
3527 /* If this is a comparison of a field, we may be able to simplify it. */
3528 if ((TREE_CODE (arg0
) == COMPONENT_REF
3529 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
3530 && (code
== EQ_EXPR
|| code
== NE_EXPR
)
3531 /* Handle the constant case even without -O
3532 to make sure the warnings are given. */
3533 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
3535 t1
= optimize_bit_field_compare (code
, type
, arg0
, arg1
);
3539 /* From here on, the only cases we handle are when the result is
3540 known to be a constant.
3542 To compute GT, swap the arguments and do LT.
3543 To compute GE, do LT and invert the result.
3544 To compute LE, swap the arguments, do LT and invert the result.
3545 To compute NE, do EQ and invert the result.
3547 Therefore, the code below must handle only EQ and LT. */
3549 if (code
== LE_EXPR
|| code
== GT_EXPR
)
3551 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
3552 code
= swap_tree_comparison (code
);
3555 /* Note that it is safe to invert for real values here because we
3556 will check below in the one case that it matters. */
3559 if (code
== NE_EXPR
|| code
== GE_EXPR
)
3562 code
= invert_tree_comparison (code
);
3565 /* Compute a result for LT or EQ if args permit;
3566 otherwise return T. */
3567 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
3569 if (code
== EQ_EXPR
)
3570 t1
= build_int_2 ((TREE_INT_CST_LOW (arg0
)
3571 == TREE_INT_CST_LOW (arg1
))
3572 && (TREE_INT_CST_HIGH (arg0
)
3573 == TREE_INT_CST_HIGH (arg1
)),
3576 t1
= build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0
))
3577 ? INT_CST_LT_UNSIGNED (arg0
, arg1
)
3578 : INT_CST_LT (arg0
, arg1
)),
3582 /* Assume a nonexplicit constant cannot equal an explicit one,
3583 since such code would be undefined anyway.
3584 Exception: on sysvr4, using #pragma weak,
3585 a label can come out as 0. */
3586 else if (TREE_CODE (arg1
) == INTEGER_CST
3587 && !integer_zerop (arg1
)
3588 && TREE_CONSTANT (arg0
)
3589 && TREE_CODE (arg0
) == ADDR_EXPR
3591 t1
= build_int_2 (0, 0);
3593 /* Two real constants can be compared explicitly. */
3594 else if (TREE_CODE (arg0
) == REAL_CST
&& TREE_CODE (arg1
) == REAL_CST
)
3596 /* If either operand is a NaN, the result is false with two
3597 exceptions: First, an NE_EXPR is true on NaNs, but that case
3598 is already handled correctly since we will be inverting the
3599 result for NE_EXPR. Second, if we had inverted a LE_EXPR
3600 or a GE_EXPR into a LT_EXPR, we must return true so that it
3601 will be inverted into false. */
3603 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0
))
3604 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
)))
3605 t1
= build_int_2 (invert
&& code
== LT_EXPR
, 0);
3607 else if (code
== EQ_EXPR
)
3608 t1
= build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
),
3609 TREE_REAL_CST (arg1
)),
3612 t1
= build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0
),
3613 TREE_REAL_CST (arg1
)),
3617 if (t1
== NULL_TREE
)
3621 TREE_INT_CST_LOW (t1
) ^= 1;
3623 TREE_TYPE (t1
) = type
;
3627 if (TREE_CODE (arg0
) == INTEGER_CST
)
3628 return TREE_OPERAND (t
, (integer_zerop (arg0
) ? 2 : 1));
3629 else if (operand_equal_p (arg1
, TREE_OPERAND (expr
, 2), 0))
3630 return omit_one_operand (type
, arg1
, arg0
);
3632 /* If the second operand is zero, invert the comparison and swap
3633 the second and third operands. Likewise if the second operand
3634 is constant and the third is not or if the third operand is
3635 equivalent to the first operand of the comparison. */
3637 if (integer_zerop (arg1
)
3638 || (TREE_CONSTANT (arg1
) && ! TREE_CONSTANT (TREE_OPERAND (t
, 2)))
3639 || (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
3640 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
3641 TREE_OPERAND (t
, 2),
3642 TREE_OPERAND (arg0
, 1))))
3644 /* See if this can be inverted. If it can't, possibly because
3645 it was a floating-point inequality comparison, don't do
3647 tem
= invert_truthvalue (arg0
);
3649 if (TREE_CODE (tem
) != TRUTH_NOT_EXPR
)
3651 arg0
= TREE_OPERAND (t
, 0) = tem
;
3652 TREE_OPERAND (t
, 1) = TREE_OPERAND (t
, 2);
3653 TREE_OPERAND (t
, 2) = arg1
;
3654 arg1
= TREE_OPERAND (t
, 1);
3658 /* If we have A op B ? A : C, we may be able to convert this to a
3659 simpler expression, depending on the operation and the values
3662 if (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
3663 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
3664 arg1
, TREE_OPERAND (arg0
, 1)))
3666 tree arg2
= TREE_OPERAND (t
, 2);
3667 enum tree_code comp_code
= TREE_CODE (arg0
);
3669 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
3670 depending on the comparison operation. */
3671 if (integer_zerop (TREE_OPERAND (arg0
, 1))
3672 && TREE_CODE (arg2
) == NEGATE_EXPR
3673 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
3677 return fold (build1 (NEGATE_EXPR
, type
, arg1
));
3679 return convert (type
, arg1
);
3682 return fold (build1 (ABS_EXPR
, type
, arg1
));
3685 return fold (build1 (NEGATE_EXPR
, type
,
3686 fold (build1 (ABS_EXPR
, type
, arg1
))));
3689 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
3692 if (integer_zerop (TREE_OPERAND (arg0
, 1)) && integer_zerop (arg2
))
3694 if (comp_code
== NE_EXPR
)
3695 return convert (type
, arg1
);
3696 else if (comp_code
== EQ_EXPR
)
3697 return convert (type
, integer_zero_node
);
3700 /* If this is A op B ? A : B, this is either A, B, min (A, B),
3701 or max (A, B), depending on the operation. */
3703 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 1),
3704 arg2
, TREE_OPERAND (arg0
, 0)))
3708 return convert (type
, arg2
);
3710 return convert (type
, arg1
);
3713 return fold (build (MIN_EXPR
, type
, arg1
, arg2
));
3716 return fold (build (MAX_EXPR
, type
, arg1
, arg2
));
3719 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
3720 we might still be able to simplify this. For example,
3721 if C1 is one less or one more than C2, this might have started
3722 out as a MIN or MAX and been transformed by this function. */
3724 if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
3725 && TREE_CODE (arg2
) == INTEGER_CST
)
3729 /* We can replace A with C1 in this case. */
3730 arg1
= TREE_OPERAND (t
, 1)
3731 = convert (type
, TREE_OPERAND (arg0
, 1));
3735 /* If C1 is C2 + 1, this is min(A, C2). */
3736 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
), 1)
3737 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3738 const_binop (PLUS_EXPR
, arg2
,
3739 integer_one_node
), 1))
3740 return fold (build (MIN_EXPR
, type
, arg1
, arg2
));
3744 /* If C1 is C2 - 1, this is min(A, C2). */
3745 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
), 1)
3746 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3747 const_binop (MINUS_EXPR
, arg2
,
3748 integer_one_node
), 1))
3749 return fold (build (MIN_EXPR
, type
, arg1
, arg2
));
3753 /* If C1 is C2 - 1, this is max(A, C2). */
3754 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
), 1)
3755 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3756 const_binop (MINUS_EXPR
, arg2
,
3757 integer_one_node
), 1))
3758 return fold (build (MAX_EXPR
, type
, arg1
, arg2
));
3762 /* If C1 is C2 + 1, this is max(A, C2). */
3763 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
), 1)
3764 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3765 const_binop (PLUS_EXPR
, arg2
,
3766 integer_one_node
), 1))
3767 return fold (build (MAX_EXPR
, type
, arg1
, arg2
));
3772 /* Convert A ? 1 : 0 to simply A. */
3773 if (integer_onep (TREE_OPERAND (t
, 1))
3774 && integer_zerop (TREE_OPERAND (t
, 2))
3775 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
3776 call to fold will try to move the conversion inside
3777 a COND, which will recurse. In that case, the COND_EXPR
3778 is probably the best choice, so leave it alone. */
3779 && type
== TREE_TYPE (arg0
))
3783 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
3784 operation is simply A & 2. */
3786 if (integer_zerop (TREE_OPERAND (t
, 2))
3787 && TREE_CODE (arg0
) == NE_EXPR
3788 && integer_zerop (TREE_OPERAND (arg0
, 1))
3789 && integer_pow2p (arg1
)
3790 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
3791 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
3793 return convert (type
, TREE_OPERAND (arg0
, 0));
3798 if (!TREE_SIDE_EFFECTS (arg0
))
3804 } /* switch (code) */
This page took 0.219278 seconds and 5 git commands to generate.