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