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-inl.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() {
21 if (!c_entry_stub_constant_.is_set()) {
22 c_entry_stub_constant_.set(
23 ImmovableHeapConstant(CEntryStub(isolate(), 1).GetCode()));
25 return c_entry_stub_constant_.get();
29 Node* JSGraph::UndefinedConstant() {
30 if (!undefined_constant_.is_set()) {
31 undefined_constant_.set(
32 ImmovableHeapConstant(factory()->undefined_value()));
34 return undefined_constant_.get();
38 Node* JSGraph::TheHoleConstant() {
39 if (!the_hole_constant_.is_set()) {
40 the_hole_constant_.set(ImmovableHeapConstant(factory()->the_hole_value()));
42 return the_hole_constant_.get();
46 Node* JSGraph::TrueConstant() {
47 if (!true_constant_.is_set()) {
48 true_constant_.set(ImmovableHeapConstant(factory()->true_value()));
50 return true_constant_.get();
54 Node* JSGraph::FalseConstant() {
55 if (!false_constant_.is_set()) {
56 false_constant_.set(ImmovableHeapConstant(factory()->false_value()));
58 return false_constant_.get();
62 Node* JSGraph::NullConstant() {
63 if (!null_constant_.is_set()) {
64 null_constant_.set(ImmovableHeapConstant(factory()->null_value()));
66 return null_constant_.get();
70 Node* JSGraph::ZeroConstant() {
71 if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0));
72 return zero_constant_.get();
76 Node* JSGraph::OneConstant() {
77 if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0));
78 return one_constant_.get();
82 Node* JSGraph::NaNConstant() {
83 if (!nan_constant_.is_set()) {
84 nan_constant_.set(NumberConstant(base::OS::nan_value()));
86 return nan_constant_.get();
90 Node* JSGraph::HeapConstant(Unique<HeapObject> value) {
91 // TODO(turbofan): canonicalize heap constants using Unique<T>
92 return graph()->NewNode(common()->HeapConstant(value));
96 Node* JSGraph::HeapConstant(Handle<HeapObject> value) {
97 // TODO(titzer): We could also match against the addresses of immortable
98 // immovables here, even without access to the heap, thus always
99 // canonicalizing references to them.
100 // return HeapConstant(Unique<Object>::CreateUninitialized(value));
101 // TODO(turbofan): This is a work-around to make Unique::HashCode() work for
102 // value numbering. We need some sane way to compute a unique hash code for
103 // arbitrary handles here.
104 Unique<HeapObject> unique(reinterpret_cast<Address>(*value.location()),
106 return HeapConstant(unique);
110 Node* JSGraph::Constant(Handle<Object> value) {
111 // Dereference the handle to determine if a number constant or other
112 // canonicalized node can be used.
113 if (value->IsNumber()) {
114 return Constant(value->Number());
115 } else if (value->IsUndefined()) {
116 return UndefinedConstant();
117 } else if (value->IsTrue()) {
118 return TrueConstant();
119 } else if (value->IsFalse()) {
120 return FalseConstant();
121 } else if (value->IsNull()) {
122 return NullConstant();
123 } else if (value->IsTheHole()) {
124 return TheHoleConstant();
126 return HeapConstant(Handle<HeapObject>::cast(value));
131 Node* JSGraph::Constant(double value) {
132 if (bit_cast<int64_t>(value) == bit_cast<int64_t>(0.0)) return ZeroConstant();
133 if (bit_cast<int64_t>(value) == bit_cast<int64_t>(1.0)) return OneConstant();
134 return NumberConstant(value);
138 Node* JSGraph::Constant(int32_t value) {
139 if (value == 0) return ZeroConstant();
140 if (value == 1) return OneConstant();
141 return NumberConstant(value);
145 Node* JSGraph::Int32Constant(int32_t value) {
146 Node** loc = cache_.FindInt32Constant(value);
148 *loc = graph()->NewNode(common()->Int32Constant(value));
154 Node* JSGraph::Int64Constant(int64_t value) {
155 Node** loc = cache_.FindInt64Constant(value);
157 *loc = graph()->NewNode(common()->Int64Constant(value));
163 Node* JSGraph::NumberConstant(double value) {
164 Node** loc = cache_.FindNumberConstant(value);
166 *loc = graph()->NewNode(common()->NumberConstant(value));
172 Node* JSGraph::Float32Constant(float value) {
173 // TODO(turbofan): cache float32 constants.
174 return graph()->NewNode(common()->Float32Constant(value));
178 Node* JSGraph::Float64Constant(double value) {
179 Node** loc = cache_.FindFloat64Constant(value);
181 *loc = graph()->NewNode(common()->Float64Constant(value));
187 Node* JSGraph::ExternalConstant(ExternalReference reference) {
188 Node** loc = cache_.FindExternalConstant(reference);
190 *loc = graph()->NewNode(common()->ExternalConstant(reference));
196 void JSGraph::GetCachedNodes(NodeVector* nodes) {
197 cache_.GetCachedNodes(nodes);
198 SetOncePointer<Node>* ptrs[] = {
199 &c_entry_stub_constant_, &undefined_constant_, &the_hole_constant_,
200 &true_constant_, &false_constant_, &null_constant_,
201 &zero_constant_, &one_constant_, &nan_constant_};
202 for (size_t i = 0; i < arraysize(ptrs); i++) {
203 if (ptrs[i]->is_set()) nodes->push_back(ptrs[i]->get());
207 } // namespace compiler
208 } // namespace internal