This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Teach computeKnownBits for PHI nodes to compute sign bit for a recurrence with a NSW addition.
ClosedPublic

Authored by craig.topper on Jun 18 2016, 1:26 PM.

Details

Summary

If a operation for a recurrence is an addition with no signed wrap and both input sign bits are 0, then the result sign bit must also be 0. Similar for the negative case.

I found this deficiency while playing around with a loop in the x86 backend that contained a signed division that could be optimized into an unsigned division if we could prove both inputs were positive. One of them being the loop induction variable. With this patch we can perform the conversion for this case. The test case here is a contrived variation of the loop I was looking at.

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper retitled this revision from to [ValueTracking] Teach computeKnownBits for PHI nodes to compute sign bit for a recurrence with a NSW addition..
craig.topper updated this object.
craig.topper added a subscriber: llvm-commits.
sanjoy requested changes to this revision.Jun 20 2016, 11:50 AM
sanjoy edited edge metadata.
sanjoy added inline comments.
lib/Analysis/ValueTracking.cpp
1251 ↗(On Diff #61171)

Can you please add a test for this second bit (i.e. negative stays negative)?

This revision now requires changes to proceed.Jun 20 2016, 11:50 AM
craig.topper added inline comments.Jun 20 2016, 12:23 PM
lib/Analysis/ValueTracking.cpp
1251 ↗(On Diff #61171)

Do you have a suggestion for how to test that? I knew of the sign bit being 0 check in instcombine of sdiv so I used that for the 0 case. But I don't know enough about what optimizations can benefit from knowing the sign bit is set.

sanjoy added inline comments.Jun 20 2016, 12:38 PM
lib/Analysis/ValueTracking.cpp
1251 ↗(On Diff #61171)

Perhaps you could check that a bitwise-and with 1 << (bitwidth - 1) folds to a constant? If that doesn't fold despite knowing the sign bit, then that's a separate bug. :)

craig.topper edited edge metadata.

I've added the additional test case for negative staying negative.

sanjoy accepted this revision.Jun 28 2016, 6:19 PM
sanjoy edited edge metadata.
This revision is now accepted and ready to land.Jun 28 2016, 6:19 PM

lgtm (with text so that it goes to the list this time)

This revision was automatically updated to reflect the committed changes.