blob: a49ce64a2d3a347d276e8303e3ef16bbf061900a [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 "driver/dex_compilation_unit.h"
21#include "nodes.h"
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010022#include "ssa_liveness_analysis.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010023
24namespace art {
25
26/**
27 * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra.
28 */
29class HGraphVisualizerPrinter : public HGraphVisitor {
30 public:
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010031 HGraphVisualizerPrinter(HGraph* graph,
32 std::ostream& output,
33 const char* pass_name,
34 const CodeGenerator& codegen)
35 : HGraphVisitor(graph),
36 output_(output),
37 pass_name_(pass_name),
38 codegen_(codegen),
39 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010040
41 void StartTag(const char* name) {
42 AddIndent();
43 output_ << "begin_" << name << std::endl;
44 indent_++;
45 }
46
47 void EndTag(const char* name) {
48 indent_--;
49 AddIndent();
50 output_ << "end_" << name << std::endl;
51 }
52
53 void PrintProperty(const char* name, const char* property) {
54 AddIndent();
55 output_ << name << " \"" << property << "\"" << std::endl;
56 }
57
58 void PrintProperty(const char* name, const char* property, int id) {
59 AddIndent();
60 output_ << name << " \"" << property << id << "\"" << std::endl;
61 }
62
63 void PrintEmptyProperty(const char* name) {
64 AddIndent();
65 output_ << name << std::endl;
66 }
67
68 void PrintTime(const char* name) {
69 AddIndent();
70 output_ << name << " " << time(NULL) << std::endl;
71 }
72
73 void PrintInt(const char* name, int value) {
74 AddIndent();
75 output_ << name << " " << value << std::endl;
76 }
77
78 void AddIndent() {
79 for (size_t i = 0; i < indent_; ++i) {
80 output_ << " ";
81 }
82 }
83
84 void PrintPredecessors(HBasicBlock* block) {
85 AddIndent();
86 output_ << "predecessors";
87 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
88 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
89 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
90 }
91 output_<< std::endl;
92 }
93
94 void PrintSuccessors(HBasicBlock* block) {
95 AddIndent();
96 output_ << "successors";
97 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
98 HBasicBlock* successor = block->GetSuccessors().Get(i);
99 output_ << " \"B" << successor->GetBlockId() << "\" ";
100 }
101 output_<< std::endl;
102 }
103
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100104 void DumpLocation(Location location, Primitive::Type type) {
105 if (location.IsRegister()) {
106 if (type == Primitive::kPrimDouble || type == Primitive::kPrimFloat) {
107 codegen_.DumpFloatingPointRegister(output_, location.reg().RegId());
108 } else {
109 codegen_.DumpCoreRegister(output_, location.reg().RegId());
110 }
111 } else {
112 DCHECK(location.IsStackSlot());
113 output_ << location.GetStackIndex() << "(sp)";
114 }
115 }
116
117 void VisitParallelMove(HParallelMove* instruction) {
118 output_ << instruction->DebugName();
119 output_ << " (";
120 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
121 MoveOperands* move = instruction->MoveOperandsAt(i);
122 DumpLocation(move->GetSource(), Primitive::kPrimInt);
123 output_ << " -> ";
124 DumpLocation(move->GetDestination(), Primitive::kPrimInt);
125 if (i + 1 != e) {
126 output_ << ", ";
127 }
128 }
129 output_ << ")";
130 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100131
132 void VisitInstruction(HInstruction* instruction) {
133 output_ << instruction->DebugName();
134 if (instruction->InputCount() > 0) {
135 output_ << " [ ";
136 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
137 output_ << "v" << inputs.Current()->GetId() << " ";
138 }
139 output_ << "]";
140 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100141 if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100142 output_ << " (liveness: " << instruction->GetLifetimePosition();
143 if (instruction->HasLiveInterval()) {
144 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100145 const LiveInterval& interval = *instruction->GetLiveInterval();
146 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100147 }
148 output_ << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100149 } else if (pass_name_ == kRegisterAllocatorPassName) {
150 LocationSummary* locations = instruction->GetLocations();
151 if (locations != nullptr) {
152 output_ << " ( ";
153 for (size_t i = 0; i < instruction->InputCount(); ++i) {
154 DumpLocation(locations->InAt(i), instruction->InputAt(i)->GetType());
155 output_ << " ";
156 }
157 output_ << ")";
158 if (locations->Out().IsValid()) {
159 output_ << " -> ";
160 DumpLocation(locations->Out(), instruction->GetType());
161 }
162 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100163 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100164 }
165
166 void PrintInstructions(const HInstructionList& list) {
167 const char* kEndInstructionMarker = "<|@";
168 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
169 HInstruction* instruction = it.Current();
170 AddIndent();
171 int bci = 0;
172 output_ << bci << " " << instruction->NumberOfUses() << " v" << instruction->GetId() << " ";
173 instruction->Accept(this);
174 output_ << kEndInstructionMarker << std::endl;
175 }
176 }
177
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100178 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100179 StartTag("cfg");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100180 PrintProperty("name", pass_name_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100181 VisitInsertionOrder();
182 EndTag("cfg");
183 }
184
185 void VisitBasicBlock(HBasicBlock* block) {
186 StartTag("block");
187 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100188 if (block->GetLifetimeStart() != kNoLifetime) {
189 // Piggy back on these fields to show the lifetime of the block.
190 PrintInt("from_bci", block->GetLifetimeStart());
191 PrintInt("to_bci", block->GetLifetimeEnd());
192 } else {
193 PrintInt("from_bci", -1);
194 PrintInt("to_bci", -1);
195 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100196 PrintPredecessors(block);
197 PrintSuccessors(block);
198 PrintEmptyProperty("xhandlers");
199 PrintEmptyProperty("flags");
200 if (block->GetDominator() != nullptr) {
201 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
202 }
203
204 StartTag("states");
205 StartTag("locals");
206 PrintInt("size", 0);
207 PrintProperty("method", "None");
208 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
209 AddIndent();
210 HInstruction* instruction = it.Current();
211 output_ << instruction->GetId() << " v" << instruction->GetId() << "[ ";
212 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
213 output_ << inputs.Current()->GetId() << " ";
214 }
215 output_ << "]" << std::endl;
216 }
217 EndTag("locals");
218 EndTag("states");
219
220 StartTag("HIR");
221 PrintInstructions(block->GetPhis());
222 PrintInstructions(block->GetInstructions());
223 EndTag("HIR");
224 EndTag("block");
225 }
226
227 private:
228 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100229 const char* pass_name_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100230 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100231 size_t indent_;
232
233 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
234};
235
236HGraphVisualizer::HGraphVisualizer(std::ostream* output,
237 HGraph* graph,
238 const char* string_filter,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100239 const CodeGenerator& codegen,
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100240 const DexCompilationUnit& cu)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100241 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100242 if (output == nullptr) {
243 return;
244 }
245 std::string pretty_name = PrettyMethod(cu.GetDexMethodIndex(), *cu.GetDexFile());
246 if (pretty_name.find(string_filter) == std::string::npos) {
247 return;
248 }
249
250 is_enabled_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100251 HGraphVisualizerPrinter printer(graph, *output_, "", codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100252 printer.StartTag("compilation");
253 printer.PrintProperty("name", pretty_name.c_str());
254 printer.PrintProperty("method", pretty_name.c_str());
255 printer.PrintTime("date");
256 printer.EndTag("compilation");
257}
258
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100259HGraphVisualizer::HGraphVisualizer(std::ostream* output,
260 HGraph* graph,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100261 const CodeGenerator& codegen,
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100262 const char* name)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100263 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100264 if (output == nullptr) {
265 return;
266 }
267
268 is_enabled_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100269 HGraphVisualizerPrinter printer(graph, *output_, "", codegen_);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100270 printer.StartTag("compilation");
271 printer.PrintProperty("name", name);
272 printer.PrintProperty("method", name);
273 printer.PrintTime("date");
274 printer.EndTag("compilation");
275}
276
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277void HGraphVisualizer::DumpGraph(const char* pass_name) {
278 if (!is_enabled_) {
279 return;
280 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100281 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, codegen_);
282 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100283}
284
285} // namespace art