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