[llvm-commits] [llvm] r118660 - in /llvm/trunk: include/llvm/Analysis/AliasAnalysis.h lib/Analysis/AliasAnalysis.cpp lib/Analysis/BasicAliasAnalysis.cpp lib/Analysis/IPA/GlobalsModRef.cpp lib/Analysis/TypeBasedAliasAnalysis.cpp lib/Transforms/IPO/FunctionAttrs.cpp test/Transforms/FunctionAttrs/2008-09-03-ReadNone.ll test/Transforms/FunctionAttrs/2008-09-03-ReadOnly.ll

Dan Gohman gohman at apple.com
Tue Nov 9 17:02:18 PST 2010


Author: djg
Date: Tue Nov  9 19:02:18 2010
New Revision: 118660

URL: http://llvm.org/viewvc/llvm-project?rev=118660&view=rev
Log:
Make ModRefBehavior a lattice. Use this to clean up AliasAnalysis
chaining and simplify FunctionAttrs' GetModRefBehavior logic.

Modified:
    llvm/trunk/include/llvm/Analysis/AliasAnalysis.h
    llvm/trunk/lib/Analysis/AliasAnalysis.cpp
    llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp
    llvm/trunk/lib/Analysis/IPA/GlobalsModRef.cpp
    llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp
    llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp
    llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadNone.ll
    llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadOnly.ll

Modified: llvm/trunk/include/llvm/Analysis/AliasAnalysis.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/AliasAnalysis.h?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/AliasAnalysis.h (original)
+++ llvm/trunk/include/llvm/Analysis/AliasAnalysis.h Tue Nov  9 19:02:18 2010
@@ -175,6 +175,9 @@
   ///
   enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
 
+  /// These values define additional bits used to define the
+  /// ModRefBehavior values.
+  enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
 
   /// ModRefBehavior - Summary of how a function affects memory in the program.
   /// Loads from constant globals are not considered memory accesses for this
@@ -187,20 +190,20 @@
     /// This property corresponds to the GCC 'const' attribute.
     /// This property corresponds to the LLVM IR 'readnone' attribute.
     /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
-    DoesNotAccessMemory,
+    DoesNotAccessMemory = Nowhere | NoModRef,
 
     /// AccessesArgumentsReadonly - This function loads through function
     /// arguments and does not perform any non-local stores or volatile
     /// loads.
     ///
     /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
-    AccessesArgumentsReadonly,
+    AccessesArgumentsReadonly = ArgumentPointees | Ref,
 
     /// AccessesArguments - This function accesses function arguments in well
     /// known (possibly volatile) ways, but does not access any other memory.
     ///
     /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
-    AccessesArguments,
+    AccessesArguments = ArgumentPointees | ModRef,
 
     /// OnlyReadsMemory - This function does not perform any non-local stores or
     /// volatile loads, but may read from any memory location.
@@ -208,11 +211,11 @@
     /// This property corresponds to the GCC 'pure' attribute.
     /// This property corresponds to the LLVM IR 'readonly' attribute.
     /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
-    OnlyReadsMemory,
+    OnlyReadsMemory = Anywhere | Ref,
 
     /// UnknownModRefBehavior - This indicates that the function could not be
     /// classified into one of the behaviors above.
-    UnknownModRefBehavior
+    UnknownModRefBehavior = Anywhere | ModRef
   };
 
   /// getModRefBehavior - Return the behavior when calling the given call site.
@@ -270,12 +273,9 @@
   /// true.  For use when the call site is not known.
   ///
   static bool onlyReadsMemory(ModRefBehavior MRB) {
-    return MRB == DoesNotAccessMemory ||
-           MRB == AccessesArgumentsReadonly ||
-           MRB == OnlyReadsMemory;
+    return !(MRB & Mod);
   }
 
-
   /// getModRefInfo - Return information about whether or not an instruction may
   /// read or write the specified memory location.  An instruction
   /// that doesn't read or write memory may be trivially LICM'd for example.

Modified: llvm/trunk/lib/Analysis/AliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/AliasAnalysis.cpp?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/AliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/AliasAnalysis.cpp Tue Nov  9 19:02:18 2010
@@ -179,7 +179,7 @@
 
   // Otherwise, fall back to the next AA in the chain. But we can merge
   // in any result we've managed to compute.
-  return std::min(AA->getModRefBehavior(CS), Min);
+  return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
 }
 
 AliasAnalysis::ModRefBehavior

