-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[InstCombine] recognize missed i128 split optimization #129363
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
RKSimon
merged 8 commits into
llvm:main
from
bassiounix:user/bassiounix/InstCombine-i128-split-optimization
Mar 6, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb3a1f1
[InstCombine] recognize missed i128 split optimization
bassiounix 5e37f32
fix: update pattern constant source
bassiounix 899c8d5
add(InstCombine): tests for the ext optimization
bassiounix efd069d
chore(test): rename test file
bassiounix d8ef2ec
chore(InstCombine): add more types to the test
bassiounix cc1e8cc
add(InstCombine): negative test for ext split optimization
bassiounix 4e423c6
fix(style): change variable names to be readable
bassiounix aa83856
chore(InstCombine): add comments describing the pattern
bassiounix 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,196 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|
||
; PR129363 | ||
|
||
; ext split from i32 to i128 | ||
define i128 @i128_ext_split(i32 noundef %x) { | ||
; CHECK-LABEL: define i128 @i128_ext_split( | ||
; CHECK-SAME: i32 noundef [[X:%.*]]) { | ||
; CHECK-NEXT: [[XX:%.*]] = sext i32 [[X]] to i128 | ||
; CHECK-NEXT: ret i128 [[XX]] | ||
; | ||
%LowerSrc = sext i32 %x to i64 | ||
%sign = ashr i32 %x, 31 | ||
%UpperSrc = sext i32 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
%res = or disjoint i128 %hi, %lo | ||
ret i128 %res | ||
} | ||
|
||
; ext split from i32 to i128 | ||
define void @i128_ext_split_store(i32 %x, ptr %out) { | ||
; CHECK-LABEL: define void @i128_ext_split_store( | ||
; CHECK-SAME: i32 [[X:%.*]], ptr [[OUT:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[RES:%.*]] = sext i32 [[X]] to i128 | ||
; CHECK-NEXT: store i128 [[RES]], ptr [[OUT]], align 16 | ||
; CHECK-NEXT: ret void | ||
; | ||
entry: | ||
%LowerSrc = sext i32 %x to i64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
|
||
%sign = ashr i32 %x, 31 | ||
%UpperSrc = sext i32 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 64 | ||
|
||
%res = or disjoint i128 %hi, %lo | ||
store i128 %res, ptr %out, align 16 | ||
ret void | ||
} | ||
|
||
; ext split from i16 to i64 | ||
define void @i64_ext_split_store(i16 %x, ptr %out) { | ||
; CHECK-LABEL: define void @i64_ext_split_store( | ||
; CHECK-SAME: i16 [[X:%.*]], ptr [[OUT:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[RES:%.*]] = sext i16 [[X]] to i64 | ||
; CHECK-NEXT: store i64 [[RES]], ptr [[OUT]], align 16 | ||
; CHECK-NEXT: ret void | ||
; | ||
entry: | ||
%LowerSrc = sext i16 %x to i32 | ||
%lo = zext i32 %LowerSrc to i64 | ||
|
||
%sign = ashr i16 %x, 15 | ||
%UpperSrc = sext i16 %sign to i32 | ||
%widen = zext i32 %UpperSrc to i64 | ||
%hi = shl nuw i64 %widen, 32 | ||
|
||
%res = or disjoint i64 %hi, %lo | ||
store i64 %res, ptr %out, align 16 | ||
ret void | ||
} | ||
bassiounix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
; ext split from i16 to i128 | ||
define void @i128_ext_split_store_i16(i16 %x, ptr %out) { | ||
; CHECK-LABEL: define void @i128_ext_split_store_i16( | ||
; CHECK-SAME: i16 [[X:%.*]], ptr [[OUT:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[RES:%.*]] = sext i16 [[X]] to i128 | ||
; CHECK-NEXT: store i128 [[RES]], ptr [[OUT]], align 16 | ||
; CHECK-NEXT: ret void | ||
; | ||
entry: | ||
%LowerSrc = sext i16 %x to i64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
|
||
%sign = ashr i16 %x, 15 | ||
%UpperSrc = sext i16 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 64 | ||
|
||
%res = or disjoint i128 %hi, %lo | ||
store i128 %res, ptr %out, align 16 | ||
ret void | ||
} | ||
|
||
; negative test - wrong constant value | ||
define i128 @i128_ext_split_neg1(i32 %x) { | ||
; CHECK-LABEL: define i128 @i128_ext_split_neg1( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[LOWERSRC:%.*]] = sext i32 [[X]] to i64 | ||
; CHECK-NEXT: [[LO:%.*]] = zext i64 [[LOWERSRC]] to i128 | ||
; CHECK-NEXT: [[SIGN:%.*]] = ashr i32 [[X]], 31 | ||
; CHECK-NEXT: [[UPPERSRC:%.*]] = sext i32 [[SIGN]] to i64 | ||
; CHECK-NEXT: [[WIDEN:%.*]] = zext i64 [[UPPERSRC]] to i128 | ||
; CHECK-NEXT: [[HI:%.*]] = shl nuw i128 [[WIDEN]], 65 | ||
; CHECK-NEXT: [[RES:%.*]] = or disjoint i128 [[HI]], [[LO]] | ||
; CHECK-NEXT: ret i128 [[RES]] | ||
; | ||
entry: | ||
%LowerSrc = sext i32 %x to i64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
|
||
%sign = ashr i32 %x, 31 | ||
%UpperSrc = sext i32 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 65 | ||
|
||
%res = or disjoint i128 %hi, %lo | ||
ret i128 %res | ||
} | ||
|
||
; negative test - wrong shift value | ||
define i128 @i128_ext_split_neg2(i32 %x) { | ||
; CHECK-LABEL: define i128 @i128_ext_split_neg2( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[LOWERSRC:%.*]] = sext i32 [[X]] to i64 | ||
; CHECK-NEXT: [[LO:%.*]] = zext i64 [[LOWERSRC]] to i128 | ||
; CHECK-NEXT: [[SIGN:%.*]] = ashr i32 [[X]], 3 | ||
; CHECK-NEXT: [[UPPERSRC:%.*]] = sext i32 [[SIGN]] to i64 | ||
; CHECK-NEXT: [[WIDEN:%.*]] = zext i64 [[UPPERSRC]] to i128 | ||
; CHECK-NEXT: [[HI:%.*]] = shl nuw i128 [[WIDEN]], 64 | ||
; CHECK-NEXT: [[RES:%.*]] = or disjoint i128 [[HI]], [[LO]] | ||
; CHECK-NEXT: ret i128 [[RES]] | ||
; | ||
entry: | ||
%LowerSrc = sext i32 %x to i64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
|
||
%sign = ashr i32 %x, 3 | ||
%UpperSrc = sext i32 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 64 | ||
|
||
%res = or disjoint i128 %hi, %lo | ||
ret i128 %res | ||
} | ||
|
||
; negative test - wrong ext instruction | ||
define i128 @i128_ext_split_neg3(i32 %x) { | ||
; CHECK-LABEL: define i128 @i128_ext_split_neg3( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[LO:%.*]] = zext i32 [[X]] to i128 | ||
; CHECK-NEXT: [[SIGN:%.*]] = ashr i32 [[X]], 31 | ||
; CHECK-NEXT: [[UPPERSRC:%.*]] = sext i32 [[SIGN]] to i64 | ||
; CHECK-NEXT: [[WIDEN:%.*]] = zext i64 [[UPPERSRC]] to i128 | ||
; CHECK-NEXT: [[HI:%.*]] = shl nuw i128 [[WIDEN]], 64 | ||
; CHECK-NEXT: [[RES:%.*]] = or disjoint i128 [[HI]], [[LO]] | ||
; CHECK-NEXT: ret i128 [[RES]] | ||
; | ||
entry: | ||
%LowerSrc = zext i32 %x to i64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
|
||
%sign = ashr i32 %x, 31 | ||
%UpperSrc = sext i32 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 64 | ||
|
||
%res = or disjoint i128 %hi, %lo | ||
ret i128 %res | ||
} | ||
|
||
; negative test - wrong shift | ||
define i128 @i128_ext_split_neg4(i32 %x) { | ||
; CHECK-LABEL: define i128 @i128_ext_split_neg4( | ||
; CHECK-SAME: i32 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: [[LOWERSRC:%.*]] = sext i32 [[X]] to i64 | ||
; CHECK-NEXT: [[LO:%.*]] = zext i64 [[LOWERSRC]] to i128 | ||
; CHECK-NEXT: [[SIGN:%.*]] = lshr i32 [[X]], 31 | ||
; CHECK-NEXT: [[WIDEN:%.*]] = zext nneg i32 [[SIGN]] to i128 | ||
; CHECK-NEXT: [[HI:%.*]] = shl nuw nsw i128 [[WIDEN]], 64 | ||
; CHECK-NEXT: [[RES:%.*]] = or disjoint i128 [[HI]], [[LO]] | ||
; CHECK-NEXT: ret i128 [[RES]] | ||
; | ||
entry: | ||
%LowerSrc = sext i32 %x to i64 | ||
%lo = zext i64 %LowerSrc to i128 | ||
|
||
%sign = lshr i32 %x, 31 | ||
%UpperSrc = sext i32 %sign to i64 | ||
%widen = zext i64 %UpperSrc to i128 | ||
%hi = shl nuw i128 %widen, 64 | ||
|
||
%res = or disjoint i128 %hi, %lo | ||
ret i128 %res | ||
} |
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.