[llvm] r304945 - GlobalsModRef: Ensure optnone+readonly/readnone attributes are respected

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 7 14:37:39 PDT 2017


Author: dblaikie
Date: Wed Jun  7 16:37:39 2017
New Revision: 304945

URL: http://llvm.org/viewvc/llvm-project?rev=304945&view=rev
Log:
GlobalsModRef: Ensure optnone+readonly/readnone attributes are respected

Modified:
    llvm/trunk/lib/Analysis/GlobalsModRef.cpp
    llvm/trunk/unittests/Analysis/GlobalsModRefTest.cpp

Modified: llvm/trunk/lib/Analysis/GlobalsModRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/GlobalsModRef.cpp?rev=304945&r1=304944&r2=304945&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/GlobalsModRef.cpp (original)
+++ llvm/trunk/lib/Analysis/GlobalsModRef.cpp Wed Jun  7 16:37:39 2017
@@ -477,8 +477,7 @@ void GlobalsAAResult::AnalyzeCallGraph(C
 
     Function *F = SCC[0]->getFunction();
 
-    if (!F || !F->isDefinitionExact() ||
-        F->hasFnAttribute(Attribute::OptimizeNone)) {
+    if (!F || !F->isDefinitionExact()) {
       // Calls externally or not exact - can't say anything useful. Remove any
       // existing function records (may have been created when scanning
       // globals).
@@ -498,7 +497,7 @@ void GlobalsAAResult::AnalyzeCallGraph(C
         break;
       }
 
-      if (F->isDeclaration()) {
+      if (F->isDeclaration() || F->hasFnAttribute(Attribute::OptimizeNone)) {
         // Try to get mod/ref behaviour from function attributes.
         if (F->doesNotAccessMemory()) {
           // Can't do better than that!
@@ -549,12 +548,10 @@ void GlobalsAAResult::AnalyzeCallGraph(C
         break; // The mod/ref lattice saturates here.
 
       // Don't prove any properties based on the implementation of an optnone
-      // function.
-      if (Node->getFunction()->hasFnAttribute(Attribute::OptimizeNone)) {
-        FI.addModRefInfo(MRI_Ref);
-        FI.addModRefInfo(MRI_Mod);
+      // function. Function attributes were already used as a best approximation
+      // above.
+      if (Node->getFunction()->hasFnAttribute(Attribute::OptimizeNone))
         continue;
-      }
 
       for (Instruction &I : instructions(Node->getFunction())) {
         if (FI.getModRefInfo() == MRI_ModRef)

Modified: llvm/trunk/unittests/Analysis/GlobalsModRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/GlobalsModRefTest.cpp?rev=304945&r1=304944&r2=304945&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/GlobalsModRefTest.cpp (original)
+++ llvm/trunk/unittests/Analysis/GlobalsModRefTest.cpp Wed Jun  7 16:37:39 2017
@@ -16,7 +16,13 @@ using namespace llvm;
 
 TEST(GlobalsModRef, OptNone) {
   StringRef Assembly = R"(
-    define void @f() optnone {
+    define void @f1() optnone {
+      ret void
+    }
+    define void @f2() optnone readnone {
+      ret void
+    }
+    define void @f3() optnone readonly {
       ret void
     }
   )";
@@ -27,9 +33,14 @@ TEST(GlobalsModRef, OptNone) {
   ASSERT_TRUE(M) << "Bad assembly?";
 
   const auto &funcs = M->functions();
-  ASSERT_NE(funcs.begin(), funcs.end());
-  EXPECT_EQ(std::next(funcs.begin()), funcs.end());
-  const Function &F = *funcs.begin();
+  auto I = funcs.begin();
+  ASSERT_NE(I, funcs.end());
+  const Function &F1 = *I;
+  ASSERT_NE(++I, funcs.end());
+  const Function &F2 = *I;
+  ASSERT_NE(++I, funcs.end());
+  const Function &F3 = *I;
+  EXPECT_EQ(++I, funcs.end());
 
   Triple Trip(M->getTargetTriple());
   TargetLibraryInfoImpl TLII(Trip);
@@ -37,5 +48,8 @@ TEST(GlobalsModRef, OptNone) {
   llvm::CallGraph CG(*M);
 
   auto AAR = GlobalsAAResult::analyzeModule(*M, TLI, CG);
-  EXPECT_EQ(FMRB_UnknownModRefBehavior, AAR.getModRefBehavior(&F));
+
+  EXPECT_EQ(FMRB_UnknownModRefBehavior, AAR.getModRefBehavior(&F1));
+  EXPECT_EQ(FMRB_DoesNotAccessMemory, AAR.getModRefBehavior(&F2));
+  EXPECT_EQ(FMRB_OnlyReadsMemory, AAR.getModRefBehavior(&F3));
 }




More information about the llvm-commits mailing list