mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-01 05:01:59 +01:00
[PM] Port DSE to the new pass manager
Patch by JakeVanAdrighem. Thanks! llvm-svn: 269847
This commit is contained in:
parent
a50570956b
commit
8ef32dfb5e
@ -105,7 +105,7 @@ void initializeCrossDSOCFIPass(PassRegistry&);
|
|||||||
void initializeDAEPass(PassRegistry&);
|
void initializeDAEPass(PassRegistry&);
|
||||||
void initializeDAHPass(PassRegistry&);
|
void initializeDAHPass(PassRegistry&);
|
||||||
void initializeDCELegacyPassPass(PassRegistry&);
|
void initializeDCELegacyPassPass(PassRegistry&);
|
||||||
void initializeDSEPass(PassRegistry&);
|
void initializeDSELegacyPassPass(PassRegistry&);
|
||||||
void initializeDeadInstEliminationPass(PassRegistry&);
|
void initializeDeadInstEliminationPass(PassRegistry&);
|
||||||
void initializeDeadMachineInstructionElimPass(PassRegistry&);
|
void initializeDeadMachineInstructionElimPass(PassRegistry&);
|
||||||
void initializeDelinearizationPass(PassRegistry &);
|
void initializeDelinearizationPass(PassRegistry &);
|
||||||
|
34
include/llvm/Transforms/Scalar/DeadStoreElimination.h
Normal file
34
include/llvm/Transforms/Scalar/DeadStoreElimination.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
//===- DeadStoreElimination.h - Fast Dead Store Elimination -------------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under 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.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_TRANSFORMS_SCALAR_DSE_H
|
||||||
|
#define LLVM_TRANSFORMS_SCALAR_DSE_H
|
||||||
|
|
||||||
|
#include "llvm/IR/Function.h"
|
||||||
|
#include "llvm/IR/PassManager.h"
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
|
||||||
|
/// This class implements a trivial dead store elimination. We consider
|
||||||
|
/// only the redundant stores that are local to a single Basic Block.
|
||||||
|
class DSEPass : public PassInfoMixin<DSEPass> {
|
||||||
|
public:
|
||||||
|
PreservedAnalyses run(Function &F, AnalysisManager<Function> &FAM);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // LLVM_TRANSFORMS_SCALAR_DSE_H
|
@ -64,6 +64,7 @@
|
|||||||
#include "llvm/Transforms/PGOInstrumentation.h"
|
#include "llvm/Transforms/PGOInstrumentation.h"
|
||||||
#include "llvm/Transforms/Scalar/ADCE.h"
|
#include "llvm/Transforms/Scalar/ADCE.h"
|
||||||
#include "llvm/Transforms/Scalar/DCE.h"
|
#include "llvm/Transforms/Scalar/DCE.h"
|
||||||
|
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
|
||||||
#include "llvm/Transforms/Scalar/EarlyCSE.h"
|
#include "llvm/Transforms/Scalar/EarlyCSE.h"
|
||||||
#include "llvm/Transforms/Scalar/GVN.h"
|
#include "llvm/Transforms/Scalar/GVN.h"
|
||||||
#include "llvm/Transforms/Scalar/LoopRotation.h"
|
#include "llvm/Transforms/Scalar/LoopRotation.h"
|
||||||
|
@ -111,6 +111,7 @@ FUNCTION_ALIAS_ANALYSIS("type-based-aa", TypeBasedAA())
|
|||||||
FUNCTION_PASS("aa-eval", AAEvaluator())
|
FUNCTION_PASS("aa-eval", AAEvaluator())
|
||||||
FUNCTION_PASS("adce", ADCEPass())
|
FUNCTION_PASS("adce", ADCEPass())
|
||||||
FUNCTION_PASS("dce", DCEPass())
|
FUNCTION_PASS("dce", DCEPass())
|
||||||
|
FUNCTION_PASS("dse", DSEPass())
|
||||||
FUNCTION_PASS("early-cse", EarlyCSEPass())
|
FUNCTION_PASS("early-cse", EarlyCSEPass())
|
||||||
FUNCTION_PASS("instcombine", InstCombinePass())
|
FUNCTION_PASS("instcombine", InstCombinePass())
|
||||||
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
|
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "llvm/Transforms/Scalar.h"
|
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
|
||||||
#include "llvm/ADT/STLExtras.h"
|
#include "llvm/ADT/STLExtras.h"
|
||||||
#include "llvm/ADT/SetVector.h"
|
#include "llvm/ADT/SetVector.h"
|
||||||
#include "llvm/ADT/Statistic.h"
|
#include "llvm/ADT/Statistic.h"
|
||||||
@ -36,6 +36,7 @@
|
|||||||
#include "llvm/Pass.h"
|
#include "llvm/Pass.h"
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
|
#include "llvm/Transforms/Scalar.h"
|
||||||
#include "llvm/Transforms/Utils/Local.h"
|
#include "llvm/Transforms/Utils/Local.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
@ -45,84 +46,19 @@ STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
|
|||||||
STATISTIC(NumFastStores, "Number of stores deleted");
|
STATISTIC(NumFastStores, "Number of stores deleted");
|
||||||
STATISTIC(NumFastOther , "Number of other instrs removed");
|
STATISTIC(NumFastOther , "Number of other instrs removed");
|
||||||
|
|
||||||
namespace {
|
|
||||||
struct DSE : public FunctionPass {
|
|
||||||
AliasAnalysis *AA;
|
|
||||||
MemoryDependenceResults *MD;
|
|
||||||
DominatorTree *DT;
|
|
||||||
const TargetLibraryInfo *TLI;
|
|
||||||
|
|
||||||
static char ID; // Pass identification, replacement for typeid
|
|
||||||
DSE() : FunctionPass(ID), AA(nullptr), MD(nullptr), DT(nullptr) {
|
|
||||||
initializeDSEPass(*PassRegistry::getPassRegistry());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool runOnFunction(Function &F) override {
|
|
||||||
if (skipFunction(F))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
|
||||||
MD = &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
|
|
||||||
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
||||||
TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
|
||||||
|
|
||||||
bool Changed = false;
|
|
||||||
for (BasicBlock &I : F)
|
|
||||||
// Only check non-dead blocks. Dead blocks may have strange pointer
|
|
||||||
// cycles that will confuse alias analysis.
|
|
||||||
if (DT->isReachableFromEntry(&I))
|
|
||||||
Changed |= runOnBasicBlock(I);
|
|
||||||
|
|
||||||
AA = nullptr; MD = nullptr; DT = nullptr;
|
|
||||||
return Changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool runOnBasicBlock(BasicBlock &BB);
|
|
||||||
bool MemoryIsNotModifiedBetween(Instruction *FirstI, Instruction *SecondI);
|
|
||||||
bool HandleFree(CallInst *F);
|
|
||||||
bool handleEndBlock(BasicBlock &BB);
|
|
||||||
void RemoveAccessedObjects(const MemoryLocation &LoadedLoc,
|
|
||||||
SmallSetVector<Value *, 16> &DeadStackObjects,
|
|
||||||
const DataLayout &DL);
|
|
||||||
|
|
||||||
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
||||||
AU.setPreservesCFG();
|
|
||||||
AU.addRequired<DominatorTreeWrapperPass>();
|
|
||||||
AU.addRequired<AAResultsWrapperPass>();
|
|
||||||
AU.addRequired<MemoryDependenceWrapperPass>();
|
|
||||||
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
||||||
AU.addPreserved<DominatorTreeWrapperPass>();
|
|
||||||
AU.addPreserved<GlobalsAAWrapperPass>();
|
|
||||||
AU.addPreserved<MemoryDependenceWrapperPass>();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
char DSE::ID = 0;
|
|
||||||
INITIALIZE_PASS_BEGIN(DSE, "dse", "Dead Store Elimination", false, false)
|
|
||||||
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
||||||
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
|
||||||
INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
|
|
||||||
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
|
|
||||||
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
||||||
INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
|
|
||||||
|
|
||||||
FunctionPass *llvm::createDeadStoreEliminationPass() { return new DSE(); }
|
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// Helper functions
|
// Helper functions
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
/// DeleteDeadInstruction - Delete this instruction. Before we do, go through
|
/// Delete this instruction. Before we do, go through and zero out all the
|
||||||
/// and zero out all the operands of this instruction. If any of them become
|
/// operands of this instruction. If any of them become dead, delete them and
|
||||||
/// dead, delete them and the computation tree that feeds them.
|
/// the computation tree that feeds them.
|
||||||
///
|
|
||||||
/// If ValueSet is non-null, remove any deleted instructions from it as well.
|
/// If ValueSet is non-null, remove any deleted instructions from it as well.
|
||||||
///
|
static void
|
||||||
static void DeleteDeadInstruction(Instruction *I,
|
deleteDeadInstruction(Instruction *I, MemoryDependenceResults &MD,
|
||||||
MemoryDependenceResults &MD,
|
|
||||||
const TargetLibraryInfo &TLI,
|
const TargetLibraryInfo &TLI,
|
||||||
SmallSetVector<Value*, 16> *ValueSet = nullptr) {
|
SmallSetVector<Value *, 16> *ValueSet = nullptr) {
|
||||||
SmallVector<Instruction*, 32> NowDeadInsts;
|
SmallVector<Instruction*, 32> NowDeadInsts;
|
||||||
|
|
||||||
NowDeadInsts.push_back(I);
|
NowDeadInsts.push_back(I);
|
||||||
@ -156,9 +92,8 @@ static void DeleteDeadInstruction(Instruction *I,
|
|||||||
} while (!NowDeadInsts.empty());
|
} while (!NowDeadInsts.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Does this instruction write some memory? This only returns true for things
|
||||||
/// hasMemoryWrite - Does this instruction write some memory? This only returns
|
/// that we can analyze with other helpers below.
|
||||||
/// true for things that we can analyze with other helpers below.
|
|
||||||
static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI) {
|
static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI) {
|
||||||
if (isa<StoreInst>(I))
|
if (isa<StoreInst>(I))
|
||||||
return true;
|
return true;
|
||||||
@ -197,9 +132,9 @@ static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// getLocForWrite - Return a Location stored to by the specified instruction.
|
/// Return a Location stored to by the specified instruction. If isRemovable
|
||||||
/// If isRemovable returns true, this function and getLocForRead completely
|
/// returns true, this function and getLocForRead completely describe the memory
|
||||||
/// describe the memory operations for this instruction.
|
/// operations for this instruction.
|
||||||
static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
|
static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
|
||||||
if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
|
if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
|
||||||
return MemoryLocation::get(SI);
|
return MemoryLocation::get(SI);
|
||||||
@ -228,8 +163,8 @@ static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// getLocForRead - Return the location read by the specified "hasMemoryWrite"
|
/// Return the location read by the specified "hasMemoryWrite" instruction if
|
||||||
/// instruction if any.
|
/// any.
|
||||||
static MemoryLocation getLocForRead(Instruction *Inst,
|
static MemoryLocation getLocForRead(Instruction *Inst,
|
||||||
const TargetLibraryInfo &TLI) {
|
const TargetLibraryInfo &TLI) {
|
||||||
assert(hasMemoryWrite(Inst, TLI) && "Unknown instruction case");
|
assert(hasMemoryWrite(Inst, TLI) && "Unknown instruction case");
|
||||||
@ -241,9 +176,8 @@ static MemoryLocation getLocForRead(Instruction *Inst,
|
|||||||
return MemoryLocation();
|
return MemoryLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If the value of this instruction and the memory it writes to is unused, may
|
||||||
/// isRemovable - If the value of this instruction and the memory it writes to
|
/// we delete this instruction?
|
||||||
/// is unused, may we delete this instruction?
|
|
||||||
static bool isRemovable(Instruction *I) {
|
static bool isRemovable(Instruction *I) {
|
||||||
// Don't remove volatile/atomic stores.
|
// Don't remove volatile/atomic stores.
|
||||||
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
||||||
@ -307,7 +241,7 @@ static bool isShortenableAtTheBeginning(Instruction *I) {
|
|||||||
return II && II->getIntrinsicID() == Intrinsic::memset;
|
return II && II->getIntrinsicID() == Intrinsic::memset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// getStoredPointerOperand - Return the pointer that is being written to.
|
/// Return the pointer that is being written to.
|
||||||
static Value *getStoredPointerOperand(Instruction *I) {
|
static Value *getStoredPointerOperand(Instruction *I) {
|
||||||
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
if (StoreInst *SI = dyn_cast<StoreInst>(I))
|
||||||
return SI->getPointerOperand();
|
return SI->getPointerOperand();
|
||||||
@ -458,7 +392,7 @@ static OverwriteResult isOverwrite(const MemoryLocation &Later,
|
|||||||
return OverwriteUnknown;
|
return OverwriteUnknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// isPossibleSelfRead - If 'Inst' might be a self read (i.e. a noop copy of a
|
/// If 'Inst' might be a self read (i.e. a noop copy of a
|
||||||
/// memory region into an identical pointer) then it doesn't actually make its
|
/// memory region into an identical pointer) then it doesn't actually make its
|
||||||
/// input dead in the traditional sense. Consider this case:
|
/// input dead in the traditional sense. Consider this case:
|
||||||
///
|
///
|
||||||
@ -503,11 +437,317 @@ static bool isPossibleSelfRead(Instruction *Inst,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
/// Returns true if the memory which is accessed by the second instruction is not
|
||||||
// DSE Pass
|
/// modified between the first and the second instruction.
|
||||||
//===----------------------------------------------------------------------===//
|
/// Precondition: Second instruction must be dominated by the first
|
||||||
|
/// instruction.
|
||||||
|
static bool memoryIsNotModifiedBetween(Instruction *FirstI,
|
||||||
|
Instruction *SecondI,
|
||||||
|
AliasAnalysis *AA) {
|
||||||
|
SmallVector<BasicBlock *, 16> WorkList;
|
||||||
|
SmallPtrSet<BasicBlock *, 8> Visited;
|
||||||
|
BasicBlock::iterator FirstBBI(FirstI);
|
||||||
|
++FirstBBI;
|
||||||
|
BasicBlock::iterator SecondBBI(SecondI);
|
||||||
|
BasicBlock *FirstBB = FirstI->getParent();
|
||||||
|
BasicBlock *SecondBB = SecondI->getParent();
|
||||||
|
MemoryLocation MemLoc = MemoryLocation::get(SecondI);
|
||||||
|
|
||||||
bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
// Start checking the store-block.
|
||||||
|
WorkList.push_back(SecondBB);
|
||||||
|
bool isFirstBlock = true;
|
||||||
|
|
||||||
|
// Check all blocks going backward until we reach the load-block.
|
||||||
|
while (!WorkList.empty()) {
|
||||||
|
BasicBlock *B = WorkList.pop_back_val();
|
||||||
|
|
||||||
|
// Ignore instructions before LI if this is the FirstBB.
|
||||||
|
BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin());
|
||||||
|
|
||||||
|
BasicBlock::iterator EI;
|
||||||
|
if (isFirstBlock) {
|
||||||
|
// Ignore instructions after SI if this is the first visit of SecondBB.
|
||||||
|
assert(B == SecondBB && "first block is not the store block");
|
||||||
|
EI = SecondBBI;
|
||||||
|
isFirstBlock = false;
|
||||||
|
} else {
|
||||||
|
// It's not SecondBB or (in case of a loop) the second visit of SecondBB.
|
||||||
|
// In this case we also have to look at instructions after SI.
|
||||||
|
EI = B->end();
|
||||||
|
}
|
||||||
|
for (; BI != EI; ++BI) {
|
||||||
|
Instruction *I = &*BI;
|
||||||
|
if (I->mayWriteToMemory() && I != SecondI) {
|
||||||
|
auto Res = AA->getModRefInfo(I, MemLoc);
|
||||||
|
if (Res != MRI_NoModRef)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (B != FirstBB) {
|
||||||
|
assert(B != &FirstBB->getParent()->getEntryBlock() &&
|
||||||
|
"Should not hit the entry block because SI must be dominated by LI");
|
||||||
|
for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) {
|
||||||
|
if (!Visited.insert(*PredI).second)
|
||||||
|
continue;
|
||||||
|
WorkList.push_back(*PredI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Find all blocks that will unconditionally lead to the block BB and append
|
||||||
|
/// them to F.
|
||||||
|
static void findUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
|
||||||
|
BasicBlock *BB, DominatorTree *DT) {
|
||||||
|
for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
|
||||||
|
BasicBlock *Pred = *I;
|
||||||
|
if (Pred == BB) continue;
|
||||||
|
TerminatorInst *PredTI = Pred->getTerminator();
|
||||||
|
if (PredTI->getNumSuccessors() != 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (DT->isReachableFromEntry(Pred))
|
||||||
|
Blocks.push_back(Pred);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handle frees of entire structures whose dependency is a store
|
||||||
|
/// to a field of that structure.
|
||||||
|
static bool handleFree(CallInst *F, AliasAnalysis *AA,
|
||||||
|
MemoryDependenceResults *MD, DominatorTree *DT,
|
||||||
|
const TargetLibraryInfo *TLI) {
|
||||||
|
bool MadeChange = false;
|
||||||
|
|
||||||
|
MemoryLocation Loc = MemoryLocation(F->getOperand(0));
|
||||||
|
SmallVector<BasicBlock *, 16> Blocks;
|
||||||
|
Blocks.push_back(F->getParent());
|
||||||
|
const DataLayout &DL = F->getModule()->getDataLayout();
|
||||||
|
|
||||||
|
while (!Blocks.empty()) {
|
||||||
|
BasicBlock *BB = Blocks.pop_back_val();
|
||||||
|
Instruction *InstPt = BB->getTerminator();
|
||||||
|
if (BB == F->getParent()) InstPt = F;
|
||||||
|
|
||||||
|
MemDepResult Dep =
|
||||||
|
MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB);
|
||||||
|
while (Dep.isDef() || Dep.isClobber()) {
|
||||||
|
Instruction *Dependency = Dep.getInst();
|
||||||
|
if (!hasMemoryWrite(Dependency, *TLI) || !isRemovable(Dependency))
|
||||||
|
break;
|
||||||
|
|
||||||
|
Value *DepPointer =
|
||||||
|
GetUnderlyingObject(getStoredPointerOperand(Dependency), DL);
|
||||||
|
|
||||||
|
// Check for aliasing.
|
||||||
|
if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
|
||||||
|
break;
|
||||||
|
|
||||||
|
auto Next = ++Dependency->getIterator();
|
||||||
|
|
||||||
|
// DCE instructions only used to calculate that store
|
||||||
|
deleteDeadInstruction(Dependency, *MD, *TLI);
|
||||||
|
++NumFastStores;
|
||||||
|
MadeChange = true;
|
||||||
|
|
||||||
|
// Inst's old Dependency is now deleted. Compute the next dependency,
|
||||||
|
// which may also be dead, as in
|
||||||
|
// s[0] = 0;
|
||||||
|
// s[1] = 0; // This has just been deleted.
|
||||||
|
// free(s);
|
||||||
|
Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Dep.isNonLocal())
|
||||||
|
findUnconditionalPreds(Blocks, BB, DT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return MadeChange;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check to see if the specified location may alias any of the stack objects in
|
||||||
|
/// the DeadStackObjects set. If so, they become live because the location is
|
||||||
|
/// being loaded.
|
||||||
|
static void removeAccessedObjects(const MemoryLocation &LoadedLoc,
|
||||||
|
SmallSetVector<Value *, 16> &DeadStackObjects,
|
||||||
|
const DataLayout &DL, AliasAnalysis *AA,
|
||||||
|
const TargetLibraryInfo *TLI) {
|
||||||
|
const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL);
|
||||||
|
|
||||||
|
// A constant can't be in the dead pointer set.
|
||||||
|
if (isa<Constant>(UnderlyingPointer))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// If the kill pointer can be easily reduced to an alloca, don't bother doing
|
||||||
|
// extraneous AA queries.
|
||||||
|
if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
|
||||||
|
DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove objects that could alias LoadedLoc.
|
||||||
|
DeadStackObjects.remove_if([&](Value *I) {
|
||||||
|
// See if the loaded location could alias the stack location.
|
||||||
|
MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI));
|
||||||
|
return !AA->isNoAlias(StackLoc, LoadedLoc);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove dead stores to stack-allocated locations in the function end block.
|
||||||
|
/// Ex:
|
||||||
|
/// %A = alloca i32
|
||||||
|
/// ...
|
||||||
|
/// store i32 1, i32* %A
|
||||||
|
/// ret void
|
||||||
|
static bool handleEndBlock(BasicBlock &BB, AliasAnalysis *AA,
|
||||||
|
MemoryDependenceResults *MD,
|
||||||
|
const TargetLibraryInfo *TLI) {
|
||||||
|
bool MadeChange = false;
|
||||||
|
|
||||||
|
// Keep track of all of the stack objects that are dead at the end of the
|
||||||
|
// function.
|
||||||
|
SmallSetVector<Value*, 16> DeadStackObjects;
|
||||||
|
|
||||||
|
// Find all of the alloca'd pointers in the entry block.
|
||||||
|
BasicBlock &Entry = BB.getParent()->front();
|
||||||
|
for (Instruction &I : Entry) {
|
||||||
|
if (isa<AllocaInst>(&I))
|
||||||
|
DeadStackObjects.insert(&I);
|
||||||
|
|
||||||
|
// Okay, so these are dead heap objects, but if the pointer never escapes
|
||||||
|
// then it's leaked by this function anyways.
|
||||||
|
else if (isAllocLikeFn(&I, TLI) && !PointerMayBeCaptured(&I, true, true))
|
||||||
|
DeadStackObjects.insert(&I);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Treat byval or inalloca arguments the same, stores to them are dead at the
|
||||||
|
// end of the function.
|
||||||
|
for (Argument &AI : BB.getParent()->args())
|
||||||
|
if (AI.hasByValOrInAllocaAttr())
|
||||||
|
DeadStackObjects.insert(&AI);
|
||||||
|
|
||||||
|
const DataLayout &DL = BB.getModule()->getDataLayout();
|
||||||
|
|
||||||
|
// Scan the basic block backwards
|
||||||
|
for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
|
||||||
|
--BBI;
|
||||||
|
|
||||||
|
// If we find a store, check to see if it points into a dead stack value.
|
||||||
|
if (hasMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) {
|
||||||
|
// See through pointer-to-pointer bitcasts
|
||||||
|
SmallVector<Value *, 4> Pointers;
|
||||||
|
GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL);
|
||||||
|
|
||||||
|
// Stores to stack values are valid candidates for removal.
|
||||||
|
bool AllDead = true;
|
||||||
|
for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
|
||||||
|
E = Pointers.end(); I != E; ++I)
|
||||||
|
if (!DeadStackObjects.count(*I)) {
|
||||||
|
AllDead = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (AllDead) {
|
||||||
|
Instruction *Dead = &*BBI++;
|
||||||
|
|
||||||
|
DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
|
||||||
|
<< *Dead << "\n Objects: ";
|
||||||
|
for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
|
||||||
|
E = Pointers.end(); I != E; ++I) {
|
||||||
|
dbgs() << **I;
|
||||||
|
if (std::next(I) != E)
|
||||||
|
dbgs() << ", ";
|
||||||
|
}
|
||||||
|
dbgs() << '\n');
|
||||||
|
|
||||||
|
// DCE instructions only used to calculate that store.
|
||||||
|
deleteDeadInstruction(Dead, *MD, *TLI, &DeadStackObjects);
|
||||||
|
++NumFastStores;
|
||||||
|
MadeChange = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any dead non-memory-mutating instructions.
|
||||||
|
if (isInstructionTriviallyDead(&*BBI, TLI)) {
|
||||||
|
Instruction *Inst = &*BBI++;
|
||||||
|
deleteDeadInstruction(Inst, *MD, *TLI, &DeadStackObjects);
|
||||||
|
++NumFastOther;
|
||||||
|
MadeChange = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isa<AllocaInst>(BBI)) {
|
||||||
|
// Remove allocas from the list of dead stack objects; there can't be
|
||||||
|
// any references before the definition.
|
||||||
|
DeadStackObjects.remove(&*BBI);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto CS = CallSite(&*BBI)) {
|
||||||
|
// Remove allocation function calls from the list of dead stack objects;
|
||||||
|
// there can't be any references before the definition.
|
||||||
|
if (isAllocLikeFn(&*BBI, TLI))
|
||||||
|
DeadStackObjects.remove(&*BBI);
|
||||||
|
|
||||||
|
// If this call does not access memory, it can't be loading any of our
|
||||||
|
// pointers.
|
||||||
|
if (AA->doesNotAccessMemory(CS))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// If the call might load from any of our allocas, then any store above
|
||||||
|
// the call is live.
|
||||||
|
DeadStackObjects.remove_if([&](Value *I) {
|
||||||
|
// See if the call site touches the value.
|
||||||
|
ModRefInfo A = AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI));
|
||||||
|
|
||||||
|
return A == MRI_ModRef || A == MRI_Ref;
|
||||||
|
});
|
||||||
|
|
||||||
|
// If all of the allocas were clobbered by the call then we're not going
|
||||||
|
// to find anything else to process.
|
||||||
|
if (DeadStackObjects.empty())
|
||||||
|
break;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryLocation LoadedLoc;
|
||||||
|
|
||||||
|
// If we encounter a use of the pointer, it is no longer considered dead
|
||||||
|
if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
|
||||||
|
if (!L->isUnordered()) // Be conservative with atomic/volatile load
|
||||||
|
break;
|
||||||
|
LoadedLoc = MemoryLocation::get(L);
|
||||||
|
} else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
|
||||||
|
LoadedLoc = MemoryLocation::get(V);
|
||||||
|
} else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) {
|
||||||
|
LoadedLoc = MemoryLocation::getForSource(MTI);
|
||||||
|
} else if (!BBI->mayReadFromMemory()) {
|
||||||
|
// Instruction doesn't read memory. Note that stores that weren't removed
|
||||||
|
// above will hit this case.
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
// Unknown inst; assume it clobbers everything.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any allocas from the DeadPointer set that are loaded, as this
|
||||||
|
// makes any stores above the access live.
|
||||||
|
removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI);
|
||||||
|
|
||||||
|
// If all of the allocas were clobbered by the access then we're not going
|
||||||
|
// to find anything else to process.
|
||||||
|
if (DeadStackObjects.empty())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MadeChange;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool eliminateDeadStores(BasicBlock &BB, AliasAnalysis *AA,
|
||||||
|
MemoryDependenceResults *MD, DominatorTree *DT,
|
||||||
|
const TargetLibraryInfo *TLI) {
|
||||||
const DataLayout &DL = BB.getModule()->getDataLayout();
|
const DataLayout &DL = BB.getModule()->getDataLayout();
|
||||||
bool MadeChange = false;
|
bool MadeChange = false;
|
||||||
|
|
||||||
@ -517,7 +757,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
|
|
||||||
// Handle 'free' calls specially.
|
// Handle 'free' calls specially.
|
||||||
if (CallInst *F = isFreeCall(Inst, TLI)) {
|
if (CallInst *F = isFreeCall(Inst, TLI)) {
|
||||||
MadeChange |= HandleFree(F);
|
MadeChange |= handleFree(F, AA, MD, DT, TLI);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -530,11 +770,11 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
||||||
|
|
||||||
auto RemoveDeadInstAndUpdateBBI = [&](Instruction *DeadInst) {
|
auto RemoveDeadInstAndUpdateBBI = [&](Instruction *DeadInst) {
|
||||||
// DeleteDeadInstruction can delete the current instruction. Save BBI
|
// deleteDeadInstruction can delete the current instruction. Save BBI
|
||||||
// in case we need it.
|
// in case we need it.
|
||||||
WeakVH NextInst(&*BBI);
|
WeakVH NextInst(&*BBI);
|
||||||
|
|
||||||
DeleteDeadInstruction(DeadInst, *MD, *TLI);
|
deleteDeadInstruction(DeadInst, *MD, *TLI);
|
||||||
|
|
||||||
if (!NextInst) // Next instruction deleted.
|
if (!NextInst) // Next instruction deleted.
|
||||||
BBI = BB.begin();
|
BBI = BB.begin();
|
||||||
@ -547,7 +787,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
if (LoadInst *DepLoad = dyn_cast<LoadInst>(SI->getValueOperand())) {
|
if (LoadInst *DepLoad = dyn_cast<LoadInst>(SI->getValueOperand())) {
|
||||||
if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
|
if (SI->getPointerOperand() == DepLoad->getPointerOperand() &&
|
||||||
isRemovable(SI) &&
|
isRemovable(SI) &&
|
||||||
MemoryIsNotModifiedBetween(DepLoad, SI)) {
|
memoryIsNotModifiedBetween(DepLoad, SI, AA)) {
|
||||||
|
|
||||||
DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n "
|
DEBUG(dbgs() << "DSE: Remove Store Of Load from same pointer:\n "
|
||||||
<< "LOAD: " << *DepLoad << "\n STORE: " << *SI << '\n');
|
<< "LOAD: " << *DepLoad << "\n STORE: " << *SI << '\n');
|
||||||
@ -566,7 +806,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
GetUnderlyingObject(SI->getPointerOperand(), DL));
|
GetUnderlyingObject(SI->getPointerOperand(), DL));
|
||||||
|
|
||||||
if (UnderlyingPointer && isCallocLikeFn(UnderlyingPointer, TLI) &&
|
if (UnderlyingPointer && isCallocLikeFn(UnderlyingPointer, TLI) &&
|
||||||
MemoryIsNotModifiedBetween(UnderlyingPointer, SI)) {
|
memoryIsNotModifiedBetween(UnderlyingPointer, SI, AA)) {
|
||||||
DEBUG(dbgs()
|
DEBUG(dbgs()
|
||||||
<< "DSE: Remove null store to the calloc'ed object:\n DEAD: "
|
<< "DSE: Remove null store to the calloc'ed object:\n DEAD: "
|
||||||
<< *Inst << "\n OBJECT: " << *UnderlyingPointer << '\n');
|
<< *Inst << "\n OBJECT: " << *UnderlyingPointer << '\n');
|
||||||
@ -618,11 +858,11 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
<< *DepWrite << "\n KILLER: " << *Inst << '\n');
|
<< *DepWrite << "\n KILLER: " << *Inst << '\n');
|
||||||
|
|
||||||
// Delete the store and now-dead instructions that feed it.
|
// Delete the store and now-dead instructions that feed it.
|
||||||
DeleteDeadInstruction(DepWrite, *MD, *TLI);
|
deleteDeadInstruction(DepWrite, *MD, *TLI);
|
||||||
++NumFastStores;
|
++NumFastStores;
|
||||||
MadeChange = true;
|
MadeChange = true;
|
||||||
|
|
||||||
// DeleteDeadInstruction can delete the current instruction in loop
|
// deleteDeadInstruction can delete the current instruction in loop
|
||||||
// cases, reset BBI.
|
// cases, reset BBI.
|
||||||
BBI = Inst->getIterator();
|
BBI = Inst->getIterator();
|
||||||
if (BBI != BB.begin())
|
if (BBI != BB.begin())
|
||||||
@ -698,309 +938,87 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
// If this block ends in a return, unwind, or unreachable, all allocas are
|
// 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.
|
// dead at its end, which means stores to them are also dead.
|
||||||
if (BB.getTerminator()->getNumSuccessors() == 0)
|
if (BB.getTerminator()->getNumSuccessors() == 0)
|
||||||
MadeChange |= handleEndBlock(BB);
|
MadeChange |= handleEndBlock(BB, AA, MD, TLI);
|
||||||
|
|
||||||
return MadeChange;
|
return MadeChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the memory which is accessed by the second instruction is not
|
static bool eliminateDeadStores(Function &F, AliasAnalysis *AA,
|
||||||
/// modified between the first and the second instruction.
|
MemoryDependenceResults *MD, DominatorTree *DT,
|
||||||
/// Precondition: Second instruction must be dominated by the first
|
const TargetLibraryInfo *TLI) {
|
||||||
/// instruction.
|
bool MadeChange = false;
|
||||||
bool DSE::MemoryIsNotModifiedBetween(Instruction *FirstI,
|
for (BasicBlock &BB : F)
|
||||||
Instruction *SecondI) {
|
// Only check non-dead blocks. Dead blocks may have strange pointer
|
||||||
SmallVector<BasicBlock *, 16> WorkList;
|
// cycles that will confuse alias analysis.
|
||||||
SmallPtrSet<BasicBlock *, 8> Visited;
|
if (DT->isReachableFromEntry(&BB))
|
||||||
BasicBlock::iterator FirstBBI(FirstI);
|
MadeChange |= eliminateDeadStores(BB, AA, MD, DT, TLI);
|
||||||
++FirstBBI;
|
return MadeChange;
|
||||||
BasicBlock::iterator SecondBBI(SecondI);
|
}
|
||||||
BasicBlock *FirstBB = FirstI->getParent();
|
|
||||||
BasicBlock *SecondBB = SecondI->getParent();
|
|
||||||
MemoryLocation MemLoc = MemoryLocation::get(SecondI);
|
|
||||||
|
|
||||||
// Start checking the store-block.
|
//===----------------------------------------------------------------------===//
|
||||||
WorkList.push_back(SecondBB);
|
// DSE Pass
|
||||||
bool isFirstBlock = true;
|
//===----------------------------------------------------------------------===//
|
||||||
|
PreservedAnalyses DSEPass::run(Function &F, FunctionAnalysisManager &AM) {
|
||||||
|
AliasAnalysis *AA = &AM.getResult<AAManager>(F);
|
||||||
|
DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
|
||||||
|
MemoryDependenceResults *MD = &AM.getResult<MemoryDependenceAnalysis>(F);
|
||||||
|
const TargetLibraryInfo *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
|
||||||
|
|
||||||
// Check all blocks going backward until we reach the load-block.
|
if (!eliminateDeadStores(F, AA, MD, DT, TLI))
|
||||||
while (!WorkList.empty()) {
|
return PreservedAnalyses::all();
|
||||||
BasicBlock *B = WorkList.pop_back_val();
|
PreservedAnalyses PA;
|
||||||
|
PA.preserve<DominatorTreeAnalysis>();
|
||||||
|
PA.preserve<GlobalsAA>();
|
||||||
|
PA.preserve<MemoryDependenceAnalysis>();
|
||||||
|
return PA;
|
||||||
|
}
|
||||||
|
|
||||||
// Ignore instructions before LI if this is the FirstBB.
|
/// A legacy pass for the legacy pass manager that wraps \c DSEPass.
|
||||||
BasicBlock::iterator BI = (B == FirstBB ? FirstBBI : B->begin());
|
class DSELegacyPass : public FunctionPass {
|
||||||
|
public:
|
||||||
BasicBlock::iterator EI;
|
DSELegacyPass() : FunctionPass(ID) {
|
||||||
if (isFirstBlock) {
|
initializeDSELegacyPassPass(*PassRegistry::getPassRegistry());
|
||||||
// Ignore instructions after SI if this is the first visit of SecondBB.
|
|
||||||
assert(B == SecondBB && "first block is not the store block");
|
|
||||||
EI = SecondBBI;
|
|
||||||
isFirstBlock = false;
|
|
||||||
} else {
|
|
||||||
// It's not SecondBB or (in case of a loop) the second visit of SecondBB.
|
|
||||||
// In this case we also have to look at instructions after SI.
|
|
||||||
EI = B->end();
|
|
||||||
}
|
}
|
||||||
for (; BI != EI; ++BI) {
|
|
||||||
Instruction *I = &*BI;
|
bool runOnFunction(Function &F) override {
|
||||||
if (I->mayWriteToMemory() && I != SecondI) {
|
if (skipFunction(F))
|
||||||
auto Res = AA->getModRefInfo(I, MemLoc);
|
|
||||||
if (Res != MRI_NoModRef)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
||||||
|
AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
||||||
|
MemoryDependenceResults *MD =
|
||||||
|
&getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
|
||||||
|
const TargetLibraryInfo *TLI =
|
||||||
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
||||||
|
|
||||||
|
return eliminateDeadStores(F, AA, MD, DT, TLI);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
||||||
|
AU.setPreservesCFG();
|
||||||
|
AU.addRequired<DominatorTreeWrapperPass>();
|
||||||
|
AU.addRequired<AAResultsWrapperPass>();
|
||||||
|
AU.addRequired<MemoryDependenceWrapperPass>();
|
||||||
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
||||||
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
||||||
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
||||||
|
AU.addPreserved<MemoryDependenceWrapperPass>();
|
||||||
}
|
}
|
||||||
if (B != FirstBB) {
|
|
||||||
assert(B != &FirstBB->getParent()->getEntryBlock() &&
|
static char ID; // Pass identification, replacement for typeid
|
||||||
"Should not hit the entry block because SI must be dominated by LI");
|
};
|
||||||
for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) {
|
|
||||||
if (!Visited.insert(*PredI).second)
|
char DSELegacyPass::ID = 0;
|
||||||
continue;
|
INITIALIZE_PASS_BEGIN(DSELegacyPass, "dse", "Dead Store Elimination", false,
|
||||||
WorkList.push_back(*PredI);
|
false)
|
||||||
}
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
||||||
}
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
||||||
}
|
INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
|
||||||
return true;
|
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
|
||||||
}
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
||||||
|
INITIALIZE_PASS_END(DSELegacyPass, "dse", "Dead Store Elimination", false,
|
||||||
/// Find all blocks that will unconditionally lead to the block BB and append
|
false)
|
||||||
/// them to F.
|
|
||||||
static void FindUnconditionalPreds(SmallVectorImpl<BasicBlock *> &Blocks,
|
FunctionPass *llvm::createDeadStoreEliminationPass() {
|
||||||
BasicBlock *BB, DominatorTree *DT) {
|
return new DSELegacyPass();
|
||||||
for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
|
|
||||||
BasicBlock *Pred = *I;
|
|
||||||
if (Pred == BB) continue;
|
|
||||||
TerminatorInst *PredTI = Pred->getTerminator();
|
|
||||||
if (PredTI->getNumSuccessors() != 1)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (DT->isReachableFromEntry(Pred))
|
|
||||||
Blocks.push_back(Pred);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// HandleFree - Handle frees of entire structures whose dependency is a store
|
|
||||||
/// to a field of that structure.
|
|
||||||
bool DSE::HandleFree(CallInst *F) {
|
|
||||||
bool MadeChange = false;
|
|
||||||
|
|
||||||
MemoryLocation Loc = MemoryLocation(F->getOperand(0));
|
|
||||||
SmallVector<BasicBlock *, 16> Blocks;
|
|
||||||
Blocks.push_back(F->getParent());
|
|
||||||
const DataLayout &DL = F->getModule()->getDataLayout();
|
|
||||||
|
|
||||||
while (!Blocks.empty()) {
|
|
||||||
BasicBlock *BB = Blocks.pop_back_val();
|
|
||||||
Instruction *InstPt = BB->getTerminator();
|
|
||||||
if (BB == F->getParent()) InstPt = F;
|
|
||||||
|
|
||||||
MemDepResult Dep =
|
|
||||||
MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB);
|
|
||||||
while (Dep.isDef() || Dep.isClobber()) {
|
|
||||||
Instruction *Dependency = Dep.getInst();
|
|
||||||
if (!hasMemoryWrite(Dependency, *TLI) || !isRemovable(Dependency))
|
|
||||||
break;
|
|
||||||
|
|
||||||
Value *DepPointer =
|
|
||||||
GetUnderlyingObject(getStoredPointerOperand(Dependency), DL);
|
|
||||||
|
|
||||||
// Check for aliasing.
|
|
||||||
if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
|
|
||||||
break;
|
|
||||||
|
|
||||||
auto Next = ++Dependency->getIterator();
|
|
||||||
|
|
||||||
// DCE instructions only used to calculate that store
|
|
||||||
DeleteDeadInstruction(Dependency, *MD, *TLI);
|
|
||||||
++NumFastStores;
|
|
||||||
MadeChange = true;
|
|
||||||
|
|
||||||
// Inst's old Dependency is now deleted. Compute the next dependency,
|
|
||||||
// which may also be dead, as in
|
|
||||||
// s[0] = 0;
|
|
||||||
// s[1] = 0; // This has just been deleted.
|
|
||||||
// free(s);
|
|
||||||
Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Dep.isNonLocal())
|
|
||||||
FindUnconditionalPreds(Blocks, BB, DT);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MadeChange;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// handleEndBlock - Remove dead stores to stack-allocated locations in the
|
|
||||||
/// function end block. Ex:
|
|
||||||
/// %A = alloca i32
|
|
||||||
/// ...
|
|
||||||
/// store i32 1, i32* %A
|
|
||||||
/// ret void
|
|
||||||
bool DSE::handleEndBlock(BasicBlock &BB) {
|
|
||||||
bool MadeChange = false;
|
|
||||||
|
|
||||||
// Keep track of all of the stack objects that are dead at the end of the
|
|
||||||
// function.
|
|
||||||
SmallSetVector<Value*, 16> DeadStackObjects;
|
|
||||||
|
|
||||||
// Find all of the alloca'd pointers in the entry block.
|
|
||||||
BasicBlock &Entry = BB.getParent()->front();
|
|
||||||
for (Instruction &I : Entry) {
|
|
||||||
if (isa<AllocaInst>(&I))
|
|
||||||
DeadStackObjects.insert(&I);
|
|
||||||
|
|
||||||
// Okay, so these are dead heap objects, but if the pointer never escapes
|
|
||||||
// then it's leaked by this function anyways.
|
|
||||||
else if (isAllocLikeFn(&I, TLI) && !PointerMayBeCaptured(&I, true, true))
|
|
||||||
DeadStackObjects.insert(&I);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Treat byval or inalloca arguments the same, stores to them are dead at the
|
|
||||||
// end of the function.
|
|
||||||
for (Argument &AI : BB.getParent()->args())
|
|
||||||
if (AI.hasByValOrInAllocaAttr())
|
|
||||||
DeadStackObjects.insert(&AI);
|
|
||||||
|
|
||||||
const DataLayout &DL = BB.getModule()->getDataLayout();
|
|
||||||
|
|
||||||
// Scan the basic block backwards
|
|
||||||
for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){
|
|
||||||
--BBI;
|
|
||||||
|
|
||||||
// If we find a store, check to see if it points into a dead stack value.
|
|
||||||
if (hasMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) {
|
|
||||||
// See through pointer-to-pointer bitcasts
|
|
||||||
SmallVector<Value *, 4> Pointers;
|
|
||||||
GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL);
|
|
||||||
|
|
||||||
// Stores to stack values are valid candidates for removal.
|
|
||||||
bool AllDead = true;
|
|
||||||
for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
|
|
||||||
E = Pointers.end(); I != E; ++I)
|
|
||||||
if (!DeadStackObjects.count(*I)) {
|
|
||||||
AllDead = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (AllDead) {
|
|
||||||
Instruction *Dead = &*BBI++;
|
|
||||||
|
|
||||||
DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
|
|
||||||
<< *Dead << "\n Objects: ";
|
|
||||||
for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
|
|
||||||
E = Pointers.end(); I != E; ++I) {
|
|
||||||
dbgs() << **I;
|
|
||||||
if (std::next(I) != E)
|
|
||||||
dbgs() << ", ";
|
|
||||||
}
|
|
||||||
dbgs() << '\n');
|
|
||||||
|
|
||||||
// DCE instructions only used to calculate that store.
|
|
||||||
DeleteDeadInstruction(Dead, *MD, *TLI, &DeadStackObjects);
|
|
||||||
++NumFastStores;
|
|
||||||
MadeChange = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove any dead non-memory-mutating instructions.
|
|
||||||
if (isInstructionTriviallyDead(&*BBI, TLI)) {
|
|
||||||
Instruction *Inst = &*BBI++;
|
|
||||||
DeleteDeadInstruction(Inst, *MD, *TLI, &DeadStackObjects);
|
|
||||||
++NumFastOther;
|
|
||||||
MadeChange = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isa<AllocaInst>(BBI)) {
|
|
||||||
// Remove allocas from the list of dead stack objects; there can't be
|
|
||||||
// any references before the definition.
|
|
||||||
DeadStackObjects.remove(&*BBI);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (auto CS = CallSite(&*BBI)) {
|
|
||||||
// Remove allocation function calls from the list of dead stack objects;
|
|
||||||
// there can't be any references before the definition.
|
|
||||||
if (isAllocLikeFn(&*BBI, TLI))
|
|
||||||
DeadStackObjects.remove(&*BBI);
|
|
||||||
|
|
||||||
// If this call does not access memory, it can't be loading any of our
|
|
||||||
// pointers.
|
|
||||||
if (AA->doesNotAccessMemory(CS))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// If the call might load from any of our allocas, then any store above
|
|
||||||
// the call is live.
|
|
||||||
DeadStackObjects.remove_if([&](Value *I) {
|
|
||||||
// See if the call site touches the value.
|
|
||||||
ModRefInfo A = AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI));
|
|
||||||
|
|
||||||
return A == MRI_ModRef || A == MRI_Ref;
|
|
||||||
});
|
|
||||||
|
|
||||||
// If all of the allocas were clobbered by the call then we're not going
|
|
||||||
// to find anything else to process.
|
|
||||||
if (DeadStackObjects.empty())
|
|
||||||
break;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
MemoryLocation LoadedLoc;
|
|
||||||
|
|
||||||
// If we encounter a use of the pointer, it is no longer considered dead
|
|
||||||
if (LoadInst *L = dyn_cast<LoadInst>(BBI)) {
|
|
||||||
if (!L->isUnordered()) // Be conservative with atomic/volatile load
|
|
||||||
break;
|
|
||||||
LoadedLoc = MemoryLocation::get(L);
|
|
||||||
} else if (VAArgInst *V = dyn_cast<VAArgInst>(BBI)) {
|
|
||||||
LoadedLoc = MemoryLocation::get(V);
|
|
||||||
} else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(BBI)) {
|
|
||||||
LoadedLoc = MemoryLocation::getForSource(MTI);
|
|
||||||
} else if (!BBI->mayReadFromMemory()) {
|
|
||||||
// Instruction doesn't read memory. Note that stores that weren't removed
|
|
||||||
// above will hit this case.
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
// Unknown inst; assume it clobbers everything.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove any allocas from the DeadPointer set that are loaded, as this
|
|
||||||
// makes any stores above the access live.
|
|
||||||
RemoveAccessedObjects(LoadedLoc, DeadStackObjects, DL);
|
|
||||||
|
|
||||||
// If all of the allocas were clobbered by the access then we're not going
|
|
||||||
// to find anything else to process.
|
|
||||||
if (DeadStackObjects.empty())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return MadeChange;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// RemoveAccessedObjects - Check to see if the specified location may alias any
|
|
||||||
/// of the stack objects in the DeadStackObjects set. If so, they become live
|
|
||||||
/// because the location is being loaded.
|
|
||||||
void DSE::RemoveAccessedObjects(const MemoryLocation &LoadedLoc,
|
|
||||||
SmallSetVector<Value *, 16> &DeadStackObjects,
|
|
||||||
const DataLayout &DL) {
|
|
||||||
const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL);
|
|
||||||
|
|
||||||
// A constant can't be in the dead pointer set.
|
|
||||||
if (isa<Constant>(UnderlyingPointer))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// If the kill pointer can be easily reduced to an alloca, don't bother doing
|
|
||||||
// extraneous AA queries.
|
|
||||||
if (isa<AllocaInst>(UnderlyingPointer) || isa<Argument>(UnderlyingPointer)) {
|
|
||||||
DeadStackObjects.remove(const_cast<Value*>(UnderlyingPointer));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove objects that could alias LoadedLoc.
|
|
||||||
DeadStackObjects.remove_if([&](Value *I) {
|
|
||||||
// See if the loaded location could alias the stack location.
|
|
||||||
MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI));
|
|
||||||
return !AA->isNoAlias(StackLoc, LoadedLoc);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
|
|||||||
initializeDCELegacyPassPass(Registry);
|
initializeDCELegacyPassPass(Registry);
|
||||||
initializeDeadInstEliminationPass(Registry);
|
initializeDeadInstEliminationPass(Registry);
|
||||||
initializeScalarizerPass(Registry);
|
initializeScalarizerPass(Registry);
|
||||||
initializeDSEPass(Registry);
|
initializeDSELegacyPassPass(Registry);
|
||||||
initializeGVNLegacyPassPass(Registry);
|
initializeGVNLegacyPassPass(Registry);
|
||||||
initializeEarlyCSELegacyPassPass(Registry);
|
initializeEarlyCSELegacyPassPass(Registry);
|
||||||
initializeFlattenCFGPassPass(Registry);
|
initializeFlattenCFGPassPass(Registry);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
; RUN: opt < %s -basicaa -dse -S | FileCheck %s
|
; RUN: opt < %s -basicaa -dse -S | FileCheck %s
|
||||||
|
; RUN: opt < %s -aa-pipeline=basic-aa -passes=dse -S | FileCheck %s
|
||||||
target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
|
target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
|
||||||
|
|
||||||
declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
|
declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
|
||||||
|
Loading…
x
Reference in New Issue
Block a user