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 size_t ProjectionIndexOf(const Operator* const op) {
107 DCHECK_EQ(IrOpcode::kProjection, op->opcode());
108 return OpParameter<size_t>(op);
112 #define CACHED_OP_LIST(V) \
113 V(Always, Operator::kPure, 0, 0, 0, 1, 0, 0) \
114 V(Dead, Operator::kFoldable, 0, 0, 0, 0, 0, 1) \
115 V(End, Operator::kKontrol, 0, 0, 1, 0, 0, 0) \
116 V(IfTrue, Operator::kKontrol, 0, 0, 1, 0, 0, 1) \
117 V(IfFalse, Operator::kKontrol, 0, 0, 1, 0, 0, 1) \
118 V(IfDefault, Operator::kKontrol, 0, 0, 1, 0, 0, 1) \
119 V(Throw, Operator::kFoldable, 1, 1, 1, 0, 0, 1) \
120 V(Return, Operator::kNoThrow, 1, 1, 1, 0, 0, 1) \
121 V(OsrNormalEntry, Operator::kFoldable, 0, 1, 1, 0, 1, 1) \
122 V(OsrLoopEntry, Operator::kFoldable, 0, 1, 1, 0, 1, 1)
125 #define CACHED_LOOP_LIST(V) \
130 #define CACHED_MERGE_LIST(V) \
141 #define CACHED_PARAMETER_LIST(V) \
151 struct CommonOperatorGlobalCache FINAL {
152 #define CACHED(Name, properties, value_input_count, effect_input_count, \
153 control_input_count, value_output_count, effect_output_count, \
154 control_output_count) \
155 struct Name##Operator FINAL : public Operator { \
157 : Operator(IrOpcode::k##Name, properties, #Name, value_input_count, \
158 effect_input_count, control_input_count, \
159 value_output_count, effect_output_count, \
160 control_output_count) {} \
162 Name##Operator k##Name##Operator;
163 CACHED_OP_LIST(CACHED)
166 template <BranchHint kBranchHint>
167 struct BranchOperator FINAL : public Operator1<BranchHint> {
169 : Operator1<BranchHint>( // --
170 IrOpcode::kBranch, Operator::kKontrol, // opcode
172 1, 0, 1, 0, 0, 2, // counts
173 kBranchHint) {} // parameter
175 BranchOperator<BranchHint::kNone> kBranchNoneOperator;
176 BranchOperator<BranchHint::kTrue> kBranchTrueOperator;
177 BranchOperator<BranchHint::kFalse> kBranchFalseOperator;
179 template <size_t kInputCount>
180 struct LoopOperator FINAL : public Operator {
183 IrOpcode::kLoop, Operator::kKontrol, // opcode
185 0, 0, kInputCount, 0, 0, 1) {} // counts
187 #define CACHED_LOOP(input_count) \
188 LoopOperator<input_count> kLoop##input_count##Operator;
189 CACHED_LOOP_LIST(CACHED_LOOP)
192 template <size_t kInputCount>
193 struct MergeOperator FINAL : public Operator {
196 IrOpcode::kMerge, Operator::kKontrol, // opcode
198 0, 0, kInputCount, 0, 0, 1) {} // counts
200 #define CACHED_MERGE(input_count) \
201 MergeOperator<input_count> kMerge##input_count##Operator;
202 CACHED_MERGE_LIST(CACHED_MERGE)
205 template <int kIndex>
206 struct ParameterOperator FINAL : public Operator1<int> {
208 : Operator1<int>( // --
209 IrOpcode::kParameter, Operator::kPure, // opcode
211 1, 0, 0, 1, 0, 0, // counts,
212 kIndex) {} // parameter
214 #define CACHED_PARAMETER(index) \
215 ParameterOperator<index> kParameter##index##Operator;
216 CACHED_PARAMETER_LIST(CACHED_PARAMETER)
217 #undef CACHED_PARAMETER
221 static base::LazyInstance<CommonOperatorGlobalCache>::type kCache =
222 LAZY_INSTANCE_INITIALIZER;
225 CommonOperatorBuilder::CommonOperatorBuilder(Zone* zone)
226 : cache_(kCache.Get()), zone_(zone) {}
229 #define CACHED(Name, properties, value_input_count, effect_input_count, \
230 control_input_count, value_output_count, effect_output_count, \
231 control_output_count) \
232 const Operator* CommonOperatorBuilder::Name() { \
233 return &cache_.k##Name##Operator; \
235 CACHED_OP_LIST(CACHED)
239 const Operator* CommonOperatorBuilder::Branch(BranchHint hint) {
241 case BranchHint::kNone:
242 return &cache_.kBranchNoneOperator;
243 case BranchHint::kTrue:
244 return &cache_.kBranchTrueOperator;
245 case BranchHint::kFalse:
246 return &cache_.kBranchFalseOperator;
253 const Operator* CommonOperatorBuilder::Switch(size_t control_output_count) {
254 DCHECK_GE(control_output_count, 3u); // Disallow trivial switches.
255 return new (zone()) Operator( // --
256 IrOpcode::kSwitch, Operator::kKontrol, // opcode
258 1, 0, 1, 0, 0, control_output_count); // counts
262 const Operator* CommonOperatorBuilder::IfValue(int32_t index) {
263 return new (zone()) Operator1<int32_t>( // --
264 IrOpcode::kIfValue, Operator::kKontrol, // opcode
266 0, 0, 1, 0, 0, 1, // counts
271 const Operator* CommonOperatorBuilder::Start(int num_formal_parameters) {
272 // Outputs are formal parameters, plus context, receiver, and JSFunction.
273 const int value_output_count = num_formal_parameters + 3;
274 return new (zone()) Operator( // --
275 IrOpcode::kStart, Operator::kFoldable, // opcode
277 0, 0, 0, value_output_count, 1, 1); // counts
281 const Operator* CommonOperatorBuilder::Loop(int control_input_count) {
282 switch (control_input_count) {
283 #define CACHED_LOOP(input_count) \
285 return &cache_.kLoop##input_count##Operator;
286 CACHED_LOOP_LIST(CACHED_LOOP)
292 return new (zone()) Operator( // --
293 IrOpcode::kLoop, Operator::kKontrol, // opcode
295 0, 0, control_input_count, 0, 0, 1); // counts
299 const Operator* CommonOperatorBuilder::Merge(int control_input_count) {
300 switch (control_input_count) {
301 #define CACHED_MERGE(input_count) \
303 return &cache_.kMerge##input_count##Operator;
304 CACHED_MERGE_LIST(CACHED_MERGE)
310 return new (zone()) Operator( // --
311 IrOpcode::kMerge, Operator::kKontrol, // opcode
313 0, 0, control_input_count, 0, 0, 1); // counts
317 const Operator* CommonOperatorBuilder::Parameter(int index) {
319 #define CACHED_PARAMETER(index) \
321 return &cache_.kParameter##index##Operator;
322 CACHED_PARAMETER_LIST(CACHED_PARAMETER)
323 #undef CACHED_PARAMETER
328 return new (zone()) Operator1<int>( // --
329 IrOpcode::kParameter, Operator::kPure, // opcode
331 1, 0, 0, 1, 0, 0, // counts
336 const Operator* CommonOperatorBuilder::OsrValue(int index) {
337 return new (zone()) Operator1<int>( // --
338 IrOpcode::kOsrValue, Operator::kNoProperties, // opcode
340 0, 0, 1, 1, 0, 0, // counts
345 const Operator* CommonOperatorBuilder::Int32Constant(int32_t value) {
346 return new (zone()) Operator1<int32_t>( // --
347 IrOpcode::kInt32Constant, Operator::kPure, // opcode
348 "Int32Constant", // name
349 0, 0, 0, 1, 0, 0, // counts
354 const Operator* CommonOperatorBuilder::Int64Constant(int64_t value) {
355 return new (zone()) Operator1<int64_t>( // --
356 IrOpcode::kInt64Constant, Operator::kPure, // opcode
357 "Int64Constant", // name
358 0, 0, 0, 1, 0, 0, // counts
363 const Operator* CommonOperatorBuilder::Float32Constant(volatile float value) {
365 Operator1<float, base::bit_equal_to<float>, base::bit_hash<float>>( // --
366 IrOpcode::kFloat32Constant, Operator::kPure, // opcode
367 "Float32Constant", // name
368 0, 0, 0, 1, 0, 0, // counts
373 const Operator* CommonOperatorBuilder::Float64Constant(volatile double value) {
374 return new (zone()) Operator1<double, base::bit_equal_to<double>,
375 base::bit_hash<double>>( // --
376 IrOpcode::kFloat64Constant, Operator::kPure, // opcode
377 "Float64Constant", // name
378 0, 0, 0, 1, 0, 0, // counts
383 const Operator* CommonOperatorBuilder::ExternalConstant(
384 const ExternalReference& value) {
385 return new (zone()) Operator1<ExternalReference>( // --
386 IrOpcode::kExternalConstant, Operator::kPure, // opcode
387 "ExternalConstant", // name
388 0, 0, 0, 1, 0, 0, // counts
393 const Operator* CommonOperatorBuilder::NumberConstant(volatile double value) {
394 return new (zone()) Operator1<double, base::bit_equal_to<double>,
395 base::bit_hash<double>>( // --
396 IrOpcode::kNumberConstant, Operator::kPure, // opcode
397 "NumberConstant", // name
398 0, 0, 0, 1, 0, 0, // counts
403 const Operator* CommonOperatorBuilder::HeapConstant(
404 const Unique<HeapObject>& value) {
405 return new (zone()) Operator1<Unique<HeapObject>>( // --
406 IrOpcode::kHeapConstant, Operator::kPure, // opcode
407 "HeapConstant", // name
408 0, 0, 0, 1, 0, 0, // counts
413 const Operator* CommonOperatorBuilder::Select(MachineType type,
415 return new (zone()) Operator1<SelectParameters>( // --
416 IrOpcode::kSelect, Operator::kPure, // opcode
418 3, 0, 0, 1, 0, 0, // counts
419 SelectParameters(type, hint)); // parameter
423 const Operator* CommonOperatorBuilder::Phi(MachineType type, int arguments) {
424 DCHECK(arguments > 0); // Disallow empty phis.
425 return new (zone()) Operator1<MachineType>( // --
426 IrOpcode::kPhi, Operator::kPure, // opcode
428 arguments, 0, 1, 1, 0, 0, // counts
433 const Operator* CommonOperatorBuilder::EffectPhi(int arguments) {
434 DCHECK(arguments > 0); // Disallow empty phis.
435 return new (zone()) Operator( // --
436 IrOpcode::kEffectPhi, Operator::kPure, // opcode
438 0, arguments, 1, 0, 1, 0); // counts
442 const Operator* CommonOperatorBuilder::EffectSet(int arguments) {
443 DCHECK(arguments > 1); // Disallow empty/singleton sets.
444 return new (zone()) Operator( // --
445 IrOpcode::kEffectSet, Operator::kPure, // opcode
447 0, arguments, 0, 0, 1, 0); // counts
451 const Operator* CommonOperatorBuilder::ValueEffect(int arguments) {
452 DCHECK(arguments > 0); // Disallow empty value effects.
453 return new (zone()) Operator( // --
454 IrOpcode::kValueEffect, Operator::kPure, // opcode
455 "ValueEffect", // name
456 arguments, 0, 0, 0, 1, 0); // counts
460 const Operator* CommonOperatorBuilder::Finish(int arguments) {
461 DCHECK(arguments > 0); // Disallow empty finishes.
462 return new (zone()) Operator( // --
463 IrOpcode::kFinish, Operator::kPure, // opcode
465 1, arguments, 0, 1, 0, 0); // counts
469 const Operator* CommonOperatorBuilder::StateValues(int arguments) {
470 return new (zone()) Operator( // --
471 IrOpcode::kStateValues, Operator::kPure, // opcode
472 "StateValues", // name
473 arguments, 0, 0, 1, 0, 0); // counts
477 const Operator* CommonOperatorBuilder::FrameState(
478 FrameStateType type, BailoutId bailout_id,
479 OutputFrameStateCombine state_combine, MaybeHandle<JSFunction> jsfunction) {
480 return new (zone()) Operator1<FrameStateCallInfo>( // --
481 IrOpcode::kFrameState, Operator::kPure, // opcode
482 "FrameState", // name
483 4, 0, 0, 1, 0, 0, // counts
484 FrameStateCallInfo(type, bailout_id, state_combine, jsfunction));
488 const Operator* CommonOperatorBuilder::Call(const CallDescriptor* descriptor) {
489 class CallOperator FINAL : public Operator1<const CallDescriptor*> {
491 CallOperator(const CallDescriptor* descriptor, const char* mnemonic)
492 : Operator1<const CallDescriptor*>(
493 IrOpcode::kCall, descriptor->properties(), mnemonic,
494 descriptor->InputCount() + descriptor->FrameStateCount(),
495 Operator::ZeroIfPure(descriptor->properties()),
496 Operator::ZeroIfPure(descriptor->properties()),
497 descriptor->ReturnCount(),
498 Operator::ZeroIfPure(descriptor->properties()), 0, descriptor) {}
500 void PrintParameter(std::ostream& os) const OVERRIDE {
501 os << "[" << *parameter() << "]";
504 return new (zone()) CallOperator(descriptor, "Call");
508 const Operator* CommonOperatorBuilder::Projection(size_t index) {
509 return new (zone()) Operator1<size_t>( // --
510 IrOpcode::kProjection, // opcode
511 Operator::kFoldable | Operator::kNoThrow, // flags
512 "Projection", // name
513 1, 0, 0, 1, 0, 0, // counts
518 const Operator* CommonOperatorBuilder::ResizeMergeOrPhi(const Operator* op,
520 if (op->opcode() == IrOpcode::kPhi) {
521 return Phi(OpParameter<MachineType>(op), size);
522 } else if (op->opcode() == IrOpcode::kEffectPhi) {
523 return EffectPhi(size);
524 } else if (op->opcode() == IrOpcode::kMerge) {
526 } else if (op->opcode() == IrOpcode::kLoop) {
535 } // namespace compiler
536 } // namespace internal