blob: 5891350894f388fcede599efcfd0a1da1216a57c [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) {
Vladimir Markoe9004912016-06-16 16:50:52 +010042 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +010043 if (!inputs.empty()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000044 PrintString("(");
45 bool first = true;
Vladimir Marko372f10e2016-05-17 16:30:10 +010046 for (const HInstruction* input : inputs) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000047 if (first) {
48 first = false;
49 } else {
50 PrintString(", ");
51 }
Vladimir Marko372f10e2016-05-17 16:30:10 +010052 PrintInt(input->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000053 }
54 PrintString(")");
55 }
56 if (instruction->HasUses()) {
57 PrintString(" [");
58 bool first = true;
Vladimir Marko46817b82016-03-29 12:21:58 +010059 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000060 if (first) {
61 first = false;
62 } else {
63 PrintString(", ");
64 }
Vladimir Marko46817b82016-03-29 12:21:58 +010065 PrintInt(use.GetUser()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000066 }
67 PrintString("]");
68 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069 PrintNewLine();
70 }
71
Alexandre Rames2ed20af2015-03-06 13:55:35 +000072 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073 PrintString("BasicBlock ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000074 PrintInt(block->GetBlockId());
Vladimir Marko60584552015-09-03 13:35:12 +000075 const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors();
76 if (!predecessors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077 PrintString(", pred: ");
Vladimir Marko60584552015-09-03 13:35:12 +000078 for (size_t i = 0; i < predecessors.size() -1; i++) {
79 PrintInt(predecessors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080 PrintString(", ");
81 }
Vladimir Marko60584552015-09-03 13:35:12 +000082 PrintInt(predecessors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083 }
Vladimir Marko60584552015-09-03 13:35:12 +000084 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
85 if (!successors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 PrintString(", succ: ");
Vladimir Marko60584552015-09-03 13:35:12 +000087 for (size_t i = 0; i < successors.size() - 1; i++) {
88 PrintInt(successors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000089 PrintString(", ");
90 }
Vladimir Marko60584552015-09-03 13:35:12 +000091 PrintInt(successors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000092 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093 PrintNewLine();
94 HGraphVisitor::VisitBasicBlock(block);
95 }
96
97 virtual void PrintNewLine() = 0;
98 virtual void PrintInt(int value) = 0;
99 virtual void PrintString(const char* value) = 0;
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter);
103};
104
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100105class StringPrettyPrinter : public HPrettyPrinter {
106 public:
107 explicit StringPrettyPrinter(HGraph* graph)
108 : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { }
109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void PrintInt(int value) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100111 str_ += StringPrintf("%d", value);
112 }
113
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000114 void PrintString(const char* value) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100115 str_ += value;
116 }
117
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000118 void PrintNewLine() OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100119 str_ += '\n';
120 }
121
122 void Clear() { str_.clear(); }
123
124 std::string str() const { return str_; }
125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100127 current_block_ = block;
128 HPrettyPrinter::VisitBasicBlock(block);
129 }
130
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000131 void VisitGoto(HGoto* gota) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100132 PrintString(" ");
133 PrintInt(gota->GetId());
134 PrintString(": Goto ");
Vladimir Markoec7802a2015-10-01 20:57:57 +0100135 PrintInt(current_block_->GetSuccessors()[0]->GetBlockId());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100136 PrintNewLine();
137 }
138
139 private:
140 std::string str_;
141 HBasicBlock* current_block_;
142
143 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
144};
145
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146} // namespace art
147
148#endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_