This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] shrink type of sdiv if dividend is sexted and constant divisor is small enough (PR28153)
ClosedPublic

Authored by spatel on Jun 27 2016, 1:45 PM.

Diff Detail

Event Timeline

spatel updated this revision to Diff 62013.Jun 27 2016, 1:45 PM
spatel retitled this revision from to [InstCombine] shrink type of sdiv if dividend is sexted and constant divisor is small enough (PR28153).
spatel updated this object.
spatel added reviewers: eli.friedman, majnemer, hans.
spatel added a subscriber: llvm-commits.
majnemer added inline comments.Jun 27 2016, 2:55 PM
lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1156

Say X is i16 -32768 and C is i32 65535, the result of the sdiv will be -32768/65535 which is zero.
However, trunc of C to i16 is 65535. the result of X sdiv C will be -32768/-1 which is UB because it overflows.

spatel added inline comments.Jun 27 2016, 3:12 PM
lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1156

The code comment may be misleading. Your example won't fold because getMinSignedBits() for 65535 is 17. It may be clearer when you see the limits in the test cases. Any ideas about how to make that code comment more specific? Should I put your example case in there?

spatel added inline comments.Jun 27 2016, 3:18 PM
lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1156

Sorry if this comment wasn't clear as well. :)
I checked in a couple of negative test cases ahead of this patch; they're the last ones in test/Transforms/InstCombine/div.ll.

majnemer accepted this revision.Jun 27 2016, 3:21 PM
majnemer edited edge metadata.

LGTM

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
1156

I missed the MinSignedBits part of your patch, seems fine to me. I think it'd be good to include by testcase.

This revision is now accepted and ready to land.Jun 27 2016, 3:22 PM
This revision was automatically updated to reflect the committed changes.