public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Philipp Tomsich <philipp.tomsich@vrull.eu>
To: Jim Wilson <jimw@sifive.com>
Cc: GCC Patches <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: Mon, 16 Nov 2020 23:59:29 +0100	[thread overview]
Message-ID: <CAAeLtUBfW9QMWaeJkWya1TKFb-Hvxhi9gTPFtwRs+o-FguxspQ@mail.gmail.com> (raw)
In-Reply-To: <CAFyWVaagbyJ0NJDq_P6Er-VEuy=G9ARe7SUPo-pz3DHPLkrCAA@mail.gmail.com>

On Mon, 16 Nov 2020 at 23:38, Jim Wilson <jimw@sifive.com> wrote:

> On Mon, Nov 16, 2020 at 10:57 AM Philipp Tomsich <philipp.tomsich@vrull.eu>
> wrote:
>
>> 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.
>>
>
> This has the risk of breaking some user code.  I've seen people write code
> for RISC-V knowing that the hardware truncates shift counts, and so not
> doing the full calculation to get the right value but just letting the
> compiler/hardware calculate it for them via truncation.  Of course this
> code has implemented defined result, but there is no reason to break it
> unnecessarily.
>

While undefined behavior (as per the C standard), GCC uses a predictable
behaviour for negative shift-amounts (and shifts that are wider than the
type):

int func(int a)
{
  return a << -1;
}

will raise the following warning:

shift-neg.c: In function 'func':
shift-neg.c:3:12: warning: left shift count is negative
[-Wshift-count-negative]
    3 |   return a << -1;
      |            ^~

and return 0:

func:
    li a0,0
    ret
    .size func, .-func


Having two different results generated here, depending on what parts of GCC
"see" the shift-amount, doesn't seem sensible and likely to cause breakage
in the long term.
I fully agree that this is undefined behavior (so no well-formed program
should rely on it), but I would prefer to have a common behavior
independent on when the constant is known.

Philipp.

  reply	other threads:[~2020-11-16 22:59 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 [this message]
2020-11-16 23:38 ` Jeff Law
2020-11-17 11:53   ` Philipp Tomsich
2020-11-17 15:56     ` Jeff Law
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=CAAeLtUBfW9QMWaeJkWya1TKFb-Hvxhi9gTPFtwRs+o-FguxspQ@mail.gmail.com \
    --to=philipp.tomsich@vrull.eu \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jimw@sifive.com \
    --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).