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