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