blob: 883d2e17350df880b95cfca010234294ff801d47 [file] [log] [blame]
Justin Bogner0638b7ba2015-09-25 21:03:46 +00001//===- ADCE.cpp - Code to perform dead code elimination -------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb28986f2001-06-30 06:39:11 +00009//
Owen Anderson7686b552008-05-29 08:45:13 +000010// This file implements the Aggressive Dead Code Elimination pass. This pass
11// optimistically assumes that all instructions are dead until proven otherwise,
Nadav Rotem465834c2012-07-24 10:51:42 +000012// allowing it to eliminate dead computations that other DCE passes do not
Owen Anderson7686b552008-05-29 08:45:13 +000013// catch, particularly involving loop computations.
Chris Lattnerb28986f2001-06-30 06:39:11 +000014//
15//===----------------------------------------------------------------------===//
16
Justin Bogner19b67992015-10-30 23:13:18 +000017#include "llvm/Transforms/Scalar/ADCE.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000018#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/DepthFirstIterator.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000020#include "llvm/ADT/GraphTraits.h"
Mikael Holmen60181042017-11-03 14:15:08 +000021#include "llvm/ADT/MapVector.h"
David Callahanebcf9162016-12-13 16:42:18 +000022#include "llvm/ADT/PostOrderIterator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/SmallPtrSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/Statistic.h"
James Molloyefbba722015-09-10 10:22:12 +000026#include "llvm/Analysis/GlobalsModRef.h"
David Callahan012d1c02016-08-24 00:10:06 +000027#include "llvm/Analysis/IteratedDominanceFrontier.h"
28#include "llvm/Analysis/PostDominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000030#include "llvm/IR/CFG.h"
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000031#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000032#include "llvm/IR/DebugLoc.h"
Chijun Sima21a8b602018-08-03 05:08:17 +000033#include "llvm/IR/DomTreeUpdater.h"
Jakub Kuderski2724d452017-08-22 16:30:21 +000034#include "llvm/IR/Dominators.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000035#include "llvm/IR/Function.h"
Chijun Sima21a8b602018-08-03 05:08:17 +000036#include "llvm/IR/IRBuilder.h"
Chandler Carruth83948572014-03-04 10:30:26 +000037#include "llvm/IR/InstIterator.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000038#include "llvm/IR/InstrTypes.h"
39#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Instructions.h"
41#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000042#include "llvm/IR/PassManager.h"
43#include "llvm/IR/Use.h"
44#include "llvm/IR/Value.h"
Owen Anderson7686b552008-05-29 08:45:13 +000045#include "llvm/Pass.h"
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +000046#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000047#include "llvm/Support/Casting.h"
48#include "llvm/Support/CommandLine.h"
49#include "llvm/Support/Debug.h"
50#include "llvm/Support/raw_ostream.h"
Justin Bogner19b67992015-10-30 23:13:18 +000051#include "llvm/Transforms/Scalar.h"
Eugene Zelenko3b879392017-10-13 21:17:07 +000052#include <cassert>
53#include <cstddef>
54#include <utility>
55
Chris Lattnerfc7bdac2003-12-19 09:08:34 +000056using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000057
Chandler Carruth964daaa2014-04-22 02:55:47 +000058#define DEBUG_TYPE "adce"
59
Owen Anderson7686b552008-05-29 08:45:13 +000060STATISTIC(NumRemoved, "Number of instructions removed");
David Callahanebcf9162016-12-13 16:42:18 +000061STATISTIC(NumBranchesRemoved, "Number of branch instructions removed");
Chris Lattner019f3642002-05-06 17:27:57 +000062
Tobias Grosser335b6bf2017-03-14 10:18:11 +000063// This is a temporary option until we change the interface to this pass based
64// on optimization level.
David Callahan947be0f2016-08-16 14:31:51 +000065static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow",
David Callahanebcf9162016-12-13 16:42:18 +000066 cl::init(true), cl::Hidden);
67
68// This option enables removing of may-be-infinite loops which have no other
69// effect.
70static cl::opt<bool> RemoveLoops("adce-remove-loops", cl::init(false),
71 cl::Hidden);
David Callahan947be0f2016-08-16 14:31:51 +000072
David Callahancc5cd4d2016-08-03 04:28:39 +000073namespace {
Eugene Zelenko3b879392017-10-13 21:17:07 +000074
David Callahan947be0f2016-08-16 14:31:51 +000075/// Information about Instructions
76struct InstInfoType {
77 /// True if the associated instruction is live.
78 bool Live = false;
Eugene Zelenko3b879392017-10-13 21:17:07 +000079
David Callahan947be0f2016-08-16 14:31:51 +000080 /// Quick access to information for block containing associated Instruction.
81 struct BlockInfoType *Block = nullptr;
82};
83
84/// Information about basic blocks relevant to dead code elimination.
85struct BlockInfoType {
86 /// True when this block contains a live instructions.
87 bool Live = false;
Eugene Zelenko3b879392017-10-13 21:17:07 +000088
David Callahan947be0f2016-08-16 14:31:51 +000089 /// True when this block ends in an unconditional branch.
90 bool UnconditionalBranch = false;
Eugene Zelenko3b879392017-10-13 21:17:07 +000091
David Callahanc165a4e2016-09-19 23:17:58 +000092 /// True when this block is known to have live PHI nodes.
93 bool HasLivePhiNodes = false;
Eugene Zelenko3b879392017-10-13 21:17:07 +000094
David Callahanc165a4e2016-09-19 23:17:58 +000095 /// Control dependence sources need to be live for this block.
96 bool CFLive = false;
David Callahan947be0f2016-08-16 14:31:51 +000097
98 /// Quick access to the LiveInfo for the terminator,
99 /// holds the value &InstInfo[Terminator]
100 InstInfoType *TerminatorLiveInfo = nullptr;
101
David Callahan947be0f2016-08-16 14:31:51 +0000102 /// Corresponding BasicBlock.
103 BasicBlock *BB = nullptr;
104
David Callahanebcf9162016-12-13 16:42:18 +0000105 /// Cache of BB->getTerminator().
David Callahan947be0f2016-08-16 14:31:51 +0000106 TerminatorInst *Terminator = nullptr;
David Callahanebcf9162016-12-13 16:42:18 +0000107
108 /// Post-order numbering of reverse control flow graph.
109 unsigned PostOrder;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000110
111 bool terminatorIsLive() const { return TerminatorLiveInfo->Live; }
David Callahan947be0f2016-08-16 14:31:51 +0000112};
113
David Callahan45e442e2016-08-05 19:38:11 +0000114class AggressiveDeadCodeElimination {
David Callahancc5cd4d2016-08-03 04:28:39 +0000115 Function &F;
Jakub Kuderski2724d452017-08-22 16:30:21 +0000116
117 // ADCE does not use DominatorTree per se, but it updates it to preserve the
118 // analysis.
Chijun Simaeacad792018-08-04 02:50:12 +0000119 DominatorTree *DT;
David Callahan012d1c02016-08-24 00:10:06 +0000120 PostDominatorTree &PDT;
David Callahan947be0f2016-08-16 14:31:51 +0000121
122 /// Mapping of blocks to associated information, an element in BlockInfoVec.
Mikael Holmen60181042017-11-03 14:15:08 +0000123 /// Use MapVector to get deterministic iteration order.
124 MapVector<BasicBlock *, BlockInfoType> BlockInfo;
David Callahan947be0f2016-08-16 14:31:51 +0000125 bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; }
126
127 /// Mapping of instructions to associated information.
128 DenseMap<Instruction *, InstInfoType> InstInfo;
129 bool isLive(Instruction *I) { return InstInfo[I].Live; }
130
David Callahan45e442e2016-08-05 19:38:11 +0000131 /// Instructions known to be live where we need to mark
132 /// reaching definitions as live.
David Callahancc5cd4d2016-08-03 04:28:39 +0000133 SmallVector<Instruction *, 128> Worklist;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000134
David Callahan45e442e2016-08-05 19:38:11 +0000135 /// Debug info scopes around a live instruction.
David Callahancc5cd4d2016-08-03 04:28:39 +0000136 SmallPtrSet<const Metadata *, 32> AliveScopes;
David Callahan45e442e2016-08-05 19:38:11 +0000137
David Callahan947be0f2016-08-16 14:31:51 +0000138 /// Set of blocks with not known to have live terminators.
139 SmallPtrSet<BasicBlock *, 16> BlocksWithDeadTerminators;
140
David Callahanebcf9162016-12-13 16:42:18 +0000141 /// The set of blocks which we have determined whose control
142 /// dependence sources must be live and which have not had
Tobias Grosser335b6bf2017-03-14 10:18:11 +0000143 /// those dependences analyzed.
David Callahan947be0f2016-08-16 14:31:51 +0000144 SmallPtrSet<BasicBlock *, 16> NewLiveBlocks;
145
146 /// Set up auxiliary data structures for Instructions and BasicBlocks and
147 /// initialize the Worklist to the set of must-be-live Instruscions.
David Callahan45e442e2016-08-05 19:38:11 +0000148 void initialize();
Eugene Zelenko3b879392017-10-13 21:17:07 +0000149
David Callahan947be0f2016-08-16 14:31:51 +0000150 /// Return true for operations which are always treated as live.
David Callahan45e442e2016-08-05 19:38:11 +0000151 bool isAlwaysLive(Instruction &I);
Eugene Zelenko3b879392017-10-13 21:17:07 +0000152
David Callahan947be0f2016-08-16 14:31:51 +0000153 /// Return true for instrumentation instructions for value profiling.
David Callahan45e442e2016-08-05 19:38:11 +0000154 bool isInstrumentsConstant(Instruction &I);
David Callahan45e442e2016-08-05 19:38:11 +0000155
156 /// Propagate liveness to reaching definitions.
157 void markLiveInstructions();
Eugene Zelenko3b879392017-10-13 21:17:07 +0000158
David Callahan45e442e2016-08-05 19:38:11 +0000159 /// Mark an instruction as live.
David Callahan947be0f2016-08-16 14:31:51 +0000160 void markLive(Instruction *I);
Eugene Zelenko3b879392017-10-13 21:17:07 +0000161
David Callahanebcf9162016-12-13 16:42:18 +0000162 /// Mark a block as live.
163 void markLive(BlockInfoType &BB);
164 void markLive(BasicBlock *BB) { markLive(BlockInfo[BB]); }
165
David Callahanc165a4e2016-09-19 23:17:58 +0000166 /// Mark terminators of control predecessors of a PHI node live.
167 void markPhiLive(PHINode *PN);
David Callahan947be0f2016-08-16 14:31:51 +0000168
169 /// Record the Debug Scopes which surround live debug information.
David Callahancc5cd4d2016-08-03 04:28:39 +0000170 void collectLiveScopes(const DILocalScope &LS);
171 void collectLiveScopes(const DILocation &DL);
David Callahan947be0f2016-08-16 14:31:51 +0000172
173 /// Analyze dead branches to find those whose branches are the sources
174 /// of control dependences impacting a live block. Those branches are
175 /// marked live.
176 void markLiveBranchesFromControlDependences();
David Callahan45e442e2016-08-05 19:38:11 +0000177
Fangrui Song956ee792018-03-30 22:22:31 +0000178 /// Remove instructions not marked live, return if any instruction was
179 /// removed.
David Callahan45e442e2016-08-05 19:38:11 +0000180 bool removeDeadInstructions();
181
Tobias Grosser335b6bf2017-03-14 10:18:11 +0000182 /// Identify connected sections of the control flow graph which have
David Callahanebcf9162016-12-13 16:42:18 +0000183 /// dead terminators and rewrite the control flow graph to remove them.
184 void updateDeadRegions();
185
186 /// Set the BlockInfo::PostOrder field based on a post-order
187 /// numbering of the reverse control flow graph.
188 void computeReversePostOrder();
189
190 /// Make the terminator of this block an unconditional branch to \p Target.
191 void makeUnconditional(BasicBlock *BB, BasicBlock *Target);
192
David Callahancc5cd4d2016-08-03 04:28:39 +0000193public:
Chijun Simaeacad792018-08-04 02:50:12 +0000194 AggressiveDeadCodeElimination(Function &F, DominatorTree *DT,
Eugene Zelenko3b879392017-10-13 21:17:07 +0000195 PostDominatorTree &PDT)
196 : F(F), DT(DT), PDT(PDT) {}
197
198 bool performDeadCodeElimination();
David Callahancc5cd4d2016-08-03 04:28:39 +0000199};
Eugene Zelenko3b879392017-10-13 21:17:07 +0000200
201} // end anonymous namespace
David Callahancc5cd4d2016-08-03 04:28:39 +0000202
David Callahan45e442e2016-08-05 19:38:11 +0000203bool AggressiveDeadCodeElimination::performDeadCodeElimination() {
204 initialize();
205 markLiveInstructions();
206 return removeDeadInstructions();
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000207}
208
David Callahan947be0f2016-08-16 14:31:51 +0000209static bool isUnconditionalBranch(TerminatorInst *Term) {
David Callahanebcf9162016-12-13 16:42:18 +0000210 auto *BR = dyn_cast<BranchInst>(Term);
David Callahan947be0f2016-08-16 14:31:51 +0000211 return BR && BR->isUnconditional();
212}
213
David Callahan45e442e2016-08-05 19:38:11 +0000214void AggressiveDeadCodeElimination::initialize() {
David Callahan947be0f2016-08-16 14:31:51 +0000215 auto NumBlocks = F.size();
216
217 // We will have an entry in the map for each block so we grow the
218 // structure to twice that size to keep the load factor low in the hash table.
219 BlockInfo.reserve(NumBlocks);
220 size_t NumInsts = 0;
David Callahan012d1c02016-08-24 00:10:06 +0000221
David Callahan947be0f2016-08-16 14:31:51 +0000222 // Iterate over blocks and initialize BlockInfoVec entries, count
223 // instructions to size the InstInfo hash table.
224 for (auto &BB : F) {
225 NumInsts += BB.size();
226 auto &Info = BlockInfo[&BB];
227 Info.BB = &BB;
228 Info.Terminator = BB.getTerminator();
229 Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator);
230 }
231
232 // Initialize instruction map and set pointers to block info.
233 InstInfo.reserve(NumInsts);
234 for (auto &BBInfo : BlockInfo)
235 for (Instruction &I : *BBInfo.second.BB)
236 InstInfo[&I].Block = &BBInfo.second;
237
238 // Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not
239 // add any more elements to either after this point.
240 for (auto &BBInfo : BlockInfo)
241 BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator];
242
David Callahan45e442e2016-08-05 19:38:11 +0000243 // Collect the set of "root" instructions that are known live.
244 for (Instruction &I : instructions(F))
245 if (isAlwaysLive(I))
David Callahan947be0f2016-08-16 14:31:51 +0000246 markLive(&I);
247
248 if (!RemoveControlFlowFlag)
249 return;
250
David Callahanebcf9162016-12-13 16:42:18 +0000251 if (!RemoveLoops) {
252 // This stores state for the depth-first iterator. In addition
253 // to recording which nodes have been visited we also record whether
254 // a node is currently on the "stack" of active ancestors of the current
255 // node.
Eugene Zelenko3b879392017-10-13 21:17:07 +0000256 using StatusMap = DenseMap<BasicBlock *, bool>;
257
David Callahanebcf9162016-12-13 16:42:18 +0000258 class DFState : public StatusMap {
259 public:
260 std::pair<StatusMap::iterator, bool> insert(BasicBlock *BB) {
261 return StatusMap::insert(std::make_pair(BB, true));
262 }
David Callahan947be0f2016-08-16 14:31:51 +0000263
David Callahanebcf9162016-12-13 16:42:18 +0000264 // Invoked after we have visited all children of a node.
265 void completed(BasicBlock *BB) { (*this)[BB] = false; }
266
267 // Return true if \p BB is currently on the active stack
268 // of ancestors.
269 bool onStack(BasicBlock *BB) {
270 auto Iter = find(BB);
271 return Iter != end() && Iter->second;
272 }
273 } State;
Taewook Ohd3f1ec92017-01-26 04:32:40 +0000274
David Callahanebcf9162016-12-13 16:42:18 +0000275 State.reserve(F.size());
276 // Iterate over blocks in depth-first pre-order and
277 // treat all edges to a block already seen as loop back edges
278 // and mark the branch live it if there is a back edge.
279 for (auto *BB: depth_first_ext(&F.getEntryBlock(), State)) {
280 TerminatorInst *Term = BB->getTerminator();
281 if (isLive(Term))
282 continue;
283
284 for (auto *Succ : successors(BB))
285 if (State.onStack(Succ)) {
286 // back edge....
287 markLive(Term);
288 break;
289 }
290 }
291 }
292
Jakub Kuderski638c0852017-08-15 18:14:57 +0000293 // Mark blocks live if there is no path from the block to a
294 // return of the function.
295 // We do this by seeing which of the postdomtree root children exit the
296 // program, and for all others, mark the subtree live.
297 for (auto &PDTChild : children<DomTreeNode *>(PDT.getRootNode())) {
298 auto *BB = PDTChild->getBlock();
299 auto &Info = BlockInfo[BB];
300 // Real function return
301 if (isa<ReturnInst>(Info.Terminator)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000302 LLVM_DEBUG(dbgs() << "post-dom root child is a return: " << BB->getName()
303 << '\n';);
Tobias Grosserf818c332017-03-02 21:08:37 +0000304 continue;
305 }
Jakub Kuderski638c0852017-08-15 18:14:57 +0000306
307 // This child is something else, like an infinite loop.
308 for (auto DFNode : depth_first(PDTChild))
309 markLive(BlockInfo[DFNode->getBlock()].Terminator);
David Callahan012d1c02016-08-24 00:10:06 +0000310 }
David Callahan947be0f2016-08-16 14:31:51 +0000311
312 // Treat the entry block as always live
313 auto *BB = &F.getEntryBlock();
314 auto &EntryInfo = BlockInfo[BB];
315 EntryInfo.Live = true;
316 if (EntryInfo.UnconditionalBranch)
317 markLive(EntryInfo.Terminator);
318
319 // Build initial collection of blocks with dead terminators
320 for (auto &BBInfo : BlockInfo)
321 if (!BBInfo.second.terminatorIsLive())
322 BlocksWithDeadTerminators.insert(BBInfo.second.BB);
David Callahan45e442e2016-08-05 19:38:11 +0000323}
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000324
David Callahan45e442e2016-08-05 19:38:11 +0000325bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) {
David Callahan45e442e2016-08-05 19:38:11 +0000326 // TODO -- use llvm::isInstructionTriviallyDead
David Callahan947be0f2016-08-16 14:31:51 +0000327 if (I.isEHPad() || I.mayHaveSideEffects()) {
David Callahan45e442e2016-08-05 19:38:11 +0000328 // Skip any value profile instrumentation calls if they are
329 // instrumenting constants.
David Callahan947be0f2016-08-16 14:31:51 +0000330 if (isInstrumentsConstant(I))
331 return false;
332 return true;
David Callahan45e442e2016-08-05 19:38:11 +0000333 }
Chandler Carruth9ae926b2018-08-26 09:51:22 +0000334 if (!I.isTerminator())
David Callahan947be0f2016-08-16 14:31:51 +0000335 return false;
336 if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I)))
337 return false;
338 return true;
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000339}
340
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +0000341// Check if this instruction is a runtime call for value profiling and
342// if it's instrumenting a constant.
David Callahan45e442e2016-08-05 19:38:11 +0000343bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {
344 // TODO -- move this test into llvm::isInstructionTriviallyDead
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +0000345 if (CallInst *CI = dyn_cast<CallInst>(&I))
346 if (Function *Callee = CI->getCalledFunction())
347 if (Callee->getName().equals(getInstrProfValueProfFuncName()))
348 if (isa<Constant>(CI->getArgOperand(0)))
349 return true;
350 return false;
351}
352
David Callahan45e442e2016-08-05 19:38:11 +0000353void AggressiveDeadCodeElimination::markLiveInstructions() {
David Callahan947be0f2016-08-16 14:31:51 +0000354 // Propagate liveness backwards to operands.
355 do {
356 // Worklist holds newly discovered live instructions
357 // where we need to mark the inputs as live.
358 while (!Worklist.empty()) {
359 Instruction *LiveInst = Worklist.pop_back_val();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000360 LLVM_DEBUG(dbgs() << "work live: "; LiveInst->dump(););
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000361
David Callahan947be0f2016-08-16 14:31:51 +0000362 for (Use &OI : LiveInst->operands())
363 if (Instruction *Inst = dyn_cast<Instruction>(OI))
364 markLive(Inst);
David Callahanebcf9162016-12-13 16:42:18 +0000365
David Callahanc165a4e2016-09-19 23:17:58 +0000366 if (auto *PN = dyn_cast<PHINode>(LiveInst))
367 markPhiLive(PN);
Hal Finkel8626ed22015-02-15 15:51:25 +0000368 }
David Callahanebcf9162016-12-13 16:42:18 +0000369
370 // After data flow liveness has been identified, examine which branch
371 // decisions are required to determine live instructions are executed.
David Callahan947be0f2016-08-16 14:31:51 +0000372 markLiveBranchesFromControlDependences();
David Callahan012d1c02016-08-24 00:10:06 +0000373
David Callahan947be0f2016-08-16 14:31:51 +0000374 } while (!Worklist.empty());
David Callahan947be0f2016-08-16 14:31:51 +0000375}
376
377void AggressiveDeadCodeElimination::markLive(Instruction *I) {
David Callahan947be0f2016-08-16 14:31:51 +0000378 auto &Info = InstInfo[I];
379 if (Info.Live)
380 return;
381
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000382 LLVM_DEBUG(dbgs() << "mark live: "; I->dump());
David Callahan947be0f2016-08-16 14:31:51 +0000383 Info.Live = true;
384 Worklist.push_back(I);
385
David Callahanebcf9162016-12-13 16:42:18 +0000386 // Collect the live debug info scopes attached to this instruction.
387 if (const DILocation *DL = I->getDebugLoc())
388 collectLiveScopes(*DL);
389
David Callahan947be0f2016-08-16 14:31:51 +0000390 // Mark the containing block live
391 auto &BBInfo = *Info.Block;
David Callahanebcf9162016-12-13 16:42:18 +0000392 if (BBInfo.Terminator == I) {
David Callahan947be0f2016-08-16 14:31:51 +0000393 BlocksWithDeadTerminators.erase(BBInfo.BB);
David Callahanebcf9162016-12-13 16:42:18 +0000394 // For live terminators, mark destination blocks
395 // live to preserve this control flow edges.
396 if (!BBInfo.UnconditionalBranch)
397 for (auto *BB : successors(I->getParent()))
398 markLive(BB);
399 }
400 markLive(BBInfo);
401}
402
403void AggressiveDeadCodeElimination::markLive(BlockInfoType &BBInfo) {
David Callahan947be0f2016-08-16 14:31:51 +0000404 if (BBInfo.Live)
405 return;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000406 LLVM_DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n');
David Callahan947be0f2016-08-16 14:31:51 +0000407 BBInfo.Live = true;
David Callahanc165a4e2016-09-19 23:17:58 +0000408 if (!BBInfo.CFLive) {
409 BBInfo.CFLive = true;
410 NewLiveBlocks.insert(BBInfo.BB);
411 }
David Callahan947be0f2016-08-16 14:31:51 +0000412
413 // Mark unconditional branches at the end of live
414 // blocks as live since there is no work to do for them later
David Callahanebcf9162016-12-13 16:42:18 +0000415 if (BBInfo.UnconditionalBranch)
David Callahan012d1c02016-08-24 00:10:06 +0000416 markLive(BBInfo.Terminator);
David Callahan45e442e2016-08-05 19:38:11 +0000417}
418
419void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) {
420 if (!AliveScopes.insert(&LS).second)
421 return;
David Callahan947be0f2016-08-16 14:31:51 +0000422
David Callahan45e442e2016-08-05 19:38:11 +0000423 if (isa<DISubprogram>(LS))
424 return;
David Callahan947be0f2016-08-16 14:31:51 +0000425
David Callahan45e442e2016-08-05 19:38:11 +0000426 // Tail-recurse through the scope chain.
427 collectLiveScopes(cast<DILocalScope>(*LS.getScope()));
428}
429
430void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {
431 // Even though DILocations are not scopes, shove them into AliveScopes so we
432 // don't revisit them.
433 if (!AliveScopes.insert(&DL).second)
434 return;
David Callahan947be0f2016-08-16 14:31:51 +0000435
David Callahan45e442e2016-08-05 19:38:11 +0000436 // Collect live scopes from the scope chain.
437 collectLiveScopes(*DL.getScope());
David Callahan947be0f2016-08-16 14:31:51 +0000438
David Callahan45e442e2016-08-05 19:38:11 +0000439 // Tail-recurse through the inlined-at chain.
440 if (const DILocation *IA = DL.getInlinedAt())
441 collectLiveScopes(*IA);
442}
443
David Callahanc165a4e2016-09-19 23:17:58 +0000444void AggressiveDeadCodeElimination::markPhiLive(PHINode *PN) {
445 auto &Info = BlockInfo[PN->getParent()];
446 // Only need to check this once per block.
447 if (Info.HasLivePhiNodes)
448 return;
449 Info.HasLivePhiNodes = true;
450
451 // If a predecessor block is not live, mark it as control-flow live
452 // which will trigger marking live branches upon which
453 // that block is control dependent.
454 for (auto *PredBB : predecessors(Info.BB)) {
455 auto &Info = BlockInfo[PredBB];
456 if (!Info.CFLive) {
457 Info.CFLive = true;
458 NewLiveBlocks.insert(PredBB);
459 }
460 }
461}
462
David Callahan947be0f2016-08-16 14:31:51 +0000463void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() {
David Callahan012d1c02016-08-24 00:10:06 +0000464 if (BlocksWithDeadTerminators.empty())
465 return;
466
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000467 LLVM_DEBUG({
David Callahan012d1c02016-08-24 00:10:06 +0000468 dbgs() << "new live blocks:\n";
469 for (auto *BB : NewLiveBlocks)
470 dbgs() << "\t" << BB->getName() << '\n';
471 dbgs() << "dead terminator blocks:\n";
472 for (auto *BB : BlocksWithDeadTerminators)
473 dbgs() << "\t" << BB->getName() << '\n';
474 });
475
476 // The dominance frontier of a live block X in the reverse
477 // control graph is the set of blocks upon which X is control
478 // dependent. The following sequence computes the set of blocks
479 // which currently have dead terminators that are control
480 // dependence sources of a block which is in NewLiveBlocks.
481
482 SmallVector<BasicBlock *, 32> IDFBlocks;
483 ReverseIDFCalculator IDFs(PDT);
484 IDFs.setDefiningBlocks(NewLiveBlocks);
485 IDFs.setLiveInBlocks(BlocksWithDeadTerminators);
486 IDFs.calculate(IDFBlocks);
David Callahan947be0f2016-08-16 14:31:51 +0000487 NewLiveBlocks.clear();
David Callahan012d1c02016-08-24 00:10:06 +0000488
489 // Dead terminators which control live blocks are now marked live.
David Callahanebcf9162016-12-13 16:42:18 +0000490 for (auto *BB : IDFBlocks) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000491 LLVM_DEBUG(dbgs() << "live control in: " << BB->getName() << '\n');
David Callahan012d1c02016-08-24 00:10:06 +0000492 markLive(BB->getTerminator());
493 }
David Callahan45e442e2016-08-05 19:38:11 +0000494}
495
David Callahanc165a4e2016-09-19 23:17:58 +0000496//===----------------------------------------------------------------------===//
497//
498// Routines to update the CFG and SSA information before removing dead code.
499//
500//===----------------------------------------------------------------------===//
David Callahan45e442e2016-08-05 19:38:11 +0000501bool AggressiveDeadCodeElimination::removeDeadInstructions() {
David Callahanebcf9162016-12-13 16:42:18 +0000502 // Updates control and dataflow around dead blocks
503 updateDeadRegions();
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000504
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000505 LLVM_DEBUG({
David Callahanebcf9162016-12-13 16:42:18 +0000506 for (Instruction &I : instructions(F)) {
507 // Check if the instruction is alive.
508 if (isLive(&I))
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000509 continue;
510
Hsiangkai Wangef72e482018-08-06 03:59:47 +0000511 if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
David Callahanebcf9162016-12-13 16:42:18 +0000512 // Check if the scope of this variable location is alive.
513 if (AliveScopes.count(DII->getDebugLoc()->getScope()))
514 continue;
515
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000516 // If intrinsic is pointing at a live SSA value, there may be an
517 // earlier optimization bug: if we know the location of the variable,
518 // why isn't the scope of the location alive?
519 if (Value *V = DII->getVariableLocation())
520 if (Instruction *II = dyn_cast<Instruction>(V))
David Callahan947be0f2016-08-16 14:31:51 +0000521 if (isLive(II))
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000522 dbgs() << "Dropping debug info for " << *DII << "\n";
David Callahanebcf9162016-12-13 16:42:18 +0000523 }
524 }
525 });
526
527 // The inverse of the live set is the dead set. These are those instructions
528 // that have no side effects and do not influence the control flow or return
529 // value of the function, and may therefore be deleted safely.
530 // NOTE: We reuse the Worklist vector here for memory efficiency.
531 for (Instruction &I : instructions(F)) {
532 // Check if the instruction is alive.
533 if (isLive(&I))
534 continue;
535
536 if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
537 // Check if the scope of this variable location is alive.
538 if (AliveScopes.count(DII->getDebugLoc()->getScope()))
539 continue;
540
541 // Fallthrough and drop the intrinsic.
Chris Lattneracfd27d2001-09-09 22:26:47 +0000542 }
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000543
544 // Prepare to delete.
545 Worklist.push_back(&I);
546 I.dropAllReferences();
Hal Finkel92fb2d32015-02-15 15:51:23 +0000547 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000548
Hal Finkel92fb2d32015-02-15 15:51:23 +0000549 for (Instruction *&I : Worklist) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000550 ++NumRemoved;
Hal Finkel92fb2d32015-02-15 15:51:23 +0000551 I->eraseFromParent();
Chris Lattneracfd27d2001-09-09 22:26:47 +0000552 }
Devang Patel53b39b52008-11-11 00:54:10 +0000553
Hal Finkel75901292015-02-15 15:45:28 +0000554 return !Worklist.empty();
Chris Lattnerb28986f2001-06-30 06:39:11 +0000555}
Owen Anderson7686b552008-05-29 08:45:13 +0000556
David Callahanebcf9162016-12-13 16:42:18 +0000557// A dead region is the set of dead blocks with a common live post-dominator.
558void AggressiveDeadCodeElimination::updateDeadRegions() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000559 LLVM_DEBUG({
David Callahanebcf9162016-12-13 16:42:18 +0000560 dbgs() << "final dead terminator blocks: " << '\n';
561 for (auto *BB : BlocksWithDeadTerminators)
562 dbgs() << '\t' << BB->getName()
563 << (BlockInfo[BB].Live ? " LIVE\n" : "\n");
564 });
565
566 // Don't compute the post ordering unless we needed it.
567 bool HavePostOrder = false;
568
569 for (auto *BB : BlocksWithDeadTerminators) {
570 auto &Info = BlockInfo[BB];
571 if (Info.UnconditionalBranch) {
572 InstInfo[Info.Terminator].Live = true;
573 continue;
574 }
575
576 if (!HavePostOrder) {
577 computeReversePostOrder();
578 HavePostOrder = true;
579 }
580
581 // Add an unconditional branch to the successor closest to the
582 // end of the function which insures a path to the exit for each
583 // live edge.
584 BlockInfoType *PreferredSucc = nullptr;
585 for (auto *Succ : successors(BB)) {
586 auto *Info = &BlockInfo[Succ];
587 if (!PreferredSucc || PreferredSucc->PostOrder < Info->PostOrder)
588 PreferredSucc = Info;
589 }
590 assert((PreferredSucc && PreferredSucc->PostOrder > 0) &&
Tobias Grosser335b6bf2017-03-14 10:18:11 +0000591 "Failed to find safe successor for dead branch");
Jakub Kuderski2724d452017-08-22 16:30:21 +0000592
593 // Collect removed successors to update the (Post)DominatorTrees.
594 SmallPtrSet<BasicBlock *, 4> RemovedSuccessors;
David Callahanebcf9162016-12-13 16:42:18 +0000595 bool First = true;
596 for (auto *Succ : successors(BB)) {
Jakub Kuderski2724d452017-08-22 16:30:21 +0000597 if (!First || Succ != PreferredSucc->BB) {
David Callahanebcf9162016-12-13 16:42:18 +0000598 Succ->removePredecessor(BB);
Jakub Kuderski2724d452017-08-22 16:30:21 +0000599 RemovedSuccessors.insert(Succ);
600 } else
David Callahanebcf9162016-12-13 16:42:18 +0000601 First = false;
602 }
603 makeUnconditional(BB, PreferredSucc->BB);
Jakub Kuderski2724d452017-08-22 16:30:21 +0000604
605 // Inform the dominators about the deleted CFG edges.
606 SmallVector<DominatorTree::UpdateType, 4> DeletedEdges;
607 for (auto *Succ : RemovedSuccessors) {
608 // It might have happened that the same successor appeared multiple times
609 // and the CFG edge wasn't really removed.
610 if (Succ != PreferredSucc->BB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000611 LLVM_DEBUG(dbgs() << "ADCE: (Post)DomTree edge enqueued for deletion"
612 << BB->getName() << " -> " << Succ->getName()
613 << "\n");
Jakub Kuderski2724d452017-08-22 16:30:21 +0000614 DeletedEdges.push_back({DominatorTree::Delete, BB, Succ});
615 }
616 }
617
Chijun Simaeacad792018-08-04 02:50:12 +0000618 DomTreeUpdater(DT, &PDT, DomTreeUpdater::UpdateStrategy::Eager)
Chijun Sima21a8b602018-08-03 05:08:17 +0000619 .applyUpdates(DeletedEdges);
Jakub Kuderski2724d452017-08-22 16:30:21 +0000620
David Callahanebcf9162016-12-13 16:42:18 +0000621 NumBranchesRemoved += 1;
622 }
623}
624
625// reverse top-sort order
626void AggressiveDeadCodeElimination::computeReversePostOrder() {
Tobias Grosser335b6bf2017-03-14 10:18:11 +0000627 // This provides a post-order numbering of the reverse control flow graph
David Callahanebcf9162016-12-13 16:42:18 +0000628 // Note that it is incomplete in the presence of infinite loops but we don't
629 // need numbers blocks which don't reach the end of the functions since
630 // all branches in those blocks are forced live.
Taewook Ohd3f1ec92017-01-26 04:32:40 +0000631
Tobias Grosser335b6bf2017-03-14 10:18:11 +0000632 // For each block without successors, extend the DFS from the block
David Callahanebcf9162016-12-13 16:42:18 +0000633 // backward through the graph
634 SmallPtrSet<BasicBlock*, 16> Visited;
635 unsigned PostOrder = 0;
636 for (auto &BB : F) {
637 if (succ_begin(&BB) != succ_end(&BB))
638 continue;
639 for (BasicBlock *Block : inverse_post_order_ext(&BB,Visited))
640 BlockInfo[Block].PostOrder = PostOrder++;
641 }
642}
643
644void AggressiveDeadCodeElimination::makeUnconditional(BasicBlock *BB,
645 BasicBlock *Target) {
646 TerminatorInst *PredTerm = BB->getTerminator();
647 // Collect the live debug info scopes attached to this instruction.
648 if (const DILocation *DL = PredTerm->getDebugLoc())
649 collectLiveScopes(*DL);
650
651 // Just mark live an existing unconditional branch
652 if (isUnconditionalBranch(PredTerm)) {
653 PredTerm->setSuccessor(0, Target);
654 InstInfo[PredTerm].Live = true;
655 return;
656 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000657 LLVM_DEBUG(dbgs() << "making unconditional " << BB->getName() << '\n');
David Callahanebcf9162016-12-13 16:42:18 +0000658 NumBranchesRemoved += 1;
659 IRBuilder<> Builder(PredTerm);
660 auto *NewTerm = Builder.CreateBr(Target);
661 InstInfo[NewTerm].Live = true;
662 if (const DILocation *DL = PredTerm->getDebugLoc())
663 NewTerm->setDebugLoc(DL);
Jakub Kuderski2724d452017-08-22 16:30:21 +0000664
665 InstInfo.erase(PredTerm);
666 PredTerm->eraseFromParent();
David Callahanebcf9162016-12-13 16:42:18 +0000667}
668
David Callahan012d1c02016-08-24 00:10:06 +0000669//===----------------------------------------------------------------------===//
670//
671// Pass Manager integration code
672//
673//===----------------------------------------------------------------------===//
674PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {
Chijun Simaeacad792018-08-04 02:50:12 +0000675 // ADCE does not need DominatorTree, but require DominatorTree here
676 // to update analysis if it is already available.
677 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
David Callahan012d1c02016-08-24 00:10:06 +0000678 auto &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F);
Jakub Kuderski2724d452017-08-22 16:30:21 +0000679 if (!AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination())
Davide Italiano688616f2016-05-31 17:39:39 +0000680 return PreservedAnalyses::all();
681
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000682 PreservedAnalyses PA;
683 PA.preserveSet<CFGAnalyses>();
Davide Italiano688616f2016-05-31 17:39:39 +0000684 PA.preserve<GlobalsAA>();
Jakub Kuderski2724d452017-08-22 16:30:21 +0000685 PA.preserve<DominatorTreeAnalysis>();
686 PA.preserve<PostDominatorTreeAnalysis>();
Davide Italiano688616f2016-05-31 17:39:39 +0000687 return PA;
Duncan Sands9e064a22008-05-29 14:38:23 +0000688}
Justin Bogner19b67992015-10-30 23:13:18 +0000689
690namespace {
Eugene Zelenko3b879392017-10-13 21:17:07 +0000691
Justin Bogner19b67992015-10-30 23:13:18 +0000692struct ADCELegacyPass : public FunctionPass {
693 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko3b879392017-10-13 21:17:07 +0000694
Justin Bogner19b67992015-10-30 23:13:18 +0000695 ADCELegacyPass() : FunctionPass(ID) {
696 initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
697 }
698
David Callahancc5cd4d2016-08-03 04:28:39 +0000699 bool runOnFunction(Function &F) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000700 if (skipFunction(F))
Justin Bogner19b67992015-10-30 23:13:18 +0000701 return false;
Jakub Kuderski2724d452017-08-22 16:30:21 +0000702
Chijun Simaeacad792018-08-04 02:50:12 +0000703 // ADCE does not need DominatorTree, but require DominatorTree here
704 // to update analysis if it is already available.
705 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
706 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
David Callahan012d1c02016-08-24 00:10:06 +0000707 auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
Jakub Kuderski2724d452017-08-22 16:30:21 +0000708 return AggressiveDeadCodeElimination(F, DT, PDT)
709 .performDeadCodeElimination();
Justin Bogner19b67992015-10-30 23:13:18 +0000710 }
711
David Callahancc5cd4d2016-08-03 04:28:39 +0000712 void getAnalysisUsage(AnalysisUsage &AU) const override {
David Callahan012d1c02016-08-24 00:10:06 +0000713 AU.addRequired<PostDominatorTreeWrapperPass>();
David Callahanebcf9162016-12-13 16:42:18 +0000714 if (!RemoveControlFlowFlag)
715 AU.setPreservesCFG();
Jakub Kuderski2724d452017-08-22 16:30:21 +0000716 else {
717 AU.addPreserved<DominatorTreeWrapperPass>();
718 AU.addPreserved<PostDominatorTreeWrapperPass>();
719 }
Justin Bogner19b67992015-10-30 23:13:18 +0000720 AU.addPreserved<GlobalsAAWrapperPass>();
721 }
722};
Eugene Zelenko3b879392017-10-13 21:17:07 +0000723
724} // end anonymous namespace
Justin Bogner19b67992015-10-30 23:13:18 +0000725
726char ADCELegacyPass::ID = 0;
Eugene Zelenko3b879392017-10-13 21:17:07 +0000727
David Callahan012d1c02016-08-24 00:10:06 +0000728INITIALIZE_PASS_BEGIN(ADCELegacyPass, "adce",
729 "Aggressive Dead Code Elimination", false, false)
730INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
731INITIALIZE_PASS_END(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
732 false, false)
Justin Bogner19b67992015-10-30 23:13:18 +0000733
734FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }