blob: c56100dfa1e9496a90de6f278a5385c5b2ca3c6f [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000018#include "base/stringprintf.h"
19#include "builder.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000021#include "dex_instruction.h"
22#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "optimizing_unit_test.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000024#include "pretty_printer.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025
26#include "gtest/gtest.h"
27
28namespace art {
29
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000030static void TestCode(const uint16_t* data, const char* expected) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031 ArenaPool pool;
32 ArenaAllocator allocator(&pool);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010033 HGraph* graph = CreateGraph(&allocator);
David Brazdil5e8b1372015-01-23 14:39:08 +000034 HGraphBuilder builder(graph);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000035 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000036 bool graph_built = builder.BuildGraph(*item);
37 ASSERT_TRUE(graph_built);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038 StringPrettyPrinter printer(graph);
39 printer.VisitInsertionOrder();
40 ASSERT_STREQ(expected, printer.str().c_str());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041}
42
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043TEST(PrettyPrinterTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
45 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000046
47 const char* expected =
48 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000049 " 2: SuspendCheck\n"
50 " 3: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000053 "BasicBlock 2, pred: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054 " 1: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057}
58
59TEST(PrettyPrinterTest, CFG1) {
60 const char* expected =
61 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000062 " 3: SuspendCheck\n"
63 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000064 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000065 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000066 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000067 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000068 "BasicBlock 3, pred: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000069 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000070
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000071 const uint16_t data[] =
72 ZERO_REGISTER_CODE_ITEM(
73 Instruction::GOTO | 0x100,
74 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000076 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077}
78
79TEST(PrettyPrinterTest, CFG2) {
80 const char* expected =
81 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000082 " 4: SuspendCheck\n"
83 " 5: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000085 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000087 " 1: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 "BasicBlock 3, pred: 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000089 " 2: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000091 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000093 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 Instruction::GOTO | 0x100,
95 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000096 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000097
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000098 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000099}
100
101TEST(PrettyPrinterTest, CFG3) {
102 const char* expected =
103 "BasicBlock 0, succ: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000104 " 4: SuspendCheck\n"
105 " 5: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 "BasicBlock 1, pred: 0, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 " 0: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 "BasicBlock 2, pred: 3, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000109 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000110 "BasicBlock 3, pred: 1, succ: 2\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000111 " 2: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000112 "BasicBlock 4, pred: 2\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000113 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000114
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000115 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000116 Instruction::GOTO | 0x200,
117 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 Instruction::GOTO_16, 3,
124 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000125 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000129 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000130 Instruction::GOTO_32, 4, 0,
131 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000132 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000133
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000134 TestCode(data3, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000135}
136
137TEST(PrettyPrinterTest, CFG4) {
138 const char* expected =
139 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 " 3: SuspendCheck\n"
141 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000142 "BasicBlock 1, pred: 0, 1, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 " 0: SuspendCheck\n"
144 " 1: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000145 "BasicBlock 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000147
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000148 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000149 Instruction::NOP,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000150 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000151
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000152 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000153
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000154 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
155 Instruction::GOTO_32, 0, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000157 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000158}
159
160TEST(PrettyPrinterTest, CFG5) {
161 const char* expected =
162 "BasicBlock 0, succ: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000163 " 3: SuspendCheck\n"
164 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 "BasicBlock 1, pred: 0, 2, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000166 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000167 "BasicBlock 2, succ: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000168 " 1: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000169 "BasicBlock 3, pred: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000170 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 Instruction::RETURN_VOID,
174 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000175 Instruction::GOTO | 0xFE00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000177 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000178}
179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000180TEST(PrettyPrinterTest, CFG6) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000181 const char* expected =
182 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000183 " 0: Local [4, 3, 2]\n"
184 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 " 10: SuspendCheck\n"
186 " 11: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000187 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000188 " 2: StoreLocal(0, 1)\n"
189 " 3: LoadLocal(0) [5]\n"
190 " 4: LoadLocal(0) [5]\n"
191 " 5: Equal(3, 4) [6]\n"
192 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000194 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000195 "BasicBlock 3, pred: 1, 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 " 8: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000200 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
201 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202 Instruction::IF_EQ, 3,
203 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000204 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000206 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207}
208
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000209TEST(PrettyPrinterTest, CFG7) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000210 const char* expected =
211 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000212 " 0: Local [4, 3, 2]\n"
213 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000214 " 11: SuspendCheck\n"
215 " 12: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000217 " 2: StoreLocal(0, 1)\n"
218 " 3: LoadLocal(0) [5]\n"
219 " 4: LoadLocal(0) [5]\n"
220 " 5: Equal(3, 4) [6]\n"
221 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 "BasicBlock 2, pred: 1, 3, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224 "BasicBlock 3, pred: 1, 2, succ: 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000225 " 8: SuspendCheck\n"
226 " 9: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 "BasicBlock 4\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000228 " 10: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000229
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000230 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
231 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000232 Instruction::IF_EQ, 3,
233 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000234 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000236 TestCode(data, expected);
237}
238
239TEST(PrettyPrinterTest, IntConstant) {
240 const char* expected =
241 "BasicBlock 0, succ: 1\n"
242 " 0: Local [2]\n"
243 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000244 " 5: SuspendCheck\n"
245 " 6: Goto 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000246 "BasicBlock 1, pred: 0, succ: 2\n"
247 " 2: StoreLocal(0, 1)\n"
248 " 3: ReturnVoid\n"
249 "BasicBlock 2, pred: 1\n"
250 " 4: Exit\n";
251
252 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
253 Instruction::CONST_4 | 0 | 0,
254 Instruction::RETURN_VOID);
255
256 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000258} // namespace art