Extend the AliasAnalysis::pointsToConstantMemory interface to allow it
authorDan Gohman <gohman@apple.com>
Mon, 8 Nov 2010 16:45:26 +0000 (16:45 +0000)
committerDan Gohman <gohman@apple.com>
Mon, 8 Nov 2010 16:45:26 +0000 (16:45 +0000)
to optionally look for constant or local (alloca) memory.

Teach BasicAliasAnalysis::pointsToConstantMemory to look through Select
and Phi nodes, and to support looking for local memory.

Remove FunctionAttrs' PointsToLocalOrConstantMemory function, now that
AliasAnalysis knows all the tricks that it knew.

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

include/llvm/Analysis/AliasAnalysis.h
lib/Analysis/AliasAnalysis.cpp
lib/Analysis/AliasAnalysisCounter.cpp
lib/Analysis/AliasDebugger.cpp
lib/Analysis/BasicAliasAnalysis.cpp
lib/Analysis/NoAliasAnalysis.cpp
lib/Analysis/TypeBasedAliasAnalysis.cpp
lib/Transforms/IPO/FunctionAttrs.cpp
test/Transforms/FunctionAttrs/2008-12-29-Constant.ll

index 6a22ad36a59eeb314e3288efe08fba5fc3c895e5..48a93962627f7e56127b0c31c0d4676c14317690 100644 (file)
@@ -154,15 +154,16 @@ public:
     return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
   }
 
-  /// pointsToConstantMemory - If the specified memory location is known to be
-  /// constant, return true.  This allows disambiguation of store
-  /// instructions from constant pointers.
-  ///
-  virtual bool pointsToConstantMemory(const Location &Loc);
+  /// pointsToConstantMemory - If the specified memory location is
+  /// known to be constant, return true. If OrLocal is true and the
+  /// specified memory location is known to be "local" (derived from
+  /// an alloca), return true. Otherwise return false.
+  virtual bool pointsToConstantMemory(const Location &Loc,
+                                      bool OrLocal = false);
 
   /// pointsToConstantMemory - A convenient wrapper.
-  bool pointsToConstantMemory(const Value *P) {
-    return pointsToConstantMemory(Location(P));
+  bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
+    return pointsToConstantMemory(Location(P), OrLocal);
   }
 
   //===--------------------------------------------------------------------===//
index 472d57ab4026f002c6497c1cfdf8c67b4d27e492..d513ebbc2a9d6bef655a18b056dd34ae19ba543f 100644 (file)
@@ -49,9 +49,10 @@ AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
   return AA->alias(LocA, LocB);
 }
 
-bool AliasAnalysis::pointsToConstantMemory(const Location &Loc) {
+bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
+                                           bool OrLocal) {
   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
-  return AA->pointsToConstantMemory(Loc);
+  return AA->pointsToConstantMemory(Loc, OrLocal);
 }
 
 void AliasAnalysis::deleteValue(Value *V) {
index a82d9598295018b5cacaf1a0de6cb3f1d08f6847..23be94ffb08128d2b5fab279e157f4bf014d1e60 100644 (file)
@@ -95,8 +95,8 @@ namespace {
     }
     
     // FIXME: We could count these too...
-    bool pointsToConstantMemory(const Location &Loc) {
-      return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc);
+    bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
+      return getAnalysis<AliasAnalysis>().pointsToConstantMemory(Loc, OrLocal);
     }
 
     // Forwarding functions: just delegate to a real AA implementation, counting
index 4b9aefcb1144631d434ea5ebe88bdc16bd9a12ec..f15c05153e1062ca68f7e77aa92cd5bd7ec2b464 100644 (file)
@@ -113,9 +113,9 @@ namespace {
       return AliasAnalysis::getModRefInfo(CS1,CS2);
     }
     
