d54c7ec46af07b940d15204d2a1e21b4af19ac7e
[platform/upstream/nodejs.git] / deps / v8 / 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 LStoreNamedField::PrintDataTo(StringStream* stream) {
347   object()->PrintTo(stream);
348   std::ostringstream os;
349   os << hydrogen()->access() << " <- ";
350   stream->Add(os.str().c_str());
351   value()->PrintTo(stream);
352 }
353
354
355 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
356   object()->PrintTo(stream);
357   stream->Add(".");
358   stream->Add(String::cast(*name())->ToCString().get());
359   stream->Add(" <- ");
360   value()->PrintTo(stream);
361 }
362
363
364 void LLoadKeyed::PrintDataTo(StringStream* stream) {
365   elements()->PrintTo(stream);
366   stream->Add("[");
367   key()->PrintTo(stream);
368   if (hydrogen()->IsDehoisted()) {
369     stream->Add(" + %d]", base_offset());
370   } else {
371     stream->Add("]");
372   }
373 }
374
375
376 void LStoreKeyed::PrintDataTo(StringStream* stream) {
377   elements()->PrintTo(stream);
378   stream->Add("[");
379   key()->PrintTo(stream);
380   if (hydrogen()->IsDehoisted()) {
381     stream->Add(" + %d] <-", base_offset());
382   } else {
383     stream->Add("] <- ");
384   }
385
386   if (value() == NULL) {
387     DCHECK(hydrogen()->IsConstantHoleStore() &&
388            hydrogen()->value()->representation().IsDouble());
389     stream->Add("<the hole(nan)>");
390   } else {
391     value()->PrintTo(stream);
392   }
393 }
394
395
396 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
397   object()->PrintTo(stream);
398   stream->Add("[");
399   key()->PrintTo(stream);
400   stream->Add("] <- ");
401   value()->PrintTo(stream);
402 }
403
404
405 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
406   object()->PrintTo(stream);
407   stream->Add(" %p -> %p", *original_map(), *transitioned_map());
408 }
409
410
411 int LPlatformChunk::GetNextSpillIndex(RegisterKind kind) {
412   // Skip a slot if for a double-width slot.
413   if (kind == DOUBLE_REGISTERS) spill_slot_count_++;
414   return spill_slot_count_++;
415 }
416
417
418 LOperand* LPlatformChunk::GetNextSpillSlot(RegisterKind kind) {
419   int index = GetNextSpillIndex(kind);
420   if (kind == DOUBLE_REGISTERS) {
421     return LDoubleStackSlot::Create(index, zone());
422   } else {
423     DCHECK(kind == GENERAL_REGISTERS);
424     return LStackSlot::Create(index, zone());
425   }
426 }
427
428
429 LPlatformChunk* LChunkBuilder::Build() {
430   DCHECK(is_unused());
431   chunk_ = new (zone()) LPlatformChunk(info(), graph());
432   LPhase phase("L_Building chunk", chunk_);
433   status_ = BUILDING;
434
435   // If compiling for OSR, reserve space for the unoptimized frame,
436   // which will be subsumed into this frame.
437   if (graph()->has_osr()) {
438     for (int i = graph()->osr()->UnoptimizedFrameSlots(); i > 0; i--) {
439       chunk_->GetNextSpillIndex(GENERAL_REGISTERS);
440     }
441   }
442
443   const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
444   for (int i = 0; i < blocks->length(); i++) {
445     HBasicBlock* next = NULL;
446     if (i < blocks->length() - 1) next = blocks->at(i + 1);
447     DoBasicBlock(blocks->at(i), next);
448     if (is_aborted()) return NULL;
449   }
450   status_ = DONE;
451   return chunk_;
452 }
453
454
455 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
456   return new (zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
457                                    Register::ToAllocationIndex(reg));
458 }
459
460
461 LUnallocated* LChunkBuilder::ToUnallocated(DoubleRegister reg) {
462   return new (zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
463                                    DoubleRegister::ToAllocationIndex(reg));
464 }
465
466
467 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
468   return Use(value, ToUnallocated(fixed_register));
469 }
470
471
472 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, DoubleRegister reg) {
473   return Use(value, ToUnallocated(reg));
474 }
475
476
477 LOperand* LChunkBuilder::UseRegister(HValue* value) {
478   return Use(value,
479              new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
480 }
481
482
483 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
484   return Use(value, new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
485                                               LUnallocated::USED_AT_START));
486 }
487
488
489 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
490   return Use(value, new (zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
491 }
492
493
494 LOperand* LChunkBuilder::Use(HValue* value) {
495   return Use(value, new (zone()) LUnallocated(LUnallocated::NONE));
496 }
497
498
499 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
500   return Use(value, new (zone())
501              LUnallocated(LUnallocated::NONE, LUnallocated::USED_AT_START));
502 }
503
504
505 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
506   return value->IsConstant()
507              ? chunk_->DefineConstantOperand(HConstant::cast(value))
508              : Use(value);
509 }
510
511
512 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
513   return value->IsConstant()
514              ? chunk_->DefineConstantOperand(HConstant::cast(value))
515              : UseAtStart(value);
516 }
517
518
519 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
520   return value->IsConstant()
521              ? chunk_->DefineConstantOperand(HConstant::cast(value))
522              : UseRegister(value);
523 }
524
525
526 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
527   return value->IsConstant()
528              ? chunk_->DefineConstantOperand(HConstant::cast(value))
529              : UseRegisterAtStart(value);
530 }
531
532
533 LOperand* LChunkBuilder::UseConstant(HValue* value) {
534   return chunk_->DefineConstantOperand(HConstant::cast(value));
535 }
536
537
538 LOperand* LChunkBuilder::UseAny(HValue* value) {
539   return value->IsConstant()
540              ? chunk_->DefineConstantOperand(HConstant::cast(value))
541              : Use(value, new (zone()) LUnallocated(LUnallocated::ANY));
542 }
543
544
545 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
546   if (value->EmitAtUses()) {
547     HInstruction* instr = HInstruction::cast(value);
548     VisitInstruction(instr);
549   }
550   operand->set_virtual_register(value->id());
551   return operand;
552 }
553
554
555 LInstruction* LChunkBuilder::Define(LTemplateResultInstruction<1>* instr,
556                                     LUnallocated* result) {
557   result->set_virtual_register(current_instruction_->id());
558   instr->set_result(result);
559   return instr;
560 }
561
562
563 LInstruction* LChunkBuilder::DefineAsRegister(
564     LTemplateResultInstruction<1>* instr) {
565   return Define(instr,
566                 new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
567 }
568
569
570 LInstruction* LChunkBuilder::DefineAsSpilled(
571     LTemplateResultInstruction<1>* instr, int index) {
572   return Define(instr,
573                 new (zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
574 }
575
576
577 LInstruction* LChunkBuilder::DefineSameAsFirst(
578     LTemplateResultInstruction<1>* instr) {
579   return Define(instr,
580                 new (zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
581 }
582
583
584 LInstruction* LChunkBuilder::DefineFixed(LTemplateResultInstruction<1>* instr,
585                                          Register reg) {
586   return Define(instr, ToUnallocated(reg));
587 }
588
589
590 LInstruction* LChunkBuilder::DefineFixedDouble(
591     LTemplateResultInstruction<1>* instr, DoubleRegister reg) {
592   return Define(instr, ToUnallocated(reg));
593 }
594
595
596 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
597   HEnvironment* hydrogen_env = current_block_->last_environment();
598   int argument_index_accumulator = 0;
599   ZoneList<HValue*> objects_to_materialize(0, zone());
600   instr->set_environment(CreateEnvironment(
601       hydrogen_env, &argument_index_accumulator, &objects_to_materialize));
602   return instr;
603 }
604
605
606 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
607                                         HInstruction* hinstr,
608                                         CanDeoptimize can_deoptimize) {
609   info()->MarkAsNonDeferredCalling();
610 #ifdef DEBUG
611   instr->VerifyCall();
612 #endif
613   instr->MarkAsCall();
614   instr = AssignPointerMap(instr);
615
616   // If instruction does not have side-effects lazy deoptimization
617   // after the call will try to deoptimize to the point before the call.
618   // Thus we still need to attach environment to this call even if
619   // call sequence can not deoptimize eagerly.
620   bool needs_environment = (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
621                            !hinstr->HasObservableSideEffects();
622   if (needs_environment && !instr->HasEnvironment()) {
623     instr = AssignEnvironment(instr);
624     // We can't really figure out if the environment is needed or not.
625     instr->environment()->set_has_been_used();
626   }
627
628   return instr;
629 }
630
631
632 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
633   DCHECK(!instr->HasPointerMap());
634   instr->set_pointer_map(new (zone()) LPointerMap(zone()));
635   return instr;
636 }
637
638
639 LUnallocated* LChunkBuilder::TempRegister() {
640   LUnallocated* operand =
641       new (zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
642   int vreg = allocator_->GetVirtualRegister();
643   if (!allocator_->AllocationOk()) {
644     Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
645     vreg = 0;
646   }
647   operand->set_virtual_register(vreg);
648   return operand;
649 }
650
651
652 LUnallocated* LChunkBuilder::TempDoubleRegister() {
653   LUnallocated* operand =
654       new (zone()) LUnallocated(LUnallocated::MUST_HAVE_DOUBLE_REGISTER);
655   int vreg = allocator_->GetVirtualRegister();
656   if (!allocator_->AllocationOk()) {
657     Abort(kOutOfVirtualRegistersWhileTryingToAllocateTempRegister);
658     vreg = 0;
659   }
660   operand->set_virtual_register(vreg);
661   return operand;
662 }
663
664
665 LOperand* LChunkBuilder::FixedTemp(Register reg) {
666   LUnallocated* operand = ToUnallocated(reg);
667   DCHECK(operand->HasFixedPolicy());
668   return operand;
669 }
670
671
672 LOperand* LChunkBuilder::FixedTemp(DoubleRegister reg) {
673   LUnallocated* operand = ToUnallocated(reg);
674   DCHECK(operand->HasFixedPolicy());
675   return operand;
676 }
677
678
679 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
680   return new (zone()) LLabel(instr->block());
681 }
682
683
684 LInstruction* LChunkBuilder::DoDummyUse(HDummyUse* instr) {
685   return DefineAsRegister(new (zone()) LDummyUse(UseAny(instr->value())));
686 }
687
688
689 LInstruction* LChunkBuilder::DoEnvironmentMarker(HEnvironmentMarker* instr) {
690   UNREACHABLE();
691   return NULL;
692 }
693
694
695 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
696   return AssignEnvironment(new (zone()) LDeoptimize);
697 }
698
699
700 LInstruction* LChunkBuilder::DoShift(Token::Value op,
701                                      HBitwiseBinaryOperation* instr) {
702   if (instr->representation().IsSmiOrInteger32()) {
703     DCHECK(instr->left()->representation().Equals(instr->representation()));
704     DCHECK(instr->right()->representation().Equals(instr->representation()));
705     LOperand* left = UseRegisterAtStart(instr->left());
706
707     HValue* right_value = instr->right();
708     LOperand* right = NULL;
709     int constant_value = 0;
710     bool does_deopt = false;
711     if (right_value->IsConstant()) {
712       HConstant* constant = HConstant::cast(right_value);
713       right = chunk_->DefineConstantOperand(constant);
714       constant_value = constant->Integer32Value() & 0x1f;
715       // Left shifts can deoptimize if we shift by > 0 and the result cannot be
716       // truncated to smi.
717       if (instr->representation().IsSmi() && constant_value > 0) {
718         does_deopt = !instr->CheckUsesForFlag(HValue::kTruncatingToSmi);
719       }
720     } else {
721       right = UseRegisterAtStart(right_value);
722     }
723
724     // Shift operations can only deoptimize if we do a logical shift
725     // by 0 and the result cannot be truncated to int32.
726     if (op == Token::SHR && constant_value == 0) {
727       does_deopt = !instr->CheckFlag(HInstruction::kUint32);
728     }
729
730     LInstruction* result =
731         DefineAsRegister(new (zone()) LShiftI(op, left, right, does_deopt));
732     return does_deopt ? AssignEnvironment(result) : result;
733   } else {
734     return DoArithmeticT(op, instr);
735   }
736 }
737
738
739 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
740                                            HArithmeticBinaryOperation* instr) {
741   DCHECK(instr->representation().IsDouble());
742   DCHECK(instr->left()->representation().IsDouble());
743   DCHECK(instr->right()->representation().IsDouble());
744   if (op == Token::MOD) {
745     LOperand* left = UseFixedDouble(instr->left(), d1);
746     LOperand* right = UseFixedDouble(instr->right(), d2);
747     LArithmeticD* result = new (zone()) LArithmeticD(op, left, right);
748     // We call a C function for double modulo. It can't trigger a GC. We need
749     // to use fixed result register for the call.
750     // TODO(fschneider): Allow any register as input registers.
751     return MarkAsCall(DefineFixedDouble(result, d1), instr);
752   } else {
753     LOperand* left = UseRegisterAtStart(instr->left());
754     LOperand* right = UseRegisterAtStart(instr->right());
755     LArithmeticD* result = new (zone()) LArithmeticD(op, left, right);
756     return DefineAsRegister(result);
757   }
758 }
759
760
761 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
762                                            HBinaryOperation* instr) {
763   HValue* left = instr->left();
764   HValue* right = instr->right();
765   DCHECK(left->representation().IsTagged());
766   DCHECK(right->representation().IsTagged());
767   LOperand* context = UseFixed(instr->context(), cp);
768   LOperand* left_operand = UseFixed(left, r4);
769   LOperand* right_operand = UseFixed(right, r3);
770   LArithmeticT* result =
771       new (zone()) LArithmeticT(op, context, left_operand, right_operand);
772   return MarkAsCall(DefineFixed(result, r3), instr);
773 }
774
775
776 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
777   DCHECK(is_building());
778   current_block_ = block;
779   next_block_ = next_block;
780   if (block->IsStartBlock()) {
781     block->UpdateEnvironment(graph_->start_environment());
782     argument_count_ = 0;
783   } else if (block->predecessors()->length() == 1) {
784     // We have a single predecessor => copy environment and outgoing
785     // argument count from the predecessor.
786     DCHECK(block->phis()->length() == 0);
787     HBasicBlock* pred = block->predecessors()->at(0);
788     HEnvironment* last_environment = pred->last_environment();
789     DCHECK(last_environment != NULL);
790     // Only copy the environment, if it is later used again.
791     if (pred->end()->SecondSuccessor() == NULL) {
792       DCHECK(pred->end()->FirstSuccessor() == block);
793     } else {
794       if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
795           pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
796         last_environment = last_environment->Copy();
797       }
798     }
799     block->UpdateEnvironment(last_environment);
800     DCHECK(pred->argument_count() >= 0);
801     argument_count_ = pred->argument_count();
802   } else {
803     // We are at a state join => process phis.
804     HBasicBlock* pred = block->predecessors()->at(0);
805     // No need to copy the environment, it cannot be used later.
806     HEnvironment* last_environment = pred->last_environment();
807     for (int i = 0; i < block->phis()->length(); ++i) {
808       HPhi* phi = block->phis()->at(i);
809       if (phi->HasMergedIndex()) {
810         last_environment->SetValueAt(phi->merged_index(), phi);
811       }
812     }
813     for (int i = 0; i < block->deleted_phis()->length(); ++i) {
814       if (block->deleted_phis()->at(i) < last_environment->length()) {
815         last_environment->SetValueAt(block->deleted_phis()->at(i),
816                                      graph_->GetConstantUndefined());
817       }
818     }
819     block->UpdateEnvironment(last_environment);
820     // Pick up the outgoing argument count of one of the predecessors.
821     argument_count_ = pred->argument_count();
822   }
823   HInstruction* current = block->first();
824   int start = chunk_->instructions()->length();
825   while (current != NULL && !is_aborted()) {
826     // Code for constants in registers is generated lazily.
827     if (!current->EmitAtUses()) {
828       VisitInstruction(current);
829     }
830     current = current->next();
831   }
832   int end = chunk_->instructions()->length() - 1;
833   if (end >= start) {
834     block->set_first_instruction_index(start);
835     block->set_last_instruction_index(end);
836   }
837   block->set_argument_count(argument_count_);
838   next_block_ = NULL;
839   current_block_ = NULL;
840 }
841
842
843 void LChunkBuilder::VisitInstruction(HInstruction* current) {
844   HInstruction* old_current = current_instruction_;
845   current_instruction_ = current;
846
847   LInstruction* instr = NULL;
848   if (current->CanReplaceWithDummyUses()) {
849     if (current->OperandCount() == 0) {
850       instr = DefineAsRegister(new (zone()) LDummy());
851     } else {
852       DCHECK(!current->OperandAt(0)->IsControlInstruction());
853       instr = DefineAsRegister(new (zone())
854                                LDummyUse(UseAny(current->OperandAt(0))));
855     }
856     for (int i = 1; i < current->OperandCount(); ++i) {
857       if (current->OperandAt(i)->IsControlInstruction()) continue;
858       LInstruction* dummy =
859           new (zone()) LDummyUse(UseAny(current->OperandAt(i)));
860       dummy->set_hydrogen_value(current);
861       chunk_->AddInstruction(dummy, current_block_);
862     }
863   } else {
864     HBasicBlock* successor;
865     if (current->IsControlInstruction() &&
866         HControlInstruction::cast(current)->KnownSuccessorBlock(&successor) &&
867         successor != NULL) {
868       instr = new (zone()) LGoto(successor);
869     } else {
870       instr = current->CompileToLithium(this);
871     }
872   }
873
874   argument_count_ += current->argument_delta();
875   DCHECK(argument_count_ >= 0);
876
877   if (instr != NULL) {
878     AddInstruction(instr, current);
879   }
880
881   current_instruction_ = old_current;
882 }
883
884
885 void LChunkBuilder::AddInstruction(LInstruction* instr,
886                                    HInstruction* hydrogen_val) {
887   // Associate the hydrogen instruction first, since we may need it for
888   // the ClobbersRegisters() or ClobbersDoubleRegisters() calls below.
889   instr->set_hydrogen_value(hydrogen_val);
890
891 #if DEBUG
892   // Make sure that the lithium instruction has either no fixed register
893   // constraints in temps or the result OR no uses that are only used at
894   // start. If this invariant doesn't hold, the register allocator can decide
895   // to insert a split of a range immediately before the instruction due to an
896   // already allocated register needing to be used for the instruction's fixed
897   // register constraint. In this case, The register allocator won't see an
898   // interference between the split child and the use-at-start (it would if
899   // the it was just a plain use), so it is free to move the split child into
900   // the same register that is used for the use-at-start.
901   // See https://code.google.com/p/chromium/issues/detail?id=201590
902   if (!(instr->ClobbersRegisters() &&
903         instr->ClobbersDoubleRegisters(isolate()))) {
904     int fixed = 0;
905     int used_at_start = 0;
906     for (UseIterator it(instr); !it.Done(); it.Advance()) {
907       LUnallocated* operand = LUnallocated::cast(it.Current());
908       if (operand->IsUsedAtStart()) ++used_at_start;
909     }
910     if (instr->Output() != NULL) {
911       if (LUnallocated::cast(instr->Output())->HasFixedPolicy()) ++fixed;
912     }
913     for (TempIterator it(instr); !it.Done(); it.Advance()) {
914       LUnallocated* operand = LUnallocated::cast(it.Current());
915       if (operand->HasFixedPolicy()) ++fixed;
916     }
917     DCHECK(fixed == 0 || used_at_start == 0);
918   }
919 #endif
920
921   if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
922     instr = AssignPointerMap(instr);
923   }
924   if (FLAG_stress_environments && !instr->HasEnvironment()) {
925     instr = AssignEnvironment(instr);
926   }
927   chunk_->AddInstruction(instr, current_block_);
928
929   if (instr->IsCall()) {
930     HValue* hydrogen_value_for_lazy_bailout = hydrogen_val;
931     LInstruction* instruction_needing_environment = NULL;
932     if (hydrogen_val->HasObservableSideEffects()) {
933       HSimulate* sim = HSimulate::cast(hydrogen_val->next());
934       instruction_needing_environment = instr;
935       sim->ReplayEnvironment(current_block_->last_environment());
936       hydrogen_value_for_lazy_bailout = sim;
937     }
938     LInstruction* bailout = AssignEnvironment(new (zone()) LLazyBailout());
939     bailout->set_hydrogen_value(hydrogen_value_for_lazy_bailout);
940     chunk_->AddInstruction(bailout, current_block_);
941     if (instruction_needing_environment != NULL) {
942       // Store the lazy deopt environment with the instruction if needed.
943       // Right now it is only used for LInstanceOfKnownGlobal.
944       instruction_needing_environment->SetDeferredLazyDeoptimizationEnvironment(
945           bailout->environment());
946     }
947   }
948 }
949
950
951 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
952   return new (zone()) LGoto(instr->FirstSuccessor());
953 }
954
955
956 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
957   HValue* value = instr->value();
958   Representation r = value->representation();
959   HType type = value->type();
960   ToBooleanStub::Types expected = instr->expected_input_types();
961   if (expected.IsEmpty()) expected = ToBooleanStub::Types::Generic();
962
963   bool easy_case = !r.IsTagged() || type.IsBoolean() || type.IsSmi() ||
964                    type.IsJSArray() || type.IsHeapNumber() || type.IsString();
965   LInstruction* branch = new (zone()) LBranch(UseRegister(value));
966   if (!easy_case &&
967       ((!expected.Contains(ToBooleanStub::SMI) && expected.NeedsMap()) ||
968        !expected.IsGeneric())) {
969     branch = AssignEnvironment(branch);
970   }
971   return branch;
972 }
973
974
975 LInstruction* LChunkBuilder::DoDebugBreak(HDebugBreak* instr) {
976   return new (zone()) LDebugBreak();
977 }
978
979
980 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
981   DCHECK(instr->value()->representation().IsTagged());
982   LOperand* value = UseRegisterAtStart(instr->value());
983   LOperand* temp = TempRegister();
984   return new (zone()) LCmpMapAndBranch(value, temp);
985 }
986
987
988 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* instr) {
989   info()->MarkAsRequiresFrame();
990   LOperand* value = UseRegister(instr->value());
991   return DefineAsRegister(new (zone()) LArgumentsLength(value));
992 }
993
994
995 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
996   info()->MarkAsRequiresFrame();
997   return DefineAsRegister(new (zone()) LArgumentsElements);
998 }
999
1000
1001 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1002   LOperand* context = UseFixed(instr->context(), cp);
1003   LInstanceOf* result = new (zone()) LInstanceOf(
1004       context, UseFixed(instr->left(), r3), UseFixed(instr->right(), r4));
1005   return MarkAsCall(DefineFixed(result, r3), instr);
1006 }
1007
1008
1009 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1010     HInstanceOfKnownGlobal* instr) {
1011   LInstanceOfKnownGlobal* result = new (zone())
1012       LInstanceOfKnownGlobal(UseFixed(instr->context(), cp),
1013                              UseFixed(instr->left(), r3), FixedTemp(r7));
1014   return MarkAsCall(DefineFixed(result, r3), instr);
1015 }
1016
1017
1018 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1019   LOperand* receiver = UseRegisterAtStart(instr->receiver());
1020   LOperand* function = UseRegisterAtStart(instr->function());
1021   LWrapReceiver* result = new (zone()) LWrapReceiver(receiver, function);
1022   return AssignEnvironment(DefineAsRegister(result));
1023 }
1024
1025
1026 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1027   LOperand* function = UseFixed(instr->function(), r4);
1028   LOperand* receiver = UseFixed(instr->receiver(), r3);
1029   LOperand* length = UseFixed(instr->length(), r5);
1030   LOperand* elements = UseFixed(instr->elements(), r6);
1031   LApplyArguments* result =
1032       new (zone()) LApplyArguments(function, receiver, length, elements);
1033   return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
1034 }
1035
1036
1037 LInstruction* LChunkBuilder::DoPushArguments(HPushArguments* instr) {
1038   int argc = instr->OperandCount();
1039   for (int i = 0; i < argc; ++i) {
1040     LOperand* argument = Use(instr->argument(i));
1041     AddInstruction(new (zone()) LPushArgument(argument), instr);
1042   }
1043   return NULL;
1044 }
1045
1046
1047 LInstruction* LChunkBuilder::DoStoreCodeEntry(
1048     HStoreCodeEntry* store_code_entry) {
1049   LOperand* function = UseRegister(store_code_entry->function());
1050   LOperand* code_object = UseTempRegister(store_code_entry->code_object());
1051   return new (zone()) LStoreCodeEntry(function, code_object);
1052 }
1053
1054
1055 LInstruction* LChunkBuilder::DoInnerAllocatedObject(
1056     HInnerAllocatedObject* instr) {
1057   LOperand* base_object = UseRegisterAtStart(instr->base_object());
1058   LOperand* offset = UseRegisterOrConstantAtStart(instr->offset());
1059   return DefineAsRegister(new (zone())
1060                           LInnerAllocatedObject(base_object, offset));
1061 }
1062
1063
1064 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1065   return instr->HasNoUses() ? NULL
1066                             : DefineAsRegister(new (zone()) LThisFunction);
1067 }
1068
1069
1070 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1071   if (instr->HasNoUses()) return NULL;
1072
1073   if (info()->IsStub()) {
1074     return DefineFixed(new (zone()) LContext, cp);
1075   }
1076
1077   return DefineAsRegister(new (zone()) LContext);
1078 }
1079
1080
1081 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1082   LOperand* context = UseFixed(instr->context(), cp);
1083   return MarkAsCall(new (zone()) LDeclareGlobals(context), instr);
1084 }
1085
1086
1087 LInstruction* LChunkBuilder::DoCallJSFunction(HCallJSFunction* instr) {
1088   LOperand* function = UseFixed(instr->function(), r4);
1089
1090   LCallJSFunction* result = new (zone()) LCallJSFunction(function);
1091
1092   return MarkAsCall(DefineFixed(result, r3), instr);
1093 }
1094
1095
1096 LInstruction* LChunkBuilder::DoCallWithDescriptor(HCallWithDescriptor* instr) {
1097   CallInterfaceDescriptor descriptor = instr->descriptor();
1098
1099   LOperand* target = UseRegisterOrConstantAtStart(instr->target());
1100   ZoneList<LOperand*> ops(instr->OperandCount(), zone());
1101   ops.Add(target, zone());
1102   for (int i = 1; i < instr->OperandCount(); i++) {
1103     LOperand* op =
1104         UseFixed(instr->OperandAt(i), descriptor.GetParameterRegister(i - 1));
1105     ops.Add(op, zone());
1106   }
1107
1108   LCallWithDescriptor* result =
1109       new (zone()) LCallWithDescriptor(descriptor, ops, zone());
1110   return MarkAsCall(DefineFixed(result, r3), instr);
1111 }
1112
1113
1114 LInstruction* LChunkBuilder::DoTailCallThroughMegamorphicCache(
1115     HTailCallThroughMegamorphicCache* instr) {
1116   LOperand* context = UseFixed(instr->context(), cp);
1117   LOperand* receiver_register =
1118       UseFixed(instr->receiver(), LoadDescriptor::ReceiverRegister());
1119   LOperand* name_register =
1120       UseFixed(instr->name(), LoadDescriptor::NameRegister());
1121   LOperand* slot = NULL;
1122   LOperand* vector = NULL;
1123   if (FLAG_vector_ics) {
1124     slot = UseFixed(instr->slot(), VectorLoadICDescriptor::SlotRegister());
1125     vector =
1126         UseFixed(instr->vector(), VectorLoadICDescriptor::VectorRegister());
1127   }
1128
1129   // Not marked as call. It can't deoptimize, and it never returns.
1130   return new (zone()) LTailCallThroughMegamorphicCache(
1131       context, receiver_register, name_register, slot, vector);
1132 }
1133
1134
1135 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1136   LOperand* context = UseFixed(instr->context(), cp);
1137   LOperand* function = UseFixed(instr->function(), r4);
1138   LInvokeFunction* result = new (zone()) LInvokeFunction(context, function);
1139   return MarkAsCall(DefineFixed(result, r3), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1140 }
1141
1142
1143 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1144   switch (instr->op()) {
1145     case kMathFloor:
1146       return DoMathFloor(instr);
1147     case kMathRound:
1148       return DoMathRound(instr);
1149     case kMathFround:
1150       return DoMathFround(instr);
1151     case kMathAbs:
1152       return DoMathAbs(instr);
1153     case kMathLog:
1154       return DoMathLog(instr);
1155     case kMathExp:
1156       return DoMathExp(instr);
1157     case kMathSqrt:
1158       return DoMathSqrt(instr);
1159     case kMathPowHalf:
1160       return DoMathPowHalf(instr);
1161     case kMathClz32:
1162       return DoMathClz32(instr);
1163     default:
1164       UNREACHABLE();
1165       return NULL;
1166   }
1167 }
1168
1169
1170 LInstruction* LChunkBuilder::DoMathFloor(HUnaryMathOperation* instr) {
1171   LOperand* input = UseRegister(instr->value());
1172   LMathFloor* result = new (zone()) LMathFloor(input);
1173   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
1174 }
1175
1176
1177 LInstruction* LChunkBuilder::DoMathRound(HUnaryMathOperation* instr) {
1178   LOperand* input = UseRegister(instr->value());
1179   LOperand* temp = TempDoubleRegister();
1180   LMathRound* result = new (zone()) LMathRound(input, temp);
1181   return AssignEnvironment(DefineAsRegister(result));
1182 }
1183
1184
1185 LInstruction* LChunkBuilder::DoMathFround(HUnaryMathOperation* instr) {
1186   LOperand* input = UseRegister(instr->value());
1187   LMathFround* result = new (zone()) LMathFround(input);
1188   return DefineAsRegister(result);
1189 }
1190
1191
1192 LInstruction* LChunkBuilder::DoMathAbs(HUnaryMathOperation* instr) {
1193   Representation r = instr->value()->representation();
1194   LOperand* context = (r.IsDouble() || r.IsSmiOrInteger32())
1195                           ? NULL
1196                           : UseFixed(instr->context(), cp);
1197   LOperand* input = UseRegister(instr->value());
1198   LInstruction* result =
1199       DefineAsRegister(new (zone()) LMathAbs(context, input));
1200   if (!r.IsDouble() && !r.IsSmiOrInteger32()) result = AssignPointerMap(result);
1201   if (!r.IsDouble()) result = AssignEnvironment(result);
1202   return result;
1203 }
1204
1205
1206 LInstruction* LChunkBuilder::DoMathLog(HUnaryMathOperation* instr) {
1207   DCHECK(instr->representation().IsDouble());
1208   DCHECK(instr->value()->representation().IsDouble());
1209   LOperand* input = UseFixedDouble(instr->value(), d1);
1210   return MarkAsCall(DefineFixedDouble(new (zone()) LMathLog(input), d1), instr);
1211 }
1212
1213
1214 LInstruction* LChunkBuilder::DoMathClz32(HUnaryMathOperation* instr) {
1215   LOperand* input = UseRegisterAtStart(instr->value());
1216   LMathClz32* result = new (zone()) LMathClz32(input);
1217   return DefineAsRegister(result);
1218 }
1219
1220
1221 LInstruction* LChunkBuilder::DoMathExp(HUnaryMathOperation* instr) {
1222   DCHECK(instr->representation().IsDouble());
1223   DCHECK(instr->value()->representation().IsDouble());
1224   LOperand* input = UseRegister(instr->value());
1225   LOperand* temp1 = TempRegister();
1226   LOperand* temp2 = TempRegister();
1227   LOperand* double_temp = TempDoubleRegister();
1228   LMathExp* result = new (zone()) LMathExp(input, double_temp, temp1, temp2);
1229   return DefineAsRegister(result);
1230 }
1231
1232
1233 LInstruction* LChunkBuilder::DoMathSqrt(HUnaryMathOperation* instr) {
1234   LOperand* input = UseRegisterAtStart(instr->value());
1235   LMathSqrt* result = new (zone()) LMathSqrt(input);
1236   return DefineAsRegister(result);
1237 }
1238
1239
1240 LInstruction* LChunkBuilder::DoMathPowHalf(HUnaryMathOperation* instr) {
1241   LOperand* input = UseRegisterAtStart(instr->value());
1242   LMathPowHalf* result = new (zone()) LMathPowHalf(input);
1243   return DefineAsRegister(result);
1244 }
1245
1246
1247 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1248   LOperand* context = UseFixed(instr->context(), cp);
1249   LOperand* constructor = UseFixed(instr->constructor(), r4);
1250   LCallNew* result = new (zone()) LCallNew(context, constructor);
1251   return MarkAsCall(DefineFixed(result, r3), instr);
1252 }
1253
1254
1255 LInstruction* LChunkBuilder::DoCallNewArray(HCallNewArray* instr) {
1256   LOperand* context = UseFixed(instr->context(), cp);
1257   LOperand* constructor = UseFixed(instr->constructor(), r4);
1258   LCallNewArray* result = new (zone()) LCallNewArray(context, constructor);
1259   return MarkAsCall(DefineFixed(result, r3), instr);
1260 }
1261
1262
1263 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1264   LOperand* context = UseFixed(instr->context(), cp);
1265   LOperand* function = UseFixed(instr->function(), r4);
1266   LOperand* slot = NULL;
1267   LOperand* vector = NULL;
1268   if (instr->HasVectorAndSlot()) {
1269     slot = FixedTemp(r6);
1270     vector = FixedTemp(r5);
1271   }
1272
1273   LCallFunction* call =
1274       new (zone()) LCallFunction(context, function, slot, vector);
1275   return MarkAsCall(DefineFixed(call, r3), instr);
1276 }
1277
1278
1279 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1280   LOperand* context = UseFixed(instr->context(), cp);
1281   return MarkAsCall(DefineFixed(new (zone()) LCallRuntime(context), r3), instr);
1282 }
1283
1284
1285 LInstruction* LChunkBuilder::DoRor(HRor* instr) {
1286   return DoShift(Token::ROR, instr);
1287 }
1288
1289
1290 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1291   return DoShift(Token::SHR, instr);
1292 }
1293
1294
1295 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1296   return DoShift(Token::SAR, instr);
1297 }
1298
1299
1300 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1301   return DoShift(Token::SHL, instr);
1302 }
1303
1304
1305 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1306   if (instr->representation().IsSmiOrInteger32()) {
1307     DCHECK(instr->left()->representation().Equals(instr->representation()));
1308     DCHECK(instr->right()->representation().Equals(instr->representation()));
1309     DCHECK(instr->CheckFlag(HValue::kTruncatingToInt32));
1310
1311     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1312     LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1313     return DefineAsRegister(new (zone()) LBitI(left, right));
1314   } else {
1315     return DoArithmeticT(instr->op(), instr);
1316   }
1317 }
1318
1319
1320 LInstruction* LChunkBuilder::DoDivByPowerOf2I(HDiv* instr) {
1321   DCHECK(instr->representation().IsSmiOrInteger32());
1322   DCHECK(instr->left()->representation().Equals(instr->representation()));
1323   DCHECK(instr->right()->representation().Equals(instr->representation()));
1324   LOperand* dividend = UseRegister(instr->left());
1325   int32_t divisor = instr->right()->GetInteger32Constant();
1326   LInstruction* result =
1327       DefineAsRegister(new (zone()) LDivByPowerOf2I(dividend, divisor));
1328   if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1329       (instr->CheckFlag(HValue::kCanOverflow) && divisor == -1) ||
1330       (!instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
1331        divisor != 1 && divisor != -1)) {
1332     result = AssignEnvironment(result);
1333   }
1334   return result;
1335 }
1336
1337
1338 LInstruction* LChunkBuilder::DoDivByConstI(HDiv* instr) {
1339   DCHECK(instr->representation().IsInteger32());
1340   DCHECK(instr->left()->representation().Equals(instr->representation()));
1341   DCHECK(instr->right()->representation().Equals(instr->representation()));
1342   LOperand* dividend = UseRegister(instr->left());
1343   int32_t divisor = instr->right()->GetInteger32Constant();
1344   LInstruction* result =
1345       DefineAsRegister(new (zone()) LDivByConstI(dividend, divisor));
1346   if (divisor == 0 ||
1347       (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1348       !instr->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1349     result = AssignEnvironment(result);
1350   }
1351   return result;
1352 }
1353
1354
1355 LInstruction* LChunkBuilder::DoDivI(HDiv* instr) {
1356   DCHECK(instr->representation().IsSmiOrInteger32());
1357   DCHECK(instr->left()->representation().Equals(instr->representation()));
1358   DCHECK(instr->right()->representation().Equals(instr->representation()));
1359   LOperand* dividend = UseRegister(instr->left());
1360   LOperand* divisor = UseRegister(instr->right());
1361   LInstruction* result =
1362       DefineAsRegister(new (zone()) LDivI(dividend, divisor));
1363   if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1364       instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1365       (instr->CheckFlag(HValue::kCanOverflow) &&
1366        !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32)) ||
1367       (!instr->IsMathFloorOfDiv() &&
1368        !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1369     result = AssignEnvironment(result);
1370   }
1371   return result;
1372 }
1373
1374
1375 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1376   if (instr->representation().IsSmiOrInteger32()) {
1377     if (instr->RightIsPowerOf2()) {
1378       return DoDivByPowerOf2I(instr);
1379     } else if (instr->right()->IsConstant()) {
1380       return DoDivByConstI(instr);
1381     } else {
1382       return DoDivI(instr);
1383     }
1384   } else if (instr->representation().IsDouble()) {
1385     return DoArithmeticD(Token::DIV, instr);
1386   } else {
1387     return DoArithmeticT(Token::DIV, instr);
1388   }
1389 }
1390
1391
1392 LInstruction* LChunkBuilder::DoFlooringDivByPowerOf2I(HMathFloorOfDiv* instr) {
1393   LOperand* dividend = UseRegisterAtStart(instr->left());
1394   int32_t divisor = instr->right()->GetInteger32Constant();
1395   LInstruction* result =
1396       DefineAsRegister(new (zone()) LFlooringDivByPowerOf2I(dividend, divisor));
1397   if ((instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0) ||
1398       (instr->CheckFlag(HValue::kLeftCanBeMinInt) && divisor == -1)) {
1399     result = AssignEnvironment(result);
1400   }
1401   return result;
1402 }
1403
1404
1405 LInstruction* LChunkBuilder::DoFlooringDivByConstI(HMathFloorOfDiv* instr) {
1406   DCHECK(instr->representation().IsInteger32());
1407   DCHECK(instr->left()->representation().Equals(instr->representation()));
1408   DCHECK(instr->right()->representation().Equals(instr->representation()));
1409   LOperand* dividend = UseRegister(instr->left());
1410   int32_t divisor = instr->right()->GetInteger32Constant();
1411   LOperand* temp =
1412       ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
1413        (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive)))
1414           ? NULL
1415           : TempRegister();
1416   LInstruction* result = DefineAsRegister(
1417       new (zone()) LFlooringDivByConstI(dividend, divisor, temp));
1418   if (divisor == 0 ||
1419       (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
1420     result = AssignEnvironment(result);
1421   }
1422   return result;
1423 }
1424
1425
1426 LInstruction* LChunkBuilder::DoFlooringDivI(HMathFloorOfDiv* instr) {
1427   DCHECK(instr->representation().IsSmiOrInteger32());
1428   DCHECK(instr->left()->representation().Equals(instr->representation()));
1429   DCHECK(instr->right()->representation().Equals(instr->representation()));
1430   LOperand* dividend = UseRegister(instr->left());
1431   LOperand* divisor = UseRegister(instr->right());
1432   LInstruction* result =
1433       DefineAsRegister(new (zone()) LFlooringDivI(dividend, divisor));
1434   if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1435       instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1436       (instr->CheckFlag(HValue::kCanOverflow) &&
1437        !instr->CheckFlag(HValue::kAllUsesTruncatingToInt32))) {
1438     result = AssignEnvironment(result);
1439   }
1440   return result;
1441 }
1442
1443
1444 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1445   if (instr->RightIsPowerOf2()) {
1446     return DoFlooringDivByPowerOf2I(instr);
1447   } else if (instr->right()->IsConstant()) {
1448     return DoFlooringDivByConstI(instr);
1449   } else {
1450     return DoFlooringDivI(instr);
1451   }
1452 }
1453
1454
1455 LInstruction* LChunkBuilder::DoModByPowerOf2I(HMod* instr) {
1456   DCHECK(instr->representation().IsSmiOrInteger32());
1457   DCHECK(instr->left()->representation().Equals(instr->representation()));
1458   DCHECK(instr->right()->representation().Equals(instr->representation()));
1459   LOperand* dividend = UseRegisterAtStart(instr->left());
1460   int32_t divisor = instr->right()->GetInteger32Constant();
1461   LInstruction* result =
1462       DefineSameAsFirst(new (zone()) LModByPowerOf2I(dividend, divisor));
1463   if (instr->CheckFlag(HValue::kLeftCanBeNegative) &&
1464       instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1465     result = AssignEnvironment(result);
1466   }
1467   return result;
1468 }
1469
1470
1471 LInstruction* LChunkBuilder::DoModByConstI(HMod* instr) {
1472   DCHECK(instr->representation().IsSmiOrInteger32());
1473   DCHECK(instr->left()->representation().Equals(instr->representation()));
1474   DCHECK(instr->right()->representation().Equals(instr->representation()));
1475   LOperand* dividend = UseRegister(instr->left());
1476   int32_t divisor = instr->right()->GetInteger32Constant();
1477   LInstruction* result =
1478       DefineAsRegister(new (zone()) LModByConstI(dividend, divisor));
1479   if (divisor == 0 || instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1480     result = AssignEnvironment(result);
1481   }
1482   return result;
1483 }
1484
1485
1486 LInstruction* LChunkBuilder::DoModI(HMod* instr) {
1487   DCHECK(instr->representation().IsSmiOrInteger32());
1488   DCHECK(instr->left()->representation().Equals(instr->representation()));
1489   DCHECK(instr->right()->representation().Equals(instr->representation()));
1490   LOperand* dividend = UseRegister(instr->left());
1491   LOperand* divisor = UseRegister(instr->right());
1492   LInstruction* result =
1493       DefineAsRegister(new (zone()) LModI(dividend, divisor));
1494   if (instr->CheckFlag(HValue::kCanBeDivByZero) ||
1495       instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1496     result = AssignEnvironment(result);
1497   }
1498   return result;
1499 }
1500
1501
1502 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1503   if (instr->representation().IsSmiOrInteger32()) {
1504     if (instr->RightIsPowerOf2()) {
1505       return DoModByPowerOf2I(instr);
1506     } else if (instr->right()->IsConstant()) {
1507       return DoModByConstI(instr);
1508     } else {
1509       return DoModI(instr);
1510     }
1511   } else if (instr->representation().IsDouble()) {
1512     return DoArithmeticD(Token::MOD, instr);
1513   } else {
1514     return DoArithmeticT(Token::MOD, instr);
1515   }
1516 }
1517
1518
1519 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1520   if (instr->representation().IsSmiOrInteger32()) {
1521     DCHECK(instr->left()->representation().Equals(instr->representation()));
1522     DCHECK(instr->right()->representation().Equals(instr->representation()));
1523     HValue* left = instr->BetterLeftOperand();
1524     HValue* right = instr->BetterRightOperand();
1525     LOperand* left_op;
1526     LOperand* right_op;
1527     bool can_overflow = instr->CheckFlag(HValue::kCanOverflow);
1528     bool bailout_on_minus_zero = instr->CheckFlag(HValue::kBailoutOnMinusZero);
1529
1530     int32_t constant_value = 0;
1531     if (right->IsConstant()) {
1532       HConstant* constant = HConstant::cast(right);
1533       constant_value = constant->Integer32Value();
1534       // Constants -1, 0 and 1 can be optimized if the result can overflow.
1535       // For other constants, it can be optimized only without overflow.
1536       if (!can_overflow || ((constant_value >= -1) && (constant_value <= 1))) {
1537         left_op = UseRegisterAtStart(left);
1538         right_op = UseConstant(right);
1539       } else {
1540         if (bailout_on_minus_zero) {
1541           left_op = UseRegister(left);
1542         } else {
1543           left_op = UseRegisterAtStart(left);
1544         }
1545         right_op = UseRegister(right);
1546       }
1547     } else {
1548       if (bailout_on_minus_zero) {
1549         left_op = UseRegister(left);
1550       } else {
1551         left_op = UseRegisterAtStart(left);
1552       }
1553       right_op = UseRegister(right);
1554     }
1555     LMulI* mul = new (zone()) LMulI(left_op, right_op);
1556     if (right_op->IsConstantOperand()
1557             ? ((can_overflow && constant_value == -1) ||
1558                (bailout_on_minus_zero && constant_value <= 0))
1559             : (can_overflow || bailout_on_minus_zero)) {
1560       AssignEnvironment(mul);
1561     }
1562     return DefineAsRegister(mul);
1563
1564   } else if (instr->representation().IsDouble()) {
1565     return DoArithmeticD(Token::MUL, instr);
1566   } else {
1567     return DoArithmeticT(Token::MUL, instr);
1568   }
1569 }
1570
1571
1572 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1573   if (instr->representation().IsSmiOrInteger32()) {
1574     DCHECK(instr->left()->representation().Equals(instr->representation()));
1575     DCHECK(instr->right()->representation().Equals(instr->representation()));
1576
1577     if (instr->left()->IsConstant() &&
1578         !instr->CheckFlag(HValue::kCanOverflow)) {
1579       // If lhs is constant, do reverse subtraction instead.
1580       return DoRSub(instr);
1581     }
1582
1583     LOperand* left = UseRegisterAtStart(instr->left());
1584     LOperand* right = UseOrConstantAtStart(instr->right());
1585     LSubI* sub = new (zone()) LSubI(left, right);
1586     LInstruction* result = DefineAsRegister(sub);
1587     if (instr->CheckFlag(HValue::kCanOverflow)) {
1588       result = AssignEnvironment(result);
1589     }
1590     return result;
1591   } else if (instr->representation().IsDouble()) {
1592     return DoArithmeticD(Token::SUB, instr);
1593   } else {
1594     return DoArithmeticT(Token::SUB, instr);
1595   }
1596 }
1597
1598
1599 LInstruction* LChunkBuilder::DoRSub(HSub* instr) {
1600   DCHECK(instr->representation().IsSmiOrInteger32());
1601   DCHECK(instr->left()->representation().Equals(instr->representation()));
1602   DCHECK(instr->right()->representation().Equals(instr->representation()));
1603   DCHECK(!instr->CheckFlag(HValue::kCanOverflow));
1604
1605   // Note: The lhs of the subtraction becomes the rhs of the
1606   // reverse-subtraction.
1607   LOperand* left = UseRegisterAtStart(instr->right());
1608   LOperand* right = UseOrConstantAtStart(instr->left());
1609   LRSubI* rsb = new (zone()) LRSubI(left, right);
1610   LInstruction* result = DefineAsRegister(rsb);
1611   return result;
1612 }
1613
1614
1615 LInstruction* LChunkBuilder::DoMultiplyAdd(HMul* mul, HValue* addend) {
1616   LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1617   LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1618   LOperand* addend_op = UseRegisterAtStart(addend);
1619   return DefineSameAsFirst(
1620       new (zone()) LMultiplyAddD(addend_op, multiplier_op, multiplicand_op));
1621 }
1622
1623
1624 LInstruction* LChunkBuilder::DoMultiplySub(HValue* minuend, HMul* mul) {
1625   LOperand* minuend_op = UseRegisterAtStart(minuend);
1626   LOperand* multiplier_op = UseRegisterAtStart(mul->left());
1627   LOperand* multiplicand_op = UseRegisterAtStart(mul->right());
1628
1629   return DefineSameAsFirst(
1630       new (zone()) LMultiplySubD(minuend_op, multiplier_op, multiplicand_op));
1631 }
1632
1633
1634 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1635   if (instr->representation().IsSmiOrInteger32()) {
1636     DCHECK(instr->left()->representation().Equals(instr->representation()));
1637     DCHECK(instr->right()->representation().Equals(instr->representation()));
1638     LOperand* left = UseRegisterAtStart(instr->BetterLeftOperand());
1639     LOperand* right = UseOrConstantAtStart(instr->BetterRightOperand());
1640     LAddI* add = new (zone()) LAddI(left, right);
1641     LInstruction* result = DefineAsRegister(add);
1642     if (instr->CheckFlag(HValue::kCanOverflow)) {
1643       result = AssignEnvironment(result);
1644     }
1645     return result;
1646   } else if (instr->representation().IsExternal()) {
1647     DCHECK(instr->left()->representation().IsExternal());
1648     DCHECK(instr->right()->representation().IsInteger32());
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, CAN_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::DoCheckInstanceType(HCheckInstanceType* instr) {
2017   LOperand* value = UseRegisterAtStart(instr->value());
2018   LInstruction* result = new (zone()) LCheckInstanceType(value);
2019   return AssignEnvironment(result);
2020 }
2021
2022
2023 LInstruction* LChunkBuilder::DoCheckValue(HCheckValue* instr) {
2024   LOperand* value = UseRegisterAtStart(instr->value());
2025   return AssignEnvironment(new (zone()) LCheckValue(value));
2026 }
2027
2028
2029 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
2030   if (instr->IsStabilityCheck()) return new (zone()) LCheckMaps;
2031   LOperand* value = UseRegisterAtStart(instr->value());
2032   LInstruction* result = AssignEnvironment(new (zone()) LCheckMaps(value));
2033   if (instr->HasMigrationTarget()) {
2034     info()->MarkAsDeferredCalling();
2035     result = AssignPointerMap(result);
2036   }
2037   return result;
2038 }
2039
2040
2041 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
2042   HValue* value = instr->value();
2043   Representation input_rep = value->representation();
2044   LOperand* reg = UseRegister(value);
2045   if (input_rep.IsDouble()) {
2046     return DefineAsRegister(new (zone()) LClampDToUint8(reg));
2047   } else if (input_rep.IsInteger32()) {
2048     return DefineAsRegister(new (zone()) LClampIToUint8(reg));
2049   } else {
2050     DCHECK(input_rep.IsSmiOrTagged());
2051     LClampTToUint8* result =
2052         new (zone()) LClampTToUint8(reg, TempDoubleRegister());
2053     return AssignEnvironment(DefineAsRegister(result));
2054   }
2055 }
2056
2057
2058 LInstruction* LChunkBuilder::DoDoubleBits(HDoubleBits* instr) {
2059   HValue* value = instr->value();
2060   DCHECK(value->representation().IsDouble());
2061   return DefineAsRegister(new (zone()) LDoubleBits(UseRegister(value)));
2062 }
2063
2064
2065 LInstruction* LChunkBuilder::DoConstructDouble(HConstructDouble* instr) {
2066   LOperand* lo = UseRegister(instr->lo());
2067   LOperand* hi = UseRegister(instr->hi());
2068   return DefineAsRegister(new (zone()) LConstructDouble(hi, lo));
2069 }
2070
2071
2072 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
2073   LOperand* context = info()->IsStub() ? UseFixed(instr->context(), cp) : NULL;
2074   LOperand* parameter_count = UseRegisterOrConstant(instr->parameter_count());
2075   return new (zone())
2076       LReturn(UseFixed(instr->value(), r3), context, parameter_count);
2077 }
2078
2079
2080 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
2081   Representation r = instr->representation();
2082   if (r.IsSmi()) {
2083     return DefineAsRegister(new (zone()) LConstantS);
2084   } else if (r.IsInteger32()) {
2085     return DefineAsRegister(new (zone()) LConstantI);
2086   } else if (r.IsDouble()) {
2087     return DefineAsRegister(new (zone()) LConstantD);
2088   } else if (r.IsExternal()) {
2089     return DefineAsRegister(new (zone()) LConstantE);
2090   } else if (r.IsTagged()) {
2091     return DefineAsRegister(new (zone()) LConstantT);
2092   } else {
2093     UNREACHABLE();
2094     return NULL;
2095   }
2096 }
2097
2098
2099 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
2100   LLoadGlobalCell* result = new (zone()) LLoadGlobalCell;
2101   return instr->RequiresHoleCheck()
2102              ? AssignEnvironment(DefineAsRegister(result))
2103              : DefineAsRegister(result);
2104 }
2105
2106
2107 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
2108   LOperand* context = UseFixed(instr->context(), cp);
2109   LOperand* global_object =
2110       UseFixed(instr->global_object(), LoadDescriptor::ReceiverRegister());
2111   LOperand* vector = NULL;
2112   if (instr->HasVectorAndSlot()) {
2113     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2114   }
2115   LLoadGlobalGeneric* result =
2116       new (zone()) LLoadGlobalGeneric(context, global_object, vector);
2117   return MarkAsCall(DefineFixed(result, r3), instr);
2118 }
2119
2120
2121 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
2122   LOperand* value = UseRegister(instr->value());
2123   // Use a temp to check the value in the cell in the case where we perform
2124   // a hole check.
2125   return instr->RequiresHoleCheck()
2126              ? AssignEnvironment(new (zone())
2127                                  LStoreGlobalCell(value, TempRegister()))
2128              : new (zone()) LStoreGlobalCell(value, NULL);
2129 }
2130
2131
2132 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
2133   LOperand* context = UseRegisterAtStart(instr->value());
2134   LInstruction* result =
2135       DefineAsRegister(new (zone()) LLoadContextSlot(context));
2136   if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2137     result = AssignEnvironment(result);
2138   }
2139   return result;
2140 }
2141
2142
2143 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
2144   LOperand* context;
2145   LOperand* value;
2146   if (instr->NeedsWriteBarrier()) {
2147     context = UseTempRegister(instr->context());
2148     value = UseTempRegister(instr->value());
2149   } else {
2150     context = UseRegister(instr->context());
2151     value = UseRegister(instr->value());
2152   }
2153   LInstruction* result = new (zone()) LStoreContextSlot(context, value);
2154   if (instr->RequiresHoleCheck() && instr->DeoptimizesOnHole()) {
2155     result = AssignEnvironment(result);
2156   }
2157   return result;
2158 }
2159
2160
2161 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
2162   LOperand* obj = UseRegisterAtStart(instr->object());
2163   return DefineAsRegister(new (zone()) LLoadNamedField(obj));
2164 }
2165
2166
2167 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
2168   LOperand* context = UseFixed(instr->context(), cp);
2169   LOperand* object =
2170       UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2171   LOperand* vector = NULL;
2172   if (instr->HasVectorAndSlot()) {
2173     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2174   }
2175
2176   LInstruction* result =
2177       DefineFixed(new (zone()) LLoadNamedGeneric(context, object, vector), r3);
2178   return MarkAsCall(result, instr);
2179 }
2180
2181
2182 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
2183     HLoadFunctionPrototype* instr) {
2184   return AssignEnvironment(DefineAsRegister(
2185       new (zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
2186 }
2187
2188
2189 LInstruction* LChunkBuilder::DoLoadRoot(HLoadRoot* instr) {
2190   return DefineAsRegister(new (zone()) LLoadRoot);
2191 }
2192
2193
2194 LInstruction* LChunkBuilder::DoLoadKeyed(HLoadKeyed* instr) {
2195   DCHECK(instr->key()->representation().IsSmiOrInteger32());
2196   ElementsKind elements_kind = instr->elements_kind();
2197   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2198   LInstruction* result = NULL;
2199
2200   if (!instr->is_typed_elements()) {
2201     LOperand* obj = NULL;
2202     if (instr->representation().IsDouble()) {
2203       obj = UseRegister(instr->elements());
2204     } else {
2205       obj = UseRegisterAtStart(instr->elements());
2206     }
2207     result = DefineAsRegister(new (zone()) LLoadKeyed(obj, key));
2208   } else {
2209     DCHECK((instr->representation().IsInteger32() &&
2210             !IsDoubleOrFloatElementsKind(elements_kind)) ||
2211            (instr->representation().IsDouble() &&
2212             IsDoubleOrFloatElementsKind(elements_kind)));
2213     LOperand* backing_store = UseRegister(instr->elements());
2214     result = DefineAsRegister(new (zone()) LLoadKeyed(backing_store, key));
2215   }
2216
2217   if ((instr->is_external() || instr->is_fixed_typed_array())
2218           ?
2219           // see LCodeGen::DoLoadKeyedExternalArray
2220           ((elements_kind == EXTERNAL_UINT32_ELEMENTS ||
2221             elements_kind == UINT32_ELEMENTS) &&
2222            !instr->CheckFlag(HInstruction::kUint32))
2223           :
2224           // see LCodeGen::DoLoadKeyedFixedDoubleArray and
2225           // LCodeGen::DoLoadKeyedFixedArray
2226           instr->RequiresHoleCheck()) {
2227     result = AssignEnvironment(result);
2228   }
2229   return result;
2230 }
2231
2232
2233 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
2234   LOperand* context = UseFixed(instr->context(), cp);
2235   LOperand* object =
2236       UseFixed(instr->object(), LoadDescriptor::ReceiverRegister());
2237   LOperand* key = UseFixed(instr->key(), LoadDescriptor::NameRegister());
2238   LOperand* vector = NULL;
2239   if (instr->HasVectorAndSlot()) {
2240     vector = FixedTemp(VectorLoadICDescriptor::VectorRegister());
2241   }
2242
2243   LInstruction* result = DefineFixed(
2244       new (zone()) LLoadKeyedGeneric(context, object, key, vector), r3);
2245   return MarkAsCall(result, instr);
2246 }
2247
2248
2249 LInstruction* LChunkBuilder::DoStoreKeyed(HStoreKeyed* instr) {
2250   if (!instr->is_typed_elements()) {
2251     DCHECK(instr->elements()->representation().IsTagged());
2252     bool needs_write_barrier = instr->NeedsWriteBarrier();
2253     LOperand* object = NULL;
2254     LOperand* key = NULL;
2255     LOperand* val = NULL;
2256
2257     if (instr->value()->representation().IsDouble()) {
2258       object = UseRegisterAtStart(instr->elements());
2259       val = UseRegister(instr->value());
2260       key = UseRegisterOrConstantAtStart(instr->key());
2261     } else {
2262       if (needs_write_barrier) {
2263         object = UseTempRegister(instr->elements());
2264         val = UseTempRegister(instr->value());
2265         key = UseTempRegister(instr->key());
2266       } else {
2267         object = UseRegisterAtStart(instr->elements());
2268         val = UseRegisterAtStart(instr->value());
2269         key = UseRegisterOrConstantAtStart(instr->key());
2270       }
2271     }
2272
2273     return new (zone()) LStoreKeyed(object, key, val);
2274   }
2275
2276   DCHECK((instr->value()->representation().IsInteger32() &&
2277           !IsDoubleOrFloatElementsKind(instr->elements_kind())) ||
2278          (instr->value()->representation().IsDouble() &&
2279           IsDoubleOrFloatElementsKind(instr->elements_kind())));
2280   DCHECK((instr->is_fixed_typed_array() &&
2281           instr->elements()->representation().IsTagged()) ||
2282          (instr->is_external() &&
2283           instr->elements()->representation().IsExternal()));
2284   LOperand* val = UseRegister(instr->value());
2285   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
2286   LOperand* backing_store = UseRegister(instr->elements());
2287   return new (zone()) LStoreKeyed(backing_store, key, val);
2288 }
2289
2290
2291 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
2292   LOperand* context = UseFixed(instr->context(), cp);
2293   LOperand* obj =
2294       UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2295   LOperand* key = UseFixed(instr->key(), StoreDescriptor::NameRegister());
2296   LOperand* val = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2297
2298   DCHECK(instr->object()->representation().IsTagged());
2299   DCHECK(instr->key()->representation().IsTagged());
2300   DCHECK(instr->value()->representation().IsTagged());
2301
2302   return MarkAsCall(new (zone()) LStoreKeyedGeneric(context, obj, key, val),
2303                     instr);
2304 }
2305
2306
2307 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2308     HTransitionElementsKind* instr) {
2309   if (IsSimpleMapChangeTransition(instr->from_kind(), instr->to_kind())) {
2310     LOperand* object = UseRegister(instr->object());
2311     LOperand* new_map_reg = TempRegister();
2312     LTransitionElementsKind* result =
2313         new (zone()) LTransitionElementsKind(object, NULL, new_map_reg);
2314     return result;
2315   } else {
2316     LOperand* object = UseFixed(instr->object(), r3);
2317     LOperand* context = UseFixed(instr->context(), cp);
2318     LTransitionElementsKind* result =
2319         new (zone()) LTransitionElementsKind(object, context, NULL);
2320     return MarkAsCall(result, instr);
2321   }
2322 }
2323
2324
2325 LInstruction* LChunkBuilder::DoTrapAllocationMemento(
2326     HTrapAllocationMemento* instr) {
2327   LOperand* object = UseRegister(instr->object());
2328   LOperand* temp = TempRegister();
2329   LTrapAllocationMemento* result =
2330       new (zone()) LTrapAllocationMemento(object, temp);
2331   return AssignEnvironment(result);
2332 }
2333
2334
2335 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2336   bool is_in_object = instr->access().IsInobject();
2337   bool needs_write_barrier = instr->NeedsWriteBarrier();
2338   bool needs_write_barrier_for_map =
2339       instr->has_transition() && instr->NeedsWriteBarrierForMap();
2340
2341   LOperand* obj;
2342   if (needs_write_barrier) {
2343     obj = is_in_object ? UseRegister(instr->object())
2344                        : UseTempRegister(instr->object());
2345   } else {
2346     obj = needs_write_barrier_for_map ? UseRegister(instr->object())
2347                                       : UseRegisterAtStart(instr->object());
2348   }
2349
2350   LOperand* val;
2351   if (needs_write_barrier) {
2352     val = UseTempRegister(instr->value());
2353   } else if (instr->field_representation().IsDouble()) {
2354     val = UseRegisterAtStart(instr->value());
2355   } else {
2356     val = UseRegister(instr->value());
2357   }
2358
2359   // We need a temporary register for write barrier of the map field.
2360   LOperand* temp = needs_write_barrier_for_map ? TempRegister() : NULL;
2361
2362   return new (zone()) LStoreNamedField(obj, val, temp);
2363 }
2364
2365
2366 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2367   LOperand* context = UseFixed(instr->context(), cp);
2368   LOperand* obj =
2369       UseFixed(instr->object(), StoreDescriptor::ReceiverRegister());
2370   LOperand* val = UseFixed(instr->value(), StoreDescriptor::ValueRegister());
2371
2372   LInstruction* result = new (zone()) LStoreNamedGeneric(context, obj, val);
2373   return MarkAsCall(result, instr);
2374 }
2375
2376
2377 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2378   LOperand* context = UseFixed(instr->context(), cp);
2379   LOperand* left = UseFixed(instr->left(), r4);
2380   LOperand* right = UseFixed(instr->right(), r3);
2381   return MarkAsCall(
2382       DefineFixed(new (zone()) LStringAdd(context, left, right), r3), instr);
2383 }
2384
2385
2386 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2387   LOperand* string = UseTempRegister(instr->string());
2388   LOperand* index = UseTempRegister(instr->index());
2389   LOperand* context = UseAny(instr->context());
2390   LStringCharCodeAt* result =
2391       new (zone()) LStringCharCodeAt(context, string, index);
2392   return AssignPointerMap(DefineAsRegister(result));
2393 }
2394
2395
2396 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2397   LOperand* char_code = UseRegister(instr->value());
2398   LOperand* context = UseAny(instr->context());
2399   LStringCharFromCode* result =
2400       new (zone()) LStringCharFromCode(context, char_code);
2401   return AssignPointerMap(DefineAsRegister(result));
2402 }
2403
2404
2405 LInstruction* LChunkBuilder::DoAllocate(HAllocate* instr) {
2406   info()->MarkAsDeferredCalling();
2407   LOperand* context = UseAny(instr->context());
2408   LOperand* size = UseRegisterOrConstant(instr->size());
2409   LOperand* temp1 = TempRegister();
2410   LOperand* temp2 = TempRegister();
2411   LAllocate* result = new (zone()) LAllocate(context, size, temp1, temp2);
2412   return AssignPointerMap(DefineAsRegister(result));
2413 }
2414
2415
2416 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2417   LOperand* context = UseFixed(instr->context(), cp);
2418   return MarkAsCall(DefineFixed(new (zone()) LRegExpLiteral(context), r3),
2419                     instr);
2420 }
2421
2422
2423 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2424   LOperand* context = UseFixed(instr->context(), cp);
2425   return MarkAsCall(DefineFixed(new (zone()) LFunctionLiteral(context), r3),
2426                     instr);
2427 }
2428
2429
2430 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2431   DCHECK(argument_count_ == 0);
2432   allocator_->MarkAsOsrEntry();
2433   current_block_->last_environment()->set_ast_id(instr->ast_id());
2434   return AssignEnvironment(new (zone()) LOsrEntry);
2435 }
2436
2437
2438 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2439   LParameter* result = new (zone()) LParameter;
2440   if (instr->kind() == HParameter::STACK_PARAMETER) {
2441     int spill_index = chunk()->GetParameterStackSlot(instr->index());
2442     return DefineAsSpilled(result, spill_index);
2443   } else {
2444     DCHECK(info()->IsStub());
2445     CallInterfaceDescriptor descriptor =
2446         info()->code_stub()->GetCallInterfaceDescriptor();
2447     int index = static_cast<int>(instr->index());
2448     Register reg = descriptor.GetEnvironmentParameterRegister(index);
2449     return DefineFixed(result, reg);
2450   }
2451 }
2452
2453
2454 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2455   // Use an index that corresponds to the location in the unoptimized frame,
2456   // which the optimized frame will subsume.
2457   int env_index = instr->index();
2458   int spill_index = 0;
2459   if (instr->environment()->is_parameter_index(env_index)) {
2460     spill_index = chunk()->GetParameterStackSlot(env_index);
2461   } else {
2462     spill_index = env_index - instr->environment()->first_local_index();
2463     if (spill_index > LUnallocated::kMaxFixedSlotIndex) {
2464       Retry(kTooManySpillSlotsNeededForOSR);
2465       spill_index = 0;
2466     }
2467   }
2468   return DefineAsSpilled(new (zone()) LUnknownOSRValue, spill_index);
2469 }
2470
2471
2472 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2473   LOperand* context = UseFixed(instr->context(), cp);
2474   return MarkAsCall(DefineFixed(new (zone()) LCallStub(context), r3), instr);
2475 }
2476
2477
2478 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2479   // There are no real uses of the arguments object.
2480   // arguments.length and element access are supported directly on
2481   // stack arguments, and any real arguments object use causes a bailout.
2482   // So this value is never used.
2483   return NULL;
2484 }
2485
2486
2487 LInstruction* LChunkBuilder::DoCapturedObject(HCapturedObject* instr) {
2488   instr->ReplayEnvironment(current_block_->last_environment());
2489
2490   // There are no real uses of a captured object.
2491   return NULL;
2492 }
2493
2494
2495 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2496   info()->MarkAsRequiresFrame();
2497   LOperand* args = UseRegister(instr->arguments());
2498   LOperand* length = UseRegisterOrConstantAtStart(instr->length());
2499   LOperand* index = UseRegisterOrConstantAtStart(instr->index());
2500   return DefineAsRegister(new (zone()) LAccessArgumentsAt(args, length, index));
2501 }
2502
2503
2504 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2505   LOperand* object = UseFixed(instr->value(), r3);
2506   LToFastProperties* result = new (zone()) LToFastProperties(object);
2507   return MarkAsCall(DefineFixed(result, r3), instr);
2508 }
2509
2510
2511 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2512   LOperand* context = UseFixed(instr->context(), cp);
2513   LTypeof* result = new (zone()) LTypeof(context, UseFixed(instr->value(), r3));
2514   return MarkAsCall(DefineFixed(result, r3), instr);
2515 }
2516
2517
2518 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2519   return new (zone()) LTypeofIsAndBranch(UseRegister(instr->value()));
2520 }
2521
2522
2523 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2524     HIsConstructCallAndBranch* instr) {
2525   return new (zone()) LIsConstructCallAndBranch(TempRegister());
2526 }
2527
2528
2529 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2530   instr->ReplayEnvironment(current_block_->last_environment());
2531   return NULL;
2532 }
2533
2534
2535 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2536   if (instr->is_function_entry()) {
2537     LOperand* context = UseFixed(instr->context(), cp);
2538     return MarkAsCall(new (zone()) LStackCheck(context), instr);
2539   } else {
2540     DCHECK(instr->is_backwards_branch());
2541     LOperand* context = UseAny(instr->context());
2542     return AssignEnvironment(
2543         AssignPointerMap(new (zone()) LStackCheck(context)));
2544   }
2545 }
2546
2547
2548 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2549   HEnvironment* outer = current_block_->last_environment();
2550   outer->set_ast_id(instr->ReturnId());
2551   HConstant* undefined = graph()->GetConstantUndefined();
2552   HEnvironment* inner = outer->CopyForInlining(
2553       instr->closure(), instr->arguments_count(), instr->function(), undefined,
2554       instr->inlining_kind());
2555   // Only replay binding of arguments object if it wasn't removed from graph.
2556   if (instr->arguments_var() != NULL && instr->arguments_object()->IsLinked()) {
2557     inner->Bind(instr->arguments_var(), instr->arguments_object());
2558   }
2559   inner->BindContext(instr->closure_context());
2560   inner->set_entry(instr);
2561   current_block_->UpdateEnvironment(inner);
2562   chunk_->AddInlinedClosure(instr->closure());
2563   return NULL;
2564 }
2565
2566
2567 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2568   LInstruction* pop = NULL;
2569
2570   HEnvironment* env = current_block_->last_environment();
2571
2572   if (env->entry()->arguments_pushed()) {
2573     int argument_count = env->arguments_environment()->parameter_count();
2574     pop = new (zone()) LDrop(argument_count);
2575     DCHECK(instr->argument_delta() == -argument_count);
2576   }
2577
2578   HEnvironment* outer =
2579       current_block_->last_environment()->DiscardInlined(false);
2580   current_block_->UpdateEnvironment(outer);
2581
2582   return pop;
2583 }
2584
2585
2586 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2587   LOperand* context = UseFixed(instr->context(), cp);
2588   LOperand* object = UseFixed(instr->enumerable(), r3);
2589   LForInPrepareMap* result = new (zone()) LForInPrepareMap(context, object);
2590   return MarkAsCall(DefineFixed(result, r3), instr, CAN_DEOPTIMIZE_EAGERLY);
2591 }
2592
2593
2594 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2595   LOperand* map = UseRegister(instr->map());
2596   return AssignEnvironment(
2597       DefineAsRegister(new (zone()) LForInCacheArray(map)));
2598 }
2599
2600
2601 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2602   LOperand* value = UseRegisterAtStart(instr->value());
2603   LOperand* map = UseRegisterAtStart(instr->map());
2604   return AssignEnvironment(new (zone()) LCheckMapValue(value, map));
2605 }
2606
2607
2608 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2609   LOperand* object = UseRegister(instr->object());
2610   LOperand* index = UseTempRegister(instr->index());
2611   LLoadFieldByIndex* load = new (zone()) LLoadFieldByIndex(object, index);
2612   LInstruction* result = DefineSameAsFirst(load);
2613   return AssignPointerMap(result);
2614 }
2615
2616
2617 LInstruction* LChunkBuilder::DoStoreFrameContext(HStoreFrameContext* instr) {
2618   LOperand* context = UseRegisterAtStart(instr->context());
2619   return new (zone()) LStoreFrameContext(context);
2620 }
2621
2622
2623 LInstruction* LChunkBuilder::DoAllocateBlockContext(
2624     HAllocateBlockContext* instr) {
2625   LOperand* context = UseFixed(instr->context(), cp);
2626   LOperand* function = UseRegisterAtStart(instr->function());
2627   LAllocateBlockContext* result =
2628       new (zone()) LAllocateBlockContext(context, function);
2629   return MarkAsCall(DefineFixed(result, cp), instr);
2630 }
2631 }
2632 }  // namespace v8::internal