1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "src/lithium-allocator-inl.h"
8 #include "src/arm/lithium-arm.h"
9 #include "src/arm/lithium-codegen-arm.h"
10 #include "src/hydrogen-osr.h"
15 #define DEFINE_COMPILE(type) \
16 void L##type::CompileToNative(LCodeGen* generator) { \
17 generator->Do##type(this); \
19 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
23 void LInstruction::VerifyCall() {
24 // Call instructions can use only fixed registers as temporaries and
25 // outputs because all registers are blocked by the calling convention.
26 // Inputs operands must use a fixed register or use-at-start policy or
27 // a non-register policy.
28 ASSERT(Output() == NULL ||
29 LUnallocated::cast(Output())->HasFixedPolicy() ||
30 !LUnallocated::cast(Output())->HasRegisterPolicy());
31 for (UseIterator it(this); !it.Done(); it.Advance()) {
32 LUnallocated* operand = LUnallocated::cast(it.Current());
33 ASSERT(operand->HasFixedPolicy() ||
34 operand->IsUsedAtStart());
36 for (TempIterator it(this); !it.Done(); it.Advance()) {
37 LUnallocated* operand = LUnallocated::cast(it.Current());
38 ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
44 void LInstruction::PrintTo(StringStream* stream) {
45 stream->Add("%s ", this->Mnemonic());
47 PrintOutputOperandTo(stream);
51 if (HasEnvironment()) {
53 environment()->PrintTo(stream);
56 if (HasPointerMap()) {
58 pointer_map()->PrintTo(stream);
63 void LInstruction::PrintDataTo(StringStream* stream) {
65 for (int i = 0; i < InputCount(); i++) {
66 if (i > 0) stream->Add(" ");
67 if (InputAt(i) == NULL) {
70 InputAt(i)->PrintTo(stream);
76 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
77 if (HasResult()) result()->PrintTo(stream);
81 void LLabel::PrintDataTo(StringStream* stream) {
82 LGap::PrintDataTo(stream);
83 LLabel* rep = replacement();
85 stream->Add(" Dead block replaced with B%d", rep->block_id());
90 bool LGap::IsRedundant() const {
91 for (int i = 0; i < 4; i++) {
92 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
101 void LGap::PrintDataTo(StringStream* stream) {
102 for (int i = 0; i < 4; i++) {
104 if (parallel_moves_[i] != NULL) {
105 parallel_moves_[i]->PrintDataTo(stream);
112 const char* LArithmeticD::Mnemonic() const {
114 case Token::ADD: return "add-d";
115 case Token::SUB: return "sub-d";
116 case Token::MUL: return "mul-d";
117 case Token::DIV: return "div-d";
118 case Token::MOD: return "mod-d";
126 const char* LArithmeticT::Mnemonic() const {
128 case Token::ADD: return "add-t";
129 case Token::SUB: return "sub-t";
130 case Token::MUL: return "mul-t";
131 case Token::MOD: return "mod-t";
132 case Token::DIV: return "div-t";
133 case Token::BIT_AND: return "bit-and-t";
134 case Token::BIT_OR: return "bit-or-t";
135 case Token::BIT_XOR: return "bit-xor-t";
136 case Token::ROR: return "ror-t";
137 case Token::SHL: return "shl-t";
138 case Token::SAR: return "sar-t";
139 case Token::SHR: return "shr-t";
147 bool LGoto::HasInterestingComment(LCodeGen* gen) const {
148 return !gen->IsNextEmittedBlock(block_id());
152 void LGoto::PrintDataTo(StringStream* stream) {
153 stream->Add("B%d", block_id());
157 void LBranch::PrintDataTo(StringStream* stream) {
158 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
159 value()->PrintTo(stream);
163 void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
165 left()->PrintTo(stream);
166 stream->Add(" %s ", Token::String(op()));
167 right()->PrintTo(stream);
168 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
172 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
173 stream->Add("if is_object(");
174 value()->PrintTo(stream);
175 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
179 void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
180 stream->Add("if is_string(");
181 value()->PrintTo(stream);
182 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
186 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
187 stream->Add("if is_smi(");
188 value()->PrintTo(stream);
189 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
193 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
194 stream->Add("if is_undetectable(");
195 value()->PrintTo(stream);
196 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
200 void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
201 stream->Add("if string_compare(");
202 left()->PrintTo(stream);
203 right()->PrintTo(stream);
204 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
208 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
209 stream->Add("if has_instance_type(");
210 value()->PrintTo(stream);
211 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
215 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
216 stream->Add("if has_cached_array_index(");
217 value()->PrintTo(stream);
218 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
222 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
223 stream->Add("if class_of_test(");
224 value()->PrintTo(stream);
225 stream->Add(", \"%o\") then B%d else B%d",
226 *hydrogen()->class_name(),
232 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
233 stream->Add("if typeof ");
234 value()->PrintTo(stream);
235 stream->Add(" == \"%s\" then B%d else B%d",
236 hydrogen()->type_literal()->ToCString().get(),
237 true_block_id(), false_block_id());
241 void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
243 function()->PrintTo(stream);
244 stream->Add(".code_entry = ");
245 code_object()->PrintTo(stream);
249 void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
251 base_object()->PrintTo(stream);
253 offset()->PrintTo(stream);
257 void LCallJSFunction::PrintDataTo(StringStream* stream) {
259 function()->PrintTo(stream);
260 stream->Add("#%d / ", arity());
264 void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
265 for (int i = 0; i < InputCount(); i++) {
266 InputAt(i)->PrintTo(stream);
269 stream->Add("#%d / ", arity());
273 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
274 context()->PrintTo(stream);
275 stream->Add("[%d]", slot_index());
279 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
280 context()->PrintTo(stream);
281 stream->Add("[%d] <- ", slot_index());
282 value()->PrintTo(stream);
286 void LInvokeFunction::PrintDataTo(StringStream* stream) {
288 function()->PrintTo(stream);
289 stream->Add(" #%d / ", arity());
293 void LCallNew::PrintDataTo(StringStream* stream) {
295 constructor()->PrintTo(stream);
296 stream->Add(" #%d / ", arity());
300 void LCallNewArray::PrintDataTo(StringStream* stream) {
302 constructor()->PrintTo(stream);
303 stream->Add(" #%d / ", arity());
304 ElementsKind kind = hydrogen()->elements_kind();
305 stream->Add(" (%s) ", ElementsKindToString(kind));
309 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
310 arguments()->PrintTo(stream);
311 stream->Add(" length ");
312 length()->PrintTo(stream);
313 stream->Add(" index ");
314 index()->PrintTo(stream);
318 void LStoreNamedField::PrintDataTo(StringStream* stream) {
319 object()->PrintTo(stream);
320 hydrogen()->access().PrintTo(stream);
322 value()->PrintTo(stream);
326 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
327 object()->PrintTo(stream);
329 stream->Add(String::cast(*name())->ToCString().get());
331 value()->PrintTo(stream);
335 void LLoadKeyed::PrintDataTo(StringStream* stream) {
336 elements()->PrintTo(stream);
338 key()->PrintTo(stream);
339 if (hydrogen()->IsDehoisted()) {
340 stream->Add(" + %d]", base_offset());
347 void LStoreKeyed::PrintDataTo(StringStream* stream) {
348 elements()->PrintTo(stream);
350 key()->PrintTo(stream);
351 if (hydrogen()->IsDehoisted()) {
352 stream->Add(" + %d] <-", base_offset());
354 stream->Add("] <- ");
357 if (value() == NULL) {
358 ASSERT(hydrogen()->IsConstantHoleStore() &&
359 hydrogen()->value()->representation().IsDouble());
360 stream->Add("<the hole(nan)>");
362 value()->PrintTo(stream);
367 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
368 object()->PrintTo(stream);
370 key()->PrintTo(stream);
371 stream->Add("] <- ");
372 value()->PrintTo(stream);
376 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
377 object()->PrintTo(stream);
378 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
382 int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
383 // Skip a slot if for a double-width slot.
384 if (kind == DOUBLE_REGISTERS) spill_slot_count_++;
385 return spill_slot_count_++;
389 LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
390 int index = GetNextSpillIndex(kind);
391 if (kind == DOUBLE_REGISTERS) {
392 return LDoubleStackSlot::Create(index, zone());
394 ASSERT(kind == GENERAL_REGISTERS);
395 return LStackSlot::Create(index, zone());
400 LPlatformChunk* LChunkBuilder::Build() {
402 chunk_ = new(zone()) LPlatformChunk(info(), graph());
403 LPhase phase("L_Building chunk", chunk_);
406 // If compiling for OSR, reserve space for the unoptimized frame,
407 // which will be subsumed into this frame.
408 if (graph()->has_osr()) {
409 for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
410 chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
414 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
415 for (int i = 0; i < blocks->length(); i++) {
416 HBasicBlock* next = NULL;
417 if (i < blocks->length() - 1) next = blocks->at(i + 1);
418 DoBasicBlock(blocks->at(i), next);
419 if (is_aborted()) return NULL;
426 void LChunkBuilder::Abort(BailoutReason reason) {
427 info()->set_bailout_reason(reason);
432 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
433 return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
434 Register::ToAllocationIndex(reg));
438 LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
439 return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
440 DoubleRegister::ToAllocationIndex(reg));
444 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
445 return Use(value, ToUnallocated(fixed_register));
449 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
450 return Use(value, ToUnallocated(reg));
454 LOperand* LChunkBuilder::UseRegister(HValue* value) {
455 return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
459 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
461 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
462 LUnallocated::USED_AT_START));
466 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
467 return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
471 LOperand* LChunkBuilder::Use(HValue* value) {
472 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
476 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
477 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
478 LUnallocated::USED_AT_START));
482 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
483 return value->IsConstant()
484 ? chunk_->DefineConstantOperand(HConstant::cast(value))
489 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
490 return value->IsConstant()
491 ? chunk_->DefineConstantOperand(HConstant::cast(value))
496 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
497 return value->IsConstant()
498 ? chunk_->DefineConstantOperand(HConstant::cast(value))
499 : UseRegister(value);
503 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
504 return value->IsConstant()
505 ? chunk_->DefineConstantOperand(HConstant::cast(value))
506 : UseRegisterAtStart(value);
510 LOperand* LChunkBuilder::UseConstant(HValue* value) {
511 return chunk_->DefineConstantOperand(HConstant::cast(value));
515 LOperand* LChunkBuilder::UseAny(HValue* value) {
516 return value->IsConstant()
517 ? chunk_->DefineConstantOperand(HConstant::cast(value))
518 : Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
522 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
523 if (value->EmitAtUses()) {
524 HInstruction* instr = HInstruction::cast(value);
525 VisitInstruction(instr);
527 operand->set_virtual_register(value->id());
532 LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
533 LUnallocated* result) {
534 result->set_virtual_register(current_instruction_->id());
535 instr->set_result(result);
540 LInstruction* LChunkBuilder::DefineAsRegister(
541 LTemplateResultInstruction<1>* instr) {
543 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
547 LInstruction* LChunkBuilder::DefineAsSpilled(
548 LTemplateResultInstruction<1>* instr, int index) {
550 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
554 LInstruction* LChunkBuilder::DefineSameAsFirst(
555 LTemplateResultInstruction<1>* instr) {
557 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
561 LInstruction* LChunkBuilder::DefineFixed(
562 LTemplateResultInstruction<1>* instr, Register reg) {
563 return Define(instr, ToUnallocated(reg));
567 LInstruction* LChunkBuilder::DefineFixedDouble(
568 LTemplateResultInstruction<1>* instr, DoubleRegister reg) {
569 return Define(instr, ToUnallocated(reg));
573 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
574 HEnvironment* hydrogen_env = current_block_->last_environment();
575 int argument_index_accumulator = 0;
576 ZoneList<HValue*> objects_to_materialize(0, zone());
577 instr->set_environment(CreateEnvironment(hydrogen_env,
578 &argument_index_accumulator,
579 &objects_to_materialize));
584 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
585 HInstruction* hinstr,
586 CanDeoptimize can_deoptimize) {
587 info()->MarkAsNonDeferredCalling();
592 instr = AssignPointerMap(instr);
594 // If instruction does not have side-effects lazy deoptimization
595 // after the call will try to deoptimize to the point before the call.
596 // Thus we still need to attach environment to this call even if
597 // call sequence can not deoptimize eagerly.
598 bool needs_environment =
599 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
600 !hinstr->HasObservableSideEffects();
601 if (needs_environment && !instr->HasEnvironment()) {
602 instr = AssignEnvironment(instr);
603 // We can't really figure out if the environment is needed or not.
604 instr->environment()->set_has_been_used();
611 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
612 ASSERT(!instr->HasPointerMap());
613 instr->set_pointer_map(new(zone()) LPointerMap(zone()));
618 LUnallocated* LChunkBuilder::TempRegister() {
619 LUnallocated* operand =
620 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
621 int vreg = allocator_->GetVirtualRegister();
622 if (!allocator_->AllocationOk()) {
623 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
626 operand->set_virtual_register(vreg);
631 LUnallocated* LChunkBuilder::TempDoubleRegister() {
632 LUnallocated* operand =
633 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_DOUBLE_REGISTER);
634 int vreg = allocator_->GetVirtualRegister();
635 if (!allocator_->AllocationOk()) {
636 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
639 operand->set_virtual_register(vreg);
644 LOperand* LChunkBuilder::FixedTemp(Register reg) {
645 LUnallocated* operand = ToUnallocated(reg);
646 ASSERT(operand->HasFixedPolicy());
651 LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
652 LUnallocated* operand = ToUnallocated(reg);
653 ASSERT(operand->HasFixedPolicy());
658 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
659 return new(zone()) LLabel(instr->block());
663 LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
664 return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
668 LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
674 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
675 return AssignEnvironment(new(zone()) LDeoptimize);
679 LInstruction* LChunkBuilder::DoShift(Token::Value op,
680 HBitwiseBinaryOperation* instr) {
681 if (instr->representation().IsSmiOrInteger32()) {
682 ASSERT(instr->left()->representation().Equals(instr->representation()));
683 ASSERT(instr->right()->representation().Equals(instr->representation()));
684 LOperand* left = UseRegisterAtStart(instr->left());
686 HValue* right_value = instr->right();
687 LOperand* right = NULL;
688 int constant_value = 0;
689 bool does_deopt = false;
690 if (right_value->IsConstant()) {
691 HConstant* constant = HConstant::cast(right_value);
692 right = chunk_->DefineConstantOperand(constant);
693 constant_value = constant->Integer32Value() & 0x1f;
694 // Left shifts can deoptimize if we shift by > 0 and the result cannot be
696 if (instr->representation().IsSmi() && constant_value > 0) {
697 does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
700 right = UseRegisterAtStart(right_value);
703 // Shift operations can only deoptimize if we do a logical shift
704 // by 0 and the result cannot be truncated to int32.
705 if (op == Token::SHR && constant_value == 0) {
706 if (FLAG_opt_safe_uint32_operations) {
707 does_deopt = !instr->CheckFlag(HInstruction::kUint32);
709 does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToInt32);
713 LInstruction* result =
714 DefineAsRegister(new(zone()) LShiftI(op, left, right, does_deopt));
715 return does_deopt ? AssignEnvironment(result) : result;
717 return DoArithmeticT(op, instr);
722 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
723 HArithmeticBinaryOperation* instr) {
724 ASSERT(instr->representation().IsDouble());
725 ASSERT(instr->left()->representation().IsDouble());
726 ASSERT(instr->right()->representation().IsDouble());
727 if (op == Token::MOD) {
728 LOperand* left = UseFixedDouble(instr->left(), d0);
729 LOperand* right = UseFixedDouble(instr->right(), d1);
730 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
731 return MarkAsCall(DefineFixedDouble(result, d0), instr);
733 LOperand* left = UseRegisterAtStart(instr->left());
734 LOperand* right = UseRegisterAtStart(instr->right());
735 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
736 return DefineAsRegister(result);
741 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
742 HBinaryOperation* instr) {
743 HValue* left = instr->left();
744 HValue* right = instr->right();
745 ASSERT(left->representation().IsTagged());
746 ASSERT(right->representation().IsTagged());
747 LOperand* context = UseFixed(instr->context(), cp);
748 LOperand* left_operand = UseFixed(left, r1);
749 LOperand* right_operand = UseFixed(right, r0);
750 LArithmeticT* result =
751 new(zone()) LArithmeticT(op, context, left_operand, right_operand);
752 return MarkAsCall(DefineFixed(result, r0), instr);
756 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
757 ASSERT(is_building());
758 current_block_ = block;
759 next_block_ = next_block;
760 if (block->IsStartBlock()) {
761 block->UpdateEnvironment(graph_->start_environment());
763 } else if (block->predecessors()->length() == 1) {
764 // We have a single predecessor => copy environment and outgoing
765 // argument count from the predecessor.
766 ASSERT(block->phis()->length() == 0);
767 HBasicBlock* pred = block->predecessors()->at(0);
768 HEnvironment* last_environment = pred->last_environment();
769 ASSERT(last_environment != NULL);
770 // Only copy the environment, if it is later used again.
771 if (pred->end()->SecondSuccessor() == NULL) {
772 ASSERT(pred->end()->FirstSuccessor() == block);
774 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
775 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
776 last_environment = last_environment->Copy();
779 block->UpdateEnvironment(last_environment);
780 ASSERT(pred->argument_count() >= 0);
781 argument_count_ = pred->argument_count();
783 // We are at a state join => process phis.
784 HBasicBlock* pred = block->predecessors()->at(0);
785 // No need to copy the environment, it cannot be used later.
786 HEnvironment* last_environment = pred->last_environment();
787 for (int i = 0; i < block->phis()->length(); ++i) {
788 HPhi* phi = block->phis()->at(i);
789 if (phi->HasMergedIndex()) {
790 last_environment->SetValueAt(phi->merged_index(), phi);
793 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
794 if (block->deleted_phis()->at(i) < last_environment->length()) {
795 last_environment->SetValueAt(block->deleted_phis()->at(i),
796 graph_->GetConstantUndefined());
799 block->UpdateEnvironment(last_environment);
800 // Pick up the outgoing argument count of one of the predecessors.
801 argument_count_ = pred->argument_count();
803 HInstruction* current = block->first();
804 int start = chunk_->instructions()->length();
805 while (current != NULL && !is_aborted()) {
806 // Code for constants in registers is generated lazily.
807 if (!current->EmitAtUses()) {
808 VisitInstruction(current);
810 current = current->next();
812 int end = chunk_->instructions()->length() - 1;
814 block->set_first_instruction_index(start);
815 block->set_last_instruction_index(end);
817 block->set_argument_count(argument_count_);
819 current_block_ = NULL;
823 void LChunkBuilder::VisitInstruction(HInstruction* current) {
824 HInstruction* old_current = current_instruction_;
825 current_instruction_ = current;
827 LInstruction* instr = NULL;
828 if (current->CanReplaceWithDummyUses()) {
829 if (current->OperandCount() == 0) {
830 instr = DefineAsRegister(new(zone()) LDummy());
832 ASSERT(!current->OperandAt(0)->IsControlInstruction());
833 instr = DefineAsRegister(new(zone())
834 LDummyUse(UseAny(current->OperandAt(0))));
836 for (int i = 1; i < current->OperandCount(); ++i) {
837 if (current->OperandAt(i)->IsControlInstruction()) continue;
838 LInstruction* dummy =
839 new(zone()) LDummyUse(UseAny(current->OperandAt(i)));
840 dummy->set_hydrogen_value(current);
841 chunk_->AddInstruction(dummy, current_block_);
844 HBasicBlock* successor;
845 if (current->IsControlInstruction() &&
846 HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
848 instr = new(zone()) LGoto(successor);
850 instr = current->CompileToLithium(this);
854 argument_count_ += current->argument_delta();
855 ASSERT(argument_count_ >= 0);
858 AddInstruction(instr, current);
861 current_instruction_ = old_current;
865 void LChunkBuilder::AddInstruction(LInstruction* instr,
866 HInstruction* hydrogen_val) {
867 // Associate the hydrogen instruction first, since we may need it for
868 // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
869 instr->set_hydrogen_value(hydrogen_val);
872 // Make sure that the lithium instruction has either no fixed register
873 // constraints in temps or the result OR no uses that are only used at
874 // start. If this invariant doesn't hold, the register allocator can decide
875 // to insert a split of a range immediately before the instruction due to an
876 // already allocated register needing to be used for the instruction's fixed
877 // register constraint. In this case, The register allocator won't see an
878 // interference between the split child and the use-at-start (it would if
879 // the it was just a plain use), so it is free to move the split child into
880 // the same register that is used for the use-at-start.
881 // See https://code.google.com/p/chromium/issues/detail?id=201590
882 if (!(instr->ClobbersRegisters() &&
883 instr->ClobbersDoubleRegisters(isolate()))) {
885 int used_at_start = 0;
886 for (UseIterator it(instr); !it.Done(); it.Advance()) {
887 LUnallocated* operand = LUnallocated::cast(it.Current());
888 if (operand->IsUsedAtStart()) ++used_at_start;
890 if (instr->Output() != NULL) {
891 if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
893 for (TempIterator it(instr); !it.Done(); it.Advance()) {
894 LUnallocated* operand = LUnallocated::cast(it.Current());
895 if (operand->HasFixedPolicy()) ++fixed;
897 ASSERT(fixed == 0 || used_at_start == 0);
901 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
902 instr = AssignPointerMap(instr);
904 if (FLAG_stress_environments && !instr->HasEnvironment()) {
905 instr = AssignEnvironment(instr);
907 chunk_->AddInstruction(instr, current_block_);
909 if (instr->IsCall()) {
910 HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
911 LInstruction* instruction_needing_environment = NULL;
912 if (hydrogen_val->HasObservableSideEffects()) {
913 HSimulate* sim = HSimulate::cast(hydrogen_val->next());
914 instruction_needing_environment = instr;
915 sim->ReplayEnvironment(current_block_->last_environment());
916 hydrogen_value_for_lazy_bailout = sim;
918 LInstruction* bailout = AssignEnvironment(new(zone()) LLazyBailout());
919 bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
920 chunk_->AddInstruction(bailout, current_block_);
921 if (instruction_needing_environment != NULL) {
922 // Store the lazy deopt environment with the instruction if needed.
923 // Right now it is only used for LInstanceOfKnownGlobal.
924 instruction_needing_environment->
925 SetDeferredLazyDeoptimizationEnvironment(bailout->environment());
931 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
932 return new(zone()) LGoto(instr->FirstSuccessor());
936 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
937 HValue* value = instr->value();
938 Representation r = value->representation();
939 HType type = value->type();
940 ToBooleanStub::Types expected = instr->expected_input_types();
941 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
943 bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
944 type.IsJSArray() || type.IsHeapNumber() || type.IsString();
945 LInstruction* branch = new(zone()) LBranch(UseRegister(value));
947 ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
948 !expected.IsGeneric())) {
949 branch = AssignEnvironment(branch);
955 LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
956 return new(zone()) LDebugBreak();
960 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
961 ASSERT(instr->value()->representation().IsTagged());
962 LOperand* value = UseRegisterAtStart(instr->value());
963 LOperand* temp = TempRegister();
964 return new(zone()) LCmpMapAndBranch(value, temp);
968 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* instr) {
969 info()->MarkAsRequiresFrame();
970 LOperand* value = UseRegister(instr->value());
971 return DefineAsRegister(new(zone()) LArgumentsLength(value));
975 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
976 info()->MarkAsRequiresFrame();
977 return DefineAsRegister(new(zone()) LArgumentsElements);
981 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
982 LOperand* context = UseFixed(instr->context(), cp);
983 LInstanceOf* result =
984 new(zone()) LInstanceOf(context, UseFixed(instr->left(), r0),
985 UseFixed(instr->right(), r1));
986 return MarkAsCall(DefineFixed(result, r0), instr);
990 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
991 HInstanceOfKnownGlobal* instr) {
992 LInstanceOfKnownGlobal* result =
993 new(zone()) LInstanceOfKnownGlobal(
994 UseFixed(instr->context(), cp),
995 UseFixed(instr->left(), r0),
997 return MarkAsCall(DefineFixed(result, r0), instr);
1001 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1002 LOperand* receiver = UseRegisterAtStart(instr->receiver());
1003 LOperand* function = UseRegisterAtStart(instr->function());
1004 LWrapReceiver* result = new(zone()) LWrapReceiver(receiver, function);
1005 return AssignEnvironment(DefineAsRegister(result));
1009 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1010 LOperand* function = UseFixed(instr->function(), r1);
1011 LOperand* receiver = UseFixed(instr->receiver(), r0);
1012 LOperand* length = UseFixed(instr->length(), r2);
1013 LOperand* elements = UseFixed(instr->elements(), r3);
1014 LApplyArguments* result = new(zone()) LApplyArguments(function,
1018 return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
1022 LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1023 int argc = instr->OperandCount();
1024 for (int i = 0; i < argc; ++i) {
1025 LOperand* argument = Use(instr->argument(i));
1026 AddInstruction(new(zone()) LPushArgument(argument), instr);
1032 LInstruction* LChunkBuilder::DoStoreCodeEntry(
1033 HStoreCodeEntry* store_code_entry) {
1034 LOperand* function = UseRegister(store_code_entry->function());
1035 LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1036 return new(zone()) LStoreCodeEntry(function, code_object);
1040 LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1041 HInnerAllocatedObject* instr) {
1042 LOperand* base_object = UseRegisterAtStart(instr->base_object());
1043 LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1044 return DefineAsRegister(
1045 new(zone()) LInnerAllocatedObject(base_object, offset));
1049 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1050 return instr->HasNoUses()
1052 : DefineAsRegister(new(zone()) LThisFunction);
1056 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1057 if (instr->HasNoUses()) return NULL;
1059 if (info()->IsStub()) {
1060 return DefineFixed(new(zone()) LContext, cp);
1063 return DefineAsRegister(new(zone()) LContext);
1067 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1068 LOperand* context = UseFixed(instr->context(), cp);
1069 return MarkAsCall(new(zone()) LDeclareGlobals(context), instr);
1073 LInstruction* LChunkBuilder::DoCallJSFunction(
1074 HCallJSFunction* instr) {
1075 LOperand* function = UseFixed(instr->function(), r1);
1077 LCallJSFunction* result = new(zone()) LCallJSFunction(function);
1079 return MarkAsCall(DefineFixed(result, r0), instr);
1083 LInstruction* LChunkBuilder::DoCallWithDescriptor(
1084 HCallWithDescriptor* instr) {
1085 const CallInterfaceDescriptor* descriptor = instr->descriptor();
1087 LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1088 ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1089 ops.Add(target, zone());
1090 for (int i = 1; i < instr->OperandCount(); i++) {
1091 LOperand* op = UseFixed(instr->OperandAt(i),
1092 descriptor->GetParameterRegister(i - 1));
1093 ops.Add(op, zone());
1096 LCallWithDescriptor* result = new(zone()) LCallWithDescriptor(
1097 descriptor, ops, zone());
1098 return MarkAsCall(DefineFixed(result, r0), instr);
1102 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1103 LOperand* context = UseFixed(instr->context(), cp);
1104 LOperand* function = UseFixed(instr->function(), r1);
1105 LInvokeFunction* result = new(zone()) LInvokeFunction(context, function);
1106 return MarkAsCall(DefineFixed(result, r0), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1110 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1111 switch (instr->op()) {
1112 case kMathFloor: return DoMathFloor(instr);
1113 case kMathRound: return DoMathRound(instr);
1114 case kMathAbs: return DoMathAbs(instr);
1115 case kMathLog: return DoMathLog(instr);
1116 case kMathExp: return DoMathExp(instr);
1117 case kMathSqrt: return DoMathSqrt(instr);
1118 case kMathPowHalf: return DoMathPowHalf(instr);
1119 case kMathClz32: return DoMathClz32(instr);
1127 LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1128 LOperand* input = UseRegister(instr->value());
1129 LMathFloor* result = new(zone()) LMathFloor(input);
1130 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1134 LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1135 LOperand* input = UseRegister(instr->value());
1136 LOperand* temp = TempDoubleRegister();
1137 LMathRound* result = new(zone()) LMathRound(input, temp);
1138 return AssignEnvironment(DefineAsRegister(result));
1142 LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1143 Representation r = instr->value()->representation();
1144 LOperand* context = (r.IsDouble() || r.IsSmiOrInteger32())
1146 : UseFixed(instr->context(), cp);
1147 LOperand* input = UseRegister(instr->value());
1148 LInstruction* result =
1149 DefineAsRegister(new(zone()) LMathAbs(context, input));
1150 if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1151 if (!r.IsDouble()) result = AssignEnvironment(result);
1156 LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1157 ASSERT(instr->representation().IsDouble());
1158 ASSERT(instr->value()->representation().IsDouble());
1159 LOperand* input = UseFixedDouble(instr->value(), d0);
1160 return MarkAsCall(DefineFixedDouble(new(zone()) LMathLog(input), d0), instr);
1164 LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1165 LOperand* input = UseRegisterAtStart(instr->value());
1166 LMathClz32* result = new(zone()) LMathClz32(input);
1167 return DefineAsRegister(result);
1171 LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1172 ASSERT(instr->representation().IsDouble());
1173 ASSERT(instr->value()->representation().IsDouble());
1174 LOperand* input = UseRegister(instr->value());
1175 LOperand* temp1 = TempRegister();
1176 LOperand* temp2 = TempRegister();
1177 LOperand* double_temp = TempDoubleRegister();
1178 LMathExp* result = new(zone()) LMathExp(input, double_temp, temp1, temp2);
1179 return DefineAsRegister(result);
1183 LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1184 LOperand* input = UseRegisterAtStart(instr->value());
1185 LMathSqrt* result = new(zone()) LMathSqrt(input);
1186 return DefineAsRegister(result);
1190 LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1191 LOperand* input = UseRegisterAtStart(instr->value());
1192 LMathPowHalf* result = new(zone()) LMathPowHalf(input);
1193 return DefineAsRegister(result);
1197 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1198 LOperand* context = UseFixed(instr->context(), cp);
1199 LOperand* constructor = UseFixed(instr->constructor(), r1);
1200 LCallNew* result = new(zone()) LCallNew(context, constructor);
1201 return MarkAsCall(DefineFixed(result, r0), instr);
1205 LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1206 LOperand* context = UseFixed(instr->context(), cp);
1207 LOperand* constructor = UseFixed(instr->constructor(), r1);
1208 LCallNewArray* result = new(zone()) LCallNewArray(context, constructor);
1209 return MarkAsCall(DefineFixed(result, r0), instr);
1213 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1214 LOperand* context = UseFixed(instr->context(), cp);
1215 LOperand* function = UseFixed(instr->function(), r1);
1216 LCallFunction* call = new(zone()) LCallFunction(context, function);
1217 return MarkAsCall(DefineFixed(call, r0), instr);
1221 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1222 LOperand* context = UseFixed(instr->context(), cp);
1223 return MarkAsCall(DefineFixed(new(zone()) LCallRuntime(context), r0), instr);
1227 LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1228 return DoShift(Token::ROR, instr);
1232 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1233 return DoShift(Token::SHR, instr);
1237 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1238 return DoShift(Token::SAR, instr);
1242 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1243 return DoShift(Token::SHL, instr);
1247 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1248 if (instr->representation().IsSmiOrInteger32()) {
1249 ASSERT(instr->left()->representation().Equals(instr->representation()));
1250 ASSERT(instr->right()->representation().Equals(instr->representation()));
1251 ASSERT(instr->CheckFlag(HValue::kTruncatingToInt32));
1253 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1254 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1255 return DefineAsRegister(new(zone()) LBitI(left, right));
1257 return DoArithmeticT(instr->op(), instr);
1262 LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1263 ASSERT(instr->representation().IsSmiOrInteger32());
1264 ASSERT(instr->left()->representation().Equals(instr->representation()));
1265 ASSERT(instr->right()->representation().Equals(instr->representation()));
1266 LOperand* dividend = UseRegister(instr->left());
1267 int32_t divisor = instr->right()->GetInteger32Constant();
1268 LInstruction* result = DefineAsRegister(new(zone()) LDivByPowerOf2I(
1269 dividend, divisor));
1270 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1271 (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1272 (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1273 divisor != 1 && divisor != -1)) {
1274 result = AssignEnvironment(result);
1280 LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1281 ASSERT(instr->representation().IsInteger32());
1282 ASSERT(instr->left()->representation().Equals(instr->representation()));
1283 ASSERT(instr->right()->representation().Equals(instr->representation()));
1284 LOperand* dividend = UseRegister(instr->left());
1285 int32_t divisor = instr->right()->GetInteger32Constant();
1286 LInstruction* result = DefineAsRegister(new(zone()) LDivByConstI(
1287 dividend, divisor));
1289 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1290 !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1291 result = AssignEnvironment(result);
1297 LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1298 ASSERT(instr->representation().IsSmiOrInteger32());
1299 ASSERT(instr->left()->representation().Equals(instr->representation()));
1300 ASSERT(instr->right()->representation().Equals(instr->representation()));
1301 LOperand* dividend = UseRegister(instr->left());
1302 LOperand* divisor = UseRegister(instr->right());
1304 CpuFeatures::IsSupported(SUDIV) ? NULL : TempDoubleRegister();
1305 LInstruction* result =
1306 DefineAsRegister(new(zone()) LDivI(dividend, divisor, temp));
1307 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1308 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1309 (instr->CheckFlag(HValue::kCanOverflow) &&
1310 (!CpuFeatures::IsSupported(SUDIV) ||
1311 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) ||
1312 (!instr->IsMathFloorOfDiv() &&
1313 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1314 result = AssignEnvironment(result);
1320 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1321 if (instr->representation().IsSmiOrInteger32()) {
1322 if (instr->RightIsPowerOf2()) {
1323 return DoDivByPowerOf2I(instr);
1324 } else if (instr->right()->IsConstant()) {
1325 return DoDivByConstI(instr);
1327 return DoDivI(instr);
1329 } else if (instr->representation().IsDouble()) {
1330 return DoArithmeticD(Token::DIV, instr);
1332 return DoArithmeticT(Token::DIV, instr);
1337 LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1338 LOperand* dividend = UseRegisterAtStart(instr->left());
1339 int32_t divisor = instr->right()->GetInteger32Constant();
1340 LInstruction* result = DefineAsRegister(new(zone()) LFlooringDivByPowerOf2I(
1341 dividend, divisor));
1342 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1343 (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1344 result = AssignEnvironment(result);
1350 LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1351 ASSERT(instr->representation().IsInteger32());
1352 ASSERT(instr->left()->representation().Equals(instr->representation()));
1353 ASSERT(instr->right()->representation().Equals(instr->representation()));
1354 LOperand* dividend = UseRegister(instr->left());
1355 int32_t divisor = instr->right()->GetInteger32Constant();
1357 ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1358 (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
1359 NULL : TempRegister();
1360 LInstruction* result = DefineAsRegister(
1361 new(zone()) LFlooringDivByConstI(dividend, divisor, temp));
1363 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1364 result = AssignEnvironment(result);
1370 LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
1371 ASSERT(instr->representation().IsSmiOrInteger32());
1372 ASSERT(instr->left()->representation().Equals(instr->representation()));
1373 ASSERT(instr->right()->representation().Equals(instr->representation()));
1374 LOperand* dividend = UseRegister(instr->left());
1375 LOperand* divisor = UseRegister(instr->right());
1377 CpuFeatures::IsSupported(SUDIV) ? NULL : TempDoubleRegister();
1378 LFlooringDivI* div = new(zone()) LFlooringDivI(dividend, divisor, temp);
1379 return AssignEnvironment(DefineAsRegister(div));
1383 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1384 if (instr->RightIsPowerOf2()) {
1385 return DoFlooringDivByPowerOf2I(instr);
1386 } else if (instr->right()->IsConstant()) {
1387 return DoFlooringDivByConstI(instr);
1389 return DoFlooringDivI(instr);
1394 LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
1395 ASSERT(instr->representation().IsSmiOrInteger32());
1396 ASSERT(instr->left()->representation().Equals(instr->representation()));
1397 ASSERT(instr->right()->representation().Equals(instr->representation()));
1398 LOperand* dividend = UseRegisterAtStart(instr->left());
1399 int32_t divisor = instr->right()->GetInteger32Constant();
1400 LInstruction* result = DefineSameAsFirst(new(zone()) LModByPowerOf2I(
1401 dividend, divisor));
1402 if (instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1403 result = AssignEnvironment(result);
1409 LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
1410 ASSERT(instr->representation().IsSmiOrInteger32());
1411 ASSERT(instr->left()->representation().Equals(instr->representation()));
1412 ASSERT(instr->right()->representation().Equals(instr->representation()));
1413 LOperand* dividend = UseRegister(instr->left());
1414 int32_t divisor = instr->right()->GetInteger32Constant();
1415 LInstruction* result = DefineAsRegister(new(zone()) LModByConstI(
1416 dividend, divisor));
1417 if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1418 result = AssignEnvironment(result);
1424 LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1425 ASSERT(instr->representation().IsSmiOrInteger32());
1426 ASSERT(instr->left()->representation().Equals(instr->representation()));
1427 ASSERT(instr->right()->representation().Equals(instr->representation()));
1428 LOperand* dividend = UseRegister(instr->left());
1429 LOperand* divisor = UseRegister(instr->right());
1431 CpuFeatures::IsSupported(SUDIV) ? NULL : TempDoubleRegister();
1433 CpuFeatures::IsSupported(SUDIV) ? NULL : TempDoubleRegister();
1434 LInstruction* result = DefineAsRegister(new(zone()) LModI(
1435 dividend, divisor, temp, temp2));
1436 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1437 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1438 result = AssignEnvironment(result);
1444 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1445 if (instr->representation().IsSmiOrInteger32()) {
1446 if (instr->RightIsPowerOf2()) {
1447 return DoModByPowerOf2I(instr);
1448 } else if (instr->right()->IsConstant()) {
1449 return DoModByConstI(instr);
1451 return DoModI(instr);
1453 } else if (instr->representation().IsDouble()) {
1454 return DoArithmeticD(Token::MOD, instr);
1456 return DoArithmeticT(Token::MOD, instr);
1461 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1462 if (instr->representation().IsSmiOrInteger32()) {
1463 ASSERT(instr->left()->representation().Equals(instr->representation()));
1464 ASSERT(instr->right()->representation().Equals(instr->representation()));
1465 HValue* left = instr->BetterLeftOperand();
1466 HValue* right = instr->BetterRightOperand();
1469 bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1470 bool bailout_on_minus_zero = instr->CheckFlag(HValue::kBailoutOnMinusZero);
1472 if (right->IsConstant()) {
1473 HConstant* constant = HConstant::cast(right);
1474 int32_t constant_value = constant->Integer32Value();
1475 // Constants -1, 0 and 1 can be optimized if the result can overflow.
1476 // For other constants, it can be optimized only without overflow.
1477 if (!can_overflow || ((constant_value >= -1) && (constant_value <= 1))) {
1478 left_op = UseRegisterAtStart(left);
1479 right_op = UseConstant(right);
1481 if (bailout_on_minus_zero) {
1482 left_op = UseRegister(left);
1484 left_op = UseRegisterAtStart(left);
1486 right_op = UseRegister(right);
1489 if (bailout_on_minus_zero) {
1490 left_op = UseRegister(left);
1492 left_op = UseRegisterAtStart(left);
1494 right_op = UseRegister(right);
1496 LMulI* mul = new(zone()) LMulI(left_op, right_op);
1497 if (can_overflow || bailout_on_minus_zero) {
1498 AssignEnvironment(mul);
1500 return DefineAsRegister(mul);
1502 } else if (instr->representation().IsDouble()) {
1503 if (instr->UseCount() == 1 && (instr->uses().value()->IsAdd() ||
1504 instr->uses().value()->IsSub())) {
1505 HBinaryOperation* use = HBinaryOperation::cast(instr->uses().value());
1507 if (use->IsAdd() && instr == use->left()) {
1508 // This mul is the lhs of an add. The add and mul will be folded into a
1509 // multiply-add in DoAdd.
1512 if (instr == use->right() && use->IsAdd() && !use->left()->IsMul()) {
1513 // This mul is the rhs of an add, where the lhs is not another mul.
1514 // The add and mul will be folded into a multiply-add in DoAdd.
1517 if (instr == use->right() && use->IsSub()) {
1518 // This mul is the rhs of a sub. The sub and mul will be folded into a
1519 // multiply-sub in DoSub.
1524 return DoArithmeticD(Token::MUL, instr);
1526 return DoArithmeticT(Token::MUL, instr);
1531 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1532 if (instr->representation().IsSmiOrInteger32()) {
1533 ASSERT(instr->left()->representation().Equals(instr->representation()));
1534 ASSERT(instr->right()->representation().Equals(instr->representation()));
1536 if (instr->left()->IsConstant()) {
1537 // If lhs is constant, do reverse subtraction instead.
1538 return DoRSub(instr);
1541 LOperand* left = UseRegisterAtStart(instr->left());
1542 LOperand* right = UseOrConstantAtStart(instr->right());
1543 LSubI* sub = new(zone()) LSubI(left, right);
1544 LInstruction* result = DefineAsRegister(sub);
1545 if (instr->CheckFlag(HValue::kCanOverflow)) {
1546 result = AssignEnvironment(result);
1549 } else if (instr->representation().IsDouble()) {
1550 if (instr->right()->IsMul()) {
1551 return DoMultiplySub(instr->left(), HMul::cast(instr->right()));
1554 return DoArithmeticD(Token::SUB, instr);
1556 return DoArithmeticT(Token::SUB, instr);
1561 LInstruction* LChunkBuilder::DoRSub(HSub* instr) {
1562 ASSERT(instr->representation().IsSmiOrInteger32());
1563 ASSERT(instr->left()->representation().Equals(instr->representation()));
1564 ASSERT(instr->right()->representation().Equals(instr->representation()));
1566 // Note: The lhs of the subtraction becomes the rhs of the
1567 // reverse-subtraction.
1568 LOperand* left = UseRegisterAtStart(instr->right());
1569 LOperand* right = UseOrConstantAtStart(instr->left());
1570 LRSubI* rsb = new(zone()) LRSubI(left, right);
1571 LInstruction* result = DefineAsRegister(rsb);
1572 if (instr->CheckFlag(HValue::kCanOverflow)) {
1573 result = AssignEnvironment(result);
1579 LInstruction* LChunkBuilder::DoMultiplyAdd(HMul* mul, HValue* addend) {
1580 LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1581 LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1582 LOperand* addend_op = UseRegisterAtStart(addend);
1583 return DefineSameAsFirst(new(zone()) LMultiplyAddD(addend_op, multiplier_op,
1588 LInstruction* LChunkBuilder::DoMultiplySub(HValue* minuend, HMul* mul) {
1589 LOperand* minuend_op = UseRegisterAtStart(minuend);
1590 LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1591 LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1593 return DefineSameAsFirst(new(zone()) LMultiplySubD(minuend_op,
1599 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1600 if (instr->representation().IsSmiOrInteger32()) {
1601 ASSERT(instr->left()->representation().Equals(instr->representation()));
1602 ASSERT(instr->right()->representation().Equals(instr->representation()));
1603 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1604 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1605 LAddI* add = new(zone()) LAddI(left, right);
1606 LInstruction* result = DefineAsRegister(add);
1607 if (instr->CheckFlag(HValue::kCanOverflow)) {
1608 result = AssignEnvironment(result);
1611 } else if (instr->representation().IsExternal()) {
1612 ASSERT(instr->left()->representation().IsExternal());
1613 ASSERT(instr->right()->representation().IsInteger32());
1614 ASSERT(!instr->CheckFlag(HValue::kCanOverflow));
1615 LOperand* left = UseRegisterAtStart(instr->left());
1616 LOperand* right = UseOrConstantAtStart(instr->right());
1617 LAddI* add = new(zone()) LAddI(left, right);
1618 LInstruction* result = DefineAsRegister(add);
1620 } else if (instr->representation().IsDouble()) {
1621 if (instr->left()->IsMul()) {
1622 return DoMultiplyAdd(HMul::cast(instr->left()), instr->right());
1625 if (instr->right()->IsMul()) {
1626 ASSERT(!instr->left()->IsMul());
1627 return DoMultiplyAdd(HMul::cast(instr->right()), instr->left());
1630 return DoArithmeticD(Token::ADD, instr);
1632 return DoArithmeticT(Token::ADD, instr);
1637 LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
1638 LOperand* left = NULL;
1639 LOperand* right = NULL;
1640 if (instr->representation().IsSmiOrInteger32()) {
1641 ASSERT(instr->left()->representation().Equals(instr->representation()));
1642 ASSERT(instr->right()->representation().Equals(instr->representation()));
1643 left = UseRegisterAtStart(instr->BetterLeftOperand());
1644 right = UseOrConstantAtStart(instr->BetterRightOperand());
1646 ASSERT(instr->representation().IsDouble());
1647 ASSERT(instr->left()->representation().IsDouble());
1648 ASSERT(instr->right()->representation().IsDouble());
1649 left = UseRegisterAtStart(instr->left());
1650 right = UseRegisterAtStart(instr->right());
1652 return DefineAsRegister(new(zone()) LMathMinMax(left, right));
1656 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1657 ASSERT(instr->representation().IsDouble());
1658 // We call a C function for double power. It can't trigger a GC.
1659 // We need to use fixed result register for the call.
1660 Representation exponent_type = instr->right()->representation();
1661 ASSERT(instr->left()->representation().IsDouble());
1662 LOperand* left = UseFixedDouble(instr->left(), d0);
1663 LOperand* right = exponent_type.IsDouble() ?
1664 UseFixedDouble(instr->right(), d1) :
1665 UseFixed(instr->right(), r2);
1666 LPower* result = new(zone()) LPower(left, right);
1667 return MarkAsCall(DefineFixedDouble(result, d2),
1669 CAN_DEOPTIMIZE_EAGERLY);
1673 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1674 ASSERT(instr->left()->representation().IsTagged());
1675 ASSERT(instr->right()->representation().IsTagged());
1676 LOperand* context = UseFixed(instr->context(), cp);
1677 LOperand* left = UseFixed(instr->left(), r1);
1678 LOperand* right = UseFixed(instr->right(), r0);
1679 LCmpT* result = new(zone()) LCmpT(context, left, right);
1680 return MarkAsCall(DefineFixed(result, r0), instr);
1684 LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
1685 HCompareNumericAndBranch* instr) {
1686 Representation r = instr->representation();
1687 if (r.IsSmiOrInteger32()) {
1688 ASSERT(instr->left()->representation().Equals(r));
1689 ASSERT(instr->right()->representation().Equals(r));
1690 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1691 LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1692 return new(zone()) LCompareNumericAndBranch(left, right);
1694 ASSERT(r.IsDouble());
1695 ASSERT(instr->left()->representation().IsDouble());
1696 ASSERT(instr->right()->representation().IsDouble());
1697 LOperand* left = UseRegisterAtStart(instr->left());
1698 LOperand* right = UseRegisterAtStart(instr->right());
1699 return new(zone()) LCompareNumericAndBranch(left, right);
1704 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1705 HCompareObjectEqAndBranch* instr) {
1706 LOperand* left = UseRegisterAtStart(instr->left());
1707 LOperand* right = UseRegisterAtStart(instr->right());
1708 return new(zone()) LCmpObjectEqAndBranch(left, right);
1712 LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
1713 HCompareHoleAndBranch* instr) {
1714 LOperand* value = UseRegisterAtStart(instr->value());
1715 return new(zone()) LCmpHoleAndBranch(value);
1719 LInstruction* LChunkBuilder::DoCompareMinusZeroAndBranch(
1720 HCompareMinusZeroAndBranch* instr) {
1721 LOperand* value = UseRegister(instr->value());
1722 LOperand* scratch = TempRegister();
1723 return new(zone()) LCompareMinusZeroAndBranch(value, scratch);
1727 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1728 ASSERT(instr->value()->representation().IsTagged());
1729 LOperand* value = UseRegisterAtStart(instr->value());
1730 LOperand* temp = TempRegister();
1731 return new(zone()) LIsObjectAndBranch(value, temp);
1735 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1736 ASSERT(instr->value()->representation().IsTagged());
1737 LOperand* value = UseRegisterAtStart(instr->value());
1738 LOperand* temp = TempRegister();
1739 return new(zone()) LIsStringAndBranch(value, temp);
1743 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1744 ASSERT(instr->value()->representation().IsTagged());
1745 return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1749 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1750 HIsUndetectableAndBranch* instr) {
1751 ASSERT(instr->value()->representation().IsTagged());
1752 LOperand* value = UseRegisterAtStart(instr->value());
1753 return new(zone()) LIsUndetectableAndBranch(value, TempRegister());
1757 LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1758 HStringCompareAndBranch* instr) {
1759 ASSERT(instr->left()->representation().IsTagged());
1760 ASSERT(instr->right()->representation().IsTagged());
1761 LOperand* context = UseFixed(instr->context(), cp);
1762 LOperand* left = UseFixed(instr->left(), r1);
1763 LOperand* right = UseFixed(instr->right(), r0);
1764 LStringCompareAndBranch* result =
1765 new(zone()) LStringCompareAndBranch(context, left, right);
1766 return MarkAsCall(result, instr);
1770 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1771 HHasInstanceTypeAndBranch* instr) {
1772 ASSERT(instr->value()->representation().IsTagged());
1773 LOperand* value = UseRegisterAtStart(instr->value());
1774 return new(zone()) LHasInstanceTypeAndBranch(value);
1778 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1779 HGetCachedArrayIndex* instr) {
1780 ASSERT(instr->value()->representation().IsTagged());
1781 LOperand* value = UseRegisterAtStart(instr->value());
1783 return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1787 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1788 HHasCachedArrayIndexAndBranch* instr) {
1789 ASSERT(instr->value()->representation().IsTagged());
1790 return new(zone()) LHasCachedArrayIndexAndBranch(
1791 UseRegisterAtStart(instr->value()));
1795 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1796 HClassOfTestAndBranch* instr) {
1797 ASSERT(instr->value()->representation().IsTagged());
1798 LOperand* value = UseRegister(instr->value());
1799 return new(zone()) LClassOfTestAndBranch(value, TempRegister());
1803 LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
1804 LOperand* map = UseRegisterAtStart(instr->value());
1805 return DefineAsRegister(new(zone()) LMapEnumLength(map));
1809 LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1810 LOperand* object = UseFixed(instr->value(), r0);
1811 LDateField* result =
1812 new(zone()) LDateField(object, FixedTemp(r1), instr->index());
1813 return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
1817 LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
1818 LOperand* string = UseRegisterAtStart(instr->string());
1819 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1820 return DefineAsRegister(new(zone()) LSeqStringGetChar(string, index));
1824 LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1825 LOperand* string = UseRegisterAtStart(instr->string());
1826 LOperand* index = FLAG_debug_code
1827 ? UseRegisterAtStart(instr->index())
1828 : UseRegisterOrConstantAtStart(instr->index());
1829 LOperand* value = UseRegisterAtStart(instr->value());
1830 LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), cp) : NULL;
1831 return new(zone()) LSeqStringSetChar(context, string, index, value);
1835 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1836 if (!FLAG_debug_code && instr->skip_check()) return NULL;
1837 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1838 LOperand* length = !index->IsConstantOperand()
1839 ? UseRegisterOrConstantAtStart(instr->length())
1840 : UseRegisterAtStart(instr->length());
1841 LInstruction* result = new(zone()) LBoundsCheck(index, length);
1842 if (!FLAG_debug_code || !instr->skip_check()) {
1843 result = AssignEnvironment(result);
1849 LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1850 HBoundsCheckBaseIndexInformation* instr) {
1856 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1857 // The control instruction marking the end of a block that completed
1858 // abruptly (e.g., threw an exception). There is nothing specific to do.
1863 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1868 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1869 // All HForceRepresentation instructions should be eliminated in the
1870 // representation change phase of Hydrogen.
1876 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1877 Representation from = instr->from();
1878 Representation to = instr->to();
1879 HValue* val = instr->value();
1881 if (to.IsTagged()) {
1882 LOperand* value = UseRegister(val);
1883 return DefineSameAsFirst(new(zone()) LDummyUse(value));
1885 from = Representation::Tagged();
1887 if (from.IsTagged()) {
1888 if (to.IsDouble()) {
1889 LOperand* value = UseRegister(val);
1890 LInstruction* result = DefineAsRegister(new(zone()) LNumberUntagD(value));
1891 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1893 } else if (to.IsSmi()) {
1894 LOperand* value = UseRegister(val);
1895 if (val->type().IsSmi()) {
1896 return DefineSameAsFirst(new(zone()) LDummyUse(value));
1898 return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
1900 ASSERT(to.IsInteger32());
1901 if (val->type().IsSmi() || val->representation().IsSmi()) {
1902 LOperand* value = UseRegisterAtStart(val);
1903 return DefineAsRegister(new(zone()) LSmiUntag(value, false));
1905 LOperand* value = UseRegister(val);
1906 LOperand* temp1 = TempRegister();
1907 LOperand* temp2 = TempDoubleRegister();
1908 LInstruction* result =
1909 DefineSameAsFirst(new(zone()) LTaggedToI(value, temp1, temp2));
1910 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1914 } else if (from.IsDouble()) {
1915 if (to.IsTagged()) {
1916 info()->MarkAsDeferredCalling();
1917 LOperand* value = UseRegister(val);
1918 LOperand* temp1 = TempRegister();
1919 LOperand* temp2 = TempRegister();
1920 LUnallocated* result_temp = TempRegister();
1921 LNumberTagD* result = new(zone()) LNumberTagD(value, temp1, temp2);
1922 return AssignPointerMap(Define(result, result_temp));
1923 } else if (to.IsSmi()) {
1924 LOperand* value = UseRegister(val);
1925 return AssignEnvironment(
1926 DefineAsRegister(new(zone()) LDoubleToSmi(value)));
1928 ASSERT(to.IsInteger32());
1929 LOperand* value = UseRegister(val);
1930 LInstruction* result = DefineAsRegister(new(zone()) LDoubleToI(value));
1931 if (!instr->CanTruncateToInt32()) result = AssignEnvironment(result);
1934 } else if (from.IsInteger32()) {
1935 info()->MarkAsDeferredCalling();
1936 if (to.IsTagged()) {
1937 if (!instr->CheckFlag(HValue::kCanOverflow)) {
1938 LOperand* value = UseRegisterAtStart(val);
1939 return DefineAsRegister(new(zone()) LSmiTag(value));
1940 } else if (val->CheckFlag(HInstruction::kUint32)) {
1941 LOperand* value = UseRegisterAtStart(val);
1942 LOperand* temp1 = TempRegister();
1943 LOperand* temp2 = TempRegister();
1944 LNumberTagU* result = new(zone()) LNumberTagU(value, temp1, temp2);
1945 return AssignPointerMap(DefineAsRegister(result));
1947 LOperand* value = UseRegisterAtStart(val);
1948 LOperand* temp1 = TempRegister();
1949 LOperand* temp2 = TempRegister();
1950 LNumberTagI* result = new(zone()) LNumberTagI(value, temp1, temp2);
1951 return AssignPointerMap(DefineAsRegister(result));
1953 } else if (to.IsSmi()) {
1954 LOperand* value = UseRegister(val);
1955 LInstruction* result = DefineAsRegister(new(zone()) LSmiTag(value));
1956 if (instr->CheckFlag(HValue::kCanOverflow)) {
1957 result = AssignEnvironment(result);
1961 ASSERT(to.IsDouble());
1962 if (val->CheckFlag(HInstruction::kUint32)) {
1963 return DefineAsRegister(new(zone()) LUint32ToDouble(UseRegister(val)));
1965 return DefineAsRegister(new(zone()) LInteger32ToDouble(Use(val)));
1974 LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
1975 LOperand* value = UseRegisterAtStart(instr->value());
1976 LInstruction* result = new(zone()) LCheckNonSmi(value);
1977 if (!instr->value()->type().IsHeapObject()) {
1978 result = AssignEnvironment(result);
1984 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1985 LOperand* value = UseRegisterAtStart(instr->value());
1986 return AssignEnvironment(new(zone()) LCheckSmi(value));
1990 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1991 LOperand* value = UseRegisterAtStart(instr->value());
1992 LInstruction* result = new(zone()) LCheckInstanceType(value);
1993 return AssignEnvironment(result);
1997 LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
1998 LOperand* value = UseRegisterAtStart(instr->value());
1999 return AssignEnvironment(new(zone()) LCheckValue(value));
2003 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2004 if (instr->IsStabilityCheck()) return new(zone()) LCheckMaps;
2005 LOperand* value = UseRegisterAtStart(instr->value());
2006 LInstruction* result = AssignEnvironment(new(zone()) LCheckMaps(value));
2007 if (instr->HasMigrationTarget()) {
2008 info()->MarkAsDeferredCalling();
2009 result = AssignPointerMap(result);
2015 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2016 HValue* value = instr->value();
2017 Representation input_rep = value->representation();
2018 LOperand* reg = UseRegister(value);
2019 if (input_rep.IsDouble()) {
2020 return DefineAsRegister(new(zone()) LClampDToUint8(reg));
2021 } else if (input_rep.IsInteger32()) {
2022 return DefineAsRegister(new(zone()) LClampIToUint8(reg));
2024 ASSERT(input_rep.IsSmiOrTagged());
2025 // Register allocator doesn't (yet) support allocation of double
2026 // temps. Reserve d1 explicitly.
2027 LClampTToUint8* result =
2028 new(zone()) LClampTToUint8(reg, TempDoubleRegister());
2029 return AssignEnvironment(DefineAsRegister(result));
2034 LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2035 HValue* value = instr->value();
2036 ASSERT(value->representation().IsDouble());
2037 return DefineAsRegister(new(zone()) LDoubleBits(UseRegister(value)));
2041 LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2042 LOperand* lo = UseRegister(instr->lo());
2043 LOperand* hi = UseRegister(instr->hi());
2044 return DefineAsRegister(new(zone()) LConstructDouble(hi, lo));
2048 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2049 LOperand* context = info()->IsStub()
2050 ? UseFixed(instr->context(), cp)
2052 LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2053 return new(zone()) LReturn(UseFixed(instr->value(), r0), context,
2058 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2059 Representation r = instr->representation();
2061 return DefineAsRegister(new(zone()) LConstantS);
2062 } else if (r.IsInteger32()) {
2063 return DefineAsRegister(new(zone()) LConstantI);
2064 } else if (r.IsDouble()) {
2065 return DefineAsRegister(new(zone()) LConstantD);
2066 } else if (r.IsExternal()) {
2067 return DefineAsRegister(new(zone()) LConstantE);
2068 } else if (r.IsTagged()) {
2069 return DefineAsRegister(new(zone()) LConstantT);
2077 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2078 LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
2079 return instr->RequiresHoleCheck()
2080 ? AssignEnvironment(DefineAsRegister(result))
2081 : DefineAsRegister(result);
2085 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2086 LOperand* context = UseFixed(instr->context(), cp);
2087 LOperand* global_object = UseFixed(instr->global_object(), r0);
2088 LLoadGlobalGeneric* result =
2089 new(zone()) LLoadGlobalGeneric(context, global_object);
2090 return MarkAsCall(DefineFixed(result, r0), instr);
2094 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2095 LOperand* value = UseRegister(instr->value());
2096 // Use a temp to check the value in the cell in the case where we perform
2098 return instr->RequiresHoleCheck()
2099 ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
2100 : new(zone()) LStoreGlobalCell(value, NULL);
2104 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2105 LOperand* context = UseRegisterAtStart(instr->value());
2106 LInstruction* result =
2107 DefineAsRegister(new(zone()) LLoadContextSlot(context));
2108 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2109 result = AssignEnvironment(result);
2115 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2118 if (instr->NeedsWriteBarrier()) {
2119 context = UseTempRegister(instr->context());
2120 value = UseTempRegister(instr->value());
2122 context = UseRegister(instr->context());
2123 value = UseRegister(instr->value());
2125 LInstruction* result = new(zone()) LStoreContextSlot(context, value);
2126 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2127 result = AssignEnvironment(result);
2133 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2134 LOperand* obj = UseRegisterAtStart(instr->object());
2135 return DefineAsRegister(new(zone()) LLoadNamedField(obj));
2139 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2140 LOperand* context = UseFixed(instr->context(), cp);
2141 LOperand* object = UseFixed(instr->object(), r0);
2142 LInstruction* result =
2143 DefineFixed(new(zone()) LLoadNamedGeneric(context, object), r0);
2144 return MarkAsCall(result, instr);
2148 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2149 HLoadFunctionPrototype* instr) {
2150 return AssignEnvironment(DefineAsRegister(
2151 new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
2155 LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2156 return DefineAsRegister(new(zone()) LLoadRoot);
2160 LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2161 ASSERT(instr->key()->representation().IsSmiOrInteger32());
2162 ElementsKind elements_kind = instr->elements_kind();
2163 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2164 LInstruction* result = NULL;
2166 if (!instr->is_typed_elements()) {
2167 LOperand* obj = NULL;
2168 if (instr->representation().IsDouble()) {
2169 obj = UseRegister(instr->elements());
2171 ASSERT(instr->representation().IsSmiOrTagged());
2172 obj = UseRegisterAtStart(instr->elements());
2174 result = DefineAsRegister(new(zone()) LLoadKeyed(obj, key));
2177 (instr->representation().IsInteger32() &&
2178 !IsDoubleOrFloatElementsKind(elements_kind)) ||
2179 (instr->representation().IsDouble() &&
2180 IsDoubleOrFloatElementsKind(elements_kind)));
2181 LOperand* backing_store = UseRegister(instr->elements());
2182 result = DefineAsRegister(new(zone()) LLoadKeyed(backing_store, key));
2185 if ((instr->is_external() || instr->is_fixed_typed_array()) ?
2186 // see LCodeGen::DoLoadKeyedExternalArray
2187 ((elements_kind == EXTERNAL_UINT32_ELEMENTS ||
2188 elements_kind == UINT32_ELEMENTS) &&
2189 !instr->CheckFlag(HInstruction::kUint32)) :
2190 // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2191 // LCodeGen::DoLoadKeyedFixedArray
2192 instr->RequiresHoleCheck()) {
2193 result = AssignEnvironment(result);
2199 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2200 LOperand* context = UseFixed(instr->context(), cp);
2201 LOperand* object = UseFixed(instr->object(), r1);
2202 LOperand* key = UseFixed(instr->key(), r0);
2204 LInstruction* result =
2205 DefineFixed(new(zone()) LLoadKeyedGeneric(context, object, key), r0);
2206 return MarkAsCall(result, instr);
2210 LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2211 if (!instr->is_typed_elements()) {
2212 ASSERT(instr->elements()->representation().IsTagged());
2213 bool needs_write_barrier = instr->NeedsWriteBarrier();
2214 LOperand* object = NULL;
2215 LOperand* key = NULL;
2216 LOperand* val = NULL;
2218 if (instr->value()->representation().IsDouble()) {
2219 object = UseRegisterAtStart(instr->elements());
2220 val = UseRegister(instr->value());
2221 key = UseRegisterOrConstantAtStart(instr->key());
2223 ASSERT(instr->value()->representation().IsSmiOrTagged());
2224 if (needs_write_barrier) {
2225 object = UseTempRegister(instr->elements());
2226 val = UseTempRegister(instr->value());
2227 key = UseTempRegister(instr->key());
2229 object = UseRegisterAtStart(instr->elements());
2230 val = UseRegisterAtStart(instr->value());
2231 key = UseRegisterOrConstantAtStart(instr->key());
2235 return new(zone()) LStoreKeyed(object, key, val);
2239 (instr->value()->representation().IsInteger32() &&
2240 !IsDoubleOrFloatElementsKind(instr->elements_kind())) ||
2241 (instr->value()->representation().IsDouble() &&
2242 IsDoubleOrFloatElementsKind(instr->elements_kind())));
2243 ASSERT((instr->is_fixed_typed_array() &&
2244 instr->elements()->representation().IsTagged()) ||
2245 (instr->is_external() &&
2246 instr->elements()->representation().IsExternal()));
2247 LOperand* val = UseRegister(instr->value());
2248 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2249 LOperand* backing_store = UseRegister(instr->elements());
2250 return new(zone()) LStoreKeyed(backing_store, key, val);
2254 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2255 LOperand* context = UseFixed(instr->context(), cp);
2256 LOperand* obj = UseFixed(instr->object(), r2);
2257 LOperand* key = UseFixed(instr->key(), r1);
2258 LOperand* val = UseFixed(instr->value(), r0);
2260 ASSERT(instr->object()->representation().IsTagged());
2261 ASSERT(instr->key()->representation().IsTagged());
2262 ASSERT(instr->value()->representation().IsTagged());
2265 new(zone()) LStoreKeyedGeneric(context, obj, key, val), instr);
2269 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2270 HTransitionElementsKind* instr) {
2271 if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2272 LOperand* object = UseRegister(instr->object());
2273 LOperand* new_map_reg = TempRegister();
2274 LTransitionElementsKind* result =
2275 new(zone()) LTransitionElementsKind(object, NULL, new_map_reg);
2278 LOperand* object = UseFixed(instr->object(), r0);
2279 LOperand* context = UseFixed(instr->context(), cp);
2280 LTransitionElementsKind* result =
2281 new(zone()) LTransitionElementsKind(object, context, NULL);
2282 return MarkAsCall(result, instr);
2287 LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2288 HTrapAllocationMemento* instr) {
2289 LOperand* object = UseRegister(instr->object());
2290 LOperand* temp = TempRegister();
2291 LTrapAllocationMemento* result =
2292 new(zone()) LTrapAllocationMemento(object, temp);
2293 return AssignEnvironment(result);
2297 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2298 bool is_in_object = instr->access().IsInobject();
2299 bool needs_write_barrier = instr->NeedsWriteBarrier();
2300 bool needs_write_barrier_for_map = instr->has_transition() &&
2301 instr->NeedsWriteBarrierForMap();
2304 if (needs_write_barrier) {
2306 ? UseRegister(instr->object())
2307 : UseTempRegister(instr->object());
2309 obj = needs_write_barrier_for_map
2310 ? UseRegister(instr->object())
2311 : UseRegisterAtStart(instr->object());
2315 if (needs_write_barrier || instr->field_representation().IsSmi()) {
2316 val = UseTempRegister(instr->value());
2317 } else if (instr->field_representation().IsDouble()) {
2318 val = UseRegisterAtStart(instr->value());
2320 val = UseRegister(instr->value());
2323 // We need a temporary register for write barrier of the map field.
2324 LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;
2326 return new(zone()) LStoreNamedField(obj, val, temp);
2330 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2331 LOperand* context = UseFixed(instr->context(), cp);
2332 LOperand* obj = UseFixed(instr->object(), r1);
2333 LOperand* val = UseFixed(instr->value(), r0);
2335 LInstruction* result = new(zone()) LStoreNamedGeneric(context, obj, val);
2336 return MarkAsCall(result, instr);
2340 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2341 LOperand* context = UseFixed(instr->context(), cp);
2342 LOperand* left = UseFixed(instr->left(), r1);
2343 LOperand* right = UseFixed(instr->right(), r0);
2345 DefineFixed(new(zone()) LStringAdd(context, left, right), r0),
2350 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2351 LOperand* string = UseTempRegister(instr->string());
2352 LOperand* index = UseTempRegister(instr->index());
2353 LOperand* context = UseAny(instr->context());
2354 LStringCharCodeAt* result =
2355 new(zone()) LStringCharCodeAt(context, string, index);
2356 return AssignPointerMap(DefineAsRegister(result));
2360 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2361 LOperand* char_code = UseRegister(instr->value());
2362 LOperand* context = UseAny(instr->context());
2363 LStringCharFromCode* result =
2364 new(zone()) LStringCharFromCode(context, char_code);
2365 return AssignPointerMap(DefineAsRegister(result));
2369 LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2370 info()->MarkAsDeferredCalling();
2371 LOperand* context = UseAny(instr->context());
2372 LOperand* size = instr->size()->IsConstant()
2373 ? UseConstant(instr->size())
2374 : UseTempRegister(instr->size());
2375 LOperand* temp1 = TempRegister();
2376 LOperand* temp2 = TempRegister();
2377 LAllocate* result = new(zone()) LAllocate(context, size, temp1, temp2);
2378 return AssignPointerMap(DefineAsRegister(result));
2382 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2383 LOperand* context = UseFixed(instr->context(), cp);
2385 DefineFixed(new(zone()) LRegExpLiteral(context), r0), instr);
2389 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2390 LOperand* context = UseFixed(instr->context(), cp);
2392 DefineFixed(new(zone()) LFunctionLiteral(context), r0), instr);
2396 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2397 ASSERT(argument_count_ == 0);
2398 allocator_->MarkAsOsrEntry();
2399 current_block_->last_environment()->set_ast_id(instr->ast_id());
2400 return AssignEnvironment(new(zone()) LOsrEntry);
2404 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2405 LParameter* result = new(zone()) LParameter;
2406 if (instr->kind() == HParameter::STACK_PARAMETER) {
2407 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2408 return DefineAsSpilled(result, spill_index);
2410 ASSERT(info()->IsStub());
2411 CodeStubInterfaceDescriptor* descriptor =
2412 info()->code_stub()->GetInterfaceDescriptor();
2413 int index = static_cast<int>(instr->index());
2414 Register reg = descriptor->GetParameterRegister(index);
2415 return DefineFixed(result, reg);
2420 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2421 // Use an index that corresponds to the location in the unoptimized frame,
2422 // which the optimized frame will subsume.
2423 int env_index = instr->index();
2424 int spill_index = 0;
2425 if (instr->environment()->is_parameter_index(env_index)) {
2426 spill_index = chunk()->GetParameterStackSlot(env_index);
2428 spill_index = env_index - instr->environment()->first_local_index();
2429 if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2430 Abort(kTooManySpillSlotsNeededForOSR);
2434 return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2438 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2439 LOperand* context = UseFixed(instr->context(), cp);
2440 return MarkAsCall(DefineFixed(new(zone()) LCallStub(context), r0), instr);
2444 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2445 // There are no real uses of the arguments object.
2446 // arguments.length and element access are supported directly on
2447 // stack arguments, and any real arguments object use causes a bailout.
2448 // So this value is never used.
2453 LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2454 instr->ReplayEnvironment(current_block_->last_environment());
2456 // There are no real uses of a captured object.
2461 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2462 info()->MarkAsRequiresFrame();
2463 LOperand* args = UseRegister(instr->arguments());
2464 LOperand* length = UseRegisterOrConstantAtStart(instr->length());
2465 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
2466 return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2470 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2471 LOperand* object = UseFixed(instr->value(), r0);
2472 LToFastProperties* result = new(zone()) LToFastProperties(object);
2473 return MarkAsCall(DefineFixed(result, r0), instr);
2477 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2478 LOperand* context = UseFixed(instr->context(), cp);
2479 LTypeof* result = new(zone()) LTypeof(context, UseFixed(instr->value(), r0));
2480 return MarkAsCall(DefineFixed(result, r0), instr);
2484 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2485 return new(zone()) LTypeofIsAndBranch(UseRegister(instr->value()));
2489 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2490 HIsConstructCallAndBranch* instr) {
2491 return new(zone()) LIsConstructCallAndBranch(TempRegister());
2495 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2496 instr->ReplayEnvironment(current_block_->last_environment());
2501 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2502 if (instr->is_function_entry()) {
2503 LOperand* context = UseFixed(instr->context(), cp);
2504 return MarkAsCall(new(zone()) LStackCheck(context), instr);
2506 ASSERT(instr->is_backwards_branch());
2507 LOperand* context = UseAny(instr->context());
2508 return AssignEnvironment(
2509 AssignPointerMap(new(zone()) LStackCheck(context)));
2514 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2515 HEnvironment* outer = current_block_->last_environment();
2516 outer->set_ast_id(instr->ReturnId());
2517 HConstant* undefined = graph()->GetConstantUndefined();
2518 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2519 instr->arguments_count(),
2522 instr->inlining_kind());
2523 // Only replay binding of arguments object if it wasn't removed from graph.
2524 if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2525 inner->Bind(instr->arguments_var(), instr->arguments_object());
2527 inner->set_entry(instr);
2528 current_block_->UpdateEnvironment(inner);
2529 chunk_->AddInlinedClosure(instr->closure());
2534 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2535 LInstruction* pop = NULL;
2537 HEnvironment* env = current_block_->last_environment();
2539 if (env->entry()->arguments_pushed()) {
2540 int argument_count = env->arguments_environment()->parameter_count();
2541 pop = new(zone()) LDrop(argument_count);
2542 ASSERT(instr->argument_delta() == -argument_count);
2545 HEnvironment* outer = current_block_->last_environment()->
2546 DiscardInlined(false);
2547 current_block_->UpdateEnvironment(outer);
2553 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2554 LOperand* context = UseFixed(instr->context(), cp);
2555 LOperand* object = UseFixed(instr->enumerable(), r0);
2556 LForInPrepareMap* result = new(zone()) LForInPrepareMap(context, object);
2557 return MarkAsCall(DefineFixed(result, r0), instr, CAN_DEOPTIMIZE_EAGERLY);
2561 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2562 LOperand* map = UseRegister(instr->map());
2563 return AssignEnvironment(DefineAsRegister(new(zone()) LForInCacheArray(map)));
2567 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2568 LOperand* value = UseRegisterAtStart(instr->value());
2569 LOperand* map = UseRegisterAtStart(instr->map());
2570 return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2574 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2575 LOperand* object = UseRegister(instr->object());
2576 LOperand* index = UseTempRegister(instr->index());
2577 LLoadFieldByIndex* load = new(zone()) LLoadFieldByIndex(object, index);
2578 LInstruction* result = DefineSameAsFirst(load);
2579 return AssignPointerMap(result);
2583 LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2584 LOperand* context = UseRegisterAtStart(instr->context());
2585 return new(zone()) LStoreFrameContext(context);
2589 LInstruction* LChunkBuilder::DoAllocateBlockContext(
2590 HAllocateBlockContext* instr) {
2591 LOperand* context = UseFixed(instr->context(), cp);
2592 LOperand* function = UseRegisterAtStart(instr->function());
2593 LAllocateBlockContext* result =
2594 new(zone()) LAllocateBlockContext(context, function);
2595 return MarkAsCall(DefineFixed(result, cp), instr);
2598 } } // namespace v8::internal