blob: 9efc13f61bd00b642cc0d508fcf49f82d242a433 [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
Alexandre Rameseb7b7392015-06-19 14:47:01 +010019#include <dlfcn.h>
20
21#include <cctype>
22#include <sstream>
23
Aart Bik09e8d5f2016-01-22 16:49:55 -080024#include "bounds_check_elimination.h"
David Brazdilbadd8262016-02-02 16:28:56 +000025#include "builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010026#include "code_generator.h"
David Brazdila4b8c212015-05-07 09:59:30 +010027#include "dead_code_elimination.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010028#include "disassembler.h"
Calin Juravlecdfed3d2015-10-26 14:05:01 +000029#include "inliner.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080030#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010031#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000032#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010033#include "reference_type_propagation.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080034#include "register_allocator.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010035#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010036#include "utils/assembler.h"
David Brazdilc74652862015-05-13 17:50:09 +010037
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010038namespace art {
39
David Brazdilc74652862015-05-13 17:50:09 +010040static bool HasWhitespace(const char* str) {
41 DCHECK(str != nullptr);
42 while (str[0] != 0) {
43 if (isspace(str[0])) {
44 return true;
45 }
46 str++;
47 }
48 return false;
49}
50
51class StringList {
52 public:
David Brazdilc7a24852015-05-15 16:44:05 +010053 enum Format {
54 kArrayBrackets,
55 kSetBrackets,
56 };
57
David Brazdilc74652862015-05-13 17:50:09 +010058 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010059 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010060
61 // Construct StringList from a linked list. List element class T
62 // must provide methods `GetNext` and `Dump`.
63 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010064 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010065 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
66 current->Dump(NewEntryStream());
67 }
68 }
69
70 std::ostream& NewEntryStream() {
71 if (is_empty_) {
72 is_empty_ = false;
73 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010074 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010075 }
76 return sstream_;
77 }
78
79 private:
David Brazdilc7a24852015-05-15 16:44:05 +010080 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010081 bool is_empty_;
82 std::ostringstream sstream_;
83
84 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
85};
86
87std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010088 switch (list.format_) {
89 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
90 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
91 default:
92 LOG(FATAL) << "Invalid StringList format";
93 UNREACHABLE();
94 }
David Brazdilc74652862015-05-13 17:50:09 +010095}
96
Alexandre Rameseb7b7392015-06-19 14:47:01 +010097typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
98 DisassemblerOptions* options);
99class HGraphVisualizerDisassembler {
100 public:
101 HGraphVisualizerDisassembler(InstructionSet instruction_set, const uint8_t* base_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100102 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100103 libart_disassembler_handle_ =
104 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
105 if (libart_disassembler_handle_ == nullptr) {
106 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
107 return;
108 }
109 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
110 dlsym(libart_disassembler_handle_, "create_disassembler"));
111 if (create_disassembler == nullptr) {
112 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
113 return;
114 }
115 // Reading the disassembly from 0x0 is easier, so we print relative
116 // addresses. We will only disassemble the code once everything has
117 // been generated, so we can read data in literal pools.
118 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
119 instruction_set,
120 new DisassemblerOptions(/* absolute_addresses */ false,
121 base_address,
122 /* can_read_literals */ true)));
123 }
124
125 ~HGraphVisualizerDisassembler() {
126 // We need to call ~Disassembler() before we close the library.
127 disassembler_.reset();
128 if (libart_disassembler_handle_ != nullptr) {
129 dlclose(libart_disassembler_handle_);
130 }
131 }
132
133 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100134 if (disassembler_ == nullptr) {
135 return;
136 }
137
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100138 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
139 if (instruction_set_ == kThumb2) {
140 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
141 // address is used to distinguish between the two.
142 base += 1;
143 }
144 disassembler_->Dump(output, base + start, base + end);
145 }
146
147 private:
148 InstructionSet instruction_set_;
149 std::unique_ptr<Disassembler> disassembler_;
150
151 void* libart_disassembler_handle_;
152};
153
154
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100155/**
156 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
157 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100158class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100159 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100160 HGraphVisualizerPrinter(HGraph* graph,
161 std::ostream& output,
162 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000163 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100164 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100165 const CodeGenerator& codegen,
166 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100167 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100168 output_(output),
169 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000170 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100171 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100172 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100173 disasm_info_(disasm_info),
174 disassembler_(disasm_info_ != nullptr
175 ? new HGraphVisualizerDisassembler(
176 codegen_.GetInstructionSet(),
177 codegen_.GetAssembler().CodeBufferBaseAddress())
178 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100179 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100180
David Brazdilfa02c9d2016-03-30 09:41:02 +0100181 void Flush() {
182 // We use "\n" instead of std::endl to avoid implicit flushing which
183 // generates too many syscalls during debug-GC tests (b/27826765).
184 output_ << std::flush;
185 }
186
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100187 void StartTag(const char* name) {
188 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100189 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100190 indent_++;
191 }
192
193 void EndTag(const char* name) {
194 indent_--;
195 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100196 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100197 }
198
199 void PrintProperty(const char* name, const char* property) {
200 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100201 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100202 }
203
204 void PrintProperty(const char* name, const char* property, int id) {
205 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100206 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100207 }
208
209 void PrintEmptyProperty(const char* name) {
210 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100211 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100212 }
213
214 void PrintTime(const char* name) {
215 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100216 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100217 }
218
219 void PrintInt(const char* name, int value) {
220 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100221 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100222 }
223
224 void AddIndent() {
225 for (size_t i = 0; i < indent_; ++i) {
226 output_ << " ";
227 }
228 }
229
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100230 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100231 // Note that Primitive::Descriptor would not work for us
232 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100233 switch (type) {
234 case Primitive::kPrimBoolean: return 'z';
235 case Primitive::kPrimByte: return 'b';
236 case Primitive::kPrimChar: return 'c';
237 case Primitive::kPrimShort: return 's';
238 case Primitive::kPrimInt: return 'i';
239 case Primitive::kPrimLong: return 'j';
240 case Primitive::kPrimFloat: return 'f';
241 case Primitive::kPrimDouble: return 'd';
242 case Primitive::kPrimNot: return 'l';
243 case Primitive::kPrimVoid: return 'v';
244 }
245 LOG(FATAL) << "Unreachable";
246 return 'v';
247 }
248
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100249 void PrintPredecessors(HBasicBlock* block) {
250 AddIndent();
251 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000252 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100253 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
254 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100255 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
256 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
257 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100258 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100259 }
260
261 void PrintSuccessors(HBasicBlock* block) {
262 AddIndent();
263 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000264 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100265 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000266 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100267 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000268 }
269
270 void PrintExceptionHandlers(HBasicBlock* block) {
271 AddIndent();
272 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000273 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100274 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100275 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100276 if (block->IsExitBlock() &&
277 (disasm_info_ != nullptr) &&
278 !disasm_info_->GetSlowPathIntervals().empty()) {
279 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
280 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100281 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100282 }
283
David Brazdilc74652862015-05-13 17:50:09 +0100284 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100285 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100286 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100287 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100288 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100289 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100290 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100291 HConstant* constant = location.GetConstant();
292 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100293 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100294 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100295 stream << constant->AsLongConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100296 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100297 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100298 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100299 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100300 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000301 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100302 codegen_.DumpFloatingPointRegister(stream, location.low());
303 stream << "|";
304 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000305 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100306 codegen_.DumpCoreRegister(stream, location.low());
307 stream << "|";
308 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400309 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100310 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100311 } else {
312 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100313 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100314 }
315 }
316
David Brazdilc74652862015-05-13 17:50:09 +0100317 std::ostream& StartAttributeStream(const char* name = nullptr) {
318 if (name == nullptr) {
319 output_ << " ";
320 } else {
321 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
322 output_ << " " << name << ":";
323 }
324 return output_;
325 }
326
David Brazdilb7e4a062014-12-29 15:35:02 +0000327 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100328 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
329 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100330 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
331 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100332 std::ostream& str = moves.NewEntryStream();
333 DumpLocation(str, move->GetSource());
334 str << "->";
335 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100336 }
David Brazdilc74652862015-05-13 17:50:09 +0100337 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100338 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100339
David Brazdil36cf0952015-01-08 19:28:33 +0000340 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100341 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000342 }
343
David Brazdil36cf0952015-01-08 19:28:33 +0000344 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100345 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000346 }
347
David Brazdil36cf0952015-01-08 19:28:33 +0000348 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100349 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000350 }
351
David Brazdil36cf0952015-01-08 19:28:33 +0000352 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100353 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000354 }
355
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000356 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100357 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100358 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000359 }
360
Calin Juravle27df7582015-04-17 19:12:31 +0100361 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100362 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100363 }
364
David Brazdilbff75032015-07-08 17:26:51 +0000365 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
366 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
367 }
368
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100369 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Calin Juravle0ba218d2015-05-19 18:46:01 +0100370 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100371 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100372 StartAttributeStream("needs_access_check") << std::boolalpha
373 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100374 }
375
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000376 void VisitLoadString(HLoadString* load_string) OVERRIDE {
377 StartAttributeStream("load_kind") << load_string->GetLoadKind();
378 }
379
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100380 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000381 StartAttributeStream("check_kind") << check_cast->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100382 StartAttributeStream("must_do_null_check") << std::boolalpha
383 << check_cast->MustDoNullCheck() << std::noboolalpha;
384 }
385
386 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000387 StartAttributeStream("check_kind") << instance_of->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100388 StartAttributeStream("must_do_null_check") << std::boolalpha
389 << instance_of->MustDoNullCheck() << std::noboolalpha;
390 }
391
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100392 void VisitArraySet(HArraySet* array_set) OVERRIDE {
393 StartAttributeStream("value_can_be_null") << std::boolalpha
394 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000395 StartAttributeStream("needs_type_check") << std::boolalpha
396 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100397 }
398
Roland Levillain31dd3d62016-02-16 12:21:02 +0000399 void VisitCompare(HCompare* compare) OVERRIDE {
400 ComparisonBias bias = compare->GetBias();
401 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
402 ? "gt"
403 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
404 }
405
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100406 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100407 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray242febb2015-07-01 16:10:44 +0100408 StartAttributeStream("method_name") << PrettyMethod(
409 invoke->GetDexMethodIndex(), GetGraph()->GetDexFile(), /* with_signature */ false);
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100410 }
411
Calin Juravle175dc732015-08-25 15:42:32 +0100412 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
413 VisitInvoke(invoke);
414 StartAttributeStream("invoke_type") << invoke->GetOriginalInvokeType();
415 }
416
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100417 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
418 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000419 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100420 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000421 if (invoke->IsStatic()) {
422 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
423 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100424 }
425
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000426 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
427 VisitInvoke(invoke);
428 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
429 }
430
David Brazdil11edec72016-03-24 12:40:52 +0000431 void VisitInstanceFieldGet(HInstanceFieldGet* iget) OVERRIDE {
432 StartAttributeStream("field_name") << PrettyField(iget->GetFieldInfo().GetFieldIndex(),
433 iget->GetFieldInfo().GetDexFile(),
434 /* with type */ false);
435 StartAttributeStream("field_type") << iget->GetFieldType();
436 }
437
438 void VisitInstanceFieldSet(HInstanceFieldSet* iset) OVERRIDE {
439 StartAttributeStream("field_name") << PrettyField(iset->GetFieldInfo().GetFieldIndex(),
440 iset->GetFieldInfo().GetDexFile(),
441 /* with type */ false);
442 StartAttributeStream("field_type") << iset->GetFieldType();
443 }
444
Calin Juravlee460d1d2015-09-29 04:52:17 +0100445 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
446 StartAttributeStream("field_type") << field_access->GetFieldType();
447 }
448
449 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
450 StartAttributeStream("field_type") << field_access->GetFieldType();
451 }
452
453 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
454 StartAttributeStream("field_type") << field_access->GetFieldType();
455 }
456
457 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
458 StartAttributeStream("field_type") << field_access->GetFieldType();
459 }
460
David Brazdilfc6a86a2015-06-26 10:33:45 +0000461 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100462 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000463 }
464
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300465#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
466 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) OVERRIDE {
467 StartAttributeStream("kind") << instruction->GetOpKind();
468 }
Artem Serov7fc63502016-02-09 17:15:29 +0000469
470 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) OVERRIDE {
471 StartAttributeStream("kind") << instruction->GetOpKind();
472 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300473#endif
474
Alexandre Rames418318f2015-11-20 15:55:47 +0000475#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames8626b742015-11-25 16:28:08 +0000476 void VisitArm64DataProcWithShifterOp(HArm64DataProcWithShifterOp* instruction) OVERRIDE {
477 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
478 if (HArm64DataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
479 StartAttributeStream("shift") << instruction->GetShiftAmount();
480 }
481 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000482#endif
483
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800484 bool IsPass(const char* name) {
485 return strcmp(pass_name_, name) == 0;
486 }
487
David Brazdilb7e4a062014-12-29 15:35:02 +0000488 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100489 output_ << instruction->DebugName();
490 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100491 StringList inputs;
492 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
493 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100494 }
David Brazdilc74652862015-05-13 17:50:09 +0100495 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100496 }
David Brazdilc74652862015-05-13 17:50:09 +0100497 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800498 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100499 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100500 for (HEnvironment* environment = instruction->GetEnvironment();
501 environment != nullptr;
502 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100503 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100504 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
505 HInstruction* insn = environment->GetInstructionAt(i);
506 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100507 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100508 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100509 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100510 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800511 }
David Brazdilc74652862015-05-13 17:50:09 +0100512 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800513 }
David Brazdilc74652862015-05-13 17:50:09 +0100514 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800515 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800516 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000517 && is_after_pass_
518 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100519 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100520 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100521 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100522 StartAttributeStream("ranges")
523 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100524 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
525 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
526 StartAttributeStream("is_fixed") << interval->IsFixed();
527 StartAttributeStream("is_split") << interval->IsSplit();
528 StartAttributeStream("is_low") << interval->IsLowInterval();
529 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100530 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000531 }
532
533 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100534 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100535 LocationSummary* locations = instruction->GetLocations();
536 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100537 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100538 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100539 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100540 }
David Brazdilc74652862015-05-13 17:50:09 +0100541 std::ostream& attr = StartAttributeStream("locations");
542 attr << inputs << "->";
543 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100544 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000545 }
546
547 if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
548 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)
549 || IsPass(HDeadCodeElimination::kInitialDeadCodeEliminationPassName)
Aart Bik09e8d5f2016-01-22 16:49:55 -0800550 || IsPass(BoundsCheckElimination::kBoundsCheckEliminationPassName)
Nicolas Geoffrayad4ed082016-01-27 14:15:23 +0000551 || IsPass(RegisterAllocator::kRegisterAllocatorPassName)
David Brazdilbadd8262016-02-02 16:28:56 +0000552 || IsPass(HGraphBuilder::kBuilderPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000553 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
554 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100555 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000556 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100557 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000558 HLoopInformation* outer = info->GetPreHeader()->GetLoopInformation();
559 if (outer != nullptr) {
560 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
561 } else {
562 StartAttributeStream("outer_loop") << "none";
563 }
564 StartAttributeStream("irreducible")
565 << std::boolalpha << info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000566 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000567 }
568
David Brazdilbadd8262016-02-02 16:28:56 +0000569 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000570 || IsPass(HInliner::kInlinerPassName))
Calin Juravle2e768302015-07-28 14:41:11 +0000571 && (instruction->GetType() == Primitive::kPrimNot)) {
572 ReferenceTypeInfo info = instruction->IsLoadClass()
573 ? instruction->AsLoadClass()->GetLoadedClassRTI()
574 : instruction->GetReferenceTypeInfo();
575 ScopedObjectAccess soa(Thread::Current());
576 if (info.IsValid()) {
577 StartAttributeStream("klass") << PrettyDescriptor(info.GetTypeHandle().Get());
578 StartAttributeStream("can_be_null")
579 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
580 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Calin Juravle98893e12015-10-02 21:05:03 +0100581 } else if (instruction->IsLoadClass()) {
582 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000583 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500584 // The NullConstant may be added to the graph during other passes that happen between
585 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
586 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
587 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000588 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000589 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000590 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
591 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100592 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100593 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100594 if (disasm_info_ != nullptr) {
595 DCHECK(disassembler_ != nullptr);
596 // If the information is available, disassemble the code generated for
597 // this instruction.
598 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
599 if (it != disasm_info_->GetInstructionIntervals().end()
600 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100601 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100602 disassembler_->Disassemble(output_, it->second.start, it->second.end);
603 }
604 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100605 }
606
607 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100608 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
609 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100610 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100611 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000612 AddIndent();
613 output_ << bci << " " << num_uses << " "
614 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000615 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100616 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100617 }
618 }
619
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100620 void DumpStartOfDisassemblyBlock(const char* block_name,
621 int predecessor_index,
622 int successor_index) {
623 StartTag("block");
624 PrintProperty("name", block_name);
625 PrintInt("from_bci", -1);
626 PrintInt("to_bci", -1);
627 if (predecessor_index != -1) {
628 PrintProperty("predecessors", "B", predecessor_index);
629 } else {
630 PrintEmptyProperty("predecessors");
631 }
632 if (successor_index != -1) {
633 PrintProperty("successors", "B", successor_index);
634 } else {
635 PrintEmptyProperty("successors");
636 }
637 PrintEmptyProperty("xhandlers");
638 PrintEmptyProperty("flags");
639 StartTag("states");
640 StartTag("locals");
641 PrintInt("size", 0);
642 PrintProperty("method", "None");
643 EndTag("locals");
644 EndTag("states");
645 StartTag("HIR");
646 }
647
648 void DumpEndOfDisassemblyBlock() {
649 EndTag("HIR");
650 EndTag("block");
651 }
652
653 void DumpDisassemblyBlockForFrameEntry() {
654 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
655 -1,
656 GetGraph()->GetEntryBlock()->GetBlockId());
657 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
658 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
659 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100660 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100661 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
662 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100663 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100664 DumpEndOfDisassemblyBlock();
665 }
666
667 void DumpDisassemblyBlockForSlowPaths() {
668 if (disasm_info_->GetSlowPathIntervals().empty()) {
669 return;
670 }
671 // If the graph has an exit block we attach the block for the slow paths
672 // after it. Else we just add the block to the graph without linking it to
673 // any other.
674 DumpStartOfDisassemblyBlock(
675 kDisassemblyBlockSlowPaths,
676 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
677 -1);
678 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100679 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100680 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100681 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100682 }
683 DumpEndOfDisassemblyBlock();
684 }
685
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100686 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100687 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100688 std::string pass_desc = std::string(pass_name_)
689 + " ("
690 + (is_after_pass_ ? "after" : "before")
691 + (graph_in_bad_state_ ? ", bad_state" : "")
692 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000693 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100694 if (disasm_info_ != nullptr) {
695 DumpDisassemblyBlockForFrameEntry();
696 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100697 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100698 if (disasm_info_ != nullptr) {
699 DumpDisassemblyBlockForSlowPaths();
700 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100701 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100702 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100703 }
704
David Brazdilb7e4a062014-12-29 15:35:02 +0000705 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100706 StartTag("block");
707 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100708 if (block->GetLifetimeStart() != kNoLifetime) {
709 // Piggy back on these fields to show the lifetime of the block.
710 PrintInt("from_bci", block->GetLifetimeStart());
711 PrintInt("to_bci", block->GetLifetimeEnd());
712 } else {
713 PrintInt("from_bci", -1);
714 PrintInt("to_bci", -1);
715 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100716 PrintPredecessors(block);
717 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000718 PrintExceptionHandlers(block);
719
720 if (block->IsCatchBlock()) {
721 PrintProperty("flags", "catch_block");
722 } else {
723 PrintEmptyProperty("flags");
724 }
725
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100726 if (block->GetDominator() != nullptr) {
727 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
728 }
729
730 StartTag("states");
731 StartTag("locals");
732 PrintInt("size", 0);
733 PrintProperty("method", "None");
734 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
735 AddIndent();
736 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100737 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
738 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100739 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
740 output_ << inputs.Current()->GetId() << " ";
741 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100742 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100743 }
744 EndTag("locals");
745 EndTag("states");
746
747 StartTag("HIR");
748 PrintInstructions(block->GetPhis());
749 PrintInstructions(block->GetInstructions());
750 EndTag("HIR");
751 EndTag("block");
752 }
753
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100754 static constexpr const char* const kEndInstructionMarker = "<|@";
755 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
756 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
757
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100758 private:
759 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100760 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000761 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100762 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100763 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100764 const DisassemblyInformation* disasm_info_;
765 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100766 size_t indent_;
767
768 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
769};
770
771HGraphVisualizer::HGraphVisualizer(std::ostream* output,
772 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100773 const CodeGenerator& codegen)
774 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100775
David Brazdil62e074f2015-04-07 18:09:37 +0100776void HGraphVisualizer::PrintHeader(const char* method_name) const {
777 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100778 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100779 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 printer.PrintProperty("name", method_name);
781 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100782 printer.PrintTime("date");
783 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100784 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100785}
786
David Brazdilffee3d32015-07-06 11:48:53 +0100787void HGraphVisualizer::DumpGraph(const char* pass_name,
788 bool is_after_pass,
789 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000790 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100791 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100792 HGraphVisualizerPrinter printer(graph_,
793 *output_,
794 pass_name,
795 is_after_pass,
796 graph_in_bad_state,
797 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000798 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100799 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100800}
801
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100802void HGraphVisualizer::DumpGraphWithDisassembly() const {
803 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100804 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100805 HGraphVisualizerPrinter printer(graph_,
806 *output_,
807 "disassembly",
808 /* is_after_pass */ true,
809 /* graph_in_bad_state */ false,
810 codegen_,
811 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100812 printer.Run();
813 }
814}
815
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100816} // namespace art