Skip to content

Commit db5f5e0

Browse files
committed
[VectorCombine] Add Cmp and Select for shuffleToIdentity
Other than some additional checks needed for compare predicates and selects with scalar condition operands, these are relatively simple additions to what already exists.
1 parent 335e00f commit db5f5e0

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,10 @@ static Value *generateNewInstTree(ArrayRef<InstLane> Item, FixedVectorType *Ty,
17421742
if (auto *BI = dyn_cast<BinaryOperator>(I))
17431743
return Builder.CreateBinOp((Instruction::BinaryOps)BI->getOpcode(), Ops[0],
17441744
Ops[1]);
1745+
if (auto CI = dyn_cast<CmpInst>(I))
1746+
return Builder.CreateCmp(CI->getPredicate(), Ops[0], Ops[1]);
1747+
if (auto SI = dyn_cast<SelectInst>(I))
1748+
return Builder.CreateSelect(Ops[0], Ops[1], Ops[2], "", SI);
17451749
if (II)
17461750
return Builder.CreateIntrinsic(DstTy, II->getIntrinsicID(), Ops);
17471751
assert(isa<UnaryInstruction>(I) && "Unexpected instruction type in Generate");
@@ -1810,6 +1814,12 @@ bool VectorCombine::foldShuffleToIdentity(Instruction &I) {
18101814
return false;
18111815
if (V->getValueID() != FrontV->getValueID())
18121816
return false;
1817+
if (auto *CI = dyn_cast<CmpInst>(V))
1818+
if (CI->getPredicate() != cast<CmpInst>(FrontV)->getPredicate())
1819+
return false;
1820+
if (auto *SI = dyn_cast<SelectInst>(V))
1821+
if (!isa<VectorType>(SI->getOperand(0)->getType()))
1822+
return false;
18131823
if (isa<CallInst>(V) && !isa<IntrinsicInst>(V))
18141824
return false;
18151825
auto *II = dyn_cast<IntrinsicInst>(V);
@@ -1821,12 +1831,17 @@ bool VectorCombine::foldShuffleToIdentity(Instruction &I) {
18211831

18221832
// Check the operator is one that we support. We exclude div/rem in case
18231833
// they hit UB from poison lanes.
1824-
if (isa<BinaryOperator>(FrontV) &&
1825-
!cast<BinaryOperator>(FrontV)->isIntDivRem()) {
1834+
if ((isa<BinaryOperator>(FrontV) &&
1835+
!cast<BinaryOperator>(FrontV)->isIntDivRem()) ||
1836+
isa<CmpInst>(FrontV)) {
18261837
Worklist.push_back(generateInstLaneVectorFromOperand(Item, 0));
18271838
Worklist.push_back(generateInstLaneVectorFromOperand(Item, 1));
18281839
} else if (isa<UnaryOperator>(FrontV)) {
18291840
Worklist.push_back(generateInstLaneVectorFromOperand(Item, 0));
1841+
} else if (isa<SelectInst>(FrontV)) {
1842+
Worklist.push_back(generateInstLaneVectorFromOperand(Item, 0));
1843+
Worklist.push_back(generateInstLaneVectorFromOperand(Item, 1));
1844+
Worklist.push_back(generateInstLaneVectorFromOperand(Item, 2));
18301845
} else if (auto *II = dyn_cast<IntrinsicInst>(FrontV);
18311846
II && isTriviallyVectorizable(II->getIntrinsicID())) {
18321847
for (unsigned Op = 0, E = II->getNumOperands() - 1; Op < E; Op++) {

llvm/test/Transforms/VectorCombine/AArch64/shuffletoidentity.ll

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -427,19 +427,8 @@ define <8 x i8> @extrause_shuffle(<8 x i8> %a, <8 x i8> %b) {
427427

428428
define <8 x i8> @icmpsel(<8 x i8> %a, <8 x i8> %b, <8 x i8> %c, <8 x i8> %d) {
429429
; CHECK-LABEL: @icmpsel(
430-
; CHECK-NEXT: [[AB:%.*]] = shufflevector <8 x i8> [[A:%.*]], <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
431-
; CHECK-NEXT: [[AT:%.*]] = shufflevector <8 x i8> [[A]], <8 x i8> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
432-
; CHECK-NEXT: [[BB:%.*]] = shufflevector <8 x i8> [[B:%.*]], <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
433-
; CHECK-NEXT: [[BT:%.*]] = shufflevector <8 x i8> [[B]], <8 x i8> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
434-
; CHECK-NEXT: [[CB:%.*]] = shufflevector <8 x i8> [[C:%.*]], <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
435-
; CHECK-NEXT: [[CT:%.*]] = shufflevector <8 x i8> [[C]], <8 x i8> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
436-
; CHECK-NEXT: [[DB:%.*]] = shufflevector <8 x i8> [[D:%.*]], <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
437-
; CHECK-NEXT: [[DT:%.*]] = shufflevector <8 x i8> [[D]], <8 x i8> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
438-
; CHECK-NEXT: [[ABT1:%.*]] = icmp slt <4 x i8> [[AT]], [[BT]]
439-
; CHECK-NEXT: [[ABB1:%.*]] = icmp slt <4 x i8> [[AB]], [[BB]]
440-
; CHECK-NEXT: [[ABT:%.*]] = select <4 x i1> [[ABT1]], <4 x i8> [[CT]], <4 x i8> [[DT]]
441-
; CHECK-NEXT: [[ABB:%.*]] = select <4 x i1> [[ABB1]], <4 x i8> [[CB]], <4 x i8> [[DB]]
442-
; CHECK-NEXT: [[R:%.*]] = shufflevector <4 x i8> [[ABT]], <4 x i8> [[ABB]], <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
430+
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <8 x i8> [[A:%.*]], [[B:%.*]]
431+
; CHECK-NEXT: [[R:%.*]] = select <8 x i1> [[TMP1]], <8 x i8> [[C:%.*]], <8 x i8> [[D:%.*]]
443432
; CHECK-NEXT: ret <8 x i8> [[R]]
444433
;
445434
%ab = shufflevector <8 x i8> %a, <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
@@ -493,19 +482,8 @@ define <8 x i8> @icmpsel_diffentcond(<8 x i8> %a, <8 x i8> %b, <8 x i8> %c, <8 x
493482

494483
define <8 x i8> @fcmpsel(<8 x half> %a, <8 x half> %b, <8 x i8> %c, <8 x i8> %d) {
495484
; CHECK-LABEL: @fcmpsel(
496-
; CHECK-NEXT: [[AB:%.*]] = shufflevector <8 x half> [[A:%.*]], <8 x half> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
497-
; CHECK-NEXT: [[AT:%.*]] = shufflevector <8 x half> [[A]], <8 x half> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
498-
; CHECK-NEXT: [[BB:%.*]] = shufflevector <8 x half> [[B:%.*]], <8 x half> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
499-
; CHECK-NEXT: [[BT:%.*]] = shufflevector <8 x half> [[B]], <8 x half> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
500-
; CHECK-NEXT: [[CB:%.*]] = shufflevector <8 x i8> [[C:%.*]], <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
501-
; CHECK-NEXT: [[CT:%.*]] = shufflevector <8 x i8> [[C]], <8 x i8> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
502-
; CHECK-NEXT: [[DB:%.*]] = shufflevector <8 x i8> [[D:%.*]], <8 x i8> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
503-
; CHECK-NEXT: [[DT:%.*]] = shufflevector <8 x i8> [[D]], <8 x i8> poison, <4 x i32> <i32 7, i32 6, i32 5, i32 4>
504-
; CHECK-NEXT: [[ABT1:%.*]] = fcmp olt <4 x half> [[AT]], [[BT]]
505-
; CHECK-NEXT: [[ABB1:%.*]] = fcmp olt <4 x half> [[AB]], [[BB]]
506-
; CHECK-NEXT: [[ABT:%.*]] = select <4 x i1> [[ABT1]], <4 x i8> [[CT]], <4 x i8> [[DT]]
507-
; CHECK-NEXT: [[ABB:%.*]] = select <4 x i1> [[ABB1]], <4 x i8> [[CB]], <4 x i8> [[DB]]
508-
; CHECK-NEXT: [[R:%.*]] = shufflevector <4 x i8> [[ABT]], <4 x i8> [[ABB]], <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
485+
; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt <8 x half> [[A:%.*]], [[B:%.*]]
486+
; CHECK-NEXT: [[R:%.*]] = select <8 x i1> [[TMP1]], <8 x i8> [[C:%.*]], <8 x i8> [[D:%.*]]
509487
; CHECK-NEXT: ret <8 x i8> [[R]]
510488
;
511489
%ab = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>

0 commit comments

Comments
 (0)