blob: 82ee441aa8b9ad8db1bfce1d59988c9f1ba748db [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();
78 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
79 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 Marko356bd282017-03-01 12:01:11 +000098 ArenaVector<HInstruction*> args({ array, index, value, extra_arg1, extra_arg2 },
Vladimir Markoca6fff82017-10-03 14:49:14 +010099 GetAllocator()->Adapter());
Vladimir Marko356bd282017-03-01 12:01:11 +0000100 for (HInstruction* insn : args) {
101 entry_->AddInstruction(insn);
102 }
103
104 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100105 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000106 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100107 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
108 /* number_of_vregs */ 5,
109 /* method */ nullptr,
110 /* dex_pc */ 0u,
111 null_check);
Vladimir Marko356bd282017-03-01 12:01:11 +0000112 null_check_env->CopyFrom(args);
113 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100114 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000115 block->AddInstruction(length);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100116 HInstruction* bounds_check = new (GetAllocator()) HBoundsCheck(index, length, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000117 block->AddInstruction(bounds_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100118 HEnvironment* bounds_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
119 /* number_of_vregs */ 5,
120 /* method */ nullptr,
121 /* dex_pc */ 0u,
122 bounds_check);
Vladimir Marko356bd282017-03-01 12:01:11 +0000123 bounds_check_env->CopyFrom(args);
124 bounds_check->SetRawEnvironment(bounds_check_env);
125 HInstruction* array_set =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100126 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000127 block->AddInstruction(array_set);
128
129 graph_->BuildDominatorTree();
130 SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get());
131 ssa_analysis.Analyze();
132
133 EXPECT_FALSE(graph_->IsDebuggable());
134 EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
135 static const char* const expected[] = {
136 "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
137 "is_high: 0",
138 "ranges: { [4,21) }, uses: { 19 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
139 "is_high: 0",
140 "ranges: { [6,21) }, uses: { 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
141 "is_high: 0",
142 // Environment uses do not keep the non-reference argument alive.
143 "ranges: { [8,10) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
144 // Environment uses keep the reference argument alive.
145 "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
146 };
147 ASSERT_EQ(arraysize(expected), args.size());
148 size_t arg_index = 0u;
149 for (HInstruction* arg : args) {
150 std::ostringstream arg_dump;
151 arg->GetLiveInterval()->Dump(arg_dump);
152 EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
153 ++arg_index;
154 }
155}
156
157TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 HInstruction* array = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159 graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100160 HInstruction* index = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100161 graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100162 HInstruction* value = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100163 graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100164 HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100165 graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100166 HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100167 graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference);
Vladimir Marko356bd282017-03-01 12:01:11 +0000168 ArenaVector<HInstruction*> args({ array, index, value, extra_arg1, extra_arg2 },
Vladimir Markoca6fff82017-10-03 14:49:14 +0100169 GetAllocator()->Adapter());
Vladimir Marko356bd282017-03-01 12:01:11 +0000170 for (HInstruction* insn : args) {
171 entry_->AddInstruction(insn);
172 }
173
174 HBasicBlock* block = CreateSuccessor(entry_);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100175 HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000176 block->AddInstruction(null_check);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100177 HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(),
178 /* number_of_vregs */ 5,
179 /* method */ nullptr,
180 /* dex_pc */ 0u,
181 null_check);
Vladimir Marko356bd282017-03-01 12:01:11 +0000182 null_check_env->CopyFrom(args);
183 null_check->SetRawEnvironment(null_check_env);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100184 HInstruction* length = new (GetAllocator()) HArrayLength(array, 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000185 block->AddInstruction(length);
186 // Use HAboveOrEqual+HDeoptimize as the bounds check.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100187 HInstruction* ae = new (GetAllocator()) HAboveOrEqual(index, length);
Vladimir Marko356bd282017-03-01 12:01:11 +0000188 block->AddInstruction(ae);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100189 HInstruction* deoptimize = new(GetAllocator()) HDeoptimize(
190 GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc */ 0u);
Vladimir Marko356bd282017-03-01 12:01:11 +0000191 block->AddInstruction(deoptimize);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100192 HEnvironment* deoptimize_env = new (GetAllocator()) HEnvironment(GetAllocator(),
193 /* number_of_vregs */ 5,
194 /* method */ nullptr,
195 /* dex_pc */ 0u,
196 deoptimize);
Vladimir Marko356bd282017-03-01 12:01:11 +0000197 deoptimize_env->CopyFrom(args);
198 deoptimize->SetRawEnvironment(deoptimize_env);
199 HInstruction* array_set =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100200 new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0);
Vladimir Marko356bd282017-03-01 12:01:11 +0000201 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