blob: 3f20daa3d174540aed830a7597ba2fde251e8ff3 [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"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/compiler/node-properties.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/compiler/typer.h"
9
10namespace v8 {
11namespace internal {
12namespace compiler {
13
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#define CACHED(name, expr) \
15 cached_nodes_[name] ? cached_nodes_[name] : (cached_nodes_[name] = (expr))
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016
Ben Murdochc5610432016-08-08 18:44:38 +010017Node* JSGraph::AllocateInNewSpaceStubConstant() {
18 return CACHED(kAllocateInNewSpaceStubConstant,
19 HeapConstant(isolate()->builtins()->AllocateInNewSpace()));
20}
21
22Node* JSGraph::AllocateInOldSpaceStubConstant() {
23 return CACHED(kAllocateInOldSpaceStubConstant,
24 HeapConstant(isolate()->builtins()->AllocateInOldSpace()));
25}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026
Ben Murdoch61f157c2016-09-16 13:49:30 +010027Node* JSGraph::ToNumberBuiltinConstant() {
28 return CACHED(kToNumberBuiltinConstant,
29 HeapConstant(isolate()->builtins()->ToNumber()));
30}
31
Emily Bernierd0a1eb72015-03-24 16:35:39 -040032Node* JSGraph::CEntryStubConstant(int result_size) {
33 if (result_size == 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 return CACHED(kCEntryStubConstant,
35 HeapConstant(CEntryStub(isolate(), 1).GetCode()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 return HeapConstant(CEntryStub(isolate(), result_size).GetCode());
38}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040039
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
41Node* JSGraph::EmptyFixedArrayConstant() {
42 return CACHED(kEmptyFixedArrayConstant,
43 HeapConstant(factory()->empty_fixed_array()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044}
45
Ben Murdoch61f157c2016-09-16 13:49:30 +010046Node* JSGraph::EmptyLiteralsArrayConstant() {
47 return CACHED(kEmptyLiteralsArrayConstant,
48 HeapConstant(factory()->empty_literals_array()));
49}
50
Ben Murdochc5610432016-08-08 18:44:38 +010051Node* JSGraph::HeapNumberMapConstant() {
52 return CACHED(kHeapNumberMapConstant,
53 HeapConstant(factory()->heap_number_map()));
54}
55
Ben Murdochda12d292016-06-02 14:46:10 +010056Node* JSGraph::OptimizedOutConstant() {
57 return CACHED(kOptimizedOutConstant,
58 HeapConstant(factory()->optimized_out()));
59}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060
Ben Murdochc5610432016-08-08 18:44:38 +010061Node* JSGraph::StaleRegisterConstant() {
62 return CACHED(kStaleRegisterConstant,
63 HeapConstant(factory()->stale_register()));
64}
65
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066Node* JSGraph::UndefinedConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 return CACHED(kUndefinedConstant, HeapConstant(factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068}
69
70
71Node* JSGraph::TheHoleConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 return CACHED(kTheHoleConstant, HeapConstant(factory()->the_hole_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073}
74
75
76Node* JSGraph::TrueConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 return CACHED(kTrueConstant, HeapConstant(factory()->true_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078}
79
80
81Node* JSGraph::FalseConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 return CACHED(kFalseConstant, HeapConstant(factory()->false_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083}
84
85
86Node* JSGraph::NullConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 return CACHED(kNullConstant, HeapConstant(factory()->null_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088}
89
90
91Node* JSGraph::ZeroConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 return CACHED(kZeroConstant, NumberConstant(0.0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093}
94
95
96Node* JSGraph::OneConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 return CACHED(kOneConstant, NumberConstant(1.0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098}
99
100
101Node* JSGraph::NaNConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 return CACHED(kNaNConstant,
103 NumberConstant(std::numeric_limits<double>::quiet_NaN()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104}
105
106
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107Node* JSGraph::HeapConstant(Handle<HeapObject> value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 Node** loc = cache_.FindHeapConstant(value);
109 if (*loc == nullptr) {
110 *loc = graph()->NewNode(common()->HeapConstant(value));
111 }
112 return *loc;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113}
114
115
116Node* JSGraph::Constant(Handle<Object> value) {
117 // Dereference the handle to determine if a number constant or other
118 // canonicalized node can be used.
119 if (value->IsNumber()) {
120 return Constant(value->Number());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100121 } else if (value->IsUndefined(isolate())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 return UndefinedConstant();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100123 } else if (value->IsTrue(isolate())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 return TrueConstant();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100125 } else if (value->IsFalse(isolate())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 return FalseConstant();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100127 } else if (value->IsNull(isolate())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 return NullConstant();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100129 } else if (value->IsTheHole(isolate())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 return TheHoleConstant();
131 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400132 return HeapConstant(Handle<HeapObject>::cast(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 }
134}
135
136
137Node* JSGraph::Constant(double value) {
138 if (bit_cast<int64_t>(value) == bit_cast<int64_t>(0.0)) return ZeroConstant();
139 if (bit_cast<int64_t>(value) == bit_cast<int64_t>(1.0)) return OneConstant();
140 return NumberConstant(value);
141}
142
143
144Node* JSGraph::Constant(int32_t value) {
145 if (value == 0) return ZeroConstant();
146 if (value == 1) return OneConstant();
147 return NumberConstant(value);
148}
149
150
151Node* JSGraph::Int32Constant(int32_t value) {
152 Node** loc = cache_.FindInt32Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000153 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400154 *loc = graph()->NewNode(common()->Int32Constant(value));
155 }
156 return *loc;
157}
158
159
160Node* JSGraph::Int64Constant(int64_t value) {
161 Node** loc = cache_.FindInt64Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400163 *loc = graph()->NewNode(common()->Int64Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 }
165 return *loc;
166}
167
Ben Murdochc5610432016-08-08 18:44:38 +0100168Node* JSGraph::RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100169 Node** loc = cache_.FindRelocatableInt32Constant(
170 value, static_cast<RelocInfoMode>(rmode));
Ben Murdochc5610432016-08-08 18:44:38 +0100171 if (*loc == nullptr) {
172 *loc = graph()->NewNode(common()->RelocatableInt32Constant(value, rmode));
173 }
174 return *loc;
175}
176
177Node* JSGraph::RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100178 Node** loc = cache_.FindRelocatableInt64Constant(
179 value, static_cast<RelocInfoMode>(rmode));
Ben Murdochc5610432016-08-08 18:44:38 +0100180 if (*loc == nullptr) {
181 *loc = graph()->NewNode(common()->RelocatableInt64Constant(value, rmode));
182 }
183 return *loc;
184}
185
186Node* JSGraph::RelocatableIntPtrConstant(intptr_t value,
187 RelocInfo::Mode rmode) {
188 return kPointerSize == 8
189 ? RelocatableInt64Constant(value, rmode)
190 : RelocatableInt32Constant(static_cast<int>(value), rmode);
191}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192
193Node* JSGraph::NumberConstant(double value) {
194 Node** loc = cache_.FindNumberConstant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400196 *loc = graph()->NewNode(common()->NumberConstant(value));
197 }
198 return *loc;
199}
200
201
202Node* JSGraph::Float32Constant(float value) {
203 Node** loc = cache_.FindFloat32Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400205 *loc = graph()->NewNode(common()->Float32Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 }
207 return *loc;
208}
209
210
211Node* JSGraph::Float64Constant(double value) {
212 Node** loc = cache_.FindFloat64Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400214 *loc = graph()->NewNode(common()->Float64Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 }
216 return *loc;
217}
218
219
220Node* JSGraph::ExternalConstant(ExternalReference reference) {
221 Node** loc = cache_.FindExternalConstant(reference);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000222 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400223 *loc = graph()->NewNode(common()->ExternalConstant(reference));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 }
225 return *loc;
226}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400227
228
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000229Node* JSGraph::ExternalConstant(Runtime::FunctionId function_id) {
230 return ExternalConstant(ExternalReference(function_id, isolate()));
231}
232
Ben Murdoch61f157c2016-09-16 13:49:30 +0100233Node* JSGraph::EmptyStateValues() {
234 return CACHED(kEmptyStateValues, graph()->NewNode(common()->StateValues(0)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235}
236
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000237Node* JSGraph::Dead() {
238 return CACHED(kDead, graph()->NewNode(common()->Dead()));
239}
240
241
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400242void JSGraph::GetCachedNodes(NodeVector* nodes) {
243 cache_.GetCachedNodes(nodes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 for (size_t i = 0; i < arraysize(cached_nodes_); i++) {
245 if (Node* node = cached_nodes_[i]) {
246 if (!node->IsDead()) nodes->push_back(node);
247 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400248 }
249}
250
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251} // namespace compiler
252} // namespace internal
253} // namespace v8