lithium-x64.cc 87.6 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/v8.h"
6

7
#if V8_TARGET_ARCH_X64
8

9
#include "src/hydrogen-osr.h"
10 11
#include "src/lithium-allocator-inl.h"
#include "src/x64/lithium-codegen-x64.h"
12
#include "src/x64/lithium-x64.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18 19 20 21 22 23
#define DEFINE_COMPILE(type)                            \
  void L##type::CompileToNative(LCodeGen* generator) {  \
    generator->Do##type(this);                          \
  }
LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
#undef DEFINE_COMPILE

24

25 26
#ifdef DEBUG
void LInstruction::VerifyCall() {
27 28 29 30
  // Call instructions can use only fixed registers as temporaries and
  // outputs because all registers are blocked by the calling convention.
  // Inputs operands must use a fixed register or use-at-start policy or
  // a non-register policy.
31 32 33
  ASSERT(Output() == NULL ||
         LUnallocated::cast(Output())->HasFixedPolicy() ||
         !LUnallocated::cast(Output())->HasRegisterPolicy());
34 35
  for (UseIterator it(this); !it.Done(); it.Advance()) {
    LUnallocated* operand = LUnallocated::cast(it.Current());
36 37
    ASSERT(operand->HasFixedPolicy() ||
           operand->IsUsedAtStart());
38
  }
39 40
  for (TempIterator it(this); !it.Done(); it.Advance()) {
    LUnallocated* operand = LUnallocated::cast(it.Current());
41
    ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
42 43 44 45 46
  }
}
#endif


47 48
void LInstruction::PrintTo(StringStream* stream) {
  stream->Add("%s ", this->Mnemonic());
49 50

  PrintOutputOperandTo(stream);
51

52 53 54 55
  PrintDataTo(stream);

  if (HasEnvironment()) {
    stream->Add(" ");
56
    environment()->PrintTo(stream);
57 58 59 60
  }

  if (HasPointerMap()) {
    stream->Add(" ");
61 62 63 64 65
    pointer_map()->PrintTo(stream);
  }
}


66
void LInstruction::PrintDataTo(StringStream* stream) {
67
  stream->Add("= ");
68
  for (int i = 0; i < InputCount(); i++) {
69
    if (i > 0) stream->Add(" ");
70 71 72 73 74
    if (InputAt(i) == NULL) {
      stream->Add("NULL");
    } else {
      InputAt(i)->PrintTo(stream);
    }
75
  }
76 77 78
}


79 80
void LInstruction::PrintOutputOperandTo(StringStream* stream) {
  if (HasResult()) result()->PrintTo(stream);
81 82 83 84
}


void LLabel::PrintDataTo(StringStream* stream) {
fschneider@chromium.org's avatar
fschneider@chromium.org committed
85
  LGap::PrintDataTo(stream);
86 87 88 89 90 91 92
  LLabel* rep = replacement();
  if (rep != NULL) {
    stream->Add(" Dead block replaced with B%d", rep->block_id());
  }
}


93 94 95 96 97 98
bool LGap::IsRedundant() const {
  for (int i = 0; i < 4; i++) {
    if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
      return false;
    }
  }
fschneider@chromium.org's avatar
fschneider@chromium.org committed
99

100 101 102 103
  return true;
}


104
void LGap::PrintDataTo(StringStream* stream) {
105 106 107 108 109 110 111 112 113 114
  for (int i = 0; i < 4; i++) {
    stream->Add("(");
    if (parallel_moves_[i] != NULL) {
      parallel_moves_[i]->PrintDataTo(stream);
    }
    stream->Add(") ");
  }
}


115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
const char* LArithmeticD::Mnemonic() const {
  switch (op()) {
    case Token::ADD: return "add-d";
    case Token::SUB: return "sub-d";
    case Token::MUL: return "mul-d";
    case Token::DIV: return "div-d";
    case Token::MOD: return "mod-d";
    default:
      UNREACHABLE();
      return NULL;
  }
}


const char* LArithmeticT::Mnemonic() const {
  switch (op()) {
    case Token::ADD: return "add-t";
    case Token::SUB: return "sub-t";
    case Token::MUL: return "mul-t";
    case Token::MOD: return "mod-t";
    case Token::DIV: return "div-t";
136 137 138
    case Token::BIT_AND: return "bit-and-t";
    case Token::BIT_OR: return "bit-or-t";
    case Token::BIT_XOR: return "bit-xor-t";
139
    case Token::ROR: return "ror-t";
140 141 142
    case Token::SHL: return "sal-t";
    case Token::SAR: return "sar-t";
    case Token::SHR: return "shr-t";
143 144 145 146 147 148 149
    default:
      UNREACHABLE();
      return NULL;
  }
}


150 151 152 153 154
bool LGoto::HasInterestingComment(LCodeGen* gen) const {
  return !gen->IsNextEmittedBlock(block_id());
}


155 156 157 158
template<int R>
bool LTemplateResultInstruction<R>::MustSignExtendResult(
    LPlatformChunk* chunk) const {
  HValue* hvalue = this->hydrogen_value();
159 160 161
  return hvalue != NULL &&
      hvalue->representation().IsInteger32() &&
      chunk->GetDehoistedKeyIds()->Contains(hvalue->id());
162 163 164
}


165 166 167 168 169
void LGoto::PrintDataTo(StringStream* stream) {
  stream->Add("B%d", block_id());
}


170 171
void LBranch::PrintDataTo(StringStream* stream) {
  stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
172
  value()->PrintTo(stream);
173 174 175
}


176
void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
177
  stream->Add("if ");
178
  left()->PrintTo(stream);
179
  stream->Add(" %s ", Token::String(op()));
180
  right()->PrintTo(stream);
181 182 183 184 185 186
  stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
}


void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_object(");
187
  value()->PrintTo(stream);
188 189 190 191
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


192 193
void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_string(");
194
  value()->PrintTo(stream);
195 196 197 198
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


199 200
void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_smi(");
201
  value()->PrintTo(stream);
202 203 204 205
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


206 207
void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_undetectable(");
208
  value()->PrintTo(stream);
209 210 211 212
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


213 214
void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if string_compare(");
215 216
  left()->PrintTo(stream);
  right()->PrintTo(stream);
217 218 219 220
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


221 222
void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if has_instance_type(");
223
  value()->PrintTo(stream);
224 225 226 227 228 229
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if has_cached_array_index(");
230
  value()->PrintTo(stream);
231 232 233 234 235 236
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if class_of_test(");
237
  value()->PrintTo(stream);
238 239 240 241 242 243 244 245 246
  stream->Add(", \"%o\") then B%d else B%d",
              *hydrogen()->class_name(),
              true_block_id(),
              false_block_id());
}


void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if typeof ");
247
  value()->PrintTo(stream);
248
  stream->Add(" == \"%s\" then B%d else B%d",
249
              hydrogen()->type_literal()->ToCString().get(),
250 251 252 253
              true_block_id(), false_block_id());
}


254 255 256 257 258 259 260 261
void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
  stream->Add(" = ");
  function()->PrintTo(stream);
  stream->Add(".code_entry = ");
  code_object()->PrintTo(stream);
}


262 263 264
void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
  stream->Add(" = ");
  base_object()->PrintTo(stream);
265 266
  stream->Add(" + ");
  offset()->PrintTo(stream);
267 268 269
}


270 271 272 273 274 275 276 277 278 279 280 281
void LCallJSFunction::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
  function()->PrintTo(stream);
  stream->Add("#%d / ", arity());
}


void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
  for (int i = 0; i < InputCount(); i++) {
    InputAt(i)->PrintTo(stream);
    stream->Add(" ");
  }
282 283 284 285
  stream->Add("#%d / ", arity());
}


286
void LLoadContextSlot::PrintDataTo(StringStream* stream) {
287
  context()->PrintTo(stream);
288
  stream->Add("[%d]", slot_index());
289 290 291
}


292
void LStoreContextSlot::PrintDataTo(StringStream* stream) {
293
  context()->PrintTo(stream);
whesse@chromium.org's avatar
whesse@chromium.org committed
294
  stream->Add("[%d] <- ", slot_index());
295
  value()->PrintTo(stream);
296 297 298
}


299 300
void LInvokeFunction::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
301
  function()->PrintTo(stream);
302 303 304 305
  stream->Add(" #%d / ", arity());
}


306 307
void LCallNew::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
308
  constructor()->PrintTo(stream);
309 310 311 312
  stream->Add(" #%d / ", arity());
}


313 314 315 316
void LCallNewArray::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
  constructor()->PrintTo(stream);
  stream->Add(" #%d / ", arity());
317
  ElementsKind kind = hydrogen()->elements_kind();
318 319 320 321
  stream->Add(" (%s) ", ElementsKindToString(kind));
}


322 323 324 325 326 327 328 329 330 331 332
void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
  arguments()->PrintTo(stream);

  stream->Add(" length ");
  length()->PrintTo(stream);

  stream->Add(" index ");
  index()->PrintTo(stream);
}


333
int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
334 335 336 337 338 339 340 341 342 343
  if (kind == DOUBLE_REGISTERS && kDoubleSize == 2 * kPointerSize) {
    // Skip a slot if for a double-width slot for x32 port.
    spill_slot_count_++;
    // The spill slot's address is at rbp - (index + 1) * kPointerSize -
    // StandardFrameConstants::kFixedFrameSizeFromFp. kFixedFrameSizeFromFp is
    // 2 * kPointerSize, if rbp is aligned at 8-byte boundary, the below "|= 1"
    // will make sure the spilled doubles are aligned at 8-byte boundary.
    // TODO(haitao): make sure rbp is aligned at 8-byte boundary for x32 port.
    spill_slot_count_ |= 1;
  }
344
  return spill_slot_count_++;
345 346 347
}


348
LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
349 350 351
  // All stack slots are Double stack slots on x64.
  // Alternatively, at some point, start using half-size
  // stack slots for int32 values.
352 353
  int index = GetNextSpillIndex(kind);
  if (kind == DOUBLE_REGISTERS) {
354
    return LDoubleStackSlot::Create(index, zone());
355
  } else {
356
    ASSERT(kind == GENERAL_REGISTERS);
357
    return LStackSlot::Create(index, zone());
358
  }
359 360 361
}


362
void LStoreNamedField::PrintDataTo(StringStream* stream) {
363
  object()->PrintTo(stream);
364
  hydrogen()->access().PrintTo(stream);
365 366 367 368 369
  stream->Add(" <- ");
  value()->PrintTo(stream);
}


370 371 372
void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
  stream->Add(".");
373
  stream->Add(String::cast(*name())->ToCString().get());
374 375 376 377 378
  stream->Add(" <- ");
  value()->PrintTo(stream);
}


379 380 381 382 383
void LLoadKeyed::PrintDataTo(StringStream* stream) {
  elements()->PrintTo(stream);
  stream->Add("[");
  key()->PrintTo(stream);
  if (hydrogen()->IsDehoisted()) {
384
    stream->Add(" + %d]", base_offset());
385 386 387 388 389 390
  } else {
    stream->Add("]");
  }
}


