Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_COMPILER_OPERATOR_H_ | 5 #ifndef V8_COMPILER_OPERATOR_H_ |
| 6 #define V8_COMPILER_OPERATOR_H_ | 6 #define V8_COMPILER_OPERATOR_H_ |
| 7 | 7 |
| 8 #include <ostream> // NOLINT(readability/streams) | 8 #include <ostream> // NOLINT(readability/streams) |
| 9 | 9 |
| 10 #include "src/base/flags.h" | 10 #include "src/base/flags.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 kNoRead = 1 << 4, // Has no scheduling dependency on Effects | 42 kNoRead = 1 << 4, // Has no scheduling dependency on Effects |
| 43 kNoWrite = 1 << 5, // Does not modify any Effects and thereby | 43 kNoWrite = 1 << 5, // Does not modify any Effects and thereby |
| 44 // create new scheduling dependencies. | 44 // create new scheduling dependencies. |
| 45 kNoThrow = 1 << 6, // Can never generate an exception. | 45 kNoThrow = 1 << 6, // Can never generate an exception. |
| 46 kFoldable = kNoRead | kNoWrite, | 46 kFoldable = kNoRead | kNoWrite, |
| 47 kEliminatable = kNoWrite | kNoThrow, | 47 kEliminatable = kNoWrite | kNoThrow, |
| 48 kPure = kNoRead | kNoWrite | kNoThrow | kIdempotent | 48 kPure = kNoRead | kNoWrite | kNoThrow | kIdempotent |
| 49 }; | 49 }; |
| 50 typedef base::Flags<Property, uint8_t> Properties; | 50 typedef base::Flags<Property, uint8_t> Properties; |
| 51 | 51 |
| 52 Operator(Opcode opcode, Properties properties, const char* mnemonic) | 52 // Constructor. |
| 53 : opcode_(opcode), properties_(properties), mnemonic_(mnemonic) {} | 53 Operator(Opcode opcode, Properties properties, const char* mnemonic, |
| 54 virtual ~Operator(); | 54 size_t value_in, size_t effect_in, size_t control_in, |
|
rossberg
2014/10/29 09:43:21
Using size_t here but int for the getters seems in
titzer
2014/10/29 11:59:26
Done.
| |
| 55 size_t value_out, size_t effect_out, size_t control_out); | |
| 56 | |
| 57 virtual ~Operator() {} | |
| 55 | 58 |
| 56 // A small integer unique to all instances of a particular kind of operator, | 59 // A small integer unique to all instances of a particular kind of operator, |
| 57 // useful for quick matching for specific kinds of operators. For fast access | 60 // useful for quick matching for specific kinds of operators. For fast access |
| 58 // the opcode is stored directly in the operator object. | 61 // the opcode is stored directly in the operator object. |
| 59 Opcode opcode() const { return opcode_; } | 62 Opcode opcode() const { return opcode_; } |
| 60 | 63 |
| 61 // Returns a constant string representing the mnemonic of the operator, | 64 // Returns a constant string representing the mnemonic of the operator, |
| 62 // without the static parameters. Useful for debugging. | 65 // without the static parameters. Useful for debugging. |
| 63 const char* mnemonic() const { return mnemonic_; } | 66 const char* mnemonic() const { return mnemonic_; } |
| 64 | 67 |
| 65 // Check if this operator equals another operator. Equivalent operators can | 68 // Check if this operator equals another operator. Equivalent operators can |
| 66 // be merged, and nodes with equivalent operators and equivalent inputs | 69 // be merged, and nodes with equivalent operators and equivalent inputs |
| 67 // can be merged. | 70 // can be merged. |
| 68 virtual bool Equals(const Operator*) const = 0; | 71 virtual bool Equals(const Operator* that) const { |
| 72 return this->opcode() == that->opcode(); | |
| 73 } | |
| 69 | 74 |
| 70 // Compute a hashcode to speed up equivalence-set checking. | 75 // Compute a hashcode to speed up equivalence-set checking. |
| 71 // Equal operators should always have equal hashcodes, and unequal operators | 76 // Equal operators should always have equal hashcodes, and unequal operators |
| 72 // should have unequal hashcodes with high probability. | 77 // should have unequal hashcodes with high probability. |
| 73 virtual size_t HashCode() const = 0; | 78 virtual size_t HashCode() const { return base::hash<Opcode>()(opcode()); } |
| 74 | 79 |
| 75 // Check whether this operator has the given property. | 80 // Check whether this operator has the given property. |
| 76 bool HasProperty(Property property) const { | 81 bool HasProperty(Property property) const { |
| 77 return (properties() & property) == property; | 82 return (properties() & property) == property; |
| 78 } | 83 } |
| 79 | 84 |
| 80 // Number of data inputs to the operator, for verifying graph structure. | 85 // Number of data inputs to the operator, for verifying graph structure. |
| 81 virtual int InputCount() const = 0; | 86 // TODO(titzer): convert callers to ValueInputCount(); |
| 87 int InputCount() const { return ValueInputCount(); } | |
| 82 | 88 |
| 83 // Number of data outputs from the operator, for verifying graph structure. | 89 // Number of data outputs from the operator, for verifying graph structure. |
| 84 virtual int OutputCount() const = 0; | 90 // TODO(titzer): convert callers to ValueOutputCount(); |
| 91 int OutputCount() const { return ValueOutputCount(); } | |
| 85 | 92 |
| 86 Properties properties() const { return properties_; } | 93 Properties properties() const { return properties_; } |
| 87 | 94 |
| 95 int ValueInputCount() const { return value_in_; } | |
| 96 int EffectInputCount() const { return effect_in_; } | |
| 97 int ControlInputCount() const { return control_in_; } | |
| 98 | |
| 99 int ValueOutputCount() const { return value_out_; } | |
| 100 int EffectOutputCount() const { return effect_out_; } | |
| 101 int ControlOutputCount() const { return control_out_; } | |
| 102 | |
| 88 // TODO(titzer): API for input and output types, for typechecking graph. | 103 // TODO(titzer): API for input and output types, for typechecking graph. |
| 89 protected: | 104 protected: |
| 90 // Print the full operator into the given stream, including any | 105 // Print the full operator into the given stream, including any |
| 91 // static parameters. Useful for debugging and visualizing the IR. | 106 // static parameters. Useful for debugging and visualizing the IR. |
| 92 virtual void PrintTo(std::ostream& os) const = 0; // NOLINT | 107 virtual void PrintTo(std::ostream& os) const; |
| 93 friend std::ostream& operator<<(std::ostream& os, const Operator& op); | 108 friend std::ostream& operator<<(std::ostream& os, const Operator& op); |
| 94 | 109 |
| 95 private: | 110 private: |
| 96 Opcode opcode_; | 111 Opcode opcode_; |
| 97 Properties properties_; | 112 Properties properties_; |
| 98 const char* mnemonic_; | 113 const char* mnemonic_; |
| 114 uint32_t value_in_; | |
| 115 uint16_t effect_in_; | |
| 116 uint16_t control_in_; | |
| 117 uint16_t value_out_; | |
| 118 uint8_t effect_out_; | |
| 119 uint8_t control_out_; | |
| 99 | 120 |
| 100 DISALLOW_COPY_AND_ASSIGN(Operator); | 121 DISALLOW_COPY_AND_ASSIGN(Operator); |
| 101 }; | 122 }; |
| 102 | 123 |
| 103 DEFINE_OPERATORS_FOR_FLAGS(Operator::Properties) | 124 DEFINE_OPERATORS_FOR_FLAGS(Operator::Properties) |
| 104 | 125 |
| 105 std::ostream& operator<<(std::ostream& os, const Operator& op); | 126 std::ostream& operator<<(std::ostream& os, const Operator& op); |
| 106 | 127 |
| 107 | 128 |
| 108 // An implementation of Operator that has no static parameters. Such operators | |
| 109 // have just a name, an opcode, and a fixed number of inputs and outputs. | |
| 110 // They can represented by singletons and shared globally. | |
| 111 class SimpleOperator : public Operator { | |
| 112 public: | |
| 113 SimpleOperator(Opcode opcode, Properties properties, size_t input_count, | |
| 114 size_t output_count, const char* mnemonic); | |
| 115 ~SimpleOperator(); | |
| 116 | |
| 117 virtual bool Equals(const Operator* that) const FINAL; | |
| 118 virtual size_t HashCode() const FINAL; | |
| 119 virtual int InputCount() const FINAL; | |
| 120 virtual int OutputCount() const FINAL; | |
| 121 | |
| 122 private: | |
| 123 virtual void PrintTo(std::ostream& os) const FINAL; | |
| 124 | |
| 125 uint8_t input_count_; | |
| 126 uint8_t output_count_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(SimpleOperator); | |
| 129 }; | |
| 130 | |
| 131 | |
| 132 // A templatized implementation of Operator that has one static parameter of | 129 // A templatized implementation of Operator that has one static parameter of |
| 133 // type {T}. | 130 // type {T}. |
| 134 template <typename T, typename Pred = std::equal_to<T>, | 131 template <typename T, typename Pred = std::equal_to<T>, |
| 135 typename Hash = base::hash<T>> | 132 typename Hash = base::hash<T>> |
| 136 class Operator1 : public Operator { | 133 class Operator1 : public Operator { |
| 137 public: | 134 public: |
| 138 Operator1(Opcode opcode, Properties properties, int input_count, | 135 Operator1(Opcode opcode, Properties properties, const char* mnemonic, |
| 139 int output_count, const char* mnemonic, T parameter, | 136 size_t value_in, size_t effect_in, size_t control_in, |
| 140 Pred const& pred = Pred(), Hash const& hash = Hash()) | 137 size_t value_out, size_t effect_out, size_t control_out, |
| 141 : Operator(opcode, properties, mnemonic), | 138 T parameter, Pred const& pred = Pred(), Hash const& hash = Hash()) |
| 142 input_count_(input_count), | 139 : Operator(opcode, properties, mnemonic, value_in, effect_in, control_in, |
| 143 output_count_(output_count), | 140 value_out, effect_out, control_out), |
| 144 parameter_(parameter), | 141 parameter_(parameter), |
| 145 pred_(pred), | 142 pred_(pred), |
| 146 hash_(hash) {} | 143 hash_(hash) {} |
| 147 | 144 |
| 148 T const& parameter() const { return parameter_; } | 145 T const& parameter() const { return parameter_; } |
| 149 | 146 |
| 150 virtual bool Equals(const Operator* other) const FINAL { | 147 virtual bool Equals(const Operator* other) const FINAL { |
| 151 if (opcode() != other->opcode()) return false; | 148 if (opcode() != other->opcode()) return false; |
| 152 const Operator1<T>* that = static_cast<const Operator1<T>*>(other); | 149 const Operator1<T>* that = static_cast<const Operator1<T>*>(other); |
| 153 return this->pred_(this->parameter(), that->parameter()); | 150 return this->pred_(this->parameter(), that->parameter()); |
| 154 } | 151 } |
| 155 virtual size_t HashCode() const FINAL { | 152 virtual size_t HashCode() const FINAL { |
| 156 return base::hash_combine(this->opcode(), this->hash_(this->parameter())); | 153 return base::hash_combine(this->opcode(), this->hash_(this->parameter())); |
| 157 } | 154 } |
| 158 virtual int InputCount() const FINAL { return input_count_; } | |
| 159 virtual int OutputCount() const FINAL { return output_count_; } | |
| 160 virtual void PrintParameter(std::ostream& os) const { | 155 virtual void PrintParameter(std::ostream& os) const { |
| 161 os << "[" << this->parameter() << "]"; | 156 os << "[" << this->parameter() << "]"; |
| 162 } | 157 } |
| 163 | 158 |
| 164 protected: | 159 protected: |
| 165 virtual void PrintTo(std::ostream& os) const FINAL { | 160 virtual void PrintTo(std::ostream& os) const FINAL { |
| 166 os << mnemonic(); | 161 os << mnemonic(); |
| 167 PrintParameter(os); | 162 PrintParameter(os); |
| 168 } | 163 } |
| 169 | 164 |
| 170 private: | 165 private: |
| 171 int const input_count_; | |
| 172 int const output_count_; | |
| 173 T const parameter_; | 166 T const parameter_; |
| 174 Pred const pred_; | 167 Pred const pred_; |
| 175 Hash const hash_; | 168 Hash const hash_; |
| 176 }; | 169 }; |
| 177 | 170 |
| 178 | 171 |
| 179 // Helper to extract parameters from Operator1<*> operator. | 172 // Helper to extract parameters from Operator1<*> operator. |
| 180 template <typename T> | 173 template <typename T> |
| 181 inline T const& OpParameter(const Operator* op) { | 174 inline T const& OpParameter(const Operator* op) { |
| 182 return static_cast<const Operator1<T>*>(op)->parameter(); | 175 return static_cast<const Operator1<T>*>(op)->parameter(); |
| 183 } | 176 } |
| 184 | 177 |
| 185 } // namespace compiler | 178 } // namespace compiler |
| 186 } // namespace internal | 179 } // namespace internal |
| 187 } // namespace v8 | 180 } // namespace v8 |
| 188 | 181 |
| 189 #endif // V8_COMPILER_OPERATOR_H_ | 182 #endif // V8_COMPILER_OPERATOR_H_ |
| OLD | NEW |