lithium-mips.cc 84.1 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "v8.h"

#include "lithium-allocator-inl.h"
#include "mips/lithium-mips.h"
#include "mips/lithium-codegen-mips.h"

namespace v8 {
namespace internal {

#define DEFINE_COMPILE(type)                            \
  void L##type::CompileToNative(LCodeGen* generator) {  \
    generator->Do##type(this);                          \
  }
LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
#undef DEFINE_COMPILE

#ifdef DEBUG
void LInstruction::VerifyCall() {
  // 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.
  ASSERT(Output() == NULL ||
         LUnallocated::cast(Output())->HasFixedPolicy() ||
         !LUnallocated::cast(Output())->HasRegisterPolicy());
  for (UseIterator it(this); !it.Done(); it.Advance()) {
    LUnallocated* operand = LUnallocated::cast(it.Current());
    ASSERT(operand->HasFixedPolicy() ||
           operand->IsUsedAtStart());
  }
  for (TempIterator it(this); !it.Done(); it.Advance()) {
    LUnallocated* operand = LUnallocated::cast(it.Current());
    ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
  }
}
#endif


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

  PrintOutputOperandTo(stream);

  PrintDataTo(stream);

  if (HasEnvironment()) {
    stream->Add(" ");
    environment()->PrintTo(stream);
  }

  if (HasPointerMap()) {
    stream->Add(" ");
    pointer_map()->PrintTo(stream);
  }
}


85
void LInstruction::PrintDataTo(StringStream* stream) {
86
  stream->Add("= ");
87
  for (int i = 0; i < InputCount(); i++) {
88
    if (i > 0) stream->Add(" ");
89 90 91 92 93
    if (InputAt(i) == NULL) {
      stream->Add("NULL");
    } else {
      InputAt(i)->PrintTo(stream);
    }
94 95 96 97
  }
}


98 99
void LInstruction::PrintOutputOperandTo(StringStream* stream) {
  if (HasResult()) result()->PrintTo(stream);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
}


void LLabel::PrintDataTo(StringStream* stream) {
  LGap::PrintDataTo(stream);
  LLabel* rep = replacement();
  if (rep != NULL) {
    stream->Add(" Dead block replaced with B%d", rep->block_id());
  }
}


bool LGap::IsRedundant() const {
  for (int i = 0; i < 4; i++) {
    if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
      return false;
    }
  }

  return true;
}


void LGap::PrintDataTo(StringStream* stream) {
  for (int i = 0; i < 4; i++) {
    stream->Add("(");
    if (parallel_moves_[i] != NULL) {
      parallel_moves_[i]->PrintDataTo(stream);
    }
    stream->Add(") ");
  }
}


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";
    case Token::BIT_AND: return "bit-and-t";
    case Token::BIT_OR: return "bit-or-t";
    case Token::BIT_XOR: return "bit-xor-t";
158
    case Token::ROR: return "ror-t";
159 160 161 162 163 164 165 166 167 168
    case Token::SHL: return "sll-t";
    case Token::SAR: return "sra-t";
    case Token::SHR: return "srl-t";
    default:
      UNREACHABLE();
      return NULL;
  }
}


169 170 171 172 173
bool LGoto::HasInterestingComment(LCodeGen* gen) const {
  return !gen->IsNextEmittedBlock(block_id());
}


174 175 176 177 178 179 180
void LGoto::PrintDataTo(StringStream* stream) {
  stream->Add("B%d", block_id());
}


void LBranch::PrintDataTo(StringStream* stream) {
  stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
181
  value()->PrintTo(stream);
182 183 184
}


185 186 187 188
LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
  return new(zone()) LDebugBreak();
}

189

190
void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
191
  stream->Add("if ");
192
  left()->PrintTo(stream);
193
  stream->Add(" %s ", Token::String(op()));
194
  right()->PrintTo(stream);
195 196 197 198 199 200
  stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
}


void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_object(");
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 LIsStringAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_string(");
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 LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_smi(");
215
  value()->PrintTo(stream);
216 217 218 219 220 221
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_undetectable(");
222
  value()->PrintTo(stream);
223 224 225 226
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


227 228
void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if string_compare(");
229 230
  left()->PrintTo(stream);
  right()->PrintTo(stream);
231 232 233 234
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


235 236
void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if has_instance_type(");
237
  value()->PrintTo(stream);
238 239 240 241 242 243
  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(");
244
  value()->PrintTo(stream);
245 246 247 248 249 250
  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(");
251
  value()->PrintTo(stream);
252 253 254 255 256 257 258 259 260
  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 ");
261
  value()->PrintTo(stream);
262 263 264 265 266 267
  stream->Add(" == \"%s\" then B%d else B%d",
              *hydrogen()->type_literal()->ToCString(),
              true_block_id(), false_block_id());
}


268 269 270 271 272 273 274
void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
  stream->Add(" = ");
  base_object()->PrintTo(stream);
  stream->Add(" + %d", offset());
}


275 276 277 278 279
void LCallConstantFunction::PrintDataTo(StringStream* stream) {
  stream->Add("#%d / ", arity());
}


280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
ExternalReference LLinkObjectInList::GetReference(Isolate* isolate) {
  switch (hydrogen()->known_list()) {
    case HLinkObjectInList::ALLOCATION_SITE_LIST:
      return ExternalReference::allocation_sites_list_address(isolate);
  }

  UNREACHABLE();
  // Return a dummy value
  return ExternalReference::isolate_address(isolate);
}


void LLinkObjectInList::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
  stream->Add(" offset %d", hydrogen()->store_field().offset());
}


298
void LLoadContextSlot::PrintDataTo(StringStream* stream) {
299
  context()->PrintTo(stream);
300 301 302 303 304
  stream->Add("[%d]", slot_index());
}


void LStoreContextSlot::PrintDataTo(StringStream* stream) {
305
  context()->PrintTo(stream);
306
  stream->Add("[%d] <- ", slot_index());
307
  value()->PrintTo(stream);
308 309 310 311 312
}


void LInvokeFunction::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
313
  function()->PrintTo(stream);
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  stream->Add(" #%d / ", arity());
}


void LCallKeyed::PrintDataTo(StringStream* stream) {
  stream->Add("[a2] #%d / ", arity());
}


void LCallNamed::PrintDataTo(StringStream* stream) {
  SmartArrayPointer<char> name_string = name()->ToCString();
  stream->Add("%s #%d / ", *name_string, arity());
}


void LCallGlobal::PrintDataTo(StringStream* stream) {
  SmartArrayPointer<char> name_string = name()->ToCString();
  stream->Add("%s #%d / ", *name_string, arity());
}


void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
  stream->Add("#%d / ", arity());
}


void LCallNew::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
342
  constructor()->PrintTo(stream);
343 344 345 346
  stream->Add(" #%d / ", arity());
}


347 348 349 350
void LCallNewArray::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
  constructor()->PrintTo(stream);
  stream->Add(" #%d / ", arity());
351
  ElementsKind kind = hydrogen()->elements_kind();
352 353 354 355
  stream->Add(" (%s) ", ElementsKindToString(kind));
}


356 357 358 359 360 361 362 363 364 365 366
void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
  arguments()->PrintTo(stream);
  stream->Add(" length ");
  length()->PrintTo(stream);
  stream->Add(" index ");
  index()->PrintTo(stream);
}


void LStoreNamedField::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
367
  hydrogen()->access().PrintTo(stream);
368 369 370 371 372 373 374 375 376 377 378 379 380 381
  stream->Add(" <- ");
  value()->PrintTo(stream);
}


void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
  stream->Add(".");
  stream->Add(*String::cast(*name())->ToCString());
  stream->Add(" <- ");
  value()->PrintTo(stream);
}


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


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

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


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


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


429
int LPlatformChunk::GetNextSpillIndex(bool is_double) {
430 431 432 433 434 435
  // Skip a slot if for a double-width slot.
  if (is_double) spill_slot_count_++;
  return spill_slot_count_++;
}


436
LOperand* LPlatformChunk::GetNextSpillSlot(bool is_double)  {
437 438
  int index = GetNextSpillIndex(is_double);
  if (is_double) {
439
    return LDoubleStackSlot::Create(index, zone());
440
  } else {
441
    return LStackSlot::Create(index, zone());
442 443 444 445
  }
}


446
LPlatformChunk* LChunkBuilder::Build() {
447
  ASSERT(is_unused());
448
  chunk_ = new(zone()) LPlatformChunk(info(), graph());
449
  LPhase phase("L_Building chunk", chunk_);
450 451 452 453 454 455 456 457 458 459 460 461 462
  status_ = BUILDING;
  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_;
}


463 464
void LCodeGen::Abort(const char* reason) {
  info()->set_bailout_reason(reason);
465 466 467 468 469
  status_ = ABORTED;
}


LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
470 471
  return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
                                  Register::ToAllocationIndex(reg));
472 473 474 475
}


LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
476 477
  return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
                                  DoubleRegister::ToAllocationIndex(reg));
478 479 480 481 482 483 484 485 486 487 488 489 490 491
}


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


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


LOperand* LChunkBuilder::UseRegister(HValue* value) {
492
  return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
493 494 495 496 497
}


LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
  return Use(value,
498 499
             new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
                                      LUnallocated::USED_AT_START));
500 501 502 503
}


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


LOperand* LChunkBuilder::Use(HValue* value) {
509
  return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
510 511 512 513
}