391
void LStoreKeyed::PrintDataTo(StringStream* stream) {
392 393 394
  elements()->PrintTo(stream);
  stream->Add("[");
  key()->PrintTo(stream);
395
  if (hydrogen()->IsDehoisted()) {
396
    stream->Add(" + %d] <-", base_offset());
397 398 399
  } else {
    stream->Add("] <- ");
  }
400 401 402 403 404 405 406 407

  if (value() == NULL) {
    ASSERT(hydrogen()->IsConstantHoleStore() &&
           hydrogen()->value()->representation().IsDouble());
    stream->Add("<the hole(nan)>");
  } else {
    value()->PrintTo(stream);
  }
408 409 410
}


411
void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
412 413 414 415 416 417 418 419
  object()->PrintTo(stream);
  stream->Add("[");
  key()->PrintTo(stream);
  stream->Add("] <- ");
  value()->PrintTo(stream);
}


420 421 422 423 424 425
void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
  stream->Add(" %p -> %p", *original_map(), *transitioned_map());
}


426
LPlatformChunk* LChunkBuilder::Build() {
427
  ASSERT(is_unused());
428
  chunk_ = new(zone()) LPlatformChunk(info(), graph());
429
  LPhase phase("L_Building chunk", chunk_);
430
  status_ = BUILDING;
431 432 433 434 435

  // If compiling for OSR, reserve space for the unoptimized frame,
  // which will be subsumed into this frame.
  if (graph()->has_osr()) {
    for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
436
      chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
437 438 439
    }
  }

440 441 442 443 444 445 446 447 448 449 450 451
  const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
  for (int i = 0; i < blocks->length(); i++) {
    HBasicBlock* next = NULL;
    if (i < blocks->length() - 1) next = blocks->at(i + 1);
    DoBasicBlock(blocks->at(i), next);
    if (is_aborted()) return NULL;
  }
  status_ = DONE;
  return chunk_;
}


452
void LChunkBuilder::Abort(BailoutReason reason) {
453
  info()->set_bailout_reason(reason);
454 455 456 457
  status_ = ABORTED;
}


458
LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
459 460
  return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
                                  Register::ToAllocationIndex(reg));
461 462 463 464
}


LUnallocated* LChunkBuilder::ToUnallocated(XMMRegister reg) {
465 466
  return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
                                  XMMRegister::ToAllocationIndex(reg));
467 468 469 470 471 472 473 474 475 476 477 478 479 480
}


LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
  return Use(value, ToUnallocated(fixed_register));
}


LOperand* LChunkBuilder::UseFixedDouble(HValue* value, XMMRegister reg) {
  return Use(value, ToUnallocated(reg));
}


LOperand* LChunkBuilder::UseRegister(HValue* value) {
481
  return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
482 483 484 485 486
}


LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
  return Use(value,
487
             new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
488 489 490 491 492
                              LUnallocated::USED_AT_START));
}


LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
493
  return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
494 495 496
}


497 498 499 500 501 502 503
LOperand* LChunkBuilder::UseTempRegisterOrConstant(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
      : UseTempRegister(value);
}


504
LOperand* LChunkBuilder::Use(HValue* value) {
505
  return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
506 507 508 509
}


LOperand* LChunkBuilder::UseAtStart(HValue* value) {
510
  return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
                                     LUnallocated::USED_AT_START));
}


LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
      : Use(value);
}


LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
      : UseAtStart(value);
}


LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
      : UseRegister(value);
}


LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
      : UseRegisterAtStart(value);
}


543 544 545 546 547
LOperand* LChunkBuilder::UseConstant(HValue* value) {
  return chunk_->DefineConstantOperand(HConstant::cast(value));
}


548 549 550
LOperand* LChunkBuilder::UseAny(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
551
      :  Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
552 553 554
}


555 556 557 558 559
LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
  if (value->EmitAtUses()) {
    HInstruction* instr = HInstruction::cast(value);
    VisitInstruction(instr);
  }
560
  operand->set_virtual_register(value->id());
561 562 563 564
  return operand;
}


565
LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
566
                                    LUnallocated* result) {
567
  result->set_virtual_register(current_instruction_->id());
568 569 570 571 572 573
  instr->set_result(result);
  return instr;
}


LInstruction* LChunkBuilder::DefineAsRegister(
574
    LTemplateResultInstruction<1>* instr) {
575 576
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
577 578 579 580
}


LInstruction* LChunkBuilder::DefineAsSpilled(
581
    LTemplateResultInstruction<1>* instr,
582
    int index) {
583 584
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
585 586 587 588
}


LInstruction* LChunkBuilder::DefineSameAsFirst(
589
    LTemplateResultInstruction<1>* instr) {
590 591
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
592 593 594
}


595
LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
596 597 598 599 600 601
                                         Register reg) {
  return Define(instr, ToUnallocated(reg));
}


LInstruction* LChunkBuilder::DefineFixedDouble(
602
    LTemplateResultInstruction<1>* instr,
603 604 605 606 607 608 609
    XMMRegister reg) {
  return Define(instr, ToUnallocated(reg));
}


LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
  HEnvironment* hydrogen_env = current_block_->last_environment();
610
  int argument_index_accumulator = 0;
611
  ZoneList<HValue*> objects_to_materialize(0, zone());
612
  instr->set_environment(CreateEnvironment(hydrogen_env,
613 614
                                           &argument_index_accumulator,
                                           &objects_to_materialize));
615 616 617 618 619 620 621
  return instr;
}


LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
                                        HInstruction* hinstr,
                                        CanDeoptimize can_deoptimize) {
622 623
  info()->MarkAsNonDeferredCalling();

624 625 626 627
#ifdef DEBUG
  instr->VerifyCall();
#endif
  instr->MarkAsCall();
628 629 630 631 632 633 634
  instr = AssignPointerMap(instr);

  // If instruction does not have side-effects lazy deoptimization
  // after the call will try to deoptimize to the point before the call.
  // Thus we still need to attach environment to this call even if
  // call sequence can not deoptimize eagerly.
  bool needs_environment =
635 636
      (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
      !hinstr->HasObservableSideEffects();
637 638
  if (needs_environment && !instr->HasEnvironment()) {
    instr = AssignEnvironment(instr);
639 640
    // We can't really figure out if the environment is needed or not.
    instr->environment()->set_has_been_used();
641 642 643 644 645 646 647 648
  }

  return instr;
}


LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
  ASSERT(!instr->HasPointerMap());
649
  instr->set_pointer_map(new(zone()) LPointerMap(zone()));
650 651 652 653 654
  return instr;
}


LUnallocated* LChunkBuilder::TempRegister() {
655 656
  LUnallocated* operand =
      new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
657 658
  int vreg = allocator_->GetVirtualRegister();
  if (!allocator_->AllocationOk()) {
659
    Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
660
    vreg = 0;
661 662
  }
  operand->set_virtual_register(vreg);
663 664 665 666 667 668
  return operand;
}


LOperand* LChunkBuilder::FixedTemp(Register reg) {
  LUnallocated* operand = ToUnallocated(reg);
669
  ASSERT(operand->HasFixedPolicy());
670 671 672 673 674 675
  return operand;
}


LOperand* LChunkBuilder::FixedTemp(XMMRegister reg) {
  LUnallocated* operand = ToUnallocated(reg);
676
  ASSERT(operand->HasFixedPolicy());
677 678 679 680 681
  return operand;
}


LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
682
  return new(zone()) LLabel(instr->block());
683 684 685
}


686 687 688 689 690
LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
  return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
}


691 692 693 694 695 696
LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
  UNREACHABLE();
  return NULL;
}


697
LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
698
  return AssignEnvironment(new(zone()) LDeoptimize);
699 700 701
}


702 703
LInstruction* LChunkBuilder::DoShift(Token::Value op,
                                     HBitwiseBinaryOperation* instr) {
704 705 706 707
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
    LOperand* left = UseRegisterAtStart(instr->left());
708

709 710 711
    HValue* right_value = instr->right();
    LOperand* right = NULL;
    int constant_value = 0;
712
    bool does_deopt = false;
713 714 715 716
    if (right_value->IsConstant()) {
      HConstant* constant = HConstant::cast(right_value);
      right = chunk_->DefineConstantOperand(constant);
      constant_value = constant->Integer32Value() & 0x1f;
717 718 719 720 721 722
      if (SmiValuesAre31Bits() && instr->representation().IsSmi() &&
          constant_value > 0) {
        // Left shift can deoptimize if we shift by > 0 and the result
        // cannot be truncated to smi.
        does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
      }
723
    } else {
724
      right = UseFixed(right_value, rcx);
725 726
    }

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    // Shift operations can only deoptimize if we do a logical shift by 0 and
    // the result cannot be truncated to int32.
    if (op == Token::SHR && constant_value == 0) {
      if (FLAG_opt_safe_uint32_operations) {
        does_deopt = !instr->CheckFlag(HInstruction::kUint32);
      } else {
        does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToInt32);
      }
    }

    LInstruction* result =
        DefineSameAsFirst(new(zone()) LShiftI(op, left, right, does_deopt));
    return does_deopt ? AssignEnvironment(result) : result;
  } else {
    return DoArithmeticT(op, instr);
  }
743 744 745 746 747
}


LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
                                           HArithmeticBinaryOperation* instr) {
748 749 750
  ASSERT(instr->representation().IsDouble());
  ASSERT(instr->left()->representation().IsDouble());
  ASSERT(instr->right()->representation().IsDouble());
751
  if (op == Token::MOD) {
752 753
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    LOperand* right = UseFixedDouble(instr->BetterRightOperand(), xmm1);
754 755
    LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
    return MarkAsCall(DefineSameAsFirst(result), instr);
756 757 758 759 760
  } else {
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
    LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
    return DefineSameAsFirst(result);
761
  }
762 763 764 765
}


LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
766
                                           HBinaryOperation* instr) {
767 768
  HValue* left = instr->left();
  HValue* right = instr->right();
769 770
  ASSERT(left->representation().IsTagged());
  ASSERT(right->representation().IsTagged());
771
  LOperand* context = UseFixed(instr->context(), rsi);
772 773
  LOperand* left_operand = UseFixed(left, rdx);
  LOperand* right_operand = UseFixed(right, rax);
774
  LArithmeticT* result =
775
      new(zone()) LArithmeticT(op, context, left_operand, right_operand);
776
  return MarkAsCall(DefineFixed(result, rax), instr);
777 778
}

779

780
void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
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
  ASSERT(is_building());
  current_block_ = block;
  next_block_ = next_block;
  if (block->IsStartBlock()) {
    block->UpdateEnvironment(graph_->start_environment());
    argument_count_ = 0;
  } else if (block->predecessors()->length() == 1) {
    // We have a single predecessor => copy environment and outgoing
    // argument count from the predecessor.
    ASSERT(block->phis()->length() == 0);
    HBasicBlock* pred = block->predecessors()->at(0);
    HEnvironment* last_environment = pred->last_environment();
    ASSERT(last_environment != NULL);
    // Only copy the environment, if it is later used again.
    if (pred->end()->SecondSuccessor() == NULL) {
      ASSERT(pred->end()->FirstSuccessor() == block);
    } else {
      if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
          pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
        last_environment = last_environment->Copy();
      }
    }
    block->UpdateEnvironment(last_environment);
    ASSERT(pred->argument_count() >= 0);
    argument_count_ = pred->argument_count();
  } else {
    // We are at a state join => process phis.
    HBasicBlock* pred = block->predecessors()->at(0);
    // No need to copy the environment, it cannot be used later.
    HEnvironment* last_environment = pred->last_environment();
    for (int i = 0; i < block->phis()->length(); ++i) {
      HPhi* phi = block->phis()->at(i);
813
      if (phi->HasMergedIndex()) {
814 815
        last_environment->SetValueAt(phi->merged_index(), phi);
      }
816 817
    }
    for (int i = 0; i < block->deleted_phis()->length(); ++i) {
818 819 820 821
      if (block->deleted_phis()->at(i) < last_environment->length()) {
        last_environment->SetValueAt(block->deleted_phis()->at(i),
                                     graph_->GetConstantUndefined());
      }
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    }
    block->UpdateEnvironment(last_environment);
    // Pick up the outgoing argument count of one of the predecessors.
    argument_count_ = pred->argument_count();
  }
  HInstruction* current = block->first();
  int start = chunk_->instructions()->length();
  while (current != NULL && !is_aborted()) {
    // Code for constants in registers is generated lazily.
    if (!current->EmitAtUses()) {
      VisitInstruction(current);
    }
    current = current->next();
  }
  int end = chunk_->instructions()->length() - 1;
  if (end >= start) {
    block->set_first_instruction_index(start);
    block->set_last_instruction_index(end);
  }
  block->set_argument_count(argument_count_);
  next_block_ = NULL;
  current_block_ = NULL;
