33
33
#include " llvm/Analysis/OptimizationRemarkEmitter.h"
34
34
#include " llvm/Analysis/TargetLibraryInfo.h"
35
35
#include " llvm/Analysis/VectorUtils.h"
36
+ #include " llvm/Analysis/WithCache.h"
36
37
#include " llvm/IR/Argument.h"
37
38
#include " llvm/IR/Attributes.h"
38
39
#include " llvm/IR/BasicBlock.h"
@@ -178,31 +179,29 @@ void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
178
179
SimplifyQuery (DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
179
180
}
180
181
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
-
187
182
KnownBits llvm::computeKnownBits (const Value *V, const DataLayout &DL,
188
183
unsigned Depth, AssumptionCache *AC,
189
184
const Instruction *CxtI,
190
185
const DominatorTree *DT, bool UseInstrInfo) {
191
- return :: computeKnownBits (
186
+ return computeKnownBits (
192
187
V, Depth, SimplifyQuery (DL, DT, AC, safeCxtI (V, CxtI), UseInstrInfo));
193
188
}
194
189
195
190
KnownBits llvm::computeKnownBits (const Value *V, const APInt &DemandedElts,
196
191
const DataLayout &DL, unsigned Depth,
197
192
AssumptionCache *AC, const Instruction *CxtI,
198
193
const DominatorTree *DT, bool UseInstrInfo) {
199
- return :: computeKnownBits (
194
+ return computeKnownBits (
200
195
V, DemandedElts, Depth,
201
196
SimplifyQuery (DL, DT, AC, safeCxtI (V, CxtI), UseInstrInfo));
202
197
}
203
198
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,
205
201
const SimplifyQuery &SQ) {
202
+ const Value *LHS = LHSCache.getValue ();
203
+ const Value *RHS = RHSCache.getValue ();
204
+
206
205
assert (LHS->getType () == RHS->getType () &&
207
206
" LHS and RHS should have the same type" );
208
207
assert (LHS->getType ()->isIntOrIntVectorTy () &&
@@ -250,12 +249,9 @@ bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
250
249
match (LHS, m_Not (m_c_Or (m_Specific (A), m_Specific (B)))))
251
250
return true ;
252
251
}
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));
259
255
}
260
256
261
257
bool llvm::isOnlyUsedInZeroEqualityComparison (const Instruction *I) {
@@ -1784,19 +1780,19 @@ static void computeKnownBitsFromOperator(const Operator *I,
1784
1780
1785
1781
// / Determine which bits of V are known to be either zero or one and return
1786
1782
// / 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) {
1789
1785
KnownBits Known (getBitWidth (V->getType (), Q.DL ));
1790
- computeKnownBits (V, DemandedElts, Known, Depth, Q);
1786
+ :: computeKnownBits (V, DemandedElts, Known, Depth, Q);
1791
1787
return Known;
1792
1788
}
1793
1789
1794
1790
// / Determine which bits of V are known to be either zero or one and return
1795
1791
// / 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) {
1798
1794
KnownBits Known (getBitWidth (V->getType (), Q.DL ));
1799
- computeKnownBits (V, Known, Depth, Q);
1795
+ :: computeKnownBits (V, Known, Depth, Q);
1800
1796
return Known;
1801
1797
}
1802
1798
@@ -6256,10 +6252,11 @@ static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
6256
6252
6257
6253
// / Combine constant ranges from computeConstantRange() and computeKnownBits().
6258
6254
static ConstantRange
6259
- computeConstantRangeIncludingKnownBits (const Value *V, bool ForSigned,
6255
+ computeConstantRangeIncludingKnownBits (const WithCache<const Value *> &V,
6256
+ bool ForSigned,
6260
6257
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);
6263
6260
ConstantRange CR2 = computeConstantRange (V, ForSigned, SQ.IIQ .UseInstrInfo );
6264
6261
ConstantRange::PreferredRangeType RangeType =
6265
6262
ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;
@@ -6269,8 +6266,8 @@ computeConstantRangeIncludingKnownBits(const Value *V, bool ForSigned,
6269
6266
OverflowResult llvm::computeOverflowForUnsignedMul (const Value *LHS,
6270
6267
const Value *RHS,
6271
6268
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);
6274
6271
ConstantRange LHSRange = ConstantRange::fromKnownBits (LHSKnown, false );
6275
6272
ConstantRange RHSRange = ConstantRange::fromKnownBits (RHSKnown, false );
6276
6273
return mapOverflowResult (LHSRange.unsignedMulMayOverflow (RHSRange));
@@ -6307,28 +6304,29 @@ OverflowResult llvm::computeOverflowForSignedMul(const Value *LHS,
6307
6304
// product is exactly the minimum negative number.
6308
6305
// E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
6309
6306
// 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);
6312
6309
if (LHSKnown.isNonNegative () || RHSKnown.isNonNegative ())
6313
6310
return OverflowResult::NeverOverflows;
6314
6311
}
6315
6312
return OverflowResult::MayOverflow;
6316
6313
}
6317
6314
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) {
6321
6319
ConstantRange LHSRange =
6322
6320
computeConstantRangeIncludingKnownBits (LHS, /* ForSigned=*/ false , SQ);
6323
6321
ConstantRange RHSRange =
6324
6322
computeConstantRangeIncludingKnownBits (RHS, /* ForSigned=*/ false , SQ);
6325
6323
return mapOverflowResult (LHSRange.unsignedAddMayOverflow (RHSRange));
6326
6324
}
6327
6325
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) {
6332
6330
if (Add && Add->hasNoSignedWrap ()) {
6333
6331
return OverflowResult::NeverOverflows;
6334
6332
}
@@ -6944,9 +6942,10 @@ OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
6944
6942
Add, SQ);
6945
6943
}
6946
6944
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) {
6950
6949
return ::computeOverflowForSignedAdd (LHS, RHS, nullptr , SQ);
6951
6950
}
6952
6951
0 commit comments