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