Roland Levillain | ccc07a9 | 2014-09-16 14:48: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 "graph_checker.h" |
| 18 | #include "optimizing_unit_test.h" |
| 19 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 20 | namespace art { |
| 21 | |
| 22 | /** |
| 23 | * Create a simple control-flow graph composed of two blocks: |
| 24 | * |
| 25 | * BasicBlock 0, succ: 1 |
David Brazdil | dbf5d75 | 2015-07-30 18:21:41 +0100 | [diff] [blame] | 26 | * 0: ReturnVoid 1 |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 27 | * BasicBlock 1, pred: 0 |
| 28 | * 1: Exit |
| 29 | */ |
| 30 | HGraph* CreateSimpleCFG(ArenaAllocator* allocator) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 31 | HGraph* graph = CreateGraph(allocator); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 32 | HBasicBlock* entry_block = new (allocator) HBasicBlock(graph); |
David Brazdil | dbf5d75 | 2015-07-30 18:21:41 +0100 | [diff] [blame] | 33 | entry_block->AddInstruction(new (allocator) HReturnVoid()); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 34 | graph->AddBlock(entry_block); |
| 35 | graph->SetEntryBlock(entry_block); |
| 36 | HBasicBlock* exit_block = new (allocator) HBasicBlock(graph); |
| 37 | exit_block->AddInstruction(new (allocator) HExit()); |
| 38 | graph->AddBlock(exit_block); |
| 39 | graph->SetExitBlock(exit_block); |
| 40 | entry_block->AddSuccessor(exit_block); |
| 41 | return graph; |
| 42 | } |
| 43 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 44 | static void TestCode(const uint16_t* data) { |
| 45 | ArenaPool pool; |
| 46 | ArenaAllocator allocator(&pool); |
| 47 | HGraph* graph = CreateCFG(&allocator, data); |
| 48 | ASSERT_NE(graph, nullptr); |
| 49 | |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 50 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 51 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 52 | ASSERT_TRUE(graph_checker.IsValid()); |
| 53 | } |
| 54 | |
| 55 | static void TestCodeSSA(const uint16_t* data) { |
| 56 | ArenaPool pool; |
| 57 | ArenaAllocator allocator(&pool); |
| 58 | HGraph* graph = CreateCFG(&allocator, data); |
| 59 | ASSERT_NE(graph, nullptr); |
| 60 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 61 | TransformToSsa(graph); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 62 | |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 63 | SSAChecker ssa_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 64 | ssa_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 65 | ASSERT_TRUE(ssa_checker.IsValid()); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | TEST(GraphChecker, ReturnVoid) { |
| 70 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
| 71 | Instruction::RETURN_VOID); |
| 72 | |
| 73 | TestCode(data); |
| 74 | } |
| 75 | |
| 76 | TEST(GraphChecker, CFG1) { |
| 77 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
| 78 | Instruction::GOTO | 0x100, |
| 79 | Instruction::RETURN_VOID); |
| 80 | |
| 81 | TestCode(data); |
| 82 | } |
| 83 | |
| 84 | TEST(GraphChecker, CFG2) { |
| 85 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 86 | Instruction::CONST_4 | 0 | 0, |
| 87 | Instruction::IF_EQ, 3, |
| 88 | Instruction::GOTO | 0x100, |
| 89 | Instruction::RETURN_VOID); |
| 90 | |
| 91 | TestCode(data); |
| 92 | } |
| 93 | |
| 94 | TEST(GraphChecker, CFG3) { |
| 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::GOTO | 0xFF00); |
| 100 | |
| 101 | TestCode(data); |
| 102 | } |
| 103 | |
| 104 | // Test case with an invalid graph containing inconsistent |
| 105 | // predecessor/successor arcs in CFG. |
| 106 | TEST(GraphChecker, InconsistentPredecessorsAndSuccessors) { |
| 107 | ArenaPool pool; |
| 108 | ArenaAllocator allocator(&pool); |
| 109 | |
| 110 | HGraph* graph = CreateSimpleCFG(&allocator); |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 111 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 112 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 113 | ASSERT_TRUE(graph_checker.IsValid()); |
| 114 | |
| 115 | // Remove the entry block from the exit block's predecessors, to create an |
| 116 | // inconsistent successor/predecessor relation. |
| 117 | graph->GetExitBlock()->RemovePredecessor(graph->GetEntryBlock()); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 118 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 119 | ASSERT_FALSE(graph_checker.IsValid()); |
| 120 | } |
| 121 | |
| 122 | // Test case with an invalid graph containing a non-branch last |
| 123 | // instruction in a block. |
| 124 | TEST(GraphChecker, BlockEndingWithNonBranchInstruction) { |
| 125 | ArenaPool pool; |
| 126 | ArenaAllocator allocator(&pool); |
| 127 | |
| 128 | HGraph* graph = CreateSimpleCFG(&allocator); |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 129 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 130 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 131 | ASSERT_TRUE(graph_checker.IsValid()); |
| 132 | |
| 133 | // Remove the sole instruction of the exit block (composed of a |
| 134 | // single Exit instruction) to make it invalid (i.e. not ending by a |
| 135 | // branch instruction). |
| 136 | HBasicBlock* exit_block = graph->GetExitBlock(); |
| 137 | HInstruction* last_inst = exit_block->GetLastInstruction(); |
| 138 | exit_block->RemoveInstruction(last_inst); |
| 139 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 140 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 141 | ASSERT_FALSE(graph_checker.IsValid()); |
| 142 | } |
| 143 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 144 | class SSACheckerTest : public CommonCompilerTest {}; |
| 145 | |
| 146 | TEST_F(SSACheckerTest, SSAPhi) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 147 | // This code creates one Phi function during the conversion to SSA form. |
| 148 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 149 | Instruction::CONST_4 | 0 | 0, |
| 150 | Instruction::IF_EQ, 3, |
| 151 | Instruction::CONST_4 | 4 << 12 | 0, |
| 152 | Instruction::RETURN | 0 << 8); |
| 153 | |
| 154 | TestCodeSSA(data); |
| 155 | } |
| 156 | |
| 157 | } // namespace art |