blob: e89bf6d8018280675bfbc4c656f0bcd15332f50d [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
30class SsaLivenessAnalysisTest : public testing::Test {
31 public:
32 SsaLivenessAnalysisTest()
33 : pool_(),
34 allocator_(&pool_),
35 graph_(CreateGraph(&allocator_)),
Vladimir Markobd68e972017-03-14 18:07:35 +000036 compiler_options_(),
Vladimir Marko356bd282017-03-01 12:01:11 +000037 instruction_set_(kRuntimeISA) {
38 std::string error_msg;
39 instruction_set_features_ =
40 InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg);
41 codegen_ = CodeGenerator::Create(graph_,
42 instruction_set_,
43 *instruction_set_features_,
Vladimir Markobd68e972017-03-14 18:07:35 +000044 compiler_options_);
Vladimir Marko356bd282017-03-01 12:01:11 +000045 CHECK(codegen_ != nullptr) << instruction_set_ << " is not a supported target architecture.";
46 // Create entry block.
47 entry_ = new (&allocator_) HBasicBlock(graph_);
48 graph_->AddBlock(entry_);
49 graph_->SetEntryBlock(entry_);
50 }
51
52 protected:
53 HBasicBlock* CreateSuccessor(HBasicBlock* block) {
54 HGraph* graph = block->GetGraph();
55 HBasicBlock* successor = new (&allocator_) HBasicBlock(graph);
56 graph->AddBlock(successor);
57 block->AddSuccessor(successor);
58 return successor;
59 }
60
61 ArenaPool pool_;
62 ArenaAllocator allocator_;
63 HGraph* graph_;
Vladimir Markobd68e972017-03-14 18:07:35 +000064 CompilerOptions compiler_options_;
Vladimir Marko356bd282017-03-01 12:01:11 +000065 InstructionSet instruction_set_;
66 std::unique_ptr<const InstructionSetFeatures> instruction_set_features_;
67 std::unique_ptr<CodeGenerator> codegen_;
68 HBasicBlock* entry_;
69};
70
71TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
72 HInstruction* arg = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000074 entry_->AddInstruction(arg);
75
76 HBasicBlock* block = CreateSuccessor(entry_);
77 HInstruction* ret = new (&allocator_) HReturn(arg);
78 block->AddInstruction(ret);
79 block->AddInstruction(new (&allocator_) HExit());
80
81 graph_->BuildDominatorTree();
82 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
83 ssa_analysis.Analyze();
84
85 std::ostringstream arg_dump;
86 arg->GetLiveInterval()->Dump(arg_dump);
87 EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
88 arg_dump.str().c_str());
89}
90
91TEST_F(SsaLivenessAnalysisTest, TestAput) {
92 HInstruction* array = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Marko356bd282017-03-01 12:01:11 +000094 HInstruction* index = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000096 HInstruction* value = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010097 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +000098 HInstruction* extra_arg1 = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010099 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +0000100 HInstruction* extra_arg2 = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100101 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko356bd282017-03-01 12:01:11 +0000102 ArenaVector<HInstruction*> args({ array, index, value, extra_arg1, extra_arg2 },
103 allocator_.Adapter());
104 for (HInstruction* insn : args) {
105 entry_->AddInstruction(insn);
106 }
107
108 HBasicBlock* block = CreateSuccessor(entry_);
109 HInstruction* null_check = new (&allocator_) HNullCheck(array, 0);
110 block->AddInstruction(null_check);
111 HEnvironment* null_check_env = new (&allocator_) HEnvironment(&allocator_,
112 /* number_of_vregs */ 5,
113 /* method */ nullptr,
114 /* dex_pc */ 0u,
115 null_check);
116 null_check_env->CopyFrom(args);
117 null_check->SetRawEnvironment(null_check_env);
118 HInstruction* length = new (&allocator_) HArrayLength(array, 0);
119 block->AddInstruction(length);
120 HInstruction* bounds_check = new (&allocator_) HBoundsCheck(index, length, /* dex_pc */ 0u);
121 block->AddInstruction(bounds_check);
122 HEnvironment* bounds_check_env = new (&allocator_) HEnvironment(&allocator_,
123 /* number_of_vregs */ 5,
124 /* method */ nullptr,
125 /* dex_pc */ 0u,
126 bounds_check);
127 bounds_check_env->CopyFrom(args);
128 bounds_check->SetRawEnvironment(bounds_check_env);
129 HInstruction* array_set =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130 new (&allocator_) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000131 block->AddInstruction(array_set);
132
133 graph_->BuildDominatorTree();
134 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
135 ssa_analysis.Analyze();
136
137 EXPECT_FALSE(graph_->IsDebuggable());
138 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
139 static const char* const expected[] = {
140 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
141 "is_high: 0",
142 "ranges: { [4,21) }, uses: { 19 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
143 "is_high: 0",
144 "ranges: { [6,21) }, uses: { 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
145 "is_high: 0",
146 // Environment uses do not keep the non-reference argument alive.
147 "ranges: { [8,10) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
148 // Environment uses keep the reference argument alive.
149 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
150 };
151 ASSERT_EQ(arraysize(expected), args.size());
152 size_t arg_index = 0u;
153 for (HInstruction* arg : args) {
154 std::ostringstream arg_dump;
155 arg->GetLiveInterval()->Dump(arg_dump);
156 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
157 ++arg_index;
158 }
159}
160
161TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
162 HInstruction* array = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100163 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Marko356bd282017-03-01 12:01:11 +0000164 HInstruction* index = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100165 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +0000166 HInstruction* value = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100167 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +0000168 HInstruction* extra_arg1 = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100169 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Marko356bd282017-03-01 12:01:11 +0000170 HInstruction* extra_arg2 = new (&allocator_) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100171 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko356bd282017-03-01 12:01:11 +0000172 ArenaVector<HInstruction*> args({ array, index, value, extra_arg1, extra_arg2 },
173 allocator_.Adapter());
174 for (HInstruction* insn : args) {
175 entry_->AddInstruction(insn);
176 }
177
178 HBasicBlock* block = CreateSuccessor(entry_);
179 HInstruction* null_check = new (&allocator_) HNullCheck(array, 0);
180 block->AddInstruction(null_check);
181 HEnvironment* null_check_env = new (&allocator_) HEnvironment(&allocator_,
182 /* number_of_vregs */ 5,
183 /* method */ nullptr,
184 /* dex_pc */ 0u,
185 null_check);
186 null_check_env->CopyFrom(args);
187 null_check->SetRawEnvironment(null_check_env);
188 HInstruction* length = new (&allocator_) HArrayLength(array, 0);
189 block->AddInstruction(length);
190 // Use HAboveOrEqual+HDeoptimize as the bounds check.
191 HInstruction* ae = new (&allocator_) HAboveOrEqual(index, length);
192 block->AddInstruction(ae);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000193 HInstruction* deoptimize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100194 new(&allocator_) HDeoptimize(&allocator_, ae, DeoptimizationKind::kBlockBCE, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000195 block->AddInstruction(deoptimize);
196 HEnvironment* deoptimize_env = new (&allocator_) HEnvironment(&allocator_,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000197 /* number_of_vregs */ 5,
198 /* method */ nullptr,
199 /* dex_pc */ 0u,
200 deoptimize);
Vladimir Marko356bd282017-03-01 12:01:11 +0000201 deoptimize_env->CopyFrom(args);
202 deoptimize->SetRawEnvironment(deoptimize_env);
203 HInstruction* array_set =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100204 new (&allocator_) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000205 block->AddInstruction(array_set);
206
207 graph_->BuildDominatorTree();
208 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
209 ssa_analysis.Analyze();
210
211 EXPECT_FALSE(graph_->IsDebuggable());
212 EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
213 static const char* const expected[] = {
214 "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
215 "is_high: 0",
216 "ranges: { [4,23) }, uses: { 19 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
217 "is_high: 0",
218 "ranges: { [6,23) }, uses: { 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
219 // Environment use in HDeoptimize keeps even the non-reference argument alive.
220 "ranges: { [8,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
221 // Environment uses keep the reference argument alive.
222 "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
223 };
224 ASSERT_EQ(arraysize(expected), args.size());
225 size_t arg_index = 0u;
226 for (HInstruction* arg : args) {
227 std::ostringstream arg_dump;
228 arg->GetLiveInterval()->Dump(arg_dump);
229 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
230 ++arg_index;
231 }
232}
233
234} // namespace art