This is an archive of the discontinued LLVM Phabricator instance.

[TargetLowering] try to create -1 constant operand for math ops via demanded bits
ClosedPublic

Authored by spatel on Feb 6 2018, 2:24 PM.

Details

Summary

This reverses instcombine's demanded bits' transform which always tries to clear bits in constants.

As noted in PR35792 and shown in the test diffs:
https://bugs.llvm.org/show_bug.cgi?id=35792
...we can do better in codegen by trying to form -1. The x86 sub test shows a missed opportunity. We also don't get the shift case from the bug report, but I thought it'd be best to post this as-is to make sure it looks ok.

I did investigate changing instcombine's behavior, but it would be more work to change canonicalization in IR. Clearing bits / shrinking constants can allow killing instructions, so we'd have to figure out how to not regress those cases.

Diff Detail

Repository
rL LLVM

Event Timeline

spatel created this revision.Feb 6 2018, 2:24 PM
craig.topper added inline comments.Feb 6 2018, 11:16 PM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1246 ↗(On Diff #133079)

Do we need to do anything with NSW/NUW flags?

spatel added inline comments.Feb 7 2018, 7:59 AM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1246 ↗(On Diff #133079)

Good question. I saw the block above this and was wondering why it needed to explicitly disable those flags. It's because if we don't manually disable those flags, getNode() may return an existing node that has the flags set. That's scary...

The new math op in this case can't guarantee nsw/nuw, so yes, I think we need to explicitly disable those.

spatel updated this revision to Diff 133236.Feb 7 2018, 9:42 AM

Patch updated:
Explicitly clear nsw/nuw.

RKSimon accepted this revision.Feb 8 2018, 8:02 AM

LGTM

This revision is now accepted and ready to land.Feb 8 2018, 8:02 AM
This revision was automatically updated to reflect the committed changes.