LLVM API Documentation
00001 //===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass is an extremely simple version of the SimplifyCFG pass. Its sole 00011 // job is to delete LLVM basic blocks that are not reachable from the entry 00012 // node. To do this, it performs a simple depth first traversal of the CFG, 00013 // then deletes any unvisited nodes. 00014 // 00015 // Note that this pass is really a hack. In particular, the instruction 00016 // selectors for various targets should just not generate code for unreachable 00017 // blocks. Until LLVM has a more systematic way of defining instruction 00018 // selectors, however, we cannot really expect them to handle additional 00019 // complexity. 00020 // 00021 //===----------------------------------------------------------------------===// 00022 00023 #include "llvm/CodeGen/Passes.h" 00024 #include "llvm/Constant.h" 00025 #include "llvm/Instructions.h" 00026 #include "llvm/Function.h" 00027 #include "llvm/Pass.h" 00028 #include "llvm/Type.h" 00029 #include "llvm/CodeGen/MachineFunctionPass.h" 00030 #include "llvm/CodeGen/MachineModuleInfo.h" 00031 #include "llvm/CodeGen/MachineRegisterInfo.h" 00032 #include "llvm/Support/CFG.h" 00033 #include "llvm/Support/Compiler.h" 00034 #include "llvm/Target/TargetInstrInfo.h" 00035 #include "llvm/ADT/DepthFirstIterator.h" 00036 #include "llvm/ADT/SmallPtrSet.h" 00037 using namespace llvm; 00038 00039 namespace { 00040 class VISIBILITY_HIDDEN UnreachableBlockElim : public FunctionPass { 00041 virtual bool runOnFunction(Function &F); 00042 public: 00043 static char ID; // Pass identification, replacement for typeid 00044 UnreachableBlockElim() : FunctionPass(&ID) {} 00045 }; 00046 } 00047 char UnreachableBlockElim::ID = 0; 00048 static RegisterPass<UnreachableBlockElim> 00049 X("unreachableblockelim", "Remove unreachable blocks from the CFG"); 00050 00051 FunctionPass *llvm::createUnreachableBlockEliminationPass() { 00052 return new UnreachableBlockElim(); 00053 } 00054 00055 bool UnreachableBlockElim::runOnFunction(Function &F) { 00056 SmallPtrSet<BasicBlock*, 8> Reachable; 00057 00058 // Mark all reachable blocks. 00059 for (df_ext_iterator<Function*, SmallPtrSet<BasicBlock*, 8> > I = 00060 df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I) 00061 /* Mark all reachable blocks */; 00062 00063 // Loop over all dead blocks, remembering them and deleting all instructions 00064 // in them. 00065 std::vector<BasicBlock*> DeadBlocks; 00066 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) 00067 if (!Reachable.count(I)) { 00068 BasicBlock *BB = I; 00069 DeadBlocks.push_back(BB); 00070 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { 00071 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); 00072 BB->getInstList().pop_front(); 00073 } 00074 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) 00075 (*SI)->removePredecessor(BB); 00076 BB->dropAllReferences(); 00077 } 00078 00079 // Actually remove the blocks now. 00080 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) 00081 DeadBlocks[i]->eraseFromParent(); 00082 00083 return DeadBlocks.size(); 00084 } 00085 00086 00087 namespace { 00088 class VISIBILITY_HIDDEN UnreachableMachineBlockElim : 00089 public MachineFunctionPass { 00090 virtual bool runOnMachineFunction(MachineFunction &F); 00091 MachineModuleInfo *MMI; 00092 public: 00093 static char ID; // Pass identification, replacement for typeid 00094 UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {} 00095 }; 00096 } 00097 char UnreachableMachineBlockElim::ID = 0; 00098 00099 static RegisterPass<UnreachableMachineBlockElim> 00100 Y("unreachable-mbb-elimination", 00101 "Remove unreachable machine basic blocks"); 00102 00103 const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y; 00104 00105 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) { 00106 SmallPtrSet<MachineBasicBlock*, 8> Reachable; 00107 00108 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 00109 00110 // Mark all reachable blocks. 00111 for (df_ext_iterator<MachineFunction*, SmallPtrSet<MachineBasicBlock*, 8> > 00112 I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); 00113 I != E; ++I) 00114 /* Mark all reachable blocks */; 00115 00116 // Loop over all dead blocks, remembering them and deleting all instructions 00117 // in them. 00118 std::vector<MachineBasicBlock*> DeadBlocks; 00119 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00120 MachineBasicBlock *BB = I; 00121 00122 // Test for deadness. 00123 if (!Reachable.count(BB)) { 00124 DeadBlocks.push_back(BB); 00125 00126 while (BB->succ_begin() != BB->succ_end()) { 00127 MachineBasicBlock* succ = *BB->succ_begin(); 00128 00129 MachineBasicBlock::iterator start = succ->begin(); 00130 while (start != succ->end() && 00131 start->getOpcode() == TargetInstrInfo::PHI) { 00132 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2) 00133 if (start->getOperand(i).isMBB() && 00134 start->getOperand(i).getMBB() == BB) { 00135 start->RemoveOperand(i); 00136 start->RemoveOperand(i-1); 00137 } 00138 00139 start++; 00140 } 00141 00142 BB->removeSuccessor(BB->succ_begin()); 00143 } 00144 } 00145 } 00146 00147 // Actually remove the blocks now. 00148 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) { 00149 MachineBasicBlock *MBB = DeadBlocks[i]; 00150 // If there are any labels in the basic block, unregister them from 00151 // MachineModuleInfo. 00152 if (MMI && !MBB->empty()) { 00153 for (MachineBasicBlock::iterator I = MBB->begin(), 00154 E = MBB->end(); I != E; ++I) { 00155 if (I->isLabel()) 00156 // The label ID # is always operand #0, an immediate. 00157 MMI->InvalidateLabel(I->getOperand(0).getImm()); 00158 } 00159 } 00160 MBB->eraseFromParent(); 00161 } 00162 00163 // Cleanup PHI nodes. 00164 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00165 MachineBasicBlock *BB = I; 00166 // Prune unneeded PHI entries. 00167 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(), 00168 BB->pred_end()); 00169 MachineBasicBlock::iterator phi = BB->begin(); 00170 while (phi != BB->end() && 00171 phi->getOpcode() == TargetInstrInfo::PHI) { 00172 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2) 00173 if (!preds.count(phi->getOperand(i).getMBB())) { 00174 phi->RemoveOperand(i); 00175 phi->RemoveOperand(i-1); 00176 } 00177 00178 if (phi->getNumOperands() == 3) { 00179 unsigned Input = phi->getOperand(1).getReg(); 00180 unsigned Output = phi->getOperand(0).getReg(); 00181 00182 MachineInstr* temp = phi; 00183 ++phi; 00184 temp->eraseFromParent(); 00185 00186 if (Input != Output) 00187 F.getRegInfo().replaceRegWith(Output, Input); 00188 00189 continue; 00190 } 00191 00192 ++phi; 00193 } 00194 } 00195 00196 F.RenumberBlocks(); 00197 00198 return DeadBlocks.size(); 00199 }