blob: 52e3e374b6836c4182f81c5742595b4a1a8db730 [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
19#include "driver/dex_compilation_unit.h"
20#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:
30 HGraphVisualizerPrinter(HGraph* graph, std::ostream& output)
31 : HGraphVisitor(graph), output_(output), indent_(0) {}
32
33 void StartTag(const char* name) {
34 AddIndent();
35 output_ << "begin_" << name << std::endl;
36 indent_++;
37 }
38
39 void EndTag(const char* name) {
40 indent_--;
41 AddIndent();
42 output_ << "end_" << name << std::endl;
43 }
44
45 void PrintProperty(const char* name, const char* property) {
46 AddIndent();
47 output_ << name << " \"" << property << "\"" << std::endl;
48 }
49
50 void PrintProperty(const char* name, const char* property, int id) {
51 AddIndent();
52 output_ << name << " \"" << property << id << "\"" << std::endl;
53 }
54
55 void PrintEmptyProperty(const char* name) {
56 AddIndent();
57 output_ << name << std::endl;
58 }
59
60 void PrintTime(const char* name) {
61 AddIndent();
62 output_ << name << " " << time(NULL) << std::endl;
63 }
64
65 void PrintInt(const char* name, int value) {
66 AddIndent();
67 output_ << name << " " << value << std::endl;
68 }
69
70 void AddIndent() {
71 for (size_t i = 0; i < indent_; ++i) {
72 output_ << " ";
73 }
74 }
75
76 void PrintPredecessors(HBasicBlock* block) {
77 AddIndent();
78 output_ << "predecessors";
79 for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) {
80 HBasicBlock* predecessor = block->GetPredecessors().Get(i);
81 output_ << " \"B" << predecessor->GetBlockId() << "\" ";
82 }
83 output_<< std::endl;
84 }
85
86 void PrintSuccessors(HBasicBlock* block) {
87 AddIndent();
88 output_ << "successors";
89 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
90 HBasicBlock* successor = block->GetSuccessors().Get(i);
91 output_ << " \"B" << successor->GetBlockId() << "\" ";
92 }
93 output_<< std::endl;
94 }
95
96
97 void VisitInstruction(HInstruction* instruction) {
98 output_ << instruction->DebugName();
99 if (instruction->InputCount() > 0) {
100 output_ << " [ ";
101 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
102 output_ << "v" << inputs.Current()->GetId() << " ";
103 }
104 output_ << "]";
105 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100106 if (instruction->GetLifetimePosition() != kNoLifetime) {
107 output_ << " (liveness: " << instruction->GetLifetimePosition();
108 if (instruction->HasLiveInterval()) {
109 output_ << " ";
110 const GrowableArray<LiveRange>& ranges = instruction->GetLiveInterval()->GetRanges();
111 size_t i = ranges.Size() - 1;
112 do {
113 output_ << "[" << ranges.Get(i).GetStart() << "," << ranges.Get(i).GetEnd() << "[";
114 if (i == 0) {
115 break;
116 } else {
117 --i;
118 output_ << ",";
119 }
120 } while (true);
121 }
122 output_ << ")";
123 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100124 }
125
126 void PrintInstructions(const HInstructionList& list) {
127 const char* kEndInstructionMarker = "<|@";
128 for (HInstructionIterator it(list); !it.Done(); it.Advance()) {
129 HInstruction* instruction = it.Current();
130 AddIndent();
131 int bci = 0;
132 output_ << bci << " " << instruction->NumberOfUses() << " v" << instruction->GetId() << " ";
133 instruction->Accept(this);
134 output_ << kEndInstructionMarker << std::endl;
135 }
136 }
137
138 void Run(const char* pass_name) {
139 StartTag("cfg");
140 PrintProperty("name", pass_name);
141 VisitInsertionOrder();
142 EndTag("cfg");
143 }
144
145 void VisitBasicBlock(HBasicBlock* block) {
146 StartTag("block");
147 PrintProperty("name", "B", block->GetBlockId());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100148 if (block->GetLifetimeStart() != kNoLifetime) {
149 // Piggy back on these fields to show the lifetime of the block.
150 PrintInt("from_bci", block->GetLifetimeStart());
151 PrintInt("to_bci", block->GetLifetimeEnd());
152 } else {
153 PrintInt("from_bci", -1);
154 PrintInt("to_bci", -1);
155 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100156 PrintPredecessors(block);
157 PrintSuccessors(block);
158 PrintEmptyProperty("xhandlers");
159 PrintEmptyProperty("flags");
160 if (block->GetDominator() != nullptr) {
161 PrintProperty("dominator", "B", block->GetDominator()->GetBlockId());
162 }
163
164 StartTag("states");
165 StartTag("locals");
166 PrintInt("size", 0);
167 PrintProperty("method", "None");
168 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
169 AddIndent();
170 HInstruction* instruction = it.Current();
171 output_ << instruction->GetId() << " v" << instruction->GetId() << "[ ";
172 for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) {
173 output_ << inputs.Current()->GetId() << " ";
174 }
175 output_ << "]" << std::endl;
176 }
177 EndTag("locals");
178 EndTag("states");
179
180 StartTag("HIR");
181 PrintInstructions(block->GetPhis());
182 PrintInstructions(block->GetInstructions());
183 EndTag("HIR");
184 EndTag("block");
185 }
186
187 private:
188 std::ostream& output_;
189 size_t indent_;
190
191 DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter);
192};
193
194HGraphVisualizer::HGraphVisualizer(std::ostream* output,
195 HGraph* graph,
196 const char* string_filter,
197 const DexCompilationUnit& cu)
198 : output_(output), graph_(graph), is_enabled_(false) {
199 if (output == nullptr) {
200 return;
201 }
202 std::string pretty_name = PrettyMethod(cu.GetDexMethodIndex(), *cu.GetDexFile());
203 if (pretty_name.find(string_filter) == std::string::npos) {
204 return;
205 }
206
207 is_enabled_ = true;
208 HGraphVisualizerPrinter printer(graph, *output_);
209 printer.StartTag("compilation");
210 printer.PrintProperty("name", pretty_name.c_str());
211 printer.PrintProperty("method", pretty_name.c_str());
212 printer.PrintTime("date");
213 printer.EndTag("compilation");
214}
215
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100216HGraphVisualizer::HGraphVisualizer(std::ostream* output,
217 HGraph* graph,
218 const char* name)
219 : output_(output), graph_(graph), is_enabled_(false) {
220 if (output == nullptr) {
221 return;
222 }
223
224 is_enabled_ = true;
225 HGraphVisualizerPrinter printer(graph, *output_);
226 printer.StartTag("compilation");
227 printer.PrintProperty("name", name);
228 printer.PrintProperty("method", name);
229 printer.PrintTime("date");
230 printer.EndTag("compilation");
231}
232
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100233void HGraphVisualizer::DumpGraph(const char* pass_name) {
234 if (!is_enabled_) {
235 return;
236 }
237 HGraphVisualizerPrinter printer(graph_, *output_);
238 printer.Run(pass_name);
239}
240
241} // namespace art