blob: e2772d5ee38b30ad212a8914598b70d16fc25989 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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.
4
5#include "src/crankshaft/ia32/lithium-ia32.h"
6
7#include <sstream>
8
9#if V8_TARGET_ARCH_IA32
10
11#include "src/crankshaft/hydrogen-osr.h"
12#include "src/crankshaft/ia32/lithium-codegen-ia32.h"
13#include "src/crankshaft/lithium-inl.h"
14
15namespace v8 {
16namespace internal {
17
18#define DEFINE_COMPILE(type) \
19 void L##type::CompileToNative(LCodeGen* generator) { \
20 generator->Do##type(this); \
21 }
22LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
23#undef DEFINE_COMPILE
24
25
26#ifdef DEBUG
27void LInstruction::VerifyCall() {
28 // Call instructions can use only fixed registers as temporaries and
29 // outputs because all registers are blocked by the calling convention.
30 // Inputs operands must use a fixed register or use-at-start policy or
31 // a non-register policy.
32 DCHECK(Output() == NULL ||
33 LUnallocated::cast(Output())->HasFixedPolicy() ||
34 !LUnallocated::cast(Output())->HasRegisterPolicy());
35 for (UseIterator it(this); !it.Done(); it.Advance()) {
36 LUnallocated* operand = LUnallocated::cast(it.Current());
37 DCHECK(operand->HasFixedPolicy() ||
38 operand->IsUsedAtStart());
39 }
40 for (TempIterator it(this); !it.Done(); it.Advance()) {
41 LUnallocated* operand = LUnallocated::cast(it.Current());
42 DCHECK(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
43 }
44}
45#endif
46
47
48bool LInstruction::HasDoubleRegisterResult() {
49 return HasResult() && result()->IsDoubleRegister();
50}
51
52
53bool LInstruction::HasDoubleRegisterInput() {
54 for (int i = 0; i < InputCount(); i++) {
55 LOperand* op = InputAt(i);
56 if (op != NULL && op->IsDoubleRegister()) {
57 return true;
58 }
59 }
60 return false;
61}
62
63
64void LInstruction::PrintTo(StringStream* stream) {
65 stream->Add("%s ", this->Mnemonic());
66
67 PrintOutputOperandTo(stream);
68
69 PrintDataTo(stream);
70
71 if (HasEnvironment()) {
72 stream->Add(" ");
73 environment()->PrintTo(stream);
74 }
75
76 if (HasPointerMap()) {
77 stream->Add(" ");
78 pointer_map()->PrintTo(stream);
79 }
80}
81
82
83void LInstruction::PrintDataTo(StringStream* stream) {
84 stream->Add("= ");
85 for (int i = 0; i < InputCount(); i++) {
86 if (i > 0) stream->Add(" ");
87 if (InputAt(i) == NULL) {
88 stream->Add("NULL");
89 } else {
90 InputAt(i)->PrintTo(stream);
91 }
92 }
93}
94
95
96void LInstruction::PrintOutputOperandTo(StringStream* stream) {
97 if (HasResult()) result()->PrintTo(stream);
98}
99
100
101void LLabel::PrintDataTo(StringStream* stream) {
102 LGap::PrintDataTo(stream);
103 LLabel* rep = replacement();
104 if (rep != NULL) {
105 stream->Add(" Dead block replaced with B%d", rep->block_id());
106 }
107}
108
109
110bool LGap::IsRedundant() const {
111 for (int i = 0; i < 4; i++) {
112 if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
113 return false;
114 }
115 }
116
117 return true;
118}
119
120
121void LGap::PrintDataTo(StringStream* stream) {
122 for (int i = 0; i < 4; i++) {
123 stream->Add("(");
124 if (parallel_moves_[i] != NULL) {
125 parallel_moves_[i]->PrintDataTo(stream);
126 }
127 stream->Add(") ");
128 }
129}
130
131
132const char* LArithmeticD::Mnemonic() const {
133 switch (op()) {
134 case Token::ADD: return "add-d";
135 case Token::SUB: return "sub-d";
136 case Token::MUL: return "mul-d";
137 case Token::DIV: return "div-d";
138 case Token::MOD: return "mod-d";
139 default:
140 UNREACHABLE();
141 return NULL;
142 }
143}
144
145
146const char* LArithmeticT::Mnemonic() const {
147 switch (op()) {
148 case Token::ADD: return "add-t";
149 case Token::SUB: return "sub-t";
150 case Token::MUL: return "mul-t";
151 case Token::MOD: return "mod-t";
152 case Token::DIV: return "div-t";
153 case Token::BIT_AND: return "bit-and-t";
154 case Token::BIT_OR: return "bit-or-t";
155 case Token::BIT_XOR: return "bit-xor-t";
156 case Token::ROR: return "ror-t";
157 case Token::SHL: return "sal-t";
158 case Token::SAR: return "sar-t";
159 case Token::SHR: return "shr-t";
160 default:
161 UNREACHABLE();
162 return NULL;
163 }
164}
165
166
167bool LGoto::HasInterestingComment(LCodeGen* gen) const {
168 return !gen->IsNextEmittedBlock(block_id());
169}
170
171
172void LGoto::PrintDataTo(StringStream* stream) {
173 stream->Add("B%d", block_id());
174}
175
176
177void LBranch::PrintDataTo(StringStream* stream) {
178 stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
179 value()->PrintTo(stream);
180}
181
182
183void LCompareNumericAndBranch::PrintDataTo(StringStream* stream) {
184 stream->Add("if ");
185 left()->PrintTo(stream);
186 stream->Add(" %s ", Token::String(op()));
187 right()->PrintTo(stream);
188 stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
189}
190
191
192void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
193 stream->Add("if is_string(");
194 value()->PrintTo(stream);
195 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
196}
197
198
199void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
200 stream->Add("if is_smi(");
201 value()->PrintTo(stream);
202 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
203}
204
205
206void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
207 stream->Add("if is_undetectable(");
208 value()->PrintTo(stream);
209 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
210}
211
212
213void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
214 stream->Add("if string_compare(");
215 left()->PrintTo(stream);
216 right()->PrintTo(stream);
217 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
218}
219
220
221void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
222 stream->Add("if has_instance_type(");
223 value()->PrintTo(stream);
224 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
225}
226
227
228void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
229 stream->Add("if has_cached_array_index(");
230 value()->PrintTo(stream);
231 stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
232}
233
234
235void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
236 stream->Add("if class_of_test(");
237 value()->PrintTo(stream);
238 stream->Add(", \"%o\") then B%d else B%d",
239 *hydrogen()->class_name(),
240 true_block_id(),
241 false_block_id());
242}
243
244
245void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
246 stream->Add("if typeof ");
247 value()->PrintTo(stream);
248 stream->Add(" == \"%s\" then B%d else B%d",
249 hydrogen()->type_literal()->ToCString().get(),
250 true_block_id(), false_block_id());
251}
252
253
254void LStoreCodeEntry::PrintDataTo(StringStream* stream) {
255 stream->Add(" = ");
256 function()->PrintTo(stream);
257 stream->Add(".code_entry = ");
258 code_object()->PrintTo(stream);
259}
260
261
262void LInnerAllocatedObject::PrintDataTo(StringStream* stream) {
263 stream->Add(" = ");
264 base_object()->PrintTo(stream);
265 stream->Add(" + ");
266 offset()->PrintTo(stream);
267}
268
269
270void LCallFunction::PrintDataTo(StringStream* stream) {
271 context()->PrintTo(stream);
272 stream->Add(" ");
273 function()->PrintTo(stream);
274 if (hydrogen()->HasVectorAndSlot()) {
275 stream->Add(" (type-feedback-vector ");
276 temp_vector()->PrintTo(stream);
277 stream->Add(" ");
278 temp_slot()->PrintTo(stream);
279 stream->Add(")");
280 }
281}
282
283
284void LCallJSFunction::PrintDataTo(StringStream* stream) {
285 stream->Add("= ");
286 function()->PrintTo(stream);
287 stream->Add("#%d / ", arity());
288}
289
290
291void LCallWithDescriptor::PrintDataTo(StringStream* stream) {
292 for (int i = 0; i < InputCount(); i++) {
293 InputAt(i)->PrintTo(stream);
294 stream->Add(" ");
295 }
296 stream->Add("#%d / ", arity());
297}
298
299
300void LLoadContextSlot::PrintDataTo(StringStream* stream) {
301 context()->PrintTo(stream);
302 stream->Add("[%d]", slot_index());
303}
304
305
306void LStoreContextSlot::PrintDataTo(StringStream* stream) {
307 context()->PrintTo(stream);
308 stream->Add("[%d] <- ", slot_index());
309 value()->PrintTo(stream);
310}
311
312
313void LInvokeFunction::PrintDataTo(StringStream* stream) {
314 stream->Add("= ");
315 context()->PrintTo(stream);
316 stream->Add(" ");
317 function()->PrintTo(stream);
318 stream->Add(" #%d / ", arity());
319}
320
321
322void LCallNewArray::PrintDataTo(StringStream* stream) {
323 stream->Add("= ");
324 context()->PrintTo(stream);
325 stream->Add(" ");
326 constructor()->PrintTo(stream);
327 stream->Add(" #%d / ", arity());
328 ElementsKind kind = hydrogen()->elements_kind();
329 stream->Add(" (%s) ", ElementsKindToString(kind));
330}
331
332
333void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
334 arguments()->PrintTo(stream);
335
336 stream->Add(" length ");
337 length()->PrintTo(stream);
338
339 stream->Add(" index ");
340 index()->PrintTo(stream);
341}
342
343
344int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
345 // Skip a slot if for a double-width slot.
346 if (kind == DOUBLE_REGISTERS) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100347 current_frame_slots_++;
348 current_frame_slots_ |= 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000349 num_double_slots_++;
350 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100351 return current_frame_slots_++;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352}
353
354
355LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
356 int index = GetNextSpillIndex(kind);
357 if (kind == DOUBLE_REGISTERS) {
358 return LDoubleStackSlot::Create(index, zone());
359 } else {
360 DCHECK(kind == GENERAL_REGISTERS);
361 return LStackSlot::Create(index, zone());
362 }
363}
364
365
366void LStoreNamedField::PrintDataTo(StringStream* stream) {
367 object()->PrintTo(stream);
368 std::ostringstream os;
369 os << hydrogen()->access() << " <- ";
370 stream->Add(os.str().c_str());
371 value()->PrintTo(stream);
372}
373
374
375void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
376 object()->PrintTo(stream);
377 stream->Add(".");
378 stream->Add(String::cast(*name())->ToCString().get());
379 stream->Add(" <- ");
380 value()->PrintTo(stream);
381}
382
383
384void LLoadKeyed::PrintDataTo(StringStream* stream) {
385 elements()->PrintTo(stream);
386 stream->Add("[");
387 key()->PrintTo(stream);
388 if (hydrogen()->IsDehoisted()) {
389 stream->Add(" + %d]", base_offset());
390 } else {
391 stream->Add("]");
392 }
393}
394
395
396void LStoreKeyed::PrintDataTo(StringStream* stream) {
397 elements()->PrintTo(stream);
398 stream->Add("[");
399 key()->PrintTo(stream);
400 if (hydrogen()->IsDehoisted()) {
401 stream->Add(" + %d] <-", base_offset());
402 } else {
403 stream->Add("] <- ");
404 }
405
406 if (value() == NULL) {
407 DCHECK(hydrogen()->IsConstantHoleStore() &&
408 hydrogen()->value()->representation().IsDouble());
409 stream->Add("<the hole(nan)>");
410 } else {
411 value()->PrintTo(stream);
412 }
413}
414
415
416void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
417 object()->PrintTo(stream);
418 stream->Add("[");
419 key()->PrintTo(stream);
420 stream->Add("] <- ");
421 value()->PrintTo(stream);
422}
423
424
425void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
426 object()->PrintTo(stream);
427 stream->Add(" %p -> %p", *original_map(), *transitioned_map());
428}
429
430
431LPlatformChunk* LChunkBuilder::Build() {
432 DCHECK(is_unused());
433 chunk_ = new(zone()) LPlatformChunk(info(), graph());
434 LPhase phase("L_Building chunk", chunk_);
435 status_ = BUILDING;
436
437 // Reserve the first spill slot for the state of dynamic alignment.
438 if (info()->IsOptimizing()) {
439 int alignment_state_index = chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100440 DCHECK_EQ(alignment_state_index, 4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 USE(alignment_state_index);
442 }
443
444 // If compiling for OSR, reserve space for the unoptimized frame,
445 // which will be subsumed into this frame.
446 if (graph()->has_osr()) {
447 for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
448 chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
449 }
450 }
451
452 const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
453 for (int i = 0; i < blocks->length(); i++) {
454 HBasicBlock* next = NULL;
455 if (i < blocks->length() - 1) next = blocks->at(i + 1);
456 DoBasicBlock(blocks->at(i), next);
457 if (is_aborted()) return NULL;
458 }
459 status_ = DONE;
460 return chunk_;
461}
462
463
464LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
465 return new (zone()) LUnallocated(LUnallocated::FIXED_REGISTER, reg.code());
466}
467
468
469LUnallocated* LChunkBuilder::ToUnallocated(XMMRegister reg) {
470 return new (zone())
471 LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER, reg.code());
472}
473
474
475LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
476 return Use(value, ToUnallocated(fixed_register));
477}
478
479
480LOperand* LChunkBuilder::UseFixedDouble(HValue* value, XMMRegister reg) {
481 return Use(value, ToUnallocated(reg));
482}
483
484
485LOperand* LChunkBuilder::UseRegister(HValue* value) {
486 return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
487}
488
489
490LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
491 return Use(value,
492 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
493 LUnallocated::USED_AT_START));
494}
495
496
497LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
498 return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
499}
500
501
502LOperand* LChunkBuilder::Use(HValue* value) {
503 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
504}
505
506
507LOperand* LChunkBuilder::UseAtStart(HValue* value) {
508 return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
509 LUnallocated::USED_AT_START));
510}
511
512
513static inline bool CanBeImmediateConstant(HValue* value) {
514 return value->IsConstant() && HConstant::cast(value)->NotInNewSpace();
515}
516
517
518LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
519 return CanBeImmediateConstant(value)
520 ? chunk_->DefineConstantOperand(HConstant::cast(value))
521 : Use(value);
522}
523
524
525LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
526 return CanBeImmediateConstant(value)
527 ? chunk_->DefineConstantOperand(HConstant::cast(value))
528 : UseAtStart(value);
529}
530
531
532LOperand* LChunkBuilder::UseFixedOrConstant(HValue* value,
533 Register fixed_register) {
534 return CanBeImmediateConstant(value)
535 ? chunk_->DefineConstantOperand(HConstant::cast(value))
536 : UseFixed(value, fixed_register);
537}
538
539
540LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
541 return CanBeImmediateConstant(value)
542 ? chunk_->DefineConstantOperand(HConstant::cast(value))
543 : UseRegister(value);
544}
545
546
547LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
548 return CanBeImmediateConstant(value)
549 ? chunk_->DefineConstantOperand(HConstant::cast(value))
550 : UseRegisterAtStart(value);
551}
552
553
554LOperand* LChunkBuilder::UseConstant(HValue* value) {
555 return chunk_->DefineConstantOperand(HConstant::cast(value));
556}
557
558
559LOperand* LChunkBuilder::UseAny(HValue* value) {
560 return value->IsConstant()
561 ? chunk_->DefineConstantOperand(HConstant::cast(value))
562 : Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
563}
564
565
566LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
567 if (value->EmitAtUses()) {
568 HInstruction* instr = HInstruction::cast(value);
569 VisitInstruction(instr);
570 }
571 operand->set_virtual_register(value->id());
572 return operand;
573}
574
575
576LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
577 LUnallocated* result) {
578 result->set_virtual_register(current_instruction_->id());
579 instr->set_result(result);
580 return instr;
581}
582
583
584LInstruction* LChunkBuilder::DefineAsRegister(
585 LTemplateResultInstruction<1>* instr) {
586 return Define(instr,
587 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
588}
589
590
591LInstruction* LChunkBuilder::DefineAsSpilled(
592 LTemplateResultInstruction<1>* instr,
593 int index) {
594 return Define(instr,
595 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
596}
597
598
599LInstruction* LChunkBuilder::DefineSameAsFirst(
600 LTemplateResultInstruction<1>* instr) {
601 return Define(instr,
602 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
603}
604
605
606LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
607 Register reg) {
608 return Define(instr, ToUnallocated(reg));
609}
610
611
612LInstruction* LChunkBuilder::DefineFixedDouble(
613 LTemplateResultInstruction<1>* instr,
614 XMMRegister reg) {
615 return Define(instr, ToUnallocated(reg));
616}
617
618
619LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
620 HEnvironment* hydrogen_env = current_block_->last_environment();
621 int argument_index_accumulator = 0;
622 ZoneList<HValue*> objects_to_materialize(0, zone());
623 instr->set_environment(CreateEnvironment(
624 hydrogen_env, &argument_index_accumulator, &objects_to_materialize));
625 return instr;
626}
627
628
629LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
630 HInstruction* hinstr,
631 CanDeoptimize can_deoptimize) {
632 info()->MarkAsNonDeferredCalling();
633
634#ifdef DEBUG
635 instr->VerifyCall();
636#endif
637 instr->MarkAsCall();
638 instr = AssignPointerMap(instr);
639
640 // If instruction does not have side-effects lazy deoptimization
641 // after the call will try to deoptimize to the point before the call.
642 // Thus we still need to attach environment to this call even if
643 // call sequence can not deoptimize eagerly.
644 bool needs_environment =
645 (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
646 !hinstr->HasObservableSideEffects();
647 if (needs_environment && !instr->HasEnvironment()) {
648 instr = AssignEnvironment(instr);
649 // We can't really figure out if the environment is needed or not.
650 instr->environment()->set_has_been_used();
651 }
652
653 return instr;
654}
655
656
657LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
658 DCHECK(!instr->HasPointerMap());
659 instr->set_pointer_map(new(zone()) LPointerMap(zone()));
660 return instr;
661}
662
663
664LUnallocated* LChunkBuilder::TempRegister() {
665 LUnallocated* operand =
666 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
667 int vreg = allocator_->GetVirtualRegister();
668 if (!allocator_->AllocationOk()) {
669 Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
670 vreg = 0;
671 }
672 operand->set_virtual_register(vreg);
673 return operand;
674}
675
676
677LOperand* LChunkBuilder::FixedTemp(Register reg) {
678 LUnallocated* operand = ToUnallocated(reg);
679 DCHECK(operand->HasFixedPolicy());
680 return operand;
681}
682
683
684LOperand* LChunkBuilder::FixedTemp(XMMRegister reg) {
685 LUnallocated* operand = ToUnallocated(reg);
686 DCHECK(operand->HasFixedPolicy());
687 return operand;
688}
689
690
691LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
692 return new(zone()) LLabel(instr->block());
693}
694
695
696LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
697 return DefineAsRegister(new(zone()) LDummyUse(UseAny(instr->value())));
698}
699
700
701LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
702 UNREACHABLE();
703 return NULL;
704}
705
706
707LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
708 return AssignEnvironment(new(zone()) LDeoptimize);
709}
710
711
712LInstruction* LChunkBuilder::DoShift(Token::Value op,
713 HBitwiseBinaryOperation* instr) {
714 if (instr->representation().IsSmiOrInteger32()) {
715 DCHECK(instr->left()->representation().Equals(instr->representation()));
716 DCHECK(instr->right()->representation().Equals(instr->representation()));
717 LOperand* left = UseRegisterAtStart(instr->left());
718
719 HValue* right_value = instr->right();
720 LOperand* right = NULL;
721 int constant_value = 0;
722 bool does_deopt = false;
723 if (right_value->IsConstant()) {
724 HConstant* constant = HConstant::cast(right_value);
725 right = chunk_->DefineConstantOperand(constant);
726 constant_value = constant->Integer32Value() & 0x1f;
727 // Left shifts can deoptimize if we shift by > 0 and the result cannot be
728 // truncated to smi.
729 if (instr->representation().IsSmi() && constant_value > 0) {
730 does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
731 }
732 } else {
733 right = UseFixed(right_value, ecx);
734 }
735
736 // Shift operations can only deoptimize if we do a logical shift by 0 and
737 // the result cannot be truncated to int32.
738 if (op == Token::SHR && constant_value == 0) {
739 does_deopt = !instr->CheckFlag(HInstruction::kUint32);
740 }
741
742 LInstruction* result =
743 DefineSameAsFirst(new(zone()) LShiftI(op, left, right, does_deopt));
744 return does_deopt ? AssignEnvironment(result) : result;
745 } else {
746 return DoArithmeticT(op, instr);
747 }
748}
749
750
751LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
752 HArithmeticBinaryOperation* instr) {
753 DCHECK(instr->representation().IsDouble());
754 DCHECK(instr->left()->representation().IsDouble());
755 DCHECK(instr->right()->representation().IsDouble());
756 if (op == Token::MOD) {
757 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
758 LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
759 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
760 return MarkAsCall(DefineSameAsFirst(result), instr);
761 } else {
762 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
763 LOperand* right = UseRegisterAtStart(instr->BetterRightOperand());
764 LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
765 return CpuFeatures::IsSupported(AVX) ? DefineAsRegister(result)
766 : DefineSameAsFirst(result);
767 }
768}
769
770
771LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
772 HBinaryOperation* instr) {
773 HValue* left = instr->left();
774 HValue* right = instr->right();
775 DCHECK(left->representation().IsTagged());
776 DCHECK(right->representation().IsTagged());
777 LOperand* context = UseFixed(instr->context(), esi);
778 LOperand* left_operand = UseFixed(left, edx);
779 LOperand* right_operand = UseFixed(right, eax);
780 LArithmeticT* result =
781 new(zone()) LArithmeticT(op, context, left_operand, right_operand);
782 return MarkAsCall(DefineFixed(result, eax), instr);
783}
784
785
786void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
787 DCHECK(is_building());
788 current_block_ = block;
789 next_block_ = next_block;
790 if (block->IsStartBlock()) {
791 block->UpdateEnvironment(graph_->start_environment());
792 argument_count_ = 0;
793 } else if (block->predecessors()->length() == 1) {
794 // We have a single predecessor => copy environment and outgoing
795 // argument count from the predecessor.
796 DCHECK(block->phis()->length() == 0);
797 HBasicBlock* pred = block->predecessors()->at(0);
798 HEnvironment* last_environment = pred->last_environment();
799 DCHECK(last_environment != NULL);
800 // Only copy the environment, if it is later used again.
801 if (pred->end()->SecondSuccessor() == NULL) {
802 DCHECK(pred->end()->FirstSuccessor() == block);
803 } else {
804 if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
805 pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
806 last_environment = last_environment->Copy();
807 }
808 }
809 block->UpdateEnvironment(last_environment);
810 DCHECK(pred->argument_count() >= 0);
811 argument_count_ = pred->argument_count();
812 } else {
813 // We are at a state join => process phis.
814 HBasicBlock* pred = block->predecessors()->at(0);
815 // No need to copy the environment, it cannot be used later.
816 HEnvironment* last_environment = pred->last_environment();
817 for (int i = 0; i < block->phis()->length(); ++i) {
818 HPhi* phi = block->phis()->at(i);
819 if (phi->HasMergedIndex()) {
820 last_environment->SetValueAt(phi->merged_index(), phi);
821 }
822 }
823 for (int i = 0; i < block->deleted_phis()->length(); ++i) {
824 if (block->deleted_phis()->at(i) < last_environment->length()) {
825 last_environment->SetValueAt(block->deleted_phis()->at(i),
826 graph_->GetConstantUndefined());
827 }
828 }
829 block->UpdateEnvironment(last_environment);
830 // Pick up the outgoing argument count of one of the predecessors.
831 argument_count_ = pred->argument_count();
832 }
833 HInstruction* current = block->first();
834 int start = chunk_->instructions()->length();
835 while (current != NULL && !is_aborted()) {
836 // Code for constants in registers is generated lazily.
837 if (!current->EmitAtUses()) {
838 VisitInstruction(current);
839 }
840 current = current->next();
841 }
842 int end = chunk_->instructions()->length() - 1;
843 if (end >= start) {
844 block->set_first_instruction_index(start);
845 block->set_last_instruction_index(end);
846 }
847 block->set_argument_count(argument_count_);
848 next_block_ = NULL;
849 current_block_ = NULL;
850}
851
852
853void LChunkBuilder::VisitInstruction(HInstruction* current) {
854 HInstruction* old_current = current_instruction_;
855 current_instruction_ = current;
856
857 LInstruction* instr = NULL;
858 if (current->CanReplaceWithDummyUses()) {
859 if (current->OperandCount() == 0) {
860 instr = DefineAsRegister(new(zone()) LDummy());
861 } else {
862 DCHECK(!current->OperandAt(0)->IsControlInstruction());
863 instr = DefineAsRegister(new(zone())
864 LDummyUse(UseAny(current->OperandAt(0))));
865 }
866 for (int i = 1; i < current->OperandCount(); ++i) {
867 if (current->OperandAt(i)->IsControlInstruction()) continue;
868 LInstruction* dummy =
869 new(zone()) LDummyUse(UseAny(current->OperandAt(i)));
870 dummy->set_hydrogen_value(current);
871 chunk_->AddInstruction(dummy, current_block_);
872 }
873 } else {
874 HBasicBlock* successor;
875 if (current->IsControlInstruction() &&
876 HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
877 successor != NULL) {
878 instr = new(zone()) LGoto(successor);
879 } else {
880 instr = current->CompileToLithium(this);
881 }
882 }
883
884 argument_count_ += current->argument_delta();
885 DCHECK(argument_count_ >= 0);
886
887 if (instr != NULL) {
888 AddInstruction(instr, current);
889 }
890
891 current_instruction_ = old_current;
892}
893
894
895void LChunkBuilder::AddInstruction(LInstruction* instr,
896 HInstruction* hydrogen_val) {
897 // Associate the hydrogen instruction first, since we may need it for
898 // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
899 instr->set_hydrogen_value(hydrogen_val);
900
901#if DEBUG
902 // Make sure that the lithium instruction has either no fixed register
903 // constraints in temps or the result OR no uses that are only used at
904 // start. If this invariant doesn't hold, the register allocator can decide
905 // to insert a split of a range immediately before the instruction due to an
906 // already allocated register needing to be used for the instruction's fixed
907 // register constraint. In this case, The register allocator won't see an
908 // interference between the split child and the use-at-start (it would if
909 // the it was just a plain use), so it is free to move the split child into
910 // the same register that is used for the use-at-start.
911 // See https://code.google.com/p/chromium/issues/detail?id=201590
912 if (!(instr->ClobbersRegisters() &&
913 instr->ClobbersDoubleRegisters(isolate()))) {
914 int fixed = 0;
915 int used_at_start = 0;
916 for (UseIterator it(instr); !it.Done(); it.Advance()) {
917 LUnallocated* operand = LUnallocated::cast(it.Current());
918 if (operand->IsUsedAtStart()) ++used_at_start;
919 }
920 if (instr->Output() != NULL) {
921 if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
922 }
923 for (TempIterator it(instr); !it.Done(); it.Advance()) {
924 LUnallocated* operand = LUnallocated::cast(it.Current());
925 if (operand->HasFixedPolicy()) ++fixed;
926 }
927 DCHECK(fixed == 0 || used_at_start == 0);
928 }
929#endif
930
931 if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
932 instr = AssignPointerMap(instr);
933 }
934 if (FLAG_stress_environments && !instr->HasEnvironment()) {
935 instr = AssignEnvironment(instr);
936 }
937 chunk_->AddInstruction(instr, current_block_);
938
939 if (instr->IsCall() || instr->IsPrologue()) {
940 HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
941 if (hydrogen_val->HasObservableSideEffects()) {
942 HSimulate* sim = HSimulate::cast(hydrogen_val->next());
943 sim->ReplayEnvironment(current_block_->last_environment());
944 hydrogen_value_for_lazy_bailout = sim;
945 }
946 LInstruction* bailout = AssignEnvironment(new(zone()) LLazyBailout());
947 bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
948 chunk_->AddInstruction(bailout, current_block_);
949 }
950}
951
952
953LInstruction* LChunkBuilder::DoPrologue(HPrologue* instr) {
954 return new (zone()) LPrologue();
955}
956
957
958LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
959 return new(zone()) LGoto(instr->FirstSuccessor());
960}
961
962
963LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
964 HValue* value = instr->value();
965 Representation r = value->representation();
966 HType type = value->type();
967 ToBooleanStub::Types expected = instr->expected_input_types();
968 if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
969
970 bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
971 type.IsJSArray() || type.IsHeapNumber() || type.IsString();
972 LOperand* temp = !easy_case && expected.NeedsMap() ? TempRegister() : NULL;
973 LInstruction* branch = new(zone()) LBranch(UseRegister(value), temp);
974 if (!easy_case &&
975 ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
976 !expected.IsGeneric())) {
977 branch = AssignEnvironment(branch);
978 }
979 return branch;
980}
981
982
983LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
984 return new(zone()) LDebugBreak();
985}
986
987
988LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
989 DCHECK(instr->value()->representation().IsTagged());
990 LOperand* value = UseRegisterAtStart(instr->value());
991 return new(zone()) LCmpMapAndBranch(value);
992}
993
994
995LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
996 info()->MarkAsRequiresFrame();
997 return DefineAsRegister(new(zone()) LArgumentsLength(Use(length->value())));
998}
999
1000
1001LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1002 info()->MarkAsRequiresFrame();
1003 return DefineAsRegister(new(zone()) LArgumentsElements);
1004}
1005
1006
1007LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1008 LOperand* left =
1009 UseFixed(instr->left(), InstanceOfDescriptor::LeftRegister());
1010 LOperand* right =
1011 UseFixed(instr->right(), InstanceOfDescriptor::RightRegister());
1012 LOperand* context = UseFixed(instr->context(), esi);
1013 LInstanceOf* result = new (zone()) LInstanceOf(context, left, right);
1014 return MarkAsCall(DefineFixed(result, eax), instr);
1015}
1016
1017
1018LInstruction* LChunkBuilder::DoHasInPrototypeChainAndBranch(
1019 HHasInPrototypeChainAndBranch* instr) {
1020 LOperand* object = UseRegister(instr->object());
1021 LOperand* prototype = UseRegister(instr->prototype());
1022 LOperand* temp = TempRegister();
1023 LHasInPrototypeChainAndBranch* result =
1024 new (zone()) LHasInPrototypeChainAndBranch(object, prototype, temp);
1025 return AssignEnvironment(result);
1026}
1027
1028
1029LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1030 LOperand* receiver = UseRegister(instr->receiver());
1031 LOperand* function = UseRegister(instr->function());
1032 LOperand* temp = TempRegister();
1033 LWrapReceiver* result =
1034 new(zone()) LWrapReceiver(receiver, function, temp);
1035 return AssignEnvironment(DefineSameAsFirst(result));
1036}
1037
1038
1039LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1040 LOperand* function = UseFixed(instr->function(), edi);
1041 LOperand* receiver = UseFixed(instr->receiver(), eax);
1042 LOperand* length = UseFixed(instr->length(), ebx);
1043 LOperand* elements = UseFixed(instr->elements(), ecx);
1044 LApplyArguments* result = new(zone()) LApplyArguments(function,
1045 receiver,
1046 length,
1047 elements);
1048 return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
1049}
1050
1051
1052LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1053 int argc = instr->OperandCount();
1054 for (int i = 0; i < argc; ++i) {
1055 LOperand* argument = UseAny(instr->argument(i));
1056 AddInstruction(new(zone()) LPushArgument(argument), instr);
1057 }
1058 return NULL;
1059}
1060
1061
1062LInstruction* LChunkBuilder::DoStoreCodeEntry(
1063 HStoreCodeEntry* store_code_entry) {
1064 LOperand* function = UseRegister(store_code_entry->function());
1065 LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1066 return new(zone()) LStoreCodeEntry(function, code_object);
1067}
1068
1069
1070LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1071 HInnerAllocatedObject* instr) {
1072 LOperand* base_object = UseRegisterAtStart(instr->base_object());
1073 LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1074 return DefineAsRegister(
1075 new(zone()) LInnerAllocatedObject(base_object, offset));
1076}
1077
1078
1079LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1080 return instr->HasNoUses()
1081 ? NULL
1082 : DefineAsRegister(new(zone()) LThisFunction);
1083}
1084
1085
1086LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1087 if (instr->HasNoUses()) return NULL;
1088
1089 if (info()->IsStub()) {
1090 return DefineFixed(new(zone()) LContext, esi);
1091 }
1092
1093 return DefineAsRegister(new(zone()) LContext);
1094}
1095
1096
1097LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1098 LOperand* context = UseFixed(instr->context(), esi);
1099 return MarkAsCall(new(zone()) LDeclareGlobals(context), instr);
1100}
1101
1102
1103LInstruction* LChunkBuilder::DoCallJSFunction(
1104 HCallJSFunction* instr) {
1105 LOperand* function = UseFixed(instr->function(), edi);
1106
1107 LCallJSFunction* result = new(zone()) LCallJSFunction(function);
1108
1109 return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1110}
1111
1112
1113LInstruction* LChunkBuilder::DoCallWithDescriptor(
1114 HCallWithDescriptor* instr) {
1115 CallInterfaceDescriptor descriptor = instr->descriptor();
1116 LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1117 ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1118 // Target
1119 ops.Add(target, zone());
1120 // Context
1121 LOperand* op = UseFixed(instr->OperandAt(1), esi);
1122 ops.Add(op, zone());
1123 // Other register parameters
1124 for (int i = LCallWithDescriptor::kImplicitRegisterParameterCount;
1125 i < instr->OperandCount(); i++) {
1126 op =
1127 UseFixed(instr->OperandAt(i),
1128 descriptor.GetRegisterParameter(
1129 i - LCallWithDescriptor::kImplicitRegisterParameterCount));
1130 ops.Add(op, zone());
1131 }
1132
1133 LCallWithDescriptor* result = new(zone()) LCallWithDescriptor(
1134 descriptor, ops, zone());
1135 return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1136}
1137
1138
1139LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1140 LOperand* context = UseFixed(instr->context(), esi);
1141 LOperand* function = UseFixed(instr->function(), edi);
1142 LInvokeFunction* result = new(zone()) LInvokeFunction(context, function);
1143 return MarkAsCall(DefineFixed(result, eax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1144}
1145
1146
1147LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1148 switch (instr->op()) {
1149 case kMathFloor:
1150 return DoMathFloor(instr);
1151 case kMathRound:
1152 return DoMathRound(instr);
1153 case kMathFround:
1154 return DoMathFround(instr);
1155 case kMathAbs:
1156 return DoMathAbs(instr);
1157 case kMathLog:
1158 return DoMathLog(instr);
1159 case kMathExp:
1160 return DoMathExp(instr);
1161 case kMathSqrt:
1162 return DoMathSqrt(instr);
1163 case kMathPowHalf:
1164 return DoMathPowHalf(instr);
1165 case kMathClz32:
1166 return DoMathClz32(instr);
1167 default:
1168 UNREACHABLE();
1169 return NULL;
1170 }
1171}
1172
1173
1174LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1175 LOperand* input = UseRegisterAtStart(instr->value());
1176 LMathFloor* result = new(zone()) LMathFloor(input);
1177 return AssignEnvironment(DefineAsRegister(result));
1178}
1179
1180
1181LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1182 LOperand* input = UseRegister(instr->value());
1183 LOperand* temp = FixedTemp(xmm4);
1184 LMathRound* result = new(zone()) LMathRound(input, temp);
1185 return AssignEnvironment(DefineAsRegister(result));
1186}
1187
1188
1189LInstruction* LChunkBuilder::DoMathFround(HUnaryMathOperation* instr) {
1190 LOperand* input = UseRegister(instr->value());
1191 LMathFround* result = new (zone()) LMathFround(input);
1192 return DefineAsRegister(result);
1193}
1194
1195
1196LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1197 LOperand* context = UseAny(instr->context()); // Deferred use.
1198 LOperand* input = UseRegisterAtStart(instr->value());
1199 LInstruction* result =
1200 DefineSameAsFirst(new(zone()) LMathAbs(context, input));
1201 Representation r = instr->value()->representation();
1202 if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1203 if (!r.IsDouble()) result = AssignEnvironment(result);
1204 return result;
1205}
1206
1207
1208LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1209 DCHECK(instr->representation().IsDouble());
1210 DCHECK(instr->value()->representation().IsDouble());
1211 LOperand* input = UseRegisterAtStart(instr->value());
1212 return MarkAsCall(DefineSameAsFirst(new(zone()) LMathLog(input)), instr);
1213}
1214
1215
1216LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1217 LOperand* input = UseRegisterAtStart(instr->value());
1218 LMathClz32* result = new(zone()) LMathClz32(input);
1219 return DefineAsRegister(result);
1220}
1221
1222
1223LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1224 DCHECK(instr->representation().IsDouble());
1225 DCHECK(instr->value()->representation().IsDouble());
1226 LOperand* value = UseTempRegister(instr->value());
1227 LOperand* temp1 = TempRegister();
1228 LOperand* temp2 = TempRegister();
1229 LMathExp* result = new(zone()) LMathExp(value, temp1, temp2);
1230 return DefineAsRegister(result);
1231}
1232
1233
1234LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1235 LOperand* input = UseAtStart(instr->value());
1236 return DefineAsRegister(new(zone()) LMathSqrt(input));
1237}
1238
1239
1240LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1241 LOperand* input = UseRegisterAtStart(instr->value());
1242 LOperand* temp = TempRegister();
1243 LMathPowHalf* result = new(zone()) LMathPowHalf(input, temp);
1244 return DefineSameAsFirst(result);
1245}
1246
1247
1248LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1249 LOperand* context = UseFixed(instr->context(), esi);
1250 LOperand* constructor = UseFixed(instr->constructor(), edi);
1251 LCallNewArray* result = new(zone()) LCallNewArray(context, constructor);
1252 return MarkAsCall(DefineFixed(result, eax), instr);
1253}
1254
1255
1256LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1257 LOperand* context = UseFixed(instr->context(), esi);
1258 LOperand* function = UseFixed(instr->function(), edi);
1259 LOperand* slot = NULL;
1260 LOperand* vector = NULL;
1261 if (instr->HasVectorAndSlot()) {
1262 slot = FixedTemp(edx);
1263 vector = FixedTemp(ebx);
1264 }
1265
1266 LCallFunction* call =
1267 new (zone()) LCallFunction(context, function, slot, vector);
1268 return MarkAsCall(DefineFixed(call, eax), instr);
1269}
1270
1271
1272LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1273 LOperand* context = UseFixed(instr->context(), esi);
1274 return MarkAsCall(DefineFixed(new(zone()) LCallRuntime(context), eax), instr);
1275}
1276
1277
1278LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1279 return DoShift(Token::ROR, instr);
1280}
1281
1282
1283LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1284 return DoShift(Token::SHR, instr);
1285}
1286
1287
1288LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1289 return DoShift(Token::SAR, instr);
1290}
1291
1292
1293LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1294 return DoShift(Token::SHL, instr);
1295}
1296
1297
1298LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1299 if (instr->representation().IsSmiOrInteger32()) {
1300 DCHECK(instr->left()->representation().Equals(instr->representation()));
1301 DCHECK(instr->right()->representation().Equals(instr->representation()));
1302 DCHECK(instr->CheckFlag(HValue::kTruncatingToInt32));
1303
1304 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1305 LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1306 return DefineSameAsFirst(new(zone()) LBitI(left, right));
1307 } else {
1308 return DoArithmeticT(instr->op(), instr);
1309 }
1310}
1311
1312
1313LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1314 DCHECK(instr->representation().IsSmiOrInteger32());
1315 DCHECK(instr->left()->representation().Equals(instr->representation()));
1316 DCHECK(instr->right()->representation().Equals(instr->representation()));
1317 LOperand* dividend = UseRegister(instr->left());
1318 int32_t divisor = instr->right()->GetInteger32Constant();
1319 LInstruction* result = DefineAsRegister(new(zone()) LDivByPowerOf2I(
1320 dividend, divisor));
1321 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1322 (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1323 (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1324 divisor != 1 && divisor != -1)) {
1325 result = AssignEnvironment(result);
1326 }
1327 return result;
1328}
1329
1330
1331LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1332 DCHECK(instr->representation().IsInteger32());
1333 DCHECK(instr->left()->representation().Equals(instr->representation()));
1334 DCHECK(instr->right()->representation().Equals(instr->representation()));
1335 LOperand* dividend = UseRegister(instr->left());
1336 int32_t divisor = instr->right()->GetInteger32Constant();
1337 LOperand* temp1 = FixedTemp(eax);
1338 LOperand* temp2 = FixedTemp(edx);
1339 LInstruction* result = DefineFixed(new(zone()) LDivByConstI(
1340 dividend, divisor, temp1, temp2), edx);
1341 if (divisor == 0 ||
1342 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1343 !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1344 result = AssignEnvironment(result);
1345 }
1346 return result;
1347}
1348
1349
1350LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1351 DCHECK(instr->representation().IsSmiOrInteger32());
1352 DCHECK(instr->left()->representation().Equals(instr->representation()));
1353 DCHECK(instr->right()->representation().Equals(instr->representation()));
1354 LOperand* dividend = UseFixed(instr->left(), eax);
1355 LOperand* divisor = UseRegister(instr->right());
1356 LOperand* temp = FixedTemp(edx);
1357 LInstruction* result = DefineFixed(new(zone()) LDivI(
1358 dividend, divisor, temp), eax);
1359 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1360 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1361 instr->CheckFlag(HValue::kCanOverflow) ||
1362 !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) {
1363 result = AssignEnvironment(result);
1364 }
1365 return result;
1366}
1367
1368
1369LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1370 if (instr->representation().IsSmiOrInteger32()) {
1371 if (instr->RightIsPowerOf2()) {
1372 return DoDivByPowerOf2I(instr);
1373 } else if (instr->right()->IsConstant()) {
1374 return DoDivByConstI(instr);
1375 } else {
1376 return DoDivI(instr);
1377 }
1378 } else if (instr->representation().IsDouble()) {
1379 return DoArithmeticD(Token::DIV, instr);
1380 } else {
1381 return DoArithmeticT(Token::DIV, instr);
1382 }
1383}
1384
1385
1386LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1387 LOperand* dividend = UseRegisterAtStart(instr->left());
1388 int32_t divisor = instr->right()->GetInteger32Constant();
1389 LInstruction* result = DefineSameAsFirst(new(zone()) LFlooringDivByPowerOf2I(
1390 dividend, divisor));
1391 if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1392 (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1393 result = AssignEnvironment(result);
1394 }
1395 return result;
1396}
1397
1398
1399LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1400 DCHECK(instr->representation().IsInteger32());
1401 DCHECK(instr->left()->representation().Equals(instr->representation()));
1402 DCHECK(instr->right()->representation().Equals(instr->representation()));
1403 LOperand* dividend = UseRegister(instr->left());
1404 int32_t divisor = instr->right()->GetInteger32Constant();
1405 LOperand* temp1 = FixedTemp(eax);
1406 LOperand* temp2 = FixedTemp(edx);
1407 LOperand* temp3 =
1408 ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1409 (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
1410 NULL : TempRegister();
1411 LInstruction* result =
1412 DefineFixed(new(zone()) LFlooringDivByConstI(dividend,
1413 divisor,
1414 temp1,
1415 temp2,
1416 temp3),
1417 edx);
1418 if (divisor == 0 ||
1419 (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1420 result = AssignEnvironment(result);
1421 }
1422 return result;
1423}
1424
1425
1426LInstruction* 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 = UseFixed(instr->left(), eax);
1431 LOperand* divisor = UseRegister(instr->right());
1432 LOperand* temp = FixedTemp(edx);
1433 LInstruction* result = DefineFixed(new(zone()) LFlooringDivI(
1434 dividend, divisor, temp), eax);
1435 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1436 instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1437 instr->CheckFlag(HValue::kCanOverflow)) {
1438 result = AssignEnvironment(result);
1439 }
1440 return result;
1441}
1442
1443
1444LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1445 if (instr->RightIsPowerOf2()) {
1446 return DoFlooringDivByPowerOf2I(instr);
1447 } else if (instr->right()->IsConstant()) {
1448 return DoFlooringDivByConstI(instr);
1449 } else {
1450 return DoFlooringDivI(instr);
1451 }
1452}
1453
1454
1455LInstruction* 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 = DefineSameAsFirst(new(zone()) LModByPowerOf2I(
1462 dividend, divisor));
1463 if (instr->CheckFlag(HValue::kLeftCanBeNegative) &&
1464 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1465 result = AssignEnvironment(result);
1466 }
1467 return result;
1468}
1469
1470
1471LInstruction* 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 LOperand* temp1 = FixedTemp(eax);
1478 LOperand* temp2 = FixedTemp(edx);
1479 LInstruction* result = DefineFixed(new(zone()) LModByConstI(
1480 dividend, divisor, temp1, temp2), eax);
1481 if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1482 result = AssignEnvironment(result);
1483 }
1484 return result;
1485}
1486
1487
1488LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1489 DCHECK(instr->representation().IsSmiOrInteger32());
1490 DCHECK(instr->left()->representation().Equals(instr->representation()));
1491 DCHECK(instr->right()->representation().Equals(instr->representation()));
1492 LOperand* dividend = UseFixed(instr->left(), eax);
1493 LOperand* divisor = UseRegister(instr->right());
1494 LOperand* temp = FixedTemp(edx);
1495 LInstruction* result = DefineFixed(new(zone()) LModI(
1496 dividend, divisor, temp), edx);
1497 if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1498 instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1499 result = AssignEnvironment(result);
1500 }
1501 return result;
1502}
1503
1504
1505LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1506 if (instr->representation().IsSmiOrInteger32()) {
1507 if (instr->RightIsPowerOf2()) {
1508 return DoModByPowerOf2I(instr);
1509 } else if (instr->right()->IsConstant()) {
1510 return DoModByConstI(instr);
1511 } else {
1512 return DoModI(instr);
1513 }
1514 } else if (instr->representation().IsDouble()) {
1515 return DoArithmeticD(Token::MOD, instr);
1516 } else {
1517 return DoArithmeticT(Token::MOD, instr);
1518 }
1519}
1520
1521
1522LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1523 if (instr->representation().IsSmiOrInteger32()) {
1524 DCHECK(instr->left()->representation().Equals(instr->representation()));
1525 DCHECK(instr->right()->representation().Equals(instr->representation()));
1526 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001527 HValue* h_right = instr->BetterRightOperand();
1528 LOperand* right = UseOrConstant(h_right);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001529 LOperand* temp = NULL;
1530 if (instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1531 temp = TempRegister();
1532 }
1533 LMulI* mul = new(zone()) LMulI(left, right, temp);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001534 int constant_value =
1535 h_right->IsConstant() ? HConstant::cast(h_right)->Integer32Value() : 0;
1536 // |needs_environment| must mirror the cases where LCodeGen::DoMulI calls
1537 // |DeoptimizeIf|.
1538 bool needs_environment =
1539 instr->CheckFlag(HValue::kCanOverflow) ||
1540 (instr->CheckFlag(HValue::kBailoutOnMinusZero) &&
1541 (!right->IsConstantOperand() || constant_value <= 0));
1542 if (needs_environment) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543 AssignEnvironment(mul);
1544 }
1545 return DefineSameAsFirst(mul);
1546 } else if (instr->representation().IsDouble()) {
1547 return DoArithmeticD(Token::MUL, instr);
1548 } else {
1549 return DoArithmeticT(Token::MUL, instr);
1550 }
1551}
1552
1553
1554LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1555 if (instr->representation().IsSmiOrInteger32()) {
1556 DCHECK(instr->left()->representation().Equals(instr->representation()));
1557 DCHECK(instr->right()->representation().Equals(instr->representation()));
1558 LOperand* left = UseRegisterAtStart(instr->left());
1559 LOperand* right = UseOrConstantAtStart(instr->right());
1560 LSubI* sub = new(zone()) LSubI(left, right);
1561 LInstruction* result = DefineSameAsFirst(sub);
1562 if (instr->CheckFlag(HValue::kCanOverflow)) {
1563 result = AssignEnvironment(result);
1564 }
1565 return result;
1566 } else if (instr->representation().IsDouble()) {
1567 return DoArithmeticD(Token::SUB, instr);
1568 } else {
1569 return DoArithmeticT(Token::SUB, instr);
1570 }
1571}
1572
1573
1574LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1575 if (instr->representation().IsSmiOrInteger32()) {
1576 DCHECK(instr->left()->representation().Equals(instr->representation()));
1577 DCHECK(instr->right()->representation().Equals(instr->representation()));
1578 // Check to see if it would be advantageous to use an lea instruction rather
1579 // than an add. This is the case when no overflow check is needed and there
1580 // are multiple uses of the add's inputs, so using a 3-register add will
1581 // preserve all input values for later uses.
1582 bool use_lea = LAddI::UseLea(instr);
1583 LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1584 HValue* right_candidate = instr->BetterRightOperand();
1585 LOperand* right = use_lea
1586 ? UseRegisterOrConstantAtStart(right_candidate)
1587 : UseOrConstantAtStart(right_candidate);
1588 LAddI* add = new(zone()) LAddI(left, right);
1589 bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1590 LInstruction* result = use_lea
1591 ? DefineAsRegister(add)
1592 : DefineSameAsFirst(add);
1593 if (can_overflow) {
1594 result = AssignEnvironment(result);
1595 }
1596 return result;
1597 } else if (instr->representation().IsDouble()) {
1598 return DoArithmeticD(Token::ADD, instr);
1599 } else if (instr->representation().IsExternal()) {
1600 DCHECK(instr->IsConsistentExternalRepresentation());
1601 DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1602 bool use_lea = LAddI::UseLea(instr);
1603 LOperand* left = UseRegisterAtStart(instr->left());
1604 HValue* right_candidate = instr->right();
1605 LOperand* right = use_lea
1606 ? UseRegisterOrConstantAtStart(right_candidate)
1607 : UseOrConstantAtStart(right_candidate);
1608 LAddI* add = new(zone()) LAddI(left, right);
1609 LInstruction* result = use_lea
1610 ? DefineAsRegister(add)
1611 : DefineSameAsFirst(add);
1612 return result;
1613 } else {
1614 return DoArithmeticT(Token::ADD, instr);
1615 }
1616}
1617
1618
1619LInstruction* LChunkBuilder::DoMathMinMax(HMathMinMax* instr) {
1620 LOperand* left = NULL;
1621 LOperand* right = NULL;
1622 if (instr->representation().IsSmiOrInteger32()) {
1623 DCHECK(instr->left()->representation().Equals(instr->representation()));
1624 DCHECK(instr->right()->representation().Equals(instr->representation()));
1625 left = UseRegisterAtStart(instr->BetterLeftOperand());
1626 right = UseOrConstantAtStart(instr->BetterRightOperand());
1627 } else {
1628 DCHECK(instr->representation().IsDouble());
1629 DCHECK(instr->left()->representation().IsDouble());
1630 DCHECK(instr->right()->representation().IsDouble());
1631 left = UseRegisterAtStart(instr->left());
1632 right = UseRegisterAtStart(instr->right());
1633 }
1634 LMathMinMax* minmax = new(zone()) LMathMinMax(left, right);
1635 return DefineSameAsFirst(minmax);
1636}
1637
1638
1639LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1640 DCHECK(instr->representation().IsDouble());
1641 // We call a C function for double power. It can't trigger a GC.
1642 // We need to use fixed result register for the call.
1643 Representation exponent_type = instr->right()->representation();
1644 DCHECK(instr->left()->representation().IsDouble());
1645 LOperand* left = UseFixedDouble(instr->left(), xmm2);
1646 LOperand* right =
1647 exponent_type.IsDouble()
1648 ? UseFixedDouble(instr->right(), xmm1)
1649 : UseFixed(instr->right(), MathPowTaggedDescriptor::exponent());
1650 LPower* result = new(zone()) LPower(left, right);
1651 return MarkAsCall(DefineFixedDouble(result, xmm3), instr,
1652 CAN_DEOPTIMIZE_EAGERLY);
1653}
1654
1655
1656LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1657 DCHECK(instr->left()->representation().IsSmiOrTagged());
1658 DCHECK(instr->right()->representation().IsSmiOrTagged());
1659 LOperand* context = UseFixed(instr->context(), esi);
1660 LOperand* left = UseFixed(instr->left(), edx);
1661 LOperand* right = UseFixed(instr->right(), eax);
1662 LCmpT* result = new(zone()) LCmpT(context, left, right);
1663 return MarkAsCall(DefineFixed(result, eax), instr);
1664}
1665
1666
1667LInstruction* LChunkBuilder::DoCompareNumericAndBranch(
1668 HCompareNumericAndBranch* instr) {
1669 Representation r = instr->representation();
1670 if (r.IsSmiOrInteger32()) {
1671 DCHECK(instr->left()->representation().Equals(r));
1672 DCHECK(instr->right()->representation().Equals(r));
1673 LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1674 LOperand* right = UseOrConstantAtStart(instr->right());
1675 return new(zone()) LCompareNumericAndBranch(left, right);
1676 } else {
1677 DCHECK(r.IsDouble());
1678 DCHECK(instr->left()->representation().IsDouble());
1679 DCHECK(instr->right()->representation().IsDouble());
1680 LOperand* left;
1681 LOperand* right;
1682 if (CanBeImmediateConstant(instr->left()) &&
1683 CanBeImmediateConstant(instr->right())) {
1684 // The code generator requires either both inputs to be constant
1685 // operands, or neither.
1686 left = UseConstant(instr->left());
1687 right = UseConstant(instr->right());
1688 } else {
1689 left = UseRegisterAtStart(instr->left());
1690 right = UseRegisterAtStart(instr->right());
1691 }
1692 return new(zone()) LCompareNumericAndBranch(left, right);
1693 }
1694}
1695
1696
1697LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1698 HCompareObjectEqAndBranch* instr) {
1699 LOperand* left = UseRegisterAtStart(instr->left());
1700 LOperand* right = UseOrConstantAtStart(instr->right());
1701 return new(zone()) LCmpObjectEqAndBranch(left, right);
1702}
1703
1704
1705LInstruction* LChunkBuilder::DoCompareHoleAndBranch(
1706 HCompareHoleAndBranch* instr) {
1707 LOperand* value = UseRegisterAtStart(instr->value());
1708 return new(zone()) LCmpHoleAndBranch(value);
1709}
1710
1711
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001712LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1713 DCHECK(instr->value()->representation().IsTagged());
1714 LOperand* temp = TempRegister();
1715 return new(zone()) LIsStringAndBranch(UseRegister(instr->value()), temp);
1716}
1717
1718
1719LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1720 DCHECK(instr->value()->representation().IsTagged());
1721 return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1722}
1723
1724
1725LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1726 HIsUndetectableAndBranch* instr) {
1727 DCHECK(instr->value()->representation().IsTagged());
1728 return new(zone()) LIsUndetectableAndBranch(
1729 UseRegisterAtStart(instr->value()), TempRegister());
1730}
1731
1732
1733LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1734 HStringCompareAndBranch* instr) {
1735 DCHECK(instr->left()->representation().IsTagged());
1736 DCHECK(instr->right()->representation().IsTagged());
1737 LOperand* context = UseFixed(instr->context(), esi);
1738 LOperand* left = UseFixed(instr->left(), edx);
1739 LOperand* right = UseFixed(instr->right(), eax);
1740
1741 LStringCompareAndBranch* result = new(zone())
1742 LStringCompareAndBranch(context, left, right);
1743
1744 return MarkAsCall(result, instr);
1745}
1746
1747
1748LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1749 HHasInstanceTypeAndBranch* instr) {
1750 DCHECK(instr->value()->representation().IsTagged());
1751 return new(zone()) LHasInstanceTypeAndBranch(
1752 UseRegisterAtStart(instr->value()),
1753 TempRegister());
1754}
1755
1756
1757LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1758 HGetCachedArrayIndex* instr) {
1759 DCHECK(instr->value()->representation().IsTagged());
1760 LOperand* value = UseRegisterAtStart(instr->value());
1761
1762 return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1763}
1764
1765
1766LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1767 HHasCachedArrayIndexAndBranch* instr) {
1768 DCHECK(instr->value()->representation().IsTagged());
1769 return new(zone()) LHasCachedArrayIndexAndBranch(
1770 UseRegisterAtStart(instr->value()));
1771}
1772
1773
1774LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1775 HClassOfTestAndBranch* instr) {
1776 DCHECK(instr->value()->representation().IsTagged());
1777 return new(zone()) LClassOfTestAndBranch(UseRegister(instr->value()),
1778 TempRegister(),
1779 TempRegister());
1780}
1781
1782
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001783LInstruction* LChunkBuilder::DoSeqStringGetChar(HSeqStringGetChar* instr) {
1784 LOperand* string = UseRegisterAtStart(instr->string());
1785 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1786 return DefineAsRegister(new(zone()) LSeqStringGetChar(string, index));
1787}
1788
1789
1790LOperand* LChunkBuilder::GetSeqStringSetCharOperand(HSeqStringSetChar* instr) {
1791 if (instr->encoding() == String::ONE_BYTE_ENCODING) {
1792 if (FLAG_debug_code) {
1793 return UseFixed(instr->value(), eax);
1794 } else {
1795 return UseFixedOrConstant(instr->value(), eax);
1796 }
1797 } else {
1798 if (FLAG_debug_code) {
1799 return UseRegisterAtStart(instr->value());
1800 } else {
1801 return UseRegisterOrConstantAtStart(instr->value());
1802 }
1803 }
1804}
1805
1806
1807LInstruction* LChunkBuilder::DoSeqStringSetChar(HSeqStringSetChar* instr) {
1808 LOperand* string = UseRegisterAtStart(instr->string());
1809 LOperand* index = FLAG_debug_code
1810 ? UseRegisterAtStart(instr->index())
1811 : UseRegisterOrConstantAtStart(instr->index());
1812 LOperand* value = GetSeqStringSetCharOperand(instr);
1813 LOperand* context = FLAG_debug_code ? UseFixed(instr->context(), esi) : NULL;
1814 LInstruction* result = new(zone()) LSeqStringSetChar(context, string,
1815 index, value);
1816 if (FLAG_debug_code) {
1817 result = MarkAsCall(result, instr);
1818 }
1819 return result;
1820}
1821
1822
1823LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1824 if (!FLAG_debug_code && instr->skip_check()) return NULL;
1825 LOperand* index = UseRegisterOrConstantAtStart(instr->index());
1826 LOperand* length = !index->IsConstantOperand()
1827 ? UseOrConstantAtStart(instr->length())
1828 : UseAtStart(instr->length());
1829 LInstruction* result = new(zone()) LBoundsCheck(index, length);
1830 if (!FLAG_debug_code || !instr->skip_check()) {
1831 result = AssignEnvironment(result);
1832 }
1833 return result;
1834}
1835
1836
1837LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1838 HBoundsCheckBaseIndexInformation* instr) {
1839 UNREACHABLE();
1840 return NULL;
1841}
1842
1843
1844LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1845 // The control instruction marking the end of a block that completed
1846 // abruptly (e.g., threw an exception). There is nothing specific to do.
1847 return NULL;
1848}
1849
1850
1851LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1852 return NULL;
1853}
1854
1855
1856LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1857 // All HForceRepresentation instructions should be eliminated in the
1858 // representation change phase of Hydrogen.
1859 UNREACHABLE();
1860 return NULL;
1861}
1862
1863
1864LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1865 Representation from = instr->from();
1866 Representation to = instr->to();
1867 HValue* val = instr->value();
1868 if (from.IsSmi()) {
1869 if (to.IsTagged()) {
1870 LOperand* value = UseRegister(val);
1871 return DefineSameAsFirst(new(zone()) LDummyUse(value));
1872 }
1873 from = Representation::Tagged();
1874 }
1875 if (from.IsTagged()) {
1876 if (to.IsDouble()) {
1877 LOperand* value = UseRegister(val);
1878 LOperand* temp = TempRegister();
1879 LInstruction* result =
1880 DefineAsRegister(new(zone()) LNumberUntagD(value, temp));
1881 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1882 return result;
1883 } else if (to.IsSmi()) {
1884 LOperand* value = UseRegister(val);
1885 if (val->type().IsSmi()) {
1886 return DefineSameAsFirst(new(zone()) LDummyUse(value));
1887 }
1888 return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
1889 } else {
1890 DCHECK(to.IsInteger32());
1891 if (val->type().IsSmi() || val->representation().IsSmi()) {
1892 LOperand* value = UseRegister(val);
1893 return DefineSameAsFirst(new(zone()) LSmiUntag(value, false));
1894 } else {
1895 LOperand* value = UseRegister(val);
1896 bool truncating = instr->CanTruncateToInt32();
1897 LOperand* xmm_temp = !truncating ? FixedTemp(xmm1) : NULL;
1898 LInstruction* result =
1899 DefineSameAsFirst(new(zone()) LTaggedToI(value, xmm_temp));
1900 if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1901 return result;
1902 }
1903 }
1904 } else if (from.IsDouble()) {
1905 if (to.IsTagged()) {
1906 info()->MarkAsDeferredCalling();
1907 LOperand* value = UseRegisterAtStart(val);
1908 LOperand* temp = FLAG_inline_new ? TempRegister() : NULL;
1909 LUnallocated* result_temp = TempRegister();
1910 LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
1911 return AssignPointerMap(Define(result, result_temp));
1912 } else if (to.IsSmi()) {
1913 LOperand* value = UseRegister(val);
1914 return AssignEnvironment(
1915 DefineAsRegister(new(zone()) LDoubleToSmi(value)));
1916 } else {
1917 DCHECK(to.IsInteger32());
1918 bool truncating = instr->CanTruncateToInt32();
1919 bool needs_temp = !truncating;
1920 LOperand* value = needs_temp ? UseTempRegister(val) : UseRegister(val);
1921 LOperand* temp = needs_temp ? TempRegister() : NULL;
1922 LInstruction* result =
1923 DefineAsRegister(new(zone()) LDoubleToI(value, temp));
1924 if (!truncating) result = AssignEnvironment(result);
1925 return result;
1926 }
1927 } else if (from.IsInteger32()) {
1928 info()->MarkAsDeferredCalling();
1929 if (to.IsTagged()) {
1930 LOperand* value = UseRegister(val);
1931 if (!instr->CheckFlag(HValue::kCanOverflow)) {
1932 return DefineSameAsFirst(new(zone()) LSmiTag(value));
1933 } else if (val->CheckFlag(HInstruction::kUint32)) {
1934 LOperand* temp = TempRegister();
1935 LNumberTagU* result = new(zone()) LNumberTagU(value, temp);
1936 return AssignPointerMap(DefineSameAsFirst(result));
1937 } else {
1938 LOperand* temp = TempRegister();
1939 LNumberTagI* result = new(zone()) LNumberTagI(value, temp);
1940 return AssignPointerMap(DefineSameAsFirst(result));
1941 }
1942 } else if (to.IsSmi()) {
1943 LOperand* value = UseRegister(val);
1944 LInstruction* result = DefineSameAsFirst(new(zone()) LSmiTag(value));
1945 if (instr->CheckFlag(HValue::kCanOverflow)) {
1946 result = AssignEnvironment(result);
1947 }
1948 return result;
1949 } else {
1950 DCHECK(to.IsDouble());
1951 if (val->CheckFlag(HInstruction::kUint32)) {
1952 return DefineAsRegister(new(zone()) LUint32ToDouble(UseRegister(val)));
1953 } else {
1954 return DefineAsRegister(new(zone()) LInteger32ToDouble(Use(val)));
1955 }
1956 }
1957 }
1958 UNREACHABLE();
1959 return NULL;
1960}
1961
1962
1963LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
1964 LOperand* value = UseAtStart(instr->value());
1965 LInstruction* result = new(zone()) LCheckNonSmi(value);
1966 if (!instr->value()->type().IsHeapObject()) {
1967 result = AssignEnvironment(result);
1968 }
1969 return result;
1970}
1971
1972
1973LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1974 LOperand* value = UseRegisterAtStart(instr->value());
1975 return AssignEnvironment(new(zone()) LCheckSmi(value));
1976}
1977
1978
1979LInstruction* LChunkBuilder::DoCheckArrayBufferNotNeutered(
1980 HCheckArrayBufferNotNeutered* instr) {
1981 LOperand* view = UseRegisterAtStart(instr->value());
1982 LOperand* scratch = TempRegister();
1983 LCheckArrayBufferNotNeutered* result =
1984 new (zone()) LCheckArrayBufferNotNeutered(view, scratch);
1985 return AssignEnvironment(result);
1986}
1987
1988
1989LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1990 LOperand* value = UseRegisterAtStart(instr->value());
1991 LOperand* temp = TempRegister();
1992 LCheckInstanceType* result = new(zone()) LCheckInstanceType(value, temp);
1993 return AssignEnvironment(result);
1994}
1995
1996
1997LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
1998 // If the object is in new space, we'll emit a global cell compare and so
1999 // want the value in a register. If the object gets promoted before we
2000 // emit code, we will still get the register but will do an immediate
2001 // compare instead of the cell compare. This is safe.
2002 LOperand* value = instr->object_in_new_space()
2003 ? UseRegisterAtStart(instr->value()) : UseAtStart(instr->value());
2004 return AssignEnvironment(new(zone()) LCheckValue(value));
2005}
2006
2007
2008LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2009 if (instr->IsStabilityCheck()) return new(zone()) LCheckMaps;
2010 LOperand* value = UseRegisterAtStart(instr->value());
2011 LInstruction* result = AssignEnvironment(new(zone()) LCheckMaps(value));
2012 if (instr->HasMigrationTarget()) {
2013 info()->MarkAsDeferredCalling();
2014 result = AssignPointerMap(result);
2015 }
2016 return result;
2017}
2018
2019
2020LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2021 HValue* value = instr->value();
2022 Representation input_rep = value->representation();
2023 if (input_rep.IsDouble()) {
2024 LOperand* reg = UseRegister(value);
2025 return DefineFixed(new(zone()) LClampDToUint8(reg), eax);
2026 } else if (input_rep.IsInteger32()) {
2027 LOperand* reg = UseFixed(value, eax);
2028 return DefineFixed(new(zone()) LClampIToUint8(reg), eax);
2029 } else {
2030 DCHECK(input_rep.IsSmiOrTagged());
2031 LOperand* reg = UseFixed(value, eax);
2032 // Register allocator doesn't (yet) support allocation of double
2033 // temps. Reserve xmm1 explicitly.
2034 LOperand* temp = FixedTemp(xmm1);
2035 LClampTToUint8* result = new(zone()) LClampTToUint8(reg, temp);
2036 return AssignEnvironment(DefineFixed(result, eax));
2037 }
2038}
2039
2040
2041LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2042 HValue* value = instr->value();
2043 DCHECK(value->representation().IsDouble());
2044 return DefineAsRegister(new(zone()) LDoubleBits(UseRegister(value)));
2045}
2046
2047
2048LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2049 LOperand* lo = UseRegister(instr->lo());
2050 LOperand* hi = UseRegister(instr->hi());
2051 return DefineAsRegister(new(zone()) LConstructDouble(hi, lo));
2052}
2053
2054
2055LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2056 LOperand* context = info()->IsStub() ? UseFixed(instr->context(), esi) : NULL;
2057 LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2058 return new(zone()) LReturn(
2059 UseFixed(instr->value(), eax), context, parameter_count);
2060}
2061
2062
2063LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2064 Representation r = instr->representation();
2065 if (r.IsSmi()) {
2066 return DefineAsRegister(new(zone()) LConstantS);
2067 } else if (r.IsInteger32()) {
2068 return DefineAsRegister(new(zone()) LConstantI);
2069 } else if (r.IsDouble()) {
2070 uint64_t const bits = instr->DoubleValueAsBits();
2071 LOperand* temp = bits ? TempRegister() : nullptr;
2072 return DefineAsRegister(new(zone()) LConstantD(temp));
2073 } else if (r.IsExternal()) {
2074 return DefineAsRegister(new(zone()) LConstantE);
2075 } else if (r.IsTagged()) {
2076 return DefineAsRegister(new(zone()) LConstantT);
2077 } else {
2078 UNREACHABLE();
2079 return NULL;
2080 }
2081}
2082
2083
2084LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2085 LOperand* context = UseFixed(instr->context(), esi);
2086 LOperand* global_object =
2087 UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2088 LOperand* vector = NULL;
2089 if (instr->HasVectorAndSlot()) {
2090 vector = FixedTemp(LoadWithVectorDescriptor::VectorRegister());
2091 }
2092
2093 LLoadGlobalGeneric* result =
2094 new(zone()) LLoadGlobalGeneric(context, global_object, vector);
2095 return MarkAsCall(DefineFixed(result, eax), instr);
2096}
2097
2098
2099LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2100 LOperand* context = UseRegisterAtStart(instr->value());
2101 LInstruction* result =
2102 DefineAsRegister(new(zone()) LLoadContextSlot(context));
2103 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2104 result = AssignEnvironment(result);
2105 }
2106 return result;
2107}
2108
2109
2110LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2111 LOperand* value;
2112 LOperand* temp;
2113 LOperand* context = UseRegister(instr->context());
2114 if (instr->NeedsWriteBarrier()) {
2115 value = UseTempRegister(instr->value());
2116 temp = TempRegister();
2117 } else {
2118 value = UseRegister(instr->value());
2119 temp = NULL;
2120 }
2121 LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
2122 if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2123 result = AssignEnvironment(result);
2124 }
2125 return result;
2126}
2127
2128
2129LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2130 LOperand* obj = (instr->access().IsExternalMemory() &&
2131 instr->access().offset() == 0)
2132 ? UseRegisterOrConstantAtStart(instr->object())
2133 : UseRegisterAtStart(instr->object());
2134 return DefineAsRegister(new(zone()) LLoadNamedField(obj));
2135}
2136
2137
2138LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2139 LOperand* context = UseFixed(instr->context(), esi);
2140 LOperand* object =
2141 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2142 LOperand* vector = NULL;
2143 if (instr->HasVectorAndSlot()) {
2144 vector = FixedTemp(LoadWithVectorDescriptor::VectorRegister());
2145 }
2146 LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(
2147 context, object, vector);
2148 return MarkAsCall(DefineFixed(result, eax), instr);
2149}
2150
2151
2152LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2153 HLoadFunctionPrototype* instr) {
2154 return AssignEnvironment(DefineAsRegister(
2155 new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()),
2156 TempRegister())));
2157}
2158
2159
2160LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2161 return DefineAsRegister(new(zone()) LLoadRoot);
2162}
2163
2164
2165LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2166 DCHECK(instr->key()->representation().IsSmiOrInteger32());
2167 ElementsKind elements_kind = instr->elements_kind();
2168 bool clobbers_key = ExternalArrayOpRequiresTemp(
2169 instr->key()->representation(), elements_kind);
2170 LOperand* key = clobbers_key
2171 ? UseTempRegister(instr->key())
2172 : UseRegisterOrConstantAtStart(instr->key());
2173 LInstruction* result = NULL;
2174
2175 if (!instr->is_fixed_typed_array()) {
2176 LOperand* obj = UseRegisterAtStart(instr->elements());
2177 result = DefineAsRegister(new (zone()) LLoadKeyed(obj, key, nullptr));
2178 } else {
2179 DCHECK(
2180 (instr->representation().IsInteger32() &&
2181 !(IsDoubleOrFloatElementsKind(instr->elements_kind()))) ||
2182 (instr->representation().IsDouble() &&
2183 (IsDoubleOrFloatElementsKind(instr->elements_kind()))));
2184 LOperand* backing_store = UseRegister(instr->elements());
2185 LOperand* backing_store_owner = UseAny(instr->backing_store_owner());
2186 result = DefineAsRegister(
2187 new (zone()) LLoadKeyed(backing_store, key, backing_store_owner));
2188 }
2189
2190 bool needs_environment;
2191 if (instr->is_fixed_typed_array()) {
2192 // see LCodeGen::DoLoadKeyedExternalArray
2193 needs_environment = elements_kind == UINT32_ELEMENTS &&
2194 !instr->CheckFlag(HInstruction::kUint32);
2195 } else {
2196 // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2197 // LCodeGen::DoLoadKeyedFixedArray
2198 needs_environment =
2199 instr->RequiresHoleCheck() ||
2200 (instr->hole_mode() == CONVERT_HOLE_TO_UNDEFINED && info()->IsStub());
2201 }
2202
2203 if (needs_environment) {
2204 result = AssignEnvironment(result);
2205 }
2206 return result;
2207}
2208
2209
2210LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2211 LOperand* context = UseFixed(instr->context(), esi);
2212 LOperand* object =
2213 UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2214 LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2215 LOperand* vector = NULL;
2216 if (instr->HasVectorAndSlot()) {
2217 vector = FixedTemp(LoadWithVectorDescriptor::VectorRegister());
2218 }
2219 LLoadKeyedGeneric* result =
2220 new(zone()) LLoadKeyedGeneric(context, object, key, vector);
2221 return MarkAsCall(DefineFixed(result, eax), instr);
2222}
2223
2224
2225LOperand* LChunkBuilder::GetStoreKeyedValueOperand(HStoreKeyed* instr) {
2226 ElementsKind elements_kind = instr->elements_kind();
2227
2228 // Determine if we need a byte register in this case for the value.
2229 bool val_is_fixed_register =
2230 elements_kind == UINT8_ELEMENTS ||
2231 elements_kind == INT8_ELEMENTS ||
2232 elements_kind == UINT8_CLAMPED_ELEMENTS;
2233 if (val_is_fixed_register) {
2234 return UseFixed(instr->value(), eax);
2235 }
2236
2237 return UseRegister(instr->value());
2238}
2239
2240
2241LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2242 if (!instr->is_fixed_typed_array()) {
2243 DCHECK(instr->elements()->representation().IsTagged());
2244 DCHECK(instr->key()->representation().IsInteger32() ||
2245 instr->key()->representation().IsSmi());
2246
2247 if (instr->value()->representation().IsDouble()) {
2248 LOperand* object = UseRegisterAtStart(instr->elements());
2249 LOperand* val = NULL;
2250 val = UseRegisterAtStart(instr->value());
2251 LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2252 return new (zone()) LStoreKeyed(object, key, val, nullptr);
2253 } else {
2254 DCHECK(instr->value()->representation().IsSmiOrTagged());
2255 bool needs_write_barrier = instr->NeedsWriteBarrier();
2256
2257 LOperand* obj = UseRegister(instr->elements());
2258 LOperand* val;
2259 LOperand* key;
2260 if (needs_write_barrier) {
2261 val = UseTempRegister(instr->value());
2262 key = UseTempRegister(instr->key());
2263 } else {
2264 val = UseRegisterOrConstantAtStart(instr->value());
2265 key = UseRegisterOrConstantAtStart(instr->key());
2266 }
2267 return new (zone()) LStoreKeyed(obj, key, val, nullptr);
2268 }
2269 }
2270
2271 ElementsKind elements_kind = instr->elements_kind();
2272 DCHECK(
2273 (instr->value()->representation().IsInteger32() &&
2274 !IsDoubleOrFloatElementsKind(elements_kind)) ||
2275 (instr->value()->representation().IsDouble() &&
2276 IsDoubleOrFloatElementsKind(elements_kind)));
2277 DCHECK(instr->elements()->representation().IsExternal());
2278
2279 LOperand* backing_store = UseRegister(instr->elements());
2280 LOperand* backing_store_owner = UseAny(instr->backing_store_owner());
2281 LOperand* val = GetStoreKeyedValueOperand(instr);
2282 bool clobbers_key = ExternalArrayOpRequiresTemp(
2283 instr->key()->representation(), elements_kind);
2284 LOperand* key = clobbers_key
2285 ? UseTempRegister(instr->key())
2286 : UseRegisterOrConstantAtStart(instr->key());
2287 return new (zone()) LStoreKeyed(backing_store, key, val, backing_store_owner);
2288}
2289
2290
2291LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2292 LOperand* context = UseFixed(instr->context(), esi);
2293 LOperand* object =
2294 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2295 LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2296 LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2297
2298 DCHECK(instr->object()->representation().IsTagged());
2299 DCHECK(instr->key()->representation().IsTagged());
2300 DCHECK(instr->value()->representation().IsTagged());
2301
2302 LOperand* slot = NULL;
2303 LOperand* vector = NULL;
2304 if (instr->HasVectorAndSlot()) {
2305 slot = FixedTemp(VectorStoreICDescriptor::SlotRegister());
2306 vector = FixedTemp(VectorStoreICDescriptor::VectorRegister());
2307 }
2308
2309 LStoreKeyedGeneric* result = new (zone())
2310 LStoreKeyedGeneric(context, object, key, value, slot, vector);
2311 return MarkAsCall(result, instr);
2312}
2313
2314
2315LInstruction* LChunkBuilder::DoTransitionElementsKind(
2316 HTransitionElementsKind* instr) {
2317 if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2318 LOperand* object = UseRegister(instr->object());
2319 LOperand* new_map_reg = TempRegister();
2320 LOperand* temp_reg = TempRegister();
2321 LTransitionElementsKind* result =
2322 new(zone()) LTransitionElementsKind(object, NULL,
2323 new_map_reg, temp_reg);
2324 return result;
2325 } else {
2326 LOperand* object = UseFixed(instr->object(), eax);
2327 LOperand* context = UseFixed(instr->context(), esi);
2328 LTransitionElementsKind* result =
2329 new(zone()) LTransitionElementsKind(object, context, NULL, NULL);
2330 return MarkAsCall(result, instr);
2331 }
2332}
2333
2334
2335LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2336 HTrapAllocationMemento* instr) {
2337 LOperand* object = UseRegister(instr->object());
2338 LOperand* temp = TempRegister();
2339 LTrapAllocationMemento* result =
2340 new(zone()) LTrapAllocationMemento(object, temp);
2341 return AssignEnvironment(result);
2342}
2343
2344
2345LInstruction* LChunkBuilder::DoMaybeGrowElements(HMaybeGrowElements* instr) {
2346 info()->MarkAsDeferredCalling();
2347 LOperand* context = UseFixed(instr->context(), esi);
2348 LOperand* object = Use(instr->object());
2349 LOperand* elements = Use(instr->elements());
2350 LOperand* key = UseRegisterOrConstant(instr->key());
2351 LOperand* current_capacity = UseRegisterOrConstant(instr->current_capacity());
2352
2353 LMaybeGrowElements* result = new (zone())
2354 LMaybeGrowElements(context, object, elements, key, current_capacity);
2355 DefineFixed(result, eax);
2356 return AssignPointerMap(AssignEnvironment(result));
2357}
2358
2359
2360LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2361 bool is_in_object = instr->access().IsInobject();
2362 bool is_external_location = instr->access().IsExternalMemory() &&
2363 instr->access().offset() == 0;
2364 bool needs_write_barrier = instr->NeedsWriteBarrier();
2365 bool needs_write_barrier_for_map = instr->has_transition() &&
2366 instr->NeedsWriteBarrierForMap();
2367
2368 LOperand* obj;
2369 if (needs_write_barrier) {
2370 obj = is_in_object
2371 ? UseRegister(instr->object())
2372 : UseTempRegister(instr->object());
2373 } else if (is_external_location) {
2374 DCHECK(!is_in_object);
2375 DCHECK(!needs_write_barrier);
2376 DCHECK(!needs_write_barrier_for_map);
2377 obj = UseRegisterOrConstant(instr->object());
2378 } else {
2379 obj = needs_write_barrier_for_map
2380 ? UseRegister(instr->object())
2381 : UseRegisterAtStart(instr->object());
2382 }
2383
2384 bool can_be_constant = instr->value()->IsConstant() &&
2385 HConstant::cast(instr->value())->NotInNewSpace() &&
2386 !instr->field_representation().IsDouble();
2387
2388 LOperand* val;
2389 if (instr->field_representation().IsInteger8() ||
2390 instr->field_representation().IsUInteger8()) {
2391 // mov_b requires a byte register (i.e. any of eax, ebx, ecx, edx).
2392 // Just force the value to be in eax and we're safe here.
2393 val = UseFixed(instr->value(), eax);
2394 } else if (needs_write_barrier) {
2395 val = UseTempRegister(instr->value());
2396 } else if (can_be_constant) {
2397 val = UseRegisterOrConstant(instr->value());
2398 } else if (instr->field_representation().IsDouble()) {
2399 val = UseRegisterAtStart(instr->value());
2400 } else {
2401 val = UseRegister(instr->value());
2402 }
2403
2404 // We only need a scratch register if we have a write barrier or we
2405 // have a store into the properties array (not in-object-property).
2406 LOperand* temp = (!is_in_object || needs_write_barrier ||
2407 needs_write_barrier_for_map) ? TempRegister() : NULL;
2408
2409 // We need a temporary register for write barrier of the map field.
2410 LOperand* temp_map = needs_write_barrier_for_map ? TempRegister() : NULL;
2411
2412 return new(zone()) LStoreNamedField(obj, val, temp, temp_map);
2413}
2414
2415
2416LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2417 LOperand* context = UseFixed(instr->context(), esi);
2418 LOperand* object =
2419 UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2420 LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2421 LOperand* slot = NULL;
2422 LOperand* vector = NULL;
2423 if (instr->HasVectorAndSlot()) {
2424 slot = FixedTemp(VectorStoreICDescriptor::SlotRegister());
2425 vector = FixedTemp(VectorStoreICDescriptor::VectorRegister());
2426 }
2427
2428 LStoreNamedGeneric* result =
2429 new (zone()) LStoreNamedGeneric(context, object, value, slot, vector);
2430 return MarkAsCall(result, instr);
2431}
2432
2433
2434LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2435 LOperand* context = UseFixed(instr->context(), esi);
2436 LOperand* left = UseFixed(instr->left(), edx);
2437 LOperand* right = UseFixed(instr->right(), eax);
2438 LStringAdd* string_add = new(zone()) LStringAdd(context, left, right);
2439 return MarkAsCall(DefineFixed(string_add, eax), instr);
2440}
2441
2442
2443LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2444 LOperand* string = UseTempRegister(instr->string());
2445 LOperand* index = UseTempRegister(instr->index());
2446 LOperand* context = UseAny(instr->context());
2447 LStringCharCodeAt* result =
2448 new(zone()) LStringCharCodeAt(context, string, index);
2449 return AssignPointerMap(DefineAsRegister(result));
2450}
2451
2452
2453LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2454 LOperand* char_code = UseRegister(instr->value());
2455 LOperand* context = UseAny(instr->context());
2456 LStringCharFromCode* result =
2457 new(zone()) LStringCharFromCode(context, char_code);
2458 return AssignPointerMap(DefineAsRegister(result));
2459}
2460
2461
2462LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2463 info()->MarkAsDeferredCalling();
2464 LOperand* context = UseAny(instr->context());
2465 LOperand* size = instr->size()->IsConstant()
2466 ? UseConstant(instr->size())
2467 : UseTempRegister(instr->size());
2468 LOperand* temp = TempRegister();
2469 LAllocate* result = new(zone()) LAllocate(context, size, temp);
2470 return AssignPointerMap(DefineAsRegister(result));
2471}
2472
2473
2474LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2475 DCHECK(argument_count_ == 0);
2476 allocator_->MarkAsOsrEntry();
2477 current_block_->last_environment()->set_ast_id(instr->ast_id());
2478 return AssignEnvironment(new(zone()) LOsrEntry);
2479}
2480
2481
2482LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2483 LParameter* result = new(zone()) LParameter;
2484 if (instr->kind() == HParameter::STACK_PARAMETER) {
2485 int spill_index = chunk()->GetParameterStackSlot(instr->index());
2486 return DefineAsSpilled(result, spill_index);
2487 } else {
2488 DCHECK(info()->IsStub());
Ben Murdoch097c5b22016-05-18 11:27:45 +01002489 CallInterfaceDescriptor descriptor = graph()->descriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002490 int index = static_cast<int>(instr->index());
2491 Register reg = descriptor.GetRegisterParameter(index);
2492 return DefineFixed(result, reg);
2493 }
2494}
2495
2496
2497LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2498 // Use an index that corresponds to the location in the unoptimized frame,
2499 // which the optimized frame will subsume.
2500 int env_index = instr->index();
2501 int spill_index = 0;
2502 if (instr->environment()->is_parameter_index(env_index)) {
2503 spill_index = chunk()->GetParameterStackSlot(env_index);
2504 } else {
2505 spill_index = env_index - instr->environment()->first_local_index();
2506 if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2507 Retry(kNotEnoughSpillSlotsForOsr);
2508 spill_index = 0;
2509 }
2510 if (spill_index == 0) {
2511 // The dynamic frame alignment state overwrites the first local.
2512 // The first local is saved at the end of the unoptimized frame.
2513 spill_index = graph()->osr()->UnoptimizedFrameSlots();
2514 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002515 spill_index += StandardFrameConstants::kFixedSlotCount;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002516 }
2517 return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2518}
2519
2520
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002521LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2522 // There are no real uses of the arguments object.
2523 // arguments.length and element access are supported directly on
2524 // stack arguments, and any real arguments object use causes a bailout.
2525 // So this value is never used.
2526 return NULL;
2527}
2528
2529
2530LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2531 instr->ReplayEnvironment(current_block_->last_environment());
2532
2533 // There are no real uses of a captured object.
2534 return NULL;
2535}
2536
2537
2538LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2539 info()->MarkAsRequiresFrame();
2540 LOperand* args = UseRegister(instr->arguments());
2541 LOperand* length;
2542 LOperand* index;
2543 if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
2544 length = UseRegisterOrConstant(instr->length());
2545 index = UseOrConstant(instr->index());
2546 } else {
2547 length = UseTempRegister(instr->length());
2548 index = Use(instr->index());
2549 }
2550 return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2551}
2552
2553
2554LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2555 LOperand* object = UseFixed(instr->value(), eax);
2556 LToFastProperties* result = new(zone()) LToFastProperties(object);
2557 return MarkAsCall(DefineFixed(result, eax), instr);
2558}
2559
2560
2561LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2562 LOperand* context = UseFixed(instr->context(), esi);
2563 LOperand* value = UseFixed(instr->value(), ebx);
2564 LTypeof* result = new(zone()) LTypeof(context, value);
2565 return MarkAsCall(DefineFixed(result, eax), instr);
2566}
2567
2568
2569LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2570 return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2571}
2572
2573
2574LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2575 instr->ReplayEnvironment(current_block_->last_environment());
2576 return NULL;
2577}
2578
2579
2580LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2581 info()->MarkAsDeferredCalling();
2582 if (instr->is_function_entry()) {
2583 LOperand* context = UseFixed(instr->context(), esi);
2584 return MarkAsCall(new(zone()) LStackCheck(context), instr);
2585 } else {
2586 DCHECK(instr->is_backwards_branch());
2587 LOperand* context = UseAny(instr->context());
2588 return AssignEnvironment(
2589 AssignPointerMap(new(zone()) LStackCheck(context)));
2590 }
2591}
2592
2593
2594LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2595 HEnvironment* outer = current_block_->last_environment();
2596 outer->set_ast_id(instr->ReturnId());
2597 HConstant* undefined = graph()->GetConstantUndefined();
2598 HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2599 instr->arguments_count(),
2600 instr->function(),
2601 undefined,
2602 instr->inlining_kind());
2603 // Only replay binding of arguments object if it wasn't removed from graph.
2604 if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2605 inner->Bind(instr->arguments_var(), instr->arguments_object());
2606 }
2607 inner->BindContext(instr->closure_context());
2608 inner->set_entry(instr);
2609 current_block_->UpdateEnvironment(inner);
2610 chunk_->AddInlinedFunction(instr->shared());
2611 return NULL;
2612}
2613
2614
2615LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2616 LInstruction* pop = NULL;
2617
2618 HEnvironment* env = current_block_->last_environment();
2619
2620 if (env->entry()->arguments_pushed()) {
2621 int argument_count = env->arguments_environment()->parameter_count();
2622 pop = new(zone()) LDrop(argument_count);
2623 DCHECK(instr->argument_delta() == -argument_count);
2624 }
2625
2626 HEnvironment* outer = current_block_->last_environment()->
2627 DiscardInlined(false);
2628 current_block_->UpdateEnvironment(outer);
2629 return pop;
2630}
2631
2632
2633LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2634 LOperand* context = UseFixed(instr->context(), esi);
2635 LOperand* object = UseFixed(instr->enumerable(), eax);
2636 LForInPrepareMap* result = new(zone()) LForInPrepareMap(context, object);
2637 return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
2638}
2639
2640
2641LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2642 LOperand* map = UseRegister(instr->map());
2643 return AssignEnvironment(DefineAsRegister(
2644 new(zone()) LForInCacheArray(map)));
2645}
2646
2647
2648LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2649 LOperand* value = UseRegisterAtStart(instr->value());
2650 LOperand* map = UseRegisterAtStart(instr->map());
2651 return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2652}
2653
2654
2655LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2656 LOperand* object = UseRegister(instr->object());
2657 LOperand* index = UseTempRegister(instr->index());
2658 LLoadFieldByIndex* load = new(zone()) LLoadFieldByIndex(object, index);
2659 LInstruction* result = DefineSameAsFirst(load);
2660 return AssignPointerMap(result);
2661}
2662
2663
2664LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2665 LOperand* context = UseRegisterAtStart(instr->context());
2666 return new(zone()) LStoreFrameContext(context);
2667}
2668
2669
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002670} // namespace internal
2671} // namespace v8
2672
2673#endif // V8_TARGET_ARCH_IA32