3333#include " llvm/Analysis/OptimizationRemarkEmitter.h"
3434#include " llvm/Analysis/TargetLibraryInfo.h"
3535#include " llvm/Analysis/VectorUtils.h"
36+ #include " llvm/Analysis/WithCache.h"
3637#include " llvm/IR/Argument.h"
3738#include " llvm/IR/Attributes.h"
3839#include " llvm/IR/BasicBlock.h"
@@ -178,31 +179,29 @@ void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
178179 SimplifyQuery (DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
179180}
180181
181- static KnownBits computeKnownBits (const Value *V, const APInt &DemandedElts,
182- unsigned Depth, const SimplifyQuery &Q);
183-
184- static KnownBits computeKnownBits (const Value *V, unsigned Depth,
185- const SimplifyQuery &Q);
186-
187182KnownBits llvm::computeKnownBits (const Value *V, const DataLayout &DL,
188183 unsigned Depth, AssumptionCache *AC,
189184 const Instruction *CxtI,
190185 const DominatorTree *DT, bool UseInstrInfo) {
191- return :: computeKnownBits (
186+ return computeKnownBits (
192187 V, Depth, SimplifyQuery (DL, DT, AC, safeCxtI (V, CxtI), UseInstrInfo));
193188}
194189
195190KnownBits llvm::computeKnownBits (const Value *V, const APInt &DemandedElts,
196191 const DataLayout &DL, unsigned Depth,
197192 AssumptionCache *AC, const Instruction *CxtI,
198193 const DominatorTree *DT, bool UseInstrInfo) {
199- return :: computeKnownBits (
194+ return computeKnownBits (
200195 V, DemandedElts, Depth,
201196 SimplifyQuery (DL, DT, AC, safeCxtI (V, CxtI), UseInstrInfo));
202197}
203198
204- bool llvm::haveNoCommonBitsSet (const Value *LHS, const Value *RHS,
199+ bool llvm::haveNoCommonBitsSet (const WithCache<const Value *> &LHSCache,
200+ const WithCache<const Value *> &RHSCache,
205201 const SimplifyQuery &SQ) {
202+ const Value *LHS = LHSCache.getValue ();
203+ const Value *RHS = RHSCache.getValue ();
204+
206205 assert (LHS->getType () == RHS->getType () &&
207206 " LHS and RHS should have the same type" );
208207 assert (LHS->getType ()->isIntOrIntVectorTy () &&
@@ -250,12 +249,9 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
250249 match (LHS, m_Not (m_c_Or (m_Specific (A), m_Specific (B)))))
251250 return true ;
252251 }
253- IntegerType *IT = cast<IntegerType>(LHS->getType ()->getScalarType ());
254- KnownBits LHSKnown (IT->getBitWidth ());
255- KnownBits RHSKnown (IT->getBitWidth ());
256- ::computeKnownBits (LHS, LHSKnown, 0 , SQ);
257- ::computeKnownBits (RHS, RHSKnown, 0 , SQ);
258- return KnownBits::haveNoCommonBitsSet (LHSKnown, RHSKnown);
252+
253+ return KnownBits::haveNoCommonBitsSet (LHSCache.getKnownBits (SQ),
254+ RHSCache.getKnownBits (SQ));
259255}
260256
261257bool llvm::isOnlyUsedInZeroEqualityComparison (const Instruction *I) {
@@ -1784,19 +1780,19 @@ static void computeKnownBitsFromOperator(const Operator *I,
17841780
17851781// / Determine which bits of V are known to be either zero or one and return
17861782// / them.
1787- KnownBits computeKnownBits (const Value *V, const APInt &DemandedElts,
1788- unsigned Depth, const SimplifyQuery &Q) {
1783+ KnownBits llvm:: computeKnownBits (const Value *V, const APInt &DemandedElts,
1784+ unsigned Depth, const SimplifyQuery &Q) {
17891785 KnownBits Known (getBitWidth (V->getType (), Q.DL ));
1790- computeKnownBits (V, DemandedElts, Known, Depth, Q);
1786+ :: computeKnownBits (V, DemandedElts, Known, Depth, Q);
17911787 return Known;
17921788}
17931789
17941790// / Determine which bits of V are known to be either zero or one and return
17951791// / them.
1796- KnownBits computeKnownBits (const Value *V, unsigned Depth,
1797- const SimplifyQuery &Q) {
1792+ KnownBits llvm:: computeKnownBits (const Value *V, unsigned Depth,
1793+ const SimplifyQuery &Q) {
17981794 KnownBits Known (getBitWidth (V->getType (), Q.DL ));
1799- computeKnownBits (V, Known, Depth, Q);
1795+ :: computeKnownBits (V, Known, Depth, Q);
18001796 return Known;
18011797}
18021798
@@ -6256,10 +6252,11 @@ static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
62566252
62576253// / Combine constant ranges from computeConstantRange() and computeKnownBits().
62586254static ConstantRange
6259- computeConstantRangeIncludingKnownBits (const Value *V, bool ForSigned,
6255+ computeConstantRangeIncludingKnownBits (const WithCache<const Value *> &V,
6256+ bool ForSigned,
62606257 const SimplifyQuery &SQ) {
6261- KnownBits Known = :: computeKnownBits (V, /* Depth= */ 0 , SQ);
6262- ConstantRange CR1 = ConstantRange::fromKnownBits (Known , ForSigned);
6258+ ConstantRange CR1 =
6259+ ConstantRange::fromKnownBits (V. getKnownBits (SQ) , ForSigned);
62636260 ConstantRange CR2 = computeConstantRange (V, ForSigned, SQ.IIQ .UseInstrInfo );
62646261 ConstantRange::PreferredRangeType RangeType =
62656262 ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;
@@ -6269,8 +6266,8 @@ computeConstantRangeIncludingKnownBits(const Value *V, bool ForSigned,
62696266OverflowResult llvm::computeOverflowForUnsignedMul (const Value *LHS,
62706267 const Value *RHS,
62716268 const SimplifyQuery &SQ) {
6272- KnownBits LHSKnown = :: computeKnownBits (LHS, /* Depth=*/ 0 , SQ);
6273- KnownBits RHSKnown = :: computeKnownBits (RHS, /* Depth=*/ 0 , SQ);
6269+ KnownBits LHSKnown = computeKnownBits (LHS, /* Depth=*/ 0 , SQ);
6270+ KnownBits RHSKnown = computeKnownBits (RHS, /* Depth=*/ 0 , SQ);
62746271 ConstantRange LHSRange = ConstantRange::fromKnownBits (LHSKnown, false );
62756272 ConstantRange RHSRange = ConstantRange::fromKnownBits (RHSKnown, false );
62766273 return mapOverflowResult (LHSRange.unsignedMulMayOverflow (RHSRange));
@@ -6307,28 +6304,29 @@ OverflowResult llvm::computeOverflowForSignedMul(const Value *LHS,
63076304 // product is exactly the minimum negative number.
63086305 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
63096306 // For simplicity we just check if at least one side is not negative.
6310- KnownBits LHSKnown = :: computeKnownBits (LHS, /* Depth=*/ 0 , SQ);
6311- KnownBits RHSKnown = :: computeKnownBits (RHS, /* Depth=*/ 0 , SQ);
6307+ KnownBits LHSKnown = computeKnownBits (LHS, /* Depth=*/ 0 , SQ);
6308+ KnownBits RHSKnown = computeKnownBits (RHS, /* Depth=*/ 0 , SQ);
63126309 if (LHSKnown.isNonNegative () || RHSKnown.isNonNegative ())
63136310 return OverflowResult::NeverOverflows;
63146311 }
63156312 return OverflowResult::MayOverflow;
63166313}
63176314
6318- OverflowResult llvm::computeOverflowForUnsignedAdd (const Value *LHS,
6319- const Value *RHS,
6320- const SimplifyQuery &SQ) {
6315+ OverflowResult
6316+ llvm::computeOverflowForUnsignedAdd (const WithCache<const Value *> &LHS,
6317+ const WithCache<const Value *> &RHS,
6318+ const SimplifyQuery &SQ) {
63216319 ConstantRange LHSRange =
63226320 computeConstantRangeIncludingKnownBits (LHS, /* ForSigned=*/ false , SQ);
63236321 ConstantRange RHSRange =
63246322 computeConstantRangeIncludingKnownBits (RHS, /* ForSigned=*/ false , SQ);
63256323 return mapOverflowResult (LHSRange.unsignedAddMayOverflow (RHSRange));
63266324}
63276325
6328- static OverflowResult computeOverflowForSignedAdd ( const Value *LHS,
6329- const Value *RHS ,
6330- const AddOperator *Add ,
6331- const SimplifyQuery &SQ) {
6326+ static OverflowResult
6327+ computeOverflowForSignedAdd ( const WithCache< const Value *> &LHS ,
6328+ const WithCache< const Value *> &RHS ,
6329+ const AddOperator *Add, const SimplifyQuery &SQ) {
63326330 if (Add && Add->hasNoSignedWrap ()) {
63336331 return OverflowResult::NeverOverflows;
63346332 }
@@ -6944,9 +6942,10 @@ OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
69446942 Add, SQ);
69456943}
69466944
6947- OverflowResult llvm::computeOverflowForSignedAdd (const Value *LHS,
6948- const Value *RHS,
6949- const SimplifyQuery &SQ) {
6945+ OverflowResult
6946+ llvm::computeOverflowForSignedAdd (const WithCache<const Value *> &LHS,
6947+ const WithCache<const Value *> &RHS,
6948+ const SimplifyQuery &SQ) {
69506949 return ::computeOverflowForSignedAdd (LHS, RHS, nullptr , SQ);
69516950}
69526951
0 commit comments