This is an archive of the discontinued LLVM Phabricator instance.

[KnownBits] Add support for nuw/nsw on shifts
ClosedPublic

Authored by nikic on May 23 2023, 6:17 AM.

Details

Summary

Implement precise nuw/nsw support in the KnownBits implementation, replacing the rather crude handling in ValueTracking.

Diff Detail

Event Timeline

nikic created this revision.May 23 2023, 6:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 23 2023, 6:17 AM
nikic requested review of this revision.May 23 2023, 6:17 AM
goldstein.w.n added inline comments.May 23 2023, 9:55 AM
llvm/lib/Support/KnownBits.cpp
176

nit style: Instead of nesting if statements, can you invert them and just return immediately? Think it makes code easier to read.

213

Imo, this check should just be at the top. Then you can drop the RHS.getConstant().ult(BitWidth) check before ShiftByConst

232

in the NSW/NUW we should be able to further bound MaxShiftAmount with LHS.countLeadingZeros no? (and poison if MinShiftAmunt > LHS.countLeadingZeros)

253–258

can you assert no conflict? Its not super obvious just reading the code that the loop will be enough to clear the all zeros/ones initial state in Known.

nikic updated this revision to Diff 525077.May 24 2023, 2:34 AM

Rebase, check for conflicts, use early return.

nikic added inline comments.May 24 2023, 2:40 AM
llvm/lib/Support/KnownBits.cpp
176

I've done this for the first check here and also for the isUnknown checks in https://github.com/llvm/llvm-project/commit/2d48a771fc00681414c54ea3936bff91f3b253c4.

232

Yes, but this is already implicitly handled by the loop below. It stops once it gets the first shift amount that results in poison due to nuw/nsw.

253–258

You are right, it's necessary to check for conflicts here. I've added a general assertion for this in the exhaustive tests, so we don't have to remember checking it for every function: https://github.com/llvm/llvm-project/commit/d4bfc144eaa6e34eafaab1e8574421ebd2bb78ed

goldstein.w.n accepted this revision.May 24 2023, 9:54 AM

LGTM.

llvm/lib/Support/KnownBits.cpp
232

Ah, fair enough.

255

This should only ever occur if NUW or NSW right? Maybe assert that?

This revision is now accepted and ready to land.May 24 2023, 9:54 AM
This revision was automatically updated to reflect the committed changes.