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