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