[llvm-commits] [llvm] r54133 - in /llvm/trunk: lib/Transforms/Scalar/DeadStoreElimination.cpp test/Transforms/DeadStoreElimination/2008-07-28-load-store.ll
Owen Anderson
resistor at mac.com
Mon Jul 28 09:14:43 PDT 2008
Author: resistor
Date: Mon Jul 28 11:14:26 2008
New Revision: 54133
URL: http://llvm.org/viewvc/llvm-project?rev=54133&view=rev
Log:
Add support for eliminating stores that store the same value that was just loaded.
This fixes PR2599.
Added:
llvm/trunk/test/Transforms/DeadStoreElimination/2008-07-28-load-store.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp?rev=54133&r1=54132&r2=54133&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/DeadStoreElimination.cpp Mon Jul 28 11:14:26 2008
@@ -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 @@
// 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 @@
// 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 @@
possiblyDead.insert(D);
BBI++;
+ MD.removeInstruction(S);
S->eraseFromParent();
NumFastStores++;
MadeChange = true;
Added: llvm/trunk/test/Transforms/DeadStoreElimination/2008-07-28-load-store.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/DeadStoreElimination/2008-07-28-load-store.ll?rev=54133&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/DeadStoreElimination/2008-07-28-load-store.ll (added)
+++ llvm/trunk/test/Transforms/DeadStoreElimination/2008-07-28-load-store.ll Mon Jul 28 11:14:26 2008
@@ -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
+}
More information about the llvm-commits
mailing list