Simplify the loop in StrChrOptimizer. FileCheckize test.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@115095 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer
2010-09-29 22:29:12 +00:00
parent 8cf6c60710
commit e260990fa1
2 changed files with 18 additions and 25 deletions

View File

@ -252,21 +252,14 @@ struct StrChrOpt : public LibCallOptimization {
// strchr can find the nul character. // strchr can find the nul character.
Str += '\0'; Str += '\0';
char CharValue = CharC->getSExtValue();
// Compute the offset. // Compute the offset.
uint64_t i = 0; size_t I = Str.find(CharC->getSExtValue());
while (1) { if (I == std::string::npos) // Didn't find the char. strchr returns null.
if (i == Str.size()) // Didn't find the char. strchr returns null. return Constant::getNullValue(CI->getType());
return Constant::getNullValue(CI->getType());
// Did we find our match?
if (Str[i] == CharValue)
break;
++i;
}
// strchr(s+n,c) -> gep(s+n+i,c) // 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"); return B.CreateGEP(SrcStr, Idx, "strchr");
} }
}; };

View File

@ -1,26 +1,26 @@
; Test that the StrChrOptimizer works correctly ; Test that the StrChrOptimizer works correctly
; RUN: opt < %s -simplify-libcalls -S | \ ; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
; RUN: not grep {call.*@strchr}
; This transformation requires the pointer size, as it assumes that size_t is ; This transformation requires the pointer size, as it assumes that size_t is
; the size of a pointer. ; the size of a pointer.
target datalayout = "-p:64:64:64" target datalayout = "-p:64:64:64"
@hello = constant [14 x i8] c"hello world\5Cn\00" ; <[14 x i8]*> [#uses=1] @hello = constant [14 x i8] c"hello world\5Cn\00"
@null = constant [1 x i8] zeroinitializer ; <[1 x i8]*> [#uses=1] @null = constant [1 x i8] zeroinitializer
declare i8* @strchr(i8*, i32) declare i8* @strchr(i8*, i32)
declare i32 @puts(i8*) define i32 @foo(i32 %index) {
%hello_p = getelementptr [14 x i8]* @hello, i32 0, i32 0
define i32 @main() { %null_p = getelementptr [1 x i8]* @null, i32 0, i32 0
%hello_p = getelementptr [14 x i8]* @hello, i32 0, i32 0 ; <i8*> [#uses=2] %world = call i8* @strchr(i8* %hello_p, i32 119)
%null_p = getelementptr [1 x i8]* @null, i32 0, i32 0 ; <i8*> [#uses=1] ; CHECK: getelementptr i8* %hello_p, i64 6
%world = call i8* @strchr( i8* %hello_p, i32 119 ) ; <i8*> [#uses=1] %ignore = call i8* @strchr(i8* %null_p, i32 119)
%ignore = call i8* @strchr( i8* %null_p, i32 119 ) ; <i8*> [#uses=0] ; CHECK-NOT: call i8* strchr
%len = call i32 @puts( i8* %world ) ; <i32> [#uses=1] %null = call i8* @strchr(i8* %hello_p, i32 0)
%index = add i32 %len, 112 ; <i32> [#uses=2] ; CHECK: getelementptr i8* %hello_p, i64 13
%result = call i8* @strchr( i8* %hello_p, i32 %index ) ; <i8*> [#uses=0] %result = call i8* @strchr(i8* %hello_p, i32 %index)
; CHECK: call i8* @memchr(i8* %hello_p, i32 %index, i64 14)
ret i32 %index ret i32 %index
} }