blob: bb8a4dc08ebcc545c0f2ff3be93a99a8b3731de4 [file] [log] [blame]
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001/*
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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010018#include "builder.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010019#include "code_generator.h"
David Sehr9e734c72018-01-04 17:56:19 -080020#include "dex/dex_file.h"
21#include "dex/dex_instruction.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000022#include "driver/compiler_options.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010023#include "nodes.h"
24#include "optimizing_unit_test.h"
Nicolas Geoffray360231a2014-10-08 21:07:48 +010025#include "prepare_for_register_allocation.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010026#include "ssa_liveness_analysis.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010027
Vladimir Marko0a516052019-10-14 13:00:44 +000028namespace art {
David Brazdild9510df2015-11-04 23:30:22 +000029
Vladimir Markoca6fff82017-10-03 14:49:14 +010030class LiveRangesTest : public OptimizingUnitTest {
Vladimir Markof91fc122020-05-13 09:21:00 +010031 protected:
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080032 HGraph* BuildGraph(const std::vector<uint16_t>& data);
Vladimir Markof91fc122020-05-13 09:21:00 +010033
34 std::unique_ptr<CompilerOptions> compiler_options_;
Vladimir Markoca6fff82017-10-03 14:49:14 +010035};
David Brazdil4833f5a2015-12-16 10:37:39 +000036
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080037HGraph* LiveRangesTest::BuildGraph(const std::vector<uint16_t>& data) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010038 HGraph* graph = CreateCFG(data);
Vladimir Markof91fc122020-05-13 09:21:00 +010039 compiler_options_ = CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default");
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000040 // Suspend checks implementation may change in the future, and this test relies
41 // on how instructions are ordered.
42 RemoveSuspendChecks(graph);
Nicolas Geoffray360231a2014-10-08 21:07:48 +010043 // `Inline` conditions into ifs.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +010044 PrepareForRegisterAllocation(graph, *compiler_options_).Run();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010045 return graph;
46}
47
David Brazdil4833f5a2015-12-16 10:37:39 +000048TEST_F(LiveRangesTest, CFG1) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049 /*
50 * Test the following snippet:
51 * return 0;
52 *
53 * Which becomes the following graph (numbered by lifetime position):
54 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010055 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010057 * 8: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 * 12: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080061 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010062 Instruction::CONST_4 | 0 | 0,
63 Instruction::RETURN);
64
Vladimir Markoca6fff82017-10-03 14:49:14 +010065 HGraph* graph = BuildGraph(data);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010066
Vladimir Markoa0431112018-06-25 09:32:54 +010067 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
68 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069 liveness.Analyze();
70
71 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010072 LiveRange* range = interval->GetFirstRange();
73 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010074 // Last use is the return instruction.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +010075 ASSERT_EQ(8u, range->GetEnd());
Vladimir Markoec7802a2015-10-01 20:57:57 +010076 HBasicBlock* block = graph->GetBlocks()[1];
Roland Levillain476df552014-10-09 17:51:36 +010077 ASSERT_TRUE(block->GetLastInstruction()->IsReturn());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010078 ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition());
79 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010080}
81
David Brazdil4833f5a2015-12-16 10:37:39 +000082TEST_F(LiveRangesTest, CFG2) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010083 /*
84 * Test the following snippet:
85 * var a = 0;
86 * if (0 == 0) {
87 * } else {
88 * }
89 * return a;
90 *
91 * Which becomes the following graph (numbered by lifetime position):
92 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010093 * 4: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010094 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010095 * 8: equal
96 * 10: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010097 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010098 * 14: goto 18: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010099 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100 * 22: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100101 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100102 * 26: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100103 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800104 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100105 Instruction::CONST_4 | 0 | 0,
106 Instruction::IF_EQ, 3,
107 Instruction::GOTO | 0x100,
108 Instruction::RETURN | 0 << 8);
109
Vladimir Markoca6fff82017-10-03 14:49:14 +0100110 HGraph* graph = BuildGraph(data);
Vladimir Markoa0431112018-06-25 09:32:54 +0100111 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
112 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100113 liveness.Analyze();
114
115 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100116 LiveRange* range = interval->GetFirstRange();
117 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100118 // Last use is the return instruction.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100119 ASSERT_EQ(22u, range->GetEnd());
Vladimir Markoec7802a2015-10-01 20:57:57 +0100120 HBasicBlock* block = graph->GetBlocks()[3];
Roland Levillain476df552014-10-09 17:51:36 +0100121 ASSERT_TRUE(block->GetLastInstruction()->IsReturn());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100122 ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition());
123 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100124}
125
David Brazdil4833f5a2015-12-16 10:37:39 +0000126TEST_F(LiveRangesTest, CFG3) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100127 /*
128 * Test the following snippet:
129 * var a = 0;
130 * if (0 == 0) {
131 * } else {
132 * a = 4;
133 * }
134 * return a;
135 *
136 * Which becomes the following graph (numbered by lifetime position):
137 * 2: constant0
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 * 4: constant4
139 * 6: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100140 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141 * 10: equal
142 * 12: if
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100143 * / \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100144 * 16: goto 20: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100145 * \ /
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 * 22: phi
147 * 24: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148 * |
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100149 * 28: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100150 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800151 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100152 Instruction::CONST_4 | 0 | 0,
153 Instruction::IF_EQ, 3,
154 Instruction::CONST_4 | 4 << 12 | 0,
155 Instruction::RETURN | 0 << 8);
156
Vladimir Markoca6fff82017-10-03 14:49:14 +0100157 HGraph* graph = BuildGraph(data);
Vladimir Markoa0431112018-06-25 09:32:54 +0100158 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
159 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100160 liveness.Analyze();
161
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100162 // Test for the 4 constant.
163 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100164 LiveRange* range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100165 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100166 // Last use is the phi at the return block so instruction is live until
167 // the end of the then block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100168 ASSERT_EQ(18u, range->GetEnd());
169 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100170
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100171 // Test for the 0 constant.
172 interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100173 // The then branch is a hole for this constant, therefore its interval has 2 ranges.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100174 // First range starts from the definition and ends at the if block.
175 range = interval->GetFirstRange();
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100176 ASSERT_EQ(2u, range->GetStart());
177 // 14 is the end of the if block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100178 ASSERT_EQ(14u, range->GetEnd());
179 // Second range is the else block.
180 range = range->GetNext();
181 ASSERT_EQ(18u, range->GetStart());
182 // Last use is the phi at the return block.
183 ASSERT_EQ(22u, range->GetEnd());
184 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185
186 // Test for the phi.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100188 range = interval->GetFirstRange();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100189 ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100190 ASSERT_EQ(22u, range->GetStart());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100191 ASSERT_EQ(24u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100192 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100193}
194
David Brazdil4833f5a2015-12-16 10:37:39 +0000195TEST_F(LiveRangesTest, Loop1) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100196 /*
197 * Test the following snippet:
198 * var a = 0;
199 * while (a == a) {
200 * a = 4;
201 * }
202 * return 5;
203 *
204 * Which becomes the following graph (numbered by lifetime position):
205 * 2: constant0
David Brazdildee58d62016-04-07 09:54:26 +0000206 * 4: constant5
207 * 6: constant4
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100208 * 8: goto
209 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100210 * 12: goto
211 * |
212 * 14: phi
213 * 16: equal
214 * 18: if +++++
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100215 * | \ +
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100216 * | 22: goto
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100217 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100218 * 26: return
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 * |
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100220 * 30: exit
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100221 */
222
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800223 const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM(
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100224 Instruction::CONST_4 | 0 | 0,
225 Instruction::IF_EQ, 4,
226 Instruction::CONST_4 | 4 << 12 | 0,
227 Instruction::GOTO | 0xFD00,
228 Instruction::CONST_4 | 5 << 12 | 1 << 8,
229 Instruction::RETURN | 1 << 8);
230
Vladimir Markoca6fff82017-10-03 14:49:14 +0100231 HGraph* graph = BuildGraph(data);
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +0100232 RemoveSuspendChecks(graph);
Vladimir Markoa0431112018-06-25 09:32:54 +0100233 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
234 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100235 liveness.Analyze();
236
237 // Test for the 0 constant.
David Brazdildee58d62016-04-07 09:54:26 +0000238 LiveInterval* interval = graph->GetIntConstant(0)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100239 LiveRange* range = interval->GetFirstRange();
240 ASSERT_EQ(2u, range->GetStart());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100241 // Last use is the loop phi so instruction is live until
242 // the end of the pre loop header.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100243 ASSERT_EQ(14u, range->GetEnd());
244 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100245
246 // Test for the 4 constant.
David Brazdildee58d62016-04-07 09:54:26 +0000247 interval = graph->GetIntConstant(4)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100248 range = interval->GetFirstRange();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100249 // The instruction is live until the end of the loop.
David Brazdildee58d62016-04-07 09:54:26 +0000250 ASSERT_EQ(6u, range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100251 ASSERT_EQ(24u, range->GetEnd());
252 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100253
254 // Test for the 5 constant.
David Brazdildee58d62016-04-07 09:54:26 +0000255 interval = graph->GetIntConstant(5)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100256 range = interval->GetFirstRange();
257 // The instruction is live until the return instruction after the loop.
David Brazdildee58d62016-04-07 09:54:26 +0000258 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100259 ASSERT_EQ(26u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100260 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100261
262 // Test for the phi.
263 interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100264 range = interval->GetFirstRange();
David Brazdilb3e773e2016-01-26 11:28:37 +0000265 // Instruction is input of non-materialized Equal and hence live until If.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100266 ASSERT_EQ(14u, range->GetStart());
David Brazdilb3e773e2016-01-26 11:28:37 +0000267 ASSERT_EQ(19u, range->GetEnd());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100268 ASSERT_TRUE(range->GetNext() == nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100269}
270
David Brazdil4833f5a2015-12-16 10:37:39 +0000271TEST_F(LiveRangesTest, Loop2) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100272 /*
273 * Test the following snippet:
274 * var a = 0;
275 * while (a == a) {
276 * a = a + a;
277 * }
278 * return a;
279 *
280 * Which becomes the following graph (numbered by lifetime position):
281 * 2: constant0
282 * 4: goto
283 * |
284 * 8: goto
285 * |
286 * 10: phi
287 * 12: equal
288 * 14: if +++++
289 * | \ +
David Brazdilbadd8262016-02-02 16:28:56 +0000290 * | 18: add
291 * | 20: goto
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100292 * |
David Brazdilbadd8262016-02-02 16:28:56 +0000293 * 24: return
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100294 * |
David Brazdilbadd8262016-02-02 16:28:56 +0000295 * 28: exit
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100296 *
297 * We want to make sure the phi at 10 has a lifetime hole after the add at 20.
298 */
299
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800300 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100301 Instruction::CONST_4 | 0 | 0,
302 Instruction::IF_EQ, 6,
303 Instruction::ADD_INT, 0, 0,
304 Instruction::GOTO | 0xFB00,
305 Instruction::RETURN | 0 << 8);
306
Vladimir Markoca6fff82017-10-03 14:49:14 +0100307 HGraph* graph = BuildGraph(data);
Vladimir Markoa0431112018-06-25 09:32:54 +0100308 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
309 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100310 liveness.Analyze();
311
312 // Test for the 0 constant.
313 HIntConstant* constant = liveness.GetInstructionFromSsaIndex(0)->AsIntConstant();
314 LiveInterval* interval = constant->GetLiveInterval();
315 LiveRange* range = interval->GetFirstRange();
316 ASSERT_EQ(2u, range->GetStart());
317 // Last use is the loop phi so instruction is live until
318 // the end of the pre loop header.
319 ASSERT_EQ(10u, range->GetEnd());
320 ASSERT_TRUE(range->GetNext() == nullptr);
321
322 // Test for the loop phi.
323 HPhi* phi = liveness.GetInstructionFromSsaIndex(1)->AsPhi();
324 interval = phi->GetLiveInterval();
325 range = interval->GetFirstRange();
326 ASSERT_EQ(10u, range->GetStart());
David Brazdilbadd8262016-02-02 16:28:56 +0000327 ASSERT_EQ(19u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100328 range = range->GetNext();
329 ASSERT_TRUE(range != nullptr);
David Brazdilbadd8262016-02-02 16:28:56 +0000330 ASSERT_EQ(22u, range->GetStart());
331 ASSERT_EQ(24u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100332
333 // Test for the add instruction.
334 HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd();
335 interval = add->GetLiveInterval();
336 range = interval->GetFirstRange();
David Brazdilbadd8262016-02-02 16:28:56 +0000337 ASSERT_EQ(18u, range->GetStart());
338 ASSERT_EQ(22u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100339 ASSERT_TRUE(range->GetNext() == nullptr);
340}
341
David Brazdil4833f5a2015-12-16 10:37:39 +0000342TEST_F(LiveRangesTest, CFG4) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100343 /*
344 * Test the following snippet:
345 * var a = 0;
346 * var b = 4;
347 * if (a == a) {
348 * a = b + a;
349 * } else {
350 * a = b + a
351 * }
352 * return b;
353 *
354 * Which becomes the following graph (numbered by lifetime position):
355 * 2: constant0
356 * 4: constant4
357 * 6: goto
358 * |
359 * 10: equal
360 * 12: if
361 * / \
362 * 16: add 22: add
363 * 18: goto 24: goto
364 * \ /
365 * 26: phi
366 * 28: return
367 * |
368 * 32: exit
369 *
370 * We want to make sure the constant0 has a lifetime hole after the 16: add.
371 */
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800372 const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM(
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100373 Instruction::CONST_4 | 0 | 0,
374 Instruction::CONST_4 | 4 << 12 | 1 << 8,
375 Instruction::IF_EQ, 5,
376 Instruction::ADD_INT, 1 << 8,
377 Instruction::GOTO | 0x300,
378 Instruction::ADD_INT, 1 << 8,
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000379 Instruction::RETURN);
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100380
Vladimir Markoca6fff82017-10-03 14:49:14 +0100381 HGraph* graph = BuildGraph(data);
Vladimir Markoa0431112018-06-25 09:32:54 +0100382 std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_);
383 SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100384 liveness.Analyze();
385
386 // Test for the 0 constant.
387 LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval();
388 LiveRange* range = interval->GetFirstRange();
389 ASSERT_EQ(2u, range->GetStart());
Mark Mendell09b84632015-02-13 17:48:38 -0500390 ASSERT_EQ(17u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100391 range = range->GetNext();
392 ASSERT_TRUE(range != nullptr);
393 ASSERT_EQ(20u, range->GetStart());
Mark Mendell09b84632015-02-13 17:48:38 -0500394 ASSERT_EQ(23u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100395 ASSERT_TRUE(range->GetNext() == nullptr);
396
397 // Test for the 4 constant.
398 interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval();
399 range = interval->GetFirstRange();
400 ASSERT_EQ(4u, range->GetStart());
Nicolas Geoffraya3c00e52014-11-25 11:18:37 +0000401 ASSERT_EQ(17u, range->GetEnd());
402 range = range->GetNext();
403 ASSERT_EQ(20u, range->GetStart());
404 ASSERT_EQ(23u, range->GetEnd());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100405 ASSERT_TRUE(range->GetNext() == nullptr);
406
407 // Test for the first add.
408 HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd();
409 interval = add->GetLiveInterval();
410 range = interval->GetFirstRange();
411 ASSERT_EQ(16u, range->GetStart());
412 ASSERT_EQ(20u, range->GetEnd());
413 ASSERT_TRUE(range->GetNext() == nullptr);
414
415 // Test for the second add.
416 add = liveness.GetInstructionFromSsaIndex(3)->AsAdd();
417 interval = add->GetLiveInterval();
418 range = interval->GetFirstRange();
419 ASSERT_EQ(22u, range->GetStart());
420 ASSERT_EQ(26u, range->GetEnd());
421 ASSERT_TRUE(range->GetNext() == nullptr);
422
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100423 HPhi* phi = liveness.GetInstructionFromSsaIndex(4)->AsPhi();
Vladimir Marko46817b82016-03-29 12:21:58 +0100424 ASSERT_TRUE(phi->GetUses().HasExactlyOneElement());
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100425 interval = phi->GetLiveInterval();
426 range = interval->GetFirstRange();
427 ASSERT_EQ(26u, range->GetStart());
428 ASSERT_EQ(28u, range->GetEnd());
429 ASSERT_TRUE(range->GetNext() == nullptr);
430}
431
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100432} // namespace art