Teach memdep to use pointsToConstantMemory to determine that loads
authorDan Gohman <gohman@apple.com>
Fri, 29 Oct 2010 01:14:04 +0000 (01:14 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 29 Oct 2010 01:14:04 +0000 (01:14 +0000)
from constant memory don't alias any stores.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@117636 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/MemoryDependenceAnalysis.cpp
test/Analysis/TypeBasedAliasAnalysis/dse.ll

index 5940105a3aaf90f37b014edb3fdde3ac9bdfb762..c72cd1e83a84d86041ce332706af0ea5e607a9b2 100644 (file)
@@ -161,8 +161,9 @@ getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
 }
 
 /// getPointerDependencyFrom - Return the instruction on which a memory
-/// location depends.  If isLoad is true, this routine ignore may-aliases with
-/// read-only operations.
+/// location depends.  If isLoad is true, this routine ignores may-aliases with
+/// read-only operations.  If isLoad is false, this routine ignores may-aliases
+/// with reads from read-only locations.
 MemDepResult MemoryDependenceAnalysis::
 getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad, 
                          BasicBlock::iterator ScanIt, BasicBlock *BB) {
@@ -225,17 +226,21 @@ getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad,
       Value *Pointer = LI->getPointerOperand();
       uint64_t PointerSize = AA->getTypeStoreSize(LI->getType());
       MDNode *TBAATag = LI->getMetadata(LLVMContext::MD_tbaa);
+      AliasAnalysis::Location LoadLoc(Pointer, PointerSize, TBAATag);
       
       // If we found a pointer, check if it could be the same as our pointer.
-      AliasAnalysis::AliasResult R =
-        AA->alias(AliasAnalysis::Location(Pointer, PointerSize, TBAATag),
-                  MemLoc);
+      AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc);
       if (R == AliasAnalysis::NoAlias)
         continue;
       
       // May-alias loads don't depend on each other without a dependence.
       if (isLoad && R == AliasAnalysis::MayAlias)
         continue;
+
+      // Stores don't alias loads from read-only memory.
+      if (!isLoad && AA->pointsToConstantMemory(LoadLoc))
+        continue;
+
       // Stores depend on may and must aliased loads, loads depend on must-alias
       // loads.
       return MemDepResult::getDef(Inst);
index 180149a7ad1b985ae84fe2d52bbfbe6864e71c17..09a7f2c3b7880f73ce036d2a2211096464c727e8 100644 (file)
@@ -25,6 +25,29 @@ define i8 @test0_no(i8* %a, i8* %b) nounwind {
   ret i8 %y
 }
 
+; CHECK: @test1_yes
+; CHECK-NEXT: load i8* %b
+; CHECK-NEXT: store i8 1, i8* %a
+; CHECK-NEXT: ret i8 %y
+define i8 @test1_yes(i8* %a, i8* %b) nounwind {
+  store i8 0, i8* %a
+  %y = load i8* %b, !tbaa !5
+  store i8 1, i8* %a
+  ret i8 %y
+}
+
+; CHECK: @test1_no
+; CHECK-NEXT: store i8 0, i8* %a
+; CHECK-NEXT: load i8* %b
+; CHECK-NEXT: store i8 1, i8* %a
+; CHECK-NEXT: ret i8 %y
+define i8 @test1_no(i8* %a, i8* %b) nounwind {
+  store i8 0, i8* %a
+  %y = load i8* %b, !tbaa !6
+  store i8 1, i8* %a
+  ret i8 %y
+}
+
 ; Root note.
 !0 = metadata !{ }
 ; Some type.