2007-08-03 02:11:11 +08:00
|
|
|
//===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
|
2007-07-11 08:46:18 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-07-12 03:03:09 +08:00
|
|
|
// This file was developed by Owen Anderson and is distributed under
|
2007-07-11 08:46:18 +08:00
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a trivial dead store elimination that only considers
|
|
|
|
// basic-block local redundant stores.
|
|
|
|
//
|
|
|
|
// FIXME: This should eventually be extended to be a post-dominator tree
|
|
|
|
// traversal. Doing so would be pretty trivial.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-08-01 14:36:51 +08:00
|
|
|
#define DEBUG_TYPE "dse"
|
2007-07-11 08:46:18 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2007-07-13 05:41:30 +08:00
|
|
|
#include "llvm/Constants.h"
|
2007-07-11 08:46:18 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-07-12 07:19:17 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2007-07-11 08:46:18 +08:00
|
|
|
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
|
2007-07-12 07:19:17 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-07-11 08:46:18 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumFastStores, "Number of stores deleted");
|
|
|
|
STATISTIC(NumFastOther , "Number of other instrs removed");
|
|
|
|
|
|
|
|
namespace {
|
2007-08-01 14:36:51 +08:00
|
|
|
struct VISIBILITY_HIDDEN DSE : public FunctionPass {
|
2007-07-11 08:46:18 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 14:36:51 +08:00
|
|
|
DSE() : FunctionPass((intptr_t)&ID) {}
|
2007-07-11 08:46:18 +08:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
|
|
|
Changed |= runOnBasicBlock(*I);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
2007-08-03 02:11:11 +08:00
|
|
|
bool handleFreeWithNonTrivialDependency(FreeInst* F,
|
|
|
|
Instruction* dependency,
|
|
|
|
SetVector<Instruction*>& possiblyDead);
|
2007-07-13 05:41:30 +08:00
|
|
|
bool handleEndBlock(BasicBlock& BB, SetVector<Instruction*>& possiblyDead);
|
2007-08-09 01:50:09 +08:00
|
|
|
bool RemoveUndeadPointers(Value* pointer,
|
2007-07-13 05:41:30 +08:00
|
|
|
BasicBlock::iterator& BBI,
|
2007-08-09 01:50:09 +08:00
|
|
|
SmallPtrSet<AllocaInst*, 64>& deadPointers,
|
2007-07-13 05:41:30 +08:00
|
|
|
SetVector<Instruction*>& possiblyDead);
|
2007-07-11 08:46:18 +08:00
|
|
|
void DeleteDeadInstructionChains(Instruction *I,
|
|
|
|
SetVector<Instruction*> &DeadInsts);
|
2007-08-08 12:52:29 +08:00
|
|
|
|
2007-08-08 14:06:02 +08:00
|
|
|
/// Find the base pointer that a pointer came from
|
|
|
|
/// Because this is used to find pointers that originate
|
|
|
|
/// from allocas, it is safe to ignore GEP indices, since
|
|
|
|
/// either the store will be in the alloca, and thus dead,
|
|
|
|
/// or beyond the end of the alloca, and thus undefined.
|
2007-07-14 02:26:26 +08:00
|
|
|
void TranslatePointerBitCasts(Value*& v) {
|
2007-08-03 02:11:11 +08:00
|
|
|
assert(isa<PointerType>(v->getType()) &&
|
|
|
|
"Translating a non-pointer type?");
|
2007-08-08 12:52:29 +08:00
|
|
|
while (true) {
|
2007-07-17 07:34:39 +08:00
|
|
|
if (BitCastInst* C = dyn_cast<BitCastInst>(v))
|
|
|
|
v = C->getOperand(0);
|
|
|
|
else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(v))
|
|
|
|
v = G->getOperand(0);
|
2007-08-08 12:52:29 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2007-07-14 02:26:26 +08:00
|
|
|
}
|
2007-07-11 08:46:18 +08:00
|
|
|
|
|
|
|
// getAnalysisUsage - We require post dominance frontiers (aka Control
|
|
|
|
// Dependence Graph)
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
2007-07-12 07:19:17 +08:00
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
2007-07-11 08:46:18 +08:00
|
|
|
AU.addRequired<MemoryDependenceAnalysis>();
|
2007-07-12 07:19:17 +08:00
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2007-07-11 08:46:18 +08:00
|
|
|
AU.addPreserved<MemoryDependenceAnalysis>();
|
|
|
|
}
|
|
|
|
};
|
2007-08-01 14:36:51 +08:00
|
|
|
char DSE::ID = 0;
|
|
|
|
RegisterPass<DSE> X("dse", "Dead Store Elimination");
|
2007-07-11 08:46:18 +08:00
|
|
|
}
|
|
|
|
|
2007-08-01 14:36:51 +08:00
|
|
|
FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
|
2007-07-11 08:46:18 +08:00
|
|
|
|
2007-08-01 14:36:51 +08:00
|
|
|
bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
2007-07-11 08:46:18 +08:00
|
|
|
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
|
|
|
|
|
2007-07-12 03:03:09 +08:00
|
|
|
// Record the last-seen store to this pointer
|
2007-07-11 08:46:18 +08:00
|
|
|
DenseMap<Value*, StoreInst*> lastStore;
|
2007-07-12 03:03:09 +08:00
|
|
|
// Record instructions possibly made dead by deleting a store
|
2007-07-11 08:46:18 +08:00
|
|
|
SetVector<Instruction*> possiblyDead;
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// Do a top-down walk on the BB
|
2007-08-03 02:11:11 +08:00
|
|
|
for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end();
|
|
|
|
BBI != BBE; ++BBI) {
|
2007-07-12 05:06:56 +08:00
|
|
|
// If we find a store or a free...
|
2007-08-08 12:52:29 +08:00
|
|
|
if (!isa<StoreInst>(BBI) && !isa<FreeInst>(BBI))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Value* pointer = 0;
|
2007-08-27 05:14:47 +08:00
|
|
|
if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
|
|
|
|
if (!S->isVolatile())
|
|
|
|
pointer = S->getPointerOperand();
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
} else
|
2007-08-08 14:06:02 +08:00
|
|
|
pointer = cast<FreeInst>(BBI)->getPointerOperand();
|
2007-07-12 04:38:34 +08:00
|
|
|
|
2007-08-08 12:52:29 +08:00
|
|
|
StoreInst*& last = lastStore[pointer];
|
|
|
|
bool deletedStore = false;
|
2007-07-11 08:46:18 +08:00
|
|
|
|
2007-08-08 12:52:29 +08:00
|
|
|
// ... to a pointer that has been stored to before...
|
|
|
|
if (last) {
|
2007-08-09 12:42:44 +08:00
|
|
|
Instruction* dep = MD.getDependency(BBI);
|
2007-07-11 08:46:18 +08:00
|
|
|
|
2007-08-08 12:52:29 +08:00
|
|
|
// ... and no other memory dependencies are between them....
|
|
|
|
while (dep != MemoryDependenceAnalysis::None &&
|
|
|
|
dep != MemoryDependenceAnalysis::NonLocal &&
|
|
|
|
isa<StoreInst>(dep)) {
|
|
|
|
if (dep != last) {
|
2007-08-09 12:42:44 +08:00
|
|
|
dep = MD.getDependency(BBI, dep);
|
2007-08-08 12:52:29 +08:00
|
|
|
continue;
|
|
|
|
}
|
2007-07-17 05:52:50 +08:00
|
|
|
|
2007-08-08 12:52:29 +08:00
|
|
|
// Remove it!
|
|
|
|
MD.removeInstruction(last);
|
2007-07-11 08:46:18 +08:00
|
|
|
|
2007-08-08 12:52:29 +08:00
|
|
|
// DCE instructions only used to calculate that store
|
|
|
|
if (Instruction* D = dyn_cast<Instruction>(last->getOperand(0)))
|
|
|
|
possiblyDead.insert(D);
|
|
|
|
if (Instruction* D = dyn_cast<Instruction>(last->getOperand(1)))
|
|
|
|
possiblyDead.insert(D);
|
2007-07-11 08:46:18 +08:00
|
|
|
|
2007-08-08 12:52:29 +08:00
|
|
|
last->eraseFromParent();
|
|
|
|
NumFastStores++;
|
|
|
|
deletedStore = true;
|
|
|
|
MadeChange = true;
|
|
|
|
|
|
|
|
break;
|
2007-07-11 08:46:18 +08:00
|
|
|
}
|
2007-08-08 12:52:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle frees whose dependencies are non-trivial.
|
|
|
|
if (FreeInst* F = dyn_cast<FreeInst>(BBI)) {
|
|
|
|
if (!deletedStore)
|
|
|
|
MadeChange |= handleFreeWithNonTrivialDependency(F,
|
2007-08-09 12:42:44 +08:00
|
|
|
MD.getDependency(F),
|
2007-08-08 12:52:29 +08:00
|
|
|
possiblyDead);
|
|
|
|
// No known stores after the free
|
|
|
|
last = 0;
|
|
|
|
} else {
|
|
|
|
// Update our most-recent-store map.
|
|
|
|
last = cast<StoreInst>(BBI);
|
2007-07-11 08:46:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-13 05:41:30 +08:00
|
|
|
// If this block ends in a return, unwind, unreachable, and eventually
|
|
|
|
// tailcall, then all allocas are dead at its end.
|
|
|
|
if (BB.getTerminator()->getNumSuccessors() == 0)
|
|
|
|
MadeChange |= handleEndBlock(BB, possiblyDead);
|
|
|
|
|
2007-07-11 08:46:18 +08:00
|
|
|
// Do a trivial DCE
|
|
|
|
while (!possiblyDead.empty()) {
|
|
|
|
Instruction *I = possiblyDead.back();
|
|
|
|
possiblyDead.pop_back();
|
|
|
|
DeleteDeadInstructionChains(I, possiblyDead);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2007-07-12 07:19:17 +08:00
|
|
|
/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
|
|
|
|
/// dependency is a store to a field of that structure
|
2007-08-01 14:36:51 +08:00
|
|
|
bool DSE::handleFreeWithNonTrivialDependency(FreeInst* F, Instruction* dep,
|
2007-08-03 02:11:11 +08:00
|
|
|
SetVector<Instruction*>& possiblyDead) {
|
2007-07-12 07:19:17 +08:00
|
|
|
TargetData &TD = getAnalysis<TargetData>();
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
|
|
|
|
|
2007-07-13 02:08:51 +08:00
|
|
|
if (dep == MemoryDependenceAnalysis::None ||
|
|
|
|
dep == MemoryDependenceAnalysis::NonLocal)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
StoreInst* dependency = dyn_cast<StoreInst>(dep);
|
|
|
|
if (!dependency)
|
|
|
|
return false;
|
2007-08-27 05:14:47 +08:00
|
|
|
else if (dependency->isVolatile())
|
|
|
|
return false;
|
2007-07-13 02:08:51 +08:00
|
|
|
|
2007-07-12 07:19:17 +08:00
|
|
|
Value* depPointer = dependency->getPointerOperand();
|
2007-08-03 02:11:11 +08:00
|
|
|
const Type* depType = dependency->getOperand(0)->getType();
|
|
|
|
unsigned depPointerSize = TD.getTypeSize(depType);
|
2007-07-14 02:26:26 +08:00
|
|
|
|
2007-07-12 07:19:17 +08:00
|
|
|
// Check for aliasing
|
|
|
|
AliasAnalysis::AliasResult A = AA.alias(F->getPointerOperand(), ~0UL,
|
|
|
|
depPointer, depPointerSize);
|
|
|
|
|
|
|
|
if (A == AliasAnalysis::MustAlias) {
|
|
|
|
// Remove it!
|
|
|
|
MD.removeInstruction(dependency);
|
|
|
|
|
|
|
|
// DCE instructions only used to calculate that store
|
|
|
|
if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(0)))
|
|
|
|
possiblyDead.insert(D);
|
2007-07-14 02:26:26 +08:00
|
|
|
if (Instruction* D = dyn_cast<Instruction>(dependency->getOperand(1)))
|
|
|
|
possiblyDead.insert(D);
|
2007-07-12 07:19:17 +08:00
|
|
|
|
|
|
|
dependency->eraseFromParent();
|
|
|
|
NumFastStores++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-03 02:11:11 +08:00
|
|
|
/// handleEndBlock - Remove dead stores to stack-allocated locations in the
|
2007-08-09 01:50:09 +08:00
|
|
|
/// function end block. Ex:
|
|
|
|
/// %A = alloca i32
|
|
|
|
/// ...
|
|
|
|
/// store i32 1, i32* %A
|
|
|
|
/// ret void
|
2007-08-03 02:11:11 +08:00
|
|
|
bool DSE::handleEndBlock(BasicBlock& BB,
|
|
|
|
SetVector<Instruction*>& possiblyDead) {
|
2007-07-13 05:41:30 +08:00
|
|
|
TargetData &TD = getAnalysis<TargetData>();
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// Pointers alloca'd in this function are dead in the end block
|
2007-08-09 01:50:09 +08:00
|
|
|
SmallPtrSet<AllocaInst*, 64> deadPointers;
|
2007-07-13 05:41:30 +08:00
|
|
|
|
|
|
|
// Find all of the alloca'd pointers in the entry block
|
|
|
|
BasicBlock *Entry = BB.getParent()->begin();
|
|
|
|
for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
|
|
|
|
deadPointers.insert(AI);
|
|
|
|
|
|
|
|
// Scan the basic block backwards
|
|
|
|
for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
|
|
|
|
--BBI;
|
|
|
|
|
|
|
|
if (deadPointers.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If we find a store whose pointer is dead...
|
|
|
|
if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
|
2007-08-27 05:14:47 +08:00
|
|
|
if (!S->isVolatile()) {
|
|
|
|
Value* pointerOperand = S->getPointerOperand();
|
|
|
|
// See through pointer-to-pointer bitcasts
|
|
|
|
TranslatePointerBitCasts(pointerOperand);
|
2007-07-14 02:26:26 +08:00
|
|
|
|
2007-08-27 05:14:47 +08:00
|
|
|
if (deadPointers.count(pointerOperand)){
|
|
|
|
// Remove it!
|
|
|
|
MD.removeInstruction(S);
|
2007-07-13 05:41:30 +08:00
|
|
|
|
2007-08-27 05:14:47 +08:00
|
|
|
// DCE instructions only used to calculate that store
|
|
|
|
if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
|
|
|
|
possiblyDead.insert(D);
|
|
|
|
if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
|
|
|
|
possiblyDead.insert(D);
|
2007-07-13 05:41:30 +08:00
|
|
|
|
2007-08-27 05:14:47 +08:00
|
|
|
BBI++;
|
|
|
|
S->eraseFromParent();
|
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2007-07-13 05:41:30 +08:00
|
|
|
}
|
2007-08-09 01:50:09 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value* killPointer = 0;
|
2007-07-13 05:41:30 +08:00
|
|
|
|
|
|
|
// If we encounter a use of the pointer, it is no longer considered dead
|
2007-08-09 01:50:09 +08:00
|
|
|
if (LoadInst* L = dyn_cast<LoadInst>(BBI)) {
|
2007-07-13 05:41:30 +08:00
|
|
|
killPointer = L->getPointerOperand();
|
|
|
|
} else if (VAArgInst* V = dyn_cast<VAArgInst>(BBI)) {
|
|
|
|
killPointer = V->getOperand(0);
|
|
|
|
} else if (AllocaInst* A = dyn_cast<AllocaInst>(BBI)) {
|
|
|
|
deadPointers.erase(A);
|
|
|
|
continue;
|
|
|
|
} else if (CallSite::get(BBI).getInstruction() != 0) {
|
2007-08-09 01:58:56 +08:00
|
|
|
// If this call does not access memory, it can't
|
|
|
|
// be undeadifying any of our pointers.
|
|
|
|
CallSite CS = CallSite::get(BBI);
|
|
|
|
if (CS.getCalledFunction() &&
|
|
|
|
AA.doesNotAccessMemory(CS.getCalledFunction()))
|
|
|
|
continue;
|
|
|
|
|
2007-08-09 02:38:28 +08:00
|
|
|
unsigned modRef = 0;
|
|
|
|
unsigned other = 0;
|
|
|
|
|
2007-07-13 05:41:30 +08:00
|
|
|
// Remove any pointers made undead by the call from the dead set
|
|
|
|
std::vector<Instruction*> dead;
|
2007-08-09 01:50:09 +08:00
|
|
|
for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
|
2007-07-13 05:41:30 +08:00
|
|
|
E = deadPointers.end(); I != E; ++I) {
|
2007-08-09 02:38:28 +08:00
|
|
|
// HACK: if we detect that our AA is imprecise, it's not
|
|
|
|
// worth it to scan the rest of the deadPointers set. Just
|
|
|
|
// assume that the AA will return ModRef for everything, and
|
|
|
|
// go ahead and bail.
|
|
|
|
if (modRef >= 16 && other == 0) {
|
|
|
|
deadPointers.clear();
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2007-07-13 05:41:30 +08:00
|
|
|
// Get size information for the alloca
|
|
|
|
unsigned pointerSize = ~0UL;
|
|
|
|
if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
|
2007-08-03 02:11:11 +08:00
|
|
|
pointerSize = C->getZExtValue() * \
|
|
|
|
TD.getTypeSize((*I)->getAllocatedType());
|
2007-07-13 05:41:30 +08:00
|
|
|
|
|
|
|
// See if the call site touches it
|
2007-08-09 01:58:56 +08:00
|
|
|
AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I, pointerSize);
|
2007-08-09 02:38:28 +08:00
|
|
|
|
|
|
|
if (A == AliasAnalysis::ModRef)
|
|
|
|
modRef++;
|
|
|
|
else
|
|
|
|
other++;
|
|
|
|
|
2007-07-14 02:26:26 +08:00
|
|
|
if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
|
2007-07-13 05:41:30 +08:00
|
|
|
dead.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<Instruction*>::iterator I = dead.begin(), E = dead.end();
|
|
|
|
I != E; ++I)
|
|
|
|
deadPointers.erase(*I);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!killPointer)
|
|
|
|
continue;
|
|
|
|
|
2007-08-09 02:38:28 +08:00
|
|
|
TranslatePointerBitCasts(killPointer);
|
|
|
|
|
2007-07-13 05:41:30 +08:00
|
|
|
// Deal with undead pointers
|
2007-08-09 01:50:09 +08:00
|
|
|
MadeChange |= RemoveUndeadPointers(killPointer, BBI,
|
2007-07-13 05:41:30 +08:00
|
|
|
deadPointers, possiblyDead);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2007-08-09 02:38:28 +08:00
|
|
|
/// RemoveUndeadPointers - check for uses of a pointer that make it
|
|
|
|
/// undead when scanning for dead stores to alloca's.
|
2007-08-09 01:50:09 +08:00
|
|
|
bool DSE::RemoveUndeadPointers(Value* killPointer,
|
2007-07-13 05:41:30 +08:00
|
|
|
BasicBlock::iterator& BBI,
|
2007-08-09 01:50:09 +08:00
|
|
|
SmallPtrSet<AllocaInst*, 64>& deadPointers,
|
2007-07-13 05:41:30 +08:00
|
|
|
SetVector<Instruction*>& possiblyDead) {
|
|
|
|
TargetData &TD = getAnalysis<TargetData>();
|
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
|
|
|
|
|
2007-08-09 02:38:28 +08:00
|
|
|
// If the kill pointer can be easily reduced to an alloca,
|
|
|
|
// don't bother doing extraneous AA queries
|
|
|
|
if (AllocaInst* A = dyn_cast<AllocaInst>(killPointer)) {
|
|
|
|
if (deadPointers.count(A))
|
|
|
|
deadPointers.erase(A);
|
|
|
|
return false;
|
2007-08-09 03:12:31 +08:00
|
|
|
} else if (isa<GlobalValue>(killPointer)) {
|
|
|
|
// A global can't be in the dead pointer set
|
|
|
|
return false;
|
2007-08-09 02:38:28 +08:00
|
|
|
}
|
|
|
|
|
2007-07-13 05:41:30 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
std::vector<Instruction*> undead;
|
|
|
|
|
2007-08-09 01:50:09 +08:00
|
|
|
for (SmallPtrSet<AllocaInst*, 64>::iterator I = deadPointers.begin(),
|
2007-07-13 05:41:30 +08:00
|
|
|
E = deadPointers.end(); I != E; ++I) {
|
|
|
|
// Get size information for the alloca
|
|
|
|
unsigned pointerSize = ~0UL;
|
|
|
|
if (ConstantInt* C = dyn_cast<ConstantInt>((*I)->getArraySize()))
|
2007-08-03 02:11:11 +08:00
|
|
|
pointerSize = C->getZExtValue() * \
|
|
|
|
TD.getTypeSize((*I)->getAllocatedType());
|
2007-07-13 05:41:30 +08:00
|
|
|
|
|
|
|
// See if this pointer could alias it
|
2007-08-03 02:11:11 +08:00
|
|
|
AliasAnalysis::AliasResult A = AA.alias(*I, pointerSize,
|
2007-08-09 01:50:09 +08:00
|
|
|
killPointer, ~0UL);
|
2007-07-13 05:41:30 +08:00
|
|
|
|
|
|
|
// If it must-alias and a store, we can delete it
|
|
|
|
if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
|
|
|
|
StoreInst* S = cast<StoreInst>(BBI);
|
|
|
|
|
|
|
|
// Remove it!
|
|
|
|
MD.removeInstruction(S);
|
|
|
|
|
|
|
|
// DCE instructions only used to calculate that store
|
|
|
|
if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
|
|
|
|
possiblyDead.insert(D);
|
2007-07-14 02:26:26 +08:00
|
|
|
if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
|
|
|
|
possiblyDead.insert(D);
|
2007-07-13 05:41:30 +08:00
|
|
|
|
|
|
|
BBI++;
|
|
|
|
S->eraseFromParent();
|
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Otherwise, it is undead
|
|
|
|
} else if (A != AliasAnalysis::NoAlias)
|
|
|
|
undead.push_back(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<Instruction*>::iterator I = undead.begin(), E = undead.end();
|
|
|
|
I != E; ++I)
|
|
|
|
deadPointers.erase(*I);
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2007-08-09 01:50:09 +08:00
|
|
|
/// DeleteDeadInstructionChains - takes an instruction and a setvector of
|
|
|
|
/// dead instructions. If I is dead, it is erased, and its operands are
|
|
|
|
/// checked for deadness. If they are dead, they are added to the dead
|
|
|
|
/// setvector.
|
2007-08-01 14:36:51 +08:00
|
|
|
void DSE::DeleteDeadInstructionChains(Instruction *I,
|
2007-07-11 08:46:18 +08:00
|
|
|
SetVector<Instruction*> &DeadInsts) {
|
|
|
|
// Instruction must be dead.
|
|
|
|
if (!I->use_empty() || !isInstructionTriviallyDead(I)) return;
|
|
|
|
|
|
|
|
// Let the memory dependence know
|
|
|
|
getAnalysis<MemoryDependenceAnalysis>().removeInstruction(I);
|
|
|
|
|
|
|
|
// See if this made any operands dead. We do it this way in case the
|
|
|
|
// instruction uses the same operand twice. We don't want to delete a
|
|
|
|
// value then reference it.
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
|
2007-07-12 03:03:09 +08:00
|
|
|
if (I->getOperand(i)->hasOneUse())
|
|
|
|
if (Instruction* Op = dyn_cast<Instruction>(I->getOperand(i)))
|
|
|
|
DeadInsts.insert(Op); // Attempt to nuke it later.
|
|
|
|
|
2007-07-11 08:46:18 +08:00
|
|
|
I->setOperand(i, 0); // Drop from the operand list.
|
|
|
|
}
|
|
|
|
|
|
|
|
I->eraseFromParent();
|
|
|
|
++NumFastOther;
|
|
|
|
}
|