lithium-mips.cc 77.5 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
// 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

LOsrEntry::LOsrEntry() {
45
  for (int i = 0; i < Register::NumAllocatableRegisters(); ++i) {
46 47
    register_spills_[i] = NULL;
  }
48
  for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) {
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    double_register_spills_[i] = NULL;
  }
}


void LOsrEntry::MarkSpilledRegister(int allocation_index,
                                    LOperand* spill_operand) {
  ASSERT(spill_operand->IsStackSlot());
  ASSERT(register_spills_[allocation_index] == NULL);
  register_spills_[allocation_index] = spill_operand;
}


#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 LOsrEntry::MarkSpilledDoubleRegister(int allocation_index,
                                          LOperand* spill_operand) {
  ASSERT(spill_operand->IsDoubleStackSlot());
  ASSERT(double_register_spills_[allocation_index] == NULL);
  double_register_spills_[allocation_index] = spill_operand;
}


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);
  }
}


111
void LInstruction::PrintDataTo(StringStream* stream) {
112
  stream->Add("= ");
113
  for (int i = 0; i < InputCount(); i++) {
114
    if (i > 0) stream->Add(" ");
115 116 117 118 119
    if (InputAt(i) == NULL) {
      stream->Add("NULL");
    } else {
      InputAt(i)->PrintTo(stream);
    }
120 121 122 123
  }
}


124 125
void LInstruction::PrintOutputOperandTo(StringStream* stream) {
  if (HasResult()) result()->PrintTo(stream);
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}


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";
184
    case Token::ROR: return "ror-t";
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    case Token::SHL: return "sll-t";
    case Token::SAR: return "sra-t";
    case Token::SHR: return "srl-t";
    default:
      UNREACHABLE();
      return NULL;
  }
}


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());
202
  value()->PrintTo(stream);
203 204 205 206 207
}


void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if ");
208
  left()->PrintTo(stream);
209
  stream->Add(" %s ", Token::String(op()));
210
  right()->PrintTo(stream);
211 212 213 214 215 216
  stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
}


void LIsNilAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if ");
217
  value()->PrintTo(stream);
218 219 220 221 222 223 224 225
  stream->Add(kind() == kStrictEquality ? " === " : " == ");
  stream->Add(nil() == kNullValue ? "null" : "undefined");
  stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
}


void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_object(");
226
  value()->PrintTo(stream);
227 228 229 230
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


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


238 239
void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_smi(");
240
  value()->PrintTo(stream);
241 242 243 244 245 246
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if is_undetectable(");
247
  value()->PrintTo(stream);
248 249 250 251
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


252 253
void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if string_compare(");
254 255
  left()->PrintTo(stream);
  right()->PrintTo(stream);
256 257 258 259
  stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
}


260 261
void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
  stream->Add("if has_instance_type(");
262
  value()->PrintTo(stream);
263 264 265 266 267 268
  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(");
269
  value()->PrintTo(stream);
270 271 272 273 274 275
  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(");
276
  value()->PrintTo(stream);
277 278 279 280 281 282 283 284 285
  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 ");
286
  value()->PrintTo(stream);
287 288 289 290 291 292 293 294 295 296 297 298 299
  stream->Add(" == \"%s\" then B%d else B%d",
              *hydrogen()->type_literal()->ToCString(),
              true_block_id(), false_block_id());
}


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


void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
  stream->Add("/%s ", hydrogen()->OpName());
300
  value()->PrintTo(stream);
301 302 303
}


304 305 306 307 308
void LMathExp::PrintDataTo(StringStream* stream) {
  value()->PrintTo(stream);
}


309
void LLoadContextSlot::PrintDataTo(StringStream* stream) {
310
  context()->PrintTo(stream);
311 312 313 314 315
  stream->Add("[%d]", slot_index());
}


void LStoreContextSlot::PrintDataTo(StringStream* stream) {
316
  context()->PrintTo(stream);
317
  stream->Add("[%d] <- ", slot_index());
318
  value()->PrintTo(stream);
319 320 321 322 323
}


void LInvokeFunction::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
324
  function()->PrintTo(stream);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  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("= ");
353
  constructor()->PrintTo(stream);
354 355 356 357
  stream->Add(" #%d / ", arity());
}


358 359 360 361 362 363 364 365 366 367 368
void LCallNewArray::PrintDataTo(StringStream* stream) {
  stream->Add("= ");
  constructor()->PrintTo(stream);
  stream->Add(" #%d / ", arity());
  ASSERT(hydrogen()->property_cell()->value()->IsSmi());
  ElementsKind kind = static_cast<ElementsKind>(
      Smi::cast(hydrogen()->property_cell()->value())->value());
  stream->Add(" (%s) ", ElementsKindToString(kind));
}


369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
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);
  stream->Add(".");
  stream->Add(*String::cast(*name())->ToCString());
  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);
}


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


408 409
void LStoreKeyed::PrintDataTo(StringStream* stream) {
  elements()->PrintTo(stream);
410 411
  stream->Add("[");
  key()->PrintTo(stream);
412 413 414 415 416
  if (hydrogen()->IsDehoisted()) {
    stream->Add(" + %d] <-", additional_index());
  } else {
    stream->Add("] <- ");
  }
417 418 419 420
  value()->PrintTo(stream);
}


421 422 423 424 425 426 427 428 429
void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
  stream->Add("[");
  key()->PrintTo(stream);
  stream->Add("] <- ");
  value()->PrintTo(stream);
}


430 431 432 433 434 435
void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
  object()->PrintTo(stream);
  stream->Add(" %p -> %p", *original_map(), *transitioned_map());
}


436
int LPlatformChunk::GetNextSpillIndex(bool is_double) {
437 438 439 440 441 442
  // Skip a slot if for a double-width slot.
  if (is_double) spill_slot_count_++;
  return spill_slot_count_++;
}


443
LOperand* LPlatformChunk::GetNextSpillSlot(bool is_double)  {
444 445
  int index = GetNextSpillIndex(is_double);
  if (is_double) {
446
    return LDoubleStackSlot::Create(index, zone());
447
  } else {
448
    return LStackSlot::Create(index, zone());
449 450 451 452
  }
}


453
LPlatformChunk* LChunkBuilder::Build() {
454
  ASSERT(is_unused());
455
  chunk_ = new(zone()) LPlatformChunk(info(), graph());
456
  HPhase phase("L_Building chunk", chunk_);
457 458 459 460 461 462 463 464 465 466 467 468 469
  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_;
}


470 471
void LCodeGen::Abort(const char* reason) {
  info()->set_bailout_reason(reason);
472 473 474 475 476
  status_ = ABORTED;
}


LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
477 478
  return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
                                  Register::ToAllocationIndex(reg));
479 480 481 482
}


LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
483 484
  return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
                                  DoubleRegister::ToAllocationIndex(reg));
485 486 487 488 489 490 491 492 493 494 495 496 497 498
}


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) {
499
  return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
500 501 502 503 504
}


LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
  return Use(value,
505 506
             new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
                                      LUnallocated::USED_AT_START));
507 508 509 510
}


LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
511
  return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
512 513 514 515
}


LOperand* LChunkBuilder::Use(HValue* value) {
516
  return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
517 518 519 520
}


LOperand* LChunkBuilder::UseAtStart(HValue* value) {
521
  return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
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 547 548 549 550 551 552 553 554 555 556
                                     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);
}


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


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


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


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


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


template<int I, int T>
LInstruction* LChunkBuilder::DefineSameAsFirst(
    LTemplateInstruction<1, I, T>* instr) {
599 600
  return Define(instr,
                new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
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 628 629
}


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) {
630
  info()->MarkAsNonDeferredCalling();
631 632 633 634 635 636
#ifdef DEBUG
  instr->VerifyCall();
#endif
  instr->MarkAsCall();
  instr = AssignPointerMap(instr);

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

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

  return instr;
}


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


LUnallocated* LChunkBuilder::TempRegister() {
669 670
  LUnallocated* operand =
      new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
671 672
  operand->set_virtual_register(allocator_->GetVirtualRegister());
  if (!allocator_->AllocationOk()) Abort("Not enough virtual registers.");
673 674 675 676 677 678
  return operand;
}


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


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


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


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


701
LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
702
  return AssignEnvironment(new(zone()) LDeoptimize);
703 704 705 706
}


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


LInstruction* LChunkBuilder::DoShift(Token::Value op,
                                     HBitwiseBinaryOperation* instr) {
  if (instr->representation().IsTagged()) {
    ASSERT(instr->left()->representation().IsTagged());
    ASSERT(instr->right()->representation().IsTagged());

    LOperand* left = UseFixed(instr->left(), a1);
    LOperand* right = UseFixed(instr->right(), a0);
719
    LArithmeticT* result = new(zone()) LArithmeticT(op, left, right);
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    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);
  }

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

  LInstruction* result =
756
      DefineAsRegister(new(zone()) LShiftI(op, left, right, does_deopt));
757 758 759 760 761 762 763 764 765 766 767 768
  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());
769
  LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
  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();
  ASSERT(left->representation().IsTagged());
  ASSERT(right->representation().IsTagged());
  LOperand* left_operand = UseFixed(left, a1);
  LOperand* right_operand = UseFixed(right, a0);
787 788
  LArithmeticT* result =
      new(zone()) LArithmeticT(op, left_operand, right_operand);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
  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);
      last_environment->SetValueAt(phi->merged_index(), phi);
    }
    for (int i = 0; i < block->deleted_phis()->length(); ++i) {
      last_environment->SetValueAt(block->deleted_phis()->at(i),
                                   graph_->GetConstantUndefined());
    }
    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) {
    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);
883 884
  BailoutId ast_id = hydrogen_env->ast_id();
  ASSERT(!ast_id.IsNone() ||
885
         hydrogen_env->frame_type() != JS_FUNCTION);
886
  int value_count = hydrogen_env->length();
887 888 889 890 891 892 893
  LEnvironment* result = new(zone()) LEnvironment(
      hydrogen_env->closure(),
      hydrogen_env->frame_type(),
      ast_id,
      hydrogen_env->parameter_count(),
      argument_count_,
      value_count,
894
      outer,
895
      hydrogen_env->entry(),
896
      zone());
897
  int argument_index = *argument_index_accumulator;
898 899 900 901 902 903 904 905
  for (int i = 0; i < value_count; ++i) {
    if (hydrogen_env->is_special_index(i)) continue;

    HValue* value = hydrogen_env->values()->at(i);
    LOperand* op = NULL;
    if (value->IsArgumentsObject()) {
      op = NULL;
    } else if (value->IsPushArgument()) {
906
      op = new(zone()) LArgument(argument_index++);
907 908 909
    } else {
      op = UseAny(value);
    }
910 911 912
    result->AddValue(op,
                     value->representation(),
                     value->CheckFlag(HInstruction::kUint32));
913 914
  }

915
  if (hydrogen_env->frame_type() == JS_FUNCTION) {
916 917 918
    *argument_index_accumulator = argument_index;
  }

919 920 921 922 923
  return result;
}


LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
924
  return new(zone()) LGoto(instr->FirstSuccessor()->block_id());
925 926 927 928
}


LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
929 930 931
  HValue* value = instr->value();
  if (value->EmitAtUses()) {
    HBasicBlock* successor = HConstant::cast(value)->ToBoolean()
932 933
        ? instr->FirstSuccessor()
        : instr->SecondSuccessor();
934
    return new(zone()) LGoto(successor->block_id());
935
  }
936

937
  LBranch* result = new(zone()) LBranch(UseRegister(value));
938 939 940 941 942 943 944 945
  // Tagged values that are not known smis or booleans require a
  // deoptimization environment.
  Representation rep = value->representation();
  HType type = value->type();
  if (rep.IsTagged() && !type.IsSmi() && !type.IsBoolean()) {
    return AssignEnvironment(result);
  }
  return result;
946 947 948 949 950 951 952
}


LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* value = UseRegisterAtStart(instr->value());
  LOperand* temp = TempRegister();
953
  return new(zone()) LCmpMapAndBranch(value, temp);
954 955 956 957
}


LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
958 959
  return DefineAsRegister(
      new(zone()) LArgumentsLength(UseRegister(length->value())));
960 961 962 963
}


LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
964
  return DefineAsRegister(new(zone()) LArgumentsElements);
965 966 967 968 969
}


LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
  LInstanceOf* result =
970 971
      new(zone()) LInstanceOf(UseFixed(instr->left(), a0),
                              UseFixed(instr->right(), a1));
972 973 974 975 976 977 978
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
    HInstanceOfKnownGlobal* instr) {
  LInstanceOfKnownGlobal* result =
979 980
      new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->left(), a0),
                                         FixedTemp(t0));
981 982 983 984
  return MarkAsCall(DefineFixed(result, v0), instr);
}


985 986 987 988 989 990
LInstruction* LChunkBuilder::DoInstanceSize(HInstanceSize* instr) {
  LOperand* object = UseRegisterAtStart(instr->object());
  return DefineAsRegister(new(zone()) LInstanceSize(object));
}


991 992 993 994 995 996 997 998
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));
}


999 1000 1001 1002 1003
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);
1004 1005 1006 1007
  LApplyArguments* result = new(zone()) LApplyArguments(function,
                                                        receiver,
                                                        length,
                                                        elements);
