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" |
| 22 | #include "nodes.h" |
| 23 | #include "optimizing_unit_test.h" |
| 24 | #include "ssa_liveness_analysis.h" |
| 25 | #include "utils/arena_allocator.h" |
| 26 | |
| 27 | #include "gtest/gtest.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | static HGraph* BuildGraph(const uint16_t* data, ArenaAllocator* allocator) { |
| 32 | HGraphBuilder builder(allocator); |
| 33 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 34 | HGraph* graph = builder.BuildGraph(*item); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 35 | // Suspend checks implementation may change in the future, and this test relies |
| 36 | // on how instructions are ordered. |
| 37 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 38 | graph->BuildDominatorTree(); |
| 39 | graph->TransformToSSA(); |
| 40 | graph->FindNaturalLoops(); |
| 41 | return graph; |
| 42 | } |
| 43 | |
| 44 | TEST(LiveRangesTest, CFG1) { |
| 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 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 65 | x86::CodeGeneratorX86 codegen(graph); |
| 66 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 67 | liveness.Analyze(); |
| 68 | |
| 69 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 70 | LiveRange* range = interval->GetFirstRange(); |
| 71 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 72 | // Last use is the return instruction. |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 73 | ASSERT_EQ(9u, range->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 74 | HBasicBlock* block = graph->GetBlocks().Get(1); |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame^] | 75 | ASSERT_TRUE(block->GetLastInstruction()->IsReturn()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 76 | ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition()); |
| 77 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(LiveRangesTest, CFG2) { |
| 81 | /* |
| 82 | * Test the following snippet: |
| 83 | * var a = 0; |
| 84 | * if (0 == 0) { |
| 85 | * } else { |
| 86 | * } |
| 87 | * return a; |
| 88 | * |
| 89 | * Which becomes the following graph (numbered by lifetime position): |
| 90 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 91 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 92 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 93 | * 8: equal |
| 94 | * 10: if |
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 | * 14: goto 18: goto |
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 | * 22: return |
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 | * 26: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 101 | */ |
| 102 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 103 | Instruction::CONST_4 | 0 | 0, |
| 104 | Instruction::IF_EQ, 3, |
| 105 | Instruction::GOTO | 0x100, |
| 106 | Instruction::RETURN | 0 << 8); |
| 107 | |
| 108 | ArenaPool pool; |
| 109 | ArenaAllocator allocator(&pool); |
| 110 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 111 | x86::CodeGeneratorX86 codegen(graph); |
| 112 | SsaLivenessAnalysis liveness(*graph, &codegen); |
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 | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 119 | ASSERT_EQ(23u, range->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 120 | HBasicBlock* block = graph->GetBlocks().Get(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 | |
| 126 | TEST(LiveRangesTest, CFG3) { |
| 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 | */ |
| 151 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 152 | Instruction::CONST_4 | 0 | 0, |
| 153 | Instruction::IF_EQ, 3, |
| 154 | Instruction::CONST_4 | 4 << 12 | 0, |
| 155 | Instruction::RETURN | 0 << 8); |
| 156 | |
| 157 | ArenaPool pool; |
| 158 | ArenaAllocator allocator(&pool); |
| 159 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 160 | x86::CodeGeneratorX86 codegen(graph); |
| 161 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 162 | liveness.Analyze(); |
| 163 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 164 | // Test for the 4 constant. |
| 165 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 166 | LiveRange* range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 167 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 168 | // Last use is the phi at the return block so instruction is live until |
| 169 | // the end of the then block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 170 | ASSERT_EQ(18u, range->GetEnd()); |
| 171 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 172 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 173 | // Test for the 0 constant. |
| 174 | interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 175 | // 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] | 176 | // First range starts from the definition and ends at the if block. |
| 177 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 178 | ASSERT_EQ(2u, range->GetStart()); |
| 179 | // 14 is the end of the if block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 180 | ASSERT_EQ(14u, range->GetEnd()); |
| 181 | // Second range is the else block. |
| 182 | range = range->GetNext(); |
| 183 | ASSERT_EQ(18u, range->GetStart()); |
| 184 | // Last use is the phi at the return block. |
| 185 | ASSERT_EQ(22u, range->GetEnd()); |
| 186 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 187 | |
| 188 | // Test for the phi. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 189 | interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 190 | range = interval->GetFirstRange(); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 191 | ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(2)->GetLifetimePosition()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 192 | ASSERT_EQ(22u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 193 | ASSERT_EQ(25u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 194 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 197 | TEST(LiveRangesTest, Loop1) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Test the following snippet: |
| 200 | * var a = 0; |
| 201 | * while (a == a) { |
| 202 | * a = 4; |
| 203 | * } |
| 204 | * return 5; |
| 205 | * |
| 206 | * Which becomes the following graph (numbered by lifetime position): |
| 207 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 208 | * 4: constant4 |
| 209 | * 6: constant5 |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 210 | * 8: goto |
| 211 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 212 | * 12: goto |
| 213 | * | |
| 214 | * 14: phi |
| 215 | * 16: equal |
| 216 | * 18: if +++++ |
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 | * | 22: goto |
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 | * 26: return |
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 | * 30: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 223 | */ |
| 224 | |
| 225 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 226 | Instruction::CONST_4 | 0 | 0, |
| 227 | Instruction::IF_EQ, 4, |
| 228 | Instruction::CONST_4 | 4 << 12 | 0, |
| 229 | Instruction::GOTO | 0xFD00, |
| 230 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 231 | Instruction::RETURN | 1 << 8); |
| 232 | |
| 233 | ArenaPool pool; |
| 234 | ArenaAllocator allocator(&pool); |
| 235 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 9ebc72c | 2014-09-25 16:33:42 +0100 | [diff] [blame] | 236 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 237 | x86::CodeGeneratorX86 codegen(graph); |
| 238 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 239 | liveness.Analyze(); |
| 240 | |
| 241 | // Test for the 0 constant. |
| 242 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 243 | LiveRange* range = interval->GetFirstRange(); |
| 244 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 245 | // Last use is the loop phi so instruction is live until |
| 246 | // the end of the pre loop header. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 247 | ASSERT_EQ(14u, range->GetEnd()); |
| 248 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 249 | |
| 250 | // Test for the 4 constant. |
| 251 | interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 252 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 253 | // The instruction is live until the end of the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 254 | ASSERT_EQ(4u, range->GetStart()); |
| 255 | ASSERT_EQ(24u, 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 5 constant. |
| 259 | interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 260 | range = interval->GetFirstRange(); |
| 261 | // The instruction is live until the return instruction after the loop. |
| 262 | ASSERT_EQ(6u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 263 | ASSERT_EQ(27u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 264 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 265 | |
| 266 | // Test for the phi. |
| 267 | interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 268 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 269 | // Instruction is consumed by the if. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 270 | ASSERT_EQ(14u, range->GetStart()); |
Nicolas Geoffray | 9ae0daa | 2014-09-30 22:40:23 +0100 | [diff] [blame] | 271 | ASSERT_EQ(16u, 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 | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 275 | TEST(LiveRangesTest, Loop2) { |
| 276 | /* |
| 277 | * Test the following snippet: |
| 278 | * var a = 0; |
| 279 | * while (a == a) { |
| 280 | * a = a + a; |
| 281 | * } |
| 282 | * return a; |
| 283 | * |
| 284 | * Which becomes the following graph (numbered by lifetime position): |
| 285 | * 2: constant0 |
| 286 | * 4: goto |
| 287 | * | |
| 288 | * 8: goto |
| 289 | * | |
| 290 | * 10: phi |
| 291 | * 12: equal |
| 292 | * 14: if +++++ |
| 293 | * | \ + |
| 294 | * | 18: suspend |
| 295 | * | 20: add |
| 296 | * | 22: goto |
| 297 | * | |
| 298 | * 26: return |
| 299 | * | |
| 300 | * 30: exit |
| 301 | * |
| 302 | * We want to make sure the phi at 10 has a lifetime hole after the add at 20. |
| 303 | */ |
| 304 | |
| 305 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 306 | Instruction::CONST_4 | 0 | 0, |
| 307 | Instruction::IF_EQ, 6, |
| 308 | Instruction::ADD_INT, 0, 0, |
| 309 | Instruction::GOTO | 0xFB00, |
| 310 | Instruction::RETURN | 0 << 8); |
| 311 | |
| 312 | ArenaPool pool; |
| 313 | ArenaAllocator allocator(&pool); |
| 314 | HGraph* graph = BuildGraph(data, &allocator); |
| 315 | x86::CodeGeneratorX86 codegen(graph); |
| 316 | SsaLivenessAnalysis liveness(*graph, &codegen); |
| 317 | liveness.Analyze(); |
| 318 | |
| 319 | // Test for the 0 constant. |
| 320 | HIntConstant* constant = liveness.GetInstructionFromSsaIndex(0)->AsIntConstant(); |
| 321 | LiveInterval* interval = constant->GetLiveInterval(); |
| 322 | LiveRange* range = interval->GetFirstRange(); |
| 323 | ASSERT_EQ(2u, range->GetStart()); |
| 324 | // Last use is the loop phi so instruction is live until |
| 325 | // the end of the pre loop header. |
| 326 | ASSERT_EQ(10u, range->GetEnd()); |
| 327 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 328 | |
| 329 | // Test for the loop phi. |
| 330 | HPhi* phi = liveness.GetInstructionFromSsaIndex(1)->AsPhi(); |
| 331 | interval = phi->GetLiveInterval(); |
| 332 | range = interval->GetFirstRange(); |
| 333 | ASSERT_EQ(10u, range->GetStart()); |
| 334 | ASSERT_EQ(21u, range->GetEnd()); |
| 335 | range = range->GetNext(); |
| 336 | ASSERT_TRUE(range != nullptr); |
| 337 | ASSERT_EQ(24u, range->GetStart()); |
| 338 | ASSERT_EQ(27u, range->GetEnd()); |
| 339 | |
| 340 | // Test for the add instruction. |
| 341 | HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd(); |
| 342 | interval = add->GetLiveInterval(); |
| 343 | range = interval->GetFirstRange(); |
| 344 | ASSERT_EQ(20u, range->GetStart()); |
| 345 | ASSERT_EQ(24u, range->GetEnd()); |
| 346 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 347 | } |
| 348 | |
| 349 | TEST(LiveRangesTest, CFG4) { |
| 350 | /* |
| 351 | * Test the following snippet: |
| 352 | * var a = 0; |
| 353 | * var b = 4; |
| 354 | * if (a == a) { |
| 355 | * a = b + a; |
| 356 | * } else { |
| 357 | * a = b + a |
| 358 | * } |
| 359 | * return b; |
| 360 | * |
| 361 | * Which becomes the following graph (numbered by lifetime position): |
| 362 | * 2: constant0 |
| 363 | * 4: constant4 |
| 364 | * 6: goto |
| 365 | * | |
| 366 | * 10: equal |
| 367 | * 12: if |
| 368 | * / \ |
| 369 | * 16: add 22: add |
| 370 | * 18: goto 24: goto |
| 371 | * \ / |
| 372 | * 26: phi |
| 373 | * 28: return |
| 374 | * | |
| 375 | * 32: exit |
| 376 | * |
| 377 | * We want to make sure the constant0 has a lifetime hole after the 16: add. |
| 378 | */ |
| 379 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 380 | Instruction::CONST_4 | 0 | 0, |
| 381 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 382 | Instruction::IF_EQ, 5, |
| 383 | Instruction::ADD_INT, 1 << 8, |
| 384 | Instruction::GOTO | 0x300, |
| 385 | Instruction::ADD_INT, 1 << 8, |
| 386 | Instruction::RETURN | 1 << 8); |
| 387 | |
| 388 | ArenaPool pool; |
| 389 | ArenaAllocator allocator(&pool); |
| 390 | HGraph* graph = BuildGraph(data, &allocator); |
| 391 | x86::CodeGeneratorX86 codegen(graph); |
| 392 | SsaLivenessAnalysis liveness(*graph, &codegen); |
| 393 | liveness.Analyze(); |
| 394 | |
| 395 | // Test for the 0 constant. |
| 396 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
| 397 | LiveRange* range = interval->GetFirstRange(); |
| 398 | ASSERT_EQ(2u, range->GetStart()); |
| 399 | ASSERT_EQ(16u, range->GetEnd()); |
| 400 | range = range->GetNext(); |
| 401 | ASSERT_TRUE(range != nullptr); |
| 402 | ASSERT_EQ(20u, range->GetStart()); |
| 403 | ASSERT_EQ(22u, range->GetEnd()); |
| 404 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 405 | |
| 406 | // Test for the 4 constant. |
| 407 | interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
| 408 | range = interval->GetFirstRange(); |
| 409 | ASSERT_EQ(4u, range->GetStart()); |
| 410 | ASSERT_EQ(29u, range->GetEnd()); |
| 411 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 412 | |
| 413 | // Test for the first add. |
| 414 | HAdd* add = liveness.GetInstructionFromSsaIndex(2)->AsAdd(); |
| 415 | interval = add->GetLiveInterval(); |
| 416 | range = interval->GetFirstRange(); |
| 417 | ASSERT_EQ(16u, range->GetStart()); |
| 418 | ASSERT_EQ(20u, range->GetEnd()); |
| 419 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 420 | |
| 421 | // Test for the second add. |
| 422 | add = liveness.GetInstructionFromSsaIndex(3)->AsAdd(); |
| 423 | interval = add->GetLiveInterval(); |
| 424 | range = interval->GetFirstRange(); |
| 425 | ASSERT_EQ(22u, range->GetStart()); |
| 426 | ASSERT_EQ(26u, range->GetEnd()); |
| 427 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 428 | |
| 429 | // Test for the phi, which is unused. |
| 430 | HPhi* phi = liveness.GetInstructionFromSsaIndex(4)->AsPhi(); |
| 431 | ASSERT_EQ(phi->NumberOfUses(), 0u); |
| 432 | interval = phi->GetLiveInterval(); |
| 433 | range = interval->GetFirstRange(); |
| 434 | ASSERT_EQ(26u, range->GetStart()); |
| 435 | ASSERT_EQ(28u, range->GetEnd()); |
| 436 | ASSERT_TRUE(range->GetNext() == nullptr); |
| 437 | } |
| 438 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 439 | } // namespace art |