Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "register_allocator.h" |
| 18 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 19 | #include "arch/x86/instruction_set_features_x86.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 20 | #include "base/arena_allocator.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 21 | #include "builder.h" |
| 22 | #include "code_generator.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 23 | #include "code_generator_x86.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 24 | #include "dex_file.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 25 | #include "dex_file_types.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 26 | #include "dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 27 | #include "driver/compiler_options.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 28 | #include "nodes.h" |
| 29 | #include "optimizing_unit_test.h" |
Matthew Gharrity | e928885 | 2016-07-14 14:08:16 -0700 | [diff] [blame] | 30 | #include "register_allocator_linear_scan.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 31 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 32 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 33 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 36 | using Strategy = RegisterAllocator::Strategy; |
| 37 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 38 | // Note: the register allocator tests rely on the fact that constants have live |
| 39 | // intervals and registers get allocated to them. |
| 40 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 41 | class RegisterAllocatorTest : public CommonCompilerTest { |
| 42 | protected: |
| 43 | // These functions need to access private variables of LocationSummary, so we declare it |
| 44 | // as a member of RegisterAllocatorTest, which we make a friend class. |
| 45 | static void SameAsFirstInputHint(Strategy strategy); |
| 46 | static void ExpectedInRegisterHint(Strategy strategy); |
| 47 | }; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 48 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 49 | // This macro should include all register allocation strategies that should be tested. |
| 50 | #define TEST_ALL_STRATEGIES(test_name)\ |
| 51 | TEST_F(RegisterAllocatorTest, test_name##_LinearScan) {\ |
| 52 | test_name(Strategy::kRegisterAllocatorLinearScan);\ |
| 53 | }\ |
| 54 | TEST_F(RegisterAllocatorTest, test_name##_GraphColor) {\ |
| 55 | test_name(Strategy::kRegisterAllocatorGraphColor);\ |
| 56 | } |
| 57 | |
| 58 | static bool Check(const uint16_t* data, Strategy strategy) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 59 | ArenaPool pool; |
| 60 | ArenaAllocator allocator(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 61 | HGraph* graph = CreateCFG(&allocator, data); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 62 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 63 | X86InstructionSetFeatures::FromCppDefines()); |
| 64 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 65 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 66 | liveness.Analyze(); |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 67 | RegisterAllocator* register_allocator = |
| 68 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 69 | register_allocator->AllocateRegisters(); |
| 70 | return register_allocator->Validate(false); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Unit testing of RegisterAllocator::ValidateIntervals. Register allocator |
| 75 | * tests are based on this validation method. |
| 76 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 77 | TEST_F(RegisterAllocatorTest, ValidateIntervals) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 78 | ArenaPool pool; |
| 79 | ArenaAllocator allocator(&pool); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 80 | HGraph* graph = CreateGraph(&allocator); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 81 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 82 | X86InstructionSetFeatures::FromCppDefines()); |
| 83 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 84 | ArenaVector<LiveInterval*> intervals(allocator.Adapter()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 85 | |
| 86 | // Test with two intervals of the same range. |
| 87 | { |
| 88 | static constexpr size_t ranges[][2] = {{0, 42}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 89 | intervals.push_back(BuildInterval(ranges, arraysize(ranges), &allocator, 0)); |
| 90 | intervals.push_back(BuildInterval(ranges, arraysize(ranges), &allocator, 1)); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 91 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 92 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 93 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 94 | intervals[1]->SetRegister(0); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 95 | ASSERT_FALSE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 96 | intervals, 0, 0, codegen, &allocator, true, false)); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 97 | intervals.clear(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Test with two non-intersecting intervals. |
| 101 | { |
| 102 | static constexpr size_t ranges1[][2] = {{0, 42}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 103 | intervals.push_back(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 104 | static constexpr size_t ranges2[][2] = {{42, 43}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 105 | intervals.push_back(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1)); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 106 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 107 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 108 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 109 | intervals[1]->SetRegister(0); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 110 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 111 | intervals, 0, 0, codegen, &allocator, true, false)); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 112 | intervals.clear(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Test with two non-intersecting intervals, with one with a lifetime hole. |
| 116 | { |
| 117 | static constexpr size_t ranges1[][2] = {{0, 42}, {45, 48}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 118 | intervals.push_back(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 119 | static constexpr size_t ranges2[][2] = {{42, 43}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 120 | intervals.push_back(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1)); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 121 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 122 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 123 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 124 | intervals[1]->SetRegister(0); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 125 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 126 | intervals, 0, 0, codegen, &allocator, true, false)); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 127 | intervals.clear(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Test with intersecting intervals. |
| 131 | { |
| 132 | static constexpr size_t ranges1[][2] = {{0, 42}, {44, 48}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 133 | intervals.push_back(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 134 | static constexpr size_t ranges2[][2] = {{42, 47}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 135 | intervals.push_back(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1)); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 136 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 137 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 138 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 139 | intervals[1]->SetRegister(0); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 140 | ASSERT_FALSE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 141 | intervals, 0, 0, codegen, &allocator, true, false)); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 142 | intervals.clear(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Test with siblings. |
| 146 | { |
| 147 | static constexpr size_t ranges1[][2] = {{0, 42}, {44, 48}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 148 | intervals.push_back(BuildInterval(ranges1, arraysize(ranges1), &allocator, 0)); |
| 149 | intervals[0]->SplitAt(43); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 150 | static constexpr size_t ranges2[][2] = {{42, 47}}; |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 151 | intervals.push_back(BuildInterval(ranges2, arraysize(ranges2), &allocator, 1)); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 152 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 153 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 154 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 155 | intervals[1]->SetRegister(0); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 156 | // Sibling of the first interval has no register allocated to it. |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 157 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 158 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 159 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 160 | intervals[0]->GetNextSibling()->SetRegister(0); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 161 | ASSERT_FALSE(RegisterAllocator::ValidateIntervals( |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 162 | intervals, 0, 0, codegen, &allocator, true, false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 166 | static void CFG1(Strategy strategy) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 167 | /* |
| 168 | * Test the following snippet: |
| 169 | * return 0; |
| 170 | * |
| 171 | * Which becomes the following graph: |
| 172 | * constant0 |
| 173 | * goto |
| 174 | * | |
| 175 | * return |
| 176 | * | |
| 177 | * exit |
| 178 | */ |
| 179 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 180 | Instruction::CONST_4 | 0 | 0, |
| 181 | Instruction::RETURN); |
| 182 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 183 | ASSERT_TRUE(Check(data, strategy)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 186 | TEST_ALL_STRATEGIES(CFG1); |
| 187 | |
| 188 | static void Loop1(Strategy strategy) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 189 | /* |
| 190 | * Test the following snippet: |
| 191 | * int a = 0; |
| 192 | * while (a == a) { |
| 193 | * a = 4; |
| 194 | * } |
| 195 | * return 5; |
| 196 | * |
| 197 | * Which becomes the following graph: |
| 198 | * constant0 |
| 199 | * constant4 |
| 200 | * constant5 |
| 201 | * goto |
| 202 | * | |
| 203 | * goto |
| 204 | * | |
| 205 | * phi |
| 206 | * equal |
| 207 | * if +++++ |
| 208 | * | \ + |
| 209 | * | goto |
| 210 | * | |
| 211 | * return |
| 212 | * | |
| 213 | * exit |
| 214 | */ |
| 215 | |
| 216 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 217 | Instruction::CONST_4 | 0 | 0, |
| 218 | Instruction::IF_EQ, 4, |
| 219 | Instruction::CONST_4 | 4 << 12 | 0, |
| 220 | Instruction::GOTO | 0xFD00, |
| 221 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 222 | Instruction::RETURN | 1 << 8); |
| 223 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 224 | ASSERT_TRUE(Check(data, strategy)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 227 | TEST_ALL_STRATEGIES(Loop1); |
| 228 | |
| 229 | static void Loop2(Strategy strategy) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 230 | /* |
| 231 | * Test the following snippet: |
| 232 | * int a = 0; |
| 233 | * while (a == 8) { |
| 234 | * a = 4 + 5; |
| 235 | * } |
| 236 | * return 6 + 7; |
| 237 | * |
| 238 | * Which becomes the following graph: |
| 239 | * constant0 |
| 240 | * constant4 |
| 241 | * constant5 |
| 242 | * constant6 |
| 243 | * constant7 |
| 244 | * constant8 |
| 245 | * goto |
| 246 | * | |
| 247 | * goto |
| 248 | * | |
| 249 | * phi |
| 250 | * equal |
| 251 | * if +++++ |
| 252 | * | \ + |
| 253 | * | 4 + 5 |
| 254 | * | goto |
| 255 | * | |
| 256 | * 6 + 7 |
| 257 | * return |
| 258 | * | |
| 259 | * exit |
| 260 | */ |
| 261 | |
| 262 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 263 | Instruction::CONST_4 | 0 | 0, |
| 264 | Instruction::CONST_4 | 8 << 12 | 1 << 8, |
| 265 | Instruction::IF_EQ | 1 << 8, 7, |
| 266 | Instruction::CONST_4 | 4 << 12 | 0 << 8, |
| 267 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 268 | Instruction::ADD_INT, 1 << 8 | 0, |
| 269 | Instruction::GOTO | 0xFA00, |
| 270 | Instruction::CONST_4 | 6 << 12 | 1 << 8, |
| 271 | Instruction::CONST_4 | 7 << 12 | 1 << 8, |
| 272 | Instruction::ADD_INT, 1 << 8 | 0, |
| 273 | Instruction::RETURN | 1 << 8); |
| 274 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 275 | ASSERT_TRUE(Check(data, strategy)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 278 | TEST_ALL_STRATEGIES(Loop2); |
| 279 | |
| 280 | static void Loop3(Strategy strategy) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 281 | /* |
| 282 | * Test the following snippet: |
| 283 | * int a = 0 |
| 284 | * do { |
| 285 | * b = a; |
| 286 | * a++; |
| 287 | * } while (a != 5) |
| 288 | * return b; |
| 289 | * |
| 290 | * Which becomes the following graph: |
| 291 | * constant0 |
| 292 | * constant1 |
| 293 | * constant5 |
| 294 | * goto |
| 295 | * | |
| 296 | * goto |
| 297 | * |++++++++++++ |
| 298 | * phi + |
| 299 | * a++ + |
| 300 | * equals + |
| 301 | * if + |
| 302 | * |++++++++++++ |
| 303 | * return |
| 304 | * | |
| 305 | * exit |
| 306 | */ |
| 307 | |
| 308 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 309 | Instruction::CONST_4 | 0 | 0, |
| 310 | Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8, |
| 311 | Instruction::CONST_4 | 5 << 12 | 2 << 8, |
| 312 | Instruction::IF_NE | 1 << 8 | 2 << 12, 3, |
| 313 | Instruction::RETURN | 0 << 8, |
| 314 | Instruction::MOVE | 1 << 12 | 0 << 8, |
| 315 | Instruction::GOTO | 0xF900); |
| 316 | |
| 317 | ArenaPool pool; |
| 318 | ArenaAllocator allocator(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 319 | HGraph* graph = CreateCFG(&allocator, data); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 320 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 321 | X86InstructionSetFeatures::FromCppDefines()); |
| 322 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 323 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 324 | liveness.Analyze(); |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 325 | RegisterAllocator* register_allocator = |
| 326 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 327 | register_allocator->AllocateRegisters(); |
| 328 | ASSERT_TRUE(register_allocator->Validate(false)); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 329 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 330 | HBasicBlock* loop_header = graph->GetBlocks()[2]; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 331 | HPhi* phi = loop_header->GetFirstPhi()->AsPhi(); |
| 332 | |
| 333 | LiveInterval* phi_interval = phi->GetLiveInterval(); |
| 334 | LiveInterval* loop_update = phi->InputAt(1)->GetLiveInterval(); |
| 335 | ASSERT_TRUE(phi_interval->HasRegister()); |
| 336 | ASSERT_TRUE(loop_update->HasRegister()); |
| 337 | ASSERT_NE(phi_interval->GetRegister(), loop_update->GetRegister()); |
| 338 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 339 | HBasicBlock* return_block = graph->GetBlocks()[3]; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 340 | HReturn* ret = return_block->GetLastInstruction()->AsReturn(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 341 | ASSERT_EQ(phi_interval->GetRegister(), ret->InputAt(0)->GetLiveInterval()->GetRegister()); |
| 342 | } |
| 343 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 344 | TEST_ALL_STRATEGIES(Loop3); |
| 345 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 346 | TEST_F(RegisterAllocatorTest, FirstRegisterUse) { |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 347 | const uint16_t data[] = THREE_REGISTERS_CODE_ITEM( |
| 348 | Instruction::CONST_4 | 0 | 0, |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 349 | Instruction::XOR_INT_LIT8 | 1 << 8, 1 << 8, |
| 350 | Instruction::XOR_INT_LIT8 | 0 << 8, 1 << 8, |
| 351 | Instruction::XOR_INT_LIT8 | 1 << 8, 1 << 8 | 1, |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 352 | Instruction::RETURN_VOID); |
| 353 | |
| 354 | ArenaPool pool; |
| 355 | ArenaAllocator allocator(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 356 | HGraph* graph = CreateCFG(&allocator, data); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 357 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 358 | X86InstructionSetFeatures::FromCppDefines()); |
| 359 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 360 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 361 | liveness.Analyze(); |
| 362 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 363 | HXor* first_xor = graph->GetBlocks()[1]->GetFirstInstruction()->AsXor(); |
| 364 | HXor* last_xor = graph->GetBlocks()[1]->GetLastInstruction()->GetPrevious()->AsXor(); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 365 | ASSERT_EQ(last_xor->InputAt(0), first_xor); |
| 366 | LiveInterval* interval = first_xor->GetLiveInterval(); |
| 367 | ASSERT_EQ(interval->GetEnd(), last_xor->GetLifetimePosition()); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 368 | ASSERT_TRUE(interval->GetNextSibling() == nullptr); |
| 369 | |
| 370 | // We need a register for the output of the instruction. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 371 | ASSERT_EQ(interval->FirstRegisterUse(), first_xor->GetLifetimePosition()); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 372 | |
| 373 | // Split at the next instruction. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 374 | interval = interval->SplitAt(first_xor->GetLifetimePosition() + 2); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 375 | // The user of the split is the last add. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 376 | ASSERT_EQ(interval->FirstRegisterUse(), last_xor->GetLifetimePosition()); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 377 | |
| 378 | // Split before the last add. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 379 | LiveInterval* new_interval = interval->SplitAt(last_xor->GetLifetimePosition() - 1); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 380 | // Ensure the current interval has no register use... |
| 381 | ASSERT_EQ(interval->FirstRegisterUse(), kNoLifetime); |
| 382 | // And the new interval has it for the last add. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 383 | ASSERT_EQ(new_interval->FirstRegisterUse(), last_xor->GetLifetimePosition()); |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 386 | static void DeadPhi(Strategy strategy) { |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 387 | /* Test for a dead loop phi taking as back-edge input a phi that also has |
| 388 | * this loop phi as input. Walking backwards in SsaDeadPhiElimination |
| 389 | * does not solve the problem because the loop phi will be visited last. |
| 390 | * |
| 391 | * Test the following snippet: |
| 392 | * int a = 0 |
| 393 | * do { |
| 394 | * if (true) { |
| 395 | * a = 2; |
| 396 | * } |
| 397 | * } while (true); |
| 398 | */ |
| 399 | |
| 400 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 401 | Instruction::CONST_4 | 0 | 0, |
| 402 | Instruction::CONST_4 | 1 << 8 | 0, |
| 403 | Instruction::IF_NE | 1 << 8 | 1 << 12, 3, |
| 404 | Instruction::CONST_4 | 2 << 12 | 0 << 8, |
| 405 | Instruction::GOTO | 0xFD00, |
| 406 | Instruction::RETURN_VOID); |
| 407 | |
| 408 | ArenaPool pool; |
| 409 | ArenaAllocator allocator(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 410 | HGraph* graph = CreateCFG(&allocator, data); |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 411 | SsaDeadPhiElimination(graph).Run(); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 412 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 413 | X86InstructionSetFeatures::FromCppDefines()); |
| 414 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 415 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 416 | liveness.Analyze(); |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 417 | RegisterAllocator* register_allocator = |
| 418 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 419 | register_allocator->AllocateRegisters(); |
| 420 | ASSERT_TRUE(register_allocator->Validate(false)); |
Nicolas Geoffray | 3ac17fc | 2014-08-06 23:02:54 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 423 | TEST_ALL_STRATEGIES(DeadPhi); |
| 424 | |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 425 | /** |
| 426 | * Test that the TryAllocateFreeReg method works in the presence of inactive intervals |
| 427 | * that share the same register. It should split the interval it is currently |
| 428 | * allocating for at the minimum lifetime position between the two inactive intervals. |
Matthew Gharrity | d9ffd0d | 2016-06-22 10:27:55 -0700 | [diff] [blame] | 429 | * This test only applies to the linear scan allocator. |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 430 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 431 | TEST_F(RegisterAllocatorTest, FreeUntil) { |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 432 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 433 | Instruction::CONST_4 | 0 | 0, |
| 434 | Instruction::RETURN); |
| 435 | |
| 436 | ArenaPool pool; |
| 437 | ArenaAllocator allocator(&pool); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 438 | HGraph* graph = CreateCFG(&allocator, data); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 439 | SsaDeadPhiElimination(graph).Run(); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 440 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 441 | X86InstructionSetFeatures::FromCppDefines()); |
| 442 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 443 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 444 | liveness.Analyze(); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 445 | RegisterAllocatorLinearScan register_allocator(&allocator, &codegen, liveness); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 446 | |
| 447 | // Add an artifical range to cover the temps that will be put in the unhandled list. |
| 448 | LiveInterval* unhandled = graph->GetEntryBlock()->GetFirstInstruction()->GetLiveInterval(); |
| 449 | unhandled->AddLoopRange(0, 60); |
Nicolas Geoffray | 23a8188 | 2015-06-01 18:12:38 +0100 | [diff] [blame] | 450 | |
Nicolas Geoffray | 30971d6 | 2015-06-01 18:37:24 +0100 | [diff] [blame] | 451 | // Populate the instructions in the liveness object, to please the register allocator. |
Nicolas Geoffray | 23a8188 | 2015-06-01 18:12:38 +0100 | [diff] [blame] | 452 | for (size_t i = 0; i < 60; ++i) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 453 | liveness.instructions_from_lifetime_position_.push_back( |
Nicolas Geoffray | 23a8188 | 2015-06-01 18:12:38 +0100 | [diff] [blame] | 454 | graph->GetEntryBlock()->GetFirstInstruction()); |
| 455 | } |
| 456 | |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 457 | // For SSA value intervals, only an interval resulted from a split may intersect |
| 458 | // with inactive intervals. |
| 459 | unhandled = register_allocator.Split(unhandled, 5); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 460 | |
| 461 | // Add three temps holding the same register, and starting at different positions. |
| 462 | // Put the one that should be picked in the middle of the inactive list to ensure |
| 463 | // we do not depend on an order. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 464 | LiveInterval* interval = LiveInterval::MakeFixedInterval(&allocator, 0, DataType::Type::kInt32); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 465 | interval->AddRange(40, 50); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 466 | register_allocator.inactive_.push_back(interval); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 467 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 468 | interval = LiveInterval::MakeFixedInterval(&allocator, 0, DataType::Type::kInt32); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 469 | interval->AddRange(20, 30); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 470 | register_allocator.inactive_.push_back(interval); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 471 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 472 | interval = LiveInterval::MakeFixedInterval(&allocator, 0, DataType::Type::kInt32); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 473 | interval->AddRange(60, 70); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 474 | register_allocator.inactive_.push_back(interval); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 475 | |
| 476 | register_allocator.number_of_registers_ = 1; |
| 477 | register_allocator.registers_array_ = allocator.AllocArray<size_t>(1); |
| 478 | register_allocator.processing_core_registers_ = true; |
| 479 | register_allocator.unhandled_ = ®ister_allocator.unhandled_core_intervals_; |
| 480 | |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 481 | ASSERT_TRUE(register_allocator.TryAllocateFreeReg(unhandled)); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 482 | |
| 483 | // Check that we have split the interval. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 484 | ASSERT_EQ(1u, register_allocator.unhandled_->size()); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 485 | // Check that we know need to find a new register where the next interval |
| 486 | // that uses the register starts. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 487 | ASSERT_EQ(20u, register_allocator.unhandled_->front()->GetStart()); |
Nicolas Geoffray | aac0f39 | 2014-09-16 14:11:14 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 490 | static HGraph* BuildIfElseWithPhi(ArenaAllocator* allocator, |
| 491 | HPhi** phi, |
| 492 | HInstruction** input1, |
| 493 | HInstruction** input2) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 494 | HGraph* graph = CreateGraph(allocator); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 495 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 496 | graph->AddBlock(entry); |
| 497 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 498 | HInstruction* parameter = new (allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 499 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 500 | entry->AddInstruction(parameter); |
| 501 | |
| 502 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 503 | graph->AddBlock(block); |
| 504 | entry->AddSuccessor(block); |
| 505 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 506 | HInstruction* test = new (allocator) HInstanceFieldGet(parameter, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 507 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 508 | DataType::Type::kBool, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 509 | MemberOffset(22), |
| 510 | false, |
| 511 | kUnknownFieldIndex, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 512 | kUnknownClassDefIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 513 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 514 | 0); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 515 | block->AddInstruction(test); |
| 516 | block->AddInstruction(new (allocator) HIf(test)); |
| 517 | HBasicBlock* then = new (allocator) HBasicBlock(graph); |
| 518 | HBasicBlock* else_ = new (allocator) HBasicBlock(graph); |
| 519 | HBasicBlock* join = new (allocator) HBasicBlock(graph); |
| 520 | graph->AddBlock(then); |
| 521 | graph->AddBlock(else_); |
| 522 | graph->AddBlock(join); |
| 523 | |
| 524 | block->AddSuccessor(then); |
| 525 | block->AddSuccessor(else_); |
| 526 | then->AddSuccessor(join); |
| 527 | else_->AddSuccessor(join); |
| 528 | then->AddInstruction(new (allocator) HGoto()); |
| 529 | else_->AddInstruction(new (allocator) HGoto()); |
| 530 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 531 | *phi = new (allocator) HPhi(allocator, 0, 0, DataType::Type::kInt32); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 532 | join->AddPhi(*phi); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 533 | *input1 = new (allocator) HInstanceFieldGet(parameter, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 534 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 535 | DataType::Type::kInt32, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 536 | MemberOffset(42), |
| 537 | false, |
| 538 | kUnknownFieldIndex, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 539 | kUnknownClassDefIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 540 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 541 | 0); |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 542 | *input2 = new (allocator) HInstanceFieldGet(parameter, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 543 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 544 | DataType::Type::kInt32, |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 545 | MemberOffset(42), |
| 546 | false, |
| 547 | kUnknownFieldIndex, |
| 548 | kUnknownClassDefIndex, |
| 549 | graph->GetDexFile(), |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 550 | 0); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 551 | then->AddInstruction(*input1); |
| 552 | else_->AddInstruction(*input2); |
| 553 | join->AddInstruction(new (allocator) HExit()); |
| 554 | (*phi)->AddInput(*input1); |
| 555 | (*phi)->AddInput(*input2); |
| 556 | |
| 557 | graph->BuildDominatorTree(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 558 | graph->AnalyzeLoops(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 559 | return graph; |
| 560 | } |
| 561 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 562 | static void PhiHint(Strategy strategy) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 563 | ArenaPool pool; |
| 564 | ArenaAllocator allocator(&pool); |
| 565 | HPhi *phi; |
| 566 | HInstruction *input1, *input2; |
| 567 | |
| 568 | { |
| 569 | HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 570 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 571 | X86InstructionSetFeatures::FromCppDefines()); |
| 572 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 573 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 574 | liveness.Analyze(); |
| 575 | |
| 576 | // Check that the register allocator is deterministic. |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 577 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 578 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 579 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 580 | |
| 581 | ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 0); |
| 582 | ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 0); |
| 583 | ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 0); |
| 584 | } |
| 585 | |
| 586 | { |
| 587 | HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 588 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 589 | X86InstructionSetFeatures::FromCppDefines()); |
| 590 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 591 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 592 | liveness.Analyze(); |
| 593 | |
| 594 | // Set the phi to a specific register, and check that the inputs get allocated |
| 595 | // the same register. |
Nicolas Geoffray | 18c219b | 2015-02-04 09:38:49 +0000 | [diff] [blame] | 596 | phi->GetLocations()->UpdateOut(Location::RegisterLocation(2)); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 597 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 598 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 599 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 600 | |
| 601 | ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2); |
| 602 | ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2); |
| 603 | ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2); |
| 604 | } |
| 605 | |
| 606 | { |
| 607 | HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 608 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 609 | X86InstructionSetFeatures::FromCppDefines()); |
| 610 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 611 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 612 | liveness.Analyze(); |
| 613 | |
| 614 | // Set input1 to a specific register, and check that the phi and other input get allocated |
| 615 | // the same register. |
Nicolas Geoffray | 18c219b | 2015-02-04 09:38:49 +0000 | [diff] [blame] | 616 | input1->GetLocations()->UpdateOut(Location::RegisterLocation(2)); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 617 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 618 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 619 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 620 | |
| 621 | ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2); |
| 622 | ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2); |
| 623 | ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2); |
| 624 | } |
| 625 | |
| 626 | { |
| 627 | HGraph* graph = BuildIfElseWithPhi(&allocator, &phi, &input1, &input2); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 628 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 629 | X86InstructionSetFeatures::FromCppDefines()); |
| 630 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 631 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 632 | liveness.Analyze(); |
| 633 | |
| 634 | // Set input2 to a specific register, and check that the phi and other input get allocated |
| 635 | // the same register. |
Nicolas Geoffray | 18c219b | 2015-02-04 09:38:49 +0000 | [diff] [blame] | 636 | input2->GetLocations()->UpdateOut(Location::RegisterLocation(2)); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 637 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 638 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 639 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 640 | |
| 641 | ASSERT_EQ(input1->GetLiveInterval()->GetRegister(), 2); |
| 642 | ASSERT_EQ(input2->GetLiveInterval()->GetRegister(), 2); |
| 643 | ASSERT_EQ(phi->GetLiveInterval()->GetRegister(), 2); |
| 644 | } |
| 645 | } |
| 646 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 647 | // TODO: Enable this test for graph coloring register allocation when iterative move |
| 648 | // coalescing is merged. |
| 649 | TEST_F(RegisterAllocatorTest, PhiHint_LinearScan) { |
| 650 | PhiHint(Strategy::kRegisterAllocatorLinearScan); |
| 651 | } |
| 652 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 653 | static HGraph* BuildFieldReturn(ArenaAllocator* allocator, |
| 654 | HInstruction** field, |
| 655 | HInstruction** ret) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 656 | HGraph* graph = CreateGraph(allocator); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 657 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 658 | graph->AddBlock(entry); |
| 659 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 660 | HInstruction* parameter = new (allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 661 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kReference); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 662 | entry->AddInstruction(parameter); |
| 663 | |
| 664 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 665 | graph->AddBlock(block); |
| 666 | entry->AddSuccessor(block); |
| 667 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 668 | *field = new (allocator) HInstanceFieldGet(parameter, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 669 | nullptr, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 670 | DataType::Type::kInt32, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 671 | MemberOffset(42), |
| 672 | false, |
| 673 | kUnknownFieldIndex, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 674 | kUnknownClassDefIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 675 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 676 | 0); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 677 | block->AddInstruction(*field); |
| 678 | *ret = new (allocator) HReturn(*field); |
| 679 | block->AddInstruction(*ret); |
| 680 | |
| 681 | HBasicBlock* exit = new (allocator) HBasicBlock(graph); |
| 682 | graph->AddBlock(exit); |
| 683 | block->AddSuccessor(exit); |
| 684 | exit->AddInstruction(new (allocator) HExit()); |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 685 | |
| 686 | graph->BuildDominatorTree(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 687 | return graph; |
| 688 | } |
| 689 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 690 | void RegisterAllocatorTest::ExpectedInRegisterHint(Strategy strategy) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 691 | ArenaPool pool; |
| 692 | ArenaAllocator allocator(&pool); |
| 693 | HInstruction *field, *ret; |
| 694 | |
| 695 | { |
| 696 | HGraph* graph = BuildFieldReturn(&allocator, &field, &ret); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 697 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 698 | X86InstructionSetFeatures::FromCppDefines()); |
| 699 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 700 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 701 | liveness.Analyze(); |
| 702 | |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 703 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 704 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 705 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 706 | |
| 707 | // Sanity check that in normal conditions, the register should be hinted to 0 (EAX). |
| 708 | ASSERT_EQ(field->GetLiveInterval()->GetRegister(), 0); |
| 709 | } |
| 710 | |
| 711 | { |
| 712 | HGraph* graph = BuildFieldReturn(&allocator, &field, &ret); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 713 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 714 | X86InstructionSetFeatures::FromCppDefines()); |
| 715 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 716 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 717 | liveness.Analyze(); |
| 718 | |
| 719 | // Check that the field gets put in the register expected by its use. |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 720 | // Don't use SetInAt because we are overriding an already allocated location. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 721 | ret->GetLocations()->inputs_[0] = Location::RegisterLocation(2); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 722 | |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 723 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 724 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 725 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 726 | |
| 727 | ASSERT_EQ(field->GetLiveInterval()->GetRegister(), 2); |
| 728 | } |
| 729 | } |
| 730 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 731 | // TODO: Enable this test for graph coloring register allocation when iterative move |
| 732 | // coalescing is merged. |
| 733 | TEST_F(RegisterAllocatorTest, ExpectedInRegisterHint_LinearScan) { |
| 734 | ExpectedInRegisterHint(Strategy::kRegisterAllocatorLinearScan); |
| 735 | } |
| 736 | |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 737 | static HGraph* BuildTwoSubs(ArenaAllocator* allocator, |
| 738 | HInstruction** first_sub, |
| 739 | HInstruction** second_sub) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 740 | HGraph* graph = CreateGraph(allocator); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 741 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 742 | graph->AddBlock(entry); |
| 743 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 744 | HInstruction* parameter = new (allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 745 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 746 | entry->AddInstruction(parameter); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 747 | |
| 748 | HInstruction* constant1 = graph->GetIntConstant(1); |
| 749 | HInstruction* constant2 = graph->GetIntConstant(2); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 750 | |
| 751 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 752 | graph->AddBlock(block); |
| 753 | entry->AddSuccessor(block); |
| 754 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 755 | *first_sub = new (allocator) HSub(DataType::Type::kInt32, parameter, constant1); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 756 | block->AddInstruction(*first_sub); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 757 | *second_sub = new (allocator) HSub(DataType::Type::kInt32, *first_sub, constant2); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 758 | block->AddInstruction(*second_sub); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 759 | |
| 760 | block->AddInstruction(new (allocator) HExit()); |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 761 | |
| 762 | graph->BuildDominatorTree(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 763 | return graph; |
| 764 | } |
| 765 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 766 | void RegisterAllocatorTest::SameAsFirstInputHint(Strategy strategy) { |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 767 | ArenaPool pool; |
| 768 | ArenaAllocator allocator(&pool); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 769 | HInstruction *first_sub, *second_sub; |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 770 | |
| 771 | { |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 772 | HGraph* graph = BuildTwoSubs(&allocator, &first_sub, &second_sub); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 773 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 774 | X86InstructionSetFeatures::FromCppDefines()); |
| 775 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 776 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 777 | liveness.Analyze(); |
| 778 | |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 779 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 780 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 781 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 782 | |
| 783 | // Sanity check that in normal conditions, the registers are the same. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 784 | ASSERT_EQ(first_sub->GetLiveInterval()->GetRegister(), 1); |
| 785 | ASSERT_EQ(second_sub->GetLiveInterval()->GetRegister(), 1); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | { |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 789 | HGraph* graph = BuildTwoSubs(&allocator, &first_sub, &second_sub); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 790 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 791 | X86InstructionSetFeatures::FromCppDefines()); |
| 792 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 793 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 794 | liveness.Analyze(); |
| 795 | |
| 796 | // check that both adds get the same register. |
Nicolas Geoffray | 18c219b | 2015-02-04 09:38:49 +0000 | [diff] [blame] | 797 | // Don't use UpdateOutput because output is already allocated. |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 798 | first_sub->InputAt(0)->GetLocations()->output_ = Location::RegisterLocation(2); |
| 799 | ASSERT_EQ(first_sub->GetLocations()->Out().GetPolicy(), Location::kSameAsFirstInput); |
| 800 | ASSERT_EQ(second_sub->GetLocations()->Out().GetPolicy(), Location::kSameAsFirstInput); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 801 | |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 802 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 803 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 804 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 805 | |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 806 | ASSERT_EQ(first_sub->GetLiveInterval()->GetRegister(), 2); |
| 807 | ASSERT_EQ(second_sub->GetLiveInterval()->GetRegister(), 2); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 811 | // TODO: Enable this test for graph coloring register allocation when iterative move |
| 812 | // coalescing is merged. |
| 813 | TEST_F(RegisterAllocatorTest, SameAsFirstInputHint_LinearScan) { |
| 814 | SameAsFirstInputHint(Strategy::kRegisterAllocatorLinearScan); |
| 815 | } |
| 816 | |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 817 | static HGraph* BuildDiv(ArenaAllocator* allocator, |
| 818 | HInstruction** div) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 819 | HGraph* graph = CreateGraph(allocator); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 820 | HBasicBlock* entry = new (allocator) HBasicBlock(graph); |
| 821 | graph->AddBlock(entry); |
| 822 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 823 | HInstruction* first = new (allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 824 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 825 | HInstruction* second = new (allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 826 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 827 | entry->AddInstruction(first); |
| 828 | entry->AddInstruction(second); |
| 829 | |
| 830 | HBasicBlock* block = new (allocator) HBasicBlock(graph); |
| 831 | graph->AddBlock(block); |
| 832 | entry->AddSuccessor(block); |
| 833 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 834 | *div = |
| 835 | new (allocator) HDiv(DataType::Type::kInt32, first, second, 0); // don't care about dex_pc. |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 836 | block->AddInstruction(*div); |
| 837 | |
| 838 | block->AddInstruction(new (allocator) HExit()); |
David Brazdil | 10f56cb | 2015-03-24 18:49:14 +0000 | [diff] [blame] | 839 | |
| 840 | graph->BuildDominatorTree(); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 841 | return graph; |
| 842 | } |
| 843 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 844 | static void ExpectedExactInRegisterAndSameOutputHint(Strategy strategy) { |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 845 | ArenaPool pool; |
| 846 | ArenaAllocator allocator(&pool); |
| 847 | HInstruction *div; |
| 848 | |
| 849 | { |
| 850 | HGraph* graph = BuildDiv(&allocator, &div); |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 851 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 852 | X86InstructionSetFeatures::FromCppDefines()); |
| 853 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 854 | SsaLivenessAnalysis liveness(graph, &codegen); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 855 | liveness.Analyze(); |
| 856 | |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 857 | RegisterAllocator* register_allocator = |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 858 | RegisterAllocator::Create(&allocator, &codegen, liveness, strategy); |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 859 | register_allocator->AllocateRegisters(); |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 860 | |
| 861 | // div on x86 requires its first input in eax and the output be the same as the first input. |
| 862 | ASSERT_EQ(div->GetLiveInterval()->GetRegister(), 0); |
| 863 | } |
| 864 | } |
| 865 | |
Matthew Gharrity | 2ac06bc | 2016-08-05 09:34:52 -0700 | [diff] [blame] | 866 | // TODO: Enable this test for graph coloring register allocation when iterative move |
| 867 | // coalescing is merged. |
| 868 | TEST_F(RegisterAllocatorTest, ExpectedExactInRegisterAndSameOutputHint_LinearScan) { |
| 869 | ExpectedExactInRegisterAndSameOutputHint(Strategy::kRegisterAllocatorLinearScan); |
| 870 | } |
| 871 | |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 872 | // Test a bug in the register allocator, where allocating a blocked |
| 873 | // register would lead to spilling an inactive interval at the wrong |
| 874 | // position. |
Matthew Gharrity | d9ffd0d | 2016-06-22 10:27:55 -0700 | [diff] [blame] | 875 | // This test only applies to the linear scan allocator. |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 876 | TEST_F(RegisterAllocatorTest, SpillInactive) { |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 877 | ArenaPool pool; |
| 878 | |
| 879 | // Create a synthesized graph to please the register_allocator and |
| 880 | // ssa_liveness_analysis code. |
| 881 | ArenaAllocator allocator(&pool); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 882 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 883 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 884 | graph->AddBlock(entry); |
| 885 | graph->SetEntryBlock(entry); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 886 | HInstruction* one = new (&allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 887 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 888 | HInstruction* two = new (&allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 889 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 890 | HInstruction* three = new (&allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 891 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 892 | HInstruction* four = new (&allocator) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 893 | graph->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 894 | entry->AddInstruction(one); |
| 895 | entry->AddInstruction(two); |
| 896 | entry->AddInstruction(three); |
| 897 | entry->AddInstruction(four); |
| 898 | |
| 899 | HBasicBlock* block = new (&allocator) HBasicBlock(graph); |
| 900 | graph->AddBlock(block); |
| 901 | entry->AddSuccessor(block); |
| 902 | block->AddInstruction(new (&allocator) HExit()); |
| 903 | |
| 904 | // We create a synthesized user requesting a register, to avoid just spilling the |
| 905 | // intervals. |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 906 | HPhi* user = new (&allocator) HPhi(&allocator, 0, 1, DataType::Type::kInt32); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 907 | user->AddInput(one); |
| 908 | user->SetBlock(block); |
| 909 | LocationSummary* locations = new (&allocator) LocationSummary(user, LocationSummary::kNoCall); |
| 910 | locations->SetInAt(0, Location::RequiresRegister()); |
| 911 | static constexpr size_t phi_ranges[][2] = {{20, 30}}; |
| 912 | BuildInterval(phi_ranges, arraysize(phi_ranges), &allocator, -1, user); |
| 913 | |
| 914 | // Create an interval with lifetime holes. |
| 915 | static constexpr size_t ranges1[][2] = {{0, 2}, {4, 6}, {8, 10}}; |
| 916 | LiveInterval* first = BuildInterval(ranges1, arraysize(ranges1), &allocator, -1, one); |
Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 917 | first->uses_.push_front(*new(&allocator) UsePosition(user, false, 8)); |
| 918 | first->uses_.push_front(*new(&allocator) UsePosition(user, false, 7)); |
| 919 | first->uses_.push_front(*new(&allocator) UsePosition(user, false, 6)); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 920 | |
| 921 | locations = new (&allocator) LocationSummary(first->GetDefinedBy(), LocationSummary::kNoCall); |
| 922 | locations->SetOut(Location::RequiresRegister()); |
| 923 | first = first->SplitAt(1); |
| 924 | |
| 925 | // Create an interval that conflicts with the next interval, to force the next |
| 926 | // interval to call `AllocateBlockedReg`. |
| 927 | static constexpr size_t ranges2[][2] = {{2, 4}}; |
| 928 | LiveInterval* second = BuildInterval(ranges2, arraysize(ranges2), &allocator, -1, two); |
| 929 | locations = new (&allocator) LocationSummary(second->GetDefinedBy(), LocationSummary::kNoCall); |
| 930 | locations->SetOut(Location::RequiresRegister()); |
| 931 | |
| 932 | // Create an interval that will lead to splitting the first interval. The bug occured |
| 933 | // by splitting at a wrong position, in this case at the next intersection between |
| 934 | // this interval and the first interval. We would have then put the interval with ranges |
| 935 | // "[0, 2(, [4, 6(" in the list of handled intervals, even though we haven't processed intervals |
| 936 | // before lifetime position 6 yet. |
| 937 | static constexpr size_t ranges3[][2] = {{2, 4}, {8, 10}}; |
| 938 | LiveInterval* third = BuildInterval(ranges3, arraysize(ranges3), &allocator, -1, three); |
Vladimir Marko | 82b0740 | 2017-03-01 19:02:04 +0000 | [diff] [blame] | 939 | third->uses_.push_front(*new(&allocator) UsePosition(user, false, 8)); |
| 940 | third->uses_.push_front(*new(&allocator) UsePosition(user, false, 4)); |
| 941 | third->uses_.push_front(*new(&allocator) UsePosition(user, false, 3)); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 942 | locations = new (&allocator) LocationSummary(third->GetDefinedBy(), LocationSummary::kNoCall); |
| 943 | locations->SetOut(Location::RequiresRegister()); |
| 944 | third = third->SplitAt(3); |
| 945 | |
| 946 | // Because the first part of the split interval was considered handled, this interval |
| 947 | // was free to allocate the same register, even though it conflicts with it. |
| 948 | static constexpr size_t ranges4[][2] = {{4, 6}}; |
| 949 | LiveInterval* fourth = BuildInterval(ranges4, arraysize(ranges4), &allocator, -1, four); |
| 950 | locations = new (&allocator) LocationSummary(fourth->GetDefinedBy(), LocationSummary::kNoCall); |
| 951 | locations->SetOut(Location::RequiresRegister()); |
| 952 | |
Mark Mendell | fb8d279 | 2015-03-31 22:16:59 -0400 | [diff] [blame] | 953 | std::unique_ptr<const X86InstructionSetFeatures> features_x86( |
| 954 | X86InstructionSetFeatures::FromCppDefines()); |
| 955 | x86::CodeGeneratorX86 codegen(graph, *features_x86.get(), CompilerOptions()); |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 956 | SsaLivenessAnalysis liveness(graph, &codegen); |
Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 957 | // Populate the instructions in the liveness object, to please the register allocator. |
| 958 | for (size_t i = 0; i < 32; ++i) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 959 | liveness.instructions_from_lifetime_position_.push_back(user); |
Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 960 | } |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 961 | |
Matthew Gharrity | 8f49d4b | 2016-07-14 13:24:00 -0700 | [diff] [blame] | 962 | RegisterAllocatorLinearScan register_allocator(&allocator, &codegen, liveness); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 963 | register_allocator.unhandled_core_intervals_.push_back(fourth); |
| 964 | register_allocator.unhandled_core_intervals_.push_back(third); |
| 965 | register_allocator.unhandled_core_intervals_.push_back(second); |
| 966 | register_allocator.unhandled_core_intervals_.push_back(first); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 967 | |
| 968 | // Set just one register available to make all intervals compete for the same. |
| 969 | register_allocator.number_of_registers_ = 1; |
| 970 | register_allocator.registers_array_ = allocator.AllocArray<size_t>(1); |
| 971 | register_allocator.processing_core_registers_ = true; |
| 972 | register_allocator.unhandled_ = ®ister_allocator.unhandled_core_intervals_; |
| 973 | register_allocator.LinearScan(); |
| 974 | |
| 975 | // Test that there is no conflicts between intervals. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 976 | ArenaVector<LiveInterval*> intervals(allocator.Adapter()); |
| 977 | intervals.push_back(first); |
| 978 | intervals.push_back(second); |
| 979 | intervals.push_back(third); |
| 980 | intervals.push_back(fourth); |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 981 | ASSERT_TRUE(RegisterAllocator::ValidateIntervals( |
| 982 | intervals, 0, 0, codegen, &allocator, true, false)); |
| 983 | } |
| 984 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 985 | } // namespace art |