blob: 5288ca6727742d724cee4f93f6a65ad094027d4f [file] [log] [blame]
Chris Lattnerfc3c82a2004-07-02 05:46:10 +00001//===-- UnreachableBlockElim.cpp - Remove unreachable blocks for codegen --===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattnerfc3c82a2004-07-02 05:46:10 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattnerfc3c82a2004-07-02 05:46:10 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000010// This pass is an extremely simple version of the SimplifyCFG pass. Its sole
11// job is to delete LLVM basic blocks that are not reachable from the entry
12// node. To do this, it performs a simple depth first traversal of the CFG,
13// then deletes any unvisited nodes.
14//
15// Note that this pass is really a hack. In particular, the instruction
16// selectors for various targets should just not generate code for unreachable
17// blocks. Until LLVM has a more systematic way of defining instruction
18// selectors, however, we cannot really expect them to handle additional
19// complexity.
20//
21//===----------------------------------------------------------------------===//
22
Wei Mi302c35d2016-07-08 03:32:49 +000023#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/ADT/DepthFirstIterator.h"
25#include "llvm/ADT/SmallPtrSet.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000026#include "llvm/CodeGen/MachineDominators.h"
Owen Andersonbd3ba462008-08-04 23:54:43 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Krzysztof Parzyszek01351382017-04-28 21:56:33 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman4f3cfba2009-08-01 00:34:30 +000029#include "llvm/CodeGen/MachineLoopInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Owen Anderson5d2f8072008-08-05 21:40:45 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Wei Mi302c35d2016-07-08 03:32:49 +000032#include "llvm/CodeGen/Passes.h"
David Blaikie48319232017-11-08 01:01:31 +000033#include "llvm/CodeGen/TargetInstrInfo.h"
Chandler Carruth03e36d72014-03-04 11:45:46 +000034#include "llvm/IR/CFG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000035#include "llvm/IR/Constant.h"
Chandler Carruth56e13942014-01-13 09:26:24 +000036#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000037#include "llvm/IR/Function.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/Type.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000040#include "llvm/Pass.h"
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000041using namespace llvm;
42
Wei Mi302c35d2016-07-08 03:32:49 +000043static bool eliminateUnreachableBlock(Function &F) {
David Callahan8be61a82016-10-05 21:36:16 +000044 df_iterator_default_set<BasicBlock*> Reachable;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000045
46 // Mark all reachable blocks.
Craig Topper273fd112014-08-24 23:23:06 +000047 for (BasicBlock *BB : depth_first_ext(&F, Reachable))
48 (void)BB/* Mark all reachable blocks */;
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000049
50 // Loop over all dead blocks, remembering them and deleting all instructions
51 // in them.
52 std::vector<BasicBlock*> DeadBlocks;
53 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +000054 if (!Reachable.count(&*I)) {
55 BasicBlock *BB = &*I;
Chris Lattner7f0566c2004-07-06 06:36:11 +000056 DeadBlocks.push_back(BB);
57 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
Owen Andersona7235ea2009-07-31 20:28:14 +000058 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
Chris Lattner7f0566c2004-07-06 06:36:11 +000059 BB->getInstList().pop_front();
60 }
Duncan P. N. Exon Smithfacdfc62014-07-21 17:06:51 +000061 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
62 (*SI)->removePredecessor(BB);
Chris Lattner7f0566c2004-07-06 06:36:11 +000063 BB->dropAllReferences();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +000064 }
65
Owen Andersonbd3ba462008-08-04 23:54:43 +000066 // Actually remove the blocks now.
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000067 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i) {
Owen Andersonbd3ba462008-08-04 23:54:43 +000068 DeadBlocks[i]->eraseFromParent();
Andreas Neustifterff5dfdf2009-09-09 17:53:39 +000069 }
Owen Andersonbd3ba462008-08-04 23:54:43 +000070
Alexander Kornienkob4c62672015-01-15 11:41:30 +000071 return !DeadBlocks.empty();
Owen Andersonbd3ba462008-08-04 23:54:43 +000072}
73
Wei Mi302c35d2016-07-08 03:32:49 +000074namespace {
75class UnreachableBlockElimLegacyPass : public FunctionPass {
76 bool runOnFunction(Function &F) override {
77 return eliminateUnreachableBlock(F);
78 }
79
80public:
81 static char ID; // Pass identification, replacement for typeid
82 UnreachableBlockElimLegacyPass() : FunctionPass(ID) {
83 initializeUnreachableBlockElimLegacyPassPass(
84 *PassRegistry::getPassRegistry());
85 }
86
87 void getAnalysisUsage(AnalysisUsage &AU) const override {
88 AU.addPreserved<DominatorTreeWrapperPass>();
89 }
90};
91}
92char UnreachableBlockElimLegacyPass::ID = 0;
93INITIALIZE_PASS(UnreachableBlockElimLegacyPass, "unreachableblockelim",
94 "Remove unreachable blocks from the CFG", false, false)
95
96FunctionPass *llvm::createUnreachableBlockEliminationPass() {
97 return new UnreachableBlockElimLegacyPass();
98}
99
100PreservedAnalyses UnreachableBlockElimPass::run(Function &F,
101 FunctionAnalysisManager &AM) {
102 bool Changed = eliminateUnreachableBlock(F);
103 if (!Changed)
104 return PreservedAnalyses::all();
105 PreservedAnalyses PA;
106 PA.preserve<DominatorTreeAnalysis>();
107 return PA;
108}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000109
110namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000111 class UnreachableMachineBlockElim : public MachineFunctionPass {
Craig Topper9f998de2014-03-07 09:26:03 +0000112 bool runOnMachineFunction(MachineFunction &F) override;
113 void getAnalysisUsage(AnalysisUsage &AU) const override;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000114 MachineModuleInfo *MMI;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000115 public:
116 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +0000117 UnreachableMachineBlockElim() : MachineFunctionPass(ID) {}
Owen Andersonbd3ba462008-08-04 23:54:43 +0000118 };
119}
120char UnreachableMachineBlockElim::ID = 0;
121
Owen Anderson02dd53e2010-08-23 17:52:01 +0000122INITIALIZE_PASS(UnreachableMachineBlockElim, "unreachable-mbb-elimination",
Owen Andersonce665bd2010-10-07 22:25:06 +0000123 "Remove unreachable machine basic blocks", false, false)
Owen Andersonbd3ba462008-08-04 23:54:43 +0000124
Owen Anderson90c579d2010-08-06 18:33:48 +0000125char &llvm::UnreachableMachineBlockElimID = UnreachableMachineBlockElim::ID;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000126
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000127void UnreachableMachineBlockElim::getAnalysisUsage(AnalysisUsage &AU) const {
128 AU.addPreserved<MachineLoopInfo>();
129 AU.addPreserved<MachineDominatorTree>();
130 MachineFunctionPass::getAnalysisUsage(AU);
131}
132
Owen Andersonbd3ba462008-08-04 23:54:43 +0000133bool UnreachableMachineBlockElim::runOnMachineFunction(MachineFunction &F) {
David Callahan8be61a82016-10-05 21:36:16 +0000134 df_iterator_default_set<MachineBasicBlock*> Reachable;
Owen Andersonb0725312010-09-29 20:57:19 +0000135 bool ModifiedPHI = false;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000136
Duncan Sands1465d612009-01-28 13:14:17 +0000137 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000138 MachineDominatorTree *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
139 MachineLoopInfo *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000140
Owen Andersonbd3ba462008-08-04 23:54:43 +0000141 // Mark all reachable blocks.
Craig Topper273fd112014-08-24 23:23:06 +0000142 for (MachineBasicBlock *BB : depth_first_ext(&F, Reachable))
143 (void)BB/* Mark all reachable blocks */;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000144
145 // Loop over all dead blocks, remembering them and deleting all instructions
146 // in them.
147 std::vector<MachineBasicBlock*> DeadBlocks;
Owen Anderson9200e812008-08-06 23:16:52 +0000148 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000149 MachineBasicBlock *BB = &*I;
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000150
Owen Anderson9200e812008-08-06 23:16:52 +0000151 // Test for deadness.
152 if (!Reachable.count(BB)) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000153 DeadBlocks.push_back(BB);
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000154
Dan Gohman4f3cfba2009-08-01 00:34:30 +0000155 // Update dominator and loop info.
156 if (MLI) MLI->removeBlock(BB);
157 if (MDT && MDT->getNode(BB)) MDT->eraseNode(BB);
158
Owen Andersonbd3ba462008-08-04 23:54:43 +0000159 while (BB->succ_begin() != BB->succ_end()) {
160 MachineBasicBlock* succ = *BB->succ_begin();
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000161
Owen Andersonbd3ba462008-08-04 23:54:43 +0000162 MachineBasicBlock::iterator start = succ->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000163 while (start != succ->end() && start->isPHI()) {
Owen Andersonbd3ba462008-08-04 23:54:43 +0000164 for (unsigned i = start->getNumOperands() - 1; i >= 2; i-=2)
Dan Gohmand735b802008-10-03 15:45:36 +0000165 if (start->getOperand(i).isMBB() &&
Owen Andersonbd3ba462008-08-04 23:54:43 +0000166 start->getOperand(i).getMBB() == BB) {
167 start->RemoveOperand(i);
168 start->RemoveOperand(i-1);
169 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000170
Owen Anderson9200e812008-08-06 23:16:52 +0000171 start++;
Owen Andersonbd3ba462008-08-04 23:54:43 +0000172 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000173
Owen Andersonbd3ba462008-08-04 23:54:43 +0000174 BB->removeSuccessor(BB->succ_begin());
175 }
176 }
Owen Anderson9200e812008-08-06 23:16:52 +0000177 }
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000178
179 // Actually remove the blocks now.
Chris Lattner18589de2010-03-14 02:24:55 +0000180 for (unsigned i = 0, e = DeadBlocks.size(); i != e; ++i)
181 DeadBlocks[i]->eraseFromParent();
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000182
Owen Anderson9200e812008-08-06 23:16:52 +0000183 // Cleanup PHI nodes.
184 for (MachineFunction::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Duncan P. N. Exon Smithac4d7b62015-10-09 22:56:24 +0000185 MachineBasicBlock *BB = &*I;
Owen Anderson9200e812008-08-06 23:16:52 +0000186 // Prune unneeded PHI entries.
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000187 SmallPtrSet<MachineBasicBlock*, 8> preds(BB->pred_begin(),
Owen Anderson9200e812008-08-06 23:16:52 +0000188 BB->pred_end());
189 MachineBasicBlock::iterator phi = BB->begin();
Chris Lattner518bb532010-02-09 19:54:29 +0000190 while (phi != BB->end() && phi->isPHI()) {
Owen Andersonb12ab972008-08-08 18:00:05 +0000191 for (unsigned i = phi->getNumOperands() - 1; i >= 2; i-=2)
192 if (!preds.count(phi->getOperand(i).getMBB())) {
193 phi->RemoveOperand(i);
194 phi->RemoveOperand(i-1);
Owen Andersonb0725312010-09-29 20:57:19 +0000195 ModifiedPHI = true;
Owen Andersonb12ab972008-08-08 18:00:05 +0000196 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000197
Owen Anderson9200e812008-08-06 23:16:52 +0000198 if (phi->getNumOperands() == 3) {
Krzysztof Parzyszek01351382017-04-28 21:56:33 +0000199 const MachineOperand &Input = phi->getOperand(1);
200 const MachineOperand &Output = phi->getOperand(0);
201 unsigned InputReg = Input.getReg();
202 unsigned OutputReg = Output.getReg();
203 assert(Output.getSubReg() == 0 && "Cannot have output subregister");
Owen Andersonb0725312010-09-29 20:57:19 +0000204 ModifiedPHI = true;
Owen Anderson9200e812008-08-06 23:16:52 +0000205
Krzysztof Parzyszek01351382017-04-28 21:56:33 +0000206 if (InputReg != OutputReg) {
Cameron Zwarichf031d092011-05-27 05:04:51 +0000207 MachineRegisterInfo &MRI = F.getRegInfo();
Krzysztof Parzyszek01351382017-04-28 21:56:33 +0000208 unsigned InputSub = Input.getSubReg();
Mikael Holmen1df940a2017-05-10 06:33:43 +0000209 if (InputSub == 0 &&
Mikael Holmen2a53f0e2017-10-04 07:42:45 +0000210 MRI.constrainRegClass(InputReg, MRI.getRegClass(OutputReg)) &&
211 !Input.isUndef()) {
Krzysztof Parzyszek01351382017-04-28 21:56:33 +0000212 MRI.replaceRegWith(OutputReg, InputReg);
213 } else {
Mikael Holmen1df940a2017-05-10 06:33:43 +0000214 // The input register to the PHI has a subregister or it can't be
Mikael Holmen2a53f0e2017-10-04 07:42:45 +0000215 // constrained to the proper register class or it is undef:
Krzysztof Parzyszek01351382017-04-28 21:56:33 +0000216 // insert a COPY instead of simply replacing the output
217 // with the input.
218 const TargetInstrInfo *TII = F.getSubtarget().getInstrInfo();
219 BuildMI(*BB, BB->getFirstNonPHI(), phi->getDebugLoc(),
220 TII->get(TargetOpcode::COPY), OutputReg)
221 .addReg(InputReg, getRegState(Input), InputSub);
222 }
223 phi++->eraseFromParent();
Cameron Zwarichf031d092011-05-27 05:04:51 +0000224 }
Owen Anderson9200e812008-08-06 23:16:52 +0000225 continue;
226 }
Anton Korobeynikoved532ca2008-10-31 20:08:30 +0000227
Owen Anderson9200e812008-08-06 23:16:52 +0000228 ++phi;
229 }
230 }
231
Owen Anderson59c0d4f2008-08-05 00:30:10 +0000232 F.RenumberBlocks();
233
Alexander Kornienkob4c62672015-01-15 11:41:30 +0000234 return (!DeadBlocks.empty() || ModifiedPHI);
Chris Lattnerfc3c82a2004-07-02 05:46:10 +0000235}