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/globals.h"
14 #include "src/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 InstructionSequence;
26 class NodeOrigin;
27 class NodeOriginTable;
28 class RegisterAllocationData;
29 class Schedule;
30 class SourcePositionTable;
31
32 struct TurboJsonFile : public std::ofstream {
33 TurboJsonFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode);
34 ~TurboJsonFile();
35 };
36
37 struct SourcePositionAsJSON {
SourcePositionAsJSONSourcePositionAsJSON38 explicit SourcePositionAsJSON(const SourcePosition& sp) : sp(sp) {}
39 const SourcePosition& sp;
40 };
41
42 V8_INLINE V8_EXPORT_PRIVATE SourcePositionAsJSON
AsJSON(const SourcePosition & sp)43 AsJSON(const SourcePosition& sp) {
44 return SourcePositionAsJSON(sp);
45 }
46
47 struct NodeOriginAsJSON {
NodeOriginAsJSONNodeOriginAsJSON48 explicit NodeOriginAsJSON(const NodeOrigin& no) : no(no) {}
49 const NodeOrigin& no;
50 };
51
AsJSON(const NodeOrigin & no)52 V8_INLINE V8_EXPORT_PRIVATE NodeOriginAsJSON AsJSON(const NodeOrigin& no) {
53 return NodeOriginAsJSON(no);
54 }
55
56 std::ostream& operator<<(std::ostream& out, const SourcePositionAsJSON& pos);
57
58 // Small helper that deduplicates SharedFunctionInfos.
59 class SourceIdAssigner {
60 public:
SourceIdAssigner(size_t size)61 explicit SourceIdAssigner(size_t size) {
62 printed_.reserve(size);
63 source_ids_.reserve(size);
64 }
65 int GetIdFor(Handle<SharedFunctionInfo> shared);
GetIdAt(size_t pos)66 int GetIdAt(size_t pos) const { return source_ids_[pos]; }
67
68 private:
69 std::vector<Handle<SharedFunctionInfo>> printed_;
70 std::vector<int> source_ids_;
71 };
72
73 void JsonPrintAllSourceWithPositions(std::ostream& os,
74 OptimizedCompilationInfo* info,
75 Isolate* isolate);
76
77 void JsonPrintFunctionSource(std::ostream& os, int source_id,
78 std::unique_ptr<char[]> function_name,
79 Handle<Script> script, Isolate* isolate,
80 Handle<SharedFunctionInfo> shared,
81 bool with_key = false);
82 std::unique_ptr<char[]> GetVisualizerLogFileName(OptimizedCompilationInfo* info,
83 const char* optional_base_dir,
84 const char* phase,
85 const char* suffix);
86
87 struct GraphAsJSON {
GraphAsJSONGraphAsJSON88 GraphAsJSON(const Graph& g, SourcePositionTable* p, NodeOriginTable* o)
89 : graph(g), positions(p), origins(o) {}
90 const Graph& graph;
91 const SourcePositionTable* positions;
92 const NodeOriginTable* origins;
93 };
94
AsJSON(const Graph & g,SourcePositionTable * p,NodeOriginTable * o)95 V8_INLINE V8_EXPORT_PRIVATE GraphAsJSON AsJSON(const Graph& g,
96 SourcePositionTable* p,
97 NodeOriginTable* o) {
98 return GraphAsJSON(g, p, o);
99 }
100
101 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
102 const GraphAsJSON& ad);
103
104 struct AsRPO {
AsRPOAsRPO105 explicit AsRPO(const Graph& g) : graph(g) {}
106 const Graph& graph;
107 };
108
109 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsRPO& ad);
110
111 struct AsC1VCompilation {
AsC1VCompilationAsC1VCompilation112 explicit AsC1VCompilation(const OptimizedCompilationInfo* info)
113 : info_(info) {}
114 const OptimizedCompilationInfo* info_;
115 };
116
117 struct AsScheduledGraph {
AsScheduledGraphAsScheduledGraph118 explicit AsScheduledGraph(const Schedule* schedule) : schedule(schedule) {}
119 const Schedule* schedule;
120 };
121
122 std::ostream& operator<<(std::ostream& os, const AsScheduledGraph& scheduled);
123 struct AsC1V {
124 AsC1V(const char* phase, const Schedule* schedule,
125 const SourcePositionTable* positions = nullptr,
126 const InstructionSequence* instructions = nullptr)
schedule_AsC1V127 : schedule_(schedule),
128 instructions_(instructions),
129 positions_(positions),
130 phase_(phase) {}
131 const Schedule* schedule_;
132 const InstructionSequence* instructions_;
133 const SourcePositionTable* positions_;
134 const char* phase_;
135 };
136
137 struct AsC1VRegisterAllocationData {
138 explicit AsC1VRegisterAllocationData(
139 const char* phase, const RegisterAllocationData* data = nullptr)
phase_AsC1VRegisterAllocationData140 : phase_(phase), data_(data) {}
141 const char* phase_;
142 const RegisterAllocationData* data_;
143 };
144
145 std::ostream& operator<<(std::ostream& os, const AsC1VCompilation& ac);
146 std::ostream& operator<<(std::ostream& os, const AsC1V& ac);
147 std::ostream& operator<<(std::ostream& os,
148 const AsC1VRegisterAllocationData& ac);
149
150 } // namespace compiler
151 } // namespace internal
152 } // namespace v8
153
154 #endif // V8_COMPILER_GRAPH_VISUALIZER_H_
155