Skip to content

Commit 3d423c9

Browse files
macurtis-amdronlieb
authored andcommitted
[InstCombine] un-revert shufflevector fold commits (oclBlender* break)
1831109 [InstCombine] Do not fold `shufflevector(select)` if the select condition is a vector (llvm#113993) 5903c6a InstCombine: Fold shufflevector(select) and shufflevector(phi) (llvm#113746) Change-Id: I35528e2f6682157bbe5a178a4108efc091457769
1 parent 581591b commit 3d423c9

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,21 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
29002900
if (Instruction *I = foldIdentityPaddedShuffles(SVI))
29012901
return I;
29022902

2903+
if (match(RHS, m_Constant())) {
2904+
if (auto *SI = dyn_cast<SelectInst>(LHS)) {
2905+
// We cannot do this fold for elementwise select since ShuffleVector is
2906+
// not elementwise.
2907+
if (SI->getCondition()->getType()->isIntegerTy()) {
2908+
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
2909+
return I;
2910+
}
2911+
}
2912+
if (auto *PN = dyn_cast<PHINode>(LHS)) {
2913+
if (Instruction *I = foldOpIntoPhi(SVI, PN))
2914+
return I;
2915+
}
2916+
}
2917+
29032918
if (match(RHS, m_Poison()) && canEvaluateShuffled(LHS, Mask)) {
29042919
Value *V = evaluateInDifferentElementOrder(LHS, Mask, Builder);
29052920
return replaceInstUsesWith(SVI, V);

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,3 +2377,71 @@ define <2 x i32> @not_splat_shuffle2(i32 %x) {
23772377
%shuf = shufflevector <2 x i32> %vec, <2 x i32> undef, <2 x i32> <i32 1, i32 3>
23782378
ret <2 x i32> %shuf
23792379
}
2380+
define <2 x i32> @foldselect0(i1 %c) {
2381+
; CHECK-LABEL: @foldselect0(
2382+
; CHECK-NEXT: [[SHUF:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 7, i32 42>, <2 x i32> <i32 1, i32 0>
2383+
; CHECK-NEXT: ret <2 x i32> [[SHUF]]
2384+
;
2385+
%sel = select i1 %c, <2 x i32> <i32 42, i32 7>, <2 x i32> <i32 0, i32 1>
2386+
%shuf = shufflevector <2 x i32> %sel, <2 x i32> poison, <2 x i32> <i32 1, i32 0>
2387+
ret <2 x i32> %shuf
2388+
}
2389+
2390+
; Make sure we do not crash in this case.
2391+
define <4 x float> @shuf_larger_length_vec_select(<2 x i1> %cond) {
2392+
; CHECK-LABEL: @shuf_larger_length_vec_select(
2393+
; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[COND:%.*]], <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00>
2394+
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x float> [[SEL]], <2 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
2395+
; CHECK-NEXT: ret <4 x float> [[SHUF]]
2396+
;
2397+
%sel = select <2 x i1> %cond, <2 x float> zeroinitializer, <2 x float> splat(float 1.000000e+00)
2398+
%shuf = shufflevector <2 x float> %sel, <2 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
2399+
ret <4 x float> %shuf
2400+
}
2401+
2402+
; Make sure we do not fold in this case.
2403+
define <4 x i32> @shuf_same_length_vec_select(<4 x i1> %cond) {
2404+
; CHECK-LABEL: @shuf_same_length_vec_select(
2405+
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[COND:%.*]], <4 x i32> <i32 poison, i32 1, i32 2, i32 3>, <4 x i32> <i32 poison, i32 5, i32 6, i32 7>
2406+
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SEL]], <4 x i32> <i32 poison, i32 9, i32 poison, i32 poison>, <4 x i32> <i32 2, i32 1, i32 3, i32 5>
2407+
; CHECK-NEXT: ret <4 x i32> [[SHUF]]
2408+
;
2409+
%sel = select <4 x i1> %cond, <4 x i32> <i32 0, i32 1, i32 2, i32 3>, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
2410+
%shuf = shufflevector <4 x i32> %sel, <4 x i32> <i32 8, i32 9, i32 10, i32 11>, <4 x i32> <i32 2, i32 1, i32 3, i32 5>
2411+
ret <4 x i32> %shuf
2412+
}
2413+
2414+
declare i1 @cond()
2415+
declare <4 x i32> @value()
2416+
2417+
define <4 x i32> @foldphi1() {
2418+
; CHECK-LABEL: @foldphi1(
2419+
; CHECK-NEXT: entry:
2420+
; CHECK-NEXT: br label [[LOOP:%.*]]
2421+
; CHECK: loop:
2422+
; CHECK-NEXT: [[V:%.*]] = phi <4 x i32> [ zeroinitializer, [[ENTRY:%.*]] ], [ [[XOR:%.*]], [[LOOP]] ]
2423+
; CHECK-NEXT: [[VAL:%.*]] = call <4 x i32> @value()
2424+
; CHECK-NEXT: [[XOR]] = xor <4 x i32> [[V]], [[VAL]]
2425+
; CHECK-NEXT: [[C:%.*]] = call i1 @cond()
2426+
; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
2427+
; CHECK: exit:
2428+
; CHECK-NEXT: [[SHUF1:%.*]] = shufflevector <4 x i32> [[XOR]], <4 x i32> poison, <4 x i32> <i32 3, i32 0, i32 1, i32 2>
2429+
; CHECK-NEXT: ret <4 x i32> [[SHUF1]]
2430+
;
2431+
entry:
2432+
br label %loop
2433+
2434+
loop:
2435+
%v = phi <4 x i32> [zeroinitializer, %entry], [%shuf1, %loop]
2436+
2437+
%shuf0 = shufflevector <4 x i32> %v, <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
2438+
%val = call <4 x i32> @value()
2439+
%xor = xor <4 x i32> %shuf0, %val
2440+
%shuf1 = shufflevector <4 x i32> %xor, <4 x i32> poison, <4 x i32> <i32 3, i32 0, i32 1, i32 2>
2441+
2442+
%c = call i1 @cond()
2443+
br i1 %c, label %loop, label %exit
2444+
2445+
exit:
2446+
ret <4 x i32> %shuf1
2447+
}

0 commit comments

Comments
 (0)