844 845 846
}


847 848 849
void LChunkBuilder::VisitInstruction(HInstruction* current) {
  HInstruction* old_current = current_instruction_;
  current_instruction_ = current;
850 851 852

  LInstruction* instr = NULL;
  if (current->CanReplaceWithDummyUses()) {
853 854 855
    if (current->OperandCount() == 0) {
      instr = DefineAsRegister(new(zone()) LDummy());
    } else {
856
      ASSERT(!current->OperandAt(0)->IsControlInstruction());
857 858 859
      instr = DefineAsRegister(new(zone())
          LDummyUse(UseAny(current->OperandAt(0))));
    }
860
    for (int i = 1; i < current->OperandCount(); ++i) {
861
      if (current->OperandAt(i)->IsControlInstruction()) continue;
862 863 864 865 866 867
      LInstruction* dummy =
          new(zone()) LDummyUse(UseAny(current->OperandAt(i)));
      dummy->set_hydrogen_value(current);
      chunk_->AddInstruction(dummy, current_block_);
    }
  } else {
868 869 870 871 872 873 874 875
    HBasicBlock* successor;
    if (current->IsControlInstruction() &&
        HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
        successor != NULL) {
      instr = new(zone()) LGoto(successor);
    } else {
      instr = current->CompileToLithium(this);
    }
876
  }
877

878 879 880
  argument_count_ += current->argument_delta();
  ASSERT(argument_count_ >= 0);

881
  if (instr != NULL) {
882 883 884 885 886 887 888 889 890 891 892 893
    AddInstruction(instr, current);
  }

  current_instruction_ = old_current;
}


void LChunkBuilder::AddInstruction(LInstruction* instr,
                                   HInstruction* hydrogen_val) {
  // Associate the hydrogen instruction first, since we may need it for
  // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
  instr->set_hydrogen_value(hydrogen_val);
894

895
#if DEBUG
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  // Make sure that the lithium instruction has either no fixed register
  // constraints in temps or the result OR no uses that are only used at
  // start. If this invariant doesn't hold, the register allocator can decide
  // to insert a split of a range immediately before the instruction due to an
  // already allocated register needing to be used for the instruction's fixed
  // register constraint. In this case, The register allocator won't see an
  // interference between the split child and the use-at-start (it would if
  // the it was just a plain use), so it is free to move the split child into
  // the same register that is used for the use-at-start.
  // See https://code.google.com/p/chromium/issues/detail?id=201590
  if (!(instr->ClobbersRegisters() &&
        instr->ClobbersDoubleRegisters(isolate()))) {
    int fixed = 0;
    int used_at_start = 0;
    for (UseIterator it(instr); !it.Done(); it.Advance()) {
      LUnallocated* operand = LUnallocated::cast(it.Current());
      if (operand->IsUsedAtStart()) ++used_at_start;
    }
    if (instr->Output() != NULL) {
      if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
916
    }
917 918 919 920 921 922
    for (TempIterator it(instr); !it.Done(); it.Advance()) {
      LUnallocated* operand = LUnallocated::cast(it.Current());
      if (operand->HasFixedPolicy()) ++fixed;
    }
    ASSERT(fixed == 0 || used_at_start == 0);
  }
923 924
#endif

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
  if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
    instr = AssignPointerMap(instr);
  }
  if (FLAG_stress_environments && !instr->HasEnvironment()) {
    instr = AssignEnvironment(instr);
  }
  chunk_->AddInstruction(instr, current_block_);

  if (instr->IsCall()) {
    HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
    LInstruction* instruction_needing_environment = NULL;
    if (hydrogen_val->HasObservableSideEffects()) {
      HSimulate* sim = HSimulate::cast(hydrogen_val->next());
      instruction_needing_environment = instr;
      sim->ReplayEnvironment(current_block_->last_environment());
      hydrogen_value_for_lazy_bailout = sim;
941
    }
942 943 944 945 946 947 948 949
    LInstruction* bailout = AssignEnvironment(new(zone()) LLazyBailout());
    bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
    chunk_->AddInstruction(bailout, current_block_);
    if (instruction_needing_environment != NULL) {
      // Store the lazy deopt environment with the instruction if needed.
      // Right now it is only used for LInstanceOfKnownGlobal.
      instruction_needing_environment->
          SetDeferredLazyDeoptimizationEnvironment(bailout->environment());
950
    }
951 952 953 954 955
  }
}


LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
956
  return new(zone()) LGoto(instr->FirstSuccessor());
957 958 959
}


960 961 962 963 964
LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
  return new(zone()) LDebugBreak();
}


965
LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
966
  HValue* value = instr->value();
967
  Representation r = value->representation();
968
  HType type = value->type();
969 970 971 972 973 974 975 976 977 978
  ToBooleanStub::Types expected = instr->expected_input_types();
  if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();

  bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
      type.IsJSArray() || type.IsHeapNumber() || type.IsString();
  LInstruction* branch = new(zone()) LBranch(UseRegister(value));
  if (!easy_case &&
      ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
       !expected.IsGeneric())) {
    branch = AssignEnvironment(branch);
979
  }
980
  return branch;
981 982 983
}


984
LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
985 986
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* value = UseRegisterAtStart(instr->value());
987
  return new(zone()) LCmpMapAndBranch(value);
988 989 990 991
}


LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
992
  info()->MarkAsRequiresFrame();
993
  return DefineAsRegister(new(zone()) LArgumentsLength(Use(length->value())));
994 995 996 997
}


LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
998
  info()->MarkAsRequiresFrame();
999
  return DefineAsRegister(new(zone()) LArgumentsElements);
1000 1001 1002 1003
}


LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1004 1005
  LOperand* left = UseFixed(instr->left(), rax);
  LOperand* right = UseFixed(instr->right(), rdx);
1006 1007
  LOperand* context = UseFixed(instr->context(), rsi);
  LInstanceOf* result = new(zone()) LInstanceOf(context, left, right);
1008
  return MarkAsCall(DefineFixed(result, rax), instr);
1009 1010 1011 1012 1013
}


LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
    HInstanceOfKnownGlobal* instr) {
1014
  LInstanceOfKnownGlobal* result =
1015 1016
      new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->context(), rsi),
                                         UseFixed(instr->left(), rax),
1017
                                         FixedTemp(rdi));
1018
  return MarkAsCall(DefineFixed(result, rax), instr);
1019 1020 1021
}


1022 1023 1024 1025 1026 1027 1028 1029
LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
  LOperand* receiver = UseRegister(instr->receiver());
  LOperand* function = UseRegisterAtStart(instr->function());
  LWrapReceiver* result = new(zone()) LWrapReceiver(receiver, function);
  return AssignEnvironment(DefineSameAsFirst(result));
}


1030
LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1031 1032 1033 1034
  LOperand* function = UseFixed(instr->function(), rdi);
  LOperand* receiver = UseFixed(instr->receiver(), rax);
  LOperand* length = UseFixed(instr->length(), rbx);
  LOperand* elements = UseFixed(instr->elements(), rcx);
1035
  LApplyArguments* result = new(zone()) LApplyArguments(function,
1036 1037 1038 1039
                                                receiver,
                                                length,
                                                elements);
  return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
1040 1041 1042
}


1043 1044 1045 1046 1047 1048 1049
LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
  int argc = instr->OperandCount();
  for (int i = 0; i < argc; ++i) {
    LOperand* argument = UseOrConstant(instr->argument(i));
    AddInstruction(new(zone()) LPushArgument(argument), instr);
  }
  return NULL;
1050 1051 1052
}


1053 1054 1055 1056 1057 1058 1059 1060
LInstruction* LChunkBuilder::DoStoreCodeEntry(
    HStoreCodeEntry* store_code_entry) {
  LOperand* function = UseRegister(store_code_entry->function());
  LOperand* code_object = UseTempRegister(store_code_entry->code_object());
  return new(zone()) LStoreCodeEntry(function, code_object);
}


1061
LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1062 1063 1064 1065 1066
    HInnerAllocatedObject* instr) {
  LOperand* base_object = UseRegisterAtStart(instr->base_object());
  LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
  return DefineAsRegister(
      new(zone()) LInnerAllocatedObject(base_object, offset));
1067 1068 1069
}


1070
LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1071 1072 1073
  return instr->HasNoUses()
      ? NULL
      : DefineAsRegister(new(zone()) LThisFunction);
1074 1075 1076
}


1077
LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1078 1079 1080 1081
  if (instr->HasNoUses()) return NULL;

  if (info()->IsStub()) {
    return DefineFixed(new(zone()) LContext, rsi);
1082 1083
  }

1084
  return DefineAsRegister(new(zone()) LContext);
1085 1086 1087
}


1088
LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1089 1090
  LOperand* context = UseFixed(instr->context(), rsi);
  return MarkAsCall(new(zone()) LDeclareGlobals(context), instr);
1091 1092 1093
}


1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
LInstruction* LChunkBuilder::DoCallJSFunction(
    HCallJSFunction* instr) {
  LOperand* function = UseFixed(instr->function(), rdi);

  LCallJSFunction* result = new(zone()) LCallJSFunction(function);

  return MarkAsCall(DefineFixed(result, rax), instr);
}


LInstruction* LChunkBuilder::DoCallWithDescriptor(
    HCallWithDescriptor* instr) {
  const CallInterfaceDescriptor* descriptor = instr->descriptor();

  LOperand* target = UseRegisterOrConstantAtStart(instr->target());
  ZoneList<LOperand*> ops(instr->OperandCount(), zone());
  ops.Add(target, zone());
  for (int i = 1; i < instr->OperandCount(); i++) {
    LOperand* op = UseFixed(instr->OperandAt(i),
        descriptor->GetParameterRegister(i - 1));
    ops.Add(op, zone());
  }

  LCallWithDescriptor* result = new(zone()) LCallWithDescriptor(
      descriptor, ops, zone());
  return MarkAsCall(DefineFixed(result, rax), instr);
1120 1121 1122
}


1123
LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1124
  LOperand* context = UseFixed(instr->context(), rsi);
1125
  LOperand* function = UseFixed(instr->function(), rdi);
1126
  LInvokeFunction* result = new(zone()) LInvokeFunction(context, function);
