[llvm-commits] [llvm] r115095 - in /llvm/trunk: lib/Transforms/Scalar/SimplifyLibCalls.cpp test/Transforms/SimplifyLibCalls/StrChr.ll

Benjamin Kramer benny.kra at googlemail.com
Wed Sep 29 15:29:12 PDT 2010


Author: d0k
Date: Wed Sep 29 17:29:12 2010
New Revision: 115095

URL: http://llvm.org/viewvc/llvm-project?rev=115095&view=rev
Log:
Simplify the loop in StrChrOptimizer. FileCheckize test.

Modified:
    llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/SimplifyLibCalls/StrChr.ll

Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp?rev=115095&r1=115094&r2=115095&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp Wed Sep 29 17:29:12 2010
@@ -252,21 +252,14 @@
 
     // strchr can find the nul character.
     Str += '\0';
-    char CharValue = CharC->getSExtValue();
 
     // Compute the offset.
-    uint64_t i = 0;
-    while (1) {
-      if (i == Str.size())    // Didn't find the char.  strchr returns null.
-        return Constant::getNullValue(CI->getType());
-      // Did we find our match?
-      if (Str[i] == CharValue)
-        break;
-      ++i;
-    }
+    size_t I = Str.find(CharC->getSExtValue());
+    if (I == std::string::npos) // Didn't find the char.  strchr returns null.
+      return Constant::getNullValue(CI->getType());
 
     // strchr(s+n,c)  -> gep(s+n+i,c)
-    Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
+    Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
     return B.CreateGEP(SrcStr, Idx, "strchr");
   }
 };

Modified: llvm/trunk/test/Transforms/SimplifyLibCalls/StrChr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyLibCalls/StrChr.ll?rev=115095&r1=115094&r2=115095&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyLibCalls/StrChr.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyLibCalls/StrChr.ll Wed Sep 29 17:29:12 2010
@@ -1,26 +1,26 @@
 ; Test that the StrChrOptimizer works correctly
-; RUN: opt < %s -simplify-libcalls -S | \
-; RUN:   not grep {call.*@strchr}
+; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
 
 ; This transformation requires the pointer size, as it assumes that size_t is
 ; the size of a pointer.
 target datalayout = "-p:64:64:64"
 
- at hello = constant [14 x i8] c"hello world\5Cn\00"		; <[14 x i8]*> [#uses=1]
- at null = constant [1 x i8] zeroinitializer		; <[1 x i8]*> [#uses=1]
+ at hello = constant [14 x i8] c"hello world\5Cn\00"
+ at null = constant [1 x i8] zeroinitializer
 
 declare i8* @strchr(i8*, i32)
 
-declare i32 @puts(i8*)
-
-define i32 @main() {
-	%hello_p = getelementptr [14 x i8]* @hello, i32 0, i32 0		; <i8*> [#uses=2]
-	%null_p = getelementptr [1 x i8]* @null, i32 0, i32 0		; <i8*> [#uses=1]
-	%world = call i8* @strchr( i8* %hello_p, i32 119 )		; <i8*> [#uses=1]
-	%ignore = call i8* @strchr( i8* %null_p, i32 119 )		; <i8*> [#uses=0]
-	%len = call i32 @puts( i8* %world )		; <i32> [#uses=1]
-	%index = add i32 %len, 112		; <i32> [#uses=2]
-	%result = call i8* @strchr( i8* %hello_p, i32 %index )		; <i8*> [#uses=0]
+define i32 @foo(i32 %index) {
+	%hello_p = getelementptr [14 x i8]* @hello, i32 0, i32 0
+	%null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
+	%world = call i8* @strchr(i8* %hello_p, i32 119)
+; CHECK: getelementptr i8* %hello_p, i64 6
+	%ignore = call i8* @strchr(i8* %null_p, i32 119)
+; CHECK-NOT: call i8* strchr
+	%null = call i8* @strchr(i8* %hello_p, i32 0)
+; CHECK: getelementptr i8* %hello_p, i64 13
+	%result = call i8* @strchr(i8* %hello_p, i32 %index)
+; CHECK: call i8* @memchr(i8* %hello_p, i32 %index, i64 14)
 	ret i32 %index
 }
 





More information about the llvm-commits mailing list