[ADT] Don't use a fixture just to get a nonce type for this unittest.
[oota-llvm.git] / unittests / IR / PassManagerTest.cpp
index 157c881ea59b8eabea2779b2c80297dda2b7fd35..41af0b0bd25c4165b98b34b4e4a3679619a8a765 100644 (file)
@@ -29,13 +29,16 @@ public:
   /// \brief Returns an opaque, unique ID for this pass type.
   static void *ID() { return (void *)&PassID; }
 
+  /// \brief Returns the name of the analysis.
+  static StringRef name() { return "TestFunctionAnalysis"; }
+
   TestFunctionAnalysis(int &Runs) : Runs(Runs) {}
 
   /// \brief Run the analysis pass over the function and return a result.
-  Result run(Function *F, FunctionAnalysisManager *AM) {
+  Result run(Function &F, FunctionAnalysisManager *AM) {
     ++Runs;
     int Count = 0;
-    for (Function::iterator BBI = F->begin(), BBE = F->end(); BBI != BBE; ++BBI)
+    for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
       for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
            ++II)
         ++Count;
@@ -58,14 +61,16 @@ public:
     int FunctionCount;
   };
 
-  static void *ID() { return (void * )&PassID; }
+  static void *ID() { return (void *)&PassID; }
+
+  static StringRef name() { return "TestModuleAnalysis"; }
 
   TestModuleAnalysis(int &Runs) : Runs(Runs) {}
 
-  Result run(Module *M, ModuleAnalysisManager *AM) {
+  Result run(Module &M, ModuleAnalysisManager *AM) {
     ++Runs;
     int Count = 0;
-    for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+    for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
       ++Count;
     return Result(Count);
   }
@@ -81,7 +86,7 @@ char TestModuleAnalysis::PassID;
 struct TestModulePass {
   TestModulePass(int &RunCount) : RunCount(RunCount) {}
 
-  PreservedAnalyses run(Module *M) {
+  PreservedAnalyses run(Module &M) {
     ++RunCount;
     return PreservedAnalyses::none();
   }
@@ -92,15 +97,13 @@ struct TestModulePass {
 };
 
 struct TestPreservingModulePass {
-  PreservedAnalyses run(Module *M) {
-    return PreservedAnalyses::all();
-  }
+  PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
 
   static StringRef name() { return "TestPreservingModulePass"; }
 };
 
 struct TestMinPreservingModulePass {
-  PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM) {
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager *AM) {
     PreservedAnalyses PA;
 
     // Force running an analysis.
@@ -121,13 +124,13 @@ struct TestFunctionPass {
         AnalyzedFunctionCount(AnalyzedFunctionCount),
         OnlyUseCachedResults(OnlyUseCachedResults) {}
 
-  PreservedAnalyses run(Function *F, FunctionAnalysisManager *AM) {
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager *AM) {
     ++RunCount;
 
     const ModuleAnalysisManager &MAM =
         AM->getResult<ModuleAnalysisManagerFunctionProxy>(F).getManager();
     if (TestModuleAnalysis::Result *TMA =
-            MAM.getCachedResult<TestModuleAnalysis>(F->getParent()))
+            MAM.getCachedResult<TestModuleAnalysis>(*F.getParent()))
       AnalyzedFunctionCount += TMA->FunctionCount;
 
     if (OnlyUseCachedResults) {
@@ -157,9 +160,9 @@ struct TestFunctionPass {
 struct TestInvalidationFunctionPass {
   TestInvalidationFunctionPass(StringRef FunctionName) : Name(FunctionName) {}
 
-  PreservedAnalyses run(Function *F) {
-    return F->getName() == Name ? PreservedAnalyses::none()
-                                : PreservedAnalyses::all();
+  PreservedAnalyses run(Function &F) {
+    return F.getName() == Name ? PreservedAnalyses::none()
+                               : PreservedAnalyses::all();
   }
 
   static StringRef name() { return "TestInvalidationFunctionPass"; }
@@ -167,10 +170,10 @@ struct TestInvalidationFunctionPass {
   StringRef Name;
 };
 
-Module *parseIR(const char *IR) {
+std::unique_ptr<Module> parseIR(const char *IR) {
   LLVMContext &C = getGlobalContext();
   SMDiagnostic Err;
-  return ParseAssemblyString(IR, 0, Err, C);
+  return parseAssemblyString(IR, Err, C);
 }
 
 class PassManagerTest : public ::testing::Test {
@@ -193,6 +196,39 @@ public:
                   "}\n")) {}
 };
 
+TEST_F(PassManagerTest, BasicPreservedAnalyses) {
+  PreservedAnalyses PA1 = PreservedAnalyses();
+  EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>());
+  EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>());
+  PreservedAnalyses PA2 = PreservedAnalyses::none();
+  EXPECT_FALSE(PA2.preserved<TestFunctionAnalysis>());
+  EXPECT_FALSE(PA2.preserved<TestModuleAnalysis>());
+  PreservedAnalyses PA3 = PreservedAnalyses::all();
+  EXPECT_TRUE(PA3.preserved<TestFunctionAnalysis>());
+  EXPECT_TRUE(PA3.preserved<TestModuleAnalysis>());
+  PreservedAnalyses PA4 = PA1;
+  EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>());
+  EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>());
+  PA4 = PA3;
+  EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>());
+  EXPECT_TRUE(PA4.preserved<TestModuleAnalysis>());
+  PA4 = std::move(PA2);
+  EXPECT_FALSE(PA4.preserved<TestFunctionAnalysis>());
+  EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>());
+  PA4.preserve<TestFunctionAnalysis>();
+  EXPECT_TRUE(PA4.preserved<TestFunctionAnalysis>());
+  EXPECT_FALSE(PA4.preserved<TestModuleAnalysis>());
+  PA1.preserve<TestModuleAnalysis>();
+  EXPECT_FALSE(PA1.preserved<TestFunctionAnalysis>());
+  EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>());
+  PA1.preserve<TestFunctionAnalysis>();
+  EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>());
+  EXPECT_TRUE(PA1.preserved<TestModuleAnalysis>());
+  PA1.intersect(PA4);
+  EXPECT_TRUE(PA1.preserved<TestFunctionAnalysis>());
+  EXPECT_FALSE(PA1.preserved<TestModuleAnalysis>());
+}
+
 TEST_F(PassManagerTest, Basic) {
   FunctionAnalysisManager FAM;
   int FunctionAnalysisRuns = 0;
@@ -211,10 +247,18 @@ TEST_F(PassManagerTest, Basic) {
   int AnalyzedInstrCount1 = 0;
   int AnalyzedFunctionCount1 = 0;
   {
+    // Pointless scoped copy to test move assignment.
+    ModulePassManager NestedMPM;
     FunctionPassManager FPM;
-    FPM.addPass(TestFunctionPass(FunctionPassRunCount1, AnalyzedInstrCount1,
-                                 AnalyzedFunctionCount1));
-    MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
+    {
+      // Pointless scope to test move assignment.
+      FunctionPassManager NestedFPM;
+      NestedFPM.addPass(TestFunctionPass(FunctionPassRunCount1, AnalyzedInstrCount1,
+                                   AnalyzedFunctionCount1));
+      FPM = std::move(NestedFPM);
+    }
+    NestedMPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
+    MPM = std::move(NestedMPM);
   }
 
   // Count the runs over a module.
@@ -271,7 +315,7 @@ TEST_F(PassManagerTest, Basic) {
     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
   }
 
-  MPM.run(M.get(), &MAM);
+  MPM.run(*M, &MAM);
 
   // Validate module pass counters.
   EXPECT_EQ(1, ModulePassRunCount);