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/code-stubs.h"
6 #include "src/compiler/js-graph.h"
7 #include "src/compiler/node-properties.h"
8 #include "src/compiler/typer.h"
14 Node* JSGraph::ImmovableHeapConstant(Handle<HeapObject> object) {
15 Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(object);
16 return graph()->NewNode(common()->HeapConstant(unique));
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()));
26 return c_entry_stub_constant_.get();
29 return ImmovableHeapConstant(CEntryStub(isolate(), result_size).GetCode());
33 Node* JSGraph::UndefinedConstant() {
34 if (!undefined_constant_.is_set()) {
35 undefined_constant_.set(
36 ImmovableHeapConstant(factory()->undefined_value()));
38 return undefined_constant_.get();
42 Node* JSGraph::TheHoleConstant() {
43 if (!the_hole_constant_.is_set()) {
44 the_hole_constant_.set(ImmovableHeapConstant(factory()->the_hole_value()));
46 return the_hole_constant_.get();
50 Node* JSGraph::TrueConstant() {
51 if (!true_constant_.is_set()) {
52 true_constant_.set(ImmovableHeapConstant(factory()->true_value()));
54 return true_constant_.get();
58 Node* JSGraph::FalseConstant() {
59 if (!false_constant_.is_set()) {
60 false_constant_.set(ImmovableHeapConstant(factory()->false_value()));
62 return false_constant_.get();
66 Node* JSGraph::NullConstant() {
67 if (!null_constant_.is_set()) {
68 null_constant_.set(ImmovableHeapConstant(factory()->null_value()));
70 return null_constant_.get();
74 Node* JSGraph::ZeroConstant() {
75 if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0));
76 return zero_constant_.get();
80 Node* JSGraph::OneConstant() {
81 if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0));
82 return one_constant_.get();
86 Node* JSGraph::NaNConstant() {
87 if (!nan_constant_.is_set()) {
88 nan_constant_.set(NumberConstant(std::numeric_limits<double>::quiet_NaN()));
90 return nan_constant_.get();
94 Node* JSGraph::HeapConstant(Unique<HeapObject> value) {
95 // TODO(turbofan): canonicalize heap constants using Unique<T>
96 return graph()->NewNode(common()->HeapConstant(value));
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()),
110 return HeapConstant(unique);
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();
130 return HeapConstant(Handle<HeapObject>::cast(value));
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);
142 Node* JSGraph::Constant(int32_t value) {
143 if (value == 0) return ZeroConstant();
144 if (value == 1) return OneConstant();
145 return NumberConstant(value);
149 Node* JSGraph::Int32Constant(int32_t value) {
150 Node** loc = cache_.FindInt32Constant(value);
152 *loc = graph()->NewNode(common()->Int32Constant(value));
158 Node* JSGraph::Int64Constant(int64_t value) {
159 Node** loc = cache_.FindInt64Constant(value);
161 *loc = graph()->NewNode(common()->Int64Constant(value));
167 Node* JSGraph::NumberConstant(double value) {
168 Node** loc = cache_.FindNumberConstant(value);
170 *loc = graph()->NewNode(common()->NumberConstant(value));
176 Node* JSGraph::Float32Constant(float value) {
177 Node** loc = cache_.FindFloat32Constant(value);
179 *loc = graph()->NewNode(common()->Float32Constant(value));
185 Node* JSGraph::Float64Constant(double value) {
186 Node** loc = cache_.FindFloat64Constant(value);
188 *loc = graph()->NewNode(common()->Float64Constant(value));
194 Node* JSGraph::ExternalConstant(ExternalReference reference) {
195 Node** loc = cache_.FindExternalConstant(reference);
197 *loc = graph()->NewNode(common()->ExternalConstant(reference));
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);
212 return empty_frame_state_.get();
216 Node* JSGraph::DeadControl() {
217 if (!dead_control_.is_set()) {
218 Node* dead_node = graph()->NewNode(common()->Dead());
219 dead_control_.set(dead_node);
221 return dead_control_.get();
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());
236 } // namespace compiler
237 } // namespace internal