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

5
#include "src/code-stubs.h"
6

7
#include "src/bailout-reason.h"
8 9
#include "src/crankshaft/hydrogen.h"
#include "src/crankshaft/lithium.h"
10
#include "src/field-index.h"
11
#include "src/ic/ic.h"
12 13 14 15 16

namespace v8 {
namespace internal {


17
static LChunk* OptimizeGraph(HGraph* graph) {
18 19 20
  DisallowHeapAllocation no_allocation;
  DisallowHandleAllocation no_handles;
  DisallowHandleDereference no_deref;
21

22
  DCHECK(graph != NULL);
23
  BailoutReason bailout_reason = kNoReason;
24
  if (!graph->Optimize(&bailout_reason)) {
25
    FATAL(GetBailoutReason(bailout_reason));
26
  }
27
  LChunk* chunk = LChunk::NewChunk(graph);
28
  if (chunk == NULL) {
29
    FATAL(GetBailoutReason(graph->info()->bailout_reason()));
30 31
  }
  return chunk;
32 33 34 35 36
}


class CodeStubGraphBuilderBase : public HGraphBuilder {
 public:
37 38
  explicit CodeStubGraphBuilderBase(CompilationInfo* info, CodeStub* code_stub)
      : HGraphBuilder(info, code_stub->GetCallInterfaceDescriptor()),
39
        arguments_length_(NULL),
40
        info_(info),
41 42
        code_stub_(code_stub),
        descriptor_(code_stub),
43
        context_(NULL) {
44
    int parameter_count = GetParameterCount();
45
    parameters_.Reset(new HParameter*[parameter_count]);
46
  }
47 48 49
  virtual bool BuildGraph();

 protected:
50
  virtual HValue* BuildCodeStub() = 0;
51 52
  int GetParameterCount() const { return descriptor_.GetParameterCount(); }
  int GetRegisterParameterCount() const {
53 54
    return descriptor_.GetRegisterParameterCount();
  }
55
  HParameter* GetParameter(int parameter) {
56
    DCHECK(parameter < GetParameterCount());
57 58
    return parameters_[parameter];
  }
59 60 61 62 63 64 65
  Representation GetParameterRepresentation(int parameter) {
    return RepresentationFromType(descriptor_.GetParameterType(parameter));
  }
  bool IsParameterCountRegister(int index) const {
    return descriptor_.GetRegisterParameter(index)
        .is(descriptor_.stack_parameter_count());
  }
66 67
  HValue* GetArgumentsLength() {
    // This is initialized in BuildGraph()
68
    DCHECK(arguments_length_ != NULL);
69 70
    return arguments_length_;
  }
71
  CompilationInfo* info() { return info_; }
72
  CodeStub* stub() { return code_stub_; }
73
  HContext* context() { return context_; }
74
  Isolate* isolate() { return info_->isolate(); }
75

76
  HLoadNamedField* BuildLoadNamedField(HValue* object, FieldIndex index);
77
  void BuildStoreNamedField(HValue* object, HValue* value, FieldIndex index,
78 79
                            Representation representation,
                            bool transition_to_field);
80

81 82
  HValue* BuildPushElement(HValue* object, HValue* argc,
                           HValue* argument_elements, ElementsKind kind);
83

84 85 86
  HValue* UnmappedCase(HValue* elements, HValue* key, HValue* value);
  HValue* EmitKeyedSloppyArguments(HValue* receiver, HValue* key,
                                   HValue* value);
87

88
  HValue* BuildArrayConstructor(ElementsKind kind,
89 90
                                AllocationSiteOverrideMode override_mode);
  HValue* BuildInternalArrayConstructor(ElementsKind kind);
91

92 93
  HValue* BuildToString(HValue* input, bool convert);
  HValue* BuildToPrimitive(HValue* input, HValue* input_map);
94

95
 private:
96 97 98 99
  HValue* BuildArraySingleArgumentConstructor(JSArrayBuilder* builder);
  HValue* BuildArrayNArgumentsConstructor(JSArrayBuilder* builder,
                                          ElementsKind kind);

rmcilroy's avatar
rmcilroy committed
100
  base::SmartArrayPointer<HParameter*> parameters_;
101
  HValue* arguments_length_;
102
  CompilationInfo* info_;
103
  CodeStub* code_stub_;
104
  CodeStubDescriptor descriptor_;
105
  HContext* context_;
106 107 108 109
};


bool CodeStubGraphBuilderBase::BuildGraph() {
110 111 112
  // Update the static counter each time a new code stub is generated.
  isolate()->counters()->code_stubs()->Increment();

113
  if (FLAG_trace_hydrogen_stubs) {
114
    const char* name = CodeStub::MajorName(stub()->MajorKey());
115
    PrintF("-----------------------------------------------------------\n");
116
    PrintF("Compiling stub %s using hydrogen\n", name);
117
    isolate()->GetHTracer()->TraceCompilation(info());
118 119
  }

120
  int param_count = GetParameterCount();
121
  int register_param_count = GetRegisterParameterCount();
122
  HEnvironment* start_environment = graph()->start_environment();
123
  HBasicBlock* next_block = CreateBasicBlock(start_environment);
124
  Goto(next_block);
125 126 127
  next_block->SetJoinId(BailoutId::StubEntry());
  set_current_block(next_block);

128
  bool runtime_stack_params = descriptor_.stack_parameter_count().is_valid();
129
  HInstruction* stack_parameter_count = NULL;
130
  for (int i = 0; i < param_count; ++i) {
131
    Representation r = GetParameterRepresentation(i);
132 133 134 135 136 137
    HParameter* param;
    if (i >= register_param_count) {
      param = Add<HParameter>(i - register_param_count,
                              HParameter::STACK_PARAMETER, r);
    } else {
      param = Add<HParameter>(i, HParameter::REGISTER_PARAMETER, r);
138
      start_environment->Bind(i, param);
139
    }
140
    parameters_[i] = param;
141
    if (i < register_param_count && IsParameterCountRegister(i)) {
142 143 144 145
      param->set_type(HType::Smi());
      stack_parameter_count = param;
      arguments_length_ = stack_parameter_count;
    }
146
  }
147

148
  DCHECK(!runtime_stack_params || arguments_length_ != NULL);
149
  if (!runtime_stack_params) {
150 151 152
    stack_parameter_count =
        Add<HConstant>(param_count - register_param_count - 1);
    // graph()->GetConstantMinus1();
153
    arguments_length_ = graph()->GetConstant0();
154 155
  }

156
  context_ = Add<HContext>();
157
  start_environment->BindContext(context_);
158
  start_environment->Bind(param_count, context_);
159

160
  Add<HSimulate>(BailoutId::StubEntry());
161

162 163
  NoObservableSideEffectsScope no_effects(this);

164
  HValue* return_value = BuildCodeStub();
165 166

  // We might have extra expressions to pop from the stack in addition to the
167
  // arguments above.
168
  HInstruction* stack_pop_count = stack_parameter_count;
169
  if (descriptor_.function_mode() == JS_FUNCTION_STUB_MODE) {
170
    if (!stack_parameter_count->IsConstant() &&
171
        descriptor_.hint_stack_parameter_count() < 0) {
172
      HInstruction* constant_one = graph()->GetConstant1();
173
      stack_pop_count = AddUncasted<HAdd>(stack_parameter_count, constant_one);
174
      stack_pop_count->ClearFlag(HValue::kCanOverflow);
175 176
      // TODO(mvstanton): verify that stack_parameter_count+1 really fits in a
      // smi.
177
    } else {
178
      int count = descriptor_.hint_stack_parameter_count();
179
      stack_pop_count = Add<HConstant>(count);
180
    }
181 182
  }

183
  if (current_block() != NULL) {
184 185
    HReturn* hreturn_instruction = New<HReturn>(return_value,
                                                stack_pop_count);
186
    FinishCurrentBlock(hreturn_instruction);
187
  }
188 189 190
  return true;
}

191

192 193 194
template <class Stub>
class CodeStubGraphBuilder: public CodeStubGraphBuilderBase {
 public:
195 196
  explicit CodeStubGraphBuilder(CompilationInfo* info, CodeStub* stub)
      : CodeStubGraphBuilderBase(info, stub) {}
197 198

 protected:
199
  virtual HValue* BuildCodeStub() {
200
    if (casted_stub()->IsUninitialized()) {
201
      return BuildCodeUninitializedStub();
202 203
    } else {
      return BuildCodeInitializedStub();
204 205 206 207 208 209 210 211 212 213 214
    }
  }

  virtual HValue* BuildCodeInitializedStub() {
    UNIMPLEMENTED();
    return NULL;
  }

