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 | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 19 | #include "dex_file.h" |
| 20 | #include "dex_instruction.h" |
| 21 | #include "nodes.h" |
| 22 | #include "optimizing_unit_test.h" |
| 23 | #include "ssa_liveness_analysis.h" |
| 24 | #include "utils/arena_allocator.h" |
| 25 | |
| 26 | #include "gtest/gtest.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | static HGraph* BuildGraph(const uint16_t* data, ArenaAllocator* allocator) { |
| 31 | HGraphBuilder builder(allocator); |
| 32 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 33 | HGraph* graph = builder.BuildGraph(*item); |
| 34 | graph->BuildDominatorTree(); |
| 35 | graph->TransformToSSA(); |
| 36 | graph->FindNaturalLoops(); |
| 37 | return graph; |
| 38 | } |
| 39 | |
| 40 | TEST(LiveRangesTest, CFG1) { |
| 41 | /* |
| 42 | * Test the following snippet: |
| 43 | * return 0; |
| 44 | * |
| 45 | * Which becomes the following graph (numbered by lifetime position): |
| 46 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 47 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 48 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 49 | * 8: return |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 50 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 51 | * 12: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 52 | */ |
| 53 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 54 | Instruction::CONST_4 | 0 | 0, |
| 55 | Instruction::RETURN); |
| 56 | |
| 57 | ArenaPool pool; |
| 58 | ArenaAllocator allocator(&pool); |
| 59 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 60 | |
| 61 | CodeGenerator* codegen = CodeGenerator::Create(&allocator, graph, InstructionSet::kX86); |
| 62 | SsaLivenessAnalysis liveness(*graph, codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 63 | liveness.Analyze(); |
| 64 | |
| 65 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 66 | LiveRange* range = interval->GetFirstRange(); |
| 67 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 68 | // Last use is the return instruction. |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 69 | ASSERT_EQ(9u, range->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 70 | HBasicBlock* block = graph->GetBlocks().Get(1); |
| 71 | ASSERT_TRUE(block->GetLastInstruction()->AsReturn() != nullptr); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 72 | ASSERT_EQ(8u, block->GetLastInstruction()->GetLifetimePosition()); |
| 73 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(LiveRangesTest, CFG2) { |
| 77 | /* |
| 78 | * Test the following snippet: |
| 79 | * var a = 0; |
| 80 | * if (0 == 0) { |
| 81 | * } else { |
| 82 | * } |
| 83 | * return a; |
| 84 | * |
| 85 | * Which becomes the following graph (numbered by lifetime position): |
| 86 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 87 | * 4: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 88 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 89 | * 8: equal |
| 90 | * 10: if |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 91 | * / \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 92 | * 14: goto 18: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 93 | * \ / |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 94 | * 22: return |
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 | * 26: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 97 | */ |
| 98 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 99 | Instruction::CONST_4 | 0 | 0, |
| 100 | Instruction::IF_EQ, 3, |
| 101 | Instruction::GOTO | 0x100, |
| 102 | Instruction::RETURN | 0 << 8); |
| 103 | |
| 104 | ArenaPool pool; |
| 105 | ArenaAllocator allocator(&pool); |
| 106 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 107 | CodeGenerator* codegen = CodeGenerator::Create(&allocator, graph, InstructionSet::kX86); |
| 108 | SsaLivenessAnalysis liveness(*graph, codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 109 | liveness.Analyze(); |
| 110 | |
| 111 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 112 | LiveRange* range = interval->GetFirstRange(); |
| 113 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 114 | // Last use is the return instruction. |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 115 | ASSERT_EQ(23u, range->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 116 | HBasicBlock* block = graph->GetBlocks().Get(3); |
| 117 | ASSERT_TRUE(block->GetLastInstruction()->AsReturn() != nullptr); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 118 | ASSERT_EQ(22u, block->GetLastInstruction()->GetLifetimePosition()); |
| 119 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TEST(LiveRangesTest, CFG3) { |
| 123 | /* |
| 124 | * Test the following snippet: |
| 125 | * var a = 0; |
| 126 | * if (0 == 0) { |
| 127 | * } else { |
| 128 | * a = 4; |
| 129 | * } |
| 130 | * return a; |
| 131 | * |
| 132 | * Which becomes the following graph (numbered by lifetime position): |
| 133 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 134 | * 4: constant4 |
| 135 | * 6: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 136 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 137 | * 10: equal |
| 138 | * 12: if |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 139 | * / \ |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 140 | * 16: goto 20: goto |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 141 | * \ / |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 142 | * 22: phi |
| 143 | * 24: return |
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 | * 38: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 146 | */ |
| 147 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 148 | Instruction::CONST_4 | 0 | 0, |
| 149 | Instruction::IF_EQ, 3, |
| 150 | Instruction::CONST_4 | 4 << 12 | 0, |
| 151 | Instruction::RETURN | 0 << 8); |
| 152 | |
| 153 | ArenaPool pool; |
| 154 | ArenaAllocator allocator(&pool); |
| 155 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 156 | CodeGenerator* codegen = CodeGenerator::Create(&allocator, graph, InstructionSet::kX86); |
| 157 | SsaLivenessAnalysis liveness(*graph, codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 158 | liveness.Analyze(); |
| 159 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 160 | // Test for the 4 constant. |
| 161 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 162 | LiveRange* range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 163 | ASSERT_EQ(4u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 164 | // Last use is the phi at the return block so instruction is live until |
| 165 | // the end of the then block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 166 | ASSERT_EQ(18u, range->GetEnd()); |
| 167 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 168 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 169 | // Test for the 0 constant. |
| 170 | interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 171 | // 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] | 172 | // First range starts from the definition and ends at the if block. |
| 173 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 174 | ASSERT_EQ(2u, range->GetStart()); |
| 175 | // 14 is the end of the if block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 176 | ASSERT_EQ(14u, range->GetEnd()); |
| 177 | // Second range is the else block. |
| 178 | range = range->GetNext(); |
| 179 | ASSERT_EQ(18u, range->GetStart()); |
| 180 | // Last use is the phi at the return block. |
| 181 | ASSERT_EQ(22u, range->GetEnd()); |
| 182 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 183 | |
| 184 | // Test for the phi. |
| 185 | interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 186 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 187 | ASSERT_EQ(22u, liveness.GetInstructionFromSsaIndex(3)->GetLifetimePosition()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 188 | ASSERT_EQ(22u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 189 | ASSERT_EQ(25u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 190 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | TEST(LiveRangesTest, Loop) { |
| 194 | /* |
| 195 | * Test the following snippet: |
| 196 | * var a = 0; |
| 197 | * while (a == a) { |
| 198 | * a = 4; |
| 199 | * } |
| 200 | * return 5; |
| 201 | * |
| 202 | * Which becomes the following graph (numbered by lifetime position): |
| 203 | * 2: constant0 |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 204 | * 4: constant4 |
| 205 | * 6: constant5 |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 206 | * 8: goto |
| 207 | * | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 208 | * 12: goto |
| 209 | * | |
| 210 | * 14: phi |
| 211 | * 16: equal |
| 212 | * 18: if +++++ |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 213 | * | \ + |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 214 | * | 22: goto |
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 | * 26: return |
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 | * 30: exit |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 219 | */ |
| 220 | |
| 221 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 222 | Instruction::CONST_4 | 0 | 0, |
| 223 | Instruction::IF_EQ, 4, |
| 224 | Instruction::CONST_4 | 4 << 12 | 0, |
| 225 | Instruction::GOTO | 0xFD00, |
| 226 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 227 | Instruction::RETURN | 1 << 8); |
| 228 | |
| 229 | ArenaPool pool; |
| 230 | ArenaAllocator allocator(&pool); |
| 231 | HGraph* graph = BuildGraph(data, &allocator); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 232 | CodeGenerator* codegen = CodeGenerator::Create(&allocator, graph, InstructionSet::kX86); |
| 233 | SsaLivenessAnalysis liveness(*graph, codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 234 | liveness.Analyze(); |
| 235 | |
| 236 | // Test for the 0 constant. |
| 237 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 238 | LiveRange* range = interval->GetFirstRange(); |
| 239 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 240 | // Last use is the loop phi so instruction is live until |
| 241 | // the end of the pre loop header. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 242 | ASSERT_EQ(14u, range->GetEnd()); |
| 243 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 244 | |
| 245 | // Test for the 4 constant. |
| 246 | interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 247 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 248 | // The instruction is live until the end of the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 249 | ASSERT_EQ(4u, range->GetStart()); |
| 250 | ASSERT_EQ(24u, range->GetEnd()); |
| 251 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 252 | |
| 253 | // Test for the 5 constant. |
| 254 | interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 255 | range = interval->GetFirstRange(); |
| 256 | // The instruction is live until the return instruction after the loop. |
| 257 | ASSERT_EQ(6u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 258 | ASSERT_EQ(27u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 259 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 260 | |
| 261 | // Test for the phi. |
| 262 | interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 263 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 264 | // Instruction is consumed by the if. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 265 | ASSERT_EQ(14u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 266 | ASSERT_EQ(17u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 267 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | } // namespace art |