Skip to content

Commit 61225c0

Browse files
committed
[ValueTracking][InstCombine] Introduce and use ComputeMinSignedBits
This introduces a new ComputeMinSignedBits method for ValueTracking that returns the BitWidth - SignBits + 1 from ComputeSignBits, and represents the minimum bit size for the value as a signed integer. Similar to the existing APInt::getMinSignedBits method, this can make some of the reasoning around ComputeSignBits more natural. See https://reviews.llvm.org/D112298
1 parent 9e65062 commit 61225c0

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6;
203203
const DominatorTree *DT = nullptr,
204204
bool UseInstrInfo = true);
205205

206+
/// Get the minimum bit size for this Value \p Op as a signed integer.
207+
/// i.e. x == sext(trunc(x to MinSignedBits) to bitwidth(x)).
208+
/// Similar to the APInt::getMinSignedBits function.
209+
unsigned ComputeMinSignedBits(const Value *Op, const DataLayout &DL,
210+
unsigned Depth = 0,
211+
AssumptionCache *AC = nullptr,
212+
const Instruction *CxtI = nullptr,
213+
const DominatorTree *DT = nullptr);
214+
206215
/// This function computes the integer multiple of Base that equals V. If
207216
/// successful, it returns true and returns the multiple in Multiple. If
208217
/// unsuccessful, it returns false. Also, if V can be simplified to an

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
480480
return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
481481
}
482482

483+
unsigned ComputeMinSignedBits(const Value *Op, unsigned Depth = 0,
484+
const Instruction *CxtI = nullptr) const {
485+
return llvm::ComputeMinSignedBits(Op, DL, Depth, &AC, CxtI, &DT);
486+
}
487+
483488
OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
484489
const Value *RHS,
485490
const Instruction *CxtI) const {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
396396
V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
397397
}
398398

399+
unsigned llvm::ComputeMinSignedBits(const Value *V, const DataLayout &DL,
400+
unsigned Depth, AssumptionCache *AC,
401+
const Instruction *CxtI,
402+
const DominatorTree *DT) {
403+
unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
404+
return V->getType()->getScalarSizeInBits() - SignBits + 1;
405+
}
406+
399407
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
400408
bool NSW, const APInt &DemandedElts,
401409
KnownBits &KnownOut, KnownBits &Known2,

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,9 +1270,8 @@ static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
12701270
// This is only really a signed overflow check if the inputs have been
12711271
// sign-extended; check for that condition. For example, if CI2 is 2^31 and
12721272
// the operands of the add are 64 bits wide, we need at least 33 sign bits.
1273-
unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1;
1274-
if (IC.ComputeNumSignBits(A, 0, &I) < NeededSignBits ||
1275-
IC.ComputeNumSignBits(B, 0, &I) < NeededSignBits)
1273+
if (IC.ComputeMinSignedBits(A, 0, &I) > NewWidth ||
1274+
IC.ComputeMinSignedBits(B, 0, &I) > NewWidth)
12761275
return nullptr;
12771276

12781277
// In order to replace the original add with a narrower

0 commit comments

Comments
 (0)