blob: 4c47f2ee5745c21e4be641554fd66fba6efa91bb [file] [log] [blame]
Alexandre Rames22aa54b2016-10-18 09:32:29 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "scheduler.h"
18
Alexandre Rames22aa54b2016-10-18 09:32:29 +010019#include "base/arena_allocator.h"
20#include "builder.h"
21#include "codegen_test_utils.h"
22#include "common_compiler_test.h"
xueliang.zhong2a3471f2017-05-08 18:36:40 +010023#include "load_store_analysis.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010024#include "nodes.h"
25#include "optimizing_unit_test.h"
26#include "pc_relative_fixups_x86.h"
27#include "register_allocator.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010028
29#ifdef ART_ENABLE_CODEGEN_arm64
30#include "scheduler_arm64.h"
31#endif
32
xueliang.zhongf7caf682017-03-01 16:07:02 +000033#ifdef ART_ENABLE_CODEGEN_arm
34#include "scheduler_arm.h"
35#endif
36
Alexandre Rames22aa54b2016-10-18 09:32:29 +010037namespace art {
38
39// Return all combinations of ISA and code generator that are executable on
40// hardware, or on simulator, and that we'd like to test.
41static ::std::vector<CodegenTargetConfig> GetTargetConfigs() {
42 ::std::vector<CodegenTargetConfig> v;
43 ::std::vector<CodegenTargetConfig> test_config_candidates = {
44#ifdef ART_ENABLE_CODEGEN_arm
Roland Levillain9983e302017-07-14 14:34:22 +010045 // TODO: Should't this be `kThumb2` instead of `kArm` here?
Vladimir Marko33bff252017-11-01 14:35:42 +000046 CodegenTargetConfig(InstructionSet::kArm, create_codegen_arm_vixl32),
Alexandre Rames22aa54b2016-10-18 09:32:29 +010047#endif
48#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Marko33bff252017-11-01 14:35:42 +000049 CodegenTargetConfig(InstructionSet::kArm64, create_codegen_arm64),
Alexandre Rames22aa54b2016-10-18 09:32:29 +010050#endif
51#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Marko33bff252017-11-01 14:35:42 +000052 CodegenTargetConfig(InstructionSet::kX86, create_codegen_x86),
Alexandre Rames22aa54b2016-10-18 09:32:29 +010053#endif
54#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Marko33bff252017-11-01 14:35:42 +000055 CodegenTargetConfig(InstructionSet::kX86_64, create_codegen_x86_64),
Alexandre Rames22aa54b2016-10-18 09:32:29 +010056#endif
57#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko33bff252017-11-01 14:35:42 +000058 CodegenTargetConfig(InstructionSet::kMips, create_codegen_mips),
Alexandre Rames22aa54b2016-10-18 09:32:29 +010059#endif
60#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko33bff252017-11-01 14:35:42 +000061 CodegenTargetConfig(InstructionSet::kMips64, create_codegen_mips64)
Alexandre Rames22aa54b2016-10-18 09:32:29 +010062#endif
63 };
64
Vladimir Marko7d157fc2017-05-10 16:29:23 +010065 for (const CodegenTargetConfig& test_config : test_config_candidates) {
Alexandre Rames22aa54b2016-10-18 09:32:29 +010066 if (CanExecute(test_config.GetInstructionSet())) {
67 v.push_back(test_config);
68 }
69 }
70
71 return v;
72}
73
Vladimir Markoca6fff82017-10-03 14:49:14 +010074class SchedulerTest : public OptimizingUnitTest {
xueliang.zhongf7caf682017-03-01 16:07:02 +000075 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +010076 SchedulerTest() : graph_(CreateGraph()) { }
Alexandre Rames22aa54b2016-10-18 09:32:29 +010077
xueliang.zhongf7caf682017-03-01 16:07:02 +000078 // Build scheduling graph, and run target specific scheduling on it.
79 void TestBuildDependencyGraphAndSchedule(HScheduler* scheduler) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010080 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_);
81 HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_);
xueliang.zhongf7caf682017-03-01 16:07:02 +000082 graph_->AddBlock(entry);
83 graph_->AddBlock(block1);
84 graph_->SetEntryBlock(entry);
85
86 // entry:
87 // array ParameterValue
88 // c1 IntConstant
89 // c2 IntConstant
90 // block1:
91 // add1 Add [c1, c2]
92 // add2 Add [add1, c2]
93 // mul Mul [add1, add2]
94 // div_check DivZeroCheck [add2] (env: add2, mul)
95 // div Div [add1, div_check]
96 // array_get1 ArrayGet [array, add1]
97 // array_set1 ArraySet [array, add1, add2]
98 // array_get2 ArrayGet [array, add1]
99 // array_set2 ArraySet [array, add1, add2]
100
Vladimir Markoca6fff82017-10-03 14:49:14 +0100101 HInstruction* array = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
102 dex::TypeIndex(0),
103 0,
104 DataType::Type::kReference);
xueliang.zhongf7caf682017-03-01 16:07:02 +0000105 HInstruction* c1 = graph_->GetIntConstant(1);
106 HInstruction* c2 = graph_->GetIntConstant(10);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100107 HInstruction* add1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, c1, c2);
108 HInstruction* add2 = new (GetAllocator()) HAdd(DataType::Type::kInt32, add1, c2);
109 HInstruction* mul = new (GetAllocator()) HMul(DataType::Type::kInt32, add1, add2);
110 HInstruction* div_check = new (GetAllocator()) HDivZeroCheck(add2, 0);
111 HInstruction* div = new (GetAllocator()) HDiv(DataType::Type::kInt32, add1, div_check, 0);
112 HInstruction* array_get1 =
113 new (GetAllocator()) HArrayGet(array, add1, DataType::Type::kInt32, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100114 HInstruction* array_set1 =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100115 new (GetAllocator()) HArraySet(array, add1, add2, DataType::Type::kInt32, 0);
116 HInstruction* array_get2 =
117 new (GetAllocator()) HArrayGet(array, add1, DataType::Type::kInt32, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100118 HInstruction* array_set2 =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100119 new (GetAllocator()) HArraySet(array, add1, add2, DataType::Type::kInt32, 0);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100120
xueliang.zhongf7caf682017-03-01 16:07:02 +0000121 DCHECK(div_check->CanThrow());
122
123 entry->AddInstruction(array);
124
125 HInstruction* block_instructions[] = {add1,
126 add2,
127 mul,
128 div_check,
129 div,
130 array_get1,
131 array_set1,
132 array_get2,
133 array_set2};
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100134 for (HInstruction* instr : block_instructions) {
xueliang.zhongf7caf682017-03-01 16:07:02 +0000135 block1->AddInstruction(instr);
136 }
137
Vladimir Markoca6fff82017-10-03 14:49:14 +0100138 HEnvironment* environment = new (GetAllocator()) HEnvironment(GetAllocator(),
139 2,
140 graph_->GetArtMethod(),
141 0,
142 div_check);
xueliang.zhongf7caf682017-03-01 16:07:02 +0000143 div_check->SetRawEnvironment(environment);
144 environment->SetRawEnvAt(0, add2);
145 add2->AddEnvUseAt(div_check->GetEnvironment(), 0);
146 environment->SetRawEnvAt(1, mul);
147 mul->AddEnvUseAt(div_check->GetEnvironment(), 1);
148
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000149 TestSchedulingGraph scheduling_graph(GetScopedAllocator());
xueliang.zhongf7caf682017-03-01 16:07:02 +0000150 // Instructions must be inserted in reverse order into the scheduling graph.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100151 for (HInstruction* instr : ReverseRange(block_instructions)) {
xueliang.zhongf7caf682017-03-01 16:07:02 +0000152 scheduling_graph.AddNode(instr);
153 }
154
155 // Should not have dependencies cross basic blocks.
156 ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, c1));
157 ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add2, c2));
158
159 // Define-use dependency.
160 ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(add2, add1));
161 ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, add2));
162 ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div_check, add2));
163 ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(div_check, add1));
164 ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div, div_check));
165 ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add1));
166 ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add2));
167
168 // Read and write dependencies
169 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, array_get1));
170 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_get2));
171 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_get2, array_set1));
Vladimir Marko09d041b2018-07-30 12:51:59 +0100172 // Unnecessary dependency is not stored, we rely on transitive dependencies.
173 // The array_set2 -> array_get2 -> array_set1 dependencies are tested above.
174 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_set1));
xueliang.zhongf7caf682017-03-01 16:07:02 +0000175
176 // Env dependency.
177 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(div_check, mul));
178 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(mul, div_check));
179
180 // CanThrow.
181 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, div_check));
182
183 // Exercise the code path of target specific scheduler and SchedulingLatencyVisitor.
184 scheduler->Schedule(graph_);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100185 }
186
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800187 void CompileWithRandomSchedulerAndRun(const std::vector<uint16_t>& data,
188 bool has_result,
189 int expected) {
xueliang.zhongf7caf682017-03-01 16:07:02 +0000190 for (CodegenTargetConfig target_config : GetTargetConfigs()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100191 HGraph* graph = CreateCFG(data);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100192
xueliang.zhongf7caf682017-03-01 16:07:02 +0000193 // Schedule the graph randomly.
194 HInstructionScheduling scheduling(graph, target_config.GetInstructionSet());
195 scheduling.Run(/*only_optimize_loop_blocks*/ false, /*schedule_randomly*/ true);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100196
Vladimir Markoa0431112018-06-25 09:32:54 +0100197 OverrideInstructionSetFeatures(target_config.GetInstructionSet(), "default");
xueliang.zhongf7caf682017-03-01 16:07:02 +0000198 RunCode(target_config,
Vladimir Markoa0431112018-06-25 09:32:54 +0100199 *compiler_options_,
xueliang.zhongf7caf682017-03-01 16:07:02 +0000200 graph,
201 [](HGraph* graph_arg) { RemoveSuspendChecks(graph_arg); },
202 has_result, expected);
203 }
204 }
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100205
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100206 void TestDependencyGraphOnAliasingArrayAccesses(HScheduler* scheduler) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100207 HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100208 graph_->AddBlock(entry);
209 graph_->SetEntryBlock(entry);
210 graph_->BuildDominatorTree();
211
Vladimir Markoca6fff82017-10-03 14:49:14 +0100212 HInstruction* arr = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100213 dex::TypeIndex(0),
214 0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100215 DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100216 HInstruction* i = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
217 dex::TypeIndex(1),
218 1,
219 DataType::Type::kInt32);
220 HInstruction* j = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
221 dex::TypeIndex(1),
222 1,
223 DataType::Type::kInt32);
224 HInstruction* object = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
225 dex::TypeIndex(0),
226 0,
227 DataType::Type::kReference);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100228 HInstruction* c0 = graph_->GetIntConstant(0);
229 HInstruction* c1 = graph_->GetIntConstant(1);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100230 HInstruction* add0 = new (GetAllocator()) HAdd(DataType::Type::kInt32, i, c0);
231 HInstruction* add1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, i, c1);
232 HInstruction* sub0 = new (GetAllocator()) HSub(DataType::Type::kInt32, i, c0);
233 HInstruction* sub1 = new (GetAllocator()) HSub(DataType::Type::kInt32, i, c1);
234 HInstruction* arr_set_0 =
235 new (GetAllocator()) HArraySet(arr, c0, c0, DataType::Type::kInt32, 0);
236 HInstruction* arr_set_1 =
237 new (GetAllocator()) HArraySet(arr, c1, c0, DataType::Type::kInt32, 0);
238 HInstruction* arr_set_i = new (GetAllocator()) HArraySet(arr, i, c0, DataType::Type::kInt32, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100239 HInstruction* arr_set_add0 =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100240 new (GetAllocator()) HArraySet(arr, add0, c0, DataType::Type::kInt32, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100241 HInstruction* arr_set_add1 =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100242 new (GetAllocator()) HArraySet(arr, add1, c0, DataType::Type::kInt32, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100243 HInstruction* arr_set_sub0 =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100244 new (GetAllocator()) HArraySet(arr, sub0, c0, DataType::Type::kInt32, 0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100245 HInstruction* arr_set_sub1 =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100246 new (GetAllocator()) HArraySet(arr, sub1, c0, DataType::Type::kInt32, 0);
247 HInstruction* arr_set_j = new (GetAllocator()) HArraySet(arr, j, c0, DataType::Type::kInt32, 0);
248 HInstanceFieldSet* set_field10 = new (GetAllocator()) HInstanceFieldSet(object,
249 c1,
250 nullptr,
251 DataType::Type::kInt32,
252 MemberOffset(10),
253 false,
254 kUnknownFieldIndex,
255 kUnknownClassDefIndex,
256 graph_->GetDexFile(),
257 0);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100258
259 HInstruction* block_instructions[] = {arr,
260 i,
261 j,
262 object,
263 add0,
264 add1,
265 sub0,
266 sub1,
267 arr_set_0,
268 arr_set_1,
269 arr_set_i,
270 arr_set_add0,
271 arr_set_add1,
272 arr_set_sub0,
273 arr_set_sub1,
274 arr_set_j,
275 set_field10};
276
277 for (HInstruction* instr : block_instructions) {
278 entry->AddInstruction(instr);
279 }
280
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100281 HeapLocationCollector heap_location_collector(graph_);
282 heap_location_collector.VisitBasicBlock(entry);
283 heap_location_collector.BuildAliasingMatrix();
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000284 TestSchedulingGraph scheduling_graph(GetScopedAllocator(), &heap_location_collector);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100285
286 for (HInstruction* instr : ReverseRange(block_instructions)) {
287 // Build scheduling graph with memory access aliasing information
288 // from LSA/heap_location_collector.
289 scheduling_graph.AddNode(instr);
290 }
291
292 // LSA/HeapLocationCollector should see those ArraySet instructions.
293 ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 9U);
294 ASSERT_TRUE(heap_location_collector.HasHeapStores());
295
296 // Test queries on HeapLocationCollector's aliasing matrix after load store analysis.
297 // HeapLocationCollector and SchedulingGraph should report consistent relationships.
298 size_t loc1 = HeapLocationCollector::kHeapLocationNotFound;
299 size_t loc2 = HeapLocationCollector::kHeapLocationNotFound;
300
301 // Test side effect dependency: array[0] and array[1]
Aart Bikb765a3f2018-05-10 14:47:48 -0700302 loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_0);
303 loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_1);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100304 ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
305 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_1, arr_set_0));
306
307 // Test side effect dependency based on LSA analysis: array[i] and array[j]
Aart Bikb765a3f2018-05-10 14:47:48 -0700308 loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
309 loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_j);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100310 ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
Vladimir Marko09d041b2018-07-30 12:51:59 +0100311 // Unnecessary dependency is not stored, we rely on transitive dependencies.
312 // The arr_set_j -> arr_set_sub0 -> arr_set_add0 -> arr_set_i dependencies are tested below.
313 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_i));
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100314
315 // Test side effect dependency based on LSA analysis: array[i] and array[i+0]
Aart Bikb765a3f2018-05-10 14:47:48 -0700316 loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
317 loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_add0);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100318 ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
319 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_add0, arr_set_i));
320
321 // Test side effect dependency based on LSA analysis: array[i] and array[i-0]
Aart Bikb765a3f2018-05-10 14:47:48 -0700322 loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
323 loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_sub0);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100324 ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2));
Vladimir Marko09d041b2018-07-30 12:51:59 +0100325 // Unnecessary dependency is not stored, we rely on transitive dependencies.
326 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub0, arr_set_i));
327 // Instead, we rely on arr_set_sub0 -> arr_set_add0 -> arr_set_i, the latter is tested above.
328 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub0, arr_set_add0));
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100329
330 // Test side effect dependency based on LSA analysis: array[i] and array[i+1]
Aart Bikb765a3f2018-05-10 14:47:48 -0700331 loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i);
332 loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_add1);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100333 ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
334 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_add1, arr_set_i));
335
336 // Test side effect dependency based on LSA analysis: array[i+1] and array[i-1]
Aart Bikb765a3f2018-05-10 14:47:48 -0700337 loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_add1);
338 loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_sub1);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100339 ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2));
340 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub1, arr_set_add1));
341
342 // Test side effect dependency based on LSA analysis: array[j] and all others array accesses
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100343 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_sub0));
344 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_add1));
345 ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_sub1));
Vladimir Marko09d041b2018-07-30 12:51:59 +0100346 // Unnecessary dependencies are not stored, we rely on transitive dependencies.
347 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_i));
348 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_add0));
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100349
350 // Test that ArraySet and FieldSet should not have side effect dependency
351 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_i, set_field10));
352 ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, set_field10));
353
354 // Exercise target specific scheduler and SchedulingLatencyVisitor.
355 scheduler->Schedule(graph_);
356 }
357
Evgeny Astigeevich957c5382019-03-18 12:37:58 +0000358 class TestSchedulingGraph : public SchedulingGraph {
359 public:
360 explicit TestSchedulingGraph(ScopedArenaAllocator* allocator,
361 const HeapLocationCollector *heap_location_collector = nullptr)
362 : SchedulingGraph(allocator, heap_location_collector) {}
363
364 bool HasImmediateDataDependency(const HInstruction* instruction,
365 const HInstruction* other_instruction) const {
366 const SchedulingNode* node = GetNode(instruction);
367 const SchedulingNode* other = GetNode(other_instruction);
368 if (node == nullptr || other == nullptr) {
369 // Both instructions must be in current basic block, i.e. the SchedulingGraph can see their
370 // corresponding SchedulingNode in the graph, and tell whether there is a dependency.
371 // Otherwise there is no dependency from SchedulingGraph's perspective, for example,
372 // instruction and other_instruction are in different basic blocks.
373 return false;
374 }
375 return node->HasDataDependency(other);
376 }
377
378 bool HasImmediateOtherDependency(const HInstruction* instruction,
379 const HInstruction* other_instruction) const {
380 const SchedulingNode* node = GetNode(instruction);
381 const SchedulingNode* other = GetNode(other_instruction);
382 if (node == nullptr || other == nullptr) {
383 // Both instructions must be in current basic block, i.e. the SchedulingGraph can see their
384 // corresponding SchedulingNode in the graph, and tell whether there is a dependency.
385 // Otherwise there is no dependency from SchedulingGraph's perspective, for example,
386 // instruction and other_instruction are in different basic blocks.
387 return false;
388 }
389 return node->HasOtherDependency(other);
390 }
391 };
392
xueliang.zhongf7caf682017-03-01 16:07:02 +0000393 HGraph* graph_;
394};
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100395
xueliang.zhongf7caf682017-03-01 16:07:02 +0000396#if defined(ART_ENABLE_CODEGEN_arm64)
397TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM64) {
398 CriticalPathSchedulingNodeSelector critical_path_selector;
Vladimir Markoced04832018-07-26 14:42:17 +0100399 arm64::HSchedulerARM64 scheduler(&critical_path_selector);
xueliang.zhongf7caf682017-03-01 16:07:02 +0000400 TestBuildDependencyGraphAndSchedule(&scheduler);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100401}
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100402
403TEST_F(SchedulerTest, ArrayAccessAliasingARM64) {
404 CriticalPathSchedulingNodeSelector critical_path_selector;
Vladimir Markoced04832018-07-26 14:42:17 +0100405 arm64::HSchedulerARM64 scheduler(&critical_path_selector);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100406 TestDependencyGraphOnAliasingArrayAccesses(&scheduler);
407}
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100408#endif
409
xueliang.zhongf7caf682017-03-01 16:07:02 +0000410#if defined(ART_ENABLE_CODEGEN_arm)
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100411TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM) {
xueliang.zhongf7caf682017-03-01 16:07:02 +0000412 CriticalPathSchedulingNodeSelector critical_path_selector;
413 arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr);
Vladimir Markoced04832018-07-26 14:42:17 +0100414 arm::HSchedulerARM scheduler(&critical_path_selector, &arm_latency_visitor);
xueliang.zhongf7caf682017-03-01 16:07:02 +0000415 TestBuildDependencyGraphAndSchedule(&scheduler);
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100416}
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100417
418TEST_F(SchedulerTest, ArrayAccessAliasingARM) {
419 CriticalPathSchedulingNodeSelector critical_path_selector;
420 arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr);
Vladimir Markoced04832018-07-26 14:42:17 +0100421 arm::HSchedulerARM scheduler(&critical_path_selector, &arm_latency_visitor);
xueliang.zhong2a3471f2017-05-08 18:36:40 +0100422 TestDependencyGraphOnAliasingArrayAccesses(&scheduler);
423}
xueliang.zhongf7caf682017-03-01 16:07:02 +0000424#endif
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100425
426TEST_F(SchedulerTest, RandomScheduling) {
427 //
428 // Java source: crafted code to make sure (random) scheduling should get correct result.
429 //
430 // int result = 0;
431 // float fr = 10.0f;
432 // for (int i = 1; i < 10; i++) {
433 // fr ++;
434 // int t1 = result >> i;
435 // int t2 = result * i;
436 // result = result + t1 - t2;
437 // fr = fr / i;
438 // result += (int)fr;
439 // }
440 // return result;
441 //
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800442 const std::vector<uint16_t> data = SIX_REGISTERS_CODE_ITEM(
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100443 Instruction::CONST_4 | 0 << 12 | 2 << 8, // const/4 v2, #int 0
444 Instruction::CONST_HIGH16 | 0 << 8, 0x4120, // const/high16 v0, #float 10.0 // #41200000
445 Instruction::CONST_4 | 1 << 12 | 1 << 8, // const/4 v1, #int 1
446 Instruction::CONST_16 | 5 << 8, 0x000a, // const/16 v5, #int 10
447 Instruction::IF_GE | 5 << 12 | 1 << 8, 0x0014, // if-ge v1, v5, 001a // +0014
448 Instruction::CONST_HIGH16 | 5 << 8, 0x3f80, // const/high16 v5, #float 1.0 // #3f800000
449 Instruction::ADD_FLOAT_2ADDR | 5 << 12 | 0 << 8, // add-float/2addr v0, v5
450 Instruction::SHR_INT | 3 << 8, 1 << 8 | 2 , // shr-int v3, v2, v1
451 Instruction::MUL_INT | 4 << 8, 1 << 8 | 2, // mul-int v4, v2, v1
452 Instruction::ADD_INT | 5 << 8, 3 << 8 | 2, // add-int v5, v2, v3
453 Instruction::SUB_INT | 2 << 8, 4 << 8 | 5, // sub-int v2, v5, v4
454 Instruction::INT_TO_FLOAT | 1 << 12 | 5 << 8, // int-to-float v5, v1
455 Instruction::DIV_FLOAT_2ADDR | 5 << 12 | 0 << 8, // div-float/2addr v0, v5
456 Instruction::FLOAT_TO_INT | 0 << 12 | 5 << 8, // float-to-int v5, v0
457 Instruction::ADD_INT_2ADDR | 5 << 12 | 2 << 8, // add-int/2addr v2, v5
458 Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8 | 1, // add-int/lit8 v1, v1, #int 1 // #01
459 Instruction::GOTO | 0xeb << 8, // goto 0004 // -0015
460 Instruction::RETURN | 2 << 8); // return v2
461
462 constexpr int kNumberOfRuns = 10;
463 for (int i = 0; i < kNumberOfRuns; ++i) {
464 CompileWithRandomSchedulerAndRun(data, true, 138774);
465 }
466}
467
468} // namespace art