Skip to content

Commit 8e2ff38

Browse files
rotaterighttstellar
authored andcommitted
[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. (cherry picked from commit e2a0f51)
1 parent 266c82f commit 8e2ff38

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,10 +4127,12 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
41274127
TrueVal, FalseVal))
41284128
return V;
41294129

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

llvm/test/Transforms/InstSimplify/select.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,10 @@ declare i32 @llvm.ctpop.i32(i32)
983983

984984
define <2 x i32> @vec_select_no_equivalence(<2 x i32> %x, <2 x i32> %y) {
985985
; CHECK-LABEL: @vec_select_no_equivalence(
986-
; CHECK-NEXT: ret <2 x i32> zeroinitializer
986+
; CHECK-NEXT: [[X10:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> <i32 1, i32 0>
987+
; CHECK-NEXT: [[COND:%.*]] = icmp eq <2 x i32> [[X]], zeroinitializer
988+
; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[COND]], <2 x i32> [[X10]], <2 x i32> zeroinitializer
989+
; CHECK-NEXT: ret <2 x i32> [[S]]
987990
;
988991
%x10 = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> <i32 1, i32 0>
989992
%cond = icmp eq <2 x i32> %x, zeroinitializer

0 commit comments

Comments
 (0)