blob: a7727c06a7b2ec3139fcff114f9a1e1ff5839204 [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());
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010073 const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors();
74 if (!predecessors.IsEmpty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075 PrintString(", pred: ");
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010076 for (size_t i = 0; i < predecessors.Size() -1; i++) {
77 PrintInt(predecessors.Get(i)->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000078 PrintString(", ");
79 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010080 PrintInt(predecessors.Peek()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010082 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
83 if (!successors.IsEmpty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084 PrintString(", succ: ");
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010085 for (size_t i = 0; i < successors.Size() - 1; i++) {
86 PrintInt(successors.Get(i)->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087 PrintString(", ");
88 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010089 PrintInt(successors.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
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100103class StringPrettyPrinter : public HPrettyPrinter {
104 public:
105 explicit StringPrettyPrinter(HGraph* graph)
106 : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { }
107
108 virtual void PrintInt(int value) {
109 str_ += StringPrintf("%d", value);
110 }
111
112 virtual void PrintString(const char* value) {
113 str_ += value;
114 }
115
116 virtual void PrintNewLine() {
117 str_ += '\n';
118 }
119
120 void Clear() { str_.clear(); }
121
122 std::string str() const { return str_; }
123
124 virtual void VisitBasicBlock(HBasicBlock* block) {
125 current_block_ = block;
126 HPrettyPrinter::VisitBasicBlock(block);
127 }
128
129 virtual void VisitGoto(HGoto* gota) {
130 PrintString(" ");
131 PrintInt(gota->GetId());
132 PrintString(": Goto ");
133 PrintInt(current_block_->GetSuccessors().Get(0)->GetBlockId());
134 PrintNewLine();
135 }
136
137 private:
138 std::string str_;
139 HBasicBlock* current_block_;
140
141 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
142};
143
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000144} // namespace art
145
146#endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_