blob: 7f64be42d432addeeec1fa8b4b8fe157c0a00d06 [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
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010084 char GetTypeId(Primitive::Type type) {
85 switch (type) {
86 case Primitive::kPrimBoolean: return 'z';
87 case Primitive::kPrimByte: return 'b';
88 case Primitive::kPrimChar: return 'c';
89 case Primitive::kPrimShort: return 's';
90 case Primitive::kPrimInt: return 'i';
91 case Primitive::kPrimLong: return 'j';
92 case Primitive::kPrimFloat: return 'f';
93 case Primitive::kPrimDouble: return 'd';
94 case Primitive::kPrimNot: return 'l';
95 case Primitive::kPrimVoid: return 'v';
96 }
97 LOG(FATAL) << "Unreachable";
98 return 'v';
99 }
100
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100101 void PrintPredecessors(HBasicBlock* block) {
102 AddIndent();
103 output_ << "predecessors";
104 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
105 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
106 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
107 }
108 output_<< std::endl;
109 }
110
111 void PrintSuccessors(HBasicBlock* block) {
112 AddIndent();
113 output_ << "successors";
114 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
115 HBasicBlock* successor = block->GetSuccessors().Get(i);
116 output_ << " \"B" << successor->GetBlockId() << "\" ";
117 }
118 output_<< std::endl;
119 }
120
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100121 void DumpLocation(Location location, Primitive::Type type) {
122 if (location.IsRegister()) {
123 if (type == Primitive::kPrimDouble || type == Primitive::kPrimFloat) {
124 codegen_.DumpFloatingPointRegister(output_, location.reg().RegId());
125 } else {
126 codegen_.DumpCoreRegister(output_, location.reg().RegId());
127 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100128 } else if (location.IsConstant()) {
129 output_ << "constant";
130 } else if (location.IsInvalid()) {
131 output_ << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100132 } else if (location.IsStackSlot()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100133 output_ << location.GetStackIndex() << "(sp)";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100134 } else {
135 DCHECK(location.IsDoubleStackSlot());
136 output_ << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100137 }
138 }
139
140 void VisitParallelMove(HParallelMove* instruction) {
141 output_ << instruction->DebugName();
142 output_ << " (";
143 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
144 MoveOperands* move = instruction->MoveOperandsAt(i);
145 DumpLocation(move->GetSource(), Primitive::kPrimInt);
146 output_ << " -> ";
147 DumpLocation(move->GetDestination(), Primitive::kPrimInt);
148 if (i + 1 != e) {
149 output_ << ", ";
150 }
151 }
152 output_ << ")";
153 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100154
155 void VisitInstruction(HInstruction* instruction) {
156 output_ << instruction->DebugName();
157 if (instruction->InputCount() > 0) {
158 output_ << " [ ";
159 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100160 output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100161 }
162 output_ << "]";
163 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100164 if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100165 output_ << " (liveness: " << instruction->GetLifetimePosition();
166 if (instruction->HasLiveInterval()) {
167 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100168 const LiveInterval& interval = *instruction->GetLiveInterval();
169 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100170 }
171 output_ << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100172 } else if (pass_name_ == kRegisterAllocatorPassName) {
173 LocationSummary* locations = instruction->GetLocations();
174 if (locations != nullptr) {
175 output_ << " ( ";
176 for (size_t i = 0; i < instruction->InputCount(); ++i) {
177 DumpLocation(locations->InAt(i), instruction->InputAt(i)->GetType());
178 output_ << " ";
179 }
180 output_ << ")";
181 if (locations->Out().IsValid()) {
182 output_ << " -> ";
183 DumpLocation(locations->Out(), instruction->GetType());
184 }
185 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100187 }
188
189 void PrintInstructions(const HInstructionList& list) {
190 const char* kEndInstructionMarker = "<|@";
191 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
192 HInstruction* instruction = it.Current();
193 AddIndent();
194 int bci = 0;
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100195 output_ << bci << " " << instruction->NumberOfUses()
196 << " " << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100197 instruction->Accept(this);
198 output_ << kEndInstructionMarker << std::endl;
199 }
200 }
201
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100202 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100203 StartTag("cfg");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100204 PrintProperty("name", pass_name_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100205 VisitInsertionOrder();
206 EndTag("cfg");
207 }
208
209 void VisitBasicBlock(HBasicBlock* block) {
210 StartTag("block");
211 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100212 if (block->GetLifetimeStart() != kNoLifetime) {
213 // Piggy back on these fields to show the lifetime of the block.
214 PrintInt("from_bci", block->GetLifetimeStart());
215 PrintInt("to_bci", block->GetLifetimeEnd());
216 } else {
217 PrintInt("from_bci", -1);
218 PrintInt("to_bci", -1);
219 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100220 PrintPredecessors(block);
221 PrintSuccessors(block);
222 PrintEmptyProperty("xhandlers");
223 PrintEmptyProperty("flags");
224 if (block->GetDominator() != nullptr) {
225 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
226 }
227
228 StartTag("states");
229 StartTag("locals");
230 PrintInt("size", 0);
231 PrintProperty("method", "None");
232 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
233 AddIndent();
234 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100235 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
236 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100237 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
238 output_ << inputs.Current()->GetId() << " ";
239 }
240 output_ << "]" << std::endl;
241 }
242 EndTag("locals");
243 EndTag("states");
244
245 StartTag("HIR");
246 PrintInstructions(block->GetPhis());
247 PrintInstructions(block->GetInstructions());
248 EndTag("HIR");
249 EndTag("block");
250 }
251
252 private:
253 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100254 const char* pass_name_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100255 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100256 size_t indent_;
257
258 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
259};
260
261HGraphVisualizer::HGraphVisualizer(std::ostream* output,
262 HGraph* graph,
263 const char* string_filter,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100264 const CodeGenerator& codegen,
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100265 const DexCompilationUnit& cu)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100266 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100267 if (output == nullptr) {
268 return;
269 }
270 std::string pretty_name = PrettyMethod(cu.GetDexMethodIndex(), *cu.GetDexFile());
271 if (pretty_name.find(string_filter) == std::string::npos) {
272 return;
273 }
274
275 is_enabled_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100276 HGraphVisualizerPrinter printer(graph, *output_, "", codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277 printer.StartTag("compilation");
278 printer.PrintProperty("name", pretty_name.c_str());
279 printer.PrintProperty("method", pretty_name.c_str());
280 printer.PrintTime("date");
281 printer.EndTag("compilation");
282}
283
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100284HGraphVisualizer::HGraphVisualizer(std::ostream* output,
285 HGraph* graph,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100286 const CodeGenerator& codegen,
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100287 const char* name)
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100288 : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100289 if (output == nullptr) {
290 return;
291 }
292
293 is_enabled_ = true;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100294 HGraphVisualizerPrinter printer(graph, *output_, "", codegen_);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100295 printer.StartTag("compilation");
296 printer.PrintProperty("name", name);
297 printer.PrintProperty("method", name);
298 printer.PrintTime("date");
299 printer.EndTag("compilation");
300}
301
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100302void HGraphVisualizer::DumpGraph(const char* pass_name) {
303 if (!is_enabled_) {
304 return;
305 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100306 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, codegen_);
307 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100308}
309
310} // namespace art