From: Kugan <kugan.vivekanandarajah@linaro.org>
To: Richard Biener <richard.guenther@gmail.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>
Subject: Re: [3/7] Optimize ZEXT_EXPR with tree-vrp
Date: Sun, 11 Oct 2015 02:56:00 -0000 [thread overview]
Message-ID: <5619CFE6.40403@linaro.org> (raw)
In-Reply-To: <CAFiYyc0CLLQuZfhFZ3NQad7225tEYyZmg+m2Vq0GwjaS7=RR4Q@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1776 bytes --]
On 09/10/15 21:29, Richard Biener wrote:
> + unsigned int prec = tree_to_uhwi (vr1.min);
>
> this should use unsigned HOST_WIDE_INT
>
> + wide_int sign_bit = wi::shwi (1ULL << (prec - 1),
> + TYPE_PRECISION (TREE_TYPE (vr0.min)));
>
> use wi::one (TYPE_PRECISION (TREE_TYPE (vr0.min))) << (prec - 1);
>
> That is, you really need to handle precisions bigger than HOST_WIDE_INT.
>
> But I suppose wide_int really misses a test_bit function (it has a set_bit
> one already).
>
> + if (wi::bit_and (must_be_nonzero, sign_bit) == sign_bit)
> + {
> + /* If to-be-extended sign bit is one. */
> + tmin = type_min;
> + tmax = may_be_nonzero;
>
> I think tmax should be zero-extended may_be_nonzero from prec.
>
> + else if (wi::bit_and (may_be_nonzero, sign_bit)
> + != sign_bit)
> + {
> + /* If to-be-extended sign bit is zero. */
> + tmin = must_be_nonzero;
> + tmax = may_be_nonzero;
>
> likewise here tmin/tmax should be zero-extended may/must_be_nonzero from prec.
>
> + case SEXT_EXPR:
> + {
> + unsigned int prec = tree_to_uhwi (op1);
> + wide_int sign_bit = wi::shwi (1ULL << (prec - 1),
> + TYPE_PRECISION (TREE_TYPE (vr0.min)));
> + wide_int mask = wi::shwi (((1ULL << (prec - 1)) - 1),
> + TYPE_PRECISION (TREE_TYPE (vr0.max)));
>
> this has the same host precision issues of 1ULL (HOST_WIDE_INT).
> There is wi::mask, eventually you can use wi::set_bit_in_zero to
> produce the sign-bit wide_int (also above).
Thanks Ricahrd. Does the attached patch looks better ?
Thanks,
Kugan
[-- Attachment #2: 0003-Optimize-ZEXT_EXPR-with-tree-vrp.patch --]
[-- Type: text/x-diff, Size: 3524 bytes --]
From cf5f75f5c96d30cdd968e71035a398cb0d5fcff7 Mon Sep 17 00:00:00 2001
From: Kugan Vivekanandarajah <kugan.vivekanandarajah@linaro.org>
Date: Mon, 17 Aug 2015 13:45:52 +1000
Subject: [PATCH 3/7] Optimize ZEXT_EXPR with tree-vrp
---
gcc/tree-vrp.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c
index 2cd71a2..c04d290 100644
--- a/gcc/tree-vrp.c
+++ b/gcc/tree-vrp.c
@@ -2317,6 +2317,7 @@ extract_range_from_binary_expr_1 (value_range_t *vr,
&& code != LSHIFT_EXPR
&& code != MIN_EXPR
&& code != MAX_EXPR
+ && code != SEXT_EXPR
&& code != BIT_AND_EXPR
&& code != BIT_IOR_EXPR
&& code != BIT_XOR_EXPR)
@@ -2877,6 +2878,52 @@ extract_range_from_binary_expr_1 (value_range_t *vr,
extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
return;
}
+ else if (code == SEXT_EXPR)
+ {
+ gcc_assert (range_int_cst_p (&vr1));
+ HOST_WIDE_INT prec = tree_to_uhwi (vr1.min);
+ type = vr0.type;
+ wide_int tmin, tmax;
+ wide_int may_be_nonzero, must_be_nonzero;
+
+ wide_int type_min = wi::min_value (prec, SIGNED);
+ wide_int type_max = wi::max_value (prec, SIGNED);
+ type_min = wide_int_to_tree (expr_type, type_min);
+ type_max = wide_int_to_tree (expr_type, type_max);
+ wide_int sign_bit
+ = wi::set_bit_in_zero (prec - 1,
+ TYPE_PRECISION (TREE_TYPE (vr0.min)));
+ if (zero_nonzero_bits_from_vr (expr_type, &vr0,
+ &may_be_nonzero,
+ &must_be_nonzero))
+ {
+ if (wi::bit_and (must_be_nonzero, sign_bit) == sign_bit)
+ {
+ /* If to-be-extended sign bit is one. */
+ tmin = type_min;
+ tmax = wi::zext (may_be_nonzero, prec);
+ }
+ else if (wi::bit_and (may_be_nonzero, sign_bit)
+ != sign_bit)
+ {
+ /* If to-be-extended sign bit is zero. */
+ tmin = wi::zext (must_be_nonzero, prec);
+ tmax = wi::zext (may_be_nonzero, prec);
+ }
+ else
+ {
+ tmin = type_min;
+ tmax = type_max;
+ }
+ }
+ else
+ {
+ tmin = type_min;
+ tmax = type_max;
+ }
+ min = wide_int_to_tree (expr_type, tmin);
+ max = wide_int_to_tree (expr_type, tmax);
+ }
else if (code == RSHIFT_EXPR
|| code == LSHIFT_EXPR)
{
@@ -9244,6 +9291,28 @@ simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
break;
}
break;
+ case SEXT_EXPR:
+ {
+ unsigned int prec = tree_to_uhwi (op1);
+ wide_int sign_bit
+ = wi::set_bit_in_zero (prec - 1,
+ TYPE_PRECISION (TREE_TYPE (vr0.min)));
+ wide_int mask = wi::mask (prec, true,
+ TYPE_PRECISION (TREE_TYPE (vr0.min)));
+ if (wi::bit_and (must_be_nonzero0, sign_bit) == sign_bit)
+ {
+ /* If to-be-extended sign bit is one. */
+ if (wi::bit_and (must_be_nonzero0, mask) == mask)
+ op = op0;
+ }
+ else if (wi::bit_and (may_be_nonzero0, sign_bit) != sign_bit)
+ {
+ /* If to-be-extended sign bit is zero. */
+ if (wi::bit_and (may_be_nonzero0, mask) == 0)
+ op = op0;
+ }
+ }
+ break;
default:
gcc_unreachable ();
}
@@ -9946,6 +10015,7 @@ simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
case BIT_AND_EXPR:
case BIT_IOR_EXPR:
+ case SEXT_EXPR:
/* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
if all the bits being cleared are already cleared or
all the bits being set are already set. */
--
1.9.1
next prev parent reply other threads:[~2015-10-11 2:56 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-07 2:55 [0/7] Type promotion pass and elimination of zext/sext Kugan
2015-09-07 2:57 ` [1/7] Add new tree code SEXT_EXPR Kugan
2015-09-15 13:20 ` Richard Biener
2015-10-11 10:35 ` Kugan
2015-10-12 12:22 ` Richard Biener
2015-10-15 5:49 ` Kugan
2015-10-21 10:49 ` Richard Biener
2015-09-07 2:58 ` [2/7] Add new type promotion pass Kugan
2015-10-15 5:52 ` Kugan
2015-10-15 22:47 ` Richard Henderson
2015-09-07 3:00 ` [3/7] Optimize ZEXT_EXPR with tree-vrp Kugan
2015-09-15 13:18 ` Richard Biener
2015-10-06 23:12 ` kugan
2015-10-07 8:20 ` Richard Biener
2015-10-07 23:40 ` Kugan
2015-10-09 10:29 ` Richard Biener
2015-10-11 2:56 ` Kugan [this message]
2015-10-12 12:13 ` Richard Biener
2015-09-07 3:01 ` [5/7] Allow gimple debug stmt in widen mode Kugan
2015-09-07 13:46 ` Michael Matz
2015-09-08 0:01 ` Kugan
2015-09-15 13:02 ` Richard Biener
2015-10-15 5:45 ` Kugan
2015-10-16 9:27 ` Richard Biener
2015-10-18 20:51 ` Kugan
2015-09-07 3:01 ` [4/7] Use correct promoted mode sign for result of GIMPLE_CALL Kugan
2015-09-07 13:16 ` Michael Matz
2015-09-08 0:00 ` Kugan
2015-09-08 15:45 ` Jeff Law
2015-09-08 22:09 ` Jim Wilson
2015-09-15 12:51 ` Richard Biener
2015-10-07 1:03 ` kugan
2015-09-07 3:03 ` [6/7] Temporary workaround to get aarch64 bootstrap Kugan
2015-09-07 3:03 ` [5/7] Allow gimple debug stmt in widen mode Kugan
2015-09-07 5:54 ` [7/7] Adjust-arm-test cases Kugan
2015-11-02 11:43 ` Richard Earnshaw
2015-10-20 20:13 ` [0/7] Type promotion pass and elimination of zext/sext Kugan
2015-10-21 12:56 ` Richard Biener
2015-10-21 13:57 ` Richard Biener
2015-10-21 17:17 ` Joseph Myers
2015-10-21 18:11 ` Richard Henderson
2015-10-22 12:48 ` Richard Biener
2015-10-22 11:01 ` Kugan
2015-10-22 14:24 ` Richard Biener
2015-10-27 1:48 ` kugan
2015-10-28 15:51 ` Richard Biener
2015-11-02 9:17 ` Kugan
2015-11-03 14:40 ` Richard Biener
2015-11-08 9:43 ` Kugan
2015-11-10 14:13 ` Richard Biener
2015-11-12 6:08 ` Kugan
2015-11-14 1:15 ` Kugan
2015-11-18 14:04 ` Richard Biener
2015-11-18 15:06 ` Richard Biener
2015-11-24 2:52 ` Kugan
2015-12-10 0:27 ` Kugan
2015-12-16 13:18 ` Richard Biener
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5619CFE6.40403@linaro.org \
--to=kugan.vivekanandarajah@linaro.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=richard.guenther@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).