54 #define DEBUG_TYPE "demanded-bits" 59 "Demanded bits analysis",
false,
false)
81 return isa<TerminatorInst>(
I) || isa<DbgInfoIntrinsic>(I) ||
85 void DemandedBits::determineLiveOperandBits(
96 auto ComputeKnownBits =
97 [&](
unsigned BitWidth,
const Value *V1,
const Value *
V2) {
111 case Instruction::Invoke:
112 if (
const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI))
113 switch (II->getIntrinsicID()) {
115 case Intrinsic::bswap:
120 case Intrinsic::bitreverse:
125 case Intrinsic::ctlz:
126 if (OperandNo == 0) {
130 ComputeKnownBits(BitWidth, I,
nullptr);
135 case Intrinsic::cttz:
136 if (OperandNo == 0) {
140 ComputeKnownBits(BitWidth, I,
nullptr);
148 case Instruction::Sub:
149 case Instruction::Mul:
155 case Instruction::Shl:
157 if (
auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->
getOperand(1))) {
159 AB = AOut.
lshr(ShiftAmt);
170 case Instruction::LShr:
172 if (
auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->
getOperand(1))) {
174 AB = AOut.
shl(ShiftAmt);
178 if (cast<LShrOperator>(UserI)->isExact())
182 case Instruction::AShr:
184 if (
auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->
getOperand(1))) {
185 uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
186 AB = AOut.
shl(ShiftAmt);
196 if (cast<AShrOperator>(UserI)->isExact())
200 case Instruction::And:
207 if (OperandNo == 0) {
208 ComputeKnownBits(BitWidth, I, UserI->
getOperand(1));
212 ComputeKnownBits(BitWidth, UserI->
getOperand(0),
I);
216 case Instruction::Or:
223 if (OperandNo == 0) {
224 ComputeKnownBits(BitWidth, I, UserI->
getOperand(1));
228 ComputeKnownBits(BitWidth, UserI->
getOperand(0),
I);
229 AB &= ~(Known.
One & ~Known2.
One);
232 case Instruction::Xor:
233 case Instruction::PHI:
236 case Instruction::Trunc:
237 AB = AOut.
zext(BitWidth);
239 case Instruction::ZExt:
240 AB = AOut.
trunc(BitWidth);
242 case Instruction::SExt:
243 AB = AOut.
trunc(BitWidth);
260 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
261 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
262 DB.emplace(F, AC, DT);
270 void DemandedBits::performAnalysis() {
292 if (AliveBits.try_emplace(&I,
IT->getBitWidth(), 0).
second)
313 while (!Worklist.
empty()) {
319 AOut = AliveBits[UserI];
325 Visited.insert(UserI);
334 unsigned BitWidth =
IT->getBitWidth();
338 AB =
APInt(BitWidth, 0);
343 determineLiveOperandBits(UserI, I, OI.getOperandNo(), AOut, AB,
350 APInt ABPrev(BitWidth, 0);
351 auto ABI = AliveBits.find(I);
352 if (ABI != AliveBits.end())
353 ABPrev = ABI->second;
355 APInt ABNew = AB | ABPrev;
356 if (ABNew != ABPrev || ABI == AliveBits.end()) {
357 AliveBits[
I] = std::move(ABNew);
360 }
else if (!Visited.count(I)) {
372 auto Found = AliveBits.find(I);
373 if (Found != AliveBits.end())
374 return Found->second;
381 return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
387 for (
auto &KV : AliveBits) {
389 <<
" for " << *KV.first <<
'\n';
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
void initializeDemandedBitsWrapperPassPass(PassRegistry &)
void setSignBit()
Set the sign bit to 1.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
bool isInstructionDead(Instruction *I)
Return true if, during analysis, I could not be reached.
APInt zext(unsigned width) const
Zero extend to a new width.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
An immutable pass that tracks lazily created AssumptionCache objects.
APInt trunc(unsigned width) const
Truncate to new width.
Analysis pass which computes a DominatorTree.
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
This defines the Use class.
unsigned getBitWidth() const
Return the number of bits in the APInt.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
APInt shl(unsigned shiftAmt) const
Left-shift function.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
A Use represents the edge between a Value definition and its users.
bool isIntegerTy() const
True if this is an instance of IntegerType.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
An analysis that produces DemandedBits for a function.
This file implements a class to represent arbitrary precision integral constant values and operations...
unsigned getActiveBits() const
Compute the number of active bits in the value.
Type * getType() const
All values are typed, get the type of this value.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
DemandedBits run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce demanded bits information.
APInt reverseBits() const
DemandedBitsWrapperPass()
Value * getOperand(unsigned i) const
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
A set of analyses that are preserved following a run of a transformation pass.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits", "Demanded bits analysis", false, false) INITIALIZE_PASS_END(DemandedBitsWrapperPass
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Class to represent integer types.
demanded Demanded bits analysis
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
A function analysis which provides an AssumptionCache.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
static Twine utohexstr(const uint64_t &Val)
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate IT block based on arch"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT, "arm-no-restrict-it", "Allow IT blocks based on ARMv7")))
LLVM_NODISCARD bool empty() const
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
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...
LLVM Value Representation.
static bool isAlwaysLive(Instruction *I)
This class implements an extremely fast bulk output stream that can only output to a stream...
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
inst_range instructions(Function *F)
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
This header defines various interfaces for pass management in LLVM.
FunctionPass * createDemandedBitsWrapperPass()
Create a demanded bits analysis pass.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
A special type used by analysis passes to provide an address that identifies that particular analysis...
void releaseMemory() override
Clean up memory in between runs.
void print(raw_ostream &OS)
A wrapper class for inspecting calls to intrinsic functions.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property...