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