From 00a6d1448d27b5140b911caf2eca9585abdae5c8 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Mon, 26 Nov 2007 02:26:36 +0000 Subject: [PATCH] Allow GVN to eliminate read-only function calls when it can detect that they are redundant. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44323 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/MemoryDependenceAnalysis.cpp | 9 +++++---- lib/Transforms/Scalar/GVN.cpp | 19 ++++++++++++++++++- test/Analysis/BasicAA/pure-const-dce.ll | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/Analysis/MemoryDependenceAnalysis.cpp b/lib/Analysis/MemoryDependenceAnalysis.cpp index 5375d52c33c..68366f6d912 100644 --- a/lib/Analysis/MemoryDependenceAnalysis.cpp +++ b/lib/Analysis/MemoryDependenceAnalysis.cpp @@ -79,9 +79,6 @@ Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C, if (StoreInst* S = dyn_cast(QI)) { pointer = S->getPointerOperand(); pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType()); - } else if (LoadInst* L = dyn_cast(QI)) { - pointer = L->getPointerOperand(); - pointerSize = TD.getTypeStoreSize(L->getType()); } else if (AllocationInst* AI = dyn_cast(QI)) { pointer = AI; if (ConstantInt* C = dyn_cast(AI->getArraySize())) @@ -98,7 +95,11 @@ Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C, // FreeInsts erase the entire structure pointerSize = ~0UL; } else if (CallSite::get(QI).getInstruction() != 0) { - if (AA.getModRefInfo(C, CallSite::get(QI)) != AliasAnalysis::NoModRef) { + AliasAnalysis::ModRefBehavior result = + AA.getModRefBehavior(cast(QI)->getCalledFunction(), + CallSite::get(QI)); + if (result != AliasAnalysis::DoesNotAccessMemory && + result != AliasAnalysis::OnlyReadsMemory) { if (!start && !block) { depGraphLocal.insert(std::make_pair(C.getInstruction(), std::make_pair(QI, true))); diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 7cbd617a2eb..11fa336c7bb 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -476,7 +476,8 @@ uint32_t ValueTable::lookup_or_add(Value* V) { if (CallInst* C = dyn_cast(V)) { if (C->getCalledFunction() && - AA->doesNotAccessMemory(C->getCalledFunction())) { + (AA->doesNotAccessMemory(C->getCalledFunction()) || + AA->onlyReadsMemory(C->getCalledFunction()))) { Expression e = create_expression(C); DenseMap::iterator EI = expressionNumbering.find(e); @@ -1038,6 +1039,22 @@ bool GVN::processInstruction(Instruction* I, } else if (currAvail.test(num)) { Value* repl = find_leader(currAvail, num); + if (CallInst* CI = dyn_cast(I)) { + AliasAnalysis& AA = getAnalysis(); + if (CI->getCalledFunction() && + !AA.doesNotAccessMemory(CI->getCalledFunction())) { + MemoryDependenceAnalysis& MD = getAnalysis(); + if (MD.getDependency(CI) != MD.getDependency(cast(repl))) { + // There must be an intervening may-alias store, so nothing from + // this point on will be able to be replaced with the preceding call + currAvail.erase(repl); + currAvail.insert(I); + + return false; + } + } + } + VN.erase(I); I->replaceAllUsesWith(repl); toErase.push_back(I); diff --git a/test/Analysis/BasicAA/pure-const-dce.ll b/test/Analysis/BasicAA/pure-const-dce.ll index 6abc7da30e4..b01b5c5cb81 100644 --- a/test/Analysis/BasicAA/pure-const-dce.ll +++ b/test/Analysis/BasicAA/pure-const-dce.ll @@ -1,5 +1,5 @@ ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestConst | count 2 -; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestPure | not count 2 +; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestPure | count 3 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestNone | count 4 @g = global i32 0 ; [#uses=1] -- 2.34.1