X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FUnreachableBlockElim.cpp;h=c3b213cebe9543a95020d13b26a95c215e91661d;hb=58b1ac76d470eb5faa7e98feae97c4906d4d146e;hp=ca682fd65a0a7d53c66e87512d3d199cad55ce4b;hpb=5d2f8072d41bcde63c9f4267efe2d50bacc1e5f3;p=oota-llvm.git diff --git a/lib/CodeGen/UnreachableBlockElim.cpp b/lib/CodeGen/UnreachableBlockElim.cpp index ca682fd65a0..c3b213cebe9 100644 --- a/lib/CodeGen/UnreachableBlockElim.cpp +++ b/lib/CodeGen/UnreachableBlockElim.cpp @@ -27,11 +27,13 @@ #include "llvm/Pass.h" #include "llvm/Type.h" #include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/Support/CFG.h" #include "llvm/Support/Compiler.h" #include "llvm/Target/TargetInstrInfo.h" #include "llvm/ADT/DepthFirstIterator.h" +#include "llvm/ADT/SmallPtrSet.h" using namespace llvm; namespace { @@ -39,7 +41,7 @@ namespace { virtual bool runOnFunction(Function &F); public: static char ID; // Pass identification, replacement for typeid - UnreachableBlockElim() : FunctionPass((intptr_t)&ID) {} + UnreachableBlockElim() : FunctionPass(&ID) {} }; } char UnreachableBlockElim::ID = 0; @@ -51,11 +53,11 @@ FunctionPass *llvm::createUnreachableBlockEliminationPass() { } bool UnreachableBlockElim::runOnFunction(Function &F) { - std::set Reachable; + SmallPtrSet Reachable; // Mark all reachable blocks. - for (df_ext_iterator I = df_ext_begin(&F, Reachable), - E = df_ext_end(&F, Reachable); I != E; ++I) + for (df_ext_iterator > I = + df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); I != E; ++I) /* Mark all reachable blocks */; // Loop over all dead blocks, remembering them and deleting all instructions @@ -83,13 +85,13 @@ bool UnreachableBlockElim::runOnFunction(Function &F) { namespace { - class VISIBILITY_HIDDEN UnreachableMachineBlockElim : + class VISIBILITY_HIDDEN UnreachableMachineBlockElim : public MachineFunctionPass { virtual bool runOnMachineFunction(MachineFunction &F); - + MachineModuleInfo *MMI; public: static char ID; // Pass identification, replacement for typeid - UnreachableMachineBlockElim() : MachineFunctionPass((intptr_t)&ID) {} + UnreachableMachineBlockElim() : MachineFunctionPass(&ID) {} }; } char UnreachableMachineBlockElim::ID = 0; @@ -101,24 +103,29 @@ Y("unreachable-mbb-elimination", const PassInfo *const llvm::UnreachableMachineBlockElimID = &Y; bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) { - std::set Reachable; + SmallPtrSet Reachable; + + MMI = getAnalysisIfAvailable(); // Mark all reachable blocks. - for (df_ext_iterator I = df_ext_begin(&F, Reachable), - E = df_ext_end(&F, Reachable); I != E; ++I) + for (df_ext_iterator > + I = df_ext_begin(&F, Reachable), E = df_ext_end(&F, Reachable); + I != E; ++I) /* Mark all reachable blocks */; // Loop over all dead blocks, remembering them and deleting all instructions // in them. std::vector DeadBlocks; - for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) - if (!Reachable.count(I)) { - MachineBasicBlock *BB = I; + for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { + MachineBasicBlock *BB = I; + + // Test for deadness. + if (!Reachable.count(BB)) { DeadBlocks.push_back(BB); - + while (BB->succ_begin() != BB->succ_end()) { MachineBasicBlock* succ = *BB->succ_begin(); - + MachineBasicBlock::iterator start = succ->begin(); while (start != succ->end() && start->getOpcode() == TargetInstrInfo::PHI) { @@ -128,30 +135,65 @@ bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) { start->RemoveOperand(i); start->RemoveOperand(i-1); } - - if (start->getNumOperands() == 3) { - MachineInstr* phi = start; - unsigned Input = phi->getOperand(1).getReg(); - unsigned Output = phi->getOperand(0).getReg(); - - start++; - phi->eraseFromParent(); - - F.getRegInfo().replaceRegWith(Output, Input); - } else - start++; + + start++; } - + BB->removeSuccessor(BB->succ_begin()); } } + } // Actually remove the blocks now. - for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) - DeadBlocks[i]->eraseFromParent(); + 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(); + } + + // Cleanup PHI nodes. + for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) { + MachineBasicBlock *BB = I; + // Prune unneeded PHI entries. + SmallPtrSet preds(BB->pred_begin(), + BB->pred_end()); + MachineBasicBlock::iterator phi = BB->begin(); + while (phi != BB->end() && + phi->getOpcode() == TargetInstrInfo::PHI) { + for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2) + if (!preds.count(phi->getOperand(i).getMBB())) { + phi->RemoveOperand(i); + phi->RemoveOperand(i-1); + } + + 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); + + continue; + } + + ++phi; + } + } F.RenumberBlocks(); return DeadBlocks.size(); } -