2004-07-02 13:46:10 +08:00
|
|
|
//===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
|
2005-04-22 06:36:52 +08:00
|
|
|
//
|
2004-07-02 13:46:10 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:36:52 +08:00
|
|
|
//
|
2004-07-02 13:46:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-22 06:36:52 +08:00
|
|
|
//
|
2004-07-02 13:46:10 +08: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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-08-01 08:34:30 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2008-08-05 07:54:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2009-08-01 08:34:30 +08:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2008-08-06 05:40:45 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2008-08-05 07:54:43 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2004-07-02 13:46:10 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class UnreachableBlockElim : public FunctionPass {
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2007-05-02 05:15:47 +08:00
|
|
|
public:
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
UnreachableBlockElim() : FunctionPass(ID) {
|
|
|
|
initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-09-10 01:53:39 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2009-09-10 01:53:39 +08:00
|
|
|
}
|
2004-07-02 13:46:10 +08:00
|
|
|
};
|
|
|
|
}
|
2008-05-13 08:00:25 +08:00
|
|
|
char UnreachableBlockElim::ID = 0;
|
2010-07-22 06:09:45 +08:00
|
|
|
INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Remove unreachable blocks from the CFG", false, false)
|
2004-07-02 13:46:10 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createUnreachableBlockEliminationPass() {
|
|
|
|
return new UnreachableBlockElim();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnreachableBlockElim::runOnFunction(Function &F) {
|
2008-08-15 05:01:00 +08:00
|
|
|
SmallPtrSet<BasicBlock*, 8> Reachable;
|
2004-07-02 13:46:10 +08:00
|
|
|
|
|
|
|
// Mark all reachable blocks.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (BasicBlock *BB : depth_first_ext(&F, Reachable))
|
|
|
|
(void)BB/* Mark all reachable blocks */;
|
2004-07-02 13:46:10 +08: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)
|
|
|
|
if (!Reachable.count(I)) {
|
2004-07-06 14:36:11 +08:00
|
|
|
BasicBlock *BB = I;
|
|
|
|
DeadBlocks.push_back(BB);
|
|
|
|
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
|
2009-08-01 04:28:14 +08:00
|
|
|
PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
|
2004-07-06 14:36:11 +08:00
|
|
|
BB->getInstList().pop_front();
|
|
|
|
}
|
2014-07-22 01:06:51 +08:00
|
|
|
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
|
|
|
|
(*SI)->removePredecessor(BB);
|
2004-07-06 14:36:11 +08:00
|
|
|
BB->dropAllReferences();
|
2004-07-02 13:46:10 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 07:54:43 +08:00
|
|
|
// Actually remove the blocks now.
|
2009-09-10 01:53:39 +08:00
|
|
|
for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
|
2008-08-05 07:54:43 +08:00
|
|
|
DeadBlocks[i]->eraseFromParent();
|
2009-09-10 01:53:39 +08:00
|
|
|
}
|
2008-08-05 07:54:43 +08:00
|
|
|
|
2015-01-15 19:41:30 +08:00
|
|
|
return !DeadBlocks.empty();
|
2008-08-05 07:54:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class UnreachableMachineBlockElim : public MachineFunctionPass {
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2008-11-01 04:08:30 +08:00
|
|
|
MachineModuleInfo *MMI;
|
2008-08-05 07:54:43 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-08-07 02:33:48 +08:00
|
|
|
UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
|
2008-08-05 07:54:43 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
char UnreachableMachineBlockElim::ID = 0;
|
|
|
|
|
2010-08-24 01:52:01 +08:00
|
|
|
INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Remove unreachable machine basic blocks", false, false)
|
2008-08-05 07:54:43 +08:00
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
|
2008-08-05 07:54:43 +08:00
|
|
|
|
2009-08-01 08:34:30 +08:00
|
|
|
void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2008-08-05 07:54:43 +08:00
|
|
|
bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
|
2008-08-15 05:01:00 +08:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> Reachable;
|
2010-09-30 04:57:19 +08:00
|
|
|
bool ModifiedPHI = false;
|
2008-08-05 07:54:43 +08:00
|
|
|
|
2009-01-28 21:14:17 +08:00
|
|
|
MMI = getAnalysisIfAvailable<MachineModuleInfo>();
|
2009-08-01 08:34:30 +08:00
|
|
|
MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
|
|
|
|
MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-05 07:54:43 +08:00
|
|
|
// Mark all reachable blocks.
|
2014-08-25 07:23:06 +08:00
|
|
|
for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable))
|
|
|
|
(void)BB/* Mark all reachable blocks */;
|
2008-08-05 07:54:43 +08:00
|
|
|
|
|
|
|
// Loop over all dead blocks, remembering them and deleting all instructions
|
|
|
|
// in them.
|
|
|
|
std::vector<MachineBasicBlock*> DeadBlocks;
|
2008-08-07 07:16:52 +08:00
|
|
|
for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *BB = I;
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-07 07:16:52 +08:00
|
|
|
// Test for deadness.
|
|
|
|
if (!Reachable.count(BB)) {
|
2008-08-05 07:54:43 +08:00
|
|
|
DeadBlocks.push_back(BB);
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2009-08-01 08:34:30 +08:00
|
|
|
// Update dominator and loop info.
|
|
|
|
if (MLI) MLI->removeBlock(BB);
|
|
|
|
if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
|
|
|
|
|
2008-08-05 07:54:43 +08:00
|
|
|
while (BB->succ_begin() != BB->succ_end()) {
|
|
|
|
MachineBasicBlock* succ = *BB->succ_begin();
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-05 07:54:43 +08:00
|
|
|
MachineBasicBlock::iterator start = succ->begin();
|
2010-02-10 03:54:29 +08:00
|
|
|
while (start != succ->end() && start->isPHI()) {
|
2008-08-05 07:54:43 +08:00
|
|
|
for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
|
2008-10-03 23:45:36 +08:00
|
|
|
if (start->getOperand(i).isMBB() &&
|
2008-08-05 07:54:43 +08:00
|
|
|
start->getOperand(i).getMBB() == BB) {
|
|
|
|
start->RemoveOperand(i);
|
|
|
|
start->RemoveOperand(i-1);
|
|
|
|
}
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-07 07:16:52 +08:00
|
|
|
start++;
|
2008-08-05 07:54:43 +08:00
|
|
|
}
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-05 07:54:43 +08:00
|
|
|
BB->removeSuccessor(BB->succ_begin());
|
|
|
|
}
|
|
|
|
}
|
2008-08-07 07:16:52 +08:00
|
|
|
}
|
2004-07-02 13:46:10 +08:00
|
|
|
|
|
|
|
// Actually remove the blocks now.
|
2010-03-14 10:24:55 +08:00
|
|
|
for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
|
|
|
|
DeadBlocks[i]->eraseFromParent();
|
2004-07-02 13:46:10 +08:00
|
|
|
|
2008-08-07 07:16:52 +08:00
|
|
|
// Cleanup PHI nodes.
|
|
|
|
for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *BB = I;
|
|
|
|
// Prune unneeded PHI entries.
|
2008-11-01 04:08:30 +08:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
|
2008-08-07 07:16:52 +08:00
|
|
|
BB->pred_end());
|
|
|
|
MachineBasicBlock::iterator phi = BB->begin();
|
2010-02-10 03:54:29 +08:00
|
|
|
while (phi != BB->end() && phi->isPHI()) {
|
2008-08-09 02:00:05 +08: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);
|
2010-09-30 04:57:19 +08:00
|
|
|
ModifiedPHI = true;
|
2008-08-09 02:00:05 +08:00
|
|
|
}
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-07 07:16:52 +08:00
|
|
|
if (phi->getNumOperands() == 3) {
|
|
|
|
unsigned Input = phi->getOperand(1).getReg();
|
|
|
|
unsigned Output = phi->getOperand(0).getReg();
|
|
|
|
|
|
|
|
MachineInstr* temp = phi;
|
|
|
|
++phi;
|
|
|
|
temp->eraseFromParent();
|
2010-09-30 04:57:19 +08:00
|
|
|
ModifiedPHI = true;
|
2008-08-07 07:16:52 +08:00
|
|
|
|
2011-05-27 13:04:51 +08:00
|
|
|
if (Input != Output) {
|
|
|
|
MachineRegisterInfo &MRI = F.getRegInfo();
|
|
|
|
MRI.constrainRegClass(Input, MRI.getRegClass(Output));
|
|
|
|
MRI.replaceRegWith(Output, Input);
|
|
|
|
}
|
2008-08-09 02:00:05 +08:00
|
|
|
|
2008-08-07 07:16:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-11-01 04:08:30 +08:00
|
|
|
|
2008-08-07 07:16:52 +08:00
|
|
|
++phi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-05 08:30:10 +08:00
|
|
|
F.RenumberBlocks();
|
|
|
|
|
2015-01-15 19:41:30 +08:00
|
|
|
return (!DeadBlocks.empty() || ModifiedPHI);
|
2004-07-02 13:46:10 +08:00
|
|
|
}
|