mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-01 05:01:59 +01:00
Do simple cross-block DSE when we encounter a free statement. Fixes PR11240.
llvm-svn: 143808
This commit is contained in:
parent
351ccbc5d4
commit
7ea3dd8ae5
@ -34,6 +34,7 @@
|
|||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
#include "llvm/ADT/SmallPtrSet.h"
|
#include "llvm/ADT/SmallPtrSet.h"
|
||||||
#include "llvm/ADT/Statistic.h"
|
#include "llvm/ADT/Statistic.h"
|
||||||
|
#include "llvm/ADT/STLExtras.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
STATISTIC(NumFastStores, "Number of stores deleted");
|
STATISTIC(NumFastStores, "Number of stores deleted");
|
||||||
@ -43,25 +44,26 @@ namespace {
|
|||||||
struct DSE : public FunctionPass {
|
struct DSE : public FunctionPass {
|
||||||
AliasAnalysis *AA;
|
AliasAnalysis *AA;
|
||||||
MemoryDependenceAnalysis *MD;
|
MemoryDependenceAnalysis *MD;
|
||||||
|
DominatorTree *DT;
|
||||||
|
|
||||||
static char ID; // Pass identification, replacement for typeid
|
static char ID; // Pass identification, replacement for typeid
|
||||||
DSE() : FunctionPass(ID), AA(0), MD(0) {
|
DSE() : FunctionPass(ID), AA(0), MD(0), DT(0) {
|
||||||
initializeDSEPass(*PassRegistry::getPassRegistry());
|
initializeDSEPass(*PassRegistry::getPassRegistry());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool runOnFunction(Function &F) {
|
virtual bool runOnFunction(Function &F) {
|
||||||
AA = &getAnalysis<AliasAnalysis>();
|
AA = &getAnalysis<AliasAnalysis>();
|
||||||
MD = &getAnalysis<MemoryDependenceAnalysis>();
|
MD = &getAnalysis<MemoryDependenceAnalysis>();
|
||||||
DominatorTree &DT = getAnalysis<DominatorTree>();
|
DT = &getAnalysis<DominatorTree>();
|
||||||
|
|
||||||
bool Changed = false;
|
bool Changed = false;
|
||||||
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
|
||||||
// Only check non-dead blocks. Dead blocks may have strange pointer
|
// Only check non-dead blocks. Dead blocks may have strange pointer
|
||||||
// cycles that will confuse alias analysis.
|
// cycles that will confuse alias analysis.
|
||||||
if (DT.isReachableFromEntry(I))
|
if (DT->isReachableFromEntry(I))
|
||||||
Changed |= runOnBasicBlock(*I);
|
Changed |= runOnBasicBlock(*I);
|
||||||
|
|
||||||
AA = 0; MD = 0;
|
AA = 0; MD = 0; DT = 0;
|
||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -549,24 +551,49 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
return MadeChange;
|
return MadeChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
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
|
/// HandleFree - Handle frees of entire structures whose dependency is a store
|
||||||
/// to a field of that structure.
|
/// to a field of that structure.
|
||||||
bool DSE::HandleFree(CallInst *F) {
|
bool DSE::HandleFree(CallInst *F) {
|
||||||
bool MadeChange = false;
|
bool MadeChange = false;
|
||||||
|
|
||||||
MemDepResult Dep = MD->getDependency(F);
|
AliasAnalysis::Location Loc = AliasAnalysis::Location(F->getOperand(0));
|
||||||
|
SmallVector<BasicBlock *, 16> Blocks;
|
||||||
|
Blocks.push_back(F->getParent());
|
||||||
|
|
||||||
|
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, BB);
|
||||||
while (Dep.isDef() || Dep.isClobber()) {
|
while (Dep.isDef() || Dep.isClobber()) {
|
||||||
Instruction *Dependency = Dep.getInst();
|
Instruction *Dependency = Dep.getInst();
|
||||||
if (!hasMemoryWrite(Dependency) || !isRemovable(Dependency))
|
if (!hasMemoryWrite(Dependency) || !isRemovable(Dependency))
|
||||||
return MadeChange;
|
break;
|
||||||
|
|
||||||
Value *DepPointer =
|
Value *DepPointer =
|
||||||
GetUnderlyingObject(getStoredPointerOperand(Dependency));
|
GetUnderlyingObject(getStoredPointerOperand(Dependency));
|
||||||
|
|
||||||
// Check for aliasing.
|
// Check for aliasing.
|
||||||
if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
|
if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
|
||||||
return MadeChange;
|
break;
|
||||||
|
|
||||||
|
Instruction *Next = llvm::next(BasicBlock::iterator(Dependency));
|
||||||
|
|
||||||
// DCE instructions only used to calculate that store
|
// DCE instructions only used to calculate that store
|
||||||
DeleteDeadInstruction(Dependency, *MD);
|
DeleteDeadInstruction(Dependency, *MD);
|
||||||
@ -578,8 +605,12 @@ bool DSE::HandleFree(CallInst *F) {
|
|||||||
// s[0] = 0;
|
// s[0] = 0;
|
||||||
// s[1] = 0; // This has just been deleted.
|
// s[1] = 0; // This has just been deleted.
|
||||||
// free(s);
|
// free(s);
|
||||||
Dep = MD->getDependency(F);
|
Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
if (Dep.isNonLocal())
|
||||||
|
FindUnconditionalPreds(Blocks, BB, DT);
|
||||||
|
}
|
||||||
|
|
||||||
return MadeChange;
|
return MadeChange;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
target datalayout = "e-p:64:64:64"
|
target datalayout = "e-p:64:64:64"
|
||||||
|
|
||||||
|
declare void @free(i8* nocapture)
|
||||||
|
declare noalias i8* @malloc(i64)
|
||||||
|
|
||||||
; CHECK: @test
|
; CHECK: @test
|
||||||
; CHECK-NEXT: bitcast
|
; CHECK-NEXT: bitcast
|
||||||
; CHECK-NEXT: @free
|
; CHECK-NEXT: @free
|
||||||
@ -26,10 +29,10 @@ define void @test2({i32, i32}* %P) {
|
|||||||
ret void
|
ret void
|
||||||
}
|
}
|
||||||
|
|
||||||
; CHECK: @test4
|
; CHECK: @test3
|
||||||
; CHECK-NOT: store
|
; CHECK-NOT: store
|
||||||
; CHECK: ret void
|
; CHECK: ret void
|
||||||
define void @test4() {
|
define void @test3() {
|
||||||
%m = call i8* @malloc(i64 24)
|
%m = call i8* @malloc(i64 24)
|
||||||
store i8 0, i8* %m
|
store i8 0, i8* %m
|
||||||
%m1 = getelementptr i8* %m, i64 1
|
%m1 = getelementptr i8* %m, i64 1
|
||||||
@ -38,5 +41,20 @@ define void @test4() {
|
|||||||
ret void
|
ret void
|
||||||
}
|
}
|
||||||
|
|
||||||
declare void @free(i8*)
|
; PR11240
|
||||||
declare i8* @malloc(i64)
|
; CHECK: @test4
|
||||||
|
; CHECK-NOT: store
|
||||||
|
; CHECK: ret void
|
||||||
|
define void @test4(i1 %x) nounwind {
|
||||||
|
entry:
|
||||||
|
%alloc1 = tail call noalias i8* @malloc(i64 4) nounwind
|
||||||
|
br i1 %x, label %skipinit1, label %init1
|
||||||
|
|
||||||
|
init1:
|
||||||
|
store i8 1, i8* %alloc1
|
||||||
|
br label %skipinit1
|
||||||
|
|
||||||
|
skipinit1:
|
||||||
|
tail call void @free(i8* %alloc1) nounwind
|
||||||
|
ret void
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user