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