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