@@ -7286,8 +7286,9 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
7286
7286
Instruction *InstCombinerImpl::foldFCmpIntToFPConst (FCmpInst &I,
7287
7287
Instruction *LHSI,
7288
7288
Constant *RHSC) {
7289
- if (!isa<ConstantFP>(RHSC)) return nullptr ;
7290
- const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF ();
7289
+ const APFloat *RHS;
7290
+ if (!match (RHSC, m_APFloat (RHS)))
7291
+ return nullptr ;
7291
7292
7292
7293
// Get the width of the mantissa. We don't want to hack on conversions that
7293
7294
// might lose information from the integer, e.g. "i64 -> float"
@@ -7302,20 +7303,20 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7302
7303
FCmpInst::Predicate P = I.getPredicate ();
7303
7304
bool IsExact = false ;
7304
7305
APSInt RHSCvt (IntWidth, LHSUnsigned);
7305
- RHS. convertToInteger (RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7306
+ RHS-> convertToInteger (RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
7306
7307
7307
7308
// If the floating point constant isn't an integer value, we know if we will
7308
7309
// ever compare equal / not equal to it.
7309
7310
if (!IsExact) {
7310
7311
// TODO: Can never be -0.0 and other non-representable values
7311
- APFloat RHSRoundInt (RHS);
7312
+ APFloat RHSRoundInt (* RHS);
7312
7313
RHSRoundInt.roundToIntegral (APFloat::rmNearestTiesToEven);
7313
- if (RHS != RHSRoundInt) {
7314
+ if (* RHS != RHSRoundInt) {
7314
7315
if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)
7315
- return replaceInstUsesWith (I, Builder. getFalse ());
7316
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7316
7317
7317
7318
assert (P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);
7318
- return replaceInstUsesWith (I, Builder. getTrue ());
7319
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7319
7320
}
7320
7321
}
7321
7322
@@ -7332,9 +7333,9 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7332
7333
// to distinguish it from one less than that value.
7333
7334
if ((int )IntWidth > MantissaWidth) {
7334
7335
// Conversion would lose accuracy. Check if loss can impact comparison.
7335
- int Exp = ilogb (RHS);
7336
+ int Exp = ilogb (* RHS);
7336
7337
if (Exp == APFloat::IEK_Inf) {
7337
- int MaxExponent = ilogb (APFloat::getLargest (RHS. getSemantics ()));
7338
+ int MaxExponent = ilogb (APFloat::getLargest (RHS-> getSemantics ()));
7338
7339
if (MaxExponent < (int )IntWidth - !LHSUnsigned)
7339
7340
// Conversion could create infinity.
7340
7341
return nullptr ;
@@ -7350,7 +7351,7 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7350
7351
// Otherwise, we can potentially simplify the comparison. We know that it
7351
7352
// will always come through as an integer value and we know the constant is
7352
7353
// not a NAN (it would have been previously simplified).
7353
- assert (!RHS. isNaN () && " NaN comparison not already folded!" );
7354
+ assert (!RHS-> isNaN () && " NaN comparison not already folded!" );
7354
7355
7355
7356
ICmpInst::Predicate Pred;
7356
7357
switch (I.getPredicate ()) {
@@ -7380,9 +7381,9 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7380
7381
Pred = ICmpInst::ICMP_NE;
7381
7382
break ;
7382
7383
case FCmpInst::FCMP_ORD:
7383
- return replaceInstUsesWith (I, Builder. getTrue ());
7384
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7384
7385
case FCmpInst::FCMP_UNO:
7385
- return replaceInstUsesWith (I, Builder. getFalse ());
7386
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7386
7387
}
7387
7388
7388
7389
// Now we know that the APFloat is a normal number, zero or inf.
@@ -7392,50 +7393,50 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7392
7393
if (!LHSUnsigned) {
7393
7394
// If the RHS value is > SignedMax, fold the comparison. This handles +INF
7394
7395
// and large values.
7395
- APFloat SMax (RHS. getSemantics ());
7396
+ APFloat SMax (RHS-> getSemantics ());
7396
7397
SMax.convertFromAPInt (APInt::getSignedMaxValue (IntWidth), true ,
7397
7398
APFloat::rmNearestTiesToEven);
7398
- if (SMax < RHS) { // smax < 13123.0
7399
+ if (SMax < * RHS) { // smax < 13123.0
7399
7400
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
7400
7401
Pred == ICmpInst::ICMP_SLE)
7401
- return replaceInstUsesWith (I, Builder. getTrue ());
7402
- return replaceInstUsesWith (I, Builder. getFalse ());
7402
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7403
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7403
7404
}
7404
7405
} else {
7405
7406
// If the RHS value is > UnsignedMax, fold the comparison. This handles
7406
7407
// +INF and large values.
7407
- APFloat UMax (RHS. getSemantics ());
7408
+ APFloat UMax (RHS-> getSemantics ());
7408
7409
UMax.convertFromAPInt (APInt::getMaxValue (IntWidth), false ,
7409
7410
APFloat::rmNearestTiesToEven);
7410
- if (UMax < RHS) { // umax < 13123.0
7411
+ if (UMax < * RHS) { // umax < 13123.0
7411
7412
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
7412
7413
Pred == ICmpInst::ICMP_ULE)
7413
- return replaceInstUsesWith (I, Builder. getTrue ());
7414
- return replaceInstUsesWith (I, Builder. getFalse ());
7414
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7415
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7415
7416
}
7416
7417
}
7417
7418
7418
7419
if (!LHSUnsigned) {
7419
7420
// See if the RHS value is < SignedMin.
7420
- APFloat SMin (RHS. getSemantics ());
7421
+ APFloat SMin (RHS-> getSemantics ());
7421
7422
SMin.convertFromAPInt (APInt::getSignedMinValue (IntWidth), true ,
7422
7423
APFloat::rmNearestTiesToEven);
7423
- if (SMin > RHS) { // smin > 12312.0
7424
+ if (SMin > * RHS) { // smin > 12312.0
7424
7425
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
7425
7426
Pred == ICmpInst::ICMP_SGE)
7426
- return replaceInstUsesWith (I, Builder. getTrue ());
7427
- return replaceInstUsesWith (I, Builder. getFalse ());
7427
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7428
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7428
7429
}
7429
7430
} else {
7430
7431
// See if the RHS value is < UnsignedMin.
7431
- APFloat UMin (RHS. getSemantics ());
7432
+ APFloat UMin (RHS-> getSemantics ());
7432
7433
UMin.convertFromAPInt (APInt::getMinValue (IntWidth), false ,
7433
7434
APFloat::rmNearestTiesToEven);
7434
- if (UMin > RHS) { // umin > 12312.0
7435
+ if (UMin > * RHS) { // umin > 12312.0
7435
7436
if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
7436
7437
Pred == ICmpInst::ICMP_UGE)
7437
- return replaceInstUsesWith (I, Builder. getTrue ());
7438
- return replaceInstUsesWith (I, Builder. getFalse ());
7438
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7439
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7439
7440
}
7440
7441
}
7441
7442
@@ -7445,66 +7446,66 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7445
7446
// Don't do this for zero, because -0.0 is not fractional.
7446
7447
APSInt RHSInt (IntWidth, LHSUnsigned);
7447
7448
bool IsExact;
7448
- RHS. convertToInteger (RHSInt, APFloat::rmTowardZero, &IsExact);
7449
- if (!RHS. isZero ()) {
7449
+ RHS-> convertToInteger (RHSInt, APFloat::rmTowardZero, &IsExact);
7450
+ if (!RHS-> isZero ()) {
7450
7451
if (!IsExact) {
7451
7452
// If we had a comparison against a fractional value, we have to adjust
7452
7453
// the compare predicate and sometimes the value. RHSC is rounded towards
7453
7454
// zero at this point.
7454
7455
switch (Pred) {
7455
7456
default : llvm_unreachable (" Unexpected integer comparison!" );
7456
7457
case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
7457
- return replaceInstUsesWith (I, Builder. getTrue ());
7458
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7458
7459
case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
7459
- return replaceInstUsesWith (I, Builder. getFalse ());
7460
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7460
7461
case ICmpInst::ICMP_ULE:
7461
7462
// (float)int <= 4.4 --> int <= 4
7462
7463
// (float)int <= -4.4 --> false
7463
- if (RHS. isNegative ())
7464
- return replaceInstUsesWith (I, Builder. getFalse ());
7464
+ if (RHS-> isNegative ())
7465
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7465
7466
break ;
7466
7467
case ICmpInst::ICMP_SLE:
7467
7468
// (float)int <= 4.4 --> int <= 4
7468
7469
// (float)int <= -4.4 --> int < -4
7469
- if (RHS. isNegative ())
7470
+ if (RHS-> isNegative ())
7470
7471
Pred = ICmpInst::ICMP_SLT;
7471
7472
break ;
7472
7473
case ICmpInst::ICMP_ULT:
7473
7474
// (float)int < -4.4 --> false
7474
7475
// (float)int < 4.4 --> int <= 4
7475
- if (RHS. isNegative ())
7476
- return replaceInstUsesWith (I, Builder. getFalse ());
7476
+ if (RHS-> isNegative ())
7477
+ return replaceInstUsesWith (I, ConstantInt:: getFalse (I. getType () ));
7477
7478
Pred = ICmpInst::ICMP_ULE;
7478
7479
break ;
7479
7480
case ICmpInst::ICMP_SLT:
7480
7481
// (float)int < -4.4 --> int < -4
7481
7482
// (float)int < 4.4 --> int <= 4
7482
- if (!RHS. isNegative ())
7483
+ if (!RHS-> isNegative ())
7483
7484
Pred = ICmpInst::ICMP_SLE;
7484
7485
break ;
7485
7486
case ICmpInst::ICMP_UGT:
7486
7487
// (float)int > 4.4 --> int > 4
7487
7488
// (float)int > -4.4 --> true
7488
- if (RHS. isNegative ())
7489
- return replaceInstUsesWith (I, Builder. getTrue ());
7489
+ if (RHS-> isNegative ())
7490
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7490
7491
break ;
7491
7492
case ICmpInst::ICMP_SGT:
7492
7493
// (float)int > 4.4 --> int > 4
7493
7494
// (float)int > -4.4 --> int >= -4
7494
- if (RHS. isNegative ())
7495
+ if (RHS-> isNegative ())
7495
7496
Pred = ICmpInst::ICMP_SGE;
7496
7497
break ;
7497
7498
case ICmpInst::ICMP_UGE:
7498
7499
// (float)int >= -4.4 --> true
7499
7500
// (float)int >= 4.4 --> int > 4
7500
- if (RHS. isNegative ())
7501
- return replaceInstUsesWith (I, Builder. getTrue ());
7501
+ if (RHS-> isNegative ())
7502
+ return replaceInstUsesWith (I, ConstantInt:: getTrue (I. getType () ));
7502
7503
Pred = ICmpInst::ICMP_UGT;
7503
7504
break ;
7504
7505
case ICmpInst::ICMP_SGE:
7505
7506
// (float)int >= -4.4 --> int >= -4
7506
7507
// (float)int >= 4.4 --> int > 4
7507
- if (!RHS. isNegative ())
7508
+ if (!RHS-> isNegative ())
7508
7509
Pred = ICmpInst::ICMP_SGT;
7509
7510
break ;
7510
7511
}
@@ -7513,7 +7514,8 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
7513
7514
7514
7515
// Lower this FP comparison into an appropriate integer version of the
7515
7516
// comparison.
7516
- return new ICmpInst (Pred, LHSI->getOperand (0 ), Builder.getInt (RHSInt));
7517
+ return new ICmpInst (Pred, LHSI->getOperand (0 ),
7518
+ ConstantInt::get (LHSI->getOperand (0 )->getType (), RHSInt));
7517
7519
}
7518
7520
7519
7521
// / Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
0 commit comments