LOperand* LChunkBuilder::UseAtStart(HValue* value) {
514
  return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
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 543 544 545 546
                                     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);
}


547 548 549 550 551
LOperand* LChunkBuilder::UseConstant(HValue* value) {
  return chunk_->DefineConstantOperand(HConstant::cast(value));
}


552 553 554
LOperand* LChunkBuilder::UseAny(HValue* value) {
  return value->IsConstant()
      ? chunk_->DefineConstantOperand(HConstant::cast(value))
555
      :  Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
556 557 558 559 560 561 562 563
}


LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
  if (value->EmitAtUses()) {
    HInstruction* instr = HInstruction::cast(value);
    VisitInstruction(instr);
  }
564
  operand->set_virtual_register(value->id());
565 566 567 568 569 570 571
  return operand;
}


template<int I, int T>
LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
                                    LUnallocated* result) {
572
  result->set_virtual_register(current_instruction_->id());
573 574 575 576 577 578 579 580
  instr->set_result(result);
  return instr;
}


template<int I, int T>
LInstruction* LChunkBuilder::DefineAsRegister(
    LTemplateInstruction<1, I, T>* instr) {
581 582
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
583 584 585 586 587 588
}


template<int I, int T>
LInstruction* LChunkBuilder::DefineAsSpilled(
    LTemplateInstruction<1, I, T>* instr, int index) {
589 590
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
591 592 593 594 595 596
}


template<int I, int T>
LInstruction* LChunkBuilder::DefineSameAsFirst(
    LTemplateInstruction<1, I, T>* instr) {
597 598
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
}


template<int I, int T>
LInstruction* LChunkBuilder::DefineFixed(
    LTemplateInstruction<1, I, T>* instr, Register reg) {
  return Define(instr, ToUnallocated(reg));
}


template<int I, int T>
LInstruction* LChunkBuilder::DefineFixedDouble(
    LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
  return Define(instr, ToUnallocated(reg));
}


LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
  HEnvironment* hydrogen_env = current_block_->last_environment();
  int argument_index_accumulator = 0;
  instr->set_environment(CreateEnvironment(hydrogen_env,
                                           &argument_index_accumulator));
  return instr;
}


LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
                                        HInstruction* hinstr,
                                        CanDeoptimize can_deoptimize) {
628
  info()->MarkAsNonDeferredCalling();
629 630 631 632 633 634
#ifdef DEBUG
  instr->VerifyCall();
#endif
  instr->MarkAsCall();
  instr = AssignPointerMap(instr);

635
  if (hinstr->HasObservableSideEffects()) {
636 637
    ASSERT(hinstr->next()->IsSimulate());
    HSimulate* sim = HSimulate::cast(hinstr->next());
638
    ASSERT(instruction_pending_deoptimization_environment_ == NULL);
639
    ASSERT(pending_deoptimization_ast_id_.IsNone());
640 641
    instruction_pending_deoptimization_environment_ = instr;
    pending_deoptimization_ast_id_ = sim->ast_id();
642 643 644 645 646 647 648
  }

  // 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 =
649 650
      (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
      !hinstr->HasObservableSideEffects();
651 652 653 654 655 656 657 658 659 660
  if (needs_environment && !instr->HasEnvironment()) {
    instr = AssignEnvironment(instr);
  }

  return instr;
}


LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
  ASSERT(!instr->HasPointerMap());
661
  instr->set_pointer_map(new(zone()) LPointerMap(position_, zone()));
662 663 664 665 666
  return instr;
}


LUnallocated* LChunkBuilder::TempRegister() {
667 668
  LUnallocated* operand =
      new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
669 670 671
  int vreg = allocator_->GetVirtualRegister();
  if (!allocator_->AllocationOk()) {
    Abort("Out of virtual registers while trying to allocate temp register.");
672
    vreg = 0;
673 674
  }
  operand->set_virtual_register(vreg);
675 676 677 678 679 680
  return operand;
}


LOperand* LChunkBuilder::FixedTemp(Register reg) {
  LUnallocated* operand = ToUnallocated(reg);
681
  ASSERT(operand->HasFixedPolicy());
682 683 684 685 686 687
  return operand;
}


LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
  LUnallocated* operand = ToUnallocated(reg);
688
  ASSERT(operand->HasFixedPolicy());
689 690 691 692 693
  return operand;
}


LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
694
  return new(zone()) LLabel(instr->block());
695 696 697
}


698 699 700 701 702
LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
  return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
}


703 704 705 706 707 708
LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
  UNREACHABLE();
  return NULL;
}


709
LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
710
  return AssignEnvironment(new(zone()) LDeoptimize);
711 712 713 714 715
}


LInstruction* LChunkBuilder::DoShift(Token::Value op,
                                     HBitwiseBinaryOperation* instr) {
716 717 718
  if (instr->representation().IsSmiOrTagged()) {
    ASSERT(instr->left()->representation().IsSmiOrTagged());
    ASSERT(instr->right()->representation().IsSmiOrTagged());
719 720 721

    LOperand* left = UseFixed(instr->left(), a1);
    LOperand* right = UseFixed(instr->right(), a0);
722
    LArithmeticT* result = new(zone()) LArithmeticT(op, left, right);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    return MarkAsCall(DefineFixed(result, v0), instr);
  }

  ASSERT(instr->representation().IsInteger32());
  ASSERT(instr->left()->representation().IsInteger32());
  ASSERT(instr->right()->representation().IsInteger32());
  LOperand* left = UseRegisterAtStart(instr->left());

  HValue* right_value = instr->right();
  LOperand* right = NULL;
  int constant_value = 0;
  if (right_value->IsConstant()) {
    HConstant* constant = HConstant::cast(right_value);
    right = chunk_->DefineConstantOperand(constant);
    constant_value = constant->Integer32Value() & 0x1f;
  } else {
    right = UseRegisterAtStart(right_value);
  }

742 743
  // Shift operations can only deoptimize if we do a logical shift
  // by 0 and the result cannot be truncated to int32.
744
  bool does_deopt = false;
745 746 747 748
  if (op == Token::SHR && constant_value == 0) {
    if (FLAG_opt_safe_uint32_operations) {
      does_deopt = !instr->CheckFlag(HInstruction::kUint32);
    } else {
749 750 751 752 753
      for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
        if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
          does_deopt = true;
          break;
        }
754 755 756 757 758
      }
    }
  }

  LInstruction* result =
759
      DefineAsRegister(new(zone()) LShiftI(op, left, right, does_deopt));
760 761 762 763 764 765 766 767 768 769 770 771
  return does_deopt ? AssignEnvironment(result) : result;
}


LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
                                           HArithmeticBinaryOperation* instr) {
  ASSERT(instr->representation().IsDouble());
  ASSERT(instr->left()->representation().IsDouble());
  ASSERT(instr->right()->representation().IsDouble());
  ASSERT(op != Token::MOD);
  LOperand* left = UseRegisterAtStart(instr->left());
  LOperand* right = UseRegisterAtStart(instr->right());
772
  LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
773 774 775 776 777 778 779 780 781 782 783 784 785
  return DefineAsRegister(result);
}


LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
                                           HArithmeticBinaryOperation* instr) {
  ASSERT(op == Token::ADD ||
         op == Token::DIV ||
         op == Token::MOD ||
         op == Token::MUL ||
         op == Token::SUB);
  HValue* left = instr->left();
  HValue* right = instr->right();
786 787
  ASSERT(left->representation().IsTagged());
  ASSERT(right->representation().IsTagged());
788 789
  LOperand* left_operand = UseFixed(left, a1);
  LOperand* right_operand = UseFixed(right, a0);
790 791
  LArithmeticT* result =
      new(zone()) LArithmeticT(op, left_operand, right_operand);
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  return MarkAsCall(DefineFixed(result, v0), instr);
}


void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
  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);
829 830 831
      if (phi->merged_index() < last_environment->length()) {
        last_environment->SetValueAt(phi->merged_index(), phi);
      }
832 833
    }
    for (int i = 0; i < block->deleted_phis()->length(); ++i) {
834 835 836 837
      if (block->deleted_phis()->at(i) < last_environment->length()) {
        last_environment->SetValueAt(block->deleted_phis()->at(i),
                                     graph_->GetConstantUndefined());
      }
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
    }
    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;
}


void LChunkBuilder::VisitInstruction(HInstruction* current) {
  HInstruction* old_current = current_instruction_;
  current_instruction_ = current;
  if (current->has_position()) position_ = current->position();
  LInstruction* instr = current->CompileToLithium(this);

  if (instr != NULL) {
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
#if DEBUG
    // 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())) {
      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;
      }
      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);
    }
#endif

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
      instr = AssignPointerMap(instr);
    }
    if (FLAG_stress_environments && !instr->HasEnvironment()) {
      instr = AssignEnvironment(instr);
    }
    instr->set_hydrogen_value(current);
    chunk_->AddInstruction(instr, current_block_);
  }
  current_instruction_ = old_current;
}


LEnvironment* LChunkBuilder::CreateEnvironment(
    HEnvironment* hydrogen_env,
    int* argument_index_accumulator) {
  if (hydrogen_env == NULL) return NULL;

  LEnvironment* outer =
      CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator);
919 920
  BailoutId ast_id = hydrogen_env->ast_id();
  ASSERT(!ast_id.IsNone() ||
921
         hydrogen_env->frame_type() != JS_FUNCTION);