Modified: llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp Tue Nov  9 19:02:18 2010
@@ -595,7 +595,7 @@
     Min = OnlyReadsMemory;
 
   // The AliasAnalysis base class has some smarts, lets use them.
-  return std::min(AliasAnalysis::getModRefBehavior(CS), Min);
+  return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
 }
 
 /// getModRefBehavior - Return the behavior when calling the given function.
@@ -613,12 +613,14 @@
 #undef GET_INTRINSIC_MODREF_BEHAVIOR
   }
 
+  ModRefBehavior Min = UnknownModRefBehavior;
+
   // If the function declares it only reads memory, go with that.
   if (F->onlyReadsMemory())
-    return OnlyReadsMemory;
+    Min = OnlyReadsMemory;
 
   // Otherwise be conservative.
-  return AliasAnalysis::getModRefBehavior(F);
+  return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
 }
 
 /// getModRefInfo - Check to see if the specified callsite can clobber the
@@ -671,6 +673,8 @@
       return NoModRef;
   }
 
+  ModRefResult Min = ModRef;
+
   // Finally, handle specific knowledge of intrinsics.
   const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
   if (II != 0)
@@ -686,7 +690,7 @@
       if (isNoAlias(Location(Dest, Len), Loc)) {
         if (isNoAlias(Location(Src, Len), Loc))
           return NoModRef;
-        return Ref;
+        Min = Ref;
       }
       break;
     }
@@ -745,7 +749,7 @@
     }
 
   // The AliasAnalysis base class has some smarts, lets use them.
-  return AliasAnalysis::getModRefInfo(CS, Loc);
+  return ModRefResult(AliasAnalysis::getModRefInfo(CS, Loc) & Min);
 }
 
 /// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction

Modified: llvm/trunk/lib/Analysis/IPA/GlobalsModRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/IPA/GlobalsModRef.cpp?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/IPA/GlobalsModRef.cpp (original)
+++ llvm/trunk/lib/Analysis/IPA/GlobalsModRef.cpp Tue Nov  9 19:02:18 2010
@@ -120,28 +120,33 @@
     /// called from the specified call site.  The call site may be null in which
     /// case the most generic behavior of this function should be returned.
     ModRefBehavior getModRefBehavior(const Function *F) {
+      ModRefBehavior Min = UnknownModRefBehavior;
+
       if (FunctionRecord *FR = getFunctionInfo(F)) {
         if (FR->FunctionEffect == 0)
-          return DoesNotAccessMemory;
+          Min = DoesNotAccessMemory;
         else if ((FR->FunctionEffect & Mod) == 0)
-          return OnlyReadsMemory;
+          Min = OnlyReadsMemory;
       }
-      return AliasAnalysis::getModRefBehavior(F);
+
+      return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
     }
     
     /// getModRefBehavior - Return the behavior of the specified function if
     /// called from the specified call site.  The call site may be null in which
     /// case the most generic behavior of this function should be returned.
     ModRefBehavior getModRefBehavior(ImmutableCallSite CS) {
-      const Function* F = CS.getCalledFunction();
-      if (!F) return AliasAnalysis::getModRefBehavior(CS);
-      if (FunctionRecord *FR = getFunctionInfo(F)) {
-        if (FR->FunctionEffect == 0)
-          return DoesNotAccessMemory;
-        else if ((FR->FunctionEffect & Mod) == 0)
-          return OnlyReadsMemory;
-      }
-      return AliasAnalysis::getModRefBehavior(CS);
+      ModRefBehavior Min = UnknownModRefBehavior;
+
+      if (const Function* F = CS.getCalledFunction())
+        if (FunctionRecord *FR = getFunctionInfo(F)) {
+          if (FR->FunctionEffect == 0)
+            Min = DoesNotAccessMemory;
+          else if ((FR->FunctionEffect & Mod) == 0)
+            Min = OnlyReadsMemory;
+        }
+
+      return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
     }
 
     virtual void deleteValue(Value *V);

Modified: llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/TypeBasedAliasAnalysis.cpp Tue Nov  9 19:02:18 2010
@@ -256,11 +256,12 @@
     if (TBAANode(M).TypeIsImmutable())
       Min = OnlyReadsMemory;
 
-  return std::min(AliasAnalysis::getModRefBehavior(CS), Min);
+  return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
 }
 
 AliasAnalysis::ModRefBehavior
 TypeBasedAliasAnalysis::getModRefBehavior(const Function *F) {
+  // Functions don't have metadata. Just chain to the next implementation.
   return AliasAnalysis::getModRefBehavior(F);
 }
 

