This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold add+negate through select into sub
ClosedPublic

Authored by spatel on Sep 6 2022, 11:28 AM.

Details

Summary

This transform came up as a potential DAGCombine in D133282, so I wanted to see how it escaped in IR too.

We do general folds in InstCombiner::SimplifySelectsFeedingBinaryOp() by checking if either arm of a select simplifies when the trailing binop is threaded into the select.

So as long as one side simplifies, it's a good fold to combine a negate and add into 1 subtract.

This is an example with a zero arm in the select:
https://alive2.llvm.org/ce/z/Hgu_Tj

And this models the tests with a cancelling 'not' op:
https://alive2.llvm.org/ce/z/BuzVV_

Diff Detail

Event Timeline

spatel created this revision.Sep 6 2022, 11:28 AM
spatel requested review of this revision.Sep 6 2022, 11:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 6 2022, 11:28 AM
craig.topper added inline comments.Sep 6 2022, 1:28 PM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
882

Are we wanting to ensure that True and False are different or that only one of them is non-null?

spatel added inline comments.Sep 6 2022, 1:55 PM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
882

We want exactly one to be non-null. If they're both non-null (both arms simplified), then it'll get handled by the existing code path. If both are null (not simplifiable), then the transform won't work.

craig.topper added inline comments.Sep 6 2022, 1:58 PM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
882

That’s not exactly what that check does though. Do we need to convert to book first before comparing?

spatel added inline comments.Sep 6 2022, 2:02 PM
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
882

Yes - sorry, clicked 'submit' before finishing the comment. I'll change that check.

spatel updated this revision to Diff 458278.Sep 6 2022, 2:09 PM

Patch updated:
Changed the check for simplified operands to do what the code comment says. I couldn't think of a test to show a failure given the other constraints.

spatel marked 2 inline comments as done.Sep 6 2022, 2:09 PM
This revision is now accepted and ready to land.Sep 6 2022, 3:30 PM
This revision was landed with ongoing or failed builds.Sep 7 2022, 5:23 AM
This revision was automatically updated to reflect the committed changes.