This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Fold add (mul x, C), x to mul x, C+1
ClosedPublic

Authored by nikic on Apr 14 2023, 7:17 AM.

Details

Summary

While this is normally non-canonical IR, this pattern can appear during SDAG lowering if the add is actually a getelementptr, as illustrated in @test_ptr. This pattern comes up when doing provenance-aware high-bit pointer tagging.

Proof: https://alive2.llvm.org/ce/z/DLoEcs

Diff Detail

Event Timeline

nikic created this revision.Apr 14 2023, 7:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 7:17 AM
nikic requested review of this revision.Apr 14 2023, 7:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 7:17 AM
RKSimon accepted this revision.Apr 14 2023, 7:22 AM

LGTM with one minor

llvm/test/CodeGen/X86/add-of-mul.ll
24

pre-add this new test.

This revision is now accepted and ready to land.Apr 14 2023, 7:22 AM
goldstein.w.n added inline comments.Apr 14 2023, 9:16 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3061

Does the equivilent ever come up for sub?

nikic added inline comments.Apr 14 2023, 12:17 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
3061

I don't think so. You'd need something like gep(x, sub(0, mul(x, C))) for that, and that would be canonicalized to gep(x, mul(x, -C)) rather than becoming sub(x, mul(x, C)) in the backend.

This revision was automatically updated to reflect the committed changes.