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