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); |
| 75 | ASSERT_TRUE(block->GetLastInstruction()->AsReturn() != nullptr); |
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); |
| 121 | ASSERT_TRUE(block->GetLastInstruction()->AsReturn() != nullptr); |
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 | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 149 | * 38: 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 | |
| 197 | TEST(LiveRangesTest, Loop) { |
| 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 | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 236 | x86::CodeGeneratorX86 codegen(graph); |
| 237 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 238 | liveness.Analyze(); |
| 239 | |
| 240 | // Test for the 0 constant. |
| 241 | LiveInterval* interval = liveness.GetInstructionFromSsaIndex(0)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 242 | LiveRange* range = interval->GetFirstRange(); |
| 243 | ASSERT_EQ(2u, range->GetStart()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 244 | // Last use is the loop phi so instruction is live until |
| 245 | // the end of the pre loop header. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 246 | ASSERT_EQ(14u, range->GetEnd()); |
| 247 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 248 | |
| 249 | // Test for the 4 constant. |
| 250 | interval = liveness.GetInstructionFromSsaIndex(1)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 251 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 252 | // The instruction is live until the end of the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 253 | ASSERT_EQ(4u, range->GetStart()); |
| 254 | ASSERT_EQ(24u, range->GetEnd()); |
| 255 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 256 | |
| 257 | // Test for the 5 constant. |
| 258 | interval = liveness.GetInstructionFromSsaIndex(2)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 259 | range = interval->GetFirstRange(); |
| 260 | // The instruction is live until the return instruction after the loop. |
| 261 | ASSERT_EQ(6u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 262 | ASSERT_EQ(27u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 263 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 264 | |
| 265 | // Test for the phi. |
| 266 | interval = liveness.GetInstructionFromSsaIndex(3)->GetLiveInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 267 | range = interval->GetFirstRange(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 268 | // Instruction is consumed by the if. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 269 | ASSERT_EQ(14u, range->GetStart()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 270 | ASSERT_EQ(17u, range->GetEnd()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 271 | ASSERT_TRUE(range->GetNext() == nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | } // namespace art |