blob: da6b294d7110d5a615e83eea013a06abdd1e49e3 [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
17#include "base/stringprintf.h"
18#include "builder.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000019#include "dex_file.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000020#include "dex_instruction.h"
21#include "nodes.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000022#include "optimizing_unit_test.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "pretty_printer.h"
24#include "utils/arena_allocator.h"
25
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);
33 HGraphBuilder builder(&allocator);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000034 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
35 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036 ASSERT_NE(graph, nullptr);
37 StringPrettyPrinter printer(graph);
38 printer.VisitInsertionOrder();
39 ASSERT_STREQ(expected, printer.str().c_str());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040}
41
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000042TEST(PrettyPrinterTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
44 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045
46 const char* expected =
47 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000048 " 2: SuspendCheck\n"
49 " 3: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000050 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000052 "BasicBlock 2, pred: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000053 " 1: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000054
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000055 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000056}
57
58TEST(PrettyPrinterTest, CFG1) {
59 const char* expected =
60 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000061 " 3: SuspendCheck\n"
62 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000063 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000064 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000065 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000066 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000067 "BasicBlock 3, pred: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000068 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000069
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000070 const uint16_t data[] =
71 ZERO_REGISTER_CODE_ITEM(
72 Instruction::GOTO | 0x100,
73 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000075 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000076}
77
78TEST(PrettyPrinterTest, CFG2) {
79 const char* expected =
80 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000081 " 4: SuspendCheck\n"
82 " 5: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000084 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000086 " 1: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087 "BasicBlock 3, pred: 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000088 " 2: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000089 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000090 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000092 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 Instruction::GOTO | 0x100,
94 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000095 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000096
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000097 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000098}
99
100TEST(PrettyPrinterTest, CFG3) {
101 const char* expected =
102 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000103 " 5: SuspendCheck\n"
104 " 6: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000105 "BasicBlock 1, pred: 0, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 " 0: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000107 "BasicBlock 2, pred: 3, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000108 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000109 "BasicBlock 3, pred: 1, succ: 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000110 " 2: SuspendCheck\n"
111 " 3: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000112 "BasicBlock 4, pred: 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000113 " 4: 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"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163 " 4: SuspendCheck\n"
164 " 5: 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"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 " 1: SuspendCheck\n"
169 " 2: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 "BasicBlock 3, pred: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000172
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000173 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174 Instruction::RETURN_VOID,
175 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000176 Instruction::GOTO | 0xFE00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000178 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000179}
180
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000181TEST(PrettyPrinterTest, CFG6) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182 const char* expected =
183 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 " 0: Local [4, 3, 2]\n"
185 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 " 10: SuspendCheck\n"
187 " 11: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000188 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000189 " 2: StoreLocal(0, 1)\n"
190 " 3: LoadLocal(0) [5]\n"
191 " 4: LoadLocal(0) [5]\n"
192 " 5: Equal(3, 4) [6]\n"
193 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000195 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196 "BasicBlock 3, pred: 1, 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000197 " 8: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000199 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000201 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
202 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000203 Instruction::IF_EQ, 3,
204 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000205 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000208}
209
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000210TEST(PrettyPrinterTest, CFG7) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000211 const char* expected =
212 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000213 " 0: Local [4, 3, 2]\n"
214 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000215 " 11: SuspendCheck\n"
216 " 12: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000218 " 2: StoreLocal(0, 1)\n"
219 " 3: LoadLocal(0) [5]\n"
220 " 4: LoadLocal(0) [5]\n"
221 " 5: Equal(3, 4) [6]\n"
222 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223 "BasicBlock 2, pred: 1, 3, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000224 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000225 "BasicBlock 3, pred: 1, 2, succ: 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000226 " 8: SuspendCheck\n"
227 " 9: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000228 "BasicBlock 4\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000229 " 10: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000230
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000231 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
232 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000233 Instruction::IF_EQ, 3,
234 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000235 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000237 TestCode(data, expected);
238}
239
240TEST(PrettyPrinterTest, IntConstant) {
241 const char* expected =
242 "BasicBlock 0, succ: 1\n"
243 " 0: Local [2]\n"
244 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000245 " 5: SuspendCheck\n"
246 " 6: Goto 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000247 "BasicBlock 1, pred: 0, succ: 2\n"
248 " 2: StoreLocal(0, 1)\n"
249 " 3: ReturnVoid\n"
250 "BasicBlock 2, pred: 1\n"
251 " 4: Exit\n";
252
253 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
254 Instruction::CONST_4 | 0 | 0,
255 Instruction::RETURN_VOID);
256
257 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000259} // namespace art