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