1127 1128 1129 1130
  return MarkAsCall(DefineFixed(result, rax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
}


1131
LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1132 1133 1134 1135 1136 1137 1138 1139
  switch (instr->op()) {
    case kMathFloor: return DoMathFloor(instr);
    case kMathRound: return DoMathRound(instr);
    case kMathAbs: return DoMathAbs(instr);
    case kMathLog: return DoMathLog(instr);
    case kMathExp: return DoMathExp(instr);
    case kMathSqrt: return DoMathSqrt(instr);
    case kMathPowHalf: return DoMathPowHalf(instr);
1140
    case kMathClz32: return DoMathClz32(instr);
1141 1142 1143
    default:
      UNREACHABLE();
      return NULL;
1144
  }
1145 1146 1147
}


1148 1149 1150 1151 1152 1153 1154 1155
LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
  LOperand* input = UseRegisterAtStart(instr->value());
  LMathFloor* result = new(zone()) LMathFloor(input);
  return AssignEnvironment(DefineAsRegister(result));
}


LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1156 1157 1158
  LOperand* input = UseRegister(instr->value());
  LOperand* temp = FixedTemp(xmm4);
  LMathRound* result = new(zone()) LMathRound(input, temp);
1159 1160 1161
  return AssignEnvironment(DefineAsRegister(result));
}

1162

1163
LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1164
  LOperand* context = UseAny(instr->context());
1165
  LOperand* input = UseRegisterAtStart(instr->value());
1166 1167 1168 1169 1170 1171
  LInstruction* result =
      DefineSameAsFirst(new(zone()) LMathAbs(context, input));
  Representation r = instr->value()->representation();
  if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
  if (!r.IsDouble()) result = AssignEnvironment(result);
  return result;
1172 1173 1174 1175
}


LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1176 1177 1178
  ASSERT(instr->representation().IsDouble());
  ASSERT(instr->value()->representation().IsDouble());
  LOperand* input = UseRegisterAtStart(instr->value());
1179
  return MarkAsCall(DefineSameAsFirst(new(zone()) LMathLog(input)), instr);
1180 1181 1182
}


1183 1184 1185 1186 1187 1188 1189
LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
  LOperand* input = UseRegisterAtStart(instr->value());
  LMathClz32* result = new(zone()) LMathClz32(input);
  return DefineAsRegister(result);
}


1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
  ASSERT(instr->representation().IsDouble());
  ASSERT(instr->value()->representation().IsDouble());
  LOperand* value = UseTempRegister(instr->value());
  LOperand* temp1 = TempRegister();
  LOperand* temp2 = TempRegister();
  LMathExp* result = new(zone()) LMathExp(value, temp1, temp2);
  return DefineAsRegister(result);
}


LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1202 1203
  LOperand* input = UseAtStart(instr->value());
  return DefineAsRegister(new(zone()) LMathSqrt(input));
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
}


LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
  LOperand* input = UseRegisterAtStart(instr->value());
  LMathPowHalf* result = new(zone()) LMathPowHalf(input);
  return DefineSameAsFirst(result);
}


1214
LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1215
  LOperand* context = UseFixed(instr->context(), rsi);
1216
  LOperand* constructor = UseFixed(instr->constructor(), rdi);
1217
  LCallNew* result = new(zone()) LCallNew(context, constructor);
1218
  return MarkAsCall(DefineFixed(result, rax), instr);
1219 1220 1221
}


1222
LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1223
  LOperand* context = UseFixed(instr->context(), rsi);
1224
  LOperand* constructor = UseFixed(instr->constructor(), rdi);
1225
  LCallNewArray* result = new(zone()) LCallNewArray(context, constructor);
1226 1227 1228 1229
  return MarkAsCall(DefineFixed(result, rax), instr);
}


1230
LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1231
  LOperand* context = UseFixed(instr->context(), rsi);
1232
  LOperand* function = UseFixed(instr->function(), rdi);
1233
  LCallFunction* call = new(zone()) LCallFunction(context, function);
verwaest@chromium.org's avatar
verwaest@chromium.org committed
1234
  return MarkAsCall(DefineFixed(call, rax), instr);
1235 1236 1237 1238
}


LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1239 1240 1241
  LOperand* context = UseFixed(instr->context(), rsi);
  LCallRuntime* result = new(zone()) LCallRuntime(context);
  return MarkAsCall(DefineFixed(result, rax), instr);
1242 1243 1244
}


1245 1246 1247 1248 1249
LInstruction* LChunkBuilder::DoRor(HRor* instr) {
  return DoShift(Token::ROR, instr);
}


1250
LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1251
  return DoShift(Token::SHR, instr);
1252 1253 1254 1255
}


LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1256
  return DoShift(Token::SAR, instr);
1257 1258 1259 1260
}


LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1261
  return DoShift(Token::SHL, instr);
1262 1263 1264
}


1265
LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1266 1267 1268
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1269
    ASSERT(instr->CheckFlag(HValue::kTruncatingToInt32));
1270

1271 1272
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1273
    return DefineSameAsFirst(new(zone()) LBitI(left, right));
1274
  } else {
1275
    return DoArithmeticT(instr->op(), instr);
1276
  }
1277 1278 1279
}


1280 1281 1282 1283 1284 1285
LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
  ASSERT(instr->representation().IsSmiOrInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseRegister(instr->left());
  int32_t divisor = instr->right()->GetInteger32Constant();
1286 1287 1288 1289
  LInstruction* result = DefineAsRegister(new(zone()) LDivByPowerOf2I(
          dividend, divisor));
  if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
      (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1290
      (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1291 1292 1293 1294
       divisor != 1 && divisor != -1)) {
    result = AssignEnvironment(result);
  }
  return result;
1295 1296 1297
}


1298 1299 1300 1301 1302 1303 1304 1305
LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
  ASSERT(instr->representation().IsInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseRegister(instr->left());
  int32_t divisor = instr->right()->GetInteger32Constant();
  LOperand* temp1 = FixedTemp(rax);
  LOperand* temp2 = FixedTemp(rdx);
1306 1307 1308 1309 1310 1311 1312 1313
  LInstruction* result = DefineFixed(new(zone()) LDivByConstI(
          dividend, divisor, temp1, temp2), rdx);
  if (divisor == 0 ||
      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
      !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
    result = AssignEnvironment(result);
  }
  return result;
1314 1315 1316
}


1317
LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1318 1319 1320 1321 1322 1323
  ASSERT(instr->representation().IsSmiOrInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseFixed(instr->left(), rax);
  LOperand* divisor = UseRegister(instr->right());
  LOperand* temp = FixedTemp(rdx);
1324 1325 1326 1327 1328
  LInstruction* result = DefineFixed(new(zone()) LDivI(
          dividend, divisor, temp), rax);
  if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
      instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
      instr->CheckFlag(HValue::kCanOverflow) ||
1329
      !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1330 1331 1332
    result = AssignEnvironment(result);
  }
  return result;
1333 1334 1335
}


1336
LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1337
  if (instr->representation().IsSmiOrInteger32()) {
1338 1339 1340 1341 1342 1343 1344
    if (instr->RightIsPowerOf2()) {
      return DoDivByPowerOf2I(instr);
    } else if (instr->right()->IsConstant()) {
      return DoDivByConstI(instr);
    } else {
      return DoDivI(instr);
    }
1345 1346
  } else if (instr->representation().IsDouble()) {
    return DoArithmeticD(Token::DIV, instr);
1347 1348 1349
  } else {
    return DoArithmeticT(Token::DIV, instr);
  }
1350 1351 1352
}


1353 1354 1355
LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
  LOperand* dividend = UseRegisterAtStart(instr->left());
  int32_t divisor = instr->right()->GetInteger32Constant();
1356 1357 1358 1359 1360 1361 1362
  LInstruction* result = DefineSameAsFirst(new(zone()) LFlooringDivByPowerOf2I(
          dividend, divisor));
  if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
      (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
    result = AssignEnvironment(result);
  }
  return result;
1363 1364 1365 1366
}


LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1367 1368 1369 1370
  ASSERT(instr->representation().IsInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseRegister(instr->left());
1371
  int32_t divisor = instr->right()->GetInteger32Constant();
1372 1373
  LOperand* temp1 = FixedTemp(rax);
  LOperand* temp2 = FixedTemp(rdx);
1374 1375 1376 1377
  LOperand* temp3 =
      ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
       (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
      NULL : TempRegister();
1378
  LInstruction* result =
1379 1380 1381
      DefineFixed(new(zone()) LFlooringDivByConstI(dividend,
                                                   divisor,
                                                   temp1,
1382 1383
                                                   temp2,
                                                   temp3),
1384
                  rdx);
1385 1386 1387 1388 1389
  if (divisor == 0 ||
      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
    result = AssignEnvironment(result);
  }
  return result;
1390
}
1391

1392

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
  ASSERT(instr->representation().IsSmiOrInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseFixed(instr->left(), rax);
  LOperand* divisor = UseRegister(instr->right());
  LOperand* temp = FixedTemp(rdx);
  LInstruction* result = DefineFixed(new(zone()) LFlooringDivI(
          dividend, divisor, temp), rax);
  if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
      instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
      instr->CheckFlag(HValue::kCanOverflow)) {
    result = AssignEnvironment(result);
  }
  return result;
}


1411 1412 1413
LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
  if (instr->RightIsPowerOf2()) {
    return DoFlooringDivByPowerOf2I(instr);
1414 1415
  } else if (instr->right()->IsConstant()) {
    return DoFlooringDivByConstI(instr);
1416
  } else {
1417
    return DoFlooringDivI(instr);
1418 1419 1420 1421
  }
}


1422 1423 1424 1425 1426 1427
LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
  ASSERT(instr->representation().IsSmiOrInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseRegisterAtStart(instr->left());
  int32_t divisor = instr->right()->GetInteger32Constant();
1428 1429 1430 1431 1432 1433
  LInstruction* result = DefineSameAsFirst(new(zone()) LModByPowerOf2I(
          dividend, divisor));
  if (instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
    result = AssignEnvironment(result);
  }
  return result;
1434 1435 1436
}


1437 1438 1439 1440 1441 1442 1443 1444
LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
  ASSERT(instr->representation().IsSmiOrInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseRegister(instr->left());
  int32_t divisor = instr->right()->GetInteger32Constant();
  LOperand* temp1 = FixedTemp(rax);
  LOperand* temp2 = FixedTemp(rdx);
1445 1446 1447 1448 1449 1450
  LInstruction* result = DefineFixed(new(zone()) LModByConstI(
          dividend, divisor, temp1, temp2), rax);
  if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
    result = AssignEnvironment(result);
  }
  return result;
1451 1452 1453
}


1454 1455 1456 1457 1458 1459 1460
LInstruction* LChunkBuilder::DoModI(HMod* instr) {
  ASSERT(instr->representation().IsSmiOrInteger32());
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  LOperand* dividend = UseFixed(instr->left(), rax);
  LOperand* divisor = UseRegister(instr->right());
  LOperand* temp = FixedTemp(rdx);
1461 1462 1463 1464 1465 1466 1467
  LInstruction* result = DefineFixed(new(zone()) LModI(
          dividend, divisor, temp), rdx);
  if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
      instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
    result = AssignEnvironment(result);
  }
  return result;
1468 1469 1470
}


