OLD | NEW |
(Empty) | |
| 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 "src/v8.h" |
| 9 |
| 10 #include "src/assembler.h" |
| 11 #include "src/ostreams.h" |
| 12 #include "src/unique.h" |
| 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 Operator(uint8_t opcode, uint16_t properties) |
| 33 : opcode_(opcode), properties_(properties) {} |
| 34 virtual ~Operator() {} |
| 35 |
| 36 // Properties inform the operator-independent optimizer about legal |
| 37 // transformations for nodes that have this operator. |
| 38 enum Property { |
| 39 kNoProperties = 0, |
| 40 kReducible = 1 << 0, // Participates in strength reduction. |
| 41 kCommutative = 1 << 1, // OP(a, b) == OP(b, a) for all inputs. |
| 42 kAssociative = 1 << 2, // OP(a, OP(b,c)) == OP(OP(a,b), c) for all inputs. |
| 43 kIdempotent = 1 << 3, // OP(a); OP(a) == OP(a). |
| 44 kNoRead = 1 << 4, // Has no scheduling dependency on Effects |
| 45 kNoWrite = 1 << 5, // Does not modify any Effects and thereby |
| 46 // create new scheduling dependencies. |
| 47 kNoThrow = 1 << 6, // Can never generate an exception. |
| 48 kFoldable = kNoRead | kNoWrite, |
| 49 kEliminatable = kNoWrite | kNoThrow, |
| 50 kPure = kNoRead | kNoWrite | kNoThrow | kIdempotent |
| 51 }; |
| 52 |
| 53 // A small integer unique to all instances of a particular kind of operator, |
| 54 // useful for quick matching for specific kinds of operators. For fast access |
| 55 // the opcode is stored directly in the operator object. |
| 56 inline uint8_t opcode() const { return opcode_; } |
| 57 |
| 58 // Returns a constant string representing the mnemonic of the operator, |
| 59 // without the static parameters. Useful for debugging. |
| 60 virtual const char* mnemonic() = 0; |
| 61 |
| 62 // Check if this operator equals another operator. Equivalent operators can |
| 63 // be merged, and nodes with equivalent operators and equivalent inputs |
| 64 // can be merged. |
| 65 virtual bool Equals(Operator* other) = 0; |
| 66 |
| 67 // Compute a hashcode to speed up equivalence-set checking. |
| 68 // Equal operators should always have equal hashcodes, and unequal operators |
| 69 // should have unequal hashcodes with high probability. |
| 70 virtual int HashCode() = 0; |
| 71 |
| 72 // Check whether this operator has the given property. |
| 73 inline bool HasProperty(Property property) const { |
| 74 return (properties_ & static_cast<int>(property)) == property; |
| 75 } |
| 76 |
| 77 // Number of data inputs to the operator, for verifying graph structure. |
| 78 virtual int InputCount() = 0; |
| 79 |
| 80 // Number of data outputs from the operator, for verifying graph structure. |
| 81 virtual int OutputCount() = 0; |
| 82 |
| 83 inline Property properties() { return static_cast<Property>(properties_); } |
| 84 |
| 85 // TODO(titzer): API for input and output types, for typechecking graph. |
| 86 private: |
| 87 // Print the full operator into the given stream, including any |
| 88 // static parameters. Useful for debugging and visualizing the IR. |
| 89 virtual OStream& PrintTo(OStream& os) const = 0; // NOLINT |
| 90 friend OStream& operator<<(OStream& os, const Operator& op); |
| 91 |
| 92 uint8_t opcode_; |
| 93 uint16_t properties_; |
| 94 }; |
| 95 |
| 96 OStream& operator<<(OStream& os, const Operator& op); |
| 97 |
| 98 // An implementation of Operator that has no static parameters. Such operators |
| 99 // have just a name, an opcode, and a fixed number of inputs and outputs. |
| 100 // They can represented by singletons and shared globally. |
| 101 class SimpleOperator : public Operator { |
| 102 public: |
| 103 SimpleOperator(uint8_t opcode, uint16_t properties, int input_count, |
| 104 int output_count, const char* mnemonic) |
| 105 : Operator(opcode, properties), |
| 106 input_count_(input_count), |
| 107 output_count_(output_count), |
| 108 mnemonic_(mnemonic) {} |
| 109 |
| 110 virtual const char* mnemonic() { return mnemonic_; } |
| 111 virtual bool Equals(Operator* that) { return opcode() == that->opcode(); } |
| 112 virtual int HashCode() { return opcode(); } |
| 113 virtual int InputCount() { return input_count_; } |
| 114 virtual int OutputCount() { return output_count_; } |
| 115 |
| 116 private: |
| 117 virtual OStream& PrintTo(OStream& os) const { // NOLINT |
| 118 return os << mnemonic_; |
| 119 } |
| 120 |
| 121 int input_count_; |
| 122 int output_count_; |
| 123 const char* mnemonic_; |
| 124 }; |
| 125 |
| 126 // Template specialization implements a kind of type class for dealing with the |
| 127 // static parameters of Operator1 automatically. |
| 128 template <typename T> |
| 129 struct StaticParameterTraits { |
| 130 static OStream& PrintTo(OStream& os, T val) { // NOLINT |
| 131 return os << "??"; |
| 132 } |
| 133 static int HashCode(T a) { return 0; } |
| 134 static bool Equals(T a, T b) { |
| 135 return false; // Not every T has a ==. By default, be conservative. |
| 136 } |
| 137 }; |
| 138 |
| 139 template <> |
| 140 struct StaticParameterTraits<ExternalReference> { |
| 141 static OStream& PrintTo(OStream& os, ExternalReference val) { // NOLINT |
| 142 os << val.address(); |
| 143 const Runtime::Function* function = |
| 144 Runtime::FunctionForEntry(val.address()); |
| 145 if (function != NULL) { |
| 146 os << " <" << function->name << ".entry>"; |
| 147 } |
| 148 return os; |
| 149 } |
| 150 static int HashCode(ExternalReference a) { |
| 151 return reinterpret_cast<intptr_t>(a.address()) & 0xFFFFFFFF; |
| 152 } |
| 153 static bool Equals(ExternalReference a, ExternalReference b) { |
| 154 return a == b; |
| 155 } |
| 156 }; |
| 157 |
| 158 // Specialization for static parameters of type {int}. |
| 159 template <> |
| 160 struct StaticParameterTraits<int> { |
| 161 static OStream& PrintTo(OStream& os, int val) { // NOLINT |
| 162 return os << val; |
| 163 } |
| 164 static int HashCode(int a) { return a; } |
| 165 static bool Equals(int a, int b) { return a == b; } |
| 166 }; |
| 167 |
| 168 // Specialization for static parameters of type {double}. |
| 169 template <> |
| 170 struct StaticParameterTraits<double> { |
| 171 static OStream& PrintTo(OStream& os, double val) { // NOLINT |
| 172 return os << val; |
| 173 } |
| 174 static int HashCode(double a) { |
| 175 return static_cast<int>(BitCast<int64_t>(a)); |
| 176 } |
| 177 static bool Equals(double a, double b) { |
| 178 return BitCast<int64_t>(a) == BitCast<int64_t>(b); |
| 179 } |
| 180 }; |
| 181 |
| 182 // Specialization for static parameters of type {PrintableUnique<Object>}. |
| 183 template <> |
| 184 struct StaticParameterTraits<PrintableUnique<Object> > { |
| 185 static OStream& PrintTo(OStream& os, PrintableUnique<Object> val) { // NOLINT |
| 186 return os << val.string(); |
| 187 } |
| 188 static int HashCode(PrintableUnique<Object> a) { return a.Hashcode(); } |
| 189 static bool Equals(PrintableUnique<Object> a, PrintableUnique<Object> b) { |
| 190 return a == b; |
| 191 } |
| 192 }; |
| 193 |
| 194 // Specialization for static parameters of type {PrintableUnique<Name>}. |
| 195 template <> |
| 196 struct StaticParameterTraits<PrintableUnique<Name> > { |
| 197 static OStream& PrintTo(OStream& os, PrintableUnique<Name> val) { // NOLINT |
| 198 return os << val.string(); |
| 199 } |
| 200 static int HashCode(PrintableUnique<Name> a) { return a.Hashcode(); } |
| 201 static bool Equals(PrintableUnique<Name> a, PrintableUnique<Name> b) { |
| 202 return a == b; |
| 203 } |
| 204 }; |
| 205 |
| 206 #if DEBUG |
| 207 // Specialization for static parameters of type {Handle<Object>} to prevent any |
| 208 // direct usage of Handles in constants. |
| 209 template <> |
| 210 struct StaticParameterTraits<Handle<Object> > { |
| 211 static OStream& PrintTo(OStream& os, Handle<Object> val) { // NOLINT |
| 212 UNREACHABLE(); // Should use PrintableUnique<Object> instead |
| 213 return os; |
| 214 } |
| 215 static int HashCode(Handle<Object> a) { |
| 216 UNREACHABLE(); // Should use PrintableUnique<Object> instead |
| 217 return 0; |
| 218 } |
| 219 static bool Equals(Handle<Object> a, Handle<Object> b) { |
| 220 UNREACHABLE(); // Should use PrintableUnique<Object> instead |
| 221 return false; |
| 222 } |
| 223 }; |
| 224 #endif |
| 225 |
| 226 // A templatized implementation of Operator that has one static parameter of |
| 227 // type {T}. If a specialization of StaticParameterTraits<{T}> exists, then |
| 228 // operators of this kind can automatically be hashed, compared, and printed. |
| 229 template <typename T> |
| 230 class Operator1 : public Operator { |
| 231 public: |
| 232 Operator1(uint8_t opcode, uint16_t properties, int input_count, |
| 233 int output_count, const char* mnemonic, T parameter) |
| 234 : Operator(opcode, properties), |
| 235 input_count_(input_count), |
| 236 output_count_(output_count), |
| 237 mnemonic_(mnemonic), |
| 238 parameter_(parameter) {} |
| 239 |
| 240 const T& parameter() const { return parameter_; } |
| 241 |
| 242 virtual const char* mnemonic() { return mnemonic_; } |
| 243 virtual bool Equals(Operator* other) { |
| 244 if (opcode() != other->opcode()) return false; |
| 245 Operator1<T>* that = static_cast<Operator1<T>*>(other); |
| 246 T temp1 = this->parameter_; |
| 247 T temp2 = that->parameter_; |
| 248 return StaticParameterTraits<T>::Equals(temp1, temp2); |
| 249 } |
| 250 virtual int HashCode() { |
| 251 return opcode() + 33 * StaticParameterTraits<T>::HashCode(this->parameter_); |
| 252 } |
| 253 virtual int InputCount() { return input_count_; } |
| 254 virtual int OutputCount() { return output_count_; } |
| 255 virtual OStream& PrintParameter(OStream& os) const { // NOLINT |
| 256 return StaticParameterTraits<T>::PrintTo(os << "[", parameter_) << "]"; |
| 257 } |
| 258 |
| 259 private: |
| 260 virtual OStream& PrintTo(OStream& os) const { // NOLINT |
| 261 return PrintParameter(os << mnemonic_); |
| 262 } |
| 263 |
| 264 int input_count_; |
| 265 int output_count_; |
| 266 const char* mnemonic_; |
| 267 T parameter_; |
| 268 }; |
| 269 |
| 270 // Type definitions for operators with specific types of parameters. |
| 271 typedef Operator1<PrintableUnique<Name> > NameOperator; |
| 272 } |
| 273 } |
| 274 } // namespace v8::internal::compiler |
| 275 |
| 276 #endif // V8_COMPILER_OPERATOR_H_ |
OLD | NEW |