  virtual HValue* BuildCodeUninitializedStub() {
    // Force a deopt that falls back to the runtime.
    HValue* undefined = graph()->GetConstantUndefined();
215 216 217
    IfBuilder builder(this);
    builder.IfNot<HCompareObjectEqAndBranch, HValue*>(undefined, undefined);
    builder.Then();
218
    builder.ElseDeopt(Deoptimizer::kForcedDeoptToRuntime);
219 220 221
    return undefined;
  }

222 223 224 225
  Stub* casted_stub() { return static_cast<Stub*>(stub()); }
};


226 227
Handle<Code> HydrogenCodeStub::GenerateLightweightMissCode(
    ExternalReference miss) {
228
  Factory* factory = isolate()->factory();
229 230

  // Generate the new code.
231
  MacroAssembler masm(isolate(), NULL, 256, CodeObjectRequired::kYes);
232 233 234

  {
    // Update the static counter each time a new code stub is generated.
235
    isolate()->counters()->code_stubs()->Increment();
236 237 238

    // Generate the code for the stub.
    masm.set_generating_stub(true);
239 240
    // TODO(yangguo): remove this once we can serialize IC stubs.
    masm.enable_serializer();
241
    NoCurrentFrameScope scope(&masm);
242
    GenerateLightweightMiss(&masm, miss);
243 244 245 246 247 248 249 250
  }

  // Create the code object.
  CodeDesc desc;
  masm.GetCode(&desc);

  // Copy the generated code into a heap object.
  Handle<Code> new_object = factory->NewCode(
251
      desc, GetCodeFlags(), masm.CodeObject(), NeedsImmovableCode());
252 253 254 255
  return new_object;
}


256
template <class Stub>
257 258
static Handle<Code> DoGenerateCode(Stub* stub) {
  Isolate* isolate = stub->isolate();
259
  CodeStubDescriptor descriptor(stub);
260 261

  // If we are uninitialized we can use a light-weight stub to enter
262 263
  // the runtime that is significantly faster than using the standard
  // stub-failure deopt mechanism.
264 265
  if (stub->IsUninitialized() && descriptor.has_miss_handler()) {
    DCHECK(!descriptor.stack_parameter_count().is_valid());
266
    return stub->GenerateLightweightMissCode(descriptor.miss_handler());
267
  }
268
  base::ElapsedTimer timer;
269 270 271
  if (FLAG_profile_hydrogen_code_stub_compilation) {
    timer.Start();
  }
272
  Zone zone(isolate->allocator());
273 274
  CompilationInfo info(CStrVector(CodeStub::MajorName(stub->MajorKey())),
                       isolate, &zone, stub->GetCodeFlags());
275 276 277 278 279 280 281
  // Parameter count is number of stack parameters.
  int parameter_count = descriptor.GetStackParameterCount();
  if (descriptor.function_mode() == NOT_JS_FUNCTION_STUB_MODE) {
    parameter_count--;
  }
  info.set_parameter_count(parameter_count);
  CodeStubGraphBuilder<Stub> builder(&info, stub);
282
  LChunk* chunk = OptimizeGraph(builder.CreateGraph());
283 284
  Handle<Code> code = chunk->Codegen();
  if (FLAG_profile_hydrogen_code_stub_compilation) {
285 286
    OFStream os(stdout);
    os << "[Lazy compilation of " << stub << " took "
287
       << timer.Elapsed().InMillisecondsF() << " ms]" << std::endl;
288 289
  }
  return code;
290 291 292
}


293 294 295 296
template <>
HValue* CodeStubGraphBuilder<NumberToStringStub>::BuildCodeStub() {
  info()->MarkAsSavesCallerDoubles();
  HValue* number = GetParameter(NumberToStringStub::kNumber);
297
  return BuildNumberToString(number, Type::Number());
298 299 300
}


301 302
Handle<Code> NumberToStringStub::GenerateCode() {
  return DoGenerateCode(this);
303 304 305
}


306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
// Returns the type string of a value; see ECMA-262, 11.4.3 (p 47).
template <>
HValue* CodeStubGraphBuilder<TypeofStub>::BuildCodeStub() {
  Factory* factory = isolate()->factory();
  HConstant* number_string = Add<HConstant>(factory->number_string());
  HValue* object = GetParameter(TypeofStub::kObject);

  IfBuilder is_smi(this);
  HValue* smi_check = is_smi.If<HIsSmiAndBranch>(object);
  is_smi.Then();
  { Push(number_string); }
  is_smi.Else();
  {
    IfBuilder is_number(this);
    is_number.If<HCompareMap>(object, isolate()->factory()->heap_number_map());
    is_number.Then();
    { Push(number_string); }
    is_number.Else();
    {
      HValue* map = AddLoadMap(object, smi_check);
      HValue* instance_type = Add<HLoadNamedField>(
          map, nullptr, HObjectAccess::ForMapInstanceType());
      IfBuilder is_string(this);
      is_string.If<HCompareNumericAndBranch>(
          instance_type, Add<HConstant>(FIRST_NONSTRING_TYPE), Token::LT);
      is_string.Then();
      { Push(Add<HConstant>(factory->string_string())); }
      is_string.Else();
      {
        HConstant* object_string = Add<HConstant>(factory->object_string());
        IfBuilder is_oddball(this);
        is_oddball.If<HCompareNumericAndBranch>(
            instance_type, Add<HConstant>(ODDBALL_TYPE), Token::EQ);
        is_oddball.Then();
        {
341 342
          Push(Add<HLoadNamedField>(object, nullptr,
                                    HObjectAccess::ForOddballTypeOf()));
343 344 345 346 347 348 349 350 351 352
        }
        is_oddball.Else();
        {
          IfBuilder is_symbol(this);
          is_symbol.If<HCompareNumericAndBranch>(
              instance_type, Add<HConstant>(SYMBOL_TYPE), Token::EQ);
          is_symbol.Then();
          { Push(Add<HConstant>(factory->symbol_string())); }
          is_symbol.Else();
          {
353 354 355 356 357 358
            HValue* bit_field = Add<HLoadNamedField>(
                map, nullptr, HObjectAccess::ForMapBitField());
            HValue* bit_field_masked = AddUncasted<HBitwise>(
                Token::BIT_AND, bit_field,
                Add<HConstant>((1 << Map::kIsCallable) |
                               (1 << Map::kIsUndetectable)));
359
            IfBuilder is_function(this);
360 361 362
            is_function.If<HCompareNumericAndBranch>(
                bit_field_masked, Add<HConstant>(1 << Map::kIsCallable),
                Token::EQ);
363 364 365 366
            is_function.Then();
            { Push(Add<HConstant>(factory->function_string())); }
            is_function.Else();
            {
367 368 369 370 371 372 373 374 375 376 377
#define SIMD128_BUILDER_OPEN(TYPE, Type, type, lane_count, lane_type) \
  IfBuilder is_##type(this);                                          \
  is_##type.If<HCompareObjectEqAndBranch>(                            \
      map, Add<HConstant>(factory->type##_map()));                    \
  is_##type.Then();                                                   \
  { Push(Add<HConstant>(factory->type##_string())); }                 \
  is_##type.Else(); {
              SIMD128_TYPES(SIMD128_BUILDER_OPEN)
#undef SIMD128_BUILDER_OPEN
              // Is it an undetectable object?
              IfBuilder is_undetectable(this);
378
              is_undetectable.If<HCompareNumericAndBranch>(
379
                  bit_field_masked, graph()->GetConstant0(), Token::NE);
380
              is_undetectable.Then();
381
              {
382 383
                // typeof an undetectable object is 'undefined'.
                Push(Add<HConstant>(factory->undefined_string()));
384
              }
385 386 387 388 389 390 391 392 393
              is_undetectable.Else();
              {
                // For any kind of object not handled above, the spec rule for
                // host objects gives that it is okay to return "object".
                Push(object_string);
              }
#define SIMD128_BUILDER_CLOSE(TYPE, Type, type, lane_count, lane_type) }
              SIMD128_TYPES(SIMD128_BUILDER_CLOSE)
#undef SIMD128_BUILDER_CLOSE
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
            }
            is_function.End();
          }
          is_symbol.End();
        }
        is_oddball.End();
      }
      is_string.End();
    }
    is_number.End();
  }
  is_smi.End();

  return environment()->Pop();
}


Handle<Code> TypeofStub::GenerateCode() { return DoGenerateCode(this); }


414 415 416 417 418 419 420 421 422 423 424 425
template <>
HValue* CodeStubGraphBuilder<FastCloneRegExpStub>::BuildCodeStub() {
  HValue* closure = GetParameter(0);
  HValue* literal_index = GetParameter(1);

  // This stub is very performance sensitive, the generated code must be tuned
  // so that it doesn't build and eager frame.
  info()->MarkMustNotHaveEagerFrame();

  HValue* literals_array = Add<HLoadNamedField>(
      closure, nullptr, HObjectAccess::ForLiteralsPointer());
  HInstruction* boilerplate = Add<HLoadKeyed>(
426 427
      literals_array, literal_index, nullptr, nullptr, FAST_ELEMENTS,
      NEVER_RETURN_HOLE, LiteralsArray::kOffsetToFirstLiteral - kHeapObjectTag);
428 429 430 431 432 433 434 435 436 437

  IfBuilder if_notundefined(this);
  if_notundefined.IfNot<HCompareObjectEqAndBranch>(
      boilerplate, graph()->GetConstantUndefined());
  if_notundefined.Then();
  {
    int result_size =
        JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
    HValue* result =
        Add<HAllocate>(Add<HConstant>(result_size), HType::JSObject(),
438
                       NOT_TENURED, JS_REGEXP_TYPE, graph()->GetConstant0());
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    Add<HStoreNamedField>(
        result, HObjectAccess::ForMap(),
        Add<HLoadNamedField>(boilerplate, nullptr, HObjectAccess::ForMap()));
    Add<HStoreNamedField>(
        result, HObjectAccess::ForPropertiesPointer(),
        Add<HLoadNamedField>(boilerplate, nullptr,
                             HObjectAccess::ForPropertiesPointer()));
    Add<HStoreNamedField>(
        result, HObjectAccess::ForElementsPointer(),
        Add<HLoadNamedField>(boilerplate, nullptr,
                             HObjectAccess::ForElementsPointer()));
    for (int offset = JSObject::kHeaderSize; offset < result_size;
         offset += kPointerSize) {
      HObjectAccess access = HObjectAccess::ForObservableJSObjectOffset(offset);
      Add<HStoreNamedField>(result, access,
                            Add<HLoadNamedField>(boilerplate, nullptr, access));
    }
    Push(result);
  }
  if_notundefined.ElseDeopt(Deoptimizer::kUninitializedBoilerplateInFastClone);
  if_notundefined.End();

  return Pop();
}


Handle<Code> FastCloneRegExpStub::GenerateCode() {
  return DoGenerateCode(this);
}


470 471 472
template <>
HValue* CodeStubGraphBuilder<FastCloneShallowArrayStub>::BuildCodeStub() {
  Factory* factory = isolate()->factory();
473
  HValue* undefined = graph()->GetConstantUndefined();
474
  AllocationSiteMode alloc_site_mode = casted_stub()->allocation_site_mode();
475 476
  HValue* closure = GetParameter(0);
  HValue* literal_index = GetParameter(1);
477 478 479 480

  // This stub is very performance sensitive, the generated code must be tuned
  // so that it doesn't build and eager frame.
  info()->MarkMustNotHaveEagerFrame();
481

482 483 484
  HValue* literals_array = Add<HLoadNamedField>(
      closure, nullptr, HObjectAccess::ForLiteralsPointer());

485
  HInstruction* allocation_site = Add<HLoadKeyed>(
486 487
      literals_array, literal_index, nullptr, nullptr, FAST_ELEMENTS,
      NEVER_RETURN_HOLE, LiteralsArray::kOffsetToFirstLiteral - kHeapObjectTag);
488
  IfBuilder checker(this);
489 490
  checker.IfNot<HCompareObjectEqAndBranch, HValue*>(allocation_site,
                                                    undefined);
491
  checker.Then();
492

493 494
  HObjectAccess access = HObjectAccess::ForAllocationSiteOffset(
      AllocationSite::kTransitionInfoOffset);
495 496
  HInstruction* boilerplate =
      Add<HLoadNamedField>(allocation_site, nullptr, access);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
  HValue* elements = AddLoadElements(boilerplate);
  HValue* capacity = AddLoadFixedArrayLength(elements);
  IfBuilder zero_capacity(this);
  zero_capacity.If<HCompareNumericAndBranch>(capacity, graph()->GetConstant0(),
                                           Token::EQ);
  zero_capacity.Then();
  Push(BuildCloneShallowArrayEmpty(boilerplate,
                                   allocation_site,
                                   alloc_site_mode));
  zero_capacity.Else();
  IfBuilder if_fixed_cow(this);
  if_fixed_cow.If<HCompareMap>(elements, factory->fixed_cow_array_map());
  if_fixed_cow.Then();
  Push(BuildCloneShallowArrayCow(boilerplate,
                                 allocation_site,
                                 alloc_site_mode,
                                 FAST_ELEMENTS));
  if_fixed_cow.Else();
  IfBuilder if_fixed(this);
  if_fixed.If<HCompareMap>(elements, factory->fixed_array_map());
  if_fixed.Then();
  Push(BuildCloneShallowArrayNonEmpty(boilerplate,
                                      allocation_site,
                                      alloc_site_mode,
                                      FAST_ELEMENTS));

  if_fixed.Else();
  Push(BuildCloneShallowArrayNonEmpty(boilerplate,
                                      allocation_site,
                                      alloc_site_mode,
                                      FAST_DOUBLE_ELEMENTS));
  if_fixed.End();
  if_fixed_cow.End();
  zero_capacity.End();
531

532
  checker.ElseDeopt(Deoptimizer::kUninitializedBoilerplateLiterals);
533 534 535
  checker.End();

  return environment()->Pop();
536 537 538
}


539 540
Handle<Code> FastCloneShallowArrayStub::GenerateCode() {
  return DoGenerateCode(this);
541 542 543
}


544 545
template <>
HValue* CodeStubGraphBuilder<CreateAllocationSiteStub>::BuildCodeStub() {
546 547 548 549
  // This stub is performance sensitive, the generated code must be tuned
  // so that it doesn't build an eager frame.
  info()->MarkMustNotHaveEagerFrame();

550
  HValue* size = Add<HConstant>(AllocationSite::kSize);
551 552 553
  HInstruction* object =
      Add<HAllocate>(size, HType::JSObject(), TENURED, JS_OBJECT_TYPE,
                     graph()->GetConstant0());
554 555

  // Store the map
556
  Handle<Map> allocation_site_map = isolate()->factory()->allocation_site_map();
557 558 559
  AddStoreMapConstant(object, allocation_site_map);

  // Store the payload (smi elements kind)
560
  HValue* initial_elements_kind = Add<HConstant>(GetInitialFastElementsKind());
561
  Add<HStoreNamedField>(object,
562 563
                        HObjectAccess::ForAllocationSiteOffset(
                            AllocationSite::kTransitionInfoOffset),
564
                        initial_elements_kind);
565

566 567
  // Unlike literals, constructed arrays don't have nested sites
  Add<HStoreNamedField>(object,
568 569
                        HObjectAccess::ForAllocationSiteOffset(
                            AllocationSite::kNestedSiteOffset),
570
                        graph()->GetConstant0());
571

572
  // Pretenuring calculation field.
573 574
  Add<HStoreNamedField>(object,
                        HObjectAccess::ForAllocationSiteOffset(
575
                            AllocationSite::kPretenureDataOffset),
576
                        graph()->GetConstant0());
577

578
  // Pretenuring memento creation count field.
579 580
  Add<HStoreNamedField>(object,
                        HObjectAccess::ForAllocationSiteOffset(
581
                            AllocationSite::kPretenureCreateCountOffset),
582
                        graph()->GetConstant0());
583

584 585 586
  // Store an empty fixed array for the code dependency.
  HConstant* empty_fixed_array =
    Add<HConstant>(isolate()->factory()->empty_fixed_array());
587
  Add<HStoreNamedField>(
588
      object,
589 590
      HObjectAccess::ForAllocationSiteOffset(
          AllocationSite::kDependentCodeOffset),
591
      empty_fixed_array);
592

593 594 595
  // Link the object to the allocation site list
  HValue* site_list = Add<HConstant>(
      ExternalReference::allocation_sites_list_address(isolate()));
596 597
  HValue* site = Add<HLoadNamedField>(site_list, nullptr,
                                      HObjectAccess::ForAllocationSiteList());
598 599 600 601 602 603 604
  // TODO(mvstanton): This is a store to a weak pointer, which we may want to
  // mark as such in order to skip the write barrier, once we have a unified
  // system for weakness. For now we decided to keep it like this because having
  // an initial write barrier backed store makes this pointer strong until the
  // next GC, and allocation sites are designed to survive several GCs anyway.
  Add<HStoreNamedField>(
      object,
605
      HObjectAccess::ForAllocationSiteOffset(AllocationSite::kWeakNextOffset),
606
      site);
607
  Add<HStoreNamedField>(site_list, HObjectAccess::ForAllocationSiteList(),
608
                        object);
609

610 611
  HInstruction* feedback_vector = GetParameter(0);
  HInstruction* slot = GetParameter(1);
612
  Add<HStoreKeyed>(feedback_vector, slot, object, nullptr, FAST_ELEMENTS,
613 614
                   INITIALIZING_STORE);
  return feedback_vector;
615 616 617
}


618 619
Handle<Code> CreateAllocationSiteStub::GenerateCode() {
  return DoGenerateCode(this);
620 621 622
}


623 624 625 626 627 628 629 630
template <>
HValue* CodeStubGraphBuilder<CreateWeakCellStub>::BuildCodeStub() {
  // This stub is performance sensitive, the generated code must be tuned
  // so that it doesn't build an eager frame.
  info()->MarkMustNotHaveEagerFrame();

  HValue* size = Add<HConstant>(WeakCell::kSize);
  HInstruction* object =
631 632
      Add<HAllocate>(size, HType::JSObject(), TENURED, JS_OBJECT_TYPE,
                     graph()->GetConstant0());
633 634 635 636 637 638 639

  Handle<Map> weak_cell_map = isolate()->factory()->weak_cell_map();
  AddStoreMapConstant(object, weak_cell_map);

  HInstruction* value = GetParameter(CreateWeakCellDescriptor::kValueIndex);
  Add<HStoreNamedField>(object, HObjectAccess::ForWeakCellValue(), value);
  Add<HStoreNamedField>(object, HObjectAccess::ForWeakCellNext(),
640
                        graph()->GetConstantHole());
641 642 643 644

  HInstruction* feedback_vector =
      GetParameter(CreateWeakCellDescriptor::kVectorIndex);
  HInstruction* slot = GetParameter(CreateWeakCellDescriptor::kSlotIndex);
645
  Add<HStoreKeyed>(feedback_vector, slot, object, nullptr, FAST_ELEMENTS,
646 647 648 649 650 651 652 653
                   INITIALIZING_STORE);
  return graph()->GetConstant0();
}


Handle<Code> CreateWeakCellStub::GenerateCode() { return DoGenerateCode(this); }


654
template <>
655
HValue* CodeStubGraphBuilder<LoadScriptContextFieldStub>::BuildCodeStub() {
656 657 658
  int context_index = casted_stub()->context_index();
  int slot_index = casted_stub()->slot_index();

659
  HValue* script_context = BuildGetScriptContext(context_index);
660
  return Add<HLoadNamedField>(script_context, nullptr,
661 662 663 664
                              HObjectAccess::ForContextSlot(slot_index));
}


665
Handle<Code> LoadScriptContextFieldStub::GenerateCode() {
666 667 668 669
  return DoGenerateCode(this);
}


670
template <>
671
HValue* CodeStubGraphBuilder<StoreScriptContextFieldStub>::BuildCodeStub() {
672 673 674
  int context_index = casted_stub()->context_index();
  int slot_index = casted_stub()->slot_index();

675 676
  HValue* script_context = BuildGetScriptContext(context_index);
  Add<HStoreNamedField>(script_context,
677 678 679 680 681 682
                        HObjectAccess::ForContextSlot(slot_index),
                        GetParameter(2), STORE_TO_INITIALIZED_ENTRY);
  return GetParameter(2);
}


683
Handle<Code> StoreScriptContextFieldStub::GenerateCode() {
684 685 686
  return DoGenerateCode(this);
}

687 688
HValue* CodeStubGraphBuilderBase::BuildPushElement(HValue* object, HValue* argc,
                                                   HValue* argument_elements,
689
                                                   ElementsKind kind) {
690 691 692 693 694 695 696 697
  // Precheck whether all elements fit into the array.
  if (!IsFastObjectElementsKind(kind)) {
    LoopBuilder builder(this, context(), LoopBuilder::kPostIncrement);
    HValue* start = graph()->GetConstant0();
    HValue* key = builder.BeginBody(start, argc, Token::LT);
    {
      HInstruction* argument =
          Add<HAccessArgumentsAt>(argument_elements, argc, key);
698 699 700 701 702 703 704 705 706
      IfBuilder can_store(this);
      can_store.IfNot<HIsSmiAndBranch>(argument);
      if (IsFastDoubleElementsKind(kind)) {
        can_store.And();
        can_store.IfNot<HCompareMap>(argument,
                                     isolate()->factory()->heap_number_map());
      }
      can_store.ThenDeopt(Deoptimizer::kFastArrayPushFailed);
      can_store.End();
707 708 709 710
    }
    builder.EndBody();
  }

711 712
  HValue* length = Add<HLoadNamedField>(object, nullptr,
                                        HObjectAccess::ForArrayLength(kind));
713 714 715
  HValue* new_length = AddUncasted<HAdd>(length, argc);
  HValue* max_key = AddUncasted<HSub>(new_length, graph()->GetConstant1());

716 717
  HValue* elements = Add<HLoadNamedField>(object, nullptr,
                                          HObjectAccess::ForElementsPointer());
718
  elements = BuildCheckForCapacityGrow(object, elements, kind, length, max_key,
719
                                       true, STORE);
720 721 722 723 724 725 726 727 728 729 730

  LoopBuilder builder(this, context(), LoopBuilder::kPostIncrement);
  HValue* start = graph()->GetConstant0();
  HValue* key = builder.BeginBody(start, argc, Token::LT);
  {
    HValue* argument = Add<HAccessArgumentsAt>(argument_elements, argc, key);
    HValue* index = AddUncasted<HAdd>(key, length);
    AddElementAccess(elements, index, argument, object, nullptr, kind, STORE);
  }
  builder.EndBody();
  return new_length;
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
}

template <>
HValue* CodeStubGraphBuilder<FastArrayPushStub>::BuildCodeStub() {
  // TODO(verwaest): Fix deoptimizer messages.
  HValue* argc = GetArgumentsLength();
  HInstruction* argument_elements = Add<HArgumentsElements>(false, false);
  HInstruction* object = Add<HAccessArgumentsAt>(argument_elements, argc,
                                                 graph()->GetConstantMinus1());
  BuildCheckHeapObject(object);
  HValue* map = Add<HLoadNamedField>(object, nullptr, HObjectAccess::ForMap());
  Add<HCheckInstanceType>(object, HCheckInstanceType::IS_JS_ARRAY);

  // Disallow pushing onto prototypes. It might be the JSArray prototype.
  // Disallow pushing onto non-extensible objects.
  {
    HValue* bit_field2 =
        Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapBitField2());
    HValue* mask =
        Add<HConstant>(static_cast<int>(Map::IsPrototypeMapBits::kMask) |
                       (1 << Map::kIsExtensible));
    HValue* bits = AddUncasted<HBitwise>(Token::BIT_AND, bit_field2, mask);
    IfBuilder check(this);
    check.If<HCompareNumericAndBranch>(
        bits, Add<HConstant>(1 << Map::kIsExtensible), Token::NE);
    check.ThenDeopt(Deoptimizer::kFastArrayPushFailed);
    check.End();
  }

  // Disallow pushing onto arrays in dictionary named property mode. We need to
  // figure out whether the length property is still writable.
  {
    HValue* bit_field3 =
        Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapBitField3());
    HValue* mask = Add<HConstant>(static_cast<int>(Map::DictionaryMap::kMask));
    HValue* bit = AddUncasted<HBitwise>(Token::BIT_AND, bit_field3, mask);
    IfBuilder check(this);
    check.If<HCompareNumericAndBranch>(bit, mask, Token::EQ);
    check.ThenDeopt(Deoptimizer::kFastArrayPushFailed);
    check.End();
  }

  // Check whether the length property is writable. The length property is the
  // only default named property on arrays. It's nonconfigurable, hence is
  // guaranteed to stay the first property.
  {
    HValue* descriptors =
        Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapDescriptors());
    HValue* details = Add<HLoadKeyed>(
        descriptors, Add<HConstant>(DescriptorArray::ToDetailsIndex(0)),
        nullptr, nullptr, FAST_SMI_ELEMENTS);
    HValue* mask =
        Add<HConstant>(READ_ONLY << PropertyDetails::AttributesField::kShift);
    HValue* bit = AddUncasted<HBitwise>(Token::BIT_AND, details, mask);
    IfBuilder readonly(this);
    readonly.If<HCompareNumericAndBranch>(bit, mask, Token::EQ);
    readonly.ThenDeopt(Deoptimizer::kFastArrayPushFailed);
    readonly.End();
  }

  HValue* null = Add<HLoadRoot>(Heap::kNullValueRootIndex);
  HValue* empty = Add<HLoadRoot>(Heap::kEmptyFixedArrayRootIndex);
  environment()->Push(map);
  LoopBuilder check_prototypes(this);
  check_prototypes.BeginBody(1);
  {
    HValue* parent_map = environment()->Pop();
    HValue* prototype = Add<HLoadNamedField>(parent_map, nullptr,
                                             HObjectAccess::ForPrototype());

    IfBuilder is_null(this);
    is_null.If<HCompareObjectEqAndBranch>(prototype, null);
    is_null.Then();
    check_prototypes.Break();
    is_null.End();

    HValue* prototype_map =
        Add<HLoadNamedField>(prototype, nullptr, HObjectAccess::ForMap());
    HValue* instance_type = Add<HLoadNamedField>(
        prototype_map, nullptr, HObjectAccess::ForMapInstanceType());
    IfBuilder check_instance_type(this);
    check_instance_type.If<HCompareNumericAndBranch>(
        instance_type, Add<HConstant>(LAST_CUSTOM_ELEMENTS_RECEIVER),
        Token::LTE);
    check_instance_type.ThenDeopt(Deoptimizer::kFastArrayPushFailed);
    check_instance_type.End();

    HValue* elements = Add<HLoadNamedField>(
        prototype, nullptr, HObjectAccess::ForElementsPointer());
    IfBuilder no_elements(this);
    no_elements.IfNot<HCompareObjectEqAndBranch>(elements, empty);
    no_elements.ThenDeopt(Deoptimizer::kFastArrayPushFailed);
    no_elements.End();

    environment()->Push(prototype_map);
  }
  check_prototypes.EndBody();

  HValue* bit_field2 =
      Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapBitField2());
  HValue* kind = BuildDecodeField<Map::ElementsKindBits>(bit_field2);

  // Below we only check the upper bound of the relevant ranges to include both
  // holey and non-holey versions. We check them in order smi, object, double
  // since smi < object < double.
  STATIC_ASSERT(FAST_SMI_ELEMENTS < FAST_HOLEY_SMI_ELEMENTS);
  STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS < FAST_HOLEY_ELEMENTS);
  STATIC_ASSERT(FAST_ELEMENTS < FAST_HOLEY_ELEMENTS);
  STATIC_ASSERT(FAST_HOLEY_ELEMENTS < FAST_HOLEY_DOUBLE_ELEMENTS);
  STATIC_ASSERT(FAST_DOUBLE_ELEMENTS < FAST_HOLEY_DOUBLE_ELEMENTS);
  IfBuilder has_smi_elements(this);
  has_smi_elements.If<HCompareNumericAndBranch>(
      kind, Add<HConstant>(FAST_HOLEY_SMI_ELEMENTS), Token::LTE);
  has_smi_elements.Then();
  {
846 847 848
    HValue* new_length = BuildPushElement(object, argc, argument_elements,
                                          FAST_HOLEY_SMI_ELEMENTS);
    environment()->Push(new_length);
849 850 851 852 853 854 855 856
  }
  has_smi_elements.Else();
  {
    IfBuilder has_object_elements(this);
    has_object_elements.If<HCompareNumericAndBranch>(
        kind, Add<HConstant>(FAST_HOLEY_ELEMENTS), Token::LTE);
    has_object_elements.Then();
    {
857 858 859
      HValue* new_length = BuildPushElement(object, argc, argument_elements,
                                            FAST_HOLEY_ELEMENTS);
      environment()->Push(new_length);
860 861 862 863 864 865 866 867
    }
    has_object_elements.Else();
    {
      IfBuilder has_double_elements(this);
      has_double_elements.If<HCompareNumericAndBranch>(
          kind, Add<HConstant>(FAST_HOLEY_DOUBLE_ELEMENTS), Token::LTE);
      has_double_elements.Then();
      {
868 869 870
        HValue* new_length = BuildPushElement(object, argc, argument_elements,
                                              FAST_HOLEY_DOUBLE_ELEMENTS);
        environment()->Push(new_length);
871 872 873 874 875 876 877 878
      }
      has_double_elements.ElseDeopt(Deoptimizer::kFastArrayPushFailed);
      has_double_elements.End();
    }
    has_object_elements.End();
  }
  has_smi_elements.End();

879
  return environment()->Pop();
880 881 882
}

Handle<Code> FastArrayPushStub::GenerateCode() { return DoGenerateCode(this); }
883

884 885
template <>
HValue* CodeStubGraphBuilder<GrowArrayElementsStub>::BuildCodeStub() {
886 887 888 889 890
  ElementsKind kind = casted_stub()->elements_kind();
  if (IsFastDoubleElementsKind(kind)) {
    info()->MarkAsSavesCallerDoubles();
  }

891 892 893 894
  HValue* object = GetParameter(GrowArrayElementsDescriptor::kObjectIndex);
  HValue* key = GetParameter(GrowArrayElementsDescriptor::kKeyIndex);

  HValue* elements = AddLoadElements(object);
895 896 897
  HValue* current_capacity = Add<HLoadNamedField>(
      elements, nullptr, HObjectAccess::ForFixedArrayLength());

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  HValue* length =
      casted_stub()->is_js_array()
          ? Add<HLoadNamedField>(object, static_cast<HValue*>(NULL),
                                 HObjectAccess::ForArrayLength(kind))
          : current_capacity;

  return BuildCheckAndGrowElementsCapacity(object, elements, kind, length,
                                           current_capacity, key);
}


Handle<Code> GrowArrayElementsStub::GenerateCode() {
  return DoGenerateCode(this);
}


914
template <>
915
HValue* CodeStubGraphBuilder<LoadFastElementStub>::BuildCodeStub() {
916 917 918 919
  LoadKeyedHoleMode hole_mode = casted_stub()->convert_hole_to_undefined()
                                    ? CONVERT_HOLE_TO_UNDEFINED
                                    : NEVER_RETURN_HOLE;

920
  HInstruction* load = BuildUncheckedMonomorphicElementAccess(
921 922
      GetParameter(LoadDescriptor::kReceiverIndex),
      GetParameter(LoadDescriptor::kNameIndex), NULL,
923
      casted_stub()->is_js_array(), casted_stub()->elements_kind(), LOAD,
924
      hole_mode, STANDARD_STORE);
925
  return load;
926 927 928
}


929
Handle<Code> LoadFastElementStub::GenerateCode() {
930
  return DoGenerateCode(this);
931 932 933
}


934
HLoadNamedField* CodeStubGraphBuilderBase::BuildLoadNamedField(
935 936 937 938 939 940
    HValue* object, FieldIndex index) {
  Representation representation = index.is_double()
      ? Representation::Double()
      : Representation::Tagged();
  int offset = index.offset();
  HObjectAccess access = index.is_inobject()
941 942
      ? HObjectAccess::ForObservableJSObjectOffset(offset, representation)
      : HObjectAccess::ForBackingStoreOffset(offset, representation);
943 944
  if (index.is_double() &&
      (!FLAG_unbox_double_fields || !index.is_inobject())) {
945 946
    // Load the heap number.
    object = Add<HLoadNamedField>(
947
        object, nullptr, access.WithRepresentation(Representation::Tagged()));
948 949 950
    // Load the double value from it.
    access = HObjectAccess::ForHeapNumberValue();
  }
951
  return Add<HLoadNamedField>(object, nullptr, access);
952 953 954
}


955 956
template<>
HValue* CodeStubGraphBuilder<LoadFieldStub>::BuildCodeStub() {
957
  return BuildLoadNamedField(GetParameter(0), casted_stub()->index());
958 959 960
}


961 962
Handle<Code> LoadFieldStub::GenerateCode() {
  return DoGenerateCode(this);
963 964 965
}


966 967 968 969 970 971 972 973 974 975 976 977
template <>
HValue* CodeStubGraphBuilder<ArrayBufferViewLoadFieldStub>::BuildCodeStub() {
  return BuildArrayBufferViewFieldAccessor(GetParameter(0), nullptr,
                                           casted_stub()->index());
}


Handle<Code> ArrayBufferViewLoadFieldStub::GenerateCode() {
  return DoGenerateCode(this);
}


978 979 980 981 982
template <>
HValue* CodeStubGraphBuilder<LoadConstantStub>::BuildCodeStub() {
  HValue* map = AddLoadMap(GetParameter(0), NULL);
  HObjectAccess descriptors_access = HObjectAccess::ForObservableJSObjectOffset(
      Map::kDescriptorsOffset, Representation::Tagged());
983
  HValue* descriptors = Add<HLoadNamedField>(map, nullptr, descriptors_access);
984
  HObjectAccess value_access = HObjectAccess::ForObservableJSObjectOffset(
985
      DescriptorArray::GetValueOffset(casted_stub()->constant_index()));
986
  return Add<HLoadNamedField>(descriptors, nullptr, value_access);
987 988 989 990 991 992
}


Handle<Code> LoadConstantStub::GenerateCode() { return DoGenerateCode(this); }


993 994 995
HValue* CodeStubGraphBuilderBase::UnmappedCase(HValue* elements, HValue* key,
                                               HValue* value) {
  HValue* result = NULL;
996
  HInstruction* backing_store =
997 998
      Add<HLoadKeyed>(elements, graph()->GetConstant1(), nullptr, nullptr,
                      FAST_ELEMENTS, ALLOW_RETURN_HOLE);
999
  Add<HCheckMaps>(backing_store, isolate()->factory()->fixed_array_map());
1000 1001
  HValue* backing_store_length = Add<HLoadNamedField>(
      backing_store, nullptr, HObjectAccess::ForFixedArrayLength());
1002 1003 1004 1005 1006
  IfBuilder in_unmapped_range(this);
  in_unmapped_range.If<HCompareNumericAndBranch>(key, backing_store_length,
                                                 Token::LT);
  in_unmapped_range.Then();
  {
1007
    if (value == NULL) {
1008 1009
      result = Add<HLoadKeyed>(backing_store, key, nullptr, nullptr,
                               FAST_HOLEY_ELEMENTS, NEVER_RETURN_HOLE);
1010
    } else {
1011
      Add<HStoreKeyed>(backing_store, key, value, nullptr, FAST_HOLEY_ELEMENTS);
1012
    }
1013
  }
1014
  in_unmapped_range.ElseDeopt(Deoptimizer::kOutsideOfRange);
1015 1016 1017 1018 1019
  in_unmapped_range.End();
  return result;
}


1020 1021 1022
HValue* CodeStubGraphBuilderBase::EmitKeyedSloppyArguments(HValue* receiver,
                                                           HValue* key,
                                                           HValue* value) {
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  // Mapped arguments are actual arguments. Unmapped arguments are values added
  // to the arguments object after it was created for the call. Mapped arguments
  // are stored in the context at indexes given by elements[key + 2]. Unmapped
  // arguments are stored as regular indexed properties in the arguments array,
  // held at elements[1]. See NewSloppyArguments() in runtime.cc for a detailed
  // look at argument object construction.
  //
  // The sloppy arguments elements array has a special format:
  //
  // 0: context
  // 1: unmapped arguments array
  // 2: mapped_index0,
  // 3: mapped_index1,
  // ...
  //
  // length is 2 + min(number_of_actual_arguments, number_of_formal_arguments).
  // If key + 2 >= elements.length then attempt to look in the unmapped
  // arguments array (given by elements[1]) and return the value at key, missing
  // to the runtime if the unmapped arguments array is not a fixed array or if
  // key >= unmapped_arguments_array.length.
  //
  // Otherwise, t = elements[key + 2]. If t is the hole, then look up the value
  // in the unmapped arguments array, as described above. Otherwise, t is a Smi
  // index into the context array given at elements[0]. Return the value at
  // context[t].

1049 1050
  bool is_load = value == NULL;

1051 1052 1053 1054
  key = AddUncasted<HForceRepresentation>(key, Representation::Smi());
  IfBuilder positive_smi(this);
  positive_smi.If<HCompareNumericAndBranch>(key, graph()->GetConstant0(),
                                            Token::LT);
1055
  positive_smi.ThenDeopt(Deoptimizer::kKeyIsNegative);
1056 1057 1058
  positive_smi.End();

  HValue* constant_two = Add<HConstant>(2);
1059 1060 1061
  HValue* elements = AddLoadElements(receiver, nullptr);
  HValue* elements_length = Add<HLoadNamedField>(
      elements, nullptr, HObjectAccess::ForFixedArrayLength());
1062 1063 1064 1065 1066 1067
  HValue* adjusted_length = AddUncasted<HSub>(elements_length, constant_two);
  IfBuilder in_range(this);
  in_range.If<HCompareNumericAndBranch>(key, adjusted_length, Token::LT);
  in_range.Then();
  {
    HValue* index = AddUncasted<HAdd>(key, constant_two);
1068 1069 1070
    HInstruction* mapped_index =
        Add<HLoadKeyed>(elements, index, nullptr, nullptr, FAST_HOLEY_ELEMENTS,
                        ALLOW_RETURN_HOLE);
1071 1072 1073 1074 1075 1076 1077 1078 1079

    IfBuilder is_valid(this);
    is_valid.IfNot<HCompareObjectEqAndBranch>(mapped_index,
                                              graph()->GetConstantHole());
    is_valid.Then();
    {
      // TODO(mvstanton): I'd like to assert from this point, that if the
      // mapped_index is not the hole that it is indeed, a smi. An unnecessary
      // smi check is being emitted.
1080
      HValue* the_context = Add<HLoadKeyed>(elements, graph()->GetConstant0(),
1081
                                            nullptr, nullptr, FAST_ELEMENTS);
1082
      STATIC_ASSERT(Context::kHeaderSize == FixedArray::kHeaderSize);
1083
      if (is_load) {
1084 1085 1086
        HValue* result =
            Add<HLoadKeyed>(the_context, mapped_index, nullptr, nullptr,
                            FAST_ELEMENTS, ALLOW_RETURN_HOLE);
1087 1088 1089
        environment()->Push(result);
      } else {
        DCHECK(value != NULL);
1090 1091
        Add<HStoreKeyed>(the_context, mapped_index, value, nullptr,
                         FAST_ELEMENTS);
1092 1093
        environment()->Push(value);
      }
1094 1095 1096
    }
    is_valid.Else();
    {
1097 1098
      HValue* result = UnmappedCase(elements, key, value);
      environment()->Push(is_load ? result : value);
1099 1100 1101 1102 1103
    }
    is_valid.End();
  }
  in_range.Else();
  {
1104 1105
    HValue* result = UnmappedCase(elements, key, value);
    environment()->Push(is_load ? result : value);
1106 1107 1108 1109 1110 1111 1112
  }
  in_range.End();

  return environment()->Pop();
}


1113 1114 1115 1116 1117 1118 1119 1120 1121
template <>
HValue* CodeStubGraphBuilder<KeyedLoadSloppyArgumentsStub>::BuildCodeStub() {
  HValue* receiver = GetParameter(LoadDescriptor::kReceiverIndex);
  HValue* key = GetParameter(LoadDescriptor::kNameIndex);

  return EmitKeyedSloppyArguments(receiver, key, NULL);
}


1122 1123 1124 1125 1126
Handle<Code> KeyedLoadSloppyArgumentsStub::GenerateCode() {
  return DoGenerateCode(this);
}


1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
template <>
HValue* CodeStubGraphBuilder<KeyedStoreSloppyArgumentsStub>::BuildCodeStub() {
  HValue* receiver = GetParameter(StoreDescriptor::kReceiverIndex);
  HValue* key = GetParameter(StoreDescriptor::kNameIndex);
  HValue* value = GetParameter(StoreDescriptor::kValueIndex);

  return EmitKeyedSloppyArguments(receiver, key, value);
}


Handle<Code> KeyedStoreSloppyArgumentsStub::GenerateCode() {
  return DoGenerateCode(this);
}


1142 1143
void CodeStubGraphBuilderBase::BuildStoreNamedField(
    HValue* object, HValue* value, FieldIndex index,
1144
    Representation representation, bool transition_to_field) {
1145 1146 1147 1148 1149 1150 1151 1152
  DCHECK(!index.is_double() || representation.IsDouble());
  int offset = index.offset();
  HObjectAccess access =
      index.is_inobject()
          ? HObjectAccess::ForObservableJSObjectOffset(offset, representation)
          : HObjectAccess::ForBackingStoreOffset(offset, representation);

  if (representation.IsDouble()) {
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    if (!FLAG_unbox_double_fields || !index.is_inobject()) {
      HObjectAccess heap_number_access =
          access.WithRepresentation(Representation::Tagged());
      if (transition_to_field) {
        // The store requires a mutable HeapNumber to be allocated.
        NoObservableSideEffectsScope no_side_effects(this);
        HInstruction* heap_number_size = Add<HConstant>(HeapNumber::kSize);

        // TODO(hpayer): Allocation site pretenuring support.
        HInstruction* heap_number =
            Add<HAllocate>(heap_number_size, HType::HeapObject(), NOT_TENURED,
1164
                           MUTABLE_HEAP_NUMBER_TYPE, graph()->GetConstant0());
1165 1166 1167 1168 1169 1170 1171 1172 1173
        AddStoreMapConstant(heap_number,
                            isolate()->factory()->mutable_heap_number_map());
        Add<HStoreNamedField>(heap_number, HObjectAccess::ForHeapNumberValue(),
                              value);
        // Store the new mutable heap number into the object.
        access = heap_number_access;
        value = heap_number;
      } else {
        // Load the heap number.
1174
        object = Add<HLoadNamedField>(object, nullptr, heap_number_access);
1175 1176 1177
        // Store the double value into it.
        access = HObjectAccess::ForHeapNumberValue();
      }
1178
    }
1179 1180 1181 1182
  } else if (representation.IsHeapObject()) {
    BuildCheckHeapObject(value);
  }

1183
  Add<HStoreNamedField>(object, access, value, INITIALIZING_STORE);
1184 1185 1186 1187 1188 1189
}


template <>
HValue* CodeStubGraphBuilder<StoreFieldStub>::BuildCodeStub() {
  BuildStoreNamedField(GetParameter(0), GetParameter(2), casted_stub()->index(),
1190
                       casted_stub()->representation(), false);
1191 1192 1193 1194 1195 1196 1197
  return GetParameter(2);
}


Handle<Code> StoreFieldStub::GenerateCode() { return DoGenerateCode(this); }


1198
template <>
1199
HValue* CodeStubGraphBuilder<StoreTransitionStub>::BuildCodeStub() {
1200
  HValue* object = GetParameter(StoreTransitionHelper::ReceiverIndex());
1201 1202 1203

  switch (casted_stub()->store_mode()) {
    case StoreTransitionStub::ExtendStorageAndStoreMapAndValue: {
1204 1205
      HValue* properties = Add<HLoadNamedField>(
          object, nullptr, HObjectAccess::ForPropertiesPointer());
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
      HValue* length = AddLoadFixedArrayLength(properties);
      HValue* delta =
          Add<HConstant>(static_cast<int32_t>(JSObject::kFieldsAdded));
      HValue* new_capacity = AddUncasted<HAdd>(length, delta);

      // Grow properties array.
      ElementsKind kind = FAST_ELEMENTS;
      Add<HBoundsCheck>(new_capacity,
                        Add<HConstant>((Page::kMaxRegularHeapObjectSize -
                                        FixedArray::kHeaderSize) >>
                                       ElementsKindToShiftSize(kind)));

      // Reuse this code for properties backing store allocation.
      HValue* new_properties =
          BuildAllocateAndInitializeArray(kind, new_capacity);

      BuildCopyProperties(properties, new_properties, length, new_capacity);

      Add<HStoreNamedField>(object, HObjectAccess::ForPropertiesPointer(),
                            new_properties);
    }
    // Fall through.
    case StoreTransitionStub::StoreMapAndValue:
1229
      // Store the new value into the "extended" object.
1230
      BuildStoreNamedField(
1231
          object, GetParameter(StoreTransitionHelper::ValueIndex()),
1232 1233 1234 1235 1236 1237
          casted_stub()->index(), casted_stub()->representation(), true);
    // Fall through.

    case StoreTransitionStub::StoreMapOnly:
      // And finally update the map.
      Add<HStoreNamedField>(object, HObjectAccess::ForMap(),
1238
                            GetParameter(StoreTransitionHelper::MapIndex()));
1239 1240
      break;
  }
1241
  return GetParameter(StoreTransitionHelper::ValueIndex());
1242 1243 1244
}


1245 1246 1247
Handle<Code> StoreTransitionStub::GenerateCode() {
  return DoGenerateCode(this);
}
1248 1249


1250
template <>
1251
HValue* CodeStubGraphBuilder<StoreFastElementStub>::BuildCodeStub() {
1252
  BuildUncheckedMonomorphicElementAccess(
1253 1254 1255
      GetParameter(StoreDescriptor::kReceiverIndex),
      GetParameter(StoreDescriptor::kNameIndex),
      GetParameter(StoreDescriptor::kValueIndex), casted_stub()->is_js_array(),
1256 1257
      casted_stub()->elements_kind(), STORE, NEVER_RETURN_HOLE,
      casted_stub()->store_mode());
1258 1259 1260 1261 1262

  return GetParameter(2);
}


1263
Handle<Code> StoreFastElementStub::GenerateCode() {
1264
  return DoGenerateCode(this);
1265 1266 1267
}


1268
template <>
1269
HValue* CodeStubGraphBuilder<TransitionElementsKindStub>::BuildCodeStub() {
1270 1271
  info()->MarkAsSavesCallerDoubles();

1272 1273 1274 1275
  BuildTransitionElementsKind(GetParameter(0),
                              GetParameter(1),
                              casted_stub()->from_kind(),
                              casted_stub()->to_kind(),
1276
                              casted_stub()->is_js_array());
1277

1278
  return GetParameter(0);
1279 1280 1281
}


1282 1283
Handle<Code> TransitionElementsKindStub::GenerateCode() {
  return DoGenerateCode(this);
1284 1285
}

1286
HValue* CodeStubGraphBuilderBase::BuildArrayConstructor(
1287
    ElementsKind kind, AllocationSiteOverrideMode override_mode) {
1288
  HValue* constructor = GetParameter(ArrayConstructorStubBase::kConstructor);
1289
  HValue* alloc_site = GetParameter(ArrayConstructorStubBase::kAllocationSite);
1290
  JSArrayBuilder array_builder(this, kind, alloc_site, constructor,
1291
                               override_mode);
1292
  return BuildArrayNArgumentsConstructor(&array_builder, kind);
1293 1294
}

1295
HValue* CodeStubGraphBuilderBase::BuildInternalArrayConstructor(
1296
    ElementsKind kind) {
1297 1298 1299
  HValue* constructor = GetParameter(
      InternalArrayConstructorStubBase::kConstructor);
  JSArrayBuilder array_builder(this, kind, constructor);
1300
  return BuildArrayNArgumentsConstructor(&array_builder, kind);
1301 1302 1303
}


1304 1305
HValue* CodeStubGraphBuilderBase::BuildArraySingleArgumentConstructor(
    JSArrayBuilder* array_builder) {
1306 1307 1308 1309
  // Smi check and range check on the input arg.
  HValue* constant_one = graph()->GetConstant1();
  HValue* constant_zero = graph()->GetConstant0();

1310
  HInstruction* elements = Add<HArgumentsElements>(false);
1311 1312
  HInstruction* argument = Add<HAccessArgumentsAt>(
      elements, constant_one, constant_zero);
1313

1314
  return BuildAllocateArrayFromLength(array_builder, argument);
1315 1316 1317
}


1318 1319
HValue* CodeStubGraphBuilderBase::BuildArrayNArgumentsConstructor(
    JSArrayBuilder* array_builder, ElementsKind kind) {
1320 1321 1322 1323 1324 1325
  // Insert a bounds check because the number of arguments might exceed
  // the kInitialMaxFastElementArray limit. This cannot happen for code
  // that was parsed, but calling via Array.apply(thisArg, [...]) might
  // trigger it.
  HValue* length = GetArgumentsLength();
  HConstant* max_alloc_length =
1326
      Add<HConstant>(JSArray::kInitialMaxFastElementArray);
1327 1328
  HValue* checked_length = Add<HBoundsCheck>(length, max_alloc_length);

1329 1330 1331 1332 1333
  // We need to fill with the hole if it's a smi array in the multi-argument
  // case because we might have to bail out while copying arguments into
  // the array because they aren't compatible with a smi array.
  // If it's a double array, no problem, and if it's fast then no
  // problem either because doubles are boxed.
1334 1335 1336 1337 1338 1339
  //
  // TODO(mvstanton): consider an instruction to memset fill the array
  // with zero in this case instead.
  JSArrayBuilder::FillMode fill_mode = IsFastSmiElementsKind(kind)
      ? JSArrayBuilder::FILL_WITH_HOLE
      : JSArrayBuilder::DONT_FILL_WITH_HOLE;
1340 1341
  HValue* new_object = array_builder->AllocateArray(checked_length,
                                                    checked_length,
1342
                                                    fill_mode);
1343
  HValue* elements = array_builder->GetElementsLocation();
1344
  DCHECK(elements != NULL);
1345 1346 1347 1348 1349 1350

  // Now populate the elements correctly.
  LoopBuilder builder(this,
                      context(),
                      LoopBuilder::kPostIncrement);
  HValue* start = graph()->GetConstant0();
1351
  HValue* key = builder.BeginBody(start, checked_length, Token::LT);
1352
  HInstruction* argument_elements = Add<HArgumentsElements>(false);
1353
  HInstruction* argument = Add<HAccessArgumentsAt>(
1354
      argument_elements, checked_length, key);
1355

1356
  Add<HStoreKeyed>(elements, key, argument, nullptr, kind);
1357 1358
  builder.EndBody();
  return new_object;
1359 1360 1361
}


1362 1363 1364
template <>
HValue* CodeStubGraphBuilder<ArrayNArgumentsConstructorStub>::BuildCodeStub() {
  ElementsKind kind = casted_stub()->elements_kind();
1365
  AllocationSiteOverrideMode override_mode = casted_stub()->override_mode();
1366
  return BuildArrayConstructor(kind, override_mode);
1367 1368 1369
}


1370 1371
Handle<Code> ArrayNArgumentsConstructorStub::GenerateCode() {
  return DoGenerateCode(this);
1372 1373
}

1374

1375 1376 1377 1378
template <>
HValue* CodeStubGraphBuilder<InternalArrayNArgumentsConstructorStub>::
    BuildCodeStub() {
  ElementsKind kind = casted_stub()->elements_kind();
1379
  return BuildInternalArrayConstructor(kind);
1380 1381 1382
}


1383 1384
Handle<Code> InternalArrayNArgumentsConstructorStub::GenerateCode() {
  return DoGenerateCode(this);
1385 1386 1387
}


1388
template <>
1389
HValue* CodeStubGraphBuilder<BinaryOpICStub>::BuildCodeInitializedStub() {
1390
  BinaryOpICState state = casted_stub()->state();
1391

1392 1393 1394
  HValue* left = GetParameter(BinaryOpICStub::kLeft);
  HValue* right = GetParameter(BinaryOpICStub::kRight);

1395 1396 1397
  Type* left_type = state.GetLeftType();
  Type* right_type = state.GetRightType();
  Type* result_type = state.GetResultType();
1398

1399
  DCHECK(!left_type->Is(Type::None()) && !right_type->Is(Type::None()) &&
1400
         (state.HasSideEffects() || !result_type->Is(Type::None())));
1401 1402

  HValue* result = NULL;
1403
  HAllocationMode allocation_mode(NOT_TENURED);
1404
  if (state.op() == Token::ADD &&
1405 1406
      (left_type->Maybe(Type::String()) || right_type->Maybe(Type::String())) &&
      !left_type->Is(Type::String()) && !right_type->Is(Type::String())) {
1407
    // For the generic add stub a fast case for string addition is performance
1408 1409
    // critical.
    if (left_type->Maybe(Type::String())) {
1410 1411 1412 1413
      IfBuilder if_leftisstring(this);
      if_leftisstring.If<HIsStringAndBranch>(left);
      if_leftisstring.Then();
      {
1414 1415 1416
        Push(BuildBinaryOperation(state.op(), left, right, Type::String(),
                                  right_type, result_type,
                                  state.fixed_right_arg(), allocation_mode));
1417 1418 1419
      }
      if_leftisstring.Else();
      {
1420 1421 1422
        Push(BuildBinaryOperation(state.op(), left, right, left_type,
                                  right_type, result_type,
                                  state.fixed_right_arg(), allocation_mode));
1423 1424
      }
      if_leftisstring.End();
1425 1426
      result = Pop();
    } else {
1427 1428 1429 1430
      IfBuilder if_rightisstring(this);
      if_rightisstring.If<HIsStringAndBranch>(right);
      if_rightisstring.Then();
      {
1431 1432 1433
        Push(BuildBinaryOperation(state.op(), left, right, left_type,
                                  Type::String(), result_type,
                                  state.fixed_right_arg(), allocation_mode));
1434 1435 1436
      }
      if_rightisstring.Else();
      {
1437 1438 1439
        Push(BuildBinaryOperation(state.op(), left, right, left_type,
                                  right_type, result_type,
                                  state.fixed_right_arg(), allocation_mode));
1440 1441
      }
      if_rightisstring.End();
1442 1443 1444
      result = Pop();
    }
  } else {
1445 1446 1447
    result = BuildBinaryOperation(state.op(), left, right, left_type,
                                  right_type, result_type,
                                  state.fixed_right_arg(), allocation_mode);
1448 1449 1450 1451
  }

  // If we encounter a generic argument, the number conversion is
  // observable, thus we cannot afford to bail out after the fact.
1452
  if (!state.HasSideEffects()) {
1453 1454 1455 1456 1457 1458 1459
    result = EnforceNumberType(result, result_type);
  }

  return result;
}


1460 1461
Handle<Code> BinaryOpICStub::GenerateCode() {
  return DoGenerateCode(this);
1462 1463 1464
}


1465 1466
template <>
HValue* CodeStubGraphBuilder<BinaryOpWithAllocationSiteStub>::BuildCodeStub() {
1467
  BinaryOpICState state = casted_stub()->state();
1468 1469 1470 1471 1472 1473

  HValue* allocation_site = GetParameter(
      BinaryOpWithAllocationSiteStub::kAllocationSite);
  HValue* left = GetParameter(BinaryOpWithAllocationSiteStub::kLeft);
  HValue* right = GetParameter(BinaryOpWithAllocationSiteStub::kRight);

1474 1475 1476
  Type* left_type = state.GetLeftType();
  Type* right_type = state.GetRightType();
  Type* result_type = state.GetResultType();
1477 1478
  HAllocationMode allocation_mode(allocation_site);

1479 1480
  return BuildBinaryOperation(state.op(), left, right, left_type, right_type,
                              result_type, state.fixed_right_arg(),
1481
                              allocation_mode);
1482 1483 1484
}


1485 1486
Handle<Code> BinaryOpWithAllocationSiteStub::GenerateCode() {
  return DoGenerateCode(this);
1487 1488 1489
}


1490
HValue* CodeStubGraphBuilderBase::BuildToString(HValue* input, bool convert) {
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
  if (!convert) return BuildCheckString(input);
  IfBuilder if_inputissmi(this);
  HValue* inputissmi = if_inputissmi.If<HIsSmiAndBranch>(input);
  if_inputissmi.Then();
  {
    // Convert the input smi to a string.
    Push(BuildNumberToString(input, Type::SignedSmall()));
  }
  if_inputissmi.Else();
  {
    HValue* input_map =
        Add<HLoadNamedField>(input, inputissmi, HObjectAccess::ForMap());
    HValue* input_instance_type = Add<HLoadNamedField>(
        input_map, inputissmi, HObjectAccess::ForMapInstanceType());
    IfBuilder if_inputisstring(this);
    if_inputisstring.If<HCompareNumericAndBranch>(
        input_instance_type, Add<HConstant>(FIRST_NONSTRING_TYPE), Token::LT);
    if_inputisstring.Then();
    {
      // The input is already a string.
      Push(input);
    }
    if_inputisstring.Else();
    {
      // Convert to primitive first (if necessary), see
      // ES6 section 12.7.3 The Addition operator.
      IfBuilder if_inputisprimitive(this);
      STATIC_ASSERT(FIRST_PRIMITIVE_TYPE == FIRST_TYPE);
      if_inputisprimitive.If<HCompareNumericAndBranch>(
          input_instance_type, Add<HConstant>(LAST_PRIMITIVE_TYPE), Token::LTE);
      if_inputisprimitive.Then();
      {
        // The input is already a primitive.
        Push(input);
      }
      if_inputisprimitive.Else();
      {
1528 1529
        // Convert the input to a primitive.
        Push(BuildToPrimitive(input, input_map));
1530 1531 1532 1533 1534
      }
      if_inputisprimitive.End();
      // Convert the primitive to a string value.
      ToStringStub stub(isolate());
      HValue* values[] = {context(), Pop()};
1535 1536 1537
      Push(AddUncasted<HCallWithDescriptor>(Add<HConstant>(stub.GetCode()), 0,
                                            stub.GetCallInterfaceDescriptor(),
                                            ArrayVector(values)));
1538 1539 1540 1541 1542 1543 1544 1545
    }
    if_inputisstring.End();
  }
  if_inputissmi.End();
  return Pop();
}


1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
HValue* CodeStubGraphBuilderBase::BuildToPrimitive(HValue* input,
                                                   HValue* input_map) {
  // Get the native context of the caller.
  HValue* native_context = BuildGetNativeContext();

  // Determine the initial map of the %ObjectPrototype%.
  HValue* object_function_prototype_map =
      Add<HLoadNamedField>(native_context, nullptr,
                           HObjectAccess::ForContextSlot(
                               Context::OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX));

  // Determine the initial map of the %StringPrototype%.
  HValue* string_function_prototype_map =
      Add<HLoadNamedField>(native_context, nullptr,
                           HObjectAccess::ForContextSlot(
                               Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));

  // Determine the initial map of the String function.
  HValue* string_function = Add<HLoadNamedField>(
      native_context, nullptr,
      HObjectAccess::ForContextSlot(Context::STRING_FUNCTION_INDEX));
  HValue* string_function_initial_map = Add<HLoadNamedField>(
      string_function, nullptr, HObjectAccess::ForPrototypeOrInitialMap());

  // Determine the map of the [[Prototype]] of {input}.
  HValue* input_prototype =
      Add<HLoadNamedField>(input_map, nullptr, HObjectAccess::ForPrototype());
  HValue* input_prototype_map =
      Add<HLoadNamedField>(input_prototype, nullptr, HObjectAccess::ForMap());

  // For string wrappers (JSValue instances with [[StringData]] internal
  // fields), we can shortcirciut the ToPrimitive if
  //
  //  (a) the {input} map matches the initial map of the String function,
  //  (b) the {input} [[Prototype]] is the unmodified %StringPrototype% (i.e.
  //      no one monkey-patched toString, @@toPrimitive or valueOf), and
  //  (c) the %ObjectPrototype% (i.e. the [[Prototype]] of the
  //      %StringPrototype%) is also unmodified, that is no one sneaked a
  //      @@toPrimitive into the %ObjectPrototype%.
  //
  // If all these assumptions hold, we can just take the [[StringData]] value
  // and return it.
  // TODO(bmeurer): This just repairs a regression introduced by removing the
  // weird (and broken) intrinsic %_IsStringWrapperSafeForDefaultValue, which
  // was intendend to something similar to this, although less efficient and
  // wrong in the presence of @@toPrimitive. Long-term we might want to move
  // into the direction of having a ToPrimitiveStub that can do common cases
  // while staying in JavaScript land (i.e. not going to C++).
  IfBuilder if_inputisstringwrapper(this);
  if_inputisstringwrapper.If<HCompareObjectEqAndBranch>(
      input_map, string_function_initial_map);
  if_inputisstringwrapper.And();
  if_inputisstringwrapper.If<HCompareObjectEqAndBranch>(
      input_prototype_map, string_function_prototype_map);
  if_inputisstringwrapper.And();
  if_inputisstringwrapper.If<HCompareObjectEqAndBranch>(
      Add<HLoadNamedField>(Add<HLoadNamedField>(input_prototype_map, nullptr,
                                                HObjectAccess::ForPrototype()),
                           nullptr, HObjectAccess::ForMap()),
      object_function_prototype_map);
  if_inputisstringwrapper.Then();
  {
    Push(BuildLoadNamedField(
        input, FieldIndex::ForInObjectOffset(JSValue::kValueOffset)));
  }
  if_inputisstringwrapper.Else();
  {
    // TODO(bmeurer): Add support for fast ToPrimitive conversion using
    // a dedicated ToPrimitiveStub.
    Add<HPushArguments>(input);
    Push(Add<HCallRuntime>(Runtime::FunctionForId(Runtime::kToPrimitive), 1));
  }
  if_inputisstringwrapper.End();
  return Pop();
}


1623
template <>
1624 1625
HValue* CodeStubGraphBuilder<StringAddStub>::BuildCodeInitializedStub() {
  StringAddStub* stub = casted_stub();
1626 1627 1628
  StringAddFlags flags = stub->flags();
  PretenureFlag pretenure_flag = stub->pretenure_flag();

1629 1630
  HValue* left = GetParameter(StringAddStub::kLeft);
  HValue* right = GetParameter(StringAddStub::kRight);
1631 1632 1633

  // Make sure that both arguments are strings if not known in advance.
  if ((flags & STRING_ADD_CHECK_LEFT) == STRING_ADD_CHECK_LEFT) {
1634
    left =
1635
        BuildToString(left, (flags & STRING_ADD_CONVERT) == STRING_ADD_CONVERT);
1636 1637
  }
  if ((flags & STRING_ADD_CHECK_RIGHT) == STRING_ADD_CHECK_RIGHT) {
1638 1639
    right = BuildToString(right,
                          (flags & STRING_ADD_CONVERT) == STRING_ADD_CONVERT);
1640 1641
  }

1642
  return BuildStringAdd(left, right, HAllocationMode(pretenure_flag));
1643 1644 1645
}


1646 1647
Handle<Code> StringAddStub::GenerateCode() {
  return DoGenerateCode(this);
1648 1649
}

1650
template <>
1651 1652
HValue* CodeStubGraphBuilder<ToBooleanICStub>::BuildCodeInitializedStub() {
  ToBooleanICStub* stub = casted_stub();
1653
  IfBuilder if_true(this);
1654
  if_true.If<HBranch>(GetParameter(0), stub->types());
1655
  if_true.Then();
1656
  if_true.Return(graph()->GetConstantTrue());
1657 1658
  if_true.Else();
  if_true.End();
1659
  return graph()->GetConstantFalse();
1660 1661
}

1662
Handle<Code> ToBooleanICStub::GenerateCode() { return DoGenerateCode(this); }
1663

1664 1665 1666
template <>
HValue* CodeStubGraphBuilder<StoreGlobalStub>::BuildCodeInitializedStub() {
  StoreGlobalStub* stub = casted_stub();
1667
  HParameter* value = GetParameter(StoreDescriptor::kValueIndex);
1668 1669 1670
  if (stub->check_global()) {
    // Check that the map of the global has not changed: use a placeholder map
    // that will be replaced later with the global object's map.
1671 1672 1673 1674 1675
    HParameter* proxy = GetParameter(StoreDescriptor::kReceiverIndex);
    HValue* proxy_map =
        Add<HLoadNamedField>(proxy, nullptr, HObjectAccess::ForMap());
    HValue* global =
        Add<HLoadNamedField>(proxy_map, nullptr, HObjectAccess::ForPrototype());
1676 1677 1678 1679
    HValue* map_cell = Add<HConstant>(isolate()->factory()->NewWeakCell(
        StoreGlobalStub::global_map_placeholder(isolate())));
    HValue* expected_map = Add<HLoadNamedField>(
        map_cell, nullptr, HObjectAccess::ForWeakCellValue());
1680 1681 1682 1683
    HValue* map =
        Add<HLoadNamedField>(global, nullptr, HObjectAccess::ForMap());
    IfBuilder map_check(this);
    map_check.IfNot<HCompareObjectEqAndBranch>(expected_map, map);
1684
    map_check.ThenDeopt(Deoptimizer::kUnknownMap);
1685
    map_check.End();
1686
  }
1687

1688 1689 1690 1691
  HValue* weak_cell = Add<HConstant>(isolate()->factory()->NewWeakCell(
      StoreGlobalStub::property_cell_placeholder(isolate())));
  HValue* cell = Add<HLoadNamedField>(weak_cell, nullptr,
                                      HObjectAccess::ForWeakCellValue());
1692
  Add<HCheckHeapObject>(cell);
1693
  HObjectAccess access = HObjectAccess::ForPropertyCellValue();
1694 1695 1696
  // Load the payload of the global parameter cell. A hole indicates that the
  // cell has been invalidated and that the store must be handled by the
  // runtime.
1697
  HValue* cell_contents = Add<HLoadNamedField>(cell, nullptr, access);
1698

1699 1700 1701 1702
  auto cell_type = stub->cell_type();
  if (cell_type == PropertyCellType::kConstant ||
      cell_type == PropertyCellType::kUndefined) {
    // This is always valid for all states a cell can be in.
1703 1704 1705
    IfBuilder builder(this);
    builder.If<HCompareObjectEqAndBranch>(cell_contents, value);
    builder.Then();
1706 1707
    builder.ElseDeopt(
        Deoptimizer::kUnexpectedCellContentsInConstantGlobalStore);
1708 1709
    builder.End();
  } else {
1710
    IfBuilder builder(this);
1711
    HValue* hole_value = graph()->GetConstantHole();
1712 1713
    builder.If<HCompareObjectEqAndBranch>(cell_contents, hole_value);
    builder.Then();
1714
    builder.Deopt(Deoptimizer::kUnexpectedCellContentsInGlobalStore);
1715
    builder.Else();
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    // When dealing with constant types, the type may be allowed to change, as
    // long as optimized code remains valid.
    if (cell_type == PropertyCellType::kConstantType) {
      switch (stub->constant_type()) {
        case PropertyCellConstantType::kSmi:
          access = access.WithRepresentation(Representation::Smi());
          break;
        case PropertyCellConstantType::kStableMap: {
          // It is sufficient here to check that the value and cell contents
          // have identical maps, no matter if they are stable or not or if they
          // are the maps that were originally in the cell or not. If optimized
          // code will deopt when a cell has a unstable map and if it has a
          // dependency on a stable map, it will deopt if the map destabilizes.
          Add<HCheckHeapObject>(value);
          Add<HCheckHeapObject>(cell_contents);
          HValue* expected_map = Add<HLoadNamedField>(cell_contents, nullptr,
                                                      HObjectAccess::ForMap());
          HValue* map =
              Add<HLoadNamedField>(value, nullptr, HObjectAccess::ForMap());
          IfBuilder map_check(this);
          map_check.IfNot<HCompareObjectEqAndBranch>(expected_map, map);
          map_check.ThenDeopt(Deoptimizer::kUnknownMap);
          map_check.End();
          access = access.WithRepresentation(Representation::HeapObject());
          break;
        }
      }
    }
verwaest's avatar
verwaest committed
1744
    Add<HStoreNamedField>(cell, access, value);
1745 1746
    builder.End();
  }
1747

1748 1749 1750 1751
  return value;
}


1752
Handle<Code> StoreGlobalStub::GenerateCode() {
1753 1754 1755 1756 1757
  return DoGenerateCode(this);
}


template <>
1758
HValue* CodeStubGraphBuilder<ElementsTransitionAndStoreStub>::BuildCodeStub() {
1759 1760 1761 1762
  HValue* object = GetParameter(StoreTransitionHelper::ReceiverIndex());
  HValue* key = GetParameter(StoreTransitionHelper::NameIndex());
  HValue* value = GetParameter(StoreTransitionHelper::ValueIndex());
  HValue* map = GetParameter(StoreTransitionHelper::MapIndex());
1763 1764 1765

  if (FLAG_trace_elements_transitions) {
    // Tracing elements transitions is the job of the runtime.
1766 1767
    Add<HDeoptimize>(Deoptimizer::kTracingElementsTransitions,
                     Deoptimizer::EAGER);
1768 1769 1770 1771 1772 1773 1774 1775
  } else {
    info()->MarkAsSavesCallerDoubles();

    BuildTransitionElementsKind(object, map,
                                casted_stub()->from_kind(),
                                casted_stub()->to_kind(),
                                casted_stub()->is_jsarray());

1776 1777 1778
    BuildUncheckedMonomorphicElementAccess(object, key, value,
                                           casted_stub()->is_jsarray(),
                                           casted_stub()->to_kind(),
1779
                                           STORE, ALLOW_RETURN_HOLE,
1780
                                           casted_stub()->store_mode());
1781 1782 1783 1784 1785 1786
  }

  return value;
}


1787 1788
Handle<Code> ElementsTransitionAndStoreStub::GenerateCode() {
  return DoGenerateCode(this);
1789 1790 1791
}


1792 1793
template <>
HValue* CodeStubGraphBuilder<ToObjectStub>::BuildCodeStub() {
1794
  HValue* receiver = GetParameter(TypeConversionDescriptor::kArgumentIndex);
1795 1796 1797 1798 1799 1800 1801
  return BuildToObject(receiver);
}


Handle<Code> ToObjectStub::GenerateCode() { return DoGenerateCode(this); }


1802 1803 1804 1805 1806 1807
template<>
HValue* CodeStubGraphBuilder<FastNewClosureStub>::BuildCodeStub() {
  Counters* counters = isolate()->counters();
  Factory* factory = isolate()->factory();
  HInstruction* empty_fixed_array =
      Add<HConstant>(factory->empty_fixed_array());
1808 1809
  HInstruction* empty_literals_array =
      Add<HConstant>(factory->empty_literals_array());
1810 1811
  HValue* shared_info = GetParameter(0);

1812 1813
  AddIncrementCounter(counters->fast_new_closure_total());

1814 1815 1816
  // Create a new closure from the given function info in new space
  HValue* size = Add<HConstant>(JSFunction::kSize);
  HInstruction* js_function =
1817 1818
      Add<HAllocate>(size, HType::JSObject(), NOT_TENURED, JS_FUNCTION_TYPE,
                     graph()->GetConstant0());
1819

1820
  int map_index = Context::FunctionMapIndex(casted_stub()->language_mode(),
1821 1822 1823 1824 1825
                                            casted_stub()->kind());

  // Compute the function map in the current native context and set that
  // as the map of the allocated object.
  HInstruction* native_context = BuildGetNativeContext();
1826 1827
  HInstruction* map_slot_value = Add<HLoadNamedField>(
      native_context, nullptr, HObjectAccess::ForContextSlot(map_index));
1828 1829 1830 1831 1832 1833 1834 1835
  Add<HStoreNamedField>(js_function, HObjectAccess::ForMap(), map_slot_value);

  // Initialize the rest of the function.
  Add<HStoreNamedField>(js_function, HObjectAccess::ForPropertiesPointer(),
                        empty_fixed_array);
  Add<HStoreNamedField>(js_function, HObjectAccess::ForElementsPointer(),
                        empty_fixed_array);
  Add<HStoreNamedField>(js_function, HObjectAccess::ForLiteralsPointer(),
1836
                        empty_literals_array);
1837 1838 1839 1840 1841 1842
  Add<HStoreNamedField>(js_function, HObjectAccess::ForPrototypeOrInitialMap(),
                        graph()->GetConstantHole());
  Add<HStoreNamedField>(
      js_function, HObjectAccess::ForSharedFunctionInfoPointer(), shared_info);
  Add<HStoreNamedField>(js_function, HObjectAccess::ForFunctionContextPointer(),
                        context());
1843

1844 1845 1846 1847 1848 1849 1850
  Handle<Code> lazy_builtin(
      isolate()->builtins()->builtin(Builtins::kCompileLazy));
  HConstant* lazy = Add<HConstant>(lazy_builtin);
  Add<HStoreCodeEntry>(js_function, lazy);
  Add<HStoreNamedField>(js_function,
                        HObjectAccess::ForNextFunctionLinkPointer(),
                        graph()->GetConstantUndefined());
1851

1852
  return js_function;
1853 1854 1855
}


1856 1857
Handle<Code> FastNewClosureStub::GenerateCode() {
  return DoGenerateCode(this);
1858 1859 1860
}


1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
template<>
HValue* CodeStubGraphBuilder<FastNewContextStub>::BuildCodeStub() {
  int length = casted_stub()->slots() + Context::MIN_CONTEXT_SLOTS;

  // Get the function.
  HParameter* function = GetParameter(FastNewContextStub::kFunction);

  // Allocate the context in new space.
  HAllocate* function_context = Add<HAllocate>(
      Add<HConstant>(length * kPointerSize + FixedArray::kHeaderSize),
1871 1872
      HType::HeapObject(), NOT_TENURED, FIXED_ARRAY_TYPE,
      graph()->GetConstant0());
1873 1874 1875 1876 1877 1878

  // Set up the object header.
  AddStoreMapConstant(function_context,
                      isolate()->factory()->function_context_map());
  Add<HStoreNamedField>(function_context,
                        HObjectAccess::ForFixedArrayLength(),
1879
                        Add<HConstant>(length));
1880 1881 1882 1883

  // Set up the fixed slots.
  Add<HStoreNamedField>(function_context,
                        HObjectAccess::ForContextSlot(Context::CLOSURE_INDEX),
1884
                        function);
1885 1886
  Add<HStoreNamedField>(function_context,
                        HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX),
1887
                        context());
1888 1889
  Add<HStoreNamedField>(function_context,
                        HObjectAccess::ForContextSlot(Context::EXTENSION_INDEX),
1890
                        graph()->GetConstantHole());
1891

1892 1893
  // Copy the native context from the previous context.
  HValue* native_context = Add<HLoadNamedField>(
1894
      context(), nullptr,
1895 1896 1897 1898
      HObjectAccess::ForContextSlot(Context::NATIVE_CONTEXT_INDEX));
  Add<HStoreNamedField>(function_context, HObjectAccess::ForContextSlot(
                                              Context::NATIVE_CONTEXT_INDEX),
                        native_context);
1899 1900 1901 1902 1903

