Skip to content

Commit e2a0f51

Browse files
committed
[InstSimplify] fix potential miscompile in select value equivalence
This is the sibling fix to c590a98 - as there, we can't subsitute a vector value the equality compare replacement that we are trying requires that the comparison is true for the entire value. Vector select can be partly true/false.
1 parent 78e5cf6 commit e2a0f51

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -4149,10 +4149,12 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
41494149
TrueVal, FalseVal))
41504150
return V;
41514151

4152-
// If we have an equality comparison, then we know the value in one of the
4153-
// arms of the select. See if substituting this value into the arm and
4152+
// If we have a scalar equality comparison, then we know the value in one of
4153+
// the arms of the select. See if substituting this value into the arm and
41544154
// simplifying the result yields the same value as the other arm.
4155-
if (Pred == ICmpInst::ICMP_EQ) {
4155+
// Note that the equivalence/replacement opportunity does not hold for vectors
4156+
// because each element of a vector select is chosen independently.
4157+
if (Pred == ICmpInst::ICMP_EQ && !CondVal->getType()->isVectorTy()) {
41564158
if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q,
41574159
/* AllowRefinement */ false, MaxRecurse) ==
41584160
TrueVal ||

llvm/test/Transforms/InstSimplify/select.ll

+4-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,10 @@ declare i32 @llvm.ctpop.i32(i32)
10291029

10301030
define <2 x i32> @vec_select_no_equivalence(<2 x i32> %x, <2 x i32> %y) {
10311031
; CHECK-LABEL: @vec_select_no_equivalence(
1032-
; CHECK-NEXT: ret <2 x i32> zeroinitializer
1032+
; CHECK-NEXT: [[X10:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> <i32 1, i32 0>
1033+
; CHECK-NEXT: [[COND:%.*]] = icmp eq <2 x i32> [[X]], zeroinitializer
1034+
; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[COND]], <2 x i32> [[X10]], <2 x i32> zeroinitializer
1035+
; CHECK-NEXT: ret <2 x i32> [[S]]
10331036
;
10341037
%x10 = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> <i32 1, i32 0>
10351038
%cond = icmp eq <2 x i32> %x, zeroinitializer

0 commit comments

Comments
 (0)