diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -287,6 +287,12 @@ return New; } +static Value *mergeAttributesAndFlags(CallInst *NewCI, const CallInst &Old) { + NewCI->setAttributes(Old.getAttributes()); + NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); + return copyFlags(Old, NewCI); +} + // Helper to avoid truncating the length if size_t is 32-bits. static StringRef substr(StringRef Str, uint64_t Len) { return Len >= Str.size() ? Str : Str.substr(0, Len); @@ -654,9 +660,7 @@ CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return Dst; } @@ -688,9 +692,7 @@ // We have enough information to now generate the memcpy call to do the // copy for us. Make a memcpy to copy the nul byte with align = 1. CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return DstEnd; } @@ -759,9 +761,7 @@ // D[N' - 1] if necessary. CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), ConstantInt::get(DL.getIntPtrType(PT), NBytes)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); if (!NulTerm) { Value *EndOff = ConstantInt::get(CI->getType(), NBytes); @@ -860,9 +860,7 @@ // S and N are constant. CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), ConstantInt::get(DL.getIntPtrType(PT), N)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); if (!RetEnd) return Dst; @@ -1557,9 +1555,7 @@ // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n) CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), Align(1), Size); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return CI->getArgOperand(0); } @@ -1613,9 +1609,7 @@ // Propagate attributes, but memcpy has no return value, so make sure that // any return attributes are compliant. // TODO: Attach return value attributes to the 1st operand to preserve them? - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N); } @@ -1628,9 +1622,7 @@ // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n) CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), Align(1), Size); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return CI->getArgOperand(0); } @@ -1643,9 +1635,7 @@ // memset(p, v, n) -> llvm.memset(align 1 p, v, n) Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return CI->getArgOperand(0); } @@ -3791,9 +3781,7 @@ CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), Align(1), CI->getArgOperand(2)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return CI->getArgOperand(0); } return nullptr; @@ -3805,9 +3793,7 @@ CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), Align(1), CI->getArgOperand(2)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return CI->getArgOperand(0); } return nullptr; @@ -3819,9 +3805,7 @@ Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), Align(1)); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - copyFlags(*CI, NewCI); + mergeAttributesAndFlags(NewCI, *CI); return CI->getArgOperand(0); } return nullptr; @@ -3833,10 +3817,7 @@ if (isFortifiedCallFoldable(CI, 3, 2)) if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, DL, TLI)) { - CallInst *NewCI = cast(Call); - NewCI->setAttributes(CI->getAttributes()); - NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType())); - return copyFlags(*CI, NewCI); + return mergeAttributesAndFlags(cast(Call), *CI); } return nullptr; }