51 std::vector<BasicBlock*> DeadBlocks;
53 if (!Reachable.
count(&*
I)) {
55 DeadBlocks.push_back(BB);
61 (*SI)->removePredecessor(BB);
66 for (
unsigned i = 0, e = DeadBlocks.size();
i != e; ++
i) {
67 DeadBlocks[
i]->eraseFromParent();
70 return !DeadBlocks.empty();
74 class UnreachableBlockElimLegacyPass :
public FunctionPass {
75 bool runOnFunction(
Function &
F)
override {
93 "Remove unreachable blocks from the CFG",
false,
false)
96 return new UnreachableBlockElimLegacyPass();
121 INITIALIZE_PASS(UnreachableMachineBlockElim,
"unreachable-mbb-elimination",
122 "Remove unreachable machine basic blocks",
false,
false)
126 void UnreachableMachineBlockElim::getAnalysisUsage(
AnalysisUsage &AU)
const {
132 bool UnreachableMachineBlockElim::runOnMachineFunction(
MachineFunction &
F) {
134 bool ModifiedPHI =
false;
136 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
146 std::vector<MachineBasicBlock*> DeadBlocks;
151 if (!Reachable.
count(BB)) {
152 DeadBlocks.push_back(BB);
155 if (MLI) MLI->removeBlock(BB);
156 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
162 while (start != succ->
end() && start->isPHI()) {
163 for (
unsigned i = start->getNumOperands() - 1;
i >= 2;
i-=2)
164 if (start->getOperand(
i).isMBB() &&
165 start->getOperand(
i).getMBB() == BB) {
166 start->RemoveOperand(
i);
167 start->RemoveOperand(
i-1);
179 for (
unsigned i = 0, e = DeadBlocks.size();
i != e; ++
i)
180 DeadBlocks[
i]->eraseFromParent();
189 while (phi != BB->
end() && phi->isPHI()) {
190 for (
unsigned i = phi->getNumOperands() - 1;
i >= 2;
i-=2)
191 if (!preds.count(phi->getOperand(
i).getMBB())) {
192 phi->RemoveOperand(
i);
193 phi->RemoveOperand(
i-1);
197 if (phi->getNumOperands() == 3) {
198 unsigned Input = phi->getOperand(1).getReg();
199 unsigned Output = phi->getOperand(0).getReg();
201 phi++->eraseFromParent();
204 if (Input != Output) {
219 return (!DeadBlocks.empty() || ModifiedPHI);
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Analysis pass which computes a DominatorTree.
void initializeUnreachableBlockElimLegacyPassPass(PassRegistry &)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
iterator begin()
Instruction iterator methods.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
static bool eliminateUnreachableBlock(Function &F)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
char & UnreachableMachineBlockElimID
UnreachableMachineBlockElimination - This pass removes unreachable machine basic blocks.
INITIALIZE_PASS(UnreachableBlockElimLegacyPass,"unreachableblockelim","Remove unreachable blocks from the CFG", false, false) FunctionPass *llvm
FunctionPass * createUnreachableBlockEliminationPass()
createUnreachableBlockEliminationPass - The LLVM code generator does not work well with unreachable b...
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Interval::succ_iterator succ_end(Interval *I)
A set of analyses that are preserved following a run of a transformation pass.
unsigned const MachineRegisterInfo * MRI
LLVM Basic Block Representation.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
Represent the analysis usage information of a pass.
const InstListType & getInstList() const
Return the underlying instruction list container.
FunctionPass class - This class is used to implement most global optimizations.
succ_iterator succ_begin()
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
pred_iterator pred_begin()
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void preserve()
Mark an analysis as preserved.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
void dropAllReferences()
Cause all subinstructions to "let go" of all the references that said subinstructions are maintaining...
This class contains meta information specific to a module.