Skip to content

Commit 5903c6a

Browse files
authored
InstCombine: Fold shufflevector(select) and shufflevector(phi) (llvm#113746)
- Transform `shufflevector(select(c, x, y), C)` to `select(c, shufflevector(x, C), shufflevector(y, C))` by re-using the `FoldOpIntoSelect` helper. - Transform `shufflevector(phi(x, y), C)` to `phi(shufflevector(x, C), shufflevector(y, C))` by re-using the `foldOpInotPhi` helper.
1 parent 36c1194 commit 5903c6a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,17 @@ 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+
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
2906+
return I;
2907+
}
2908+
if (auto *PN = dyn_cast<PHINode>(LHS)) {
2909+
if (Instruction *I = foldOpIntoPhi(SVI, PN))
2910+
return I;
2911+
}
2912+
}
2913+
29032914
if (match(RHS, m_Poison()) && canEvaluateShuffled(LHS, Mask)) {
29042915
Value *V = evaluateInDifferentElementOrder(LHS, Mask, Builder);
29052916
return replaceInstUsesWith(SVI, V);

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,3 +2377,47 @@ 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+
declare i1 @cond()
2391+
declare <4 x i32> @value()
2392+
2393+
define <4 x i32> @foldphi1() {
2394+
; CHECK-LABEL: @foldphi1(
2395+
; CHECK-NEXT: entry:
2396+
; CHECK-NEXT: br label [[LOOP:%.*]]
2397+
; CHECK: loop:
2398+
; CHECK-NEXT: [[V:%.*]] = phi <4 x i32> [ zeroinitializer, [[ENTRY:%.*]] ], [ [[XOR:%.*]], [[LOOP]] ]
2399+
; CHECK-NEXT: [[VAL:%.*]] = call <4 x i32> @value()
2400+
; CHECK-NEXT: [[XOR]] = xor <4 x i32> [[V]], [[VAL]]
2401+
; CHECK-NEXT: [[C:%.*]] = call i1 @cond()
2402+
; CHECK-NEXT: br i1 [[C]], label [[LOOP]], label [[EXIT:%.*]]
2403+
; CHECK: exit:
2404+
; CHECK-NEXT: [[SHUF1:%.*]] = shufflevector <4 x i32> [[XOR]], <4 x i32> poison, <4 x i32> <i32 3, i32 0, i32 1, i32 2>
2405+
; CHECK-NEXT: ret <4 x i32> [[SHUF1]]
2406+
;
2407+
entry:
2408+
br label %loop
2409+
2410+
loop:
2411+
%v = phi <4 x i32> [zeroinitializer, %entry], [%shuf1, %loop]
2412+
2413+
%shuf0 = shufflevector <4 x i32> %v, <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
2414+
%val = call <4 x i32> @value()
2415+
%xor = xor <4 x i32> %shuf0, %val
2416+
%shuf1 = shufflevector <4 x i32> %xor, <4 x i32> poison, <4 x i32> <i32 3, i32 0, i32 1, i32 2>
2417+
2418+
%c = call i1 @cond()
2419+
br i1 %c, label %loop, label %exit
2420+
2421+
exit:
2422+
ret <4 x i32> %shuf1
2423+
}

0 commit comments

Comments
 (0)