46#define DEBUG_TYPE "demanded-bits"
49 return I->isTerminator() ||
I->isEHPad() ||
I->mayHaveSideEffects();
52void DemandedBits::determineLiveOperandBits(
55 bool &KnownBitsComputed) {
64 auto ComputeKnownBits =
66 if (KnownBitsComputed)
68 KnownBitsComputed =
true;
79 auto GetShiftedRange = [&](uint64_t Min, uint64_t
Max,
bool ShiftLeft) {
80 auto ShiftF = [ShiftLeft](
const APInt &
Mask,
unsigned ShiftAmnt) {
81 return ShiftLeft ?
Mask.shl(ShiftAmnt) :
Mask.lshr(ShiftAmnt);
84 uint64_t LoopRange =
Max - Min;
87 for (
unsigned ShiftAmnt = 1; ShiftAmnt <= LoopRange; ShiftAmnt <<= 1) {
88 if (LoopRange & ShiftAmnt) {
90 Mask |= ShiftF(Shifted, LoopRange - ShiftAmnt + 1);
92 LoopRange -= ShiftAmnt;
95 Shifted |= ShiftF(Shifted, ShiftAmnt);
97 AB = ShiftF(Mask, Min);
102 case Instruction::Call:
103 case Instruction::Invoke:
105 switch (
II->getIntrinsicID()) {
107 case Intrinsic::bswap:
112 case Intrinsic::bitreverse:
117 case Intrinsic::ctlz:
118 if (OperandNo == 0) {
122 ComputeKnownBits(
BitWidth, Val,
nullptr);
127 case Intrinsic::cttz:
128 if (OperandNo == 0) {
132 ComputeKnownBits(
BitWidth, Val,
nullptr);
137 case Intrinsic::fshl:
138 case Intrinsic::fshr: {
140 if (OperandNo == 2) {
149 if (
II->getIntrinsicID() == Intrinsic::fshr)
154 else if (OperandNo == 1)
159 case Intrinsic::umax:
160 case Intrinsic::umin:
161 case Intrinsic::smax:
162 case Intrinsic::smin:
170 case Instruction::Add:
178 case Instruction::Sub:
186 case Instruction::Mul:
192 case Instruction::Shl:
193 if (OperandNo == 0) {
194 const APInt *ShiftAmtC;
202 if (S->hasNoSignedWrap())
204 else if (S->hasNoUnsignedWrap())
211 GetShiftedRange(Min, Max,
false);
213 if (S->hasNoSignedWrap())
215 else if (S->hasNoUnsignedWrap())
220 case Instruction::LShr:
221 if (OperandNo == 0) {
222 const APInt *ShiftAmtC;
225 AB = AOut.
shl(ShiftAmt);
246 GetShiftedRange(Min, Max,
true);
252 case Instruction::AShr:
253 if (OperandNo == 0) {
254 const APInt *ShiftAmtC;
257 AB = AOut.
shl(ShiftAmt);
273 GetShiftedRange(Min, Max,
true);
292 case Instruction::And:
303 AB &= ~(Known.
Zero & ~Known2.Zero);
305 case Instruction::Or:
316 AB &= ~(Known.
One & ~Known2.One);
318 case Instruction::Xor:
319 case Instruction::PHI:
322 case Instruction::Trunc:
325 case Instruction::ZExt:
328 case Instruction::SExt:
338 case Instruction::Select:
342 case Instruction::ExtractElement:
346 case Instruction::InsertElement:
347 case Instruction::ShuffleVector:
348 if (OperandNo == 0 || OperandNo == 1)
354void DemandedBits::performAnalysis() {
364 SmallSetVector<Instruction*, 16> Worklist;
377 if (
T->isIntOrIntVectorTy()) {
378 if (AliveBits.try_emplace(&
I,
T->getScalarSizeInBits(), 0).second)
385 for (Use &OI :
I.operands()) {
387 Type *
T = J->getType();
388 if (
T->isIntOrIntVectorTy())
402 while (!Worklist.
empty()) {
407 bool InputIsKnownDead =
false;
409 AOut = AliveBits[UserI];
419 KnownBits Known, Known2;
420 bool KnownBitsComputed =
false;
431 Type *
T = OI->getType();
432 if (
T->isIntOrIntVectorTy()) {
433 unsigned BitWidth =
T->getScalarSizeInBits();
435 if (InputIsKnownDead) {
440 determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
441 Known, Known2, KnownBitsComputed);
445 DeadUses.insert(&OI);
454 auto Res = AliveBits.try_emplace(
I);
455 if (Res.second || (AB |= Res.first->second) != Res.first->second) {
456 Res.first->second = std::move(AB);
460 }
else if (
I && Visited.insert(
I).second) {
470 auto Found = AliveBits.find(
I);
471 if (Found != AliveBits.end())
472 return Found->second;
479 Type *
T = (*U)->getType();
482 unsigned BitWidth =
DL.getTypeSizeInBits(
T->getScalarType());
486 if (!
T->isIntOrIntVectorTy())
497 bool KnownBitsComputed =
false;
499 determineLiveOperandBits(UserI, *U, U->getOperandNo(), AOut, AB, Known,
500 Known2, KnownBitsComputed);
508 return !Visited.count(
I) && !AliveBits.contains(
I) && !
isAlwaysLive(
I);
513 if (!(*U)->getType()->isIntOrIntVectorTy())
522 if (DeadUses.count(U))
528 auto Found = AliveBits.find(UserI);
529 if (Found != AliveBits.end() && Found->second.isZero())
541 V->printAsOperand(OS,
false);
547 OS <<
"Printing analysis 'Demanded Bits Analysis' for function '" << F.getName() <<
"':\n";
549 for (
auto &KV : AliveBits) {
551 PrintDB(
I, KV.second);
553 for (
Use &OI :
I->operands()) {
563 bool CarryZero,
bool CarryOne) {
564 assert(!(CarryZero && CarryOne) &&
565 "Carry can't be zero and one at the same time");
583 APInt RProp = RAOut + (RAOut | ~RBound);
584 APInt RACarry = RProp ^ ~RBound;
588 APInt NeededToMaintainCarryZero;
589 APInt NeededToMaintainCarryOne;
590 if (OperandNo == 0) {
591 NeededToMaintainCarryZero =
LHS.Zero |
~RHS.Zero;
592 NeededToMaintainCarryOne =
LHS.One |
~RHS.One;
594 NeededToMaintainCarryZero =
RHS.Zero |
~LHS.Zero;
595 NeededToMaintainCarryOne =
RHS.One |
~LHS.One;
600 APInt PossibleSumOne =
LHS.One +
RHS.One + CarryOne;
613 APInt NeededToMaintainCarry = (~PossibleSumZero | NeededToMaintainCarryZero) &
614 (PossibleSumOne | NeededToMaintainCarryOne);
616 APInt AB = AOut | (ACarry & NeededToMaintainCarry);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool isAlwaysLive(Instruction *I)
static APInt determineLiveOperandBitsAddCarry(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS, bool CarryZero, bool CarryOne)
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
uint64_t IntrinsicInst * II
This file implements a set that has insertion order iteration characteristics.
Class for arbitrary precision integers.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
unsigned getActiveBits() const
Compute the number of active bits in the value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
LLVM_ABI APInt reverseBits() const
unsigned countr_zero() const
Count the number of trailing zero bits.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
bool isMask(unsigned numBits) const
APInt shl(unsigned shiftAmt) const
Left-shift function.
LLVM_ABI APInt byteSwap() const
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
A parsed version of the target data layout string in and methods for querying it.
An analysis that produces DemandedBits for a function.
LLVM_ABI DemandedBits run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce demanded bits information.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI void print(raw_ostream &OS)
LLVM_ABI APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
static LLVM_ABI APInt determineLiveOperandBitsAdd(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS)
Compute alive bits of one addition operand from alive output and known operand bits.
LLVM_ABI bool isInstructionDead(Instruction *I)
Return true if, during analysis, I could not be reached.
static LLVM_ABI APInt determineLiveOperandBitsSub(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS)
Compute alive bits of one subtraction operand from alive output and known operand bits.
LLVM_ABI bool isUseDead(Use *U)
Return whether this use is dead by means of not having any demanded bits.
Analysis pass which computes a DominatorTree.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
value_type pop_back_val()
static Twine utohexstr(const uint64_t &Val)
The instances of the Type class are immutable: once they are created, they are never changed.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
This class implements an extremely fast bulk output stream that can only output to a stream.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool match(Val *V, const Pattern &P)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A special type used by analysis passes to provide an address that identifies that particular analysis...
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.