blob: 7759ba14418c29c26aaa165d470958ba1abc0f93 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include "src/code-stubs.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006#include "src/compiler/js-graph.h"
7#include "src/compiler/node-properties-inl.h"
8#include "src/compiler/typer.h"
9
10namespace v8 {
11namespace internal {
12namespace compiler {
13
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014Node* JSGraph::ImmovableHeapConstant(Handle<HeapObject> object) {
15 Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(object);
16 return graph()->NewNode(common()->HeapConstant(unique));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017}
18
19
Emily Bernierd0a1eb72015-03-24 16:35:39 -040020Node* 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();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028
29 return ImmovableHeapConstant(CEntryStub(isolate(), result_size).GetCode());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030}
31
32
33Node* 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
42Node* 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
50Node* 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
58Node* 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
66Node* 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
74Node* JSGraph::ZeroConstant() {
75 if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0));
76 return zero_constant_.get();
77}
78
79
80Node* JSGraph::OneConstant() {
81 if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0));
82 return one_constant_.get();
83}
84
85
86Node* JSGraph::NaNConstant() {
87 if (!nan_constant_.is_set()) {
88 nan_constant_.set(NumberConstant(base::OS::nan_value()));
89 }
90 return nan_constant_.get();
91}
92
93
Emily Bernierd0a1eb72015-03-24 16:35:39 -040094Node* JSGraph::HeapConstant(Unique<HeapObject> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 // TODO(turbofan): canonicalize heap constants using Unique<T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040096 return graph()->NewNode(common()->HeapConstant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097}
98
99
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400100Node* JSGraph::HeapConstant(Handle<HeapObject> value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 // 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.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400108 Unique<HeapObject> unique(reinterpret_cast<Address>(*value.location()),
109 value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 return HeapConstant(unique);
111}
112
113
114Node* 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 {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400130 return HeapConstant(Handle<HeapObject>::cast(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 }
132}
133
134
135Node* 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
142Node* JSGraph::Constant(int32_t value) {
143 if (value == 0) return ZeroConstant();
144 if (value == 1) return OneConstant();
145 return NumberConstant(value);
146}
147
148
149Node* JSGraph::Int32Constant(int32_t value) {
150 Node** loc = cache_.FindInt32Constant(value);
151 if (*loc == NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400152 *loc = graph()->NewNode(common()->Int32Constant(value));
153 }
154 return *loc;
155}
156
157
158Node* JSGraph::Int64Constant(int64_t value) {
159 Node** loc = cache_.FindInt64Constant(value);
160 if (*loc == NULL) {
161 *loc = graph()->NewNode(common()->Int64Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 }
163 return *loc;
164}
165
166
167Node* JSGraph::NumberConstant(double value) {
168 Node** loc = cache_.FindNumberConstant(value);
169 if (*loc == NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400170 *loc = graph()->NewNode(common()->NumberConstant(value));
171 }
172 return *loc;
173}
174
175
176Node* JSGraph::Float32Constant(float value) {
177 Node** loc = cache_.FindFloat32Constant(value);
178 if (*loc == NULL) {
179 *loc = graph()->NewNode(common()->Float32Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 }
181 return *loc;
182}
183
184
185Node* JSGraph::Float64Constant(double value) {
186 Node** loc = cache_.FindFloat64Constant(value);
187 if (*loc == NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400188 *loc = graph()->NewNode(common()->Float64Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 }
190 return *loc;
191}
192
193
194Node* JSGraph::ExternalConstant(ExternalReference reference) {
195 Node** loc = cache_.FindExternalConstant(reference);
196 if (*loc == NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400197 *loc = graph()->NewNode(common()->ExternalConstant(reference));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 }
199 return *loc;
200}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400201
202
203void JSGraph::GetCachedNodes(NodeVector* nodes) {
204 cache_.GetCachedNodes(nodes);
205 SetOncePointer<Node>* ptrs[] = {
206 &c_entry_stub_constant_, &undefined_constant_, &the_hole_constant_,
207 &true_constant_, &false_constant_, &null_constant_,
208 &zero_constant_, &one_constant_, &nan_constant_};
209 for (size_t i = 0; i < arraysize(ptrs); i++) {
210 if (ptrs[i]->is_set()) nodes->push_back(ptrs[i]->get());
211 }
212}
213
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214} // namespace compiler
215} // namespace internal
216} // namespace v8