Skip to content

Commit a78bbed

Browse files
author
git apple-llvm automerger
committed
Merge commit '22fe5c367336' from apple/master into swift/master-next
2 parents 504fda0 + 22fe5c3 commit a78bbed

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3909,18 +3909,21 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
39093909

39103910
/// Try to simplify a select instruction when its condition operand is a
39113911
/// floating-point comparison.
3912-
static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F) {
3912+
static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
3913+
const SimplifyQuery &Q) {
39133914
FCmpInst::Predicate Pred;
39143915
if (!match(Cond, m_FCmp(Pred, m_Specific(T), m_Specific(F))) &&
39153916
!match(Cond, m_FCmp(Pred, m_Specific(F), m_Specific(T))))
39163917
return nullptr;
39173918

3918-
// TODO: The transform may not be valid with -0.0. An incomplete way of
3919-
// testing for that possibility is to check if at least one operand is a
3920-
// non-zero constant.
3919+
// This transform is safe if we do not have (do not care about) -0.0 or if
3920+
// at least one operand is known to not be -0.0. Otherwise, the select can
3921+
// change the sign of a zero operand.
3922+
bool HasNoSignedZeros = Q.CxtI && isa<FPMathOperator>(Q.CxtI) &&
3923+
Q.CxtI->hasNoSignedZeros();
39213924
const APFloat *C;
3922-
if ((match(T, m_APFloat(C)) && C->isNonZero()) ||
3923-
(match(F, m_APFloat(C)) && C->isNonZero())) {
3925+
if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) ||
3926+
(match(F, m_APFloat(C)) && C->isNonZero())) {
39243927
// (T == F) ? T : F --> F
39253928
// (F == T) ? T : F --> F
39263929
if (Pred == FCmpInst::FCMP_OEQ)
@@ -3971,7 +3974,7 @@ static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
39713974
simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
39723975
return V;
39733976

3974-
if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal))
3977+
if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q))
39753978
return V;
39763979

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

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ define double @une_zero_swapped(double %x) {
104104

105105
define double @oeq_zero_nsz(double %x) {
106106
; CHECK-LABEL: @oeq_zero_nsz(
107-
; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq double [[X:%.*]], 0.000000e+00
108-
; CHECK-NEXT: [[COND:%.*]] = select nsz i1 [[CMP]], double [[X]], double 0.000000e+00
109-
; CHECK-NEXT: ret double [[COND]]
107+
; CHECK-NEXT: ret double 0.000000e+00
110108
;
111109
%cmp = fcmp oeq double %x, 0.0
112110
%cond = select nsz i1 %cmp, double %x, double 0.0
@@ -117,9 +115,7 @@ define double @oeq_zero_nsz(double %x) {
117115

118116
define float @oeq_zero_swapped_nsz(float %x) {
119117
; CHECK-LABEL: @oeq_zero_swapped_nsz(
120-
; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq float [[X:%.*]], 0.000000e+00
121-
; CHECK-NEXT: [[COND:%.*]] = select fast i1 [[CMP]], float 0.000000e+00, float [[X]]
122-
; CHECK-NEXT: ret float [[COND]]
118+
; CHECK-NEXT: ret float [[X:%.*]]
123119
;
124120
%cmp = fcmp oeq float %x, 0.0
125121
%cond = select fast i1 %cmp, float 0.0, float %x
@@ -130,9 +126,7 @@ define float @oeq_zero_swapped_nsz(float %x) {
130126

131127
define double @une_zero_nsz(double %x) {
132128
; CHECK-LABEL: @une_zero_nsz(
133-
; CHECK-NEXT: [[CMP:%.*]] = fcmp une double [[X:%.*]], 0.000000e+00
134-
; CHECK-NEXT: [[COND:%.*]] = select ninf nsz i1 [[CMP]], double [[X]], double 0.000000e+00
135-
; CHECK-NEXT: ret double [[COND]]
129+
; CHECK-NEXT: ret double [[X:%.*]]
136130
;
137131
%cmp = fcmp une double %x, 0.0
138132
%cond = select nsz ninf i1 %cmp, double %x, double 0.0
@@ -143,9 +137,7 @@ define double @une_zero_nsz(double %x) {
143137

144138
define <2 x double> @une_zero_swapped_nsz(<2 x double> %x) {
145139
; CHECK-LABEL: @une_zero_swapped_nsz(
146-
; CHECK-NEXT: [[CMP:%.*]] = fcmp une <2 x double> [[X:%.*]], zeroinitializer
147-
; CHECK-NEXT: [[COND:%.*]] = select nsz <2 x i1> [[CMP]], <2 x double> zeroinitializer, <2 x double> [[X]]
148-
; CHECK-NEXT: ret <2 x double> [[COND]]
140+
; CHECK-NEXT: ret <2 x double> zeroinitializer
149141
;
150142
%cmp = fcmp une <2 x double> %x, <double 0.0, double 0.0>
151143
%cond = select nsz <2 x i1> %cmp, <2 x double> <double 0.0, double 0.0>, <2 x double> %x
@@ -156,9 +148,7 @@ define <2 x double> @une_zero_swapped_nsz(<2 x double> %x) {
156148

157149
define double @oeq_nsz(double %x, double %y) {
158150
; CHECK-LABEL: @oeq_nsz(
159-
; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq double [[X:%.*]], [[Y:%.*]]
160-
; CHECK-NEXT: [[COND:%.*]] = select fast i1 [[CMP]], double [[X]], double [[Y]]
161-
; CHECK-NEXT: ret double [[COND]]
151+
; CHECK-NEXT: ret double [[Y:%.*]]
162152
;
163153
%cmp = fcmp oeq double %x, %y
164154
%cond = select fast i1 %cmp, double %x, double %y
@@ -169,9 +159,7 @@ define double @oeq_nsz(double %x, double %y) {
169159

170160
define <2 x float> @oeq_swapped_nsz(<2 x float> %x, <2 x float> %y) {
171161
; CHECK-LABEL: @oeq_swapped_nsz(
172-
; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq <2 x float> [[X:%.*]], [[Y:%.*]]
173-
; CHECK-NEXT: [[COND:%.*]] = select nnan nsz <2 x i1> [[CMP]], <2 x float> [[Y]], <2 x float> [[X]]
174-
; CHECK-NEXT: ret <2 x float> [[COND]]
162+
; CHECK-NEXT: ret <2 x float> [[X:%.*]]
175163
;
176164
%cmp = fcmp oeq <2 x float> %x, %y
177165
%cond = select nsz nnan <2 x i1> %cmp, <2 x float> %y, <2 x float> %x
@@ -182,9 +170,7 @@ define <2 x float> @oeq_swapped_nsz(<2 x float> %x, <2 x float> %y) {
182170

183171
define double @une_nsz(double %x, double %y) {
184172
; CHECK-LABEL: @une_nsz(
185-
; CHECK-NEXT: [[CMP:%.*]] = fcmp une double [[X:%.*]], [[Y:%.*]]
186-
; CHECK-NEXT: [[COND:%.*]] = select nsz i1 [[CMP]], double [[X]], double [[Y]]
187-
; CHECK-NEXT: ret double [[COND]]
173+
; CHECK-NEXT: ret double [[X:%.*]]
188174
;
189175
%cmp = fcmp une double %x, %y
190176
%cond = select nsz i1 %cmp, double %x, double %y
@@ -195,9 +181,7 @@ define double @une_nsz(double %x, double %y) {
195181

196182
define <2 x double> @une_swapped_nsz(<2 x double> %x, <2 x double> %y) {
197183
; CHECK-LABEL: @une_swapped_nsz(
198-
; CHECK-NEXT: [[CMP:%.*]] = fcmp une <2 x double> [[X:%.*]], [[Y:%.*]]
199-
; CHECK-NEXT: [[COND:%.*]] = select fast <2 x i1> [[CMP]], <2 x double> [[Y]], <2 x double> [[X]]
200-
; CHECK-NEXT: ret <2 x double> [[COND]]
184+
; CHECK-NEXT: ret <2 x double> [[Y:%.*]]
201185
;
202186
%cmp = fcmp une <2 x double> %x, %y
203187
%cond = select fast <2 x i1> %cmp, <2 x double> %y, <2 x double> %x

0 commit comments

Comments
 (0)