Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 5 | #include "src/code-stubs.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 14 | Node* JSGraph::ImmovableHeapConstant(Handle<HeapObject> object) { |
| 15 | Unique<HeapObject> unique = Unique<HeapObject>::CreateImmovable(object); |
| 16 | return graph()->NewNode(common()->HeapConstant(unique)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 20 | Node* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 28 | |
| 29 | return ImmovableHeapConstant(CEntryStub(isolate(), result_size).GetCode()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | |
| 33 | Node* 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 | |
| 42 | Node* 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 | |
| 50 | Node* 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 | |
| 58 | Node* 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 | |
| 66 | Node* 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 | |
| 74 | Node* JSGraph::ZeroConstant() { |
| 75 | if (!zero_constant_.is_set()) zero_constant_.set(NumberConstant(0.0)); |
| 76 | return zero_constant_.get(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | Node* JSGraph::OneConstant() { |
| 81 | if (!one_constant_.is_set()) one_constant_.set(NumberConstant(1.0)); |
| 82 | return one_constant_.get(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | Node* 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 94 | Node* JSGraph::HeapConstant(Unique<HeapObject> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | // TODO(turbofan): canonicalize heap constants using Unique<T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 96 | return graph()->NewNode(common()->HeapConstant(value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 100 | Node* JSGraph::HeapConstant(Handle<HeapObject> value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | // 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 108 | Unique<HeapObject> unique(reinterpret_cast<Address>(*value.location()), |
| 109 | value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 110 | return HeapConstant(unique); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | Node* 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 130 | return HeapConstant(Handle<HeapObject>::cast(value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | Node* 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 | |
| 142 | Node* JSGraph::Constant(int32_t value) { |
| 143 | if (value == 0) return ZeroConstant(); |
| 144 | if (value == 1) return OneConstant(); |
| 145 | return NumberConstant(value); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | Node* JSGraph::Int32Constant(int32_t value) { |
| 150 | Node** loc = cache_.FindInt32Constant(value); |
| 151 | if (*loc == NULL) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 152 | *loc = graph()->NewNode(common()->Int32Constant(value)); |
| 153 | } |
| 154 | return *loc; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | Node* JSGraph::Int64Constant(int64_t value) { |
| 159 | Node** loc = cache_.FindInt64Constant(value); |
| 160 | if (*loc == NULL) { |
| 161 | *loc = graph()->NewNode(common()->Int64Constant(value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 162 | } |
| 163 | return *loc; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | Node* JSGraph::NumberConstant(double value) { |
| 168 | Node** loc = cache_.FindNumberConstant(value); |
| 169 | if (*loc == NULL) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 170 | *loc = graph()->NewNode(common()->NumberConstant(value)); |
| 171 | } |
| 172 | return *loc; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | Node* JSGraph::Float32Constant(float value) { |
| 177 | Node** loc = cache_.FindFloat32Constant(value); |
| 178 | if (*loc == NULL) { |
| 179 | *loc = graph()->NewNode(common()->Float32Constant(value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 180 | } |
| 181 | return *loc; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | Node* JSGraph::Float64Constant(double value) { |
| 186 | Node** loc = cache_.FindFloat64Constant(value); |
| 187 | if (*loc == NULL) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 188 | *loc = graph()->NewNode(common()->Float64Constant(value)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 189 | } |
| 190 | return *loc; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | Node* JSGraph::ExternalConstant(ExternalReference reference) { |
| 195 | Node** loc = cache_.FindExternalConstant(reference); |
| 196 | if (*loc == NULL) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 197 | *loc = graph()->NewNode(common()->ExternalConstant(reference)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | } |
| 199 | return *loc; |
| 200 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 201 | |
| 202 | |
| 203 | void 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | } // namespace compiler |
| 215 | } // namespace internal |
| 216 | } // namespace v8 |