ART: Use ScopedArenaAllocator for pass-local data.

Passes using local ArenaAllocator were hiding their memory
usage from the allocation counting, making it difficult to
track down where memory was used. Using ScopedArenaAllocator
reveals the memory usage.

This changes the HGraph constructor which requires a lot of
changes in tests. Refactor these tests to limit the amount
of work needed the next time we change that constructor.

Test: m test-art-host-gtest
Test: testrunner.py --host
Test: Build with kArenaAllocatorCountAllocations = true.
Bug: 64312607
Change-Id: I34939e4086b500d6e827ff3ef2211d1a421ac91a
diff --git a/compiler/optimizing/scheduler_test.cc b/compiler/optimizing/scheduler_test.cc
index 0e6e0c5..7e1ec70 100644
--- a/compiler/optimizing/scheduler_test.cc
+++ b/compiler/optimizing/scheduler_test.cc
@@ -71,16 +71,14 @@
   return v;
 }
 
-class SchedulerTest : public CommonCompilerTest {
+class SchedulerTest : public OptimizingUnitTest {
  public:
-  SchedulerTest() : pool_(), allocator_(&pool_) {
-    graph_ = CreateGraph(&allocator_);
-  }
+  SchedulerTest() : graph_(CreateGraph()) { }
 
   // Build scheduling graph, and run target specific scheduling on it.
   void TestBuildDependencyGraphAndSchedule(HScheduler* scheduler) {
-    HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_);
-    HBasicBlock* block1 = new (&allocator_) HBasicBlock(graph_);
+    HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_);
+    HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_);
     graph_->AddBlock(entry);
     graph_->AddBlock(block1);
     graph_->SetEntryBlock(entry);
@@ -100,23 +98,25 @@
     // array_get2    ArrayGet [array, add1]
     // array_set2    ArraySet [array, add1, add2]
 
-    HInstruction* array = new (&allocator_) HParameterValue(graph_->GetDexFile(),
-                                                            dex::TypeIndex(0),
-                                                            0,
-                                                            DataType::Type::kReference);
+    HInstruction* array = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
+                                                           dex::TypeIndex(0),
+                                                           0,
+                                                           DataType::Type::kReference);
     HInstruction* c1 = graph_->GetIntConstant(1);
     HInstruction* c2 = graph_->GetIntConstant(10);
-    HInstruction* add1 = new (&allocator_) HAdd(DataType::Type::kInt32, c1, c2);
-    HInstruction* add2 = new (&allocator_) HAdd(DataType::Type::kInt32, add1, c2);
-    HInstruction* mul = new (&allocator_) HMul(DataType::Type::kInt32, add1, add2);
-    HInstruction* div_check = new (&allocator_) HDivZeroCheck(add2, 0);
-    HInstruction* div = new (&allocator_) HDiv(DataType::Type::kInt32, add1, div_check, 0);
-    HInstruction* array_get1 = new (&allocator_) HArrayGet(array, add1, DataType::Type::kInt32, 0);
+    HInstruction* add1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, c1, c2);
+    HInstruction* add2 = new (GetAllocator()) HAdd(DataType::Type::kInt32, add1, c2);
+    HInstruction* mul = new (GetAllocator()) HMul(DataType::Type::kInt32, add1, add2);
+    HInstruction* div_check = new (GetAllocator()) HDivZeroCheck(add2, 0);
+    HInstruction* div = new (GetAllocator()) HDiv(DataType::Type::kInt32, add1, div_check, 0);
+    HInstruction* array_get1 =
+        new (GetAllocator()) HArrayGet(array, add1, DataType::Type::kInt32, 0);
     HInstruction* array_set1 =
-        new (&allocator_) HArraySet(array, add1, add2, DataType::Type::kInt32, 0);
-    HInstruction* array_get2 = new (&allocator_) HArrayGet(array, add1, DataType::Type::kInt32, 0);
+        new (GetAllocator()) HArraySet(array, add1, add2, DataType::Type::kInt32, 0);
+    HInstruction* array_get2 =
+        new (GetAllocator()) HArrayGet(array, add1, DataType::Type::kInt32, 0);
     HInstruction* array_set2 =
-        new (&allocator_) HArraySet(array, add1, add2, DataType::Type::kInt32, 0);
+        new (GetAllocator()) HArraySet(array, add1, add2, DataType::Type::kInt32, 0);
 
     DCHECK(div_check->CanThrow());
 
@@ -135,18 +135,19 @@
       block1->AddInstruction(instr);
     }
 
