[llvm-commits] [llvm] r40489 - in /llvm/trunk: include/llvm/Analysis/MemoryDependenceAnalysis.h lib/Analysis/MemoryDependenceAnalysis.cpp lib/Transforms/Scalar/GVN.cpp test/Transforms/GVN/dominated.ll test/Transforms/GVN/nonlocal.ll test/Transforms/GVN/semidominated.ll
Owen Anderson
resistor at mac.com
Wed Jul 25 12:57:03 PDT 2007
Author: resistor
Date: Wed Jul 25 14:57:03 2007
New Revision: 40489
URL: http://llvm.org/viewvc/llvm-project?rev=40489&view=rev
Log:
Add basic support for performing whole-function RLE.
Note: This has not yet been thoroughly tested. Use at your own risk.
Added:
llvm/trunk/test/Transforms/GVN/dominated.ll
llvm/trunk/test/Transforms/GVN/nonlocal.ll
llvm/trunk/test/Transforms/GVN/semidominated.ll
Modified:
llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h
llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
llvm/trunk/lib/Transforms/Scalar/GVN.cpp
Modified: llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h?rev=40489&r1=40488&r2=40489&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h (original)
+++ llvm/trunk/include/llvm/Analysis/MemoryDependenceAnalysis.h Wed Jul 25 14:57:03 2007
@@ -38,7 +38,8 @@
Instruction* getCallSiteDependency(CallSite C, Instruction* start,
bool local = true);
- SmallPtrSet<Instruction*, 4> nonLocalHelper(Instruction* query, BasicBlock* block);
+ bool nonLocalHelper(Instruction* query, BasicBlock* block,
+ DenseMap<BasicBlock*, Value*>& resp);
public:
static Instruction* NonLocal;
@@ -67,7 +68,8 @@
Instruction* getDependency(Instruction* query, Instruction* start = 0,
BasicBlock* block = 0);
- SmallPtrSet<Instruction*, 4> getNonLocalDependency(Instruction* query);
+ bool getNonLocalDependency(Instruction* query,
+ DenseMap<BasicBlock*, Value*>& resp);
/// removeInstruction - Remove an instruction from the dependence analysis,
/// updating the dependence of instructions that previously depended on it.
Modified: llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp?rev=40489&r1=40488&r2=40489&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp Wed Jul 25 14:57:03 2007
@@ -26,8 +26,8 @@
char MemoryDependenceAnalysis::ID = 0;
-Instruction* MemoryDependenceAnalysis::NonLocal = (Instruction*)0;
-Instruction* MemoryDependenceAnalysis::None = (Instruction*)(~0 - 1);
+Instruction* MemoryDependenceAnalysis::NonLocal = (Instruction*)-2;
+Instruction* MemoryDependenceAnalysis::None = (Instruction*)-3;
// Register this pass...
static RegisterPass<MemoryDependenceAnalysis> X("memdep",
@@ -101,52 +101,49 @@
return NonLocal;
}
-SmallPtrSet<Instruction*, 4> MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
- BasicBlock* block) {
- SmallPtrSet<Instruction*, 4> ret;
+bool MemoryDependenceAnalysis::nonLocalHelper(Instruction* query,
+ BasicBlock* block,
+ DenseMap<BasicBlock*, Value*>& resp) {
+ if (resp.count(block))
+ return resp[block] != None;
- Instruction* localDep = getDependency(query, block->end(), block);
+ Instruction* localDep = getDependency(query, 0, block);
if (localDep != NonLocal) {
- ret.insert(localDep);
- return ret;
+ resp.insert(std::make_pair(block, localDep));
+ return true;
}
+ bool inserted = false;
for (pred_iterator PI = pred_begin(block), PE = pred_end(block);
- PI != PE; ++PI) {
- SmallPtrSet<Instruction*, 4> pred_deps = nonLocalHelper(query, *PI);
- for (SmallPtrSet<Instruction*, 4>::iterator I = pred_deps.begin(),
- E = pred_deps.end(); I != E; ++I)
- ret.insert(*I);
- }
+ PI != PE; ++PI)
+ inserted |= nonLocalHelper(query, *PI, resp);
- if (ret.empty())
- ret.insert(None);
+ if (!inserted)
+ resp.insert(std::make_pair(block, None));
- return ret;
+ return inserted;
}
-SmallPtrSet<Instruction*, 4> MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query) {
- SmallPtrSet<Instruction*, 4> ret;
-
+bool MemoryDependenceAnalysis::getNonLocalDependency(Instruction* query,
+ DenseMap<BasicBlock*, Value*>& resp) {
Instruction* localDep = getDependency(query);
if (localDep != NonLocal) {
- ret.insert(localDep);
- return ret;
+ resp.insert(std::make_pair(query->getParent(), localDep));
+ return true;
}
+ bool inserted = false;
+
BasicBlock* parent = query->getParent();
for (pred_iterator PI = pred_begin(parent), PE = pred_end(parent);
PI != PE; ++PI) {
- SmallPtrSet<Instruction*, 4> pred_deps = nonLocalHelper(query, *PI);
- for (SmallPtrSet<Instruction*, 4>::iterator I = pred_deps.begin(),
- E = pred_deps.end(); I != E; ++I)
- ret.insert(*I);
+ inserted |= nonLocalHelper(query, *PI, resp);
}
- if (ret.empty())
- ret.insert(None);
+ if (!inserted)
+ resp.insert(std::make_pair(query->getParent(), None));
- return ret;
+ return inserted;
}
/// getDependency - Return the instruction on which a memory operation
@@ -163,12 +160,14 @@
// If we have a _confirmed_ cached entry, return it
if (cachedResult.second)
return cachedResult.first;
- else if (cachedResult.first != NonLocal)
+ else if (cachedResult.first && cachedResult.first != NonLocal)
// If we have an unconfirmed cached entry, we can start our search from there
QI = cachedResult.first;
if (start)
QI = start;
+ else if (!start && block)
+ QI = block->end();
AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
TargetData& TD = getAnalysis<TargetData>();
@@ -212,7 +211,7 @@
if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
// All volatile loads/stores depend on each other
if (queryIsVolatile && S->isVolatile()) {
- if (!start) {
+ if (!start || block) {
depGraphLocal.insert(std::make_pair(query, std::make_pair(S, true)));
reverseDep.insert(std::make_pair(S, query));
}
@@ -225,7 +224,7 @@
} else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
// All volatile loads/stores depend on each other
if (queryIsVolatile && L->isVolatile()) {
- if (!start) {
+ if (!start || block) {
depGraphLocal.insert(std::make_pair(query, std::make_pair(L, true)));
reverseDep.insert(std::make_pair(L, query));
}
@@ -253,7 +252,7 @@
// Call insts need special handling. Check is they can modify our pointer
if (AA.getModRefInfo(CallSite::get(QI), dependee, dependeeSize) !=
AliasAnalysis::NoModRef) {
- if (!start) {
+ if (!start || block) {
depGraphLocal.insert(std::make_pair(query, std::make_pair(QI, true)));
reverseDep.insert(std::make_pair(QI, query));
}
@@ -270,7 +269,7 @@
dependee, dependeeSize);
if (R != AliasAnalysis::NoAlias) {
- if (!start) {
+ if (!start || block) {
depGraphLocal.insert(std::make_pair(query, std::make_pair(QI, true)));
reverseDep.insert(std::make_pair(QI, query));
}
@@ -281,7 +280,7 @@
}
// If we found nothing, return the non-local flag
- if (!start) {
+ if (!start || block) {
depGraphLocal.insert(std::make_pair(query,
std::make_pair(NonLocal, true)));
reverseDep.insert(std::make_pair(NonLocal, query));
Modified: llvm/trunk/lib/Transforms/Scalar/GVN.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVN.cpp?rev=40489&r1=40488&r2=40489&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/GVN.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/GVN.cpp Wed Jul 25 14:57:03 2007
@@ -15,6 +15,7 @@
#define DEBUG_TYPE "gvn"
#include "llvm/Value.h"
#include "llvm/Transforms/Scalar.h"
+#include "llvm/BasicBlock.h"
#include "llvm/Instructions.h"
#include "llvm/Function.h"
#include "llvm/DerivedTypes.h"
@@ -648,6 +649,10 @@
ValueNumberedSet& currAvail,
DenseMap<Value*, LoadInst*>& lastSeenLoad,
SmallVector<Instruction*, 4>& toErase);
+ bool processNonLocalLoad(LoadInst* L, SmallVector<Instruction*, 4>& toErase);
+ Value *performPHIConstruction(BasicBlock *BB, LoadInst* orig,
+ DenseMap<BasicBlock*, Value*> &Phis);
+ void dump(DenseMap<BasicBlock*, Value*>& d);
};
char GVN::ID = 0;
@@ -687,6 +692,88 @@
s.insert(v);
}
+void GVN::dump(DenseMap<BasicBlock*, Value*>& d) {
+ printf("{\n");
+ for (DenseMap<BasicBlock*, Value*>::iterator I = d.begin(),
+ E = d.end(); I != E; ++I) {
+ if (I->second == MemoryDependenceAnalysis::None)
+ printf("None\n");
+ else
+ I->second->dump();
+ }
+ printf("}\n");
+}
+
+
+Value *GVN::performPHIConstruction(BasicBlock *BB, LoadInst* orig,
+ DenseMap<BasicBlock*, Value*> &Phis) {
+ DenseMap<BasicBlock*, Value*>::iterator DI = Phis.find(BB);
+ if (DI != Phis.end())
+ return DI->second;
+
+ unsigned numPreds = std::distance(pred_begin(BB), pred_end(BB));
+
+ if (numPreds == 1) {
+ Phis[BB] = Phis[*pred_begin(BB)];
+ return Phis[BB];
+ } else {
+ PHINode *PN = new PHINode(orig->getType(), orig->getName()+".rle", BB->begin());
+ PN->reserveOperandSpace(numPreds);
+
+ // Fill in the incoming values for the block.
+ for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
+ PN->addIncoming(performPHIConstruction(*PI, orig, Phis), *PI);
+
+ bool all_same = PN->getNumIncomingValues() != 1;
+ Value* first = PN->getIncomingValue(0);
+ for (unsigned i = 1; i < PN->getNumIncomingValues(); ++i)
+ all_same &= (PN->getIncomingValue(i) == first);
+
+ if (all_same) {
+ PN->eraseFromParent();
+ return first;
+ } else {
+ return PN;
+ }
+ }
+}
+
+bool GVN::processNonLocalLoad(LoadInst* L, SmallVector<Instruction*, 4>& toErase) {
+ MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+
+ DenseMap<BasicBlock*, Value*> deps;
+ bool ret = MD.getNonLocalDependency(L, deps);
+ if (!ret)
+ return false;
+
+ DenseMap<BasicBlock*, Value*> repl;
+ for (DenseMap<BasicBlock*, Value*>::iterator I = deps.begin(), E = deps.end();
+ I != E; ++I)
+ if (I->second == MemoryDependenceAnalysis::None) {
+ return false;
+ } else if (StoreInst* S = dyn_cast<StoreInst>(I->second)) {
+ if (S->getPointerOperand() == L->getPointerOperand())
+ repl.insert(std::make_pair(I->first, S->getOperand(0)));
+ else
+ return false;
+ } else if (LoadInst* LD = dyn_cast<LoadInst>(I->second)) {
+ if (LD->getPointerOperand() == L->getPointerOperand())
+ repl.insert(std::make_pair(I->first, LD));
+ else
+ return false;
+ } else {
+ return false;
+ }
+
+ Value* v = performPHIConstruction(L->getParent(), L, repl);
+
+ MD.removeInstruction(L);
+ L->replaceAllUsesWith(v);
+ toErase.push_back(L);
+
+ return true;
+}
+
bool GVN::processLoad(LoadInst* L,
DenseMap<Value*, LoadInst*>& lastLoad,
SmallVector<Instruction*, 4>& toErase) {
@@ -701,6 +788,9 @@
// ... to a pointer that has been loaded from before...
MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
Instruction* dep = MD.getDependency(L);
+ if (dep == MemoryDependenceAnalysis::NonLocal &&
+ L->getParent() != &L->getParent()->getParent()->getEntryBlock())
+ processNonLocalLoad(L, toErase);
bool deletedLoad = false;
while (dep != MemoryDependenceAnalysis::None &&
@@ -802,15 +892,17 @@
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
BI != BE; ++BI) {
- processInstruction(BI, currAvail, lastSeenLoad, toErase);
+ changed_function |= processInstruction(BI, currAvail, lastSeenLoad, toErase);
+
+ NumGVNInstr += toErase.size();
+
+ for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
+ E = toErase.end(); I != E; ++I)
+ (*I)->eraseFromParent();
+
+ toErase.clear();
}
}
- NumGVNInstr = toErase.size();
-
- for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
- E = toErase.end(); I != E; ++I)
- (*I)->eraseFromParent();
-
return changed_function;
}
Added: llvm/trunk/test/Transforms/GVN/dominated.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVN/dominated.ll?rev=40489&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GVN/dominated.ll (added)
+++ llvm/trunk/test/Transforms/GVN/dominated.ll Wed Jul 25 14:57:03 2007
@@ -0,0 +1,20 @@
+; RUN: llvm-as < %s | opt -gvn | llvm-dis | not grep DEAD
+
+define i32 @main(i32** %p) {
+block1:
+ %z = load i32** %p
+ br i1 true, label %block2, label %block3
+
+block2:
+ %a = load i32** %p
+ br label %block4
+
+block3:
+ %b = load i32** %p
+ br label %block4
+
+block4:
+ %DEAD = load i32** %p
+ %c = load i32* %DEAD
+ ret i32 %c
+}
\ No newline at end of file
Added: llvm/trunk/test/Transforms/GVN/nonlocal.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVN/nonlocal.ll?rev=40489&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GVN/nonlocal.ll (added)
+++ llvm/trunk/test/Transforms/GVN/nonlocal.ll Wed Jul 25 14:57:03 2007
@@ -0,0 +1,19 @@
+; RUN: llvm-as < %s | opt -gvn | llvm-dis | not grep {DEAD =}
+
+define i32 @main(i32** %p) {
+block1:
+ br i1 true, label %block2, label %block3
+
+block2:
+ %a = load i32** %p
+ br label %block4
+
+block3:
+ %b = load i32** %p
+ br label %block4
+
+block4:
+ %DEAD = load i32** %p
+ %c = load i32* %DEAD
+ ret i32 %c
+}
\ No newline at end of file
Added: llvm/trunk/test/Transforms/GVN/semidominated.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVN/semidominated.ll?rev=40489&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GVN/semidominated.ll (added)
+++ llvm/trunk/test/Transforms/GVN/semidominated.ll Wed Jul 25 14:57:03 2007
@@ -0,0 +1,19 @@
+; RUN: llvm-as < %s | opt -gvn | llvm-dis | not grep {DEAD =}
+
+define i32 @main(i32* %p) {
+block1:
+ %z = load i32* %p
+ br i1 true, label %block2, label %block3
+
+block2:
+ br label %block4
+
+block3:
+ %b = bitcast i32 0 to i32
+ store i32 %b, i32* %p
+ br label %block4
+
+block4:
+ %DEAD = load i32* %p
+ ret i32 %DEAD
+}
\ No newline at end of file
More information about the llvm-commits
mailing list