Modified: llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/FunctionAttrs.cpp Tue Nov  9 19:02:18 2010
@@ -128,54 +128,41 @@
         // Ignore calls to functions in the same SCC.
         if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
           continue;
-        switch (AA->getModRefBehavior(CS)) {
-        case AliasAnalysis::DoesNotAccessMemory:
-          // Ignore calls that don't access memory.
-          continue;
-        case AliasAnalysis::OnlyReadsMemory:
-          // Handle calls that only read from memory.
-          ReadsMemory = true;
-          continue;
-        case AliasAnalysis::AccessesArguments:
-          // Check whether all pointer arguments point to local memory, and
-          // ignore calls that only access local memory.
-          for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
-               CI != CE; ++CI) {
-            Value *Arg = *CI;
-            if (Arg->getType()->isPointerTy()) {
-              AliasAnalysis::Location Loc(Arg,
-                                          AliasAnalysis::UnknownSize,
-                                          I->getMetadata(LLVMContext::MD_tbaa));
-              if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
-                // Writes memory.  Just give up.
-                return false;
-            }
-          }
-          // Only reads and writes local memory.
-          continue;
-        case AliasAnalysis::AccessesArgumentsReadonly:
-          // Check whether all pointer arguments point to local memory, and
-          // ignore calls that only access local memory.
-          for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
-               CI != CE; ++CI) {
-            Value *Arg = *CI;
-            if (Arg->getType()->isPointerTy()) {
-              AliasAnalysis::Location Loc(Arg,
-                                          AliasAnalysis::UnknownSize,
-                                          I->getMetadata(LLVMContext::MD_tbaa));
-              if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
-                // Reads non-local memory.
-                ReadsMemory = true;
-                break;
+        AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
+        // If the call doesn't access arbitrary memory, we may be able to
+        // figure out something.
+        if (!(MRB & AliasAnalysis::Anywhere &
+              ~AliasAnalysis::ArgumentPointees)) {
+          // If the call accesses argument pointees, check each argument.
+          if (MRB & AliasAnalysis::AccessesArguments)
+            // Check whether all pointer arguments point to local memory, and
+            // ignore calls that only access local memory.
+            for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
+                 CI != CE; ++CI) {
+              Value *Arg = *CI;
+              if (Arg->getType()->isPointerTy()) {
+                AliasAnalysis::Location Loc(Arg,
+                                            AliasAnalysis::UnknownSize,
+                                            I->getMetadata(LLVMContext::MD_tbaa));
+                if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
+                  if (MRB & AliasAnalysis::Mod)
+                    // Writes non-local memory.  Give up.
+                    return false;
+                  if (MRB & AliasAnalysis::Ref)
+                    // Ok, it reads non-local memory.
+                    ReadsMemory = true;
+                }
               }
             }
-          }
-          // Only reads memory.
           continue;
-        default:
-          // Otherwise, be conservative.
-          break;
         }
+        // The call could access any memory. If that includes writes, give up.
+        if (MRB & AliasAnalysis::Mod)
+          return false;
+        // If it reads, note it.
+        if (MRB & AliasAnalysis::Ref)
+          ReadsMemory = true;
+        continue;
       } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
         // Ignore non-volatile loads from local memory.
         if (!LI->isVolatile()) {

Modified: llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadNone.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadNone.ll?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadNone.ll (original)
+++ llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadNone.ll Tue Nov  9 19:02:18 2010
@@ -1,4 +1,4 @@
-; RUN: opt < %s -functionattrs -S | grep readnone | count 4
+; RUN: opt < %s -basicaa -functionattrs -S | grep readnone | count 4
 @x = global i32 0
 
 declare i32 @e() readnone

Modified: llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadOnly.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadOnly.ll?rev=118660&r1=118659&r2=118660&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadOnly.ll (original)
+++ llvm/trunk/test/Transforms/FunctionAttrs/2008-09-03-ReadOnly.ll Tue Nov  9 19:02:18 2010
@@ -1,4 +1,4 @@
-; RUN: opt < %s -functionattrs -S | grep readonly | count 2
+; RUN: opt < %s -basicaa -functionattrs -S | grep readonly | count 2
 
 define i32 @f() {
 entry:





More information about the llvm-commits mailing list