blob: 5f1328f54525162cda153f1b0c971ddbb0155c94 [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"
Calin Juravlecdfed3d2015-10-26 14:05:01 +000027#include "inliner.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080028#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010029#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000030#include "optimization.h"
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +010031#include "reference_type_propagation.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080032#include "register_allocator.h"
David Brazdil4833f5a2015-12-16 10:37:39 +000033#include "ssa_builder.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010034#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010035#include "utils/assembler.h"
David Brazdilc74652862015-05-13 17:50:09 +010036
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010037namespace art {
38
David Brazdilc74652862015-05-13 17:50:09 +010039static bool HasWhitespace(const char* str) {
40 DCHECK(str != nullptr);
41 while (str[0] != 0) {
42 if (isspace(str[0])) {
43 return true;
44 }
45 str++;
46 }
47 return false;
48}
49
50class StringList {
51 public:
David Brazdilc7a24852015-05-15 16:44:05 +010052 enum Format {
53 kArrayBrackets,
54 kSetBrackets,
55 };
56
David Brazdilc74652862015-05-13 17:50:09 +010057 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010058 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010059
60 // Construct StringList from a linked list. List element class T
61 // must provide methods `GetNext` and `Dump`.
62 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010063 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010064 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
65 current->Dump(NewEntryStream());
66 }
67 }
68
69 std::ostream& NewEntryStream() {
70 if (is_empty_) {
71 is_empty_ = false;
72 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010073 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010074 }
75 return sstream_;
76 }
77
78 private:
David Brazdilc7a24852015-05-15 16:44:05 +010079 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010080 bool is_empty_;
81 std::ostringstream sstream_;
82
83 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
84};
85
86std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010087 switch (list.format_) {
88 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
89 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
90 default:
91 LOG(FATAL) << "Invalid StringList format";
92 UNREACHABLE();
93 }
David Brazdilc74652862015-05-13 17:50:09 +010094}
95
Alexandre Rameseb7b7392015-06-19 14:47:01 +010096typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
97 DisassemblerOptions* options);
98class HGraphVisualizerDisassembler {
99 public:
100 HGraphVisualizerDisassembler(InstructionSet instruction_set, const uint8_t* base_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100101 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100102 libart_disassembler_handle_ =
103 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
104 if (libart_disassembler_handle_ == nullptr) {
105 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
106 return;
107 }
108 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
109 dlsym(libart_disassembler_handle_, "create_disassembler"));
110 if (create_disassembler == nullptr) {
111 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
112 return;
113 }
114 // Reading the disassembly from 0x0 is easier, so we print relative
115 // addresses. We will only disassemble the code once everything has
116 // been generated, so we can read data in literal pools.
117 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
118 instruction_set,
119 new DisassemblerOptions(/* absolute_addresses */ false,
120 base_address,
121 /* can_read_literals */ true)));
122 }
123
124 ~HGraphVisualizerDisassembler() {
125 // We need to call ~Disassembler() before we close the library.
126 disassembler_.reset();
127 if (libart_disassembler_handle_ != nullptr) {
128 dlclose(libart_disassembler_handle_);
129 }
130 }
131
132 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100133 if (disassembler_ == nullptr) {
134 return;
135 }
136
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100137 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
138 if (instruction_set_ == kThumb2) {
139 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
140 // address is used to distinguish between the two.
141 base += 1;
142 }
143 disassembler_->Dump(output, base + start, base + end);
144 }
145
146 private:
147 InstructionSet instruction_set_;
148 std::unique_ptr<Disassembler> disassembler_;
149
150 void* libart_disassembler_handle_;
151};
152
153
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100154/**
155 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
156 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100157class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100158 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100159 HGraphVisualizerPrinter(HGraph* graph,
160 std::ostream& output,
161 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000162 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100163 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100164 const CodeGenerator& codegen,
165 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100166 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100167 output_(output),
168 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000169 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100170 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100171 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100172 disasm_info_(disasm_info),
173 disassembler_(disasm_info_ != nullptr
174 ? new HGraphVisualizerDisassembler(
175 codegen_.GetInstructionSet(),
176 codegen_.GetAssembler().CodeBufferBaseAddress())
177 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100178 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100179
180 void StartTag(const char* name) {
181 AddIndent();
182 output_ << "begin_" << name << std::endl;
183 indent_++;
184 }
185
186 void EndTag(const char* name) {
187 indent_--;
188 AddIndent();
189 output_ << "end_" << name << std::endl;
190 }
191
192 void PrintProperty(const char* name, const char* property) {
193 AddIndent();
194 output_ << name << " \"" << property << "\"" << std::endl;
195 }
196
197 void PrintProperty(const char* name, const char* property, int id) {
198 AddIndent();
199 output_ << name << " \"" << property << id << "\"" << std::endl;
200 }
201
202 void PrintEmptyProperty(const char* name) {
203 AddIndent();
204 output_ << name << std::endl;
205 }
206
207 void PrintTime(const char* name) {
208 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800209 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100210 }
211
212 void PrintInt(const char* name, int value) {
213 AddIndent();
214 output_ << name << " " << value << std::endl;
215 }
216
217 void AddIndent() {
218 for (size_t i = 0; i < indent_; ++i) {
219 output_ << " ";
220 }
221 }
222
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100223 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100224 // Note that Primitive::Descriptor would not work for us
225 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100226 switch (type) {
227 case Primitive::kPrimBoolean: return 'z';
228 case Primitive::kPrimByte: return 'b';
229 case Primitive::kPrimChar: return 'c';
230 case Primitive::kPrimShort: return 's';
231 case Primitive::kPrimInt: return 'i';
232 case Primitive::kPrimLong: return 'j';
233 case Primitive::kPrimFloat: return 'f';
234 case Primitive::kPrimDouble: return 'd';
235 case Primitive::kPrimNot: return 'l';
236 case Primitive::kPrimVoid: return 'v';
237 }
238 LOG(FATAL) << "Unreachable";
239 return 'v';
240 }
241
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100242 void PrintPredecessors(HBasicBlock* block) {
243 AddIndent();
244 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000245 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100246 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
247 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100248 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
249 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
250 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100251 output_<< std::endl;
252 }
253
254 void PrintSuccessors(HBasicBlock* block) {
255 AddIndent();
256 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000257 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100258 output_ << " \"B" << successor->GetBlockId() << "\" ";
David Brazdilfc6a86a2015-06-26 10:33:45 +0000259 }
260 output_<< std::endl;
261 }
262
263 void PrintExceptionHandlers(HBasicBlock* block) {
264 AddIndent();
265 output_ << "xhandlers";
David Brazdild26a4112015-11-10 11:07:31 +0000266 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
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 Juravle386062d2015-10-07 18:55:43 +0100365 StartAttributeStream("needs_access_check") << std::boolalpha
366 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100367 }
368
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100369 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
370 StartAttributeStream("must_do_null_check") << std::boolalpha
371 << check_cast->MustDoNullCheck() << std::noboolalpha;
372 }
373
374 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
375 StartAttributeStream("must_do_null_check") << std::boolalpha
376 << instance_of->MustDoNullCheck() << std::noboolalpha;
377 }
378
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100379 void VisitArraySet(HArraySet* array_set) OVERRIDE {
380 StartAttributeStream("value_can_be_null") << std::boolalpha
381 << array_set->GetValueCanBeNull() << std::noboolalpha;
382 }
383
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100384 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100385 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray242febb2015-07-01 16:10:44 +0100386 StartAttributeStream("method_name") << PrettyMethod(
387 invoke->GetDexMethodIndex(), GetGraph()->GetDexFile(), /* with_signature */ false);
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100388 }
389
Calin Juravle175dc732015-08-25 15:42:32 +0100390 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
391 VisitInvoke(invoke);
392 StartAttributeStream("invoke_type") << invoke->GetOriginalInvokeType();
393 }
394
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100395 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
396 VisitInvoke(invoke);
Vladimir Markof64242a2015-12-01 14:58:23 +0000397 StartAttributeStream("method_load_kind") << invoke->GetMethodLoadKind();
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100398 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000399 if (invoke->IsStatic()) {
400 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
401 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100402 }
403
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000404 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
405 VisitInvoke(invoke);
406 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
407 }
408
Calin Juravlee460d1d2015-09-29 04:52:17 +0100409 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
410 StartAttributeStream("field_type") << field_access->GetFieldType();
411 }
412
413 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
414 StartAttributeStream("field_type") << field_access->GetFieldType();
415 }
416
417 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
418 StartAttributeStream("field_type") << field_access->GetFieldType();
419 }
420
421 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
422 StartAttributeStream("field_type") << field_access->GetFieldType();
423 }
424
David Brazdilfc6a86a2015-06-26 10:33:45 +0000425 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100426 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000427 }
428
Alexandre Rames418318f2015-11-20 15:55:47 +0000429#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames8626b742015-11-25 16:28:08 +0000430 void VisitArm64DataProcWithShifterOp(HArm64DataProcWithShifterOp* instruction) OVERRIDE {
431 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
432 if (HArm64DataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
433 StartAttributeStream("shift") << instruction->GetShiftAmount();
434 }
435 }
436
Alexandre Rames418318f2015-11-20 15:55:47 +0000437 void VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instruction) OVERRIDE {
438 StartAttributeStream("kind") << instruction->GetOpKind();
439 }
440#endif
441
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800442 bool IsPass(const char* name) {
443 return strcmp(pass_name_, name) == 0;
444 }
445
David Brazdilb7e4a062014-12-29 15:35:02 +0000446 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100447 output_ << instruction->DebugName();
448 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100449 StringList inputs;
450 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
451 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100452 }
David Brazdilc74652862015-05-13 17:50:09 +0100453 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100454 }
David Brazdilc74652862015-05-13 17:50:09 +0100455 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800456 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100457 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100458 for (HEnvironment* environment = instruction->GetEnvironment();
459 environment != nullptr;
460 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100461 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100462 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
463 HInstruction* insn = environment->GetInstructionAt(i);
464 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100465 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100466 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100467 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100468 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800469 }
David Brazdilc74652862015-05-13 17:50:09 +0100470 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800471 }
David Brazdilc74652862015-05-13 17:50:09 +0100472 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800473 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800474 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000475 && is_after_pass_
476 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100477 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100478 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100479 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100480 StartAttributeStream("ranges")
481 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100482 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
483 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
484 StartAttributeStream("is_fixed") << interval->IsFixed();
485 StartAttributeStream("is_split") << interval->IsSplit();
486 StartAttributeStream("is_low") << interval->IsLowInterval();
487 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100488 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800489 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100490 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100491 LocationSummary* locations = instruction->GetLocations();
492 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100493 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100494 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100495 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100496 }
David Brazdilc74652862015-05-13 17:50:09 +0100497 std::ostream& attr = StartAttributeStream("locations");
498 attr << inputs << "->";
499 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100500 }
David Brazdila4b8c212015-05-07 09:59:30 +0100501 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
502 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000503 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
504 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100505 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000506 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100507 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000508 }
David Brazdil4833f5a2015-12-16 10:37:39 +0000509 } else if ((IsPass(SsaBuilder::kSsaBuilderPassName)
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000510 || IsPass(HInliner::kInlinerPassName))
Calin Juravle2e768302015-07-28 14:41:11 +0000511 && (instruction->GetType() == Primitive::kPrimNot)) {
512 ReferenceTypeInfo info = instruction->IsLoadClass()
513 ? instruction->AsLoadClass()->GetLoadedClassRTI()
514 : instruction->GetReferenceTypeInfo();
515 ScopedObjectAccess soa(Thread::Current());
516 if (info.IsValid()) {
517 StartAttributeStream("klass") << PrettyDescriptor(info.GetTypeHandle().Get());
518 StartAttributeStream("can_be_null")
519 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
520 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Calin Juravle98893e12015-10-02 21:05:03 +0100521 } else if (instruction->IsLoadClass()) {
522 StartAttributeStream("klass") << "unresolved";
David Brazdil4833f5a2015-12-16 10:37:39 +0000523 } else {
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500524 // The NullConstant may be added to the graph during other passes that happen between
525 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
526 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
527 // So we should check NullConstants for validity only after reference type propagation.
David Brazdil4833f5a2015-12-16 10:37:39 +0000528 DCHECK(graph_in_bad_state_ ||
529 (!is_after_pass_ && IsPass(SsaBuilder::kSsaBuilderPassName)))
530 << instruction->DebugName() << instruction->GetId() << " has invalid rti "
531 << (is_after_pass_ ? "after" : "before") << " pass " << pass_name_;
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100532 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100533 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100534 if (disasm_info_ != nullptr) {
535 DCHECK(disassembler_ != nullptr);
536 // If the information is available, disassemble the code generated for
537 // this instruction.
538 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
539 if (it != disasm_info_->GetInstructionIntervals().end()
540 && it->second.start != it->second.end) {
541 output_ << std::endl;
542 disassembler_->Disassemble(output_, it->second.start, it->second.end);
543 }
544 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100545 }
546
547 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100548 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
549 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100550 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000551 size_t num_uses = 0;
552 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
553 !use_it.Done();
554 use_it.Advance()) {
555 ++num_uses;
556 }
557 AddIndent();
558 output_ << bci << " " << num_uses << " "
559 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000560 PrintInstruction(instruction);
David Brazdilc74652862015-05-13 17:50:09 +0100561 output_ << " " << kEndInstructionMarker << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100562 }
563 }
564
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100565 void DumpStartOfDisassemblyBlock(const char* block_name,
566 int predecessor_index,
567 int successor_index) {
568 StartTag("block");
569 PrintProperty("name", block_name);
570 PrintInt("from_bci", -1);
571 PrintInt("to_bci", -1);
572 if (predecessor_index != -1) {
573 PrintProperty("predecessors", "B", predecessor_index);
574 } else {
575 PrintEmptyProperty("predecessors");
576 }
577 if (successor_index != -1) {
578 PrintProperty("successors", "B", successor_index);
579 } else {
580 PrintEmptyProperty("successors");
581 }
582 PrintEmptyProperty("xhandlers");
583 PrintEmptyProperty("flags");
584 StartTag("states");
585 StartTag("locals");
586 PrintInt("size", 0);
587 PrintProperty("method", "None");
588 EndTag("locals");
589 EndTag("states");
590 StartTag("HIR");
591 }
592
593 void DumpEndOfDisassemblyBlock() {
594 EndTag("HIR");
595 EndTag("block");
596 }
597
598 void DumpDisassemblyBlockForFrameEntry() {
599 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
600 -1,
601 GetGraph()->GetEntryBlock()->GetBlockId());
602 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
603 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
604 if (frame_entry.start != frame_entry.end) {
605 output_ << std::endl;
606 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
607 }
608 output_ << kEndInstructionMarker << std::endl;
609 DumpEndOfDisassemblyBlock();
610 }
611
612 void DumpDisassemblyBlockForSlowPaths() {
613 if (disasm_info_->GetSlowPathIntervals().empty()) {
614 return;
615 }
616 // If the graph has an exit block we attach the block for the slow paths
617 // after it. Else we just add the block to the graph without linking it to
618 // any other.
619 DumpStartOfDisassemblyBlock(
620 kDisassemblyBlockSlowPaths,
621 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
622 -1);
623 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
624 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << std::endl;
625 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
626 output_ << kEndInstructionMarker << std::endl;
627 }
628 DumpEndOfDisassemblyBlock();
629 }
630
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100631 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100632 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100633 std::string pass_desc = std::string(pass_name_)
634 + " ("
635 + (is_after_pass_ ? "after" : "before")
636 + (graph_in_bad_state_ ? ", bad_state" : "")
637 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000638 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100639 if (disasm_info_ != nullptr) {
640 DumpDisassemblyBlockForFrameEntry();
641 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100642 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100643 if (disasm_info_ != nullptr) {
644 DumpDisassemblyBlockForSlowPaths();
645 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100646 EndTag("cfg");
647 }
648
David Brazdilb7e4a062014-12-29 15:35:02 +0000649 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100650 StartTag("block");
651 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100652 if (block->GetLifetimeStart() != kNoLifetime) {
653 // Piggy back on these fields to show the lifetime of the block.
654 PrintInt("from_bci", block->GetLifetimeStart());
655 PrintInt("to_bci", block->GetLifetimeEnd());
656 } else {
657 PrintInt("from_bci", -1);
658 PrintInt("to_bci", -1);
659 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100660 PrintPredecessors(block);
661 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000662 PrintExceptionHandlers(block);
663
664 if (block->IsCatchBlock()) {
665 PrintProperty("flags", "catch_block");
666 } else {
667 PrintEmptyProperty("flags");
668 }
669
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100670 if (block->GetDominator() != nullptr) {
671 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
672 }
673
674 StartTag("states");
675 StartTag("locals");
676 PrintInt("size", 0);
677 PrintProperty("method", "None");
678 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
679 AddIndent();
680 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100681 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
682 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100683 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
684 output_ << inputs.Current()->GetId() << " ";
685 }
686 output_ << "]" << std::endl;
687 }
688 EndTag("locals");
689 EndTag("states");
690
691 StartTag("HIR");
692 PrintInstructions(block->GetPhis());
693 PrintInstructions(block->GetInstructions());
694 EndTag("HIR");
695 EndTag("block");
696 }
697
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100698 static constexpr const char* const kEndInstructionMarker = "<|@";
699 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
700 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
701
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100702 private:
703 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100704 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000705 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100706 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100707 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100708 const DisassemblyInformation* disasm_info_;
709 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100710 size_t indent_;
711
712 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
713};
714
715HGraphVisualizer::HGraphVisualizer(std::ostream* output,
716 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100717 const CodeGenerator& codegen)
718 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100719
David Brazdil62e074f2015-04-07 18:09:37 +0100720void HGraphVisualizer::PrintHeader(const char* method_name) const {
721 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100722 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100723 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000724 printer.PrintProperty("name", method_name);
725 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100726 printer.PrintTime("date");
727 printer.EndTag("compilation");
728}
729
David Brazdilffee3d32015-07-06 11:48:53 +0100730void HGraphVisualizer::DumpGraph(const char* pass_name,
731 bool is_after_pass,
732 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000733 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100734 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100735 HGraphVisualizerPrinter printer(graph_,
736 *output_,
737 pass_name,
738 is_after_pass,
739 graph_in_bad_state,
740 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000741 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100742 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100743}
744
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100745void HGraphVisualizer::DumpGraphWithDisassembly() const {
746 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100747 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100748 HGraphVisualizerPrinter printer(graph_,
749 *output_,
750 "disassembly",
751 /* is_after_pass */ true,
752 /* graph_in_bad_state */ false,
753 codegen_,
754 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100755 printer.Run();
756 }
757}
758
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100759} // namespace art