-    HEnvironment* environment = new (&allocator_) HEnvironment(&allocator_,
-                                                               2,
-                                                               graph_->GetArtMethod(),
-                                                               0,
-                                                               div_check);
+    HEnvironment* environment = new (GetAllocator()) HEnvironment(GetAllocator(),
+                                                                  2,
+                                                                  graph_->GetArtMethod(),
+                                                                  0,
+                                                                  div_check);
     div_check->SetRawEnvironment(environment);
     environment->SetRawEnvAt(0, add2);
     add2->AddEnvUseAt(div_check->GetEnvironment(), 0);
     environment->SetRawEnvAt(1, mul);
     mul->AddEnvUseAt(div_check->GetEnvironment(), 1);
 
-    SchedulingGraph scheduling_graph(scheduler, graph_->GetArena());
+    ScopedArenaAllocator allocator(graph_->GetArenaStack());
+    SchedulingGraph scheduling_graph(scheduler, &allocator);
     // Instructions must be inserted in reverse order into the scheduling graph.
     for (HInstruction* instr : ReverseRange(block_instructions)) {
       scheduling_graph.AddNode(instr);
@@ -184,7 +185,7 @@
 
   void CompileWithRandomSchedulerAndRun(const uint16_t* data, bool has_result, int expected) {
     for (CodegenTargetConfig target_config : GetTargetConfigs()) {
-      HGraph* graph = CreateCFG(&allocator_, data);
+      HGraph* graph = CreateCFG(data);
 
       // Schedule the graph randomly.
       HInstructionScheduling scheduling(graph, target_config.GetInstructionSet());
@@ -198,55 +199,57 @@
   }
 
   void TestDependencyGraphOnAliasingArrayAccesses(HScheduler* scheduler) {
-    HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_);
+    HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_);
     graph_->AddBlock(entry);
     graph_->SetEntryBlock(entry);
     graph_->BuildDominatorTree();
 
-    HInstruction* arr = new (&allocator_) HParameterValue(graph_->GetDexFile(),
-                                                          dex::TypeIndex(0),
-                                                          0,
-                                                          DataType::Type::kReference);
-    HInstruction* i = new (&allocator_) HParameterValue(graph_->GetDexFile(),
-                                                        dex::TypeIndex(1),
-                                                        1,
-                                                        DataType::Type::kInt32);
-    HInstruction* j = new (&allocator_) HParameterValue(graph_->GetDexFile(),
-                                                        dex::TypeIndex(1),
-                                                        1,
-                                                        DataType::Type::kInt32);
-    HInstruction* object = new (&allocator_) HParameterValue(graph_->GetDexFile(),
+    HInstruction* arr = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
                                                              dex::TypeIndex(0),
                                                              0,
                                                              DataType::Type::kReference);
+    HInstruction* i = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
+                                                           dex::TypeIndex(1),
+                                                           1,
+                                                           DataType::Type::kInt32);
+    HInstruction* j = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
+                                                           dex::TypeIndex(1),
+                                                           1,
+                                                           DataType::Type::kInt32);
+    HInstruction* object = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
+                                                                dex::TypeIndex(0),
+                                                                0,
+                                                                DataType::Type::kReference);
     HInstruction* c0 = graph_->GetIntConstant(0);
     HInstruction* c1 = graph_->GetIntConstant(1);
-    HInstruction* add0 = new (&allocator_) HAdd(DataType::Type::kInt32, i, c0);
-    HInstruction* add1 = new (&allocator_) HAdd(DataType::Type::kInt32, i, c1);
-    HInstruction* sub0 = new (&allocator_) HSub(DataType::Type::kInt32, i, c0);
-    HInstruction* sub1 = new (&allocator_) HSub(DataType::Type::kInt32, i, c1);
-    HInstruction* arr_set_0 = new (&allocator_) HArraySet(arr, c0, c0, DataType::Type::kInt32, 0);
-    HInstruction* arr_set_1 = new (&allocator_) HArraySet(arr, c1, c0, DataType::Type::kInt32, 0);
-    HInstruction* arr_set_i = new (&allocator_) HArraySet(arr, i, c0, DataType::Type::kInt32, 0);
+    HInstruction* add0 = new (GetAllocator()) HAdd(DataType::Type::kInt32, i, c0);
+    HInstruction* add1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, i, c1);
+    HInstruction* sub0 = new (GetAllocator()) HSub(DataType::Type::kInt32, i, c0);
+    HInstruction* sub1 = new (GetAllocator()) HSub(DataType::Type::kInt32, i, c1);
+    HInstruction* arr_set_0 =
+        new (GetAllocator()) HArraySet(arr, c0, c0, DataType::Type::kInt32, 0);
+    HInstruction* arr_set_1 =
+        new (GetAllocator()) HArraySet(arr, c1, c0, DataType::Type::kInt32, 0);
+    HInstruction* arr_set_i = new (GetAllocator()) HArraySet(arr, i, c0, DataType::Type::kInt32, 0);
     HInstruction* arr_set_add0 =
-        new (&allocator_) HArraySet(arr, add0, c0, DataType::Type::kInt32, 0);
+        new (GetAllocator()) HArraySet(arr, add0, c0, DataType::Type::kInt32, 0);
     HInstruction* arr_set_add1 =
