Line data Source code
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 Instruction;
26 : class InstructionBlock;
27 : class InstructionOperand;
28 : class InstructionSequence;
29 : class NodeOrigin;
30 : class NodeOriginTable;
31 : class RegisterAllocationData;
32 : class Schedule;
33 : class SourcePositionTable;
34 :
35 : struct TurboJsonFile : public std::ofstream {
36 : TurboJsonFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode);
37 : ~TurboJsonFile() override;
38 : };
39 :
40 : struct TurboCfgFile : public std::ofstream {
41 : explicit TurboCfgFile(Isolate* isolate = nullptr);
42 : ~TurboCfgFile() override;
43 : };
44 :
45 : struct SourcePositionAsJSON {
46 : explicit SourcePositionAsJSON(const SourcePosition& sp) : sp(sp) {}
47 : const SourcePosition& sp;
48 : };
49 :
50 : V8_INLINE V8_EXPORT_PRIVATE SourcePositionAsJSON
51 : AsJSON(const SourcePosition& sp) {
52 : return SourcePositionAsJSON(sp);
53 : }
54 :
55 : struct NodeOriginAsJSON {
56 : explicit NodeOriginAsJSON(const NodeOrigin& no) : no(no) {}
57 : const NodeOrigin& no;
58 : };
59 :
60 : V8_INLINE V8_EXPORT_PRIVATE NodeOriginAsJSON AsJSON(const NodeOrigin& no) {
61 : return NodeOriginAsJSON(no);
62 : }
63 :
64 : std::ostream& operator<<(std::ostream& out, const SourcePositionAsJSON& pos);
65 :
66 : // Small helper that deduplicates SharedFunctionInfos.
67 12 : class SourceIdAssigner {
68 : public:
69 6 : explicit SourceIdAssigner(size_t size) {
70 6 : printed_.reserve(size);
71 6 : source_ids_.reserve(size);
72 6 : }
73 : int GetIdFor(Handle<SharedFunctionInfo> shared);
74 10 : int GetIdAt(size_t pos) const { return source_ids_[pos]; }
75 :
76 : private:
77 : std::vector<Handle<SharedFunctionInfo>> printed_;
78 : std::vector<int> source_ids_;
79 : };
80 :
81 : void JsonPrintAllSourceWithPositions(std::ostream& os,
82 : OptimizedCompilationInfo* info,
83 : Isolate* isolate);
84 :
85 : void JsonPrintFunctionSource(std::ostream& os, int source_id,
86 : std::unique_ptr<char[]> function_name,
87 : Handle<Script> script, Isolate* isolate,
88 : Handle<SharedFunctionInfo> shared,
89 : bool with_key = false);
90 : std::unique_ptr<char[]> GetVisualizerLogFileName(OptimizedCompilationInfo* info,
91 : const char* optional_base_dir,
92 : const char* phase,
93 : const char* suffix);
94 :
95 : struct GraphAsJSON {
96 : GraphAsJSON(const Graph& g, SourcePositionTable* p, NodeOriginTable* o)
97 42 : : graph(g), positions(p), origins(o) {}
98 : const Graph& graph;
99 : const SourcePositionTable* positions;
100 : const NodeOriginTable* origins;
101 : };
102 :
103 : V8_INLINE V8_EXPORT_PRIVATE GraphAsJSON AsJSON(const Graph& g,
104 : SourcePositionTable* p,
105 : NodeOriginTable* o) {
106 : return GraphAsJSON(g, p, o);
107 : }
108 :
109 : V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
110 : const GraphAsJSON& ad);
111 :
112 : struct AsRPO {
113 17 : explicit AsRPO(const Graph& g) : graph(g) {}
114 : const Graph& graph;
115 : };
116 :
117 : V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsRPO& ad);
118 :
119 : struct AsC1VCompilation {
120 : explicit AsC1VCompilation(const OptimizedCompilationInfo* info)
121 1 : : info_(info) {}
122 : const OptimizedCompilationInfo* info_;
123 : };
124 :
125 : struct AsScheduledGraph {
126 0 : explicit AsScheduledGraph(const Schedule* schedule) : schedule(schedule) {}
127 : const Schedule* schedule;
128 : };
129 :
130 : std::ostream& operator<<(std::ostream& os, const AsScheduledGraph& scheduled);
131 : struct AsC1V {
132 : AsC1V(const char* phase, const Schedule* schedule,
133 : const SourcePositionTable* positions = nullptr,
134 : const InstructionSequence* instructions = nullptr)
135 : : schedule_(schedule),
136 : instructions_(instructions),
137 : positions_(positions),
138 1 : phase_(phase) {}
139 : const Schedule* schedule_;
140 : const InstructionSequence* instructions_;
141 : const SourcePositionTable* positions_;
142 : const char* phase_;
143 : };
144 :
145 : struct AsC1VRegisterAllocationData {
146 : explicit AsC1VRegisterAllocationData(
147 : const char* phase, const RegisterAllocationData* data = nullptr)
148 3 : : phase_(phase), data_(data) {}
149 : const char* phase_;
150 : const RegisterAllocationData* data_;
151 : };
152 :
153 : std::ostream& operator<<(std::ostream& os, const AsC1VCompilation& ac);
154 : std::ostream& operator<<(std::ostream& os, const AsC1V& ac);
155 : std::ostream& operator<<(std::ostream& os,
156 : const AsC1VRegisterAllocationData& ac);
157 :
158 : struct InstructionOperandAsJSON {
159 : const InstructionOperand* op_;
160 : const InstructionSequence* code_;
161 : };
162 :
163 : std::ostream& operator<<(std::ostream& os, const InstructionOperandAsJSON& o);
164 :
165 : struct InstructionAsJSON {
166 : int index_;
167 : const Instruction* instr_;
168 : const InstructionSequence* code_;
169 : };
170 : std::ostream& operator<<(std::ostream& os, const InstructionAsJSON& i);
171 :
172 : struct InstructionBlockAsJSON {
173 : const InstructionBlock* block_;
174 : const InstructionSequence* code_;
175 : };
176 :
177 : std::ostream& operator<<(std::ostream& os, const InstructionBlockAsJSON& b);
178 :
179 : struct InstructionSequenceAsJSON {
180 : const InstructionSequence* sequence_;
181 : };
182 : std::ostream& operator<<(std::ostream& os, const InstructionSequenceAsJSON& s);
183 :
184 : } // namespace compiler
185 : } // namespace internal
186 : } // namespace v8
187 :
188 : #endif // V8_COMPILER_GRAPH_VISUALIZER_H_
|