  // Initialize the rest of the slots to undefined.
  for (int i = Context::MIN_CONTEXT_SLOTS; i < length; ++i) {
    Add<HStoreNamedField>(function_context,
                          HObjectAccess::ForContextSlot(i),
1904
                          graph()->GetConstantUndefined());
1905 1906 1907 1908 1909 1910
  }

  return function_context;
}


1911 1912
Handle<Code> FastNewContextStub::GenerateCode() {
  return DoGenerateCode(this);
1913 1914 1915
}


1916 1917
template <>
HValue* CodeStubGraphBuilder<LoadDictionaryElementStub>::BuildCodeStub() {
1918 1919
  HValue* receiver = GetParameter(LoadDescriptor::kReceiverIndex);
  HValue* key = GetParameter(LoadDescriptor::kNameIndex);
1920 1921 1922

  Add<HCheckSmi>(key);

1923 1924 1925 1926
  HValue* elements = AddLoadElements(receiver);

  HValue* hash = BuildElementIndexHash(key);

1927
  return BuildUncheckedDictionaryElementLoad(receiver, elements, key, hash);
1928 1929 1930
}


1931
Handle<Code> LoadDictionaryElementStub::GenerateCode() {
1932
  return DoGenerateCode(this);
1933 1934 1935
}


1936 1937 1938 1939 1940 1941 1942
template<>
HValue* CodeStubGraphBuilder<RegExpConstructResultStub>::BuildCodeStub() {
  // Determine the parameters.
  HValue* length = GetParameter(RegExpConstructResultStub::kLength);
  HValue* index = GetParameter(RegExpConstructResultStub::kIndex);
  HValue* input = GetParameter(RegExpConstructResultStub::kInput);

1943 1944
  info()->MarkMustNotHaveEagerFrame();

1945 1946 1947 1948
  return BuildRegExpConstructResult(length, index, input);
}


