This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Transform `(add (shl (neg X), Cnt))` -> `(sub (shl X, Cnt))`
ClosedPublic

Authored by goldstein.w.n on Jul 9 2023, 4:31 PM.

Diff Detail

Event Timeline

goldstein.w.n created this revision.Jul 9 2023, 4:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 9 2023, 4:31 PM
goldstein.w.n requested review of this revision.Jul 9 2023, 4:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 9 2023, 4:31 PM
nikic added a comment.Jul 25 2023, 2:18 PM

We have a general Negator infrastructure, which we could use to fold add X, Y to sub X, -Y if the latter is free. However, given how we currently use it to fold sub X, Y to add X, -Y, actually trying to do that would probably result in many new and exiting infinite combine loops, so I'm happy to take the one-off pattern...

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1201

Can this function be reduced to something along the lines of this?

if (match(I, m_c_Add(m_OneUse(m_Shl(m_OneUse(m_Neg(m_Value(X))), m_Value(Cnt))),
                     m_Value(X))) {
  Value *NewShl = Builder.CreateShl(X, Cnt);
  return BinaryOperator::CreateSub(Other, NewShl);
}
llvm/test/Transforms/InstCombine/add-shift.ll
62–80

This only covers one of the one-use conditions.

goldstein.w.n marked 2 inline comments as done.

Simplify logic greatly

nikic accepted this revision.Jul 26 2023, 12:38 AM

LG

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1185

Y -> Cnt to match the name in the implementation.

1196

This is probably small enough to inline now.

This revision is now accepted and ready to land.Jul 26 2023, 12:38 AM
This revision was landed with ongoing or failed builds.Aug 16 2023, 8:41 PM
This revision was automatically updated to reflect the committed changes.