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