blob: 55191214a6b653a7492b9fddada768257889c28d [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"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010030#include "disassembler.h"
Calin Juravlecdfed3d2015-10-26 14:05:01 +000031#include "inliner.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080032#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010033#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000034#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010035#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070036#include "register_allocator_linear_scan.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010037#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010039#include "utils/assembler.h"
Vladimir Marko82b07402017-03-01 19:02:04 +000040#include "utils/intrusive_forward_list.h"
David Brazdilc74652862015-05-13 17:50:09 +010041
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010042namespace art {
43
David Brazdilc74652862015-05-13 17:50:09 +010044static bool HasWhitespace(const char* str) {
45 DCHECK(str != nullptr);
46 while (str[0] != 0) {
47 if (isspace(str[0])) {
48 return true;
49 }
50 str++;
51 }
52 return false;
53}
54
55class StringList {
56 public:
David Brazdilc7a24852015-05-15 16:44:05 +010057 enum Format {
58 kArrayBrackets,
59 kSetBrackets,
60 };
61
David Brazdilc74652862015-05-13 17:50:09 +010062 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010063 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010064
65 // Construct StringList from a linked list. List element class T
66 // must provide methods `GetNext` and `Dump`.
67 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010068 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010069 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
70 current->Dump(NewEntryStream());
71 }
72 }
Vladimir Marko82b07402017-03-01 19:02:04 +000073 // Construct StringList from a list of elements. The value type must provide method `Dump`.
74 template <typename Container>
75 explicit StringList(const Container& list, Format format = kArrayBrackets) : StringList(format) {
76 for (const typename Container::value_type& current : list) {
77 current.Dump(NewEntryStream());
78 }
79 }
David Brazdilc74652862015-05-13 17:50:09 +010080
81 std::ostream& NewEntryStream() {
82 if (is_empty_) {
83 is_empty_ = false;
84 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010085 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010086 }
87 return sstream_;
88 }
89
90 private:
David Brazdilc7a24852015-05-15 16:44:05 +010091 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010092 bool is_empty_;
93 std::ostringstream sstream_;
94
95 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
96};
97
98std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010099 switch (list.format_) {
100 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
101 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
102 default:
103 LOG(FATAL) << "Invalid StringList format";
104 UNREACHABLE();
105 }
David Brazdilc74652862015-05-13 17:50:09 +0100106}
107
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100108typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
109 DisassemblerOptions* options);
110class 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) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100116 libart_disassembler_handle_ =
117 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
118 if (libart_disassembler_handle_ == nullptr) {
119 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
120 return;
121 }
122 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
123 dlsym(libart_disassembler_handle_, "create_disassembler"));
124 if (create_disassembler == nullptr) {
125 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
126 return;
127 }
128 // Reading the disassembly from 0x0 is easier, so we print relative
129 // addresses. We will only disassemble the code once everything has
130 // been generated, so we can read data in literal pools.
131 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
132 instruction_set,
133 new DisassemblerOptions(/* absolute_addresses */ false,
134 base_address,
Aart Bikd3059e72016-05-11 10:30:47 -0700135 end_address,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700136 /* can_read_literals */ true,
137 Is64BitInstructionSet(instruction_set)
138 ? &Thread::DumpThreadOffset<PointerSize::k64>
139 : &Thread::DumpThreadOffset<PointerSize::k32>)));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100140 }
141
142 ~HGraphVisualizerDisassembler() {
143 // We need to call ~Disassembler() before we close the library.
144 disassembler_.reset();
145 if (libart_disassembler_handle_ != nullptr) {
146 dlclose(libart_disassembler_handle_);
147 }
148 }
149
150 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100151 if (disassembler_ == nullptr) {
152 return;
153 }
154
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100155 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
Vladimir Marko33bff252017-11-01 14:35:42 +0000156 if (instruction_set_ == InstructionSet::kThumb2) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100157 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
158 // address is used to distinguish between the two.
159 base += 1;
160 }
161 disassembler_->Dump(output, base + start, base + end);
162 }
163
164 private:
165 InstructionSet instruction_set_;
166 std::unique_ptr<Disassembler> disassembler_;
167
168 void* libart_disassembler_handle_;
169};
170
171
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100172/**
173 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
174 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100175class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100176 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100177 HGraphVisualizerPrinter(HGraph* graph,
178 std::ostream& output,
179 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000180 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100181 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100182 const CodeGenerator& codegen,
183 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100184 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100185 output_(output),
186 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000187 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100188 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100189 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100190 disasm_info_(disasm_info),
191 disassembler_(disasm_info_ != nullptr
192 ? new HGraphVisualizerDisassembler(
193 codegen_.GetInstructionSet(),
Aart Bikd3059e72016-05-11 10:30:47 -0700194 codegen_.GetAssembler().CodeBufferBaseAddress(),
195 codegen_.GetAssembler().CodeBufferBaseAddress()
196 + codegen_.GetAssembler().CodeSize())
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100197 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100198 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100199
David Brazdilfa02c9d2016-03-30 09:41:02 +0100200 void Flush() {
201 // We use "\n" instead of std::endl to avoid implicit flushing which
202 // generates too many syscalls during debug-GC tests (b/27826765).
203 output_ << std::flush;
204 }
205
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100206 void StartTag(const char* name) {
207 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100208 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100209 indent_++;
210 }
211
212 void EndTag(const char* name) {
213 indent_--;
214 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100215 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100216 }
217
218 void PrintProperty(const char* name, const char* property) {
219 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100220 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100221 }
222
223 void PrintProperty(const char* name, const char* property, int id) {
224 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100225 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100226 }
227
228 void PrintEmptyProperty(const char* name) {
229 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100230 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100231 }
232
233 void PrintTime(const char* name) {
234 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100235 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100236 }
237
238 void PrintInt(const char* name, int value) {
239 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100240 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100241 }
242
243 void AddIndent() {
244 for (size_t i = 0; i < indent_; ++i) {
245 output_ << " ";
246 }
247 }
248
249 void PrintPredecessors(HBasicBlock* block) {
250 AddIndent();
251 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000252 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100253 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
254 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100255 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
256 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
257 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100258 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100259 }
260
261 void PrintSuccessors(HBasicBlock* block) {
262 AddIndent();
263 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000264 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100265 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000266 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100267 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000268 }
269
270 void PrintExceptionHandlers(HBasicBlock* block) {
271 AddIndent();
272 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000273 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100274 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100275 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100276 if (block->IsExitBlock() &&
277 (disasm_info_ != nullptr) &&
278 !disasm_info_->GetSlowPathIntervals().empty()) {
279 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
280 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100281 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100282 }
283
David Brazdilc74652862015-05-13 17:50:09 +0100284 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100285 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100286 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100287 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100288 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100289 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100290 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100291 HConstant* constant = location.GetConstant();
292 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100293 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100294 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100295 stream << constant->AsLongConstant()->GetValue();
Alexandre Ramesc2c52a12016-08-02 13:45:28 +0100296 } else if (constant->IsFloatConstant()) {
297 stream << constant->AsFloatConstant()->GetValue();
298 } else if (constant->IsDoubleConstant()) {
299 stream << constant->AsDoubleConstant()->GetValue();
300 } else if (constant->IsNullConstant()) {
301 stream << "null";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100302 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100303 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100304 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100305 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100306 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000307 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100308 codegen_.DumpFloatingPointRegister(stream, location.low());
309 stream << "|";
310 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000311 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100312 codegen_.DumpCoreRegister(stream, location.low());
313 stream << "|";
314 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400315 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100316 stream << "unallocated";
Aart Bik5576f372017-03-23 16:17:37 -0700317 } else if (location.IsDoubleStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100318 stream << "2x" << location.GetStackIndex() << "(sp)";
Aart Bik5576f372017-03-23 16:17:37 -0700319 } else {
320 DCHECK(location.IsSIMDStackSlot());
321 stream << "4x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100322 }
323 }
324
David Brazdilc74652862015-05-13 17:50:09 +0100325 std::ostream& StartAttributeStream(const char* name = nullptr) {
326 if (name == nullptr) {
327 output_ << " ";
328 } else {
329 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
330 output_ << " " << name << ":";
331 }
332 return output_;
333 }
334
David Brazdilb7e4a062014-12-29 15:35:02 +0000335 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100336 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
337 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100338 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
339 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100340 std::ostream& str = moves.NewEntryStream();
341 DumpLocation(str, move->GetSource());
342 str << "->";
343 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100344 }
David Brazdilc74652862015-05-13 17:50:09 +0100345 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100346 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100347
David Brazdil36cf0952015-01-08 19:28:33 +0000348 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100349 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000350 }
351
David Brazdil36cf0952015-01-08 19:28:33 +0000352 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100353 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000354 }
355
David Brazdil36cf0952015-01-08 19:28:33 +0000356 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100357 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000358 }
359
David Brazdil36cf0952015-01-08 19:28:33 +0000360 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100361 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000362 }
363
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000364 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100365 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100366 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000367 }
368
Calin Juravle27df7582015-04-17 19:12:31 +0100369 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100370 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100371 }
372
David Brazdilbff75032015-07-08 17:26:51 +0000373 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
374 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
375 }
376
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100377 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100378 StartAttributeStream("load_kind") << load_class->GetLoadKind();
379 const char* descriptor = load_class->GetDexFile().GetTypeDescriptor(
380 load_class->GetDexFile().GetTypeId(load_class->GetTypeIndex()));
381 StartAttributeStream("class_name") << PrettyDescriptor(descriptor);
Calin Juravle0ba218d2015-05-19 18:46:01 +0100382 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100383 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100384 StartAttributeStream("needs_access_check") << std::boolalpha
385 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100386 }
387
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000388 void VisitLoadString(HLoadString* load_string) OVERRIDE {
389 StartAttributeStream("load_kind") << load_string->GetLoadKind();
390 }
391
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000392 void HandleTypeCheckInstruction(HTypeCheckInstruction* check) {
393 StartAttributeStream("check_kind") << check->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100394 StartAttributeStream("must_do_null_check") << std::boolalpha
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000395 << check->MustDoNullCheck() << std::noboolalpha;
396 if (check->GetTypeCheckKind() == TypeCheckKind::kBitstringCheck) {
397 StartAttributeStream("path_to_root") << std::hex
398 << "0x" << check->GetBitstringPathToRoot() << std::dec;
399 StartAttributeStream("mask") << std::hex << "0x" << check->GetBitstringMask() << std::dec;
400 }
401 }
402
403 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
404 HandleTypeCheckInstruction(check_cast);
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100405 }
406
407 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000408 HandleTypeCheckInstruction(instance_of);
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100409 }
410
Vladimir Markodce016e2016-04-28 13:10:02 +0100411 void VisitArrayLength(HArrayLength* array_length) OVERRIDE {
412 StartAttributeStream("is_string_length") << std::boolalpha
413 << array_length->IsStringLength() << std::noboolalpha;
Mark Mendellee8d9712016-07-12 11:13:15 -0400414 if (array_length->IsEmittedAtUseSite()) {
415 StartAttributeStream("emitted_at_use") << "true";
416 }
Vladimir Markodce016e2016-04-28 13:10:02 +0100417 }
418
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100419 void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE {
420 StartAttributeStream("is_string_char_at") << std::boolalpha
421 << bounds_check->IsStringCharAt() << std::noboolalpha;
422 }
423
424 void VisitArrayGet(HArrayGet* array_get) OVERRIDE {
425 StartAttributeStream("is_string_char_at") << std::boolalpha
426 << array_get->IsStringCharAt() << std::noboolalpha;
427 }
428
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100429 void VisitArraySet(HArraySet* array_set) OVERRIDE {
430 StartAttributeStream("value_can_be_null") << std::boolalpha
431 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000432 StartAttributeStream("needs_type_check") << std::boolalpha
433 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100434 }
435
Roland Levillain31dd3d62016-02-16 12:21:02 +0000436 void VisitCompare(HCompare* compare) OVERRIDE {
437 ComparisonBias bias = compare->GetBias();
438 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
439 ? "gt"
440 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
441 }
442
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100443 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray5ceac0e2017-06-26 13:19:09 +0100445 ArtMethod* method = invoke->GetResolvedMethod();
446 // We don't print signatures, which conflict with c1visualizer format.
447 static constexpr bool kWithSignature = false;
448 // Note that we can only use the graph's dex file for the unresolved case. The
449 // other invokes might be coming from inlined methods.
450 ScopedObjectAccess soa(Thread::Current());
451 std::string method_name = (method == nullptr)
452 ? GetGraph()->GetDexFile().PrettyMethod(invoke->GetDexMethodIndex(), kWithSignature)
453 : method->PrettyMethod(kWithSignature);
454 StartAttributeStream("method_name") << method_name;
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100455 }
456
Calin Juravle175dc732015-08-25 15:42:32 +0100457 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
458 VisitInvoke(invoke);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100459 StartAttributeStream("invoke_type") << invoke->GetInvokeType();
Calin Juravle175dc732015-08-25 15:42:32 +0100460 }
461
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100462 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
463 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000464 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100465 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000466 if (invoke->IsStatic()) {
467 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
468 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100469 }
470
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000471 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
472 VisitInvoke(invoke);
473 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
474 }
475
Orion Hodsonac141392017-01-13 11:53:47 +0000476 void VisitInvokePolymorphic(HInvokePolymorphic* invoke) OVERRIDE {
477 VisitInvoke(invoke);
478 StartAttributeStream("invoke_type") << "InvokePolymorphic";
479 }
480
David Brazdil11edec72016-03-24 12:40:52 +0000481 void VisitInstanceFieldGet(HInstanceFieldGet* iget) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700482 StartAttributeStream("field_name") <<
483 iget->GetFieldInfo().GetDexFile().PrettyField(iget->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000484 /* with type */ false);
485 StartAttributeStream("field_type") << iget->GetFieldType();
486 }
487
488 void VisitInstanceFieldSet(HInstanceFieldSet* iset) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700489 StartAttributeStream("field_name") <<
490 iset->GetFieldInfo().GetDexFile().PrettyField(iset->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000491 /* with type */ false);
492 StartAttributeStream("field_type") << iset->GetFieldType();
493 }
494
Vladimir Markobf3243b2017-08-30 14:06:54 +0100495 void VisitStaticFieldGet(HStaticFieldGet* sget) OVERRIDE {
496 StartAttributeStream("field_name") <<
497 sget->GetFieldInfo().GetDexFile().PrettyField(sget->GetFieldInfo().GetFieldIndex(),
498 /* with type */ false);
499 StartAttributeStream("field_type") << sget->GetFieldType();
500 }
501
502 void VisitStaticFieldSet(HStaticFieldSet* sset) OVERRIDE {
503 StartAttributeStream("field_name") <<
504 sset->GetFieldInfo().GetDexFile().PrettyField(sset->GetFieldInfo().GetFieldIndex(),
505 /* with type */ false);
506 StartAttributeStream("field_type") << sset->GetFieldType();
507 }
508
Calin Juravlee460d1d2015-09-29 04:52:17 +0100509 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
510 StartAttributeStream("field_type") << field_access->GetFieldType();
511 }
512
513 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
514 StartAttributeStream("field_type") << field_access->GetFieldType();
515 }
516
517 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
518 StartAttributeStream("field_type") << field_access->GetFieldType();
519 }
520
521 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
522 StartAttributeStream("field_type") << field_access->GetFieldType();
523 }
524
David Brazdilfc6a86a2015-06-26 10:33:45 +0000525 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100526 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000527 }
528
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000529 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE {
530 StartAttributeStream("kind") << deoptimize->GetKind();
531 }
532
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100533 void VisitVecOperation(HVecOperation* vec_operation) OVERRIDE {
534 StartAttributeStream("packed_type") << vec_operation->GetPackedType();
535 }
536
Aart Bik38a3f212017-10-20 17:02:21 -0700537 void VisitVecMemoryOperation(HVecMemoryOperation* vec_mem_operation) OVERRIDE {
538 StartAttributeStream("alignment") << vec_mem_operation->GetAlignment().ToString();
539 }
540
Aart Bikf3e61ee2017-04-12 17:09:20 -0700541 void VisitVecHalvingAdd(HVecHalvingAdd* hadd) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100542 VisitVecBinaryOperation(hadd);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700543 StartAttributeStream("unsigned") << std::boolalpha << hadd->IsUnsigned() << std::noboolalpha;
544 StartAttributeStream("rounded") << std::boolalpha << hadd->IsRounded() << std::noboolalpha;
545 }
546
Aart Bikc8e93c72017-05-10 10:49:22 -0700547 void VisitVecMin(HVecMin* min) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100548 VisitVecBinaryOperation(min);
Aart Bikc8e93c72017-05-10 10:49:22 -0700549 StartAttributeStream("unsigned") << std::boolalpha << min->IsUnsigned() << std::noboolalpha;
550 }
551
552 void VisitVecMax(HVecMax* max) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100553 VisitVecBinaryOperation(max);
Aart Bikc8e93c72017-05-10 10:49:22 -0700554 StartAttributeStream("unsigned") << std::boolalpha << max->IsUnsigned() << std::noboolalpha;
555 }
556
Artem Serovf34dd202017-04-10 17:41:46 +0100557 void VisitVecMultiplyAccumulate(HVecMultiplyAccumulate* instruction) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100558 VisitVecOperation(instruction);
Artem Serovf34dd202017-04-10 17:41:46 +0100559 StartAttributeStream("kind") << instruction->GetOpKind();
560 }
561
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300562#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
563 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) OVERRIDE {
564 StartAttributeStream("kind") << instruction->GetOpKind();
565 }
Artem Serov7fc63502016-02-09 17:15:29 +0000566
567 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) OVERRIDE {
568 StartAttributeStream("kind") << instruction->GetOpKind();
569 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300570
Anton Kirilov74234da2017-01-13 14:42:47 +0000571 void VisitDataProcWithShifterOp(HDataProcWithShifterOp* instruction) OVERRIDE {
Alexandre Rames8626b742015-11-25 16:28:08 +0000572 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
Anton Kirilov74234da2017-01-13 14:42:47 +0000573 if (HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000574 StartAttributeStream("shift") << instruction->GetShiftAmount();
575 }
576 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000577#endif
578
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800579 bool IsPass(const char* name) {
580 return strcmp(pass_name_, name) == 0;
581 }
582
David Brazdilb7e4a062014-12-29 15:35:02 +0000583 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100584 output_ << instruction->DebugName();
Vladimir Markoe9004912016-06-16 16:50:52 +0100585 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100586 if (!inputs.empty()) {
587 StringList input_list;
588 for (const HInstruction* input : inputs) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100589 input_list.NewEntryStream() << DataType::TypeId(input->GetType()) << input->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100590 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100591 StartAttributeStream() << input_list;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100592 }
David Brazdilc74652862015-05-13 17:50:09 +0100593 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800594 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100595 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100596 for (HEnvironment* environment = instruction->GetEnvironment();
597 environment != nullptr;
598 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100599 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100600 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
601 HInstruction* insn = environment->GetInstructionAt(i);
602 if (insn != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100603 vregs.NewEntryStream() << DataType::TypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100604 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100605 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100606 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800607 }
David Brazdilc74652862015-05-13 17:50:09 +0100608 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800609 }
David Brazdilc74652862015-05-13 17:50:09 +0100610 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800611 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800612 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000613 && is_after_pass_
614 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100615 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100616 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100617 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100618 StartAttributeStream("ranges")
619 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
Vladimir Marko82b07402017-03-01 19:02:04 +0000620 StartAttributeStream("uses") << StringList(interval->GetUses());
621 StartAttributeStream("env_uses") << StringList(interval->GetEnvironmentUses());
David Brazdilc74652862015-05-13 17:50:09 +0100622 StartAttributeStream("is_fixed") << interval->IsFixed();
623 StartAttributeStream("is_split") << interval->IsSplit();
624 StartAttributeStream("is_low") << interval->IsLowInterval();
625 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000627 }
628
629 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100630 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100631 LocationSummary* locations = instruction->GetLocations();
632 if (locations != nullptr) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100633 StringList input_list;
634 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
635 DumpLocation(input_list.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100636 }
David Brazdilc74652862015-05-13 17:50:09 +0100637 std::ostream& attr = StartAttributeStream("locations");
Vladimir Marko372f10e2016-05-17 16:30:10 +0100638 attr << input_list << "->";
David Brazdilc74652862015-05-13 17:50:09 +0100639 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100640 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000641 }
642
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100643 HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
644 if (loop_info == nullptr) {
645 StartAttributeStream("loop") << "none";
646 } else {
647 StartAttributeStream("loop") << "B" << loop_info->GetHeader()->GetBlockId();
648 HLoopInformation* outer = loop_info->GetPreHeader()->GetLoopInformation();
649 if (outer != nullptr) {
650 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000651 } else {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100652 StartAttributeStream("outer_loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000653 }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100654 StartAttributeStream("irreducible")
655 << std::boolalpha << loop_info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000656 }
657
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000658 // For the builder and the inliner, we want to add extra information on HInstructions
659 // that have reference types, and also HInstanceOf/HCheckcast.
David Brazdilbadd8262016-02-02 16:28:56 +0000660 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000661 || IsPass(HInliner::kInlinerPassName))
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000662 && (instruction->GetType() == DataType::Type::kReference ||
663 instruction->IsInstanceOf() ||
664 instruction->IsCheckCast())) {
665 ReferenceTypeInfo info = (instruction->GetType() == DataType::Type::kReference)
666 ? instruction->IsLoadClass()
667 ? instruction->AsLoadClass()->GetLoadedClassRTI()
668 : instruction->GetReferenceTypeInfo()
669 : instruction->IsInstanceOf()
670 ? instruction->AsInstanceOf()->GetTargetClassRTI()
671 : instruction->AsCheckCast()->GetTargetClassRTI();
Calin Juravle2e768302015-07-28 14:41:11 +0000672 ScopedObjectAccess soa(Thread::Current());
673 if (info.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700674 StartAttributeStream("klass")
675 << mirror::Class::PrettyDescriptor(info.GetTypeHandle().Get());
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000676 if (instruction->GetType() == DataType::Type::kReference) {
677 StartAttributeStream("can_be_null")
678 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
679 }
Calin Juravle2e768302015-07-28 14:41:11 +0000680 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Vladimir Markoeb0ebed2018-01-10 18:26:38 +0000681 } else if (instruction->IsLoadClass() ||
682 instruction->IsInstanceOf() ||
683 instruction->IsCheckCast()) {
Calin Juravle98893e12015-10-02 21:05:03 +0100684 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000685 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500686 // The NullConstant may be added to the graph during other passes that happen between
687 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
688 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
689 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000690 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000691 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000692 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
693 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100694 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100695 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100696 if (disasm_info_ != nullptr) {
697 DCHECK(disassembler_ != nullptr);
698 // If the information is available, disassemble the code generated for
699 // this instruction.
700 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
701 if (it != disasm_info_->GetInstructionIntervals().end()
702 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100703 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100704 disassembler_->Disassemble(output_, it->second.start, it->second.end);
705 }
706 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100707 }
708
709 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100710 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
711 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100712 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100713 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000714 AddIndent();
715 output_ << bci << " " << num_uses << " "
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100716 << DataType::TypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000717 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100718 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100719 }
720 }
721
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100722 void DumpStartOfDisassemblyBlock(const char* block_name,
723 int predecessor_index,
724 int successor_index) {
725 StartTag("block");
726 PrintProperty("name", block_name);
727 PrintInt("from_bci", -1);
728 PrintInt("to_bci", -1);
729 if (predecessor_index != -1) {
730 PrintProperty("predecessors", "B", predecessor_index);
731 } else {
732 PrintEmptyProperty("predecessors");
733 }
734 if (successor_index != -1) {
735 PrintProperty("successors", "B", successor_index);
736 } else {
737 PrintEmptyProperty("successors");
738 }
739 PrintEmptyProperty("xhandlers");
740 PrintEmptyProperty("flags");
741 StartTag("states");
742 StartTag("locals");
743 PrintInt("size", 0);
744 PrintProperty("method", "None");
745 EndTag("locals");
746 EndTag("states");
747 StartTag("HIR");
748 }
749
750 void DumpEndOfDisassemblyBlock() {
751 EndTag("HIR");
752 EndTag("block");
753 }
754
755 void DumpDisassemblyBlockForFrameEntry() {
756 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
757 -1,
758 GetGraph()->GetEntryBlock()->GetBlockId());
759 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
760 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
761 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100762 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100763 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
764 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100765 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100766 DumpEndOfDisassemblyBlock();
767 }
768
769 void DumpDisassemblyBlockForSlowPaths() {
770 if (disasm_info_->GetSlowPathIntervals().empty()) {
771 return;
772 }
773 // If the graph has an exit block we attach the block for the slow paths
774 // after it. Else we just add the block to the graph without linking it to
775 // any other.
776 DumpStartOfDisassemblyBlock(
777 kDisassemblyBlockSlowPaths,
778 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
779 -1);
780 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100781 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100782 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100783 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100784 }
785 DumpEndOfDisassemblyBlock();
786 }
787
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100788 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100789 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100790 std::string pass_desc = std::string(pass_name_)
791 + " ("
792 + (is_after_pass_ ? "after" : "before")
793 + (graph_in_bad_state_ ? ", bad_state" : "")
794 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000795 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100796 if (disasm_info_ != nullptr) {
797 DumpDisassemblyBlockForFrameEntry();
798 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100799 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100800 if (disasm_info_ != nullptr) {
801 DumpDisassemblyBlockForSlowPaths();
802 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100803 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100804 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100805 }
806
David Brazdilb7e4a062014-12-29 15:35:02 +0000807 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100808 StartTag("block");
809 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100810 if (block->GetLifetimeStart() != kNoLifetime) {
811 // Piggy back on these fields to show the lifetime of the block.
812 PrintInt("from_bci", block->GetLifetimeStart());
813 PrintInt("to_bci", block->GetLifetimeEnd());
814 } else {
815 PrintInt("from_bci", -1);
816 PrintInt("to_bci", -1);
817 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100818 PrintPredecessors(block);
819 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000820 PrintExceptionHandlers(block);
821
822 if (block->IsCatchBlock()) {
823 PrintProperty("flags", "catch_block");
824 } else {
825 PrintEmptyProperty("flags");
826 }
827
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100828 if (block->GetDominator() != nullptr) {
829 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
830 }
831
832 StartTag("states");
833 StartTag("locals");
834 PrintInt("size", 0);
835 PrintProperty("method", "None");
836 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
837 AddIndent();
838 HInstruction* instruction = it.Current();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 output_ << instruction->GetId() << " " << DataType::TypeId(instruction->GetType())
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100840 << instruction->GetId() << "[ ";
Vladimir Marko372f10e2016-05-17 16:30:10 +0100841 for (const HInstruction* input : instruction->GetInputs()) {
842 output_ << input->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100843 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100844 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100845 }
846 EndTag("locals");
847 EndTag("states");
848
849 StartTag("HIR");
850 PrintInstructions(block->GetPhis());
851 PrintInstructions(block->GetInstructions());
852 EndTag("HIR");
853 EndTag("block");
854 }
855
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100856 static constexpr const char* const kEndInstructionMarker = "<|@";
857 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
858 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
859
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100860 private:
861 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100862 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000863 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100864 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100865 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100866 const DisassemblyInformation* disasm_info_;
867 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100868 size_t indent_;
869
870 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
871};
872
873HGraphVisualizer::HGraphVisualizer(std::ostream* output,
874 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100875 const CodeGenerator& codegen)
876 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100877
David Brazdil62e074f2015-04-07 18:09:37 +0100878void HGraphVisualizer::PrintHeader(const char* method_name) const {
879 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100880 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100881 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000882 printer.PrintProperty("name", method_name);
883 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100884 printer.PrintTime("date");
885 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100886 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100887}
888
David Brazdilffee3d32015-07-06 11:48:53 +0100889void HGraphVisualizer::DumpGraph(const char* pass_name,
890 bool is_after_pass,
891 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000892 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100893 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100894 HGraphVisualizerPrinter printer(graph_,
895 *output_,
896 pass_name,
897 is_after_pass,
898 graph_in_bad_state,
899 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000900 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100901 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100902}
903
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100904void HGraphVisualizer::DumpGraphWithDisassembly() const {
905 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100906 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100907 HGraphVisualizerPrinter printer(graph_,
908 *output_,
909 "disassembly",
910 /* is_after_pass */ true,
911 /* graph_in_bad_state */ false,
912 codegen_,
913 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100914 printer.Run();
915 }
916}
917
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100918} // namespace art