1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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. */
30 /* The entry points in this file are fold, size_int_wide, 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'.
42 force_fit_type takes a constant and prior overflow indicator, and
43 forces the value to fit the type. It returns an overflow indicator. */
56 static void encode
PARAMS ((HOST_WIDE_INT
*,
57 unsigned HOST_WIDE_INT
,
59 static void decode
PARAMS ((HOST_WIDE_INT
*,
60 unsigned HOST_WIDE_INT
*,
62 static tree negate_expr
PARAMS ((tree
));
63 static tree split_tree
PARAMS ((tree
, enum tree_code
, tree
*, tree
*,
65 static tree associate_trees
PARAMS ((tree
, tree
, enum tree_code
, tree
));
66 static tree int_const_binop
PARAMS ((enum tree_code
, tree
, tree
, int, int));
67 static void const_binop_1
PARAMS ((PTR
));
68 static tree const_binop
PARAMS ((enum tree_code
, tree
, tree
, int));
69 static void fold_convert_1
PARAMS ((PTR
));
70 static tree fold_convert
PARAMS ((tree
, tree
));
71 static enum tree_code invert_tree_comparison
PARAMS ((enum tree_code
));
72 static enum tree_code swap_tree_comparison
PARAMS ((enum tree_code
));
73 static int truth_value_p
PARAMS ((enum tree_code
));
74 static int operand_equal_for_comparison_p
PARAMS ((tree
, tree
, tree
));
75 static int twoval_comparison_p
PARAMS ((tree
, tree
*, tree
*, int *));
76 static tree eval_subst
PARAMS ((tree
, tree
, tree
, tree
, tree
));
77 static tree omit_one_operand
PARAMS ((tree
, tree
, tree
));
78 static tree pedantic_omit_one_operand
PARAMS ((tree
, tree
, tree
));
79 static tree distribute_bit_expr
PARAMS ((enum tree_code
, tree
, tree
, tree
));
80 static tree make_bit_field_ref
PARAMS ((tree
, tree
, int, int, int));
81 static tree optimize_bit_field_compare
PARAMS ((enum tree_code
, tree
,
83 static tree decode_field_reference
PARAMS ((tree
, HOST_WIDE_INT
*,
85 enum machine_mode
*, int *,
86 int *, tree
*, tree
*));
87 static int all_ones_mask_p
PARAMS ((tree
, int));
88 static int simple_operand_p
PARAMS ((tree
));
89 static tree range_binop
PARAMS ((enum tree_code
, tree
, tree
, int,
91 static tree make_range
PARAMS ((tree
, int *, tree
*, tree
*));
92 static tree build_range_check
PARAMS ((tree
, tree
, int, tree
, tree
));
93 static int merge_ranges
PARAMS ((int *, tree
*, tree
*, int, tree
, tree
,
95 static tree fold_range_test
PARAMS ((tree
));
96 static tree unextend
PARAMS ((tree
, int, int, tree
));
97 static tree fold_truthop
PARAMS ((enum tree_code
, tree
, tree
, tree
));
98 static tree optimize_minmax_comparison
PARAMS ((tree
));
99 static tree extract_muldiv
PARAMS ((tree
, tree
, enum tree_code
, tree
));
100 static tree strip_compound_expr
PARAMS ((tree
, tree
));
101 static int multiple_of_p
PARAMS ((tree
, tree
, tree
));
102 static tree constant_boolean_node
PARAMS ((int, tree
));
103 static int count_cond
PARAMS ((tree
, int));
104 static tree fold_binary_op_with_conditional_arg
105 PARAMS ((enum tree_code
, tree
, tree
, tree
, int));
108 #define BRANCH_COST 1
111 #if defined(HOST_EBCDIC)
112 /* bit 8 is significant in EBCDIC */
113 #define CHARMASK 0xff
115 #define CHARMASK 0x7f
118 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
119 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
120 and SUM1. Then this yields nonzero if overflow occurred during the
123 Overflow occurs if A and B have the same sign, but A and SUM differ in
124 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
126 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
128 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
129 We do that by representing the two-word integer in 4 words, with only
130 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
131 number. The value of the word is LOWPART + HIGHPART * BASE. */
134 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
135 #define HIGHPART(x) \
136 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
137 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
139 /* Unpack a two-word integer into 4 words.
140 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
141 WORDS points to the array of HOST_WIDE_INTs. */
144 encode (words
, low
, hi
)
145 HOST_WIDE_INT
*words
;
146 unsigned HOST_WIDE_INT low
;
149 words
[0] = LOWPART (low
);
150 words
[1] = HIGHPART (low
);
151 words
[2] = LOWPART (hi
);
152 words
[3] = HIGHPART (hi
);
155 /* Pack an array of 4 words into a two-word integer.
156 WORDS points to the array of words.
157 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
160 decode (words
, low
, hi
)
161 HOST_WIDE_INT
*words
;
162 unsigned HOST_WIDE_INT
*low
;
165 *low
= words
[0] + words
[1] * BASE
;
166 *hi
= words
[2] + words
[3] * BASE
;
169 /* Make the integer constant T valid for its type by setting to 0 or 1 all
170 the bits in the constant that don't belong in the type.
172 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
173 nonzero, a signed overflow has already occurred in calculating T, so
176 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
180 force_fit_type (t
, overflow
)
184 unsigned HOST_WIDE_INT low
;
188 if (TREE_CODE (t
) == REAL_CST
)
190 #ifdef CHECK_FLOAT_VALUE
191 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t
)), TREE_REAL_CST (t
),
197 else if (TREE_CODE (t
) != INTEGER_CST
)
200 low
= TREE_INT_CST_LOW (t
);
201 high
= TREE_INT_CST_HIGH (t
);
203 if (POINTER_TYPE_P (TREE_TYPE (t
)))
206 prec
= TYPE_PRECISION (TREE_TYPE (t
));
208 /* First clear all bits that are beyond the type's precision. */
210 if (prec
== 2 * HOST_BITS_PER_WIDE_INT
)
212 else if (prec
> HOST_BITS_PER_WIDE_INT
)
213 TREE_INT_CST_HIGH (t
)
214 &= ~((HOST_WIDE_INT
) (-1) << (prec
- HOST_BITS_PER_WIDE_INT
));
217 TREE_INT_CST_HIGH (t
) = 0;
218 if (prec
< HOST_BITS_PER_WIDE_INT
)
219 TREE_INT_CST_LOW (t
) &= ~((unsigned HOST_WIDE_INT
) (-1) << prec
);
222 /* Unsigned types do not suffer sign extension or overflow unless they
224 if (TREE_UNSIGNED (TREE_TYPE (t
))
225 && ! (TREE_CODE (TREE_TYPE (t
)) == INTEGER_TYPE
226 && TYPE_IS_SIZETYPE (TREE_TYPE (t
))))
229 /* If the value's sign bit is set, extend the sign. */
230 if (prec
!= 2 * HOST_BITS_PER_WIDE_INT
231 && (prec
> HOST_BITS_PER_WIDE_INT
232 ? 0 != (TREE_INT_CST_HIGH (t
)
234 << (prec
- HOST_BITS_PER_WIDE_INT
- 1)))
235 : 0 != (TREE_INT_CST_LOW (t
)
236 & ((unsigned HOST_WIDE_INT
) 1 << (prec
- 1)))))
238 /* Value is negative:
239 set to 1 all the bits that are outside this type's precision. */
240 if (prec
> HOST_BITS_PER_WIDE_INT
)
241 TREE_INT_CST_HIGH (t
)
242 |= ((HOST_WIDE_INT
) (-1) << (prec
- HOST_BITS_PER_WIDE_INT
));
245 TREE_INT_CST_HIGH (t
) = -1;
246 if (prec
< HOST_BITS_PER_WIDE_INT
)
247 TREE_INT_CST_LOW (t
) |= ((unsigned HOST_WIDE_INT
) (-1) << prec
);
251 /* Return nonzero if signed overflow occurred. */
253 ((overflow
| (low
^ TREE_INT_CST_LOW (t
)) | (high
^ TREE_INT_CST_HIGH (t
)))
257 /* Add two doubleword integers with doubleword result.
258 Each argument is given as two `HOST_WIDE_INT' pieces.
259 One argument is L1 and H1; the other, L2 and H2.
260 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
263 add_double (l1
, h1
, l2
, h2
, lv
, hv
)
264 unsigned HOST_WIDE_INT l1
, l2
;
265 HOST_WIDE_INT h1
, h2
;
266 unsigned HOST_WIDE_INT
*lv
;
269 unsigned HOST_WIDE_INT l
;
273 h
= h1
+ h2
+ (l
< l1
);
277 return OVERFLOW_SUM_SIGN (h1
, h2
, h
);
280 /* Negate a doubleword integer with doubleword result.
281 Return nonzero if the operation overflows, assuming it's signed.
282 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
283 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
286 neg_double (l1
, h1
, lv
, hv
)
287 unsigned HOST_WIDE_INT l1
;
289 unsigned HOST_WIDE_INT
*lv
;
296 return (*hv
& h1
) < 0;
306 /* Multiply two doubleword integers with doubleword result.
307 Return nonzero if the operation overflows, assuming it's signed.
308 Each argument is given as two `HOST_WIDE_INT' pieces.
309 One argument is L1 and H1; the other, L2 and H2.
310 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
313 mul_double (l1
, h1
, l2
, h2
, lv
, hv
)
314 unsigned HOST_WIDE_INT l1
, l2
;
315 HOST_WIDE_INT h1
, h2
;
316 unsigned HOST_WIDE_INT
*lv
;
319 HOST_WIDE_INT arg1
[4];
320 HOST_WIDE_INT arg2
[4];
321 HOST_WIDE_INT prod
[4 * 2];
322 register unsigned HOST_WIDE_INT carry
;
323 register int i
, j
, k
;
324 unsigned HOST_WIDE_INT toplow
, neglow
;
325 HOST_WIDE_INT tophigh
, neghigh
;
327 encode (arg1
, l1
, h1
);
328 encode (arg2
, l2
, h2
);
330 memset ((char *) prod
, 0, sizeof prod
);
332 for (i
= 0; i
< 4; i
++)
335 for (j
= 0; j
< 4; j
++)
338 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
339 carry
+= arg1
[i
] * arg2
[j
];
340 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
342 prod
[k
] = LOWPART (carry
);
343 carry
= HIGHPART (carry
);
348 decode (prod
, lv
, hv
); /* This ignores prod[4] through prod[4*2-1] */
350 /* Check for overflow by calculating the top half of the answer in full;
351 it should agree with the low half's sign bit. */
352 decode (prod
+ 4, &toplow
, &tophigh
);
355 neg_double (l2
, h2
, &neglow
, &neghigh
);
356 add_double (neglow
, neghigh
, toplow
, tophigh
, &toplow
, &tophigh
);
360 neg_double (l1
, h1
, &neglow
, &neghigh
);
361 add_double (neglow
, neghigh
, toplow
, tophigh
, &toplow
, &tophigh
);
363 return (*hv
< 0 ? ~(toplow
& tophigh
) : toplow
| tophigh
) != 0;
366 /* Shift the doubleword integer in L1, H1 left by COUNT places
367 keeping only PREC bits of result.
368 Shift right if COUNT is negative.
369 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
370 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
373 lshift_double (l1
, h1
, count
, prec
, lv
, hv
, arith
)
374 unsigned HOST_WIDE_INT l1
;
375 HOST_WIDE_INT h1
, count
;
377 unsigned HOST_WIDE_INT
*lv
;
383 rshift_double (l1
, h1
, -count
, prec
, lv
, hv
, arith
);
387 #ifdef SHIFT_COUNT_TRUNCATED
388 if (SHIFT_COUNT_TRUNCATED
)
392 if (count
>= 2 * HOST_BITS_PER_WIDE_INT
)
394 /* Shifting by the host word size is undefined according to the
395 ANSI standard, so we must handle this as a special case. */
399 else if (count
>= HOST_BITS_PER_WIDE_INT
)
401 *hv
= l1
<< (count
- HOST_BITS_PER_WIDE_INT
);
406 *hv
= (((unsigned HOST_WIDE_INT
) h1
<< count
)
407 | (l1
>> (HOST_BITS_PER_WIDE_INT
- count
- 1) >> 1));
412 /* Shift the doubleword integer in L1, H1 right by COUNT places
413 keeping only PREC bits of result. COUNT must be positive.
414 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
415 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
418 rshift_double (l1
, h1
, count
, prec
, lv
, hv
, arith
)
419 unsigned HOST_WIDE_INT l1
;
420 HOST_WIDE_INT h1
, count
;
421 unsigned int prec ATTRIBUTE_UNUSED
;
422 unsigned HOST_WIDE_INT
*lv
;
426 unsigned HOST_WIDE_INT signmask
;
429 ? -((unsigned HOST_WIDE_INT
) h1
>> (HOST_BITS_PER_WIDE_INT
- 1))
432 #ifdef SHIFT_COUNT_TRUNCATED
433 if (SHIFT_COUNT_TRUNCATED
)
437 if (count
>= 2 * HOST_BITS_PER_WIDE_INT
)
439 /* Shifting by the host word size is undefined according to the
440 ANSI standard, so we must handle this as a special case. */
444 else if (count
>= HOST_BITS_PER_WIDE_INT
)
447 *lv
= ((signmask
<< (2 * HOST_BITS_PER_WIDE_INT
- count
- 1) << 1)
448 | ((unsigned HOST_WIDE_INT
) h1
>> (count
- HOST_BITS_PER_WIDE_INT
)));
453 | ((unsigned HOST_WIDE_INT
) h1
<< (HOST_BITS_PER_WIDE_INT
- count
- 1) << 1));
454 *hv
= ((signmask
<< (HOST_BITS_PER_WIDE_INT
- count
))
455 | ((unsigned HOST_WIDE_INT
) h1
>> count
));
459 /* Rotate the doubleword integer in L1, H1 left by COUNT places
460 keeping only PREC bits of result.
461 Rotate right if COUNT is negative.
462 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
465 lrotate_double (l1
, h1
, count
, prec
, lv
, hv
)
466 unsigned HOST_WIDE_INT l1
;
467 HOST_WIDE_INT h1
, count
;
469 unsigned HOST_WIDE_INT
*lv
;
472 unsigned HOST_WIDE_INT s1l
, s2l
;
473 HOST_WIDE_INT s1h
, s2h
;
479 lshift_double (l1
, h1
, count
, prec
, &s1l
, &s1h
, 0);
480 rshift_double (l1
, h1
, prec
- count
, prec
, &s2l
, &s2h
, 0);
485 /* Rotate the doubleword integer in L1, H1 left by COUNT places
486 keeping only PREC bits of result. COUNT must be positive.
487 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
490 rrotate_double (l1
, h1
, count
, prec
, lv
, hv
)
491 unsigned HOST_WIDE_INT l1
;
492 HOST_WIDE_INT h1
, count
;
494 unsigned HOST_WIDE_INT
*lv
;
497 unsigned HOST_WIDE_INT s1l
, s2l
;
498 HOST_WIDE_INT s1h
, s2h
;
504 rshift_double (l1
, h1
, count
, prec
, &s1l
, &s1h
, 0);
505 lshift_double (l1
, h1
, prec
- count
, prec
, &s2l
, &s2h
, 0);
510 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
511 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
512 CODE is a tree code for a kind of division, one of
513 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
515 It controls how the quotient is rounded to a integer.
516 Return nonzero if the operation overflows.
517 UNS nonzero says do unsigned division. */
520 div_and_round_double (code
, uns
,
521 lnum_orig
, hnum_orig
, lden_orig
, hden_orig
,
522 lquo
, hquo
, lrem
, hrem
)
525 unsigned HOST_WIDE_INT lnum_orig
; /* num == numerator == dividend */
526 HOST_WIDE_INT hnum_orig
;
527 unsigned HOST_WIDE_INT lden_orig
; /* den == denominator == divisor */
528 HOST_WIDE_INT hden_orig
;
529 unsigned HOST_WIDE_INT
*lquo
, *lrem
;
530 HOST_WIDE_INT
*hquo
, *hrem
;
533 HOST_WIDE_INT num
[4 + 1]; /* extra element for scaling. */
534 HOST_WIDE_INT den
[4], quo
[4];
536 unsigned HOST_WIDE_INT work
;
537 unsigned HOST_WIDE_INT carry
= 0;
538 unsigned HOST_WIDE_INT lnum
= lnum_orig
;
539 HOST_WIDE_INT hnum
= hnum_orig
;
540 unsigned HOST_WIDE_INT lden
= lden_orig
;
541 HOST_WIDE_INT hden
= hden_orig
;
544 if (hden
== 0 && lden
== 0)
545 overflow
= 1, lden
= 1;
547 /* calculate quotient sign and convert operands to unsigned. */
553 /* (minimum integer) / (-1) is the only overflow case. */
554 if (neg_double (lnum
, hnum
, &lnum
, &hnum
)
555 && ((HOST_WIDE_INT
) lden
& hden
) == -1)
561 neg_double (lden
, hden
, &lden
, &hden
);
565 if (hnum
== 0 && hden
== 0)
566 { /* single precision */
568 /* This unsigned division rounds toward zero. */
574 { /* trivial case: dividend < divisor */
575 /* hden != 0 already checked. */
582 memset ((char *) quo
, 0, sizeof quo
);
584 memset ((char *) num
, 0, sizeof num
); /* to zero 9th element */
585 memset ((char *) den
, 0, sizeof den
);
587 encode (num
, lnum
, hnum
);
588 encode (den
, lden
, hden
);
590 /* Special code for when the divisor < BASE. */
591 if (hden
== 0 && lden
< (unsigned HOST_WIDE_INT
) BASE
)
593 /* hnum != 0 already checked. */
594 for (i
= 4 - 1; i
>= 0; i
--)
596 work
= num
[i
] + carry
* BASE
;
597 quo
[i
] = work
/ lden
;
603 /* Full double precision division,
604 with thanks to Don Knuth's "Seminumerical Algorithms". */
605 int num_hi_sig
, den_hi_sig
;
606 unsigned HOST_WIDE_INT quo_est
, scale
;
608 /* Find the highest non-zero divisor digit. */
609 for (i
= 4 - 1;; i
--)
616 /* Insure that the first digit of the divisor is at least BASE/2.
617 This is required by the quotient digit estimation algorithm. */
619 scale
= BASE
/ (den
[den_hi_sig
] + 1);
621 { /* scale divisor and dividend */
623 for (i
= 0; i
<= 4 - 1; i
++)
625 work
= (num
[i
] * scale
) + carry
;
626 num
[i
] = LOWPART (work
);
627 carry
= HIGHPART (work
);
632 for (i
= 0; i
<= 4 - 1; i
++)
634 work
= (den
[i
] * scale
) + carry
;
635 den
[i
] = LOWPART (work
);
636 carry
= HIGHPART (work
);
637 if (den
[i
] != 0) den_hi_sig
= i
;
644 for (i
= num_hi_sig
- den_hi_sig
- 1; i
>= 0; i
--)
646 /* Guess the next quotient digit, quo_est, by dividing the first
647 two remaining dividend digits by the high order quotient digit.
648 quo_est is never low and is at most 2 high. */
649 unsigned HOST_WIDE_INT tmp
;
651 num_hi_sig
= i
+ den_hi_sig
+ 1;
652 work
= num
[num_hi_sig
] * BASE
+ num
[num_hi_sig
- 1];
653 if (num
[num_hi_sig
] != den
[den_hi_sig
])
654 quo_est
= work
/ den
[den_hi_sig
];
658 /* Refine quo_est so it's usually correct, and at most one high. */
659 tmp
= work
- quo_est
* den
[den_hi_sig
];
661 && (den
[den_hi_sig
- 1] * quo_est
662 > (tmp
* BASE
+ num
[num_hi_sig
- 2])))
665 /* Try QUO_EST as the quotient digit, by multiplying the
666 divisor by QUO_EST and subtracting from the remaining dividend.
667 Keep in mind that QUO_EST is the I - 1st digit. */
670 for (j
= 0; j
<= den_hi_sig
; j
++)
672 work
= quo_est
* den
[j
] + carry
;
673 carry
= HIGHPART (work
);
674 work
= num
[i
+ j
] - LOWPART (work
);
675 num
[i
+ j
] = LOWPART (work
);
676 carry
+= HIGHPART (work
) != 0;
679 /* If quo_est was high by one, then num[i] went negative and
680 we need to correct things. */
681 if (num
[num_hi_sig
] < carry
)
684 carry
= 0; /* add divisor back in */
685 for (j
= 0; j
<= den_hi_sig
; j
++)
687 work
= num
[i
+ j
] + den
[j
] + carry
;
688 carry
= HIGHPART (work
);
689 num
[i
+ j
] = LOWPART (work
);
692 num
[num_hi_sig
] += carry
;
695 /* Store the quotient digit. */
700 decode (quo
, lquo
, hquo
);
703 /* if result is negative, make it so. */
705 neg_double (*lquo
, *hquo
, lquo
, hquo
);
707 /* compute trial remainder: rem = num - (quo * den) */
708 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
709 neg_double (*lrem
, *hrem
, lrem
, hrem
);
710 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
715 case TRUNC_MOD_EXPR
: /* round toward zero */
716 case EXACT_DIV_EXPR
: /* for this one, it shouldn't matter */
720 case FLOOR_MOD_EXPR
: /* round toward negative infinity */
721 if (quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio < 0 && rem != 0 */
724 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1,
732 case CEIL_MOD_EXPR
: /* round toward positive infinity */
733 if (!quo_neg
&& (*lrem
!= 0 || *hrem
!= 0)) /* ratio > 0 && rem != 0 */
735 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
743 case ROUND_MOD_EXPR
: /* round to closest integer */
745 unsigned HOST_WIDE_INT labs_rem
= *lrem
;
746 HOST_WIDE_INT habs_rem
= *hrem
;
747 unsigned HOST_WIDE_INT labs_den
= lden
, ltwice
;
748 HOST_WIDE_INT habs_den
= hden
, htwice
;
750 /* Get absolute values */
752 neg_double (*lrem
, *hrem
, &labs_rem
, &habs_rem
);
754 neg_double (lden
, hden
, &labs_den
, &habs_den
);
756 /* If (2 * abs (lrem) >= abs (lden)) */
757 mul_double ((HOST_WIDE_INT
) 2, (HOST_WIDE_INT
) 0,
758 labs_rem
, habs_rem
, <wice
, &htwice
);
760 if (((unsigned HOST_WIDE_INT
) habs_den
761 < (unsigned HOST_WIDE_INT
) htwice
)
762 || (((unsigned HOST_WIDE_INT
) habs_den
763 == (unsigned HOST_WIDE_INT
) htwice
)
764 && (labs_den
< ltwice
)))
768 add_double (*lquo
, *hquo
,
769 (HOST_WIDE_INT
) -1, (HOST_WIDE_INT
) -1, lquo
, hquo
);
772 add_double (*lquo
, *hquo
, (HOST_WIDE_INT
) 1, (HOST_WIDE_INT
) 0,
784 /* compute true remainder: rem = num - (quo * den) */
785 mul_double (*lquo
, *hquo
, lden_orig
, hden_orig
, lrem
, hrem
);
786 neg_double (*lrem
, *hrem
, lrem
, hrem
);
787 add_double (lnum_orig
, hnum_orig
, *lrem
, *hrem
, lrem
, hrem
);
791 #ifndef REAL_ARITHMETIC
792 /* Effectively truncate a real value to represent the nearest possible value
793 in a narrower mode. The result is actually represented in the same data
794 type as the argument, but its value is usually different.
796 A trap may occur during the FP operations and it is the responsibility
797 of the calling function to have a handler established. */
800 real_value_truncate (mode
, arg
)
801 enum machine_mode mode
;
804 return REAL_VALUE_TRUNCATE (mode
, arg
);
807 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
809 /* Check for infinity in an IEEE double precision number. */
815 /* The IEEE 64-bit double format. */
820 unsigned exponent
: 11;
821 unsigned mantissa1
: 20;
826 unsigned mantissa1
: 20;
827 unsigned exponent
: 11;
833 if (u
.big_endian
.sign
== 1)
836 return (u
.big_endian
.exponent
== 2047
837 && u
.big_endian
.mantissa1
== 0
838 && u
.big_endian
.mantissa2
== 0);
843 return (u
.little_endian
.exponent
== 2047
844 && u
.little_endian
.mantissa1
== 0
845 && u
.little_endian
.mantissa2
== 0);
849 /* Check whether an IEEE double precision number is a NaN. */
855 /* The IEEE 64-bit double format. */
860 unsigned exponent
: 11;
861 unsigned mantissa1
: 20;
866 unsigned mantissa1
: 20;
867 unsigned exponent
: 11;
873 if (u
.big_endian
.sign
== 1)
876 return (u
.big_endian
.exponent
== 2047
877 && (u
.big_endian
.mantissa1
!= 0
878 || u
.big_endian
.mantissa2
!= 0));
883 return (u
.little_endian
.exponent
== 2047
884 && (u
.little_endian
.mantissa1
!= 0
885 || u
.little_endian
.mantissa2
!= 0));
889 /* Check for a negative IEEE double precision number. */
895 /* The IEEE 64-bit double format. */
900 unsigned exponent
: 11;
901 unsigned mantissa1
: 20;
906 unsigned mantissa1
: 20;
907 unsigned exponent
: 11;
913 if (u
.big_endian
.sign
== 1)
916 return u
.big_endian
.sign
;
921 return u
.little_endian
.sign
;
924 #else /* Target not IEEE */
926 /* Let's assume other float formats don't have infinity.
927 (This can be overridden by redefining REAL_VALUE_ISINF.) */
931 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED
;
936 /* Let's assume other float formats don't have NaNs.
937 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
941 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED
;
946 /* Let's assume other float formats don't have minus zero.
947 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
955 #endif /* Target not IEEE */
957 /* Try to change R into its exact multiplicative inverse in machine mode
958 MODE. Return nonzero function value if successful. */
961 exact_real_inverse (mode
, r
)
962 enum machine_mode mode
;
971 #ifdef CHECK_FLOAT_VALUE
975 /* Usually disable if bounds checks are not reliable. */
976 if ((HOST_FLOAT_FORMAT
!= TARGET_FLOAT_FORMAT
) && !flag_pretend_float
)
979 /* Set array index to the less significant bits in the unions, depending
980 on the endian-ness of the host doubles.
981 Disable if insufficient information on the data structure. */
982 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
985 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
988 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
991 #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
996 if (setjmp (float_error
))
998 /* Don't do the optimization if there was an arithmetic error. */
1000 set_float_handler (NULL
);
1003 set_float_handler (float_error
);
1005 /* Domain check the argument. */
1010 #ifdef REAL_INFINITY
1011 if (REAL_VALUE_ISINF (x
.d
) || REAL_VALUE_ISNAN (x
.d
))
1015 /* Compute the reciprocal and check for numerical exactness.
1016 It is unnecessary to check all the significand bits to determine
1017 whether X is a power of 2. If X is not, then it is impossible for
1018 the bottom half significand of both X and 1/X to be all zero bits.
1019 Hence we ignore the data structure of the top half and examine only
1020 the low order bits of the two significands. */
1022 if (x
.i
[K
] != 0 || x
.i
[K
+ 1] != 0 || t
.i
[K
] != 0 || t
.i
[K
+ 1] != 0)
1025 /* Truncate to the required mode and range-check the result. */
1026 y
.d
= REAL_VALUE_TRUNCATE (mode
, t
.d
);
1027 #ifdef CHECK_FLOAT_VALUE
1029 if (CHECK_FLOAT_VALUE (mode
, y
.d
, i
))
1033 /* Fail if truncation changed the value. */
1034 if (y
.d
!= t
.d
|| y
.d
== 0.0)
1037 #ifdef REAL_INFINITY
1038 if (REAL_VALUE_ISINF (y
.d
) || REAL_VALUE_ISNAN (y
.d
))
1042 /* Output the reciprocal and return success flag. */
1043 set_float_handler (NULL
);
1048 /* Convert C99 hexadecimal floating point string constant S. Return
1049 real value type in mode MODE. This function uses the host computer's
1050 floating point arithmetic when there is no REAL_ARITHMETIC. */
1053 real_hex_to_f (s
, mode
)
1055 enum machine_mode mode
;
1059 unsigned HOST_WIDE_INT low
, high
;
1060 int shcount
, nrmcount
, k
;
1061 int sign
, expsign
, isfloat
;
1062 int lost
= 0;/* Nonzero low order bits shifted out and discarded. */
1063 int frexpon
= 0; /* Bits after the decimal point. */
1064 int expon
= 0; /* Value of exponent. */
1065 int decpt
= 0; /* How many decimal points. */
1066 int gotp
= 0; /* How many P's. */
1073 while (*p
== ' ' || *p
== '\t')
1076 /* Sign, if any, comes first. */
1084 /* The string is supposed to start with 0x or 0X . */
1088 if (*p
== 'x' || *p
== 'X')
1102 while ((c
= *p
) != '\0')
1104 if ((c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'F')
1105 || (c
>= 'a' && c
<= 'f'))
1108 if (k
>= 'a' && k
<= 'f')
1115 if ((high
& 0xf0000000) == 0)
1117 high
= (high
<< 4) + ((low
>> 28) & 15);
1118 low
= (low
<< 4) + k
;
1125 /* Record nonzero lost bits. */
1138 else if (c
== 'p' || c
== 'P')
1142 /* Sign of exponent. */
1149 /* Value of exponent.
1150 The exponent field is a decimal integer. */
1151 while (ISDIGIT (*p
))
1153 k
= (*p
++ & CHARMASK
) - '0';
1154 expon
= 10 * expon
+ k
;
1158 /* F suffix is ambiguous in the significand part
1159 so it must appear after the decimal exponent field. */
1160 if (*p
== 'f' || *p
== 'F')
1168 else if (c
== 'l' || c
== 'L')
1177 /* Abort if last character read was not legitimate. */
1179 if ((c
!= '\0' && c
!= ' ' && c
!= '\n' && c
!= '\r') || (decpt
> 1))
1182 /* There must be either one decimal point or one p. */
1183 if (decpt
== 0 && gotp
== 0)
1187 if (high
== 0 && low
== 0)
1199 /* Leave a high guard bit for carry-out. */
1200 if ((high
& 0x80000000) != 0)
1203 low
= (low
>> 1) | (high
<< 31);
1208 if ((high
& 0xffff8000) == 0)
1210 high
= (high
<< 16) + ((low
>> 16) & 0xffff);
1215 while ((high
& 0xc0000000) == 0)
1217 high
= (high
<< 1) + ((low
>> 31) & 1);
1222 if (isfloat
|| GET_MODE_SIZE (mode
) == UNITS_PER_WORD
)
1224 /* Keep 24 bits precision, bits 0x7fffff80.
1225 Rounding bit is 0x40. */
1226 lost
= lost
| low
| (high
& 0x3f);
1230 if ((high
& 0x80) || lost
)
1237 /* We need real.c to do long double formats, so here default
1238 to double precision. */
1239 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1241 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1242 Rounding bit is low word 0x200. */
1243 lost
= lost
| (low
& 0x1ff);
1246 if ((low
& 0x400) || lost
)
1248 low
= (low
+ 0x200) & 0xfffffc00;
1255 /* Assume it's a VAX with 56-bit significand,
1256 bits 0x7fffffff ffffff80. */
1257 lost
= lost
| (low
& 0x7f);
1260 if ((low
& 0x80) || lost
)
1262 low
= (low
+ 0x40) & 0xffffff80;
1272 ip
= REAL_VALUE_LDEXP (ip
, 32) + (double) low
;
1273 /* Apply shifts and exponent value as power of 2. */
1274 ip
= REAL_VALUE_LDEXP (ip
, expon
- (nrmcount
+ frexpon
));
1281 #endif /* no REAL_ARITHMETIC */
1283 /* Given T, an expression, return the negation of T. Allow for T to be
1284 null, in which case return null. */
1296 type
= TREE_TYPE (t
);
1297 STRIP_SIGN_NOPS (t
);
1299 switch (TREE_CODE (t
))
1303 if (! TREE_UNSIGNED (type
)
1304 && 0 != (tem
= fold (build1 (NEGATE_EXPR
, type
, t
)))
1305 && ! TREE_OVERFLOW (tem
))
1310 return convert (type
, TREE_OPERAND (t
, 0));
1313 /* - (A - B) -> B - A */
1314 if (! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
)
1315 return convert (type
,
1316 fold (build (MINUS_EXPR
, TREE_TYPE (t
),
1317 TREE_OPERAND (t
, 1),
1318 TREE_OPERAND (t
, 0))));
1325 return convert (type
, build1 (NEGATE_EXPR
, TREE_TYPE (t
), t
));
1328 /* Split a tree IN into a constant, literal and variable parts that could be
1329 combined with CODE to make IN. "constant" means an expression with
1330 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1331 commutative arithmetic operation. Store the constant part into *CONP,
1332 the literal in &LITP and return the variable part. If a part isn't
1333 present, set it to null. If the tree does not decompose in this way,
1334 return the entire tree as the variable part and the other parts as null.
1336 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1337 case, we negate an operand that was subtracted. If NEGATE_P is true, we
1338 are negating all of IN.
1340 If IN is itself a literal or constant, return it as appropriate.
1342 Note that we do not guarantee that any of the three values will be the
1343 same type as IN, but they will have the same signedness and mode. */
1346 split_tree (in
, code
, conp
, litp
, negate_p
)
1348 enum tree_code code
;
1357 /* Strip any conversions that don't change the machine mode or signedness. */
1358 STRIP_SIGN_NOPS (in
);
1360 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
)
1362 else if (TREE_CODE (in
) == code
1363 || (! FLOAT_TYPE_P (TREE_TYPE (in
))
1364 /* We can associate addition and subtraction together (even
1365 though the C standard doesn't say so) for integers because
1366 the value is not affected. For reals, the value might be
1367 affected, so we can't. */
1368 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
1369 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
1371 tree op0
= TREE_OPERAND (in
, 0);
1372 tree op1
= TREE_OPERAND (in
, 1);
1373 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
1374 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
1376 /* First see if either of the operands is a literal, then a constant. */
1377 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
)
1378 *litp
= op0
, op0
= 0;
1379 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
)
1380 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
1382 if (op0
!= 0 && TREE_CONSTANT (op0
))
1383 *conp
= op0
, op0
= 0;
1384 else if (op1
!= 0 && TREE_CONSTANT (op1
))
1385 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
1387 /* If we haven't dealt with either operand, this is not a case we can
1388 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1389 if (op0
!= 0 && op1
!= 0)
1394 var
= op1
, neg_var_p
= neg1_p
;
1396 /* Now do any needed negations. */
1397 if (neg_litp_p
) *litp
= negate_expr (*litp
);
1398 if (neg_conp_p
) *conp
= negate_expr (*conp
);
1399 if (neg_var_p
) var
= negate_expr (var
);
1401 else if (TREE_CONSTANT (in
))
1408 var
= negate_expr (var
);
1409 *conp
= negate_expr (*conp
);
1410 *litp
= negate_expr (*litp
);
1416 /* Re-associate trees split by the above function. T1 and T2 are either
1417 expressions to associate or null. Return the new expression, if any. If
1418 we build an operation, do it in TYPE and with CODE, except if CODE is a
1419 MINUS_EXPR, in which case we use PLUS_EXPR since split_tree will already
1420 have taken care of the negations. */
1423 associate_trees (t1
, t2
, code
, type
)
1425 enum tree_code code
;
1433 if (code
== MINUS_EXPR
)
1436 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1437 try to fold this since we will have infinite recursion. But do
1438 deal with any NEGATE_EXPRs. */
1439 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
1440 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
1442 if (TREE_CODE (t1
) == NEGATE_EXPR
)
1443 return build (MINUS_EXPR
, type
, convert (type
, t2
),
1444 convert (type
, TREE_OPERAND (t1
, 0)));
1445 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
1446 return build (MINUS_EXPR
, type
, convert (type
, t1
),
1447 convert (type
, TREE_OPERAND (t2
, 0)));
1449 return build (code
, type
, convert (type
, t1
), convert (type
, t2
));
1452 return fold (build (code
, type
, convert (type
, t1
), convert (type
, t2
)));
1455 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1456 to produce a new constant.
1458 If NOTRUNC is nonzero, do not truncate the result to fit the data type.
1459 If FORSIZE is nonzero, compute overflow for unsigned types. */
1462 int_const_binop (code
, arg1
, arg2
, notrunc
, forsize
)
1463 enum tree_code code
;
1464 register tree arg1
, arg2
;
1465 int notrunc
, forsize
;
1467 unsigned HOST_WIDE_INT int1l
, int2l
;
1468 HOST_WIDE_INT int1h
, int2h
;
1469 unsigned HOST_WIDE_INT low
;
1471 unsigned HOST_WIDE_INT garbagel
;
1472 HOST_WIDE_INT garbageh
;
1474 int uns
= TREE_UNSIGNED (TREE_TYPE (arg1
));
1476 int no_overflow
= 0;
1478 int1l
= TREE_INT_CST_LOW (arg1
);
1479 int1h
= TREE_INT_CST_HIGH (arg1
);
1480 int2l
= TREE_INT_CST_LOW (arg2
);
1481 int2h
= TREE_INT_CST_HIGH (arg2
);
1486 low
= int1l
| int2l
, hi
= int1h
| int2h
;
1490 low
= int1l
^ int2l
, hi
= int1h
^ int2h
;
1494 low
= int1l
& int2l
, hi
= int1h
& int2h
;
1497 case BIT_ANDTC_EXPR
:
1498 low
= int1l
& ~int2l
, hi
= int1h
& ~int2h
;
1504 /* It's unclear from the C standard whether shifts can overflow.
1505 The following code ignores overflow; perhaps a C standard
1506 interpretation ruling is needed. */
1507 lshift_double (int1l
, int1h
, int2l
, TYPE_PRECISION (TREE_TYPE (arg1
)),
1515 lrotate_double (int1l
, int1h
, int2l
, TYPE_PRECISION (TREE_TYPE (arg1
)),
1520 overflow
= add_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1524 neg_double (int2l
, int2h
, &low
, &hi
);
1525 add_double (int1l
, int1h
, low
, hi
, &low
, &hi
);
1526 overflow
= OVERFLOW_SUM_SIGN (hi
, int2h
, int1h
);
1530 overflow
= mul_double (int1l
, int1h
, int2l
, int2h
, &low
, &hi
);
1533 case TRUNC_DIV_EXPR
:
1534 case FLOOR_DIV_EXPR
: case CEIL_DIV_EXPR
:
1535 case EXACT_DIV_EXPR
:
1536 /* This is a shortcut for a common special case. */
1537 if (int2h
== 0 && (HOST_WIDE_INT
) int2l
> 0
1538 && ! TREE_CONSTANT_OVERFLOW (arg1
)
1539 && ! TREE_CONSTANT_OVERFLOW (arg2
)
1540 && int1h
== 0 && (HOST_WIDE_INT
) int1l
>= 0)
1542 if (code
== CEIL_DIV_EXPR
)
1545 low
= int1l
/ int2l
, hi
= 0;
1549 /* ... fall through ... */
1551 case ROUND_DIV_EXPR
:
1552 if (int2h
== 0 && int2l
== 1)
1554 low
= int1l
, hi
= int1h
;
1557 if (int1l
== int2l
&& int1h
== int2h
1558 && ! (int1l
== 0 && int1h
== 0))
1563 overflow
= div_and_round_double (code
, uns
,
1564 int1l
, int1h
, int2l
, int2h
,
1565 &low
, &hi
, &garbagel
, &garbageh
);
1568 case TRUNC_MOD_EXPR
:
1569 case FLOOR_MOD_EXPR
: case CEIL_MOD_EXPR
:
1570 /* This is a shortcut for a common special case. */
1571 if (int2h
== 0 && (HOST_WIDE_INT
) int2l
> 0
1572 && ! TREE_CONSTANT_OVERFLOW (arg1
)
1573 && ! TREE_CONSTANT_OVERFLOW (arg2
)
1574 && int1h
== 0 && (HOST_WIDE_INT
) int1l
>= 0)
1576 if (code
== CEIL_MOD_EXPR
)
1578 low
= int1l
% int2l
, hi
= 0;
1582 /* ... fall through ... */
1584 case ROUND_MOD_EXPR
:
1585 overflow
= div_and_round_double (code
, uns
,
1586 int1l
, int1h
, int2l
, int2h
,
1587 &garbagel
, &garbageh
, &low
, &hi
);
1593 low
= (((unsigned HOST_WIDE_INT
) int1h
1594 < (unsigned HOST_WIDE_INT
) int2h
)
1595 || (((unsigned HOST_WIDE_INT
) int1h
1596 == (unsigned HOST_WIDE_INT
) int2h
)
1599 low
= (int1h
< int2h
1600 || (int1h
== int2h
&& int1l
< int2l
));
1602 if (low
== (code
== MIN_EXPR
))
1603 low
= int1l
, hi
= int1h
;
1605 low
= int2l
, hi
= int2h
;
1612 if (forsize
&& hi
== 0 && low
< 10000
1613 && overflow
== 0 && ! TREE_OVERFLOW (arg1
) && ! TREE_OVERFLOW (arg2
))
1614 return size_int_type_wide (low
, TREE_TYPE (arg1
));
1617 t
= build_int_2 (low
, hi
);
1618 TREE_TYPE (t
) = TREE_TYPE (arg1
);
1622 = ((notrunc
? (!uns
|| forsize
) && overflow
1623 : force_fit_type (t
, (!uns
|| forsize
) && overflow
) && ! no_overflow
)
1624 | TREE_OVERFLOW (arg1
)
1625 | TREE_OVERFLOW (arg2
));
1627 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1628 So check if force_fit_type truncated the value. */
1630 && ! TREE_OVERFLOW (t
)
1631 && (TREE_INT_CST_HIGH (t
) != hi
1632 || TREE_INT_CST_LOW (t
) != low
))
1633 TREE_OVERFLOW (t
) = 1;
1635 TREE_CONSTANT_OVERFLOW (t
) = (TREE_OVERFLOW (t
)
1636 | TREE_CONSTANT_OVERFLOW (arg1
)
1637 | TREE_CONSTANT_OVERFLOW (arg2
));
1641 /* Define input and output argument for const_binop_1. */
1644 enum tree_code code
; /* Input: tree code for operation. */
1645 tree type
; /* Input: tree type for operation. */
1646 REAL_VALUE_TYPE d1
, d2
; /* Input: floating point operands. */
1647 tree t
; /* Output: constant for result. */
1650 /* Do the real arithmetic for const_binop while protected by a
1651 float overflow handler. */
1654 const_binop_1 (data
)
1657 struct cb_args
*args
= (struct cb_args
*) data
;
1658 REAL_VALUE_TYPE value
;
1660 #ifdef REAL_ARITHMETIC
1661 REAL_ARITHMETIC (value
, args
->code
, args
->d1
, args
->d2
);
1666 value
= args
->d1
+ args
->d2
;
1670 value
= args
->d1
- args
->d2
;
1674 value
= args
->d1
* args
->d2
;
1678 #ifndef REAL_INFINITY
1683 value
= args
->d1
/ args
->d2
;
1687 value
= MIN (args
->d1
, args
->d2
);
1691 value
= MAX (args
->d1
, args
->d2
);
1697 #endif /* no REAL_ARITHMETIC */
1700 = build_real (args
->type
,
1701 real_value_truncate (TYPE_MODE (args
->type
), value
));
1704 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1705 constant. We assume ARG1 and ARG2 have the same data type, or at least
1706 are the same kind of constant and the same machine mode.
1708 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1711 const_binop (code
, arg1
, arg2
, notrunc
)
1712 enum tree_code code
;
1713 register tree arg1
, arg2
;
1719 if (TREE_CODE (arg1
) == INTEGER_CST
)
1720 return int_const_binop (code
, arg1
, arg2
, notrunc
, 0);
1722 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1723 if (TREE_CODE (arg1
) == REAL_CST
)
1729 struct cb_args args
;
1731 d1
= TREE_REAL_CST (arg1
);
1732 d2
= TREE_REAL_CST (arg2
);
1734 /* If either operand is a NaN, just return it. Otherwise, set up
1735 for floating-point trap; we return an overflow. */
1736 if (REAL_VALUE_ISNAN (d1
))
1738 else if (REAL_VALUE_ISNAN (d2
))
1741 /* Setup input for const_binop_1() */
1742 args
.type
= TREE_TYPE (arg1
);
1747 if (do_float_handler (const_binop_1
, (PTR
) &args
))
1748 /* Receive output from const_binop_1. */
1752 /* We got an exception from const_binop_1. */
1753 t
= copy_node (arg1
);
1758 = (force_fit_type (t
, overflow
)
1759 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
));
1760 TREE_CONSTANT_OVERFLOW (t
)
1762 | TREE_CONSTANT_OVERFLOW (arg1
)
1763 | TREE_CONSTANT_OVERFLOW (arg2
);
1766 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1767 if (TREE_CODE (arg1
) == COMPLEX_CST
)
1769 register tree type
= TREE_TYPE (arg1
);
1770 register tree r1
= TREE_REALPART (arg1
);
1771 register tree i1
= TREE_IMAGPART (arg1
);
1772 register tree r2
= TREE_REALPART (arg2
);
1773 register tree i2
= TREE_IMAGPART (arg2
);
1779 t
= build_complex (type
,
1780 const_binop (PLUS_EXPR
, r1
, r2
, notrunc
),
1781 const_binop (PLUS_EXPR
, i1
, i2
, notrunc
));
1785 t
= build_complex (type
,
1786 const_binop (MINUS_EXPR
, r1
, r2
, notrunc
),
1787 const_binop (MINUS_EXPR
, i1
, i2
, notrunc
));
1791 t
= build_complex (type
,
1792 const_binop (MINUS_EXPR
,
1793 const_binop (MULT_EXPR
,
1795 const_binop (MULT_EXPR
,
1798 const_binop (PLUS_EXPR
,
1799 const_binop (MULT_EXPR
,
1801 const_binop (MULT_EXPR
,
1808 register tree magsquared
1809 = const_binop (PLUS_EXPR
,
1810 const_binop (MULT_EXPR
, r2
, r2
, notrunc
),
1811 const_binop (MULT_EXPR
, i2
, i2
, notrunc
),
1814 t
= build_complex (type
,
1816 (INTEGRAL_TYPE_P (TREE_TYPE (r1
))
1817 ? TRUNC_DIV_EXPR
: RDIV_EXPR
,
1818 const_binop (PLUS_EXPR
,
1819 const_binop (MULT_EXPR
, r1
, r2
,
1821 const_binop (MULT_EXPR
, i1
, i2
,
1824 magsquared
, notrunc
),
1826 (INTEGRAL_TYPE_P (TREE_TYPE (r1
))
1827 ? TRUNC_DIV_EXPR
: RDIV_EXPR
,
1828 const_binop (MINUS_EXPR
,
1829 const_binop (MULT_EXPR
, i1
, r2
,
1831 const_binop (MULT_EXPR
, r1
, i2
,
1834 magsquared
, notrunc
));
1846 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1847 bits are given by NUMBER and of the sizetype represented by KIND. */
1850 size_int_wide (number
, kind
)
1851 HOST_WIDE_INT number
;
1852 enum size_type_kind kind
;
1854 return size_int_type_wide (number
, sizetype_tab
[(int) kind
]);
1857 /* Likewise, but the desired type is specified explicitly. */
1860 size_int_type_wide (number
, type
)
1861 HOST_WIDE_INT number
;
1864 /* Type-size nodes already made for small sizes. */
1865 static tree size_table
[2048 + 1];
1866 static int init_p
= 0;
1871 ggc_add_tree_root ((tree
*) size_table
,
1872 sizeof size_table
/ sizeof (tree
));
1876 /* If this is a positive number that fits in the table we use to hold
1877 cached entries, see if it is already in the table and put it there
1879 if (number
>= 0 && number
< (int) ARRAY_SIZE (size_table
))
1881 if (size_table
[number
] != 0)
1882 for (t
= size_table
[number
]; t
!= 0; t
= TREE_CHAIN (t
))
1883 if (TREE_TYPE (t
) == type
)
1886 t
= build_int_2 (number
, 0);
1887 TREE_TYPE (t
) = type
;
1888 TREE_CHAIN (t
) = size_table
[number
];
1889 size_table
[number
] = t
;
1894 t
= build_int_2 (number
, number
< 0 ? -1 : 0);
1895 TREE_TYPE (t
) = type
;
1896 TREE_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (t
) = force_fit_type (t
, 0);
1900 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1901 is a tree code. The type of the result is taken from the operands.
1902 Both must be the same type integer type and it must be a size type.
1903 If the operands are constant, so is the result. */
1906 size_binop (code
, arg0
, arg1
)
1907 enum tree_code code
;
1910 tree type
= TREE_TYPE (arg0
);
1912 if (TREE_CODE (type
) != INTEGER_TYPE
|| ! TYPE_IS_SIZETYPE (type
)
1913 || type
!= TREE_TYPE (arg1
))
1916 /* Handle the special case of two integer constants faster. */
1917 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1919 /* And some specific cases even faster than that. */
1920 if (code
== PLUS_EXPR
&& integer_zerop (arg0
))
1922 else if ((code
== MINUS_EXPR
|| code
== PLUS_EXPR
)
1923 && integer_zerop (arg1
))
1925 else if (code
== MULT_EXPR
&& integer_onep (arg0
))
1928 /* Handle general case of two integer constants. */
1929 return int_const_binop (code
, arg0
, arg1
, 0, 1);
1932 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1933 return error_mark_node
;
1935 return fold (build (code
, type
, arg0
, arg1
));
1938 /* Given two values, either both of sizetype or both of bitsizetype,
1939 compute the difference between the two values. Return the value
1940 in signed type corresponding to the type of the operands. */
1943 size_diffop (arg0
, arg1
)
1946 tree type
= TREE_TYPE (arg0
);
1949 if (TREE_CODE (type
) != INTEGER_TYPE
|| ! TYPE_IS_SIZETYPE (type
)
1950 || type
!= TREE_TYPE (arg1
))
1953 /* If the type is already signed, just do the simple thing. */
1954 if (! TREE_UNSIGNED (type
))
1955 return size_binop (MINUS_EXPR
, arg0
, arg1
);
1957 ctype
= (type
== bitsizetype
|| type
== ubitsizetype
1958 ? sbitsizetype
: ssizetype
);
1960 /* If either operand is not a constant, do the conversions to the signed
1961 type and subtract. The hardware will do the right thing with any
1962 overflow in the subtraction. */
1963 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1964 return size_binop (MINUS_EXPR
, convert (ctype
, arg0
),
1965 convert (ctype
, arg1
));
1967 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1968 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1969 overflow) and negate (which can't either). Special-case a result
1970 of zero while we're here. */
1971 if (tree_int_cst_equal (arg0
, arg1
))
1972 return convert (ctype
, integer_zero_node
);
1973 else if (tree_int_cst_lt (arg1
, arg0
))
1974 return convert (ctype
, size_binop (MINUS_EXPR
, arg0
, arg1
));
1976 return size_binop (MINUS_EXPR
, convert (ctype
, integer_zero_node
),
1977 convert (ctype
, size_binop (MINUS_EXPR
, arg1
, arg0
)));
1980 /* This structure is used to communicate arguments to fold_convert_1. */
1983 tree arg1
; /* Input: value to convert. */
1984 tree type
; /* Input: type to convert value to. */
1985 tree t
; /* Ouput: result of conversion. */
1988 /* Function to convert floating-point constants, protected by floating
1989 point exception handler. */
1992 fold_convert_1 (data
)
1995 struct fc_args
*args
= (struct fc_args
*) data
;
1997 args
->t
= build_real (args
->type
,
1998 real_value_truncate (TYPE_MODE (args
->type
),
1999 TREE_REAL_CST (args
->arg1
)));
2002 /* Given T, a tree representing type conversion of ARG1, a constant,
2003 return a constant tree representing the result of conversion. */
2006 fold_convert (t
, arg1
)
2010 register tree type
= TREE_TYPE (t
);
2013 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
))
2015 if (TREE_CODE (arg1
) == INTEGER_CST
)
2017 /* If we would build a constant wider than GCC supports,
2018 leave the conversion unfolded. */
2019 if (TYPE_PRECISION (type
) > 2 * HOST_BITS_PER_WIDE_INT
)
2022 /* If we are trying to make a sizetype for a small integer, use
2023 size_int to pick up cached types to reduce duplicate nodes. */
2024 if (TREE_CODE (type
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (type
)
2025 && !TREE_CONSTANT_OVERFLOW (arg1
)
2026 && compare_tree_int (arg1
, 10000) < 0)
2027 return size_int_type_wide (TREE_INT_CST_LOW (arg1
), type
);
2029 /* Given an integer constant, make new constant with new type,
2030 appropriately sign-extended or truncated. */
2031 t
= build_int_2 (TREE_INT_CST_LOW (arg1
),
2032 TREE_INT_CST_HIGH (arg1
));
2033 TREE_TYPE (t
) = type
;
2034 /* Indicate an overflow if (1) ARG1 already overflowed,
2035 or (2) force_fit_type indicates an overflow.
2036 Tell force_fit_type that an overflow has already occurred
2037 if ARG1 is a too-large unsigned value and T is signed.
2038 But don't indicate an overflow if converting a pointer. */
2040 = ((force_fit_type (t
,
2041 (TREE_INT_CST_HIGH (arg1
) < 0
2042 && (TREE_UNSIGNED (type
)
2043 < TREE_UNSIGNED (TREE_TYPE (arg1
)))))
2044 && ! POINTER_TYPE_P (TREE_TYPE (arg1
)))
2045 || TREE_OVERFLOW (arg1
));
2046 TREE_CONSTANT_OVERFLOW (t
)
2047 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg1
);
2049 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2050 else if (TREE_CODE (arg1
) == REAL_CST
)
2052 /* Don't initialize these, use assignments.
2053 Initialized local aggregates don't work on old compilers. */
2057 tree type1
= TREE_TYPE (arg1
);
2060 x
= TREE_REAL_CST (arg1
);
2061 l
= real_value_from_int_cst (type1
, TYPE_MIN_VALUE (type
));
2063 no_upper_bound
= (TYPE_MAX_VALUE (type
) == NULL
);
2064 if (!no_upper_bound
)
2065 u
= real_value_from_int_cst (type1
, TYPE_MAX_VALUE (type
));
2067 /* See if X will be in range after truncation towards 0.
2068 To compensate for truncation, move the bounds away from 0,
2069 but reject if X exactly equals the adjusted bounds. */
2070 #ifdef REAL_ARITHMETIC
2071 REAL_ARITHMETIC (l
, MINUS_EXPR
, l
, dconst1
);
2072 if (!no_upper_bound
)
2073 REAL_ARITHMETIC (u
, PLUS_EXPR
, u
, dconst1
);
2076 if (!no_upper_bound
)
2079 /* If X is a NaN, use zero instead and show we have an overflow.
2080 Otherwise, range check. */
2081 if (REAL_VALUE_ISNAN (x
))
2082 overflow
= 1, x
= dconst0
;
2083 else if (! (REAL_VALUES_LESS (l
, x
)
2085 && REAL_VALUES_LESS (x
, u
)))
2088 #ifndef REAL_ARITHMETIC
2090 HOST_WIDE_INT low
, high
;
2091 HOST_WIDE_INT half_word
2092 = (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
/ 2);
2097 high
= (HOST_WIDE_INT
) (x
/ half_word
/ half_word
);
2098 x
-= (REAL_VALUE_TYPE
) high
* half_word
* half_word
;
2099 if (x
>= (REAL_VALUE_TYPE
) half_word
* half_word
/ 2)
2101 low
= x
- (REAL_VALUE_TYPE
) half_word
* half_word
/ 2;
2102 low
|= (HOST_WIDE_INT
) -1 << (HOST_BITS_PER_WIDE_INT
- 1);
2105 low
= (HOST_WIDE_INT
) x
;
2106 if (TREE_REAL_CST (arg1
) < 0)
2107 neg_double (low
, high
, &low
, &high
);
2108 t
= build_int_2 (low
, high
);
2112 HOST_WIDE_INT low
, high
;
2113 REAL_VALUE_TO_INT (&low
, &high
, x
);
2114 t
= build_int_2 (low
, high
);
2117 TREE_TYPE (t
) = type
;
2119 = TREE_OVERFLOW (arg1
) | force_fit_type (t
, overflow
);
2120 TREE_CONSTANT_OVERFLOW (t
)
2121 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg1
);
2123 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2124 TREE_TYPE (t
) = type
;
2126 else if (TREE_CODE (type
) == REAL_TYPE
)
2128 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2129 if (TREE_CODE (arg1
) == INTEGER_CST
)
2130 return build_real_from_int_cst (type
, arg1
);
2131 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2132 if (TREE_CODE (arg1
) == REAL_CST
)
2134 struct fc_args args
;
2136 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
)))
2139 TREE_TYPE (arg1
) = type
;
2143 /* Setup input for fold_convert_1() */
2147 if (do_float_handler (fold_convert_1
, (PTR
) &args
))
2149 /* Receive output from fold_convert_1() */
2154 /* We got an exception from fold_convert_1() */
2156 t
= copy_node (arg1
);
2160 = TREE_OVERFLOW (arg1
) | force_fit_type (t
, overflow
);
2161 TREE_CONSTANT_OVERFLOW (t
)
2162 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg1
);
2166 TREE_CONSTANT (t
) = 1;
2170 /* Return an expr equal to X but certainly not valid as an lvalue. */
2178 /* These things are certainly not lvalues. */
2179 if (TREE_CODE (x
) == NON_LVALUE_EXPR
2180 || TREE_CODE (x
) == INTEGER_CST
2181 || TREE_CODE (x
) == REAL_CST
2182 || TREE_CODE (x
) == STRING_CST
2183 || TREE_CODE (x
) == ADDR_EXPR
)
2186 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2187 TREE_CONSTANT (result
) = TREE_CONSTANT (x
);
2191 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2192 Zero means allow extended lvalues. */
2194 int pedantic_lvalues
;
2196 /* When pedantic, return an expr equal to X but certainly not valid as a
2197 pedantic lvalue. Otherwise, return X. */
2200 pedantic_non_lvalue (x
)
2203 if (pedantic_lvalues
)
2204 return non_lvalue (x
);
2209 /* Given a tree comparison code, return the code that is the logical inverse
2210 of the given code. It is not safe to do this for floating-point
2211 comparisons, except for NE_EXPR and EQ_EXPR. */
2213 static enum tree_code
2214 invert_tree_comparison (code
)
2215 enum tree_code code
;
2236 /* Similar, but return the comparison that results if the operands are
2237 swapped. This is safe for floating-point. */
2239 static enum tree_code
2240 swap_tree_comparison (code
)
2241 enum tree_code code
;
2261 /* Return nonzero if CODE is a tree code that represents a truth value. */
2264 truth_value_p (code
)
2265 enum tree_code code
;
2267 return (TREE_CODE_CLASS (code
) == '<'
2268 || code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
2269 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
2270 || code
== TRUTH_XOR_EXPR
|| code
== TRUTH_NOT_EXPR
);
2273 /* Return nonzero if two operands are necessarily equal.
2274 If ONLY_CONST is non-zero, only return non-zero for constants.
2275 This function tests whether the operands are indistinguishable;
2276 it does not test whether they are equal using C's == operation.
2277 The distinction is important for IEEE floating point, because
2278 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2279 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2282 operand_equal_p (arg0
, arg1
, only_const
)
2286 /* If both types don't have the same signedness, then we can't consider
2287 them equal. We must check this before the STRIP_NOPS calls
2288 because they may change the signedness of the arguments. */
2289 if (TREE_UNSIGNED (TREE_TYPE (arg0
)) != TREE_UNSIGNED (TREE_TYPE (arg1
)))
2295 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2296 /* This is needed for conversions and for COMPONENT_REF.
2297 Might as well play it safe and always test this. */
2298 || TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2299 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2300 || TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
)))
2303 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2304 We don't care about side effects in that case because the SAVE_EXPR
2305 takes care of that for us. In all other cases, two expressions are
2306 equal if they have no side effects. If we have two identical
2307 expressions with side effects that should be treated the same due
2308 to the only side effects being identical SAVE_EXPR's, that will
2309 be detected in the recursive calls below. */
2310 if (arg0
== arg1
&& ! only_const
2311 && (TREE_CODE (arg0
) == SAVE_EXPR
2312 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2315 /* Next handle constant cases, those for which we can return 1 even
2316 if ONLY_CONST is set. */
2317 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2318 switch (TREE_CODE (arg0
))
2321 return (! TREE_CONSTANT_OVERFLOW (arg0
)
2322 && ! TREE_CONSTANT_OVERFLOW (arg1
)
2323 && tree_int_cst_equal (arg0
, arg1
));
2326 return (! TREE_CONSTANT_OVERFLOW (arg0
)
2327 && ! TREE_CONSTANT_OVERFLOW (arg1
)
2328 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0
),
2329 TREE_REAL_CST (arg1
)));
2332 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2334 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2338 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2339 && ! memcmp (TREE_STRING_POINTER (arg0
),
2340 TREE_STRING_POINTER (arg1
),
2341 TREE_STRING_LENGTH (arg0
)));
2344 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2353 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2356 /* Two conversions are equal only if signedness and modes match. */
2357 if ((TREE_CODE (arg0
) == NOP_EXPR
|| TREE_CODE (arg0
) == CONVERT_EXPR
)
2358 && (TREE_UNSIGNED (TREE_TYPE (arg0
))
2359 != TREE_UNSIGNED (TREE_TYPE (arg1
))))
2362 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2363 TREE_OPERAND (arg1
, 0), 0);
2367 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0)
2368 && operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1),
2372 /* For commutative ops, allow the other order. */
2373 return ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
2374 || TREE_CODE (arg0
) == MIN_EXPR
|| TREE_CODE (arg0
) == MAX_EXPR
2375 || TREE_CODE (arg0
) == BIT_IOR_EXPR
2376 || TREE_CODE (arg0
) == BIT_XOR_EXPR
2377 || TREE_CODE (arg0
) == BIT_AND_EXPR
2378 || TREE_CODE (arg0
) == NE_EXPR
|| TREE_CODE (arg0
) == EQ_EXPR
)
2379 && operand_equal_p (TREE_OPERAND (arg0
, 0),
2380 TREE_OPERAND (arg1
, 1), 0)
2381 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2382 TREE_OPERAND (arg1
, 0), 0));
2385 /* If either of the pointer (or reference) expressions we are dereferencing
2386 contain a side effect, these cannot be equal. */
2387 if (TREE_SIDE_EFFECTS (arg0
)
2388 || TREE_SIDE_EFFECTS (arg1
))
2391 switch (TREE_CODE (arg0
))
2394 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2395 TREE_OPERAND (arg1
, 0), 0);
2399 case ARRAY_RANGE_REF
:
2400 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
2401 TREE_OPERAND (arg1
, 0), 0)
2402 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2403 TREE_OPERAND (arg1
, 1), 0));
2406 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
2407 TREE_OPERAND (arg1
, 0), 0)
2408 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2409 TREE_OPERAND (arg1
, 1), 0)
2410 && operand_equal_p (TREE_OPERAND (arg0
, 2),
2411 TREE_OPERAND (arg1
, 2), 0));
2417 if (TREE_CODE (arg0
) == RTL_EXPR
)
2418 return rtx_equal_p (RTL_EXPR_RTL (arg0
), RTL_EXPR_RTL (arg1
));
2426 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2427 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2429 When in doubt, return 0. */
2432 operand_equal_for_comparison_p (arg0
, arg1
, other
)
2436 int unsignedp1
, unsignedpo
;
2437 tree primarg0
, primarg1
, primother
;
2438 unsigned int correct_width
;
2440 if (operand_equal_p (arg0
, arg1
, 0))
2443 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
2444 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
2447 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2448 and see if the inner values are the same. This removes any
2449 signedness comparison, which doesn't matter here. */
2450 primarg0
= arg0
, primarg1
= arg1
;
2451 STRIP_NOPS (primarg0
);
2452 STRIP_NOPS (primarg1
);
2453 if (operand_equal_p (primarg0
, primarg1
, 0))
2456 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2457 actual comparison operand, ARG0.
2459 First throw away any conversions to wider types
2460 already present in the operands. */
2462 primarg1
= get_narrower (arg1
, &unsignedp1
);
2463 primother
= get_narrower (other
, &unsignedpo
);
2465 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
2466 if (unsignedp1
== unsignedpo
2467 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
2468 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
2470 tree type
= TREE_TYPE (arg0
);
2472 /* Make sure shorter operand is extended the right way
2473 to match the longer operand. */
2474 primarg1
= convert (signed_or_unsigned_type (unsignedp1
,
2475 TREE_TYPE (primarg1
)),
2478 if (operand_equal_p (arg0
, convert (type
, primarg1
), 0))
2485 /* See if ARG is an expression that is either a comparison or is performing
2486 arithmetic on comparisons. The comparisons must only be comparing
2487 two different values, which will be stored in *CVAL1 and *CVAL2; if
2488 they are non-zero it means that some operands have already been found.
2489 No variables may be used anywhere else in the expression except in the
2490 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2491 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2493 If this is true, return 1. Otherwise, return zero. */
2496 twoval_comparison_p (arg
, cval1
, cval2
, save_p
)
2498 tree
*cval1
, *cval2
;
2501 enum tree_code code
= TREE_CODE (arg
);
2502 char class = TREE_CODE_CLASS (code
);
2504 /* We can handle some of the 'e' cases here. */
2505 if (class == 'e' && code
== TRUTH_NOT_EXPR
)
2507 else if (class == 'e'
2508 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
2509 || code
== COMPOUND_EXPR
))
2512 else if (class == 'e' && code
== SAVE_EXPR
&& SAVE_EXPR_RTL (arg
) == 0
2513 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
2515 /* If we've already found a CVAL1 or CVAL2, this expression is
2516 two complex to handle. */
2517 if (*cval1
|| *cval2
)
2527 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
2530 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
2531 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
2532 cval1
, cval2
, save_p
));
2538 if (code
== COND_EXPR
)
2539 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
2540 cval1
, cval2
, save_p
)
2541 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
2542 cval1
, cval2
, save_p
)
2543 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
2544 cval1
, cval2
, save_p
));
2548 /* First see if we can handle the first operand, then the second. For
2549 the second operand, we know *CVAL1 can't be zero. It must be that
2550 one side of the comparison is each of the values; test for the
2551 case where this isn't true by failing if the two operands
2554 if (operand_equal_p (TREE_OPERAND (arg
, 0),
2555 TREE_OPERAND (arg
, 1), 0))
2559 *cval1
= TREE_OPERAND (arg
, 0);
2560 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
2562 else if (*cval2
== 0)
2563 *cval2
= TREE_OPERAND (arg
, 0);
2564 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
2569 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
2571 else if (*cval2
== 0)
2572 *cval2
= TREE_OPERAND (arg
, 1);
2573 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
2585 /* ARG is a tree that is known to contain just arithmetic operations and
2586 comparisons. Evaluate the operations in the tree substituting NEW0 for
2587 any occurrence of OLD0 as an operand of a comparison and likewise for
2591 eval_subst (arg
, old0
, new0
, old1
, new1
)
2593 tree old0
, new0
, old1
, new1
;
2595 tree type
= TREE_TYPE (arg
);
2596 enum tree_code code
= TREE_CODE (arg
);
2597 char class = TREE_CODE_CLASS (code
);
2599 /* We can handle some of the 'e' cases here. */
2600 if (class == 'e' && code
== TRUTH_NOT_EXPR
)
2602 else if (class == 'e'
2603 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2609 return fold (build1 (code
, type
,
2610 eval_subst (TREE_OPERAND (arg
, 0),
2611 old0
, new0
, old1
, new1
)));
2614 return fold (build (code
, type
,
2615 eval_subst (TREE_OPERAND (arg
, 0),
2616 old0
, new0
, old1
, new1
),
2617 eval_subst (TREE_OPERAND (arg
, 1),
2618 old0
, new0
, old1
, new1
)));
2624 return eval_subst (TREE_OPERAND (arg
, 0), old0
, new0
, old1
, new1
);
2627 return eval_subst (TREE_OPERAND (arg
, 1), old0
, new0
, old1
, new1
);
2630 return fold (build (code
, type
,
2631 eval_subst (TREE_OPERAND (arg
, 0),
2632 old0
, new0
, old1
, new1
),
2633 eval_subst (TREE_OPERAND (arg
, 1),
2634 old0
, new0
, old1
, new1
),
2635 eval_subst (TREE_OPERAND (arg
, 2),
2636 old0
, new0
, old1
, new1
)));
2640 /* fall through - ??? */
2644 tree arg0
= TREE_OPERAND (arg
, 0);
2645 tree arg1
= TREE_OPERAND (arg
, 1);
2647 /* We need to check both for exact equality and tree equality. The
2648 former will be true if the operand has a side-effect. In that
2649 case, we know the operand occurred exactly once. */
2651 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
2653 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
2656 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
2658 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
2661 return fold (build (code
, type
, arg0
, arg1
));
2669 /* Return a tree for the case when the result of an expression is RESULT
2670 converted to TYPE and OMITTED was previously an operand of the expression
2671 but is now not needed (e.g., we folded OMITTED * 0).
2673 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2674 the conversion of RESULT to TYPE. */
2677 omit_one_operand (type
, result
, omitted
)
2678 tree type
, result
, omitted
;
2680 tree t
= convert (type
, result
);
2682 if (TREE_SIDE_EFFECTS (omitted
))
2683 return build (COMPOUND_EXPR
, type
, omitted
, t
);
2685 return non_lvalue (t
);
2688 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2691 pedantic_omit_one_operand (type
, result
, omitted
)
2692 tree type
, result
, omitted
;
2694 tree t
= convert (type
, result
);
2696 if (TREE_SIDE_EFFECTS (omitted
))
2697 return build (COMPOUND_EXPR
, type
, omitted
, t
);
2699 return pedantic_non_lvalue (t
);
2702 /* Return a simplified tree node for the truth-negation of ARG. This
2703 never alters ARG itself. We assume that ARG is an operation that
2704 returns a truth value (0 or 1). */
2707 invert_truthvalue (arg
)
2710 tree type
= TREE_TYPE (arg
);
2711 enum tree_code code
= TREE_CODE (arg
);
2713 if (code
== ERROR_MARK
)
2716 /* If this is a comparison, we can simply invert it, except for
2717 floating-point non-equality comparisons, in which case we just
2718 enclose a TRUTH_NOT_EXPR around what we have. */
2720 if (TREE_CODE_CLASS (code
) == '<')
2722 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg
, 0)))
2723 && !flag_unsafe_math_optimizations
2726 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
2728 return build (invert_tree_comparison (code
), type
,
2729 TREE_OPERAND (arg
, 0), TREE_OPERAND (arg
, 1));
2735 return convert (type
, build_int_2 (integer_zerop (arg
), 0));
2737 case TRUTH_AND_EXPR
:
2738 return build (TRUTH_OR_EXPR
, type
,
2739 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2740 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2743 return build (TRUTH_AND_EXPR
, type
,
2744 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2745 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2747 case TRUTH_XOR_EXPR
:
2748 /* Here we can invert either operand. We invert the first operand
2749 unless the second operand is a TRUTH_NOT_EXPR in which case our
2750 result is the XOR of the first operand with the inside of the
2751 negation of the second operand. */
2753 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
2754 return build (TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
2755 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
2757 return build (TRUTH_XOR_EXPR
, type
,
2758 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2759 TREE_OPERAND (arg
, 1));
2761 case TRUTH_ANDIF_EXPR
:
2762 return build (TRUTH_ORIF_EXPR
, type
,
2763 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2764 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2766 case TRUTH_ORIF_EXPR
:
2767 return build (TRUTH_ANDIF_EXPR
, type
,
2768 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2769 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2771 case TRUTH_NOT_EXPR
:
2772 return TREE_OPERAND (arg
, 0);
2775 return build (COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
2776 invert_truthvalue (TREE_OPERAND (arg
, 1)),
2777 invert_truthvalue (TREE_OPERAND (arg
, 2)));
2780 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg
, 0),
2781 invert_truthvalue (TREE_OPERAND (arg
, 1)));
2783 case WITH_RECORD_EXPR
:
2784 return build (WITH_RECORD_EXPR
, type
,
2785 invert_truthvalue (TREE_OPERAND (arg
, 0)),
2786 TREE_OPERAND (arg
, 1));
2788 case NON_LVALUE_EXPR
:
2789 return invert_truthvalue (TREE_OPERAND (arg
, 0));
2794 return build1 (TREE_CODE (arg
), type
,
2795 invert_truthvalue (TREE_OPERAND (arg
, 0)));
2798 if (!integer_onep (TREE_OPERAND (arg
, 1)))
2800 return build (EQ_EXPR
, type
, arg
, convert (type
, integer_zero_node
));
2803 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
2805 case CLEANUP_POINT_EXPR
:
2806 return build1 (CLEANUP_POINT_EXPR
, type
,
2807 invert_truthvalue (TREE_OPERAND (arg
, 0)));
2812 if (TREE_CODE (TREE_TYPE (arg
)) != BOOLEAN_TYPE
)
2814 return build1 (TRUTH_NOT_EXPR
, type
, arg
);
2817 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2818 operands are another bit-wise operation with a common input. If so,
2819 distribute the bit operations to save an operation and possibly two if
2820 constants are involved. For example, convert
2821 (A | B) & (A | C) into A | (B & C)
2822 Further simplification will occur if B and C are constants.
2824 If this optimization cannot be done, 0 will be returned. */
2827 distribute_bit_expr (code
, type
, arg0
, arg1
)
2828 enum tree_code code
;
2835 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2836 || TREE_CODE (arg0
) == code
2837 || (TREE_CODE (arg0
) != BIT_AND_EXPR
2838 && TREE_CODE (arg0
) != BIT_IOR_EXPR
))
2841 if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0), 0))
2843 common
= TREE_OPERAND (arg0
, 0);
2844 left
= TREE_OPERAND (arg0
, 1);
2845 right
= TREE_OPERAND (arg1
, 1);
2847 else if (operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1), 0))
2849 common
= TREE_OPERAND (arg0
, 0);
2850 left
= TREE_OPERAND (arg0
, 1);
2851 right
= TREE_OPERAND (arg1
, 0);
2853 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 0), 0))
2855 common
= TREE_OPERAND (arg0
, 1);
2856 left
= TREE_OPERAND (arg0
, 0);
2857 right
= TREE_OPERAND (arg1
, 1);
2859 else if (operand_equal_p (TREE_OPERAND (arg0
, 1), TREE_OPERAND (arg1
, 1), 0))
2861 common
= TREE_OPERAND (arg0
, 1);
2862 left
= TREE_OPERAND (arg0
, 0);
2863 right
= TREE_OPERAND (arg1
, 0);
2868 return fold (build (TREE_CODE (arg0
), type
, common
,
2869 fold (build (code
, type
, left
, right
))));
2872 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2873 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2876 make_bit_field_ref (inner
, type
, bitsize
, bitpos
, unsignedp
)
2879 int bitsize
, bitpos
;
2882 tree result
= build (BIT_FIELD_REF
, type
, inner
,
2883 size_int (bitsize
), bitsize_int (bitpos
));
2885 TREE_UNSIGNED (result
) = unsignedp
;
2890 /* Optimize a bit-field compare.
2892 There are two cases: First is a compare against a constant and the
2893 second is a comparison of two items where the fields are at the same
2894 bit position relative to the start of a chunk (byte, halfword, word)
2895 large enough to contain it. In these cases we can avoid the shift
2896 implicit in bitfield extractions.
2898 For constants, we emit a compare of the shifted constant with the
2899 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2900 compared. For two fields at the same position, we do the ANDs with the
2901 similar mask and compare the result of the ANDs.
2903 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2904 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2905 are the left and right operands of the comparison, respectively.
2907 If the optimization described above can be done, we return the resulting
2908 tree. Otherwise we return zero. */
2911 optimize_bit_field_compare (code
, compare_type
, lhs
, rhs
)
2912 enum tree_code code
;
2916 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
2917 tree type
= TREE_TYPE (lhs
);
2918 tree signed_type
, unsigned_type
;
2919 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
2920 enum machine_mode lmode
, rmode
, nmode
;
2921 int lunsignedp
, runsignedp
;
2922 int lvolatilep
= 0, rvolatilep
= 0;
2923 unsigned int alignment
;
2924 tree linner
, rinner
= NULL_TREE
;
2928 /* Get all the information about the extractions being done. If the bit size
2929 if the same as the size of the underlying object, we aren't doing an
2930 extraction at all and so can do nothing. We also don't want to
2931 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2932 then will no longer be able to replace it. */
2933 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
2934 &lunsignedp
, &lvolatilep
, &alignment
);
2935 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
2936 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
)
2941 /* If this is not a constant, we can only do something if bit positions,
2942 sizes, and signedness are the same. */
2943 rinner
= get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
2944 &runsignedp
, &rvolatilep
, &alignment
);
2946 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
2947 || lunsignedp
!= runsignedp
|| offset
!= 0
2948 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
)
2952 /* See if we can find a mode to refer to this field. We should be able to,
2953 but fail if we can't. */
2954 nmode
= get_best_mode (lbitsize
, lbitpos
,
2955 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
2956 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
2957 TYPE_ALIGN (TREE_TYPE (rinner
))),
2958 word_mode
, lvolatilep
|| rvolatilep
);
2959 if (nmode
== VOIDmode
)
2962 /* Set signed and unsigned types of the precision of this mode for the
2964 signed_type
= type_for_mode (nmode
, 0);
2965 unsigned_type
= type_for_mode (nmode
, 1);
2967 /* Compute the bit position and size for the new reference and our offset
2968 within it. If the new reference is the same size as the original, we
2969 won't optimize anything, so return zero. */
2970 nbitsize
= GET_MODE_BITSIZE (nmode
);
2971 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
2973 if (nbitsize
== lbitsize
)
2976 if (BYTES_BIG_ENDIAN
)
2977 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
2979 /* Make the mask to be used against the extracted field. */
2980 mask
= build_int_2 (~0, ~0);
2981 TREE_TYPE (mask
) = unsigned_type
;
2982 force_fit_type (mask
, 0);
2983 mask
= convert (unsigned_type
, mask
);
2984 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
), 0);
2985 mask
= const_binop (RSHIFT_EXPR
, mask
,
2986 size_int (nbitsize
- lbitsize
- lbitpos
), 0);
2989 /* If not comparing with constant, just rework the comparison
2991 return build (code
, compare_type
,
2992 build (BIT_AND_EXPR
, unsigned_type
,
2993 make_bit_field_ref (linner
, unsigned_type
,
2994 nbitsize
, nbitpos
, 1),
2996 build (BIT_AND_EXPR
, unsigned_type
,
2997 make_bit_field_ref (rinner
, unsigned_type
,
2998 nbitsize
, nbitpos
, 1),
3001 /* Otherwise, we are handling the constant case. See if the constant is too
3002 big for the field. Warn and return a tree of for 0 (false) if so. We do
3003 this not only for its own sake, but to avoid having to test for this
3004 error case below. If we didn't, we might generate wrong code.
3006 For unsigned fields, the constant shifted right by the field length should
3007 be all zero. For signed fields, the high-order bits should agree with
3012 if (! integer_zerop (const_binop (RSHIFT_EXPR
,
3013 convert (unsigned_type
, rhs
),
3014 size_int (lbitsize
), 0)))
3016 warning ("comparison is always %d due to width of bitfield",
3018 return convert (compare_type
,
3020 ? integer_one_node
: integer_zero_node
));
3025 tree tem
= const_binop (RSHIFT_EXPR
, convert (signed_type
, rhs
),
3026 size_int (lbitsize
- 1), 0);
3027 if (! integer_zerop (tem
) && ! integer_all_onesp (tem
))
3029 warning ("comparison is always %d due to width of bitfield",
3031 return convert (compare_type
,
3033 ? integer_one_node
: integer_zero_node
));
3037 /* Single-bit compares should always be against zero. */
3038 if (lbitsize
== 1 && ! integer_zerop (rhs
))
3040 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
3041 rhs
= convert (type
, integer_zero_node
);
3044 /* Make a new bitfield reference, shift the constant over the
3045 appropriate number of bits and mask it with the computed mask
3046 (in case this was a signed field). If we changed it, make a new one. */
3047 lhs
= make_bit_field_ref (linner
, unsigned_type
, nbitsize
, nbitpos
, 1);
3050 TREE_SIDE_EFFECTS (lhs
) = 1;
3051 TREE_THIS_VOLATILE (lhs
) = 1;
3054 rhs
= fold (const_binop (BIT_AND_EXPR
,
3055 const_binop (LSHIFT_EXPR
,
3056 convert (unsigned_type
, rhs
),
3057 size_int (lbitpos
), 0),
3060 return build (code
, compare_type
,
3061 build (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
),
3065 /* Subroutine for fold_truthop: decode a field reference.
3067 If EXP is a comparison reference, we return the innermost reference.
3069 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3070 set to the starting bit number.
3072 If the innermost field can be completely contained in a mode-sized
3073 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3075 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3076 otherwise it is not changed.
3078 *PUNSIGNEDP is set to the signedness of the field.
3080 *PMASK is set to the mask used. This is either contained in a
3081 BIT_AND_EXPR or derived from the width of the field.
3083 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3085 Return 0 if this is not a component reference or is one that we can't
3086 do anything with. */
3089 decode_field_reference (exp
, pbitsize
, pbitpos
, pmode
, punsignedp
,
3090 pvolatilep
, pmask
, pand_mask
)
3092 HOST_WIDE_INT
*pbitsize
, *pbitpos
;
3093 enum machine_mode
*pmode
;
3094 int *punsignedp
, *pvolatilep
;
3099 tree mask
, inner
, offset
;
3101 unsigned int precision
;
3102 unsigned int alignment
;
3104 /* All the optimizations using this function assume integer fields.
3105 There are problems with FP fields since the type_for_size call
3106 below can fail for, e.g., XFmode. */
3107 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
3112 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
3114 and_mask
= TREE_OPERAND (exp
, 1);
3115 exp
= TREE_OPERAND (exp
, 0);
3116 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
3117 if (TREE_CODE (and_mask
) != INTEGER_CST
)
3121 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
3122 punsignedp
, pvolatilep
, &alignment
);
3123 if ((inner
== exp
&& and_mask
== 0)
3124 || *pbitsize
< 0 || offset
!= 0
3125 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
3128 /* Compute the mask to access the bitfield. */
3129 unsigned_type
= type_for_size (*pbitsize
, 1);
3130 precision
= TYPE_PRECISION (unsigned_type
);
3132 mask
= build_int_2 (~0, ~0);
3133 TREE_TYPE (mask
) = unsigned_type
;
3134 force_fit_type (mask
, 0);
3135 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
), 0);
3136 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
), 0);
3138 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3140 mask
= fold (build (BIT_AND_EXPR
, unsigned_type
,
3141 convert (unsigned_type
, and_mask
), mask
));
3144 *pand_mask
= and_mask
;
3148 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3152 all_ones_mask_p (mask
, size
)
3156 tree type
= TREE_TYPE (mask
);
3157 unsigned int precision
= TYPE_PRECISION (type
);
3160 tmask
= build_int_2 (~0, ~0);
3161 TREE_TYPE (tmask
) = signed_type (type
);
3162 force_fit_type (tmask
, 0);
3164 tree_int_cst_equal (mask
,
3165 const_binop (RSHIFT_EXPR
,
3166 const_binop (LSHIFT_EXPR
, tmask
,
3167 size_int (precision
- size
),
3169 size_int (precision
- size
), 0));
3172 /* Subroutine for fold_truthop: determine if an operand is simple enough
3173 to be evaluated unconditionally. */
3176 simple_operand_p (exp
)
3179 /* Strip any conversions that don't change the machine mode. */
3180 while ((TREE_CODE (exp
) == NOP_EXPR
3181 || TREE_CODE (exp
) == CONVERT_EXPR
)
3182 && (TYPE_MODE (TREE_TYPE (exp
))
3183 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp
, 0)))))
3184 exp
= TREE_OPERAND (exp
, 0);
3186 return (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'c'
3188 && ! TREE_ADDRESSABLE (exp
)
3189 && ! TREE_THIS_VOLATILE (exp
)
3190 && ! DECL_NONLOCAL (exp
)
3191 /* Don't regard global variables as simple. They may be
3192 allocated in ways unknown to the compiler (shared memory,
3193 #pragma weak, etc). */
3194 && ! TREE_PUBLIC (exp
)
3195 && ! DECL_EXTERNAL (exp
)
3196 /* Loading a static variable is unduly expensive, but global
3197 registers aren't expensive. */
3198 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
3201 /* The following functions are subroutines to fold_range_test and allow it to
3202 try to change a logical combination of comparisons into a range test.
3205 X == 2 || X == 3 || X == 4 || X == 5
3209 (unsigned) (X - 2) <= 3
3211 We describe each set of comparisons as being either inside or outside
3212 a range, using a variable named like IN_P, and then describe the
3213 range with a lower and upper bound. If one of the bounds is omitted,
3214 it represents either the highest or lowest value of the type.
3216 In the comments below, we represent a range by two numbers in brackets
3217 preceded by a "+" to designate being inside that range, or a "-" to
3218 designate being outside that range, so the condition can be inverted by
3219 flipping the prefix. An omitted bound is represented by a "-". For
3220 example, "- [-, 10]" means being outside the range starting at the lowest
3221 possible value and ending at 10, in other words, being greater than 10.
3222 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3225 We set up things so that the missing bounds are handled in a consistent
3226 manner so neither a missing bound nor "true" and "false" need to be
3227 handled using a special case. */
3229 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3230 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3231 and UPPER1_P are nonzero if the respective argument is an upper bound
3232 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3233 must be specified for a comparison. ARG1 will be converted to ARG0's
3234 type if both are specified. */
3237 range_binop (code
, type
, arg0
, upper0_p
, arg1
, upper1_p
)
3238 enum tree_code code
;
3241 int upper0_p
, upper1_p
;
3247 /* If neither arg represents infinity, do the normal operation.
3248 Else, if not a comparison, return infinity. Else handle the special
3249 comparison rules. Note that most of the cases below won't occur, but
3250 are handled for consistency. */
3252 if (arg0
!= 0 && arg1
!= 0)
3254 tem
= fold (build (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
3255 arg0
, convert (TREE_TYPE (arg0
), arg1
)));
3257 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
3260 if (TREE_CODE_CLASS (code
) != '<')
3263 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3264 for neither. In real maths, we cannot assume open ended ranges are
3265 the same. But, this is computer arithmetic, where numbers are finite.
3266 We can therefore make the transformation of any unbounded range with
3267 the value Z, Z being greater than any representable number. This permits
3268 us to treat unbounded ranges as equal. */
3269 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
3270 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
3274 result
= sgn0
== sgn1
;
3277 result
= sgn0
!= sgn1
;
3280 result
= sgn0
< sgn1
;
3283 result
= sgn0
<= sgn1
;
3286 result
= sgn0
> sgn1
;
3289 result
= sgn0
>= sgn1
;
3295 return convert (type
, result
? integer_one_node
: integer_zero_node
);
3298 /* Given EXP, a logical expression, set the range it is testing into
3299 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3300 actually being tested. *PLOW and *PHIGH will be made of the same type
3301 as the returned expression. If EXP is not a comparison, we will most
3302 likely not be returning a useful value and range. */
3305 make_range (exp
, pin_p
, plow
, phigh
)
3310 enum tree_code code
;
3311 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, type
= NULL_TREE
;
3312 tree orig_type
= NULL_TREE
;
3314 tree low
, high
, n_low
, n_high
;
3316 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3317 and see if we can refine the range. Some of the cases below may not
3318 happen, but it doesn't seem worth worrying about this. We "continue"
3319 the outer loop when we've changed something; otherwise we "break"
3320 the switch, which will "break" the while. */
3322 in_p
= 0, low
= high
= convert (TREE_TYPE (exp
), integer_zero_node
);
3326 code
= TREE_CODE (exp
);
3328 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
3330 arg0
= TREE_OPERAND (exp
, 0);
3331 if (TREE_CODE_CLASS (code
) == '<'
3332 || TREE_CODE_CLASS (code
) == '1'
3333 || TREE_CODE_CLASS (code
) == '2')
3334 type
= TREE_TYPE (arg0
);
3335 if (TREE_CODE_CLASS (code
) == '2'
3336 || TREE_CODE_CLASS (code
) == '<'
3337 || (TREE_CODE_CLASS (code
) == 'e'
3338 && TREE_CODE_LENGTH (code
) > 1))
3339 arg1
= TREE_OPERAND (exp
, 1);
3342 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3343 lose a cast by accident. */
3344 if (type
!= NULL_TREE
&& orig_type
== NULL_TREE
)
3349 case TRUTH_NOT_EXPR
:
3350 in_p
= ! in_p
, exp
= arg0
;
3353 case EQ_EXPR
: case NE_EXPR
:
3354 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
3355 /* We can only do something if the range is testing for zero
3356 and if the second operand is an integer constant. Note that
3357 saying something is "in" the range we make is done by
3358 complementing IN_P since it will set in the initial case of
3359 being not equal to zero; "out" is leaving it alone. */
3360 if (low
== 0 || high
== 0
3361 || ! integer_zerop (low
) || ! integer_zerop (high
)
3362 || TREE_CODE (arg1
) != INTEGER_CST
)
3367 case NE_EXPR
: /* - [c, c] */
3370 case EQ_EXPR
: /* + [c, c] */
3371 in_p
= ! in_p
, low
= high
= arg1
;
3373 case GT_EXPR
: /* - [-, c] */
3374 low
= 0, high
= arg1
;
3376 case GE_EXPR
: /* + [c, -] */
3377 in_p
= ! in_p
, low
= arg1
, high
= 0;
3379 case LT_EXPR
: /* - [c, -] */
3380 low
= arg1
, high
= 0;
3382 case LE_EXPR
: /* + [-, c] */
3383 in_p
= ! in_p
, low
= 0, high
= arg1
;
3391 /* If this is an unsigned comparison, we also know that EXP is
3392 greater than or equal to zero. We base the range tests we make
3393 on that fact, so we record it here so we can parse existing
3395 if (TREE_UNSIGNED (type
) && (low
== 0 || high
== 0))
3397 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, in_p
, low
, high
,
3398 1, convert (type
, integer_zero_node
),
3402 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
3404 /* If the high bound is missing, but we
3405 have a low bound, reverse the range so
3406 it goes from zero to the low bound minus 1. */
3407 if (high
== 0 && low
)
3410 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
3411 integer_one_node
, 0);
3412 low
= convert (type
, integer_zero_node
);
3418 /* (-x) IN [a,b] -> x in [-b, -a] */
3419 n_low
= range_binop (MINUS_EXPR
, type
,
3420 convert (type
, integer_zero_node
), 0, high
, 1);
3421 n_high
= range_binop (MINUS_EXPR
, type
,
3422 convert (type
, integer_zero_node
), 0, low
, 0);
3423 low
= n_low
, high
= n_high
;
3429 exp
= build (MINUS_EXPR
, type
, negate_expr (arg0
),
3430 convert (type
, integer_one_node
));
3433 case PLUS_EXPR
: case MINUS_EXPR
:
3434 if (TREE_CODE (arg1
) != INTEGER_CST
)
3437 /* If EXP is signed, any overflow in the computation is undefined,
3438 so we don't worry about it so long as our computations on
3439 the bounds don't overflow. For unsigned, overflow is defined
3440 and this is exactly the right thing. */
3441 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
3442 type
, low
, 0, arg1
, 0);
3443 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
3444 type
, high
, 1, arg1
, 0);
3445 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
3446 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
3449 /* Check for an unsigned range which has wrapped around the maximum
3450 value thus making n_high < n_low, and normalize it. */
3451 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
3453 low
= range_binop (PLUS_EXPR
, type
, n_high
, 0,
3454 integer_one_node
, 0);
3455 high
= range_binop (MINUS_EXPR
, type
, n_low
, 0,
3456 integer_one_node
, 0);
3458 /* If the range is of the form +/- [ x+1, x ], we won't
3459 be able to normalize it. But then, it represents the
3460 whole range or the empty set, so make it
3462 if (tree_int_cst_equal (n_low
, low
)
3463 && tree_int_cst_equal (n_high
, high
))
3469 low
= n_low
, high
= n_high
;
3474 case NOP_EXPR
: case NON_LVALUE_EXPR
: case CONVERT_EXPR
:
3475 if (TYPE_PRECISION (type
) > TYPE_PRECISION (orig_type
))
3478 if (! INTEGRAL_TYPE_P (type
)
3479 || (low
!= 0 && ! int_fits_type_p (low
, type
))
3480 || (high
!= 0 && ! int_fits_type_p (high
, type
)))
3483 n_low
= low
, n_high
= high
;
3486 n_low
= convert (type
, n_low
);
3489 n_high
= convert (type
, n_high
);
3491 /* If we're converting from an unsigned to a signed type,
3492 we will be doing the comparison as unsigned. The tests above
3493 have already verified that LOW and HIGH are both positive.
3495 So we have to make sure that the original unsigned value will
3496 be interpreted as positive. */
3497 if (TREE_UNSIGNED (type
) && ! TREE_UNSIGNED (TREE_TYPE (exp
)))
3499 tree equiv_type
= type_for_mode (TYPE_MODE (type
), 1);
3502 /* A range without an upper bound is, naturally, unbounded.
3503 Since convert would have cropped a very large value, use
3504 the max value for the destination type. */
3506 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
3507 : TYPE_MAX_VALUE (type
);
3509 high_positive
= fold (build (RSHIFT_EXPR
, type
,
3510 convert (type
, high_positive
),
3511 convert (type
, integer_one_node
)));
3513 /* If the low bound is specified, "and" the range with the
3514 range for which the original unsigned value will be
3518 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
3520 1, convert (type
, integer_zero_node
),
3524 in_p
= (n_in_p
== in_p
);
3528 /* Otherwise, "or" the range with the range of the input
3529 that will be interpreted as negative. */
3530 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
3532 1, convert (type
, integer_zero_node
),
3536 in_p
= (in_p
!= n_in_p
);
3541 low
= n_low
, high
= n_high
;
3551 /* If EXP is a constant, we can evaluate whether this is true or false. */
3552 if (TREE_CODE (exp
) == INTEGER_CST
)
3554 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
3556 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
3562 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
3566 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3567 type, TYPE, return an expression to test if EXP is in (or out of, depending
3568 on IN_P) the range. */
3571 build_range_check (type
, exp
, in_p
, low
, high
)
3577 tree etype
= TREE_TYPE (exp
);
3581 && (0 != (value
= build_range_check (type
, exp
, 1, low
, high
))))
3582 return invert_truthvalue (value
);
3584 else if (low
== 0 && high
== 0)
3585 return convert (type
, integer_one_node
);
3588 return fold (build (LE_EXPR
, type
, exp
, high
));
3591 return fold (build (GE_EXPR
, type
, exp
, low
));
3593 else if (operand_equal_p (low
, high
, 0))
3594 return fold (build (EQ_EXPR
, type
, exp
, low
));
3596 else if (TREE_UNSIGNED (etype
) && integer_zerop (low
))
3597 return build_range_check (type
, exp
, 1, 0, high
);
3599 else if (integer_zerop (low
))
3601 utype
= unsigned_type (etype
);
3602 return build_range_check (type
, convert (utype
, exp
), 1, 0,
3603 convert (utype
, high
));
3606 else if (0 != (value
= const_binop (MINUS_EXPR
, high
, low
, 0))
3607 && ! TREE_OVERFLOW (value
))
3608 return build_range_check (type
,
3609 fold (build (MINUS_EXPR
, etype
, exp
, low
)),
3610 1, convert (etype
, integer_zero_node
), value
);
3615 /* Given two ranges, see if we can merge them into one. Return 1 if we
3616 can, 0 if we can't. Set the output range into the specified parameters. */
3619 merge_ranges (pin_p
, plow
, phigh
, in0_p
, low0
, high0
, in1_p
, low1
, high1
)
3623 tree low0
, high0
, low1
, high1
;
3631 int lowequal
= ((low0
== 0 && low1
== 0)
3632 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
3633 low0
, 0, low1
, 0)));
3634 int highequal
= ((high0
== 0 && high1
== 0)
3635 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
3636 high0
, 1, high1
, 1)));
3638 /* Make range 0 be the range that starts first, or ends last if they
3639 start at the same value. Swap them if it isn't. */
3640 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
3643 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
3644 high1
, 1, high0
, 1))))
3646 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
3647 tem
= low0
, low0
= low1
, low1
= tem
;
3648 tem
= high0
, high0
= high1
, high1
= tem
;
3651 /* Now flag two cases, whether the ranges are disjoint or whether the
3652 second range is totally subsumed in the first. Note that the tests
3653 below are simplified by the ones above. */
3654 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
3655 high0
, 1, low1
, 0));
3656 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
3657 high1
, 1, high0
, 1));
3659 /* We now have four cases, depending on whether we are including or
3660 excluding the two ranges. */
3663 /* If they don't overlap, the result is false. If the second range
3664 is a subset it is the result. Otherwise, the range is from the start
3665 of the second to the end of the first. */
3667 in_p
= 0, low
= high
= 0;
3669 in_p
= 1, low
= low1
, high
= high1
;
3671 in_p
= 1, low
= low1
, high
= high0
;
3674 else if (in0_p
&& ! in1_p
)
3676 /* If they don't overlap, the result is the first range. If they are
3677 equal, the result is false. If the second range is a subset of the
3678 first, and the ranges begin at the same place, we go from just after
3679 the end of the first range to the end of the second. If the second
3680 range is not a subset of the first, or if it is a subset and both
3681 ranges end at the same place, the range starts at the start of the
3682 first range and ends just before the second range.
3683 Otherwise, we can't describe this as a single range. */
3685 in_p
= 1, low
= low0
, high
= high0
;
3686 else if (lowequal
&& highequal
)
3687 in_p
= 0, low
= high
= 0;
3688 else if (subset
&& lowequal
)
3690 in_p
= 1, high
= high0
;
3691 low
= range_binop (PLUS_EXPR
, NULL_TREE
, high1
, 0,
3692 integer_one_node
, 0);
3694 else if (! subset
|| highequal
)
3696 in_p
= 1, low
= low0
;
3697 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low1
, 0,
3698 integer_one_node
, 0);
3704 else if (! in0_p
&& in1_p
)
3706 /* If they don't overlap, the result is the second range. If the second
3707 is a subset of the first, the result is false. Otherwise,
3708 the range starts just after the first range and ends at the
3709 end of the second. */
3711 in_p
= 1, low
= low1
, high
= high1
;
3712 else if (subset
|| highequal
)
3713 in_p
= 0, low
= high
= 0;
3716 in_p
= 1, high
= high1
;
3717 low
= range_binop (PLUS_EXPR
, NULL_TREE
, high0
, 1,
3718 integer_one_node
, 0);
3724 /* The case where we are excluding both ranges. Here the complex case
3725 is if they don't overlap. In that case, the only time we have a
3726 range is if they are adjacent. If the second is a subset of the
3727 first, the result is the first. Otherwise, the range to exclude
3728 starts at the beginning of the first range and ends at the end of the
3732 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
3733 range_binop (PLUS_EXPR
, NULL_TREE
,
3735 integer_one_node
, 1),
3737 in_p
= 0, low
= low0
, high
= high1
;
3742 in_p
= 0, low
= low0
, high
= high0
;
3744 in_p
= 0, low
= low0
, high
= high1
;
3747 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
3751 /* EXP is some logical combination of boolean tests. See if we can
3752 merge it into some range test. Return the new tree if so. */
3755 fold_range_test (exp
)
3758 int or_op
= (TREE_CODE (exp
) == TRUTH_ORIF_EXPR
3759 || TREE_CODE (exp
) == TRUTH_OR_EXPR
);
3760 int in0_p
, in1_p
, in_p
;
3761 tree low0
, low1
, low
, high0
, high1
, high
;
3762 tree lhs
= make_range (TREE_OPERAND (exp
, 0), &in0_p
, &low0
, &high0
);
3763 tree rhs
= make_range (TREE_OPERAND (exp
, 1), &in1_p
, &low1
, &high1
);
3766 /* If this is an OR operation, invert both sides; we will invert
3767 again at the end. */
3769 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
3771 /* If both expressions are the same, if we can merge the ranges, and we
3772 can build the range test, return it or it inverted. If one of the
3773 ranges is always true or always false, consider it to be the same
3774 expression as the other. */
3775 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
3776 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
3778 && 0 != (tem
= (build_range_check (TREE_TYPE (exp
),
3780 : rhs
!= 0 ? rhs
: integer_zero_node
,
3782 return or_op
? invert_truthvalue (tem
) : tem
;
3784 /* On machines where the branch cost is expensive, if this is a
3785 short-circuited branch and the underlying object on both sides
3786 is the same, make a non-short-circuit operation. */
3787 else if (BRANCH_COST
>= 2
3788 && lhs
!= 0 && rhs
!= 0
3789 && (TREE_CODE (exp
) == TRUTH_ANDIF_EXPR
3790 || TREE_CODE (exp
) == TRUTH_ORIF_EXPR
)
3791 && operand_equal_p (lhs
, rhs
, 0))
3793 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3794 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3795 which cases we can't do this. */
3796 if (simple_operand_p (lhs
))
3797 return build (TREE_CODE (exp
) == TRUTH_ANDIF_EXPR
3798 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
3799 TREE_TYPE (exp
), TREE_OPERAND (exp
, 0),
3800 TREE_OPERAND (exp
, 1));
3802 else if (global_bindings_p () == 0
3803 && ! contains_placeholder_p (lhs
))
3805 tree common
= save_expr (lhs
);
3807 if (0 != (lhs
= build_range_check (TREE_TYPE (exp
), common
,
3808 or_op
? ! in0_p
: in0_p
,
3810 && (0 != (rhs
= build_range_check (TREE_TYPE (exp
), common
,
3811 or_op
? ! in1_p
: in1_p
,
3813 return build (TREE_CODE (exp
) == TRUTH_ANDIF_EXPR
3814 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
3815 TREE_TYPE (exp
), lhs
, rhs
);
3822 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3823 bit value. Arrange things so the extra bits will be set to zero if and
3824 only if C is signed-extended to its full width. If MASK is nonzero,
3825 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3828 unextend (c
, p
, unsignedp
, mask
)
3834 tree type
= TREE_TYPE (c
);
3835 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
3838 if (p
== modesize
|| unsignedp
)
3841 /* We work by getting just the sign bit into the low-order bit, then
3842 into the high-order bit, then sign-extend. We then XOR that value
3844 temp
= const_binop (RSHIFT_EXPR
, c
, size_int (p
- 1), 0);
3845 temp
= const_binop (BIT_AND_EXPR
, temp
, size_int (1), 0);
3847 /* We must use a signed type in order to get an arithmetic right shift.
3848 However, we must also avoid introducing accidental overflows, so that
3849 a subsequent call to integer_zerop will work. Hence we must
3850 do the type conversion here. At this point, the constant is either
3851 zero or one, and the conversion to a signed type can never overflow.
3852 We could get an overflow if this conversion is done anywhere else. */
3853 if (TREE_UNSIGNED (type
))
3854 temp
= convert (signed_type (type
), temp
);
3856 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1), 0);
3857 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1), 0);
3859 temp
= const_binop (BIT_AND_EXPR
, temp
, convert (TREE_TYPE (c
), mask
), 0);
3860 /* If necessary, convert the type back to match the type of C. */
3861 if (TREE_UNSIGNED (type
))
3862 temp
= convert (type
, temp
);
3864 return convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
, 0));
3867 /* Find ways of folding logical expressions of LHS and RHS:
3868 Try to merge two comparisons to the same innermost item.
3869 Look for range tests like "ch >= '0' && ch <= '9'".
3870 Look for combinations of simple terms on machines with expensive branches
3871 and evaluate the RHS unconditionally.
3873 For example, if we have p->a == 2 && p->b == 4 and we can make an
3874 object large enough to span both A and B, we can do this with a comparison
3875 against the object ANDed with the a mask.
3877 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3878 operations to do this with one comparison.
3880 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3881 function and the one above.
3883 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3884 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3886 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3889 We return the simplified tree or 0 if no optimization is possible. */
3892 fold_truthop (code
, truth_type
, lhs
, rhs
)
3893 enum tree_code code
;
3894 tree truth_type
, lhs
, rhs
;
3896 /* If this is the "or" of two comparisons, we can do something if
3897 the comparisons are NE_EXPR. If this is the "and", we can do something
3898 if the comparisons are EQ_EXPR. I.e.,
3899 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3901 WANTED_CODE is this operation code. For single bit fields, we can
3902 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3903 comparison for one-bit fields. */
3905 enum tree_code wanted_code
;
3906 enum tree_code lcode
, rcode
;
3907 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
3908 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
3909 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
3910 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
3911 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
3912 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
3913 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
3914 enum machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
3915 enum machine_mode lnmode
, rnmode
;
3916 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
3917 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
3918 tree l_const
, r_const
;
3919 tree lntype
, rntype
, result
;
3920 int first_bit
, end_bit
;
3923 /* Start by getting the comparison codes. Fail if anything is volatile.
3924 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3925 it were surrounded with a NE_EXPR. */
3927 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
3930 lcode
= TREE_CODE (lhs
);
3931 rcode
= TREE_CODE (rhs
);
3933 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
3934 lcode
= NE_EXPR
, lhs
= build (NE_EXPR
, truth_type
, lhs
, integer_zero_node
);
3936 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
3937 rcode
= NE_EXPR
, rhs
= build (NE_EXPR
, truth_type
, rhs
, integer_zero_node
);
3939 if (TREE_CODE_CLASS (lcode
) != '<' || TREE_CODE_CLASS (rcode
) != '<')
3942 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
3943 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
3945 ll_arg
= TREE_OPERAND (lhs
, 0);
3946 lr_arg
= TREE_OPERAND (lhs
, 1);
3947 rl_arg
= TREE_OPERAND (rhs
, 0);
3948 rr_arg
= TREE_OPERAND (rhs
, 1);
3950 /* If the RHS can be evaluated unconditionally and its operands are
3951 simple, it wins to evaluate the RHS unconditionally on machines
3952 with expensive branches. In this case, this isn't a comparison
3953 that can be merged. Avoid doing this if the RHS is a floating-point
3954 comparison since those can trap. */
3956 if (BRANCH_COST
>= 2
3957 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
3958 && simple_operand_p (rl_arg
)
3959 && simple_operand_p (rr_arg
))
3960 return build (code
, truth_type
, lhs
, rhs
);
3962 /* See if the comparisons can be merged. Then get all the parameters for
3965 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
3966 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
3970 ll_inner
= decode_field_reference (ll_arg
,
3971 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
3972 &ll_unsignedp
, &volatilep
, &ll_mask
,
3974 lr_inner
= decode_field_reference (lr_arg
,
3975 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
3976 &lr_unsignedp
, &volatilep
, &lr_mask
,
3978 rl_inner
= decode_field_reference (rl_arg
,
3979 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
3980 &rl_unsignedp
, &volatilep
, &rl_mask
,
3982 rr_inner
= decode_field_reference (rr_arg
,
3983 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
3984 &rr_unsignedp
, &volatilep
, &rr_mask
,
3987 /* It must be true that the inner operation on the lhs of each
3988 comparison must be the same if we are to be able to do anything.
3989 Then see if we have constants. If not, the same must be true for
3991 if (volatilep
|| ll_inner
== 0 || rl_inner
== 0
3992 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
3995 if (TREE_CODE (lr_arg
) == INTEGER_CST
3996 && TREE_CODE (rr_arg
) == INTEGER_CST
)
3997 l_const
= lr_arg
, r_const
= rr_arg
;
3998 else if (lr_inner
== 0 || rr_inner
== 0
3999 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
4002 l_const
= r_const
= 0;
4004 /* If either comparison code is not correct for our logical operation,
4005 fail. However, we can convert a one-bit comparison against zero into
4006 the opposite comparison against that bit being set in the field. */
4008 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
4009 if (lcode
!= wanted_code
)
4011 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
4013 /* Make the left operand unsigned, since we are only interested
4014 in the value of one bit. Otherwise we are doing the wrong
4023 /* This is analogous to the code for l_const above. */
4024 if (rcode
!= wanted_code
)
4026 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
4035 /* See if we can find a mode that contains both fields being compared on
4036 the left. If we can't, fail. Otherwise, update all constants and masks
4037 to be relative to a field of that size. */
4038 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
4039 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
4040 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
,
4041 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
4043 if (lnmode
== VOIDmode
)
4046 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
4047 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
4048 lntype
= type_for_size (lnbitsize
, 1);
4049 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
4051 if (BYTES_BIG_ENDIAN
)
4053 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
4054 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
4057 ll_mask
= const_binop (LSHIFT_EXPR
, convert (lntype
, ll_mask
),
4058 size_int (xll_bitpos
), 0);
4059 rl_mask
= const_binop (LSHIFT_EXPR
, convert (lntype
, rl_mask
),
4060 size_int (xrl_bitpos
), 0);
4064 l_const
= convert (lntype
, l_const
);
4065 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
4066 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
), 0);
4067 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
4068 fold (build1 (BIT_NOT_EXPR
,
4072 warning ("comparison is always %d", wanted_code
== NE_EXPR
);
4074 return convert (truth_type
,
4075 wanted_code
== NE_EXPR
4076 ? integer_one_node
: integer_zero_node
);
4081 r_const
= convert (lntype
, r_const
);
4082 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
4083 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
), 0);
4084 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
4085 fold (build1 (BIT_NOT_EXPR
,
4089 warning ("comparison is always %d", wanted_code
== NE_EXPR
);
4091 return convert (truth_type
,
4092 wanted_code
== NE_EXPR
4093 ? integer_one_node
: integer_zero_node
);
4097 /* If the right sides are not constant, do the same for it. Also,
4098 disallow this optimization if a size or signedness mismatch occurs
4099 between the left and right sides. */
4102 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
4103 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
4104 /* Make sure the two fields on the right
4105 correspond to the left without being swapped. */
4106 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
4109 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
4110 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
4111 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
,
4112 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
4114 if (rnmode
== VOIDmode
)
4117 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
4118 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
4119 rntype
= type_for_size (rnbitsize
, 1);
4120 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
4122 if (BYTES_BIG_ENDIAN
)
4124 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
4125 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
4128 lr_mask
= const_binop (LSHIFT_EXPR
, convert (rntype
, lr_mask
),
4129 size_int (xlr_bitpos
), 0);
4130 rr_mask
= const_binop (LSHIFT_EXPR
, convert (rntype
, rr_mask
),
4131 size_int (xrr_bitpos
), 0);
4133 /* Make a mask that corresponds to both fields being compared.
4134 Do this for both items being compared. If the operands are the
4135 same size and the bits being compared are in the same position
4136 then we can do this by masking both and comparing the masked
4138 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
, 0);
4139 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
, 0);
4140 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
4142 lhs
= make_bit_field_ref (ll_inner
, lntype
, lnbitsize
, lnbitpos
,
4143 ll_unsignedp
|| rl_unsignedp
);
4144 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
4145 lhs
= build (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
4147 rhs
= make_bit_field_ref (lr_inner
, rntype
, rnbitsize
, rnbitpos
,
4148 lr_unsignedp
|| rr_unsignedp
);
4149 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
4150 rhs
= build (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
4152 return build (wanted_code
, truth_type
, lhs
, rhs
);
4155 /* There is still another way we can do something: If both pairs of
4156 fields being compared are adjacent, we may be able to make a wider
4157 field containing them both.
4159 Note that we still must mask the lhs/rhs expressions. Furthermore,
4160 the mask must be shifted to account for the shift done by
4161 make_bit_field_ref. */
4162 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
4163 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
4164 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
4165 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
4169 lhs
= make_bit_field_ref (ll_inner
, lntype
, ll_bitsize
+ rl_bitsize
,
4170 MIN (ll_bitpos
, rl_bitpos
), ll_unsignedp
);
4171 rhs
= make_bit_field_ref (lr_inner
, rntype
, lr_bitsize
+ rr_bitsize
,
4172 MIN (lr_bitpos
, rr_bitpos
), lr_unsignedp
);
4174 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
4175 size_int (MIN (xll_bitpos
, xrl_bitpos
)), 0);
4176 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
4177 size_int (MIN (xlr_bitpos
, xrr_bitpos
)), 0);
4179 /* Convert to the smaller type before masking out unwanted bits. */
4181 if (lntype
!= rntype
)
4183 if (lnbitsize
> rnbitsize
)
4185 lhs
= convert (rntype
, lhs
);
4186 ll_mask
= convert (rntype
, ll_mask
);
4189 else if (lnbitsize
< rnbitsize
)
4191 rhs
= convert (lntype
, rhs
);
4192 lr_mask
= convert (lntype
, lr_mask
);
4197 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
4198 lhs
= build (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
4200 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
4201 rhs
= build (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
4203 return build (wanted_code
, truth_type
, lhs
, rhs
);
4209 /* Handle the case of comparisons with constants. If there is something in
4210 common between the masks, those bits of the constants must be the same.
4211 If not, the condition is always false. Test for this to avoid generating
4212 incorrect code below. */
4213 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
, 0);
4214 if (! integer_zerop (result
)
4215 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
, 0),
4216 const_binop (BIT_AND_EXPR
, result
, r_const
, 0)) != 1)
4218 if (wanted_code
== NE_EXPR
)
4220 warning ("`or' of unmatched not-equal tests is always 1");
4221 return convert (truth_type
, integer_one_node
);
4225 warning ("`and' of mutually exclusive equal-tests is always 0");
4226 return convert (truth_type
, integer_zero_node
);
4230 /* Construct the expression we will return. First get the component
4231 reference we will make. Unless the mask is all ones the width of
4232 that field, perform the mask operation. Then compare with the
4234 result
= make_bit_field_ref (ll_inner
, lntype
, lnbitsize
, lnbitpos
,
4235 ll_unsignedp
|| rl_unsignedp
);
4237 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
, 0);
4238 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
4239 result
= build (BIT_AND_EXPR
, lntype
, result
, ll_mask
);
4241 return build (wanted_code
, truth_type
, result
,
4242 const_binop (BIT_IOR_EXPR
, l_const
, r_const
, 0));
4245 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4249 optimize_minmax_comparison (t
)
4252 tree type
= TREE_TYPE (t
);
4253 tree arg0
= TREE_OPERAND (t
, 0);
4254 enum tree_code op_code
;
4255 tree comp_const
= TREE_OPERAND (t
, 1);
4257 int consts_equal
, consts_lt
;
4260 STRIP_SIGN_NOPS (arg0
);
4262 op_code
= TREE_CODE (arg0
);
4263 minmax_const
= TREE_OPERAND (arg0
, 1);
4264 consts_equal
= tree_int_cst_equal (minmax_const
, comp_const
);
4265 consts_lt
= tree_int_cst_lt (minmax_const
, comp_const
);
4266 inner
= TREE_OPERAND (arg0
, 0);
4268 /* If something does not permit us to optimize, return the original tree. */
4269 if ((op_code
!= MIN_EXPR
&& op_code
!= MAX_EXPR
)
4270 || TREE_CODE (comp_const
) != INTEGER_CST
4271 || TREE_CONSTANT_OVERFLOW (comp_const
)
4272 || TREE_CODE (minmax_const
) != INTEGER_CST
4273 || TREE_CONSTANT_OVERFLOW (minmax_const
))
4276 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4277 and GT_EXPR, doing the rest with recursive calls using logical
4279 switch (TREE_CODE (t
))
4281 case NE_EXPR
: case LT_EXPR
: case LE_EXPR
:
4283 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t
)));
4287 fold (build (TRUTH_ORIF_EXPR
, type
,
4288 optimize_minmax_comparison
4289 (build (EQ_EXPR
, type
, arg0
, comp_const
)),
4290 optimize_minmax_comparison
4291 (build (GT_EXPR
, type
, arg0
, comp_const
))));
4294 if (op_code
== MAX_EXPR
&& consts_equal
)
4295 /* MAX (X, 0) == 0 -> X <= 0 */
4296 return fold (build (LE_EXPR
, type
, inner
, comp_const
));
4298 else if (op_code
== MAX_EXPR
&& consts_lt
)
4299 /* MAX (X, 0) == 5 -> X == 5 */
4300 return fold (build (EQ_EXPR
, type
, inner
, comp_const
));
4302 else if (op_code
== MAX_EXPR
)
4303 /* MAX (X, 0) == -1 -> false */
4304 return omit_one_operand (type
, integer_zero_node
, inner
);
4306 else if (consts_equal
)
4307 /* MIN (X, 0) == 0 -> X >= 0 */
4308 return fold (build (GE_EXPR
, type
, inner
, comp_const
));
4311 /* MIN (X, 0) == 5 -> false */
4312 return omit_one_operand (type
, integer_zero_node
, inner
);
4315 /* MIN (X, 0) == -1 -> X == -1 */
4316 return fold (build (EQ_EXPR
, type
, inner
, comp_const
));
4319 if (op_code
== MAX_EXPR
&& (consts_equal
|| consts_lt
))
4320 /* MAX (X, 0) > 0 -> X > 0
4321 MAX (X, 0) > 5 -> X > 5 */
4322 return fold (build (GT_EXPR
, type
, inner
, comp_const
));
4324 else if (op_code
== MAX_EXPR
)
4325 /* MAX (X, 0) > -1 -> true */
4326 return omit_one_operand (type
, integer_one_node
, inner
);
4328 else if (op_code
== MIN_EXPR
&& (consts_equal
|| consts_lt
))
4329 /* MIN (X, 0) > 0 -> false
4330 MIN (X, 0) > 5 -> false */
4331 return omit_one_operand (type
, integer_zero_node
, inner
);
4334 /* MIN (X, 0) > -1 -> X > -1 */
4335 return fold (build (GT_EXPR
, type
, inner
, comp_const
));
4342 /* T is an integer expression that is being multiplied, divided, or taken a
4343 modulus (CODE says which and what kind of divide or modulus) by a
4344 constant C. See if we can eliminate that operation by folding it with
4345 other operations already in T. WIDE_TYPE, if non-null, is a type that
4346 should be used for the computation if wider than our type.
4348 For example, if we are dividing (X * 8) + (Y + 16) by 4, we can return
4349 (X * 2) + (Y + 4). We must, however, be assured that either the original
4350 expression would not overflow or that overflow is undefined for the type
4351 in the language in question.
4353 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4354 the machine has a multiply-accumulate insn or that this is part of an
4355 addressing calculation.
4357 If we return a non-null expression, it is an equivalent form of the
4358 original computation, but need not be in the original type. */
4361 extract_muldiv (t
, c
, code
, wide_type
)
4364 enum tree_code code
;
4367 tree type
= TREE_TYPE (t
);
4368 enum tree_code tcode
= TREE_CODE (t
);
4369 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
4370 > GET_MODE_SIZE (TYPE_MODE (type
)))
4371 ? wide_type
: type
);
4373 int same_p
= tcode
== code
;
4374 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
4376 /* Don't deal with constants of zero here; they confuse the code below. */
4377 if (integer_zerop (c
))
4380 if (TREE_CODE_CLASS (tcode
) == '1')
4381 op0
= TREE_OPERAND (t
, 0);
4383 if (TREE_CODE_CLASS (tcode
) == '2')
4384 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
4386 /* Note that we need not handle conditional operations here since fold
4387 already handles those cases. So just do arithmetic here. */
4391 /* For a constant, we can always simplify if we are a multiply
4392 or (for divide and modulus) if it is a multiple of our constant. */
4393 if (code
== MULT_EXPR
4394 || integer_zerop (const_binop (TRUNC_MOD_EXPR
, t
, c
, 0)))
4395 return const_binop (code
, convert (ctype
, t
), convert (ctype
, c
), 0);
4398 case CONVERT_EXPR
: case NON_LVALUE_EXPR
: case NOP_EXPR
:
4399 /* If op0 is an expression, and is unsigned, and the type is
4400 smaller than ctype, then we cannot widen the expression. */
4401 if ((TREE_CODE_CLASS (TREE_CODE (op0
)) == '<'
4402 || TREE_CODE_CLASS (TREE_CODE (op0
)) == '1'
4403 || TREE_CODE_CLASS (TREE_CODE (op0
)) == '2'
4404 || TREE_CODE_CLASS (TREE_CODE (op0
)) == 'e')
4405 && TREE_UNSIGNED (TREE_TYPE (op0
))
4406 && ! (TREE_CODE (TREE_TYPE (op0
)) == INTEGER_TYPE
4407 && TYPE_IS_SIZETYPE (TREE_TYPE (op0
)))
4408 && (GET_MODE_SIZE (TYPE_MODE (ctype
))
4409 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0
)))))
4412 /* Pass the constant down and see if we can make a simplification. If
4413 we can, replace this expression with the inner simplification for
4414 possible later conversion to our or some other type. */
4415 if (0 != (t1
= extract_muldiv (op0
, convert (TREE_TYPE (op0
), c
), code
,
4416 code
== MULT_EXPR
? ctype
: NULL_TREE
)))
4420 case NEGATE_EXPR
: case ABS_EXPR
:
4421 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
)) != 0)
4422 return fold (build1 (tcode
, ctype
, convert (ctype
, t1
)));
4425 case MIN_EXPR
: case MAX_EXPR
:
4426 /* If widening the type changes the signedness, then we can't perform
4427 this optimization as that changes the result. */
4428 if (TREE_UNSIGNED (ctype
) != TREE_UNSIGNED (type
))
4431 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4432 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
)) != 0
4433 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
)) != 0)
4435 if (tree_int_cst_sgn (c
) < 0)
4436 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
4438 return fold (build (tcode
, ctype
, convert (ctype
, t1
),
4439 convert (ctype
, t2
)));
4443 case WITH_RECORD_EXPR
:
4444 if ((t1
= extract_muldiv (TREE_OPERAND (t
, 0), c
, code
, wide_type
)) != 0)
4445 return build (WITH_RECORD_EXPR
, TREE_TYPE (t1
), t1
,
4446 TREE_OPERAND (t
, 1));
4450 /* If this has not been evaluated and the operand has no side effects,
4451 we can see if we can do something inside it and make a new one.
4452 Note that this test is overly conservative since we can do this
4453 if the only reason it had side effects is that it was another
4454 similar SAVE_EXPR, but that isn't worth bothering with. */
4455 if (SAVE_EXPR_RTL (t
) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0))
4456 && 0 != (t1
= extract_muldiv (TREE_OPERAND (t
, 0), c
, code
,
4459 t1
= save_expr (t1
);
4460 if (SAVE_EXPR_PERSISTENT_P (t
) && TREE_CODE (t1
) == SAVE_EXPR
)
4461 SAVE_EXPR_PERSISTENT_P (t1
) = 1;
4462 if (is_pending_size (t
))
4463 put_pending_size (t1
);
4468 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
4469 /* If the second operand is constant, this is a multiplication
4470 or floor division, by a power of two, so we can treat it that
4471 way unless the multiplier or divisor overflows. */
4472 if (TREE_CODE (op1
) == INTEGER_CST
4473 /* const_binop may not detect overflow correctly,
4474 so check for it explicitly here. */
4475 && TYPE_PRECISION (TREE_TYPE (size_one_node
)) > TREE_INT_CST_LOW (op1
)
4476 && TREE_INT_CST_HIGH (op1
) == 0
4477 && 0 != (t1
= convert (ctype
,
4478 const_binop (LSHIFT_EXPR
, size_one_node
,
4480 && ! TREE_OVERFLOW (t1
))
4481 return extract_muldiv (build (tcode
== LSHIFT_EXPR
4482 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
4483 ctype
, convert (ctype
, op0
), t1
),
4484 c
, code
, wide_type
);
4487 case PLUS_EXPR
: case MINUS_EXPR
:
4488 /* See if we can eliminate the operation on both sides. If we can, we
4489 can return a new PLUS or MINUS. If we can't, the only remaining
4490 cases where we can do anything are if the second operand is a
4492 t1
= extract_muldiv (op0
, c
, code
, wide_type
);
4493 t2
= extract_muldiv (op1
, c
, code
, wide_type
);
4494 if (t1
!= 0 && t2
!= 0
4495 && (code
== MULT_EXPR
4496 /* If not multiplication, we can only do this if either operand
4497 is divisible by c. */
4498 || multiple_of_p (ctype
, op0
, c
)
4499 || multiple_of_p (ctype
, op1
, c
)))
4500 return fold (build (tcode
, ctype
, convert (ctype
, t1
),
4501 convert (ctype
, t2
)));
4503 /* If this was a subtraction, negate OP1 and set it to be an addition.
4504 This simplifies the logic below. */
4505 if (tcode
== MINUS_EXPR
)
4506 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
4508 if (TREE_CODE (op1
) != INTEGER_CST
)
4511 /* If either OP1 or C are negative, this optimization is not safe for
4512 some of the division and remainder types while for others we need
4513 to change the code. */
4514 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
4516 if (code
== CEIL_DIV_EXPR
)
4517 code
= FLOOR_DIV_EXPR
;
4518 else if (code
== CEIL_MOD_EXPR
)
4519 code
= FLOOR_MOD_EXPR
;
4520 else if (code
== FLOOR_DIV_EXPR
)
4521 code
= CEIL_DIV_EXPR
;
4522 else if (code
== FLOOR_MOD_EXPR
)
4523 code
= CEIL_MOD_EXPR
;
4524 else if (code
!= MULT_EXPR
)
4528 /* If it's a multiply or a division/modulus operation of a multiple
4529 of our constant, do the operation and verify it doesn't overflow. */
4530 if (code
== MULT_EXPR
4531 || integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
, 0)))
4533 op1
= const_binop (code
, convert (ctype
, op1
), convert (ctype
, c
), 0);
4534 if (op1
== 0 || TREE_OVERFLOW (op1
))
4540 /* If we have an unsigned type is not a sizetype, we cannot widen
4541 the operation since it will change the result if the original
4542 computation overflowed. */
4543 if (TREE_UNSIGNED (ctype
)
4544 && ! (TREE_CODE (ctype
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (ctype
))
4548 /* If we were able to eliminate our operation from the first side,
4549 apply our operation to the second side and reform the PLUS. */
4550 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
4551 return fold (build (tcode
, ctype
, convert (ctype
, t1
), op1
));
4553 /* The last case is if we are a multiply. In that case, we can
4554 apply the distributive law to commute the multiply and addition
4555 if the multiplication of the constants doesn't overflow. */
4556 if (code
== MULT_EXPR
)
4557 return fold (build (tcode
, ctype
, fold (build (code
, ctype
,
4558 convert (ctype
, op0
),
4559 convert (ctype
, c
))),
4565 /* We have a special case here if we are doing something like
4566 (C * 8) % 4 since we know that's zero. */
4567 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
4568 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
4569 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
4570 && integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
, 0)))
4571 return omit_one_operand (type
, integer_zero_node
, op0
);
4573 /* ... fall through ... */
4575 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
4576 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
4577 /* If we can extract our operation from the LHS, do so and return a
4578 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4579 do something only if the second operand is a constant. */
4581 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
)) != 0)
4582 return fold (build (tcode
, ctype
, convert (ctype
, t1
),
4583 convert (ctype
, op1
)));
4584 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
4585 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
)) != 0)
4586 return fold (build (tcode
, ctype
, convert (ctype
, op0
),
4587 convert (ctype
, t1
)));
4588 else if (TREE_CODE (op1
) != INTEGER_CST
)
4591 /* If these are the same operation types, we can associate them
4592 assuming no overflow. */
4594 && 0 != (t1
= const_binop (MULT_EXPR
, convert (ctype
, op1
),
4595 convert (ctype
, c
), 0))
4596 && ! TREE_OVERFLOW (t1
))
4597 return fold (build (tcode
, ctype
, convert (ctype
, op0
), t1
));
4599 /* If these operations "cancel" each other, we have the main
4600 optimizations of this pass, which occur when either constant is a
4601 multiple of the other, in which case we replace this with either an
4602 operation or CODE or TCODE.
4604 If we have an unsigned type that is not a sizetype, we canot do
4605 this since it will change the result if the original computation
4607 if ((! TREE_UNSIGNED (ctype
)
4608 || (TREE_CODE (ctype
) == INTEGER_TYPE
&& TYPE_IS_SIZETYPE (ctype
)))
4609 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
4610 || (tcode
== MULT_EXPR
4611 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
4612 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
)))
4614 if (integer_zerop (const_binop (TRUNC_MOD_EXPR
, op1
, c
, 0)))
4615 return fold (build (tcode
, ctype
, convert (ctype
, op0
),
4617 const_binop (TRUNC_DIV_EXPR
,
4619 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR
, c
, op1
, 0)))
4620 return fold (build (code
, ctype
, convert (ctype
, op0
),
4622 const_binop (TRUNC_DIV_EXPR
,
4634 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4635 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4636 that we may sometimes modify the tree. */
4639 strip_compound_expr (t
, s
)
4643 enum tree_code code
= TREE_CODE (t
);
4645 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4646 if (code
== COMPOUND_EXPR
&& TREE_CODE (TREE_OPERAND (t
, 0)) == CONVERT_EXPR
4647 && TREE_OPERAND (TREE_OPERAND (t
, 0), 0) == s
)
4648 return TREE_OPERAND (t
, 1);
4650 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4651 don't bother handling any other types. */
4652 else if (code
== COND_EXPR
)
4654 TREE_OPERAND (t
, 0) = strip_compound_expr (TREE_OPERAND (t
, 0), s
);
4655 TREE_OPERAND (t
, 1) = strip_compound_expr (TREE_OPERAND (t
, 1), s
);
4656 TREE_OPERAND (t
, 2) = strip_compound_expr (TREE_OPERAND (t
, 2), s
);
4658 else if (TREE_CODE_CLASS (code
) == '1')
4659 TREE_OPERAND (t
, 0) = strip_compound_expr (TREE_OPERAND (t
, 0), s
);
4660 else if (TREE_CODE_CLASS (code
) == '<'
4661 || TREE_CODE_CLASS (code
) == '2')
4663 TREE_OPERAND (t
, 0) = strip_compound_expr (TREE_OPERAND (t
, 0), s
);
4664 TREE_OPERAND (t
, 1) = strip_compound_expr (TREE_OPERAND (t
, 1), s
);
4670 /* Return a node which has the indicated constant VALUE (either 0 or
4671 1), and is of the indicated TYPE. */
4674 constant_boolean_node (value
, type
)
4678 if (type
== integer_type_node
)
4679 return value
? integer_one_node
: integer_zero_node
;
4680 else if (TREE_CODE (type
) == BOOLEAN_TYPE
)
4681 return truthvalue_conversion (value
? integer_one_node
:
4685 tree t
= build_int_2 (value
, 0);
4687 TREE_TYPE (t
) = type
;
4692 /* Utility function for the following routine, to see how complex a nesting of
4693 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4694 we don't care (to avoid spending too much time on complex expressions.). */
4697 count_cond (expr
, lim
)
4703 if (TREE_CODE (expr
) != COND_EXPR
)
4708 ctrue
= count_cond (TREE_OPERAND (expr
, 1), lim
- 1);
4709 cfalse
= count_cond (TREE_OPERAND (expr
, 2), lim
- 1 - ctrue
);
4710 return MIN (lim
, 1 + ctrue
+ cfalse
);
4713 /* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4714 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4715 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4716 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4717 COND is the first argument to CODE; otherwise (as in the example
4718 given here), it is the second argument. TYPE is the type of the
4719 original expression. */
4722 fold_binary_op_with_conditional_arg (code
, type
, cond
, arg
, cond_first_p
)
4723 enum tree_code code
;
4729 tree test
, true_value
, false_value
;
4730 tree lhs
= NULL_TREE
;
4731 tree rhs
= NULL_TREE
;
4732 /* In the end, we'll produce a COND_EXPR. Both arms of the
4733 conditional expression will be binary operations. The left-hand
4734 side of the expression to be executed if the condition is true
4735 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4736 of the expression to be executed if the condition is true will be
4737 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analagous --
4738 but apply to the expression to be executed if the conditional is
4744 /* These are the codes to use for the left-hand side and right-hand
4745 side of the COND_EXPR. Normally, they are the same as CODE. */
4746 enum tree_code lhs_code
= code
;
4747 enum tree_code rhs_code
= code
;
4748 /* And these are the types of the expressions. */
4749 tree lhs_type
= type
;
4750 tree rhs_type
= type
;
4754 true_rhs
= false_rhs
= &arg
;
4755 true_lhs
= &true_value
;
4756 false_lhs
= &false_value
;
4760 true_lhs
= false_lhs
= &arg
;
4761 true_rhs
= &true_value
;
4762 false_rhs
= &false_value
;
4765 if (TREE_CODE (cond
) == COND_EXPR
)
4767 test
= TREE_OPERAND (cond
, 0);
4768 true_value
= TREE_OPERAND (cond
, 1);
4769 false_value
= TREE_OPERAND (cond
, 2);
4770 /* If this operand throws an expression, then it does not make
4771 sense to try to perform a logical or arithmetic operation
4772 involving it. Instead of building `a + throw 3' for example,
4773 we simply build `a, throw 3'. */
4774 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
4776 lhs_code
= COMPOUND_EXPR
;
4778 lhs_type
= void_type_node
;
4780 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
4782 rhs_code
= COMPOUND_EXPR
;
4784 rhs_type
= void_type_node
;
4789 tree testtype
= TREE_TYPE (cond
);
4791 true_value
= convert (testtype
, integer_one_node
);
4792 false_value
= convert (testtype
, integer_zero_node
);
4795 /* If ARG is complex we want to make sure we only evaluate
4796 it once. Though this is only required if it is volatile, it
4797 might be more efficient even if it is not. However, if we
4798 succeed in folding one part to a constant, we do not need
4799 to make this SAVE_EXPR. Since we do this optimization
4800 primarily to see if we do end up with constant and this
4801 SAVE_EXPR interferes with later optimizations, suppressing
4802 it when we can is important.
4804 If we are not in a function, we can't make a SAVE_EXPR, so don't
4805 try to do so. Don't try to see if the result is a constant
4806 if an arm is a COND_EXPR since we get exponential behavior
4809 if (TREE_CODE (arg
) != SAVE_EXPR
&& ! TREE_CONSTANT (arg
)
4810 && global_bindings_p () == 0
4811 && ((TREE_CODE (arg
) != VAR_DECL
4812 && TREE_CODE (arg
) != PARM_DECL
)
4813 || TREE_SIDE_EFFECTS (arg
)))
4815 if (TREE_CODE (true_value
) != COND_EXPR
)
4816 lhs
= fold (build (lhs_code
, lhs_type
, *true_lhs
, *true_rhs
));
4818 if (TREE_CODE (false_value
) != COND_EXPR
)
4819 rhs
= fold (build (rhs_code
, rhs_type
, *false_lhs
, *false_rhs
));
4821 if ((lhs
== 0 || ! TREE_CONSTANT (lhs
))
4822 && (rhs
== 0 || !TREE_CONSTANT (rhs
)))
4823 arg
= save_expr (arg
), lhs
= rhs
= 0;
4827 lhs
= fold (build (lhs_code
, lhs_type
, *true_lhs
, *true_rhs
));
4829 rhs
= fold (build (rhs_code
, rhs_type
, *false_lhs
, *false_rhs
));
4831 test
= fold (build (COND_EXPR
, type
, test
, lhs
, rhs
));
4833 if (TREE_CODE (arg
) == SAVE_EXPR
)
4834 return build (COMPOUND_EXPR
, type
,
4835 convert (void_type_node
, arg
),
4836 strip_compound_expr (test
, arg
));
4838 return convert (type
, test
);
4842 /* Perform constant folding and related simplification of EXPR.
4843 The related simplifications include x*1 => x, x*0 => 0, etc.,
4844 and application of the associative law.
4845 NOP_EXPR conversions may be removed freely (as long as we
4846 are careful not to change the C type of the overall expression)
4847 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4848 but we can constant-fold them if they have constant operands. */
4854 register tree t
= expr
;
4855 tree t1
= NULL_TREE
;
4857 tree type
= TREE_TYPE (expr
);
4858 register tree arg0
= NULL_TREE
, arg1
= NULL_TREE
;
4859 register enum tree_code code
= TREE_CODE (t
);
4860 register int kind
= TREE_CODE_CLASS (code
);
4862 /* WINS will be nonzero when the switch is done
4863 if all operands are constant. */
4866 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4867 Likewise for a SAVE_EXPR that's already been evaluated. */
4868 if (code
== RTL_EXPR
|| (code
== SAVE_EXPR
&& SAVE_EXPR_RTL (t
) != 0))
4871 /* Return right away if a constant. */
4875 #ifdef MAX_INTEGER_COMPUTATION_MODE
4876 check_max_integer_computation_mode (expr
);
4879 if (code
== NOP_EXPR
|| code
== FLOAT_EXPR
|| code
== CONVERT_EXPR
)
4883 /* Special case for conversion ops that can have fixed point args. */
4884 arg0
= TREE_OPERAND (t
, 0);
4886 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
4888 STRIP_SIGN_NOPS (arg0
);
4890 if (arg0
!= 0 && TREE_CODE (arg0
) == COMPLEX_CST
)
4891 subop
= TREE_REALPART (arg0
);
4895 if (subop
!= 0 && TREE_CODE (subop
) != INTEGER_CST
4896 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4897 && TREE_CODE (subop
) != REAL_CST
4898 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4900 /* Note that TREE_CONSTANT isn't enough:
4901 static var addresses are constant but we can't
4902 do arithmetic on them. */
4905 else if (IS_EXPR_CODE_CLASS (kind
) || kind
== 'r')
4907 register int len
= first_rtl_op (code
);
4909 for (i
= 0; i
< len
; i
++)
4911 tree op
= TREE_OPERAND (t
, i
);
4915 continue; /* Valid for CALL_EXPR, at least. */
4917 if (kind
== '<' || code
== RSHIFT_EXPR
)
4919 /* Signedness matters here. Perhaps we can refine this
4921 STRIP_SIGN_NOPS (op
);
4924 /* Strip any conversions that don't change the mode. */
4927 if (TREE_CODE (op
) == COMPLEX_CST
)
4928 subop
= TREE_REALPART (op
);
4932 if (TREE_CODE (subop
) != INTEGER_CST
4933 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4934 && TREE_CODE (subop
) != REAL_CST
4935 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4937 /* Note that TREE_CONSTANT isn't enough:
4938 static var addresses are constant but we can't
4939 do arithmetic on them. */
4949 /* If this is a commutative operation, and ARG0 is a constant, move it
4950 to ARG1 to reduce the number of tests below. */
4951 if ((code
== PLUS_EXPR
|| code
== MULT_EXPR
|| code
== MIN_EXPR
4952 || code
== MAX_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
4953 || code
== BIT_AND_EXPR
)
4954 && (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
))
4956 tem
= arg0
; arg0
= arg1
; arg1
= tem
;
4958 tem
= TREE_OPERAND (t
, 0); TREE_OPERAND (t
, 0) = TREE_OPERAND (t
, 1);
4959 TREE_OPERAND (t
, 1) = tem
;
4962 /* Now WINS is set as described above,
4963 ARG0 is the first operand of EXPR,
4964 and ARG1 is the second operand (if it has more than one operand).
4966 First check for cases where an arithmetic operation is applied to a
4967 compound, conditional, or comparison operation. Push the arithmetic
4968 operation inside the compound or conditional to see if any folding
4969 can then be done. Convert comparison to conditional for this purpose.
4970 The also optimizes non-constant cases that used to be done in
4973 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
4974 one of the operands is a comparison and the other is a comparison, a
4975 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
4976 code below would make the expression more complex. Change it to a
4977 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
4978 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
4980 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
4981 || code
== EQ_EXPR
|| code
== NE_EXPR
)
4982 && ((truth_value_p (TREE_CODE (arg0
))
4983 && (truth_value_p (TREE_CODE (arg1
))
4984 || (TREE_CODE (arg1
) == BIT_AND_EXPR
4985 && integer_onep (TREE_OPERAND (arg1
, 1)))))
4986 || (truth_value_p (TREE_CODE (arg1
))
4987 && (truth_value_p (TREE_CODE (arg0
))
4988 || (TREE_CODE (arg0
) == BIT_AND_EXPR
4989 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
4991 t
= fold (build (code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
4992 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
4996 if (code
== EQ_EXPR
)
4997 t
= invert_truthvalue (t
);
5002 if (TREE_CODE_CLASS (code
) == '1')
5004 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
5005 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5006 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 1))));
5007 else if (TREE_CODE (arg0
) == COND_EXPR
)
5009 t
= fold (build (COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5010 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 1))),
5011 fold (build1 (code
, type
, TREE_OPERAND (arg0
, 2)))));
5013 /* If this was a conversion, and all we did was to move into
5014 inside the COND_EXPR, bring it back out. But leave it if
5015 it is a conversion from integer to integer and the
5016 result precision is no wider than a word since such a
5017 conversion is cheap and may be optimized away by combine,
5018 while it couldn't if it were outside the COND_EXPR. Then return
5019 so we don't get into an infinite recursion loop taking the
5020 conversion out and then back in. */
5022 if ((code
== NOP_EXPR
|| code
== CONVERT_EXPR
5023 || code
== NON_LVALUE_EXPR
)
5024 && TREE_CODE (t
) == COND_EXPR
5025 && TREE_CODE (TREE_OPERAND (t
, 1)) == code
5026 && TREE_CODE (TREE_OPERAND (t
, 2)) == code
5027 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 1), 0))
5028 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 2), 0)))
5029 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t
))
5031 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 1), 0))))
5032 && TYPE_PRECISION (TREE_TYPE (t
)) <= BITS_PER_WORD
))
5033 t
= build1 (code
, type
,
5035 TREE_TYPE (TREE_OPERAND
5036 (TREE_OPERAND (t
, 1), 0)),
5037 TREE_OPERAND (t
, 0),
5038 TREE_OPERAND (TREE_OPERAND (t
, 1), 0),
5039 TREE_OPERAND (TREE_OPERAND (t
, 2), 0)));
5042 else if (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<')
5043 return fold (build (COND_EXPR
, type
, arg0
,
5044 fold (build1 (code
, type
, integer_one_node
)),
5045 fold (build1 (code
, type
, integer_zero_node
))));
5047 else if (TREE_CODE_CLASS (code
) == '2'
5048 || TREE_CODE_CLASS (code
) == '<')
5050 if (TREE_CODE (arg1
) == COMPOUND_EXPR
)
5051 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
5052 fold (build (code
, type
,
5053 arg0
, TREE_OPERAND (arg1
, 1))));
5054 else if ((TREE_CODE (arg1
) == COND_EXPR
5055 || (TREE_CODE_CLASS (TREE_CODE (arg1
)) == '<'
5056 && TREE_CODE_CLASS (code
) != '<'))
5057 && (TREE_CODE (arg0
) != COND_EXPR
5058 || count_cond (arg0
, 25) + count_cond (arg1
, 25) <= 25)
5059 && (! TREE_SIDE_EFFECTS (arg0
)
5060 || (global_bindings_p () == 0
5061 && ! contains_placeholder_p (arg0
))))
5063 fold_binary_op_with_conditional_arg (code
, type
, arg1
, arg0
,
5064 /*cond_first_p=*/0);
5065 else if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
5066 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5067 fold (build (code
, type
, TREE_OPERAND (arg0
, 1), arg1
)));
5068 else if ((TREE_CODE (arg0
) == COND_EXPR
5069 || (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
5070 && TREE_CODE_CLASS (code
) != '<'))
5071 && (TREE_CODE (arg1
) != COND_EXPR
5072 || count_cond (arg0
, 25) + count_cond (arg1
, 25) <= 25)
5073 && (! TREE_SIDE_EFFECTS (arg1
)
5074 || (global_bindings_p () == 0
5075 && ! contains_placeholder_p (arg1
))))
5077 fold_binary_op_with_conditional_arg (code
, type
, arg0
, arg1
,
5078 /*cond_first_p=*/1);
5080 else if (TREE_CODE_CLASS (code
) == '<'
5081 && TREE_CODE (arg0
) == COMPOUND_EXPR
)
5082 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5083 fold (build (code
, type
, TREE_OPERAND (arg0
, 1), arg1
)));
5084 else if (TREE_CODE_CLASS (code
) == '<'
5085 && TREE_CODE (arg1
) == COMPOUND_EXPR
)
5086 return build (COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
5087 fold (build (code
, type
, arg0
, TREE_OPERAND (arg1
, 1))));
5099 return fold (DECL_INITIAL (t
));
5104 case FIX_TRUNC_EXPR
:
5105 /* Other kinds of FIX are not handled properly by fold_convert. */
5107 if (TREE_TYPE (TREE_OPERAND (t
, 0)) == TREE_TYPE (t
))
5108 return TREE_OPERAND (t
, 0);
5110 /* Handle cases of two conversions in a row. */
5111 if (TREE_CODE (TREE_OPERAND (t
, 0)) == NOP_EXPR
5112 || TREE_CODE (TREE_OPERAND (t
, 0)) == CONVERT_EXPR
)
5114 tree inside_type
= TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5115 tree inter_type
= TREE_TYPE (TREE_OPERAND (t
, 0));
5116 tree final_type
= TREE_TYPE (t
);
5117 int inside_int
= INTEGRAL_TYPE_P (inside_type
);
5118 int inside_ptr
= POINTER_TYPE_P (inside_type
);
5119 int inside_float
= FLOAT_TYPE_P (inside_type
);
5120 unsigned int inside_prec
= TYPE_PRECISION (inside_type
);
5121 int inside_unsignedp
= TREE_UNSIGNED (inside_type
);
5122 int inter_int
= INTEGRAL_TYPE_P (inter_type
);
5123 int inter_ptr
= POINTER_TYPE_P (inter_type
);
5124 int inter_float
= FLOAT_TYPE_P (inter_type
);
5125 unsigned int inter_prec
= TYPE_PRECISION (inter_type
);
5126 int inter_unsignedp
= TREE_UNSIGNED (inter_type
);
5127 int final_int
= INTEGRAL_TYPE_P (final_type
);
5128 int final_ptr
= POINTER_TYPE_P (final_type
);
5129 int final_float
= FLOAT_TYPE_P (final_type
);
5130 unsigned int final_prec
= TYPE_PRECISION (final_type
);
5131 int final_unsignedp
= TREE_UNSIGNED (final_type
);
5133 /* In addition to the cases of two conversions in a row
5134 handled below, if we are converting something to its own
5135 type via an object of identical or wider precision, neither
5136 conversion is needed. */
5137 if (TYPE_MAIN_VARIANT (inside_type
) == TYPE_MAIN_VARIANT (final_type
)
5138 && ((inter_int
&& final_int
) || (inter_float
&& final_float
))
5139 && inter_prec
>= final_prec
)
5140 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5142 /* Likewise, if the intermediate and final types are either both
5143 float or both integer, we don't need the middle conversion if
5144 it is wider than the final type and doesn't change the signedness
5145 (for integers). Avoid this if the final type is a pointer
5146 since then we sometimes need the inner conversion. Likewise if
5147 the outer has a precision not equal to the size of its mode. */
5148 if ((((inter_int
|| inter_ptr
) && (inside_int
|| inside_ptr
))
5149 || (inter_float
&& inside_float
))
5150 && inter_prec
>= inside_prec
5151 && (inter_float
|| inter_unsignedp
== inside_unsignedp
)
5152 && ! (final_prec
!= GET_MODE_BITSIZE (TYPE_MODE (final_type
))
5153 && TYPE_MODE (final_type
) == TYPE_MODE (inter_type
))
5155 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5157 /* If we have a sign-extension of a zero-extended value, we can
5158 replace that by a single zero-extension. */
5159 if (inside_int
&& inter_int
&& final_int
5160 && inside_prec
< inter_prec
&& inter_prec
< final_prec
5161 && inside_unsignedp
&& !inter_unsignedp
)
5162 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5164 /* Two conversions in a row are not needed unless:
5165 - some conversion is floating-point (overstrict for now), or
5166 - the intermediate type is narrower than both initial and
5168 - the intermediate type and innermost type differ in signedness,
5169 and the outermost type is wider than the intermediate, or
5170 - the initial type is a pointer type and the precisions of the
5171 intermediate and final types differ, or
5172 - the final type is a pointer type and the precisions of the
5173 initial and intermediate types differ. */
5174 if (! inside_float
&& ! inter_float
&& ! final_float
5175 && (inter_prec
> inside_prec
|| inter_prec
> final_prec
)
5176 && ! (inside_int
&& inter_int
5177 && inter_unsignedp
!= inside_unsignedp
5178 && inter_prec
< final_prec
)
5179 && ((inter_unsignedp
&& inter_prec
> inside_prec
)
5180 == (final_unsignedp
&& final_prec
> inter_prec
))
5181 && ! (inside_ptr
&& inter_prec
!= final_prec
)
5182 && ! (final_ptr
&& inside_prec
!= inter_prec
)
5183 && ! (final_prec
!= GET_MODE_BITSIZE (TYPE_MODE (final_type
))
5184 && TYPE_MODE (final_type
) == TYPE_MODE (inter_type
))
5186 return convert (final_type
, TREE_OPERAND (TREE_OPERAND (t
, 0), 0));
5189 if (TREE_CODE (TREE_OPERAND (t
, 0)) == MODIFY_EXPR
5190 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t
, 0), 1))
5191 /* Detect assigning a bitfield. */
5192 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t
, 0), 0)) == COMPONENT_REF
5193 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t
, 0), 0), 1))))
5195 /* Don't leave an assignment inside a conversion
5196 unless assigning a bitfield. */
5197 tree prev
= TREE_OPERAND (t
, 0);
5198 TREE_OPERAND (t
, 0) = TREE_OPERAND (prev
, 1);
5199 /* First do the assignment, then return converted constant. */
5200 t
= build (COMPOUND_EXPR
, TREE_TYPE (t
), prev
, fold (t
));
5206 TREE_CONSTANT (t
) = TREE_CONSTANT (arg0
);
5209 return fold_convert (t
, arg0
);
5211 #if 0 /* This loses on &"foo"[0]. */
5216 /* Fold an expression like: "foo"[2] */
5217 if (TREE_CODE (arg0
) == STRING_CST
5218 && TREE_CODE (arg1
) == INTEGER_CST
5219 && compare_tree_int (arg1
, TREE_STRING_LENGTH (arg0
)) < 0)
5221 t
= build_int_2 (TREE_STRING_POINTER (arg0
)[TREE_INT_CST_LOW (arg
))], 0);
5222 TREE_TYPE (t
) = TREE_TYPE (TREE_TYPE (arg0
));
5223 force_fit_type (t
, 0);
5230 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
5232 tree m
= purpose_member (arg1
, CONSTRUCTOR_ELTS (arg0
));
5239 TREE_CONSTANT (t
) = wins
;
5245 if (TREE_CODE (arg0
) == INTEGER_CST
)
5247 unsigned HOST_WIDE_INT low
;
5249 int overflow
= neg_double (TREE_INT_CST_LOW (arg0
),
5250 TREE_INT_CST_HIGH (arg0
),
5252 t
= build_int_2 (low
, high
);
5253 TREE_TYPE (t
) = type
;
5255 = (TREE_OVERFLOW (arg0
)
5256 | force_fit_type (t
, overflow
&& !TREE_UNSIGNED (type
)));
5257 TREE_CONSTANT_OVERFLOW (t
)
5258 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg0
);
5260 else if (TREE_CODE (arg0
) == REAL_CST
)
5261 t
= build_real (type
, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0
)));
5263 else if (TREE_CODE (arg0
) == NEGATE_EXPR
)
5264 return TREE_OPERAND (arg0
, 0);
5266 /* Convert - (a - b) to (b - a) for non-floating-point. */
5267 else if (TREE_CODE (arg0
) == MINUS_EXPR
5268 && (! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
))
5269 return build (MINUS_EXPR
, type
, TREE_OPERAND (arg0
, 1),
5270 TREE_OPERAND (arg0
, 0));
5277 if (TREE_CODE (arg0
) == INTEGER_CST
)
5279 /* If the value is unsigned, then the absolute value is
5280 the same as the ordinary value. */
5281 if (TREE_UNSIGNED (type
))
5283 /* Similarly, if the value is non-negative. */
5284 else if (INT_CST_LT (integer_minus_one_node
, arg0
))
5286 /* If the value is negative, then the absolute value is
5290 unsigned HOST_WIDE_INT low
;
5292 int overflow
= neg_double (TREE_INT_CST_LOW (arg0
),
5293 TREE_INT_CST_HIGH (arg0
),
5295 t
= build_int_2 (low
, high
);
5296 TREE_TYPE (t
) = type
;
5298 = (TREE_OVERFLOW (arg0
)
5299 | force_fit_type (t
, overflow
));
5300 TREE_CONSTANT_OVERFLOW (t
)
5301 = TREE_OVERFLOW (t
) | TREE_CONSTANT_OVERFLOW (arg0
);
5304 else if (TREE_CODE (arg0
) == REAL_CST
)
5306 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
5307 t
= build_real (type
,
5308 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0
)));
5311 else if (TREE_CODE (arg0
) == ABS_EXPR
|| TREE_CODE (arg0
) == NEGATE_EXPR
)
5312 return build1 (ABS_EXPR
, type
, TREE_OPERAND (arg0
, 0));
5316 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
5317 return convert (type
, arg0
);
5318 else if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
5319 return build (COMPLEX_EXPR
, type
,
5320 TREE_OPERAND (arg0
, 0),
5321 negate_expr (TREE_OPERAND (arg0
, 1)));
5322 else if (TREE_CODE (arg0
) == COMPLEX_CST
)
5323 return build_complex (type
, TREE_REALPART (arg0
),
5324 negate_expr (TREE_IMAGPART (arg0
)));
5325 else if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
5326 return fold (build (TREE_CODE (arg0
), type
,
5327 fold (build1 (CONJ_EXPR
, type
,
5328 TREE_OPERAND (arg0
, 0))),
5329 fold (build1 (CONJ_EXPR
,
5330 type
, TREE_OPERAND (arg0
, 1)))));
5331 else if (TREE_CODE (arg0
) == CONJ_EXPR
)
5332 return TREE_OPERAND (arg0
, 0);
5338 t
= build_int_2 (~ TREE_INT_CST_LOW (arg0
),
5339 ~ TREE_INT_CST_HIGH (arg0
));
5340 TREE_TYPE (t
) = type
;
5341 force_fit_type (t
, 0);
5342 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg0
);
5343 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (arg0
);
5345 else if (TREE_CODE (arg0
) == BIT_NOT_EXPR
)
5346 return TREE_OPERAND (arg0
, 0);
5350 /* A + (-B) -> A - B */
5351 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
5352 return fold (build (MINUS_EXPR
, type
, arg0
, TREE_OPERAND (arg1
, 0)));
5353 /* (-A) + B -> B - A */
5354 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
5355 return fold (build (MINUS_EXPR
, type
, arg1
, TREE_OPERAND (arg0
, 0)));
5356 else if (! FLOAT_TYPE_P (type
))
5358 if (integer_zerop (arg1
))
5359 return non_lvalue (convert (type
, arg0
));
5361 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5362 with a constant, and the two constants have no bits in common,
5363 we should treat this as a BIT_IOR_EXPR since this may produce more
5365 if (TREE_CODE (arg0
) == BIT_AND_EXPR
5366 && TREE_CODE (arg1
) == BIT_AND_EXPR
5367 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
5368 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
5369 && integer_zerop (const_binop (BIT_AND_EXPR
,
5370 TREE_OPERAND (arg0
, 1),
5371 TREE_OPERAND (arg1
, 1), 0)))
5373 code
= BIT_IOR_EXPR
;
5377 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5378 (plus (plus (mult) (mult)) (foo)) so that we can
5379 take advantage of the factoring cases below. */
5380 if ((TREE_CODE (arg0
) == PLUS_EXPR
5381 && TREE_CODE (arg1
) == MULT_EXPR
)
5382 || (TREE_CODE (arg1
) == PLUS_EXPR
5383 && TREE_CODE (arg0
) == MULT_EXPR
))
5385 tree parg0
, parg1
, parg
, marg
;
5387 if (TREE_CODE (arg0
) == PLUS_EXPR
)
5388 parg
= arg0
, marg
= arg1
;
5390 parg
= arg1
, marg
= arg0
;
5391 parg0
= TREE_OPERAND (parg
, 0);
5392 parg1
= TREE_OPERAND (parg
, 1);
5396 if (TREE_CODE (parg0
) == MULT_EXPR
5397 && TREE_CODE (parg1
) != MULT_EXPR
)
5398 return fold (build (PLUS_EXPR
, type
,
5399 fold (build (PLUS_EXPR
, type
, parg0
, marg
)),
5401 if (TREE_CODE (parg0
) != MULT_EXPR
5402 && TREE_CODE (parg1
) == MULT_EXPR
)
5403 return fold (build (PLUS_EXPR
, type
,
5404 fold (build (PLUS_EXPR
, type
, parg1
, marg
)),
5408 if (TREE_CODE (arg0
) == MULT_EXPR
&& TREE_CODE (arg1
) == MULT_EXPR
)
5410 tree arg00
, arg01
, arg10
, arg11
;
5411 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
5413 /* (A * C) + (B * C) -> (A+B) * C.
5414 We are most concerned about the case where C is a constant,
5415 but other combinations show up during loop reduction. Since
5416 it is not difficult, try all four possibilities. */
5418 arg00
= TREE_OPERAND (arg0
, 0);
5419 arg01
= TREE_OPERAND (arg0
, 1);
5420 arg10
= TREE_OPERAND (arg1
, 0);
5421 arg11
= TREE_OPERAND (arg1
, 1);
5424 if (operand_equal_p (arg01
, arg11
, 0))
5425 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
5426 else if (operand_equal_p (arg00
, arg10
, 0))
5427 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
5428 else if (operand_equal_p (arg00
, arg11
, 0))
5429 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
5430 else if (operand_equal_p (arg01
, arg10
, 0))
5431 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
5433 /* No identical multiplicands; see if we can find a common
5434 power-of-two factor in non-power-of-two multiplies. This
5435 can help in multi-dimensional array access. */
5436 else if (TREE_CODE (arg01
) == INTEGER_CST
5437 && TREE_CODE (arg11
) == INTEGER_CST
5438 && TREE_INT_CST_HIGH (arg01
) == 0
5439 && TREE_INT_CST_HIGH (arg11
) == 0)
5441 HOST_WIDE_INT int01
, int11
, tmp
;
5442 int01
= TREE_INT_CST_LOW (arg01
);
5443 int11
= TREE_INT_CST_LOW (arg11
);
5445 /* Move min of absolute values to int11. */
5446 if ((int01
>= 0 ? int01
: -int01
)
5447 < (int11
>= 0 ? int11
: -int11
))
5449 tmp
= int01
, int01
= int11
, int11
= tmp
;
5450 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
5451 alt0
= arg01
, arg01
= arg11
, arg11
= alt0
;
5454 if (exact_log2 (int11
) > 0 && int01
% int11
== 0)
5456 alt0
= fold (build (MULT_EXPR
, type
, arg00
,
5457 build_int_2 (int01
/ int11
, 0)));
5464 return fold (build (MULT_EXPR
, type
,
5465 fold (build (PLUS_EXPR
, type
, alt0
, alt1
)),
5469 /* In IEEE floating point, x+0 may not equal x. */
5470 else if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
5471 || flag_unsafe_math_optimizations
)
5472 && real_zerop (arg1
))
5473 return non_lvalue (convert (type
, arg0
));
5474 /* x+(-0) equals x, even for IEEE. */
5475 else if (TREE_CODE (arg1
) == REAL_CST
5476 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1
)))
5477 return non_lvalue (convert (type
, arg0
));
5480 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5481 is a rotate of A by C1 bits. */
5482 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5483 is a rotate of A by B bits. */
5485 register enum tree_code code0
, code1
;
5486 code0
= TREE_CODE (arg0
);
5487 code1
= TREE_CODE (arg1
);
5488 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
5489 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
5490 && operand_equal_p (TREE_OPERAND (arg0
, 0),
5491 TREE_OPERAND (arg1
, 0), 0)
5492 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
5494 register tree tree01
, tree11
;
5495 register enum tree_code code01
, code11
;
5497 tree01
= TREE_OPERAND (arg0
, 1);
5498 tree11
= TREE_OPERAND (arg1
, 1);
5499 STRIP_NOPS (tree01
);
5500 STRIP_NOPS (tree11
);
5501 code01
= TREE_CODE (tree01
);
5502 code11
= TREE_CODE (tree11
);
5503 if (code01
== INTEGER_CST
5504 && code11
== INTEGER_CST
5505 && TREE_INT_CST_HIGH (tree01
) == 0
5506 && TREE_INT_CST_HIGH (tree11
) == 0
5507 && ((TREE_INT_CST_LOW (tree01
) + TREE_INT_CST_LOW (tree11
))
5508 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
5509 return build (LROTATE_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5510 code0
== LSHIFT_EXPR
? tree01
: tree11
);
5511 else if (code11
== MINUS_EXPR
)
5513 tree tree110
, tree111
;
5514 tree110
= TREE_OPERAND (tree11
, 0);
5515 tree111
= TREE_OPERAND (tree11
, 1);
5516 STRIP_NOPS (tree110
);
5517 STRIP_NOPS (tree111
);
5518 if (TREE_CODE (tree110
) == INTEGER_CST
5519 && 0 == compare_tree_int (tree110
,
5521 (TREE_TYPE (TREE_OPERAND
5523 && operand_equal_p (tree01
, tree111
, 0))
5524 return build ((code0
== LSHIFT_EXPR
5527 type
, TREE_OPERAND (arg0
, 0), tree01
);
5529 else if (code01
== MINUS_EXPR
)
5531 tree tree010
, tree011
;
5532 tree010
= TREE_OPERAND (tree01
, 0);
5533 tree011
= TREE_OPERAND (tree01
, 1);
5534 STRIP_NOPS (tree010
);
5535 STRIP_NOPS (tree011
);
5536 if (TREE_CODE (tree010
) == INTEGER_CST
5537 && 0 == compare_tree_int (tree010
,
5539 (TREE_TYPE (TREE_OPERAND
5541 && operand_equal_p (tree11
, tree011
, 0))
5542 return build ((code0
!= LSHIFT_EXPR
5545 type
, TREE_OPERAND (arg0
, 0), tree11
);
5551 /* In most languages, can't associate operations on floats through
5552 parentheses. Rather than remember where the parentheses were, we
5553 don't associate floats at all. It shouldn't matter much. However,
5554 associating multiplications is only very slightly inaccurate, so do
5555 that if -funsafe-math-optimizations is specified. */
5558 && (! FLOAT_TYPE_P (type
)
5559 || (flag_unsafe_math_optimizations
&& code
== MULT_EXPR
)))
5561 tree var0
, con0
, lit0
, var1
, con1
, lit1
;
5563 /* Split both trees into variables, constants, and literals. Then
5564 associate each group together, the constants with literals,
5565 then the result with variables. This increases the chances of
5566 literals being recombined later and of generating relocatable
5567 expressions for the sum of a constant and literal. */
5568 var0
= split_tree (arg0
, code
, &con0
, &lit0
, 0);
5569 var1
= split_tree (arg1
, code
, &con1
, &lit1
, code
== MINUS_EXPR
);
5571 /* Only do something if we found more than two objects. Otherwise,
5572 nothing has changed and we risk infinite recursion. */
5573 if (2 < ((var0
!= 0) + (var1
!= 0) + (con0
!= 0) + (con1
!= 0)
5574 + (lit0
!= 0) + (lit1
!= 0)))
5576 var0
= associate_trees (var0
, var1
, code
, type
);
5577 con0
= associate_trees (con0
, con1
, code
, type
);
5578 lit0
= associate_trees (lit0
, lit1
, code
, type
);
5579 con0
= associate_trees (con0
, lit0
, code
, type
);
5580 return convert (type
, associate_trees (var0
, con0
, code
, type
));
5585 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5586 if (TREE_CODE (arg1
) == REAL_CST
)
5588 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5590 t1
= const_binop (code
, arg0
, arg1
, 0);
5591 if (t1
!= NULL_TREE
)
5593 /* The return value should always have
5594 the same type as the original expression. */
5595 if (TREE_TYPE (t1
) != TREE_TYPE (t
))
5596 t1
= convert (TREE_TYPE (t
), t1
);
5603 /* A - (-B) -> A + B */
5604 if (TREE_CODE (arg1
) == NEGATE_EXPR
)
5605 return fold (build (PLUS_EXPR
, type
, arg0
, TREE_OPERAND (arg1
, 0)));
5606 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5607 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& TREE_CODE (arg1
) == REAL_CST
)
5609 fold (build (MINUS_EXPR
, type
,
5610 build_real (TREE_TYPE (arg1
),
5611 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1
))),
5612 TREE_OPERAND (arg0
, 0)));
5614 if (! FLOAT_TYPE_P (type
))
5616 if (! wins
&& integer_zerop (arg0
))
5617 return negate_expr (convert (type
, arg1
));
5618 if (integer_zerop (arg1
))
5619 return non_lvalue (convert (type
, arg0
));
5621 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5622 about the case where C is a constant, just try one of the
5623 four possibilities. */
5625 if (TREE_CODE (arg0
) == MULT_EXPR
&& TREE_CODE (arg1
) == MULT_EXPR
5626 && operand_equal_p (TREE_OPERAND (arg0
, 1),
5627 TREE_OPERAND (arg1
, 1), 0))
5628 return fold (build (MULT_EXPR
, type
,
5629 fold (build (MINUS_EXPR
, type
,
5630 TREE_OPERAND (arg0
, 0),
5631 TREE_OPERAND (arg1
, 0))),
5632 TREE_OPERAND (arg0
, 1)));
5635 else if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
5636 || flag_unsafe_math_optimizations
)
5638 /* Except with IEEE floating point, 0-x equals -x. */
5639 if (! wins
&& real_zerop (arg0
))
5640 return negate_expr (convert (type
, arg1
));
5641 /* Except with IEEE floating point, x-0 equals x. */
5642 if (real_zerop (arg1
))
5643 return non_lvalue (convert (type
, arg0
));
5646 /* Fold &x - &x. This can happen from &x.foo - &x.
5647 This is unsafe for certain floats even in non-IEEE formats.
5648 In IEEE, it is unsafe because it does wrong for NaNs.
5649 Also note that operand_equal_p is always false if an operand
5652 if ((! FLOAT_TYPE_P (type
) || flag_unsafe_math_optimizations
)
5653 && operand_equal_p (arg0
, arg1
, 0))
5654 return convert (type
, integer_zero_node
);
5659 /* (-A) * (-B) -> A * B */
5660 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& TREE_CODE (arg1
) == NEGATE_EXPR
)
5661 return fold (build (MULT_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5662 TREE_OPERAND (arg1
, 0)));
5664 if (! FLOAT_TYPE_P (type
))
5666 if (integer_zerop (arg1
))
5667 return omit_one_operand (type
, arg1
, arg0
);
5668 if (integer_onep (arg1
))
5669 return non_lvalue (convert (type
, arg0
));
5671 /* (a * (1 << b)) is (a << b) */
5672 if (TREE_CODE (arg1
) == LSHIFT_EXPR
5673 && integer_onep (TREE_OPERAND (arg1
, 0)))
5674 return fold (build (LSHIFT_EXPR
, type
, arg0
,
5675 TREE_OPERAND (arg1
, 1)));
5676 if (TREE_CODE (arg0
) == LSHIFT_EXPR
5677 && integer_onep (TREE_OPERAND (arg0
, 0)))
5678 return fold (build (LSHIFT_EXPR
, type
, arg1
,
5679 TREE_OPERAND (arg0
, 1)));
5681 if (TREE_CODE (arg1
) == INTEGER_CST
5682 && 0 != (tem
= extract_muldiv (TREE_OPERAND (t
, 0), arg1
,
5684 return convert (type
, tem
);
5689 /* x*0 is 0, except for IEEE floating point. */
5690 if ((TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
5691 || flag_unsafe_math_optimizations
)
5692 && real_zerop (arg1
))
5693 return omit_one_operand (type
, arg1
, arg0
);
5694 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5695 However, ANSI says we can drop signals,
5696 so we can do this anyway. */
5697 if (real_onep (arg1
))
5698 return non_lvalue (convert (type
, arg0
));
5700 if (! wins
&& real_twop (arg1
) && global_bindings_p () == 0
5701 && ! contains_placeholder_p (arg0
))
5703 tree arg
= save_expr (arg0
);
5704 return build (PLUS_EXPR
, type
, arg
, arg
);
5711 if (integer_all_onesp (arg1
))
5712 return omit_one_operand (type
, arg1
, arg0
);
5713 if (integer_zerop (arg1
))
5714 return non_lvalue (convert (type
, arg0
));
5715 t1
= distribute_bit_expr (code
, type
, arg0
, arg1
);
5716 if (t1
!= NULL_TREE
)
5719 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5721 This results in more efficient code for machines without a NAND
5722 instruction. Combine will canonicalize to the first form
5723 which will allow use of NAND instructions provided by the
5724 backend if they exist. */
5725 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
5726 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
5728 return fold (build1 (BIT_NOT_EXPR
, type
,
5729 build (BIT_AND_EXPR
, type
,
5730 TREE_OPERAND (arg0
, 0),
5731 TREE_OPERAND (arg1
, 0))));
5734 /* See if this can be simplified into a rotate first. If that
5735 is unsuccessful continue in the association code. */
5739 if (integer_zerop (arg1
))
5740 return non_lvalue (convert (type
, arg0
));
5741 if (integer_all_onesp (arg1
))
5742 return fold (build1 (BIT_NOT_EXPR
, type
, arg0
));
5744 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5745 with a constant, and the two constants have no bits in common,
5746 we should treat this as a BIT_IOR_EXPR since this may produce more
5748 if (TREE_CODE (arg0
) == BIT_AND_EXPR
5749 && TREE_CODE (arg1
) == BIT_AND_EXPR
5750 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
5751 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
5752 && integer_zerop (const_binop (BIT_AND_EXPR
,
5753 TREE_OPERAND (arg0
, 1),
5754 TREE_OPERAND (arg1
, 1), 0)))
5756 code
= BIT_IOR_EXPR
;
5760 /* See if this can be simplified into a rotate first. If that
5761 is unsuccessful continue in the association code. */
5766 if (integer_all_onesp (arg1
))
5767 return non_lvalue (convert (type
, arg0
));
5768 if (integer_zerop (arg1
))
5769 return omit_one_operand (type
, arg1
, arg0
);
5770 t1
= distribute_bit_expr (code
, type
, arg0
, arg1
);
5771 if (t1
!= NULL_TREE
)
5773 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5774 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == NOP_EXPR
5775 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
5778 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1
, 0)));
5780 if (prec
< BITS_PER_WORD
&& prec
< HOST_BITS_PER_WIDE_INT
5781 && (~TREE_INT_CST_LOW (arg0
)
5782 & (((HOST_WIDE_INT
) 1 << prec
) - 1)) == 0)
5783 return build1 (NOP_EXPR
, type
, TREE_OPERAND (arg1
, 0));
5785 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
5786 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
5789 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
5791 if (prec
< BITS_PER_WORD
&& prec
< HOST_BITS_PER_WIDE_INT
5792 && (~TREE_INT_CST_LOW (arg1
)
5793 & (((HOST_WIDE_INT
) 1 << prec
) - 1)) == 0)
5794 return build1 (NOP_EXPR
, type
, TREE_OPERAND (arg0
, 0));
5797 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5799 This results in more efficient code for machines without a NOR
5800 instruction. Combine will canonicalize to the first form
5801 which will allow use of NOR instructions provided by the
5802 backend if they exist. */
5803 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
5804 && TREE_CODE (arg1
) == BIT_NOT_EXPR
)
5806 return fold (build1 (BIT_NOT_EXPR
, type
,
5807 build (BIT_IOR_EXPR
, type
,
5808 TREE_OPERAND (arg0
, 0),
5809 TREE_OPERAND (arg1
, 0))));
5814 case BIT_ANDTC_EXPR
:
5815 if (integer_all_onesp (arg0
))
5816 return non_lvalue (convert (type
, arg1
));
5817 if (integer_zerop (arg0
))
5818 return omit_one_operand (type
, arg0
, arg1
);
5819 if (TREE_CODE (arg1
) == INTEGER_CST
)
5821 arg1
= fold (build1 (BIT_NOT_EXPR
, type
, arg1
));
5822 code
= BIT_AND_EXPR
;
5828 /* In most cases, do nothing with a divide by zero. */
5829 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5830 #ifndef REAL_INFINITY
5831 if (TREE_CODE (arg1
) == REAL_CST
&& real_zerop (arg1
))
5834 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5836 /* (-A) / (-B) -> A / B */
5837 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& TREE_CODE (arg1
) == NEGATE_EXPR
)
5838 return fold (build (RDIV_EXPR
, type
, TREE_OPERAND (arg0
, 0),
5839 TREE_OPERAND (arg1
, 0)));
5841 /* In IEEE floating point, x/1 is not equivalent to x for snans.
5842 However, ANSI says we can drop signals, so we can do this anyway. */
5843 if (real_onep (arg1
))
5844 return non_lvalue (convert (type
, arg0
));
5846 /* If ARG1 is a constant, we can convert this to a multiply by the
5847 reciprocal. This does not have the same rounding properties,
5848 so only do this if -funsafe-math-optimizations. We can actually
5849 always safely do it if ARG1 is a power of two, but it's hard to
5850 tell if it is or not in a portable manner. */
5851 if (TREE_CODE (arg1
) == REAL_CST
)
5853 if (flag_unsafe_math_optimizations
5854 && 0 != (tem
= const_binop (code
, build_real (type
, dconst1
),
5856 return fold (build (MULT_EXPR
, type
, arg0
, tem
));
5857 /* Find the reciprocal if optimizing and the result is exact. */
5861 r
= TREE_REAL_CST (arg1
);
5862 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0
)), &r
))
5864 tem
= build_real (type
, r
);
5865 return fold (build (MULT_EXPR
, type
, arg0
, tem
));
5871 case TRUNC_DIV_EXPR
:
5872 case ROUND_DIV_EXPR
:
5873 case FLOOR_DIV_EXPR
:
5875 case EXACT_DIV_EXPR
:
5876 if (integer_onep (arg1
))
5877 return non_lvalue (convert (type
, arg0
));
5878 if (integer_zerop (arg1
))
5881 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
5882 operation, EXACT_DIV_EXPR.
5884 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
5885 At one time others generated faster code, it's not clear if they do
5886 after the last round to changes to the DIV code in expmed.c. */
5887 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
5888 && multiple_of_p (type
, arg0
, arg1
))
5889 return fold (build (EXACT_DIV_EXPR
, type
, arg0
, arg1
));
5891 if (TREE_CODE (arg1
) == INTEGER_CST
5892 && 0 != (tem
= extract_muldiv (TREE_OPERAND (t
, 0), arg1
,
5894 return convert (type
, tem
);
5899 case FLOOR_MOD_EXPR
:
5900 case ROUND_MOD_EXPR
:
5901 case TRUNC_MOD_EXPR
:
5902 if (integer_onep (arg1
))
5903 return omit_one_operand (type
, integer_zero_node
, arg0
);
5904 if (integer_zerop (arg1
))
5907 if (TREE_CODE (arg1
) == INTEGER_CST
5908 && 0 != (tem
= extract_muldiv (TREE_OPERAND (t
, 0), arg1
,
5910 return convert (type
, tem
);
5918 if (integer_zerop (arg1
))
5919 return non_lvalue (convert (type
, arg0
));
5920 /* Since negative shift count is not well-defined,
5921 don't try to compute it in the compiler. */
5922 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
5924 /* Rewrite an LROTATE_EXPR by a constant into an
5925 RROTATE_EXPR by a new constant. */
5926 if (code
== LROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
)
5928 TREE_SET_CODE (t
, RROTATE_EXPR
);
5929 code
= RROTATE_EXPR
;
5930 TREE_OPERAND (t
, 1) = arg1
5933 convert (TREE_TYPE (arg1
),
5934 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type
)), 0)),
5936 if (tree_int_cst_sgn (arg1
) < 0)
5940 /* If we have a rotate of a bit operation with the rotate count and
5941 the second operand of the bit operation both constant,
5942 permute the two operations. */
5943 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
5944 && (TREE_CODE (arg0
) == BIT_AND_EXPR
5945 || TREE_CODE (arg0
) == BIT_ANDTC_EXPR
5946 || TREE_CODE (arg0
) == BIT_IOR_EXPR
5947 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
5948 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
5949 return fold (build (TREE_CODE (arg0
), type
,
5950 fold (build (code
, type
,
5951 TREE_OPERAND (arg0
, 0), arg1
)),
5952 fold (build (code
, type
,
5953 TREE_OPERAND (arg0
, 1), arg1
))));
5955 /* Two consecutive rotates adding up to the width of the mode can
5957 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
5958 && TREE_CODE (arg0
) == RROTATE_EXPR
5959 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
5960 && TREE_INT_CST_HIGH (arg1
) == 0
5961 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0
, 1)) == 0
5962 && ((TREE_INT_CST_LOW (arg1
)
5963 + TREE_INT_CST_LOW (TREE_OPERAND (arg0
, 1)))
5964 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type
))))
5965 return TREE_OPERAND (arg0
, 0);
5970 if (operand_equal_p (arg0
, arg1
, 0))
5971 return omit_one_operand (type
, arg0
, arg1
);
5972 if (INTEGRAL_TYPE_P (type
)
5973 && operand_equal_p (arg1
, TYPE_MIN_VALUE (type
), 1))
5974 return omit_one_operand (type
, arg1
, arg0
);
5978 if (operand_equal_p (arg0
, arg1
, 0))
5979 return omit_one_operand (type
, arg0
, arg1
);
5980 if (INTEGRAL_TYPE_P (type
)
5981 && TYPE_MAX_VALUE (type
)
5982 && operand_equal_p (arg1
, TYPE_MAX_VALUE (type
), 1))
5983 return omit_one_operand (type
, arg1
, arg0
);
5986 case TRUTH_NOT_EXPR
:
5987 /* Note that the operand of this must be an int
5988 and its values must be 0 or 1.
5989 ("true" is a fixed value perhaps depending on the language,
5990 but we don't handle values other than 1 correctly yet.) */
5991 tem
= invert_truthvalue (arg0
);
5992 /* Avoid infinite recursion. */
5993 if (TREE_CODE (tem
) == TRUTH_NOT_EXPR
)
5995 return convert (type
, tem
);
5997 case TRUTH_ANDIF_EXPR
:
5998 /* Note that the operands of this must be ints
5999 and their values must be 0 or 1.
6000 ("true" is a fixed value perhaps depending on the language.) */
6001 /* If first arg is constant zero, return it. */
6002 if (integer_zerop (arg0
))
6003 return convert (type
, arg0
);
6004 case TRUTH_AND_EXPR
:
6005 /* If either arg is constant true, drop it. */
6006 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
6007 return non_lvalue (convert (type
, arg1
));
6008 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
6009 /* Preserve sequence points. */
6010 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
6011 return non_lvalue (convert (type
, arg0
));
6012 /* If second arg is constant zero, result is zero, but first arg
6013 must be evaluated. */
6014 if (integer_zerop (arg1
))
6015 return omit_one_operand (type
, arg1
, arg0
);
6016 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6017 case will be handled here. */
6018 if (integer_zerop (arg0
))
6019 return omit_one_operand (type
, arg0
, arg1
);
6022 /* We only do these simplifications if we are optimizing. */
6026 /* Check for things like (A || B) && (A || C). We can convert this
6027 to A || (B && C). Note that either operator can be any of the four
6028 truth and/or operations and the transformation will still be
6029 valid. Also note that we only care about order for the
6030 ANDIF and ORIF operators. If B contains side effects, this
6031 might change the truth-value of A. */
6032 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
6033 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
6034 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
6035 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
6036 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
6037 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
6039 tree a00
= TREE_OPERAND (arg0
, 0);
6040 tree a01
= TREE_OPERAND (arg0
, 1);
6041 tree a10
= TREE_OPERAND (arg1
, 0);
6042 tree a11
= TREE_OPERAND (arg1
, 1);
6043 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
6044 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
6045 && (code
== TRUTH_AND_EXPR
6046 || code
== TRUTH_OR_EXPR
));
6048 if (operand_equal_p (a00
, a10
, 0))
6049 return fold (build (TREE_CODE (arg0
), type
, a00
,
6050 fold (build (code
, type
, a01
, a11
))));
6051 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
6052 return fold (build (TREE_CODE (arg0
), type
, a00
,
6053 fold (build (code
, type
, a01
, a10
))));
6054 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
6055 return fold (build (TREE_CODE (arg0
), type
, a01
,
6056 fold (build (code
, type
, a00
, a11
))));
6058 /* This case if tricky because we must either have commutative
6059 operators or else A10 must not have side-effects. */
6061 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
6062 && operand_equal_p (a01
, a11
, 0))
6063 return fold (build (TREE_CODE (arg0
), type
,
6064 fold (build (code
, type
, a00
, a10
)),
6068 /* See if we can build a range comparison. */
6069 if (0 != (tem
= fold_range_test (t
)))
6072 /* Check for the possibility of merging component references. If our
6073 lhs is another similar operation, try to merge its rhs with our
6074 rhs. Then try to merge our lhs and rhs. */
6075 if (TREE_CODE (arg0
) == code
6076 && 0 != (tem
= fold_truthop (code
, type
,
6077 TREE_OPERAND (arg0
, 1), arg1
)))
6078 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
6080 if ((tem
= fold_truthop (code
, type
, arg0
, arg1
)) != 0)
6085 case TRUTH_ORIF_EXPR
:
6086 /* Note that the operands of this must be ints
6087 and their values must be 0 or true.
6088 ("true" is a fixed value perhaps depending on the language.) */
6089 /* If first arg is constant true, return it. */
6090 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
6091 return convert (type
, arg0
);
6093 /* If either arg is constant zero, drop it. */
6094 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
6095 return non_lvalue (convert (type
, arg1
));
6096 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
6097 /* Preserve sequence points. */
6098 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
6099 return non_lvalue (convert (type
, arg0
));
6100 /* If second arg is constant true, result is true, but we must
6101 evaluate first arg. */
6102 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
6103 return omit_one_operand (type
, arg1
, arg0
);
6104 /* Likewise for first arg, but note this only occurs here for
6106 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
6107 return omit_one_operand (type
, arg0
, arg1
);
6110 case TRUTH_XOR_EXPR
:
6111 /* If either arg is constant zero, drop it. */
6112 if (integer_zerop (arg0
))
6113 return non_lvalue (convert (type
, arg1
));
6114 if (integer_zerop (arg1
))
6115 return non_lvalue (convert (type
, arg0
));
6116 /* If either arg is constant true, this is a logical inversion. */
6117 if (integer_onep (arg0
))
6118 return non_lvalue (convert (type
, invert_truthvalue (arg1
)));
6119 if (integer_onep (arg1
))
6120 return non_lvalue (convert (type
, invert_truthvalue (arg0
)));
6129 if (FLOAT_TYPE_P (TREE_TYPE (arg0
)))
6131 /* (-a) CMP (-b) -> b CMP a */
6132 if (TREE_CODE (arg0
) == NEGATE_EXPR
6133 && TREE_CODE (arg1
) == NEGATE_EXPR
)
6134 return fold (build (code
, type
, TREE_OPERAND (arg1
, 0),
6135 TREE_OPERAND (arg0
, 0)));
6136 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6137 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& TREE_CODE (arg1
) == REAL_CST
)
6140 (swap_tree_comparison (code
), type
,
6141 TREE_OPERAND (arg0
, 0),
6142 build_real (TREE_TYPE (arg1
),
6143 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1
)))));
6144 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6145 /* a CMP (-0) -> a CMP 0 */
6146 if (TREE_CODE (arg1
) == REAL_CST
6147 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1
)))
6148 return fold (build (code
, type
, arg0
,
6149 build_real (TREE_TYPE (arg1
), dconst0
)));
6152 /* If one arg is a constant integer, put it last. */
6153 if (TREE_CODE (arg0
) == INTEGER_CST
6154 && TREE_CODE (arg1
) != INTEGER_CST
)
6156 TREE_OPERAND (t
, 0) = arg1
;
6157 TREE_OPERAND (t
, 1) = arg0
;
6158 arg0
= TREE_OPERAND (t
, 0);
6159 arg1
= TREE_OPERAND (t
, 1);
6160 code
= swap_tree_comparison (code
);
6161 TREE_SET_CODE (t
, code
);
6164 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6165 First, see if one arg is constant; find the constant arg
6166 and the other one. */
6168 tree constop
= 0, varop
= NULL_TREE
;
6169 int constopnum
= -1;
6171 if (TREE_CONSTANT (arg1
))
6172 constopnum
= 1, constop
= arg1
, varop
= arg0
;
6173 if (TREE_CONSTANT (arg0
))
6174 constopnum
= 0, constop
= arg0
, varop
= arg1
;
6176 if (constop
&& TREE_CODE (varop
) == POSTINCREMENT_EXPR
)
6178 /* This optimization is invalid for ordered comparisons
6179 if CONST+INCR overflows or if foo+incr might overflow.
6180 This optimization is invalid for floating point due to rounding.
6181 For pointer types we assume overflow doesn't happen. */
6182 if (POINTER_TYPE_P (TREE_TYPE (varop
))
6183 || (! FLOAT_TYPE_P (TREE_TYPE (varop
))
6184 && (code
== EQ_EXPR
|| code
== NE_EXPR
)))
6187 = fold (build (PLUS_EXPR
, TREE_TYPE (varop
),
6188 constop
, TREE_OPERAND (varop
, 1)));
6190 /* Do not overwrite the current varop to be a preincrement,
6191 create a new node so that we won't confuse our caller who
6192 might create trees and throw them away, reusing the
6193 arguments that they passed to build. This shows up in
6194 the THEN or ELSE parts of ?: being postincrements. */
6195 varop
= build (PREINCREMENT_EXPR
, TREE_TYPE (varop
),
6196 TREE_OPERAND (varop
, 0),
6197 TREE_OPERAND (varop
, 1));
6199 /* If VAROP is a reference to a bitfield, we must mask
6200 the constant by the width of the field. */
6201 if (TREE_CODE (TREE_OPERAND (varop
, 0)) == COMPONENT_REF
6202 && DECL_BIT_FIELD(TREE_OPERAND
6203 (TREE_OPERAND (varop
, 0), 1)))
6206 = TREE_INT_CST_LOW (DECL_SIZE
6208 (TREE_OPERAND (varop
, 0), 1)));
6209 tree mask
, unsigned_type
;
6210 unsigned int precision
;
6211 tree folded_compare
;
6213 /* First check whether the comparison would come out
6214 always the same. If we don't do that we would
6215 change the meaning with the masking. */
6216 if (constopnum
== 0)
6217 folded_compare
= fold (build (code
, type
, constop
,
6218 TREE_OPERAND (varop
, 0)));
6220 folded_compare
= fold (build (code
, type
,
6221 TREE_OPERAND (varop
, 0),
6223 if (integer_zerop (folded_compare
)
6224 || integer_onep (folded_compare
))
6225 return omit_one_operand (type
, folded_compare
, varop
);
6227 unsigned_type
= type_for_size (size
, 1);
6228 precision
= TYPE_PRECISION (unsigned_type
);
6229 mask
= build_int_2 (~0, ~0);
6230 TREE_TYPE (mask
) = unsigned_type
;
6231 force_fit_type (mask
, 0);
6232 mask
= const_binop (RSHIFT_EXPR
, mask
,
6233 size_int (precision
- size
), 0);
6234 newconst
= fold (build (BIT_AND_EXPR
,
6235 TREE_TYPE (varop
), newconst
,
6236 convert (TREE_TYPE (varop
),
6240 t
= build (code
, type
,
6241 (constopnum
== 0) ? newconst
: varop
,
6242 (constopnum
== 1) ? newconst
: varop
);
6246 else if (constop
&& TREE_CODE (varop
) == POSTDECREMENT_EXPR
)
6248 if (POINTER_TYPE_P (TREE_TYPE (varop
))
6249 || (! FLOAT_TYPE_P (TREE_TYPE (varop
))
6250 && (code
== EQ_EXPR
|| code
== NE_EXPR
)))
6253 = fold (build (MINUS_EXPR
, TREE_TYPE (varop
),
6254 constop
, TREE_OPERAND (varop
, 1)));
6256 /* Do not overwrite the current varop to be a predecrement,
6257 create a new node so that we won't confuse our caller who
6258 might create trees and throw them away, reusing the
6259 arguments that they passed to build. This shows up in
6260 the THEN or ELSE parts of ?: being postdecrements. */
6261 varop
= build (PREDECREMENT_EXPR
, TREE_TYPE (varop
),
6262 TREE_OPERAND (varop
, 0),
6263 TREE_OPERAND (varop
, 1));
6265 if (TREE_CODE (TREE_OPERAND (varop
, 0)) == COMPONENT_REF
6266 && DECL_BIT_FIELD(TREE_OPERAND
6267 (TREE_OPERAND (varop
, 0), 1)))
6270 = TREE_INT_CST_LOW (DECL_SIZE
6272 (TREE_OPERAND (varop
, 0), 1)));
6273 tree mask
, unsigned_type
;
6274 unsigned int precision
;
6275 tree folded_compare
;
6277 if (constopnum
== 0)
6278 folded_compare
= fold (build (code
, type
, constop
,
6279 TREE_OPERAND (varop
, 0)));
6281 folded_compare
= fold (build (code
, type
,
6282 TREE_OPERAND (varop
, 0),
6284 if (integer_zerop (folded_compare
)
6285 || integer_onep (folded_compare
))
6286 return omit_one_operand (type
, folded_compare
, varop
);
6288 unsigned_type
= type_for_size (size
, 1);
6289 precision
= TYPE_PRECISION (unsigned_type
);
6290 mask
= build_int_2 (~0, ~0);
6291 TREE_TYPE (mask
) = TREE_TYPE (varop
);
6292 force_fit_type (mask
, 0);
6293 mask
= const_binop (RSHIFT_EXPR
, mask
,
6294 size_int (precision
- size
), 0);
6295 newconst
= fold (build (BIT_AND_EXPR
,
6296 TREE_TYPE (varop
), newconst
,
6297 convert (TREE_TYPE (varop
),
6301 t
= build (code
, type
,
6302 (constopnum
== 0) ? newconst
: varop
,
6303 (constopnum
== 1) ? newconst
: varop
);
6309 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6310 if (TREE_CODE (arg1
) == INTEGER_CST
6311 && TREE_CODE (arg0
) != INTEGER_CST
6312 && tree_int_cst_sgn (arg1
) > 0)
6314 switch (TREE_CODE (t
))
6318 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
, 0);
6319 t
= build (code
, type
, TREE_OPERAND (t
, 0), arg1
);
6324 arg1
= const_binop (MINUS_EXPR
, arg1
, integer_one_node
, 0);
6325 t
= build (code
, type
, TREE_OPERAND (t
, 0), arg1
);
6333 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6334 a MINUS_EXPR of a constant, we can convert it into a comparison with
6335 a revised constant as long as no overflow occurs. */
6336 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
6337 && TREE_CODE (arg1
) == INTEGER_CST
6338 && (TREE_CODE (arg0
) == PLUS_EXPR
6339 || TREE_CODE (arg0
) == MINUS_EXPR
)
6340 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
6341 && 0 != (tem
= const_binop (TREE_CODE (arg0
) == PLUS_EXPR
6342 ? MINUS_EXPR
: PLUS_EXPR
,
6343 arg1
, TREE_OPERAND (arg0
, 1), 0))
6344 && ! TREE_CONSTANT_OVERFLOW (tem
))
6345 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
6347 /* Similarly for a NEGATE_EXPR. */
6348 else if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
6349 && TREE_CODE (arg0
) == NEGATE_EXPR
6350 && TREE_CODE (arg1
) == INTEGER_CST
6351 && 0 != (tem
= negate_expr (arg1
))
6352 && TREE_CODE (tem
) == INTEGER_CST
6353 && ! TREE_CONSTANT_OVERFLOW (tem
))
6354 return fold (build (code
, type
, TREE_OPERAND (arg0
, 0), tem
));
6356 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6357 for !=. Don't do this for ordered comparisons due to overflow. */
6358 else if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6359 && integer_zerop (arg1
) && TREE_CODE (arg0
) == MINUS_EXPR
)
6360 return fold (build (code
, type
,
6361 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1)));
6363 /* If we are widening one operand of an integer comparison,
6364 see if the other operand is similarly being widened. Perhaps we
6365 can do the comparison in the narrower type. */
6366 else if (TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
6367 && TREE_CODE (arg0
) == NOP_EXPR
6368 && (tem
= get_unwidened (arg0
, NULL_TREE
)) != arg0
6369 && (t1
= get_unwidened (arg1
, TREE_TYPE (tem
))) != 0
6370 && (TREE_TYPE (t1
) == TREE_TYPE (tem
)
6371 || (TREE_CODE (t1
) == INTEGER_CST
6372 && int_fits_type_p (t1
, TREE_TYPE (tem
)))))
6373 return fold (build (code
, type
, tem
, convert (TREE_TYPE (tem
), t1
)));
6375 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6376 constant, we can simplify it. */
6377 else if (TREE_CODE (arg1
) == INTEGER_CST
6378 && (TREE_CODE (arg0
) == MIN_EXPR
6379 || TREE_CODE (arg0
) == MAX_EXPR
)
6380 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
6381 return optimize_minmax_comparison (t
);
6383 /* If we are comparing an ABS_EXPR with a constant, we can
6384 convert all the cases into explicit comparisons, but they may
6385 well not be faster than doing the ABS and one comparison.
6386 But ABS (X) <= C is a range comparison, which becomes a subtraction
6387 and a comparison, and is probably faster. */
6388 else if (code
== LE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
6389 && TREE_CODE (arg0
) == ABS_EXPR
6390 && ! TREE_SIDE_EFFECTS (arg0
)
6391 && (0 != (tem
= negate_expr (arg1
)))
6392 && TREE_CODE (tem
) == INTEGER_CST
6393 && ! TREE_CONSTANT_OVERFLOW (tem
))
6394 return fold (build (TRUTH_ANDIF_EXPR
, type
,
6395 build (GE_EXPR
, type
, TREE_OPERAND (arg0
, 0), tem
),
6396 build (LE_EXPR
, type
,
6397 TREE_OPERAND (arg0
, 0), arg1
)));
6399 /* If this is an EQ or NE comparison with zero and ARG0 is
6400 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6401 two operations, but the latter can be done in one less insn
6402 on machines that have only two-operand insns or on which a
6403 constant cannot be the first operand. */
6404 if (integer_zerop (arg1
) && (code
== EQ_EXPR
|| code
== NE_EXPR
)
6405 && TREE_CODE (arg0
) == BIT_AND_EXPR
)
6407 if (TREE_CODE (TREE_OPERAND (arg0
, 0)) == LSHIFT_EXPR
6408 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0)))
6410 fold (build (code
, type
,
6411 build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
6413 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
6414 TREE_OPERAND (arg0
, 1),
6415 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1)),
6416 convert (TREE_TYPE (arg0
),
6419 else if (TREE_CODE (TREE_OPERAND (arg0
, 1)) == LSHIFT_EXPR
6420 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0
, 1), 0)))
6422 fold (build (code
, type
,
6423 build (BIT_AND_EXPR
, TREE_TYPE (arg0
),
6425 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
6426 TREE_OPERAND (arg0
, 0),
6427 TREE_OPERAND (TREE_OPERAND (arg0
, 1), 1)),
6428 convert (TREE_TYPE (arg0
),
6433 /* If this is an NE or EQ comparison of zero against the result of a
6434 signed MOD operation whose second operand is a power of 2, make
6435 the MOD operation unsigned since it is simpler and equivalent. */
6436 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6437 && integer_zerop (arg1
)
6438 && ! TREE_UNSIGNED (TREE_TYPE (arg0
))
6439 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
6440 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
6441 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
6442 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
6443 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6445 tree newtype
= unsigned_type (TREE_TYPE (arg0
));
6446 tree newmod
= build (TREE_CODE (arg0
), newtype
,
6447 convert (newtype
, TREE_OPERAND (arg0
, 0)),
6448 convert (newtype
, TREE_OPERAND (arg0
, 1)));
6450 return build (code
, type
, newmod
, convert (newtype
, arg1
));
6453 /* If this is an NE comparison of zero with an AND of one, remove the
6454 comparison since the AND will give the correct value. */
6455 if (code
== NE_EXPR
&& integer_zerop (arg1
)
6456 && TREE_CODE (arg0
) == BIT_AND_EXPR
6457 && integer_onep (TREE_OPERAND (arg0
, 1)))
6458 return convert (type
, arg0
);
6460 /* If we have (A & C) == C where C is a power of 2, convert this into
6461 (A & C) != 0. Similarly for NE_EXPR. */
6462 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
6463 && TREE_CODE (arg0
) == BIT_AND_EXPR
6464 && integer_pow2p (TREE_OPERAND (arg0
, 1))
6465 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
6466 return build (code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
6467 arg0
, integer_zero_node
);
6469 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6470 and similarly for >= into !=. */
6471 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
6472 && TREE_UNSIGNED (TREE_TYPE (arg0
))
6473 && TREE_CODE (arg1
) == LSHIFT_EXPR
6474 && integer_onep (TREE_OPERAND (arg1
, 0)))
6475 return build (code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
6476 build (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
6477 TREE_OPERAND (arg1
, 1)),
6478 convert (TREE_TYPE (arg0
), integer_zero_node
));
6480 else if ((code
== LT_EXPR
|| code
== GE_EXPR
)
6481 && TREE_UNSIGNED (TREE_TYPE (arg0
))
6482 && (TREE_CODE (arg1
) == NOP_EXPR
6483 || TREE_CODE (arg1
) == CONVERT_EXPR
)
6484 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
6485 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
6487 build (code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
6488 convert (TREE_TYPE (arg0
),
6489 build (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
6490 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1))),
6491 convert (TREE_TYPE (arg0
), integer_zero_node
));
6493 /* Simplify comparison of something with itself. (For IEEE
6494 floating-point, we can only do some of these simplifications.) */
6495 if (operand_equal_p (arg0
, arg1
, 0))
6502 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
6503 return constant_boolean_node (1, type
);
6505 TREE_SET_CODE (t
, code
);
6509 /* For NE, we can only do this simplification if integer. */
6510 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
6512 /* ... fall through ... */
6515 return constant_boolean_node (0, type
);
6521 /* An unsigned comparison against 0 can be simplified. */
6522 if (integer_zerop (arg1
)
6523 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
6524 || POINTER_TYPE_P (TREE_TYPE (arg1
)))
6525 && TREE_UNSIGNED (TREE_TYPE (arg1
)))
6527 switch (TREE_CODE (t
))
6531 TREE_SET_CODE (t
, NE_EXPR
);
6535 TREE_SET_CODE (t
, EQ_EXPR
);
6538 return omit_one_operand (type
,
6539 convert (type
, integer_one_node
),
6542 return omit_one_operand (type
,
6543 convert (type
, integer_zero_node
),
6550 /* Comparisons with the highest or lowest possible integer of
6551 the specified size will have known values and an unsigned
6552 <= 0x7fffffff can be simplified. */
6554 int width
= GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1
)));
6556 if (TREE_CODE (arg1
) == INTEGER_CST
6557 && ! TREE_CONSTANT_OVERFLOW (arg1
)
6558 && width
<= HOST_BITS_PER_WIDE_INT
6559 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
6560 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
6562 if (TREE_INT_CST_HIGH (arg1
) == 0
6563 && (TREE_INT_CST_LOW (arg1
)
6564 == ((unsigned HOST_WIDE_INT
) 1 << (width
- 1)) - 1)
6565 && ! TREE_UNSIGNED (TREE_TYPE (arg1
)))
6566 switch (TREE_CODE (t
))
6569 return omit_one_operand (type
,
6570 convert (type
, integer_zero_node
),
6573 TREE_SET_CODE (t
, EQ_EXPR
);
6577 return omit_one_operand (type
,
6578 convert (type
, integer_one_node
),
6581 TREE_SET_CODE (t
, NE_EXPR
);
6588 else if (TREE_INT_CST_HIGH (arg1
) == -1
6589 && (- TREE_INT_CST_LOW (arg1
)
6590 == ((unsigned HOST_WIDE_INT
) 1 << (width
- 1)))
6591 && ! TREE_UNSIGNED (TREE_TYPE (arg1
)))
6592 switch (TREE_CODE (t
))
6595 return omit_one_operand (type
,
6596 convert (type
, integer_zero_node
),
6599 TREE_SET_CODE (t
, EQ_EXPR
);
6603 return omit_one_operand (type
,
6604 convert (type
, integer_one_node
),
6607 TREE_SET_CODE (t
, NE_EXPR
);
6614 else if (TREE_INT_CST_HIGH (arg1
) == 0
6615 && (TREE_INT_CST_LOW (arg1
)
6616 == ((unsigned HOST_WIDE_INT
) 1 << (width
- 1)) - 1)
6617 && TREE_UNSIGNED (TREE_TYPE (arg1
))
6618 /* signed_type does not work on pointer types. */
6619 && INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
6621 switch (TREE_CODE (t
))
6624 return fold (build (GE_EXPR
, type
,
6625 convert (signed_type (TREE_TYPE (arg0
)),
6627 convert (signed_type (TREE_TYPE (arg1
)),
6628 integer_zero_node
)));
6630 return fold (build (LT_EXPR
, type
,
6631 convert (signed_type (TREE_TYPE (arg0
)),
6633 convert (signed_type (TREE_TYPE (arg1
)),
6634 integer_zero_node
)));
6642 /* If we are comparing an expression that just has comparisons
6643 of two integer values, arithmetic expressions of those comparisons,
6644 and constants, we can simplify it. There are only three cases
6645 to check: the two values can either be equal, the first can be
6646 greater, or the second can be greater. Fold the expression for
6647 those three values. Since each value must be 0 or 1, we have
6648 eight possibilities, each of which corresponds to the constant 0
6649 or 1 or one of the six possible comparisons.
6651 This handles common cases like (a > b) == 0 but also handles
6652 expressions like ((x > y) - (y > x)) > 0, which supposedly
6653 occur in macroized code. */
6655 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
6657 tree cval1
= 0, cval2
= 0;
6660 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
6661 /* Don't handle degenerate cases here; they should already
6662 have been handled anyway. */
6663 && cval1
!= 0 && cval2
!= 0
6664 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
6665 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
6666 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
6667 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
6668 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
6669 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
6670 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
6672 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
6673 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
6675 /* We can't just pass T to eval_subst in case cval1 or cval2
6676 was the same as ARG1. */
6679 = fold (build (code
, type
,
6680 eval_subst (arg0
, cval1
, maxval
, cval2
, minval
),
6683 = fold (build (code
, type
,
6684 eval_subst (arg0
, cval1
, maxval
, cval2
, maxval
),
6687 = fold (build (code
, type
,
6688 eval_subst (arg0
, cval1
, minval
, cval2
, maxval
),
6691 /* All three of these results should be 0 or 1. Confirm they
6692 are. Then use those values to select the proper code
6695 if ((integer_zerop (high_result
)
6696 || integer_onep (high_result
))
6697 && (integer_zerop (equal_result
)
6698 || integer_onep (equal_result
))
6699 && (integer_zerop (low_result
)
6700 || integer_onep (low_result
)))
6702 /* Make a 3-bit mask with the high-order bit being the
6703 value for `>', the next for '=', and the low for '<'. */
6704 switch ((integer_onep (high_result
) * 4)
6705 + (integer_onep (equal_result
) * 2)
6706 + integer_onep (low_result
))
6710 return omit_one_operand (type
, integer_zero_node
, arg0
);
6731 return omit_one_operand (type
, integer_one_node
, arg0
);
6734 t
= build (code
, type
, cval1
, cval2
);
6736 return save_expr (t
);
6743 /* If this is a comparison of a field, we may be able to simplify it. */
6744 if ((TREE_CODE (arg0
) == COMPONENT_REF
6745 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
6746 && (code
== EQ_EXPR
|| code
== NE_EXPR
)
6747 /* Handle the constant case even without -O
6748 to make sure the warnings are given. */
6749 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
6751 t1
= optimize_bit_field_compare (code
, type
, arg0
, arg1
);
6755 /* If this is a comparison of complex values and either or both sides
6756 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6757 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6758 This may prevent needless evaluations. */
6759 if ((code
== EQ_EXPR
|| code
== NE_EXPR
)
6760 && TREE_CODE (TREE_TYPE (arg0
)) == COMPLEX_TYPE
6761 && (TREE_CODE (arg0
) == COMPLEX_EXPR
6762 || TREE_CODE (arg1
) == COMPLEX_EXPR
6763 || TREE_CODE (arg0
) == COMPLEX_CST
6764 || TREE_CODE (arg1
) == COMPLEX_CST
))
6766 tree subtype
= TREE_TYPE (TREE_TYPE (arg0
));
6767 tree real0
, imag0
, real1
, imag1
;
6769 arg0
= save_expr (arg0
);
6770 arg1
= save_expr (arg1
);
6771 real0
= fold (build1 (REALPART_EXPR
, subtype
, arg0
));
6772 imag0
= fold (build1 (IMAGPART_EXPR
, subtype
, arg0
));
6773 real1
= fold (build1 (REALPART_EXPR
, subtype
, arg1
));
6774 imag1
= fold (build1 (IMAGPART_EXPR
, subtype
, arg1
));
6776 return fold (build ((code
== EQ_EXPR
? TRUTH_ANDIF_EXPR
6779 fold (build (code
, type
, real0
, real1
)),
6780 fold (build (code
, type
, imag0
, imag1
))));
6783 /* From here on, the only cases we handle are when the result is
6784 known to be a constant.
6786 To compute GT, swap the arguments and do LT.
6787 To compute GE, do LT and invert the result.
6788 To compute LE, swap the arguments, do LT and invert the result.
6789 To compute NE, do EQ and invert the result.
6791 Therefore, the code below must handle only EQ and LT. */
6793 if (code
== LE_EXPR
|| code
== GT_EXPR
)
6795 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6796 code
= swap_tree_comparison (code
);
6799 /* Note that it is safe to invert for real values here because we
6800 will check below in the one case that it matters. */
6804 if (code
== NE_EXPR
|| code
== GE_EXPR
)
6807 code
= invert_tree_comparison (code
);
6810 /* Compute a result for LT or EQ if args permit;
6811 otherwise return T. */
6812 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
6814 if (code
== EQ_EXPR
)
6815 t1
= build_int_2 (tree_int_cst_equal (arg0
, arg1
), 0);
6817 t1
= build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0
))
6818 ? INT_CST_LT_UNSIGNED (arg0
, arg1
)
6819 : INT_CST_LT (arg0
, arg1
)),
6823 #if 0 /* This is no longer useful, but breaks some real code. */
6824 /* Assume a nonexplicit constant cannot equal an explicit one,
6825 since such code would be undefined anyway.
6826 Exception: on sysvr4, using #pragma weak,
6827 a label can come out as 0. */
6828 else if (TREE_CODE (arg1
) == INTEGER_CST
6829 && !integer_zerop (arg1
)
6830 && TREE_CONSTANT (arg0
)
6831 && TREE_CODE (arg0
) == ADDR_EXPR
6833 t1
= build_int_2 (0, 0);
6835 /* Two real constants can be compared explicitly. */
6836 else if (TREE_CODE (arg0
) == REAL_CST
&& TREE_CODE (arg1
) == REAL_CST
)
6838 /* If either operand is a NaN, the result is false with two
6839 exceptions: First, an NE_EXPR is true on NaNs, but that case
6840 is already handled correctly since we will be inverting the
6841 result for NE_EXPR. Second, if we had inverted a LE_EXPR
6842 or a GE_EXPR into a LT_EXPR, we must return true so that it
6843 will be inverted into false. */
6845 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0
))
6846 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
)))
6847 t1
= build_int_2 (invert
&& code
== LT_EXPR
, 0);
6849 else if (code
== EQ_EXPR
)
6850 t1
= build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
),
6851 TREE_REAL_CST (arg1
)),
6854 t1
= build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0
),
6855 TREE_REAL_CST (arg1
)),
6859 if (t1
== NULL_TREE
)
6863 TREE_INT_CST_LOW (t1
) ^= 1;
6865 TREE_TYPE (t1
) = type
;
6866 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
6867 return truthvalue_conversion (t1
);
6871 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
6872 so all simple results must be passed through pedantic_non_lvalue. */
6873 if (TREE_CODE (arg0
) == INTEGER_CST
)
6874 return pedantic_non_lvalue
6875 (TREE_OPERAND (t
, (integer_zerop (arg0
) ? 2 : 1)));
6876 else if (operand_equal_p (arg1
, TREE_OPERAND (expr
, 2), 0))
6877 return pedantic_omit_one_operand (type
, arg1
, arg0
);
6879 /* If the second operand is zero, invert the comparison and swap
6880 the second and third operands. Likewise if the second operand
6881 is constant and the third is not or if the third operand is
6882 equivalent to the first operand of the comparison. */
6884 if (integer_zerop (arg1
)
6885 || (TREE_CONSTANT (arg1
) && ! TREE_CONSTANT (TREE_OPERAND (t
, 2)))
6886 || (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
6887 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
6888 TREE_OPERAND (t
, 2),
6889 TREE_OPERAND (arg0
, 1))))
6891 /* See if this can be inverted. If it can't, possibly because
6892 it was a floating-point inequality comparison, don't do
6894 tem
= invert_truthvalue (arg0
);
6896 if (TREE_CODE (tem
) != TRUTH_NOT_EXPR
)
6898 t
= build (code
, type
, tem
,
6899 TREE_OPERAND (t
, 2), TREE_OPERAND (t
, 1));
6901 /* arg1 should be the first argument of the new T. */
6902 arg1
= TREE_OPERAND (t
, 1);
6907 /* If we have A op B ? A : C, we may be able to convert this to a
6908 simpler expression, depending on the operation and the values
6909 of B and C. IEEE floating point prevents this though,
6910 because A or B might be -0.0 or a NaN. */
6912 if (TREE_CODE_CLASS (TREE_CODE (arg0
)) == '<'
6913 && (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
6914 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 0)))
6915 || flag_unsafe_math_optimizations
)
6916 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
6917 arg1
, TREE_OPERAND (arg0
, 1)))
6919 tree arg2
= TREE_OPERAND (t
, 2);
6920 enum tree_code comp_code
= TREE_CODE (arg0
);
6924 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A),
6925 depending on the comparison operation. */
6926 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0
, 1)))
6927 ? real_zerop (TREE_OPERAND (arg0
, 1))
6928 : integer_zerop (TREE_OPERAND (arg0
, 1)))
6929 && TREE_CODE (arg2
) == NEGATE_EXPR
6930 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
6938 (convert (TREE_TYPE (TREE_OPERAND (t
, 1)),
6942 return pedantic_non_lvalue (convert (type
, arg1
));
6945 if (TREE_UNSIGNED (TREE_TYPE (arg1
)))
6946 arg1
= convert (signed_type (TREE_TYPE (arg1
)), arg1
);
6947 return pedantic_non_lvalue
6948 (convert (type
, fold (build1 (ABS_EXPR
,
6949 TREE_TYPE (arg1
), arg1
))));
6952 if (TREE_UNSIGNED (TREE_TYPE (arg1
)))
6953 arg1
= convert (signed_type (TREE_TYPE (arg1
)), arg1
);
6954 return pedantic_non_lvalue
6955 (negate_expr (convert (type
,
6956 fold (build1 (ABS_EXPR
,
6963 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
6966 if (integer_zerop (TREE_OPERAND (arg0
, 1)) && integer_zerop (arg2
))
6968 if (comp_code
== NE_EXPR
)
6969 return pedantic_non_lvalue (convert (type
, arg1
));
6970 else if (comp_code
== EQ_EXPR
)
6971 return pedantic_non_lvalue (convert (type
, integer_zero_node
));
6974 /* If this is A op B ? A : B, this is either A, B, min (A, B),
6975 or max (A, B), depending on the operation. */
6977 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 1),
6978 arg2
, TREE_OPERAND (arg0
, 0)))
6980 tree comp_op0
= TREE_OPERAND (arg0
, 0);
6981 tree comp_op1
= TREE_OPERAND (arg0
, 1);
6982 tree comp_type
= TREE_TYPE (comp_op0
);
6984 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
6985 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
6991 return pedantic_non_lvalue (convert (type
, arg2
));
6993 return pedantic_non_lvalue (convert (type
, arg1
));
6996 /* In C++ a ?: expression can be an lvalue, so put the
6997 operand which will be used if they are equal first
6998 so that we can convert this back to the
6999 corresponding COND_EXPR. */
7000 return pedantic_non_lvalue
7001 (convert (type
, fold (build (MIN_EXPR
, comp_type
,
7002 (comp_code
== LE_EXPR
7003 ? comp_op0
: comp_op1
),
7004 (comp_code
== LE_EXPR
7005 ? comp_op1
: comp_op0
)))));
7009 return pedantic_non_lvalue
7010 (convert (type
, fold (build (MAX_EXPR
, comp_type
,
7011 (comp_code
== GE_EXPR
7012 ? comp_op0
: comp_op1
),
7013 (comp_code
== GE_EXPR
7014 ? comp_op1
: comp_op0
)))));
7021 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7022 we might still be able to simplify this. For example,
7023 if C1 is one less or one more than C2, this might have started
7024 out as a MIN or MAX and been transformed by this function.
7025 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7027 if (INTEGRAL_TYPE_P (type
)
7028 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
7029 && TREE_CODE (arg2
) == INTEGER_CST
)
7033 /* We can replace A with C1 in this case. */
7034 arg1
= convert (type
, TREE_OPERAND (arg0
, 1));
7035 t
= build (code
, type
, TREE_OPERAND (t
, 0), arg1
,
7036 TREE_OPERAND (t
, 2));
7040 /* If C1 is C2 + 1, this is min(A, C2). */
7041 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
), 1)
7042 && operand_equal_p (TREE_OPERAND (arg0
, 1),
7043 const_binop (PLUS_EXPR
, arg2
,
7044 integer_one_node
, 0), 1))
7045 return pedantic_non_lvalue
7046 (fold (build (MIN_EXPR
, type
, arg1
, arg2
)));
7050 /* If C1 is C2 - 1, this is min(A, C2). */
7051 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
), 1)
7052 && operand_equal_p (TREE_OPERAND (arg0
, 1),
7053 const_binop (MINUS_EXPR
, arg2
,
7054 integer_one_node
, 0), 1))
7055 return pedantic_non_lvalue
7056 (fold (build (MIN_EXPR
, type
, arg1
, arg2
)));
7060 /* If C1 is C2 - 1, this is max(A, C2). */
7061 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
), 1)
7062 && operand_equal_p (TREE_OPERAND (arg0
, 1),
7063 const_binop (MINUS_EXPR
, arg2
,
7064 integer_one_node
, 0), 1))
7065 return pedantic_non_lvalue
7066 (fold (build (MAX_EXPR
, type
, arg1
, arg2
)));
7070 /* If C1 is C2 + 1, this is max(A, C2). */
7071 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
), 1)
7072 && operand_equal_p (TREE_OPERAND (arg0
, 1),
7073 const_binop (PLUS_EXPR
, arg2
,
7074 integer_one_node
, 0), 1))
7075 return pedantic_non_lvalue
7076 (fold (build (MAX_EXPR
, type
, arg1
, arg2
)));
7085 /* If the second operand is simpler than the third, swap them
7086 since that produces better jump optimization results. */
7087 if ((TREE_CONSTANT (arg1
) || DECL_P (arg1
)
7088 || TREE_CODE (arg1
) == SAVE_EXPR
)
7089 && ! (TREE_CONSTANT (TREE_OPERAND (t
, 2))
7090 || DECL_P (TREE_OPERAND (t
, 2))
7091 || TREE_CODE (TREE_OPERAND (t
, 2)) == SAVE_EXPR
))
7093 /* See if this can be inverted. If it can't, possibly because
7094 it was a floating-point inequality comparison, don't do
7096 tem
= invert_truthvalue (arg0
);
7098 if (TREE_CODE (tem
) != TRUTH_NOT_EXPR
)
7100 t
= build (code
, type
, tem
,
7101 TREE_OPERAND (t
, 2), TREE_OPERAND (t
, 1));
7103 /* arg1 should be the first argument of the new T. */
7104 arg1
= TREE_OPERAND (t
, 1);
7109 /* Convert A ? 1 : 0 to simply A. */
7110 if (integer_onep (TREE_OPERAND (t
, 1))
7111 && integer_zerop (TREE_OPERAND (t
, 2))
7112 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7113 call to fold will try to move the conversion inside
7114 a COND, which will recurse. In that case, the COND_EXPR
7115 is probably the best choice, so leave it alone. */
7116 && type
== TREE_TYPE (arg0
))
7117 return pedantic_non_lvalue (arg0
);
7119 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7120 operation is simply A & 2. */
7122 if (integer_zerop (TREE_OPERAND (t
, 2))
7123 && TREE_CODE (arg0
) == NE_EXPR
7124 && integer_zerop (TREE_OPERAND (arg0
, 1))
7125 && integer_pow2p (arg1
)
7126 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
7127 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
7129 return pedantic_non_lvalue (convert (type
, TREE_OPERAND (arg0
, 0)));
7134 /* When pedantic, a compound expression can be neither an lvalue
7135 nor an integer constant expression. */
7136 if (TREE_SIDE_EFFECTS (arg0
) || pedantic
)
7138 /* Don't let (0, 0) be null pointer constant. */
7139 if (integer_zerop (arg1
))
7140 return build1 (NOP_EXPR
, type
, arg1
);
7141 return convert (type
, arg1
);
7145 return build_complex (type
, arg0
, arg1
);
7149 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
7151 else if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
7152 return omit_one_operand (type
, TREE_OPERAND (arg0
, 0),
7153 TREE_OPERAND (arg0
, 1));
7154 else if (TREE_CODE (arg0
) == COMPLEX_CST
)
7155 return TREE_REALPART (arg0
);
7156 else if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
7157 return fold (build (TREE_CODE (arg0
), type
,
7158 fold (build1 (REALPART_EXPR
, type
,
7159 TREE_OPERAND (arg0
, 0))),
7160 fold (build1 (REALPART_EXPR
,
7161 type
, TREE_OPERAND (arg0
, 1)))));
7165 if (TREE_CODE (TREE_TYPE (arg0
)) != COMPLEX_TYPE
)
7166 return convert (type
, integer_zero_node
);
7167 else if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
7168 return omit_one_operand (type
, TREE_OPERAND (arg0
, 1),
7169 TREE_OPERAND (arg0
, 0));
7170 else if (TREE_CODE (arg0
) == COMPLEX_CST
)
7171 return TREE_IMAGPART (arg0
);
7172 else if (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
7173 return fold (build (TREE_CODE (arg0
), type
,
7174 fold (build1 (IMAGPART_EXPR
, type
,
7175 TREE_OPERAND (arg0
, 0))),
7176 fold (build1 (IMAGPART_EXPR
, type
,
7177 TREE_OPERAND (arg0
, 1)))));
7180 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7182 case CLEANUP_POINT_EXPR
:
7183 if (! has_cleanups (arg0
))
7184 return TREE_OPERAND (t
, 0);
7187 enum tree_code code0
= TREE_CODE (arg0
);
7188 int kind0
= TREE_CODE_CLASS (code0
);
7189 tree arg00
= TREE_OPERAND (arg0
, 0);
7192 if (kind0
== '1' || code0
== TRUTH_NOT_EXPR
)
7193 return fold (build1 (code0
, type
,
7194 fold (build1 (CLEANUP_POINT_EXPR
,
7195 TREE_TYPE (arg00
), arg00
))));
7197 if (kind0
== '<' || kind0
== '2'
7198 || code0
== TRUTH_ANDIF_EXPR
|| code0
== TRUTH_ORIF_EXPR
7199 || code0
== TRUTH_AND_EXPR
|| code0
== TRUTH_OR_EXPR
7200 || code0
== TRUTH_XOR_EXPR
)
7202 arg01
= TREE_OPERAND (arg0
, 1);
7204 if (TREE_CONSTANT (arg00
)
7205 || ((code0
== TRUTH_ANDIF_EXPR
|| code0
== TRUTH_ORIF_EXPR
)
7206 && ! has_cleanups (arg00
)))
7207 return fold (build (code0
, type
, arg00
,
7208 fold (build1 (CLEANUP_POINT_EXPR
,
7209 TREE_TYPE (arg01
), arg01
))));
7211 if (TREE_CONSTANT (arg01
))
7212 return fold (build (code0
, type
,
7213 fold (build1 (CLEANUP_POINT_EXPR
,
7214 TREE_TYPE (arg00
), arg00
)),
7222 /* Check for a built-in function. */
7223 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
7224 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0))
7226 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0)))
7228 tree tmp
= fold_builtin (expr
);
7236 } /* switch (code) */
7239 /* Determine if first argument is a multiple of second argument. Return 0 if
7240 it is not, or we cannot easily determined it to be.
7242 An example of the sort of thing we care about (at this point; this routine
7243 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7244 fold cases do now) is discovering that
7246 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7252 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7254 This code also handles discovering that
7256 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7258 is a multiple of 8 so we don't have to worry about dealing with a
7261 Note that we *look* inside a SAVE_EXPR only to determine how it was
7262 calculated; it is not safe for fold to do much of anything else with the
7263 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7264 at run time. For example, the latter example above *cannot* be implemented
7265 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7266 evaluation time of the original SAVE_EXPR is not necessarily the same at
7267 the time the new expression is evaluated. The only optimization of this
7268 sort that would be valid is changing
7270 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7274 SAVE_EXPR (I) * SAVE_EXPR (J)
7276 (where the same SAVE_EXPR (J) is used in the original and the
7277 transformed version). */
7280 multiple_of_p (type
, top
, bottom
)
7285 if (operand_equal_p (top
, bottom
, 0))
7288 if (TREE_CODE (type
) != INTEGER_TYPE
)
7291 switch (TREE_CODE (top
))
7294 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
7295 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
7299 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
7300 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
7303 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
7307 op1
= TREE_OPERAND (top
, 1);
7308 /* const_binop may not detect overflow correctly,
7309 so check for it explicitly here. */
7310 if (TYPE_PRECISION (TREE_TYPE (size_one_node
))
7311 > TREE_INT_CST_LOW (op1
)
7312 && TREE_INT_CST_HIGH (op1
) == 0
7313 && 0 != (t1
= convert (type
,
7314 const_binop (LSHIFT_EXPR
, size_one_node
,
7316 && ! TREE_OVERFLOW (t1
))
7317 return multiple_of_p (type
, t1
, bottom
);
7322 /* Can't handle conversions from non-integral or wider integral type. */
7323 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
7324 || (TYPE_PRECISION (type
)
7325 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
7328 /* .. fall through ... */
7331 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
7334 if (TREE_CODE (bottom
) != INTEGER_CST
7335 || (TREE_UNSIGNED (type
)
7336 && (tree_int_cst_sgn (top
) < 0
7337 || tree_int_cst_sgn (bottom
) < 0)))
7339 return integer_zerop (const_binop (TRUNC_MOD_EXPR
,
7347 /* Return true if `t' is known to be non-negative. */
7350 tree_expr_nonnegative_p (t
)
7353 switch (TREE_CODE (t
))
7359 return tree_int_cst_sgn (t
) >= 0;
7361 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1))
7362 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 2));
7364 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
7366 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
7367 && tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
7369 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 0))
7370 || tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
7372 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
7374 return tree_expr_nonnegative_p (TREE_OPERAND (t
, 1));
7376 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t
));
7379 if (truth_value_p (TREE_CODE (t
)))
7380 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7383 /* We don't know sign of `t', so be conservative and return false. */
7388 /* Return true if `r' is known to be non-negative.
7389 Only handles constants at the moment. */
7392 rtl_expr_nonnegative_p (r
)
7395 switch (GET_CODE (r
))
7398 return INTVAL (r
) >= 0;
7401 if (GET_MODE (r
) == VOIDmode
)
7402 return CONST_DOUBLE_HIGH (r
) >= 0;
7407 /* These are always nonnegative. */