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); |
David Brazdil | 8e73ac3 | 2016-02-15 18:20:01 +0000 | [diff] [blame] | 41 | graph->BuildDominatorTree(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 42 | return graph; |
| 43 | } |
| 44 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 45 | static void TestCode(const uint16_t* data) { |
| 46 | ArenaPool pool; |
| 47 | ArenaAllocator allocator(&pool); |
| 48 | HGraph* graph = CreateCFG(&allocator, data); |
| 49 | ASSERT_NE(graph, nullptr); |
| 50 | |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 51 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 52 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 53 | ASSERT_TRUE(graph_checker.IsValid()); |
| 54 | } |
| 55 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 56 | class GraphCheckerTest : public CommonCompilerTest {}; |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 57 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 58 | TEST_F(GraphCheckerTest, ReturnVoid) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 59 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
| 60 | Instruction::RETURN_VOID); |
| 61 | |
| 62 | TestCode(data); |
| 63 | } |
| 64 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 65 | TEST_F(GraphCheckerTest, CFG1) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 66 | const uint16_t data[] = ZERO_REGISTER_CODE_ITEM( |
| 67 | Instruction::GOTO | 0x100, |
| 68 | Instruction::RETURN_VOID); |
| 69 | |
| 70 | TestCode(data); |
| 71 | } |
| 72 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 73 | TEST_F(GraphCheckerTest, CFG2) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 74 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 75 | Instruction::CONST_4 | 0 | 0, |
| 76 | Instruction::IF_EQ, 3, |
| 77 | Instruction::GOTO | 0x100, |
| 78 | Instruction::RETURN_VOID); |
| 79 | |
| 80 | TestCode(data); |
| 81 | } |
| 82 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 83 | TEST_F(GraphCheckerTest, CFG3) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 84 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 85 | Instruction::CONST_4 | 0 | 0, |
| 86 | Instruction::IF_EQ, 3, |
| 87 | Instruction::GOTO | 0x100, |
| 88 | Instruction::GOTO | 0xFF00); |
| 89 | |
| 90 | TestCode(data); |
| 91 | } |
| 92 | |
| 93 | // Test case with an invalid graph containing inconsistent |
| 94 | // predecessor/successor arcs in CFG. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 95 | TEST_F(GraphCheckerTest, InconsistentPredecessorsAndSuccessors) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 96 | ArenaPool pool; |
| 97 | ArenaAllocator allocator(&pool); |
| 98 | |
| 99 | HGraph* graph = CreateSimpleCFG(&allocator); |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 100 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 101 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 102 | ASSERT_TRUE(graph_checker.IsValid()); |
| 103 | |
| 104 | // Remove the entry block from the exit block's predecessors, to create an |
| 105 | // inconsistent successor/predecessor relation. |
| 106 | graph->GetExitBlock()->RemovePredecessor(graph->GetEntryBlock()); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 107 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 108 | ASSERT_FALSE(graph_checker.IsValid()); |
| 109 | } |
| 110 | |
| 111 | // Test case with an invalid graph containing a non-branch last |
| 112 | // instruction in a block. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 113 | TEST_F(GraphCheckerTest, BlockEndingWithNonBranchInstruction) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 114 | ArenaPool pool; |
| 115 | ArenaAllocator allocator(&pool); |
| 116 | |
| 117 | HGraph* graph = CreateSimpleCFG(&allocator); |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 118 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 119 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 120 | ASSERT_TRUE(graph_checker.IsValid()); |
| 121 | |
| 122 | // Remove the sole instruction of the exit block (composed of a |
| 123 | // single Exit instruction) to make it invalid (i.e. not ending by a |
| 124 | // branch instruction). |
| 125 | HBasicBlock* exit_block = graph->GetExitBlock(); |
| 126 | HInstruction* last_inst = exit_block->GetLastInstruction(); |
| 127 | exit_block->RemoveInstruction(last_inst); |
| 128 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 129 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 130 | ASSERT_FALSE(graph_checker.IsValid()); |
| 131 | } |
| 132 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 133 | TEST_F(GraphCheckerTest, SSAPhi) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 134 | // This code creates one Phi function during the conversion to SSA form. |
| 135 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 136 | Instruction::CONST_4 | 0 | 0, |
| 137 | Instruction::IF_EQ, 3, |
| 138 | Instruction::CONST_4 | 4 << 12 | 0, |
| 139 | Instruction::RETURN | 0 << 8); |
| 140 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 141 | TestCode(data); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace art |