From 3104a6bad05b9b1432b7afd5c31e1318bd8e1814 Mon Sep 17 00:00:00 2001 From: Andrew Kaylor Date: Tue, 26 Jul 2016 17:23:13 +0000 Subject: [PATCH] Re-committing r275284: add support to inline __builtin_mempcpy Patch by Sunita Marathe Differential Revision: http://reviews.llvm.org/D21920 llvm-svn: 276771 --- llvm/include/llvm/Analysis/TargetLibraryInfo.def | 3 ++ llvm/include/llvm/Analysis/TargetLibraryInfo.h | 2 +- llvm/lib/Analysis/TargetLibraryInfo.cpp | 1 + .../CodeGen/SelectionDAG/SelectionDAGBuilder.cpp | 47 ++++++++++++++++++++++ .../lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h | 1 + llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 1 + .../test/Transforms/InferFunctionAttrs/annotate.ll | 3 ++ .../test/Transforms/InferFunctionAttrs/no-proto.ll | 3 ++ 8 files changed, 60 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.def b/llvm/include/llvm/Analysis/TargetLibraryInfo.def index b2a593d..5d5e5b1 100644 --- a/llvm/include/llvm/Analysis/TargetLibraryInfo.def +++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.def @@ -734,6 +734,9 @@ TLI_DEFINE_STRING_INTERNAL("memcpy") /// void *memmove(void *s1, const void *s2, size_t n); TLI_DEFINE_ENUM_INTERNAL(memmove) TLI_DEFINE_STRING_INTERNAL("memmove") +/// void *mempcpy(void *s1, const void *s2, size_t n); +TLI_DEFINE_ENUM_INTERNAL(mempcpy) +TLI_DEFINE_STRING_INTERNAL("mempcpy") // void *memrchr(const void *s, int c, size_t n); TLI_DEFINE_ENUM_INTERNAL(memrchr) TLI_DEFINE_STRING_INTERNAL("memrchr") diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.h b/llvm/include/llvm/Analysis/TargetLibraryInfo.h index 7efa6f0..411dd74 100644 --- a/llvm/include/llvm/Analysis/TargetLibraryInfo.h +++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.h @@ -251,7 +251,7 @@ public: case LibFunc::exp2: case LibFunc::exp2f: case LibFunc::exp2l: case LibFunc::memcmp: case LibFunc::strcmp: case LibFunc::strcpy: case LibFunc::stpcpy: case LibFunc::strlen: case LibFunc::strnlen: - case LibFunc::memchr: + case LibFunc::memchr: case LibFunc::mempcpy: return true; } return false; diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp index 93d537a..cb02faf 100644 --- a/llvm/lib/Analysis/TargetLibraryInfo.cpp +++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp @@ -642,6 +642,7 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, return false; // fallthrough case LibFunc::memcpy: + case LibFunc::mempcpy: case LibFunc::memmove: return (NumParams == 3 && FTy.getReturnType() == FTy.getParamType(0) && FTy.getParamType(0)->isPointerTy() && diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index e03282c..af86d87 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -6044,6 +6044,49 @@ bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) { return false; } +/// +/// visitMemPCpyCall -- lower a mempcpy call as a memcpy followed by code to +/// to adjust the dst pointer by the size of the copied memory. +bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) { + + // Verify argument count: void *mempcpy(void *, const void *, size_t) + if (I.getNumArgOperands() != 3) + return false; + + SDValue Dst = getValue(I.getArgOperand(0)); + SDValue Src = getValue(I.getArgOperand(1)); + SDValue Size = getValue(I.getArgOperand(2)); + + unsigned DstAlign = DAG.InferPtrAlignment(Dst); + unsigned SrcAlign = DAG.InferPtrAlignment(Src); + unsigned Align = std::min(DstAlign, SrcAlign); + if (Align == 0) // Alignment of one or both could not be inferred. + Align = 1; // 0 and 1 both specify no alignment, but 0 is reserved. + + bool isVol = false; + SDLoc sdl = getCurSDLoc(); + + // In the mempcpy context we need to pass in a false value for isTailCall + // because the return pointer needs to be adjusted by the size of + // the copied memory. + SDValue MC = DAG.getMemcpy(getRoot(), sdl, Dst, Src, Size, Align, isVol, + false, /*isTailCall=*/false, + MachinePointerInfo(I.getArgOperand(0)), + MachinePointerInfo(I.getArgOperand(1))); + assert(MC.getNode() != nullptr && + "** memcpy should not be lowered as TailCall in mempcpy context **"); + DAG.setRoot(MC); + + // Check if Size needs to be truncated or extended. + Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType()); + + // Adjust return pointer to point just past the last dst byte. + SDValue DstPlusSize = DAG.getNode(ISD::ADD, sdl, Dst.getValueType(), + Dst, Size); + setValue(&I, DstPlusSize); + return true; +} + /// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an /// optimized form. If so, return true and lower it, otherwise return false /// and it will be lowered like a normal call. @@ -6334,6 +6377,10 @@ void SelectionDAGBuilder::visitCall(const CallInst &I) { if (visitMemCmpCall(I)) return; break; + case LibFunc::mempcpy: + if (visitMemPCpyCall(I)) + return; + break; case LibFunc::memchr: if (visitMemChrCall(I)) return; diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h index b9888ae..18c39d0 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h @@ -885,6 +885,7 @@ private: void visitPHI(const PHINode &I); void visitCall(const CallInst &I); bool visitMemCmpCall(const CallInst &I); + bool visitMemPCpyCall(const CallInst &I); bool visitMemChrCall(const CallInst &I); bool visitStrCpyCall(const CallInst &I, bool isStpcpy); bool visitStrCmpCall(const CallInst &I); diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index f4260a9..e61b04fb 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -250,6 +250,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { Changed |= setDoesNotCapture(F, 2); return Changed; case LibFunc::memcpy: + case LibFunc::mempcpy: case LibFunc::memccpy: case LibFunc::memmove: Changed |= setDoesNotThrow(F); diff --git a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll index 039114d..64676bf 100644 --- a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll +++ b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll @@ -499,6 +499,9 @@ declare i32 @memcmp(i8*, i8*, i64) ; CHECK: declare i8* @memcpy(i8*, i8* nocapture readonly, i64) [[G0]] declare i8* @memcpy(i8*, i8*, i64) +; CHECK: declare i8* @mempcpy(i8*, i8* nocapture readonly, i64) [[G0]] +declare i8* @mempcpy(i8*, i8*, i64) + ; CHECK: declare i8* @memmove(i8*, i8* nocapture readonly, i64) [[G0]] declare i8* @memmove(i8*, i8*, i64) diff --git a/llvm/test/Transforms/InferFunctionAttrs/no-proto.ll b/llvm/test/Transforms/InferFunctionAttrs/no-proto.ll index 256f5c3..25a4805 100644 --- a/llvm/test/Transforms/InferFunctionAttrs/no-proto.ll +++ b/llvm/test/Transforms/InferFunctionAttrs/no-proto.ll @@ -480,6 +480,9 @@ declare void @memcmp(...) ; CHECK: declare void @memcpy(...) declare void @memcpy(...) +; CHECK: declare void @mempcpy(...) +declare void @mempcpy(...) + ; CHECK: declare void @memmove(...) declare void @memmove(...) -- 2.7.4