1 // Copyright 2013 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.
5 #include "src/hydrogen.h"
11 #include "src/allocation-site-scopes.h"
12 #include "src/ast-numbering.h"
13 #include "src/full-codegen/full-codegen.h"
14 #include "src/hydrogen-bce.h"
15 #include "src/hydrogen-bch.h"
16 #include "src/hydrogen-canonicalize.h"
17 #include "src/hydrogen-check-elimination.h"
18 #include "src/hydrogen-dce.h"
19 #include "src/hydrogen-dehoist.h"
20 #include "src/hydrogen-environment-liveness.h"
21 #include "src/hydrogen-escape-analysis.h"
22 #include "src/hydrogen-gvn.h"
23 #include "src/hydrogen-infer-representation.h"
24 #include "src/hydrogen-infer-types.h"
25 #include "src/hydrogen-load-elimination.h"
26 #include "src/hydrogen-mark-deoptimize.h"
27 #include "src/hydrogen-mark-unreachable.h"
28 #include "src/hydrogen-osr.h"
29 #include "src/hydrogen-range-analysis.h"
30 #include "src/hydrogen-redundant-phi.h"
31 #include "src/hydrogen-removable-simulates.h"
32 #include "src/hydrogen-representation-changes.h"
33 #include "src/hydrogen-sce.h"
34 #include "src/hydrogen-store-elimination.h"
35 #include "src/hydrogen-uint32-analysis.h"
36 #include "src/ic/call-optimization.h"
37 #include "src/ic/ic.h"
39 #include "src/ic/ic-inl.h"
40 #include "src/lithium-allocator.h"
41 #include "src/parser.h"
42 #include "src/runtime/runtime.h"
43 #include "src/scopeinfo.h"
44 #include "src/typing.h"
46 #if V8_TARGET_ARCH_IA32
47 #include "src/ia32/lithium-codegen-ia32.h" // NOLINT
48 #elif V8_TARGET_ARCH_X64
49 #include "src/x64/lithium-codegen-x64.h" // NOLINT
50 #elif V8_TARGET_ARCH_ARM64
51 #include "src/arm64/lithium-codegen-arm64.h" // NOLINT
52 #elif V8_TARGET_ARCH_ARM
53 #include "src/arm/lithium-codegen-arm.h" // NOLINT
54 #elif V8_TARGET_ARCH_PPC
55 #include "src/ppc/lithium-codegen-ppc.h" // NOLINT
56 #elif V8_TARGET_ARCH_MIPS
57 #include "src/mips/lithium-codegen-mips.h" // NOLINT
58 #elif V8_TARGET_ARCH_MIPS64
59 #include "src/mips64/lithium-codegen-mips64.h" // NOLINT
60 #elif V8_TARGET_ARCH_X87
61 #include "src/x87/lithium-codegen-x87.h" // NOLINT
63 #error Unsupported target architecture.
69 HBasicBlock::HBasicBlock(HGraph* graph)
70 : block_id_(graph->GetNextBlockID()),
72 phis_(4, graph->zone()),
76 loop_information_(NULL),
77 predecessors_(2, graph->zone()),
79 dominated_blocks_(4, graph->zone()),
80 last_environment_(NULL),
82 first_instruction_index_(-1),
83 last_instruction_index_(-1),
84 deleted_phis_(4, graph->zone()),
85 parent_loop_header_(NULL),
86 inlined_entry_block_(NULL),
87 is_inline_return_target_(false),
89 dominates_loop_successors_(false),
91 is_ordered_(false) { }
94 Isolate* HBasicBlock::isolate() const {
95 return graph_->isolate();
99 void HBasicBlock::MarkUnreachable() {
100 is_reachable_ = false;
104 void HBasicBlock::AttachLoopInformation() {
105 DCHECK(!IsLoopHeader());
106 loop_information_ = new(zone()) HLoopInformation(this, zone());
110 void HBasicBlock::DetachLoopInformation() {
111 DCHECK(IsLoopHeader());
112 loop_information_ = NULL;
116 void HBasicBlock::AddPhi(HPhi* phi) {
117 DCHECK(!IsStartBlock());
118 phis_.Add(phi, zone());
123 void HBasicBlock::RemovePhi(HPhi* phi) {
124 DCHECK(phi->block() == this);
125 DCHECK(phis_.Contains(phi));
127 phis_.RemoveElement(phi);
132 void HBasicBlock::AddInstruction(HInstruction* instr, SourcePosition position) {
133 DCHECK(!IsStartBlock() || !IsFinished());
134 DCHECK(!instr->IsLinked());
135 DCHECK(!IsFinished());
137 if (!position.IsUnknown()) {
138 instr->set_position(position);
140 if (first_ == NULL) {
141 DCHECK(last_environment() != NULL);
142 DCHECK(!last_environment()->ast_id().IsNone());
143 HBlockEntry* entry = new(zone()) HBlockEntry();
144 entry->InitializeAsFirst(this);
145 if (!position.IsUnknown()) {
146 entry->set_position(position);
148 DCHECK(!FLAG_hydrogen_track_positions ||
149 !graph()->info()->IsOptimizing() || instr->IsAbnormalExit());
151 first_ = last_ = entry;
153 instr->InsertAfter(last_);
157 HPhi* HBasicBlock::AddNewPhi(int merged_index) {
158 if (graph()->IsInsideNoSideEffectsScope()) {
159 merged_index = HPhi::kInvalidMergedIndex;
161 HPhi* phi = new(zone()) HPhi(merged_index, zone());
167 HSimulate* HBasicBlock::CreateSimulate(BailoutId ast_id,
168 RemovableSimulate removable) {
169 DCHECK(HasEnvironment());
170 HEnvironment* environment = last_environment();
171 DCHECK(ast_id.IsNone() ||
172 ast_id == BailoutId::StubEntry() ||
173 environment->closure()->shared()->VerifyBailoutId(ast_id));
175 int push_count = environment->push_count();
176 int pop_count = environment->pop_count();
179 new(zone()) HSimulate(ast_id, pop_count, zone(), removable);
181 instr->set_closure(environment->closure());
183 // Order of pushed values: newest (top of stack) first. This allows
184 // HSimulate::MergeWith() to easily append additional pushed values
185 // that are older (from further down the stack).
186 for (int i = 0; i < push_count; ++i) {
187 instr->AddPushedValue(environment->ExpressionStackAt(i));
189 for (GrowableBitVector::Iterator it(environment->assigned_variables(),
193 int index = it.Current();
194 instr->AddAssignedValue(index, environment->Lookup(index));
196 environment->ClearHistory();
201 void HBasicBlock::Finish(HControlInstruction* end, SourcePosition position) {
202 DCHECK(!IsFinished());
203 AddInstruction(end, position);
205 for (HSuccessorIterator it(end); !it.Done(); it.Advance()) {
206 it.Current()->RegisterPredecessor(this);
211 void HBasicBlock::Goto(HBasicBlock* block, SourcePosition position,
212 FunctionState* state, bool add_simulate) {
213 bool drop_extra = state != NULL &&
214 state->inlining_kind() == NORMAL_RETURN;
216 if (block->IsInlineReturnTarget()) {
217 HEnvironment* env = last_environment();
218 int argument_count = env->arguments_environment()->parameter_count();
219 AddInstruction(new(zone())
220 HLeaveInlined(state->entry(), argument_count),
222 UpdateEnvironment(last_environment()->DiscardInlined(drop_extra));
225 if (add_simulate) AddNewSimulate(BailoutId::None(), position);
226 HGoto* instr = new(zone()) HGoto(block);
227 Finish(instr, position);
231 void HBasicBlock::AddLeaveInlined(HValue* return_value, FunctionState* state,
232 SourcePosition position) {
233 HBasicBlock* target = state->function_return();
234 bool drop_extra = state->inlining_kind() == NORMAL_RETURN;
236 DCHECK(target->IsInlineReturnTarget());
237 DCHECK(return_value != NULL);
238 HEnvironment* env = last_environment();
239 int argument_count = env->arguments_environment()->parameter_count();
240 AddInstruction(new(zone()) HLeaveInlined(state->entry(), argument_count),
242 UpdateEnvironment(last_environment()->DiscardInlined(drop_extra));
243 last_environment()->Push(return_value);
244 AddNewSimulate(BailoutId::None(), position);
245 HGoto* instr = new(zone()) HGoto(target);
246 Finish(instr, position);
250 void HBasicBlock::SetInitialEnvironment(HEnvironment* env) {
251 DCHECK(!HasEnvironment());
252 DCHECK(first() == NULL);
253 UpdateEnvironment(env);
257 void HBasicBlock::UpdateEnvironment(HEnvironment* env) {
258 last_environment_ = env;
259 graph()->update_maximum_environment_size(env->first_expression_index());
263 void HBasicBlock::SetJoinId(BailoutId ast_id) {
264 int length = predecessors_.length();
266 for (int i = 0; i < length; i++) {
267 HBasicBlock* predecessor = predecessors_[i];
268 DCHECK(predecessor->end()->IsGoto());
269 HSimulate* simulate = HSimulate::cast(predecessor->end()->previous());
271 (predecessor->last_environment()->closure().is_null() ||
272 predecessor->last_environment()->closure()->shared()
273 ->VerifyBailoutId(ast_id)));
274 simulate->set_ast_id(ast_id);
275 predecessor->last_environment()->set_ast_id(ast_id);
280 bool HBasicBlock::Dominates(HBasicBlock* other) const {
281 HBasicBlock* current = other->dominator();
282 while (current != NULL) {
283 if (current == this) return true;
284 current = current->dominator();
290 bool HBasicBlock::EqualToOrDominates(HBasicBlock* other) const {
291 if (this == other) return true;
292 return Dominates(other);
296 int HBasicBlock::LoopNestingDepth() const {
297 const HBasicBlock* current = this;
298 int result = (current->IsLoopHeader()) ? 1 : 0;
299 while (current->parent_loop_header() != NULL) {
300 current = current->parent_loop_header();
307 void HBasicBlock::PostProcessLoopHeader(IterationStatement* stmt) {
308 DCHECK(IsLoopHeader());
310 SetJoinId(stmt->EntryId());
311 if (predecessors()->length() == 1) {
312 // This is a degenerated loop.
313 DetachLoopInformation();
317 // Only the first entry into the loop is from outside the loop. All other
318 // entries must be back edges.
319 for (int i = 1; i < predecessors()->length(); ++i) {
320 loop_information()->RegisterBackEdge(predecessors()->at(i));
325 void HBasicBlock::MarkSuccEdgeUnreachable(int succ) {
326 DCHECK(IsFinished());
327 HBasicBlock* succ_block = end()->SuccessorAt(succ);
329 DCHECK(succ_block->predecessors()->length() == 1);
330 succ_block->MarkUnreachable();
334 void HBasicBlock::RegisterPredecessor(HBasicBlock* pred) {
335 if (HasPredecessor()) {
336 // Only loop header blocks can have a predecessor added after
337 // instructions have been added to the block (they have phis for all
338 // values in the environment, these phis may be eliminated later).
339 DCHECK(IsLoopHeader() || first_ == NULL);
340 HEnvironment* incoming_env = pred->last_environment();
341 if (IsLoopHeader()) {
342 DCHECK_EQ(phis()->length(), incoming_env->length());
343 for (int i = 0; i < phis_.length(); ++i) {
344 phis_[i]->AddInput(incoming_env->values()->at(i));
347 last_environment()->AddIncomingEdge(this, pred->last_environment());
349 } else if (!HasEnvironment() && !IsFinished()) {
350 DCHECK(!IsLoopHeader());
351 SetInitialEnvironment(pred->last_environment()->Copy());
354 predecessors_.Add(pred, zone());
358 void HBasicBlock::AddDominatedBlock(HBasicBlock* block) {
359 DCHECK(!dominated_blocks_.Contains(block));
360 // Keep the list of dominated blocks sorted such that if there is two
361 // succeeding block in this list, the predecessor is before the successor.
363 while (index < dominated_blocks_.length() &&
364 dominated_blocks_[index]->block_id() < block->block_id()) {
367 dominated_blocks_.InsertAt(index, block, zone());
371 void HBasicBlock::AssignCommonDominator(HBasicBlock* other) {
372 if (dominator_ == NULL) {
374 other->AddDominatedBlock(this);
375 } else if (other->dominator() != NULL) {
376 HBasicBlock* first = dominator_;
377 HBasicBlock* second = other;
379 while (first != second) {
380 if (first->block_id() > second->block_id()) {
381 first = first->dominator();
383 second = second->dominator();
385 DCHECK(first != NULL && second != NULL);
388 if (dominator_ != first) {
389 DCHECK(dominator_->dominated_blocks_.Contains(this));
390 dominator_->dominated_blocks_.RemoveElement(this);
392 first->AddDominatedBlock(this);
398 void HBasicBlock::AssignLoopSuccessorDominators() {
399 // Mark blocks that dominate all subsequent reachable blocks inside their
400 // loop. Exploit the fact that blocks are sorted in reverse post order. When
401 // the loop is visited in increasing block id order, if the number of
402 // non-loop-exiting successor edges at the dominator_candidate block doesn't
403 // exceed the number of previously encountered predecessor edges, there is no
404 // path from the loop header to any block with higher id that doesn't go
405 // through the dominator_candidate block. In this case, the
406 // dominator_candidate block is guaranteed to dominate all blocks reachable
407 // from it with higher ids.
408 HBasicBlock* last = loop_information()->GetLastBackEdge();
409 int outstanding_successors = 1; // one edge from the pre-header
410 // Header always dominates everything.
411 MarkAsLoopSuccessorDominator();
412 for (int j = block_id(); j <= last->block_id(); ++j) {
413 HBasicBlock* dominator_candidate = graph_->blocks()->at(j);
414 for (HPredecessorIterator it(dominator_candidate); !it.Done();
416 HBasicBlock* predecessor = it.Current();
417 // Don't count back edges.
418 if (predecessor->block_id() < dominator_candidate->block_id()) {
419 outstanding_successors--;
423 // If more successors than predecessors have been seen in the loop up to
424 // now, it's not possible to guarantee that the current block dominates
425 // all of the blocks with higher IDs. In this case, assume conservatively
426 // that those paths through loop that don't go through the current block
427 // contain all of the loop's dependencies. Also be careful to record
428 // dominator information about the current loop that's being processed,
429 // and not nested loops, which will be processed when
430 // AssignLoopSuccessorDominators gets called on their header.
431 DCHECK(outstanding_successors >= 0);
432 HBasicBlock* parent_loop_header = dominator_candidate->parent_loop_header();
433 if (outstanding_successors == 0 &&
434 (parent_loop_header == this && !dominator_candidate->IsLoopHeader())) {
435 dominator_candidate->MarkAsLoopSuccessorDominator();
437 HControlInstruction* end = dominator_candidate->end();
438 for (HSuccessorIterator it(end); !it.Done(); it.Advance()) {
439 HBasicBlock* successor = it.Current();
440 // Only count successors that remain inside the loop and don't loop back
442 if (successor->block_id() > dominator_candidate->block_id() &&
443 successor->block_id() <= last->block_id()) {
444 // Backwards edges must land on loop headers.
445 DCHECK(successor->block_id() > dominator_candidate->block_id() ||
446 successor->IsLoopHeader());
447 outstanding_successors++;
454 int HBasicBlock::PredecessorIndexOf(HBasicBlock* predecessor) const {
455 for (int i = 0; i < predecessors_.length(); ++i) {
456 if (predecessors_[i] == predecessor) return i;
464 void HBasicBlock::Verify() {
465 // Check that every block is finished.
466 DCHECK(IsFinished());
467 DCHECK(block_id() >= 0);
469 // Check that the incoming edges are in edge split form.
470 if (predecessors_.length() > 1) {
471 for (int i = 0; i < predecessors_.length(); ++i) {
472 DCHECK(predecessors_[i]->end()->SecondSuccessor() == NULL);
479 void HLoopInformation::RegisterBackEdge(HBasicBlock* block) {
480 this->back_edges_.Add(block, block->zone());
485 HBasicBlock* HLoopInformation::GetLastBackEdge() const {
487 HBasicBlock* result = NULL;
488 for (int i = 0; i < back_edges_.length(); ++i) {
489 HBasicBlock* cur = back_edges_[i];
490 if (cur->block_id() > max_id) {
491 max_id = cur->block_id();
499 void HLoopInformation::AddBlock(HBasicBlock* block) {
500 if (block == loop_header()) return;
501 if (block->parent_loop_header() == loop_header()) return;
502 if (block->parent_loop_header() != NULL) {
503 AddBlock(block->parent_loop_header());
505 block->set_parent_loop_header(loop_header());
506 blocks_.Add(block, block->zone());
507 for (int i = 0; i < block->predecessors()->length(); ++i) {
508 AddBlock(block->predecessors()->at(i));
516 // Checks reachability of the blocks in this graph and stores a bit in
517 // the BitVector "reachable()" for every block that can be reached
518 // from the start block of the graph. If "dont_visit" is non-null, the given
519 // block is treated as if it would not be part of the graph. "visited_count()"
520 // returns the number of reachable blocks.
521 class ReachabilityAnalyzer BASE_EMBEDDED {
523 ReachabilityAnalyzer(HBasicBlock* entry_block,
525 HBasicBlock* dont_visit)
527 stack_(16, entry_block->zone()),
528 reachable_(block_count, entry_block->zone()),
529 dont_visit_(dont_visit) {
530 PushBlock(entry_block);
534 int visited_count() const { return visited_count_; }
535 const BitVector* reachable() const { return &reachable_; }
538 void PushBlock(HBasicBlock* block) {
539 if (block != NULL && block != dont_visit_ &&
540 !reachable_.Contains(block->block_id())) {
541 reachable_.Add(block->block_id());
542 stack_.Add(block, block->zone());
548 while (!stack_.is_empty()) {
549 HControlInstruction* end = stack_.RemoveLast()->end();
550 for (HSuccessorIterator it(end); !it.Done(); it.Advance()) {
551 PushBlock(it.Current());
557 ZoneList<HBasicBlock*> stack_;
558 BitVector reachable_;
559 HBasicBlock* dont_visit_;
563 void HGraph::Verify(bool do_full_verify) const {
564 Heap::RelocationLock relocation_lock(isolate()->heap());
565 AllowHandleDereference allow_deref;
566 AllowDeferredHandleDereference allow_deferred_deref;
567 for (int i = 0; i < blocks_.length(); i++) {
568 HBasicBlock* block = blocks_.at(i);
572 // Check that every block contains at least one node and that only the last
573 // node is a control instruction.
574 HInstruction* current = block->first();
575 DCHECK(current != NULL && current->IsBlockEntry());
576 while (current != NULL) {
577 DCHECK((current->next() == NULL) == current->IsControlInstruction());
578 DCHECK(current->block() == block);
580 current = current->next();
583 // Check that successors are correctly set.
584 HBasicBlock* first = block->end()->FirstSuccessor();
585 HBasicBlock* second = block->end()->SecondSuccessor();
586 DCHECK(second == NULL || first != NULL);
588 // Check that the predecessor array is correct.
590 DCHECK(first->predecessors()->Contains(block));
591 if (second != NULL) {
592 DCHECK(second->predecessors()->Contains(block));
596 // Check that phis have correct arguments.
597 for (int j = 0; j < block->phis()->length(); j++) {
598 HPhi* phi = block->phis()->at(j);
602 // Check that all join blocks have predecessors that end with an
603 // unconditional goto and agree on their environment node id.
604 if (block->predecessors()->length() >= 2) {
606 block->predecessors()->first()->last_environment()->ast_id();
607 for (int k = 0; k < block->predecessors()->length(); k++) {
608 HBasicBlock* predecessor = block->predecessors()->at(k);
609 DCHECK(predecessor->end()->IsGoto() ||
610 predecessor->end()->IsDeoptimize());
611 DCHECK(predecessor->last_environment()->ast_id() == id);
616 // Check special property of first block to have no predecessors.
617 DCHECK(blocks_.at(0)->predecessors()->is_empty());
619 if (do_full_verify) {
620 // Check that the graph is fully connected.
621 ReachabilityAnalyzer analyzer(entry_block_, blocks_.length(), NULL);
622 DCHECK(analyzer.visited_count() == blocks_.length());
624 // Check that entry block dominator is NULL.
625 DCHECK(entry_block_->dominator() == NULL);
628 for (int i = 0; i < blocks_.length(); ++i) {
629 HBasicBlock* block = blocks_.at(i);
630 if (block->dominator() == NULL) {
631 // Only start block may have no dominator assigned to.
634 // Assert that block is unreachable if dominator must not be visited.
635 ReachabilityAnalyzer dominator_analyzer(entry_block_,
638 DCHECK(!dominator_analyzer.reachable()->Contains(block->block_id()));
647 HConstant* HGraph::GetConstant(SetOncePointer<HConstant>* pointer,
649 if (!pointer->is_set()) {
650 // Can't pass GetInvalidContext() to HConstant::New, because that will
651 // recursively call GetConstant
652 HConstant* constant = HConstant::New(isolate(), zone(), NULL, value);
653 constant->InsertAfter(entry_block()->first());
654 pointer->set(constant);
657 return ReinsertConstantIfNecessary(pointer->get());
661 HConstant* HGraph::ReinsertConstantIfNecessary(HConstant* constant) {
662 if (!constant->IsLinked()) {
663 // The constant was removed from the graph. Reinsert.
664 constant->ClearFlag(HValue::kIsDead);
665 constant->InsertAfter(entry_block()->first());
671 HConstant* HGraph::GetConstant0() {
672 return GetConstant(&constant_0_, 0);
676 HConstant* HGraph::GetConstant1() {
677 return GetConstant(&constant_1_, 1);
681 HConstant* HGraph::GetConstantMinus1() {
682 return GetConstant(&constant_minus1_, -1);
686 HConstant* HGraph::GetConstantBool(bool value) {
687 return value ? GetConstantTrue() : GetConstantFalse();
691 #define DEFINE_GET_CONSTANT(Name, name, type, htype, boolean_value) \
692 HConstant* HGraph::GetConstant##Name() { \
693 if (!constant_##name##_.is_set()) { \
694 HConstant* constant = new(zone()) HConstant( \
695 Unique<Object>::CreateImmovable(isolate()->factory()->name##_value()), \
696 Unique<Map>::CreateImmovable(isolate()->factory()->type##_map()), \
698 Representation::Tagged(), \
704 constant->InsertAfter(entry_block()->first()); \
705 constant_##name##_.set(constant); \
707 return ReinsertConstantIfNecessary(constant_##name##_.get()); \
711 DEFINE_GET_CONSTANT(Undefined, undefined, undefined, HType::Undefined(), false)
712 DEFINE_GET_CONSTANT(True, true, boolean, HType::Boolean(), true)
713 DEFINE_GET_CONSTANT(False, false, boolean, HType::Boolean(), false)
714 DEFINE_GET_CONSTANT(Hole, the_hole, the_hole, HType::None(), false)
715 DEFINE_GET_CONSTANT(Null, null, null, HType::Null(), false)
718 #undef DEFINE_GET_CONSTANT
720 #define DEFINE_IS_CONSTANT(Name, name) \
721 bool HGraph::IsConstant##Name(HConstant* constant) { \
722 return constant_##name##_.is_set() && constant == constant_##name##_.get(); \
724 DEFINE_IS_CONSTANT(Undefined, undefined)
725 DEFINE_IS_CONSTANT(0, 0)
726 DEFINE_IS_CONSTANT(1, 1)
727 DEFINE_IS_CONSTANT(Minus1, minus1)
728 DEFINE_IS_CONSTANT(True, true)
729 DEFINE_IS_CONSTANT(False, false)
730 DEFINE_IS_CONSTANT(Hole, the_hole)
731 DEFINE_IS_CONSTANT(Null, null)
733 #undef DEFINE_IS_CONSTANT
736 HConstant* HGraph::GetInvalidContext() {
737 return GetConstant(&constant_invalid_context_, 0xFFFFC0C7);
741 bool HGraph::IsStandardConstant(HConstant* constant) {
742 if (IsConstantUndefined(constant)) return true;
743 if (IsConstant0(constant)) return true;
744 if (IsConstant1(constant)) return true;
745 if (IsConstantMinus1(constant)) return true;
746 if (IsConstantTrue(constant)) return true;
747 if (IsConstantFalse(constant)) return true;
748 if (IsConstantHole(constant)) return true;
749 if (IsConstantNull(constant)) return true;
754 HGraphBuilder::IfBuilder::IfBuilder() : builder_(NULL), needs_compare_(true) {}
757 HGraphBuilder::IfBuilder::IfBuilder(HGraphBuilder* builder)
758 : needs_compare_(true) {
763 HGraphBuilder::IfBuilder::IfBuilder(HGraphBuilder* builder,
764 HIfContinuation* continuation)
765 : needs_compare_(false), first_true_block_(NULL), first_false_block_(NULL) {
766 InitializeDontCreateBlocks(builder);
767 continuation->Continue(&first_true_block_, &first_false_block_);
771 void HGraphBuilder::IfBuilder::InitializeDontCreateBlocks(
772 HGraphBuilder* builder) {
777 did_else_if_ = false;
781 pending_merge_block_ = false;
782 split_edge_merge_block_ = NULL;
783 merge_at_join_blocks_ = NULL;
784 normal_merge_at_join_block_count_ = 0;
785 deopt_merge_at_join_block_count_ = 0;
789 void HGraphBuilder::IfBuilder::Initialize(HGraphBuilder* builder) {
790 InitializeDontCreateBlocks(builder);
791 HEnvironment* env = builder->environment();
792 first_true_block_ = builder->CreateBasicBlock(env->Copy());
793 first_false_block_ = builder->CreateBasicBlock(env->Copy());
797 HControlInstruction* HGraphBuilder::IfBuilder::AddCompare(
798 HControlInstruction* compare) {
799 DCHECK(did_then_ == did_else_);
801 // Handle if-then-elseif
807 pending_merge_block_ = false;
808 split_edge_merge_block_ = NULL;
809 HEnvironment* env = builder()->environment();
810 first_true_block_ = builder()->CreateBasicBlock(env->Copy());
811 first_false_block_ = builder()->CreateBasicBlock(env->Copy());
813 if (split_edge_merge_block_ != NULL) {
814 HEnvironment* env = first_false_block_->last_environment();
815 HBasicBlock* split_edge = builder()->CreateBasicBlock(env->Copy());
817 compare->SetSuccessorAt(0, split_edge);
818 compare->SetSuccessorAt(1, first_false_block_);
820 compare->SetSuccessorAt(0, first_true_block_);
821 compare->SetSuccessorAt(1, split_edge);
823 builder()->GotoNoSimulate(split_edge, split_edge_merge_block_);
825 compare->SetSuccessorAt(0, first_true_block_);
826 compare->SetSuccessorAt(1, first_false_block_);
828 builder()->FinishCurrentBlock(compare);
829 needs_compare_ = false;
834 void HGraphBuilder::IfBuilder::Or() {
835 DCHECK(!needs_compare_);
838 HEnvironment* env = first_false_block_->last_environment();
839 if (split_edge_merge_block_ == NULL) {
840 split_edge_merge_block_ = builder()->CreateBasicBlock(env->Copy());
841 builder()->GotoNoSimulate(first_true_block_, split_edge_merge_block_);
842 first_true_block_ = split_edge_merge_block_;
844 builder()->set_current_block(first_false_block_);
845 first_false_block_ = builder()->CreateBasicBlock(env->Copy());
849 void HGraphBuilder::IfBuilder::And() {
850 DCHECK(!needs_compare_);
853 HEnvironment* env = first_false_block_->last_environment();
854 if (split_edge_merge_block_ == NULL) {
855 split_edge_merge_block_ = builder()->CreateBasicBlock(env->Copy());
856 builder()->GotoNoSimulate(first_false_block_, split_edge_merge_block_);
857 first_false_block_ = split_edge_merge_block_;
859 builder()->set_current_block(first_true_block_);
860 first_true_block_ = builder()->CreateBasicBlock(env->Copy());
864 void HGraphBuilder::IfBuilder::CaptureContinuation(
865 HIfContinuation* continuation) {
866 DCHECK(!did_else_if_);
870 HBasicBlock* true_block = NULL;
871 HBasicBlock* false_block = NULL;
872 Finish(&true_block, &false_block);
873 DCHECK(true_block != NULL);
874 DCHECK(false_block != NULL);
875 continuation->Capture(true_block, false_block);
877 builder()->set_current_block(NULL);
882 void HGraphBuilder::IfBuilder::JoinContinuation(HIfContinuation* continuation) {
883 DCHECK(!did_else_if_);
886 HBasicBlock* true_block = NULL;
887 HBasicBlock* false_block = NULL;
888 Finish(&true_block, &false_block);
889 merge_at_join_blocks_ = NULL;
890 if (true_block != NULL && !true_block->IsFinished()) {
891 DCHECK(continuation->IsTrueReachable());
892 builder()->GotoNoSimulate(true_block, continuation->true_branch());
894 if (false_block != NULL && !false_block->IsFinished()) {
895 DCHECK(continuation->IsFalseReachable());
896 builder()->GotoNoSimulate(false_block, continuation->false_branch());
903 void HGraphBuilder::IfBuilder::Then() {
907 if (needs_compare_) {
908 // Handle if's without any expressions, they jump directly to the "else"
909 // branch. However, we must pretend that the "then" branch is reachable,
910 // so that the graph builder visits it and sees any live range extending
911 // constructs within it.
912 HConstant* constant_false = builder()->graph()->GetConstantFalse();
913 ToBooleanStub::Types boolean_type = ToBooleanStub::Types();
914 boolean_type.Add(ToBooleanStub::BOOLEAN);
915 HBranch* branch = builder()->New<HBranch>(
916 constant_false, boolean_type, first_true_block_, first_false_block_);
917 builder()->FinishCurrentBlock(branch);
919 builder()->set_current_block(first_true_block_);
920 pending_merge_block_ = true;
924 void HGraphBuilder::IfBuilder::Else() {
928 AddMergeAtJoinBlock(false);
929 builder()->set_current_block(first_false_block_);
930 pending_merge_block_ = true;
935 void HGraphBuilder::IfBuilder::Deopt(Deoptimizer::DeoptReason reason) {
937 builder()->Add<HDeoptimize>(reason, Deoptimizer::EAGER);
938 AddMergeAtJoinBlock(true);
942 void HGraphBuilder::IfBuilder::Return(HValue* value) {
943 HValue* parameter_count = builder()->graph()->GetConstantMinus1();
944 builder()->FinishExitCurrentBlock(
945 builder()->New<HReturn>(value, parameter_count));
946 AddMergeAtJoinBlock(false);
950 void HGraphBuilder::IfBuilder::AddMergeAtJoinBlock(bool deopt) {
951 if (!pending_merge_block_) return;
952 HBasicBlock* block = builder()->current_block();
953 DCHECK(block == NULL || !block->IsFinished());
954 MergeAtJoinBlock* record = new (builder()->zone())
955 MergeAtJoinBlock(block, deopt, merge_at_join_blocks_);
956 merge_at_join_blocks_ = record;
958 DCHECK(block->end() == NULL);
960 normal_merge_at_join_block_count_++;
962 deopt_merge_at_join_block_count_++;
965 builder()->set_current_block(NULL);
966 pending_merge_block_ = false;
970 void HGraphBuilder::IfBuilder::Finish() {
975 AddMergeAtJoinBlock(false);
978 AddMergeAtJoinBlock(false);
984 void HGraphBuilder::IfBuilder::Finish(HBasicBlock** then_continuation,
985 HBasicBlock** else_continuation) {
988 MergeAtJoinBlock* else_record = merge_at_join_blocks_;
989 if (else_continuation != NULL) {
990 *else_continuation = else_record->block_;
992 MergeAtJoinBlock* then_record = else_record->next_;
993 if (then_continuation != NULL) {
994 *then_continuation = then_record->block_;
996 DCHECK(then_record->next_ == NULL);
1000 void HGraphBuilder::IfBuilder::EndUnreachable() {
1001 if (captured_) return;
1003 builder()->set_current_block(nullptr);
1007 void HGraphBuilder::IfBuilder::End() {
1008 if (captured_) return;
1011 int total_merged_blocks = normal_merge_at_join_block_count_ +
1012 deopt_merge_at_join_block_count_;
1013 DCHECK(total_merged_blocks >= 1);
1014 HBasicBlock* merge_block =
1015 total_merged_blocks == 1 ? NULL : builder()->graph()->CreateBasicBlock();
1017 // Merge non-deopt blocks first to ensure environment has right size for
1019 MergeAtJoinBlock* current = merge_at_join_blocks_;
1020 while (current != NULL) {
1021 if (!current->deopt_ && current->block_ != NULL) {
1022 // If there is only one block that makes it through to the end of the
1023 // if, then just set it as the current block and continue rather then
1024 // creating an unnecessary merge block.
1025 if (total_merged_blocks == 1) {
1026 builder()->set_current_block(current->block_);
1029 builder()->GotoNoSimulate(current->block_, merge_block);
1031 current = current->next_;
1034 // Merge deopt blocks, padding when necessary.
1035 current = merge_at_join_blocks_;
1036 while (current != NULL) {
1037 if (current->deopt_ && current->block_ != NULL) {
1038 current->block_->FinishExit(
1039 HAbnormalExit::New(builder()->isolate(), builder()->zone(), NULL),
1040 SourcePosition::Unknown());
1042 current = current->next_;
1044 builder()->set_current_block(merge_block);
1048 HGraphBuilder::LoopBuilder::LoopBuilder(HGraphBuilder* builder) {
1049 Initialize(builder, NULL, kWhileTrue, NULL);
1053 HGraphBuilder::LoopBuilder::LoopBuilder(HGraphBuilder* builder, HValue* context,
1054 LoopBuilder::Direction direction) {
1055 Initialize(builder, context, direction, builder->graph()->GetConstant1());
1059 HGraphBuilder::LoopBuilder::LoopBuilder(HGraphBuilder* builder, HValue* context,
1060 LoopBuilder::Direction direction,
1061 HValue* increment_amount) {
1062 Initialize(builder, context, direction, increment_amount);
1063 increment_amount_ = increment_amount;
1067 void HGraphBuilder::LoopBuilder::Initialize(HGraphBuilder* builder,
1069 Direction direction,
1070 HValue* increment_amount) {
1073 direction_ = direction;
1074 increment_amount_ = increment_amount;
1077 header_block_ = builder->CreateLoopHeaderBlock();
1080 exit_trampoline_block_ = NULL;
1084 HValue* HGraphBuilder::LoopBuilder::BeginBody(
1086 HValue* terminating,
1087 Token::Value token) {
1088 DCHECK(direction_ != kWhileTrue);
1089 HEnvironment* env = builder_->environment();
1090 phi_ = header_block_->AddNewPhi(env->values()->length());
1091 phi_->AddInput(initial);
1093 builder_->GotoNoSimulate(header_block_);
1095 HEnvironment* body_env = env->Copy();
1096 HEnvironment* exit_env = env->Copy();
1097 // Remove the phi from the expression stack
1100 body_block_ = builder_->CreateBasicBlock(body_env);
1101 exit_block_ = builder_->CreateBasicBlock(exit_env);
1103 builder_->set_current_block(header_block_);
1105 builder_->FinishCurrentBlock(builder_->New<HCompareNumericAndBranch>(
1106 phi_, terminating, token, body_block_, exit_block_));
1108 builder_->set_current_block(body_block_);
1109 if (direction_ == kPreIncrement || direction_ == kPreDecrement) {
1110 Isolate* isolate = builder_->isolate();
1111 HValue* one = builder_->graph()->GetConstant1();
1112 if (direction_ == kPreIncrement) {
1113 increment_ = HAdd::New(isolate, zone(), context_, phi_, one);
1115 increment_ = HSub::New(isolate, zone(), context_, phi_, one);
1117 increment_->ClearFlag(HValue::kCanOverflow);
1118 builder_->AddInstruction(increment_);
1126 void HGraphBuilder::LoopBuilder::BeginBody(int drop_count) {
1127 DCHECK(direction_ == kWhileTrue);
1128 HEnvironment* env = builder_->environment();
1129 builder_->GotoNoSimulate(header_block_);
1130 builder_->set_current_block(header_block_);
1131 env->Drop(drop_count);
1135 void HGraphBuilder::LoopBuilder::Break() {
1136 if (exit_trampoline_block_ == NULL) {
1137 // Its the first time we saw a break.
1138 if (direction_ == kWhileTrue) {
1139 HEnvironment* env = builder_->environment()->Copy();
1140 exit_trampoline_block_ = builder_->CreateBasicBlock(env);
1142 HEnvironment* env = exit_block_->last_environment()->Copy();
1143 exit_trampoline_block_ = builder_->CreateBasicBlock(env);
1144 builder_->GotoNoSimulate(exit_block_, exit_trampoline_block_);
1148 builder_->GotoNoSimulate(exit_trampoline_block_);
1149 builder_->set_current_block(NULL);
1153 void HGraphBuilder::LoopBuilder::EndBody() {
1156 if (direction_ == kPostIncrement || direction_ == kPostDecrement) {
1157 Isolate* isolate = builder_->isolate();
1158 if (direction_ == kPostIncrement) {
1160 HAdd::New(isolate, zone(), context_, phi_, increment_amount_);
1163 HSub::New(isolate, zone(), context_, phi_, increment_amount_);
1165 increment_->ClearFlag(HValue::kCanOverflow);
1166 builder_->AddInstruction(increment_);
1169 if (direction_ != kWhileTrue) {
1170 // Push the new increment value on the expression stack to merge into
1172 builder_->environment()->Push(increment_);
1174 HBasicBlock* last_block = builder_->current_block();
1175 builder_->GotoNoSimulate(last_block, header_block_);
1176 header_block_->loop_information()->RegisterBackEdge(last_block);
1178 if (exit_trampoline_block_ != NULL) {
1179 builder_->set_current_block(exit_trampoline_block_);
1181 builder_->set_current_block(exit_block_);
1187 HGraph* HGraphBuilder::CreateGraph() {
1188 graph_ = new(zone()) HGraph(info_);
1189 if (FLAG_hydrogen_stats) isolate()->GetHStatistics()->Initialize(info_);
1190 CompilationPhase phase("H_Block building", info_);
1191 set_current_block(graph()->entry_block());
1192 if (!BuildGraph()) return NULL;
1193 graph()->FinalizeUniqueness();
1198 HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) {
1199 DCHECK(current_block() != NULL);
1200 DCHECK(!FLAG_hydrogen_track_positions ||
1201 !position_.IsUnknown() ||
1202 !info_->IsOptimizing());
1203 current_block()->AddInstruction(instr, source_position());
1204 if (graph()->IsInsideNoSideEffectsScope()) {
1205 instr->SetFlag(HValue::kHasNoObservableSideEffects);
1211 void HGraphBuilder::FinishCurrentBlock(HControlInstruction* last) {
1212 DCHECK(!FLAG_hydrogen_track_positions ||
1213 !info_->IsOptimizing() ||
1214 !position_.IsUnknown());
1215 current_block()->Finish(last, source_position());
1216 if (last->IsReturn() || last->IsAbnormalExit()) {
1217 set_current_block(NULL);
1222 void HGraphBuilder::FinishExitCurrentBlock(HControlInstruction* instruction) {
1223 DCHECK(!FLAG_hydrogen_track_positions || !info_->IsOptimizing() ||
1224 !position_.IsUnknown());
1225 current_block()->FinishExit(instruction, source_position());
1226 if (instruction->IsReturn() || instruction->IsAbnormalExit()) {
1227 set_current_block(NULL);
1232 void HGraphBuilder::AddIncrementCounter(StatsCounter* counter) {
1233 if (FLAG_native_code_counters && counter->Enabled()) {
1234 HValue* reference = Add<HConstant>(ExternalReference(counter));
1236 Add<HLoadNamedField>(reference, nullptr, HObjectAccess::ForCounter());
1237 HValue* new_value = AddUncasted<HAdd>(old_value, graph()->GetConstant1());
1238 new_value->ClearFlag(HValue::kCanOverflow); // Ignore counter overflow
1239 Add<HStoreNamedField>(reference, HObjectAccess::ForCounter(),
1240 new_value, STORE_TO_INITIALIZED_ENTRY);
1245 void HGraphBuilder::AddSimulate(BailoutId id,
1246 RemovableSimulate removable) {
1247 DCHECK(current_block() != NULL);
1248 DCHECK(!graph()->IsInsideNoSideEffectsScope());
1249 current_block()->AddNewSimulate(id, source_position(), removable);
1253 HBasicBlock* HGraphBuilder::CreateBasicBlock(HEnvironment* env) {
1254 HBasicBlock* b = graph()->CreateBasicBlock();
1255 b->SetInitialEnvironment(env);
1260 HBasicBlock* HGraphBuilder::CreateLoopHeaderBlock() {
1261 HBasicBlock* header = graph()->CreateBasicBlock();
1262 HEnvironment* entry_env = environment()->CopyAsLoopHeader(header);
1263 header->SetInitialEnvironment(entry_env);
1264 header->AttachLoopInformation();
1269 HValue* HGraphBuilder::BuildGetElementsKind(HValue* object) {
1270 HValue* map = Add<HLoadNamedField>(object, nullptr, HObjectAccess::ForMap());
1272 HValue* bit_field2 =
1273 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapBitField2());
1274 return BuildDecodeField<Map::ElementsKindBits>(bit_field2);
1278 HValue* HGraphBuilder::BuildCheckHeapObject(HValue* obj) {
1279 if (obj->type().IsHeapObject()) return obj;
1280 return Add<HCheckHeapObject>(obj);
1284 void HGraphBuilder::FinishExitWithHardDeoptimization(
1285 Deoptimizer::DeoptReason reason) {
1286 Add<HDeoptimize>(reason, Deoptimizer::EAGER);
1287 FinishExitCurrentBlock(New<HAbnormalExit>());
1291 HValue* HGraphBuilder::BuildCheckString(HValue* string) {
1292 if (!string->type().IsString()) {
1293 DCHECK(!string->IsConstant() ||
1294 !HConstant::cast(string)->HasStringValue());
1295 BuildCheckHeapObject(string);
1296 return Add<HCheckInstanceType>(string, HCheckInstanceType::IS_STRING);
1302 HValue* HGraphBuilder::BuildWrapReceiver(HValue* object, HValue* function) {
1303 if (object->type().IsJSObject()) return object;
1304 if (function->IsConstant() &&
1305 HConstant::cast(function)->handle(isolate())->IsJSFunction()) {
1306 Handle<JSFunction> f = Handle<JSFunction>::cast(
1307 HConstant::cast(function)->handle(isolate()));
1308 SharedFunctionInfo* shared = f->shared();
1309 if (is_strict(shared->language_mode()) || shared->native()) return object;
1311 return Add<HWrapReceiver>(object, function);
1315 HValue* HGraphBuilder::BuildCheckAndGrowElementsCapacity(
1316 HValue* object, HValue* elements, ElementsKind kind, HValue* length,
1317 HValue* capacity, HValue* key) {
1318 HValue* max_gap = Add<HConstant>(static_cast<int32_t>(JSObject::kMaxGap));
1319 HValue* max_capacity = AddUncasted<HAdd>(capacity, max_gap);
1320 Add<HBoundsCheck>(key, max_capacity);
1322 HValue* new_capacity = BuildNewElementsCapacity(key);
1323 HValue* new_elements = BuildGrowElementsCapacity(object, elements, kind, kind,
1324 length, new_capacity);
1325 return new_elements;
1329 HValue* HGraphBuilder::BuildCheckForCapacityGrow(
1336 PropertyAccessType access_type) {
1337 IfBuilder length_checker(this);
1339 Token::Value token = IsHoleyElementsKind(kind) ? Token::GTE : Token::EQ;
1340 length_checker.If<HCompareNumericAndBranch>(key, length, token);
1342 length_checker.Then();
1344 HValue* current_capacity = AddLoadFixedArrayLength(elements);
1346 if (top_info()->IsStub()) {
1347 IfBuilder capacity_checker(this);
1348 capacity_checker.If<HCompareNumericAndBranch>(key, current_capacity,
1350 capacity_checker.Then();
1351 HValue* new_elements = BuildCheckAndGrowElementsCapacity(
1352 object, elements, kind, length, current_capacity, key);
1353 environment()->Push(new_elements);
1354 capacity_checker.Else();
1355 environment()->Push(elements);
1356 capacity_checker.End();
1358 HValue* result = Add<HMaybeGrowElements>(
1359 object, elements, key, current_capacity, is_js_array, kind);
1360 environment()->Push(result);
1364 HValue* new_length = AddUncasted<HAdd>(key, graph_->GetConstant1());
1365 new_length->ClearFlag(HValue::kCanOverflow);
1367 Add<HStoreNamedField>(object, HObjectAccess::ForArrayLength(kind),
1371 if (access_type == STORE && kind == FAST_SMI_ELEMENTS) {
1372 HValue* checked_elements = environment()->Top();
1374 // Write zero to ensure that the new element is initialized with some smi.
1375 Add<HStoreKeyed>(checked_elements, key, graph()->GetConstant0(), kind);
1378 length_checker.Else();
1379 Add<HBoundsCheck>(key, length);
1381 environment()->Push(elements);
1382 length_checker.End();
1384 return environment()->Pop();
1388 HValue* HGraphBuilder::BuildCopyElementsOnWrite(HValue* object,
1392 Factory* factory = isolate()->factory();
1394 IfBuilder cow_checker(this);
1396 cow_checker.If<HCompareMap>(elements, factory->fixed_cow_array_map());
1399 HValue* capacity = AddLoadFixedArrayLength(elements);
1401 HValue* new_elements = BuildGrowElementsCapacity(object, elements, kind,
1402 kind, length, capacity);
1404 environment()->Push(new_elements);
1408 environment()->Push(elements);
1412 return environment()->Pop();
1416 void HGraphBuilder::BuildTransitionElementsKind(HValue* object,
1418 ElementsKind from_kind,
1419 ElementsKind to_kind,
1421 DCHECK(!IsFastHoleyElementsKind(from_kind) ||
1422 IsFastHoleyElementsKind(to_kind));
1424 if (AllocationSite::GetMode(from_kind, to_kind) == TRACK_ALLOCATION_SITE) {
1425 Add<HTrapAllocationMemento>(object);
1428 if (!IsSimpleMapChangeTransition(from_kind, to_kind)) {
1429 HInstruction* elements = AddLoadElements(object);
1431 HInstruction* empty_fixed_array = Add<HConstant>(
1432 isolate()->factory()->empty_fixed_array());
1434 IfBuilder if_builder(this);
1436 if_builder.IfNot<HCompareObjectEqAndBranch>(elements, empty_fixed_array);
1440 HInstruction* elements_length = AddLoadFixedArrayLength(elements);
1442 HInstruction* array_length =
1444 ? Add<HLoadNamedField>(object, nullptr,
1445 HObjectAccess::ForArrayLength(from_kind))
1448 BuildGrowElementsCapacity(object, elements, from_kind, to_kind,
1449 array_length, elements_length);
1454 Add<HStoreNamedField>(object, HObjectAccess::ForMap(), map);
1458 void HGraphBuilder::BuildJSObjectCheck(HValue* receiver,
1459 int bit_field_mask) {
1460 // Check that the object isn't a smi.
1461 Add<HCheckHeapObject>(receiver);
1463 // Get the map of the receiver.
1465 Add<HLoadNamedField>(receiver, nullptr, HObjectAccess::ForMap());
1467 // Check the instance type and if an access check is needed, this can be
1468 // done with a single load, since both bytes are adjacent in the map.
1469 HObjectAccess access(HObjectAccess::ForMapInstanceTypeAndBitField());
1470 HValue* instance_type_and_bit_field =
1471 Add<HLoadNamedField>(map, nullptr, access);
1473 HValue* mask = Add<HConstant>(0x00FF | (bit_field_mask << 8));
1474 HValue* and_result = AddUncasted<HBitwise>(Token::BIT_AND,
1475 instance_type_and_bit_field,
1477 HValue* sub_result = AddUncasted<HSub>(and_result,
1478 Add<HConstant>(JS_OBJECT_TYPE));
1479 Add<HBoundsCheck>(sub_result,
1480 Add<HConstant>(LAST_JS_OBJECT_TYPE + 1 - JS_OBJECT_TYPE));
1484 void HGraphBuilder::BuildKeyedIndexCheck(HValue* key,
1485 HIfContinuation* join_continuation) {
1486 // The sometimes unintuitively backward ordering of the ifs below is
1487 // convoluted, but necessary. All of the paths must guarantee that the
1488 // if-true of the continuation returns a smi element index and the if-false of
1489 // the continuation returns either a symbol or a unique string key. All other
1490 // object types cause a deopt to fall back to the runtime.
1492 IfBuilder key_smi_if(this);
1493 key_smi_if.If<HIsSmiAndBranch>(key);
1496 Push(key); // Nothing to do, just continue to true of continuation.
1500 HValue* map = Add<HLoadNamedField>(key, nullptr, HObjectAccess::ForMap());
1501 HValue* instance_type =
1502 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapInstanceType());
1504 // Non-unique string, check for a string with a hash code that is actually
1506 STATIC_ASSERT(LAST_UNIQUE_NAME_TYPE == FIRST_NONSTRING_TYPE);
1507 IfBuilder not_string_or_name_if(this);
1508 not_string_or_name_if.If<HCompareNumericAndBranch>(
1510 Add<HConstant>(LAST_UNIQUE_NAME_TYPE),
1513 not_string_or_name_if.Then();
1515 // Non-smi, non-Name, non-String: Try to convert to smi in case of
1517 // TODO(danno): This could call some variant of ToString
1518 Push(AddUncasted<HForceRepresentation>(key, Representation::Smi()));
1520 not_string_or_name_if.Else();
1522 // String or Name: check explicitly for Name, they can short-circuit
1523 // directly to unique non-index key path.
1524 IfBuilder not_symbol_if(this);
1525 not_symbol_if.If<HCompareNumericAndBranch>(
1527 Add<HConstant>(SYMBOL_TYPE),
1530 not_symbol_if.Then();
1532 // String: check whether the String is a String of an index. If it is,
1533 // extract the index value from the hash.
1534 HValue* hash = Add<HLoadNamedField>(key, nullptr,
1535 HObjectAccess::ForNameHashField());
1536 HValue* not_index_mask = Add<HConstant>(static_cast<int>(
1537 String::kContainsCachedArrayIndexMask));
1539 HValue* not_index_test = AddUncasted<HBitwise>(
1540 Token::BIT_AND, hash, not_index_mask);
1542 IfBuilder string_index_if(this);
1543 string_index_if.If<HCompareNumericAndBranch>(not_index_test,
1544 graph()->GetConstant0(),
1546 string_index_if.Then();
1548 // String with index in hash: extract string and merge to index path.
1549 Push(BuildDecodeField<String::ArrayIndexValueBits>(hash));
1551 string_index_if.Else();
1553 // Key is a non-index String, check for uniqueness/internalization.
1554 // If it's not internalized yet, internalize it now.
1555 HValue* not_internalized_bit = AddUncasted<HBitwise>(
1558 Add<HConstant>(static_cast<int>(kIsNotInternalizedMask)));
1560 IfBuilder internalized(this);
1561 internalized.If<HCompareNumericAndBranch>(not_internalized_bit,
1562 graph()->GetConstant0(),
1564 internalized.Then();
1567 internalized.Else();
1568 Add<HPushArguments>(key);
1569 HValue* intern_key = Add<HCallRuntime>(
1570 isolate()->factory()->empty_string(),
1571 Runtime::FunctionForId(Runtime::kInternalizeString), 1);
1575 // Key guaranteed to be a unique string
1577 string_index_if.JoinContinuation(join_continuation);
1579 not_symbol_if.Else();
1581 Push(key); // Key is symbol
1583 not_symbol_if.JoinContinuation(join_continuation);
1585 not_string_or_name_if.JoinContinuation(join_continuation);
1587 key_smi_if.JoinContinuation(join_continuation);
1591 void HGraphBuilder::BuildNonGlobalObjectCheck(HValue* receiver) {
1592 // Get the the instance type of the receiver, and make sure that it is
1593 // not one of the global object types.
1595 Add<HLoadNamedField>(receiver, nullptr, HObjectAccess::ForMap());
1596 HValue* instance_type =
1597 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapInstanceType());
1598 STATIC_ASSERT(JS_BUILTINS_OBJECT_TYPE == JS_GLOBAL_OBJECT_TYPE + 1);
1599 HValue* min_global_type = Add<HConstant>(JS_GLOBAL_OBJECT_TYPE);
1600 HValue* max_global_type = Add<HConstant>(JS_BUILTINS_OBJECT_TYPE);
1602 IfBuilder if_global_object(this);
1603 if_global_object.If<HCompareNumericAndBranch>(instance_type,
1606 if_global_object.And();
1607 if_global_object.If<HCompareNumericAndBranch>(instance_type,
1610 if_global_object.ThenDeopt(Deoptimizer::kReceiverWasAGlobalObject);
1611 if_global_object.End();
1615 void HGraphBuilder::BuildTestForDictionaryProperties(
1617 HIfContinuation* continuation) {
1618 HValue* properties = Add<HLoadNamedField>(
1619 object, nullptr, HObjectAccess::ForPropertiesPointer());
1620 HValue* properties_map =
1621 Add<HLoadNamedField>(properties, nullptr, HObjectAccess::ForMap());
1622 HValue* hash_map = Add<HLoadRoot>(Heap::kHashTableMapRootIndex);
1623 IfBuilder builder(this);
1624 builder.If<HCompareObjectEqAndBranch>(properties_map, hash_map);
1625 builder.CaptureContinuation(continuation);
1629 HValue* HGraphBuilder::BuildKeyedLookupCacheHash(HValue* object,
1631 // Load the map of the receiver, compute the keyed lookup cache hash
1632 // based on 32 bits of the map pointer and the string hash.
1633 HValue* object_map =
1634 Add<HLoadNamedField>(object, nullptr, HObjectAccess::ForMapAsInteger32());
1635 HValue* shifted_map = AddUncasted<HShr>(
1636 object_map, Add<HConstant>(KeyedLookupCache::kMapHashShift));
1637 HValue* string_hash =
1638 Add<HLoadNamedField>(key, nullptr, HObjectAccess::ForStringHashField());
1639 HValue* shifted_hash = AddUncasted<HShr>(
1640 string_hash, Add<HConstant>(String::kHashShift));
1641 HValue* xor_result = AddUncasted<HBitwise>(Token::BIT_XOR, shifted_map,
1643 int mask = (KeyedLookupCache::kCapacityMask & KeyedLookupCache::kHashMask);
1644 return AddUncasted<HBitwise>(Token::BIT_AND, xor_result,
1645 Add<HConstant>(mask));
1649 HValue* HGraphBuilder::BuildElementIndexHash(HValue* index) {
1650 int32_t seed_value = static_cast<uint32_t>(isolate()->heap()->HashSeed());
1651 HValue* seed = Add<HConstant>(seed_value);
1652 HValue* hash = AddUncasted<HBitwise>(Token::BIT_XOR, index, seed);
1654 // hash = ~hash + (hash << 15);
1655 HValue* shifted_hash = AddUncasted<HShl>(hash, Add<HConstant>(15));
1656 HValue* not_hash = AddUncasted<HBitwise>(Token::BIT_XOR, hash,
1657 graph()->GetConstantMinus1());
1658 hash = AddUncasted<HAdd>(shifted_hash, not_hash);
1660 // hash = hash ^ (hash >> 12);
1661 shifted_hash = AddUncasted<HShr>(hash, Add<HConstant>(12));
1662 hash = AddUncasted<HBitwise>(Token::BIT_XOR, hash, shifted_hash);
1664 // hash = hash + (hash << 2);
1665 shifted_hash = AddUncasted<HShl>(hash, Add<HConstant>(2));
1666 hash = AddUncasted<HAdd>(hash, shifted_hash);
1668 // hash = hash ^ (hash >> 4);
1669 shifted_hash = AddUncasted<HShr>(hash, Add<HConstant>(4));
1670 hash = AddUncasted<HBitwise>(Token::BIT_XOR, hash, shifted_hash);
1672 // hash = hash * 2057;
1673 hash = AddUncasted<HMul>(hash, Add<HConstant>(2057));
1674 hash->ClearFlag(HValue::kCanOverflow);
1676 // hash = hash ^ (hash >> 16);
1677 shifted_hash = AddUncasted<HShr>(hash, Add<HConstant>(16));
1678 return AddUncasted<HBitwise>(Token::BIT_XOR, hash, shifted_hash);
1682 HValue* HGraphBuilder::BuildUncheckedDictionaryElementLoad(
1683 HValue* receiver, HValue* elements, HValue* key, HValue* hash,
1684 LanguageMode language_mode) {
1686 Add<HLoadKeyed>(elements, Add<HConstant>(NameDictionary::kCapacityIndex),
1687 nullptr, FAST_ELEMENTS);
1689 HValue* mask = AddUncasted<HSub>(capacity, graph()->GetConstant1());
1690 mask->ChangeRepresentation(Representation::Integer32());
1691 mask->ClearFlag(HValue::kCanOverflow);
1693 HValue* entry = hash;
1694 HValue* count = graph()->GetConstant1();
1698 HIfContinuation return_or_loop_continuation(graph()->CreateBasicBlock(),
1699 graph()->CreateBasicBlock());
1700 HIfContinuation found_key_match_continuation(graph()->CreateBasicBlock(),
1701 graph()->CreateBasicBlock());
1702 LoopBuilder probe_loop(this);
1703 probe_loop.BeginBody(2); // Drop entry, count from last environment to
1704 // appease live range building without simulates.
1708 entry = AddUncasted<HBitwise>(Token::BIT_AND, entry, mask);
1709 int entry_size = SeededNumberDictionary::kEntrySize;
1710 HValue* base_index = AddUncasted<HMul>(entry, Add<HConstant>(entry_size));
1711 base_index->ClearFlag(HValue::kCanOverflow);
1712 int start_offset = SeededNumberDictionary::kElementsStartIndex;
1714 AddUncasted<HAdd>(base_index, Add<HConstant>(start_offset));
1715 key_index->ClearFlag(HValue::kCanOverflow);
1717 HValue* candidate_key =
1718 Add<HLoadKeyed>(elements, key_index, nullptr, FAST_ELEMENTS);
1719 IfBuilder if_undefined(this);
1720 if_undefined.If<HCompareObjectEqAndBranch>(candidate_key,
1721 graph()->GetConstantUndefined());
1722 if_undefined.Then();
1724 // element == undefined means "not found". Call the runtime.
1725 // TODO(jkummerow): walk the prototype chain instead.
1726 Add<HPushArguments>(receiver, key);
1727 Push(Add<HCallRuntime>(
1728 isolate()->factory()->empty_string(),
1729 Runtime::FunctionForId(is_strong(language_mode)
1730 ? Runtime::kKeyedGetPropertyStrong
1731 : Runtime::kKeyedGetProperty),
1734 if_undefined.Else();
1736 IfBuilder if_match(this);
1737 if_match.If<HCompareObjectEqAndBranch>(candidate_key, key);
1741 // Update non-internalized string in the dictionary with internalized key?
1742 IfBuilder if_update_with_internalized(this);
1744 if_update_with_internalized.IfNot<HIsSmiAndBranch>(candidate_key);
1745 if_update_with_internalized.And();
1746 HValue* map = AddLoadMap(candidate_key, smi_check);
1747 HValue* instance_type =
1748 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapInstanceType());
1749 HValue* not_internalized_bit = AddUncasted<HBitwise>(
1750 Token::BIT_AND, instance_type,
1751 Add<HConstant>(static_cast<int>(kIsNotInternalizedMask)));
1752 if_update_with_internalized.If<HCompareNumericAndBranch>(
1753 not_internalized_bit, graph()->GetConstant0(), Token::NE);
1754 if_update_with_internalized.And();
1755 if_update_with_internalized.IfNot<HCompareObjectEqAndBranch>(
1756 candidate_key, graph()->GetConstantHole());
1757 if_update_with_internalized.AndIf<HStringCompareAndBranch>(candidate_key,
1759 if_update_with_internalized.Then();
1760 // Replace a key that is a non-internalized string by the equivalent
1761 // internalized string for faster further lookups.
1762 Add<HStoreKeyed>(elements, key_index, key, FAST_ELEMENTS);
1763 if_update_with_internalized.Else();
1765 if_update_with_internalized.JoinContinuation(&found_key_match_continuation);
1766 if_match.JoinContinuation(&found_key_match_continuation);
1768 IfBuilder found_key_match(this, &found_key_match_continuation);
1769 found_key_match.Then();
1770 // Key at current probe matches. Relevant bits in the |details| field must
1771 // be zero, otherwise the dictionary element requires special handling.
1772 HValue* details_index =
1773 AddUncasted<HAdd>(base_index, Add<HConstant>(start_offset + 2));
1774 details_index->ClearFlag(HValue::kCanOverflow);
1776 Add<HLoadKeyed>(elements, details_index, nullptr, FAST_ELEMENTS);
1777 int details_mask = PropertyDetails::TypeField::kMask;
1778 details = AddUncasted<HBitwise>(Token::BIT_AND, details,
1779 Add<HConstant>(details_mask));
1780 IfBuilder details_compare(this);
1781 details_compare.If<HCompareNumericAndBranch>(
1782 details, graph()->GetConstant0(), Token::EQ);
1783 details_compare.Then();
1784 HValue* result_index =
1785 AddUncasted<HAdd>(base_index, Add<HConstant>(start_offset + 1));
1786 result_index->ClearFlag(HValue::kCanOverflow);
1787 Push(Add<HLoadKeyed>(elements, result_index, nullptr, FAST_ELEMENTS));
1788 details_compare.Else();
1789 Add<HPushArguments>(receiver, key);
1790 Push(Add<HCallRuntime>(
1791 isolate()->factory()->empty_string(),
1792 Runtime::FunctionForId(is_strong(language_mode)
1793 ? Runtime::kKeyedGetPropertyStrong
1794 : Runtime::kKeyedGetProperty),
1796 details_compare.End();
1798 found_key_match.Else();
1799 found_key_match.JoinContinuation(&return_or_loop_continuation);
1801 if_undefined.JoinContinuation(&return_or_loop_continuation);
1803 IfBuilder return_or_loop(this, &return_or_loop_continuation);
1804 return_or_loop.Then();
1807 return_or_loop.Else();
1808 entry = AddUncasted<HAdd>(entry, count);
1809 entry->ClearFlag(HValue::kCanOverflow);
1810 count = AddUncasted<HAdd>(count, graph()->GetConstant1());
1811 count->ClearFlag(HValue::kCanOverflow);
1815 probe_loop.EndBody();
1817 return_or_loop.End();
1823 HValue* HGraphBuilder::BuildRegExpConstructResult(HValue* length,
1826 NoObservableSideEffectsScope scope(this);
1827 HConstant* max_length = Add<HConstant>(JSObject::kInitialMaxFastElementArray);
1828 Add<HBoundsCheck>(length, max_length);
1830 // Generate size calculation code here in order to make it dominate
1831 // the JSRegExpResult allocation.
1832 ElementsKind elements_kind = FAST_ELEMENTS;
1833 HValue* size = BuildCalculateElementsSize(elements_kind, length);
1835 // Allocate the JSRegExpResult and the FixedArray in one step.
1836 HValue* result = Add<HAllocate>(
1837 Add<HConstant>(JSRegExpResult::kSize), HType::JSArray(),
1838 NOT_TENURED, JS_ARRAY_TYPE);
1840 // Initialize the JSRegExpResult header.
1841 HValue* global_object = Add<HLoadNamedField>(
1843 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
1844 HValue* native_context = Add<HLoadNamedField>(
1845 global_object, nullptr, HObjectAccess::ForGlobalObjectNativeContext());
1846 Add<HStoreNamedField>(
1847 result, HObjectAccess::ForMap(),
1848 Add<HLoadNamedField>(
1849 native_context, nullptr,
1850 HObjectAccess::ForContextSlot(Context::REGEXP_RESULT_MAP_INDEX)));
1851 HConstant* empty_fixed_array =
1852 Add<HConstant>(isolate()->factory()->empty_fixed_array());
1853 Add<HStoreNamedField>(
1854 result, HObjectAccess::ForJSArrayOffset(JSArray::kPropertiesOffset),
1856 Add<HStoreNamedField>(
1857 result, HObjectAccess::ForJSArrayOffset(JSArray::kElementsOffset),
1859 Add<HStoreNamedField>(
1860 result, HObjectAccess::ForJSArrayOffset(JSArray::kLengthOffset), length);
1862 // Initialize the additional fields.
1863 Add<HStoreNamedField>(
1864 result, HObjectAccess::ForJSArrayOffset(JSRegExpResult::kIndexOffset),
1866 Add<HStoreNamedField>(
1867 result, HObjectAccess::ForJSArrayOffset(JSRegExpResult::kInputOffset),
1870 // Allocate and initialize the elements header.
1871 HAllocate* elements = BuildAllocateElements(elements_kind, size);
1872 BuildInitializeElementsHeader(elements, elements_kind, length);
1874 if (!elements->has_size_upper_bound()) {
1875 HConstant* size_in_bytes_upper_bound = EstablishElementsAllocationSize(
1876 elements_kind, max_length->Integer32Value());
1877 elements->set_size_upper_bound(size_in_bytes_upper_bound);
1880 Add<HStoreNamedField>(
1881 result, HObjectAccess::ForJSArrayOffset(JSArray::kElementsOffset),
1884 // Initialize the elements contents with undefined.
1885 BuildFillElementsWithValue(
1886 elements, elements_kind, graph()->GetConstant0(), length,
1887 graph()->GetConstantUndefined());
1893 HValue* HGraphBuilder::BuildNumberToString(HValue* object, Type* type) {
1894 NoObservableSideEffectsScope scope(this);
1896 // Convert constant numbers at compile time.
1897 if (object->IsConstant() && HConstant::cast(object)->HasNumberValue()) {
1898 Handle<Object> number = HConstant::cast(object)->handle(isolate());
1899 Handle<String> result = isolate()->factory()->NumberToString(number);
1900 return Add<HConstant>(result);
1903 // Create a joinable continuation.
1904 HIfContinuation found(graph()->CreateBasicBlock(),
1905 graph()->CreateBasicBlock());
1907 // Load the number string cache.
1908 HValue* number_string_cache =
1909 Add<HLoadRoot>(Heap::kNumberStringCacheRootIndex);
1911 // Make the hash mask from the length of the number string cache. It
1912 // contains two elements (number and string) for each cache entry.
1913 HValue* mask = AddLoadFixedArrayLength(number_string_cache);
1914 mask->set_type(HType::Smi());
1915 mask = AddUncasted<HSar>(mask, graph()->GetConstant1());
1916 mask = AddUncasted<HSub>(mask, graph()->GetConstant1());
1918 // Check whether object is a smi.
1919 IfBuilder if_objectissmi(this);
1920 if_objectissmi.If<HIsSmiAndBranch>(object);
1921 if_objectissmi.Then();
1923 // Compute hash for smi similar to smi_get_hash().
1924 HValue* hash = AddUncasted<HBitwise>(Token::BIT_AND, object, mask);
1927 HValue* key_index = AddUncasted<HShl>(hash, graph()->GetConstant1());
1928 HValue* key = Add<HLoadKeyed>(number_string_cache, key_index, nullptr,
1929 FAST_ELEMENTS, ALLOW_RETURN_HOLE);
1931 // Check if object == key.
1932 IfBuilder if_objectiskey(this);
1933 if_objectiskey.If<HCompareObjectEqAndBranch>(object, key);
1934 if_objectiskey.Then();
1936 // Make the key_index available.
1939 if_objectiskey.JoinContinuation(&found);
1941 if_objectissmi.Else();
1943 if (type->Is(Type::SignedSmall())) {
1944 if_objectissmi.Deopt(Deoptimizer::kExpectedSmi);
1946 // Check if the object is a heap number.
1947 IfBuilder if_objectisnumber(this);
1948 HValue* objectisnumber = if_objectisnumber.If<HCompareMap>(
1949 object, isolate()->factory()->heap_number_map());
1950 if_objectisnumber.Then();
1952 // Compute hash for heap number similar to double_get_hash().
1953 HValue* low = Add<HLoadNamedField>(
1954 object, objectisnumber,
1955 HObjectAccess::ForHeapNumberValueLowestBits());
1956 HValue* high = Add<HLoadNamedField>(
1957 object, objectisnumber,
1958 HObjectAccess::ForHeapNumberValueHighestBits());
1959 HValue* hash = AddUncasted<HBitwise>(Token::BIT_XOR, low, high);
1960 hash = AddUncasted<HBitwise>(Token::BIT_AND, hash, mask);
1963 HValue* key_index = AddUncasted<HShl>(hash, graph()->GetConstant1());
1964 HValue* key = Add<HLoadKeyed>(number_string_cache, key_index, nullptr,
1965 FAST_ELEMENTS, ALLOW_RETURN_HOLE);
1967 // Check if the key is a heap number and compare it with the object.
1968 IfBuilder if_keyisnotsmi(this);
1969 HValue* keyisnotsmi = if_keyisnotsmi.IfNot<HIsSmiAndBranch>(key);
1970 if_keyisnotsmi.Then();
1972 IfBuilder if_keyisheapnumber(this);
1973 if_keyisheapnumber.If<HCompareMap>(
1974 key, isolate()->factory()->heap_number_map());
1975 if_keyisheapnumber.Then();
1977 // Check if values of key and object match.
1978 IfBuilder if_keyeqobject(this);
1979 if_keyeqobject.If<HCompareNumericAndBranch>(
1980 Add<HLoadNamedField>(key, keyisnotsmi,
1981 HObjectAccess::ForHeapNumberValue()),
1982 Add<HLoadNamedField>(object, objectisnumber,
1983 HObjectAccess::ForHeapNumberValue()),
1985 if_keyeqobject.Then();
1987 // Make the key_index available.
1990 if_keyeqobject.JoinContinuation(&found);
1992 if_keyisheapnumber.JoinContinuation(&found);
1994 if_keyisnotsmi.JoinContinuation(&found);
1996 if_objectisnumber.Else();
1998 if (type->Is(Type::Number())) {
1999 if_objectisnumber.Deopt(Deoptimizer::kExpectedHeapNumber);
2002 if_objectisnumber.JoinContinuation(&found);
2005 if_objectissmi.JoinContinuation(&found);
2007 // Check for cache hit.
2008 IfBuilder if_found(this, &found);
2011 // Count number to string operation in native code.
2012 AddIncrementCounter(isolate()->counters()->number_to_string_native());
2014 // Load the value in case of cache hit.
2015 HValue* key_index = Pop();
2016 HValue* value_index = AddUncasted<HAdd>(key_index, graph()->GetConstant1());
2017 Push(Add<HLoadKeyed>(number_string_cache, value_index, nullptr,
2018 FAST_ELEMENTS, ALLOW_RETURN_HOLE));
2022 // Cache miss, fallback to runtime.
2023 Add<HPushArguments>(object);
2024 Push(Add<HCallRuntime>(
2025 isolate()->factory()->empty_string(),
2026 Runtime::FunctionForId(Runtime::kNumberToStringSkipCache),
2035 HAllocate* HGraphBuilder::BuildAllocate(
2036 HValue* object_size,
2038 InstanceType instance_type,
2039 HAllocationMode allocation_mode) {
2040 // Compute the effective allocation size.
2041 HValue* size = object_size;
2042 if (allocation_mode.CreateAllocationMementos()) {
2043 size = AddUncasted<HAdd>(size, Add<HConstant>(AllocationMemento::kSize));
2044 size->ClearFlag(HValue::kCanOverflow);
2047 // Perform the actual allocation.
2048 HAllocate* object = Add<HAllocate>(
2049 size, type, allocation_mode.GetPretenureMode(),
2050 instance_type, allocation_mode.feedback_site());
2052 // Setup the allocation memento.
2053 if (allocation_mode.CreateAllocationMementos()) {
2054 BuildCreateAllocationMemento(
2055 object, object_size, allocation_mode.current_site());
2062 HValue* HGraphBuilder::BuildAddStringLengths(HValue* left_length,
2063 HValue* right_length) {
2064 // Compute the combined string length and check against max string length.
2065 HValue* length = AddUncasted<HAdd>(left_length, right_length);
2066 // Check that length <= kMaxLength <=> length < MaxLength + 1.
2067 HValue* max_length = Add<HConstant>(String::kMaxLength + 1);
2068 Add<HBoundsCheck>(length, max_length);
2073 HValue* HGraphBuilder::BuildCreateConsString(
2077 HAllocationMode allocation_mode) {
2078 // Determine the string instance types.
2079 HInstruction* left_instance_type = AddLoadStringInstanceType(left);
2080 HInstruction* right_instance_type = AddLoadStringInstanceType(right);
2082 // Allocate the cons string object. HAllocate does not care whether we
2083 // pass CONS_STRING_TYPE or CONS_ONE_BYTE_STRING_TYPE here, so we just use
2084 // CONS_STRING_TYPE here. Below we decide whether the cons string is
2085 // one-byte or two-byte and set the appropriate map.
2086 DCHECK(HAllocate::CompatibleInstanceTypes(CONS_STRING_TYPE,
2087 CONS_ONE_BYTE_STRING_TYPE));
2088 HAllocate* result = BuildAllocate(Add<HConstant>(ConsString::kSize),
2089 HType::String(), CONS_STRING_TYPE,
2092 // Compute intersection and difference of instance types.
2093 HValue* anded_instance_types = AddUncasted<HBitwise>(
2094 Token::BIT_AND, left_instance_type, right_instance_type);
2095 HValue* xored_instance_types = AddUncasted<HBitwise>(
2096 Token::BIT_XOR, left_instance_type, right_instance_type);
2098 // We create a one-byte cons string if
2099 // 1. both strings are one-byte, or
2100 // 2. at least one of the strings is two-byte, but happens to contain only
2101 // one-byte characters.
2102 // To do this, we check
2103 // 1. if both strings are one-byte, or if the one-byte data hint is set in
2105 // 2. if one of the strings has the one-byte data hint set and the other
2106 // string is one-byte.
2107 IfBuilder if_onebyte(this);
2108 STATIC_ASSERT(kOneByteStringTag != 0);
2109 STATIC_ASSERT(kOneByteDataHintMask != 0);
2110 if_onebyte.If<HCompareNumericAndBranch>(
2111 AddUncasted<HBitwise>(
2112 Token::BIT_AND, anded_instance_types,
2113 Add<HConstant>(static_cast<int32_t>(
2114 kStringEncodingMask | kOneByteDataHintMask))),
2115 graph()->GetConstant0(), Token::NE);
2117 STATIC_ASSERT(kOneByteStringTag != 0 &&
2118 kOneByteDataHintTag != 0 &&
2119 kOneByteDataHintTag != kOneByteStringTag);
2120 if_onebyte.If<HCompareNumericAndBranch>(
2121 AddUncasted<HBitwise>(
2122 Token::BIT_AND, xored_instance_types,
2123 Add<HConstant>(static_cast<int32_t>(
2124 kOneByteStringTag | kOneByteDataHintTag))),
2125 Add<HConstant>(static_cast<int32_t>(
2126 kOneByteStringTag | kOneByteDataHintTag)), Token::EQ);
2129 // We can safely skip the write barrier for storing the map here.
2130 Add<HStoreNamedField>(
2131 result, HObjectAccess::ForMap(),
2132 Add<HConstant>(isolate()->factory()->cons_one_byte_string_map()));
2136 // We can safely skip the write barrier for storing the map here.
2137 Add<HStoreNamedField>(
2138 result, HObjectAccess::ForMap(),
2139 Add<HConstant>(isolate()->factory()->cons_string_map()));
2143 // Initialize the cons string fields.
2144 Add<HStoreNamedField>(result, HObjectAccess::ForStringHashField(),
2145 Add<HConstant>(String::kEmptyHashField));
2146 Add<HStoreNamedField>(result, HObjectAccess::ForStringLength(), length);
2147 Add<HStoreNamedField>(result, HObjectAccess::ForConsStringFirst(), left);
2148 Add<HStoreNamedField>(result, HObjectAccess::ForConsStringSecond(), right);
2150 // Count the native string addition.
2151 AddIncrementCounter(isolate()->counters()->string_add_native());
2157 void HGraphBuilder::BuildCopySeqStringChars(HValue* src,
2159 String::Encoding src_encoding,
2162 String::Encoding dst_encoding,
2164 DCHECK(dst_encoding != String::ONE_BYTE_ENCODING ||
2165 src_encoding == String::ONE_BYTE_ENCODING);
2166 LoopBuilder loop(this, context(), LoopBuilder::kPostIncrement);
2167 HValue* index = loop.BeginBody(graph()->GetConstant0(), length, Token::LT);
2169 HValue* src_index = AddUncasted<HAdd>(src_offset, index);
2171 AddUncasted<HSeqStringGetChar>(src_encoding, src, src_index);
2172 HValue* dst_index = AddUncasted<HAdd>(dst_offset, index);
2173 Add<HSeqStringSetChar>(dst_encoding, dst, dst_index, value);
2179 HValue* HGraphBuilder::BuildObjectSizeAlignment(
2180 HValue* unaligned_size, int header_size) {
2181 DCHECK((header_size & kObjectAlignmentMask) == 0);
2182 HValue* size = AddUncasted<HAdd>(
2183 unaligned_size, Add<HConstant>(static_cast<int32_t>(
2184 header_size + kObjectAlignmentMask)));
2185 size->ClearFlag(HValue::kCanOverflow);
2186 return AddUncasted<HBitwise>(
2187 Token::BIT_AND, size, Add<HConstant>(static_cast<int32_t>(
2188 ~kObjectAlignmentMask)));
2192 HValue* HGraphBuilder::BuildUncheckedStringAdd(
2195 HAllocationMode allocation_mode) {
2196 // Determine the string lengths.
2197 HValue* left_length = AddLoadStringLength(left);
2198 HValue* right_length = AddLoadStringLength(right);
2200 // Compute the combined string length.
2201 HValue* length = BuildAddStringLengths(left_length, right_length);
2203 // Do some manual constant folding here.
2204 if (left_length->IsConstant()) {
2205 HConstant* c_left_length = HConstant::cast(left_length);
2206 DCHECK_NE(0, c_left_length->Integer32Value());
2207 if (c_left_length->Integer32Value() + 1 >= ConsString::kMinLength) {
2208 // The right string contains at least one character.
2209 return BuildCreateConsString(length, left, right, allocation_mode);
2211 } else if (right_length->IsConstant()) {
2212 HConstant* c_right_length = HConstant::cast(right_length);
2213 DCHECK_NE(0, c_right_length->Integer32Value());
2214 if (c_right_length->Integer32Value() + 1 >= ConsString::kMinLength) {
2215 // The left string contains at least one character.
2216 return BuildCreateConsString(length, left, right, allocation_mode);
2220 // Check if we should create a cons string.
2221 IfBuilder if_createcons(this);
2222 if_createcons.If<HCompareNumericAndBranch>(
2223 length, Add<HConstant>(ConsString::kMinLength), Token::GTE);
2224 if_createcons.Then();
2226 // Create a cons string.
2227 Push(BuildCreateConsString(length, left, right, allocation_mode));
2229 if_createcons.Else();
2231 // Determine the string instance types.
2232 HValue* left_instance_type = AddLoadStringInstanceType(left);
2233 HValue* right_instance_type = AddLoadStringInstanceType(right);
2235 // Compute union and difference of instance types.
2236 HValue* ored_instance_types = AddUncasted<HBitwise>(
2237 Token::BIT_OR, left_instance_type, right_instance_type);
2238 HValue* xored_instance_types = AddUncasted<HBitwise>(
2239 Token::BIT_XOR, left_instance_type, right_instance_type);
2241 // Check if both strings have the same encoding and both are
2243 IfBuilder if_sameencodingandsequential(this);
2244 if_sameencodingandsequential.If<HCompareNumericAndBranch>(
2245 AddUncasted<HBitwise>(
2246 Token::BIT_AND, xored_instance_types,
2247 Add<HConstant>(static_cast<int32_t>(kStringEncodingMask))),
2248 graph()->GetConstant0(), Token::EQ);
2249 if_sameencodingandsequential.And();
2250 STATIC_ASSERT(kSeqStringTag == 0);
2251 if_sameencodingandsequential.If<HCompareNumericAndBranch>(
2252 AddUncasted<HBitwise>(
2253 Token::BIT_AND, ored_instance_types,
2254 Add<HConstant>(static_cast<int32_t>(kStringRepresentationMask))),
2255 graph()->GetConstant0(), Token::EQ);
2256 if_sameencodingandsequential.Then();
2258 HConstant* string_map =
2259 Add<HConstant>(isolate()->factory()->string_map());
2260 HConstant* one_byte_string_map =
2261 Add<HConstant>(isolate()->factory()->one_byte_string_map());
2263 // Determine map and size depending on whether result is one-byte string.
2264 IfBuilder if_onebyte(this);
2265 STATIC_ASSERT(kOneByteStringTag != 0);
2266 if_onebyte.If<HCompareNumericAndBranch>(
2267 AddUncasted<HBitwise>(
2268 Token::BIT_AND, ored_instance_types,
2269 Add<HConstant>(static_cast<int32_t>(kStringEncodingMask))),
2270 graph()->GetConstant0(), Token::NE);
2273 // Allocate sequential one-byte string object.
2275 Push(one_byte_string_map);
2279 // Allocate sequential two-byte string object.
2280 HValue* size = AddUncasted<HShl>(length, graph()->GetConstant1());
2281 size->ClearFlag(HValue::kCanOverflow);
2282 size->SetFlag(HValue::kUint32);
2287 HValue* map = Pop();
2289 // Calculate the number of bytes needed for the characters in the
2290 // string while observing object alignment.
2291 STATIC_ASSERT((SeqString::kHeaderSize & kObjectAlignmentMask) == 0);
2292 HValue* size = BuildObjectSizeAlignment(Pop(), SeqString::kHeaderSize);
2294 // Allocate the string object. HAllocate does not care whether we pass
2295 // STRING_TYPE or ONE_BYTE_STRING_TYPE here, so we just use STRING_TYPE.
2296 HAllocate* result = BuildAllocate(
2297 size, HType::String(), STRING_TYPE, allocation_mode);
2298 Add<HStoreNamedField>(result, HObjectAccess::ForMap(), map);
2300 // Initialize the string fields.
2301 Add<HStoreNamedField>(result, HObjectAccess::ForStringHashField(),
2302 Add<HConstant>(String::kEmptyHashField));
2303 Add<HStoreNamedField>(result, HObjectAccess::ForStringLength(), length);
2305 // Copy characters to the result string.
2306 IfBuilder if_twobyte(this);
2307 if_twobyte.If<HCompareObjectEqAndBranch>(map, string_map);
2310 // Copy characters from the left string.
2311 BuildCopySeqStringChars(
2312 left, graph()->GetConstant0(), String::TWO_BYTE_ENCODING,
2313 result, graph()->GetConstant0(), String::TWO_BYTE_ENCODING,
2316 // Copy characters from the right string.
2317 BuildCopySeqStringChars(
2318 right, graph()->GetConstant0(), String::TWO_BYTE_ENCODING,
2319 result, left_length, String::TWO_BYTE_ENCODING,
2324 // Copy characters from the left string.
2325 BuildCopySeqStringChars(
2326 left, graph()->GetConstant0(), String::ONE_BYTE_ENCODING,
2327 result, graph()->GetConstant0(), String::ONE_BYTE_ENCODING,
2330 // Copy characters from the right string.
2331 BuildCopySeqStringChars(
2332 right, graph()->GetConstant0(), String::ONE_BYTE_ENCODING,
2333 result, left_length, String::ONE_BYTE_ENCODING,
2338 // Count the native string addition.
2339 AddIncrementCounter(isolate()->counters()->string_add_native());
2341 // Return the sequential string.
2344 if_sameencodingandsequential.Else();
2346 // Fallback to the runtime to add the two strings.
2347 Add<HPushArguments>(left, right);
2348 Push(Add<HCallRuntime>(isolate()->factory()->empty_string(),
2349 Runtime::FunctionForId(Runtime::kStringAddRT), 2));
2351 if_sameencodingandsequential.End();
2353 if_createcons.End();
2359 HValue* HGraphBuilder::BuildStringAdd(
2362 HAllocationMode allocation_mode) {
2363 NoObservableSideEffectsScope no_effects(this);
2365 // Determine string lengths.
2366 HValue* left_length = AddLoadStringLength(left);
2367 HValue* right_length = AddLoadStringLength(right);
2369 // Check if left string is empty.
2370 IfBuilder if_leftempty(this);
2371 if_leftempty.If<HCompareNumericAndBranch>(
2372 left_length, graph()->GetConstant0(), Token::EQ);
2373 if_leftempty.Then();
2375 // Count the native string addition.
2376 AddIncrementCounter(isolate()->counters()->string_add_native());
2378 // Just return the right string.
2381 if_leftempty.Else();
2383 // Check if right string is empty.
2384 IfBuilder if_rightempty(this);
2385 if_rightempty.If<HCompareNumericAndBranch>(
2386 right_length, graph()->GetConstant0(), Token::EQ);
2387 if_rightempty.Then();
2389 // Count the native string addition.
2390 AddIncrementCounter(isolate()->counters()->string_add_native());
2392 // Just return the left string.
2395 if_rightempty.Else();
2397 // Add the two non-empty strings.
2398 Push(BuildUncheckedStringAdd(left, right, allocation_mode));
2400 if_rightempty.End();
2408 HInstruction* HGraphBuilder::BuildUncheckedMonomorphicElementAccess(
2409 HValue* checked_object,
2413 ElementsKind elements_kind,
2414 PropertyAccessType access_type,
2415 LoadKeyedHoleMode load_mode,
2416 KeyedAccessStoreMode store_mode) {
2417 DCHECK(top_info()->IsStub() || checked_object->IsCompareMap() ||
2418 checked_object->IsCheckMaps());
2419 DCHECK((!IsExternalArrayElementsKind(elements_kind) &&
2420 !IsFixedTypedArrayElementsKind(elements_kind)) ||
2422 // No GVNFlag is necessary for ElementsKind if there is an explicit dependency
2423 // on a HElementsTransition instruction. The flag can also be removed if the
2424 // map to check has FAST_HOLEY_ELEMENTS, since there can be no further
2425 // ElementsKind transitions. Finally, the dependency can be removed for stores
2426 // for FAST_ELEMENTS, since a transition to HOLEY elements won't change the
2427 // generated store code.
2428 if ((elements_kind == FAST_HOLEY_ELEMENTS) ||
2429 (elements_kind == FAST_ELEMENTS && access_type == STORE)) {
2430 checked_object->ClearDependsOnFlag(kElementsKind);
2433 bool fast_smi_only_elements = IsFastSmiElementsKind(elements_kind);
2434 bool fast_elements = IsFastObjectElementsKind(elements_kind);
2435 HValue* elements = AddLoadElements(checked_object);
2436 if (access_type == STORE && (fast_elements || fast_smi_only_elements) &&
2437 store_mode != STORE_NO_TRANSITION_HANDLE_COW) {
2438 HCheckMaps* check_cow_map = Add<HCheckMaps>(
2439 elements, isolate()->factory()->fixed_array_map());
2440 check_cow_map->ClearDependsOnFlag(kElementsKind);
2442 HInstruction* length = NULL;
2444 length = Add<HLoadNamedField>(
2445 checked_object->ActualValue(), checked_object,
2446 HObjectAccess::ForArrayLength(elements_kind));
2448 length = AddLoadFixedArrayLength(elements);
2450 length->set_type(HType::Smi());
2451 HValue* checked_key = NULL;
2452 if (IsExternalArrayElementsKind(elements_kind) ||
2453 IsFixedTypedArrayElementsKind(elements_kind)) {
2454 checked_object = Add<HCheckArrayBufferNotNeutered>(checked_object);
2456 HValue* backing_store;
2457 if (IsExternalArrayElementsKind(elements_kind)) {
2458 backing_store = Add<HLoadNamedField>(
2459 elements, nullptr, HObjectAccess::ForExternalArrayExternalPointer());
2461 HValue* external_pointer = Add<HLoadNamedField>(
2463 HObjectAccess::ForFixedTypedArrayBaseExternalPointer());
2464 HValue* base_pointer = Add<HLoadNamedField>(
2466 HObjectAccess::ForFixedTypedArrayBaseBasePointer());
2467 backing_store = AddUncasted<HAdd>(external_pointer, base_pointer,
2468 Strength::WEAK, AddOfExternalAndTagged);
2470 if (store_mode == STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS) {
2471 NoObservableSideEffectsScope no_effects(this);
2472 IfBuilder length_checker(this);
2473 length_checker.If<HCompareNumericAndBranch>(key, length, Token::LT);
2474 length_checker.Then();
2475 IfBuilder negative_checker(this);
2476 HValue* bounds_check = negative_checker.If<HCompareNumericAndBranch>(
2477 key, graph()->GetConstant0(), Token::GTE);
2478 negative_checker.Then();
2479 HInstruction* result = AddElementAccess(
2480 backing_store, key, val, bounds_check, elements_kind, access_type);
2481 negative_checker.ElseDeopt(Deoptimizer::kNegativeKeyEncountered);
2482 negative_checker.End();
2483 length_checker.End();
2486 DCHECK(store_mode == STANDARD_STORE);
2487 checked_key = Add<HBoundsCheck>(key, length);
2488 return AddElementAccess(
2489 backing_store, checked_key, val,
2490 checked_object, elements_kind, access_type);
2493 DCHECK(fast_smi_only_elements ||
2495 IsFastDoubleElementsKind(elements_kind));
2497 // In case val is stored into a fast smi array, assure that the value is a smi
2498 // before manipulating the backing store. Otherwise the actual store may
2499 // deopt, leaving the backing store in an invalid state.
2500 if (access_type == STORE && IsFastSmiElementsKind(elements_kind) &&
2501 !val->type().IsSmi()) {
2502 val = AddUncasted<HForceRepresentation>(val, Representation::Smi());
2505 if (IsGrowStoreMode(store_mode)) {
2506 NoObservableSideEffectsScope no_effects(this);
2507 Representation representation = HStoreKeyed::RequiredValueRepresentation(
2508 elements_kind, STORE_TO_INITIALIZED_ENTRY);
2509 val = AddUncasted<HForceRepresentation>(val, representation);
2510 elements = BuildCheckForCapacityGrow(checked_object, elements,
2511 elements_kind, length, key,
2512 is_js_array, access_type);
2515 checked_key = Add<HBoundsCheck>(key, length);
2517 if (access_type == STORE && (fast_elements || fast_smi_only_elements)) {
2518 if (store_mode == STORE_NO_TRANSITION_HANDLE_COW) {
2519 NoObservableSideEffectsScope no_effects(this);
2520 elements = BuildCopyElementsOnWrite(checked_object, elements,
2521 elements_kind, length);
2523 HCheckMaps* check_cow_map = Add<HCheckMaps>(
2524 elements, isolate()->factory()->fixed_array_map());
2525 check_cow_map->ClearDependsOnFlag(kElementsKind);
2529 return AddElementAccess(elements, checked_key, val, checked_object,
2530 elements_kind, access_type, load_mode);
2534 HValue* HGraphBuilder::BuildAllocateArrayFromLength(
2535 JSArrayBuilder* array_builder,
2536 HValue* length_argument) {
2537 if (length_argument->IsConstant() &&
2538 HConstant::cast(length_argument)->HasSmiValue()) {
2539 int array_length = HConstant::cast(length_argument)->Integer32Value();
2540 if (array_length == 0) {
2541 return array_builder->AllocateEmptyArray();
2543 return array_builder->AllocateArray(length_argument,
2549 HValue* constant_zero = graph()->GetConstant0();
2550 HConstant* max_alloc_length =
2551 Add<HConstant>(JSObject::kInitialMaxFastElementArray);
2552 HInstruction* checked_length = Add<HBoundsCheck>(length_argument,
2554 IfBuilder if_builder(this);
2555 if_builder.If<HCompareNumericAndBranch>(checked_length, constant_zero,
2558 const int initial_capacity = JSArray::kPreallocatedArrayElements;
2559 HConstant* initial_capacity_node = Add<HConstant>(initial_capacity);
2560 Push(initial_capacity_node); // capacity
2561 Push(constant_zero); // length
2563 if (!(top_info()->IsStub()) &&
2564 IsFastPackedElementsKind(array_builder->kind())) {
2565 // We'll come back later with better (holey) feedback.
2567 Deoptimizer::kHoleyArrayDespitePackedElements_kindFeedback);
2569 Push(checked_length); // capacity
2570 Push(checked_length); // length
2574 // Figure out total size
2575 HValue* length = Pop();
2576 HValue* capacity = Pop();
2577 return array_builder->AllocateArray(capacity, max_alloc_length, length);
2581 HValue* HGraphBuilder::BuildCalculateElementsSize(ElementsKind kind,
2583 int elements_size = IsFastDoubleElementsKind(kind)
2587 HConstant* elements_size_value = Add<HConstant>(elements_size);
2589 HMul::NewImul(isolate(), zone(), context(), capacity->ActualValue(),
2590 elements_size_value);
2591 AddInstruction(mul);
2592 mul->ClearFlag(HValue::kCanOverflow);
2594 STATIC_ASSERT(FixedDoubleArray::kHeaderSize == FixedArray::kHeaderSize);
2596 HConstant* header_size = Add<HConstant>(FixedArray::kHeaderSize);
2597 HValue* total_size = AddUncasted<HAdd>(mul, header_size);
2598 total_size->ClearFlag(HValue::kCanOverflow);
2603 HAllocate* HGraphBuilder::AllocateJSArrayObject(AllocationSiteMode mode) {
2604 int base_size = JSArray::kSize;
2605 if (mode == TRACK_ALLOCATION_SITE) {
2606 base_size += AllocationMemento::kSize;
2608 HConstant* size_in_bytes = Add<HConstant>(base_size);
2609 return Add<HAllocate>(
2610 size_in_bytes, HType::JSArray(), NOT_TENURED, JS_OBJECT_TYPE);
2614 HConstant* HGraphBuilder::EstablishElementsAllocationSize(
2617 int base_size = IsFastDoubleElementsKind(kind)
2618 ? FixedDoubleArray::SizeFor(capacity)
2619 : FixedArray::SizeFor(capacity);
2621 return Add<HConstant>(base_size);
2625 HAllocate* HGraphBuilder::BuildAllocateElements(ElementsKind kind,
2626 HValue* size_in_bytes) {
2627 InstanceType instance_type = IsFastDoubleElementsKind(kind)
2628 ? FIXED_DOUBLE_ARRAY_TYPE
2631 return Add<HAllocate>(size_in_bytes, HType::HeapObject(), NOT_TENURED,
2636 void HGraphBuilder::BuildInitializeElementsHeader(HValue* elements,
2639 Factory* factory = isolate()->factory();
2640 Handle<Map> map = IsFastDoubleElementsKind(kind)
2641 ? factory->fixed_double_array_map()
2642 : factory->fixed_array_map();
2644 Add<HStoreNamedField>(elements, HObjectAccess::ForMap(), Add<HConstant>(map));
2645 Add<HStoreNamedField>(elements, HObjectAccess::ForFixedArrayLength(),
2650 HValue* HGraphBuilder::BuildAllocateAndInitializeArray(ElementsKind kind,
2652 // The HForceRepresentation is to prevent possible deopt on int-smi
2653 // conversion after allocation but before the new object fields are set.
2654 capacity = AddUncasted<HForceRepresentation>(capacity, Representation::Smi());
2655 HValue* size_in_bytes = BuildCalculateElementsSize(kind, capacity);
2656 HValue* new_array = BuildAllocateElements(kind, size_in_bytes);
2657 BuildInitializeElementsHeader(new_array, kind, capacity);
2662 void HGraphBuilder::BuildJSArrayHeader(HValue* array,
2665 AllocationSiteMode mode,
2666 ElementsKind elements_kind,
2667 HValue* allocation_site_payload,
2668 HValue* length_field) {
2669 Add<HStoreNamedField>(array, HObjectAccess::ForMap(), array_map);
2671 HConstant* empty_fixed_array =
2672 Add<HConstant>(isolate()->factory()->empty_fixed_array());
2674 Add<HStoreNamedField>(
2675 array, HObjectAccess::ForPropertiesPointer(), empty_fixed_array);
2677 Add<HStoreNamedField>(
2678 array, HObjectAccess::ForElementsPointer(),
2679 elements != NULL ? elements : empty_fixed_array);
2681 Add<HStoreNamedField>(
2682 array, HObjectAccess::ForArrayLength(elements_kind), length_field);
2684 if (mode == TRACK_ALLOCATION_SITE) {
2685 BuildCreateAllocationMemento(
2686 array, Add<HConstant>(JSArray::kSize), allocation_site_payload);
2691 HInstruction* HGraphBuilder::AddElementAccess(
2693 HValue* checked_key,
2696 ElementsKind elements_kind,
2697 PropertyAccessType access_type,
2698 LoadKeyedHoleMode load_mode) {
2699 if (access_type == STORE) {
2700 DCHECK(val != NULL);
2701 if (elements_kind == EXTERNAL_UINT8_CLAMPED_ELEMENTS ||
2702 elements_kind == UINT8_CLAMPED_ELEMENTS) {
2703 val = Add<HClampToUint8>(val);
2705 return Add<HStoreKeyed>(elements, checked_key, val, elements_kind,
2706 STORE_TO_INITIALIZED_ENTRY);
2709 DCHECK(access_type == LOAD);
2710 DCHECK(val == NULL);
2711 HLoadKeyed* load = Add<HLoadKeyed>(
2712 elements, checked_key, dependency, elements_kind, load_mode);
2713 if (elements_kind == EXTERNAL_UINT32_ELEMENTS ||
2714 elements_kind == UINT32_ELEMENTS) {
2715 graph()->RecordUint32Instruction(load);
2721 HLoadNamedField* HGraphBuilder::AddLoadMap(HValue* object,
2722 HValue* dependency) {
2723 return Add<HLoadNamedField>(object, dependency, HObjectAccess::ForMap());
2727 HLoadNamedField* HGraphBuilder::AddLoadElements(HValue* object,
2728 HValue* dependency) {
2729 return Add<HLoadNamedField>(
2730 object, dependency, HObjectAccess::ForElementsPointer());
2734 HLoadNamedField* HGraphBuilder::AddLoadFixedArrayLength(
2736 HValue* dependency) {
2737 return Add<HLoadNamedField>(
2738 array, dependency, HObjectAccess::ForFixedArrayLength());
2742 HLoadNamedField* HGraphBuilder::AddLoadArrayLength(HValue* array,
2744 HValue* dependency) {
2745 return Add<HLoadNamedField>(
2746 array, dependency, HObjectAccess::ForArrayLength(kind));
2750 HValue* HGraphBuilder::BuildNewElementsCapacity(HValue* old_capacity) {
2751 HValue* half_old_capacity = AddUncasted<HShr>(old_capacity,
2752 graph_->GetConstant1());
2754 HValue* new_capacity = AddUncasted<HAdd>(half_old_capacity, old_capacity);
2755 new_capacity->ClearFlag(HValue::kCanOverflow);
2757 HValue* min_growth = Add<HConstant>(16);
2759 new_capacity = AddUncasted<HAdd>(new_capacity, min_growth);
2760 new_capacity->ClearFlag(HValue::kCanOverflow);
2762 return new_capacity;
2766 HValue* HGraphBuilder::BuildGrowElementsCapacity(HValue* object,
2769 ElementsKind new_kind,
2771 HValue* new_capacity) {
2772 Add<HBoundsCheck>(new_capacity, Add<HConstant>(
2773 (Page::kMaxRegularHeapObjectSize - FixedArray::kHeaderSize) >>
2774 ElementsKindToShiftSize(new_kind)));
2776 HValue* new_elements =
2777 BuildAllocateAndInitializeArray(new_kind, new_capacity);
2779 BuildCopyElements(elements, kind, new_elements,
2780 new_kind, length, new_capacity);
2782 Add<HStoreNamedField>(object, HObjectAccess::ForElementsPointer(),
2785 return new_elements;
2789 void HGraphBuilder::BuildFillElementsWithValue(HValue* elements,
2790 ElementsKind elements_kind,
2795 to = AddLoadFixedArrayLength(elements);
2798 // Special loop unfolding case
2799 STATIC_ASSERT(JSArray::kPreallocatedArrayElements <=
2800 kElementLoopUnrollThreshold);
2801 int initial_capacity = -1;
2802 if (from->IsInteger32Constant() && to->IsInteger32Constant()) {
2803 int constant_from = from->GetInteger32Constant();
2804 int constant_to = to->GetInteger32Constant();
2806 if (constant_from == 0 && constant_to <= kElementLoopUnrollThreshold) {
2807 initial_capacity = constant_to;
2811 if (initial_capacity >= 0) {
2812 for (int i = 0; i < initial_capacity; i++) {
2813 HInstruction* key = Add<HConstant>(i);
2814 Add<HStoreKeyed>(elements, key, value, elements_kind);
2817 // Carefully loop backwards so that the "from" remains live through the loop
2818 // rather than the to. This often corresponds to keeping length live rather
2819 // then capacity, which helps register allocation, since length is used more
2820 // other than capacity after filling with holes.
2821 LoopBuilder builder(this, context(), LoopBuilder::kPostDecrement);
2823 HValue* key = builder.BeginBody(to, from, Token::GT);
2825 HValue* adjusted_key = AddUncasted<HSub>(key, graph()->GetConstant1());
2826 adjusted_key->ClearFlag(HValue::kCanOverflow);
2828 Add<HStoreKeyed>(elements, adjusted_key, value, elements_kind);
2835 void HGraphBuilder::BuildFillElementsWithHole(HValue* elements,
2836 ElementsKind elements_kind,
2839 // Fast elements kinds need to be initialized in case statements below cause a
2840 // garbage collection.
2842 HValue* hole = IsFastSmiOrObjectElementsKind(elements_kind)
2843 ? graph()->GetConstantHole()
2844 : Add<HConstant>(HConstant::kHoleNaN);
2846 // Since we're about to store a hole value, the store instruction below must
2847 // assume an elements kind that supports heap object values.
2848 if (IsFastSmiOrObjectElementsKind(elements_kind)) {
2849 elements_kind = FAST_HOLEY_ELEMENTS;
2852 BuildFillElementsWithValue(elements, elements_kind, from, to, hole);
2856 void HGraphBuilder::BuildCopyProperties(HValue* from_properties,
2857 HValue* to_properties, HValue* length,
2859 ElementsKind kind = FAST_ELEMENTS;
2861 BuildFillElementsWithValue(to_properties, kind, length, capacity,
2862 graph()->GetConstantUndefined());
2864 LoopBuilder builder(this, context(), LoopBuilder::kPostDecrement);
2866 HValue* key = builder.BeginBody(length, graph()->GetConstant0(), Token::GT);
2868 key = AddUncasted<HSub>(key, graph()->GetConstant1());
2869 key->ClearFlag(HValue::kCanOverflow);
2871 HValue* element = Add<HLoadKeyed>(from_properties, key, nullptr, kind);
2873 Add<HStoreKeyed>(to_properties, key, element, kind);
2879 void HGraphBuilder::BuildCopyElements(HValue* from_elements,
2880 ElementsKind from_elements_kind,
2881 HValue* to_elements,
2882 ElementsKind to_elements_kind,
2885 int constant_capacity = -1;
2886 if (capacity != NULL &&
2887 capacity->IsConstant() &&
2888 HConstant::cast(capacity)->HasInteger32Value()) {
2889 int constant_candidate = HConstant::cast(capacity)->Integer32Value();
2890 if (constant_candidate <= kElementLoopUnrollThreshold) {
2891 constant_capacity = constant_candidate;
2895 bool pre_fill_with_holes =
2896 IsFastDoubleElementsKind(from_elements_kind) &&
2897 IsFastObjectElementsKind(to_elements_kind);
2898 if (pre_fill_with_holes) {
2899 // If the copy might trigger a GC, make sure that the FixedArray is
2900 // pre-initialized with holes to make sure that it's always in a
2901 // consistent state.
2902 BuildFillElementsWithHole(to_elements, to_elements_kind,
2903 graph()->GetConstant0(), NULL);
2906 if (constant_capacity != -1) {
2907 // Unroll the loop for small elements kinds.
2908 for (int i = 0; i < constant_capacity; i++) {
2909 HValue* key_constant = Add<HConstant>(i);
2910 HInstruction* value = Add<HLoadKeyed>(from_elements, key_constant,
2911 nullptr, from_elements_kind);
2912 Add<HStoreKeyed>(to_elements, key_constant, value, to_elements_kind);
2915 if (!pre_fill_with_holes &&
2916 (capacity == NULL || !length->Equals(capacity))) {
2917 BuildFillElementsWithHole(to_elements, to_elements_kind,
2921 LoopBuilder builder(this, context(), LoopBuilder::kPostDecrement);
2923 HValue* key = builder.BeginBody(length, graph()->GetConstant0(),
2926 key = AddUncasted<HSub>(key, graph()->GetConstant1());
2927 key->ClearFlag(HValue::kCanOverflow);
2929 HValue* element = Add<HLoadKeyed>(from_elements, key, nullptr,
2930 from_elements_kind, ALLOW_RETURN_HOLE);
2932 ElementsKind kind = (IsHoleyElementsKind(from_elements_kind) &&
2933 IsFastSmiElementsKind(to_elements_kind))
2934 ? FAST_HOLEY_ELEMENTS : to_elements_kind;
2936 if (IsHoleyElementsKind(from_elements_kind) &&
2937 from_elements_kind != to_elements_kind) {
2938 IfBuilder if_hole(this);
2939 if_hole.If<HCompareHoleAndBranch>(element);
2941 HConstant* hole_constant = IsFastDoubleElementsKind(to_elements_kind)
2942 ? Add<HConstant>(HConstant::kHoleNaN)
2943 : graph()->GetConstantHole();
2944 Add<HStoreKeyed>(to_elements, key, hole_constant, kind);
2946 HStoreKeyed* store = Add<HStoreKeyed>(to_elements, key, element, kind);
2947 store->SetFlag(HValue::kAllowUndefinedAsNaN);
2950 HStoreKeyed* store = Add<HStoreKeyed>(to_elements, key, element, kind);
2951 store->SetFlag(HValue::kAllowUndefinedAsNaN);
2957 Counters* counters = isolate()->counters();
2958 AddIncrementCounter(counters->inlined_copied_elements());
2962 HValue* HGraphBuilder::BuildCloneShallowArrayCow(HValue* boilerplate,
2963 HValue* allocation_site,
2964 AllocationSiteMode mode,
2965 ElementsKind kind) {
2966 HAllocate* array = AllocateJSArrayObject(mode);
2968 HValue* map = AddLoadMap(boilerplate);
2969 HValue* elements = AddLoadElements(boilerplate);
2970 HValue* length = AddLoadArrayLength(boilerplate, kind);
2972 BuildJSArrayHeader(array,
2983 HValue* HGraphBuilder::BuildCloneShallowArrayEmpty(HValue* boilerplate,
2984 HValue* allocation_site,
2985 AllocationSiteMode mode) {
2986 HAllocate* array = AllocateJSArrayObject(mode);
2988 HValue* map = AddLoadMap(boilerplate);
2990 BuildJSArrayHeader(array,
2992 NULL, // set elements to empty fixed array
2996 graph()->GetConstant0());
3001 HValue* HGraphBuilder::BuildCloneShallowArrayNonEmpty(HValue* boilerplate,
3002 HValue* allocation_site,
3003 AllocationSiteMode mode,
3004 ElementsKind kind) {
3005 HValue* boilerplate_elements = AddLoadElements(boilerplate);
3006 HValue* capacity = AddLoadFixedArrayLength(boilerplate_elements);
3008 // Generate size calculation code here in order to make it dominate
3009 // the JSArray allocation.
3010 HValue* elements_size = BuildCalculateElementsSize(kind, capacity);
3012 // Create empty JSArray object for now, store elimination should remove
3013 // redundant initialization of elements and length fields and at the same
3014 // time the object will be fully prepared for GC if it happens during
3015 // elements allocation.
3016 HValue* result = BuildCloneShallowArrayEmpty(
3017 boilerplate, allocation_site, mode);
3019 HAllocate* elements = BuildAllocateElements(kind, elements_size);
3021 // This function implicitly relies on the fact that the
3022 // FastCloneShallowArrayStub is called only for literals shorter than
3023 // JSObject::kInitialMaxFastElementArray.
3024 // Can't add HBoundsCheck here because otherwise the stub will eager a frame.
3025 HConstant* size_upper_bound = EstablishElementsAllocationSize(
3026 kind, JSObject::kInitialMaxFastElementArray);
3027 elements->set_size_upper_bound(size_upper_bound);
3029 Add<HStoreNamedField>(result, HObjectAccess::ForElementsPointer(), elements);
3031 // The allocation for the cloned array above causes register pressure on
3032 // machines with low register counts. Force a reload of the boilerplate
3033 // elements here to free up a register for the allocation to avoid unnecessary
3035 boilerplate_elements = AddLoadElements(boilerplate);
3036 boilerplate_elements->SetFlag(HValue::kCantBeReplaced);
3038 // Copy the elements array header.
3039 for (int i = 0; i < FixedArrayBase::kHeaderSize; i += kPointerSize) {
3040 HObjectAccess access = HObjectAccess::ForFixedArrayHeader(i);
3041 Add<HStoreNamedField>(
3043 Add<HLoadNamedField>(boilerplate_elements, nullptr, access));
3046 // And the result of the length
3047 HValue* length = AddLoadArrayLength(boilerplate, kind);
3048 Add<HStoreNamedField>(result, HObjectAccess::ForArrayLength(kind), length);
3050 BuildCopyElements(boilerplate_elements, kind, elements,
3051 kind, length, NULL);
3056 void HGraphBuilder::BuildCompareNil(HValue* value, Type* type,
3057 HIfContinuation* continuation,
3058 MapEmbedding map_embedding) {
3059 IfBuilder if_nil(this);
3060 bool some_case_handled = false;
3061 bool some_case_missing = false;
3063 if (type->Maybe(Type::Null())) {
3064 if (some_case_handled) if_nil.Or();
3065 if_nil.If<HCompareObjectEqAndBranch>(value, graph()->GetConstantNull());
3066 some_case_handled = true;
3068 some_case_missing = true;
3071 if (type->Maybe(Type::Undefined())) {
3072 if (some_case_handled) if_nil.Or();
3073 if_nil.If<HCompareObjectEqAndBranch>(value,
3074 graph()->GetConstantUndefined());
3075 some_case_handled = true;
3077 some_case_missing = true;
3080 if (type->Maybe(Type::Undetectable())) {
3081 if (some_case_handled) if_nil.Or();
3082 if_nil.If<HIsUndetectableAndBranch>(value);
3083 some_case_handled = true;
3085 some_case_missing = true;
3088 if (some_case_missing) {
3091 if (type->NumClasses() == 1) {
3092 BuildCheckHeapObject(value);
3093 // For ICs, the map checked below is a sentinel map that gets replaced by
3094 // the monomorphic map when the code is used as a template to generate a
3095 // new IC. For optimized functions, there is no sentinel map, the map
3096 // emitted below is the actual monomorphic map.
3097 if (map_embedding == kEmbedMapsViaWeakCells) {
3099 Add<HConstant>(Map::WeakCellForMap(type->Classes().Current()));
3100 HValue* expected_map = Add<HLoadNamedField>(
3101 cell, nullptr, HObjectAccess::ForWeakCellValue());
3103 Add<HLoadNamedField>(value, nullptr, HObjectAccess::ForMap());
3104 IfBuilder map_check(this);
3105 map_check.IfNot<HCompareObjectEqAndBranch>(expected_map, map);
3106 map_check.ThenDeopt(Deoptimizer::kUnknownMap);
3109 DCHECK(map_embedding == kEmbedMapsDirectly);
3110 Add<HCheckMaps>(value, type->Classes().Current());
3113 if_nil.Deopt(Deoptimizer::kTooManyUndetectableTypes);
3117 if_nil.CaptureContinuation(continuation);
3121 void HGraphBuilder::BuildCreateAllocationMemento(
3122 HValue* previous_object,
3123 HValue* previous_object_size,
3124 HValue* allocation_site) {
3125 DCHECK(allocation_site != NULL);
3126 HInnerAllocatedObject* allocation_memento = Add<HInnerAllocatedObject>(
3127 previous_object, previous_object_size, HType::HeapObject());
3128 AddStoreMapConstant(
3129 allocation_memento, isolate()->factory()->allocation_memento_map());
3130 Add<HStoreNamedField>(
3132 HObjectAccess::ForAllocationMementoSite(),
3134 if (FLAG_allocation_site_pretenuring) {
3135 HValue* memento_create_count =
3136 Add<HLoadNamedField>(allocation_site, nullptr,
3137 HObjectAccess::ForAllocationSiteOffset(
3138 AllocationSite::kPretenureCreateCountOffset));
3139 memento_create_count = AddUncasted<HAdd>(
3140 memento_create_count, graph()->GetConstant1());
3141 // This smi value is reset to zero after every gc, overflow isn't a problem
3142 // since the counter is bounded by the new space size.
3143 memento_create_count->ClearFlag(HValue::kCanOverflow);
3144 Add<HStoreNamedField>(
3145 allocation_site, HObjectAccess::ForAllocationSiteOffset(
3146 AllocationSite::kPretenureCreateCountOffset), memento_create_count);
3151 HInstruction* HGraphBuilder::BuildGetNativeContext() {
3152 // Get the global object, then the native context
3153 HValue* global_object = Add<HLoadNamedField>(
3155 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
3156 return Add<HLoadNamedField>(global_object, nullptr,
3157 HObjectAccess::ForObservableJSObjectOffset(
3158 GlobalObject::kNativeContextOffset));
3162 HInstruction* HGraphBuilder::BuildGetNativeContext(HValue* closure) {
3163 // Get the global object, then the native context
3164 HInstruction* context = Add<HLoadNamedField>(
3165 closure, nullptr, HObjectAccess::ForFunctionContextPointer());
3166 HInstruction* global_object = Add<HLoadNamedField>(
3168 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
3169 HObjectAccess access = HObjectAccess::ForObservableJSObjectOffset(
3170 GlobalObject::kNativeContextOffset);
3171 return Add<HLoadNamedField>(global_object, nullptr, access);
3175 HInstruction* HGraphBuilder::BuildGetScriptContext(int context_index) {
3176 HValue* native_context = BuildGetNativeContext();
3177 HValue* script_context_table = Add<HLoadNamedField>(
3178 native_context, nullptr,
3179 HObjectAccess::ForContextSlot(Context::SCRIPT_CONTEXT_TABLE_INDEX));
3180 return Add<HLoadNamedField>(script_context_table, nullptr,
3181 HObjectAccess::ForScriptContext(context_index));
3185 HValue* HGraphBuilder::BuildGetParentContext(HValue* depth, int depth_value) {
3186 HValue* script_context = context();
3187 if (depth != NULL) {
3188 HValue* zero = graph()->GetConstant0();
3190 Push(script_context);
3193 LoopBuilder loop(this);
3194 loop.BeginBody(2); // Drop script_context and depth from last environment
3195 // to appease live range building without simulates.
3197 script_context = Pop();
3199 script_context = Add<HLoadNamedField>(
3200 script_context, nullptr,
3201 HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX));
3202 depth = AddUncasted<HSub>(depth, graph()->GetConstant1());
3203 depth->ClearFlag(HValue::kCanOverflow);
3205 IfBuilder if_break(this);
3206 if_break.If<HCompareNumericAndBranch, HValue*>(depth, zero, Token::EQ);
3209 Push(script_context); // The result.
3214 Push(script_context);
3220 script_context = Pop();
3221 } else if (depth_value > 0) {
3222 // Unroll the above loop.
3223 for (int i = 0; i < depth_value; i++) {
3224 script_context = Add<HLoadNamedField>(
3225 script_context, nullptr,
3226 HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX));
3229 return script_context;
3233 HInstruction* HGraphBuilder::BuildGetArrayFunction() {
3234 HInstruction* native_context = BuildGetNativeContext();
3235 HInstruction* index =
3236 Add<HConstant>(static_cast<int32_t>(Context::ARRAY_FUNCTION_INDEX));
3237 return Add<HLoadKeyed>(native_context, index, nullptr, FAST_ELEMENTS);
3241 HValue* HGraphBuilder::BuildArrayBufferViewFieldAccessor(HValue* object,
3242 HValue* checked_object,
3244 NoObservableSideEffectsScope scope(this);
3245 HObjectAccess access = HObjectAccess::ForObservableJSObjectOffset(
3246 index.offset(), Representation::Tagged());
3247 HInstruction* buffer = Add<HLoadNamedField>(
3248 object, checked_object, HObjectAccess::ForJSArrayBufferViewBuffer());
3249 HInstruction* field = Add<HLoadNamedField>(object, checked_object, access);
3251 HInstruction* flags = Add<HLoadNamedField>(
3252 buffer, nullptr, HObjectAccess::ForJSArrayBufferBitField());
3253 HValue* was_neutered_mask =
3254 Add<HConstant>(1 << JSArrayBuffer::WasNeutered::kShift);
3255 HValue* was_neutered_test =
3256 AddUncasted<HBitwise>(Token::BIT_AND, flags, was_neutered_mask);
3258 IfBuilder if_was_neutered(this);
3259 if_was_neutered.If<HCompareNumericAndBranch>(
3260 was_neutered_test, graph()->GetConstant0(), Token::NE);
3261 if_was_neutered.Then();
3262 Push(graph()->GetConstant0());
3263 if_was_neutered.Else();
3265 if_was_neutered.End();
3271 HGraphBuilder::JSArrayBuilder::JSArrayBuilder(HGraphBuilder* builder,
3273 HValue* allocation_site_payload,
3274 HValue* constructor_function,
3275 AllocationSiteOverrideMode override_mode) :
3278 allocation_site_payload_(allocation_site_payload),
3279 constructor_function_(constructor_function) {
3280 DCHECK(!allocation_site_payload->IsConstant() ||
3281 HConstant::cast(allocation_site_payload)->handle(
3282 builder_->isolate())->IsAllocationSite());
3283 mode_ = override_mode == DISABLE_ALLOCATION_SITES
3284 ? DONT_TRACK_ALLOCATION_SITE
3285 : AllocationSite::GetMode(kind);
3289 HGraphBuilder::JSArrayBuilder::JSArrayBuilder(HGraphBuilder* builder,
3291 HValue* constructor_function) :
3294 mode_(DONT_TRACK_ALLOCATION_SITE),
3295 allocation_site_payload_(NULL),
3296 constructor_function_(constructor_function) {
3300 HValue* HGraphBuilder::JSArrayBuilder::EmitMapCode() {
3301 if (!builder()->top_info()->IsStub()) {
3302 // A constant map is fine.
3303 Handle<Map> map(builder()->isolate()->get_initial_js_array_map(kind_),
3304 builder()->isolate());
3305 return builder()->Add<HConstant>(map);
3308 if (constructor_function_ != NULL && kind_ == GetInitialFastElementsKind()) {
3309 // No need for a context lookup if the kind_ matches the initial
3310 // map, because we can just load the map in that case.
3311 HObjectAccess access = HObjectAccess::ForPrototypeOrInitialMap();
3312 return builder()->Add<HLoadNamedField>(constructor_function_, nullptr,
3316 // TODO(mvstanton): we should always have a constructor function if we
3317 // are creating a stub.
3318 HInstruction* native_context = constructor_function_ != NULL
3319 ? builder()->BuildGetNativeContext(constructor_function_)
3320 : builder()->BuildGetNativeContext();
3322 HInstruction* index = builder()->Add<HConstant>(
3323 static_cast<int32_t>(Context::JS_ARRAY_MAPS_INDEX));
3325 HInstruction* map_array =
3326 builder()->Add<HLoadKeyed>(native_context, index, nullptr, FAST_ELEMENTS);
3328 HInstruction* kind_index = builder()->Add<HConstant>(kind_);
3330 return builder()->Add<HLoadKeyed>(map_array, kind_index, nullptr,
3335 HValue* HGraphBuilder::JSArrayBuilder::EmitInternalMapCode() {
3336 // Find the map near the constructor function
3337 HObjectAccess access = HObjectAccess::ForPrototypeOrInitialMap();
3338 return builder()->Add<HLoadNamedField>(constructor_function_, nullptr,
3343 HAllocate* HGraphBuilder::JSArrayBuilder::AllocateEmptyArray() {
3344 HConstant* capacity = builder()->Add<HConstant>(initial_capacity());
3345 return AllocateArray(capacity,
3347 builder()->graph()->GetConstant0());
3351 HAllocate* HGraphBuilder::JSArrayBuilder::AllocateArray(
3353 HConstant* capacity_upper_bound,
3354 HValue* length_field,
3355 FillMode fill_mode) {
3356 return AllocateArray(capacity,
3357 capacity_upper_bound->GetInteger32Constant(),
3363 HAllocate* HGraphBuilder::JSArrayBuilder::AllocateArray(
3365 int capacity_upper_bound,
3366 HValue* length_field,
3367 FillMode fill_mode) {
3368 HConstant* elememts_size_upper_bound = capacity->IsInteger32Constant()
3369 ? HConstant::cast(capacity)
3370 : builder()->EstablishElementsAllocationSize(kind_, capacity_upper_bound);
3372 HAllocate* array = AllocateArray(capacity, length_field, fill_mode);
3373 if (!elements_location_->has_size_upper_bound()) {
3374 elements_location_->set_size_upper_bound(elememts_size_upper_bound);
3380 HAllocate* HGraphBuilder::JSArrayBuilder::AllocateArray(
3382 HValue* length_field,
3383 FillMode fill_mode) {
3384 // These HForceRepresentations are because we store these as fields in the
3385 // objects we construct, and an int32-to-smi HChange could deopt. Accept
3386 // the deopt possibility now, before allocation occurs.
3388 builder()->AddUncasted<HForceRepresentation>(capacity,
3389 Representation::Smi());
3391 builder()->AddUncasted<HForceRepresentation>(length_field,
3392 Representation::Smi());
3394 // Generate size calculation code here in order to make it dominate
3395 // the JSArray allocation.
3396 HValue* elements_size =
3397 builder()->BuildCalculateElementsSize(kind_, capacity);
3399 // Allocate (dealing with failure appropriately)
3400 HAllocate* array_object = builder()->AllocateJSArrayObject(mode_);
3402 // Fill in the fields: map, properties, length
3404 if (allocation_site_payload_ == NULL) {
3405 map = EmitInternalMapCode();
3407 map = EmitMapCode();
3410 builder()->BuildJSArrayHeader(array_object,
3412 NULL, // set elements to empty fixed array
3415 allocation_site_payload_,
3418 // Allocate and initialize the elements
3419 elements_location_ = builder()->BuildAllocateElements(kind_, elements_size);
3421 builder()->BuildInitializeElementsHeader(elements_location_, kind_, capacity);
3424 builder()->Add<HStoreNamedField>(
3425 array_object, HObjectAccess::ForElementsPointer(), elements_location_);
3427 if (fill_mode == FILL_WITH_HOLE) {
3428 builder()->BuildFillElementsWithHole(elements_location_, kind_,
3429 graph()->GetConstant0(), capacity);
3432 return array_object;
3436 HValue* HGraphBuilder::AddLoadJSBuiltin(Builtins::JavaScript builtin) {
3437 HValue* global_object = Add<HLoadNamedField>(
3439 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
3440 HObjectAccess access = HObjectAccess::ForObservableJSObjectOffset(
3441 GlobalObject::kBuiltinsOffset);
3442 HValue* builtins = Add<HLoadNamedField>(global_object, nullptr, access);
3443 HObjectAccess function_access = HObjectAccess::ForObservableJSObjectOffset(
3444 JSBuiltinsObject::OffsetOfFunctionWithId(builtin));
3445 return Add<HLoadNamedField>(builtins, nullptr, function_access);
3449 HOptimizedGraphBuilder::HOptimizedGraphBuilder(CompilationInfo* info)
3450 : HGraphBuilder(info),
3451 function_state_(NULL),
3452 initial_function_state_(this, info, NORMAL_RETURN, 0),
3456 globals_(10, info->zone()),
3457 osr_(new(info->zone()) HOsrBuilder(this)) {
3458 // This is not initialized in the initializer list because the
3459 // constructor for the initial state relies on function_state_ == NULL
3460 // to know it's the initial state.
3461 function_state_ = &initial_function_state_;
3462 InitializeAstVisitor(info->isolate(), info->zone());
3463 if (top_info()->is_tracking_positions()) {
3464 SetSourcePosition(info->shared_info()->start_position());
3469 HBasicBlock* HOptimizedGraphBuilder::CreateJoin(HBasicBlock* first,
3470 HBasicBlock* second,
3471 BailoutId join_id) {
3472 if (first == NULL) {
3474 } else if (second == NULL) {
3477 HBasicBlock* join_block = graph()->CreateBasicBlock();
3478 Goto(first, join_block);
3479 Goto(second, join_block);
3480 join_block->SetJoinId(join_id);
3486 HBasicBlock* HOptimizedGraphBuilder::JoinContinue(IterationStatement* statement,
3487 HBasicBlock* exit_block,
3488 HBasicBlock* continue_block) {
3489 if (continue_block != NULL) {
3490 if (exit_block != NULL) Goto(exit_block, continue_block);
3491 continue_block->SetJoinId(statement->ContinueId());
3492 return continue_block;
3498 HBasicBlock* HOptimizedGraphBuilder::CreateLoop(IterationStatement* statement,
3499 HBasicBlock* loop_entry,
3500 HBasicBlock* body_exit,
3501 HBasicBlock* loop_successor,
3502 HBasicBlock* break_block) {
3503 if (body_exit != NULL) Goto(body_exit, loop_entry);
3504 loop_entry->PostProcessLoopHeader(statement);
3505 if (break_block != NULL) {
3506 if (loop_successor != NULL) Goto(loop_successor, break_block);
3507 break_block->SetJoinId(statement->ExitId());
3510 return loop_successor;
3514 // Build a new loop header block and set it as the current block.
3515 HBasicBlock* HOptimizedGraphBuilder::BuildLoopEntry() {
3516 HBasicBlock* loop_entry = CreateLoopHeaderBlock();
3518 set_current_block(loop_entry);
3523 HBasicBlock* HOptimizedGraphBuilder::BuildLoopEntry(
3524 IterationStatement* statement) {
3525 HBasicBlock* loop_entry = osr()->HasOsrEntryAt(statement)
3526 ? osr()->BuildOsrLoopEntry(statement)
3532 void HBasicBlock::FinishExit(HControlInstruction* instruction,
3533 SourcePosition position) {
3534 Finish(instruction, position);
3539 std::ostream& operator<<(std::ostream& os, const HBasicBlock& b) {
3540 return os << "B" << b.block_id();
3544 HGraph::HGraph(CompilationInfo* info)
3545 : isolate_(info->isolate()),
3548 blocks_(8, info->zone()),
3549 values_(16, info->zone()),
3551 uint32_instructions_(NULL),
3554 zone_(info->zone()),
3555 is_recursive_(false),
3556 use_optimistic_licm_(false),
3557 depends_on_empty_array_proto_elements_(false),
3558 type_change_checksum_(0),
3559 maximum_environment_size_(0),
3560 no_side_effects_scope_count_(0),
3561 disallow_adding_new_values_(false) {
3562 if (info->IsStub()) {
3563 CallInterfaceDescriptor descriptor =
3564 info->code_stub()->GetCallInterfaceDescriptor();
3565 start_environment_ =
3566 new (zone_) HEnvironment(zone_, descriptor.GetRegisterParameterCount());
3568 if (info->is_tracking_positions()) {
3569 info->TraceInlinedFunction(info->shared_info(), SourcePosition::Unknown(),
3570 InlinedFunctionInfo::kNoParentId);
3572 start_environment_ =
3573 new(zone_) HEnvironment(NULL, info->scope(), info->closure(), zone_);
3575 start_environment_->set_ast_id(BailoutId::FunctionEntry());
3576 entry_block_ = CreateBasicBlock();
3577 entry_block_->SetInitialEnvironment(start_environment_);
3581 HBasicBlock* HGraph::CreateBasicBlock() {
3582 HBasicBlock* result = new(zone()) HBasicBlock(this);
3583 blocks_.Add(result, zone());
3588 void HGraph::FinalizeUniqueness() {
3589 DisallowHeapAllocation no_gc;
3590 for (int i = 0; i < blocks()->length(); ++i) {
3591 for (HInstructionIterator it(blocks()->at(i)); !it.Done(); it.Advance()) {
3592 it.Current()->FinalizeUniqueness();
3598 int HGraph::SourcePositionToScriptPosition(SourcePosition pos) {
3599 return (FLAG_hydrogen_track_positions && !pos.IsUnknown())
3600 ? info()->start_position_for(pos.inlining_id()) + pos.position()
3605 // Block ordering was implemented with two mutually recursive methods,
3606 // HGraph::Postorder and HGraph::PostorderLoopBlocks.
3607 // The recursion could lead to stack overflow so the algorithm has been
3608 // implemented iteratively.
3609 // At a high level the algorithm looks like this:
3611 // Postorder(block, loop_header) : {
3612 // if (block has already been visited or is of another loop) return;
3613 // mark block as visited;
3614 // if (block is a loop header) {
3615 // VisitLoopMembers(block, loop_header);
3616 // VisitSuccessorsOfLoopHeader(block);
3618 // VisitSuccessors(block)
3620 // put block in result list;
3623 // VisitLoopMembers(block, outer_loop_header) {
3624 // foreach (block b in block loop members) {
3625 // VisitSuccessorsOfLoopMember(b, outer_loop_header);
3626 // if (b is loop header) VisitLoopMembers(b);
3630 // VisitSuccessorsOfLoopMember(block, outer_loop_header) {
3631 // foreach (block b in block successors) Postorder(b, outer_loop_header)
3634 // VisitSuccessorsOfLoopHeader(block) {
3635 // foreach (block b in block successors) Postorder(b, block)
3638 // VisitSuccessors(block, loop_header) {
3639 // foreach (block b in block successors) Postorder(b, loop_header)
3642 // The ordering is started calling Postorder(entry, NULL).
3644 // Each instance of PostorderProcessor represents the "stack frame" of the
3645 // recursion, and particularly keeps the state of the loop (iteration) of the
3646 // "Visit..." function it represents.
3647 // To recycle memory we keep all the frames in a double linked list but
3648 // this means that we cannot use constructors to initialize the frames.
3650 class PostorderProcessor : public ZoneObject {
3652 // Back link (towards the stack bottom).
3653 PostorderProcessor* parent() {return father_; }
3654 // Forward link (towards the stack top).
3655 PostorderProcessor* child() {return child_; }
3656 HBasicBlock* block() { return block_; }
3657 HLoopInformation* loop() { return loop_; }
3658 HBasicBlock* loop_header() { return loop_header_; }
3660 static PostorderProcessor* CreateEntryProcessor(Zone* zone,
3661 HBasicBlock* block) {
3662 PostorderProcessor* result = new(zone) PostorderProcessor(NULL);
3663 return result->SetupSuccessors(zone, block, NULL);
3666 PostorderProcessor* PerformStep(Zone* zone,
3667 ZoneList<HBasicBlock*>* order) {
3668 PostorderProcessor* next =
3669 PerformNonBacktrackingStep(zone, order);
3673 return Backtrack(zone, order);
3678 explicit PostorderProcessor(PostorderProcessor* father)
3679 : father_(father), child_(NULL), successor_iterator(NULL) { }
3681 // Each enum value states the cycle whose state is kept by this instance.
3685 SUCCESSORS_OF_LOOP_HEADER,
3687 SUCCESSORS_OF_LOOP_MEMBER
3690 // Each "Setup..." method is like a constructor for a cycle state.
3691 PostorderProcessor* SetupSuccessors(Zone* zone,
3693 HBasicBlock* loop_header) {
3694 if (block == NULL || block->IsOrdered() ||
3695 block->parent_loop_header() != loop_header) {
3699 loop_header_ = NULL;
3704 block->MarkAsOrdered();
3706 if (block->IsLoopHeader()) {
3707 kind_ = SUCCESSORS_OF_LOOP_HEADER;
3708 loop_header_ = block;
3709 InitializeSuccessors();
3710 PostorderProcessor* result = Push(zone);
3711 return result->SetupLoopMembers(zone, block, block->loop_information(),
3714 DCHECK(block->IsFinished());
3716 loop_header_ = loop_header;
3717 InitializeSuccessors();
3723 PostorderProcessor* SetupLoopMembers(Zone* zone,
3725 HLoopInformation* loop,
3726 HBasicBlock* loop_header) {
3727 kind_ = LOOP_MEMBERS;
3730 loop_header_ = loop_header;
3731 InitializeLoopMembers();
3735 PostorderProcessor* SetupSuccessorsOfLoopMember(
3737 HLoopInformation* loop,
3738 HBasicBlock* loop_header) {
3739 kind_ = SUCCESSORS_OF_LOOP_MEMBER;
3742 loop_header_ = loop_header;
3743 InitializeSuccessors();
3747 // This method "allocates" a new stack frame.
3748 PostorderProcessor* Push(Zone* zone) {
3749 if (child_ == NULL) {
3750 child_ = new(zone) PostorderProcessor(this);
3755 void ClosePostorder(ZoneList<HBasicBlock*>* order, Zone* zone) {
3756 DCHECK(block_->end()->FirstSuccessor() == NULL ||
3757 order->Contains(block_->end()->FirstSuccessor()) ||
3758 block_->end()->FirstSuccessor()->IsLoopHeader());
3759 DCHECK(block_->end()->SecondSuccessor() == NULL ||
3760 order->Contains(block_->end()->SecondSuccessor()) ||
3761 block_->end()->SecondSuccessor()->IsLoopHeader());
3762 order->Add(block_, zone);
3765 // This method is the basic block to walk up the stack.
3766 PostorderProcessor* Pop(Zone* zone,
3767 ZoneList<HBasicBlock*>* order) {
3770 case SUCCESSORS_OF_LOOP_HEADER:
3771 ClosePostorder(order, zone);
3775 case SUCCESSORS_OF_LOOP_MEMBER:
3776 if (block()->IsLoopHeader() && block() != loop_->loop_header()) {
3777 // In this case we need to perform a LOOP_MEMBERS cycle so we
3778 // initialize it and return this instead of father.
3779 return SetupLoopMembers(zone, block(),
3780 block()->loop_information(), loop_header_);
3791 // Walks up the stack.
3792 PostorderProcessor* Backtrack(Zone* zone,
3793 ZoneList<HBasicBlock*>* order) {
3794 PostorderProcessor* parent = Pop(zone, order);
3795 while (parent != NULL) {
3796 PostorderProcessor* next =
3797 parent->PerformNonBacktrackingStep(zone, order);
3801 parent = parent->Pop(zone, order);
3807 PostorderProcessor* PerformNonBacktrackingStep(
3809 ZoneList<HBasicBlock*>* order) {
3810 HBasicBlock* next_block;
3813 next_block = AdvanceSuccessors();
3814 if (next_block != NULL) {
3815 PostorderProcessor* result = Push(zone);
3816 return result->SetupSuccessors(zone, next_block, loop_header_);
3819 case SUCCESSORS_OF_LOOP_HEADER:
3820 next_block = AdvanceSuccessors();
3821 if (next_block != NULL) {
3822 PostorderProcessor* result = Push(zone);
3823 return result->SetupSuccessors(zone, next_block, block());
3827 next_block = AdvanceLoopMembers();
3828 if (next_block != NULL) {
3829 PostorderProcessor* result = Push(zone);
3830 return result->SetupSuccessorsOfLoopMember(next_block,
3831 loop_, loop_header_);
3834 case SUCCESSORS_OF_LOOP_MEMBER:
3835 next_block = AdvanceSuccessors();
3836 if (next_block != NULL) {
3837 PostorderProcessor* result = Push(zone);
3838 return result->SetupSuccessors(zone, next_block, loop_header_);
3847 // The following two methods implement a "foreach b in successors" cycle.
3848 void InitializeSuccessors() {
3851 successor_iterator = HSuccessorIterator(block_->end());
3854 HBasicBlock* AdvanceSuccessors() {
3855 if (!successor_iterator.Done()) {
3856 HBasicBlock* result = successor_iterator.Current();
3857 successor_iterator.Advance();
3863 // The following two methods implement a "foreach b in loop members" cycle.
3864 void InitializeLoopMembers() {
3866 loop_length = loop_->blocks()->length();
3869 HBasicBlock* AdvanceLoopMembers() {
3870 if (loop_index < loop_length) {
3871 HBasicBlock* result = loop_->blocks()->at(loop_index);
3880 PostorderProcessor* father_;
3881 PostorderProcessor* child_;
3882 HLoopInformation* loop_;
3883 HBasicBlock* block_;
3884 HBasicBlock* loop_header_;
3887 HSuccessorIterator successor_iterator;
3891 void HGraph::OrderBlocks() {
3892 CompilationPhase phase("H_Block ordering", info());
3895 // Initially the blocks must not be ordered.
3896 for (int i = 0; i < blocks_.length(); ++i) {
3897 DCHECK(!blocks_[i]->IsOrdered());
3901 PostorderProcessor* postorder =
3902 PostorderProcessor::CreateEntryProcessor(zone(), blocks_[0]);
3905 postorder = postorder->PerformStep(zone(), &blocks_);
3909 // Now all blocks must be marked as ordered.
3910 for (int i = 0; i < blocks_.length(); ++i) {
3911 DCHECK(blocks_[i]->IsOrdered());
3915 // Reverse block list and assign block IDs.
3916 for (int i = 0, j = blocks_.length(); --j >= i; ++i) {
3917 HBasicBlock* bi = blocks_[i];
3918 HBasicBlock* bj = blocks_[j];
3919 bi->set_block_id(j);
3920 bj->set_block_id(i);
3927 void HGraph::AssignDominators() {
3928 HPhase phase("H_Assign dominators", this);
3929 for (int i = 0; i < blocks_.length(); ++i) {
3930 HBasicBlock* block = blocks_[i];
3931 if (block->IsLoopHeader()) {
3932 // Only the first predecessor of a loop header is from outside the loop.
3933 // All others are back edges, and thus cannot dominate the loop header.
3934 block->AssignCommonDominator(block->predecessors()->first());
3935 block->AssignLoopSuccessorDominators();
3937 for (int j = blocks_[i]->predecessors()->length() - 1; j >= 0; --j) {
3938 blocks_[i]->AssignCommonDominator(blocks_[i]->predecessors()->at(j));
3945 bool HGraph::CheckArgumentsPhiUses() {
3946 int block_count = blocks_.length();
3947 for (int i = 0; i < block_count; ++i) {
3948 for (int j = 0; j < blocks_[i]->phis()->length(); ++j) {
3949 HPhi* phi = blocks_[i]->phis()->at(j);
3950 // We don't support phi uses of arguments for now.
3951 if (phi->CheckFlag(HValue::kIsArguments)) return false;
3958 bool HGraph::CheckConstPhiUses() {
3959 int block_count = blocks_.length();
3960 for (int i = 0; i < block_count; ++i) {
3961 for (int j = 0; j < blocks_[i]->phis()->length(); ++j) {
3962 HPhi* phi = blocks_[i]->phis()->at(j);
3963 // Check for the hole value (from an uninitialized const).
3964 for (int k = 0; k < phi->OperandCount(); k++) {
3965 if (phi->OperandAt(k) == GetConstantHole()) return false;
3973 void HGraph::CollectPhis() {
3974 int block_count = blocks_.length();
3975 phi_list_ = new(zone()) ZoneList<HPhi*>(block_count, zone());
3976 for (int i = 0; i < block_count; ++i) {
3977 for (int j = 0; j < blocks_[i]->phis()->length(); ++j) {
3978 HPhi* phi = blocks_[i]->phis()->at(j);
3979 phi_list_->Add(phi, zone());
3985 // Implementation of utility class to encapsulate the translation state for
3986 // a (possibly inlined) function.
3987 FunctionState::FunctionState(HOptimizedGraphBuilder* owner,
3988 CompilationInfo* info, InliningKind inlining_kind,
3991 compilation_info_(info),
3992 call_context_(NULL),
3993 inlining_kind_(inlining_kind),
3994 function_return_(NULL),
3995 test_context_(NULL),
3997 arguments_object_(NULL),
3998 arguments_elements_(NULL),
3999 inlining_id_(inlining_id),
4000 outer_source_position_(SourcePosition::Unknown()),
4001 outer_(owner->function_state()) {
4002 if (outer_ != NULL) {
4003 // State for an inline function.
4004 if (owner->ast_context()->IsTest()) {
4005 HBasicBlock* if_true = owner->graph()->CreateBasicBlock();
4006 HBasicBlock* if_false = owner->graph()->CreateBasicBlock();
4007 if_true->MarkAsInlineReturnTarget(owner->current_block());
4008 if_false->MarkAsInlineReturnTarget(owner->current_block());
4009 TestContext* outer_test_context = TestContext::cast(owner->ast_context());
4010 Expression* cond = outer_test_context->condition();
4011 // The AstContext constructor pushed on the context stack. This newed
4012 // instance is the reason that AstContext can't be BASE_EMBEDDED.
4013 test_context_ = new TestContext(owner, cond, if_true, if_false);
4015 function_return_ = owner->graph()->CreateBasicBlock();
4016 function_return()->MarkAsInlineReturnTarget(owner->current_block());
4018 // Set this after possibly allocating a new TestContext above.
4019 call_context_ = owner->ast_context();
4022 // Push on the state stack.
4023 owner->set_function_state(this);
4025 if (compilation_info_->is_tracking_positions()) {
4026 outer_source_position_ = owner->source_position();
4027 owner->EnterInlinedSource(
4028 info->shared_info()->start_position(),
4030 owner->SetSourcePosition(info->shared_info()->start_position());
4035 FunctionState::~FunctionState() {
4036 delete test_context_;
4037 owner_->set_function_state(outer_);
4039 if (compilation_info_->is_tracking_positions()) {
4040 owner_->set_source_position(outer_source_position_);
4041 owner_->EnterInlinedSource(
4042 outer_->compilation_info()->shared_info()->start_position(),
4043 outer_->inlining_id());
4048 // Implementation of utility classes to represent an expression's context in
4050 AstContext::AstContext(HOptimizedGraphBuilder* owner, Expression::Context kind)
4053 outer_(owner->ast_context()),
4054 typeof_mode_(NOT_INSIDE_TYPEOF) {
4055 owner->set_ast_context(this); // Push.
4057 DCHECK(owner->environment()->frame_type() == JS_FUNCTION);
4058 original_length_ = owner->environment()->length();
4063 AstContext::~AstContext() {
4064 owner_->set_ast_context(outer_); // Pop.
4068 EffectContext::~EffectContext() {
4069 DCHECK(owner()->HasStackOverflow() ||
4070 owner()->current_block() == NULL ||
4071 (owner()->environment()->length() == original_length_ &&
4072 owner()->environment()->frame_type() == JS_FUNCTION));
4076 ValueContext::~ValueContext() {
4077 DCHECK(owner()->HasStackOverflow() ||
4078 owner()->current_block() == NULL ||
4079 (owner()->environment()->length() == original_length_ + 1 &&
4080 owner()->environment()->frame_type() == JS_FUNCTION));
4084 void EffectContext::ReturnValue(HValue* value) {
4085 // The value is simply ignored.
4089 void ValueContext::ReturnValue(HValue* value) {
4090 // The value is tracked in the bailout environment, and communicated
4091 // through the environment as the result of the expression.
4092 if (value->CheckFlag(HValue::kIsArguments)) {
4093 if (flag_ == ARGUMENTS_FAKED) {
4094 value = owner()->graph()->GetConstantUndefined();
4095 } else if (!arguments_allowed()) {
4096 owner()->Bailout(kBadValueContextForArgumentsValue);
4099 owner()->Push(value);
4103 void TestContext::ReturnValue(HValue* value) {
4108 void EffectContext::ReturnInstruction(HInstruction* instr, BailoutId ast_id) {
4109 DCHECK(!instr->IsControlInstruction());
4110 owner()->AddInstruction(instr);
4111 if (instr->HasObservableSideEffects()) {
4112 owner()->Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
4117 void EffectContext::ReturnControl(HControlInstruction* instr,
4119 DCHECK(!instr->HasObservableSideEffects());
4120 HBasicBlock* empty_true = owner()->graph()->CreateBasicBlock();
4121 HBasicBlock* empty_false = owner()->graph()->CreateBasicBlock();
4122 instr->SetSuccessorAt(0, empty_true);
4123 instr->SetSuccessorAt(1, empty_false);
4124 owner()->FinishCurrentBlock(instr);
4125 HBasicBlock* join = owner()->CreateJoin(empty_true, empty_false, ast_id);
4126 owner()->set_current_block(join);
4130 void EffectContext::ReturnContinuation(HIfContinuation* continuation,
4132 HBasicBlock* true_branch = NULL;
4133 HBasicBlock* false_branch = NULL;
4134 continuation->Continue(&true_branch, &false_branch);
4135 if (!continuation->IsTrueReachable()) {
4136 owner()->set_current_block(false_branch);
4137 } else if (!continuation->IsFalseReachable()) {
4138 owner()->set_current_block(true_branch);
4140 HBasicBlock* join = owner()->CreateJoin(true_branch, false_branch, ast_id);
4141 owner()->set_current_block(join);
4146 void ValueContext::ReturnInstruction(HInstruction* instr, BailoutId ast_id) {
4147 DCHECK(!instr->IsControlInstruction());
4148 if (!arguments_allowed() && instr->CheckFlag(HValue::kIsArguments)) {
4149 return owner()->Bailout(kBadValueContextForArgumentsObjectValue);
4151 owner()->AddInstruction(instr);
4152 owner()->Push(instr);
4153 if (instr->HasObservableSideEffects()) {
4154 owner()->Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
4159 void ValueContext::ReturnControl(HControlInstruction* instr, BailoutId ast_id) {
4160 DCHECK(!instr->HasObservableSideEffects());
4161 if (!arguments_allowed() && instr->CheckFlag(HValue::kIsArguments)) {
4162 return owner()->Bailout(kBadValueContextForArgumentsObjectValue);
4164 HBasicBlock* materialize_false = owner()->graph()->CreateBasicBlock();
4165 HBasicBlock* materialize_true = owner()->graph()->CreateBasicBlock();
4166 instr->SetSuccessorAt(0, materialize_true);
4167 instr->SetSuccessorAt(1, materialize_false);
4168 owner()->FinishCurrentBlock(instr);
4169 owner()->set_current_block(materialize_true);
4170 owner()->Push(owner()->graph()->GetConstantTrue());
4171 owner()->set_current_block(materialize_false);
4172 owner()->Push(owner()->graph()->GetConstantFalse());
4174 owner()->CreateJoin(materialize_true, materialize_false, ast_id);
4175 owner()->set_current_block(join);
4179 void ValueContext::ReturnContinuation(HIfContinuation* continuation,
4181 HBasicBlock* materialize_true = NULL;
4182 HBasicBlock* materialize_false = NULL;
4183 continuation->Continue(&materialize_true, &materialize_false);
4184 if (continuation->IsTrueReachable()) {
4185 owner()->set_current_block(materialize_true);
4186 owner()->Push(owner()->graph()->GetConstantTrue());
4187 owner()->set_current_block(materialize_true);
4189 if (continuation->IsFalseReachable()) {
4190 owner()->set_current_block(materialize_false);
4191 owner()->Push(owner()->graph()->GetConstantFalse());
4192 owner()->set_current_block(materialize_false);
4194 if (continuation->TrueAndFalseReachable()) {
4196 owner()->CreateJoin(materialize_true, materialize_false, ast_id);
4197 owner()->set_current_block(join);
4202 void TestContext::ReturnInstruction(HInstruction* instr, BailoutId ast_id) {
4203 DCHECK(!instr->IsControlInstruction());
4204 HOptimizedGraphBuilder* builder = owner();
4205 builder->AddInstruction(instr);
4206 // We expect a simulate after every expression with side effects, though
4207 // this one isn't actually needed (and wouldn't work if it were targeted).
4208 if (instr->HasObservableSideEffects()) {
4209 builder->Push(instr);
4210 builder->Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
4217 void TestContext::ReturnControl(HControlInstruction* instr, BailoutId ast_id) {
4218 DCHECK(!instr->HasObservableSideEffects());
4219 HBasicBlock* empty_true = owner()->graph()->CreateBasicBlock();
4220 HBasicBlock* empty_false = owner()->graph()->CreateBasicBlock();
4221 instr->SetSuccessorAt(0, empty_true);
4222 instr->SetSuccessorAt(1, empty_false);
4223 owner()->FinishCurrentBlock(instr);
4224 owner()->Goto(empty_true, if_true(), owner()->function_state());
4225 owner()->Goto(empty_false, if_false(), owner()->function_state());
4226 owner()->set_current_block(NULL);
4230 void TestContext::ReturnContinuation(HIfContinuation* continuation,
4232 HBasicBlock* true_branch = NULL;
4233 HBasicBlock* false_branch = NULL;
4234 continuation->Continue(&true_branch, &false_branch);
4235 if (continuation->IsTrueReachable()) {
4236 owner()->Goto(true_branch, if_true(), owner()->function_state());
4238 if (continuation->IsFalseReachable()) {
4239 owner()->Goto(false_branch, if_false(), owner()->function_state());
4241 owner()->set_current_block(NULL);
4245 void TestContext::BuildBranch(HValue* value) {
4246 // We expect the graph to be in edge-split form: there is no edge that
4247 // connects a branch node to a join node. We conservatively ensure that
4248 // property by always adding an empty block on the outgoing edges of this
4250 HOptimizedGraphBuilder* builder = owner();
4251 if (value != NULL && value->CheckFlag(HValue::kIsArguments)) {
4252 builder->Bailout(kArgumentsObjectValueInATestContext);
4254 ToBooleanStub::Types expected(condition()->to_boolean_types());
4255 ReturnControl(owner()->New<HBranch>(value, expected), BailoutId::None());
4259 // HOptimizedGraphBuilder infrastructure for bailing out and checking bailouts.
4260 #define CHECK_BAILOUT(call) \
4263 if (HasStackOverflow()) return; \
4267 #define CHECK_ALIVE(call) \
4270 if (HasStackOverflow() || current_block() == NULL) return; \
4274 #define CHECK_ALIVE_OR_RETURN(call, value) \
4277 if (HasStackOverflow() || current_block() == NULL) return value; \
4281 void HOptimizedGraphBuilder::Bailout(BailoutReason reason) {
4282 current_info()->AbortOptimization(reason);
4287 void HOptimizedGraphBuilder::VisitForEffect(Expression* expr) {
4288 EffectContext for_effect(this);
4293 void HOptimizedGraphBuilder::VisitForValue(Expression* expr,
4294 ArgumentsAllowedFlag flag) {
4295 ValueContext for_value(this, flag);
4300 void HOptimizedGraphBuilder::VisitForTypeOf(Expression* expr) {
4301 ValueContext for_value(this, ARGUMENTS_NOT_ALLOWED);
4302 for_value.set_typeof_mode(INSIDE_TYPEOF);
4307 void HOptimizedGraphBuilder::VisitForControl(Expression* expr,
4308 HBasicBlock* true_block,
4309 HBasicBlock* false_block) {
4310 TestContext for_test(this, expr, true_block, false_block);
4315 void HOptimizedGraphBuilder::VisitExpressions(
4316 ZoneList<Expression*>* exprs) {
4317 for (int i = 0; i < exprs->length(); ++i) {
4318 CHECK_ALIVE(VisitForValue(exprs->at(i)));
4323 void HOptimizedGraphBuilder::VisitExpressions(ZoneList<Expression*>* exprs,
4324 ArgumentsAllowedFlag flag) {
4325 for (int i = 0; i < exprs->length(); ++i) {
4326 CHECK_ALIVE(VisitForValue(exprs->at(i), flag));
4331 bool HOptimizedGraphBuilder::BuildGraph() {
4332 if (IsSubclassConstructor(current_info()->function()->kind())) {
4333 Bailout(kSuperReference);
4337 int slots = current_info()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
4338 if (current_info()->scope()->is_script_scope() && slots > 0) {
4339 Bailout(kScriptContext);
4343 Scope* scope = current_info()->scope();
4346 // Add an edge to the body entry. This is warty: the graph's start
4347 // environment will be used by the Lithium translation as the initial
4348 // environment on graph entry, but it has now been mutated by the
4349 // Hydrogen translation of the instructions in the start block. This
4350 // environment uses values which have not been defined yet. These
4351 // Hydrogen instructions will then be replayed by the Lithium
4352 // translation, so they cannot have an environment effect. The edge to
4353 // the body's entry block (along with some special logic for the start
4354 // block in HInstruction::InsertAfter) seals the start block from
4355 // getting unwanted instructions inserted.
4357 // TODO(kmillikin): Fix this. Stop mutating the initial environment.
4358 // Make the Hydrogen instructions in the initial block into Hydrogen
4359 // values (but not instructions), present in the initial environment and
4360 // not replayed by the Lithium translation.
4361 HEnvironment* initial_env = environment()->CopyWithoutHistory();
4362 HBasicBlock* body_entry = CreateBasicBlock(initial_env);
4364 body_entry->SetJoinId(BailoutId::FunctionEntry());
4365 set_current_block(body_entry);
4367 VisitDeclarations(scope->declarations());
4368 Add<HSimulate>(BailoutId::Declarations());
4370 Add<HStackCheck>(HStackCheck::kFunctionEntry);
4372 VisitStatements(current_info()->function()->body());
4373 if (HasStackOverflow()) return false;
4375 if (current_block() != NULL) {
4376 Add<HReturn>(graph()->GetConstantUndefined());
4377 set_current_block(NULL);
4380 // If the checksum of the number of type info changes is the same as the
4381 // last time this function was compiled, then this recompile is likely not
4382 // due to missing/inadequate type feedback, but rather too aggressive
4383 // optimization. Disable optimistic LICM in that case.
4384 Handle<Code> unoptimized_code(current_info()->shared_info()->code());
4385 DCHECK(unoptimized_code->kind() == Code::FUNCTION);
4386 Handle<TypeFeedbackInfo> type_info(
4387 TypeFeedbackInfo::cast(unoptimized_code->type_feedback_info()));
4388 int checksum = type_info->own_type_change_checksum();
4389 int composite_checksum = graph()->update_type_change_checksum(checksum);
4390 graph()->set_use_optimistic_licm(
4391 !type_info->matches_inlined_type_change_checksum(composite_checksum));
4392 type_info->set_inlined_type_change_checksum(composite_checksum);
4394 // Perform any necessary OSR-specific cleanups or changes to the graph.
4395 osr()->FinishGraph();
4401 bool HGraph::Optimize(BailoutReason* bailout_reason) {
4405 // We need to create a HConstant "zero" now so that GVN will fold every
4406 // zero-valued constant in the graph together.
4407 // The constant is needed to make idef-based bounds check work: the pass
4408 // evaluates relations with "zero" and that zero cannot be created after GVN.
4412 // Do a full verify after building the graph and computing dominators.
4416 if (FLAG_analyze_environment_liveness && maximum_environment_size() != 0) {
4417 Run<HEnvironmentLivenessAnalysisPhase>();
4420 if (!CheckConstPhiUses()) {
4421 *bailout_reason = kUnsupportedPhiUseOfConstVariable;
4424 Run<HRedundantPhiEliminationPhase>();
4425 if (!CheckArgumentsPhiUses()) {
4426 *bailout_reason = kUnsupportedPhiUseOfArguments;
4430 // Find and mark unreachable code to simplify optimizations, especially gvn,
4431 // where unreachable code could unnecessarily defeat LICM.
4432 Run<HMarkUnreachableBlocksPhase>();
4434 if (FLAG_dead_code_elimination) Run<HDeadCodeEliminationPhase>();
4435 if (FLAG_use_escape_analysis) Run<HEscapeAnalysisPhase>();
4437 if (FLAG_load_elimination) Run<HLoadEliminationPhase>();
4441 if (has_osr()) osr()->FinishOsrValues();
4443 Run<HInferRepresentationPhase>();
4445 // Remove HSimulate instructions that have turned out not to be needed
4446 // after all by folding them into the following HSimulate.
4447 // This must happen after inferring representations.
4448 Run<HMergeRemovableSimulatesPhase>();
4450 Run<HMarkDeoptimizeOnUndefinedPhase>();
4451 Run<HRepresentationChangesPhase>();
4453 Run<HInferTypesPhase>();
4455 // Must be performed before canonicalization to ensure that Canonicalize
4456 // will not remove semantically meaningful ToInt32 operations e.g. BIT_OR with
4458 Run<HUint32AnalysisPhase>();
4460 if (FLAG_use_canonicalizing) Run<HCanonicalizePhase>();
4462 if (FLAG_use_gvn) Run<HGlobalValueNumberingPhase>();
4464 if (FLAG_check_elimination) Run<HCheckEliminationPhase>();
4466 if (FLAG_store_elimination) Run<HStoreEliminationPhase>();
4468 Run<HRangeAnalysisPhase>();
4470 Run<HComputeChangeUndefinedToNaN>();
4472 // Eliminate redundant stack checks on backwards branches.
4473 Run<HStackCheckEliminationPhase>();
4475 if (FLAG_array_bounds_checks_elimination) Run<HBoundsCheckEliminationPhase>();
4476 if (FLAG_array_bounds_checks_hoisting) Run<HBoundsCheckHoistingPhase>();
4477 if (FLAG_array_index_dehoisting) Run<HDehoistIndexComputationsPhase>();
4478 if (FLAG_dead_code_elimination) Run<HDeadCodeEliminationPhase>();
4480 RestoreActualValues();
4482 // Find unreachable code a second time, GVN and other optimizations may have
4483 // made blocks unreachable that were previously reachable.
4484 Run<HMarkUnreachableBlocksPhase>();
4490 void HGraph::RestoreActualValues() {
4491 HPhase phase("H_Restore actual values", this);
4493 for (int block_index = 0; block_index < blocks()->length(); block_index++) {
4494 HBasicBlock* block = blocks()->at(block_index);
4497 for (int i = 0; i < block->phis()->length(); i++) {
4498 HPhi* phi = block->phis()->at(i);
4499 DCHECK(phi->ActualValue() == phi);
4503 for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
4504 HInstruction* instruction = it.Current();
4505 if (instruction->ActualValue() == instruction) continue;
4506 if (instruction->CheckFlag(HValue::kIsDead)) {
4507 // The instruction was marked as deleted but left in the graph
4508 // as a control flow dependency point for subsequent
4510 instruction->DeleteAndReplaceWith(instruction->ActualValue());
4512 DCHECK(instruction->IsInformativeDefinition());
4513 if (instruction->IsPurelyInformativeDefinition()) {
4514 instruction->DeleteAndReplaceWith(instruction->RedefinedOperand());
4516 instruction->ReplaceAllUsesWith(instruction->ActualValue());
4524 void HOptimizedGraphBuilder::PushArgumentsFromEnvironment(int count) {
4525 ZoneList<HValue*> arguments(count, zone());
4526 for (int i = 0; i < count; ++i) {
4527 arguments.Add(Pop(), zone());
4530 HPushArguments* push_args = New<HPushArguments>();
4531 while (!arguments.is_empty()) {
4532 push_args->AddInput(arguments.RemoveLast());
4534 AddInstruction(push_args);
4538 template <class Instruction>
4539 HInstruction* HOptimizedGraphBuilder::PreProcessCall(Instruction* call) {
4540 PushArgumentsFromEnvironment(call->argument_count());
4545 void HOptimizedGraphBuilder::SetUpScope(Scope* scope) {
4546 // First special is HContext.
4547 HInstruction* context = Add<HContext>();
4548 environment()->BindContext(context);
4550 // Create an arguments object containing the initial parameters. Set the
4551 // initial values of parameters including "this" having parameter index 0.
4552 DCHECK_EQ(scope->num_parameters() + 1, environment()->parameter_count());
4553 HArgumentsObject* arguments_object =
4554 New<HArgumentsObject>(environment()->parameter_count());
4555 for (int i = 0; i < environment()->parameter_count(); ++i) {
4556 HInstruction* parameter = Add<HParameter>(i);
4557 arguments_object->AddArgument(parameter, zone());
4558 environment()->Bind(i, parameter);
4560 AddInstruction(arguments_object);
4561 graph()->SetArgumentsObject(arguments_object);
4563 HConstant* undefined_constant = graph()->GetConstantUndefined();
4564 // Initialize specials and locals to undefined.
4565 for (int i = environment()->parameter_count() + 1;
4566 i < environment()->length();
4568 environment()->Bind(i, undefined_constant);
4571 // Handle the arguments and arguments shadow variables specially (they do
4572 // not have declarations).
4573 if (scope->arguments() != NULL) {
4574 environment()->Bind(scope->arguments(),
4575 graph()->GetArgumentsObject());
4579 Variable* rest = scope->rest_parameter(&rest_index);
4581 return Bailout(kRestParameter);
4584 if (scope->this_function_var() != nullptr ||
4585 scope->new_target_var() != nullptr) {
4586 return Bailout(kSuperReference);
4591 void HOptimizedGraphBuilder::VisitStatements(ZoneList<Statement*>* statements) {
4592 for (int i = 0; i < statements->length(); i++) {
4593 Statement* stmt = statements->at(i);
4594 CHECK_ALIVE(Visit(stmt));
4595 if (stmt->IsJump()) break;
4600 void HOptimizedGraphBuilder::VisitBlock(Block* stmt) {
4601 DCHECK(!HasStackOverflow());
4602 DCHECK(current_block() != NULL);
4603 DCHECK(current_block()->HasPredecessor());
4605 Scope* outer_scope = scope();
4606 Scope* scope = stmt->scope();
4607 BreakAndContinueInfo break_info(stmt, outer_scope);
4609 { BreakAndContinueScope push(&break_info, this);
4610 if (scope != NULL) {
4611 if (scope->ContextLocalCount() > 0) {
4612 // Load the function object.
4613 Scope* declaration_scope = scope->DeclarationScope();
4614 HInstruction* function;
4615 HValue* outer_context = environment()->context();
4616 if (declaration_scope->is_script_scope() ||
4617 declaration_scope->is_eval_scope()) {
4618 function = new (zone())
4619 HLoadContextSlot(outer_context, Context::CLOSURE_INDEX,
4620 HLoadContextSlot::kNoCheck);
4622 function = New<HThisFunction>();
4624 AddInstruction(function);
4625 // Allocate a block context and store it to the stack frame.
4626 HInstruction* inner_context = Add<HAllocateBlockContext>(
4627 outer_context, function, scope->GetScopeInfo(isolate()));
4628 HInstruction* instr = Add<HStoreFrameContext>(inner_context);
4630 environment()->BindContext(inner_context);
4631 if (instr->HasObservableSideEffects()) {
4632 AddSimulate(stmt->EntryId(), REMOVABLE_SIMULATE);
4635 VisitDeclarations(scope->declarations());
4636 AddSimulate(stmt->DeclsId(), REMOVABLE_SIMULATE);
4638 CHECK_BAILOUT(VisitStatements(stmt->statements()));
4640 set_scope(outer_scope);
4641 if (scope != NULL && current_block() != NULL &&
4642 scope->ContextLocalCount() > 0) {
4643 HValue* inner_context = environment()->context();
4644 HValue* outer_context = Add<HLoadNamedField>(
4645 inner_context, nullptr,
4646 HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX));
4648 HInstruction* instr = Add<HStoreFrameContext>(outer_context);
4649 environment()->BindContext(outer_context);
4650 if (instr->HasObservableSideEffects()) {
4651 AddSimulate(stmt->ExitId(), REMOVABLE_SIMULATE);
4654 HBasicBlock* break_block = break_info.break_block();
4655 if (break_block != NULL) {
4656 if (current_block() != NULL) Goto(break_block);
4657 break_block->SetJoinId(stmt->ExitId());
4658 set_current_block(break_block);
4663 void HOptimizedGraphBuilder::VisitExpressionStatement(
4664 ExpressionStatement* stmt) {
4665 DCHECK(!HasStackOverflow());
4666 DCHECK(current_block() != NULL);
4667 DCHECK(current_block()->HasPredecessor());
4668 VisitForEffect(stmt->expression());
4672 void HOptimizedGraphBuilder::VisitEmptyStatement(EmptyStatement* stmt) {
4673 DCHECK(!HasStackOverflow());
4674 DCHECK(current_block() != NULL);
4675 DCHECK(current_block()->HasPredecessor());
4679 void HOptimizedGraphBuilder::VisitIfStatement(IfStatement* stmt) {
4680 DCHECK(!HasStackOverflow());
4681 DCHECK(current_block() != NULL);
4682 DCHECK(current_block()->HasPredecessor());
4683 if (stmt->condition()->ToBooleanIsTrue()) {
4684 Add<HSimulate>(stmt->ThenId());
4685 Visit(stmt->then_statement());
4686 } else if (stmt->condition()->ToBooleanIsFalse()) {
4687 Add<HSimulate>(stmt->ElseId());
4688 Visit(stmt->else_statement());
4690 HBasicBlock* cond_true = graph()->CreateBasicBlock();
4691 HBasicBlock* cond_false = graph()->CreateBasicBlock();
4692 CHECK_BAILOUT(VisitForControl(stmt->condition(), cond_true, cond_false));
4694 if (cond_true->HasPredecessor()) {
4695 cond_true->SetJoinId(stmt->ThenId());
4696 set_current_block(cond_true);
4697 CHECK_BAILOUT(Visit(stmt->then_statement()));
4698 cond_true = current_block();
4703 if (cond_false->HasPredecessor()) {
4704 cond_false->SetJoinId(stmt->ElseId());
4705 set_current_block(cond_false);
4706 CHECK_BAILOUT(Visit(stmt->else_statement()));
4707 cond_false = current_block();
4712 HBasicBlock* join = CreateJoin(cond_true, cond_false, stmt->IfId());
4713 set_current_block(join);
4718 HBasicBlock* HOptimizedGraphBuilder::BreakAndContinueScope::Get(
4719 BreakableStatement* stmt,
4724 BreakAndContinueScope* current = this;
4725 while (current != NULL && current->info()->target() != stmt) {
4726 *drop_extra += current->info()->drop_extra();
4727 current = current->next();
4729 DCHECK(current != NULL); // Always found (unless stack is malformed).
4730 *scope = current->info()->scope();
4732 if (type == BREAK) {
4733 *drop_extra += current->info()->drop_extra();
4736 HBasicBlock* block = NULL;
4739 block = current->info()->break_block();
4740 if (block == NULL) {
4741 block = current->owner()->graph()->CreateBasicBlock();
4742 current->info()->set_break_block(block);
4747 block = current->info()->continue_block();
4748 if (block == NULL) {
4749 block = current->owner()->graph()->CreateBasicBlock();
4750 current->info()->set_continue_block(block);
4759 void HOptimizedGraphBuilder::VisitContinueStatement(
4760 ContinueStatement* stmt) {
4761 DCHECK(!HasStackOverflow());
4762 DCHECK(current_block() != NULL);
4763 DCHECK(current_block()->HasPredecessor());
4764 Scope* outer_scope = NULL;
4765 Scope* inner_scope = scope();
4767 HBasicBlock* continue_block = break_scope()->Get(
4768 stmt->target(), BreakAndContinueScope::CONTINUE,
4769 &outer_scope, &drop_extra);
4770 HValue* context = environment()->context();
4772 int context_pop_count = inner_scope->ContextChainLength(outer_scope);
4773 if (context_pop_count > 0) {
4774 while (context_pop_count-- > 0) {
4775 HInstruction* context_instruction = Add<HLoadNamedField>(
4777 HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX));
4778 context = context_instruction;
4780 HInstruction* instr = Add<HStoreFrameContext>(context);
4781 if (instr->HasObservableSideEffects()) {
4782 AddSimulate(stmt->target()->EntryId(), REMOVABLE_SIMULATE);
4784 environment()->BindContext(context);
4787 Goto(continue_block);
4788 set_current_block(NULL);
4792 void HOptimizedGraphBuilder::VisitBreakStatement(BreakStatement* stmt) {
4793 DCHECK(!HasStackOverflow());
4794 DCHECK(current_block() != NULL);
4795 DCHECK(current_block()->HasPredecessor());
4796 Scope* outer_scope = NULL;
4797 Scope* inner_scope = scope();
4799 HBasicBlock* break_block = break_scope()->Get(
4800 stmt->target(), BreakAndContinueScope::BREAK,
4801 &outer_scope, &drop_extra);
4802 HValue* context = environment()->context();
4804 int context_pop_count = inner_scope->ContextChainLength(outer_scope);
4805 if (context_pop_count > 0) {
4806 while (context_pop_count-- > 0) {
4807 HInstruction* context_instruction = Add<HLoadNamedField>(
4809 HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX));
4810 context = context_instruction;
4812 HInstruction* instr = Add<HStoreFrameContext>(context);
4813 if (instr->HasObservableSideEffects()) {
4814 AddSimulate(stmt->target()->ExitId(), REMOVABLE_SIMULATE);
4816 environment()->BindContext(context);
4819 set_current_block(NULL);
4823 void HOptimizedGraphBuilder::VisitReturnStatement(ReturnStatement* stmt) {
4824 DCHECK(!HasStackOverflow());
4825 DCHECK(current_block() != NULL);
4826 DCHECK(current_block()->HasPredecessor());
4827 FunctionState* state = function_state();
4828 AstContext* context = call_context();
4829 if (context == NULL) {
4830 // Not an inlined return, so an actual one.
4831 CHECK_ALIVE(VisitForValue(stmt->expression()));
4832 HValue* result = environment()->Pop();
4833 Add<HReturn>(result);
4834 } else if (state->inlining_kind() == CONSTRUCT_CALL_RETURN) {
4835 // Return from an inlined construct call. In a test context the return value
4836 // will always evaluate to true, in a value context the return value needs
4837 // to be a JSObject.
4838 if (context->IsTest()) {
4839 TestContext* test = TestContext::cast(context);
4840 CHECK_ALIVE(VisitForEffect(stmt->expression()));
4841 Goto(test->if_true(), state);
4842 } else if (context->IsEffect()) {
4843 CHECK_ALIVE(VisitForEffect(stmt->expression()));
4844 Goto(function_return(), state);
4846 DCHECK(context->IsValue());
4847 CHECK_ALIVE(VisitForValue(stmt->expression()));
4848 HValue* return_value = Pop();
4849 HValue* receiver = environment()->arguments_environment()->Lookup(0);
4850 HHasInstanceTypeAndBranch* typecheck =
4851 New<HHasInstanceTypeAndBranch>(return_value,
4852 FIRST_SPEC_OBJECT_TYPE,
4853 LAST_SPEC_OBJECT_TYPE);
4854 HBasicBlock* if_spec_object = graph()->CreateBasicBlock();
4855 HBasicBlock* not_spec_object = graph()->CreateBasicBlock();
4856 typecheck->SetSuccessorAt(0, if_spec_object);
4857 typecheck->SetSuccessorAt(1, not_spec_object);
4858 FinishCurrentBlock(typecheck);
4859 AddLeaveInlined(if_spec_object, return_value, state);
4860 AddLeaveInlined(not_spec_object, receiver, state);
4862 } else if (state->inlining_kind() == SETTER_CALL_RETURN) {
4863 // Return from an inlined setter call. The returned value is never used, the
4864 // value of an assignment is always the value of the RHS of the assignment.
4865 CHECK_ALIVE(VisitForEffect(stmt->expression()));
4866 if (context->IsTest()) {
4867 HValue* rhs = environment()->arguments_environment()->Lookup(1);
4868 context->ReturnValue(rhs);
4869 } else if (context->IsEffect()) {
4870 Goto(function_return(), state);
4872 DCHECK(context->IsValue());
4873 HValue* rhs = environment()->arguments_environment()->Lookup(1);
4874 AddLeaveInlined(rhs, state);
4877 // Return from a normal inlined function. Visit the subexpression in the
4878 // expression context of the call.
4879 if (context->IsTest()) {
4880 TestContext* test = TestContext::cast(context);
4881 VisitForControl(stmt->expression(), test->if_true(), test->if_false());
4882 } else if (context->IsEffect()) {
4883 // Visit in value context and ignore the result. This is needed to keep
4884 // environment in sync with full-codegen since some visitors (e.g.
4885 // VisitCountOperation) use the operand stack differently depending on
4887 CHECK_ALIVE(VisitForValue(stmt->expression()));
4889 Goto(function_return(), state);
4891 DCHECK(context->IsValue());
4892 CHECK_ALIVE(VisitForValue(stmt->expression()));
4893 AddLeaveInlined(Pop(), state);
4896 set_current_block(NULL);
4900 void HOptimizedGraphBuilder::VisitWithStatement(WithStatement* stmt) {
4901 DCHECK(!HasStackOverflow());
4902 DCHECK(current_block() != NULL);
4903 DCHECK(current_block()->HasPredecessor());
4904 return Bailout(kWithStatement);
4908 void HOptimizedGraphBuilder::VisitSwitchStatement(SwitchStatement* stmt) {
4909 DCHECK(!HasStackOverflow());
4910 DCHECK(current_block() != NULL);
4911 DCHECK(current_block()->HasPredecessor());
4913 ZoneList<CaseClause*>* clauses = stmt->cases();
4914 int clause_count = clauses->length();
4915 ZoneList<HBasicBlock*> body_blocks(clause_count, zone());
4917 CHECK_ALIVE(VisitForValue(stmt->tag()));
4918 Add<HSimulate>(stmt->EntryId());
4919 HValue* tag_value = Top();
4920 Type* tag_type = stmt->tag()->bounds().lower;
4922 // 1. Build all the tests, with dangling true branches
4923 BailoutId default_id = BailoutId::None();
4924 for (int i = 0; i < clause_count; ++i) {
4925 CaseClause* clause = clauses->at(i);
4926 if (clause->is_default()) {
4927 body_blocks.Add(NULL, zone());
4928 if (default_id.IsNone()) default_id = clause->EntryId();
4932 // Generate a compare and branch.
4933 CHECK_ALIVE(VisitForValue(clause->label()));
4934 HValue* label_value = Pop();
4936 Type* label_type = clause->label()->bounds().lower;
4937 Type* combined_type = clause->compare_type();
4938 HControlInstruction* compare = BuildCompareInstruction(
4939 Token::EQ_STRICT, tag_value, label_value, tag_type, label_type,
4941 ScriptPositionToSourcePosition(stmt->tag()->position()),
4942 ScriptPositionToSourcePosition(clause->label()->position()),
4943 PUSH_BEFORE_SIMULATE, clause->id());
4945 HBasicBlock* next_test_block = graph()->CreateBasicBlock();
4946 HBasicBlock* body_block = graph()->CreateBasicBlock();
4947 body_blocks.Add(body_block, zone());
4948 compare->SetSuccessorAt(0, body_block);
4949 compare->SetSuccessorAt(1, next_test_block);
4950 FinishCurrentBlock(compare);
4952 set_current_block(body_block);
4953 Drop(1); // tag_value
4955 set_current_block(next_test_block);
4958 // Save the current block to use for the default or to join with the
4960 HBasicBlock* last_block = current_block();
4961 Drop(1); // tag_value
4963 // 2. Loop over the clauses and the linked list of tests in lockstep,
4964 // translating the clause bodies.
4965 HBasicBlock* fall_through_block = NULL;
4967 BreakAndContinueInfo break_info(stmt, scope());
4968 { BreakAndContinueScope push(&break_info, this);
4969 for (int i = 0; i < clause_count; ++i) {
4970 CaseClause* clause = clauses->at(i);
4972 // Identify the block where normal (non-fall-through) control flow
4974 HBasicBlock* normal_block = NULL;
4975 if (clause->is_default()) {
4976 if (last_block == NULL) continue;
4977 normal_block = last_block;
4978 last_block = NULL; // Cleared to indicate we've handled it.
4980 normal_block = body_blocks[i];
4983 if (fall_through_block == NULL) {
4984 set_current_block(normal_block);
4986 HBasicBlock* join = CreateJoin(fall_through_block,
4989 set_current_block(join);
4992 CHECK_BAILOUT(VisitStatements(clause->statements()));
4993 fall_through_block = current_block();
4997 // Create an up-to-3-way join. Use the break block if it exists since
4998 // it's already a join block.
4999 HBasicBlock* break_block = break_info.break_block();
5000 if (break_block == NULL) {
5001 set_current_block(CreateJoin(fall_through_block,
5005 if (fall_through_block != NULL) Goto(fall_through_block, break_block);
5006 if (last_block != NULL) Goto(last_block, break_block);
5007 break_block->SetJoinId(stmt->ExitId());
5008 set_current_block(break_block);
5013 void HOptimizedGraphBuilder::VisitLoopBody(IterationStatement* stmt,
5014 HBasicBlock* loop_entry) {
5015 Add<HSimulate>(stmt->StackCheckId());
5016 HStackCheck* stack_check =
5017 HStackCheck::cast(Add<HStackCheck>(HStackCheck::kBackwardsBranch));
5018 DCHECK(loop_entry->IsLoopHeader());
5019 loop_entry->loop_information()->set_stack_check(stack_check);
5020 CHECK_BAILOUT(Visit(stmt->body()));
5024 void HOptimizedGraphBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) {
5025 DCHECK(!HasStackOverflow());
5026 DCHECK(current_block() != NULL);
5027 DCHECK(current_block()->HasPredecessor());
5028 DCHECK(current_block() != NULL);
5029 HBasicBlock* loop_entry = BuildLoopEntry(stmt);
5031 BreakAndContinueInfo break_info(stmt, scope());
5033 BreakAndContinueScope push(&break_info, this);
5034 CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry));
5036 HBasicBlock* body_exit =
5037 JoinContinue(stmt, current_block(), break_info.continue_block());
5038 HBasicBlock* loop_successor = NULL;
5039 if (body_exit != NULL && !stmt->cond()->ToBooleanIsTrue()) {
5040 set_current_block(body_exit);
5041 loop_successor = graph()->CreateBasicBlock();
5042 if (stmt->cond()->ToBooleanIsFalse()) {
5043 loop_entry->loop_information()->stack_check()->Eliminate();
5044 Goto(loop_successor);
5047 // The block for a true condition, the actual predecessor block of the
5049 body_exit = graph()->CreateBasicBlock();
5050 CHECK_BAILOUT(VisitForControl(stmt->cond(), body_exit, loop_successor));
5052 if (body_exit != NULL && body_exit->HasPredecessor()) {
5053 body_exit->SetJoinId(stmt->BackEdgeId());
5057 if (loop_successor->HasPredecessor()) {
5058 loop_successor->SetJoinId(stmt->ExitId());
5060 loop_successor = NULL;
5063 HBasicBlock* loop_exit = CreateLoop(stmt,
5067 break_info.break_block());
5068 set_current_block(loop_exit);
5072 void HOptimizedGraphBuilder::VisitWhileStatement(WhileStatement* stmt) {
5073 DCHECK(!HasStackOverflow());
5074 DCHECK(current_block() != NULL);
5075 DCHECK(current_block()->HasPredecessor());
5076 DCHECK(current_block() != NULL);
5077 HBasicBlock* loop_entry = BuildLoopEntry(stmt);
5079 // If the condition is constant true, do not generate a branch.
5080 HBasicBlock* loop_successor = NULL;
5081 if (!stmt->cond()->ToBooleanIsTrue()) {
5082 HBasicBlock* body_entry = graph()->CreateBasicBlock();
5083 loop_successor = graph()->CreateBasicBlock();
5084 CHECK_BAILOUT(VisitForControl(stmt->cond(), body_entry, loop_successor));
5085 if (body_entry->HasPredecessor()) {
5086 body_entry->SetJoinId(stmt->BodyId());
5087 set_current_block(body_entry);
5089 if (loop_successor->HasPredecessor()) {
5090 loop_successor->SetJoinId(stmt->ExitId());
5092 loop_successor = NULL;
5096 BreakAndContinueInfo break_info(stmt, scope());
5097 if (current_block() != NULL) {
5098 BreakAndContinueScope push(&break_info, this);
5099 CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry));
5101 HBasicBlock* body_exit =
5102 JoinContinue(stmt, current_block(), break_info.continue_block());
5103 HBasicBlock* loop_exit = CreateLoop(stmt,
5107 break_info.break_block());
5108 set_current_block(loop_exit);
5112 void HOptimizedGraphBuilder::VisitForStatement(ForStatement* stmt) {
5113 DCHECK(!HasStackOverflow());
5114 DCHECK(current_block() != NULL);
5115 DCHECK(current_block()->HasPredecessor());
5116 if (stmt->init() != NULL) {
5117 CHECK_ALIVE(Visit(stmt->init()));
5119 DCHECK(current_block() != NULL);
5120 HBasicBlock* loop_entry = BuildLoopEntry(stmt);
5122 HBasicBlock* loop_successor = NULL;
5123 if (stmt->cond() != NULL) {
5124 HBasicBlock* body_entry = graph()->CreateBasicBlock();
5125 loop_successor = graph()->CreateBasicBlock();
5126 CHECK_BAILOUT(VisitForControl(stmt->cond(), body_entry, loop_successor));
5127 if (body_entry->HasPredecessor()) {
5128 body_entry->SetJoinId(stmt->BodyId());
5129 set_current_block(body_entry);
5131 if (loop_successor->HasPredecessor()) {
5132 loop_successor->SetJoinId(stmt->ExitId());
5134 loop_successor = NULL;
5138 BreakAndContinueInfo break_info(stmt, scope());
5139 if (current_block() != NULL) {
5140 BreakAndContinueScope push(&break_info, this);
5141 CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry));
5143 HBasicBlock* body_exit =
5144 JoinContinue(stmt, current_block(), break_info.continue_block());
5146 if (stmt->next() != NULL && body_exit != NULL) {
5147 set_current_block(body_exit);
5148 CHECK_BAILOUT(Visit(stmt->next()));
5149 body_exit = current_block();
5152 HBasicBlock* loop_exit = CreateLoop(stmt,
5156 break_info.break_block());
5157 set_current_block(loop_exit);
5161 void HOptimizedGraphBuilder::VisitForInStatement(ForInStatement* stmt) {
5162 DCHECK(!HasStackOverflow());
5163 DCHECK(current_block() != NULL);
5164 DCHECK(current_block()->HasPredecessor());
5166 if (!FLAG_optimize_for_in) {
5167 return Bailout(kForInStatementOptimizationIsDisabled);
5170 if (!stmt->each()->IsVariableProxy() ||
5171 !stmt->each()->AsVariableProxy()->var()->IsStackLocal()) {
5172 return Bailout(kForInStatementWithNonLocalEachVariable);
5175 Variable* each_var = stmt->each()->AsVariableProxy()->var();
5177 CHECK_ALIVE(VisitForValue(stmt->enumerable()));
5178 HValue* enumerable = Top(); // Leave enumerable at the top.
5180 IfBuilder if_undefined_or_null(this);
5181 if_undefined_or_null.If<HCompareObjectEqAndBranch>(
5182 enumerable, graph()->GetConstantUndefined());
5183 if_undefined_or_null.Or();
5184 if_undefined_or_null.If<HCompareObjectEqAndBranch>(
5185 enumerable, graph()->GetConstantNull());
5186 if_undefined_or_null.ThenDeopt(Deoptimizer::kUndefinedOrNullInForIn);
5187 if_undefined_or_null.End();
5188 BuildForInBody(stmt, each_var, enumerable);
5192 void HOptimizedGraphBuilder::BuildForInBody(ForInStatement* stmt,
5194 HValue* enumerable) {
5196 HInstruction* array;
5197 HInstruction* enum_length;
5198 bool fast = stmt->for_in_type() == ForInStatement::FAST_FOR_IN;
5200 map = Add<HForInPrepareMap>(enumerable);
5201 Add<HSimulate>(stmt->PrepareId());
5203 array = Add<HForInCacheArray>(enumerable, map,
5204 DescriptorArray::kEnumCacheBridgeCacheIndex);
5205 enum_length = Add<HMapEnumLength>(map);
5207 HInstruction* index_cache = Add<HForInCacheArray>(
5208 enumerable, map, DescriptorArray::kEnumCacheBridgeIndicesCacheIndex);
5209 HForInCacheArray::cast(array)
5210 ->set_index_cache(HForInCacheArray::cast(index_cache));
5212 Add<HSimulate>(stmt->PrepareId());
5214 NoObservableSideEffectsScope no_effects(this);
5215 BuildJSObjectCheck(enumerable, 0);
5217 Add<HSimulate>(stmt->ToObjectId());
5219 map = graph()->GetConstant1();
5220 Runtime::FunctionId function_id = Runtime::kGetPropertyNamesFast;
5221 Add<HPushArguments>(enumerable);
5222 array = Add<HCallRuntime>(isolate()->factory()->empty_string(),
5223 Runtime::FunctionForId(function_id), 1);
5225 Add<HSimulate>(stmt->EnumId());
5227 Handle<Map> array_map = isolate()->factory()->fixed_array_map();
5228 HValue* check = Add<HCheckMaps>(array, array_map);
5229 enum_length = AddLoadFixedArrayLength(array, check);
5232 HInstruction* start_index = Add<HConstant>(0);
5239 HBasicBlock* loop_entry = BuildLoopEntry(stmt);
5241 // Reload the values to ensure we have up-to-date values inside of the loop.
5242 // This is relevant especially for OSR where the values don't come from the
5243 // computation above, but from the OSR entry block.
5244 enumerable = environment()->ExpressionStackAt(4);
5245 HValue* index = environment()->ExpressionStackAt(0);
5246 HValue* limit = environment()->ExpressionStackAt(1);
5248 // Check that we still have more keys.
5249 HCompareNumericAndBranch* compare_index =
5250 New<HCompareNumericAndBranch>(index, limit, Token::LT);
5251 compare_index->set_observed_input_representation(
5252 Representation::Smi(), Representation::Smi());
5254 HBasicBlock* loop_body = graph()->CreateBasicBlock();
5255 HBasicBlock* loop_successor = graph()->CreateBasicBlock();
5257 compare_index->SetSuccessorAt(0, loop_body);
5258 compare_index->SetSuccessorAt(1, loop_successor);
5259 FinishCurrentBlock(compare_index);
5261 set_current_block(loop_successor);
5264 set_current_block(loop_body);
5267 Add<HLoadKeyed>(environment()->ExpressionStackAt(2), // Enum cache.
5268 index, index, FAST_ELEMENTS);
5271 // Check if the expected map still matches that of the enumerable.
5272 // If not just deoptimize.
5273 Add<HCheckMapValue>(enumerable, environment()->ExpressionStackAt(3));
5274 Bind(each_var, key);
5276 Add<HPushArguments>(enumerable, key);
5277 Runtime::FunctionId function_id = Runtime::kForInFilter;
5278 key = Add<HCallRuntime>(isolate()->factory()->empty_string(),
5279 Runtime::FunctionForId(function_id), 2);
5281 Add<HSimulate>(stmt->FilterId());
5283 Bind(each_var, key);
5284 IfBuilder if_undefined(this);
5285 if_undefined.If<HCompareObjectEqAndBranch>(key,
5286 graph()->GetConstantUndefined());
5287 if_undefined.ThenDeopt(Deoptimizer::kUndefined);
5289 Add<HSimulate>(stmt->AssignmentId());
5292 BreakAndContinueInfo break_info(stmt, scope(), 5);
5294 BreakAndContinueScope push(&break_info, this);
5295 CHECK_BAILOUT(VisitLoopBody(stmt, loop_entry));
5298 HBasicBlock* body_exit =
5299 JoinContinue(stmt, current_block(), break_info.continue_block());
5301 if (body_exit != NULL) {
5302 set_current_block(body_exit);
5304 HValue* current_index = Pop();
5305 Push(AddUncasted<HAdd>(current_index, graph()->GetConstant1()));
5306 body_exit = current_block();
5309 HBasicBlock* loop_exit = CreateLoop(stmt,
5313 break_info.break_block());
5315 set_current_block(loop_exit);
5319 void HOptimizedGraphBuilder::VisitForOfStatement(ForOfStatement* stmt) {
5320 DCHECK(!HasStackOverflow());
5321 DCHECK(current_block() != NULL);
5322 DCHECK(current_block()->HasPredecessor());
5323 return Bailout(kForOfStatement);
5327 void HOptimizedGraphBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) {
5328 DCHECK(!HasStackOverflow());
5329 DCHECK(current_block() != NULL);
5330 DCHECK(current_block()->HasPredecessor());
5331 return Bailout(kTryCatchStatement);
5335 void HOptimizedGraphBuilder::VisitTryFinallyStatement(
5336 TryFinallyStatement* stmt) {
5337 DCHECK(!HasStackOverflow());
5338 DCHECK(current_block() != NULL);
5339 DCHECK(current_block()->HasPredecessor());
5340 return Bailout(kTryFinallyStatement);
5344 void HOptimizedGraphBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) {
5345 DCHECK(!HasStackOverflow());
5346 DCHECK(current_block() != NULL);
5347 DCHECK(current_block()->HasPredecessor());
5348 return Bailout(kDebuggerStatement);
5352 void HOptimizedGraphBuilder::VisitCaseClause(CaseClause* clause) {
5357 void HOptimizedGraphBuilder::VisitFunctionLiteral(FunctionLiteral* expr) {
5358 DCHECK(!HasStackOverflow());
5359 DCHECK(current_block() != NULL);
5360 DCHECK(current_block()->HasPredecessor());
5361 Handle<SharedFunctionInfo> shared_info = Compiler::GetSharedFunctionInfo(
5362 expr, current_info()->script(), top_info());
5363 // We also have a stack overflow if the recursive compilation did.
5364 if (HasStackOverflow()) return;
5365 HFunctionLiteral* instr =
5366 New<HFunctionLiteral>(shared_info, expr->pretenure());
5367 return ast_context()->ReturnInstruction(instr, expr->id());
5371 void HOptimizedGraphBuilder::VisitClassLiteral(ClassLiteral* lit) {
5372 DCHECK(!HasStackOverflow());
5373 DCHECK(current_block() != NULL);
5374 DCHECK(current_block()->HasPredecessor());
5375 return Bailout(kClassLiteral);
5379 void HOptimizedGraphBuilder::VisitNativeFunctionLiteral(
5380 NativeFunctionLiteral* expr) {
5381 DCHECK(!HasStackOverflow());
5382 DCHECK(current_block() != NULL);
5383 DCHECK(current_block()->HasPredecessor());
5384 return Bailout(kNativeFunctionLiteral);
5388 void HOptimizedGraphBuilder::VisitConditional(Conditional* expr) {
5389 DCHECK(!HasStackOverflow());
5390 DCHECK(current_block() != NULL);
5391 DCHECK(current_block()->HasPredecessor());
5392 HBasicBlock* cond_true = graph()->CreateBasicBlock();
5393 HBasicBlock* cond_false = graph()->CreateBasicBlock();
5394 CHECK_BAILOUT(VisitForControl(expr->condition(), cond_true, cond_false));
5396 // Visit the true and false subexpressions in the same AST context as the
5397 // whole expression.
5398 if (cond_true->HasPredecessor()) {
5399 cond_true->SetJoinId(expr->ThenId());
5400 set_current_block(cond_true);
5401 CHECK_BAILOUT(Visit(expr->then_expression()));
5402 cond_true = current_block();
5407 if (cond_false->HasPredecessor()) {
5408 cond_false->SetJoinId(expr->ElseId());
5409 set_current_block(cond_false);
5410 CHECK_BAILOUT(Visit(expr->else_expression()));
5411 cond_false = current_block();
5416 if (!ast_context()->IsTest()) {
5417 HBasicBlock* join = CreateJoin(cond_true, cond_false, expr->id());
5418 set_current_block(join);
5419 if (join != NULL && !ast_context()->IsEffect()) {
5420 return ast_context()->ReturnValue(Pop());
5426 HOptimizedGraphBuilder::GlobalPropertyAccess
5427 HOptimizedGraphBuilder::LookupGlobalProperty(Variable* var, LookupIterator* it,
5428 PropertyAccessType access_type) {
5429 if (var->is_this() || !current_info()->has_global_object()) {
5433 switch (it->state()) {
5434 case LookupIterator::ACCESSOR:
5435 case LookupIterator::ACCESS_CHECK:
5436 case LookupIterator::INTERCEPTOR:
5437 case LookupIterator::INTEGER_INDEXED_EXOTIC:
5438 case LookupIterator::NOT_FOUND:
5440 case LookupIterator::DATA:
5441 if (access_type == STORE && it->IsReadOnly()) return kUseGeneric;
5443 case LookupIterator::JSPROXY:
5444 case LookupIterator::TRANSITION:
5452 HValue* HOptimizedGraphBuilder::BuildContextChainWalk(Variable* var) {
5453 DCHECK(var->IsContextSlot());
5454 HValue* context = environment()->context();
5455 int length = scope()->ContextChainLength(var->scope());
5456 while (length-- > 0) {
5457 context = Add<HLoadNamedField>(
5459 HObjectAccess::ForContextSlot(Context::PREVIOUS_INDEX));
5465 void HOptimizedGraphBuilder::VisitVariableProxy(VariableProxy* expr) {
5466 DCHECK(!HasStackOverflow());
5467 DCHECK(current_block() != NULL);
5468 DCHECK(current_block()->HasPredecessor());
5469 Variable* variable = expr->var();
5470 switch (variable->location()) {
5471 case VariableLocation::GLOBAL:
5472 case VariableLocation::UNALLOCATED: {
5473 if (IsLexicalVariableMode(variable->mode())) {
5474 // TODO(rossberg): should this be an DCHECK?
5475 return Bailout(kReferenceToGlobalLexicalVariable);
5477 // Handle known global constants like 'undefined' specially to avoid a
5478 // load from a global cell for them.
5479 Handle<Object> constant_value =
5480 isolate()->factory()->GlobalConstantFor(variable->name());
5481 if (!constant_value.is_null()) {
5482 HConstant* instr = New<HConstant>(constant_value);
5483 return ast_context()->ReturnInstruction(instr, expr->id());
5486 Handle<GlobalObject> global(current_info()->global_object());
5488 // Lookup in script contexts.
5490 Handle<ScriptContextTable> script_contexts(
5491 global->native_context()->script_context_table());
5492 ScriptContextTable::LookupResult lookup;
5493 if (ScriptContextTable::Lookup(script_contexts, variable->name(),
5495 Handle<Context> script_context = ScriptContextTable::GetContext(
5496 script_contexts, lookup.context_index);
5497 Handle<Object> current_value =
5498 FixedArray::get(script_context, lookup.slot_index);
5500 // If the values is not the hole, it will stay initialized,
5501 // so no need to generate a check.
5502 if (*current_value == *isolate()->factory()->the_hole_value()) {
5503 return Bailout(kReferenceToUninitializedVariable);
5505 HInstruction* result = New<HLoadNamedField>(
5506 Add<HConstant>(script_context), nullptr,
5507 HObjectAccess::ForContextSlot(lookup.slot_index));
5508 return ast_context()->ReturnInstruction(result, expr->id());
5512 LookupIterator it(global, variable->name(), LookupIterator::OWN);
5513 GlobalPropertyAccess type = LookupGlobalProperty(variable, &it, LOAD);
5515 if (type == kUseCell) {
5516 Handle<PropertyCell> cell = it.GetPropertyCell();
5517 top_info()->dependencies()->AssumePropertyCell(cell);
5518 auto cell_type = it.property_details().cell_type();
5519 if (cell_type == PropertyCellType::kConstant ||
5520 cell_type == PropertyCellType::kUndefined) {
5521 Handle<Object> constant_object(cell->value(), isolate());
5522 if (constant_object->IsConsString()) {
5524 String::Flatten(Handle<String>::cast(constant_object));
5526 HConstant* constant = New<HConstant>(constant_object);
5527 return ast_context()->ReturnInstruction(constant, expr->id());
5529 auto access = HObjectAccess::ForPropertyCellValue();
5530 UniqueSet<Map>* field_maps = nullptr;
5531 if (cell_type == PropertyCellType::kConstantType) {
5532 switch (cell->GetConstantType()) {
5533 case PropertyCellConstantType::kSmi:
5534 access = access.WithRepresentation(Representation::Smi());
5536 case PropertyCellConstantType::kStableMap: {
5537 // Check that the map really is stable. The heap object could
5538 // have mutated without the cell updating state. In that case,
5539 // make no promises about the loaded value except that it's a
5542 access.WithRepresentation(Representation::HeapObject());
5543 Handle<Map> map(HeapObject::cast(cell->value())->map());
5544 if (map->is_stable()) {
5545 field_maps = new (zone())
5546 UniqueSet<Map>(Unique<Map>::CreateImmovable(map), zone());
5552 HConstant* cell_constant = Add<HConstant>(cell);
5553 HLoadNamedField* instr;
5554 if (field_maps == nullptr) {
5555 instr = New<HLoadNamedField>(cell_constant, nullptr, access);
5557 instr = New<HLoadNamedField>(cell_constant, nullptr, access,
5558 field_maps, HType::HeapObject());
5560 instr->ClearDependsOnFlag(kInobjectFields);
5561 instr->SetDependsOnFlag(kGlobalVars);
5562 return ast_context()->ReturnInstruction(instr, expr->id());
5564 } else if (variable->IsGlobalSlot()) {
5565 DCHECK(variable->index() > 0);
5566 DCHECK(variable->IsStaticGlobalObjectProperty());
5567 // Each var occupies two slots in the context: for reads and writes.
5568 int slot_index = variable->index();
5569 int depth = scope()->ContextChainLength(variable->scope());
5571 HLoadGlobalViaContext* instr =
5572 New<HLoadGlobalViaContext>(depth, slot_index);
5573 return ast_context()->ReturnInstruction(instr, expr->id());
5576 HValue* global_object = Add<HLoadNamedField>(
5578 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
5579 HLoadGlobalGeneric* instr = New<HLoadGlobalGeneric>(
5580 global_object, variable->name(), ast_context()->typeof_mode());
5581 instr->SetVectorAndSlot(handle(current_feedback_vector(), isolate()),
5582 expr->VariableFeedbackSlot());
5583 return ast_context()->ReturnInstruction(instr, expr->id());
5587 case VariableLocation::PARAMETER:
5588 case VariableLocation::LOCAL: {
5589 HValue* value = LookupAndMakeLive(variable);
5590 if (value == graph()->GetConstantHole()) {
5591 DCHECK(IsDeclaredVariableMode(variable->mode()) &&
5592 variable->mode() != VAR);
5593 return Bailout(kReferenceToUninitializedVariable);
5595 return ast_context()->ReturnValue(value);
5598 case VariableLocation::CONTEXT: {
5599 HValue* context = BuildContextChainWalk(variable);
5600 HLoadContextSlot::Mode mode;
5601 switch (variable->mode()) {
5604 mode = HLoadContextSlot::kCheckDeoptimize;
5607 mode = HLoadContextSlot::kCheckReturnUndefined;
5610 mode = HLoadContextSlot::kNoCheck;
5613 HLoadContextSlot* instr =
5614 new(zone()) HLoadContextSlot(context, variable->index(), mode);
5615 return ast_context()->ReturnInstruction(instr, expr->id());
5618 case VariableLocation::LOOKUP:
5619 return Bailout(kReferenceToAVariableWhichRequiresDynamicLookup);
5624 void HOptimizedGraphBuilder::VisitLiteral(Literal* expr) {
5625 DCHECK(!HasStackOverflow());
5626 DCHECK(current_block() != NULL);
5627 DCHECK(current_block()->HasPredecessor());
5628 HConstant* instr = New<HConstant>(expr->value());
5629 return ast_context()->ReturnInstruction(instr, expr->id());
5633 void HOptimizedGraphBuilder::VisitRegExpLiteral(RegExpLiteral* expr) {
5634 DCHECK(!HasStackOverflow());
5635 DCHECK(current_block() != NULL);
5636 DCHECK(current_block()->HasPredecessor());
5637 Handle<JSFunction> closure = function_state()->compilation_info()->closure();
5638 Handle<FixedArray> literals(closure->literals());
5639 HRegExpLiteral* instr = New<HRegExpLiteral>(literals,
5642 expr->literal_index());
5643 return ast_context()->ReturnInstruction(instr, expr->id());
5647 static bool CanInlinePropertyAccess(Handle<Map> map) {
5648 if (map->instance_type() == HEAP_NUMBER_TYPE) return true;
5649 if (map->instance_type() < FIRST_NONSTRING_TYPE) return true;
5650 return map->IsJSObjectMap() && !map->is_dictionary_map() &&
5651 !map->has_named_interceptor() &&
5652 // TODO(verwaest): Whitelist contexts to which we have access.
5653 !map->is_access_check_needed();
5657 // Determines whether the given array or object literal boilerplate satisfies
5658 // all limits to be considered for fast deep-copying and computes the total
5659 // size of all objects that are part of the graph.
5660 static bool IsFastLiteral(Handle<JSObject> boilerplate,
5662 int* max_properties) {
5663 if (boilerplate->map()->is_deprecated() &&
5664 !JSObject::TryMigrateInstance(boilerplate)) {
5668 DCHECK(max_depth >= 0 && *max_properties >= 0);
5669 if (max_depth == 0) return false;
5671 Isolate* isolate = boilerplate->GetIsolate();
5672 Handle<FixedArrayBase> elements(boilerplate->elements());
5673 if (elements->length() > 0 &&
5674 elements->map() != isolate->heap()->fixed_cow_array_map()) {
5675 if (boilerplate->HasFastSmiOrObjectElements()) {
5676 Handle<FixedArray> fast_elements = Handle<FixedArray>::cast(elements);
5677 int length = elements->length();
5678 for (int i = 0; i < length; i++) {
5679 if ((*max_properties)-- == 0) return false;
5680 Handle<Object> value(fast_elements->get(i), isolate);
5681 if (value->IsJSObject()) {
5682 Handle<JSObject> value_object = Handle<JSObject>::cast(value);
5683 if (!IsFastLiteral(value_object,
5690 } else if (!boilerplate->HasFastDoubleElements()) {
5695 Handle<FixedArray> properties(boilerplate->properties());
5696 if (properties->length() > 0) {
5699 Handle<DescriptorArray> descriptors(
5700 boilerplate->map()->instance_descriptors());
5701 int limit = boilerplate->map()->NumberOfOwnDescriptors();
5702 for (int i = 0; i < limit; i++) {
5703 PropertyDetails details = descriptors->GetDetails(i);
5704 if (details.type() != DATA) continue;
5705 if ((*max_properties)-- == 0) return false;
5706 FieldIndex field_index = FieldIndex::ForDescriptor(boilerplate->map(), i);
5707 if (boilerplate->IsUnboxedDoubleField(field_index)) continue;
5708 Handle<Object> value(boilerplate->RawFastPropertyAt(field_index),
5710 if (value->IsJSObject()) {
5711 Handle<JSObject> value_object = Handle<JSObject>::cast(value);
5712 if (!IsFastLiteral(value_object,
5724 void HOptimizedGraphBuilder::VisitObjectLiteral(ObjectLiteral* expr) {
5725 DCHECK(!HasStackOverflow());
5726 DCHECK(current_block() != NULL);
5727 DCHECK(current_block()->HasPredecessor());
5729 Handle<JSFunction> closure = function_state()->compilation_info()->closure();
5730 HInstruction* literal;
5732 // Check whether to use fast or slow deep-copying for boilerplate.
5733 int max_properties = kMaxFastLiteralProperties;
5734 Handle<Object> literals_cell(closure->literals()->get(expr->literal_index()),
5736 Handle<AllocationSite> site;
5737 Handle<JSObject> boilerplate;
5738 if (!literals_cell->IsUndefined()) {
5739 // Retrieve the boilerplate
5740 site = Handle<AllocationSite>::cast(literals_cell);
5741 boilerplate = Handle<JSObject>(JSObject::cast(site->transition_info()),
5745 if (!boilerplate.is_null() &&
5746 IsFastLiteral(boilerplate, kMaxFastLiteralDepth, &max_properties)) {
5747 AllocationSiteUsageContext site_context(isolate(), site, false);
5748 site_context.EnterNewScope();
5749 literal = BuildFastLiteral(boilerplate, &site_context);
5750 site_context.ExitScope(site, boilerplate);
5752 NoObservableSideEffectsScope no_effects(this);
5753 Handle<FixedArray> closure_literals(closure->literals(), isolate());
5754 Handle<FixedArray> constant_properties = expr->constant_properties();
5755 int literal_index = expr->literal_index();
5756 int flags = expr->ComputeFlags(true);
5758 Add<HPushArguments>(Add<HConstant>(closure_literals),
5759 Add<HConstant>(literal_index),
5760 Add<HConstant>(constant_properties),
5761 Add<HConstant>(flags));
5763 Runtime::FunctionId function_id = Runtime::kCreateObjectLiteral;
5764 literal = Add<HCallRuntime>(isolate()->factory()->empty_string(),
5765 Runtime::FunctionForId(function_id),
5769 // The object is expected in the bailout environment during computation
5770 // of the property values and is the value of the entire expression.
5773 for (int i = 0; i < expr->properties()->length(); i++) {
5774 ObjectLiteral::Property* property = expr->properties()->at(i);
5775 if (property->is_computed_name()) return Bailout(kComputedPropertyName);
5776 if (property->IsCompileTimeValue()) continue;
5778 Literal* key = property->key()->AsLiteral();
5779 Expression* value = property->value();
5781 switch (property->kind()) {
5782 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
5783 DCHECK(!CompileTimeValue::IsCompileTimeValue(value));
5785 case ObjectLiteral::Property::COMPUTED:
5786 // It is safe to use [[Put]] here because the boilerplate already
5787 // contains computed properties with an uninitialized value.
5788 if (key->value()->IsInternalizedString()) {
5789 if (property->emit_store()) {
5790 CHECK_ALIVE(VisitForValue(value));
5791 HValue* value = Pop();
5793 // Add [[HomeObject]] to function literals.
5794 if (FunctionLiteral::NeedsHomeObject(property->value())) {
5795 Handle<Symbol> sym = isolate()->factory()->home_object_symbol();
5796 HInstruction* store_home = BuildKeyedGeneric(
5797 STORE, NULL, value, Add<HConstant>(sym), literal);
5798 AddInstruction(store_home);
5799 DCHECK(store_home->HasObservableSideEffects());
5800 Add<HSimulate>(property->value()->id(), REMOVABLE_SIMULATE);
5803 Handle<Map> map = property->GetReceiverType();
5804 Handle<String> name = key->AsPropertyName();
5806 if (map.is_null()) {
5807 // If we don't know the monomorphic type, do a generic store.
5808 CHECK_ALIVE(store = BuildNamedGeneric(
5809 STORE, NULL, literal, name, value));
5811 PropertyAccessInfo info(this, STORE, map, name);
5812 if (info.CanAccessMonomorphic()) {
5813 HValue* checked_literal = Add<HCheckMaps>(literal, map);
5814 DCHECK(!info.IsAccessorConstant());
5815 store = BuildMonomorphicAccess(
5816 &info, literal, checked_literal, value,
5817 BailoutId::None(), BailoutId::None());
5819 CHECK_ALIVE(store = BuildNamedGeneric(
5820 STORE, NULL, literal, name, value));
5823 if (store->IsInstruction()) {
5824 AddInstruction(HInstruction::cast(store));
5826 DCHECK(store->HasObservableSideEffects());
5827 Add<HSimulate>(key->id(), REMOVABLE_SIMULATE);
5829 CHECK_ALIVE(VisitForEffect(value));
5834 case ObjectLiteral::Property::PROTOTYPE:
5835 case ObjectLiteral::Property::SETTER:
5836 case ObjectLiteral::Property::GETTER:
5837 return Bailout(kObjectLiteralWithComplexProperty);
5838 default: UNREACHABLE();
5842 if (expr->has_function()) {
5843 // Return the result of the transformation to fast properties
5844 // instead of the original since this operation changes the map
5845 // of the object. This makes sure that the original object won't
5846 // be used by other optimized code before it is transformed
5847 // (e.g. because of code motion).
5848 HToFastProperties* result = Add<HToFastProperties>(Pop());
5849 return ast_context()->ReturnValue(result);
5851 return ast_context()->ReturnValue(Pop());
5856 void HOptimizedGraphBuilder::VisitArrayLiteral(ArrayLiteral* expr) {
5857 DCHECK(!HasStackOverflow());
5858 DCHECK(current_block() != NULL);
5859 DCHECK(current_block()->HasPredecessor());
5860 expr->BuildConstantElements(isolate());
5861 ZoneList<Expression*>* subexprs = expr->values();
5862 int length = subexprs->length();
5863 HInstruction* literal;
5865 Handle<AllocationSite> site;
5866 Handle<FixedArray> literals(environment()->closure()->literals(), isolate());
5867 bool uninitialized = false;
5868 Handle<Object> literals_cell(literals->get(expr->literal_index()),
5870 Handle<JSObject> boilerplate_object;
5871 if (literals_cell->IsUndefined()) {
5872 uninitialized = true;
5873 Handle<Object> raw_boilerplate;
5874 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
5875 isolate(), raw_boilerplate,
5876 Runtime::CreateArrayLiteralBoilerplate(
5877 isolate(), literals, expr->constant_elements(),
5878 is_strong(function_language_mode())),
5879 Bailout(kArrayBoilerplateCreationFailed));
5881 boilerplate_object = Handle<JSObject>::cast(raw_boilerplate);
5882 AllocationSiteCreationContext creation_context(isolate());
5883 site = creation_context.EnterNewScope();
5884 if (JSObject::DeepWalk(boilerplate_object, &creation_context).is_null()) {
5885 return Bailout(kArrayBoilerplateCreationFailed);
5887 creation_context.ExitScope(site, boilerplate_object);
5888 literals->set(expr->literal_index(), *site);
5890 if (boilerplate_object->elements()->map() ==
5891 isolate()->heap()->fixed_cow_array_map()) {
5892 isolate()->counters()->cow_arrays_created_runtime()->Increment();
5895 DCHECK(literals_cell->IsAllocationSite());
5896 site = Handle<AllocationSite>::cast(literals_cell);
5897 boilerplate_object = Handle<JSObject>(
5898 JSObject::cast(site->transition_info()), isolate());
5901 DCHECK(!boilerplate_object.is_null());
5902 DCHECK(site->SitePointsToLiteral());
5904 ElementsKind boilerplate_elements_kind =
5905 boilerplate_object->GetElementsKind();
5907 // Check whether to use fast or slow deep-copying for boilerplate.
5908 int max_properties = kMaxFastLiteralProperties;
5909 if (IsFastLiteral(boilerplate_object,
5910 kMaxFastLiteralDepth,
5912 AllocationSiteUsageContext site_context(isolate(), site, false);
5913 site_context.EnterNewScope();
5914 literal = BuildFastLiteral(boilerplate_object, &site_context);
5915 site_context.ExitScope(site, boilerplate_object);
5917 NoObservableSideEffectsScope no_effects(this);
5918 // Boilerplate already exists and constant elements are never accessed,
5919 // pass an empty fixed array to the runtime function instead.
5920 Handle<FixedArray> constants = isolate()->factory()->empty_fixed_array();
5921 int literal_index = expr->literal_index();
5922 int flags = expr->ComputeFlags(true);
5924 Add<HPushArguments>(Add<HConstant>(literals),
5925 Add<HConstant>(literal_index),
5926 Add<HConstant>(constants),
5927 Add<HConstant>(flags));
5929 Runtime::FunctionId function_id = Runtime::kCreateArrayLiteral;
5930 literal = Add<HCallRuntime>(isolate()->factory()->empty_string(),
5931 Runtime::FunctionForId(function_id),
5934 // Register to deopt if the boilerplate ElementsKind changes.
5935 top_info()->dependencies()->AssumeTransitionStable(site);
5938 // The array is expected in the bailout environment during computation
5939 // of the property values and is the value of the entire expression.
5941 // The literal index is on the stack, too.
5942 Push(Add<HConstant>(expr->literal_index()));
5944 HInstruction* elements = NULL;
5946 for (int i = 0; i < length; i++) {
5947 Expression* subexpr = subexprs->at(i);
5948 if (subexpr->IsSpread()) {
5949 return Bailout(kSpread);
5952 // If the subexpression is a literal or a simple materialized literal it
5953 // is already set in the cloned array.
5954 if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
5956 CHECK_ALIVE(VisitForValue(subexpr));
5957 HValue* value = Pop();
5958 if (!Smi::IsValid(i)) return Bailout(kNonSmiKeyInArrayLiteral);
5960 elements = AddLoadElements(literal);
5962 HValue* key = Add<HConstant>(i);
5964 switch (boilerplate_elements_kind) {
5965 case FAST_SMI_ELEMENTS:
5966 case FAST_HOLEY_SMI_ELEMENTS:
5968 case FAST_HOLEY_ELEMENTS:
5969 case FAST_DOUBLE_ELEMENTS:
5970 case FAST_HOLEY_DOUBLE_ELEMENTS: {
5971 HStoreKeyed* instr = Add<HStoreKeyed>(elements, key, value,
5972 boilerplate_elements_kind);
5973 instr->SetUninitialized(uninitialized);
5981 Add<HSimulate>(expr->GetIdForElement(i));
5984 Drop(1); // array literal index
5985 return ast_context()->ReturnValue(Pop());
5989 HCheckMaps* HOptimizedGraphBuilder::AddCheckMap(HValue* object,
5991 BuildCheckHeapObject(object);
5992 return Add<HCheckMaps>(object, map);
5996 HInstruction* HOptimizedGraphBuilder::BuildLoadNamedField(
5997 PropertyAccessInfo* info,
5998 HValue* checked_object) {
5999 // See if this is a load for an immutable property
6000 if (checked_object->ActualValue()->IsConstant()) {
6001 Handle<Object> object(
6002 HConstant::cast(checked_object->ActualValue())->handle(isolate()));
6004 if (object->IsJSObject()) {
6005 LookupIterator it(object, info->name(),
6006 LookupIterator::OWN_SKIP_INTERCEPTOR);
6007 Handle<Object> value = JSReceiver::GetDataProperty(&it);
6008 if (it.IsFound() && it.IsReadOnly() && !it.IsConfigurable()) {
6009 return New<HConstant>(value);
6014 HObjectAccess access = info->access();
6015 if (access.representation().IsDouble() &&
6016 (!FLAG_unbox_double_fields || !access.IsInobject())) {
6017 // Load the heap number.
6018 checked_object = Add<HLoadNamedField>(
6019 checked_object, nullptr,
6020 access.WithRepresentation(Representation::Tagged()));
6021 // Load the double value from it.
6022 access = HObjectAccess::ForHeapNumberValue();
6025 SmallMapList* map_list = info->field_maps();
6026 if (map_list->length() == 0) {
6027 return New<HLoadNamedField>(checked_object, checked_object, access);
6030 UniqueSet<Map>* maps = new(zone()) UniqueSet<Map>(map_list->length(), zone());
6031 for (int i = 0; i < map_list->length(); ++i) {
6032 maps->Add(Unique<Map>::CreateImmovable(map_list->at(i)), zone());
6034 return New<HLoadNamedField>(
6035 checked_object, checked_object, access, maps, info->field_type());
6039 HInstruction* HOptimizedGraphBuilder::BuildStoreNamedField(
6040 PropertyAccessInfo* info,
6041 HValue* checked_object,
6043 bool transition_to_field = info->IsTransition();
6044 // TODO(verwaest): Move this logic into PropertyAccessInfo.
6045 HObjectAccess field_access = info->access();
6047 HStoreNamedField *instr;
6048 if (field_access.representation().IsDouble() &&
6049 (!FLAG_unbox_double_fields || !field_access.IsInobject())) {
6050 HObjectAccess heap_number_access =
6051 field_access.WithRepresentation(Representation::Tagged());
6052 if (transition_to_field) {
6053 // The store requires a mutable HeapNumber to be allocated.
6054 NoObservableSideEffectsScope no_side_effects(this);
6055 HInstruction* heap_number_size = Add<HConstant>(HeapNumber::kSize);
6057 // TODO(hpayer): Allocation site pretenuring support.
6058 HInstruction* heap_number = Add<HAllocate>(heap_number_size,
6059 HType::HeapObject(),
6061 MUTABLE_HEAP_NUMBER_TYPE);
6062 AddStoreMapConstant(
6063 heap_number, isolate()->factory()->mutable_heap_number_map());
6064 Add<HStoreNamedField>(heap_number, HObjectAccess::ForHeapNumberValue(),
6066 instr = New<HStoreNamedField>(checked_object->ActualValue(),
6070 // Already holds a HeapNumber; load the box and write its value field.
6071 HInstruction* heap_number =
6072 Add<HLoadNamedField>(checked_object, nullptr, heap_number_access);
6073 instr = New<HStoreNamedField>(heap_number,
6074 HObjectAccess::ForHeapNumberValue(),
6075 value, STORE_TO_INITIALIZED_ENTRY);
6078 if (field_access.representation().IsHeapObject()) {
6079 BuildCheckHeapObject(value);
6082 if (!info->field_maps()->is_empty()) {
6083 DCHECK(field_access.representation().IsHeapObject());
6084 value = Add<HCheckMaps>(value, info->field_maps());
6087 // This is a normal store.
6088 instr = New<HStoreNamedField>(
6089 checked_object->ActualValue(), field_access, value,
6090 transition_to_field ? INITIALIZING_STORE : STORE_TO_INITIALIZED_ENTRY);
6093 if (transition_to_field) {
6094 Handle<Map> transition(info->transition());
6095 DCHECK(!transition->is_deprecated());
6096 instr->SetTransition(Add<HConstant>(transition));
6102 bool HOptimizedGraphBuilder::PropertyAccessInfo::IsCompatible(
6103 PropertyAccessInfo* info) {
6104 if (!CanInlinePropertyAccess(map_)) return false;
6106 // Currently only handle Type::Number as a polymorphic case.
6107 // TODO(verwaest): Support monomorphic handling of numbers with a HCheckNumber
6109 if (IsNumberType()) return false;
6111 // Values are only compatible for monomorphic load if they all behave the same
6112 // regarding value wrappers.
6113 if (IsValueWrapped() != info->IsValueWrapped()) return false;
6115 if (!LookupDescriptor()) return false;
6118 return (!info->IsFound() || info->has_holder()) &&
6119 map()->prototype() == info->map()->prototype();
6122 // Mismatch if the other access info found the property in the prototype
6124 if (info->has_holder()) return false;
6126 if (IsAccessorConstant()) {
6127 return accessor_.is_identical_to(info->accessor_) &&
6128 api_holder_.is_identical_to(info->api_holder_);
6131 if (IsDataConstant()) {
6132 return constant_.is_identical_to(info->constant_);
6136 if (!info->IsData()) return false;
6138 Representation r = access_.representation();
6140 if (!info->access_.representation().IsCompatibleForLoad(r)) return false;
6142 if (!info->access_.representation().IsCompatibleForStore(r)) return false;
6144 if (info->access_.offset() != access_.offset()) return false;
6145 if (info->access_.IsInobject() != access_.IsInobject()) return false;
6147 if (field_maps_.is_empty()) {
6148 info->field_maps_.Clear();
6149 } else if (!info->field_maps_.is_empty()) {
6150 for (int i = 0; i < field_maps_.length(); ++i) {
6151 info->field_maps_.AddMapIfMissing(field_maps_.at(i), info->zone());
6153 info->field_maps_.Sort();
6156 // We can only merge stores that agree on their field maps. The comparison
6157 // below is safe, since we keep the field maps sorted.
6158 if (field_maps_.length() != info->field_maps_.length()) return false;
6159 for (int i = 0; i < field_maps_.length(); ++i) {
6160 if (!field_maps_.at(i).is_identical_to(info->field_maps_.at(i))) {
6165 info->GeneralizeRepresentation(r);
6166 info->field_type_ = info->field_type_.Combine(field_type_);
6171 bool HOptimizedGraphBuilder::PropertyAccessInfo::LookupDescriptor() {
6172 if (!map_->IsJSObjectMap()) return true;
6173 LookupDescriptor(*map_, *name_);
6174 return LoadResult(map_);
6178 bool HOptimizedGraphBuilder::PropertyAccessInfo::LoadResult(Handle<Map> map) {
6179 if (!IsLoad() && IsProperty() && IsReadOnly()) {
6184 // Construct the object field access.
6185 int index = GetLocalFieldIndexFromMap(map);
6186 access_ = HObjectAccess::ForField(map, index, representation(), name_);
6188 // Load field map for heap objects.
6189 return LoadFieldMaps(map);
6190 } else if (IsAccessorConstant()) {
6191 Handle<Object> accessors = GetAccessorsFromMap(map);
6192 if (!accessors->IsAccessorPair()) return false;
6193 Object* raw_accessor =
6194 IsLoad() ? Handle<AccessorPair>::cast(accessors)->getter()
6195 : Handle<AccessorPair>::cast(accessors)->setter();
6196 if (!raw_accessor->IsJSFunction()) return false;
6197 Handle<JSFunction> accessor = handle(JSFunction::cast(raw_accessor));
6198 if (accessor->shared()->IsApiFunction()) {
6199 CallOptimization call_optimization(accessor);
6200 if (call_optimization.is_simple_api_call()) {
6201 CallOptimization::HolderLookup holder_lookup;
6203 call_optimization.LookupHolderOfExpectedType(map_, &holder_lookup);
6206 accessor_ = accessor;
6207 } else if (IsDataConstant()) {
6208 constant_ = GetConstantFromMap(map);
6215 bool HOptimizedGraphBuilder::PropertyAccessInfo::LoadFieldMaps(
6217 // Clear any previously collected field maps/type.
6218 field_maps_.Clear();
6219 field_type_ = HType::Tagged();
6221 // Figure out the field type from the accessor map.
6222 Handle<HeapType> field_type = GetFieldTypeFromMap(map);
6224 // Collect the (stable) maps from the field type.
6225 int num_field_maps = field_type->NumClasses();
6226 if (num_field_maps > 0) {
6227 DCHECK(access_.representation().IsHeapObject());
6228 field_maps_.Reserve(num_field_maps, zone());
6229 HeapType::Iterator<Map> it = field_type->Classes();
6230 while (!it.Done()) {
6231 Handle<Map> field_map = it.Current();
6232 if (!field_map->is_stable()) {
6233 field_maps_.Clear();
6236 field_maps_.Add(field_map, zone());
6241 if (field_maps_.is_empty()) {
6242 // Store is not safe if the field map was cleared.
6243 return IsLoad() || !field_type->Is(HeapType::None());
6247 DCHECK_EQ(num_field_maps, field_maps_.length());
6249 // Determine field HType from field HeapType.
6250 field_type_ = HType::FromType<HeapType>(field_type);
6251 DCHECK(field_type_.IsHeapObject());
6253 // Add dependency on the map that introduced the field.
6254 top_info()->dependencies()->AssumeFieldType(GetFieldOwnerFromMap(map));
6259 bool HOptimizedGraphBuilder::PropertyAccessInfo::LookupInPrototypes() {
6260 Handle<Map> map = this->map();
6262 while (map->prototype()->IsJSObject()) {
6263 holder_ = handle(JSObject::cast(map->prototype()));
6264 if (holder_->map()->is_deprecated()) {
6265 JSObject::TryMigrateInstance(holder_);
6267 map = Handle<Map>(holder_->map());
6268 if (!CanInlinePropertyAccess(map)) {
6272 LookupDescriptor(*map, *name_);
6273 if (IsFound()) return LoadResult(map);
6277 return !map->prototype()->IsJSReceiver();
6281 bool HOptimizedGraphBuilder::PropertyAccessInfo::IsIntegerIndexedExotic() {
6282 InstanceType instance_type = map_->instance_type();
6283 return instance_type == JS_TYPED_ARRAY_TYPE &&
6284 IsSpecialIndex(isolate()->unicode_cache(), *name_);
6288 bool HOptimizedGraphBuilder::PropertyAccessInfo::CanAccessMonomorphic() {
6289 if (!CanInlinePropertyAccess(map_)) return false;
6290 if (IsJSObjectFieldAccessor()) return IsLoad();
6291 if (IsJSArrayBufferViewFieldAccessor()) return IsLoad();
6292 if (map_->function_with_prototype() && !map_->has_non_instance_prototype() &&
6293 name_.is_identical_to(isolate()->factory()->prototype_string())) {
6296 if (!LookupDescriptor()) return false;
6297 if (IsFound()) return IsLoad() || !IsReadOnly();
6298 if (IsIntegerIndexedExotic()) return false;
6299 if (!LookupInPrototypes()) return false;
6300 if (IsLoad()) return true;
6302 if (IsAccessorConstant()) return true;
6303 LookupTransition(*map_, *name_, NONE);
6304 if (IsTransitionToData() && map_->unused_property_fields() > 0) {
6305 // Construct the object field access.
6306 int descriptor = transition()->LastAdded();
6308 transition()->instance_descriptors()->GetFieldIndex(descriptor) -
6309 map_->inobject_properties();
6310 PropertyDetails details =
6311 transition()->instance_descriptors()->GetDetails(descriptor);
6312 Representation representation = details.representation();
6313 access_ = HObjectAccess::ForField(map_, index, representation, name_);
6315 // Load field map for heap objects.
6316 return LoadFieldMaps(transition());
6322 bool HOptimizedGraphBuilder::PropertyAccessInfo::CanAccessAsMonomorphic(
6323 SmallMapList* maps) {
6324 DCHECK(map_.is_identical_to(maps->first()));
6325 if (!CanAccessMonomorphic()) return false;
6326 STATIC_ASSERT(kMaxLoadPolymorphism == kMaxStorePolymorphism);
6327 if (maps->length() > kMaxLoadPolymorphism) return false;
6328 HObjectAccess access = HObjectAccess::ForMap(); // bogus default
6329 if (GetJSObjectFieldAccess(&access)) {
6330 for (int i = 1; i < maps->length(); ++i) {
6331 PropertyAccessInfo test_info(builder_, access_type_, maps->at(i), name_);
6332 HObjectAccess test_access = HObjectAccess::ForMap(); // bogus default
6333 if (!test_info.GetJSObjectFieldAccess(&test_access)) return false;
6334 if (!access.Equals(test_access)) return false;
6338 if (GetJSArrayBufferViewFieldAccess(&access)) {
6339 for (int i = 1; i < maps->length(); ++i) {
6340 PropertyAccessInfo test_info(builder_, access_type_, maps->at(i), name_);
6341 HObjectAccess test_access = HObjectAccess::ForMap(); // bogus default
6342 if (!test_info.GetJSArrayBufferViewFieldAccess(&test_access)) {
6345 if (!access.Equals(test_access)) return false;
6350 // Currently only handle numbers as a polymorphic case.
6351 // TODO(verwaest): Support monomorphic handling of numbers with a HCheckNumber
6353 if (IsNumberType()) return false;
6355 // Multiple maps cannot transition to the same target map.
6356 DCHECK(!IsLoad() || !IsTransition());
6357 if (IsTransition() && maps->length() > 1) return false;
6359 for (int i = 1; i < maps->length(); ++i) {
6360 PropertyAccessInfo test_info(builder_, access_type_, maps->at(i), name_);
6361 if (!test_info.IsCompatible(this)) return false;
6368 Handle<Map> HOptimizedGraphBuilder::PropertyAccessInfo::map() {
6369 JSFunction* ctor = IC::GetRootConstructor(
6370 *map_, current_info()->closure()->context()->native_context());
6371 if (ctor != NULL) return handle(ctor->initial_map());
6376 static bool NeedsWrapping(Handle<Map> map, Handle<JSFunction> target) {
6377 return !map->IsJSObjectMap() &&
6378 is_sloppy(target->shared()->language_mode()) &&
6379 !target->shared()->native();
6383 bool HOptimizedGraphBuilder::PropertyAccessInfo::NeedsWrappingFor(
6384 Handle<JSFunction> target) const {
6385 return NeedsWrapping(map_, target);
6389 HValue* HOptimizedGraphBuilder::BuildMonomorphicAccess(
6390 PropertyAccessInfo* info, HValue* object, HValue* checked_object,
6391 HValue* value, BailoutId ast_id, BailoutId return_id,
6392 bool can_inline_accessor) {
6393 HObjectAccess access = HObjectAccess::ForMap(); // bogus default
6394 if (info->GetJSObjectFieldAccess(&access)) {
6395 DCHECK(info->IsLoad());
6396 return New<HLoadNamedField>(object, checked_object, access);
6399 if (info->GetJSArrayBufferViewFieldAccess(&access)) {
6400 DCHECK(info->IsLoad());
6401 checked_object = Add<HCheckArrayBufferNotNeutered>(checked_object);
6402 return New<HLoadNamedField>(object, checked_object, access);
6405 if (info->name().is_identical_to(isolate()->factory()->prototype_string()) &&
6406 info->map()->function_with_prototype()) {
6407 DCHECK(!info->map()->has_non_instance_prototype());
6408 return New<HLoadFunctionPrototype>(checked_object);
6411 HValue* checked_holder = checked_object;
6412 if (info->has_holder()) {
6413 Handle<JSObject> prototype(JSObject::cast(info->map()->prototype()));
6414 checked_holder = BuildCheckPrototypeMaps(prototype, info->holder());
6417 if (!info->IsFound()) {
6418 DCHECK(info->IsLoad());
6419 if (is_strong(function_language_mode())) {
6420 return New<HCallRuntime>(
6421 isolate()->factory()->empty_string(),
6422 Runtime::FunctionForId(Runtime::kThrowStrongModeImplicitConversion),
6425 return graph()->GetConstantUndefined();
6429 if (info->IsData()) {
6430 if (info->IsLoad()) {
6431 return BuildLoadNamedField(info, checked_holder);
6433 return BuildStoreNamedField(info, checked_object, value);
6437 if (info->IsTransition()) {
6438 DCHECK(!info->IsLoad());
6439 return BuildStoreNamedField(info, checked_object, value);
6442 if (info->IsAccessorConstant()) {
6443 Push(checked_object);
6444 int argument_count = 1;
6445 if (!info->IsLoad()) {
6450 if (info->NeedsWrappingFor(info->accessor())) {
6451 HValue* function = Add<HConstant>(info->accessor());
6452 PushArgumentsFromEnvironment(argument_count);
6453 return New<HCallFunction>(function, argument_count, WRAP_AND_CALL);
6454 } else if (FLAG_inline_accessors && can_inline_accessor) {
6455 bool success = info->IsLoad()
6456 ? TryInlineGetter(info->accessor(), info->map(), ast_id, return_id)
6458 info->accessor(), info->map(), ast_id, return_id, value);
6459 if (success || HasStackOverflow()) return NULL;
6462 PushArgumentsFromEnvironment(argument_count);
6463 return BuildCallConstantFunction(info->accessor(), argument_count);
6466 DCHECK(info->IsDataConstant());
6467 if (info->IsLoad()) {
6468 return New<HConstant>(info->constant());
6470 return New<HCheckValue>(value, Handle<JSFunction>::cast(info->constant()));
6475 void HOptimizedGraphBuilder::HandlePolymorphicNamedFieldAccess(
6476 PropertyAccessType access_type, Expression* expr, BailoutId ast_id,
6477 BailoutId return_id, HValue* object, HValue* value, SmallMapList* maps,
6478 Handle<String> name) {
6479 // Something did not match; must use a polymorphic load.
6481 HBasicBlock* join = NULL;
6482 HBasicBlock* number_block = NULL;
6483 bool handled_string = false;
6485 bool handle_smi = false;
6486 STATIC_ASSERT(kMaxLoadPolymorphism == kMaxStorePolymorphism);
6488 for (i = 0; i < maps->length() && count < kMaxLoadPolymorphism; ++i) {
6489 PropertyAccessInfo info(this, access_type, maps->at(i), name);
6490 if (info.IsStringType()) {
6491 if (handled_string) continue;
6492 handled_string = true;
6494 if (info.CanAccessMonomorphic()) {
6496 if (info.IsNumberType()) {
6503 if (i < maps->length()) {
6509 HControlInstruction* smi_check = NULL;
6510 handled_string = false;
6512 for (i = 0; i < maps->length() && count < kMaxLoadPolymorphism; ++i) {
6513 PropertyAccessInfo info(this, access_type, maps->at(i), name);
6514 if (info.IsStringType()) {
6515 if (handled_string) continue;
6516 handled_string = true;
6518 if (!info.CanAccessMonomorphic()) continue;
6521 join = graph()->CreateBasicBlock();
6523 HBasicBlock* empty_smi_block = graph()->CreateBasicBlock();
6524 HBasicBlock* not_smi_block = graph()->CreateBasicBlock();
6525 number_block = graph()->CreateBasicBlock();
6526 smi_check = New<HIsSmiAndBranch>(
6527 object, empty_smi_block, not_smi_block);
6528 FinishCurrentBlock(smi_check);
6529 GotoNoSimulate(empty_smi_block, number_block);
6530 set_current_block(not_smi_block);
6532 BuildCheckHeapObject(object);
6536 HBasicBlock* if_true = graph()->CreateBasicBlock();
6537 HBasicBlock* if_false = graph()->CreateBasicBlock();
6538 HUnaryControlInstruction* compare;
6541 if (info.IsNumberType()) {
6542 Handle<Map> heap_number_map = isolate()->factory()->heap_number_map();
6543 compare = New<HCompareMap>(object, heap_number_map, if_true, if_false);
6544 dependency = smi_check;
6545 } else if (info.IsStringType()) {
6546 compare = New<HIsStringAndBranch>(object, if_true, if_false);
6547 dependency = compare;
6549 compare = New<HCompareMap>(object, info.map(), if_true, if_false);
6550 dependency = compare;
6552 FinishCurrentBlock(compare);
6554 if (info.IsNumberType()) {
6555 GotoNoSimulate(if_true, number_block);
6556 if_true = number_block;
6559 set_current_block(if_true);
6562 BuildMonomorphicAccess(&info, object, dependency, value, ast_id,
6563 return_id, FLAG_polymorphic_inlining);
6565 HValue* result = NULL;
6566 switch (access_type) {
6575 if (access == NULL) {
6576 if (HasStackOverflow()) return;
6578 if (access->IsInstruction()) {
6579 HInstruction* instr = HInstruction::cast(access);
6580 if (!instr->IsLinked()) AddInstruction(instr);
6582 if (!ast_context()->IsEffect()) Push(result);
6585 if (current_block() != NULL) Goto(join);
6586 set_current_block(if_false);
6589 // Finish up. Unconditionally deoptimize if we've handled all the maps we
6590 // know about and do not want to handle ones we've never seen. Otherwise
6591 // use a generic IC.
6592 if (count == maps->length() && FLAG_deoptimize_uncommon_cases) {
6593 FinishExitWithHardDeoptimization(
6594 Deoptimizer::kUnknownMapInPolymorphicAccess);
6596 HInstruction* instr = BuildNamedGeneric(access_type, expr, object, name,
6598 AddInstruction(instr);
6599 if (!ast_context()->IsEffect()) Push(access_type == LOAD ? instr : value);
6604 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6605 if (!ast_context()->IsEffect()) ast_context()->ReturnValue(Pop());
6610 DCHECK(join != NULL);
6611 if (join->HasPredecessor()) {
6612 join->SetJoinId(ast_id);
6613 set_current_block(join);
6614 if (!ast_context()->IsEffect()) ast_context()->ReturnValue(Pop());
6616 set_current_block(NULL);
6621 static bool ComputeReceiverTypes(Expression* expr,
6625 SmallMapList* maps = expr->GetReceiverTypes();
6627 bool monomorphic = expr->IsMonomorphic();
6628 if (maps != NULL && receiver->HasMonomorphicJSObjectType()) {
6629 Map* root_map = receiver->GetMonomorphicJSObjectMap()->FindRootMap();
6630 maps->FilterForPossibleTransitions(root_map);
6631 monomorphic = maps->length() == 1;
6633 return monomorphic && CanInlinePropertyAccess(maps->first());
6637 static bool AreStringTypes(SmallMapList* maps) {
6638 for (int i = 0; i < maps->length(); i++) {
6639 if (maps->at(i)->instance_type() >= FIRST_NONSTRING_TYPE) return false;
6645 void HOptimizedGraphBuilder::BuildStore(Expression* expr,
6648 BailoutId return_id,
6649 bool is_uninitialized) {
6650 if (!prop->key()->IsPropertyName()) {
6652 HValue* value = Pop();
6653 HValue* key = Pop();
6654 HValue* object = Pop();
6655 bool has_side_effects = false;
6656 HValue* result = HandleKeyedElementAccess(
6657 object, key, value, expr, ast_id, return_id, STORE, &has_side_effects);
6658 if (has_side_effects) {
6659 if (!ast_context()->IsEffect()) Push(value);
6660 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6661 if (!ast_context()->IsEffect()) Drop(1);
6663 if (result == NULL) return;
6664 return ast_context()->ReturnValue(value);
6668 HValue* value = Pop();
6669 HValue* object = Pop();
6671 Literal* key = prop->key()->AsLiteral();
6672 Handle<String> name = Handle<String>::cast(key->value());
6673 DCHECK(!name.is_null());
6675 HValue* access = BuildNamedAccess(STORE, ast_id, return_id, expr, object,
6676 name, value, is_uninitialized);
6677 if (access == NULL) return;
6679 if (!ast_context()->IsEffect()) Push(value);
6680 if (access->IsInstruction()) AddInstruction(HInstruction::cast(access));
6681 if (access->HasObservableSideEffects()) {
6682 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6684 if (!ast_context()->IsEffect()) Drop(1);
6685 return ast_context()->ReturnValue(value);
6689 void HOptimizedGraphBuilder::HandlePropertyAssignment(Assignment* expr) {
6690 Property* prop = expr->target()->AsProperty();
6691 DCHECK(prop != NULL);
6692 CHECK_ALIVE(VisitForValue(prop->obj()));
6693 if (!prop->key()->IsPropertyName()) {
6694 CHECK_ALIVE(VisitForValue(prop->key()));
6696 CHECK_ALIVE(VisitForValue(expr->value()));
6697 BuildStore(expr, prop, expr->id(),
6698 expr->AssignmentId(), expr->IsUninitialized());
6702 // Because not every expression has a position and there is not common
6703 // superclass of Assignment and CountOperation, we cannot just pass the
6704 // owning expression instead of position and ast_id separately.
6705 void HOptimizedGraphBuilder::HandleGlobalVariableAssignment(
6709 Handle<GlobalObject> global(current_info()->global_object());
6711 // Lookup in script contexts.
6713 Handle<ScriptContextTable> script_contexts(
6714 global->native_context()->script_context_table());
6715 ScriptContextTable::LookupResult lookup;
6716 if (ScriptContextTable::Lookup(script_contexts, var->name(), &lookup)) {
6717 if (lookup.mode == CONST) {
6718 return Bailout(kNonInitializerAssignmentToConst);
6720 Handle<Context> script_context =
6721 ScriptContextTable::GetContext(script_contexts, lookup.context_index);
6723 Handle<Object> current_value =
6724 FixedArray::get(script_context, lookup.slot_index);
6726 // If the values is not the hole, it will stay initialized,
6727 // so no need to generate a check.
6728 if (*current_value == *isolate()->factory()->the_hole_value()) {
6729 return Bailout(kReferenceToUninitializedVariable);
6732 HStoreNamedField* instr = Add<HStoreNamedField>(
6733 Add<HConstant>(script_context),
6734 HObjectAccess::ForContextSlot(lookup.slot_index), value);
6736 DCHECK(instr->HasObservableSideEffects());
6737 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6742 LookupIterator it(global, var->name(), LookupIterator::OWN);
6743 GlobalPropertyAccess type = LookupGlobalProperty(var, &it, STORE);
6744 if (type == kUseCell) {
6745 Handle<PropertyCell> cell = it.GetPropertyCell();
6746 top_info()->dependencies()->AssumePropertyCell(cell);
6747 auto cell_type = it.property_details().cell_type();
6748 if (cell_type == PropertyCellType::kConstant ||
6749 cell_type == PropertyCellType::kUndefined) {
6750 Handle<Object> constant(cell->value(), isolate());
6751 if (value->IsConstant()) {
6752 HConstant* c_value = HConstant::cast(value);
6753 if (!constant.is_identical_to(c_value->handle(isolate()))) {
6754 Add<HDeoptimize>(Deoptimizer::kConstantGlobalVariableAssignment,
6755 Deoptimizer::EAGER);
6758 HValue* c_constant = Add<HConstant>(constant);
6759 IfBuilder builder(this);
6760 if (constant->IsNumber()) {
6761 builder.If<HCompareNumericAndBranch>(value, c_constant, Token::EQ);
6763 builder.If<HCompareObjectEqAndBranch>(value, c_constant);
6767 Add<HDeoptimize>(Deoptimizer::kConstantGlobalVariableAssignment,
6768 Deoptimizer::EAGER);
6772 HConstant* cell_constant = Add<HConstant>(cell);
6773 auto access = HObjectAccess::ForPropertyCellValue();
6774 if (cell_type == PropertyCellType::kConstantType) {
6775 switch (cell->GetConstantType()) {
6776 case PropertyCellConstantType::kSmi:
6777 access = access.WithRepresentation(Representation::Smi());
6779 case PropertyCellConstantType::kStableMap: {
6780 // The map may no longer be stable, deopt if it's ever different from
6781 // what is currently there, which will allow for restablization.
6782 Handle<Map> map(HeapObject::cast(cell->value())->map());
6783 Add<HCheckHeapObject>(value);
6784 value = Add<HCheckMaps>(value, map);
6785 access = access.WithRepresentation(Representation::HeapObject());
6790 HInstruction* instr = Add<HStoreNamedField>(cell_constant, access, value);
6791 instr->ClearChangesFlag(kInobjectFields);
6792 instr->SetChangesFlag(kGlobalVars);
6793 if (instr->HasObservableSideEffects()) {
6794 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6796 } else if (var->IsGlobalSlot()) {
6797 DCHECK(var->index() > 0);
6798 DCHECK(var->IsStaticGlobalObjectProperty());
6799 // Each var occupies two slots in the context: for reads and writes.
6800 int slot_index = var->index() + 1;
6801 int depth = scope()->ContextChainLength(var->scope());
6803 HStoreGlobalViaContext* instr = Add<HStoreGlobalViaContext>(
6804 value, depth, slot_index, function_language_mode());
6806 DCHECK(instr->HasObservableSideEffects());
6807 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6810 HValue* global_object = Add<HLoadNamedField>(
6812 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
6813 HStoreNamedGeneric* instr =
6814 Add<HStoreNamedGeneric>(global_object, var->name(), value,
6815 function_language_mode(), PREMONOMORPHIC);
6817 DCHECK(instr->HasObservableSideEffects());
6818 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
6823 void HOptimizedGraphBuilder::HandleCompoundAssignment(Assignment* expr) {
6824 Expression* target = expr->target();
6825 VariableProxy* proxy = target->AsVariableProxy();
6826 Property* prop = target->AsProperty();
6827 DCHECK(proxy == NULL || prop == NULL);
6829 // We have a second position recorded in the FullCodeGenerator to have
6830 // type feedback for the binary operation.
6831 BinaryOperation* operation = expr->binary_operation();
6833 if (proxy != NULL) {
6834 Variable* var = proxy->var();
6835 if (var->mode() == LET) {
6836 return Bailout(kUnsupportedLetCompoundAssignment);
6839 CHECK_ALIVE(VisitForValue(operation));
6841 switch (var->location()) {
6842 case VariableLocation::GLOBAL:
6843 case VariableLocation::UNALLOCATED:
6844 HandleGlobalVariableAssignment(var,
6846 expr->AssignmentId());
6849 case VariableLocation::PARAMETER:
6850 case VariableLocation::LOCAL:
6851 if (var->mode() == CONST_LEGACY) {
6852 return Bailout(kUnsupportedConstCompoundAssignment);
6854 if (var->mode() == CONST) {
6855 return Bailout(kNonInitializerAssignmentToConst);
6857 BindIfLive(var, Top());
6860 case VariableLocation::CONTEXT: {
6861 // Bail out if we try to mutate a parameter value in a function
6862 // using the arguments object. We do not (yet) correctly handle the
6863 // arguments property of the function.
6864 if (current_info()->scope()->arguments() != NULL) {
6865 // Parameters will be allocated to context slots. We have no
6866 // direct way to detect that the variable is a parameter so we do
6867 // a linear search of the parameter variables.
6868 int count = current_info()->scope()->num_parameters();
6869 for (int i = 0; i < count; ++i) {
6870 if (var == current_info()->scope()->parameter(i)) {
6871 Bailout(kAssignmentToParameterFunctionUsesArgumentsObject);
6876 HStoreContextSlot::Mode mode;
6878 switch (var->mode()) {
6880 mode = HStoreContextSlot::kCheckDeoptimize;
6883 return Bailout(kNonInitializerAssignmentToConst);
6885 return ast_context()->ReturnValue(Pop());
6887 mode = HStoreContextSlot::kNoCheck;
6890 HValue* context = BuildContextChainWalk(var);
6891 HStoreContextSlot* instr = Add<HStoreContextSlot>(
6892 context, var->index(), mode, Top());
6893 if (instr->HasObservableSideEffects()) {
6894 Add<HSimulate>(expr->AssignmentId(), REMOVABLE_SIMULATE);
6899 case VariableLocation::LOOKUP:
6900 return Bailout(kCompoundAssignmentToLookupSlot);
6902 return ast_context()->ReturnValue(Pop());
6904 } else if (prop != NULL) {
6905 CHECK_ALIVE(VisitForValue(prop->obj()));
6906 HValue* object = Top();
6908 if (!prop->key()->IsPropertyName() || prop->IsStringAccess()) {
6909 CHECK_ALIVE(VisitForValue(prop->key()));
6913 CHECK_ALIVE(PushLoad(prop, object, key));
6915 CHECK_ALIVE(VisitForValue(expr->value()));
6916 HValue* right = Pop();
6917 HValue* left = Pop();
6919 Push(BuildBinaryOperation(operation, left, right, PUSH_BEFORE_SIMULATE));
6921 BuildStore(expr, prop, expr->id(),
6922 expr->AssignmentId(), expr->IsUninitialized());
6924 return Bailout(kInvalidLhsInCompoundAssignment);
6929 void HOptimizedGraphBuilder::VisitAssignment(Assignment* expr) {
6930 DCHECK(!HasStackOverflow());
6931 DCHECK(current_block() != NULL);
6932 DCHECK(current_block()->HasPredecessor());
6933 VariableProxy* proxy = expr->target()->AsVariableProxy();
6934 Property* prop = expr->target()->AsProperty();
6935 DCHECK(proxy == NULL || prop == NULL);
6937 if (expr->is_compound()) {
6938 HandleCompoundAssignment(expr);
6943 HandlePropertyAssignment(expr);
6944 } else if (proxy != NULL) {
6945 Variable* var = proxy->var();
6947 if (var->mode() == CONST) {
6948 if (expr->op() != Token::INIT_CONST) {
6949 return Bailout(kNonInitializerAssignmentToConst);
6951 } else if (var->mode() == CONST_LEGACY) {
6952 if (expr->op() != Token::INIT_CONST_LEGACY) {
6953 CHECK_ALIVE(VisitForValue(expr->value()));
6954 return ast_context()->ReturnValue(Pop());
6957 if (var->IsStackAllocated()) {
6958 // We insert a use of the old value to detect unsupported uses of const
6959 // variables (e.g. initialization inside a loop).
6960 HValue* old_value = environment()->Lookup(var);
6961 Add<HUseConst>(old_value);
6965 if (proxy->IsArguments()) return Bailout(kAssignmentToArguments);
6967 // Handle the assignment.
6968 switch (var->location()) {
6969 case VariableLocation::GLOBAL:
6970 case VariableLocation::UNALLOCATED:
6971 CHECK_ALIVE(VisitForValue(expr->value()));
6972 HandleGlobalVariableAssignment(var,
6974 expr->AssignmentId());
6975 return ast_context()->ReturnValue(Pop());
6977 case VariableLocation::PARAMETER:
6978 case VariableLocation::LOCAL: {
6979 // Perform an initialization check for let declared variables
6981 if (var->mode() == LET && expr->op() == Token::ASSIGN) {
6982 HValue* env_value = environment()->Lookup(var);
6983 if (env_value == graph()->GetConstantHole()) {
6984 return Bailout(kAssignmentToLetVariableBeforeInitialization);
6987 // We do not allow the arguments object to occur in a context where it
6988 // may escape, but assignments to stack-allocated locals are
6990 CHECK_ALIVE(VisitForValue(expr->value(), ARGUMENTS_ALLOWED));
6991 HValue* value = Pop();
6992 BindIfLive(var, value);
6993 return ast_context()->ReturnValue(value);
6996 case VariableLocation::CONTEXT: {
6997 // Bail out if we try to mutate a parameter value in a function using
6998 // the arguments object. We do not (yet) correctly handle the
6999 // arguments property of the function.
7000 if (current_info()->scope()->arguments() != NULL) {
7001 // Parameters will rewrite to context slots. We have no direct way
7002 // to detect that the variable is a parameter.
7003 int count = current_info()->scope()->num_parameters();
7004 for (int i = 0; i < count; ++i) {
7005 if (var == current_info()->scope()->parameter(i)) {
7006 return Bailout(kAssignmentToParameterInArgumentsObject);
7011 CHECK_ALIVE(VisitForValue(expr->value()));
7012 HStoreContextSlot::Mode mode;
7013 if (expr->op() == Token::ASSIGN) {
7014 switch (var->mode()) {
7016 mode = HStoreContextSlot::kCheckDeoptimize;
7019 // This case is checked statically so no need to
7020 // perform checks here
7023 return ast_context()->ReturnValue(Pop());
7025 mode = HStoreContextSlot::kNoCheck;
7027 } else if (expr->op() == Token::INIT_VAR ||
7028 expr->op() == Token::INIT_LET ||
7029 expr->op() == Token::INIT_CONST) {
7030 mode = HStoreContextSlot::kNoCheck;
7032 DCHECK(expr->op() == Token::INIT_CONST_LEGACY);
7034 mode = HStoreContextSlot::kCheckIgnoreAssignment;
7037 HValue* context = BuildContextChainWalk(var);
7038 HStoreContextSlot* instr = Add<HStoreContextSlot>(
7039 context, var->index(), mode, Top());
7040 if (instr->HasObservableSideEffects()) {
7041 Add<HSimulate>(expr->AssignmentId(), REMOVABLE_SIMULATE);
7043 return ast_context()->ReturnValue(Pop());
7046 case VariableLocation::LOOKUP:
7047 return Bailout(kAssignmentToLOOKUPVariable);
7050 return Bailout(kInvalidLeftHandSideInAssignment);
7055 void HOptimizedGraphBuilder::VisitYield(Yield* expr) {
7056 // Generators are not optimized, so we should never get here.
7061 void HOptimizedGraphBuilder::VisitThrow(Throw* expr) {
7062 DCHECK(!HasStackOverflow());
7063 DCHECK(current_block() != NULL);
7064 DCHECK(current_block()->HasPredecessor());
7065 if (!ast_context()->IsEffect()) {
7066 // The parser turns invalid left-hand sides in assignments into throw
7067 // statements, which may not be in effect contexts. We might still try
7068 // to optimize such functions; bail out now if we do.
7069 return Bailout(kInvalidLeftHandSideInAssignment);
7071 CHECK_ALIVE(VisitForValue(expr->exception()));
7073 HValue* value = environment()->Pop();
7074 if (!top_info()->is_tracking_positions()) SetSourcePosition(expr->position());
7075 Add<HPushArguments>(value);
7076 Add<HCallRuntime>(isolate()->factory()->empty_string(),
7077 Runtime::FunctionForId(Runtime::kThrow), 1);
7078 Add<HSimulate>(expr->id());
7080 // If the throw definitely exits the function, we can finish with a dummy
7081 // control flow at this point. This is not the case if the throw is inside
7082 // an inlined function which may be replaced.
7083 if (call_context() == NULL) {
7084 FinishExitCurrentBlock(New<HAbnormalExit>());
7089 HInstruction* HGraphBuilder::AddLoadStringInstanceType(HValue* string) {
7090 if (string->IsConstant()) {
7091 HConstant* c_string = HConstant::cast(string);
7092 if (c_string->HasStringValue()) {
7093 return Add<HConstant>(c_string->StringValue()->map()->instance_type());
7096 return Add<HLoadNamedField>(
7097 Add<HLoadNamedField>(string, nullptr, HObjectAccess::ForMap()), nullptr,
7098 HObjectAccess::ForMapInstanceType());
7102 HInstruction* HGraphBuilder::AddLoadStringLength(HValue* string) {
7103 return AddInstruction(BuildLoadStringLength(string));
7107 HInstruction* HGraphBuilder::BuildLoadStringLength(HValue* string) {
7108 if (string->IsConstant()) {
7109 HConstant* c_string = HConstant::cast(string);
7110 if (c_string->HasStringValue()) {
7111 return New<HConstant>(c_string->StringValue()->length());
7114 return New<HLoadNamedField>(string, nullptr,
7115 HObjectAccess::ForStringLength());
7119 HInstruction* HOptimizedGraphBuilder::BuildNamedGeneric(
7120 PropertyAccessType access_type, Expression* expr, HValue* object,
7121 Handle<String> name, HValue* value, bool is_uninitialized) {
7122 if (is_uninitialized) {
7124 Deoptimizer::kInsufficientTypeFeedbackForGenericNamedAccess,
7127 if (access_type == LOAD) {
7128 Handle<TypeFeedbackVector> vector =
7129 handle(current_feedback_vector(), isolate());
7130 FeedbackVectorICSlot slot = expr->AsProperty()->PropertyFeedbackSlot();
7132 if (!expr->AsProperty()->key()->IsPropertyName()) {
7133 // It's possible that a keyed load of a constant string was converted
7134 // to a named load. Here, at the last minute, we need to make sure to
7135 // use a generic Keyed Load if we are using the type vector, because
7136 // it has to share information with full code.
7137 HConstant* key = Add<HConstant>(name);
7138 HLoadKeyedGeneric* result = New<HLoadKeyedGeneric>(
7139 object, key, function_language_mode(), PREMONOMORPHIC);
7140 result->SetVectorAndSlot(vector, slot);
7144 HLoadNamedGeneric* result = New<HLoadNamedGeneric>(
7145 object, name, function_language_mode(), PREMONOMORPHIC);
7146 result->SetVectorAndSlot(vector, slot);
7149 return New<HStoreNamedGeneric>(object, name, value,
7150 function_language_mode(), PREMONOMORPHIC);
7156 HInstruction* HOptimizedGraphBuilder::BuildKeyedGeneric(
7157 PropertyAccessType access_type,
7162 if (access_type == LOAD) {
7163 InlineCacheState initial_state = expr->AsProperty()->GetInlineCacheState();
7164 HLoadKeyedGeneric* result = New<HLoadKeyedGeneric>(
7165 object, key, function_language_mode(), initial_state);
7166 // HLoadKeyedGeneric with vector ics benefits from being encoded as
7167 // MEGAMORPHIC because the vector/slot combo becomes unnecessary.
7168 if (initial_state != MEGAMORPHIC) {
7169 // We need to pass vector information.
7170 Handle<TypeFeedbackVector> vector =
7171 handle(current_feedback_vector(), isolate());
7172 FeedbackVectorICSlot slot = expr->AsProperty()->PropertyFeedbackSlot();
7173 result->SetVectorAndSlot(vector, slot);
7177 return New<HStoreKeyedGeneric>(object, key, value, function_language_mode(),
7183 LoadKeyedHoleMode HOptimizedGraphBuilder::BuildKeyedHoleMode(Handle<Map> map) {
7184 // Loads from a "stock" fast holey double arrays can elide the hole check.
7185 // Loads from a "stock" fast holey array can convert the hole to undefined
7187 LoadKeyedHoleMode load_mode = NEVER_RETURN_HOLE;
7188 bool holey_double_elements =
7189 *map == isolate()->get_initial_js_array_map(FAST_HOLEY_DOUBLE_ELEMENTS);
7190 bool holey_elements =
7191 *map == isolate()->get_initial_js_array_map(FAST_HOLEY_ELEMENTS);
7192 if ((holey_double_elements || holey_elements) &&
7193 isolate()->IsFastArrayConstructorPrototypeChainIntact()) {
7195 holey_double_elements ? ALLOW_RETURN_HOLE : CONVERT_HOLE_TO_UNDEFINED;
7197 Handle<JSObject> prototype(JSObject::cast(map->prototype()), isolate());
7198 Handle<JSObject> object_prototype = isolate()->initial_object_prototype();
7199 BuildCheckPrototypeMaps(prototype, object_prototype);
7200 graph()->MarkDependsOnEmptyArrayProtoElements();
7206 HInstruction* HOptimizedGraphBuilder::BuildMonomorphicElementAccess(
7212 PropertyAccessType access_type,
7213 KeyedAccessStoreMode store_mode) {
7214 HCheckMaps* checked_object = Add<HCheckMaps>(object, map, dependency);
7216 if (access_type == STORE && map->prototype()->IsJSObject()) {
7217 // monomorphic stores need a prototype chain check because shape
7218 // changes could allow callbacks on elements in the chain that
7219 // aren't compatible with monomorphic keyed stores.
7220 PrototypeIterator iter(map);
7221 JSObject* holder = NULL;
7222 while (!iter.IsAtEnd()) {
7223 holder = JSObject::cast(*PrototypeIterator::GetCurrent(iter));
7226 DCHECK(holder && holder->IsJSObject());
7228 BuildCheckPrototypeMaps(handle(JSObject::cast(map->prototype())),
7229 Handle<JSObject>(holder));
7232 LoadKeyedHoleMode load_mode = BuildKeyedHoleMode(map);
7233 return BuildUncheckedMonomorphicElementAccess(
7234 checked_object, key, val,
7235 map->instance_type() == JS_ARRAY_TYPE,
7236 map->elements_kind(), access_type,
7237 load_mode, store_mode);
7241 static bool CanInlineElementAccess(Handle<Map> map) {
7242 return map->IsJSObjectMap() && !map->has_dictionary_elements() &&
7243 !map->has_sloppy_arguments_elements() &&
7244 !map->has_indexed_interceptor() && !map->is_access_check_needed();
7248 HInstruction* HOptimizedGraphBuilder::TryBuildConsolidatedElementLoad(
7252 SmallMapList* maps) {
7253 // For polymorphic loads of similar elements kinds (i.e. all tagged or all
7254 // double), always use the "worst case" code without a transition. This is
7255 // much faster than transitioning the elements to the worst case, trading a
7256 // HTransitionElements for a HCheckMaps, and avoiding mutation of the array.
7257 bool has_double_maps = false;
7258 bool has_smi_or_object_maps = false;
7259 bool has_js_array_access = false;
7260 bool has_non_js_array_access = false;
7261 bool has_seen_holey_elements = false;
7262 Handle<Map> most_general_consolidated_map;
7263 for (int i = 0; i < maps->length(); ++i) {
7264 Handle<Map> map = maps->at(i);
7265 if (!CanInlineElementAccess(map)) return NULL;
7266 // Don't allow mixing of JSArrays with JSObjects.
7267 if (map->instance_type() == JS_ARRAY_TYPE) {
7268 if (has_non_js_array_access) return NULL;
7269 has_js_array_access = true;
7270 } else if (has_js_array_access) {
7273 has_non_js_array_access = true;
7275 // Don't allow mixed, incompatible elements kinds.
7276 if (map->has_fast_double_elements()) {
7277 if (has_smi_or_object_maps) return NULL;
7278 has_double_maps = true;
7279 } else if (map->has_fast_smi_or_object_elements()) {
7280 if (has_double_maps) return NULL;
7281 has_smi_or_object_maps = true;
7285 // Remember if we've ever seen holey elements.
7286 if (IsHoleyElementsKind(map->elements_kind())) {
7287 has_seen_holey_elements = true;
7289 // Remember the most general elements kind, the code for its load will
7290 // properly handle all of the more specific cases.
7291 if ((i == 0) || IsMoreGeneralElementsKindTransition(
7292 most_general_consolidated_map->elements_kind(),
7293 map->elements_kind())) {
7294 most_general_consolidated_map = map;
7297 if (!has_double_maps && !has_smi_or_object_maps) return NULL;
7299 HCheckMaps* checked_object = Add<HCheckMaps>(object, maps);
7300 // FAST_ELEMENTS is considered more general than FAST_HOLEY_SMI_ELEMENTS.
7301 // If we've seen both, the consolidated load must use FAST_HOLEY_ELEMENTS.
7302 ElementsKind consolidated_elements_kind = has_seen_holey_elements
7303 ? GetHoleyElementsKind(most_general_consolidated_map->elements_kind())
7304 : most_general_consolidated_map->elements_kind();
7305 HInstruction* instr = BuildUncheckedMonomorphicElementAccess(
7306 checked_object, key, val,
7307 most_general_consolidated_map->instance_type() == JS_ARRAY_TYPE,
7308 consolidated_elements_kind,
7309 LOAD, NEVER_RETURN_HOLE, STANDARD_STORE);
7314 HValue* HOptimizedGraphBuilder::HandlePolymorphicElementAccess(
7320 PropertyAccessType access_type,
7321 KeyedAccessStoreMode store_mode,
7322 bool* has_side_effects) {
7323 *has_side_effects = false;
7324 BuildCheckHeapObject(object);
7326 if (access_type == LOAD) {
7327 HInstruction* consolidated_load =
7328 TryBuildConsolidatedElementLoad(object, key, val, maps);
7329 if (consolidated_load != NULL) {
7330 *has_side_effects |= consolidated_load->HasObservableSideEffects();
7331 return consolidated_load;
7335 // Elements_kind transition support.
7336 MapHandleList transition_target(maps->length());
7337 // Collect possible transition targets.
7338 MapHandleList possible_transitioned_maps(maps->length());
7339 for (int i = 0; i < maps->length(); ++i) {
7340 Handle<Map> map = maps->at(i);
7341 // Loads from strings or loads with a mix of string and non-string maps
7342 // shouldn't be handled polymorphically.
7343 DCHECK(access_type != LOAD || !map->IsStringMap());
7344 ElementsKind elements_kind = map->elements_kind();
7345 if (CanInlineElementAccess(map) && IsFastElementsKind(elements_kind) &&
7346 elements_kind != GetInitialFastElementsKind()) {
7347 possible_transitioned_maps.Add(map);
7349 if (IsSloppyArgumentsElements(elements_kind)) {
7350 HInstruction* result = BuildKeyedGeneric(access_type, expr, object, key,
7352 *has_side_effects = result->HasObservableSideEffects();
7353 return AddInstruction(result);
7356 // Get transition target for each map (NULL == no transition).
7357 for (int i = 0; i < maps->length(); ++i) {
7358 Handle<Map> map = maps->at(i);
7359 Handle<Map> transitioned_map =
7360 Map::FindTransitionedMap(map, &possible_transitioned_maps);
7361 transition_target.Add(transitioned_map);
7364 MapHandleList untransitionable_maps(maps->length());
7365 HTransitionElementsKind* transition = NULL;
7366 for (int i = 0; i < maps->length(); ++i) {
7367 Handle<Map> map = maps->at(i);
7368 DCHECK(map->IsMap());
7369 if (!transition_target.at(i).is_null()) {
7370 DCHECK(Map::IsValidElementsTransition(
7371 map->elements_kind(),
7372 transition_target.at(i)->elements_kind()));
7373 transition = Add<HTransitionElementsKind>(object, map,
7374 transition_target.at(i));
7376 untransitionable_maps.Add(map);
7380 // If only one map is left after transitioning, handle this case
7382 DCHECK(untransitionable_maps.length() >= 1);
7383 if (untransitionable_maps.length() == 1) {
7384 Handle<Map> untransitionable_map = untransitionable_maps[0];
7385 HInstruction* instr = NULL;
7386 if (!CanInlineElementAccess(untransitionable_map)) {
7387 instr = AddInstruction(BuildKeyedGeneric(access_type, expr, object, key,
7390 instr = BuildMonomorphicElementAccess(
7391 object, key, val, transition, untransitionable_map, access_type,
7394 *has_side_effects |= instr->HasObservableSideEffects();
7395 return access_type == STORE ? val : instr;
7398 HBasicBlock* join = graph()->CreateBasicBlock();
7400 for (int i = 0; i < untransitionable_maps.length(); ++i) {
7401 Handle<Map> map = untransitionable_maps[i];
7402 ElementsKind elements_kind = map->elements_kind();
7403 HBasicBlock* this_map = graph()->CreateBasicBlock();
7404 HBasicBlock* other_map = graph()->CreateBasicBlock();
7405 HCompareMap* mapcompare =
7406 New<HCompareMap>(object, map, this_map, other_map);
7407 FinishCurrentBlock(mapcompare);
7409 set_current_block(this_map);
7410 HInstruction* access = NULL;
7411 if (!CanInlineElementAccess(map)) {
7412 access = AddInstruction(BuildKeyedGeneric(access_type, expr, object, key,
7415 DCHECK(IsFastElementsKind(elements_kind) ||
7416 IsExternalArrayElementsKind(elements_kind) ||
7417 IsFixedTypedArrayElementsKind(elements_kind));
7418 LoadKeyedHoleMode load_mode = BuildKeyedHoleMode(map);
7419 // Happily, mapcompare is a checked object.
7420 access = BuildUncheckedMonomorphicElementAccess(
7421 mapcompare, key, val,
7422 map->instance_type() == JS_ARRAY_TYPE,
7423 elements_kind, access_type,
7427 *has_side_effects |= access->HasObservableSideEffects();
7428 // The caller will use has_side_effects and add a correct Simulate.
7429 access->SetFlag(HValue::kHasNoObservableSideEffects);
7430 if (access_type == LOAD) {
7433 NoObservableSideEffectsScope scope(this);
7434 GotoNoSimulate(join);
7435 set_current_block(other_map);
7438 // Ensure that we visited at least one map above that goes to join. This is
7439 // necessary because FinishExitWithHardDeoptimization does an AbnormalExit
7440 // rather than joining the join block. If this becomes an issue, insert a
7441 // generic access in the case length() == 0.
7442 DCHECK(join->predecessors()->length() > 0);
7443 // Deopt if none of the cases matched.
7444 NoObservableSideEffectsScope scope(this);
7445 FinishExitWithHardDeoptimization(
7446 Deoptimizer::kUnknownMapInPolymorphicElementAccess);
7447 set_current_block(join);
7448 return access_type == STORE ? val : Pop();
7452 HValue* HOptimizedGraphBuilder::HandleKeyedElementAccess(
7453 HValue* obj, HValue* key, HValue* val, Expression* expr, BailoutId ast_id,
7454 BailoutId return_id, PropertyAccessType access_type,
7455 bool* has_side_effects) {
7456 if (key->ActualValue()->IsConstant()) {
7457 Handle<Object> constant =
7458 HConstant::cast(key->ActualValue())->handle(isolate());
7459 uint32_t array_index;
7460 if (constant->IsString() &&
7461 !Handle<String>::cast(constant)->AsArrayIndex(&array_index)) {
7462 if (!constant->IsUniqueName()) {
7463 constant = isolate()->factory()->InternalizeString(
7464 Handle<String>::cast(constant));
7467 BuildNamedAccess(access_type, ast_id, return_id, expr, obj,
7468 Handle<String>::cast(constant), val, false);
7469 if (access == NULL || access->IsPhi() ||
7470 HInstruction::cast(access)->IsLinked()) {
7471 *has_side_effects = false;
7473 HInstruction* instr = HInstruction::cast(access);
7474 AddInstruction(instr);
7475 *has_side_effects = instr->HasObservableSideEffects();
7481 DCHECK(!expr->IsPropertyName());
7482 HInstruction* instr = NULL;
7485 bool monomorphic = ComputeReceiverTypes(expr, obj, &maps, zone());
7487 bool force_generic = false;
7488 if (expr->GetKeyType() == PROPERTY) {
7489 // Non-Generic accesses assume that elements are being accessed, and will
7490 // deopt for non-index keys, which the IC knows will occur.
7491 // TODO(jkummerow): Consider adding proper support for property accesses.
7492 force_generic = true;
7493 monomorphic = false;
7494 } else if (access_type == STORE &&
7495 (monomorphic || (maps != NULL && !maps->is_empty()))) {
7496 // Stores can't be mono/polymorphic if their prototype chain has dictionary
7497 // elements. However a receiver map that has dictionary elements itself
7498 // should be left to normal mono/poly behavior (the other maps may benefit
7499 // from highly optimized stores).
7500 for (int i = 0; i < maps->length(); i++) {
7501 Handle<Map> current_map = maps->at(i);
7502 if (current_map->DictionaryElementsInPrototypeChainOnly()) {
7503 force_generic = true;
7504 monomorphic = false;
7508 } else if (access_type == LOAD && !monomorphic &&
7509 (maps != NULL && !maps->is_empty())) {
7510 // Polymorphic loads have to go generic if any of the maps are strings.
7511 // If some, but not all of the maps are strings, we should go generic
7512 // because polymorphic access wants to key on ElementsKind and isn't
7513 // compatible with strings.
7514 for (int i = 0; i < maps->length(); i++) {
7515 Handle<Map> current_map = maps->at(i);
7516 if (current_map->IsStringMap()) {
7517 force_generic = true;
7524 Handle<Map> map = maps->first();
7525 if (!CanInlineElementAccess(map)) {
7526 instr = AddInstruction(BuildKeyedGeneric(access_type, expr, obj, key,
7529 BuildCheckHeapObject(obj);
7530 instr = BuildMonomorphicElementAccess(
7531 obj, key, val, NULL, map, access_type, expr->GetStoreMode());
7533 } else if (!force_generic && (maps != NULL && !maps->is_empty())) {
7534 return HandlePolymorphicElementAccess(expr, obj, key, val, maps,
7535 access_type, expr->GetStoreMode(),
7538 if (access_type == STORE) {
7539 if (expr->IsAssignment() &&
7540 expr->AsAssignment()->HasNoTypeInformation()) {
7541 Add<HDeoptimize>(Deoptimizer::kInsufficientTypeFeedbackForKeyedStore,
7545 if (expr->AsProperty()->HasNoTypeInformation()) {
7546 Add<HDeoptimize>(Deoptimizer::kInsufficientTypeFeedbackForKeyedLoad,
7550 instr = AddInstruction(BuildKeyedGeneric(access_type, expr, obj, key, val));
7552 *has_side_effects = instr->HasObservableSideEffects();
7557 void HOptimizedGraphBuilder::EnsureArgumentsArePushedForAccess() {
7558 // Outermost function already has arguments on the stack.
7559 if (function_state()->outer() == NULL) return;
7561 if (function_state()->arguments_pushed()) return;
7563 // Push arguments when entering inlined function.
7564 HEnterInlined* entry = function_state()->entry();
7565 entry->set_arguments_pushed();
7567 HArgumentsObject* arguments = entry->arguments_object();
7568 const ZoneList<HValue*>* arguments_values = arguments->arguments_values();
7570 HInstruction* insert_after = entry;
7571 for (int i = 0; i < arguments_values->length(); i++) {
7572 HValue* argument = arguments_values->at(i);
7573 HInstruction* push_argument = New<HPushArguments>(argument);
7574 push_argument->InsertAfter(insert_after);
7575 insert_after = push_argument;
7578 HArgumentsElements* arguments_elements = New<HArgumentsElements>(true);
7579 arguments_elements->ClearFlag(HValue::kUseGVN);
7580 arguments_elements->InsertAfter(insert_after);
7581 function_state()->set_arguments_elements(arguments_elements);
7585 bool HOptimizedGraphBuilder::TryArgumentsAccess(Property* expr) {
7586 VariableProxy* proxy = expr->obj()->AsVariableProxy();
7587 if (proxy == NULL) return false;
7588 if (!proxy->var()->IsStackAllocated()) return false;
7589 if (!environment()->Lookup(proxy->var())->CheckFlag(HValue::kIsArguments)) {
7593 HInstruction* result = NULL;
7594 if (expr->key()->IsPropertyName()) {
7595 Handle<String> name = expr->key()->AsLiteral()->AsPropertyName();
7596 if (!String::Equals(name, isolate()->factory()->length_string())) {
7600 if (function_state()->outer() == NULL) {
7601 HInstruction* elements = Add<HArgumentsElements>(false);
7602 result = New<HArgumentsLength>(elements);
7604 // Number of arguments without receiver.
7605 int argument_count = environment()->
7606 arguments_environment()->parameter_count() - 1;
7607 result = New<HConstant>(argument_count);
7610 Push(graph()->GetArgumentsObject());
7611 CHECK_ALIVE_OR_RETURN(VisitForValue(expr->key()), true);
7612 HValue* key = Pop();
7613 Drop(1); // Arguments object.
7614 if (function_state()->outer() == NULL) {
7615 HInstruction* elements = Add<HArgumentsElements>(false);
7616 HInstruction* length = Add<HArgumentsLength>(elements);
7617 HInstruction* checked_key = Add<HBoundsCheck>(key, length);
7618 result = New<HAccessArgumentsAt>(elements, length, checked_key);
7620 EnsureArgumentsArePushedForAccess();
7622 // Number of arguments without receiver.
7623 HInstruction* elements = function_state()->arguments_elements();
7624 int argument_count = environment()->
7625 arguments_environment()->parameter_count() - 1;
7626 HInstruction* length = Add<HConstant>(argument_count);
7627 HInstruction* checked_key = Add<HBoundsCheck>(key, length);
7628 result = New<HAccessArgumentsAt>(elements, length, checked_key);
7631 ast_context()->ReturnInstruction(result, expr->id());
7636 HValue* HOptimizedGraphBuilder::BuildNamedAccess(
7637 PropertyAccessType access, BailoutId ast_id, BailoutId return_id,
7638 Expression* expr, HValue* object, Handle<String> name, HValue* value,
7639 bool is_uninitialized) {
7641 ComputeReceiverTypes(expr, object, &maps, zone());
7642 DCHECK(maps != NULL);
7644 if (maps->length() > 0) {
7645 PropertyAccessInfo info(this, access, maps->first(), name);
7646 if (!info.CanAccessAsMonomorphic(maps)) {
7647 HandlePolymorphicNamedFieldAccess(access, expr, ast_id, return_id, object,
7652 HValue* checked_object;
7653 // Type::Number() is only supported by polymorphic load/call handling.
7654 DCHECK(!info.IsNumberType());
7655 BuildCheckHeapObject(object);
7656 if (AreStringTypes(maps)) {
7658 Add<HCheckInstanceType>(object, HCheckInstanceType::IS_STRING);
7660 checked_object = Add<HCheckMaps>(object, maps);
7662 return BuildMonomorphicAccess(
7663 &info, object, checked_object, value, ast_id, return_id);
7666 return BuildNamedGeneric(access, expr, object, name, value, is_uninitialized);
7670 void HOptimizedGraphBuilder::PushLoad(Property* expr,
7673 ValueContext for_value(this, ARGUMENTS_NOT_ALLOWED);
7675 if (key != NULL) Push(key);
7676 BuildLoad(expr, expr->LoadId());
7680 void HOptimizedGraphBuilder::BuildLoad(Property* expr,
7682 HInstruction* instr = NULL;
7683 if (expr->IsStringAccess()) {
7684 HValue* index = Pop();
7685 HValue* string = Pop();
7686 HInstruction* char_code = BuildStringCharCodeAt(string, index);
7687 AddInstruction(char_code);
7688 instr = NewUncasted<HStringCharFromCode>(char_code);
7690 } else if (expr->key()->IsPropertyName()) {
7691 Handle<String> name = expr->key()->AsLiteral()->AsPropertyName();
7692 HValue* object = Pop();
7694 HValue* value = BuildNamedAccess(LOAD, ast_id, expr->LoadId(), expr, object,
7695 name, NULL, expr->IsUninitialized());
7696 if (value == NULL) return;
7697 if (value->IsPhi()) return ast_context()->ReturnValue(value);
7698 instr = HInstruction::cast(value);
7699 if (instr->IsLinked()) return ast_context()->ReturnValue(instr);
7702 HValue* key = Pop();
7703 HValue* obj = Pop();
7705 bool has_side_effects = false;
7706 HValue* load = HandleKeyedElementAccess(
7707 obj, key, NULL, expr, ast_id, expr->LoadId(), LOAD, &has_side_effects);
7708 if (has_side_effects) {
7709 if (ast_context()->IsEffect()) {
7710 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
7713 Add<HSimulate>(ast_id, REMOVABLE_SIMULATE);
7717 if (load == NULL) return;
7718 return ast_context()->ReturnValue(load);
7720 return ast_context()->ReturnInstruction(instr, ast_id);
7724 void HOptimizedGraphBuilder::VisitProperty(Property* expr) {
7725 DCHECK(!HasStackOverflow());
7726 DCHECK(current_block() != NULL);
7727 DCHECK(current_block()->HasPredecessor());
7729 if (TryArgumentsAccess(expr)) return;
7731 CHECK_ALIVE(VisitForValue(expr->obj()));
7732 if (!expr->key()->IsPropertyName() || expr->IsStringAccess()) {
7733 CHECK_ALIVE(VisitForValue(expr->key()));
7736 BuildLoad(expr, expr->id());
7740 HInstruction* HGraphBuilder::BuildConstantMapCheck(Handle<JSObject> constant) {
7741 HCheckMaps* check = Add<HCheckMaps>(
7742 Add<HConstant>(constant), handle(constant->map()));
7743 check->ClearDependsOnFlag(kElementsKind);
7748 HInstruction* HGraphBuilder::BuildCheckPrototypeMaps(Handle<JSObject> prototype,
7749 Handle<JSObject> holder) {
7750 PrototypeIterator iter(isolate(), prototype,
7751 PrototypeIterator::START_AT_RECEIVER);
7752 while (holder.is_null() ||
7753 !PrototypeIterator::GetCurrent(iter).is_identical_to(holder)) {
7754 BuildConstantMapCheck(
7755 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
7757 if (iter.IsAtEnd()) {
7761 return BuildConstantMapCheck(
7762 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
7766 void HOptimizedGraphBuilder::AddCheckPrototypeMaps(Handle<JSObject> holder,
7767 Handle<Map> receiver_map) {
7768 if (!holder.is_null()) {
7769 Handle<JSObject> prototype(JSObject::cast(receiver_map->prototype()));
7770 BuildCheckPrototypeMaps(prototype, holder);
7775 HInstruction* HOptimizedGraphBuilder::NewPlainFunctionCall(
7776 HValue* fun, int argument_count, bool pass_argument_count) {
7777 return New<HCallJSFunction>(fun, argument_count, pass_argument_count);
7781 HInstruction* HOptimizedGraphBuilder::NewArgumentAdaptorCall(
7782 HValue* fun, HValue* context,
7783 int argument_count, HValue* expected_param_count) {
7784 ArgumentAdaptorDescriptor descriptor(isolate());
7785 HValue* arity = Add<HConstant>(argument_count - 1);
7787 HValue* op_vals[] = { context, fun, arity, expected_param_count };
7789 Handle<Code> adaptor =
7790 isolate()->builtins()->ArgumentsAdaptorTrampoline();
7791 HConstant* adaptor_value = Add<HConstant>(adaptor);
7793 return New<HCallWithDescriptor>(adaptor_value, argument_count, descriptor,
7794 Vector<HValue*>(op_vals, arraysize(op_vals)));
7798 HInstruction* HOptimizedGraphBuilder::BuildCallConstantFunction(
7799 Handle<JSFunction> jsfun, int argument_count) {
7800 HValue* target = Add<HConstant>(jsfun);
7801 // For constant functions, we try to avoid calling the
7802 // argument adaptor and instead call the function directly
7803 int formal_parameter_count =
7804 jsfun->shared()->internal_formal_parameter_count();
7805 bool dont_adapt_arguments =
7806 (formal_parameter_count ==
7807 SharedFunctionInfo::kDontAdaptArgumentsSentinel);
7808 int arity = argument_count - 1;
7809 bool can_invoke_directly =
7810 dont_adapt_arguments || formal_parameter_count == arity;
7811 if (can_invoke_directly) {
7812 if (jsfun.is_identical_to(current_info()->closure())) {
7813 graph()->MarkRecursive();
7815 return NewPlainFunctionCall(target, argument_count, dont_adapt_arguments);
7817 HValue* param_count_value = Add<HConstant>(formal_parameter_count);
7818 HValue* context = Add<HLoadNamedField>(
7819 target, nullptr, HObjectAccess::ForFunctionContextPointer());
7820 return NewArgumentAdaptorCall(target, context,
7821 argument_count, param_count_value);
7828 class FunctionSorter {
7830 explicit FunctionSorter(int index = 0, int ticks = 0, int size = 0)
7831 : index_(index), ticks_(ticks), size_(size) {}
7833 int index() const { return index_; }
7834 int ticks() const { return ticks_; }
7835 int size() const { return size_; }
7844 inline bool operator<(const FunctionSorter& lhs, const FunctionSorter& rhs) {
7845 int diff = lhs.ticks() - rhs.ticks();
7846 if (diff != 0) return diff > 0;
7847 return lhs.size() < rhs.size();
7851 void HOptimizedGraphBuilder::HandlePolymorphicCallNamed(Call* expr,
7854 Handle<String> name) {
7855 int argument_count = expr->arguments()->length() + 1; // Includes receiver.
7856 FunctionSorter order[kMaxCallPolymorphism];
7858 bool handle_smi = false;
7859 bool handled_string = false;
7860 int ordered_functions = 0;
7863 for (i = 0; i < maps->length() && ordered_functions < kMaxCallPolymorphism;
7865 PropertyAccessInfo info(this, LOAD, maps->at(i), name);
7866 if (info.CanAccessMonomorphic() && info.IsDataConstant() &&
7867 info.constant()->IsJSFunction()) {
7868 if (info.IsStringType()) {
7869 if (handled_string) continue;
7870 handled_string = true;
7872 Handle<JSFunction> target = Handle<JSFunction>::cast(info.constant());
7873 if (info.IsNumberType()) {
7876 expr->set_target(target);
7877 order[ordered_functions++] = FunctionSorter(
7878 i, target->shared()->profiler_ticks(), InliningAstSize(target));
7882 std::sort(order, order + ordered_functions);
7884 if (i < maps->length()) {
7886 ordered_functions = -1;
7889 HBasicBlock* number_block = NULL;
7890 HBasicBlock* join = NULL;
7891 handled_string = false;
7894 for (int fn = 0; fn < ordered_functions; ++fn) {
7895 int i = order[fn].index();
7896 PropertyAccessInfo info(this, LOAD, maps->at(i), name);
7897 if (info.IsStringType()) {
7898 if (handled_string) continue;
7899 handled_string = true;
7901 // Reloads the target.
7902 info.CanAccessMonomorphic();
7903 Handle<JSFunction> target = Handle<JSFunction>::cast(info.constant());
7905 expr->set_target(target);
7907 // Only needed once.
7908 join = graph()->CreateBasicBlock();
7910 HBasicBlock* empty_smi_block = graph()->CreateBasicBlock();
7911 HBasicBlock* not_smi_block = graph()->CreateBasicBlock();
7912 number_block = graph()->CreateBasicBlock();
7913 FinishCurrentBlock(New<HIsSmiAndBranch>(
7914 receiver, empty_smi_block, not_smi_block));
7915 GotoNoSimulate(empty_smi_block, number_block);
7916 set_current_block(not_smi_block);
7918 BuildCheckHeapObject(receiver);
7922 HBasicBlock* if_true = graph()->CreateBasicBlock();
7923 HBasicBlock* if_false = graph()->CreateBasicBlock();
7924 HUnaryControlInstruction* compare;
7926 Handle<Map> map = info.map();
7927 if (info.IsNumberType()) {
7928 Handle<Map> heap_number_map = isolate()->factory()->heap_number_map();
7929 compare = New<HCompareMap>(receiver, heap_number_map, if_true, if_false);
7930 } else if (info.IsStringType()) {
7931 compare = New<HIsStringAndBranch>(receiver, if_true, if_false);
7933 compare = New<HCompareMap>(receiver, map, if_true, if_false);
7935 FinishCurrentBlock(compare);
7937 if (info.IsNumberType()) {
7938 GotoNoSimulate(if_true, number_block);
7939 if_true = number_block;
7942 set_current_block(if_true);
7944 AddCheckPrototypeMaps(info.holder(), map);
7946 HValue* function = Add<HConstant>(expr->target());
7947 environment()->SetExpressionStackAt(0, function);
7949 CHECK_ALIVE(VisitExpressions(expr->arguments()));
7950 bool needs_wrapping = info.NeedsWrappingFor(target);
7951 bool try_inline = FLAG_polymorphic_inlining && !needs_wrapping;
7952 if (FLAG_trace_inlining && try_inline) {
7953 Handle<JSFunction> caller = current_info()->closure();
7954 base::SmartArrayPointer<char> caller_name =
7955 caller->shared()->DebugName()->ToCString();
7956 PrintF("Trying to inline the polymorphic call to %s from %s\n",
7957 name->ToCString().get(),
7960 if (try_inline && TryInlineCall(expr)) {
7961 // Trying to inline will signal that we should bailout from the
7962 // entire compilation by setting stack overflow on the visitor.
7963 if (HasStackOverflow()) return;
7965 // Since HWrapReceiver currently cannot actually wrap numbers and strings,
7966 // use the regular CallFunctionStub for method calls to wrap the receiver.
7967 // TODO(verwaest): Support creation of value wrappers directly in
7969 HInstruction* call = needs_wrapping
7970 ? NewUncasted<HCallFunction>(
7971 function, argument_count, WRAP_AND_CALL)
7972 : BuildCallConstantFunction(target, argument_count);
7973 PushArgumentsFromEnvironment(argument_count);
7974 AddInstruction(call);
7975 Drop(1); // Drop the function.
7976 if (!ast_context()->IsEffect()) Push(call);
7979 if (current_block() != NULL) Goto(join);
7980 set_current_block(if_false);
7983 // Finish up. Unconditionally deoptimize if we've handled all the maps we
7984 // know about and do not want to handle ones we've never seen. Otherwise
7985 // use a generic IC.
7986 if (ordered_functions == maps->length() && FLAG_deoptimize_uncommon_cases) {
7987 FinishExitWithHardDeoptimization(Deoptimizer::kUnknownMapInPolymorphicCall);
7989 Property* prop = expr->expression()->AsProperty();
7990 HInstruction* function = BuildNamedGeneric(
7991 LOAD, prop, receiver, name, NULL, prop->IsUninitialized());
7992 AddInstruction(function);
7994 AddSimulate(prop->LoadId(), REMOVABLE_SIMULATE);
7996 environment()->SetExpressionStackAt(1, function);
7997 environment()->SetExpressionStackAt(0, receiver);
7998 CHECK_ALIVE(VisitExpressions(expr->arguments()));
8000 CallFunctionFlags flags = receiver->type().IsJSObject()
8001 ? NO_CALL_FUNCTION_FLAGS : CALL_AS_METHOD;
8002 HInstruction* call = New<HCallFunction>(
8003 function, argument_count, flags);
8005 PushArgumentsFromEnvironment(argument_count);
8007 Drop(1); // Function.
8010 AddInstruction(call);
8011 if (!ast_context()->IsEffect()) Push(call);
8014 return ast_context()->ReturnInstruction(call, expr->id());
8018 // We assume that control flow is always live after an expression. So
8019 // even without predecessors to the join block, we set it as the exit
8020 // block and continue by adding instructions there.
8021 DCHECK(join != NULL);
8022 if (join->HasPredecessor()) {
8023 set_current_block(join);
8024 join->SetJoinId(expr->id());
8025 if (!ast_context()->IsEffect()) return ast_context()->ReturnValue(Pop());
8027 set_current_block(NULL);
8032 void HOptimizedGraphBuilder::TraceInline(Handle<JSFunction> target,
8033 Handle<JSFunction> caller,
8034 const char* reason) {
8035 if (FLAG_trace_inlining) {
8036 base::SmartArrayPointer<char> target_name =
8037 target->shared()->DebugName()->ToCString();
8038 base::SmartArrayPointer<char> caller_name =
8039 caller->shared()->DebugName()->ToCString();
8040 if (reason == NULL) {
8041 PrintF("Inlined %s called from %s.\n", target_name.get(),
8044 PrintF("Did not inline %s called from %s (%s).\n",
8045 target_name.get(), caller_name.get(), reason);
8051 static const int kNotInlinable = 1000000000;
8054 int HOptimizedGraphBuilder::InliningAstSize(Handle<JSFunction> target) {
8055 if (!FLAG_use_inlining) return kNotInlinable;
8057 // Precondition: call is monomorphic and we have found a target with the
8058 // appropriate arity.
8059 Handle<JSFunction> caller = current_info()->closure();
8060 Handle<SharedFunctionInfo> target_shared(target->shared());
8062 // Always inline functions that force inlining.
8063 if (target_shared->force_inline()) {
8066 if (target->IsBuiltin()) {
8067 return kNotInlinable;
8070 if (target_shared->IsApiFunction()) {
8071 TraceInline(target, caller, "target is api function");
8072 return kNotInlinable;
8075 // Do a quick check on source code length to avoid parsing large
8076 // inlining candidates.
8077 if (target_shared->SourceSize() >
8078 Min(FLAG_max_inlined_source_size, kUnlimitedMaxInlinedSourceSize)) {
8079 TraceInline(target, caller, "target text too big");
8080 return kNotInlinable;
8083 // Target must be inlineable.
8084 if (!target_shared->IsInlineable()) {
8085 TraceInline(target, caller, "target not inlineable");
8086 return kNotInlinable;
8088 if (target_shared->disable_optimization_reason() != kNoReason) {
8089 TraceInline(target, caller, "target contains unsupported syntax [early]");
8090 return kNotInlinable;
8093 int nodes_added = target_shared->ast_node_count();
8098 bool HOptimizedGraphBuilder::TryInline(Handle<JSFunction> target,
8099 int arguments_count,
8100 HValue* implicit_return_value,
8101 BailoutId ast_id, BailoutId return_id,
8102 InliningKind inlining_kind) {
8103 if (target->context()->native_context() !=
8104 top_info()->closure()->context()->native_context()) {
8107 int nodes_added = InliningAstSize(target);
8108 if (nodes_added == kNotInlinable) return false;
8110 Handle<JSFunction> caller = current_info()->closure();
8112 if (nodes_added > Min(FLAG_max_inlined_nodes, kUnlimitedMaxInlinedNodes)) {
8113 TraceInline(target, caller, "target AST is too large [early]");
8117 // Don't inline deeper than the maximum number of inlining levels.
8118 HEnvironment* env = environment();
8119 int current_level = 1;
8120 while (env->outer() != NULL) {
8121 if (current_level == FLAG_max_inlining_levels) {
8122 TraceInline(target, caller, "inline depth limit reached");
8125 if (env->outer()->frame_type() == JS_FUNCTION) {
8131 // Don't inline recursive functions.
8132 for (FunctionState* state = function_state();
8134 state = state->outer()) {
8135 if (*state->compilation_info()->closure() == *target) {
8136 TraceInline(target, caller, "target is recursive");
8141 // We don't want to add more than a certain number of nodes from inlining.
8142 // Always inline small methods (<= 10 nodes).
8143 if (inlined_count_ > Min(FLAG_max_inlined_nodes_cumulative,
8144 kUnlimitedMaxInlinedNodesCumulative)) {
8145 TraceInline(target, caller, "cumulative AST node limit reached");
8149 // Parse and allocate variables.
8150 // Use the same AstValueFactory for creating strings in the sub-compilation
8151 // step, but don't transfer ownership to target_info.
8152 ParseInfo parse_info(zone(), target);
8153 parse_info.set_ast_value_factory(
8154 top_info()->parse_info()->ast_value_factory());
8155 parse_info.set_ast_value_factory_owned(false);
8157 CompilationInfo target_info(&parse_info);
8158 Handle<SharedFunctionInfo> target_shared(target->shared());
8159 if (target_shared->HasDebugInfo()) {
8160 TraceInline(target, caller, "target is being debugged");
8163 if (!Compiler::ParseAndAnalyze(target_info.parse_info())) {
8164 if (target_info.isolate()->has_pending_exception()) {
8165 // Parse or scope error, never optimize this function.
8167 target_shared->DisableOptimization(kParseScopeError);
8169 TraceInline(target, caller, "parse failure");
8173 if (target_info.scope()->num_heap_slots() > 0) {
8174 TraceInline(target, caller, "target has context-allocated variables");
8177 FunctionLiteral* function = target_info.function();
8179 // The following conditions must be checked again after re-parsing, because
8180 // earlier the information might not have been complete due to lazy parsing.
8181 nodes_added = function->ast_node_count();
8182 if (nodes_added > Min(FLAG_max_inlined_nodes, kUnlimitedMaxInlinedNodes)) {
8183 TraceInline(target, caller, "target AST is too large [late]");
8186 if (function->dont_optimize()) {
8187 TraceInline(target, caller, "target contains unsupported syntax [late]");
8191 // If the function uses the arguments object check that inlining of functions
8192 // with arguments object is enabled and the arguments-variable is
8194 if (function->scope()->arguments() != NULL) {
8195 if (!FLAG_inline_arguments) {
8196 TraceInline(target, caller, "target uses arguments object");
8201 // All declarations must be inlineable.
8202 ZoneList<Declaration*>* decls = target_info.scope()->declarations();
8203 int decl_count = decls->length();
8204 for (int i = 0; i < decl_count; ++i) {
8205 if (!decls->at(i)->IsInlineable()) {
8206 TraceInline(target, caller, "target has non-trivial declaration");
8211 // Generate the deoptimization data for the unoptimized version of
8212 // the target function if we don't already have it.
8213 if (!Compiler::EnsureDeoptimizationSupport(&target_info)) {
8214 TraceInline(target, caller, "could not generate deoptimization info");
8218 // In strong mode it is an error to call a function with too few arguments.
8219 // In that case do not inline because then the arity check would be skipped.
8220 if (is_strong(function->language_mode()) &&
8221 arguments_count < function->parameter_count()) {
8222 TraceInline(target, caller,
8223 "too few arguments passed to a strong function");
8227 // ----------------------------------------------------------------
8228 // After this point, we've made a decision to inline this function (so
8229 // TryInline should always return true).
8231 // Type-check the inlined function.
8232 DCHECK(target_shared->has_deoptimization_support());
8233 AstTyper::Run(&target_info);
8235 int inlining_id = 0;
8236 if (top_info()->is_tracking_positions()) {
8237 inlining_id = top_info()->TraceInlinedFunction(
8238 target_shared, source_position(), function_state()->inlining_id());
8241 // Save the pending call context. Set up new one for the inlined function.
8242 // The function state is new-allocated because we need to delete it
8243 // in two different places.
8244 FunctionState* target_state =
8245 new FunctionState(this, &target_info, inlining_kind, inlining_id);
8247 HConstant* undefined = graph()->GetConstantUndefined();
8249 HEnvironment* inner_env =
8250 environment()->CopyForInlining(target,
8254 function_state()->inlining_kind());
8256 HConstant* context = Add<HConstant>(Handle<Context>(target->context()));
8257 inner_env->BindContext(context);
8259 // Create a dematerialized arguments object for the function, also copy the
8260 // current arguments values to use them for materialization.
8261 HEnvironment* arguments_env = inner_env->arguments_environment();
8262 int parameter_count = arguments_env->parameter_count();
8263 HArgumentsObject* arguments_object = Add<HArgumentsObject>(parameter_count);
8264 for (int i = 0; i < parameter_count; i++) {
8265 arguments_object->AddArgument(arguments_env->Lookup(i), zone());
8268 // If the function uses arguments object then bind bind one.
8269 if (function->scope()->arguments() != NULL) {
8270 DCHECK(function->scope()->arguments()->IsStackAllocated());
8271 inner_env->Bind(function->scope()->arguments(), arguments_object);
8274 // Capture the state before invoking the inlined function for deopt in the
8275 // inlined function. This simulate has no bailout-id since it's not directly
8276 // reachable for deopt, and is only used to capture the state. If the simulate
8277 // becomes reachable by merging, the ast id of the simulate merged into it is
8279 Add<HSimulate>(BailoutId::None());
8281 current_block()->UpdateEnvironment(inner_env);
8282 Scope* saved_scope = scope();
8283 set_scope(target_info.scope());
8284 HEnterInlined* enter_inlined =
8285 Add<HEnterInlined>(return_id, target, context, arguments_count, function,
8286 function_state()->inlining_kind(),
8287 function->scope()->arguments(), arguments_object);
8288 if (top_info()->is_tracking_positions()) {
8289 enter_inlined->set_inlining_id(inlining_id);
8291 function_state()->set_entry(enter_inlined);
8293 VisitDeclarations(target_info.scope()->declarations());
8294 VisitStatements(function->body());
8295 set_scope(saved_scope);
8296 if (HasStackOverflow()) {
8297 // Bail out if the inline function did, as we cannot residualize a call
8298 // instead, but do not disable optimization for the outer function.
8299 TraceInline(target, caller, "inline graph construction failed");
8300 target_shared->DisableOptimization(kInliningBailedOut);
8301 current_info()->RetryOptimization(kInliningBailedOut);
8302 delete target_state;
8306 // Update inlined nodes count.
8307 inlined_count_ += nodes_added;
8309 Handle<Code> unoptimized_code(target_shared->code());
8310 DCHECK(unoptimized_code->kind() == Code::FUNCTION);
8311 Handle<TypeFeedbackInfo> type_info(
8312 TypeFeedbackInfo::cast(unoptimized_code->type_feedback_info()));
8313 graph()->update_type_change_checksum(type_info->own_type_change_checksum());
8315 TraceInline(target, caller, NULL);
8317 if (current_block() != NULL) {
8318 FunctionState* state = function_state();
8319 if (state->inlining_kind() == CONSTRUCT_CALL_RETURN) {
8320 // Falling off the end of an inlined construct call. In a test context the
8321 // return value will always evaluate to true, in a value context the
8322 // return value is the newly allocated receiver.
8323 if (call_context()->IsTest()) {
8324 Goto(inlined_test_context()->if_true(), state);
8325 } else if (call_context()->IsEffect()) {
8326 Goto(function_return(), state);
8328 DCHECK(call_context()->IsValue());
8329 AddLeaveInlined(implicit_return_value, state);
8331 } else if (state->inlining_kind() == SETTER_CALL_RETURN) {
8332 // Falling off the end of an inlined setter call. The returned value is
8333 // never used, the value of an assignment is always the value of the RHS
8334 // of the assignment.
8335 if (call_context()->IsTest()) {
8336 inlined_test_context()->ReturnValue(implicit_return_value);
8337 } else if (call_context()->IsEffect()) {
8338 Goto(function_return(), state);
8340 DCHECK(call_context()->IsValue());
8341 AddLeaveInlined(implicit_return_value, state);
8344 // Falling off the end of a normal inlined function. This basically means
8345 // returning undefined.
8346 if (call_context()->IsTest()) {
8347 Goto(inlined_test_context()->if_false(), state);
8348 } else if (call_context()->IsEffect()) {
8349 Goto(function_return(), state);
8351 DCHECK(call_context()->IsValue());
8352 AddLeaveInlined(undefined, state);
8357 // Fix up the function exits.
8358 if (inlined_test_context() != NULL) {
8359 HBasicBlock* if_true = inlined_test_context()->if_true();
8360 HBasicBlock* if_false = inlined_test_context()->if_false();
8362 HEnterInlined* entry = function_state()->entry();
8364 // Pop the return test context from the expression context stack.
8365 DCHECK(ast_context() == inlined_test_context());
8366 ClearInlinedTestContext();
8367 delete target_state;
8369 // Forward to the real test context.
8370 if (if_true->HasPredecessor()) {
8371 entry->RegisterReturnTarget(if_true, zone());
8372 if_true->SetJoinId(ast_id);
8373 HBasicBlock* true_target = TestContext::cast(ast_context())->if_true();
8374 Goto(if_true, true_target, function_state());
8376 if (if_false->HasPredecessor()) {
8377 entry->RegisterReturnTarget(if_false, zone());
8378 if_false->SetJoinId(ast_id);
8379 HBasicBlock* false_target = TestContext::cast(ast_context())->if_false();
8380 Goto(if_false, false_target, function_state());
8382 set_current_block(NULL);
8385 } else if (function_return()->HasPredecessor()) {
8386 function_state()->entry()->RegisterReturnTarget(function_return(), zone());
8387 function_return()->SetJoinId(ast_id);
8388 set_current_block(function_return());
8390 set_current_block(NULL);
8392 delete target_state;
8397 bool HOptimizedGraphBuilder::TryInlineCall(Call* expr) {
8398 return TryInline(expr->target(), expr->arguments()->length(), NULL,
8399 expr->id(), expr->ReturnId(), NORMAL_RETURN);
8403 bool HOptimizedGraphBuilder::TryInlineConstruct(CallNew* expr,
8404 HValue* implicit_return_value) {
8405 return TryInline(expr->target(), expr->arguments()->length(),
8406 implicit_return_value, expr->id(), expr->ReturnId(),
8407 CONSTRUCT_CALL_RETURN);
8411 bool HOptimizedGraphBuilder::TryInlineGetter(Handle<JSFunction> getter,
8412 Handle<Map> receiver_map,
8414 BailoutId return_id) {
8415 if (TryInlineApiGetter(getter, receiver_map, ast_id)) return true;
8416 return TryInline(getter, 0, NULL, ast_id, return_id, GETTER_CALL_RETURN);
8420 bool HOptimizedGraphBuilder::TryInlineSetter(Handle<JSFunction> setter,
8421 Handle<Map> receiver_map,
8423 BailoutId assignment_id,
8424 HValue* implicit_return_value) {
8425 if (TryInlineApiSetter(setter, receiver_map, id)) return true;
8426 return TryInline(setter, 1, implicit_return_value, id, assignment_id,
8427 SETTER_CALL_RETURN);
8431 bool HOptimizedGraphBuilder::TryInlineIndirectCall(Handle<JSFunction> function,
8433 int arguments_count) {
8434 return TryInline(function, arguments_count, NULL, expr->id(),
8435 expr->ReturnId(), NORMAL_RETURN);
8439 bool HOptimizedGraphBuilder::TryInlineBuiltinFunctionCall(Call* expr) {
8440 if (!expr->target()->shared()->HasBuiltinFunctionId()) return false;
8441 BuiltinFunctionId id = expr->target()->shared()->builtin_function_id();
8444 if (!FLAG_fast_math) break;
8445 // Fall through if FLAG_fast_math.
8453 if (expr->arguments()->length() == 1) {
8454 HValue* argument = Pop();
8455 Drop(2); // Receiver and function.
8456 HInstruction* op = NewUncasted<HUnaryMathOperation>(argument, id);
8457 ast_context()->ReturnInstruction(op, expr->id());
8462 if (expr->arguments()->length() == 2) {
8463 HValue* right = Pop();
8464 HValue* left = Pop();
8465 Drop(2); // Receiver and function.
8467 HMul::NewImul(isolate(), zone(), context(), left, right);
8468 ast_context()->ReturnInstruction(op, expr->id());
8473 // Not supported for inlining yet.
8481 bool HOptimizedGraphBuilder::IsReadOnlyLengthDescriptor(
8482 Handle<Map> jsarray_map) {
8483 DCHECK(!jsarray_map->is_dictionary_map());
8484 Isolate* isolate = jsarray_map->GetIsolate();
8485 Handle<Name> length_string = isolate->factory()->length_string();
8486 DescriptorArray* descriptors = jsarray_map->instance_descriptors();
8487 int number = descriptors->SearchWithCache(*length_string, *jsarray_map);
8488 DCHECK_NE(DescriptorArray::kNotFound, number);
8489 return descriptors->GetDetails(number).IsReadOnly();
8494 bool HOptimizedGraphBuilder::CanInlineArrayResizeOperation(
8495 Handle<Map> receiver_map) {
8496 return !receiver_map.is_null() &&
8497 receiver_map->instance_type() == JS_ARRAY_TYPE &&
8498 IsFastElementsKind(receiver_map->elements_kind()) &&
8499 !receiver_map->is_dictionary_map() &&
8500 !IsReadOnlyLengthDescriptor(receiver_map) &&
8501 !receiver_map->is_observed() && receiver_map->is_extensible();
8505 bool HOptimizedGraphBuilder::TryInlineBuiltinMethodCall(
8506 Call* expr, Handle<JSFunction> function, Handle<Map> receiver_map,
8507 int args_count_no_receiver) {
8508 if (!function->shared()->HasBuiltinFunctionId()) return false;
8509 BuiltinFunctionId id = function->shared()->builtin_function_id();
8510 int argument_count = args_count_no_receiver + 1; // Plus receiver.
8512 if (receiver_map.is_null()) {
8513 HValue* receiver = environment()->ExpressionStackAt(args_count_no_receiver);
8514 if (receiver->IsConstant() &&
8515 HConstant::cast(receiver)->handle(isolate())->IsHeapObject()) {
8517 handle(Handle<HeapObject>::cast(
8518 HConstant::cast(receiver)->handle(isolate()))->map());
8521 // Try to inline calls like Math.* as operations in the calling function.
8523 case kStringCharCodeAt:
8525 if (argument_count == 2) {
8526 HValue* index = Pop();
8527 HValue* string = Pop();
8528 Drop(1); // Function.
8529 HInstruction* char_code =
8530 BuildStringCharCodeAt(string, index);
8531 if (id == kStringCharCodeAt) {
8532 ast_context()->ReturnInstruction(char_code, expr->id());
8535 AddInstruction(char_code);
8536 HInstruction* result = NewUncasted<HStringCharFromCode>(char_code);
8537 ast_context()->ReturnInstruction(result, expr->id());
8541 case kStringFromCharCode:
8542 if (argument_count == 2) {
8543 HValue* argument = Pop();
8544 Drop(2); // Receiver and function.
8545 HInstruction* result = NewUncasted<HStringCharFromCode>(argument);
8546 ast_context()->ReturnInstruction(result, expr->id());
8551 if (!FLAG_fast_math) break;
8552 // Fall through if FLAG_fast_math.
8560 if (argument_count == 2) {
8561 HValue* argument = Pop();
8562 Drop(2); // Receiver and function.
8563 HInstruction* op = NewUncasted<HUnaryMathOperation>(argument, id);
8564 ast_context()->ReturnInstruction(op, expr->id());
8569 if (argument_count == 3) {
8570 HValue* right = Pop();
8571 HValue* left = Pop();
8572 Drop(2); // Receiver and function.
8573 HInstruction* result = NULL;
8574 // Use sqrt() if exponent is 0.5 or -0.5.
8575 if (right->IsConstant() && HConstant::cast(right)->HasDoubleValue()) {
8576 double exponent = HConstant::cast(right)->DoubleValue();
8577 if (exponent == 0.5) {
8578 result = NewUncasted<HUnaryMathOperation>(left, kMathPowHalf);
8579 } else if (exponent == -0.5) {
8580 HValue* one = graph()->GetConstant1();
8581 HInstruction* sqrt = AddUncasted<HUnaryMathOperation>(
8582 left, kMathPowHalf);
8583 // MathPowHalf doesn't have side effects so there's no need for
8584 // an environment simulation here.
8585 DCHECK(!sqrt->HasObservableSideEffects());
8586 result = NewUncasted<HDiv>(one, sqrt);
8587 } else if (exponent == 2.0) {
8588 result = NewUncasted<HMul>(left, left);
8592 if (result == NULL) {
8593 result = NewUncasted<HPower>(left, right);
8595 ast_context()->ReturnInstruction(result, expr->id());
8601 if (argument_count == 3) {
8602 HValue* right = Pop();
8603 HValue* left = Pop();
8604 Drop(2); // Receiver and function.
8605 HMathMinMax::Operation op = (id == kMathMin) ? HMathMinMax::kMathMin
8606 : HMathMinMax::kMathMax;
8607 HInstruction* result = NewUncasted<HMathMinMax>(left, right, op);
8608 ast_context()->ReturnInstruction(result, expr->id());
8613 if (argument_count == 3) {
8614 HValue* right = Pop();
8615 HValue* left = Pop();
8616 Drop(2); // Receiver and function.
8617 HInstruction* result =
8618 HMul::NewImul(isolate(), zone(), context(), left, right);
8619 ast_context()->ReturnInstruction(result, expr->id());
8624 if (!CanInlineArrayResizeOperation(receiver_map)) return false;
8625 ElementsKind elements_kind = receiver_map->elements_kind();
8627 Drop(args_count_no_receiver);
8629 HValue* reduced_length;
8630 HValue* receiver = Pop();
8632 HValue* checked_object = AddCheckMap(receiver, receiver_map);
8634 Add<HLoadNamedField>(checked_object, nullptr,
8635 HObjectAccess::ForArrayLength(elements_kind));
8637 Drop(1); // Function.
8639 { NoObservableSideEffectsScope scope(this);
8640 IfBuilder length_checker(this);
8642 HValue* bounds_check = length_checker.If<HCompareNumericAndBranch>(
8643 length, graph()->GetConstant0(), Token::EQ);
8644 length_checker.Then();
8646 if (!ast_context()->IsEffect()) Push(graph()->GetConstantUndefined());
8648 length_checker.Else();
8649 HValue* elements = AddLoadElements(checked_object);
8650 // Ensure that we aren't popping from a copy-on-write array.
8651 if (IsFastSmiOrObjectElementsKind(elements_kind)) {
8652 elements = BuildCopyElementsOnWrite(checked_object, elements,
8653 elements_kind, length);
8655 reduced_length = AddUncasted<HSub>(length, graph()->GetConstant1());
8656 result = AddElementAccess(elements, reduced_length, NULL,
8657 bounds_check, elements_kind, LOAD);
8658 HValue* hole = IsFastSmiOrObjectElementsKind(elements_kind)
8659 ? graph()->GetConstantHole()
8660 : Add<HConstant>(HConstant::kHoleNaN);
8661 if (IsFastSmiOrObjectElementsKind(elements_kind)) {
8662 elements_kind = FAST_HOLEY_ELEMENTS;
8665 elements, reduced_length, hole, bounds_check, elements_kind, STORE);
8666 Add<HStoreNamedField>(
8667 checked_object, HObjectAccess::ForArrayLength(elements_kind),
8668 reduced_length, STORE_TO_INITIALIZED_ENTRY);
8670 if (!ast_context()->IsEffect()) Push(result);
8672 length_checker.End();
8674 result = ast_context()->IsEffect() ? graph()->GetConstant0() : Top();
8675 Add<HSimulate>(expr->id(), REMOVABLE_SIMULATE);
8676 if (!ast_context()->IsEffect()) Drop(1);
8678 ast_context()->ReturnValue(result);
8682 if (!CanInlineArrayResizeOperation(receiver_map)) return false;
8683 ElementsKind elements_kind = receiver_map->elements_kind();
8685 // If there may be elements accessors in the prototype chain, the fast
8686 // inlined version can't be used.
8687 if (receiver_map->DictionaryElementsInPrototypeChainOnly()) return false;
8688 // If there currently can be no elements accessors on the prototype chain,
8689 // it doesn't mean that there won't be any later. Install a full prototype
8690 // chain check to trap element accessors being installed on the prototype
8691 // chain, which would cause elements to go to dictionary mode and result
8693 Handle<JSObject> prototype(JSObject::cast(receiver_map->prototype()));
8694 BuildCheckPrototypeMaps(prototype, Handle<JSObject>());
8696 // Protect against adding elements to the Array prototype, which needs to
8697 // route through appropriate bottlenecks.
8698 if (isolate()->IsFastArrayConstructorPrototypeChainIntact() &&
8699 !prototype->IsJSArray()) {
8703 const int argc = args_count_no_receiver;
8704 if (argc != 1) return false;
8706 HValue* value_to_push = Pop();
8707 HValue* array = Pop();
8708 Drop(1); // Drop function.
8710 HInstruction* new_size = NULL;
8711 HValue* length = NULL;
8714 NoObservableSideEffectsScope scope(this);
8716 length = Add<HLoadNamedField>(
8717 array, nullptr, HObjectAccess::ForArrayLength(elements_kind));
8719 new_size = AddUncasted<HAdd>(length, graph()->GetConstant1());
8721 bool is_array = receiver_map->instance_type() == JS_ARRAY_TYPE;
8722 HValue* checked_array = Add<HCheckMaps>(array, receiver_map);
8723 BuildUncheckedMonomorphicElementAccess(
8724 checked_array, length, value_to_push, is_array, elements_kind,
8725 STORE, NEVER_RETURN_HOLE, STORE_AND_GROW_NO_TRANSITION);
8727 if (!ast_context()->IsEffect()) Push(new_size);
8728 Add<HSimulate>(expr->id(), REMOVABLE_SIMULATE);
8729 if (!ast_context()->IsEffect()) Drop(1);
8732 ast_context()->ReturnValue(new_size);
8736 if (!CanInlineArrayResizeOperation(receiver_map)) return false;
8737 ElementsKind kind = receiver_map->elements_kind();
8739 // If there may be elements accessors in the prototype chain, the fast
8740 // inlined version can't be used.
8741 if (receiver_map->DictionaryElementsInPrototypeChainOnly()) return false;
8743 // If there currently can be no elements accessors on the prototype chain,
8744 // it doesn't mean that there won't be any later. Install a full prototype
8745 // chain check to trap element accessors being installed on the prototype
8746 // chain, which would cause elements to go to dictionary mode and result
8748 BuildCheckPrototypeMaps(
8749 handle(JSObject::cast(receiver_map->prototype()), isolate()),
8750 Handle<JSObject>::null());
8752 // Threshold for fast inlined Array.shift().
8753 HConstant* inline_threshold = Add<HConstant>(static_cast<int32_t>(16));
8755 Drop(args_count_no_receiver);
8756 HValue* receiver = Pop();
8757 HValue* function = Pop();
8761 NoObservableSideEffectsScope scope(this);
8763 HValue* length = Add<HLoadNamedField>(
8764 receiver, nullptr, HObjectAccess::ForArrayLength(kind));
8766 IfBuilder if_lengthiszero(this);
8767 HValue* lengthiszero = if_lengthiszero.If<HCompareNumericAndBranch>(
8768 length, graph()->GetConstant0(), Token::EQ);
8769 if_lengthiszero.Then();
8771 if (!ast_context()->IsEffect()) Push(graph()->GetConstantUndefined());
8773 if_lengthiszero.Else();
8775 HValue* elements = AddLoadElements(receiver);
8777 // Check if we can use the fast inlined Array.shift().
8778 IfBuilder if_inline(this);
8779 if_inline.If<HCompareNumericAndBranch>(
8780 length, inline_threshold, Token::LTE);
8781 if (IsFastSmiOrObjectElementsKind(kind)) {
8782 // We cannot handle copy-on-write backing stores here.
8783 if_inline.AndIf<HCompareMap>(
8784 elements, isolate()->factory()->fixed_array_map());
8788 // Remember the result.
8789 if (!ast_context()->IsEffect()) {
8790 Push(AddElementAccess(elements, graph()->GetConstant0(), NULL,
8791 lengthiszero, kind, LOAD));
8794 // Compute the new length.
8795 HValue* new_length = AddUncasted<HSub>(
8796 length, graph()->GetConstant1());
8797 new_length->ClearFlag(HValue::kCanOverflow);
8799 // Copy the remaining elements.
8800 LoopBuilder loop(this, context(), LoopBuilder::kPostIncrement);
8802 HValue* new_key = loop.BeginBody(
8803 graph()->GetConstant0(), new_length, Token::LT);
8804 HValue* key = AddUncasted<HAdd>(new_key, graph()->GetConstant1());
8805 key->ClearFlag(HValue::kCanOverflow);
8806 ElementsKind copy_kind =
8807 kind == FAST_HOLEY_SMI_ELEMENTS ? FAST_HOLEY_ELEMENTS : kind;
8808 HValue* element = AddUncasted<HLoadKeyed>(
8809 elements, key, lengthiszero, copy_kind, ALLOW_RETURN_HOLE);
8810 HStoreKeyed* store =
8811 Add<HStoreKeyed>(elements, new_key, element, copy_kind);
8812 store->SetFlag(HValue::kAllowUndefinedAsNaN);
8816 // Put a hole at the end.
8817 HValue* hole = IsFastSmiOrObjectElementsKind(kind)
8818 ? graph()->GetConstantHole()
8819 : Add<HConstant>(HConstant::kHoleNaN);
8820 if (IsFastSmiOrObjectElementsKind(kind)) kind = FAST_HOLEY_ELEMENTS;
8822 elements, new_length, hole, kind, INITIALIZING_STORE);
8824 // Remember new length.
8825 Add<HStoreNamedField>(
8826 receiver, HObjectAccess::ForArrayLength(kind),
8827 new_length, STORE_TO_INITIALIZED_ENTRY);
8831 Add<HPushArguments>(receiver);
8832 result = Add<HCallJSFunction>(function, 1, true);
8833 if (!ast_context()->IsEffect()) Push(result);
8837 if_lengthiszero.End();
8839 result = ast_context()->IsEffect() ? graph()->GetConstant0() : Top();
8840 Add<HSimulate>(expr->id(), REMOVABLE_SIMULATE);
8841 if (!ast_context()->IsEffect()) Drop(1);
8842 ast_context()->ReturnValue(result);
8846 case kArrayLastIndexOf: {
8847 if (receiver_map.is_null()) return false;
8848 if (receiver_map->instance_type() != JS_ARRAY_TYPE) return false;
8849 ElementsKind kind = receiver_map->elements_kind();
8850 if (!IsFastElementsKind(kind)) return false;
8851 if (receiver_map->is_observed()) return false;
8852 if (argument_count != 2) return false;
8853 if (!receiver_map->is_extensible()) return false;
8855 // If there may be elements accessors in the prototype chain, the fast
8856 // inlined version can't be used.
8857 if (receiver_map->DictionaryElementsInPrototypeChainOnly()) return false;
8859 // If there currently can be no elements accessors on the prototype chain,
8860 // it doesn't mean that there won't be any later. Install a full prototype
8861 // chain check to trap element accessors being installed on the prototype
8862 // chain, which would cause elements to go to dictionary mode and result
8864 BuildCheckPrototypeMaps(
8865 handle(JSObject::cast(receiver_map->prototype()), isolate()),
8866 Handle<JSObject>::null());
8868 HValue* search_element = Pop();
8869 HValue* receiver = Pop();
8870 Drop(1); // Drop function.
8872 ArrayIndexOfMode mode = (id == kArrayIndexOf)
8873 ? kFirstIndexOf : kLastIndexOf;
8874 HValue* index = BuildArrayIndexOf(receiver, search_element, kind, mode);
8876 if (!ast_context()->IsEffect()) Push(index);
8877 Add<HSimulate>(expr->id(), REMOVABLE_SIMULATE);
8878 if (!ast_context()->IsEffect()) Drop(1);
8879 ast_context()->ReturnValue(index);
8883 // Not yet supported for inlining.
8890 bool HOptimizedGraphBuilder::TryInlineApiFunctionCall(Call* expr,
8892 Handle<JSFunction> function = expr->target();
8893 int argc = expr->arguments()->length();
8894 SmallMapList receiver_maps;
8895 return TryInlineApiCall(function,
8904 bool HOptimizedGraphBuilder::TryInlineApiMethodCall(
8907 SmallMapList* receiver_maps) {
8908 Handle<JSFunction> function = expr->target();
8909 int argc = expr->arguments()->length();
8910 return TryInlineApiCall(function,
8919 bool HOptimizedGraphBuilder::TryInlineApiGetter(Handle<JSFunction> function,
8920 Handle<Map> receiver_map,
8922 SmallMapList receiver_maps(1, zone());
8923 receiver_maps.Add(receiver_map, zone());
8924 return TryInlineApiCall(function,
8925 NULL, // Receiver is on expression stack.
8933 bool HOptimizedGraphBuilder::TryInlineApiSetter(Handle<JSFunction> function,
8934 Handle<Map> receiver_map,
8936 SmallMapList receiver_maps(1, zone());
8937 receiver_maps.Add(receiver_map, zone());
8938 return TryInlineApiCall(function,
8939 NULL, // Receiver is on expression stack.
8947 bool HOptimizedGraphBuilder::TryInlineApiCall(Handle<JSFunction> function,
8949 SmallMapList* receiver_maps,
8952 ApiCallType call_type) {
8953 if (function->context()->native_context() !=
8954 top_info()->closure()->context()->native_context()) {
8957 CallOptimization optimization(function);
8958 if (!optimization.is_simple_api_call()) return false;
8959 Handle<Map> holder_map;
8960 for (int i = 0; i < receiver_maps->length(); ++i) {
8961 auto map = receiver_maps->at(i);
8962 // Don't inline calls to receivers requiring accesschecks.
8963 if (map->is_access_check_needed()) return false;
8965 if (call_type == kCallApiFunction) {
8966 // Cannot embed a direct reference to the global proxy map
8967 // as it maybe dropped on deserialization.
8968 CHECK(!isolate()->serializer_enabled());
8969 DCHECK_EQ(0, receiver_maps->length());
8970 receiver_maps->Add(handle(function->global_proxy()->map()), zone());
8972 CallOptimization::HolderLookup holder_lookup =
8973 CallOptimization::kHolderNotFound;
8974 Handle<JSObject> api_holder = optimization.LookupHolderOfExpectedType(
8975 receiver_maps->first(), &holder_lookup);
8976 if (holder_lookup == CallOptimization::kHolderNotFound) return false;
8978 if (FLAG_trace_inlining) {
8979 PrintF("Inlining api function ");
8980 function->ShortPrint();
8984 bool is_function = false;
8985 bool is_store = false;
8986 switch (call_type) {
8987 case kCallApiFunction:
8988 case kCallApiMethod:
8989 // Need to check that none of the receiver maps could have changed.
8990 Add<HCheckMaps>(receiver, receiver_maps);
8991 // Need to ensure the chain between receiver and api_holder is intact.
8992 if (holder_lookup == CallOptimization::kHolderFound) {
8993 AddCheckPrototypeMaps(api_holder, receiver_maps->first());
8995 DCHECK_EQ(holder_lookup, CallOptimization::kHolderIsReceiver);
8997 // Includes receiver.
8998 PushArgumentsFromEnvironment(argc + 1);
9001 case kCallApiGetter:
9002 // Receiver and prototype chain cannot have changed.
9004 DCHECK_NULL(receiver);
9005 // Receiver is on expression stack.
9007 Add<HPushArguments>(receiver);
9009 case kCallApiSetter:
9012 // Receiver and prototype chain cannot have changed.
9014 DCHECK_NULL(receiver);
9015 // Receiver and value are on expression stack.
9016 HValue* value = Pop();
9018 Add<HPushArguments>(receiver, value);
9023 HValue* holder = NULL;
9024 switch (holder_lookup) {
9025 case CallOptimization::kHolderFound:
9026 holder = Add<HConstant>(api_holder);
9028 case CallOptimization::kHolderIsReceiver:
9031 case CallOptimization::kHolderNotFound:
9035 Handle<CallHandlerInfo> api_call_info = optimization.api_call_info();
9036 Handle<Object> call_data_obj(api_call_info->data(), isolate());
9037 bool call_data_undefined = call_data_obj->IsUndefined();
9038 HValue* call_data = Add<HConstant>(call_data_obj);
9039 ApiFunction fun(v8::ToCData<Address>(api_call_info->callback()));
9040 ExternalReference ref = ExternalReference(&fun,
9041 ExternalReference::DIRECT_API_CALL,
9043 HValue* api_function_address = Add<HConstant>(ExternalReference(ref));
9045 HValue* op_vals[] = {context(), Add<HConstant>(function), call_data, holder,
9046 api_function_address, nullptr};
9048 HInstruction* call = nullptr;
9050 CallApiAccessorStub stub(isolate(), is_store, call_data_undefined);
9051 Handle<Code> code = stub.GetCode();
9052 HConstant* code_value = Add<HConstant>(code);
9053 ApiAccessorDescriptor descriptor(isolate());
9054 call = New<HCallWithDescriptor>(
9055 code_value, argc + 1, descriptor,
9056 Vector<HValue*>(op_vals, arraysize(op_vals) - 1));
9057 } else if (argc <= CallApiFunctionWithFixedArgsStub::kMaxFixedArgs) {
9058 CallApiFunctionWithFixedArgsStub stub(isolate(), argc, call_data_undefined);
9059 Handle<Code> code = stub.GetCode();
9060 HConstant* code_value = Add<HConstant>(code);
9061 ApiFunctionWithFixedArgsDescriptor descriptor(isolate());
9062 call = New<HCallWithDescriptor>(
9063 code_value, argc + 1, descriptor,
9064 Vector<HValue*>(op_vals, arraysize(op_vals) - 1));
9065 Drop(1); // Drop function.
9067 op_vals[arraysize(op_vals) - 1] = Add<HConstant>(argc);
9068 CallApiFunctionStub stub(isolate(), call_data_undefined);
9069 Handle<Code> code = stub.GetCode();
9070 HConstant* code_value = Add<HConstant>(code);
9071 ApiFunctionDescriptor descriptor(isolate());
9073 New<HCallWithDescriptor>(code_value, argc + 1, descriptor,
9074 Vector<HValue*>(op_vals, arraysize(op_vals)));
9075 Drop(1); // Drop function.
9078 ast_context()->ReturnInstruction(call, ast_id);
9083 void HOptimizedGraphBuilder::HandleIndirectCall(Call* expr, HValue* function,
9084 int arguments_count) {
9085 Handle<JSFunction> known_function;
9086 int args_count_no_receiver = arguments_count - 1;
9087 if (function->IsConstant() &&
9088 HConstant::cast(function)->handle(isolate())->IsJSFunction()) {
9090 Handle<JSFunction>::cast(HConstant::cast(function)->handle(isolate()));
9091 if (TryInlineBuiltinMethodCall(expr, known_function, Handle<Map>(),
9092 args_count_no_receiver)) {
9093 if (FLAG_trace_inlining) {
9094 PrintF("Inlining builtin ");
9095 known_function->ShortPrint();
9101 if (TryInlineIndirectCall(known_function, expr, args_count_no_receiver)) {
9106 PushArgumentsFromEnvironment(arguments_count);
9107 HInvokeFunction* call =
9108 New<HInvokeFunction>(function, known_function, arguments_count);
9109 Drop(1); // Function
9110 ast_context()->ReturnInstruction(call, expr->id());
9114 bool HOptimizedGraphBuilder::TryIndirectCall(Call* expr) {
9115 DCHECK(expr->expression()->IsProperty());
9117 if (!expr->IsMonomorphic()) {
9120 Handle<Map> function_map = expr->GetReceiverTypes()->first();
9121 if (function_map->instance_type() != JS_FUNCTION_TYPE ||
9122 !expr->target()->shared()->HasBuiltinFunctionId()) {
9126 switch (expr->target()->shared()->builtin_function_id()) {
9127 case kFunctionCall: {
9128 if (expr->arguments()->length() == 0) return false;
9129 BuildFunctionCall(expr);
9132 case kFunctionApply: {
9133 // For .apply, only the pattern f.apply(receiver, arguments)
9135 if (current_info()->scope()->arguments() == NULL) return false;
9137 if (!CanBeFunctionApplyArguments(expr)) return false;
9139 BuildFunctionApply(expr);
9142 default: { return false; }
9148 void HOptimizedGraphBuilder::BuildFunctionApply(Call* expr) {
9149 ZoneList<Expression*>* args = expr->arguments();
9150 CHECK_ALIVE(VisitForValue(args->at(0)));
9151 HValue* receiver = Pop(); // receiver
9152 HValue* function = Pop(); // f
9155 Handle<Map> function_map = expr->GetReceiverTypes()->first();
9156 HValue* checked_function = AddCheckMap(function, function_map);
9158 if (function_state()->outer() == NULL) {
9159 HInstruction* elements = Add<HArgumentsElements>(false);
9160 HInstruction* length = Add<HArgumentsLength>(elements);
9161 HValue* wrapped_receiver = BuildWrapReceiver(receiver, checked_function);
9162 HInstruction* result = New<HApplyArguments>(function,
9166 ast_context()->ReturnInstruction(result, expr->id());
9168 // We are inside inlined function and we know exactly what is inside
9169 // arguments object. But we need to be able to materialize at deopt.
9170 DCHECK_EQ(environment()->arguments_environment()->parameter_count(),
9171 function_state()->entry()->arguments_object()->arguments_count());
9172 HArgumentsObject* args = function_state()->entry()->arguments_object();
9173 const ZoneList<HValue*>* arguments_values = args->arguments_values();
9174 int arguments_count = arguments_values->length();
9176 Push(BuildWrapReceiver(receiver, checked_function));
9177 for (int i = 1; i < arguments_count; i++) {
9178 Push(arguments_values->at(i));
9180 HandleIndirectCall(expr, function, arguments_count);
9186 void HOptimizedGraphBuilder::BuildFunctionCall(Call* expr) {
9187 HValue* function = Top(); // f
9188 Handle<Map> function_map = expr->GetReceiverTypes()->first();
9189 HValue* checked_function = AddCheckMap(function, function_map);
9191 // f and call are on the stack in the unoptimized code
9192 // during evaluation of the arguments.
9193 CHECK_ALIVE(VisitExpressions(expr->arguments()));
9195 int args_length = expr->arguments()->length();
9196 int receiver_index = args_length - 1;
9197 // Patch the receiver.
9198 HValue* receiver = BuildWrapReceiver(
9199 environment()->ExpressionStackAt(receiver_index), checked_function);
9200 environment()->SetExpressionStackAt(receiver_index, receiver);
9202 // Call must not be on the stack from now on.
9203 int call_index = args_length + 1;
9204 environment()->RemoveExpressionStackAt(call_index);
9206 HandleIndirectCall(expr, function, args_length);
9210 HValue* HOptimizedGraphBuilder::ImplicitReceiverFor(HValue* function,
9211 Handle<JSFunction> target) {
9212 SharedFunctionInfo* shared = target->shared();
9213 if (is_sloppy(shared->language_mode()) && !shared->native()) {
9214 // Cannot embed a direct reference to the global proxy
9215 // as is it dropped on deserialization.
9216 CHECK(!isolate()->serializer_enabled());
9217 Handle<JSObject> global_proxy(target->context()->global_proxy());
9218 return Add<HConstant>(global_proxy);
9220 return graph()->GetConstantUndefined();
9224 void HOptimizedGraphBuilder::BuildArrayCall(Expression* expression,
9225 int arguments_count,
9227 Handle<AllocationSite> site) {
9228 Add<HCheckValue>(function, array_function());
9230 if (IsCallArrayInlineable(arguments_count, site)) {
9231 BuildInlinedCallArray(expression, arguments_count, site);
9235 HInstruction* call = PreProcessCall(New<HCallNewArray>(
9236 function, arguments_count + 1, site->GetElementsKind(), site));
9237 if (expression->IsCall()) {
9240 ast_context()->ReturnInstruction(call, expression->id());
9244 HValue* HOptimizedGraphBuilder::BuildArrayIndexOf(HValue* receiver,
9245 HValue* search_element,
9247 ArrayIndexOfMode mode) {
9248 DCHECK(IsFastElementsKind(kind));
9250 NoObservableSideEffectsScope no_effects(this);
9252 HValue* elements = AddLoadElements(receiver);
9253 HValue* length = AddLoadArrayLength(receiver, kind);
9256 HValue* terminating;
9258 LoopBuilder::Direction direction;
9259 if (mode == kFirstIndexOf) {
9260 initial = graph()->GetConstant0();
9261 terminating = length;
9263 direction = LoopBuilder::kPostIncrement;
9265 DCHECK_EQ(kLastIndexOf, mode);
9267 terminating = graph()->GetConstant0();
9269 direction = LoopBuilder::kPreDecrement;
9272 Push(graph()->GetConstantMinus1());
9273 if (IsFastDoubleElementsKind(kind) || IsFastSmiElementsKind(kind)) {
9274 // Make sure that we can actually compare numbers correctly below, see
9275 // https://code.google.com/p/chromium/issues/detail?id=407946 for details.
9276 search_element = AddUncasted<HForceRepresentation>(
9277 search_element, IsFastSmiElementsKind(kind) ? Representation::Smi()
9278 : Representation::Double());
9280 LoopBuilder loop(this, context(), direction);
9282 HValue* index = loop.BeginBody(initial, terminating, token);
9283 HValue* element = AddUncasted<HLoadKeyed>(elements, index, nullptr, kind,
9285 IfBuilder if_issame(this);
9286 if_issame.If<HCompareNumericAndBranch>(element, search_element,
9298 IfBuilder if_isstring(this);
9299 if_isstring.If<HIsStringAndBranch>(search_element);
9302 LoopBuilder loop(this, context(), direction);
9304 HValue* index = loop.BeginBody(initial, terminating, token);
9305 HValue* element = AddUncasted<HLoadKeyed>(elements, index, nullptr,
9306 kind, ALLOW_RETURN_HOLE);
9307 IfBuilder if_issame(this);
9308 if_issame.If<HIsStringAndBranch>(element);
9309 if_issame.AndIf<HStringCompareAndBranch>(
9310 element, search_element, Token::EQ_STRICT);
9323 IfBuilder if_isnumber(this);
9324 if_isnumber.If<HIsSmiAndBranch>(search_element);
9325 if_isnumber.OrIf<HCompareMap>(
9326 search_element, isolate()->factory()->heap_number_map());
9329 HValue* search_number =
9330 AddUncasted<HForceRepresentation>(search_element,
9331 Representation::Double());
9332 LoopBuilder loop(this, context(), direction);
9334 HValue* index = loop.BeginBody(initial, terminating, token);
9335 HValue* element = AddUncasted<HLoadKeyed>(elements, index, nullptr,
9336 kind, ALLOW_RETURN_HOLE);
9338 IfBuilder if_element_isnumber(this);
9339 if_element_isnumber.If<HIsSmiAndBranch>(element);
9340 if_element_isnumber.OrIf<HCompareMap>(
9341 element, isolate()->factory()->heap_number_map());
9342 if_element_isnumber.Then();
9345 AddUncasted<HForceRepresentation>(element,
9346 Representation::Double());
9347 IfBuilder if_issame(this);
9348 if_issame.If<HCompareNumericAndBranch>(
9349 number, search_number, Token::EQ_STRICT);
9358 if_element_isnumber.End();
9364 LoopBuilder loop(this, context(), direction);
9366 HValue* index = loop.BeginBody(initial, terminating, token);
9367 HValue* element = AddUncasted<HLoadKeyed>(elements, index, nullptr,
9368 kind, ALLOW_RETURN_HOLE);
9369 IfBuilder if_issame(this);
9370 if_issame.If<HCompareObjectEqAndBranch>(
9371 element, search_element);
9391 bool HOptimizedGraphBuilder::TryHandleArrayCall(Call* expr, HValue* function) {
9392 if (!array_function().is_identical_to(expr->target())) {
9396 Handle<AllocationSite> site = expr->allocation_site();
9397 if (site.is_null()) return false;
9399 BuildArrayCall(expr,
9400 expr->arguments()->length(),
9407 bool HOptimizedGraphBuilder::TryHandleArrayCallNew(CallNew* expr,
9409 if (!array_function().is_identical_to(expr->target())) {
9413 Handle<AllocationSite> site = expr->allocation_site();
9414 if (site.is_null()) return false;
9416 BuildArrayCall(expr, expr->arguments()->length(), function, site);
9421 bool HOptimizedGraphBuilder::CanBeFunctionApplyArguments(Call* expr) {
9422 ZoneList<Expression*>* args = expr->arguments();
9423 if (args->length() != 2) return false;
9424 VariableProxy* arg_two = args->at(1)->AsVariableProxy();
9425 if (arg_two == NULL || !arg_two->var()->IsStackAllocated()) return false;
9426 HValue* arg_two_value = LookupAndMakeLive(arg_two->var());
9427 if (!arg_two_value->CheckFlag(HValue::kIsArguments)) return false;
9432 void HOptimizedGraphBuilder::VisitCall(Call* expr) {
9433 DCHECK(!HasStackOverflow());
9434 DCHECK(current_block() != NULL);
9435 DCHECK(current_block()->HasPredecessor());
9436 if (!top_info()->is_tracking_positions()) SetSourcePosition(expr->position());
9437 Expression* callee = expr->expression();
9438 int argument_count = expr->arguments()->length() + 1; // Plus receiver.
9439 HInstruction* call = NULL;
9441 Property* prop = callee->AsProperty();
9443 CHECK_ALIVE(VisitForValue(prop->obj()));
9444 HValue* receiver = Top();
9447 ComputeReceiverTypes(expr, receiver, &maps, zone());
9449 if (prop->key()->IsPropertyName() && maps->length() > 0) {
9450 Handle<String> name = prop->key()->AsLiteral()->AsPropertyName();
9451 PropertyAccessInfo info(this, LOAD, maps->first(), name);
9452 if (!info.CanAccessAsMonomorphic(maps)) {
9453 HandlePolymorphicCallNamed(expr, receiver, maps, name);
9458 if (!prop->key()->IsPropertyName()) {
9459 CHECK_ALIVE(VisitForValue(prop->key()));
9463 CHECK_ALIVE(PushLoad(prop, receiver, key));
9464 HValue* function = Pop();
9466 if (function->IsConstant() &&
9467 HConstant::cast(function)->handle(isolate())->IsJSFunction()) {
9468 // Push the function under the receiver.
9469 environment()->SetExpressionStackAt(0, function);
9472 Handle<JSFunction> known_function = Handle<JSFunction>::cast(
9473 HConstant::cast(function)->handle(isolate()));
9474 expr->set_target(known_function);
9476 if (TryIndirectCall(expr)) return;
9477 CHECK_ALIVE(VisitExpressions(expr->arguments()));
9479 Handle<Map> map = maps->length() == 1 ? maps->first() : Handle<Map>();
9480 if (TryInlineBuiltinMethodCall(expr, known_function, map,
9481 expr->arguments()->length())) {
9482 if (FLAG_trace_inlining) {
9483 PrintF("Inlining builtin ");
9484 known_function->ShortPrint();
9489 if (TryInlineApiMethodCall(expr, receiver, maps)) return;
9491 // Wrap the receiver if necessary.
9492 if (NeedsWrapping(maps->first(), known_function)) {
9493 // Since HWrapReceiver currently cannot actually wrap numbers and
9494 // strings, use the regular CallFunctionStub for method calls to wrap
9496 // TODO(verwaest): Support creation of value wrappers directly in
9498 call = New<HCallFunction>(
9499 function, argument_count, WRAP_AND_CALL);
9500 } else if (TryInlineCall(expr)) {
9503 call = BuildCallConstantFunction(known_function, argument_count);
9507 ArgumentsAllowedFlag arguments_flag = ARGUMENTS_NOT_ALLOWED;
9508 if (CanBeFunctionApplyArguments(expr) && expr->is_uninitialized()) {
9509 // We have to use EAGER deoptimization here because Deoptimizer::SOFT
9510 // gets ignored by the always-opt flag, which leads to incorrect code.
9512 Deoptimizer::kInsufficientTypeFeedbackForCallWithArguments,
9513 Deoptimizer::EAGER);
9514 arguments_flag = ARGUMENTS_FAKED;
9517 // Push the function under the receiver.
9518 environment()->SetExpressionStackAt(0, function);
9521 CHECK_ALIVE(VisitExpressions(expr->arguments(), arguments_flag));
9522 CallFunctionFlags flags = receiver->type().IsJSObject()
9523 ? NO_CALL_FUNCTION_FLAGS : CALL_AS_METHOD;
9524 call = New<HCallFunction>(function, argument_count, flags);
9526 PushArgumentsFromEnvironment(argument_count);
9529 VariableProxy* proxy = expr->expression()->AsVariableProxy();
9530 if (proxy != NULL && proxy->var()->is_possibly_eval(isolate())) {
9531 return Bailout(kPossibleDirectCallToEval);
9534 // The function is on the stack in the unoptimized code during
9535 // evaluation of the arguments.
9536 CHECK_ALIVE(VisitForValue(expr->expression()));
9537 HValue* function = Top();
9538 if (function->IsConstant() &&
9539 HConstant::cast(function)->handle(isolate())->IsJSFunction()) {
9540 Handle<Object> constant = HConstant::cast(function)->handle(isolate());
9541 Handle<JSFunction> target = Handle<JSFunction>::cast(constant);
9542 expr->SetKnownGlobalTarget(target);
9545 // Placeholder for the receiver.
9546 Push(graph()->GetConstantUndefined());
9547 CHECK_ALIVE(VisitExpressions(expr->arguments()));
9549 if (expr->IsMonomorphic()) {
9550 Add<HCheckValue>(function, expr->target());
9552 // Patch the global object on the stack by the expected receiver.
9553 HValue* receiver = ImplicitReceiverFor(function, expr->target());
9554 const int receiver_index = argument_count - 1;
9555 environment()->SetExpressionStackAt(receiver_index, receiver);
9557 if (TryInlineBuiltinFunctionCall(expr)) {
9558 if (FLAG_trace_inlining) {
9559 PrintF("Inlining builtin ");
9560 expr->target()->ShortPrint();
9565 if (TryInlineApiFunctionCall(expr, receiver)) return;
9566 if (TryHandleArrayCall(expr, function)) return;
9567 if (TryInlineCall(expr)) return;
9569 PushArgumentsFromEnvironment(argument_count);
9570 call = BuildCallConstantFunction(expr->target(), argument_count);
9572 PushArgumentsFromEnvironment(argument_count);
9573 HCallFunction* call_function =
9574 New<HCallFunction>(function, argument_count);
9575 call = call_function;
9576 if (expr->is_uninitialized() &&
9577 expr->IsUsingCallFeedbackICSlot(isolate())) {
9578 // We've never seen this call before, so let's have Crankshaft learn
9579 // through the type vector.
9580 Handle<TypeFeedbackVector> vector =
9581 handle(current_feedback_vector(), isolate());
9582 FeedbackVectorICSlot slot = expr->CallFeedbackICSlot();
9583 call_function->SetVectorAndSlot(vector, slot);
9588 Drop(1); // Drop the function.
9589 return ast_context()->ReturnInstruction(call, expr->id());
9593 void HOptimizedGraphBuilder::BuildInlinedCallArray(
9594 Expression* expression,
9596 Handle<AllocationSite> site) {
9597 DCHECK(!site.is_null());
9598 DCHECK(argument_count >= 0 && argument_count <= 1);
9599 NoObservableSideEffectsScope no_effects(this);
9601 // We should at least have the constructor on the expression stack.
9602 HValue* constructor = environment()->ExpressionStackAt(argument_count);
9604 // Register on the site for deoptimization if the transition feedback changes.
9605 top_info()->dependencies()->AssumeTransitionStable(site);
9606 ElementsKind kind = site->GetElementsKind();
9607 HInstruction* site_instruction = Add<HConstant>(site);
9609 // In the single constant argument case, we may have to adjust elements kind
9610 // to avoid creating a packed non-empty array.
9611 if (argument_count == 1 && !IsHoleyElementsKind(kind)) {
9612 HValue* argument = environment()->Top();
9613 if (argument->IsConstant()) {
9614 HConstant* constant_argument = HConstant::cast(argument);
9615 DCHECK(constant_argument->HasSmiValue());
9616 int constant_array_size = constant_argument->Integer32Value();
9617 if (constant_array_size != 0) {
9618 kind = GetHoleyElementsKind(kind);
9624 JSArrayBuilder array_builder(this,
9628 DISABLE_ALLOCATION_SITES);
9629 HValue* new_object = argument_count == 0
9630 ? array_builder.AllocateEmptyArray()
9631 : BuildAllocateArrayFromLength(&array_builder, Top());
9633 int args_to_drop = argument_count + (expression->IsCall() ? 2 : 1);
9635 ast_context()->ReturnValue(new_object);
9639 // Checks whether allocation using the given constructor can be inlined.
9640 static bool IsAllocationInlineable(Handle<JSFunction> constructor) {
9641 return constructor->has_initial_map() &&
9642 constructor->initial_map()->instance_type() == JS_OBJECT_TYPE &&
9643 constructor->initial_map()->instance_size() <
9644 HAllocate::kMaxInlineSize;
9648 bool HOptimizedGraphBuilder::IsCallArrayInlineable(
9650 Handle<AllocationSite> site) {
9651 Handle<JSFunction> caller = current_info()->closure();
9652 Handle<JSFunction> target = array_function();
9653 // We should have the function plus array arguments on the environment stack.
9654 DCHECK(environment()->length() >= (argument_count + 1));
9655 DCHECK(!site.is_null());
9657 bool inline_ok = false;
9658 if (site->CanInlineCall()) {
9659 // We also want to avoid inlining in certain 1 argument scenarios.
9660 if (argument_count == 1) {
9661 HValue* argument = Top();
9662 if (argument->IsConstant()) {
9663 // Do not inline if the constant length argument is not a smi or
9664 // outside the valid range for unrolled loop initialization.
9665 HConstant* constant_argument = HConstant::cast(argument);
9666 if (constant_argument->HasSmiValue()) {
9667 int value = constant_argument->Integer32Value();
9668 inline_ok = value >= 0 && value <= kElementLoopUnrollThreshold;
9670 TraceInline(target, caller,
9671 "Constant length outside of valid inlining range.");
9675 TraceInline(target, caller,
9676 "Dont inline [new] Array(n) where n isn't constant.");
9678 } else if (argument_count == 0) {
9681 TraceInline(target, caller, "Too many arguments to inline.");
9684 TraceInline(target, caller, "AllocationSite requested no inlining.");
9688 TraceInline(target, caller, NULL);
9694 void HOptimizedGraphBuilder::VisitCallNew(CallNew* expr) {
9695 DCHECK(!HasStackOverflow());
9696 DCHECK(current_block() != NULL);
9697 DCHECK(current_block()->HasPredecessor());
9698 if (!top_info()->is_tracking_positions()) SetSourcePosition(expr->position());
9699 int argument_count = expr->arguments()->length() + 1; // Plus constructor.
9700 Factory* factory = isolate()->factory();
9702 // The constructor function is on the stack in the unoptimized code
9703 // during evaluation of the arguments.
9704 CHECK_ALIVE(VisitForValue(expr->expression()));
9705 HValue* function = Top();
9706 CHECK_ALIVE(VisitExpressions(expr->arguments()));
9708 if (function->IsConstant() &&
9709 HConstant::cast(function)->handle(isolate())->IsJSFunction()) {
9710 Handle<Object> constant = HConstant::cast(function)->handle(isolate());
9711 expr->SetKnownGlobalTarget(Handle<JSFunction>::cast(constant));
9714 if (FLAG_inline_construct &&
9715 expr->IsMonomorphic() &&
9716 IsAllocationInlineable(expr->target())) {
9717 Handle<JSFunction> constructor = expr->target();
9718 HValue* check = Add<HCheckValue>(function, constructor);
9720 // Force completion of inobject slack tracking before generating
9721 // allocation code to finalize instance size.
9722 if (constructor->IsInobjectSlackTrackingInProgress()) {
9723 constructor->CompleteInobjectSlackTracking();
9726 // Calculate instance size from initial map of constructor.
9727 DCHECK(constructor->has_initial_map());
9728 Handle<Map> initial_map(constructor->initial_map());
9729 int instance_size = initial_map->instance_size();
9731 // Allocate an instance of the implicit receiver object.
9732 HValue* size_in_bytes = Add<HConstant>(instance_size);
9733 HAllocationMode allocation_mode;
9734 if (FLAG_pretenuring_call_new) {
9735 if (FLAG_allocation_site_pretenuring) {
9736 // Try to use pretenuring feedback.
9737 Handle<AllocationSite> allocation_site = expr->allocation_site();
9738 allocation_mode = HAllocationMode(allocation_site);
9739 // Take a dependency on allocation site.
9740 top_info()->dependencies()->AssumeTenuringDecision(allocation_site);
9744 HAllocate* receiver = BuildAllocate(
9745 size_in_bytes, HType::JSObject(), JS_OBJECT_TYPE, allocation_mode);
9746 receiver->set_known_initial_map(initial_map);
9748 // Initialize map and fields of the newly allocated object.
9749 { NoObservableSideEffectsScope no_effects(this);
9750 DCHECK(initial_map->instance_type() == JS_OBJECT_TYPE);
9751 Add<HStoreNamedField>(receiver,
9752 HObjectAccess::ForMapAndOffset(initial_map, JSObject::kMapOffset),
9753 Add<HConstant>(initial_map));
9754 HValue* empty_fixed_array = Add<HConstant>(factory->empty_fixed_array());
9755 Add<HStoreNamedField>(receiver,
9756 HObjectAccess::ForMapAndOffset(initial_map,
9757 JSObject::kPropertiesOffset),
9759 Add<HStoreNamedField>(receiver,
9760 HObjectAccess::ForMapAndOffset(initial_map,
9761 JSObject::kElementsOffset),
9763 BuildInitializeInobjectProperties(receiver, initial_map);
9766 // Replace the constructor function with a newly allocated receiver using
9767 // the index of the receiver from the top of the expression stack.
9768 const int receiver_index = argument_count - 1;
9769 DCHECK(environment()->ExpressionStackAt(receiver_index) == function);
9770 environment()->SetExpressionStackAt(receiver_index, receiver);
9772 if (TryInlineConstruct(expr, receiver)) {
9773 // Inlining worked, add a dependency on the initial map to make sure that
9774 // this code is deoptimized whenever the initial map of the constructor
9776 top_info()->dependencies()->AssumeInitialMapCantChange(initial_map);
9780 // TODO(mstarzinger): For now we remove the previous HAllocate and all
9781 // corresponding instructions and instead add HPushArguments for the
9782 // arguments in case inlining failed. What we actually should do is for
9783 // inlining to try to build a subgraph without mutating the parent graph.
9784 HInstruction* instr = current_block()->last();
9786 HInstruction* prev_instr = instr->previous();
9787 instr->DeleteAndReplaceWith(NULL);
9789 } while (instr != check);
9790 environment()->SetExpressionStackAt(receiver_index, function);
9791 HInstruction* call =
9792 PreProcessCall(New<HCallNew>(function, argument_count));
9793 return ast_context()->ReturnInstruction(call, expr->id());
9795 // The constructor function is both an operand to the instruction and an
9796 // argument to the construct call.
9797 if (TryHandleArrayCallNew(expr, function)) return;
9799 HInstruction* call =
9800 PreProcessCall(New<HCallNew>(function, argument_count));
9801 return ast_context()->ReturnInstruction(call, expr->id());
9806 void HOptimizedGraphBuilder::BuildInitializeInobjectProperties(
9807 HValue* receiver, Handle<Map> initial_map) {
9808 if (initial_map->inobject_properties() != 0) {
9809 HConstant* undefined = graph()->GetConstantUndefined();
9810 for (int i = 0; i < initial_map->inobject_properties(); i++) {
9811 int property_offset = initial_map->GetInObjectPropertyOffset(i);
9812 Add<HStoreNamedField>(receiver, HObjectAccess::ForMapAndOffset(
9813 initial_map, property_offset),
9820 HValue* HGraphBuilder::BuildAllocateEmptyArrayBuffer(HValue* byte_length) {
9821 // We HForceRepresentation here to avoid allocations during an *-to-tagged
9822 // HChange that could cause GC while the array buffer object is not fully
9824 HObjectAccess byte_length_access(HObjectAccess::ForJSArrayBufferByteLength());
9825 byte_length = AddUncasted<HForceRepresentation>(
9826 byte_length, byte_length_access.representation());
9828 BuildAllocate(Add<HConstant>(JSArrayBuffer::kSizeWithInternalFields),
9829 HType::JSObject(), JS_ARRAY_BUFFER_TYPE, HAllocationMode());
9831 HValue* global_object = Add<HLoadNamedField>(
9833 HObjectAccess::ForContextSlot(Context::GLOBAL_OBJECT_INDEX));
9834 HValue* native_context = Add<HLoadNamedField>(
9835 global_object, nullptr, HObjectAccess::ForGlobalObjectNativeContext());
9836 Add<HStoreNamedField>(
9837 result, HObjectAccess::ForMap(),
9838 Add<HLoadNamedField>(
9839 native_context, nullptr,
9840 HObjectAccess::ForContextSlot(Context::ARRAY_BUFFER_MAP_INDEX)));
9842 HConstant* empty_fixed_array =
9843 Add<HConstant>(isolate()->factory()->empty_fixed_array());
9844 Add<HStoreNamedField>(
9845 result, HObjectAccess::ForJSArrayOffset(JSArray::kPropertiesOffset),
9847 Add<HStoreNamedField>(
9848 result, HObjectAccess::ForJSArrayOffset(JSArray::kElementsOffset),
9850 Add<HStoreNamedField>(
9851 result, HObjectAccess::ForJSArrayBufferBackingStore().WithRepresentation(
9852 Representation::Smi()),
9853 graph()->GetConstant0());
9854 Add<HStoreNamedField>(result, byte_length_access, byte_length);
9855 Add<HStoreNamedField>(result, HObjectAccess::ForJSArrayBufferBitFieldSlot(),
9856 graph()->GetConstant0());
9857 Add<HStoreNamedField>(
9858 result, HObjectAccess::ForJSArrayBufferBitField(),
9859 Add<HConstant>((1 << JSArrayBuffer::IsExternal::kShift) |
9860 (1 << JSArrayBuffer::IsNeuterable::kShift)));
9862 for (int field = 0; field < v8::ArrayBuffer::kInternalFieldCount; ++field) {
9863 Add<HStoreNamedField>(
9865 HObjectAccess::ForObservableJSObjectOffset(
9866 JSArrayBuffer::kSize + field * kPointerSize, Representation::Smi()),
9867 graph()->GetConstant0());
9874 template <class ViewClass>
9875 void HGraphBuilder::BuildArrayBufferViewInitialization(
9878 HValue* byte_offset,
9879 HValue* byte_length) {
9881 for (int offset = ViewClass::kSize;
9882 offset < ViewClass::kSizeWithInternalFields;
9883 offset += kPointerSize) {
9884 Add<HStoreNamedField>(obj,
9885 HObjectAccess::ForObservableJSObjectOffset(offset),
9886 graph()->GetConstant0());
9889 Add<HStoreNamedField>(
9891 HObjectAccess::ForJSArrayBufferViewByteOffset(),
9893 Add<HStoreNamedField>(
9895 HObjectAccess::ForJSArrayBufferViewByteLength(),
9897 Add<HStoreNamedField>(obj, HObjectAccess::ForJSArrayBufferViewBuffer(),
9902 void HOptimizedGraphBuilder::GenerateDataViewInitialize(
9903 CallRuntime* expr) {
9904 ZoneList<Expression*>* arguments = expr->arguments();
9906 DCHECK(arguments->length()== 4);
9907 CHECK_ALIVE(VisitForValue(arguments->at(0)));
9908 HValue* obj = Pop();
9910 CHECK_ALIVE(VisitForValue(arguments->at(1)));
9911 HValue* buffer = Pop();
9913 CHECK_ALIVE(VisitForValue(arguments->at(2)));
9914 HValue* byte_offset = Pop();
9916 CHECK_ALIVE(VisitForValue(arguments->at(3)));
9917 HValue* byte_length = Pop();
9920 NoObservableSideEffectsScope scope(this);
9921 BuildArrayBufferViewInitialization<JSDataView>(
9922 obj, buffer, byte_offset, byte_length);
9927 static Handle<Map> TypedArrayMap(Isolate* isolate,
9928 ExternalArrayType array_type,
9929 ElementsKind target_kind) {
9930 Handle<Context> native_context = isolate->native_context();
9931 Handle<JSFunction> fun;
9932 switch (array_type) {
9933 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
9934 case kExternal##Type##Array: \
9935 fun = Handle<JSFunction>(native_context->type##_array_fun()); \
9938 TYPED_ARRAYS(TYPED_ARRAY_CASE)
9939 #undef TYPED_ARRAY_CASE
9941 Handle<Map> map(fun->initial_map());
9942 return Map::AsElementsKind(map, target_kind);
9946 HValue* HOptimizedGraphBuilder::BuildAllocateExternalElements(
9947 ExternalArrayType array_type,
9948 bool is_zero_byte_offset,
9949 HValue* buffer, HValue* byte_offset, HValue* length) {
9950 Handle<Map> external_array_map(
9951 isolate()->heap()->MapForExternalArrayType(array_type));
9953 // The HForceRepresentation is to prevent possible deopt on int-smi
9954 // conversion after allocation but before the new object fields are set.
9955 length = AddUncasted<HForceRepresentation>(length, Representation::Smi());
9957 Add<HAllocate>(Add<HConstant>(ExternalArray::kSize), HType::HeapObject(),
9958 NOT_TENURED, external_array_map->instance_type());
9960 AddStoreMapConstant(elements, external_array_map);
9961 Add<HStoreNamedField>(elements,
9962 HObjectAccess::ForFixedArrayLength(), length);
9964 HValue* backing_store = Add<HLoadNamedField>(
9965 buffer, nullptr, HObjectAccess::ForJSArrayBufferBackingStore());
9967 HValue* typed_array_start;
9968 if (is_zero_byte_offset) {
9969 typed_array_start = backing_store;
9971 HInstruction* external_pointer =
9972 AddUncasted<HAdd>(backing_store, byte_offset);
9973 // Arguments are checked prior to call to TypedArrayInitialize,
9974 // including byte_offset.
9975 external_pointer->ClearFlag(HValue::kCanOverflow);
9976 typed_array_start = external_pointer;
9979 Add<HStoreNamedField>(elements,
9980 HObjectAccess::ForExternalArrayExternalPointer(),
9987 HValue* HOptimizedGraphBuilder::BuildAllocateFixedTypedArray(
9988 ExternalArrayType array_type, size_t element_size,
9989 ElementsKind fixed_elements_kind, HValue* byte_length, HValue* length,
9992 (FixedTypedArrayBase::kHeaderSize & kObjectAlignmentMask) == 0);
9995 // if fixed array's elements are not aligned to object's alignment,
9996 // we need to align the whole array to object alignment.
9997 if (element_size % kObjectAlignment != 0) {
9998 total_size = BuildObjectSizeAlignment(
9999 byte_length, FixedTypedArrayBase::kHeaderSize);
10001 total_size = AddUncasted<HAdd>(byte_length,
10002 Add<HConstant>(FixedTypedArrayBase::kHeaderSize));
10003 total_size->ClearFlag(HValue::kCanOverflow);
10006 // The HForceRepresentation is to prevent possible deopt on int-smi
10007 // conversion after allocation but before the new object fields are set.
10008 length = AddUncasted<HForceRepresentation>(length, Representation::Smi());
10009 Handle<Map> fixed_typed_array_map(
10010 isolate()->heap()->MapForFixedTypedArray(array_type));
10011 HAllocate* elements =
10012 Add<HAllocate>(total_size, HType::HeapObject(), NOT_TENURED,
10013 fixed_typed_array_map->instance_type());
10015 #ifndef V8_HOST_ARCH_64_BIT
10016 if (array_type == kExternalFloat64Array) {
10017 elements->MakeDoubleAligned();
10021 AddStoreMapConstant(elements, fixed_typed_array_map);
10023 Add<HStoreNamedField>(elements,
10024 HObjectAccess::ForFixedArrayLength(),
10026 Add<HStoreNamedField>(
10027 elements, HObjectAccess::ForFixedTypedArrayBaseBasePointer(), elements);
10029 Add<HStoreNamedField>(
10030 elements, HObjectAccess::ForFixedTypedArrayBaseExternalPointer(),
10031 Add<HConstant>(ExternalReference::fixed_typed_array_base_data_offset()));
10033 HValue* filler = Add<HConstant>(static_cast<int32_t>(0));
10036 LoopBuilder builder(this, context(), LoopBuilder::kPostIncrement);
10038 HValue* backing_store = AddUncasted<HAdd>(
10039 Add<HConstant>(ExternalReference::fixed_typed_array_base_data_offset()),
10040 elements, Strength::WEAK, AddOfExternalAndTagged);
10042 HValue* key = builder.BeginBody(
10043 Add<HConstant>(static_cast<int32_t>(0)),
10044 length, Token::LT);
10045 Add<HStoreKeyed>(backing_store, key, filler, fixed_elements_kind);
10053 void HOptimizedGraphBuilder::GenerateTypedArrayInitialize(
10054 CallRuntime* expr) {
10055 ZoneList<Expression*>* arguments = expr->arguments();
10057 static const int kObjectArg = 0;
10058 static const int kArrayIdArg = 1;
10059 static const int kBufferArg = 2;
10060 static const int kByteOffsetArg = 3;
10061 static const int kByteLengthArg = 4;
10062 static const int kInitializeArg = 5;
10063 static const int kArgsLength = 6;
10064 DCHECK(arguments->length() == kArgsLength);
10067 CHECK_ALIVE(VisitForValue(arguments->at(kObjectArg)));
10068 HValue* obj = Pop();
10070 if (!arguments->at(kArrayIdArg)->IsLiteral()) {
10071 // This should never happen in real use, but can happen when fuzzing.
10073 Bailout(kNeedSmiLiteral);
10076 Handle<Object> value =
10077 static_cast<Literal*>(arguments->at(kArrayIdArg))->value();
10078 if (!value->IsSmi()) {
10079 // This should never happen in real use, but can happen when fuzzing.
10081 Bailout(kNeedSmiLiteral);
10084 int array_id = Smi::cast(*value)->value();
10087 if (!arguments->at(kBufferArg)->IsNullLiteral()) {
10088 CHECK_ALIVE(VisitForValue(arguments->at(kBufferArg)));
10094 HValue* byte_offset;
10095 bool is_zero_byte_offset;
10097 if (arguments->at(kByteOffsetArg)->IsLiteral()
10098 && Smi::FromInt(0) ==
10099 *static_cast<Literal*>(arguments->at(kByteOffsetArg))->value()) {
10100 byte_offset = Add<HConstant>(static_cast<int32_t>(0));
10101 is_zero_byte_offset = true;
10103 CHECK_ALIVE(VisitForValue(arguments->at(kByteOffsetArg)));
10104 byte_offset = Pop();
10105 is_zero_byte_offset = false;
10106 DCHECK(buffer != NULL);
10109 CHECK_ALIVE(VisitForValue(arguments->at(kByteLengthArg)));
10110 HValue* byte_length = Pop();
10112 CHECK(arguments->at(kInitializeArg)->IsLiteral());
10113 bool initialize = static_cast<Literal*>(arguments->at(kInitializeArg))
10117 NoObservableSideEffectsScope scope(this);
10118 IfBuilder byte_offset_smi(this);
10120 if (!is_zero_byte_offset) {
10121 byte_offset_smi.If<HIsSmiAndBranch>(byte_offset);
10122 byte_offset_smi.Then();
10125 ExternalArrayType array_type =
10126 kExternalInt8Array; // Bogus initialization.
10127 size_t element_size = 1; // Bogus initialization.
10128 ElementsKind external_elements_kind = // Bogus initialization.
10129 EXTERNAL_INT8_ELEMENTS;
10130 ElementsKind fixed_elements_kind = // Bogus initialization.
10132 Runtime::ArrayIdToTypeAndSize(array_id,
10134 &external_elements_kind,
10135 &fixed_elements_kind,
10139 { // byte_offset is Smi.
10140 HValue* allocated_buffer = buffer;
10141 if (buffer == NULL) {
10142 allocated_buffer = BuildAllocateEmptyArrayBuffer(byte_length);
10144 BuildArrayBufferViewInitialization<JSTypedArray>(obj, allocated_buffer,
10145 byte_offset, byte_length);
10148 HInstruction* length = AddUncasted<HDiv>(byte_length,
10149 Add<HConstant>(static_cast<int32_t>(element_size)));
10151 Add<HStoreNamedField>(obj,
10152 HObjectAccess::ForJSTypedArrayLength(),
10156 if (buffer != NULL) {
10157 elements = BuildAllocateExternalElements(
10158 array_type, is_zero_byte_offset, buffer, byte_offset, length);
10159 Handle<Map> obj_map = TypedArrayMap(
10160 isolate(), array_type, external_elements_kind);
10161 AddStoreMapConstant(obj, obj_map);
10163 DCHECK(is_zero_byte_offset);
10164 elements = BuildAllocateFixedTypedArray(array_type, element_size,
10165 fixed_elements_kind, byte_length,
10166 length, initialize);
10168 Add<HStoreNamedField>(
10169 obj, HObjectAccess::ForElementsPointer(), elements);
10172 if (!is_zero_byte_offset) {
10173 byte_offset_smi.Else();
10174 { // byte_offset is not Smi.
10176 CHECK_ALIVE(VisitForValue(arguments->at(kArrayIdArg)));
10180 CHECK_ALIVE(VisitForValue(arguments->at(kInitializeArg)));
10181 PushArgumentsFromEnvironment(kArgsLength);
10182 Add<HCallRuntime>(expr->name(), expr->function(), kArgsLength);
10185 byte_offset_smi.End();
10189 void HOptimizedGraphBuilder::GenerateMaxSmi(CallRuntime* expr) {
10190 DCHECK(expr->arguments()->length() == 0);
10191 HConstant* max_smi = New<HConstant>(static_cast<int32_t>(Smi::kMaxValue));
10192 return ast_context()->ReturnInstruction(max_smi, expr->id());
10196 void HOptimizedGraphBuilder::GenerateTypedArrayMaxSizeInHeap(
10197 CallRuntime* expr) {
10198 DCHECK(expr->arguments()->length() == 0);
10199 HConstant* result = New<HConstant>(static_cast<int32_t>(
10200 FLAG_typed_array_max_size_in_heap));
10201 return ast_context()->ReturnInstruction(result, expr->id());
10205 void HOptimizedGraphBuilder::GenerateArrayBufferGetByteLength(
10206 CallRuntime* expr) {
10207 DCHECK(expr->arguments()->length() == 1);
10208 CHECK_ALIVE(VisitForValue(expr->arguments()->at(0)));
10209 HValue* buffer = Pop();
10210 HInstruction* result = New<HLoadNamedField>(
10211 buffer, nullptr, HObjectAccess::ForJSArrayBufferByteLength());
10212 return ast_context()->ReturnInstruction(result, expr->id());
10216 void HOptimizedGraphBuilder::GenerateArrayBufferViewGetByteLength(
10217 CallRuntime* expr) {
10218 NoObservableSideEffectsScope scope(this);
10219 DCHECK(expr->arguments()->length() == 1);
10220 CHECK_ALIVE(VisitForValue(expr->arguments()->at(0)));
10221 HValue* view = Pop();
10223 return ast_context()->ReturnValue(BuildArrayBufferViewFieldAccessor(
10225 FieldIndex::ForInObjectOffset(JSArrayBufferView::kByteLengthOffset)));
10229 void HOptimizedGraphBuilder::GenerateArrayBufferViewGetByteOffset(
10230 CallRuntime* expr) {
10231 NoObservableSideEffectsScope scope(this);
10232 DCHECK(expr->arguments()->length() == 1);
10233 CHECK_ALIVE(VisitForValue(expr->arguments()->at(0)));
10234 HValue* view = Pop();
10236 return ast_context()->ReturnValue(BuildArrayBufferViewFieldAccessor(
10238 FieldIndex::ForInObjectOffset(JSArrayBufferView::kByteOffsetOffset)));
10242 void HOptimizedGraphBuilder::GenerateTypedArrayGetLength(
10243 CallRuntime* expr) {
10244 NoObservableSideEffectsScope scope(this);
10245 DCHECK(expr->arguments()->length() == 1);
10246 CHECK_ALIVE(VisitForValue(expr->arguments()->at(0)));
10247 HValue* view = Pop();
10249 return ast_context()->ReturnValue(BuildArrayBufferViewFieldAccessor(
10251 FieldIndex::ForInObjectOffset(JSTypedArray::kLengthOffset)));
10255 void HOptimizedGraphBuilder::VisitCallRuntime(CallRuntime* expr) {
10256 DCHECK(!HasStackOverflow());
10257 DCHECK(current_block() != NULL);
10258 DCHECK(current_block()->HasPredecessor());
10259 if (expr->is_jsruntime()) {
10260 return Bailout(kCallToAJavaScriptRuntimeFunction);
10263 const Runtime::Function* function = expr->function();
10264 DCHECK(function != NULL);
10265 switch (function->function_id) {
10266 #define CALL_INTRINSIC_GENERATOR(Name) \
10267 case Runtime::kInline##Name: \
10268 return Generate##Name(expr);
10270 FOR_EACH_HYDROGEN_INTRINSIC(CALL_INTRINSIC_GENERATOR)
10271 #undef CALL_INTRINSIC_GENERATOR
10273 Handle<String> name = expr->name();
10274 int argument_count = expr->arguments()->length();
10275 CHECK_ALIVE(VisitExpressions(expr->arguments()));
10276 PushArgumentsFromEnvironment(argument_count);
10277 HCallRuntime* call = New<HCallRuntime>(name, function, argument_count);
10278 return ast_context()->ReturnInstruction(call, expr->id());
10284 void HOptimizedGraphBuilder::VisitUnaryOperation(UnaryOperation* expr) {
10285 DCHECK(!HasStackOverflow());
10286 DCHECK(current_block() != NULL);
10287 DCHECK(current_block()->HasPredecessor());
10288 switch (expr->op()) {
10289 case Token::DELETE: return VisitDelete(expr);
10290 case Token::VOID: return VisitVoid(expr);
10291 case Token::TYPEOF: return VisitTypeof(expr);
10292 case Token::NOT: return VisitNot(expr);
10293 default: UNREACHABLE();
10298 void HOptimizedGraphBuilder::VisitDelete(UnaryOperation* expr) {
10299 Property* prop = expr->expression()->AsProperty();
10300 VariableProxy* proxy = expr->expression()->AsVariableProxy();
10301 if (prop != NULL) {
10302 CHECK_ALIVE(VisitForValue(prop->obj()));
10303 CHECK_ALIVE(VisitForValue(prop->key()));
10304 HValue* key = Pop();
10305 HValue* obj = Pop();
10306 HValue* function = AddLoadJSBuiltin(Builtins::DELETE);
10307 Add<HPushArguments>(obj, key, Add<HConstant>(function_language_mode()));
10308 // TODO(olivf) InvokeFunction produces a check for the parameter count,
10309 // even though we are certain to pass the correct number of arguments here.
10310 HInstruction* instr = New<HInvokeFunction>(function, 3);
10311 return ast_context()->ReturnInstruction(instr, expr->id());
10312 } else if (proxy != NULL) {
10313 Variable* var = proxy->var();
10314 if (var->IsUnallocatedOrGlobalSlot()) {
10315 Bailout(kDeleteWithGlobalVariable);
10316 } else if (var->IsStackAllocated() || var->IsContextSlot()) {
10317 // Result of deleting non-global variables is false. 'this' is not really
10318 // a variable, though we implement it as one. The subexpression does not
10319 // have side effects.
10320 HValue* value = var->HasThisName(isolate()) ? graph()->GetConstantTrue()
10321 : graph()->GetConstantFalse();
10322 return ast_context()->ReturnValue(value);
10324 Bailout(kDeleteWithNonGlobalVariable);
10327 // Result of deleting non-property, non-variable reference is true.
10328 // Evaluate the subexpression for side effects.
10329 CHECK_ALIVE(VisitForEffect(expr->expression()));
10330 return ast_context()->ReturnValue(graph()->GetConstantTrue());
10335 void HOptimizedGraphBuilder::VisitVoid(UnaryOperation* expr) {
10336 CHECK_ALIVE(VisitForEffect(expr->expression()));
10337 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
10341 void HOptimizedGraphBuilder::VisitTypeof(UnaryOperation* expr) {
10342 CHECK_ALIVE(VisitForTypeOf(expr->expression()));
10343 HValue* value = Pop();
10344 HInstruction* instr = New<HTypeof>(value);
10345 return ast_context()->ReturnInstruction(instr, expr->id());
10349 void HOptimizedGraphBuilder::VisitNot(UnaryOperation* expr) {
10350 if (ast_context()->IsTest()) {
10351 TestContext* context = TestContext::cast(ast_context());
10352 VisitForControl(expr->expression(),
10353 context->if_false(),
10354 context->if_true());
10358 if (ast_context()->IsEffect()) {
10359 VisitForEffect(expr->expression());
10363 DCHECK(ast_context()->IsValue());
10364 HBasicBlock* materialize_false = graph()->CreateBasicBlock();
10365 HBasicBlock* materialize_true = graph()->CreateBasicBlock();
10366 CHECK_BAILOUT(VisitForControl(expr->expression(),
10368 materialize_true));
10370 if (materialize_false->HasPredecessor()) {
10371 materialize_false->SetJoinId(expr->MaterializeFalseId());
10372 set_current_block(materialize_false);
10373 Push(graph()->GetConstantFalse());
10375 materialize_false = NULL;
10378 if (materialize_true->HasPredecessor()) {
10379 materialize_true->SetJoinId(expr->MaterializeTrueId());
10380 set_current_block(materialize_true);
10381 Push(graph()->GetConstantTrue());
10383 materialize_true = NULL;
10386 HBasicBlock* join =
10387 CreateJoin(materialize_false, materialize_true, expr->id());
10388 set_current_block(join);
10389 if (join != NULL) return ast_context()->ReturnValue(Pop());
10393 static Representation RepresentationFor(Type* type) {
10394 DisallowHeapAllocation no_allocation;
10395 if (type->Is(Type::None())) return Representation::None();
10396 if (type->Is(Type::SignedSmall())) return Representation::Smi();
10397 if (type->Is(Type::Signed32())) return Representation::Integer32();
10398 if (type->Is(Type::Number())) return Representation::Double();
10399 return Representation::Tagged();
10403 HInstruction* HOptimizedGraphBuilder::BuildIncrement(
10404 bool returns_original_input,
10405 CountOperation* expr) {
10406 // The input to the count operation is on top of the expression stack.
10407 Representation rep = RepresentationFor(expr->type());
10408 if (rep.IsNone() || rep.IsTagged()) {
10409 rep = Representation::Smi();
10412 if (returns_original_input && !is_strong(function_language_mode())) {
10413 // We need an explicit HValue representing ToNumber(input). The
10414 // actual HChange instruction we need is (sometimes) added in a later
10415 // phase, so it is not available now to be used as an input to HAdd and
10416 // as the return value.
10417 HInstruction* number_input = AddUncasted<HForceRepresentation>(Pop(), rep);
10418 if (!rep.IsDouble()) {
10419 number_input->SetFlag(HInstruction::kFlexibleRepresentation);
10420 number_input->SetFlag(HInstruction::kCannotBeTagged);
10422 Push(number_input);
10425 // The addition has no side effects, so we do not need
10426 // to simulate the expression stack after this instruction.
10427 // Any later failures deopt to the load of the input or earlier.
10428 HConstant* delta = (expr->op() == Token::INC)
10429 ? graph()->GetConstant1()
10430 : graph()->GetConstantMinus1();
10431 HInstruction* instr =
10432 AddUncasted<HAdd>(Top(), delta, strength(function_language_mode()));
10433 if (instr->IsAdd()) {
10434 HAdd* add = HAdd::cast(instr);
10435 add->set_observed_input_representation(1, rep);
10436 add->set_observed_input_representation(2, Representation::Smi());
10438 if (!is_strong(function_language_mode())) {
10439 instr->ClearAllSideEffects();
10441 Add<HSimulate>(expr->ToNumberId(), REMOVABLE_SIMULATE);
10443 instr->SetFlag(HInstruction::kCannotBeTagged);
10448 void HOptimizedGraphBuilder::BuildStoreForEffect(Expression* expr,
10451 BailoutId return_id,
10455 EffectContext for_effect(this);
10457 if (key != NULL) Push(key);
10459 BuildStore(expr, prop, ast_id, return_id);
10463 void HOptimizedGraphBuilder::VisitCountOperation(CountOperation* expr) {
10464 DCHECK(!HasStackOverflow());
10465 DCHECK(current_block() != NULL);
10466 DCHECK(current_block()->HasPredecessor());
10467 if (!top_info()->is_tracking_positions()) SetSourcePosition(expr->position());
10468 Expression* target = expr->expression();
10469 VariableProxy* proxy = target->AsVariableProxy();
10470 Property* prop = target->AsProperty();
10471 if (proxy == NULL && prop == NULL) {
10472 return Bailout(kInvalidLhsInCountOperation);
10475 // Match the full code generator stack by simulating an extra stack
10476 // element for postfix operations in a non-effect context. The return
10477 // value is ToNumber(input).
10478 bool returns_original_input =
10479 expr->is_postfix() && !ast_context()->IsEffect();
10480 HValue* input = NULL; // ToNumber(original_input).
10481 HValue* after = NULL; // The result after incrementing or decrementing.
10483 if (proxy != NULL) {
10484 Variable* var = proxy->var();
10485 if (var->mode() == CONST_LEGACY) {
10486 return Bailout(kUnsupportedCountOperationWithConst);
10488 if (var->mode() == CONST) {
10489 return Bailout(kNonInitializerAssignmentToConst);
10491 // Argument of the count operation is a variable, not a property.
10492 DCHECK(prop == NULL);
10493 CHECK_ALIVE(VisitForValue(target));
10495 after = BuildIncrement(returns_original_input, expr);
10496 input = returns_original_input ? Top() : Pop();
10499 switch (var->location()) {
10500 case VariableLocation::GLOBAL:
10501 case VariableLocation::UNALLOCATED:
10502 HandleGlobalVariableAssignment(var,
10504 expr->AssignmentId());
10507 case VariableLocation::PARAMETER:
10508 case VariableLocation::LOCAL:
10509 BindIfLive(var, after);
10512 case VariableLocation::CONTEXT: {
10513 // Bail out if we try to mutate a parameter value in a function
10514 // using the arguments object. We do not (yet) correctly handle the
10515 // arguments property of the function.
10516 if (current_info()->scope()->arguments() != NULL) {
10517 // Parameters will rewrite to context slots. We have no direct
10518 // way to detect that the variable is a parameter so we use a
10519 // linear search of the parameter list.
10520 int count = current_info()->scope()->num_parameters();
10521 for (int i = 0; i < count; ++i) {
10522 if (var == current_info()->scope()->parameter(i)) {
10523 return Bailout(kAssignmentToParameterInArgumentsObject);
10528 HValue* context = BuildContextChainWalk(var);
10529 HStoreContextSlot::Mode mode = IsLexicalVariableMode(var->mode())
10530 ? HStoreContextSlot::kCheckDeoptimize : HStoreContextSlot::kNoCheck;
10531 HStoreContextSlot* instr = Add<HStoreContextSlot>(context, var->index(),
10533 if (instr->HasObservableSideEffects()) {
10534 Add<HSimulate>(expr->AssignmentId(), REMOVABLE_SIMULATE);
10539 case VariableLocation::LOOKUP:
10540 return Bailout(kLookupVariableInCountOperation);
10543 Drop(returns_original_input ? 2 : 1);
10544 return ast_context()->ReturnValue(expr->is_postfix() ? input : after);
10547 // Argument of the count operation is a property.
10548 DCHECK(prop != NULL);
10549 if (returns_original_input) Push(graph()->GetConstantUndefined());
10551 CHECK_ALIVE(VisitForValue(prop->obj()));
10552 HValue* object = Top();
10554 HValue* key = NULL;
10555 if (!prop->key()->IsPropertyName() || prop->IsStringAccess()) {
10556 CHECK_ALIVE(VisitForValue(prop->key()));
10560 CHECK_ALIVE(PushLoad(prop, object, key));
10562 after = BuildIncrement(returns_original_input, expr);
10564 if (returns_original_input) {
10566 // Drop object and key to push it again in the effect context below.
10567 Drop(key == NULL ? 1 : 2);
10568 environment()->SetExpressionStackAt(0, input);
10569 CHECK_ALIVE(BuildStoreForEffect(
10570 expr, prop, expr->id(), expr->AssignmentId(), object, key, after));
10571 return ast_context()->ReturnValue(Pop());
10574 environment()->SetExpressionStackAt(0, after);
10575 return BuildStore(expr, prop, expr->id(), expr->AssignmentId());
10579 HInstruction* HOptimizedGraphBuilder::BuildStringCharCodeAt(
10582 if (string->IsConstant() && index->IsConstant()) {
10583 HConstant* c_string = HConstant::cast(string);
10584 HConstant* c_index = HConstant::cast(index);
10585 if (c_string->HasStringValue() && c_index->HasNumberValue()) {
10586 int32_t i = c_index->NumberValueAsInteger32();
10587 Handle<String> s = c_string->StringValue();
10588 if (i < 0 || i >= s->length()) {
10589 return New<HConstant>(std::numeric_limits<double>::quiet_NaN());
10591 return New<HConstant>(s->Get(i));
10594 string = BuildCheckString(string);
10595 index = Add<HBoundsCheck>(index, AddLoadStringLength(string));
10596 return New<HStringCharCodeAt>(string, index);
10600 // Checks if the given shift amounts have following forms:
10601 // (N1) and (N2) with N1 + N2 = 32; (sa) and (32 - sa).
10602 static bool ShiftAmountsAllowReplaceByRotate(HValue* sa,
10603 HValue* const32_minus_sa) {
10604 if (sa->IsConstant() && const32_minus_sa->IsConstant()) {
10605 const HConstant* c1 = HConstant::cast(sa);
10606 const HConstant* c2 = HConstant::cast(const32_minus_sa);
10607 return c1->HasInteger32Value() && c2->HasInteger32Value() &&
10608 (c1->Integer32Value() + c2->Integer32Value() == 32);
10610 if (!const32_minus_sa->IsSub()) return false;
10611 HSub* sub = HSub::cast(const32_minus_sa);
10612 return sub->left()->EqualsInteger32Constant(32) && sub->right() == sa;
10616 // Checks if the left and the right are shift instructions with the oposite
10617 // directions that can be replaced by one rotate right instruction or not.
10618 // Returns the operand and the shift amount for the rotate instruction in the
10620 bool HGraphBuilder::MatchRotateRight(HValue* left,
10623 HValue** shift_amount) {
10626 if (left->IsShl() && right->IsShr()) {
10627 shl = HShl::cast(left);
10628 shr = HShr::cast(right);
10629 } else if (left->IsShr() && right->IsShl()) {
10630 shl = HShl::cast(right);
10631 shr = HShr::cast(left);
10635 if (shl->left() != shr->left()) return false;
10637 if (!ShiftAmountsAllowReplaceByRotate(shl->right(), shr->right()) &&
10638 !ShiftAmountsAllowReplaceByRotate(shr->right(), shl->right())) {
10641 *operand = shr->left();
10642 *shift_amount = shr->right();
10647 bool CanBeZero(HValue* right) {
10648 if (right->IsConstant()) {
10649 HConstant* right_const = HConstant::cast(right);
10650 if (right_const->HasInteger32Value() &&
10651 (right_const->Integer32Value() & 0x1f) != 0) {
10659 HValue* HGraphBuilder::EnforceNumberType(HValue* number,
10661 if (expected->Is(Type::SignedSmall())) {
10662 return AddUncasted<HForceRepresentation>(number, Representation::Smi());
10664 if (expected->Is(Type::Signed32())) {
10665 return AddUncasted<HForceRepresentation>(number,
10666 Representation::Integer32());
10672 HValue* HGraphBuilder::TruncateToNumber(HValue* value, Type** expected) {
10673 if (value->IsConstant()) {
10674 HConstant* constant = HConstant::cast(value);
10675 Maybe<HConstant*> number =
10676 constant->CopyToTruncatedNumber(isolate(), zone());
10677 if (number.IsJust()) {
10678 *expected = Type::Number(zone());
10679 return AddInstruction(number.FromJust());
10683 // We put temporary values on the stack, which don't correspond to anything
10684 // in baseline code. Since nothing is observable we avoid recording those
10685 // pushes with a NoObservableSideEffectsScope.
10686 NoObservableSideEffectsScope no_effects(this);
10688 Type* expected_type = *expected;
10690 // Separate the number type from the rest.
10691 Type* expected_obj =
10692 Type::Intersect(expected_type, Type::NonNumber(zone()), zone());
10693 Type* expected_number =
10694 Type::Intersect(expected_type, Type::Number(zone()), zone());
10696 // We expect to get a number.
10697 // (We need to check first, since Type::None->Is(Type::Any()) == true.
10698 if (expected_obj->Is(Type::None())) {
10699 DCHECK(!expected_number->Is(Type::None(zone())));
10703 if (expected_obj->Is(Type::Undefined(zone()))) {
10704 // This is already done by HChange.
10705 *expected = Type::Union(expected_number, Type::Number(zone()), zone());
10713 HValue* HOptimizedGraphBuilder::BuildBinaryOperation(
10714 BinaryOperation* expr,
10717 PushBeforeSimulateBehavior push_sim_result) {
10718 Type* left_type = expr->left()->bounds().lower;
10719 Type* right_type = expr->right()->bounds().lower;
10720 Type* result_type = expr->bounds().lower;
10721 Maybe<int> fixed_right_arg = expr->fixed_right_arg();
10722 Handle<AllocationSite> allocation_site = expr->allocation_site();
10724 HAllocationMode allocation_mode;
10725 if (FLAG_allocation_site_pretenuring && !allocation_site.is_null()) {
10726 allocation_mode = HAllocationMode(allocation_site);
10728 HValue* result = HGraphBuilder::BuildBinaryOperation(
10729 expr->op(), left, right, left_type, right_type, result_type,
10730 fixed_right_arg, allocation_mode, strength(function_language_mode()),
10732 // Add a simulate after instructions with observable side effects, and
10733 // after phis, which are the result of BuildBinaryOperation when we
10734 // inlined some complex subgraph.
10735 if (result->HasObservableSideEffects() || result->IsPhi()) {
10736 if (push_sim_result == PUSH_BEFORE_SIMULATE) {
10738 Add<HSimulate>(expr->id(), REMOVABLE_SIMULATE);
10741 Add<HSimulate>(expr->id(), REMOVABLE_SIMULATE);
10748 HValue* HGraphBuilder::BuildBinaryOperation(
10749 Token::Value op, HValue* left, HValue* right, Type* left_type,
10750 Type* right_type, Type* result_type, Maybe<int> fixed_right_arg,
10751 HAllocationMode allocation_mode, Strength strength, BailoutId opt_id) {
10752 bool maybe_string_add = false;
10753 if (op == Token::ADD) {
10754 // If we are adding constant string with something for which we don't have
10755 // a feedback yet, assume that it's also going to be a string and don't
10756 // generate deopt instructions.
10757 if (!left_type->IsInhabited() && right->IsConstant() &&
10758 HConstant::cast(right)->HasStringValue()) {
10759 left_type = Type::String();
10762 if (!right_type->IsInhabited() && left->IsConstant() &&
10763 HConstant::cast(left)->HasStringValue()) {
10764 right_type = Type::String();
10767 maybe_string_add = (left_type->Maybe(Type::String()) ||
10768 left_type->Maybe(Type::Receiver()) ||
10769 right_type->Maybe(Type::String()) ||
10770 right_type->Maybe(Type::Receiver()));
10773 Representation left_rep = RepresentationFor(left_type);
10774 Representation right_rep = RepresentationFor(right_type);
10776 if (!left_type->IsInhabited()) {
10778 Deoptimizer::kInsufficientTypeFeedbackForLHSOfBinaryOperation,
10779 Deoptimizer::SOFT);
10780 left_type = Type::Any(zone());
10781 left_rep = RepresentationFor(left_type);
10782 maybe_string_add = op == Token::ADD;
10785 if (!right_type->IsInhabited()) {
10787 Deoptimizer::kInsufficientTypeFeedbackForRHSOfBinaryOperation,
10788 Deoptimizer::SOFT);
10789 right_type = Type::Any(zone());
10790 right_rep = RepresentationFor(right_type);
10791 maybe_string_add = op == Token::ADD;
10794 if (!maybe_string_add && !is_strong(strength)) {
10795 left = TruncateToNumber(left, &left_type);
10796 right = TruncateToNumber(right, &right_type);
10799 // Special case for string addition here.
10800 if (op == Token::ADD &&
10801 (left_type->Is(Type::String()) || right_type->Is(Type::String()))) {
10802 // Validate type feedback for left argument.
10803 if (left_type->Is(Type::String())) {
10804 left = BuildCheckString(left);
10807 // Validate type feedback for right argument.
10808 if (right_type->Is(Type::String())) {
10809 right = BuildCheckString(right);
10812 // Convert left argument as necessary.
10813 if (left_type->Is(Type::Number()) && !is_strong(strength)) {
10814 DCHECK(right_type->Is(Type::String()));
10815 left = BuildNumberToString(left, left_type);
10816 } else if (!left_type->Is(Type::String())) {
10817 DCHECK(right_type->Is(Type::String()));
10818 HValue* function = AddLoadJSBuiltin(
10819 is_strong(strength) ? Builtins::STRING_ADD_RIGHT_STRONG
10820 : Builtins::STRING_ADD_RIGHT);
10821 Add<HPushArguments>(left, right);
10822 return AddUncasted<HInvokeFunction>(function, 2);
10825 // Convert right argument as necessary.
10826 if (right_type->Is(Type::Number()) && !is_strong(strength)) {
10827 DCHECK(left_type->Is(Type::String()));
10828 right = BuildNumberToString(right, right_type);
10829 } else if (!right_type->Is(Type::String())) {
10830 DCHECK(left_type->Is(Type::String()));
10831 HValue* function = AddLoadJSBuiltin(is_strong(strength)
10832 ? Builtins::STRING_ADD_LEFT_STRONG
10833 : Builtins::STRING_ADD_LEFT);
10834 Add<HPushArguments>(left, right);
10835 return AddUncasted<HInvokeFunction>(function, 2);
10838 // Fast paths for empty constant strings.
10839 Handle<String> left_string =
10840 left->IsConstant() && HConstant::cast(left)->HasStringValue()
10841 ? HConstant::cast(left)->StringValue()
10842 : Handle<String>();
10843 Handle<String> right_string =
10844 right->IsConstant() && HConstant::cast(right)->HasStringValue()
10845 ? HConstant::cast(right)->StringValue()
10846 : Handle<String>();
10847 if (!left_string.is_null() && left_string->length() == 0) return right;
10848 if (!right_string.is_null() && right_string->length() == 0) return left;
10849 if (!left_string.is_null() && !right_string.is_null()) {
10850 return AddUncasted<HStringAdd>(
10851 left, right, strength, allocation_mode.GetPretenureMode(),
10852 STRING_ADD_CHECK_NONE, allocation_mode.feedback_site());
10855 // Register the dependent code with the allocation site.
10856 if (!allocation_mode.feedback_site().is_null()) {
10857 DCHECK(!graph()->info()->IsStub());
10858 Handle<AllocationSite> site(allocation_mode.feedback_site());
10859 top_info()->dependencies()->AssumeTenuringDecision(site);
10862 // Inline the string addition into the stub when creating allocation
10863 // mementos to gather allocation site feedback, or if we can statically
10864 // infer that we're going to create a cons string.
10865 if ((graph()->info()->IsStub() &&
10866 allocation_mode.CreateAllocationMementos()) ||
10867 (left->IsConstant() &&
10868 HConstant::cast(left)->HasStringValue() &&
10869 HConstant::cast(left)->StringValue()->length() + 1 >=
10870 ConsString::kMinLength) ||
10871 (right->IsConstant() &&
10872 HConstant::cast(right)->HasStringValue() &&
10873 HConstant::cast(right)->StringValue()->length() + 1 >=
10874 ConsString::kMinLength)) {
10875 return BuildStringAdd(left, right, allocation_mode);
10878 // Fallback to using the string add stub.
10879 return AddUncasted<HStringAdd>(
10880 left, right, strength, allocation_mode.GetPretenureMode(),
10881 STRING_ADD_CHECK_NONE, allocation_mode.feedback_site());
10884 if (graph()->info()->IsStub()) {
10885 left = EnforceNumberType(left, left_type);
10886 right = EnforceNumberType(right, right_type);
10889 Representation result_rep = RepresentationFor(result_type);
10891 bool is_non_primitive = (left_rep.IsTagged() && !left_rep.IsSmi()) ||
10892 (right_rep.IsTagged() && !right_rep.IsSmi());
10894 HInstruction* instr = NULL;
10895 // Only the stub is allowed to call into the runtime, since otherwise we would
10896 // inline several instructions (including the two pushes) for every tagged
10897 // operation in optimized code, which is more expensive, than a stub call.
10898 if (graph()->info()->IsStub() && is_non_primitive) {
10900 AddLoadJSBuiltin(BinaryOpIC::TokenToJSBuiltin(op, strength));
10901 Add<HPushArguments>(left, right);
10902 instr = AddUncasted<HInvokeFunction>(function, 2);
10904 if (is_strong(strength) && Token::IsBitOp(op)) {
10905 // TODO(conradw): This is not efficient, but is necessary to prevent
10906 // conversion of oddball values to numbers in strong mode. It would be
10907 // better to prevent the conversion rather than adding a runtime check.
10908 IfBuilder if_builder(this);
10909 if_builder.If<HHasInstanceTypeAndBranch>(left, ODDBALL_TYPE);
10910 if_builder.OrIf<HHasInstanceTypeAndBranch>(right, ODDBALL_TYPE);
10913 isolate()->factory()->empty_string(),
10914 Runtime::FunctionForId(Runtime::kThrowStrongModeImplicitConversion),
10916 if (!graph()->info()->IsStub()) {
10917 Add<HSimulate>(opt_id, REMOVABLE_SIMULATE);
10923 instr = AddUncasted<HAdd>(left, right, strength);
10926 instr = AddUncasted<HSub>(left, right, strength);
10929 instr = AddUncasted<HMul>(left, right, strength);
10932 if (fixed_right_arg.IsJust() &&
10933 !right->EqualsInteger32Constant(fixed_right_arg.FromJust())) {
10934 HConstant* fixed_right =
10935 Add<HConstant>(static_cast<int>(fixed_right_arg.FromJust()));
10936 IfBuilder if_same(this);
10937 if_same.If<HCompareNumericAndBranch>(right, fixed_right, Token::EQ);
10939 if_same.ElseDeopt(Deoptimizer::kUnexpectedRHSOfBinaryOperation);
10940 right = fixed_right;
10942 instr = AddUncasted<HMod>(left, right, strength);
10946 instr = AddUncasted<HDiv>(left, right, strength);
10948 case Token::BIT_XOR:
10949 case Token::BIT_AND:
10950 instr = AddUncasted<HBitwise>(op, left, right, strength);
10952 case Token::BIT_OR: {
10953 HValue* operand, *shift_amount;
10954 if (left_type->Is(Type::Signed32()) &&
10955 right_type->Is(Type::Signed32()) &&
10956 MatchRotateRight(left, right, &operand, &shift_amount)) {
10957 instr = AddUncasted<HRor>(operand, shift_amount, strength);
10959 instr = AddUncasted<HBitwise>(op, left, right, strength);
10964 instr = AddUncasted<HSar>(left, right, strength);
10967 instr = AddUncasted<HShr>(left, right, strength);
10968 if (instr->IsShr() && CanBeZero(right)) {
10969 graph()->RecordUint32Instruction(instr);
10973 instr = AddUncasted<HShl>(left, right, strength);
10980 if (instr->IsBinaryOperation()) {
10981 HBinaryOperation* binop = HBinaryOperation::cast(instr);
10982 binop->set_observed_input_representation(1, left_rep);
10983 binop->set_observed_input_representation(2, right_rep);
10984 binop->initialize_output_representation(result_rep);
10985 if (graph()->info()->IsStub()) {
10986 // Stub should not call into stub.
10987 instr->SetFlag(HValue::kCannotBeTagged);
10988 // And should truncate on HForceRepresentation already.
10989 if (left->IsForceRepresentation()) {
10990 left->CopyFlag(HValue::kTruncatingToSmi, instr);
10991 left->CopyFlag(HValue::kTruncatingToInt32, instr);
10993 if (right->IsForceRepresentation()) {
10994 right->CopyFlag(HValue::kTruncatingToSmi, instr);
10995 right->CopyFlag(HValue::kTruncatingToInt32, instr);
11003 // Check for the form (%_ClassOf(foo) === 'BarClass').
11004 static bool IsClassOfTest(CompareOperation* expr) {
11005 if (expr->op() != Token::EQ_STRICT) return false;
11006 CallRuntime* call = expr->left()->AsCallRuntime();
11007 if (call == NULL) return false;
11008 Literal* literal = expr->right()->AsLiteral();
11009 if (literal == NULL) return false;
11010 if (!literal->value()->IsString()) return false;
11011 if (!call->name()->IsOneByteEqualTo(STATIC_CHAR_VECTOR("_ClassOf"))) {
11014 DCHECK(call->arguments()->length() == 1);
11019 void HOptimizedGraphBuilder::VisitBinaryOperation(BinaryOperation* expr) {
11020 DCHECK(!HasStackOverflow());
11021 DCHECK(current_block() != NULL);
11022 DCHECK(current_block()->HasPredecessor());
11023 switch (expr->op()) {
11025 return VisitComma(expr);
11028 return VisitLogicalExpression(expr);
11030 return VisitArithmeticExpression(expr);
11035 void HOptimizedGraphBuilder::VisitComma(BinaryOperation* expr) {
11036 CHECK_ALIVE(VisitForEffect(expr->left()));
11037 // Visit the right subexpression in the same AST context as the entire
11039 Visit(expr->right());
11043 void HOptimizedGraphBuilder::VisitLogicalExpression(BinaryOperation* expr) {
11044 bool is_logical_and = expr->op() == Token::AND;
11045 if (ast_context()->IsTest()) {
11046 TestContext* context = TestContext::cast(ast_context());
11047 // Translate left subexpression.
11048 HBasicBlock* eval_right = graph()->CreateBasicBlock();
11049 if (is_logical_and) {
11050 CHECK_BAILOUT(VisitForControl(expr->left(),
11052 context->if_false()));
11054 CHECK_BAILOUT(VisitForControl(expr->left(),
11055 context->if_true(),
11059 // Translate right subexpression by visiting it in the same AST
11060 // context as the entire expression.
11061 if (eval_right->HasPredecessor()) {
11062 eval_right->SetJoinId(expr->RightId());
11063 set_current_block(eval_right);
11064 Visit(expr->right());
11067 } else if (ast_context()->IsValue()) {
11068 CHECK_ALIVE(VisitForValue(expr->left()));
11069 DCHECK(current_block() != NULL);
11070 HValue* left_value = Top();
11072 // Short-circuit left values that always evaluate to the same boolean value.
11073 if (expr->left()->ToBooleanIsTrue() || expr->left()->ToBooleanIsFalse()) {
11074 // l (evals true) && r -> r
11075 // l (evals true) || r -> l
11076 // l (evals false) && r -> l
11077 // l (evals false) || r -> r
11078 if (is_logical_and == expr->left()->ToBooleanIsTrue()) {
11080 CHECK_ALIVE(VisitForValue(expr->right()));
11082 return ast_context()->ReturnValue(Pop());
11085 // We need an extra block to maintain edge-split form.
11086 HBasicBlock* empty_block = graph()->CreateBasicBlock();
11087 HBasicBlock* eval_right = graph()->CreateBasicBlock();
11088 ToBooleanStub::Types expected(expr->left()->to_boolean_types());
11089 HBranch* test = is_logical_and
11090 ? New<HBranch>(left_value, expected, eval_right, empty_block)
11091 : New<HBranch>(left_value, expected, empty_block, eval_right);
11092 FinishCurrentBlock(test);
11094 set_current_block(eval_right);
11095 Drop(1); // Value of the left subexpression.
11096 CHECK_BAILOUT(VisitForValue(expr->right()));
11098 HBasicBlock* join_block =
11099 CreateJoin(empty_block, current_block(), expr->id());
11100 set_current_block(join_block);
11101 return ast_context()->ReturnValue(Pop());
11104 DCHECK(ast_context()->IsEffect());
11105 // In an effect context, we don't need the value of the left subexpression,
11106 // only its control flow and side effects. We need an extra block to
11107 // maintain edge-split form.
11108 HBasicBlock* empty_block = graph()->CreateBasicBlock();
11109 HBasicBlock* right_block = graph()->CreateBasicBlock();
11110 if (is_logical_and) {
11111 CHECK_BAILOUT(VisitForControl(expr->left(), right_block, empty_block));
11113 CHECK_BAILOUT(VisitForControl(expr->left(), empty_block, right_block));
11116 // TODO(kmillikin): Find a way to fix this. It's ugly that there are
11117 // actually two empty blocks (one here and one inserted by
11118 // TestContext::BuildBranch, and that they both have an HSimulate though the
11119 // second one is not a merge node, and that we really have no good AST ID to
11120 // put on that first HSimulate.
11122 if (empty_block->HasPredecessor()) {
11123 empty_block->SetJoinId(expr->id());
11125 empty_block = NULL;
11128 if (right_block->HasPredecessor()) {
11129 right_block->SetJoinId(expr->RightId());
11130 set_current_block(right_block);
11131 CHECK_BAILOUT(VisitForEffect(expr->right()));
11132 right_block = current_block();
11134 right_block = NULL;
11137 HBasicBlock* join_block =
11138 CreateJoin(empty_block, right_block, expr->id());
11139 set_current_block(join_block);
11140 // We did not materialize any value in the predecessor environments,
11141 // so there is no need to handle it here.
11146 void HOptimizedGraphBuilder::VisitArithmeticExpression(BinaryOperation* expr) {
11147 CHECK_ALIVE(VisitForValue(expr->left()));
11148 CHECK_ALIVE(VisitForValue(expr->right()));
11149 SetSourcePosition(expr->position());
11150 HValue* right = Pop();
11151 HValue* left = Pop();
11153 BuildBinaryOperation(expr, left, right,
11154 ast_context()->IsEffect() ? NO_PUSH_BEFORE_SIMULATE
11155 : PUSH_BEFORE_SIMULATE);
11156 if (top_info()->is_tracking_positions() && result->IsBinaryOperation()) {
11157 HBinaryOperation::cast(result)->SetOperandPositions(
11159 ScriptPositionToSourcePosition(expr->left()->position()),
11160 ScriptPositionToSourcePosition(expr->right()->position()));
11162 return ast_context()->ReturnValue(result);
11166 void HOptimizedGraphBuilder::HandleLiteralCompareTypeof(CompareOperation* expr,
11167 Expression* sub_expr,
11168 Handle<String> check) {
11169 CHECK_ALIVE(VisitForTypeOf(sub_expr));
11170 SetSourcePosition(expr->position());
11171 HValue* value = Pop();
11172 HTypeofIsAndBranch* instr = New<HTypeofIsAndBranch>(value, check);
11173 return ast_context()->ReturnControl(instr, expr->id());
11177 static bool IsLiteralCompareBool(Isolate* isolate,
11181 return op == Token::EQ_STRICT &&
11182 ((left->IsConstant() &&
11183 HConstant::cast(left)->handle(isolate)->IsBoolean()) ||
11184 (right->IsConstant() &&
11185 HConstant::cast(right)->handle(isolate)->IsBoolean()));
11189 void HOptimizedGraphBuilder::VisitCompareOperation(CompareOperation* expr) {
11190 DCHECK(!HasStackOverflow());
11191 DCHECK(current_block() != NULL);
11192 DCHECK(current_block()->HasPredecessor());
11194 if (!top_info()->is_tracking_positions()) SetSourcePosition(expr->position());
11196 // Check for a few fast cases. The AST visiting behavior must be in sync
11197 // with the full codegen: We don't push both left and right values onto
11198 // the expression stack when one side is a special-case literal.
11199 Expression* sub_expr = NULL;
11200 Handle<String> check;
11201 if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) {
11202 return HandleLiteralCompareTypeof(expr, sub_expr, check);
11204 if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) {
11205 return HandleLiteralCompareNil(expr, sub_expr, kUndefinedValue);
11207 if (expr->IsLiteralCompareNull(&sub_expr)) {
11208 return HandleLiteralCompareNil(expr, sub_expr, kNullValue);
11211 if (IsClassOfTest(expr)) {
11212 CallRuntime* call = expr->left()->AsCallRuntime();
11213 DCHECK(call->arguments()->length() == 1);
11214 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11215 HValue* value = Pop();
11216 Literal* literal = expr->right()->AsLiteral();
11217 Handle<String> rhs = Handle<String>::cast(literal->value());
11218 HClassOfTestAndBranch* instr = New<HClassOfTestAndBranch>(value, rhs);
11219 return ast_context()->ReturnControl(instr, expr->id());
11222 Type* left_type = expr->left()->bounds().lower;
11223 Type* right_type = expr->right()->bounds().lower;
11224 Type* combined_type = expr->combined_type();
11226 CHECK_ALIVE(VisitForValue(expr->left()));
11227 CHECK_ALIVE(VisitForValue(expr->right()));
11229 HValue* right = Pop();
11230 HValue* left = Pop();
11231 Token::Value op = expr->op();
11233 if (IsLiteralCompareBool(isolate(), left, op, right)) {
11234 HCompareObjectEqAndBranch* result =
11235 New<HCompareObjectEqAndBranch>(left, right);
11236 return ast_context()->ReturnControl(result, expr->id());
11239 if (op == Token::INSTANCEOF) {
11240 // Check to see if the rhs of the instanceof is a known function.
11241 if (right->IsConstant() &&
11242 HConstant::cast(right)->handle(isolate())->IsJSFunction()) {
11243 Handle<Object> function = HConstant::cast(right)->handle(isolate());
11244 Handle<JSFunction> target = Handle<JSFunction>::cast(function);
11245 HInstanceOfKnownGlobal* result =
11246 New<HInstanceOfKnownGlobal>(left, target);
11247 return ast_context()->ReturnInstruction(result, expr->id());
11250 HInstanceOf* result = New<HInstanceOf>(left, right);
11251 return ast_context()->ReturnInstruction(result, expr->id());
11253 } else if (op == Token::IN) {
11254 HValue* function = AddLoadJSBuiltin(Builtins::IN);
11255 Add<HPushArguments>(left, right);
11256 // TODO(olivf) InvokeFunction produces a check for the parameter count,
11257 // even though we are certain to pass the correct number of arguments here.
11258 HInstruction* result = New<HInvokeFunction>(function, 2);
11259 return ast_context()->ReturnInstruction(result, expr->id());
11262 PushBeforeSimulateBehavior push_behavior =
11263 ast_context()->IsEffect() ? NO_PUSH_BEFORE_SIMULATE
11264 : PUSH_BEFORE_SIMULATE;
11265 HControlInstruction* compare = BuildCompareInstruction(
11266 op, left, right, left_type, right_type, combined_type,
11267 ScriptPositionToSourcePosition(expr->left()->position()),
11268 ScriptPositionToSourcePosition(expr->right()->position()),
11269 push_behavior, expr->id());
11270 if (compare == NULL) return; // Bailed out.
11271 return ast_context()->ReturnControl(compare, expr->id());
11275 HControlInstruction* HOptimizedGraphBuilder::BuildCompareInstruction(
11276 Token::Value op, HValue* left, HValue* right, Type* left_type,
11277 Type* right_type, Type* combined_type, SourcePosition left_position,
11278 SourcePosition right_position, PushBeforeSimulateBehavior push_sim_result,
11279 BailoutId bailout_id) {
11280 // Cases handled below depend on collected type feedback. They should
11281 // soft deoptimize when there is no type feedback.
11282 if (!combined_type->IsInhabited()) {
11284 Deoptimizer::kInsufficientTypeFeedbackForCombinedTypeOfBinaryOperation,
11285 Deoptimizer::SOFT);
11286 combined_type = left_type = right_type = Type::Any(zone());
11289 Representation left_rep = RepresentationFor(left_type);
11290 Representation right_rep = RepresentationFor(right_type);
11291 Representation combined_rep = RepresentationFor(combined_type);
11293 if (combined_type->Is(Type::Receiver())) {
11294 if (Token::IsEqualityOp(op)) {
11295 // HCompareObjectEqAndBranch can only deal with object, so
11296 // exclude numbers.
11297 if ((left->IsConstant() &&
11298 HConstant::cast(left)->HasNumberValue()) ||
11299 (right->IsConstant() &&
11300 HConstant::cast(right)->HasNumberValue())) {
11301 Add<HDeoptimize>(Deoptimizer::kTypeMismatchBetweenFeedbackAndConstant,
11302 Deoptimizer::SOFT);
11303 // The caller expects a branch instruction, so make it happy.
11304 return New<HBranch>(graph()->GetConstantTrue());
11306 // Can we get away with map check and not instance type check?
11307 HValue* operand_to_check =
11308 left->block()->block_id() < right->block()->block_id() ? left : right;
11309 if (combined_type->IsClass()) {
11310 Handle<Map> map = combined_type->AsClass()->Map();
11311 AddCheckMap(operand_to_check, map);
11312 HCompareObjectEqAndBranch* result =
11313 New<HCompareObjectEqAndBranch>(left, right);
11314 if (top_info()->is_tracking_positions()) {
11315 result->set_operand_position(zone(), 0, left_position);
11316 result->set_operand_position(zone(), 1, right_position);
11320 BuildCheckHeapObject(operand_to_check);
11321 Add<HCheckInstanceType>(operand_to_check,
11322 HCheckInstanceType::IS_SPEC_OBJECT);
11323 HCompareObjectEqAndBranch* result =
11324 New<HCompareObjectEqAndBranch>(left, right);
11328 Bailout(kUnsupportedNonPrimitiveCompare);
11331 } else if (combined_type->Is(Type::InternalizedString()) &&
11332 Token::IsEqualityOp(op)) {
11333 // If we have a constant argument, it should be consistent with the type
11334 // feedback (otherwise we fail assertions in HCompareObjectEqAndBranch).
11335 if ((left->IsConstant() &&
11336 !HConstant::cast(left)->HasInternalizedStringValue()) ||
11337 (right->IsConstant() &&
11338 !HConstant::cast(right)->HasInternalizedStringValue())) {
11339 Add<HDeoptimize>(Deoptimizer::kTypeMismatchBetweenFeedbackAndConstant,
11340 Deoptimizer::SOFT);
11341 // The caller expects a branch instruction, so make it happy.
11342 return New<HBranch>(graph()->GetConstantTrue());
11344 BuildCheckHeapObject(left);
11345 Add<HCheckInstanceType>(left, HCheckInstanceType::IS_INTERNALIZED_STRING);
11346 BuildCheckHeapObject(right);
11347 Add<HCheckInstanceType>(right, HCheckInstanceType::IS_INTERNALIZED_STRING);
11348 HCompareObjectEqAndBranch* result =
11349 New<HCompareObjectEqAndBranch>(left, right);
11351 } else if (combined_type->Is(Type::String())) {
11352 BuildCheckHeapObject(left);
11353 Add<HCheckInstanceType>(left, HCheckInstanceType::IS_STRING);
11354 BuildCheckHeapObject(right);
11355 Add<HCheckInstanceType>(right, HCheckInstanceType::IS_STRING);
11356 HStringCompareAndBranch* result =
11357 New<HStringCompareAndBranch>(left, right, op);
11360 if (combined_rep.IsTagged() || combined_rep.IsNone()) {
11361 HCompareGeneric* result = Add<HCompareGeneric>(
11362 left, right, op, strength(function_language_mode()));
11363 result->set_observed_input_representation(1, left_rep);
11364 result->set_observed_input_representation(2, right_rep);
11365 if (result->HasObservableSideEffects()) {
11366 if (push_sim_result == PUSH_BEFORE_SIMULATE) {
11368 AddSimulate(bailout_id, REMOVABLE_SIMULATE);
11371 AddSimulate(bailout_id, REMOVABLE_SIMULATE);
11374 // TODO(jkummerow): Can we make this more efficient?
11375 HBranch* branch = New<HBranch>(result);
11378 HCompareNumericAndBranch* result = New<HCompareNumericAndBranch>(
11379 left, right, op, strength(function_language_mode()));
11380 result->set_observed_input_representation(left_rep, right_rep);
11381 if (top_info()->is_tracking_positions()) {
11382 result->SetOperandPositions(zone(), left_position, right_position);
11390 void HOptimizedGraphBuilder::HandleLiteralCompareNil(CompareOperation* expr,
11391 Expression* sub_expr,
11393 DCHECK(!HasStackOverflow());
11394 DCHECK(current_block() != NULL);
11395 DCHECK(current_block()->HasPredecessor());
11396 DCHECK(expr->op() == Token::EQ || expr->op() == Token::EQ_STRICT);
11397 if (!top_info()->is_tracking_positions()) SetSourcePosition(expr->position());
11398 CHECK_ALIVE(VisitForValue(sub_expr));
11399 HValue* value = Pop();
11400 if (expr->op() == Token::EQ_STRICT) {
11401 HConstant* nil_constant = nil == kNullValue
11402 ? graph()->GetConstantNull()
11403 : graph()->GetConstantUndefined();
11404 HCompareObjectEqAndBranch* instr =
11405 New<HCompareObjectEqAndBranch>(value, nil_constant);
11406 return ast_context()->ReturnControl(instr, expr->id());
11408 DCHECK_EQ(Token::EQ, expr->op());
11409 Type* type = expr->combined_type()->Is(Type::None())
11410 ? Type::Any(zone()) : expr->combined_type();
11411 HIfContinuation continuation;
11412 BuildCompareNil(value, type, &continuation);
11413 return ast_context()->ReturnContinuation(&continuation, expr->id());
11418 void HOptimizedGraphBuilder::VisitSpread(Spread* expr) { UNREACHABLE(); }
11421 HInstruction* HOptimizedGraphBuilder::BuildThisFunction() {
11422 // If we share optimized code between different closures, the
11423 // this-function is not a constant, except inside an inlined body.
11424 if (function_state()->outer() != NULL) {
11425 return New<HConstant>(
11426 function_state()->compilation_info()->closure());
11428 return New<HThisFunction>();
11433 HInstruction* HOptimizedGraphBuilder::BuildFastLiteral(
11434 Handle<JSObject> boilerplate_object,
11435 AllocationSiteUsageContext* site_context) {
11436 NoObservableSideEffectsScope no_effects(this);
11437 Handle<Map> initial_map(boilerplate_object->map());
11438 InstanceType instance_type = initial_map->instance_type();
11439 DCHECK(instance_type == JS_ARRAY_TYPE || instance_type == JS_OBJECT_TYPE);
11441 HType type = instance_type == JS_ARRAY_TYPE
11442 ? HType::JSArray() : HType::JSObject();
11443 HValue* object_size_constant = Add<HConstant>(initial_map->instance_size());
11445 PretenureFlag pretenure_flag = NOT_TENURED;
11446 Handle<AllocationSite> current_site(*site_context->current(), isolate());
11447 if (FLAG_allocation_site_pretenuring) {
11448 pretenure_flag = current_site->GetPretenureMode();
11449 top_info()->dependencies()->AssumeTenuringDecision(current_site);
11452 top_info()->dependencies()->AssumeTransitionStable(current_site);
11454 HInstruction* object = Add<HAllocate>(
11455 object_size_constant, type, pretenure_flag, instance_type, current_site);
11457 // If allocation folding reaches Page::kMaxRegularHeapObjectSize the
11458 // elements array may not get folded into the object. Hence, we set the
11459 // elements pointer to empty fixed array and let store elimination remove
11460 // this store in the folding case.
11461 HConstant* empty_fixed_array = Add<HConstant>(
11462 isolate()->factory()->empty_fixed_array());
11463 Add<HStoreNamedField>(object, HObjectAccess::ForElementsPointer(),
11464 empty_fixed_array);
11466 BuildEmitObjectHeader(boilerplate_object, object);
11468 // Similarly to the elements pointer, there is no guarantee that all
11469 // property allocations can get folded, so pre-initialize all in-object
11470 // properties to a safe value.
11471 BuildInitializeInobjectProperties(object, initial_map);
11473 Handle<FixedArrayBase> elements(boilerplate_object->elements());
11474 int elements_size = (elements->length() > 0 &&
11475 elements->map() != isolate()->heap()->fixed_cow_array_map()) ?
11476 elements->Size() : 0;
11478 if (pretenure_flag == TENURED &&
11479 elements->map() == isolate()->heap()->fixed_cow_array_map() &&
11480 isolate()->heap()->InNewSpace(*elements)) {
11481 // If we would like to pretenure a fixed cow array, we must ensure that the
11482 // array is already in old space, otherwise we'll create too many old-to-
11483 // new-space pointers (overflowing the store buffer).
11484 elements = Handle<FixedArrayBase>(
11485 isolate()->factory()->CopyAndTenureFixedCOWArray(
11486 Handle<FixedArray>::cast(elements)));
11487 boilerplate_object->set_elements(*elements);
11490 HInstruction* object_elements = NULL;
11491 if (elements_size > 0) {
11492 HValue* object_elements_size = Add<HConstant>(elements_size);
11493 InstanceType instance_type = boilerplate_object->HasFastDoubleElements()
11494 ? FIXED_DOUBLE_ARRAY_TYPE : FIXED_ARRAY_TYPE;
11496 Add<HAllocate>(object_elements_size, HType::HeapObject(),
11497 pretenure_flag, instance_type, current_site);
11498 BuildEmitElements(boilerplate_object, elements, object_elements,
11500 Add<HStoreNamedField>(object, HObjectAccess::ForElementsPointer(),
11503 Handle<Object> elements_field =
11504 Handle<Object>(boilerplate_object->elements(), isolate());
11505 HInstruction* object_elements_cow = Add<HConstant>(elements_field);
11506 Add<HStoreNamedField>(object, HObjectAccess::ForElementsPointer(),
11507 object_elements_cow);
11510 // Copy in-object properties.
11511 if (initial_map->NumberOfFields() != 0 ||
11512 initial_map->unused_property_fields() > 0) {
11513 BuildEmitInObjectProperties(boilerplate_object, object, site_context,
11520 void HOptimizedGraphBuilder::BuildEmitObjectHeader(
11521 Handle<JSObject> boilerplate_object,
11522 HInstruction* object) {
11523 DCHECK(boilerplate_object->properties()->length() == 0);
11525 Handle<Map> boilerplate_object_map(boilerplate_object->map());
11526 AddStoreMapConstant(object, boilerplate_object_map);
11528 Handle<Object> properties_field =
11529 Handle<Object>(boilerplate_object->properties(), isolate());
11530 DCHECK(*properties_field == isolate()->heap()->empty_fixed_array());
11531 HInstruction* properties = Add<HConstant>(properties_field);
11532 HObjectAccess access = HObjectAccess::ForPropertiesPointer();
11533 Add<HStoreNamedField>(object, access, properties);
11535 if (boilerplate_object->IsJSArray()) {
11536 Handle<JSArray> boilerplate_array =
11537 Handle<JSArray>::cast(boilerplate_object);
11538 Handle<Object> length_field =
11539 Handle<Object>(boilerplate_array->length(), isolate());
11540 HInstruction* length = Add<HConstant>(length_field);
11542 DCHECK(boilerplate_array->length()->IsSmi());
11543 Add<HStoreNamedField>(object, HObjectAccess::ForArrayLength(
11544 boilerplate_array->GetElementsKind()), length);
11549 void HOptimizedGraphBuilder::BuildEmitInObjectProperties(
11550 Handle<JSObject> boilerplate_object,
11551 HInstruction* object,
11552 AllocationSiteUsageContext* site_context,
11553 PretenureFlag pretenure_flag) {
11554 Handle<Map> boilerplate_map(boilerplate_object->map());
11555 Handle<DescriptorArray> descriptors(boilerplate_map->instance_descriptors());
11556 int limit = boilerplate_map->NumberOfOwnDescriptors();
11558 int copied_fields = 0;
11559 for (int i = 0; i < limit; i++) {
11560 PropertyDetails details = descriptors->GetDetails(i);
11561 if (details.type() != DATA) continue;
11563 FieldIndex field_index = FieldIndex::ForDescriptor(*boilerplate_map, i);
11566 int property_offset = field_index.offset();
11567 Handle<Name> name(descriptors->GetKey(i));
11569 // The access for the store depends on the type of the boilerplate.
11570 HObjectAccess access = boilerplate_object->IsJSArray() ?
11571 HObjectAccess::ForJSArrayOffset(property_offset) :
11572 HObjectAccess::ForMapAndOffset(boilerplate_map, property_offset);
11574 if (boilerplate_object->IsUnboxedDoubleField(field_index)) {
11575 CHECK(!boilerplate_object->IsJSArray());
11576 double value = boilerplate_object->RawFastDoublePropertyAt(field_index);
11577 access = access.WithRepresentation(Representation::Double());
11578 Add<HStoreNamedField>(object, access, Add<HConstant>(value));
11581 Handle<Object> value(boilerplate_object->RawFastPropertyAt(field_index),
11584 if (value->IsJSObject()) {
11585 Handle<JSObject> value_object = Handle<JSObject>::cast(value);
11586 Handle<AllocationSite> current_site = site_context->EnterNewScope();
11587 HInstruction* result =
11588 BuildFastLiteral(value_object, site_context);
11589 site_context->ExitScope(current_site, value_object);
11590 Add<HStoreNamedField>(object, access, result);
11592 Representation representation = details.representation();
11593 HInstruction* value_instruction;
11595 if (representation.IsDouble()) {
11596 // Allocate a HeapNumber box and store the value into it.
11597 HValue* heap_number_constant = Add<HConstant>(HeapNumber::kSize);
11598 // This heap number alloc does not have a corresponding
11599 // AllocationSite. That is okay because
11600 // 1) it's a child object of another object with a valid allocation site
11601 // 2) we can just use the mode of the parent object for pretenuring
11602 HInstruction* double_box =
11603 Add<HAllocate>(heap_number_constant, HType::HeapObject(),
11604 pretenure_flag, MUTABLE_HEAP_NUMBER_TYPE);
11605 AddStoreMapConstant(double_box,
11606 isolate()->factory()->mutable_heap_number_map());
11607 // Unwrap the mutable heap number from the boilerplate.
11608 HValue* double_value =
11609 Add<HConstant>(Handle<HeapNumber>::cast(value)->value());
11610 Add<HStoreNamedField>(
11611 double_box, HObjectAccess::ForHeapNumberValue(), double_value);
11612 value_instruction = double_box;
11613 } else if (representation.IsSmi()) {
11614 value_instruction = value->IsUninitialized()
11615 ? graph()->GetConstant0()
11616 : Add<HConstant>(value);
11617 // Ensure that value is stored as smi.
11618 access = access.WithRepresentation(representation);
11620 value_instruction = Add<HConstant>(value);
11623 Add<HStoreNamedField>(object, access, value_instruction);
11627 int inobject_properties = boilerplate_object->map()->inobject_properties();
11628 HInstruction* value_instruction =
11629 Add<HConstant>(isolate()->factory()->one_pointer_filler_map());
11630 for (int i = copied_fields; i < inobject_properties; i++) {
11631 DCHECK(boilerplate_object->IsJSObject());
11632 int property_offset = boilerplate_object->GetInObjectPropertyOffset(i);
11633 HObjectAccess access =
11634 HObjectAccess::ForMapAndOffset(boilerplate_map, property_offset);
11635 Add<HStoreNamedField>(object, access, value_instruction);
11640 void HOptimizedGraphBuilder::BuildEmitElements(
11641 Handle<JSObject> boilerplate_object,
11642 Handle<FixedArrayBase> elements,
11643 HValue* object_elements,
11644 AllocationSiteUsageContext* site_context) {
11645 ElementsKind kind = boilerplate_object->map()->elements_kind();
11646 int elements_length = elements->length();
11647 HValue* object_elements_length = Add<HConstant>(elements_length);
11648 BuildInitializeElementsHeader(object_elements, kind, object_elements_length);
11650 // Copy elements backing store content.
11651 if (elements->IsFixedDoubleArray()) {
11652 BuildEmitFixedDoubleArray(elements, kind, object_elements);
11653 } else if (elements->IsFixedArray()) {
11654 BuildEmitFixedArray(elements, kind, object_elements,
11662 void HOptimizedGraphBuilder::BuildEmitFixedDoubleArray(
11663 Handle<FixedArrayBase> elements,
11665 HValue* object_elements) {
11666 HInstruction* boilerplate_elements = Add<HConstant>(elements);
11667 int elements_length = elements->length();
11668 for (int i = 0; i < elements_length; i++) {
11669 HValue* key_constant = Add<HConstant>(i);
11670 HInstruction* value_instruction = Add<HLoadKeyed>(
11671 boilerplate_elements, key_constant, nullptr, kind, ALLOW_RETURN_HOLE);
11672 HInstruction* store = Add<HStoreKeyed>(object_elements, key_constant,
11673 value_instruction, kind);
11674 store->SetFlag(HValue::kAllowUndefinedAsNaN);
11679 void HOptimizedGraphBuilder::BuildEmitFixedArray(
11680 Handle<FixedArrayBase> elements,
11682 HValue* object_elements,
11683 AllocationSiteUsageContext* site_context) {
11684 HInstruction* boilerplate_elements = Add<HConstant>(elements);
11685 int elements_length = elements->length();
11686 Handle<FixedArray> fast_elements = Handle<FixedArray>::cast(elements);
11687 for (int i = 0; i < elements_length; i++) {
11688 Handle<Object> value(fast_elements->get(i), isolate());
11689 HValue* key_constant = Add<HConstant>(i);
11690 if (value->IsJSObject()) {
11691 Handle<JSObject> value_object = Handle<JSObject>::cast(value);
11692 Handle<AllocationSite> current_site = site_context->EnterNewScope();
11693 HInstruction* result =
11694 BuildFastLiteral(value_object, site_context);
11695 site_context->ExitScope(current_site, value_object);
11696 Add<HStoreKeyed>(object_elements, key_constant, result, kind);
11698 ElementsKind copy_kind =
11699 kind == FAST_HOLEY_SMI_ELEMENTS ? FAST_HOLEY_ELEMENTS : kind;
11700 HInstruction* value_instruction =
11701 Add<HLoadKeyed>(boilerplate_elements, key_constant, nullptr,
11702 copy_kind, ALLOW_RETURN_HOLE);
11703 Add<HStoreKeyed>(object_elements, key_constant, value_instruction,
11710 void HOptimizedGraphBuilder::VisitThisFunction(ThisFunction* expr) {
11711 DCHECK(!HasStackOverflow());
11712 DCHECK(current_block() != NULL);
11713 DCHECK(current_block()->HasPredecessor());
11714 HInstruction* instr = BuildThisFunction();
11715 return ast_context()->ReturnInstruction(instr, expr->id());
11719 void HOptimizedGraphBuilder::VisitSuperPropertyReference(
11720 SuperPropertyReference* expr) {
11721 DCHECK(!HasStackOverflow());
11722 DCHECK(current_block() != NULL);
11723 DCHECK(current_block()->HasPredecessor());
11724 return Bailout(kSuperReference);
11728 void HOptimizedGraphBuilder::VisitSuperCallReference(SuperCallReference* expr) {
11729 DCHECK(!HasStackOverflow());
11730 DCHECK(current_block() != NULL);
11731 DCHECK(current_block()->HasPredecessor());
11732 return Bailout(kSuperReference);
11736 void HOptimizedGraphBuilder::VisitDeclarations(
11737 ZoneList<Declaration*>* declarations) {
11738 DCHECK(globals_.is_empty());
11739 AstVisitor::VisitDeclarations(declarations);
11740 if (!globals_.is_empty()) {
11741 Handle<FixedArray> array =
11742 isolate()->factory()->NewFixedArray(globals_.length(), TENURED);
11743 for (int i = 0; i < globals_.length(); ++i) array->set(i, *globals_.at(i));
11745 DeclareGlobalsEvalFlag::encode(current_info()->is_eval()) |
11746 DeclareGlobalsNativeFlag::encode(current_info()->is_native()) |
11747 DeclareGlobalsLanguageMode::encode(current_info()->language_mode());
11748 Add<HDeclareGlobals>(array, flags);
11749 globals_.Rewind(0);
11754 void HOptimizedGraphBuilder::VisitVariableDeclaration(
11755 VariableDeclaration* declaration) {
11756 VariableProxy* proxy = declaration->proxy();
11757 VariableMode mode = declaration->mode();
11758 Variable* variable = proxy->var();
11759 bool hole_init = mode == LET || mode == CONST || mode == CONST_LEGACY;
11760 switch (variable->location()) {
11761 case VariableLocation::GLOBAL:
11762 case VariableLocation::UNALLOCATED:
11763 globals_.Add(variable->name(), zone());
11764 globals_.Add(variable->binding_needs_init()
11765 ? isolate()->factory()->the_hole_value()
11766 : isolate()->factory()->undefined_value(), zone());
11768 case VariableLocation::PARAMETER:
11769 case VariableLocation::LOCAL:
11771 HValue* value = graph()->GetConstantHole();
11772 environment()->Bind(variable, value);
11775 case VariableLocation::CONTEXT:
11777 HValue* value = graph()->GetConstantHole();
11778 HValue* context = environment()->context();
11779 HStoreContextSlot* store = Add<HStoreContextSlot>(
11780 context, variable->index(), HStoreContextSlot::kNoCheck, value);
11781 if (store->HasObservableSideEffects()) {
11782 Add<HSimulate>(proxy->id(), REMOVABLE_SIMULATE);
11786 case VariableLocation::LOOKUP:
11787 return Bailout(kUnsupportedLookupSlotInDeclaration);
11792 void HOptimizedGraphBuilder::VisitFunctionDeclaration(
11793 FunctionDeclaration* declaration) {
11794 VariableProxy* proxy = declaration->proxy();
11795 Variable* variable = proxy->var();
11796 switch (variable->location()) {
11797 case VariableLocation::GLOBAL:
11798 case VariableLocation::UNALLOCATED: {
11799 globals_.Add(variable->name(), zone());
11800 Handle<SharedFunctionInfo> function = Compiler::GetSharedFunctionInfo(
11801 declaration->fun(), current_info()->script(), top_info());
11802 // Check for stack-overflow exception.
11803 if (function.is_null()) return SetStackOverflow();
11804 globals_.Add(function, zone());
11807 case VariableLocation::PARAMETER:
11808 case VariableLocation::LOCAL: {
11809 CHECK_ALIVE(VisitForValue(declaration->fun()));
11810 HValue* value = Pop();
11811 BindIfLive(variable, value);
11814 case VariableLocation::CONTEXT: {
11815 CHECK_ALIVE(VisitForValue(declaration->fun()));
11816 HValue* value = Pop();
11817 HValue* context = environment()->context();
11818 HStoreContextSlot* store = Add<HStoreContextSlot>(
11819 context, variable->index(), HStoreContextSlot::kNoCheck, value);
11820 if (store->HasObservableSideEffects()) {
11821 Add<HSimulate>(proxy->id(), REMOVABLE_SIMULATE);
11825 case VariableLocation::LOOKUP:
11826 return Bailout(kUnsupportedLookupSlotInDeclaration);
11831 void HOptimizedGraphBuilder::VisitImportDeclaration(
11832 ImportDeclaration* declaration) {
11837 void HOptimizedGraphBuilder::VisitExportDeclaration(
11838 ExportDeclaration* declaration) {
11843 // Generators for inline runtime functions.
11844 // Support for types.
11845 void HOptimizedGraphBuilder::GenerateIsSmi(CallRuntime* call) {
11846 DCHECK(call->arguments()->length() == 1);
11847 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11848 HValue* value = Pop();
11849 HIsSmiAndBranch* result = New<HIsSmiAndBranch>(value);
11850 return ast_context()->ReturnControl(result, call->id());
11854 void HOptimizedGraphBuilder::GenerateIsSpecObject(CallRuntime* call) {
11855 DCHECK(call->arguments()->length() == 1);
11856 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11857 HValue* value = Pop();
11858 HHasInstanceTypeAndBranch* result =
11859 New<HHasInstanceTypeAndBranch>(value,
11860 FIRST_SPEC_OBJECT_TYPE,
11861 LAST_SPEC_OBJECT_TYPE);
11862 return ast_context()->ReturnControl(result, call->id());
11866 void HOptimizedGraphBuilder::GenerateIsFunction(CallRuntime* call) {
11867 DCHECK(call->arguments()->length() == 1);
11868 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11869 HValue* value = Pop();
11870 HHasInstanceTypeAndBranch* result =
11871 New<HHasInstanceTypeAndBranch>(value, JS_FUNCTION_TYPE);
11872 return ast_context()->ReturnControl(result, call->id());
11876 void HOptimizedGraphBuilder::GenerateIsMinusZero(CallRuntime* call) {
11877 DCHECK(call->arguments()->length() == 1);
11878 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11879 HValue* value = Pop();
11880 HCompareMinusZeroAndBranch* result = New<HCompareMinusZeroAndBranch>(value);
11881 return ast_context()->ReturnControl(result, call->id());
11885 void HOptimizedGraphBuilder::GenerateHasCachedArrayIndex(CallRuntime* call) {
11886 DCHECK(call->arguments()->length() == 1);
11887 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11888 HValue* value = Pop();
11889 HHasCachedArrayIndexAndBranch* result =
11890 New<HHasCachedArrayIndexAndBranch>(value);
11891 return ast_context()->ReturnControl(result, call->id());
11895 void HOptimizedGraphBuilder::GenerateIsArray(CallRuntime* call) {
11896 DCHECK(call->arguments()->length() == 1);
11897 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11898 HValue* value = Pop();
11899 HHasInstanceTypeAndBranch* result =
11900 New<HHasInstanceTypeAndBranch>(value, JS_ARRAY_TYPE);
11901 return ast_context()->ReturnControl(result, call->id());
11905 void HOptimizedGraphBuilder::GenerateIsTypedArray(CallRuntime* call) {
11906 DCHECK(call->arguments()->length() == 1);
11907 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11908 HValue* value = Pop();
11909 HHasInstanceTypeAndBranch* result =
11910 New<HHasInstanceTypeAndBranch>(value, JS_TYPED_ARRAY_TYPE);
11911 return ast_context()->ReturnControl(result, call->id());
11915 void HOptimizedGraphBuilder::GenerateIsRegExp(CallRuntime* call) {
11916 DCHECK(call->arguments()->length() == 1);
11917 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11918 HValue* value = Pop();
11919 HHasInstanceTypeAndBranch* result =
11920 New<HHasInstanceTypeAndBranch>(value, JS_REGEXP_TYPE);
11921 return ast_context()->ReturnControl(result, call->id());
11925 void HOptimizedGraphBuilder::GenerateIsObject(CallRuntime* call) {
11926 DCHECK(call->arguments()->length() == 1);
11927 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11928 HValue* value = Pop();
11929 HIsObjectAndBranch* result = New<HIsObjectAndBranch>(value);
11930 return ast_context()->ReturnControl(result, call->id());
11934 void HOptimizedGraphBuilder::GenerateIsJSProxy(CallRuntime* call) {
11935 DCHECK(call->arguments()->length() == 1);
11936 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11937 HValue* value = Pop();
11938 HIfContinuation continuation;
11939 IfBuilder if_proxy(this);
11941 HValue* smicheck = if_proxy.IfNot<HIsSmiAndBranch>(value);
11943 HValue* map = Add<HLoadNamedField>(value, smicheck, HObjectAccess::ForMap());
11944 HValue* instance_type =
11945 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapInstanceType());
11946 if_proxy.If<HCompareNumericAndBranch>(
11947 instance_type, Add<HConstant>(FIRST_JS_PROXY_TYPE), Token::GTE);
11949 if_proxy.If<HCompareNumericAndBranch>(
11950 instance_type, Add<HConstant>(LAST_JS_PROXY_TYPE), Token::LTE);
11952 if_proxy.CaptureContinuation(&continuation);
11953 return ast_context()->ReturnContinuation(&continuation, call->id());
11957 void HOptimizedGraphBuilder::GenerateHasFastPackedElements(CallRuntime* call) {
11958 DCHECK(call->arguments()->length() == 1);
11959 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11960 HValue* object = Pop();
11961 HIfContinuation continuation(graph()->CreateBasicBlock(),
11962 graph()->CreateBasicBlock());
11963 IfBuilder if_not_smi(this);
11964 if_not_smi.IfNot<HIsSmiAndBranch>(object);
11967 NoObservableSideEffectsScope no_effects(this);
11969 IfBuilder if_fast_packed(this);
11970 HValue* elements_kind = BuildGetElementsKind(object);
11971 if_fast_packed.If<HCompareNumericAndBranch>(
11972 elements_kind, Add<HConstant>(FAST_SMI_ELEMENTS), Token::EQ);
11973 if_fast_packed.Or();
11974 if_fast_packed.If<HCompareNumericAndBranch>(
11975 elements_kind, Add<HConstant>(FAST_ELEMENTS), Token::EQ);
11976 if_fast_packed.Or();
11977 if_fast_packed.If<HCompareNumericAndBranch>(
11978 elements_kind, Add<HConstant>(FAST_DOUBLE_ELEMENTS), Token::EQ);
11979 if_fast_packed.JoinContinuation(&continuation);
11981 if_not_smi.JoinContinuation(&continuation);
11982 return ast_context()->ReturnContinuation(&continuation, call->id());
11986 void HOptimizedGraphBuilder::GenerateIsUndetectableObject(CallRuntime* call) {
11987 DCHECK(call->arguments()->length() == 1);
11988 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
11989 HValue* value = Pop();
11990 HIsUndetectableAndBranch* result = New<HIsUndetectableAndBranch>(value);
11991 return ast_context()->ReturnControl(result, call->id());
11995 // Support for construct call checks.
11996 void HOptimizedGraphBuilder::GenerateIsConstructCall(CallRuntime* call) {
11997 DCHECK(call->arguments()->length() == 0);
11998 if (function_state()->outer() != NULL) {
11999 // We are generating graph for inlined function.
12000 HValue* value = function_state()->inlining_kind() == CONSTRUCT_CALL_RETURN
12001 ? graph()->GetConstantTrue()
12002 : graph()->GetConstantFalse();
12003 return ast_context()->ReturnValue(value);
12005 return ast_context()->ReturnControl(New<HIsConstructCallAndBranch>(),
12011 // Support for arguments.length and arguments[?].
12012 void HOptimizedGraphBuilder::GenerateArgumentsLength(CallRuntime* call) {
12013 DCHECK(call->arguments()->length() == 0);
12014 HInstruction* result = NULL;
12015 if (function_state()->outer() == NULL) {
12016 HInstruction* elements = Add<HArgumentsElements>(false);
12017 result = New<HArgumentsLength>(elements);
12019 // Number of arguments without receiver.
12020 int argument_count = environment()->
12021 arguments_environment()->parameter_count() - 1;
12022 result = New<HConstant>(argument_count);
12024 return ast_context()->ReturnInstruction(result, call->id());
12028 void HOptimizedGraphBuilder::GenerateArguments(CallRuntime* call) {
12029 DCHECK(call->arguments()->length() == 1);
12030 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12031 HValue* index = Pop();
12032 HInstruction* result = NULL;
12033 if (function_state()->outer() == NULL) {
12034 HInstruction* elements = Add<HArgumentsElements>(false);
12035 HInstruction* length = Add<HArgumentsLength>(elements);
12036 HInstruction* checked_index = Add<HBoundsCheck>(index, length);
12037 result = New<HAccessArgumentsAt>(elements, length, checked_index);
12039 EnsureArgumentsArePushedForAccess();
12041 // Number of arguments without receiver.
12042 HInstruction* elements = function_state()->arguments_elements();
12043 int argument_count = environment()->
12044 arguments_environment()->parameter_count() - 1;
12045 HInstruction* length = Add<HConstant>(argument_count);
12046 HInstruction* checked_key = Add<HBoundsCheck>(index, length);
12047 result = New<HAccessArgumentsAt>(elements, length, checked_key);
12049 return ast_context()->ReturnInstruction(result, call->id());
12053 void HOptimizedGraphBuilder::GenerateValueOf(CallRuntime* call) {
12054 DCHECK(call->arguments()->length() == 1);
12055 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12056 HValue* object = Pop();
12058 IfBuilder if_objectisvalue(this);
12059 HValue* objectisvalue = if_objectisvalue.If<HHasInstanceTypeAndBranch>(
12060 object, JS_VALUE_TYPE);
12061 if_objectisvalue.Then();
12063 // Return the actual value.
12064 Push(Add<HLoadNamedField>(
12065 object, objectisvalue,
12066 HObjectAccess::ForObservableJSObjectOffset(
12067 JSValue::kValueOffset)));
12068 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12070 if_objectisvalue.Else();
12072 // If the object is not a value return the object.
12074 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12076 if_objectisvalue.End();
12077 return ast_context()->ReturnValue(Pop());
12081 void HOptimizedGraphBuilder::GenerateJSValueGetValue(CallRuntime* call) {
12082 DCHECK(call->arguments()->length() == 1);
12083 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12084 HValue* value = Pop();
12085 HInstruction* result = Add<HLoadNamedField>(
12087 HObjectAccess::ForObservableJSObjectOffset(JSValue::kValueOffset));
12088 return ast_context()->ReturnInstruction(result, call->id());
12092 void HOptimizedGraphBuilder::GenerateIsDate(CallRuntime* call) {
12093 DCHECK_EQ(1, call->arguments()->length());
12094 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12095 HValue* value = Pop();
12096 HHasInstanceTypeAndBranch* result =
12097 New<HHasInstanceTypeAndBranch>(value, JS_DATE_TYPE);
12098 return ast_context()->ReturnControl(result, call->id());
12102 void HOptimizedGraphBuilder::GenerateThrowNotDateError(CallRuntime* call) {
12103 DCHECK_EQ(0, call->arguments()->length());
12104 Add<HDeoptimize>(Deoptimizer::kNotADateObject, Deoptimizer::EAGER);
12105 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12106 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12110 void HOptimizedGraphBuilder::GenerateDateField(CallRuntime* call) {
12111 DCHECK(call->arguments()->length() == 2);
12112 DCHECK_NOT_NULL(call->arguments()->at(1)->AsLiteral());
12113 Smi* index = Smi::cast(*(call->arguments()->at(1)->AsLiteral()->value()));
12114 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12115 HValue* date = Pop();
12116 HDateField* result = New<HDateField>(date, index);
12117 return ast_context()->ReturnInstruction(result, call->id());
12121 void HOptimizedGraphBuilder::GenerateOneByteSeqStringSetChar(
12122 CallRuntime* call) {
12123 DCHECK(call->arguments()->length() == 3);
12124 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12125 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12126 CHECK_ALIVE(VisitForValue(call->arguments()->at(2)));
12127 HValue* string = Pop();
12128 HValue* value = Pop();
12129 HValue* index = Pop();
12130 Add<HSeqStringSetChar>(String::ONE_BYTE_ENCODING, string,
12132 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12133 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12137 void HOptimizedGraphBuilder::GenerateTwoByteSeqStringSetChar(
12138 CallRuntime* call) {
12139 DCHECK(call->arguments()->length() == 3);
12140 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12141 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12142 CHECK_ALIVE(VisitForValue(call->arguments()->at(2)));
12143 HValue* string = Pop();
12144 HValue* value = Pop();
12145 HValue* index = Pop();
12146 Add<HSeqStringSetChar>(String::TWO_BYTE_ENCODING, string,
12148 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12149 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12153 void HOptimizedGraphBuilder::GenerateSetValueOf(CallRuntime* call) {
12154 DCHECK(call->arguments()->length() == 2);
12155 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12156 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12157 HValue* value = Pop();
12158 HValue* object = Pop();
12160 // Check if object is a JSValue.
12161 IfBuilder if_objectisvalue(this);
12162 if_objectisvalue.If<HHasInstanceTypeAndBranch>(object, JS_VALUE_TYPE);
12163 if_objectisvalue.Then();
12165 // Create in-object property store to kValueOffset.
12166 Add<HStoreNamedField>(object,
12167 HObjectAccess::ForObservableJSObjectOffset(JSValue::kValueOffset),
12169 if (!ast_context()->IsEffect()) {
12172 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12174 if_objectisvalue.Else();
12176 // Nothing to do in this case.
12177 if (!ast_context()->IsEffect()) {
12180 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12182 if_objectisvalue.End();
12183 if (!ast_context()->IsEffect()) {
12186 return ast_context()->ReturnValue(value);
12190 // Fast support for charCodeAt(n).
12191 void HOptimizedGraphBuilder::GenerateStringCharCodeAt(CallRuntime* call) {
12192 DCHECK(call->arguments()->length() == 2);
12193 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12194 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12195 HValue* index = Pop();
12196 HValue* string = Pop();
12197 HInstruction* result = BuildStringCharCodeAt(string, index);
12198 return ast_context()->ReturnInstruction(result, call->id());
12202 // Fast support for string.charAt(n) and string[n].
12203 void HOptimizedGraphBuilder::GenerateStringCharFromCode(CallRuntime* call) {
12204 DCHECK(call->arguments()->length() == 1);
12205 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12206 HValue* char_code = Pop();
12207 HInstruction* result = NewUncasted<HStringCharFromCode>(char_code);
12208 return ast_context()->ReturnInstruction(result, call->id());
12212 // Fast support for string.charAt(n) and string[n].
12213 void HOptimizedGraphBuilder::GenerateStringCharAt(CallRuntime* call) {
12214 DCHECK(call->arguments()->length() == 2);
12215 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12216 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12217 HValue* index = Pop();
12218 HValue* string = Pop();
12219 HInstruction* char_code = BuildStringCharCodeAt(string, index);
12220 AddInstruction(char_code);
12221 HInstruction* result = NewUncasted<HStringCharFromCode>(char_code);
12222 return ast_context()->ReturnInstruction(result, call->id());
12226 // Fast support for object equality testing.
12227 void HOptimizedGraphBuilder::GenerateObjectEquals(CallRuntime* call) {
12228 DCHECK(call->arguments()->length() == 2);
12229 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12230 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12231 HValue* right = Pop();
12232 HValue* left = Pop();
12233 HCompareObjectEqAndBranch* result =
12234 New<HCompareObjectEqAndBranch>(left, right);
12235 return ast_context()->ReturnControl(result, call->id());
12239 // Fast support for StringAdd.
12240 void HOptimizedGraphBuilder::GenerateStringAdd(CallRuntime* call) {
12241 DCHECK_EQ(2, call->arguments()->length());
12242 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12243 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12244 HValue* right = Pop();
12245 HValue* left = Pop();
12246 HInstruction* result =
12247 NewUncasted<HStringAdd>(left, right, strength(function_language_mode()));
12248 return ast_context()->ReturnInstruction(result, call->id());
12252 // Fast support for SubString.
12253 void HOptimizedGraphBuilder::GenerateSubString(CallRuntime* call) {
12254 DCHECK_EQ(3, call->arguments()->length());
12255 CHECK_ALIVE(VisitExpressions(call->arguments()));
12256 PushArgumentsFromEnvironment(call->arguments()->length());
12257 HCallStub* result = New<HCallStub>(CodeStub::SubString, 3);
12258 return ast_context()->ReturnInstruction(result, call->id());
12262 // Fast support for StringCompare.
12263 void HOptimizedGraphBuilder::GenerateStringCompare(CallRuntime* call) {
12264 DCHECK_EQ(2, call->arguments()->length());
12265 CHECK_ALIVE(VisitExpressions(call->arguments()));
12266 PushArgumentsFromEnvironment(call->arguments()->length());
12267 HCallStub* result = New<HCallStub>(CodeStub::StringCompare, 2);
12268 return ast_context()->ReturnInstruction(result, call->id());
12272 void HOptimizedGraphBuilder::GenerateStringGetLength(CallRuntime* call) {
12273 DCHECK(call->arguments()->length() == 1);
12274 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12275 HValue* string = Pop();
12276 HInstruction* result = BuildLoadStringLength(string);
12277 return ast_context()->ReturnInstruction(result, call->id());
12281 // Support for direct calls from JavaScript to native RegExp code.
12282 void HOptimizedGraphBuilder::GenerateRegExpExec(CallRuntime* call) {
12283 DCHECK_EQ(4, call->arguments()->length());
12284 CHECK_ALIVE(VisitExpressions(call->arguments()));
12285 PushArgumentsFromEnvironment(call->arguments()->length());
12286 HCallStub* result = New<HCallStub>(CodeStub::RegExpExec, 4);
12287 return ast_context()->ReturnInstruction(result, call->id());
12291 void HOptimizedGraphBuilder::GenerateDoubleLo(CallRuntime* call) {
12292 DCHECK_EQ(1, call->arguments()->length());
12293 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12294 HValue* value = Pop();
12295 HInstruction* result = NewUncasted<HDoubleBits>(value, HDoubleBits::LOW);
12296 return ast_context()->ReturnInstruction(result, call->id());
12300 void HOptimizedGraphBuilder::GenerateDoubleHi(CallRuntime* call) {
12301 DCHECK_EQ(1, call->arguments()->length());
12302 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12303 HValue* value = Pop();
12304 HInstruction* result = NewUncasted<HDoubleBits>(value, HDoubleBits::HIGH);
12305 return ast_context()->ReturnInstruction(result, call->id());
12309 void HOptimizedGraphBuilder::GenerateConstructDouble(CallRuntime* call) {
12310 DCHECK_EQ(2, call->arguments()->length());
12311 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12312 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12313 HValue* lo = Pop();
12314 HValue* hi = Pop();
12315 HInstruction* result = NewUncasted<HConstructDouble>(hi, lo);
12316 return ast_context()->ReturnInstruction(result, call->id());
12320 // Construct a RegExp exec result with two in-object properties.
12321 void HOptimizedGraphBuilder::GenerateRegExpConstructResult(CallRuntime* call) {
12322 DCHECK_EQ(3, call->arguments()->length());
12323 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12324 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12325 CHECK_ALIVE(VisitForValue(call->arguments()->at(2)));
12326 HValue* input = Pop();
12327 HValue* index = Pop();
12328 HValue* length = Pop();
12329 HValue* result = BuildRegExpConstructResult(length, index, input);
12330 return ast_context()->ReturnValue(result);
12334 // Support for fast native caches.
12335 void HOptimizedGraphBuilder::GenerateGetFromCache(CallRuntime* call) {
12336 return Bailout(kInlinedRuntimeFunctionGetFromCache);
12340 // Fast support for number to string.
12341 void HOptimizedGraphBuilder::GenerateNumberToString(CallRuntime* call) {
12342 DCHECK_EQ(1, call->arguments()->length());
12343 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12344 HValue* number = Pop();
12345 HValue* result = BuildNumberToString(number, Type::Any(zone()));
12346 return ast_context()->ReturnValue(result);
12350 // Fast call for custom callbacks.
12351 void HOptimizedGraphBuilder::GenerateCallFunction(CallRuntime* call) {
12352 // 1 ~ The function to call is not itself an argument to the call.
12353 int arg_count = call->arguments()->length() - 1;
12354 DCHECK(arg_count >= 1); // There's always at least a receiver.
12356 CHECK_ALIVE(VisitExpressions(call->arguments()));
12357 // The function is the last argument
12358 HValue* function = Pop();
12359 // Push the arguments to the stack
12360 PushArgumentsFromEnvironment(arg_count);
12362 IfBuilder if_is_jsfunction(this);
12363 if_is_jsfunction.If<HHasInstanceTypeAndBranch>(function, JS_FUNCTION_TYPE);
12365 if_is_jsfunction.Then();
12367 HInstruction* invoke_result =
12368 Add<HInvokeFunction>(function, arg_count);
12369 if (!ast_context()->IsEffect()) {
12370 Push(invoke_result);
12372 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12375 if_is_jsfunction.Else();
12377 HInstruction* call_result =
12378 Add<HCallFunction>(function, arg_count);
12379 if (!ast_context()->IsEffect()) {
12382 Add<HSimulate>(call->id(), FIXED_SIMULATE);
12384 if_is_jsfunction.End();
12386 if (ast_context()->IsEffect()) {
12387 // EffectContext::ReturnValue ignores the value, so we can just pass
12388 // 'undefined' (as we do not have the call result anymore).
12389 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12391 return ast_context()->ReturnValue(Pop());
12396 // Fast call to math functions.
12397 void HOptimizedGraphBuilder::GenerateMathPow(CallRuntime* call) {
12398 DCHECK_EQ(2, call->arguments()->length());
12399 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12400 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12401 HValue* right = Pop();
12402 HValue* left = Pop();
12403 HInstruction* result = NewUncasted<HPower>(left, right);
12404 return ast_context()->ReturnInstruction(result, call->id());
12408 void HOptimizedGraphBuilder::GenerateMathClz32(CallRuntime* call) {
12409 DCHECK(call->arguments()->length() == 1);
12410 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12411 HValue* value = Pop();
12412 HInstruction* result = NewUncasted<HUnaryMathOperation>(value, kMathClz32);
12413 return ast_context()->ReturnInstruction(result, call->id());
12417 void HOptimizedGraphBuilder::GenerateMathFloor(CallRuntime* call) {
12418 DCHECK(call->arguments()->length() == 1);
12419 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12420 HValue* value = Pop();
12421 HInstruction* result = NewUncasted<HUnaryMathOperation>(value, kMathFloor);
12422 return ast_context()->ReturnInstruction(result, call->id());
12426 void HOptimizedGraphBuilder::GenerateMathLogRT(CallRuntime* call) {
12427 DCHECK(call->arguments()->length() == 1);
12428 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12429 HValue* value = Pop();
12430 HInstruction* result = NewUncasted<HUnaryMathOperation>(value, kMathLog);
12431 return ast_context()->ReturnInstruction(result, call->id());
12435 void HOptimizedGraphBuilder::GenerateMathSqrt(CallRuntime* call) {
12436 DCHECK(call->arguments()->length() == 1);
12437 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12438 HValue* value = Pop();
12439 HInstruction* result = NewUncasted<HUnaryMathOperation>(value, kMathSqrt);
12440 return ast_context()->ReturnInstruction(result, call->id());
12444 void HOptimizedGraphBuilder::GenerateLikely(CallRuntime* call) {
12445 DCHECK(call->arguments()->length() == 1);
12446 Visit(call->arguments()->at(0));
12450 void HOptimizedGraphBuilder::GenerateUnlikely(CallRuntime* call) {
12451 return GenerateLikely(call);
12455 void HOptimizedGraphBuilder::GenerateFixedArrayGet(CallRuntime* call) {
12456 DCHECK(call->arguments()->length() == 2);
12457 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12458 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12459 HValue* index = Pop();
12460 HValue* object = Pop();
12461 HInstruction* result = New<HLoadKeyed>(
12462 object, index, nullptr, FAST_HOLEY_ELEMENTS, ALLOW_RETURN_HOLE);
12463 return ast_context()->ReturnInstruction(result, call->id());
12467 void HOptimizedGraphBuilder::GenerateFixedArraySet(CallRuntime* call) {
12468 DCHECK(call->arguments()->length() == 3);
12469 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12470 CHECK_ALIVE(VisitForValue(call->arguments()->at(1)));
12471 CHECK_ALIVE(VisitForValue(call->arguments()->at(2)));
12472 HValue* value = Pop();
12473 HValue* index = Pop();
12474 HValue* object = Pop();
12475 NoObservableSideEffectsScope no_effects(this);
12476 Add<HStoreKeyed>(object, index, value, FAST_HOLEY_ELEMENTS);
12477 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12481 void HOptimizedGraphBuilder::GenerateTheHole(CallRuntime* call) {
12482 DCHECK(call->arguments()->length() == 0);
12483 return ast_context()->ReturnValue(graph()->GetConstantHole());
12487 void HOptimizedGraphBuilder::GenerateJSCollectionGetTable(CallRuntime* call) {
12488 DCHECK(call->arguments()->length() == 1);
12489 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12490 HValue* receiver = Pop();
12491 HInstruction* result = New<HLoadNamedField>(
12492 receiver, nullptr, HObjectAccess::ForJSCollectionTable());
12493 return ast_context()->ReturnInstruction(result, call->id());
12497 void HOptimizedGraphBuilder::GenerateStringGetRawHashField(CallRuntime* call) {
12498 DCHECK(call->arguments()->length() == 1);
12499 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12500 HValue* object = Pop();
12501 HInstruction* result = New<HLoadNamedField>(
12502 object, nullptr, HObjectAccess::ForStringHashField());
12503 return ast_context()->ReturnInstruction(result, call->id());
12507 template <typename CollectionType>
12508 HValue* HOptimizedGraphBuilder::BuildAllocateOrderedHashTable() {
12509 static const int kCapacity = CollectionType::kMinCapacity;
12510 static const int kBucketCount = kCapacity / CollectionType::kLoadFactor;
12511 static const int kFixedArrayLength = CollectionType::kHashTableStartIndex +
12513 (kCapacity * CollectionType::kEntrySize);
12514 static const int kSizeInBytes =
12515 FixedArray::kHeaderSize + (kFixedArrayLength * kPointerSize);
12517 // Allocate the table and add the proper map.
12519 Add<HAllocate>(Add<HConstant>(kSizeInBytes), HType::HeapObject(),
12520 NOT_TENURED, FIXED_ARRAY_TYPE);
12521 AddStoreMapConstant(table, isolate()->factory()->ordered_hash_table_map());
12523 // Initialize the FixedArray...
12524 HValue* length = Add<HConstant>(kFixedArrayLength);
12525 Add<HStoreNamedField>(table, HObjectAccess::ForFixedArrayLength(), length);
12527 // ...and the OrderedHashTable fields.
12528 Add<HStoreNamedField>(
12530 HObjectAccess::ForOrderedHashTableNumberOfBuckets<CollectionType>(),
12531 Add<HConstant>(kBucketCount));
12532 Add<HStoreNamedField>(
12534 HObjectAccess::ForOrderedHashTableNumberOfElements<CollectionType>(),
12535 graph()->GetConstant0());
12536 Add<HStoreNamedField>(
12537 table, HObjectAccess::ForOrderedHashTableNumberOfDeletedElements<
12539 graph()->GetConstant0());
12541 // Fill the buckets with kNotFound.
12542 HValue* not_found = Add<HConstant>(CollectionType::kNotFound);
12543 for (int i = 0; i < kBucketCount; ++i) {
12544 Add<HStoreNamedField>(
12545 table, HObjectAccess::ForOrderedHashTableBucket<CollectionType>(i),
12549 // Fill the data table with undefined.
12550 HValue* undefined = graph()->GetConstantUndefined();
12551 for (int i = 0; i < (kCapacity * CollectionType::kEntrySize); ++i) {
12552 Add<HStoreNamedField>(table,
12553 HObjectAccess::ForOrderedHashTableDataTableIndex<
12554 CollectionType, kBucketCount>(i),
12562 void HOptimizedGraphBuilder::GenerateSetInitialize(CallRuntime* call) {
12563 DCHECK(call->arguments()->length() == 1);
12564 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12565 HValue* receiver = Pop();
12567 NoObservableSideEffectsScope no_effects(this);
12568 HValue* table = BuildAllocateOrderedHashTable<OrderedHashSet>();
12569 Add<HStoreNamedField>(receiver, HObjectAccess::ForJSCollectionTable(), table);
12570 return ast_context()->ReturnValue(receiver);
12574 void HOptimizedGraphBuilder::GenerateMapInitialize(CallRuntime* call) {
12575 DCHECK(call->arguments()->length() == 1);
12576 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12577 HValue* receiver = Pop();
12579 NoObservableSideEffectsScope no_effects(this);
12580 HValue* table = BuildAllocateOrderedHashTable<OrderedHashMap>();
12581 Add<HStoreNamedField>(receiver, HObjectAccess::ForJSCollectionTable(), table);
12582 return ast_context()->ReturnValue(receiver);
12586 template <typename CollectionType>
12587 void HOptimizedGraphBuilder::BuildOrderedHashTableClear(HValue* receiver) {
12588 HValue* old_table = Add<HLoadNamedField>(
12589 receiver, nullptr, HObjectAccess::ForJSCollectionTable());
12590 HValue* new_table = BuildAllocateOrderedHashTable<CollectionType>();
12591 Add<HStoreNamedField>(
12592 old_table, HObjectAccess::ForOrderedHashTableNextTable<CollectionType>(),
12594 Add<HStoreNamedField>(
12595 old_table, HObjectAccess::ForOrderedHashTableNumberOfDeletedElements<
12597 Add<HConstant>(CollectionType::kClearedTableSentinel));
12598 Add<HStoreNamedField>(receiver, HObjectAccess::ForJSCollectionTable(),
12603 void HOptimizedGraphBuilder::GenerateSetClear(CallRuntime* call) {
12604 DCHECK(call->arguments()->length() == 1);
12605 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12606 HValue* receiver = Pop();
12608 NoObservableSideEffectsScope no_effects(this);
12609 BuildOrderedHashTableClear<OrderedHashSet>(receiver);
12610 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12614 void HOptimizedGraphBuilder::GenerateMapClear(CallRuntime* call) {
12615 DCHECK(call->arguments()->length() == 1);
12616 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12617 HValue* receiver = Pop();
12619 NoObservableSideEffectsScope no_effects(this);
12620 BuildOrderedHashTableClear<OrderedHashMap>(receiver);
12621 return ast_context()->ReturnValue(graph()->GetConstantUndefined());
12625 void HOptimizedGraphBuilder::GenerateGetCachedArrayIndex(CallRuntime* call) {
12626 DCHECK(call->arguments()->length() == 1);
12627 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12628 HValue* value = Pop();
12629 HGetCachedArrayIndex* result = New<HGetCachedArrayIndex>(value);
12630 return ast_context()->ReturnInstruction(result, call->id());
12634 void HOptimizedGraphBuilder::GenerateFastOneByteArrayJoin(CallRuntime* call) {
12635 // Simply returning undefined here would be semantically correct and even
12636 // avoid the bailout. Nevertheless, some ancient benchmarks like SunSpider's
12637 // string-fasta would tank, because fullcode contains an optimized version.
12638 // Obviously the fullcode => Crankshaft => bailout => fullcode dance is
12639 // faster... *sigh*
12640 return Bailout(kInlinedRuntimeFunctionFastOneByteArrayJoin);
12644 void HOptimizedGraphBuilder::GenerateDebugBreakInOptimizedCode(
12645 CallRuntime* call) {
12646 Add<HDebugBreak>();
12647 return ast_context()->ReturnValue(graph()->GetConstant0());
12651 void HOptimizedGraphBuilder::GenerateDebugIsActive(CallRuntime* call) {
12652 DCHECK(call->arguments()->length() == 0);
12654 Add<HConstant>(ExternalReference::debug_is_active_address(isolate()));
12656 Add<HLoadNamedField>(ref, nullptr, HObjectAccess::ForExternalUInteger8());
12657 return ast_context()->ReturnValue(value);
12661 void HOptimizedGraphBuilder::GenerateGetPrototype(CallRuntime* call) {
12662 DCHECK(call->arguments()->length() == 1);
12663 CHECK_ALIVE(VisitForValue(call->arguments()->at(0)));
12664 HValue* object = Pop();
12666 NoObservableSideEffectsScope no_effects(this);
12668 HValue* map = Add<HLoadNamedField>(object, nullptr, HObjectAccess::ForMap());
12669 HValue* bit_field =
12670 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForMapBitField());
12671 HValue* is_access_check_needed_mask =
12672 Add<HConstant>(1 << Map::kIsAccessCheckNeeded);
12673 HValue* is_access_check_needed_test = AddUncasted<HBitwise>(
12674 Token::BIT_AND, bit_field, is_access_check_needed_mask);
12677 Add<HLoadNamedField>(map, nullptr, HObjectAccess::ForPrototype());
12678 HValue* proto_map =
12679 Add<HLoadNamedField>(proto, nullptr, HObjectAccess::ForMap());
12680 HValue* proto_bit_field =
12681 Add<HLoadNamedField>(proto_map, nullptr, HObjectAccess::ForMapBitField());
12682 HValue* is_hidden_prototype_mask =
12683 Add<HConstant>(1 << Map::kIsHiddenPrototype);
12684 HValue* is_hidden_prototype_test = AddUncasted<HBitwise>(
12685 Token::BIT_AND, proto_bit_field, is_hidden_prototype_mask);
12688 IfBuilder needs_runtime(this);
12689 needs_runtime.If<HCompareNumericAndBranch>(
12690 is_access_check_needed_test, graph()->GetConstant0(), Token::NE);
12691 needs_runtime.OrIf<HCompareNumericAndBranch>(
12692 is_hidden_prototype_test, graph()->GetConstant0(), Token::NE);
12694 needs_runtime.Then();
12696 Add<HPushArguments>(object);
12697 Push(Add<HCallRuntime>(
12698 call->name(), Runtime::FunctionForId(Runtime::kGetPrototype), 1));
12701 needs_runtime.Else();
12704 return ast_context()->ReturnValue(Pop());
12708 #undef CHECK_BAILOUT
12712 HEnvironment::HEnvironment(HEnvironment* outer,
12714 Handle<JSFunction> closure,
12716 : closure_(closure),
12718 frame_type_(JS_FUNCTION),
12719 parameter_count_(0),
12720 specials_count_(1),
12726 ast_id_(BailoutId::None()),
12728 Scope* declaration_scope = scope->DeclarationScope();
12729 Initialize(declaration_scope->num_parameters() + 1,
12730 declaration_scope->num_stack_slots(), 0);
12734 HEnvironment::HEnvironment(Zone* zone, int parameter_count)
12735 : values_(0, zone),
12737 parameter_count_(parameter_count),
12738 specials_count_(1),
12744 ast_id_(BailoutId::None()),
12746 Initialize(parameter_count, 0, 0);
12750 HEnvironment::HEnvironment(const HEnvironment* other, Zone* zone)
12751 : values_(0, zone),
12752 frame_type_(JS_FUNCTION),
12753 parameter_count_(0),
12754 specials_count_(0),
12760 ast_id_(other->ast_id()),
12766 HEnvironment::HEnvironment(HEnvironment* outer,
12767 Handle<JSFunction> closure,
12768 FrameType frame_type,
12771 : closure_(closure),
12772 values_(arguments, zone),
12773 frame_type_(frame_type),
12774 parameter_count_(arguments),
12775 specials_count_(0),
12781 ast_id_(BailoutId::None()),
12786 void HEnvironment::Initialize(int parameter_count,
12788 int stack_height) {
12789 parameter_count_ = parameter_count;
12790 local_count_ = local_count;
12792 // Avoid reallocating the temporaries' backing store on the first Push.
12793 int total = parameter_count + specials_count_ + local_count + stack_height;
12794 values_.Initialize(total + 4, zone());
12795 for (int i = 0; i < total; ++i) values_.Add(NULL, zone());
12799 void HEnvironment::Initialize(const HEnvironment* other) {
12800 closure_ = other->closure();
12801 values_.AddAll(other->values_, zone());
12802 assigned_variables_.Union(other->assigned_variables_, zone());
12803 frame_type_ = other->frame_type_;
12804 parameter_count_ = other->parameter_count_;
12805 local_count_ = other->local_count_;
12806 if (other->outer_ != NULL) outer_ = other->outer_->Copy(); // Deep copy.
12807 entry_ = other->entry_;
12808 pop_count_ = other->pop_count_;
12809 push_count_ = other->push_count_;
12810 specials_count_ = other->specials_count_;
12811 ast_id_ = other->ast_id_;
12815 void HEnvironment::AddIncomingEdge(HBasicBlock* block, HEnvironment* other) {
12816 DCHECK(!block->IsLoopHeader());
12817 DCHECK(values_.length() == other->values_.length());
12819 int length = values_.length();
12820 for (int i = 0; i < length; ++i) {
12821 HValue* value = values_[i];
12822 if (value != NULL && value->IsPhi() && value->block() == block) {
12823 // There is already a phi for the i'th value.
12824 HPhi* phi = HPhi::cast(value);
12825 // Assert index is correct and that we haven't missed an incoming edge.
12826 DCHECK(phi->merged_index() == i || !phi->HasMergedIndex());
12827 DCHECK(phi->OperandCount() == block->predecessors()->length());
12828 phi->AddInput(other->values_[i]);
12829 } else if (values_[i] != other->values_[i]) {
12830 // There is a fresh value on the incoming edge, a phi is needed.
12831 DCHECK(values_[i] != NULL && other->values_[i] != NULL);
12832 HPhi* phi = block->AddNewPhi(i);
12833 HValue* old_value = values_[i];
12834 for (int j = 0; j < block->predecessors()->length(); j++) {
12835 phi->AddInput(old_value);
12837 phi->AddInput(other->values_[i]);
12838 this->values_[i] = phi;
12844 void HEnvironment::Bind(int index, HValue* value) {
12845 DCHECK(value != NULL);
12846 assigned_variables_.Add(index, zone());
12847 values_[index] = value;
12851 bool HEnvironment::HasExpressionAt(int index) const {
12852 return index >= parameter_count_ + specials_count_ + local_count_;
12856 bool HEnvironment::ExpressionStackIsEmpty() const {
12857 DCHECK(length() >= first_expression_index());
12858 return length() == first_expression_index();
12862 void HEnvironment::SetExpressionStackAt(int index_from_top, HValue* value) {
12863 int count = index_from_top + 1;
12864 int index = values_.length() - count;
12865 DCHECK(HasExpressionAt(index));
12866 // The push count must include at least the element in question or else
12867 // the new value will not be included in this environment's history.
12868 if (push_count_ < count) {
12869 // This is the same effect as popping then re-pushing 'count' elements.
12870 pop_count_ += (count - push_count_);
12871 push_count_ = count;
12873 values_[index] = value;
12877 HValue* HEnvironment::RemoveExpressionStackAt(int index_from_top) {
12878 int count = index_from_top + 1;
12879 int index = values_.length() - count;
12880 DCHECK(HasExpressionAt(index));
12881 // Simulate popping 'count' elements and then
12882 // pushing 'count - 1' elements back.
12883 pop_count_ += Max(count - push_count_, 0);
12884 push_count_ = Max(push_count_ - count, 0) + (count - 1);
12885 return values_.Remove(index);
12889 void HEnvironment::Drop(int count) {
12890 for (int i = 0; i < count; ++i) {
12896 HEnvironment* HEnvironment::Copy() const {
12897 return new(zone()) HEnvironment(this, zone());
12901 HEnvironment* HEnvironment::CopyWithoutHistory() const {
12902 HEnvironment* result = Copy();
12903 result->ClearHistory();
12908 HEnvironment* HEnvironment::CopyAsLoopHeader(HBasicBlock* loop_header) const {
12909 HEnvironment* new_env = Copy();
12910 for (int i = 0; i < values_.length(); ++i) {
12911 HPhi* phi = loop_header->AddNewPhi(i);
12912 phi->AddInput(values_[i]);
12913 new_env->values_[i] = phi;
12915 new_env->ClearHistory();
12920 HEnvironment* HEnvironment::CreateStubEnvironment(HEnvironment* outer,
12921 Handle<JSFunction> target,
12922 FrameType frame_type,
12923 int arguments) const {
12924 HEnvironment* new_env =
12925 new(zone()) HEnvironment(outer, target, frame_type,
12926 arguments + 1, zone());
12927 for (int i = 0; i <= arguments; ++i) { // Include receiver.
12928 new_env->Push(ExpressionStackAt(arguments - i));
12930 new_env->ClearHistory();
12935 HEnvironment* HEnvironment::CopyForInlining(
12936 Handle<JSFunction> target,
12938 FunctionLiteral* function,
12939 HConstant* undefined,
12940 InliningKind inlining_kind) const {
12941 DCHECK(frame_type() == JS_FUNCTION);
12943 // Outer environment is a copy of this one without the arguments.
12944 int arity = function->scope()->num_parameters();
12946 HEnvironment* outer = Copy();
12947 outer->Drop(arguments + 1); // Including receiver.
12948 outer->ClearHistory();
12950 if (inlining_kind == CONSTRUCT_CALL_RETURN) {
12951 // Create artificial constructor stub environment. The receiver should
12952 // actually be the constructor function, but we pass the newly allocated
12953 // object instead, DoComputeConstructStubFrame() relies on that.
12954 outer = CreateStubEnvironment(outer, target, JS_CONSTRUCT, arguments);
12955 } else if (inlining_kind == GETTER_CALL_RETURN) {
12956 // We need an additional StackFrame::INTERNAL frame for restoring the
12957 // correct context.
12958 outer = CreateStubEnvironment(outer, target, JS_GETTER, arguments);
12959 } else if (inlining_kind == SETTER_CALL_RETURN) {
12960 // We need an additional StackFrame::INTERNAL frame for temporarily saving
12961 // the argument of the setter, see StoreStubCompiler::CompileStoreViaSetter.
12962 outer = CreateStubEnvironment(outer, target, JS_SETTER, arguments);
12965 if (arity != arguments) {
12966 // Create artificial arguments adaptation environment.
12967 outer = CreateStubEnvironment(outer, target, ARGUMENTS_ADAPTOR, arguments);
12970 HEnvironment* inner =
12971 new(zone()) HEnvironment(outer, function->scope(), target, zone());
12972 // Get the argument values from the original environment.
12973 for (int i = 0; i <= arity; ++i) { // Include receiver.
12974 HValue* push = (i <= arguments) ?
12975 ExpressionStackAt(arguments - i) : undefined;
12976 inner->SetValueAt(i, push);
12978 inner->SetValueAt(arity + 1, context());
12979 for (int i = arity + 2; i < inner->length(); ++i) {
12980 inner->SetValueAt(i, undefined);
12983 inner->set_ast_id(BailoutId::FunctionEntry());
12988 std::ostream& operator<<(std::ostream& os, const HEnvironment& env) {
12989 for (int i = 0; i < env.length(); i++) {
12990 if (i == 0) os << "parameters\n";
12991 if (i == env.parameter_count()) os << "specials\n";
12992 if (i == env.parameter_count() + env.specials_count()) os << "locals\n";
12993 if (i == env.parameter_count() + env.specials_count() + env.local_count()) {
12994 os << "expressions\n";
12996 HValue* val = env.values()->at(i);
13009 void HTracer::TraceCompilation(CompilationInfo* info) {
13010 Tag tag(this, "compilation");
13011 if (info->IsOptimizing()) {
13012 Handle<String> name = info->function()->debug_name();
13013 PrintStringProperty("name", name->ToCString().get());
13015 trace_.Add("method \"%s:%d\"\n",
13016 name->ToCString().get(),
13017 info->optimization_id());
13019 CodeStub::Major major_key = info->code_stub()->MajorKey();
13020 PrintStringProperty("name", CodeStub::MajorName(major_key, false));
13021 PrintStringProperty("method", "stub");
13023 PrintLongProperty("date",
13024 static_cast<int64_t>(base::OS::TimeCurrentMillis()));
13028 void HTracer::TraceLithium(const char* name, LChunk* chunk) {
13029 DCHECK(!chunk->isolate()->concurrent_recompilation_enabled());
13030 AllowHandleDereference allow_deref;
13031 AllowDeferredHandleDereference allow_deferred_deref;
13032 Trace(name, chunk->graph(), chunk);
13036 void HTracer::TraceHydrogen(const char* name, HGraph* graph) {
13037 DCHECK(!graph->isolate()->concurrent_recompilation_enabled());
13038 AllowHandleDereference allow_deref;
13039 AllowDeferredHandleDereference allow_deferred_deref;
13040 Trace(name, graph, NULL);
13044 void HTracer::Trace(const char* name, HGraph* graph, LChunk* chunk) {
13045 Tag tag(this, "cfg");
13046 PrintStringProperty("name", name);
13047 const ZoneList<HBasicBlock*>* blocks = graph->blocks();
13048 for (int i = 0; i < blocks->length(); i++) {
13049 HBasicBlock* current = blocks->at(i);
13050 Tag block_tag(this, "block");
13051 PrintBlockProperty("name", current->block_id());
13052 PrintIntProperty("from_bci", -1);
13053 PrintIntProperty("to_bci", -1);
13055 if (!current->predecessors()->is_empty()) {
13057 trace_.Add("predecessors");
13058 for (int j = 0; j < current->predecessors()->length(); ++j) {
13059 trace_.Add(" \"B%d\"", current->predecessors()->at(j)->block_id());
13063 PrintEmptyProperty("predecessors");
13066 if (current->end()->SuccessorCount() == 0) {
13067 PrintEmptyProperty("successors");
13070 trace_.Add("successors");
13071 for (HSuccessorIterator it(current->end()); !it.Done(); it.Advance()) {
13072 trace_.Add(" \"B%d\"", it.Current()->block_id());
13077 PrintEmptyProperty("xhandlers");
13081 trace_.Add("flags");
13082 if (current->IsLoopSuccessorDominator()) {
13083 trace_.Add(" \"dom-loop-succ\"");
13085 if (current->IsUnreachable()) {
13086 trace_.Add(" \"dead\"");
13088 if (current->is_osr_entry()) {
13089 trace_.Add(" \"osr\"");
13094 if (current->dominator() != NULL) {
13095 PrintBlockProperty("dominator", current->dominator()->block_id());
13098 PrintIntProperty("loop_depth", current->LoopNestingDepth());
13100 if (chunk != NULL) {
13101 int first_index = current->first_instruction_index();
13102 int last_index = current->last_instruction_index();
13105 LifetimePosition::FromInstructionIndex(first_index).Value());
13108 LifetimePosition::FromInstructionIndex(last_index).Value());
13112 Tag states_tag(this, "states");
13113 Tag locals_tag(this, "locals");
13114 int total = current->phis()->length();
13115 PrintIntProperty("size", current->phis()->length());
13116 PrintStringProperty("method", "None");
13117 for (int j = 0; j < total; ++j) {
13118 HPhi* phi = current->phis()->at(j);
13120 std::ostringstream os;
13121 os << phi->merged_index() << " " << NameOf(phi) << " " << *phi << "\n";
13122 trace_.Add(os.str().c_str());
13127 Tag HIR_tag(this, "HIR");
13128 for (HInstructionIterator it(current); !it.Done(); it.Advance()) {
13129 HInstruction* instruction = it.Current();
13130 int uses = instruction->UseCount();
13132 std::ostringstream os;
13133 os << "0 " << uses << " " << NameOf(instruction) << " " << *instruction;
13134 if (graph->info()->is_tracking_positions() &&
13135 instruction->has_position() && instruction->position().raw() != 0) {
13136 const SourcePosition pos = instruction->position();
13138 if (pos.inlining_id() != 0) os << pos.inlining_id() << "_";
13139 os << pos.position();
13142 trace_.Add(os.str().c_str());
13147 if (chunk != NULL) {
13148 Tag LIR_tag(this, "LIR");
13149 int first_index = current->first_instruction_index();
13150 int last_index = current->last_instruction_index();
13151 if (first_index != -1 && last_index != -1) {
13152 const ZoneList<LInstruction*>* instructions = chunk->instructions();
13153 for (int i = first_index; i <= last_index; ++i) {
13154 LInstruction* linstr = instructions->at(i);
13155 if (linstr != NULL) {
13158 LifetimePosition::FromInstructionIndex(i).Value());
13159 linstr->PrintTo(&trace_);
13160 std::ostringstream os;
13161 os << " [hir:" << NameOf(linstr->hydrogen_value()) << "] <|@\n";
13162 trace_.Add(os.str().c_str());
13171 void HTracer::TraceLiveRanges(const char* name, LAllocator* allocator) {
13172 Tag tag(this, "intervals");
13173 PrintStringProperty("name", name);
13175 const Vector<LiveRange*>* fixed_d = allocator->fixed_double_live_ranges();
13176 for (int i = 0; i < fixed_d->length(); ++i) {
13177 TraceLiveRange(fixed_d->at(i), "fixed", allocator->zone());
13180 const Vector<LiveRange*>* fixed = allocator->fixed_live_ranges();
13181 for (int i = 0; i < fixed->length(); ++i) {
13182 TraceLiveRange(fixed->at(i), "fixed", allocator->zone());
13185 const ZoneList<LiveRange*>* live_ranges = allocator->live_ranges();
13186 for (int i = 0; i < live_ranges->length(); ++i) {
13187 TraceLiveRange(live_ranges->at(i), "object", allocator->zone());
13192 void HTracer::TraceLiveRange(LiveRange* range, const char* type,
13194 if (range != NULL && !range->IsEmpty()) {
13196 trace_.Add("%d %s", range->id(), type);
13197 if (range->HasRegisterAssigned()) {
13198 LOperand* op = range->CreateAssignedOperand(zone);
13199 int assigned_reg = op->index();
13200 if (op->IsDoubleRegister()) {
13201 trace_.Add(" \"%s\"",
13202 DoubleRegister::AllocationIndexToString(assigned_reg));
13204 DCHECK(op->IsRegister());
13205 trace_.Add(" \"%s\"", Register::AllocationIndexToString(assigned_reg));
13207 } else if (range->IsSpilled()) {
13208 LOperand* op = range->TopLevel()->GetSpillOperand();
13209 if (op->IsDoubleStackSlot()) {
13210 trace_.Add(" \"double_stack:%d\"", op->index());
13212 DCHECK(op->IsStackSlot());
13213 trace_.Add(" \"stack:%d\"", op->index());
13216 int parent_index = -1;
13217 if (range->IsChild()) {
13218 parent_index = range->parent()->id();
13220 parent_index = range->id();
13222 LOperand* op = range->FirstHint();
13223 int hint_index = -1;
13224 if (op != NULL && op->IsUnallocated()) {
13225 hint_index = LUnallocated::cast(op)->virtual_register();
13227 trace_.Add(" %d %d", parent_index, hint_index);
13228 UseInterval* cur_interval = range->first_interval();
13229 while (cur_interval != NULL && range->Covers(cur_interval->start())) {
13230 trace_.Add(" [%d, %d[",
13231 cur_interval->start().Value(),
13232 cur_interval->end().Value());
13233 cur_interval = cur_interval->next();
13236 UsePosition* current_pos = range->first_pos();
13237 while (current_pos != NULL) {
13238 if (current_pos->RegisterIsBeneficial() || FLAG_trace_all_uses) {
13239 trace_.Add(" %d M", current_pos->pos().Value());
13241 current_pos = current_pos->next();
13244 trace_.Add(" \"\"\n");
13249 void HTracer::FlushToFile() {
13250 AppendChars(filename_.start(), trace_.ToCString().get(), trace_.length(),
13256 void HStatistics::Initialize(CompilationInfo* info) {
13257 if (info->shared_info().is_null()) return;
13258 source_size_ += info->shared_info()->SourceSize();
13262 void HStatistics::Print() {
13265 "----------------------------------------"
13266 "----------------------------------------\n"
13267 "--- Hydrogen timing results:\n"
13268 "----------------------------------------"
13269 "----------------------------------------\n");
13270 base::TimeDelta sum;
13271 for (int i = 0; i < times_.length(); ++i) {
13275 for (int i = 0; i < names_.length(); ++i) {
13276 PrintF("%33s", names_[i]);
13277 double ms = times_[i].InMillisecondsF();
13278 double percent = times_[i].PercentOf(sum);
13279 PrintF(" %8.3f ms / %4.1f %% ", ms, percent);
13281 size_t size = sizes_[i];
13282 double size_percent = static_cast<double>(size) * 100 / total_size_;
13283 PrintF(" %9zu bytes / %4.1f %%\n", size, size_percent);
13287 "----------------------------------------"
13288 "----------------------------------------\n");
13289 base::TimeDelta total = create_graph_ + optimize_graph_ + generate_code_;
13290 PrintF("%33s %8.3f ms / %4.1f %% \n", "Create graph",
13291 create_graph_.InMillisecondsF(), create_graph_.PercentOf(total));
13292 PrintF("%33s %8.3f ms / %4.1f %% \n", "Optimize graph",
13293 optimize_graph_.InMillisecondsF(), optimize_graph_.PercentOf(total));
13294 PrintF("%33s %8.3f ms / %4.1f %% \n", "Generate and install code",
13295 generate_code_.InMillisecondsF(), generate_code_.PercentOf(total));
13297 "----------------------------------------"
13298 "----------------------------------------\n");
13299 PrintF("%33s %8.3f ms %9zu bytes\n", "Total",
13300 total.InMillisecondsF(), total_size_);
13301 PrintF("%33s (%.1f times slower than full code gen)\n", "",
13302 total.TimesOf(full_code_gen_));
13304 double source_size_in_kb = static_cast<double>(source_size_) / 1024;
13305 double normalized_time = source_size_in_kb > 0
13306 ? total.InMillisecondsF() / source_size_in_kb
13308 double normalized_size_in_kb =
13309 source_size_in_kb > 0
13310 ? static_cast<double>(total_size_) / 1024 / source_size_in_kb
13312 PrintF("%33s %8.3f ms %7.3f kB allocated\n",
13313 "Average per kB source", normalized_time, normalized_size_in_kb);
13317 void HStatistics::SaveTiming(const char* name, base::TimeDelta time,
13319 total_size_ += size;
13320 for (int i = 0; i < names_.length(); ++i) {
13321 if (strcmp(names_[i], name) == 0) {
13333 HPhase::~HPhase() {
13334 if (ShouldProduceTraceOutput()) {
13335 isolate()->GetHTracer()->TraceHydrogen(name(), graph_);
13339 graph_->Verify(false); // No full verify.
13343 } // namespace internal