blob: 5ff31cead589b7b90367c9ec63edfcf7527faa8a [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
Vladimir Marko69d310e2017-10-09 14:12:23 +010024#include "art_method.h"
Aart Bik09e8d5f2016-01-22 16:49:55 -080025#include "bounds_check_elimination.h"
David Brazdilbadd8262016-02-02 16:28:56 +000026#include "builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010027#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010028#include "data_type-inl.h"
David Brazdila4b8c212015-05-07 09:59:30 +010029#include "dead_code_elimination.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080030#include "dex/descriptors_names.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010031#include "disassembler.h"
Calin Juravlecdfed3d2015-10-26 14:05:01 +000032#include "inliner.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080033#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010034#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000035#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010036#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070037#include "register_allocator_linear_scan.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010038#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010039#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010040#include "utils/assembler.h"
Vladimir Marko82b07402017-03-01 19:02:04 +000041#include "utils/intrusive_forward_list.h"
David Brazdilc74652862015-05-13 17:50:09 +010042
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010043namespace art {
44
David Brazdilc74652862015-05-13 17:50:09 +010045static bool HasWhitespace(const char* str) {
46 DCHECK(str != nullptr);
47 while (str[0] != 0) {
48 if (isspace(str[0])) {
49 return true;
50 }
51 str++;
52 }
53 return false;
54}
55
56class StringList {
57 public:
David Brazdilc7a24852015-05-15 16:44:05 +010058 enum Format {
59 kArrayBrackets,
60 kSetBrackets,
61 };
62
David Brazdilc74652862015-05-13 17:50:09 +010063 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010064 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010065
66 // Construct StringList from a linked list. List element class T
67 // must provide methods `GetNext` and `Dump`.
68 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010069 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010070 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
71 current->Dump(NewEntryStream());
72 }
73 }
Vladimir Marko82b07402017-03-01 19:02:04 +000074 // Construct StringList from a list of elements. The value type must provide method `Dump`.
75 template <typename Container>
76 explicit StringList(const Container& list, Format format = kArrayBrackets) : StringList(format) {
77 for (const typename Container::value_type& current : list) {
78 current.Dump(NewEntryStream());
79 }
80 }
David Brazdilc74652862015-05-13 17:50:09 +010081
82 std::ostream& NewEntryStream() {
83 if (is_empty_) {
84 is_empty_ = false;
85 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010086 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010087 }
88 return sstream_;
89 }
90
91 private:
David Brazdilc7a24852015-05-15 16:44:05 +010092 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010093 bool is_empty_;
94 std::ostringstream sstream_;
95
96 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
97};
98
99std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +0100100 switch (list.format_) {
101 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
102 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
103 default:
104 LOG(FATAL) << "Invalid StringList format";
105 UNREACHABLE();
106 }
David Brazdilc74652862015-05-13 17:50:09 +0100107}
108
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100109typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
110 DisassemblerOptions* options);
111class HGraphVisualizerDisassembler {
112 public:
Aart Bikd3059e72016-05-11 10:30:47 -0700113 HGraphVisualizerDisassembler(InstructionSet instruction_set,
114 const uint8_t* base_address,
115 const uint8_t* end_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100116 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100117 libart_disassembler_handle_ =
118 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
119 if (libart_disassembler_handle_ == nullptr) {
120 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
121 return;
122 }
123 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
124 dlsym(libart_disassembler_handle_, "create_disassembler"));
125 if (create_disassembler == nullptr) {
126 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
127 return;
128 }
129 // Reading the disassembly from 0x0 is easier, so we print relative
130 // addresses. We will only disassemble the code once everything has
131 // been generated, so we can read data in literal pools.
132 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
133 instruction_set,
134 new DisassemblerOptions(/* absolute_addresses */ false,
135 base_address,
Aart Bikd3059e72016-05-11 10:30:47 -0700136 end_address,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700137 /* can_read_literals */ true,
138 Is64BitInstructionSet(instruction_set)
139 ? &Thread::DumpThreadOffset<PointerSize::k64>
140 : &Thread::DumpThreadOffset<PointerSize::k32>)));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100141 }
142
143 ~HGraphVisualizerDisassembler() {
144 // We need to call ~Disassembler() before we close the library.
145 disassembler_.reset();
146 if (libart_disassembler_handle_ != nullptr) {
147 dlclose(libart_disassembler_handle_);
148 }
149 }
150
151 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100152 if (disassembler_ == nullptr) {
153 return;
154 }
155
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100156 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
Vladimir Marko33bff252017-11-01 14:35:42 +0000157 if (instruction_set_ == InstructionSet::kThumb2) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100158 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
159 // address is used to distinguish between the two.
160 base += 1;
161 }
162 disassembler_->Dump(output, base + start, base + end);
163 }
164
165 private:
166 InstructionSet instruction_set_;
167 std::unique_ptr<Disassembler> disassembler_;
168
169 void* libart_disassembler_handle_;
170};
171
172
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100173/**
174 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
175 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100176class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100177 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100178 HGraphVisualizerPrinter(HGraph* graph,
179 std::ostream& output,
180 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000181 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100182 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100183 const CodeGenerator& codegen,
184 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100185 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100186 output_(output),
187 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000188 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100189 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100190 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100191 disasm_info_(disasm_info),
192 disassembler_(disasm_info_ != nullptr
193 ? new HGraphVisualizerDisassembler(
194 codegen_.GetInstructionSet(),
Aart Bikd3059e72016-05-11 10:30:47 -0700195 codegen_.GetAssembler().CodeBufferBaseAddress(),
196 codegen_.GetAssembler().CodeBufferBaseAddress()
197 + codegen_.GetAssembler().CodeSize())
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100198 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100199 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100200
David Brazdilfa02c9d2016-03-30 09:41:02 +0100201 void Flush() {
202 // We use "\n" instead of std::endl to avoid implicit flushing which
203 // generates too many syscalls during debug-GC tests (b/27826765).
204 output_ << std::flush;
205 }
206
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100207 void StartTag(const char* name) {
208 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100209 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100210 indent_++;
211 }
212
213 void EndTag(const char* name) {
214 indent_--;
215 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100216 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100217 }
218
219 void PrintProperty(const char* name, const char* property) {
220 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100221 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100222 }
223
224 void PrintProperty(const char* name, const char* property, int id) {
225 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100226 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100227 }
228
229 void PrintEmptyProperty(const char* name) {
230 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100231 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100232 }
233
234 void PrintTime(const char* name) {
235 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100236 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100237 }
238
239 void PrintInt(const char* name, int value) {
240 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100241 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100242 }
243
244 void AddIndent() {
245 for (size_t i = 0; i < indent_; ++i) {
246 output_ << " ";
247 }
248 }
249
250 void PrintPredecessors(HBasicBlock* block) {
251 AddIndent();
252 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000253 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100254 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
255 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100256 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
257 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
258 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100259 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100260 }
261
262 void PrintSuccessors(HBasicBlock* block) {
263 AddIndent();
264 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000265 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100266 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000267 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100268 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000269 }
270
271 void PrintExceptionHandlers(HBasicBlock* block) {
272 AddIndent();
273 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000274 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100275 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100276 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100277 if (block->IsExitBlock() &&
278 (disasm_info_ != nullptr) &&
279 !disasm_info_->GetSlowPathIntervals().empty()) {
280 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
281 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100282 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100283 }
284
David Brazdilc74652862015-05-13 17:50:09 +0100285 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100286 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100287 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100288 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100289 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100290 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100291 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100292 HConstant* constant = location.GetConstant();
293 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100294 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100295 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100296 stream << constant->AsLongConstant()->GetValue();
Alexandre Ramesc2c52a12016-08-02 13:45:28 +0100297 } else if (constant->IsFloatConstant()) {
298 stream << constant->AsFloatConstant()->GetValue();
299 } else if (constant->IsDoubleConstant()) {
300 stream << constant->AsDoubleConstant()->GetValue();
301 } else if (constant->IsNullConstant()) {
302 stream << "null";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100303 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100304 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100305 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100306 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100307 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000308 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100309 codegen_.DumpFloatingPointRegister(stream, location.low());
310 stream << "|";
311 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000312 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100313 codegen_.DumpCoreRegister(stream, location.low());
314 stream << "|";
315 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400316 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100317 stream << "unallocated";
Aart Bik5576f372017-03-23 16:17:37 -0700318 } else if (location.IsDoubleStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100319 stream << "2x" << location.GetStackIndex() << "(sp)";
Aart Bik5576f372017-03-23 16:17:37 -0700320 } else {
321 DCHECK(location.IsSIMDStackSlot());
322 stream << "4x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100323 }
324 }
325
David Brazdilc74652862015-05-13 17:50:09 +0100326 std::ostream& StartAttributeStream(const char* name = nullptr) {
327 if (name == nullptr) {
328 output_ << " ";
329 } else {
330 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
331 output_ << " " << name << ":";
332 }
333 return output_;
334 }
335
David Brazdilb7e4a062014-12-29 15:35:02 +0000336 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100337 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
338 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100339 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
340 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100341 std::ostream& str = moves.NewEntryStream();
342 DumpLocation(str, move->GetSource());
343 str << "->";
344 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100345 }
David Brazdilc74652862015-05-13 17:50:09 +0100346 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100347 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100348
David Brazdil36cf0952015-01-08 19:28:33 +0000349 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100350 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000351 }
352
David Brazdil36cf0952015-01-08 19:28:33 +0000353 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100354 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000355 }
356
David Brazdil36cf0952015-01-08 19:28:33 +0000357 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100358 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000359 }
360
David Brazdil36cf0952015-01-08 19:28:33 +0000361 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100362 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000363 }
364
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000365 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100366 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100367 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000368 }
369
Calin Juravle27df7582015-04-17 19:12:31 +0100370 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100371 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100372 }
373
David Brazdilbff75032015-07-08 17:26:51 +0000374 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
375 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
376 }
377
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100378 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100379 StartAttributeStream("load_kind") << load_class->GetLoadKind();
380 const char* descriptor = load_class->GetDexFile().GetTypeDescriptor(
381 load_class->GetDexFile().GetTypeId(load_class->GetTypeIndex()));
382 StartAttributeStream("class_name") << PrettyDescriptor(descriptor);
Calin Juravle0ba218d2015-05-19 18:46:01 +0100383 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100384 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100385 StartAttributeStream("needs_access_check") << std::boolalpha
386 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100387 }
388
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000389 void VisitLoadString(HLoadString* load_string) OVERRIDE {
390 StartAttributeStream("load_kind") << load_string->GetLoadKind();
391 }
392
Vladimir Marko3f413232018-02-12 18:39:15 +0000393 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000394 StartAttributeStream("check_kind") << check_cast->GetTypeCheckKind();
395 StartAttributeStream("must_do_null_check") << std::boolalpha
396 << check_cast->MustDoNullCheck() << std::noboolalpha;
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100397 }
398
399 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000400 StartAttributeStream("check_kind") << instance_of->GetTypeCheckKind();
401 StartAttributeStream("must_do_null_check") << std::boolalpha
402 << instance_of->MustDoNullCheck() << std::noboolalpha;
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100403 }
404
Vladimir Markodce016e2016-04-28 13:10:02 +0100405 void VisitArrayLength(HArrayLength* array_length) OVERRIDE {
406 StartAttributeStream("is_string_length") << std::boolalpha
407 << array_length->IsStringLength() << std::noboolalpha;
Mark Mendellee8d9712016-07-12 11:13:15 -0400408 if (array_length->IsEmittedAtUseSite()) {
409 StartAttributeStream("emitted_at_use") << "true";
410 }
Vladimir Markodce016e2016-04-28 13:10:02 +0100411 }
412
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100413 void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE {
414 StartAttributeStream("is_string_char_at") << std::boolalpha
415 << bounds_check->IsStringCharAt() << std::noboolalpha;
416 }
417
418 void VisitArrayGet(HArrayGet* array_get) OVERRIDE {
419 StartAttributeStream("is_string_char_at") << std::boolalpha
420 << array_get->IsStringCharAt() << std::noboolalpha;
421 }
422
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100423 void VisitArraySet(HArraySet* array_set) OVERRIDE {
424 StartAttributeStream("value_can_be_null") << std::boolalpha
425 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000426 StartAttributeStream("needs_type_check") << std::boolalpha
427 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100428 }
429
Roland Levillain31dd3d62016-02-16 12:21:02 +0000430 void VisitCompare(HCompare* compare) OVERRIDE {
431 ComparisonBias bias = compare->GetBias();
432 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
433 ? "gt"
434 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
435 }
436
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100437 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100438 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray5ceac0e2017-06-26 13:19:09 +0100439 ArtMethod* method = invoke->GetResolvedMethod();
440 // We don't print signatures, which conflict with c1visualizer format.
441 static constexpr bool kWithSignature = false;
442 // Note that we can only use the graph's dex file for the unresolved case. The
443 // other invokes might be coming from inlined methods.
444 ScopedObjectAccess soa(Thread::Current());
445 std::string method_name = (method == nullptr)
446 ? GetGraph()->GetDexFile().PrettyMethod(invoke->GetDexMethodIndex(), kWithSignature)
447 : method->PrettyMethod(kWithSignature);
448 StartAttributeStream("method_name") << method_name;
Aart Bik2c148f02018-02-02 14:30:35 -0800449 StartAttributeStream("always_throws") << std::boolalpha
450 << invoke->AlwaysThrows()
451 << std::noboolalpha;
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100452 }
453
Calin Juravle175dc732015-08-25 15:42:32 +0100454 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
455 VisitInvoke(invoke);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100456 StartAttributeStream("invoke_type") << invoke->GetInvokeType();
Calin Juravle175dc732015-08-25 15:42:32 +0100457 }
458
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100459 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
460 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000461 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100462 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000463 if (invoke->IsStatic()) {
464 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
465 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100466 }
467
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000468 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
469 VisitInvoke(invoke);
470 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
471 }
472
Orion Hodsonac141392017-01-13 11:53:47 +0000473 void VisitInvokePolymorphic(HInvokePolymorphic* invoke) OVERRIDE {
474 VisitInvoke(invoke);
475 StartAttributeStream("invoke_type") << "InvokePolymorphic";
476 }
477
David Brazdil11edec72016-03-24 12:40:52 +0000478 void VisitInstanceFieldGet(HInstanceFieldGet* iget) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700479 StartAttributeStream("field_name") <<
480 iget->GetFieldInfo().GetDexFile().PrettyField(iget->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000481 /* with type */ false);
482 StartAttributeStream("field_type") << iget->GetFieldType();
483 }
484
485 void VisitInstanceFieldSet(HInstanceFieldSet* iset) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700486 StartAttributeStream("field_name") <<
487 iset->GetFieldInfo().GetDexFile().PrettyField(iset->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000488 /* with type */ false);
489 StartAttributeStream("field_type") << iset->GetFieldType();
490 }
491
Vladimir Markobf3243b2017-08-30 14:06:54 +0100492 void VisitStaticFieldGet(HStaticFieldGet* sget) OVERRIDE {
493 StartAttributeStream("field_name") <<
494 sget->GetFieldInfo().GetDexFile().PrettyField(sget->GetFieldInfo().GetFieldIndex(),
495 /* with type */ false);
496 StartAttributeStream("field_type") << sget->GetFieldType();
497 }
498
499 void VisitStaticFieldSet(HStaticFieldSet* sset) OVERRIDE {
500 StartAttributeStream("field_name") <<
501 sset->GetFieldInfo().GetDexFile().PrettyField(sset->GetFieldInfo().GetFieldIndex(),
502 /* with type */ false);
503 StartAttributeStream("field_type") << sset->GetFieldType();
504 }
505
Calin Juravlee460d1d2015-09-29 04:52:17 +0100506 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
507 StartAttributeStream("field_type") << field_access->GetFieldType();
508 }
509
510 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
511 StartAttributeStream("field_type") << field_access->GetFieldType();
512 }
513
514 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
515 StartAttributeStream("field_type") << field_access->GetFieldType();
516 }
517
518 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
519 StartAttributeStream("field_type") << field_access->GetFieldType();
520 }
521
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100523 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000524 }
525
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000526 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE {
527 StartAttributeStream("kind") << deoptimize->GetKind();
528 }
529
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100530 void VisitVecOperation(HVecOperation* vec_operation) OVERRIDE {
531 StartAttributeStream("packed_type") << vec_operation->GetPackedType();
532 }
533
Aart Bik38a3f212017-10-20 17:02:21 -0700534 void VisitVecMemoryOperation(HVecMemoryOperation* vec_mem_operation) OVERRIDE {
535 StartAttributeStream("alignment") << vec_mem_operation->GetAlignment().ToString();
536 }
537
Aart Bikf3e61ee2017-04-12 17:09:20 -0700538 void VisitVecHalvingAdd(HVecHalvingAdd* hadd) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100539 VisitVecBinaryOperation(hadd);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700540 StartAttributeStream("rounded") << std::boolalpha << hadd->IsRounded() << std::noboolalpha;
541 }
542
Artem Serovf34dd202017-04-10 17:41:46 +0100543 void VisitVecMultiplyAccumulate(HVecMultiplyAccumulate* instruction) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100544 VisitVecOperation(instruction);
Artem Serovf34dd202017-04-10 17:41:46 +0100545 StartAttributeStream("kind") << instruction->GetOpKind();
546 }
547
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300548#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
549 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) OVERRIDE {
550 StartAttributeStream("kind") << instruction->GetOpKind();
551 }
Artem Serov7fc63502016-02-09 17:15:29 +0000552
553 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) OVERRIDE {
554 StartAttributeStream("kind") << instruction->GetOpKind();
555 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300556
Anton Kirilov74234da2017-01-13 14:42:47 +0000557 void VisitDataProcWithShifterOp(HDataProcWithShifterOp* instruction) OVERRIDE {
Alexandre Rames8626b742015-11-25 16:28:08 +0000558 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
Anton Kirilov74234da2017-01-13 14:42:47 +0000559 if (HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000560 StartAttributeStream("shift") << instruction->GetShiftAmount();
561 }
562 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000563#endif
564
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800565 bool IsPass(const char* name) {
566 return strcmp(pass_name_, name) == 0;
567 }
568
David Brazdilb7e4a062014-12-29 15:35:02 +0000569 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100570 output_ << instruction->DebugName();
Vladimir Markoe9004912016-06-16 16:50:52 +0100571 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100572 if (!inputs.empty()) {
573 StringList input_list;
574 for (const HInstruction* input : inputs) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100575 input_list.NewEntryStream() << DataType::TypeId(input->GetType()) << input->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100576 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100577 StartAttributeStream() << input_list;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100578 }
David Brazdilc74652862015-05-13 17:50:09 +0100579 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800580 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100581 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100582 for (HEnvironment* environment = instruction->GetEnvironment();
583 environment != nullptr;
584 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100585 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100586 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
587 HInstruction* insn = environment->GetInstructionAt(i);
588 if (insn != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100589 vregs.NewEntryStream() << DataType::TypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100590 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100591 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100592 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800593 }
David Brazdilc74652862015-05-13 17:50:09 +0100594 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800595 }
David Brazdilc74652862015-05-13 17:50:09 +0100596 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800597 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800598 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000599 && is_after_pass_
600 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100601 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100602 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100603 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100604 StartAttributeStream("ranges")
605 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
Vladimir Marko82b07402017-03-01 19:02:04 +0000606 StartAttributeStream("uses") << StringList(interval->GetUses());
607 StartAttributeStream("env_uses") << StringList(interval->GetEnvironmentUses());
David Brazdilc74652862015-05-13 17:50:09 +0100608 StartAttributeStream("is_fixed") << interval->IsFixed();
609 StartAttributeStream("is_split") << interval->IsSplit();
610 StartAttributeStream("is_low") << interval->IsLowInterval();
611 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100612 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000613 }
614
615 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100616 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100617 LocationSummary* locations = instruction->GetLocations();
618 if (locations != nullptr) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100619 StringList input_list;
620 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
621 DumpLocation(input_list.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100622 }
David Brazdilc74652862015-05-13 17:50:09 +0100623 std::ostream& attr = StartAttributeStream("locations");
Vladimir Marko372f10e2016-05-17 16:30:10 +0100624 attr << input_list << "->";
David Brazdilc74652862015-05-13 17:50:09 +0100625 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100626 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000627 }
628
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100629 HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
630 if (loop_info == nullptr) {
631 StartAttributeStream("loop") << "none";
632 } else {
633 StartAttributeStream("loop") << "B" << loop_info->GetHeader()->GetBlockId();
634 HLoopInformation* outer = loop_info->GetPreHeader()->GetLoopInformation();
635 if (outer != nullptr) {
636 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000637 } else {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100638 StartAttributeStream("outer_loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000639 }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100640 StartAttributeStream("irreducible")
641 << std::boolalpha << loop_info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000642 }
643
David Brazdilbadd8262016-02-02 16:28:56 +0000644 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000645 || IsPass(HInliner::kInlinerPassName))
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000646 && (instruction->GetType() == DataType::Type::kReference)) {
647 ReferenceTypeInfo info = instruction->IsLoadClass()
648 ? instruction->AsLoadClass()->GetLoadedClassRTI()
649 : instruction->GetReferenceTypeInfo();
Calin Juravle2e768302015-07-28 14:41:11 +0000650 ScopedObjectAccess soa(Thread::Current());
651 if (info.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700652 StartAttributeStream("klass")
653 << mirror::Class::PrettyDescriptor(info.GetTypeHandle().Get());
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000654 StartAttributeStream("can_be_null")
655 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
Calin Juravle2e768302015-07-28 14:41:11 +0000656 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +0000657 } else if (instruction->IsLoadClass()) {
Calin Juravle98893e12015-10-02 21:05:03 +0100658 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000659 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500660 // The NullConstant may be added to the graph during other passes that happen between
661 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
662 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
663 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000664 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000665 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000666 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
667 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100668 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100669 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100670 if (disasm_info_ != nullptr) {
671 DCHECK(disassembler_ != nullptr);
672 // If the information is available, disassemble the code generated for
673 // this instruction.
674 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
675 if (it != disasm_info_->GetInstructionIntervals().end()
676 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100677 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100678 disassembler_->Disassemble(output_, it->second.start, it->second.end);
679 }
680 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100681 }
682
683 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100684 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
685 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100686 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100687 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000688 AddIndent();
689 output_ << bci << " " << num_uses << " "
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100690 << DataType::TypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000691 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100692 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100693 }
694 }
695
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100696 void DumpStartOfDisassemblyBlock(const char* block_name,
697 int predecessor_index,
698 int successor_index) {
699 StartTag("block");
700 PrintProperty("name", block_name);
701 PrintInt("from_bci", -1);
702 PrintInt("to_bci", -1);
703 if (predecessor_index != -1) {
704 PrintProperty("predecessors", "B", predecessor_index);
705 } else {
706 PrintEmptyProperty("predecessors");
707 }
708 if (successor_index != -1) {
709 PrintProperty("successors", "B", successor_index);
710 } else {
711 PrintEmptyProperty("successors");
712 }
713 PrintEmptyProperty("xhandlers");
714 PrintEmptyProperty("flags");
715 StartTag("states");
716 StartTag("locals");
717 PrintInt("size", 0);
718 PrintProperty("method", "None");
719 EndTag("locals");
720 EndTag("states");
721 StartTag("HIR");
722 }
723
724 void DumpEndOfDisassemblyBlock() {
725 EndTag("HIR");
726 EndTag("block");
727 }
728
729 void DumpDisassemblyBlockForFrameEntry() {
730 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
731 -1,
732 GetGraph()->GetEntryBlock()->GetBlockId());
733 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
734 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
735 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100736 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100737 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
738 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100739 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100740 DumpEndOfDisassemblyBlock();
741 }
742
743 void DumpDisassemblyBlockForSlowPaths() {
744 if (disasm_info_->GetSlowPathIntervals().empty()) {
745 return;
746 }
747 // If the graph has an exit block we attach the block for the slow paths
748 // after it. Else we just add the block to the graph without linking it to
749 // any other.
750 DumpStartOfDisassemblyBlock(
751 kDisassemblyBlockSlowPaths,
752 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
753 -1);
754 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100755 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100756 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100757 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100758 }
759 DumpEndOfDisassemblyBlock();
760 }
761
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100762 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100763 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100764 std::string pass_desc = std::string(pass_name_)
765 + " ("
766 + (is_after_pass_ ? "after" : "before")
767 + (graph_in_bad_state_ ? ", bad_state" : "")
768 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000769 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100770 if (disasm_info_ != nullptr) {
771 DumpDisassemblyBlockForFrameEntry();
772 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100773 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100774 if (disasm_info_ != nullptr) {
775 DumpDisassemblyBlockForSlowPaths();
776 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100777 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100778 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100779 }
780
David Brazdilb7e4a062014-12-29 15:35:02 +0000781 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100782 StartTag("block");
783 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100784 if (block->GetLifetimeStart() != kNoLifetime) {
785 // Piggy back on these fields to show the lifetime of the block.
786 PrintInt("from_bci", block->GetLifetimeStart());
787 PrintInt("to_bci", block->GetLifetimeEnd());
788 } else {
789 PrintInt("from_bci", -1);
790 PrintInt("to_bci", -1);
791 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100792 PrintPredecessors(block);
793 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000794 PrintExceptionHandlers(block);
795
796 if (block->IsCatchBlock()) {
797 PrintProperty("flags", "catch_block");
798 } else {
799 PrintEmptyProperty("flags");
800 }
801
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100802 if (block->GetDominator() != nullptr) {
803 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
804 }
805
806 StartTag("states");
807 StartTag("locals");
808 PrintInt("size", 0);
809 PrintProperty("method", "None");
810 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
811 AddIndent();
812 HInstruction* instruction = it.Current();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100813 output_ << instruction->GetId() << " " << DataType::TypeId(instruction->GetType())
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100814 << instruction->GetId() << "[ ";
Vladimir Marko372f10e2016-05-17 16:30:10 +0100815 for (const HInstruction* input : instruction->GetInputs()) {
816 output_ << input->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100817 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100818 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100819 }
820 EndTag("locals");
821 EndTag("states");
822
823 StartTag("HIR");
824 PrintInstructions(block->GetPhis());
825 PrintInstructions(block->GetInstructions());
826 EndTag("HIR");
827 EndTag("block");
828 }
829
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100830 static constexpr const char* const kEndInstructionMarker = "<|@";
831 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
832 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
833
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100834 private:
835 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100836 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000837 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100838 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100839 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100840 const DisassemblyInformation* disasm_info_;
841 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100842 size_t indent_;
843
844 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
845};
846
847HGraphVisualizer::HGraphVisualizer(std::ostream* output,
848 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100849 const CodeGenerator& codegen)
850 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100851
David Brazdil62e074f2015-04-07 18:09:37 +0100852void HGraphVisualizer::PrintHeader(const char* method_name) const {
853 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100854 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100855 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000856 printer.PrintProperty("name", method_name);
857 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100858 printer.PrintTime("date");
859 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100860 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100861}
862
David Brazdilffee3d32015-07-06 11:48:53 +0100863void HGraphVisualizer::DumpGraph(const char* pass_name,
864 bool is_after_pass,
865 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000866 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100867 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100868 HGraphVisualizerPrinter printer(graph_,
869 *output_,
870 pass_name,
871 is_after_pass,
872 graph_in_bad_state,
873 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000874 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100875 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100876}
877
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100878void HGraphVisualizer::DumpGraphWithDisassembly() const {
879 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100881 HGraphVisualizerPrinter printer(graph_,
882 *output_,
883 "disassembly",
884 /* is_after_pass */ true,
885 /* graph_in_bad_state */ false,
886 codegen_,
887 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100888 printer.Run();
889 }
890}
891
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100892} // namespace art