diff options
author | Meador Inge <meadori@codesourcery.com> | 2012-10-31 04:29:58 +0000 |
---|---|---|
committer | Meador Inge <meadori@codesourcery.com> | 2012-10-31 04:29:58 +0000 |
commit | 08684d1f069711692502d091669a6031c31cdd4a (patch) | |
tree | 7402252dfa81b08d1ba144a9d8a10b57e0396ad9 /lib/Transforms/Utils/SimplifyLibCalls.cpp | |
parent | c5c970ee852e345ded08cdfc33c4621ca76211b1 (diff) | |
download | llvm-08684d1f069711692502d091669a6031c31cdd4a.tar.gz llvm-08684d1f069711692502d091669a6031c31cdd4a.tar.bz2 llvm-08684d1f069711692502d091669a6031c31cdd4a.tar.xz |
instcombine: Migrate strpbrk optimizations
This patch migrates the strpbrk optimizations from the simplify-libcalls
pass into the instcombine library call simplifier.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167105 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/SimplifyLibCalls.cpp')
-rw-r--r-- | lib/Transforms/Utils/SimplifyLibCalls.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index 658e7c3428..1aac8c5745 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -717,6 +717,41 @@ struct StrLenOpt : public LibCallOptimization { } }; +struct StrPBrkOpt : public LibCallOptimization { + virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { + FunctionType *FT = Callee->getFunctionType(); + if (FT->getNumParams() != 2 || + FT->getParamType(0) != B.getInt8PtrTy() || + FT->getParamType(1) != FT->getParamType(0) || + FT->getReturnType() != FT->getParamType(0)) + return 0; + + StringRef S1, S2; + bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); + bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); + + // strpbrk(s, "") -> NULL + // strpbrk("", s) -> NULL + if ((HasS1 && S1.empty()) || (HasS2 && S2.empty())) + return Constant::getNullValue(CI->getType()); + + // Constant folding. + if (HasS1 && HasS2) { + size_t I = S1.find_first_of(S2); + if (I == std::string::npos) // No match. + return Constant::getNullValue(CI->getType()); + + return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk"); + } + + // strpbrk(s, "a") -> strchr(s, 'a') + if (TD && HasS2 && S2.size() == 1) + return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI); + + return 0; + } +}; + } // End anonymous namespace. namespace llvm { @@ -745,6 +780,7 @@ class LibCallSimplifierImpl { StpCpyOpt StpCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen; + StrPBrkOpt StrPBrk; void initOptimizations(); public: @@ -777,6 +813,7 @@ void LibCallSimplifierImpl::initOptimizations() { Optimizations["stpcpy"] = &StpCpy; Optimizations["strncpy"] = &StrNCpy; Optimizations["strlen"] = &StrLen; + Optimizations["strpbrk"] = &StrPBrk; } Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) { |