From: Jeff Law <law@redhat.com>
To: Philipp Tomsich <philipp.tomsich@vrull.eu>
Cc: gcc-patches@gcc.gnu.org, Philipp Tomsich <prt@gnu.org>
Subject: Re: [PATCH v1 1/2] Simplify shifts wider than the bitwidth of types
Date: Tue, 17 Nov 2020 08:56:19 -0700 [thread overview]
Message-ID: <d398b57a-1e29-ebad-d348-91bc50cb3a99@redhat.com> (raw)
In-Reply-To: <CAAeLtUABWu9DavwzR81-VQVvf9-Utc+ZxbunHHWWQbFmxdKcOA@mail.gmail.com>
On 11/17/20 4:53 AM, Philipp Tomsich wrote:
> Jeff,
>
> On Tue, 17 Nov 2020 at 00:38, Jeff Law <law@redhat.com
> <mailto:law@redhat.com>> wrote:
>
>
> On 11/16/20 11:57 AM, Philipp Tomsich wrote:
> > From: Philipp Tomsich <prt@gnu.org <mailto:prt@gnu.org>>
> >
> > While most shifts wider than the bitwidth of a type will be
> caught by
> > other passes, it is possible that these show up for VRP.
> > Consider the following example:
> > int func (int a, int b, int c)
> > {
> > return (a << ((b && c) - 1));
> > }
> >
> > This adds simplify_using_ranges::simplify_lshift_using_ranges to
> > detect and rewrite such cases. If the intersection of meaningful
> > shift amounts for the underlying type and the value-range computed
> > for the shift-amount (whether an integer constant or a variable) is
> > empty, the statement is replaced with the zero-constant of the same
> > precision as the result.
> >
> > gcc/ChangeLog:
> >
> > * vr-values.h (simplify_using_ranges): Declare.
> > * vr-values.c (simplify_lshift_using_ranges): New function.
> > (simplify): Use simplify_lshift_using_ranges for LSHIFT_EXPR.
>
> Umm, isn't this a shift wider than the bitwidth undefined
> behavior? We
> should be generating warnings for that, not trying to further optimize
> it :-)
>
>
> The shift is undefined behavior on the language level (for C) and a
> warning
> will be generated, if such a shift is encountered; additionally, the
> shift will be
> replaced with the value 0.
>
> However, in the above case, the shift is generated only in the middle end:
> At 136t.walloca, I still have:
>
> # RANGE [-1, 0]
> _1 = iftmp.1_2 + -1;
> _6 = a_5(D) << _1;
>
> Whereas at 137t.pre, this is changed into:
>
> Found partial redundancy for expression {lshift_expr,a_5(D),_1} (0006)
> Inserted _9 = a_5(D) << -1;
>
>
> In other words, the change to VRP canonicalizes what a lshift_expr with an
> shift-amount outside of the type width means... it doesn't assume anything
> about the original language.
> Do we assume that a LSHIFT_EXPR has the same semantics as for a
> C-language shift-left? If so, then pre should not generate the LSHIFT_EXPR
> for _9... or we might even catch this later in path isolation (as
> undefined
> behavior, insert a __builtin_trap() and emit a warning)?
>
> Note that in his comment to patch 2/2, Jim has noted that user code for
> RISC-V may assume a truncation of the shift-operand...
What I'd suggest doing would be to leave the invalid shift count in the
IL in VRP, then extend the erroneous path isolation code to turn an
invalid shift into a trap (conditionally of course).
jeff
next prev parent reply other threads:[~2020-11-17 15:56 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-16 18:57 Philipp Tomsich
2020-11-16 18:57 ` [PATCH v1 2/2] RISC-V: Adjust predicates for immediate shift operands Philipp Tomsich
2020-11-16 22:27 ` Jim Wilson
2020-11-16 22:45 ` Philipp Tomsich
2020-11-17 17:06 ` Jim Wilson
2020-11-16 22:38 ` [PATCH v1 1/2] Simplify shifts wider than the bitwidth of types Jim Wilson
2020-11-16 22:59 ` Philipp Tomsich
2020-11-16 23:38 ` Jeff Law
2020-11-17 11:53 ` Philipp Tomsich
2020-11-17 15:56 ` Jeff Law [this message]
2020-11-17 16:29 ` Philipp Tomsich
2020-11-17 16:46 ` Jakub Jelinek
2020-11-17 16:54 ` Jeff Law
2020-11-17 16:58 ` Jakub Jelinek
2020-11-18 23:46 ` Jeff Law
2020-11-17 17:23 ` Philipp Tomsich
2020-11-17 18:02 ` Jakub Jelinek
2020-11-17 17:14 ` Jim Wilson
2020-11-17 17:55 ` Jakub Jelinek
2020-11-17 16:35 ` Philipp Tomsich
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=d398b57a-1e29-ebad-d348-91bc50cb3a99@redhat.com \
--to=law@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=philipp.tomsich@vrull.eu \
--cc=prt@gnu.org \
/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).