blob: c6579dc5e0c97cd02b8352dd00904041b991f3f2 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080020#include "android-base/stringprintf.h"
21
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "nodes.h"
23
24namespace art {
25
26class HPrettyPrinter : public HGraphVisitor {
27 public:
28 explicit HPrettyPrinter(HGraph* graph) : HGraphVisitor(graph) { }
29
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030 void PrintPreInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031 PrintString(" ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000032 PrintInt(instruction->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033 PrintString(": ");
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034 }
35
Alexandre Rames2ed20af2015-03-06 13:55:35 +000036 void VisitInstruction(HInstruction* instruction) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010037 PrintPreInstruction(instruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038 PrintString(instruction->DebugName());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039 PrintPostInstruction(instruction);
40 }
41
42 void PrintPostInstruction(HInstruction* instruction) {
Vladimir Markoe9004912016-06-16 16:50:52 +010043 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +010044 if (!inputs.empty()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000045 PrintString("(");
46 bool first = true;
Vladimir Marko372f10e2016-05-17 16:30:10 +010047 for (const HInstruction* input : inputs) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000048 if (first) {
49 first = false;
50 } else {
51 PrintString(", ");
52 }
Vladimir Marko372f10e2016-05-17 16:30:10 +010053 PrintInt(input->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054 }
55 PrintString(")");
56 }
57 if (instruction->HasUses()) {
58 PrintString(" [");
59 bool first = true;
Vladimir Marko46817b82016-03-29 12:21:58 +010060 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000061 if (first) {
62 first = false;
63 } else {
64 PrintString(", ");
65 }
Vladimir Marko46817b82016-03-29 12:21:58 +010066 PrintInt(use.GetUser()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000067 }
68 PrintString("]");
69 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070 PrintNewLine();
71 }
72
Alexandre Rames2ed20af2015-03-06 13:55:35 +000073 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074 PrintString("BasicBlock ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075 PrintInt(block->GetBlockId());
Vladimir Marko60584552015-09-03 13:35:12 +000076 const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors();
77 if (!predecessors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000078 PrintString(", pred: ");
Vladimir Marko60584552015-09-03 13:35:12 +000079 for (size_t i = 0; i < predecessors.size() -1; i++) {
80 PrintInt(predecessors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 PrintString(", ");
82 }
Vladimir Marko60584552015-09-03 13:35:12 +000083 PrintInt(predecessors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084 }
Vladimir Marko60584552015-09-03 13:35:12 +000085 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
86 if (!successors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087 PrintString(", succ: ");
Vladimir Marko60584552015-09-03 13:35:12 +000088 for (size_t i = 0; i < successors.size() - 1; i++) {
89 PrintInt(successors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090 PrintString(", ");
91 }
Vladimir Marko60584552015-09-03 13:35:12 +000092 PrintInt(successors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000094 PrintNewLine();
95 HGraphVisitor::VisitBasicBlock(block);
96 }
97
98 virtual void PrintNewLine() = 0;
99 virtual void PrintInt(int value) = 0;
100 virtual void PrintString(const char* value) = 0;
101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter);
104};
105
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100106class StringPrettyPrinter : public HPrettyPrinter {
107 public:
108 explicit StringPrettyPrinter(HGraph* graph)
109 : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { }
110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void PrintInt(int value) OVERRIDE {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800112 str_ += android::base::StringPrintf("%d", value);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100113 }
114
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000115 void PrintString(const char* value) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100116 str_ += value;
117 }
118
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000119 void PrintNewLine() OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100120 str_ += '\n';
121 }
122
123 void Clear() { str_.clear(); }
124
125 std::string str() const { return str_; }
126
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000127 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100128 current_block_ = block;
129 HPrettyPrinter::VisitBasicBlock(block);
130 }
131
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000132 void VisitGoto(HGoto* gota) OVERRIDE {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100133 PrintString(" ");
134 PrintInt(gota->GetId());
135 PrintString(": Goto ");
Vladimir Markoec7802a2015-10-01 20:57:57 +0100136 PrintInt(current_block_->GetSuccessors()[0]->GetBlockId());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100137 PrintNewLine();
138 }
139
140 private:
141 std::string str_;
142 HBasicBlock* current_block_;
143
144 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
145};
146
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000147} // namespace art
148
149#endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_