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 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 19 | #include <dlfcn.h> |
| 20 | |
| 21 | #include <cctype> |
| 22 | #include <sstream> |
| 23 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 24 | #include "code_generator.h" |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 25 | #include "dead_code_elimination.h" |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 26 | #include "disassembler.h" |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame^] | 27 | #include "inliner.h" |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 28 | #include "licm.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 29 | #include "nodes.h" |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 30 | #include "optimization.h" |
Nicolas Geoffray | 7cb499b | 2015-06-17 11:35:11 +0100 | [diff] [blame] | 31 | #include "reference_type_propagation.h" |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 32 | #include "register_allocator.h" |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 33 | #include "ssa_liveness_analysis.h" |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 34 | #include "utils/assembler.h" |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 35 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 36 | namespace art { |
| 37 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 38 | static bool HasWhitespace(const char* str) { |
| 39 | DCHECK(str != nullptr); |
| 40 | while (str[0] != 0) { |
| 41 | if (isspace(str[0])) { |
| 42 | return true; |
| 43 | } |
| 44 | str++; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | class StringList { |
| 50 | public: |
David Brazdil | c7a2485 | 2015-05-15 16:44:05 +0100 | [diff] [blame] | 51 | enum Format { |
| 52 | kArrayBrackets, |
| 53 | kSetBrackets, |
| 54 | }; |
| 55 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 56 | // Create an empty list |
David Brazdil | f1a9ff7 | 2015-05-18 16:04:53 +0100 | [diff] [blame] | 57 | explicit StringList(Format format = kArrayBrackets) : format_(format), is_empty_(true) {} |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 58 | |
| 59 | // Construct StringList from a linked list. List element class T |
| 60 | // must provide methods `GetNext` and `Dump`. |
| 61 | template<class T> |
David Brazdil | c7a2485 | 2015-05-15 16:44:05 +0100 | [diff] [blame] | 62 | explicit StringList(T* first_entry, Format format = kArrayBrackets) : StringList(format) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 63 | for (T* current = first_entry; current != nullptr; current = current->GetNext()) { |
| 64 | current->Dump(NewEntryStream()); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | std::ostream& NewEntryStream() { |
| 69 | if (is_empty_) { |
| 70 | is_empty_ = false; |
| 71 | } else { |
David Brazdil | c57397b | 2015-05-15 16:01:59 +0100 | [diff] [blame] | 72 | sstream_ << ","; |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 73 | } |
| 74 | return sstream_; |
| 75 | } |
| 76 | |
| 77 | private: |
David Brazdil | c7a2485 | 2015-05-15 16:44:05 +0100 | [diff] [blame] | 78 | Format format_; |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 79 | bool is_empty_; |
| 80 | std::ostringstream sstream_; |
| 81 | |
| 82 | friend std::ostream& operator<<(std::ostream& os, const StringList& list); |
| 83 | }; |
| 84 | |
| 85 | std::ostream& operator<<(std::ostream& os, const StringList& list) { |
David Brazdil | c7a2485 | 2015-05-15 16:44:05 +0100 | [diff] [blame] | 86 | switch (list.format_) { |
| 87 | case StringList::kArrayBrackets: return os << "[" << list.sstream_.str() << "]"; |
| 88 | case StringList::kSetBrackets: return os << "{" << list.sstream_.str() << "}"; |
| 89 | default: |
| 90 | LOG(FATAL) << "Invalid StringList format"; |
| 91 | UNREACHABLE(); |
| 92 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 95 | typedef Disassembler* create_disasm_prototype(InstructionSet instruction_set, |
| 96 | DisassemblerOptions* options); |
| 97 | class HGraphVisualizerDisassembler { |
| 98 | public: |
| 99 | HGraphVisualizerDisassembler(InstructionSet instruction_set, const uint8_t* base_address) |
David Brazdil | 3a690be | 2015-06-23 10:22:38 +0100 | [diff] [blame] | 100 | : instruction_set_(instruction_set), disassembler_(nullptr) { |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 101 | libart_disassembler_handle_ = |
| 102 | dlopen(kIsDebugBuild ? "libartd-disassembler.so" : "libart-disassembler.so", RTLD_NOW); |
| 103 | if (libart_disassembler_handle_ == nullptr) { |
| 104 | LOG(WARNING) << "Failed to dlopen libart-disassembler: " << dlerror(); |
| 105 | return; |
| 106 | } |
| 107 | create_disasm_prototype* create_disassembler = reinterpret_cast<create_disasm_prototype*>( |
| 108 | dlsym(libart_disassembler_handle_, "create_disassembler")); |
| 109 | if (create_disassembler == nullptr) { |
| 110 | LOG(WARNING) << "Could not find create_disassembler entry: " << dlerror(); |
| 111 | return; |
| 112 | } |
| 113 | // Reading the disassembly from 0x0 is easier, so we print relative |
| 114 | // addresses. We will only disassemble the code once everything has |
| 115 | // been generated, so we can read data in literal pools. |
| 116 | disassembler_ = std::unique_ptr<Disassembler>((*create_disassembler)( |
| 117 | instruction_set, |
| 118 | new DisassemblerOptions(/* absolute_addresses */ false, |
| 119 | base_address, |
| 120 | /* can_read_literals */ true))); |
| 121 | } |
| 122 | |
| 123 | ~HGraphVisualizerDisassembler() { |
| 124 | // We need to call ~Disassembler() before we close the library. |
| 125 | disassembler_.reset(); |
| 126 | if (libart_disassembler_handle_ != nullptr) { |
| 127 | dlclose(libart_disassembler_handle_); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void Disassemble(std::ostream& output, size_t start, size_t end) const { |
David Brazdil | 3a690be | 2015-06-23 10:22:38 +0100 | [diff] [blame] | 132 | if (disassembler_ == nullptr) { |
| 133 | return; |
| 134 | } |
| 135 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 136 | const uint8_t* base = disassembler_->GetDisassemblerOptions()->base_address_; |
| 137 | if (instruction_set_ == kThumb2) { |
| 138 | // ARM and Thumb-2 use the same disassembler. The bottom bit of the |
| 139 | // address is used to distinguish between the two. |
| 140 | base += 1; |
| 141 | } |
| 142 | disassembler_->Dump(output, base + start, base + end); |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | InstructionSet instruction_set_; |
| 147 | std::unique_ptr<Disassembler> disassembler_; |
| 148 | |
| 149 | void* libart_disassembler_handle_; |
| 150 | }; |
| 151 | |
| 152 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 153 | /** |
| 154 | * HGraph visitor to generate a file suitable for the c1visualizer tool and IRHydra. |
| 155 | */ |
Nicolas Geoffray | 842acd4 | 2015-07-01 13:00:15 +0100 | [diff] [blame] | 156 | class HGraphVisualizerPrinter : public HGraphDelegateVisitor { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 157 | public: |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 158 | HGraphVisualizerPrinter(HGraph* graph, |
| 159 | std::ostream& output, |
| 160 | const char* pass_name, |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 161 | bool is_after_pass, |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 162 | bool graph_in_bad_state, |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 163 | const CodeGenerator& codegen, |
| 164 | const DisassemblyInformation* disasm_info = nullptr) |
Nicolas Geoffray | 842acd4 | 2015-07-01 13:00:15 +0100 | [diff] [blame] | 165 | : HGraphDelegateVisitor(graph), |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 166 | output_(output), |
| 167 | pass_name_(pass_name), |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 168 | is_after_pass_(is_after_pass), |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 169 | graph_in_bad_state_(graph_in_bad_state), |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 170 | codegen_(codegen), |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 171 | disasm_info_(disasm_info), |
| 172 | disassembler_(disasm_info_ != nullptr |
| 173 | ? new HGraphVisualizerDisassembler( |
| 174 | codegen_.GetInstructionSet(), |
| 175 | codegen_.GetAssembler().CodeBufferBaseAddress()) |
| 176 | : nullptr), |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 177 | indent_(0) {} |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 178 | |
| 179 | void StartTag(const char* name) { |
| 180 | AddIndent(); |
| 181 | output_ << "begin_" << name << std::endl; |
| 182 | indent_++; |
| 183 | } |
| 184 | |
| 185 | void EndTag(const char* name) { |
| 186 | indent_--; |
| 187 | AddIndent(); |
| 188 | output_ << "end_" << name << std::endl; |
| 189 | } |
| 190 | |
| 191 | void PrintProperty(const char* name, const char* property) { |
| 192 | AddIndent(); |
| 193 | output_ << name << " \"" << property << "\"" << std::endl; |
| 194 | } |
| 195 | |
| 196 | void PrintProperty(const char* name, const char* property, int id) { |
| 197 | AddIndent(); |
| 198 | output_ << name << " \"" << property << id << "\"" << std::endl; |
| 199 | } |
| 200 | |
| 201 | void PrintEmptyProperty(const char* name) { |
| 202 | AddIndent(); |
| 203 | output_ << name << std::endl; |
| 204 | } |
| 205 | |
| 206 | void PrintTime(const char* name) { |
| 207 | AddIndent(); |
Jean Christophe Beyler | 0ada95d | 2014-12-04 11:20:20 -0800 | [diff] [blame] | 208 | output_ << name << " " << time(nullptr) << std::endl; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void PrintInt(const char* name, int value) { |
| 212 | AddIndent(); |
| 213 | output_ << name << " " << value << std::endl; |
| 214 | } |
| 215 | |
| 216 | void AddIndent() { |
| 217 | for (size_t i = 0; i < indent_; ++i) { |
| 218 | output_ << " "; |
| 219 | } |
| 220 | } |
| 221 | |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 222 | char GetTypeId(Primitive::Type type) { |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 223 | // Note that Primitive::Descriptor would not work for us |
| 224 | // because it does not handle reference types (that is kPrimNot). |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 225 | switch (type) { |
| 226 | case Primitive::kPrimBoolean: return 'z'; |
| 227 | case Primitive::kPrimByte: return 'b'; |
| 228 | case Primitive::kPrimChar: return 'c'; |
| 229 | case Primitive::kPrimShort: return 's'; |
| 230 | case Primitive::kPrimInt: return 'i'; |
| 231 | case Primitive::kPrimLong: return 'j'; |
| 232 | case Primitive::kPrimFloat: return 'f'; |
| 233 | case Primitive::kPrimDouble: return 'd'; |
| 234 | case Primitive::kPrimNot: return 'l'; |
| 235 | case Primitive::kPrimVoid: return 'v'; |
| 236 | } |
| 237 | LOG(FATAL) << "Unreachable"; |
| 238 | return 'v'; |
| 239 | } |
| 240 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 241 | void PrintPredecessors(HBasicBlock* block) { |
| 242 | AddIndent(); |
| 243 | output_ << "predecessors"; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 244 | for (HBasicBlock* predecessor : block->GetPredecessors()) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 245 | output_ << " \"B" << predecessor->GetBlockId() << "\" "; |
| 246 | } |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 247 | if (block->IsEntryBlock() && (disasm_info_ != nullptr)) { |
| 248 | output_ << " \"" << kDisassemblyBlockFrameEntry << "\" "; |
| 249 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 250 | output_<< std::endl; |
| 251 | } |
| 252 | |
| 253 | void PrintSuccessors(HBasicBlock* block) { |
| 254 | AddIndent(); |
| 255 | output_ << "successors"; |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 256 | for (HBasicBlock* successor : block->GetNormalSuccessors()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 257 | output_ << " \"B" << successor->GetBlockId() << "\" "; |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 258 | } |
| 259 | output_<< std::endl; |
| 260 | } |
| 261 | |
| 262 | void PrintExceptionHandlers(HBasicBlock* block) { |
| 263 | AddIndent(); |
| 264 | output_ << "xhandlers"; |
David Brazdil | d26a411 | 2015-11-10 11:07:31 +0000 | [diff] [blame] | 265 | for (HBasicBlock* handler : block->GetExceptionalSuccessors()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 266 | output_ << " \"B" << handler->GetBlockId() << "\" "; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 267 | } |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 268 | if (block->IsExitBlock() && |
| 269 | (disasm_info_ != nullptr) && |
| 270 | !disasm_info_->GetSlowPathIntervals().empty()) { |
| 271 | output_ << " \"" << kDisassemblyBlockSlowPaths << "\" "; |
| 272 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 273 | output_<< std::endl; |
| 274 | } |
| 275 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 276 | void DumpLocation(std::ostream& stream, const Location& location) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 277 | if (location.IsRegister()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 278 | codegen_.DumpCoreRegister(stream, location.reg()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 279 | } else if (location.IsFpuRegister()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 280 | codegen_.DumpFloatingPointRegister(stream, location.reg()); |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 281 | } else if (location.IsConstant()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 282 | stream << "#"; |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 283 | HConstant* constant = location.GetConstant(); |
| 284 | if (constant->IsIntConstant()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 285 | stream << constant->AsIntConstant()->GetValue(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 286 | } else if (constant->IsLongConstant()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 287 | stream << constant->AsLongConstant()->GetValue(); |
Nicolas Geoffray | 18efde5 | 2014-09-22 15:51:11 +0100 | [diff] [blame] | 288 | } |
Nicolas Geoffray | 96f89a2 | 2014-07-11 10:57:49 +0100 | [diff] [blame] | 289 | } else if (location.IsInvalid()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 290 | stream << "invalid"; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 291 | } else if (location.IsStackSlot()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 292 | stream << location.GetStackIndex() << "(sp)"; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 293 | } else if (location.IsFpuRegisterPair()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 294 | codegen_.DumpFloatingPointRegister(stream, location.low()); |
| 295 | stream << "|"; |
| 296 | codegen_.DumpFloatingPointRegister(stream, location.high()); |
Nicolas Geoffray | 6c2dff8 | 2015-01-21 14:56:54 +0000 | [diff] [blame] | 297 | } else if (location.IsRegisterPair()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 298 | codegen_.DumpCoreRegister(stream, location.low()); |
| 299 | stream << "|"; |
| 300 | codegen_.DumpCoreRegister(stream, location.high()); |
Mark Mendell | 09ed1a3 | 2015-03-25 08:30:06 -0400 | [diff] [blame] | 301 | } else if (location.IsUnallocated()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 302 | stream << "unallocated"; |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 303 | } else { |
| 304 | DCHECK(location.IsDoubleStackSlot()); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 305 | stream << "2x" << location.GetStackIndex() << "(sp)"; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 309 | std::ostream& StartAttributeStream(const char* name = nullptr) { |
| 310 | if (name == nullptr) { |
| 311 | output_ << " "; |
| 312 | } else { |
| 313 | DCHECK(!HasWhitespace(name)) << "Checker does not allow spaces in attributes"; |
| 314 | output_ << " " << name << ":"; |
| 315 | } |
| 316 | return output_; |
| 317 | } |
| 318 | |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 319 | void VisitParallelMove(HParallelMove* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 320 | StartAttributeStream("liveness") << instruction->GetLifetimePosition(); |
| 321 | StringList moves; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 322 | for (size_t i = 0, e = instruction->NumMoves(); i < e; ++i) { |
| 323 | MoveOperands* move = instruction->MoveOperandsAt(i); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 324 | std::ostream& str = moves.NewEntryStream(); |
| 325 | DumpLocation(str, move->GetSource()); |
| 326 | str << "->"; |
| 327 | DumpLocation(str, move->GetDestination()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 328 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 329 | StartAttributeStream("moves") << moves; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 330 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 331 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 332 | void VisitIntConstant(HIntConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 333 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 334 | } |
| 335 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 336 | void VisitLongConstant(HLongConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 337 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 338 | } |
| 339 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 340 | void VisitFloatConstant(HFloatConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 341 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 342 | } |
| 343 | |
David Brazdil | 36cf095 | 2015-01-08 19:28:33 +0000 | [diff] [blame] | 344 | void VisitDoubleConstant(HDoubleConstant* instruction) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 345 | StartAttributeStream() << instruction->GetValue(); |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 348 | void VisitPhi(HPhi* phi) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 349 | StartAttributeStream("reg") << phi->GetRegNumber(); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 350 | StartAttributeStream("is_catch_phi") << std::boolalpha << phi->IsCatchPhi() << std::noboolalpha; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 353 | void VisitMemoryBarrier(HMemoryBarrier* barrier) OVERRIDE { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 354 | StartAttributeStream("kind") << barrier->GetBarrierKind(); |
Calin Juravle | 27df758 | 2015-04-17 19:12:31 +0100 | [diff] [blame] | 355 | } |
| 356 | |
David Brazdil | bff7503 | 2015-07-08 17:26:51 +0000 | [diff] [blame] | 357 | void VisitMonitorOperation(HMonitorOperation* monitor) OVERRIDE { |
| 358 | StartAttributeStream("kind") << (monitor->IsEnter() ? "enter" : "exit"); |
| 359 | } |
| 360 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 361 | void VisitLoadClass(HLoadClass* load_class) OVERRIDE { |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 362 | StartAttributeStream("gen_clinit_check") << std::boolalpha |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 363 | << load_class->MustGenerateClinitCheck() << std::noboolalpha; |
Calin Juravle | 386062d | 2015-10-07 18:55:43 +0100 | [diff] [blame] | 364 | StartAttributeStream("needs_access_check") << std::boolalpha |
| 365 | << load_class->NeedsAccessCheck() << std::noboolalpha; |
Calin Juravle | 0ba218d | 2015-05-19 18:46:01 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Guillaume "Vermeille" Sanchez | 9099ef7 | 2015-05-20 15:19:21 +0100 | [diff] [blame] | 368 | void VisitCheckCast(HCheckCast* check_cast) OVERRIDE { |
| 369 | StartAttributeStream("must_do_null_check") << std::boolalpha |
| 370 | << check_cast->MustDoNullCheck() << std::noboolalpha; |
| 371 | } |
| 372 | |
| 373 | void VisitInstanceOf(HInstanceOf* instance_of) OVERRIDE { |
| 374 | StartAttributeStream("must_do_null_check") << std::boolalpha |
| 375 | << instance_of->MustDoNullCheck() << std::noboolalpha; |
| 376 | } |
| 377 | |
Nicolas Geoffray | 6e7455e | 2015-09-28 16:25:37 +0100 | [diff] [blame] | 378 | void VisitArraySet(HArraySet* array_set) OVERRIDE { |
| 379 | StartAttributeStream("value_can_be_null") << std::boolalpha |
| 380 | << array_set->GetValueCanBeNull() << std::noboolalpha; |
| 381 | } |
| 382 | |
Nicolas Geoffray | 842acd4 | 2015-07-01 13:00:15 +0100 | [diff] [blame] | 383 | void VisitInvoke(HInvoke* invoke) OVERRIDE { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 384 | StartAttributeStream("dex_file_index") << invoke->GetDexMethodIndex(); |
Nicolas Geoffray | 242febb | 2015-07-01 16:10:44 +0100 | [diff] [blame] | 385 | StartAttributeStream("method_name") << PrettyMethod( |
| 386 | invoke->GetDexMethodIndex(), GetGraph()->GetDexFile(), /* with_signature */ false); |
Nicolas Geoffray | 842acd4 | 2015-07-01 13:00:15 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 389 | void VisitInvokeUnresolved(HInvokeUnresolved* invoke) OVERRIDE { |
| 390 | VisitInvoke(invoke); |
| 391 | StartAttributeStream("invoke_type") << invoke->GetOriginalInvokeType(); |
| 392 | } |
| 393 | |
Nicolas Geoffray | 842acd4 | 2015-07-01 13:00:15 +0100 | [diff] [blame] | 394 | void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE { |
| 395 | VisitInvoke(invoke); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 396 | StartAttributeStream("recursive") << std::boolalpha |
| 397 | << invoke->IsRecursive() |
| 398 | << std::noboolalpha; |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 399 | StartAttributeStream("intrinsic") << invoke->GetIntrinsic(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Calin Juravle | e460d1d | 2015-09-29 04:52:17 +0100 | [diff] [blame] | 402 | void VisitUnresolvedInstanceFieldGet(HUnresolvedInstanceFieldGet* field_access) OVERRIDE { |
| 403 | StartAttributeStream("field_type") << field_access->GetFieldType(); |
| 404 | } |
| 405 | |
| 406 | void VisitUnresolvedInstanceFieldSet(HUnresolvedInstanceFieldSet* field_access) OVERRIDE { |
| 407 | StartAttributeStream("field_type") << field_access->GetFieldType(); |
| 408 | } |
| 409 | |
| 410 | void VisitUnresolvedStaticFieldGet(HUnresolvedStaticFieldGet* field_access) OVERRIDE { |
| 411 | StartAttributeStream("field_type") << field_access->GetFieldType(); |
| 412 | } |
| 413 | |
| 414 | void VisitUnresolvedStaticFieldSet(HUnresolvedStaticFieldSet* field_access) OVERRIDE { |
| 415 | StartAttributeStream("field_type") << field_access->GetFieldType(); |
| 416 | } |
| 417 | |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 418 | void VisitTryBoundary(HTryBoundary* try_boundary) OVERRIDE { |
David Brazdil | 56e1acc | 2015-06-30 15:41:36 +0100 | [diff] [blame] | 419 | StartAttributeStream("kind") << (try_boundary->IsEntry() ? "entry" : "exit"); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 422 | bool IsPass(const char* name) { |
| 423 | return strcmp(pass_name_, name) == 0; |
| 424 | } |
| 425 | |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 426 | void PrintInstruction(HInstruction* instruction) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 427 | output_ << instruction->DebugName(); |
| 428 | if (instruction->InputCount() > 0) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 429 | StringList inputs; |
| 430 | for (HInputIterator it(instruction); !it.Done(); it.Advance()) { |
| 431 | inputs.NewEntryStream() << GetTypeId(it.Current()->GetType()) << it.Current()->GetId(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 432 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 433 | StartAttributeStream() << inputs; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 434 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 435 | instruction->Accept(this); |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 436 | if (instruction->HasEnvironment()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 437 | StringList envs; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 438 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 439 | environment != nullptr; |
| 440 | environment = environment->GetParent()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 441 | StringList vregs; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 442 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 443 | HInstruction* insn = environment->GetInstructionAt(i); |
| 444 | if (insn != nullptr) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 445 | vregs.NewEntryStream() << GetTypeId(insn->GetType()) << insn->GetId(); |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 446 | } else { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 447 | vregs.NewEntryStream() << "_"; |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 448 | } |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 449 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 450 | envs.NewEntryStream() << vregs; |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 451 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 452 | StartAttributeStream("env") << envs; |
Zheng Xu | bb7a28a | 2015-01-09 14:40:47 +0800 | [diff] [blame] | 453 | } |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 454 | if (IsPass(SsaLivenessAnalysis::kLivenessPassName) |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 455 | && is_after_pass_ |
| 456 | && instruction->GetLifetimePosition() != kNoLifetime) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 457 | StartAttributeStream("liveness") << instruction->GetLifetimePosition(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 458 | if (instruction->HasLiveInterval()) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 459 | LiveInterval* interval = instruction->GetLiveInterval(); |
David Brazdil | c7a2485 | 2015-05-15 16:44:05 +0100 | [diff] [blame] | 460 | StartAttributeStream("ranges") |
| 461 | << StringList(interval->GetFirstRange(), StringList::kSetBrackets); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 462 | StartAttributeStream("uses") << StringList(interval->GetFirstUse()); |
| 463 | StartAttributeStream("env_uses") << StringList(interval->GetFirstEnvironmentUse()); |
| 464 | StartAttributeStream("is_fixed") << interval->IsFixed(); |
| 465 | StartAttributeStream("is_split") << interval->IsSplit(); |
| 466 | StartAttributeStream("is_low") << interval->IsLowInterval(); |
| 467 | StartAttributeStream("is_high") << interval->IsHighInterval(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 468 | } |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 469 | } else if (IsPass(RegisterAllocator::kRegisterAllocatorPassName) && is_after_pass_) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 470 | StartAttributeStream("liveness") << instruction->GetLifetimePosition(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 471 | LocationSummary* locations = instruction->GetLocations(); |
| 472 | if (locations != nullptr) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 473 | StringList inputs; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 474 | for (size_t i = 0; i < instruction->InputCount(); ++i) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 475 | DumpLocation(inputs.NewEntryStream(), locations->InAt(i)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 476 | } |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 477 | std::ostream& attr = StartAttributeStream("locations"); |
| 478 | attr << inputs << "->"; |
| 479 | DumpLocation(attr, locations->Out()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 480 | } |
David Brazdil | a4b8c21 | 2015-05-07 09:59:30 +0100 | [diff] [blame] | 481 | } else if (IsPass(LICM::kLoopInvariantCodeMotionPassName) |
| 482 | || IsPass(HDeadCodeElimination::kFinalDeadCodeEliminationPassName)) { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 483 | HLoopInformation* info = instruction->GetBlock()->GetLoopInformation(); |
| 484 | if (info == nullptr) { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 485 | StartAttributeStream("loop") << "none"; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 486 | } else { |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 487 | StartAttributeStream("loop") << "B" << info->GetHeader()->GetBlockId(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 488 | } |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame^] | 489 | } else if ((IsPass(ReferenceTypePropagation::kReferenceTypePropagationPassName) |
| 490 | || IsPass(HInliner::kInlinerPassName)) |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 491 | && (instruction->GetType() == Primitive::kPrimNot)) { |
| 492 | ReferenceTypeInfo info = instruction->IsLoadClass() |
| 493 | ? instruction->AsLoadClass()->GetLoadedClassRTI() |
| 494 | : instruction->GetReferenceTypeInfo(); |
| 495 | ScopedObjectAccess soa(Thread::Current()); |
| 496 | if (info.IsValid()) { |
| 497 | StartAttributeStream("klass") << PrettyDescriptor(info.GetTypeHandle().Get()); |
| 498 | StartAttributeStream("can_be_null") |
| 499 | << std::boolalpha << instruction->CanBeNull() << std::noboolalpha; |
| 500 | StartAttributeStream("exact") << std::boolalpha << info.IsExact() << std::noboolalpha; |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 501 | } else if (instruction->IsLoadClass()) { |
| 502 | StartAttributeStream("klass") << "unresolved"; |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 503 | } else { |
Calin Juravle | 98893e1 | 2015-10-02 21:05:03 +0100 | [diff] [blame] | 504 | DCHECK(!is_after_pass_) |
| 505 | << "Expected a valid rti after reference type propagation"; |
Nicolas Geoffray | 7cb499b | 2015-06-17 11:35:11 +0100 | [diff] [blame] | 506 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 507 | } |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 508 | if (disasm_info_ != nullptr) { |
| 509 | DCHECK(disassembler_ != nullptr); |
| 510 | // If the information is available, disassemble the code generated for |
| 511 | // this instruction. |
| 512 | auto it = disasm_info_->GetInstructionIntervals().find(instruction); |
| 513 | if (it != disasm_info_->GetInstructionIntervals().end() |
| 514 | && it->second.start != it->second.end) { |
| 515 | output_ << std::endl; |
| 516 | disassembler_->Disassemble(output_, it->second.start, it->second.end); |
| 517 | } |
| 518 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void PrintInstructions(const HInstructionList& list) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 522 | for (HInstructionIterator it(list); !it.Done(); it.Advance()) { |
| 523 | HInstruction* instruction = it.Current(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 524 | int bci = 0; |
David Brazdil | ea55b93 | 2015-01-27 17:12:29 +0000 | [diff] [blame] | 525 | size_t num_uses = 0; |
| 526 | for (HUseIterator<HInstruction*> use_it(instruction->GetUses()); |
| 527 | !use_it.Done(); |
| 528 | use_it.Advance()) { |
| 529 | ++num_uses; |
| 530 | } |
| 531 | AddIndent(); |
| 532 | output_ << bci << " " << num_uses << " " |
| 533 | << GetTypeId(instruction->GetType()) << instruction->GetId() << " "; |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 534 | PrintInstruction(instruction); |
David Brazdil | c7465286 | 2015-05-13 17:50:09 +0100 | [diff] [blame] | 535 | output_ << " " << kEndInstructionMarker << std::endl; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 539 | void DumpStartOfDisassemblyBlock(const char* block_name, |
| 540 | int predecessor_index, |
| 541 | int successor_index) { |
| 542 | StartTag("block"); |
| 543 | PrintProperty("name", block_name); |
| 544 | PrintInt("from_bci", -1); |
| 545 | PrintInt("to_bci", -1); |
| 546 | if (predecessor_index != -1) { |
| 547 | PrintProperty("predecessors", "B", predecessor_index); |
| 548 | } else { |
| 549 | PrintEmptyProperty("predecessors"); |
| 550 | } |
| 551 | if (successor_index != -1) { |
| 552 | PrintProperty("successors", "B", successor_index); |
| 553 | } else { |
| 554 | PrintEmptyProperty("successors"); |
| 555 | } |
| 556 | PrintEmptyProperty("xhandlers"); |
| 557 | PrintEmptyProperty("flags"); |
| 558 | StartTag("states"); |
| 559 | StartTag("locals"); |
| 560 | PrintInt("size", 0); |
| 561 | PrintProperty("method", "None"); |
| 562 | EndTag("locals"); |
| 563 | EndTag("states"); |
| 564 | StartTag("HIR"); |
| 565 | } |
| 566 | |
| 567 | void DumpEndOfDisassemblyBlock() { |
| 568 | EndTag("HIR"); |
| 569 | EndTag("block"); |
| 570 | } |
| 571 | |
| 572 | void DumpDisassemblyBlockForFrameEntry() { |
| 573 | DumpStartOfDisassemblyBlock(kDisassemblyBlockFrameEntry, |
| 574 | -1, |
| 575 | GetGraph()->GetEntryBlock()->GetBlockId()); |
| 576 | output_ << " 0 0 disasm " << kDisassemblyBlockFrameEntry << " "; |
| 577 | GeneratedCodeInterval frame_entry = disasm_info_->GetFrameEntryInterval(); |
| 578 | if (frame_entry.start != frame_entry.end) { |
| 579 | output_ << std::endl; |
| 580 | disassembler_->Disassemble(output_, frame_entry.start, frame_entry.end); |
| 581 | } |
| 582 | output_ << kEndInstructionMarker << std::endl; |
| 583 | DumpEndOfDisassemblyBlock(); |
| 584 | } |
| 585 | |
| 586 | void DumpDisassemblyBlockForSlowPaths() { |
| 587 | if (disasm_info_->GetSlowPathIntervals().empty()) { |
| 588 | return; |
| 589 | } |
| 590 | // If the graph has an exit block we attach the block for the slow paths |
| 591 | // after it. Else we just add the block to the graph without linking it to |
| 592 | // any other. |
| 593 | DumpStartOfDisassemblyBlock( |
| 594 | kDisassemblyBlockSlowPaths, |
| 595 | GetGraph()->HasExitBlock() ? GetGraph()->GetExitBlock()->GetBlockId() : -1, |
| 596 | -1); |
| 597 | for (SlowPathCodeInfo info : disasm_info_->GetSlowPathIntervals()) { |
| 598 | output_ << " 0 0 disasm " << info.slow_path->GetDescription() << std::endl; |
| 599 | disassembler_->Disassemble(output_, info.code_interval.start, info.code_interval.end); |
| 600 | output_ << kEndInstructionMarker << std::endl; |
| 601 | } |
| 602 | DumpEndOfDisassemblyBlock(); |
| 603 | } |
| 604 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 605 | void Run() { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 606 | StartTag("cfg"); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 607 | std::string pass_desc = std::string(pass_name_) |
| 608 | + " (" |
| 609 | + (is_after_pass_ ? "after" : "before") |
| 610 | + (graph_in_bad_state_ ? ", bad_state" : "") |
| 611 | + ")"; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 612 | PrintProperty("name", pass_desc.c_str()); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 613 | if (disasm_info_ != nullptr) { |
| 614 | DumpDisassemblyBlockForFrameEntry(); |
| 615 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 616 | VisitInsertionOrder(); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 617 | if (disasm_info_ != nullptr) { |
| 618 | DumpDisassemblyBlockForSlowPaths(); |
| 619 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 620 | EndTag("cfg"); |
| 621 | } |
| 622 | |
David Brazdil | b7e4a06 | 2014-12-29 15:35:02 +0000 | [diff] [blame] | 623 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 624 | StartTag("block"); |
| 625 | PrintProperty("name", "B", block->GetBlockId()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 626 | if (block->GetLifetimeStart() != kNoLifetime) { |
| 627 | // Piggy back on these fields to show the lifetime of the block. |
| 628 | PrintInt("from_bci", block->GetLifetimeStart()); |
| 629 | PrintInt("to_bci", block->GetLifetimeEnd()); |
| 630 | } else { |
| 631 | PrintInt("from_bci", -1); |
| 632 | PrintInt("to_bci", -1); |
| 633 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 634 | PrintPredecessors(block); |
| 635 | PrintSuccessors(block); |
David Brazdil | fc6a86a | 2015-06-26 10:33:45 +0000 | [diff] [blame] | 636 | PrintExceptionHandlers(block); |
| 637 | |
| 638 | if (block->IsCatchBlock()) { |
| 639 | PrintProperty("flags", "catch_block"); |
| 640 | } else { |
| 641 | PrintEmptyProperty("flags"); |
| 642 | } |
| 643 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 644 | if (block->GetDominator() != nullptr) { |
| 645 | PrintProperty("dominator", "B", block->GetDominator()->GetBlockId()); |
| 646 | } |
| 647 | |
| 648 | StartTag("states"); |
| 649 | StartTag("locals"); |
| 650 | PrintInt("size", 0); |
| 651 | PrintProperty("method", "None"); |
| 652 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 653 | AddIndent(); |
| 654 | HInstruction* instruction = it.Current(); |
Nicolas Geoffray | b09aacb | 2014-09-17 18:21:53 +0100 | [diff] [blame] | 655 | output_ << instruction->GetId() << " " << GetTypeId(instruction->GetType()) |
| 656 | << instruction->GetId() << "[ "; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 657 | for (HInputIterator inputs(instruction); !inputs.Done(); inputs.Advance()) { |
| 658 | output_ << inputs.Current()->GetId() << " "; |
| 659 | } |
| 660 | output_ << "]" << std::endl; |
| 661 | } |
| 662 | EndTag("locals"); |
| 663 | EndTag("states"); |
| 664 | |
| 665 | StartTag("HIR"); |
| 666 | PrintInstructions(block->GetPhis()); |
| 667 | PrintInstructions(block->GetInstructions()); |
| 668 | EndTag("HIR"); |
| 669 | EndTag("block"); |
| 670 | } |
| 671 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 672 | static constexpr const char* const kEndInstructionMarker = "<|@"; |
| 673 | static constexpr const char* const kDisassemblyBlockFrameEntry = "FrameEntry"; |
| 674 | static constexpr const char* const kDisassemblyBlockSlowPaths = "SlowPaths"; |
| 675 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 676 | private: |
| 677 | std::ostream& output_; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 678 | const char* pass_name_; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 679 | const bool is_after_pass_; |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 680 | const bool graph_in_bad_state_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 681 | const CodeGenerator& codegen_; |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 682 | const DisassemblyInformation* disasm_info_; |
| 683 | std::unique_ptr<HGraphVisualizerDisassembler> disassembler_; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 684 | size_t indent_; |
| 685 | |
| 686 | DISALLOW_COPY_AND_ASSIGN(HGraphVisualizerPrinter); |
| 687 | }; |
| 688 | |
| 689 | HGraphVisualizer::HGraphVisualizer(std::ostream* output, |
| 690 | HGraph* graph, |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 691 | const CodeGenerator& codegen) |
| 692 | : output_(output), graph_(graph), codegen_(codegen) {} |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 693 | |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 694 | void HGraphVisualizer::PrintHeader(const char* method_name) const { |
| 695 | DCHECK(output_ != nullptr); |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 696 | HGraphVisualizerPrinter printer(graph_, *output_, "", true, false, codegen_); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 697 | printer.StartTag("compilation"); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 698 | printer.PrintProperty("name", method_name); |
| 699 | printer.PrintProperty("method", method_name); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 700 | printer.PrintTime("date"); |
| 701 | printer.EndTag("compilation"); |
| 702 | } |
| 703 | |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 704 | void HGraphVisualizer::DumpGraph(const char* pass_name, |
| 705 | bool is_after_pass, |
| 706 | bool graph_in_bad_state) const { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 707 | DCHECK(output_ != nullptr); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 708 | if (!graph_->GetBlocks().empty()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 709 | HGraphVisualizerPrinter printer(graph_, |
| 710 | *output_, |
| 711 | pass_name, |
| 712 | is_after_pass, |
| 713 | graph_in_bad_state, |
| 714 | codegen_); |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 715 | printer.Run(); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 716 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 717 | } |
| 718 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 719 | void HGraphVisualizer::DumpGraphWithDisassembly() const { |
| 720 | DCHECK(output_ != nullptr); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 721 | if (!graph_->GetBlocks().empty()) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 722 | HGraphVisualizerPrinter printer(graph_, |
| 723 | *output_, |
| 724 | "disassembly", |
| 725 | /* is_after_pass */ true, |
| 726 | /* graph_in_bad_state */ false, |
| 727 | codegen_, |
| 728 | codegen_.GetDisassemblyInformation()); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 729 | printer.Run(); |
| 730 | } |
| 731 | } |
| 732 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 733 | } // namespace art |