blob: 3035e4657d344ad32aad0ac450bbc7197c0bc819 [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"
Vladimir Marko82b07402017-03-01 19:02:04 +000037#include "utils/intrusive_forward_list.h"
David Brazdilc74652862015-05-13 17:50:09 +010038
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010039namespace art {
40
David Brazdilc74652862015-05-13 17:50:09 +010041static bool HasWhitespace(const char* str) {
42 DCHECK(str != nullptr);
43 while (str[0] != 0) {
44 if (isspace(str[0])) {
45 return true;
46 }
47 str++;
48 }
49 return false;
50}
51
52class StringList {
53 public:
David Brazdilc7a24852015-05-15 16:44:05 +010054 enum Format {
55 kArrayBrackets,
56 kSetBrackets,
57 };
58
David Brazdilc74652862015-05-13 17:50:09 +010059 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010060 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010061
62 // Construct StringList from a linked list. List element class T
63 // must provide methods `GetNext` and `Dump`.
64 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010065 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010066 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
67 current->Dump(NewEntryStream());
68 }
69 }
Vladimir Marko82b07402017-03-01 19:02:04 +000070 // Construct StringList from a list of elements. The value type must provide method `Dump`.
71 template <typename Container>
72 explicit StringList(const Container& list, Format format = kArrayBrackets) : StringList(format) {
73 for (const typename Container::value_type& current : list) {
74 current.Dump(NewEntryStream());
75 }
76 }
David Brazdilc74652862015-05-13 17:50:09 +010077
78 std::ostream& NewEntryStream() {
79 if (is_empty_) {
80 is_empty_ = false;
81 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010082 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010083 }
84 return sstream_;
85 }
86
87 private:
David Brazdilc7a24852015-05-15 16:44:05 +010088 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010089 bool is_empty_;
90 std::ostringstream sstream_;
91
92 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
93};
94
95std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010096 switch (list.format_) {
97 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
98 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
99 default:
100 LOG(FATAL) << "Invalid StringList format";
101 UNREACHABLE();
102 }
David Brazdilc74652862015-05-13 17:50:09 +0100103}
104
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100105typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
106 DisassemblerOptions* options);
107class HGraphVisualizerDisassembler {
108 public:
Aart Bikd3059e72016-05-11 10:30:47 -0700109 HGraphVisualizerDisassembler(InstructionSet instruction_set,
110 const uint8_t* base_address,
111 const uint8_t* end_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100112 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100113 libart_disassembler_handle_ =
114 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
115 if (libart_disassembler_handle_ == nullptr) {
116 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
117 return;
118 }
119 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
120 dlsym(libart_disassembler_handle_, "create_disassembler"));
121 if (create_disassembler == nullptr) {
122 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
123 return;
124 }
125 // Reading the disassembly from 0x0 is easier, so we print relative
126 // addresses. We will only disassemble the code once everything has
127 // been generated, so we can read data in literal pools.
128 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
129 instruction_set,
130 new DisassemblerOptions(/* absolute_addresses */ false,
131 base_address,
Aart Bikd3059e72016-05-11 10:30:47 -0700132 end_address,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700133 /* can_read_literals */ true,
134 Is64BitInstructionSet(instruction_set)
135 ? &Thread::DumpThreadOffset<PointerSize::k64>
136 : &Thread::DumpThreadOffset<PointerSize::k32>)));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100137 }
138
139 ~HGraphVisualizerDisassembler() {
140 // We need to call ~Disassembler() before we close the library.
141 disassembler_.reset();
142 if (libart_disassembler_handle_ != nullptr) {
143 dlclose(libart_disassembler_handle_);
144 }
145 }
146
147 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100148 if (disassembler_ == nullptr) {
149 return;
150 }
151
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100152 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
153 if (instruction_set_ == kThumb2) {
154 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
155 // address is used to distinguish between the two.
156 base += 1;
157 }
158 disassembler_->Dump(output, base + start, base + end);
159 }
160
161 private:
162 InstructionSet instruction_set_;
163 std::unique_ptr<Disassembler> disassembler_;
164
165 void* libart_disassembler_handle_;
166};
167
168
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100169/**
170 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
171 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100172class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100173 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100174 HGraphVisualizerPrinter(HGraph* graph,
175 std::ostream& output,
176 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000177 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100178 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100179 const CodeGenerator& codegen,
180 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100181 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100182 output_(output),
183 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000184 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100185 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100186 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100187 disasm_info_(disasm_info),
188 disassembler_(disasm_info_ != nullptr
189 ? new HGraphVisualizerDisassembler(
190 codegen_.GetInstructionSet(),
Aart Bikd3059e72016-05-11 10:30:47 -0700191 codegen_.GetAssembler().CodeBufferBaseAddress(),
192 codegen_.GetAssembler().CodeBufferBaseAddress()
193 + codegen_.GetAssembler().CodeSize())
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100194 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100195 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100196
David Brazdilfa02c9d2016-03-30 09:41:02 +0100197 void Flush() {
198 // We use "\n" instead of std::endl to avoid implicit flushing which
199 // generates too many syscalls during debug-GC tests (b/27826765).
200 output_ << std::flush;
201 }
202
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100203 void StartTag(const char* name) {
204 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100205 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100206 indent_++;
207 }
208
209 void EndTag(const char* name) {
210 indent_--;
211 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100212 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100213 }
214
215 void PrintProperty(const char* name, const char* property) {
216 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100217 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100218 }
219
220 void PrintProperty(const char* name, const char* property, int id) {
221 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100222 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100223 }
224
225 void PrintEmptyProperty(const char* name) {
226 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100227 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100228 }
229
230 void PrintTime(const char* name) {
231 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100232 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100233 }
234
235 void PrintInt(const char* name, int value) {
236 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100237 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100238 }
239
240 void AddIndent() {
241 for (size_t i = 0; i < indent_; ++i) {
242 output_ << " ";
243 }
244 }
245
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100246 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100247 // Note that Primitive::Descriptor would not work for us
248 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100249 switch (type) {
250 case Primitive::kPrimBoolean: return 'z';
251 case Primitive::kPrimByte: return 'b';
252 case Primitive::kPrimChar: return 'c';
253 case Primitive::kPrimShort: return 's';
254 case Primitive::kPrimInt: return 'i';
255 case Primitive::kPrimLong: return 'j';
256 case Primitive::kPrimFloat: return 'f';
257 case Primitive::kPrimDouble: return 'd';
258 case Primitive::kPrimNot: return 'l';
259 case Primitive::kPrimVoid: return 'v';
260 }
261 LOG(FATAL) << "Unreachable";
262 return 'v';
263 }
264
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100265 void PrintPredecessors(HBasicBlock* block) {
266 AddIndent();
267 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000268 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100269 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
270 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100271 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
272 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
273 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100274 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100275 }
276
277 void PrintSuccessors(HBasicBlock* block) {
278 AddIndent();
279 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000280 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100281 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000282 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100283 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000284 }
285
286 void PrintExceptionHandlers(HBasicBlock* block) {
287 AddIndent();
288 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000289 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100290 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100291 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100292 if (block->IsExitBlock() &&
293 (disasm_info_ != nullptr) &&
294 !disasm_info_->GetSlowPathIntervals().empty()) {
295 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
296 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100297 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100298 }
299
David Brazdilc74652862015-05-13 17:50:09 +0100300 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100301 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100302 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100303 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100304 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100305 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100306 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100307 HConstant* constant = location.GetConstant();
308 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100309 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100310 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100311 stream << constant->AsLongConstant()->GetValue();
Alexandre Ramesc2c52a12016-08-02 13:45:28 +0100312 } else if (constant->IsFloatConstant()) {
313 stream << constant->AsFloatConstant()->GetValue();
314 } else if (constant->IsDoubleConstant()) {
315 stream << constant->AsDoubleConstant()->GetValue();
316 } else if (constant->IsNullConstant()) {
317 stream << "null";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100318 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100319 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100320 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100321 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100322 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000323 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100324 codegen_.DumpFloatingPointRegister(stream, location.low());
325 stream << "|";
326 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000327 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100328 codegen_.DumpCoreRegister(stream, location.low());
329 stream << "|";
330 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400331 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100332 stream << "unallocated";
Aart Bik5576f372017-03-23 16:17:37 -0700333 } else if (location.IsDoubleStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100334 stream << "2x" << location.GetStackIndex() << "(sp)";
Aart Bik5576f372017-03-23 16:17:37 -0700335 } else {
336 DCHECK(location.IsSIMDStackSlot());
337 stream << "4x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100338 }
339 }
340
David Brazdilc74652862015-05-13 17:50:09 +0100341 std::ostream& StartAttributeStream(const char* name = nullptr) {
342 if (name == nullptr) {
343 output_ << " ";
344 } else {
345 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
346 output_ << " " << name << ":";
347 }
348 return output_;
349 }
350
David Brazdilb7e4a062014-12-29 15:35:02 +0000351 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100352 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
353 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100354 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
355 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100356 std::ostream& str = moves.NewEntryStream();
357 DumpLocation(str, move->GetSource());
358 str << "->";
359 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100360 }
David Brazdilc74652862015-05-13 17:50:09 +0100361 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100362 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100363
David Brazdil36cf0952015-01-08 19:28:33 +0000364 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100365 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000366 }
367
David Brazdil36cf0952015-01-08 19:28:33 +0000368 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100369 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000370 }
371
David Brazdil36cf0952015-01-08 19:28:33 +0000372 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100373 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000374 }
375
David Brazdil36cf0952015-01-08 19:28:33 +0000376 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100377 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000378 }
379
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000380 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100381 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100382 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000383 }
384
Calin Juravle27df7582015-04-17 19:12:31 +0100385 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100386 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100387 }
388
David Brazdilbff75032015-07-08 17:26:51 +0000389 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
390 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
391 }
392
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100393 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100394 StartAttributeStream("load_kind") << load_class->GetLoadKind();
395 const char* descriptor = load_class->GetDexFile().GetTypeDescriptor(
396 load_class->GetDexFile().GetTypeId(load_class->GetTypeIndex()));
397 StartAttributeStream("class_name") << PrettyDescriptor(descriptor);
Calin Juravle0ba218d2015-05-19 18:46:01 +0100398 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100399 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100400 StartAttributeStream("needs_access_check") << std::boolalpha
401 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100402 }
403
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000404 void VisitLoadString(HLoadString* load_string) OVERRIDE {
405 StartAttributeStream("load_kind") << load_string->GetLoadKind();
406 }
407
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100408 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000409 StartAttributeStream("check_kind") << check_cast->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100410 StartAttributeStream("must_do_null_check") << std::boolalpha
411 << check_cast->MustDoNullCheck() << std::noboolalpha;
412 }
413
414 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000415 StartAttributeStream("check_kind") << instance_of->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100416 StartAttributeStream("must_do_null_check") << std::boolalpha
417 << instance_of->MustDoNullCheck() << std::noboolalpha;
418 }
419
Vladimir Markodce016e2016-04-28 13:10:02 +0100420 void VisitArrayLength(HArrayLength* array_length) OVERRIDE {
421 StartAttributeStream("is_string_length") << std::boolalpha
422 << array_length->IsStringLength() << std::noboolalpha;
Mark Mendellee8d9712016-07-12 11:13:15 -0400423 if (array_length->IsEmittedAtUseSite()) {
424 StartAttributeStream("emitted_at_use") << "true";
425 }
Vladimir Markodce016e2016-04-28 13:10:02 +0100426 }
427
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100428 void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE {
429 StartAttributeStream("is_string_char_at") << std::boolalpha
430 << bounds_check->IsStringCharAt() << std::noboolalpha;
431 }
432
433 void VisitArrayGet(HArrayGet* array_get) OVERRIDE {
434 StartAttributeStream("is_string_char_at") << std::boolalpha
435 << array_get->IsStringCharAt() << std::noboolalpha;
436 }
437
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100438 void VisitArraySet(HArraySet* array_set) OVERRIDE {
439 StartAttributeStream("value_can_be_null") << std::boolalpha
440 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000441 StartAttributeStream("needs_type_check") << std::boolalpha
442 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100443 }
444
Roland Levillain31dd3d62016-02-16 12:21:02 +0000445 void VisitCompare(HCompare* compare) OVERRIDE {
446 ComparisonBias bias = compare->GetBias();
447 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
448 ? "gt"
449 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
450 }
451
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100452 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100453 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray5ceac0e2017-06-26 13:19:09 +0100454 ArtMethod* method = invoke->GetResolvedMethod();
455 // We don't print signatures, which conflict with c1visualizer format.
456 static constexpr bool kWithSignature = false;
457 // Note that we can only use the graph's dex file for the unresolved case. The
458 // other invokes might be coming from inlined methods.
459 ScopedObjectAccess soa(Thread::Current());
460 std::string method_name = (method == nullptr)
461 ? GetGraph()->GetDexFile().PrettyMethod(invoke->GetDexMethodIndex(), kWithSignature)
462 : method->PrettyMethod(kWithSignature);
463 StartAttributeStream("method_name") << method_name;
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100464 }
465
Calin Juravle175dc732015-08-25 15:42:32 +0100466 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
467 VisitInvoke(invoke);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100468 StartAttributeStream("invoke_type") << invoke->GetInvokeType();
Calin Juravle175dc732015-08-25 15:42:32 +0100469 }
470
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100471 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
472 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000473 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100474 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000475 if (invoke->IsStatic()) {
476 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
477 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100478 }
479
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000480 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
481 VisitInvoke(invoke);
482 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
483 }
484
Orion Hodsonac141392017-01-13 11:53:47 +0000485 void VisitInvokePolymorphic(HInvokePolymorphic* invoke) OVERRIDE {
486 VisitInvoke(invoke);
487 StartAttributeStream("invoke_type") << "InvokePolymorphic";
488 }
489
David Brazdil11edec72016-03-24 12:40:52 +0000490 void VisitInstanceFieldGet(HInstanceFieldGet* iget) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700491 StartAttributeStream("field_name") <<
492 iget->GetFieldInfo().GetDexFile().PrettyField(iget->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000493 /* with type */ false);
494 StartAttributeStream("field_type") << iget->GetFieldType();
495 }
496
497 void VisitInstanceFieldSet(HInstanceFieldSet* iset) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700498 StartAttributeStream("field_name") <<
499 iset->GetFieldInfo().GetDexFile().PrettyField(iset->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000500 /* with type */ false);
501 StartAttributeStream("field_type") << iset->GetFieldType();
502 }
503
Vladimir Markobf3243b2017-08-30 14:06:54 +0100504 void VisitStaticFieldGet(HStaticFieldGet* sget) OVERRIDE {
505 StartAttributeStream("field_name") <<
506 sget->GetFieldInfo().GetDexFile().PrettyField(sget->GetFieldInfo().GetFieldIndex(),
507 /* with type */ false);
508 StartAttributeStream("field_type") << sget->GetFieldType();
509 }
510
511 void VisitStaticFieldSet(HStaticFieldSet* sset) OVERRIDE {
512 StartAttributeStream("field_name") <<
513 sset->GetFieldInfo().GetDexFile().PrettyField(sset->GetFieldInfo().GetFieldIndex(),
514 /* with type */ false);
515 StartAttributeStream("field_type") << sset->GetFieldType();
516 }
517
Calin Juravlee460d1d2015-09-29 04:52:17 +0100518 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
519 StartAttributeStream("field_type") << field_access->GetFieldType();
520 }
521
522 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
523 StartAttributeStream("field_type") << field_access->GetFieldType();
524 }
525
526 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
527 StartAttributeStream("field_type") << field_access->GetFieldType();
528 }
529
530 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
531 StartAttributeStream("field_type") << field_access->GetFieldType();
532 }
533
David Brazdilfc6a86a2015-06-26 10:33:45 +0000534 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100535 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000536 }
537
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000538 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE {
539 StartAttributeStream("kind") << deoptimize->GetKind();
540 }
541
Aart Bikf3e61ee2017-04-12 17:09:20 -0700542 void VisitVecHalvingAdd(HVecHalvingAdd* hadd) OVERRIDE {
543 StartAttributeStream("unsigned") << std::boolalpha << hadd->IsUnsigned() << std::noboolalpha;
544 StartAttributeStream("rounded") << std::boolalpha << hadd->IsRounded() << std::noboolalpha;
545 }
546
Aart Bikc8e93c72017-05-10 10:49:22 -0700547 void VisitVecMin(HVecMin* min) OVERRIDE {
548 StartAttributeStream("unsigned") << std::boolalpha << min->IsUnsigned() << std::noboolalpha;
549 }
550
551 void VisitVecMax(HVecMax* max) OVERRIDE {
552 StartAttributeStream("unsigned") << std::boolalpha << max->IsUnsigned() << std::noboolalpha;
553 }
554
Artem Serovf34dd202017-04-10 17:41:46 +0100555 void VisitVecMultiplyAccumulate(HVecMultiplyAccumulate* instruction) OVERRIDE {
556 StartAttributeStream("kind") << instruction->GetOpKind();
557 }
558
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300559#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
560 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) OVERRIDE {
561 StartAttributeStream("kind") << instruction->GetOpKind();
562 }
Artem Serov7fc63502016-02-09 17:15:29 +0000563
564 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) OVERRIDE {
565 StartAttributeStream("kind") << instruction->GetOpKind();
566 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300567
Anton Kirilov74234da2017-01-13 14:42:47 +0000568 void VisitDataProcWithShifterOp(HDataProcWithShifterOp* instruction) OVERRIDE {
Alexandre Rames8626b742015-11-25 16:28:08 +0000569 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
Anton Kirilov74234da2017-01-13 14:42:47 +0000570 if (HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000571 StartAttributeStream("shift") << instruction->GetShiftAmount();
572 }
573 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000574#endif
575
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800576 bool IsPass(const char* name) {
577 return strcmp(pass_name_, name) == 0;
578 }
579
David Brazdilb7e4a062014-12-29 15:35:02 +0000580 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100581 output_ << instruction->DebugName();
Vladimir Markoe9004912016-06-16 16:50:52 +0100582 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100583 if (!inputs.empty()) {
584 StringList input_list;
585 for (const HInstruction* input : inputs) {
586 input_list.NewEntryStream() << GetTypeId(input->GetType()) << input->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100587 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100588 StartAttributeStream() << input_list;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100589 }
David Brazdilc74652862015-05-13 17:50:09 +0100590 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800591 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100592 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100593 for (HEnvironment* environment = instruction->GetEnvironment();
594 environment != nullptr;
595 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100596 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100597 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
598 HInstruction* insn = environment->GetInstructionAt(i);
599 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100600 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100601 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100602 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100603 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800604 }
David Brazdilc74652862015-05-13 17:50:09 +0100605 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800606 }
David Brazdilc74652862015-05-13 17:50:09 +0100607 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800608 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800609 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000610 && is_after_pass_
611 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100612 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100613 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100614 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100615 StartAttributeStream("ranges")
616 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
Vladimir Marko82b07402017-03-01 19:02:04 +0000617 StartAttributeStream("uses") << StringList(interval->GetUses());
618 StartAttributeStream("env_uses") << StringList(interval->GetEnvironmentUses());
David Brazdilc74652862015-05-13 17:50:09 +0100619 StartAttributeStream("is_fixed") << interval->IsFixed();
620 StartAttributeStream("is_split") << interval->IsSplit();
621 StartAttributeStream("is_low") << interval->IsLowInterval();
622 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100623 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000624 }
625
626 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100627 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100628 LocationSummary* locations = instruction->GetLocations();
629 if (locations != nullptr) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100630 StringList input_list;
631 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
632 DumpLocation(input_list.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100633 }
David Brazdilc74652862015-05-13 17:50:09 +0100634 std::ostream& attr = StartAttributeStream("locations");
Vladimir Marko372f10e2016-05-17 16:30:10 +0100635 attr << input_list << "->";
David Brazdilc74652862015-05-13 17:50:09 +0100636 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100637 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000638 }
639
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100640 HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
641 if (loop_info == nullptr) {
642 StartAttributeStream("loop") << "none";
643 } else {
644 StartAttributeStream("loop") << "B" << loop_info->GetHeader()->GetBlockId();
645 HLoopInformation* outer = loop_info->GetPreHeader()->GetLoopInformation();
646 if (outer != nullptr) {
647 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000648 } else {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100649 StartAttributeStream("outer_loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000650 }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100651 StartAttributeStream("irreducible")
652 << std::boolalpha << loop_info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000653 }
654
David Brazdilbadd8262016-02-02 16:28:56 +0000655 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000656 || IsPass(HInliner::kInlinerPassName))
Calin Juravle2e768302015-07-28 14:41:11 +0000657 && (instruction->GetType() == Primitive::kPrimNot)) {
658 ReferenceTypeInfo info = instruction->IsLoadClass()
659 ? instruction->AsLoadClass()->GetLoadedClassRTI()
660 : instruction->GetReferenceTypeInfo();
661 ScopedObjectAccess soa(Thread::Current());
662 if (info.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700663 StartAttributeStream("klass")
664 << mirror::Class::PrettyDescriptor(info.GetTypeHandle().Get());
Calin Juravle2e768302015-07-28 14:41:11 +0000665 StartAttributeStream("can_be_null")
666 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
667 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Calin Juravle98893e12015-10-02 21:05:03 +0100668 } else if (instruction->IsLoadClass()) {
669 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000670 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500671 // The NullConstant may be added to the graph during other passes that happen between
672 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
673 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
674 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000675 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000676 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000677 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
678 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100679 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100680 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100681 if (disasm_info_ != nullptr) {
682 DCHECK(disassembler_ != nullptr);
683 // If the information is available, disassemble the code generated for
684 // this instruction.
685 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
686 if (it != disasm_info_->GetInstructionIntervals().end()
687 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100688 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100689 disassembler_->Disassemble(output_, it->second.start, it->second.end);
690 }
691 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100692 }
693
694 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100695 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
696 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100697 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100698 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000699 AddIndent();
700 output_ << bci << " " << num_uses << " "
701 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000702 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100703 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100704 }
705 }
706
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100707 void DumpStartOfDisassemblyBlock(const char* block_name,
708 int predecessor_index,
709 int successor_index) {
710 StartTag("block");
711 PrintProperty("name", block_name);
712 PrintInt("from_bci", -1);
713 PrintInt("to_bci", -1);
714 if (predecessor_index != -1) {
715 PrintProperty("predecessors", "B", predecessor_index);
716 } else {
717 PrintEmptyProperty("predecessors");
718 }
719 if (successor_index != -1) {
720 PrintProperty("successors", "B", successor_index);
721 } else {
722 PrintEmptyProperty("successors");
723 }
724 PrintEmptyProperty("xhandlers");
725 PrintEmptyProperty("flags");
726 StartTag("states");
727 StartTag("locals");
728 PrintInt("size", 0);
729 PrintProperty("method", "None");
730 EndTag("locals");
731 EndTag("states");
732 StartTag("HIR");
733 }
734
735 void DumpEndOfDisassemblyBlock() {
736 EndTag("HIR");
737 EndTag("block");
738 }
739
740 void DumpDisassemblyBlockForFrameEntry() {
741 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
742 -1,
743 GetGraph()->GetEntryBlock()->GetBlockId());
744 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
745 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
746 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100747 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100748 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
749 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100750 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100751 DumpEndOfDisassemblyBlock();
752 }
753
754 void DumpDisassemblyBlockForSlowPaths() {
755 if (disasm_info_->GetSlowPathIntervals().empty()) {
756 return;
757 }
758 // If the graph has an exit block we attach the block for the slow paths
759 // after it. Else we just add the block to the graph without linking it to
760 // any other.
761 DumpStartOfDisassemblyBlock(
762 kDisassemblyBlockSlowPaths,
763 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
764 -1);
765 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100766 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100767 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100768 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100769 }
770 DumpEndOfDisassemblyBlock();
771 }
772
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100773 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100774 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100775 std::string pass_desc = std::string(pass_name_)
776 + " ("
777 + (is_after_pass_ ? "after" : "before")
778 + (graph_in_bad_state_ ? ", bad_state" : "")
779 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000780 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100781 if (disasm_info_ != nullptr) {
782 DumpDisassemblyBlockForFrameEntry();
783 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100784 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100785 if (disasm_info_ != nullptr) {
786 DumpDisassemblyBlockForSlowPaths();
787 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100788 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100789 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100790 }
791
David Brazdilb7e4a062014-12-29 15:35:02 +0000792 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100793 StartTag("block");
794 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100795 if (block->GetLifetimeStart() != kNoLifetime) {
796 // Piggy back on these fields to show the lifetime of the block.
797 PrintInt("from_bci", block->GetLifetimeStart());
798 PrintInt("to_bci", block->GetLifetimeEnd());
799 } else {
800 PrintInt("from_bci", -1);
801 PrintInt("to_bci", -1);
802 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100803 PrintPredecessors(block);
804 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000805 PrintExceptionHandlers(block);
806
807 if (block->IsCatchBlock()) {
808 PrintProperty("flags", "catch_block");
809 } else {
810 PrintEmptyProperty("flags");
811 }
812
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100813 if (block->GetDominator() != nullptr) {
814 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
815 }
816
817 StartTag("states");
818 StartTag("locals");
819 PrintInt("size", 0);
820 PrintProperty("method", "None");
821 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
822 AddIndent();
823 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100824 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
825 << instruction->GetId() << "[ ";
Vladimir Marko372f10e2016-05-17 16:30:10 +0100826 for (const HInstruction* input : instruction->GetInputs()) {
827 output_ << input->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100828 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100829 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100830 }
831 EndTag("locals");
832 EndTag("states");
833
834 StartTag("HIR");
835 PrintInstructions(block->GetPhis());
836 PrintInstructions(block->GetInstructions());
837 EndTag("HIR");
838 EndTag("block");
839 }
840
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100841 static constexpr const char* const kEndInstructionMarker = "<|@";
842 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
843 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
844
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100845 private:
846 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100847 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000848 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100849 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100850 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100851 const DisassemblyInformation* disasm_info_;
852 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100853 size_t indent_;
854
855 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
856};
857
858HGraphVisualizer::HGraphVisualizer(std::ostream* output,
859 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100860 const CodeGenerator& codegen)
861 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100862
David Brazdil62e074f2015-04-07 18:09:37 +0100863void HGraphVisualizer::PrintHeader(const char* method_name) const {
864 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100865 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100866 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000867 printer.PrintProperty("name", method_name);
868 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100869 printer.PrintTime("date");
870 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100871 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100872}
873
David Brazdilffee3d32015-07-06 11:48:53 +0100874void HGraphVisualizer::DumpGraph(const char* pass_name,
875 bool is_after_pass,
876 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000877 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100878 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100879 HGraphVisualizerPrinter printer(graph_,
880 *output_,
881 pass_name,
882 is_after_pass,
883 graph_in_bad_state,
884 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000885 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100886 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100887}
888
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100889void HGraphVisualizer::DumpGraphWithDisassembly() const {
890 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100891 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100892 HGraphVisualizerPrinter printer(graph_,
893 *output_,
894 "disassembly",
895 /* is_after_pass */ true,
896 /* graph_in_bad_state */ false,
897 codegen_,
898 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100899 printer.Run();
900 }
901}
902
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100903} // namespace art