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