mirror of
https://github.com/RPCSX/llvm.git
synced 2025-01-18 18:32:32 +00:00
[SimplifyLibCalls] Turn memchr(const, C, const) into a bitfield check.
strchr("123!", C) != nullptr is a common pattern to check if C is one of 1, 2, 3 or !. If the largest element of the string is smaller than the target's register size we can easily create a bitfield and just do a simple test for set membership. int foo(char C) { return strchr("123!", C) != nullptr; } now becomes cmpl $64, %edi ## range check sbbb %al, %al movabsq $0xE000200000001, %rcx btq %rdi, %rcx ## bit test sbbb %cl, %cl andb %al, %cl ## and the two conditions andb $1, %cl movzbl %cl, %eax ## returning an int ret (imho the backend should expand this into a series of branches, but that's a different story) The code is currently limited to bit fields that fit in a register, so usually 64 or 32 bits. Sadly, this misses anything using alpha chars or {}. This could be fixed by just emitting a i128 bit field, but that can generate really ugly code so we have to find a better way. To some degree this is also recreating switch lowering logic, but we can't simply emit a switch instruction and thus change the CFG within instcombine. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232902 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
2bb644f469
commit
fd48a80e14
@ -1268,7 +1268,8 @@ int foo (void) {
|
||||
..
|
||||
else if (strchr ("<>", *intel_parser.op_string)
|
||||
|
||||
Those should be turned into a switch.
|
||||
Those should be turned into a switch. SimplifyLibCalls only gets the second
|
||||
case.
|
||||
|
||||
//===---------------------------------------------------------------------===//
|
||||
|
||||
|
@ -762,11 +762,9 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) {
|
||||
if (LenC && LenC->isNullValue())
|
||||
return Constant::getNullValue(CI->getType());
|
||||
|
||||
// Check if all arguments are constants. If so, we can constant fold.
|
||||
// From now on we need at least constant length and string.
|
||||
StringRef Str;
|
||||
if (!CharC || !LenC ||
|
||||
!getConstantStringInfo(SrcStr, Str, /*Offset=*/0,
|
||||
/*TrimAtNul=*/false))
|
||||
if (!LenC || !getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
|
||||
return nullptr;
|
||||
|
||||
// Truncate the string to LenC. If Str is smaller than LenC we will still only
|
||||
@ -774,6 +772,54 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) {
|
||||
// return null if we don't find the char.
|
||||
Str = Str.substr(0, LenC->getZExtValue());
|
||||
|
||||
// If the char is variable but the input str and length are not we can turn
|
||||
// this memchr call into a simple bit field test. Of course this only works
|
||||
// when the return value is only checked against null.
|
||||
//
|
||||
// It would be really nice to reuse switch lowering here but we can't change
|
||||
// the CFG at this point.
|
||||
//
|
||||
// memchr("\r\n", C, 2) != nullptr -> (C & ((1 << '\r') | (1 << '\n'))) != 0
|
||||
// after bounds check.
|
||||
if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) {
|
||||
unsigned char Max = *std::max_element(Str.begin(), Str.end());
|
||||
|
||||
// Make sure the bit field we're about to create fits in a register on the
|
||||
// target.
|
||||
// FIXME: On a 64 bit architecture this prevents us from using the
|
||||
// interesting range of alpha ascii chars. We could do better by emitting
|
||||
// two bitfields or shifting the range by 64 if no lower chars are used.
|
||||
if (!DL.fitsInLegalInteger(Max + 1))
|
||||
return nullptr;
|
||||
|
||||
// For the bit field use a power-of-2 type with at least 8 bits to avoid
|
||||
// creating unnecessary illegal types.
|
||||
unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
|
||||
|
||||
// Now build the bit field.
|
||||
APInt Bitfield(Width, 0);
|
||||
for (char C : Str)
|
||||
Bitfield.setBit((unsigned char)C);
|
||||
Value *BitfieldC = B.getInt(Bitfield);
|
||||
|
||||
// First check that the bit field access is within bounds.
|
||||
Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
|
||||
Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
|
||||
"memchr.bounds");
|
||||
|
||||
// Create code that checks if the given bit is set in the field.
|
||||
Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
|
||||
Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
|
||||
|
||||
// Finally merge both checks and cast to pointer type. The inttoptr
|
||||
// implicitly zexts the i1 to intptr type.
|
||||
return B.CreateIntToPtr(B.CreateAnd(Bounds, Bits, "memchr"), CI->getType());
|
||||
}
|
||||
|
||||
// Check if all arguments are constants. If so, we can constant fold.
|
||||
if (!CharC)
|
||||
return nullptr;
|
||||
|
||||
// Compute the offset.
|
||||
size_t I = Str.find(CharC->getSExtValue() & 0xFF);
|
||||
if (I == StringRef::npos) // Didn't find the char. memchr returns null.
|
||||
|
@ -1,11 +1,14 @@
|
||||
; Test that the memchr library call simplifier works correctly.
|
||||
; RUN: opt < %s -instcombine -S | FileCheck %s
|
||||
|
||||
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
|
||||
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
|
||||
|
||||
@hello = constant [14 x i8] c"hello world\5Cn\00"
|
||||
@hellonull = constant [14 x i8] c"hello\00world\5Cn\00"
|
||||
@null = constant [1 x i8] zeroinitializer
|
||||
@newlines = constant [3 x i8] c"\0D\0A\00"
|
||||
@single = constant [2 x i8] c"\1F\00"
|
||||
@spaces = constant [4 x i8] c" \0D\0A\00"
|
||||
@chp = global i8* zeroinitializer
|
||||
|
||||
declare i8* @memchr(i8*, i32, i32)
|
||||
@ -119,3 +122,67 @@ define void @test9() {
|
||||
store i8* %dst, i8** @chp
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @test10() {
|
||||
; CHECK-LABEL: @test10
|
||||
; CHECK: store i8* null, i8** @chp, align 4
|
||||
; CHECK-NOT: call i8* @memchr
|
||||
; CHECK: ret void
|
||||
|
||||
%str = getelementptr [14 x i8], [14 x i8]* @hello, i32 0, i32 0
|
||||
%dst = call i8* @memchr(i8* %str, i32 119, i32 6)
|
||||
store i8* %dst, i8** @chp
|
||||
ret void
|
||||
}
|
||||
|
||||
; Check transformation memchr("\r\n", C, 2) != nullptr -> (C & 9216) != 0
|
||||
define i1 @test11(i32 %C) {
|
||||
; CHECK-LABEL: @test11
|
||||
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
|
||||
; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
|
||||
; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
|
||||
; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9216
|
||||
; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
|
||||
; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
|
||||
; CHECK-NEXT: ret i1 %memchr
|
||||
|
||||
%dst = call i8* @memchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C, i32 2)
|
||||
%cmp = icmp ne i8* %dst, null
|
||||
ret i1 %cmp
|
||||
}
|
||||
|
||||
; No 64 bits here
|
||||
define i1 @test12(i32 %C) {
|
||||
; CHECK-LABEL: @test12
|
||||
; CHECK-NEXT: %dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i32 0, i32 0), i32 %C, i32 3)
|
||||
; CHECK-NEXT: %cmp = icmp ne i8* %dst, null
|
||||
; CHECK-NEXT: ret i1 %cmp
|
||||
|
||||
%dst = call i8* @memchr(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @spaces, i64 0, i64 0), i32 %C, i32 3)
|
||||
%cmp = icmp ne i8* %dst, null
|
||||
ret i1 %cmp
|
||||
}
|
||||
|
||||
define i1 @test13(i32 %C) {
|
||||
; CHECK-LABEL: @test13
|
||||
; CHECK-NEXT: %memchr.bounds = icmp ult i32 %C, 32
|
||||
; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, %C
|
||||
; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], -2147483647
|
||||
; CHECK-NEXT: %memchr.bits = icmp ne i32 [[AND]], 0
|
||||
; CHECK-NEXT: %memchr = and i1 %memchr.bounds, %memchr.bits
|
||||
; CHECK-NEXT: ret i1 %memchr
|
||||
|
||||
%dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 2)
|
||||
%cmp = icmp ne i8* %dst, null
|
||||
ret i1 %cmp
|
||||
}
|
||||
|
||||
define i1 @test14(i32 %C) {
|
||||
; CHECK-LABEL: @test14
|
||||
; CHECK-NEXT: icmp eq i32 %C, 31
|
||||
; CHECK-NEXT: ret
|
||||
|
||||
%dst = call i8* @memchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @single, i64 0, i64 0), i32 %C, i32 1)
|
||||
%cmp = icmp ne i8* %dst, null
|
||||
ret i1 %cmp
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
; Test that the strchr library call simplifier works correctly.
|
||||
; RUN: opt < %s -instcombine -S | FileCheck %s
|
||||
|
||||
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
|
||||
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
|
||||
|
||||
@hello = constant [14 x i8] c"hello world\5Cn\00"
|
||||
@null = constant [1 x i8] zeroinitializer
|
||||
@newlines = constant [3 x i8] c"\0D\0A\00"
|
||||
@chp = global i8* zeroinitializer
|
||||
|
||||
declare i8* @strchr(i8*, i32)
|
||||
@ -76,3 +77,19 @@ define void @test_simplify6(i8* %str) {
|
||||
store i8* %dst, i8** @chp
|
||||
ret void
|
||||
}
|
||||
|
||||
; Check transformation strchr("\r\n", C) != nullptr -> (C & 9217) != 0
|
||||
define i1 @test_simplify7(i32 %C) {
|
||||
; CHECK-LABEL: @test_simplify7
|
||||
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i32 %C to i16
|
||||
; CHECK-NEXT: %memchr.bounds = icmp ult i16 [[TRUNC]], 16
|
||||
; CHECK-NEXT: [[SHL:%.*]] = shl i16 1, [[TRUNC]]
|
||||
; CHECK-NEXT: [[AND:%.*]] = and i16 [[SHL]], 9217
|
||||
; CHECK-NEXT: %memchr.bits = icmp ne i16 [[AND]], 0
|
||||
; CHECK-NEXT: %memchr1 = and i1 %memchr.bounds, %memchr.bits
|
||||
; CHECK-NEXT: ret i1 %memchr1
|
||||
|
||||
%dst = call i8* @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @newlines, i64 0, i64 0), i32 %C)
|
||||
%cmp = icmp ne i8* %dst, null
|
||||
ret i1 %cmp
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user