922
  int value_count = hydrogen_env->length() - hydrogen_env->specials_count();
923 924 925 926 927 928 929
  LEnvironment* result = new(zone()) LEnvironment(
      hydrogen_env->closure(),
      hydrogen_env->frame_type(),
      ast_id,
      hydrogen_env->parameter_count(),
      argument_count_,
      value_count,
930
      outer,
931
      hydrogen_env->entry(),
932
      zone());
933
  bool needs_arguments_object_materialization = false;
934
  int argument_index = *argument_index_accumulator;
935
  for (int i = 0; i < hydrogen_env->length(); ++i) {
936 937 938 939 940
    if (hydrogen_env->is_special_index(i)) continue;

    HValue* value = hydrogen_env->values()->at(i);
    LOperand* op = NULL;
    if (value->IsArgumentsObject()) {
941
      needs_arguments_object_materialization = true;
942 943
      op = NULL;
    } else if (value->IsPushArgument()) {
944
      op = new(zone()) LArgument(argument_index++);
945 946 947
    } else {
      op = UseAny(value);
    }
948 949 950
    result->AddValue(op,
                     value->representation(),
                     value->CheckFlag(HInstruction::kUint32));
951 952
  }

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
  if (needs_arguments_object_materialization) {
    HArgumentsObject* arguments = hydrogen_env->entry() == NULL
        ? graph()->GetArgumentsObject()
        : hydrogen_env->entry()->arguments_object();
    ASSERT(arguments->IsLinked());
    for (int i = 1; i < arguments->arguments_count(); ++i) {
      HValue* value = arguments->arguments_values()->at(i);
      ASSERT(!value->IsArgumentsObject() && !value->IsPushArgument());
      LOperand* op = UseAny(value);
      result->AddValue(op,
                       value->representation(),
                       value->CheckFlag(HInstruction::kUint32));
    }
  }

968
  if (hydrogen_env->frame_type() == JS_FUNCTION) {
969 970 971
    *argument_index_accumulator = argument_index;
  }

972 973 974 975 976
  return result;
}


LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
977
  return new(zone()) LGoto(instr->FirstSuccessor()->block_id());
978 979 980 981
}


LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
982 983
  HValue* value = instr->value();
  if (value->EmitAtUses()) {
984
    HBasicBlock* successor = HConstant::cast(value)->BooleanValue()
985 986
        ? instr->FirstSuccessor()
        : instr->SecondSuccessor();
987
    return new(zone()) LGoto(successor->block_id());
988
  }
989

990
  LBranch* result = new(zone()) LBranch(UseRegister(value));
991
  // Tagged values that are not known smis or booleans require a
992 993
  // deoptimization environment. If the instruction is generic no
  // environment is needed since all cases are handled.
994 995
  Representation rep = value->representation();
  HType type = value->type();
996 997 998
  ToBooleanStub::Types expected = instr->expected_input_types();
  if (rep.IsTagged() && !type.IsSmi() && !type.IsBoolean() &&
      !expected.IsGeneric()) {
999 1000 1001
    return AssignEnvironment(result);
  }
  return result;
1002 1003 1004 1005 1006 1007 1008
}


LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* value = UseRegisterAtStart(instr->value());
  LOperand* temp = TempRegister();
1009
  return new(zone()) LCmpMapAndBranch(value, temp);
1010 1011 1012 1013
}


LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1014
  info()->MarkAsRequiresFrame();
1015 1016
  return DefineAsRegister(
      new(zone()) LArgumentsLength(UseRegister(length->value())));
1017 1018 1019 1020
}


LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1021
  info()->MarkAsRequiresFrame();
1022
  return DefineAsRegister(new(zone()) LArgumentsElements);
1023 1024 1025 1026 1027
}


LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
  LInstanceOf* result =
1028 1029
      new(zone()) LInstanceOf(UseFixed(instr->left(), a0),
                              UseFixed(instr->right(), a1));
1030 1031 1032 1033 1034 1035 1036
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
    HInstanceOfKnownGlobal* instr) {
  LInstanceOfKnownGlobal* result =
1037 1038
      new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->left(), a0),
                                         FixedTemp(t0));
1039 1040 1041 1042
  return MarkAsCall(DefineFixed(result, v0), instr);
}


1043 1044 1045 1046 1047 1048
LInstruction* LChunkBuilder::DoInstanceSize(HInstanceSize* instr) {
  LOperand* object = UseRegisterAtStart(instr->object());
  return DefineAsRegister(new(zone()) LInstanceSize(object));
}


1049 1050 1051 1052 1053 1054 1055 1056
LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
  LOperand* receiver = UseRegisterAtStart(instr->receiver());
  LOperand* function = UseRegisterAtStart(instr->function());
  LWrapReceiver* result = new(zone()) LWrapReceiver(receiver, function);
  return AssignEnvironment(DefineSameAsFirst(result));
}


1057 1058 1059 1060 1061
LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
  LOperand* function = UseFixed(instr->function(), a1);
  LOperand* receiver = UseFixed(instr->receiver(), a0);
  LOperand* length = UseFixed(instr->length(), a2);
  LOperand* elements = UseFixed(instr->elements(), a3);
1062 1063 1064 1065
  LApplyArguments* result = new(zone()) LApplyArguments(function,
                                                        receiver,
                                                        length,
                                                        elements);
1066 1067 1068 1069 1070 1071 1072
  return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
  ++argument_count_;
  LOperand* argument = Use(instr->argument());
1073
  return new(zone()) LPushArgument(argument);
1074 1075 1076
}


1077 1078 1079 1080 1081 1082 1083 1084 1085
LInstruction* LChunkBuilder::DoInnerAllocatedObject(
    HInnerAllocatedObject* inner_object) {
  LOperand* base_object = UseRegisterAtStart(inner_object->base_object());
  LInnerAllocatedObject* result =
    new(zone()) LInnerAllocatedObject(base_object);
  return DefineAsRegister(result);
}


1086
LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1087 1088 1089
  return instr->HasNoUses()
      ? NULL
      : DefineAsRegister(new(zone()) LThisFunction);
1090 1091 1092 1093
}


LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1094 1095 1096 1097 1098 1099 1100 1101
  // If there is a non-return use, the context must be allocated in a register.
  for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
    if (!it.value()->IsReturn()) {
      return DefineAsRegister(new(zone()) LContext);
    }
  }

  return NULL;
1102 1103 1104 1105 1106
}


LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
  LOperand* context = UseRegisterAtStart(instr->value());
1107
  return DefineAsRegister(new(zone()) LOuterContext(context));
1108 1109 1110
}


1111
LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1112
  return MarkAsCall(new(zone()) LDeclareGlobals, instr);
1113 1114 1115
}


1116 1117
LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
  LOperand* context = UseRegisterAtStart(instr->value());
1118
  return DefineAsRegister(new(zone()) LGlobalObject(context));
1119 1120 1121 1122 1123
}


LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
  LOperand* global_object = UseRegisterAtStart(instr->value());
1124
  return DefineAsRegister(new(zone()) LGlobalReceiver(global_object));
1125 1126 1127 1128 1129 1130
}


LInstruction* LChunkBuilder::DoCallConstantFunction(
    HCallConstantFunction* instr) {
  argument_count_ -= instr->argument_count();
1131
  return MarkAsCall(DefineFixed(new(zone()) LCallConstantFunction, v0), instr);
1132 1133 1134 1135 1136 1137
}


LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
  LOperand* function = UseFixed(instr->function(), a1);
  argument_count_ -= instr->argument_count();
1138
  LInvokeFunction* result = new(zone()) LInvokeFunction(function);
1139 1140 1141 1142 1143
  return MarkAsCall(DefineFixed(result, v0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  switch (instr->op()) {
    case kMathFloor: return DoMathFloor(instr);
    case kMathRound: return DoMathRound(instr);
    case kMathAbs: return DoMathAbs(instr);
    case kMathLog: return DoMathLog(instr);
    case kMathSin: return DoMathSin(instr);
    case kMathCos: return DoMathCos(instr);
    case kMathTan: return DoMathTan(instr);
    case kMathExp: return DoMathExp(instr);
    case kMathSqrt: return DoMathSqrt(instr);
    case kMathPowHalf: return DoMathPowHalf(instr);
    default:
      UNREACHABLE();
      return NULL;
1158 1159 1160 1161
  }
}


1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
  LOperand* input = UseFixedDouble(instr->value(), f4);
  LMathLog* result = new(zone()) LMathLog(input);
  return MarkAsCall(DefineFixedDouble(result, f4), instr);
}


LInstruction* LChunkBuilder::DoMathSin(HUnaryMathOperation* instr) {
  LOperand* input = UseFixedDouble(instr->value(), f4);
  LMathSin* result = new(zone()) LMathSin(input);
  return MarkAsCall(DefineFixedDouble(result, f4), instr);
}


LInstruction* LChunkBuilder::DoMathCos(HUnaryMathOperation* instr) {
  LOperand* input = UseFixedDouble(instr->value(), f4);
  LMathCos* result = new(zone()) LMathCos(input);
  return MarkAsCall(DefineFixedDouble(result, f4), instr);
}


