blob: 08bfa5d80f3a83ca6c5ad379412c7543b127ff1e [file] [log] [blame]
Roland Levillainccc07a92014-09-16 14:48:16 +01001/*
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 Levillainccc07a92014-09-16 14:48:16 +010020namespace art {
21
Vladimir Markoca6fff82017-10-03 14:49:14 +010022class GraphCheckerTest : public OptimizingUnitTest {
23 protected:
24 HGraph* CreateSimpleCFG();
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080025 void TestCode(const std::vector<uint16_t>& data);
Vladimir Markoca6fff82017-10-03 14:49:14 +010026};
27
Roland Levillainccc07a92014-09-16 14:48:16 +010028/**
29 * Create a simple control-flow graph composed of two blocks:
30 *
31 * BasicBlock 0, succ: 1
David Brazdildbf5d752015-07-30 18:21:41 +010032 * 0: ReturnVoid 1
Roland Levillainccc07a92014-09-16 14:48:16 +010033 * BasicBlock 1, pred: 0
34 * 1: Exit
35 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010036HGraph* GraphCheckerTest::CreateSimpleCFG() {
37 HGraph* graph = CreateGraph();
38 HBasicBlock* entry_block = new (GetAllocator()) HBasicBlock(graph);
39 entry_block->AddInstruction(new (GetAllocator()) HReturnVoid());
Roland Levillainccc07a92014-09-16 14:48:16 +010040 graph->AddBlock(entry_block);
41 graph->SetEntryBlock(entry_block);
Vladimir Markoca6fff82017-10-03 14:49:14 +010042 HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph);
43 exit_block->AddInstruction(new (GetAllocator()) HExit());
Roland Levillainccc07a92014-09-16 14:48:16 +010044 graph->AddBlock(exit_block);
45 graph->SetExitBlock(exit_block);
46 entry_block->AddSuccessor(exit_block);
David Brazdil8e73ac32016-02-15 18:20:01 +000047 graph->BuildDominatorTree();
Roland Levillainccc07a92014-09-16 14:48:16 +010048 return graph;
49}
50
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080051void GraphCheckerTest::TestCode(const std::vector<uint16_t>& data) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010052 HGraph* graph = CreateCFG(data);
Roland Levillainccc07a92014-09-16 14:48:16 +010053 ASSERT_NE(graph, nullptr);
54
Vladimir Marko655e5852015-10-12 10:38:28 +010055 GraphChecker graph_checker(graph);
Roland Levillain633021e2014-10-01 14:12:25 +010056 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +010057 ASSERT_TRUE(graph_checker.IsValid());
58}
59
David Brazdilbadd8262016-02-02 16:28:56 +000060TEST_F(GraphCheckerTest, ReturnVoid) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080061 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Roland Levillainccc07a92014-09-16 14:48:16 +010062 Instruction::RETURN_VOID);
63
64 TestCode(data);
65}
66
David Brazdilbadd8262016-02-02 16:28:56 +000067TEST_F(GraphCheckerTest, CFG1) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080068 const std::vector<uint16_t> data = ZERO_REGISTER_CODE_ITEM(
Roland Levillainccc07a92014-09-16 14:48:16 +010069 Instruction::GOTO | 0x100,
70 Instruction::RETURN_VOID);
71
72 TestCode(data);
73}
74
David Brazdilbadd8262016-02-02 16:28:56 +000075TEST_F(GraphCheckerTest, CFG2) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080076 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Roland Levillainccc07a92014-09-16 14:48:16 +010077 Instruction::CONST_4 | 0 | 0,
78 Instruction::IF_EQ, 3,
79 Instruction::GOTO | 0x100,
80 Instruction::RETURN_VOID);
81
82 TestCode(data);
83}
84
David Brazdilbadd8262016-02-02 16:28:56 +000085TEST_F(GraphCheckerTest, CFG3) {
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -080086 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Roland Levillainccc07a92014-09-16 14:48:16 +010087 Instruction::CONST_4 | 0 | 0,
88 Instruction::IF_EQ, 3,
89 Instruction::GOTO | 0x100,
90 Instruction::GOTO | 0xFF00);
91
92 TestCode(data);
93}
94
95// Test case with an invalid graph containing inconsistent
96// predecessor/successor arcs in CFG.
David Brazdilbadd8262016-02-02 16:28:56 +000097TEST_F(GraphCheckerTest, InconsistentPredecessorsAndSuccessors) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010098 HGraph* graph = CreateSimpleCFG();
Vladimir Marko655e5852015-10-12 10:38:28 +010099 GraphChecker graph_checker(graph);
Roland Levillain633021e2014-10-01 14:12:25 +0100100 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +0100101 ASSERT_TRUE(graph_checker.IsValid());
102
103 // Remove the entry block from the exit block's predecessors, to create an
104 // inconsistent successor/predecessor relation.
105 graph->GetExitBlock()->RemovePredecessor(graph->GetEntryBlock());
Roland Levillain633021e2014-10-01 14:12:25 +0100106 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +0100107 ASSERT_FALSE(graph_checker.IsValid());
108}
109
110// Test case with an invalid graph containing a non-branch last
111// instruction in a block.
David Brazdilbadd8262016-02-02 16:28:56 +0000112TEST_F(GraphCheckerTest, BlockEndingWithNonBranchInstruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100113 HGraph* graph = CreateSimpleCFG();
Vladimir Marko655e5852015-10-12 10:38:28 +0100114 GraphChecker graph_checker(graph);
Roland Levillain633021e2014-10-01 14:12:25 +0100115 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +0100116 ASSERT_TRUE(graph_checker.IsValid());
117
118 // Remove the sole instruction of the exit block (composed of a
119 // single Exit instruction) to make it invalid (i.e. not ending by a
120 // branch instruction).
121 HBasicBlock* exit_block = graph->GetExitBlock();
122 HInstruction* last_inst = exit_block->GetLastInstruction();
123 exit_block->RemoveInstruction(last_inst);
124
Roland Levillain633021e2014-10-01 14:12:25 +0100125 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +0100126 ASSERT_FALSE(graph_checker.IsValid());
127}
128
David Brazdilbadd8262016-02-02 16:28:56 +0000129TEST_F(GraphCheckerTest, SSAPhi) {
Roland Levillainccc07a92014-09-16 14:48:16 +0100130 // This code creates one Phi function during the conversion to SSA form.
Mathieu Chartierfa3db3d2018-01-12 14:42:18 -0800131 const std::vector<uint16_t> data = ONE_REGISTER_CODE_ITEM(
Roland Levillainccc07a92014-09-16 14:48:16 +0100132 Instruction::CONST_4 | 0 | 0,
133 Instruction::IF_EQ, 3,
134 Instruction::CONST_4 | 4 << 12 | 0,
135 Instruction::RETURN | 0 << 8);
136
David Brazdilbadd8262016-02-02 16:28:56 +0000137 TestCode(data);
Roland Levillainccc07a92014-09-16 14:48:16 +0100138}
139
140} // namespace art