blob: d05c5149127ddfb064339fdf4f1256c32e79b7ea [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 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100155class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100156 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,
David Brazdilffee3d32015-07-06 11:48:53 +0100161 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100162 const CodeGenerator& codegen,
163 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100164 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100165 output_(output),
166 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000167 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100168 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100169 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100170 disasm_info_(disasm_info),
171 disassembler_(disasm_info_ != nullptr
172 ? new HGraphVisualizerDisassembler(
173 codegen_.GetInstructionSet(),
174 codegen_.GetAssembler().CodeBufferBaseAddress())
175 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100176 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100177
178 void StartTag(const char* name) {
179 AddIndent();
180 output_ << "begin_" << name << std::endl;
181 indent_++;
182 }
183
184 void EndTag(const char* name) {
185 indent_--;
186 AddIndent();
187 output_ << "end_" << name << std::endl;
188 }
189
190 void PrintProperty(const char* name, const char* property) {
191 AddIndent();
192 output_ << name << " \"" << property << "\"" << std::endl;
193 }
194
195 void PrintProperty(const char* name, const char* property, int id) {
196 AddIndent();
197 output_ << name << " \"" << property << id << "\"" << std::endl;
198 }
199
200 void PrintEmptyProperty(const char* name) {
201 AddIndent();
202 output_ << name << std::endl;
203 }
204
205 void PrintTime(const char* name) {
206 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800207 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100208 }
209
210 void PrintInt(const char* name, int value) {
211 AddIndent();
212 output_ << name << " " << value << std::endl;
213 }
214
215 void AddIndent() {
216 for (size_t i = 0; i < indent_; ++i) {
217 output_ << " ";
218 }
219 }
220
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100221 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100222 // Note that Primitive::Descriptor would not work for us
223 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100224 switch (type) {
225 case Primitive::kPrimBoolean: return 'z';
226 case Primitive::kPrimByte: return 'b';
227 case Primitive::kPrimChar: return 'c';
228 case Primitive::kPrimShort: return 's';
229 case Primitive::kPrimInt: return 'i';
230 case Primitive::kPrimLong: return 'j';
231 case Primitive::kPrimFloat: return 'f';
232 case Primitive::kPrimDouble: return 'd';
233 case Primitive::kPrimNot: return 'l';
234 case Primitive::kPrimVoid: return 'v';
235 }
236 LOG(FATAL) << "Unreachable";
237 return 'v';
238 }
239
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100240 void PrintPredecessors(HBasicBlock* block) {
241 AddIndent();
242 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000243 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100244 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
245 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100246 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
247 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
248 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100249 output_<< std::endl;
250 }
251
252 void PrintSuccessors(HBasicBlock* block) {
253 AddIndent();
254 output_ << "successors";
David Brazdilffee3d32015-07-06 11:48:53 +0100255 for (size_t i = 0; i < block->NumberOfNormalSuccessors(); ++i) {
Vladimir Marko60584552015-09-03 13:35:12 +0000256 HBasicBlock* successor = block->GetSuccessor(i);
David Brazdilffee3d32015-07-06 11:48:53 +0100257 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000258 }
259 output_<< std::endl;
260 }
261
262 void PrintExceptionHandlers(HBasicBlock* block) {
263 AddIndent();
264 output_ << "xhandlers";
Vladimir Marko60584552015-09-03 13:35:12 +0000265 for (size_t i = block->NumberOfNormalSuccessors(); i < block->GetSuccessors().size(); ++i) {
266 HBasicBlock* handler = block->GetSuccessor(i);
David Brazdilffee3d32015-07-06 11:48:53 +0100267 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100268 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100269 if (block->IsExitBlock() &&
270 (disasm_info_ != nullptr) &&
271 !disasm_info_->GetSlowPathIntervals().empty()) {
272 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
273 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100274 output_<< std::endl;
275 }
276
David Brazdilc74652862015-05-13 17:50:09 +0100277 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100278 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100279 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100280 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100281 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100282 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100283 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100284 HConstant* constant = location.GetConstant();
285 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100286 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100287 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100288 stream << constant->AsLongConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100289 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100290 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100291 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100292 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100293 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000294 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100295 codegen_.DumpFloatingPointRegister(stream, location.low());
296 stream << "|";
297 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000298 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100299 codegen_.DumpCoreRegister(stream, location.low());
300 stream << "|";
301 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400302 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100303 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100304 } else {
305 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100306 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100307 }
308 }
309
David Brazdilc74652862015-05-13 17:50:09 +0100310 std::ostream& StartAttributeStream(const char* name = nullptr) {
311 if (name == nullptr) {
312 output_ << " ";
313 } else {
314 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
315 output_ << " " << name << ":";
316 }
317 return output_;
318 }
319
David Brazdilb7e4a062014-12-29 15:35:02 +0000320 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100321 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
322 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100323 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
324 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100325 std::ostream& str = moves.NewEntryStream();
326 DumpLocation(str, move->GetSource());
327 str << "->";
328 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100329 }
David Brazdilc74652862015-05-13 17:50:09 +0100330 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100331 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100332
David Brazdil36cf0952015-01-08 19:28:33 +0000333 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100334 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000335 }
336
David Brazdil36cf0952015-01-08 19:28:33 +0000337 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100338 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000339 }
340
David Brazdil36cf0952015-01-08 19:28:33 +0000341 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100342 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000343 }
344
David Brazdil36cf0952015-01-08 19:28:33 +0000345 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100346 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000347 }
348
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000349 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100350 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100351 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000352 }
353
Calin Juravle27df7582015-04-17 19:12:31 +0100354 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100355 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100356 }
357
David Brazdilbff75032015-07-08 17:26:51 +0000358 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
359 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
360 }
361
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100362 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Calin Juravle0ba218d2015-05-19 18:46:01 +0100363 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100364 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100365 }
366
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100367 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
368 StartAttributeStream("must_do_null_check") << std::boolalpha
369 << check_cast->MustDoNullCheck() << std::noboolalpha;
370 }
371
372 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
373 StartAttributeStream("must_do_null_check") << std::boolalpha
374 << instance_of->MustDoNullCheck() << std::noboolalpha;
375 }
376
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100377 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100378 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray242febb2015-07-01 16:10:44 +0100379 StartAttributeStream("method_name") << PrettyMethod(
380 invoke->GetDexMethodIndex(), GetGraph()->GetDexFile(), /* with_signature */ false);
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100381 }
382
Calin Juravle175dc732015-08-25 15:42:32 +0100383 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
384 VisitInvoke(invoke);
385 StartAttributeStream("invoke_type") << invoke->GetOriginalInvokeType();
386 }
387
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100388 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
389 VisitInvoke(invoke);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100390 StartAttributeStream("recursive") << std::boolalpha
391 << invoke->IsRecursive()
392 << std::noboolalpha;
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100393 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100394 }
395
David Brazdilfc6a86a2015-06-26 10:33:45 +0000396 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100397 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000398 }
399
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800400 bool IsPass(const char* name) {
401 return strcmp(pass_name_, name) == 0;
402 }
403
Calin Juravlea5ae3c32015-07-28 14:40:50 +0000404 bool IsReferenceTypePropagationPass() {
405 return strstr(pass_name_, ReferenceTypePropagation::kReferenceTypePropagationPassName)
406 != nullptr;
407 }
408
David Brazdilb7e4a062014-12-29 15:35:02 +0000409 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100410 output_ << instruction->DebugName();
411 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100412 StringList inputs;
413 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
414 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100415 }
David Brazdilc74652862015-05-13 17:50:09 +0100416 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100417 }
David Brazdilc74652862015-05-13 17:50:09 +0100418 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800419 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100420 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100421 for (HEnvironment* environment = instruction->GetEnvironment();
422 environment != nullptr;
423 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100424 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100425 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
426 HInstruction* insn = environment->GetInstructionAt(i);
427 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100428 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100429 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100430 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100431 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800432 }
David Brazdilc74652862015-05-13 17:50:09 +0100433 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800434 }
David Brazdilc74652862015-05-13 17:50:09 +0100435 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800436 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800437 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000438 && is_after_pass_
439 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100440 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100441 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100442 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100443 StartAttributeStream("ranges")
444 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100445 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
446 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
447 StartAttributeStream("is_fixed") << interval->IsFixed();
448 StartAttributeStream("is_split") << interval->IsSplit();
449 StartAttributeStream("is_low") << interval->IsLowInterval();
450 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100451 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800452 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100453 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100454 LocationSummary* locations = instruction->GetLocations();
455 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100456 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100457 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100458 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100459 }
David Brazdilc74652862015-05-13 17:50:09 +0100460 std::ostream& attr = StartAttributeStream("locations");
461 attr << inputs << "->";
462 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100463 }
David Brazdila4b8c212015-05-07 09:59:30 +0100464 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
465 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000466 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
467 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100468 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000469 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100470 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000471 }
Calin Juravle2e768302015-07-28 14:41:11 +0000472 } else if (IsReferenceTypePropagationPass()
473 && (instruction->GetType() == Primitive::kPrimNot)) {
474 ReferenceTypeInfo info = instruction->IsLoadClass()
475 ? instruction->AsLoadClass()->GetLoadedClassRTI()
476 : instruction->GetReferenceTypeInfo();
477 ScopedObjectAccess soa(Thread::Current());
478 if (info.IsValid()) {
479 StartAttributeStream("klass") << PrettyDescriptor(info.GetTypeHandle().Get());
480 StartAttributeStream("can_be_null")
481 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
482 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
483 } else {
484 DCHECK(!is_after_pass_) << "Type info should be valid after reference type propagation";
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100485 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100486 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100487 if (disasm_info_ != nullptr) {
488 DCHECK(disassembler_ != nullptr);
489 // If the information is available, disassemble the code generated for
490 // this instruction.
491 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
492 if (it != disasm_info_->GetInstructionIntervals().end()
493 && it->second.start != it->second.end) {
494 output_ << std::endl;
495 disassembler_->Disassemble(output_, it->second.start, it->second.end);
496 }
497 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100498 }
499
500 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100501 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
502 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100503 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000504 size_t num_uses = 0;
505 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
506 !use_it.Done();
507 use_it.Advance()) {
508 ++num_uses;
509 }
510 AddIndent();
511 output_ << bci << " " << num_uses << " "
512 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000513 PrintInstruction(instruction);
David Brazdilc74652862015-05-13 17:50:09 +0100514 output_ << " " << kEndInstructionMarker << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100515 }
516 }
517
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100518 void DumpStartOfDisassemblyBlock(const char* block_name,
519 int predecessor_index,
520 int successor_index) {
521 StartTag("block");
522 PrintProperty("name", block_name);
523 PrintInt("from_bci", -1);
524 PrintInt("to_bci", -1);
525 if (predecessor_index != -1) {
526 PrintProperty("predecessors", "B", predecessor_index);
527 } else {
528 PrintEmptyProperty("predecessors");
529 }
530 if (successor_index != -1) {
531 PrintProperty("successors", "B", successor_index);
532 } else {
533 PrintEmptyProperty("successors");
534 }
535 PrintEmptyProperty("xhandlers");
536 PrintEmptyProperty("flags");
537 StartTag("states");
538 StartTag("locals");
539 PrintInt("size", 0);
540 PrintProperty("method", "None");
541 EndTag("locals");
542 EndTag("states");
543 StartTag("HIR");
544 }
545
546 void DumpEndOfDisassemblyBlock() {
547 EndTag("HIR");
548 EndTag("block");
549 }
550
551 void DumpDisassemblyBlockForFrameEntry() {
552 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
553 -1,
554 GetGraph()->GetEntryBlock()->GetBlockId());
555 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
556 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
557 if (frame_entry.start != frame_entry.end) {
558 output_ << std::endl;
559 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
560 }
561 output_ << kEndInstructionMarker << std::endl;
562 DumpEndOfDisassemblyBlock();
563 }
564
565 void DumpDisassemblyBlockForSlowPaths() {
566 if (disasm_info_->GetSlowPathIntervals().empty()) {
567 return;
568 }
569 // If the graph has an exit block we attach the block for the slow paths
570 // after it. Else we just add the block to the graph without linking it to
571 // any other.
572 DumpStartOfDisassemblyBlock(
573 kDisassemblyBlockSlowPaths,
574 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
575 -1);
576 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
577 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << std::endl;
578 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
579 output_ << kEndInstructionMarker << std::endl;
580 }
581 DumpEndOfDisassemblyBlock();
582 }
583
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100584 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100585 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100586 std::string pass_desc = std::string(pass_name_)
587 + " ("
588 + (is_after_pass_ ? "after" : "before")
589 + (graph_in_bad_state_ ? ", bad_state" : "")
590 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000591 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100592 if (disasm_info_ != nullptr) {
593 DumpDisassemblyBlockForFrameEntry();
594 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100595 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100596 if (disasm_info_ != nullptr) {
597 DumpDisassemblyBlockForSlowPaths();
598 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100599 EndTag("cfg");
600 }
601
David Brazdilb7e4a062014-12-29 15:35:02 +0000602 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100603 StartTag("block");
604 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100605 if (block->GetLifetimeStart() != kNoLifetime) {
606 // Piggy back on these fields to show the lifetime of the block.
607 PrintInt("from_bci", block->GetLifetimeStart());
608 PrintInt("to_bci", block->GetLifetimeEnd());
609 } else {
610 PrintInt("from_bci", -1);
611 PrintInt("to_bci", -1);
612 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100613 PrintPredecessors(block);
614 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000615 PrintExceptionHandlers(block);
616
617 if (block->IsCatchBlock()) {
618 PrintProperty("flags", "catch_block");
619 } else {
620 PrintEmptyProperty("flags");
621 }
622
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100623 if (block->GetDominator() != nullptr) {
624 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
625 }
626
627 StartTag("states");
628 StartTag("locals");
629 PrintInt("size", 0);
630 PrintProperty("method", "None");
631 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
632 AddIndent();
633 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100634 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
635 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100636 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
637 output_ << inputs.Current()->GetId() << " ";
638 }
639 output_ << "]" << std::endl;
640 }
641 EndTag("locals");
642 EndTag("states");
643
644 StartTag("HIR");
645 PrintInstructions(block->GetPhis());
646 PrintInstructions(block->GetInstructions());
647 EndTag("HIR");
648 EndTag("block");
649 }
650
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100651 static constexpr const char* const kEndInstructionMarker = "<|@";
652 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
653 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
654
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100655 private:
656 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100657 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000658 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100659 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100660 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100661 const DisassemblyInformation* disasm_info_;
662 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100663 size_t indent_;
664
665 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
666};
667
668HGraphVisualizer::HGraphVisualizer(std::ostream* output,
669 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100670 const CodeGenerator& codegen)
671 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100672
David Brazdil62e074f2015-04-07 18:09:37 +0100673void HGraphVisualizer::PrintHeader(const char* method_name) const {
674 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100675 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100676 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000677 printer.PrintProperty("name", method_name);
678 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100679 printer.PrintTime("date");
680 printer.EndTag("compilation");
681}
682
David Brazdilffee3d32015-07-06 11:48:53 +0100683void HGraphVisualizer::DumpGraph(const char* pass_name,
684 bool is_after_pass,
685 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000686 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100687 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100688 HGraphVisualizerPrinter printer(graph_,
689 *output_,
690 pass_name,
691 is_after_pass,
692 graph_in_bad_state,
693 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000694 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100695 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100696}
697
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100698void HGraphVisualizer::DumpGraphWithDisassembly() const {
699 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100701 HGraphVisualizerPrinter printer(graph_,
702 *output_,
703 "disassembly",
704 /* is_after_pass */ true,
705 /* graph_in_bad_state */ false,
706 codegen_,
707 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100708 printer.Run();
709 }
710}
711
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100712} // namespace art