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" |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 20 | #include "dead_code_elimination.h" |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 21 | #include "licm.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 22 | #include "nodes.h" |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 23 | #include "optimization.h" |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 24 | #include "register_allocator.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 25 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 26 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 27 | #include <cctype> |
| 28 | #include <sstream> |
| 29 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 30 | namespace art { |
| 31 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 32 | static bool HasWhitespace(const char* str) { |
| 33 | DCHECK(str != nullptr); |
| 34 | while (str[0] != 0) { |
| 35 | if (isspace(str[0])) { |
| 36 | return true; |
| 37 | } |
| 38 | str++; |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | class StringList { |
| 44 | public: |
| 45 | // Create an empty list |
| 46 | StringList() : is_empty_(true) {} |
| 47 | |
| 48 | // Construct StringList from a linked list. List element class T |
| 49 | // must provide methods `GetNext` and `Dump`. |
| 50 | template<class T> |
| 51 | StringList(T* first_entry) |
| 52 | : StringList() { |
| 53 | for (T* current = first_entry; current != nullptr; current = current->GetNext()) { |
| 54 | current->Dump(NewEntryStream()); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | std::ostream& NewEntryStream() { |
| 59 | if (is_empty_) { |
| 60 | is_empty_ = false; |
| 61 | } else { |
| 62 | sstream_ << " "; |
| 63 | } |
| 64 | return sstream_; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | bool is_empty_; |
| 69 | std::ostringstream sstream_; |
| 70 | |
| 71 | friend std::ostream& operator<<(std::ostream& os, const StringList& list); |
| 72 | }; |
| 73 | |
| 74 | std::ostream& operator<<(std::ostream& os, const StringList& list) { |
| 75 | return os << "[ " << list.sstream_.str() << " ]"; |
| 76 | } |
| 77 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 78 | /** |
| 79 | * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra. |
| 80 | */ |
| 81 | class HGraphVisualizerPrinter : public HGraphVisitor { |
| 82 | public: |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 83 | HGraphVisualizerPrinter(HGraph* graph, |
| 84 | std::ostream& output, |
| 85 | const char* pass_name, |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 86 | bool is_after_pass, |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 87 | const CodeGenerator& codegen) |
| 88 | : HGraphVisitor(graph), |
| 89 | output_(output), |
| 90 | pass_name_(pass_name), |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 91 | is_after_pass_(is_after_pass), |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 92 | codegen_(codegen), |
| 93 | indent_(0) {} |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 94 | |
| 95 | void StartTag(const char* name) { |
| 96 | AddIndent(); |
| 97 | output_ << "begin_" << name << std::endl; |
| 98 | indent_++; |
| 99 | } |
| 100 | |
| 101 | void EndTag(const char* name) { |
| 102 | indent_--; |
| 103 | AddIndent(); |
| 104 | output_ << "end_" << name << std::endl; |
| 105 | } |
| 106 | |
| 107 | void PrintProperty(const char* name, const char* property) { |
| 108 | AddIndent(); |
| 109 | output_ << name << " \"" << property << "\"" << std::endl; |
| 110 | } |
| 111 | |
| 112 | void PrintProperty(const char* name, const char* property, int id) { |
| 113 | AddIndent(); |
| 114 | output_ << name << " \"" << property << id << "\"" << std::endl; |
| 115 | } |
| 116 | |
| 117 | void PrintEmptyProperty(const char* name) { |
| 118 | AddIndent(); |
| 119 | output_ << name << std::endl; |
| 120 | } |
| 121 | |
| 122 | void PrintTime(const char* name) { |
| 123 | AddIndent(); |
Jean Christophe Beyler | 0ada95d | 2014-12-04 11:20:20 -0800 | [diff] [blame] | 124 | output_ << name << " " << time(nullptr) << std::endl; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void PrintInt(const char* name, int value) { |
| 128 | AddIndent(); |
| 129 | output_ << name << " " << value << std::endl; |
| 130 | } |
| 131 | |
| 132 | void AddIndent() { |
| 133 | for (size_t i = 0; i < indent_; ++i) { |
| 134 | output_ << " "; |
| 135 | } |
| 136 | } |
| 137 | |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 138 | char GetTypeId(Primitive::Type type) { |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 139 | // Note that Primitive::Descriptor would not work for us |
| 140 | // because it does not handle reference types (that is kPrimNot). |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 141 | switch (type) { |
| 142 | case Primitive::kPrimBoolean: return 'z'; |
| 143 | case Primitive::kPrimByte: return 'b'; |
| 144 | case Primitive::kPrimChar: return 'c'; |
| 145 | case Primitive::kPrimShort: return 's'; |
| 146 | case Primitive::kPrimInt: return 'i'; |
| 147 | case Primitive::kPrimLong: return 'j'; |
| 148 | case Primitive::kPrimFloat: return 'f'; |
| 149 | case Primitive::kPrimDouble: return 'd'; |
| 150 | case Primitive::kPrimNot: return 'l'; |
| 151 | case Primitive::kPrimVoid: return 'v'; |
| 152 | } |
| 153 | LOG(FATAL) << "Unreachable"; |
| 154 | return 'v'; |
| 155 | } |
| 156 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 157 | void PrintPredecessors(HBasicBlock* block) { |
| 158 | AddIndent(); |
| 159 | output_ << "predecessors"; |
| 160 | for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { |
| 161 | HBasicBlock* predecessor = block->GetPredecessors().Get(i); |
| 162 | output_ << " \"B" << predecessor->GetBlockId() << "\" "; |
| 163 | } |
| 164 | output_<< std::endl; |
| 165 | } |
| 166 | |
| 167 | void PrintSuccessors(HBasicBlock* block) { |
| 168 | AddIndent(); |
| 169 | output_ << "successors"; |
| 170 | for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) { |
| 171 | HBasicBlock* successor = block->GetSuccessors().Get(i); |
| 172 | output_ << " \"B" << successor->GetBlockId() << "\" "; |
| 173 | } |
| 174 | output_<< std::endl; |
| 175 | } |
| 176 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 177 | void DumpLocation(std::ostream& stream, const Location& location) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 178 | if (location.IsRegister()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 179 | codegen_.DumpCoreRegister(stream, location.reg()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 180 | } else if (location.IsFpuRegister()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 181 | codegen_.DumpFloatingPointRegister(stream, location.reg()); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 182 | } else if (location.IsConstant()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 183 | stream << "#"; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 184 | HConstant* constant = location.GetConstant(); |
| 185 | if (constant->IsIntConstant()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 186 | stream << constant->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 187 | } else if (constant->IsLongConstant()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 188 | stream << constant->AsLongConstant()->GetValue(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 189 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 190 | } else if (location.IsInvalid()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 191 | stream << "invalid"; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 192 | } else if (location.IsStackSlot()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 193 | stream << location.GetStackIndex() << "(sp)"; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 194 | } else if (location.IsFpuRegisterPair()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 195 | codegen_.DumpFloatingPointRegister(stream, location.low()); |
| 196 | stream << "|"; |
| 197 | codegen_.DumpFloatingPointRegister(stream, location.high()); |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 198 | } else if (location.IsRegisterPair()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 199 | codegen_.DumpCoreRegister(stream, location.low()); |
| 200 | stream << "|"; |
| 201 | codegen_.DumpCoreRegister(stream, location.high()); |
Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 202 | } else if (location.IsUnallocated()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 203 | stream << "unallocated"; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 204 | } else { |
| 205 | DCHECK(location.IsDoubleStackSlot()); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 206 | stream << "2x" << location.GetStackIndex() << "(sp)"; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 210 | std::ostream& StartAttributeStream(const char* name = nullptr) { |
| 211 | if (name == nullptr) { |
| 212 | output_ << " "; |
| 213 | } else { |
| 214 | DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes"; |
| 215 | output_ << " " << name << ":"; |
| 216 | } |
| 217 | return output_; |
| 218 | } |
| 219 | |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 220 | void VisitParallelMove(HParallelMove* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 221 | StartAttributeStream("liveness") << instruction->GetLifetimePosition(); |
| 222 | StringList moves; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 223 | for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) { |
| 224 | MoveOperands* move = instruction->MoveOperandsAt(i); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 225 | std::ostream& str = moves.NewEntryStream(); |
| 226 | DumpLocation(str, move->GetSource()); |
| 227 | str << "->"; |
| 228 | DumpLocation(str, move->GetDestination()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 229 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 230 | StartAttributeStream("moves") << moves; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 231 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 232 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 233 | void VisitIntConstant(HIntConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 234 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 235 | } |
| 236 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 237 | void VisitLongConstant(HLongConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 238 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 239 | } |
| 240 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 241 | void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 242 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 243 | } |
| 244 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 245 | void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 246 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 249 | void VisitPhi(HPhi* phi) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 250 | StartAttributeStream("reg") << phi->GetRegNumber(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 253 | void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 254 | StartAttributeStream("kind") << barrier->GetBarrierKind(); |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 257 | bool IsPass(const char* name) { |
| 258 | return strcmp(pass_name_, name) == 0; |
| 259 | } |
| 260 | |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 261 | void PrintInstruction(HInstruction* instruction) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 262 | output_ << instruction->DebugName(); |
| 263 | if (instruction->InputCount() > 0) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 264 | StringList inputs; |
| 265 | for (HInputIterator it(instruction); !it.Done(); it.Advance()) { |
| 266 | inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 267 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 268 | StartAttributeStream() << inputs; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 269 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 270 | instruction->Accept(this); |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 271 | if (instruction->HasEnvironment()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 272 | StringList envs; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 273 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 274 | environment != nullptr; |
| 275 | environment = environment->GetParent()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 276 | StringList vregs; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 277 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 278 | HInstruction* insn = environment->GetInstructionAt(i); |
| 279 | if (insn != nullptr) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 280 | vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId(); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 281 | } else { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 282 | vregs.NewEntryStream() << "_"; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 283 | } |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 284 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 285 | envs.NewEntryStream() << vregs; |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 286 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 287 | StartAttributeStream("env") << envs; |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 288 | } |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 289 | if (IsPass(SsaLivenessAnalysis::kLivenessPassName) |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 290 | && is_after_pass_ |
| 291 | && instruction->GetLifetimePosition() != kNoLifetime) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 292 | StartAttributeStream("liveness") << instruction->GetLifetimePosition(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 293 | if (instruction->HasLiveInterval()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 294 | LiveInterval* interval = instruction->GetLiveInterval(); |
| 295 | StartAttributeStream("ranges") << StringList(interval->GetFirstRange()); |
| 296 | StartAttributeStream("uses") << StringList(interval->GetFirstUse()); |
| 297 | StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse()); |
| 298 | StartAttributeStream("is_fixed") << interval->IsFixed(); |
| 299 | StartAttributeStream("is_split") << interval->IsSplit(); |
| 300 | StartAttributeStream("is_low") << interval->IsLowInterval(); |
| 301 | StartAttributeStream("is_high") << interval->IsHighInterval(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 302 | } |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 303 | } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 304 | StartAttributeStream("liveness") << instruction->GetLifetimePosition(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 305 | LocationSummary* locations = instruction->GetLocations(); |
| 306 | if (locations != nullptr) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 307 | StringList inputs; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 308 | for (size_t i = 0; i < instruction->InputCount(); ++i) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 309 | DumpLocation(inputs.NewEntryStream(), locations->InAt(i)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 310 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 311 | std::ostream& attr = StartAttributeStream("locations"); |
| 312 | attr << inputs << "->"; |
| 313 | DumpLocation(attr, locations->Out()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 314 | } |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 315 | } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName) |
| 316 | || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 317 | HLoopInformation* info = instruction->GetBlock()->GetLoopInformation(); |
| 318 | if (info == nullptr) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 319 | StartAttributeStream("loop") << "none"; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 320 | } else { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 321 | StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 322 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 323 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void PrintInstructions(const HInstructionList& list) { |
| 327 | const char* kEndInstructionMarker = "<|@"; |
| 328 | for (HInstructionIterator it(list); !it.Done(); it.Advance()) { |
| 329 | HInstruction* instruction = it.Current(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 330 | int bci = 0; |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 331 | size_t num_uses = 0; |
| 332 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
| 333 | !use_it.Done(); |
| 334 | use_it.Advance()) { |
| 335 | ++num_uses; |
| 336 | } |
| 337 | AddIndent(); |
| 338 | output_ << bci << " " << num_uses << " " |
| 339 | << GetTypeId(instruction->GetType()) << instruction->GetId() << " "; |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 340 | PrintInstruction(instruction); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 341 | output_ << " " << kEndInstructionMarker << std::endl; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 345 | void Run() { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 346 | StartTag("cfg"); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 347 | std::string pass_desc = std::string(pass_name_) + (is_after_pass_ ? " (after)" : " (before)"); |
| 348 | PrintProperty("name", pass_desc.c_str()); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 349 | VisitInsertionOrder(); |
| 350 | EndTag("cfg"); |
| 351 | } |
| 352 | |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 353 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 354 | StartTag("block"); |
| 355 | PrintProperty("name", "B", block->GetBlockId()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 356 | if (block->GetLifetimeStart() != kNoLifetime) { |
| 357 | // Piggy back on these fields to show the lifetime of the block. |
| 358 | PrintInt("from_bci", block->GetLifetimeStart()); |
| 359 | PrintInt("to_bci", block->GetLifetimeEnd()); |
| 360 | } else { |
| 361 | PrintInt("from_bci", -1); |
| 362 | PrintInt("to_bci", -1); |
| 363 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 364 | PrintPredecessors(block); |
| 365 | PrintSuccessors(block); |
| 366 | PrintEmptyProperty("xhandlers"); |
| 367 | PrintEmptyProperty("flags"); |
| 368 | if (block->GetDominator() != nullptr) { |
| 369 | PrintProperty("dominator", "B", block->GetDominator()->GetBlockId()); |
| 370 | } |
| 371 | |
| 372 | StartTag("states"); |
| 373 | StartTag("locals"); |
| 374 | PrintInt("size", 0); |
| 375 | PrintProperty("method", "None"); |
| 376 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 377 | AddIndent(); |
| 378 | HInstruction* instruction = it.Current(); |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 379 | output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType()) |
| 380 | << instruction->GetId() << "[ "; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 381 | for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) { |
| 382 | output_ << inputs.Current()->GetId() << " "; |
| 383 | } |
| 384 | output_ << "]" << std::endl; |
| 385 | } |
| 386 | EndTag("locals"); |
| 387 | EndTag("states"); |
| 388 | |
| 389 | StartTag("HIR"); |
| 390 | PrintInstructions(block->GetPhis()); |
| 391 | PrintInstructions(block->GetInstructions()); |
| 392 | EndTag("HIR"); |
| 393 | EndTag("block"); |
| 394 | } |
| 395 | |
| 396 | private: |
| 397 | std::ostream& output_; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 398 | const char* pass_name_; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 399 | const bool is_after_pass_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 400 | const CodeGenerator& codegen_; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 401 | size_t indent_; |
| 402 | |
| 403 | DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter); |
| 404 | }; |
| 405 | |
| 406 | HGraphVisualizer::HGraphVisualizer(std::ostream* output, |
| 407 | HGraph* graph, |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 408 | const CodeGenerator& codegen) |
| 409 | : output_(output), graph_(graph), codegen_(codegen) {} |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 410 | |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 411 | void HGraphVisualizer::PrintHeader(const char* method_name) const { |
| 412 | DCHECK(output_ != nullptr); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 413 | HGraphVisualizerPrinter printer(graph_, *output_, "", true, codegen_); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 414 | printer.StartTag("compilation"); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 415 | printer.PrintProperty("name", method_name); |
| 416 | printer.PrintProperty("method", method_name); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 417 | printer.PrintTime("date"); |
| 418 | printer.EndTag("compilation"); |
| 419 | } |
| 420 | |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 421 | void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 422 | DCHECK(output_ != nullptr); |
| 423 | if (!graph_->GetBlocks().IsEmpty()) { |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 424 | HGraphVisualizerPrinter printer(graph_, *output_, pass_name, is_after_pass, codegen_); |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 425 | printer.Run(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 426 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | } // namespace art |