blob: b14b0a70e23fd5a82a3b82d2642f2452adae4442 [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
145 void VisitParallelMove(HParallelMove* instruction) {
146 output_ << instruction->DebugName();
147 output_ << " (";
148 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
149 MoveOperands* move = instruction->MoveOperandsAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100150 DumpLocation(move->GetSource());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100151 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100152 DumpLocation(move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100153 if (i + 1 != e) {
154 output_ << ", ";
155 }
156 }
157 output_ << ")";
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100158 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100159 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100160
161 void VisitInstruction(HInstruction* instruction) {
162 output_ << instruction->DebugName();
163 if (instruction->InputCount() > 0) {
164 output_ << " [ ";
165 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100166 output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100167 }
168 output_ << "]";
169 }
David Brazdilee690a32014-12-01 17:04:16 +0000170 if (instruction->IsIntConstant()) {
171 output_ << " " << instruction->AsIntConstant()->GetValue();
172 } else if (instruction->IsLongConstant()) {
173 output_ << " " << instruction->AsLongConstant()->GetValue();
174 } else if (instruction->IsFloatConstant()) {
175 output_ << " " << instruction->AsFloatConstant()->GetValue();
176 } else if (instruction->IsDoubleConstant()) {
177 output_ << " " << instruction->AsDoubleConstant()->GetValue();
178 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100179 if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100180 output_ << " (liveness: " << instruction->GetLifetimePosition();
181 if (instruction->HasLiveInterval()) {
182 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100183 const LiveInterval& interval = *instruction->GetLiveInterval();
184 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185 }
186 output_ << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100187 } else if (pass_name_ == kRegisterAllocatorPassName) {
188 LocationSummary* locations = instruction->GetLocations();
189 if (locations != nullptr) {
190 output_ << " ( ";
191 for (size_t i = 0; i < instruction->InputCount(); ++i) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100192 DumpLocation(locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100193 output_ << " ";
194 }
195 output_ << ")";
196 if (locations->Out().IsValid()) {
197 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100198 DumpLocation(locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100199 }
200 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100201 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100202 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100203 }
204
205 void PrintInstructions(const HInstructionList& list) {
206 const char* kEndInstructionMarker = "<|@";
207 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
208 HInstruction* instruction = it.Current();
209 AddIndent();
210 int bci = 0;
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100211 output_ << bci << " " << instruction->NumberOfUses()
212 << " " << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100213 instruction->Accept(this);
214 output_ << kEndInstructionMarker << std::endl;
215 }
216 }
217
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100218 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100219 StartTag("cfg");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100220 PrintProperty("name", pass_name_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100221 VisitInsertionOrder();
222 EndTag("cfg");
223 }
224
225 void VisitBasicBlock(HBasicBlock* block) {
226 StartTag("block");
227 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100228 if (block->GetLifetimeStart() != kNoLifetime) {
229 // Piggy back on these fields to show the lifetime of the block.
230 PrintInt("from_bci", block->GetLifetimeStart());
231 PrintInt("to_bci", block->GetLifetimeEnd());
232 } else {
233 PrintInt("from_bci", -1);
234 PrintInt("to_bci", -1);
235 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100236 PrintPredecessors(block);
237 PrintSuccessors(block);
238 PrintEmptyProperty("xhandlers");
239 PrintEmptyProperty("flags");
240 if (block->GetDominator() != nullptr) {
241 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
242 }
243
244 StartTag("states");
245 StartTag("locals");
246 PrintInt("size", 0);
247 PrintProperty("method", "None");
248 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
249 AddIndent();
250 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100251 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
252 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100253 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
254 output_ << inputs.Current()->GetId() << " ";
255 }
256 output_ << "]" << std::endl;
257 }
258 EndTag("locals");
259 EndTag("states");
260
261 StartTag("HIR");
262 PrintInstructions(block->GetPhis());
263 PrintInstructions(block->GetInstructions());
264 EndTag("HIR");
265 EndTag("block");
266 }
267
268 private:
269 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100270 const char* pass_name_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100271 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100272 size_t indent_;
273
274 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
275};
276
277HGraphVisualizer::HGraphVisualizer(std::ostream* output,
278 HGraph* graph,
279 const char* string_filter,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100280 const CodeGenerator& codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000281 const char* method_name)
David Brazdilee690a32014-12-01 17:04:16 +0000282 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100283 if (output == nullptr) {
284 return;
285 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000286 if (strstr(method_name, string_filter) == nullptr) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100287 return;
288 }
289
290 is_enabled_ = true;
David Brazdilee690a32014-12-01 17:04:16 +0000291 HGraphVisualizerPrinter printer(graph_, *output_, "", codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100292 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000293 printer.PrintProperty("name", method_name);
294 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100295 printer.PrintTime("date");
296 printer.EndTag("compilation");
297}
298
David Brazdilee690a32014-12-01 17:04:16 +0000299void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
300 if (is_enabled_) {
301 std::string pass_desc = std::string(pass_name) + (is_after_pass ? " (after)" : " (before)");
302 HGraphVisualizerPrinter printer(graph_, *output_, pass_desc.c_str(), codegen_);
303 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100304 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100305}
306
307} // namespace art