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" |
| 20 | #include "llvm/ADT/SmallPtrSet.h" |
| 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CFG.h" |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 27 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/IntrinsicInst.h" |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 31 | #include "llvm/ProfileData/InstrProf.h" |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | fc7bdac | 2003-12-19 09:08:34 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "adce" |
| 36 | |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 37 | STATISTIC(NumRemoved, "Number of instructions removed"); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 38 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 39 | // This is a tempoary option until we change the interface |
| 40 | // to this pass based on optimization level. |
| 41 | static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow", |
| 42 | cl::init(false), cl::Hidden); |
| 43 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 44 | namespace { |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 45 | /// Information about Instructions |
| 46 | struct InstInfoType { |
| 47 | /// True if the associated instruction is live. |
| 48 | bool Live = false; |
| 49 | /// Quick access to information for block containing associated Instruction. |
| 50 | struct BlockInfoType *Block = nullptr; |
| 51 | }; |
| 52 | |
| 53 | /// Information about basic blocks relevant to dead code elimination. |
| 54 | struct BlockInfoType { |
| 55 | /// True when this block contains a live instructions. |
| 56 | bool Live = false; |
| 57 | /// True when this block ends in an unconditional branch. |
| 58 | bool UnconditionalBranch = false; |
| 59 | |
| 60 | /// Quick access to the LiveInfo for the terminator, |
| 61 | /// holds the value &InstInfo[Terminator] |
| 62 | InstInfoType *TerminatorLiveInfo = nullptr; |
| 63 | |
| 64 | bool terminatorIsLive() const { return TerminatorLiveInfo->Live; } |
| 65 | |
| 66 | /// Corresponding BasicBlock. |
| 67 | BasicBlock *BB = nullptr; |
| 68 | |
| 69 | /// Cache of BB->getTerminator() |
| 70 | TerminatorInst *Terminator = nullptr; |
| 71 | }; |
| 72 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 73 | class AggressiveDeadCodeElimination { |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 74 | Function &F; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 75 | |
| 76 | /// Mapping of blocks to associated information, an element in BlockInfoVec. |
| 77 | DenseMap<BasicBlock *, BlockInfoType> BlockInfo; |
| 78 | bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; } |
| 79 | |
| 80 | /// Mapping of instructions to associated information. |
| 81 | DenseMap<Instruction *, InstInfoType> InstInfo; |
| 82 | bool isLive(Instruction *I) { return InstInfo[I].Live; } |
| 83 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 84 | /// Instructions known to be live where we need to mark |
| 85 | /// reaching definitions as live. |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 86 | SmallVector<Instruction *, 128> Worklist; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 87 | /// Debug info scopes around a live instruction. |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 88 | SmallPtrSet<const Metadata *, 32> AliveScopes; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 89 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 90 | /// Set of blocks with not known to have live terminators. |
| 91 | SmallPtrSet<BasicBlock *, 16> BlocksWithDeadTerminators; |
| 92 | |
| 93 | /// The set of blocks which we have determined are live in the |
| 94 | /// most recent iteration of propagating liveness. |
| 95 | SmallPtrSet<BasicBlock *, 16> NewLiveBlocks; |
| 96 | |
| 97 | /// Set up auxiliary data structures for Instructions and BasicBlocks and |
| 98 | /// initialize the Worklist to the set of must-be-live Instruscions. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 99 | void initialize(); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 100 | /// Return true for operations which are always treated as live. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 101 | bool isAlwaysLive(Instruction &I); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 102 | /// Return true for instrumentation instructions for value profiling. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 103 | bool isInstrumentsConstant(Instruction &I); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 104 | |
| 105 | /// Propagate liveness to reaching definitions. |
| 106 | void markLiveInstructions(); |
| 107 | /// Mark an instruction as live. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 108 | void markLive(Instruction *I); |
| 109 | |
| 110 | /// Record the Debug Scopes which surround live debug information. |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 111 | void collectLiveScopes(const DILocalScope &LS); |
| 112 | void collectLiveScopes(const DILocation &DL); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 113 | |
| 114 | /// Analyze dead branches to find those whose branches are the sources |
| 115 | /// of control dependences impacting a live block. Those branches are |
| 116 | /// marked live. |
| 117 | void markLiveBranchesFromControlDependences(); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 118 | |
| 119 | /// Remove instructions not marked live, return if any any instruction |
| 120 | /// was removed. |
| 121 | bool removeDeadInstructions(); |
| 122 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 123 | public: |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 124 | AggressiveDeadCodeElimination(Function &F) : F(F) {} |
| 125 | bool performDeadCodeElimination(); |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 126 | }; |
| 127 | } |
| 128 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 129 | bool AggressiveDeadCodeElimination::performDeadCodeElimination() { |
| 130 | initialize(); |
| 131 | markLiveInstructions(); |
| 132 | return removeDeadInstructions(); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 133 | } |
| 134 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 135 | static bool isUnconditionalBranch(TerminatorInst *Term) { |
| 136 | auto BR = dyn_cast<BranchInst>(Term); |
| 137 | return BR && BR->isUnconditional(); |
| 138 | } |
| 139 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 140 | void AggressiveDeadCodeElimination::initialize() { |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 141 | |
| 142 | auto NumBlocks = F.size(); |
| 143 | |
| 144 | // We will have an entry in the map for each block so we grow the |
| 145 | // structure to twice that size to keep the load factor low in the hash table. |
| 146 | BlockInfo.reserve(NumBlocks); |
| 147 | size_t NumInsts = 0; |
| 148 | |
| 149 | // Iterate over blocks and initialize BlockInfoVec entries, count |
| 150 | // instructions to size the InstInfo hash table. |
| 151 | for (auto &BB : F) { |
| 152 | NumInsts += BB.size(); |
| 153 | auto &Info = BlockInfo[&BB]; |
| 154 | Info.BB = &BB; |
| 155 | Info.Terminator = BB.getTerminator(); |
| 156 | Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator); |
| 157 | } |
| 158 | |
| 159 | // Initialize instruction map and set pointers to block info. |
| 160 | InstInfo.reserve(NumInsts); |
| 161 | for (auto &BBInfo : BlockInfo) |
| 162 | for (Instruction &I : *BBInfo.second.BB) |
| 163 | InstInfo[&I].Block = &BBInfo.second; |
| 164 | |
| 165 | // Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not |
| 166 | // add any more elements to either after this point. |
| 167 | for (auto &BBInfo : BlockInfo) |
| 168 | BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator]; |
| 169 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 170 | // Collect the set of "root" instructions that are known live. |
| 171 | for (Instruction &I : instructions(F)) |
| 172 | if (isAlwaysLive(I)) |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 173 | markLive(&I); |
| 174 | |
| 175 | if (!RemoveControlFlowFlag) |
| 176 | return; |
| 177 | |
| 178 | // This is temporary: will update with post order traveral to |
| 179 | // find loop bottoms |
| 180 | SmallPtrSet<BasicBlock *, 16> Seen; |
| 181 | for (auto &BB : F) { |
| 182 | Seen.insert(&BB); |
| 183 | TerminatorInst *Term = BB.getTerminator(); |
| 184 | if (isLive(Term)) |
| 185 | continue; |
| 186 | |
| 187 | for (auto Succ : successors(&BB)) |
| 188 | if (Seen.count(Succ)) { |
| 189 | // back edge.... |
| 190 | markLive(Term); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | // end temporary handling of loops |
| 195 | |
| 196 | // Treat the entry block as always live |
| 197 | auto *BB = &F.getEntryBlock(); |
| 198 | auto &EntryInfo = BlockInfo[BB]; |
| 199 | EntryInfo.Live = true; |
| 200 | if (EntryInfo.UnconditionalBranch) |
| 201 | markLive(EntryInfo.Terminator); |
| 202 | |
| 203 | // Build initial collection of blocks with dead terminators |
| 204 | for (auto &BBInfo : BlockInfo) |
| 205 | if (!BBInfo.second.terminatorIsLive()) |
| 206 | BlocksWithDeadTerminators.insert(BBInfo.second.BB); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 207 | } |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 208 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 209 | bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) { |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 210 | // TODO -- use llvm::isInstructionTriviallyDead |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 211 | if (I.isEHPad() || I.mayHaveSideEffects()) { |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 212 | // Skip any value profile instrumentation calls if they are |
| 213 | // instrumenting constants. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 214 | if (isInstrumentsConstant(I)) |
| 215 | return false; |
| 216 | return true; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 217 | } |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 218 | if (!isa<TerminatorInst>(I)) |
| 219 | return false; |
| 220 | if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I))) |
| 221 | return false; |
| 222 | return true; |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 225 | // Check if this instruction is a runtime call for value profiling and |
| 226 | // if it's instrumenting a constant. |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 227 | bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) { |
| 228 | // TODO -- move this test into llvm::isInstructionTriviallyDead |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 229 | if (CallInst *CI = dyn_cast<CallInst>(&I)) |
| 230 | if (Function *Callee = CI->getCalledFunction()) |
| 231 | if (Callee->getName().equals(getInstrProfValueProfFuncName())) |
| 232 | if (isa<Constant>(CI->getArgOperand(0))) |
| 233 | return true; |
| 234 | return false; |
| 235 | } |
| 236 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 237 | void AggressiveDeadCodeElimination::markLiveInstructions() { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 238 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 239 | // Propagate liveness backwards to operands. |
| 240 | do { |
| 241 | // Worklist holds newly discovered live instructions |
| 242 | // where we need to mark the inputs as live. |
| 243 | while (!Worklist.empty()) { |
| 244 | Instruction *LiveInst = Worklist.pop_back_val(); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 245 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 246 | // Collect the live debug info scopes attached to this instruction. |
| 247 | if (const DILocation *DL = LiveInst->getDebugLoc()) |
| 248 | collectLiveScopes(*DL); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 249 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 250 | DEBUG(dbgs() << "work live: "; LiveInst->dump();); |
| 251 | for (Use &OI : LiveInst->operands()) |
| 252 | if (Instruction *Inst = dyn_cast<Instruction>(OI)) |
| 253 | markLive(Inst); |
Hal Finkel | 8626ed2 | 2015-02-15 15:51:25 +0000 | [diff] [blame] | 254 | } |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 255 | markLiveBranchesFromControlDependences(); |
| 256 | // TODO -- handle PhiNodes |
| 257 | } while (!Worklist.empty()); |
| 258 | |
| 259 | // temporary until control dependences are implemented |
| 260 | assert(BlocksWithDeadTerminators.empty()); |
| 261 | } |
| 262 | |
| 263 | void AggressiveDeadCodeElimination::markLive(Instruction *I) { |
| 264 | |
| 265 | auto &Info = InstInfo[I]; |
| 266 | if (Info.Live) |
| 267 | return; |
| 268 | |
| 269 | DEBUG(dbgs() << "mark live: "; I->dump()); |
| 270 | Info.Live = true; |
| 271 | Worklist.push_back(I); |
| 272 | |
| 273 | // Mark the containing block live |
| 274 | auto &BBInfo = *Info.Block; |
| 275 | if (BBInfo.Terminator == I) |
| 276 | BlocksWithDeadTerminators.erase(BBInfo.BB); |
| 277 | if (BBInfo.Live) |
| 278 | return; |
| 279 | |
| 280 | DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n'); |
| 281 | BBInfo.Live = true; |
| 282 | NewLiveBlocks.insert(BBInfo.BB); |
| 283 | |
| 284 | // Mark unconditional branches at the end of live |
| 285 | // blocks as live since there is no work to do for them later |
| 286 | if (BBInfo.UnconditionalBranch && I != BBInfo.Terminator) |
| 287 | markLive(BBInfo.Terminator); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) { |
| 291 | if (!AliveScopes.insert(&LS).second) |
| 292 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 293 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 294 | if (isa<DISubprogram>(LS)) |
| 295 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 296 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 297 | // Tail-recurse through the scope chain. |
| 298 | collectLiveScopes(cast<DILocalScope>(*LS.getScope())); |
| 299 | } |
| 300 | |
| 301 | void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) { |
| 302 | // Even though DILocations are not scopes, shove them into AliveScopes so we |
| 303 | // don't revisit them. |
| 304 | if (!AliveScopes.insert(&DL).second) |
| 305 | return; |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 306 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 307 | // Collect live scopes from the scope chain. |
| 308 | collectLiveScopes(*DL.getScope()); |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 309 | |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 310 | // Tail-recurse through the inlined-at chain. |
| 311 | if (const DILocation *IA = DL.getInlinedAt()) |
| 312 | collectLiveScopes(*IA); |
| 313 | } |
| 314 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 315 | void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() { |
| 316 | |
| 317 | // This is a place holder, mark all read operations live |
| 318 | // The next patch will replace this with using iterated dominance |
| 319 | // frontier to compute branches that need to be live because they |
| 320 | // control live blocks with live operations |
| 321 | SmallVector<TerminatorInst *, 16> DeadTerminators; |
| 322 | for (auto *BB : BlocksWithDeadTerminators) |
| 323 | DeadTerminators.push_back(BB->getTerminator()); |
| 324 | for (auto I : DeadTerminators) |
| 325 | markLive(I); |
| 326 | NewLiveBlocks.clear(); |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | bool AggressiveDeadCodeElimination::removeDeadInstructions() { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 330 | |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 331 | // The inverse of the live set is the dead set. These are those instructions |
| 332 | // which have no side effects and do not influence the control flow or return |
| 333 | // value of the function, and may therefore be deleted safely. |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 334 | // NOTE: We reuse the Worklist vector here for memory efficiency. |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 335 | for (Instruction &I : instructions(F)) { |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 336 | // Check if the instruction is alive. |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 337 | if (isLive(&I)) |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 338 | continue; |
| 339 | |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 340 | assert(!I.isTerminator() && "NYI: Removing Control Flow"); |
| 341 | |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 342 | if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) { |
| 343 | // Check if the scope of this variable location is alive. |
| 344 | if (AliveScopes.count(DII->getDebugLoc()->getScope())) |
| 345 | continue; |
| 346 | |
| 347 | // Fallthrough and drop the intrinsic. |
| 348 | DEBUG({ |
| 349 | // If intrinsic is pointing at a live SSA value, there may be an |
| 350 | // earlier optimization bug: if we know the location of the variable, |
| 351 | // why isn't the scope of the location alive? |
| 352 | if (Value *V = DII->getVariableLocation()) |
| 353 | if (Instruction *II = dyn_cast<Instruction>(V)) |
David Callahan | 947be0f | 2016-08-16 14:31:51 +0000 | [diff] [blame^] | 354 | if (isLive(II)) |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 355 | dbgs() << "Dropping debug info for " << *DII << "\n"; |
| 356 | }); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 357 | } |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 358 | |
| 359 | // Prepare to delete. |
| 360 | Worklist.push_back(&I); |
| 361 | I.dropAllReferences(); |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 362 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 363 | |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 364 | for (Instruction *&I : Worklist) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 365 | ++NumRemoved; |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 366 | I->eraseFromParent(); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 367 | } |
Devang Patel | 53b39b5 | 2008-11-11 00:54:10 +0000 | [diff] [blame] | 368 | |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 369 | return !Worklist.empty(); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 370 | } |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 371 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 372 | PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &) { |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 373 | if (!AggressiveDeadCodeElimination(F).performDeadCodeElimination()) |
Davide Italiano | 688616f | 2016-05-31 17:39:39 +0000 | [diff] [blame] | 374 | return PreservedAnalyses::all(); |
| 375 | |
Michael Kuperstein | 835facd | 2016-06-28 00:54:12 +0000 | [diff] [blame] | 376 | // FIXME: This should also 'preserve the CFG'. |
Davide Italiano | 688616f | 2016-05-31 17:39:39 +0000 | [diff] [blame] | 377 | auto PA = PreservedAnalyses(); |
| 378 | PA.preserve<GlobalsAA>(); |
| 379 | return PA; |
Duncan Sands | 9e064a2 | 2008-05-29 14:38:23 +0000 | [diff] [blame] | 380 | } |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 381 | |
| 382 | namespace { |
| 383 | struct ADCELegacyPass : public FunctionPass { |
| 384 | static char ID; // Pass identification, replacement for typeid |
| 385 | ADCELegacyPass() : FunctionPass(ID) { |
| 386 | initializeADCELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 387 | } |
| 388 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 389 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 390 | if (skipFunction(F)) |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 391 | return false; |
David Callahan | 45e442e | 2016-08-05 19:38:11 +0000 | [diff] [blame] | 392 | return AggressiveDeadCodeElimination(F).performDeadCodeElimination(); |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 393 | } |
| 394 | |
David Callahan | cc5cd4d | 2016-08-03 04:28:39 +0000 | [diff] [blame] | 395 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 396 | AU.setPreservesCFG(); |
| 397 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 398 | } |
| 399 | }; |
| 400 | } |
| 401 | |
| 402 | char ADCELegacyPass::ID = 0; |
| 403 | INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination", |
| 404 | false, false) |
| 405 | |
| 406 | FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); } |