2015-09-26 05:03:46 +08:00
|
|
|
//===- ADCE.cpp - Code to perform dead code elimination -------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-30 14:39:11 +08:00
|
|
|
//
|
2008-05-29 16:45:13 +08:00
|
|
|
// This file implements the Aggressive Dead Code Elimination pass. This pass
|
|
|
|
// optimistically assumes that all instructions are dead until proven otherwise,
|
2012-07-24 18:51:42 +08:00
|
|
|
// allowing it to eliminate dead computations that other DCE passes do not
|
2008-05-29 16:45:13 +08:00
|
|
|
// catch, particularly involving loop computations.
|
2001-06-30 14:39:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-31 07:13:18 +08:00
|
|
|
#include "llvm/Transforms/Scalar/ADCE.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2017-11-03 22:15:08 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2016-12-14 00:42:18 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-09-10 18:22:12 +08:00
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2016-08-24 08:10:06 +08:00
|
|
|
#include "llvm/Analysis/IteratedDominanceFrontier.h"
|
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2016-03-30 06:57:12 +08:00
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2017-08-23 00:30:21 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2016-12-14 00:42:18 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/IR/PassManager.h"
|
|
|
|
#include "llvm/IR/Use.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2008-05-29 16:45:13 +08:00
|
|
|
#include "llvm/Pass.h"
|
2016-04-14 02:52:19 +08:00
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-10-31 07:13:18 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-10-14 05:17:07 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <utility>
|
|
|
|
|
2003-12-19 17:08:34 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "adce"
|
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
STATISTIC(NumRemoved, "Number of instructions removed");
|
2016-12-14 00:42:18 +08:00
|
|
|
STATISTIC(NumBranchesRemoved, "Number of branch instructions removed");
|
2002-05-07 01:27:57 +08:00
|
|
|
|
2017-03-14 18:18:11 +08:00
|
|
|
// This is a temporary option until we change the interface to this pass based
|
|
|
|
// on optimization level.
|
2016-08-16 22:31:51 +08:00
|
|
|
static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow",
|
2016-12-14 00:42:18 +08:00
|
|
|
cl::init(true), cl::Hidden);
|
|
|
|
|
|
|
|
// This option enables removing of may-be-infinite loops which have no other
|
|
|
|
// effect.
|
|
|
|
static cl::opt<bool> RemoveLoops("adce-remove-loops", cl::init(false),
|
|
|
|
cl::Hidden);
|
2016-08-16 22:31:51 +08:00
|
|
|
|
2016-08-03 12:28:39 +08:00
|
|
|
namespace {
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
/// Information about Instructions
|
|
|
|
struct InstInfoType {
|
|
|
|
/// True if the associated instruction is live.
|
|
|
|
bool Live = false;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
/// Quick access to information for block containing associated Instruction.
|
|
|
|
struct BlockInfoType *Block = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Information about basic blocks relevant to dead code elimination.
|
|
|
|
struct BlockInfoType {
|
|
|
|
/// True when this block contains a live instructions.
|
|
|
|
bool Live = false;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
/// True when this block ends in an unconditional branch.
|
|
|
|
bool UnconditionalBranch = false;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-09-20 07:17:58 +08:00
|
|
|
/// True when this block is known to have live PHI nodes.
|
|
|
|
bool HasLivePhiNodes = false;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-09-20 07:17:58 +08:00
|
|
|
/// Control dependence sources need to be live for this block.
|
|
|
|
bool CFLive = false;
|
2016-08-16 22:31:51 +08:00
|
|
|
|
|
|
|
/// Quick access to the LiveInfo for the terminator,
|
|
|
|
/// holds the value &InstInfo[Terminator]
|
|
|
|
InstInfoType *TerminatorLiveInfo = nullptr;
|
|
|
|
|
|
|
|
/// Corresponding BasicBlock.
|
|
|
|
BasicBlock *BB = nullptr;
|
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
/// Cache of BB->getTerminator().
|
2016-08-16 22:31:51 +08:00
|
|
|
TerminatorInst *Terminator = nullptr;
|
2016-12-14 00:42:18 +08:00
|
|
|
|
|
|
|
/// Post-order numbering of reverse control flow graph.
|
|
|
|
unsigned PostOrder;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
|
|
|
bool terminatorIsLive() const { return TerminatorLiveInfo->Live; }
|
2016-08-16 22:31:51 +08:00
|
|
|
};
|
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
class AggressiveDeadCodeElimination {
|
2016-08-03 12:28:39 +08:00
|
|
|
Function &F;
|
2017-08-23 00:30:21 +08:00
|
|
|
|
|
|
|
// ADCE does not use DominatorTree per se, but it updates it to preserve the
|
|
|
|
// analysis.
|
|
|
|
DominatorTree &DT;
|
2016-08-24 08:10:06 +08:00
|
|
|
PostDominatorTree &PDT;
|
2016-08-16 22:31:51 +08:00
|
|
|
|
|
|
|
/// Mapping of blocks to associated information, an element in BlockInfoVec.
|
2017-11-03 22:15:08 +08:00
|
|
|
/// Use MapVector to get deterministic iteration order.
|
|
|
|
MapVector<BasicBlock *, BlockInfoType> BlockInfo;
|
2016-08-16 22:31:51 +08:00
|
|
|
bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; }
|
|
|
|
|
|
|
|
/// Mapping of instructions to associated information.
|
|
|
|
DenseMap<Instruction *, InstInfoType> InstInfo;
|
|
|
|
bool isLive(Instruction *I) { return InstInfo[I].Live; }
|
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
/// Instructions known to be live where we need to mark
|
|
|
|
/// reaching definitions as live.
|
2016-08-03 12:28:39 +08:00
|
|
|
SmallVector<Instruction *, 128> Worklist;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
/// Debug info scopes around a live instruction.
|
2016-08-03 12:28:39 +08:00
|
|
|
SmallPtrSet<const Metadata *, 32> AliveScopes;
|
2016-08-06 03:38:11 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
/// Set of blocks with not known to have live terminators.
|
|
|
|
SmallPtrSet<BasicBlock *, 16> BlocksWithDeadTerminators;
|
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
/// The set of blocks which we have determined whose control
|
|
|
|
/// dependence sources must be live and which have not had
|
2017-03-14 18:18:11 +08:00
|
|
|
/// those dependences analyzed.
|
2016-08-16 22:31:51 +08:00
|
|
|
SmallPtrSet<BasicBlock *, 16> NewLiveBlocks;
|
|
|
|
|
|
|
|
/// Set up auxiliary data structures for Instructions and BasicBlocks and
|
|
|
|
/// initialize the Worklist to the set of must-be-live Instruscions.
|
2016-08-06 03:38:11 +08:00
|
|
|
void initialize();
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
/// Return true for operations which are always treated as live.
|
2016-08-06 03:38:11 +08:00
|
|
|
bool isAlwaysLive(Instruction &I);
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
/// Return true for instrumentation instructions for value profiling.
|
2016-08-06 03:38:11 +08:00
|
|
|
bool isInstrumentsConstant(Instruction &I);
|
|
|
|
|
|
|
|
/// Propagate liveness to reaching definitions.
|
|
|
|
void markLiveInstructions();
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
/// Mark an instruction as live.
|
2016-08-16 22:31:51 +08:00
|
|
|
void markLive(Instruction *I);
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
/// Mark a block as live.
|
|
|
|
void markLive(BlockInfoType &BB);
|
|
|
|
void markLive(BasicBlock *BB) { markLive(BlockInfo[BB]); }
|
|
|
|
|
2016-09-20 07:17:58 +08:00
|
|
|
/// Mark terminators of control predecessors of a PHI node live.
|
|
|
|
void markPhiLive(PHINode *PN);
|
2016-08-16 22:31:51 +08:00
|
|
|
|
|
|
|
/// Record the Debug Scopes which surround live debug information.
|
2016-08-03 12:28:39 +08:00
|
|
|
void collectLiveScopes(const DILocalScope &LS);
|
|
|
|
void collectLiveScopes(const DILocation &DL);
|
2016-08-16 22:31:51 +08:00
|
|
|
|
|
|
|
/// Analyze dead branches to find those whose branches are the sources
|
|
|
|
/// of control dependences impacting a live block. Those branches are
|
|
|
|
/// marked live.
|
|
|
|
void markLiveBranchesFromControlDependences();
|
2016-08-06 03:38:11 +08:00
|
|
|
|
2018-03-31 06:22:31 +08:00
|
|
|
/// Remove instructions not marked live, return if any instruction was
|
|
|
|
/// removed.
|
2016-08-06 03:38:11 +08:00
|
|
|
bool removeDeadInstructions();
|
|
|
|
|
2017-03-14 18:18:11 +08:00
|
|
|
/// Identify connected sections of the control flow graph which have
|
2016-12-14 00:42:18 +08:00
|
|
|
/// dead terminators and rewrite the control flow graph to remove them.
|
|
|
|
void updateDeadRegions();
|
|
|
|
|
|
|
|
/// Set the BlockInfo::PostOrder field based on a post-order
|
|
|
|
/// numbering of the reverse control flow graph.
|
|
|
|
void computeReversePostOrder();
|
|
|
|
|
|
|
|
/// Make the terminator of this block an unconditional branch to \p Target.
|
|
|
|
void makeUnconditional(BasicBlock *BB, BasicBlock *Target);
|
|
|
|
|
2016-08-03 12:28:39 +08:00
|
|
|
public:
|
2017-10-14 05:17:07 +08:00
|
|
|
AggressiveDeadCodeElimination(Function &F, DominatorTree &DT,
|
|
|
|
PostDominatorTree &PDT)
|
|
|
|
: F(F), DT(DT), PDT(PDT) {}
|
|
|
|
|
|
|
|
bool performDeadCodeElimination();
|
2016-08-03 12:28:39 +08:00
|
|
|
};
|
2017-10-14 05:17:07 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2016-08-03 12:28:39 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
bool AggressiveDeadCodeElimination::performDeadCodeElimination() {
|
|
|
|
initialize();
|
|
|
|
markLiveInstructions();
|
|
|
|
return removeDeadInstructions();
|
2016-03-30 06:57:12 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
static bool isUnconditionalBranch(TerminatorInst *Term) {
|
2016-12-14 00:42:18 +08:00
|
|
|
auto *BR = dyn_cast<BranchInst>(Term);
|
2016-08-16 22:31:51 +08:00
|
|
|
return BR && BR->isUnconditional();
|
|
|
|
}
|
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
void AggressiveDeadCodeElimination::initialize() {
|
2016-08-16 22:31:51 +08:00
|
|
|
auto NumBlocks = F.size();
|
|
|
|
|
|
|
|
// We will have an entry in the map for each block so we grow the
|
|
|
|
// structure to twice that size to keep the load factor low in the hash table.
|
|
|
|
BlockInfo.reserve(NumBlocks);
|
|
|
|
size_t NumInsts = 0;
|
2016-08-24 08:10:06 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
// Iterate over blocks and initialize BlockInfoVec entries, count
|
|
|
|
// instructions to size the InstInfo hash table.
|
|
|
|
for (auto &BB : F) {
|
|
|
|
NumInsts += BB.size();
|
|
|
|
auto &Info = BlockInfo[&BB];
|
|
|
|
Info.BB = &BB;
|
|
|
|
Info.Terminator = BB.getTerminator();
|
|
|
|
Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize instruction map and set pointers to block info.
|
|
|
|
InstInfo.reserve(NumInsts);
|
|
|
|
for (auto &BBInfo : BlockInfo)
|
|
|
|
for (Instruction &I : *BBInfo.second.BB)
|
|
|
|
InstInfo[&I].Block = &BBInfo.second;
|
|
|
|
|
|
|
|
// Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not
|
|
|
|
// add any more elements to either after this point.
|
|
|
|
for (auto &BBInfo : BlockInfo)
|
|
|
|
BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator];
|
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
// Collect the set of "root" instructions that are known live.
|
|
|
|
for (Instruction &I : instructions(F))
|
|
|
|
if (isAlwaysLive(I))
|
2016-08-16 22:31:51 +08:00
|
|
|
markLive(&I);
|
|
|
|
|
|
|
|
if (!RemoveControlFlowFlag)
|
|
|
|
return;
|
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
if (!RemoveLoops) {
|
|
|
|
// This stores state for the depth-first iterator. In addition
|
|
|
|
// to recording which nodes have been visited we also record whether
|
|
|
|
// a node is currently on the "stack" of active ancestors of the current
|
|
|
|
// node.
|
2017-10-14 05:17:07 +08:00
|
|
|
using StatusMap = DenseMap<BasicBlock *, bool>;
|
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
class DFState : public StatusMap {
|
|
|
|
public:
|
|
|
|
std::pair<StatusMap::iterator, bool> insert(BasicBlock *BB) {
|
|
|
|
return StatusMap::insert(std::make_pair(BB, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoked after we have visited all children of a node.
|
|
|
|
void completed(BasicBlock *BB) { (*this)[BB] = false; }
|
2016-08-16 22:31:51 +08:00
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
// Return true if \p BB is currently on the active stack
|
|
|
|
// of ancestors.
|
|
|
|
bool onStack(BasicBlock *BB) {
|
|
|
|
auto Iter = find(BB);
|
|
|
|
return Iter != end() && Iter->second;
|
|
|
|
}
|
|
|
|
} State;
|
2017-01-26 12:32:40 +08:00
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
State.reserve(F.size());
|
|
|
|
// Iterate over blocks in depth-first pre-order and
|
|
|
|
// treat all edges to a block already seen as loop back edges
|
|
|
|
// and mark the branch live it if there is a back edge.
|
|
|
|
for (auto *BB: depth_first_ext(&F.getEntryBlock(), State)) {
|
|
|
|
TerminatorInst *Term = BB->getTerminator();
|
|
|
|
if (isLive(Term))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto *Succ : successors(BB))
|
|
|
|
if (State.onStack(Succ)) {
|
|
|
|
// back edge....
|
|
|
|
markLive(Term);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[Dominators] Include infinite loops in PostDominatorTree
Summary:
This patch teaches PostDominatorTree about infinite loops. It is built on top of D29705 by @dberlin which includes a very detailed motivation for this change.
What's new is that the patch also teaches the incremental updater how to deal with reverse-unreachable regions and how to properly maintain and verify tree roots. Before that, the incremental algorithm sometimes ended up preserving reverse-unreachable regions after updates that wouldn't appear in the tree if it was constructed from scratch on the same CFG.
This patch makes the following assumptions:
- A sequence of updates should produce the same tree as a recalculating it.
- Any sequence of the same updates should lead to the same tree.
- Siblings and roots are unordered.
The last two properties are essential to efficiently perform batch updates in the future.
When it comes to the first one, we can decide later that the consistency between freshly built tree and an updated one doesn't matter match, as there are many correct ways to pick roots in infinite loops, and to relax this assumption. That should enable us to recalculate postdominators less frequently.
This patch is pretty conservative when it comes to incremental updates on reverse-unreachable regions and ends up recalculating the whole tree in many cases. It should be possible to improve the performance in many cases, if we decide that it's important enough.
That being said, my experiments showed that reverse-unreachable are very rare in the IR emitted by clang when bootstrapping clang. Here are the statistics I collected by analyzing IR between passes and after each removePredecessor call:
```
# functions: 52283
# samples: 337609
# reverse unreachable BBs: 216022
# BBs: 247840796
Percent reverse-unreachable: 0.08716159869015269 %
Max(PercRevUnreachable) in a function: 87.58620689655172 %
# > 25 % samples: 471 ( 0.1395104988314885 % samples )
... in 145 ( 0.27733680163724345 % functions )
```
Most of the reverse-unreachable regions come from invalid IR where it wouldn't be possible to construct a PostDomTree anyway.
I would like to commit this patch in the next week in order to be able to complete the work that depends on it before the end of my internship, so please don't wait long to voice your concerns :).
Reviewers: dberlin, sanjoy, grosser, brzycki, davide, chandlerc, hfinkel
Reviewed By: dberlin
Subscribers: nhaehnle, javed.absar, kparzysz, uabelho, jlebar, hiraditya, llvm-commits, dberlin, david2050
Differential Revision: https://reviews.llvm.org/D35851
llvm-svn: 310940
2017-08-16 02:14:57 +08:00
|
|
|
// Mark blocks live if there is no path from the block to a
|
|
|
|
// return of the function.
|
|
|
|
// We do this by seeing which of the postdomtree root children exit the
|
|
|
|
// program, and for all others, mark the subtree live.
|
|
|
|
for (auto &PDTChild : children<DomTreeNode *>(PDT.getRootNode())) {
|
|
|
|
auto *BB = PDTChild->getBlock();
|
|
|
|
auto &Info = BlockInfo[BB];
|
|
|
|
// Real function return
|
|
|
|
if (isa<ReturnInst>(Info.Terminator)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "post-dom root child is a return: " << BB->getName()
|
|
|
|
<< '\n';);
|
2017-03-03 05:08:37 +08:00
|
|
|
continue;
|
|
|
|
}
|
[Dominators] Include infinite loops in PostDominatorTree
Summary:
This patch teaches PostDominatorTree about infinite loops. It is built on top of D29705 by @dberlin which includes a very detailed motivation for this change.
What's new is that the patch also teaches the incremental updater how to deal with reverse-unreachable regions and how to properly maintain and verify tree roots. Before that, the incremental algorithm sometimes ended up preserving reverse-unreachable regions after updates that wouldn't appear in the tree if it was constructed from scratch on the same CFG.
This patch makes the following assumptions:
- A sequence of updates should produce the same tree as a recalculating it.
- Any sequence of the same updates should lead to the same tree.
- Siblings and roots are unordered.
The last two properties are essential to efficiently perform batch updates in the future.
When it comes to the first one, we can decide later that the consistency between freshly built tree and an updated one doesn't matter match, as there are many correct ways to pick roots in infinite loops, and to relax this assumption. That should enable us to recalculate postdominators less frequently.
This patch is pretty conservative when it comes to incremental updates on reverse-unreachable regions and ends up recalculating the whole tree in many cases. It should be possible to improve the performance in many cases, if we decide that it's important enough.
That being said, my experiments showed that reverse-unreachable are very rare in the IR emitted by clang when bootstrapping clang. Here are the statistics I collected by analyzing IR between passes and after each removePredecessor call:
```
# functions: 52283
# samples: 337609
# reverse unreachable BBs: 216022
# BBs: 247840796
Percent reverse-unreachable: 0.08716159869015269 %
Max(PercRevUnreachable) in a function: 87.58620689655172 %
# > 25 % samples: 471 ( 0.1395104988314885 % samples )
... in 145 ( 0.27733680163724345 % functions )
```
Most of the reverse-unreachable regions come from invalid IR where it wouldn't be possible to construct a PostDomTree anyway.
I would like to commit this patch in the next week in order to be able to complete the work that depends on it before the end of my internship, so please don't wait long to voice your concerns :).
Reviewers: dberlin, sanjoy, grosser, brzycki, davide, chandlerc, hfinkel
Reviewed By: dberlin
Subscribers: nhaehnle, javed.absar, kparzysz, uabelho, jlebar, hiraditya, llvm-commits, dberlin, david2050
Differential Revision: https://reviews.llvm.org/D35851
llvm-svn: 310940
2017-08-16 02:14:57 +08:00
|
|
|
|
|
|
|
// This child is something else, like an infinite loop.
|
|
|
|
for (auto DFNode : depth_first(PDTChild))
|
|
|
|
markLive(BlockInfo[DFNode->getBlock()].Terminator);
|
2016-08-24 08:10:06 +08:00
|
|
|
}
|
2016-08-16 22:31:51 +08:00
|
|
|
|
|
|
|
// Treat the entry block as always live
|
|
|
|
auto *BB = &F.getEntryBlock();
|
|
|
|
auto &EntryInfo = BlockInfo[BB];
|
|
|
|
EntryInfo.Live = true;
|
|
|
|
if (EntryInfo.UnconditionalBranch)
|
|
|
|
markLive(EntryInfo.Terminator);
|
|
|
|
|
|
|
|
// Build initial collection of blocks with dead terminators
|
|
|
|
for (auto &BBInfo : BlockInfo)
|
|
|
|
if (!BBInfo.second.terminatorIsLive())
|
|
|
|
BlocksWithDeadTerminators.insert(BBInfo.second.BB);
|
2016-08-06 03:38:11 +08:00
|
|
|
}
|
2016-03-30 06:57:12 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) {
|
|
|
|
// TODO -- use llvm::isInstructionTriviallyDead
|
2016-08-16 22:31:51 +08:00
|
|
|
if (I.isEHPad() || I.mayHaveSideEffects()) {
|
2016-08-06 03:38:11 +08:00
|
|
|
// Skip any value profile instrumentation calls if they are
|
|
|
|
// instrumenting constants.
|
2016-08-16 22:31:51 +08:00
|
|
|
if (isInstrumentsConstant(I))
|
|
|
|
return false;
|
|
|
|
return true;
|
2016-08-06 03:38:11 +08:00
|
|
|
}
|
2016-08-16 22:31:51 +08:00
|
|
|
if (!isa<TerminatorInst>(I))
|
|
|
|
return false;
|
|
|
|
if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I)))
|
|
|
|
return false;
|
|
|
|
return true;
|
2016-03-30 06:57:12 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 02:52:19 +08:00
|
|
|
// Check if this instruction is a runtime call for value profiling and
|
|
|
|
// if it's instrumenting a constant.
|
2016-08-06 03:38:11 +08:00
|
|
|
bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {
|
|
|
|
// TODO -- move this test into llvm::isInstructionTriviallyDead
|
2016-04-14 02:52:19 +08:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&I))
|
|
|
|
if (Function *Callee = CI->getCalledFunction())
|
|
|
|
if (Callee->getName().equals(getInstrProfValueProfFuncName()))
|
|
|
|
if (isa<Constant>(CI->getArgOperand(0)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
void AggressiveDeadCodeElimination::markLiveInstructions() {
|
2016-08-16 22:31:51 +08:00
|
|
|
// Propagate liveness backwards to operands.
|
|
|
|
do {
|
|
|
|
// Worklist holds newly discovered live instructions
|
|
|
|
// where we need to mark the inputs as live.
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
Instruction *LiveInst = Worklist.pop_back_val();
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "work live: "; LiveInst->dump(););
|
2016-03-30 06:57:12 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
for (Use &OI : LiveInst->operands())
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(OI))
|
|
|
|
markLive(Inst);
|
2016-12-14 00:42:18 +08:00
|
|
|
|
2016-09-20 07:17:58 +08:00
|
|
|
if (auto *PN = dyn_cast<PHINode>(LiveInst))
|
|
|
|
markPhiLive(PN);
|
2015-02-15 23:51:25 +08:00
|
|
|
}
|
2016-12-14 00:42:18 +08:00
|
|
|
|
|
|
|
// After data flow liveness has been identified, examine which branch
|
|
|
|
// decisions are required to determine live instructions are executed.
|
2016-08-16 22:31:51 +08:00
|
|
|
markLiveBranchesFromControlDependences();
|
2016-08-24 08:10:06 +08:00
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
} while (!Worklist.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDeadCodeElimination::markLive(Instruction *I) {
|
|
|
|
auto &Info = InstInfo[I];
|
|
|
|
if (Info.Live)
|
|
|
|
return;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "mark live: "; I->dump());
|
2016-08-16 22:31:51 +08:00
|
|
|
Info.Live = true;
|
|
|
|
Worklist.push_back(I);
|
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
// Collect the live debug info scopes attached to this instruction.
|
|
|
|
if (const DILocation *DL = I->getDebugLoc())
|
|
|
|
collectLiveScopes(*DL);
|
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
// Mark the containing block live
|
|
|
|
auto &BBInfo = *Info.Block;
|
2016-12-14 00:42:18 +08:00
|
|
|
if (BBInfo.Terminator == I) {
|
2016-08-16 22:31:51 +08:00
|
|
|
BlocksWithDeadTerminators.erase(BBInfo.BB);
|
2016-12-14 00:42:18 +08:00
|
|
|
// For live terminators, mark destination blocks
|
|
|
|
// live to preserve this control flow edges.
|
|
|
|
if (!BBInfo.UnconditionalBranch)
|
|
|
|
for (auto *BB : successors(I->getParent()))
|
|
|
|
markLive(BB);
|
|
|
|
}
|
|
|
|
markLive(BBInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDeadCodeElimination::markLive(BlockInfoType &BBInfo) {
|
2016-08-16 22:31:51 +08:00
|
|
|
if (BBInfo.Live)
|
|
|
|
return;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n');
|
2016-08-16 22:31:51 +08:00
|
|
|
BBInfo.Live = true;
|
2016-09-20 07:17:58 +08:00
|
|
|
if (!BBInfo.CFLive) {
|
|
|
|
BBInfo.CFLive = true;
|
|
|
|
NewLiveBlocks.insert(BBInfo.BB);
|
|
|
|
}
|
2016-08-16 22:31:51 +08:00
|
|
|
|
|
|
|
// Mark unconditional branches at the end of live
|
|
|
|
// blocks as live since there is no work to do for them later
|
2016-12-14 00:42:18 +08:00
|
|
|
if (BBInfo.UnconditionalBranch)
|
2016-08-24 08:10:06 +08:00
|
|
|
markLive(BBInfo.Terminator);
|
2016-08-06 03:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) {
|
|
|
|
if (!AliveScopes.insert(&LS).second)
|
|
|
|
return;
|
2016-08-16 22:31:51 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
if (isa<DISubprogram>(LS))
|
|
|
|
return;
|
2016-08-16 22:31:51 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
// Tail-recurse through the scope chain.
|
|
|
|
collectLiveScopes(cast<DILocalScope>(*LS.getScope()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {
|
|
|
|
// Even though DILocations are not scopes, shove them into AliveScopes so we
|
|
|
|
// don't revisit them.
|
|
|
|
if (!AliveScopes.insert(&DL).second)
|
|
|
|
return;
|
2016-08-16 22:31:51 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
// Collect live scopes from the scope chain.
|
|
|
|
collectLiveScopes(*DL.getScope());
|
2016-08-16 22:31:51 +08:00
|
|
|
|
2016-08-06 03:38:11 +08:00
|
|
|
// Tail-recurse through the inlined-at chain.
|
|
|
|
if (const DILocation *IA = DL.getInlinedAt())
|
|
|
|
collectLiveScopes(*IA);
|
|
|
|
}
|
|
|
|
|
2016-09-20 07:17:58 +08:00
|
|
|
void AggressiveDeadCodeElimination::markPhiLive(PHINode *PN) {
|
|
|
|
auto &Info = BlockInfo[PN->getParent()];
|
|
|
|
// Only need to check this once per block.
|
|
|
|
if (Info.HasLivePhiNodes)
|
|
|
|
return;
|
|
|
|
Info.HasLivePhiNodes = true;
|
|
|
|
|
|
|
|
// If a predecessor block is not live, mark it as control-flow live
|
|
|
|
// which will trigger marking live branches upon which
|
|
|
|
// that block is control dependent.
|
|
|
|
for (auto *PredBB : predecessors(Info.BB)) {
|
|
|
|
auto &Info = BlockInfo[PredBB];
|
|
|
|
if (!Info.CFLive) {
|
|
|
|
Info.CFLive = true;
|
|
|
|
NewLiveBlocks.insert(PredBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 22:31:51 +08:00
|
|
|
void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() {
|
2016-08-24 08:10:06 +08:00
|
|
|
if (BlocksWithDeadTerminators.empty())
|
|
|
|
return;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2016-08-24 08:10:06 +08:00
|
|
|
dbgs() << "new live blocks:\n";
|
|
|
|
for (auto *BB : NewLiveBlocks)
|
|
|
|
dbgs() << "\t" << BB->getName() << '\n';
|
|
|
|
dbgs() << "dead terminator blocks:\n";
|
|
|
|
for (auto *BB : BlocksWithDeadTerminators)
|
|
|
|
dbgs() << "\t" << BB->getName() << '\n';
|
|
|
|
});
|
|
|
|
|
|
|
|
// The dominance frontier of a live block X in the reverse
|
|
|
|
// control graph is the set of blocks upon which X is control
|
|
|
|
// dependent. The following sequence computes the set of blocks
|
|
|
|
// which currently have dead terminators that are control
|
|
|
|
// dependence sources of a block which is in NewLiveBlocks.
|
|
|
|
|
|
|
|
SmallVector<BasicBlock *, 32> IDFBlocks;
|
|
|
|
ReverseIDFCalculator IDFs(PDT);
|
|
|
|
IDFs.setDefiningBlocks(NewLiveBlocks);
|
|
|
|
IDFs.setLiveInBlocks(BlocksWithDeadTerminators);
|
|
|
|
IDFs.calculate(IDFBlocks);
|
2016-08-16 22:31:51 +08:00
|
|
|
NewLiveBlocks.clear();
|
2016-08-24 08:10:06 +08:00
|
|
|
|
|
|
|
// Dead terminators which control live blocks are now marked live.
|
2016-12-14 00:42:18 +08:00
|
|
|
for (auto *BB : IDFBlocks) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "live control in: " << BB->getName() << '\n');
|
2016-08-24 08:10:06 +08:00
|
|
|
markLive(BB->getTerminator());
|
|
|
|
}
|
2016-08-06 03:38:11 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 07:17:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Routines to update the CFG and SSA information before removing dead code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-08-06 03:38:11 +08:00
|
|
|
bool AggressiveDeadCodeElimination::removeDeadInstructions() {
|
2016-12-14 00:42:18 +08:00
|
|
|
// Updates control and dataflow around dead blocks
|
|
|
|
updateDeadRegions();
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2016-12-14 00:42:18 +08:00
|
|
|
for (Instruction &I : instructions(F)) {
|
|
|
|
// Check if the instruction is alive.
|
|
|
|
if (isLive(&I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
|
|
|
|
// Check if the scope of this variable location is alive.
|
|
|
|
if (AliveScopes.count(DII->getDebugLoc()->getScope()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If intrinsic is pointing at a live SSA value, there may be an
|
|
|
|
// earlier optimization bug: if we know the location of the variable,
|
|
|
|
// why isn't the scope of the location alive?
|
|
|
|
if (Value *V = DII->getVariableLocation())
|
|
|
|
if (Instruction *II = dyn_cast<Instruction>(V))
|
|
|
|
if (isLive(II))
|
|
|
|
dbgs() << "Dropping debug info for " << *DII << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
// The inverse of the live set is the dead set. These are those instructions
|
2016-12-14 00:42:18 +08:00
|
|
|
// that have no side effects and do not influence the control flow or return
|
2008-05-29 16:45:13 +08:00
|
|
|
// value of the function, and may therefore be deleted safely.
|
2015-02-15 23:45:28 +08:00
|
|
|
// NOTE: We reuse the Worklist vector here for memory efficiency.
|
2015-08-07 03:10:45 +08:00
|
|
|
for (Instruction &I : instructions(F)) {
|
2016-03-30 06:57:12 +08:00
|
|
|
// Check if the instruction is alive.
|
2016-08-16 22:31:51 +08:00
|
|
|
if (isLive(&I))
|
2016-03-30 06:57:12 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
|
|
|
|
// Check if the scope of this variable location is alive.
|
|
|
|
if (AliveScopes.count(DII->getDebugLoc()->getScope()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Fallthrough and drop the intrinsic.
|
2003-06-25 07:02:45 +08:00
|
|
|
}
|
2016-03-30 06:57:12 +08:00
|
|
|
|
|
|
|
// Prepare to delete.
|
|
|
|
Worklist.push_back(&I);
|
|
|
|
I.dropAllReferences();
|
2015-02-15 23:51:23 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2015-02-15 23:51:23 +08:00
|
|
|
for (Instruction *&I : Worklist) {
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumRemoved;
|
2015-02-15 23:51:23 +08:00
|
|
|
I->eraseFromParent();
|
2004-12-13 07:49:37 +08:00
|
|
|
}
|
2008-11-11 08:54:10 +08:00
|
|
|
|
2015-02-15 23:45:28 +08:00
|
|
|
return !Worklist.empty();
|
2001-09-10 06:26:47 +08:00
|
|
|
}
|
2008-05-29 16:45:13 +08:00
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
// A dead region is the set of dead blocks with a common live post-dominator.
|
|
|
|
void AggressiveDeadCodeElimination::updateDeadRegions() {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2016-12-14 00:42:18 +08:00
|
|
|
dbgs() << "final dead terminator blocks: " << '\n';
|
|
|
|
for (auto *BB : BlocksWithDeadTerminators)
|
|
|
|
dbgs() << '\t' << BB->getName()
|
|
|
|
<< (BlockInfo[BB].Live ? " LIVE\n" : "\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Don't compute the post ordering unless we needed it.
|
|
|
|
bool HavePostOrder = false;
|
|
|
|
|
|
|
|
for (auto *BB : BlocksWithDeadTerminators) {
|
|
|
|
auto &Info = BlockInfo[BB];
|
|
|
|
if (Info.UnconditionalBranch) {
|
|
|
|
InstInfo[Info.Terminator].Live = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HavePostOrder) {
|
|
|
|
computeReversePostOrder();
|
|
|
|
HavePostOrder = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an unconditional branch to the successor closest to the
|
|
|
|
// end of the function which insures a path to the exit for each
|
|
|
|
// live edge.
|
|
|
|
BlockInfoType *PreferredSucc = nullptr;
|
|
|
|
for (auto *Succ : successors(BB)) {
|
|
|
|
auto *Info = &BlockInfo[Succ];
|
|
|
|
if (!PreferredSucc || PreferredSucc->PostOrder < Info->PostOrder)
|
|
|
|
PreferredSucc = Info;
|
|
|
|
}
|
|
|
|
assert((PreferredSucc && PreferredSucc->PostOrder > 0) &&
|
2017-03-14 18:18:11 +08:00
|
|
|
"Failed to find safe successor for dead branch");
|
2017-08-23 00:30:21 +08:00
|
|
|
|
|
|
|
// Collect removed successors to update the (Post)DominatorTrees.
|
|
|
|
SmallPtrSet<BasicBlock *, 4> RemovedSuccessors;
|
2016-12-14 00:42:18 +08:00
|
|
|
bool First = true;
|
|
|
|
for (auto *Succ : successors(BB)) {
|
2017-08-23 00:30:21 +08:00
|
|
|
if (!First || Succ != PreferredSucc->BB) {
|
2016-12-14 00:42:18 +08:00
|
|
|
Succ->removePredecessor(BB);
|
2017-08-23 00:30:21 +08:00
|
|
|
RemovedSuccessors.insert(Succ);
|
|
|
|
} else
|
2016-12-14 00:42:18 +08:00
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
makeUnconditional(BB, PreferredSucc->BB);
|
2017-08-23 00:30:21 +08:00
|
|
|
|
|
|
|
// Inform the dominators about the deleted CFG edges.
|
|
|
|
SmallVector<DominatorTree::UpdateType, 4> DeletedEdges;
|
|
|
|
for (auto *Succ : RemovedSuccessors) {
|
|
|
|
// It might have happened that the same successor appeared multiple times
|
|
|
|
// and the CFG edge wasn't really removed.
|
|
|
|
if (Succ != PreferredSucc->BB) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ADCE: (Post)DomTree edge enqueued for deletion"
|
|
|
|
<< BB->getName() << " -> " << Succ->getName()
|
|
|
|
<< "\n");
|
2017-08-23 00:30:21 +08:00
|
|
|
DeletedEdges.push_back({DominatorTree::Delete, BB, Succ});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DT.applyUpdates(DeletedEdges);
|
|
|
|
PDT.applyUpdates(DeletedEdges);
|
|
|
|
|
2016-12-14 00:42:18 +08:00
|
|
|
NumBranchesRemoved += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse top-sort order
|
|
|
|
void AggressiveDeadCodeElimination::computeReversePostOrder() {
|
2017-03-14 18:18:11 +08:00
|
|
|
// This provides a post-order numbering of the reverse control flow graph
|
2016-12-14 00:42:18 +08:00
|
|
|
// Note that it is incomplete in the presence of infinite loops but we don't
|
|
|
|
// need numbers blocks which don't reach the end of the functions since
|
|
|
|
// all branches in those blocks are forced live.
|
2017-01-26 12:32:40 +08:00
|
|
|
|
2017-03-14 18:18:11 +08:00
|
|
|
// For each block without successors, extend the DFS from the block
|
2016-12-14 00:42:18 +08:00
|
|
|
// backward through the graph
|
|
|
|
SmallPtrSet<BasicBlock*, 16> Visited;
|
|
|
|
unsigned PostOrder = 0;
|
|
|
|
for (auto &BB : F) {
|
|
|
|
if (succ_begin(&BB) != succ_end(&BB))
|
|
|
|
continue;
|
|
|
|
for (BasicBlock *Block : inverse_post_order_ext(&BB,Visited))
|
|
|
|
BlockInfo[Block].PostOrder = PostOrder++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggressiveDeadCodeElimination::makeUnconditional(BasicBlock *BB,
|
|
|
|
BasicBlock *Target) {
|
|
|
|
TerminatorInst *PredTerm = BB->getTerminator();
|
|
|
|
// Collect the live debug info scopes attached to this instruction.
|
|
|
|
if (const DILocation *DL = PredTerm->getDebugLoc())
|
|
|
|
collectLiveScopes(*DL);
|
|
|
|
|
|
|
|
// Just mark live an existing unconditional branch
|
|
|
|
if (isUnconditionalBranch(PredTerm)) {
|
|
|
|
PredTerm->setSuccessor(0, Target);
|
|
|
|
InstInfo[PredTerm].Live = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "making unconditional " << BB->getName() << '\n');
|
2016-12-14 00:42:18 +08:00
|
|
|
NumBranchesRemoved += 1;
|
|
|
|
IRBuilder<> Builder(PredTerm);
|
|
|
|
auto *NewTerm = Builder.CreateBr(Target);
|
|
|
|
InstInfo[NewTerm].Live = true;
|
|
|
|
if (const DILocation *DL = PredTerm->getDebugLoc())
|
|
|
|
NewTerm->setDebugLoc(DL);
|
2017-08-23 00:30:21 +08:00
|
|
|
|
|
|
|
InstInfo.erase(PredTerm);
|
|
|
|
PredTerm->eraseFromParent();
|
2016-12-14 00:42:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-24 08:10:06 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Pass Manager integration code
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {
|
2017-08-23 00:30:21 +08:00
|
|
|
auto &DT = FAM.getResult<DominatorTreeAnalysis>(F);
|
2016-08-24 08:10:06 +08:00
|
|
|
auto &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F);
|
2017-08-23 00:30:21 +08:00
|
|
|
if (!AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination())
|
2016-06-01 01:39:39 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
2017-01-15 14:32:49 +08:00
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
2016-06-01 01:39:39 +08:00
|
|
|
PA.preserve<GlobalsAA>();
|
2017-08-23 00:30:21 +08:00
|
|
|
PA.preserve<DominatorTreeAnalysis>();
|
|
|
|
PA.preserve<PostDominatorTreeAnalysis>();
|
2016-06-01 01:39:39 +08:00
|
|
|
return PA;
|
2008-05-29 22:38:23 +08:00
|
|
|
}
|
2015-10-31 07:13:18 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2015-10-31 07:13:18 +08:00
|
|
|
struct ADCELegacyPass : public FunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2015-10-31 07:13:18 +08:00
|
|
|
ADCELegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2016-08-03 12:28:39 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipFunction(F))
|
2015-10-31 07:13:18 +08:00
|
|
|
return false;
|
2017-08-23 00:30:21 +08:00
|
|
|
|
|
|
|
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2016-08-24 08:10:06 +08:00
|
|
|
auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
|
2017-08-23 00:30:21 +08:00
|
|
|
return AggressiveDeadCodeElimination(F, DT, PDT)
|
|
|
|
.performDeadCodeElimination();
|
2015-10-31 07:13:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 12:28:39 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2017-08-23 00:30:21 +08:00
|
|
|
// We require DominatorTree here only to update and thus preserve it.
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2016-08-24 08:10:06 +08:00
|
|
|
AU.addRequired<PostDominatorTreeWrapperPass>();
|
2016-12-14 00:42:18 +08:00
|
|
|
if (!RemoveControlFlowFlag)
|
|
|
|
AU.setPreservesCFG();
|
2017-08-23 00:30:21 +08:00
|
|
|
else {
|
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
|
|
|
AU.addPreserved<PostDominatorTreeWrapperPass>();
|
|
|
|
}
|
2015-10-31 07:13:18 +08:00
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
|
|
|
}
|
|
|
|
};
|
2017-10-14 05:17:07 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2015-10-31 07:13:18 +08:00
|
|
|
|
|
|
|
char ADCELegacyPass::ID = 0;
|
2017-10-14 05:17:07 +08:00
|
|
|
|
2016-08-24 08:10:06 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(ADCELegacyPass, "adce",
|
|
|
|
"Aggressive Dead Code Elimination", false, false)
|
2017-08-23 00:30:21 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2016-08-24 08:10:06 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
|
|
|
|
false, false)
|
2015-10-31 07:13:18 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }
|