Add OptimizingUnitTestHelper::GraphChecker methods

GraphChecker should be always used in gtests where it is possible.
Currently only ImprovedOptimizingUnitTest allows unit tests to use
GraphChecker. Unit tests based on OptimizingUnitTest cannot use this
functionality.

Another issue is that GraphChecker has reference type information
checks which unit tests cannot satisfy.

The CL resolves the issues by:
* Adding a public GraphChecker::SetRefTypeInfoCheckEnabled.
* Adding a private OptimizingUnitTestHelper::CheckGraph(HGraph* graph,
bool check_ref_type_info).
* Adding a public OptimizingUnitTestHelper::CheckGraph(graph) to perform
all checks.
* Adding a public
OptimizingUnitTestHelper::CheckGraphSkipRefTypeInfoChecks(graph) to
perform all checks but reference type information checks.
* Updating ImprovedOptimizingUnitTest::CheckGraph to use
OptimizingUnitTestHelper::CheckGraph.

To demonstrate how the new API can be used, unit tests for the
Load-Store-Analysis pass are updated.

Test: test.py --host --optimizing --jit --gtest
Test: test.py --target --optimizing --jit
Test: run-gtests.sh
Change-Id: I7ca0983e66d9904073f0d711b3de96cccfe42746
diff --git a/compiler/optimizing/graph_checker.cc b/compiler/optimizing/graph_checker.cc
index 01d9603..95cfe3e 100644
--- a/compiler/optimizing/graph_checker.cc
+++ b/compiler/optimizing/graph_checker.cc
@@ -497,7 +497,7 @@
   }
 
   // Ensure that reference type instructions have reference type info.
-  if (instruction->GetType() == DataType::Type::kReference) {
+  if (check_reference_type_info_ && instruction->GetType() == DataType::Type::kReference) {
     if (!instruction->GetReferenceTypeInfo().IsValid()) {
       AddError(StringPrintf("Reference type instruction %s:%d does not have "
                             "valid reference type information.",
diff --git a/compiler/optimizing/graph_checker.h b/compiler/optimizing/graph_checker.h
index d085609..564b137 100644
--- a/compiler/optimizing/graph_checker.h
+++ b/compiler/optimizing/graph_checker.h
@@ -95,6 +95,15 @@
     }
   }
 
+  // Enable/Disable the reference type info check.
+  //
+  // Return: the previous status of the check.
+  bool SetRefTypeInfoCheckEnabled(bool value = true) {
+    bool old_value = check_reference_type_info_;
+    check_reference_type_info_ = value;
+    return old_value;
+  }
+
  protected:
   // Report a new error.
   void AddError(const std::string& error) {
@@ -111,6 +120,10 @@
   const char* const dump_prefix_;
   ScopedArenaAllocator allocator_;
   ArenaBitVector seen_ids_;
+  // Whether to perform the reference type info check for instructions which use or produce
+  // object references, e.g. HNewInstance, HLoadClass.
+  // The default value is true.
+  bool check_reference_type_info_ = true;
 
   DISALLOW_COPY_AND_ASSIGN(GraphChecker);
 };
diff --git a/compiler/optimizing/load_store_analysis_test.cc b/compiler/optimizing/load_store_analysis_test.cc
index bfe7a4f..d725aba 100644
--- a/compiler/optimizing/load_store_analysis_test.cc
+++ b/compiler/optimizing/load_store_analysis_test.cc
@@ -106,6 +106,8 @@
   ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
   ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc3));
   ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc3));
+
+  EXPECT_TRUE(CheckGraph(graph_));
 }
 
 TEST_F(LoadStoreAnalysisTest, FieldHeapLocations) {
@@ -183,6 +185,8 @@
   ASSERT_TRUE(loc1 != loc2);
   // accesses to different fields of the same object should not alias.
   ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
+
+  EXPECT_TRUE(CheckGraph(graph_));
 }
 
 TEST_F(LoadStoreAnalysisTest, ArrayIndexAliasingTest) {
@@ -273,6 +277,8 @@
   loc1 = heap_location_collector.GetArrayHeapLocation(arr_set4);
   loc2 = heap_location_collector.GetArrayHeapLocation(arr_set8);
   ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
+
+  EXPECT_TRUE(CheckGraphSkipRefTypeInfoChecks(graph_));
 }
 
 TEST_F(LoadStoreAnalysisTest, ArrayAliasingTest) {
diff --git a/compiler/optimizing/optimizing_unit_test.h b/compiler/optimizing/optimizing_unit_test.h
index 0157126..eb262bc 100644
--- a/compiler/optimizing/optimizing_unit_test.h
+++ b/compiler/optimizing/optimizing_unit_test.h
@@ -180,7 +180,29 @@
     }
   }
 
+  // Run GraphChecker with all checks.
+  //
+  // Return: the status whether the run is successful.
+  bool CheckGraph(HGraph* graph) {
+    return CheckGraph(graph, /*check_ref_type_info=*/true);
+  }
+
+  // Run GraphChecker with all checks except reference type information checks.
+  //
+  // Return: the status whether the run is successful.
+  bool CheckGraphSkipRefTypeInfoChecks(HGraph* graph) {
+    return CheckGraph(graph, /*check_ref_type_info=*/false);
+  }
+
  private:
+  bool CheckGraph(HGraph* graph, bool check_ref_type_info) {
+    GraphChecker checker(graph);
+    checker.SetRefTypeInfoCheckEnabled(check_ref_type_info);
+    checker.Run();
+    checker.Dump(std::cerr);
+    return checker.IsValid();
+  }
+
   std::vector<std::unique_ptr<const StandardDexFile>> dex_files_;
   std::unique_ptr<ArenaPoolAndAllocator> pool_and_allocator_;
   std::unique_ptr<VariableSizedHandleScope> handles_;
@@ -223,15 +245,11 @@
   }
 
   bool CheckGraph() {
-    GraphChecker checker(graph_);
-    checker.Run();
-    if (!checker.IsValid()) {
-      for (const std::string& error : checker.GetErrors()) {
-        std::cout << error << std::endl;
-      }
-      return false;
-    }
-    return true;
+    return OptimizingUnitTestHelper::CheckGraph(graph_);
+  }
+
+  bool CheckGraphSkipRefTypeInfoChecks() {
+    return OptimizingUnitTestHelper::CheckGraphSkipRefTypeInfoChecks(graph_);
   }
 
   HEnvironment* ManuallyBuildEnvFor(HInstruction* instruction,