blob: 48bcd10b10b9db037bb1e5f2cd72a01c48dfdf14 [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"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010033#include "ssa_liveness_analysis.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010034#include "utils/assembler.h"
David Brazdilc74652862015-05-13 17:50:09 +010035
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010036namespace art {
37
David Brazdilc74652862015-05-13 17:50:09 +010038static bool HasWhitespace(const char* str) {
39 DCHECK(str != nullptr);
40 while (str[0] != 0) {
41 if (isspace(str[0])) {
42 return true;
43 }
44 str++;
45 }
46 return false;
47}
48
49class StringList {
50 public:
David Brazdilc7a24852015-05-15 16:44:05 +010051 enum Format {
52 kArrayBrackets,
53 kSetBrackets,
54 };
55
David Brazdilc74652862015-05-13 17:50:09 +010056 // Create an empty list
David Brazdilf1a9ff72015-05-18 16:04:53 +010057 explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {}
David Brazdilc74652862015-05-13 17:50:09 +010058
59 // Construct StringList from a linked list. List element class T
60 // must provide methods `GetNext` and `Dump`.
61 template<class T>
David Brazdilc7a24852015-05-15 16:44:05 +010062 explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) {
David Brazdilc74652862015-05-13 17:50:09 +010063 for (T* current = first_entry; current != nullptr; current = current->GetNext()) {
64 current->Dump(NewEntryStream());
65 }
66 }
67
68 std::ostream& NewEntryStream() {
69 if (is_empty_) {
70 is_empty_ = false;
71 } else {
David Brazdilc57397b2015-05-15 16:01:59 +010072 sstream_ << ",";
David Brazdilc74652862015-05-13 17:50:09 +010073 }
74 return sstream_;
75 }
76
77 private:
David Brazdilc7a24852015-05-15 16:44:05 +010078 Format format_;
David Brazdilc74652862015-05-13 17:50:09 +010079 bool is_empty_;
80 std::ostringstream sstream_;
81
82 friend std::ostream& operator<<(std::ostream& os, const StringList& list);
83};
84
85std::ostream& operator<<(std::ostream& os, const StringList& list) {
David Brazdilc7a24852015-05-15 16:44:05 +010086 switch (list.format_) {
87 case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]";
88 case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}";
89 default:
90 LOG(FATAL) << "Invalid StringList format";
91 UNREACHABLE();
92 }
David Brazdilc74652862015-05-13 17:50:09 +010093}
94
Alexandre Rameseb7b7392015-06-19 14:47:01 +010095typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set,
96 DisassemblerOptions* options);
97class HGraphVisualizerDisassembler {
98 public:
99 HGraphVisualizerDisassembler(InstructionSet instruction_set, const uint8_t* base_address)
David Brazdil3a690be2015-06-23 10:22:38 +0100100 : instruction_set_(instruction_set), disassembler_(nullptr) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100101 libart_disassembler_handle_ =
102 dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW);
103 if (libart_disassembler_handle_ == nullptr) {
104 LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror();
105 return;
106 }
107 create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>(
108 dlsym(libart_disassembler_handle_, "create_disassembler"));
109 if (create_disassembler == nullptr) {
110 LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror();
111 return;
112 }
113 // Reading the disassembly from 0x0 is easier, so we print relative
114 // addresses. We will only disassemble the code once everything has
115 // been generated, so we can read data in literal pools.
116 disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)(
117 instruction_set,
118 new DisassemblerOptions(/* absolute_addresses */ false,
119 base_address,
120 /* can_read_literals */ true)));
121 }
122
123 ~HGraphVisualizerDisassembler() {
124 // We need to call ~Disassembler() before we close the library.
125 disassembler_.reset();
126 if (libart_disassembler_handle_ != nullptr) {
127 dlclose(libart_disassembler_handle_);
128 }
129 }
130
131 void Disassemble(std::ostream& output, size_t start, size_t end) const {
David Brazdil3a690be2015-06-23 10:22:38 +0100132 if (disassembler_ == nullptr) {
133 return;
134 }
135
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100136 const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_;
137 if (instruction_set_ == kThumb2) {
138 // ARM and Thumb-2 use the same disassembler. The bottom bit of the
139 // address is used to distinguish between the two.
140 base += 1;
141 }
142 disassembler_->Dump(output, base + start, base + end);
143 }
144
145 private:
146 InstructionSet instruction_set_;
147 std::unique_ptr<Disassembler> disassembler_;
148
149 void* libart_disassembler_handle_;
150};
151
152
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100153/**
154 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
155 */
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100156class HGraphVisualizerPrinter : public HGraphDelegateVisitor {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100157 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100158 HGraphVisualizerPrinter(HGraph* graph,
159 std::ostream& output,
160 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000161 bool is_after_pass,
David Brazdilffee3d32015-07-06 11:48:53 +0100162 bool graph_in_bad_state,
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100163 const CodeGenerator& codegen,
164 const DisassemblyInformation* disasm_info = nullptr)
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100165 : HGraphDelegateVisitor(graph),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100166 output_(output),
167 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000168 is_after_pass_(is_after_pass),
David Brazdilffee3d32015-07-06 11:48:53 +0100169 graph_in_bad_state_(graph_in_bad_state),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100170 codegen_(codegen),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100171 disasm_info_(disasm_info),
172 disassembler_(disasm_info_ != nullptr
173 ? new HGraphVisualizerDisassembler(
174 codegen_.GetInstructionSet(),
175 codegen_.GetAssembler().CodeBufferBaseAddress())
176 : nullptr),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100177 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100178
179 void StartTag(const char* name) {
180 AddIndent();
181 output_ << "begin_" << name << std::endl;
182 indent_++;
183 }
184
185 void EndTag(const char* name) {
186 indent_--;
187 AddIndent();
188 output_ << "end_" << name << std::endl;
189 }
190
191 void PrintProperty(const char* name, const char* property) {
192 AddIndent();
193 output_ << name << " \"" << property << "\"" << std::endl;
194 }
195
196 void PrintProperty(const char* name, const char* property, int id) {
197 AddIndent();
198 output_ << name << " \"" << property << id << "\"" << std::endl;
199 }
200
201 void PrintEmptyProperty(const char* name) {
202 AddIndent();
203 output_ << name << std::endl;
204 }
205
206 void PrintTime(const char* name) {
207 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800208 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100209 }
210
211 void PrintInt(const char* name, int value) {
212 AddIndent();
213 output_ << name << " " << value << std::endl;
214 }
215
216 void AddIndent() {
217 for (size_t i = 0; i < indent_; ++i) {
218 output_ << " ";
219 }
220 }
221
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100222 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100223 // Note that Primitive::Descriptor would not work for us
224 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100225 switch (type) {
226 case Primitive::kPrimBoolean: return 'z';
227 case Primitive::kPrimByte: return 'b';
228 case Primitive::kPrimChar: return 'c';
229 case Primitive::kPrimShort: return 's';
230 case Primitive::kPrimInt: return 'i';
231 case Primitive::kPrimLong: return 'j';
232 case Primitive::kPrimFloat: return 'f';
233 case Primitive::kPrimDouble: return 'd';
234 case Primitive::kPrimNot: return 'l';
235 case Primitive::kPrimVoid: return 'v';
236 }
237 LOG(FATAL) << "Unreachable";
238 return 'v';
239 }
240
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100241 void PrintPredecessors(HBasicBlock* block) {
242 AddIndent();
243 output_ << "predecessors";
Vladimir Marko60584552015-09-03 13:35:12 +0000244 for (HBasicBlock* predecessor : block->GetPredecessors()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100245 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
246 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100247 if (block->IsEntryBlock() && (disasm_info_ != nullptr)) {
248 output_ << " \"" << kDisassemblyBlockFrameEntry << "\" ";
249 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100250 output_<< std::endl;
251 }
252
253 void PrintSuccessors(HBasicBlock* block) {
254 AddIndent();
255 output_ << "successors";
David Brazdild26a4112015-11-10 11:07:31 +0000256 for (HBasicBlock* successor : block->GetNormalSuccessors()) {
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";
David Brazdild26a4112015-11-10 11:07:31 +0000265 for (HBasicBlock* handler : block->GetExceptionalSuccessors()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100266 output_ << " \"B" << handler->GetBlockId() << "\" ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100267 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100268 if (block->IsExitBlock() &&
269 (disasm_info_ != nullptr) &&
270 !disasm_info_->GetSlowPathIntervals().empty()) {
271 output_ << " \"" << kDisassemblyBlockSlowPaths << "\" ";
272 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100273 output_<< std::endl;
274 }
275
David Brazdilc74652862015-05-13 17:50:09 +0100276 void DumpLocation(std::ostream& stream, const Location& location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100277 if (location.IsRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100278 codegen_.DumpCoreRegister(stream, location.reg());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100279 } else if (location.IsFpuRegister()) {
David Brazdilc74652862015-05-13 17:50:09 +0100280 codegen_.DumpFloatingPointRegister(stream, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100281 } else if (location.IsConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100282 stream << "#";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100283 HConstant* constant = location.GetConstant();
284 if (constant->IsIntConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100285 stream << constant->AsIntConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100286 } else if (constant->IsLongConstant()) {
David Brazdilc74652862015-05-13 17:50:09 +0100287 stream << constant->AsLongConstant()->GetValue();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100288 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100289 } else if (location.IsInvalid()) {
David Brazdilc74652862015-05-13 17:50:09 +0100290 stream << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100291 } else if (location.IsStackSlot()) {
David Brazdilc74652862015-05-13 17:50:09 +0100292 stream << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000293 } else if (location.IsFpuRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100294 codegen_.DumpFloatingPointRegister(stream, location.low());
295 stream << "|";
296 codegen_.DumpFloatingPointRegister(stream, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000297 } else if (location.IsRegisterPair()) {
David Brazdilc74652862015-05-13 17:50:09 +0100298 codegen_.DumpCoreRegister(stream, location.low());
299 stream << "|";
300 codegen_.DumpCoreRegister(stream, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400301 } else if (location.IsUnallocated()) {
David Brazdilc74652862015-05-13 17:50:09 +0100302 stream << "unallocated";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100303 } else {
304 DCHECK(location.IsDoubleStackSlot());
David Brazdilc74652862015-05-13 17:50:09 +0100305 stream << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100306 }
307 }
308
David Brazdilc74652862015-05-13 17:50:09 +0100309 std::ostream& StartAttributeStream(const char* name = nullptr) {
310 if (name == nullptr) {
311 output_ << " ";
312 } else {
313 DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes";
314 output_ << " " << name << ":";
315 }
316 return output_;
317 }
318
David Brazdilb7e4a062014-12-29 15:35:02 +0000319 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100320 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
321 StringList moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100322 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
323 MoveOperands* move = instruction->MoveOperandsAt(i);
David Brazdilc74652862015-05-13 17:50:09 +0100324 std::ostream& str = moves.NewEntryStream();
325 DumpLocation(str, move->GetSource());
326 str << "->";
327 DumpLocation(str, move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100328 }
David Brazdilc74652862015-05-13 17:50:09 +0100329 StartAttributeStream("moves") << moves;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100330 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100331
David Brazdil36cf0952015-01-08 19:28:33 +0000332 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100333 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000334 }
335
David Brazdil36cf0952015-01-08 19:28:33 +0000336 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100337 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000338 }
339
David Brazdil36cf0952015-01-08 19:28:33 +0000340 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100341 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000342 }
343
David Brazdil36cf0952015-01-08 19:28:33 +0000344 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100345 StartAttributeStream() << instruction->GetValue();
David Brazdilb7e4a062014-12-29 15:35:02 +0000346 }
347
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000348 void VisitPhi(HPhi* phi) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100349 StartAttributeStream("reg") << phi->GetRegNumber();
David Brazdilffee3d32015-07-06 11:48:53 +0100350 StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000351 }
352
Calin Juravle27df7582015-04-17 19:12:31 +0100353 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
David Brazdilc74652862015-05-13 17:50:09 +0100354 StartAttributeStream("kind") << barrier->GetBarrierKind();
Calin Juravle27df7582015-04-17 19:12:31 +0100355 }
356
David Brazdilbff75032015-07-08 17:26:51 +0000357 void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE {
358 StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit");
359 }
360
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100361 void VisitLoadClass(HLoadClass* load_class) OVERRIDE {
Calin Juravle0ba218d2015-05-19 18:46:01 +0100362 StartAttributeStream("gen_clinit_check") << std::boolalpha
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100363 << load_class->MustGenerateClinitCheck() << std::noboolalpha;
Calin Juravle386062d2015-10-07 18:55:43 +0100364 StartAttributeStream("needs_access_check") << std::boolalpha
365 << load_class->NeedsAccessCheck() << std::noboolalpha;
Calin Juravle0ba218d2015-05-19 18:46:01 +0100366 }
367
Guillaume "Vermeille" Sanchez9099ef72015-05-20 15:19:21 +0100368 void VisitCheckCast(HCheckCast* check_cast) OVERRIDE {
369 StartAttributeStream("must_do_null_check") << std::boolalpha
370 << check_cast->MustDoNullCheck() << std::noboolalpha;
371 }
372
373 void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE {
374 StartAttributeStream("must_do_null_check") << std::boolalpha
375 << instance_of->MustDoNullCheck() << std::noboolalpha;
376 }
377
Nicolas Geoffray6e7455e2015-09-28 16:25:37 +0100378 void VisitArraySet(HArraySet* array_set) OVERRIDE {
379 StartAttributeStream("value_can_be_null") << std::boolalpha
380 << array_set->GetValueCanBeNull() << std::noboolalpha;
381 }
382
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100383 void VisitInvoke(HInvoke* invoke) OVERRIDE {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100384 StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex();
Nicolas Geoffray242febb2015-07-01 16:10:44 +0100385 StartAttributeStream("method_name") << PrettyMethod(
386 invoke->GetDexMethodIndex(), GetGraph()->GetDexFile(), /* with_signature */ false);
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100387 }
388
Calin Juravle175dc732015-08-25 15:42:32 +0100389 void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE {
390 VisitInvoke(invoke);
391 StartAttributeStream("invoke_type") << invoke->GetOriginalInvokeType();
392 }
393
Nicolas Geoffray842acd42015-07-01 13:00:15 +0100394 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
395 VisitInvoke(invoke);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100396 StartAttributeStream("recursive") << std::boolalpha
397 << invoke->IsRecursive()
398 << std::noboolalpha;
Scott Wakelingd60a1af2015-07-22 14:32:44 +0100399 StartAttributeStream("intrinsic") << invoke->GetIntrinsic();
Vladimir Markofbb184a2015-11-13 14:47:00 +0000400 if (invoke->IsStatic()) {
401 StartAttributeStream("clinit_check") << invoke->GetClinitCheckRequirement();
402 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100403 }
404
Calin Juravlee460d1d2015-09-29 04:52:17 +0100405 void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE {
406 StartAttributeStream("field_type") << field_access->GetFieldType();
407 }
408
409 void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE {
410 StartAttributeStream("field_type") << field_access->GetFieldType();
411 }
412
413 void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE {
414 StartAttributeStream("field_type") << field_access->GetFieldType();
415 }
416
417 void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE {
418 StartAttributeStream("field_type") << field_access->GetFieldType();
419 }
420
David Brazdilfc6a86a2015-06-26 10:33:45 +0000421 void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE {
David Brazdil56e1acc2015-06-30 15:41:36 +0100422 StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit");
David Brazdilfc6a86a2015-06-26 10:33:45 +0000423 }
424
Alexandre Rames418318f2015-11-20 15:55:47 +0000425#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames8626b742015-11-25 16:28:08 +0000426 void VisitArm64DataProcWithShifterOp(HArm64DataProcWithShifterOp* instruction) OVERRIDE {
427 StartAttributeStream("kind") << instruction->GetInstrKind() << "+" << instruction->GetOpKind();
428 if (HArm64DataProcWithShifterOp::IsShiftOp(instruction->GetOpKind())) {
429 StartAttributeStream("shift") << instruction->GetShiftAmount();
430 }
431 }
432
Alexandre Rames418318f2015-11-20 15:55:47 +0000433 void VisitArm64MultiplyAccumulate(HArm64MultiplyAccumulate* instruction) OVERRIDE {
434 StartAttributeStream("kind") << instruction->GetOpKind();
435 }
436#endif
437
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800438 bool IsPass(const char* name) {
439 return strcmp(pass_name_, name) == 0;
440 }
441
David Brazdilb7e4a062014-12-29 15:35:02 +0000442 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100443 output_ << instruction->DebugName();
444 if (instruction->InputCount() > 0) {
David Brazdilc74652862015-05-13 17:50:09 +0100445 StringList inputs;
446 for (HInputIterator it(instruction); !it.Done(); it.Advance()) {
447 inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100448 }
David Brazdilc74652862015-05-13 17:50:09 +0100449 StartAttributeStream() << inputs;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100450 }
David Brazdilc74652862015-05-13 17:50:09 +0100451 instruction->Accept(this);
Zheng Xubb7a28a2015-01-09 14:40:47 +0800452 if (instruction->HasEnvironment()) {
David Brazdilc74652862015-05-13 17:50:09 +0100453 StringList envs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100454 for (HEnvironment* environment = instruction->GetEnvironment();
455 environment != nullptr;
456 environment = environment->GetParent()) {
David Brazdilc74652862015-05-13 17:50:09 +0100457 StringList vregs;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100458 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
459 HInstruction* insn = environment->GetInstructionAt(i);
460 if (insn != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100461 vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100462 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100463 vregs.NewEntryStream() << "_";
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100464 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800465 }
David Brazdilc74652862015-05-13 17:50:09 +0100466 envs.NewEntryStream() << vregs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800467 }
David Brazdilc74652862015-05-13 17:50:09 +0100468 StartAttributeStream("env") << envs;
Zheng Xubb7a28a2015-01-09 14:40:47 +0800469 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800470 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000471 && is_after_pass_
472 && instruction->GetLifetimePosition() != kNoLifetime) {
David Brazdilc74652862015-05-13 17:50:09 +0100473 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100474 if (instruction->HasLiveInterval()) {
David Brazdilc74652862015-05-13 17:50:09 +0100475 LiveInterval* interval = instruction->GetLiveInterval();
David Brazdilc7a24852015-05-15 16:44:05 +0100476 StartAttributeStream("ranges")
477 << StringList(interval->GetFirstRange(), StringList::kSetBrackets);
David Brazdilc74652862015-05-13 17:50:09 +0100478 StartAttributeStream("uses") << StringList(interval->GetFirstUse());
479 StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse());
480 StartAttributeStream("is_fixed") << interval->IsFixed();
481 StartAttributeStream("is_split") << interval->IsSplit();
482 StartAttributeStream("is_low") << interval->IsLowInterval();
483 StartAttributeStream("is_high") << interval->IsHighInterval();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100484 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800485 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
David Brazdilc74652862015-05-13 17:50:09 +0100486 StartAttributeStream("liveness") << instruction->GetLifetimePosition();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100487 LocationSummary* locations = instruction->GetLocations();
488 if (locations != nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100489 StringList inputs;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100490 for (size_t i = 0; i < instruction->InputCount(); ++i) {
David Brazdilc74652862015-05-13 17:50:09 +0100491 DumpLocation(inputs.NewEntryStream(), locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100492 }
David Brazdilc74652862015-05-13 17:50:09 +0100493 std::ostream& attr = StartAttributeStream("locations");
494 attr << inputs << "->";
495 DumpLocation(attr, locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100496 }
David Brazdila4b8c212015-05-07 09:59:30 +0100497 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)
498 || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000499 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
500 if (info == nullptr) {
David Brazdilc74652862015-05-13 17:50:09 +0100501 StartAttributeStream("loop") << "none";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000502 } else {
David Brazdilc74652862015-05-13 17:50:09 +0100503 StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId();
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000504 }
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000505 } else if ((IsPass(ReferenceTypePropagation::kReferenceTypePropagationPassName)
506 || IsPass(HInliner::kInlinerPassName))
Calin Juravle2e768302015-07-28 14:41:11 +0000507 && (instruction->GetType() == Primitive::kPrimNot)) {
508 ReferenceTypeInfo info = instruction->IsLoadClass()
509 ? instruction->AsLoadClass()->GetLoadedClassRTI()
510 : instruction->GetReferenceTypeInfo();
511 ScopedObjectAccess soa(Thread::Current());
512 if (info.IsValid()) {
513 StartAttributeStream("klass") << PrettyDescriptor(info.GetTypeHandle().Get());
514 StartAttributeStream("can_be_null")
515 << std::boolalpha << instruction->CanBeNull() << std::noboolalpha;
516 StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha;
Calin Juravle98893e12015-10-02 21:05:03 +0100517 } else if (instruction->IsLoadClass()) {
518 StartAttributeStream("klass") << "unresolved";
Mark Mendellb2d38fd2015-11-16 12:21:53 -0500519 } else if (instruction->IsNullConstant()) {
520 // The NullConstant may be added to the graph during other passes that happen between
521 // ReferenceTypePropagation and Inliner (e.g. InstructionSimplifier). If the inliner
522 // doesn't run or doesn't inline anything, the NullConstant remains untyped.
523 // So we should check NullConstants for validity only after reference type propagation.
524 //
525 // Note: The infrastructure to properly type NullConstants everywhere is to complex to add
526 // for the benefits.
527 StartAttributeStream("klass") << "not_set";
528 DCHECK(!is_after_pass_
529 || !IsPass(ReferenceTypePropagation::kReferenceTypePropagationPassName))
530 << " Expected a valid rti after reference type propagation";
Calin Juravle2e768302015-07-28 14:41:11 +0000531 } else {
Calin Juravle98893e12015-10-02 21:05:03 +0100532 DCHECK(!is_after_pass_)
533 << "Expected a valid rti after reference type propagation";
Nicolas Geoffray7cb499b2015-06-17 11:35:11 +0100534 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100535 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100536 if (disasm_info_ != nullptr) {
537 DCHECK(disassembler_ != nullptr);
538 // If the information is available, disassemble the code generated for
539 // this instruction.
540 auto it = disasm_info_->GetInstructionIntervals().find(instruction);
541 if (it != disasm_info_->GetInstructionIntervals().end()
542 && it->second.start != it->second.end) {
543 output_ << std::endl;
544 disassembler_->Disassemble(output_, it->second.start, it->second.end);
545 }
546 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100547 }
548
549 void PrintInstructions(const HInstructionList& list) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100550 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
551 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100552 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000553 size_t num_uses = 0;
554 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
555 !use_it.Done();
556 use_it.Advance()) {
557 ++num_uses;
558 }
559 AddIndent();
560 output_ << bci << " " << num_uses << " "
561 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000562 PrintInstruction(instruction);
David Brazdilc74652862015-05-13 17:50:09 +0100563 output_ << " " << kEndInstructionMarker << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100564 }
565 }
566
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100567 void DumpStartOfDisassemblyBlock(const char* block_name,
568 int predecessor_index,
569 int successor_index) {
570 StartTag("block");
571 PrintProperty("name", block_name);
572 PrintInt("from_bci", -1);
573 PrintInt("to_bci", -1);
574 if (predecessor_index != -1) {
575 PrintProperty("predecessors", "B", predecessor_index);
576 } else {
577 PrintEmptyProperty("predecessors");
578 }
579 if (successor_index != -1) {
580 PrintProperty("successors", "B", successor_index);
581 } else {
582 PrintEmptyProperty("successors");
583 }
584 PrintEmptyProperty("xhandlers");
585 PrintEmptyProperty("flags");
586 StartTag("states");
587 StartTag("locals");
588 PrintInt("size", 0);
589 PrintProperty("method", "None");
590 EndTag("locals");
591 EndTag("states");
592 StartTag("HIR");
593 }
594
595 void DumpEndOfDisassemblyBlock() {
596 EndTag("HIR");
597 EndTag("block");
598 }
599
600 void DumpDisassemblyBlockForFrameEntry() {
601 DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry,
602 -1,
603 GetGraph()->GetEntryBlock()->GetBlockId());
604 output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " ";
605 GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval();
606 if (frame_entry.start != frame_entry.end) {
607 output_ << std::endl;
608 disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end);
609 }
610 output_ << kEndInstructionMarker << std::endl;
611 DumpEndOfDisassemblyBlock();
612 }
613
614 void DumpDisassemblyBlockForSlowPaths() {
615 if (disasm_info_->GetSlowPathIntervals().empty()) {
616 return;
617 }
618 // If the graph has an exit block we attach the block for the slow paths
619 // after it. Else we just add the block to the graph without linking it to
620 // any other.
621 DumpStartOfDisassemblyBlock(
622 kDisassemblyBlockSlowPaths,
623 GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1,
624 -1);
625 for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) {
626 output_ << " 0 0 disasm " << info.slow_path->GetDescription() << std::endl;
627 disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end);
628 output_ << kEndInstructionMarker << std::endl;
629 }
630 DumpEndOfDisassemblyBlock();
631 }
632
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100633 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100634 StartTag("cfg");
David Brazdilffee3d32015-07-06 11:48:53 +0100635 std::string pass_desc = std::string(pass_name_)
636 + " ("
637 + (is_after_pass_ ? "after" : "before")
638 + (graph_in_bad_state_ ? ", bad_state" : "")
639 + ")";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000640 PrintProperty("name", pass_desc.c_str());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100641 if (disasm_info_ != nullptr) {
642 DumpDisassemblyBlockForFrameEntry();
643 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100644 VisitInsertionOrder();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100645 if (disasm_info_ != nullptr) {
646 DumpDisassemblyBlockForSlowPaths();
647 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100648 EndTag("cfg");
649 }
650
David Brazdilb7e4a062014-12-29 15:35:02 +0000651 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100652 StartTag("block");
653 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100654 if (block->GetLifetimeStart() != kNoLifetime) {
655 // Piggy back on these fields to show the lifetime of the block.
656 PrintInt("from_bci", block->GetLifetimeStart());
657 PrintInt("to_bci", block->GetLifetimeEnd());
658 } else {
659 PrintInt("from_bci", -1);
660 PrintInt("to_bci", -1);
661 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100662 PrintPredecessors(block);
663 PrintSuccessors(block);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000664 PrintExceptionHandlers(block);
665
666 if (block->IsCatchBlock()) {
667 PrintProperty("flags", "catch_block");
668 } else {
669 PrintEmptyProperty("flags");
670 }
671
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100672 if (block->GetDominator() != nullptr) {
673 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
674 }
675
676 StartTag("states");
677 StartTag("locals");
678 PrintInt("size", 0);
679 PrintProperty("method", "None");
680 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
681 AddIndent();
682 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100683 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
684 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100685 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
686 output_ << inputs.Current()->GetId() << " ";
687 }
688 output_ << "]" << std::endl;
689 }
690 EndTag("locals");
691 EndTag("states");
692
693 StartTag("HIR");
694 PrintInstructions(block->GetPhis());
695 PrintInstructions(block->GetInstructions());
696 EndTag("HIR");
697 EndTag("block");
698 }
699
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100700 static constexpr const char* const kEndInstructionMarker = "<|@";
701 static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry";
702 static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths";
703
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100704 private:
705 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100706 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000707 const bool is_after_pass_;
David Brazdilffee3d32015-07-06 11:48:53 +0100708 const bool graph_in_bad_state_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100709 const CodeGenerator& codegen_;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100710 const DisassemblyInformation* disasm_info_;
711 std::unique_ptr<HGraphVisualizerDisassembler> disassembler_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100712 size_t indent_;
713
714 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
715};
716
717HGraphVisualizer::HGraphVisualizer(std::ostream* output,
718 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100719 const CodeGenerator& codegen)
720 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100721
David Brazdil62e074f2015-04-07 18:09:37 +0100722void HGraphVisualizer::PrintHeader(const char* method_name) const {
723 DCHECK(output_ != nullptr);
David Brazdilffee3d32015-07-06 11:48:53 +0100724 HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100725 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000726 printer.PrintProperty("name", method_name);
727 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100728 printer.PrintTime("date");
729 printer.EndTag("compilation");
730}
731
David Brazdilffee3d32015-07-06 11:48:53 +0100732void HGraphVisualizer::DumpGraph(const char* pass_name,
733 bool is_after_pass,
734 bool graph_in_bad_state) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000735 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100736 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100737 HGraphVisualizerPrinter printer(graph_,
738 *output_,
739 pass_name,
740 is_after_pass,
741 graph_in_bad_state,
742 codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000743 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100744 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100745}
746
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100747void HGraphVisualizer::DumpGraphWithDisassembly() const {
748 DCHECK(output_ != nullptr);
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100749 if (!graph_->GetBlocks().empty()) {
David Brazdilffee3d32015-07-06 11:48:53 +0100750 HGraphVisualizerPrinter printer(graph_,
751 *output_,
752 "disassembly",
753 /* is_after_pass */ true,
754 /* graph_in_bad_state */ false,
755 codegen_,
756 codegen_.GetDisassemblyInformation());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100757 printer.Run();
758 }
759}
760
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100761} // namespace art