Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / 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 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 {                           \
119     Name##Operator()                                                        \
120         : Operator(IrOpcode::k##Name, properties, #Name, value_input_count, \
121                    effect_input_count, control_input_count, 0, 0,           \
122                    control_output_count) {}                                 \
123   };                                                                        \
124   Name##Operator k##Name##Operator;
125   CACHED_OP_LIST(CACHED)
126 #undef CACHED
127 };
128
129
130 static base::LazyInstance<CommonOperatorGlobalCache>::type kCache =
131     LAZY_INSTANCE_INITIALIZER;
132
133
134 CommonOperatorBuilder::CommonOperatorBuilder(Zone* zone)
135     : cache_(kCache.Get()), zone_(zone) {}
136
137
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;                                   \
142   }
143 CACHED_OP_LIST(CACHED)
144 #undef CACHED
145
146
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);
150 }
151
152
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
158       "Start",                                // name
159       0, 0, 0, value_output_count, 1, 1);     // counts
160 }
161
162
163 const Operator* CommonOperatorBuilder::Merge(int controls) {
164   return new (zone()) Operator(               // --
165       IrOpcode::kMerge, Operator::kFoldable,  // opcode
166       "Merge",                                // name
167       0, 0, controls, 0, 0, 1);               // counts
168 }
169
170
171 const Operator* CommonOperatorBuilder::Loop(int controls) {
172   return new (zone()) Operator(              // --
173       IrOpcode::kLoop, Operator::kFoldable,  // opcode
174       "Loop",                                // name
175       0, 0, controls, 0, 0, 1);              // counts
176 }
177
178
179 const Operator* CommonOperatorBuilder::Terminate(int effects) {
180   return new (zone()) Operator(               // --
181       IrOpcode::kTerminate, Operator::kPure,  // opcode
182       "Terminate",                            // name
183       0, effects, 1, 0, 0, 1);                // counts
184 }
185
186
187 const Operator* CommonOperatorBuilder::Parameter(int index) {
188   return new (zone()) Operator1<int>(         // --
189       IrOpcode::kParameter, Operator::kPure,  // opcode
190       "Parameter",                            // name
191       1, 0, 0, 1, 0, 0,                       // counts
192       index);                                 // parameter
193 }
194
195
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
201       value);                                     // parameter
202 }
203
204
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
210       value);                                     // parameter
211 }
212
213
214 const Operator* CommonOperatorBuilder::Float32Constant(volatile float value) {
215   return new (zone())
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
220           value);                                       // parameter
221 }
222
223
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
230       value);                                             // parameter
231 }
232
233
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
240       value);                                        // parameter
241 }
242
243
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
250       value);                                             // parameter
251 }
252
253
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
260       value);                                         // parameter
261 }
262
263
264 const Operator* CommonOperatorBuilder::Select(MachineType type,
265                                               BranchHint hint) {
266   return new (zone()) Operator1<SelectParameters>(  // --
267       IrOpcode::kSelect, Operator::kPure,           // opcode
268       "Select",                                     // name
269       3, 0, 0, 1, 0, 0,                             // counts
270       SelectParameters(type, hint));                // parameter
271 }
272
273
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
278       "Phi",                                   // name
279       arguments, 0, 1, 1, 0, 0,                // counts
280       type);                                   // parameter
281 }
282
283
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
288       "EffectPhi",                            // name
289       0, arguments, 1, 0, 1, 0);              // counts
290 }
291
292
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
299 }
300
301
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
306       "Finish",                            // name
307       1, arguments, 0, 1, 0, 0);           // counts
308 }
309
310
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
316 }
317
318
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));
327 }
328
329
330 const Operator* CommonOperatorBuilder::Call(const CallDescriptor* descriptor) {
331   class CallOperator FINAL : public Operator1<const CallDescriptor*> {
332    public:
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) {}
341
342     virtual void PrintParameter(std::ostream& os) const OVERRIDE {
343       os << "[" << *parameter() << "]";
344     }
345   };
346   return new (zone()) CallOperator(descriptor, "Call");
347 }
348
349
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
355       index);                                  // parameter
356 }
357
358 }  // namespace compiler
359 }  // namespace internal
360 }  // namespace v8