This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold binop of shifts with related amounts
ClosedPublic

Authored by nikic on Jun 14 2023, 8:32 AM.

Details

Summary

Fold

binop(shift(ShiftedC1, ShAmt), shift(ShiftedC2, add(ShAmt, AddC)))
  ->
shift(binop(ShiftedC1, shift(ShiftedC2, AddC)), ShAmt)

where both shifts are the same and AddC is a valid shift amount.

Proofs: https://alive2.llvm.org/ce/z/PhVVeg

Diff Detail

Event Timeline

nikic created this revision.Jun 14 2023, 8:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 8:32 AM
nikic requested review of this revision.Jun 14 2023, 8:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 14 2023, 8:32 AM
nikic updated this revision to Diff 531353.Jun 14 2023, 8:32 AM
nikic edited the summary of this revision. (Show Details)

Conservatively bail on constant expressions.

goldstein.w.n added inline comments.Jun 14 2023, 9:55 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2125

https://alive2.llvm.org/ce/z/Y46Cnq add with shl works as well. Fine with only handling BW for now but maybe InstructionCombining.cpp is a better place to put this so its easier to extend in the future.

2136

nit, since early out is an option, make this 2 seperate if statements to keep the expr a little less busy?

2146

static_cast instead of C-style

2152

nit: BinaryOperator::create(...) then drop the replaceInstUsesWith below?

goldstein.w.n added inline comments.Jun 14 2023, 11:42 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2151

nit: Builder.CreateBinOp(ShiftOp, ShiftedC2, AddC) -> constantexpr.

llvm/test/Transforms/InstCombine/binop-of-displaced-shifts.ll
232

maybe a negative test addc is non-splat and one element > bitwidth.

nikic updated this revision to Diff 531647.Jun 15 2023, 1:54 AM
nikic marked 5 inline comments as done.
nikic edited the summary of this revision. (Show Details)

Support adds, address comments.

nikic retitled this revision from [InstCombine] Fold binop op shifts with related amounts to [InstCombine] Fold binop of shifts with related amounts.Jun 15 2023, 1:54 AM
nikic added inline comments.Jun 15 2023, 2:14 AM
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
2151

IRBuilder is preferred unless the Constant * type is required. It will work transparently when shift constant expressions are removed.

This revision is now accepted and ready to land.Jun 27 2023, 10:10 AM
This revision was automatically updated to reflect the committed changes.