-    bool pointsToConstantMemory(const Location &Loc) {
+    bool pointsToConstantMemory(const Location &Loc, bool OrLocal) {
       assert(Vals.find(Loc.Ptr) != Vals.end() && "Never seen value in AA before");
-      return AliasAnalysis::pointsToConstantMemory(Loc);
+      return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
     }
 
     virtual void deleteValue(Value *V) {
index 221c8639e0fc748d21fa62bd02581eab92c17703..db493eeedca09100365d13c8f1f5a31272517408 100644 (file)
@@ -456,7 +456,7 @@ namespace {
 
     /// pointsToConstantMemory - Chase pointers until we find a (constant
     /// global) or not.
-    virtual bool pointsToConstantMemory(const Location &Loc);
+    virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
 
     /// getModRefBehavior - Return the behavior when calling the given
     /// call site.
@@ -517,18 +517,61 @@ ImmutablePass *llvm::createBasicAliasAnalysisPass() {
   return new BasicAliasAnalysis();
 }
 
+/// pointsToConstantMemory - Returns whether the given pointer value
+/// points to memory that is local to the function, with global constants being
+/// considered local to all functions.
+bool
+BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc, bool OrLocal) {
+  assert(Visited.empty() && "Visited must be cleared after use!");
 
-/// pointsToConstantMemory - Chase pointers until we find a (constant
-/// global) or not.
-bool BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
-  if (const GlobalVariable *GV = 
-        dyn_cast<GlobalVariable>(Loc.Ptr->getUnderlyingObject()))
-    // Note: this doesn't require GV to be "ODR" because it isn't legal for a
-    // global to be marked constant in some modules and non-constant in others.
-    // GV may even be a declaration, not a definition.
-    return GV->isConstant();
+  SmallVector<const Value *, 16> Worklist;
+  Worklist.push_back(Loc.Ptr);
+  do {
+    const Value *V = Worklist.pop_back_val()->getUnderlyingObject();
+    if (!Visited.insert(V)) {
+      Visited.clear();
+      return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
+    }
+
+    // An alloca instruction defines local memory.
+    if (OrLocal && isa<AllocaInst>(V))
+      continue;
+
+    // A global constant counts as local memory for our purposes.
+    if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
+      // Note: this doesn't require GV to be "ODR" because it isn't legal for a
+      // global to be marked constant in some modules and non-constant in
+      // others.  GV may even be a declaration, not a definition.
+      if (!GV->isConstant()) {
+        Visited.clear();
+        return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
+      }
+      continue;
+    }
+
+    // If both select values point to local memory, then so does the select.
+    if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
+      Worklist.push_back(SI->getTrueValue());
+      Worklist.push_back(SI->getFalseValue());
+      continue;
+    }
+
+    // If all values incoming to a phi node point to local memory, then so does
+    // the phi.
+    if (const PHINode *PN = dyn_cast<PHINode>(V)) {
+      for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
+        Worklist.push_back(PN->getIncomingValue(i));
+      continue;
+    }
+
+    // Otherwise be conservative.
+    Visited.clear();
+    return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
+
+  } while (!Worklist.empty());
 
-  return AliasAnalysis::pointsToConstantMemory(Loc);
+  Visited.clear();
+  return true;
 }
 
 /// getModRefBehavior - Return the behavior when calling the given call site.
index 7602149edc042c308d44fa2682fed57a4cf76c94..ab7a69241e9ae39e60fc15a9bcada04c9e5e0b33 100644 (file)
@@ -50,7 +50,10 @@ namespace {
       return UnknownModRefBehavior;
     }
 
-    virtual bool pointsToConstantMemory(const Location &Loc) { return false; }
+    virtual bool pointsToConstantMemory(const Location &Loc,
+                                        bool OrLocal) {
+      return false;
+    }
     virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
                                        const Location &Loc) {
       return ModRef;
index 0516d64a710f6e35e81288577d9542d6e219052f..044a93f8661a82d220e2179b45c15fdbf668c7d0 100644 (file)
@@ -138,7 +138,7 @@ namespace {
   private:
     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
     virtual AliasResult alias(const Location &LocA, const Location &LocB);
-    virtual bool pointsToConstantMemory(const Location &Loc);
+    virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
     virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
                                        const Location &Loc);
     virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
@@ -225,19 +225,20 @@ TypeBasedAliasAnalysis::alias(const Location &LocA,
   return NoAlias;
 }
 
-bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
+bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc,
+                                                    bool OrLocal) {
   if (!EnableTBAA)
-    return AliasAnalysis::pointsToConstantMemory(Loc);
+    return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
 
   const MDNode *M = Loc.TBAATag;
-  if (!M) return AliasAnalysis::pointsToConstantMemory(Loc);
+  if (!M) return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
 
   // If this is an "immutable" type, we can assume the pointer is pointing
   // to constant memory.
   if (TBAANode(M).TypeIsImmutable())
     return true;
 
-  return AliasAnalysis::pointsToConstantMemory(Loc);
+  return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
 }
 
 AliasAnalysis::ModRefResult
