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