This is an archive of the discontinued LLVM Phabricator instance.

InstCombineSimplifyDemanded: Remove nsw/nuw flags when optimizing demanded bits
ClosedPublic

Authored by MatzeB on Apr 30 2015, 2:12 PM.

Details

Summary

When optimizing demanded bits of the operands of an Add we have to
remove the nsw/nuw flags as we have no guarantee anymore that we don't
wrap. This is legal here because the top bit is not demanded. In fact
this operaion was already performed but missed in the case of an Add
with a constant on the right side. To fix this this patch refactors the
code to unify the code paths in SimplifyDemandedUseBits() handling of
Add/Sub:

  • The transformation of Add->Or is removed from the simplify demand code because the equivalent transformation exists in InstCombiner::visitAdd()
  • KnownOnes/KnownZero are not adjusted for Add x, C anymore as computeKnownBits() already performs these computations.
  • The simplification of the operands is unified. In this new version constant on the right side of a Sub are shrunk now as I could not find a reason why not to do so.
  • The special case for clearing nsw/nuw in ShrinkDemandedConstant() is not necessary anymore as the caller does that already.

This also fixes PR11449.

Diff Detail

Repository
rL LLVM

Event Timeline

MatzeB updated this revision to Diff 24768.Apr 30 2015, 2:12 PM
MatzeB retitled this revision from to InstCombineSimplifyDemanded: Remove nsw/nuw flags when optimizing demanded bits.
MatzeB updated this object.
MatzeB edited the test plan for this revision. (Show Details)
MatzeB added a reviewer: majnemer.
MatzeB set the repository for this revision to rL LLVM.
MatzeB added a subscriber: Unknown Object (MLST).

Turns out PR11449 was already fixed before this patch. The patch is still necessary for the more complicated testcase that is included.

majnemer edited edge metadata.Apr 30 2015, 2:42 PM

Turns out PR11449 was already fixed before this patch. The patch is still necessary for the more complicated testcase that is included.

Yes, I fixed PR23309 with r235544 and followed it up with r235558. I didn't realize that PR11449 was already filed. Looks like I missed something though, happy to see a more comprehensive solution. :)

lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
522 ↗(On Diff #24768)

Any reason why you changed this? countLeadingZeros returns an unsigned.

524 ↗(On Diff #24768)

Update this comment to read for both ADD and SUB?

majnemer accepted this revision.Apr 30 2015, 2:43 PM
majnemer edited edge metadata.

LGTM with my review comments addressed.

This revision is now accepted and ready to land.Apr 30 2015, 2:43 PM
This revision was automatically updated to reflect the committed changes.