summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/osr.h
blob: 1f562c56bf8d0661052018e22f79b138cd505887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2014 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_OSR_H_
#define V8_COMPILER_OSR_H_

#include "src/zone/zone.h"

// TurboFan structures OSR graphs in a way that separates almost all phases of
// compilation from OSR implementation details. This is accomplished with
// special control nodes that are added at graph building time. In particular,
// the graph is built in such a way that typing still computes the best types
// and optimizations and lowering work unchanged. All that remains is to
// deconstruct the OSR artifacts before scheduling and code generation.

// Graphs built for OSR from the AstGraphBuilder are structured as follows:
//                             Start
//          +-------------------^^-----+
//          |                          |
//   OsrNormalEntry               OsrLoopEntry <-------------+
//          |                          |                     |
//   control flow before loop          |           A     OsrValue
//          |                          |           |         |
//          | +------------------------+           | +-------+
//          | | +-------------+                    | | +--------+
//          | | |             |                    | | |        |
//        ( Loop )<-----------|------------------ ( phi )       |
//          |                 |                                 |
//      loop body             | backedge(s)                     |
//          |  |              |                                 |
//          |  +--------------+                        B  <-----+
//          |
//         end

// The control structure expresses the relationship that the loop has a separate
// entrypoint which corresponds to entering the loop directly from the middle
// of unoptimized code.
// Similarly, the values that come in from unoptimized code are represented with
// {OsrValue} nodes that merge into any phis associated with the OSR loop.
// In the above diagram, nodes {A} and {B} represent values in the "normal"
// graph that correspond to the values of those phis before the loop and on any
// backedges, respectively.

// To deconstruct OSR, we simply replace the uses of the {OsrNormalEntry}
// control node with {Dead} and {OsrLoopEntry} with start and run the
// {ControlReducer}. Control reduction propagates the dead control forward,
// essentially "killing" all the code before the OSR loop. The entrypoint to the
// loop corresponding to the "normal" entry path will also be removed, as well
// as the inputs to the loop phis, resulting in the reduced graph:

//                             Start
//         Dead                  |^-------------------------+
//          |                    |                          |
//          |                    |                          |
//          |                    |                          |
//    disconnected, dead         |           A=dead      OsrValue
//                               |                          |
//            +------------------+                   +------+
//            | +-------------+                      | +--------+
//            | |             |                      | |        |
//        ( Loop )<-----------|------------------ ( phi )       |
//          |                 |                                 |
//      loop body             | backedge(s)                     |
//          |  |              |                                 |
//          |  +--------------+                        B  <-----+
//          |
//         end

// Other than the presences of the OsrValue nodes, this is a normal, schedulable
// graph. OsrValue nodes are handled specially in the instruction selector to
// simply load from the unoptimized frame.

// For nested OSR loops, loop peeling must first be applied as many times as
// necessary in order to bring the OSR loop up to the top level (i.e. to be
// an outer loop).

namespace v8 {
namespace internal {

class CompilationInfo;

namespace compiler {

class JSGraph;
class CommonOperatorBuilder;
class Frame;
class Linkage;

// Encapsulates logic relating to OSR compilations as well has handles some
// details of the frame layout.
class OsrHelper {
 public:
  explicit OsrHelper(CompilationInfo* info);
  // Only for testing.
  OsrHelper(size_t parameter_count, size_t stack_slot_count)
      : parameter_count_(parameter_count),
        stack_slot_count_(stack_slot_count) {}

  // Deconstructs the artificial {OsrNormalEntry} and rewrites the graph so
  // that only the path corresponding to {OsrLoopEntry} remains.
  void Deconstruct(JSGraph* jsgraph, CommonOperatorBuilder* common,
                   Zone* tmp_zone);

  // Prepares the frame w.r.t. OSR.
  void SetupFrame(Frame* frame);

  // Returns the number of unoptimized frame slots for this OSR.
  size_t UnoptimizedFrameSlots() { return stack_slot_count_; }

  // Returns the environment index of the first stack slot.
  static int FirstStackSlotIndex(int parameter_count) {
    // n.b. unlike Crankshaft, TurboFan environments do not contain the context.
    return 1 + parameter_count;  // receiver + params
  }

 private:
  size_t parameter_count_;
  size_t stack_slot_count_;
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_OSR_H_