1008 1009 1010 1011 1012 1013 1014
  return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
  ++argument_count_;
  LOperand* argument = Use(instr->argument());
1015
  return new(zone()) LPushArgument(argument);
1016 1017 1018 1019
}


LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1020 1021 1022
  return instr->HasNoUses()
      ? NULL
      : DefineAsRegister(new(zone()) LThisFunction);
1023 1024 1025 1026
}


LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1027 1028 1029 1030 1031 1032 1033 1034
  // 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;
1035 1036 1037 1038 1039
}


LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
  LOperand* context = UseRegisterAtStart(instr->value());
1040
  return DefineAsRegister(new(zone()) LOuterContext(context));
1041 1042 1043
}


1044
LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1045
  return MarkAsCall(new(zone()) LDeclareGlobals, instr);
1046 1047 1048
}


1049 1050
LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
  LOperand* context = UseRegisterAtStart(instr->value());
1051
  return DefineAsRegister(new(zone()) LGlobalObject(context));
1052 1053 1054 1055 1056
}


LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
  LOperand* global_object = UseRegisterAtStart(instr->value());
1057
  return DefineAsRegister(new(zone()) LGlobalReceiver(global_object));
1058 1059 1060 1061 1062 1063
}


LInstruction* LChunkBuilder::DoCallConstantFunction(
    HCallConstantFunction* instr) {
  argument_count_ -= instr->argument_count();
1064
  return MarkAsCall(DefineFixed(new(zone()) LCallConstantFunction, v0), instr);
1065 1066 1067 1068 1069 1070
}


LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
  LOperand* function = UseFixed(instr->function(), a1);
  argument_count_ -= instr->argument_count();
1071
  LInvokeFunction* result = new(zone()) LInvokeFunction(function);
1072 1073 1074 1075 1076 1077
  return MarkAsCall(DefineFixed(result, v0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
  BuiltinFunctionId op = instr->op();
1078
  if (op == kMathLog || op == kMathSin || op == kMathCos || op == kMathTan) {
1079
    LOperand* input = UseFixedDouble(instr->value(), f4);
1080
    LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input, NULL);
1081
    return MarkAsCall(DefineFixedDouble(result, f4), instr);
1082 1083 1084 1085 1086 1087 1088 1089 1090
  } else if (op == kMathExp) {
    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);
1091 1092 1093 1094 1095
  } else if (op == kMathPowHalf) {
    // Input cannot be the same as the result.
    // See lithium-codegen-mips.cc::DoMathPowHalf.
    LOperand* input = UseFixedDouble(instr->value(), f8);
    LOperand* temp = FixedTemp(f6);
1096
    LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input, temp);
1097
    return DefineFixedDouble(result, f4);
1098 1099
  } else {
    LOperand* input = UseRegisterAtStart(instr->value());
1100 1101 1102

    LOperand* temp = (op == kMathRound) ? FixedTemp(f6) :
        (op == kMathFloor) ? TempRegister() : NULL;
1103
    LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input, temp);
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    switch (op) {
      case kMathAbs:
        return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
      case kMathFloor:
        return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
      case kMathSqrt:
        return DefineAsRegister(result);
      case kMathRound:
        return AssignEnvironment(DefineAsRegister(result));
      default:
        UNREACHABLE();
        return NULL;
    }
  }
}


LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
  ASSERT(instr->key()->representation().IsTagged());
  argument_count_ -= instr->argument_count();
  LOperand* key = UseFixed(instr->key(), a2);
1125
  return MarkAsCall(DefineFixed(new(zone()) LCallKeyed(key), v0), instr);
1126 1127 1128 1129 1130
}


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


LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
  argument_count_ -= instr->argument_count();
1137
  return MarkAsCall(DefineFixed(new(zone()) LCallGlobal, v0), instr);
1138 1139 1140 1141 1142
}


LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
  argument_count_ -= instr->argument_count();
1143
  return MarkAsCall(DefineFixed(new(zone()) LCallKnownGlobal, v0), instr);
1144 1145 1146 1147 1148 1149
}


LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
  LOperand* constructor = UseFixed(instr->constructor(), a1);
  argument_count_ -= instr->argument_count();
1150
  LCallNew* result = new(zone()) LCallNew(constructor);
1151 1152 1153 1154
  return MarkAsCall(DefineFixed(result, v0), instr);
}


1155 1156 1157 1158 1159 1160 1161 1162
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);
}


1163
LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1164
  LOperand* function = UseFixed(instr->function(), a1);
1165
  argument_count_ -= instr->argument_count();
1166 1167
  return MarkAsCall(DefineFixed(new(zone()) LCallFunction(function), v0),
                    instr);
1168 1169 1170 1171 1172
}


LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
  argument_count_ -= instr->argument_count();
1173
  return MarkAsCall(DefineFixed(new(zone()) LCallRuntime, v0), instr);
1174 1175 1176
}


1177 1178 1179 1180 1181
LInstruction* LChunkBuilder::DoRor(HRor* instr) {
  return DoShift(Token::ROR, instr);
}


1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
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);
}


1197 1198 1199 1200 1201 1202 1203
LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
  if (instr->representation().IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());

    LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
    LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1204
    return DefineAsRegister(new(zone()) LBitI(left, right));
1205 1206 1207 1208 1209 1210 1211
  } else {
    ASSERT(instr->representation().IsTagged());
    ASSERT(instr->left()->representation().IsTagged());
    ASSERT(instr->right()->representation().IsTagged());

    LOperand* left = UseFixed(instr->left(), a1);
    LOperand* right = UseFixed(instr->right(), a0);
1212
    LArithmeticT* result = new(zone()) LArithmeticT(instr->op(), left, right);
1213 1214
    return MarkAsCall(DefineFixed(result, v0), instr);
  }
1215 1216 1217 1218 1219 1220
}


LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
  ASSERT(instr->value()->representation().IsInteger32());
  ASSERT(instr->representation().IsInteger32());
1221
  if (instr->HasNoUses()) return NULL;
1222 1223
  LOperand* value = UseRegisterAtStart(instr->value());
  return DefineAsRegister(new(zone()) LBitNotI(value));
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
}


LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
  if (instr->representation().IsDouble()) {
    return DoArithmeticD(Token::DIV, instr);
  } else if (instr->representation().IsInteger32()) {
    // TODO(1042) The fixed register allocation
    // is needed because we call TypeRecordingBinaryOpStub from
    // the generated code, which requires registers a0
    // and a1 to be used. We should remove that
    // when we provide a native implementation.
    LOperand* dividend = UseFixed(instr->left(), a0);
    LOperand* divisor = UseFixed(instr->right(), a1);
    return AssignEnvironment(AssignPointerMap(
1239
             DefineFixed(new(zone()) LDivI(dividend, divisor), v0)));
1240 1241 1242 1243 1244 1245
  } else {
    return DoArithmeticT(Token::DIV, instr);
  }
}


