summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/operator.h
blob: 4294d344fe9b33ffe949eebea1f01f30ea64d779 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// 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_OPERATOR_H_
#define V8_COMPILER_OPERATOR_H_

#include "src/v8.h"

#include "src/assembler.h"
#include "src/ostreams.h"
#include "src/unique.h"

namespace v8 {
namespace internal {
namespace compiler {

// An operator represents description of the "computation" of a node in the
// compiler IR. A computation takes values (i.e. data) as input and produces
// zero or more values as output. The side-effects of a computation must be
// captured by additional control and data dependencies which are part of the
// IR graph.
// Operators are immutable and describe the statically-known parts of a
// computation. Thus they can be safely shared by many different nodes in the
// IR graph, or even globally between graphs. Operators can have "static
// parameters" which are compile-time constant parameters to the operator, such
// as the name for a named field access, the ID of a runtime function, etc.
// Static parameters are private to the operator and only semantically
// meaningful to the operator itself.
class Operator : public ZoneObject {
 public:
  Operator(uint8_t opcode, uint16_t properties)
      : opcode_(opcode), properties_(properties) {}
  virtual ~Operator() {}

  // Properties inform the operator-independent optimizer about legal
  // transformations for nodes that have this operator.
  enum Property {
    kNoProperties = 0,
    kReducible = 1 << 0,    // Participates in strength reduction.
    kCommutative = 1 << 1,  // OP(a, b) == OP(b, a) for all inputs.
    kAssociative = 1 << 2,  // OP(a, OP(b,c)) == OP(OP(a,b), c) for all inputs.
    kIdempotent = 1 << 3,   // OP(a); OP(a) == OP(a).
    kNoRead = 1 << 4,       // Has no scheduling dependency on Effects
    kNoWrite = 1 << 5,      // Does not modify any Effects and thereby
                            // create new scheduling dependencies.
    kNoThrow = 1 << 6,      // Can never generate an exception.
    kFoldable = kNoRead | kNoWrite,
    kEliminatable = kNoWrite | kNoThrow,
    kPure = kNoRead | kNoWrite | kNoThrow | kIdempotent
  };

  // A small integer unique to all instances of a particular kind of operator,
  // useful for quick matching for specific kinds of operators. For fast access
  // the opcode is stored directly in the operator object.
  inline uint8_t opcode() const { return opcode_; }

  // Returns a constant string representing the mnemonic of the operator,
  // without the static parameters. Useful for debugging.
  virtual const char* mnemonic() = 0;

  // Check if this operator equals another operator. Equivalent operators can
  // be merged, and nodes with equivalent operators and equivalent inputs
  // can be merged.
  virtual bool Equals(Operator* other) = 0;

  // Compute a hashcode to speed up equivalence-set checking.
  // Equal operators should always have equal hashcodes, and unequal operators
  // should have unequal hashcodes with high probability.
  virtual int HashCode() = 0;

  // Check whether this operator has the given property.
  inline bool HasProperty(Property property) const {
    return (properties_ & static_cast<int>(property)) == property;
  }

  // Number of data inputs to the operator, for verifying graph structure.
  virtual int InputCount() = 0;

  // Number of data outputs from the operator, for verifying graph structure.
  virtual int OutputCount() = 0;

  inline Property properties() { return static_cast<Property>(properties_); }

  // TODO(titzer): API for input and output types, for typechecking graph.
 private:
  // Print the full operator into the given stream, including any
  // static parameters. Useful for debugging and visualizing the IR.
  virtual OStream& PrintTo(OStream& os) const = 0;  // NOLINT
  friend OStream& operator<<(OStream& os, const Operator& op);

  uint8_t opcode_;
  uint16_t properties_;
};

OStream& operator<<(OStream& os, const Operator& op);

// An implementation of Operator that has no static parameters. Such operators
// have just a name, an opcode, and a fixed number of inputs and outputs.
// They can represented by singletons and shared globally.
class SimpleOperator : public Operator {
 public:
  SimpleOperator(uint8_t opcode, uint16_t properties, int input_count,
                 int output_count, const char* mnemonic)
      : Operator(opcode, properties),
        input_count_(input_count),
        output_count_(output_count),
        mnemonic_(mnemonic) {}

  virtual const char* mnemonic() { return mnemonic_; }
  virtual bool Equals(Operator* that) { return opcode() == that->opcode(); }
  virtual int HashCode() { return opcode(); }
  virtual int InputCount() { return input_count_; }
  virtual int OutputCount() { return output_count_; }

 private:
  virtual OStream& PrintTo(OStream& os) const {  // NOLINT
    return os << mnemonic_;
  }

