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