blob: 99b521913196bb84edc2953ace88b13d87a742da [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();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -080069 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010070 }
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 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100170 if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100171 output_ << " (liveness: " << instruction->GetLifetimePosition();
172 if (instruction->HasLiveInterval()) {
173 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100174 const LiveInterval& interval = *instruction->GetLiveInterval();
175 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100176 }
177 output_ << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100178 } else if (pass_name_ == kRegisterAllocatorPassName) {
179 LocationSummary* locations = instruction->GetLocations();
180 if (locations != nullptr) {
181 output_ << " ( ";
182 for (size_t i = 0; i < instruction->InputCount(); ++i) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100183 DumpLocation(locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100184 output_ << " ";
185 }
186 output_ << ")";
187 if (locations->Out().IsValid()) {
188 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100189 DumpLocation(locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100190 }
191 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100192 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100193 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100194 }
195
196 void PrintInstructions(const HInstructionList& list) {
197 const char* kEndInstructionMarker = "<|@";
198 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
199 HInstruction* instruction = it.Current();
200 AddIndent();
201 int bci = 0;
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100202 output_ << bci << " " << instruction->NumberOfUses()
203 << " " << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100204 instruction->Accept(this);
205 output_ << kEndInstructionMarker << std::endl;
206 }
207 }
208
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100209 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100210 StartTag("cfg");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100211 PrintProperty("name", pass_name_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100212 VisitInsertionOrder();
213 EndTag("cfg");
214 }
215
216 void VisitBasicBlock(HBasicBlock* block) {
217 StartTag("block");
218 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 if (block->GetLifetimeStart() != kNoLifetime) {
220 // Piggy back on these fields to show the lifetime of the block.
221 PrintInt("from_bci", block->GetLifetimeStart());
222 PrintInt("to_bci", block->GetLifetimeEnd());
223 } else {
224 PrintInt("from_bci", -1);
225 PrintInt("to_bci", -1);
226 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100227 PrintPredecessors(block);
228 PrintSuccessors(block);
229 PrintEmptyProperty("xhandlers");
230 PrintEmptyProperty("flags");
231 if (block->GetDominator() != nullptr) {
232 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
233 }
234
235 StartTag("states");
236 StartTag("locals");
237 PrintInt("size", 0);
238 PrintProperty("method", "None");
239 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
240 AddIndent();
241 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100242 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
243 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100244 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
245 output_ << inputs.Current()->GetId() << " ";
246 }
247 output_ << "]" << std::endl;
248 }
249 EndTag("locals");
250 EndTag("states");
251
252 StartTag("HIR");
253 PrintInstructions(block->GetPhis());
254 PrintInstructions(block->GetInstructions());
255 EndTag("HIR");
256 EndTag("block");
257 }
258
259 private:
260 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100261 const char* pass_name_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100262 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100263 size_t indent_;
264
265 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
266};
267
268HGraphVisualizer::HGraphVisualizer(std::ostream* output,
269 HGraph* graph,
270 const char* string_filter,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100271 const CodeGenerator& codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000272 const char* method_name)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100273 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100274 if (output == nullptr) {
275 return;
276 }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000277 if (strstr(method_name, string_filter) == nullptr) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100278 return;
279 }
280
281 is_enabled_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100282 HGraphVisualizerPrinter printer(graph, *output_, "", codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100283 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000284 printer.PrintProperty("name", method_name);
285 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100286 printer.PrintTime("date");
287 printer.EndTag("compilation");
288}
289
Roland Levillain75be2832014-10-17 17:02:00 +0100290void HGraphVisualizer::DumpGraph(const char* pass_name) const {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100291 if (!is_enabled_) {
292 return;
293 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100294 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, codegen_);
295 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100296}
297
298} // namespace art