  int input_count_;
  int output_count_;
  const char* mnemonic_;
};

// Template specialization implements a kind of type class for dealing with the
// static parameters of Operator1 automatically.
template <typename T>
struct StaticParameterTraits {
  static OStream& PrintTo(OStream& os, T val) {  // NOLINT
    return os << "??";
  }
  static int HashCode(T a) { return 0; }
  static bool Equals(T a, T b) {
    return false;  // Not every T has a ==. By default, be conservative.
  }
};

template <>
struct StaticParameterTraits<ExternalReference> {
  static OStream& PrintTo(OStream& os, ExternalReference val) {  // NOLINT
    os << val.address();
    const Runtime::Function* function =
        Runtime::FunctionForEntry(val.address());
    if (function != NULL) {
      os << " <" << function->name << ".entry>";
    }
    return os;
  }
  static int HashCode(ExternalReference a) {
    return reinterpret_cast<intptr_t>(a.address()) & 0xFFFFFFFF;
  }
  static bool Equals(ExternalReference a, ExternalReference b) {
    return a == b;
  }
};

// Specialization for static parameters of type {int}.
template <>
struct StaticParameterTraits<int> {
  static OStream& PrintTo(OStream& os, int val) {  // NOLINT
    return os << val;
  }
  static int HashCode(int a) { return a; }
  static bool Equals(int a, int b) { return a == b; }
};

// Specialization for static parameters of type {double}.
template <>
struct StaticParameterTraits<double> {
  static OStream& PrintTo(OStream& os, double val) {  // NOLINT
    return os << val;
  }
  static int HashCode(double a) {
    return static_cast<int>(BitCast<int64_t>(a));
  }
  static bool Equals(double a, double b) {
    return BitCast<int64_t>(a) == BitCast<int64_t>(b);
  }
};

// Specialization for static parameters of type {PrintableUnique<Object>}.
template <>
struct StaticParameterTraits<PrintableUnique<Object> > {
  static OStream& PrintTo(OStream& os, PrintableUnique<Object> val) {  // NOLINT
    return os << val.string();
  }
  static int HashCode(PrintableUnique<Object> a) {
    return static_cast<int>(a.Hashcode());
  }
  static bool Equals(PrintableUnique<Object> a, PrintableUnique<Object> b) {
    return a == b;
  }
};

// Specialization for static parameters of type {PrintableUnique<Name>}.
template <>
struct StaticParameterTraits<PrintableUnique<Name> > {
  static OStream& PrintTo(OStream& os, PrintableUnique<Name> val) {  // NOLINT
    return os << val.string();
  }
  static int HashCode(PrintableUnique<Name> a) {
    return static_cast<int>(a.Hashcode());
  }
  static bool Equals(PrintableUnique<Name> a, PrintableUnique<Name> b) {
    return a == b;
  }
};

#if DEBUG
// Specialization for static parameters of type {Handle<Object>} to prevent any
// direct usage of Handles in constants.
template <>
struct StaticParameterTraits<Handle<Object> > {
  static OStream& PrintTo(OStream& os, Handle<Object> val) {  // NOLINT
    UNREACHABLE();  // Should use PrintableUnique<Object> instead
    return os;
  }
  static int HashCode(Handle<Object> a) {
    UNREACHABLE();  // Should use PrintableUnique<Object> instead
    return 0;
  }
  static bool Equals(Handle<Object> a, Handle<Object> b) {
    UNREACHABLE();  // Should use PrintableUnique<Object> instead
    return false;
  }
};
#endif

// A templatized implementation of Operator that has one static parameter of
// type {T}. If a specialization of StaticParameterTraits<{T}> exists, then
// operators of this kind can automatically be hashed, compared, and printed.
template <typename T>
class Operator1 : public Operator {
 public:
  Operator1(uint8_t opcode, uint16_t properties, int input_count,
            int output_count, const char* mnemonic, T parameter)
      : Operator(opcode, properties),
        input_count_(input_count),
        output_count_(output_count),
        mnemonic_(mnemonic),
        parameter_(parameter) {}

  const T& parameter() const { return parameter_; }

  virtual const char* mnemonic() { return mnemonic_; }
  virtual bool Equals(Operator* other) {
    if (opcode() != other->opcode()) return false;
    Operator1<T>* that = static_cast<Operator1<T>*>(other);
    T temp1 = this->parameter_;
    T temp2 = that->parameter_;
    return StaticParameterTraits<T>::Equals(temp1, temp2);
  }
  virtual int HashCode() {
    return opcode() + 33 * StaticParameterTraits<T>::HashCode(this->parameter_);
  }
  virtual int InputCount() { return input_count_; }
  virtual int OutputCount() { return output_count_; }
  virtual OStream& PrintParameter(OStream& os) const {  // NOLINT
    return StaticParameterTraits<T>::PrintTo(os << "[", parameter_) << "]";
  }

 private:
  virtual OStream& PrintTo(OStream& os) const {  // NOLINT
    return PrintParameter(os << mnemonic_);
  }

  int input_count_;
  int output_count_;
  const char* mnemonic_;
  T parameter_;
};

// Type definitions for operators with specific types of parameters.
typedef Operator1<PrintableUnique<Name> > NameOperator;
}
}
}  // namespace v8::internal::compiler

#endif  // V8_COMPILER_OPERATOR_H_