blob: 2df0f34c7deee5f190301bdd64ab9874a43ba13c [file] [log] [blame]
Vladimir Marko356bd282017-03-01 12:01:11 +00001/*
2 * Copyright (C) 2017 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 "ssa_liveness_analysis.h"
18
Vladimir Marko356bd282017-03-01 12:01:11 +000019#include "arch/instruction_set.h"
20#include "arch/instruction_set_features.h"
21#include "base/arena_allocator.h"
22#include "base/arena_containers.h"
Vladimír Marko434d9682022-11-04 14:04:17 +000023#include "base/macros.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000024#include "code_generator.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "driver/compiler_options.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000026#include "nodes.h"
27#include "optimizing_unit_test.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000028
Vladimír Marko434d9682022-11-04 14:04:17 +000029namespace art HIDDEN {
Vladimir Marko356bd282017-03-01 12:01:11 +000030
Vladimir Markoca6fff82017-10-03 14:49:14 +010031class SsaLivenessAnalysisTest : public OptimizingUnitTest {
Vladimir Markoa0431112018-06-25 09:32:54 +010032 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010033 void SetUp() override {
Vladimir Markoa0431112018-06-25 09:32:54 +010034 OptimizingUnitTest::SetUp();
35 graph_ = CreateGraph();
Vladimir Markof91fc122020-05-13 09:21:00 +010036 compiler_options_ = CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default");
Vladimir Markoa0431112018-06-25 09:32:54 +010037 codegen_ = CodeGenerator::Create(graph_, *compiler_options_);
Vladimir Markof91fc122020-05-13 09:21:00 +010038 CHECK(codegen_ != nullptr);
Vladimir Marko356bd282017-03-01 12:01:11 +000039 // Create entry block.
Vladimir Markoca6fff82017-10-03 14:49:14 +010040 entry_ = new (GetAllocator()) HBasicBlock(graph_);
Vladimir Marko356bd282017-03-01 12:01:11 +000041 graph_->AddBlock(entry_);
42 graph_->SetEntryBlock(entry_);
43 }
44
45 protected:
46 HBasicBlock* CreateSuccessor(HBasicBlock* block) {
47 HGraph* graph = block->GetGraph();
Vladimir Markoca6fff82017-10-03 14:49:14 +010048 HBasicBlock* successor = new (GetAllocator()) HBasicBlock(graph);
Vladimir Marko356bd282017-03-01 12:01:11 +000049 graph->AddBlock(successor);
50 block->AddSuccessor(successor);
51 return successor;
52 }
53
Vladimir Marko356bd282017-03-01 12:01:11 +000054 HGraph* graph_;
Vladimir Markof91fc122020-05-13 09:21:00 +010055 std::unique_ptr<CompilerOptions> compiler_options_;
Vladimir Marko356bd282017-03-01 12:01:11 +000056 std::unique_ptr<CodeGenerator> codegen_;
57 HBasicBlock* entry_;
58};
59
60TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010061 HInstruction* arg = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000063 entry_->AddInstruction(arg);
64
65 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010066 HInstruction* ret = new (GetAllocator()) HReturn(arg);
Vladimir Marko356bd282017-03-01 12:01:11 +000067 block->AddInstruction(ret);
Vladimir Markoca6fff82017-10-03 14:49:14 +010068 block->AddInstruction(new (GetAllocator()) HExit());
Vladimir Marko356bd282017-03-01 12:01:11 +000069
70 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +010071 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +000072 ssa_analysis.Analyze();
73
74 std::ostringstream arg_dump;
75 arg->GetLiveInterval()->Dump(arg_dump);
76 EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
77 arg_dump.str().c_str());
78}
79
80TEST_F(SsaLivenessAnalysisTest, TestAput) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010081 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010082 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +010083 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010085 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010086 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010087 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010088 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010089 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +010091 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +000092 for (HInstruction* insn : args) {
93 entry_->AddInstruction(insn);
94 }
95
96 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010097 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +000098 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +010099 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800100 /* number_of_vregs= */ 5,
101 /* method= */ nullptr,
102 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100103 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100104 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000105 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100106 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000107 block->AddInstruction(length);
Andreas Gampe3db70682018-12-26 15:12:03 -0800108 HInstruction* bounds_check = new (GetAllocator()) HBoundsCheck(index, length, /* dex_pc= */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000109 block->AddInstruction(bounds_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100110 HEnvironment* bounds_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800111 /* number_of_vregs= */ 5,
112 /* method= */ nullptr,
113 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100114 bounds_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100115 bounds_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000116 bounds_check->SetRawEnvironment(bounds_check_env);
117 HInstruction* array_set =
Andreas Gampe3db70682018-12-26 15:12:03 -0800118 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc= */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000119 block->AddInstruction(array_set);
120
121 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100122 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000123 ssa_analysis.Analyze();
124
125 EXPECT_FALSE(graph_->IsDebuggable());
126 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
127 static const char* const expected[] = {
128 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
129 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100130 "ranges: { [4,21) }, uses: { 19 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000131 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100132 "ranges: { [6,21) }, uses: { 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000133 "is_high: 0",
134 // Environment uses do not keep the non-reference argument alive.
Artem Serovd6750532018-05-30 20:07:43 +0100135 "ranges: { [8,10) }, uses: { }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000136 // Environment uses keep the reference argument alive.
137 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
138 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100139 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000140 size_t arg_index = 0u;
141 for (HInstruction* arg : args) {
142 std::ostringstream arg_dump;
143 arg->GetLiveInterval()->Dump(arg_dump);
144 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
145 ++arg_index;
146 }
147}
148
149TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100150 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100152 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100154 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100156 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100160 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +0000161 for (HInstruction* insn : args) {
162 entry_->AddInstruction(insn);
163 }
164
165 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100166 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000167 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100168 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800169 /* number_of_vregs= */ 5,
170 /* method= */ nullptr,
171 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100172 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100173 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000174 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100175 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000176 block->AddInstruction(length);
177 // Use HAboveOrEqual+HDeoptimize as the bounds check.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100178 HInstruction* ae = new (GetAllocator()) HAboveOrEqual(index, length);
Vladimir Marko356bd282017-03-01 12:01:11 +0000179 block->AddInstruction(ae);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100180 HInstruction* deoptimize = new(GetAllocator()) HDeoptimize(
Andreas Gampe3db70682018-12-26 15:12:03 -0800181 GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc= */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000182 block->AddInstruction(deoptimize);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100183 HEnvironment* deoptimize_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800184 /* number_of_vregs= */ 5,
185 /* method= */ nullptr,
186 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100187 deoptimize);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100188 deoptimize_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000189 deoptimize->SetRawEnvironment(deoptimize_env);
190 HInstruction* array_set =
Andreas Gampe3db70682018-12-26 15:12:03 -0800191 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc= */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000192 block->AddInstruction(array_set);
193
194 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100195 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000196 ssa_analysis.Analyze();
197
198 EXPECT_FALSE(graph_->IsDebuggable());
199 EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
200 static const char* const expected[] = {
201 "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
202 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100203 "ranges: { [4,23) }, uses: { 19 23 }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000204 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100205 "ranges: { [6,23) }, uses: { 23 }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000206 // Environment use in HDeoptimize keeps even the non-reference argument alive.
Artem Serovd6750532018-05-30 20:07:43 +0100207 "ranges: { [8,21) }, uses: { }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000208 // Environment uses keep the reference argument alive.
209 "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
210 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100211 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000212 size_t arg_index = 0u;
213 for (HInstruction* arg : args) {
214 std::ostringstream arg_dump;
215 arg->GetLiveInterval()->Dump(arg_dump);
216 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
217 ++arg_index;
218 }
219}
220
221} // namespace art