blob: 675d83d5154de99fb593419c81742da387c3c04c [file] [log] [blame]
Dragos Sbirlea64479192013-08-01 15:38:43 -07001/*
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 Sbirlea6547fa92013-08-05 18:33:30 -070020#include "safe_map.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070021#include "base/stringprintf.h"
22#include "file_output_stream.h"
Dragos Sbirleabfaf44f2013-08-06 15:41:44 -070023#include "sea_ir/ir/sea.h"
Dragos Sbirlea64479192013-08-01 15:38:43 -070024#include "sea_ir/types/type_inference.h"
25
26namespace sea_ir {
27
28class 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
38class DotGenerationVisitor: public IRVisitor {
39 public:
40 explicit DotGenerationVisitor(const DotConversionOptions* const options,
Dragos Sbirlea6547fa92013-08-05 18:33:30 -070041 art::SafeMap<int, const Type*>* types): graph_(), types_(types), options_(options) { }
Dragos Sbirlea64479192013-08-01 15:38:43 -070042
43 virtual void Initialize(SeaGraph* graph);
44 // Saves the ssa def->use edges corresponding to @instruction.
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070045 void ToDotSSAEdges(InstructionNode* instruction);
Dragos Sbirlea7b89bc02013-08-05 16:24:57 -070046 void ToDotSSAEdges(PhiInstructionNode* instruction);
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070047 void Visit(SeaGraph* graph) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070048 dot_text_ += "digraph seaOfNodes {\ncompound=true\n";
49 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070050 void Visit(SignatureNode* parameter);
Dragos Sbirlea64479192013-08-01 15:38:43 -070051
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 Sbirlea147c00c2013-08-05 11:35:48 -070055 void Visit(Region* region);
56 void Visit(InstructionNode* instruction);
57 void Visit(PhiInstructionNode* phi);
58 void Visit(UnnamedConstInstructionNode* instruction);
Dragos Sbirlea64479192013-08-01 15:38:43 -070059
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070060 void Visit(ConstInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070061 Visit(reinterpret_cast<InstructionNode*>(instruction));
62 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070063 void Visit(ReturnInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070064 Visit(reinterpret_cast<InstructionNode*>(instruction));
65 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070066 void Visit(IfNeInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070067 Visit(reinterpret_cast<InstructionNode*>(instruction));
68 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070069 void Visit(MoveResultInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070070 Visit(reinterpret_cast<InstructionNode*>(instruction));
71 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070072 void Visit(InvokeStaticInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070073 Visit(reinterpret_cast<InstructionNode*>(instruction));
74 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070075 void Visit(AddIntInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070076 Visit(reinterpret_cast<InstructionNode*>(instruction));
77 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070078 void Visit(GotoInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070079 Visit(reinterpret_cast<InstructionNode*>(instruction));
80 }
Dragos Sbirlea147c00c2013-08-05 11:35:48 -070081 void Visit(IfEqzInstructionNode* instruction) {
Dragos Sbirlea64479192013-08-01 15:38:43 -070082 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 Sbirlea6547fa92013-08-05 18:33:30 -070092 art::SafeMap<int, const Type*>* types_;
Dragos Sbirlea64479192013-08-01 15:38:43 -070093 const DotConversionOptions* const options_;
94};
95
96// Stores options for turning a SEA IR graph to a .dot file.
97class DotConversion {
98 public:
99 DotConversion(): options_() { }
100 // Saves to @filename the .dot representation of @graph with the options @options.
Dragos Sbirlea6547fa92013-08-05 18:33:30 -0700101 void DumpSea(SeaGraph* graph, std::string filename,
102 art::SafeMap<int, const Type*>* types) const {
Dragos Sbirlea64479192013-08-01 15:38:43 -0700103 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_