blob: ba0249345d98bbca42d893e6ee68199638575ebb [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"
Matthew Gharritye9288852016-07-14 14:08:16 -070034#include "register_allocator_linear_scan.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:
Aart Bikd3059e72016-05-11 10:30:47 -0700101 HGraphVisualizerDisassembler(InstructionSet instruction_set,
102 const uint8_t* base_address,
103 const uint8_t* end_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100104 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100105 libart_disassembler_handle_ =
106 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
107 if (libart_disassembler_handle_ == nullptr) {
108 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
109 return;
110 }
111 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
112 dlsym(libart_disassembler_handle_, "create_disassembler"));
113 if (create_disassembler == nullptr) {
114 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
115 return;
116 }
117 // Reading the disassembly from 0x0 is easier, so we print relative
118 // addresses. We will only disassemble the code once everything has
119 // been generated, so we can read data in literal pools.
120 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
121 instruction_set,
122 new DisassemblerOptions(/* absolute_addresses */ false,
123 base_address,
Aart Bikd3059e72016-05-11 10:30:47 -0700124 end_address,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700125 /* can_read_literals */ true,
126 Is64BitInstructionSet(instruction_set)
127 ? &Thread::DumpThreadOffset<PointerSize::k64>
128 : &Thread::DumpThreadOffset<PointerSize::k32>)));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100129 }
130
131 ~HGraphVisualizerDisassembler() {
132 // We need to call ~Disassembler() before we close the library.
133 disassembler_.reset();
134 if (libart_disassembler_handle_ != nullptr) {
135 dlclose(libart_disassembler_handle_);
136 }
137 }
138
139 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100140 if (disassembler_ == nullptr) {
141 return;
142 }
143
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100144 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
145 if (instruction_set_ == kThumb2) {
146 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
147 // address is used to distinguish between the two.
148 base += 1;
149 }
150 disassembler_->Dump(output, base + start, base + end);
151 }
152
153 private:
154 InstructionSet instruction_set_;
155 std::unique_ptr<Disassembler> disassembler_;
156
157 void* libart_disassembler_handle_;
158};
159
160
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100161/**
162 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
163 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100164class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100165 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100166 HGraphVisualizerPrinter(HGraph* graph,
167 std::ostream& output,
168 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000169 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100170 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100171 const CodeGenerator& codegen,
172 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100173 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100174 output_(output),
175 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000176 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100177 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100178 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100179 disasm_info_(disasm_info),
180 disassembler_(disasm_info_ != nullptr
181 ? new HGraphVisualizerDisassembler(
182 codegen_.GetInstructionSet(),
Aart Bikd3059e72016-05-11 10:30:47 -0700183 codegen_.GetAssembler().CodeBufferBaseAddress(),
184 codegen_.GetAssembler().CodeBufferBaseAddress()
185 + codegen_.GetAssembler().CodeSize())
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100186 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100187 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100188
David Brazdilfa02c9d2016-03-30 09:41:02 +0100189 void Flush() {
190 // We use "\n" instead of std::endl to avoid implicit flushing which
191 // generates too many syscalls during debug-GC tests (b/27826765).
192 output_ << std::flush;
193 }
194
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100195 void StartTag(const char* name) {
196 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100197 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100198 indent_++;
199 }
200
201 void EndTag(const char* name) {
202 indent_--;
203 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100204 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100205 }
206
207 void PrintProperty(const char* name, const char* property) {
208 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100209 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100210 }
211
212 void PrintProperty(const char* name, const char* property, int id) {
213 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100214 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100215 }
216
217 void PrintEmptyProperty(const char* name) {
218 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100219 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100220 }
221
222 void PrintTime(const char* name) {
223 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100224 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100225 }
226
227 void PrintInt(const char* name, int value) {
228 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100229 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100230 }
231
232 void AddIndent() {
233 for (size_t i = 0; i < indent_; ++i) {
234 output_ << " ";
235 }
236 }
237
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100238 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100239 // Note that Primitive::Descriptor would not work for us
240 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100241 switch (type) {
242 case Primitive::kPrimBoolean: return 'z';
243 case Primitive::kPrimByte: return 'b';
244 case Primitive::kPrimChar: return 'c';
245 case Primitive::kPrimShort: return 's';
246 case Primitive::kPrimInt: return 'i';
247 case Primitive::kPrimLong: return 'j';
248 case Primitive::kPrimFloat: return 'f';
249 case Primitive::kPrimDouble: return 'd';
250 case Primitive::kPrimNot: return 'l';
251 case Primitive::kPrimVoid: return 'v';
252 }
253 LOG(FATAL) << "Unreachable";
254 return 'v';
255 }
256
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100257 void PrintPredecessors(HBasicBlock* block) {
258 AddIndent();
259 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000260 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100261 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
262 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100263 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
264 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
265 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100266 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100267 }
268
269 void PrintSuccessors(HBasicBlock* block) {
270 AddIndent();
271 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000272 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100273 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000274 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100275 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000276 }
277
278 void PrintExceptionHandlers(HBasicBlock* block) {
279 AddIndent();
280 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000281 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100282 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100283 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100284 if (block->IsExitBlock() &&
285 (disasm_info_ != nullptr) &&
286 !disasm_info_->GetSlowPathIntervals().empty()) {
287 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
288 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100289 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100290 }
291
David Brazdilc74652862015-05-13 17:50:09 +0100292 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100293 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100294 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100295 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100296 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100297 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100298 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100299 HConstant* constant = location.GetConstant();
300 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100301 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100302 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100303 stream << constant->AsLongConstant()->GetValue();
Alexandre Ramesc2c52a12016-08-02 13:45:28 +0100304 } else if (constant->IsFloatConstant()) {
305 stream << constant->AsFloatConstant()->GetValue();
306 } else if (constant->IsDoubleConstant()) {
307 stream << constant->AsDoubleConstant()->GetValue();
308 } else if (constant->IsNullConstant()) {
309 stream << "null";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100310 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100311 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100312 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100313 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100314 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000315 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100316 codegen_.DumpFloatingPointRegister(stream, location.low());
317 stream << "|";
318 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000319 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100320 codegen_.DumpCoreRegister(stream, location.low());
321 stream << "|";
322 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400323 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100324 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100325 } else {
326 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100327 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100328 }
329 }
330
David Brazdilc74652862015-05-13 17:50:09 +0100331 std::ostream& StartAttributeStream(const char* name = nullptr) {
332 if (name == nullptr) {
333 output_ << " ";
334 } else {
335 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
336 output_ << " " << name << ":";
337 }
338 return output_;
339 }
340
David Brazdilb7e4a062014-12-29 15:35:02 +0000341 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100342 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
343 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100344 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
345 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100346 std::ostream& str = moves.NewEntryStream();
347 DumpLocation(str, move->GetSource());
348 str << "->";
349 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100350 }
David Brazdilc74652862015-05-13 17:50:09 +0100351 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100352 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100353
David Brazdil36cf0952015-01-08 19:28:33 +0000354 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100355 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000356 }
357
David Brazdil36cf0952015-01-08 19:28:33 +0000358 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100359 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000360 }
361
David Brazdil36cf0952015-01-08 19:28:33 +0000362 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100363 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000364 }
365
David Brazdil36cf0952015-01-08 19:28:33 +0000366 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100367 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000368 }
369
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000370 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100371 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100372 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000373 }
374
Calin Juravle27df7582015-04-17 19:12:31 +0100375 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100376 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100377 }
378
David Brazdilbff75032015-07-08 17:26:51 +0000379 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
380 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
381 }
382
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100383 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100384 StartAttributeStream("load_kind") << load_class->GetLoadKind();
385 const char* descriptor = load_class->GetDexFile().GetTypeDescriptor(
386 load_class->GetDexFile().GetTypeId(load_class->GetTypeIndex()));
387 StartAttributeStream("class_name") << PrettyDescriptor(descriptor);
Calin Juravle0ba218d2015-05-19 18:46:01 +0100388 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100389 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100390 StartAttributeStream("needs_access_check") << std::boolalpha
391 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100392 }
393
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000394 void VisitLoadString(HLoadString* load_string) OVERRIDE {
395 StartAttributeStream("load_kind") << load_string->GetLoadKind();
396 }
397
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100398 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000399 StartAttributeStream("check_kind") << check_cast->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100400 StartAttributeStream("must_do_null_check") << std::boolalpha
401 << check_cast->MustDoNullCheck() << std::noboolalpha;
402 }
403
404 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000405 StartAttributeStream("check_kind") << instance_of->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100406 StartAttributeStream("must_do_null_check") << std::boolalpha
407 << instance_of->MustDoNullCheck() << std::noboolalpha;
408 }
409
Vladimir Markodce016e2016-04-28 13:10:02 +0100410 void VisitArrayLength(HArrayLength* array_length) OVERRIDE {
411 StartAttributeStream("is_string_length") << std::boolalpha
412 << array_length->IsStringLength() << std::noboolalpha;
Mark Mendellee8d9712016-07-12 11:13:15 -0400413 if (array_length->IsEmittedAtUseSite()) {
414 StartAttributeStream("emitted_at_use") << "true";
415 }
Vladimir Markodce016e2016-04-28 13:10:02 +0100416 }
417
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100418 void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE {
419 StartAttributeStream("is_string_char_at") << std::boolalpha
420 << bounds_check->IsStringCharAt() << std::noboolalpha;
421 }
422
423 void VisitArrayGet(HArrayGet* array_get) OVERRIDE {
424 StartAttributeStream("is_string_char_at") << std::boolalpha
425 << array_get->IsStringCharAt() << std::noboolalpha;
426 }
427
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100428 void VisitArraySet(HArraySet* array_set) OVERRIDE {
429 StartAttributeStream("value_can_be_null") << std::boolalpha
430 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000431 StartAttributeStream("needs_type_check") << std::boolalpha
432 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100433 }
434
Roland Levillain31dd3d62016-02-16 12:21:02 +0000435 void VisitCompare(HCompare* compare) OVERRIDE {
436 ComparisonBias bias = compare->GetBias();
437 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
438 ? "gt"
439 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
440 }
441
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100442 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100443 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
David Sehr709b0702016-10-13 09:12:37 -0700444 StartAttributeStream("method_name") << GetGraph()->GetDexFile().PrettyMethod(
445 invoke->GetDexMethodIndex(), /* with_signature */ false);
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100446 }
447
Calin Juravle175dc732015-08-25 15:42:32 +0100448 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
449 VisitInvoke(invoke);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100450 StartAttributeStream("invoke_type") << invoke->GetInvokeType();
Calin Juravle175dc732015-08-25 15:42:32 +0100451 }
452
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100453 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
454 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000455 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100456 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000457 if (invoke->IsStatic()) {
458 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
459 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100460 }
461
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000462 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
463 VisitInvoke(invoke);
464 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
465 }
466
Orion Hodsonac141392017-01-13 11:53:47 +0000467 void VisitInvokePolymorphic(HInvokePolymorphic* invoke) OVERRIDE {
468 VisitInvoke(invoke);
469 StartAttributeStream("invoke_type") << "InvokePolymorphic";
470 }
471
David Brazdil11edec72016-03-24 12:40:52 +0000472 void VisitInstanceFieldGet(HInstanceFieldGet* iget) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700473 StartAttributeStream("field_name") <<
474 iget->GetFieldInfo().GetDexFile().PrettyField(iget->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000475 /* with type */ false);
476 StartAttributeStream("field_type") << iget->GetFieldType();
477 }
478
479 void VisitInstanceFieldSet(HInstanceFieldSet* iset) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700480 StartAttributeStream("field_name") <<
481 iset->GetFieldInfo().GetDexFile().PrettyField(iset->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000482 /* with type */ false);
483 StartAttributeStream("field_type") << iset->GetFieldType();
484 }
485
Calin Juravlee460d1d2015-09-29 04:52:17 +0100486 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
487 StartAttributeStream("field_type") << field_access->GetFieldType();
488 }
489
490 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
491 StartAttributeStream("field_type") << field_access->GetFieldType();
492 }
493
494 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
495 StartAttributeStream("field_type") << field_access->GetFieldType();
496 }
497
498 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
499 StartAttributeStream("field_type") << field_access->GetFieldType();
500 }
501
David Brazdilfc6a86a2015-06-26 10:33:45 +0000502 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100503 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000504 }
505
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000506 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE {
507 StartAttributeStream("kind") << deoptimize->GetKind();
508 }
509
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300510#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
511 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) OVERRIDE {
512 StartAttributeStream("kind") << instruction->GetOpKind();
513 }
Artem Serov7fc63502016-02-09 17:15:29 +0000514
515 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) OVERRIDE {
516 StartAttributeStream("kind") << instruction->GetOpKind();
517 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300518
Anton Kirilov74234da2017-01-13 14:42:47 +0000519 void VisitDataProcWithShifterOp(HDataProcWithShifterOp* instruction) OVERRIDE {
Alexandre Rames8626b742015-11-25 16:28:08 +0000520 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
Anton Kirilov74234da2017-01-13 14:42:47 +0000521 if (HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000522 StartAttributeStream("shift") << instruction->GetShiftAmount();
523 }
524 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000525#endif
526
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800527 bool IsPass(const char* name) {
528 return strcmp(pass_name_, name) == 0;
529 }
530
David Brazdilb7e4a062014-12-29 15:35:02 +0000531 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100532 output_ << instruction->DebugName();
Vladimir Markoe9004912016-06-16 16:50:52 +0100533 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100534 if (!inputs.empty()) {
535 StringList input_list;
536 for (const HInstruction* input : inputs) {
537 input_list.NewEntryStream() << GetTypeId(input->GetType()) << input->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100538 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100539 StartAttributeStream() << input_list;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100540 }
David Brazdilc74652862015-05-13 17:50:09 +0100541 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800542 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100543 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100544 for (HEnvironment* environment = instruction->GetEnvironment();
545 environment != nullptr;
546 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100547 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100548 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
549 HInstruction* insn = environment->GetInstructionAt(i);
550 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100551 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100552 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100553 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100554 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800555 }
David Brazdilc74652862015-05-13 17:50:09 +0100556 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800557 }
David Brazdilc74652862015-05-13 17:50:09 +0100558 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800559 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800560 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000561 && is_after_pass_
562 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100563 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100564 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100565 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100566 StartAttributeStream("ranges")
567 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100568 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
569 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
570 StartAttributeStream("is_fixed") << interval->IsFixed();
571 StartAttributeStream("is_split") << interval->IsSplit();
572 StartAttributeStream("is_low") << interval->IsLowInterval();
573 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100574 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000575 }
576
577 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100578 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100579 LocationSummary* locations = instruction->GetLocations();
580 if (locations != nullptr) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100581 StringList input_list;
582 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
583 DumpLocation(input_list.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100584 }
David Brazdilc74652862015-05-13 17:50:09 +0100585 std::ostream& attr = StartAttributeStream("locations");
Vladimir Marko372f10e2016-05-17 16:30:10 +0100586 attr << input_list << "->";
David Brazdilc74652862015-05-13 17:50:09 +0100587 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100588 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000589 }
590
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100591 HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
592 if (loop_info == nullptr) {
593 StartAttributeStream("loop") << "none";
594 } else {
595 StartAttributeStream("loop") << "B" << loop_info->GetHeader()->GetBlockId();
596 HLoopInformation* outer = loop_info->GetPreHeader()->GetLoopInformation();
597 if (outer != nullptr) {
598 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000599 } else {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100600 StartAttributeStream("outer_loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000601 }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100602 StartAttributeStream("irreducible")
603 << std::boolalpha << loop_info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000604 }
605
David Brazdilbadd8262016-02-02 16:28:56 +0000606 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000607 || IsPass(HInliner::kInlinerPassName))
Calin Juravle2e768302015-07-28 14:41:11 +0000608 && (instruction->GetType() == Primitive::kPrimNot)) {
609 ReferenceTypeInfo info = instruction->IsLoadClass()
610 ? instruction->AsLoadClass()->GetLoadedClassRTI()
611 : instruction->GetReferenceTypeInfo();
612 ScopedObjectAccess soa(Thread::Current());
613 if (info.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700614 StartAttributeStream("klass")
615 << mirror::Class::PrettyDescriptor(info.GetTypeHandle().Get());
Calin Juravle2e768302015-07-28 14:41:11 +0000616 StartAttributeStream("can_be_null")
617 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
618 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Calin Juravle98893e12015-10-02 21:05:03 +0100619 } else if (instruction->IsLoadClass()) {
620 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000621 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500622 // The NullConstant may be added to the graph during other passes that happen between
623 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
624 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
625 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000626 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000627 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000628 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
629 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100630 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100631 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100632 if (disasm_info_ != nullptr) {
633 DCHECK(disassembler_ != nullptr);
634 // If the information is available, disassemble the code generated for
635 // this instruction.
636 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
637 if (it != disasm_info_->GetInstructionIntervals().end()
638 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100639 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100640 disassembler_->Disassemble(output_, it->second.start, it->second.end);
641 }
642 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100643 }
644
645 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100646 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
647 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100648 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100649 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000650 AddIndent();
651 output_ << bci << " " << num_uses << " "
652 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000653 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100654 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100655 }
656 }
657
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100658 void DumpStartOfDisassemblyBlock(const char* block_name,
659 int predecessor_index,
660 int successor_index) {
661 StartTag("block");
662 PrintProperty("name", block_name);
663 PrintInt("from_bci", -1);
664 PrintInt("to_bci", -1);
665 if (predecessor_index != -1) {
666 PrintProperty("predecessors", "B", predecessor_index);
667 } else {
668 PrintEmptyProperty("predecessors");
669 }
670 if (successor_index != -1) {
671 PrintProperty("successors", "B", successor_index);
672 } else {
673 PrintEmptyProperty("successors");
674 }
675 PrintEmptyProperty("xhandlers");
676 PrintEmptyProperty("flags");
677 StartTag("states");
678 StartTag("locals");
679 PrintInt("size", 0);
680 PrintProperty("method", "None");
681 EndTag("locals");
682 EndTag("states");
683 StartTag("HIR");
684 }
685
686 void DumpEndOfDisassemblyBlock() {
687 EndTag("HIR");
688 EndTag("block");
689 }
690
691 void DumpDisassemblyBlockForFrameEntry() {
692 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
693 -1,
694 GetGraph()->GetEntryBlock()->GetBlockId());
695 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
696 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
697 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100698 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100699 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
700 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100701 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100702 DumpEndOfDisassemblyBlock();
703 }
704
705 void DumpDisassemblyBlockForSlowPaths() {
706 if (disasm_info_->GetSlowPathIntervals().empty()) {
707 return;
708 }
709 // If the graph has an exit block we attach the block for the slow paths
710 // after it. Else we just add the block to the graph without linking it to
711 // any other.
712 DumpStartOfDisassemblyBlock(
713 kDisassemblyBlockSlowPaths,
714 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
715 -1);
716 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100717 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100718 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100719 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100720 }
721 DumpEndOfDisassemblyBlock();
722 }
723
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100724 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100725 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100726 std::string pass_desc = std::string(pass_name_)
727 + " ("
728 + (is_after_pass_ ? "after" : "before")
729 + (graph_in_bad_state_ ? ", bad_state" : "")
730 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000731 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100732 if (disasm_info_ != nullptr) {
733 DumpDisassemblyBlockForFrameEntry();
734 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100735 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100736 if (disasm_info_ != nullptr) {
737 DumpDisassemblyBlockForSlowPaths();
738 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100739 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100740 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100741 }
742
David Brazdilb7e4a062014-12-29 15:35:02 +0000743 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100744 StartTag("block");
745 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100746 if (block->GetLifetimeStart() != kNoLifetime) {
747 // Piggy back on these fields to show the lifetime of the block.
748 PrintInt("from_bci", block->GetLifetimeStart());
749 PrintInt("to_bci", block->GetLifetimeEnd());
750 } else {
751 PrintInt("from_bci", -1);
752 PrintInt("to_bci", -1);
753 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100754 PrintPredecessors(block);
755 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000756 PrintExceptionHandlers(block);
757
758 if (block->IsCatchBlock()) {
759 PrintProperty("flags", "catch_block");
760 } else {
761 PrintEmptyProperty("flags");
762 }
763
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100764 if (block->GetDominator() != nullptr) {
765 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
766 }
767
768 StartTag("states");
769 StartTag("locals");
770 PrintInt("size", 0);
771 PrintProperty("method", "None");
772 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
773 AddIndent();
774 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100775 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
776 << instruction->GetId() << "[ ";
Vladimir Marko372f10e2016-05-17 16:30:10 +0100777 for (const HInstruction* input : instruction->GetInputs()) {
778 output_ << input->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100779 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100780 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100781 }
782 EndTag("locals");
783 EndTag("states");
784
785 StartTag("HIR");
786 PrintInstructions(block->GetPhis());
787 PrintInstructions(block->GetInstructions());
788 EndTag("HIR");
789 EndTag("block");
790 }
791
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100792 static constexpr const char* const kEndInstructionMarker = "<|@";
793 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
794 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
795
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100796 private:
797 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100798 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000799 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100800 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100801 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100802 const DisassemblyInformation* disasm_info_;
803 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100804 size_t indent_;
805
806 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
807};
808
809HGraphVisualizer::HGraphVisualizer(std::ostream* output,
810 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100811 const CodeGenerator& codegen)
812 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100813
David Brazdil62e074f2015-04-07 18:09:37 +0100814void HGraphVisualizer::PrintHeader(const char* method_name) const {
815 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100816 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100817 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000818 printer.PrintProperty("name", method_name);
819 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100820 printer.PrintTime("date");
821 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100822 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100823}
824
David Brazdilffee3d32015-07-06 11:48:53 +0100825void HGraphVisualizer::DumpGraph(const char* pass_name,
826 bool is_after_pass,
827 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000828 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100829 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100830 HGraphVisualizerPrinter printer(graph_,
831 *output_,
832 pass_name,
833 is_after_pass,
834 graph_in_bad_state,
835 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000836 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100837 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100838}
839
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100840void HGraphVisualizer::DumpGraphWithDisassembly() const {
841 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100842 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100843 HGraphVisualizerPrinter printer(graph_,
844 *output_,
845 "disassembly",
846 /* is_after_pass */ true,
847 /* graph_in_bad_state */ false,
848 codegen_,
849 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100850 printer.Run();
851 }
852}
853
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100854} // namespace art