blob: 659141fbf48bc8a87869a1a423428160ab725370 [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
22/**
23 * Create a simple control-flow graph composed of two blocks:
24 *
25 * BasicBlock 0, succ: 1
David Brazdildbf5d752015-07-30 18:21:41 +010026 * 0: ReturnVoid 1
Roland Levillainccc07a92014-09-16 14:48:16 +010027 * BasicBlock 1, pred: 0
28 * 1: Exit
29 */
30HGraph* CreateSimpleCFG(ArenaAllocator* allocator) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010031 HGraph* graph = CreateGraph(allocator);
Roland Levillainccc07a92014-09-16 14:48:16 +010032 HBasicBlock* entry_block = new (allocator) HBasicBlock(graph);
David Brazdildbf5d752015-07-30 18:21:41 +010033 entry_block->AddInstruction(new (allocator) HReturnVoid());
Roland Levillainccc07a92014-09-16 14:48:16 +010034 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 Levillainccc07a92014-09-16 14:48:16 +010044static 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 Marko655e5852015-10-12 10:38:28 +010050 GraphChecker graph_checker(graph);
Roland Levillain633021e2014-10-01 14:12:25 +010051 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +010052 ASSERT_TRUE(graph_checker.IsValid());
53}
54
David Brazdilbadd8262016-02-02 16:28:56 +000055class GraphCheckerTest : public CommonCompilerTest {};
Roland Levillainccc07a92014-09-16 14:48:16 +010056
David Brazdilbadd8262016-02-02 16:28:56 +000057TEST_F(GraphCheckerTest, ReturnVoid) {
Roland Levillainccc07a92014-09-16 14:48:16 +010058 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
59 Instruction::RETURN_VOID);
60
61 TestCode(data);
62}
63
David Brazdilbadd8262016-02-02 16:28:56 +000064TEST_F(GraphCheckerTest, CFG1) {
Roland Levillainccc07a92014-09-16 14:48:16 +010065 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
66 Instruction::GOTO | 0x100,
67 Instruction::RETURN_VOID);
68
69 TestCode(data);
70}
71
David Brazdilbadd8262016-02-02 16:28:56 +000072TEST_F(GraphCheckerTest, CFG2) {
Roland Levillainccc07a92014-09-16 14:48:16 +010073 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
74 Instruction::CONST_4 | 0 | 0,
75 Instruction::IF_EQ, 3,
76 Instruction::GOTO | 0x100,
77 Instruction::RETURN_VOID);
78
79 TestCode(data);
80}
81
David Brazdilbadd8262016-02-02 16:28:56 +000082TEST_F(GraphCheckerTest, CFG3) {
Roland Levillainccc07a92014-09-16 14:48:16 +010083 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
84 Instruction::CONST_4 | 0 | 0,
85 Instruction::IF_EQ, 3,
86 Instruction::GOTO | 0x100,
87 Instruction::GOTO | 0xFF00);
88
89 TestCode(data);
90}
91
92// Test case with an invalid graph containing inconsistent
93// predecessor/successor arcs in CFG.
David Brazdilbadd8262016-02-02 16:28:56 +000094TEST_F(GraphCheckerTest, InconsistentPredecessorsAndSuccessors) {
Roland Levillainccc07a92014-09-16 14:48:16 +010095 ArenaPool pool;
96 ArenaAllocator allocator(&pool);
97
98 HGraph* graph = CreateSimpleCFG(&allocator);
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) {
Roland Levillainccc07a92014-09-16 14:48:16 +0100113 ArenaPool pool;
114 ArenaAllocator allocator(&pool);
115
116 HGraph* graph = CreateSimpleCFG(&allocator);
Vladimir Marko655e5852015-10-12 10:38:28 +0100117 GraphChecker graph_checker(graph);
Roland Levillain633021e2014-10-01 14:12:25 +0100118 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +0100119 ASSERT_TRUE(graph_checker.IsValid());
120
121 // Remove the sole instruction of the exit block (composed of a
122 // single Exit instruction) to make it invalid (i.e. not ending by a
123 // branch instruction).
124 HBasicBlock* exit_block = graph->GetExitBlock();
125 HInstruction* last_inst = exit_block->GetLastInstruction();
126 exit_block->RemoveInstruction(last_inst);
127
Roland Levillain633021e2014-10-01 14:12:25 +0100128 graph_checker.Run();
Roland Levillainccc07a92014-09-16 14:48:16 +0100129 ASSERT_FALSE(graph_checker.IsValid());
130}
131
David Brazdilbadd8262016-02-02 16:28:56 +0000132TEST_F(GraphCheckerTest, SSAPhi) {
Roland Levillainccc07a92014-09-16 14:48:16 +0100133 // This code creates one Phi function during the conversion to SSA form.
134 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
135 Instruction::CONST_4 | 0 | 0,
136 Instruction::IF_EQ, 3,
137 Instruction::CONST_4 | 4 << 12 | 0,
138 Instruction::RETURN | 0 << 8);
139
David Brazdilbadd8262016-02-02 16:28:56 +0000140 TestCode(data);
Roland Levillainccc07a92014-09-16 14:48:16 +0100141}
142
143} // namespace art