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