deps: upgrade v8 to 3.31.74.1
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / common-operator.cc
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.
4
5 #include "src/compiler/common-operator.h"
6
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"
13 #include "src/zone.h"
14
15 namespace v8 {
16 namespace internal {
17 namespace compiler {
18
19 std::ostream& operator<<(std::ostream& os, BranchHint hint) {
20   switch (hint) {
21     case BranchHint::kNone:
22       return os << "None";
23     case BranchHint::kTrue:
24       return os << "True";
25     case BranchHint::kFalse:
26       return os << "False";
27   }
28   UNREACHABLE();
29   return os;
30 }
31
32
33 BranchHint BranchHintOf(const Operator* const op) {
34   DCHECK_EQ(IrOpcode::kBranch, op->opcode());
35   return OpParameter<BranchHint>(op);
36 }
37
38
39 bool operator==(SelectParameters const& lhs, SelectParameters const& rhs) {
40   return lhs.type() == rhs.type() && lhs.hint() == rhs.hint();
41 }
42
43
44 bool operator!=(SelectParameters const& lhs, SelectParameters const& rhs) {
45   return !(lhs == rhs);
46 }
47
48
49 size_t hash_value(SelectParameters const& p) {
50   return base::hash_combine(p.type(), p.hint());
51 }
52
53
54 std::ostream& operator<<(std::ostream& os, SelectParameters const& p) {
55   return os << p.type() << "|" << p.hint();
56 }
57
58
59 SelectParameters const& SelectParametersOf(const Operator* const op) {
60   DCHECK_EQ(IrOpcode::kSelect, op->opcode());
61   return OpParameter<SelectParameters>(op);
62 }
63
64
65 size_t hash_value(OutputFrameStateCombine const& sc) {
66   return base::hash_combine(sc.kind_, sc.parameter_);
67 }
68
69
70 std::ostream& operator<<(std::ostream& os, OutputFrameStateCombine const& sc) {
71   switch (sc.kind_) {
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_ << ")";
77   }
78   UNREACHABLE();
79   return os;
80 }
81
82
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();
86 }
87
88
89 bool operator!=(FrameStateCallInfo const& lhs, FrameStateCallInfo const& rhs) {
90   return !(lhs == rhs);
91 }
92
93
94 size_t hash_value(FrameStateCallInfo const& info) {
95   return base::hash_combine(info.type(), info.bailout_id(),
96                             info.state_combine());
97 }
98
99
100 std::ostream& operator<<(std::ostream& os, FrameStateCallInfo const& info) {
101   return os << info.type() << ", " << info.bailout_id() << ", "
102             << info.state_combine();
103 }
104
105
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)
113
114
115 #define CACHED_LOOP_LIST(V) \
116   V(1)                      \
117   V(2)
118
119
120 #define CACHED_MERGE_LIST(V) \
121   V(1)                       \
122   V(2)                       \
123   V(3)                       \
124   V(4)                       \
125   V(5)                       \
126   V(6)                       \
127   V(7)                       \
128   V(8)
129
130
131 #define CACHED_PARAMETER_LIST(V) \
132   V(0)                           \
133   V(1)                           \
134   V(2)                           \
135   V(3)                           \
136   V(4)                           \
137   V(5)                           \
138   V(6)
139
140
141 struct CommonOperatorGlobalCache FINAL {
142 #define CACHED(Name, properties, value_input_count, effect_input_count,     \
143                control_input_count, control_output_count)                   \
144   struct Name##Operator FINAL : public Operator {                           \
145     Name##Operator()                                                        \
146         : Operator(IrOpcode::k##Name, properties, #Name, value_input_count, \
147                    effect_input_count, control_input_count, 0, 0,           \
148                    control_output_count) {}                                 \
149   };                                                                        \
150   Name##Operator k##Name##Operator;
151   CACHED_OP_LIST(CACHED)
152 #undef CACHED
153
154   template <BranchHint kBranchHint>
155   struct BranchOperator FINAL : public Operator1<BranchHint> {
156     BranchOperator()
157         : Operator1<BranchHint>(                       // --
158               IrOpcode::kBranch, Operator::kFoldable,  // opcode
159               "Branch",                                // name
160               1, 0, 1, 0, 0, 2,                        // counts
161               kBranchHint) {}                          // parameter
162   };
163   BranchOperator<BranchHint::kNone> kBranchNoneOperator;
164   BranchOperator<BranchHint::kTrue> kBranchTrueOperator;
165   BranchOperator<BranchHint::kFalse> kBranchFalseOperator;
166
167   template <size_t kInputCount>
168   struct LoopOperator FINAL : public Operator {
169     LoopOperator()
170         : Operator(                                  // --
171               IrOpcode::kLoop, Operator::kFoldable,  // opcode
172               "Loop",                                // name
173               0, 0, kInputCount, 0, 0, 1) {}         // counts
174   };
175 #define CACHED_LOOP(input_count) \
176   LoopOperator<input_count> kLoop##input_count##Operator;
177   CACHED_LOOP_LIST(CACHED_LOOP)
178 #undef CACHED_LOOP
179
180   template <size_t kInputCount>
181   struct MergeOperator FINAL : public Operator {
182     MergeOperator()
183         : Operator(                                   // --
184               IrOpcode::kMerge, Operator::kFoldable,  // opcode
185               "Merge",                                // name
186               0, 0, kInputCount, 0, 0, 1) {}          // counts
187   };
188 #define CACHED_MERGE(input_count) \
189   MergeOperator<input_count> kMerge##input_count##Operator;
190   CACHED_MERGE_LIST(CACHED_MERGE)
191 #undef CACHED_MERGE
192
193   template <int kIndex>
194   struct ParameterOperator FINAL : public Operator1<int> {
195     ParameterOperator()
196         : Operator1<int>(                             // --
197               IrOpcode::kParameter, Operator::kPure,  // opcode
198               "Parameter",                            // name
199               1, 0, 0, 1, 0, 0,                       // counts,
200               kIndex) {}                              // parameter
201   };
202 #define CACHED_PARAMETER(index) \
203   ParameterOperator<index> kParameter##index##Operator;
204   CACHED_PARAMETER_LIST(CACHED_PARAMETER)
205 #undef CACHED_PARAMETER
206 };
207
208
209 static base::LazyInstance<CommonOperatorGlobalCache>::type kCache =
210     LAZY_INSTANCE_INITIALIZER;
211
212
213 CommonOperatorBuilder::CommonOperatorBuilder(Zone* zone)
214     : cache_(kCache.Get()), zone_(zone) {}
215
216
217 #define CACHED(Name, properties, value_input_count, effect_input_count, \
218                control_input_count, control_output_count)               \
219   const Operator* CommonOperatorBuilder::Name() {                       \
220     return &cache_.k##Name##Operator;                                   \
221   }
222 CACHED_OP_LIST(CACHED)
223 #undef CACHED
224
225
226 const Operator* CommonOperatorBuilder::Branch(BranchHint hint) {
227   switch (hint) {
228     case BranchHint::kNone:
229       return &cache_.kBranchNoneOperator;
230     case BranchHint::kTrue:
231       return &cache_.kBranchTrueOperator;
232     case BranchHint::kFalse:
233       return &cache_.kBranchFalseOperator;
234   }
235   UNREACHABLE();
236   return nullptr;
237 }
238
239
240 const Operator* CommonOperatorBuilder::Start(int num_formal_parameters) {
241   // Outputs are formal parameters, plus context, receiver, and JSFunction.
242   const int value_output_count = num_formal_parameters + 3;
243   return new (zone()) Operator(               // --
244       IrOpcode::kStart, Operator::kFoldable,  // opcode
245       "Start",                                // name
246       0, 0, 0, value_output_count, 1, 1);     // counts
247 }
248
249
250 const Operator* CommonOperatorBuilder::Loop(int control_input_count) {
251   switch (control_input_count) {
252 #define CACHED_LOOP(input_count) \
253   case input_count:              \
254     return &cache_.kLoop##input_count##Operator;
255     CACHED_LOOP_LIST(CACHED_LOOP)
256 #undef CACHED_LOOP
257     default:
258       break;
259   }
260   // Uncached.
261   return new (zone()) Operator(              // --
262       IrOpcode::kLoop, Operator::kFoldable,  // opcode
263       "Loop",                                // name
264       0, 0, control_input_count, 0, 0, 1);   // counts
265 }
266
267
268 const Operator* CommonOperatorBuilder::Merge(int control_input_count) {
269   switch (control_input_count) {
270 #define CACHED_MERGE(input_count) \
271   case input_count:               \
272     return &cache_.kMerge##input_count##Operator;
273     CACHED_MERGE_LIST(CACHED_MERGE)
274 #undef CACHED_MERGE
275     default:
276       break;
277   }
278   // Uncached.
279   return new (zone()) Operator(               // --
280       IrOpcode::kMerge, Operator::kFoldable,  // opcode
281       "Merge",                                // name
282       0, 0, control_input_count, 0, 0, 1);    // counts
283 }
284
285
286 const Operator* CommonOperatorBuilder::Terminate(int effects) {
287   return new (zone()) Operator(               // --
288       IrOpcode::kTerminate, Operator::kPure,  // opcode
289       "Terminate",                            // name
290       0, effects, 1, 0, 0, 1);                // counts
291 }
292
293
294 const Operator* CommonOperatorBuilder::Parameter(int index) {
295   switch (index) {
296 #define CACHED_PARAMETER(index) \
297   case index:                   \
298     return &cache_.kParameter##index##Operator;
299     CACHED_PARAMETER_LIST(CACHED_PARAMETER)
300 #undef CACHED_PARAMETER
301     default:
302       break;
303   }
304   // Uncached.
305   return new (zone()) Operator1<int>(         // --
306       IrOpcode::kParameter, Operator::kPure,  // opcode
307       "Parameter",                            // name
308       1, 0, 0, 1, 0, 0,                       // counts
309       index);                                 // parameter
310 }
311
312
313 const Operator* CommonOperatorBuilder::Int32Constant(int32_t value) {
314   return new (zone()) Operator1<int32_t>(         // --
315       IrOpcode::kInt32Constant, Operator::kPure,  // opcode
316       "Int32Constant",                            // name
317       0, 0, 0, 1, 0, 0,                           // counts
318       value);                                     // parameter
319 }
320
321
322 const Operator* CommonOperatorBuilder::Int64Constant(int64_t value) {
323   return new (zone()) Operator1<int64_t>(         // --
324       IrOpcode::kInt64Constant, Operator::kPure,  // opcode
325       "Int64Constant",                            // name
326       0, 0, 0, 1, 0, 0,                           // counts
327       value);                                     // parameter
328 }
329
330
331 const Operator* CommonOperatorBuilder::Float32Constant(volatile float value) {
332   return new (zone())
333       Operator1<float, base::bit_equal_to<float>, base::bit_hash<float>>(  // --
334           IrOpcode::kFloat32Constant, Operator::kPure,  // opcode
335           "Float32Constant",                            // name
336           0, 0, 0, 1, 0, 0,                             // counts
337           value);                                       // parameter
338 }
339
340
341 const Operator* CommonOperatorBuilder::Float64Constant(volatile double value) {
342   return new (zone()) Operator1<double, base::bit_equal_to<double>,
343                                 base::bit_hash<double>>(  // --
344       IrOpcode::kFloat64Constant, Operator::kPure,        // opcode
345       "Float64Constant",                                  // name
346       0, 0, 0, 1, 0, 0,                                   // counts
347       value);                                             // parameter
348 }
349
350
351 const Operator* CommonOperatorBuilder::ExternalConstant(
352     const ExternalReference& value) {
353   return new (zone()) Operator1<ExternalReference>(  // --
354       IrOpcode::kExternalConstant, Operator::kPure,  // opcode
355       "ExternalConstant",                            // name
356       0, 0, 0, 1, 0, 0,                              // counts
357       value);                                        // parameter
358 }
359
360
361 const Operator* CommonOperatorBuilder::NumberConstant(volatile double value) {
362   return new (zone()) Operator1<double, base::bit_equal_to<double>,
363                                 base::bit_hash<double>>(  // --
364       IrOpcode::kNumberConstant, Operator::kPure,         // opcode
365       "NumberConstant",                                   // name
366       0, 0, 0, 1, 0, 0,                                   // counts
367       value);                                             // parameter
368 }
369
370
371 const Operator* CommonOperatorBuilder::HeapConstant(
372     const Unique<HeapObject>& value) {
373   return new (zone()) Operator1<Unique<HeapObject>>(  // --
374       IrOpcode::kHeapConstant, Operator::kPure,       // opcode
375       "HeapConstant",                                 // name
376       0, 0, 0, 1, 0, 0,                               // counts
377       value);                                         // parameter
378 }
379
380
381 const Operator* CommonOperatorBuilder::Select(MachineType type,
382                                               BranchHint hint) {
383   return new (zone()) Operator1<SelectParameters>(  // --
384       IrOpcode::kSelect, Operator::kPure,           // opcode
385       "Select",                                     // name
386       3, 0, 0, 1, 0, 0,                             // counts
387       SelectParameters(type, hint));                // parameter
388 }
389
390
391 const Operator* CommonOperatorBuilder::Phi(MachineType type, int arguments) {
392   DCHECK(arguments > 0);                       // Disallow empty phis.
393   return new (zone()) Operator1<MachineType>(  // --
394       IrOpcode::kPhi, Operator::kPure,         // opcode
395       "Phi",                                   // name
396       arguments, 0, 1, 1, 0, 0,                // counts
397       type);                                   // parameter
398 }
399
400
401 const Operator* CommonOperatorBuilder::EffectPhi(int arguments) {
402   DCHECK(arguments > 0);                      // Disallow empty phis.
403   return new (zone()) Operator(               // --
404       IrOpcode::kEffectPhi, Operator::kPure,  // opcode
405       "EffectPhi",                            // name
406       0, arguments, 1, 0, 1, 0);              // counts
407 }
408
409
410 const Operator* CommonOperatorBuilder::ValueEffect(int arguments) {
411   DCHECK(arguments > 0);                        // Disallow empty value effects.
412   return new (zone()) Operator(                 // --
413       IrOpcode::kValueEffect, Operator::kPure,  // opcode
414       "ValueEffect",                            // name
415       arguments, 0, 0, 0, 1, 0);                // counts
416 }
417
418
419 const Operator* CommonOperatorBuilder::Finish(int arguments) {
420   DCHECK(arguments > 0);                   // Disallow empty finishes.
421   return new (zone()) Operator(            // --
422       IrOpcode::kFinish, Operator::kPure,  // opcode
423       "Finish",                            // name
424       1, arguments, 0, 1, 0, 0);           // counts
425 }
426
427
428 const Operator* CommonOperatorBuilder::StateValues(int arguments) {
429   return new (zone()) Operator(                 // --
430       IrOpcode::kStateValues, Operator::kPure,  // opcode
431       "StateValues",                            // name
432       arguments, 0, 0, 1, 0, 0);                // counts
433 }
434
435
436 const Operator* CommonOperatorBuilder::FrameState(
437     FrameStateType type, BailoutId bailout_id,
438     OutputFrameStateCombine state_combine, MaybeHandle<JSFunction> jsfunction) {
439   return new (zone()) Operator1<FrameStateCallInfo>(  // --
440       IrOpcode::kFrameState, Operator::kPure,         // opcode
441       "FrameState",                                   // name
442       4, 0, 0, 1, 0, 0,                               // counts
443       FrameStateCallInfo(type, bailout_id, state_combine, jsfunction));
444 }
445
446
447 const Operator* CommonOperatorBuilder::Call(const CallDescriptor* descriptor) {
448   class CallOperator FINAL : public Operator1<const CallDescriptor*> {
449    public:
450     CallOperator(const CallDescriptor* descriptor, const char* mnemonic)
451         : Operator1<const CallDescriptor*>(
452               IrOpcode::kCall, descriptor->properties(), mnemonic,
453               descriptor->InputCount() + descriptor->FrameStateCount(),
454               Operator::ZeroIfPure(descriptor->properties()),
455               Operator::ZeroIfPure(descriptor->properties()),
456               descriptor->ReturnCount(),
457               Operator::ZeroIfPure(descriptor->properties()), 0, descriptor) {}
458
459     void PrintParameter(std::ostream& os) const OVERRIDE {
460       os << "[" << *parameter() << "]";
461     }
462   };
463   return new (zone()) CallOperator(descriptor, "Call");
464 }
465
466
467 const Operator* CommonOperatorBuilder::Projection(size_t index) {
468   return new (zone()) Operator1<size_t>(       // --
469       IrOpcode::kProjection, Operator::kPure,  // opcode
470       "Projection",                            // name
471       1, 0, 0, 1, 0, 0,                        // counts
472       index);                                  // parameter
473 }
474
475 }  // namespace compiler
476 }  // namespace internal
477 }  // namespace v8