2007-08-02 18:11:11 +00:00
|
|
|
//===- DeadStoreElimination.cpp - Fast Dead Store Elimination -------------===//
|
2007-07-11 00:46:18 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 00:46:18 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 06:36:51 +00:00
|
|
|
#define DEBUG_TYPE "dse"
|
2007-07-11 00:46:18 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2007-07-12 21:41:30 +00:00
|
|
|
#include "llvm/Constants.h"
|
2007-07-11 00:46:18 +00:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2008-01-29 06:18:36 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2007-07-11 00:46:18 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-07-11 23:19:17 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2008-07-28 16:14:26 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2009-10-27 20:05:49 +00:00
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2007-07-11 00:46:18 +00:00
|
|
|
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
|
2007-07-11 23:19:17 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-07-11 00:46:18 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumFastStores, "Number of stores deleted");
|
|
|
|
STATISTIC(NumFastOther , "Number of other instrs removed");
|
|
|
|
|
|
|
|
namespace {
|
2009-09-02 06:11:42 +00:00
|
|
|
struct DSE : public FunctionPass {
|
2009-07-24 18:13:53 +00:00
|
|
|
TargetData *TD;
|
|
|
|
|
2007-07-11 00:46:18 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
DSE() : FunctionPass(&ID) {}
|
2007-07-11 00:46:18 +00:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
2010-02-11 05:11:54 +00:00
|
|
|
|
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
|
|
|
|
2007-07-11 00:46:18 +00:00
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
2010-02-11 05:11:54 +00:00
|
|
|
// Only check non-dead blocks. Dead blocks may have strange pointer
|
|
|
|
// cycles that will confuse alias analysis.
|
|
|
|
if (DT.isReachableFromEntry(I))
|
|
|
|
Changed |= runOnBasicBlock(*I);
|
2007-07-11 00:46:18 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
2008-11-29 01:43:36 +00:00
|
|
|
|
2007-07-11 00:46:18 +00:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB);
|
2009-10-24 04:23:03 +00:00
|
|
|
bool handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep);
|
2008-11-28 00:27:14 +00:00
|
|
|
bool handleEndBlock(BasicBlock &BB);
|
2010-01-03 04:39:07 +00:00
|
|
|
bool RemoveUndeadPointers(Value *Ptr, uint64_t killPointerSize,
|
|
|
|
BasicBlock::iterator &BBI,
|
|
|
|
SmallPtrSet<Value*, 64> &deadPointers);
|
2008-11-28 00:27:14 +00:00
|
|
|
void DeleteDeadInstruction(Instruction *I,
|
|
|
|
SmallPtrSet<Value*, 64> *deadPointers = 0);
|
|
|
|
|
2007-07-11 00:46:18 +00:00
|
|
|
|
|
|
|
// getAnalysisUsage - We require post dominance frontiers (aka Control
|
|
|
|
// Dependence Graph)
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
2008-07-28 16:14:26 +00:00
|
|
|
AU.addRequired<DominatorTree>();
|
2007-07-11 23:19:17 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2007-07-11 00:46:18 +00:00
|
|
|
AU.addRequired<MemoryDependenceAnalysis>();
|
2008-07-28 16:14:26 +00:00
|
|
|
AU.addPreserved<DominatorTree>();
|
2007-07-11 23:19:17 +00:00
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2007-07-11 00:46:18 +00:00
|
|
|
AU.addPreserved<MemoryDependenceAnalysis>();
|
|
|
|
}
|
2010-01-03 04:39:07 +00:00
|
|
|
|
|
|
|
unsigned getPointerSize(Value *V) const;
|
2007-07-11 00:46:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char DSE::ID = 0;
|
|
|
|
static RegisterPass<DSE> X("dse", "Dead Store Elimination");
|
|
|
|
|
2007-08-01 06:36:51 +00:00
|
|
|
FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
|
2007-07-11 00:46:18 +00:00
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
/// doesClobberMemory - Does this instruction clobber (write without reading)
|
|
|
|
/// some memory?
|
|
|
|
static bool doesClobberMemory(Instruction *I) {
|
|
|
|
if (isa<StoreInst>(I))
|
|
|
|
return true;
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
|
|
|
|
switch (II->getIntrinsicID()) {
|
2009-12-02 06:35:55 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case Intrinsic::memset:
|
|
|
|
case Intrinsic::memmove:
|
|
|
|
case Intrinsic::memcpy:
|
|
|
|
case Intrinsic::init_trampoline:
|
|
|
|
case Intrinsic::lifetime_end:
|
|
|
|
return true;
|
2009-11-10 06:46:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-10 13:49:50 +00:00
|
|
|
/// isElidable - If the value of this instruction and the memory it writes to is
|
2009-11-10 06:46:40 +00:00
|
|
|
/// unused, may we delete this instrtction?
|
|
|
|
static bool isElidable(Instruction *I) {
|
|
|
|
assert(doesClobberMemory(I));
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
|
|
|
|
return II->getIntrinsicID() != Intrinsic::lifetime_end;
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
|
|
|
return !SI->isVolatile();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getPointerOperand - Return the pointer that is being clobbered.
|
|
|
|
static Value *getPointerOperand(Instruction *I) {
|
|
|
|
assert(doesClobberMemory(I));
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
|
|
|
return SI->getPointerOperand();
|
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
|
|
|
|
return MI->getOperand(1);
|
2009-12-02 06:35:55 +00:00
|
|
|
|
|
|
|
switch (cast<IntrinsicInst>(I)->getIntrinsicID()) {
|
|
|
|
default: assert(false && "Unexpected intrinsic!");
|
|
|
|
case Intrinsic::init_trampoline:
|
|
|
|
return I->getOperand(1);
|
|
|
|
case Intrinsic::lifetime_end:
|
|
|
|
return I->getOperand(2);
|
2009-11-10 13:49:50 +00:00
|
|
|
}
|
2009-11-10 06:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getStoreSize - Return the length in bytes of the write by the clobbering
|
|
|
|
/// instruction. If variable or unknown, returns -1.
|
|
|
|
static unsigned getStoreSize(Instruction *I, const TargetData *TD) {
|
|
|
|
assert(doesClobberMemory(I));
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
|
|
|
if (!TD) return -1u;
|
2009-11-10 07:00:43 +00:00
|
|
|
return TD->getTypeStoreSize(SI->getOperand(0)->getType());
|
2009-11-10 06:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *Len;
|
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
|
|
|
|
Len = MI->getLength();
|
|
|
|
} else {
|
2009-12-02 06:35:55 +00:00
|
|
|
switch (cast<IntrinsicInst>(I)->getIntrinsicID()) {
|
|
|
|
default: assert(false && "Unexpected intrinsic!");
|
|
|
|
case Intrinsic::init_trampoline:
|
|
|
|
return -1u;
|
|
|
|
case Intrinsic::lifetime_end:
|
|
|
|
Len = I->getOperand(1);
|
|
|
|
break;
|
2009-11-10 13:49:50 +00:00
|
|
|
}
|
2009-11-10 06:46:40 +00:00
|
|
|
}
|
|
|
|
if (ConstantInt *LenCI = dyn_cast<ConstantInt>(Len))
|
|
|
|
if (!LenCI->isAllOnesValue())
|
|
|
|
return LenCI->getZExtValue();
|
|
|
|
return -1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isStoreAtLeastAsWideAs - Return true if the size of the store in I1 is
|
|
|
|
/// greater than or equal to the store in I2. This returns false if we don't
|
|
|
|
/// know.
|
2009-11-04 23:20:12 +00:00
|
|
|
///
|
2009-11-10 06:46:40 +00:00
|
|
|
static bool isStoreAtLeastAsWideAs(Instruction *I1, Instruction *I2,
|
|
|
|
const TargetData *TD) {
|
|
|
|
const Type *I1Ty = getPointerOperand(I1)->getType();
|
|
|
|
const Type *I2Ty = getPointerOperand(I2)->getType();
|
2009-11-04 23:20:12 +00:00
|
|
|
|
|
|
|
// Exactly the same type, must have exactly the same size.
|
2009-11-10 06:46:40 +00:00
|
|
|
if (I1Ty == I2Ty) return true;
|
2009-11-04 23:20:12 +00:00
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
int I1Size = getStoreSize(I1, TD);
|
|
|
|
int I2Size = getStoreSize(I2, TD);
|
2009-11-04 23:20:12 +00:00
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
return I1Size != -1 && I2Size != -1 && I1Size >= I2Size;
|
2009-11-04 23:20:12 +00:00
|
|
|
}
|
|
|
|
|
2007-08-01 06:36:51 +00:00
|
|
|
bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
2010-01-03 04:39:07 +00:00
|
|
|
MemoryDependenceAnalysis &MD = getAnalysis<MemoryDependenceAnalysis>();
|
2009-07-24 18:13:53 +00:00
|
|
|
TD = getAnalysisIfAvailable<TargetData>();
|
2007-11-01 05:29:16 +00:00
|
|
|
|
2007-07-11 00:46:18 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2009-09-02 06:31:02 +00:00
|
|
|
// Do a top-down walk on the BB.
|
2008-11-28 21:29:52 +00:00
|
|
|
for (BasicBlock::iterator BBI = BB.begin(), BBE = BB.end(); BBI != BBE; ) {
|
|
|
|
Instruction *Inst = BBI++;
|
|
|
|
|
2009-07-24 18:13:53 +00:00
|
|
|
// If we find a store or a free, get its memory dependence.
|
2009-11-10 06:46:40 +00:00
|
|
|
if (!doesClobberMemory(Inst) && !isFreeCall(Inst))
|
2007-08-08 04:52:29 +00:00
|
|
|
continue;
|
2008-12-07 00:25:15 +00:00
|
|
|
|
2008-12-06 00:53:22 +00:00
|
|
|
MemDepResult InstDep = MD.getDependency(Inst);
|
|
|
|
|
|
|
|
// Ignore non-local stores.
|
|
|
|
// FIXME: cross-block DSE would be fun. :)
|
|
|
|
if (InstDep.isNonLocal()) continue;
|
|
|
|
|
|
|
|
// Handle frees whose dependencies are non-trivial.
|
2009-10-26 23:43:48 +00:00
|
|
|
if (isFreeCall(Inst)) {
|
2009-10-24 04:23:03 +00:00
|
|
|
MadeChange |= handleFreeWithNonTrivialDependency(Inst, InstDep);
|
2008-12-06 00:53:22 +00:00
|
|
|
continue;
|
2008-10-01 15:25:41 +00:00
|
|
|
}
|
2008-11-28 21:29:52 +00:00
|
|
|
|
2008-12-06 00:53:22 +00:00
|
|
|
// If not a definite must-alias dependency, ignore it.
|
|
|
|
if (!InstDep.isDef())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this is a store-store dependence, then the previous store is dead so
|
|
|
|
// long as this store is at least as big as it.
|
2009-11-10 06:46:40 +00:00
|
|
|
if (doesClobberMemory(InstDep.getInst())) {
|
|
|
|
Instruction *DepStore = InstDep.getInst();
|
|
|
|
if (isStoreAtLeastAsWideAs(Inst, DepStore, TD) &&
|
|
|
|
isElidable(DepStore)) {
|
2008-11-28 00:27:14 +00:00
|
|
|
// Delete the store and now-dead instructions that feed it.
|
2008-12-06 00:53:22 +00:00
|
|
|
DeleteDeadInstruction(DepStore);
|
2007-08-08 04:52:29 +00:00
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
2009-09-02 06:31:02 +00:00
|
|
|
|
|
|
|
// DeleteDeadInstruction can delete the current instruction in loop
|
|
|
|
// cases, reset BBI.
|
|
|
|
BBI = Inst;
|
2008-11-29 20:29:04 +00:00
|
|
|
if (BBI != BB.begin())
|
2008-11-28 22:50:08 +00:00
|
|
|
--BBI;
|
2008-11-28 21:29:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-10 06:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isElidable(Inst))
|
|
|
|
continue;
|
2007-08-08 04:52:29 +00:00
|
|
|
|
2008-12-06 00:53:22 +00:00
|
|
|
// If we're storing the same value back to a pointer that we just
|
|
|
|
// loaded from, then the store can be removed.
|
2009-11-10 06:46:40 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
|
|
|
if (LoadInst *DepLoad = dyn_cast<LoadInst>(InstDep.getInst())) {
|
|
|
|
if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
|
|
|
|
SI->getOperand(0) == DepLoad) {
|
|
|
|
// DeleteDeadInstruction can delete the current instruction. Save BBI
|
|
|
|
// in case we need it.
|
|
|
|
WeakVH NextInst(BBI);
|
|
|
|
|
|
|
|
DeleteDeadInstruction(SI);
|
|
|
|
|
|
|
|
if (NextInst == 0) // Next instruction deleted.
|
|
|
|
BBI = BB.begin();
|
|
|
|
else if (BBI != BB.begin()) // Revisit this instruction if possible.
|
|
|
|
--BBI;
|
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-28 07:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a lifetime end marker, we can throw away the store.
|
2009-11-10 06:46:40 +00:00
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(InstDep.getInst())) {
|
2009-10-28 07:05:35 +00:00
|
|
|
if (II->getIntrinsicID() == Intrinsic::lifetime_end) {
|
|
|
|
// Delete the store and now-dead instructions that feed it.
|
|
|
|
// DeleteDeadInstruction can delete the current instruction. Save BBI
|
2009-09-02 06:31:02 +00:00
|
|
|
// in case we need it.
|
|
|
|
WeakVH NextInst(BBI);
|
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
DeleteDeadInstruction(Inst);
|
2009-09-02 06:31:02 +00:00
|
|
|
|
|
|
|
if (NextInst == 0) // Next instruction deleted.
|
|
|
|
BBI = BB.begin();
|
|
|
|
else if (BBI != BB.begin()) // Revisit this instruction if possible.
|
2008-12-06 00:53:22 +00:00
|
|
|
--BBI;
|
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
2008-12-05 21:04:20 +00:00
|
|
|
}
|
2007-07-11 00:46:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-28 21:29:52 +00:00
|
|
|
// If this block ends in a return, unwind, or unreachable, all allocas are
|
|
|
|
// dead at its end, which means stores to them are also dead.
|
2007-07-12 21:41:30 +00:00
|
|
|
if (BB.getTerminator()->getNumSuccessors() == 0)
|
2008-11-28 00:27:14 +00:00
|
|
|
MadeChange |= handleEndBlock(BB);
|
2007-07-11 00:46:18 +00:00
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2007-07-11 23:19:17 +00:00
|
|
|
/// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
|
2008-11-28 00:27:14 +00:00
|
|
|
/// dependency is a store to a field of that structure.
|
2009-10-24 04:23:03 +00:00
|
|
|
bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) {
|
2007-07-11 23:19:17 +00:00
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
Instruction *Dependency = Dep.getInst();
|
|
|
|
if (!Dependency || !doesClobberMemory(Dependency) || !isElidable(Dependency))
|
2007-08-26 21:14:47 +00:00
|
|
|
return false;
|
2007-07-12 18:08:51 +00:00
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject();
|
2008-01-20 10:49:23 +00:00
|
|
|
|
2008-12-06 00:53:22 +00:00
|
|
|
// Check for aliasing.
|
2009-10-26 23:43:48 +00:00
|
|
|
if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
|
2008-12-06 00:53:22 +00:00
|
|
|
AliasAnalysis::MustAlias)
|
2008-11-28 00:27:14 +00:00
|
|
|
return false;
|
2007-07-11 23:19:17 +00:00
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
// DCE instructions only used to calculate that store
|
2008-12-06 00:53:22 +00:00
|
|
|
DeleteDeadInstruction(Dependency);
|
2008-11-28 00:27:14 +00:00
|
|
|
NumFastStores++;
|
|
|
|
return true;
|
2007-07-11 23:19:17 +00:00
|
|
|
}
|
|
|
|
|
2007-08-02 18:11:11 +00:00
|
|
|
/// handleEndBlock - Remove dead stores to stack-allocated locations in the
|
2007-08-08 17:50:09 +00:00
|
|
|
/// function end block. Ex:
|
|
|
|
/// %A = alloca i32
|
|
|
|
/// ...
|
|
|
|
/// store i32 1, i32* %A
|
|
|
|
/// ret void
|
2008-11-28 00:27:14 +00:00
|
|
|
bool DSE::handleEndBlock(BasicBlock &BB) {
|
2007-07-12 21:41:30 +00:00
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// Pointers alloca'd in this function are dead in the end block
|
2008-01-29 06:18:36 +00:00
|
|
|
SmallPtrSet<Value*, 64> deadPointers;
|
2007-07-12 21:41:30 +00:00
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
// Find all of the alloca'd pointers in the entry block.
|
2007-07-12 21:41:30 +00:00
|
|
|
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);
|
2008-11-28 00:27:14 +00:00
|
|
|
|
|
|
|
// Treat byval arguments the same, stores to them are dead at the end of the
|
|
|
|
// function.
|
2008-01-29 06:18:36 +00:00
|
|
|
for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
|
|
|
|
AE = BB.getParent()->arg_end(); AI != AE; ++AI)
|
|
|
|
if (AI->hasByValAttr())
|
|
|
|
deadPointers.insert(AI);
|
2007-07-12 21:41:30 +00:00
|
|
|
|
|
|
|
// Scan the basic block backwards
|
|
|
|
for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
|
|
|
|
--BBI;
|
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
// If we find a store whose pointer is dead.
|
2009-11-10 06:46:40 +00:00
|
|
|
if (doesClobberMemory(BBI)) {
|
|
|
|
if (isElidable(BBI)) {
|
2007-08-26 21:14:47 +00:00
|
|
|
// See through pointer-to-pointer bitcasts
|
2009-11-10 06:46:40 +00:00
|
|
|
Value *pointerOperand = getPointerOperand(BBI)->getUnderlyingObject();
|
2008-10-01 15:25:41 +00:00
|
|
|
|
2008-01-25 10:10:33 +00:00
|
|
|
// Alloca'd pointers or byval arguments (which are functionally like
|
|
|
|
// alloca's) are valid candidates for removal.
|
2008-01-29 06:18:36 +00:00
|
|
|
if (deadPointers.count(pointerOperand)) {
|
2008-11-28 00:27:14 +00:00
|
|
|
// DCE instructions only used to calculate that store.
|
2009-11-10 06:46:40 +00:00
|
|
|
Instruction *Dead = BBI;
|
2007-08-26 21:14:47 +00:00
|
|
|
BBI++;
|
2009-11-10 06:46:40 +00:00
|
|
|
DeleteDeadInstruction(Dead, &deadPointers);
|
2007-08-26 21:14:47 +00:00
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
2009-11-10 06:46:40 +00:00
|
|
|
continue;
|
2007-08-26 21:14:47 +00:00
|
|
|
}
|
2007-07-12 21:41:30 +00:00
|
|
|
}
|
2007-08-08 17:50:09 +00:00
|
|
|
|
2009-11-10 06:46:40 +00:00
|
|
|
// Because a memcpy or memmove is also a load, we can't skip it if we
|
|
|
|
// didn't remove it.
|
|
|
|
if (!isa<MemTransferInst>(BBI))
|
2008-01-29 06:18:36 +00:00
|
|
|
continue;
|
2007-08-08 17:50:09 +00:00
|
|
|
}
|
|
|
|
|
2010-01-03 04:39:07 +00:00
|
|
|
Value *killPointer = 0;
|
2008-02-04 04:53:00 +00:00
|
|
|
uint64_t killPointerSize = ~0UL;
|
2007-07-12 21:41:30 +00:00
|
|
|
|
|
|
|
// If we encounter a use of the pointer, it is no longer considered dead
|
2008-11-28 00:27:14 +00:00
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
|
2008-05-13 01:48:26 +00:00
|
|
|
// However, if this load is unused and not volatile, we can go ahead and
|
|
|
|
// remove it, and not have to worry about it making our pointer undead!
|
2008-04-28 19:51:27 +00:00
|
|
|
if (L->use_empty() && !L->isVolatile()) {
|
2008-01-30 01:24:47 +00:00
|
|
|
BBI++;
|
2008-11-28 00:27:14 +00:00
|
|
|
DeleteDeadInstruction(L, &deadPointers);
|
2008-01-30 01:24:47 +00:00
|
|
|
NumFastOther++;
|
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:41:30 +00:00
|
|
|
killPointer = L->getPointerOperand();
|
2010-01-03 04:39:07 +00:00
|
|
|
} else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
|
2007-07-12 21:41:30 +00:00
|
|
|
killPointer = V->getOperand(0);
|
2009-11-10 06:46:40 +00:00
|
|
|
} else if (isa<MemTransferInst>(BBI) &&
|
|
|
|
isa<ConstantInt>(cast<MemTransferInst>(BBI)->getLength())) {
|
|
|
|
killPointer = cast<MemTransferInst>(BBI)->getSource();
|
2008-02-04 04:53:00 +00:00
|
|
|
killPointerSize = cast<ConstantInt>(
|
2009-11-10 06:46:40 +00:00
|
|
|
cast<MemTransferInst>(BBI)->getLength())->getZExtValue();
|
2010-01-03 04:39:07 +00:00
|
|
|
} else if (AllocaInst *A = dyn_cast<AllocaInst>(BBI)) {
|
2007-07-12 21:41:30 +00:00
|
|
|
deadPointers.erase(A);
|
2008-01-30 01:24:47 +00:00
|
|
|
|
|
|
|
// Dead alloca's can be DCE'd when we reach them
|
2008-01-30 08:01:28 +00:00
|
|
|
if (A->use_empty()) {
|
2008-01-30 01:24:47 +00:00
|
|
|
BBI++;
|
2008-11-28 00:27:14 +00:00
|
|
|
DeleteDeadInstruction(A, &deadPointers);
|
2008-01-30 01:24:47 +00:00
|
|
|
NumFastOther++;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
2007-07-12 21:41:30 +00:00
|
|
|
continue;
|
|
|
|
} else if (CallSite::get(BBI).getInstruction() != 0) {
|
2007-08-08 17:58:56 +00:00
|
|
|
// If this call does not access memory, it can't
|
|
|
|
// be undeadifying any of our pointers.
|
|
|
|
CallSite CS = CallSite::get(BBI);
|
2007-12-01 07:51:45 +00:00
|
|
|
if (AA.doesNotAccessMemory(CS))
|
2007-08-08 17:58:56 +00:00
|
|
|
continue;
|
|
|
|
|
2007-08-08 18:38:28 +00:00
|
|
|
unsigned modRef = 0;
|
|
|
|
unsigned other = 0;
|
|
|
|
|
2007-07-12 21:41:30 +00:00
|
|
|
// Remove any pointers made undead by the call from the dead set
|
2008-01-29 06:18:36 +00:00
|
|
|
std::vector<Value*> dead;
|
|
|
|
for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
|
2007-07-12 21:41:30 +00:00
|
|
|
E = deadPointers.end(); I != E; ++I) {
|
2007-08-08 18:38:28 +00: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;
|
|
|
|
}
|
2010-01-03 04:39:07 +00:00
|
|
|
|
2007-07-12 21:41:30 +00:00
|
|
|
// See if the call site touches it
|
2010-01-03 04:39:07 +00:00
|
|
|
AliasAnalysis::ModRefResult A = AA.getModRefInfo(CS, *I,
|
|
|
|
getPointerSize(*I));
|
2007-08-08 18:38:28 +00:00
|
|
|
|
|
|
|
if (A == AliasAnalysis::ModRef)
|
|
|
|
modRef++;
|
|
|
|
else
|
|
|
|
other++;
|
|
|
|
|
2007-07-13 18:26:26 +00:00
|
|
|
if (A == AliasAnalysis::ModRef || A == AliasAnalysis::Ref)
|
2007-07-12 21:41:30 +00:00
|
|
|
dead.push_back(*I);
|
|
|
|
}
|
|
|
|
|
2008-01-29 06:18:36 +00:00
|
|
|
for (std::vector<Value*>::iterator I = dead.begin(), E = dead.end();
|
2007-07-12 21:41:30 +00:00
|
|
|
I != E; ++I)
|
2008-01-29 06:18:36 +00:00
|
|
|
deadPointers.erase(*I);
|
2007-07-12 21:41:30 +00:00
|
|
|
|
|
|
|
continue;
|
2008-11-28 00:27:14 +00:00
|
|
|
} else if (isInstructionTriviallyDead(BBI)) {
|
2008-01-30 01:24:47 +00:00
|
|
|
// For any non-memory-affecting non-terminators, DCE them as we reach them
|
2008-11-28 00:27:14 +00:00
|
|
|
Instruction *Inst = BBI;
|
|
|
|
BBI++;
|
|
|
|
DeleteDeadInstruction(Inst, &deadPointers);
|
|
|
|
NumFastOther++;
|
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
2007-07-12 21:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!killPointer)
|
|
|
|
continue;
|
2008-10-01 15:25:41 +00:00
|
|
|
|
|
|
|
killPointer = killPointer->getUnderlyingObject();
|
|
|
|
|
2007-07-12 21:41:30 +00:00
|
|
|
// Deal with undead pointers
|
2008-02-04 04:53:00 +00:00
|
|
|
MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
|
2008-11-28 00:27:14 +00:00
|
|
|
deadPointers);
|
2007-07-12 21:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2007-08-08 18:38:28 +00:00
|
|
|
/// RemoveUndeadPointers - check for uses of a pointer that make it
|
|
|
|
/// undead when scanning for dead stores to alloca's.
|
2010-01-03 04:39:07 +00:00
|
|
|
bool DSE::RemoveUndeadPointers(Value *killPointer, uint64_t killPointerSize,
|
2008-11-28 00:27:14 +00:00
|
|
|
BasicBlock::iterator &BBI,
|
2010-01-03 04:39:07 +00:00
|
|
|
SmallPtrSet<Value*, 64> &deadPointers) {
|
2007-07-12 21:41:30 +00:00
|
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
2010-01-03 04:39:07 +00:00
|
|
|
|
2007-08-08 18:38:28 +00:00
|
|
|
// If the kill pointer can be easily reduced to an alloca,
|
2008-11-28 00:27:14 +00:00
|
|
|
// don't bother doing extraneous AA queries.
|
2008-01-29 06:18:36 +00:00
|
|
|
if (deadPointers.count(killPointer)) {
|
|
|
|
deadPointers.erase(killPointer);
|
2007-08-08 18:38:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
// A global can't be in the dead pointer set.
|
|
|
|
if (isa<GlobalValue>(killPointer))
|
|
|
|
return false;
|
|
|
|
|
2007-07-12 21:41:30 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
SmallVector<Value*, 16> undead;
|
2010-01-03 04:39:07 +00:00
|
|
|
|
2008-01-29 06:18:36 +00:00
|
|
|
for (SmallPtrSet<Value*, 64>::iterator I = deadPointers.begin(),
|
2010-01-03 04:39:07 +00:00
|
|
|
E = deadPointers.end(); I != E; ++I) {
|
2007-07-12 21:41:30 +00:00
|
|
|
// See if this pointer could alias it
|
2010-01-03 04:39:07 +00:00
|
|
|
AliasAnalysis::AliasResult A = AA.alias(*I, getPointerSize(*I),
|
2008-02-04 04:53:00 +00:00
|
|
|
killPointer, killPointerSize);
|
2007-07-12 21:41:30 +00:00
|
|
|
|
|
|
|
// If it must-alias and a store, we can delete it
|
|
|
|
if (isa<StoreInst>(BBI) && A == AliasAnalysis::MustAlias) {
|
2010-01-03 04:39:07 +00:00
|
|
|
StoreInst *S = cast<StoreInst>(BBI);
|
2007-07-12 21:41:30 +00:00
|
|
|
|
|
|
|
// Remove it!
|
2010-01-03 04:39:07 +00:00
|
|
|
++BBI;
|
2008-11-28 00:27:14 +00:00
|
|
|
DeleteDeadInstruction(S, &deadPointers);
|
2007-07-12 21:41:30 +00:00
|
|
|
NumFastStores++;
|
|
|
|
MadeChange = true;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Otherwise, it is undead
|
2008-11-28 00:27:14 +00:00
|
|
|
} else if (A != AliasAnalysis::NoAlias)
|
|
|
|
undead.push_back(*I);
|
2007-07-12 21:41:30 +00:00
|
|
|
}
|
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
for (SmallVector<Value*, 16>::iterator I = undead.begin(), E = undead.end();
|
2007-07-12 21:41:30 +00:00
|
|
|
I != E; ++I)
|
2008-01-29 06:18:36 +00:00
|
|
|
deadPointers.erase(*I);
|
2007-07-12 21:41:30 +00:00
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
|
|
|
|
/// and zero out all the operands of this instruction. If any of them become
|
|
|
|
/// dead, delete them and the computation tree that feeds them.
|
|
|
|
///
|
|
|
|
/// If ValueSet is non-null, remove any deleted instructions from it as well.
|
|
|
|
///
|
|
|
|
void DSE::DeleteDeadInstruction(Instruction *I,
|
|
|
|
SmallPtrSet<Value*, 64> *ValueSet) {
|
|
|
|
SmallVector<Instruction*, 32> NowDeadInsts;
|
|
|
|
|
|
|
|
NowDeadInsts.push_back(I);
|
|
|
|
--NumFastOther;
|
|
|
|
|
|
|
|
// Before we touch this instruction, remove it from memdep!
|
|
|
|
MemoryDependenceAnalysis &MDA = getAnalysis<MemoryDependenceAnalysis>();
|
2010-01-05 16:27:25 +00:00
|
|
|
do {
|
|
|
|
Instruction *DeadInst = NowDeadInsts.pop_back_val();
|
2008-11-28 00:27:14 +00:00
|
|
|
|
|
|
|
++NumFastOther;
|
|
|
|
|
|
|
|
// This instruction is dead, zap it, in stages. Start by removing it from
|
|
|
|
// MemDep, which needs to know the operands and needs it to be in the
|
|
|
|
// function.
|
|
|
|
MDA.removeInstruction(DeadInst);
|
2007-07-11 19:03:09 +00:00
|
|
|
|
2008-11-28 00:27:14 +00:00
|
|
|
for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
|
|
|
|
Value *Op = DeadInst->getOperand(op);
|
|
|
|
DeadInst->setOperand(op, 0);
|
|
|
|
|
|
|
|
// If this operand just became dead, add it to the NowDeadInsts list.
|
|
|
|
if (!Op->use_empty()) continue;
|
|
|
|
|
|
|
|
if (Instruction *OpI = dyn_cast<Instruction>(Op))
|
|
|
|
if (isInstructionTriviallyDead(OpI))
|
|
|
|
NowDeadInsts.push_back(OpI);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeadInst->eraseFromParent();
|
|
|
|
|
|
|
|
if (ValueSet) ValueSet->erase(DeadInst);
|
2010-01-05 16:27:25 +00:00
|
|
|
} while (!NowDeadInsts.empty());
|
2007-07-11 00:46:18 +00:00
|
|
|
}
|
2010-01-03 04:39:07 +00:00
|
|
|
|
|
|
|
unsigned DSE::getPointerSize(Value *V) const {
|
|
|
|
if (TD) {
|
|
|
|
if (AllocaInst *A = dyn_cast<AllocaInst>(V)) {
|
|
|
|
// Get size information for the alloca
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(A->getArraySize()))
|
|
|
|
return C->getZExtValue() * TD->getTypeAllocSize(A->getAllocatedType());
|
|
|
|
} else {
|
|
|
|
assert(isa<Argument>(V) && "Expected AllocaInst or Argument!");
|
|
|
|
const PointerType *PT = cast<PointerType>(V->getType());
|
|
|
|
return TD->getTypeAllocSize(PT->getElementType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ~0U;
|
|
|
|
}
|