Allow GVN to eliminate read-only function calls when it can detect that they are...
authorOwen Anderson <resistor@mac.com>
Mon, 26 Nov 2007 02:26:36 +0000 (02:26 +0000)
committerOwen Anderson <resistor@mac.com>
Mon, 26 Nov 2007 02:26:36 +0000 (02:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44323 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/MemoryDependenceAnalysis.cpp
lib/Transforms/Scalar/GVN.cpp
test/Analysis/BasicAA/pure-const-dce.ll

index 5375d52c33c626e2afa90fbd8220c69853371397..68366f6d912dcaf17ef11c18f5482aa6bd224e3e 100644 (file)
@@ -79,9 +79,6 @@ Instruction* MemoryDependenceAnalysis::getCallSiteDependency(CallSite C,
     if (StoreInst* S = dyn_cast<StoreInst>(QI)) {
       pointer = S->getPointerOperand();
       pointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
-    } else if (LoadInst* L = dyn_cast<LoadInst>(QI)) {
-      pointer = L->getPointerOperand();
-      pointerSize = TD.getTypeStoreSize(L->getType());
     } else if (AllocationInst* AI = dyn_cast<AllocationInst>(QI)) {
       pointer = AI;
       if (ConstantInt* C = dyn_cast<ConstantInt>(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<CallInst>(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)));
index 7cbd617a2ebfca45ead3d1199542ca1fa82e885d..11fa336c7bbeb707f23ec5f706edd57ad7485d1b 100644 (file)
@@ -476,7 +476,8 @@ uint32_t ValueTable::lookup_or_add(Value* V) {
   
   if (CallInst* C = dyn_cast<CallInst>(V)) {
     if (C->getCalledFunction() &&
-        AA->doesNotAccessMemory(C->getCalledFunction())) {
+        (AA->doesNotAccessMemory(C->getCalledFunction()) ||
+         AA->onlyReadsMemory(C->getCalledFunction()))) {
       Expression e = create_expression(C);
     
       DenseMap<Expression, uint32_t>::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<CallInst>(I)) {
+      AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
+      if (CI->getCalledFunction() &&
+          !AA.doesNotAccessMemory(CI->getCalledFunction())) {
+        MemoryDependenceAnalysis& MD = getAnalysis<MemoryDependenceAnalysis>();
+        if (MD.getDependency(CI) != MD.getDependency(cast<CallInst>(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);
index 6abc7da30e4d3893b2d2238edc365eae0523194f..b01b5c5cb81c339b8b6f88b37a5bd16709c207be 100644 (file)
@@ -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              ; <i32*> [#uses=1]