ADCE.cpp 25 KB

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