649b0d68d1097c289603e206f34f10a623be2f2a
[platform/upstream/nodejs.git] / deps / v8 / src / compiler / js-graph.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/code-stubs.h"
6 #include "src/compiler/js-graph.h"
7 #include "src/compiler/node-properties.h"
8 #include "src/compiler/typer.h"
9
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13
14 Node* JSGraph::ImmovableHeapConstant(Handle<HeapObject> object) {
15   Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(object);
16   return graph()->NewNode(common()->HeapConstant(unique));
17 }
18
19
20 Node* JSGraph::CEntryStubConstant(int result_size) {
21   if (result_size == 1) {
22     if (!c_entry_stub_constant_.is_set()) {
23       c_entry_stub_constant_.set(
24           ImmovableHeapConstant(CEntryStub(isolate(), 1).GetCode()));
25     }
26     return c_entry_stub_constant_.get();
27   }
28
29   return ImmovableHeapConstant(CEntryStub(isolate(), result_size).GetCode());
30 }
31
32
33 Node* JSGraph::UndefinedConstant() {
34   if (!undefined_constant_.is_set()) {
35     undefined_constant_.set(
36         ImmovableHeapConstant(factory()->undefined_value()));
37   }
38   return undefined_constant_.get();
39 }
40
41
42 Node* JSGraph::TheHoleConstant() {
43   if (!the_hole_constant_.is_set()) {
44     the_hole_constant_.set(ImmovableHeapConstant(factory()->the_hole_value()));
45   }
46   return the_hole_constant_.get();
47 }
48
49
50 Node* JSGraph::TrueConstant() {
51   if (!true_constant_.is_set()) {
52     true_constant_.set(ImmovableHeapConstant(factory()->true_value()));
53   }
54   return true_constant_.get();
55 }
56
57
58 Node* JSGraph::FalseConstant() {
59   if (!false_constant_.is_set()) {
60     false_constant_.set(ImmovableHeapConstant(factory()->false_value()));
61   }
62   return false_constant_.get();
63 }
64
65
66 Node* JSGraph::NullConstant() {
67   if (!null_constant_.is_set()) {
68     null_constant_.set(ImmovableHeapConstant(factory()->null_value()));
69   }
70   return null_constant_.get();
71 }
72
73
74 Node* JSGraph::ZeroConstant() {
75   if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0));
76   return zero_constant_.get();
77 }
78
79
80 Node* JSGraph::OneConstant() {
81   if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0));
82   return one_constant_.get();
83 }
84
85
86 Node* JSGraph::NaNConstant() {
87   if (!nan_constant_.is_set()) {
88     nan_constant_.set(NumberConstant(std::numeric_limits<double>::quiet_NaN()));
89   }
90   return nan_constant_.get();
91 }
92
93
94 Node* JSGraph::HeapConstant(Unique<HeapObject> value) {
95   // TODO(turbofan): canonicalize heap constants using Unique<T>
96   return graph()->NewNode(common()->HeapConstant(value));
97 }
98
99
100 Node* JSGraph::HeapConstant(Handle<HeapObject> value) {
101   // TODO(titzer): We could also match against the addresses of immortable
102   // immovables here, even without access to the heap, thus always
103   // canonicalizing references to them.
104   // return HeapConstant(Unique<Object>::CreateUninitialized(value));
105   // TODO(turbofan): This is a work-around to make Unique::HashCode() work for
106   // value numbering. We need some sane way to compute a unique hash code for
107   // arbitrary handles here.
108   Unique<HeapObject> unique(reinterpret_cast<Address>(*value.location()),
109                             value);
110   return HeapConstant(unique);
111 }
112
113
114 Node* JSGraph::Constant(Handle<Object> value) {
115   // Dereference the handle to determine if a number constant or other
116   // canonicalized node can be used.
117   if (value->IsNumber()) {
118     return Constant(value->Number());
119   } else if (value->IsUndefined()) {
120     return UndefinedConstant();
121   } else if (value->IsTrue()) {
122     return TrueConstant();
123   } else if (value->IsFalse()) {
124     return FalseConstant();
125   } else if (value->IsNull()) {
126     return NullConstant();
127   } else if (value->IsTheHole()) {
128     return TheHoleConstant();
129   } else {
130     return HeapConstant(Handle<HeapObject>::cast(value));
131   }
132 }
133
134
135 Node* JSGraph::Constant(double value) {
136   if (bit_cast<int64_t>(value) == bit_cast<int64_t>(0.0)) return ZeroConstant();
137   if (bit_cast<int64_t>(value) == bit_cast<int64_t>(1.0)) return OneConstant();
138   return NumberConstant(value);
139 }
140
141
142 Node* JSGraph::Constant(int32_t value) {
143   if (value == 0) return ZeroConstant();
144   if (value == 1) return OneConstant();
145   return NumberConstant(value);
146 }
147
148
149 Node* JSGraph::Int32Constant(int32_t value) {
150   Node** loc = cache_.FindInt32Constant(value);
151   if (*loc == NULL) {
152     *loc = graph()->NewNode(common()->Int32Constant(value));
153   }
154   return *loc;
155 }
156
157
158 Node* JSGraph::Int64Constant(int64_t value) {
159   Node** loc = cache_.FindInt64Constant(value);
160   if (*loc == NULL) {
161     *loc = graph()->NewNode(common()->Int64Constant(value));
162   }
163   return *loc;
164 }
165
166
167 Node* JSGraph::NumberConstant(double value) {
168   Node** loc = cache_.FindNumberConstant(value);
169   if (*loc == NULL) {
170     *loc = graph()->NewNode(common()->NumberConstant(value));
171   }
172   return *loc;
173 }
174
175
176 Node* JSGraph::Float32Constant(float value) {
177   Node** loc = cache_.FindFloat32Constant(value);
178   if (*loc == NULL) {
179     *loc = graph()->NewNode(common()->Float32Constant(value));
180   }
181   return *loc;
182 }
183
184
185 Node* JSGraph::Float64Constant(double value) {
186   Node** loc = cache_.FindFloat64Constant(value);
187   if (*loc == NULL) {
188     *loc = graph()->NewNode(common()->Float64Constant(value));
189   }
190   return *loc;
191 }
192
193
194 Node* JSGraph::ExternalConstant(ExternalReference reference) {
195   Node** loc = cache_.FindExternalConstant(reference);
196   if (*loc == NULL) {
197     *loc = graph()->NewNode(common()->ExternalConstant(reference));
198   }
199   return *loc;
200 }
201
202
203 Node* JSGraph::EmptyFrameState() {
204   if (!empty_frame_state_.is_set()) {
205     Node* values = graph()->NewNode(common()->StateValues(0));
206     Node* state_node = graph()->NewNode(
207         common()->FrameState(JS_FRAME, BailoutId(0),
208                              OutputFrameStateCombine::Ignore()),
209         values, values, values, NoContextConstant(), UndefinedConstant());
210     empty_frame_state_.set(state_node);
211   }
212   return empty_frame_state_.get();
213 }
214
215
216 Node* JSGraph::DeadControl() {
217   if (!dead_control_.is_set()) {
218     Node* dead_node = graph()->NewNode(common()->Dead());
219     dead_control_.set(dead_node);
220   }
221   return dead_control_.get();
222 }
223
224
225 void JSGraph::GetCachedNodes(NodeVector* nodes) {
226   cache_.GetCachedNodes(nodes);
227   SetOncePointer<Node>* ptrs[] = {
228       &c_entry_stub_constant_, &undefined_constant_, &the_hole_constant_,
229       &true_constant_,         &false_constant_,     &null_constant_,
230       &zero_constant_,         &one_constant_,       &nan_constant_};
231   for (size_t i = 0; i < arraysize(ptrs); i++) {
232     if (ptrs[i]->is_set()) nodes->push_back(ptrs[i]->get());
233   }
234 }
235
236 }  // namespace compiler
237 }  // namespace internal
238 }  // namespace v8