blob: a683c698d9eddea8e1c6be86eb00012bb42d1e5c [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
28namespace art {
29
Vladimir Markoca6fff82017-10-03 14:49:14 +010030class SsaLivenessAnalysisTest : public OptimizingUnitTest {
Vladimir Markoa0431112018-06-25 09:32:54 +010031 protected:
32 void SetUp() OVERRIDE {
33 OptimizingUnitTest::SetUp();
34 graph_ = CreateGraph();
35 codegen_ = CodeGenerator::Create(graph_, *compiler_options_);
Vladimir Marko356bd282017-03-01 12:01:11 +000036 CHECK(codegen_ != nullptr) << instruction_set_ << " is not a supported target architecture.";
37 // Create entry block.
Vladimir Markoca6fff82017-10-03 14:49:14 +010038 entry_ = new (GetAllocator()) HBasicBlock(graph_);
Vladimir Marko356bd282017-03-01 12:01:11 +000039 graph_->AddBlock(entry_);
40 graph_->SetEntryBlock(entry_);
41 }
42
43 protected:
44 HBasicBlock* CreateSuccessor(HBasicBlock* block) {
45 HGraph* graph = block->GetGraph();
Vladimir Markoca6fff82017-10-03 14:49:14 +010046 HBasicBlock* successor = new (GetAllocator()) HBasicBlock(graph);
Vladimir Marko356bd282017-03-01 12:01:11 +000047 graph->AddBlock(successor);
48 block->AddSuccessor(successor);
49 return successor;
50 }
51
Vladimir Marko356bd282017-03-01 12:01:11 +000052 HGraph* graph_;
Vladimir Marko356bd282017-03-01 12:01:11 +000053 std::unique_ptr<CodeGenerator> codegen_;
54 HBasicBlock* entry_;
55};
56
57TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010058 HInstruction* arg = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000060 entry_->AddInstruction(arg);
61
62 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010063 HInstruction* ret = new (GetAllocator()) HReturn(arg);
Vladimir Marko356bd282017-03-01 12:01:11 +000064 block->AddInstruction(ret);
Vladimir Markoca6fff82017-10-03 14:49:14 +010065 block->AddInstruction(new (GetAllocator()) HExit());
Vladimir Marko356bd282017-03-01 12:01:11 +000066
67 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +010068 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +000069 ssa_analysis.Analyze();
70
71 std::ostringstream arg_dump;
72 arg->GetLiveInterval()->Dump(arg_dump);
73 EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
74 arg_dump.str().c_str());
75}
76
77TEST_F(SsaLivenessAnalysisTest, TestAput) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010078 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +010080 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010082 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010084 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010086 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +010088 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +000089 for (HInstruction* insn : args) {
90 entry_->AddInstruction(insn);
91 }
92
93 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010094 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +000095 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +010096 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
97 /* number_of_vregs */ 5,
98 /* method */ nullptr,
99 /* dex_pc */ 0u,
100 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100101 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000102 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100103 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000104 block->AddInstruction(length);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100105 HInstruction* bounds_check = new (GetAllocator()) HBoundsCheck(index, length, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000106 block->AddInstruction(bounds_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100107 HEnvironment* bounds_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
108 /* number_of_vregs */ 5,
109 /* method */ nullptr,
110 /* dex_pc */ 0u,
111 bounds_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100112 bounds_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000113 bounds_check->SetRawEnvironment(bounds_check_env);
114 HInstruction* array_set =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100115 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000116 block->AddInstruction(array_set);
117
118 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100119 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000120 ssa_analysis.Analyze();
121
122 EXPECT_FALSE(graph_->IsDebuggable());
123 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
124 static const char* const expected[] = {
125 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
126 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100127 "ranges: { [4,21) }, uses: { 19 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000128 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100129 "ranges: { [6,21) }, uses: { 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000130 "is_high: 0",
131 // Environment uses do not keep the non-reference argument alive.
Artem Serovd6750532018-05-30 20:07:43 +0100132 "ranges: { [8,10) }, uses: { }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000133 // Environment uses keep the reference argument alive.
134 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
135 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100136 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000137 size_t arg_index = 0u;
138 for (HInstruction* arg : args) {
139 std::ostringstream arg_dump;
140 arg->GetLiveInterval()->Dump(arg_dump);
141 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
142 ++arg_index;
143 }
144}
145
146TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100147 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100151 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100153 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100155 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100156 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100157 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +0000158 for (HInstruction* insn : args) {
159 entry_->AddInstruction(insn);
160 }
161
162 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100163 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000164 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100165 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
166 /* number_of_vregs */ 5,
167 /* method */ nullptr,
168 /* dex_pc */ 0u,
169 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100170 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000171 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100172 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000173 block->AddInstruction(length);
174 // Use HAboveOrEqual+HDeoptimize as the bounds check.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100175 HInstruction* ae = new (GetAllocator()) HAboveOrEqual(index, length);
Vladimir Marko356bd282017-03-01 12:01:11 +0000176 block->AddInstruction(ae);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100177 HInstruction* deoptimize = new(GetAllocator()) HDeoptimize(
178 GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000179 block->AddInstruction(deoptimize);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100180 HEnvironment* deoptimize_env = new (GetAllocator()) HEnvironment(GetAllocator(),
181 /* number_of_vregs */ 5,
182 /* method */ nullptr,
183 /* dex_pc */ 0u,
184 deoptimize);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100185 deoptimize_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000186 deoptimize->SetRawEnvironment(deoptimize_env);
187 HInstruction* array_set =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100188 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000189 block->AddInstruction(array_set);
190
191 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100192 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000193 ssa_analysis.Analyze();
194
195 EXPECT_FALSE(graph_->IsDebuggable());
196 EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
197 static const char* const expected[] = {
198 "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
199 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100200 "ranges: { [4,23) }, uses: { 19 23 }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 "
Vladimir Marko356bd282017-03-01 12:01:11 +0000201 "is_high: 0",
Artem Serovd6750532018-05-30 20:07:43 +0100202 "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 +0000203 // Environment use in HDeoptimize keeps even the non-reference argument alive.
Artem Serovd6750532018-05-30 20:07:43 +0100204 "ranges: { [8,21) }, uses: { }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
Vladimir Marko356bd282017-03-01 12:01:11 +0000205 // Environment uses keep the reference argument alive.
206 "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
207 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100208 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000209 size_t arg_index = 0u;
210 for (HInstruction* arg : args) {
211 std::ostringstream arg_dump;
212 arg->GetLiveInterval()->Dump(arg_dump);
213 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
214 ++arg_index;
215 }
216}
217
218} // namespace art