A global variable with internal linkage where all uses are in one function and whose...
authorAnders Carlsson <andersca@mac.com>
Wed, 23 Mar 2011 02:19:48 +0000 (02:19 +0000)
committerAnders Carlsson <andersca@mac.com>
Wed, 23 Mar 2011 02:19:48 +0000 (02:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@128140 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/BasicAliasAnalysis.cpp
test/Analysis/BasicAA/non-escaping-local-object.ll [new file with mode: 0644]

index f7bcd9ec44d809c8a49d40b80566d1b2992a732b..e807933aaaffffa27d2d303f3cadb3ee5f56fb64 100644 (file)
@@ -57,6 +57,29 @@ static bool isKnownNonNull(const Value *V) {
   return false;
 }
 
+/// areAllUsesInOneFunction - Return true if all the uses of the given value
+/// are in the same function. Note that this returns false if any of the uses
+/// are from non-instruction values.
+static bool areAllUsesInOneFunction(const Value *V) {
+  const llvm::Function *Fn = 0;
+
+  for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); 
+       UI != E; ++UI) {
+    if (const Instruction *I = dyn_cast<Instruction>(*UI)) {
+      if (!Fn) {
+        Fn = I->getParent()->getParent();
+        continue;
+      } 
+      
+      if (Fn != I->getParent()->getParent())
+        return false;
+    } else
+      return false;
+  }
+
+  return true;
+}
+
 /// isNonEscapingLocalObject - Return true if the pointer is to a function-local
 /// object that never escapes from the function.
 static bool isNonEscapingLocalObject(const Value *V) {
@@ -79,6 +102,16 @@ static bool isNonEscapingLocalObject(const Value *V) {
         return true;
       return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
     }
+
+  // If this is an internal global variable that's only used in this function,
+  // check if it escapes the function.
+  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
+    if (GV->hasInternalLinkage() && areAllUsesInOneFunction(GV)) {
+      return !PointerMayBeCaptured(V, /*ReturnCaptures=*/true, 
+                                   /*StoreCaptures=*/true);
+    }
+  }
+
   return false;
 }
 
diff --git a/test/Analysis/BasicAA/non-escaping-local-object.ll b/test/Analysis/BasicAA/non-escaping-local-object.ll
new file mode 100644 (file)
index 0000000..f5b73b6
--- /dev/null
@@ -0,0 +1,19 @@
+; RUN: opt -basicaa -aa-eval -print-all-alias-modref-info -disable-output < %s |& FileCheck  %s
+
+@global = internal global i32 0
+
+declare void @should_not_be_called()
+declare i32 @f()
+
+; CHECK: Function: g: 2 pointers, 0 call sites
+define void @g(i32* nocapture %p) {
+  store i32 0, i32* @global
+
+  ; @global is internal, is only used in this function, and never has its
+  ; address taken so it can't alias p.
+  ; CHECK: NoAlias:    i32* %p, i32* @global
+  store i32 1, i32* %p
+  %1 = load i32* @global
+  ret void
+}
+