LInstruction* LChunkBuilder::DoMathTan(HUnaryMathOperation* instr) {
  LOperand* input = UseFixedDouble(instr->value(), f4);
  LMathTan* result = new(zone()) LMathTan(input);
  return MarkAsCall(DefineFixedDouble(result, f4), instr);
}


LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
  ASSERT(instr->representation().IsDouble());
  ASSERT(instr->value()->representation().IsDouble());
  LOperand* input = UseTempRegister(instr->value());
  LOperand* temp1 = TempRegister();
  LOperand* temp2 = TempRegister();
  LOperand* double_temp = FixedTemp(f6);  // Chosen by fair dice roll.
  LMathExp* result = new(zone()) LMathExp(input, double_temp, temp1, temp2);
  return DefineAsRegister(result);
}


LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
  // Input cannot be the same as the result, see LCodeGen::DoMathPowHalf.
  LOperand* input = UseFixedDouble(instr->value(), f8);
  LOperand* temp = FixedTemp(f6);
  LMathPowHalf* result = new(zone()) LMathPowHalf(input, temp);
  return DefineFixedDouble(result, f4);
}


LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
  LOperand* input = UseRegister(instr->value());
  LMathAbs* result = new(zone()) LMathAbs(input);
  return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
}


LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
  LOperand* input = UseRegister(instr->value());
  LOperand* temp = TempRegister();
  LMathFloor* result = new(zone()) LMathFloor(input, temp);
  return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
}


LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
  LOperand* input = UseRegister(instr->value());
  LMathSqrt* result = new(zone()) LMathSqrt(input);
  return DefineAsRegister(result);
}


LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
  LOperand* input = UseRegister(instr->value());
  LOperand* temp = FixedTemp(f6);
  LMathRound* result = new(zone()) LMathRound(input, temp);
  return AssignEnvironment(DefineAsRegister(result));
}


1241 1242 1243 1244
LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
  ASSERT(instr->key()->representation().IsTagged());
  argument_count_ -= instr->argument_count();
  LOperand* key = UseFixed(instr->key(), a2);
1245
  return MarkAsCall(DefineFixed(new(zone()) LCallKeyed(key), v0), instr);
1246 1247 1248 1249 1250
}


LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
  argument_count_ -= instr->argument_count();
1251
  return MarkAsCall(DefineFixed(new(zone()) LCallNamed, v0), instr);
1252 1253 1254 1255 1256
}


LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
  argument_count_ -= instr->argument_count();
1257
  return MarkAsCall(DefineFixed(new(zone()) LCallGlobal, v0), instr);
1258 1259 1260 1261 1262
}


LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
  argument_count_ -= instr->argument_count();
1263
  return MarkAsCall(DefineFixed(new(zone()) LCallKnownGlobal, v0), instr);
1264 1265 1266 1267 1268 1269
}


LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
  LOperand* constructor = UseFixed(instr->constructor(), a1);
  argument_count_ -= instr->argument_count();
1270
  LCallNew* result = new(zone()) LCallNew(constructor);
1271 1272 1273 1274
  return MarkAsCall(DefineFixed(result, v0), instr);
}


1275 1276 1277 1278 1279 1280 1281 1282
LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
  LOperand* constructor = UseFixed(instr->constructor(), a1);
  argument_count_ -= instr->argument_count();
  LCallNewArray* result = new(zone()) LCallNewArray(constructor);
  return MarkAsCall(DefineFixed(result, v0), instr);
}


1283
LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1284
  LOperand* function = UseFixed(instr->function(), a1);
1285
  argument_count_ -= instr->argument_count();
1286 1287
  return MarkAsCall(DefineFixed(new(zone()) LCallFunction(function), v0),
                    instr);
1288 1289 1290 1291 1292
}


LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
  argument_count_ -= instr->argument_count();
1293
  return MarkAsCall(DefineFixed(new(zone()) LCallRuntime, v0), instr);
1294 1295 1296
}


1297 1298 1299 1300 1301
LInstruction* LChunkBuilder::DoRor(HRor* instr) {
  return DoShift(Token::ROR, instr);
}


1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
LInstruction* LChunkBuilder::DoShr(HShr* instr) {
  return DoShift(Token::SHR, instr);
}


LInstruction* LChunkBuilder::DoSar(HSar* instr) {
  return DoShift(Token::SAR, instr);
}


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


1317
LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1318 1319 1320
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1321

1322 1323
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1324
    return DefineAsRegister(new(zone()) LBitI(left, right));
1325
  } else {
1326 1327 1328
    ASSERT(instr->representation().IsTagged());
    ASSERT(instr->left()->representation().IsTagged());
    ASSERT(instr->right()->representation().IsTagged());
1329 1330 1331

    LOperand* left = UseFixed(instr->left(), a1);
    LOperand* right = UseFixed(instr->right(), a0);
1332
    LArithmeticT* result = new(zone()) LArithmeticT(instr->op(), left, right);
1333 1334
    return MarkAsCall(DefineFixed(result, v0), instr);
  }
1335 1336 1337 1338 1339 1340
}


LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
  ASSERT(instr->value()->representation().IsInteger32());
  ASSERT(instr->representation().IsInteger32());
1341
  if (instr->HasNoUses()) return NULL;
1342 1343
  LOperand* value = UseRegisterAtStart(instr->value());
  return DefineAsRegister(new(zone()) LBitNotI(value));
1344 1345 1346 1347 1348 1349
}


LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
  if (instr->representation().IsDouble()) {
    return DoArithmeticD(Token::DIV, instr);
1350 1351 1352
  } else if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1353 1354 1355 1356
    LOperand* dividend = UseRegister(instr->left());
    LOperand* divisor = UseRegister(instr->right());
    LDivI* div = new(zone()) LDivI(dividend, divisor);
    return AssignEnvironment(DefineAsRegister(div));
1357 1358 1359 1360 1361 1362
  } else {
    return DoArithmeticT(Token::DIV, instr);
  }
}


1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
bool LChunkBuilder::HasMagicNumberForDivisor(int32_t divisor) {
  uint32_t divisor_abs = abs(divisor);
  // Dividing by 0, 1, and powers of 2 is easy.
  // Note that IsPowerOf2(0) returns true;
  ASSERT(IsPowerOf2(0) == true);
  if (IsPowerOf2(divisor_abs)) return true;

  // We have magic numbers for a few specific divisors.
  // Details and proofs can be found in:
  // - Hacker's Delight, Henry S. Warren, Jr.
  // - The PowerPC Compiler Writer's Guide
  // and probably many others.
  //
  // We handle
  //   <divisor with magic numbers> * <power of 2>
  // but not
  //   <divisor with magic numbers> * <other divisor with magic numbers>
  int32_t power_of_2_factor =
    CompilerIntrinsics::CountTrailingZeros(divisor_abs);
  DivMagicNumbers magic_numbers =
    DivMagicNumberFor(divisor_abs >> power_of_2_factor);
  if (magic_numbers.M != InvalidDivMagicNumber.M) return true;

  return false;
}


HValue* LChunkBuilder::SimplifiedDivisorForMathFloorOfDiv(HValue* divisor) {
  // Only optimize when we have magic numbers for the divisor.
  // The standard integer division routine is usually slower than transitionning
  // to FPU.
  if (divisor->IsConstant() &&
      HConstant::cast(divisor)->HasInteger32Value()) {
    HConstant* constant_val = HConstant::cast(divisor);
    return constant_val->CopyToRepresentation(Representation::Integer32(),
                                                divisor->block()->zone());
  }
1400 1401 1402 1403
  return NULL;
}


1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
    HValue* right = instr->right();
    LOperand* dividend = UseRegister(instr->left());
    LOperand* divisor = UseRegisterOrConstant(right);
    LOperand* remainder = TempRegister();
    ASSERT(right->IsConstant() &&
           HConstant::cast(right)->HasInteger32Value());
    return AssignEnvironment(DefineAsRegister(
          new(zone()) LMathFloorOfDiv(dividend, divisor, remainder)));
}


1416
LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1417 1418
  HValue* left = instr->left();
  HValue* right = instr->right();
1419 1420 1421
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1422
    if (instr->HasPowerOf2Divisor()) {
1423 1424 1425 1426 1427 1428 1429 1430
      ASSERT(!right->CanBeZero());
      LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
                                     UseOrConstant(right));
      LInstruction* result = DefineAsRegister(mod);
      return (left->CanBeNegative() &&
              instr->CheckFlag(HValue::kBailoutOnMinusZero))
          ? AssignEnvironment(result)
          : result;
1431 1432 1433 1434
    } else if (instr->fixed_right_arg().has_value) {
      LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
                                     UseRegisterAtStart(right));
      return AssignEnvironment(DefineAsRegister(mod));
1435
    } else {
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
      LModI* mod = new(zone()) LModI(UseRegister(left),
                                     UseRegister(right),
                                     TempRegister(),
                                     FixedTemp(f20),
                                     FixedTemp(f22));
      LInstruction* result = DefineAsRegister(mod);
      return (right->CanBeZero() ||
              (left->RangeCanInclude(kMinInt) &&
               right->RangeCanInclude(-1)) ||
              instr->CheckFlag(HValue::kBailoutOnMinusZero))
          ? AssignEnvironment(result)
          : result;
1448
    }
