LLVM API Documentation

UnreachableBlockElim.cpp
Go to the documentation of this file.
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/ADT/DepthFirstIterator.h"
00025 #include "llvm/ADT/SmallPtrSet.h"
00026 #include "llvm/CodeGen/MachineDominators.h"
00027 #include "llvm/CodeGen/MachineFunctionPass.h"
00028 #include "llvm/CodeGen/MachineLoopInfo.h"
00029 #include "llvm/CodeGen/MachineModuleInfo.h"
00030 #include "llvm/CodeGen/MachineRegisterInfo.h"
00031 #include "llvm/IR/CFG.h"
00032 #include "llvm/IR/Constant.h"
00033 #include "llvm/IR/Dominators.h"
00034 #include "llvm/IR/Function.h"
00035 #include "llvm/IR/Instructions.h"
00036 #include "llvm/IR/Type.h"
00037 #include "llvm/Pass.h"
00038 #include "llvm/Target/TargetInstrInfo.h"
00039 using namespace llvm;
00040 
00041 namespace {
00042   class UnreachableBlockElim : public FunctionPass {
00043     bool runOnFunction(Function &F) override;
00044   public:
00045     static char ID; // Pass identification, replacement for typeid
00046     UnreachableBlockElim() : FunctionPass(ID) {
00047       initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
00048     }
00049 
00050     void getAnalysisUsage(AnalysisUsage &AU) const override {
00051       AU.addPreserved<DominatorTreeWrapperPass>();
00052     }
00053   };
00054 }
00055 char UnreachableBlockElim::ID = 0;
00056 INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
00057                 "Remove unreachable blocks from the CFG", false, false)
00058 
00059 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
00060   return new UnreachableBlockElim();
00061 }
00062 
00063 bool UnreachableBlockElim::runOnFunction(Function &F) {
00064   SmallPtrSet<BasicBlock*, 8> Reachable;
00065 
00066   // Mark all reachable blocks.
00067   for (BasicBlock *BB : depth_first_ext(&F, Reachable))
00068     (void)BB/* Mark all reachable blocks */;
00069 
00070   // Loop over all dead blocks, remembering them and deleting all instructions
00071   // in them.
00072   std::vector<BasicBlock*> DeadBlocks;
00073   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
00074     if (!Reachable.count(I)) {
00075       BasicBlock *BB = I;
00076       DeadBlocks.push_back(BB);
00077       while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
00078         PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
00079         BB->getInstList().pop_front();
00080       }
00081       for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
00082         (*SI)->removePredecessor(BB);
00083       BB->dropAllReferences();
00084     }
00085 
00086   // Actually remove the blocks now.
00087   for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
00088     DeadBlocks[i]->eraseFromParent();
00089   }
00090 
00091   return DeadBlocks.size();
00092 }
00093 
00094 
00095 namespace {
00096   class UnreachableMachineBlockElim : public MachineFunctionPass {
00097     bool runOnMachineFunction(MachineFunction &F) override;
00098     void getAnalysisUsage(AnalysisUsage &AU) const override;
00099     MachineModuleInfo *MMI;
00100   public:
00101     static char ID; // Pass identification, replacement for typeid
00102     UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
00103   };
00104 }
00105 char UnreachableMachineBlockElim::ID = 0;
00106 
00107 INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
00108   "Remove unreachable machine basic blocks", false, false)
00109 
00110 char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
00111 
00112 void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
00113   AU.addPreserved<MachineLoopInfo>();
00114   AU.addPreserved<MachineDominatorTree>();
00115   MachineFunctionPass::getAnalysisUsage(AU);
00116 }
00117 
00118 bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
00119   SmallPtrSet<MachineBasicBlock*, 8> Reachable;
00120   bool ModifiedPHI = false;
00121 
00122   MMI = getAnalysisIfAvailable<MachineModuleInfo>();
00123   MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
00124   MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
00125 
00126   // Mark all reachable blocks.
00127   for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable))
00128     (void)BB/* Mark all reachable blocks */;
00129 
00130   // Loop over all dead blocks, remembering them and deleting all instructions
00131   // in them.
00132   std::vector<MachineBasicBlock*> DeadBlocks;
00133   for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
00134     MachineBasicBlock *BB = I;
00135 
00136     // Test for deadness.
00137     if (!Reachable.count(BB)) {
00138       DeadBlocks.push_back(BB);
00139 
00140       // Update dominator and loop info.
00141       if (MLI) MLI->removeBlock(BB);
00142       if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
00143 
00144       while (BB->succ_begin() != BB->succ_end()) {
00145         MachineBasicBlock* succ = *BB->succ_begin();
00146 
00147         MachineBasicBlock::iterator start = succ->begin();
00148         while (start != succ->end() && start->isPHI()) {
00149           for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
00150             if (start->getOperand(i).isMBB() &&
00151                 start->getOperand(i).getMBB() == BB) {
00152               start->RemoveOperand(i);
00153               start->RemoveOperand(i-1);
00154             }
00155 
00156           start++;
00157         }
00158 
00159         BB->removeSuccessor(BB->succ_begin());
00160       }
00161     }
00162   }
00163 
00164   // Actually remove the blocks now.
00165   for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
00166     DeadBlocks[i]->eraseFromParent();
00167 
00168   // Cleanup PHI nodes.
00169   for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
00170     MachineBasicBlock *BB = I;
00171     // Prune unneeded PHI entries.
00172     SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
00173                                              BB->pred_end());
00174     MachineBasicBlock::iterator phi = BB->begin();
00175     while (phi != BB->end() && phi->isPHI()) {
00176       for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
00177         if (!preds.count(phi->getOperand(i).getMBB())) {
00178           phi->RemoveOperand(i);
00179           phi->RemoveOperand(i-1);
00180           ModifiedPHI = true;
00181         }
00182 
00183       if (phi->getNumOperands() == 3) {
00184         unsigned Input = phi->getOperand(1).getReg();
00185         unsigned Output = phi->getOperand(0).getReg();
00186 
00187         MachineInstr* temp = phi;
00188         ++phi;
00189         temp->eraseFromParent();
00190         ModifiedPHI = true;
00191 
00192         if (Input != Output) {
00193           MachineRegisterInfo &MRI = F.getRegInfo();
00194           MRI.constrainRegClass(Input, MRI.getRegClass(Output));
00195           MRI.replaceRegWith(Output, Input);
00196         }
00197 
00198         continue;
00199       }
00200 
00201       ++phi;
00202     }
00203   }
00204 
00205   F.RenumberBlocks();
00206 
00207   return (DeadBlocks.size() || ModifiedPHI);
00208 }