Skip to content

Commit 1b3cc4e

Browse files
committed
[ValueTracking] Use SimplifyQuery for the overflow APIs (NFC)
Accept a SimplifyQuery instead of an unpacked list of arguments.
1 parent 500a6c9 commit 1b3cc4e

File tree

6 files changed

+86
-127
lines changed

6 files changed

+86
-127
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/ArrayRef.h"
1818
#include "llvm/ADT/SmallSet.h"
19+
#include "llvm/Analysis/SimplifyQuery.h"
1920
#include "llvm/IR/Constants.h"
2021
#include "llvm/IR/DataLayout.h"
2122
#include "llvm/IR/FMF.h"
@@ -39,7 +40,6 @@ struct KnownBits;
3940
class Loop;
4041
class LoopInfo;
4142
class MDNode;
42-
struct SimplifyQuery;
4343
class StringRef;
4444
class TargetLibraryInfo;
4545
class Value;
@@ -851,44 +851,20 @@ enum class OverflowResult {
851851
};
852852

853853
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS,
854-
const DataLayout &DL,
855-
AssumptionCache *AC,
856-
const Instruction *CxtI,
857-
const DominatorTree *DT,
858-
bool UseInstrInfo = true);
854+
const SimplifyQuery &SQ);
859855
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
860-
const DataLayout &DL,
861-
AssumptionCache *AC,
862-
const Instruction *CxtI,
863-
const DominatorTree *DT,
864-
bool UseInstrInfo = true);
856+
const SimplifyQuery &SQ);
865857
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, const Value *RHS,
866-
const DataLayout &DL,
867-
AssumptionCache *AC,
868-
const Instruction *CxtI,
869-
const DominatorTree *DT,
870-
bool UseInstrInfo = true);
858+
const SimplifyQuery &SQ);
871859
OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS,
872-
const DataLayout &DL,
873-
AssumptionCache *AC = nullptr,
874-
const Instruction *CxtI = nullptr,
875-
const DominatorTree *DT = nullptr);
860+
const SimplifyQuery &SQ);
876861
/// This version also leverages the sign bit of Add if known.
877862
OverflowResult computeOverflowForSignedAdd(const AddOperator *Add,
878-
const DataLayout &DL,
879-
AssumptionCache *AC = nullptr,
880-
const Instruction *CxtI = nullptr,
881-
const DominatorTree *DT = nullptr);
863+
const SimplifyQuery &SQ);
882864
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS,
883-
const DataLayout &DL,
884-
AssumptionCache *AC,
885-
const Instruction *CxtI,
886-
const DominatorTree *DT);
865+
const SimplifyQuery &SQ);
887866
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
888-
const DataLayout &DL,
889-
AssumptionCache *AC,
890-
const Instruction *CxtI,
891-
const DominatorTree *DT);
867+
const SimplifyQuery &SQ);
892868

893869
/// Returns true if the arithmetic part of the \p WO 's result is
894870
/// used only along the paths control dependent on the computation

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,34 +500,40 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
500500
OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
501501
const Value *RHS,
502502
const Instruction *CxtI) const {
503-
return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
503+
return llvm::computeOverflowForUnsignedMul(LHS, RHS,
504+
SQ.getWithInstruction(CxtI));
504505
}
505506

506507
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
507508
const Instruction *CxtI) const {
508-
return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
509+
return llvm::computeOverflowForSignedMul(LHS, RHS,
510+
SQ.getWithInstruction(CxtI));
509511
}
510512

511513
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
512514
const Value *RHS,
513515
const Instruction *CxtI) const {
514-
return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
516+
return llvm::computeOverflowForUnsignedAdd(LHS, RHS,
517+
SQ.getWithInstruction(CxtI));
515518
}
516519

517520
OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS,
518521
const Instruction *CxtI) const {
519-
return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
522+
return llvm::computeOverflowForSignedAdd(LHS, RHS,
523+
SQ.getWithInstruction(CxtI));
520524
}
521525

522526
OverflowResult computeOverflowForUnsignedSub(const Value *LHS,
523527
const Value *RHS,
524528
const Instruction *CxtI) const {
525-
return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
529+
return llvm::computeOverflowForUnsignedSub(LHS, RHS,
530+
SQ.getWithInstruction(CxtI));
526531
}
527532

528533
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
529534
const Instruction *CxtI) const {
530-
return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
535+
return llvm::computeOverflowForSignedSub(LHS, RHS,
536+
SQ.getWithInstruction(CxtI));
531537
}
532538

533539
virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 53 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6245,37 +6245,30 @@ static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
62456245
}
62466246

