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::clmul:
122 case Intrinsic::ctlz:
123 if (OperandNo == 0) {
127 ComputeKnownBits(
BitWidth, Val,
nullptr);
132 case Intrinsic::cttz:
133 if (OperandNo == 0) {
137 ComputeKnownBits(
BitWidth, Val,
nullptr);
142 case Intrinsic::fshl:
143 case Intrinsic::fshr: {
145 if (OperandNo == 2) {
154 if (
II->getIntrinsicID() == Intrinsic::fshr)
159 else if (OperandNo == 1)
164 case Intrinsic::umax:
165 case Intrinsic::umin:
166 case Intrinsic::smax:
167 case Intrinsic::smin:
175 case Instruction::Add:
183 case Instruction::Sub:
191 case Instruction::Mul:
197 case Instruction::Shl:
198 if (OperandNo == 0) {
199 const APInt *ShiftAmtC;
207 if (S->hasNoSignedWrap())
209 else if (S->hasNoUnsignedWrap())
216 GetShiftedRange(Min, Max,
false);
218 if (S->hasNoSignedWrap())
220 else if (S->hasNoUnsignedWrap())
225 case Instruction::LShr:
226 if (OperandNo == 0) {
227 const APInt *ShiftAmtC;
230 AB = AOut.
shl(ShiftAmt);
251 GetShiftedRange(Min, Max,
true);
257 case Instruction::AShr:
258 if (OperandNo == 0) {
259 const APInt *ShiftAmtC;
262 AB = AOut.
shl(ShiftAmt);
278 GetShiftedRange(Min, Max,
true);
297 case Instruction::And:
308 AB &= ~(Known.
Zero & ~Known2.Zero);
310 case Instruction::Or:
321 AB &= ~(Known.
One & ~Known2.One);
323 case Instruction::Xor:
324 case Instruction::PHI:
327 case Instruction::Trunc:
330 case Instruction::ZExt:
333 case Instruction::SExt:
343 case Instruction::Select:
347 case Instruction::ExtractElement:
351 case Instruction::InsertElement:
352 case Instruction::ShuffleVector:
353 if (OperandNo == 0 || OperandNo == 1)
359void DemandedBits::performAnalysis() {
369 SmallSetVector<Instruction*, 16> Worklist;
382 if (
T->isIntOrIntVectorTy()) {
383 if (AliveBits.try_emplace(&
I,
T->getScalarSizeInBits(), 0).second)
390 for (Use &OI :
I.operands()) {
392 Type *
T = J->getType();
393 if (
T->isIntOrIntVectorTy())
407 while (!Worklist.
empty()) {
412 bool InputIsKnownDead =
false;
414 AOut = AliveBits[UserI];
424 KnownBits Known, Known2;
425 bool KnownBitsComputed =
false;
436 Type *
T = OI->getType();
437 if (
T->isIntOrIntVectorTy()) {
438 unsigned BitWidth =
T->getScalarSizeInBits();
440 if (InputIsKnownDead) {
445 determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
446 Known, Known2, KnownBitsComputed);
450 DeadUses.insert(&OI);
459 auto Res = AliveBits.try_emplace(
I);
460 if (Res.second || (AB |= Res.first->second) != Res.first->second) {
461 Res.first->second = std::move(AB);
465 }
else if (
I && Visited.insert(
I).second) {
475 auto Found = AliveBits.find(
I);
476 if (Found != AliveBits.end())
477 return Found->second;
484 Type *
T = (*U)->getType();
487 unsigned BitWidth =
DL.getTypeSizeInBits(
T->getScalarType());
491 if (!
T->isIntOrIntVectorTy())
502 bool KnownBitsComputed =
false;
504 determineLiveOperandBits(UserI, *U, U->getOperandNo(), AOut, AB, Known,
505 Known2, KnownBitsComputed);
513 return !Visited.count(
I) && !AliveBits.contains(
I) && !
isAlwaysLive(
I);
518 if (!(*U)->getType()->isIntOrIntVectorTy())
527 if (DeadUses.count(U))
533 auto Found = AliveBits.find(UserI);
534 if (Found != AliveBits.end() && Found->second.isZero())
546 V->printAsOperand(OS,
false);
552 OS <<
"Printing analysis 'Demanded Bits Analysis' for function '" << F.getName() <<
"':\n";
554 for (
auto &KV : AliveBits) {
556 PrintDB(
I, KV.second);
558 for (
Use &OI :
I->operands()) {
568 bool CarryZero,
bool CarryOne) {
569 assert(!(CarryZero && CarryOne) &&
570 "Carry can't be zero and one at the same time");
588 APInt RProp = RAOut + (RAOut | ~RBound);
589 APInt RACarry = RProp ^ ~RBound;
593 APInt NeededToMaintainCarryZero;
594 APInt NeededToMaintainCarryOne;
595 if (OperandNo == 0) {
596 NeededToMaintainCarryZero =
LHS.Zero |
~RHS.Zero;
597 NeededToMaintainCarryOne =
LHS.One |
~RHS.One;
599 NeededToMaintainCarryZero =
RHS.Zero |
~LHS.Zero;
600 NeededToMaintainCarryOne =
RHS.One |
~LHS.One;
605 APInt PossibleSumOne =
LHS.One +
RHS.One + CarryOne;
618 APInt NeededToMaintainCarry = (~PossibleSumZero | NeededToMaintainCarryZero) &
619 (PossibleSumOne | NeededToMaintainCarryOne);
621 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(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.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
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.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
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.