2008-05-29 10:45:13 +02:00
|
|
|
//===- DCE.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,
|
|
|
|
// allowing it to eliminate dead computations that other DCE passes do not
|
|
|
|
// catch, particularly involving loop computations.
|
2001-06-30 08:39:11 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
#define DEBUG_TYPE "adce"
|
2002-05-07 22:03:00 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2008-07-02 20:05:19 +02:00
|
|
|
#include "llvm/BasicBlock.h"
|
2004-04-10 08:53:09 +02:00
|
|
|
#include "llvm/Instructions.h"
|
2009-03-04 22:24:04 +01:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2008-05-29 10:45:13 +02:00
|
|
|
#include "llvm/Pass.h"
|
2008-07-02 20:05:19 +02:00
|
|
|
#include "llvm/Support/CFG.h"
|
2008-05-29 10:45:13 +02:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2008-07-02 20:05:19 +02:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2008-07-02 20:41:09 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2008-06-23 08:13:12 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-07-02 20:41:09 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-12-19 10:08:34 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
STATISTIC(NumRemoved, "Number of instructions removed");
|
2002-05-06 19:27:57 +02:00
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
namespace {
|
2009-09-02 08:11:42 +02:00
|
|
|
struct ADCE : public FunctionPass {
|
2008-05-29 10:45:13 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 19:05:41 +02:00
|
|
|
ADCE() : FunctionPass(&ID) {}
|
2008-05-29 10:45:13 +02:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function& F);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage& AU) const {
|
|
|
|
AU.setPreservesCFG();
|
2002-07-30 02:22:34 +02:00
|
|
|
}
|
2008-05-29 10:45:13 +02:00
|
|
|
|
|
|
|
};
|
2003-06-25 01:02:45 +02:00
|
|
|
}
|
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
char ADCE::ID = 0;
|
|
|
|
static RegisterPass<ADCE> X("adce", "Aggressive Dead Code Elimination");
|
2003-06-25 01:02:45 +02:00
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
bool ADCE::runOnFunction(Function& F) {
|
2008-07-02 20:41:09 +02:00
|
|
|
SmallPtrSet<Instruction*, 128> alive;
|
|
|
|
SmallVector<Instruction*, 128> worklist;
|
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
// Collect the set of "root" instructions that are known live.
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
|
|
|
if (isa<TerminatorInst>(I.getInstructionIterator()) ||
|
2009-03-04 22:24:04 +01:00
|
|
|
isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
|
2009-05-06 08:49:50 +02:00
|
|
|
I->mayHaveSideEffects()) {
|
2008-05-29 10:45:13 +02:00
|
|
|
alive.insert(I.getInstructionIterator());
|
|
|
|
worklist.push_back(I.getInstructionIterator());
|
2001-09-10 00:26:47 +02:00
|
|
|
}
|
2008-05-16 06:34:51 +02:00
|
|
|
|
2008-05-29 10:45:13 +02:00
|
|
|
// Propagate liveness backwards to operands.
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
Instruction* curr = worklist.back();
|
|
|
|
worklist.pop_back();
|
|
|
|
|
|
|
|
for (Instruction::op_iterator OI = curr->op_begin(), OE = curr->op_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
if (Instruction* Inst = dyn_cast<Instruction>(OI))
|
|
|
|
if (alive.insert(Inst))
|
|
|
|
worklist.push_back(Inst);
|
2001-09-10 00:26:47 +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.
|
2008-06-23 08:13:12 +02:00
|
|
|
// NOTE: We reuse the worklist vector here for memory efficiency.
|
2008-05-29 10:45:13 +02:00
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
|
|
|
if (!alive.count(I.getInstructionIterator())) {
|
2008-06-23 08:13:12 +02:00
|
|
|
worklist.push_back(I.getInstructionIterator());
|
2008-05-29 10:45:13 +02:00
|
|
|
I->dropAllReferences();
|
2003-06-25 01:02:45 +02:00
|
|
|
}
|
2008-05-29 10:45:13 +02:00
|
|
|
|
2008-06-23 08:13:12 +02:00
|
|
|
for (SmallVector<Instruction*, 1024>::iterator I = worklist.begin(),
|
|
|
|
E = worklist.end(); I != E; ++I) {
|
2008-05-29 10:45:13 +02:00
|
|
|
NumRemoved++;
|
|
|
|
(*I)->eraseFromParent();
|
2004-12-13 00:49:37 +01:00
|
|
|
}
|
2008-11-11 01:54:10 +01:00
|
|
|
|
2008-11-19 19:59:41 +01:00
|
|
|
return !worklist.empty();
|
2001-09-10 00:26:47 +02:00
|
|
|
}
|
2008-05-29 10:45:13 +02:00
|
|
|
|
|
|
|
FunctionPass *llvm::createAggressiveDCEPass() {
|
|
|
|
return new ADCE();
|
2008-05-29 16:38:23 +02:00
|
|
|
}
|