diff options
author | Jakub Jelinek <jakub@redhat.com> | 2006-11-05 13:13:46 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@gcc.gnu.org> | 2006-11-05 13:13:46 +0100 |
commit | 789e604dfa7e323198db58ad181a4f7842e52aa8 (patch) | |
tree | 522eca333cf72507bfdc594f48625ff147830b42 /gcc/fold-const.c | |
parent | 75c0ed055fcc0a895c8b9f195cdbfad094e84204 (diff) | |
download | gcc-789e604dfa7e323198db58ad181a4f7842e52aa8.zip gcc-789e604dfa7e323198db58ad181a4f7842e52aa8.tar.gz gcc-789e604dfa7e323198db58ad181a4f7842e52aa8.tar.bz2 |
re PR middle-end/29695 (Folding breaks (a & 0x80) ? 0x80 : 0 for unsigned char or unsigned short a)
PR middle-end/29695
* fold-const.c (fold_ternary): Fix A < 0 ? <sign bit of A> : 0
simplification.
* gcc.c-torture/execute/pr29695-1.c: New test.
* gcc.c-torture/execute/pr29695-2.c: New test.
From-SVN: r118497
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r-- | gcc/fold-const.c | 77 |
1 files changed, 70 insertions, 7 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 0ac3c86..d70e5b0 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -11380,13 +11380,76 @@ fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2) /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */ if (TREE_CODE (arg0) == LT_EXPR - && integer_zerop (TREE_OPERAND (arg0, 1)) - && integer_zerop (op2) - && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1))) - return fold_convert (type, - fold_build2 (BIT_AND_EXPR, - TREE_TYPE (tem), tem, - fold_convert (TREE_TYPE (tem), arg1))); + && integer_zerop (TREE_OPERAND (arg0, 1)) + && integer_zerop (op2) + && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1))) + { + /* sign_bit_p only checks ARG1 bits within A's precision. + If <sign bit of A> has wider type than A, bits outside + of A's precision in <sign bit of A> need to be checked. + If they are all 0, this optimization needs to be done + in unsigned A's type, if they are all 1 in signed A's type, + otherwise this can't be done. */ + if (TYPE_PRECISION (TREE_TYPE (tem)) + < TYPE_PRECISION (TREE_TYPE (arg1)) + && TYPE_PRECISION (TREE_TYPE (tem)) + < TYPE_PRECISION (type)) + { + unsigned HOST_WIDE_INT mask_lo; + HOST_WIDE_INT mask_hi; + int inner_width, outer_width; + tree tem_type; + + inner_width = TYPE_PRECISION (TREE_TYPE (tem)); + outer_width = TYPE_PRECISION (TREE_TYPE (arg1)); + if (outer_width > TYPE_PRECISION (type)) + outer_width = TYPE_PRECISION (type); + + if (outer_width > HOST_BITS_PER_WIDE_INT) + { + mask_hi = ((unsigned HOST_WIDE_INT) -1 + >> (2 * HOST_BITS_PER_WIDE_INT - outer_width)); + mask_lo = -1; + } + else + { + mask_hi = 0; + mask_lo = ((unsigned HOST_WIDE_INT) -1 + >> (HOST_BITS_PER_WIDE_INT - outer_width)); + } + if (inner_width > HOST_BITS_PER_WIDE_INT) + { + mask_hi &= ~((unsigned HOST_WIDE_INT) -1 + >> (HOST_BITS_PER_WIDE_INT - inner_width)); + mask_lo = 0; + } + else + mask_lo &= ~((unsigned HOST_WIDE_INT) -1 + >> (HOST_BITS_PER_WIDE_INT - inner_width)); + + if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi + && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo) + { + tem_type = lang_hooks.types.signed_type (TREE_TYPE (tem)); + tem = fold_convert (tem_type, tem); + } + else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0 + && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0) + { + tem_type = lang_hooks.types.unsigned_type (TREE_TYPE (tem)); + tem = fold_convert (tem_type, tem); + } + else + tem = NULL; + } + + if (tem) + return fold_convert (type, + fold_build2 (BIT_AND_EXPR, + TREE_TYPE (tem), tem, + fold_convert (TREE_TYPE (tem), + arg1))); + } /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was already handled above. */ |