62476247
/// Combine constant ranges from computeConstantRange() and computeKnownBits().
6248-
static ConstantRange computeConstantRangeIncludingKnownBits(
6249-
const Value *V, bool ForSigned, const DataLayout &DL, AssumptionCache *AC,
6250-
const Instruction *CxtI, const DominatorTree *DT,
6251-
bool UseInstrInfo = true) {
6252-
KnownBits Known =
6253-
computeKnownBits(V, DL, /*Depth=*/0, AC, CxtI, DT, UseInstrInfo);
6248+
static ConstantRange
6249+
computeConstantRangeIncludingKnownBits(const Value *V, bool ForSigned,
6250+
const SimplifyQuery &SQ) {
6251+
KnownBits Known = ::computeKnownBits(V, /*Depth=*/0, SQ);
62546252
ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
6255-
ConstantRange CR2 = computeConstantRange(V, ForSigned, UseInstrInfo);
6253+
ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
62566254
ConstantRange::PreferredRangeType RangeType =
62576255
ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;
62586256
return CR1.intersectWith(CR2, RangeType);
62596257
}
62606258

6261-
OverflowResult llvm::computeOverflowForUnsignedMul(
6262-
const Value *LHS, const Value *RHS, const DataLayout &DL,
6263-
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
6264-
bool UseInstrInfo) {
6265-
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
6266-
UseInstrInfo);
6267-
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
6268-
UseInstrInfo);
6259+
OverflowResult llvm::computeOverflowForUnsignedMul(const Value *LHS,
6260+
const Value *RHS,
6261+
const SimplifyQuery &SQ) {
6262+
KnownBits LHSKnown = ::computeKnownBits(LHS, /*Depth=*/0, SQ);
6263+
KnownBits RHSKnown = ::computeKnownBits(RHS, /*Depth=*/0, SQ);
62696264
ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
62706265
ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
62716266
return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
62726267
}
62736268

6274-
OverflowResult
6275-
llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
6276-
const DataLayout &DL, AssumptionCache *AC,
6277-
const Instruction *CxtI,
6278-
const DominatorTree *DT, bool UseInstrInfo) {
6269+
OverflowResult llvm::computeOverflowForSignedMul(const Value *LHS,
6270+
const Value *RHS,
6271+
const SimplifyQuery &SQ) {
62796272
// Multiplying n * m significant bits yields a result of n + m significant
62806273
// bits. If the total number of significant bits does not exceed the
62816274
// result bit width (minus 1), there is no overflow.
@@ -6286,8 +6279,8 @@ llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
62866279

62876280
// Note that underestimating the number of sign bits gives a more
62886281
// conservative answer.
6289-
unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) +
6290-
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT);
6282+
unsigned SignBits =
6283+
::ComputeNumSignBits(LHS, 0, SQ) + ::ComputeNumSignBits(RHS, 0, SQ);
62916284

62926285
// First handle the easy case: if we have enough sign bits there's
62936286
// definitely no overflow.
@@ -6304,34 +6297,28 @@ llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
63046297
// product is exactly the minimum negative number.
63056298
// E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
63066299
// For simplicity we just check if at least one side is not negative.
6307-
KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
6308-
UseInstrInfo);
6309-
KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
6310-
UseInstrInfo);
6300+
KnownBits LHSKnown = ::computeKnownBits(LHS, /*Depth=*/0, SQ);
6301+
KnownBits RHSKnown = ::computeKnownBits(RHS, /*Depth=*/0, SQ);
63116302
if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
63126303
return OverflowResult::NeverOverflows;
63136304
}
63146305
return OverflowResult::MayOverflow;
63156306
}
63166307

6317-
OverflowResult llvm::computeOverflowForUnsignedAdd(
6318-
const Value *LHS, const Value *RHS, const DataLayout &DL,
6319-
AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
6320-
bool UseInstrInfo) {
6321-
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
6322-
LHS, /*ForSigned=*/false, DL, AC, CxtI, DT, UseInstrInfo);
6323-
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
6324-
RHS, /*ForSigned=*/false, DL, AC, CxtI, DT, UseInstrInfo);
6308+
OverflowResult llvm::computeOverflowForUnsignedAdd(const Value *LHS,
6309+
const Value *RHS,
6310+
const SimplifyQuery &SQ) {
6311+
ConstantRange LHSRange =
6312+
computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6313+
ConstantRange RHSRange =
6314+
computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
63256315
return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
63266316
}
63276317

63286318
static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
63296319
const Value *RHS,
63306320
const AddOperator *Add,
6331-
const DataLayout &DL,
6332-
AssumptionCache *AC,
6333-
const Instruction *CxtI,
6334-
const DominatorTree *DT) {
6321+
const SimplifyQuery &SQ) {
63356322
if (Add && Add->hasNoSignedWrap()) {
63366323
return OverflowResult::NeverOverflows;
63376324
}
@@ -6350,14 +6337,14 @@ static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
63506337
//
63516338
// Since the carry into the most significant position is always equal to
63526339
// the carry out of the addition, there is no signed overflow.
6353-
if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
6354-
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
6340+
if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6341+
::ComputeNumSignBits(RHS, 0, SQ) > 1)
63556342
return OverflowResult::NeverOverflows;
63566343

