blob: 04db7a6308e21d12fd3288ce057405868d1f8f7e [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 Geoffray818f2102014-02-18 16:43:35 +000030class StringPrettyPrinter : public HPrettyPrinter {
31 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000032 explicit StringPrettyPrinter(HGraph* graph)
33 : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35 virtual void PrintInt(int value) {
36 str_ += StringPrintf("%d", value);
37 }
38
39 virtual void PrintString(const char* value) {
40 str_ += value;
41 }
42
43 virtual void PrintNewLine() {
44 str_ += '\n';
45 }
46
47 void Clear() { str_.clear(); }
48
49 std::string str() const { return str_; }
50
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000051 virtual void VisitBasicBlock(HBasicBlock* block) {
52 current_block_ = block;
53 HPrettyPrinter::VisitBasicBlock(block);
54 }
55
56 virtual void VisitGoto(HGoto* gota) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000057 PrintString(" ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058 PrintInt(gota->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059 PrintString(": Goto ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060 PrintInt(current_block_->GetSuccessors()->Get(0)->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000061 PrintNewLine();
62 }
63
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064 private:
65 std::string str_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000066 HBasicBlock* current_block_;
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
69};
70
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000071static void TestCode(const uint16_t* data, const char* expected) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072 ArenaPool pool;
73 ArenaAllocator allocator(&pool);
74 HGraphBuilder builder(&allocator);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000075 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
76 HGraph* graph = builder.BuildGraph(*item);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077 ASSERT_NE(graph, nullptr);
78 StringPrettyPrinter printer(graph);
79 printer.VisitInsertionOrder();
80 ASSERT_STREQ(expected, printer.str().c_str());
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081}
82
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083TEST(PrettyPrinterTest, ReturnVoid) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000084 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
85 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086
87 const char* expected =
88 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000089 " 2: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000091 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092 "BasicBlock 2, pred: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000093 " 1: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000095 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000096}
97
98TEST(PrettyPrinterTest, CFG1) {
99 const char* expected =
100 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000101 " 3: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000102 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000103 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000104 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000105 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 "BasicBlock 3, pred: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000107 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000109 const uint16_t data[] =
110 ZERO_REGISTER_CODE_ITEM(
111 Instruction::GOTO | 0x100,
112 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000114 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000115}
116
117TEST(PrettyPrinterTest, CFG2) {
118 const char* expected =
119 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000121 "BasicBlock 1, pred: 0, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122 " 0: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000124 " 1: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000125 "BasicBlock 3, pred: 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126 " 2: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000128 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000130 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000131 Instruction::GOTO | 0x100,
132 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000133 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000135 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000136}
137
138TEST(PrettyPrinterTest, CFG3) {
139 const char* expected =
140 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000141 " 4: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000142 "BasicBlock 1, pred: 0, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000143 " 0: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000144 "BasicBlock 2, pred: 3, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000145 " 1: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146 "BasicBlock 3, pred: 1, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000147 " 2: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148 "BasicBlock 4, pred: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000149 " 3: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000151 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000152 Instruction::GOTO | 0x200,
153 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000154 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000156 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000158 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 Instruction::GOTO_16, 3,
160 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000161 Instruction::GOTO_16, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000163 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000165 const uint16_t data3[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000166 Instruction::GOTO_32, 4, 0,
167 Instruction::RETURN_VOID,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000168 Instruction::GOTO_32, 0xFFFF, 0xFFFF);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000169
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000170 TestCode(data3, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171}
172
173TEST(PrettyPrinterTest, CFG4) {
174 const char* expected =
175 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000176 " 2: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000177 "BasicBlock 1, pred: 0, 1, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000178 " 0: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000179 "BasicBlock 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000180 " 1: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000181
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000182 const uint16_t data1[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000183 Instruction::NOP,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000186 TestCode(data1, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000188 const uint16_t data2[] = ZERO_REGISTER_CODE_ITEM(
189 Instruction::GOTO_32, 0, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000190
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000191 TestCode(data2, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000192}
193
194TEST(PrettyPrinterTest, CFG5) {
195 const char* expected =
196 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000197 " 3: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 "BasicBlock 1, pred: 0, 2, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000199 " 0: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200 "BasicBlock 2, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000201 " 1: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202 "BasicBlock 3, pred: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000203 " 2: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000205 const uint16_t data[] = ZERO_REGISTER_CODE_ITEM(
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206 Instruction::RETURN_VOID,
207 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000208 Instruction::GOTO | 0xFE00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000210 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000211}
212
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000213TEST(PrettyPrinterTest, CFG6) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214 const char* expected =
215 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000216 " 0: Local [4, 3, 2]\n"
217 " 1: IntConstant [2]\n"
218 " 10: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000220 " 2: StoreLocal(0, 1)\n"
221 " 3: LoadLocal(0) [5]\n"
222 " 4: LoadLocal(0) [5]\n"
223 " 5: Equal(3, 4) [6]\n"
224 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000225 "BasicBlock 2, pred: 1, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000226 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 "BasicBlock 3, pred: 1, 2, succ: 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000228 " 8: ReturnVoid\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000229 "BasicBlock 4, pred: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000230 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000232 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
233 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234 Instruction::IF_EQ, 3,
235 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000236 Instruction::RETURN_VOID);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000237
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000238 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000239}
240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000241TEST(PrettyPrinterTest, CFG7) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242 const char* expected =
243 "BasicBlock 0, succ: 1\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000244 " 0: Local [4, 3, 2]\n"
245 " 1: IntConstant [2]\n"
246 " 10: Goto 1\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 "BasicBlock 1, pred: 0, succ: 3, 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000248 " 2: StoreLocal(0, 1)\n"
249 " 3: LoadLocal(0) [5]\n"
250 " 4: LoadLocal(0) [5]\n"
251 " 5: Equal(3, 4) [6]\n"
252 " 6: If(5)\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000253 "BasicBlock 2, pred: 1, 3, succ: 3\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000254 " 7: Goto 3\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 "BasicBlock 3, pred: 1, 2, succ: 2\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000256 " 8: Goto 2\n"
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257 "BasicBlock 4\n"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000258 " 9: Exit\n";
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000259
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000260 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
261 Instruction::CONST_4 | 0 | 0,
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 Instruction::IF_EQ, 3,
263 Instruction::GOTO | 0x100,
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000264 Instruction::GOTO | 0xFF00);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000265
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000266 TestCode(data, expected);
267}
268
269TEST(PrettyPrinterTest, IntConstant) {
270 const char* expected =
271 "BasicBlock 0, succ: 1\n"
272 " 0: Local [2]\n"
273 " 1: IntConstant [2]\n"
274 " 5: Goto 1\n"
275 "BasicBlock 1, pred: 0, succ: 2\n"
276 " 2: StoreLocal(0, 1)\n"
277 " 3: ReturnVoid\n"
278 "BasicBlock 2, pred: 1\n"
279 " 4: Exit\n";
280
281 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
282 Instruction::CONST_4 | 0 | 0,
283 Instruction::RETURN_VOID);
284
285 TestCode(data, expected);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287} // namespace art