[SimplifyLibCalls] Reduce code duplication. NFC

Reviewed By: nikic, nickdesaulniers, xbolva00

Differential Revision: https://reviews.llvm.org/D135073
This commit is contained in:
Alex Richardson 2022-10-03 15:43:50 +00:00
parent 11adae5089
commit 3890a456d8
1 changed files with 18 additions and 37 deletions

View File

@ -287,6 +287,12 @@ static Value *copyFlags(const CallInst &Old, Value *New) {
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 @@ Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
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 @@ Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
// 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 @@ Value *LibCallSimplifier::optimizeStrLCpy(CallInst *CI, IRBuilderBase &B) {
// 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 @@ Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
// 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 @@ Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) {
// 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 @@ Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
// 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 @@ Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
// 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 @@ Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
// 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 @@ Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
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 @@ Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
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 *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
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 @@ Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
if (isFortifiedCallFoldable(CI, 3, 2))
if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
CI->getArgOperand(2), B, DL, TLI)) {
CallInst *NewCI = cast<CallInst>(Call);
NewCI->setAttributes(CI->getAttributes());
NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
return copyFlags(*CI, NewCI);
return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
}
return nullptr;
}