3ba0cae7ad9c4107d6deaf59d9423577516d91ba
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / x64 / lithium-x64.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #if defined(V8_TARGET_ARCH_X64)
31
32 #include "lithium-allocator-inl.h"
33 #include "x64/lithium-x64.h"
34 #include "x64/lithium-codegen-x64.h"
35
36 namespace v8 {
37 namespace internal {
38
39 #define DEFINE_COMPILE(type)                            \
40   void L##type::CompileToNative(LCodeGen* generator) {  \
41     generator->Do##type(this);                          \
42   }
43 LITHIUM_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
44 #undef DEFINE_COMPILE
45
46 LOsrEntry::LOsrEntry() {
47   for (int i = 0; i < Register::kNumAllocatableRegisters; ++i) {
48     register_spills_[i] = NULL;
49   }
50   for (int i = 0; i < DoubleRegister::kNumAllocatableRegisters; ++i) {
51     double_register_spills_[i] = NULL;
52   }
53 }
54
55
56 void LOsrEntry::MarkSpilledRegister(int allocation_index,
57                                     LOperand* spill_operand) {
58   ASSERT(spill_operand->IsStackSlot());
59   ASSERT(register_spills_[allocation_index] == NULL);
60   register_spills_[allocation_index] = spill_operand;
61 }
62
63
64 void LOsrEntry::MarkSpilledDoubleRegister(int allocation_index,
65                                           LOperand* spill_operand) {
66   ASSERT(spill_operand->IsDoubleStackSlot());
67   ASSERT(double_register_spills_[allocation_index] == NULL);
68   double_register_spills_[allocation_index] = spill_operand;
69 }
70
71
72 #ifdef DEBUG
73 void LInstruction::VerifyCall() {
74   // Call instructions can use only fixed registers as temporaries and
75   // outputs because all registers are blocked by the calling convention.
76   // Inputs operands must use a fixed register or use-at-start policy or
77   // a non-register policy.
78   ASSERT(Output() == NULL ||
79          LUnallocated::cast(Output())->HasFixedPolicy() ||
80          !LUnallocated::cast(Output())->HasRegisterPolicy());
81   for (UseIterator it(this); !it.Done(); it.Advance()) {
82     LUnallocated* operand = LUnallocated::cast(it.Current());
83     ASSERT(operand->HasFixedPolicy() ||
84            operand->IsUsedAtStart());
85   }
86   for (TempIterator it(this); !it.Done(); it.Advance()) {
87     LUnallocated* operand = LUnallocated::cast(it.Current());
88     ASSERT(operand->HasFixedPolicy() ||!operand->HasRegisterPolicy());
89   }
90 }
91 #endif
92
93
94 void LInstruction::PrintTo(StringStream* stream) {
95   stream->Add("%s ", this->Mnemonic());
96
97   PrintOutputOperandTo(stream);
98
99   PrintDataTo(stream);
100
101   if (HasEnvironment()) {
102     stream->Add(" ");
103     environment()->PrintTo(stream);
104   }
105
106   if (HasPointerMap()) {
107     stream->Add(" ");
108     pointer_map()->PrintTo(stream);
109   }
110 }
111
112
113 void LInstruction::PrintDataTo(StringStream* stream) {
114   stream->Add("= ");
115   for (int i = 0; i < InputCount(); i++) {
116     if (i > 0) stream->Add(" ");
117     InputAt(i)->PrintTo(stream);
118   }
119 }
120
121
122 void LInstruction::PrintOutputOperandTo(StringStream* stream) {
123   if (HasResult()) result()->PrintTo(stream);
124 }
125
126
127 void LLabel::PrintDataTo(StringStream* stream) {
128   LGap::PrintDataTo(stream);
129   LLabel* rep = replacement();
130   if (rep != NULL) {
131     stream->Add(" Dead block replaced with B%d", rep->block_id());
132   }
133 }
134
135
136 bool LGap::IsRedundant() const {
137   for (int i = 0; i < 4; i++) {
138     if (parallel_moves_[i] != NULL && !parallel_moves_[i]->IsRedundant()) {
139       return false;
140     }
141   }
142
143   return true;
144 }
145
146
147 void LGap::PrintDataTo(StringStream* stream) {
148   for (int i = 0; i < 4; i++) {
149     stream->Add("(");
150     if (parallel_moves_[i] != NULL) {
151       parallel_moves_[i]->PrintDataTo(stream);
152     }
153     stream->Add(") ");
154   }
155 }
156
157
158 const char* LArithmeticD::Mnemonic() const {
159   switch (op()) {
160     case Token::ADD: return "add-d";
161     case Token::SUB: return "sub-d";
162     case Token::MUL: return "mul-d";
163     case Token::DIV: return "div-d";
164     case Token::MOD: return "mod-d";
165     default:
166       UNREACHABLE();
167       return NULL;
168   }
169 }
170
171
172 const char* LArithmeticT::Mnemonic() const {
173   switch (op()) {
174     case Token::ADD: return "add-t";
175     case Token::SUB: return "sub-t";
176     case Token::MUL: return "mul-t";
177     case Token::MOD: return "mod-t";
178     case Token::DIV: return "div-t";
179     case Token::BIT_AND: return "bit-and-t";
180     case Token::BIT_OR: return "bit-or-t";
181     case Token::BIT_XOR: return "bit-xor-t";
182     case Token::SHL: return "sal-t";
183     case Token::SAR: return "sar-t";
184     case Token::SHR: return "shr-t";
185     default:
186       UNREACHABLE();
187       return NULL;
188   }
189 }
190
191
192 void LGoto::PrintDataTo(StringStream* stream) {
193   stream->Add("B%d", block_id());
194 }
195
196
197 void LBranch::PrintDataTo(StringStream* stream) {
198   stream->Add("B%d | B%d on ", true_block_id(), false_block_id());
199   InputAt(0)->PrintTo(stream);
200 }
201
202
203 void LCmpIDAndBranch::PrintDataTo(StringStream* stream) {
204   stream->Add("if ");
205   InputAt(0)->PrintTo(stream);
206   stream->Add(" %s ", Token::String(op()));
207   InputAt(1)->PrintTo(stream);
208   stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
209 }
210
211
212 void LIsNilAndBranch::PrintDataTo(StringStream* stream) {
213   stream->Add("if ");
214   InputAt(0)->PrintTo(stream);
215   stream->Add(kind() == kStrictEquality ? " === " : " == ");
216   stream->Add(nil() == kNullValue ? "null" : "undefined");
217   stream->Add(" then B%d else B%d", true_block_id(), false_block_id());
218 }
219
220
221 void LIsObjectAndBranch::PrintDataTo(StringStream* stream) {
222   stream->Add("if is_object(");
223   InputAt(0)->PrintTo(stream);
224   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
225 }
226
227
228 void LIsStringAndBranch::PrintDataTo(StringStream* stream) {
229   stream->Add("if is_string(");
230   InputAt(0)->PrintTo(stream);
231   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
232 }
233
234
235 void LIsSmiAndBranch::PrintDataTo(StringStream* stream) {
236   stream->Add("if is_smi(");
237   InputAt(0)->PrintTo(stream);
238   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
239 }
240
241
242 void LIsUndetectableAndBranch::PrintDataTo(StringStream* stream) {
243   stream->Add("if is_undetectable(");
244   InputAt(0)->PrintTo(stream);
245   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
246 }
247
248
249 void LStringCompareAndBranch::PrintDataTo(StringStream* stream) {
250   stream->Add("if string_compare(");
251   InputAt(0)->PrintTo(stream);
252   InputAt(1)->PrintTo(stream);
253   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
254 }
255
256
257 void LHasInstanceTypeAndBranch::PrintDataTo(StringStream* stream) {
258   stream->Add("if has_instance_type(");
259   InputAt(0)->PrintTo(stream);
260   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
261 }
262
263
264 void LHasCachedArrayIndexAndBranch::PrintDataTo(StringStream* stream) {
265   stream->Add("if has_cached_array_index(");
266   InputAt(0)->PrintTo(stream);
267   stream->Add(") then B%d else B%d", true_block_id(), false_block_id());
268 }
269
270
271 void LClassOfTestAndBranch::PrintDataTo(StringStream* stream) {
272   stream->Add("if class_of_test(");
273   InputAt(0)->PrintTo(stream);
274   stream->Add(", \"%o\") then B%d else B%d",
275               *hydrogen()->class_name(),
276               true_block_id(),
277               false_block_id());
278 }
279
280
281 void LTypeofIsAndBranch::PrintDataTo(StringStream* stream) {
282   stream->Add("if typeof ");
283   InputAt(0)->PrintTo(stream);
284   stream->Add(" == \"%s\" then B%d else B%d",
285               *hydrogen()->type_literal()->ToCString(),
286               true_block_id(), false_block_id());
287 }
288
289
290 void LCallConstantFunction::PrintDataTo(StringStream* stream) {
291   stream->Add("#%d / ", arity());
292 }
293
294
295 void LUnaryMathOperation::PrintDataTo(StringStream* stream) {
296   stream->Add("/%s ", hydrogen()->OpName());
297   InputAt(0)->PrintTo(stream);
298 }
299
300
301 void LLoadContextSlot::PrintDataTo(StringStream* stream) {
302   InputAt(0)->PrintTo(stream);
303   stream->Add("[%d]", slot_index());
304 }
305
306
307 void LStoreContextSlot::PrintDataTo(StringStream* stream) {
308   InputAt(0)->PrintTo(stream);
309   stream->Add("[%d] <- ", slot_index());
310   InputAt(1)->PrintTo(stream);
311 }
312
313
314 void LInvokeFunction::PrintDataTo(StringStream* stream) {
315   stream->Add("= ");
316   InputAt(0)->PrintTo(stream);
317   stream->Add(" #%d / ", arity());
318 }
319
320
321 void LCallKeyed::PrintDataTo(StringStream* stream) {
322   stream->Add("[rcx] #%d / ", arity());
323 }
324
325
326 void LCallNamed::PrintDataTo(StringStream* stream) {
327   SmartArrayPointer<char> name_string = name()->ToCString();
328   stream->Add("%s #%d / ", *name_string, arity());
329 }
330
331
332 void LCallGlobal::PrintDataTo(StringStream* stream) {
333   SmartArrayPointer<char> name_string = name()->ToCString();
334   stream->Add("%s #%d / ", *name_string, arity());
335 }
336
337
338 void LCallKnownGlobal::PrintDataTo(StringStream* stream) {
339   stream->Add("#%d / ", arity());
340 }
341
342
343 void LCallNew::PrintDataTo(StringStream* stream) {
344   stream->Add("= ");
345   InputAt(0)->PrintTo(stream);
346   stream->Add(" #%d / ", arity());
347 }
348
349
350 void LAccessArgumentsAt::PrintDataTo(StringStream* stream) {
351   arguments()->PrintTo(stream);
352
353   stream->Add(" length ");
354   length()->PrintTo(stream);
355
356   stream->Add(" index ");
357   index()->PrintTo(stream);
358 }
359
360
361 int LChunk::GetNextSpillIndex(bool is_double) {
362   return spill_slot_count_++;
363 }
364
365
366 LOperand* LChunk::GetNextSpillSlot(bool is_double) {
367   // All stack slots are Double stack slots on x64.
368   // Alternatively, at some point, start using half-size
369   // stack slots for int32 values.
370   int index = GetNextSpillIndex(is_double);
371   if (is_double) {
372     return LDoubleStackSlot::Create(index);
373   } else {
374     return LStackSlot::Create(index);
375   }
376 }
377
378
379 void LChunk::MarkEmptyBlocks() {
380   HPhase phase("L_Mark empty blocks", this);
381   for (int i = 0; i < graph()->blocks()->length(); ++i) {
382     HBasicBlock* block = graph()->blocks()->at(i);
383     int first = block->first_instruction_index();
384     int last = block->last_instruction_index();
385     LInstruction* first_instr = instructions()->at(first);
386     LInstruction* last_instr = instructions()->at(last);
387
388     LLabel* label = LLabel::cast(first_instr);
389     if (last_instr->IsGoto()) {
390       LGoto* goto_instr = LGoto::cast(last_instr);
391       if (label->IsRedundant() &&
392           !label->is_loop_header()) {
393         bool can_eliminate = true;
394         for (int i = first + 1; i < last && can_eliminate; ++i) {
395           LInstruction* cur = instructions()->at(i);
396           if (cur->IsGap()) {
397             LGap* gap = LGap::cast(cur);
398             if (!gap->IsRedundant()) {
399               can_eliminate = false;
400             }
401           } else {
402             can_eliminate = false;
403           }
404         }
405
406         if (can_eliminate) {
407           label->set_replacement(GetLabel(goto_instr->block_id()));
408         }
409       }
410     }
411   }
412 }
413
414
415 void LStoreNamedField::PrintDataTo(StringStream* stream) {
416   object()->PrintTo(stream);
417   stream->Add(".");
418   stream->Add(*String::cast(*name())->ToCString());
419   stream->Add(" <- ");
420   value()->PrintTo(stream);
421 }
422
423
424 void LStoreNamedGeneric::PrintDataTo(StringStream* stream) {
425   object()->PrintTo(stream);
426   stream->Add(".");
427   stream->Add(*String::cast(*name())->ToCString());
428   stream->Add(" <- ");
429   value()->PrintTo(stream);
430 }
431
432
433 void LStoreKeyedFastElement::PrintDataTo(StringStream* stream) {
434   object()->PrintTo(stream);
435   stream->Add("[");
436   key()->PrintTo(stream);
437   stream->Add("] <- ");
438   value()->PrintTo(stream);
439 }
440
441
442 void LStoreKeyedFastDoubleElement::PrintDataTo(StringStream* stream) {
443   elements()->PrintTo(stream);
444   stream->Add("[");
445   key()->PrintTo(stream);
446   stream->Add("] <- ");
447   value()->PrintTo(stream);
448 }
449
450
451 void LStoreKeyedGeneric::PrintDataTo(StringStream* stream) {
452   object()->PrintTo(stream);
453   stream->Add("[");
454   key()->PrintTo(stream);
455   stream->Add("] <- ");
456   value()->PrintTo(stream);
457 }
458
459
460 void LTransitionElementsKind::PrintDataTo(StringStream* stream) {
461   object()->PrintTo(stream);
462   stream->Add(" %p -> %p", *original_map(), *transitioned_map());
463 }
464
465
466 void LChunk::AddInstruction(LInstruction* instr, HBasicBlock* block) {
467   LInstructionGap* gap = new(graph_->zone()) LInstructionGap(block);
468   int index = -1;
469   if (instr->IsControl()) {
470     instructions_.Add(gap);
471     index = instructions_.length();
472     instructions_.Add(instr);
473   } else {
474     index = instructions_.length();
475     instructions_.Add(instr);
476     instructions_.Add(gap);
477   }
478   if (instr->HasPointerMap()) {
479     pointer_maps_.Add(instr->pointer_map());
480     instr->pointer_map()->set_lithium_position(index);
481   }
482 }
483
484
485 LConstantOperand* LChunk::DefineConstantOperand(HConstant* constant) {
486   return LConstantOperand::Create(constant->id());
487 }
488
489
490 int LChunk::GetParameterStackSlot(int index) const {
491   // The receiver is at index 0, the first parameter at index 1, so we
492   // shift all parameter indexes down by the number of parameters, and
493   // make sure they end up negative so they are distinguishable from
494   // spill slots.
495   int result = index - info()->scope()->num_parameters() - 1;
496   ASSERT(result < 0);
497   return result;
498 }
499
500 // A parameter relative to ebp in the arguments stub.
501 int LChunk::ParameterAt(int index) {
502   ASSERT(-1 <= index);  // -1 is the receiver.
503   return (1 + info()->scope()->num_parameters() - index) *
504       kPointerSize;
505 }
506
507
508 LGap* LChunk::GetGapAt(int index) const {
509   return LGap::cast(instructions_[index]);
510 }
511
512
513 bool LChunk::IsGapAt(int index) const {
514   return instructions_[index]->IsGap();
515 }
516
517
518 int LChunk::NearestGapPos(int index) const {
519   while (!IsGapAt(index)) index--;
520   return index;
521 }
522
523
524 void LChunk::AddGapMove(int index, LOperand* from, LOperand* to) {
525   GetGapAt(index)->GetOrCreateParallelMove(LGap::START)->AddMove(from, to);
526 }
527
528
529 Handle<Object> LChunk::LookupLiteral(LConstantOperand* operand) const {
530   return HConstant::cast(graph_->LookupValue(operand->index()))->handle();
531 }
532
533
534 Representation LChunk::LookupLiteralRepresentation(
535     LConstantOperand* operand) const {
536   return graph_->LookupValue(operand->index())->representation();
537 }
538
539
540 LChunk* LChunkBuilder::Build() {
541   ASSERT(is_unused());
542   chunk_ = new(zone()) LChunk(info(), graph());
543   HPhase phase("L_Building chunk", chunk_);
544   status_ = BUILDING;
545   const ZoneList<HBasicBlock*>* blocks = graph()->blocks();
546   for (int i = 0; i < blocks->length(); i++) {
547     HBasicBlock* next = NULL;
548     if (i < blocks->length() - 1) next = blocks->at(i + 1);
549     DoBasicBlock(blocks->at(i), next);
550     if (is_aborted()) return NULL;
551   }
552   status_ = DONE;
553   return chunk_;
554 }
555
556
557 void LChunkBuilder::Abort(const char* format, ...) {
558   if (FLAG_trace_bailout) {
559     SmartArrayPointer<char> name(
560         info()->shared_info()->DebugName()->ToCString());
561     PrintF("Aborting LChunk building in @\"%s\": ", *name);
562     va_list arguments;
563     va_start(arguments, format);
564     OS::VPrint(format, arguments);
565     va_end(arguments);
566     PrintF("\n");
567   }
568   status_ = ABORTED;
569 }
570
571
572 LUnallocated* LChunkBuilder::ToUnallocated(Register reg) {
573   return new(zone()) LUnallocated(LUnallocated::FIXED_REGISTER,
574                                   Register::ToAllocationIndex(reg));
575 }
576
577
578 LUnallocated* LChunkBuilder::ToUnallocated(XMMRegister reg) {
579   return new(zone()) LUnallocated(LUnallocated::FIXED_DOUBLE_REGISTER,
580                                   XMMRegister::ToAllocationIndex(reg));
581 }
582
583
584 LOperand* LChunkBuilder::UseFixed(HValue* value, Register fixed_register) {
585   return Use(value, ToUnallocated(fixed_register));
586 }
587
588
589 LOperand* LChunkBuilder::UseFixedDouble(HValue* value, XMMRegister reg) {
590   return Use(value, ToUnallocated(reg));
591 }
592
593
594 LOperand* LChunkBuilder::UseRegister(HValue* value) {
595   return Use(value, new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
596 }
597
598
599 LOperand* LChunkBuilder::UseRegisterAtStart(HValue* value) {
600   return Use(value,
601              new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER,
602                               LUnallocated::USED_AT_START));
603 }
604
605
606 LOperand* LChunkBuilder::UseTempRegister(HValue* value) {
607   return Use(value, new(zone()) LUnallocated(LUnallocated::WRITABLE_REGISTER));
608 }
609
610
611 LOperand* LChunkBuilder::Use(HValue* value) {
612   return Use(value, new(zone()) LUnallocated(LUnallocated::NONE));
613 }
614
615
616 LOperand* LChunkBuilder::UseAtStart(HValue* value) {
617   return Use(value, new(zone()) LUnallocated(LUnallocated::NONE,
618                                      LUnallocated::USED_AT_START));
619 }
620
621
622 LOperand* LChunkBuilder::UseOrConstant(HValue* value) {
623   return value->IsConstant()
624       ? chunk_->DefineConstantOperand(HConstant::cast(value))
625       : Use(value);
626 }
627
628
629 LOperand* LChunkBuilder::UseOrConstantAtStart(HValue* value) {
630   return value->IsConstant()
631       ? chunk_->DefineConstantOperand(HConstant::cast(value))
632       : UseAtStart(value);
633 }
634
635
636 LOperand* LChunkBuilder::UseRegisterOrConstant(HValue* value) {
637   return value->IsConstant()
638       ? chunk_->DefineConstantOperand(HConstant::cast(value))
639       : UseRegister(value);
640 }
641
642
643 LOperand* LChunkBuilder::UseRegisterOrConstantAtStart(HValue* value) {
644   return value->IsConstant()
645       ? chunk_->DefineConstantOperand(HConstant::cast(value))
646       : UseRegisterAtStart(value);
647 }
648
649
650 LOperand* LChunkBuilder::UseAny(HValue* value) {
651   return value->IsConstant()
652       ? chunk_->DefineConstantOperand(HConstant::cast(value))
653       :  Use(value, new(zone()) LUnallocated(LUnallocated::ANY));
654 }
655
656
657 LOperand* LChunkBuilder::Use(HValue* value, LUnallocated* operand) {
658   if (value->EmitAtUses()) {
659     HInstruction* instr = HInstruction::cast(value);
660     VisitInstruction(instr);
661   }
662   operand->set_virtual_register(value->id());
663   return operand;
664 }
665
666
667 template<int I, int T>
668 LInstruction* LChunkBuilder::Define(LTemplateInstruction<1, I, T>* instr,
669                                     LUnallocated* result) {
670   result->set_virtual_register(current_instruction_->id());
671   instr->set_result(result);
672   return instr;
673 }
674
675
676 template<int I, int T>
677 LInstruction* LChunkBuilder::DefineAsRegister(
678     LTemplateInstruction<1, I, T>* instr) {
679   return Define(instr,
680                 new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER));
681 }
682
683
684 template<int I, int T>
685 LInstruction* LChunkBuilder::DefineAsSpilled(
686     LTemplateInstruction<1, I, T>* instr,
687     int index) {
688   return Define(instr,
689                 new(zone()) LUnallocated(LUnallocated::FIXED_SLOT, index));
690 }
691
692
693 template<int I, int T>
694 LInstruction* LChunkBuilder::DefineSameAsFirst(
695     LTemplateInstruction<1, I, T>* instr) {
696   return Define(instr,
697                 new(zone()) LUnallocated(LUnallocated::SAME_AS_FIRST_INPUT));
698 }
699
700
701 template<int I, int T>
702 LInstruction* LChunkBuilder::DefineFixed(LTemplateInstruction<1, I, T>* instr,
703                                          Register reg) {
704   return Define(instr, ToUnallocated(reg));
705 }
706
707
708 template<int I, int T>
709 LInstruction* LChunkBuilder::DefineFixedDouble(
710     LTemplateInstruction<1, I, T>* instr,
711     XMMRegister reg) {
712   return Define(instr, ToUnallocated(reg));
713 }
714
715
716 LInstruction* LChunkBuilder::AssignEnvironment(LInstruction* instr) {
717   HEnvironment* hydrogen_env = current_block_->last_environment();
718   int argument_index_accumulator = 0;
719   instr->set_environment(CreateEnvironment(hydrogen_env,
720                                            &argument_index_accumulator));
721   return instr;
722 }
723
724
725 LInstruction* LChunkBuilder::MarkAsCall(LInstruction* instr,
726                                         HInstruction* hinstr,
727                                         CanDeoptimize can_deoptimize) {
728 #ifdef DEBUG
729   instr->VerifyCall();
730 #endif
731   instr->MarkAsCall();
732   instr = AssignPointerMap(instr);
733
734   if (hinstr->HasObservableSideEffects()) {
735     ASSERT(hinstr->next()->IsSimulate());
736     HSimulate* sim = HSimulate::cast(hinstr->next());
737     ASSERT(instruction_pending_deoptimization_environment_ == NULL);
738     ASSERT(pending_deoptimization_ast_id_ == AstNode::kNoNumber);
739     instruction_pending_deoptimization_environment_ = instr;
740     pending_deoptimization_ast_id_ = sim->ast_id();
741   }
742
743   // If instruction does not have side-effects lazy deoptimization
744   // after the call will try to deoptimize to the point before the call.
745   // Thus we still need to attach environment to this call even if
746   // call sequence can not deoptimize eagerly.
747   bool needs_environment =
748       (can_deoptimize == CAN_DEOPTIMIZE_EAGERLY) ||
749       !hinstr->HasObservableSideEffects();
750   if (needs_environment && !instr->HasEnvironment()) {
751     instr = AssignEnvironment(instr);
752   }
753
754   return instr;
755 }
756
757
758 LInstruction* LChunkBuilder::AssignPointerMap(LInstruction* instr) {
759   ASSERT(!instr->HasPointerMap());
760   instr->set_pointer_map(new(zone()) LPointerMap(position_));
761   return instr;
762 }
763
764
765 LUnallocated* LChunkBuilder::TempRegister() {
766   LUnallocated* operand =
767       new(zone()) LUnallocated(LUnallocated::MUST_HAVE_REGISTER);
768   operand->set_virtual_register(allocator_->GetVirtualRegister());
769   if (!allocator_->AllocationOk()) Abort("Not enough virtual registers.");
770   return operand;
771 }
772
773
774 LOperand* LChunkBuilder::FixedTemp(Register reg) {
775   LUnallocated* operand = ToUnallocated(reg);
776   ASSERT(operand->HasFixedPolicy());
777   return operand;
778 }
779
780
781 LOperand* LChunkBuilder::FixedTemp(XMMRegister reg) {
782   LUnallocated* operand = ToUnallocated(reg);
783   ASSERT(operand->HasFixedPolicy());
784   return operand;
785 }
786
787
788 LInstruction* LChunkBuilder::DoBlockEntry(HBlockEntry* instr) {
789   return new(zone()) LLabel(instr->block());
790 }
791
792
793 LInstruction* LChunkBuilder::DoSoftDeoptimize(HSoftDeoptimize* instr) {
794   return AssignEnvironment(new(zone()) LDeoptimize);
795 }
796
797
798 LInstruction* LChunkBuilder::DoDeoptimize(HDeoptimize* instr) {
799   return AssignEnvironment(new(zone()) LDeoptimize);
800 }
801
802
803 LInstruction* LChunkBuilder::DoShift(Token::Value op,
804                                      HBitwiseBinaryOperation* instr) {
805   if (instr->representation().IsTagged()) {
806     ASSERT(instr->left()->representation().IsTagged());
807     ASSERT(instr->right()->representation().IsTagged());
808
809     LOperand* left = UseFixed(instr->left(), rdx);
810     LOperand* right = UseFixed(instr->right(), rax);
811     LArithmeticT* result = new(zone()) LArithmeticT(op, left, right);
812     return MarkAsCall(DefineFixed(result, rax), instr);
813   }
814
815   ASSERT(instr->representation().IsInteger32());
816   ASSERT(instr->left()->representation().IsInteger32());
817   ASSERT(instr->right()->representation().IsInteger32());
818   LOperand* left = UseRegisterAtStart(instr->left());
819
820   HValue* right_value = instr->right();
821   LOperand* right = NULL;
822   int constant_value = 0;
823   if (right_value->IsConstant()) {
824     HConstant* constant = HConstant::cast(right_value);
825     right = chunk_->DefineConstantOperand(constant);
826     constant_value = constant->Integer32Value() & 0x1f;
827   } else {
828     right = UseFixed(right_value, rcx);
829   }
830
831   // Shift operations can only deoptimize if we do a logical shift by 0 and
832   // the result cannot be truncated to int32.
833   bool may_deopt = (op == Token::SHR && constant_value == 0);
834   bool does_deopt = false;
835   if (may_deopt) {
836     for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
837       if (!it.value()->CheckFlag(HValue::kTruncatingToInt32)) {
838         does_deopt = true;
839         break;
840       }
841     }
842   }
843
844   LInstruction* result =
845       DefineSameAsFirst(new(zone()) LShiftI(op, left, right, does_deopt));
846   return does_deopt ? AssignEnvironment(result) : result;
847 }
848
849
850 LInstruction* LChunkBuilder::DoArithmeticD(Token::Value op,
851                                            HArithmeticBinaryOperation* instr) {
852   ASSERT(instr->representation().IsDouble());
853   ASSERT(instr->left()->representation().IsDouble());
854   ASSERT(instr->right()->representation().IsDouble());
855   ASSERT(op != Token::MOD);
856   LOperand* left = UseRegisterAtStart(instr->left());
857   LOperand* right = UseRegisterAtStart(instr->right());
858   LArithmeticD* result = new(zone()) LArithmeticD(op, left, right);
859   return DefineSameAsFirst(result);
860 }
861
862
863 LInstruction* LChunkBuilder::DoArithmeticT(Token::Value op,
864                                            HArithmeticBinaryOperation* instr) {
865   ASSERT(op == Token::ADD ||
866          op == Token::DIV ||
867          op == Token::MOD ||
868          op == Token::MUL ||
869          op == Token::SUB);
870   HValue* left = instr->left();
871   HValue* right = instr->right();
872   ASSERT(left->representation().IsTagged());
873   ASSERT(right->representation().IsTagged());
874   LOperand* left_operand = UseFixed(left, rdx);
875   LOperand* right_operand = UseFixed(right, rax);
876   LArithmeticT* result =
877       new(zone()) LArithmeticT(op, left_operand, right_operand);
878   return MarkAsCall(DefineFixed(result, rax), instr);
879 }
880
881
882 void LChunkBuilder::DoBasicBlock(HBasicBlock* block, HBasicBlock* next_block) {
883   ASSERT(is_building());
884   current_block_ = block;
885   next_block_ = next_block;
886   if (block->IsStartBlock()) {
887     block->UpdateEnvironment(graph_->start_environment());
888     argument_count_ = 0;
889   } else if (block->predecessors()->length() == 1) {
890     // We have a single predecessor => copy environment and outgoing
891     // argument count from the predecessor.
892     ASSERT(block->phis()->length() == 0);
893     HBasicBlock* pred = block->predecessors()->at(0);
894     HEnvironment* last_environment = pred->last_environment();
895     ASSERT(last_environment != NULL);
896     // Only copy the environment, if it is later used again.
897     if (pred->end()->SecondSuccessor() == NULL) {
898       ASSERT(pred->end()->FirstSuccessor() == block);
899     } else {
900       if (pred->end()->FirstSuccessor()->block_id() > block->block_id() ||
901           pred->end()->SecondSuccessor()->block_id() > block->block_id()) {
902         last_environment = last_environment->Copy();
903       }
904     }
905     block->UpdateEnvironment(last_environment);
906     ASSERT(pred->argument_count() >= 0);
907     argument_count_ = pred->argument_count();
908   } else {
909     // We are at a state join => process phis.
910     HBasicBlock* pred = block->predecessors()->at(0);
911     // No need to copy the environment, it cannot be used later.
912     HEnvironment* last_environment = pred->last_environment();
913     for (int i = 0; i < block->phis()->length(); ++i) {
914       HPhi* phi = block->phis()->at(i);
915       last_environment->SetValueAt(phi->merged_index(), phi);
916     }
917     for (int i = 0; i < block->deleted_phis()->length(); ++i) {
918       last_environment->SetValueAt(block->deleted_phis()->at(i),
919                                    graph_->GetConstantUndefined());
920     }
921     block->UpdateEnvironment(last_environment);
922     // Pick up the outgoing argument count of one of the predecessors.
923     argument_count_ = pred->argument_count();
924   }
925   HInstruction* current = block->first();
926   int start = chunk_->instructions()->length();
927   while (current != NULL && !is_aborted()) {
928     // Code for constants in registers is generated lazily.
929     if (!current->EmitAtUses()) {
930       VisitInstruction(current);
931     }
932     current = current->next();
933   }
934   int end = chunk_->instructions()->length() - 1;
935   if (end >= start) {
936     block->set_first_instruction_index(start);
937     block->set_last_instruction_index(end);
938   }
939   block->set_argument_count(argument_count_);
940   next_block_ = NULL;
941   current_block_ = NULL;
942 }
943
944
945 void LChunkBuilder::VisitInstruction(HInstruction* current) {
946   HInstruction* old_current = current_instruction_;
947   current_instruction_ = current;
948   if (current->has_position()) position_ = current->position();
949   LInstruction* instr = current->CompileToLithium(this);
950
951   if (instr != NULL) {
952     if (FLAG_stress_pointer_maps && !instr->HasPointerMap()) {
953       instr = AssignPointerMap(instr);
954     }
955     if (FLAG_stress_environments && !instr->HasEnvironment()) {
956       instr = AssignEnvironment(instr);
957     }
958     instr->set_hydrogen_value(current);
959     chunk_->AddInstruction(instr, current_block_);
960   }
961   current_instruction_ = old_current;
962 }
963
964
965 LEnvironment* LChunkBuilder::CreateEnvironment(
966     HEnvironment* hydrogen_env,
967     int* argument_index_accumulator) {
968   if (hydrogen_env == NULL) return NULL;
969
970   LEnvironment* outer =
971       CreateEnvironment(hydrogen_env->outer(), argument_index_accumulator);
972   int ast_id = hydrogen_env->ast_id();
973   ASSERT(ast_id != AstNode::kNoNumber ||
974          hydrogen_env->frame_type() != JS_FUNCTION);
975   int value_count = hydrogen_env->length();
976   LEnvironment* result = new(zone()) LEnvironment(
977       hydrogen_env->closure(),
978       hydrogen_env->frame_type(),
979       ast_id,
980       hydrogen_env->parameter_count(),
981       argument_count_,
982       value_count,
983       outer);
984   int argument_index = *argument_index_accumulator;
985   for (int i = 0; i < value_count; ++i) {
986     if (hydrogen_env->is_special_index(i)) continue;
987
988     HValue* value = hydrogen_env->values()->at(i);
989     LOperand* op = NULL;
990     if (value->IsArgumentsObject()) {
991       op = NULL;
992     } else if (value->IsPushArgument()) {
993       op = new(zone()) LArgument(argument_index++);
994     } else {
995       op = UseAny(value);
996     }
997     result->AddValue(op, value->representation());
998   }
999
1000   if (hydrogen_env->frame_type() == JS_FUNCTION) {
1001     *argument_index_accumulator = argument_index;
1002   }
1003
1004   return result;
1005 }
1006
1007
1008 LInstruction* LChunkBuilder::DoGoto(HGoto* instr) {
1009   return new(zone()) LGoto(instr->FirstSuccessor()->block_id());
1010 }
1011
1012
1013 LInstruction* LChunkBuilder::DoBranch(HBranch* instr) {
1014   HValue* value = instr->value();
1015   if (value->EmitAtUses()) {
1016     ASSERT(value->IsConstant());
1017     ASSERT(!value->representation().IsDouble());
1018     HBasicBlock* successor = HConstant::cast(value)->ToBoolean()
1019         ? instr->FirstSuccessor()
1020         : instr->SecondSuccessor();
1021     return new(zone()) LGoto(successor->block_id());
1022   }
1023
1024   LBranch* result = new(zone()) LBranch(UseRegister(value));
1025   // Tagged values that are not known smis or booleans require a
1026   // deoptimization environment.
1027   Representation rep = value->representation();
1028   HType type = value->type();
1029   if (rep.IsTagged() && !type.IsSmi() && !type.IsBoolean()) {
1030     return AssignEnvironment(result);
1031   }
1032   return result;
1033 }
1034
1035
1036 LInstruction* LChunkBuilder::DoCompareMap(HCompareMap* instr) {
1037   ASSERT(instr->value()->representation().IsTagged());
1038   LOperand* value = UseRegisterAtStart(instr->value());
1039   return new(zone()) LCmpMapAndBranch(value);
1040 }
1041
1042
1043 LInstruction* LChunkBuilder::DoArgumentsLength(HArgumentsLength* length) {
1044   return DefineAsRegister(new(zone()) LArgumentsLength(Use(length->value())));
1045 }
1046
1047
1048 LInstruction* LChunkBuilder::DoArgumentsElements(HArgumentsElements* elems) {
1049   return DefineAsRegister(new(zone()) LArgumentsElements);
1050 }
1051
1052
1053 LInstruction* LChunkBuilder::DoInstanceOf(HInstanceOf* instr) {
1054   LOperand* left = UseFixed(instr->left(), rax);
1055   LOperand* right = UseFixed(instr->right(), rdx);
1056   LInstanceOf* result = new(zone()) LInstanceOf(left, right);
1057   return MarkAsCall(DefineFixed(result, rax), instr);
1058 }
1059
1060
1061 LInstruction* LChunkBuilder::DoInstanceOfKnownGlobal(
1062     HInstanceOfKnownGlobal* instr) {
1063   LInstanceOfKnownGlobal* result =
1064       new(zone()) LInstanceOfKnownGlobal(UseFixed(instr->left(), rax),
1065                                          FixedTemp(rdi));
1066   return MarkAsCall(DefineFixed(result, rax), instr);
1067 }
1068
1069
1070 LInstruction* LChunkBuilder::DoWrapReceiver(HWrapReceiver* instr) {
1071   LOperand* receiver = UseRegister(instr->receiver());
1072   LOperand* function = UseRegisterAtStart(instr->function());
1073   LWrapReceiver* result = new(zone()) LWrapReceiver(receiver, function);
1074   return AssignEnvironment(DefineSameAsFirst(result));
1075 }
1076
1077
1078 LInstruction* LChunkBuilder::DoApplyArguments(HApplyArguments* instr) {
1079   LOperand* function = UseFixed(instr->function(), rdi);
1080   LOperand* receiver = UseFixed(instr->receiver(), rax);
1081   LOperand* length = UseFixed(instr->length(), rbx);
1082   LOperand* elements = UseFixed(instr->elements(), rcx);
1083   LApplyArguments* result = new(zone()) LApplyArguments(function,
1084                                                 receiver,
1085                                                 length,
1086                                                 elements);
1087   return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
1088 }
1089
1090
1091 LInstruction* LChunkBuilder::DoPushArgument(HPushArgument* instr) {
1092   ++argument_count_;
1093   LOperand* argument = UseOrConstant(instr->argument());
1094   return new(zone()) LPushArgument(argument);
1095 }
1096
1097
1098 LInstruction* LChunkBuilder::DoThisFunction(HThisFunction* instr) {
1099   return instr->HasNoUses()
1100       ? NULL
1101       : DefineAsRegister(new(zone()) LThisFunction);
1102 }
1103
1104
1105 LInstruction* LChunkBuilder::DoContext(HContext* instr) {
1106   return instr->HasNoUses() ? NULL : DefineAsRegister(new(zone()) LContext);
1107 }
1108
1109
1110 LInstruction* LChunkBuilder::DoOuterContext(HOuterContext* instr) {
1111   LOperand* context = UseRegisterAtStart(instr->value());
1112   return DefineAsRegister(new(zone()) LOuterContext(context));
1113 }
1114
1115
1116 LInstruction* LChunkBuilder::DoDeclareGlobals(HDeclareGlobals* instr) {
1117   return MarkAsCall(new(zone()) LDeclareGlobals, instr);
1118 }
1119
1120
1121 LInstruction* LChunkBuilder::DoGlobalObject(HGlobalObject* instr) {
1122   return DefineAsRegister(new(zone()) LGlobalObject);
1123 }
1124
1125
1126 LInstruction* LChunkBuilder::DoGlobalReceiver(HGlobalReceiver* instr) {
1127   LOperand* global_object = UseRegisterAtStart(instr->value());
1128   return DefineAsRegister(new(zone()) LGlobalReceiver(global_object));
1129 }
1130
1131
1132 LInstruction* LChunkBuilder::DoCallConstantFunction(
1133     HCallConstantFunction* instr) {
1134   argument_count_ -= instr->argument_count();
1135   return MarkAsCall(DefineFixed(new(zone()) LCallConstantFunction, rax), instr);
1136 }
1137
1138
1139 LInstruction* LChunkBuilder::DoInvokeFunction(HInvokeFunction* instr) {
1140   LOperand* function = UseFixed(instr->function(), rdi);
1141   argument_count_ -= instr->argument_count();
1142   LInvokeFunction* result = new(zone()) LInvokeFunction(function);
1143   return MarkAsCall(DefineFixed(result, rax), instr, CANNOT_DEOPTIMIZE_EAGERLY);
1144 }
1145
1146
1147 LInstruction* LChunkBuilder::DoUnaryMathOperation(HUnaryMathOperation* instr) {
1148   BuiltinFunctionId op = instr->op();
1149   if (op == kMathLog || op == kMathSin || op == kMathCos || op == kMathTan) {
1150     LOperand* input = UseFixedDouble(instr->value(), xmm1);
1151     LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input);
1152     return MarkAsCall(DefineFixedDouble(result, xmm1), instr);
1153   } else {
1154     LOperand* input = UseRegisterAtStart(instr->value());
1155     LUnaryMathOperation* result = new(zone()) LUnaryMathOperation(input);
1156     switch (op) {
1157       case kMathAbs:
1158         return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1159       case kMathFloor:
1160         return AssignEnvironment(DefineAsRegister(result));
1161       case kMathRound:
1162         return AssignEnvironment(DefineAsRegister(result));
1163       case kMathSqrt:
1164         return DefineSameAsFirst(result);
1165       case kMathPowHalf:
1166         return DefineSameAsFirst(result);
1167       default:
1168         UNREACHABLE();
1169         return NULL;
1170     }
1171   }
1172 }
1173
1174
1175 LInstruction* LChunkBuilder::DoCallKeyed(HCallKeyed* instr) {
1176   ASSERT(instr->key()->representation().IsTagged());
1177   LOperand* key = UseFixed(instr->key(), rcx);
1178   argument_count_ -= instr->argument_count();
1179   LCallKeyed* result = new(zone()) LCallKeyed(key);
1180   return MarkAsCall(DefineFixed(result, rax), instr);
1181 }
1182
1183
1184 LInstruction* LChunkBuilder::DoCallNamed(HCallNamed* instr) {
1185   argument_count_ -= instr->argument_count();
1186   return MarkAsCall(DefineFixed(new(zone()) LCallNamed, rax), instr);
1187 }
1188
1189
1190 LInstruction* LChunkBuilder::DoCallGlobal(HCallGlobal* instr) {
1191   argument_count_ -= instr->argument_count();
1192   return MarkAsCall(DefineFixed(new(zone()) LCallGlobal, rax), instr);
1193 }
1194
1195
1196 LInstruction* LChunkBuilder::DoCallKnownGlobal(HCallKnownGlobal* instr) {
1197   argument_count_ -= instr->argument_count();
1198   return MarkAsCall(DefineFixed(new(zone()) LCallKnownGlobal, rax), instr);
1199 }
1200
1201
1202 LInstruction* LChunkBuilder::DoCallNew(HCallNew* instr) {
1203   LOperand* constructor = UseFixed(instr->constructor(), rdi);
1204   argument_count_ -= instr->argument_count();
1205   LCallNew* result = new(zone()) LCallNew(constructor);
1206   return MarkAsCall(DefineFixed(result, rax), instr);
1207 }
1208
1209
1210 LInstruction* LChunkBuilder::DoCallFunction(HCallFunction* instr) {
1211   LOperand* function = UseFixed(instr->function(), rdi);
1212   argument_count_ -= instr->argument_count();
1213   LCallFunction* result = new(zone()) LCallFunction(function);
1214   return MarkAsCall(DefineFixed(result, rax), instr);
1215 }
1216
1217
1218 LInstruction* LChunkBuilder::DoCallRuntime(HCallRuntime* instr) {
1219   argument_count_ -= instr->argument_count();
1220   return MarkAsCall(DefineFixed(new(zone()) LCallRuntime, rax), instr);
1221 }
1222
1223
1224 LInstruction* LChunkBuilder::DoShr(HShr* instr) {
1225   return DoShift(Token::SHR, instr);
1226 }
1227
1228
1229 LInstruction* LChunkBuilder::DoSar(HSar* instr) {
1230   return DoShift(Token::SAR, instr);
1231 }
1232
1233
1234 LInstruction* LChunkBuilder::DoShl(HShl* instr) {
1235   return DoShift(Token::SHL, instr);
1236 }
1237
1238
1239 LInstruction* LChunkBuilder::DoBitwise(HBitwise* instr) {
1240   if (instr->representation().IsInteger32()) {
1241     ASSERT(instr->left()->representation().IsInteger32());
1242     ASSERT(instr->right()->representation().IsInteger32());
1243
1244     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1245     LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1246     return DefineSameAsFirst(new(zone()) LBitI(left, right));
1247   } else {
1248     ASSERT(instr->representation().IsTagged());
1249     ASSERT(instr->left()->representation().IsTagged());
1250     ASSERT(instr->right()->representation().IsTagged());
1251
1252     LOperand* left = UseFixed(instr->left(), rdx);
1253     LOperand* right = UseFixed(instr->right(), rax);
1254     LArithmeticT* result = new(zone()) LArithmeticT(instr->op(), left, right);
1255     return MarkAsCall(DefineFixed(result, rax), instr);
1256   }
1257 }
1258
1259
1260 LInstruction* LChunkBuilder::DoBitNot(HBitNot* instr) {
1261   ASSERT(instr->value()->representation().IsInteger32());
1262   ASSERT(instr->representation().IsInteger32());
1263   if (instr->HasNoUses()) return NULL;
1264   LOperand* input = UseRegisterAtStart(instr->value());
1265   LBitNotI* result = new(zone()) LBitNotI(input);
1266   return DefineSameAsFirst(result);
1267 }
1268
1269
1270 LInstruction* LChunkBuilder::DoDiv(HDiv* instr) {
1271   if (instr->representation().IsDouble()) {
1272     return DoArithmeticD(Token::DIV, instr);
1273   } else if (instr->representation().IsInteger32()) {
1274     // The temporary operand is necessary to ensure that right is not allocated
1275     // into rdx.
1276     LOperand* temp = FixedTemp(rdx);
1277     LOperand* dividend = UseFixed(instr->left(), rax);
1278     LOperand* divisor = UseRegister(instr->right());
1279     LDivI* result = new(zone()) LDivI(dividend, divisor, temp);
1280     return AssignEnvironment(DefineFixed(result, rax));
1281   } else {
1282     ASSERT(instr->representation().IsTagged());
1283     return DoArithmeticT(Token::DIV, instr);
1284   }
1285 }
1286
1287
1288 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
1289   UNIMPLEMENTED();
1290   return NULL;
1291 }
1292
1293
1294 LInstruction* LChunkBuilder::DoMod(HMod* instr) {
1295   if (instr->representation().IsInteger32()) {
1296     ASSERT(instr->left()->representation().IsInteger32());
1297     ASSERT(instr->right()->representation().IsInteger32());
1298
1299     LInstruction* result;
1300     if (instr->HasPowerOf2Divisor()) {
1301       ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero));
1302       LOperand* value = UseRegisterAtStart(instr->left());
1303       LModI* mod =
1304           new(zone()) LModI(value, UseOrConstant(instr->right()), NULL);
1305       result = DefineSameAsFirst(mod);
1306     } else {
1307       // The temporary operand is necessary to ensure that right is not
1308       // allocated into edx.
1309       LOperand* temp = FixedTemp(rdx);
1310       LOperand* value = UseFixed(instr->left(), rax);
1311       LOperand* divisor = UseRegister(instr->right());
1312       LModI* mod = new(zone()) LModI(value, divisor, temp);
1313       result = DefineFixed(mod, rdx);
1314     }
1315
1316     return (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
1317             instr->CheckFlag(HValue::kCanBeDivByZero))
1318         ? AssignEnvironment(result)
1319         : result;
1320   } else if (instr->representation().IsTagged()) {
1321     return DoArithmeticT(Token::MOD, instr);
1322   } else {
1323     ASSERT(instr->representation().IsDouble());
1324     // We call a C function for double modulo. It can't trigger a GC.
1325     // We need to use fixed result register for the call.
1326     // TODO(fschneider): Allow any register as input registers.
1327     LOperand* left = UseFixedDouble(instr->left(), xmm2);
1328     LOperand* right = UseFixedDouble(instr->right(), xmm1);
1329     LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right);
1330     return MarkAsCall(DefineFixedDouble(result, xmm1), instr);
1331   }
1332 }
1333
1334
1335 LInstruction* LChunkBuilder::DoMul(HMul* instr) {
1336   if (instr->representation().IsInteger32()) {
1337     ASSERT(instr->left()->representation().IsInteger32());
1338     ASSERT(instr->right()->representation().IsInteger32());
1339     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1340     LOperand* right = UseOrConstant(instr->MostConstantOperand());
1341     LMulI* mul = new(zone()) LMulI(left, right);
1342     if (instr->CheckFlag(HValue::kCanOverflow) ||
1343         instr->CheckFlag(HValue::kBailoutOnMinusZero)) {
1344       AssignEnvironment(mul);
1345     }
1346     return DefineSameAsFirst(mul);
1347   } else if (instr->representation().IsDouble()) {
1348     return DoArithmeticD(Token::MUL, instr);
1349   } else {
1350     ASSERT(instr->representation().IsTagged());
1351     return DoArithmeticT(Token::MUL, instr);
1352   }
1353 }
1354
1355
1356 LInstruction* LChunkBuilder::DoSub(HSub* instr) {
1357   if (instr->representation().IsInteger32()) {
1358     ASSERT(instr->left()->representation().IsInteger32());
1359     ASSERT(instr->right()->representation().IsInteger32());
1360     LOperand* left = UseRegisterAtStart(instr->left());
1361     LOperand* right = UseOrConstantAtStart(instr->right());
1362     LSubI* sub = new(zone()) LSubI(left, right);
1363     LInstruction* result = DefineSameAsFirst(sub);
1364     if (instr->CheckFlag(HValue::kCanOverflow)) {
1365       result = AssignEnvironment(result);
1366     }
1367     return result;
1368   } else if (instr->representation().IsDouble()) {
1369     return DoArithmeticD(Token::SUB, instr);
1370   } else {
1371     ASSERT(instr->representation().IsTagged());
1372     return DoArithmeticT(Token::SUB, instr);
1373   }
1374 }
1375
1376
1377 LInstruction* LChunkBuilder::DoAdd(HAdd* instr) {
1378   if (instr->representation().IsInteger32()) {
1379     ASSERT(instr->left()->representation().IsInteger32());
1380     ASSERT(instr->right()->representation().IsInteger32());
1381     LOperand* left = UseRegisterAtStart(instr->LeastConstantOperand());
1382     LOperand* right = UseOrConstantAtStart(instr->MostConstantOperand());
1383     LAddI* add = new(zone()) LAddI(left, right);
1384     LInstruction* result = DefineSameAsFirst(add);
1385     if (instr->CheckFlag(HValue::kCanOverflow)) {
1386       result = AssignEnvironment(result);
1387     }
1388     return result;
1389   } else if (instr->representation().IsDouble()) {
1390     return DoArithmeticD(Token::ADD, instr);
1391   } else {
1392     ASSERT(instr->representation().IsTagged());
1393     return DoArithmeticT(Token::ADD, instr);
1394   }
1395   return NULL;
1396 }
1397
1398
1399 LInstruction* LChunkBuilder::DoPower(HPower* instr) {
1400   ASSERT(instr->representation().IsDouble());
1401   // We call a C function for double power. It can't trigger a GC.
1402   // We need to use fixed result register for the call.
1403   Representation exponent_type = instr->right()->representation();
1404   ASSERT(instr->left()->representation().IsDouble());
1405   LOperand* left = UseFixedDouble(instr->left(), xmm2);
1406   LOperand* right = exponent_type.IsDouble() ?
1407       UseFixedDouble(instr->right(), xmm1) :
1408 #ifdef _WIN64
1409       UseFixed(instr->right(), rdx);
1410 #else
1411       UseFixed(instr->right(), rdi);
1412 #endif
1413   LPower* result = new(zone()) LPower(left, right);
1414   return MarkAsCall(DefineFixedDouble(result, xmm3), instr,
1415                     CAN_DEOPTIMIZE_EAGERLY);
1416 }
1417
1418
1419 LInstruction* LChunkBuilder::DoRandom(HRandom* instr) {
1420   ASSERT(instr->representation().IsDouble());
1421   ASSERT(instr->global_object()->representation().IsTagged());
1422 #ifdef _WIN64
1423   LOperand* global_object = UseFixed(instr->global_object(), rcx);
1424 #else
1425   LOperand* global_object = UseFixed(instr->global_object(), rdi);
1426 #endif
1427   LRandom* result = new(zone()) LRandom(global_object);
1428   return MarkAsCall(DefineFixedDouble(result, xmm1), instr);
1429 }
1430
1431
1432 LInstruction* LChunkBuilder::DoCompareGeneric(HCompareGeneric* instr) {
1433   ASSERT(instr->left()->representation().IsTagged());
1434   ASSERT(instr->right()->representation().IsTagged());
1435   LOperand* left = UseFixed(instr->left(), rdx);
1436   LOperand* right = UseFixed(instr->right(), rax);
1437   LCmpT* result = new(zone()) LCmpT(left, right);
1438   return MarkAsCall(DefineFixed(result, rax), instr);
1439 }
1440
1441
1442 LInstruction* LChunkBuilder::DoCompareIDAndBranch(
1443     HCompareIDAndBranch* instr) {
1444   Representation r = instr->GetInputRepresentation();
1445   if (r.IsInteger32()) {
1446     ASSERT(instr->left()->representation().IsInteger32());
1447     ASSERT(instr->right()->representation().IsInteger32());
1448     LOperand* left = UseRegisterOrConstantAtStart(instr->left());
1449     LOperand* right = UseOrConstantAtStart(instr->right());
1450     return new(zone()) LCmpIDAndBranch(left, right);
1451   } else {
1452     ASSERT(r.IsDouble());
1453     ASSERT(instr->left()->representation().IsDouble());
1454     ASSERT(instr->right()->representation().IsDouble());
1455     LOperand* left;
1456     LOperand* right;
1457     if (instr->left()->IsConstant() && instr->right()->IsConstant()) {
1458       left = UseRegisterOrConstantAtStart(instr->left());
1459       right = UseRegisterOrConstantAtStart(instr->right());
1460     } else {
1461       left = UseRegisterAtStart(instr->left());
1462       right = UseRegisterAtStart(instr->right());
1463     }
1464     return new(zone()) LCmpIDAndBranch(left, right);
1465   }
1466 }
1467
1468
1469 LInstruction* LChunkBuilder::DoCompareObjectEqAndBranch(
1470     HCompareObjectEqAndBranch* instr) {
1471   LOperand* left = UseRegisterAtStart(instr->left());
1472   LOperand* right = UseRegisterAtStart(instr->right());
1473   return new(zone()) LCmpObjectEqAndBranch(left, right);
1474 }
1475
1476
1477 LInstruction* LChunkBuilder::DoCompareConstantEqAndBranch(
1478     HCompareConstantEqAndBranch* instr) {
1479   LOperand* value = UseRegisterAtStart(instr->value());
1480   return new(zone()) LCmpConstantEqAndBranch(value);
1481 }
1482
1483
1484 LInstruction* LChunkBuilder::DoIsNilAndBranch(HIsNilAndBranch* instr) {
1485   ASSERT(instr->value()->representation().IsTagged());
1486   LOperand* temp = instr->kind() == kStrictEquality ? NULL : TempRegister();
1487   return new(zone()) LIsNilAndBranch(UseRegisterAtStart(instr->value()), temp);
1488 }
1489
1490
1491 LInstruction* LChunkBuilder::DoIsObjectAndBranch(HIsObjectAndBranch* instr) {
1492   ASSERT(instr->value()->representation().IsTagged());
1493   return new(zone()) LIsObjectAndBranch(UseRegisterAtStart(instr->value()));
1494 }
1495
1496
1497 LInstruction* LChunkBuilder::DoIsStringAndBranch(HIsStringAndBranch* instr) {
1498   ASSERT(instr->value()->representation().IsTagged());
1499   LOperand* value = UseRegisterAtStart(instr->value());
1500   LOperand* temp = TempRegister();
1501   return new(zone()) LIsStringAndBranch(value, temp);
1502 }
1503
1504
1505 LInstruction* LChunkBuilder::DoIsSmiAndBranch(HIsSmiAndBranch* instr) {
1506   ASSERT(instr->value()->representation().IsTagged());
1507   return new(zone()) LIsSmiAndBranch(Use(instr->value()));
1508 }
1509
1510
1511 LInstruction* LChunkBuilder::DoIsUndetectableAndBranch(
1512     HIsUndetectableAndBranch* instr) {
1513   ASSERT(instr->value()->representation().IsTagged());
1514   LOperand* value = UseRegisterAtStart(instr->value());
1515   LOperand* temp = TempRegister();
1516   return new(zone()) LIsUndetectableAndBranch(value, temp);
1517 }
1518
1519
1520 LInstruction* LChunkBuilder::DoStringCompareAndBranch(
1521     HStringCompareAndBranch* instr) {
1522
1523   ASSERT(instr->left()->representation().IsTagged());
1524   ASSERT(instr->right()->representation().IsTagged());
1525   LOperand* left = UseFixed(instr->left(), rdx);
1526   LOperand* right = UseFixed(instr->right(), rax);
1527   LStringCompareAndBranch* result =
1528       new(zone()) LStringCompareAndBranch(left, right);
1529
1530   return MarkAsCall(result, instr);
1531 }
1532
1533
1534 LInstruction* LChunkBuilder::DoHasInstanceTypeAndBranch(
1535     HHasInstanceTypeAndBranch* instr) {
1536   ASSERT(instr->value()->representation().IsTagged());
1537   LOperand* value = UseRegisterAtStart(instr->value());
1538   return new(zone()) LHasInstanceTypeAndBranch(value);
1539 }
1540
1541
1542 LInstruction* LChunkBuilder::DoGetCachedArrayIndex(
1543     HGetCachedArrayIndex* instr)  {
1544   ASSERT(instr->value()->representation().IsTagged());
1545   LOperand* value = UseRegisterAtStart(instr->value());
1546
1547   return DefineAsRegister(new(zone()) LGetCachedArrayIndex(value));
1548 }
1549
1550
1551 LInstruction* LChunkBuilder::DoHasCachedArrayIndexAndBranch(
1552     HHasCachedArrayIndexAndBranch* instr) {
1553   ASSERT(instr->value()->representation().IsTagged());
1554   LOperand* value = UseRegisterAtStart(instr->value());
1555   return new(zone()) LHasCachedArrayIndexAndBranch(value);
1556 }
1557
1558
1559 LInstruction* LChunkBuilder::DoClassOfTestAndBranch(
1560     HClassOfTestAndBranch* instr) {
1561   LOperand* value = UseRegister(instr->value());
1562   return new(zone()) LClassOfTestAndBranch(value,
1563                                            TempRegister(),
1564                                            TempRegister());
1565 }
1566
1567
1568 LInstruction* LChunkBuilder::DoJSArrayLength(HJSArrayLength* instr) {
1569   LOperand* array = UseRegisterAtStart(instr->value());
1570   return DefineAsRegister(new(zone()) LJSArrayLength(array));
1571 }
1572
1573
1574 LInstruction* LChunkBuilder::DoFixedArrayBaseLength(
1575     HFixedArrayBaseLength* instr) {
1576   LOperand* array = UseRegisterAtStart(instr->value());
1577   return DefineAsRegister(new(zone()) LFixedArrayBaseLength(array));
1578 }
1579
1580
1581 LInstruction* LChunkBuilder::DoElementsKind(HElementsKind* instr) {
1582   LOperand* object = UseRegisterAtStart(instr->value());
1583   return DefineAsRegister(new(zone()) LElementsKind(object));
1584 }
1585
1586
1587 LInstruction* LChunkBuilder::DoValueOf(HValueOf* instr) {
1588   LOperand* object = UseRegister(instr->value());
1589   LValueOf* result = new(zone()) LValueOf(object);
1590   return DefineSameAsFirst(result);
1591 }
1592
1593
1594 LInstruction* LChunkBuilder::DoDateField(HDateField* instr) {
1595   LOperand* object = UseFixed(instr->value(), rax);
1596   LDateField* result = new LDateField(object, instr->index());
1597   return MarkAsCall(DefineFixed(result, rax), instr);
1598 }
1599
1600
1601 LInstruction* LChunkBuilder::DoBoundsCheck(HBoundsCheck* instr) {
1602   LOperand* value = UseRegisterOrConstantAtStart(instr->index());
1603   LOperand* length = Use(instr->length());
1604   return AssignEnvironment(new(zone()) LBoundsCheck(value, length));
1605 }
1606
1607
1608 LInstruction* LChunkBuilder::DoAbnormalExit(HAbnormalExit* instr) {
1609   // The control instruction marking the end of a block that completed
1610   // abruptly (e.g., threw an exception).  There is nothing specific to do.
1611   return NULL;
1612 }
1613
1614
1615 LInstruction* LChunkBuilder::DoThrow(HThrow* instr) {
1616   LOperand* value = UseFixed(instr->value(), rax);
1617   return MarkAsCall(new(zone()) LThrow(value), instr);
1618 }
1619
1620
1621 LInstruction* LChunkBuilder::DoUseConst(HUseConst* instr) {
1622   return NULL;
1623 }
1624
1625
1626 LInstruction* LChunkBuilder::DoForceRepresentation(HForceRepresentation* bad) {
1627   // All HForceRepresentation instructions should be eliminated in the
1628   // representation change phase of Hydrogen.
1629   UNREACHABLE();
1630   return NULL;
1631 }
1632
1633
1634 LInstruction* LChunkBuilder::DoChange(HChange* instr) {
1635   Representation from = instr->from();
1636   Representation to = instr->to();
1637   if (from.IsTagged()) {
1638     if (to.IsDouble()) {
1639       LOperand* value = UseRegister(instr->value());
1640       LNumberUntagD* res = new(zone()) LNumberUntagD(value);
1641       return AssignEnvironment(DefineAsRegister(res));
1642     } else {
1643       ASSERT(to.IsInteger32());
1644       LOperand* value = UseRegister(instr->value());
1645       bool needs_check = !instr->value()->type().IsSmi();
1646       if (needs_check) {
1647         bool truncating = instr->CanTruncateToInt32();
1648         LOperand* xmm_temp = truncating ? NULL : FixedTemp(xmm1);
1649         LTaggedToI* res = new(zone()) LTaggedToI(value, xmm_temp);
1650         return AssignEnvironment(DefineSameAsFirst(res));
1651       } else {
1652         return DefineSameAsFirst(new(zone()) LSmiUntag(value, needs_check));
1653       }
1654     }
1655   } else if (from.IsDouble()) {
1656     if (to.IsTagged()) {
1657       LOperand* value = UseRegister(instr->value());
1658       LOperand* temp = TempRegister();
1659
1660       // Make sure that temp and result_temp are different registers.
1661       LUnallocated* result_temp = TempRegister();
1662       LNumberTagD* result = new(zone()) LNumberTagD(value, temp);
1663       return AssignPointerMap(Define(result, result_temp));
1664     } else {
1665       ASSERT(to.IsInteger32());
1666       LOperand* value = UseRegister(instr->value());
1667       return AssignEnvironment(DefineAsRegister(new(zone()) LDoubleToI(value)));
1668     }
1669   } else if (from.IsInteger32()) {
1670     if (to.IsTagged()) {
1671       HValue* val = instr->value();
1672       LOperand* value = UseRegister(val);
1673       if (val->HasRange() && val->range()->IsInSmiRange()) {
1674         return DefineSameAsFirst(new(zone()) LSmiTag(value));
1675       } else {
1676         LNumberTagI* result = new(zone()) LNumberTagI(value);
1677         return AssignEnvironment(AssignPointerMap(DefineSameAsFirst(result)));
1678       }
1679     } else {
1680       ASSERT(to.IsDouble());
1681       LOperand* value = Use(instr->value());
1682       return DefineAsRegister(new(zone()) LInteger32ToDouble(value));
1683     }
1684   }
1685   UNREACHABLE();
1686   return NULL;
1687 }
1688
1689
1690 LInstruction* LChunkBuilder::DoCheckNonSmi(HCheckNonSmi* instr) {
1691   LOperand* value = UseRegisterAtStart(instr->value());
1692   return AssignEnvironment(new(zone()) LCheckNonSmi(value));
1693 }
1694
1695
1696 LInstruction* LChunkBuilder::DoCheckInstanceType(HCheckInstanceType* instr) {
1697   LOperand* value = UseRegisterAtStart(instr->value());
1698   LCheckInstanceType* result = new(zone()) LCheckInstanceType(value);
1699   return AssignEnvironment(result);
1700 }
1701
1702
1703 LInstruction* LChunkBuilder::DoCheckPrototypeMaps(HCheckPrototypeMaps* instr) {
1704   LOperand* temp = TempRegister();
1705   LCheckPrototypeMaps* result = new(zone()) LCheckPrototypeMaps(temp);
1706   return AssignEnvironment(result);
1707 }
1708
1709
1710 LInstruction* LChunkBuilder::DoCheckSmi(HCheckSmi* instr) {
1711   LOperand* value = UseRegisterAtStart(instr->value());
1712   return AssignEnvironment(new(zone()) LCheckSmi(value));
1713 }
1714
1715
1716 LInstruction* LChunkBuilder::DoCheckFunction(HCheckFunction* instr) {
1717   LOperand* value = UseRegisterAtStart(instr->value());
1718   return AssignEnvironment(new(zone()) LCheckFunction(value));
1719 }
1720
1721
1722 LInstruction* LChunkBuilder::DoCheckMaps(HCheckMaps* instr) {
1723   LOperand* value = UseRegisterAtStart(instr->value());
1724   LCheckMaps* result = new(zone()) LCheckMaps(value);
1725   return AssignEnvironment(result);
1726 }
1727
1728
1729 LInstruction* LChunkBuilder::DoClampToUint8(HClampToUint8* instr) {
1730   HValue* value = instr->value();
1731   Representation input_rep = value->representation();
1732   LOperand* reg = UseRegister(value);
1733   if (input_rep.IsDouble()) {
1734     return DefineAsRegister(new(zone()) LClampDToUint8(reg,
1735                                                TempRegister()));
1736   } else if (input_rep.IsInteger32()) {
1737     return DefineSameAsFirst(new(zone()) LClampIToUint8(reg));
1738   } else {
1739     ASSERT(input_rep.IsTagged());
1740     // Register allocator doesn't (yet) support allocation of double
1741     // temps. Reserve xmm1 explicitly.
1742     LClampTToUint8* result = new(zone()) LClampTToUint8(reg,
1743                                                         TempRegister(),
1744                                                         FixedTemp(xmm1));
1745     return AssignEnvironment(DefineSameAsFirst(result));
1746   }
1747 }
1748
1749
1750 LInstruction* LChunkBuilder::DoReturn(HReturn* instr) {
1751   return new(zone()) LReturn(UseFixed(instr->value(), rax));
1752 }
1753
1754
1755 LInstruction* LChunkBuilder::DoConstant(HConstant* instr) {
1756   Representation r = instr->representation();
1757   if (r.IsInteger32()) {
1758     return DefineAsRegister(new(zone()) LConstantI);
1759   } else if (r.IsDouble()) {
1760     LOperand* temp = TempRegister();
1761     return DefineAsRegister(new(zone()) LConstantD(temp));
1762   } else if (r.IsTagged()) {
1763     return DefineAsRegister(new(zone()) LConstantT);
1764   } else {
1765     UNREACHABLE();
1766     return NULL;
1767   }
1768 }
1769
1770
1771 LInstruction* LChunkBuilder::DoLoadGlobalCell(HLoadGlobalCell* instr) {
1772   LLoadGlobalCell* result = new(zone()) LLoadGlobalCell;
1773   return instr->RequiresHoleCheck()
1774       ? AssignEnvironment(DefineAsRegister(result))
1775       : DefineAsRegister(result);
1776 }
1777
1778
1779 LInstruction* LChunkBuilder::DoLoadGlobalGeneric(HLoadGlobalGeneric* instr) {
1780   LOperand* global_object = UseFixed(instr->global_object(), rax);
1781   LLoadGlobalGeneric* result = new(zone()) LLoadGlobalGeneric(global_object);
1782   return MarkAsCall(DefineFixed(result, rax), instr);
1783 }
1784
1785
1786 LInstruction* LChunkBuilder::DoStoreGlobalCell(HStoreGlobalCell* instr) {
1787   LOperand* value = UseRegister(instr->value());
1788   // Use a temp to avoid reloading the cell value address in the case where
1789   // we perform a hole check.
1790   return instr->RequiresHoleCheck()
1791       ? AssignEnvironment(new(zone()) LStoreGlobalCell(value, TempRegister()))
1792       : new(zone()) LStoreGlobalCell(value, NULL);
1793 }
1794
1795
1796 LInstruction* LChunkBuilder::DoStoreGlobalGeneric(HStoreGlobalGeneric* instr) {
1797   LOperand* global_object = UseFixed(instr->global_object(), rdx);
1798   LOperand* value = UseFixed(instr->value(), rax);
1799   LStoreGlobalGeneric* result =  new(zone()) LStoreGlobalGeneric(global_object,
1800                                                                  value);
1801   return MarkAsCall(result, instr);
1802 }
1803
1804
1805 LInstruction* LChunkBuilder::DoLoadContextSlot(HLoadContextSlot* instr) {
1806   LOperand* context = UseRegisterAtStart(instr->value());
1807   LInstruction* result =
1808       DefineAsRegister(new(zone()) LLoadContextSlot(context));
1809   return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1810 }
1811
1812
1813 LInstruction* LChunkBuilder::DoStoreContextSlot(HStoreContextSlot* instr) {
1814   LOperand* context;
1815   LOperand* value;
1816   LOperand* temp;
1817   if (instr->NeedsWriteBarrier()) {
1818     context = UseTempRegister(instr->context());
1819     value = UseTempRegister(instr->value());
1820     temp = TempRegister();
1821   } else {
1822     context = UseRegister(instr->context());
1823     value = UseRegister(instr->value());
1824     temp = NULL;
1825   }
1826   LInstruction* result = new(zone()) LStoreContextSlot(context, value, temp);
1827   return instr->RequiresHoleCheck() ? AssignEnvironment(result) : result;
1828 }
1829
1830
1831 LInstruction* LChunkBuilder::DoLoadNamedField(HLoadNamedField* instr) {
1832   ASSERT(instr->representation().IsTagged());
1833   LOperand* obj = UseRegisterAtStart(instr->object());
1834   return DefineAsRegister(new(zone()) LLoadNamedField(obj));
1835 }
1836
1837
1838 LInstruction* LChunkBuilder::DoLoadNamedFieldPolymorphic(
1839     HLoadNamedFieldPolymorphic* instr) {
1840   ASSERT(instr->representation().IsTagged());
1841   if (instr->need_generic()) {
1842     LOperand* obj = UseFixed(instr->object(), rax);
1843     LLoadNamedFieldPolymorphic* result =
1844         new(zone()) LLoadNamedFieldPolymorphic(obj);
1845     return MarkAsCall(DefineFixed(result, rax), instr);
1846   } else {
1847     LOperand* obj = UseRegisterAtStart(instr->object());
1848     LLoadNamedFieldPolymorphic* result =
1849         new(zone()) LLoadNamedFieldPolymorphic(obj);
1850     return AssignEnvironment(DefineAsRegister(result));
1851   }
1852 }
1853
1854
1855 LInstruction* LChunkBuilder::DoLoadNamedGeneric(HLoadNamedGeneric* instr) {
1856   LOperand* object = UseFixed(instr->object(), rax);
1857   LLoadNamedGeneric* result = new(zone()) LLoadNamedGeneric(object);
1858   return MarkAsCall(DefineFixed(result, rax), instr);
1859 }
1860
1861
1862 LInstruction* LChunkBuilder::DoLoadFunctionPrototype(
1863     HLoadFunctionPrototype* instr) {
1864   return AssignEnvironment(DefineAsRegister(
1865       new(zone()) LLoadFunctionPrototype(UseRegister(instr->function()))));
1866 }
1867
1868
1869 LInstruction* LChunkBuilder::DoLoadElements(HLoadElements* instr) {
1870   LOperand* input = UseRegisterAtStart(instr->value());
1871   return DefineAsRegister(new(zone()) LLoadElements(input));
1872 }
1873
1874
1875 LInstruction* LChunkBuilder::DoLoadExternalArrayPointer(
1876     HLoadExternalArrayPointer* instr) {
1877   LOperand* input = UseRegisterAtStart(instr->value());
1878   return DefineAsRegister(new(zone()) LLoadExternalArrayPointer(input));
1879 }
1880
1881
1882 LInstruction* LChunkBuilder::DoLoadKeyedFastElement(
1883     HLoadKeyedFastElement* instr) {
1884   ASSERT(instr->representation().IsTagged());
1885   ASSERT(instr->key()->representation().IsInteger32());
1886   LOperand* obj = UseRegisterAtStart(instr->object());
1887   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1888   LLoadKeyedFastElement* result = new(zone()) LLoadKeyedFastElement(obj, key);
1889   if (instr->RequiresHoleCheck()) AssignEnvironment(result);
1890   return DefineAsRegister(result);
1891 }
1892
1893
1894 LInstruction* LChunkBuilder::DoLoadKeyedFastDoubleElement(
1895     HLoadKeyedFastDoubleElement* instr) {
1896   ASSERT(instr->representation().IsDouble());
1897   ASSERT(instr->key()->representation().IsInteger32());
1898   LOperand* elements = UseRegisterAtStart(instr->elements());
1899   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1900   LLoadKeyedFastDoubleElement* result =
1901       new(zone()) LLoadKeyedFastDoubleElement(elements, key);
1902   return AssignEnvironment(DefineAsRegister(result));
1903 }
1904
1905
1906 LInstruction* LChunkBuilder::DoLoadKeyedSpecializedArrayElement(
1907     HLoadKeyedSpecializedArrayElement* instr) {
1908   ElementsKind elements_kind = instr->elements_kind();
1909   ASSERT(
1910       (instr->representation().IsInteger32() &&
1911        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1912        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1913       (instr->representation().IsDouble() &&
1914        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1915        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1916   ASSERT(instr->key()->representation().IsInteger32());
1917   LOperand* external_pointer = UseRegister(instr->external_pointer());
1918   LOperand* key = UseRegisterOrConstant(instr->key());
1919   LLoadKeyedSpecializedArrayElement* result =
1920       new(zone()) LLoadKeyedSpecializedArrayElement(external_pointer, key);
1921   LInstruction* load_instr = DefineAsRegister(result);
1922   // An unsigned int array load might overflow and cause a deopt, make sure it
1923   // has an environment.
1924   return (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) ?
1925       AssignEnvironment(load_instr) : load_instr;
1926 }
1927
1928
1929 LInstruction* LChunkBuilder::DoLoadKeyedGeneric(HLoadKeyedGeneric* instr) {
1930   LOperand* object = UseFixed(instr->object(), rdx);
1931   LOperand* key = UseFixed(instr->key(), rax);
1932
1933   LLoadKeyedGeneric* result = new(zone()) LLoadKeyedGeneric(object, key);
1934   return MarkAsCall(DefineFixed(result, rax), instr);
1935 }
1936
1937
1938 LInstruction* LChunkBuilder::DoStoreKeyedFastElement(
1939     HStoreKeyedFastElement* instr) {
1940   bool needs_write_barrier = instr->NeedsWriteBarrier();
1941   ASSERT(instr->value()->representation().IsTagged());
1942   ASSERT(instr->object()->representation().IsTagged());
1943   ASSERT(instr->key()->representation().IsInteger32());
1944
1945   LOperand* obj = UseTempRegister(instr->object());
1946   LOperand* val = needs_write_barrier
1947       ? UseTempRegister(instr->value())
1948       : UseRegisterAtStart(instr->value());
1949   LOperand* key = needs_write_barrier
1950       ? UseTempRegister(instr->key())
1951       : UseRegisterOrConstantAtStart(instr->key());
1952   return new(zone()) LStoreKeyedFastElement(obj, key, val);
1953 }
1954
1955
1956 LInstruction* LChunkBuilder::DoStoreKeyedFastDoubleElement(
1957     HStoreKeyedFastDoubleElement* instr) {
1958   ASSERT(instr->value()->representation().IsDouble());
1959   ASSERT(instr->elements()->representation().IsTagged());
1960   ASSERT(instr->key()->representation().IsInteger32());
1961
1962   LOperand* elements = UseRegisterAtStart(instr->elements());
1963   LOperand* val = UseTempRegister(instr->value());
1964   LOperand* key = UseRegisterOrConstantAtStart(instr->key());
1965
1966   return new(zone()) LStoreKeyedFastDoubleElement(elements, key, val);
1967 }
1968
1969
1970 LInstruction* LChunkBuilder::DoStoreKeyedSpecializedArrayElement(
1971     HStoreKeyedSpecializedArrayElement* instr) {
1972   ElementsKind elements_kind = instr->elements_kind();
1973   ASSERT(
1974       (instr->value()->representation().IsInteger32() &&
1975        (elements_kind != EXTERNAL_FLOAT_ELEMENTS) &&
1976        (elements_kind != EXTERNAL_DOUBLE_ELEMENTS)) ||
1977       (instr->value()->representation().IsDouble() &&
1978        ((elements_kind == EXTERNAL_FLOAT_ELEMENTS) ||
1979        (elements_kind == EXTERNAL_DOUBLE_ELEMENTS))));
1980   ASSERT(instr->external_pointer()->representation().IsExternal());
1981   ASSERT(instr->key()->representation().IsInteger32());
1982
1983   LOperand* external_pointer = UseRegister(instr->external_pointer());
1984   bool val_is_temp_register =
1985       elements_kind == EXTERNAL_PIXEL_ELEMENTS ||
1986       elements_kind == EXTERNAL_FLOAT_ELEMENTS;
1987   LOperand* val = val_is_temp_register
1988       ? UseTempRegister(instr->value())
1989       : UseRegister(instr->value());
1990   LOperand* key = UseRegisterOrConstant(instr->key());
1991
1992   return new(zone()) LStoreKeyedSpecializedArrayElement(external_pointer,
1993                                                         key,
1994                                                         val);
1995 }
1996
1997
1998 LInstruction* LChunkBuilder::DoStoreKeyedGeneric(HStoreKeyedGeneric* instr) {
1999   LOperand* object = UseFixed(instr->object(), rdx);
2000   LOperand* key = UseFixed(instr->key(), rcx);
2001   LOperand* value = UseFixed(instr->value(), rax);
2002
2003   ASSERT(instr->object()->representation().IsTagged());
2004   ASSERT(instr->key()->representation().IsTagged());
2005   ASSERT(instr->value()->representation().IsTagged());
2006
2007   LStoreKeyedGeneric* result =
2008       new(zone()) LStoreKeyedGeneric(object, key, value);
2009   return MarkAsCall(result, instr);
2010 }
2011
2012
2013 LInstruction* LChunkBuilder::DoTransitionElementsKind(
2014     HTransitionElementsKind* instr) {
2015   if (instr->original_map()->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
2016       instr->transitioned_map()->elements_kind() == FAST_ELEMENTS) {
2017     LOperand* object = UseRegister(instr->object());
2018     LOperand* new_map_reg = TempRegister();
2019     LOperand* temp_reg = TempRegister();
2020     LTransitionElementsKind* result =
2021         new(zone()) LTransitionElementsKind(object, new_map_reg, temp_reg);
2022     return DefineSameAsFirst(result);
2023   } else {
2024     LOperand* object = UseFixed(instr->object(), rax);
2025     LOperand* fixed_object_reg = FixedTemp(rdx);
2026     LOperand* new_map_reg = FixedTemp(rbx);
2027     LTransitionElementsKind* result =
2028         new(zone()) LTransitionElementsKind(object,
2029                                             new_map_reg,
2030                                             fixed_object_reg);
2031     return MarkAsCall(DefineFixed(result, rax), instr);
2032   }
2033 }
2034
2035
2036 LInstruction* LChunkBuilder::DoStoreNamedField(HStoreNamedField* instr) {
2037   bool needs_write_barrier = instr->NeedsWriteBarrier();
2038
2039   LOperand* obj = needs_write_barrier
2040       ? UseTempRegister(instr->object())
2041       : UseRegisterAtStart(instr->object());
2042
2043   LOperand* val = needs_write_barrier
2044       ? UseTempRegister(instr->value())
2045       : UseRegister(instr->value());
2046
2047   // We only need a scratch register if we have a write barrier or we
2048   // have a store into the properties array (not in-object-property).
2049   LOperand* temp = (!instr->is_in_object() || needs_write_barrier)
2050       ? TempRegister() : NULL;
2051
2052   return new(zone()) LStoreNamedField(obj, val, temp);
2053 }
2054
2055
2056 LInstruction* LChunkBuilder::DoStoreNamedGeneric(HStoreNamedGeneric* instr) {
2057   LOperand* object = UseFixed(instr->object(), rdx);
2058   LOperand* value = UseFixed(instr->value(), rax);
2059
2060   LStoreNamedGeneric* result = new(zone()) LStoreNamedGeneric(object, value);
2061   return MarkAsCall(result, instr);
2062 }
2063
2064
2065 LInstruction* LChunkBuilder::DoStringAdd(HStringAdd* instr) {
2066   LOperand* left = UseOrConstantAtStart(instr->left());
2067   LOperand* right = UseOrConstantAtStart(instr->right());
2068   return MarkAsCall(DefineFixed(new(zone()) LStringAdd(left, right), rax),
2069                     instr);
2070 }
2071
2072
2073 LInstruction* LChunkBuilder::DoStringCharCodeAt(HStringCharCodeAt* instr) {
2074   LOperand* string = UseTempRegister(instr->string());
2075   LOperand* index = UseTempRegister(instr->index());
2076   LStringCharCodeAt* result = new(zone()) LStringCharCodeAt(string, index);
2077   return AssignEnvironment(AssignPointerMap(DefineAsRegister(result)));
2078 }
2079
2080
2081 LInstruction* LChunkBuilder::DoStringCharFromCode(HStringCharFromCode* instr) {
2082   LOperand* char_code = UseRegister(instr->value());
2083   LStringCharFromCode* result = new(zone()) LStringCharFromCode(char_code);
2084   return AssignPointerMap(DefineAsRegister(result));
2085 }
2086
2087
2088 LInstruction* LChunkBuilder::DoStringLength(HStringLength* instr) {
2089   LOperand* string = UseRegisterAtStart(instr->value());
2090   return DefineAsRegister(new(zone()) LStringLength(string));
2091 }
2092
2093
2094 LInstruction* LChunkBuilder::DoAllocateObject(HAllocateObject* instr) {
2095   LAllocateObject* result = new LAllocateObject(TempRegister());
2096   return AssignPointerMap(DefineAsRegister(result));
2097 }
2098
2099
2100 LInstruction* LChunkBuilder::DoFastLiteral(HFastLiteral* instr) {
2101   return MarkAsCall(DefineFixed(new(zone()) LFastLiteral, rax), instr);
2102 }
2103
2104
2105 LInstruction* LChunkBuilder::DoArrayLiteral(HArrayLiteral* instr) {
2106   return MarkAsCall(DefineFixed(new(zone()) LArrayLiteral, rax), instr);
2107 }
2108
2109
2110 LInstruction* LChunkBuilder::DoObjectLiteral(HObjectLiteral* instr) {
2111   return MarkAsCall(DefineFixed(new(zone()) LObjectLiteral, rax), instr);
2112 }
2113
2114
2115 LInstruction* LChunkBuilder::DoRegExpLiteral(HRegExpLiteral* instr) {
2116   return MarkAsCall(DefineFixed(new(zone()) LRegExpLiteral, rax), instr);
2117 }
2118
2119
2120 LInstruction* LChunkBuilder::DoFunctionLiteral(HFunctionLiteral* instr) {
2121   return MarkAsCall(DefineFixed(new(zone()) LFunctionLiteral, rax), instr);
2122 }
2123
2124
2125 LInstruction* LChunkBuilder::DoDeleteProperty(HDeleteProperty* instr) {
2126   LOperand* object = UseAtStart(instr->object());
2127   LOperand* key = UseOrConstantAtStart(instr->key());
2128   LDeleteProperty* result = new(zone()) LDeleteProperty(object, key);
2129   return MarkAsCall(DefineFixed(result, rax), instr);
2130 }
2131
2132
2133 LInstruction* LChunkBuilder::DoOsrEntry(HOsrEntry* instr) {
2134   allocator_->MarkAsOsrEntry();
2135   current_block_->last_environment()->set_ast_id(instr->ast_id());
2136   return AssignEnvironment(new(zone()) LOsrEntry);
2137 }
2138
2139
2140 LInstruction* LChunkBuilder::DoParameter(HParameter* instr) {
2141   int spill_index = chunk()->GetParameterStackSlot(instr->index());
2142   return DefineAsSpilled(new(zone()) LParameter, spill_index);
2143 }
2144
2145
2146 LInstruction* LChunkBuilder::DoUnknownOSRValue(HUnknownOSRValue* instr) {
2147   int spill_index = chunk()->GetNextSpillIndex(false);  // Not double-width.
2148   if (spill_index > LUnallocated::kMaxFixedIndex) {
2149     Abort("Too many spill slots needed for OSR");
2150     spill_index = 0;
2151   }
2152   return DefineAsSpilled(new(zone()) LUnknownOSRValue, spill_index);
2153 }
2154
2155
2156 LInstruction* LChunkBuilder::DoCallStub(HCallStub* instr) {
2157   argument_count_ -= instr->argument_count();
2158   return MarkAsCall(DefineFixed(new(zone()) LCallStub, rax), instr);
2159 }
2160
2161
2162 LInstruction* LChunkBuilder::DoArgumentsObject(HArgumentsObject* instr) {
2163   // There are no real uses of the arguments object.
2164   // arguments.length and element access are supported directly on
2165   // stack arguments, and any real arguments object use causes a bailout.
2166   // So this value is never used.
2167   return NULL;
2168 }
2169
2170
2171 LInstruction* LChunkBuilder::DoAccessArgumentsAt(HAccessArgumentsAt* instr) {
2172   LOperand* arguments = UseRegister(instr->arguments());
2173   LOperand* length = UseTempRegister(instr->length());
2174   LOperand* index = Use(instr->index());
2175   LAccessArgumentsAt* result =
2176       new(zone()) LAccessArgumentsAt(arguments, length, index);
2177   return AssignEnvironment(DefineAsRegister(result));
2178 }
2179
2180
2181 LInstruction* LChunkBuilder::DoToFastProperties(HToFastProperties* instr) {
2182   LOperand* object = UseFixed(instr->value(), rax);
2183   LToFastProperties* result = new(zone()) LToFastProperties(object);
2184   return MarkAsCall(DefineFixed(result, rax), instr);
2185 }
2186
2187
2188 LInstruction* LChunkBuilder::DoTypeof(HTypeof* instr) {
2189   LTypeof* result = new(zone()) LTypeof(UseAtStart(instr->value()));
2190   return MarkAsCall(DefineFixed(result, rax), instr);
2191 }
2192
2193
2194 LInstruction* LChunkBuilder::DoTypeofIsAndBranch(HTypeofIsAndBranch* instr) {
2195   return new(zone()) LTypeofIsAndBranch(UseTempRegister(instr->value()));
2196 }
2197
2198
2199 LInstruction* LChunkBuilder::DoIsConstructCallAndBranch(
2200     HIsConstructCallAndBranch* instr) {
2201   return new(zone()) LIsConstructCallAndBranch(TempRegister());
2202 }
2203
2204
2205 LInstruction* LChunkBuilder::DoSimulate(HSimulate* instr) {
2206   HEnvironment* env = current_block_->last_environment();
2207   ASSERT(env != NULL);
2208
2209   env->set_ast_id(instr->ast_id());
2210
2211   env->Drop(instr->pop_count());
2212   for (int i = 0; i < instr->values()->length(); ++i) {
2213     HValue* value = instr->values()->at(i);
2214     if (instr->HasAssignedIndexAt(i)) {
2215       env->Bind(instr->GetAssignedIndexAt(i), value);
2216     } else {
2217       env->Push(value);
2218     }
2219   }
2220
2221   // If there is an instruction pending deoptimization environment create a
2222   // lazy bailout instruction to capture the environment.
2223   if (pending_deoptimization_ast_id_ == instr->ast_id()) {
2224     LLazyBailout* lazy_bailout = new(zone()) LLazyBailout;
2225     LInstruction* result = AssignEnvironment(lazy_bailout);
2226     // Store the lazy deopt environment with the instruction if needed. Right
2227     // now it is only used for LInstanceOfKnownGlobal.
2228     instruction_pending_deoptimization_environment_->
2229         SetDeferredLazyDeoptimizationEnvironment(result->environment());
2230     instruction_pending_deoptimization_environment_ = NULL;
2231     pending_deoptimization_ast_id_ = AstNode::kNoNumber;
2232     return result;
2233   }
2234
2235   return NULL;
2236 }
2237
2238
2239 LInstruction* LChunkBuilder::DoStackCheck(HStackCheck* instr) {
2240   if (instr->is_function_entry()) {
2241     return MarkAsCall(new(zone()) LStackCheck, instr);
2242   } else {
2243     ASSERT(instr->is_backwards_branch());
2244     return AssignEnvironment(AssignPointerMap(new(zone()) LStackCheck));
2245   }
2246 }
2247
2248
2249 LInstruction* LChunkBuilder::DoEnterInlined(HEnterInlined* instr) {
2250   HEnvironment* outer = current_block_->last_environment();
2251   HConstant* undefined = graph()->GetConstantUndefined();
2252   HEnvironment* inner = outer->CopyForInlining(instr->closure(),
2253                                                instr->arguments_count(),
2254                                                instr->function(),
2255                                                undefined,
2256                                                instr->call_kind(),
2257                                                instr->is_construct());
2258   if (instr->arguments_var() != NULL) {
2259     inner->Bind(instr->arguments_var(), graph()->GetArgumentsObject());
2260   }
2261   current_block_->UpdateEnvironment(inner);
2262   chunk_->AddInlinedClosure(instr->closure());
2263   return NULL;
2264 }
2265
2266
2267 LInstruction* LChunkBuilder::DoLeaveInlined(HLeaveInlined* instr) {
2268   LInstruction* pop = NULL;
2269
2270   HEnvironment* env = current_block_->last_environment();
2271
2272   if (instr->arguments_pushed()) {
2273     int argument_count = env->arguments_environment()->parameter_count();
2274     pop = new(zone()) LDrop(argument_count);
2275     argument_count_ -= argument_count;
2276   }
2277
2278   HEnvironment* outer = current_block_->last_environment()->
2279       DiscardInlined(false);
2280   current_block_->UpdateEnvironment(outer);
2281
2282   return pop;
2283 }
2284
2285
2286 LInstruction* LChunkBuilder::DoIn(HIn* instr) {
2287   LOperand* key = UseOrConstantAtStart(instr->key());
2288   LOperand* object = UseOrConstantAtStart(instr->object());
2289   LIn* result = new(zone()) LIn(key, object);
2290   return MarkAsCall(DefineFixed(result, rax), instr);
2291 }
2292
2293
2294 LInstruction* LChunkBuilder::DoForInPrepareMap(HForInPrepareMap* instr) {
2295   LOperand* object = UseFixed(instr->enumerable(), rax);
2296   LForInPrepareMap* result = new(zone()) LForInPrepareMap(object);
2297   return MarkAsCall(DefineFixed(result, rax), instr, CAN_DEOPTIMIZE_EAGERLY);
2298 }
2299
2300
2301 LInstruction* LChunkBuilder::DoForInCacheArray(HForInCacheArray* instr) {
2302   LOperand* map = UseRegister(instr->map());
2303   return AssignEnvironment(DefineAsRegister(
2304       new(zone()) LForInCacheArray(map)));
2305 }
2306
2307
2308 LInstruction* LChunkBuilder::DoCheckMapValue(HCheckMapValue* instr) {
2309   LOperand* value = UseRegisterAtStart(instr->value());
2310   LOperand* map = UseRegisterAtStart(instr->map());
2311   return AssignEnvironment(new(zone()) LCheckMapValue(value, map));
2312 }
2313
2314
2315 LInstruction* LChunkBuilder::DoLoadFieldByIndex(HLoadFieldByIndex* instr) {
2316   LOperand* object = UseRegister(instr->object());
2317   LOperand* index = UseTempRegister(instr->index());
2318   return DefineSameAsFirst(new(zone()) LLoadFieldByIndex(object, index));
2319 }
2320
2321
2322 } }  // namespace v8::internal
2323
2324 #endif  // V8_TARGET_ARCH_X64