X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=gcc%2Ffold-const.c;h=567168276e19762e41ff781985da1873cb3ae6be;hb=a39ac8645754aaee468aa8add01b601723955ca0;hp=695dc5d35122ca504e07c62d9f3e4bc4d0235f51;hpb=c348f27f02731704ee3e33e5d541060f865a75b7;p=pf3gnuchains%2Fgcc-fork.git diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 695dc5d3512..567168276e1 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -134,6 +134,9 @@ static bool reorder_operands_p (tree, tree); static tree fold_negate_const (tree, tree); static tree fold_not_const (tree, tree); static tree fold_relational_const (enum tree_code, tree, tree, tree); +static int native_encode_expr (tree, unsigned char *, int); +static tree native_interpret_expr (tree, unsigned char *, int); + /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring overflow. Suppose A, B and SUM have the same respective signs as A1, B1, @@ -4155,7 +4158,8 @@ range_predecessor (tree val) { tree type = TREE_TYPE (val); - if (INTEGRAL_TYPE_P (type) && val == TYPE_MIN_VALUE (type)) + if (INTEGRAL_TYPE_P (type) + && operand_equal_p (val, TYPE_MIN_VALUE (type), 0)) return 0; else return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0); @@ -4168,7 +4172,8 @@ range_successor (tree val) { tree type = TREE_TYPE (val); - if (INTEGRAL_TYPE_P (type) && val == TYPE_MAX_VALUE (type)) + if (INTEGRAL_TYPE_P (type) + && operand_equal_p (val, TYPE_MAX_VALUE (type), 0)) return 0; else return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0); @@ -6756,6 +6761,398 @@ fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1) return NULL_TREE; } +/* Subroutine of native_encode_expr. Encode the INTEGER_CST + specified by EXPR into the buffer PTR of length LEN bytes. + Return the number of bytes placed in the buffer, or zero + upon failure. */ + +static int +native_encode_int (tree expr, unsigned char *ptr, int len) +{ + tree type = TREE_TYPE (expr); + int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); + int byte, offset, word, words; + unsigned char value; + + if (total_bytes > len) + return 0; + words = total_bytes / UNITS_PER_WORD; + + for (byte = 0; byte < total_bytes; byte++) + { + int bitpos = byte * BITS_PER_UNIT; + if (bitpos < HOST_BITS_PER_WIDE_INT) + value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos); + else + value = (unsigned char) (TREE_INT_CST_HIGH (expr) + >> (bitpos - HOST_BITS_PER_WIDE_INT)); + + if (total_bytes > UNITS_PER_WORD) + { + word = byte / UNITS_PER_WORD; + if (WORDS_BIG_ENDIAN) + word = (words - 1) - word; + offset = word * UNITS_PER_WORD; + if (BYTES_BIG_ENDIAN) + offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); + else + offset += byte % UNITS_PER_WORD; + } + else + offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; + ptr[offset] = value; + } + return total_bytes; +} + + +/* Subroutine of native_encode_expr. Encode the REAL_CST + specified by EXPR into the buffer PTR of length LEN bytes. + Return the number of bytes placed in the buffer, or zero + upon failure. */ + +static int +native_encode_real (tree expr, unsigned char *ptr, int len) +{ + tree type = TREE_TYPE (expr); + int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); + int byte, offset, word, words; + unsigned char value; + + /* There are always 32 bits in each long, no matter the size of + the hosts long. We handle floating point representations with + up to 192 bits. */ + long tmp[6]; + + if (total_bytes > len) + return 0; + words = total_bytes / UNITS_PER_WORD; + + real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type)); + + for (byte = 0; byte < total_bytes; byte++) + { + int bitpos = byte * BITS_PER_UNIT; + value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31)); + + if (total_bytes > UNITS_PER_WORD) + { + word = byte / UNITS_PER_WORD; + if (FLOAT_WORDS_BIG_ENDIAN) + word = (words - 1) - word; + offset = word * UNITS_PER_WORD; + if (BYTES_BIG_ENDIAN) + offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); + else + offset += byte % UNITS_PER_WORD; + } + else + offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; + ptr[offset] = value; + } + return total_bytes; +} + +/* Subroutine of native_encode_expr. Encode the COMPLEX_CST + specified by EXPR into the buffer PTR of length LEN bytes. + Return the number of bytes placed in the buffer, or zero + upon failure. */ + +static int +native_encode_complex (tree expr, unsigned char *ptr, int len) +{ + int rsize, isize; + tree part; + + part = TREE_REALPART (expr); + rsize = native_encode_expr (part, ptr, len); + if (rsize == 0) + return 0; + part = TREE_IMAGPART (expr); + isize = native_encode_expr (part, ptr+rsize, len-rsize); + if (isize != rsize) + return 0; + return rsize + isize; +} + + +/* Subroutine of native_encode_expr. Encode the VECTOR_CST + specified by EXPR into the buffer PTR of length LEN bytes. + Return the number of bytes placed in the buffer, or zero + upon failure. */ + +static int +native_encode_vector (tree expr, unsigned char *ptr, int len) +{ + int i, size, offset, count; + tree elem, elements; + + size = 0; + offset = 0; + elements = TREE_VECTOR_CST_ELTS (expr); + count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)); + for (i = 0; i < count; i++) + { + if (elements) + { + elem = TREE_VALUE (elements); + elements = TREE_CHAIN (elements); + } + else + elem = NULL_TREE; + + if (elem) + { + size = native_encode_expr (elem, ptr+offset, len-offset); + if (size == 0) + return 0; + } + else if (size != 0) + { + if (offset + size > len) + return 0; + memset (ptr+offset, 0, size); + } + else + return 0; + offset += size; + } + return offset; +} + + +/* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, + REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the + buffer PTR of length LEN bytes. Return the number of bytes + placed in the buffer, or zero upon failure. */ + +static int +native_encode_expr (tree expr, unsigned char *ptr, int len) +{ + switch (TREE_CODE (expr)) + { + case INTEGER_CST: + return native_encode_int (expr, ptr, len); + + case REAL_CST: + return native_encode_real (expr, ptr, len); + + case COMPLEX_CST: + return native_encode_complex (expr, ptr, len); + + case VECTOR_CST: + return native_encode_vector (expr, ptr, len); + + default: + return 0; + } +} + + +/* Subroutine of native_interpret_expr. Interpret the contents of + the buffer PTR of length LEN as an INTEGER_CST of type TYPE. + If the buffer cannot be interpreted, return NULL_TREE. */ + +static tree +native_interpret_int (tree type, unsigned char *ptr, int len) +{ + int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); + int byte, offset, word, words; + unsigned char value; + unsigned int HOST_WIDE_INT lo = 0; + HOST_WIDE_INT hi = 0; + + if (total_bytes > len) + return NULL_TREE; + if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT) + return NULL_TREE; + words = total_bytes / UNITS_PER_WORD; + + for (byte = 0; byte < total_bytes; byte++) + { + int bitpos = byte * BITS_PER_UNIT; + if (total_bytes > UNITS_PER_WORD) + { + word = byte / UNITS_PER_WORD; + if (WORDS_BIG_ENDIAN) + word = (words - 1) - word; + offset = word * UNITS_PER_WORD; + if (BYTES_BIG_ENDIAN) + offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); + else + offset += byte % UNITS_PER_WORD; + } + else + offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; + value = ptr[offset]; + + if (bitpos < HOST_BITS_PER_WIDE_INT) + lo |= (unsigned HOST_WIDE_INT) value << bitpos; + else + hi |= (unsigned HOST_WIDE_INT) value + << (bitpos - HOST_BITS_PER_WIDE_INT); + } + + return force_fit_type (build_int_cst_wide (type, lo, hi), + 0, false, false); +} + + +/* Subroutine of native_interpret_expr. Interpret the contents of + the buffer PTR of length LEN as a REAL_CST of type TYPE. + If the buffer cannot be interpreted, return NULL_TREE. */ + +static tree +native_interpret_real (tree type, unsigned char *ptr, int len) +{ + enum machine_mode mode = TYPE_MODE (type); + int total_bytes = GET_MODE_SIZE (mode); + int byte, offset, word, words; + unsigned char value; + /* There are always 32 bits in each long, no matter the size of + the hosts long. We handle floating point representations with + up to 192 bits. */ + REAL_VALUE_TYPE r; + long tmp[6]; + + total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); + if (total_bytes > len || total_bytes > 24) + return NULL_TREE; + words = total_bytes / UNITS_PER_WORD; + + memset (tmp, 0, sizeof (tmp)); + for (byte = 0; byte < total_bytes; byte++) + { + int bitpos = byte * BITS_PER_UNIT; + if (total_bytes > UNITS_PER_WORD) + { + word = byte / UNITS_PER_WORD; + if (FLOAT_WORDS_BIG_ENDIAN) + word = (words - 1) - word; + offset = word * UNITS_PER_WORD; + if (BYTES_BIG_ENDIAN) + offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); + else + offset += byte % UNITS_PER_WORD; + } + else + offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; + value = ptr[offset]; + + tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31); + } + + real_from_target (&r, tmp, mode); + return build_real (type, r); +} + + +/* Subroutine of native_interpret_expr. Interpret the contents of + the buffer PTR of length LEN as a COMPLEX_CST of type TYPE. + If the buffer cannot be interpreted, return NULL_TREE. */ + +static tree +native_interpret_complex (tree type, unsigned char *ptr, int len) +{ + tree etype, rpart, ipart; + int size; + + etype = TREE_TYPE (type); + size = GET_MODE_SIZE (TYPE_MODE (etype)); + if (size * 2 > len) + return NULL_TREE; + rpart = native_interpret_expr (etype, ptr, size); + if (!rpart) + return NULL_TREE; + ipart = native_interpret_expr (etype, ptr+size, size); + if (!ipart) + return NULL_TREE; + return build_complex (type, rpart, ipart); +} + + +/* Subroutine of native_interpret_expr. Interpret the contents of + the buffer PTR of length LEN as a VECTOR_CST of type TYPE. + If the buffer cannot be interpreted, return NULL_TREE. */ + +static tree +native_interpret_vector (tree type, unsigned char *ptr, int len) +{ + tree etype, elem, elements; + int i, size, count; + + etype = TREE_TYPE (type); + size = GET_MODE_SIZE (TYPE_MODE (etype)); + count = TYPE_VECTOR_SUBPARTS (type); + if (size * count > len) + return NULL_TREE; + + elements = NULL_TREE; + for (i = count - 1; i >= 0; i--) + { + elem = native_interpret_expr (etype, ptr+(i*size), size); + if (!elem) + return NULL_TREE; + elements = tree_cons (NULL_TREE, elem, elements); + } + return build_vector (type, elements); +} + + +/* Subroutine of fold_view_convert_expr. Interpret the contents of + the buffer PTR of length LEN as a constant of type TYPE. For + INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P + we return a REAL_CST, etc... If the buffer cannot be interpreted, + return NULL_TREE. */ + +static tree +native_interpret_expr (tree type, unsigned char *ptr, int len) +{ + switch (TREE_CODE (type)) + { + case INTEGER_TYPE: + case ENUMERAL_TYPE: + case BOOLEAN_TYPE: + return native_interpret_int (type, ptr, len); + + case REAL_TYPE: + return native_interpret_real (type, ptr, len); + + case COMPLEX_TYPE: + return native_interpret_complex (type, ptr, len); + + case VECTOR_TYPE: + return native_interpret_vector (type, ptr, len); + + default: + return NULL_TREE; + } +} + + +/* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type + TYPE at compile-time. If we're unable to perform the conversion + return NULL_TREE. */ + +static tree +fold_view_convert_expr (tree type, tree expr) +{ + /* We support up to 512-bit values (for V8DFmode). */ + unsigned char buffer[64]; + int len; + + /* Check that the host and target are sane. */ + if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) + return NULL_TREE; + + len = native_encode_expr (expr, buffer, sizeof (buffer)); + if (len == 0) + return NULL_TREE; + + return native_interpret_expr (type, buffer, len); +} + + /* Fold a unary expression of code CODE and type TYPE with operand OP0. Return the folded expression if folding is successful. Otherwise, return NULL_TREE. */ @@ -7095,8 +7492,8 @@ fold_unary (enum tree_code code, tree type, tree op0) case VIEW_CONVERT_EXPR: if (TREE_CODE (op0) == VIEW_CONVERT_EXPR) - return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0)); - return NULL_TREE; + return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0)); + return fold_view_convert_expr (type, op0); case NEGATE_EXPR: if (negate_expr_p (arg0)) @@ -7305,33 +7702,6 @@ fold_comparison (enum tree_code code, tree type, tree op0, tree op1) if (tree_swap_operands_p (arg0, arg1, true)) return fold_build2 (swap_tree_comparison (code), type, op1, op0); - /* If this is a comparison of two exprs that look like an - ARRAY_REF of the same object, then we can fold this to a - comparison of the two offsets. */ - { - tree base0, offset0, base1, offset1; - - if (extract_array_ref (arg0, &base0, &offset0) - && extract_array_ref (arg1, &base1, &offset1) - && operand_equal_p (base0, base1, 0)) - { - /* Handle no offsets on both sides specially. */ - if (offset0 == NULL_TREE && offset1 == NULL_TREE) - return fold_build2 (code, type, integer_zero_node, - integer_zero_node); - - if (!offset0 || !offset1 - || TREE_TYPE (offset0) == TREE_TYPE (offset1)) - { - if (offset0 == NULL_TREE) - offset0 = build_int_cst (TREE_TYPE (offset1), 0); - if (offset1 == NULL_TREE) - offset1 = build_int_cst (TREE_TYPE (offset0), 0); - return fold_build2 (code, type, offset0, offset1); - } - } - } - /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */ if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST @@ -8803,6 +9173,45 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) return fold_build2 (EQ_EXPR, type, arg0, build_int_cst (TREE_TYPE (arg0), 0)); + /* Fold (X & Y) ^ Y as ~X & Y. */ + if (TREE_CODE (arg0) == BIT_AND_EXPR + && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) + { + tem = fold_convert (type, TREE_OPERAND (arg0, 0)); + return fold_build2 (BIT_AND_EXPR, type, + fold_build1 (BIT_NOT_EXPR, type, tem), + fold_convert (type, arg1)); + } + /* Fold (X & Y) ^ X as ~Y & X. */ + if (TREE_CODE (arg0) == BIT_AND_EXPR + && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) + && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) + { + tem = fold_convert (type, TREE_OPERAND (arg0, 1)); + return fold_build2 (BIT_AND_EXPR, type, + fold_build1 (BIT_NOT_EXPR, type, tem), + fold_convert (type, arg1)); + } + /* Fold X ^ (X & Y) as X & ~Y. */ + if (TREE_CODE (arg1) == BIT_AND_EXPR + && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) + { + tem = fold_convert (type, TREE_OPERAND (arg1, 1)); + return fold_build2 (BIT_AND_EXPR, type, + fold_convert (type, arg0), + fold_build1 (BIT_NOT_EXPR, type, tem)); + } + /* Fold X ^ (Y & X) as ~Y & X. */ + if (TREE_CODE (arg1) == BIT_AND_EXPR + && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) + && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) + { + tem = fold_convert (type, TREE_OPERAND (arg1, 0)); + return fold_build2 (BIT_AND_EXPR, type, + fold_build1 (BIT_NOT_EXPR, type, tem), + fold_convert (type, arg0)); + } + /* See if this can be simplified into a rotate first. If that is unsuccessful continue in the association code. */ goto bit_rotate; @@ -8877,6 +9286,45 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) build_int_cst (TREE_TYPE (tem), 0)); } + /* Fold (X ^ Y) & Y as ~X & Y. */ + if (TREE_CODE (arg0) == BIT_XOR_EXPR + && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) + { + tem = fold_convert (type, TREE_OPERAND (arg0, 0)); + return fold_build2 (BIT_AND_EXPR, type, + fold_build1 (BIT_NOT_EXPR, type, tem), + fold_convert (type, arg1)); + } + /* Fold (X ^ Y) & X as ~Y & X. */ + if (TREE_CODE (arg0) == BIT_XOR_EXPR + && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) + && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) + { + tem = fold_convert (type, TREE_OPERAND (arg0, 1)); + return fold_build2 (BIT_AND_EXPR, type, + fold_build1 (BIT_NOT_EXPR, type, tem), + fold_convert (type, arg1)); + } + /* Fold X & (X ^ Y) as X & ~Y. */ + if (TREE_CODE (arg1) == BIT_XOR_EXPR + && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) + { + tem = fold_convert (type, TREE_OPERAND (arg1, 1)); + return fold_build2 (BIT_AND_EXPR, type, + fold_convert (type, arg0), + fold_build1 (BIT_NOT_EXPR, type, tem)); + } + /* Fold X & (Y ^ X) as ~Y & X. */ + if (TREE_CODE (arg1) == BIT_XOR_EXPR + && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) + && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) + { + tem = fold_convert (type, TREE_OPERAND (arg1, 0)); + return fold_build2 (BIT_AND_EXPR, type, + fold_build1 (BIT_NOT_EXPR, type, tem), + fold_convert (type, arg0)); + } + t1 = distribute_bit_expr (code, type, arg0, arg1); if (t1 != NULL_TREE) return t1; @@ -9154,8 +9602,27 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) return NULL_TREE; case TRUNC_DIV_EXPR: - case ROUND_DIV_EXPR: case FLOOR_DIV_EXPR: + /* Simplify A / (B << N) where A and B are positive and B is + a power of 2, to A >> (N + log2(B)). */ + if (TREE_CODE (arg1) == LSHIFT_EXPR + && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0))) + { + tree sval = TREE_OPERAND (arg1, 0); + if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) + { + tree sh_cnt = TREE_OPERAND (arg1, 1); + unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval)); + + sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt), + sh_cnt, build_int_cst (NULL_TREE, pow2)); + return fold_build2 (RSHIFT_EXPR, type, + fold_convert (type, arg0), sh_cnt); + } + } + /* Fall thru */ + + case ROUND_DIV_EXPR: case CEIL_DIV_EXPR: case EXACT_DIV_EXPR: if (integer_onep (arg1)) @@ -9225,31 +9692,24 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) return omit_one_operand (type, integer_zero_node, arg0); /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, - i.e. "X % C" into "X & C2", if X and C are positive. */ + i.e. "X % C" into "X & (C - 1)", if X and C are positive. */ if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR) - && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0)) - && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) >= 0) + && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0))) { - unsigned HOST_WIDE_INT high, low; - tree mask; - int l; + tree c = arg1; + /* Also optimize A % (C << N) where C is a power of 2, + to A & ((C << N) - 1). */ + if (TREE_CODE (arg1) == LSHIFT_EXPR) + c = TREE_OPERAND (arg1, 0); - l = tree_log2 (arg1); - if (l >= HOST_BITS_PER_WIDE_INT) + if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0) { - high = ((unsigned HOST_WIDE_INT) 1 - << (l - HOST_BITS_PER_WIDE_INT)) - 1; - low = -1; - } - else - { - high = 0; - low = ((unsigned HOST_WIDE_INT) 1 << l) - 1; + tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), + arg1, integer_one_node); + return fold_build2 (BIT_AND_EXPR, type, + fold_convert (type, arg0), + fold_convert (type, mask)); } - - mask = build_int_cst_wide (type, low, high); - return fold_build2 (BIT_AND_EXPR, type, - fold_convert (type, arg0), mask); } /* X % -C is the same as X % C. */ @@ -9984,6 +10444,34 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) tem, build_int_cst (TREE_TYPE (tem), 0)); } + /* If this is a comparison of two exprs that look like an + ARRAY_REF of the same object, then we can fold this to a + comparison of the two offsets. This is only safe for + EQ_EXPR and NE_EXPR because of overflow issues. */ + { + tree base0, offset0, base1, offset1; + + if (extract_array_ref (arg0, &base0, &offset0) + && extract_array_ref (arg1, &base1, &offset1) + && operand_equal_p (base0, base1, 0)) + { + /* Handle no offsets on both sides specially. */ + if (offset0 == NULL_TREE && offset1 == NULL_TREE) + return fold_build2 (code, type, integer_zero_node, + integer_zero_node); + + if (!offset0 || !offset1 + || TREE_TYPE (offset0) == TREE_TYPE (offset1)) + { + if (offset0 == NULL_TREE) + offset0 = build_int_cst (TREE_TYPE (offset1), 0); + if (offset1 == NULL_TREE) + offset1 = build_int_cst (TREE_TYPE (offset0), 0); + return fold_build2 (code, type, offset0, offset1); + } + } + } + if (integer_zerop (arg1) && tree_expr_nonzero_p (arg0)) { @@ -10599,7 +11087,9 @@ fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2) if (integer_zerop (op2) && truth_value_p (TREE_CODE (arg0)) && truth_value_p (TREE_CODE (arg1))) - return fold_build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1); + return fold_build2 (TRUTH_ANDIF_EXPR, type, + fold_convert (type, arg0), + arg1); /* Convert A ? B : 1 into !A || B if A and B are truth values. */ if (integer_onep (op2) @@ -10609,7 +11099,9 @@ fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2) /* Only perform transformation if ARG0 is easily inverted. */ tem = invert_truthvalue (arg0); if (TREE_CODE (tem) != TRUTH_NOT_EXPR) - return fold_build2 (TRUTH_ORIF_EXPR, type, tem, arg1); + return fold_build2 (TRUTH_ORIF_EXPR, type, + fold_convert (type, tem), + arg1); } /* Convert A ? 0 : B into !A && B if A and B are truth values. */ @@ -10620,14 +11112,18 @@ fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2) /* Only perform transformation if ARG0 is easily inverted. */ tem = invert_truthvalue (arg0); if (TREE_CODE (tem) != TRUTH_NOT_EXPR) - return fold_build2 (TRUTH_ANDIF_EXPR, type, tem, op2); + return fold_build2 (TRUTH_ANDIF_EXPR, type, + fold_convert (type, tem), + op2); } /* Convert A ? 1 : B into A || B if A and B are truth values. */ if (integer_onep (arg1) && truth_value_p (TREE_CODE (arg0)) && truth_value_p (TREE_CODE (op2))) - return fold_build2 (TRUTH_ORIF_EXPR, type, arg0, op2); + return fold_build2 (TRUTH_ORIF_EXPR, type, + fold_convert (type, arg0), + op2); return NULL_TREE;