Add support for eliminating stores that store the same value that was just loaded.

This fixes PR2599.

llvm-svn: 54133
This commit is contained in:
Owen Anderson 2008-07-28 16:14:26 +00:00
parent d70cf1d5ae
commit 3f3389745d
2 changed files with 49 additions and 2 deletions

View File

@ -26,6 +26,7 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Utils/Local.h"
@ -85,9 +86,11 @@ namespace {
// Dependence Graph)
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired<DominatorTree>();
AU.addRequired<TargetData>();
AU.addRequired<AliasAnalysis>();
AU.addRequired<MemoryDependenceAnalysis>();
AU.addPreserved<DominatorTree>();
AU.addPreserved<AliasAnalysis>();
AU.addPreserved<MemoryDependenceAnalysis>();
}
@ -172,8 +175,37 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
// No known stores after the free
last = 0;
} else {
// Update our most-recent-store map.
last = cast<StoreInst>(BBI);
StoreInst* S = cast<StoreInst>(BBI);
// If we're storing the same value back to a pointer that we just
// loaded from, then the store can be removed;
if (LoadInst* L = dyn_cast<LoadInst>(S->getOperand(0))) {
Instruction* dep = MD.getDependency(S);
DominatorTree& DT = getAnalysis<DominatorTree>();
if (S->getParent() == L->getParent() &&
S->getPointerOperand() == L->getPointerOperand() &&
( dep == MemoryDependenceAnalysis::None ||
dep == MemoryDependenceAnalysis::NonLocal ||
DT.dominates(dep, L))) {
if (Instruction* D = dyn_cast<Instruction>(S->getOperand(0)))
possiblyDead.insert(D);
if (Instruction* D = dyn_cast<Instruction>(S->getOperand(1)))
possiblyDead.insert(D);
// Avoid iterator invalidation.
BBI--;
MD.removeInstruction(S);
S->eraseFromParent();
NumFastStores++;
MadeChange = true;
} else
// Update our most-recent-store map.
last = S;
} else
// Update our most-recent-store map.
last = S;
}
}
@ -287,6 +319,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
possiblyDead.insert(D);
BBI++;
MD.removeInstruction(S);
S->eraseFromParent();
NumFastStores++;
MadeChange = true;

View File

@ -0,0 +1,14 @@
; RUN: llvm-as < %s | opt -dse | llvm-dis | not grep tmp5
; PR2599
define void @foo({ i32, i32 }* %x) nounwind {
entry:
%tmp4 = getelementptr { i32, i32 }* %x, i32 0, i32 0 ; <i32*> [#uses=2]
%tmp5 = load i32* %tmp4, align 4 ; <i32> [#uses=1]
%tmp7 = getelementptr { i32, i32 }* %x, i32 0, i32 1 ; <i32*> [#uses=2]
%tmp8 = load i32* %tmp7, align 4 ; <i32> [#uses=1]
%tmp17 = sub i32 0, %tmp8 ; <i32> [#uses=1]
store i32 %tmp5, i32* %tmp4, align 4
store i32 %tmp17, i32* %tmp7, align 4
ret void
}