blob: 7e604e99b4e96bc6c2006a4a1a17779219bcfe3c [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 Geoffray3ff386a2014-03-04 14:46:47 +000048 " 2: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000049 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000050 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051 "BasicBlock 2, pred: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052 " 1: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000053
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000055}
56
57TEST(PrettyPrinterTest, CFG1) {
58 const char* expected =
59 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000060 " 3: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000061 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000062 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000063 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000064 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000065 "BasicBlock 3, pred: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000066 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000067
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000068 const uint16_t data[] =
69 ZERO_REGISTER_CODE_ITEM(
70 Instruction::GOTO | 0x100,
71 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074}
75
76TEST(PrettyPrinterTest, CFG2) {
77 const char* expected =
78 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000079 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000081 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000083 " 1: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084 "BasicBlock 3, pred: 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000085 " 2: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000087 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000089 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 Instruction::GOTO | 0x100,
91 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000092 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000094 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000095}
96
97TEST(PrettyPrinterTest, CFG3) {
98 const char* expected =
99 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000100 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000101 "BasicBlock 1, pred: 0, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000102 " 0: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000103 "BasicBlock 2, pred: 3, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000104 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000105 "BasicBlock 3, pred: 1, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 " 2: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000107 "BasicBlock 4, pred: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000108 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000109
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000110 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 Instruction::GOTO | 0x200,
112 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000114
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000115 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000116
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000117 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000118 Instruction::GOTO_16, 3,
119 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000124 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000125 Instruction::GOTO_32, 4, 0,
126 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000129 TestCode(data3, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000130}
131
132TEST(PrettyPrinterTest, CFG4) {
133 const char* expected =
134 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000135 " 2: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000136 "BasicBlock 1, pred: 0, 1, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000137 " 0: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000138 "BasicBlock 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000139 " 1: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000141 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000142 Instruction::NOP,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000144
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000145 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
148 Instruction::GOTO_32, 0, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000149
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000150 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000151}
152
153TEST(PrettyPrinterTest, CFG5) {
154 const char* expected =
155 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000156 " 3: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157 "BasicBlock 1, pred: 0, 2, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000158 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 "BasicBlock 2, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000160 " 1: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000161 "BasicBlock 3, pred: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000162 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000164 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 Instruction::RETURN_VOID,
166 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000167 Instruction::GOTO | 0xFE00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000169 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170}
171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000172TEST(PrettyPrinterTest, CFG6) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 const char* expected =
174 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000175 " 0: Local [4, 3, 2]\n"
176 " 1: IntConstant [2]\n"
177 " 10: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000178 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000179 " 2: StoreLocal(0, 1)\n"
180 " 3: LoadLocal(0) [5]\n"
181 " 4: LoadLocal(0) [5]\n"
182 " 5: Equal(3, 4) [6]\n"
183 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000184 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000185 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 "BasicBlock 3, pred: 1, 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000187 " 8: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000188 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000189 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000190
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000191 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
192 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 Instruction::IF_EQ, 3,
194 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000195 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000197 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198}
199
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000200TEST(PrettyPrinterTest, CFG7) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201 const char* expected =
202 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000203 " 0: Local [4, 3, 2]\n"
204 " 1: IntConstant [2]\n"
205 " 10: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207 " 2: StoreLocal(0, 1)\n"
208 " 3: LoadLocal(0) [5]\n"
209 " 4: LoadLocal(0) [5]\n"
210 " 5: Equal(3, 4) [6]\n"
211 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000212 "BasicBlock 2, pred: 1, 3, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000213 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214 "BasicBlock 3, pred: 1, 2, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000215 " 8: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216 "BasicBlock 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000217 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000218
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000219 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
220 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 Instruction::IF_EQ, 3,
222 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225 TestCode(data, expected);
226}
227
228TEST(PrettyPrinterTest, IntConstant) {
229 const char* expected =
230 "BasicBlock 0, succ: 1\n"
231 " 0: Local [2]\n"
232 " 1: IntConstant [2]\n"
233 " 5: Goto 1\n"
234 "BasicBlock 1, pred: 0, succ: 2\n"
235 " 2: StoreLocal(0, 1)\n"
236 " 3: ReturnVoid\n"
237 "BasicBlock 2, pred: 1\n"
238 " 4: Exit\n";
239
240 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
241 Instruction::CONST_4 | 0 | 0,
242 Instruction::RETURN_VOID);
243
244 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000245}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000246} // namespace art