Page MenuHomePhabricator

[CodeGen] Annotate signed shift lefts with NUW
AcceptedPublic

Authored by majnemer on Apr 8 2015, 9:42 AM.

Details

Summary

For signed shifts, Op1 << Op2 in C and C++ is UB if the result cannot be
represented in the equivalent unsigned type. While a stronger statement
can be made for C, let's start small and say that a signed shift left
cannot shift out any one bits.

Diff Detail

Event Timeline

majnemer updated this revision to Diff 23426.Apr 8 2015, 9:42 AM
majnemer retitled this revision from to [CodeGen] Annotate signed shift lefts with NUW.
majnemer updated this object.
majnemer added a subscriber: Unknown Object (MLST).
chandlerc edited edge metadata.Apr 8 2015, 11:10 AM

This doesn't seem correct for a negative LHS where we only shift out 1s -- I think that should be valid.

Essentially, we seem to have a strange circumstance here where NUW as specified is not a subset of the restrictions of NSW.

This doesn't seem correct for a negative LHS where we only shift out 1s -- I think that should be valid.

Essentially, we seem to have a strange circumstance here where NUW as specified is not a subset of the restrictions of NSW.

Negative LHS is forbidden in both C11 and C++14.

C11 6.5.7p4:
If E1 has a signed type and nonnegative value, and E1 × 2**E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

C++14 [expr.shift]p2:
Otherwise, if E1 has a signed type and non-negative value, and E1 × 2**E2 is representable in the corresponding unsigned type of the result type, then that value, converted to the result type, is the resulting value; otherwise, the behavior is undefined.

rsmith accepted this revision.Apr 8 2015, 11:29 AM
rsmith edited edge metadata.

LGTM

test/CodeGen/compound-type.c
1–3

Can I persuade you to FileCheck-ize this test while you're here?

This revision is now accepted and ready to land.Apr 8 2015, 11:29 AM

Before we go here, I think we should seriously ask whether we want to undo that restriction on shift left.

I can see arguments both for and against it.

Does GCC already do this optimization?

sanjoy resigned from this revision.Sep 27 2015, 2:30 PM
sanjoy removed a reviewer: sanjoy.

This revision hasn't changed for a while, removing myself from reviewers so that it does not show up in my 'waiting on others' list.

Feel free to re-add my as a reviewer later if needed.