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 | |
| 35 | virtual void VisitInstruction(HInstruction* instruction) { |
| 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) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 42 | if (instruction->InputCount() != 0) { |
| 43 | PrintString("("); |
| 44 | bool first = true; |
| 45 | for (HInputIterator it(instruction); !it.Done(); it.Advance()) { |
| 46 | if (first) { |
| 47 | first = false; |
| 48 | } else { |
| 49 | PrintString(", "); |
| 50 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 51 | PrintInt(it.Current()->GetId()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 52 | } |
| 53 | PrintString(")"); |
| 54 | } |
| 55 | if (instruction->HasUses()) { |
| 56 | PrintString(" ["); |
| 57 | bool first = true; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 58 | for (HUseIterator<HInstruction> it(instruction->GetUses()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 59 | if (first) { |
| 60 | first = false; |
| 61 | } else { |
| 62 | PrintString(", "); |
| 63 | } |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 64 | PrintInt(it.Current()->GetUser()->GetId()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 65 | } |
| 66 | PrintString("]"); |
| 67 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 68 | PrintNewLine(); |
| 69 | } |
| 70 | |
| 71 | virtual void VisitBasicBlock(HBasicBlock* block) { |
| 72 | PrintString("BasicBlock "); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 73 | PrintInt(block->GetBlockId()); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 74 | const GrowableArray<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 75 | if (!predecessors.IsEmpty()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 76 | PrintString(", pred: "); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 77 | for (size_t i = 0; i < predecessors.Size() -1; i++) { |
| 78 | PrintInt(predecessors.Get(i)->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 79 | PrintString(", "); |
| 80 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 81 | PrintInt(predecessors.Peek()->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 82 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 83 | const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors(); |
| 84 | if (!successors.IsEmpty()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 85 | PrintString(", succ: "); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 86 | for (size_t i = 0; i < successors.Size() - 1; i++) { |
| 87 | PrintInt(successors.Get(i)->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 88 | PrintString(", "); |
| 89 | } |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 90 | PrintInt(successors.Peek()->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 91 | } |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 92 | PrintNewLine(); |
| 93 | HGraphVisitor::VisitBasicBlock(block); |
| 94 | } |
| 95 | |
| 96 | virtual void PrintNewLine() = 0; |
| 97 | virtual void PrintInt(int value) = 0; |
| 98 | virtual void PrintString(const char* value) = 0; |
| 99 | |
| 100 | private: |
| 101 | DISALLOW_COPY_AND_ASSIGN(HPrettyPrinter); |
| 102 | }; |
| 103 | |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 104 | class StringPrettyPrinter : public HPrettyPrinter { |
| 105 | public: |
| 106 | explicit StringPrettyPrinter(HGraph* graph) |
| 107 | : HPrettyPrinter(graph), str_(""), current_block_(nullptr) { } |
| 108 | |
| 109 | virtual void PrintInt(int value) { |
| 110 | str_ += StringPrintf("%d", value); |
| 111 | } |
| 112 | |
| 113 | virtual void PrintString(const char* value) { |
| 114 | str_ += value; |
| 115 | } |
| 116 | |
| 117 | virtual void PrintNewLine() { |
| 118 | str_ += '\n'; |
| 119 | } |
| 120 | |
| 121 | void Clear() { str_.clear(); } |
| 122 | |
| 123 | std::string str() const { return str_; } |
| 124 | |
| 125 | virtual void VisitBasicBlock(HBasicBlock* block) { |
| 126 | current_block_ = block; |
| 127 | HPrettyPrinter::VisitBasicBlock(block); |
| 128 | } |
| 129 | |
| 130 | virtual void VisitGoto(HGoto* gota) { |
| 131 | PrintString(" "); |
| 132 | PrintInt(gota->GetId()); |
| 133 | PrintString(": Goto "); |
| 134 | PrintInt(current_block_->GetSuccessors().Get(0)->GetBlockId()); |
| 135 | PrintNewLine(); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | std::string str_; |
| 140 | HBasicBlock* current_block_; |
| 141 | |
| 142 | DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter); |
| 143 | }; |
| 144 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 145 | } // namespace art |
| 146 | |
| 147 | #endif // ART_COMPILER_OPTIMIZING_PRETTY_PRINTER_H_ |