-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AggressiveInstCombine] Expand memchr with small constant strings #98501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4361417
[AggressiveInstCombine] Add pre-commit tests. NFC.
dtcxzyw 88a959a
[AggressiveInstCombine] Expand strchr/memchr with small constant strings
dtcxzyw 3ddb24f
[AggressiveInstCombine] Address review comments.
dtcxzyw 31a7d82
[AggressiveInstCombine] Address review comments.
dtcxzyw 57f5945
Merge branch 'main' into perf/expand-strchr
dtcxzyw 42af6e6
[AggressiveInstCombine] Address review comments. NFC.
dtcxzyw e2de353
[AggressiveInstCombine] Fix test. NFC.
dtcxzyw 7e8fe26
[AggressiveInstCombine] Address review comments.
dtcxzyw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt -S -passes=aggressive-instcombine --memchr-inline-threshold=5 < %s | FileCheck %s | ||
|
||
@str = constant [5 x i8] c"01\002\00", align 1 | ||
@str_long = constant [8 x i8] c"0123456\00", align 1 | ||
|
||
declare ptr @memchr(ptr, i32, i64) | ||
|
||
define i1 @test_memchr_null(i32 %x) { | ||
; CHECK-LABEL: define i1 @test_memchr_null( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*]]: | ||
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X]] to i8 | ||
; CHECK-NEXT: switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [ | ||
; CHECK-NEXT: i8 48, label %[[MEMCHR_CASE:.*]] | ||
; CHECK-NEXT: i8 49, label %[[MEMCHR_CASE1:.*]] | ||
; CHECK-NEXT: i8 0, label %[[MEMCHR_CASE2:.*]] | ||
; CHECK-NEXT: i8 50, label %[[MEMCHR_CASE3:.*]] | ||
; CHECK-NEXT: ] | ||
; CHECK: [[MEMCHR_CASE]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS:.*]] | ||
; CHECK: [[MEMCHR_CASE1]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_CASE2]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_CASE3]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_SUCCESS]]: | ||
; CHECK-NEXT: [[MEMCHR_IDX:%.*]] = phi i64 [ 0, %[[MEMCHR_CASE]] ], [ 1, %[[MEMCHR_CASE1]] ], [ 2, %[[MEMCHR_CASE2]] ], [ 3, %[[MEMCHR_CASE3]] ] | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @str, i64 [[MEMCHR_IDX]] | ||
; CHECK-NEXT: br label %[[ENTRY_SPLIT]] | ||
; CHECK: [[ENTRY_SPLIT]]: | ||
; CHECK-NEXT: [[MEMCHR4:%.*]] = phi ptr [ null, %[[ENTRY]] ], [ [[TMP1]], %[[MEMCHR_SUCCESS]] ] | ||
; CHECK-NEXT: [[ISNULL:%.*]] = icmp eq ptr [[MEMCHR4]], null | ||
; CHECK-NEXT: ret i1 [[ISNULL]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str, i32 %x, i64 5) | ||
%isnull = icmp eq ptr %memchr, null | ||
ret i1 %isnull | ||
} | ||
|
||
define ptr @test_memchr(i32 %x) { | ||
; CHECK-LABEL: define ptr @test_memchr( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*]]: | ||
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X]] to i8 | ||
; CHECK-NEXT: switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [ | ||
; CHECK-NEXT: i8 48, label %[[MEMCHR_CASE:.*]] | ||
; CHECK-NEXT: i8 49, label %[[MEMCHR_CASE1:.*]] | ||
; CHECK-NEXT: i8 0, label %[[MEMCHR_CASE2:.*]] | ||
; CHECK-NEXT: i8 50, label %[[MEMCHR_CASE3:.*]] | ||
; CHECK-NEXT: ] | ||
; CHECK: [[MEMCHR_CASE]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS:.*]] | ||
; CHECK: [[MEMCHR_CASE1]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_CASE2]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_CASE3]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_SUCCESS]]: | ||
; CHECK-NEXT: [[MEMCHR_IDX:%.*]] = phi i64 [ 0, %[[MEMCHR_CASE]] ], [ 1, %[[MEMCHR_CASE1]] ], [ 2, %[[MEMCHR_CASE2]] ], [ 3, %[[MEMCHR_CASE3]] ] | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @str, i64 [[MEMCHR_IDX]] | ||
; CHECK-NEXT: br label %[[ENTRY_SPLIT]] | ||
; CHECK: [[ENTRY_SPLIT]]: | ||
; CHECK-NEXT: [[MEMCHR4:%.*]] = phi ptr [ null, %[[ENTRY]] ], [ [[TMP1]], %[[MEMCHR_SUCCESS]] ] | ||
; CHECK-NEXT: ret ptr [[MEMCHR4]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str, i32 %x, i64 5) | ||
ret ptr %memchr | ||
} | ||
|
||
define ptr @test_memchr_smaller_n(i32 %x) { | ||
; CHECK-LABEL: define ptr @test_memchr_smaller_n( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*]]: | ||
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X]] to i8 | ||
; CHECK-NEXT: switch i8 [[TMP0]], label %[[ENTRY_SPLIT:.*]] [ | ||
; CHECK-NEXT: i8 48, label %[[MEMCHR_CASE:.*]] | ||
; CHECK-NEXT: i8 49, label %[[MEMCHR_CASE1:.*]] | ||
; CHECK-NEXT: i8 0, label %[[MEMCHR_CASE2:.*]] | ||
; CHECK-NEXT: ] | ||
; CHECK: [[MEMCHR_CASE]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS:.*]] | ||
; CHECK: [[MEMCHR_CASE1]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_CASE2]]: | ||
; CHECK-NEXT: br label %[[MEMCHR_SUCCESS]] | ||
; CHECK: [[MEMCHR_SUCCESS]]: | ||
; CHECK-NEXT: [[MEMCHR_IDX:%.*]] = phi i64 [ 0, %[[MEMCHR_CASE]] ], [ 1, %[[MEMCHR_CASE1]] ], [ 2, %[[MEMCHR_CASE2]] ] | ||
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @str, i64 [[MEMCHR_IDX]] | ||
; CHECK-NEXT: br label %[[ENTRY_SPLIT]] | ||
; CHECK: [[ENTRY_SPLIT]]: | ||
; CHECK-NEXT: [[MEMCHR3:%.*]] = phi ptr [ null, %[[ENTRY]] ], [ [[TMP1]], %[[MEMCHR_SUCCESS]] ] | ||
; CHECK-NEXT: ret ptr [[MEMCHR3]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str, i32 %x, i64 3) | ||
ret ptr %memchr | ||
} | ||
|
||
; negative tests | ||
|
||
define ptr @test_memchr_larger_n(i32 %x) { | ||
; CHECK-LABEL: define ptr @test_memchr_larger_n( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 [[X]], i64 6) | ||
; CHECK-NEXT: ret ptr [[MEMCHR]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str, i32 %x, i64 6) | ||
ret ptr %memchr | ||
} | ||
|
||
define ptr @test_memchr_non_constant(i32 %x, ptr %str) { | ||
; CHECK-LABEL: define ptr @test_memchr_non_constant( | ||
; CHECK-SAME: i32 [[X:%.*]], ptr [[STR:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr [[STR]], i32 [[X]], i64 5) | ||
; CHECK-NEXT: ret ptr [[MEMCHR]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr %str, i32 %x, i64 5) | ||
ret ptr %memchr | ||
} | ||
|
||
define ptr @test_memchr_constant_ch() { | ||
; CHECK-LABEL: define ptr @test_memchr_constant_ch() { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 49, i64 5) | ||
; CHECK-NEXT: ret ptr [[MEMCHR]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str, i32 49, i64 5) | ||
ret ptr %memchr | ||
} | ||
|
||
define ptr @test_memchr_dynamic_n(i32 %x, i32 %y) { | ||
; CHECK-LABEL: define ptr @test_memchr_dynamic_n( | ||
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str, i32 [[X]], i32 [[Y]]) | ||
; CHECK-NEXT: ret ptr [[MEMCHR]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str, i32 %x, i32 %y) | ||
ret ptr %memchr | ||
} | ||
|
||
define ptr @test_memchr_long(i32 %x) { | ||
; CHECK-LABEL: define ptr @test_memchr_long( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[MEMCHR:%.*]] = call ptr @memchr(ptr @str_long, i32 [[X]], i64 8) | ||
; CHECK-NEXT: ret ptr [[MEMCHR]] | ||
; | ||
entry: | ||
%memchr = call ptr @memchr(ptr @str_long, i32 %x, i64 8) | ||
ret ptr %memchr | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.