blob: ef461d9ac5c7ab65a2d75a340cb08f41aa517e69 [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,
Nicolas Geoffray840e5462015-01-07 16:01:24 +000033 bool is_after_pass,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010034 const CodeGenerator& codegen)
35 : HGraphVisitor(graph),
36 output_(output),
37 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +000038 is_after_pass_(is_after_pass),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010039 codegen_(codegen),
40 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010041
42 void StartTag(const char* name) {
43 AddIndent();
44 output_ << "begin_" << name << std::endl;
45 indent_++;
46 }
47
48 void EndTag(const char* name) {
49 indent_--;
50 AddIndent();
51 output_ << "end_" << name << std::endl;
52 }
53
54 void PrintProperty(const char* name, const char* property) {
55 AddIndent();
56 output_ << name << " \"" << property << "\"" << std::endl;
57 }
58
59 void PrintProperty(const char* name, const char* property, int id) {
60 AddIndent();
61 output_ << name << " \"" << property << id << "\"" << std::endl;
62 }
63
64 void PrintEmptyProperty(const char* name) {
65 AddIndent();
66 output_ << name << std::endl;
67 }
68
69 void PrintTime(const char* name) {
70 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -080071 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010072 }
73
74 void PrintInt(const char* name, int value) {
75 AddIndent();
76 output_ << name << " " << value << std::endl;
77 }
78
79 void AddIndent() {
80 for (size_t i = 0; i < indent_; ++i) {
81 output_ << " ";
82 }
83 }
84
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010085 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +010086 // Note that Primitive::Descriptor would not work for us
87 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010088 switch (type) {
89 case Primitive::kPrimBoolean: return 'z';
90 case Primitive::kPrimByte: return 'b';
91 case Primitive::kPrimChar: return 'c';
92 case Primitive::kPrimShort: return 's';
93 case Primitive::kPrimInt: return 'i';
94 case Primitive::kPrimLong: return 'j';
95 case Primitive::kPrimFloat: return 'f';
96 case Primitive::kPrimDouble: return 'd';
97 case Primitive::kPrimNot: return 'l';
98 case Primitive::kPrimVoid: return 'v';
99 }
100 LOG(FATAL) << "Unreachable";
101 return 'v';
102 }
103
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100104 void PrintPredecessors(HBasicBlock* block) {
105 AddIndent();
106 output_ << "predecessors";
107 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
108 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
109 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
110 }
111 output_<< std::endl;
112 }
113
114 void PrintSuccessors(HBasicBlock* block) {
115 AddIndent();
116 output_ << "successors";
117 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
118 HBasicBlock* successor = block->GetSuccessors().Get(i);
119 output_ << " \"B" << successor->GetBlockId() << "\" ";
120 }
121 output_<< std::endl;
122 }
123
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100124 void DumpLocation(Location location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100125 if (location.IsRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100126 codegen_.DumpCoreRegister(output_, location.reg());
127 } else if (location.IsFpuRegister()) {
128 codegen_.DumpFloatingPointRegister(output_, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100129 } else if (location.IsConstant()) {
130 output_ << "constant";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100131 HConstant* constant = location.GetConstant();
132 if (constant->IsIntConstant()) {
133 output_ << " " << constant->AsIntConstant()->GetValue();
134 } else if (constant->IsLongConstant()) {
135 output_ << " " << constant->AsLongConstant()->GetValue();
136 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100137 } else if (location.IsInvalid()) {
138 output_ << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100139 } else if (location.IsStackSlot()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100140 output_ << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000141 } else if (location.IsFpuRegisterPair()) {
142 codegen_.DumpFloatingPointRegister(output_, location.low());
143 output_ << " and ";
144 codegen_.DumpFloatingPointRegister(output_, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000145 } else if (location.IsRegisterPair()) {
146 codegen_.DumpCoreRegister(output_, location.low());
147 output_ << " and ";
148 codegen_.DumpCoreRegister(output_, location.high());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100149 } else {
150 DCHECK(location.IsDoubleStackSlot());
151 output_ << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100152 }
153 }
154
David Brazdilb7e4a062014-12-29 15:35:02 +0000155 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100156 output_ << " (";
157 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
158 MoveOperands* move = instruction->MoveOperandsAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100159 DumpLocation(move->GetSource());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100160 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100161 DumpLocation(move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100162 if (i + 1 != e) {
163 output_ << ", ";
164 }
165 }
166 output_ << ")";
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100167 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100168 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100169
David Brazdil36cf0952015-01-08 19:28:33 +0000170 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000171 output_ << " " << instruction->GetValue();
172 }
173
David Brazdil36cf0952015-01-08 19:28:33 +0000174 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000175 output_ << " " << instruction->GetValue();
176 }
177
David Brazdil36cf0952015-01-08 19:28:33 +0000178 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000179 output_ << " " << instruction->GetValue();
180 }
181
David Brazdil36cf0952015-01-08 19:28:33 +0000182 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000183 output_ << " " << instruction->GetValue();
184 }
185
186 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100187 output_ << instruction->DebugName();
David Brazdilb7e4a062014-12-29 15:35:02 +0000188 instruction->Accept(this);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100189 if (instruction->InputCount() > 0) {
190 output_ << " [ ";
191 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100192 output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100193 }
194 output_ << "]";
195 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100196 if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100197 output_ << " (liveness: " << instruction->GetLifetimePosition();
198 if (instruction->HasLiveInterval()) {
199 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100200 const LiveInterval& interval = *instruction->GetLiveInterval();
201 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100202 }
203 output_ << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100204 } else if (pass_name_ == kRegisterAllocatorPassName) {
205 LocationSummary* locations = instruction->GetLocations();
206 if (locations != nullptr) {
207 output_ << " ( ";
208 for (size_t i = 0; i < instruction->InputCount(); ++i) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100209 DumpLocation(locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100210 output_ << " ";
211 }
212 output_ << ")";
213 if (locations->Out().IsValid()) {
214 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100215 DumpLocation(locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100216 }
217 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100218 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100220 }
221
222 void PrintInstructions(const HInstructionList& list) {
223 const char* kEndInstructionMarker = "<|@";
224 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
225 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100226 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000227 size_t num_uses = 0;
228 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
229 !use_it.Done();
230 use_it.Advance()) {
231 ++num_uses;
232 }
233 AddIndent();
234 output_ << bci << " " << num_uses << " "
235 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000236 PrintInstruction(instruction);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100237 output_ << kEndInstructionMarker << std::endl;
238 }
239 }
240
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100241 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100242 StartTag("cfg");
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000243 std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)");
244 PrintProperty("name", pass_desc.c_str());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100245 VisitInsertionOrder();
246 EndTag("cfg");
247 }
248
David Brazdilb7e4a062014-12-29 15:35:02 +0000249 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100250 StartTag("block");
251 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100252 if (block->GetLifetimeStart() != kNoLifetime) {
253 // Piggy back on these fields to show the lifetime of the block.
254 PrintInt("from_bci", block->GetLifetimeStart());
255 PrintInt("to_bci", block->GetLifetimeEnd());
256 } else {
257 PrintInt("from_bci", -1);
258 PrintInt("to_bci", -1);
259 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100260 PrintPredecessors(block);
261 PrintSuccessors(block);
262 PrintEmptyProperty("xhandlers");
263 PrintEmptyProperty("flags");
264 if (block->GetDominator() != nullptr) {
265 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
266 }
267
268 StartTag("states");
269 StartTag("locals");
270 PrintInt("size", 0);
271 PrintProperty("method", "None");
272 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
273 AddIndent();
274 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100275 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
276 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
278 output_ << inputs.Current()->GetId() << " ";
279 }
280 output_ << "]" << std::endl;
281 }
282 EndTag("locals");
283 EndTag("states");
284
285 StartTag("HIR");
286 PrintInstructions(block->GetPhis());
287 PrintInstructions(block->GetInstructions());
288 EndTag("HIR");
289 EndTag("block");
290 }
291
292 private:
293 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100294 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000295 const bool is_after_pass_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100296 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100297 size_t indent_;
298
299 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
300};
301
302HGraphVisualizer::HGraphVisualizer(std::ostream* output,
303 HGraph* graph,
304 const char* string_filter,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100305 const CodeGenerator& codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000306 const char* method_name)
David Brazdilee690a32014-12-01 17:04:16 +0000307 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100308 if (output == nullptr) {
309 return;
310 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000311 if (strstr(method_name, string_filter) == nullptr) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100312 return;
313 }
314
315 is_enabled_ = true;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000316 HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100317 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000318 printer.PrintProperty("name", method_name);
319 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100320 printer.PrintTime("date");
321 printer.EndTag("compilation");
322}
323
David Brazdilee690a32014-12-01 17:04:16 +0000324void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
325 if (is_enabled_) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000326 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000327 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100328 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100329}
330
331} // namespace art