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