blob: a477893d57bbee58df6aa14b597837c6904e5011 [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"
Vladimir Marko356bd282017-03-01 12:01:11 +000023#include "code_generator.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "driver/compiler_options.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000025#include "nodes.h"
26#include "optimizing_unit_test.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000027
Vladimir Marko0a516052019-10-14 13:00:44 +000028namespace art {
Vladimir Marko356bd282017-03-01 12:01:11 +000029
Vladimir Markoca6fff82017-10-03 14:49:14 +010030class SsaLivenessAnalysisTest : public OptimizingUnitTest {
Vladimir Markoa0431112018-06-25 09:32:54 +010031 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010032 void SetUp() override {
Vladimir Markoa0431112018-06-25 09:32:54 +010033 OptimizingUnitTest::SetUp();
34 graph_ = CreateGraph();
Vladimir Markof91fc122020-05-13 09:21:00 +010035 compiler_options_ = CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default");
Vladimir Markoa0431112018-06-25 09:32:54 +010036 codegen_ = CodeGenerator::Create(graph_, *compiler_options_);
Vladimir Markof91fc122020-05-13 09:21:00 +010037 CHECK(codegen_ != nullptr);
Vladimir Marko356bd282017-03-01 12:01:11 +000038 // Create entry block.
Vladimir Markoca6fff82017-10-03 14:49:14 +010039 entry_ = new (GetAllocator()) HBasicBlock(graph_);
Vladimir Marko356bd282017-03-01 12:01:11 +000040 graph_->AddBlock(entry_);
41 graph_->SetEntryBlock(entry_);
42 }
43
44 protected:
45 HBasicBlock* CreateSuccessor(HBasicBlock* block) {
46 HGraph* graph = block->GetGraph();
Vladimir Markoca6fff82017-10-03 14:49:14 +010047 HBasicBlock* successor = new (GetAllocator()) HBasicBlock(graph);
Vladimir Marko356bd282017-03-01 12:01:11 +000048 graph->AddBlock(successor);
49 block->AddSuccessor(successor);
50 return successor;
51 }
52
Vladimir Marko356bd282017-03-01 12:01:11 +000053 HGraph* graph_;
Vladimir Markof91fc122020-05-13 09:21:00 +010054 std::unique_ptr<CompilerOptions> compiler_options_;
Vladimir Marko356bd282017-03-01 12:01:11 +000055 std::unique_ptr<CodeGenerator> codegen_;
56 HBasicBlock* entry_;
57};
58
59TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010060 HInstruction* arg = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010061 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000062 entry_->AddInstruction(arg);
63
64 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010065 HInstruction* ret = new (GetAllocator()) HReturn(arg);
Vladimir Marko356bd282017-03-01 12:01:11 +000066 block->AddInstruction(ret);
Vladimir Markoca6fff82017-10-03 14:49:14 +010067 block->AddInstruction(new (GetAllocator()) HExit());
Vladimir Marko356bd282017-03-01 12:01:11 +000068
69 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +010070 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +000071 ssa_analysis.Analyze();
72
73 std::ostringstream arg_dump;
74 arg->GetLiveInterval()->Dump(arg_dump);
75 EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
76 arg_dump.str().c_str());
77}
78
79TEST_F(SsaLivenessAnalysisTest, TestAput) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010080 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +010082 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010084 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010086 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010088 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010089 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +010090 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +000091 for (HInstruction* insn : args) {
92 entry_->AddInstruction(insn);
93 }
94
95 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010096 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +000097 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +010098 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -080099 /* number_of_vregs= */ 5,
100 /* method= */ nullptr,
101 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100102 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100103 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000104 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100105 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000106 block->AddInstruction(length);
Andreas Gampe3db70682018-12-26 15:12:03 -0800107 HInstruction* bounds_check = new (GetAllocator()) HBoundsCheck(index, length, /* dex_pc= */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000108 block->AddInstruction(bounds_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100109 HEnvironment* bounds_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800110 /* number_of_vregs= */ 5,
111 /* method= */ nullptr,
112 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100113 bounds_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100114 bounds_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000115 bounds_check->SetRawEnvironment(bounds_check_env);
116 HInstruction* array_set =
Andreas Gampe3db70682018-12-26 15:12:03 -0800117 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc= */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000118 block->AddInstruction(array_set);
119
120 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100121 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000122 ssa_analysis.Analyze();
123
124 EXPECT_FALSE(graph_->IsDebuggable());
125 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
126 static const char* const expected[] = {
127 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
128 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100129 "ranges: { [4,21) }, uses: { 19 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000130 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100131 "ranges: { [6,21) }, uses: { 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000132 "is_high: 0",
133 // Environment uses do not keep the non-reference argument alive.
Artem Serovd6750532018-05-30 20:07:43 +0100134 "ranges: { [8,10) }, uses: { }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000135 // Environment uses keep the reference argument alive.
136 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
137 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100138 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000139 size_t arg_index = 0u;
140 for (HInstruction* arg : args) {
141 std::ostringstream arg_dump;
142 arg->GetLiveInterval()->Dump(arg_dump);
143 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
144 ++arg_index;
145 }
146}
147
148TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100151 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100153 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100155 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100156 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100157 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100159 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +0000160 for (HInstruction* insn : args) {
161 entry_->AddInstruction(insn);
162 }
163
164 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100165 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000166 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100167 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800168 /* number_of_vregs= */ 5,
169 /* method= */ nullptr,
170 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100171 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100172 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000173 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100174 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000175 block->AddInstruction(length);
176 // Use HAboveOrEqual+HDeoptimize as the bounds check.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100177 HInstruction* ae = new (GetAllocator()) HAboveOrEqual(index, length);
Vladimir Marko356bd282017-03-01 12:01:11 +0000178 block->AddInstruction(ae);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100179 HInstruction* deoptimize = new(GetAllocator()) HDeoptimize(
Andreas Gampe3db70682018-12-26 15:12:03 -0800180 GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc= */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000181 block->AddInstruction(deoptimize);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100182 HEnvironment* deoptimize_env = new (GetAllocator()) HEnvironment(GetAllocator(),
Andreas Gampe3db70682018-12-26 15:12:03 -0800183 /* number_of_vregs= */ 5,
184 /* method= */ nullptr,
185 /* dex_pc= */ 0u,
Vladimir Markoca6fff82017-10-03 14:49:14 +0100186 deoptimize);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100187 deoptimize_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000188 deoptimize->SetRawEnvironment(deoptimize_env);
189 HInstruction* array_set =
Andreas Gampe3db70682018-12-26 15:12:03 -0800190 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc= */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000191 block->AddInstruction(array_set);
192
193 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100194 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000195 ssa_analysis.Analyze();
196
197 EXPECT_FALSE(graph_->IsDebuggable());
198 EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
199 static const char* const expected[] = {
200 "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
201 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100202 "ranges: { [4,23) }, uses: { 19 23 }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000203 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100204 "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 +0000205 // Environment use in HDeoptimize keeps even the non-reference argument alive.
Artem Serovd6750532018-05-30 20:07:43 +0100206 "ranges: { [8,21) }, uses: { }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000207 // Environment uses keep the reference argument alive.
208 "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
209 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100210 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000211 size_t arg_index = 0u;
212 for (HInstruction* arg : args) {
213 std::ostringstream arg_dump;
214 arg->GetLiveInterval()->Dump(arg_dump);
215 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
216 ++arg_index;
217 }
218}
219
220} // namespace art