1471
LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1472
  if (instr->representation().IsSmiOrInteger32()) {
1473 1474 1475 1476 1477 1478 1479
    if (instr->RightIsPowerOf2()) {
      return DoModByPowerOf2I(instr);
    } else if (instr->right()->IsConstant()) {
      return DoModByConstI(instr);
    } else {
      return DoModI(instr);
    }
1480 1481
  } else if (instr->representation().IsDouble()) {
    return DoArithmeticD(Token::MOD, instr);
1482
  } else {
1483
    return DoArithmeticT(Token::MOD, instr);
1484
  }
1485 1486 1487 1488
}


LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1489 1490 1491
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1492 1493
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    LOperand* right = UseOrConstant(instr->BetterRightOperand());
1494
    LMulI* mul = new(zone()) LMulI(left, right);
1495 1496 1497 1498 1499
    if (instr->CheckFlag(HValue::kCanOverflow) ||
        instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
      AssignEnvironment(mul);
    }
    return DefineSameAsFirst(mul);
1500 1501 1502 1503 1504
  } else if (instr->representation().IsDouble()) {
    return DoArithmeticD(Token::MUL, instr);
  } else {
    return DoArithmeticT(Token::MUL, instr);
  }
1505 1506 1507 1508
}


LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1509 1510 1511
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1512 1513
    LOperand* left = UseRegisterAtStart(instr->left());
    LOperand* right = UseOrConstantAtStart(instr->right());
1514
    LSubI* sub = new(zone()) LSubI(left, right);
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
    LInstruction* result = DefineSameAsFirst(sub);
    if (instr->CheckFlag(HValue::kCanOverflow)) {
      result = AssignEnvironment(result);
    }
    return result;
  } else if (instr->representation().IsDouble()) {
    return DoArithmeticD(Token::SUB, instr);
  } else {
    return DoArithmeticT(Token::SUB, instr);
  }
1525 1526 1527 1528
}


LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1529
  if (instr->representation().IsSmiOrInteger32()) {
1530 1531 1532 1533 1534
    // Check to see if it would be advantageous to use an lea instruction rather
    // than an add. This is the case when no overflow check is needed and there
    // are multiple uses of the add's inputs, so using a 3-register add will
    // preserve all input values for later uses.
    bool use_lea = LAddI::UseLea(instr);
1535 1536
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1537 1538
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    HValue* right_candidate = instr->BetterRightOperand();
1539
    LOperand* right;
1540
    if (SmiValuesAre32Bits() && instr->representation().IsSmi()) {
1541 1542 1543 1544 1545 1546 1547
      // We cannot add a tagged immediate to a tagged value,
      // so we request it in a register.
      right = UseRegisterAtStart(right_candidate);
    } else {
      right = use_lea ? UseRegisterOrConstantAtStart(right_candidate)
                      : UseOrConstantAtStart(right_candidate);
    }
1548
    LAddI* add = new(zone()) LAddI(left, right);
1549
    bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1550 1551
    LInstruction* result = use_lea ? DefineAsRegister(add)
                                   : DefineSameAsFirst(add);
1552
    if (can_overflow) {
1553 1554 1555
      result = AssignEnvironment(result);
    }
    return result;
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
  } else if (instr->representation().IsExternal()) {
    ASSERT(instr->left()->representation().IsExternal());
    ASSERT(instr->right()->representation().IsInteger32());
    ASSERT(!instr->CheckFlag(HValue::kCanOverflow));
    bool use_lea = LAddI::UseLea(instr);
    LOperand* left = UseRegisterAtStart(instr->left());
    HValue* right_candidate = instr->right();
    LOperand* right = use_lea
        ? UseRegisterOrConstantAtStart(right_candidate)
        : UseOrConstantAtStart(right_candidate);
    LAddI* add = new(zone()) LAddI(left, right);
    LInstruction* result = use_lea
        ? DefineAsRegister(add)
        : DefineSameAsFirst(add);
    return result;
1571
  } else if (instr->representation().IsDouble()) {
ager@chromium.org's avatar
ager@chromium.org committed
1572
    return DoArithmeticD(Token::ADD, instr);
1573 1574 1575
  } else {
    return DoArithmeticT(Token::ADD, instr);
  }
1576 1577 1578 1579
  return NULL;
}


1580 1581 1582
LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
  LOperand* left = NULL;
  LOperand* right = NULL;
1583 1584 1585 1586 1587 1588
  ASSERT(instr->left()->representation().Equals(instr->representation()));
  ASSERT(instr->right()->representation().Equals(instr->representation()));
  if (instr->representation().IsSmi()) {
    left = UseRegisterAtStart(instr->BetterLeftOperand());
    right = UseAtStart(instr->BetterRightOperand());
  } else if (instr->representation().IsInteger32()) {
1589 1590
    left = UseRegisterAtStart(instr->BetterLeftOperand());
    right = UseOrConstantAtStart(instr->BetterRightOperand());
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
  } else {
    ASSERT(instr->representation().IsDouble());
    left = UseRegisterAtStart(instr->left());
    right = UseRegisterAtStart(instr->right());
  }
  LMathMinMax* minmax = new(zone()) LMathMinMax(left, right);
  return DefineSameAsFirst(minmax);
}


1601
LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1602 1603 1604 1605 1606 1607 1608
  ASSERT(instr->representation().IsDouble());
  // We call a C function for double power. It can't trigger a GC.
  // We need to use fixed result register for the call.
  Representation exponent_type = instr->right()->representation();
  ASSERT(instr->left()->representation().IsDouble());
  LOperand* left = UseFixedDouble(instr->left(), xmm2);
  LOperand* right = exponent_type.IsDouble() ?
1609
      UseFixedDouble(instr->right(), xmm1) : UseFixed(instr->right(), rdx);
1610
  LPower* result = new(zone()) LPower(left, right);
1611
  return MarkAsCall(DefineFixedDouble(result, xmm3), instr,
1612
                    CAN_DEOPTIMIZE_EAGERLY);
1613 1614 1615
}


1616
LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1617 1618
  ASSERT(instr->left()->representation().IsTagged());
  ASSERT(instr->right()->representation().IsTagged());
1619
  LOperand* context = UseFixed(instr->context(), rsi);
1620 1621
  LOperand* left = UseFixed(instr->left(), rdx);
  LOperand* right = UseFixed(instr->right(), rax);
1622
  LCmpT* result = new(zone()) LCmpT(context, left, right);
1623
  return MarkAsCall(DefineFixed(result, rax), instr);
1624 1625 1626
}


1627 1628
LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
    HCompareNumericAndBranch* instr) {
1629
  Representation r = instr->representation();
1630
  if (r.IsSmiOrInteger32()) {
1631 1632
    ASSERT(instr->left()->representation().Equals(r));
    ASSERT(instr->right()->representation().Equals(r));
1633
    LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1634
    LOperand* right = UseOrConstantAtStart(instr->right());
1635
    return new(zone()) LCompareNumericAndBranch(left, right);
1636 1637
  } else {
    ASSERT(r.IsDouble());
1638 1639
    ASSERT(instr->left()->representation().IsDouble());
    ASSERT(instr->right()->representation().IsDouble());
1640 1641 1642 1643 1644 1645 1646 1647 1648
    LOperand* left;
    LOperand* right;
    if (instr->left()->IsConstant() && instr->right()->IsConstant()) {
      left = UseRegisterOrConstantAtStart(instr->left());
      right = UseRegisterOrConstantAtStart(instr->right());
    } else {
      left = UseRegisterAtStart(instr->left());
      right = UseRegisterAtStart(instr->right());
    }
1649
    return new(zone()) LCompareNumericAndBranch(left, right);
1650
  }
1651 1652 1653
}


1654 1655
LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
    HCompareObjectEqAndBranch* instr) {
1656
  LOperand* left = UseRegisterAtStart(instr->left());
1657
  LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1658
  return new(zone()) LCmpObjectEqAndBranch(left, right);
1659 1660 1661
}


1662 1663
LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
    HCompareHoleAndBranch* instr) {
1664 1665
  LOperand* value = UseRegisterAtStart(instr->value());
  return new(zone()) LCmpHoleAndBranch(value);
1666 1667 1668
}


1669 1670 1671 1672 1673 1674 1675
LInstruction* LChunkBuilder::DoCompareMinusZeroAndBranch(
    HCompareMinusZeroAndBranch* instr) {
  LOperand* value = UseRegister(instr->value());
  return new(zone()) LCompareMinusZeroAndBranch(value);
}


1676
LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1677
  ASSERT(instr->value()->representation().IsTagged());
1678
  return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()));
1679 1680 1681
}


1682 1683
LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1684
  LOperand* value = UseRegisterAtStart(instr->value());
1685
  LOperand* temp = TempRegister();
1686
  return new(zone()) LIsStringAndBranch(value, temp);
1687 1688 1689
}


1690
LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1691
  ASSERT(instr->value()->representation().IsTagged());
1692
  return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1693 1694 1695
}


1696 1697
LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
    HIsUndetectableAndBranch* instr) {
1698
  ASSERT(instr->value()->representation().IsTagged());
1699 1700 1701
  LOperand* value = UseRegisterAtStart(instr->value());
  LOperand* temp = TempRegister();
  return new(zone()) LIsUndetectableAndBranch(value, temp);
1702 1703 1704
}


1705 1706 1707 1708 1709
LInstruction* LChunkBuilder::DoStringCompareAndBranch(
    HStringCompareAndBranch* instr) {

  ASSERT(instr->left()->representation().IsTagged());
  ASSERT(instr->right()->representation().IsTagged());
1710
  LOperand* context = UseFixed(instr->context(), rsi);
1711 1712
  LOperand* left = UseFixed(instr->left(), rdx);
  LOperand* right = UseFixed(instr->right(), rax);
1713
  LStringCompareAndBranch* result =
1714
      new(zone()) LStringCompareAndBranch(context, left, right);
1715 1716 1717 1718 1719

  return MarkAsCall(result, instr);
}


1720 1721
LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
    HHasInstanceTypeAndBranch* instr) {
1722
  ASSERT(instr->value()->representation().IsTagged());
1723 1724
  LOperand* value = UseRegisterAtStart(instr->value());
  return new(zone()) LHasInstanceTypeAndBranch(value);
1725 1726 1727
}


1728 1729
LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
    HGetCachedArrayIndex* instr)  {
1730 1731 1732
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* value = UseRegisterAtStart(instr->value());

1733
  return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1734 1735 1736
}


1737 1738
LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
    HHasCachedArrayIndexAndBranch* instr) {
1739
  ASSERT(instr->value()->representation().IsTagged());
1740 1741
  LOperand* value = UseRegisterAtStart(instr->value());
  return new(zone()) LHasCachedArrayIndexAndBranch(value);
1742 1743 1744
}


1745 1746
LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
    HClassOfTestAndBranch* instr) {
1747 1748 1749 1750
  LOperand* value = UseRegister(instr->value());
  return new(zone()) LClassOfTestAndBranch(value,
                                           TempRegister(),
                                           TempRegister());
1751 1752 1753
}


1754 1755 1756 1757 1758 1759
LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
  LOperand* map = UseRegisterAtStart(instr->value());
  return DefineAsRegister(new(zone()) LMapEnumLength(map));
}


1760
LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1761
  LOperand* object = UseFixed(instr->value(), rax);
1762
  LDateField* result = new(zone()) LDateField(object, instr->index());
1763
  return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
1764 1765 1766
}


