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.
Details
Diff Detail
Event Timeline
lib/Transforms/InstCombine/InstCombineAddSub.cpp | ||
---|---|---|
1199 | 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? |
lib/Transforms/InstCombine/InstCombineAddSub.cpp | ||
---|---|---|
1199 | 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. |
include/llvm/IR/PatternMatch.h | ||
---|---|---|
211–212 |
|
include/llvm/IR/PatternMatch.h | ||
---|---|---|
211–212 | I'm not sure I understand what you're asking for 2. |
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:
- Add the tests with baseline checks.
- Switch this transform over to m_Constant(), so it can handle arbitrary vector constants.
- 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?