Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / compiler / js-graph.cc
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.
4
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"
9
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13
14 Node* JSGraph::ImmovableHeapConstant(Handle<HeapObject> object) {
15   Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(object);
16   return graph()->NewNode(common()->HeapConstant(unique));
17 }
18
19
20 Node* JSGraph::CEntryStubConstant() {
21   if (!c_entry_stub_constant_.is_set()) {
22     c_entry_stub_constant_.set(
23         ImmovableHeapConstant(CEntryStub(isolate(), 1).GetCode()));
24   }
25   return c_entry_stub_constant_.get();
26 }
27
28
29 Node* JSGraph::UndefinedConstant() {
30   if (!undefined_constant_.is_set()) {
31     undefined_constant_.set(
32         ImmovableHeapConstant(factory()->undefined_value()));
33   }
34   return undefined_constant_.get();
35 }
36
37
38 Node* JSGraph::TheHoleConstant() {
39   if (!the_hole_constant_.is_set()) {
40     the_hole_constant_.set(ImmovableHeapConstant(factory()->the_hole_value()));
41   }
42   return the_hole_constant_.get();
43 }
44
45
46 Node* JSGraph::TrueConstant() {
47   if (!true_constant_.is_set()) {
48     true_constant_.set(ImmovableHeapConstant(factory()->true_value()));
49   }
50   return true_constant_.get();
51 }
52
53
54 Node* JSGraph::FalseConstant() {
55   if (!false_constant_.is_set()) {
56     false_constant_.set(ImmovableHeapConstant(factory()->false_value()));
57   }
58   return false_constant_.get();
59 }
60
61
62 Node* JSGraph::NullConstant() {
63   if (!null_constant_.is_set()) {
64     null_constant_.set(ImmovableHeapConstant(factory()->null_value()));
65   }
66   return null_constant_.get();
67 }
68
69
70 Node* JSGraph::ZeroConstant() {
71   if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0));
72   return zero_constant_.get();
73 }
74
75
76 Node* JSGraph::OneConstant() {
77   if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0));
78   return one_constant_.get();
79 }
80
81
82 Node* JSGraph::NaNConstant() {
83   if (!nan_constant_.is_set()) {
84     nan_constant_.set(NumberConstant(base::OS::nan_value()));
85   }
86   return nan_constant_.get();
87 }
88
89
90 Node* JSGraph::HeapConstant(Unique<HeapObject> value) {
91   // TODO(turbofan): canonicalize heap constants using Unique<T>
92   return graph()->NewNode(common()->HeapConstant(value));
93 }
94
95
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()),
105                             value);
106   return HeapConstant(unique);
107 }
108
109
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();
125   } else {
126     return HeapConstant(Handle<HeapObject>::cast(value));
127   }
128 }
129
130
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);
135 }
136
137
138 Node* JSGraph::Constant(int32_t value) {
139   if (value == 0) return ZeroConstant();
140   if (value == 1) return OneConstant();
141   return NumberConstant(value);
142 }
143
144
145 Node* JSGraph::Int32Constant(int32_t value) {
146   Node** loc = cache_.FindInt32Constant(value);
147   if (*loc == NULL) {
148     *loc = graph()->NewNode(common()->Int32Constant(value));
149   }
150   return *loc;
151 }
152
153
154 Node* JSGraph::Int64Constant(int64_t value) {
155   Node** loc = cache_.FindInt64Constant(value);
156   if (*loc == NULL) {
157     *loc = graph()->NewNode(common()->Int64Constant(value));
158   }
159   return *loc;
160 }
161
162
163 Node* JSGraph::NumberConstant(double value) {
164   Node** loc = cache_.FindNumberConstant(value);
165   if (*loc == NULL) {
166     *loc = graph()->NewNode(common()->NumberConstant(value));
167   }
168   return *loc;
169 }
170
171
172 Node* JSGraph::Float32Constant(float value) {
173   // TODO(turbofan): cache float32 constants.
174   return graph()->NewNode(common()->Float32Constant(value));
175 }
176
177
178 Node* JSGraph::Float64Constant(double value) {
179   Node** loc = cache_.FindFloat64Constant(value);
180   if (*loc == NULL) {
181     *loc = graph()->NewNode(common()->Float64Constant(value));
182   }
183   return *loc;
184 }
185
186
187 Node* JSGraph::ExternalConstant(ExternalReference reference) {
188   Node** loc = cache_.FindExternalConstant(reference);
189   if (*loc == NULL) {
190     *loc = graph()->NewNode(common()->ExternalConstant(reference));
191   }
192   return *loc;
193 }
194
195
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());
204   }
205 }
206
207 }  // namespace compiler
208 }  // namespace internal
209 }  // namespace v8