diff options
author | Meador Inge <meadori@codesourcery.com> | 2012-10-31 03:33:00 +0000 |
---|---|---|
committer | Meador Inge <meadori@codesourcery.com> | 2012-10-31 03:33:00 +0000 |
commit | a0885fb8825ed362041b5cf291a007a9f9301ff8 (patch) | |
tree | 43c9bc8a05a51d1c756183727de3de7a5f0d1a65 /lib/Transforms/Utils/SimplifyLibCalls.cpp | |
parent | 5b2c4dc5f8f70352f029d595797089821eb39b3c (diff) |
instcombine: Migrate strncpy optimizations
This patch migrates the strncpy optimizations from the simplify-libcalls
pass into the instcombine library call simplifier.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167102 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/SimplifyLibCalls.cpp')
-rw-r--r-- | lib/Transforms/Utils/SimplifyLibCalls.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index bc360010de..cc03573326 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -628,6 +628,53 @@ struct StpCpyOpt: public LibCallOptimization { } }; +struct StrNCpyOpt : public LibCallOptimization { + virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { + FunctionType *FT = Callee->getFunctionType(); + if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) || + FT->getParamType(0) != FT->getParamType(1) || + FT->getParamType(0) != B.getInt8PtrTy() || + !FT->getParamType(2)->isIntegerTy()) + return 0; + + Value *Dst = CI->getArgOperand(0); + Value *Src = CI->getArgOperand(1); + Value *LenOp = CI->getArgOperand(2); + + // See if we can get the length of the input string. + uint64_t SrcLen = GetStringLength(Src); + if (SrcLen == 0) return 0; + --SrcLen; + + if (SrcLen == 0) { + // strncpy(x, "", y) -> memset(x, '\0', y, 1) + B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1); + return Dst; + } + + uint64_t Len; + if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp)) + Len = LengthArg->getZExtValue(); + else + return 0; + + if (Len == 0) return Dst; // strncpy(x, y, 0) -> x + + // These optimizations require DataLayout. + if (!TD) return 0; + + // Let strncpy handle the zero padding + if (Len > SrcLen+1) return 0; + + Type *PT = FT->getParamType(0); + // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant] + B.CreateMemCpy(Dst, Src, + ConstantInt::get(TD->getIntPtrType(PT), Len), 1); + + return Dst; + } +}; + } // End anonymous namespace. namespace llvm { @@ -654,6 +701,7 @@ class LibCallSimplifierImpl { StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StpCpyOpt StpCpy; + StrNCpyOpt StrNCpy; void initOptimizations(); public: @@ -684,6 +732,7 @@ void LibCallSimplifierImpl::initOptimizations() { Optimizations["strncmp"] = &StrNCmp; Optimizations["strcpy"] = &StrCpy; Optimizations["stpcpy"] = &StpCpy; + Optimizations["strncpy"] = &StrNCpy; } Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) { |