1767 1768 1769 1770 1771 1772 1773
LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
  LOperand* string = UseRegisterAtStart(instr->string());
  LOperand* index = UseRegisterOrConstantAtStart(instr->index());
  return DefineAsRegister(new(zone()) LSeqStringGetChar(string, index));
}


1774
LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1775
  LOperand* string = UseRegisterAtStart(instr->string());
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
  LOperand* index = FLAG_debug_code
      ? UseRegisterAtStart(instr->index())
      : UseRegisterOrConstantAtStart(instr->index());
  LOperand* value = FLAG_debug_code
      ? UseRegisterAtStart(instr->value())
      : UseRegisterOrConstantAtStart(instr->value());
  LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), rsi) : NULL;
  LInstruction* result = new(zone()) LSeqStringSetChar(context, string,
                                                       index, value);
  if (FLAG_debug_code) {
    result = MarkAsCall(result, instr);
  }
  return result;
1789 1790 1791
}


1792
LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
  if (!FLAG_debug_code && instr->skip_check()) return NULL;
  LOperand* index = UseRegisterOrConstantAtStart(instr->index());
  LOperand* length = !index->IsConstantOperand()
      ? UseOrConstantAtStart(instr->length())
      : UseAtStart(instr->length());
  LInstruction* result = new(zone()) LBoundsCheck(index, length);
  if (!FLAG_debug_code || !instr->skip_check()) {
    result = AssignEnvironment(result);
  }
  return result;
1803 1804 1805
}


1806 1807 1808 1809 1810 1811 1812
LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
    HBoundsCheckBaseIndexInformation* instr) {
  UNREACHABLE();
  return NULL;
}


1813 1814 1815 1816 1817 1818 1819
LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
  // The control instruction marking the end of a block that completed
  // abruptly (e.g., threw an exception).  There is nothing specific to do.
  return NULL;
}


1820 1821 1822 1823 1824
LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
  return NULL;
}


1825 1826 1827 1828 1829 1830 1831 1832
LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
  // All HForceRepresentation instructions should be eliminated in the
  // representation change phase of Hydrogen.
  UNREACHABLE();
  return NULL;
}


1833
LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1834 1835
  Representation from = instr->from();
  Representation to = instr->to();
1836
  HValue* val = instr->value();
1837 1838
  if (from.IsSmi()) {
    if (to.IsTagged()) {
1839
      LOperand* value = UseRegister(val);
1840 1841 1842 1843
      return DefineSameAsFirst(new(zone()) LDummyUse(value));
    }
    from = Representation::Tagged();
  }
1844 1845
  if (from.IsTagged()) {
    if (to.IsDouble()) {
1846 1847 1848 1849
      LOperand* value = UseRegister(val);
      LInstruction* result = DefineAsRegister(new(zone()) LNumberUntagD(value));
      if (!val->representation().IsSmi()) result = AssignEnvironment(result);
      return result;
1850
    } else if (to.IsSmi()) {
1851
      LOperand* value = UseRegister(val);
1852 1853 1854
      if (val->type().IsSmi()) {
        return DefineSameAsFirst(new(zone()) LDummyUse(value));
      }
1855
      return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
1856 1857
    } else {
      ASSERT(to.IsInteger32());
1858
      if (val->type().IsSmi() || val->representation().IsSmi()) {
1859
        LOperand* value = UseRegister(val);
1860
        return DefineSameAsFirst(new(zone()) LSmiUntag(value, false));
1861
      } else {
1862
        LOperand* value = UseRegister(val);
1863 1864
        bool truncating = instr->CanTruncateToInt32();
        LOperand* xmm_temp = truncating ? NULL : FixedTemp(xmm1);
1865
        LInstruction* result =
1866
            DefineSameAsFirst(new(zone()) LTaggedToI(value, xmm_temp));
1867
        if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1868
        return result;
1869 1870 1871 1872
      }
    }
  } else if (from.IsDouble()) {
    if (to.IsTagged()) {
1873
      info()->MarkAsDeferredCalling();
1874
      LOperand* value = UseRegister(val);
1875 1876
      LOperand* temp = TempRegister();
      LUnallocated* result_temp = TempRegister();
1877
      LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
1878
      return AssignPointerMap(Define(result, result_temp));
1879
    } else if (to.IsSmi()) {
1880
      LOperand* value = UseRegister(val);
1881 1882
      return AssignEnvironment(
          DefineAsRegister(new(zone()) LDoubleToSmi(value)));
1883 1884
    } else {
      ASSERT(to.IsInteger32());
1885
      LOperand* value = UseRegister(val);
1886
      LInstruction* result = DefineAsRegister(new(zone()) LDoubleToI(value));
1887
      if (!instr->CanTruncateToInt32()) result = AssignEnvironment(result);
1888
      return result;
1889 1890
    }
  } else if (from.IsInteger32()) {
1891
    info()->MarkAsDeferredCalling();
1892
    if (to.IsTagged()) {
1893
      if (!instr->CheckFlag(HValue::kCanOverflow)) {
1894
        LOperand* value = UseRegister(val);
1895 1896
        return DefineAsRegister(new(zone()) LSmiTag(value));
      } else if (val->CheckFlag(HInstruction::kUint32)) {
1897
        LOperand* value = UseRegister(val);
1898 1899 1900
        LOperand* temp1 = TempRegister();
        LOperand* temp2 = FixedTemp(xmm1);
        LNumberTagU* result = new(zone()) LNumberTagU(value, temp1, temp2);
1901
        return AssignPointerMap(DefineSameAsFirst(result));
1902
      } else {
1903
        LOperand* value = UseRegister(val);
1904 1905 1906
        LOperand* temp1 = SmiValuesAre32Bits() ? NULL : TempRegister();
        LOperand* temp2 = SmiValuesAre32Bits() ? NULL : FixedTemp(xmm1);
        LNumberTagI* result = new(zone()) LNumberTagI(value, temp1, temp2);
1907
        return AssignPointerMap(DefineSameAsFirst(result));
1908
      }
1909 1910
    } else if (to.IsSmi()) {
      LOperand* value = UseRegister(val);
1911 1912 1913
      LInstruction* result = DefineAsRegister(new(zone()) LSmiTag(value));
      if (instr->CheckFlag(HValue::kCanOverflow)) {
        result = AssignEnvironment(result);
1914
      }
1915
      return result;
1916
    } else {
1917 1918
      ASSERT(to.IsDouble());
      if (val->CheckFlag(HInstruction::kUint32)) {
1919
        return DefineAsRegister(new(zone()) LUint32ToDouble(UseRegister(val)));
1920
      } else {
1921
        LOperand* value = Use(val);
1922 1923
        return DefineAsRegister(new(zone()) LInteger32ToDouble(value));
      }
1924 1925 1926
    }
  }
  UNREACHABLE();
1927 1928 1929 1930
  return NULL;
}


1931
LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
1932
  LOperand* value = UseRegisterAtStart(instr->value());
1933
  LInstruction* result = new(zone()) LCheckNonSmi(value);
1934 1935 1936
  if (!instr->value()->type().IsHeapObject()) {
    result = AssignEnvironment(result);
  }
1937
  return result;
1938 1939 1940
}


1941 1942 1943 1944 1945 1946
LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
  return AssignEnvironment(new(zone()) LCheckSmi(value));
}


1947
LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1948
  LOperand* value = UseRegisterAtStart(instr->value());
1949
  LCheckInstanceType* result = new(zone()) LCheckInstanceType(value);
1950
  return AssignEnvironment(result);
1951 1952 1953
}


1954
LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
1955
  LOperand* value = UseRegisterAtStart(instr->value());
1956
  return AssignEnvironment(new(zone()) LCheckValue(value));
1957 1958 1959
}


1960
LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
1961 1962 1963 1964 1965 1966
  if (instr->IsStabilityCheck()) return new(zone()) LCheckMaps;
  LOperand* value = UseRegisterAtStart(instr->value());
  LInstruction* result = AssignEnvironment(new(zone()) LCheckMaps(value));
  if (instr->HasMigrationTarget()) {
    info()->MarkAsDeferredCalling();
    result = AssignPointerMap(result);
1967 1968
  }
  return result;
1969 1970 1971
}


1972 1973 1974 1975 1976
LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
  HValue* value = instr->value();
  Representation input_rep = value->representation();
  LOperand* reg = UseRegister(value);
  if (input_rep.IsDouble()) {
1977
    return DefineAsRegister(new(zone()) LClampDToUint8(reg));
1978
  } else if (input_rep.IsInteger32()) {
1979
    return DefineSameAsFirst(new(zone()) LClampIToUint8(reg));
1980
  } else {
1981
    ASSERT(input_rep.IsSmiOrTagged());
1982 1983
    // Register allocator doesn't (yet) support allocation of double
    // temps. Reserve xmm1 explicitly.
1984 1985
    LClampTToUint8* result = new(zone()) LClampTToUint8(reg,
                                                        FixedTemp(xmm1));
1986 1987 1988 1989 1990
    return AssignEnvironment(DefineSameAsFirst(result));
  }
}


1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
  HValue* value = instr->value();
  ASSERT(value->representation().IsDouble());
  return DefineAsRegister(new(zone()) LDoubleBits(UseRegister(value)));
}


LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
  LOperand* lo = UseRegister(instr->lo());
  LOperand* hi = UseRegister(instr->hi());
  return DefineAsRegister(new(zone()) LConstructDouble(hi, lo));
}


2005
LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2006
  LOperand* context = info()->IsStub() ? UseFixed(instr->context(), rsi) : NULL;
2007
  LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2008 2009
  return new(zone()) LReturn(
      UseFixed(instr->value(), rax), context, parameter_count);
2010 2011 2012 2013
}


LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2014
  Representation r = instr->representation();
2015 2016 2017
  if (r.IsSmi()) {
    return DefineAsRegister(new(zone()) LConstantS);
  } else if (r.IsInteger32()) {
2018
    return DefineAsRegister(new(zone()) LConstantI);
2019
  } else if (r.IsDouble()) {
2020
    LOperand* temp = TempRegister();
2021
    return DefineAsRegister(new(zone()) LConstantD(temp));
2022 2023
  } else if (r.IsExternal()) {
    return DefineAsRegister(new(zone()) LConstantE);
2024
  } else if (r.IsTagged()) {
2025
    return DefineAsRegister(new(zone()) LConstantT);
2026 2027 2028 2029
  } else {
    UNREACHABLE();
    return NULL;
  }
2030 2031 2032
}


2033
LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2034
  LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
2035
  return instr->RequiresHoleCheck()
2036 2037
      ? AssignEnvironment(DefineAsRegister(result))
      : DefineAsRegister(result);
2038 2039 2040
}


2041
LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2042
  LOperand* context = UseFixed(instr->context(), rsi);
2043
  LOperand* global_object = UseFixed(instr->global_object(), rax);
2044 2045
  LLoadGlobalGeneric* result =
      new(zone()) LLoadGlobalGeneric(context, global_object);
2046 2047 2048 2049
  return MarkAsCall(DefineFixed(result, rax), instr);
}


2050
LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2051 2052 2053 2054
  LOperand* value = UseRegister(instr->value());
  // Use a temp to avoid reloading the cell value address in the case where
  // we perform a hole check.
  return instr->RequiresHoleCheck()
2055 2056
      ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
      : new(zone()) LStoreGlobalCell(value, NULL);
2057
}
2058 2059


