[InstCombine] move code to remove repeated constant check; NFCI

Also, consolidate tests for this fold in one place.

llvm-svn: 315745
This commit is contained in:
Sanjay Patel
2017-10-13 20:29:11 +00:00
parent 4d70754e3c
commit f0242de143
3 changed files with 17 additions and 19 deletions

View File

@@ -960,10 +960,15 @@ Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) {
return NV;
Value *X;
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->getScalarSizeInBits() == 1)
// zext(bool) + C -> bool ? C + 1 : C
// zext(bool) + C -> bool ? C + 1 : C
if (match(Op0, m_ZExt(m_Value(X))) &&
X->getType()->getScalarSizeInBits() == 1)
return SelectInst::Create(X, AddOne(Op1C), Op1);
// ~X + C --> (C-1) - X
if (match(Op0, m_Not(m_Value(X))))
return BinaryOperator::CreateSub(SubOne(Op1C), X);
const APInt *C;
if (!match(Op1, m_APInt(C)))
return nullptr;
@@ -1117,12 +1122,6 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
return BinaryOperator::CreateOr(LHS, RHS);
if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
Value *X;
if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
return BinaryOperator::CreateSub(SubOne(CRHS), X);
}
// FIXME: We already did a check for ConstantInt RHS above this.
// FIXME: Is this pattern covered by another fold? No regression tests fail on
// removal.