1949 1950
Handle<Code> RegExpConstructResultStub::GenerateCode() {
  return DoGenerateCode(this);
1951 1952 1953
}


1954
template <>
1955 1956
class CodeStubGraphBuilder<KeyedLoadGenericStub>
    : public CodeStubGraphBuilderBase {
1957
 public:
1958 1959
  explicit CodeStubGraphBuilder(CompilationInfo* info, CodeStub* stub)
      : CodeStubGraphBuilderBase(info, stub) {}
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

 protected:
  virtual HValue* BuildCodeStub();

  void BuildElementsKindLimitCheck(HGraphBuilder::IfBuilder* if_builder,
                                   HValue* bit_field2,
                                   ElementsKind kind);

  void BuildFastElementLoad(HGraphBuilder::IfBuilder* if_builder,
                            HValue* receiver,
                            HValue* key,
                            HValue* instance_type,
                            HValue* bit_field2,
                            ElementsKind kind);

1975 1976
  KeyedLoadGenericStub* casted_stub() {
    return static_cast<KeyedLoadGenericStub*>(stub());
1977 1978 1979 1980
  }
};


1981 1982
void CodeStubGraphBuilder<KeyedLoadGenericStub>::BuildElementsKindLimitCheck(
    HGraphBuilder::IfBuilder* if_builder, HValue* bit_field2,
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
    ElementsKind kind) {
  ElementsKind next_kind = static_cast<ElementsKind>(kind + 1);
  HValue* kind_limit = Add<HConstant>(
      static_cast<int>(Map::ElementsKindBits::encode(next_kind)));

  if_builder->If<HCompareNumericAndBranch>(bit_field2, kind_limit, Token::LT);
  if_builder->Then();
}


