Skip to content

Commit b29ba15

Browse files
committed
[SLP][REVEC] Make Instruction::Select support vector instructions.
1 parent 04a648b commit b29ba15

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9713,6 +9713,23 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
97139713
}
97149714
VecCost = std::min(VecCost, IntrinsicCost);
97159715
}
9716+
if (auto *SI = dyn_cast<SelectInst>(VL0)) {
9717+
auto *CondType =
9718+
getWidenedType(SI->getCondition()->getType(), VL.size());
9719+
unsigned CondNumElements = CondType->getNumElements();
9720+
unsigned VecTyNumElements = getNumElements(VecTy);
9721+
assert(VecTyNumElements >= CondNumElements &&
9722+
VecTyNumElements % CondNumElements == 0 &&
9723+
"Cannot vectorize Instruction::Select");
9724+
if (CondNumElements != VecTyNumElements) {
9725+
// When the return type is i1 but the source is fixed vector type, we
9726+
// need to duplicate the condition value.
9727+
VecCost += TTI->getShuffleCost(
9728+
TTI::SK_PermuteSingleSrc, CondType,
9729+
createReplicatedMask(VecTyNumElements / CondNumElements,
9730+
CondNumElements));
9731+
}
9732+
}
97169733
return VecCost + CommonCost;
97179734
};
97189735
return GetCostDiff(GetScalarCost, GetVectorCost);
@@ -13196,6 +13213,22 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1319613213
False = Builder.CreateIntCast(False, VecTy, GetOperandSignedness(2));
1319713214
}
1319813215

13216+
unsigned CondNumElements = getNumElements(Cond->getType());
13217+
unsigned TrueNumElements = getNumElements(True->getType());
13218+
assert(TrueNumElements >= CondNumElements &&
13219+
TrueNumElements % CondNumElements == 0 &&
13220+
"Cannot vectorize Instruction::Select");
13221+
assert(TrueNumElements == getNumElements(False->getType()) &&
13222+
"Cannot vectorize Instruction::Select");
13223+
if (CondNumElements != TrueNumElements) {
13224+
// When the return type is i1 but the source is fixed vector type, we
13225+
// need to duplicate the condition value.
13226+
Cond = Builder.CreateShuffleVector(
13227+
Cond, createReplicatedMask(TrueNumElements / CondNumElements,
13228+
CondNumElements));
13229+
}
13230+
assert(getNumElements(Cond->getType()) == TrueNumElements &&
13231+
"Cannot vectorize Instruction::Select");
1319913232
Value *V = Builder.CreateSelect(Cond, True, False);
1320013233
V = FinalShuffle(V, E, VecTy);
1320113234

llvm/test/Transforms/SLPVectorizer/revec.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ entry:
6060
}
6161

6262
define void @test3(ptr %x, ptr %y, ptr %z) {
63+
; CHECK-LABEL: @test3(
64+
; CHECK-NEXT: entry:
65+
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x ptr> poison, ptr [[X:%.*]], i32 0
66+
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <2 x ptr> [[TMP0]], ptr [[Y:%.*]], i32 1
67+
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x ptr> [[TMP1]], zeroinitializer
68+
; CHECK-NEXT: [[TMP3:%.*]] = load <8 x i32>, ptr [[X]], align 4
69+
; CHECK-NEXT: [[TMP4:%.*]] = load <8 x i32>, ptr [[Y]], align 4
70+
; CHECK-NEXT: [[TMP5:%.*]] = shufflevector <2 x i1> [[TMP2]], <2 x i1> poison, <8 x i32> <i32 0, i32 0, i32 0, i32 0, i32 1, i32 1, i32 1, i32 1>
71+
; CHECK-NEXT: [[TMP6:%.*]] = select <8 x i1> [[TMP5]], <8 x i32> [[TMP3]], <8 x i32> [[TMP4]]
72+
; CHECK-NEXT: store <8 x i32> [[TMP6]], ptr [[Z:%.*]], align 4
73+
; CHECK-NEXT: ret void
74+
;
6375
entry:
6476
%0 = getelementptr inbounds i32, ptr %x, i64 4
6577
%1 = getelementptr inbounds i32, ptr %y, i64 4

0 commit comments

Comments
 (0)