1 // Copyright 2014 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.
9 #include "src/hydrogen-osr.h"
10 #include "src/lithium-inl.h"
11 #include "src/ppc/lithium-codegen-ppc.h"
16 #define DEFINE_COMPILE(type) \
17 void L##type::CompileToNative(LCodeGen* generator) { \
18 generator->Do##type(this); \
20 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
24 void LInstruction::VerifyCall() {
25 // Call instructions can use only fixed registers as temporaries and
26 // outputs because all registers are blocked by the calling convention.
27 // Inputs operands must use a fixed register or use-at-start policy or
28 // a non-register policy.
29 DCHECK(Output() == NULL || 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 DCHECK(operand->HasFixedPolicy() || operand->IsUsedAtStart());
35 for (TempIterator it(this); !it.Done(); it.Advance()) {
36 LUnallocated* operand = LUnallocated::cast(it.Current());
37 DCHECK(operand->HasFixedPolicy() || !operand->HasRegisterPolicy());
43 void LInstruction::PrintTo(StringStream* stream) {
44 stream->Add("%s ", this->Mnemonic());
46 PrintOutputOperandTo(stream);
50 if (HasEnvironment()) {
52 environment()->PrintTo(stream);
55 if (HasPointerMap()) {
57 pointer_map()->PrintTo(stream);
62 void LInstruction::PrintDataTo(StringStream* stream) {
64 for (int i = 0; i < InputCount(); i++) {
65 if (i > 0) stream->Add(" ");
66 if (InputAt(i) == NULL) {
69 InputAt(i)->PrintTo(stream);
75 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
76 if (HasResult()) result()->PrintTo(stream);
80 void LLabel::PrintDataTo(StringStream* stream) {
81 LGap::PrintDataTo(stream);
82 LLabel* rep = replacement();
84 stream->Add(" Dead block replaced with B%d", rep->block_id());
89 bool LGap::IsRedundant() const {
90 for (int i = 0; i < 4; i++) {
91 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
100 void LGap::PrintDataTo(StringStream* stream) {
101 for (int i = 0; i < 4; i++) {
103 if (parallel_moves_[i] != NULL) {
104 parallel_moves_[i]->PrintDataTo(stream);
111 const char* LArithmeticD::Mnemonic() const {
130 const char* LArithmeticT::Mnemonic() const {
163 bool LGoto::HasInterestingComment(LCodeGen* gen) const {
164 return !gen->IsNextEmittedBlock(block_id());
168 void LGoto::PrintDataTo(StringStream* stream) {
169 stream->Add("B%d", block_id());
173 void LBranch::PrintDataTo(StringStream* stream) {
174 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
175 value()->PrintTo(stream);
179 void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
181 left()->PrintTo(stream);
182 stream->Add(" %s ", Token::String(op()));
183 right()->PrintTo(stream);
184 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
188 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
189 stream->Add("if is_object(");
190 value()->PrintTo(stream);
191 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
195 void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
196 stream->Add("if is_string(");
197 value()->PrintTo(stream);
198 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
202 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
203 stream->Add("if is_smi(");
204 value()->PrintTo(stream);
205 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
209 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
210 stream->Add("if is_undetectable(");
211 value()->PrintTo(stream);
212 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
216 void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
217 stream->Add("if string_compare(");
218 left()->PrintTo(stream);
219 right()->PrintTo(stream);
220 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
224 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
225 stream->Add("if has_instance_type(");
226 value()->PrintTo(stream);
227 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
231 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
232 stream->Add("if has_cached_array_index(");
233 value()->PrintTo(stream);
234 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
238 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
239 stream->Add("if class_of_test(");
240 value()->PrintTo(stream);
241 stream->Add(", \"%o\") then B%d else B%d", *hydrogen()->class_name(),
242 true_block_id(), false_block_id());
246 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
247 stream->Add("if typeof ");
248 value()->PrintTo(stream);
249 stream->Add(" == \"%s\" then B%d else B%d",
250 hydrogen()->type_literal()->ToCString().get(), true_block_id(),
255 void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
257 function()->PrintTo(stream);
258 stream->Add(".code_entry = ");
259 code_object()->PrintTo(stream);
263 void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
265 base_object()->PrintTo(stream);
267 offset()->PrintTo(stream);
271 void LCallFunction::PrintDataTo(StringStream* stream) {
272 context()->PrintTo(stream);
274 function()->PrintTo(stream);
275 if (hydrogen()->HasVectorAndSlot()) {
276 stream->Add(" (type-feedback-vector ");
277 temp_vector()->PrintTo(stream);
279 temp_slot()->PrintTo(stream);
285 void LCallJSFunction::PrintDataTo(StringStream* stream) {
287 function()->PrintTo(stream);
288 stream->Add("#%d / ", arity());
292 void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
293 for (int i = 0; i < InputCount(); i++) {
294 InputAt(i)->PrintTo(stream);
297 stream->Add("#%d / ", arity());
301 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
302 context()->PrintTo(stream);
303 stream->Add("[%d]", slot_index());
307 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
308 context()->PrintTo(stream);
309 stream->Add("[%d] <- ", slot_index());
310 value()->PrintTo(stream);
314 void LInvokeFunction::PrintDataTo(StringStream* stream) {
316 function()->PrintTo(stream);
317 stream->Add(" #%d / ", arity());
321 void LCallNew::PrintDataTo(StringStream* stream) {
323 constructor()->PrintTo(stream);
324 stream->Add(" #%d / ", arity());
328 void LCallNewArray::PrintDataTo(StringStream* stream) {
330 constructor()->PrintTo(stream);
331 stream->Add(" #%d / ", arity());
332 ElementsKind kind = hydrogen()->elements_kind();
333 stream->Add(" (%s) ", ElementsKindToString(kind));
337 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
338 arguments()->PrintTo(stream);
339 stream->Add(" length ");
340 length()->PrintTo(stream);
341 stream->Add(" index ");
342 index()->PrintTo(stream);
346 void LStoreNamedField::PrintDataTo(StringStream* stream) {
347 object()->PrintTo(stream);
348 std::ostringstream os;
349 os << hydrogen()->access() << " <- ";
350 stream->Add(os.str().c_str());
351 value()->PrintTo(stream);
355 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
356 object()->PrintTo(stream);
358 stream->Add(String::cast(*name())->ToCString().get());
360 value()->PrintTo(stream);
364 void LLoadKeyed::PrintDataTo(StringStream* stream) {
365 elements()->PrintTo(stream);
367 key()->PrintTo(stream);
368 if (hydrogen()->IsDehoisted()) {
369 stream->Add(" + %d]", base_offset());
376 void LStoreKeyed::PrintDataTo(StringStream* stream) {
377 elements()->PrintTo(stream);
379 key()->PrintTo(stream);
380 if (hydrogen()->IsDehoisted()) {
381 stream->Add(" + %d] <-", base_offset());
383 stream->Add("] <- ");
386 if (value() == NULL) {
387 DCHECK(hydrogen()->IsConstantHoleStore() &&
388 hydrogen()->value()->representation().IsDouble());
389 stream->Add("<the hole(nan)>");
391 value()->PrintTo(stream);
396 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
397 object()->PrintTo(stream);
399 key()->PrintTo(stream);
400 stream->Add("] <- ");
401 value()->PrintTo(stream);
405 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
406 object()->PrintTo(stream);
407 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
411 int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
412 // Skip a slot if for a double-width slot.
413 if (kind == DOUBLE_REGISTERS) spill_slot_count_++;
414 return spill_slot_count_++;
418 LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
419 int index = GetNextSpillIndex(kind);
420 if (kind == DOUBLE_REGISTERS) {
421 return LDoubleStackSlot::Create(index, zone());
423 DCHECK(kind == GENERAL_REGISTERS);
424 return LStackSlot::Create(index, zone());
429 LPlatformChunk* LChunkBuilder::Build() {
431 chunk_ = new (zone()) LPlatformChunk(info(), graph());
432 LPhase phase("L_Building chunk", chunk_);
435 // If compiling for OSR, reserve space for the unoptimized frame,
436 // which will be subsumed into this frame.
437 if (graph()->has_osr()) {
438 for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
439 chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
443 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
444 for (int i = 0; i < blocks->length(); i++) {
445 HBasicBlock* next = NULL;
446 if (i < blocks->length() - 1) next = blocks->at(i + 1);
447 DoBasicBlock(blocks->at(i), next);
448 if (is_aborted()) return NULL;
455 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
456 return new (zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
457 Register::ToAllocationIndex(reg));
461 LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
462 return new (zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
463 DoubleRegister::ToAllocationIndex(reg));
467 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
468 return Use(value, ToUnallocated(fixed_register));
472 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
473 return Use(value, ToUnallocated(reg));
477 LOperand* LChunkBuilder::UseRegister(HValue* value) {
479 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
483 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
484 return Use(value, new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
485 LUnallocated::USED_AT_START));
489 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
490 return Use(value, new (zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
494 LOperand* LChunkBuilder::Use(HValue* value) {
495 return Use(value, new (zone()) LUnallocated(LUnallocated::NONE));
499 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
500 return Use(value, new (zone())
501 LUnallocated(LUnallocated::NONE, LUnallocated::USED_AT_START));
505 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
506 return value->IsConstant()
507 ? chunk_->DefineConstantOperand(HConstant::cast(value))
512 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
513 return value->IsConstant()
514 ? chunk_->DefineConstantOperand(HConstant::cast(value))
519 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
520 return value->IsConstant()
521 ? chunk_->DefineConstantOperand(HConstant::cast(value))
522 : UseRegister(value);
526 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
527 return value->IsConstant()
528 ? chunk_->DefineConstantOperand(HConstant::cast(value))
529 : UseRegisterAtStart(value);
533 LOperand* LChunkBuilder::UseConstant(HValue* value) {
534 return chunk_->DefineConstantOperand(HConstant::cast(value));
538 LOperand* LChunkBuilder::UseAny(HValue* value) {
539 return value->IsConstant()
540 ? chunk_->DefineConstantOperand(HConstant::cast(value))
541 : Use(value, new (zone()) LUnallocated(LUnallocated::ANY));
545 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
546 if (value->EmitAtUses()) {
547 HInstruction* instr = HInstruction::cast(value);
548 VisitInstruction(instr);
550 operand->set_virtual_register(value->id());
555 LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
556 LUnallocated* result) {
557 result->set_virtual_register(current_instruction_->id());
558 instr->set_result(result);
563 LInstruction* LChunkBuilder::DefineAsRegister(
564 LTemplateResultInstruction<1>* instr) {
566 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
570 LInstruction* LChunkBuilder::DefineAsSpilled(
571 LTemplateResultInstruction<1>* instr, int index) {
573 new (zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
577 LInstruction* LChunkBuilder::DefineSameAsFirst(
578 LTemplateResultInstruction<1>* instr) {
580 new (zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
584 LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
586 return Define(instr, ToUnallocated(reg));
590 LInstruction* LChunkBuilder::DefineFixedDouble(
591 LTemplateResultInstruction<1>* instr, DoubleRegister reg) {
592 return Define(instr, ToUnallocated(reg));
596 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
597 HEnvironment* hydrogen_env = current_block_->last_environment();
598 int argument_index_accumulator = 0;
599 ZoneList<HValue*> objects_to_materialize(0, zone());
600 instr->set_environment(CreateEnvironment(
601 hydrogen_env, &argument_index_accumulator, &objects_to_materialize));
606 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
607 HInstruction* hinstr,
608 CanDeoptimize can_deoptimize) {
609 info()->MarkAsNonDeferredCalling();
614 instr = AssignPointerMap(instr);
616 // If instruction does not have side-effects lazy deoptimization
617 // after the call will try to deoptimize to the point before the call.
618 // Thus we still need to attach environment to this call even if
619 // call sequence can not deoptimize eagerly.
620 bool needs_environment = (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
621 !hinstr->HasObservableSideEffects();
622 if (needs_environment && !instr->HasEnvironment()) {
623 instr = AssignEnvironment(instr);
624 // We can't really figure out if the environment is needed or not.
625 instr->environment()->set_has_been_used();
632 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
633 DCHECK(!instr->HasPointerMap());
634 instr->set_pointer_map(new (zone()) LPointerMap(zone()));
639 LUnallocated* LChunkBuilder::TempRegister() {
640 LUnallocated* operand =
641 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
642 int vreg = allocator_->GetVirtualRegister();
643 if (!allocator_->AllocationOk()) {
644 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
647 operand->set_virtual_register(vreg);
652 LUnallocated* LChunkBuilder::TempDoubleRegister() {
653 LUnallocated* operand =
654 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_DOUBLE_REGISTER);
655 int vreg = allocator_->GetVirtualRegister();
656 if (!allocator_->AllocationOk()) {
657 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
660 operand->set_virtual_register(vreg);
665 LOperand* LChunkBuilder::FixedTemp(Register reg) {
666 LUnallocated* operand = ToUnallocated(reg);
667 DCHECK(operand->HasFixedPolicy());
672 LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
673 LUnallocated* operand = ToUnallocated(reg);
674 DCHECK(operand->HasFixedPolicy());
679 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
680 return new (zone()) LLabel(instr->block());
684 LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
685 return DefineAsRegister(new (zone()) LDummyUse(UseAny(instr->value())));
689 LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
695 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
696 return AssignEnvironment(new (zone()) LDeoptimize);
700 LInstruction* LChunkBuilder::DoShift(Token::Value op,
701 HBitwiseBinaryOperation* instr) {
702 if (instr->representation().IsSmiOrInteger32()) {
703 DCHECK(instr->left()->representation().Equals(instr->representation()));
704 DCHECK(instr->right()->representation().Equals(instr->representation()));
705 LOperand* left = UseRegisterAtStart(instr->left());
707 HValue* right_value = instr->right();
708 LOperand* right = NULL;
709 int constant_value = 0;
710 bool does_deopt = false;
711 if (right_value->IsConstant()) {
712 HConstant* constant = HConstant::cast(right_value);
713 right = chunk_->DefineConstantOperand(constant);
714 constant_value = constant->Integer32Value() & 0x1f;
715 // Left shifts can deoptimize if we shift by > 0 and the result cannot be
717 if (instr->representation().IsSmi() && constant_value > 0) {
718 does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
721 right = UseRegisterAtStart(right_value);
724 // Shift operations can only deoptimize if we do a logical shift
725 // by 0 and the result cannot be truncated to int32.
726 if (op == Token::SHR && constant_value == 0) {
727 does_deopt = !instr->CheckFlag(HInstruction::kUint32);
730 LInstruction* result =
731 DefineAsRegister(new (zone()) LShiftI(op, left, right, does_deopt));
732 return does_deopt ? AssignEnvironment(result) : result;
734 return DoArithmeticT(op, instr);
739 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
740 HArithmeticBinaryOperation* instr) {
741 DCHECK(instr->representation().IsDouble());
742 DCHECK(instr->left()->representation().IsDouble());
743 DCHECK(instr->right()->representation().IsDouble());
744 if (op == Token::MOD) {
745 LOperand* left = UseFixedDouble(instr->left(), d1);
746 LOperand* right = UseFixedDouble(instr->right(), d2);
747 LArithmeticD* result = new (zone()) LArithmeticD(op, left, right);
748 // We call a C function for double modulo. It can't trigger a GC. We need
749 // to use fixed result register for the call.
750 // TODO(fschneider): Allow any register as input registers.
751 return MarkAsCall(DefineFixedDouble(result, d1), instr);
753 LOperand* left = UseRegisterAtStart(instr->left());
754 LOperand* right = UseRegisterAtStart(instr->right());
755 LArithmeticD* result = new (zone()) LArithmeticD(op, left, right);
756 return DefineAsRegister(result);
761 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
762 HBinaryOperation* instr) {
763 HValue* left = instr->left();
764 HValue* right = instr->right();
765 DCHECK(left->representation().IsTagged());
766 DCHECK(right->representation().IsTagged());
767 LOperand* context = UseFixed(instr->context(), cp);
768 LOperand* left_operand = UseFixed(left, r4);
769 LOperand* right_operand = UseFixed(right, r3);
770 LArithmeticT* result =
771 new (zone()) LArithmeticT(op, context, left_operand, right_operand);
772 return MarkAsCall(DefineFixed(result, r3), instr);
776 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
777 DCHECK(is_building());
778 current_block_ = block;
779 next_block_ = next_block;
780 if (block->IsStartBlock()) {
781 block->UpdateEnvironment(graph_->start_environment());
783 } else if (block->predecessors()->length() == 1) {
784 // We have a single predecessor => copy environment and outgoing
785 // argument count from the predecessor.
786 DCHECK(block->phis()->length() == 0);
787 HBasicBlock* pred = block->predecessors()->at(0);
788 HEnvironment* last_environment = pred->last_environment();
789 DCHECK(last_environment != NULL);
790 // Only copy the environment, if it is later used again.
791 if (pred->end()->SecondSuccessor() == NULL) {
792 DCHECK(pred->end()->FirstSuccessor() == block);
794 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
795 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
796 last_environment = last_environment->Copy();
799 block->UpdateEnvironment(last_environment);
800 DCHECK(pred->argument_count() >= 0);
801 argument_count_ = pred->argument_count();
803 // We are at a state join => process phis.
804 HBasicBlock* pred = block->predecessors()->at(0);
805 // No need to copy the environment, it cannot be used later.
806 HEnvironment* last_environment = pred->last_environment();
807 for (int i = 0; i < block->phis()->length(); ++i) {
808 HPhi* phi = block->phis()->at(i);
809 if (phi->HasMergedIndex()) {
810 last_environment->SetValueAt(phi->merged_index(), phi);
813 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
814 if (block->deleted_phis()->at(i) < last_environment->length()) {
815 last_environment->SetValueAt(block->deleted_phis()->at(i),
816 graph_->GetConstantUndefined());
819 block->UpdateEnvironment(last_environment);
820 // Pick up the outgoing argument count of one of the predecessors.
821 argument_count_ = pred->argument_count();
823 HInstruction* current = block->first();
824 int start = chunk_->instructions()->length();
825 while (current != NULL && !is_aborted()) {
826 // Code for constants in registers is generated lazily.
827 if (!current->EmitAtUses()) {
828 VisitInstruction(current);
830 current = current->next();
832 int end = chunk_->instructions()->length() - 1;
834 block->set_first_instruction_index(start);
835 block->set_last_instruction_index(end);
837 block->set_argument_count(argument_count_);
839 current_block_ = NULL;
843 void LChunkBuilder::VisitInstruction(HInstruction* current) {
844 HInstruction* old_current = current_instruction_;
845 current_instruction_ = current;
847 LInstruction* instr = NULL;
848 if (current->CanReplaceWithDummyUses()) {
849 if (current->OperandCount() == 0) {
850 instr = DefineAsRegister(new (zone()) LDummy());
852 DCHECK(!current->OperandAt(0)->IsControlInstruction());
853 instr = DefineAsRegister(new (zone())
854 LDummyUse(UseAny(current->OperandAt(0))));
856 for (int i = 1; i < current->OperandCount(); ++i) {
857 if (current->OperandAt(i)->IsControlInstruction()) continue;
858 LInstruction* dummy =
859 new (zone()) LDummyUse(UseAny(current->OperandAt(i)));
860 dummy->set_hydrogen_value(current);
861 chunk_->AddInstruction(dummy, current_block_);
864 HBasicBlock* successor;
865 if (current->IsControlInstruction() &&
866 HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
868 instr = new (zone()) LGoto(successor);
870 instr = current->CompileToLithium(this);
874 argument_count_ += current->argument_delta();
875 DCHECK(argument_count_ >= 0);
878 AddInstruction(instr, current);
881 current_instruction_ = old_current;
885 void LChunkBuilder::AddInstruction(LInstruction* instr,
886 HInstruction* hydrogen_val) {
887 // Associate the hydrogen instruction first, since we may need it for
888 // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
889 instr->set_hydrogen_value(hydrogen_val);
892 // Make sure that the lithium instruction has either no fixed register
893 // constraints in temps or the result OR no uses that are only used at
894 // start. If this invariant doesn't hold, the register allocator can decide
895 // to insert a split of a range immediately before the instruction due to an
896 // already allocated register needing to be used for the instruction's fixed
897 // register constraint. In this case, The register allocator won't see an
898 // interference between the split child and the use-at-start (it would if
899 // the it was just a plain use), so it is free to move the split child into
900 // the same register that is used for the use-at-start.
901 // See https://code.google.com/p/chromium/issues/detail?id=201590
902 if (!(instr->ClobbersRegisters() &&
903 instr->ClobbersDoubleRegisters(isolate()))) {
905 int used_at_start = 0;
906 for (UseIterator it(instr); !it.Done(); it.Advance()) {
907 LUnallocated* operand = LUnallocated::cast(it.Current());
908 if (operand->IsUsedAtStart()) ++used_at_start;
910 if (instr->Output() != NULL) {
911 if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
913 for (TempIterator it(instr); !it.Done(); it.Advance()) {
914 LUnallocated* operand = LUnallocated::cast(it.Current());
915 if (operand->HasFixedPolicy()) ++fixed;
917 DCHECK(fixed == 0 || used_at_start == 0);
921 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
922 instr = AssignPointerMap(instr);
924 if (FLAG_stress_environments && !instr->HasEnvironment()) {
925 instr = AssignEnvironment(instr);
927 chunk_->AddInstruction(instr, current_block_);
929 if (instr->IsCall()) {
930 HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
931 LInstruction* instruction_needing_environment = NULL;
932 if (hydrogen_val->HasObservableSideEffects()) {
933 HSimulate* sim = HSimulate::cast(hydrogen_val->next());
934 instruction_needing_environment = instr;
935 sim->ReplayEnvironment(current_block_->last_environment());
936 hydrogen_value_for_lazy_bailout = sim;
938 LInstruction* bailout = AssignEnvironment(new (zone()) LLazyBailout());
939 bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
940 chunk_->AddInstruction(bailout, current_block_);
941 if (instruction_needing_environment != NULL) {
942 // Store the lazy deopt environment with the instruction if needed.
943 // Right now it is only used for LInstanceOfKnownGlobal.
944 instruction_needing_environment->SetDeferredLazyDeoptimizationEnvironment(
945 bailout->environment());
951 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
952 return new (zone()) LGoto(instr->FirstSuccessor());
956 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
957 HValue* value = instr->value();
958 Representation r = value->representation();
959 HType type = value->type();
960 ToBooleanStub::Types expected = instr->expected_input_types();
961 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
963 bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
964 type.IsJSArray() || type.IsHeapNumber() || type.IsString();
965 LInstruction* branch = new (zone()) LBranch(UseRegister(value));
967 ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
968 !expected.IsGeneric())) {
969 branch = AssignEnvironment(branch);
975 LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
976 return new (zone()) LDebugBreak();
980 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
981 DCHECK(instr->value()->representation().IsTagged());
982 LOperand* value = UseRegisterAtStart(instr->value());
983 LOperand* temp = TempRegister();
984 return new (zone()) LCmpMapAndBranch(value, temp);
988 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* instr) {
989 info()->MarkAsRequiresFrame();
990 LOperand* value = UseRegister(instr->value());
991 return DefineAsRegister(new (zone()) LArgumentsLength(value));
995 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
996 info()->MarkAsRequiresFrame();
997 return DefineAsRegister(new (zone()) LArgumentsElements);
1001 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1002 LOperand* context = UseFixed(instr->context(), cp);
1003 LInstanceOf* result = new (zone()) LInstanceOf(
1004 context, UseFixed(instr->left(), r3), UseFixed(instr->right(), r4));
1005 return MarkAsCall(DefineFixed(result, r3), instr);
1009 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1010 HInstanceOfKnownGlobal* instr) {
1011 LInstanceOfKnownGlobal* result = new (zone())
1012 LInstanceOfKnownGlobal(UseFixed(instr->context(), cp),
1013 UseFixed(instr->left(), r3), FixedTemp(r7));
1014 return MarkAsCall(DefineFixed(result, r3), instr);
1018 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1019 LOperand* receiver = UseRegisterAtStart(instr->receiver());
1020 LOperand* function = UseRegisterAtStart(instr->function());
1021 LWrapReceiver* result = new (zone()) LWrapReceiver(receiver, function);
1022 return AssignEnvironment(DefineAsRegister(result));
1026 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1027 LOperand* function = UseFixed(instr->function(), r4);
1028 LOperand* receiver = UseFixed(instr->receiver(), r3);
1029 LOperand* length = UseFixed(instr->length(), r5);
1030 LOperand* elements = UseFixed(instr->elements(), r6);
1031 LApplyArguments* result =
1032 new (zone()) LApplyArguments(function, receiver, length, elements);
1033 return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
1037 LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1038 int argc = instr->OperandCount();
1039 for (int i = 0; i < argc; ++i) {
1040 LOperand* argument = Use(instr->argument(i));
1041 AddInstruction(new (zone()) LPushArgument(argument), instr);
1047 LInstruction* LChunkBuilder::DoStoreCodeEntry(
1048 HStoreCodeEntry* store_code_entry) {
1049 LOperand* function = UseRegister(store_code_entry->function());
1050 LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1051 return new (zone()) LStoreCodeEntry(function, code_object);
1055 LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1056 HInnerAllocatedObject* instr) {
1057 LOperand* base_object = UseRegisterAtStart(instr->base_object());
1058 LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1059 return DefineAsRegister(new (zone())
1060 LInnerAllocatedObject(base_object, offset));
1064 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1065 return instr->HasNoUses() ? NULL
1066 : DefineAsRegister(new (zone()) LThisFunction);
1070 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1071 if (instr->HasNoUses()) return NULL;
1073 if (info()->IsStub()) {
1074 return DefineFixed(new (zone()) LContext, cp);
1077 return DefineAsRegister(new (zone()) LContext);
1081 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1082 LOperand* context = UseFixed(instr->context(), cp);
1083 return MarkAsCall(new (zone()) LDeclareGlobals(context), instr);
1087 LInstruction* LChunkBuilder::DoCallJSFunction(HCallJSFunction* instr) {
1088 LOperand* function = UseFixed(instr->function(), r4);
1090 LCallJSFunction* result = new (zone()) LCallJSFunction(function);
1092 return MarkAsCall(DefineFixed(result, r3), instr);
1096 LInstruction* LChunkBuilder::DoCallWithDescriptor(HCallWithDescriptor* instr) {
1097 CallInterfaceDescriptor descriptor = instr->descriptor();
1099 LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1100 ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1101 ops.Add(target, zone());
1102 for (int i = 1; i < instr->OperandCount(); i++) {
1104 UseFixed(instr->OperandAt(i), descriptor.GetParameterRegister(i - 1));
1105 ops.Add(op, zone());
1108 LCallWithDescriptor* result =
1109 new (zone()) LCallWithDescriptor(descriptor, ops, zone());
1110 return MarkAsCall(DefineFixed(result, r3), instr);
1114 LInstruction* LChunkBuilder::DoTailCallThroughMegamorphicCache(
1115 HTailCallThroughMegamorphicCache* instr) {
1116 LOperand* context = UseFixed(instr->context(), cp);
1117 LOperand* receiver_register =
1118 UseFixed(instr->receiver(), LoadDescriptor::ReceiverRegister());
1119 LOperand* name_register =
1120 UseFixed(instr->name(), LoadDescriptor::NameRegister());
1121 LOperand* slot = NULL;
1122 LOperand* vector = NULL;
1123 if (FLAG_vector_ics) {
1124 slot = UseFixed(instr->slot(), VectorLoadICDescriptor::SlotRegister());
1126 UseFixed(instr->vector(), VectorLoadICDescriptor::VectorRegister());
1129 // Not marked as call. It can't deoptimize, and it never returns.
1130 return new (zone()) LTailCallThroughMegamorphicCache(
1131 context, receiver_register, name_register, slot, vector);
1135 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1136 LOperand* context = UseFixed(instr->context(), cp);
1137 LOperand* function = UseFixed(instr->function(), r4);
1138 LInvokeFunction* result = new (zone()) LInvokeFunction(context, function);
1139 return MarkAsCall(DefineFixed(result, r3), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1143 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1144 switch (instr->op()) {
1146 return DoMathFloor(instr);
1148 return DoMathRound(instr);
1150 return DoMathFround(instr);
1152 return DoMathAbs(instr);
1154 return DoMathLog(instr);
1156 return DoMathExp(instr);
1158 return DoMathSqrt(instr);
1160 return DoMathPowHalf(instr);
1162 return DoMathClz32(instr);
1170 LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1171 LOperand* input = UseRegister(instr->value());
1172 LMathFloor* result = new (zone()) LMathFloor(input);
1173 return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1177 LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1178 LOperand* input = UseRegister(instr->value());
1179 LOperand* temp = TempDoubleRegister();
1180 LMathRound* result = new (zone()) LMathRound(input, temp);
1181 return AssignEnvironment(DefineAsRegister(result));
1185 LInstruction* LChunkBuilder::DoMathFround(HUnaryMathOperation* instr) {
1186 LOperand* input = UseRegister(instr->value());
1187 LMathFround* result = new (zone()) LMathFround(input);
1188 return DefineAsRegister(result);
1192 LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1193 Representation r = instr->value()->representation();
1194 LOperand* context = (r.IsDouble() || r.IsSmiOrInteger32())
1196 : UseFixed(instr->context(), cp);
1197 LOperand* input = UseRegister(instr->value());
1198 LInstruction* result =
1199 DefineAsRegister(new (zone()) LMathAbs(context, input));
1200 if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1201 if (!r.IsDouble()) result = AssignEnvironment(result);
1206 LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1207 DCHECK(instr->representation().IsDouble());
1208 DCHECK(instr->value()->representation().IsDouble());
1209 LOperand* input = UseFixedDouble(instr->value(), d1);
1210 return MarkAsCall(DefineFixedDouble(new (zone()) LMathLog(input), d1), instr);
1214 LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1215 LOperand* input = UseRegisterAtStart(instr->value());
1216 LMathClz32* result = new (zone()) LMathClz32(input);
1217 return DefineAsRegister(result);
1221 LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1222 DCHECK(instr->representation().IsDouble());
1223 DCHECK(instr->value()->representation().IsDouble());
1224 LOperand* input = UseRegister(instr->value());
1225 LOperand* temp1 = TempRegister();
1226 LOperand* temp2 = TempRegister();
1227 LOperand* double_temp = TempDoubleRegister();
1228 LMathExp* result = new (zone()) LMathExp(input, double_temp, temp1, temp2);
1229 return DefineAsRegister(result);
1233 LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1234 LOperand* input = UseRegisterAtStart(instr->value());
1235 LMathSqrt* result = new (zone()) LMathSqrt(input);
1236 return DefineAsRegister(result);
1240 LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1241 LOperand* input = UseRegisterAtStart(instr->value());
1242 LMathPowHalf* result = new (zone()) LMathPowHalf(input);
1243 return DefineAsRegister(result);
1247 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1248 LOperand* context = UseFixed(instr->context(), cp);
1249 LOperand* constructor = UseFixed(instr->constructor(), r4);
1250 LCallNew* result = new (zone()) LCallNew(context, constructor);
1251 return MarkAsCall(DefineFixed(result, r3), instr);
1255 LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1256 LOperand* context = UseFixed(instr->context(), cp);
1257 LOperand* constructor = UseFixed(instr->constructor(), r4);
1258 LCallNewArray* result = new (zone()) LCallNewArray(context, constructor);
1259 return MarkAsCall(DefineFixed(result, r3), instr);
1263 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1264 LOperand* context = UseFixed(instr->context(), cp);
1265 LOperand* function = UseFixed(instr->function(), r4);
1266 LOperand* slot = NULL;
1267 LOperand* vector = NULL;
1268 if (instr->HasVectorAndSlot()) {
1269 slot = FixedTemp(r6);
1270 vector = FixedTemp(r5);
1273 LCallFunction* call =
1274 new (zone()) LCallFunction(context, function, slot, vector);
1275 return MarkAsCall(DefineFixed(call, r3), instr);
1279 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1280 LOperand* context = UseFixed(instr->context(), cp);
1281 return MarkAsCall(DefineFixed(new (zone()) LCallRuntime(context), r3), instr);
1285 LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1286 return DoShift(Token::ROR, instr);
1290 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1291 return DoShift(Token::SHR, instr);
1295 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1296 return DoShift(Token::SAR, instr);
1300 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1301 return DoShift(Token::SHL, instr);
1305 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1306 if (instr->representation().IsSmiOrInteger32()) {
1307 DCHECK(instr->left()->representation().Equals(instr->representation()));
1308 DCHECK(instr->right()->representation().Equals(instr->representation()));
1309 DCHECK(instr->CheckFlag(HValue::kTruncatingToInt32));
1311 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1312 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1313 return DefineAsRegister(new (zone()) LBitI(left, right));
1315 return DoArithmeticT(instr->op(), instr);
1320 LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1321 DCHECK(instr->representation().IsSmiOrInteger32());
1322 DCHECK(instr->left()->representation().Equals(instr->representation()));
1323 DCHECK(instr->right()->representation().Equals(instr->representation()));
1324 LOperand* dividend = UseRegister(instr->left());
1325 int32_t divisor = instr->right()->GetInteger32Constant();
1326 LInstruction* result =
1327 DefineAsRegister(new (zone()) LDivByPowerOf2I(dividend, divisor));
1328 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1329 (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1330 (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1331 divisor != 1 && divisor != -1)) {
1332 result = AssignEnvironment(result);
1338 LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1339 DCHECK(instr->representation().IsInteger32());
1340 DCHECK(instr->left()->representation().Equals(instr->representation()));
1341 DCHECK(instr->right()->representation().Equals(instr->representation()));
1342 LOperand* dividend = UseRegister(instr->left());
1343 int32_t divisor = instr->right()->GetInteger32Constant();
1344 LInstruction* result =
1345 DefineAsRegister(new (zone()) LDivByConstI(dividend, divisor));
1347 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1348 !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1349 result = AssignEnvironment(result);
1355 LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1356 DCHECK(instr->representation().IsSmiOrInteger32());
1357 DCHECK(instr->left()->representation().Equals(instr->representation()));
1358 DCHECK(instr->right()->representation().Equals(instr->representation()));
1359 LOperand* dividend = UseRegister(instr->left());
1360 LOperand* divisor = UseRegister(instr->right());
1361 LInstruction* result =
1362 DefineAsRegister(new (zone()) LDivI(dividend, divisor));
1363 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1364 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1365 (instr->CheckFlag(HValue::kCanOverflow) &&
1366 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) ||
1367 (!instr->IsMathFloorOfDiv() &&
1368 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1369 result = AssignEnvironment(result);
1375 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1376 if (instr->representation().IsSmiOrInteger32()) {
1377 if (instr->RightIsPowerOf2()) {
1378 return DoDivByPowerOf2I(instr);
1379 } else if (instr->right()->IsConstant()) {
1380 return DoDivByConstI(instr);
1382 return DoDivI(instr);
1384 } else if (instr->representation().IsDouble()) {
1385 return DoArithmeticD(Token::DIV, instr);
1387 return DoArithmeticT(Token::DIV, instr);
1392 LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1393 LOperand* dividend = UseRegisterAtStart(instr->left());
1394 int32_t divisor = instr->right()->GetInteger32Constant();
1395 LInstruction* result =
1396 DefineAsRegister(new (zone()) LFlooringDivByPowerOf2I(dividend, divisor));
1397 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1398 (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1399 result = AssignEnvironment(result);
1405 LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1406 DCHECK(instr->representation().IsInteger32());
1407 DCHECK(instr->left()->representation().Equals(instr->representation()));
1408 DCHECK(instr->right()->representation().Equals(instr->representation()));
1409 LOperand* dividend = UseRegister(instr->left());
1410 int32_t divisor = instr->right()->GetInteger32Constant();
1412 ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1413 (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive)))
1416 LInstruction* result = DefineAsRegister(
1417 new (zone()) LFlooringDivByConstI(dividend, divisor, temp));
1419 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1420 result = AssignEnvironment(result);
1426 LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
1427 DCHECK(instr->representation().IsSmiOrInteger32());
1428 DCHECK(instr->left()->representation().Equals(instr->representation()));
1429 DCHECK(instr->right()->representation().Equals(instr->representation()));
1430 LOperand* dividend = UseRegister(instr->left());
1431 LOperand* divisor = UseRegister(instr->right());
1432 LInstruction* result =
1433 DefineAsRegister(new (zone()) LFlooringDivI(dividend, divisor));
1434 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1435 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1436 (instr->CheckFlag(HValue::kCanOverflow) &&
1437 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1438 result = AssignEnvironment(result);
1444 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1445 if (instr->RightIsPowerOf2()) {
1446 return DoFlooringDivByPowerOf2I(instr);
1447 } else if (instr->right()->IsConstant()) {
1448 return DoFlooringDivByConstI(instr);
1450 return DoFlooringDivI(instr);
1455 LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
1456 DCHECK(instr->representation().IsSmiOrInteger32());
1457 DCHECK(instr->left()->representation().Equals(instr->representation()));
1458 DCHECK(instr->right()->representation().Equals(instr->representation()));
1459 LOperand* dividend = UseRegisterAtStart(instr->left());
1460 int32_t divisor = instr->right()->GetInteger32Constant();
1461 LInstruction* result =
1462 DefineSameAsFirst(new (zone()) LModByPowerOf2I(dividend, divisor));
1463 if (instr->CheckFlag(HValue::kLeftCanBeNegative) &&
1464 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1465 result = AssignEnvironment(result);
1471 LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
1472 DCHECK(instr->representation().IsSmiOrInteger32());
1473 DCHECK(instr->left()->representation().Equals(instr->representation()));
1474 DCHECK(instr->right()->representation().Equals(instr->representation()));
1475 LOperand* dividend = UseRegister(instr->left());
1476 int32_t divisor = instr->right()->GetInteger32Constant();
1477 LInstruction* result =
1478 DefineAsRegister(new (zone()) LModByConstI(dividend, divisor));
1479 if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1480 result = AssignEnvironment(result);
1486 LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1487 DCHECK(instr->representation().IsSmiOrInteger32());
1488 DCHECK(instr->left()->representation().Equals(instr->representation()));
1489 DCHECK(instr->right()->representation().Equals(instr->representation()));
1490 LOperand* dividend = UseRegister(instr->left());
1491 LOperand* divisor = UseRegister(instr->right());
1492 LInstruction* result =
1493 DefineAsRegister(new (zone()) LModI(dividend, divisor));
1494 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1495 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1496 result = AssignEnvironment(result);
1502 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1503 if (instr->representation().IsSmiOrInteger32()) {
1504 if (instr->RightIsPowerOf2()) {
1505 return DoModByPowerOf2I(instr);
1506 } else if (instr->right()->IsConstant()) {
1507 return DoModByConstI(instr);
1509 return DoModI(instr);
1511 } else if (instr->representation().IsDouble()) {
1512 return DoArithmeticD(Token::MOD, instr);
1514 return DoArithmeticT(Token::MOD, instr);
1519 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1520 if (instr->representation().IsSmiOrInteger32()) {
1521 DCHECK(instr->left()->representation().Equals(instr->representation()));
1522 DCHECK(instr->right()->representation().Equals(instr->representation()));
1523 HValue* left = instr->BetterLeftOperand();
1524 HValue* right = instr->BetterRightOperand();
1527 bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1528 bool bailout_on_minus_zero = instr->CheckFlag(HValue::kBailoutOnMinusZero);
1530 int32_t constant_value = 0;
1531 if (right->IsConstant()) {
1532 HConstant* constant = HConstant::cast(right);
1533 constant_value = constant->Integer32Value();
1534 // Constants -1, 0 and 1 can be optimized if the result can overflow.
1535 // For other constants, it can be optimized only without overflow.
1536 if (!can_overflow || ((constant_value >= -1) && (constant_value <= 1))) {
1537 left_op = UseRegisterAtStart(left);
1538 right_op = UseConstant(right);
1540 if (bailout_on_minus_zero) {
1541 left_op = UseRegister(left);
1543 left_op = UseRegisterAtStart(left);
1545 right_op = UseRegister(right);
1548 if (bailout_on_minus_zero) {
1549 left_op = UseRegister(left);
1551 left_op = UseRegisterAtStart(left);
1553 right_op = UseRegister(right);
1555 LMulI* mul = new (zone()) LMulI(left_op, right_op);
1556 if (right_op->IsConstantOperand()
1557 ? ((can_overflow && constant_value == -1) ||
1558 (bailout_on_minus_zero && constant_value <= 0))
1559 : (can_overflow || bailout_on_minus_zero)) {
1560 AssignEnvironment(mul);
1562 return DefineAsRegister(mul);
1564 } else if (instr->representation().IsDouble()) {
1565 return DoArithmeticD(Token::MUL, instr);
1567 return DoArithmeticT(Token::MUL, instr);
1572 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1573 if (instr->representation().IsSmiOrInteger32()) {
1574 DCHECK(instr->left()->representation().Equals(instr->representation()));
1575 DCHECK(instr->right()->representation().Equals(instr->representation()));
1577 if (instr->left()->IsConstant() &&
1578 !instr->CheckFlag(HValue::kCanOverflow)) {
1579 // If lhs is constant, do reverse subtraction instead.
1580 return DoRSub(instr);
1583 LOperand* left = UseRegisterAtStart(instr->left());
1584 LOperand* right = UseOrConstantAtStart(instr->right());
1585 LSubI* sub = new (zone()) LSubI(left, right);
1586 LInstruction* result = DefineAsRegister(sub);
1587 if (instr->CheckFlag(HValue::kCanOverflow)) {
1588 result = AssignEnvironment(result);
1591 } else if (instr->representation().IsDouble()) {
1592 return DoArithmeticD(Token::SUB, instr);
1594 return DoArithmeticT(Token::SUB, instr);
1599 LInstruction* LChunkBuilder::DoRSub(HSub* instr) {
1600 DCHECK(instr->representation().IsSmiOrInteger32());
1601 DCHECK(instr->left()->representation().Equals(instr->representation()));
1602 DCHECK(instr->right()->representation().Equals(instr->representation()));
1603 DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1605 // Note: The lhs of the subtraction becomes the rhs of the
1606 // reverse-subtraction.
1607 LOperand* left = UseRegisterAtStart(instr->right());
1608 LOperand* right = UseOrConstantAtStart(instr->left());
1609 LRSubI* rsb = new (zone()) LRSubI(left, right);
1610 LInstruction* result = DefineAsRegister(rsb);
1615 LInstruction* LChunkBuilder::DoMultiplyAdd(HMul* mul, HValue* addend) {
1616 LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1617 LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1618 LOperand* addend_op = UseRegisterAtStart(addend);
1619 return DefineSameAsFirst(
1620 new (zone()) LMultiplyAddD(addend_op, multiplier_op, multiplicand_op));
1624 LInstruction* LChunkBuilder::DoMultiplySub(HValue* minuend, HMul* mul) {
1625 LOperand* minuend_op = UseRegisterAtStart(minuend);
1626 LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1627 LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1629 return DefineSameAsFirst(
1630 new (zone()) LMultiplySubD(minuend_op, multiplier_op, multiplicand_op));
1634 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1635 if (instr->representation().IsSmiOrInteger32()) {
1636 DCHECK(instr->left()->representation().Equals(instr->representation()));
1637 DCHECK(instr->right()->representation().Equals(instr->representation()));
1638 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1639 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1640 LAddI* add = new (zone()) LAddI(left, right);
1641 LInstruction* result = DefineAsRegister(add);
1642 if (instr->CheckFlag(HValue::kCanOverflow)) {
1643 result = AssignEnvironment(result);
1646 } else if (instr->representation().IsExternal()) {
1647 DCHECK(instr->left()->representation().IsExternal());
1648 DCHECK(instr->right()->representation().IsInteger32());
1649 DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1650 LOperand* left = UseRegisterAtStart(instr->left());
1651 LOperand* right = UseOrConstantAtStart(instr->right());
1652 LAddI* add = new (zone()) LAddI(left, right);
1653 LInstruction* result = DefineAsRegister(add);
1655 } else if (instr->representation().IsDouble()) {
1656 return DoArithmeticD(Token::ADD, instr);
1658 return DoArithmeticT(Token::ADD, instr);
1663 LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
1664 LOperand* left = NULL;
1665 LOperand* right = NULL;
1666 if (instr->representation().IsSmiOrInteger32()) {
1667 DCHECK(instr->left()->representation().Equals(instr->representation()));
1668 DCHECK(instr->right()->representation().Equals(instr->representation()));
1669 left = UseRegisterAtStart(instr->BetterLeftOperand());
1670 right = UseOrConstantAtStart(instr->BetterRightOperand());
1672 DCHECK(instr->representation().IsDouble());
1673 DCHECK(instr->left()->representation().IsDouble());
1674 DCHECK(instr->right()->representation().IsDouble());
1675 left = UseRegisterAtStart(instr->left());
1676 right = UseRegisterAtStart(instr->right());
1678 return DefineAsRegister(new (zone()) LMathMinMax(left, right));
1682 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1683 DCHECK(instr->representation().IsDouble());
1684 // We call a C function for double power. It can't trigger a GC.
1685 // We need to use fixed result register for the call.
1686 Representation exponent_type = instr->right()->representation();
1687 DCHECK(instr->left()->representation().IsDouble());
1688 LOperand* left = UseFixedDouble(instr->left(), d1);
1690 exponent_type.IsDouble()
1691 ? UseFixedDouble(instr->right(), d2)
1692 : UseFixed(instr->right(), MathPowTaggedDescriptor::exponent());
1693 LPower* result = new (zone()) LPower(left, right);
1694 return MarkAsCall(DefineFixedDouble(result, d3), instr,
1695 CAN_DEOPTIMIZE_EAGERLY);
1699 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1700 DCHECK(instr->left()->representation().IsTagged());
1701 DCHECK(instr->right()->representation().IsTagged());
1702 LOperand* context = UseFixed(instr->context(), cp);
1703 LOperand* left = UseFixed(instr->left(), r4);
1704 LOperand* right = UseFixed(instr->right(), r3);
1705 LCmpT* result = new (zone()) LCmpT(context, left, right);
1706 return MarkAsCall(DefineFixed(result, r3), instr);
1710 LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
1711 HCompareNumericAndBranch* instr) {
1712 Representation r = instr->representation();
1713 if (r.IsSmiOrInteger32()) {
1714 DCHECK(instr->left()->representation().Equals(r));
1715 DCHECK(instr->right()->representation().Equals(r));
1716 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1717 LOperand* right = UseRegisterOrConstantAtStart(instr->right());
1718 return new (zone()) LCompareNumericAndBranch(left, right);
1720 DCHECK(r.IsDouble());
1721 DCHECK(instr->left()->representation().IsDouble());
1722 DCHECK(instr->right()->representation().IsDouble());
1723 LOperand* left = UseRegisterAtStart(instr->left());
1724 LOperand* right = UseRegisterAtStart(instr->right());
1725 return new (zone()) LCompareNumericAndBranch(left, right);
1730 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1731 HCompareObjectEqAndBranch* instr) {
1732 LOperand* left = UseRegisterAtStart(instr->left());
1733 LOperand* right = UseRegisterAtStart(instr->right());
1734 return new (zone()) LCmpObjectEqAndBranch(left, right);
1738 LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
1739 HCompareHoleAndBranch* instr) {
1740 LOperand* value = UseRegisterAtStart(instr->value());
1741 return new (zone()) LCmpHoleAndBranch(value);
1745 LInstruction* LChunkBuilder::DoCompareMinusZeroAndBranch(
1746 HCompareMinusZeroAndBranch* instr) {
1747 LOperand* value = UseRegister(instr->value());
1748 LOperand* scratch = TempRegister();
1749 return new (zone()) LCompareMinusZeroAndBranch(value, scratch);
1753 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1754 DCHECK(instr->value()->representation().IsTagged());
1755 LOperand* value = UseRegisterAtStart(instr->value());
1756 LOperand* temp = TempRegister();
1757 return new (zone()) LIsObjectAndBranch(value, temp);
1761 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1762 DCHECK(instr->value()->representation().IsTagged());
1763 LOperand* value = UseRegisterAtStart(instr->value());
1764 LOperand* temp = TempRegister();
1765 return new (zone()) LIsStringAndBranch(value, temp);
1769 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1770 DCHECK(instr->value()->representation().IsTagged());
1771 return new (zone()) LIsSmiAndBranch(Use(instr->value()));
1775 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1776 HIsUndetectableAndBranch* instr) {
1777 DCHECK(instr->value()->representation().IsTagged());
1778 LOperand* value = UseRegisterAtStart(instr->value());
1779 return new (zone()) LIsUndetectableAndBranch(value, TempRegister());
1783 LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1784 HStringCompareAndBranch* instr) {
1785 DCHECK(instr->left()->representation().IsTagged());
1786 DCHECK(instr->right()->representation().IsTagged());
1787 LOperand* context = UseFixed(instr->context(), cp);
1788 LOperand* left = UseFixed(instr->left(), r4);
1789 LOperand* right = UseFixed(instr->right(), r3);
1790 LStringCompareAndBranch* result =
1791 new (zone()) LStringCompareAndBranch(context, left, right);
1792 return MarkAsCall(result, instr);
1796 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1797 HHasInstanceTypeAndBranch* instr) {
1798 DCHECK(instr->value()->representation().IsTagged());
1799 LOperand* value = UseRegisterAtStart(instr->value());
1800 return new (zone()) LHasInstanceTypeAndBranch(value);
1804 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1805 HGetCachedArrayIndex* instr) {
1806 DCHECK(instr->value()->representation().IsTagged());
1807 LOperand* value = UseRegisterAtStart(instr->value());
1809 return DefineAsRegister(new (zone()) LGetCachedArrayIndex(value));
1813 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1814 HHasCachedArrayIndexAndBranch* instr) {
1815 DCHECK(instr->value()->representation().IsTagged());
1817 LHasCachedArrayIndexAndBranch(UseRegisterAtStart(instr->value()));
1821 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1822 HClassOfTestAndBranch* instr) {
1823 DCHECK(instr->value()->representation().IsTagged());
1824 LOperand* value = UseRegister(instr->value());
1825 return new (zone()) LClassOfTestAndBranch(value, TempRegister());
1829 LInstruction* LChunkBuilder::DoMapEnumLength(HMapEnumLength* instr) {
1830 LOperand* map = UseRegisterAtStart(instr->value());
1831 return DefineAsRegister(new (zone()) LMapEnumLength(map));
1835 LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1836 LOperand* object = UseFixed(instr->value(), r3);
1837 LDateField* result =
1838 new (zone()) LDateField(object, FixedTemp(r4), instr->index());
1839 return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
1843 LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
1844 LOperand* string = UseRegisterAtStart(instr->string());
1845 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1846 return DefineAsRegister(new (zone()) LSeqStringGetChar(string, index));
1850 LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1851 LOperand* string = UseRegisterAtStart(instr->string());
1852 LOperand* index = FLAG_debug_code
1853 ? UseRegisterAtStart(instr->index())
1854 : UseRegisterOrConstantAtStart(instr->index());
1855 LOperand* value = UseRegisterAtStart(instr->value());
1856 LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), cp) : NULL;
1857 return new (zone()) LSeqStringSetChar(context, string, index, value);
1861 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1862 if (!FLAG_debug_code && instr->skip_check()) return NULL;
1863 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1864 LOperand* length = !index->IsConstantOperand()
1865 ? UseRegisterOrConstantAtStart(instr->length())
1866 : UseRegisterAtStart(instr->length());
1867 LInstruction* result = new (zone()) LBoundsCheck(index, length);
1868 if (!FLAG_debug_code || !instr->skip_check()) {
1869 result = AssignEnvironment(result);
1875 LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1876 HBoundsCheckBaseIndexInformation* instr) {
1882 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1883 // The control instruction marking the end of a block that completed
1884 // abruptly (e.g., threw an exception). There is nothing specific to do.
1889 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) { return NULL; }
1892 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1893 // All HForceRepresentation instructions should be eliminated in the
1894 // representation change phase of Hydrogen.
1900 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1901 Representation from = instr->from();
1902 Representation to = instr->to();
1903 HValue* val = instr->value();
1905 if (to.IsTagged()) {
1906 LOperand* value = UseRegister(val);
1907 return DefineSameAsFirst(new (zone()) LDummyUse(value));
1909 from = Representation::Tagged();
1911 if (from.IsTagged()) {
1912 if (to.IsDouble()) {
1913 LOperand* value = UseRegister(val);
1914 LInstruction* result =
1915 DefineAsRegister(new (zone()) LNumberUntagD(value));
1916 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1918 } else if (to.IsSmi()) {
1919 LOperand* value = UseRegister(val);
1920 if (val->type().IsSmi()) {
1921 return DefineSameAsFirst(new (zone()) LDummyUse(value));
1923 return AssignEnvironment(
1924 DefineSameAsFirst(new (zone()) LCheckSmi(value)));
1926 DCHECK(to.IsInteger32());
1927 if (val->type().IsSmi() || val->representation().IsSmi()) {
1928 LOperand* value = UseRegisterAtStart(val);
1929 return DefineAsRegister(new (zone()) LSmiUntag(value, false));
1931 LOperand* value = UseRegister(val);
1932 LOperand* temp1 = TempRegister();
1933 LOperand* temp2 = TempDoubleRegister();
1934 LInstruction* result =
1935 DefineSameAsFirst(new (zone()) LTaggedToI(value, temp1, temp2));
1936 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1940 } else if (from.IsDouble()) {
1941 if (to.IsTagged()) {
1942 info()->MarkAsDeferredCalling();
1943 LOperand* value = UseRegister(val);
1944 LOperand* temp1 = TempRegister();
1945 LOperand* temp2 = TempRegister();
1946 LUnallocated* result_temp = TempRegister();
1947 LNumberTagD* result = new (zone()) LNumberTagD(value, temp1, temp2);
1948 return AssignPointerMap(Define(result, result_temp));
1949 } else if (to.IsSmi()) {
1950 LOperand* value = UseRegister(val);
1951 return AssignEnvironment(
1952 DefineAsRegister(new (zone()) LDoubleToSmi(value)));
1954 DCHECK(to.IsInteger32());
1955 LOperand* value = UseRegister(val);
1956 LInstruction* result = DefineAsRegister(new (zone()) LDoubleToI(value));
1957 if (!instr->CanTruncateToInt32()) result = AssignEnvironment(result);
1960 } else if (from.IsInteger32()) {
1961 info()->MarkAsDeferredCalling();
1962 if (to.IsTagged()) {
1963 if (!instr->CheckFlag(HValue::kCanOverflow)) {
1964 LOperand* value = UseRegisterAtStart(val);
1965 return DefineAsRegister(new (zone()) LSmiTag(value));
1966 } else if (val->CheckFlag(HInstruction::kUint32)) {
1967 LOperand* value = UseRegisterAtStart(val);
1968 LOperand* temp1 = TempRegister();
1969 LOperand* temp2 = TempRegister();
1970 LNumberTagU* result = new (zone()) LNumberTagU(value, temp1, temp2);
1971 return AssignPointerMap(DefineAsRegister(result));
1973 LOperand* value = UseRegisterAtStart(val);
1974 LOperand* temp1 = TempRegister();
1975 LOperand* temp2 = TempRegister();
1976 LNumberTagI* result = new (zone()) LNumberTagI(value, temp1, temp2);
1977 return AssignPointerMap(DefineAsRegister(result));
1979 } else if (to.IsSmi()) {
1980 LOperand* value = UseRegister(val);
1981 LInstruction* result = DefineAsRegister(new (zone()) LSmiTag(value));
1982 if (instr->CheckFlag(HValue::kCanOverflow)) {
1983 result = AssignEnvironment(result);
1987 DCHECK(to.IsDouble());
1988 if (val->CheckFlag(HInstruction::kUint32)) {
1989 return DefineAsRegister(new (zone()) LUint32ToDouble(UseRegister(val)));
1991 return DefineAsRegister(new (zone()) LInteger32ToDouble(Use(val)));
2000 LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
2001 LOperand* value = UseRegisterAtStart(instr->value());
2002 LInstruction* result = new (zone()) LCheckNonSmi(value);
2003 if (!instr->value()->type().IsHeapObject()) {
2004 result = AssignEnvironment(result);
2010 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
2011 LOperand* value = UseRegisterAtStart(instr->value());
2012 return AssignEnvironment(new (zone()) LCheckSmi(value));
2016 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
2017 LOperand* value = UseRegisterAtStart(instr->value());
2018 LInstruction* result = new (zone()) LCheckInstanceType(value);
2019 return AssignEnvironment(result);
2023 LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
2024 LOperand* value = UseRegisterAtStart(instr->value());
2025 return AssignEnvironment(new (zone()) LCheckValue(value));
2029 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2030 if (instr->IsStabilityCheck()) return new (zone()) LCheckMaps;
2031 LOperand* value = UseRegisterAtStart(instr->value());
2032 LInstruction* result = AssignEnvironment(new (zone()) LCheckMaps(value));
2033 if (instr->HasMigrationTarget()) {
2034 info()->MarkAsDeferredCalling();
2035 result = AssignPointerMap(result);
2041 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2042 HValue* value = instr->value();
2043 Representation input_rep = value->representation();
2044 LOperand* reg = UseRegister(value);
2045 if (input_rep.IsDouble()) {
2046 return DefineAsRegister(new (zone()) LClampDToUint8(reg));
2047 } else if (input_rep.IsInteger32()) {
2048 return DefineAsRegister(new (zone()) LClampIToUint8(reg));
2050 DCHECK(input_rep.IsSmiOrTagged());
2051 LClampTToUint8* result =
2052 new (zone()) LClampTToUint8(reg, TempDoubleRegister());
2053 return AssignEnvironment(DefineAsRegister(result));
2058 LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2059 HValue* value = instr->value();
2060 DCHECK(value->representation().IsDouble());
2061 return DefineAsRegister(new (zone()) LDoubleBits(UseRegister(value)));
2065 LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2066 LOperand* lo = UseRegister(instr->lo());
2067 LOperand* hi = UseRegister(instr->hi());
2068 return DefineAsRegister(new (zone()) LConstructDouble(hi, lo));
2072 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2073 LOperand* context = info()->IsStub() ? UseFixed(instr->context(), cp) : NULL;
2074 LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2076 LReturn(UseFixed(instr->value(), r3), context, parameter_count);
2080 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2081 Representation r = instr->representation();
2083 return DefineAsRegister(new (zone()) LConstantS);
2084 } else if (r.IsInteger32()) {
2085 return DefineAsRegister(new (zone()) LConstantI);
2086 } else if (r.IsDouble()) {
2087 return DefineAsRegister(new (zone()) LConstantD);
2088 } else if (r.IsExternal()) {
2089 return DefineAsRegister(new (zone()) LConstantE);
2090 } else if (r.IsTagged()) {
2091 return DefineAsRegister(new (zone()) LConstantT);
2099 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2100 LLoadGlobalCell* result = new (zone()) LLoadGlobalCell;
2101 return instr->RequiresHoleCheck()
2102 ? AssignEnvironment(DefineAsRegister(result))
2103 : DefineAsRegister(result);
2107 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2108 LOperand* context = UseFixed(instr->context(), cp);
2109 LOperand* global_object =
2110 UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2111 LOperand* vector = NULL;
2112 if (instr->HasVectorAndSlot()) {
2113 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2115 LLoadGlobalGeneric* result =
2116 new (zone()) LLoadGlobalGeneric(context, global_object, vector);
2117 return MarkAsCall(DefineFixed(result, r3), instr);
2121 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2122 LOperand* value = UseRegister(instr->value());
2123 // Use a temp to check the value in the cell in the case where we perform
2125 return instr->RequiresHoleCheck()
2126 ? AssignEnvironment(new (zone())
2127 LStoreGlobalCell(value, TempRegister()))
2128 : new (zone()) LStoreGlobalCell(value, NULL);
2132 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2133 LOperand* context = UseRegisterAtStart(instr->value());
2134 LInstruction* result =
2135 DefineAsRegister(new (zone()) LLoadContextSlot(context));
2136 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2137 result = AssignEnvironment(result);
2143 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2146 if (instr->NeedsWriteBarrier()) {
2147 context = UseTempRegister(instr->context());
2148 value = UseTempRegister(instr->value());
2150 context = UseRegister(instr->context());
2151 value = UseRegister(instr->value());
2153 LInstruction* result = new (zone()) LStoreContextSlot(context, value);
2154 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2155 result = AssignEnvironment(result);
2161 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2162 LOperand* obj = UseRegisterAtStart(instr->object());
2163 return DefineAsRegister(new (zone()) LLoadNamedField(obj));
2167 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2168 LOperand* context = UseFixed(instr->context(), cp);
2170 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2171 LOperand* vector = NULL;
2172 if (instr->HasVectorAndSlot()) {
2173 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2176 LInstruction* result =
2177 DefineFixed(new (zone()) LLoadNamedGeneric(context, object, vector), r3);
2178 return MarkAsCall(result, instr);
2182 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2183 HLoadFunctionPrototype* instr) {
2184 return AssignEnvironment(DefineAsRegister(
2185 new (zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
2189 LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2190 return DefineAsRegister(new (zone()) LLoadRoot);
2194 LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2195 DCHECK(instr->key()->representation().IsSmiOrInteger32());
2196 ElementsKind elements_kind = instr->elements_kind();
2197 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2198 LInstruction* result = NULL;
2200 if (!instr->is_typed_elements()) {
2201 LOperand* obj = NULL;
2202 if (instr->representation().IsDouble()) {
2203 obj = UseRegister(instr->elements());
2205 obj = UseRegisterAtStart(instr->elements());
2207 result = DefineAsRegister(new (zone()) LLoadKeyed(obj, key));
2209 DCHECK((instr->representation().IsInteger32() &&
2210 !IsDoubleOrFloatElementsKind(elements_kind)) ||
2211 (instr->representation().IsDouble() &&
2212 IsDoubleOrFloatElementsKind(elements_kind)));
2213 LOperand* backing_store = UseRegister(instr->elements());
2214 result = DefineAsRegister(new (zone()) LLoadKeyed(backing_store, key));
2217 if ((instr->is_external() || instr->is_fixed_typed_array())
2219 // see LCodeGen::DoLoadKeyedExternalArray
2220 ((elements_kind == EXTERNAL_UINT32_ELEMENTS ||
2221 elements_kind == UINT32_ELEMENTS) &&
2222 !instr->CheckFlag(HInstruction::kUint32))
2224 // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2225 // LCodeGen::DoLoadKeyedFixedArray
2226 instr->RequiresHoleCheck()) {
2227 result = AssignEnvironment(result);
2233 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2234 LOperand* context = UseFixed(instr->context(), cp);
2236 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2237 LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2238 LOperand* vector = NULL;
2239 if (instr->HasVectorAndSlot()) {
2240 vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2243 LInstruction* result = DefineFixed(
2244 new (zone()) LLoadKeyedGeneric(context, object, key, vector), r3);
2245 return MarkAsCall(result, instr);
2249 LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2250 if (!instr->is_typed_elements()) {
2251 DCHECK(instr->elements()->representation().IsTagged());
2252 bool needs_write_barrier = instr->NeedsWriteBarrier();
2253 LOperand* object = NULL;
2254 LOperand* key = NULL;
2255 LOperand* val = NULL;
2257 if (instr->value()->representation().IsDouble()) {
2258 object = UseRegisterAtStart(instr->elements());
2259 val = UseRegister(instr->value());
2260 key = UseRegisterOrConstantAtStart(instr->key());
2262 if (needs_write_barrier) {
2263 object = UseTempRegister(instr->elements());
2264 val = UseTempRegister(instr->value());
2265 key = UseTempRegister(instr->key());
2267 object = UseRegisterAtStart(instr->elements());
2268 val = UseRegisterAtStart(instr->value());
2269 key = UseRegisterOrConstantAtStart(instr->key());
2273 return new (zone()) LStoreKeyed(object, key, val);
2276 DCHECK((instr->value()->representation().IsInteger32() &&
2277 !IsDoubleOrFloatElementsKind(instr->elements_kind())) ||
2278 (instr->value()->representation().IsDouble() &&
2279 IsDoubleOrFloatElementsKind(instr->elements_kind())));
2280 DCHECK((instr->is_fixed_typed_array() &&
2281 instr->elements()->representation().IsTagged()) ||
2282 (instr->is_external() &&
2283 instr->elements()->representation().IsExternal()));
2284 LOperand* val = UseRegister(instr->value());
2285 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2286 LOperand* backing_store = UseRegister(instr->elements());
2287 return new (zone()) LStoreKeyed(backing_store, key, val);
2291 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2292 LOperand* context = UseFixed(instr->context(), cp);
2294 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2295 LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2296 LOperand* val = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2298 DCHECK(instr->object()->representation().IsTagged());
2299 DCHECK(instr->key()->representation().IsTagged());
2300 DCHECK(instr->value()->representation().IsTagged());
2302 return MarkAsCall(new (zone()) LStoreKeyedGeneric(context, obj, key, val),
2307 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2308 HTransitionElementsKind* instr) {
2309 if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2310 LOperand* object = UseRegister(instr->object());
2311 LOperand* new_map_reg = TempRegister();
2312 LTransitionElementsKind* result =
2313 new (zone()) LTransitionElementsKind(object, NULL, new_map_reg);
2316 LOperand* object = UseFixed(instr->object(), r3);
2317 LOperand* context = UseFixed(instr->context(), cp);
2318 LTransitionElementsKind* result =
2319 new (zone()) LTransitionElementsKind(object, context, NULL);
2320 return MarkAsCall(result, instr);
2325 LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2326 HTrapAllocationMemento* instr) {
2327 LOperand* object = UseRegister(instr->object());
2328 LOperand* temp = TempRegister();
2329 LTrapAllocationMemento* result =
2330 new (zone()) LTrapAllocationMemento(object, temp);
2331 return AssignEnvironment(result);
2335 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2336 bool is_in_object = instr->access().IsInobject();
2337 bool needs_write_barrier = instr->NeedsWriteBarrier();
2338 bool needs_write_barrier_for_map =
2339 instr->has_transition() && instr->NeedsWriteBarrierForMap();
2342 if (needs_write_barrier) {
2343 obj = is_in_object ? UseRegister(instr->object())
2344 : UseTempRegister(instr->object());
2346 obj = needs_write_barrier_for_map ? UseRegister(instr->object())
2347 : UseRegisterAtStart(instr->object());
2351 if (needs_write_barrier) {
2352 val = UseTempRegister(instr->value());
2353 } else if (instr->field_representation().IsDouble()) {
2354 val = UseRegisterAtStart(instr->value());
2356 val = UseRegister(instr->value());
2359 // We need a temporary register for write barrier of the map field.
2360 LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;
2362 return new (zone()) LStoreNamedField(obj, val, temp);
2366 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2367 LOperand* context = UseFixed(instr->context(), cp);
2369 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2370 LOperand* val = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2372 LInstruction* result = new (zone()) LStoreNamedGeneric(context, obj, val);
2373 return MarkAsCall(result, instr);
2377 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2378 LOperand* context = UseFixed(instr->context(), cp);
2379 LOperand* left = UseFixed(instr->left(), r4);
2380 LOperand* right = UseFixed(instr->right(), r3);
2382 DefineFixed(new (zone()) LStringAdd(context, left, right), r3), instr);
2386 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2387 LOperand* string = UseTempRegister(instr->string());
2388 LOperand* index = UseTempRegister(instr->index());
2389 LOperand* context = UseAny(instr->context());
2390 LStringCharCodeAt* result =
2391 new (zone()) LStringCharCodeAt(context, string, index);
2392 return AssignPointerMap(DefineAsRegister(result));
2396 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2397 LOperand* char_code = UseRegister(instr->value());
2398 LOperand* context = UseAny(instr->context());
2399 LStringCharFromCode* result =
2400 new (zone()) LStringCharFromCode(context, char_code);
2401 return AssignPointerMap(DefineAsRegister(result));
2405 LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2406 info()->MarkAsDeferredCalling();
2407 LOperand* context = UseAny(instr->context());
2408 LOperand* size = UseRegisterOrConstant(instr->size());
2409 LOperand* temp1 = TempRegister();
2410 LOperand* temp2 = TempRegister();
2411 LAllocate* result = new (zone()) LAllocate(context, size, temp1, temp2);
2412 return AssignPointerMap(DefineAsRegister(result));
2416 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2417 LOperand* context = UseFixed(instr->context(), cp);
2418 return MarkAsCall(DefineFixed(new (zone()) LRegExpLiteral(context), r3),
2423 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2424 LOperand* context = UseFixed(instr->context(), cp);
2425 return MarkAsCall(DefineFixed(new (zone()) LFunctionLiteral(context), r3),
2430 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2431 DCHECK(argument_count_ == 0);
2432 allocator_->MarkAsOsrEntry();
2433 current_block_->last_environment()->set_ast_id(instr->ast_id());
2434 return AssignEnvironment(new (zone()) LOsrEntry);
2438 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2439 LParameter* result = new (zone()) LParameter;
2440 if (instr->kind() == HParameter::STACK_PARAMETER) {
2441 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2442 return DefineAsSpilled(result, spill_index);
2444 DCHECK(info()->IsStub());
2445 CallInterfaceDescriptor descriptor =
2446 info()->code_stub()->GetCallInterfaceDescriptor();
2447 int index = static_cast<int>(instr->index());
2448 Register reg = descriptor.GetEnvironmentParameterRegister(index);
2449 return DefineFixed(result, reg);
2454 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2455 // Use an index that corresponds to the location in the unoptimized frame,
2456 // which the optimized frame will subsume.
2457 int env_index = instr->index();
2458 int spill_index = 0;
2459 if (instr->environment()->is_parameter_index(env_index)) {
2460 spill_index = chunk()->GetParameterStackSlot(env_index);
2462 spill_index = env_index - instr->environment()->first_local_index();
2463 if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2464 Retry(kTooManySpillSlotsNeededForOSR);
2468 return DefineAsSpilled(new (zone()) LUnknownOSRValue, spill_index);
2472 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2473 LOperand* context = UseFixed(instr->context(), cp);
2474 return MarkAsCall(DefineFixed(new (zone()) LCallStub(context), r3), instr);
2478 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2479 // There are no real uses of the arguments object.
2480 // arguments.length and element access are supported directly on
2481 // stack arguments, and any real arguments object use causes a bailout.
2482 // So this value is never used.
2487 LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2488 instr->ReplayEnvironment(current_block_->last_environment());
2490 // There are no real uses of a captured object.
2495 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2496 info()->MarkAsRequiresFrame();
2497 LOperand* args = UseRegister(instr->arguments());
2498 LOperand* length = UseRegisterOrConstantAtStart(instr->length());
2499 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
2500 return DefineAsRegister(new (zone()) LAccessArgumentsAt(args, length, index));
2504 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2505 LOperand* object = UseFixed(instr->value(), r3);
2506 LToFastProperties* result = new (zone()) LToFastProperties(object);
2507 return MarkAsCall(DefineFixed(result, r3), instr);
2511 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2512 LOperand* context = UseFixed(instr->context(), cp);
2513 LTypeof* result = new (zone()) LTypeof(context, UseFixed(instr->value(), r3));
2514 return MarkAsCall(DefineFixed(result, r3), instr);
2518 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2519 return new (zone()) LTypeofIsAndBranch(UseRegister(instr->value()));
2523 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2524 HIsConstructCallAndBranch* instr) {
2525 return new (zone()) LIsConstructCallAndBranch(TempRegister());
2529 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2530 instr->ReplayEnvironment(current_block_->last_environment());
2535 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2536 if (instr->is_function_entry()) {
2537 LOperand* context = UseFixed(instr->context(), cp);
2538 return MarkAsCall(new (zone()) LStackCheck(context), instr);
2540 DCHECK(instr->is_backwards_branch());
2541 LOperand* context = UseAny(instr->context());
2542 return AssignEnvironment(
2543 AssignPointerMap(new (zone()) LStackCheck(context)));
2548 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2549 HEnvironment* outer = current_block_->last_environment();
2550 outer->set_ast_id(instr->ReturnId());
2551 HConstant* undefined = graph()->GetConstantUndefined();
2552 HEnvironment* inner = outer->CopyForInlining(
2553 instr->closure(), instr->arguments_count(), instr->function(), undefined,
2554 instr->inlining_kind());
2555 // Only replay binding of arguments object if it wasn't removed from graph.
2556 if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2557 inner->Bind(instr->arguments_var(), instr->arguments_object());
2559 inner->BindContext(instr->closure_context());
2560 inner->set_entry(instr);
2561 current_block_->UpdateEnvironment(inner);
2562 chunk_->AddInlinedClosure(instr->closure());
2567 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2568 LInstruction* pop = NULL;
2570 HEnvironment* env = current_block_->last_environment();
2572 if (env->entry()->arguments_pushed()) {
2573 int argument_count = env->arguments_environment()->parameter_count();
2574 pop = new (zone()) LDrop(argument_count);
2575 DCHECK(instr->argument_delta() == -argument_count);
2578 HEnvironment* outer =
2579 current_block_->last_environment()->DiscardInlined(false);
2580 current_block_->UpdateEnvironment(outer);
2586 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2587 LOperand* context = UseFixed(instr->context(), cp);
2588 LOperand* object = UseFixed(instr->enumerable(), r3);
2589 LForInPrepareMap* result = new (zone()) LForInPrepareMap(context, object);
2590 return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
2594 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2595 LOperand* map = UseRegister(instr->map());
2596 return AssignEnvironment(
2597 DefineAsRegister(new (zone()) LForInCacheArray(map)));
2601 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2602 LOperand* value = UseRegisterAtStart(instr->value());
2603 LOperand* map = UseRegisterAtStart(instr->map());
2604 return AssignEnvironment(new (zone()) LCheckMapValue(value, map));
2608 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2609 LOperand* object = UseRegister(instr->object());
2610 LOperand* index = UseTempRegister(instr->index());
2611 LLoadFieldByIndex* load = new (zone()) LLoadFieldByIndex(object, index);
2612 LInstruction* result = DefineSameAsFirst(load);
2613 return AssignPointerMap(result);
2617 LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2618 LOperand* context = UseRegisterAtStart(instr->context());
2619 return new (zone()) LStoreFrameContext(context);
2623 LInstruction* LChunkBuilder::DoAllocateBlockContext(
2624 HAllocateBlockContext* instr) {
2625 LOperand* context = UseFixed(instr->context(), cp);
2626 LOperand* function = UseRegisterAtStart(instr->function());
2627 LAllocateBlockContext* result =
2628 new (zone()) LAllocateBlockContext(context, function);
2629 return MarkAsCall(DefineFixed(result, cp), instr);
2632 } // namespace v8::internal