blob: 77ddb97707bfc41d4c39d0112c4294daa962702c [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
Vladimír Marko434d9682022-11-04 14:04:17 +000022#include "base/macros.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "nodes.h"
24
Vladimír Marko434d9682022-11-04 14:04:17 +000025namespace art HIDDEN {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000026
27class HPrettyPrinter : public HGraphVisitor {
28 public:
29 explicit HPrettyPrinter(HGraph* graph) : HGraphVisitor(graph) { }
30
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010031 void PrintPreInstruction(HInstruction* instruction) {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032 PrintString(" ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000033 PrintInt(instruction->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000034 PrintString(": ");
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010035 }
36
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010037 void VisitInstruction(HInstruction* instruction) override {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010038 PrintPreInstruction(instruction);
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039 PrintString(instruction->DebugName());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010040 PrintPostInstruction(instruction);
41 }
42
43 void PrintPostInstruction(HInstruction* instruction) {
Vladimir Markoe9004912016-06-16 16:50:52 +010044 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +010045 if (!inputs.empty()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000046 PrintString("(");
47 bool first = true;
Vladimir Marko372f10e2016-05-17 16:30:10 +010048 for (const HInstruction* input : inputs) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000049 if (first) {
50 first = false;
51 } else {
52 PrintString(", ");
53 }
Vladimir Marko372f10e2016-05-17 16:30:10 +010054 PrintInt(input->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000055 }
56 PrintString(")");
57 }
58 if (instruction->HasUses()) {
59 PrintString(" [");
60 bool first = true;
Vladimir Marko46817b82016-03-29 12:21:58 +010061 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000062 if (first) {
63 first = false;
64 } else {
65 PrintString(", ");
66 }
Vladimir Marko46817b82016-03-29 12:21:58 +010067 PrintInt(use.GetUser()->GetId());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000068 }
69 PrintString("]");
70 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071 PrintNewLine();
72 }
73
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010074 void VisitBasicBlock(HBasicBlock* block) override {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075 PrintString("BasicBlock ");
Nicolas Geoffray787c3072014-03-17 10:20:19 +000076 PrintInt(block->GetBlockId());
Vladimir Marko60584552015-09-03 13:35:12 +000077 const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors();
78 if (!predecessors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000079 PrintString(", pred: ");
Vladimir Marko60584552015-09-03 13:35:12 +000080 for (size_t i = 0; i < predecessors.size() -1; i++) {
81 PrintInt(predecessors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082 PrintString(", ");
83 }
Vladimir Marko60584552015-09-03 13:35:12 +000084 PrintInt(predecessors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 }
Vladimir Marko60584552015-09-03 13:35:12 +000086 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
87 if (!successors.empty()) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000088 PrintString(", succ: ");
Vladimir Marko60584552015-09-03 13:35:12 +000089 for (size_t i = 0; i < successors.size() - 1; i++) {
90 PrintInt(successors[i]->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091 PrintString(", ");
92 }
Vladimir Marko60584552015-09-03 13:35:12 +000093 PrintInt(successors.back()->GetBlockId());
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095 PrintNewLine();
96 HGraphVisitor::VisitBasicBlock(block);
97 }
98
99 virtual void PrintNewLine() = 0;
100 virtual void PrintInt(int value) = 0;
101 virtual void PrintString(const char* value) = 0;
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter);
105};
106
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100107class StringPrettyPrinter : public HPrettyPrinter {
108 public:
109 explicit StringPrettyPrinter(HGraph* graph)
110 : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { }
111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100112 void PrintInt(int value) override {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800113 str_ += android::base::StringPrintf("%d", value);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100114 }
115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100116 void PrintString(const char* value) override {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100117 str_ += value;
118 }
119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100120 void PrintNewLine() override {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100121 str_ += '\n';
122 }
123
124 void Clear() { str_.clear(); }
125
126 std::string str() const { return str_; }
127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100128 void VisitBasicBlock(HBasicBlock* block) override {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100129 current_block_ = block;
130 HPrettyPrinter::VisitBasicBlock(block);
131 }
132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100133 void VisitGoto(HGoto* gota) override {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100134 PrintString(" ");
135 PrintInt(gota->GetId());
136 PrintString(": Goto ");
Vladimir Markoec7802a2015-10-01 20:57:57 +0100137 PrintInt(current_block_->GetSuccessors()[0]->GetBlockId());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100138 PrintNewLine();
139 }
140
141 private:
142 std::string str_;
143 HBasicBlock* current_block_;
144
145 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
146};
147
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000148} // namespace art
149
150#endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_