Upstream version 10.39.233.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   LInstruction* result = new(zone()) LBoundsCheck(index, length);
1881   if (!FLAG_debug_code || !instr->skip_check()) {
1882     result = AssignEnvironment(result);
1883   }
1884   return result;
1885 }
1886
1887
1888 LInstruction* LChunkBuilder::DoBoundsCheckBaseIndexInformation(
1889     HBoundsCheckBaseIndexInformation* instr) {
1890   UNREACHABLE();
1891   return NULL;
1892 }
1893
1894
1895 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1896   // The control instruction marking the end of a block that completed
1897   // abruptly (e.g., threw an exception).  There is nothing specific to do.
1898   return NULL;
1899 }
1900
1901
1902 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1903   return NULL;
1904 }
1905
1906
1907 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1908   // All HForceRepresentation instructions should be eliminated in the
1909   // representation change phase of Hydrogen.
1910   UNREACHABLE();
1911   return NULL;
1912 }
1913
1914
1915 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1916   Representation from = instr->from();
1917   Representation to = instr->to();
1918   HValue* val = instr->value();
1919   if (from.IsSmi()) {
1920     if (to.IsTagged()) {
1921       LOperand* value = UseRegister(val);
1922       return DefineSameAsFirst(new(zone()) LDummyUse(value));
1923     }
1924     from = Representation::Tagged();
1925   }
1926   if (from.IsTagged()) {
1927     if (to.IsDouble()) {
1928       LOperand* value = UseRegister(val);
1929       LOperand* temp = TempRegister();
1930       LInstruction* result =
1931           DefineAsRegister(new(zone()) LNumberUntagD(value, temp));
1932       if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1933       return result;
1934     } else if (to.IsSIMD128()) {
1935       LOperand* value = UseRegister(instr->value());
1936       LOperand* temp = TempRegister();
1937       LTaggedToSIMD128* res = new(zone()) LTaggedToSIMD128(value, temp, to);
1938       return AssignEnvironment(DefineAsRegister(res));
1939     } else if (to.IsSmi()) {
1940       LOperand* value = UseRegister(val);
1941       if (val->type().IsSmi()) {
1942         return DefineSameAsFirst(new(zone()) LDummyUse(value));
1943       }
1944       return AssignEnvironment(DefineSameAsFirst(new(zone()) LCheckSmi(value)));
1945     } else {
1946       DCHECK(to.IsInteger32());
1947       if (val->type().IsSmi() || val->representation().IsSmi()) {
1948         LOperand* value = UseRegister(val);
1949         return DefineSameAsFirst(new(zone()) LSmiUntag(value, false));
1950       } else {
1951         LOperand* value = UseRegister(val);
1952         bool truncating = instr->CanTruncateToInt32();
1953         LOperand* xmm_temp = !truncating ? FixedTemp(xmm1) : NULL;
1954         LInstruction* result =
1955             DefineSameAsFirst(new(zone()) LTaggedToI(value, xmm_temp));
1956         if (!val->representation().IsSmi()) result = AssignEnvironment(result);
1957         return result;
1958       }
1959     }
1960   } else if (from.IsDouble()) {
1961     if (to.IsTagged()) {
1962       info()->MarkAsDeferredCalling();
1963       LOperand* value = UseRegisterAtStart(val);
1964       LOperand* temp = FLAG_inline_new ? TempRegister() : NULL;
1965       LUnallocated* result_temp = TempRegister();
1966       LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
1967       return AssignPointerMap(Define(result, result_temp));
1968     } else if (to.IsSmi()) {
1969       LOperand* value = UseRegister(val);
1970       return AssignEnvironment(
1971           DefineAsRegister(new(zone()) LDoubleToSmi(value)));
1972     } else {
1973       DCHECK(to.IsInteger32());
1974       bool truncating = instr->CanTruncateToInt32();
1975       bool needs_temp = !truncating;
1976       LOperand* value = needs_temp ? UseTempRegister(val) : UseRegister(val);
1977       LOperand* temp = needs_temp ? TempRegister() : NULL;
1978       LInstruction* result =
1979           DefineAsRegister(new(zone()) LDoubleToI(value, temp));
1980       if (!truncating) result = AssignEnvironment(result);
1981       return result;
1982     }
1983   } else if (from.IsInteger32()) {
1984     info()->MarkAsDeferredCalling();
1985     if (to.IsTagged()) {
1986       LOperand* value = UseRegister(val);
1987       if (!instr->CheckFlag(HValue::kCanOverflow)) {
1988         return DefineSameAsFirst(new(zone()) LSmiTag(value));
1989       } else if (val->CheckFlag(HInstruction::kUint32)) {
1990         LOperand* temp = TempRegister();
1991         LNumberTagU* result = new(zone()) LNumberTagU(value, temp);
1992         return AssignPointerMap(DefineSameAsFirst(result));
1993       } else {
1994         LOperand* temp = TempRegister();
1995         LNumberTagI* result = new(zone()) LNumberTagI(value, temp);
1996         return AssignPointerMap(DefineSameAsFirst(result));
1997       }
1998     } else if (to.IsSmi()) {
1999       LOperand* value = UseRegister(val);
2000       LInstruction* result = DefineSameAsFirst(new(zone()) LSmiTag(value));
2001       if (instr->CheckFlag(HValue::kCanOverflow)) {
2002         result = AssignEnvironment(result);
2003       }
2004       return result;
2005     } else {
2006       DCHECK(to.IsDouble());
2007       if (val->CheckFlag(HInstruction::kUint32)) {
2008         return DefineAsRegister(new(zone()) LUint32ToDouble(UseRegister(val)));
2009       } else {
2010         return DefineAsRegister(new(zone()) LInteger32ToDouble(Use(val)));
2011       }
2012     }
2013   } else if (from.IsSIMD128()) {
2014     DCHECK(to.IsTagged());
2015     info()->MarkAsDeferredCalling();
2016     LOperand* value = UseRegister(instr->value());
2017     LOperand* temp = TempRegister();
2018     LOperand* temp2 = TempRegister();
2019
2020     // Make sure that temp and result_temp are different registers.
2021     LUnallocated* result_temp = TempRegister();
2022     LSIMD128ToTagged* result = new(zone()) LSIMD128ToTagged(value, temp, temp2);
2023     return AssignPointerMap(Define(result, result_temp));
2024   }
2025   UNREACHABLE();
2026   return NULL;
2027 }
2028
2029
2030 LInstruction* LChunkBuilder::DoCheckHeapObject(HCheckHeapObject* instr) {
2031   LOperand* value = UseAtStart(instr->value());
2032   LInstruction* result = new(zone()) LCheckNonSmi(value);
2033   if (!instr->value()->type().IsHeapObject()) {
2034     result = AssignEnvironment(result);
2035   }
2036   return result;
2037 }
2038
2039
2040 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
2041   LOperand* value = UseRegisterAtStart(instr->value());
2042   return AssignEnvironment(new(zone()) LCheckSmi(value));
2043 }
2044
2045
2046 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
2047   LOperand* value = UseRegisterAtStart(instr->value());
2048   LOperand* temp = TempRegister();
2049   LCheckInstanceType* result = new(zone()) LCheckInstanceType(value, temp);
2050   return AssignEnvironment(result);
2051 }
2052
2053
2054 LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
2055   // If the object is in new space, we'll emit a global cell compare and so
2056   // want the value in a register.  If the object gets promoted before we
2057   // emit code, we will still get the register but will do an immediate
2058   // compare instead of the cell compare.  This is safe.
2059   LOperand* value = instr->object_in_new_space()
2060       ? UseRegisterAtStart(instr->value()) : UseAtStart(instr->value());
2061   return AssignEnvironment(new(zone()) LCheckValue(value));
2062 }
2063
2064
2065 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2066   if (instr->IsStabilityCheck()) return new(zone()) LCheckMaps;
2067   LOperand* value = UseRegisterAtStart(instr->value());
2068   LInstruction* result = AssignEnvironment(new(zone()) LCheckMaps(value));
2069   if (instr->HasMigrationTarget()) {
2070     info()->MarkAsDeferredCalling();
2071     result = AssignPointerMap(result);
2072   }
2073   return result;
2074 }
2075
2076
2077 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2078   HValue* value = instr->value();
2079   Representation input_rep = value->representation();
2080   if (input_rep.IsDouble()) {
2081     LOperand* reg = UseRegister(value);
2082     return DefineFixed(new(zone()) LClampDToUint8(reg), eax);
2083   } else if (input_rep.IsInteger32()) {
2084     LOperand* reg = UseFixed(value, eax);
2085     return DefineFixed(new(zone()) LClampIToUint8(reg), eax);
2086   } else {
2087     DCHECK(input_rep.IsSmiOrTagged());
2088     LOperand* reg = UseFixed(value, eax);
2089     // Register allocator doesn't (yet) support allocation of double
2090     // temps. Reserve xmm1 explicitly.
2091     LOperand* temp = FixedTemp(xmm1);
2092     LClampTToUint8* result = new(zone()) LClampTToUint8(reg, temp);
2093     return AssignEnvironment(DefineFixed(result, eax));
2094   }
2095 }
2096
2097
2098 LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2099   HValue* value = instr->value();
2100   DCHECK(value->representation().IsDouble());
2101   return DefineAsRegister(new(zone()) LDoubleBits(UseRegister(value)));
2102 }
2103
2104
2105 LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2106   LOperand* lo = UseRegister(instr->lo());
2107   LOperand* hi = UseRegister(instr->hi());
2108   return DefineAsRegister(new(zone()) LConstructDouble(hi, lo));
2109 }
2110
2111
2112 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2113   LOperand* context = info()->IsStub() ? UseFixed(instr->context(), esi) : NULL;
2114   LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2115   return new(zone()) LReturn(
2116       UseFixed(instr->value(), eax), context, parameter_count);
2117 }
2118
2119
2120 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2121   Representation r = instr->representation();
2122   if (r.IsSmi()) {
2123     return DefineAsRegister(new(zone()) LConstantS);
2124   } else if (r.IsInteger32()) {
2125     return DefineAsRegister(new(zone()) LConstantI);
2126   } else if (r.IsDouble()) {
2127     double value = instr->DoubleValue();
2128     bool value_is_zero = bit_cast<uint64_t, double>(value) == 0;
2129     LOperand* temp = value_is_zero ? NULL : TempRegister();
2130     return DefineAsRegister(new(zone()) LConstantD(temp));
2131   } else if (r.IsExternal()) {
2132     return DefineAsRegister(new(zone()) LConstantE);
2133   } else if (r.IsTagged()) {
2134     return DefineAsRegister(new(zone()) LConstantT);
2135   } else {
2136     UNREACHABLE();
2137     return NULL;
2138   }
2139 }
2140
2141
2142 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2143   LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
2144   return instr->RequiresHoleCheck()
2145       ? AssignEnvironment(DefineAsRegister(result))
2146       : DefineAsRegister(result);
2147 }
2148
2149
2150 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2151   LOperand* context = UseFixed(instr->context(), esi);
2152   LOperand* global_object =
2153       UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2154   LOperand* vector = NULL;
2155   if (FLAG_vector_ics) {
2156     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2157   }
2158
2159   LLoadGlobalGeneric* result =
2160       new(zone()) LLoadGlobalGeneric(context, global_object, vector);
2161   return MarkAsCall(DefineFixed(result, eax), instr);
2162 }
2163
2164
2165 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2166   LStoreGlobalCell* result =
2167       new(zone()) LStoreGlobalCell(UseRegister(instr->value()));
2168   return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
2169 }
2170
2171
2172 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2173   LOperand* context = UseRegisterAtStart(instr->value());
2174   LInstruction* result =
2175       DefineAsRegister(new(zone()) LLoadContextSlot(context));
2176   if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2177     result = AssignEnvironment(result);
2178   }
2179   return result;
2180 }
2181
2182
2183 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2184   LOperand* value;
2185   LOperand* temp;
2186   LOperand* context = UseRegister(instr->context());
2187   if (instr->NeedsWriteBarrier()) {
2188     value = UseTempRegister(instr->value());
2189     temp = TempRegister();
2190   } else {
2191     value = UseRegister(instr->value());
2192     temp = NULL;
2193   }
2194   LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
2195   if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2196     result = AssignEnvironment(result);
2197   }
2198   return result;
2199 }
2200
2201
2202 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2203   LOperand* obj = (instr->access().IsExternalMemory() &&
2204                    instr->access().offset() == 0)
2205       ? UseRegisterOrConstantAtStart(instr->object())
2206       : UseRegisterAtStart(instr->object());
2207   return DefineAsRegister(new(zone()) LLoadNamedField(obj));
2208 }
2209
2210
2211 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2212   LOperand* context = UseFixed(instr->context(), esi);
2213   LOperand* object =
2214       UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2215   LOperand* vector = NULL;
2216   if (FLAG_vector_ics) {
2217     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2218   }
2219   LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(
2220       context, object, vector);
2221   return MarkAsCall(DefineFixed(result, eax), instr);
2222 }
2223
2224
2225 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2226     HLoadFunctionPrototype* instr) {
2227   return AssignEnvironment(DefineAsRegister(
2228       new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()),
2229                                          TempRegister())));
2230 }
2231
2232
2233 LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2234   return DefineAsRegister(new(zone()) LLoadRoot);
2235 }
2236
2237
2238 LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2239   DCHECK(instr->key()->representation().IsSmiOrInteger32());
2240   ElementsKind elements_kind = instr->elements_kind();
2241   bool clobbers_key = ExternalArrayOpRequiresTemp(
2242       instr->key()->representation(), elements_kind);
2243   LOperand* key = clobbers_key
2244       ? UseTempRegister(instr->key())
2245       : UseRegisterOrConstantAtStart(instr->key());
2246   LInstruction* result = NULL;
2247
2248   if (!instr->is_typed_elements()) {
2249     LOperand* obj = UseRegisterAtStart(instr->elements());
2250     result = DefineAsRegister(new(zone()) LLoadKeyed(obj, key));
2251   } else {
2252     DCHECK(
2253         (instr->representation().IsInteger32() &&
2254          !(IsDoubleOrFloatElementsKind(instr->elements_kind()))) ||
2255         (instr->representation().IsDouble() &&
2256          (IsDoubleOrFloatElementsKind(instr->elements_kind()))) ||
2257         (CpuFeatures::SupportsSIMD128InCrankshaft()
2258             ? instr->representation().IsFloat32x4()
2259             : instr->representation().IsTagged() &&
2260          (IsFloat32x4ElementsKind(instr->elements_kind()))) ||
2261         (CpuFeatures::SupportsSIMD128InCrankshaft()
2262             ? instr->representation().IsFloat64x2()
2263             : instr->representation().IsTagged() &&
2264          (IsFloat64x2ElementsKind(instr->elements_kind()))) ||
2265         (CpuFeatures::SupportsSIMD128InCrankshaft()
2266             ? instr->representation().IsInt32x4()
2267             : instr->representation().IsTagged() &&
2268          (IsInt32x4ElementsKind(instr->elements_kind()))));
2269     LOperand* backing_store = UseRegister(instr->elements());
2270     result = DefineAsRegister(new(zone()) LLoadKeyed(backing_store, key));
2271   }
2272
2273   if ((instr->is_external() || instr->is_fixed_typed_array()) ?
2274       // see LCodeGen::DoLoadKeyedExternalArray
2275       ((instr->elements_kind() == EXTERNAL_UINT32_ELEMENTS ||
2276         instr->elements_kind() == UINT32_ELEMENTS) &&
2277        !instr->CheckFlag(HInstruction::kUint32)) :
2278       // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2279       // LCodeGen::DoLoadKeyedFixedArray
2280       instr->RequiresHoleCheck()) {
2281     result = AssignEnvironment(result);
2282   }
2283   return result;
2284 }
2285
2286
2287 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2288   LOperand* context = UseFixed(instr->context(), esi);
2289   LOperand* object =
2290       UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2291   LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2292   LOperand* vector = NULL;
2293   if (FLAG_vector_ics) {
2294     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2295   }
2296   LLoadKeyedGeneric* result =
2297       new(zone()) LLoadKeyedGeneric(context, object, key, vector);
2298   return MarkAsCall(DefineFixed(result, eax), instr);
2299 }
2300
2301
2302 LOperand* LChunkBuilder::GetStoreKeyedValueOperand(HStoreKeyed* instr) {
2303   ElementsKind elements_kind = instr->elements_kind();
2304
2305   // Determine if we need a byte register in this case for the value.
2306   bool val_is_fixed_register =
2307       elements_kind == EXTERNAL_INT8_ELEMENTS ||
2308       elements_kind == EXTERNAL_UINT8_ELEMENTS ||
2309       elements_kind == EXTERNAL_UINT8_CLAMPED_ELEMENTS ||
2310       elements_kind == UINT8_ELEMENTS ||
2311       elements_kind == INT8_ELEMENTS ||
2312       elements_kind == UINT8_CLAMPED_ELEMENTS;
2313   if (val_is_fixed_register) {
2314     return UseFixed(instr->value(), eax);
2315   }
2316
2317   return UseRegister(instr->value());
2318 }
2319
2320
2321 LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2322   if (!instr->is_typed_elements()) {
2323     DCHECK(instr->elements()->representation().IsTagged());
2324     DCHECK(instr->key()->representation().IsInteger32() ||
2325            instr->key()->representation().IsSmi());
2326
2327     if (instr->value()->representation().IsDouble()) {
2328       LOperand* object = UseRegisterAtStart(instr->elements());
2329       LOperand* val = NULL;
2330       val = UseRegisterAtStart(instr->value());
2331       LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2332       return new(zone()) LStoreKeyed(object, key, val);
2333     } else {
2334       DCHECK(instr->value()->representation().IsSmiOrTagged());
2335       bool needs_write_barrier = instr->NeedsWriteBarrier();
2336
2337       LOperand* obj = UseRegister(instr->elements());
2338       LOperand* val;
2339       LOperand* key;
2340       if (needs_write_barrier) {
2341         val = UseTempRegister(instr->value());
2342         key = UseTempRegister(instr->key());
2343       } else {
2344         val = UseRegisterOrConstantAtStart(instr->value());
2345         key = UseRegisterOrConstantAtStart(instr->key());
2346       }
2347       return new(zone()) LStoreKeyed(obj, key, val);
2348     }
2349   }
2350
2351   ElementsKind elements_kind = instr->elements_kind();
2352   DCHECK(
2353       (instr->value()->representation().IsInteger32() &&
2354        !IsDoubleOrFloatElementsKind(elements_kind)) ||
2355       (instr->value()->representation().IsDouble() &&
2356        IsDoubleOrFloatElementsKind(elements_kind)) ||
2357       (CpuFeatures::SupportsSIMD128InCrankshaft()
2358           ? instr->value()->representation().IsFloat32x4()
2359           : instr->value()->representation().IsTagged() &&
2360        IsFloat32x4ElementsKind(elements_kind)) ||
2361       (CpuFeatures::SupportsSIMD128InCrankshaft()
2362           ? instr->value()->representation().IsFloat64x2()
2363           : instr->value()->representation().IsTagged() &&
2364        IsFloat64x2ElementsKind(elements_kind)) ||
2365       (CpuFeatures::SupportsSIMD128InCrankshaft()
2366           ? instr->value()->representation().IsInt32x4()
2367           : instr->value()->representation().IsTagged() &&
2368        IsInt32x4ElementsKind(elements_kind)));
2369   DCHECK((instr->is_fixed_typed_array() &&
2370           instr->elements()->representation().IsTagged()) ||
2371          (instr->is_external() &&
2372           instr->elements()->representation().IsExternal()));
2373
2374   LOperand* backing_store = UseRegister(instr->elements());
2375   LOperand* val = GetStoreKeyedValueOperand(instr);
2376   bool clobbers_key = ExternalArrayOpRequiresTemp(
2377       instr->key()->representation(), elements_kind);
2378   LOperand* key = clobbers_key
2379       ? UseTempRegister(instr->key())
2380       : UseRegisterOrConstantAtStart(instr->key());
2381   return new(zone()) LStoreKeyed(backing_store, key, val);
2382 }
2383
2384
2385 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2386   LOperand* context = UseFixed(instr->context(), esi);
2387   LOperand* object =
2388       UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2389   LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2390   LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2391
2392   DCHECK(instr->object()->representation().IsTagged());
2393   DCHECK(instr->key()->representation().IsTagged());
2394   DCHECK(instr->value()->representation().IsTagged());
2395
2396   LStoreKeyedGeneric* result =
2397       new(zone()) LStoreKeyedGeneric(context, object, key, value);
2398   return MarkAsCall(result, instr);
2399 }
2400
2401
2402 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2403     HTransitionElementsKind* instr) {
2404   if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2405     LOperand* object = UseRegister(instr->object());
2406     LOperand* new_map_reg = TempRegister();
2407     LOperand* temp_reg = TempRegister();
2408     LTransitionElementsKind* result =
2409         new(zone()) LTransitionElementsKind(object, NULL,
2410                                             new_map_reg, temp_reg);
2411     return result;
2412   } else {
2413     LOperand* object = UseFixed(instr->object(), eax);
2414     LOperand* context = UseFixed(instr->context(), esi);
2415     LTransitionElementsKind* result =
2416         new(zone()) LTransitionElementsKind(object, context, NULL, NULL);
2417     return MarkAsCall(result, instr);
2418   }
2419 }
2420
2421
2422 LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2423     HTrapAllocationMemento* instr) {
2424   LOperand* object = UseRegister(instr->object());
2425   LOperand* temp = TempRegister();
2426   LTrapAllocationMemento* result =
2427       new(zone()) LTrapAllocationMemento(object, temp);
2428   return AssignEnvironment(result);
2429 }
2430
2431
2432 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2433   bool is_in_object = instr->access().IsInobject();
2434   bool is_external_location = instr->access().IsExternalMemory() &&
2435       instr->access().offset() == 0;
2436   bool needs_write_barrier = instr->NeedsWriteBarrier();
2437   bool needs_write_barrier_for_map = instr->has_transition() &&
2438       instr->NeedsWriteBarrierForMap();
2439
2440   LOperand* obj;
2441   if (needs_write_barrier) {
2442     obj = is_in_object
2443         ? UseRegister(instr->object())
2444         : UseTempRegister(instr->object());
2445   } else if (is_external_location) {
2446     DCHECK(!is_in_object);
2447     DCHECK(!needs_write_barrier);
2448     DCHECK(!needs_write_barrier_for_map);
2449     obj = UseRegisterOrConstant(instr->object());
2450   } else {
2451     obj = needs_write_barrier_for_map
2452         ? UseRegister(instr->object())
2453         : UseRegisterAtStart(instr->object());
2454   }
2455
2456   bool can_be_constant = instr->value()->IsConstant() &&
2457       HConstant::cast(instr->value())->NotInNewSpace() &&
2458       !instr->field_representation().IsDouble();
2459
2460   LOperand* val;
2461   if (instr->field_representation().IsInteger8() ||
2462       instr->field_representation().IsUInteger8()) {
2463     // mov_b requires a byte register (i.e. any of eax, ebx, ecx, edx).
2464     // Just force the value to be in eax and we're safe here.
2465     val = UseFixed(instr->value(), eax);
2466   } else if (needs_write_barrier) {
2467     val = UseTempRegister(instr->value());
2468   } else if (can_be_constant) {
2469     val = UseRegisterOrConstant(instr->value());
2470   } else if (instr->field_representation().IsDouble()) {
2471     val = UseRegisterAtStart(instr->value());
2472   } else {
2473     val = UseRegister(instr->value());
2474   }
2475
2476   // We only need a scratch register if we have a write barrier or we
2477   // have a store into the properties array (not in-object-property).
2478   LOperand* temp = (!is_in_object || needs_write_barrier ||
2479                     needs_write_barrier_for_map) ? TempRegister() : NULL;
2480
2481   // We need a temporary register for write barrier of the map field.
2482   LOperand* temp_map = needs_write_barrier_for_map ? TempRegister() : NULL;
2483
2484   return new(zone()) LStoreNamedField(obj, val, temp, temp_map);
2485 }
2486
2487
2488 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2489   LOperand* context = UseFixed(instr->context(), esi);
2490   LOperand* object =
2491       UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2492   LOperand* value = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2493
2494   LStoreNamedGeneric* result =
2495       new(zone()) LStoreNamedGeneric(context, object, value);
2496   return MarkAsCall(result, instr);
2497 }
2498
2499
2500 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2501   LOperand* context = UseFixed(instr->context(), esi);
2502   LOperand* left = UseFixed(instr->left(), edx);
2503   LOperand* right = UseFixed(instr->right(), eax);
2504   LStringAdd* string_add = new(zone()) LStringAdd(context, left, right);
2505   return MarkAsCall(DefineFixed(string_add, eax), instr);
2506 }
2507
2508
2509 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2510   LOperand* string = UseTempRegister(instr->string());
2511   LOperand* index = UseTempRegister(instr->index());
2512   LOperand* context = UseAny(instr->context());
2513   LStringCharCodeAt* result =
2514       new(zone()) LStringCharCodeAt(context, string, index);
2515   return AssignPointerMap(DefineAsRegister(result));
2516 }
2517
2518
2519 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2520   LOperand* char_code = UseRegister(instr->value());
2521   LOperand* context = UseAny(instr->context());
2522   LStringCharFromCode* result =
2523       new(zone()) LStringCharFromCode(context, char_code);
2524   return AssignPointerMap(DefineAsRegister(result));
2525 }
2526
2527
2528 LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2529   info()->MarkAsDeferredCalling();
2530   LOperand* context = UseAny(instr->context());
2531   LOperand* size = instr->size()->IsConstant()
2532       ? UseConstant(instr->size())
2533       : UseTempRegister(instr->size());
2534   LOperand* temp = TempRegister();
2535   LAllocate* result = new(zone()) LAllocate(context, size, temp);
2536   return AssignPointerMap(DefineAsRegister(result));
2537 }
2538
2539
2540 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2541   LOperand* context = UseFixed(instr->context(), esi);
2542   return MarkAsCall(
2543       DefineFixed(new(zone()) LRegExpLiteral(context), eax), instr);
2544 }
2545
2546
2547 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2548   LOperand* context = UseFixed(instr->context(), esi);
2549   return MarkAsCall(
2550       DefineFixed(new(zone()) LFunctionLiteral(context), eax), instr);
2551 }
2552
2553
2554 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2555   DCHECK(argument_count_ == 0);
2556   allocator_->MarkAsOsrEntry();
2557   current_block_->last_environment()->set_ast_id(instr->ast_id());
2558   return AssignEnvironment(new(zone()) LOsrEntry);
2559 }
2560
2561
2562 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2563   LParameter* result = new(zone()) LParameter;
2564   if (instr->kind() == HParameter::STACK_PARAMETER) {
2565     int spill_index = chunk()->GetParameterStackSlot(instr->index());
2566     return DefineAsSpilled(result, spill_index);
2567   } else {
2568     DCHECK(info()->IsStub());
2569     CallInterfaceDescriptor descriptor =
2570         info()->code_stub()->GetCallInterfaceDescriptor();
2571     int index = static_cast<int>(instr->index());
2572     Register reg = descriptor.GetEnvironmentParameterRegister(index);
2573     return DefineFixed(result, reg);
2574   }
2575 }
2576
2577
2578 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2579   // Use an index that corresponds to the location in the unoptimized frame,
2580   // which the optimized frame will subsume.
2581   int env_index = instr->index();
2582   int spill_index = 0;
2583   if (instr->environment()->is_parameter_index(env_index)) {
2584     spill_index = chunk()->GetParameterStackSlot(env_index);
2585   } else {
2586     spill_index = env_index - instr->environment()->first_local_index();
2587     if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2588       Retry(kNotEnoughSpillSlotsForOsr);
2589       spill_index = 0;
2590     }
2591     if (spill_index == 0) {
2592       // The dynamic frame alignment state overwrites the first local.
2593       // The first local is saved at the end of the unoptimized frame.
2594       spill_index = graph()->osr()->UnoptimizedFrameSlots();
2595     }
2596   }
2597   return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2598 }
2599
2600
2601 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2602   LOperand* context = UseFixed(instr->context(), esi);
2603   LCallStub* result = new(zone()) LCallStub(context);
2604   return MarkAsCall(DefineFixed(result, eax), instr);
2605 }
2606
2607
2608 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2609   // There are no real uses of the arguments object.
2610   // arguments.length and element access are supported directly on
2611   // stack arguments, and any real arguments object use causes a bailout.
2612   // So this value is never used.
2613   return NULL;
2614 }
2615
2616
2617 LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2618   instr->ReplayEnvironment(current_block_->last_environment());
2619
2620   // There are no real uses of a captured object.
2621   return NULL;
2622 }
2623
2624
2625 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2626   info()->MarkAsRequiresFrame();
2627   LOperand* args = UseRegister(instr->arguments());
2628   LOperand* length;
2629   LOperand* index;
2630   if (instr->length()->IsConstant() && instr->index()->IsConstant()) {
2631     length = UseRegisterOrConstant(instr->length());
2632     index = UseOrConstant(instr->index());
2633   } else {
2634     length = UseTempRegister(instr->length());
2635     index = Use(instr->index());
2636   }
2637   return DefineAsRegister(new(zone()) LAccessArgumentsAt(args, length, index));
2638 }
2639
2640
2641 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2642   LOperand* object = UseFixed(instr->value(), eax);
2643   LToFastProperties* result = new(zone()) LToFastProperties(object);
2644   return MarkAsCall(DefineFixed(result, eax), instr);
2645 }
2646
2647
2648 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2649   LOperand* context = UseFixed(instr->context(), esi);
2650   LOperand* value = UseAtStart(instr->value());
2651   LTypeof* result = new(zone()) LTypeof(context, value);
2652   return MarkAsCall(DefineFixed(result, eax), instr);
2653 }
2654
2655
2656 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2657   return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2658 }
2659
2660
2661 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2662     HIsConstructCallAndBranch* instr) {
2663   return new(zone()) LIsConstructCallAndBranch(TempRegister());
2664 }
2665
2666
2667 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2668   instr->ReplayEnvironment(current_block_->last_environment());
2669   return NULL;
2670 }
2671
2672
2673 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2674   info()->MarkAsDeferredCalling();
2675   if (instr->is_function_entry()) {
2676     LOperand* context = UseFixed(instr->context(), esi);
2677     return MarkAsCall(new(zone()) LStackCheck(context), instr);
2678   } else {
2679     DCHECK(instr->is_backwards_branch());
2680     LOperand* context = UseAny(instr->context());
2681     return AssignEnvironment(
2682         AssignPointerMap(new(zone()) LStackCheck(context)));
2683   }
2684 }
2685
2686
2687 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2688   HEnvironment* outer = current_block_->last_environment();
2689   outer->set_ast_id(instr->ReturnId());
2690   HConstant* undefined = graph()->GetConstantUndefined();
2691   HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2692                                                instr->arguments_count(),
2693                                                instr->function(),
2694                                                undefined,
2695                                                instr->inlining_kind());
2696   // Only replay binding of arguments object if it wasn't removed from graph.
2697   if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2698     inner->Bind(instr->arguments_var(), instr->arguments_object());
2699   }
2700   inner->BindContext(instr->closure_context());
2701   inner->set_entry(instr);
2702   current_block_->UpdateEnvironment(inner);
2703   chunk_->AddInlinedClosure(instr->closure());
2704   return NULL;
2705 }
2706
2707
2708 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2709   LInstruction* pop = NULL;
2710
2711   HEnvironment* env = current_block_->last_environment();
2712
2713   if (env->entry()->arguments_pushed()) {
2714     int argument_count = env->arguments_environment()->parameter_count();
2715     pop = new(zone()) LDrop(argument_count);
2716     DCHECK(instr->argument_delta() == -argument_count);
2717   }
2718
2719   HEnvironment* outer = current_block_->last_environment()->
2720       DiscardInlined(false);
2721   current_block_->UpdateEnvironment(outer);
2722   return pop;
2723 }
2724
2725
2726 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2727   LOperand* context = UseFixed(instr->context(), esi);
2728   LOperand* object = UseFixed(instr->enumerable(), eax);
2729   LForInPrepareMap* result = new(zone()) LForInPrepareMap(context, object);
2730   return MarkAsCall(DefineFixed(result, eax), instr, CAN_DEOPTIMIZE_EAGERLY);
2731 }
2732
2733
2734 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2735   LOperand* map = UseRegister(instr->map());
2736   return AssignEnvironment(DefineAsRegister(
2737       new(zone()) LForInCacheArray(map)));
2738 }
2739
2740
2741 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2742   LOperand* value = UseRegisterAtStart(instr->value());
2743   LOperand* map = UseRegisterAtStart(instr->map());
2744   return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2745 }
2746
2747
2748 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2749   LOperand* object = UseRegister(instr->object());
2750   LOperand* index = UseTempRegister(instr->index());
2751   LLoadFieldByIndex* load = new(zone()) LLoadFieldByIndex(object, index);
2752   LInstruction* result = DefineSameAsFirst(load);
2753   return AssignPointerMap(result);
2754 }
2755
2756
2757 LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2758   LOperand* context = UseRegisterAtStart(instr->context());
2759   return new(zone()) LStoreFrameContext(context);
2760 }
2761
2762
2763 LInstruction* LChunkBuilder::DoAllocateBlockContext(
2764     HAllocateBlockContext* instr) {
2765   LOperand* context = UseFixed(instr->context(), esi);
2766   LOperand* function = UseRegisterAtStart(instr->function());
2767   LAllocateBlockContext* result =
2768       new(zone()) LAllocateBlockContext(context, function);
2769   return MarkAsCall(DefineFixed(result, esi), instr);
2770 }
2771
2772
2773 const char* LNullarySIMDOperation::Mnemonic() const {
2774   switch (op()) {
2775 #define SIMD_NULLARY_OPERATION_CASE_ITEM(module, function, name, p4)           \
2776     case k##name:                                                              \
2777       return #module "-" #function;
2778 SIMD_NULLARY_OPERATIONS(SIMD_NULLARY_OPERATION_CASE_ITEM)
2779 #undef SIMD_NULLARY_OPERATION_CASE_ITEM
2780     default:
2781       UNREACHABLE();
2782       return NULL;
2783   }
2784 }
2785
2786
2787 LInstruction* LChunkBuilder::DoNullarySIMDOperation(
2788     HNullarySIMDOperation* instr) {
2789   LNullarySIMDOperation* result =
2790       new(zone()) LNullarySIMDOperation(instr->op());
2791   switch (instr->op()) {
2792 #define SIMD_NULLARY_OPERATION_CASE_ITEM(module, function, name, p4)           \
2793     case k##name:
2794 SIMD_NULLARY_OPERATIONS(SIMD_NULLARY_OPERATION_CASE_ITEM)
2795 #undef SIMD_NULLARY_OPERATION_CASE_ITEM
2796       return DefineAsRegister(result);
2797     default:
2798       UNREACHABLE();
2799       return NULL;
2800   }
2801 }
2802
2803
2804 const char* LUnarySIMDOperation::Mnemonic() const {
2805   switch (op()) {
2806     case kSIMD128Change: return "SIMD128-change";
2807 #define SIMD_UNARY_OPERATION_CASE_ITEM(module, function, name, p4, p5)         \
2808     case k##name:                                                              \
2809       return #module "-" #function;
2810 SIMD_UNARY_OPERATIONS(SIMD_UNARY_OPERATION_CASE_ITEM)
2811 SIMD_UNARY_OPERATIONS_FOR_PROPERTY_ACCESS(SIMD_UNARY_OPERATION_CASE_ITEM)
2812 #undef SIMD_UNARY_OPERATION_CASE_ITEM
2813     default:
2814       UNREACHABLE();
2815       return NULL;
2816   }
2817 }
2818
2819
2820 LInstruction* LChunkBuilder::DoUnarySIMDOperation(HUnarySIMDOperation* instr) {
2821   LOperand* input = UseRegisterAtStart(instr->value());
2822   LUnarySIMDOperation* result =
2823       new(zone()) LUnarySIMDOperation(input, instr->op());
2824   switch (instr->op()) {
2825     case kSIMD128Change:
2826       return AssignEnvironment(DefineAsRegister(result));
2827     case kFloat32x4Abs:
2828     case kFloat32x4Neg:
2829     case kFloat32x4Reciprocal:
2830     case kFloat32x4ReciprocalSqrt:
2831     case kFloat32x4Sqrt:
2832     case kFloat64x2Abs:
2833     case kFloat64x2Neg:
2834     case kFloat64x2Sqrt:
2835     case kInt32x4Neg:
2836     case kInt32x4Not:
2837       return DefineSameAsFirst(result);
2838     case kFloat32x4Coercion:
2839     case kFloat64x2Coercion:
2840     case kInt32x4Coercion:
2841     case kFloat32x4BitsToInt32x4:
2842     case kFloat32x4ToInt32x4:
2843     case kInt32x4BitsToFloat32x4:
2844     case kInt32x4ToFloat32x4:
2845     case kFloat32x4Splat:
2846     case kInt32x4Splat:
2847     case kFloat32x4GetSignMask:
2848     case kFloat32x4GetX:
2849     case kFloat32x4GetY:
2850     case kFloat32x4GetZ:
2851     case kFloat32x4GetW:
2852     case kFloat64x2GetSignMask:
2853     case kFloat64x2GetX:
2854     case kFloat64x2GetY:
2855     case kInt32x4GetSignMask:
2856     case kInt32x4GetX:
2857     case kInt32x4GetY:
2858     case kInt32x4GetZ:
2859     case kInt32x4GetW:
2860     case kInt32x4GetFlagX:
2861     case kInt32x4GetFlagY:
2862     case kInt32x4GetFlagZ:
2863     case kInt32x4GetFlagW:
2864       return DefineAsRegister(result);
2865     default:
2866       UNREACHABLE();
2867       return NULL;
2868   }
2869 }
2870
2871
2872 const char* LBinarySIMDOperation::Mnemonic() const {
2873   switch (op()) {
2874 #define SIMD_BINARY_OPERATION_CASE_ITEM(module, function, name, p4, p5, p6)    \
2875     case k##name:                                                              \
2876       return #module "-" #function;
2877 SIMD_BINARY_OPERATIONS(SIMD_BINARY_OPERATION_CASE_ITEM)
2878 #undef SIMD_BINARY_OPERATION_CASE_ITEM
2879     default:
2880       UNREACHABLE();
2881       return NULL;
2882   }
2883 }
2884
2885
2886 LInstruction* LChunkBuilder::DoBinarySIMDOperation(
2887     HBinarySIMDOperation* instr) {
2888   switch (instr->op()) {
2889     case kFloat32x4Add:
2890     case kFloat32x4Div:
2891     case kFloat32x4Max:
2892     case kFloat32x4Min:
2893     case kFloat32x4Mul:
2894     case kFloat32x4Sub:
2895     case kFloat32x4Scale:
2896     case kFloat32x4WithX:
2897     case kFloat32x4WithY:
2898     case kFloat32x4WithZ:
2899     case kFloat32x4WithW:
2900     case kFloat64x2Add:
2901     case kFloat64x2Div:
2902     case kFloat64x2Max:
2903     case kFloat64x2Min:
2904     case kFloat64x2Mul:
2905     case kFloat64x2Sub:
2906     case kFloat64x2Scale:
2907     case kFloat64x2WithX:
2908     case kFloat64x2WithY:
2909     case kInt32x4Add:
2910     case kInt32x4And:
2911     case kInt32x4Mul:
2912     case kInt32x4Or:
2913     case kInt32x4Sub:
2914     case kInt32x4Xor:
2915     case kInt32x4WithX:
2916     case kInt32x4WithY:
2917     case kInt32x4WithZ:
2918     case kInt32x4WithW:
2919     case kInt32x4WithFlagX:
2920     case kInt32x4WithFlagY:
2921     case kInt32x4WithFlagZ:
2922     case kInt32x4WithFlagW:
2923     case kInt32x4GreaterThan:
2924     case kInt32x4Equal:
2925     case kInt32x4LessThan: {
2926       LOperand* left = UseRegisterAtStart(instr->left());
2927       LOperand* right = UseRegisterAtStart(instr->right());
2928       LBinarySIMDOperation* result =
2929           new(zone()) LBinarySIMDOperation(left, right, instr->op());
2930       if (instr->op() == kInt32x4WithFlagX ||
2931           instr->op() == kInt32x4WithFlagY ||
2932           instr->op() == kInt32x4WithFlagZ ||
2933           instr->op() == kInt32x4WithFlagW) {
2934         return AssignEnvironment(DefineSameAsFirst(result));
2935       } else {
2936         return DefineSameAsFirst(result);
2937       }
2938     }
2939     case kFloat64x2Constructor: {
2940       LOperand* left = UseRegisterAtStart(instr->left());
2941       LOperand* right = UseRegisterAtStart(instr->right());
2942       LBinarySIMDOperation* result =
2943           new(zone()) LBinarySIMDOperation(left, right, instr->op());
2944       return DefineAsRegister(result);
2945     }
2946     case kFloat32x4Shuffle:
2947     case kInt32x4Shuffle:
2948     case kInt32x4ShiftLeft:
2949     case kInt32x4ShiftRight:
2950     case kInt32x4ShiftRightArithmetic: {
2951       LOperand* left = UseRegisterAtStart(instr->left());
2952       LOperand* right = UseOrConstant(instr->right());
2953       LBinarySIMDOperation* result =
2954           new(zone()) LBinarySIMDOperation(left, right, instr->op());
2955       return AssignEnvironment(DefineSameAsFirst(result));
2956     }
2957     case kFloat32x4LessThan:
2958     case kFloat32x4LessThanOrEqual:
2959     case kFloat32x4Equal:
2960     case kFloat32x4NotEqual:
2961     case kFloat32x4GreaterThanOrEqual:
2962     case kFloat32x4GreaterThan: {
2963       LOperand* left = UseRegisterAtStart(instr->left());
2964       LOperand* right = UseRegisterAtStart(instr->right());
2965       LBinarySIMDOperation* result =
2966           new(zone()) LBinarySIMDOperation(left, right, instr->op());
2967       return DefineAsRegister(result);
2968     }
2969     default:
2970       UNREACHABLE();
2971       return NULL;
2972   }
2973 }
2974
2975
2976 const char* LTernarySIMDOperation::Mnemonic() const {
2977   switch (op()) {
2978 #define SIMD_TERNARY_OPERATION_CASE_ITEM(module, function, name, p4, p5, p6,   \
2979                                          p7)                                   \
2980     case k##name:                                                              \
2981       return #module "-" #function;
2982 SIMD_TERNARY_OPERATIONS(SIMD_TERNARY_OPERATION_CASE_ITEM)
2983 #undef SIMD_TERNARY_OPERATION_CASE_ITEM
2984     default:
2985       UNREACHABLE();
2986       return NULL;
2987   }
2988 }
2989
2990
2991 LInstruction* LChunkBuilder::DoTernarySIMDOperation(
2992     HTernarySIMDOperation* instr) {
2993   LOperand* first = UseRegisterAtStart(instr->first());
2994   LOperand* second = UseRegisterAtStart(instr->second());
2995   LOperand* third = instr->op() == kFloat32x4ShuffleMix
2996                     ? UseOrConstant(instr->third())
2997                     : UseRegisterAtStart(instr->third());
2998   LTernarySIMDOperation* result =
2999       new(zone()) LTernarySIMDOperation(first, second, third, instr->op());
3000   switch (instr->op()) {
3001     case kInt32x4Select:
3002     case kFloat32x4Select: {
3003       return DefineAsRegister(result);
3004     }
3005     case kFloat32x4ShuffleMix: {
3006       return AssignEnvironment(DefineSameAsFirst(result));
3007     }
3008     case kFloat32x4Clamp:
3009     case kFloat64x2Clamp: {
3010       return DefineSameAsFirst(result);
3011     }
3012     default:
3013       UNREACHABLE();
3014       return NULL;
3015   }
3016 }
3017
3018
3019 const char* LQuarternarySIMDOperation::Mnemonic() const {
3020   switch (op()) {
3021 #define SIMD_QUARTERNARY_OPERATION_CASE_ITEM(module, function, name, p4, p5,   \
3022                                              p6, p7, p8)                       \
3023     case k##name:                                                              \
3024       return #module "-" #function;
3025 SIMD_QUARTERNARY_OPERATIONS(SIMD_QUARTERNARY_OPERATION_CASE_ITEM)
3026 #undef SIMD_QUARTERNARY_OPERATION_CASE_ITEM
3027     default:
3028       UNREACHABLE();
3029       return NULL;
3030   }
3031 }
3032
3033
3034 LInstruction* LChunkBuilder::DoQuarternarySIMDOperation(
3035     HQuarternarySIMDOperation* instr) {
3036   LOperand* x = UseRegisterAtStart(instr->x());
3037   LOperand* y = UseRegisterAtStart(instr->y());
3038   LOperand* z = UseRegisterAtStart(instr->z());
3039   LOperand* w = UseRegisterAtStart(instr->w());
3040   LQuarternarySIMDOperation* result =
3041       new(zone()) LQuarternarySIMDOperation(x, y, z, w, instr->op());
3042   if (instr->op() == kInt32x4Bool) {
3043     return AssignEnvironment(DefineAsRegister(result));
3044   } else {
3045     return DefineAsRegister(result);
3046   }
3047 }
3048
3049
3050 } }  // namespace v8::internal
3051
3052 #endif  // V8_TARGET_ARCH_IA32