blob: ca9cbc3d01e253a6ec7e8942860cab410d93f182 [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
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010019#include "code_generator.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080020#include "licm.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010021#include "nodes.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000022#include "optimization.h"
Andreas Gampe7c3952f2015-02-19 18:21:24 -080023#include "register_allocator.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010024#include "ssa_liveness_analysis.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010025
26namespace art {
27
28/**
29 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
30 */
31class HGraphVisualizerPrinter : public HGraphVisitor {
32 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010033 HGraphVisualizerPrinter(HGraph* graph,
34 std::ostream& output,
35 const char* pass_name,
Nicolas Geoffray840e5462015-01-07 16:01:24 +000036 bool is_after_pass,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010037 const CodeGenerator& codegen)
38 : HGraphVisitor(graph),
39 output_(output),
40 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +000041 is_after_pass_(is_after_pass),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010042 codegen_(codegen),
43 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010044
45 void StartTag(const char* name) {
46 AddIndent();
47 output_ << "begin_" << name << std::endl;
48 indent_++;
49 }
50
51 void EndTag(const char* name) {
52 indent_--;
53 AddIndent();
54 output_ << "end_" << name << std::endl;
55 }
56
57 void PrintProperty(const char* name, const char* property) {
58 AddIndent();
59 output_ << name << " \"" << property << "\"" << std::endl;
60 }
61
62 void PrintProperty(const char* name, const char* property, int id) {
63 AddIndent();
64 output_ << name << " \"" << property << id << "\"" << std::endl;
65 }
66
67 void PrintEmptyProperty(const char* name) {
68 AddIndent();
69 output_ << name << std::endl;
70 }
71
72 void PrintTime(const char* name) {
73 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -080074 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010075 }
76
77 void PrintInt(const char* name, int value) {
78 AddIndent();
79 output_ << name << " " << value << std::endl;
80 }
81
82 void AddIndent() {
83 for (size_t i = 0; i < indent_; ++i) {
84 output_ << " ";
85 }
86 }
87
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010088 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +010089 // Note that Primitive::Descriptor would not work for us
90 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010091 switch (type) {
92 case Primitive::kPrimBoolean: return 'z';
93 case Primitive::kPrimByte: return 'b';
94 case Primitive::kPrimChar: return 'c';
95 case Primitive::kPrimShort: return 's';
96 case Primitive::kPrimInt: return 'i';
97 case Primitive::kPrimLong: return 'j';
98 case Primitive::kPrimFloat: return 'f';
99 case Primitive::kPrimDouble: return 'd';
100 case Primitive::kPrimNot: return 'l';
101 case Primitive::kPrimVoid: return 'v';
102 }
103 LOG(FATAL) << "Unreachable";
104 return 'v';
105 }
106
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100107 void PrintPredecessors(HBasicBlock* block) {
108 AddIndent();
109 output_ << "predecessors";
110 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
111 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
112 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
113 }
114 output_<< std::endl;
115 }
116
117 void PrintSuccessors(HBasicBlock* block) {
118 AddIndent();
119 output_ << "successors";
120 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
121 HBasicBlock* successor = block->GetSuccessors().Get(i);
122 output_ << " \"B" << successor->GetBlockId() << "\" ";
123 }
124 output_<< std::endl;
125 }
126
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100127 void DumpLocation(Location location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100128 if (location.IsRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100129 codegen_.DumpCoreRegister(output_, location.reg());
130 } else if (location.IsFpuRegister()) {
131 codegen_.DumpFloatingPointRegister(output_, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100132 } else if (location.IsConstant()) {
133 output_ << "constant";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100134 HConstant* constant = location.GetConstant();
135 if (constant->IsIntConstant()) {
136 output_ << " " << constant->AsIntConstant()->GetValue();
137 } else if (constant->IsLongConstant()) {
138 output_ << " " << constant->AsLongConstant()->GetValue();
139 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100140 } else if (location.IsInvalid()) {
141 output_ << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100142 } else if (location.IsStackSlot()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100143 output_ << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000144 } else if (location.IsFpuRegisterPair()) {
145 codegen_.DumpFloatingPointRegister(output_, location.low());
146 output_ << " and ";
147 codegen_.DumpFloatingPointRegister(output_, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000148 } else if (location.IsRegisterPair()) {
149 codegen_.DumpCoreRegister(output_, location.low());
150 output_ << " and ";
151 codegen_.DumpCoreRegister(output_, location.high());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400152 } else if (location.IsUnallocated()) {
153 output_ << "<U>";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100154 } else {
155 DCHECK(location.IsDoubleStackSlot());
156 output_ << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100157 }
158 }
159
David Brazdilb7e4a062014-12-29 15:35:02 +0000160 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100161 output_ << " (";
162 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
163 MoveOperands* move = instruction->MoveOperandsAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100164 DumpLocation(move->GetSource());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100165 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100166 DumpLocation(move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100167 if (i + 1 != e) {
168 output_ << ", ";
169 }
170 }
171 output_ << ")";
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100172 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100173 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100174
David Brazdil36cf0952015-01-08 19:28:33 +0000175 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000176 output_ << " " << instruction->GetValue();
177 }
178
David Brazdil36cf0952015-01-08 19:28:33 +0000179 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000180 output_ << " " << instruction->GetValue();
181 }
182
David Brazdil36cf0952015-01-08 19:28:33 +0000183 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000184 output_ << " " << instruction->GetValue();
185 }
186
David Brazdil36cf0952015-01-08 19:28:33 +0000187 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000188 output_ << " " << instruction->GetValue();
189 }
190
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000191 void VisitPhi(HPhi* phi) OVERRIDE {
192 output_ << " " << phi->GetRegNumber();
193 }
194
Calin Juravle27df7582015-04-17 19:12:31 +0100195 void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE {
196 output_ << " " << barrier->GetBarrierKind();
197 }
198
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800199 bool IsPass(const char* name) {
200 return strcmp(pass_name_, name) == 0;
201 }
202
David Brazdilb7e4a062014-12-29 15:35:02 +0000203 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100204 output_ << instruction->DebugName();
David Brazdilb7e4a062014-12-29 15:35:02 +0000205 instruction->Accept(this);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100206 if (instruction->InputCount() > 0) {
207 output_ << " [ ";
208 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100209 output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100210 }
211 output_ << "]";
212 }
Zheng Xubb7a28a2015-01-09 14:40:47 +0800213 if (instruction->HasEnvironment()) {
214 HEnvironment* env = instruction->GetEnvironment();
215 output_ << " (env: [ ";
216 for (size_t i = 0, e = env->Size(); i < e; ++i) {
217 HInstruction* insn = env->GetInstructionAt(i);
218 if (insn != nullptr) {
219 output_ << GetTypeId(insn->GetType()) << insn->GetId() << " ";
220 } else {
221 output_ << " _ ";
222 }
223 }
224 output_ << "])";
225 }
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800226 if (IsPass(SsaLivenessAnalysis::kLivenessPassName)
David Brazdil5e8b1372015-01-23 14:39:08 +0000227 && is_after_pass_
228 && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100229 output_ << " (liveness: " << instruction->GetLifetimePosition();
230 if (instruction->HasLiveInterval()) {
231 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100232 const LiveInterval& interval = *instruction->GetLiveInterval();
233 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100234 }
235 output_ << ")";
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800236 } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100237 LocationSummary* locations = instruction->GetLocations();
238 if (locations != nullptr) {
239 output_ << " ( ";
240 for (size_t i = 0; i < instruction->InputCount(); ++i) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100241 DumpLocation(locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100242 output_ << " ";
243 }
244 output_ << ")";
245 if (locations->Out().IsValid()) {
246 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100247 DumpLocation(locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100248 }
249 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100250 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800251 } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName)) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000252 output_ << " ( loop_header:";
253 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
254 if (info == nullptr) {
255 output_ << "null )";
256 } else {
257 output_ << "B" << info->GetHeader()->GetBlockId() << " )";
258 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100259 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100260 }
261
262 void PrintInstructions(const HInstructionList& list) {
263 const char* kEndInstructionMarker = "<|@";
264 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
265 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100266 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000267 size_t num_uses = 0;
268 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
269 !use_it.Done();
270 use_it.Advance()) {
271 ++num_uses;
272 }
273 AddIndent();
274 output_ << bci << " " << num_uses << " "
275 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000276 PrintInstruction(instruction);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277 output_ << kEndInstructionMarker << std::endl;
278 }
279 }
280
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100281 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100282 StartTag("cfg");
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000283 std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)");
284 PrintProperty("name", pass_desc.c_str());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100285 VisitInsertionOrder();
286 EndTag("cfg");
287 }
288
David Brazdilb7e4a062014-12-29 15:35:02 +0000289 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100290 StartTag("block");
291 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 if (block->GetLifetimeStart() != kNoLifetime) {
293 // Piggy back on these fields to show the lifetime of the block.
294 PrintInt("from_bci", block->GetLifetimeStart());
295 PrintInt("to_bci", block->GetLifetimeEnd());
296 } else {
297 PrintInt("from_bci", -1);
298 PrintInt("to_bci", -1);
299 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100300 PrintPredecessors(block);
301 PrintSuccessors(block);
302 PrintEmptyProperty("xhandlers");
303 PrintEmptyProperty("flags");
304 if (block->GetDominator() != nullptr) {
305 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
306 }
307
308 StartTag("states");
309 StartTag("locals");
310 PrintInt("size", 0);
311 PrintProperty("method", "None");
312 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
313 AddIndent();
314 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100315 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
316 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100317 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
318 output_ << inputs.Current()->GetId() << " ";
319 }
320 output_ << "]" << std::endl;
321 }
322 EndTag("locals");
323 EndTag("states");
324
325 StartTag("HIR");
326 PrintInstructions(block->GetPhis());
327 PrintInstructions(block->GetInstructions());
328 EndTag("HIR");
329 EndTag("block");
330 }
331
332 private:
333 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100334 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000335 const bool is_after_pass_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100336 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100337 size_t indent_;
338
339 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
340};
341
342HGraphVisualizer::HGraphVisualizer(std::ostream* output,
343 HGraph* graph,
David Brazdil62e074f2015-04-07 18:09:37 +0100344 const CodeGenerator& codegen)
345 : output_(output), graph_(graph), codegen_(codegen) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100346
David Brazdil62e074f2015-04-07 18:09:37 +0100347void HGraphVisualizer::PrintHeader(const char* method_name) const {
348 DCHECK(output_ != nullptr);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000349 HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100350 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 printer.PrintProperty("name", method_name);
352 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100353 printer.PrintTime("date");
354 printer.EndTag("compilation");
355}
356
David Brazdilee690a32014-12-01 17:04:16 +0000357void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000358 DCHECK(output_ != nullptr);
359 if (!graph_->GetBlocks().IsEmpty()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000360 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000361 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100362 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100363}
364
365} // namespace art