graph-visualizer.h 6.27 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_GRAPH_VISUALIZER_H_
#define V8_COMPILER_GRAPH_VISUALIZER_H_

ben's avatar
ben committed
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 18
namespace v8 {
namespace internal {
19

20
class OptimizedCompilationInfo;
21 22
class SharedFunctionInfo;
class SourcePosition;
23 24 25
namespace compiler {

class Graph;
26 27
class LiveRange;
class TopLevelLiveRange;
28 29 30
class Instruction;
class InstructionBlock;
class InstructionOperand;
31
class InstructionSequence;
32 33
class NodeOrigin;
class NodeOriginTable;
34
class RegisterAllocationData;
35 36 37
class Schedule;
class SourcePositionTable;

38 39
struct TurboJsonFile : public std::ofstream {
  TurboJsonFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode);
40
  ~TurboJsonFile() override;
41 42
};

43 44 45 46 47
struct TurboCfgFile : public std::ofstream {
  explicit TurboCfgFile(Isolate* isolate = nullptr);
  ~TurboCfgFile() override;
};

48 49 50 51 52 53 54 55 56 57
struct SourcePositionAsJSON {
  explicit SourcePositionAsJSON(const SourcePosition& sp) : sp(sp) {}
  const SourcePosition& sp;
};

V8_INLINE V8_EXPORT_PRIVATE SourcePositionAsJSON
AsJSON(const SourcePosition& sp) {
  return SourcePositionAsJSON(sp);
}

58 59 60 61 62 63 64 65 66
struct NodeOriginAsJSON {
  explicit NodeOriginAsJSON(const NodeOrigin& no) : no(no) {}
  const NodeOrigin& no;
};

V8_INLINE V8_EXPORT_PRIVATE NodeOriginAsJSON AsJSON(const NodeOrigin& no) {
  return NodeOriginAsJSON(no);
}

67 68 69
std::ostream& operator<<(std::ostream& out, const SourcePositionAsJSON& pos);

// Small helper that deduplicates SharedFunctionInfos.
70
class V8_EXPORT_PRIVATE SourceIdAssigner {
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 public:
  explicit SourceIdAssigner(size_t size) {
    printed_.reserve(size);
    source_ids_.reserve(size);
  }
  int GetIdFor(Handle<SharedFunctionInfo> shared);
  int GetIdAt(size_t pos) const { return source_ids_[pos]; }

 private:
  std::vector<Handle<SharedFunctionInfo>> printed_;
  std::vector<int> source_ids_;
};

void JsonPrintAllSourceWithPositions(std::ostream& os,
                                     OptimizedCompilationInfo* info,
                                     Isolate* isolate);

void JsonPrintFunctionSource(std::ostream& os, int source_id,
                             std::unique_ptr<char[]> function_name,
                             Handle<Script> script, Isolate* isolate,
                             Handle<SharedFunctionInfo> shared,
                             bool with_key = false);
93
std::unique_ptr<char[]> GetVisualizerLogFileName(OptimizedCompilationInfo* info,
94
                                                 const char* optional_base_dir,
95 96
                                                 const char* phase,
                                                 const char* suffix);
97

98
struct GraphAsJSON {
99 100
  GraphAsJSON(const Graph& g, SourcePositionTable* p, NodeOriginTable* o)
      : graph(g), positions(p), origins(o) {}
101
  const Graph& graph;
danno's avatar
danno committed
102
  const SourcePositionTable* positions;
103
  const NodeOriginTable* origins;
104 105
};

106
V8_INLINE V8_EXPORT_PRIVATE GraphAsJSON AsJSON(const Graph& g,
107 108 109
                                               SourcePositionTable* p,
                                               NodeOriginTable* o) {
  return GraphAsJSON(g, p, o);
110 111 112 113
}

V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const GraphAsJSON& ad);
114

115 116 117 118 119
struct AsRPO {
  explicit AsRPO(const Graph& g) : graph(g) {}
  const Graph& graph;
};

120
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, const AsRPO& ad);
121

122
struct AsC1VCompilation {
123 124 125
  explicit AsC1VCompilation(const OptimizedCompilationInfo* info)
      : info_(info) {}
  const OptimizedCompilationInfo* info_;
126 127
};

128 129 130 131
struct AsScheduledGraph {
  explicit AsScheduledGraph(const Schedule* schedule) : schedule(schedule) {}
  const Schedule* schedule;
};
132

133
std::ostream& operator<<(std::ostream& os, const AsScheduledGraph& scheduled);
134 135
struct AsC1V {
  AsC1V(const char* phase, const Schedule* schedule,
136 137
        const SourcePositionTable* positions = nullptr,
        const InstructionSequence* instructions = nullptr)
138 139 140 141 142 143 144 145 146 147
      : schedule_(schedule),
        instructions_(instructions),
        positions_(positions),
        phase_(phase) {}
  const Schedule* schedule_;
  const InstructionSequence* instructions_;
  const SourcePositionTable* positions_;
  const char* phase_;
};

148 149 150 151
struct AsC1VRegisterAllocationData {
  explicit AsC1VRegisterAllocationData(
      const char* phase, const RegisterAllocationData* data = nullptr)
      : phase_(phase), data_(data) {}
152
  const char* phase_;
153
  const RegisterAllocationData* data_;
154 155 156 157
};

std::ostream& operator<<(std::ostream& os, const AsC1VCompilation& ac);
std::ostream& operator<<(std::ostream& os, const AsC1V& ac);
158 159
std::ostream& operator<<(std::ostream& os,
                         const AsC1VRegisterAllocationData& ac);
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
struct LiveRangeAsJSON {
  const LiveRange& range_;
  const InstructionSequence& code_;
};

std::ostream& operator<<(std::ostream& os,
                         const LiveRangeAsJSON& live_range_json);

struct TopLevelLiveRangeAsJSON {
  const TopLevelLiveRange& range_;
  const InstructionSequence& code_;
};

std::ostream& operator<<(
    std::ostream& os, const TopLevelLiveRangeAsJSON& top_level_live_range_json);

struct RegisterAllocationDataAsJSON {
  const RegisterAllocationData& data_;
  const InstructionSequence& code_;
};

std::ostream& operator<<(std::ostream& os,
                         const RegisterAllocationDataAsJSON& ac);

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
struct InstructionOperandAsJSON {
  const InstructionOperand* op_;
  const InstructionSequence* code_;
};

std::ostream& operator<<(std::ostream& os, const InstructionOperandAsJSON& o);

struct InstructionAsJSON {
  int index_;
  const Instruction* instr_;
  const InstructionSequence* code_;
};
std::ostream& operator<<(std::ostream& os, const InstructionAsJSON& i);

struct InstructionBlockAsJSON {
  const InstructionBlock* block_;
  const InstructionSequence* code_;
};

std::ostream& operator<<(std::ostream& os, const InstructionBlockAsJSON& b);

struct InstructionSequenceAsJSON {
  const InstructionSequence* sequence_;
};
std::ostream& operator<<(std::ostream& os, const InstructionSequenceAsJSON& s);

211 212 213
}  // namespace compiler
}  // namespace internal
}  // namespace v8
214 215

#endif  // V8_COMPILER_GRAPH_VISUALIZER_H_