blob: 2b85c7c6f9c59bac36761684ea7c44b6bf8b2252 [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)
99 : instruction_set_(instruction_set) {
100 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 {
131 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
132 if (instruction_set_ == kThumb2) {
133 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
134 // address is used to distinguish between the two.
135 base += 1;
136 }
137 disassembler_->Dump(output, base + start, base + end);
138 }
139
140 private:
141 InstructionSet instruction_set_;
142 std::unique_ptr<Disassembler> disassembler_;
143
144 void* libart_disassembler_handle_;
145};
146
147
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100148/**
149 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
150 */
151class HGraphVisualizerPrinter : public HGraphVisitor {
152 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100153 HGraphVisualizerPrinter(HGraph* graph,
154 std::ostream& output,
155 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000156 bool is_after_pass,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100157 const CodeGenerator& codegen,
158 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100159 : HGraphVisitor(graph),
160 output_(output),
161 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000162 is_after_pass_(is_after_pass),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100163 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100164 disasm_info_(disasm_info),
165 disassembler_(disasm_info_ != nullptr
166 ? new HGraphVisualizerDisassembler(
167 codegen_.GetInstructionSet(),
168 codegen_.GetAssembler().CodeBufferBaseAddress())
169 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100170 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100171
172 void StartTag(const char* name) {
173 AddIndent();
174 output_ << "begin_" << name << std::endl;
175 indent_++;
176 }
177
178 void EndTag(const char* name) {
179 indent_--;
180 AddIndent();
181 output_ << "end_" << name << std::endl;
182 }
183
184 void PrintProperty(const char* name, const char* property) {
185 AddIndent();
186 output_ << name << " \"" << property << "\"" << std::endl;
187 }
188
189 void PrintProperty(const char* name, const char* property, int id) {
190 AddIndent();
191 output_ << name << " \"" << property << id << "\"" << std::endl;
192 }
193
194 void PrintEmptyProperty(const char* name) {
195 AddIndent();
196 output_ << name << std::endl;
197 }
198
199 void PrintTime(const char* name) {
200 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800201 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100202 }
203
204 void PrintInt(const char* name, int value) {
205 AddIndent();
206 output_ << name << " " << value << std::endl;
207 }
208
209 void AddIndent() {
210 for (size_t i = 0; i < indent_; ++i) {
211 output_ << " ";
212 }
213 }
214
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100215 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100216 // Note that Primitive::Descriptor would not work for us
217 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100218 switch (type) {
219 case Primitive::kPrimBoolean: return 'z';
220 case Primitive::kPrimByte: return 'b';
221 case Primitive::kPrimChar: return 'c';
222 case Primitive::kPrimShort: return 's';
223 case Primitive::kPrimInt: return 'i';
224 case Primitive::kPrimLong: return 'j';
225 case Primitive::kPrimFloat: return 'f';
226 case Primitive::kPrimDouble: return 'd';
227 case Primitive::kPrimNot: return 'l';
228 case Primitive::kPrimVoid: return 'v';
229 }
230 LOG(FATAL) << "Unreachable";
231 return 'v';
232 }
233
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100234 void PrintPredecessors(HBasicBlock* block) {
235 AddIndent();
236 output_ << "predecessors";
237 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
238 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
239 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
240 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100241 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
242 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
243 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100244 output_<< std::endl;
245 }
246
247 void PrintSuccessors(HBasicBlock* block) {
248 AddIndent();
249 output_ << "successors";
250 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
251 HBasicBlock* successor = block->GetSuccessors().Get(i);
252 output_ << " \"B" << successor->GetBlockId() << "\" ";
253 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100254 if (block->IsExitBlock() &&
255 (disasm_info_ != nullptr) &&
256 !disasm_info_->GetSlowPathIntervals().empty()) {
257 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
258 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100259 output_<< std::endl;
260 }
261
David Brazdilc74652862015-05-13 17:50:09 +0100262 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100263 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100264 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100265 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100266 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100267 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100268 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100269 HConstant* constant = location.GetConstant();
270 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100271 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100272 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100273 stream << constant->AsLongConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100274 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100275 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100276 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100277 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100278 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000279 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100280 codegen_.DumpFloatingPointRegister(stream, location.low());
281 stream << "|";
282 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000283 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100284 codegen_.DumpCoreRegister(stream, location.low());
285 stream << "|";
286 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400287 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100288 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100289 } else {
290 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100291 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100292 }
293 }
294
David Brazdilc74652862015-05-13 17:50:09 +0100295 std::ostream& StartAttributeStream(const char* name = nullptr) {
296 if (name == nullptr) {
297 output_ << " ";
298 } else {
299 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
300 output_ << " " << name << ":";
301 }
302 return output_;
303 }
304
David Brazdilb7e4a062014-12-29 15:35:02 +0000305 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100306 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
307 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100308 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
309 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100310 std::ostream& str = moves.NewEntryStream();
311 DumpLocation(str, move->GetSource());
312 str << "->";
313 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100314 }
David Brazdilc74652862015-05-13 17:50:09 +0100315 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100316 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100317
David Brazdil36cf0952015-01-08 19:28:33 +0000318 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100319 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000320 }
321
David Brazdil36cf0952015-01-08 19:28:33 +0000322 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100323 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000324 }
325
David Brazdil36cf0952015-01-08 19:28:33 +0000326 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100327 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000328 }
329
David Brazdil36cf0952015-01-08 19:28:33 +0000330 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100331 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000332 }
333
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000334 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100335 StartAttributeStream("reg") << phi->GetRegNumber();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000336 }
337
Calin Juravle27df7582015-04-17 19:12:31 +0100338 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100339 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100340 }
341
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100342 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Calin Juravle0ba218d2015-05-19 18:46:01 +0100343 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100344 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100345 }
346
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100347 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
348 StartAttributeStream("must_do_null_check") << std::boolalpha
349 << check_cast->MustDoNullCheck() << std::noboolalpha;
350 }
351
352 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
353 StartAttributeStream("must_do_null_check") << std::boolalpha
354 << instance_of->MustDoNullCheck() << std::noboolalpha;
355 }
356
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100357 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
358 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
359 StartAttributeStream("recursive") << std::boolalpha
360 << invoke->IsRecursive()
361 << std::noboolalpha;
362 }
363
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800364 bool IsPass(const char* name) {
365 return strcmp(pass_name_, name) == 0;
366 }
367
David Brazdilb7e4a062014-12-29 15:35:02 +0000368 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100369 output_ << instruction->DebugName();
370 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100371 StringList inputs;
372 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
373 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100374 }
David Brazdilc74652862015-05-13 17:50:09 +0100375 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100376 }
David Brazdilc74652862015-05-13 17:50:09 +0100377 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800378 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100379 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100380 for (HEnvironment* environment = instruction->GetEnvironment();
381 environment != nullptr;
382 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100383 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100384 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
385 HInstruction* insn = environment->GetInstructionAt(i);
386 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100387 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100388 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100389 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100390 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800391 }
David Brazdilc74652862015-05-13 17:50:09 +0100392 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800393 }
David Brazdilc74652862015-05-13 17:50:09 +0100394 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800395 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800396 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000397 && is_after_pass_
398 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100399 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100401 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100402 StartAttributeStream("ranges")
403 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100404 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
405 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
406 StartAttributeStream("is_fixed") << interval->IsFixed();
407 StartAttributeStream("is_split") << interval->IsSplit();
408 StartAttributeStream("is_low") << interval->IsLowInterval();
409 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100410 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800411 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100412 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100413 LocationSummary* locations = instruction->GetLocations();
414 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100415 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100416 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100417 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100418 }
David Brazdilc74652862015-05-13 17:50:09 +0100419 std::ostream& attr = StartAttributeStream("locations");
420 attr << inputs << "->";
421 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100422 }
David Brazdila4b8c212015-05-07 09:59:30 +0100423 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
424 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000425 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
426 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100427 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000428 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100429 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000430 }
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100431 } else if (IsPass(ReferenceTypePropagation::kReferenceTypePropagationPassName)
432 && is_after_pass_) {
433 if (instruction->GetType() == Primitive::kPrimNot) {
434 if (instruction->IsLoadClass()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +0100435 ReferenceTypeInfo info = instruction->AsLoadClass()->GetLoadedClassRTI();
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100436 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray66389fb2015-06-19 10:35:42 +0100437 if (info.GetTypeHandle().GetReference() != nullptr) {
438 StartAttributeStream("klass") << info.GetTypeHandle().Get();
439 } else {
440 StartAttributeStream("klass") << "unresolved";
441 }
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100442 } else {
443 ReferenceTypeInfo info = instruction->GetReferenceTypeInfo();
444 if (info.IsTop()) {
445 StartAttributeStream("klass") << "java.lang.Object";
446 } else {
447 ScopedObjectAccess soa(Thread::Current());
448 StartAttributeStream("klass") << PrettyClass(info.GetTypeHandle().Get());
449 }
450 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
451 }
452 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100453 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100454 if (disasm_info_ != nullptr) {
455 DCHECK(disassembler_ != nullptr);
456 // If the information is available, disassemble the code generated for
457 // this instruction.
458 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
459 if (it != disasm_info_->GetInstructionIntervals().end()
460 && it->second.start != it->second.end) {
461 output_ << std::endl;
462 disassembler_->Disassemble(output_, it->second.start, it->second.end);
463 }
464 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100465 }
466
467 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100468 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
469 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100470 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000471 size_t num_uses = 0;
472 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
473 !use_it.Done();
474 use_it.Advance()) {
475 ++num_uses;
476 }
477 AddIndent();
478 output_ << bci << " " << num_uses << " "
479 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000480 PrintInstruction(instruction);
David Brazdilc74652862015-05-13 17:50:09 +0100481 output_ << " " << kEndInstructionMarker << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100482 }
483 }
484
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100485 void DumpStartOfDisassemblyBlock(const char* block_name,
486 int predecessor_index,
487 int successor_index) {
488 StartTag("block");
489 PrintProperty("name", block_name);
490 PrintInt("from_bci", -1);
491 PrintInt("to_bci", -1);
492 if (predecessor_index != -1) {
493 PrintProperty("predecessors", "B", predecessor_index);
494 } else {
495 PrintEmptyProperty("predecessors");
496 }
497 if (successor_index != -1) {
498 PrintProperty("successors", "B", successor_index);
499 } else {
500 PrintEmptyProperty("successors");
501 }
502 PrintEmptyProperty("xhandlers");
503 PrintEmptyProperty("flags");
504 StartTag("states");
505 StartTag("locals");
506 PrintInt("size", 0);
507 PrintProperty("method", "None");
508 EndTag("locals");
509 EndTag("states");
510 StartTag("HIR");
511 }
512
513 void DumpEndOfDisassemblyBlock() {
514 EndTag("HIR");
515 EndTag("block");
516 }
517
518 void DumpDisassemblyBlockForFrameEntry() {
519 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
520 -1,
521 GetGraph()->GetEntryBlock()->GetBlockId());
522 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
523 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
524 if (frame_entry.start != frame_entry.end) {
525 output_ << std::endl;
526 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
527 }
528 output_ << kEndInstructionMarker << std::endl;
529 DumpEndOfDisassemblyBlock();
530 }
531
532 void DumpDisassemblyBlockForSlowPaths() {
533 if (disasm_info_->GetSlowPathIntervals().empty()) {
534 return;
535 }
536 // If the graph has an exit block we attach the block for the slow paths
537 // after it. Else we just add the block to the graph without linking it to
538 // any other.
539 DumpStartOfDisassemblyBlock(
540 kDisassemblyBlockSlowPaths,
541 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
542 -1);
543 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
544 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << std::endl;
545 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
546 output_ << kEndInstructionMarker << std::endl;
547 }
548 DumpEndOfDisassemblyBlock();
549 }
550
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100551 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100552 StartTag("cfg");
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000553 std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)");
554 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100555 if (disasm_info_ != nullptr) {
556 DumpDisassemblyBlockForFrameEntry();
557 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100558 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100559 if (disasm_info_ != nullptr) {
560 DumpDisassemblyBlockForSlowPaths();
561 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100562 EndTag("cfg");
563 }
564
David Brazdilb7e4a062014-12-29 15:35:02 +0000565 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100566 StartTag("block");
567 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100568 if (block->GetLifetimeStart() != kNoLifetime) {
569 // Piggy back on these fields to show the lifetime of the block.
570 PrintInt("from_bci", block->GetLifetimeStart());
571 PrintInt("to_bci", block->GetLifetimeEnd());
572 } else {
573 PrintInt("from_bci", -1);
574 PrintInt("to_bci", -1);
575 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100576 PrintPredecessors(block);
577 PrintSuccessors(block);
578 PrintEmptyProperty("xhandlers");
579 PrintEmptyProperty("flags");
580 if (block->GetDominator() != nullptr) {
581 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
582 }
583
584 StartTag("states");
585 StartTag("locals");
586 PrintInt("size", 0);
587 PrintProperty("method", "None");
588 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
589 AddIndent();
590 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100591 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
592 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100593 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
594 output_ << inputs.Current()->GetId() << " ";
595 }
596 output_ << "]" << std::endl;
597 }
598 EndTag("locals");
599 EndTag("states");
600
601 StartTag("HIR");
602 PrintInstructions(block->GetPhis());
603 PrintInstructions(block->GetInstructions());
604 EndTag("HIR");
605 EndTag("block");
606 }
607
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100608 static constexpr const char* const kEndInstructionMarker = "<|@";
609 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
610 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
611
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100612 private:
613 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100614 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000615 const bool is_after_pass_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100616 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100617 const DisassemblyInformation* disasm_info_;
618 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100619 size_t indent_;
620
621 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
622};
623
624HGraphVisualizer::HGraphVisualizer(std::ostream* output,
625 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100626 const CodeGenerator& codegen)
627 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100628
David Brazdil62e074f2015-04-07 18:09:37 +0100629void HGraphVisualizer::PrintHeader(const char* method_name) const {
630 DCHECK(output_ != nullptr);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000631 HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100632 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000633 printer.PrintProperty("name", method_name);
634 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100635 printer.PrintTime("date");
636 printer.EndTag("compilation");
637}
638
David Brazdilee690a32014-12-01 17:04:16 +0000639void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000640 DCHECK(output_ != nullptr);
641 if (!graph_->GetBlocks().IsEmpty()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000642 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000643 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100644 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100645}
646
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100647void HGraphVisualizer::DumpGraphWithDisassembly() const {
648 DCHECK(output_ != nullptr);
649 if (!graph_->GetBlocks().IsEmpty()) {
650 HGraphVisualizerPrinter printer(
651 graph_, *output_, "disassembly", true, codegen_, codegen_.GetDisassemblyInformation());
652 printer.Run();
653 }
654}
655
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100656} // namespace art