1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "scheduler.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "base/arena_allocator.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
21*795d594fSAndroid Build Coastguard Worker #include "builder.h"
22*795d594fSAndroid Build Coastguard Worker #include "codegen_test_utils.h"
23*795d594fSAndroid Build Coastguard Worker #include "common_compiler_test.h"
24*795d594fSAndroid Build Coastguard Worker #include "load_store_analysis.h"
25*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
26*795d594fSAndroid Build Coastguard Worker #include "optimizing_unit_test.h"
27*795d594fSAndroid Build Coastguard Worker #include "pc_relative_fixups_x86.h"
28*795d594fSAndroid Build Coastguard Worker #include "register_allocator.h"
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker #ifdef ART_ENABLE_CODEGEN_arm64
31*795d594fSAndroid Build Coastguard Worker #include "scheduler_arm64.h"
32*795d594fSAndroid Build Coastguard Worker #endif
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker #ifdef ART_ENABLE_CODEGEN_arm
35*795d594fSAndroid Build Coastguard Worker #include "scheduler_arm.h"
36*795d594fSAndroid Build Coastguard Worker #endif
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
39*795d594fSAndroid Build Coastguard Worker
40*795d594fSAndroid Build Coastguard Worker // Return all combinations of ISA and code generator that are executable on
41*795d594fSAndroid Build Coastguard Worker // hardware, or on simulator, and that we'd like to test.
GetTargetConfigs()42*795d594fSAndroid Build Coastguard Worker static ::std::vector<CodegenTargetConfig> GetTargetConfigs() {
43*795d594fSAndroid Build Coastguard Worker ::std::vector<CodegenTargetConfig> v;
44*795d594fSAndroid Build Coastguard Worker ::std::vector<CodegenTargetConfig> test_config_candidates = {
45*795d594fSAndroid Build Coastguard Worker #ifdef ART_ENABLE_CODEGEN_arm
46*795d594fSAndroid Build Coastguard Worker // TODO: Should't this be `kThumb2` instead of `kArm` here?
47*795d594fSAndroid Build Coastguard Worker CodegenTargetConfig(InstructionSet::kArm, create_codegen_arm_vixl32),
48*795d594fSAndroid Build Coastguard Worker #endif
49*795d594fSAndroid Build Coastguard Worker #ifdef ART_ENABLE_CODEGEN_arm64
50*795d594fSAndroid Build Coastguard Worker CodegenTargetConfig(InstructionSet::kArm64, create_codegen_arm64),
51*795d594fSAndroid Build Coastguard Worker #endif
52*795d594fSAndroid Build Coastguard Worker #ifdef ART_ENABLE_CODEGEN_x86
53*795d594fSAndroid Build Coastguard Worker CodegenTargetConfig(InstructionSet::kX86, create_codegen_x86),
54*795d594fSAndroid Build Coastguard Worker #endif
55*795d594fSAndroid Build Coastguard Worker #ifdef ART_ENABLE_CODEGEN_x86_64
56*795d594fSAndroid Build Coastguard Worker CodegenTargetConfig(InstructionSet::kX86_64, create_codegen_x86_64),
57*795d594fSAndroid Build Coastguard Worker #endif
58*795d594fSAndroid Build Coastguard Worker };
59*795d594fSAndroid Build Coastguard Worker
60*795d594fSAndroid Build Coastguard Worker for (const CodegenTargetConfig& test_config : test_config_candidates) {
61*795d594fSAndroid Build Coastguard Worker if (CanExecuteISA(test_config.GetInstructionSet())) {
62*795d594fSAndroid Build Coastguard Worker v.push_back(test_config);
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker return v;
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker
69*795d594fSAndroid Build Coastguard Worker class SchedulerTest : public CommonCompilerTest, public OptimizingUnitTestHelper {
70*795d594fSAndroid Build Coastguard Worker public:
SchedulerTest()71*795d594fSAndroid Build Coastguard Worker SchedulerTest() : graph_(CreateGraph()) { }
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard Worker // Build scheduling graph, and run target specific scheduling on it.
TestBuildDependencyGraphAndSchedule(HScheduler * scheduler)74*795d594fSAndroid Build Coastguard Worker void TestBuildDependencyGraphAndSchedule(HScheduler* scheduler) {
75*795d594fSAndroid Build Coastguard Worker HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_);
76*795d594fSAndroid Build Coastguard Worker HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_);
77*795d594fSAndroid Build Coastguard Worker graph_->AddBlock(entry);
78*795d594fSAndroid Build Coastguard Worker graph_->AddBlock(block1);
79*795d594fSAndroid Build Coastguard Worker graph_->SetEntryBlock(entry);
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker // entry:
82*795d594fSAndroid Build Coastguard Worker // array ParameterValue
83*795d594fSAndroid Build Coastguard Worker // c1 IntConstant
84*795d594fSAndroid Build Coastguard Worker // c2 IntConstant
85*795d594fSAndroid Build Coastguard Worker // block1:
86*795d594fSAndroid Build Coastguard Worker // add1 Add [c1, c2]
87*795d594fSAndroid Build Coastguard Worker // add2 Add [add1, c2]
88*795d594fSAndroid Build Coastguard Worker // mul Mul [add1, add2]
89*795d594fSAndroid Build Coastguard Worker // div_check DivZeroCheck [add2] (env: add2, mul)
90*795d594fSAndroid Build Coastguard Worker // div Div [add1, div_check]
91*795d594fSAndroid Build Coastguard Worker // array_get1 ArrayGet [array, add1]
92*795d594fSAndroid Build Coastguard Worker // array_set1 ArraySet [array, add1, add2]
93*795d594fSAndroid Build Coastguard Worker // array_get2 ArrayGet [array, add1]
94*795d594fSAndroid Build Coastguard Worker // array_set2 ArraySet [array, add1, add2]
95*795d594fSAndroid Build Coastguard Worker
96*795d594fSAndroid Build Coastguard Worker HInstruction* array = MakeParam(DataType::Type::kReference);
97*795d594fSAndroid Build Coastguard Worker HInstruction* c1 = graph_->GetIntConstant(1);
98*795d594fSAndroid Build Coastguard Worker HInstruction* c2 = graph_->GetIntConstant(10);
99*795d594fSAndroid Build Coastguard Worker
100*795d594fSAndroid Build Coastguard Worker HInstruction* add1 = MakeBinOp<HAdd>(block1, DataType::Type::kInt32, c1, c2);
101*795d594fSAndroid Build Coastguard Worker HInstruction* add2 = MakeBinOp<HAdd>(block1, DataType::Type::kInt32, add1, c2);
102*795d594fSAndroid Build Coastguard Worker HInstruction* mul = MakeBinOp<HMul>(block1, DataType::Type::kInt32, add1, add2);
103*795d594fSAndroid Build Coastguard Worker HInstruction* div_check = new (GetAllocator()) HDivZeroCheck(add2, 0);
104*795d594fSAndroid Build Coastguard Worker block1->AddInstruction(div_check);
105*795d594fSAndroid Build Coastguard Worker HInstruction* div = new (GetAllocator()) HDiv(DataType::Type::kInt32, add1, div_check, 0);
106*795d594fSAndroid Build Coastguard Worker block1->AddInstruction(div);
107*795d594fSAndroid Build Coastguard Worker HInstruction* array_get1 = MakeArrayGet(block1, array, add1, DataType::Type::kInt32);
108*795d594fSAndroid Build Coastguard Worker HInstruction* array_set1 = MakeArraySet(block1, array, add1, add2, DataType::Type::kInt32);
109*795d594fSAndroid Build Coastguard Worker HInstruction* array_get2 = MakeArrayGet(block1, array, add1, DataType::Type::kInt32);
110*795d594fSAndroid Build Coastguard Worker HInstruction* array_set2 = MakeArraySet(block1, array, add1, add2, DataType::Type::kInt32);
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker DCHECK(div_check->CanThrow());
113*795d594fSAndroid Build Coastguard Worker
114*795d594fSAndroid Build Coastguard Worker HEnvironment* environment = HEnvironment::Create(GetAllocator(),
115*795d594fSAndroid Build Coastguard Worker /*number_of_vregs=*/ 2,
116*795d594fSAndroid Build Coastguard Worker graph_->GetArtMethod(),
117*795d594fSAndroid Build Coastguard Worker /*dex_pc=*/ 0,
118*795d594fSAndroid Build Coastguard Worker div_check);
119*795d594fSAndroid Build Coastguard Worker div_check->SetRawEnvironment(environment);
120*795d594fSAndroid Build Coastguard Worker environment->SetRawEnvAt(0, add2);
121*795d594fSAndroid Build Coastguard Worker add2->AddEnvUseAt(div_check->GetEnvironment(), 0);
122*795d594fSAndroid Build Coastguard Worker environment->SetRawEnvAt(1, mul);
123*795d594fSAndroid Build Coastguard Worker mul->AddEnvUseAt(div_check->GetEnvironment(), 1);
124*795d594fSAndroid Build Coastguard Worker
125*795d594fSAndroid Build Coastguard Worker TestSchedulingGraph scheduling_graph(GetScopedAllocator());
126*795d594fSAndroid Build Coastguard Worker // Instructions must be inserted in reverse order into the scheduling graph.
127*795d594fSAndroid Build Coastguard Worker for (HBackwardInstructionIterator it(block1->GetInstructions()); !it.Done(); it.Advance()) {
128*795d594fSAndroid Build Coastguard Worker scheduling_graph.AddNode(it.Current());
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker
131*795d594fSAndroid Build Coastguard Worker // Should not have dependencies cross basic blocks.
132*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, c1));
133*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add2, c2));
134*795d594fSAndroid Build Coastguard Worker
135*795d594fSAndroid Build Coastguard Worker // Define-use dependency.
136*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(add2, add1));
137*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, add2));
138*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div_check, add2));
139*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(div_check, add1));
140*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div, div_check));
141*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add1));
142*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add2));
143*795d594fSAndroid Build Coastguard Worker
144*795d594fSAndroid Build Coastguard Worker // Read and write dependencies
145*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, array_get1));
146*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_get2));
147*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_get2, array_set1));
148*795d594fSAndroid Build Coastguard Worker // Unnecessary dependency is not stored, we rely on transitive dependencies.
149*795d594fSAndroid Build Coastguard Worker // The array_set2 -> array_get2 -> array_set1 dependencies are tested above.
150*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_set1));
151*795d594fSAndroid Build Coastguard Worker
152*795d594fSAndroid Build Coastguard Worker // Env dependency.
153*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(div_check, mul));
154*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(mul, div_check));
155*795d594fSAndroid Build Coastguard Worker
156*795d594fSAndroid Build Coastguard Worker // CanThrow.
157*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, div_check));
158*795d594fSAndroid Build Coastguard Worker
159*795d594fSAndroid Build Coastguard Worker // Exercise the code path of target specific scheduler and SchedulingLatencyVisitor.
160*795d594fSAndroid Build Coastguard Worker scheduler->Schedule(graph_);
161*795d594fSAndroid Build Coastguard Worker }
162*795d594fSAndroid Build Coastguard Worker
CompileWithRandomSchedulerAndRun(const std::vector<uint16_t> & data,bool has_result,int expected)163*795d594fSAndroid Build Coastguard Worker void CompileWithRandomSchedulerAndRun(const std::vector<uint16_t>& data,
164*795d594fSAndroid Build Coastguard Worker bool has_result,
165*795d594fSAndroid Build Coastguard Worker int expected) {
166*795d594fSAndroid Build Coastguard Worker for (CodegenTargetConfig target_config : GetTargetConfigs()) {
167*795d594fSAndroid Build Coastguard Worker HGraph* graph = CreateCFG(data);
168*795d594fSAndroid Build Coastguard Worker
169*795d594fSAndroid Build Coastguard Worker // Schedule the graph randomly.
170*795d594fSAndroid Build Coastguard Worker HInstructionScheduling scheduling(graph, target_config.GetInstructionSet());
171*795d594fSAndroid Build Coastguard Worker scheduling.Run(/*only_optimize_loop_blocks*/ false, /*schedule_randomly*/ true);
172*795d594fSAndroid Build Coastguard Worker
173*795d594fSAndroid Build Coastguard Worker std::unique_ptr<CompilerOptions> compiler_options =
174*795d594fSAndroid Build Coastguard Worker CommonCompilerTest::CreateCompilerOptions(target_config.GetInstructionSet(), "default");
175*795d594fSAndroid Build Coastguard Worker RunCode(target_config,
176*795d594fSAndroid Build Coastguard Worker *compiler_options,
177*795d594fSAndroid Build Coastguard Worker graph,
178*795d594fSAndroid Build Coastguard Worker [](HGraph* graph_arg) { RemoveSuspendChecks(graph_arg); },
179*795d594fSAndroid Build Coastguard Worker has_result, expected);
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker }
182*795d594fSAndroid Build Coastguard Worker
TestDependencyGraphOnAliasingArrayAccesses(HScheduler * scheduler)183*795d594fSAndroid Build Coastguard Worker void TestDependencyGraphOnAliasingArrayAccesses(HScheduler* scheduler) {
184*795d594fSAndroid Build Coastguard Worker HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_);
185*795d594fSAndroid Build Coastguard Worker HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_);
186*795d594fSAndroid Build Coastguard Worker graph_->AddBlock(entry);
187*795d594fSAndroid Build Coastguard Worker graph_->AddBlock(block1);
188*795d594fSAndroid Build Coastguard Worker graph_->SetEntryBlock(entry);
189*795d594fSAndroid Build Coastguard Worker
190*795d594fSAndroid Build Coastguard Worker HInstruction* arr = MakeParam(DataType::Type::kReference);
191*795d594fSAndroid Build Coastguard Worker HInstruction* i = MakeParam(DataType::Type::kInt32);
192*795d594fSAndroid Build Coastguard Worker HInstruction* j = MakeParam(DataType::Type::kInt32);
193*795d594fSAndroid Build Coastguard Worker HInstruction* object = MakeParam(DataType::Type::kReference);
194*795d594fSAndroid Build Coastguard Worker HInstruction* c0 = graph_->GetIntConstant(0);
195*795d594fSAndroid Build Coastguard Worker HInstruction* c1 = graph_->GetIntConstant(1);
196*795d594fSAndroid Build Coastguard Worker
197*795d594fSAndroid Build Coastguard Worker HInstruction* add0 = MakeBinOp<HAdd>(block1, DataType::Type::kInt32, i, c0);
198*795d594fSAndroid Build Coastguard Worker HInstruction* add1 = MakeBinOp<HAdd>(block1, DataType::Type::kInt32, i, c1);
199*795d594fSAndroid Build Coastguard Worker HInstruction* sub0 = MakeBinOp<HSub>(block1, DataType::Type::kInt32, i, c0);
200*795d594fSAndroid Build Coastguard Worker HInstruction* sub1 = MakeBinOp<HSub>(block1, DataType::Type::kInt32, i, c1);
201*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_0 = MakeArraySet(block1, arr, c0, c0, DataType::Type::kInt32);
202*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_1 = MakeArraySet(block1, arr, c1, c0, DataType::Type::kInt32);
203*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_i = MakeArraySet(block1, arr, i, c0, DataType::Type::kInt32);
204*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_add0 = MakeArraySet(block1, arr, add0, c0, DataType::Type::kInt32);
205*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_add1 = MakeArraySet(block1, arr, add1, c0, DataType::Type::kInt32);
206*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_sub0 = MakeArraySet(block1, arr, sub0, c0, DataType::Type::kInt32);
207*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_sub1 = MakeArraySet(block1, arr, sub1, c0, DataType::Type::kInt32);
208*795d594fSAndroid Build Coastguard Worker HInstruction* arr_set_j = MakeArraySet(block1, arr, j, c0, DataType::Type::kInt32);
209*795d594fSAndroid Build Coastguard Worker HInstanceFieldSet* set_field10 = MakeIFieldSet(block1, object, c1, MemberOffset(10));
210*795d594fSAndroid Build Coastguard Worker
211*795d594fSAndroid Build Coastguard Worker HeapLocationCollector heap_location_collector(graph_, GetScopedAllocator());
212*795d594fSAndroid Build Coastguard Worker heap_location_collector.VisitBasicBlock(block1);
213*795d594fSAndroid Build Coastguard Worker heap_location_collector.BuildAliasingMatrix();
214*795d594fSAndroid Build Coastguard Worker TestSchedulingGraph scheduling_graph(GetScopedAllocator(), &heap_location_collector);
215*795d594fSAndroid Build Coastguard Worker
216*795d594fSAndroid Build Coastguard Worker for (HBackwardInstructionIterator it(block1->GetInstructions()); !it.Done(); it.Advance()) {
217*795d594fSAndroid Build Coastguard Worker // Build scheduling graph with memory access aliasing information
218*795d594fSAndroid Build Coastguard Worker // from LSA/heap_location_collector.
219*795d594fSAndroid Build Coastguard Worker scheduling_graph.AddNode(it.Current());
220*795d594fSAndroid Build Coastguard Worker }
221*795d594fSAndroid Build Coastguard Worker
222*795d594fSAndroid Build Coastguard Worker // LSA/HeapLocationCollector should see those ArraySet instructions.
223*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 9U);
224*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(heap_location_collector.HasHeapStores());
225*795d594fSAndroid Build Coastguard Worker
226*795d594fSAndroid Build Coastguard Worker // Test queries on HeapLocationCollector's aliasing matrix after load store analysis.
227*795d594fSAndroid Build Coastguard Worker // HeapLocationCollector and SchedulingGraph should report consistent relationships.
228*795d594fSAndroid Build Coastguard Worker size_t loc1 = HeapLocationCollector::kHeapLocationNotFound;
229*795d594fSAndroid Build Coastguard Worker size_t loc2 = HeapLocationCollector::kHeapLocationNotFound;
230*795d594fSAndroid Build Coastguard Worker
231*795d594fSAndroid Build Coastguard Worker // Test side effect dependency: array[0] and array[1]
232*795d594fSAndroid Build Coastguard Worker loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_0);
233*795d594fSAndroid Build Coastguard Worker loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_1);
234*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
235*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_1, arr_set_0));
236*795d594fSAndroid Build Coastguard Worker
237*795d594fSAndroid Build Coastguard Worker // Test side effect dependency based on LSA analysis: array[i] and array[j]
238*795d594fSAndroid Build Coastguard Worker loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
239*795d594fSAndroid Build Coastguard Worker loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_j);
240*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
241*795d594fSAndroid Build Coastguard Worker // Unnecessary dependency is not stored, we rely on transitive dependencies.
242*795d594fSAndroid Build Coastguard Worker // The arr_set_j -> arr_set_sub0 -> arr_set_add0 -> arr_set_i dependencies are tested below.
243*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_i));
244*795d594fSAndroid Build Coastguard Worker
245*795d594fSAndroid Build Coastguard Worker // Test side effect dependency based on LSA analysis: array[i] and array[i+0]
246*795d594fSAndroid Build Coastguard Worker loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
247*795d594fSAndroid Build Coastguard Worker loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_add0);
248*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
249*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_add0, arr_set_i));
250*795d594fSAndroid Build Coastguard Worker
251*795d594fSAndroid Build Coastguard Worker // Test side effect dependency based on LSA analysis: array[i] and array[i-0]
252*795d594fSAndroid Build Coastguard Worker loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
253*795d594fSAndroid Build Coastguard Worker loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_sub0);
254*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
255*795d594fSAndroid Build Coastguard Worker // Unnecessary dependency is not stored, we rely on transitive dependencies.
256*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub0, arr_set_i));
257*795d594fSAndroid Build Coastguard Worker // Instead, we rely on arr_set_sub0 -> arr_set_add0 -> arr_set_i, the latter is tested above.
258*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub0, arr_set_add0));
259*795d594fSAndroid Build Coastguard Worker
260*795d594fSAndroid Build Coastguard Worker // Test side effect dependency based on LSA analysis: array[i] and array[i+1]
261*795d594fSAndroid Build Coastguard Worker loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
262*795d594fSAndroid Build Coastguard Worker loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_add1);
263*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
264*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_add1, arr_set_i));
265*795d594fSAndroid Build Coastguard Worker
266*795d594fSAndroid Build Coastguard Worker // Test side effect dependency based on LSA analysis: array[i+1] and array[i-1]
267*795d594fSAndroid Build Coastguard Worker loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_add1);
268*795d594fSAndroid Build Coastguard Worker loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_sub1);
269*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
270*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub1, arr_set_add1));
271*795d594fSAndroid Build Coastguard Worker
272*795d594fSAndroid Build Coastguard Worker // Test side effect dependency based on LSA analysis: array[j] and all others array accesses
273*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_sub0));
274*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_add1));
275*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_sub1));
276*795d594fSAndroid Build Coastguard Worker // Unnecessary dependencies are not stored, we rely on transitive dependencies.
277*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_i));
278*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_add0));
279*795d594fSAndroid Build Coastguard Worker
280*795d594fSAndroid Build Coastguard Worker // Test that ArraySet and FieldSet should not have side effect dependency
281*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_i, set_field10));
282*795d594fSAndroid Build Coastguard Worker ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, set_field10));
283*795d594fSAndroid Build Coastguard Worker
284*795d594fSAndroid Build Coastguard Worker // Exercise target specific scheduler and SchedulingLatencyVisitor.
285*795d594fSAndroid Build Coastguard Worker scheduler->Schedule(graph_);
286*795d594fSAndroid Build Coastguard Worker }
287*795d594fSAndroid Build Coastguard Worker
288*795d594fSAndroid Build Coastguard Worker class TestSchedulingGraph : public SchedulingGraph {
289*795d594fSAndroid Build Coastguard Worker public:
TestSchedulingGraph(ScopedArenaAllocator * allocator,const HeapLocationCollector * heap_location_collector=nullptr)290*795d594fSAndroid Build Coastguard Worker explicit TestSchedulingGraph(ScopedArenaAllocator* allocator,
291*795d594fSAndroid Build Coastguard Worker const HeapLocationCollector *heap_location_collector = nullptr)
292*795d594fSAndroid Build Coastguard Worker : SchedulingGraph(allocator, heap_location_collector) {}
293*795d594fSAndroid Build Coastguard Worker
HasImmediateDataDependency(const HInstruction * instruction,const HInstruction * other_instruction) const294*795d594fSAndroid Build Coastguard Worker bool HasImmediateDataDependency(const HInstruction* instruction,
295*795d594fSAndroid Build Coastguard Worker const HInstruction* other_instruction) const {
296*795d594fSAndroid Build Coastguard Worker const SchedulingNode* node = GetNode(instruction);
297*795d594fSAndroid Build Coastguard Worker const SchedulingNode* other = GetNode(other_instruction);
298*795d594fSAndroid Build Coastguard Worker if (node == nullptr || other == nullptr) {
299*795d594fSAndroid Build Coastguard Worker // Both instructions must be in current basic block, i.e. the SchedulingGraph can see their
300*795d594fSAndroid Build Coastguard Worker // corresponding SchedulingNode in the graph, and tell whether there is a dependency.
301*795d594fSAndroid Build Coastguard Worker // Otherwise there is no dependency from SchedulingGraph's perspective, for example,
302*795d594fSAndroid Build Coastguard Worker // instruction and other_instruction are in different basic blocks.
303*795d594fSAndroid Build Coastguard Worker return false;
304*795d594fSAndroid Build Coastguard Worker }
305*795d594fSAndroid Build Coastguard Worker return node->HasDataDependency(other);
306*795d594fSAndroid Build Coastguard Worker }
307*795d594fSAndroid Build Coastguard Worker
HasImmediateOtherDependency(const HInstruction * instruction,const HInstruction * other_instruction) const308*795d594fSAndroid Build Coastguard Worker bool HasImmediateOtherDependency(const HInstruction* instruction,
309*795d594fSAndroid Build Coastguard Worker const HInstruction* other_instruction) const {
310*795d594fSAndroid Build Coastguard Worker const SchedulingNode* node = GetNode(instruction);
311*795d594fSAndroid Build Coastguard Worker const SchedulingNode* other = GetNode(other_instruction);
312*795d594fSAndroid Build Coastguard Worker if (node == nullptr || other == nullptr) {
313*795d594fSAndroid Build Coastguard Worker // Both instructions must be in current basic block, i.e. the SchedulingGraph can see their
314*795d594fSAndroid Build Coastguard Worker // corresponding SchedulingNode in the graph, and tell whether there is a dependency.
315*795d594fSAndroid Build Coastguard Worker // Otherwise there is no dependency from SchedulingGraph's perspective, for example,
316*795d594fSAndroid Build Coastguard Worker // instruction and other_instruction are in different basic blocks.
317*795d594fSAndroid Build Coastguard Worker return false;
318*795d594fSAndroid Build Coastguard Worker }
319*795d594fSAndroid Build Coastguard Worker return node->HasOtherDependency(other);
320*795d594fSAndroid Build Coastguard Worker }
321*795d594fSAndroid Build Coastguard Worker };
322*795d594fSAndroid Build Coastguard Worker
323*795d594fSAndroid Build Coastguard Worker HGraph* graph_;
324*795d594fSAndroid Build Coastguard Worker };
325*795d594fSAndroid Build Coastguard Worker
326*795d594fSAndroid Build Coastguard Worker #if defined(ART_ENABLE_CODEGEN_arm64)
TEST_F(SchedulerTest,DependencyGraphAndSchedulerARM64)327*795d594fSAndroid Build Coastguard Worker TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM64) {
328*795d594fSAndroid Build Coastguard Worker CriticalPathSchedulingNodeSelector critical_path_selector;
329*795d594fSAndroid Build Coastguard Worker arm64::HSchedulerARM64 scheduler(&critical_path_selector);
330*795d594fSAndroid Build Coastguard Worker TestBuildDependencyGraphAndSchedule(&scheduler);
331*795d594fSAndroid Build Coastguard Worker }
332*795d594fSAndroid Build Coastguard Worker
TEST_F(SchedulerTest,ArrayAccessAliasingARM64)333*795d594fSAndroid Build Coastguard Worker TEST_F(SchedulerTest, ArrayAccessAliasingARM64) {
334*795d594fSAndroid Build Coastguard Worker CriticalPathSchedulingNodeSelector critical_path_selector;
335*795d594fSAndroid Build Coastguard Worker arm64::HSchedulerARM64 scheduler(&critical_path_selector);
336*795d594fSAndroid Build Coastguard Worker TestDependencyGraphOnAliasingArrayAccesses(&scheduler);
337*795d594fSAndroid Build Coastguard Worker }
338*795d594fSAndroid Build Coastguard Worker #endif
339*795d594fSAndroid Build Coastguard Worker
340*795d594fSAndroid Build Coastguard Worker #if defined(ART_ENABLE_CODEGEN_arm)
TEST_F(SchedulerTest,DependencyGraphAndSchedulerARM)341*795d594fSAndroid Build Coastguard Worker TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM) {
342*795d594fSAndroid Build Coastguard Worker CriticalPathSchedulingNodeSelector critical_path_selector;
343*795d594fSAndroid Build Coastguard Worker arm::HSchedulerARM scheduler(&critical_path_selector, /*codegen=*/ nullptr);
344*795d594fSAndroid Build Coastguard Worker TestBuildDependencyGraphAndSchedule(&scheduler);
345*795d594fSAndroid Build Coastguard Worker }
346*795d594fSAndroid Build Coastguard Worker
TEST_F(SchedulerTest,ArrayAccessAliasingARM)347*795d594fSAndroid Build Coastguard Worker TEST_F(SchedulerTest, ArrayAccessAliasingARM) {
348*795d594fSAndroid Build Coastguard Worker CriticalPathSchedulingNodeSelector critical_path_selector;
349*795d594fSAndroid Build Coastguard Worker arm::HSchedulerARM scheduler(&critical_path_selector, /*codegen=*/ nullptr);
350*795d594fSAndroid Build Coastguard Worker TestDependencyGraphOnAliasingArrayAccesses(&scheduler);
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker #endif
353*795d594fSAndroid Build Coastguard Worker
TEST_F(SchedulerTest,RandomScheduling)354*795d594fSAndroid Build Coastguard Worker TEST_F(SchedulerTest, RandomScheduling) {
355*795d594fSAndroid Build Coastguard Worker //
356*795d594fSAndroid Build Coastguard Worker // Java source: crafted code to make sure (random) scheduling should get correct result.
357*795d594fSAndroid Build Coastguard Worker //
358*795d594fSAndroid Build Coastguard Worker // int result = 0;
359*795d594fSAndroid Build Coastguard Worker // float fr = 10.0f;
360*795d594fSAndroid Build Coastguard Worker // for (int i = 1; i < 10; i++) {
361*795d594fSAndroid Build Coastguard Worker // fr ++;
362*795d594fSAndroid Build Coastguard Worker // int t1 = result >> i;
363*795d594fSAndroid Build Coastguard Worker // int t2 = result * i;
364*795d594fSAndroid Build Coastguard Worker // result = result + t1 - t2;
365*795d594fSAndroid Build Coastguard Worker // fr = fr / i;
366*795d594fSAndroid Build Coastguard Worker // result += (int)fr;
367*795d594fSAndroid Build Coastguard Worker // }
368*795d594fSAndroid Build Coastguard Worker // return result;
369*795d594fSAndroid Build Coastguard Worker //
370*795d594fSAndroid Build Coastguard Worker const std::vector<uint16_t> data = SIX_REGISTERS_CODE_ITEM(
371*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 0 << 12 | 2 << 8, // const/4 v2, #int 0
372*795d594fSAndroid Build Coastguard Worker Instruction::CONST_HIGH16 | 0 << 8, 0x4120, // const/high16 v0, #float 10.0 // #41200000
373*795d594fSAndroid Build Coastguard Worker Instruction::CONST_4 | 1 << 12 | 1 << 8, // const/4 v1, #int 1
374*795d594fSAndroid Build Coastguard Worker Instruction::CONST_16 | 5 << 8, 0x000a, // const/16 v5, #int 10
375*795d594fSAndroid Build Coastguard Worker Instruction::IF_GE | 5 << 12 | 1 << 8, 0x0014, // if-ge v1, v5, 001a // +0014
376*795d594fSAndroid Build Coastguard Worker Instruction::CONST_HIGH16 | 5 << 8, 0x3f80, // const/high16 v5, #float 1.0 // #3f800000
377*795d594fSAndroid Build Coastguard Worker Instruction::ADD_FLOAT_2ADDR | 5 << 12 | 0 << 8, // add-float/2addr v0, v5
378*795d594fSAndroid Build Coastguard Worker Instruction::SHR_INT | 3 << 8, 1 << 8 | 2 , // shr-int v3, v2, v1
379*795d594fSAndroid Build Coastguard Worker Instruction::MUL_INT | 4 << 8, 1 << 8 | 2, // mul-int v4, v2, v1
380*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT | 5 << 8, 3 << 8 | 2, // add-int v5, v2, v3
381*795d594fSAndroid Build Coastguard Worker Instruction::SUB_INT | 2 << 8, 4 << 8 | 5, // sub-int v2, v5, v4
382*795d594fSAndroid Build Coastguard Worker Instruction::INT_TO_FLOAT | 1 << 12 | 5 << 8, // int-to-float v5, v1
383*795d594fSAndroid Build Coastguard Worker Instruction::DIV_FLOAT_2ADDR | 5 << 12 | 0 << 8, // div-float/2addr v0, v5
384*795d594fSAndroid Build Coastguard Worker Instruction::FLOAT_TO_INT | 0 << 12 | 5 << 8, // float-to-int v5, v0
385*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT_2ADDR | 5 << 12 | 2 << 8, // add-int/2addr v2, v5
386*795d594fSAndroid Build Coastguard Worker Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8 | 1, // add-int/lit8 v1, v1, #int 1 // #01
387*795d594fSAndroid Build Coastguard Worker Instruction::GOTO | 0xeb << 8, // goto 0004 // -0015
388*795d594fSAndroid Build Coastguard Worker Instruction::RETURN | 2 << 8); // return v2
389*795d594fSAndroid Build Coastguard Worker
390*795d594fSAndroid Build Coastguard Worker constexpr int kNumberOfRuns = 10;
391*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < kNumberOfRuns; ++i) {
392*795d594fSAndroid Build Coastguard Worker CompileWithRandomSchedulerAndRun(data, true, 138774);
393*795d594fSAndroid Build Coastguard Worker }
394*795d594fSAndroid Build Coastguard Worker }
395*795d594fSAndroid Build Coastguard Worker
396*795d594fSAndroid Build Coastguard Worker } // namespace art
397