v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
operator.h
Go to the documentation of this file.
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_OPERATOR_H_
6#define V8_COMPILER_OPERATOR_H_
7
8#include <ostream>
9
11#include "src/base/flags.h"
12#include "src/base/hashing.h"
13#include "src/common/globals.h"
14#include "src/handles/handles.h"
15#include "src/zone/zone.h"
16
17namespace v8 {
18namespace internal {
19namespace compiler {
20
21// An operator represents description of the "computation" of a node in the
22// compiler IR. A computation takes values (i.e. data) as input and produces
23// zero or more values as output. The side-effects of a computation must be
24// captured by additional control and data dependencies which are part of the
25// IR graph.
26// Operators are immutable and describe the statically-known parts of a
27// computation. Thus they can be safely shared by many different nodes in the
28// IR graph, or even globally between graphs. Operators can have "static
29// parameters" which are compile-time constant parameters to the operator, such
30// as the name for a named field access, the ID of a runtime function, etc.
31// Static parameters are private to the operator and only semantically
32// meaningful to the operator itself.
33class V8_EXPORT_PRIVATE Operator : public NON_EXPORTED_BASE(ZoneObject) {
34 public:
35 using Opcode = uint16_t;
36
37 // Properties inform the operator-independent optimizer about legal
38 // transformations for nodes that have this operator.
39 enum Property {
40 kNoProperties = 0,
41 kCommutative = 1 << 0, // OP(a, b) == OP(b, a) for all inputs.
42 kAssociative = 1 << 1, // OP(a, OP(b,c)) == OP(OP(a,b), c) for all inputs.
43 kIdempotent = 1 << 2, // OP(a); OP(a) == OP(a).
44 kNoRead = 1 << 3, // Has no scheduling dependency on Effects
45 kNoWrite = 1 << 4, // Does not modify any Effects and thereby
46 // create new scheduling dependencies.
47 kNoThrow = 1 << 5, // Can never generate an exception.
48 kNoDeopt = 1 << 6, // Can never generate an eager deoptimization exit.
49 kFoldable = kNoRead | kNoWrite,
50 kEliminatable = kNoDeopt | kNoWrite | kNoThrow,
51 kKontrol = kNoDeopt | kFoldable | kNoThrow,
52 kPure = kKontrol | kIdempotent
53 };
54
55// List of all bits, for the visualizer.
56#define OPERATOR_PROPERTY_LIST(V) \
57 V(Commutative) \
58 V(Associative) V(Idempotent) V(NoRead) V(NoWrite) V(NoThrow) V(NoDeopt)
59
61 enum class PrintVerbosity { kVerbose, kSilent };
62
63 // Constructor.
64 Operator(Opcode opcode, Properties properties, const char* mnemonic,
65 size_t value_in, size_t effect_in, size_t control_in,
66 size_t value_out, size_t effect_out, size_t control_out);
67 Operator(const Operator&) = delete;
68 Operator& operator=(const Operator&) = delete;
69
70 virtual ~Operator() = default;
71
72 // A small integer unique to all instances of a particular kind of operator,
73 // useful for quick matching for specific kinds of operators. For fast access
74 // the opcode is stored directly in the operator object.
75 constexpr Opcode opcode() const { return opcode_; }
76
77 // Returns a constant string representing the mnemonic of the operator,
78 // without the static parameters. Useful for debugging.
79 const char* mnemonic() const { return mnemonic_; }
80
81 // Check if this operator equals another operator. Equivalent operators can
82 // be merged, and nodes with equivalent operators and equivalent inputs
83 // can be merged.
84 virtual bool Equals(const Operator* that) const {
85 return this->opcode() == that->opcode();
86 }
87
88 // Compute a hashcode to speed up equivalence-set checking.
89 // Equal operators should always have equal hashcodes, and unequal operators
90 // should have unequal hashcodes with high probability.
91 virtual size_t HashCode() const { return base::hash<Opcode>()(opcode()); }
92
93 // Check whether this operator has the given property.
94 bool HasProperty(Property property) const {
95 return (properties() & property) == property;
96 }
97
98 Properties properties() const { return properties_; }
99
100 // TODO(titzer): convert return values here to size_t.
101 int ValueInputCount() const { return value_in_; }
102 int EffectInputCount() const { return effect_in_; }
103 int ControlInputCount() const { return control_in_; }
104
105 int ValueOutputCount() const { return value_out_; }
106 int EffectOutputCount() const { return effect_out_; }
107 int ControlOutputCount() const { return control_out_; }
108
109 static size_t ZeroIfEliminatable(Properties properties) {
110 return (properties & kEliminatable) == kEliminatable ? 0 : 1;
111 }
112
113 static size_t ZeroIfNoThrow(Properties properties) {
114 return (properties & kNoThrow) == kNoThrow ? 0 : 2;
115 }
116
117 static size_t ZeroIfPure(Properties properties) {
118 return (properties & kPure) == kPure ? 0 : 1;
119 }
120
121 // TODO(titzer): API for input and output types, for typechecking graph.
122
123 // Print the full operator into the given stream, including any
124 // static parameters. Useful for debugging and visualizing the IR.
125 void PrintTo(std::ostream& os,
126 PrintVerbosity verbose = PrintVerbosity::kVerbose) const {
127 // We cannot make PrintTo virtual, because default arguments to virtual
128 // methods are banned in the style guide.
129 return PrintToImpl(os, verbose);
130 }
131
132 void PrintPropsTo(std::ostream& os) const;
133
134 protected:
135 virtual void PrintToImpl(std::ostream& os, PrintVerbosity verbose) const;
136
137 private:
138 const char* mnemonic_;
141 uint32_t value_in_;
142 uint32_t effect_in_;
143 uint32_t control_in_;
144 uint32_t value_out_;
145 uint8_t effect_out_;
146 uint32_t control_out_;
147};
148
150
151V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
152 const Operator& op);
153
154// Default equality function for below Operator1<*> class.
155template <typename T>
156struct OpEqualTo : public std::equal_to<T> {};
157
158
159// Default hashing function for below Operator1<*> class.
160template <typename T>
161struct OpHash : public base::hash<T> {};
162
163
164// A templatized implementation of Operator that has one static parameter of
165// type {T} with the proper default equality and hashing functions.
166template <typename T, typename Pred = OpEqualTo<T>, typename Hash = OpHash<T>>
167class Operator1 : public Operator {
168 public:
169 Operator1(Opcode opcode, Properties properties, const char* mnemonic,
170 size_t value_in, size_t effect_in, size_t control_in,
171 size_t value_out, size_t effect_out, size_t control_out,
172 T parameter, Pred const& pred = Pred(), Hash const& hash = Hash())
173 : Operator(opcode, properties, mnemonic, value_in, effect_in, control_in,
174 value_out, effect_out, control_out),
176 pred_(pred),
177 hash_(hash) {}
178
179 T const& parameter() const { return parameter_; }
180
181 bool Equals(const Operator* other) const final {
182 if (opcode() != other->opcode()) return false;
183 const Operator1<T, Pred, Hash>* that =
184 reinterpret_cast<const Operator1<T, Pred, Hash>*>(other);
185 return this->pred_(this->parameter(), that->parameter());
186 }
187 size_t HashCode() const final {
188 return base::hash_combine(this->opcode(), this->hash_(this->parameter()));
189 }
190 // For most parameter types, we have only a verbose way to print them, namely
191 // ostream << parameter. But for some types it is particularly useful to have
192 // a shorter way to print them for the node labels in Turbolizer. The
193 // following method can be overridden to provide a concise and a verbose
194 // printing of a parameter.
195
196 virtual void PrintParameter(std::ostream& os, PrintVerbosity verbose) const {
197 os << "[" << parameter() << "]";
198 }
199
200 void PrintToImpl(std::ostream& os, PrintVerbosity verbose) const override {
201 os << mnemonic();
202 PrintParameter(os, verbose);
203 }
204
205 private:
206 T const parameter_;
207 Pred const pred_;
208 Hash const hash_;
209};
210
211
212// Helper to extract parameters from Operator1<*> operator.
213template <typename T>
214inline T const& OpParameter(const Operator* op) {
215 return reinterpret_cast<const Operator1<T, OpEqualTo<T>, OpHash<T>>*>(op)
216 ->parameter();
217}
218
219
220// NOTE: We have to be careful to use the right equal/hash functions below, for
221// float/double we always use the ones operating on the bit level, for Handle<>
222// we always use the ones operating on the location level.
223template <>
224struct OpEqualTo<float> : public base::bit_equal_to<float> {};
225template <>
226struct OpHash<float> : public base::bit_hash<float> {};
227
228template <>
229struct OpEqualTo<double> : public base::bit_equal_to<double> {};
230template <>
231struct OpHash<double> : public base::bit_hash<double> {};
232
233template <class T>
234struct OpEqualTo<IndirectHandle<T>> : public IndirectHandle<T>::equal_to {};
235template <class T>
236struct OpHash<IndirectHandle<T>> : public IndirectHandle<T>::hash {};
237
238} // namespace compiler
239} // namespace internal
240} // namespace v8
241
242#endif // V8_COMPILER_OPERATOR_H_
#define DEFINE_OPERATORS_FOR_FLAGS(Type)
Definition flags.h:100
virtual void PrintParameter(std::ostream &os, PrintVerbosity verbose) const
Definition operator.h:196
void PrintToImpl(std::ostream &os, PrintVerbosity verbose) const override
Definition operator.h:200
size_t HashCode() const final
Definition operator.h:187
Operator1(Opcode opcode, Properties properties, const char *mnemonic, size_t value_in, size_t effect_in, size_t control_in, size_t value_out, size_t effect_out, size_t control_out, T parameter, Pred const &pred=Pred(), Hash const &hash=Hash())
Definition operator.h:169
bool Equals(const Operator *other) const final
Definition operator.h:181
const char * mnemonic() const
Definition operator.h:79
Properties properties() const
Definition operator.h:98
static size_t ZeroIfNoThrow(Properties properties)
Definition operator.h:113
Operator(const Operator &)=delete
bool HasProperty(Property property) const
Definition operator.h:94
void PrintTo(std::ostream &os, PrintVerbosity verbose=PrintVerbosity::kVerbose) const
Definition operator.h:125
static size_t ZeroIfEliminatable(Properties properties)
Definition operator.h:109
static size_t ZeroIfPure(Properties properties)
Definition operator.h:117
Operator & operator=(const Operator &)=delete
virtual size_t HashCode() const
Definition operator.h:91
constexpr Opcode opcode() const
Definition operator.h:75
virtual bool Equals(const Operator *that) const
Definition operator.h:84
ArchOpcode opcode_
const std::string property
V8_INLINE size_t hash_combine(size_t seed, size_t hash)
Definition hashing.h:77
T const & OpParameter(const Operator *op)
Definition operator.h:214
std::ostream & operator<<(std::ostream &os, AccessMode access_mode)
static uint32_t Hash(RegisteredExtension *extension)
#define NON_EXPORTED_BASE(code)
#define V8_EXPORT_PRIVATE
Definition macros.h:460