Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_SEA_IR_DEBUG_DOT_GEN_H_ |
| 18 | #define ART_COMPILER_SEA_IR_DEBUG_DOT_GEN_H_ |
| 19 | |
| 20 | #include "base/stringprintf.h" |
| 21 | #include "file_output_stream.h" |
| 22 | #include "sea_ir/sea.h" |
| 23 | #include "sea_ir/types/type_inference.h" |
| 24 | |
| 25 | namespace sea_ir { |
| 26 | |
| 27 | class DotConversionOptions { |
| 28 | public: |
| 29 | DotConversionOptions(): save_use_edges_(false) { } |
| 30 | bool WillSaveUseEdges() const { |
| 31 | return save_use_edges_; |
| 32 | } |
| 33 | private: |
| 34 | bool save_use_edges_; |
| 35 | }; |
| 36 | |
| 37 | class DotGenerationVisitor: public IRVisitor { |
| 38 | public: |
| 39 | explicit DotGenerationVisitor(const DotConversionOptions* const options, |
| 40 | std::map<int, const Type*>* types): graph_(), types_(types), options_(options) { } |
| 41 | |
| 42 | virtual void Initialize(SeaGraph* graph); |
| 43 | // Saves the ssa def->use edges corresponding to @instruction. |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 44 | void ToDotSSAEdges(InstructionNode* instruction); |
| 45 | void Visit(SeaGraph* graph) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 46 | dot_text_ += "digraph seaOfNodes {\ncompound=true\n"; |
| 47 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 48 | void Visit(SignatureNode* parameter); |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 49 | |
| 50 | // Appends to @result a dot language formatted string representing the node and |
| 51 | // (by convention) outgoing edges, so that the composition of theToDot() of all nodes |
| 52 | // builds a complete dot graph (without prolog and epilog though). |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 53 | void Visit(Region* region); |
| 54 | void Visit(InstructionNode* instruction); |
| 55 | void Visit(PhiInstructionNode* phi); |
| 56 | void Visit(UnnamedConstInstructionNode* instruction); |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 57 | |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 58 | void Visit(ConstInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 59 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 60 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 61 | void Visit(ReturnInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 62 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 63 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 64 | void Visit(IfNeInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 65 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 66 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 67 | void Visit(MoveResultInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 68 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 69 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 70 | void Visit(InvokeStaticInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 71 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 72 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 73 | void Visit(AddIntInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 74 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 75 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 76 | void Visit(GotoInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 77 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 78 | } |
Dragos Sbirlea | 147c00c | 2013-08-05 11:35:48 -0700 | [diff] [blame^] | 79 | void Visit(IfEqzInstructionNode* instruction) { |
Dragos Sbirlea | 6447919 | 2013-08-01 15:38:43 -0700 | [diff] [blame] | 80 | Visit(reinterpret_cast<InstructionNode*>(instruction)); |
| 81 | } |
| 82 | |
| 83 | std::string GetResult() const { |
| 84 | return dot_text_; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | std::string dot_text_; |
| 89 | SeaGraph* graph_; |
| 90 | std::map<int, const Type*>* types_; |
| 91 | const DotConversionOptions* const options_; |
| 92 | }; |
| 93 | |
| 94 | // Stores options for turning a SEA IR graph to a .dot file. |
| 95 | class DotConversion { |
| 96 | public: |
| 97 | DotConversion(): options_() { } |
| 98 | // Saves to @filename the .dot representation of @graph with the options @options. |
| 99 | void DumpSea(SeaGraph* graph, std::string filename, std::map<int, const Type*>* types) const { |
| 100 | LOG(INFO) << "Starting to write SEA string to file."; |
| 101 | DotGenerationVisitor dgv = DotGenerationVisitor(&options_, types); |
| 102 | graph->Accept(&dgv); |
| 103 | art::File* file = art::OS::OpenFile(filename.c_str(), true, true); |
| 104 | art::FileOutputStream fos(file); |
| 105 | std::string graph_as_string = dgv.GetResult(); |
| 106 | graph_as_string += "}"; |
| 107 | fos.WriteFully(graph_as_string.c_str(), graph_as_string.size()); |
| 108 | LOG(INFO) << "Written SEA string to file."; |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | DotConversionOptions options_; |
| 113 | }; |
| 114 | |
| 115 | } // namespace sea_ir |
| 116 | #endif // ART_COMPILER_SEA_IR_DEBUG_DOT_GEN_H_ |