1449
  } else if (instr->representation().IsTagged()) {
1450 1451 1452
    return DoArithmeticT(Token::MOD, instr);
  } else {
    ASSERT(instr->representation().IsDouble());
1453 1454
    // We call a C function for double modulo. It can't trigger a GC. We need
    // to use fixed result register for the call.
1455
    // TODO(fschneider): Allow any register as input registers.
1456 1457 1458 1459
    LArithmeticD* mod = new(zone()) LArithmeticD(Token::MOD,
                                                 UseFixedDouble(left, f2),
                                                 UseFixedDouble(right, f4));
    return MarkAsCall(DefineFixedDouble(mod, f2), instr);
1460 1461 1462 1463 1464
  }
}


LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1465 1466 1467
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1468
    LOperand* left;
1469
    LOperand* right = UseOrConstant(instr->BetterRightOperand());
1470 1471 1472 1473
    LOperand* temp = NULL;
    if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
        (instr->CheckFlag(HValue::kCanOverflow) ||
        !right->IsConstantOperand())) {
1474
      left = UseRegister(instr->BetterLeftOperand());
1475 1476
      temp = TempRegister();
    } else {
1477
      left = UseRegisterAtStart(instr->BetterLeftOperand());
1478
    }
1479
    LMulI* mul = new(zone()) LMulI(left, right, temp);
1480 1481 1482 1483 1484
    if (instr->CheckFlag(HValue::kCanOverflow) ||
        instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
      AssignEnvironment(mul);
    }
    return DefineAsRegister(mul);
1485 1486

  } else if (instr->representation().IsDouble()) {
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
    if (kArchVariant == kMips32r2) {
      if (instr->UseCount() == 1 && instr->uses().value()->IsAdd()) {
        HAdd* add = HAdd::cast(instr->uses().value());
        if (instr == add->left()) {
          // This mul is the lhs of an add. The add and mul will be folded
          // into a multiply-add.
          return NULL;
        }
        if (instr == add->right() && !add->left()->IsMul()) {
          // This mul is the rhs of an add, where the lhs is not another mul.
          // The add and mul will be folded into a multiply-add.
          return NULL;
        }
      }
    }
1502 1503 1504 1505 1506 1507 1508 1509
    return DoArithmeticD(Token::MUL, instr);
  } else {
    return DoArithmeticT(Token::MUL, instr);
  }
}


LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1510 1511 1512
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1513 1514
    LOperand* left = UseRegisterAtStart(instr->left());
    LOperand* right = UseOrConstantAtStart(instr->right());
1515
    LSubI* sub = new(zone()) LSubI(left, right);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
    LInstruction* result = DefineAsRegister(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);
  }
}


1529 1530 1531 1532 1533 1534 1535 1536 1537
LInstruction* LChunkBuilder::DoMultiplyAdd(HMul* mul, HValue* addend) {
  LOperand* multiplier_op = UseRegisterAtStart(mul->left());
  LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
  LOperand* addend_op = UseRegisterAtStart(addend);
  return DefineSameAsFirst(new(zone()) LMultiplyAddD(addend_op, multiplier_op,
                                                     multiplicand_op));
}


1538
LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1539 1540 1541
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1542 1543
    LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
    LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1544
    LAddI* add = new(zone()) LAddI(left, right);
1545 1546 1547 1548 1549 1550
    LInstruction* result = DefineAsRegister(add);
    if (instr->CheckFlag(HValue::kCanOverflow)) {
      result = AssignEnvironment(result);
    }
    return result;
  } else if (instr->representation().IsDouble()) {
1551 1552 1553 1554 1555 1556 1557 1558 1559
    if (kArchVariant == kMips32r2) {
      if (instr->left()->IsMul())
        return DoMultiplyAdd(HMul::cast(instr->left()), instr->right());

      if (instr->right()->IsMul()) {
        ASSERT(!instr->left()->IsMul());
        return DoMultiplyAdd(HMul::cast(instr->right()), instr->left());
      }
    }
1560 1561
    return DoArithmeticD(Token::ADD, instr);
  } else {
1562
    ASSERT(instr->representation().IsTagged());
1563 1564 1565 1566 1567
    return DoArithmeticT(Token::ADD, instr);
  }
}


1568 1569 1570
LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
  LOperand* left = NULL;
  LOperand* right = NULL;
1571 1572 1573
  if (instr->representation().IsSmiOrInteger32()) {
    ASSERT(instr->left()->representation().Equals(instr->representation()));
    ASSERT(instr->right()->representation().Equals(instr->representation()));
1574 1575
    left = UseRegisterAtStart(instr->BetterLeftOperand());
    right = UseOrConstantAtStart(instr->BetterRightOperand());
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
  } else {
    ASSERT(instr->representation().IsDouble());
    ASSERT(instr->left()->representation().IsDouble());
    ASSERT(instr->right()->representation().IsDouble());
    left = UseRegisterAtStart(instr->left());
    right = UseRegisterAtStart(instr->right());
  }
  return DefineAsRegister(new(zone()) LMathMinMax(left, right));
}


1587 1588 1589 1590 1591 1592 1593 1594 1595
LInstruction* LChunkBuilder::DoPower(HPower* instr) {
  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(), f2);
  LOperand* right = exponent_type.IsDouble() ?
      UseFixedDouble(instr->right(), f4) :
1596
      UseFixed(instr->right(), a2);
1597
  LPower* result = new(zone()) LPower(left, right);
1598
  return MarkAsCall(DefineFixedDouble(result, f0),
1599 1600 1601 1602
                    instr,
                    CAN_DEOPTIMIZE_EAGERLY);
}

1603 1604 1605 1606 1607

LInstruction* LChunkBuilder::DoRandom(HRandom* instr) {
  ASSERT(instr->representation().IsDouble());
  ASSERT(instr->global_object()->representation().IsTagged());
  LOperand* global_object = UseFixed(instr->global_object(), a0);
1608
  LRandom* result = new(zone()) LRandom(global_object);
1609 1610 1611
  return MarkAsCall(DefineFixedDouble(result, f0), instr);
}

1612 1613 1614 1615 1616 1617

LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
  ASSERT(instr->left()->representation().IsTagged());
  ASSERT(instr->right()->representation().IsTagged());
  LOperand* left = UseFixed(instr->left(), a1);
  LOperand* right = UseFixed(instr->right(), a0);
1618
  LCmpT* result = new(zone()) LCmpT(left, right);
1619
  return MarkAsCall(DefineFixed(result, v0), instr);
1620 1621 1622
}


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


LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
    HCompareObjectEqAndBranch* instr) {
  LOperand* left = UseRegisterAtStart(instr->left());
  LOperand* right = UseRegisterAtStart(instr->right());
1648
  return new(zone()) LCmpObjectEqAndBranch(left, right);
1649 1650 1651 1652 1653 1654
}


LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* temp = TempRegister();
1655 1656
  return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()),
                                        temp);
1657 1658 1659
}


1660 1661 1662
LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* temp = TempRegister();
1663 1664
  return new(zone()) LIsStringAndBranch(UseRegisterAtStart(instr->value()),
                                        temp);
1665 1666 1667
}


1668 1669
LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1670
  return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1671 1672 1673 1674 1675 1676
}


LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
    HIsUndetectableAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1677 1678
  return new(zone()) LIsUndetectableAndBranch(
      UseRegisterAtStart(instr->value()), TempRegister());
1679 1680 1681
}


1682 1683 1684 1685 1686 1687
LInstruction* LChunkBuilder::DoStringCompareAndBranch(
    HStringCompareAndBranch* instr) {
  ASSERT(instr->left()->representation().IsTagged());
  ASSERT(instr->right()->representation().IsTagged());
  LOperand* left = UseFixed(instr->left(), a1);
  LOperand* right = UseFixed(instr->right(), a0);
1688 1689
  LStringCompareAndBranch* result =
      new(zone()) LStringCompareAndBranch(left, right);
1690 1691 1692 1693
  return MarkAsCall(result, instr);
}


1694 1695 1696
LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
    HHasInstanceTypeAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1697 1698
  LOperand* value = UseRegisterAtStart(instr->value());
  return new(zone()) LHasInstanceTypeAndBranch(value);
1699 1700 1701 1702 1703 1704 1705 1706
}


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

1707
  return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1708 1709 1710 1711 1712 1713
}


LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
    HHasCachedArrayIndexAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1714
  return new(zone()) LHasCachedArrayIndexAndBranch(
1715 1716 1717 1718 1719 1720 1721
      UseRegisterAtStart(instr->value()));
}


LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
    HClassOfTestAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1722 1723
  return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
                                           TempRegister());
1724 1725 1726
}


1727 1728 1729 1730 1731 1732
LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
  LOperand* map = UseRegisterAtStart(instr->value());
  return DefineAsRegister(new(zone()) LMapEnumLength(map));
}


1733 1734
LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
  LOperand* object = UseRegisterAtStart(instr->value());
1735
  return DefineAsRegister(new(zone()) LElementsKind(object));
1736 1737 1738 1739 1740
}


LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
  LOperand* object = UseRegister(instr->value());
1741
  LValueOf* result = new(zone()) LValueOf(object, TempRegister());
1742
  return DefineAsRegister(result);
1743 1744 1745
}


1746 1747
LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
  LOperand* object = UseFixed(instr->value(), a0);
1748 1749
  LDateField* result =
      new(zone()) LDateField(object, FixedTemp(a1), instr->index());
1750
  return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
1751 1752 1753
}


