This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Extend (add (sext x), cst) --> (sext (add x, cst')) and (add (zext x), cst) --> (zext (add x, cst')) to work for vectors
ClosedPublic

Authored by craig.topper on Aug 24 2018, 2:56 PM.

Details

Summary

This patch extends this transform to work for vectors, including non-splat vectors that contain all ConstantInts. I've added a new matcher to PatternMatch to detect vectors of all ConstantInts. We can probably also skip undefs in it, but I've left that for the future.

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.Aug 24 2018, 2:56 PM
spatel added inline comments.Aug 25 2018, 8:13 AM
lib/Transforms/InstCombine/InstCombineAddSub.cpp
1199 ↗(On Diff #162486)

I don't understand what benefit the new pattern matcher offers. We're already guaranteed that the constant is (vector) integer type because this is an 'add'. Do we need to disallow constant expressions? If not, use the existing m_Constant() matcher?

craig.topper added inline comments.Aug 25 2018, 10:12 AM
lib/Transforms/InstCombine/InstCombineAddSub.cpp
1199 ↗(On Diff #162486)

I did it to disallow constant expressions and be better than just using m_APInt. I don't think willNotOverflowSignedAdd will give an answer for constant expressions in general. So we'd just be creating new constant expressions with the call get to ConstantExpr::getTrunc for no reason. And we'd need tests for it.

lebedev.ri added inline comments.
include/llvm/IR/PatternMatch.h
211–212 ↗(On Diff #162486)
  1. This should ideally describe how this is different from the normal m_Constant() matcher (i.e. that this ignores constant expressions)
  2. Can we go the other way around, and add a matcher for the constant expressions?
craig.topper added inline comments.Aug 25 2018, 10:32 AM
include/llvm/IR/PatternMatch.h
211–212 ↗(On Diff #162486)

I'm not sure I understand what you're asking for 2.

Add more to comment. Fix typo in comment

I appreciate the concern about having a test for constant expressions if we just use m_Constant() here, but I wouldn't hold this patch up for that. We have dozens of uses of m_Constant() and barely any tests for constant expressions (presumably because those are rarely the motivation for adding a transform).

The rarity of constant expressions would also suggest that we don't really need to be worried about efficiency in handling those.

So my preference would be:

  1. Add the tests with baseline checks.
  2. Switch this transform over to m_Constant(), so it can handle arbitrary vector constants.
  3. Add m_ConstantIntOrConstantIntVec and apply it wherever we think it would make the code clearer/faster. I'm not convinced yet that it's worth the effort to exclude constant expressions, but if it is, then let's do it throughout instcombine?

Switch to m_Constant

spatel accepted this revision.Aug 27 2018, 4:34 PM

LGTM

This revision is now accepted and ready to land.Aug 27 2018, 4:34 PM
This revision was automatically updated to reflect the committed changes.