diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 844e18dd7d8c5..6ea09ed60c2f8 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1671,8 +1671,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { // Intrinsics cannot occur in an invoke or a callbr, so handle them here // instead of in visitCallBase. if (auto *MI = dyn_cast(II)) { - bool Changed = false; - // memmove/cpy/set of zero bytes is a noop. if (Constant *NumBytes = dyn_cast(MI->getLength())) { if (NumBytes->isNullValue()) @@ -1680,29 +1678,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { } // No other transformations apply to volatile transfers. - if (auto *M = dyn_cast(MI)) - if (M->isVolatile()) - return nullptr; - - // If we have a memmove and the source operation is a constant global, - // then the source and dest pointers can't alias, so we can change this - // into a call to memcpy. - if (auto *MMI = dyn_cast(MI)) { - if (GlobalVariable *GVSrc = dyn_cast(MMI->getSource())) - if (GVSrc->isConstant()) { - Module *M = CI.getModule(); - Intrinsic::ID MemCpyID = - isa(MMI) - ? Intrinsic::memcpy_element_unordered_atomic - : Intrinsic::memcpy; - Type *Tys[3] = { CI.getArgOperand(0)->getType(), - CI.getArgOperand(1)->getType(), - CI.getArgOperand(2)->getType() }; - CI.setCalledFunction( - Intrinsic::getOrInsertDeclaration(M, MemCpyID, Tys)); - Changed = true; - } - } + if (MI->isVolatile()) + return nullptr; if (AnyMemTransferInst *MTI = dyn_cast(MI)) { // memmove(x,x,size) -> noop. @@ -1734,7 +1711,25 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { return eraseInstFromFunction(CI); } - if (Changed) return II; + // If we have a memmove and the source operation is a constant global, + // then the source and dest pointers can't alias, so we can change this + // into a call to memcpy. + if (auto *MMI = dyn_cast(MI)) { + if (GlobalVariable *GVSrc = dyn_cast(MMI->getSource())) + if (GVSrc->isConstant()) { + Module *M = CI.getModule(); + Intrinsic::ID MemCpyID = + isa(MMI) + ? Intrinsic::memcpy_element_unordered_atomic + : Intrinsic::memcpy; + Type *Tys[3] = { CI.getArgOperand(0)->getType(), + CI.getArgOperand(1)->getType(), + CI.getArgOperand(2)->getType() }; + CI.setCalledFunction( + Intrinsic::getOrInsertDeclaration(M, MemCpyID, Tys)); + return II; + } + } } // For fixed width vector result intrinsics, use the generic demanded vector