31#define DEBUG_TYPE "bdce"
33STATISTIC(NumRemoved,
"Number of instructions removed (unused)");
34STATISTIC(NumSimplified,
"Number of instructions trivialized (dead bits)");
36 "Number of sign extension instructions converted to zero extension");
42 assert(
I->getType()->isIntOrIntVectorTy() &&
43 "Trivializing a non-integer value?");
48 for (
User *JU :
I->users()) {
51 auto *J = dyn_cast<Instruction>(JU);
52 if (J && J->getType()->isIntOrIntVectorTy() &&
53 !DB.getDemandedBits(J).isAllOnes()) {
69 while (!WorkList.
empty()) {
82 auto *K = dyn_cast<Instruction>(KU);
83 if (K && Visited.
insert(K).second && K->getType()->isIntOrIntVectorTy() &&
84 !DB.getDemandedBits(K).isAllOnes())
97 if (
I.mayHaveSideEffects() &&
I.use_empty())
102 if (DB.isInstructionDead(&
I) ||
103 (
I.getType()->isIntOrIntVectorTy() && DB.getDemandedBits(&
I).isZero() &&
111 if (
SExtInst *SE = dyn_cast<SExtInst>(&
I)) {
112 APInt Demanded = DB.getDemandedBits(SE);
113 const uint32_t SrcBitSize = SE->getSrcTy()->getScalarSizeInBits();
114 auto *
const DstTy = SE->getDestTy();
115 const uint32_t DestBitSize = DstTy->getScalarSizeInBits();
116 if (Demanded.
countl_zero() >= (DestBitSize - SrcBitSize)) {
119 I.replaceAllUsesWith(
120 Builder.CreateZExt(SE->getOperand(0), DstTy, SE->getName()));
128 for (
Use &U :
I.operands()) {
130 if (!U->getType()->isIntOrIntVectorTy())
133 if (!isa<Instruction>(U) && !isa<Argument>(U))
136 if (!DB.isUseDead(&U))
139 LLVM_DEBUG(
dbgs() <<
"BDCE: Trivializing: " << U <<
" (all bits dead)\n");
153 I->dropAllReferences();
158 I->eraseFromParent();
static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB)
If an instruction is trivialized (dead), then the chain of users of that instruction may need to be c...
static bool bitTrackingDCE(Function &F, DemandedBits &DB)
This is the interface for a simple mod/ref and alias analysis over globals.
Select target instructions out of generic instructions
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Class for arbitrary precision integers.
unsigned countl_zero() const
The APInt version of std::countl_zero.
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represents analyses that only rely on functions' control flow.
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
An analysis that produces DemandedBits for a function.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
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.
void preserveSet()
Mark an analysis set as preserved.
This class represents a sign extension of integer types.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A Use represents the edge between a Value definition and its users.
iterator_range< user_iterator > users()
This is an optimization pass for GlobalISel generic memory operations.
void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
auto reverse(ContainerTy &&C)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction would have no side effects if it was not used.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)