blob: e9387982878abc57434d67158c3b2dfa92fc717d [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
17
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018Node* JSGraph::CEntryStubConstant(int result_size) {
19 if (result_size == 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020 return CACHED(kCEntryStubConstant,
21 HeapConstant(CEntryStub(isolate(), 1).GetCode()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023 return HeapConstant(CEntryStub(isolate(), result_size).GetCode());
24}
Emily Bernierd0a1eb72015-03-24 16:35:39 -040025
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026
27Node* JSGraph::EmptyFixedArrayConstant() {
28 return CACHED(kEmptyFixedArrayConstant,
29 HeapConstant(factory()->empty_fixed_array()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030}
31
32
33Node* JSGraph::UndefinedConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 return CACHED(kUndefinedConstant, HeapConstant(factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035}
36
37
38Node* JSGraph::TheHoleConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039 return CACHED(kTheHoleConstant, HeapConstant(factory()->the_hole_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040}
41
42
43Node* JSGraph::TrueConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 return CACHED(kTrueConstant, HeapConstant(factory()->true_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045}
46
47
48Node* JSGraph::FalseConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 return CACHED(kFalseConstant, HeapConstant(factory()->false_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050}
51
52
53Node* JSGraph::NullConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 return CACHED(kNullConstant, HeapConstant(factory()->null_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055}
56
57
58Node* JSGraph::ZeroConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059 return CACHED(kZeroConstant, NumberConstant(0.0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060}
61
62
63Node* JSGraph::OneConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 return CACHED(kOneConstant, NumberConstant(1.0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065}
66
67
68Node* JSGraph::NaNConstant() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 return CACHED(kNaNConstant,
70 NumberConstant(std::numeric_limits<double>::quiet_NaN()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071}
72
73
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074Node* JSGraph::HeapConstant(Handle<HeapObject> value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 if (value->IsConsString()) {
76 value = String::Flatten(Handle<String>::cast(value), TENURED);
77 }
78 Node** loc = cache_.FindHeapConstant(value);
79 if (*loc == nullptr) {
80 *loc = graph()->NewNode(common()->HeapConstant(value));
81 }
82 return *loc;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083}
84
85
86Node* JSGraph::Constant(Handle<Object> value) {
87 // Dereference the handle to determine if a number constant or other
88 // canonicalized node can be used.
89 if (value->IsNumber()) {
90 return Constant(value->Number());
91 } else if (value->IsUndefined()) {
92 return UndefinedConstant();
93 } else if (value->IsTrue()) {
94 return TrueConstant();
95 } else if (value->IsFalse()) {
96 return FalseConstant();
97 } else if (value->IsNull()) {
98 return NullConstant();
99 } else if (value->IsTheHole()) {
100 return TheHoleConstant();
101 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400102 return HeapConstant(Handle<HeapObject>::cast(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 }
104}
105
106
107Node* JSGraph::Constant(double value) {
108 if (bit_cast<int64_t>(value) == bit_cast<int64_t>(0.0)) return ZeroConstant();
109 if (bit_cast<int64_t>(value) == bit_cast<int64_t>(1.0)) return OneConstant();
110 return NumberConstant(value);
111}
112
113
114Node* JSGraph::Constant(int32_t value) {
115 if (value == 0) return ZeroConstant();
116 if (value == 1) return OneConstant();
117 return NumberConstant(value);
118}
119
120
121Node* JSGraph::Int32Constant(int32_t value) {
122 Node** loc = cache_.FindInt32Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400124 *loc = graph()->NewNode(common()->Int32Constant(value));
125 }
126 return *loc;
127}
128
129
130Node* JSGraph::Int64Constant(int64_t value) {
131 Node** loc = cache_.FindInt64Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400133 *loc = graph()->NewNode(common()->Int64Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 }
135 return *loc;
136}
137
138
139Node* JSGraph::NumberConstant(double value) {
140 Node** loc = cache_.FindNumberConstant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400142 *loc = graph()->NewNode(common()->NumberConstant(value));
143 }
144 return *loc;
145}
146
147
148Node* JSGraph::Float32Constant(float value) {
149 Node** loc = cache_.FindFloat32Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400151 *loc = graph()->NewNode(common()->Float32Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 }
153 return *loc;
154}
155
156
157Node* JSGraph::Float64Constant(double value) {
158 Node** loc = cache_.FindFloat64Constant(value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400160 *loc = graph()->NewNode(common()->Float64Constant(value));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 }
162 return *loc;
163}
164
165
166Node* JSGraph::ExternalConstant(ExternalReference reference) {
167 Node** loc = cache_.FindExternalConstant(reference);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000168 if (*loc == nullptr) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400169 *loc = graph()->NewNode(common()->ExternalConstant(reference));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 }
171 return *loc;
172}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400173
174
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175Node* JSGraph::ExternalConstant(Runtime::FunctionId function_id) {
176 return ExternalConstant(ExternalReference(function_id, isolate()));
177}
178
179
180Node* JSGraph::EmptyFrameState() {
181 Node* empty_frame_state = cached_nodes_[kEmptyFrameState];
182 if (!empty_frame_state || empty_frame_state->IsDead()) {
183 Node* state_values = graph()->NewNode(common()->StateValues(0));
184 empty_frame_state = graph()->NewNode(
185 common()->FrameState(BailoutId::None(),
186 OutputFrameStateCombine::Ignore(), nullptr),
187 state_values, state_values, state_values, NoContextConstant(),
188 UndefinedConstant(), graph()->start());
189 cached_nodes_[kEmptyFrameState] = empty_frame_state;
190 }
191 return empty_frame_state;
192}
193
194
195Node* JSGraph::Dead() {
196 return CACHED(kDead, graph()->NewNode(common()->Dead()));
197}
198
199
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400200void JSGraph::GetCachedNodes(NodeVector* nodes) {
201 cache_.GetCachedNodes(nodes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000202 for (size_t i = 0; i < arraysize(cached_nodes_); i++) {
203 if (Node* node = cached_nodes_[i]) {
204 if (!node->IsDead()) nodes->push_back(node);
205 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400206 }
207}
208
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209} // namespace compiler
210} // namespace internal
211} // namespace v8