Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 1 | /* |
| 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 Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "ssa_liveness_analysis.h" |
| 18 | |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 19 | #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 Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 23 | #include "code_generator.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "driver/compiler_options.h" |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 25 | #include "nodes.h" |
| 26 | #include "optimizing_unit_test.h" |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 30 | class SsaLivenessAnalysisTest : public OptimizingUnitTest { |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 31 | public: |
| 32 | SsaLivenessAnalysisTest() |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 33 | : graph_(CreateGraph()), |
Vladimir Marko | bd68e97 | 2017-03-14 18:07:35 +0000 | [diff] [blame] | 34 | compiler_options_(), |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 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_, |
Vladimir Marko | bd68e97 | 2017-03-14 18:07:35 +0000 | [diff] [blame] | 42 | compiler_options_); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 43 | CHECK(codegen_ != nullptr) << instruction_set_ << " is not a supported target architecture."; |
| 44 | // Create entry block. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 45 | entry_ = new (GetAllocator()) HBasicBlock(graph_); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 46 | graph_->AddBlock(entry_); |
| 47 | graph_->SetEntryBlock(entry_); |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | HBasicBlock* CreateSuccessor(HBasicBlock* block) { |
| 52 | HGraph* graph = block->GetGraph(); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 53 | HBasicBlock* successor = new (GetAllocator()) HBasicBlock(graph); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 54 | graph->AddBlock(successor); |
| 55 | block->AddSuccessor(successor); |
| 56 | return successor; |
| 57 | } |
| 58 | |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 59 | HGraph* graph_; |
Vladimir Marko | bd68e97 | 2017-03-14 18:07:35 +0000 | [diff] [blame] | 60 | CompilerOptions compiler_options_; |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 61 | InstructionSet instruction_set_; |
| 62 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features_; |
| 63 | std::unique_ptr<CodeGenerator> codegen_; |
| 64 | HBasicBlock* entry_; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(SsaLivenessAnalysisTest, TestReturnArg) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 68 | HInstruction* arg = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 69 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 70 | entry_->AddInstruction(arg); |
| 71 | |
| 72 | HBasicBlock* block = CreateSuccessor(entry_); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 73 | HInstruction* ret = new (GetAllocator()) HReturn(arg); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 74 | block->AddInstruction(ret); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 75 | block->AddInstruction(new (GetAllocator()) HExit()); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 76 | |
| 77 | graph_->BuildDominatorTree(); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 78 | SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator()); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 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 | |
| 87 | TEST_F(SsaLivenessAnalysisTest, TestAput) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 88 | HInstruction* array = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 89 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 90 | HInstruction* index = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 91 | graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 92 | HInstruction* value = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 93 | graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 94 | HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 95 | graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 96 | HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 97 | graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 98 | HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 }; |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 99 | for (HInstruction* insn : args) { |
| 100 | entry_->AddInstruction(insn); |
| 101 | } |
| 102 | |
| 103 | HBasicBlock* block = CreateSuccessor(entry_); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 104 | HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 105 | block->AddInstruction(null_check); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 106 | HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(), |
| 107 | /* number_of_vregs */ 5, |
| 108 | /* method */ nullptr, |
| 109 | /* dex_pc */ 0u, |
| 110 | null_check); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 111 | null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args)); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 112 | null_check->SetRawEnvironment(null_check_env); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 113 | HInstruction* length = new (GetAllocator()) HArrayLength(array, 0); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 114 | block->AddInstruction(length); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 115 | HInstruction* bounds_check = new (GetAllocator()) HBoundsCheck(index, length, /* dex_pc */ 0u); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 116 | block->AddInstruction(bounds_check); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 117 | HEnvironment* bounds_check_env = new (GetAllocator()) HEnvironment(GetAllocator(), |
| 118 | /* number_of_vregs */ 5, |
| 119 | /* method */ nullptr, |
| 120 | /* dex_pc */ 0u, |
| 121 | bounds_check); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 122 | bounds_check_env->CopyFrom(ArrayRef<HInstruction* const>(args)); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 123 | bounds_check->SetRawEnvironment(bounds_check_env); |
| 124 | HInstruction* array_set = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 125 | new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 126 | block->AddInstruction(array_set); |
| 127 | |
| 128 | graph_->BuildDominatorTree(); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 129 | SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator()); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 130 | 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 Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 146 | static_assert(arraysize(expected) == arraysize(args), "Array size check."); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 147 | 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 | |
| 156 | TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 157 | HInstruction* array = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 158 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 159 | HInstruction* index = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 160 | graph_->GetDexFile(), dex::TypeIndex(1), 1, DataType::Type::kInt32); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 161 | HInstruction* value = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 162 | graph_->GetDexFile(), dex::TypeIndex(2), 2, DataType::Type::kInt32); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 163 | HInstruction* extra_arg1 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 164 | graph_->GetDexFile(), dex::TypeIndex(3), 3, DataType::Type::kInt32); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 165 | HInstruction* extra_arg2 = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 166 | graph_->GetDexFile(), dex::TypeIndex(4), 4, DataType::Type::kReference); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 167 | HInstruction* const args[] = { array, index, value, extra_arg1, extra_arg2 }; |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 168 | for (HInstruction* insn : args) { |
| 169 | entry_->AddInstruction(insn); |
| 170 | } |
| 171 | |
| 172 | HBasicBlock* block = CreateSuccessor(entry_); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 173 | HInstruction* null_check = new (GetAllocator()) HNullCheck(array, 0); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 174 | block->AddInstruction(null_check); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 175 | HEnvironment* null_check_env = new (GetAllocator()) HEnvironment(GetAllocator(), |
| 176 | /* number_of_vregs */ 5, |
| 177 | /* method */ nullptr, |
| 178 | /* dex_pc */ 0u, |
| 179 | null_check); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 180 | null_check_env->CopyFrom(ArrayRef<HInstruction* const>(args)); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 181 | null_check->SetRawEnvironment(null_check_env); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 182 | HInstruction* length = new (GetAllocator()) HArrayLength(array, 0); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 183 | block->AddInstruction(length); |
| 184 | // Use HAboveOrEqual+HDeoptimize as the bounds check. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 185 | HInstruction* ae = new (GetAllocator()) HAboveOrEqual(index, length); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 186 | block->AddInstruction(ae); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 187 | HInstruction* deoptimize = new(GetAllocator()) HDeoptimize( |
| 188 | GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc */ 0u); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 189 | block->AddInstruction(deoptimize); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 190 | HEnvironment* deoptimize_env = new (GetAllocator()) HEnvironment(GetAllocator(), |
| 191 | /* number_of_vregs */ 5, |
| 192 | /* method */ nullptr, |
| 193 | /* dex_pc */ 0u, |
| 194 | deoptimize); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 195 | deoptimize_env->CopyFrom(ArrayRef<HInstruction* const>(args)); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 196 | deoptimize->SetRawEnvironment(deoptimize_env); |
| 197 | HInstruction* array_set = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 198 | new (GetAllocator()) HArraySet(array, index, value, DataType::Type::kInt32, /* dex_pc */ 0); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 199 | block->AddInstruction(array_set); |
| 200 | |
| 201 | graph_->BuildDominatorTree(); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 202 | SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator()); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 203 | 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 Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 218 | static_assert(arraysize(expected) == arraysize(args), "Array size check."); |
Vladimir Marko | 356bd28 | 2017-03-01 12:01:11 +0000 | [diff] [blame] | 219 | 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 |