6357-
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
6358-
LHS, /*ForSigned=*/true, DL, AC, CxtI, DT);
6359-
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
6360-
RHS, /*ForSigned=*/true, DL, AC, CxtI, DT);
6344+
ConstantRange LHSRange =
6345+
computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6346+
ConstantRange RHSRange =
6347+
computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
63616348
OverflowResult OR =
63626349
mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
63636350
if (OR != OverflowResult::MayOverflow)
@@ -6378,8 +6365,7 @@ static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
63786365
(LHSRange.isAllNegative() || RHSRange.isAllNegative());
63796366
if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
63806367
KnownBits AddKnown(LHSRange.getBitWidth());
6381-
computeKnownBitsFromAssume(Add, AddKnown, /*Depth=*/0,
6382-
SimplifyQuery(DL, DT, AC, CxtI, DT));
6368+
computeKnownBitsFromAssume(Add, AddKnown, /*Depth=*/0, SQ);
63836369
if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
63846370
(AddKnown.isNegative() && LHSOrRHSKnownNegative))
63856371
return OverflowResult::NeverOverflows;
@@ -6390,10 +6376,7 @@ static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
63906376

63916377
OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
63926378
const Value *RHS,
6393-
const DataLayout &DL,
6394-
AssumptionCache *AC,
6395-
const Instruction *CxtI,
6396-
const DominatorTree *DT) {
6379+
const SimplifyQuery &SQ) {
63976380
// X - (X % ?)
63986381
// The remainder of a value can't have greater magnitude than itself,
63996382
// so the subtraction can't overflow.
@@ -6407,32 +6390,29 @@ OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
64076390
// See simplifyICmpWithBinOpOnLHS() for candidates.
64086391
if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
64096392
match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
6410-
if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT))
6393+
if (isGuaranteedNotToBeUndefOrPoison(LHS, SQ.AC, SQ.CxtI, SQ.DT))
64116394
return OverflowResult::NeverOverflows;
64126395

64136396
// Checking for conditions implied by dominating conditions may be expensive.
64146397
// Limit it to usub_with_overflow calls for now.
6415-
if (match(CxtI,
6398+
if (match(SQ.CxtI,
64166399
m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
6417-
if (auto C =
6418-
isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) {
6400+
if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
6401+
SQ.DL)) {
64196402
if (*C)
64206403
return OverflowResult::NeverOverflows;
64216404
return OverflowResult::AlwaysOverflowsLow;
64226405
}
6423-
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
6424-
LHS, /*ForSigned=*/false, DL, AC, CxtI, DT);
6425-
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
6426-
RHS, /*ForSigned=*/false, DL, AC, CxtI, DT);
6406+
ConstantRange LHSRange =
6407+
computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6408+
ConstantRange RHSRange =
6409+
computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
64276410
return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
64286411
}
64296412

64306413
OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
64316414
const Value *RHS,
6432-
const DataLayout &DL,
6433-
AssumptionCache *AC,
6434-
const Instruction *CxtI,
6435-
const DominatorTree *DT) {
6415+
const SimplifyQuery &SQ) {
64366416
// X - (X % ?)
64376417
// The remainder of a value can't have greater magnitude than itself,
64386418
// so the subtraction can't overflow.
@@ -6443,19 +6423,19 @@ OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
64436423
// then determining no-overflow may allow other transforms.
64446424
if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
64456425
match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
6446-
if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT))
6426+
if (isGuaranteedNotToBeUndefOrPoison(LHS, SQ.AC, SQ.CxtI, SQ.DT))
64476427
return OverflowResult::NeverOverflows;
64486428

64496429
// If LHS and RHS each have at least two sign bits, the subtraction
64506430
// cannot overflow.
6451-
if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
6452-
ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
6431+
if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6432+
::ComputeNumSignBits(RHS, 0, SQ) > 1)
64536433
return OverflowResult::NeverOverflows;
64546434

6455-
ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
6456-
LHS, /*ForSigned=*/true, DL, AC, CxtI, DT);
6457-
ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
6458-
RHS, /*ForSigned=*/true, DL, AC, CxtI, DT);
6435+
ConstantRange LHSRange =
6436+
computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6437+
ConstantRange RHSRange =
6438+
computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
64596439
return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
64606440
}
64616441

@@ -6949,21 +6929,15 @@ bool llvm::mustExecuteUBIfPoisonOnPathTo(Instruction *Root,
69496929
}
69506930

69516931
OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
6952-
const DataLayout &DL,
6953-
AssumptionCache *AC,
6954-
const Instruction *CxtI,
6955-
const DominatorTree *DT) {
6932+
const SimplifyQuery &SQ) {
69566933
return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
6957-
Add, DL, AC, CxtI, DT);
6934+
Add, SQ);
69586935
}
69596936

69606937
OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS,
69616938
const Value *RHS,
6962-
const DataLayout &DL,
6963-
AssumptionCache *AC,
6964-
const Instruction *CxtI,
6965-
const DominatorTree *DT) {
6966-
return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT);
6939+
const SimplifyQuery &SQ) {
6940+
return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
69676941
}
69686942

69696943
bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {

0 commit comments

Comments
 (0)