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