Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 19 | #include "code_generator.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 20 | #include "driver/dex_compilation_unit.h" |
| 21 | #include "nodes.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 22 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | /** |
| 27 | * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra. |
| 28 | */ |
| 29 | class HGraphVisualizerPrinter : public HGraphVisitor { |
| 30 | public: |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 31 | 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 Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 40 | |
| 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 Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 84 | char GetTypeId(Primitive::Type type) { |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 85 | // Note that Primitive::Descriptor would not work for us |
| 86 | // because it does not handle reference types (that is kPrimNot). |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 87 | switch (type) { |
| 88 | case Primitive::kPrimBoolean: return 'z'; |
| 89 | case Primitive::kPrimByte: return 'b'; |
| 90 | case Primitive::kPrimChar: return 'c'; |
| 91 | case Primitive::kPrimShort: return 's'; |
| 92 | case Primitive::kPrimInt: return 'i'; |
| 93 | case Primitive::kPrimLong: return 'j'; |
| 94 | case Primitive::kPrimFloat: return 'f'; |
| 95 | case Primitive::kPrimDouble: return 'd'; |
| 96 | case Primitive::kPrimNot: return 'l'; |
| 97 | case Primitive::kPrimVoid: return 'v'; |
| 98 | } |
| 99 | LOG(FATAL) << "Unreachable"; |
| 100 | return 'v'; |
| 101 | } |
| 102 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 103 | void PrintPredecessors(HBasicBlock* block) { |
| 104 | AddIndent(); |
| 105 | output_ << "predecessors"; |
| 106 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 107 | HBasicBlock* predecessor = block->GetPredecessors().Get(i); |
| 108 | output_ << " \"B" << predecessor->GetBlockId() << "\" "; |
| 109 | } |
| 110 | output_<< std::endl; |
| 111 | } |
| 112 | |
| 113 | void PrintSuccessors(HBasicBlock* block) { |
| 114 | AddIndent(); |
| 115 | output_ << "successors"; |
| 116 | for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) { |
| 117 | HBasicBlock* successor = block->GetSuccessors().Get(i); |
| 118 | output_ << " \"B" << successor->GetBlockId() << "\" "; |
| 119 | } |
| 120 | output_<< std::endl; |
| 121 | } |
| 122 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 123 | void DumpLocation(Location location) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 124 | if (location.IsRegister()) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 125 | codegen_.DumpCoreRegister(output_, location.reg()); |
| 126 | } else if (location.IsFpuRegister()) { |
| 127 | codegen_.DumpFloatingPointRegister(output_, location.reg()); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 128 | } else if (location.IsConstant()) { |
| 129 | output_ << "constant"; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 130 | HConstant* constant = location.GetConstant(); |
| 131 | if (constant->IsIntConstant()) { |
| 132 | output_ << " " << constant->AsIntConstant()->GetValue(); |
| 133 | } else if (constant->IsLongConstant()) { |
| 134 | output_ << " " << constant->AsLongConstant()->GetValue(); |
| 135 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 136 | } else if (location.IsInvalid()) { |
| 137 | output_ << "invalid"; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 138 | } else if (location.IsStackSlot()) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 139 | output_ << location.GetStackIndex() << "(sp)"; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 140 | } else { |
| 141 | DCHECK(location.IsDoubleStackSlot()); |
| 142 | output_ << "2x" << location.GetStackIndex() << "(sp)"; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | void VisitParallelMove(HParallelMove* instruction) { |
| 147 | output_ << instruction->DebugName(); |
| 148 | output_ << " ("; |
| 149 | for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) { |
| 150 | MoveOperands* move = instruction->MoveOperandsAt(i); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 151 | DumpLocation(move->GetSource()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 152 | output_ << " -> "; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 153 | DumpLocation(move->GetDestination()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 154 | if (i + 1 != e) { |
| 155 | output_ << ", "; |
| 156 | } |
| 157 | } |
| 158 | output_ << ")"; |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 159 | output_ << " (liveness: " << instruction->GetLifetimePosition() << ")"; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 160 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 161 | |
| 162 | void VisitInstruction(HInstruction* instruction) { |
| 163 | output_ << instruction->DebugName(); |
| 164 | if (instruction->InputCount() > 0) { |
| 165 | output_ << " [ "; |
| 166 | for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) { |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 167 | output_ << GetTypeId(inputs.Current()->GetType()) << inputs.Current()->GetId() << " "; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 168 | } |
| 169 | output_ << "]"; |
| 170 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 171 | if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 172 | output_ << " (liveness: " << instruction->GetLifetimePosition(); |
| 173 | if (instruction->HasLiveInterval()) { |
| 174 | output_ << " "; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 175 | const LiveInterval& interval = *instruction->GetLiveInterval(); |
| 176 | interval.Dump(output_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 177 | } |
| 178 | output_ << ")"; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 179 | } else if (pass_name_ == kRegisterAllocatorPassName) { |
| 180 | LocationSummary* locations = instruction->GetLocations(); |
| 181 | if (locations != nullptr) { |
| 182 | output_ << " ( "; |
| 183 | for (size_t i = 0; i < instruction->InputCount(); ++i) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 184 | DumpLocation(locations->InAt(i)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 185 | output_ << " "; |
| 186 | } |
| 187 | output_ << ")"; |
| 188 | if (locations->Out().IsValid()) { |
| 189 | output_ << " -> "; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 190 | DumpLocation(locations->Out()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 193 | output_ << " (liveness: " << instruction->GetLifetimePosition() << ")"; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 194 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void PrintInstructions(const HInstructionList& list) { |
| 198 | const char* kEndInstructionMarker = "<|@"; |
| 199 | for (HInstructionIterator it(list); !it.Done(); it.Advance()) { |
| 200 | HInstruction* instruction = it.Current(); |
| 201 | AddIndent(); |
| 202 | int bci = 0; |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 203 | output_ << bci << " " << instruction->NumberOfUses() |
| 204 | << " " << GetTypeId(instruction->GetType()) << instruction->GetId() << " "; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 205 | instruction->Accept(this); |
| 206 | output_ << kEndInstructionMarker << std::endl; |
| 207 | } |
| 208 | } |
| 209 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 210 | void Run() { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 211 | StartTag("cfg"); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 212 | PrintProperty("name", pass_name_); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 213 | VisitInsertionOrder(); |
| 214 | EndTag("cfg"); |
| 215 | } |
| 216 | |
| 217 | void VisitBasicBlock(HBasicBlock* block) { |
| 218 | StartTag("block"); |
| 219 | PrintProperty("name", "B", block->GetBlockId()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 220 | if (block->GetLifetimeStart() != kNoLifetime) { |
| 221 | // Piggy back on these fields to show the lifetime of the block. |
| 222 | PrintInt("from_bci", block->GetLifetimeStart()); |
| 223 | PrintInt("to_bci", block->GetLifetimeEnd()); |
| 224 | } else { |
| 225 | PrintInt("from_bci", -1); |
| 226 | PrintInt("to_bci", -1); |
| 227 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 228 | PrintPredecessors(block); |
| 229 | PrintSuccessors(block); |
| 230 | PrintEmptyProperty("xhandlers"); |
| 231 | PrintEmptyProperty("flags"); |
| 232 | if (block->GetDominator() != nullptr) { |
| 233 | PrintProperty("dominator", "B", block->GetDominator()->GetBlockId()); |
| 234 | } |
| 235 | |
| 236 | StartTag("states"); |
| 237 | StartTag("locals"); |
| 238 | PrintInt("size", 0); |
| 239 | PrintProperty("method", "None"); |
| 240 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 241 | AddIndent(); |
| 242 | HInstruction* instruction = it.Current(); |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 243 | output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType()) |
| 244 | << instruction->GetId() << "[ "; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 245 | for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) { |
| 246 | output_ << inputs.Current()->GetId() << " "; |
| 247 | } |
| 248 | output_ << "]" << std::endl; |
| 249 | } |
| 250 | EndTag("locals"); |
| 251 | EndTag("states"); |
| 252 | |
| 253 | StartTag("HIR"); |
| 254 | PrintInstructions(block->GetPhis()); |
| 255 | PrintInstructions(block->GetInstructions()); |
| 256 | EndTag("HIR"); |
| 257 | EndTag("block"); |
| 258 | } |
| 259 | |
| 260 | private: |
| 261 | std::ostream& output_; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 262 | const char* pass_name_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 263 | const CodeGenerator& codegen_; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 264 | size_t indent_; |
| 265 | |
| 266 | DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter); |
| 267 | }; |
| 268 | |
| 269 | HGraphVisualizer::HGraphVisualizer(std::ostream* output, |
| 270 | HGraph* graph, |
| 271 | const char* string_filter, |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 272 | const CodeGenerator& codegen, |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 273 | const DexCompilationUnit& cu) |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 274 | : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 275 | if (output == nullptr) { |
| 276 | return; |
| 277 | } |
| 278 | std::string pretty_name = PrettyMethod(cu.GetDexMethodIndex(), *cu.GetDexFile()); |
| 279 | if (pretty_name.find(string_filter) == std::string::npos) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | is_enabled_ = true; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 284 | HGraphVisualizerPrinter printer(graph, *output_, "", codegen_); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 285 | printer.StartTag("compilation"); |
| 286 | printer.PrintProperty("name", pretty_name.c_str()); |
| 287 | printer.PrintProperty("method", pretty_name.c_str()); |
| 288 | printer.PrintTime("date"); |
| 289 | printer.EndTag("compilation"); |
| 290 | } |
| 291 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 292 | HGraphVisualizer::HGraphVisualizer(std::ostream* output, |
| 293 | HGraph* graph, |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 294 | const CodeGenerator& codegen, |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 295 | const char* name) |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 296 | : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 297 | if (output == nullptr) { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | is_enabled_ = true; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 302 | HGraphVisualizerPrinter printer(graph, *output_, "", codegen_); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 303 | printer.StartTag("compilation"); |
| 304 | printer.PrintProperty("name", name); |
| 305 | printer.PrintProperty("method", name); |
| 306 | printer.PrintTime("date"); |
| 307 | printer.EndTag("compilation"); |
| 308 | } |
| 309 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 310 | void HGraphVisualizer::DumpGraph(const char* pass_name) const { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 311 | if (!is_enabled_) { |
| 312 | return; |
| 313 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 314 | HGraphVisualizerPrinter printer(graph_, *output_, pass_name, codegen_); |
| 315 | printer.Run(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | } // namespace art |