Index: lib/Transforms/Scalar/CorrelatedValuePropagation.cpp =================================================================== --- lib/Transforms/Scalar/CorrelatedValuePropagation.cpp +++ lib/Transforms/Scalar/CorrelatedValuePropagation.cpp @@ -493,6 +493,21 @@ return true; } +// NOTE: do not forget to call Instr->eraseFromParent(); afterwards! +BinaryOperator *CreateTruncatedBO(BinaryOperator *Instr, unsigned NewWidth) { + auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth); + auto *LHS = CastInst::Create(Instruction::Trunc, Instr->getOperand(0), + TruncTy, Instr->getName() + ".lhs.trunc", Instr); + auto *RHS = CastInst::Create(Instruction::Trunc, Instr->getOperand(1), + TruncTy, Instr->getName() + ".rhs.trunc", Instr); + auto *BO = BinaryOperator::Create(Instr->getOpcode(), LHS, RHS, + Instr->getName(), Instr); + auto *Zext = CastInst::Create(Instruction::ZExt, BO, Instr->getType(), + Instr->getName() + ".zext", Instr); + Instr->replaceAllUsesWith(Zext); + return BO; +} + /// Try to shrink a udiv/urem's width down to the smallest power of two that's /// sufficient to contain its operands. static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) { @@ -518,19 +533,9 @@ return false; ++NumUDivs; - auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth); - auto *LHS = CastInst::Create(Instruction::Trunc, Instr->getOperand(0), TruncTy, - Instr->getName() + ".lhs.trunc", Instr); - auto *RHS = CastInst::Create(Instruction::Trunc, Instr->getOperand(1), TruncTy, - Instr->getName() + ".rhs.trunc", Instr); - auto *BO = - BinaryOperator::Create(Instr->getOpcode(), LHS, RHS, Instr->getName(), Instr); - auto *Zext = CastInst::Create(Instruction::ZExt, BO, Instr->getType(), - Instr->getName() + ".zext", Instr); + auto *BO = CreateTruncatedBO(Instr, NewWidth); if (BO->getOpcode() == Instruction::UDiv) BO->setIsExact(Instr->isExact()); - - Instr->replaceAllUsesWith(Zext); Instr->eraseFromParent(); return true; }