blob: cc48d3196b68c8d3cc58cffa84eeefb5927e3ab4 [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
17#include "arch/instruction_set.h"
18#include "arch/instruction_set_features.h"
19#include "base/arena_allocator.h"
20#include "base/arena_containers.h"
21#include "driver/compiler_options.h"
22#include "code_generator.h"
23#include "nodes.h"
24#include "optimizing_unit_test.h"
25#include "ssa_liveness_analysis.h"
26
27namespace art {
28
29class SsaLivenessAnalysisTest : public testing::Test {
30 public:
31 SsaLivenessAnalysisTest()
32 : pool_(),
33 allocator_(&pool_),
34 graph_(CreateGraph(&allocator_)),
35 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_,
42 CompilerOptions());
43 CHECK(codegen_ != nullptr) << instruction_set_ << " is not a supported target architecture.";
44 // Create entry block.
45 entry_ = new (&allocator_) HBasicBlock(graph_);
46 graph_->AddBlock(entry_);
47 graph_->SetEntryBlock(entry_);
48 }
49
50 protected:
51 HBasicBlock* CreateSuccessor(HBasicBlock* block) {
52 HGraph* graph = block->GetGraph();
53 HBasicBlock* successor = new (&allocator_) HBasicBlock(graph);
54 graph->AddBlock(successor);
55 block->AddSuccessor(successor);
56 return successor;
57 }
58
59 ArenaPool pool_;
60 ArenaAllocator allocator_;
61 HGraph* graph_;
62 InstructionSet instruction_set_;
63 std::unique_ptr<const InstructionSetFeatures> instruction_set_features_;
64 std::unique_ptr<CodeGenerator> codegen_;
65 HBasicBlock* entry_;
66};
67
68TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
69 HInstruction* arg = new (&allocator_) HParameterValue(
70 graph_->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimInt);
71 entry_->AddInstruction(arg);
72
73 HBasicBlock* block = CreateSuccessor(entry_);
74 HInstruction* ret = new (&allocator_) HReturn(arg);
75 block->AddInstruction(ret);
76 block->AddInstruction(new (&allocator_) HExit());
77
78 graph_->BuildDominatorTree();
79 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
80 ssa_analysis.Analyze();
81
82 std::ostringstream arg_dump;
83 arg->GetLiveInterval()->Dump(arg_dump);
84 EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
85 arg_dump.str().c_str());
86}
87
88TEST_F(SsaLivenessAnalysisTest, TestAput) {
89 HInstruction* array = new (&allocator_) HParameterValue(
90 graph_->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
91 HInstruction* index = new (&allocator_) HParameterValue(
92 graph_->GetDexFile(), dex::TypeIndex(1), 1, Primitive::kPrimInt);
93 HInstruction* value = new (&allocator_) HParameterValue(
94 graph_->GetDexFile(), dex::TypeIndex(2), 2, Primitive::kPrimInt);
95 HInstruction* extra_arg1 = new (&allocator_) HParameterValue(
96 graph_->GetDexFile(), dex::TypeIndex(3), 3, Primitive::kPrimInt);
97 HInstruction* extra_arg2 = new (&allocator_) HParameterValue(
98 graph_->GetDexFile(), dex::TypeIndex(4), 4, Primitive::kPrimNot);
99 ArenaVector<HInstruction*> args({ array, index, value, extra_arg1, extra_arg2 },
100 allocator_.Adapter());
101 for (HInstruction* insn : args) {
102 entry_->AddInstruction(insn);
103 }
104
105 HBasicBlock* block = CreateSuccessor(entry_);
106 HInstruction* null_check = new (&allocator_) HNullCheck(array, 0);
107 block->AddInstruction(null_check);
108 HEnvironment* null_check_env = new (&allocator_) HEnvironment(&allocator_,
109 /* number_of_vregs */ 5,
110 /* method */ nullptr,
111 /* dex_pc */ 0u,
112 null_check);
113 null_check_env->CopyFrom(args);
114 null_check->SetRawEnvironment(null_check_env);
115 HInstruction* length = new (&allocator_) HArrayLength(array, 0);
116 block->AddInstruction(length);
117 HInstruction* bounds_check = new (&allocator_) HBoundsCheck(index, length, /* dex_pc */ 0u);
118 block->AddInstruction(bounds_check);
119 HEnvironment* bounds_check_env = new (&allocator_) HEnvironment(&allocator_,
120 /* number_of_vregs */ 5,
121 /* method */ nullptr,
122 /* dex_pc */ 0u,
123 bounds_check);
124 bounds_check_env->CopyFrom(args);
125 bounds_check->SetRawEnvironment(bounds_check_env);
126 HInstruction* array_set =
127 new (&allocator_) HArraySet(array, index, value, Primitive::kPrimInt, /* dex_pc */ 0);
128 block->AddInstruction(array_set);
129
130 graph_->BuildDominatorTree();
131 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
132 ssa_analysis.Analyze();
133
134 EXPECT_FALSE(graph_->IsDebuggable());
135 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
136 static const char* const expected[] = {
137 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
138 "is_high: 0",
139 "ranges: { [4,21) }, uses: { 19 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
140 "is_high: 0",
141 "ranges: { [6,21) }, uses: { 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
142 "is_high: 0",
143 // Environment uses do not keep the non-reference argument alive.
144 "ranges: { [8,10) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
145 // Environment uses keep the reference argument alive.
146 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
147 };
148 ASSERT_EQ(arraysize(expected), args.size());
149 size_t arg_index = 0u;
150 for (HInstruction* arg : args) {
151 std::ostringstream arg_dump;
152 arg->GetLiveInterval()->Dump(arg_dump);
153 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
154 ++arg_index;
155 }
156}
157
158TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
159 HInstruction* array = new (&allocator_) HParameterValue(
160 graph_->GetDexFile(), dex::TypeIndex(0), 0, Primitive::kPrimNot);
161 HInstruction* index = new (&allocator_) HParameterValue(
162 graph_->GetDexFile(), dex::TypeIndex(1), 1, Primitive::kPrimInt);
163 HInstruction* value = new (&allocator_) HParameterValue(
164 graph_->GetDexFile(), dex::TypeIndex(2), 2, Primitive::kPrimInt);
165 HInstruction* extra_arg1 = new (&allocator_) HParameterValue(
166 graph_->GetDexFile(), dex::TypeIndex(3), 3, Primitive::kPrimInt);
167 HInstruction* extra_arg2 = new (&allocator_) HParameterValue(
168 graph_->GetDexFile(), dex::TypeIndex(4), 4, Primitive::kPrimNot);
169 ArenaVector<HInstruction*> args({ array, index, value, extra_arg1, extra_arg2 },
170 allocator_.Adapter());
171 for (HInstruction* insn : args) {
172 entry_->AddInstruction(insn);
173 }
174
175 HBasicBlock* block = CreateSuccessor(entry_);
176 HInstruction* null_check = new (&allocator_) HNullCheck(array, 0);
177 block->AddInstruction(null_check);
178 HEnvironment* null_check_env = new (&allocator_) HEnvironment(&allocator_,
179 /* number_of_vregs */ 5,
180 /* method */ nullptr,
181 /* dex_pc */ 0u,
182 null_check);
183 null_check_env->CopyFrom(args);
184 null_check->SetRawEnvironment(null_check_env);
185 HInstruction* length = new (&allocator_) HArrayLength(array, 0);
186 block->AddInstruction(length);
187 // Use HAboveOrEqual+HDeoptimize as the bounds check.
188 HInstruction* ae = new (&allocator_) HAboveOrEqual(index, length);
189 block->AddInstruction(ae);
190 HInstruction* deoptimize = new(&allocator_) HDeoptimize(ae, /* dex_pc */ 0u);
191 block->AddInstruction(deoptimize);
192 HEnvironment* deoptimize_env = new (&allocator_) HEnvironment(&allocator_,
193 /* number_of_vregs */ 5,
194 /* method */ nullptr,
195 /* dex_pc */ 0u,
196 deoptimize);
197 deoptimize_env->CopyFrom(args);
198 deoptimize->SetRawEnvironment(deoptimize_env);
199 HInstruction* array_set =
200 new (&allocator_) HArraySet(array, index, value, Primitive::kPrimInt, /* dex_pc */ 0);
201 block->AddInstruction(array_set);
202
203 graph_->BuildDominatorTree();
204 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
205 ssa_analysis.Analyze();
206
207 EXPECT_FALSE(graph_->IsDebuggable());
208 EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
209 static const char* const expected[] = {
210 "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
211 "is_high: 0",
212 "ranges: { [4,23) }, uses: { 19 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
213 "is_high: 0",
214 "ranges: { [6,23) }, uses: { 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
215 // Environment use in HDeoptimize keeps even the non-reference argument alive.
216 "ranges: { [8,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
217 // Environment uses keep the reference argument alive.
218 "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
219 };
220 ASSERT_EQ(arraysize(expected), args.size());
221 size_t arg_index = 0u;
222 for (HInstruction* arg : args) {
223 std::ostringstream arg_dump;
224 arg->GetLiveInterval()->Dump(arg_dump);
225 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
226 ++arg_index;
227 }
228}
229
230} // namespace art