blob: eccdccf186255efd82967dd943f3c451f31007c9 [file] [log] [blame]
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "graph_visualizer.h"
18
Alexandre Rameseb7b7392015-06-19 14:47:01 +010019#include <dlfcn.h>
20
21#include <cctype>
22#include <sstream>
23
Aart Bik09e8d5f2016-01-22 16:49:55 -080024#include "bounds_check_elimination.h"
David Brazdilbadd8262016-02-02 16:28:56 +000025#include "builder.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010026#include "code_generator.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010027#include "data_type-inl.h"
David Brazdila4b8c212015-05-07 09:59:30 +010028#include "dead_code_elimination.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010029#include "disassembler.h"
Calin Juravlecdfed3d2015-10-26 14:05:01 +000030#include "inliner.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080031#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010032#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000033#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010034#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070035#include "register_allocator_linear_scan.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010037#include "utils/assembler.h"
Vladimir Marko82b07402017-03-01 19:02:04 +000038#include "utils/intrusive_forward_list.h"
David Brazdilc74652862015-05-13 17:50:09 +010039
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010040namespace art {
41
David Brazdilc74652862015-05-13 17:50:09 +010042static bool HasWhitespace(const char* str) {
43 DCHECK(str != nullptr);
44 while (str[0] != 0) {
45 if (isspace(str[0])) {
46 return true;
47 }
48 str++;
49 }
50 return false;
51}
52
53class StringList {
54 public:
David Brazdilc7a24852015-05-15 16:44:05 +010055 enum Format {
56 kArrayBrackets,
57 kSetBrackets,
58 };
59
David Brazdilc74652862015-05-13 17:50:09 +010060 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010061 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010062
63 // Construct StringList from a linked list. List element class T
64 // must provide methods `GetNext` and `Dump`.
65 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010066 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010067 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
68 current->Dump(NewEntryStream());
69 }
70 }
Vladimir Marko82b07402017-03-01 19:02:04 +000071 // Construct StringList from a list of elements. The value type must provide method `Dump`.
72 template <typename Container>
73 explicit StringList(const Container& list, Format format = kArrayBrackets) : StringList(format) {
74 for (const typename Container::value_type& current : list) {
75 current.Dump(NewEntryStream());
76 }
77 }
David Brazdilc74652862015-05-13 17:50:09 +010078
79 std::ostream& NewEntryStream() {
80 if (is_empty_) {
81 is_empty_ = false;
82 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010083 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010084 }
85 return sstream_;
86 }
87
88 private:
David Brazdilc7a24852015-05-15 16:44:05 +010089 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010090 bool is_empty_;
91 std::ostringstream sstream_;
92
93 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
94};
95
96std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010097 switch (list.format_) {
98 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
99 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
100 default:
101 LOG(FATAL) << "Invalid StringList format";
102 UNREACHABLE();
103 }
David Brazdilc74652862015-05-13 17:50:09 +0100104}
105
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100106typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
107 DisassemblerOptions* options);
108class HGraphVisualizerDisassembler {
109 public:
Aart Bikd3059e72016-05-11 10:30:47 -0700110 HGraphVisualizerDisassembler(InstructionSet instruction_set,
111 const uint8_t* base_address,
112 const uint8_t* end_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100113 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100114 libart_disassembler_handle_ =
115 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
116 if (libart_disassembler_handle_ == nullptr) {
117 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
118 return;
119 }
120 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
121 dlsym(libart_disassembler_handle_, "create_disassembler"));
122 if (create_disassembler == nullptr) {
123 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
124 return;
125 }
126 // Reading the disassembly from 0x0 is easier, so we print relative
127 // addresses. We will only disassemble the code once everything has
128 // been generated, so we can read data in literal pools.
129 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
130 instruction_set,
131 new DisassemblerOptions(/* absolute_addresses */ false,
132 base_address,
Aart Bikd3059e72016-05-11 10:30:47 -0700133 end_address,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700134 /* can_read_literals */ true,
135 Is64BitInstructionSet(instruction_set)
136 ? &Thread::DumpThreadOffset<PointerSize::k64>
137 : &Thread::DumpThreadOffset<PointerSize::k32>)));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100138 }
139
140 ~HGraphVisualizerDisassembler() {
141 // We need to call ~Disassembler() before we close the library.
142 disassembler_.reset();
143 if (libart_disassembler_handle_ != nullptr) {
144 dlclose(libart_disassembler_handle_);
145 }
146 }
147
148 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100149 if (disassembler_ == nullptr) {
150 return;
151 }
152
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100153 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
154 if (instruction_set_ == kThumb2) {
155 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
156 // address is used to distinguish between the two.
157 base += 1;
158 }
159 disassembler_->Dump(output, base + start, base + end);
160 }
161
162 private:
163 InstructionSet instruction_set_;
164 std::unique_ptr<Disassembler> disassembler_;
165
166 void* libart_disassembler_handle_;
167};
168
169
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100170/**
171 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
172 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100173class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100174 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100175 HGraphVisualizerPrinter(HGraph* graph,
176 std::ostream& output,
177 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000178 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100179 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100180 const CodeGenerator& codegen,
181 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100182 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100183 output_(output),
184 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000185 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100186 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100187 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100188 disasm_info_(disasm_info),
189 disassembler_(disasm_info_ != nullptr
190 ? new HGraphVisualizerDisassembler(
191 codegen_.GetInstructionSet(),
Aart Bikd3059e72016-05-11 10:30:47 -0700192 codegen_.GetAssembler().CodeBufferBaseAddress(),
193 codegen_.GetAssembler().CodeBufferBaseAddress()
194 + codegen_.GetAssembler().CodeSize())
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100195 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100196 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100197
David Brazdilfa02c9d2016-03-30 09:41:02 +0100198 void Flush() {
199 // We use "\n" instead of std::endl to avoid implicit flushing which
200 // generates too many syscalls during debug-GC tests (b/27826765).
201 output_ << std::flush;
202 }
203
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100204 void StartTag(const char* name) {
205 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100206 output_ << "begin_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100207 indent_++;
208 }
209
210 void EndTag(const char* name) {
211 indent_--;
212 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100213 output_ << "end_" << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100214 }
215
216 void PrintProperty(const char* name, const char* property) {
217 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100218 output_ << name << " \"" << property << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100219 }
220
221 void PrintProperty(const char* name, const char* property, int id) {
222 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100223 output_ << name << " \"" << property << id << "\"\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100224 }
225
226 void PrintEmptyProperty(const char* name) {
227 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100228 output_ << name << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100229 }
230
231 void PrintTime(const char* name) {
232 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100233 output_ << name << " " << time(nullptr) << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100234 }
235
236 void PrintInt(const char* name, int value) {
237 AddIndent();
David Brazdilfa02c9d2016-03-30 09:41:02 +0100238 output_ << name << " " << value << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100239 }
240
241 void AddIndent() {
242 for (size_t i = 0; i < indent_; ++i) {
243 output_ << " ";
244 }
245 }
246
247 void PrintPredecessors(HBasicBlock* block) {
248 AddIndent();
249 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000250 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100251 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
252 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100253 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
254 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
255 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100256 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100257 }
258
259 void PrintSuccessors(HBasicBlock* block) {
260 AddIndent();
261 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000262 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100263 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000264 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100265 output_<< "\n";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000266 }
267
268 void PrintExceptionHandlers(HBasicBlock* block) {
269 AddIndent();
270 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000271 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100272 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100273 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100274 if (block->IsExitBlock() &&
275 (disasm_info_ != nullptr) &&
276 !disasm_info_->GetSlowPathIntervals().empty()) {
277 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
278 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100279 output_<< "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100280 }
281
David Brazdilc74652862015-05-13 17:50:09 +0100282 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100283 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100284 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100285 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100286 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100287 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100288 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100289 HConstant* constant = location.GetConstant();
290 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100291 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100292 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100293 stream << constant->AsLongConstant()->GetValue();
Alexandre Ramesc2c52a12016-08-02 13:45:28 +0100294 } else if (constant->IsFloatConstant()) {
295 stream << constant->AsFloatConstant()->GetValue();
296 } else if (constant->IsDoubleConstant()) {
297 stream << constant->AsDoubleConstant()->GetValue();
298 } else if (constant->IsNullConstant()) {
299 stream << "null";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100300 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100301 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100302 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100303 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100304 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000305 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100306 codegen_.DumpFloatingPointRegister(stream, location.low());
307 stream << "|";
308 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000309 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100310 codegen_.DumpCoreRegister(stream, location.low());
311 stream << "|";
312 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400313 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100314 stream << "unallocated";
Aart Bik5576f372017-03-23 16:17:37 -0700315 } else if (location.IsDoubleStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100316 stream << "2x" << location.GetStackIndex() << "(sp)";
Aart Bik5576f372017-03-23 16:17:37 -0700317 } else {
318 DCHECK(location.IsSIMDStackSlot());
319 stream << "4x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100320 }
321 }
322
David Brazdilc74652862015-05-13 17:50:09 +0100323 std::ostream& StartAttributeStream(const char* name = nullptr) {
324 if (name == nullptr) {
325 output_ << " ";
326 } else {
327 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
328 output_ << " " << name << ":";
329 }
330 return output_;
331 }
332
David Brazdilb7e4a062014-12-29 15:35:02 +0000333 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100334 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
335 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100336 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
337 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100338 std::ostream& str = moves.NewEntryStream();
339 DumpLocation(str, move->GetSource());
340 str << "->";
341 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100342 }
David Brazdilc74652862015-05-13 17:50:09 +0100343 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100344 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100345
David Brazdil36cf0952015-01-08 19:28:33 +0000346 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100347 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000348 }
349
David Brazdil36cf0952015-01-08 19:28:33 +0000350 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100351 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000352 }
353
David Brazdil36cf0952015-01-08 19:28:33 +0000354 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100355 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000356 }
357
David Brazdil36cf0952015-01-08 19:28:33 +0000358 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100359 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000360 }
361
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000362 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100363 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100364 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000365 }
366
Calin Juravle27df7582015-04-17 19:12:31 +0100367 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100368 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100369 }
370
David Brazdilbff75032015-07-08 17:26:51 +0000371 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
372 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
373 }
374
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100375 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100376 StartAttributeStream("load_kind") << load_class->GetLoadKind();
377 const char* descriptor = load_class->GetDexFile().GetTypeDescriptor(
378 load_class->GetDexFile().GetTypeId(load_class->GetTypeIndex()));
379 StartAttributeStream("class_name") << PrettyDescriptor(descriptor);
Calin Juravle0ba218d2015-05-19 18:46:01 +0100380 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100381 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100382 StartAttributeStream("needs_access_check") << std::boolalpha
383 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100384 }
385
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000386 void VisitLoadString(HLoadString* load_string) OVERRIDE {
387 StartAttributeStream("load_kind") << load_string->GetLoadKind();
388 }
389
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100390 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000391 StartAttributeStream("check_kind") << check_cast->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100392 StartAttributeStream("must_do_null_check") << std::boolalpha
393 << check_cast->MustDoNullCheck() << std::noboolalpha;
394 }
395
396 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
Roland Levillain86503782016-02-11 19:07:30 +0000397 StartAttributeStream("check_kind") << instance_of->GetTypeCheckKind();
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100398 StartAttributeStream("must_do_null_check") << std::boolalpha
399 << instance_of->MustDoNullCheck() << std::noboolalpha;
400 }
401
Vladimir Markodce016e2016-04-28 13:10:02 +0100402 void VisitArrayLength(HArrayLength* array_length) OVERRIDE {
403 StartAttributeStream("is_string_length") << std::boolalpha
404 << array_length->IsStringLength() << std::noboolalpha;
Mark Mendellee8d9712016-07-12 11:13:15 -0400405 if (array_length->IsEmittedAtUseSite()) {
406 StartAttributeStream("emitted_at_use") << "true";
407 }
Vladimir Markodce016e2016-04-28 13:10:02 +0100408 }
409
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100410 void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE {
411 StartAttributeStream("is_string_char_at") << std::boolalpha
412 << bounds_check->IsStringCharAt() << std::noboolalpha;
413 }
414
415 void VisitArrayGet(HArrayGet* array_get) OVERRIDE {
416 StartAttributeStream("is_string_char_at") << std::boolalpha
417 << array_get->IsStringCharAt() << std::noboolalpha;
418 }
419
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100420 void VisitArraySet(HArraySet* array_set) OVERRIDE {
421 StartAttributeStream("value_can_be_null") << std::boolalpha
422 << array_set->GetValueCanBeNull() << std::noboolalpha;
Roland Levillainb133ec62016-03-23 12:40:35 +0000423 StartAttributeStream("needs_type_check") << std::boolalpha
424 << array_set->NeedsTypeCheck() << std::noboolalpha;
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100425 }
426
Roland Levillain31dd3d62016-02-16 12:21:02 +0000427 void VisitCompare(HCompare* compare) OVERRIDE {
428 ComparisonBias bias = compare->GetBias();
429 StartAttributeStream("bias") << (bias == ComparisonBias::kGtBias
430 ? "gt"
431 : (bias == ComparisonBias::kLtBias ? "lt" : "none"));
432 }
433
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100434 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray5ceac0e2017-06-26 13:19:09 +0100436 ArtMethod* method = invoke->GetResolvedMethod();
437 // We don't print signatures, which conflict with c1visualizer format.
438 static constexpr bool kWithSignature = false;
439 // Note that we can only use the graph's dex file for the unresolved case. The
440 // other invokes might be coming from inlined methods.
441 ScopedObjectAccess soa(Thread::Current());
442 std::string method_name = (method == nullptr)
443 ? GetGraph()->GetDexFile().PrettyMethod(invoke->GetDexMethodIndex(), kWithSignature)
444 : method->PrettyMethod(kWithSignature);
445 StartAttributeStream("method_name") << method_name;
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100446 }
447
Calin Juravle175dc732015-08-25 15:42:32 +0100448 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
449 VisitInvoke(invoke);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100450 StartAttributeStream("invoke_type") << invoke->GetInvokeType();
Calin Juravle175dc732015-08-25 15:42:32 +0100451 }
452
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100453 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
454 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000455 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100456 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000457 if (invoke->IsStatic()) {
458 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
459 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100460 }
461
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000462 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
463 VisitInvoke(invoke);
464 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
465 }
466
Orion Hodsonac141392017-01-13 11:53:47 +0000467 void VisitInvokePolymorphic(HInvokePolymorphic* invoke) OVERRIDE {
468 VisitInvoke(invoke);
469 StartAttributeStream("invoke_type") << "InvokePolymorphic";
470 }
471
David Brazdil11edec72016-03-24 12:40:52 +0000472 void VisitInstanceFieldGet(HInstanceFieldGet* iget) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700473 StartAttributeStream("field_name") <<
474 iget->GetFieldInfo().GetDexFile().PrettyField(iget->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000475 /* with type */ false);
476 StartAttributeStream("field_type") << iget->GetFieldType();
477 }
478
479 void VisitInstanceFieldSet(HInstanceFieldSet* iset) OVERRIDE {
David Sehr709b0702016-10-13 09:12:37 -0700480 StartAttributeStream("field_name") <<
481 iset->GetFieldInfo().GetDexFile().PrettyField(iset->GetFieldInfo().GetFieldIndex(),
David Brazdil11edec72016-03-24 12:40:52 +0000482 /* with type */ false);
483 StartAttributeStream("field_type") << iset->GetFieldType();
484 }
485
Vladimir Markobf3243b2017-08-30 14:06:54 +0100486 void VisitStaticFieldGet(HStaticFieldGet* sget) OVERRIDE {
487 StartAttributeStream("field_name") <<
488 sget->GetFieldInfo().GetDexFile().PrettyField(sget->GetFieldInfo().GetFieldIndex(),
489 /* with type */ false);
490 StartAttributeStream("field_type") << sget->GetFieldType();
491 }
492
493 void VisitStaticFieldSet(HStaticFieldSet* sset) OVERRIDE {
494 StartAttributeStream("field_name") <<
495 sset->GetFieldInfo().GetDexFile().PrettyField(sset->GetFieldInfo().GetFieldIndex(),
496 /* with type */ false);
497 StartAttributeStream("field_type") << sset->GetFieldType();
498 }
499
Calin Juravlee460d1d2015-09-29 04:52:17 +0100500 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
501 StartAttributeStream("field_type") << field_access->GetFieldType();
502 }
503
504 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
505 StartAttributeStream("field_type") << field_access->GetFieldType();
506 }
507
508 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
509 StartAttributeStream("field_type") << field_access->GetFieldType();
510 }
511
512 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
513 StartAttributeStream("field_type") << field_access->GetFieldType();
514 }
515
David Brazdilfc6a86a2015-06-26 10:33:45 +0000516 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100517 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000518 }
519
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +0000520 void VisitDeoptimize(HDeoptimize* deoptimize) OVERRIDE {
521 StartAttributeStream("kind") << deoptimize->GetKind();
522 }
523
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100524 void VisitVecOperation(HVecOperation* vec_operation) OVERRIDE {
525 StartAttributeStream("packed_type") << vec_operation->GetPackedType();
526 }
527
Aart Bikf3e61ee2017-04-12 17:09:20 -0700528 void VisitVecHalvingAdd(HVecHalvingAdd* hadd) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100529 VisitVecBinaryOperation(hadd);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700530 StartAttributeStream("unsigned") << std::boolalpha << hadd->IsUnsigned() << std::noboolalpha;
531 StartAttributeStream("rounded") << std::boolalpha << hadd->IsRounded() << std::noboolalpha;
532 }
533
Aart Bikc8e93c72017-05-10 10:49:22 -0700534 void VisitVecMin(HVecMin* min) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100535 VisitVecBinaryOperation(min);
Aart Bikc8e93c72017-05-10 10:49:22 -0700536 StartAttributeStream("unsigned") << std::boolalpha << min->IsUnsigned() << std::noboolalpha;
537 }
538
539 void VisitVecMax(HVecMax* max) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100540 VisitVecBinaryOperation(max);
Aart Bikc8e93c72017-05-10 10:49:22 -0700541 StartAttributeStream("unsigned") << std::boolalpha << max->IsUnsigned() << std::noboolalpha;
542 }
543
Artem Serovf34dd202017-04-10 17:41:46 +0100544 void VisitVecMultiplyAccumulate(HVecMultiplyAccumulate* instruction) OVERRIDE {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100545 VisitVecOperation(instruction);
Artem Serovf34dd202017-04-10 17:41:46 +0100546 StartAttributeStream("kind") << instruction->GetOpKind();
547 }
548
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300549#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
550 void VisitMultiplyAccumulate(HMultiplyAccumulate* instruction) OVERRIDE {
551 StartAttributeStream("kind") << instruction->GetOpKind();
552 }
Artem Serov7fc63502016-02-09 17:15:29 +0000553
554 void VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) OVERRIDE {
555 StartAttributeStream("kind") << instruction->GetOpKind();
556 }
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300557
Anton Kirilov74234da2017-01-13 14:42:47 +0000558 void VisitDataProcWithShifterOp(HDataProcWithShifterOp* instruction) OVERRIDE {
Alexandre Rames8626b742015-11-25 16:28:08 +0000559 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
Anton Kirilov74234da2017-01-13 14:42:47 +0000560 if (HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
Alexandre Rames8626b742015-11-25 16:28:08 +0000561 StartAttributeStream("shift") << instruction->GetShiftAmount();
562 }
563 }
Alexandre Rames418318f2015-11-20 15:55:47 +0000564#endif
565
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800566 bool IsPass(const char* name) {
567 return strcmp(pass_name_, name) == 0;
568 }
569
David Brazdilb7e4a062014-12-29 15:35:02 +0000570 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100571 output_ << instruction->DebugName();
Vladimir Markoe9004912016-06-16 16:50:52 +0100572 HConstInputsRef inputs = instruction->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100573 if (!inputs.empty()) {
574 StringList input_list;
575 for (const HInstruction* input : inputs) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100576 input_list.NewEntryStream() << DataType::TypeId(input->GetType()) << input->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100577 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100578 StartAttributeStream() << input_list;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100579 }
David Brazdilc74652862015-05-13 17:50:09 +0100580 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800581 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100582 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100583 for (HEnvironment* environment = instruction->GetEnvironment();
584 environment != nullptr;
585 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100586 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100587 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
588 HInstruction* insn = environment->GetInstructionAt(i);
589 if (insn != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100590 vregs.NewEntryStream() << DataType::TypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100591 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100592 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100593 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800594 }
David Brazdilc74652862015-05-13 17:50:09 +0100595 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800596 }
David Brazdilc74652862015-05-13 17:50:09 +0100597 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800598 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800599 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000600 && is_after_pass_
601 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100602 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100603 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100604 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100605 StartAttributeStream("ranges")
606 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
Vladimir Marko82b07402017-03-01 19:02:04 +0000607 StartAttributeStream("uses") << StringList(interval->GetUses());
608 StartAttributeStream("env_uses") << StringList(interval->GetEnvironmentUses());
David Brazdilc74652862015-05-13 17:50:09 +0100609 StartAttributeStream("is_fixed") << interval->IsFixed();
610 StartAttributeStream("is_split") << interval->IsSplit();
611 StartAttributeStream("is_low") << interval->IsLowInterval();
612 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100613 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000614 }
615
616 if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100617 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100618 LocationSummary* locations = instruction->GetLocations();
619 if (locations != nullptr) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100620 StringList input_list;
621 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
622 DumpLocation(input_list.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100623 }
David Brazdilc74652862015-05-13 17:50:09 +0100624 std::ostream& attr = StartAttributeStream("locations");
Vladimir Marko372f10e2016-05-17 16:30:10 +0100625 attr << input_list << "->";
David Brazdilc74652862015-05-13 17:50:09 +0100626 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100627 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000628 }
629
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100630 HLoopInformation* loop_info = instruction->GetBlock()->GetLoopInformation();
631 if (loop_info == nullptr) {
632 StartAttributeStream("loop") << "none";
633 } else {
634 StartAttributeStream("loop") << "B" << loop_info->GetHeader()->GetBlockId();
635 HLoopInformation* outer = loop_info->GetPreHeader()->GetLoopInformation();
636 if (outer != nullptr) {
637 StartAttributeStream("outer_loop") << "B" << outer->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000638 } else {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100639 StartAttributeStream("outer_loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000640 }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100641 StartAttributeStream("irreducible")
642 << std::boolalpha << loop_info->IsIrreducible() << std::noboolalpha;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000643 }
644
David Brazdilbadd8262016-02-02 16:28:56 +0000645 if ((IsPass(HGraphBuilder::kBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000646 || IsPass(HInliner::kInlinerPassName))
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100647 && (instruction->GetType() == DataType::Type::kReference)) {
Calin Juravle2e768302015-07-28 14:41:11 +0000648 ReferenceTypeInfo info = instruction->IsLoadClass()
649 ? instruction->AsLoadClass()->GetLoadedClassRTI()
650 : instruction->GetReferenceTypeInfo();
651 ScopedObjectAccess soa(Thread::Current());
652 if (info.IsValid()) {
David Sehr709b0702016-10-13 09:12:37 -0700653 StartAttributeStream("klass")
654 << mirror::Class::PrettyDescriptor(info.GetTypeHandle().Get());
Calin Juravle2e768302015-07-28 14:41:11 +0000655 StartAttributeStream("can_be_null")
656 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
657 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Calin Juravle98893e12015-10-02 21:05:03 +0100658 } else if (instruction->IsLoadClass()) {
659 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000660 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500661 // The NullConstant may be added to the graph during other passes that happen between
662 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
663 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
664 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000665 DCHECK(graph_in_bad_state_ ||
David Brazdilbadd8262016-02-02 16:28:56 +0000666 (!is_after_pass_ && IsPass(HGraphBuilder::kBuilderPassName)))
David Brazdil4833f5a2015-12-16 10:37:39 +0000667 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
668 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100669 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100670 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100671 if (disasm_info_ != nullptr) {
672 DCHECK(disassembler_ != nullptr);
673 // If the information is available, disassemble the code generated for
674 // this instruction.
675 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
676 if (it != disasm_info_->GetInstructionIntervals().end()
677 && it->second.start != it->second.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100678 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100679 disassembler_->Disassemble(output_, it->second.start, it->second.end);
680 }
681 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100682 }
683
684 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100685 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
686 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100687 int bci = 0;
Vladimir Marko46817b82016-03-29 12:21:58 +0100688 size_t num_uses = instruction->GetUses().SizeSlow();
David Brazdilea55b932015-01-27 17:12:29 +0000689 AddIndent();
690 output_ << bci << " " << num_uses << " "
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100691 << DataType::TypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000692 PrintInstruction(instruction);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100693 output_ << " " << kEndInstructionMarker << "\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100694 }
695 }
696
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100697 void DumpStartOfDisassemblyBlock(const char* block_name,
698 int predecessor_index,
699 int successor_index) {
700 StartTag("block");
701 PrintProperty("name", block_name);
702 PrintInt("from_bci", -1);
703 PrintInt("to_bci", -1);
704 if (predecessor_index != -1) {
705 PrintProperty("predecessors", "B", predecessor_index);
706 } else {
707 PrintEmptyProperty("predecessors");
708 }
709 if (successor_index != -1) {
710 PrintProperty("successors", "B", successor_index);
711 } else {
712 PrintEmptyProperty("successors");
713 }
714 PrintEmptyProperty("xhandlers");
715 PrintEmptyProperty("flags");
716 StartTag("states");
717 StartTag("locals");
718 PrintInt("size", 0);
719 PrintProperty("method", "None");
720 EndTag("locals");
721 EndTag("states");
722 StartTag("HIR");
723 }
724
725 void DumpEndOfDisassemblyBlock() {
726 EndTag("HIR");
727 EndTag("block");
728 }
729
730 void DumpDisassemblyBlockForFrameEntry() {
731 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
732 -1,
733 GetGraph()->GetEntryBlock()->GetBlockId());
734 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
735 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
736 if (frame_entry.start != frame_entry.end) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100737 output_ << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100738 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
739 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100740 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100741 DumpEndOfDisassemblyBlock();
742 }
743
744 void DumpDisassemblyBlockForSlowPaths() {
745 if (disasm_info_->GetSlowPathIntervals().empty()) {
746 return;
747 }
748 // If the graph has an exit block we attach the block for the slow paths
749 // after it. Else we just add the block to the graph without linking it to
750 // any other.
751 DumpStartOfDisassemblyBlock(
752 kDisassemblyBlockSlowPaths,
753 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
754 -1);
755 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
David Brazdilfa02c9d2016-03-30 09:41:02 +0100756 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100757 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
David Brazdilfa02c9d2016-03-30 09:41:02 +0100758 output_ << kEndInstructionMarker << "\n";
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100759 }
760 DumpEndOfDisassemblyBlock();
761 }
762
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100763 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100764 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100765 std::string pass_desc = std::string(pass_name_)
766 + " ("
767 + (is_after_pass_ ? "after" : "before")
768 + (graph_in_bad_state_ ? ", bad_state" : "")
769 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000770 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100771 if (disasm_info_ != nullptr) {
772 DumpDisassemblyBlockForFrameEntry();
773 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100774 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100775 if (disasm_info_ != nullptr) {
776 DumpDisassemblyBlockForSlowPaths();
777 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100778 EndTag("cfg");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100779 Flush();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100780 }
781
David Brazdilb7e4a062014-12-29 15:35:02 +0000782 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100783 StartTag("block");
784 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100785 if (block->GetLifetimeStart() != kNoLifetime) {
786 // Piggy back on these fields to show the lifetime of the block.
787 PrintInt("from_bci", block->GetLifetimeStart());
788 PrintInt("to_bci", block->GetLifetimeEnd());
789 } else {
790 PrintInt("from_bci", -1);
791 PrintInt("to_bci", -1);
792 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100793 PrintPredecessors(block);
794 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000795 PrintExceptionHandlers(block);
796
797 if (block->IsCatchBlock()) {
798 PrintProperty("flags", "catch_block");
799 } else {
800 PrintEmptyProperty("flags");
801 }
802
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100803 if (block->GetDominator() != nullptr) {
804 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
805 }
806
807 StartTag("states");
808 StartTag("locals");
809 PrintInt("size", 0);
810 PrintProperty("method", "None");
811 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
812 AddIndent();
813 HInstruction* instruction = it.Current();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100814 output_ << instruction->GetId() << " " << DataType::TypeId(instruction->GetType())
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100815 << instruction->GetId() << "[ ";
Vladimir Marko372f10e2016-05-17 16:30:10 +0100816 for (const HInstruction* input : instruction->GetInputs()) {
817 output_ << input->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100818 }
David Brazdilfa02c9d2016-03-30 09:41:02 +0100819 output_ << "]\n";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100820 }
821 EndTag("locals");
822 EndTag("states");
823
824 StartTag("HIR");
825 PrintInstructions(block->GetPhis());
826 PrintInstructions(block->GetInstructions());
827 EndTag("HIR");
828 EndTag("block");
829 }
830
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100831 static constexpr const char* const kEndInstructionMarker = "<|@";
832 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
833 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
834
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100835 private:
836 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100837 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000838 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100839 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100840 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100841 const DisassemblyInformation* disasm_info_;
842 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100843 size_t indent_;
844
845 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
846};
847
848HGraphVisualizer::HGraphVisualizer(std::ostream* output,
849 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100850 const CodeGenerator& codegen)
851 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100852
David Brazdil62e074f2015-04-07 18:09:37 +0100853void HGraphVisualizer::PrintHeader(const char* method_name) const {
854 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100855 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100856 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000857 printer.PrintProperty("name", method_name);
858 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100859 printer.PrintTime("date");
860 printer.EndTag("compilation");
David Brazdilfa02c9d2016-03-30 09:41:02 +0100861 printer.Flush();
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100862}
863
David Brazdilffee3d32015-07-06 11:48:53 +0100864void HGraphVisualizer::DumpGraph(const char* pass_name,
865 bool is_after_pass,
866 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000867 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100868 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100869 HGraphVisualizerPrinter printer(graph_,
870 *output_,
871 pass_name,
872 is_after_pass,
873 graph_in_bad_state,
874 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000875 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100876 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100877}
878
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100879void HGraphVisualizer::DumpGraphWithDisassembly() const {
880 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100881 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100882 HGraphVisualizerPrinter printer(graph_,
883 *output_,
884 "disassembly",
885 /* is_after_pass */ true,
886 /* graph_in_bad_state */ false,
887 codegen_,
888 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100889 printer.Run();
890 }
891}
892
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100893} // namespace art