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