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