js-graph.cc 9.3 KB
Newer Older
1 2 3 4 5
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/js-graph.h"
6 7

#include "src/code-stubs.h"
8
#include "src/compiler/node-properties.h"
9
#include "src/compiler/typer.h"
10
#include "src/objects-inl.h"
11 12 13 14 15

namespace v8 {
namespace internal {
namespace compiler {

16 17 18
#define CACHED(name, expr) \
  cached_nodes_[name] ? cached_nodes_[name] : (cached_nodes_[name] = (expr))

19 20
Node* JSGraph::AllocateInNewSpaceStubConstant() {
  return CACHED(kAllocateInNewSpaceStubConstant,
21
                HeapConstant(BUILTIN_CODE(isolate(), AllocateInNewSpace)));
22 23 24 25
}

Node* JSGraph::AllocateInOldSpaceStubConstant() {
  return CACHED(kAllocateInOldSpaceStubConstant,
26
                HeapConstant(BUILTIN_CODE(isolate(), AllocateInOldSpace)));
27
}
28

29 30 31 32 33
Node* JSGraph::ArrayConstructorStubConstant() {
  return CACHED(kArrayConstructorStubConstant,
                HeapConstant(ArrayConstructorStub(isolate()).GetCode()));
}

34 35
Node* JSGraph::ToNumberBuiltinConstant() {
  return CACHED(kToNumberBuiltinConstant,
36
                HeapConstant(BUILTIN_CODE(isolate(), ToNumber)));
37 38
}

39 40
Node* JSGraph::CEntryStubConstant(int result_size, SaveFPRegsMode save_doubles,
                                  ArgvMode argv_mode, bool builtin_exit_frame) {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  if (save_doubles == kDontSaveFPRegs && argv_mode == kArgvOnStack) {
    DCHECK(result_size >= 1 && result_size <= 3);
    if (!builtin_exit_frame) {
      CachedNode key;
      if (result_size == 1) {
        key = kCEntryStub1Constant;
      } else if (result_size == 2) {
        key = kCEntryStub2Constant;
      } else {
        DCHECK(result_size == 3);
        key = kCEntryStub3Constant;
      }
      return CACHED(
          key, HeapConstant(CEntryStub(isolate(), result_size, save_doubles,
                                       argv_mode, builtin_exit_frame)
                                .GetCode()));
    }
58
    CachedNode key = builtin_exit_frame
59 60
                         ? kCEntryStub1WithBuiltinExitFrameConstant
                         : kCEntryStub1Constant;
61 62 63 64
    return CACHED(key,
                  HeapConstant(CEntryStub(isolate(), result_size, save_doubles,
                                          argv_mode, builtin_exit_frame)
                                   .GetCode()));
65
  }
66 67 68
  CEntryStub stub(isolate(), result_size, save_doubles, argv_mode,
                  builtin_exit_frame);
  return HeapConstant(stub.GetCode());
69 70
}

71 72
Node* JSGraph::EmptyFixedArrayConstant() {
  return CACHED(kEmptyFixedArrayConstant,
73
                HeapConstant(factory()->empty_fixed_array()));
74 75
}

76 77 78 79
Node* JSGraph::EmptyStringConstant() {
  return CACHED(kEmptyStringConstant, HeapConstant(factory()->empty_string()));
}

80 81 82 83 84
Node* JSGraph::FixedArrayMapConstant() {
  return CACHED(kFixedArrayMapConstant,
                HeapConstant(factory()->fixed_array_map()));
}

85 86 87 88 89
Node* JSGraph::PropertyArrayMapConstant() {
  return CACHED(kPropertyArrayMapConstant,
                HeapConstant(factory()->property_array_map()));
}

90 91 92 93 94
Node* JSGraph::FixedDoubleArrayMapConstant() {
  return CACHED(kFixedDoubleArrayMapConstant,
                HeapConstant(factory()->fixed_double_array_map()));
}

95 96 97 98 99
Node* JSGraph::HeapNumberMapConstant() {
  return CACHED(kHeapNumberMapConstant,
                HeapConstant(factory()->heap_number_map()));
}

100 101 102 103
Node* JSGraph::OptimizedOutConstant() {
  return CACHED(kOptimizedOutConstant,
                HeapConstant(factory()->optimized_out()));
}
104

105 106 107 108 109
Node* JSGraph::StaleRegisterConstant() {
  return CACHED(kStaleRegisterConstant,
                HeapConstant(factory()->stale_register()));
}

110
Node* JSGraph::UndefinedConstant() {
111
  return CACHED(kUndefinedConstant, HeapConstant(factory()->undefined_value()));
112 113 114 115
}


Node* JSGraph::TheHoleConstant() {
116
  return CACHED(kTheHoleConstant, HeapConstant(factory()->the_hole_value()));
117 118 119 120
}


Node* JSGraph::TrueConstant() {
121
  return CACHED(kTrueConstant, HeapConstant(factory()->true_value()));
122 123 124 125
}


Node* JSGraph::FalseConstant() {
126
  return CACHED(kFalseConstant, HeapConstant(factory()->false_value()));
127 128 129 130
}


Node* JSGraph::NullConstant() {
131
  return CACHED(kNullConstant, HeapConstant(factory()->null_value()));
132 133 134 135
}


Node* JSGraph::ZeroConstant() {
136
  return CACHED(kZeroConstant, NumberConstant(0.0));
137 138 139
}

Node* JSGraph::OneConstant() {
140
  return CACHED(kOneConstant, NumberConstant(1.0));
141 142
}

143 144 145
Node* JSGraph::MinusOneConstant() {
  return CACHED(kMinusOneConstant, NumberConstant(-1.0));
}
146 147

Node* JSGraph::NaNConstant() {
148 149
  return CACHED(kNaNConstant,
                NumberConstant(std::numeric_limits<double>::quiet_NaN()));
150 151 152
}


153
Node* JSGraph::HeapConstant(Handle<HeapObject> value) {
154 155 156 157 158
  Node** loc = cache_.FindHeapConstant(value);
  if (*loc == nullptr) {
    *loc = graph()->NewNode(common()->HeapConstant(value));
  }
  return *loc;
159 160 161 162 163 164 165 166
}


Node* JSGraph::Constant(Handle<Object> value) {
  // Dereference the handle to determine if a number constant or other
  // canonicalized node can be used.
  if (value->IsNumber()) {
    return Constant(value->Number());
167
  } else if (value->IsUndefined(isolate())) {
168
    return UndefinedConstant();
169
  } else if (value->IsTrue(isolate())) {
170
    return TrueConstant();
171
  } else if (value->IsFalse(isolate())) {
172
    return FalseConstant();
173
  } else if (value->IsNull(isolate())) {
174
    return NullConstant();
175
  } else if (value->IsTheHole(isolate())) {
176 177
    return TheHoleConstant();
  } else {
178
    return HeapConstant(Handle<HeapObject>::cast(value));
179 180 181 182 183
  }
}


Node* JSGraph::Constant(double value) {
184 185
  if (bit_cast<int64_t>(value) == bit_cast<int64_t>(0.0)) return ZeroConstant();
  if (bit_cast<int64_t>(value) == bit_cast<int64_t>(1.0)) return OneConstant();
186 187 188 189 190 191 192 193 194 195
  return NumberConstant(value);
}


Node* JSGraph::Constant(int32_t value) {
  if (value == 0) return ZeroConstant();
  if (value == 1) return OneConstant();
  return NumberConstant(value);
}

196 197 198 199 200
Node* JSGraph::Constant(uint32_t value) {
  if (value == 0) return ZeroConstant();
  if (value == 1) return OneConstant();
  return NumberConstant(value);
}
201 202 203

Node* JSGraph::Int32Constant(int32_t value) {
  Node** loc = cache_.FindInt32Constant(value);
204
  if (*loc == nullptr) {
205
    *loc = graph()->NewNode(common()->Int32Constant(value));
206 207 208 209 210
  }
  return *loc;
}


211 212
Node* JSGraph::Int64Constant(int64_t value) {
  Node** loc = cache_.FindInt64Constant(value);
213
  if (*loc == nullptr) {
214
    *loc = graph()->NewNode(common()->Int64Constant(value));
215 216 217 218
  }
  return *loc;
}

219
Node* JSGraph::RelocatableInt32Constant(int32_t value, RelocInfo::Mode rmode) {
220 221
  Node** loc = cache_.FindRelocatableInt32Constant(
      value, static_cast<RelocInfoMode>(rmode));
222 223 224 225 226 227 228
  if (*loc == nullptr) {
    *loc = graph()->NewNode(common()->RelocatableInt32Constant(value, rmode));
  }
  return *loc;
}

Node* JSGraph::RelocatableInt64Constant(int64_t value, RelocInfo::Mode rmode) {
229 230
  Node** loc = cache_.FindRelocatableInt64Constant(
      value, static_cast<RelocInfoMode>(rmode));
231 232 233 234 235 236 237 238 239 240 241 242
  if (*loc == nullptr) {
    *loc = graph()->NewNode(common()->RelocatableInt64Constant(value, rmode));
  }
  return *loc;
}

Node* JSGraph::RelocatableIntPtrConstant(intptr_t value,
                                         RelocInfo::Mode rmode) {
  return kPointerSize == 8
             ? RelocatableInt64Constant(value, rmode)
             : RelocatableInt32Constant(static_cast<int>(value), rmode);
}
243

244 245
Node* JSGraph::NumberConstant(double value) {
  Node** loc = cache_.FindNumberConstant(value);
246
  if (*loc == nullptr) {
247
    *loc = graph()->NewNode(common()->NumberConstant(value));
248 249 250 251 252
  }
  return *loc;
}


253
Node* JSGraph::Float32Constant(float value) {
254
  Node** loc = cache_.FindFloat32Constant(value);
255
  if (*loc == nullptr) {
256 257 258
    *loc = graph()->NewNode(common()->Float32Constant(value));
  }
  return *loc;
259 260 261
}


262 263
Node* JSGraph::Float64Constant(double value) {
  Node** loc = cache_.FindFloat64Constant(value);
264
  if (*loc == nullptr) {
265
    *loc = graph()->NewNode(common()->Float64Constant(value));
266 267 268 269
  }
  return *loc;
}

270 271 272 273 274 275 276
Node* JSGraph::PointerConstant(intptr_t value) {
  Node** loc = cache_.FindPointerConstant(value);
  if (*loc == nullptr) {
    *loc = graph()->NewNode(common()->PointerConstant(value));
  }
  return *loc;
}
277

278 279
Node* JSGraph::ExternalConstant(ExternalReference reference) {
  Node** loc = cache_.FindExternalConstant(reference);
280
  if (*loc == nullptr) {
281 282 283 284 285 286
    *loc = graph()->NewNode(common()->ExternalConstant(reference));
  }
  return *loc;
}


287 288 289 290
Node* JSGraph::ExternalConstant(Runtime::FunctionId function_id) {
  return ExternalConstant(ExternalReference(function_id, isolate()));
}

291
Node* JSGraph::EmptyStateValues() {
292 293
  return CACHED(kEmptyStateValues, graph()->NewNode(common()->StateValues(
                                       0, SparseInputMask::Dense())));
294
}
295

296 297 298 299 300 301 302 303
Node* JSGraph::SingleDeadTypedStateValues() {
  return CACHED(kSingleDeadTypedStateValues,
                graph()->NewNode(common()->TypedStateValues(
                    new (graph()->zone()->New(sizeof(ZoneVector<MachineType>)))
                        ZoneVector<MachineType>(0, graph()->zone()),
                    SparseInputMask(SparseInputMask::kEndMarker << 1))));
}

304 305
Node* JSGraph::Dead() {
  return CACHED(kDead, graph()->NewNode(common()->Dead()));
306
}
307

308 309 310

void JSGraph::GetCachedNodes(NodeVector* nodes) {
  cache_.GetCachedNodes(nodes);
311
  for (size_t i = 0; i < arraysize(cached_nodes_); i++) {
312 313 314
    if (Node* node = cached_nodes_[i]) {
      if (!node->IsDead()) nodes->push_back(node);
    }
315 316 317
  }
}

318 319 320
}  // namespace compiler
}  // namespace internal
}  // namespace v8