2060
LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
whesse@chromium.org's avatar
whesse@chromium.org committed
2061
  LOperand* context = UseRegisterAtStart(instr->value());
2062 2063
  LInstruction* result =
      DefineAsRegister(new(zone()) LLoadContextSlot(context));
2064 2065 2066 2067
  if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
    result = AssignEnvironment(result);
  }
  return result;
2068 2069 2070
}


2071
LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
whesse@chromium.org's avatar
whesse@chromium.org committed
2072
  LOperand* context;
2073
  LOperand* value;
2074
  LOperand* temp;
2075
  context = UseRegister(instr->context());
2076 2077
  if (instr->NeedsWriteBarrier()) {
    value = UseTempRegister(instr->value());
2078
    temp = TempRegister();
2079 2080
  } else {
    value = UseRegister(instr->value());
2081
    temp = NULL;
2082
  }
2083
  LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
2084 2085 2086 2087
  if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
    result = AssignEnvironment(result);
  }
  return result;
2088 2089 2090
}


2091
LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2092 2093 2094 2095 2096 2097 2098 2099
  // Use the special mov rax, moffs64 encoding for external
  // memory accesses with 64-bit word-sized values.
  if (instr->access().IsExternalMemory() &&
      instr->access().offset() == 0 &&
      (instr->access().representation().IsSmi() ||
       instr->access().representation().IsTagged() ||
       instr->access().representation().IsHeapObject() ||
       instr->access().representation().IsExternal())) {
2100 2101 2102
    LOperand* obj = UseRegisterOrConstantAtStart(instr->object());
    return DefineFixed(new(zone()) LLoadNamedField(obj), rax);
  }
2103
  LOperand* obj = UseRegisterAtStart(instr->object());
2104
  return DefineAsRegister(new(zone()) LLoadNamedField(obj));
2105 2106 2107 2108
}


LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2109
  LOperand* context = UseFixed(instr->context(), rsi);
2110
  LOperand* object = UseFixed(instr->object(), rax);
2111
  LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(context, object);
2112
  return MarkAsCall(DefineFixed(result, rax), instr);
2113 2114 2115 2116 2117
}


LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
    HLoadFunctionPrototype* instr) {
2118
  return AssignEnvironment(DefineAsRegister(
2119
      new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
2120 2121 2122
}


2123 2124 2125 2126 2127
LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
  return DefineAsRegister(new(zone()) LLoadRoot);
}


2128
void LChunkBuilder::FindDehoistedKeyDefinitions(HValue* candidate) {
2129 2130 2131 2132 2133
  // We sign extend the dehoisted key at the definition point when the pointer
  // size is 64-bit. For x32 port, we sign extend the dehoisted key at the use
  // points and should not invoke this function. We can't use STATIC_ASSERT
  // here as the pointer size is 32-bit for x32.
  ASSERT(kPointerSize == kInt64Size);
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
  BitVector* dehoisted_key_ids = chunk_->GetDehoistedKeyIds();
  if (dehoisted_key_ids->Contains(candidate->id())) return;
  dehoisted_key_ids->Add(candidate->id());
  if (!candidate->IsPhi()) return;
  for (int i = 0; i < candidate->OperandCount(); ++i) {
    FindDehoistedKeyDefinitions(candidate->OperandAt(i));
  }
}


2144
LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2145 2146 2147 2148
  ASSERT((kPointerSize == kInt64Size &&
          instr->key()->representation().IsInteger32()) ||
         (kPointerSize == kInt32Size &&
          instr->key()->representation().IsSmiOrInteger32()));
2149
  ElementsKind elements_kind = instr->elements_kind();
2150
  LOperand* key = NULL;
2151
  LInstruction* result = NULL;
2152

2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
  if (kPointerSize == kInt64Size) {
    key = UseRegisterOrConstantAtStart(instr->key());
  } else {
    bool clobbers_key = ExternalArrayOpRequiresTemp(
        instr->key()->representation(), elements_kind);
    key = clobbers_key
        ? UseTempRegister(instr->key())
        : UseRegisterOrConstantAtStart(instr->key());
  }

2163
  if ((kPointerSize == kInt64Size) && instr->IsDehoisted()) {
2164 2165 2166
    FindDehoistedKeyDefinitions(instr->key());
  }

2167
  if (!instr->is_typed_elements()) {
2168
    LOperand* obj = UseRegisterAtStart(instr->elements());
2169
    result = DefineAsRegister(new(zone()) LLoadKeyed(obj, key));
2170
  } else {
2171 2172
    ASSERT(
        (instr->representation().IsInteger32() &&
2173
         !(IsDoubleOrFloatElementsKind(elements_kind))) ||
2174
        (instr->representation().IsDouble() &&
2175
         (IsDoubleOrFloatElementsKind(elements_kind))));
2176
    LOperand* backing_store = UseRegister(instr->elements());
2177
    result = DefineAsRegister(new(zone()) LLoadKeyed(backing_store, key));
2178
  }
2179

2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
  if ((instr->is_external() || instr->is_fixed_typed_array()) ?
      // see LCodeGen::DoLoadKeyedExternalArray
      ((elements_kind == EXTERNAL_UINT32_ELEMENTS ||
        elements_kind == UINT32_ELEMENTS) &&
       !instr->CheckFlag(HInstruction::kUint32)) :
      // see LCodeGen::DoLoadKeyedFixedDoubleArray and
      // LCodeGen::DoLoadKeyedFixedArray
      instr->RequiresHoleCheck()) {
    result = AssignEnvironment(result);
  }
  return result;
2191 2192 2193
}


2194
LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2195
  LOperand* context = UseFixed(instr->context(), rsi);
2196 2197 2198
  LOperand* object = UseFixed(instr->object(), rdx);
  LOperand* key = UseFixed(instr->key(), rax);

2199 2200
  LLoadKeyedGeneric* result =
      new(zone()) LLoadKeyedGeneric(context, object, key);
2201
  return MarkAsCall(DefineFixed(result, rax), instr);
2202 2203 2204
}


2205
LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2206
  ElementsKind elements_kind = instr->elements_kind();
2207

2208
  if ((kPointerSize == kInt64Size) && instr->IsDehoisted()) {
2209 2210 2211
    FindDehoistedKeyDefinitions(instr->key());
  }

2212
  if (!instr->is_typed_elements()) {
2213
    ASSERT(instr->elements()->representation().IsTagged());
2214
    bool needs_write_barrier = instr->NeedsWriteBarrier();
2215
    LOperand* object = NULL;
2216 2217 2218
    LOperand* key = NULL;
    LOperand* val = NULL;

2219 2220
    Representation value_representation = instr->value()->representation();
    if (value_representation.IsDouble()) {
2221
      object = UseRegisterAtStart(instr->elements());
2222
      val = UseRegisterAtStart(instr->value());
2223
      key = UseRegisterOrConstantAtStart(instr->key());
2224
    } else {
2225 2226
      ASSERT(value_representation.IsSmiOrTagged() ||
             value_representation.IsInteger32());
2227
      if (needs_write_barrier) {
2228
        object = UseTempRegister(instr->elements());
2229 2230 2231
        val = UseTempRegister(instr->value());
        key = UseTempRegister(instr->key());
      } else {
2232
        object = UseRegisterAtStart(instr->elements());
2233
        val = UseRegisterOrConstantAtStart(instr->value());
2234
        key = UseRegisterOrConstantAtStart(instr->key());
2235
      }
2236 2237
    }

2238
    return new(zone()) LStoreKeyed(object, key, val);
2239 2240
  }

2241
  ASSERT(
2242 2243 2244 2245 2246 2247 2248 2249
       (instr->value()->representation().IsInteger32() &&
       !IsDoubleOrFloatElementsKind(elements_kind)) ||
       (instr->value()->representation().IsDouble() &&
       IsDoubleOrFloatElementsKind(elements_kind)));
  ASSERT((instr->is_fixed_typed_array() &&
          instr->elements()->representation().IsTagged()) ||
         (instr->is_external() &&
          instr->elements()->representation().IsExternal()));
2250
  bool val_is_temp_register =
2251 2252
      elements_kind == EXTERNAL_UINT8_CLAMPED_ELEMENTS ||
      elements_kind == EXTERNAL_FLOAT32_ELEMENTS ||
2253
      elements_kind == FLOAT32_ELEMENTS;
2254 2255
  LOperand* val = val_is_temp_register ? UseTempRegister(instr->value())
      : UseRegister(instr->value());
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
  LOperand* key = NULL;
  if (kPointerSize == kInt64Size) {
    key = UseRegisterOrConstantAtStart(instr->key());
  } else {
    bool clobbers_key = ExternalArrayOpRequiresTemp(
        instr->key()->representation(), elements_kind);
    key = clobbers_key
        ? UseTempRegister(instr->key())
        : UseRegisterOrConstantAtStart(instr->key());
  }
2266 2267
  LOperand* backing_store = UseRegister(instr->elements());
  return new(zone()) LStoreKeyed(backing_store, key, val);
2268 2269 2270
}


2271
LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2272
  LOperand* context = UseFixed(instr->context(), rsi);
2273 2274 2275 2276 2277 2278 2279 2280
  LOperand* object = UseFixed(instr->object(), rdx);
  LOperand* key = UseFixed(instr->key(), rcx);
  LOperand* value = UseFixed(instr->value(), rax);

  ASSERT(instr->object()->representation().IsTagged());
  ASSERT(instr->key()->representation().IsTagged());
  ASSERT(instr->value()->representation().IsTagged());

2281
  LStoreKeyedGeneric* result =
2282
      new(zone()) LStoreKeyedGeneric(context, object, key, value);
2283
  return MarkAsCall(result, instr);
2284 2285 2286
}


2287 2288
LInstruction* LChunkBuilder::DoTransitionElementsKind(
    HTransitionElementsKind* instr) {
2289
  if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2290 2291 2292
    LOperand* object = UseRegister(instr->object());
    LOperand* new_map_reg = TempRegister();
    LOperand* temp_reg = TempRegister();
2293 2294
    LTransitionElementsKind* result = new(zone()) LTransitionElementsKind(
        object, NULL, new_map_reg, temp_reg);
2295
    return result;
2296
  } else {
2297
    LOperand* object = UseFixed(instr->object(), rax);
2298
    LOperand* context = UseFixed(instr->context(), rsi);
2299
    LTransitionElementsKind* result =
2300
        new(zone()) LTransitionElementsKind(object, context, NULL, NULL);
2301
    return MarkAsCall(result, instr);
2302 2303 2304 2305
  }
}


2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
LInstruction* LChunkBuilder::DoTrapAllocationMemento(
    HTrapAllocationMemento* instr) {
  LOperand* object = UseRegister(instr->object());
  LOperand* temp = TempRegister();
  LTrapAllocationMemento* result =
      new(zone()) LTrapAllocationMemento(object, temp);
  return AssignEnvironment(result);
}


2316
LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2317
  bool is_in_object = instr->access().IsInobject();
2318 2319
  bool is_external_location = instr->access().IsExternalMemory() &&
      instr->access().offset() == 0;
2320
  bool needs_write_barrier = instr->NeedsWriteBarrier();
2321 2322
  bool needs_write_barrier_for_map = instr->has_transition() &&
      instr->NeedsWriteBarrierForMap();
