Justin Bogner | 0638b7ba | 2015-09-25 21:03:46 +0000 | [diff] [blame] | 1 | //===- ADCE.cpp - Code to perform dead code elimination -------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 9 | // |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 10 | // This file implements the Aggressive Dead Code Elimination pass. This pass |
| 11 | // optimistically assumes that all instructions are dead until proven otherwise, |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 12 | // allowing it to eliminate dead computations that other DCE passes do not |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 13 | // catch, particularly involving loop computations. |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar/ADCE.h" |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DepthFirstIterator.h" |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/PostOrderIterator.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/GlobalsModRef.h" |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/IteratedDominanceFrontier.h" |
| 26 | #include "llvm/Analysis/PostDominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 28 | #include "llvm/IR/CFG.h" |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DebugInfoMetadata.h" |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 31 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Instructions.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 34 | #include "llvm/Pass.h" |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 35 | #include "llvm/ProfileData/InstrProf.h" |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | fc7bdac | 2003-12-19 09:08:34 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "adce" |
| 40 | |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 41 | STATISTIC(NumRemoved, "Number of instructions removed"); |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 42 | STATISTIC(NumBranchesRemoved, "Number of branch instructions removed"); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 43 | |
Tobias Grosser | 335b6bf | 2017-03-14 10:18:11 +0000 | [diff] [blame] | 44 | // This is a temporary option until we change the interface to this pass based |
| 45 | // on optimization level. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 46 | static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow", |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 47 | cl::init(true), cl::Hidden); |
| 48 | |
| 49 | // This option enables removing of may-be-infinite loops which have no other |
| 50 | // effect. |
| 51 | static cl::opt<bool> RemoveLoops("adce-remove-loops", cl::init(false), |
| 52 | cl::Hidden); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 53 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 54 | namespace { |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 55 | /// Information about Instructions |
| 56 | struct InstInfoType { |
| 57 | /// True if the associated instruction is live. |
| 58 | bool Live = false; |
| 59 | /// Quick access to information for block containing associated Instruction. |
| 60 | struct BlockInfoType *Block = nullptr; |
| 61 | }; |
| 62 | |
| 63 | /// Information about basic blocks relevant to dead code elimination. |
| 64 | struct BlockInfoType { |
| 65 | /// True when this block contains a live instructions. |
| 66 | bool Live = false; |
| 67 | /// True when this block ends in an unconditional branch. |
| 68 | bool UnconditionalBranch = false; |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 69 | /// True when this block is known to have live PHI nodes. |
| 70 | bool HasLivePhiNodes = false; |
| 71 | /// Control dependence sources need to be live for this block. |
| 72 | bool CFLive = false; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 73 | |
| 74 | /// Quick access to the LiveInfo for the terminator, |
| 75 | /// holds the value &InstInfo[Terminator] |
| 76 | InstInfoType *TerminatorLiveInfo = nullptr; |
| 77 | |
| 78 | bool terminatorIsLive() const { return TerminatorLiveInfo->Live; } |
| 79 | |
| 80 | /// Corresponding BasicBlock. |
| 81 | BasicBlock *BB = nullptr; |
| 82 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 83 | /// Cache of BB->getTerminator(). |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 84 | TerminatorInst *Terminator = nullptr; |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 85 | |
| 86 | /// Post-order numbering of reverse control flow graph. |
| 87 | unsigned PostOrder; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 90 | class AggressiveDeadCodeElimination { |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 91 | Function &F; |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 92 | PostDominatorTree &PDT; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 93 | |
| 94 | /// Mapping of blocks to associated information, an element in BlockInfoVec. |
| 95 | DenseMap<BasicBlock *, BlockInfoType> BlockInfo; |
| 96 | bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; } |
| 97 | |
| 98 | /// Mapping of instructions to associated information. |
| 99 | DenseMap<Instruction *, InstInfoType> InstInfo; |
| 100 | bool isLive(Instruction *I) { return InstInfo[I].Live; } |
| 101 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 102 | /// Instructions known to be live where we need to mark |
| 103 | /// reaching definitions as live. |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 104 | SmallVector<Instruction *, 128> Worklist; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 105 | /// Debug info scopes around a live instruction. |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 106 | SmallPtrSet<const Metadata *, 32> AliveScopes; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 107 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 108 | /// Set of blocks with not known to have live terminators. |
| 109 | SmallPtrSet<BasicBlock *, 16> BlocksWithDeadTerminators; |
| 110 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 111 | /// The set of blocks which we have determined whose control |
| 112 | /// dependence sources must be live and which have not had |
Tobias Grosser | 335b6bf | 2017-03-14 10:18:11 +0000 | [diff] [blame] | 113 | /// those dependences analyzed. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 114 | SmallPtrSet<BasicBlock *, 16> NewLiveBlocks; |
| 115 | |
| 116 | /// Set up auxiliary data structures for Instructions and BasicBlocks and |
| 117 | /// initialize the Worklist to the set of must-be-live Instruscions. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 118 | void initialize(); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 119 | /// Return true for operations which are always treated as live. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 120 | bool isAlwaysLive(Instruction &I); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 121 | /// Return true for instrumentation instructions for value profiling. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 122 | bool isInstrumentsConstant(Instruction &I); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 123 | |
| 124 | /// Propagate liveness to reaching definitions. |
| 125 | void markLiveInstructions(); |
| 126 | /// Mark an instruction as live. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 127 | void markLive(Instruction *I); |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 128 | /// Mark a block as live. |
| 129 | void markLive(BlockInfoType &BB); |
| 130 | void markLive(BasicBlock *BB) { markLive(BlockInfo[BB]); } |
| 131 | |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 132 | /// Mark terminators of control predecessors of a PHI node live. |
| 133 | void markPhiLive(PHINode *PN); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 134 | |
| 135 | /// Record the Debug Scopes which surround live debug information. |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 136 | void collectLiveScopes(const DILocalScope &LS); |
| 137 | void collectLiveScopes(const DILocation &DL); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 138 | |
| 139 | /// Analyze dead branches to find those whose branches are the sources |
| 140 | /// of control dependences impacting a live block. Those branches are |
| 141 | /// marked live. |
| 142 | void markLiveBranchesFromControlDependences(); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 143 | |
| 144 | /// Remove instructions not marked live, return if any any instruction |
| 145 | /// was removed. |
| 146 | bool removeDeadInstructions(); |
| 147 | |
Tobias Grosser | 335b6bf | 2017-03-14 10:18:11 +0000 | [diff] [blame] | 148 | /// Identify connected sections of the control flow graph which have |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 149 | /// dead terminators and rewrite the control flow graph to remove them. |
| 150 | void updateDeadRegions(); |
| 151 | |
| 152 | /// Set the BlockInfo::PostOrder field based on a post-order |
| 153 | /// numbering of the reverse control flow graph. |
| 154 | void computeReversePostOrder(); |
| 155 | |
| 156 | /// Make the terminator of this block an unconditional branch to \p Target. |
| 157 | void makeUnconditional(BasicBlock *BB, BasicBlock *Target); |
| 158 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 159 | public: |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 160 | AggressiveDeadCodeElimination(Function &F, PostDominatorTree &PDT) |
| 161 | : F(F), PDT(PDT) {} |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 162 | bool performDeadCodeElimination(); |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 163 | }; |
| 164 | } |
| 165 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 166 | bool AggressiveDeadCodeElimination::performDeadCodeElimination() { |
| 167 | initialize(); |
| 168 | markLiveInstructions(); |
| 169 | return removeDeadInstructions(); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 170 | } |
| 171 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 172 | static bool isUnconditionalBranch(TerminatorInst *Term) { |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 173 | auto *BR = dyn_cast<BranchInst>(Term); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 174 | return BR && BR->isUnconditional(); |
| 175 | } |
| 176 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 177 | void AggressiveDeadCodeElimination::initialize() { |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 178 | |
| 179 | auto NumBlocks = F.size(); |
| 180 | |
| 181 | // We will have an entry in the map for each block so we grow the |
| 182 | // structure to twice that size to keep the load factor low in the hash table. |
| 183 | BlockInfo.reserve(NumBlocks); |
| 184 | size_t NumInsts = 0; |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 185 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 186 | // Iterate over blocks and initialize BlockInfoVec entries, count |
| 187 | // instructions to size the InstInfo hash table. |
| 188 | for (auto &BB : F) { |
| 189 | NumInsts += BB.size(); |
| 190 | auto &Info = BlockInfo[&BB]; |
| 191 | Info.BB = &BB; |
| 192 | Info.Terminator = BB.getTerminator(); |
| 193 | Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator); |
| 194 | } |
| 195 | |
| 196 | // Initialize instruction map and set pointers to block info. |
| 197 | InstInfo.reserve(NumInsts); |
| 198 | for (auto &BBInfo : BlockInfo) |
| 199 | for (Instruction &I : *BBInfo.second.BB) |
| 200 | InstInfo[&I].Block = &BBInfo.second; |
| 201 | |
| 202 | // Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not |
| 203 | // add any more elements to either after this point. |
| 204 | for (auto &BBInfo : BlockInfo) |
| 205 | BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator]; |
| 206 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 207 | // Collect the set of "root" instructions that are known live. |
| 208 | for (Instruction &I : instructions(F)) |
| 209 | if (isAlwaysLive(I)) |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 210 | markLive(&I); |
| 211 | |
| 212 | if (!RemoveControlFlowFlag) |
| 213 | return; |
| 214 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 215 | if (!RemoveLoops) { |
| 216 | // This stores state for the depth-first iterator. In addition |
| 217 | // to recording which nodes have been visited we also record whether |
| 218 | // a node is currently on the "stack" of active ancestors of the current |
| 219 | // node. |
| 220 | typedef DenseMap<BasicBlock *, bool> StatusMap ; |
| 221 | class DFState : public StatusMap { |
| 222 | public: |
| 223 | std::pair<StatusMap::iterator, bool> insert(BasicBlock *BB) { |
| 224 | return StatusMap::insert(std::make_pair(BB, true)); |
| 225 | } |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 226 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 227 | // Invoked after we have visited all children of a node. |
| 228 | void completed(BasicBlock *BB) { (*this)[BB] = false; } |
| 229 | |
| 230 | // Return true if \p BB is currently on the active stack |
| 231 | // of ancestors. |
| 232 | bool onStack(BasicBlock *BB) { |
| 233 | auto Iter = find(BB); |
| 234 | return Iter != end() && Iter->second; |
| 235 | } |
| 236 | } State; |
Taewook Oh | d3f1ec9 | 2017-01-26 04:32:40 +0000 | [diff] [blame] | 237 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 238 | State.reserve(F.size()); |
| 239 | // Iterate over blocks in depth-first pre-order and |
| 240 | // treat all edges to a block already seen as loop back edges |
| 241 | // and mark the branch live it if there is a back edge. |
| 242 | for (auto *BB: depth_first_ext(&F.getEntryBlock(), State)) { |
| 243 | TerminatorInst *Term = BB->getTerminator(); |
| 244 | if (isLive(Term)) |
| 245 | continue; |
| 246 | |
| 247 | for (auto *Succ : successors(BB)) |
| 248 | if (State.onStack(Succ)) { |
| 249 | // back edge.... |
| 250 | markLive(Term); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Jakub Kuderski | 638c085 | 2017-08-15 18:14:57 +0000 | [diff] [blame^] | 256 | // Mark blocks live if there is no path from the block to a |
| 257 | // return of the function. |
| 258 | // We do this by seeing which of the postdomtree root children exit the |
| 259 | // program, and for all others, mark the subtree live. |
| 260 | for (auto &PDTChild : children<DomTreeNode *>(PDT.getRootNode())) { |
| 261 | auto *BB = PDTChild->getBlock(); |
| 262 | auto &Info = BlockInfo[BB]; |
| 263 | // Real function return |
| 264 | if (isa<ReturnInst>(Info.Terminator)) { |
| 265 | DEBUG(dbgs() << "post-dom root child is a return: " << BB->getName() |
Tobias Grosser | f818c33 | 2017-03-02 21:08:37 +0000 | [diff] [blame] | 266 | << '\n';); |
Tobias Grosser | f818c33 | 2017-03-02 21:08:37 +0000 | [diff] [blame] | 267 | continue; |
| 268 | } |
Jakub Kuderski | 638c085 | 2017-08-15 18:14:57 +0000 | [diff] [blame^] | 269 | |
| 270 | // This child is something else, like an infinite loop. |
| 271 | for (auto DFNode : depth_first(PDTChild)) |
| 272 | markLive(BlockInfo[DFNode->getBlock()].Terminator); |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 273 | } |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 274 | |
| 275 | // Treat the entry block as always live |
| 276 | auto *BB = &F.getEntryBlock(); |
| 277 | auto &EntryInfo = BlockInfo[BB]; |
| 278 | EntryInfo.Live = true; |
| 279 | if (EntryInfo.UnconditionalBranch) |
| 280 | markLive(EntryInfo.Terminator); |
| 281 | |
| 282 | // Build initial collection of blocks with dead terminators |
| 283 | for (auto &BBInfo : BlockInfo) |
| 284 | if (!BBInfo.second.terminatorIsLive()) |
| 285 | BlocksWithDeadTerminators.insert(BBInfo.second.BB); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 286 | } |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 287 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 288 | bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) { |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 289 | // TODO -- use llvm::isInstructionTriviallyDead |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 290 | if (I.isEHPad() || I.mayHaveSideEffects()) { |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 291 | // Skip any value profile instrumentation calls if they are |
| 292 | // instrumenting constants. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 293 | if (isInstrumentsConstant(I)) |
| 294 | return false; |
| 295 | return true; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 296 | } |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 297 | if (!isa<TerminatorInst>(I)) |
| 298 | return false; |
| 299 | if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I))) |
| 300 | return false; |
| 301 | return true; |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 304 | // Check if this instruction is a runtime call for value profiling and |
| 305 | // if it's instrumenting a constant. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 306 | bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) { |
| 307 | // TODO -- move this test into llvm::isInstructionTriviallyDead |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 308 | if (CallInst *CI = dyn_cast<CallInst>(&I)) |
| 309 | if (Function *Callee = CI->getCalledFunction()) |
| 310 | if (Callee->getName().equals(getInstrProfValueProfFuncName())) |
| 311 | if (isa<Constant>(CI->getArgOperand(0))) |
| 312 | return true; |
| 313 | return false; |
| 314 | } |
| 315 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 316 | void AggressiveDeadCodeElimination::markLiveInstructions() { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 317 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 318 | // Propagate liveness backwards to operands. |
| 319 | do { |
| 320 | // Worklist holds newly discovered live instructions |
| 321 | // where we need to mark the inputs as live. |
| 322 | while (!Worklist.empty()) { |
| 323 | Instruction *LiveInst = Worklist.pop_back_val(); |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 324 | DEBUG(dbgs() << "work live: "; LiveInst->dump();); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 325 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 326 | for (Use &OI : LiveInst->operands()) |
| 327 | if (Instruction *Inst = dyn_cast<Instruction>(OI)) |
| 328 | markLive(Inst); |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 329 | |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 330 | if (auto *PN = dyn_cast<PHINode>(LiveInst)) |
| 331 | markPhiLive(PN); |
Hal Finkel | 8626ed2 | 2015-02-15 15:51:25 +0000 | [diff] [blame] | 332 | } |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 333 | |
| 334 | // After data flow liveness has been identified, examine which branch |
| 335 | // decisions are required to determine live instructions are executed. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 336 | markLiveBranchesFromControlDependences(); |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 337 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 338 | } while (!Worklist.empty()); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void AggressiveDeadCodeElimination::markLive(Instruction *I) { |
| 342 | |
| 343 | auto &Info = InstInfo[I]; |
| 344 | if (Info.Live) |
| 345 | return; |
| 346 | |
| 347 | DEBUG(dbgs() << "mark live: "; I->dump()); |
| 348 | Info.Live = true; |
| 349 | Worklist.push_back(I); |
| 350 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 351 | // Collect the live debug info scopes attached to this instruction. |
| 352 | if (const DILocation *DL = I->getDebugLoc()) |
| 353 | collectLiveScopes(*DL); |
| 354 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 355 | // Mark the containing block live |
| 356 | auto &BBInfo = *Info.Block; |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 357 | if (BBInfo.Terminator == I) { |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 358 | BlocksWithDeadTerminators.erase(BBInfo.BB); |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 359 | // For live terminators, mark destination blocks |
| 360 | // live to preserve this control flow edges. |
| 361 | if (!BBInfo.UnconditionalBranch) |
| 362 | for (auto *BB : successors(I->getParent())) |
| 363 | markLive(BB); |
| 364 | } |
| 365 | markLive(BBInfo); |
| 366 | } |
| 367 | |
| 368 | void AggressiveDeadCodeElimination::markLive(BlockInfoType &BBInfo) { |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 369 | if (BBInfo.Live) |
| 370 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 371 | DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n'); |
| 372 | BBInfo.Live = true; |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 373 | if (!BBInfo.CFLive) { |
| 374 | BBInfo.CFLive = true; |
| 375 | NewLiveBlocks.insert(BBInfo.BB); |
| 376 | } |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 377 | |
| 378 | // Mark unconditional branches at the end of live |
| 379 | // blocks as live since there is no work to do for them later |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 380 | if (BBInfo.UnconditionalBranch) |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 381 | markLive(BBInfo.Terminator); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) { |
| 385 | if (!AliveScopes.insert(&LS).second) |
| 386 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 387 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 388 | if (isa<DISubprogram>(LS)) |
| 389 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 390 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 391 | // Tail-recurse through the scope chain. |
| 392 | collectLiveScopes(cast<DILocalScope>(*LS.getScope())); |
| 393 | } |
| 394 | |
| 395 | void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) { |
| 396 | // Even though DILocations are not scopes, shove them into AliveScopes so we |
| 397 | // don't revisit them. |
| 398 | if (!AliveScopes.insert(&DL).second) |
| 399 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 400 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 401 | // Collect live scopes from the scope chain. |
| 402 | collectLiveScopes(*DL.getScope()); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 403 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 404 | // Tail-recurse through the inlined-at chain. |
| 405 | if (const DILocation *IA = DL.getInlinedAt()) |
| 406 | collectLiveScopes(*IA); |
| 407 | } |
| 408 | |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 409 | void AggressiveDeadCodeElimination::markPhiLive(PHINode *PN) { |
| 410 | auto &Info = BlockInfo[PN->getParent()]; |
| 411 | // Only need to check this once per block. |
| 412 | if (Info.HasLivePhiNodes) |
| 413 | return; |
| 414 | Info.HasLivePhiNodes = true; |
| 415 | |
| 416 | // If a predecessor block is not live, mark it as control-flow live |
| 417 | // which will trigger marking live branches upon which |
| 418 | // that block is control dependent. |
| 419 | for (auto *PredBB : predecessors(Info.BB)) { |
| 420 | auto &Info = BlockInfo[PredBB]; |
| 421 | if (!Info.CFLive) { |
| 422 | Info.CFLive = true; |
| 423 | NewLiveBlocks.insert(PredBB); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 428 | void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() { |
| 429 | |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 430 | if (BlocksWithDeadTerminators.empty()) |
| 431 | return; |
| 432 | |
| 433 | DEBUG({ |
| 434 | dbgs() << "new live blocks:\n"; |
| 435 | for (auto *BB : NewLiveBlocks) |
| 436 | dbgs() << "\t" << BB->getName() << '\n'; |
| 437 | dbgs() << "dead terminator blocks:\n"; |
| 438 | for (auto *BB : BlocksWithDeadTerminators) |
| 439 | dbgs() << "\t" << BB->getName() << '\n'; |
| 440 | }); |
| 441 | |
| 442 | // The dominance frontier of a live block X in the reverse |
| 443 | // control graph is the set of blocks upon which X is control |
| 444 | // dependent. The following sequence computes the set of blocks |
| 445 | // which currently have dead terminators that are control |
| 446 | // dependence sources of a block which is in NewLiveBlocks. |
| 447 | |
| 448 | SmallVector<BasicBlock *, 32> IDFBlocks; |
| 449 | ReverseIDFCalculator IDFs(PDT); |
| 450 | IDFs.setDefiningBlocks(NewLiveBlocks); |
| 451 | IDFs.setLiveInBlocks(BlocksWithDeadTerminators); |
| 452 | IDFs.calculate(IDFBlocks); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 453 | NewLiveBlocks.clear(); |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 454 | |
| 455 | // Dead terminators which control live blocks are now marked live. |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 456 | for (auto *BB : IDFBlocks) { |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 457 | DEBUG(dbgs() << "live control in: " << BB->getName() << '\n'); |
| 458 | markLive(BB->getTerminator()); |
| 459 | } |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 460 | } |
| 461 | |
David Callahan | c165a4e | 2016-09-19 23:17:58 +0000 | [diff] [blame] | 462 | //===----------------------------------------------------------------------===// |
| 463 | // |
| 464 | // Routines to update the CFG and SSA information before removing dead code. |
| 465 | // |
| 466 | //===----------------------------------------------------------------------===// |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 467 | bool AggressiveDeadCodeElimination::removeDeadInstructions() { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 468 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 469 | // Updates control and dataflow around dead blocks |
| 470 | updateDeadRegions(); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 471 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 472 | DEBUG({ |
| 473 | for (Instruction &I : instructions(F)) { |
| 474 | // Check if the instruction is alive. |
| 475 | if (isLive(&I)) |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 476 | continue; |
| 477 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 478 | if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) { |
| 479 | // Check if the scope of this variable location is alive. |
| 480 | if (AliveScopes.count(DII->getDebugLoc()->getScope())) |
| 481 | continue; |
| 482 | |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 483 | // If intrinsic is pointing at a live SSA value, there may be an |
| 484 | // earlier optimization bug: if we know the location of the variable, |
| 485 | // why isn't the scope of the location alive? |
| 486 | if (Value *V = DII->getVariableLocation()) |
| 487 | if (Instruction *II = dyn_cast<Instruction>(V)) |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame] | 488 | if (isLive(II)) |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 489 | dbgs() << "Dropping debug info for " << *DII << "\n"; |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | }); |
| 493 | |
| 494 | // The inverse of the live set is the dead set. These are those instructions |
| 495 | // that have no side effects and do not influence the control flow or return |
| 496 | // value of the function, and may therefore be deleted safely. |
| 497 | // NOTE: We reuse the Worklist vector here for memory efficiency. |
| 498 | for (Instruction &I : instructions(F)) { |
| 499 | // Check if the instruction is alive. |
| 500 | if (isLive(&I)) |
| 501 | continue; |
| 502 | |
| 503 | if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) { |
| 504 | // Check if the scope of this variable location is alive. |
| 505 | if (AliveScopes.count(DII->getDebugLoc()->getScope())) |
| 506 | continue; |
| 507 | |
| 508 | // Fallthrough and drop the intrinsic. |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 509 | } |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 510 | |
| 511 | // Prepare to delete. |
| 512 | Worklist.push_back(&I); |
| 513 | I.dropAllReferences(); |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 514 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 515 | |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 516 | for (Instruction *&I : Worklist) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 517 | ++NumRemoved; |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 518 | I->eraseFromParent(); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 519 | } |
Devang Patel | 53b39b5 | 2008-11-11 00:54:10 +0000 | [diff] [blame] | 520 | |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 521 | return !Worklist.empty(); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 522 | } |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 523 | |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 524 | // A dead region is the set of dead blocks with a common live post-dominator. |
| 525 | void AggressiveDeadCodeElimination::updateDeadRegions() { |
| 526 | |
| 527 | DEBUG({ |
| 528 | dbgs() << "final dead terminator blocks: " << '\n'; |
| 529 | for (auto *BB : BlocksWithDeadTerminators) |
| 530 | dbgs() << '\t' << BB->getName() |
| 531 | << (BlockInfo[BB].Live ? " LIVE\n" : "\n"); |
| 532 | }); |
| 533 | |
| 534 | // Don't compute the post ordering unless we needed it. |
| 535 | bool HavePostOrder = false; |
| 536 | |
| 537 | for (auto *BB : BlocksWithDeadTerminators) { |
| 538 | auto &Info = BlockInfo[BB]; |
| 539 | if (Info.UnconditionalBranch) { |
| 540 | InstInfo[Info.Terminator].Live = true; |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | if (!HavePostOrder) { |
| 545 | computeReversePostOrder(); |
| 546 | HavePostOrder = true; |
| 547 | } |
| 548 | |
| 549 | // Add an unconditional branch to the successor closest to the |
| 550 | // end of the function which insures a path to the exit for each |
| 551 | // live edge. |
| 552 | BlockInfoType *PreferredSucc = nullptr; |
| 553 | for (auto *Succ : successors(BB)) { |
| 554 | auto *Info = &BlockInfo[Succ]; |
| 555 | if (!PreferredSucc || PreferredSucc->PostOrder < Info->PostOrder) |
| 556 | PreferredSucc = Info; |
| 557 | } |
| 558 | assert((PreferredSucc && PreferredSucc->PostOrder > 0) && |
Tobias Grosser | 335b6bf | 2017-03-14 10:18:11 +0000 | [diff] [blame] | 559 | "Failed to find safe successor for dead branch"); |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 560 | bool First = true; |
| 561 | for (auto *Succ : successors(BB)) { |
| 562 | if (!First || Succ != PreferredSucc->BB) |
| 563 | Succ->removePredecessor(BB); |
| 564 | else |
| 565 | First = false; |
| 566 | } |
| 567 | makeUnconditional(BB, PreferredSucc->BB); |
| 568 | NumBranchesRemoved += 1; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // reverse top-sort order |
| 573 | void AggressiveDeadCodeElimination::computeReversePostOrder() { |
Taewook Oh | d3f1ec9 | 2017-01-26 04:32:40 +0000 | [diff] [blame] | 574 | |
Tobias Grosser | 335b6bf | 2017-03-14 10:18:11 +0000 | [diff] [blame] | 575 | // This provides a post-order numbering of the reverse control flow graph |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 576 | // Note that it is incomplete in the presence of infinite loops but we don't |
| 577 | // need numbers blocks which don't reach the end of the functions since |
| 578 | // all branches in those blocks are forced live. |
Taewook Oh | d3f1ec9 | 2017-01-26 04:32:40 +0000 | [diff] [blame] | 579 | |
Tobias Grosser | 335b6bf | 2017-03-14 10:18:11 +0000 | [diff] [blame] | 580 | // For each block without successors, extend the DFS from the block |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 581 | // backward through the graph |
| 582 | SmallPtrSet<BasicBlock*, 16> Visited; |
| 583 | unsigned PostOrder = 0; |
| 584 | for (auto &BB : F) { |
| 585 | if (succ_begin(&BB) != succ_end(&BB)) |
| 586 | continue; |
| 587 | for (BasicBlock *Block : inverse_post_order_ext(&BB,Visited)) |
| 588 | BlockInfo[Block].PostOrder = PostOrder++; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void AggressiveDeadCodeElimination::makeUnconditional(BasicBlock *BB, |
| 593 | BasicBlock *Target) { |
| 594 | TerminatorInst *PredTerm = BB->getTerminator(); |
| 595 | // Collect the live debug info scopes attached to this instruction. |
| 596 | if (const DILocation *DL = PredTerm->getDebugLoc()) |
| 597 | collectLiveScopes(*DL); |
| 598 | |
| 599 | // Just mark live an existing unconditional branch |
| 600 | if (isUnconditionalBranch(PredTerm)) { |
| 601 | PredTerm->setSuccessor(0, Target); |
| 602 | InstInfo[PredTerm].Live = true; |
| 603 | return; |
| 604 | } |
| 605 | DEBUG(dbgs() << "making unconditional " << BB->getName() << '\n'); |
| 606 | NumBranchesRemoved += 1; |
| 607 | IRBuilder<> Builder(PredTerm); |
| 608 | auto *NewTerm = Builder.CreateBr(Target); |
| 609 | InstInfo[NewTerm].Live = true; |
| 610 | if (const DILocation *DL = PredTerm->getDebugLoc()) |
| 611 | NewTerm->setDebugLoc(DL); |
| 612 | } |
| 613 | |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 614 | //===----------------------------------------------------------------------===// |
| 615 | // |
| 616 | // Pass Manager integration code |
| 617 | // |
| 618 | //===----------------------------------------------------------------------===// |
| 619 | PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) { |
| 620 | auto &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F); |
| 621 | if (!AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination()) |
Davide Italiano | 688616f | 2016-05-31 17:39:39 +0000 | [diff] [blame] | 622 | return PreservedAnalyses::all(); |
| 623 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 624 | PreservedAnalyses PA; |
| 625 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | 688616f | 2016-05-31 17:39:39 +0000 | [diff] [blame] | 626 | PA.preserve<GlobalsAA>(); |
| 627 | return PA; |
Duncan Sands | 9e064a2 | 2008-05-29 14:38:23 +0000 | [diff] [blame] | 628 | } |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 629 | |
| 630 | namespace { |
| 631 | struct ADCELegacyPass : public FunctionPass { |
| 632 | static char ID; // Pass identification, replacement for typeid |
| 633 | ADCELegacyPass() : FunctionPass(ID) { |
| 634 | initializeADCELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 635 | } |
| 636 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 637 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 638 | if (skipFunction(F)) |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 639 | return false; |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 640 | auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree(); |
| 641 | return AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination(); |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 642 | } |
| 643 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 644 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 645 | AU.addRequired<PostDominatorTreeWrapperPass>(); |
David Callahan | ebcf916 | 2016-12-13 16:42:18 +0000 | [diff] [blame] | 646 | if (!RemoveControlFlowFlag) |
| 647 | AU.setPreservesCFG(); |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 648 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 649 | } |
| 650 | }; |
| 651 | } |
| 652 | |
| 653 | char ADCELegacyPass::ID = 0; |
David Callahan | 012d1c0 | 2016-08-24 00:10:06 +0000 | [diff] [blame] | 654 | INITIALIZE_PASS_BEGIN(ADCELegacyPass, "adce", |
| 655 | "Aggressive Dead Code Elimination", false, false) |
| 656 | INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass) |
| 657 | INITIALIZE_PASS_END(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination", |
| 658 | false, false) |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 659 | |
| 660 | FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); } |