blob: b9bfbaa17394a38d1d76453337ba52fe0e72f55e [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 Marko356bd282017-03-01 12:01:11 +000031 public:
32 SsaLivenessAnalysisTest()
Vladimir Markoca6fff82017-10-03 14:49:14 +010033 : graph_(CreateGraph()),
Vladimir Markobd68e972017-03-14 18:07:35 +000034 compiler_options_(),
Vladimir Marko356bd282017-03-01 12:01:11 +000035 instruction_set_(kRuntimeISA) {
36 std::string error_msg;
37 instruction_set_features_ =
38 InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg);
39 codegen_ = CodeGenerator::Create(graph_,
40 instruction_set_,
41 *instruction_set_features_,
Vladimir Markobd68e972017-03-14 18:07:35 +000042 compiler_options_);
Vladimir Marko356bd282017-03-01 12:01:11 +000043 CHECK(codegen_ != nullptr) << instruction_set_ << " is not a supported target architecture.";
44 // Create entry block.
Vladimir Markoca6fff82017-10-03 14:49:14 +010045 entry_ = new (GetAllocator()) HBasicBlock(graph_);
Vladimir Marko356bd282017-03-01 12:01:11 +000046 graph_->AddBlock(entry_);
47 graph_->SetEntryBlock(entry_);
48 }
49
50 protected:
51 HBasicBlock* CreateSuccessor(HBasicBlock* block) {
52 HGraph* graph = block->GetGraph();
Vladimir Markoca6fff82017-10-03 14:49:14 +010053 HBasicBlock* successor = new (GetAllocator()) HBasicBlock(graph);
Vladimir Marko356bd282017-03-01 12:01:11 +000054 graph->AddBlock(successor);
55 block->AddSuccessor(successor);
56 return successor;
57 }
58
Vladimir Marko356bd282017-03-01 12:01:11 +000059 HGraph* graph_;
Vladimir Markobd68e972017-03-14 18:07:35 +000060 CompilerOptions compiler_options_;
Vladimir Marko356bd282017-03-01 12:01:11 +000061 InstructionSet instruction_set_;
62 std::unique_ptr<const InstructionSetFeatures> instruction_set_features_;
63 std::unique_ptr<CodeGenerator> codegen_;
64 HBasicBlock* entry_;
65};
66
67TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010068 HInstruction* arg = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000070 entry_->AddInstruction(arg);
71
72 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +010073 HInstruction* ret = new (GetAllocator()) HReturn(arg);
Vladimir Marko356bd282017-03-01 12:01:11 +000074 block->AddInstruction(ret);
Vladimir Markoca6fff82017-10-03 14:49:14 +010075 block->AddInstruction(new (GetAllocator()) HExit());
Vladimir Marko356bd282017-03-01 12:01:11 +000076
77 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +010078 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +000079 ssa_analysis.Analyze();
80
81 std::ostringstream arg_dump;
82 arg->GetLiveInterval()->Dump(arg_dump);
83 EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
84 arg_dump.str().c_str());
85}
86
87TEST_F(SsaLivenessAnalysisTest, TestAput) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010088 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010089 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +010090 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010091 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010092 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010094 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +010096 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010097 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +010098 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +000099 for (HInstruction* insn : args) {
100 entry_->AddInstruction(insn);
101 }
102
103 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100104 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000105 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100106 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
107 /* number_of_vregs */ 5,
108 /* method */ nullptr,
109 /* dex_pc */ 0u,
110 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100111 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000112 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100113 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000114 block->AddInstruction(length);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100115 HInstruction* bounds_check = new (GetAllocator()) HBoundsCheck(index, length, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000116 block->AddInstruction(bounds_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100117 HEnvironment* bounds_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
118 /* number_of_vregs */ 5,
119 /* method */ nullptr,
120 /* dex_pc */ 0u,
121 bounds_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100122 bounds_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000123 bounds_check->SetRawEnvironment(bounds_check_env);
124 HInstruction* array_set =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100125 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000126 block->AddInstruction(array_set);
127
128 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100129 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000130 ssa_analysis.Analyze();
131
132 EXPECT_FALSE(graph_->IsDebuggable());
133 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
134 static const char* const expected[] = {
135 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
136 "is_high: 0",
137 "ranges: { [4,21) }, uses: { 19 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
138 "is_high: 0",
139 "ranges: { [6,21) }, uses: { 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
140 "is_high: 0",
141 // Environment uses do not keep the non-reference argument alive.
142 "ranges: { [8,10) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
143 // Environment uses keep the reference argument alive.
144 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
145 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100146 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000147 size_t arg_index = 0u;
148 for (HInstruction* arg : args) {
149 std::ostringstream arg_dump;
150 arg->GetLiveInterval()->Dump(arg_dump);
151 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
152 ++arg_index;
153 }
154}
155
156TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100157 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100160 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100161 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100162 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100163 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100164 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100165 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100166 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100167 HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 };
Vladimir Marko356bd282017-03-01 12:01:11 +0000168 for (HInstruction* insn : args) {
169 entry_->AddInstruction(insn);
170 }
171
172 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100173 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000174 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100175 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
176 /* number_of_vregs */ 5,
177 /* method */ nullptr,
178 /* dex_pc */ 0u,
179 null_check);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100180 null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000181 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100182 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000183 block->AddInstruction(length);
184 // Use HAboveOrEqual+HDeoptimize as the bounds check.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100185 HInstruction* ae = new (GetAllocator()) HAboveOrEqual(index, length);
Vladimir Marko356bd282017-03-01 12:01:11 +0000186 block->AddInstruction(ae);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100187 HInstruction* deoptimize = new(GetAllocator()) HDeoptimize(
188 GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000189 block->AddInstruction(deoptimize);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100190 HEnvironment* deoptimize_env = new (GetAllocator()) HEnvironment(GetAllocator(),
191 /* number_of_vregs */ 5,
192 /* method */ nullptr,
193 /* dex_pc */ 0u,
194 deoptimize);
Vladimir Marko69d310e2017-10-09 14:12:23 +0100195 deoptimize_env->CopyFrom(ArrayRef<HInstruction* const>(args));
Vladimir Marko356bd282017-03-01 12:01:11 +0000196 deoptimize->SetRawEnvironment(deoptimize_env);
197 HInstruction* array_set =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100198 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000199 block->AddInstruction(array_set);
200
201 graph_->BuildDominatorTree();
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100202 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
Vladimir Marko356bd282017-03-01 12:01:11 +0000203 ssa_analysis.Analyze();
204
205 EXPECT_FALSE(graph_->IsDebuggable());
206 EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
207 static const char* const expected[] = {
208 "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
209 "is_high: 0",
210 "ranges: { [4,23) }, uses: { 19 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
211 "is_high: 0",
212 "ranges: { [6,23) }, uses: { 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
213 // Environment use in HDeoptimize keeps even the non-reference argument alive.
214 "ranges: { [8,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
215 // Environment uses keep the reference argument alive.
216 "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
217 };
Vladimir Marko69d310e2017-10-09 14:12:23 +0100218 static_assert(arraysize(expected) == arraysize(args), "Array size check.");
Vladimir Marko356bd282017-03-01 12:01:11 +0000219 size_t arg_index = 0u;
220 for (HInstruction* arg : args) {
221 std::ostringstream arg_dump;
222 arg->GetLiveInterval()->Dump(arg_dump);
223 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
224 ++arg_index;
225 }
226}
227
228} // namespace art