2323 2324 2325

  LOperand* obj;
  if (needs_write_barrier) {
2326
    obj = is_in_object
2327 2328
        ? UseRegister(instr->object())
        : UseTempRegister(instr->object());
2329 2330 2331
  } else if (is_external_location) {
    ASSERT(!is_in_object);
    ASSERT(!needs_write_barrier);
2332
    ASSERT(!needs_write_barrier_for_map);
2333
    obj = UseRegisterOrConstant(instr->object());
2334
  } else {
2335 2336 2337
    obj = needs_write_barrier_for_map
        ? UseRegister(instr->object())
        : UseRegisterAtStart(instr->object());
2338
  }
2339

2340
  bool can_be_constant = instr->value()->IsConstant() &&
2341
      HConstant::cast(instr->value())->NotInNewSpace() &&
2342
      !instr->field_representation().IsDouble();
2343

2344 2345 2346
  LOperand* val;
  if (needs_write_barrier) {
    val = UseTempRegister(instr->value());
2347 2348
  } else if (is_external_location) {
    val = UseFixed(instr->value(), rax);
2349
  } else if (can_be_constant) {
2350
    val = UseRegisterOrConstant(instr->value());
2351
  } else if (instr->field_representation().IsSmi()) {
2352
    val = UseRegister(instr->value());
2353
  } else if (instr->field_representation().IsDouble()) {
2354
    val = UseRegisterAtStart(instr->value());
2355 2356 2357
  } else {
    val = UseRegister(instr->value());
  }
2358 2359 2360

  // We only need a scratch register if we have a write barrier or we
  // have a store into the properties array (not in-object-property).
2361 2362
  LOperand* temp = (!is_in_object || needs_write_barrier ||
      needs_write_barrier_for_map) ? TempRegister() : NULL;
2363

2364
  return new(zone()) LStoreNamedField(obj, val, temp);
2365 2366 2367 2368
}


LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2369
  LOperand* context = UseFixed(instr->context(), rsi);
2370 2371 2372
  LOperand* object = UseFixed(instr->object(), rdx);
  LOperand* value = UseFixed(instr->value(), rax);

2373 2374
  LStoreNamedGeneric* result =
      new(zone()) LStoreNamedGeneric(context, object, value);
2375
  return MarkAsCall(result, instr);
2376 2377 2378
}


2379
LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2380
  LOperand* context = UseFixed(instr->context(), rsi);
2381 2382
  LOperand* left = UseFixed(instr->left(), rdx);
  LOperand* right = UseFixed(instr->right(), rax);
2383 2384
  return MarkAsCall(
      DefineFixed(new(zone()) LStringAdd(context, left, right), rax), instr);
2385 2386 2387
}


2388
LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2389 2390
  LOperand* string = UseTempRegister(instr->string());
  LOperand* index = UseTempRegister(instr->index());
2391 2392 2393
  LOperand* context = UseAny(instr->context());
  LStringCharCodeAt* result =
      new(zone()) LStringCharCodeAt(context, string, index);
2394
  return AssignPointerMap(DefineAsRegister(result));
2395 2396 2397
}


2398 2399
LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
  LOperand* char_code = UseRegister(instr->value());
2400 2401 2402
  LOperand* context = UseAny(instr->context());
  LStringCharFromCode* result =
      new(zone()) LStringCharFromCode(context, char_code);
2403 2404 2405 2406
  return AssignPointerMap(DefineAsRegister(result));
}


2407 2408
LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
  info()->MarkAsDeferredCalling();
2409
  LOperand* context = UseAny(instr->context());
2410 2411 2412
  LOperand* size = instr->size()->IsConstant()
      ? UseConstant(instr->size())
      : UseTempRegister(instr->size());
2413
  LOperand* temp = TempRegister();
2414
  LAllocate* result = new(zone()) LAllocate(context, size, temp);
2415 2416 2417 2418
  return AssignPointerMap(DefineAsRegister(result));
}


2419
LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2420 2421 2422
  LOperand* context = UseFixed(instr->context(), rsi);
  LRegExpLiteral* result = new(zone()) LRegExpLiteral(context);
  return MarkAsCall(DefineFixed(result, rax), instr);
2423 2424 2425 2426
}


LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2427 2428 2429
  LOperand* context = UseFixed(instr->context(), rsi);
  LFunctionLiteral* result = new(zone()) LFunctionLiteral(context);
  return MarkAsCall(DefineFixed(result, rax), instr);
2430 2431 2432 2433
}


LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2434
  ASSERT(argument_count_ == 0);
2435 2436
  allocator_->MarkAsOsrEntry();
  current_block_->last_environment()->set_ast_id(instr->ast_id());
2437
  return AssignEnvironment(new(zone()) LOsrEntry);
2438 2439 2440 2441
}


LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2442
  LParameter* result = new(zone()) LParameter;
2443
  if (instr->kind() == HParameter::STACK_PARAMETER) {
2444 2445 2446 2447 2448
    int spill_index = chunk()->GetParameterStackSlot(instr->index());
    return DefineAsSpilled(result, spill_index);
  } else {
    ASSERT(info()->IsStub());
    CodeStubInterfaceDescriptor* descriptor =
2449
        info()->code_stub()->GetInterfaceDescriptor();
2450
    int index = static_cast<int>(instr->index());
2451
    Register reg = descriptor->GetParameterRegister(index);
2452 2453
    return DefineFixed(result, reg);
  }
2454 2455 2456 2457
}


LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
  // Use an index that corresponds to the location in the unoptimized frame,
  // which the optimized frame will subsume.
  int env_index = instr->index();
  int spill_index = 0;
  if (instr->environment()->is_parameter_index(env_index)) {
    spill_index = chunk()->GetParameterStackSlot(env_index);
  } else {
    spill_index = env_index - instr->environment()->first_local_index();
    if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
      Abort(kTooManySpillSlotsNeededForOSR);
      spill_index = 0;
    }
2470
  }
2471
  return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2472 2473 2474 2475
}


LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2476 2477 2478
  LOperand* context = UseFixed(instr->context(), rsi);
  LCallStub* result = new(zone()) LCallStub(context);
  return MarkAsCall(DefineFixed(result, rax), instr);
2479 2480 2481 2482
}


LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2483 2484 2485 2486
  // There are no real uses of the arguments object.
  // arguments.length and element access are supported directly on
  // stack arguments, and any real arguments object use causes a bailout.
  // So this value is never used.
2487 2488 2489 2490
  return NULL;
}


2491
LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2492
  instr->ReplayEnvironment(current_block_->last_environment());
2493

2494 2495 2496 2497 2498
  // There are no real uses of a captured object.
  return NULL;
}


2499
LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2500
  info()->MarkAsRequiresFrame();
2501
  LOperand* args = UseRegister(instr->arguments());
2502 2503 2504 2505 2506 2507 2508 2509 2510
  LOperand* length;
  LOperand* index;
  if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
    length = UseRegisterOrConstant(instr->length());
    index = UseOrConstant(instr->index());
  } else {
    length = UseTempRegister(instr->length());
    index = Use(instr->index());
  }
2511
  return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2512 2513 2514
}


2515 2516
LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
  LOperand* object = UseFixed(instr->value(), rax);
2517
  LToFastProperties* result = new(zone()) LToFastProperties(object);
2518 2519 2520 2521
  return MarkAsCall(DefineFixed(result, rax), instr);
}


2522
LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2523 2524 2525
  LOperand* context = UseFixed(instr->context(), rsi);
  LOperand* value = UseAtStart(instr->value());
  LTypeof* result = new(zone()) LTypeof(context, value);
2526
  return MarkAsCall(DefineFixed(result, rax), instr);
2527 2528 2529
}


2530
LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2531
  return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2532 2533
}

2534

2535 2536
LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
    HIsConstructCallAndBranch* instr) {
2537
  return new(zone()) LIsConstructCallAndBranch(TempRegister());
2538 2539 2540
}


2541
LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2542
  instr->ReplayEnvironment(current_block_->last_environment());
2543 2544 2545 2546 2547
  return NULL;
}


LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2548
  info()->MarkAsDeferredCalling();
2549
  if (instr->is_function_entry()) {
2550 2551
    LOperand* context = UseFixed(instr->context(), rsi);
    return MarkAsCall(new(zone()) LStackCheck(context), instr);
2552 2553
  } else {
    ASSERT(instr->is_backwards_branch());
2554 2555 2556
    LOperand* context = UseAny(instr->context());
    return AssignEnvironment(
        AssignPointerMap(new(zone()) LStackCheck(context)));
2557
  }
2558 2559 2560 2561
}


LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2562
  HEnvironment* outer = current_block_->last_environment();
2563
  outer->set_ast_id(instr->ReturnId());
2564 2565
  HConstant* undefined = graph()->GetConstantUndefined();
  HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2566
                                               instr->arguments_count(),
2567
                                               instr->function(),
2568
                                               undefined,
2569
                                               instr->inlining_kind());
2570 2571 2572
  // Only replay binding of arguments object if it wasn't removed from graph.
  if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
    inner->Bind(instr->arguments_var(), instr->arguments_object());
2573
  }
2574
  inner->set_entry(instr);
2575 2576
  current_block_->UpdateEnvironment(inner);
  chunk_->AddInlinedClosure(instr->closure());
2577 2578 2579 2580 2581
  return NULL;
}


LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2582 2583 2584 2585
  LInstruction* pop = NULL;

  HEnvironment* env = current_block_->last_environment();

2586
  if (env->entry()->arguments_pushed()) {
2587 2588
    int argument_count = env->arguments_environment()->parameter_count();
    pop = new(zone()) LDrop(argument_count);
2589
    ASSERT(instr->argument_delta() == -argument_count);
2590 2591
  }

2592 2593
  HEnvironment* outer = current_block_->last_environment()->
      DiscardInlined(false);
2594
  current_block_->UpdateEnvironment(outer);
2595 2596

  return pop;
2597 2598
}

2599

2600
LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2601
  LOperand* context = UseFixed(instr->context(), rsi);
2602
  LOperand* object = UseFixed(instr->enumerable(), rax);
2603
  LForInPrepareMap* result = new(zone()) LForInPrepareMap(context, object);
2604 2605 2606 2607 2608 2609 2610
  return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
  LOperand* map = UseRegister(instr->map());
  return AssignEnvironment(DefineAsRegister(
2611
      new(zone()) LForInCacheArray(map)));
2612 2613 2614 2615 2616 2617
}


LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
  LOperand* map = UseRegisterAtStart(instr->map());
2618
  return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2619 2620 2621 2622 2623 2624
}


LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
  LOperand* object = UseRegister(instr->object());
  LOperand* index = UseTempRegister(instr->index());
2625 2626 2627
  LLoadFieldByIndex* load = new(zone()) LLoadFieldByIndex(object, index);
  LInstruction* result = DefineSameAsFirst(load);
  return AssignPointerMap(result);
2628 2629 2630
}


2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
  LOperand* context = UseRegisterAtStart(instr->context());
  return new(zone()) LStoreFrameContext(context);
}


LInstruction* LChunkBuilder::DoAllocateBlockContext(
    HAllocateBlockContext* instr) {
  LOperand* context = UseFixed(instr->context(), rsi);
  LOperand* function = UseRegisterAtStart(instr->function());
  LAllocateBlockContext* result =
      new(zone()) LAllocateBlockContext(context, function);
  return MarkAsCall(DefineFixed(result, rsi), instr);
}


2647
} }  // namespace v8::internal
2648 2649

#endif  // V8_TARGET_ARCH_X64