1993 1994 1995
void CodeStubGraphBuilder<KeyedLoadGenericStub>::BuildFastElementLoad(
    HGraphBuilder::IfBuilder* if_builder, HValue* receiver, HValue* key,
    HValue* instance_type, HValue* bit_field2, ElementsKind kind) {
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
  BuildElementsKindLimitCheck(if_builder, bit_field2, kind);

  IfBuilder js_array_check(this);
  js_array_check.If<HCompareNumericAndBranch>(
      instance_type, Add<HConstant>(JS_ARRAY_TYPE), Token::EQ);
  js_array_check.Then();
  Push(BuildUncheckedMonomorphicElementAccess(receiver, key, NULL,
                                              true, kind,
                                              LOAD, NEVER_RETURN_HOLE,
                                              STANDARD_STORE));
  js_array_check.Else();
  Push(BuildUncheckedMonomorphicElementAccess(receiver, key, NULL,
                                              false, kind,
                                              LOAD, NEVER_RETURN_HOLE,
                                              STANDARD_STORE));
  js_array_check.End();
}


2015
HValue* CodeStubGraphBuilder<KeyedLoadGenericStub>::BuildCodeStub() {
2016 2017
  HValue* receiver = GetParameter(LoadDescriptor::kReceiverIndex);
  HValue* key = GetParameter(LoadDescriptor::kNameIndex);
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
  // Split into a smi/integer case and unique string case.
  HIfContinuation index_name_split_continuation(graph()->CreateBasicBlock(),
                                                graph()->CreateBasicBlock());

  BuildKeyedIndexCheck(key, &index_name_split_continuation);

  IfBuilder index_name_split(this, &index_name_split_continuation);
  index_name_split.Then();
  {
    // Key is an index (number)
    key = Pop();

    int bit_field_mask = (1 << Map::kIsAccessCheckNeeded) |
      (1 << Map::kHasIndexedInterceptor);
    BuildJSObjectCheck(receiver, bit_field_mask);

2034 2035
    HValue* map =
        Add<HLoadNamedField>(receiver, nullptr, HObjectAccess::ForMap());
2036 2037

    HValue* instance_type =
2038
        Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapInstanceType());
2039

2040 2041
    HValue* bit_field2 =
        Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapBitField2());
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060

    IfBuilder kind_if(this);
    BuildFastElementLoad(&kind_if, receiver, key, instance_type, bit_field2,
                         FAST_HOLEY_ELEMENTS);

    kind_if.Else();
    {
      BuildFastElementLoad(&kind_if, receiver, key, instance_type, bit_field2,
                           FAST_HOLEY_DOUBLE_ELEMENTS);
    }
    kind_if.Else();

    // The DICTIONARY_ELEMENTS check generates a "kind_if.Then"
    BuildElementsKindLimitCheck(&kind_if, bit_field2, DICTIONARY_ELEMENTS);
    {
      HValue* elements = AddLoadElements(receiver);

      HValue* hash = BuildElementIndexHash(key);

2061
      Push(BuildUncheckedDictionaryElementLoad(receiver, elements, key, hash));
2062 2063 2064
    }
    kind_if.Else();

