8aea3df5adc85867877924a94d78aeae6928c174
[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 size_t ProjectionIndexOf(const Operator* const op) {
107   DCHECK_EQ(IrOpcode::kProjection, op->opcode());
108   return OpParameter<size_t>(op);
109 }
110
111
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)
123
124
125 #define CACHED_LOOP_LIST(V) \
126   V(1)                      \
127   V(2)
128
129
130 #define CACHED_MERGE_LIST(V) \
131   V(1)                       \
132   V(2)                       \
133   V(3)                       \
134   V(4)                       \
135   V(5)                       \
136   V(6)                       \
137   V(7)                       \
138   V(8)
139
140
141 #define CACHED_PARAMETER_LIST(V) \
142   V(0)                           \
143   V(1)                           \
144   V(2)                           \
145   V(3)                           \
146   V(4)                           \
147   V(5)                           \
148   V(6)
149
150
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 {                            \
156     Name##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) {}                                  \
161   };                                                                         \
162   Name##Operator k##Name##Operator;
163   CACHED_OP_LIST(CACHED)
164 #undef CACHED
165
166   template <BranchHint kBranchHint>
167   struct BranchOperator FINAL : public Operator1<BranchHint> {
168     BranchOperator()
169         : Operator1<BranchHint>(                      // --
170               IrOpcode::kBranch, Operator::kKontrol,  // opcode
171               "Branch",                               // name
172               1, 0, 1, 0, 0, 2,                       // counts
173               kBranchHint) {}                         // parameter
174   };
175   BranchOperator<BranchHint::kNone> kBranchNoneOperator;
176   BranchOperator<BranchHint::kTrue> kBranchTrueOperator;
177   BranchOperator<BranchHint::kFalse> kBranchFalseOperator;
178
179   template <size_t kInputCount>
180   struct LoopOperator FINAL : public Operator {
181     LoopOperator()
182         : Operator(                                 // --
183               IrOpcode::kLoop, Operator::kKontrol,  // opcode
184               "Loop",                               // name
185               0, 0, kInputCount, 0, 0, 1) {}        // counts
186   };
187 #define CACHED_LOOP(input_count) \
188   LoopOperator<input_count> kLoop##input_count##Operator;
189   CACHED_LOOP_LIST(CACHED_LOOP)
190 #undef CACHED_LOOP
191
192   template <size_t kInputCount>
193   struct MergeOperator FINAL : public Operator {
194     MergeOperator()
195         : Operator(                                  // --
196               IrOpcode::kMerge, Operator::kKontrol,  // opcode
197               "Merge",                               // name
198               0, 0, kInputCount, 0, 0, 1) {}         // counts
199   };
200 #define CACHED_MERGE(input_count) \
201   MergeOperator<input_count> kMerge##input_count##Operator;
202   CACHED_MERGE_LIST(CACHED_MERGE)
203 #undef CACHED_MERGE
204
205   template <int kIndex>
206   struct ParameterOperator FINAL : public Operator1<int> {
207     ParameterOperator()
208         : Operator1<int>(                             // --
209               IrOpcode::kParameter, Operator::kPure,  // opcode
210               "Parameter",                            // name
211               1, 0, 0, 1, 0, 0,                       // counts,
212               kIndex) {}                              // parameter
213   };
214 #define CACHED_PARAMETER(index) \
215   ParameterOperator<index> kParameter##index##Operator;
216   CACHED_PARAMETER_LIST(CACHED_PARAMETER)
217 #undef CACHED_PARAMETER
218 };
219
220
221 static base::LazyInstance<CommonOperatorGlobalCache>::type kCache =
222     LAZY_INSTANCE_INITIALIZER;
223
224
225 CommonOperatorBuilder::CommonOperatorBuilder(Zone* zone)
226     : cache_(kCache.Get()), zone_(zone) {}
227
228
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;                                        \
234   }
235 CACHED_OP_LIST(CACHED)
236 #undef CACHED
237
238
239 const Operator* CommonOperatorBuilder::Branch(BranchHint hint) {
240   switch (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;
247   }
248   UNREACHABLE();
249   return nullptr;
250 }
251
252
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
257       "Switch",                               // name
258       1, 0, 1, 0, 0, control_output_count);   // counts
259 }
260
261
262 const Operator* CommonOperatorBuilder::IfValue(int32_t index) {
263   return new (zone()) Operator1<int32_t>(      // --
264       IrOpcode::kIfValue, Operator::kKontrol,  // opcode
265       "IfValue",                               // name
266       0, 0, 1, 0, 0, 1,                        // counts
267       index);                                  // parameter
268 }
269
270
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
276       "Start",                                // name
277       0, 0, 0, value_output_count, 1, 1);     // counts
278 }
279
280
281 const Operator* CommonOperatorBuilder::Loop(int control_input_count) {
282   switch (control_input_count) {
283 #define CACHED_LOOP(input_count) \
284   case input_count:              \
285     return &cache_.kLoop##input_count##Operator;
286     CACHED_LOOP_LIST(CACHED_LOOP)
287 #undef CACHED_LOOP
288     default:
289       break;
290   }
291   // Uncached.
292   return new (zone()) Operator(             // --
293       IrOpcode::kLoop, Operator::kKontrol,  // opcode
294       "Loop",                               // name
295       0, 0, control_input_count, 0, 0, 1);  // counts
296 }
297
298
299 const Operator* CommonOperatorBuilder::Merge(int control_input_count) {
300   switch (control_input_count) {
301 #define CACHED_MERGE(input_count) \
302   case input_count:               \
303     return &cache_.kMerge##input_count##Operator;
304     CACHED_MERGE_LIST(CACHED_MERGE)
305 #undef CACHED_MERGE
306     default:
307       break;
308   }
309   // Uncached.
310   return new (zone()) Operator(              // --
311       IrOpcode::kMerge, Operator::kKontrol,  // opcode
312       "Merge",                               // name
313       0, 0, control_input_count, 0, 0, 1);   // counts
314 }
315
316
317 const Operator* CommonOperatorBuilder::Parameter(int index) {
318   switch (index) {
319 #define CACHED_PARAMETER(index) \
320   case index:                   \
321     return &cache_.kParameter##index##Operator;
322     CACHED_PARAMETER_LIST(CACHED_PARAMETER)
323 #undef CACHED_PARAMETER
324     default:
325       break;
326   }
327   // Uncached.
328   return new (zone()) Operator1<int>(         // --
329       IrOpcode::kParameter, Operator::kPure,  // opcode
330       "Parameter",                            // name
331       1, 0, 0, 1, 0, 0,                       // counts
332       index);                                 // parameter
333 }
334
335
336 const Operator* CommonOperatorBuilder::OsrValue(int index) {
337   return new (zone()) Operator1<int>(                // --
338       IrOpcode::kOsrValue, Operator::kNoProperties,  // opcode
339       "OsrValue",                                    // name
340       0, 0, 1, 1, 0, 0,                              // counts
341       index);                                        // parameter
342 }
343
344
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
350       value);                                     // parameter
351 }
352
353
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
359       value);                                     // parameter
360 }
361
362
363 const Operator* CommonOperatorBuilder::Float32Constant(volatile float value) {
364   return new (zone())
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
369           value);                                       // parameter
370 }
371
372
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
379       value);                                             // parameter
380 }
381
382
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
389       value);                                        // parameter
390 }
391
392
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
399       value);                                             // parameter
400 }
401
402
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
409       value);                                         // parameter
410 }
411
412
413 const Operator* CommonOperatorBuilder::Select(MachineType type,
414                                               BranchHint hint) {
415   return new (zone()) Operator1<SelectParameters>(  // --
416       IrOpcode::kSelect, Operator::kPure,           // opcode
417       "Select",                                     // name
418       3, 0, 0, 1, 0, 0,                             // counts
419       SelectParameters(type, hint));                // parameter
420 }
421
422
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
427       "Phi",                                   // name
428       arguments, 0, 1, 1, 0, 0,                // counts
429       type);                                   // parameter
430 }
431
432
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
437       "EffectPhi",                            // name
438       0, arguments, 1, 0, 1, 0);              // counts
439 }
440
441
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
446       "EffectSet",                            // name
447       0, arguments, 0, 0, 1, 0);              // counts
448 }
449
450
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
457 }
458
459
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
464       "Finish",                            // name
465       1, arguments, 0, 1, 0, 0);           // counts
466 }
467
468
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
474 }
475
476
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));
485 }
486
487
488 const Operator* CommonOperatorBuilder::Call(const CallDescriptor* descriptor) {
489   class CallOperator FINAL : public Operator1<const CallDescriptor*> {
490    public:
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) {}
499
500     void PrintParameter(std::ostream& os) const OVERRIDE {
501       os << "[" << *parameter() << "]";
502     }
503   };
504   return new (zone()) CallOperator(descriptor, "Call");
505 }
506
507
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
514       index);                                    // parameter
515 }
516
517
518 const Operator* CommonOperatorBuilder::ResizeMergeOrPhi(const Operator* op,
519                                                         int size) {
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) {
525     return Merge(size);
526   } else if (op->opcode() == IrOpcode::kLoop) {
527     return Loop(size);
528   } else {
529     UNREACHABLE();
530     return nullptr;
531   }
532 }
533
534
535 }  // namespace compiler
536 }  // namespace internal
537 }  // namespace v8