[ADCE] Refactor anticipating new functionality (NFC)

Summary:
This is the first refactoring before adding new functionality.
Add a class wrapper for the functions and container for
state associated with the transformation.

No functional change

Reviewers: majnemer, nadav, mehdi_amini

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D23065

llvm-svn: 277565
This commit is contained in:
David Callahan 2016-08-03 04:28:39 +00:00
parent f9721ba5f1
commit cc5cd4dc65
1 changed files with 34 additions and 17 deletions

View File

@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar/ADCE.h" #include "llvm/Transforms/Scalar/ADCE.h"
#include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
@ -35,8 +36,28 @@ using namespace llvm;
STATISTIC(NumRemoved, "Number of instructions removed"); STATISTIC(NumRemoved, "Number of instructions removed");
static void collectLiveScopes(const DILocalScope &LS, namespace {
SmallPtrSetImpl<const Metadata *> &AliveScopes) { class AgggressiveDeadCodeElimination {
Function &F;
// Instructions known to be live
SmallPtrSet<Instruction *, 32> Alive;
// Instructions known to be live where we need to mark
// reaching definitions as live
SmallVector<Instruction *, 128> Worklist;
// Debug info scopes around a live instruction
SmallPtrSet<const Metadata *, 32> AliveScopes;
void collectLiveScopes(const DILocalScope &LS);
void collectLiveScopes(const DILocation &DL);
bool isInstrumentsConstant(Instruction &I);
public:
AgggressiveDeadCodeElimination(Function &F) : F(F) {}
bool aggressiveDCE();
};
}
void AgggressiveDeadCodeElimination::collectLiveScopes(
const DILocalScope &LS) {
if (!AliveScopes.insert(&LS).second) if (!AliveScopes.insert(&LS).second)
return; return;
@ -44,27 +65,26 @@ static void collectLiveScopes(const DILocalScope &LS,
return; return;
// Tail-recurse through the scope chain. // Tail-recurse through the scope chain.
collectLiveScopes(cast<DILocalScope>(*LS.getScope()), AliveScopes); collectLiveScopes(cast<DILocalScope>(*LS.getScope()));
} }
static void collectLiveScopes(const DILocation &DL, void AgggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {
SmallPtrSetImpl<const Metadata *> &AliveScopes) {
// Even though DILocations are not scopes, shove them into AliveScopes so we // Even though DILocations are not scopes, shove them into AliveScopes so we
// don't revisit them. // don't revisit them.
if (!AliveScopes.insert(&DL).second) if (!AliveScopes.insert(&DL).second)
return; return;
// Collect live scopes from the scope chain. // Collect live scopes from the scope chain.
collectLiveScopes(*DL.getScope(), AliveScopes); collectLiveScopes(*DL.getScope());
// Tail-recurse through the inlined-at chain. // Tail-recurse through the inlined-at chain.
if (const DILocation *IA = DL.getInlinedAt()) if (const DILocation *IA = DL.getInlinedAt())
collectLiveScopes(*IA, AliveScopes); collectLiveScopes(*IA);
} }
// Check if this instruction is a runtime call for value profiling and // Check if this instruction is a runtime call for value profiling and
// if it's instrumenting a constant. // if it's instrumenting a constant.
static bool isInstrumentsConstant(Instruction &I) { bool AgggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {
if (CallInst *CI = dyn_cast<CallInst>(&I)) if (CallInst *CI = dyn_cast<CallInst>(&I))
if (Function *Callee = CI->getCalledFunction()) if (Function *Callee = CI->getCalledFunction())
if (Callee->getName().equals(getInstrProfValueProfFuncName())) if (Callee->getName().equals(getInstrProfValueProfFuncName()))
@ -73,9 +93,7 @@ static bool isInstrumentsConstant(Instruction &I) {
return false; return false;
} }
static bool aggressiveDCE(Function& F) { bool AgggressiveDeadCodeElimination::aggressiveDCE() {
SmallPtrSet<Instruction*, 32> Alive;
SmallVector<Instruction*, 128> Worklist;
// Collect the set of "root" instructions that are known live. // Collect the set of "root" instructions that are known live.
for (Instruction &I : instructions(F)) { for (Instruction &I : instructions(F)) {
@ -91,13 +109,12 @@ static bool aggressiveDCE(Function& F) {
// Propagate liveness backwards to operands. Keep track of live debug info // Propagate liveness backwards to operands. Keep track of live debug info
// scopes. // scopes.
SmallPtrSet<const Metadata *, 32> AliveScopes;
while (!Worklist.empty()) { while (!Worklist.empty()) {
Instruction *Curr = Worklist.pop_back_val(); Instruction *Curr = Worklist.pop_back_val();
// Collect the live debug info scopes attached to this instruction. // Collect the live debug info scopes attached to this instruction.
if (const DILocation *DL = Curr->getDebugLoc()) if (const DILocation *DL = Curr->getDebugLoc())
collectLiveScopes(*DL, AliveScopes); collectLiveScopes(*DL);
for (Use &OI : Curr->operands()) { for (Use &OI : Curr->operands()) {
if (Instruction *Inst = dyn_cast<Instruction>(OI)) if (Instruction *Inst = dyn_cast<Instruction>(OI))
@ -146,7 +163,7 @@ static bool aggressiveDCE(Function& F) {
} }
PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &) { PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &) {
if (!aggressiveDCE(F)) if (!AgggressiveDeadCodeElimination(F).aggressiveDCE())
return PreservedAnalyses::all(); return PreservedAnalyses::all();
// FIXME: This should also 'preserve the CFG'. // FIXME: This should also 'preserve the CFG'.
@ -162,13 +179,13 @@ struct ADCELegacyPass : public FunctionPass {
initializeADCELegacyPassPass(*PassRegistry::getPassRegistry()); initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
} }
bool runOnFunction(Function& F) override { bool runOnFunction(Function &F) override {
if (skipFunction(F)) if (skipFunction(F))
return false; return false;
return aggressiveDCE(F); return AgggressiveDeadCodeElimination(F).aggressiveDCE();
} }
void getAnalysisUsage(AnalysisUsage& AU) const override { void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG(); AU.setPreservesCFG();
AU.addPreserved<GlobalsAAWrapperPass>(); AU.addPreserved<GlobalsAAWrapperPass>();
} }