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