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