Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [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 | #ifndef ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_ |
| 19 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 20 | #include "base/stringprintf.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 21 | #include "nodes.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class HPrettyPrinter : public HGraphVisitor { |
| 26 | public: |
| 27 | explicit HPrettyPrinter(HGraph* graph) : HGraphVisitor(graph) { } |
| 28 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 29 | void PrintPreInstruction(HInstruction* instruction) { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 30 | PrintString(" "); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 31 | PrintInt(instruction->GetId()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 32 | PrintString(": "); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 35 | void VisitInstruction(HInstruction* instruction) OVERRIDE { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 36 | PrintPreInstruction(instruction); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 37 | PrintString(instruction->DebugName()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 38 | PrintPostInstruction(instruction); |
| 39 | } |
| 40 | |
| 41 | void PrintPostInstruction(HInstruction* instruction) { |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 42 | HConstInputsRef inputs = instruction->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 43 | if (!inputs.empty()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 44 | PrintString("("); |
| 45 | bool first = true; |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 46 | for (const HInstruction* input : inputs) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 47 | if (first) { |
| 48 | first = false; |
| 49 | } else { |
| 50 | PrintString(", "); |
| 51 | } |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 52 | PrintInt(input->GetId()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 53 | } |
| 54 | PrintString(")"); |
| 55 | } |
| 56 | if (instruction->HasUses()) { |
| 57 | PrintString(" ["); |
| 58 | bool first = true; |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 59 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 60 | if (first) { |
| 61 | first = false; |
| 62 | } else { |
| 63 | PrintString(", "); |
| 64 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 65 | PrintInt(use.GetUser()->GetId()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 66 | } |
| 67 | PrintString("]"); |
| 68 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 69 | PrintNewLine(); |
| 70 | } |
| 71 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 72 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 73 | PrintString("BasicBlock "); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 74 | PrintInt(block->GetBlockId()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 75 | const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 76 | if (!predecessors.empty()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 77 | PrintString(", pred: "); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < predecessors.size() -1; i++) { |
| 79 | PrintInt(predecessors[i]->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 80 | PrintString(", "); |
| 81 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 82 | PrintInt(predecessors.back()->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 83 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 84 | const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors(); |
| 85 | if (!successors.empty()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 86 | PrintString(", succ: "); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 87 | for (size_t i = 0; i < successors.size() - 1; i++) { |
| 88 | PrintInt(successors[i]->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 89 | PrintString(", "); |
| 90 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 91 | PrintInt(successors.back()->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 92 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 93 | PrintNewLine(); |
| 94 | HGraphVisitor::VisitBasicBlock(block); |
| 95 | } |
| 96 | |
| 97 | virtual void PrintNewLine() = 0; |
| 98 | virtual void PrintInt(int value) = 0; |
| 99 | virtual void PrintString(const char* value) = 0; |
| 100 | |
| 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter); |
| 103 | }; |
| 104 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 105 | class StringPrettyPrinter : public HPrettyPrinter { |
| 106 | public: |
| 107 | explicit StringPrettyPrinter(HGraph* graph) |
| 108 | : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { } |
| 109 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 110 | void PrintInt(int value) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 111 | str_ += StringPrintf("%d", value); |
| 112 | } |
| 113 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 114 | void PrintString(const char* value) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 115 | str_ += value; |
| 116 | } |
| 117 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 118 | void PrintNewLine() OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 119 | str_ += '\n'; |
| 120 | } |
| 121 | |
| 122 | void Clear() { str_.clear(); } |
| 123 | |
| 124 | std::string str() const { return str_; } |
| 125 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 126 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 127 | current_block_ = block; |
| 128 | HPrettyPrinter::VisitBasicBlock(block); |
| 129 | } |
| 130 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 131 | void VisitGoto(HGoto* gota) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 132 | PrintString(" "); |
| 133 | PrintInt(gota->GetId()); |
| 134 | PrintString(": Goto "); |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 135 | PrintInt(current_block_->GetSuccessors()[0]->GetBlockId()); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 136 | PrintNewLine(); |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | std::string str_; |
| 141 | HBasicBlock* current_block_; |
| 142 | |
| 143 | DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter); |
| 144 | }; |
| 145 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 146 | } // namespace art |
| 147 | |
| 148 | #endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_ |