index 062e366158a84a2e80a8c246fe6841f49364c125..be5d827e8e5f2ca59fcdd26a386d6418397b0a70 100644 (file)
@@ -66,8 +66,6 @@ namespace {
       CallGraphSCCPass::getAnalysisUsage(AU);
     }
 
-    bool PointsToLocalOrConstantMemory(Value *V);
-
   private:
     AliasAnalysis *AA;
   };
@@ -83,53 +81,6 @@ INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
 
 
-/// PointsToLocalOrConstantMemory - Returns whether the given pointer value
-/// points to memory that is local to the function, with global constants being
-/// considered local to all functions.
-bool FunctionAttrs::PointsToLocalOrConstantMemory(Value *V) {
-  SmallVector<Value*, 16> Worklist;
-  unsigned MaxLookup = 8;
-
-  Worklist.push_back(V);
-
-  do {
-    V = Worklist.pop_back_val()->getUnderlyingObject();
-
-    // An alloca instruction defines local memory.
-    if (isa<AllocaInst>(V))
-      continue;
-
-    // A global constant counts as local memory for our purposes.
-    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
-      if (!GV->isConstant())
-        return false;
-      continue;
-    }
-
-    // If both select values point to local memory, then so does the select.
-    if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
-      Worklist.push_back(SI->getTrueValue());
-      Worklist.push_back(SI->getFalseValue());
-      continue;
-    }
-
-    // If all values incoming to a phi node point to local memory, then so does
-    // the phi.
-    if (PHINode *PN = dyn_cast<PHINode>(V)) {
-      // Don't bother inspecting phi nodes with many operands.
-      if (PN->getNumIncomingValues() > MaxLookup)
-        return false;
-      for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
-        Worklist.push_back(PN->getIncomingValue(i));
-      continue;
-    }
-
-    return false;
-  } while (!Worklist.empty() && --MaxLookup);
-
-  return Worklist.empty();
-}
-
 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function*, 8> SCCNodes;
@@ -190,7 +141,7 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
                CI != CE; ++CI) {
             Value *Arg = *CI;
             if (Arg->getType()->isPointerTy() &&
-                !PointsToLocalOrConstantMemory(Arg))
+                !AA->pointsToConstantMemory(Arg, /*OrLocal=*/true))
               // Writes memory.  Just give up.
               return false;
           }
@@ -203,12 +154,14 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
       } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
         // Ignore non-volatile loads from local memory.
         if (!LI->isVolatile() &&
-            PointsToLocalOrConstantMemory(LI->getPointerOperand()))
+            AA->pointsToConstantMemory(LI->getPointerOperand(),
+                                       /*OrLocal=*/true))
           continue;
       } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
         // Ignore non-volatile stores to local memory.
         if (!SI->isVolatile() &&
-            PointsToLocalOrConstantMemory(SI->getPointerOperand()))
+            AA->pointsToConstantMemory(SI->getPointerOperand(),
+                                       /*OrLocal=*/true))
           continue;
       }
 
index 672b5e1392b7a411c2d2ce5699a0bafd27761afd..9655da45c646159e91b40bb4a5da1fd5032977f0 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -functionattrs -S | grep readnone
+; RUN: opt < %s -basicaa -functionattrs -S | grep readnone
 
 @s = external constant i8              ; <i8*> [#uses=1]