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