[llvm-commits] [llvm] r115111 - in /llvm/trunk: lib/Transforms/Scalar/SimplifyLibCalls.cpp test/Transforms/SimplifyLibCalls/StrPBrk.ll
Benjamin Kramer
benny.kra at googlemail.com
Wed Sep 29 16:52:12 PDT 2010
Author: d0k
Date: Wed Sep 29 18:52:12 2010
New Revision: 115111
URL: http://llvm.org/viewvc/llvm-project?rev=115111&view=rev
Log:
Add strpbrk folding to SimplifyLibCalls.
Added:
llvm/trunk/test/Transforms/SimplifyLibCalls/StrPBrk.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp?rev=115111&r1=115110&r2=115111&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp Wed Sep 29 18:52:12 2010
@@ -522,6 +522,46 @@
}
};
+
+//===---------------------------------------===//
+// 'strpbrk' Optimizations
+
+struct StrPBrkOpt : public LibCallOptimization {
+ virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+ const FunctionType *FT = Callee->getFunctionType();
+ if (FT->getNumParams() != 2 ||
+ FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+ FT->getParamType(1) != FT->getParamType(0) ||
+ FT->getReturnType() != FT->getParamType(0))
+ return 0;
+
+ std::string 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());
+
+ Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
+ return B.CreateGEP(CI->getArgOperand(0), Idx, "strpbrk");
+ }
+
+ // strpbrk(s, "a") -> strchr(s, 'a')
+ if (TD && HasS2 && S2.size() == 1)
+ return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
+
+ return 0;
+ }
+};
+
//===---------------------------------------===//
// 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
@@ -1256,7 +1296,7 @@
// String and Memory LibCall Optimizations
StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
- StrNCpyOpt StrNCpy; StrLenOpt StrLen;
+ StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
StrToOpt StrTo; StrStrOpt StrStr;
MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
// Math Library Optimizations
@@ -1309,6 +1349,7 @@
Optimizations["strcpy"] = &StrCpy;
Optimizations["strncpy"] = &StrNCpy;
Optimizations["strlen"] = &StrLen;
+ Optimizations["strpbrk"] = &StrPBrk;
Optimizations["strtol"] = &StrTo;
Optimizations["strtod"] = &StrTo;
Optimizations["strtof"] = &StrTo;
@@ -2209,12 +2250,6 @@
// * stpcpy(str, "literal") ->
// llvm.memcpy(str,"literal",strlen("literal")+1,1)
//
-// strpbrk:
-// * strpbrk(s,a) -> offset_in_for(s,a)
-// (if s and a are both constant strings)
-// * strpbrk(s,"") -> 0
-// * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
-//
// strspn, strcspn:
// * strspn(s,a) -> const_int (if both args are constant)
// * strspn("",a) -> 0
Added: llvm/trunk/test/Transforms/SimplifyLibCalls/StrPBrk.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyLibCalls/StrPBrk.ll?rev=115111&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyLibCalls/StrPBrk.ll (added)
+++ llvm/trunk/test/Transforms/SimplifyLibCalls/StrPBrk.ll Wed Sep 29 18:52:12 2010
@@ -0,0 +1,25 @@
+; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
+
+target datalayout = "-p:64:64:64"
+
+ at hello = constant [12 x i8] c"hello world\00"
+ at w = constant [2 x i8] c"w\00"
+ at null = constant [1 x i8] zeroinitializer
+
+declare i8* @strpbrk(i8*, i8*)
+
+define void @test(i8* %s1, i8* %s2) {
+ %hello_p = getelementptr [12 x i8]* @hello, i32 0, i32 0
+ %w_p = getelementptr [2 x i8]* @w, i32 0, i32 0
+ %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
+ %test1 = call i8* @strpbrk(i8* %null_p, i8* %s2)
+ %test2 = call i8* @strpbrk(i8* %s1, i8* %null_p)
+; CHECK-NOT: call i8* @strpbrk
+ %test3 = call i8* @strpbrk(i8* %s1, i8* %w_p)
+; CHECK: call i8* @strchr(i8* %s1, i32 119)
+ %test4 = call i8* @strpbrk(i8* %hello_p, i8* %w_p)
+; CHECK: getelementptr i8* %hello_p, i64 6
+ %test5 = call i8* @strpbrk(i8* %s1, i8* %s2)
+; CHECK: call i8* @strpbrk(i8* %s1, i8* %s2)
+ ret void
+}
More information about the llvm-commits
mailing list