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