-        new (&allocator_) HArraySet(arr, add1, c0, DataType::Type::kInt32, 0);
+        new (GetAllocator()) HArraySet(arr, add1, c0, DataType::Type::kInt32, 0);
     HInstruction* arr_set_sub0 =
-        new (&allocator_) HArraySet(arr, sub0, c0, DataType::Type::kInt32, 0);
+        new (GetAllocator()) HArraySet(arr, sub0, c0, DataType::Type::kInt32, 0);
     HInstruction* arr_set_sub1 =
-        new (&allocator_) HArraySet(arr, sub1, c0, DataType::Type::kInt32, 0);
-    HInstruction* arr_set_j = new (&allocator_) HArraySet(arr, j, c0, DataType::Type::kInt32, 0);
-    HInstanceFieldSet* set_field10 = new (&allocator_) HInstanceFieldSet(object,
-                                                                         c1,
-                                                                         nullptr,
-                                                                         DataType::Type::kInt32,
-                                                                         MemberOffset(10),
-                                                                         false,
-                                                                         kUnknownFieldIndex,
-                                                                         kUnknownClassDefIndex,
-                                                                         graph_->GetDexFile(),
-                                                                         0);
+        new (GetAllocator()) HArraySet(arr, sub1, c0, DataType::Type::kInt32, 0);
+    HInstruction* arr_set_j = new (GetAllocator()) HArraySet(arr, j, c0, DataType::Type::kInt32, 0);
+    HInstanceFieldSet* set_field10 = new (GetAllocator()) HInstanceFieldSet(object,
+                                                                            c1,
+                                                                            nullptr,
+                                                                            DataType::Type::kInt32,
+                                                                            MemberOffset(10),
+                                                                            false,
+                                                                            kUnknownFieldIndex,
+                                                                            kUnknownClassDefIndex,
+                                                                            graph_->GetDexFile(),
+                                                                            0);
 
     HInstruction* block_instructions[] = {arr,
                                           i,
@@ -270,7 +273,8 @@
       entry->AddInstruction(instr);
     }
 
-    SchedulingGraph scheduling_graph(scheduler, graph_->GetArena());
+    ScopedArenaAllocator allocator(graph_->GetArenaStack());
+    SchedulingGraph scheduling_graph(scheduler, &allocator);
     HeapLocationCollector heap_location_collector(graph_);
     heap_location_collector.VisitBasicBlock(entry);
     heap_location_collector.BuildAliasingMatrix();
@@ -342,21 +346,21 @@
     scheduler->Schedule(graph_);
   }
 
-  ArenaPool pool_;
-  ArenaAllocator allocator_;
   HGraph* graph_;
 };
 
 #if defined(ART_ENABLE_CODEGEN_arm64)
 TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM64) {
   CriticalPathSchedulingNodeSelector critical_path_selector;
-  arm64::HSchedulerARM64 scheduler(&allocator_, &critical_path_selector);
+  ScopedArenaAllocator allocator(GetArenaStack());
+  arm64::HSchedulerARM64 scheduler(&allocator, &critical_path_selector);
   TestBuildDependencyGraphAndSchedule(&scheduler);
 }
 
 TEST_F(SchedulerTest, ArrayAccessAliasingARM64) {
   CriticalPathSchedulingNodeSelector critical_path_selector;
-  arm64::HSchedulerARM64 scheduler(&allocator_, &critical_path_selector);
+  ScopedArenaAllocator allocator(GetArenaStack());
+  arm64::HSchedulerARM64 scheduler(&allocator, &critical_path_selector);
   TestDependencyGraphOnAliasingArrayAccesses(&scheduler);
 }
 #endif
@@ -365,14 +369,16 @@
 TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM) {
   CriticalPathSchedulingNodeSelector critical_path_selector;
   arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr);
-  arm::HSchedulerARM scheduler(&allocator_, &critical_path_selector, &arm_latency_visitor);
+  ScopedArenaAllocator allocator(GetArenaStack());
+  arm::HSchedulerARM scheduler(&allocator, &critical_path_selector, &arm_latency_visitor);
   TestBuildDependencyGraphAndSchedule(&scheduler);
 }
 
 TEST_F(SchedulerTest, ArrayAccessAliasingARM) {
   CriticalPathSchedulingNodeSelector critical_path_selector;
   arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr);
-  arm::HSchedulerARM scheduler(&allocator_, &critical_path_selector, &arm_latency_visitor);
+  ScopedArenaAllocator allocator(GetArenaStack());
+  arm::HSchedulerARM scheduler(&allocator, &critical_path_selector, &arm_latency_visitor);
   TestDependencyGraphOnAliasingArrayAccesses(&scheduler);
 }
 #endif