blob: d94c1fa2fb48a9068ea842eb110808d1e84d6ff1 [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"
Vladimir Marko3fae1292019-06-07 11:26:25 +010025#include "base/intrusive_forward_list.h"
Aart Bik09e8d5f2016-01-22 16:49:55 -080026#include "bounds_check_elimination.h"
David Brazdilbadd8262016-02-02 16:28:56 +000027#include "builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010028#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010029#include "data_type-inl.h"
David Brazdila4b8c212015-05-07 09:59:30 +010030#include "dead_code_elimination.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080031#include "dex/descriptors_names.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010032#include "disassembler.h"
Calin Juravlecdfed3d2015-10-26 14:05:01 +000033#include "inliner.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080034#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010035#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000036#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010037#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070038#include "register_allocator_linear_scan.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010039#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010040#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010041#include "utils/assembler.h"
David Brazdilc74652862015-05-13 17:50:09 +010042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010044
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
Andreas Gampec55bb392018-09-21 00:02:02 +0000109using create_disasm_prototype = Disassembler*(InstructionSet, DisassemblerOptions*);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100110class HGraphVisualizerDisassembler {
111 public:
Aart Bikd3059e72016-05-11 10:30:47 -0700112 HGraphVisualizerDisassembler(InstructionSet instruction_set,
113 const uint8_t* base_address,
114 const uint8_t* end_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100115 : instruction_set_(instruction_set), disassembler_(nullptr) {
Roland Levillain5b768892020-02-19 15:49:02 +0000116 constexpr const char* libart_disassembler_so_name =
117 kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so";
118 libart_disassembler_handle_ = dlopen(libart_disassembler_so_name, RTLD_NOW);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100119 if (libart_disassembler_handle_ == nullptr) {
Roland Levillain5b768892020-02-19 15:49:02 +0000120 LOG(ERROR) << "Failed to dlopen " << libart_disassembler_so_name << ": " << dlerror();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100121 return;
122 }
Roland Levillain5b768892020-02-19 15:49:02 +0000123 constexpr const char* create_disassembler_symbol = "create_disassembler";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100124 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
Roland Levillain5b768892020-02-19 15:49:02 +0000125 dlsym(libart_disassembler_handle_, create_disassembler_symbol));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100126 if (create_disassembler == nullptr) {
Roland Levillain5b768892020-02-19 15:49:02 +0000127 LOG(ERROR) << "Could not find " << create_disassembler_symbol << " entry in "
128 << libart_disassembler_so_name << ": " << dlerror();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100129 return;
130 }
131 // Reading the disassembly from 0x0 is easier, so we print relative
132 // addresses. We will only disassemble the code once everything has
133 // been generated, so we can read data in literal pools.
134 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
135 instruction_set,
Andreas Gampe3db70682018-12-26 15:12:03 -0800136 new DisassemblerOptions(/* absolute_addresses= */ false,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100137 base_address,
Aart Bikd3059e72016-05-11 10:30:47 -0700138 end_address,
Andreas Gampe3db70682018-12-26 15:12:03 -0800139 /* can_read_literals= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700140 Is64BitInstructionSet(instruction_set)
141 ? &Thread::DumpThreadOffset<PointerSize::k64>
142 : &Thread::DumpThreadOffset<PointerSize::k32>)));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100143 }
144
145 ~HGraphVisualizerDisassembler() {
146 // We need to call ~Disassembler() before we close the library.
147 disassembler_.reset();
148 if (libart_disassembler_handle_ != nullptr) {
149 dlclose(libart_disassembler_handle_);
150 }
151 }
152
153 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100154 if (disassembler_ == nullptr) {
155 return;
156 }
157
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100158 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
Vladimir Marko33bff252017-11-01 14:35:42 +0000159 if (instruction_set_ == InstructionSet::kThumb2) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100160 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
161 // address is used to distinguish between the two.
162 base += 1;
163 }
164 disassembler_->Dump(output, base + start, base + end);
165 }
166
167 private:
168 InstructionSet instruction_set_;
169 std::unique_ptr<Disassembler> disassembler_;
170
171 void* libart_disassembler_handle_;
172};
173
174
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100175/**
176 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
177 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100178class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100179 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100180 HGraphVisualizerPrinter(HGraph* graph,
181 std::ostream& output,
182 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000183 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100184 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100185 const CodeGenerator& codegen,
186 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100187 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100188 output_(output),
189 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000190 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100191 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100192 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100193 disasm_info_(disasm_info),
194 disassembler_(disasm_info_ != nullptr
195 ? new HGraphVisualizerDisassembler(
196 codegen_.GetInstructionSet(),
Aart Bikd3059e72016-05-11 10:30:47 -0700197 codegen_.GetAssembler().CodeBufferBaseAddress(),
198 codegen_.GetAssembler().CodeBufferBaseAddress()
199 + codegen_.GetAssembler().CodeSize())
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100200 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100201 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100202
David Brazdilfa02c9d2016-03-30 09:41:02 +0100203 void Flush() {
204 // We use "\n" instead of std::endl to avoid implicit flushing which
205 // generates too many syscalls during debug-GC tests (b/27826765).
206 output_ << std::flush;
207 }
208
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100209 void StartTag(const char* name) {
210 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100211 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100212 indent_++;
213 }
214
215 void EndTag(const char* name) {
216 indent_--;
217 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100218 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100219 }
220
221 void PrintProperty(const char* name, const char* property) {
222 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100223 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100224 }
225
226 void PrintProperty(const char* name, const char* property, int id) {
227 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100228 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100229 }
230
231 void PrintEmptyProperty(const char* name) {
232 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100233 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100234 }
235
236 void PrintTime(const char* name) {
237 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100238 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100239 }
240
241 void PrintInt(const char* name, int value) {
242 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100243 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100244 }
245
246 void AddIndent() {
247 for (size_t i = 0; i < indent_; ++i) {
248 output_ << " ";
249 }
250 }
251
252 void PrintPredecessors(HBasicBlock* block) {
253 AddIndent();
254 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000255 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100256 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
257 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100258 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
259 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
260 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100261 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100262 }
263
264 void PrintSuccessors(HBasicBlock* block) {
265 AddIndent();
266 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000267 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100268 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000269 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100270 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000271 }
272
273 void PrintExceptionHandlers(HBasicBlock* block) {
274 AddIndent();
275 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000276 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100277 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100278 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100279 if (block->IsExitBlock() &&
280 (disasm_info_ != nullptr) &&
281 !disasm_info_->GetSlowPathIntervals().empty()) {
282 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
283 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100284 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100285 }
286
David Brazdilc74652862015-05-13 17:50:09 +0100287 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100288 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100289 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100290 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100291 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100292 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100293 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100294 HConstant* constant = location.GetConstant();
295 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100296 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100297 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100298 stream << constant->AsLongConstant()->GetValue();
Alexandre Ramesc2c52a12016-08-02 13:45:28 +0100299 } else if (constant->IsFloatConstant()) {
300 stream << constant->AsFloatConstant()->GetValue();
301 } else if (constant->IsDoubleConstant()) {
302 stream << constant->AsDoubleConstant()->GetValue();
303 } else if (constant->IsNullConstant()) {
304 stream << "null";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100305 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100306 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100307 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100308 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100309 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000310 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100311 codegen_.DumpFloatingPointRegister(stream, location.low());
312 stream << "|";
313 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000314 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100315 codegen_.DumpCoreRegister(stream, location.low());
316 stream << "|";
317 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400318 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100319 stream << "unallocated";
Aart Bik5576f372017-03-23 16:17:37 -0700320 } else if (location.IsDoubleStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100321 stream << "2x" << location.GetStackIndex() << "(sp)";
Aart Bik5576f372017-03-23 16:17:37 -0700322 } else {
323 DCHECK(location.IsSIMDStackSlot());
324 stream << "4x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100325 }
326 }
327
David Brazdilc74652862015-05-13 17:50:09 +0100328 std::ostream& StartAttributeStream(const char* name = nullptr) {
329 if (name == nullptr) {
330 output_ << " ";
331 } else {
332 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
333 output_ << " " << name << ":";
334 }
335 return output_;
336 }
337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100338 void VisitParallelMove(HParallelMove* instruction) override {
David Brazdilc74652862015-05-13 17:50:09 +0100339 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
340 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100341 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
342 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100343 std::ostream& str = moves.NewEntryStream();
344 DumpLocation(str, move->GetSource());
345 str << "->";
346 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100347 }
David Brazdilc74652862015-05-13 17:50:09 +0100348 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100349 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100350
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100351 void VisitIntConstant(HIntConstant* instruction) override {
David Brazdilc74652862015-05-13 17:50:09 +0100352 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000353 }
354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100355 void VisitLongConstant(HLongConstant* instruction) override {
David Brazdilc74652862015-05-13 17:50:09 +0100356 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000357 }
358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100359 void VisitFloatConstant(HFloatConstant* instruction) override {
David Brazdilc74652862015-05-13 17:50:09 +0100360 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000361 }
362
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100363 void VisitDoubleConstant(HDoubleConstant* instruction) override {
David Brazdilc74652862015-05-13 17:50:09 +0100364 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000365 }
366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100367 void VisitPhi(HPhi* phi) override {
David Brazdilc74652862015-05-13 17:50:09 +0100368 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100369 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000370 }
371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100372 void VisitMemoryBarrier(HMemoryBarrier* barrier) override {
David Brazdilc74652862015-05-13 17:50:09 +0100373 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100374 }
375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100376 void VisitMonitorOperation(HMonitorOperation* monitor) override {
David Brazdilbff75032015-07-08 17:26:51 +0000377 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
378 }
379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100380 void VisitLoadClass(HLoadClass* load_class) override {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100381 StartAttributeStream("load_kind") << load_class->GetLoadKind();
382 const char* descriptor = load_class->GetDexFile().GetTypeDescriptor(
383 load_class->GetDexFile().GetTypeId(load_class->GetTypeIndex()));
384 StartAttributeStream("class_name") << PrettyDescriptor(descriptor);
Calin Juravle0ba218d2015-05-19 18:46:01 +0100385 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100386 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100387 StartAttributeStream("needs_access_check") << std::boolalpha
388 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100389 }
390
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100391 void VisitLoadMethodHandle(HLoadMethodHandle* load_method_handle) override {
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100392 StartAttributeStream("load_kind") << "RuntimeCall";
393 StartAttributeStream("method_handle_index") << load_method_handle->GetMethodHandleIndex();
394 }
395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100396 void VisitLoadMethodType(HLoadMethodType* load_method_type) override {
Orion Hodson18259d72018-04-12 11:18:23 +0100397 StartAttributeStream("load_kind") << "RuntimeCall";
398 const DexFile& dex_file = load_method_type->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800399 const dex::ProtoId& proto_id = dex_file.GetProtoId(load_method_type->GetProtoIndex());
Orion Hodson18259d72018-04-12 11:18:23 +0100400 StartAttributeStream("method_type") << dex_file.GetProtoSignature(proto_id);
401 }
402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100403 void VisitLoadString(HLoadString* load_string) override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000404 StartAttributeStream("load_kind") << load_string->GetLoadKind();
405 }
406
Vladimir Marko175e7862018-03-27 09:03:13 +0000407 void HandleTypeCheckInstruction(HTypeCheckInstruction* check) {
408 StartAttributeStream("check_kind") << check->GetTypeCheckKind();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +0000409 StartAttributeStream("must_do_null_check") << std::boolalpha
Vladimir Marko175e7862018-03-27 09:03:13 +0000410 << check->MustDoNullCheck() << std::noboolalpha;
411 if (check->GetTypeCheckKind() == TypeCheckKind::kBitstringCheck) {
412 StartAttributeStream("path_to_root") << std::hex
413 << "0x" << check->GetBitstringPathToRoot() << std::dec;
414 StartAttributeStream("mask") << std::hex << "0x" << check->GetBitstringMask() << std::dec;
415 }
416 }
417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 void VisitCheckCast(HCheckCast* check_cast) override {
Vladimir Marko175e7862018-03-27 09:03:13 +0000419 HandleTypeCheckInstruction(check_cast);
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100420 }
421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100422 void VisitInstanceOf(HInstanceOf* instance_of) override {
Vladimir Marko175e7862018-03-27 09:03:13 +0000423 HandleTypeCheckInstruction(instance_of);
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100424 }
425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100426 void VisitArrayLength(HArrayLength* array_length) override {
Vladimir Markodce016e2016-04-28 13:10:02 +0100427 StartAttributeStream("is_string_length") << std::boolalpha
428 << array_length->IsStringLength() << std::noboolalpha;
Mark Mendellee8d9712016-07-12 11:13:15 -0400429 if (array_length->IsEmittedAtUseSite()) {
430 StartAttributeStream("emitted_at_use") << "true";
431 }
Vladimir Markodce016e2016-04-28 13:10:02 +0100432 }
433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100434 void VisitBoundsCheck(HBoundsCheck* bounds_check) override {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100435 StartAttributeStream("is_string_char_at") << std::boolalpha
436 << bounds_check->IsStringCharAt() << std::noboolalpha;
437 }
438
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100439 void VisitArrayGet(HArrayGet* array_get) override {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100440 StartAttributeStream("is_string_char_at") << std::boolalpha
441 << array_get->IsStringCharAt() << std::noboolalpha;
442 }
443
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100444 void VisitArraySet(HArraySet* array_set) override {
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100445 StartAttributeStream("value_can_be_null") << std::boolalpha
446 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000447 StartAttributeStream("needs_type_check") << std::boolalpha
448 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100449 }
450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100451 void VisitCompare(HCompare* compare) override {
Roland Levillain31dd3d62016-02-16 12:21:02 +0000452 ComparisonBias bias = compare->GetBias();
453 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
454 ? "gt"
455 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
456 }
457
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100458 void VisitInvoke(HInvoke* invoke) override {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100459 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray5ceac0e2017-06-26 13:19:09 +0100460 ArtMethod* method = invoke->GetResolvedMethod();
461 // We don't print signatures, which conflict with c1visualizer format.
462 static constexpr bool kWithSignature = false;
463 // Note that we can only use the graph's dex file for the unresolved case. The
464 // other invokes might be coming from inlined methods.
465 ScopedObjectAccess soa(Thread::Current());
466 std::string method_name = (method == nullptr)
467 ? GetGraph()->GetDexFile().PrettyMethod(invoke->GetDexMethodIndex(), kWithSignature)
468 : method->PrettyMethod(kWithSignature);
469 StartAttributeStream("method_name") << method_name;
Aart Bik2c148f02018-02-02 14:30:35 -0800470 StartAttributeStream("always_throws") << std::boolalpha
471 << invoke->AlwaysThrows()
472 << std::noboolalpha;
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100473 }
474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100475 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) override {
Calin Juravle175dc732015-08-25 15:42:32 +0100476 VisitInvoke(invoke);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100477 StartAttributeStream("invoke_type") << invoke->GetInvokeType();
Calin Juravle175dc732015-08-25 15:42:32 +0100478 }
479
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100480 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) override {
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100481 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000482 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100483 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000484 if (invoke->IsStatic()) {
485 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
486 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100487 }
488
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100489 void VisitInvokeVirtual(HInvokeVirtual* invoke) override {
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000490 VisitInvoke(invoke);
491 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
492 }
493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100494 void VisitInvokePolymorphic(HInvokePolymorphic* invoke) override {
Orion Hodsonac141392017-01-13 11:53:47 +0000495 VisitInvoke(invoke);
496 StartAttributeStream("invoke_type") << "InvokePolymorphic";
497 }
498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100499 void VisitInstanceFieldGet(HInstanceFieldGet* iget) override {
David Sehr709b0702016-10-13 09:12:37 -0700500 StartAttributeStream("field_name") <<
501 iget->GetFieldInfo().GetDexFile().PrettyField(iget->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000502 /* with type */ false);
503 StartAttributeStream("field_type") << iget->GetFieldType();
504 }
505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100506 void VisitInstanceFieldSet(HInstanceFieldSet* iset) override {
David Sehr709b0702016-10-13 09:12:37 -0700507 StartAttributeStream("field_name") <<
508 iset->GetFieldInfo().GetDexFile().PrettyField(iset->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000509 /* with type */ false);
510 StartAttributeStream("field_type") << iset->GetFieldType();
511 }
512
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100513 void VisitStaticFieldGet(HStaticFieldGet* sget) override {
Vladimir Markobf3243b2017-08-30 14:06:54 +0100514 StartAttributeStream("field_name") <<
515 sget->GetFieldInfo().GetDexFile().PrettyField(sget->GetFieldInfo().GetFieldIndex(),
516 /* with type */ false);
517 StartAttributeStream("field_type") << sget->GetFieldType();
518 }
519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100520 void VisitStaticFieldSet(HStaticFieldSet* sset) override {
Vladimir Markobf3243b2017-08-30 14:06:54 +0100521 StartAttributeStream("field_name") <<
522 sset->GetFieldInfo().GetDexFile().PrettyField(sset->GetFieldInfo().GetFieldIndex(),
523 /* with type */ false);
524 StartAttributeStream("field_type") << sset->GetFieldType();
525 }
526
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100527 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) override {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100528 StartAttributeStream("field_type") << field_access->GetFieldType();
529 }
530
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100531 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) override {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100532 StartAttributeStream("field_type") << field_access->GetFieldType();
533 }
534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100535 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) override {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100536 StartAttributeStream("field_type") << field_access->GetFieldType();
537 }
538
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100539 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) override {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100540 StartAttributeStream("field_type") << field_access->GetFieldType();
541 }
542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100543 void VisitTryBoundary(HTryBoundary* try_boundary) override {
David Brazdil56e1acc2015-06-30 15:41:36 +0100544 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000545 }
546
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100547 void VisitDeoptimize(HDeoptimize* deoptimize) override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000548 StartAttributeStream("kind") << deoptimize->GetKind();
549 }
550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100551 void VisitVecOperation(HVecOperation* vec_operation) override {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100552 StartAttributeStream("packed_type") << vec_operation->GetPackedType();
553 }
554
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100555 void VisitVecMemoryOperation(HVecMemoryOperation* vec_mem_operation) override {
Aart Bik38a3f212017-10-20 17:02:21 -0700556 StartAttributeStream("alignment") << vec_mem_operation->GetAlignment().ToString();
557 }
558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100559 void VisitVecHalvingAdd(HVecHalvingAdd* hadd) override {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100560 VisitVecBinaryOperation(hadd);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700561 StartAttributeStream("rounded") << std::boolalpha << hadd->IsRounded() << std::noboolalpha;
562 }
563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100564 void VisitVecMultiplyAccumulate(HVecMultiplyAccumulate* instruction) override {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100565 VisitVecOperation(instruction);
Artem Serovf34dd202017-04-10 17:41:46 +0100566 StartAttributeStream("kind") << instruction->GetOpKind();
567 }
568
Artem Serovaaac0e32018-08-07 00:52:22 +0100569 void VisitVecDotProd(HVecDotProd* instruction) override {
570 VisitVecOperation(instruction);
571 DataType::Type arg_type = instruction->InputAt(1)->AsVecOperation()->GetPackedType();
572 StartAttributeStream("type") << (instruction->IsZeroExtending() ?
573 DataType::ToUnsigned(arg_type) :
574 DataType::ToSigned(arg_type));
575 }
576
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300577#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100578 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) override {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300579 StartAttributeStream("kind") << instruction->GetOpKind();
580 }
Artem Serov7fc63502016-02-09 17:15:29 +0000581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100582 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) override {
Artem Serov7fc63502016-02-09 17:15:29 +0000583 StartAttributeStream("kind") << instruction->GetOpKind();
584 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300585
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100586 void VisitDataProcWithShifterOp(HDataProcWithShifterOp* instruction) override {
Alexandre Rames8626b742015-11-25 16:28:08 +0000587 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
Anton Kirilov74234da2017-01-13 14:42:47 +0000588 if (HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000589 StartAttributeStream("shift") << instruction->GetShiftAmount();
590 }
591 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000592#endif
593
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800594 bool IsPass(const char* name) {
595 return strcmp(pass_name_, name) == 0;
596 }
597
David Brazdilb7e4a062014-12-29 15:35:02 +0000598 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100599 output_ << instruction->DebugName();
Vladimir Markoe9004912016-06-16 16:50:52 +0100600 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100601 if (!inputs.empty()) {
602 StringList input_list;
603 for (const HInstruction* input : inputs) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 input_list.NewEntryStream() << DataType::TypeId(input->GetType()) << input->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100605 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100606 StartAttributeStream() << input_list;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100607 }
Vladimir Markofe948752018-03-23 18:11:43 +0000608 if (instruction->GetDexPc() != kNoDexPc) {
609 StartAttributeStream("dex_pc") << instruction->GetDexPc();
610 } else {
611 StartAttributeStream("dex_pc") << "n/a";
612 }
David Brazdilc74652862015-05-13 17:50:09 +0100613 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800614 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100615 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100616 for (HEnvironment* environment = instruction->GetEnvironment();
617 environment != nullptr;
618 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100619 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100620 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
621 HInstruction* insn = environment->GetInstructionAt(i);
622 if (insn != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100623 vregs.NewEntryStream() << DataType::TypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100624 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100625 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100626 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800627 }
David Brazdilc74652862015-05-13 17:50:09 +0100628 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800629 }
David Brazdilc74652862015-05-13 17:50:09 +0100630 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800631 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800632 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000633 && is_after_pass_
634 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100635 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100636 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100637 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100638 StartAttributeStream("ranges")
639 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
Vladimir Marko82b07402017-03-01 19:02:04 +0000640 StartAttributeStream("uses") << StringList(interval->GetUses());
641 StartAttributeStream("env_uses") << StringList(interval->GetEnvironmentUses());
David Brazdilc74652862015-05-13 17:50:09 +0100642 StartAttributeStream("is_fixed") << interval->IsFixed();
643 StartAttributeStream("is_split") << interval->IsSplit();
644 StartAttributeStream("is_low") << interval->IsLowInterval();
645 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100646 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 }
648
649 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100650 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100651 LocationSummary* locations = instruction->GetLocations();
652 if (locations != nullptr) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100653 StringList input_list;
654 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
655 DumpLocation(input_list.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100656 }
David Brazdilc74652862015-05-13 17:50:09 +0100657 std::ostream& attr = StartAttributeStream("locations");
Vladimir Marko372f10e2016-05-17 16:30:10 +0100658 attr << input_list << "->";
David Brazdilc74652862015-05-13 17:50:09 +0100659 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100660 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000661 }
662
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100663 HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
664 if (loop_info == nullptr) {
665 StartAttributeStream("loop") << "none";
666 } else {
667 StartAttributeStream("loop") << "B" << loop_info->GetHeader()->GetBlockId();
668 HLoopInformation* outer = loop_info->GetPreHeader()->GetLoopInformation();
669 if (outer != nullptr) {
670 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000671 } else {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100672 StartAttributeStream("outer_loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000673 }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100674 StartAttributeStream("irreducible")
675 << std::boolalpha << loop_info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000676 }
677
Vladimir Marko175e7862018-03-27 09:03:13 +0000678 // For the builder and the inliner, we want to add extra information on HInstructions
679 // that have reference types, and also HInstanceOf/HCheckcast.
David Brazdilbadd8262016-02-02 16:28:56 +0000680 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000681 || IsPass(HInliner::kInlinerPassName))
Vladimir Marko175e7862018-03-27 09:03:13 +0000682 && (instruction->GetType() == DataType::Type::kReference ||
683 instruction->IsInstanceOf() ||
684 instruction->IsCheckCast())) {
685 ReferenceTypeInfo info = (instruction->GetType() == DataType::Type::kReference)
686 ? instruction->IsLoadClass()
687 ? instruction->AsLoadClass()->GetLoadedClassRTI()
688 : instruction->GetReferenceTypeInfo()
689 : instruction->IsInstanceOf()
690 ? instruction->AsInstanceOf()->GetTargetClassRTI()
691 : instruction->AsCheckCast()->GetTargetClassRTI();
Calin Juravle2e768302015-07-28 14:41:11 +0000692 ScopedObjectAccess soa(Thread::Current());
693 if (info.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700694 StartAttributeStream("klass")
695 << mirror::Class::PrettyDescriptor(info.GetTypeHandle().Get());
Vladimir Marko175e7862018-03-27 09:03:13 +0000696 if (instruction->GetType() == DataType::Type::kReference) {
697 StartAttributeStream("can_be_null")
698 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
699 }
Calin Juravle2e768302015-07-28 14:41:11 +0000700 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Vladimir Marko175e7862018-03-27 09:03:13 +0000701 } else if (instruction->IsLoadClass() ||
702 instruction->IsInstanceOf() ||
703 instruction->IsCheckCast()) {
Calin Juravle98893e12015-10-02 21:05:03 +0100704 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000705 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500706 // The NullConstant may be added to the graph during other passes that happen between
707 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
708 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
709 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000710 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000711 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000712 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
713 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100714 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100715 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100716 if (disasm_info_ != nullptr) {
717 DCHECK(disassembler_ != nullptr);
718 // If the information is available, disassemble the code generated for
719 // this instruction.
720 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
721 if (it != disasm_info_->GetInstructionIntervals().end()
722 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100723 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100724 disassembler_->Disassemble(output_, it->second.start, it->second.end);
725 }
726 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100727 }
728
729 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100730 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
731 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100732 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100733 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000734 AddIndent();
735 output_ << bci << " " << num_uses << " "
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100736 << DataType::TypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000737 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100738 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100739 }
740 }
741
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100742 void DumpStartOfDisassemblyBlock(const char* block_name,
743 int predecessor_index,
744 int successor_index) {
745 StartTag("block");
746 PrintProperty("name", block_name);
747 PrintInt("from_bci", -1);
748 PrintInt("to_bci", -1);
749 if (predecessor_index != -1) {
750 PrintProperty("predecessors", "B", predecessor_index);
751 } else {
752 PrintEmptyProperty("predecessors");
753 }
754 if (successor_index != -1) {
755 PrintProperty("successors", "B", successor_index);
756 } else {
757 PrintEmptyProperty("successors");
758 }
759 PrintEmptyProperty("xhandlers");
760 PrintEmptyProperty("flags");
761 StartTag("states");
762 StartTag("locals");
763 PrintInt("size", 0);
764 PrintProperty("method", "None");
765 EndTag("locals");
766 EndTag("states");
767 StartTag("HIR");
768 }
769
770 void DumpEndOfDisassemblyBlock() {
771 EndTag("HIR");
772 EndTag("block");
773 }
774
775 void DumpDisassemblyBlockForFrameEntry() {
776 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
777 -1,
778 GetGraph()->GetEntryBlock()->GetBlockId());
779 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
780 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
781 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100782 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100783 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
784 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100785 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100786 DumpEndOfDisassemblyBlock();
787 }
788
789 void DumpDisassemblyBlockForSlowPaths() {
790 if (disasm_info_->GetSlowPathIntervals().empty()) {
791 return;
792 }
793 // If the graph has an exit block we attach the block for the slow paths
794 // after it. Else we just add the block to the graph without linking it to
795 // any other.
796 DumpStartOfDisassemblyBlock(
797 kDisassemblyBlockSlowPaths,
798 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
799 -1);
800 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100801 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100802 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100803 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100804 }
805 DumpEndOfDisassemblyBlock();
806 }
807
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100808 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100809 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100810 std::string pass_desc = std::string(pass_name_)
811 + " ("
812 + (is_after_pass_ ? "after" : "before")
813 + (graph_in_bad_state_ ? ", bad_state" : "")
814 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000815 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100816 if (disasm_info_ != nullptr) {
817 DumpDisassemblyBlockForFrameEntry();
818 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100819 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100820 if (disasm_info_ != nullptr) {
821 DumpDisassemblyBlockForSlowPaths();
822 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100823 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100824 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100825 }
826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100827 void VisitBasicBlock(HBasicBlock* block) override {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100828 StartTag("block");
829 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100830 if (block->GetLifetimeStart() != kNoLifetime) {
831 // Piggy back on these fields to show the lifetime of the block.
832 PrintInt("from_bci", block->GetLifetimeStart());
833 PrintInt("to_bci", block->GetLifetimeEnd());
834 } else {
835 PrintInt("from_bci", -1);
836 PrintInt("to_bci", -1);
837 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100838 PrintPredecessors(block);
839 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000840 PrintExceptionHandlers(block);
841
842 if (block->IsCatchBlock()) {
843 PrintProperty("flags", "catch_block");
844 } else {
845 PrintEmptyProperty("flags");
846 }
847
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100848 if (block->GetDominator() != nullptr) {
849 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
850 }
851
852 StartTag("states");
853 StartTag("locals");
854 PrintInt("size", 0);
855 PrintProperty("method", "None");
856 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
857 AddIndent();
858 HInstruction* instruction = it.Current();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100859 output_ << instruction->GetId() << " " << DataType::TypeId(instruction->GetType())
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100860 << instruction->GetId() << "[ ";
Vladimir Marko372f10e2016-05-17 16:30:10 +0100861 for (const HInstruction* input : instruction->GetInputs()) {
862 output_ << input->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100863 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100864 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100865 }
866 EndTag("locals");
867 EndTag("states");
868
869 StartTag("HIR");
870 PrintInstructions(block->GetPhis());
871 PrintInstructions(block->GetInstructions());
872 EndTag("HIR");
873 EndTag("block");
874 }
875
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100876 static constexpr const char* const kEndInstructionMarker = "<|@";
877 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
878 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
879
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100880 private:
881 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100882 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000883 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100884 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100885 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100886 const DisassemblyInformation* disasm_info_;
887 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100888 size_t indent_;
889
890 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
891};
892
893HGraphVisualizer::HGraphVisualizer(std::ostream* output,
894 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100895 const CodeGenerator& codegen)
896 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100897
David Brazdil62e074f2015-04-07 18:09:37 +0100898void HGraphVisualizer::PrintHeader(const char* method_name) const {
899 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100900 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100901 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000902 printer.PrintProperty("name", method_name);
903 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100904 printer.PrintTime("date");
905 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100906 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100907}
908
David Brazdilffee3d32015-07-06 11:48:53 +0100909void HGraphVisualizer::DumpGraph(const char* pass_name,
910 bool is_after_pass,
911 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000912 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100913 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100914 HGraphVisualizerPrinter printer(graph_,
915 *output_,
916 pass_name,
917 is_after_pass,
918 graph_in_bad_state,
919 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000920 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100921 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100922}
923
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100924void HGraphVisualizer::DumpGraphWithDisassembly() const {
925 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100926 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100927 HGraphVisualizerPrinter printer(graph_,
928 *output_,
929 "disassembly",
Andreas Gampe3db70682018-12-26 15:12:03 -0800930 /* is_after_pass= */ true,
931 /* graph_in_bad_state= */ false,
David Brazdilffee3d32015-07-06 11:48:53 +0100932 codegen_,
933 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100934 printer.Run();
935 }
936}
937
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100938} // namespace art