Home | History | Annotate | Download | only in mips
      1 // Copyright 2012 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "v8.h"
     29 
     30 #include "lithium-allocator-inl.h"
     31 #include "mips/lithium-mips.h"
     32 #include "mips/lithium-codegen-mips.h"
     33 
     34 namespace v8 {
     35 namespace internal {
     36 
     37 #define DEFINE_COMPILE(type)                            \
     38   void L##type::CompileToNative(LCodeGen* generator) {  \
     39     generator->Do##type(this);                          \
     40   }
     41 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
     42 #undef DEFINE_COMPILE
     43 
     44 #ifdef DEBUG
     45 void LInstruction::VerifyCall() {
     46   // Call instructions can use only fixed registers as temporaries and
     47   // outputs because all registers are blocked by the calling convention.
     48   // Inputs operands must use a fixed register or use-at-start policy or
     49   // a non-register policy.
     50   ASSERT(Output() == NULL ||
     51          LUnallocated::cast(Output())->HasFixedPolicy() ||
     52          !LUnallocated::cast(Output())->HasRegisterPolicy());
     53   for (UseIterator it(this); !it.Done(); it.Advance()) {
     54     LUnallocated* operand = LUnallocated::cast(it.Current());
     55     ASSERT(operand->HasFixedPolicy() ||
     56            operand->IsUsedAtStart());
     57   }
     58   for (TempIterator it(this); !it.Done(); it.Advance()) {
     59     LUnallocated* operand = LUnallocated::cast(it.Current());
     60     ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
     61   }
     62 }
     63 #endif
     64 
     65 
     66 void LInstruction::PrintTo(StringStream* stream) {
     67   stream->Add("%s ", this->Mnemonic());
     68 
     69   PrintOutputOperandTo(stream);
     70 
     71   PrintDataTo(stream);
     72 
     73   if (HasEnvironment()) {
     74     stream->Add(" ");
     75     environment()->PrintTo(stream);
     76   }
     77 
     78   if (HasPointerMap()) {
     79     stream->Add(" ");
     80     pointer_map()->PrintTo(stream);
     81   }
     82 }
     83 
     84 
     85 void LInstruction::PrintDataTo(StringStream* stream) {
     86   stream->Add("= ");
     87   for (int i = 0; i < InputCount(); i++) {
     88     if (i > 0) stream->Add(" ");
     89     if (InputAt(i) == NULL) {
     90       stream->Add("NULL");
     91     } else {
     92       InputAt(i)->PrintTo(stream);
     93     }
     94   }
     95 }
     96 
     97 
     98 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
     99   if (HasResult()) result()->PrintTo(stream);
    100 }
    101 
    102 
    103 void LLabel::PrintDataTo(StringStream* stream) {
    104   LGap::PrintDataTo(stream);
    105   LLabel* rep = replacement();
    106   if (rep != NULL) {
    107     stream->Add(" Dead block replaced with B%d", rep->block_id());
    108   }
    109 }
    110 
    111 
    112 bool LGap::IsRedundant() const {
    113   for (int i = 0; i < 4; i++) {
    114     if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
    115       return false;
    116     }
    117   }
    118 
    119   return true;
    120 }
    121 
    122 
    123 void LGap::PrintDataTo(StringStream* stream) {
    124   for (int i = 0; i < 4; i++) {
    125     stream->Add("(");
    126     if (parallel_moves_[i] != NULL) {
    127       parallel_moves_[i]->PrintDataTo(stream);
    128     }
    129     stream->Add(") ");
    130   }
    131 }
    132 
    133 
    134 const char* LArithmeticD::Mnemonic() const {
    135   switch (op()) {
    136     case Token::ADD: return "add-d";
    137     case Token::SUB: return "sub-d";
    138     case Token::MUL: return "mul-d";
    139     case Token::DIV: return "div-d";
    140     case Token::MOD: return "mod-d";
    141     default:
    142       UNREACHABLE();
    143       return NULL;
    144   }
    145 }
    146 
    147 
    148 const char* LArithmeticT::Mnemonic() const {
    149   switch (op()) {
    150     case Token::ADD: return "add-t";
    151     case Token::SUB: return "sub-t";
    152     case Token::MUL: return "mul-t";
    153     case Token::MOD: return "mod-t";
    154     case Token::DIV: return "div-t";
    155     case Token::BIT_AND: return "bit-and-t";
    156     case Token::BIT_OR: return "bit-or-t";
    157     case Token::BIT_XOR: return "bit-xor-t";
    158     case Token::ROR: return "ror-t";
    159     case Token::SHL: return "sll-t";
    160     case Token::SAR: return "sra-t";
    161     case Token::SHR: return "srl-t";
    162     default:
    163       UNREACHABLE();
    164       return NULL;
    165   }
    166 }
    167 
    168 
    169 bool LGoto::HasInterestingComment(LCodeGen* gen) const {
    170   return !gen->IsNextEmittedBlock(block_id());
    171 }
    172 
    173 
    174 void LGoto::PrintDataTo(StringStream* stream) {
    175   stream->Add("B%d", block_id());
    176 }
    177 
    178 
    179 void LBranch::PrintDataTo(StringStream* stream) {
    180   stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
    181   value()->PrintTo(stream);
    182 }
    183 
    184 
    185 LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
    186   return new(zone()) LDebugBreak();
    187 }
    188 
    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   stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
    196 }
    197 
    198 
    199 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
    200   stream->Add("if is_object(");
    201   value()->PrintTo(stream);
    202   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    203 }
    204 
    205 
    206 void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
    207   stream->Add("if is_string(");
    208   value()->PrintTo(stream);
    209   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    210 }
    211 
    212 
    213 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
    214   stream->Add("if is_smi(");
    215   value()->PrintTo(stream);
    216   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    217 }
    218 
    219 
    220 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
    221   stream->Add("if is_undetectable(");
    222   value()->PrintTo(stream);
    223   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    224 }
    225 
    226 
    227 void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
    228   stream->Add("if string_compare(");
    229   left()->PrintTo(stream);
    230   right()->PrintTo(stream);
    231   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    232 }
    233 
    234 
    235 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
    236   stream->Add("if has_instance_type(");
    237   value()->PrintTo(stream);
    238   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    239 }
    240 
    241 
    242 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
    243   stream->Add("if has_cached_array_index(");
    244   value()->PrintTo(stream);
    245   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
    246 }
    247 
    248 
    249 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
    250   stream->Add("if class_of_test(");
    251   value()->PrintTo(stream);
    252   stream->Add(", \"%o\") then B%d else B%d",
    253               *hydrogen()->class_name(),
    254               true_block_id(),
    255               false_block_id());
    256 }
    257 
    258 
    259 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
    260   stream->Add("if typeof ");
    261   value()->PrintTo(stream);
    262   stream->Add(" == \"%s\" then B%d else B%d",
    263               *hydrogen()->type_literal()->ToCString(),
    264               true_block_id(), false_block_id());
    265 }
    266 
    267 
    268 void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
    269   stream->Add(" = ");
    270   base_object()->PrintTo(stream);
    271   stream->Add(" + %d", offset());
    272 }
    273 
    274 
    275 void LCallConstantFunction::PrintDataTo(StringStream* stream) {
    276   stream->Add("#%d / ", arity());
    277 }
    278 
    279 
    280 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
    281   context()->PrintTo(stream);
    282   stream->Add("[%d]", slot_index());
    283 }
    284 
    285 
    286 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
    287   context()->PrintTo(stream);
    288   stream->Add("[%d] <- ", slot_index());
    289   value()->PrintTo(stream);
    290 }
    291 
    292 
    293 void LInvokeFunction::PrintDataTo(StringStream* stream) {
    294   stream->Add("= ");
    295   function()->PrintTo(stream);
    296   stream->Add(" #%d / ", arity());
    297 }
    298 
    299 
    300 void LCallKeyed::PrintDataTo(StringStream* stream) {
    301   stream->Add("[a2] #%d / ", arity());
    302 }
    303 
    304 
    305 void LCallNamed::PrintDataTo(StringStream* stream) {
    306   SmartArrayPointer<char> name_string = name()->ToCString();
    307   stream->Add("%s #%d / ", *name_string, arity());
    308 }
    309 
    310 
    311 void LCallGlobal::PrintDataTo(StringStream* stream) {
    312   SmartArrayPointer<char> name_string = name()->ToCString();
    313   stream->Add("%s #%d / ", *name_string, arity());
    314 }
    315 
    316 
    317 void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
    318   stream->Add("#%d / ", arity());
    319 }
    320 
    321 
    322 void LCallNew::PrintDataTo(StringStream* stream) {
    323   stream->Add("= ");
    324   constructor()->PrintTo(stream);
    325   stream->Add(" #%d / ", arity());
    326 }
    327 
    328 
    329 void LCallNewArray::PrintDataTo(StringStream* stream) {
    330   stream->Add("= ");
    331   constructor()->PrintTo(stream);
    332   stream->Add(" #%d / ", arity());
    333   ElementsKind kind = hydrogen()->elements_kind();
    334   stream->Add(" (%s) ", ElementsKindToString(kind));
    335 }
    336 
    337 
    338 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
    339   arguments()->PrintTo(stream);
    340   stream->Add(" length ");
    341   length()->PrintTo(stream);
    342   stream->Add(" index ");
    343   index()->PrintTo(stream);
    344 }
    345 
    346 
    347 void LStoreNamedField::PrintDataTo(StringStream* stream) {
    348   object()->PrintTo(stream);
    349   hydrogen()->access().PrintTo(stream);
    350   stream->Add(" <- ");
    351   value()->PrintTo(stream);
    352 }
    353 
    354 
    355 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
    356   object()->PrintTo(stream);
    357   stream->Add(".");
    358   stream->Add(*String::cast(*name())->ToCString());
    359   stream->Add(" <- ");
    360   value()->PrintTo(stream);
    361 }
    362 
    363 
    364 void LLoadKeyed::PrintDataTo(StringStream* stream) {
    365   elements()->PrintTo(stream);
    366   stream->Add("[");
    367   key()->PrintTo(stream);
    368   if (hydrogen()->IsDehoisted()) {
    369     stream->Add(" + %d]", additional_index());
    370   } else {
    371     stream->Add("]");
    372   }
    373 }
    374 
    375 
    376 void LStoreKeyed::PrintDataTo(StringStream* stream) {
    377   elements()->PrintTo(stream);
    378   stream->Add("[");
    379   key()->PrintTo(stream);
    380   if (hydrogen()->IsDehoisted()) {
    381     stream->Add(" + %d] <-", additional_index());
    382   } else {
    383     stream->Add("] <- ");
    384   }
    385 
    386   if (value() == NULL) {
    387     ASSERT(hydrogen()->IsConstantHoleStore() &&
    388            hydrogen()->value()->representation().IsDouble());
    389     stream->Add("<the hole(nan)>");
    390   } else {
    391     value()->PrintTo(stream);
    392   }
    393 }
    394 
    395 
    396 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
    397   object()->PrintTo(stream);
    398   stream->Add("[");
    399   key()->PrintTo(stream);
    400   stream->Add("] <- ");
    401   value()->PrintTo(stream);
    402 }
    403 
    404 
    405 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
    406   object()->PrintTo(stream);
    407   stream->Add(" %p -> %p", *original_map(), *transitioned_map());
    408 }
    409 
    410 
    411 int LPlatformChunk::GetNextSpillIndex(bool is_double) {
    412   // Skip a slot if for a double-width slot.
    413   if (is_double) spill_slot_count_++;
    414   return spill_slot_count_++;
    415 }
    416 
    417 
    418 LOperand* LPlatformChunk::GetNextSpillSlot(bool is_double)  {
    419   int index = GetNextSpillIndex(is_double);
    420   if (is_double) {
    421     return LDoubleStackSlot::Create(index, zone());
    422   } else {
    423     return LStackSlot::Create(index, zone());
    424   }
    425 }
    426 
    427 
    428 LPlatformChunk* LChunkBuilder::Build() {
    429   ASSERT(is_unused());
    430   chunk_ = new(zone()) LPlatformChunk(info(), graph());
    431   LPhase phase("L_Building chunk", chunk_);
    432   status_ = BUILDING;
    433   const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
    434   for (int i = 0; i < blocks->length(); i++) {
    435     HBasicBlock* next = NULL;
    436     if (i < blocks->length() - 1) next = blocks->at(i + 1);
    437     DoBasicBlock(blocks->at(i), next);
    438     if (is_aborted()) return NULL;
    439   }
    440   status_ = DONE;
    441   return chunk_;
    442 }
    443 
    444 
    445 void LCodeGen::Abort(BailoutReason reason) {
    446   info()->set_bailout_reason(reason);
    447   status_ = ABORTED;
    448 }
    449 
    450 
    451 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
    452   return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
    453                                   Register::ToAllocationIndex(reg));
    454 }
    455 
    456 
    457 LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
    458   return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
    459                                   DoubleRegister::ToAllocationIndex(reg));
    460 }
    461 
    462 
    463 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
    464   return Use(value, ToUnallocated(fixed_register));
    465 }
    466 
    467 
    468 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
    469   return Use(value, ToUnallocated(reg));
    470 }
    471 
    472 
    473 LOperand* LChunkBuilder::UseRegister(HValue* value) {
    474   return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
    475 }
    476 
    477 
    478 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
    479   return Use(value,
    480              new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
    481                                       LUnallocated::USED_AT_START));
    482 }
    483 
    484 
    485 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
    486   return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
    487 }
    488 
    489 
    490 LOperand* LChunkBuilder::Use(HValue* value) {
    491   return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
    492 }
    493 
    494 
    495 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
    496   return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
    497                                      LUnallocated::USED_AT_START));
    498 }
    499 
    500 
    501 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
    502   return value->IsConstant()
    503       ? chunk_->DefineConstantOperand(HConstant::cast(value))
    504       : Use(value);
    505 }
    506 
    507 
    508 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
    509   return value->IsConstant()
    510       ? chunk_->DefineConstantOperand(HConstant::cast(value))
    511       : UseAtStart(value);
    512 }
    513 
    514 
    515 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
    516   return value->IsConstant()
    517       ? chunk_->DefineConstantOperand(HConstant::cast(value))
    518       : UseRegister(value);
    519 }
    520 
    521 
    522 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
    523   return value->IsConstant()
    524       ? chunk_->DefineConstantOperand(HConstant::cast(value))
    525       : UseRegisterAtStart(value);
    526 }
    527 
    528 
    529 LOperand* LChunkBuilder::UseConstant(HValue* value) {
    530   return chunk_->DefineConstantOperand(HConstant::cast(value));
    531 }
    532 
    533 
    534 LOperand* LChunkBuilder::UseAny(HValue* value) {
    535   return value->IsConstant()
    536       ? chunk_->DefineConstantOperand(HConstant::cast(value))
    537       :  Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
    538 }
    539 
    540 
    541 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
    542   if (value->EmitAtUses()) {
    543     HInstruction* instr = HInstruction::cast(value);
    544     VisitInstruction(instr);
    545   }
    546   operand->set_virtual_register(value->id());
    547   return operand;
    548 }
    549 
    550 
    551 template<int I, int T>
    552 LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
    553                                     LUnallocated* result) {
    554   result->set_virtual_register(current_instruction_->id());
    555   instr->set_result(result);
    556   return instr;
    557 }
    558 
    559 
    560 template<int I, int T>
    561 LInstruction* LChunkBuilder::DefineAsRegister(
    562     LTemplateInstruction<1, I, T>* instr) {
    563   return Define(instr,
    564                 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
    565 }
    566 
    567 
    568 template<int I, int T>
    569 LInstruction* LChunkBuilder::DefineAsSpilled(
    570     LTemplateInstruction<1, I, T>* instr, int index) {
    571   return Define(instr,
    572                 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
    573 }
    574 
    575 
    576 template<int I, int T>
    577 LInstruction* LChunkBuilder::DefineSameAsFirst(
    578     LTemplateInstruction<1, I, T>* instr) {
    579   return Define(instr,
    580                 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
    581 }
    582 
    583 
    584 template<int I, int T>
    585 LInstruction* LChunkBuilder::DefineFixed(
    586     LTemplateInstruction<1, I, T>* instr, Register reg) {
    587   return Define(instr, ToUnallocated(reg));
    588 }
    589 
    590 
    591 template<int I, int T>
    592 LInstruction* LChunkBuilder::DefineFixedDouble(
    593     LTemplateInstruction<1, I, T>* instr, DoubleRegister reg) {
    594   return Define(instr, ToUnallocated(reg));
    595 }
    596 
    597 
    598 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
    599   HEnvironment* hydrogen_env = current_block_->last_environment();
    600   int argument_index_accumulator = 0;
    601   ZoneList<HValue*> objects_to_materialize(0, zone());
    602   instr->set_environment(CreateEnvironment(hydrogen_env,
    603                                            &argument_index_accumulator,
    604                                            &objects_to_materialize));
    605   return instr;
    606 }
    607 
    608 
    609 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
    610                                         HInstruction* hinstr,
    611                                         CanDeoptimize can_deoptimize) {
    612   info()->MarkAsNonDeferredCalling();
    613 #ifdef DEBUG
    614   instr->VerifyCall();
    615 #endif
    616   instr->MarkAsCall();
    617   instr = AssignPointerMap(instr);
    618 
    619   if (hinstr->HasObservableSideEffects()) {
    620     ASSERT(hinstr->next()->IsSimulate());
    621     HSimulate* sim = HSimulate::cast(hinstr->next());
    622     ASSERT(instruction_pending_deoptimization_environment_ == NULL);
    623     ASSERT(pending_deoptimization_ast_id_.IsNone());
    624     instruction_pending_deoptimization_environment_ = instr;
    625     pending_deoptimization_ast_id_ = sim->ast_id();
    626   }
    627 
    628   // If instruction does not have side-effects lazy deoptimization
    629   // after the call will try to deoptimize to the point before the call.
    630   // Thus we still need to attach environment to this call even if
    631   // call sequence can not deoptimize eagerly.
    632   bool needs_environment =
    633       (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
    634       !hinstr->HasObservableSideEffects();
    635   if (needs_environment && !instr->HasEnvironment()) {
    636     instr = AssignEnvironment(instr);
    637   }
    638 
    639   return instr;
    640 }
    641 
    642 
    643 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
    644   ASSERT(!instr->HasPointerMap());
    645   instr->set_pointer_map(new(zone()) LPointerMap(position_, zone()));
    646   return instr;
    647 }
    648 
    649 
    650 LUnallocated* LChunkBuilder::TempRegister() {
    651   LUnallocated* operand =
    652       new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
    653   int vreg = allocator_->GetVirtualRegister();
    654   if (!allocator_->AllocationOk()) {
    655     Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
    656     vreg = 0;
    657   }
    658   operand->set_virtual_register(vreg);
    659   return operand;
    660 }
    661 
    662 
    663 LOperand* LChunkBuilder::FixedTemp(Register reg) {
    664   LUnallocated* operand = ToUnallocated(reg);
    665   ASSERT(operand->HasFixedPolicy());
    666   return operand;
    667 }
    668 
    669 
    670 LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
    671   LUnallocated* operand = ToUnallocated(reg);
    672   ASSERT(operand->HasFixedPolicy());
    673   return operand;
    674 }
    675 
    676 
    677 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
    678   return new(zone()) LLabel(instr->block());
    679 }
    680 
    681 
    682 LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
    683   return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
    684 }
    685 
    686 
    687 LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
    688   UNREACHABLE();
    689   return NULL;
    690 }
    691 
    692 
    693 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
    694   return AssignEnvironment(new(zone()) LDeoptimize);
    695 }
    696 
    697 
    698 LInstruction* LChunkBuilder::DoShift(Token::Value op,
    699                                      HBitwiseBinaryOperation* instr) {
    700   if (instr->representation().IsTagged()) {
    701     ASSERT(instr->left()->representation().IsTagged());
    702     ASSERT(instr->right()->representation().IsTagged());
    703 
    704     LOperand* left = UseFixed(instr->left(), a1);
    705     LOperand* right = UseFixed(instr->right(), a0);
    706     LArithmeticT* result = new(zone()) LArithmeticT(op, left, right);
    707     return MarkAsCall(DefineFixed(result, v0), instr);
    708   }
    709 
    710   ASSERT(instr->representation().IsSmiOrInteger32());
    711   ASSERT(instr->left()->representation().Equals(instr->representation()));
    712   ASSERT(instr->right()->representation().Equals(instr->representation()));
    713   LOperand* left = UseRegisterAtStart(instr->left());
    714 
    715   HValue* right_value = instr->right();
    716   LOperand* right = NULL;
    717   int constant_value = 0;
    718   bool does_deopt = false;
    719   if (right_value->IsConstant()) {
    720     HConstant* constant = HConstant::cast(right_value);
    721     right = chunk_->DefineConstantOperand(constant);
    722     constant_value = constant->Integer32Value() & 0x1f;
    723     // Left shifts can deoptimize if we shift by > 0 and the result cannot be
    724     // truncated to smi.
    725     if (instr->representation().IsSmi() && constant_value > 0) {
    726       for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
    727         if (!it.value()->CheckFlag(HValue::kTruncatingToSmi)) {
    728           does_deopt = true;
    729           break;
    730         }
    731       }
    732     }
    733   } else {
    734     right = UseRegisterAtStart(right_value);
    735   }
    736 
    737   // Shift operations can deoptimize if we do a logical shift
    738   // by 0 and the result cannot be truncated to int32.
    739   if (op == Token::SHR && constant_value == 0) {
    740     if (FLAG_opt_safe_uint32_operations) {
    741       does_deopt = !instr->CheckFlag(HInstruction::kUint32);
    742     } else {
    743       for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
    744         if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
    745           does_deopt = true;
    746           break;
    747         }
    748       }
    749     }
    750   }
    751 
    752   LInstruction* result =
    753       DefineAsRegister(new(zone()) LShiftI(op, left, right, does_deopt));
    754   return does_deopt ? AssignEnvironment(result) : result;
    755 }
    756 
    757 
    758 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
    759                                            HArithmeticBinaryOperation* instr) {
    760   ASSERT(instr->representation().IsDouble());
    761   ASSERT(instr->left()->representation().IsDouble());
    762   ASSERT(instr->right()->representation().IsDouble());
    763   ASSERT(op != Token::MOD);
    764   LOperand* left = UseRegisterAtStart(instr->left());
    765   LOperand* right = UseRegisterAtStart(instr->right());
    766   LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
    767   return DefineAsRegister(result);
    768 }
    769 
    770 
    771 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
    772                                            HArithmeticBinaryOperation* instr) {
    773   ASSERT(op == Token::ADD ||
    774          op == Token::DIV ||
    775          op == Token::MOD ||
    776          op == Token::MUL ||
    777          op == Token::SUB);
    778   HValue* left = instr->left();
    779   HValue* right = instr->right();
    780   ASSERT(left->representation().IsTagged());
    781   ASSERT(right->representation().IsTagged());
    782   LOperand* left_operand = UseFixed(left, a1);
    783   LOperand* right_operand = UseFixed(right, a0);
    784   LArithmeticT* result =
    785       new(zone()) LArithmeticT(op, left_operand, right_operand);
    786   return MarkAsCall(DefineFixed(result, v0), instr);
    787 }
    788 
    789 
    790 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
    791   ASSERT(is_building());
    792   current_block_ = block;
    793   next_block_ = next_block;
    794   if (block->IsStartBlock()) {
    795     block->UpdateEnvironment(graph_->start_environment());
    796     argument_count_ = 0;
    797   } else if (block->predecessors()->length() == 1) {
    798     // We have a single predecessor => copy environment and outgoing
    799     // argument count from the predecessor.
    800     ASSERT(block->phis()->length() == 0);
    801     HBasicBlock* pred = block->predecessors()->at(0);
    802     HEnvironment* last_environment = pred->last_environment();
    803     ASSERT(last_environment != NULL);
    804     // Only copy the environment, if it is later used again.
    805     if (pred->end()->SecondSuccessor() == NULL) {
    806       ASSERT(pred->end()->FirstSuccessor() == block);
    807     } else {
    808       if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
    809           pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
    810         last_environment = last_environment->Copy();
    811       }
    812     }
    813     block->UpdateEnvironment(last_environment);
    814     ASSERT(pred->argument_count() >= 0);
    815     argument_count_ = pred->argument_count();
    816   } else {
    817     // We are at a state join => process phis.
    818     HBasicBlock* pred = block->predecessors()->at(0);
    819     // No need to copy the environment, it cannot be used later.
    820     HEnvironment* last_environment = pred->last_environment();
    821     for (int i = 0; i < block->phis()->length(); ++i) {
    822       HPhi* phi = block->phis()->at(i);
    823       if (phi->HasMergedIndex()) {
    824         last_environment->SetValueAt(phi->merged_index(), phi);
    825       }
    826     }
    827     for (int i = 0; i < block->deleted_phis()->length(); ++i) {
    828       if (block->deleted_phis()->at(i) < last_environment->length()) {
    829         last_environment->SetValueAt(block->deleted_phis()->at(i),
    830                                      graph_->GetConstantUndefined());
    831       }
    832     }
    833     block->UpdateEnvironment(last_environment);
    834     // Pick up the outgoing argument count of one of the predecessors.
    835     argument_count_ = pred->argument_count();
    836   }
    837   HInstruction* current = block->first();
    838   int start = chunk_->instructions()->length();
    839   while (current != NULL && !is_aborted()) {
    840     // Code for constants in registers is generated lazily.
    841     if (!current->EmitAtUses()) {
    842       VisitInstruction(current);
    843     }
    844     current = current->next();
    845   }
    846   int end = chunk_->instructions()->length() - 1;
    847   if (end >= start) {
    848     block->set_first_instruction_index(start);
    849     block->set_last_instruction_index(end);
    850   }
    851   block->set_argument_count(argument_count_);
    852   next_block_ = NULL;
    853   current_block_ = NULL;
    854 }
    855 
    856 
    857 void LChunkBuilder::VisitInstruction(HInstruction* current) {
    858   HInstruction* old_current = current_instruction_;
    859   current_instruction_ = current;
    860   if (current->has_position()) position_ = current->position();
    861   LInstruction* instr = current->CompileToLithium(this);
    862 
    863   if (instr != NULL) {
    864 #if DEBUG
    865     // Make sure that the lithium instruction has either no fixed register
    866     // constraints in temps or the result OR no uses that are only used at
    867     // start. If this invariant doesn't hold, the register allocator can decide
    868     // to insert a split of a range immediately before the instruction due to an
    869     // already allocated register needing to be used for the instruction's fixed
    870     // register constraint. In this case, The register allocator won't see an
    871     // interference between the split child and the use-at-start (it would if
    872     // the it was just a plain use), so it is free to move the split child into
    873     // the same register that is used for the use-at-start.
    874     // See https://code.google.com/p/chromium/issues/detail?id=201590
    875     if (!(instr->ClobbersRegisters() && instr->ClobbersDoubleRegisters())) {
    876       int fixed = 0;
    877       int used_at_start = 0;
    878       for (UseIterator it(instr); !it.Done(); it.Advance()) {
    879         LUnallocated* operand = LUnallocated::cast(it.Current());
    880         if (operand->IsUsedAtStart()) ++used_at_start;
    881       }
    882       if (instr->Output() != NULL) {
    883         if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
    884       }
    885       for (TempIterator it(instr); !it.Done(); it.Advance()) {
    886         LUnallocated* operand = LUnallocated::cast(it.Current());
    887         if (operand->HasFixedPolicy()) ++fixed;
    888       }
    889       ASSERT(fixed == 0 || used_at_start == 0);
    890     }
    891 #endif
    892 
    893     instr->set_position(position_);
    894     if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
    895       instr = AssignPointerMap(instr);
    896     }
    897     if (FLAG_stress_environments && !instr->HasEnvironment()) {
    898       instr = AssignEnvironment(instr);
    899     }
    900     instr->set_hydrogen_value(current);
    901     chunk_->AddInstruction(instr, current_block_);
    902   }
    903   current_instruction_ = old_current;
    904 }
    905 
    906 
    907 LEnvironment* LChunkBuilder::CreateEnvironment(
    908     HEnvironment* hydrogen_env,
    909     int* argument_index_accumulator,
    910     ZoneList<HValue*>* objects_to_materialize) {
    911   if (hydrogen_env == NULL) return NULL;
    912 
    913   LEnvironment* outer = CreateEnvironment(hydrogen_env->outer(),
    914                                           argument_index_accumulator,
    915                                           objects_to_materialize);
    916   BailoutId ast_id = hydrogen_env->ast_id();
    917   ASSERT(!ast_id.IsNone() ||
    918          hydrogen_env->frame_type() != JS_FUNCTION);
    919   int value_count = hydrogen_env->length() - hydrogen_env->specials_count();
    920   LEnvironment* result = new(zone()) LEnvironment(
    921       hydrogen_env->closure(),
    922       hydrogen_env->frame_type(),
    923       ast_id,
    924       hydrogen_env->parameter_count(),
    925       argument_count_,
    926       value_count,
    927       outer,
    928       hydrogen_env->entry(),
    929       zone());
    930   int argument_index = *argument_index_accumulator;
    931   int object_index = objects_to_materialize->length();
    932   for (int i = 0; i < hydrogen_env->length(); ++i) {
    933     if (hydrogen_env->is_special_index(i)) continue;
    934 
    935     LOperand* op;
    936     HValue* value = hydrogen_env->values()->at(i);
    937     if (value->IsArgumentsObject() || value->IsCapturedObject()) {
    938       objects_to_materialize->Add(value, zone());
    939       op = LEnvironment::materialization_marker();
    940     } else if (value->IsPushArgument()) {
    941       op = new(zone()) LArgument(argument_index++);
    942     } else {
    943       op = UseAny(value);
    944     }
    945     result->AddValue(op,
    946                      value->representation(),
    947                      value->CheckFlag(HInstruction::kUint32));
    948   }
    949 
    950   for (int i = object_index; i < objects_to_materialize->length(); ++i) {
    951     HValue* object_to_materialize = objects_to_materialize->at(i);
    952     int previously_materialized_object = -1;
    953     for (int prev = 0; prev < i; ++prev) {
    954       if (objects_to_materialize->at(prev) == objects_to_materialize->at(i)) {
    955         previously_materialized_object = prev;
    956         break;
    957       }
    958     }
    959     int length = object_to_materialize->OperandCount();
    960     bool is_arguments = object_to_materialize->IsArgumentsObject();
    961     if (previously_materialized_object >= 0) {
    962       result->AddDuplicateObject(previously_materialized_object);
    963       continue;
    964     } else {
    965       result->AddNewObject(is_arguments ? length - 1 : length, is_arguments);
    966     }
    967     for (int i = is_arguments ? 1 : 0; i < length; ++i) {
    968       LOperand* op;
    969       HValue* value = object_to_materialize->OperandAt(i);
    970       if (value->IsArgumentsObject() || value->IsCapturedObject()) {
    971         objects_to_materialize->Add(value, zone());
    972         op = LEnvironment::materialization_marker();
    973       } else {
    974         ASSERT(!value->IsPushArgument());
    975         op = UseAny(value);
    976       }
    977       result->AddValue(op,
    978                        value->representation(),
    979                        value->CheckFlag(HInstruction::kUint32));
    980     }
    981   }
    982 
    983   if (hydrogen_env->frame_type() == JS_FUNCTION) {
    984     *argument_index_accumulator = argument_index;
    985   }
    986 
    987   return result;
    988 }
    989 
    990 
    991 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
    992   return new(zone()) LGoto(instr->FirstSuccessor()->block_id());
    993 }
    994 
    995 
    996 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
    997   HValue* value = instr->value();
    998   if (value->EmitAtUses()) {
    999     HBasicBlock* successor = HConstant::cast(value)->BooleanValue()
   1000         ? instr->FirstSuccessor()
   1001         : instr->SecondSuccessor();
   1002     return new(zone()) LGoto(successor->block_id());
   1003   }
   1004 
   1005   LBranch* result = new(zone()) LBranch(UseRegister(value));
   1006   // Tagged values that are not known smis or booleans require a
   1007   // deoptimization environment. If the instruction is generic no
   1008   // environment is needed since all cases are handled.
   1009   Representation rep = value->representation();
   1010   HType type = value->type();
   1011   ToBooleanStub::Types expected = instr->expected_input_types();
   1012   if (rep.IsTagged() && !type.IsSmi() && !type.IsBoolean() &&
   1013       !expected.IsGeneric()) {
   1014     return AssignEnvironment(result);
   1015   }
   1016   return result;
   1017 }
   1018 
   1019 
   1020 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
   1021   ASSERT(instr->value()->representation().IsTagged());
   1022   LOperand* value = UseRegisterAtStart(instr->value());
   1023   LOperand* temp = TempRegister();
   1024   return new(zone()) LCmpMapAndBranch(value, temp);
   1025 }
   1026 
   1027 
   1028 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
   1029   info()->MarkAsRequiresFrame();
   1030   return DefineAsRegister(
   1031       new(zone()) LArgumentsLength(UseRegister(length->value())));
   1032 }
   1033 
   1034 
   1035 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
   1036   info()->MarkAsRequiresFrame();
   1037   return DefineAsRegister(new(zone()) LArgumentsElements);
   1038 }
   1039 
   1040 
   1041 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
   1042   LInstanceOf* result =
   1043       new(zone()) LInstanceOf(UseFixed(instr->left(), a0),
   1044                               UseFixed(instr->right(), a1));
   1045   return MarkAsCall(DefineFixed(result, v0), instr);
   1046 }
   1047 
   1048 
   1049 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
   1050     HInstanceOfKnownGlobal* instr) {
   1051   LInstanceOfKnownGlobal* result =
   1052       new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->left(), a0),
   1053                                          FixedTemp(t0));
   1054   return MarkAsCall(DefineFixed(result, v0), instr);
   1055 }
   1056 
   1057 
   1058 LInstruction* LChunkBuilder::DoInstanceSize(HInstanceSize* instr) {
   1059   LOperand* object = UseRegisterAtStart(instr->object());
   1060   return DefineAsRegister(new(zone()) LInstanceSize(object));
   1061 }
   1062 
   1063 
   1064 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
   1065   LOperand* receiver = UseRegisterAtStart(instr->receiver());
   1066   LOperand* function = UseRegisterAtStart(instr->function());
   1067   LWrapReceiver* result = new(zone()) LWrapReceiver(receiver, function);
   1068   return AssignEnvironment(DefineSameAsFirst(result));
   1069 }
   1070 
   1071 
   1072 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
   1073   LOperand* function = UseFixed(instr->function(), a1);
   1074   LOperand* receiver = UseFixed(instr->receiver(), a0);
   1075   LOperand* length = UseFixed(instr->length(), a2);
   1076   LOperand* elements = UseFixed(instr->elements(), a3);
   1077   LApplyArguments* result = new(zone()) LApplyArguments(function,
   1078                                                         receiver,
   1079                                                         length,
   1080                                                         elements);
   1081   return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
   1082 }
   1083 
   1084 
   1085 LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
   1086   ++argument_count_;
   1087   LOperand* argument = Use(instr->argument());
   1088   return new(zone()) LPushArgument(argument);
   1089 }
   1090 
   1091 
   1092 LInstruction* LChunkBuilder::DoInnerAllocatedObject(
   1093     HInnerAllocatedObject* inner_object) {
   1094   LOperand* base_object = UseRegisterAtStart(inner_object->base_object());
   1095   LInnerAllocatedObject* result =
   1096     new(zone()) LInnerAllocatedObject(base_object);
   1097   return DefineAsRegister(result);
   1098 }
   1099 
   1100 
   1101 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
   1102   return instr->HasNoUses()
   1103       ? NULL
   1104       : DefineAsRegister(new(zone()) LThisFunction);
   1105 }
   1106 
   1107 
   1108 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
   1109   // If there is a non-return use, the context must be allocated in a register.
   1110   for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
   1111     if (!it.value()->IsReturn()) {
   1112       return DefineAsRegister(new(zone()) LContext);
   1113     }
   1114   }
   1115 
   1116   return NULL;
   1117 }
   1118 
   1119 
   1120 LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
   1121   LOperand* context = UseRegisterAtStart(instr->value());
   1122   return DefineAsRegister(new(zone()) LOuterContext(context));
   1123 }
   1124 
   1125 
   1126 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
   1127   return MarkAsCall(new(zone()) LDeclareGlobals, instr);
   1128 }
   1129 
   1130 
   1131 LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
   1132   LOperand* context = UseRegisterAtStart(instr->value());
   1133   return DefineAsRegister(new(zone()) LGlobalObject(context));
   1134 }
   1135 
   1136 
   1137 LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
   1138   LOperand* global_object = UseRegisterAtStart(instr->value());
   1139   return DefineAsRegister(new(zone()) LGlobalReceiver(global_object));
   1140 }
   1141 
   1142 
   1143 LInstruction* LChunkBuilder::DoCallConstantFunction(
   1144     HCallConstantFunction* instr) {
   1145   argument_count_ -= instr->argument_count();
   1146   return MarkAsCall(DefineFixed(new(zone()) LCallConstantFunction, v0), instr);
   1147 }
   1148 
   1149 
   1150 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
   1151   LOperand* function = UseFixed(instr->function(), a1);
   1152   argument_count_ -= instr->argument_count();
   1153   LInvokeFunction* result = new(zone()) LInvokeFunction(function);
   1154   return MarkAsCall(DefineFixed(result, v0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
   1155 }
   1156 
   1157 
   1158 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
   1159   switch (instr->op()) {
   1160     case kMathFloor: return DoMathFloor(instr);
   1161     case kMathRound: return DoMathRound(instr);
   1162     case kMathAbs: return DoMathAbs(instr);
   1163     case kMathLog: return DoMathLog(instr);
   1164     case kMathSin: return DoMathSin(instr);
   1165     case kMathCos: return DoMathCos(instr);
   1166     case kMathTan: return DoMathTan(instr);
   1167     case kMathExp: return DoMathExp(instr);
   1168     case kMathSqrt: return DoMathSqrt(instr);
   1169     case kMathPowHalf: return DoMathPowHalf(instr);
   1170     default:
   1171       UNREACHABLE();
   1172       return NULL;
   1173   }
   1174 }
   1175 
   1176 
   1177 LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
   1178   LOperand* input = UseFixedDouble(instr->value(), f4);
   1179   LMathLog* result = new(zone()) LMathLog(input);
   1180   return MarkAsCall(DefineFixedDouble(result, f4), instr);
   1181 }
   1182 
   1183 
   1184 LInstruction* LChunkBuilder::DoMathSin(HUnaryMathOperation* instr) {
   1185   LOperand* input = UseFixedDouble(instr->value(), f4);
   1186   LMathSin* result = new(zone()) LMathSin(input);
   1187   return MarkAsCall(DefineFixedDouble(result, f4), instr);
   1188 }
   1189 
   1190 
   1191 LInstruction* LChunkBuilder::DoMathCos(HUnaryMathOperation* instr) {
   1192   LOperand* input = UseFixedDouble(instr->value(), f4);
   1193   LMathCos* result = new(zone()) LMathCos(input);
   1194   return MarkAsCall(DefineFixedDouble(result, f4), instr);
   1195 }
   1196 
   1197 
   1198 LInstruction* LChunkBuilder::DoMathTan(HUnaryMathOperation* instr) {
   1199   LOperand* input = UseFixedDouble(instr->value(), f4);
   1200   LMathTan* result = new(zone()) LMathTan(input);
   1201   return MarkAsCall(DefineFixedDouble(result, f4), instr);
   1202 }
   1203 
   1204 
   1205 LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
   1206   ASSERT(instr->representation().IsDouble());
   1207   ASSERT(instr->value()->representation().IsDouble());
   1208   LOperand* input = UseTempRegister(instr->value());
   1209   LOperand* temp1 = TempRegister();
   1210   LOperand* temp2 = TempRegister();
   1211   LOperand* double_temp = FixedTemp(f6);  // Chosen by fair dice roll.
   1212   LMathExp* result = new(zone()) LMathExp(input, double_temp, temp1, temp2);
   1213   return DefineAsRegister(result);
   1214 }
   1215 
   1216 
   1217 LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
   1218   // Input cannot be the same as the result, see LCodeGen::DoMathPowHalf.
   1219   LOperand* input = UseFixedDouble(instr->value(), f8);
   1220   LOperand* temp = FixedTemp(f6);
   1221   LMathPowHalf* result = new(zone()) LMathPowHalf(input, temp);
   1222   return DefineFixedDouble(result, f4);
   1223 }
   1224 
   1225 
   1226 LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
   1227   LOperand* input = UseRegister(instr->value());
   1228   LMathAbs* result = new(zone()) LMathAbs(input);
   1229   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
   1230 }
   1231 
   1232 
   1233 LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
   1234   LOperand* input = UseRegister(instr->value());
   1235   LOperand* temp = TempRegister();
   1236   LMathFloor* result = new(zone()) LMathFloor(input, temp);
   1237   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
   1238 }
   1239 
   1240 
   1241 LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
   1242   LOperand* input = UseRegister(instr->value());
   1243   LMathSqrt* result = new(zone()) LMathSqrt(input);
   1244   return DefineAsRegister(result);
   1245 }
   1246 
   1247 
   1248 LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
   1249   LOperand* input = UseRegister(instr->value());
   1250   LOperand* temp = FixedTemp(f6);
   1251   LMathRound* result = new(zone()) LMathRound(input, temp);
   1252   return AssignEnvironment(DefineAsRegister(result));
   1253 }
   1254 
   1255 
   1256 LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
   1257   ASSERT(instr->key()->representation().IsTagged());
   1258   argument_count_ -= instr->argument_count();
   1259   LOperand* key = UseFixed(instr->key(), a2);
   1260   return MarkAsCall(DefineFixed(new(zone()) LCallKeyed(key), v0), instr);
   1261 }
   1262 
   1263 
   1264 LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
   1265   argument_count_ -= instr->argument_count();
   1266   return MarkAsCall(DefineFixed(new(zone()) LCallNamed, v0), instr);
   1267 }
   1268 
   1269 
   1270 LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
   1271   argument_count_ -= instr->argument_count();
   1272   return MarkAsCall(DefineFixed(new(zone()) LCallGlobal, v0), instr);
   1273 }
   1274 
   1275 
   1276 LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
   1277   argument_count_ -= instr->argument_count();
   1278   return MarkAsCall(DefineFixed(new(zone()) LCallKnownGlobal, v0), instr);
   1279 }
   1280 
   1281 
   1282 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
   1283   LOperand* constructor = UseFixed(instr->constructor(), a1);
   1284   argument_count_ -= instr->argument_count();
   1285   LCallNew* result = new(zone()) LCallNew(constructor);
   1286   return MarkAsCall(DefineFixed(result, v0), instr);
   1287 }
   1288 
   1289 
   1290 LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
   1291   LOperand* constructor = UseFixed(instr->constructor(), a1);
   1292   argument_count_ -= instr->argument_count();
   1293   LCallNewArray* result = new(zone()) LCallNewArray(constructor);
   1294   return MarkAsCall(DefineFixed(result, v0), instr);
   1295 }
   1296 
   1297 
   1298 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
   1299   LOperand* function = UseFixed(instr->function(), a1);
   1300   argument_count_ -= instr->argument_count();
   1301   return MarkAsCall(DefineFixed(new(zone()) LCallFunction(function), v0),
   1302                     instr);
   1303 }
   1304 
   1305 
   1306 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
   1307   argument_count_ -= instr->argument_count();
   1308   return MarkAsCall(DefineFixed(new(zone()) LCallRuntime, v0), instr);
   1309 }
   1310 
   1311 
   1312 LInstruction* LChunkBuilder::DoRor(HRor* instr) {
   1313   return DoShift(Token::ROR, instr);
   1314 }
   1315 
   1316 
   1317 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
   1318   return DoShift(Token::SHR, instr);
   1319 }
   1320 
   1321 
   1322 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
   1323   return DoShift(Token::SAR, instr);
   1324 }
   1325 
   1326 
   1327 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
   1328   return DoShift(Token::SHL, instr);
   1329 }
   1330 
   1331 
   1332 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
   1333   if (instr->representation().IsSmiOrInteger32()) {
   1334     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1335     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1336 
   1337     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
   1338     LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
   1339     return DefineAsRegister(new(zone()) LBitI(left, right));
   1340   } else {
   1341     ASSERT(instr->representation().IsTagged());
   1342     ASSERT(instr->left()->representation().IsTagged());
   1343     ASSERT(instr->right()->representation().IsTagged());
   1344 
   1345     LOperand* left = UseFixed(instr->left(), a1);
   1346     LOperand* right = UseFixed(instr->right(), a0);
   1347     LArithmeticT* result = new(zone()) LArithmeticT(instr->op(), left, right);
   1348     return MarkAsCall(DefineFixed(result, v0), instr);
   1349   }
   1350 }
   1351 
   1352 
   1353 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
   1354   if (instr->representation().IsDouble()) {
   1355     return DoArithmeticD(Token::DIV, instr);
   1356   } else if (instr->representation().IsSmiOrInteger32()) {
   1357     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1358     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1359     LOperand* dividend = UseRegister(instr->left());
   1360     LOperand* divisor = UseRegister(instr->right());
   1361     LDivI* div = new(zone()) LDivI(dividend, divisor);
   1362     return AssignEnvironment(DefineAsRegister(div));
   1363   } else {
   1364     return DoArithmeticT(Token::DIV, instr);
   1365   }
   1366 }
   1367 
   1368 
   1369 bool LChunkBuilder::HasMagicNumberForDivisor(int32_t divisor) {
   1370   uint32_t divisor_abs = abs(divisor);
   1371   // Dividing by 0, 1, and powers of 2 is easy.
   1372   // Note that IsPowerOf2(0) returns true;
   1373   ASSERT(IsPowerOf2(0) == true);
   1374   if (IsPowerOf2(divisor_abs)) return true;
   1375 
   1376   // We have magic numbers for a few specific divisors.
   1377   // Details and proofs can be found in:
   1378   // - Hacker's Delight, Henry S. Warren, Jr.
   1379   // - The PowerPC Compiler Writer's Guide
   1380   // and probably many others.
   1381   //
   1382   // We handle
   1383   //   <divisor with magic numbers> * <power of 2>
   1384   // but not
   1385   //   <divisor with magic numbers> * <other divisor with magic numbers>
   1386   int32_t power_of_2_factor =
   1387     CompilerIntrinsics::CountTrailingZeros(divisor_abs);
   1388   DivMagicNumbers magic_numbers =
   1389     DivMagicNumberFor(divisor_abs >> power_of_2_factor);
   1390   if (magic_numbers.M != InvalidDivMagicNumber.M) return true;
   1391 
   1392   return false;
   1393 }
   1394 
   1395 
   1396 HValue* LChunkBuilder::SimplifiedDivisorForMathFloorOfDiv(HValue* divisor) {
   1397   // Only optimize when we have magic numbers for the divisor.
   1398   // The standard integer division routine is usually slower than transitionning
   1399   // to FPU.
   1400   if (divisor->IsConstant() &&
   1401       HConstant::cast(divisor)->HasInteger32Value()) {
   1402     HConstant* constant_val = HConstant::cast(divisor);
   1403     return constant_val->CopyToRepresentation(Representation::Integer32(),
   1404                                                 divisor->block()->zone());
   1405   }
   1406   return NULL;
   1407 }
   1408 
   1409 
   1410 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
   1411     HValue* right = instr->right();
   1412     LOperand* dividend = UseRegister(instr->left());
   1413     LOperand* divisor = UseRegisterOrConstant(right);
   1414     LOperand* remainder = TempRegister();
   1415     return AssignEnvironment(DefineAsRegister(
   1416           new(zone()) LMathFloorOfDiv(dividend, divisor, remainder)));
   1417 }
   1418 
   1419 
   1420 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
   1421   HValue* left = instr->left();
   1422   HValue* right = instr->right();
   1423   if (instr->representation().IsSmiOrInteger32()) {
   1424     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1425     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1426     if (instr->HasPowerOf2Divisor()) {
   1427       ASSERT(!right->CanBeZero());
   1428       LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
   1429                                      UseOrConstant(right));
   1430       LInstruction* result = DefineAsRegister(mod);
   1431       return (left->CanBeNegative() &&
   1432               instr->CheckFlag(HValue::kBailoutOnMinusZero))
   1433           ? AssignEnvironment(result)
   1434           : result;
   1435     } else if (instr->fixed_right_arg().has_value) {
   1436       LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
   1437                                      UseRegisterAtStart(right));
   1438       return AssignEnvironment(DefineAsRegister(mod));
   1439     } else {
   1440       LModI* mod = new(zone()) LModI(UseRegister(left),
   1441                                      UseRegister(right),
   1442                                      TempRegister(),
   1443                                      FixedTemp(f20),
   1444                                      FixedTemp(f22));
   1445       LInstruction* result = DefineAsRegister(mod);
   1446       return (right->CanBeZero() ||
   1447               (left->RangeCanInclude(kMinInt) &&
   1448                right->RangeCanInclude(-1)) ||
   1449               instr->CheckFlag(HValue::kBailoutOnMinusZero))
   1450           ? AssignEnvironment(result)
   1451           : result;
   1452     }
   1453   } else if (instr->representation().IsTagged()) {
   1454     return DoArithmeticT(Token::MOD, instr);
   1455   } else {
   1456     ASSERT(instr->representation().IsDouble());
   1457     // We call a C function for double modulo. It can't trigger a GC. We need
   1458     // to use fixed result register for the call.
   1459     // TODO(fschneider): Allow any register as input registers.
   1460     LArithmeticD* mod = new(zone()) LArithmeticD(Token::MOD,
   1461                                                  UseFixedDouble(left, f2),
   1462                                                  UseFixedDouble(right, f4));
   1463     return MarkAsCall(DefineFixedDouble(mod, f2), instr);
   1464   }
   1465 }
   1466 
   1467 
   1468 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
   1469   if (instr->representation().IsSmiOrInteger32()) {
   1470     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1471     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1472     LOperand* left;
   1473     LOperand* right = UseOrConstant(instr->BetterRightOperand());
   1474     LOperand* temp = NULL;
   1475     if (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
   1476         (instr->CheckFlag(HValue::kCanOverflow) ||
   1477         !right->IsConstantOperand())) {
   1478       left = UseRegister(instr->BetterLeftOperand());
   1479       temp = TempRegister();
   1480     } else {
   1481       left = UseRegisterAtStart(instr->BetterLeftOperand());
   1482     }
   1483     LMulI* mul = new(zone()) LMulI(left, right, temp);
   1484     if (instr->CheckFlag(HValue::kCanOverflow) ||
   1485         instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
   1486       AssignEnvironment(mul);
   1487     }
   1488     return DefineAsRegister(mul);
   1489 
   1490   } else if (instr->representation().IsDouble()) {
   1491     if (kArchVariant == kMips32r2) {
   1492       if (instr->UseCount() == 1 && instr->uses().value()->IsAdd()) {
   1493         HAdd* add = HAdd::cast(instr->uses().value());
   1494         if (instr == add->left()) {
   1495           // This mul is the lhs of an add. The add and mul will be folded
   1496           // into a multiply-add.
   1497           return NULL;
   1498         }
   1499         if (instr == add->right() && !add->left()->IsMul()) {
   1500           // This mul is the rhs of an add, where the lhs is not another mul.
   1501           // The add and mul will be folded into a multiply-add.
   1502           return NULL;
   1503         }
   1504       }
   1505     }
   1506     return DoArithmeticD(Token::MUL, instr);
   1507   } else {
   1508     return DoArithmeticT(Token::MUL, instr);
   1509   }
   1510 }
   1511 
   1512 
   1513 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
   1514   if (instr->representation().IsSmiOrInteger32()) {
   1515     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1516     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1517     LOperand* left = UseRegisterAtStart(instr->left());
   1518     LOperand* right = UseOrConstantAtStart(instr->right());
   1519     LSubI* sub = new(zone()) LSubI(left, right);
   1520     LInstruction* result = DefineAsRegister(sub);
   1521     if (instr->CheckFlag(HValue::kCanOverflow)) {
   1522       result = AssignEnvironment(result);
   1523     }
   1524     return result;
   1525   } else if (instr->representation().IsDouble()) {
   1526     return DoArithmeticD(Token::SUB, instr);
   1527   } else {
   1528     return DoArithmeticT(Token::SUB, instr);
   1529   }
   1530 }
   1531 
   1532 
   1533 LInstruction* LChunkBuilder::DoMultiplyAdd(HMul* mul, HValue* addend) {
   1534   LOperand* multiplier_op = UseRegisterAtStart(mul->left());
   1535   LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
   1536   LOperand* addend_op = UseRegisterAtStart(addend);
   1537   return DefineSameAsFirst(new(zone()) LMultiplyAddD(addend_op, multiplier_op,
   1538                                                      multiplicand_op));
   1539 }
   1540 
   1541 
   1542 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
   1543   if (instr->representation().IsSmiOrInteger32()) {
   1544     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1545     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1546     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
   1547     LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
   1548     LAddI* add = new(zone()) LAddI(left, right);
   1549     LInstruction* result = DefineAsRegister(add);
   1550     if (instr->CheckFlag(HValue::kCanOverflow)) {
   1551       result = AssignEnvironment(result);
   1552     }
   1553     return result;
   1554   } else if (instr->representation().IsDouble()) {
   1555     if (kArchVariant == kMips32r2) {
   1556       if (instr->left()->IsMul())
   1557         return DoMultiplyAdd(HMul::cast(instr->left()), instr->right());
   1558 
   1559       if (instr->right()->IsMul()) {
   1560         ASSERT(!instr->left()->IsMul());
   1561         return DoMultiplyAdd(HMul::cast(instr->right()), instr->left());
   1562       }
   1563     }
   1564     return DoArithmeticD(Token::ADD, instr);
   1565   } else {
   1566     ASSERT(instr->representation().IsTagged());
   1567     return DoArithmeticT(Token::ADD, instr);
   1568   }
   1569 }
   1570 
   1571 
   1572 LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
   1573   LOperand* left = NULL;
   1574   LOperand* right = NULL;
   1575   if (instr->representation().IsSmiOrInteger32()) {
   1576     ASSERT(instr->left()->representation().Equals(instr->representation()));
   1577     ASSERT(instr->right()->representation().Equals(instr->representation()));
   1578     left = UseRegisterAtStart(instr->BetterLeftOperand());
   1579     right = UseOrConstantAtStart(instr->BetterRightOperand());
   1580   } else {
   1581     ASSERT(instr->representation().IsDouble());
   1582     ASSERT(instr->left()->representation().IsDouble());
   1583     ASSERT(instr->right()->representation().IsDouble());
   1584     left = UseRegisterAtStart(instr->left());
   1585     right = UseRegisterAtStart(instr->right());
   1586   }
   1587   return DefineAsRegister(new(zone()) LMathMinMax(left, right));
   1588 }
   1589 
   1590 
   1591 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
   1592   ASSERT(instr->representation().IsDouble());
   1593   // We call a C function for double power. It can't trigger a GC.
   1594   // We need to use fixed result register for the call.
   1595   Representation exponent_type = instr->right()->representation();
   1596   ASSERT(instr->left()->representation().IsDouble());
   1597   LOperand* left = UseFixedDouble(instr->left(), f2);
   1598   LOperand* right = exponent_type.IsDouble() ?
   1599       UseFixedDouble(instr->right(), f4) :
   1600       UseFixed(instr->right(), a2);
   1601   LPower* result = new(zone()) LPower(left, right);
   1602   return MarkAsCall(DefineFixedDouble(result, f0),
   1603                     instr,
   1604                     CAN_DEOPTIMIZE_EAGERLY);
   1605 }
   1606 
   1607 
   1608 LInstruction* LChunkBuilder::DoRandom(HRandom* instr) {
   1609   ASSERT(instr->representation().IsDouble());
   1610   ASSERT(instr->global_object()->representation().IsTagged());
   1611   LOperand* global_object = UseFixed(instr->global_object(), a0);
   1612   LRandom* result = new(zone()) LRandom(global_object);
   1613   return MarkAsCall(DefineFixedDouble(result, f0), instr);
   1614 }
   1615 
   1616 
   1617 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
   1618   ASSERT(instr->left()->representation().IsTagged());
   1619   ASSERT(instr->right()->representation().IsTagged());
   1620   LOperand* left = UseFixed(instr->left(), a1);
   1621   LOperand* right = UseFixed(instr->right(), a0);
   1622   LCmpT* result = new(zone()) LCmpT(left, right);
   1623   return MarkAsCall(DefineFixed(result, v0), instr);
   1624 }
   1625 
   1626 
   1627 LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
   1628     HCompareNumericAndBranch* instr) {
   1629   Representation r = instr->representation();
   1630   if (r.IsSmiOrInteger32()) {
   1631     ASSERT(instr->left()->representation().Equals(r));
   1632     ASSERT(instr->right()->representation().Equals(r));
   1633     LOperand* left = UseRegisterOrConstantAtStart(instr->left());
   1634     LOperand* right = UseRegisterOrConstantAtStart(instr->right());
   1635     return new(zone()) LCompareNumericAndBranch(left, right);
   1636   } else {
   1637     ASSERT(r.IsDouble());
   1638     ASSERT(instr->left()->representation().IsDouble());
   1639     ASSERT(instr->right()->representation().IsDouble());
   1640     LOperand* left = UseRegisterAtStart(instr->left());
   1641     LOperand* right = UseRegisterAtStart(instr->right());
   1642     return new(zone()) LCompareNumericAndBranch(left, right);
   1643   }
   1644 }
   1645 
   1646 
   1647 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
   1648     HCompareObjectEqAndBranch* instr) {
   1649   LOperand* left = UseRegisterAtStart(instr->left());
   1650   LOperand* right = UseRegisterAtStart(instr->right());
   1651   return new(zone()) LCmpObjectEqAndBranch(left, right);
   1652 }
   1653 
   1654 
   1655 LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
   1656     HCompareHoleAndBranch* instr) {
   1657   LOperand* object = UseRegisterAtStart(instr->object());
   1658   return new(zone()) LCmpHoleAndBranch(object);
   1659 }
   1660 
   1661 
   1662 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
   1663   ASSERT(instr->value()->representation().IsTagged());
   1664   LOperand* temp = TempRegister();
   1665   return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()),
   1666                                         temp);
   1667 }
   1668 
   1669 
   1670 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
   1671   ASSERT(instr->value()->representation().IsTagged());
   1672   LOperand* temp = TempRegister();
   1673   return new(zone()) LIsStringAndBranch(UseRegisterAtStart(instr->value()),
   1674                                         temp);
   1675 }
   1676 
   1677 
   1678 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
   1679   ASSERT(instr->value()->representation().IsTagged());
   1680   return new(zone()) LIsSmiAndBranch(Use(instr->value()));
   1681 }
   1682 
   1683 
   1684 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
   1685     HIsUndetectableAndBranch* instr) {
   1686   ASSERT(instr->value()->representation().IsTagged());
   1687   return new(zone()) LIsUndetectableAndBranch(
   1688       UseRegisterAtStart(instr->value()), TempRegister());
   1689 }
   1690 
   1691 
   1692 LInstruction* LChunkBuilder::DoStringCompareAndBranch(
   1693     HStringCompareAndBranch* instr) {
   1694   ASSERT(instr->left()->representation().IsTagged());
   1695   ASSERT(instr->right()->representation().IsTagged());
   1696   LOperand* left = UseFixed(instr->left(), a1);
   1697   LOperand* right = UseFixed(instr->right(), a0);
   1698   LStringCompareAndBranch* result =
   1699       new(zone()) LStringCompareAndBranch(left, right);
   1700   return MarkAsCall(result, instr);
   1701 }
   1702 
   1703 
   1704 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
   1705     HHasInstanceTypeAndBranch* instr) {
   1706   ASSERT(instr->value()->representation().IsTagged());
   1707   LOperand* value = UseRegisterAtStart(instr->value());
   1708   return new(zone()) LHasInstanceTypeAndBranch(value);
   1709 }
   1710 
   1711 
   1712 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
   1713     HGetCachedArrayIndex* instr)  {
   1714   ASSERT(instr->value()->representation().IsTagged());
   1715   LOperand* value = UseRegisterAtStart(instr->value());
   1716 
   1717   return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
   1718 }
   1719 
   1720 
   1721 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
   1722     HHasCachedArrayIndexAndBranch* instr) {
   1723   ASSERT(instr->value()->representation().IsTagged());
   1724   return new(zone()) LHasCachedArrayIndexAndBranch(
   1725       UseRegisterAtStart(instr->value()));
   1726 }
   1727 
   1728 
   1729 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
   1730     HClassOfTestAndBranch* instr) {
   1731   ASSERT(instr->value()->representation().IsTagged());
   1732   return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
   1733                                            TempRegister());
   1734 }
   1735 
   1736 
   1737 LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
   1738   LOperand* map = UseRegisterAtStart(instr->value());
   1739   return DefineAsRegister(new(zone()) LMapEnumLength(map));
   1740 }
   1741 
   1742 
   1743 LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
   1744   LOperand* object = UseRegisterAtStart(instr->value());
   1745   return DefineAsRegister(new(zone()) LElementsKind(object));
   1746 }
   1747 
   1748 
   1749 LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
   1750   LOperand* object = UseRegister(instr->value());
   1751   LValueOf* result = new(zone()) LValueOf(object, TempRegister());
   1752   return DefineAsRegister(result);
   1753 }
   1754 
   1755 
   1756 LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
   1757   LOperand* object = UseFixed(instr->value(), a0);
   1758   LDateField* result =
   1759       new(zone()) LDateField(object, FixedTemp(a1), instr->index());
   1760   return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
   1761 }
   1762 
   1763 
   1764 LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
   1765   LOperand* string = UseRegister(instr->string());
   1766   LOperand* index = UseRegister(instr->index());
   1767   LOperand* value = UseTempRegister(instr->value());
   1768   LSeqStringSetChar* result =
   1769       new(zone()) LSeqStringSetChar(instr->encoding(), string, index, value);
   1770   return DefineAsRegister(result);
   1771 }
   1772 
   1773 
   1774 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
   1775   LOperand* value = UseRegisterOrConstantAtStart(instr->index());
   1776   LOperand* length = UseRegister(instr->length());
   1777   return AssignEnvironment(new(zone()) LBoundsCheck(value, length));
   1778 }
   1779 
   1780 
   1781 LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
   1782     HBoundsCheckBaseIndexInformation* instr) {
   1783   UNREACHABLE();
   1784   return NULL;
   1785 }
   1786 
   1787 
   1788 LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
   1789   LOperand* value = UseFixed(instr->value(), a0);
   1790   return MarkAsCall(new(zone()) LThrow(value), instr);
   1791 }
   1792 
   1793 
   1794 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
   1795   return NULL;
   1796 }
   1797 
   1798 
   1799 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
   1800   // All HForceRepresentation instructions should be eliminated in the
   1801   // representation change phase of Hydrogen.
   1802   UNREACHABLE();
   1803   return NULL;
   1804 }
   1805 
   1806 
   1807 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
   1808   Representation from = instr->from();
   1809   Representation to = instr->to();
   1810   if (from.IsSmi()) {
   1811     if (to.IsTagged()) {
   1812       LOperand* value = UseRegister(instr->value());
   1813       return DefineSameAsFirst(new(zone()) LDummyUse(value));
   1814     }
   1815     from = Representation::Tagged();
   1816   }
   1817   if (from.IsTagged()) {
   1818     if (to.IsDouble()) {
   1819       info()->MarkAsDeferredCalling();
   1820       LOperand* value = UseRegister(instr->value());
   1821       LNumberUntagD* res = new(zone()) LNumberUntagD(value);
   1822       return AssignEnvironment(DefineAsRegister(res));
   1823     } else if (to.IsSmi()) {
   1824       HValue* val = instr->value();
   1825       LOperand* value = UseRegister(val);
   1826       if (val->type().IsSmi()) {
   1827         return DefineSameAsFirst(new(zone()) LDummyUse(value));
   1828       }
   1829       return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
   1830     } else {
   1831       ASSERT(to.IsInteger32());
   1832       LOperand* value = NULL;
   1833       LInstruction* res = NULL;
   1834       if (instr->value()->type().IsSmi()) {
   1835         value = UseRegisterAtStart(instr->value());
   1836         res = DefineAsRegister(new(zone()) LSmiUntag(value, false));
   1837       } else {
   1838         value = UseRegister(instr->value());
   1839         LOperand* temp1 = TempRegister();
   1840         LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister()
   1841                                                       : NULL;
   1842         LOperand* temp3 = FixedTemp(f22);
   1843         res = DefineSameAsFirst(new(zone()) LTaggedToI(value,
   1844                                                        temp1,
   1845                                                        temp2,
   1846                                                        temp3));
   1847         res = AssignEnvironment(res);
   1848       }
   1849       return res;
   1850     }
   1851   } else if (from.IsDouble()) {
   1852     if (to.IsTagged()) {
   1853       info()->MarkAsDeferredCalling();
   1854       LOperand* value = UseRegister(instr->value());
   1855       LOperand* temp1 = TempRegister();
   1856       LOperand* temp2 = TempRegister();
   1857 
   1858       // Make sure that the temp and result_temp registers are
   1859       // different.
   1860       LUnallocated* result_temp = TempRegister();
   1861       LNumberTagD* result = new(zone()) LNumberTagD(value, temp1, temp2);
   1862       Define(result, result_temp);
   1863       return AssignPointerMap(result);
   1864     } else if (to.IsSmi()) {
   1865       LOperand* value = UseRegister(instr->value());
   1866       return AssignEnvironment(DefineAsRegister(new(zone()) LDoubleToSmi(value,
   1867           TempRegister(), TempRegister())));
   1868     } else {
   1869       ASSERT(to.IsInteger32());
   1870       LOperand* value = UseRegister(instr->value());
   1871       LOperand* temp1 = TempRegister();
   1872       LOperand* temp2 = instr->CanTruncateToInt32() ? TempRegister() : NULL;
   1873       LDoubleToI* res = new(zone()) LDoubleToI(value, temp1, temp2);
   1874       return AssignEnvironment(DefineAsRegister(res));
   1875     }
   1876   } else if (from.IsInteger32()) {
   1877     info()->MarkAsDeferredCalling();
   1878     if (to.IsTagged()) {
   1879       HValue* val = instr->value();
   1880       LOperand* value = UseRegisterAtStart(val);
   1881       if (val->CheckFlag(HInstruction::kUint32)) {
   1882         LNumberTagU* result = new(zone()) LNumberTagU(value);
   1883         return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
   1884       } else if (val->HasRange() && val->range()->IsInSmiRange()) {
   1885         return DefineAsRegister(new(zone()) LSmiTag(value));
   1886       } else {
   1887         LNumberTagI* result = new(zone()) LNumberTagI(value);
   1888         return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
   1889       }
   1890     } else if (to.IsSmi()) {
   1891       HValue* val = instr->value();
   1892       LOperand* value = UseRegister(val);
   1893       LInstruction* result =
   1894           DefineSameAsFirst(new(zone()) LInteger32ToSmi(value));
   1895       if (val->HasRange() && val->range()->IsInSmiRange()) {
   1896         return result;
   1897       }
   1898       return AssignEnvironment(result);
   1899     } else {
   1900       ASSERT(to.IsDouble());
   1901       if (instr->value()->CheckFlag(HInstruction::kUint32)) {
   1902         return DefineAsRegister(
   1903             new(zone()) LUint32ToDouble(UseRegister(instr->value())));
   1904       } else {
   1905         return DefineAsRegister(
   1906             new(zone()) LInteger32ToDouble(Use(instr->value())));
   1907       }
   1908     }
   1909   }
   1910   UNREACHABLE();
   1911   return NULL;
   1912 }
   1913 
   1914 
   1915 LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
   1916   LOperand* value = UseRegisterAtStart(instr->value());
   1917   return AssignEnvironment(new(zone()) LCheckNonSmi(value));
   1918 }
   1919 
   1920 
   1921 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
   1922   LOperand* value = UseRegisterAtStart(instr->value());
   1923   return AssignEnvironment(new(zone()) LCheckSmi(value));
   1924 }
   1925 
   1926 
   1927 LInstruction* LChunkBuilder::DoIsNumberAndBranch(HIsNumberAndBranch* instr) {
   1928   return new(zone())
   1929     LIsNumberAndBranch(UseRegisterOrConstantAtStart(instr->value()));
   1930 }
   1931 
   1932 
   1933 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
   1934   LOperand* value = UseRegisterAtStart(instr->value());
   1935   LInstruction* result = new(zone()) LCheckInstanceType(value);
   1936   return AssignEnvironment(result);
   1937 }
   1938 
   1939 
   1940 LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
   1941   LOperand* value = UseRegisterAtStart(instr->value());
   1942   return AssignEnvironment(new(zone()) LCheckFunction(value));
   1943 }
   1944 
   1945 
   1946 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
   1947   LOperand* value = NULL;
   1948   if (!instr->CanOmitMapChecks()) {
   1949     value = UseRegisterAtStart(instr->value());
   1950     if (instr->has_migration_target()) info()->MarkAsDeferredCalling();
   1951   }
   1952   LCheckMaps* result = new(zone()) LCheckMaps(value);
   1953   if (!instr->CanOmitMapChecks()) {
   1954     AssignEnvironment(result);
   1955     if (instr->has_migration_target()) return AssignPointerMap(result);
   1956   }
   1957   return result;
   1958 }
   1959 
   1960 
   1961 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
   1962   HValue* value = instr->value();
   1963   Representation input_rep = value->representation();
   1964   LOperand* reg = UseRegister(value);
   1965   if (input_rep.IsDouble()) {
   1966     // Revisit this decision, here and 8 lines below.
   1967     return DefineAsRegister(new(zone()) LClampDToUint8(reg, FixedTemp(f22)));
   1968   } else if (input_rep.IsInteger32()) {
   1969     return DefineAsRegister(new(zone()) LClampIToUint8(reg));
   1970   } else {
   1971     ASSERT(input_rep.IsSmiOrTagged());
   1972     // Register allocator doesn't (yet) support allocation of double
   1973     // temps. Reserve f22 explicitly.
   1974     LClampTToUint8* result = new(zone()) LClampTToUint8(reg, FixedTemp(f22));
   1975     return AssignEnvironment(DefineAsRegister(result));
   1976   }
   1977 }
   1978 
   1979 
   1980 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
   1981   LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
   1982   return new(zone()) LReturn(UseFixed(instr->value(), v0),
   1983                              parameter_count);
   1984 }
   1985 
   1986 
   1987 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
   1988   Representation r = instr->representation();
   1989   if (r.IsSmi()) {
   1990     return DefineAsRegister(new(zone()) LConstantS);
   1991   } else if (r.IsInteger32()) {
   1992     return DefineAsRegister(new(zone()) LConstantI);
   1993   } else if (r.IsDouble()) {
   1994     return DefineAsRegister(new(zone()) LConstantD);
   1995   } else if (r.IsExternal()) {
   1996     return DefineAsRegister(new(zone()) LConstantE);
   1997   } else if (r.IsTagged()) {
   1998     return DefineAsRegister(new(zone()) LConstantT);
   1999   } else {
   2000     UNREACHABLE();
   2001     return NULL;
   2002   }
   2003 }
   2004 
   2005 
   2006 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
   2007   LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
   2008   return instr->RequiresHoleCheck()
   2009       ? AssignEnvironment(DefineAsRegister(result))
   2010       : DefineAsRegister(result);
   2011 }
   2012 
   2013 
   2014 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
   2015   LOperand* global_object = UseFixed(instr->global_object(), a0);
   2016   LLoadGlobalGeneric* result = new(zone()) LLoadGlobalGeneric(global_object);
   2017   return MarkAsCall(DefineFixed(result, v0), instr);
   2018 }
   2019 
   2020 
   2021 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
   2022   LOperand* value = UseRegister(instr->value());
   2023   // Use a temp to check the value in the cell in the case where we perform
   2024   // a hole check.
   2025   return instr->RequiresHoleCheck()
   2026       ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
   2027       : new(zone()) LStoreGlobalCell(value, NULL);
   2028 }
   2029 
   2030 
   2031 LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
   2032   LOperand* global_object = UseFixed(instr->global_object(), a1);
   2033   LOperand* value = UseFixed(instr->value(), a0);
   2034   LStoreGlobalGeneric* result =
   2035       new(zone()) LStoreGlobalGeneric(global_object, value);
   2036   return MarkAsCall(result, instr);
   2037 }
   2038 
   2039 
   2040 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
   2041   LOperand* context = UseRegisterAtStart(instr->value());
   2042   LInstruction* result =
   2043       DefineAsRegister(new(zone()) LLoadContextSlot(context));
   2044   return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
   2045 }
   2046 
   2047 
   2048 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
   2049   LOperand* context;
   2050   LOperand* value;
   2051   if (instr->NeedsWriteBarrier()) {
   2052     context = UseTempRegister(instr->context());
   2053     value = UseTempRegister(instr->value());
   2054   } else {
   2055     context = UseRegister(instr->context());
   2056     value = UseRegister(instr->value());
   2057   }
   2058   LInstruction* result = new(zone()) LStoreContextSlot(context, value);
   2059   return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
   2060 }
   2061 
   2062 
   2063 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
   2064   LOperand* obj = UseRegisterAtStart(instr->object());
   2065   return DefineAsRegister(new(zone()) LLoadNamedField(obj));
   2066 }
   2067 
   2068 
   2069 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
   2070   LOperand* object = UseFixed(instr->object(), a0);
   2071   LInstruction* result = DefineFixed(new(zone()) LLoadNamedGeneric(object), v0);
   2072   return MarkAsCall(result, instr);
   2073 }
   2074 
   2075 
   2076 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
   2077     HLoadFunctionPrototype* instr) {
   2078   return AssignEnvironment(DefineAsRegister(
   2079       new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
   2080 }
   2081 
   2082 
   2083 LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
   2084     HLoadExternalArrayPointer* instr) {
   2085   LOperand* input = UseRegisterAtStart(instr->value());
   2086   return DefineAsRegister(new(zone()) LLoadExternalArrayPointer(input));
   2087 }
   2088 
   2089 
   2090 LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
   2091   ASSERT(instr->key()->representation().IsSmiOrInteger32());
   2092   ElementsKind elements_kind = instr->elements_kind();
   2093   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
   2094   LLoadKeyed* result = NULL;
   2095 
   2096   if (!instr->is_external()) {
   2097     LOperand* obj = NULL;
   2098     if (instr->representation().IsDouble()) {
   2099       obj = UseTempRegister(instr->elements());
   2100     } else {
   2101       ASSERT(instr->representation().IsSmiOrTagged());
   2102       obj = UseRegisterAtStart(instr->elements());
   2103     }
   2104     result = new(zone()) LLoadKeyed(obj, key);
   2105   } else {
   2106     ASSERT(
   2107         (instr->representation().IsInteger32() &&
   2108          (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
   2109          (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
   2110         (instr->representation().IsDouble() &&
   2111          ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
   2112           (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
   2113     LOperand* external_pointer = UseRegister(instr->elements());
   2114     result = new(zone()) LLoadKeyed(external_pointer, key);
   2115   }
   2116 
   2117   DefineAsRegister(result);
   2118   // An unsigned int array load might overflow and cause a deopt, make sure it
   2119   // has an environment.
   2120   bool can_deoptimize = instr->RequiresHoleCheck() ||
   2121       (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS);
   2122   return can_deoptimize ? AssignEnvironment(result) : result;
   2123 }
   2124 
   2125 
   2126 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
   2127   LOperand* object = UseFixed(instr->object(), a1);
   2128   LOperand* key = UseFixed(instr->key(), a0);
   2129 
   2130   LInstruction* result =
   2131       DefineFixed(new(zone()) LLoadKeyedGeneric(object, key), v0);
   2132   return MarkAsCall(result, instr);
   2133 }
   2134 
   2135 
   2136 LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
   2137   ElementsKind elements_kind = instr->elements_kind();
   2138 
   2139   if (!instr->is_external()) {
   2140     ASSERT(instr->elements()->representation().IsTagged());
   2141     bool needs_write_barrier = instr->NeedsWriteBarrier();
   2142     LOperand* object = NULL;
   2143     LOperand* val = NULL;
   2144     LOperand* key = NULL;
   2145 
   2146     if (instr->value()->representation().IsDouble()) {
   2147       object = UseRegisterAtStart(instr->elements());
   2148       key = UseRegisterOrConstantAtStart(instr->key());
   2149       val = UseTempRegister(instr->value());
   2150     } else {
   2151       ASSERT(instr->value()->representation().IsSmiOrTagged());
   2152       object = UseTempRegister(instr->elements());
   2153       val = needs_write_barrier ? UseTempRegister(instr->value())
   2154           : UseRegisterAtStart(instr->value());
   2155       key = needs_write_barrier ? UseTempRegister(instr->key())
   2156           : UseRegisterOrConstantAtStart(instr->key());
   2157     }
   2158 
   2159     return new(zone()) LStoreKeyed(object, key, val);
   2160   }
   2161 
   2162   ASSERT(
   2163       (instr->value()->representation().IsInteger32() &&
   2164        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
   2165        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
   2166       (instr->value()->representation().IsDouble() &&
   2167        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
   2168         (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
   2169   ASSERT(instr->elements()->representation().IsExternal());
   2170   bool val_is_temp_register =
   2171       elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
   2172       elements_kind == EXTERNAL_FLOAT_ELEMENTS;
   2173   LOperand* val = val_is_temp_register ? UseTempRegister(instr->value())
   2174       : UseRegister(instr->value());
   2175   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
   2176   LOperand* external_pointer = UseRegister(instr->elements());
   2177 
   2178   return new(zone()) LStoreKeyed(external_pointer, key, val);
   2179 }
   2180 
   2181 
   2182 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
   2183   LOperand* obj = UseFixed(instr->object(), a2);
   2184   LOperand* key = UseFixed(instr->key(), a1);
   2185   LOperand* val = UseFixed(instr->value(), a0);
   2186 
   2187   ASSERT(instr->object()->representation().IsTagged());
   2188   ASSERT(instr->key()->representation().IsTagged());
   2189   ASSERT(instr->value()->representation().IsTagged());
   2190 
   2191   return MarkAsCall(new(zone()) LStoreKeyedGeneric(obj, key, val), instr);
   2192 }
   2193 
   2194 
   2195 LInstruction* LChunkBuilder::DoTransitionElementsKind(
   2196     HTransitionElementsKind* instr) {
   2197   LOperand* object = UseRegister(instr->object());
   2198   if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
   2199     LOperand* new_map_reg = TempRegister();
   2200     LTransitionElementsKind* result =
   2201         new(zone()) LTransitionElementsKind(object, new_map_reg);
   2202     return result;
   2203   } else {
   2204     LTransitionElementsKind* result =
   2205         new(zone()) LTransitionElementsKind(object, NULL);
   2206     return AssignPointerMap(result);
   2207   }
   2208 }
   2209 
   2210 
   2211 LInstruction* LChunkBuilder::DoTrapAllocationMemento(
   2212     HTrapAllocationMemento* instr) {
   2213   LOperand* object = UseRegister(instr->object());
   2214   LOperand* temp = TempRegister();
   2215   LTrapAllocationMemento* result =
   2216       new(zone()) LTrapAllocationMemento(object, temp);
   2217   return AssignEnvironment(result);
   2218 }
   2219 
   2220 
   2221 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
   2222   bool is_in_object = instr->access().IsInobject();
   2223   bool needs_write_barrier = instr->NeedsWriteBarrier();
   2224   bool needs_write_barrier_for_map = instr->has_transition() &&
   2225       instr->NeedsWriteBarrierForMap();
   2226 
   2227   LOperand* obj;
   2228   if (needs_write_barrier) {
   2229     obj = is_in_object
   2230         ? UseRegister(instr->object())
   2231         : UseTempRegister(instr->object());
   2232   } else {
   2233     obj = needs_write_barrier_for_map
   2234         ? UseRegister(instr->object())
   2235         : UseRegisterAtStart(instr->object());
   2236   }
   2237 
   2238   LOperand* val;
   2239   if (needs_write_barrier ||
   2240       (FLAG_track_fields && instr->field_representation().IsSmi())) {
   2241     val = UseTempRegister(instr->value());
   2242   } else if (FLAG_track_double_fields &&
   2243              instr->field_representation().IsDouble()) {
   2244     val = UseRegisterAtStart(instr->value());
   2245   } else {
   2246     val = UseRegister(instr->value());
   2247   }
   2248 
   2249   // We need a temporary register for write barrier of the map field.
   2250   LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;
   2251 
   2252   LStoreNamedField* result = new(zone()) LStoreNamedField(obj, val, temp);
   2253   if (FLAG_track_heap_object_fields &&
   2254       instr->field_representation().IsHeapObject()) {
   2255     if (!instr->value()->type().IsHeapObject()) {
   2256       return AssignEnvironment(result);
   2257     }
   2258   }
   2259   return result;
   2260 }
   2261 
   2262 
   2263 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
   2264   LOperand* obj = UseFixed(instr->object(), a1);
   2265   LOperand* val = UseFixed(instr->value(), a0);
   2266 
   2267   LInstruction* result = new(zone()) LStoreNamedGeneric(obj, val);
   2268   return MarkAsCall(result, instr);
   2269 }
   2270 
   2271 
   2272 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
   2273   LOperand* left = UseRegisterAtStart(instr->left());
   2274   LOperand* right = UseRegisterAtStart(instr->right());
   2275   return MarkAsCall(DefineFixed(new(zone()) LStringAdd(left, right), v0),
   2276                     instr);
   2277 }
   2278 
   2279 
   2280 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
   2281   LOperand* string = UseTempRegister(instr->string());
   2282   LOperand* index = UseTempRegister(instr->index());
   2283   LStringCharCodeAt* result = new(zone()) LStringCharCodeAt(string, index);
   2284   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
   2285 }
   2286 
   2287 
   2288 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
   2289   LOperand* char_code = UseRegister(instr->value());
   2290   LStringCharFromCode* result = new(zone()) LStringCharFromCode(char_code);
   2291   return AssignPointerMap(DefineAsRegister(result));
   2292 }
   2293 
   2294 
   2295 LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
   2296   info()->MarkAsDeferredCalling();
   2297   LOperand* size = instr->size()->IsConstant()
   2298       ? UseConstant(instr->size())
   2299       : UseTempRegister(instr->size());
   2300   LOperand* temp1 = TempRegister();
   2301   LOperand* temp2 = TempRegister();
   2302   LAllocate* result = new(zone()) LAllocate(size, temp1, temp2);
   2303   return AssignPointerMap(DefineAsRegister(result));
   2304 }
   2305 
   2306 
   2307 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
   2308   return MarkAsCall(DefineFixed(new(zone()) LRegExpLiteral, v0), instr);
   2309 }
   2310 
   2311 
   2312 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
   2313   return MarkAsCall(DefineFixed(new(zone()) LFunctionLiteral, v0), instr);
   2314 }
   2315 
   2316 
   2317 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
   2318   ASSERT(argument_count_ == 0);
   2319   allocator_->MarkAsOsrEntry();
   2320   current_block_->last_environment()->set_ast_id(instr->ast_id());
   2321   return AssignEnvironment(new(zone()) LOsrEntry);
   2322 }
   2323 
   2324 
   2325 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
   2326   LParameter* result = new(zone()) LParameter;
   2327   if (instr->kind() == HParameter::STACK_PARAMETER) {
   2328     int spill_index = chunk()->GetParameterStackSlot(instr->index());
   2329     return DefineAsSpilled(result, spill_index);
   2330   } else {
   2331     ASSERT(info()->IsStub());
   2332     CodeStubInterfaceDescriptor* descriptor =
   2333         info()->code_stub()->GetInterfaceDescriptor(info()->isolate());
   2334     int index = static_cast<int>(instr->index());
   2335     Register reg = DESCRIPTOR_GET_PARAMETER_REGISTER(descriptor, index);
   2336     return DefineFixed(result, reg);
   2337   }
   2338 }
   2339 
   2340 
   2341 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
   2342   int spill_index = chunk()->GetNextSpillIndex(false);  // Not double-width.
   2343   if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
   2344     Abort(kTooManySpillSlotsNeededForOSR);
   2345     spill_index = 0;
   2346   }
   2347   return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
   2348 }
   2349 
   2350 
   2351 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
   2352   argument_count_ -= instr->argument_count();
   2353   return MarkAsCall(DefineFixed(new(zone()) LCallStub, v0), instr);
   2354 }
   2355 
   2356 
   2357 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
   2358   // There are no real uses of the arguments object.
   2359   // arguments.length and element access are supported directly on
   2360   // stack arguments, and any real arguments object use causes a bailout.
   2361   // So this value is never used.
   2362   return NULL;
   2363 }
   2364 
   2365 
   2366 LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
   2367   // There are no real uses of a captured object.
   2368   return NULL;
   2369 }
   2370 
   2371 
   2372 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
   2373   info()->MarkAsRequiresFrame();
   2374   LOperand* args = UseRegister(instr->arguments());
   2375   LOperand* length;
   2376   LOperand* index;
   2377   if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
   2378     length = UseRegisterOrConstant(instr->length());
   2379     index = UseOrConstant(instr->index());
   2380   } else {
   2381     length = UseTempRegister(instr->length());
   2382     index = UseRegisterAtStart(instr->index());
   2383   }
   2384   return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
   2385 }
   2386 
   2387 
   2388 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
   2389   LOperand* object = UseFixed(instr->value(), a0);
   2390   LToFastProperties* result = new(zone()) LToFastProperties(object);
   2391   return MarkAsCall(DefineFixed(result, v0), instr);
   2392 }
   2393 
   2394 
   2395 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
   2396   LTypeof* result = new(zone()) LTypeof(UseFixed(instr->value(), a0));
   2397   return MarkAsCall(DefineFixed(result, v0), instr);
   2398 }
   2399 
   2400 
   2401 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
   2402   return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
   2403 }
   2404 
   2405 
   2406 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
   2407     HIsConstructCallAndBranch* instr) {
   2408   return new(zone()) LIsConstructCallAndBranch(TempRegister());
   2409 }
   2410 
   2411 
   2412 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
   2413   HEnvironment* env = current_block_->last_environment();
   2414   ASSERT(env != NULL);
   2415 
   2416   env->set_ast_id(instr->ast_id());
   2417 
   2418   env->Drop(instr->pop_count());
   2419   for (int i = instr->values()->length() - 1; i >= 0; --i) {
   2420     HValue* value = instr->values()->at(i);
   2421     if (instr->HasAssignedIndexAt(i)) {
   2422       env->Bind(instr->GetAssignedIndexAt(i), value);
   2423     } else {
   2424       env->Push(value);
   2425     }
   2426   }
   2427 
   2428   // If there is an instruction pending deoptimization environment create a
   2429   // lazy bailout instruction to capture the environment.
   2430   if (pending_deoptimization_ast_id_ == instr->ast_id()) {
   2431     LInstruction* result = new(zone()) LLazyBailout;
   2432     result = AssignEnvironment(result);
   2433     // Store the lazy deopt environment with the instruction if needed. Right
   2434     // now it is only used for LInstanceOfKnownGlobal.
   2435     instruction_pending_deoptimization_environment_->
   2436         SetDeferredLazyDeoptimizationEnvironment(result->environment());
   2437     instruction_pending_deoptimization_environment_ = NULL;
   2438     pending_deoptimization_ast_id_ = BailoutId::None();
   2439     return result;
   2440   }
   2441 
   2442   return NULL;
   2443 }
   2444 
   2445 
   2446 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
   2447   if (instr->is_function_entry()) {
   2448     return MarkAsCall(new(zone()) LStackCheck, instr);
   2449   } else {
   2450     ASSERT(instr->is_backwards_branch());
   2451     return AssignEnvironment(AssignPointerMap(new(zone()) LStackCheck));
   2452   }
   2453 }
   2454 
   2455 
   2456 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
   2457   HEnvironment* outer = current_block_->last_environment();
   2458   HConstant* undefined = graph()->GetConstantUndefined();
   2459   HEnvironment* inner = outer->CopyForInlining(instr->closure(),
   2460                                                instr->arguments_count(),
   2461                                                instr->function(),
   2462                                                undefined,
   2463                                                instr->inlining_kind(),
   2464                                                instr->undefined_receiver());
   2465   // Only replay binding of arguments object if it wasn't removed from graph.
   2466   if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
   2467     inner->Bind(instr->arguments_var(), instr->arguments_object());
   2468   }
   2469   inner->set_entry(instr);
   2470   current_block_->UpdateEnvironment(inner);
   2471   chunk_->AddInlinedClosure(instr->closure());
   2472   return NULL;
   2473 }
   2474 
   2475 
   2476 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
   2477   LInstruction* pop = NULL;
   2478 
   2479   HEnvironment* env = current_block_->last_environment();
   2480 
   2481   if (env->entry()->arguments_pushed()) {
   2482     int argument_count = env->arguments_environment()->parameter_count();
   2483     pop = new(zone()) LDrop(argument_count);
   2484     argument_count_ -= argument_count;
   2485   }
   2486 
   2487   HEnvironment* outer = current_block_->last_environment()->
   2488       DiscardInlined(false);
   2489   current_block_->UpdateEnvironment(outer);
   2490 
   2491   return pop;
   2492 }
   2493 
   2494 
   2495 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
   2496   LOperand* object = UseFixed(instr->enumerable(), a0);
   2497   LForInPrepareMap* result = new(zone()) LForInPrepareMap(object);
   2498   return MarkAsCall(DefineFixed(result, v0), instr, CAN_DEOPTIMIZE_EAGERLY);
   2499 }
   2500 
   2501 
   2502 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
   2503   LOperand* map = UseRegister(instr->map());
   2504   return AssignEnvironment(DefineAsRegister(new(zone()) LForInCacheArray(map)));
   2505 }
   2506 
   2507 
   2508 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
   2509   LOperand* value = UseRegisterAtStart(instr->value());
   2510   LOperand* map = UseRegisterAtStart(instr->map());
   2511   return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
   2512 }
   2513 
   2514 
   2515 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
   2516   LOperand* object = UseRegister(instr->object());
   2517   LOperand* index = UseRegister(instr->index());
   2518   return DefineAsRegister(new(zone()) LLoadFieldByIndex(object, index));
   2519 }
   2520 
   2521 
   2522 } }  // namespace v8::internal
   2523