1246 1247 1248 1249 1250 1251
LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
  UNIMPLEMENTED();
  return NULL;
}


1252 1253 1254 1255 1256 1257 1258 1259 1260
LInstruction* LChunkBuilder::DoMod(HMod* instr) {
  if (instr->representation().IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());

    LModI* mod;
    if (instr->HasPowerOf2Divisor()) {
      ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
      LOperand* value = UseRegisterAtStart(instr->left());
1261
      mod = new(zone()) LModI(value, UseOrConstant(instr->right()));
1262 1263 1264
    } else {
      LOperand* dividend = UseRegister(instr->left());
      LOperand* divisor = UseRegister(instr->right());
1265 1266 1267 1268 1269
      mod = new(zone()) LModI(dividend,
                              divisor,
                              TempRegister(),
                              FixedTemp(f20),
                              FixedTemp(f22));
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    }

    if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
        instr->CheckFlag(HValue::kCanBeDivByZero)) {
      return AssignEnvironment(DefineAsRegister(mod));
    } else {
      return DefineAsRegister(mod);
    }
  } else if (instr->representation().IsTagged()) {
    return DoArithmeticT(Token::MOD, instr);
  } else {
    ASSERT(instr->representation().IsDouble());
    // We call a C function for double modulo. It can't trigger a GC.
    // We need to use fixed result register for the call.
    // TODO(fschneider): Allow any register as input registers.
    LOperand* left = UseFixedDouble(instr->left(), f2);
    LOperand* right = UseFixedDouble(instr->right(), f4);
1287
    LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right);
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    return MarkAsCall(DefineFixedDouble(result, f2), instr);
  }
}


LInstruction* LChunkBuilder::DoMul(HMul* instr) {
  if (instr->representation().IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());
    LOperand* left;
    LOperand* right = UseOrConstant(instr->MostConstantOperand());
    LOperand* temp = NULL;
    if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
        (instr->CheckFlag(HValue::kCanOverflow) ||
        !right->IsConstantOperand())) {
      left = UseRegister(instr->LeastConstantOperand());
      temp = TempRegister();
    } else {
      left = UseRegisterAtStart(instr->LeastConstantOperand());
    }
1308
    LMulI* mul = new(zone()) LMulI(left, right, temp);
1309 1310 1311 1312 1313
    if (instr->CheckFlag(HValue::kCanOverflow) ||
        instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
      AssignEnvironment(mul);
    }
    return DefineAsRegister(mul);
1314 1315

  } else if (instr->representation().IsDouble()) {
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    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;
        }
      }
    }
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    return DoArithmeticD(Token::MUL, instr);
  } else {
    return DoArithmeticT(Token::MUL, instr);
  }
}


LInstruction* LChunkBuilder::DoSub(HSub* instr) {
  if (instr->representation().IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());
    LOperand* left = UseRegisterAtStart(instr->left());
    LOperand* right = UseOrConstantAtStart(instr->right());
1344
    LSubI* sub = new(zone()) LSubI(left, right);
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
    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);
  }
}


1358 1359 1360 1361 1362 1363 1364 1365 1366
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));
}


1367 1368 1369 1370 1371 1372
LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
  if (instr->representation().IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());
    LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
    LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1373
    LAddI* add = new(zone()) LAddI(left, right);
1374 1375 1376 1377 1378 1379
    LInstruction* result = DefineAsRegister(add);
    if (instr->CheckFlag(HValue::kCanOverflow)) {
      result = AssignEnvironment(result);
    }
    return result;
  } else if (instr->representation().IsDouble()) {
1380 1381 1382 1383 1384 1385 1386 1387 1388
    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());
      }
    }
1389 1390 1391 1392 1393 1394 1395 1396
    return DoArithmeticD(Token::ADD, instr);
  } else {
    ASSERT(instr->representation().IsTagged());
    return DoArithmeticT(Token::ADD, instr);
  }
}


1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
  LOperand* left = NULL;
  LOperand* right = NULL;
  if (instr->representation().IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());
    left = UseRegisterAtStart(instr->LeastConstantOperand());
    right = UseOrConstantAtStart(instr->MostConstantOperand());
  } 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));
}


1416 1417 1418 1419 1420 1421 1422 1423 1424
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) :
1425
      UseFixed(instr->right(), a2);
1426
  LPower* result = new(zone()) LPower(left, right);
1427
  return MarkAsCall(DefineFixedDouble(result, f0),
1428 1429 1430 1431
                    instr,
                    CAN_DEOPTIMIZE_EAGERLY);
}

1432 1433 1434 1435 1436

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

1441 1442 1443 1444 1445 1446

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);
1447
  LCmpT* result = new(zone()) LCmpT(left, right);
1448
  return MarkAsCall(DefineFixed(result, v0), instr);
1449 1450 1451 1452 1453
}


LInstruction* LChunkBuilder::DoCompareIDAndBranch(
    HCompareIDAndBranch* instr) {
1454
  Representation r = instr->representation();
1455 1456 1457 1458 1459
  if (r.IsInteger32()) {
    ASSERT(instr->left()->representation().IsInteger32());
    ASSERT(instr->right()->representation().IsInteger32());
    LOperand* left = UseRegisterOrConstantAtStart(instr->left());
    LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1460
    return new(zone()) LCmpIDAndBranch(left, right);
1461 1462 1463 1464 1465 1466
  } else {
    ASSERT(r.IsDouble());
    ASSERT(instr->left()->representation().IsDouble());
    ASSERT(instr->right()->representation().IsDouble());
    LOperand* left = UseRegisterAtStart(instr->left());
    LOperand* right = UseRegisterAtStart(instr->right());
1467
    return new(zone()) LCmpIDAndBranch(left, right);
1468 1469 1470 1471 1472 1473 1474 1475
  }
}


LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
    HCompareObjectEqAndBranch* instr) {
  LOperand* left = UseRegisterAtStart(instr->left());
  LOperand* right = UseRegisterAtStart(instr->right());
1476
  return new(zone()) LCmpObjectEqAndBranch(left, right);
1477 1478 1479 1480 1481
}


LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
    HCompareConstantEqAndBranch* instr) {
1482 1483
  return new(zone()) LCmpConstantEqAndBranch(
      UseRegisterAtStart(instr->value()));
1484 1485 1486 1487 1488
}


LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1489
  return new(zone()) LIsNilAndBranch(UseRegisterAtStart(instr->value()));
1490 1491 1492 1493 1494 1495
}


LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* temp = TempRegister();
1496 1497
  return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()),
                                        temp);
1498 1499 1500
}


1501 1502 1503
LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
  LOperand* temp = TempRegister();