1754 1755 1756
LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
  LOperand* string = UseRegister(instr->string());
  LOperand* index = UseRegister(instr->index());
1757
  LOperand* value = UseTempRegister(instr->value());
1758 1759 1760 1761 1762 1763
  LSeqStringSetChar* result =
      new(zone()) LSeqStringSetChar(instr->encoding(), string, index, value);
  return DefineAsRegister(result);
}


1764 1765 1766 1767 1768
LInstruction* LChunkBuilder::DoNumericConstraint(HNumericConstraint* instr) {
  return NULL;
}


1769 1770 1771 1772 1773 1774
LInstruction* LChunkBuilder::DoInductionVariableAnnotation(
    HInductionVariableAnnotation* instr) {
  return NULL;
}


1775
LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1776
  LOperand* value = UseRegisterOrConstantAtStart(instr->index());
1777 1778
  LOperand* length = UseRegister(instr->length());
  return AssignEnvironment(new(zone()) LBoundsCheck(value, length));
1779 1780 1781
}


1782 1783 1784 1785 1786 1787 1788
LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
    HBoundsCheckBaseIndexInformation* instr) {
  UNREACHABLE();
  return NULL;
}


1789 1790 1791 1792 1793 1794 1795 1796 1797
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;
}


LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
  LOperand* value = UseFixed(instr->value(), a0);
1798
  return MarkAsCall(new(zone()) LThrow(value), instr);
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
}


LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
  return NULL;
}


LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
  // All HForceRepresentation instructions should be eliminated in the
  // representation change phase of Hydrogen.
  UNREACHABLE();
  return NULL;
}


LInstruction* LChunkBuilder::DoChange(HChange* instr) {
  Representation from = instr->from();
  Representation to = instr->to();
1818 1819 1820 1821 1822 1823 1824
  if (from.IsSmi()) {
    if (to.IsTagged()) {
      LOperand* value = UseRegister(instr->value());
      return DefineSameAsFirst(new(zone()) LDummyUse(value));
    }
    from = Representation::Tagged();
  }
1825 1826
  if (from.IsTagged()) {
    if (to.IsDouble()) {
1827
      info()->MarkAsDeferredCalling();
1828
      LOperand* value = UseRegister(instr->value());
1829
      LNumberUntagD* res = new(zone()) LNumberUntagD(value);
1830
      return AssignEnvironment(DefineAsRegister(res));
1831 1832
    } else if (to.IsSmi()) {
      HValue* val = instr->value();
1833
      LOperand* value = UseRegister(val);
1834 1835 1836
      if (val->type().IsSmi()) {
        return DefineSameAsFirst(new(zone()) LDummyUse(value));
      }
1837
      return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
1838 1839
    } else {
      ASSERT(to.IsInteger32());
1840
      LOperand* value = NULL;
1841
      LInstruction* res = NULL;
1842
      if (instr->value()->type().IsSmi()) {
1843
        value = UseRegisterAtStart(instr->value());
1844
        res = DefineAsRegister(new(zone()) LSmiUntag(value, false));
1845
      } else {
1846
        value = UseRegister(instr->value());
1847 1848 1849
        LOperand* temp1 = TempRegister();
        LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
                                                      : NULL;
1850
        LOperand* temp3 = FixedTemp(f22);
1851 1852 1853 1854
        res = DefineSameAsFirst(new(zone()) LTaggedToI(value,
                                                       temp1,
                                                       temp2,
                                                       temp3));
1855 1856 1857 1858 1859 1860
        res = AssignEnvironment(res);
      }
      return res;
    }
  } else if (from.IsDouble()) {
    if (to.IsTagged()) {
1861
      info()->MarkAsDeferredCalling();
1862 1863 1864 1865 1866 1867 1868
      LOperand* value = UseRegister(instr->value());
      LOperand* temp1 = TempRegister();
      LOperand* temp2 = TempRegister();

      // Make sure that the temp and result_temp registers are
      // different.
      LUnallocated* result_temp = TempRegister();
1869
      LNumberTagD* result = new(zone()) LNumberTagD(value, temp1, temp2);
1870 1871
      Define(result, result_temp);
      return AssignPointerMap(result);
1872 1873 1874 1875
    } else if (to.IsSmi()) {
      LOperand* value = UseRegister(instr->value());
      return AssignEnvironment(DefineAsRegister(new(zone()) LDoubleToSmi(value,
          TempRegister(), TempRegister())));
1876 1877 1878
    } else {
      ASSERT(to.IsInteger32());
      LOperand* value = UseRegister(instr->value());
1879 1880 1881
      LOperand* temp1 = TempRegister();
      LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister() : NULL;
      LDoubleToI* res = new(zone()) LDoubleToI(value, temp1, temp2);
1882 1883 1884
      return AssignEnvironment(DefineAsRegister(res));
    }
  } else if (from.IsInteger32()) {
1885
    info()->MarkAsDeferredCalling();
1886 1887
    if (to.IsTagged()) {
      HValue* val = instr->value();
1888
      LOperand* value = UseRegisterAtStart(val);
1889 1890 1891 1892
      if (val->CheckFlag(HInstruction::kUint32)) {
        LNumberTagU* result = new(zone()) LNumberTagU(value);
        return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
      } else if (val->HasRange() && val->range()->IsInSmiRange()) {
1893
        return DefineAsRegister(new(zone()) LSmiTag(value));
1894
      } else {
1895
        LNumberTagI* result = new(zone()) LNumberTagI(value);
1896
        return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1897
      }
1898 1899 1900 1901 1902 1903 1904 1905 1906
    } else if (to.IsSmi()) {
      HValue* val = instr->value();
      LOperand* value = UseRegister(val);
      LInstruction* result =
          DefineSameAsFirst(new(zone()) LInteger32ToSmi(value));
      if (val->HasRange() && val->range()->IsInSmiRange()) {
        return result;
      }
      return AssignEnvironment(result);
1907 1908
    } else {
      ASSERT(to.IsDouble());
1909 1910 1911 1912 1913 1914 1915
      if (instr->value()->CheckFlag(HInstruction::kUint32)) {
        return DefineAsRegister(
            new(zone()) LUint32ToDouble(UseRegister(instr->value())));
      } else {
        return DefineAsRegister(
            new(zone()) LInteger32ToDouble(Use(instr->value())));
      }
1916 1917 1918 1919 1920 1921 1922
    }
  }
  UNREACHABLE();
  return NULL;
}


1923
LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
1924
  LOperand* value = UseRegisterAtStart(instr->value());
1925
  return AssignEnvironment(new(zone()) LCheckNonSmi(value));
1926 1927 1928
}


1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
  return AssignEnvironment(new(zone()) LCheckSmi(value));
}


LInstruction* LChunkBuilder::DoIsNumberAndBranch(HIsNumberAndBranch* instr) {
  return new(zone())
    LIsNumberAndBranch(UseRegisterOrConstantAtStart(instr->value()));
}


1941 1942
LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
1943
  LInstruction* result = new(zone()) LCheckInstanceType(value);
1944 1945 1946 1947 1948
  return AssignEnvironment(result);
}


LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1949 1950 1951 1952 1953 1954
  LUnallocated* temp1 = NULL;
  LOperand* temp2 = NULL;
  if (!instr->CanOmitPrototypeChecks()) {
    temp1 = TempRegister();
    temp2 = TempRegister();
  }
1955
  LCheckPrototypeMaps* result = new(zone()) LCheckPrototypeMaps(temp1, temp2);
1956
  if (instr->CanOmitPrototypeChecks()) return result;
1957
  return AssignEnvironment(result);
1958 1959 1960 1961 1962
}


LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
1963
  return AssignEnvironment(new(zone()) LCheckFunction(value));
1964 1965 1966
}


1967
LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
1968 1969
  LOperand* value = NULL;
  if (!instr->CanOmitMapChecks()) value = UseRegisterAtStart(instr->value());
1970
  LInstruction* result = new(zone()) LCheckMaps(value);
1971
  if (instr->CanOmitMapChecks()) return result;
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
  return AssignEnvironment(result);
}


LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
  HValue* value = instr->value();
  Representation input_rep = value->representation();
  LOperand* reg = UseRegister(value);
  if (input_rep.IsDouble()) {
    // Revisit this decision, here and 8 lines below.
1982
    return DefineAsRegister(new(zone()) LClampDToUint8(reg, FixedTemp(f22)));
1983
  } else if (input_rep.IsInteger32()) {
1984
    return DefineAsRegister(new(zone()) LClampIToUint8(reg));
1985
  } else {
1986
    ASSERT(input_rep.IsSmiOrTagged());
1987 1988
    // Register allocator doesn't (yet) support allocation of double
    // temps. Reserve f22 explicitly.
1989
    LClampTToUint8* result = new(zone()) LClampTToUint8(reg, FixedTemp(f22));
1990 1991 1992 1993 1994 1995
    return AssignEnvironment(DefineAsRegister(result));
  }
}


LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1996 1997 1998
  LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
  return new(zone()) LReturn(UseFixed(instr->value(), v0),
                             parameter_count);
1999 2000 2001 2002 2003
}


LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
  Representation r = instr->representation();
