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/compiler/js-graph.h"
6 #include "src/compiler/node-properties-inl.h"
7 #include "src/compiler/typer.h"
13 Node* JSGraph::ImmovableHeapConstant(Handle<Object> object) {
14 PrintableUnique<Object> unique =
15 PrintableUnique<Object>::CreateImmovable(zone(), object);
16 return NewNode(common()->HeapConstant(unique));
20 Node* JSGraph::NewNode(Operator* op) {
21 Node* node = graph()->NewNode(op);
27 Node* JSGraph::UndefinedConstant() {
28 if (!undefined_constant_.is_set()) {
29 undefined_constant_.set(
30 ImmovableHeapConstant(factory()->undefined_value()));
32 return undefined_constant_.get();
36 Node* JSGraph::TheHoleConstant() {
37 if (!the_hole_constant_.is_set()) {
38 the_hole_constant_.set(ImmovableHeapConstant(factory()->the_hole_value()));
40 return the_hole_constant_.get();
44 Node* JSGraph::TrueConstant() {
45 if (!true_constant_.is_set()) {
46 true_constant_.set(ImmovableHeapConstant(factory()->true_value()));
48 return true_constant_.get();
52 Node* JSGraph::FalseConstant() {
53 if (!false_constant_.is_set()) {
54 false_constant_.set(ImmovableHeapConstant(factory()->false_value()));
56 return false_constant_.get();
60 Node* JSGraph::NullConstant() {
61 if (!null_constant_.is_set()) {
62 null_constant_.set(ImmovableHeapConstant(factory()->null_value()));
64 return null_constant_.get();
68 Node* JSGraph::ZeroConstant() {
69 if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0));
70 return zero_constant_.get();
74 Node* JSGraph::OneConstant() {
75 if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0));
76 return one_constant_.get();
80 Node* JSGraph::NaNConstant() {
81 if (!nan_constant_.is_set()) {
82 nan_constant_.set(NumberConstant(base::OS::nan_value()));
84 return nan_constant_.get();
88 Node* JSGraph::HeapConstant(PrintableUnique<Object> value) {
89 // TODO(turbofan): canonicalize heap constants using Unique<T>
90 return NewNode(common()->HeapConstant(value));
94 Node* JSGraph::HeapConstant(Handle<Object> value) {
95 // TODO(titzer): We could also match against the addresses of immortable
96 // immovables here, even without access to the heap, thus always
97 // canonicalizing references to them.
99 PrintableUnique<Object>::CreateUninitialized(zone(), value));
103 Node* JSGraph::Constant(Handle<Object> value) {
104 // Dereference the handle to determine if a number constant or other
105 // canonicalized node can be used.
106 if (value->IsNumber()) {
107 return Constant(value->Number());
108 } else if (value->IsUndefined()) {
109 return UndefinedConstant();
110 } else if (value->IsTrue()) {
111 return TrueConstant();
112 } else if (value->IsFalse()) {
113 return FalseConstant();
114 } else if (value->IsNull()) {
115 return NullConstant();
116 } else if (value->IsTheHole()) {
117 return TheHoleConstant();
119 return HeapConstant(value);
124 Node* JSGraph::Constant(double value) {
125 if (BitCast<int64_t>(value) == BitCast<int64_t>(0.0)) return ZeroConstant();
126 if (BitCast<int64_t>(value) == BitCast<int64_t>(1.0)) return OneConstant();
127 return NumberConstant(value);
131 Node* JSGraph::Constant(int32_t value) {
132 if (value == 0) return ZeroConstant();
133 if (value == 1) return OneConstant();
134 return NumberConstant(value);
138 Node* JSGraph::Int32Constant(int32_t value) {
139 Node** loc = cache_.FindInt32Constant(value);
141 *loc = NewNode(common()->Int32Constant(value));
147 Node* JSGraph::NumberConstant(double value) {
148 Node** loc = cache_.FindNumberConstant(value);
150 *loc = NewNode(common()->NumberConstant(value));
156 Node* JSGraph::Float64Constant(double value) {
157 Node** loc = cache_.FindFloat64Constant(value);
159 *loc = NewNode(common()->Float64Constant(value));
165 Node* JSGraph::ExternalConstant(ExternalReference reference) {
166 Node** loc = cache_.FindExternalConstant(reference);
168 *loc = NewNode(common()->ExternalConstant(reference));
172 } // namespace compiler
173 } // namespace internal