1504 1505
  return new(zone()) LIsStringAndBranch(UseRegisterAtStart(instr->value()),
                                        temp);
1506 1507 1508
}


1509 1510
LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1511
  return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1512 1513 1514 1515 1516 1517
}


LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
    HIsUndetectableAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1518 1519
  return new(zone()) LIsUndetectableAndBranch(
      UseRegisterAtStart(instr->value()), TempRegister());
1520 1521 1522
}


1523 1524 1525 1526 1527 1528
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);
1529 1530
  LStringCompareAndBranch* result =
      new(zone()) LStringCompareAndBranch(left, right);
1531 1532 1533 1534
  return MarkAsCall(result, instr);
}


1535 1536 1537
LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
    HHasInstanceTypeAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1538 1539
  LOperand* value = UseRegisterAtStart(instr->value());
  return new(zone()) LHasInstanceTypeAndBranch(value);
1540 1541 1542 1543 1544 1545 1546 1547
}


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

1548
  return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1549 1550 1551 1552 1553 1554
}


LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
    HHasCachedArrayIndexAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1555
  return new(zone()) LHasCachedArrayIndexAndBranch(
1556 1557 1558 1559 1560 1561 1562
      UseRegisterAtStart(instr->value()));
}


LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
    HClassOfTestAndBranch* instr) {
  ASSERT(instr->value()->representation().IsTagged());
1563 1564
  return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
                                           TempRegister());
1565 1566 1567 1568 1569
}


LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
  LOperand* array = UseRegisterAtStart(instr->value());
1570
  return DefineAsRegister(new(zone()) LJSArrayLength(array));
1571 1572 1573 1574 1575 1576
}


LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
    HFixedArrayBaseLength* instr) {
  LOperand* array = UseRegisterAtStart(instr->value());
1577
  return DefineAsRegister(new(zone()) LFixedArrayBaseLength(array));
1578 1579 1580
}


1581 1582 1583 1584 1585 1586
LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
  LOperand* map = UseRegisterAtStart(instr->value());
  return DefineAsRegister(new(zone()) LMapEnumLength(map));
}


1587 1588
LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
  LOperand* object = UseRegisterAtStart(instr->value());
1589
  return DefineAsRegister(new(zone()) LElementsKind(object));
1590 1591 1592 1593 1594
}


LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
  LOperand* object = UseRegister(instr->value());
1595
  LValueOf* result = new(zone()) LValueOf(object, TempRegister());
1596
  return DefineAsRegister(result);
1597 1598 1599
}


1600 1601
LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
  LOperand* object = UseFixed(instr->value(), a0);
1602 1603
  LDateField* result =
      new(zone()) LDateField(object, FixedTemp(a1), instr->index());
1604
  return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
1605 1606 1607
}


1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
  LOperand* string = UseRegister(instr->string());
  LOperand* index = UseRegister(instr->index());
  LOperand* value = UseRegister(instr->value());
  LSeqStringSetChar* result =
      new(zone()) LSeqStringSetChar(instr->encoding(), string, index, value);
  return DefineAsRegister(result);
}


1618 1619 1620 1621 1622
LInstruction* LChunkBuilder::DoNumericConstraint(HNumericConstraint* instr) {
  return NULL;
}


1623 1624 1625 1626 1627 1628
LInstruction* LChunkBuilder::DoInductionVariableAnnotation(
    HInductionVariableAnnotation* instr) {
  return NULL;
}


1629
LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1630
  LOperand* value = UseRegisterOrConstantAtStart(instr->index());
1631 1632
  LOperand* length = UseRegister(instr->length());
  return AssignEnvironment(new(zone()) LBoundsCheck(value, length));
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
}


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);
1645
  return MarkAsCall(new(zone()) LThrow(value), instr);
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
}


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();
  if (from.IsTagged()) {
    if (to.IsDouble()) {
1667
      info()->MarkAsDeferredCalling();
1668
      LOperand* value = UseRegister(instr->value());
1669
      LNumberUntagD* res = new(zone()) LNumberUntagD(value);
1670 1671 1672
      return AssignEnvironment(DefineAsRegister(res));
    } else {
      ASSERT(to.IsInteger32());
1673
      LOperand* value = UseRegisterAtStart(instr->value());
1674
      LInstruction* res = NULL;
1675 1676
      if (instr->value()->type().IsSmi()) {
        res = DefineAsRegister(new(zone()) LSmiUntag(value, false));
1677 1678 1679 1680
      } else {
        LOperand* temp1 = TempRegister();
        LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
                                                      : NULL;
1681
        LOperand* temp3 = FixedTemp(f22);
1682 1683 1684 1685
        res = DefineSameAsFirst(new(zone()) LTaggedToI(value,
                                                       temp1,
                                                       temp2,
                                                       temp3));
1686 1687 1688 1689 1690 1691
        res = AssignEnvironment(res);
      }
      return res;
    }
  } else if (from.IsDouble()) {
    if (to.IsTagged()) {
1692
      info()->MarkAsDeferredCalling();
1693 1694 1695 1696 1697 1698 1699
      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();
1700
      LNumberTagD* result = new(zone()) LNumberTagD(value, temp1, temp2);
1701 1702 1703 1704 1705
      Define(result, result_temp);
      return AssignPointerMap(result);
    } else {
      ASSERT(to.IsInteger32());
      LOperand* value = UseRegister(instr->value());
1706 1707 1708
      LOperand* temp1 = TempRegister();
      LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister() : NULL;
      LDoubleToI* res = new(zone()) LDoubleToI(value, temp1, temp2);
1709 1710 1711
      return AssignEnvironment(DefineAsRegister(res));
    }
  } else if (from.IsInteger32()) {
1712
    info()->MarkAsDeferredCalling();
1713 1714
    if (to.IsTagged()) {
      HValue* val = instr->value();
1715
      LOperand* value = UseRegisterAtStart(val);
1716 1717 1718 1719
      if (val->CheckFlag(HInstruction::kUint32)) {
        LNumberTagU* result = new(zone()) LNumberTagU(value);
        return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
      } else if (val->HasRange() && val->range()->IsInSmiRange()) {
1720
        return DefineAsRegister(new(zone()) LSmiTag(value));
1721
      } else {
1722
        LNumberTagI* result = new(zone()) LNumberTagI(value);
1723
        return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1724 1725 1726
      }
    } else {
      ASSERT(to.IsDouble());
1727 1728 1729 1730 1731 1732 1733
      if (instr->value()->CheckFlag(HInstruction::kUint32)) {
        return DefineAsRegister(
            new(zone()) LUint32ToDouble(UseRegister(instr->value())));
      } else {
        return DefineAsRegister(
            new(zone()) LInteger32ToDouble(Use(instr->value())));
      }
1734 1735 1736 1737 1738 1739 1740 1741 1742
    }
  }
  UNREACHABLE();
  return NULL;
}


LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
1743
  return AssignEnvironment(new(zone()) LCheckNonSmi(value));
1744 1745 1746 1747 1748
}


LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
1749
  LInstruction* result = new(zone()) LCheckInstanceType(value);
1750 1751 1752 1753 1754
  return AssignEnvironment(result);
}


LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1755
  LUnallocated* temp1 = TempRegister();
1756
  LOperand* temp2 = TempRegister();
1757 1758
  LCheckPrototypeMaps* result = new(zone()) LCheckPrototypeMaps(temp1, temp2);
  return AssignEnvironment(Define(result, temp1));
1759 1760 1761 1762 1763
}


LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
1764
  return AssignEnvironment(new(zone()) LCheckSmi(value));
1765 1766 1767
}


1768 1769 1770 1771 1772 1773
LInstruction* LChunkBuilder::DoCheckSmiOrInt32(HCheckSmiOrInt32* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
  return AssignEnvironment(new(zone()) LCheckSmi(value));
}


1774 1775
LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
1776
  return AssignEnvironment(new(zone()) LCheckFunction(value));
1777 1778 1779
}


1780
LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
1781
  LOperand* value = UseRegisterAtStart(instr->value());
1782
  LInstruction* result = new(zone()) LCheckMaps(value);
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
  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.
1793
    return DefineAsRegister(new(zone()) LClampDToUint8(reg, FixedTemp(f22)));
1794
  } else if (input_rep.IsInteger32()) {
1795
    return DefineAsRegister(new(zone()) LClampIToUint8(reg));
1796 1797 1798 1799
  } else {
    ASSERT(input_rep.IsTagged());
    // Register allocator doesn't (yet) support allocation of double
    // temps. Reserve f22 explicitly.
1800
    LClampTToUint8* result = new(zone()) LClampTToUint8(reg, FixedTemp(f22));
1801 1802 1803 1804 1805 1806
    return AssignEnvironment(DefineAsRegister(result));
  }
}


LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1807 1808 1809
  LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
  return new(zone()) LReturn(UseFixed(instr->value(), v0),
                             parameter_count);
1810 1811 1812 1813 1814 1815
}


LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
  Representation r = instr->representation();
  if (r.IsInteger32()) {
1816
    return DefineAsRegister(new(zone()) LConstantI);
1817
  } else if (r.IsDouble()) {
1818
    return DefineAsRegister(new(zone()) LConstantD);
1819
  } else if (r.IsTagged()) {
1820
    return DefineAsRegister(new(zone()) LConstantT);
1821 1822 1823 1824 1825 1826 1827 1828
  } else {
    UNREACHABLE();
    return NULL;
  }
}


LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1829
  LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
1830 1831 1832 1833 1834 1835 1836 1837
  return instr->RequiresHoleCheck()
      ? AssignEnvironment(DefineAsRegister(result))
      : DefineAsRegister(result);
}


LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
  LOperand* global_object = UseFixed(instr->global_object(), a0);
1838
  LLoadGlobalGeneric* result = new(zone()) LLoadGlobalGeneric(global_object);
1839 1840 1841 1842 1843
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
1844 1845 1846 1847
  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()
1848 1849
      ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
      : new(zone()) LStoreGlobalCell(value, NULL);
1850 1851 1852 1853 1854 1855 1856
}


LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
  LOperand* global_object = UseFixed(instr->global_object(), a1);
  LOperand* value = UseFixed(instr->value(), a0);
  LStoreGlobalGeneric* result =
1857
      new(zone()) LStoreGlobalGeneric(global_object, value);
1858 1859 1860 1861 1862 1863
  return MarkAsCall(result, instr);
}


LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
  LOperand* context = UseRegisterAtStart(instr->value());
1864 1865
  LInstruction* result =
      DefineAsRegister(new(zone()) LLoadContextSlot(context));
1866
  return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
}


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());
  }
1880
  LInstruction* result = new(zone()) LStoreContextSlot(context, value);
1881
  return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1882 1883 1884 1885 1886
}


LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
  return DefineAsRegister(
1887
      new(zone()) LLoadNamedField(UseRegisterAtStart(instr->object())));
1888 1889 1890 1891 1892 1893 1894 1895
}


LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
    HLoadNamedFieldPolymorphic* instr) {
  ASSERT(instr->representation().IsTagged());
  if (instr->need_generic()) {
    LOperand* obj = UseFixed(instr->object(), a0);
1896 1897
    LLoadNamedFieldPolymorphic* result =
        new(zone()) LLoadNamedFieldPolymorphic(obj);
1898 1899 1900
    return MarkAsCall(DefineFixed(result, v0), instr);
  } else {
    LOperand* obj = UseRegisterAtStart(instr->object());
1901 1902
    LLoadNamedFieldPolymorphic* result =
        new(zone()) LLoadNamedFieldPolymorphic(obj);
1903 1904 1905 1906 1907 1908 1909
    return AssignEnvironment(DefineAsRegister(result));
  }
}


LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
  LOperand* object = UseFixed(instr->object(), a0);
1910
  LInstruction* result = DefineFixed(new(zone()) LLoadNamedGeneric(object), v0);
1911 1912 1913 1914 1915 1916 1917
  return MarkAsCall(result, instr);
}


LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
    HLoadFunctionPrototype* instr) {
  return AssignEnvironment(DefineAsRegister(
1918
      new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
1919 1920 1921 1922 1923
}


LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
  LOperand* input = UseRegisterAtStart(instr->value());
1924
  return DefineAsRegister(new(zone()) LLoadElements(input));
1925 1926 1927 1928 1929 1930
}


LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
    HLoadExternalArrayPointer* instr) {
  LOperand* input = UseRegisterAtStart(instr->value());
1931
  return DefineAsRegister(new(zone()) LLoadExternalArrayPointer(input));
1932 1933 1934
}


1935
LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
1936 1937
  ASSERT(instr->key()->representation().IsInteger32() ||
         instr->key()->representation().IsTagged());
1938
  ElementsKind elements_kind = instr->elements_kind();
1939
  LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1940
  LLoadKeyed* result = NULL;
1941

1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
  if (!instr->is_external()) {
    LOperand* obj = NULL;
    if (instr->representation().IsDouble()) {
      obj = UseTempRegister(instr->elements());
    } else {
      ASSERT(instr->representation().IsTagged());
      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))));
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
    // float->double conversion on non-VFP2 requires an extra scratch
    // register. For convenience, just mark the elements register as "UseTemp"
    // so that it can be used as a temp during the float->double conversion
    // after it's no longer needed after the float load.
    bool needs_temp =
        !CpuFeatures::IsSupported(FPU) &&
        (elements_kind == EXTERNAL_FLOAT_ELEMENTS);
    LOperand* external_pointer = needs_temp
        ? UseTempRegister(instr->elements())
        : UseRegister(instr->elements());