2065 2066 2067
    // The SLOW_SLOPPY_ARGUMENTS_ELEMENTS check generates a "kind_if.Then"
    STATIC_ASSERT(FAST_SLOPPY_ARGUMENTS_ELEMENTS <
                  SLOW_SLOPPY_ARGUMENTS_ELEMENTS);
2068
    BuildElementsKindLimitCheck(&kind_if, bit_field2,
2069
                                SLOW_SLOPPY_ARGUMENTS_ELEMENTS);
2070
    // Non-strict elements are not handled.
2071
    Add<HDeoptimize>(Deoptimizer::kNonStrictElementsInKeyedLoadGenericStub,
2072 2073 2074
                     Deoptimizer::EAGER);
    Push(graph()->GetConstant0());

2075 2076
    kind_if.ElseDeopt(
        Deoptimizer::kElementsKindUnhandledInKeyedLoadGenericStub);
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097

    kind_if.End();
  }
  index_name_split.Else();
  {
    // Key is a unique string.
    key = Pop();

    int bit_field_mask = (1 << Map::kIsAccessCheckNeeded) |
        (1 << Map::kHasNamedInterceptor);
    BuildJSObjectCheck(receiver, bit_field_mask);

    HIfContinuation continuation;
    BuildTestForDictionaryProperties(receiver, &continuation);
    IfBuilder if_dict_properties(this, &continuation);
    if_dict_properties.Then();
    {
      //  Key is string, properties are dictionary mode
      BuildNonGlobalObjectCheck(receiver);

      HValue* properties = Add<HLoadNamedField>(
2098
          receiver, nullptr, HObjectAccess::ForPropertiesPointer());
2099 2100

      HValue* hash =
2101
          Add<HLoadNamedField>(key, nullptr, HObjectAccess::ForNameHashField());
2102

2103 2104
      hash = AddUncasted<HShr>(hash, Add<HConstant>(Name::kHashShift));

2105 2106
      HValue* value =
          BuildUncheckedDictionaryElementLoad(receiver, properties, key, hash);
2107 2108 2109 2110
      Push(value);
    }
    if_dict_properties.Else();
    {
2111 2112 2113
      // TODO(dcarney): don't use keyed lookup cache, but convert to use
      // megamorphic stub cache.
      UNREACHABLE();
2114 2115 2116 2117 2118 2119 2120
      //  Key is string, properties are fast mode
      HValue* hash = BuildKeyedLookupCacheHash(receiver, key);

      ExternalReference cache_keys_ref =
          ExternalReference::keyed_lookup_cache_keys(isolate());
      HValue* cache_keys = Add<HConstant>(cache_keys_ref);

2121 2122
      HValue* map =
          Add<HLoadNamedField>(receiver, nullptr, HObjectAccess::ForMap());
2123 2124 2125
      HValue* base_index = AddUncasted<HMul>(hash, Add<HConstant>(2));
      base_index->ClearFlag(HValue::kCanOverflow);

2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
      HIfContinuation inline_or_runtime_continuation(
          graph()->CreateBasicBlock(), graph()->CreateBasicBlock());
      {
        IfBuilder lookup_ifs[KeyedLookupCache::kEntriesPerBucket];
        for (int probe = 0; probe < KeyedLookupCache::kEntriesPerBucket;
             ++probe) {
          IfBuilder* lookup_if = &lookup_ifs[probe];
          lookup_if->Initialize(this);
          int probe_base = probe * KeyedLookupCache::kEntryLength;
          HValue* map_index = AddUncasted<HAdd>(
              base_index,
              Add<HConstant>(probe_base + KeyedLookupCache::kMapIndex));
          map_index->ClearFlag(HValue::kCanOverflow);
          HValue* key_index = AddUncasted<HAdd>(
              base_index,
              Add<HConstant>(probe_base + KeyedLookupCache::kKeyIndex));
          key_index->ClearFlag(HValue::kCanOverflow);
          HValue* map_to_check =
2144 2145
              Add<HLoadKeyed>(cache_keys, map_index, nullptr, nullptr,
                              FAST_ELEMENTS, NEVER_RETURN_HOLE, 0);
2146 2147 2148
          lookup_if->If<HCompareObjectEqAndBranch>(map_to_check, map);
          lookup_if->And();
          HValue* key_to_check =
2149 2150
              Add<HLoadKeyed>(cache_keys, key_index, nullptr, nullptr,
                              FAST_ELEMENTS, NEVER_RETURN_HOLE, 0);
2151 2152 2153 2154 2155 2156 2157 2158 2159
          lookup_if->If<HCompareObjectEqAndBranch>(key_to_check, key);
          lookup_if->Then();
          {
            ExternalReference cache_field_offsets_ref =
                ExternalReference::keyed_lookup_cache_field_offsets(isolate());
            HValue* cache_field_offsets =
                Add<HConstant>(cache_field_offsets_ref);
            HValue* index = AddUncasted<HAdd>(hash, Add<HConstant>(probe));
            index->ClearFlag(HValue::kCanOverflow);
2160
            HValue* property_index =
2161
                Add<HLoadKeyed>(cache_field_offsets, index, nullptr, cache_keys,
2162
                                INT32_ELEMENTS, NEVER_RETURN_HOLE, 0);
2163 2164 2165 2166 2167 2168
            Push(property_index);
          }
          lookup_if->Else();
        }
        for (int i = 0; i < KeyedLookupCache::kEntriesPerBucket; ++i) {
          lookup_ifs[i].JoinContinuation(&inline_or_runtime_continuation);
2169 2170
        }
      }
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180

      IfBuilder inline_or_runtime(this, &inline_or_runtime_continuation);
      inline_or_runtime.Then();
      {
        // Found a cached index, load property inline.
        Push(Add<HLoadFieldByIndex>(receiver, Pop()));
      }
      inline_or_runtime.Else();
      {
        // KeyedLookupCache miss; call runtime.
2181
        Add<HPushArguments>(receiver, key);
2182
        Push(Add<HCallRuntime>(
2183
            Runtime::FunctionForId(Runtime::kKeyedGetProperty), 2));
2184 2185
      }
      inline_or_runtime.End();
2186 2187 2188 2189 2190 2191 2192 2193 2194
    }
    if_dict_properties.End();
  }
  index_name_split.End();

  return Pop();
}


2195
Handle<Code> KeyedLoadGenericStub::GenerateCode() {
2196 2197 2198
  return DoGenerateCode(this);
}

2199 2200
}  // namespace internal
}  // namespace v8