blob: ee32518c01b204a5b5eb15d9e478d5627a28fed3 [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
Ian Rogers576ca0c2014-06-06 15:58:22 -070020#include "base/stringprintf.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000021#include "nodes.h"
22
23namespace art {
24
25class HPrettyPrinter : public HGraphVisitor {
26 public:
27 explicit HPrettyPrinter(HGraph* graph) : HGraphVisitor(graph) { }
28
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010029 void PrintPreInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030 PrintString(" ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000031 PrintInt(instruction->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032 PrintString(": ");
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010033 }
34
Alexandre Rames2ed20af2015-03-06 13:55:35 +000035 void VisitInstruction(HInstruction* instruction) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036 PrintPreInstruction(instruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037 PrintString(instruction->DebugName());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010038 PrintPostInstruction(instruction);
39 }
40
41 void PrintPostInstruction(HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000042 if (instruction->InputCount() != 0) {
43 PrintString("(");
44 bool first = true;
45 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
46 if (first) {
47 first = false;
48 } else {
49 PrintString(", ");
50 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051 PrintInt(it.Current()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052 }
53 PrintString(")");
54 }
55 if (instruction->HasUses()) {
56 PrintString(" [");
57 bool first = true;
Vladimir Markod59f3b12016-03-29 12:21:58 +010058 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059 if (first) {
60 first = false;
61 } else {
62 PrintString(", ");
63 }
Vladimir Markod59f3b12016-03-29 12:21:58 +010064 PrintInt(use.GetUser()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000065 }
66 PrintString("]");
67 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068 PrintNewLine();
69 }
70
Alexandre Rames2ed20af2015-03-06 13:55:35 +000071 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072 PrintString("BasicBlock ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000073 PrintInt(block->GetBlockId());
Vladimir Marko60584552015-09-03 13:35:12 +000074 const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors();
75 if (!predecessors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000076 PrintString(", pred: ");
Vladimir Marko60584552015-09-03 13:35:12 +000077 for (size_t i = 0; i < predecessors.size() -1; i++) {
78 PrintInt(predecessors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000079 PrintString(", ");
80 }
Vladimir Marko60584552015-09-03 13:35:12 +000081 PrintInt(predecessors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082 }
Vladimir Marko60584552015-09-03 13:35:12 +000083 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
84 if (!successors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 PrintString(", succ: ");
Vladimir Marko60584552015-09-03 13:35:12 +000086 for (size_t i = 0; i < successors.size() - 1; i++) {
87 PrintInt(successors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 PrintString(", ");
89 }
Vladimir Marko60584552015-09-03 13:35:12 +000090 PrintInt(successors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 PrintNewLine();
93 HGraphVisitor::VisitBasicBlock(block);
94 }
95
96 virtual void PrintNewLine() = 0;
97 virtual void PrintInt(int value) = 0;
98 virtual void PrintString(const char* value) = 0;
99
100 private:
101 DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter);
102};
103
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100104class StringPrettyPrinter : public HPrettyPrinter {
105 public:
106 explicit StringPrettyPrinter(HGraph* graph)
107 : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { }
108
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000109 void PrintInt(int value) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100110 str_ += StringPrintf("%d", value);
111 }
112
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000113 void PrintString(const char* value) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100114 str_ += value;
115 }
116
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000117 void PrintNewLine() OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100118 str_ += '\n';
119 }
120
121 void Clear() { str_.clear(); }
122
123 std::string str() const { return str_; }
124
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000125 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100126 current_block_ = block;
127 HPrettyPrinter::VisitBasicBlock(block);
128 }
129
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000130 void VisitGoto(HGoto* gota) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100131 PrintString(" ");
132 PrintInt(gota->GetId());
133 PrintString(": Goto ");
Vladimir Markoec7802a2015-10-01 20:57:57 +0100134 PrintInt(current_block_->GetSuccessors()[0]->GetBlockId());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100135 PrintNewLine();
136 }
137
138 private:
139 std::string str_;
140 HBasicBlock* current_block_;
141
142 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
143};
144
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145} // namespace art
146
147#endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_