diff options
author | Meador Inge <meadori@codesourcery.com> | 2012-11-08 01:33:50 +0000 |
---|---|---|
committer | Meador Inge <meadori@codesourcery.com> | 2012-11-08 01:33:50 +0000 |
commit | 7629de3326318e533ab969abd1b0cbc569b3f3b7 (patch) | |
tree | 60efc65c279893e7d010617cb51f0b8771e72143 /lib/Transforms/Utils | |
parent | eb3a8c5288110ddf183e3e8b8babc303f6b78020 (diff) | |
download | external_llvm-7629de3326318e533ab969abd1b0cbc569b3f3b7.zip external_llvm-7629de3326318e533ab969abd1b0cbc569b3f3b7.tar.gz external_llvm-7629de3326318e533ab969abd1b0cbc569b3f3b7.tar.bz2 |
instcombine: Migrate strspn optimizations
This patch migrates the strspn optimizations from the simplify-libcalls
pass into the instcombine library call simplifier.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167568 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils')
-rw-r--r-- | lib/Transforms/Utils/SimplifyLibCalls.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index 581b8d3..64c7011 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -772,6 +772,35 @@ struct StrToOpt : public LibCallOptimization { } }; +struct StrSpnOpt : 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()->isIntegerTy()) + return 0; + + StringRef S1, S2; + bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); + bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); + + // strspn(s, "") -> 0 + // strspn("", s) -> 0 + if ((HasS1 && S1.empty()) || (HasS2 && S2.empty())) + return Constant::getNullValue(CI->getType()); + + // Constant folding. + if (HasS1 && HasS2) { + size_t Pos = S1.find_first_not_of(S2); + if (Pos == StringRef::npos) Pos = S1.size(); + return ConstantInt::get(CI->getType(), Pos); + } + + return 0; + } +}; + } // End anonymous namespace. namespace llvm { @@ -802,6 +831,7 @@ class LibCallSimplifierImpl { StrLenOpt StrLen; StrPBrkOpt StrPBrk; StrToOpt StrTo; + StrSpnOpt StrSpn; void initOptimizations(); public: @@ -842,6 +872,7 @@ void LibCallSimplifierImpl::initOptimizations() { Optimizations["strtoll"] = &StrTo; Optimizations["strtold"] = &StrTo; Optimizations["strtoull"] = &StrTo; + Optimizations["strspn"] = &StrSpn; } Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) { |