blob: e688ebd034a22cac63fa7af32a6f162da588c481 [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"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010020#include "nodes.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010021#include "ssa_liveness_analysis.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010022
23namespace art {
24
25/**
26 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
27 */
28class HGraphVisualizerPrinter : public HGraphVisitor {
29 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010030 HGraphVisualizerPrinter(HGraph* graph,
31 std::ostream& output,
32 const char* pass_name,
33 const CodeGenerator& codegen)
34 : HGraphVisitor(graph),
35 output_(output),
36 pass_name_(pass_name),
37 codegen_(codegen),
38 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010039
40 void StartTag(const char* name) {
41 AddIndent();
42 output_ << "begin_" << name << std::endl;
43 indent_++;
44 }
45
46 void EndTag(const char* name) {
47 indent_--;
48 AddIndent();
49 output_ << "end_" << name << std::endl;
50 }
51
52 void PrintProperty(const char* name, const char* property) {
53 AddIndent();
54 output_ << name << " \"" << property << "\"" << std::endl;
55 }
56
57 void PrintProperty(const char* name, const char* property, int id) {
58 AddIndent();
59 output_ << name << " \"" << property << id << "\"" << std::endl;
60 }
61
62 void PrintEmptyProperty(const char* name) {
63 AddIndent();
64 output_ << name << std::endl;
65 }
66
67 void PrintTime(const char* name) {
68 AddIndent();
69 output_ << name << " " << time(NULL) << std::endl;
70 }
71
72 void PrintInt(const char* name, int value) {
73 AddIndent();
74 output_ << name << " " << value << std::endl;
75 }
76
77 void AddIndent() {
78 for (size_t i = 0; i < indent_; ++i) {
79 output_ << " ";
80 }
81 }
82
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010083 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +010084 // Note that Primitive::Descriptor would not work for us
85 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010086 switch (type) {
87 case Primitive::kPrimBoolean: return 'z';
88 case Primitive::kPrimByte: return 'b';
89 case Primitive::kPrimChar: return 'c';
90 case Primitive::kPrimShort: return 's';
91 case Primitive::kPrimInt: return 'i';
92 case Primitive::kPrimLong: return 'j';
93 case Primitive::kPrimFloat: return 'f';
94 case Primitive::kPrimDouble: return 'd';
95 case Primitive::kPrimNot: return 'l';
96 case Primitive::kPrimVoid: return 'v';
97 }
98 LOG(FATAL) << "Unreachable";
99 return 'v';
100 }
101
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100102 void PrintPredecessors(HBasicBlock* block) {
103 AddIndent();
104 output_ << "predecessors";
105 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
106 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
107 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
108 }
109 output_<< std::endl;
110 }
111
112 void PrintSuccessors(HBasicBlock* block) {
113 AddIndent();
114 output_ << "successors";
115 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
116 HBasicBlock* successor = block->GetSuccessors().Get(i);
117 output_ << " \"B" << successor->GetBlockId() << "\" ";
118 }
119 output_<< std::endl;
120 }
121
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100122 void DumpLocation(Location location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100123 if (location.IsRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100124 codegen_.DumpCoreRegister(output_, location.reg());
125 } else if (location.IsFpuRegister()) {
126 codegen_.DumpFloatingPointRegister(output_, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100127 } else if (location.IsConstant()) {
128 output_ << "constant";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100129 HConstant* constant = location.GetConstant();
130 if (constant->IsIntConstant()) {
131 output_ << " " << constant->AsIntConstant()->GetValue();
132 } else if (constant->IsLongConstant()) {
133 output_ << " " << constant->AsLongConstant()->GetValue();
134 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100135 } else if (location.IsInvalid()) {
136 output_ << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100137 } else if (location.IsStackSlot()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100138 output_ << location.GetStackIndex() << "(sp)";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100139 } else {
140 DCHECK(location.IsDoubleStackSlot());
141 output_ << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100142 }
143 }
144
David Brazdilb7e4a062014-12-29 15:35:02 +0000145 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100146 output_ << " (";
147 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
148 MoveOperands* move = instruction->MoveOperandsAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100149 DumpLocation(move->GetSource());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100150 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100151 DumpLocation(move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100152 if (i + 1 != e) {
153 output_ << ", ";
154 }
155 }
156 output_ << ")";
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100157 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100158 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100159
David Brazdil36cf0952015-01-08 19:28:33 +0000160 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000161 output_ << " " << instruction->GetValue();
162 }
163
David Brazdil36cf0952015-01-08 19:28:33 +0000164 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000165 output_ << " " << instruction->GetValue();
166 }
167
David Brazdil36cf0952015-01-08 19:28:33 +0000168 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000169 output_ << " " << instruction->GetValue();
170 }
171
David Brazdil36cf0952015-01-08 19:28:33 +0000172 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000173 output_ << " " << instruction->GetValue();
174 }
175
176 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100177 output_ << instruction->DebugName();
David Brazdilb7e4a062014-12-29 15:35:02 +0000178 instruction->Accept(this);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100179 if (instruction->InputCount() > 0) {
180 output_ << " [ ";
181 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100182 output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100183 }
184 output_ << "]";
185 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100186 if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100187 output_ << " (liveness: " << instruction->GetLifetimePosition();
188 if (instruction->HasLiveInterval()) {
189 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100190 const LiveInterval& interval = *instruction->GetLiveInterval();
191 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100192 }
193 output_ << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100194 } else if (pass_name_ == kRegisterAllocatorPassName) {
195 LocationSummary* locations = instruction->GetLocations();
196 if (locations != nullptr) {
197 output_ << " ( ";
198 for (size_t i = 0; i < instruction->InputCount(); ++i) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100199 DumpLocation(locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100200 output_ << " ";
201 }
202 output_ << ")";
203 if (locations->Out().IsValid()) {
204 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100205 DumpLocation(locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100206 }
207 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100208 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100209 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100210 }
211
212 void PrintInstructions(const HInstructionList& list) {
213 const char* kEndInstructionMarker = "<|@";
214 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
215 HInstruction* instruction = it.Current();
216 AddIndent();
217 int bci = 0;
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100218 output_ << bci << " " << instruction->NumberOfUses()
219 << " " << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000220 PrintInstruction(instruction);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100221 output_ << kEndInstructionMarker << std::endl;
222 }
223 }
224
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100225 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100226 StartTag("cfg");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100227 PrintProperty("name", pass_name_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100228 VisitInsertionOrder();
229 EndTag("cfg");
230 }
231
David Brazdilb7e4a062014-12-29 15:35:02 +0000232 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100233 StartTag("block");
234 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100235 if (block->GetLifetimeStart() != kNoLifetime) {
236 // Piggy back on these fields to show the lifetime of the block.
237 PrintInt("from_bci", block->GetLifetimeStart());
238 PrintInt("to_bci", block->GetLifetimeEnd());
239 } else {
240 PrintInt("from_bci", -1);
241 PrintInt("to_bci", -1);
242 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100243 PrintPredecessors(block);
244 PrintSuccessors(block);
245 PrintEmptyProperty("xhandlers");
246 PrintEmptyProperty("flags");
247 if (block->GetDominator() != nullptr) {
248 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
249 }
250
251 StartTag("states");
252 StartTag("locals");
253 PrintInt("size", 0);
254 PrintProperty("method", "None");
255 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
256 AddIndent();
257 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100258 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
259 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100260 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
261 output_ << inputs.Current()->GetId() << " ";
262 }
263 output_ << "]" << std::endl;
264 }
265 EndTag("locals");
266 EndTag("states");
267
268 StartTag("HIR");
269 PrintInstructions(block->GetPhis());
270 PrintInstructions(block->GetInstructions());
271 EndTag("HIR");
272 EndTag("block");
273 }
274
275 private:
276 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100277 const char* pass_name_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100278 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100279 size_t indent_;
280
281 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
282};
283
284HGraphVisualizer::HGraphVisualizer(std::ostream* output,
285 HGraph* graph,
286 const char* string_filter,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100287 const CodeGenerator& codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000288 const char* method_name)
David Brazdilee690a32014-12-01 17:04:16 +0000289 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100290 if (output == nullptr) {
291 return;
292 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000293 if (strstr(method_name, string_filter) == nullptr) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100294 return;
295 }
296
297 is_enabled_ = true;
David Brazdilee690a32014-12-01 17:04:16 +0000298 HGraphVisualizerPrinter printer(graph_, *output_, "", codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100299 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000300 printer.PrintProperty("name", method_name);
301 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100302 printer.PrintTime("date");
303 printer.EndTag("compilation");
304}
305
David Brazdilee690a32014-12-01 17:04:16 +0000306void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
307 if (is_enabled_) {
308 std::string pass_desc = std::string(pass_name) + (is_after_pass ? " (after)" : " (before)");
309 HGraphVisualizerPrinter printer(graph_, *output_, pass_desc.c_str(), codegen_);
310 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100311 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100312}
313
314} // namespace art