Skip to content

Commit 1c6c850

Browse files
authored
InstCombine: extend select-equiv to support vectors (#111966)
foldSelectEquivalence currently doesn't support GVN-like replacements on vector types. Put in the checks for potentially lane-crossing operations, and lift the limitation.
1 parent d27394a commit 1c6c850

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6950,7 +6950,9 @@ bool llvm::onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V) {
69506950
bool llvm::isNotCrossLaneOperation(const Instruction *I) {
69516951
if (auto *II = dyn_cast<IntrinsicInst>(I))
69526952
return isTriviallyVectorizable(II->getIntrinsicID());
6953-
return !isa<CallBase, BitCastInst, ShuffleVectorInst, ExtractElementInst>(I);
6953+
auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
6954+
return (!Shuffle || Shuffle->isSelect()) &&
6955+
!isa<CallBase, BitCastInst, ExtractElementInst>(I);
69546956
}
69556957

69566958
bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,10 @@ bool InstCombinerImpl::replaceInInstruction(Value *V, Value *Old, Value *New,
12881288
!isSafeToSpeculativelyExecuteWithVariableReplaced(I))
12891289
return false;
12901290

1291+
// Forbid potentially lane-crossing instructions.
1292+
if (Old->getType()->isVectorTy() && !isNotCrossLaneOperation(I))
1293+
return false;
1294+
12911295
bool Changed = false;
12921296
for (Use &U : I->operands()) {
12931297
if (U == Old) {
@@ -1366,9 +1370,8 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
13661370
// with different operands, which should not cause side-effects or trigger
13671371
// undefined behavior). Only do this if CmpRHS is a constant, as
13681372
// profitability is not clear for other cases.
1369-
// FIXME: Support vectors.
13701373
if (OldOp == CmpLHS && match(NewOp, m_ImmConstant()) &&
1371-
!match(OldOp, m_Constant()) && !Cmp.getType()->isVectorTy() &&
1374+
!match(OldOp, m_Constant()) &&
13721375
isGuaranteedNotToBeUndef(NewOp, SQ.AC, &Sel, &DT))
13731376
if (replaceInInstruction(TrueVal, OldOp, NewOp))
13741377
return &Sel;

llvm/test/Transforms/InstCombine/and-or-icmps.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ define <2 x i1> @substitute_constant_or_ne_slt_swap_vec_poison(<2 x i8> %x, <2 x
983983
define <2 x i1> @substitute_constant_or_ne_slt_swap_vec_logical(<2 x i8> %x, <2 x i8> %y) {
984984
; CHECK-LABEL: @substitute_constant_or_ne_slt_swap_vec_logical(
985985
; CHECK-NEXT: [[C1:%.*]] = icmp ne <2 x i8> [[X:%.*]], <i8 42, i8 poison>
986-
; CHECK-NEXT: [[C2:%.*]] = icmp slt <2 x i8> [[Y:%.*]], [[X]]
986+
; CHECK-NEXT: [[C2:%.*]] = icmp slt <2 x i8> [[Y:%.*]], <i8 42, i8 poison>
987987
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C1]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[C2]]
988988
; CHECK-NEXT: ret <2 x i1> [[R]]
989989
;

llvm/test/Transforms/InstCombine/select-binop-cmp.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,12 @@ define i32 @select_xor_icmp_bad_6(i32 %x, i32 %y, i32 %z) {
552552
ret i32 %C
553553
}
554554

555-
; Value equivalence substitution is all-or-nothing, so needs a scalar compare.
555+
; Value equivalence substitution is valid.
556556

557-
define <2 x i8> @select_xor_icmp_vec_bad(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
558-
; CHECK-LABEL: @select_xor_icmp_vec_bad(
557+
define <2 x i8> @select_xor_icmp_vec_equivalence(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
558+
; CHECK-LABEL: @select_xor_icmp_vec_equivalence(
559559
; CHECK-NEXT: [[A:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 5, i8 3>
560-
; CHECK-NEXT: [[B:%.*]] = xor <2 x i8> [[X]], [[Z:%.*]]
560+
; CHECK-NEXT: [[B:%.*]] = xor <2 x i8> [[Z:%.*]], <i8 5, i8 3>
561561
; CHECK-NEXT: [[C:%.*]] = select <2 x i1> [[A]], <2 x i8> [[B]], <2 x i8> [[Y:%.*]]
562562
; CHECK-NEXT: ret <2 x i8> [[C]]
563563
;
@@ -567,7 +567,7 @@ define <2 x i8> @select_xor_icmp_vec_bad(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z)
567567
ret <2 x i8> %C
568568
}
569569

570-
; Value equivalence substitution is all-or-nothing, so needs a scalar compare.
570+
; Value equivalence substitution is invalid due to lane-crossing shufflevector.
571571

572572
define <2 x i32> @vec_select_no_equivalence(<2 x i32> %x) {
573573
; CHECK-LABEL: @vec_select_no_equivalence(

llvm/test/Transforms/InstCombine/select-value-equivalence.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ define <2 x i8> @select_icmp_insertelement_eq(<2 x i8> %x, <2 x i8> %y, i8 %i) {
55
; CHECK-LABEL: define <2 x i8> @select_icmp_insertelement_eq(
66
; CHECK-SAME: <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], i8 [[I:%.*]]) {
77
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i8> [[Y]], <i8 2, i8 2>
8-
; CHECK-NEXT: [[INSERT:%.*]] = insertelement <2 x i8> [[Y]], i8 0, i8 [[I]]
8+
; CHECK-NEXT: [[INSERT:%.*]] = insertelement <2 x i8> <i8 2, i8 2>, i8 0, i8 [[I]]
99
; CHECK-NEXT: [[RETVAL:%.*]] = select <2 x i1> [[CMP]], <2 x i8> [[INSERT]], <2 x i8> [[X]]
1010
; CHECK-NEXT: ret <2 x i8> [[RETVAL]]
1111
;
@@ -19,7 +19,7 @@ define <2 x i8> @select_icmp_insertelement_ne(<2 x i8> %x, <2 x i8> %y, i8 %i) {
1919
; CHECK-LABEL: define <2 x i8> @select_icmp_insertelement_ne(
2020
; CHECK-SAME: <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], i8 [[I:%.*]]) {
2121
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq <2 x i8> [[Y]], <i8 2, i8 2>
22-
; CHECK-NEXT: [[INSERT:%.*]] = insertelement <2 x i8> [[Y]], i8 0, i8 [[I]]
22+
; CHECK-NEXT: [[INSERT:%.*]] = insertelement <2 x i8> <i8 2, i8 2>, i8 0, i8 [[I]]
2323
; CHECK-NEXT: [[RETVAL:%.*]] = select <2 x i1> [[CMP_NOT]], <2 x i8> [[INSERT]], <2 x i8> [[X]]
2424
; CHECK-NEXT: ret <2 x i8> [[RETVAL]]
2525
;
@@ -46,7 +46,7 @@ define <4 x i8> @select_icmp_shufflevector_select(<4 x i8> %x, <4 x i8> %y, <4 x
4646
; CHECK-LABEL: define <4 x i8> @select_icmp_shufflevector_select(
4747
; CHECK-SAME: <4 x i8> [[X:%.*]], <4 x i8> [[Y:%.*]], <4 x i8> [[Z:%.*]]) {
4848
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x i8> [[Y]], <i8 2, i8 2, i8 2, i8 2>
49-
; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x i8> [[Z]], <4 x i8> [[Y]], <4 x i32> <i32 0, i32 5, i32 2, i32 7>
49+
; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x i8> [[Z]], <4 x i8> <i8 poison, i8 2, i8 poison, i8 2>, <4 x i32> <i32 0, i32 5, i32 2, i32 7>
5050
; CHECK-NEXT: [[RETVAL:%.*]] = select <4 x i1> [[CMP]], <4 x i8> [[SHUFFLE]], <4 x i8> [[X]]
5151
; CHECK-NEXT: ret <4 x i8> [[RETVAL]]
5252
;

0 commit comments

Comments
 (0)