2009-06-02 17:52:33 +00:00
|
|
|
//===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass is an extremely simple version of the SimplifyCFG pass. Its sole
|
|
|
|
// job is to delete LLVM basic blocks that are not reachable from the entry
|
|
|
|
// node. To do this, it performs a simple depth first traversal of the CFG,
|
|
|
|
// then deletes any unvisited nodes.
|
|
|
|
//
|
|
|
|
// Note that this pass is really a hack. In particular, the instruction
|
|
|
|
// selectors for various targets should just not generate code for unreachable
|
|
|
|
// blocks. Until LLVM has a more systematic way of defining instruction
|
|
|
|
// selectors, however, we cannot really expect them to handle additional
|
|
|
|
// complexity.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2009-10-14 17:57:32 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/IR/Constant.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-04-08 18:41:23 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/Pass.h"
|
2009-06-02 17:52:33 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-04 14:58:56 +00:00
|
|
|
class UnreachableBlockElim : public FunctionPass {
|
2014-11-24 09:08:18 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2009-06-02 17:52:33 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2011-02-20 12:57:14 +00:00
|
|
|
UnreachableBlockElim() : FunctionPass(ID) {
|
|
|
|
initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-10-14 17:57:32 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
};
|
2015-07-05 14:21:36 +00:00
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
char UnreachableBlockElim::ID = 0;
|
2010-09-17 15:48:55 +00:00
|
|
|
INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
|
2011-02-20 12:57:14 +00:00
|
|
|
"Remove unreachable blocks from the CFG", false, false)
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createUnreachableBlockEliminationPass() {
|
|
|
|
return new UnreachableBlockElim();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnreachableBlockElim::runOnFunction(Function &F) {
|
|
|
|
SmallPtrSet<BasicBlock*, 8> Reachable;
|
|
|
|
|
|
|
|
// Mark all reachable blocks.
|
2015-01-18 16:17:27 +00:00
|
|
|
for (BasicBlock *BB : depth_first_ext(&F, Reachable))
|
|
|
|
(void)BB/* Mark all reachable blocks */;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Loop over all dead blocks, remembering them and deleting all instructions
|
|
|
|
// in them.
|
|
|
|
std::vector<BasicBlock*> DeadBlocks;
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
2015-12-30 11:46:15 +00:00
|
|
|
if (!Reachable.count(&*I)) {
|
|
|
|
BasicBlock *BB = &*I;
|
2009-06-02 17:52:33 +00:00
|
|
|
DeadBlocks.push_back(BB);
|
|
|
|
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
|
|
|
|
PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
|
|
|
|
BB->getInstList().pop_front();
|
|
|
|
}
|
|
|
|
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
|
|
|
|
(*SI)->removePredecessor(BB);
|
|
|
|
BB->dropAllReferences();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually remove the blocks now.
|
2009-10-14 17:57:32 +00:00
|
|
|
for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
|
2009-06-02 17:52:33 +00:00
|
|
|
DeadBlocks[i]->eraseFromParent();
|
2009-10-14 17:57:32 +00:00
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
return !DeadBlocks.empty();
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2009-11-04 14:58:56 +00:00
|
|
|
class UnreachableMachineBlockElim : public MachineFunctionPass {
|
2014-11-24 09:08:18 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2009-06-02 17:52:33 +00:00
|
|
|
MachineModuleInfo *MMI;
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-09-17 15:48:55 +00:00
|
|
|
UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
|
2009-06-02 17:52:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
char UnreachableMachineBlockElim::ID = 0;
|
|
|
|
|
2010-09-17 15:48:55 +00:00
|
|
|
INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
|
2011-02-20 12:57:14 +00:00
|
|
|
"Remove unreachable machine basic blocks", false, false)
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2010-09-17 15:48:55 +00:00
|
|
|
char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> Reachable;
|
2011-02-20 12:57:14 +00:00
|
|
|
bool ModifiedPHI = false;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
MMI = getAnalysisIfAvailable<MachineModuleInfo>();
|
2009-10-14 17:57:32 +00:00
|
|
|
MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
|
|
|
|
MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Mark all reachable blocks.
|
2015-01-18 16:17:27 +00:00
|
|
|
for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable))
|
|
|
|
(void)BB/* Mark all reachable blocks */;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Loop over all dead blocks, remembering them and deleting all instructions
|
|
|
|
// in them.
|
|
|
|
std::vector<MachineBasicBlock*> DeadBlocks;
|
|
|
|
for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
2015-12-30 11:46:15 +00:00
|
|
|
MachineBasicBlock *BB = &*I;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Test for deadness.
|
|
|
|
if (!Reachable.count(BB)) {
|
|
|
|
DeadBlocks.push_back(BB);
|
|
|
|
|
2009-10-14 17:57:32 +00:00
|
|
|
// Update dominator and loop info.
|
|
|
|
if (MLI) MLI->removeBlock(BB);
|
|
|
|
if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
|
|
|
|
|
2009-06-02 17:52:33 +00:00
|
|
|
while (BB->succ_begin() != BB->succ_end()) {
|
|
|
|
MachineBasicBlock* succ = *BB->succ_begin();
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator start = succ->begin();
|
2010-02-16 09:30:23 +00:00
|
|
|
while (start != succ->end() && start->isPHI()) {
|
2009-06-02 17:52:33 +00:00
|
|
|
for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
|
|
|
|
if (start->getOperand(i).isMBB() &&
|
|
|
|
start->getOperand(i).getMBB() == BB) {
|
|
|
|
start->RemoveOperand(i);
|
|
|
|
start->RemoveOperand(i-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
BB->removeSuccessor(BB->succ_begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually remove the blocks now.
|
2010-03-16 16:51:38 +00:00
|
|
|
for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
|
|
|
|
DeadBlocks[i]->eraseFromParent();
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
// Cleanup PHI nodes.
|
|
|
|
for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
2015-12-30 11:46:15 +00:00
|
|
|
MachineBasicBlock *BB = &*I;
|
2009-06-02 17:52:33 +00:00
|
|
|
// Prune unneeded PHI entries.
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
|
|
|
|
BB->pred_end());
|
|
|
|
MachineBasicBlock::iterator phi = BB->begin();
|
2010-02-16 09:30:23 +00:00
|
|
|
while (phi != BB->end() && phi->isPHI()) {
|
2009-06-02 17:52:33 +00:00
|
|
|
for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
|
|
|
|
if (!preds.count(phi->getOperand(i).getMBB())) {
|
|
|
|
phi->RemoveOperand(i);
|
|
|
|
phi->RemoveOperand(i-1);
|
2011-02-20 12:57:14 +00:00
|
|
|
ModifiedPHI = true;
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (phi->getNumOperands() == 3) {
|
|
|
|
unsigned Input = phi->getOperand(1).getReg();
|
|
|
|
unsigned Output = phi->getOperand(0).getReg();
|
|
|
|
|
|
|
|
MachineInstr* temp = phi;
|
|
|
|
++phi;
|
|
|
|
temp->eraseFromParent();
|
2011-02-20 12:57:14 +00:00
|
|
|
ModifiedPHI = true;
|
2009-06-02 17:52:33 +00:00
|
|
|
|
2011-06-12 15:42:51 +00:00
|
|
|
if (Input != Output) {
|
|
|
|
MachineRegisterInfo &MRI = F.getRegInfo();
|
|
|
|
MRI.constrainRegClass(Input, MRI.getRegClass(Output));
|
|
|
|
MRI.replaceRegWith(Output, Input);
|
|
|
|
}
|
2009-06-02 17:52:33 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++phi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
F.RenumberBlocks();
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
return (!DeadBlocks.empty() || ModifiedPHI);
|
2009-06-02 17:52:33 +00:00
|
|
|
}
|