1969 1970
    result = new(zone()) LLoadKeyed(external_pointer, key);
  }
1971

1972
  DefineAsRegister(result);
1973 1974
  // An unsigned int array load might overflow and cause a deopt, make sure it
  // has an environment.
1975 1976 1977
  bool can_deoptimize = instr->RequiresHoleCheck() ||
      (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS);
  return can_deoptimize ? AssignEnvironment(result) : result;
1978 1979 1980 1981 1982 1983 1984 1985
}


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

  LInstruction* result =
1986
      DefineFixed(new(zone()) LLoadKeyedGeneric(object, key), v0);
1987 1988 1989 1990
  return MarkAsCall(result, instr);
}


1991 1992
LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
  ElementsKind elements_kind = instr->elements_kind();
1993

1994 1995
  if (!instr->is_external()) {
    ASSERT(instr->elements()->representation().IsTagged());
1996
    bool needs_write_barrier = instr->NeedsWriteBarrier();
1997
    LOperand* object = NULL;
1998 1999 2000
    LOperand* val = NULL;
    LOperand* key = NULL;

2001 2002
    if (instr->value()->representation().IsDouble()) {
      object = UseRegisterAtStart(instr->elements());
2003 2004
      key = UseRegisterOrConstantAtStart(instr->key());
      val = UseTempRegister(instr->value());
2005 2006 2007
    } else {
      ASSERT(instr->value()->representation().IsTagged());
      object = UseTempRegister(instr->elements());
2008 2009 2010 2011
      val = needs_write_barrier ? UseTempRegister(instr->value())
          : UseRegisterAtStart(instr->value());
      key = needs_write_barrier ? UseTempRegister(instr->key())
          : UseRegisterOrConstantAtStart(instr->key());
2012 2013
    }

2014
    return new(zone()) LStoreKeyed(object, key, val);
2015 2016
  }

2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
  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);
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
}


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());

2046
  return MarkAsCall(new(zone()) LStoreKeyedGeneric(obj, key, val), instr);
2047 2048 2049 2050 2051
}


LInstruction* LChunkBuilder::DoTransitionElementsKind(
    HTransitionElementsKind* instr) {
2052
  LOperand* object = UseRegister(instr->object());
2053
  if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2054 2055
    LOperand* new_map_reg = TempRegister();
    LTransitionElementsKind* result =
2056
        new(zone()) LTransitionElementsKind(object, new_map_reg, NULL);
2057
    return DefineSameAsFirst(result);
2058 2059 2060 2061
  } else if (FLAG_compiled_transitions) {
    LTransitionElementsKind* result =
        new(zone()) LTransitionElementsKind(object, NULL, NULL);
    return AssignPointerMap(result);
2062 2063 2064 2065 2066
  } else {
    LOperand* object = UseFixed(instr->object(), a0);
    LOperand* fixed_object_reg = FixedTemp(a2);
    LOperand* new_map_reg = FixedTemp(a3);
    LTransitionElementsKind* result =
2067 2068 2069
        new(zone()) LTransitionElementsKind(object,
                                            new_map_reg,
                                            fixed_object_reg);
2070
    return MarkAsCall(result, instr);
2071 2072 2073 2074
  }
}


2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
LInstruction* LChunkBuilder::DoTrapAllocationMemento(
    HTrapAllocationMemento* instr) {
  LOperand* object = UseRegister(instr->object());
  LOperand* temp = TempRegister();
  LTrapAllocationMemento* result =
      new(zone()) LTrapAllocationMemento(object, temp);
  return AssignEnvironment(result);
}


2085 2086
LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
  bool needs_write_barrier = instr->NeedsWriteBarrier();
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
  bool needs_write_barrier_for_map = !instr->transition().is_null() &&
      instr->NeedsWriteBarrierForMap();

  LOperand* obj;
  if (needs_write_barrier) {
    obj = instr->is_in_object()
        ? UseRegister(instr->object())
        : UseTempRegister(instr->object());
  } else {
    obj = needs_write_barrier_for_map
        ? UseRegister(instr->object())
        : UseRegisterAtStart(instr->object());
  }
2100 2101 2102 2103 2104

  LOperand* val = needs_write_barrier
      ? UseTempRegister(instr->value())
      : UseRegister(instr->value());

2105 2106 2107 2108
  // We need a temporary register for write barrier of the map field.
  LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;

  return new(zone()) LStoreNamedField(obj, val, temp);
2109 2110 2111 2112 2113 2114 2115
}


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

2116
  LInstruction* result = new(zone()) LStoreNamedGeneric(obj, val);
2117 2118 2119 2120 2121 2122 2123
  return MarkAsCall(result, instr);
}


LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
  LOperand* left = UseRegisterAtStart(instr->left());
  LOperand* right = UseRegisterAtStart(instr->right());
2124 2125
  return MarkAsCall(DefineFixed(new(zone()) LStringAdd(left, right), v0),
                    instr);
2126 2127 2128 2129 2130 2131
}


LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
  LOperand* string = UseTempRegister(instr->string());
  LOperand* index = UseTempRegister(instr->index());
2132
  LStringCharCodeAt* result = new(zone()) LStringCharCodeAt(string, index);
2133 2134 2135 2136 2137 2138
  return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
}


LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
  LOperand* char_code = UseRegister(instr->value());
2139
  LStringCharFromCode* result = new(zone()) LStringCharFromCode(char_code);
2140 2141 2142 2143 2144 2145
  return AssignPointerMap(DefineAsRegister(result));
}


LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
  LOperand* string = UseRegisterAtStart(instr->value());
2146
  return DefineAsRegister(new(zone()) LStringLength(string));
2147 2148 2149
}


2150
LInstruction* LChunkBuilder::DoAllocateObject(HAllocateObject* instr) {
2151
  info()->MarkAsDeferredCalling();
2152 2153
  LAllocateObject* result =
      new(zone()) LAllocateObject(TempRegister(), TempRegister());
2154 2155 2156 2157
  return AssignPointerMap(DefineAsRegister(result));
}


2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
  info()->MarkAsDeferredCalling();
  LOperand* size = UseTempRegister(instr->size());
  LOperand* temp1 = TempRegister();
  LOperand* temp2 = TempRegister();
  LAllocate* result = new(zone()) LAllocate(size, temp1, temp2);
  return AssignPointerMap(DefineAsRegister(result));
}


2168
LInstruction* LChunkBuilder::DoFastLiteral(HFastLiteral* instr) {
2169
  return MarkAsCall(DefineFixed(new(zone()) LFastLiteral, v0), instr);
2170 2171 2172
}


