blob: 7c302a9c96e44ae0aa7407ab55ba563ccd3294ac [file] [log] [blame]
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001/*
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#include "graph_visualizer.h"
18
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010019#include "code_generator.h"
David Brazdila4b8c212015-05-07 09:59:30 +010020#include "dead_code_elimination.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080021#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010022#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000023#include "optimization.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080024#include "register_allocator.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010025#include "ssa_liveness_analysis.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010026
David Brazdilc74652862015-05-13 17:50:09 +010027#include <cctype>
28#include <sstream>
29
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010030namespace art {
31
David Brazdilc74652862015-05-13 17:50:09 +010032static bool HasWhitespace(const char* str) {
33 DCHECK(str != nullptr);
34 while (str[0] != 0) {
35 if (isspace(str[0])) {
36 return true;
37 }
38 str++;
39 }
40 return false;
41}
42
43class StringList {
44 public:
David Brazdil294a0502015-05-15 16:44:05 +010045 enum Format {
46 kArrayBrackets,
47 kSetBrackets,
48 };
49
David Brazdilc74652862015-05-13 17:50:09 +010050 // Create an empty list
David Brazdil294a0502015-05-15 16:44:05 +010051 StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010052
53 // Construct StringList from a linked list. List element class T
54 // must provide methods `GetNext` and `Dump`.
55 template<class T>
David Brazdil294a0502015-05-15 16:44:05 +010056 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010057 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
58 current->Dump(NewEntryStream());
59 }
60 }
61
62 std::ostream& NewEntryStream() {
63 if (is_empty_) {
64 is_empty_ = false;
65 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010066 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010067 }
68 return sstream_;
69 }
70
71 private:
David Brazdil294a0502015-05-15 16:44:05 +010072 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010073 bool is_empty_;
74 std::ostringstream sstream_;
75
76 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
77};
78
79std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdil294a0502015-05-15 16:44:05 +010080 if (list.format_ == StringList::kArrayBrackets) {
81 return os << "[" << list.sstream_.str() << "]";
82 } else {
83 DCHECK_EQ(list.format_, StringList::kSetBrackets);
84 return os << "{" << list.sstream_.str() << "}";
85 }
David Brazdilc74652862015-05-13 17:50:09 +010086}
87
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010088/**
89 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
90 */
91class HGraphVisualizerPrinter : public HGraphVisitor {
92 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010093 HGraphVisualizerPrinter(HGraph* graph,
94 std::ostream& output,
95 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +000096 bool is_after_pass,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010097 const CodeGenerator& codegen)
98 : HGraphVisitor(graph),
99 output_(output),
100 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000101 is_after_pass_(is_after_pass),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100102 codegen_(codegen),
103 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100104
105 void StartTag(const char* name) {
106 AddIndent();
107 output_ << "begin_" << name << std::endl;
108 indent_++;
109 }
110
111 void EndTag(const char* name) {
112 indent_--;
113 AddIndent();
114 output_ << "end_" << name << std::endl;
115 }
116
117 void PrintProperty(const char* name, const char* property) {
118 AddIndent();
119 output_ << name << " \"" << property << "\"" << std::endl;
120 }
121
122 void PrintProperty(const char* name, const char* property, int id) {
123 AddIndent();
124 output_ << name << " \"" << property << id << "\"" << std::endl;
125 }
126
127 void PrintEmptyProperty(const char* name) {
128 AddIndent();
129 output_ << name << std::endl;
130 }
131
132 void PrintTime(const char* name) {
133 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800134 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100135 }
136
137 void PrintInt(const char* name, int value) {
138 AddIndent();
139 output_ << name << " " << value << std::endl;
140 }
141
142 void AddIndent() {
143 for (size_t i = 0; i < indent_; ++i) {
144 output_ << " ";
145 }
146 }
147
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100148 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100149 // Note that Primitive::Descriptor would not work for us
150 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100151 switch (type) {
152 case Primitive::kPrimBoolean: return 'z';
153 case Primitive::kPrimByte: return 'b';
154 case Primitive::kPrimChar: return 'c';
155 case Primitive::kPrimShort: return 's';
156 case Primitive::kPrimInt: return 'i';
157 case Primitive::kPrimLong: return 'j';
158 case Primitive::kPrimFloat: return 'f';
159 case Primitive::kPrimDouble: return 'd';
160 case Primitive::kPrimNot: return 'l';
161 case Primitive::kPrimVoid: return 'v';
162 }
163 LOG(FATAL) << "Unreachable";
164 return 'v';
165 }
166
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100167 void PrintPredecessors(HBasicBlock* block) {
168 AddIndent();
169 output_ << "predecessors";
170 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
171 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
172 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
173 }
174 output_<< std::endl;
175 }
176
177 void PrintSuccessors(HBasicBlock* block) {
178 AddIndent();
179 output_ << "successors";
180 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
181 HBasicBlock* successor = block->GetSuccessors().Get(i);
182 output_ << " \"B" << successor->GetBlockId() << "\" ";
183 }
184 output_<< std::endl;
185 }
186
David Brazdilc74652862015-05-13 17:50:09 +0100187 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100188 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100189 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100190 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100191 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100192 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100193 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100194 HConstant* constant = location.GetConstant();
195 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100196 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100197 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100198 stream << constant->AsLongConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100199 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100200 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100201 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100202 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100203 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000204 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100205 codegen_.DumpFloatingPointRegister(stream, location.low());
206 stream << "|";
207 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000208 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100209 codegen_.DumpCoreRegister(stream, location.low());
210 stream << "|";
211 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400212 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100213 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100214 } else {
215 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100216 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100217 }
218 }
219
David Brazdilc74652862015-05-13 17:50:09 +0100220 std::ostream& StartAttributeStream(const char* name = nullptr) {
221 if (name == nullptr) {
222 output_ << " ";
223 } else {
224 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
225 output_ << " " << name << ":";
226 }
227 return output_;
228 }
229
David Brazdilb7e4a062014-12-29 15:35:02 +0000230 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100231 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
232 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100233 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
234 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100235 std::ostream& str = moves.NewEntryStream();
236 DumpLocation(str, move->GetSource());
237 str << "->";
238 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100239 }
David Brazdilc74652862015-05-13 17:50:09 +0100240 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100241 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100242
David Brazdil36cf0952015-01-08 19:28:33 +0000243 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100244 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000245 }
246
David Brazdil36cf0952015-01-08 19:28:33 +0000247 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100248 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000249 }
250
David Brazdil36cf0952015-01-08 19:28:33 +0000251 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100252 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000253 }
254
David Brazdil36cf0952015-01-08 19:28:33 +0000255 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100256 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000257 }
258
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000259 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100260 StartAttributeStream("reg") << phi->GetRegNumber();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000261 }
262
Calin Juravle27df7582015-04-17 19:12:31 +0100263 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100264 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100265 }
266
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800267 bool IsPass(const char* name) {
268 return strcmp(pass_name_, name) == 0;
269 }
270
David Brazdilb7e4a062014-12-29 15:35:02 +0000271 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100272 output_ << instruction->DebugName();
273 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100274 StringList inputs;
275 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
276 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277 }
David Brazdilc74652862015-05-13 17:50:09 +0100278 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100279 }
David Brazdilc74652862015-05-13 17:50:09 +0100280 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800281 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100282 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 for (HEnvironment* environment = instruction->GetEnvironment();
284 environment != nullptr;
285 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100286 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100287 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
288 HInstruction* insn = environment->GetInstructionAt(i);
289 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100290 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100291 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100292 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800294 }
David Brazdilc74652862015-05-13 17:50:09 +0100295 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800296 }
David Brazdilc74652862015-05-13 17:50:09 +0100297 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800298 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800299 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000300 && is_after_pass_
301 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100302 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100303 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100304 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdil294a0502015-05-15 16:44:05 +0100305 StartAttributeStream("ranges")
306 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100307 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
308 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
309 StartAttributeStream("is_fixed") << interval->IsFixed();
310 StartAttributeStream("is_split") << interval->IsSplit();
311 StartAttributeStream("is_low") << interval->IsLowInterval();
312 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100313 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800314 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100315 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100316 LocationSummary* locations = instruction->GetLocations();
317 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100318 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100319 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100320 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100321 }
David Brazdilc74652862015-05-13 17:50:09 +0100322 std::ostream& attr = StartAttributeStream("locations");
323 attr << inputs << "->";
324 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100325 }
David Brazdila4b8c212015-05-07 09:59:30 +0100326 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
327 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000328 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
329 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100330 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000331 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100332 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000333 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100335 }
336
337 void PrintInstructions(const HInstructionList& list) {
338 const char* kEndInstructionMarker = "<|@";
339 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
340 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100341 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000342 size_t num_uses = 0;
343 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
344 !use_it.Done();
345 use_it.Advance()) {
346 ++num_uses;
347 }
348 AddIndent();
349 output_ << bci << " " << num_uses << " "
350 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000351 PrintInstruction(instruction);
David Brazdilc74652862015-05-13 17:50:09 +0100352 output_ << " " << kEndInstructionMarker << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100353 }
354 }
355
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100356 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100357 StartTag("cfg");
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000358 std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)");
359 PrintProperty("name", pass_desc.c_str());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100360 VisitInsertionOrder();
361 EndTag("cfg");
362 }
363
David Brazdilb7e4a062014-12-29 15:35:02 +0000364 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100365 StartTag("block");
366 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100367 if (block->GetLifetimeStart() != kNoLifetime) {
368 // Piggy back on these fields to show the lifetime of the block.
369 PrintInt("from_bci", block->GetLifetimeStart());
370 PrintInt("to_bci", block->GetLifetimeEnd());
371 } else {
372 PrintInt("from_bci", -1);
373 PrintInt("to_bci", -1);
374 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100375 PrintPredecessors(block);
376 PrintSuccessors(block);
377 PrintEmptyProperty("xhandlers");
378 PrintEmptyProperty("flags");
379 if (block->GetDominator() != nullptr) {
380 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
381 }
382
383 StartTag("states");
384 StartTag("locals");
385 PrintInt("size", 0);
386 PrintProperty("method", "None");
387 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
388 AddIndent();
389 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100390 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
391 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100392 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
393 output_ << inputs.Current()->GetId() << " ";
394 }
395 output_ << "]" << std::endl;
396 }
397 EndTag("locals");
398 EndTag("states");
399
400 StartTag("HIR");
401 PrintInstructions(block->GetPhis());
402 PrintInstructions(block->GetInstructions());
403 EndTag("HIR");
404 EndTag("block");
405 }
406
407 private:
408 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100409 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000410 const bool is_after_pass_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100411 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100412 size_t indent_;
413
414 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
415};
416
417HGraphVisualizer::HGraphVisualizer(std::ostream* output,
418 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100419 const CodeGenerator& codegen)
420 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100421
David Brazdil62e074f2015-04-07 18:09:37 +0100422void HGraphVisualizer::PrintHeader(const char* method_name) const {
423 DCHECK(output_ != nullptr);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000424 HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100425 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000426 printer.PrintProperty("name", method_name);
427 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100428 printer.PrintTime("date");
429 printer.EndTag("compilation");
430}
431
David Brazdilee690a32014-12-01 17:04:16 +0000432void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000433 DCHECK(output_ != nullptr);
434 if (!graph_->GetBlocks().IsEmpty()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000435 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000436 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100437 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100438}
439
440} // namespace art