2004 2005 2006
  if (r.IsSmi()) {
    return DefineAsRegister(new(zone()) LConstantS);
  } else if (r.IsInteger32()) {
2007
    return DefineAsRegister(new(zone()) LConstantI);
2008
  } else if (r.IsDouble()) {
2009
    return DefineAsRegister(new(zone()) LConstantD);
2010
  } else if (r.IsTagged()) {
2011
    return DefineAsRegister(new(zone()) LConstantT);
2012 2013 2014 2015 2016 2017 2018 2019
  } else {
    UNREACHABLE();
    return NULL;
  }
}


LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2020
  LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
2021 2022 2023 2024 2025 2026 2027 2028
  return instr->RequiresHoleCheck()
      ? AssignEnvironment(DefineAsRegister(result))
      : DefineAsRegister(result);
}


LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
  LOperand* global_object = UseFixed(instr->global_object(), a0);
2029
  LLoadGlobalGeneric* result = new(zone()) LLoadGlobalGeneric(global_object);
2030 2031 2032 2033 2034
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2035 2036 2037 2038
  LOperand* value = UseRegister(instr->value());
  // Use a temp to check the value in the cell in the case where we perform
  // a hole check.
  return instr->RequiresHoleCheck()
2039 2040
      ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
      : new(zone()) LStoreGlobalCell(value, NULL);
2041 2042 2043 2044 2045 2046 2047
}


LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
  LOperand* global_object = UseFixed(instr->global_object(), a1);
  LOperand* value = UseFixed(instr->value(), a0);
  LStoreGlobalGeneric* result =
2048
      new(zone()) LStoreGlobalGeneric(global_object, value);
2049 2050 2051 2052
  return MarkAsCall(result, instr);
}


2053 2054 2055 2056 2057 2058 2059
LInstruction* LChunkBuilder::DoLinkObjectInList(HLinkObjectInList* instr) {
  LOperand* object = UseRegister(instr->value());
  LLinkObjectInList* result = new(zone()) LLinkObjectInList(object);
  return result;
}


2060 2061
LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
  LOperand* context = UseRegisterAtStart(instr->value());
2062 2063
  LInstruction* result =
      DefineAsRegister(new(zone()) LLoadContextSlot(context));
2064
  return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
}


LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
  LOperand* context;
  LOperand* value;
  if (instr->NeedsWriteBarrier()) {
    context = UseTempRegister(instr->context());
    value = UseTempRegister(instr->value());
  } else {
    context = UseRegister(instr->context());
    value = UseRegister(instr->value());
  }
2078
  LInstruction* result = new(zone()) LStoreContextSlot(context, value);
2079
  return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
2080 2081 2082 2083
}


LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2084 2085
  LOperand* obj = UseRegisterAtStart(instr->object());
  return DefineAsRegister(new(zone()) LLoadNamedField(obj));
2086 2087 2088 2089 2090 2091 2092 2093
}


LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
    HLoadNamedFieldPolymorphic* instr) {
  ASSERT(instr->representation().IsTagged());
  if (instr->need_generic()) {
    LOperand* obj = UseFixed(instr->object(), a0);
2094 2095
    LLoadNamedFieldPolymorphic* result =
        new(zone()) LLoadNamedFieldPolymorphic(obj);
2096 2097 2098
    return MarkAsCall(DefineFixed(result, v0), instr);
  } else {
    LOperand* obj = UseRegisterAtStart(instr->object());
2099 2100
    LLoadNamedFieldPolymorphic* result =
        new(zone()) LLoadNamedFieldPolymorphic(obj);
2101 2102 2103 2104 2105 2106 2107
    return AssignEnvironment(DefineAsRegister(result));
  }
}


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


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


LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
    HLoadExternalArrayPointer* instr) {
  LOperand* input = UseRegisterAtStart(instr->value());
2123
  return DefineAsRegister(new(zone()) LLoadExternalArrayPointer(input));
2124 2125 2126
}


2127
LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2128
  ASSERT(instr->key()->representation().IsSmiOrInteger32());
2129
  ElementsKind elements_kind = instr->elements_kind();
2130
  LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2131
  LLoadKeyed* result = NULL;
2132

2133 2134 2135 2136 2137
  if (!instr->is_external()) {
    LOperand* obj = NULL;
    if (instr->representation().IsDouble()) {
      obj = UseTempRegister(instr->elements());
    } else {
2138
      ASSERT(instr->representation().IsSmiOrTagged());
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
      obj = UseRegisterAtStart(instr->elements());
    }
    result = new(zone()) LLoadKeyed(obj, key);
  } else {
    ASSERT(
        (instr->representation().IsInteger32() &&
         (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
         (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
        (instr->representation().IsDouble() &&
         ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
          (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
2150
    LOperand* external_pointer = UseRegister(instr->elements());
2151 2152
    result = new(zone()) LLoadKeyed(external_pointer, key);
  }
2153

2154
  DefineAsRegister(result);
2155 2156
  // An unsigned int array load might overflow and cause a deopt, make sure it
  // has an environment.
2157 2158 2159
  bool can_deoptimize = instr->RequiresHoleCheck() ||
      (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS);
  return can_deoptimize ? AssignEnvironment(result) : result;
2160 2161 2162 2163 2164 2165 2166 2167
}


LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
  LOperand* object = UseFixed(instr->object(), a1);
  LOperand* key = UseFixed(instr->key(), a0);

  LInstruction* result =
2168
      DefineFixed(new(zone()) LLoadKeyedGeneric(object, key), v0);
2169 2170 2171 2172
  return MarkAsCall(result, instr);
}


2173 2174
LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
  ElementsKind elements_kind = instr->elements_kind();
2175

2176 2177
  if (!instr->is_external()) {
    ASSERT(instr->elements()->representation().IsTagged());
2178
    bool needs_write_barrier = instr->NeedsWriteBarrier();
2179
    LOperand* object = NULL;
2180 2181 2182
    LOperand* val = NULL;
    LOperand* key = NULL;

2183 2184
    if (instr->value()->representation().IsDouble()) {
      object = UseRegisterAtStart(instr->elements());
2185 2186
      key = UseRegisterOrConstantAtStart(instr->key());
      val = UseTempRegister(instr->value());
2187
    } else {
2188
      ASSERT(instr->value()->representation().IsSmiOrTagged());
2189
      object = UseTempRegister(instr->elements());
2190 2191 2192 2193
      val = needs_write_barrier ? UseTempRegister(instr->value())
          : UseRegisterAtStart(instr->value());
      key = needs_write_barrier ? UseTempRegister(instr->key())
          : UseRegisterOrConstantAtStart(instr->key());
2194 2195
    }

2196
    return new(zone()) LStoreKeyed(object, key, val);
2197 2198
  }

2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215
  ASSERT(
      (instr->value()->representation().IsInteger32() &&
       (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
       (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
      (instr->value()->representation().IsDouble() &&
       ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
  ASSERT(instr->elements()->representation().IsExternal());
  bool val_is_temp_register =
      elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
      elements_kind == EXTERNAL_FLOAT_ELEMENTS;
  LOperand* val = val_is_temp_register ? UseTempRegister(instr->value())
      : UseRegister(instr->value());
  LOperand* key = UseRegisterOrConstantAtStart(instr->key());
  LOperand* external_pointer = UseRegister(instr->elements());

  return new(zone()) LStoreKeyed(external_pointer, key, val);
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
}


LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
  LOperand* obj = UseFixed(instr->object(), a2);
  LOperand* key = UseFixed(instr->key(), a1);
  LOperand* val = UseFixed(instr->value(), a0);

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

2228
  return MarkAsCall(new(zone()) LStoreKeyedGeneric(obj, key, val), instr);
2229 2230 2231 2232 2233
}


LInstruction* LChunkBuilder::DoTransitionElementsKind(
    HTransitionElementsKind* instr) {
2234
  LOperand* object = UseRegister(instr->object());
2235
  if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2236 2237
    LOperand* new_map_reg = TempRegister();
    LTransitionElementsKind* result =
2238
        new(zone()) LTransitionElementsKind(object, new_map_reg, NULL);
2239
    return result;
2240 2241 2242 2243
  } else if (FLAG_compiled_transitions) {
    LTransitionElementsKind* result =
        new(zone()) LTransitionElementsKind(object, NULL, NULL);
    return AssignPointerMap(result);
2244 2245 2246 2247 2248
  } else {
    LOperand* object = UseFixed(instr->object(), a0);
    LOperand* fixed_object_reg = FixedTemp(a2);
    LOperand* new_map_reg = FixedTemp(a3);
    LTransitionElementsKind* result =
2249 2250 2251
        new(zone()) LTransitionElementsKind(object,
                                            new_map_reg,
                                            fixed_object_reg);
2252
    return MarkAsCall(result, instr);
2253 2254 2255 2256
  }
}


2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
LInstruction* LChunkBuilder::DoTrapAllocationMemento(
    HTrapAllocationMemento* instr) {
  LOperand* object = UseRegister(instr->object());
  LOperand* temp = TempRegister();
  LTrapAllocationMemento* result =
      new(zone()) LTrapAllocationMemento(object, temp);
  return AssignEnvironment(result);
}


2267
LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2268
  bool is_in_object = instr->access().IsInobject();
2269
  bool needs_write_barrier = instr->NeedsWriteBarrier();
2270 2271 2272 2273 2274
  bool needs_write_barrier_for_map = !instr->transition().is_null() &&
      instr->NeedsWriteBarrierForMap();

  LOperand* obj;
  if (needs_write_barrier) {
2275
    obj = is_in_object
2276 2277 2278 2279 2280 2281 2282
        ? UseRegister(instr->object())
        : UseTempRegister(instr->object());
  } else {
    obj = needs_write_barrier_for_map
        ? UseRegister(instr->object())
        : UseRegisterAtStart(instr->object());
  }
2283

2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
  LOperand* val;
  if (needs_write_barrier ||
      (FLAG_track_fields && instr->field_representation().IsSmi())) {
    val = UseTempRegister(instr->value());
  } else if (FLAG_track_double_fields &&
             instr->field_representation().IsDouble()) {
    val = UseRegisterAtStart(instr->value());
  } else {
    val = UseRegister(instr->value());
  }
2294

2295 2296 2297
  // We need a temporary register for write barrier of the map field.
  LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;

2298
  LStoreNamedField* result = new(zone()) LStoreNamedField(obj, val, temp);
2299 2300 2301 2302 2303
  if (FLAG_track_heap_object_fields &&
      instr->field_representation().IsHeapObject()) {
    if (!instr->value()->type().IsHeapObject()) {
      return AssignEnvironment(result);
    }
2304 2305
  }
  return result;
2306 2307 2308 2309 2310 2311 2312
}


LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
  LOperand* obj = UseFixed(instr->object(), a1);
  LOperand* val = UseFixed(instr->value(), a0);

2313
  LInstruction* result = new(zone()) LStoreNamedGeneric(obj, val);
2314 2315 2316 2317 2318 2319 2320
  return MarkAsCall(result, instr);
}


LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
  LOperand* left = UseRegisterAtStart(instr->left());
  LOperand* right = UseRegisterAtStart(instr->right());
2321 2322
  return MarkAsCall(DefineFixed(new(zone()) LStringAdd(left, right), v0),
                    instr);
2323 2324 2325 2326 2327 2328
}


LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
  LOperand* string = UseTempRegister(instr->string());
  LOperand* index = UseTempRegister(instr->index());
2329
  LStringCharCodeAt* result = new(zone()) LStringCharCodeAt(string, index);
2330 2331 2332 2333 2334 2335
  return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
}


LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
  LOperand* char_code = UseRegister(instr->value());
2336
  LStringCharFromCode* result = new(zone()) LStringCharFromCode(char_code);
2337 2338 2339 2340 2341 2342
  return AssignPointerMap(DefineAsRegister(result));
}


LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
  LOperand* string = UseRegisterAtStart(instr->value());
2343
  return DefineAsRegister(new(zone()) LStringLength(string));
2344 2345 2346
}


