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