blob: c82d0cc6a4e30362dd538a5ef9f3fe4a0856e931 [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#ifndef ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_
18#define ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_
19
20#include "nodes.h"
21
22namespace art {
23
24class HPrettyPrinter : public HGraphVisitor {
25 public:
26 explicit HPrettyPrinter(HGraph* graph) : HGraphVisitor(graph) { }
27
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010028 void PrintPreInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029 PrintString(" ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000030 PrintInt(instruction->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000031 PrintString(": ");
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010032 }
33
34 virtual void VisitInstruction(HInstruction* instruction) {
35 PrintPreInstruction(instruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036 PrintString(instruction->DebugName());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010037 PrintPostInstruction(instruction);
38 }
39
40 void PrintPostInstruction(HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000041 if (instruction->InputCount() != 0) {
42 PrintString("(");
43 bool first = true;
44 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
45 if (first) {
46 first = false;
47 } else {
48 PrintString(", ");
49 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000050 PrintInt(it.Current()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051 }
52 PrintString(")");
53 }
54 if (instruction->HasUses()) {
55 PrintString(" [");
56 bool first = true;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057 for (HUseIterator<HInstruction> it(instruction->GetUses()); !it.Done(); it.Advance()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058 if (first) {
59 first = false;
60 } else {
61 PrintString(", ");
62 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063 PrintInt(it.Current()->GetUser()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000064 }
65 PrintString("]");
66 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067 PrintNewLine();
68 }
69
70 virtual void VisitBasicBlock(HBasicBlock* block) {
71 PrintString("BasicBlock ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072 PrintInt(block->GetBlockId());
73 const GrowableArray<HBasicBlock*>* blocks = block->GetPredecessors();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074 if (!blocks->IsEmpty()) {
75 PrintString(", pred: ");
76 for (size_t i = 0; i < blocks->Size() -1; i++) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000077 PrintInt(blocks->Get(i)->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000078 PrintString(", ");
79 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 PrintInt(blocks->Peek()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 blocks = block->GetSuccessors();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083 if (!blocks->IsEmpty()) {
84 PrintString(", succ: ");
85 for (size_t i = 0; i < blocks->Size() - 1; i++) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 PrintInt(blocks->Get(i)->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087 PrintString(", ");
88 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 PrintInt(blocks->Peek()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091 PrintNewLine();
92 HGraphVisitor::VisitBasicBlock(block);
93 }
94
95 virtual void PrintNewLine() = 0;
96 virtual void PrintInt(int value) = 0;
97 virtual void PrintString(const char* value) = 0;
98
99 private:
100 DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter);
101};
102
103} // namespace art
104
105#endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_