[llvm-commits] [llvm] r118412 - in /llvm/trunk: 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

Dan Gohman gohman at apple.com
Mon Nov 8 08:45:26 PST 2010


Author: djg
Date: Mon Nov  8 10:45:26 2010
New Revision: 118412

URL: http://llvm.org/viewvc/llvm-project?rev=118412&view=rev
Log:
Extend the AliasAnalysis::pointsToConstantMemory interface to allow it
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.

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

Modified: llvm/trunk/include/llvm/Analysis/AliasAnalysis.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/AliasAnalysis.h?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/AliasAnalysis.h (original)
+++ llvm/trunk/include/llvm/Analysis/AliasAnalysis.h Mon Nov  8 10:45:26 2010
@@ -154,15 +154,16 @@
     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);
   }
 
   //===--------------------------------------------------------------------===//

Modified: llvm/trunk/lib/Analysis/AliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/AliasAnalysis.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/AliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/AliasAnalysis.cpp Mon Nov  8 10:45:26 2010
@@ -49,9 +49,10 @@
   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) {

Modified: llvm/trunk/lib/Analysis/AliasAnalysisCounter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/AliasAnalysisCounter.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/AliasAnalysisCounter.cpp (original)
+++ llvm/trunk/lib/Analysis/AliasAnalysisCounter.cpp Mon Nov  8 10:45:26 2010
@@ -95,8 +95,8 @@
     }
     
     // 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

Modified: llvm/trunk/lib/Analysis/AliasDebugger.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/AliasDebugger.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/AliasDebugger.cpp (original)
+++ llvm/trunk/lib/Analysis/AliasDebugger.cpp Mon Nov  8 10:45:26 2010
@@ -113,9 +113,9 @@
       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) {

Modified: llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp Mon Nov  8 10:45:26 2010
@@ -456,7 +456,7 @@
 
     /// 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 @@
   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!");
+
+  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);
 
-/// 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();
+  } while (!Worklist.empty());
 
-  return AliasAnalysis::pointsToConstantMemory(Loc);
+  Visited.clear();
+  return true;
 }
 
 /// getModRefBehavior - Return the behavior when calling the given call site.

Modified: llvm/trunk/lib/Analysis/NoAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/NoAliasAnalysis.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/NoAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/NoAliasAnalysis.cpp Mon Nov  8 10:45:26 2010
@@ -50,7 +50,10 @@
       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;

Modified: llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp Mon Nov  8 10:45:26 2010
@@ -138,7 +138,7 @@
   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 @@
   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

Modified: llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp Mon Nov  8 10:45:26 2010
@@ -66,8 +66,6 @@
       CallGraphSCCPass::getAnalysisUsage(AU);
     }
 
-    bool PointsToLocalOrConstantMemory(Value *V);
-
   private:
     AliasAnalysis *AA;
   };
@@ -83,53 +81,6 @@
 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 @@
                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 @@
       } 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;
       }
 

Modified: llvm/trunk/test/Transforms/FunctionAttrs/2008-12-29-Constant.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/FunctionAttrs/2008-12-29-Constant.ll?rev=118412&r1=118411&r2=118412&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/FunctionAttrs/2008-12-29-Constant.ll (original)
+++ llvm/trunk/test/Transforms/FunctionAttrs/2008-12-29-Constant.ll Mon Nov  8 10:45:26 2010
@@ -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]
 





More information about the llvm-commits mailing list