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