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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#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"
|
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"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.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"
|
2015-10-31 07:13:18 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
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");
|
2002-05-07 01:27:57 +08:00
|
|
|
|
2016-03-30 06:57:12 +08:00
|
|
|
static void collectLiveScopes(const DILocalScope &LS,
|
|
|
|
SmallPtrSetImpl<const Metadata *> &AliveScopes) {
|
|
|
|
if (!AliveScopes.insert(&LS).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isa<DISubprogram>(LS))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Tail-recurse through the scope chain.
|
|
|
|
collectLiveScopes(cast<DILocalScope>(*LS.getScope()), AliveScopes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void collectLiveScopes(const DILocation &DL,
|
|
|
|
SmallPtrSetImpl<const Metadata *> &AliveScopes) {
|
|
|
|
// Even though DILocations are not scopes, shove them into AliveScopes so we
|
|
|
|
// don't revisit them.
|
|
|
|
if (!AliveScopes.insert(&DL).second)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Collect live scopes from the scope chain.
|
|
|
|
collectLiveScopes(*DL.getScope(), AliveScopes);
|
|
|
|
|
|
|
|
// Tail-recurse through the inlined-at chain.
|
|
|
|
if (const DILocation *IA = DL.getInlinedAt())
|
|
|
|
collectLiveScopes(*IA, AliveScopes);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
static bool isInstrumentsConstant(Instruction &I) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-31 07:13:18 +08:00
|
|
|
static bool aggressiveDCE(Function& F) {
|
2016-01-30 09:24:31 +08:00
|
|
|
SmallPtrSet<Instruction*, 32> Alive;
|
2015-02-15 23:45:28 +08:00
|
|
|
SmallVector<Instruction*, 128> Worklist;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
// Collect the set of "root" instructions that are known live.
|
2015-08-07 03:10:45 +08:00
|
|
|
for (Instruction &I : instructions(F)) {
|
2016-03-30 06:57:12 +08:00
|
|
|
if (isa<TerminatorInst>(I) || I.isEHPad() || I.mayHaveSideEffects()) {
|
2016-04-14 02:52:19 +08:00
|
|
|
// Skip any value profile instrumentation calls if they are
|
|
|
|
// instrumenting constants.
|
|
|
|
if (isInstrumentsConstant(I))
|
|
|
|
continue;
|
2015-02-15 23:51:23 +08:00
|
|
|
Alive.insert(&I);
|
|
|
|
Worklist.push_back(&I);
|
2001-09-10 06:26:47 +08:00
|
|
|
}
|
2015-02-15 23:51:23 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2016-03-30 06:57:12 +08:00
|
|
|
// Propagate liveness backwards to operands. Keep track of live debug info
|
|
|
|
// scopes.
|
|
|
|
SmallPtrSet<const Metadata *, 32> AliveScopes;
|
2015-02-15 23:45:28 +08:00
|
|
|
while (!Worklist.empty()) {
|
2015-02-15 23:47:52 +08:00
|
|
|
Instruction *Curr = Worklist.pop_back_val();
|
2016-03-30 06:57:12 +08:00
|
|
|
|
|
|
|
// Collect the live debug info scopes attached to this instruction.
|
|
|
|
if (const DILocation *DL = Curr->getDebugLoc())
|
|
|
|
collectLiveScopes(*DL, AliveScopes);
|
|
|
|
|
2015-02-15 23:51:25 +08:00
|
|
|
for (Use &OI : Curr->operands()) {
|
2015-02-15 23:47:52 +08:00
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(OI))
|
2015-02-15 23:45:28 +08:00
|
|
|
if (Alive.insert(Inst).second)
|
|
|
|
Worklist.push_back(Inst);
|
2015-02-15 23:51:25 +08:00
|
|
|
}
|
2001-09-10 06:26:47 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
// The inverse of the live set is the dead set. These are those instructions
|
|
|
|
// which have no side effects and do not influence the control flow or return
|
|
|
|
// 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.
|
|
|
|
if (Alive.count(&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;
|
|
|
|
|
|
|
|
// Fallthrough and drop the intrinsic.
|
|
|
|
DEBUG({
|
|
|
|
// 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 (Alive.count(II))
|
|
|
|
dbgs() << "Dropping debug info for " << *DII << "\n";
|
|
|
|
});
|
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
|
|
|
|
2015-10-31 07:13:18 +08:00
|
|
|
PreservedAnalyses ADCEPass::run(Function &F) {
|
2016-06-01 01:39:39 +08:00
|
|
|
if (!aggressiveDCE(F))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
// FIXME: ADCE should also 'preserve the CFG'.
|
|
|
|
// The new pass manager has currently no way to do it.
|
|
|
|
auto PA = PreservedAnalyses();
|
|
|
|
PA.preserve<GlobalsAA>();
|
|
|
|
return PA;
|
2008-05-29 22:38:23 +08:00
|
|
|
}
|
2015-10-31 07:13:18 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ADCELegacyPass : public FunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
ADCELegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
return aggressiveDCE(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage& AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char ADCELegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
|
|
|
|
false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }
|