Skip to content

Commit 5415fef

Browse files
committed
[InstCombine] matchFunnelShift - support non-uniform constant vector shift amounts (PR46895)
Complete basic PR46895 fixes by refactoring D87452/D88402 to allow us to match non-uniform constant values. We still don't handle non-uniform vectors that contain undef elements, but that can wait until we have a decent generic mechanism for this. Differential Revision: https://reviews.llvm.org/D88420
1 parent a4e2722 commit 5415fef

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,11 +2079,20 @@ static Instruction *matchFunnelShift(Instruction &Or) {
20792079
// matches a subtraction on the R operand.
20802080
auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
20812081
// Check for constant shift amounts that sum to the bitwidth.
2082-
// TODO: Support non-uniform shift amounts.
2083-
const APInt *LC, *RC;
2084-
if (match(L, m_APIntAllowUndef(LC)) && match(R, m_APIntAllowUndef(RC)))
2085-
if (LC->ult(Width) && RC->ult(Width) && (*LC + *RC) == Width)
2086-
return ConstantInt::get(L->getType(), *LC);
2082+
const APInt *LI, *RI;
2083+
if (match(L, m_APIntAllowUndef(LI)) && match(R, m_APIntAllowUndef(RI)))
2084+
if (LI->ult(Width) && RI->ult(Width) && (*LI + *RI) == Width)
2085+
return ConstantInt::get(L->getType(), *LI);
2086+
2087+
// TODO: Support undefs in non-uniform shift amounts.
2088+
Constant *LC, *RC;
2089+
if (match(L, m_Constant(LC)) && !LC->containsUndefElement() &&
2090+
match(R, m_Constant(RC)) && !RC->containsUndefElement() &&
2091+
match(L, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2092+
match(R, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width)))) {
2093+
if (match(ConstantExpr::getAdd(LC, RC), m_SpecificInt(Width)))
2094+
return L;
2095+
}
20872096

20882097
// For non-constant cases, the following patterns currently only work for
20892098
// rotation patterns.

llvm/test/Transforms/InstCombine/funnel.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,12 @@ define <2 x i17> @fshr_v2i17_constant_splat_undef1(<2 x i17> %x, <2 x i17> %y) {
100100
ret <2 x i17> %r
101101
}
102102

103-
; TODO: Allow arbitrary shift constants.
103+
; Allow arbitrary shift constants.
104+
; TODO: Support undef elements.
104105

105106
define <2 x i32> @fshr_v2i32_constant_nonsplat(<2 x i32> %x, <2 x i32> %y) {
106107
; CHECK-LABEL: @fshr_v2i32_constant_nonsplat(
107-
; CHECK-NEXT: [[SHR:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 17, i32 19>
108-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> [[Y:%.*]], <i32 15, i32 13>
109-
; CHECK-NEXT: [[R:%.*]] = or <2 x i32> [[SHL]], [[SHR]]
108+
; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> [[Y:%.*]], <2 x i32> [[X:%.*]], <2 x i32> <i32 15, i32 13>)
110109
; CHECK-NEXT: ret <2 x i32> [[R]]
111110
;
112111
%shr = lshr <2 x i32> %x, <i32 17, i32 19>
@@ -143,9 +142,7 @@ define <2 x i32> @fshr_v2i32_constant_nonsplat_undef1(<2 x i32> %x, <2 x i32> %y
143142

144143
define <2 x i36> @fshl_v2i36_constant_nonsplat(<2 x i36> %x, <2 x i36> %y) {
145144
; CHECK-LABEL: @fshl_v2i36_constant_nonsplat(
146-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i36> [[X:%.*]], <i36 21, i36 11>
147-
; CHECK-NEXT: [[SHR:%.*]] = lshr <2 x i36> [[Y:%.*]], <i36 15, i36 25>
148-
; CHECK-NEXT: [[R:%.*]] = or <2 x i36> [[SHL]], [[SHR]]
145+
; CHECK-NEXT: [[R:%.*]] = call <2 x i36> @llvm.fshl.v2i36(<2 x i36> [[X:%.*]], <2 x i36> [[Y:%.*]], <2 x i36> <i36 21, i36 11>)
149146
; CHECK-NEXT: ret <2 x i36> [[R]]
150147
;
151148
%shl = shl <2 x i36> %x, <i36 21, i36 11>

llvm/test/Transforms/InstCombine/rotate.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,12 @@ define <2 x i17> @rotr_v2i17_constant_splat_undef1(<2 x i17> %x) {
122122
ret <2 x i17> %r
123123
}
124124

125-
; TODO: Allow arbitrary shift constants.
125+
; Allow arbitrary shift constants.
126+
; TODO: Support undef elements.
126127

127128
define <2 x i32> @rotr_v2i32_constant_nonsplat(<2 x i32> %x) {
128129
; CHECK-LABEL: @rotr_v2i32_constant_nonsplat(
129-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> [[X:%.*]], <i32 17, i32 19>
130-
; CHECK-NEXT: [[SHR:%.*]] = lshr <2 x i32> [[X]], <i32 15, i32 13>
131-
; CHECK-NEXT: [[R:%.*]] = or <2 x i32> [[SHL]], [[SHR]]
130+
; CHECK-NEXT: [[R:%.*]] = call <2 x i32> @llvm.fshl.v2i32(<2 x i32> [[X:%.*]], <2 x i32> [[X]], <2 x i32> <i32 17, i32 19>)
132131
; CHECK-NEXT: ret <2 x i32> [[R]]
133132
;
134133
%shl = shl <2 x i32> %x, <i32 17, i32 19>
@@ -165,9 +164,7 @@ define <2 x i32> @rotr_v2i32_constant_nonsplat_undef1(<2 x i32> %x) {
165164

166165
define <2 x i36> @rotl_v2i36_constant_nonsplat(<2 x i36> %x) {
167166
; CHECK-LABEL: @rotl_v2i36_constant_nonsplat(
168-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i36> [[X:%.*]], <i36 21, i36 11>
169-
; CHECK-NEXT: [[SHR:%.*]] = lshr <2 x i36> [[X]], <i36 15, i36 25>
170-
; CHECK-NEXT: [[R:%.*]] = or <2 x i36> [[SHL]], [[SHR]]
167+
; CHECK-NEXT: [[R:%.*]] = call <2 x i36> @llvm.fshl.v2i36(<2 x i36> [[X:%.*]], <2 x i36> [[X]], <2 x i36> <i36 21, i36 11>)
171168
; CHECK-NEXT: ret <2 x i36> [[R]]
172169
;
173170
%shl = shl <2 x i36> %x, <i36 21, i36 11>

0 commit comments

Comments
 (0)