Make ModRefBehavior a lattice. Use this to clean up AliasAnalysis
authorDan Gohman <gohman@apple.com>
Wed, 10 Nov 2010 01:02:18 +0000 (01:02 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 10 Nov 2010 01:02:18 +0000 (01:02 +0000)
chaining and simplify FunctionAttrs' GetModRefBehavior logic.

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

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

index 841513b715855511913848a8a4d31227fdae2f85..6b716ddee9478dde6b64166eaf1511e657a9cb2e 100644 (file)
@@ -175,6 +175,9 @@ public:
   ///
   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 @@ public:
     /// 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 @@ public:
     /// 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 @@ public:
   /// 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.
index 7f9c9c169e0f6dc140f8a71d3d386f9a3de28987..363da9fcee61370a2229650421575797eb0802b5 100644 (file)
@@ -179,7 +179,7 @@ AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
 
   // 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
index 73114fe5aab1277c40be4d9959fe77e7cd8f0172..bf1b689587c9a61089ef654472d5306978d892a7 100644 (file)
@@ -595,7 +595,7 @@ BasicAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
     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 @@ BasicAliasAnalysis::getModRefBehavior(const Function *F) {
 #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 @@ BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
       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 @@ BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
       if (isNoAlias(Location(Dest, Len), Loc)) {
         if (isNoAlias(Location(Src, Len), Loc))
           return NoModRef;
-        return Ref;
+        Min = Ref;
       }
       break;
     }
@@ -745,7 +749,7 @@ BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
     }
 
   // 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
index 6e4fff7b529cd9d9f904715ac831700f8e8407f5..456a80a972b3814acd9db9d9fa42130fbc03b176 100644 (file)
@@ -120,28 +120,33 @@ namespace {
     /// 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);
index 16ff64e4cfba33e3d4b2dc3e1d763fdb6580aa68..46c9aa95a9b574e3e76be148f277ae12156cb114 100644 (file)
@@ -256,11 +256,12 @@ TypeBasedAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
     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);
 }
 
index 8bdf5d75dadf2b07de0f8bfb66dac949bf130908..3b1b13d378fcd8c000c1119f081492787ec849ec 100644 (file)
@@ -128,54 +128,41 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
         // 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()) {
index 535a1d0fba6b01c6d3ab84e20622ba082cc04f33..946453f586ede9a5dbddeaa7ee597dc69c6bad92 100644 (file)
@@ -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
index b455fdd8c3ec0c162a366f7472da74afb28909cb..22eca132041520020054a54fb6dcfbf2b1de27f4 100644 (file)
@@ -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: