Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +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 "base/stringprintf.h" |
| 18 | #include "builder.h" |
| 19 | #include "dex_file.h" |
| 20 | #include "dex_instruction.h" |
| 21 | #include "nodes.h" |
| 22 | #include "optimizing_unit_test.h" |
| 23 | #include "pretty_printer.h" |
| 24 | #include "ssa_builder.h" |
| 25 | #include "utils/arena_allocator.h" |
| 26 | |
| 27 | #include "gtest/gtest.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 31 | class SsaPrettyPrinter : public HPrettyPrinter { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 32 | public: |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 33 | explicit SsaPrettyPrinter(HGraph* graph) : HPrettyPrinter(graph), str_("") {} |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 34 | |
| 35 | virtual void PrintInt(int value) { |
| 36 | str_ += StringPrintf("%d", value); |
| 37 | } |
| 38 | |
| 39 | virtual void PrintString(const char* value) { |
| 40 | str_ += value; |
| 41 | } |
| 42 | |
| 43 | virtual void PrintNewLine() { |
| 44 | str_ += '\n'; |
| 45 | } |
| 46 | |
| 47 | void Clear() { str_.clear(); } |
| 48 | |
| 49 | std::string str() const { return str_; } |
| 50 | |
| 51 | virtual void VisitIntConstant(HIntConstant* constant) { |
| 52 | PrintPreInstruction(constant); |
| 53 | str_ += constant->DebugName(); |
| 54 | str_ += " "; |
| 55 | PrintInt(constant->GetValue()); |
| 56 | PrintPostInstruction(constant); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | std::string str_; |
| 61 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(SsaPrettyPrinter); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static void ReNumberInstructions(HGraph* graph) { |
| 66 | int id = 0; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 67 | for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) { |
| 68 | HBasicBlock* block = graph->GetBlocks().Get(i); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 69 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 70 | it.Current()->SetId(id++); |
| 71 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 72 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 73 | it.Current()->SetId(id++); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static void TestCode(const uint16_t* data, const char* expected) { |
| 79 | ArenaPool pool; |
| 80 | ArenaAllocator allocator(&pool); |
| 81 | HGraphBuilder builder(&allocator); |
| 82 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 83 | HGraph* graph = builder.BuildGraph(*item); |
| 84 | ASSERT_NE(graph, nullptr); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 85 | |
Nicolas Geoffray | 9ebc72c | 2014-09-25 16:33:42 +0100 | [diff] [blame] | 86 | graph->BuildDominatorTree(); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 87 | // Suspend checks implementation may change in the future, and this test relies |
| 88 | // on how instructions are ordered. |
| 89 | RemoveSuspendChecks(graph); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 90 | graph->TransformToSSA(); |
| 91 | ReNumberInstructions(graph); |
| 92 | |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 93 | // Test that phis had their type set. |
| 94 | for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) { |
| 95 | for (HInstructionIterator it(graph->GetBlocks().Get(i)->GetPhis()); !it.Done(); it.Advance()) { |
| 96 | ASSERT_NE(it.Current()->GetType(), Primitive::kPrimVoid); |
| 97 | } |
| 98 | } |
| 99 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 100 | SsaPrettyPrinter printer(graph); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 101 | printer.VisitInsertionOrder(); |
| 102 | |
| 103 | ASSERT_STREQ(expected, printer.str().c_str()); |
| 104 | } |
| 105 | |
| 106 | TEST(SsaTest, CFG1) { |
| 107 | // Test that we get rid of loads and stores. |
| 108 | const char* expected = |
| 109 | "BasicBlock 0, succ: 1\n" |
| 110 | " 0: IntConstant 0 [2, 2]\n" |
| 111 | " 1: Goto\n" |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 112 | "BasicBlock 1, pred: 0, succ: 5, 2\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 113 | " 2: Equal(0, 0) [3]\n" |
| 114 | " 3: If(2)\n" |
| 115 | "BasicBlock 2, pred: 1, succ: 3\n" |
| 116 | " 4: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 117 | "BasicBlock 3, pred: 2, 5, succ: 4\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 118 | " 5: ReturnVoid\n" |
| 119 | "BasicBlock 4, pred: 3\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 120 | " 6: Exit\n" |
| 121 | // Synthesized block to avoid critical edge. |
| 122 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 123 | " 7: Goto\n"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 124 | |
| 125 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 126 | Instruction::CONST_4 | 0 | 0, |
| 127 | Instruction::IF_EQ, 3, |
| 128 | Instruction::GOTO | 0x100, |
| 129 | Instruction::RETURN_VOID); |
| 130 | |
| 131 | TestCode(data, expected); |
| 132 | } |
| 133 | |
| 134 | TEST(SsaTest, CFG2) { |
| 135 | // Test that we create a phi for the join block of an if control flow instruction |
| 136 | // when there is only code in the else branch. |
| 137 | const char* expected = |
| 138 | "BasicBlock 0, succ: 1\n" |
| 139 | " 0: IntConstant 0 [6, 3, 3]\n" |
| 140 | " 1: IntConstant 4 [6]\n" |
| 141 | " 2: Goto\n" |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 142 | "BasicBlock 1, pred: 0, succ: 5, 2\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 143 | " 3: Equal(0, 0) [4]\n" |
| 144 | " 4: If(3)\n" |
| 145 | "BasicBlock 2, pred: 1, succ: 3\n" |
| 146 | " 5: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 147 | "BasicBlock 3, pred: 2, 5, succ: 4\n" |
| 148 | " 6: Phi(1, 0) [7]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 149 | " 7: Return(6)\n" |
| 150 | "BasicBlock 4, pred: 3\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 151 | " 8: Exit\n" |
| 152 | // Synthesized block to avoid critical edge. |
| 153 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 154 | " 9: Goto\n"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 155 | |
| 156 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 157 | Instruction::CONST_4 | 0 | 0, |
| 158 | Instruction::IF_EQ, 3, |
| 159 | Instruction::CONST_4 | 4 << 12 | 0, |
| 160 | Instruction::RETURN | 0 << 8); |
| 161 | |
| 162 | TestCode(data, expected); |
| 163 | } |
| 164 | |
| 165 | TEST(SsaTest, CFG3) { |
| 166 | // Test that we create a phi for the join block of an if control flow instruction |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 167 | // when both branches update a local. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 168 | const char* expected = |
| 169 | "BasicBlock 0, succ: 1\n" |
| 170 | " 0: IntConstant 0 [4, 4]\n" |
| 171 | " 1: IntConstant 4 [8]\n" |
| 172 | " 2: IntConstant 5 [8]\n" |
| 173 | " 3: Goto\n" |
| 174 | "BasicBlock 1, pred: 0, succ: 3, 2\n" |
| 175 | " 4: Equal(0, 0) [5]\n" |
| 176 | " 5: If(4)\n" |
| 177 | "BasicBlock 2, pred: 1, succ: 4\n" |
| 178 | " 6: Goto\n" |
| 179 | "BasicBlock 3, pred: 1, succ: 4\n" |
| 180 | " 7: Goto\n" |
| 181 | "BasicBlock 4, pred: 2, 3, succ: 5\n" |
| 182 | " 8: Phi(1, 2) [9]\n" |
| 183 | " 9: Return(8)\n" |
| 184 | "BasicBlock 5, pred: 4\n" |
| 185 | " 10: Exit\n"; |
| 186 | |
| 187 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 188 | Instruction::CONST_4 | 0 | 0, |
| 189 | Instruction::IF_EQ, 4, |
| 190 | Instruction::CONST_4 | 4 << 12 | 0, |
| 191 | Instruction::GOTO | 0x200, |
| 192 | Instruction::CONST_4 | 5 << 12 | 0, |
| 193 | Instruction::RETURN | 0 << 8); |
| 194 | |
| 195 | TestCode(data, expected); |
| 196 | } |
| 197 | |
| 198 | TEST(SsaTest, Loop1) { |
| 199 | // Test that we create a phi for an initialized local at entry of a loop. |
| 200 | const char* expected = |
| 201 | "BasicBlock 0, succ: 1\n" |
| 202 | " 0: IntConstant 0 [6, 4, 2, 2]\n" |
| 203 | " 1: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 204 | "BasicBlock 1, pred: 0, succ: 5, 6\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 205 | " 2: Equal(0, 0) [3]\n" |
| 206 | " 3: If(2)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 207 | "BasicBlock 2, pred: 3, 6, succ: 3\n" |
| 208 | " 4: Phi(6, 0) [6]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 209 | " 5: Goto\n" |
Nicolas Geoffray | c83d441 | 2014-09-18 16:46:20 +0100 | [diff] [blame] | 210 | "BasicBlock 3, pred: 5, 2, succ: 2\n" |
| 211 | " 6: Phi(0, 4) [4]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 212 | " 7: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 213 | "BasicBlock 4\n" |
| 214 | // Synthesized blocks to avoid critical edge. |
| 215 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 216 | " 8: Goto\n" |
| 217 | "BasicBlock 6, pred: 1, succ: 2\n" |
| 218 | " 9: Goto\n"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 219 | |
| 220 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 221 | Instruction::CONST_4 | 0 | 0, |
| 222 | Instruction::IF_EQ, 3, |
| 223 | Instruction::GOTO | 0x100, |
| 224 | Instruction::GOTO | 0xFF00); |
| 225 | |
| 226 | TestCode(data, expected); |
| 227 | } |
| 228 | |
| 229 | TEST(SsaTest, Loop2) { |
| 230 | // Simple loop with one preheader and one back edge. |
| 231 | const char* expected = |
| 232 | "BasicBlock 0, succ: 1\n" |
| 233 | " 0: IntConstant 0 [4]\n" |
| 234 | " 1: IntConstant 4 [4]\n" |
| 235 | " 2: Goto\n" |
| 236 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 237 | " 3: Goto\n" |
| 238 | "BasicBlock 2, pred: 1, 3, succ: 4, 3\n" |
| 239 | " 4: Phi(0, 1) [5, 5]\n" |
| 240 | " 5: Equal(4, 4) [6]\n" |
| 241 | " 6: If(5)\n" |
| 242 | "BasicBlock 3, pred: 2, succ: 2\n" |
| 243 | " 7: Goto\n" |
| 244 | "BasicBlock 4, pred: 2, succ: 5\n" |
| 245 | " 8: ReturnVoid\n" |
| 246 | "BasicBlock 5, pred: 4\n" |
| 247 | " 9: Exit\n"; |
| 248 | |
| 249 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 250 | Instruction::CONST_4 | 0 | 0, |
| 251 | Instruction::IF_EQ, 4, |
| 252 | Instruction::CONST_4 | 4 << 12 | 0, |
| 253 | Instruction::GOTO | 0xFD00, |
| 254 | Instruction::RETURN_VOID); |
| 255 | |
| 256 | TestCode(data, expected); |
| 257 | } |
| 258 | |
| 259 | TEST(SsaTest, Loop3) { |
| 260 | // Test that a local not yet defined at the entry of a loop is handled properly. |
| 261 | const char* expected = |
| 262 | "BasicBlock 0, succ: 1\n" |
| 263 | " 0: IntConstant 0 [5]\n" |
| 264 | " 1: IntConstant 4 [5]\n" |
| 265 | " 2: IntConstant 5 [9]\n" |
| 266 | " 3: Goto\n" |
| 267 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 268 | " 4: Goto\n" |
| 269 | "BasicBlock 2, pred: 1, 3, succ: 4, 3\n" |
| 270 | " 5: Phi(0, 1) [6, 6]\n" |
| 271 | " 6: Equal(5, 5) [7]\n" |
| 272 | " 7: If(6)\n" |
| 273 | "BasicBlock 3, pred: 2, succ: 2\n" |
| 274 | " 8: Goto\n" |
| 275 | "BasicBlock 4, pred: 2, succ: 5\n" |
| 276 | " 9: Return(2)\n" |
| 277 | "BasicBlock 5, pred: 4\n" |
| 278 | " 10: Exit\n"; |
| 279 | |
| 280 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 281 | Instruction::CONST_4 | 0 | 0, |
| 282 | Instruction::IF_EQ, 4, |
| 283 | Instruction::CONST_4 | 4 << 12 | 0, |
| 284 | Instruction::GOTO | 0xFD00, |
| 285 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 286 | Instruction::RETURN | 1 << 8); |
| 287 | |
| 288 | TestCode(data, expected); |
| 289 | } |
| 290 | |
| 291 | TEST(SsaTest, Loop4) { |
| 292 | // Make sure we support a preheader of a loop not being the first predecessor |
| 293 | // in the predecessor list of the header. |
| 294 | const char* expected = |
| 295 | "BasicBlock 0, succ: 1\n" |
| 296 | " 0: IntConstant 0 [4]\n" |
| 297 | " 1: IntConstant 4 [4]\n" |
| 298 | " 2: Goto\n" |
| 299 | "BasicBlock 1, pred: 0, succ: 4\n" |
| 300 | " 3: Goto\n" |
Nicolas Geoffray | c83d441 | 2014-09-18 16:46:20 +0100 | [diff] [blame] | 301 | "BasicBlock 2, pred: 4, 3, succ: 5, 3\n" |
| 302 | " 4: Phi(0, 1) [9, 5, 5]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 303 | " 5: Equal(4, 4) [6]\n" |
| 304 | " 6: If(5)\n" |
| 305 | "BasicBlock 3, pred: 2, succ: 2\n" |
| 306 | " 7: Goto\n" |
| 307 | "BasicBlock 4, pred: 1, succ: 2\n" |
| 308 | " 8: Goto\n" |
| 309 | "BasicBlock 5, pred: 2, succ: 6\n" |
| 310 | " 9: Return(4)\n" |
| 311 | "BasicBlock 6, pred: 5\n" |
| 312 | " 10: Exit\n"; |
| 313 | |
| 314 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 315 | Instruction::CONST_4 | 0 | 0, |
| 316 | Instruction::GOTO | 0x500, |
| 317 | Instruction::IF_EQ, 5, |
| 318 | Instruction::CONST_4 | 4 << 12 | 0, |
| 319 | Instruction::GOTO | 0xFD00, |
| 320 | Instruction::GOTO | 0xFC00, |
| 321 | Instruction::RETURN | 0 << 8); |
| 322 | |
| 323 | TestCode(data, expected); |
| 324 | } |
| 325 | |
| 326 | TEST(SsaTest, Loop5) { |
| 327 | // Make sure we create a preheader of a loop when a header originally has two |
| 328 | // incoming blocks and one back edge. |
| 329 | const char* expected = |
| 330 | "BasicBlock 0, succ: 1\n" |
| 331 | " 0: IntConstant 0 [4, 4]\n" |
| 332 | " 1: IntConstant 4 [14]\n" |
| 333 | " 2: IntConstant 5 [14]\n" |
| 334 | " 3: Goto\n" |
| 335 | "BasicBlock 1, pred: 0, succ: 3, 2\n" |
| 336 | " 4: Equal(0, 0) [5]\n" |
| 337 | " 5: If(4)\n" |
| 338 | "BasicBlock 2, pred: 1, succ: 8\n" |
| 339 | " 6: Goto\n" |
| 340 | "BasicBlock 3, pred: 1, succ: 8\n" |
| 341 | " 7: Goto\n" |
Nicolas Geoffray | c83d441 | 2014-09-18 16:46:20 +0100 | [diff] [blame] | 342 | "BasicBlock 4, pred: 8, 5, succ: 6, 5\n" |
| 343 | " 8: Phi(14, 8) [8, 12, 9, 9]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 344 | " 9: Equal(8, 8) [10]\n" |
| 345 | " 10: If(9)\n" |
| 346 | "BasicBlock 5, pred: 4, succ: 4\n" |
| 347 | " 11: Goto\n" |
| 348 | "BasicBlock 6, pred: 4, succ: 7\n" |
| 349 | " 12: Return(8)\n" |
| 350 | "BasicBlock 7, pred: 6\n" |
| 351 | " 13: Exit\n" |
| 352 | "BasicBlock 8, pred: 2, 3, succ: 4\n" |
| 353 | " 14: Phi(1, 2) [8]\n" |
| 354 | " 15: Goto\n"; |
| 355 | |
| 356 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 357 | Instruction::CONST_4 | 0 | 0, |
| 358 | Instruction::IF_EQ, 4, |
| 359 | Instruction::CONST_4 | 4 << 12 | 0, |
| 360 | Instruction::GOTO | 0x200, |
| 361 | Instruction::CONST_4 | 5 << 12 | 0, |
| 362 | Instruction::IF_EQ, 3, |
| 363 | Instruction::GOTO | 0xFE00, |
| 364 | Instruction::RETURN | 0 << 8); |
| 365 | |
| 366 | TestCode(data, expected); |
| 367 | } |
| 368 | |
| 369 | TEST(SsaTest, Loop6) { |
| 370 | // Test a loop with one preheader and two back edges (e.g. continue). |
| 371 | const char* expected = |
| 372 | "BasicBlock 0, succ: 1\n" |
| 373 | " 0: IntConstant 0 [5]\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 374 | " 1: IntConstant 4 [14, 8, 8]\n" |
| 375 | " 2: IntConstant 5 [14]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 376 | " 3: Goto\n" |
| 377 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 378 | " 4: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 379 | "BasicBlock 2, pred: 1, 8, succ: 6, 3\n" |
| 380 | " 5: Phi(0, 14) [12, 6, 6]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 381 | " 6: Equal(5, 5) [7]\n" |
| 382 | " 7: If(6)\n" |
| 383 | "BasicBlock 3, pred: 2, succ: 5, 4\n" |
| 384 | " 8: Equal(1, 1) [9]\n" |
| 385 | " 9: If(8)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 386 | "BasicBlock 4, pred: 3, succ: 8\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 387 | " 10: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 388 | "BasicBlock 5, pred: 3, succ: 8\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 389 | " 11: Goto\n" |
| 390 | "BasicBlock 6, pred: 2, succ: 7\n" |
| 391 | " 12: Return(5)\n" |
| 392 | "BasicBlock 7, pred: 6\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 393 | " 13: Exit\n" |
| 394 | // Synthesized single back edge of loop. |
| 395 | "BasicBlock 8, pred: 5, 4, succ: 2\n" |
| 396 | " 14: Phi(1, 2) [5]\n" |
| 397 | " 15: Goto\n"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 398 | |
| 399 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 400 | Instruction::CONST_4 | 0 | 0, |
| 401 | Instruction::IF_EQ, 8, |
| 402 | Instruction::CONST_4 | 4 << 12 | 0, |
| 403 | Instruction::IF_EQ, 4, |
| 404 | Instruction::CONST_4 | 5 << 12 | 0, |
| 405 | Instruction::GOTO | 0xFA00, |
| 406 | Instruction::GOTO | 0xF900, |
| 407 | Instruction::RETURN | 0 << 8); |
| 408 | |
| 409 | TestCode(data, expected); |
| 410 | } |
| 411 | |
| 412 | TEST(SsaTest, Loop7) { |
| 413 | // Test a loop with one preheader, one back edge, and two exit edges (e.g. break). |
| 414 | const char* expected = |
| 415 | "BasicBlock 0, succ: 1\n" |
| 416 | " 0: IntConstant 0 [5]\n" |
| 417 | " 1: IntConstant 4 [5, 8, 8]\n" |
| 418 | " 2: IntConstant 5 [12]\n" |
| 419 | " 3: Goto\n" |
| 420 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 421 | " 4: Goto\n" |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 422 | "BasicBlock 2, pred: 1, 5, succ: 8, 3\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 423 | " 5: Phi(0, 1) [12, 6, 6]\n" |
| 424 | " 6: Equal(5, 5) [7]\n" |
| 425 | " 7: If(6)\n" |
| 426 | "BasicBlock 3, pred: 2, succ: 5, 4\n" |
| 427 | " 8: Equal(1, 1) [9]\n" |
| 428 | " 9: If(8)\n" |
| 429 | "BasicBlock 4, pred: 3, succ: 6\n" |
| 430 | " 10: Goto\n" |
| 431 | "BasicBlock 5, pred: 3, succ: 2\n" |
| 432 | " 11: Goto\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 433 | "BasicBlock 6, pred: 4, 8, succ: 7\n" |
| 434 | " 12: Phi(2, 5) [13]\n" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 435 | " 13: Return(12)\n" |
| 436 | "BasicBlock 7, pred: 6\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 437 | " 14: Exit\n" |
| 438 | "BasicBlock 8, pred: 2, succ: 6\n" |
| 439 | " 15: Goto\n"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 440 | |
| 441 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 442 | Instruction::CONST_4 | 0 | 0, |
| 443 | Instruction::IF_EQ, 8, |
| 444 | Instruction::CONST_4 | 4 << 12 | 0, |
| 445 | Instruction::IF_EQ, 4, |
| 446 | Instruction::CONST_4 | 5 << 12 | 0, |
| 447 | Instruction::GOTO | 0x0200, |
| 448 | Instruction::GOTO | 0xF900, |
| 449 | Instruction::RETURN | 0 << 8); |
| 450 | |
| 451 | TestCode(data, expected); |
| 452 | } |
| 453 | |
| 454 | TEST(SsaTest, DeadLocal) { |
| 455 | // Test that we correctly handle a local not being used. |
| 456 | const char* expected = |
| 457 | "BasicBlock 0, succ: 1\n" |
| 458 | " 0: IntConstant 0\n" |
| 459 | " 1: Goto\n" |
| 460 | "BasicBlock 1, pred: 0, succ: 2\n" |
| 461 | " 2: ReturnVoid\n" |
| 462 | "BasicBlock 2, pred: 1\n" |
| 463 | " 3: Exit\n"; |
| 464 | |
| 465 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 466 | Instruction::CONST_4 | 0 | 0, |
| 467 | Instruction::RETURN_VOID); |
| 468 | |
| 469 | TestCode(data, expected); |
| 470 | } |
| 471 | |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 472 | TEST(SsaTest, LocalInIf) { |
| 473 | // Test that we do not create a phi in the join block when one predecessor |
| 474 | // does not update the local. |
| 475 | const char* expected = |
| 476 | "BasicBlock 0, succ: 1\n" |
| 477 | " 0: IntConstant 0 [3, 3]\n" |
| 478 | " 1: IntConstant 4\n" |
| 479 | " 2: Goto\n" |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 480 | "BasicBlock 1, pred: 0, succ: 5, 2\n" |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 481 | " 3: Equal(0, 0) [4]\n" |
| 482 | " 4: If(3)\n" |
| 483 | "BasicBlock 2, pred: 1, succ: 3\n" |
| 484 | " 5: Goto\n" |
| 485 | "BasicBlock 3, pred: 2, 5, succ: 4\n" |
| 486 | " 6: ReturnVoid\n" |
| 487 | "BasicBlock 4, pred: 3\n" |
| 488 | " 7: Exit\n" |
| 489 | // Synthesized block to avoid critical edge. |
| 490 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 491 | " 8: Goto\n"; |
| 492 | |
| 493 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 494 | Instruction::CONST_4 | 0 | 0, |
| 495 | Instruction::IF_EQ, 3, |
| 496 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 497 | Instruction::RETURN_VOID); |
| 498 | |
| 499 | TestCode(data, expected); |
| 500 | } |
| 501 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 502 | TEST(SsaTest, MultiplePredecessors) { |
| 503 | // Test that we do not create a phi when one predecessor |
| 504 | // does not update the local. |
| 505 | const char* expected = |
| 506 | "BasicBlock 0, succ: 1\n" |
| 507 | " 0: IntConstant 0 [4, 8, 6, 6, 2, 2, 8, 4]\n" |
| 508 | " 1: Goto\n" |
| 509 | "BasicBlock 1, pred: 0, succ: 3, 2\n" |
| 510 | " 2: Equal(0, 0) [3]\n" |
| 511 | " 3: If(2)\n" |
| 512 | "BasicBlock 2, pred: 1, succ: 5\n" |
| 513 | " 4: Add(0, 0)\n" |
| 514 | " 5: Goto\n" |
| 515 | "BasicBlock 3, pred: 1, succ: 7, 4\n" |
| 516 | " 6: Equal(0, 0) [7]\n" |
| 517 | " 7: If(6)\n" |
| 518 | "BasicBlock 4, pred: 3, succ: 5\n" |
| 519 | " 8: Add(0, 0)\n" |
| 520 | " 9: Goto\n" |
| 521 | // This block should not get a phi for local 1. |
| 522 | "BasicBlock 5, pred: 2, 4, 7, succ: 6\n" |
| 523 | " 10: ReturnVoid\n" |
| 524 | "BasicBlock 6, pred: 5\n" |
| 525 | " 11: Exit\n" |
| 526 | "BasicBlock 7, pred: 3, succ: 5\n" |
| 527 | " 12: Goto\n"; |
| 528 | |
| 529 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 530 | Instruction::CONST_4 | 0 | 0, |
| 531 | Instruction::IF_EQ, 5, |
| 532 | Instruction::ADD_INT_LIT8 | 1 << 8, 0 << 8, |
| 533 | Instruction::GOTO | 0x0500, |
| 534 | Instruction::IF_EQ, 4, |
| 535 | Instruction::ADD_INT_LIT8 | 1 << 8, 0 << 8, |
| 536 | Instruction::RETURN_VOID); |
| 537 | |
| 538 | TestCode(data, expected); |
| 539 | } |
| 540 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 541 | } // namespace art |