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) { |
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; |
David Brazdil | ed59619 | 2015-01-23 10:39:45 +0000 | [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 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 71 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 72 | PrintString("BasicBlock "); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 73 | PrintInt(block->GetBlockId()); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 74 | const ArenaVector<HBasicBlock*>& predecessors = block->GetPredecessors(); |
| 75 | if (!predecessors.empty()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 76 | PrintString(", pred: "); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 77 | for (size_t i = 0; i < predecessors.size() -1; i++) { |
| 78 | PrintInt(predecessors[i]->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 79 | PrintString(", "); |
| 80 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 81 | PrintInt(predecessors.back()->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 82 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 83 | const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors(); |
| 84 | if (!successors.empty()) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 85 | PrintString(", succ: "); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 86 | for (size_t i = 0; i < successors.size() - 1; i++) { |
| 87 | PrintInt(successors[i]->GetBlockId()); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 88 | PrintString(", "); |
| 89 | } |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 90 | PrintInt(successors.back()->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 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 109 | void PrintInt(int value) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 110 | str_ += StringPrintf("%d", value); |
| 111 | } |
| 112 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 113 | void PrintString(const char* value) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 114 | str_ += value; |
| 115 | } |
| 116 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 117 | void PrintNewLine() OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 118 | str_ += '\n'; |
| 119 | } |
| 120 | |
| 121 | void Clear() { str_.clear(); } |
| 122 | |
| 123 | std::string str() const { return str_; } |
| 124 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 125 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 126 | current_block_ = block; |
| 127 | HPrettyPrinter::VisitBasicBlock(block); |
| 128 | } |
| 129 | |
Alexandre Rames | 2ed20af | 2015-03-06 13:55:35 +0000 | [diff] [blame] | 130 | void VisitGoto(HGoto* gota) OVERRIDE { |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 131 | PrintString(" "); |
| 132 | PrintInt(gota->GetId()); |
| 133 | PrintString(": Goto "); |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 134 | PrintInt(current_block_->GetSuccessor(0)->GetBlockId()); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 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_ |