-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[GISel] Combine shift + trunc + shift pattern #155583
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 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc8272a
[GISel] Combine shift + trunc + shift pattern
jyli0116 4187949
whitespace
jyli0116 74bcf6c
re,ove wip_match_opcode from GICombineRule
jyli0116 716eee0
resolved comments
jyli0116 e65e5b6
formatting
jyli0116 2deba83
cleaned up comments
jyli0116 5059b2f
changes based on comments
jyli0116 7824c6e
formatting
jyli0116 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
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 |
|---|---|---|
|
|
@@ -2094,6 +2094,70 @@ bool CombinerHelper::matchCommuteShift(MachineInstr &MI, | |
| return true; | ||
| } | ||
|
|
||
| bool CombinerHelper::matchLshrOfTruncOfLshr(MachineInstr &MI, | ||
| LshrOfTruncOfLshr &MatchInfo, | ||
| MachineInstr &ShiftMI) const { | ||
| unsigned ShiftOpcode = MI.getOpcode(); | ||
| assert(ShiftOpcode == TargetOpcode::G_LSHR); | ||
|
|
||
| Register N0 = MI.getOperand(1).getReg(); | ||
| Register N1 = MI.getOperand(2).getReg(); | ||
| unsigned OpSizeInBits = MRI.getType(N0).getScalarSizeInBits(); | ||
|
|
||
| APInt N1C, N001C; | ||
| if (!mi_match(N1, MRI, m_ICstOrSplat(N1C))) | ||
| return false; | ||
| auto N001 = ShiftMI.getOperand(2).getReg(); | ||
| if (!mi_match(N001, MRI, m_ICstOrSplat(N001C))) | ||
| return false; | ||
|
|
||
| if (N001C.getBitWidth() > N1C.getBitWidth()) | ||
| N1C = N1C.zext(N001C.getBitWidth()); | ||
| else | ||
| N001C = N001C.zext(N1C.getBitWidth()); | ||
|
|
||
| Register InnerShift = ShiftMI.getOperand(0).getReg(); | ||
| LLT InnerShiftTy = MRI.getType(InnerShift); | ||
| uint64_t InnerShiftSize = InnerShiftTy.getScalarSizeInBits(); | ||
| if ((N1C + N001C).ult(InnerShiftSize)) { | ||
| MatchInfo.Src = ShiftMI.getOperand(1).getReg(); | ||
| MatchInfo.ShiftAmt = N1C + N001C; | ||
| MatchInfo.ShiftAmtTy = MRI.getType(N001); | ||
| MatchInfo.InnerShiftTy = InnerShiftTy; | ||
|
|
||
| if ((N001C + OpSizeInBits) == InnerShiftSize) | ||
| return true; | ||
| if (MRI.hasOneUse(N0) && MRI.hasOneUse(InnerShift)) { | ||
| MatchInfo.Mask = true; | ||
| MatchInfo.MaskVal = APInt(N1C.getBitWidth(), OpSizeInBits) - N1C; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void CombinerHelper::applyLshrOfTruncOfLshr( | ||
| MachineInstr &MI, LshrOfTruncOfLshr &MatchInfo) const { | ||
| unsigned ShiftOpcode = MI.getOpcode(); | ||
| assert(ShiftOpcode == TargetOpcode::G_LSHR); | ||
|
|
||
| Register Dst = MI.getOperand(0).getReg(); | ||
| auto ShiftAmt = | ||
| Builder.buildConstant(MatchInfo.ShiftAmtTy, MatchInfo.ShiftAmt); | ||
| auto Shift = | ||
| Builder.buildLShr(MatchInfo.InnerShiftTy, MatchInfo.Src, ShiftAmt); | ||
| if (MatchInfo.Mask == true) { | ||
| APInt MaskVal = | ||
| APInt::getLowBitsSet(MatchInfo.InnerShiftTy.getScalarSizeInBits(), | ||
| MatchInfo.MaskVal.getZExtValue()); | ||
| auto Mask = Builder.buildConstant(MatchInfo.ShiftAmtTy, MaskVal); | ||
|
||
| auto And = Builder.buildAnd(MatchInfo.InnerShiftTy, Shift, Mask); | ||
| Builder.buildTrunc(Dst, And); | ||
| } else | ||
| Builder.buildTrunc(Dst, Shift); | ||
| MI.eraseFromParent(); | ||
| } | ||
|
|
||
| bool CombinerHelper::matchCombineMulToShl(MachineInstr &MI, | ||
| unsigned &ShiftVal) const { | ||
| assert(MI.getOpcode() == TargetOpcode::G_MUL && "Expected a G_MUL"); | ||
|
|
||
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,125 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: llc -mtriple=aarch64-none-eabi -global-isel=0 -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-SD | ||
| ; RUN: llc -mtriple=aarch64-none-eabi -global-isel=1 -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=CHECK,CHECK-GI | ||
|
|
||
| define i32 @s32_test1(i64 %a) { | ||
| ; CHECK-LABEL: s32_test1: | ||
| ; CHECK: // %bb.0: | ||
| ; CHECK-NEXT: lsr x0, x0, #48 | ||
| ; CHECK-NEXT: // kill: def $w0 killed $w0 killed $x0 | ||
| ; CHECK-NEXT: ret | ||
| %r = lshr i64 %a, 32 | ||
| %ret = trunc i64 %r to i32 | ||
| %x = lshr i32 %ret, 16 | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define i32 @s32_test2(i64 %a) { | ||
| ; CHECK-LABEL: s32_test2: | ||
| ; CHECK: // %bb.0: | ||
| ; CHECK-NEXT: ubfx x0, x0, #32, #16 | ||
| ; CHECK-NEXT: // kill: def $w0 killed $w0 killed $x0 | ||
| ; CHECK-NEXT: ret | ||
| %r = lshr i64 %a, 16 | ||
| %ret = trunc i64 %r to i32 | ||
| %x = lshr i32 %ret, 16 | ||
| ret i32 %x | ||
| } | ||
|
|
||
| define <8 x i8> @v8s8_test1(<8 x i16> %a) { | ||
| ; CHECK-LABEL: v8s8_test1: | ||
| ; CHECK: // %bb.0: | ||
| ; CHECK-NEXT: ushr v0.8h, v0.8h, #12 | ||
| ; CHECK-NEXT: xtn v0.8b, v0.8h | ||
| ; CHECK-NEXT: ret | ||
| %r = lshr <8 x i16> %a, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8> | ||
| %ret = trunc <8 x i16> %r to <8 x i8> | ||
| %x = lshr <8 x i8> %ret, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4> | ||
| ret <8 x i8> %x | ||
| } | ||
|
|
||
| define <8 x i8> @v8s8_test2(<8 x i16> %a) { | ||
| ; CHECK-SD-LABEL: v8s8_test2: | ||
| ; CHECK-SD: // %bb.0: | ||
| ; CHECK-SD-NEXT: ushr v0.8h, v0.8h, #8 | ||
| ; CHECK-SD-NEXT: bic v0.8h, #240 | ||
| ; CHECK-SD-NEXT: xtn v0.8b, v0.8h | ||
| ; CHECK-SD-NEXT: ret | ||
| ; | ||
| ; CHECK-GI-LABEL: v8s8_test2: | ||
| ; CHECK-GI: // %bb.0: | ||
| ; CHECK-GI-NEXT: movi v1.8h, #15 | ||
| ; CHECK-GI-NEXT: ushr v0.8h, v0.8h, #8 | ||
| ; CHECK-GI-NEXT: and v0.16b, v0.16b, v1.16b | ||
| ; CHECK-GI-NEXT: xtn v0.8b, v0.8h | ||
| ; CHECK-GI-NEXT: ret | ||
| %r = lshr <8 x i16> %a, <i16 4, i16 4, i16 4, i16 4, i16 4, i16 4, i16 4, i16 4> | ||
| %ret = trunc <8 x i16> %r to <8 x i8> | ||
| %x = lshr <8 x i8> %ret, <i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4, i8 4> | ||
| ret <8 x i8> %x | ||
| } | ||
|
|
||
| define <4 x i16> @v4s16_test1(<4 x i32> %a) { | ||
| ; CHECK-LABEL: v4s16_test1: | ||
| ; CHECK: // %bb.0: | ||
| ; CHECK-NEXT: ushr v0.4s, v0.4s, #24 | ||
| ; CHECK-NEXT: xtn v0.4h, v0.4s | ||
| ; CHECK-NEXT: ret | ||
| %r = lshr <4 x i32> %a, <i32 16, i32 16, i32 16, i32 16> | ||
| %ret = trunc <4 x i32> %r to <4 x i16> | ||
| %x = lshr <4 x i16> %ret, <i16 8, i16 8, i16 8, i16 8> | ||
| ret <4 x i16> %x | ||
| } | ||
|
|
||
| define <4 x i16> @v4s16_test2(<4 x i32> %a) { | ||
| ; CHECK-SD-LABEL: v4s16_test2: | ||
| ; CHECK-SD: // %bb.0: | ||
| ; CHECK-SD-NEXT: shrn v0.4h, v0.4s, #16 | ||
| ; CHECK-SD-NEXT: bic v0.4h, #255, lsl #8 | ||
| ; CHECK-SD-NEXT: ret | ||
| ; | ||
| ; CHECK-GI-LABEL: v4s16_test2: | ||
| ; CHECK-GI: // %bb.0: | ||
| ; CHECK-GI-NEXT: movi v1.2d, #0x0000ff000000ff | ||
| ; CHECK-GI-NEXT: ushr v0.4s, v0.4s, #16 | ||
| ; CHECK-GI-NEXT: and v0.16b, v0.16b, v1.16b | ||
| ; CHECK-GI-NEXT: xtn v0.4h, v0.4s | ||
| ; CHECK-GI-NEXT: ret | ||
| %r = lshr <4 x i32> %a, <i32 8, i32 8, i32 8, i32 8> | ||
| %ret = trunc <4 x i32> %r to <4 x i16> | ||
| %x = lshr <4 x i16> %ret, <i16 8, i16 8, i16 8, i16 8> | ||
| ret <4 x i16> %x | ||
| } | ||
|
|
||
| define <2 x i32> @v2s32_test1(<2 x i64> %a) { | ||
| ; CHECK-LABEL: v2s32_test1: | ||
| ; CHECK: // %bb.0: | ||
| ; CHECK-NEXT: ushr v0.2d, v0.2d, #48 | ||
| ; CHECK-NEXT: xtn v0.2s, v0.2d | ||
| ; CHECK-NEXT: ret | ||
| %r = lshr <2 x i64> %a, <i64 32, i64 32> | ||
| %ret = trunc <2 x i64> %r to <2 x i32> | ||
| %x = lshr <2 x i32> %ret, <i32 16, i32 16> | ||
| ret <2 x i32> %x | ||
| } | ||
|
|
||
| define <2 x i32> @v2s32_test2(<2 x i64> %a) { | ||
| ; CHECK-SD-LABEL: v2s32_test2: | ||
| ; CHECK-SD: // %bb.0: | ||
| ; CHECK-SD-NEXT: movi d1, #0x00ffff0000ffff | ||
| ; CHECK-SD-NEXT: shrn v0.2s, v0.2d, #32 | ||
| ; CHECK-SD-NEXT: and v0.8b, v0.8b, v1.8b | ||
| ; CHECK-SD-NEXT: ret | ||
| ; | ||
| ; CHECK-GI-LABEL: v2s32_test2: | ||
| ; CHECK-GI: // %bb.0: | ||
| ; CHECK-GI-NEXT: movi v1.2d, #0x0000000000ffff | ||
| ; CHECK-GI-NEXT: ushr v0.2d, v0.2d, #32 | ||
| ; CHECK-GI-NEXT: and v0.16b, v0.16b, v1.16b | ||
| ; CHECK-GI-NEXT: xtn v0.2s, v0.2d | ||
| ; CHECK-GI-NEXT: ret | ||
| %r = lshr <2 x i64> %a, <i64 16, i64 16> | ||
| %ret = trunc <2 x i64> %r to <2 x i32> | ||
| %x = lshr <2 x i32> %ret, <i32 16, i32 16> | ||
| ret <2 x i32> %x | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asserts usually get given messages in llvm.