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