1 // Copyright 2014 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.
5 #include "src/compiler/common-operator.h"
7 #include "src/assembler.h"
8 #include "src/base/lazy-instance.h"
9 #include "src/compiler/linkage.h"
10 #include "src/compiler/opcodes.h"
11 #include "src/compiler/operator.h"
12 #include "src/unique.h"
19 std::ostream& operator<<(std::ostream& os, BranchHint hint) {
21 case BranchHint::kNone:
23 case BranchHint::kTrue:
25 case BranchHint::kFalse:
33 BranchHint BranchHintOf(const Operator* const op) {
34 DCHECK_EQ(IrOpcode::kBranch, op->opcode());
35 return OpParameter<BranchHint>(op);
39 bool operator==(SelectParameters const& lhs, SelectParameters const& rhs) {
40 return lhs.type() == rhs.type() && lhs.hint() == rhs.hint();
44 bool operator!=(SelectParameters const& lhs, SelectParameters const& rhs) {
49 size_t hash_value(SelectParameters const& p) {
50 return base::hash_combine(p.type(), p.hint());
54 std::ostream& operator<<(std::ostream& os, SelectParameters const& p) {
55 return os << p.type() << "|" << p.hint();
59 SelectParameters const& SelectParametersOf(const Operator* const op) {
60 DCHECK_EQ(IrOpcode::kSelect, op->opcode());
61 return OpParameter<SelectParameters>(op);
65 size_t hash_value(OutputFrameStateCombine const& sc) {
66 return base::hash_combine(sc.kind_, sc.parameter_);
70 std::ostream& operator<<(std::ostream& os, OutputFrameStateCombine const& sc) {
72 case OutputFrameStateCombine::kPushOutput:
73 if (sc.parameter_ == 0) return os << "Ignore";
74 return os << "Push(" << sc.parameter_ << ")";
75 case OutputFrameStateCombine::kPokeAt:
76 return os << "PokeAt(" << sc.parameter_ << ")";
83 bool operator==(FrameStateCallInfo const& lhs, FrameStateCallInfo const& rhs) {
84 return lhs.type() == rhs.type() && lhs.bailout_id() == rhs.bailout_id() &&
85 lhs.state_combine() == rhs.state_combine();
89 bool operator!=(FrameStateCallInfo const& lhs, FrameStateCallInfo const& rhs) {
94 size_t hash_value(FrameStateCallInfo const& info) {
95 return base::hash_combine(info.type(), info.bailout_id(),
96 info.state_combine());
100 std::ostream& operator<<(std::ostream& os, FrameStateCallInfo const& info) {
101 return os << info.type() << ", " << info.bailout_id() << ", "
102 << info.state_combine();
106 #define CACHED_OP_LIST(V) \
107 V(Dead, Operator::kFoldable, 0, 0, 0, 1) \
108 V(End, Operator::kFoldable, 0, 0, 1, 0) \
109 V(IfTrue, Operator::kFoldable, 0, 0, 1, 1) \
110 V(IfFalse, Operator::kFoldable, 0, 0, 1, 1) \
111 V(Throw, Operator::kFoldable, 1, 1, 1, 1) \
112 V(Return, Operator::kNoProperties, 1, 1, 1, 1)
115 struct CommonOperatorGlobalCache FINAL {
116 #define CACHED(Name, properties, value_input_count, effect_input_count, \
117 control_input_count, control_output_count) \
118 struct Name##Operator FINAL : public Operator { \
120 : Operator(IrOpcode::k##Name, properties, #Name, value_input_count, \
121 effect_input_count, control_input_count, 0, 0, \
122 control_output_count) {} \
124 Name##Operator k##Name##Operator;
125 CACHED_OP_LIST(CACHED)
130 static base::LazyInstance<CommonOperatorGlobalCache>::type kCache =
131 LAZY_INSTANCE_INITIALIZER;
134 CommonOperatorBuilder::CommonOperatorBuilder(Zone* zone)
135 : cache_(kCache.Get()), zone_(zone) {}
138 #define CACHED(Name, properties, value_input_count, effect_input_count, \
139 control_input_count, control_output_count) \
140 const Operator* CommonOperatorBuilder::Name() { \
141 return &cache_.k##Name##Operator; \
143 CACHED_OP_LIST(CACHED)
147 const Operator* CommonOperatorBuilder::Branch(BranchHint hint) {
148 return new (zone()) Operator1<BranchHint>(
149 IrOpcode::kBranch, Operator::kFoldable, "Branch", 1, 0, 1, 0, 0, 2, hint);
153 const Operator* CommonOperatorBuilder::Start(int num_formal_parameters) {
154 // Outputs are formal parameters, plus context, receiver, and JSFunction.
155 const int value_output_count = num_formal_parameters + 3;
156 return new (zone()) Operator( // --
157 IrOpcode::kStart, Operator::kFoldable, // opcode
159 0, 0, 0, value_output_count, 1, 1); // counts
163 const Operator* CommonOperatorBuilder::Merge(int controls) {
164 return new (zone()) Operator( // --
165 IrOpcode::kMerge, Operator::kFoldable, // opcode
167 0, 0, controls, 0, 0, 1); // counts
171 const Operator* CommonOperatorBuilder::Loop(int controls) {
172 return new (zone()) Operator( // --
173 IrOpcode::kLoop, Operator::kFoldable, // opcode
175 0, 0, controls, 0, 0, 1); // counts
179 const Operator* CommonOperatorBuilder::Terminate(int effects) {
180 return new (zone()) Operator( // --
181 IrOpcode::kTerminate, Operator::kPure, // opcode
183 0, effects, 1, 0, 0, 1); // counts
187 const Operator* CommonOperatorBuilder::Parameter(int index) {
188 return new (zone()) Operator1<int>( // --
189 IrOpcode::kParameter, Operator::kPure, // opcode
191 1, 0, 0, 1, 0, 0, // counts
196 const Operator* CommonOperatorBuilder::Int32Constant(int32_t value) {
197 return new (zone()) Operator1<int32_t>( // --
198 IrOpcode::kInt32Constant, Operator::kPure, // opcode
199 "Int32Constant", // name
200 0, 0, 0, 1, 0, 0, // counts
205 const Operator* CommonOperatorBuilder::Int64Constant(int64_t value) {
206 return new (zone()) Operator1<int64_t>( // --
207 IrOpcode::kInt64Constant, Operator::kPure, // opcode
208 "Int64Constant", // name
209 0, 0, 0, 1, 0, 0, // counts
214 const Operator* CommonOperatorBuilder::Float32Constant(volatile float value) {
216 Operator1<float, base::bit_equal_to<float>, base::bit_hash<float>>( // --
217 IrOpcode::kFloat32Constant, Operator::kPure, // opcode
218 "Float32Constant", // name
219 0, 0, 0, 1, 0, 0, // counts
224 const Operator* CommonOperatorBuilder::Float64Constant(volatile double value) {
225 return new (zone()) Operator1<double, base::bit_equal_to<double>,
226 base::bit_hash<double>>( // --
227 IrOpcode::kFloat64Constant, Operator::kPure, // opcode
228 "Float64Constant", // name
229 0, 0, 0, 1, 0, 0, // counts
234 const Operator* CommonOperatorBuilder::ExternalConstant(
235 const ExternalReference& value) {
236 return new (zone()) Operator1<ExternalReference>( // --
237 IrOpcode::kExternalConstant, Operator::kPure, // opcode
238 "ExternalConstant", // name
239 0, 0, 0, 1, 0, 0, // counts
244 const Operator* CommonOperatorBuilder::NumberConstant(volatile double value) {
245 return new (zone()) Operator1<double, base::bit_equal_to<double>,
246 base::bit_hash<double>>( // --
247 IrOpcode::kNumberConstant, Operator::kPure, // opcode
248 "NumberConstant", // name
249 0, 0, 0, 1, 0, 0, // counts
254 const Operator* CommonOperatorBuilder::HeapConstant(
255 const Unique<HeapObject>& value) {
256 return new (zone()) Operator1<Unique<HeapObject>>( // --
257 IrOpcode::kHeapConstant, Operator::kPure, // opcode
258 "HeapConstant", // name
259 0, 0, 0, 1, 0, 0, // counts
264 const Operator* CommonOperatorBuilder::Select(MachineType type,
266 return new (zone()) Operator1<SelectParameters>( // --
267 IrOpcode::kSelect, Operator::kPure, // opcode
269 3, 0, 0, 1, 0, 0, // counts
270 SelectParameters(type, hint)); // parameter
274 const Operator* CommonOperatorBuilder::Phi(MachineType type, int arguments) {
275 DCHECK(arguments > 0); // Disallow empty phis.
276 return new (zone()) Operator1<MachineType>( // --
277 IrOpcode::kPhi, Operator::kPure, // opcode
279 arguments, 0, 1, 1, 0, 0, // counts
284 const Operator* CommonOperatorBuilder::EffectPhi(int arguments) {
285 DCHECK(arguments > 0); // Disallow empty phis.
286 return new (zone()) Operator( // --
287 IrOpcode::kEffectPhi, Operator::kPure, // opcode
289 0, arguments, 1, 0, 1, 0); // counts
293 const Operator* CommonOperatorBuilder::ValueEffect(int arguments) {
294 DCHECK(arguments > 0); // Disallow empty value effects.
295 return new (zone()) Operator( // --
296 IrOpcode::kValueEffect, Operator::kPure, // opcode
297 "ValueEffect", // name
298 arguments, 0, 0, 0, 1, 0); // counts
302 const Operator* CommonOperatorBuilder::Finish(int arguments) {
303 DCHECK(arguments > 0); // Disallow empty finishes.
304 return new (zone()) Operator( // --
305 IrOpcode::kFinish, Operator::kPure, // opcode
307 1, arguments, 0, 1, 0, 0); // counts
311 const Operator* CommonOperatorBuilder::StateValues(int arguments) {
312 return new (zone()) Operator( // --
313 IrOpcode::kStateValues, Operator::kPure, // opcode
314 "StateValues", // name
315 arguments, 0, 0, 1, 0, 0); // counts
319 const Operator* CommonOperatorBuilder::FrameState(
320 FrameStateType type, BailoutId bailout_id,
321 OutputFrameStateCombine state_combine, MaybeHandle<JSFunction> jsfunction) {
322 return new (zone()) Operator1<FrameStateCallInfo>( // --
323 IrOpcode::kFrameState, Operator::kPure, // opcode
324 "FrameState", // name
325 4, 0, 0, 1, 0, 0, // counts
326 FrameStateCallInfo(type, bailout_id, state_combine, jsfunction));
330 const Operator* CommonOperatorBuilder::Call(const CallDescriptor* descriptor) {
331 class CallOperator FINAL : public Operator1<const CallDescriptor*> {
333 CallOperator(const CallDescriptor* descriptor, const char* mnemonic)
334 : Operator1<const CallDescriptor*>(
335 IrOpcode::kCall, descriptor->properties(), mnemonic,
336 descriptor->InputCount() + descriptor->FrameStateCount(),
337 Operator::ZeroIfPure(descriptor->properties()),
338 Operator::ZeroIfPure(descriptor->properties()),
339 descriptor->ReturnCount(),
340 Operator::ZeroIfPure(descriptor->properties()), 0, descriptor) {}
342 virtual void PrintParameter(std::ostream& os) const OVERRIDE {
343 os << "[" << *parameter() << "]";
346 return new (zone()) CallOperator(descriptor, "Call");
350 const Operator* CommonOperatorBuilder::Projection(size_t index) {
351 return new (zone()) Operator1<size_t>( // --
352 IrOpcode::kProjection, Operator::kPure, // opcode
353 "Projection", // name
354 1, 0, 0, 1, 0, 0, // counts
358 } // namespace compiler
359 } // namespace internal