diff options
author | Sanjay Patel <spatel@rotateright.com> | 2018-06-03 16:35:26 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2018-06-03 16:35:26 +0000 |
commit | 3bd957b7aec4df52198f28fa9b02f711417570b4 (patch) | |
tree | 39d7bd518d7e93117749029e196070bf692286f0 /llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | |
parent | cc92f5038e7c1070e1ab4271d114ea0baade5416 (diff) | |
download | bcm5719-llvm-3bd957b7aec4df52198f28fa9b02f711417570b4.tar.gz bcm5719-llvm-3bd957b7aec4df52198f28fa9b02f711417570b4.zip |
[InstCombine] improve sub with bool folds
There's a patchwork of existing transforms trying to handle
these cases, but as seen in the changed test, we weren't
catching them all.
llvm-svn: 333845
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 69d09199a43..6fd4afffbb5 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1571,11 +1571,22 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { return BinaryOperator::CreateSub(Y, X); if (Constant *C = dyn_cast<Constant>(Op0)) { + bool IsNegate = match(C, m_ZeroInt()); Value *X; - // C - zext(bool) -> bool ? C - 1 : C - if (match(Op1, m_ZExt(m_Value(X))) && - X->getType()->getScalarSizeInBits() == 1) + if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { + // 0 - (zext bool) --> sext bool + // C - (zext bool) --> bool ? C - 1 : C + if (IsNegate) + return CastInst::CreateSExtOrBitCast(X, I.getType()); return SelectInst::Create(X, SubOne(C), C); + } + if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { + // 0 - (sext bool) --> zext bool + // C - (sext bool) --> bool ? C + 1 : C + if (IsNegate) + return CastInst::CreateZExtOrBitCast(X, I.getType()); + return SelectInst::Create(X, AddOne(C), C); + } // C - ~X == X + (1+C) if (match(Op1, m_Not(m_Value(X)))) @@ -1595,16 +1606,6 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { Constant *C2; if (match(Op1, m_Add(m_Value(X), m_Constant(C2)))) return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X); - - // Fold (sub 0, (zext bool to B)) --> (sext bool to B) - if (C->isNullValue() && match(Op1, m_ZExt(m_Value(X)))) - if (X->getType()->isIntOrIntVectorTy(1)) - return CastInst::CreateSExtOrBitCast(X, Op1->getType()); - - // Fold (sub 0, (sext bool to B)) --> (zext bool to B) - if (C->isNullValue() && match(Op1, m_SExt(m_Value(X)))) - if (X->getType()->isIntOrIntVectorTy(1)) - return CastInst::CreateZExtOrBitCast(X, Op1->getType()); } const APInt *Op0C; |