Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #include "base/arena_allocator.h" |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 18 | #include "base/macros.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 19 | #include "builder.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 20 | #include "code_generator.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 21 | #include "dex/dex_file.h" |
| 22 | #include "dex/dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 23 | #include "driver/compiler_options.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 24 | #include "nodes.h" |
| 25 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 26 | #include "prepare_for_register_allocation.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 27 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 28 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 29 | namespace art HIDDEN { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 30 | |
Santiago Aboy Solanes | 2c50b3a | 2023-02-10 10:25:31 +0000 | [diff] [blame] | 31 | class LiveRangesTest : public CommonCompilerTest, public OptimizingUnitTestHelper { |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 32 | protected: |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 33 | HGraph* BuildGraph(const std::vector<uint16_t>& data); |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 34 | |
| 35 | std::unique_ptr<CompilerOptions> compiler_options_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 36 | }; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 37 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 38 | HGraph* LiveRangesTest::BuildGraph(const std::vector<uint16_t>& data) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 39 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 40 | compiler_options_ = CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default"); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 41 | // Suspend checks implementation may change in the future, and this test relies |
| 42 | // on how instructions are ordered. |
| 43 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 44 | // `Inline` conditions into ifs. |
Nicolas Geoffray | 61ba8d2 | 2018-08-07 09:55:57 +0100 | [diff] [blame] | 45 | PrepareForRegisterAllocation(graph, *compiler_options_).Run(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 46 | return graph; |
| 47 | } |
| 48 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 49 | TEST_F(LiveRangesTest, CFG1) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 50 | /* |
| 51 | * Test the following snippet: |
| 52 | * return 0; |
| 53 | * |
| 54 | * Which becomes the following graph (numbered by lifetime position): |
| 55 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 56 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 57 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 58 | * 8: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 59 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 60 | * 12: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 61 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 62 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 63 | Instruction::CONST_4 | 0 | 0, |
| 64 | Instruction::RETURN); |
| 65 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 66 | HGraph* graph = BuildGraph(data); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 67 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 68 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 69 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 70 | liveness.Analyze(); |
| 71 | |
| 72 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 73 | LiveRange* range = interval->GetFirstRange(); |
| 74 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 75 | // Last use is the return instruction. |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 76 | ASSERT_EQ(8u, range->GetEnd()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 77 | HBasicBlock* block = graph->GetBlocks()[1]; |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 78 | ASSERT_TRUE(block->GetLastInstruction()->IsReturn()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 79 | ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition()); |
| 80 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 81 | } |
| 82 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 83 | TEST_F(LiveRangesTest, CFG2) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 84 | /* |
| 85 | * Test the following snippet: |
| 86 | * var a = 0; |
| 87 | * if (0 == 0) { |
| 88 | * } else { |
| 89 | * } |
| 90 | * return a; |
| 91 | * |
| 92 | * Which becomes the following graph (numbered by lifetime position): |
| 93 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 94 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 95 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 96 | * 8: equal |
| 97 | * 10: if |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 98 | * / \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 99 | * 14: goto 18: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 100 | * \ / |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 101 | * 22: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 102 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 103 | * 26: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 104 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 105 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 106 | Instruction::CONST_4 | 0 | 0, |
| 107 | Instruction::IF_EQ, 3, |
| 108 | Instruction::GOTO | 0x100, |
| 109 | Instruction::RETURN | 0 << 8); |
| 110 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 111 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 112 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 113 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 114 | liveness.Analyze(); |
| 115 | |
| 116 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 117 | LiveRange* range = interval->GetFirstRange(); |
| 118 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 119 | // Last use is the return instruction. |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 120 | ASSERT_EQ(22u, range->GetEnd()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 121 | HBasicBlock* block = graph->GetBlocks()[3]; |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 122 | ASSERT_TRUE(block->GetLastInstruction()->IsReturn()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 123 | ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition()); |
| 124 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 125 | } |
| 126 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 127 | TEST_F(LiveRangesTest, CFG3) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 128 | /* |
| 129 | * Test the following snippet: |
| 130 | * var a = 0; |
| 131 | * if (0 == 0) { |
| 132 | * } else { |
| 133 | * a = 4; |
| 134 | * } |
| 135 | * return a; |
| 136 | * |
| 137 | * Which becomes the following graph (numbered by lifetime position): |
| 138 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 139 | * 4: constant4 |
| 140 | * 6: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 141 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 142 | * 10: equal |
| 143 | * 12: if |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 144 | * / \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 145 | * 16: goto 20: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 146 | * \ / |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 147 | * 22: phi |
| 148 | * 24: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 149 | * | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 150 | * 28: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 151 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 152 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 153 | Instruction::CONST_4 | 0 | 0, |
| 154 | Instruction::IF_EQ, 3, |
| 155 | Instruction::CONST_4 | 4 << 12 | 0, |
| 156 | Instruction::RETURN | 0 << 8); |
| 157 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 158 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 159 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 160 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 161 | liveness.Analyze(); |
| 162 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 163 | // Test for the 4 constant. |
| 164 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 165 | LiveRange* range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 166 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 167 | // Last use is the phi at the return block so instruction is live until |
| 168 | // the end of the then block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 169 | ASSERT_EQ(18u, range->GetEnd()); |
| 170 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 171 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 172 | // Test for the 0 constant. |
| 173 | interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 174 | // The then branch is a hole for this constant, therefore its interval has 2 ranges. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 175 | // First range starts from the definition and ends at the if block. |
| 176 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 177 | ASSERT_EQ(2u, range->GetStart()); |
| 178 | // 14 is the end of the if block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 179 | ASSERT_EQ(14u, range->GetEnd()); |
| 180 | // Second range is the else block. |
| 181 | range = range->GetNext(); |
| 182 | ASSERT_EQ(18u, range->GetStart()); |
| 183 | // Last use is the phi at the return block. |
| 184 | ASSERT_EQ(22u, range->GetEnd()); |
| 185 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 186 | |
| 187 | // Test for the phi. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 188 | interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 189 | range = interval->GetFirstRange(); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 190 | ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 191 | ASSERT_EQ(22u, range->GetStart()); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 192 | ASSERT_EQ(24u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 193 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 194 | } |
| 195 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 196 | TEST_F(LiveRangesTest, Loop1) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 197 | /* |
| 198 | * Test the following snippet: |
| 199 | * var a = 0; |
| 200 | * while (a == a) { |
| 201 | * a = 4; |
| 202 | * } |
| 203 | * return 5; |
| 204 | * |
| 205 | * Which becomes the following graph (numbered by lifetime position): |
| 206 | * 2: constant0 |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 207 | * 4: constant5 |
| 208 | * 6: constant4 |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 209 | * 8: goto |
| 210 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 211 | * 12: goto |
| 212 | * | |
| 213 | * 14: phi |
| 214 | * 16: equal |
| 215 | * 18: if +++++ |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 216 | * | \ + |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 217 | * | 22: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 218 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 219 | * 26: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 220 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 221 | * 30: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 222 | */ |
| 223 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 224 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 225 | Instruction::CONST_4 | 0 | 0, |
| 226 | Instruction::IF_EQ, 4, |
| 227 | Instruction::CONST_4 | 4 << 12 | 0, |
| 228 | Instruction::GOTO | 0xFD00, |
| 229 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 230 | Instruction::RETURN | 1 << 8); |
| 231 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 232 | HGraph* graph = BuildGraph(data); |
Nicolas Geoffray | 9ebc72c | 2014-09-25 16:33:42 +0100 | [diff] [blame] | 233 | RemoveSuspendChecks(graph); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 234 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 235 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 236 | liveness.Analyze(); |
| 237 | |
| 238 | // Test for the 0 constant. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 239 | LiveInterval* interval = graph->GetIntConstant(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 240 | LiveRange* range = interval->GetFirstRange(); |
| 241 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 242 | // Last use is the loop phi so instruction is live until |
| 243 | // the end of the pre loop header. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 244 | ASSERT_EQ(14u, range->GetEnd()); |
| 245 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 246 | |
| 247 | // Test for the 4 constant. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 248 | interval = graph->GetIntConstant(4)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 249 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 250 | // The instruction is live until the end of the loop. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 251 | ASSERT_EQ(6u, range->GetStart()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 252 | ASSERT_EQ(24u, range->GetEnd()); |
| 253 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 254 | |
| 255 | // Test for the 5 constant. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 256 | interval = graph->GetIntConstant(5)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 257 | range = interval->GetFirstRange(); |
| 258 | // The instruction is live until the return instruction after the loop. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 259 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 260 | ASSERT_EQ(26u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 261 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 262 | |
| 263 | // Test for the phi. |
| 264 | interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 265 | range = interval->GetFirstRange(); |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 266 | // Instruction is input of non-materialized Equal and hence live until If. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 267 | ASSERT_EQ(14u, range->GetStart()); |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 268 | ASSERT_EQ(19u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 269 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 270 | } |
| 271 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 272 | TEST_F(LiveRangesTest, Loop2) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 273 | /* |
| 274 | * Test the following snippet: |
| 275 | * var a = 0; |
| 276 | * while (a == a) { |
| 277 | * a = a + a; |
| 278 | * } |
| 279 | * return a; |
| 280 | * |
| 281 | * Which becomes the following graph (numbered by lifetime position): |
| 282 | * 2: constant0 |
| 283 | * 4: goto |
| 284 | * | |
| 285 | * 8: goto |
| 286 | * | |
| 287 | * 10: phi |
| 288 | * 12: equal |
| 289 | * 14: if +++++ |
| 290 | * | \ + |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 291 | * | 18: add |
| 292 | * | 20: goto |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 293 | * | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 294 | * 24: return |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 295 | * | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 296 | * 28: exit |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 297 | * |
| 298 | * We want to make sure the phi at 10 has a lifetime hole after the add at 20. |
| 299 | */ |
| 300 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 301 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 302 | Instruction::CONST_4 | 0 | 0, |
| 303 | Instruction::IF_EQ, 6, |
| 304 | Instruction::ADD_INT, 0, 0, |
| 305 | Instruction::GOTO | 0xFB00, |
| 306 | Instruction::RETURN | 0 << 8); |
| 307 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 308 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 309 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 310 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 311 | liveness.Analyze(); |
| 312 | |
| 313 | // Test for the 0 constant. |
| 314 | HIntConstant* constant = liveness.GetInstructionFromSsaIndex(0)->AsIntConstant(); |
| 315 | LiveInterval* interval = constant->GetLiveInterval(); |
| 316 | LiveRange* range = interval->GetFirstRange(); |
| 317 | ASSERT_EQ(2u, range->GetStart()); |
| 318 | // Last use is the loop phi so instruction is live until |
| 319 | // the end of the pre loop header. |
| 320 | ASSERT_EQ(10u, range->GetEnd()); |
| 321 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 322 | |
| 323 | // Test for the loop phi. |
| 324 | HPhi* phi = liveness.GetInstructionFromSsaIndex(1)->AsPhi(); |
| 325 | interval = phi->GetLiveInterval(); |
| 326 | range = interval->GetFirstRange(); |
| 327 | ASSERT_EQ(10u, range->GetStart()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 328 | ASSERT_EQ(19u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 329 | range = range->GetNext(); |
| 330 | ASSERT_TRUE(range != nullptr); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 331 | ASSERT_EQ(22u, range->GetStart()); |
| 332 | ASSERT_EQ(24u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 333 | |
| 334 | // Test for the add instruction. |
| 335 | HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd(); |
| 336 | interval = add->GetLiveInterval(); |
| 337 | range = interval->GetFirstRange(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 338 | ASSERT_EQ(18u, range->GetStart()); |
| 339 | ASSERT_EQ(22u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 340 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 341 | } |
| 342 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 343 | TEST_F(LiveRangesTest, CFG4) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 344 | /* |
| 345 | * Test the following snippet: |
| 346 | * var a = 0; |
| 347 | * var b = 4; |
| 348 | * if (a == a) { |
| 349 | * a = b + a; |
| 350 | * } else { |
| 351 | * a = b + a |
| 352 | * } |
| 353 | * return b; |
| 354 | * |
| 355 | * Which becomes the following graph (numbered by lifetime position): |
| 356 | * 2: constant0 |
| 357 | * 4: constant4 |
| 358 | * 6: goto |
| 359 | * | |
| 360 | * 10: equal |
| 361 | * 12: if |
| 362 | * / \ |
| 363 | * 16: add 22: add |
| 364 | * 18: goto 24: goto |
| 365 | * \ / |
| 366 | * 26: phi |
| 367 | * 28: return |
| 368 | * | |
| 369 | * 32: exit |
| 370 | * |
| 371 | * We want to make sure the constant0 has a lifetime hole after the 16: add. |
| 372 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 373 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 374 | Instruction::CONST_4 | 0 | 0, |
| 375 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 376 | Instruction::IF_EQ, 5, |
| 377 | Instruction::ADD_INT, 1 << 8, |
| 378 | Instruction::GOTO | 0x300, |
| 379 | Instruction::ADD_INT, 1 << 8, |
Nicolas Geoffray | a3c00e5 | 2014-11-25 11:18:37 +0000 | [diff] [blame] | 380 | Instruction::RETURN); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 381 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 382 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 383 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 384 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 385 | liveness.Analyze(); |
| 386 | |
| 387 | // Test for the 0 constant. |
| 388 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
| 389 | LiveRange* range = interval->GetFirstRange(); |
| 390 | ASSERT_EQ(2u, range->GetStart()); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 391 | ASSERT_EQ(17u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 392 | range = range->GetNext(); |
| 393 | ASSERT_TRUE(range != nullptr); |
| 394 | ASSERT_EQ(20u, range->GetStart()); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 395 | ASSERT_EQ(23u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 396 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 397 | |
| 398 | // Test for the 4 constant. |
| 399 | interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
| 400 | range = interval->GetFirstRange(); |
| 401 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | a3c00e5 | 2014-11-25 11:18:37 +0000 | [diff] [blame] | 402 | ASSERT_EQ(17u, range->GetEnd()); |
| 403 | range = range->GetNext(); |
| 404 | ASSERT_EQ(20u, range->GetStart()); |
| 405 | ASSERT_EQ(23u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 406 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 407 | |
| 408 | // Test for the first add. |
| 409 | HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd(); |
| 410 | interval = add->GetLiveInterval(); |
| 411 | range = interval->GetFirstRange(); |
| 412 | ASSERT_EQ(16u, range->GetStart()); |
| 413 | ASSERT_EQ(20u, range->GetEnd()); |
| 414 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 415 | |
| 416 | // Test for the second add. |
| 417 | add = liveness.GetInstructionFromSsaIndex(3)->AsAdd(); |
| 418 | interval = add->GetLiveInterval(); |
| 419 | range = interval->GetFirstRange(); |
| 420 | ASSERT_EQ(22u, range->GetStart()); |
| 421 | ASSERT_EQ(26u, range->GetEnd()); |
| 422 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 423 | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 424 | HPhi* phi = liveness.GetInstructionFromSsaIndex(4)->AsPhi(); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 425 | ASSERT_TRUE(phi->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 426 | interval = phi->GetLiveInterval(); |
| 427 | range = interval->GetFirstRange(); |
| 428 | ASSERT_EQ(26u, range->GetStart()); |
| 429 | ASSERT_EQ(28u, range->GetEnd()); |
| 430 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 431 | } |
| 432 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 433 | } // namespace art |