blob: 30d61ef040aa799289dcab7b734e52248b28a77a [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
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010024#include "code_generator.h"
David Brazdila4b8c212015-05-07 09:59:30 +010025#include "dead_code_elimination.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010026#include "disassembler.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080027#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010028#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000029#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010030#include "reference_type_propagation.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080031#include "register_allocator.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010032#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010033#include "utils/assembler.h"
David Brazdilc74652862015-05-13 17:50:09 +010034
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010035namespace art {
36
David Brazdilc74652862015-05-13 17:50:09 +010037static bool HasWhitespace(const char* str) {
38 DCHECK(str != nullptr);
39 while (str[0] != 0) {
40 if (isspace(str[0])) {
41 return true;
42 }
43 str++;
44 }
45 return false;
46}
47
48class StringList {
49 public:
David Brazdilc7a24852015-05-15 16:44:05 +010050 enum Format {
51 kArrayBrackets,
52 kSetBrackets,
53 };
54
David Brazdilc74652862015-05-13 17:50:09 +010055 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010056 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010057
58 // Construct StringList from a linked list. List element class T
59 // must provide methods `GetNext` and `Dump`.
60 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010061 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010062 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
63 current->Dump(NewEntryStream());
64 }
65 }
66
67 std::ostream& NewEntryStream() {
68 if (is_empty_) {
69 is_empty_ = false;
70 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010071 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010072 }
73 return sstream_;
74 }
75
76 private:
David Brazdilc7a24852015-05-15 16:44:05 +010077 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010078 bool is_empty_;
79 std::ostringstream sstream_;
80
81 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
82};
83
84std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010085 switch (list.format_) {
86 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
87 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
88 default:
89 LOG(FATAL) << "Invalid StringList format";
90 UNREACHABLE();
91 }
David Brazdilc74652862015-05-13 17:50:09 +010092}
93
Alexandre Rameseb7b7392015-06-19 14:47:01 +010094typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
95 DisassemblerOptions* options);
96class HGraphVisualizerDisassembler {
97 public:
98 HGraphVisualizerDisassembler(InstructionSet instruction_set, const uint8_t* base_address)
David Brazdil3a690be2015-06-23 10:22:38 +010099 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100100 libart_disassembler_handle_ =
101 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
102 if (libart_disassembler_handle_ == nullptr) {
103 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
104 return;
105 }
106 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
107 dlsym(libart_disassembler_handle_, "create_disassembler"));
108 if (create_disassembler == nullptr) {
109 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
110 return;
111 }
112 // Reading the disassembly from 0x0 is easier, so we print relative
113 // addresses. We will only disassemble the code once everything has
114 // been generated, so we can read data in literal pools.
115 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
116 instruction_set,
117 new DisassemblerOptions(/* absolute_addresses */ false,
118 base_address,
119 /* can_read_literals */ true)));
120 }
121
122 ~HGraphVisualizerDisassembler() {
123 // We need to call ~Disassembler() before we close the library.
124 disassembler_.reset();
125 if (libart_disassembler_handle_ != nullptr) {
126 dlclose(libart_disassembler_handle_);
127 }
128 }
129
130 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100131 if (disassembler_ == nullptr) {
132 return;
133 }
134
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100135 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
136 if (instruction_set_ == kThumb2) {
137 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
138 // address is used to distinguish between the two.
139 base += 1;
140 }
141 disassembler_->Dump(output, base + start, base + end);
142 }
143
144 private:
145 InstructionSet instruction_set_;
146 std::unique_ptr<Disassembler> disassembler_;
147
148 void* libart_disassembler_handle_;
149};
150
151
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100152/**
153 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
154 */
155class HGraphVisualizerPrinter : public HGraphVisitor {
156 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100157 HGraphVisualizerPrinter(HGraph* graph,
158 std::ostream& output,
159 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000160 bool is_after_pass,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100161 const CodeGenerator& codegen,
162 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100163 : HGraphVisitor(graph),
164 output_(output),
165 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000166 is_after_pass_(is_after_pass),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100167 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100168 disasm_info_(disasm_info),
169 disassembler_(disasm_info_ != nullptr
170 ? new HGraphVisualizerDisassembler(
171 codegen_.GetInstructionSet(),
172 codegen_.GetAssembler().CodeBufferBaseAddress())
173 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100174 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100175
176 void StartTag(const char* name) {
177 AddIndent();
178 output_ << "begin_" << name << std::endl;
179 indent_++;
180 }
181
182 void EndTag(const char* name) {
183 indent_--;
184 AddIndent();
185 output_ << "end_" << name << std::endl;
186 }
187
188 void PrintProperty(const char* name, const char* property) {
189 AddIndent();
190 output_ << name << " \"" << property << "\"" << std::endl;
191 }
192
193 void PrintProperty(const char* name, const char* property, int id) {
194 AddIndent();
195 output_ << name << " \"" << property << id << "\"" << std::endl;
196 }
197
198 void PrintEmptyProperty(const char* name) {
199 AddIndent();
200 output_ << name << std::endl;
201 }
202
203 void PrintTime(const char* name) {
204 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800205 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100206 }
207
208 void PrintInt(const char* name, int value) {
209 AddIndent();
210 output_ << name << " " << value << std::endl;
211 }
212
213 void AddIndent() {
214 for (size_t i = 0; i < indent_; ++i) {
215 output_ << " ";
216 }
217 }
218
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100219 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100220 // Note that Primitive::Descriptor would not work for us
221 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100222 switch (type) {
223 case Primitive::kPrimBoolean: return 'z';
224 case Primitive::kPrimByte: return 'b';
225 case Primitive::kPrimChar: return 'c';
226 case Primitive::kPrimShort: return 's';
227 case Primitive::kPrimInt: return 'i';
228 case Primitive::kPrimLong: return 'j';
229 case Primitive::kPrimFloat: return 'f';
230 case Primitive::kPrimDouble: return 'd';
231 case Primitive::kPrimNot: return 'l';
232 case Primitive::kPrimVoid: return 'v';
233 }
234 LOG(FATAL) << "Unreachable";
235 return 'v';
236 }
237
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100238 void PrintPredecessors(HBasicBlock* block) {
239 AddIndent();
240 output_ << "predecessors";
241 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
242 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
243 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
244 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100245 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
246 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
247 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100248 output_<< std::endl;
249 }
250
251 void PrintSuccessors(HBasicBlock* block) {
252 AddIndent();
253 output_ << "successors";
254 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000255 if (!block->IsExceptionalSuccessor(i)) {
256 HBasicBlock* successor = block->GetSuccessors().Get(i);
257 output_ << " \"B" << successor->GetBlockId() << "\" ";
258 }
259 }
260 output_<< std::endl;
261 }
262
263 void PrintExceptionHandlers(HBasicBlock* block) {
264 AddIndent();
265 output_ << "xhandlers";
266 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
267 if (block->IsExceptionalSuccessor(i)) {
268 HBasicBlock* handler = block->GetSuccessors().Get(i);
269 output_ << " \"B" << handler->GetBlockId() << "\" ";
270 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100271 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100272 if (block->IsExitBlock() &&
273 (disasm_info_ != nullptr) &&
274 !disasm_info_->GetSlowPathIntervals().empty()) {
275 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
276 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277 output_<< std::endl;
278 }
279
David Brazdilc74652862015-05-13 17:50:09 +0100280 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100281 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100282 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100283 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100284 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100285 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100286 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100287 HConstant* constant = location.GetConstant();
288 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100289 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100290 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100291 stream << constant->AsLongConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100292 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100293 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100294 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100295 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100296 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000297 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100298 codegen_.DumpFloatingPointRegister(stream, location.low());
299 stream << "|";
300 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000301 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100302 codegen_.DumpCoreRegister(stream, location.low());
303 stream << "|";
304 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400305 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100306 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100307 } else {
308 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100309 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100310 }
311 }
312
David Brazdilc74652862015-05-13 17:50:09 +0100313 std::ostream& StartAttributeStream(const char* name = nullptr) {
314 if (name == nullptr) {
315 output_ << " ";
316 } else {
317 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
318 output_ << " " << name << ":";
319 }
320 return output_;
321 }
322
David Brazdilb7e4a062014-12-29 15:35:02 +0000323 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100324 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
325 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100326 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
327 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100328 std::ostream& str = moves.NewEntryStream();
329 DumpLocation(str, move->GetSource());
330 str << "->";
331 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100332 }
David Brazdilc74652862015-05-13 17:50:09 +0100333 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100334 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100335
David Brazdil36cf0952015-01-08 19:28:33 +0000336 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100337 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000338 }
339
David Brazdil36cf0952015-01-08 19:28:33 +0000340 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100341 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000342 }
343
David Brazdil36cf0952015-01-08 19:28:33 +0000344 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100345 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000346 }
347
David Brazdil36cf0952015-01-08 19:28:33 +0000348 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100349 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000350 }
351
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100353 StartAttributeStream("reg") << phi->GetRegNumber();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000354 }
355
Calin Juravle27df7582015-04-17 19:12:31 +0100356 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100357 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100358 }
359
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100360 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Calin Juravle0ba218d2015-05-19 18:46:01 +0100361 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100362 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100363 }
364
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100365 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
366 StartAttributeStream("must_do_null_check") << std::boolalpha
367 << check_cast->MustDoNullCheck() << std::noboolalpha;
368 }
369
370 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
371 StartAttributeStream("must_do_null_check") << std::boolalpha
372 << instance_of->MustDoNullCheck() << std::noboolalpha;
373 }
374
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100375 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
376 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
377 StartAttributeStream("recursive") << std::boolalpha
378 << invoke->IsRecursive()
379 << std::noboolalpha;
380 }
381
David Brazdilfc6a86a2015-06-26 10:33:45 +0000382 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
383 StartAttributeStream("is_entry") << std::boolalpha
384 << try_boundary->IsTryEntry()
385 << std::noboolalpha;
386 StartAttributeStream("is_exit") << std::boolalpha
387 << try_boundary->IsTryExit()
388 << std::noboolalpha;
389 }
390
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800391 bool IsPass(const char* name) {
392 return strcmp(pass_name_, name) == 0;
393 }
394
David Brazdilb7e4a062014-12-29 15:35:02 +0000395 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100396 output_ << instruction->DebugName();
397 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100398 StringList inputs;
399 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
400 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100401 }
David Brazdilc74652862015-05-13 17:50:09 +0100402 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100403 }
David Brazdilc74652862015-05-13 17:50:09 +0100404 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800405 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100406 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100407 for (HEnvironment* environment = instruction->GetEnvironment();
408 environment != nullptr;
409 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100410 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100411 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
412 HInstruction* insn = environment->GetInstructionAt(i);
413 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100414 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100415 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100416 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100417 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800418 }
David Brazdilc74652862015-05-13 17:50:09 +0100419 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800420 }
David Brazdilc74652862015-05-13 17:50:09 +0100421 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800422 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800423 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000424 && is_after_pass_
425 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100426 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100427 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100428 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100429 StartAttributeStream("ranges")
430 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100431 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
432 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
433 StartAttributeStream("is_fixed") << interval->IsFixed();
434 StartAttributeStream("is_split") << interval->IsSplit();
435 StartAttributeStream("is_low") << interval->IsLowInterval();
436 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100437 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800438 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100439 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100440 LocationSummary* locations = instruction->GetLocations();
441 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100442 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100443 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100444 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100445 }
David Brazdilc74652862015-05-13 17:50:09 +0100446 std::ostream& attr = StartAttributeStream("locations");
447 attr << inputs << "->";
448 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100449 }
David Brazdila4b8c212015-05-07 09:59:30 +0100450 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
451 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000452 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
453 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100454 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000455 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100456 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000457 }
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100458 } else if (IsPass(ReferenceTypePropagation::kReferenceTypePropagationPassName)
459 && is_after_pass_) {
460 if (instruction->GetType() == Primitive::kPrimNot) {
461 if (instruction->IsLoadClass()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +0100462 ReferenceTypeInfo info = instruction->AsLoadClass()->GetLoadedClassRTI();
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100463 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray66389fb2015-06-19 10:35:42 +0100464 if (info.GetTypeHandle().GetReference() != nullptr) {
465 StartAttributeStream("klass") << info.GetTypeHandle().Get();
466 } else {
467 StartAttributeStream("klass") << "unresolved";
468 }
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100469 } else {
470 ReferenceTypeInfo info = instruction->GetReferenceTypeInfo();
471 if (info.IsTop()) {
472 StartAttributeStream("klass") << "java.lang.Object";
473 } else {
474 ScopedObjectAccess soa(Thread::Current());
475 StartAttributeStream("klass") << PrettyClass(info.GetTypeHandle().Get());
476 }
477 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
478 }
479 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100480 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100481 if (disasm_info_ != nullptr) {
482 DCHECK(disassembler_ != nullptr);
483 // If the information is available, disassemble the code generated for
484 // this instruction.
485 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
486 if (it != disasm_info_->GetInstructionIntervals().end()
487 && it->second.start != it->second.end) {
488 output_ << std::endl;
489 disassembler_->Disassemble(output_, it->second.start, it->second.end);
490 }
491 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100492 }
493
494 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100495 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
496 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100497 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000498 size_t num_uses = 0;
499 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
500 !use_it.Done();
501 use_it.Advance()) {
502 ++num_uses;
503 }
504 AddIndent();
505 output_ << bci << " " << num_uses << " "
506 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000507 PrintInstruction(instruction);
David Brazdilc74652862015-05-13 17:50:09 +0100508 output_ << " " << kEndInstructionMarker << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100509 }
510 }
511
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100512 void DumpStartOfDisassemblyBlock(const char* block_name,
513 int predecessor_index,
514 int successor_index) {
515 StartTag("block");
516 PrintProperty("name", block_name);
517 PrintInt("from_bci", -1);
518 PrintInt("to_bci", -1);
519 if (predecessor_index != -1) {
520 PrintProperty("predecessors", "B", predecessor_index);
521 } else {
522 PrintEmptyProperty("predecessors");
523 }
524 if (successor_index != -1) {
525 PrintProperty("successors", "B", successor_index);
526 } else {
527 PrintEmptyProperty("successors");
528 }
529 PrintEmptyProperty("xhandlers");
530 PrintEmptyProperty("flags");
531 StartTag("states");
532 StartTag("locals");
533 PrintInt("size", 0);
534 PrintProperty("method", "None");
535 EndTag("locals");
536 EndTag("states");
537 StartTag("HIR");
538 }
539
540 void DumpEndOfDisassemblyBlock() {
541 EndTag("HIR");
542 EndTag("block");
543 }
544
545 void DumpDisassemblyBlockForFrameEntry() {
546 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
547 -1,
548 GetGraph()->GetEntryBlock()->GetBlockId());
549 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
550 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
551 if (frame_entry.start != frame_entry.end) {
552 output_ << std::endl;
553 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
554 }
555 output_ << kEndInstructionMarker << std::endl;
556 DumpEndOfDisassemblyBlock();
557 }
558
559 void DumpDisassemblyBlockForSlowPaths() {
560 if (disasm_info_->GetSlowPathIntervals().empty()) {
561 return;
562 }
563 // If the graph has an exit block we attach the block for the slow paths
564 // after it. Else we just add the block to the graph without linking it to
565 // any other.
566 DumpStartOfDisassemblyBlock(
567 kDisassemblyBlockSlowPaths,
568 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
569 -1);
570 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
571 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << std::endl;
572 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
573 output_ << kEndInstructionMarker << std::endl;
574 }
575 DumpEndOfDisassemblyBlock();
576 }
577
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100578 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100579 StartTag("cfg");
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000580 std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)");
581 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100582 if (disasm_info_ != nullptr) {
583 DumpDisassemblyBlockForFrameEntry();
584 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100585 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100586 if (disasm_info_ != nullptr) {
587 DumpDisassemblyBlockForSlowPaths();
588 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100589 EndTag("cfg");
590 }
591
David Brazdilb7e4a062014-12-29 15:35:02 +0000592 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100593 StartTag("block");
594 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100595 if (block->GetLifetimeStart() != kNoLifetime) {
596 // Piggy back on these fields to show the lifetime of the block.
597 PrintInt("from_bci", block->GetLifetimeStart());
598 PrintInt("to_bci", block->GetLifetimeEnd());
599 } else {
600 PrintInt("from_bci", -1);
601 PrintInt("to_bci", -1);
602 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100603 PrintPredecessors(block);
604 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000605 PrintExceptionHandlers(block);
606
607 if (block->IsCatchBlock()) {
608 PrintProperty("flags", "catch_block");
609 } else {
610 PrintEmptyProperty("flags");
611 }
612
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100613 if (block->GetDominator() != nullptr) {
614 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
615 }
616
617 StartTag("states");
618 StartTag("locals");
619 PrintInt("size", 0);
620 PrintProperty("method", "None");
621 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
622 AddIndent();
623 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100624 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
625 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100626 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
627 output_ << inputs.Current()->GetId() << " ";
628 }
629 output_ << "]" << std::endl;
630 }
631 EndTag("locals");
632 EndTag("states");
633
634 StartTag("HIR");
635 PrintInstructions(block->GetPhis());
636 PrintInstructions(block->GetInstructions());
637 EndTag("HIR");
638 EndTag("block");
639 }
640
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100641 static constexpr const char* const kEndInstructionMarker = "<|@";
642 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
643 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
644
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100645 private:
646 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100647 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000648 const bool is_after_pass_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100649 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100650 const DisassemblyInformation* disasm_info_;
651 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100652 size_t indent_;
653
654 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
655};
656
657HGraphVisualizer::HGraphVisualizer(std::ostream* output,
658 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100659 const CodeGenerator& codegen)
660 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100661
David Brazdil62e074f2015-04-07 18:09:37 +0100662void HGraphVisualizer::PrintHeader(const char* method_name) const {
663 DCHECK(output_ != nullptr);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000664 HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100665 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000666 printer.PrintProperty("name", method_name);
667 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100668 printer.PrintTime("date");
669 printer.EndTag("compilation");
670}
671
David Brazdilee690a32014-12-01 17:04:16 +0000672void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000673 DCHECK(output_ != nullptr);
674 if (!graph_->GetBlocks().IsEmpty()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000675 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000676 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100677 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100678}
679
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100680void HGraphVisualizer::DumpGraphWithDisassembly() const {
681 DCHECK(output_ != nullptr);
682 if (!graph_->GetBlocks().IsEmpty()) {
683 HGraphVisualizerPrinter printer(
684 graph_, *output_, "disassembly", true, codegen_, codegen_.GetDisassemblyInformation());
685 printer.Run();
686 }
687}
688
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100689} // namespace art