2015-09-25 23:03:46 +02:00
|
|
|
//===- ADCE.cpp - Code to perform dead code elimination -------------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-30 08:39:11 +02:00
|
|
|
//
|
2008-05-29 10:45:13 +02: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 12:51:42 +02:00
|
|
|
// allowing it to eliminate dead computations that other DCE passes do not
|
2008-05-29 10:45:13 +02:00
|
|
|
// catch, particularly involving loop computations.
|
2001-06-30 08:39:11 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-31 00:13:18 +01:00
|
|
|
#include "llvm/Transforms/Scalar/ADCE.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-09-10 12:22:12 +02:00
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2014-03-04 12:45:46 +01:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-03-04 11:30:26 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2008-05-29 10:45:13 +02:00
|
|
|
#include "llvm/Pass.h"
|
2015-10-31 00:13:18 +01:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2003-12-19 10:08:34 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2014-04-22 04:55:47 +02:00
|
|
|
#define DEBUG_TYPE "adce"
|
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
STATISTIC(NumRemoved, "Number of instructions removed");
|
2002-05-06 19:27:57 +02:00
|
|
|
|
2015-10-31 00:13:18 +01:00
|
|
|
static bool aggressiveDCE(Function& F) {
|
2015-02-15 16:45:28 +01:00
|
|
|
SmallPtrSet<Instruction*, 128> Alive;
|
|
|
|
SmallVector<Instruction*, 128> Worklist;
|
2012-07-24 12:51:42 +02:00
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
// Collect the set of "root" instructions that are known live.
|
2015-08-06 21:10:45 +02:00
|
|
|
for (Instruction &I : instructions(F)) {
|
2015-07-31 19:58:14 +02:00
|
|
|
if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) || I.isEHPad() ||
|
|
|
|
I.mayHaveSideEffects()) {
|
2015-02-15 16:51:23 +01:00
|
|
|
Alive.insert(&I);
|
|
|
|
Worklist.push_back(&I);
|
2001-09-10 00:26:47 +02:00
|
|
|
}
|
2015-02-15 16:51:23 +01:00
|
|
|
}
|
2012-07-24 12:51:42 +02:00
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
// Propagate liveness backwards to operands.
|
2015-02-15 16:45:28 +01:00
|
|
|
while (!Worklist.empty()) {
|
2015-02-15 16:47:52 +01:00
|
|
|
Instruction *Curr = Worklist.pop_back_val();
|
2015-02-15 16:51:25 +01:00
|
|
|
for (Use &OI : Curr->operands()) {
|
2015-02-15 16:47:52 +01:00
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(OI))
|
2015-02-15 16:45:28 +01:00
|
|
|
if (Alive.insert(Inst).second)
|
|
|
|
Worklist.push_back(Inst);
|
2015-02-15 16:51:25 +01:00
|
|
|
}
|
2001-09-10 00:26:47 +02:00
|
|
|
}
|
2012-07-24 12:51:42 +02:00
|
|
|
|
2008-05-29 10:45:13 +02: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 16:45:28 +01:00
|
|
|
// NOTE: We reuse the Worklist vector here for memory efficiency.
|
2015-08-06 21:10:45 +02:00
|
|
|
for (Instruction &I : instructions(F)) {
|
2015-02-15 16:51:23 +01:00
|
|
|
if (!Alive.count(&I)) {
|
|
|
|
Worklist.push_back(&I);
|
|
|
|
I.dropAllReferences();
|
2003-06-25 01:02:45 +02:00
|
|
|
}
|
2015-02-15 16:51:23 +01:00
|
|
|
}
|
2012-07-24 12:51:42 +02:00
|
|
|
|
2015-02-15 16:51:23 +01:00
|
|
|
for (Instruction *&I : Worklist) {
|
2010-06-22 17:08:57 +02:00
|
|
|
++NumRemoved;
|
2015-02-15 16:51:23 +01:00
|
|
|
I->eraseFromParent();
|
2004-12-13 00:49:37 +01:00
|
|
|
}
|
2008-11-11 01:54:10 +01:00
|
|
|
|
2015-02-15 16:45:28 +01:00
|
|
|
return !Worklist.empty();
|
2001-09-10 00:26:47 +02:00
|
|
|
}
|
2008-05-29 10:45:13 +02:00
|
|
|
|
2015-10-31 00:13:18 +01:00
|
|
|
PreservedAnalyses ADCEPass::run(Function &F) {
|
|
|
|
if (aggressiveDCE(F))
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
return PreservedAnalyses::all();
|
2008-05-29 16:38:23 +02:00
|
|
|
}
|
2015-10-31 00:13:18 +01: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 {
|
|
|
|
if (skipOptnoneFunction(F))
|
|
|
|
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(); }
|