2173
LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2174
  return MarkAsCall(DefineFixed(new(zone()) LArrayLiteral, v0), instr);
2175 2176 2177
}


2178
LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2179
  return MarkAsCall(DefineFixed(new(zone()) LObjectLiteral, v0), instr);
2180 2181 2182 2183
}


LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2184
  return MarkAsCall(DefineFixed(new(zone()) LRegExpLiteral, v0), instr);
2185 2186 2187 2188
}


LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2189
  return MarkAsCall(DefineFixed(new(zone()) LFunctionLiteral, v0), instr);
2190 2191 2192 2193 2194 2195
}


LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
  LOperand* object = UseFixed(instr->object(), a0);
  LOperand* key = UseFixed(instr->key(), a1);
2196
  LDeleteProperty* result = new(zone()) LDeleteProperty(object, key);
2197 2198 2199 2200 2201
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2202
  ASSERT(argument_count_ == 0);
2203 2204
  allocator_->MarkAsOsrEntry();
  current_block_->last_environment()->set_ast_id(instr->ast_id());
2205
  return AssignEnvironment(new(zone()) LOsrEntry);
2206 2207 2208 2209
}


LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2210
  LParameter* result = new(zone()) LParameter;
2211
  if (instr->kind() == HParameter::STACK_PARAMETER) {
2212 2213 2214 2215 2216 2217 2218 2219 2220
    int spill_index = chunk()->GetParameterStackSlot(instr->index());
    return DefineAsSpilled(result, spill_index);
  } else {
    ASSERT(info()->IsStub());
    CodeStubInterfaceDescriptor* descriptor =
        info()->code_stub()->GetInterfaceDescriptor(info()->isolate());
    Register reg = descriptor->register_params_[instr->index()];
    return DefineFixed(result, reg);
  }
2221 2222 2223 2224 2225 2226 2227 2228 2229
}


LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
  int spill_index = chunk()->GetNextSpillIndex(false);  // Not double-width.
  if (spill_index > LUnallocated::kMaxFixedIndex) {
    Abort("Too many spill slots needed for OSR");
    spill_index = 0;
  }
2230
  return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2231 2232 2233 2234 2235
}


LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
  argument_count_ -= instr->argument_count();
2236
  return MarkAsCall(DefineFixed(new(zone()) LCallStub, v0), instr);
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
}


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) {
2250
  LOperand* args = UseRegister(instr->arguments());
2251 2252
  LOperand* length = UseTempRegister(instr->length());
  LOperand* index = UseRegister(instr->index());
2253
  return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2254 2255 2256 2257 2258
}


LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
  LOperand* object = UseFixed(instr->value(), a0);
2259
  LToFastProperties* result = new(zone()) LToFastProperties(object);
2260 2261 2262 2263 2264
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2265
  LTypeof* result = new(zone()) LTypeof(UseFixed(instr->value(), a0));
2266 2267 2268 2269 2270
  return MarkAsCall(DefineFixed(result, v0), instr);
}


LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2271
  return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2272 2273 2274 2275 2276
}


LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
    HIsConstructCallAndBranch* instr) {
2277
  return new(zone()) LIsConstructCallAndBranch(TempRegister());
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
}


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());
2288
  for (int i = instr->values()->length() - 1; i >= 0; --i) {
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
    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()) {
2300
    LInstruction* result = new(zone()) LLazyBailout;
2301
    result = AssignEnvironment(result);
2302 2303
    // Store the lazy deopt environment with the instruction if needed. Right
    // now it is only used for LInstanceOfKnownGlobal.
2304
    instruction_pending_deoptimization_environment_->
2305 2306
        SetDeferredLazyDeoptimizationEnvironment(result->environment());
    instruction_pending_deoptimization_environment_ = NULL;
2307
    pending_deoptimization_ast_id_ = BailoutId::None();
2308 2309 2310 2311 2312 2313 2314 2315 2316
    return result;
  }

  return NULL;
}


LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
  if (instr->is_function_entry()) {
2317
    return MarkAsCall(new(zone()) LStackCheck, instr);
2318 2319
  } else {
    ASSERT(instr->is_backwards_branch());
2320
    return AssignEnvironment(AssignPointerMap(new(zone()) LStackCheck));
2321 2322 2323 2324 2325 2326 2327 2328
  }
}


LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
  HEnvironment* outer = current_block_->last_environment();
  HConstant* undefined = graph()->GetConstantUndefined();
  HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2329
                                               instr->arguments_count(),
2330 2331
                                               instr->function(),
                                               undefined,
2332 2333
                                               instr->inlining_kind(),
                                               instr->undefined_receiver());
2334 2335
  if (instr->arguments_var() != NULL) {
    inner->Bind(instr->arguments_var(), graph()->GetArgumentsObject());
2336
  }
2337
  inner->set_entry(instr);
2338 2339 2340 2341 2342 2343 2344
  current_block_->UpdateEnvironment(inner);
  chunk_->AddInlinedClosure(instr->closure());
  return NULL;
}


LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2345 2346 2347 2348
  LInstruction* pop = NULL;

  HEnvironment* env = current_block_->last_environment();

2349
  if (env->entry()->arguments_pushed()) {
2350 2351 2352 2353 2354
    int argument_count = env->arguments_environment()->parameter_count();
    pop = new(zone()) LDrop(argument_count);
    argument_count_ -= argument_count;
  }

2355 2356
  HEnvironment* outer = current_block_->last_environment()->
      DiscardInlined(false);
2357
  current_block_->UpdateEnvironment(outer);
2358 2359

  return pop;
2360 2361 2362 2363 2364 2365
}


LInstruction* LChunkBuilder::DoIn(HIn* instr) {
  LOperand* key = UseRegisterAtStart(instr->key());
  LOperand* object = UseRegisterAtStart(instr->object());
2366
  LIn* result = new(zone()) LIn(key, object);
2367 2368 2369 2370
  return MarkAsCall(DefineFixed(result, v0), instr);
}


2371 2372
LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
  LOperand* object = UseFixed(instr->enumerable(), a0);
2373
  LForInPrepareMap* result = new(zone()) LForInPrepareMap(object);
2374 2375 2376 2377 2378 2379
  return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
}


LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
  LOperand* map = UseRegister(instr->map());
2380
  return AssignEnvironment(DefineAsRegister(new(zone()) LForInCacheArray(map)));
2381 2382 2383 2384 2385 2386
}


LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
  LOperand* value = UseRegisterAtStart(instr->value());
  LOperand* map = UseRegisterAtStart(instr->map());
2387
  return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2388 2389 2390 2391 2392 2393
}


LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
  LOperand* object = UseRegister(instr->object());
  LOperand* index = UseRegister(instr->index());
2394
  return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index));
2395 2396 2397
}


2398
} }  // namespace v8::internal