Skip to content

Commit d9bbccb

Browse files
committed
InstSimplify: support floating-point equivalences
Since cd16b07 (IR: introduce CmpInst::isEquivalence), there is now an isEquivalence routine in CmpInst that we can use to determine equivalence in simplifySelectWithICmpEq. Implement this, extending the code from integer-equalities to integer and floating-point equivalences.
1 parent 55e0a01 commit d9bbccb

File tree

3 files changed

+69
-77
lines changed

3 files changed

+69
-77
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,11 +4616,11 @@ static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
46164616
}
46174617

46184618
/// Try to simplify a select instruction when its condition operand is an
4619-
/// integer equality comparison.
4620-
static Value *simplifySelectWithICmpEq(Value *CmpLHS, Value *CmpRHS,
4621-
Value *TrueVal, Value *FalseVal,
4622-
const SimplifyQuery &Q,
4623-
unsigned MaxRecurse) {
4619+
/// integer equality or floating-point equivalence comparison.
4620+
static Value *simplifySelectWithEquivalence(Value *CmpLHS, Value *CmpRHS,
4621+
Value *TrueVal, Value *FalseVal,
4622+
const SimplifyQuery &Q,
4623+
unsigned MaxRecurse) {
46244624
if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef(),
46254625
/* AllowRefinement */ false,
46264626
/* DropFlags */ nullptr, MaxRecurse) == TrueVal)
@@ -4721,11 +4721,11 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47214721
// the arms of the select. See if substituting this value into the arm and
47224722
// simplifying the result yields the same value as the other arm.
47234723
if (Pred == ICmpInst::ICMP_EQ) {
4724-
if (Value *V = simplifySelectWithICmpEq(CmpLHS, CmpRHS, TrueVal, FalseVal,
4725-
Q, MaxRecurse))
4724+
if (Value *V = simplifySelectWithEquivalence(CmpLHS, CmpRHS, TrueVal,
4725+
FalseVal, Q, MaxRecurse))
47264726
return V;
4727-
if (Value *V = simplifySelectWithICmpEq(CmpRHS, CmpLHS, TrueVal, FalseVal,
4728-
Q, MaxRecurse))
4727+
if (Value *V = simplifySelectWithEquivalence(CmpRHS, CmpLHS, TrueVal,
4728+
FalseVal, Q, MaxRecurse))
47294729
return V;
47304730

47314731
Value *X;
@@ -4734,23 +4734,23 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47344734
if (match(CmpLHS, m_Or(m_Value(X), m_Value(Y))) &&
47354735
match(CmpRHS, m_Zero())) {
47364736
// (X | Y) == 0 implies X == 0 and Y == 0.
4737-
if (Value *V = simplifySelectWithICmpEq(X, CmpRHS, TrueVal, FalseVal, Q,
4738-
MaxRecurse))
4737+
if (Value *V = simplifySelectWithEquivalence(X, CmpRHS, TrueVal, FalseVal,
4738+
Q, MaxRecurse))
47394739
return V;
4740-
if (Value *V = simplifySelectWithICmpEq(Y, CmpRHS, TrueVal, FalseVal, Q,
4741-
MaxRecurse))
4740+
if (Value *V = simplifySelectWithEquivalence(Y, CmpRHS, TrueVal, FalseVal,
4741+
Q, MaxRecurse))
47424742
return V;
47434743
}
47444744

47454745
// select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways)
47464746
if (match(CmpLHS, m_And(m_Value(X), m_Value(Y))) &&
47474747
match(CmpRHS, m_AllOnes())) {
47484748
// (X & Y) == -1 implies X == -1 and Y == -1.
4749-
if (Value *V = simplifySelectWithICmpEq(X, CmpRHS, TrueVal, FalseVal, Q,
4750-
MaxRecurse))
4749+
if (Value *V = simplifySelectWithEquivalence(X, CmpRHS, TrueVal, FalseVal,
4750+
Q, MaxRecurse))
47514751
return V;
4752-
if (Value *V = simplifySelectWithICmpEq(Y, CmpRHS, TrueVal, FalseVal, Q,
4753-
MaxRecurse))
4752+
if (Value *V = simplifySelectWithEquivalence(Y, CmpRHS, TrueVal, FalseVal,
4753+
Q, MaxRecurse))
47544754
return V;
47554755
}
47564756
}
@@ -4761,28 +4761,54 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47614761
/// Try to simplify a select instruction when its condition operand is a
47624762
/// floating-point comparison.
47634763
static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
4764-
const SimplifyQuery &Q) {
4764+
const SimplifyQuery &Q,
4765+
unsigned MaxRecurse) {
47654766
FCmpInst::Predicate Pred;
4766-
if (!match(Cond, m_FCmp(Pred, m_Specific(T), m_Specific(F))) &&
4767-
!match(Cond, m_FCmp(Pred, m_Specific(F), m_Specific(T))))
4767+
Value *CmpLHS, *CmpRHS;
4768+
if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
4769+
return nullptr;
4770+
FCmpInst *I = cast<FCmpInst>(Cond);
4771+
4772+
bool IsEquiv = I->isEquivalence(),
4773+
IsInverseEquiv = I->isEquivalence(/*Invert=*/true);
4774+
4775+
if (IsInverseEquiv)
4776+
std::swap(T, F);
4777+
4778+
// Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
4779+
if (CmpLHS == F && CmpRHS == T)
4780+
std::swap(CmpLHS, CmpRHS);
4781+
4782+
// This transforms is safe if at least one operand is known to not be zero.
4783+
// Otherwise, the select can change the sign of a zero operand.
4784+
if (IsEquiv || IsInverseEquiv) {
4785+
if (Value *V =
4786+
simplifySelectWithEquivalence(CmpLHS, CmpRHS, T, F, Q, MaxRecurse))
4787+
return V;
4788+
if (Value *V =
4789+
simplifySelectWithEquivalence(CmpRHS, CmpLHS, T, F, Q, MaxRecurse))
4790+
return V;
4791+
4792+
// (T == F) ? T : F --> F
4793+
// (T != F) ? F : T --> F
4794+
return CmpLHS == T && CmpRHS == F ? F : nullptr;
4795+
}
4796+
4797+
if (CmpLHS != T || CmpRHS != F)
47684798
return nullptr;
47694799

4770-
// This transform is safe if we do not have (do not care about) -0.0 or if
4771-
// at least one operand is known to not be -0.0. Otherwise, the select can
4772-
// change the sign of a zero operand.
4800+
// This transform is also safe if we do not have (do not care about) -0.0.
47734801
bool HasNoSignedZeros =
47744802
Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros();
4775-
const APFloat *C;
4776-
if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) ||
4777-
(match(F, m_APFloat(C)) && C->isNonZero())) {
4803+
if (HasNoSignedZeros) {
47784804
// (T == F) ? T : F --> F
4779-
// (F == T) ? T : F --> F
4780-
if (Pred == FCmpInst::FCMP_OEQ)
4805+
if (Pred == FCmpInst::FCMP_OEQ ||
4806+
(Pred == FCmpInst::FCMP_UEQ && I->hasNoNaNs()))
47814807
return F;
47824808

47834809
// (T != F) ? T : F --> T
4784-
// (F != T) ? T : F --> T
4785-
if (Pred == FCmpInst::FCMP_UNE)
4810+
if (Pred == FCmpInst::FCMP_UNE ||
4811+
(Pred == FCmpInst::FCMP_ONE && I->hasNoNaNs()))
47864812
return T;
47874813
}
47884814

@@ -4955,7 +4981,7 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
49554981
simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
49564982
return V;
49574983

4958-
if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q))
4984+
if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse))
49594985
return V;
49604986

