2008-05-29 16:45:13 +08:00
|
|
|
//===- DCE.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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 05:40:18 +08:00
|
|
|
#define DEBUG_TYPE "adce"
|
2002-05-08 04:03:00 +08:00
|
|
|
#include "llvm/Transforms/Scalar.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"
|
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"
|
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"
|
2003-12-19 17:08:34 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
STATISTIC(NumRemoved, "Number of instructions removed");
|
2002-05-07 01:27:57 +08:00
|
|
|
|
2006-12-20 05:40:18 +08:00
|
|
|
namespace {
|
2009-09-02 14:11:42 +08:00
|
|
|
struct ADCE : public FunctionPass {
|
2008-05-29 16:45:13 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
ADCE() : FunctionPass(ID) {
|
|
|
|
initializeADCEPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function& F) override;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage& AU) const override {
|
2008-05-29 16:45:13 +08:00
|
|
|
AU.setPreservesCFG();
|
2002-07-30 08:22:34 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
};
|
2003-06-25 07:02:45 +08:00
|
|
|
}
|
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
char ADCE::ID = 0;
|
2010-10-08 06:25:06 +08:00
|
|
|
INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
|
2003-06-25 07:02:45 +08:00
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
bool ADCE::runOnFunction(Function& F) {
|
2014-02-06 08:07:05 +08:00
|
|
|
if (skipOptnoneFunction(F))
|
|
|
|
return false;
|
|
|
|
|
2008-07-03 02:41:09 +08:00
|
|
|
SmallPtrSet<Instruction*, 128> alive;
|
|
|
|
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.
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
|
|
|
if (isa<TerminatorInst>(I.getInstructionIterator()) ||
|
2009-03-05 05:24:04 +08:00
|
|
|
isa<DbgInfoIntrinsic>(I.getInstructionIterator()) ||
|
2011-08-20 05:52:06 +08:00
|
|
|
isa<LandingPadInst>(I.getInstructionIterator()) ||
|
2009-05-06 14:49:50 +08:00
|
|
|
I->mayHaveSideEffects()) {
|
2008-05-29 16:45:13 +08:00
|
|
|
alive.insert(I.getInstructionIterator());
|
|
|
|
worklist.push_back(I.getInstructionIterator());
|
2001-09-10 06:26:47 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2008-05-29 16:45:13 +08:00
|
|
|
// Propagate liveness backwards to operands.
|
|
|
|
while (!worklist.empty()) {
|
2010-01-06 00:27:25 +08:00
|
|
|
Instruction* curr = worklist.pop_back_val();
|
2008-05-29 16:45:13 +08:00
|
|
|
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 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.
|
2008-06-23 14:13:12 +08:00
|
|
|
// NOTE: We reuse the worklist vector here for memory efficiency.
|
2008-05-29 16:45:13 +08:00
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
|
|
|
|
if (!alive.count(I.getInstructionIterator())) {
|
2008-06-23 14:13:12 +08:00
|
|
|
worklist.push_back(I.getInstructionIterator());
|
2008-05-29 16:45:13 +08:00
|
|
|
I->dropAllReferences();
|
2003-06-25 07:02:45 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<Instruction *>::iterator I = worklist.begin(),
|
2008-06-23 14:13:12 +08:00
|
|
|
E = worklist.end(); I != E; ++I) {
|
2010-06-22 23:08:57 +08:00
|
|
|
++NumRemoved;
|
2008-05-29 16:45:13 +08:00
|
|
|
(*I)->eraseFromParent();
|
2004-12-13 07:49:37 +08:00
|
|
|
}
|
2008-11-11 08:54:10 +08:00
|
|
|
|
2008-11-20 02:59:41 +08:00
|
|
|
return !worklist.empty();
|
2001-09-10 06:26:47 +08:00
|
|
|
}
|
2008-05-29 16:45:13 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createAggressiveDCEPass() {
|
|
|
|
return new ADCE();
|
2008-05-29 22:38:23 +08:00
|
|
|
}
|