2347 2348
LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
  info()->MarkAsDeferredCalling();
2349 2350 2351
  LOperand* size = instr->size()->IsConstant()
      ? UseConstant(instr->size())
      : UseTempRegister(instr->size());
2352 2353 2354 2355 2356 2357 2358
  LOperand* temp1 = TempRegister();
  LOperand* temp2 = TempRegister();
  LAllocate* result = new(zone()) LAllocate(size, temp1, temp2);
  return AssignPointerMap(DefineAsRegister(result));
}


2359
LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2360
  return MarkAsCall(DefineFixed(new(zone()) LRegExpLiteral, v0), instr);
2361 2362 2363 2364
}


LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2365
  return MarkAsCall(DefineFixed(new(zone()) LFunctionLiteral, v0), instr);
2366 2367 2368 2369
}


LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2370
  ASSERT(argument_count_ == 0);
2371 2372
  allocator_->MarkAsOsrEntry();
  current_block_->last_environment()->set_ast_id(instr->ast_id());
2373
  return AssignEnvironment(new(zone()) LOsrEntry);
2374 2375 2376 2377
}


LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2378
  LParameter* result = new(zone()) LParameter;
2379
  if (instr->kind() == HParameter::STACK_PARAMETER) {
2380 2381 2382 2383 2384 2385
    int spill_index = chunk()->GetParameterStackSlot(instr->index());
    return DefineAsSpilled(result, spill_index);
  } else {
    ASSERT(info()->IsStub());
    CodeStubInterfaceDescriptor* descriptor =
        info()->code_stub()->GetInterfaceDescriptor(info()->isolate());
2386 2387
    int index = static_cast<int>(instr->index());
    Register reg = DESCRIPTOR_GET_PARAMETER_REGISTER(descriptor, index);
2388 2389
    return DefineFixed(result, reg);
  }
2390 2391 2392 2393 2394
}


LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
  int spill_index = chunk()->GetNextSpillIndex(false);  // Not double-width.
2395
  if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2396 2397 2398
    Abort("Too many spill slots needed for OSR");
    spill_index = 0;
  }
2399
  return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2400 2401 2402 2403 2404
}


LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
  argument_count_ -= instr->argument_count();
2405
  return MarkAsCall(DefineFixed(new(zone()) LCallStub, v0), instr);
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
}


LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
  // 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.
  return NULL;
}


LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2419
  info()->MarkAsRequiresFrame();
2420
  LOperand* args = UseRegister(instr->arguments());
2421 2422 2423 2424 2425 2426 2427
  LOperand* length;
  LOperand* index;
  if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
    length = UseRegisterOrConstant(instr->length());
    index = UseOrConstant(instr->index());
  } else {
    length = UseTempRegister(instr->length());
2428
    index = UseRegisterAtStart(instr->index());
2429
  }
2430
  return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2431 2432 2433 2434 2435
}


LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
  LOperand* object = UseFixed(instr->value(), a0);
2436
  LToFastProperties* result = new(zone()) LToFastProperties(object);
2437 2438 2439 2440 2441
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2442
  LTypeof* result = new(zone()) LTypeof(UseFixed(instr->value(), a0));
2443 2444 2445 2446 2447
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2448
  return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2449 2450 2451 2452 2453
}


LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
    HIsConstructCallAndBranch* instr) {
2454
  return new(zone()) LIsConstructCallAndBranch(TempRegister());
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
}


LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
  HEnvironment* env = current_block_->last_environment();
  ASSERT(env != NULL);

  env->set_ast_id(instr->ast_id());

  env->Drop(instr->pop_count());
2465
  for (int i = instr->values()->length() - 1; i >= 0; --i) {
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
    HValue* value = instr->values()->at(i);
    if (instr->HasAssignedIndexAt(i)) {
      env->Bind(instr->GetAssignedIndexAt(i), value);
    } else {
      env->Push(value);
    }
  }

  // If there is an instruction pending deoptimization environment create a
  // lazy bailout instruction to capture the environment.
  if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2477
    LInstruction* result = new(zone()) LLazyBailout;
2478
    result = AssignEnvironment(result);
2479 2480
    // Store the lazy deopt environment with the instruction if needed. Right
    // now it is only used for LInstanceOfKnownGlobal.
2481
    instruction_pending_deoptimization_environment_->
2482 2483
        SetDeferredLazyDeoptimizationEnvironment(result->environment());
    instruction_pending_deoptimization_environment_ = NULL;
2484
    pending_deoptimization_ast_id_ = BailoutId::None();
2485 2486 2487 2488 2489 2490 2491 2492 2493
    return result;
  }

  return NULL;
}


LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
  if (instr->is_function_entry()) {
2494
    return MarkAsCall(new(zone()) LStackCheck, instr);
2495 2496
  } else {
    ASSERT(instr->is_backwards_branch());
2497
    return AssignEnvironment(AssignPointerMap(new(zone()) LStackCheck));
2498 2499 2500 2501 2502 2503 2504 2505
  }
}


LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
  HEnvironment* outer = current_block_->last_environment();
  HConstant* undefined = graph()->GetConstantUndefined();
  HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2506
                                               instr->arguments_count(),
2507 2508
                                               instr->function(),
                                               undefined,
2509 2510
                                               instr->inlining_kind(),
                                               instr->undefined_receiver());
2511 2512 2513
  // 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());
2514
  }
2515
  inner->set_entry(instr);
2516 2517 2518 2519 2520 2521 2522
  current_block_->UpdateEnvironment(inner);
  chunk_->AddInlinedClosure(instr->closure());
  return NULL;
}


LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2523 2524 2525 2526
  LInstruction* pop = NULL;

  HEnvironment* env = current_block_->last_environment();

2527
  if (env->entry()->arguments_pushed()) {
2528 2529 2530 2531 2532
    int argument_count = env->arguments_environment()->parameter_count();
    pop = new(zone()) LDrop(argument_count);
    argument_count_ -= argument_count;
  }

2533 2534
  HEnvironment* outer = current_block_->last_environment()->
      DiscardInlined(false);
2535
  current_block_->UpdateEnvironment(outer);
2536 2537

  return pop;
2538 2539 2540
}


2541 2542
LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
  LOperand* object = UseFixed(instr->enumerable(), a0);
2543
  LForInPrepareMap* result = new(zone()) LForInPrepareMap(object);
2544 2545 2546 2547 2548 2549
  return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
  LOperand* map = UseRegister(instr->map());
2550
  return AssignEnvironment(DefineAsRegister(new(zone()) LForInCacheArray(map)));
2551 2552 2553 2554 2555 2556
}


LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
  LOperand* map = UseRegisterAtStart(instr->map());
2557
  return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2558 2559 2560 2561 2562 2563
}


LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
  LOperand* object = UseRegister(instr->object());
  LOperand* index = UseRegister(instr->index());
2564
  return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index));
2565 2566 2567
}


2568
} }  // namespace v8::internal