49614987
if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))

llvm/test/Transforms/InstSimplify/fcmp-select.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ define double @ueq_zero_nsz(double %x) {
124124

125125
define double @ueq_zero_nsz_nnan(double %x) {
126126
; CHECK-LABEL: @ueq_zero_nsz_nnan(
127-
; CHECK-NEXT: [[CMP:%.*]] = fcmp nnan ueq double [[X:%.*]], 0.000000e+00
128-
; CHECK-NEXT: [[COND:%.*]] = select nsz i1 [[CMP]], double [[X]], double 0.000000e+00
129-
; CHECK-NEXT: ret double [[COND]]
127+
; CHECK-NEXT: ret double 0.000000e+00
130128
;
131129
%cmp = fcmp nnan ueq double %x, 0.0
132130
%cond = select nsz i1 %cmp, double %x, double 0.0
@@ -168,9 +166,7 @@ define double @one_zero_nsz(double %x) {
168166

169167
define double @one_zero_nsz_nnan(double %x) {
170168
; CHECK-LABEL: @one_zero_nsz_nnan(
171-
; CHECK-NEXT: [[CMP:%.*]] = fcmp nnan one double [[X:%.*]], 0.000000e+00
172-
; CHECK-NEXT: [[COND:%.*]] = select nsz i1 [[CMP]], double [[X]], double 0.000000e+00
173-
; CHECK-NEXT: ret double [[COND]]
169+
; CHECK-NEXT: ret double [[COND:%.*]]
174170
;
175171
%cmp = fcmp nnan one double %x, 0.0
176172
%cond = select nsz i1 %cmp, double %x, double 0.0

llvm/test/Transforms/InstSimplify/select-equivalence-fp.ll

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
define float @select_fcmp_fsub_oeq(float %x, float %y) {
55
; CHECK-LABEL: @select_fcmp_fsub_oeq(
6-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp oeq float [[Y:%.*]], 2.000000e+00
7-
; CHECK-NEXT: [[FADD:%.*]] = fsub float [[Y]], 2.000000e+00
8-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float [[FADD]], float 0.000000e+00
9-
; CHECK-NEXT: ret float [[SEL]]
6+
; CHECK-NEXT: ret float 0.000000e+00
107
;
118
%fcmp = fcmp oeq float %y, 2.
129
%fadd = fsub float %y, 2.
@@ -42,10 +39,7 @@ define float @select_fcmp_fsub_ueq(float %x, float %y) {
4239

4340
define float @select_fcmp_fsub_ueq_nnan(float %x, float %y) {
4441
; CHECK-LABEL: @select_fcmp_fsub_ueq_nnan(
45-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp nnan ueq float [[Y:%.*]], 2.000000e+00
46-
; CHECK-NEXT: [[FADD:%.*]] = fsub float [[Y]], 2.000000e+00
47-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float [[FADD]], float 0.000000e+00
48-
; CHECK-NEXT: ret float [[SEL]]
42+
; CHECK-NEXT: ret float 0.000000e+00
4943
;
5044
%fcmp = fcmp nnan ueq float %y, 2.
5145
%fadd = fsub float %y, 2.
@@ -55,10 +49,7 @@ define float @select_fcmp_fsub_ueq_nnan(float %x, float %y) {
5549

5650
define float @select_fcmp_fsub_une(float %x, float %y) {
5751
; CHECK-LABEL: @select_fcmp_fsub_une(
58-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp une float [[Y:%.*]], 2.000000e+00
59-
; CHECK-NEXT: [[FADD:%.*]] = fsub float [[Y]], 2.000000e+00
60-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float 0.000000e+00, float [[FADD]]
61-
; CHECK-NEXT: ret float [[SEL]]
52+
; CHECK-NEXT: ret float 0.000000e+00
6253
;
6354
%fcmp = fcmp une float %y, 2.
6455
%fadd = fsub float %y, 2.
@@ -94,10 +85,7 @@ define float @select_fcmp_fsub_one(float %x, float %y) {
9485

9586
define float @select_fcmp_fsub_one_nnan(float %x, float %y) {
9687
; CHECK-LABEL: @select_fcmp_fsub_one_nnan(
97-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp nnan one float [[Y:%.*]], 2.000000e+00
98-
; CHECK-NEXT: [[FADD:%.*]] = fsub float [[Y]], 2.000000e+00
99-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float 0.000000e+00, float [[FADD]]
100-
; CHECK-NEXT: ret float [[SEL]]
88+
; CHECK-NEXT: ret float 0.000000e+00
10189
;
10290
%fcmp = fcmp nnan one float %y, 2.
10391
%fadd = fsub float %y, 2.
@@ -107,10 +95,7 @@ define float @select_fcmp_fsub_one_nnan(float %x, float %y) {
10795

10896
define float @select_fcmp_fadd(float %x, float %y) {
10997
; CHECK-LABEL: @select_fcmp_fadd(
110-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp oeq float [[Y:%.*]], 2.000000e+00
111-
; CHECK-NEXT: [[FADD:%.*]] = fadd float [[Y]], 2.000000e+00
112-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float [[FADD]], float 4.000000e+00
113-
; CHECK-NEXT: ret float [[SEL]]
98+
; CHECK-NEXT: ret float 4.000000e+00
11499
;
115100
%fcmp = fcmp oeq float %y, 2.
116101
%fadd = fadd float %y, 2.
@@ -120,10 +105,7 @@ define float @select_fcmp_fadd(float %x, float %y) {
120105

121106
define <2 x float> @select_fcmp_fadd_vec(<2 x float> %x, <2 x float> %y) {
122107
; CHECK-LABEL: @select_fcmp_fadd_vec(
123-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp oeq <2 x float> [[Y:%.*]], <float 2.000000e+00, float 2.000000e+00>
124-
; CHECK-NEXT: [[FADD:%.*]] = fadd <2 x float> [[Y]], <float 2.000000e+00, float 2.000000e+00>
125-
; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[FCMP]], <2 x float> [[FADD]], <2 x float> <float 4.000000e+00, float 4.000000e+00>
126-
; CHECK-NEXT: ret <2 x float> [[SEL]]
108+
; CHECK-NEXT: ret <2 x float> splat (float 4.000000e+00)
127109
;
128110
%fcmp = fcmp oeq <2 x float> %y, <float 2., float 2.>
129111
%fadd = fadd <2 x float> %y, <float 2., float 2.>
@@ -134,10 +116,7 @@ define <2 x float> @select_fcmp_fadd_vec(<2 x float> %x, <2 x float> %y) {
134116

135117
define float @select_fcmp_fdiv(float %x, float %y) {
136118
; CHECK-LABEL: @select_fcmp_fdiv(
137-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp oeq float [[Y:%.*]], 2.000000e+00
138-
; CHECK-NEXT: [[FDIV:%.*]] = fdiv float [[Y]], 2.000000e+00
139-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float [[FDIV]], float 1.000000e+00
140-
; CHECK-NEXT: ret float [[SEL]]
119+
; CHECK-NEXT: ret float 1.000000e+00
141120
;
142121
%fcmp = fcmp oeq float %y, 2.
143122
%fdiv = fdiv float %y, 2.
@@ -147,10 +126,7 @@ define float @select_fcmp_fdiv(float %x, float %y) {
147126

148127
define float @select_fcmp_frem(float %x, float %y) {
149128
; CHECK-LABEL: @select_fcmp_frem(
150-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp oeq float [[Y:%.*]], 3.000000e+00
151-
; CHECK-NEXT: [[FREM:%.*]] = frem float [[Y]], 2.000000e+00
152-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[FCMP]], float [[FREM]], float 1.000000e+00
153-
; CHECK-NEXT: ret float [[SEL]]
129+
; CHECK-NEXT: ret float 1.000000e+00
154130
;
155131
%fcmp = fcmp oeq float %y, 3.
156132
%frem = frem float %y, 2.
@@ -160,10 +136,7 @@ define float @select_fcmp_frem(float %x, float %y) {
160136

161137
define <2 x float> @select_fcmp_insertelement(<2 x float> %x, <2 x float> %y) {
162138
; CHECK-LABEL: @select_fcmp_insertelement(
163-
; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq <2 x float> [[Y:%.*]], <float 2.000000e+00, float 2.000000e+00>
164-
; CHECK-NEXT: [[INSERT:%.*]] = insertelement <2 x float> [[Y]], float 4.000000e+00, i64 0
165-
; CHECK-NEXT: [[RETVAL:%.*]] = select <2 x i1> [[CMP]], <2 x float> [[INSERT]], <2 x float> <float 4.000000e+00, float 2.000000e+00>
166-
; CHECK-NEXT: ret <2 x float> [[RETVAL]]
139+
; CHECK-NEXT: ret <2 x float> <float 4.000000e+00, float 2.000000e+00>
167140
;
168141
%fcmp = fcmp oeq <2 x float> %y, <float 2., float 2.>
169142
%insert = insertelement <2 x float> %y, float 4., i64 0
@@ -173,10 +146,7 @@ define <2 x float> @select_fcmp_insertelement(<2 x float> %x, <2 x float> %y) {
173146

174147
define <4 x float> @select_fcmp_shufflevector_select(<4 x float> %x, <4 x float> %y) {
175148
; CHECK-LABEL: @select_fcmp_shufflevector_select(
176-
; CHECK-NEXT: [[FCMP:%.*]] = fcmp oeq <4 x float> [[Y:%.*]], <float 2.000000e+00, float 2.000000e+00, float 2.000000e+00, float 2.000000e+00>
177-
; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x float> [[Y]], <4 x float> poison, <4 x i32> <i32 4, i32 1, i32 6, i32 3>
178-
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[FCMP]], <4 x float> [[SHUFFLE]], <4 x float> <float poison, float 2.000000e+00, float poison, float 2.000000e+00>
179-
; CHECK-NEXT: ret <4 x float> [[SEL]]
149+
; CHECK-NEXT: ret <4 x float> <float poison, float 2.000000e+00, float poison, float 2.000000e+00>
180150
;
181151
%fcmp = fcmp oeq <4 x float> %y, <float 2., float 2., float 2., float 2.>
182152
%shuffle = shufflevector <4 x float> %y, <4 x float> poison, <4 x i32> <i32 4, i32 1, i32 6, i32 3>

0 commit comments

Comments
 (0)