41class UnreachableBlockElimLegacyPass :
public FunctionPass {
48 UnreachableBlockElimLegacyPass() : FunctionPass(ID) {
53 void getAnalysisUsage(AnalysisUsage &AU)
const override {
58char UnreachableBlockElimLegacyPass::ID = 0;
60 "Remove unreachable blocks from the CFG",
false,
false)
63 return new UnreachableBlockElimLegacyPass();
77class UnreachableMachineBlockElim {
86 : MDT(MDT), MPDT(MPDT), MLI(MLI) {}
91 bool runOnMachineFunction(MachineFunction &
F)
override;
92 void getAnalysisUsage(AnalysisUsage &AU)
const override;
96 UnreachableMachineBlockElimLegacy() : MachineFunctionPass(
ID) {}
100char UnreachableMachineBlockElimLegacy::ID = 0;
103 "unreachable-mbb-elimination",
104 "Remove unreachable machine basic blocks",
false,
false)
107 UnreachableMachineBlockElimLegacy::
ID;
109void UnreachableMachineBlockElimLegacy::getAnalysisUsage(
124 if (!UnreachableMachineBlockElim(MDT, MPDT, MLI).
run(MF))
129 .preserve<MachineDominatorTreeAnalysis>()
133bool UnreachableMachineBlockElimLegacy::runOnMachineFunction(
136 getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
138 getAnalysisIfAvailable<MachinePostDominatorTreeWrapperPass>();
143 getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
146 return UnreachableMachineBlockElim(MDT, MPDT, MLI).run(MF);
151 bool ModifiedPHI =
false;
159 std::vector<MachineBasicBlock*> DeadBlocks;
162 if (!Reachable.count(&BB)) {
163 DeadBlocks.push_back(&BB);
168 if (MPDT && MPDT->
getNode(&BB))
171 while (!BB.succ_empty()) {
172 (*BB.succ_begin())->removePHIsIncomingValuesForPredecessor(BB);
173 BB.removeSuccessor(BB.succ_begin());
181 for (
auto &
I : BB->instrs())
182 if (
I.shouldUpdateAdditionalCallInfo())
183 BB->getParent()->eraseAdditionalCallInfo(&
I);
185 BB->eraseFromParent();
194 for (
unsigned i =
Phi.getNumOperands() - 1; i >= 2; i -= 2) {
195 if (!preds.count(
Phi.getOperand(i).getMBB())) {
196 Phi.removeOperand(i);
197 Phi.removeOperand(i - 1);
202 if (
Phi.getNumOperands() == 3) {
210 if (InputReg != OutputReg) {
214 MRI.constrainRegClass(InputReg,
MRI.getRegClass(OutputReg)) &&
216 MRI.replaceRegWith(OutputReg, InputReg);
223 BuildMI(BB, BB.getFirstNonPHI(),
Phi.getDebugLoc(),
224 TII->get(TargetOpcode::COPY), OutputReg)
227 Phi.eraseFromParent();
240 return (!DeadBlocks.empty() || ModifiedPHI);
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool runOnFunction(Function &F, bool PostInlining)
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the SmallPtrSet class.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Analysis pass which computes a DominatorTree.
std::enable_if_t< GraphHasNodeNumbers< T * >, void > updateBlockNumbers()
Update dominator tree after renumbering blocks.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
FunctionPass class - This class is used to implement most global optimizations.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
MachineDominatorTree & getDomTree()
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Analysis pass that exposes the MachineLoopInfo for a machine function.
MachineLoopInfo & getLI()
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
MachinePostDominatorTree & getPostDomTree()
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Wrapper class representing virtual and physical registers.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
TargetInstrInfo - Interface to description of machine instruction set.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(MachineFunction &F, MachineFunctionAnalysisManager &AM)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr from_range_t from_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void initializeUnreachableBlockElimLegacyPassPass(PassRegistry &)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & UnreachableMachineBlockElimID
UnreachableMachineBlockElimination - This pass removes unreachable machine basic blocks.
LLVM_ABI bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete all basic blocks from F that are not reachable from its entry node.
unsigned getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI FunctionPass * createUnreachableBlockEliminationPass()
createUnreachableBlockEliminationPass - The LLVM code generator does not work well with unreachable b...