[PATCH] D13512: [GlobalsAA] Loosen an overly conservative bailout

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 07:33:27 PDT 2015


jmolloy created this revision.
jmolloy added reviewers: chandlerc, hfinkel.
jmolloy added a subscriber: llvm-commits.
jmolloy set the repository for this revision to rL LLVM.

When checking if a Value can alias a non-addr-taken global, if the Value is a LoadInst, then it cannot alias the global.

In order for the two to alias, the Value must be a pointer within the
global object. If that pointer has just been loaded from memory, it
cannot possibly alias the global as we know the global's address could
never have ended up in memory in the first place.

I've thrown all the testing I have at this, and it hasn't triggered any miscompares anywhere... but that's no evidence that there isn't a counterexample to my logic above.

Hal and Chandler - I've thought about this a *lot* and I think my logic is correct, but would you guys mind validating it? Thanks!

Repository:
  rL LLVM

http://reviews.llvm.org/D13512

Files:
  lib/Analysis/GlobalsModRef.cpp
  test/Analysis/GlobalsModRef/nonescaping-noalias.ll

Index: test/Analysis/GlobalsModRef/nonescaping-noalias.ll
===================================================================
--- test/Analysis/GlobalsModRef/nonescaping-noalias.ll
+++ test/Analysis/GlobalsModRef/nonescaping-noalias.ll
@@ -97,3 +97,20 @@
   %v = load i32, i32* @g1
   ret i32 %v
 }
+
+define i32 @test5(i32** %param) {
+; Ensure that we can fold a store to a load of a global across a store to
+; a parameter that has been dereferenced when the global is non-escaping.
+;
+; CHECK-LABEL: @test5(
+; CHECK: %p = load i32*
+; CHECK: store i32 42, i32* @g1
+; CHECK-NOT: load i32
+; CHECK: ret i32 42
+entry:
+  %p = load i32*, i32** %param
+  store i32 42, i32* @g1
+  store i32 7, i32* %p
+  %v = load i32, i32* @g1
+  ret i32 %v
+}
Index: lib/Analysis/GlobalsModRef.cpp
===================================================================
--- lib/Analysis/GlobalsModRef.cpp
+++ lib/Analysis/GlobalsModRef.cpp
@@ -666,14 +666,13 @@
       // non-addr-taken globals.
       continue;
     }
-    if (auto *LI = dyn_cast<LoadInst>(Input)) {
-      // A pointer loaded from a global would have been captured, and we know
-      // that the global is non-escaping, so no alias.
-      if (isa<GlobalValue>(GetUnderlyingObject(LI->getPointerOperand(), DL)))
-        continue;
-
-      // Otherwise, a load could come from anywhere, so bail.
-      return false;
+    if (isa<LoadInst>(Input)) {
+      // For a loaded pointer to alias with a an object, it must contain the
+      // address of that object plus some offset inside the object. As we know
+      // the object in this case has never had its address taken, its address
+      // cannot have ended up in memory to be loaded and therefore the loaded
+      // value does not alias with the non-addr-taken global.
+      continue;
     }
 
     // Recurse through a limited number of selects and PHIs. This is an


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13512.36740.patch
Type: text/x-patch
Size: 1884 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151007/e34fbac8/attachment.bin>


More information about the llvm-commits mailing list