blob: 9383d31e8f613fd9b7f0316792fe624c324aad9d [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 Geoffray82091da2015-01-26 10:02:45 +000021#include "optimization.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,
Nicolas Geoffray840e5462015-01-07 16:01:24 +000034 bool is_after_pass,
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010035 const CodeGenerator& codegen)
36 : HGraphVisitor(graph),
37 output_(output),
38 pass_name_(pass_name),
Nicolas Geoffray840e5462015-01-07 16:01:24 +000039 is_after_pass_(is_after_pass),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010040 codegen_(codegen),
41 indent_(0) {}
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010042
43 void StartTag(const char* name) {
44 AddIndent();
45 output_ << "begin_" << name << std::endl;
46 indent_++;
47 }
48
49 void EndTag(const char* name) {
50 indent_--;
51 AddIndent();
52 output_ << "end_" << name << std::endl;
53 }
54
55 void PrintProperty(const char* name, const char* property) {
56 AddIndent();
57 output_ << name << " \"" << property << "\"" << std::endl;
58 }
59
60 void PrintProperty(const char* name, const char* property, int id) {
61 AddIndent();
62 output_ << name << " \"" << property << id << "\"" << std::endl;
63 }
64
65 void PrintEmptyProperty(const char* name) {
66 AddIndent();
67 output_ << name << std::endl;
68 }
69
70 void PrintTime(const char* name) {
71 AddIndent();
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -080072 output_ << name << " " << time(nullptr) << std::endl;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010073 }
74
75 void PrintInt(const char* name, int value) {
76 AddIndent();
77 output_ << name << " " << value << std::endl;
78 }
79
80 void AddIndent() {
81 for (size_t i = 0; i < indent_; ++i) {
82 output_ << " ";
83 }
84 }
85
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010086 char GetTypeId(Primitive::Type type) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +010087 // Note that Primitive::Descriptor would not work for us
88 // because it does not handle reference types (that is kPrimNot).
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +010089 switch (type) {
90 case Primitive::kPrimBoolean: return 'z';
91 case Primitive::kPrimByte: return 'b';
92 case Primitive::kPrimChar: return 'c';
93 case Primitive::kPrimShort: return 's';
94 case Primitive::kPrimInt: return 'i';
95 case Primitive::kPrimLong: return 'j';
96 case Primitive::kPrimFloat: return 'f';
97 case Primitive::kPrimDouble: return 'd';
98 case Primitive::kPrimNot: return 'l';
99 case Primitive::kPrimVoid: return 'v';
100 }
101 LOG(FATAL) << "Unreachable";
102 return 'v';
103 }
104
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100105 void PrintPredecessors(HBasicBlock* block) {
106 AddIndent();
107 output_ << "predecessors";
108 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
109 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
110 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
111 }
112 output_<< std::endl;
113 }
114
115 void PrintSuccessors(HBasicBlock* block) {
116 AddIndent();
117 output_ << "successors";
118 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
119 HBasicBlock* successor = block->GetSuccessors().Get(i);
120 output_ << " \"B" << successor->GetBlockId() << "\" ";
121 }
122 output_<< std::endl;
123 }
124
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100125 void DumpLocation(Location location) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100126 if (location.IsRegister()) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100127 codegen_.DumpCoreRegister(output_, location.reg());
128 } else if (location.IsFpuRegister()) {
129 codegen_.DumpFloatingPointRegister(output_, location.reg());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100130 } else if (location.IsConstant()) {
131 output_ << "constant";
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100132 HConstant* constant = location.GetConstant();
133 if (constant->IsIntConstant()) {
134 output_ << " " << constant->AsIntConstant()->GetValue();
135 } else if (constant->IsLongConstant()) {
136 output_ << " " << constant->AsLongConstant()->GetValue();
137 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100138 } else if (location.IsInvalid()) {
139 output_ << "invalid";
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100140 } else if (location.IsStackSlot()) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100141 output_ << location.GetStackIndex() << "(sp)";
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000142 } else if (location.IsFpuRegisterPair()) {
143 codegen_.DumpFloatingPointRegister(output_, location.low());
144 output_ << " and ";
145 codegen_.DumpFloatingPointRegister(output_, location.high());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000146 } else if (location.IsRegisterPair()) {
147 codegen_.DumpCoreRegister(output_, location.low());
148 output_ << " and ";
149 codegen_.DumpCoreRegister(output_, location.high());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100150 } else {
151 DCHECK(location.IsDoubleStackSlot());
152 output_ << "2x" << location.GetStackIndex() << "(sp)";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100153 }
154 }
155
David Brazdilb7e4a062014-12-29 15:35:02 +0000156 void VisitParallelMove(HParallelMove* instruction) OVERRIDE {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100157 output_ << " (";
158 for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) {
159 MoveOperands* move = instruction->MoveOperandsAt(i);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100160 DumpLocation(move->GetSource());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100161 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100162 DumpLocation(move->GetDestination());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100163 if (i + 1 != e) {
164 output_ << ", ";
165 }
166 }
167 output_ << ")";
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100168 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100169 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100170
David Brazdil36cf0952015-01-08 19:28:33 +0000171 void VisitIntConstant(HIntConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000172 output_ << " " << instruction->GetValue();
173 }
174
David Brazdil36cf0952015-01-08 19:28:33 +0000175 void VisitLongConstant(HLongConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000176 output_ << " " << instruction->GetValue();
177 }
178
David Brazdil36cf0952015-01-08 19:28:33 +0000179 void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000180 output_ << " " << instruction->GetValue();
181 }
182
David Brazdil36cf0952015-01-08 19:28:33 +0000183 void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE {
David Brazdilb7e4a062014-12-29 15:35:02 +0000184 output_ << " " << instruction->GetValue();
185 }
186
187 void PrintInstruction(HInstruction* instruction) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100188 output_ << instruction->DebugName();
David Brazdilb7e4a062014-12-29 15:35:02 +0000189 instruction->Accept(this);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100190 if (instruction->InputCount() > 0) {
191 output_ << " [ ";
192 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100193 output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100194 }
195 output_ << "]";
196 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000197 if (pass_name_ == kLivenessPassName
198 && is_after_pass_
199 && instruction->GetLifetimePosition() != kNoLifetime) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100200 output_ << " (liveness: " << instruction->GetLifetimePosition();
201 if (instruction->HasLiveInterval()) {
202 output_ << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100203 const LiveInterval& interval = *instruction->GetLiveInterval();
204 interval.Dump(output_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100205 }
206 output_ << ")";
David Brazdil5e8b1372015-01-23 14:39:08 +0000207 } else if (pass_name_ == kRegisterAllocatorPassName && is_after_pass_) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100208 LocationSummary* locations = instruction->GetLocations();
209 if (locations != nullptr) {
210 output_ << " ( ";
211 for (size_t i = 0; i < instruction->InputCount(); ++i) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100212 DumpLocation(locations->InAt(i));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100213 output_ << " ";
214 }
215 output_ << ")";
216 if (locations->Out().IsValid()) {
217 output_ << " -> ";
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100218 DumpLocation(locations->Out());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100219 }
220 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100221 output_ << " (liveness: " << instruction->GetLifetimePosition() << ")";
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000222 } else if (pass_name_ == kLoopInvariantCodeMotionPassName) {
223 output_ << " ( loop_header:";
224 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
225 if (info == nullptr) {
226 output_ << "null )";
227 } else {
228 output_ << "B" << info->GetHeader()->GetBlockId() << " )";
229 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100230 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100231 }
232
233 void PrintInstructions(const HInstructionList& list) {
234 const char* kEndInstructionMarker = "<|@";
235 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
236 HInstruction* instruction = it.Current();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100237 int bci = 0;
David Brazdilea55b932015-01-27 17:12:29 +0000238 size_t num_uses = 0;
239 for (HUseIterator<HInstruction*> use_it(instruction->GetUses());
240 !use_it.Done();
241 use_it.Advance()) {
242 ++num_uses;
243 }
244 AddIndent();
245 output_ << bci << " " << num_uses << " "
246 << GetTypeId(instruction->GetType()) << instruction->GetId() << " ";
David Brazdilb7e4a062014-12-29 15:35:02 +0000247 PrintInstruction(instruction);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100248 output_ << kEndInstructionMarker << std::endl;
249 }
250 }
251
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100252 void Run() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100253 StartTag("cfg");
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000254 std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)");
255 PrintProperty("name", pass_desc.c_str());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100256 VisitInsertionOrder();
257 EndTag("cfg");
258 }
259
David Brazdilb7e4a062014-12-29 15:35:02 +0000260 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100261 StartTag("block");
262 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100263 if (block->GetLifetimeStart() != kNoLifetime) {
264 // Piggy back on these fields to show the lifetime of the block.
265 PrintInt("from_bci", block->GetLifetimeStart());
266 PrintInt("to_bci", block->GetLifetimeEnd());
267 } else {
268 PrintInt("from_bci", -1);
269 PrintInt("to_bci", -1);
270 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100271 PrintPredecessors(block);
272 PrintSuccessors(block);
273 PrintEmptyProperty("xhandlers");
274 PrintEmptyProperty("flags");
275 if (block->GetDominator() != nullptr) {
276 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
277 }
278
279 StartTag("states");
280 StartTag("locals");
281 PrintInt("size", 0);
282 PrintProperty("method", "None");
283 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
284 AddIndent();
285 HInstruction* instruction = it.Current();
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100286 output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType())
287 << instruction->GetId() << "[ ";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100288 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
289 output_ << inputs.Current()->GetId() << " ";
290 }
291 output_ << "]" << std::endl;
292 }
293 EndTag("locals");
294 EndTag("states");
295
296 StartTag("HIR");
297 PrintInstructions(block->GetPhis());
298 PrintInstructions(block->GetInstructions());
299 EndTag("HIR");
300 EndTag("block");
301 }
302
303 private:
304 std::ostream& output_;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100305 const char* pass_name_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000306 const bool is_after_pass_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100307 const CodeGenerator& codegen_;
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100308 size_t indent_;
309
310 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
311};
312
313HGraphVisualizer::HGraphVisualizer(std::ostream* output,
314 HGraph* graph,
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100315 const CodeGenerator& codegen,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000316 const char* method_name)
David Brazdil5e8b1372015-01-23 14:39:08 +0000317 : output_(output), graph_(graph), codegen_(codegen) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100318 if (output == nullptr) {
319 return;
320 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100321
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000322 HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100323 printer.StartTag("compilation");
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000324 printer.PrintProperty("name", method_name);
325 printer.PrintProperty("method", method_name);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100326 printer.PrintTime("date");
327 printer.EndTag("compilation");
328}
329
David Brazdilee690a32014-12-01 17:04:16 +0000330void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
David Brazdil5e8b1372015-01-23 14:39:08 +0000331 DCHECK(output_ != nullptr);
332 if (!graph_->GetBlocks().IsEmpty()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000333 HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_);
David Brazdilee690a32014-12-01 17:04:16 +0000334 printer.Run();
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100335 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100336}
337
338} // namespace art