blob: a231a72f05b26b58fa983ed829f41c00d0ce6b9e [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"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000103 " 4: SuspendCheck\n"
104 " 5: 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"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000110 " 2: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 "BasicBlock 4, pred: 2\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000112 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000114 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000115 Instruction::GOTO | 0x200,
116 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000117 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000119 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000120
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000121 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000122 Instruction::GOTO_16, 3,
123 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000124 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000125
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000128 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000129 Instruction::GOTO_32, 4, 0,
130 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000131 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000132
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000133 TestCode(data3, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134}
135
136TEST(PrettyPrinterTest, CFG4) {
137 const char* expected =
138 "BasicBlock 0, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 " 3: SuspendCheck\n"
140 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 "BasicBlock 1, pred: 0, 1, succ: 1\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 " 0: SuspendCheck\n"
143 " 1: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000144 "BasicBlock 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148 Instruction::NOP,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000149 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000151 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000152
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000153 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
154 Instruction::GOTO_32, 0, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000156 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157}
158
159TEST(PrettyPrinterTest, CFG5) {
160 const char* expected =
161 "BasicBlock 0, succ: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000162 " 3: SuspendCheck\n"
163 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 "BasicBlock 1, pred: 0, 2, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000165 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000166 "BasicBlock 2, succ: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000167 " 1: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 "BasicBlock 3, pred: 1\n"
David Brazdil2fd6aa52015-02-02 18:58:27 +0000169 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000171 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000172 Instruction::RETURN_VOID,
173 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000174 Instruction::GOTO | 0xFE00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000176 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000177}
178
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000179TEST(PrettyPrinterTest, CFG6) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180 const char* expected =
181 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000182 " 0: Local [4, 3, 2]\n"
183 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184 " 10: SuspendCheck\n"
185 " 11: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000187 " 2: StoreLocal(0, 1)\n"
188 " 3: LoadLocal(0) [5]\n"
189 " 4: LoadLocal(0) [5]\n"
190 " 5: Equal(3, 4) [6]\n"
191 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000192 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000193 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 "BasicBlock 3, pred: 1, 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000195 " 8: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000197 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000199 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
200 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201 Instruction::IF_EQ, 3,
202 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000203 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000205 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206}
207
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000208TEST(PrettyPrinterTest, CFG7) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 const char* expected =
210 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000211 " 0: Local [4, 3, 2]\n"
212 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213 " 11: SuspendCheck\n"
214 " 12: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000215 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000216 " 2: StoreLocal(0, 1)\n"
217 " 3: LoadLocal(0) [5]\n"
218 " 4: LoadLocal(0) [5]\n"
219 " 5: Equal(3, 4) [6]\n"
220 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 "BasicBlock 2, pred: 1, 3, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000222 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223 "BasicBlock 3, pred: 1, 2, succ: 2\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000224 " 8: SuspendCheck\n"
225 " 9: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000226 "BasicBlock 4\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000227 " 10: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000228
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000229 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
230 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 Instruction::IF_EQ, 3,
232 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000233 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000235 TestCode(data, expected);
236}
237
238TEST(PrettyPrinterTest, IntConstant) {
239 const char* expected =
240 "BasicBlock 0, succ: 1\n"
241 " 0: Local [2]\n"
242 " 1: IntConstant [2]\n"
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000243 " 5: SuspendCheck\n"
244 " 6: Goto 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000245 "BasicBlock 1, pred: 0, succ: 2\n"
246 " 2: StoreLocal(0, 1)\n"
247 " 3: ReturnVoid\n"
248 "BasicBlock 2, pred: 1\n"
249 " 4: Exit\n";
250
251 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
252 Instruction::CONST_4 | 0 | 0,
253 Instruction::RETURN_VOID);
254
255 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000256}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000257} // namespace art