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" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 18 | #include "builder.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 19 | #include "code_generator.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 20 | #include "dex/dex_file.h" |
| 21 | #include "dex/dex_instruction.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 22 | #include "driver/compiler_options.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 23 | #include "nodes.h" |
| 24 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 25 | #include "prepare_for_register_allocation.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 26 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 27 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 28 | namespace art { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 29 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 30 | class LiveRangesTest : public OptimizingUnitTest { |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 31 | protected: |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 32 | HGraph* BuildGraph(const std::vector<uint16_t>& data); |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 33 | |
| 34 | std::unique_ptr<CompilerOptions> compiler_options_; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 35 | }; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 36 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 37 | HGraph* LiveRangesTest::BuildGraph(const std::vector<uint16_t>& data) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 38 | HGraph* graph = CreateCFG(data); |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 39 | compiler_options_ = CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default"); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 40 | // Suspend checks implementation may change in the future, and this test relies |
| 41 | // on how instructions are ordered. |
| 42 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | 360231a | 2014-10-08 21:07:48 +0100 | [diff] [blame] | 43 | // `Inline` conditions into ifs. |
Nicolas Geoffray | 61ba8d2 | 2018-08-07 09:55:57 +0100 | [diff] [blame] | 44 | PrepareForRegisterAllocation(graph, *compiler_options_).Run(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 45 | return graph; |
| 46 | } |
| 47 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 48 | TEST_F(LiveRangesTest, CFG1) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 49 | /* |
| 50 | * Test the following snippet: |
| 51 | * return 0; |
| 52 | * |
| 53 | * Which becomes the following graph (numbered by lifetime position): |
| 54 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 55 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 56 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 57 | * 8: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 58 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 59 | * 12: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 60 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 61 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 62 | Instruction::CONST_4 | 0 | 0, |
| 63 | Instruction::RETURN); |
| 64 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 65 | HGraph* graph = BuildGraph(data); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 66 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 67 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 68 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 69 | liveness.Analyze(); |
| 70 | |
| 71 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 72 | LiveRange* range = interval->GetFirstRange(); |
| 73 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 74 | // Last use is the return instruction. |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 75 | ASSERT_EQ(8u, range->GetEnd()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 76 | HBasicBlock* block = graph->GetBlocks()[1]; |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 77 | ASSERT_TRUE(block->GetLastInstruction()->IsReturn()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 78 | ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition()); |
| 79 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 80 | } |
| 81 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 82 | TEST_F(LiveRangesTest, CFG2) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 83 | /* |
| 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 Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 93 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 94 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 95 | * 8: equal |
| 96 | * 10: if |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 97 | * / \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 98 | * 14: goto 18: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 99 | * \ / |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 100 | * 22: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 101 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 102 | * 26: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 103 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 104 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 105 | Instruction::CONST_4 | 0 | 0, |
| 106 | Instruction::IF_EQ, 3, |
| 107 | Instruction::GOTO | 0x100, |
| 108 | Instruction::RETURN | 0 << 8); |
| 109 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 110 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 111 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 112 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 113 | liveness.Analyze(); |
| 114 | |
| 115 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 116 | LiveRange* range = interval->GetFirstRange(); |
| 117 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 118 | // Last use is the return instruction. |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 119 | ASSERT_EQ(22u, range->GetEnd()); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 120 | HBasicBlock* block = graph->GetBlocks()[3]; |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 121 | ASSERT_TRUE(block->GetLastInstruction()->IsReturn()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 122 | ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition()); |
| 123 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 124 | } |
| 125 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 126 | TEST_F(LiveRangesTest, CFG3) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 127 | /* |
| 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 Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 138 | * 4: constant4 |
| 139 | * 6: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 140 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 141 | * 10: equal |
| 142 | * 12: if |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 143 | * / \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 144 | * 16: goto 20: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 145 | * \ / |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 146 | * 22: phi |
| 147 | * 24: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 148 | * | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 149 | * 28: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 150 | */ |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 151 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 152 | Instruction::CONST_4 | 0 | 0, |
| 153 | Instruction::IF_EQ, 3, |
| 154 | Instruction::CONST_4 | 4 << 12 | 0, |
| 155 | Instruction::RETURN | 0 << 8); |
| 156 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 157 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 158 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 159 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 160 | liveness.Analyze(); |
| 161 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 162 | // Test for the 4 constant. |
| 163 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 164 | LiveRange* range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 165 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 166 | // Last use is the phi at the return block so instruction is live until |
| 167 | // the end of the then block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 168 | ASSERT_EQ(18u, range->GetEnd()); |
| 169 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 170 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 171 | // Test for the 0 constant. |
| 172 | interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 173 | // 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] | 174 | // First range starts from the definition and ends at the if block. |
| 175 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 176 | ASSERT_EQ(2u, range->GetStart()); |
| 177 | // 14 is the end of the if block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 178 | 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 Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 185 | |
| 186 | // Test for the phi. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 187 | interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 188 | range = interval->GetFirstRange(); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 189 | ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 190 | ASSERT_EQ(22u, range->GetStart()); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 191 | ASSERT_EQ(24u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 192 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 193 | } |
| 194 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 195 | TEST_F(LiveRangesTest, Loop1) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 196 | /* |
| 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 Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 206 | * 4: constant5 |
| 207 | * 6: constant4 |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 208 | * 8: goto |
| 209 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 210 | * 12: goto |
| 211 | * | |
| 212 | * 14: phi |
| 213 | * 16: equal |
| 214 | * 18: if +++++ |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 215 | * | \ + |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 216 | * | 22: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 217 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 218 | * 26: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 219 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 220 | * 30: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 221 | */ |
| 222 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 223 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 224 | 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 Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 231 | HGraph* graph = BuildGraph(data); |
Nicolas Geoffray | 9ebc72c | 2014-09-25 16:33:42 +0100 | [diff] [blame] | 232 | RemoveSuspendChecks(graph); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 233 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 234 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 235 | liveness.Analyze(); |
| 236 | |
| 237 | // Test for the 0 constant. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 238 | LiveInterval* interval = graph->GetIntConstant(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 239 | LiveRange* range = interval->GetFirstRange(); |
| 240 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 241 | // Last use is the loop phi so instruction is live until |
| 242 | // the end of the pre loop header. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 243 | ASSERT_EQ(14u, range->GetEnd()); |
| 244 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 245 | |
| 246 | // Test for the 4 constant. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 247 | interval = graph->GetIntConstant(4)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 248 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 249 | // The instruction is live until the end of the loop. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 250 | ASSERT_EQ(6u, range->GetStart()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 251 | ASSERT_EQ(24u, range->GetEnd()); |
| 252 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 253 | |
| 254 | // Test for the 5 constant. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 255 | interval = graph->GetIntConstant(5)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 256 | range = interval->GetFirstRange(); |
| 257 | // The instruction is live until the return instruction after the loop. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 258 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 259 | ASSERT_EQ(26u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 260 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 261 | |
| 262 | // Test for the phi. |
| 263 | interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 264 | range = interval->GetFirstRange(); |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 265 | // Instruction is input of non-materialized Equal and hence live until If. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 266 | ASSERT_EQ(14u, range->GetStart()); |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 267 | ASSERT_EQ(19u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 268 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 269 | } |
| 270 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 271 | TEST_F(LiveRangesTest, Loop2) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 272 | /* |
| 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 Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 290 | * | 18: add |
| 291 | * | 20: goto |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 292 | * | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 293 | * 24: return |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 294 | * | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 295 | * 28: exit |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 296 | * |
| 297 | * We want to make sure the phi at 10 has a lifetime hole after the add at 20. |
| 298 | */ |
| 299 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 300 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 301 | 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 Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 307 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 308 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 309 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 310 | 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 Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 327 | ASSERT_EQ(19u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 328 | range = range->GetNext(); |
| 329 | ASSERT_TRUE(range != nullptr); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 330 | ASSERT_EQ(22u, range->GetStart()); |
| 331 | ASSERT_EQ(24u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 332 | |
| 333 | // Test for the add instruction. |
| 334 | HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd(); |
| 335 | interval = add->GetLiveInterval(); |
| 336 | range = interval->GetFirstRange(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 337 | ASSERT_EQ(18u, range->GetStart()); |
| 338 | ASSERT_EQ(22u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 339 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 340 | } |
| 341 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 342 | TEST_F(LiveRangesTest, CFG4) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 343 | /* |
| 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 Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 372 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 373 | 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 Geoffray | a3c00e5 | 2014-11-25 11:18:37 +0000 | [diff] [blame] | 379 | Instruction::RETURN); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 380 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 381 | HGraph* graph = BuildGraph(data); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 382 | std::unique_ptr<CodeGenerator> codegen = CodeGenerator::Create(graph, *compiler_options_); |
| 383 | SsaLivenessAnalysis liveness(graph, codegen.get(), GetScopedAllocator()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 384 | 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 Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 390 | ASSERT_EQ(17u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 391 | range = range->GetNext(); |
| 392 | ASSERT_TRUE(range != nullptr); |
| 393 | ASSERT_EQ(20u, range->GetStart()); |
Mark Mendell | 09b8463 | 2015-02-13 17:48:38 -0500 | [diff] [blame] | 394 | ASSERT_EQ(23u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 395 | 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 Geoffray | a3c00e5 | 2014-11-25 11:18:37 +0000 | [diff] [blame] | 401 | ASSERT_EQ(17u, range->GetEnd()); |
| 402 | range = range->GetNext(); |
| 403 | ASSERT_EQ(20u, range->GetStart()); |
| 404 | ASSERT_EQ(23u, range->GetEnd()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 405 | 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 Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 423 | HPhi* phi = liveness.GetInstructionFromSsaIndex(4)->AsPhi(); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 424 | ASSERT_TRUE(phi->GetUses().HasExactlyOneElement()); |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 425 | 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 Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 432 | } // namespace art |