1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_COMPILER_GRAPH_VISUALIZER_H_
6 #define V8_COMPILER_GRAPH_VISUALIZER_H_
7 
8 #include <stdio.h>
9 
10 #include <fstream>
11 #include <iosfwd>
12 #include <memory>
13 
14 #include "src/common/globals.h"
15 #include "src/handles/handles.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 class OptimizedCompilationInfo;
21 class SharedFunctionInfo;
22 class SourcePosition;
23 namespace compiler {
24 
25 class Graph;
26 class LiveRange;
27 class TopLevelLiveRange;
28 class Instruction;
29 class InstructionBlock;
30 class InstructionOperand;
31 class InstructionSequence;
32 class NodeOrigin;
33 class NodeOriginTable;
34 class RegisterAllocationData;
35 class Schedule;
36 class SourcePositionTable;
37 
38 struct TurboJsonFile : public std::ofstream {
39   TurboJsonFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode);
40   ~TurboJsonFile() override;
41 };
42 
43 struct TurboCfgFile : public std::ofstream {
44   explicit TurboCfgFile(Isolate* isolate = nullptr);
45   ~TurboCfgFile() override;
46 };
47 
48 struct SourcePositionAsJSON {
SourcePositionAsJSONSourcePositionAsJSON49   explicit SourcePositionAsJSON(const SourcePosition& sp) : sp(sp) {}
50   const SourcePosition& sp;
51 };
52 
53 V8_INLINE V8_EXPORT_PRIVATE SourcePositionAsJSON
AsJSON(const SourcePosition & sp)54 AsJSON(const SourcePosition& sp) {
55   return SourcePositionAsJSON(sp);
56 }
57 
58 struct NodeOriginAsJSON {
NodeOriginAsJSONNodeOriginAsJSON59   explicit NodeOriginAsJSON(const NodeOrigin& no) : no(no) {}
60   const NodeOrigin& no;
61 };
62 
AsJSON(const NodeOrigin & no)63 V8_INLINE V8_EXPORT_PRIVATE NodeOriginAsJSON AsJSON(const NodeOrigin& no) {
64   return NodeOriginAsJSON(no);
65 }
66 
67 std::ostream& operator<<(std::ostream& out, const SourcePositionAsJSON& pos);
68 
69 // Small helper that deduplicates SharedFunctionInfos.
70 class V8_EXPORT_PRIVATE SourceIdAssigner {
71  public:
SourceIdAssigner(size_t size)72   explicit SourceIdAssigner(size_t size) {
73     printed_.reserve(size);
74     source_ids_.reserve(size);
75   }
76   int GetIdFor(Handle<SharedFunctionInfo> shared);
GetIdAt(size_t pos)77   int GetIdAt(size_t pos) const { return source_ids_[pos]; }
78 
79  private:
80   std::vector<Handle<SharedFunctionInfo>> printed_;
81   std::vector<int> source_ids_;
82 };
83 
84 void JsonPrintAllSourceWithPositions(std::ostream& os,
85                                      OptimizedCompilationInfo* info,
86                                      Isolate* isolate);
87 
88 void JsonPrintFunctionSource(std::ostream& os, int source_id,
89                              std::unique_ptr<char[]> function_name,
90                              Handle<Script> script, Isolate* isolate,
91                              Handle<SharedFunctionInfo> shared,
92                              bool with_key = false);
93 std::unique_ptr<char[]> GetVisualizerLogFileName(OptimizedCompilationInfo* info,
94                                                  const char* optional_base_dir,
95                                                  const char* phase,
96                                                  const char* suffix);
97 
98 struct GraphAsJSON {
GraphAsJSONGraphAsJSON99   GraphAsJSON(const Graph& g, SourcePositionTable* p, NodeOriginTable* o)
100       : graph(g), positions(p), origins(o) {}
101   const Graph& graph;
102   const SourcePositionTable* positions;
103   const NodeOriginTable* origins;
104 };
105 
AsJSON(const Graph & g,SourcePositionTable * p,NodeOriginTable * o)106 V8_INLINE V8_EXPORT_PRIVATE GraphAsJSON AsJSON(const Graph& g,
107                                                SourcePositionTable* p,
108                                                NodeOriginTable* o) {
109   return GraphAsJSON(g, p, o);
110 }
111 
112 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
113                                            const GraphAsJSON& ad);
114 
115 struct AsRPO {
AsRPOAsRPO116   explicit AsRPO(const Graph& g) : graph(g) {}
117   const Graph& graph;
118 };
119 
120 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsRPO& ad);
121 
122 struct AsC1VCompilation {
AsC1VCompilationAsC1VCompilation123   explicit AsC1VCompilation(const OptimizedCompilationInfo* info)
124       : info_(info) {}
125   const OptimizedCompilationInfo* info_;
126 };
127 
128 struct AsScheduledGraph {
AsScheduledGraphAsScheduledGraph129   explicit AsScheduledGraph(const Schedule* schedule) : schedule(schedule) {}
130   const Schedule* schedule;
131 };
132 
133 std::ostream& operator<<(std::ostream& os, const AsScheduledGraph& scheduled);
134 struct AsC1V {
135   AsC1V(const char* phase, const Schedule* schedule,
136         const SourcePositionTable* positions = nullptr,
137         const InstructionSequence* instructions = nullptr)
schedule_AsC1V138       : schedule_(schedule),
139         instructions_(instructions),
140         positions_(positions),
141         phase_(phase) {}
142   const Schedule* schedule_;
143   const InstructionSequence* instructions_;
144   const SourcePositionTable* positions_;
145   const char* phase_;
146 };
147 
148 struct AsC1VRegisterAllocationData {
149   explicit AsC1VRegisterAllocationData(
150       const char* phase, const RegisterAllocationData* data = nullptr)
phase_AsC1VRegisterAllocationData151       : phase_(phase), data_(data) {}
152   const char* phase_;
153   const RegisterAllocationData* data_;
154 };
155 
156 std::ostream& operator<<(std::ostream& os, const AsC1VCompilation& ac);
157 std::ostream& operator<<(std::ostream& os, const AsC1V& ac);
158 std::ostream& operator<<(std::ostream& os,
159                          const AsC1VRegisterAllocationData& ac);
160 
161 struct LiveRangeAsJSON {
162   const LiveRange& range_;
163   const InstructionSequence& code_;
164 };
165 
166 std::ostream& operator<<(std::ostream& os,
167                          const LiveRangeAsJSON& live_range_json);
168 
169 struct TopLevelLiveRangeAsJSON {
170   const TopLevelLiveRange& range_;
171   const InstructionSequence& code_;
172 };
173 
174 std::ostream& operator<<(
175     std::ostream& os, const TopLevelLiveRangeAsJSON& top_level_live_range_json);
176 
177 struct RegisterAllocationDataAsJSON {
178   const RegisterAllocationData& data_;
179   const InstructionSequence& code_;
180 };
181 
182 std::ostream& operator<<(std::ostream& os,
183                          const RegisterAllocationDataAsJSON& ac);
184 
185 struct InstructionOperandAsJSON {
186   const InstructionOperand* op_;
187   const InstructionSequence* code_;
188 };
189 
190 std::ostream& operator<<(std::ostream& os, const InstructionOperandAsJSON& o);
191 
192 struct InstructionAsJSON {
193   int index_;
194   const Instruction* instr_;
195   const InstructionSequence* code_;
196 };
197 std::ostream& operator<<(std::ostream& os, const InstructionAsJSON& i);
198 
199 struct InstructionBlockAsJSON {
200   const InstructionBlock* block_;
201   const InstructionSequence* code_;
202 };
203 
204 std::ostream& operator<<(std::ostream& os, const InstructionBlockAsJSON& b);
205 
206 struct InstructionSequenceAsJSON {
207   const InstructionSequence* sequence_;
208 };
209 std::ostream& operator<<(std::ostream& os, const InstructionSequenceAsJSON& s);
210 
211 }  // namespace compiler
212 }  // namespace internal
213 }  // namespace v8
214 
215 #endif  // V8_COMPILER_GRAPH_VISUALIZER_H_
216