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 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 17 | #include "base/macros.h" |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 18 | #include "graph_checker.h" |
| 19 | #include "optimizing_unit_test.h" |
| 20 | |
Vladimír Marko | 434d968 | 2022-11-04 14:04:17 +0000 | [diff] [blame] | 21 | namespace art HIDDEN { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 22 | |
Santiago Aboy Solanes | 2c50b3a | 2023-02-10 10:25:31 +0000 | [diff] [blame] | 23 | class GraphCheckerTest : public CommonCompilerTest, public OptimizingUnitTestHelper { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 24 | protected: |
| 25 | HGraph* CreateSimpleCFG(); |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 26 | void TestCode(const std::vector<uint16_t>& data); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 29 | /** |
| 30 | * Create a simple control-flow graph composed of two blocks: |
| 31 | * |
| 32 | * BasicBlock 0, succ: 1 |
David Brazdil | dbf5d75 | 2015-07-30 18:21:41 +0100 | [diff] [blame] | 33 | * 0: ReturnVoid 1 |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 34 | * BasicBlock 1, pred: 0 |
| 35 | * 1: Exit |
| 36 | */ |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 37 | HGraph* GraphCheckerTest::CreateSimpleCFG() { |
| 38 | HGraph* graph = CreateGraph(); |
| 39 | HBasicBlock* entry_block = new (GetAllocator()) HBasicBlock(graph); |
| 40 | entry_block->AddInstruction(new (GetAllocator()) HReturnVoid()); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 41 | graph->AddBlock(entry_block); |
| 42 | graph->SetEntryBlock(entry_block); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 43 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph); |
| 44 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 45 | graph->AddBlock(exit_block); |
| 46 | graph->SetExitBlock(exit_block); |
| 47 | entry_block->AddSuccessor(exit_block); |
David Brazdil | 8e73ac3 | 2016-02-15 18:20:01 +0000 | [diff] [blame] | 48 | graph->BuildDominatorTree(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 49 | return graph; |
| 50 | } |
| 51 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 52 | void GraphCheckerTest::TestCode(const std::vector<uint16_t>& data) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 53 | HGraph* graph = CreateCFG(data); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 54 | ASSERT_NE(graph, nullptr); |
| 55 | |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 56 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 57 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 58 | ASSERT_TRUE(graph_checker.IsValid()); |
| 59 | } |
| 60 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 61 | TEST_F(GraphCheckerTest, ReturnVoid) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 62 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 63 | Instruction::RETURN_VOID); |
| 64 | |
| 65 | TestCode(data); |
| 66 | } |
| 67 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 68 | TEST_F(GraphCheckerTest, CFG1) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 69 | const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM( |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 70 | Instruction::GOTO | 0x100, |
| 71 | Instruction::RETURN_VOID); |
| 72 | |
| 73 | TestCode(data); |
| 74 | } |
| 75 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 76 | TEST_F(GraphCheckerTest, CFG2) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 77 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 78 | Instruction::CONST_4 | 0 | 0, |
| 79 | Instruction::IF_EQ, 3, |
| 80 | Instruction::GOTO | 0x100, |
| 81 | Instruction::RETURN_VOID); |
| 82 | |
| 83 | TestCode(data); |
| 84 | } |
| 85 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 86 | TEST_F(GraphCheckerTest, CFG3) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 87 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 88 | Instruction::CONST_4 | 0 | 0, |
| 89 | Instruction::IF_EQ, 3, |
| 90 | Instruction::GOTO | 0x100, |
| 91 | Instruction::GOTO | 0xFF00); |
| 92 | |
| 93 | TestCode(data); |
| 94 | } |
| 95 | |
| 96 | // Test case with an invalid graph containing inconsistent |
| 97 | // predecessor/successor arcs in CFG. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 98 | TEST_F(GraphCheckerTest, InconsistentPredecessorsAndSuccessors) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 99 | HGraph* graph = CreateSimpleCFG(); |
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) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 114 | HGraph* graph = CreateSimpleCFG(); |
Vladimir Marko | 655e585 | 2015-10-12 10:38:28 +0100 | [diff] [blame] | 115 | GraphChecker graph_checker(graph); |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 116 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 117 | ASSERT_TRUE(graph_checker.IsValid()); |
| 118 | |
| 119 | // Remove the sole instruction of the exit block (composed of a |
| 120 | // single Exit instruction) to make it invalid (i.e. not ending by a |
| 121 | // branch instruction). |
| 122 | HBasicBlock* exit_block = graph->GetExitBlock(); |
| 123 | HInstruction* last_inst = exit_block->GetLastInstruction(); |
| 124 | exit_block->RemoveInstruction(last_inst); |
| 125 | |
Roland Levillain | 633021e | 2014-10-01 14:12:25 +0100 | [diff] [blame] | 126 | graph_checker.Run(); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 127 | ASSERT_FALSE(graph_checker.IsValid()); |
| 128 | } |
| 129 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 130 | TEST_F(GraphCheckerTest, SSAPhi) { |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 131 | // This code creates one Phi function during the conversion to SSA form. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 132 | const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM( |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 133 | Instruction::CONST_4 | 0 | 0, |
| 134 | Instruction::IF_EQ, 3, |
| 135 | Instruction::CONST_4 | 4 << 12 | 0, |
| 136 | Instruction::RETURN | 0 << 8); |
| 137 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 138 | TestCode(data); |
Roland Levillain | ccc07a9 | 2014-09-16 14:48:16 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace art |