diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -669,6 +669,31 @@ } } +static void replaceSubOverflowUses(IntrinsicInst *II, Value *A, Value *B, + SmallVectorImpl &ToRemove) { + IRBuilder<> Builder(II->getParent(), II->getIterator()); + Value *Sub = nullptr; + for (User *U : make_early_inc_range(II->users())) { + if (match(U, m_ExtractValue<0>(m_Value()))) { + if (!Sub) + Sub = Builder.CreateSub(A, B); + U->replaceAllUsesWith(Sub); + } else if (match(U, m_ExtractValue<1>(m_Value()))) + U->replaceAllUsesWith(Builder.getFalse()); + else + continue; + + if (U->use_empty()) { + auto *I = cast(U); + ToRemove.push_back(I); + I->setOperand(0, PoisonValue::get(II->getType())); + } + } + + if (II->use_empty()) + II->eraseFromParent(); +} + static void tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info, SmallVectorImpl &ToRemove) { @@ -692,28 +717,7 @@ !DoesConditionHold(CmpInst::ICMP_SGE, B, ConstantInt::get(A->getType(), 0), Info)) return; - - IRBuilder<> Builder(II->getParent(), II->getIterator()); - Value *Sub = nullptr; - for (User *U : make_early_inc_range(II->users())) { - if (match(U, m_ExtractValue<0>(m_Value()))) { - if (!Sub) - Sub = Builder.CreateSub(A, B); - U->replaceAllUsesWith(Sub); - } else if (match(U, m_ExtractValue<1>(m_Value()))) - U->replaceAllUsesWith(Builder.getFalse()); - else - continue; - - if (U->use_empty()) { - auto *I = cast(U); - ToRemove.push_back(I); - I->setOperand(0, PoisonValue::get(II->getType())); - } - } - - if (II->use_empty()) - II->eraseFromParent(); + replaceSubOverflowUses(II, A, B, ToRemove); } }