2004-07-02 05:46:10 +00:00
|
|
|
//===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2004-07-02 05:46:10 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2004-07-02 05:46:10 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 22:36:52 +00:00
|
|
|
//
|
2004-07-02 05:46:10 +00:00
|
|
|
// 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"
|
2004-07-06 06:36:11 +00:00
|
|
|
#include "llvm/Constant.h"
|
2004-07-29 17:23:00 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-07-02 05:46:10 +00:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Pass.h"
|
2005-03-17 15:38:16 +00:00
|
|
|
#include "llvm/Type.h"
|
2008-08-04 23:54:43 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2008-10-31 20:08:30 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2008-08-05 21:40:45 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2004-07-02 05:46:10 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-08-04 23:54:43 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2008-08-14 21:01:00 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-07-02 05:46:10 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2006-06-28 23:17:24 +00:00
|
|
|
class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass {
|
2004-07-02 05:46:10 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2007-05-01 21:15:47 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
UnreachableBlockElim() : FunctionPass(&ID) {}
|
2004-07-02 05:46:10 +00:00
|
|
|
};
|
|
|
|
}
|
2008-05-13 00:00:25 +00:00
|
|
|
char UnreachableBlockElim::ID = 0;
|
|
|
|
static RegisterPass<UnreachableBlockElim>
|
|
|
|
X("unreachableblockelim", "Remove unreachable blocks from the CFG");
|
2004-07-02 05:46:10 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createUnreachableBlockEliminationPass() {
|
|
|
|
return new UnreachableBlockElim();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnreachableBlockElim::runOnFunction(Function &F) {
|
2008-08-14 21:01:00 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8> Reachable;
|
2004-07-02 05:46:10 +00:00
|
|
|
|
|
|
|
// Mark all reachable blocks.
|
2008-08-14 21:01:00 +00:00
|
|
|
for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I =
|
|
|
|
df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I)
|
2004-07-02 05:46:10 +00:00
|
|
|
/* Mark all reachable blocks */;
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if (!Reachable.count(I)) {
|
2004-07-06 06:36:11 +00:00
|
|
|
BasicBlock *BB = I;
|
|
|
|
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();
|
2004-07-02 05:46:10 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 23:54:43 +00:00
|
|
|
// Actually remove the blocks now.
|
|
|
|
for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
|
|
|
|
DeadBlocks[i]->eraseFromParent();
|
|
|
|
|
|
|
|
return DeadBlocks.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2008-10-31 20:08:30 +00:00
|
|
|
class VISIBILITY_HIDDEN UnreachableMachineBlockElim :
|
2008-08-04 23:54:43 +00:00
|
|
|
public MachineFunctionPass {
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &F);
|
2008-10-31 20:08:30 +00:00
|
|
|
MachineModuleInfo *MMI;
|
2008-08-04 23:54:43 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {}
|
2008-08-04 23:54:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
char UnreachableMachineBlockElim::ID = 0;
|
|
|
|
|
|
|
|
static RegisterPass<UnreachableMachineBlockElim>
|
|
|
|
Y("unreachable-mbb-elimination",
|
|
|
|
"Remove unreachable machine basic blocks");
|
|
|
|
|
|
|
|
const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y;
|
|
|
|
|
|
|
|
bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
|
2008-08-14 21:01:00 +00:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> Reachable;
|
2008-08-04 23:54:43 +00:00
|
|
|
|
2009-01-28 13:14:17 +00:00
|
|
|
MMI = getAnalysisIfAvailable<MachineModuleInfo>();
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-04 23:54:43 +00:00
|
|
|
// Mark all reachable blocks.
|
2008-08-14 21:01:00 +00:00
|
|
|
for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> >
|
|
|
|
I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable);
|
|
|
|
I != E; ++I)
|
2008-08-04 23:54:43 +00:00
|
|
|
/* Mark all reachable blocks */;
|
|
|
|
|
|
|
|
// Loop over all dead blocks, remembering them and deleting all instructions
|
|
|
|
// in them.
|
|
|
|
std::vector<MachineBasicBlock*> DeadBlocks;
|
2008-08-06 23:16:52 +00:00
|
|
|
for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *BB = I;
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-06 23:16:52 +00:00
|
|
|
// Test for deadness.
|
|
|
|
if (!Reachable.count(BB)) {
|
2008-08-04 23:54:43 +00:00
|
|
|
DeadBlocks.push_back(BB);
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-04 23:54:43 +00:00
|
|
|
while (BB->succ_begin() != BB->succ_end()) {
|
|
|
|
MachineBasicBlock* succ = *BB->succ_begin();
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-04 23:54:43 +00:00
|
|
|
MachineBasicBlock::iterator start = succ->begin();
|
|
|
|
while (start != succ->end() &&
|
|
|
|
start->getOpcode() == TargetInstrInfo::PHI) {
|
|
|
|
for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
|
2008-10-03 15:45:36 +00:00
|
|
|
if (start->getOperand(i).isMBB() &&
|
2008-08-04 23:54:43 +00:00
|
|
|
start->getOperand(i).getMBB() == BB) {
|
|
|
|
start->RemoveOperand(i);
|
|
|
|
start->RemoveOperand(i-1);
|
|
|
|
}
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-06 23:16:52 +00:00
|
|
|
start++;
|
2008-08-04 23:54:43 +00:00
|
|
|
}
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-04 23:54:43 +00:00
|
|
|
BB->removeSuccessor(BB->succ_begin());
|
|
|
|
}
|
|
|
|
}
|
2008-08-06 23:16:52 +00:00
|
|
|
}
|
2004-07-02 05:46:10 +00:00
|
|
|
|
|
|
|
// Actually remove the blocks now.
|
2008-10-31 20:08:30 +00:00
|
|
|
for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *MBB = DeadBlocks[i];
|
|
|
|
// If there are any labels in the basic block, unregister them from
|
|
|
|
// MachineModuleInfo.
|
|
|
|
if (MMI && !MBB->empty()) {
|
|
|
|
for (MachineBasicBlock::iterator I = MBB->begin(),
|
|
|
|
E = MBB->end(); I != E; ++I) {
|
|
|
|
if (I->isLabel())
|
|
|
|
// The label ID # is always operand #0, an immediate.
|
|
|
|
MMI->InvalidateLabel(I->getOperand(0).getImm());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MBB->eraseFromParent();
|
|
|
|
}
|
2004-07-02 05:46:10 +00:00
|
|
|
|
2008-08-06 23:16:52 +00:00
|
|
|
// Cleanup PHI nodes.
|
|
|
|
for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *BB = I;
|
|
|
|
// Prune unneeded PHI entries.
|
2008-10-31 20:08:30 +00:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
|
2008-08-06 23:16:52 +00:00
|
|
|
BB->pred_end());
|
|
|
|
MachineBasicBlock::iterator phi = BB->begin();
|
|
|
|
while (phi != BB->end() &&
|
|
|
|
phi->getOpcode() == TargetInstrInfo::PHI) {
|
2008-08-08 18:00:05 +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);
|
|
|
|
}
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-06 23:16:52 +00:00
|
|
|
if (phi->getNumOperands() == 3) {
|
|
|
|
unsigned Input = phi->getOperand(1).getReg();
|
|
|
|
unsigned Output = phi->getOperand(0).getReg();
|
|
|
|
|
|
|
|
MachineInstr* temp = phi;
|
|
|
|
++phi;
|
|
|
|
temp->eraseFromParent();
|
|
|
|
|
|
|
|
if (Input != Output)
|
|
|
|
F.getRegInfo().replaceRegWith(Output, Input);
|
2008-08-08 18:00:05 +00:00
|
|
|
|
2008-08-06 23:16:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-10-31 20:08:30 +00:00
|
|
|
|
2008-08-06 23:16:52 +00:00
|
|
|
++phi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-05 00:30:10 +00:00
|
|
|
F.RenumberBlocks();
|
|
|
|
|
2008-08-04 23:54:43 +00:00
|
|
|
return DeadBlocks.size();
|
2004-07-02 05:46:10 +00:00
|
|
|
}
|