1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 #include "src/accessors.h"
11 #include "src/allocation.h"
13 #include "src/bailout-reason.h"
14 #include "src/compiler.h"
15 #include "src/hydrogen-instructions.h"
16 #include "src/scopes.h"
22 // Forward declarations.
27 class HLoopInformation;
35 class HBasicBlock final : public ZoneObject {
37 explicit HBasicBlock(HGraph* graph);
41 int block_id() const { return block_id_; }
42 void set_block_id(int id) { block_id_ = id; }
43 HGraph* graph() const { return graph_; }
44 Isolate* isolate() const;
45 const ZoneList<HPhi*>* phis() const { return &phis_; }
46 HInstruction* first() const { return first_; }
47 HInstruction* last() const { return last_; }
48 void set_last(HInstruction* instr) { last_ = instr; }
49 HControlInstruction* end() const { return end_; }
50 HLoopInformation* loop_information() const { return loop_information_; }
51 HLoopInformation* current_loop() const {
52 return IsLoopHeader() ? loop_information()
53 : (parent_loop_header() != NULL
54 ? parent_loop_header()->loop_information() : NULL);
56 const ZoneList<HBasicBlock*>* predecessors() const { return &predecessors_; }
57 bool HasPredecessor() const { return predecessors_.length() > 0; }
58 const ZoneList<HBasicBlock*>* dominated_blocks() const {
59 return &dominated_blocks_;
61 const ZoneList<int>* deleted_phis() const {
62 return &deleted_phis_;
64 void RecordDeletedPhi(int merge_index) {
65 deleted_phis_.Add(merge_index, zone());
67 HBasicBlock* dominator() const { return dominator_; }
68 HEnvironment* last_environment() const { return last_environment_; }
69 int argument_count() const { return argument_count_; }
70 void set_argument_count(int count) { argument_count_ = count; }
71 int first_instruction_index() const { return first_instruction_index_; }
72 void set_first_instruction_index(int index) {
73 first_instruction_index_ = index;
75 int last_instruction_index() const { return last_instruction_index_; }
76 void set_last_instruction_index(int index) {
77 last_instruction_index_ = index;
79 bool is_osr_entry() { return is_osr_entry_; }
80 void set_osr_entry() { is_osr_entry_ = true; }
82 void AttachLoopInformation();
83 void DetachLoopInformation();
84 bool IsLoopHeader() const { return loop_information() != NULL; }
85 bool IsStartBlock() const { return block_id() == 0; }
86 void PostProcessLoopHeader(IterationStatement* stmt);
88 bool IsFinished() const { return end_ != NULL; }
89 void AddPhi(HPhi* phi);
90 void RemovePhi(HPhi* phi);
91 void AddInstruction(HInstruction* instr, SourcePosition position);
92 bool Dominates(HBasicBlock* other) const;
93 bool EqualToOrDominates(HBasicBlock* other) const;
94 int LoopNestingDepth() const;
96 void SetInitialEnvironment(HEnvironment* env);
97 void ClearEnvironment() {
99 DCHECK(end()->SuccessorCount() == 0);
100 last_environment_ = NULL;
102 bool HasEnvironment() const { return last_environment_ != NULL; }
103 void UpdateEnvironment(HEnvironment* env);
104 HBasicBlock* parent_loop_header() const { return parent_loop_header_; }
106 void set_parent_loop_header(HBasicBlock* block) {
107 DCHECK(parent_loop_header_ == NULL);
108 parent_loop_header_ = block;
111 bool HasParentLoopHeader() const { return parent_loop_header_ != NULL; }
113 void SetJoinId(BailoutId ast_id);
115 int PredecessorIndexOf(HBasicBlock* predecessor) const;
116 HPhi* AddNewPhi(int merged_index);
117 HSimulate* AddNewSimulate(BailoutId ast_id, SourcePosition position,
118 RemovableSimulate removable = FIXED_SIMULATE) {
119 HSimulate* instr = CreateSimulate(ast_id, removable);
120 AddInstruction(instr, position);
123 void AssignCommonDominator(HBasicBlock* other);
124 void AssignLoopSuccessorDominators();
126 // If a target block is tagged as an inline function return, all
127 // predecessors should contain the inlined exit sequence:
130 // Simulate (caller's environment)
131 // Goto (target block)
132 bool IsInlineReturnTarget() const { return is_inline_return_target_; }
133 void MarkAsInlineReturnTarget(HBasicBlock* inlined_entry_block) {
134 is_inline_return_target_ = true;
135 inlined_entry_block_ = inlined_entry_block;
137 HBasicBlock* inlined_entry_block() { return inlined_entry_block_; }
139 bool IsDeoptimizing() const {
140 return end() != NULL && end()->IsDeoptimize();
143 void MarkUnreachable();
144 bool IsUnreachable() const { return !is_reachable_; }
145 bool IsReachable() const { return is_reachable_; }
147 bool IsLoopSuccessorDominator() const {
148 return dominates_loop_successors_;
150 void MarkAsLoopSuccessorDominator() {
151 dominates_loop_successors_ = true;
154 bool IsOrdered() const { return is_ordered_; }
155 void MarkAsOrdered() { is_ordered_ = true; }
157 void MarkSuccEdgeUnreachable(int succ);
159 inline Zone* zone() const;
166 friend class HGraphBuilder;
168 HSimulate* CreateSimulate(BailoutId ast_id, RemovableSimulate removable);
169 void Finish(HControlInstruction* last, SourcePosition position);
170 void FinishExit(HControlInstruction* instruction, SourcePosition position);
171 void Goto(HBasicBlock* block, SourcePosition position,
172 FunctionState* state = NULL, bool add_simulate = true);
173 void GotoNoSimulate(HBasicBlock* block, SourcePosition position) {
174 Goto(block, position, NULL, false);
177 // Add the inlined function exit sequence, adding an HLeaveInlined
178 // instruction and updating the bailout environment.
179 void AddLeaveInlined(HValue* return_value, FunctionState* state,
180 SourcePosition position);
183 void RegisterPredecessor(HBasicBlock* pred);
184 void AddDominatedBlock(HBasicBlock* block);
188 ZoneList<HPhi*> phis_;
189 HInstruction* first_;
191 HControlInstruction* end_;
192 HLoopInformation* loop_information_;
193 ZoneList<HBasicBlock*> predecessors_;
194 HBasicBlock* dominator_;
195 ZoneList<HBasicBlock*> dominated_blocks_;
196 HEnvironment* last_environment_;
197 // Outgoing parameter count at block exit, set during lithium translation.
199 // Instruction indices into the lithium code stream.
200 int first_instruction_index_;
201 int last_instruction_index_;
202 ZoneList<int> deleted_phis_;
203 HBasicBlock* parent_loop_header_;
204 // For blocks marked as inline return target: the block with HEnterInlined.
205 HBasicBlock* inlined_entry_block_;
206 bool is_inline_return_target_ : 1;
207 bool is_reachable_ : 1;
208 bool dominates_loop_successors_ : 1;
209 bool is_osr_entry_ : 1;
210 bool is_ordered_ : 1;
214 std::ostream& operator<<(std::ostream& os, const HBasicBlock& b);
217 class HPredecessorIterator final BASE_EMBEDDED {
219 explicit HPredecessorIterator(HBasicBlock* block)
220 : predecessor_list_(block->predecessors()), current_(0) { }
222 bool Done() { return current_ >= predecessor_list_->length(); }
223 HBasicBlock* Current() { return predecessor_list_->at(current_); }
224 void Advance() { current_++; }
227 const ZoneList<HBasicBlock*>* predecessor_list_;
232 class HInstructionIterator final BASE_EMBEDDED {
234 explicit HInstructionIterator(HBasicBlock* block)
235 : instr_(block->first()) {
236 next_ = Done() ? NULL : instr_->next();
239 inline bool Done() const { return instr_ == NULL; }
240 inline HInstruction* Current() { return instr_; }
241 inline void Advance() {
243 next_ = Done() ? NULL : instr_->next();
247 HInstruction* instr_;
252 class HLoopInformation final : public ZoneObject {
254 HLoopInformation(HBasicBlock* loop_header, Zone* zone)
255 : back_edges_(4, zone),
256 loop_header_(loop_header),
259 blocks_.Add(loop_header, zone);
261 ~HLoopInformation() {}
263 const ZoneList<HBasicBlock*>* back_edges() const { return &back_edges_; }
264 const ZoneList<HBasicBlock*>* blocks() const { return &blocks_; }
265 HBasicBlock* loop_header() const { return loop_header_; }
266 HBasicBlock* GetLastBackEdge() const;
267 void RegisterBackEdge(HBasicBlock* block);
269 HStackCheck* stack_check() const { return stack_check_; }
270 void set_stack_check(HStackCheck* stack_check) {
271 stack_check_ = stack_check;
274 bool IsNestedInThisLoop(HLoopInformation* other) {
275 while (other != NULL) {
279 other = other->parent_loop();
283 HLoopInformation* parent_loop() {
284 HBasicBlock* parent_header = loop_header()->parent_loop_header();
285 return parent_header != NULL ? parent_header->loop_information() : NULL;
289 void AddBlock(HBasicBlock* block);
291 ZoneList<HBasicBlock*> back_edges_;
292 HBasicBlock* loop_header_;
293 ZoneList<HBasicBlock*> blocks_;
294 HStackCheck* stack_check_;
298 class BoundsCheckTable;
299 class InductionVariableBlocksTable;
300 class HGraph final : public ZoneObject {
302 explicit HGraph(CompilationInfo* info);
304 Isolate* isolate() const { return isolate_; }
305 Zone* zone() const { return zone_; }
306 CompilationInfo* info() const { return info_; }
308 const ZoneList<HBasicBlock*>* blocks() const { return &blocks_; }
309 const ZoneList<HPhi*>* phi_list() const { return phi_list_; }
310 HBasicBlock* entry_block() const { return entry_block_; }
311 HEnvironment* start_environment() const { return start_environment_; }
313 void FinalizeUniqueness();
315 void AssignDominators();
316 void RestoreActualValues();
318 // Returns false if there are phi-uses of the arguments-object
319 // which are not supported by the optimizing compiler.
320 bool CheckArgumentsPhiUses();
322 // Returns false if there are phi-uses of an uninitialized const
323 // which are not supported by the optimizing compiler.
324 bool CheckConstPhiUses();
328 HConstant* GetConstantUndefined();
329 HConstant* GetConstant0();
330 HConstant* GetConstant1();
331 HConstant* GetConstantMinus1();
332 HConstant* GetConstantTrue();
333 HConstant* GetConstantFalse();
334 HConstant* GetConstantHole();
335 HConstant* GetConstantNull();
336 HConstant* GetInvalidContext();
338 bool IsConstantUndefined(HConstant* constant);
339 bool IsConstant0(HConstant* constant);
340 bool IsConstant1(HConstant* constant);
341 bool IsConstantMinus1(HConstant* constant);
342 bool IsConstantTrue(HConstant* constant);
343 bool IsConstantFalse(HConstant* constant);
344 bool IsConstantHole(HConstant* constant);
345 bool IsConstantNull(HConstant* constant);
346 bool IsStandardConstant(HConstant* constant);
348 HBasicBlock* CreateBasicBlock();
349 HArgumentsObject* GetArgumentsObject() const {
350 return arguments_object_.get();
353 void SetArgumentsObject(HArgumentsObject* object) {
354 arguments_object_.set(object);
357 int GetMaximumValueID() const { return values_.length(); }
358 int GetNextBlockID() { return next_block_id_++; }
359 int GetNextValueID(HValue* value) {
360 DCHECK(!disallow_adding_new_values_);
361 values_.Add(value, zone());
362 return values_.length() - 1;
364 HValue* LookupValue(int id) const {
365 if (id >= 0 && id < values_.length()) return values_[id];
368 void DisallowAddingNewValues() {
369 disallow_adding_new_values_ = true;
372 bool Optimize(BailoutReason* bailout_reason);
375 void Verify(bool do_full_verify) const;
382 void set_osr(HOsrBuilder* osr) {
390 int update_type_change_checksum(int delta) {
391 type_change_checksum_ += delta;
392 return type_change_checksum_;
395 void update_maximum_environment_size(int environment_size) {
396 if (environment_size > maximum_environment_size_) {
397 maximum_environment_size_ = environment_size;
400 int maximum_environment_size() { return maximum_environment_size_; }
402 bool use_optimistic_licm() {
403 return use_optimistic_licm_;
406 void set_use_optimistic_licm(bool value) {
407 use_optimistic_licm_ = value;
410 void MarkRecursive() { is_recursive_ = true; }
411 bool is_recursive() const { return is_recursive_; }
413 void MarkDependsOnEmptyArrayProtoElements() {
414 // Add map dependency if not already added.
415 if (depends_on_empty_array_proto_elements_) return;
416 info()->dependencies()->AssumePropertyCell(
417 isolate()->factory()->array_protector());
418 depends_on_empty_array_proto_elements_ = true;
421 bool depends_on_empty_array_proto_elements() {
422 return depends_on_empty_array_proto_elements_;
425 bool has_uint32_instructions() {
426 DCHECK(uint32_instructions_ == NULL || !uint32_instructions_->is_empty());
427 return uint32_instructions_ != NULL;
430 ZoneList<HInstruction*>* uint32_instructions() {
431 DCHECK(uint32_instructions_ == NULL || !uint32_instructions_->is_empty());
432 return uint32_instructions_;
435 void RecordUint32Instruction(HInstruction* instr) {
436 DCHECK(uint32_instructions_ == NULL || !uint32_instructions_->is_empty());
437 if (uint32_instructions_ == NULL) {
438 uint32_instructions_ = new(zone()) ZoneList<HInstruction*>(4, zone());
440 uint32_instructions_->Add(instr, zone());
443 void IncrementInNoSideEffectsScope() { no_side_effects_scope_count_++; }
444 void DecrementInNoSideEffectsScope() { no_side_effects_scope_count_--; }
445 bool IsInsideNoSideEffectsScope() { return no_side_effects_scope_count_ > 0; }
447 // If we are tracking source positions then this function assigns a unique
448 // identifier to each inlining and dumps function source if it was inlined
449 // for the first time during the current optimization.
450 int TraceInlinedFunction(Handle<SharedFunctionInfo> shared,
451 SourcePosition position);
453 // Converts given SourcePosition to the absolute offset from the start of
454 // the corresponding script.
455 int SourcePositionToScriptPosition(SourcePosition position);
458 HConstant* ReinsertConstantIfNecessary(HConstant* constant);
459 HConstant* GetConstant(SetOncePointer<HConstant>* pointer,
460 int32_t integer_value);
462 template<class Phase>
470 HBasicBlock* entry_block_;
471 HEnvironment* start_environment_;
472 ZoneList<HBasicBlock*> blocks_;
473 ZoneList<HValue*> values_;
474 ZoneList<HPhi*>* phi_list_;
475 ZoneList<HInstruction*>* uint32_instructions_;
476 SetOncePointer<HConstant> constant_undefined_;
477 SetOncePointer<HConstant> constant_0_;
478 SetOncePointer<HConstant> constant_1_;
479 SetOncePointer<HConstant> constant_minus1_;
480 SetOncePointer<HConstant> constant_true_;
481 SetOncePointer<HConstant> constant_false_;
482 SetOncePointer<HConstant> constant_the_hole_;
483 SetOncePointer<HConstant> constant_null_;
484 SetOncePointer<HConstant> constant_invalid_context_;
485 SetOncePointer<HArgumentsObject> arguments_object_;
489 CompilationInfo* info_;
493 bool use_optimistic_licm_;
494 bool depends_on_empty_array_proto_elements_;
495 int type_change_checksum_;
496 int maximum_environment_size_;
497 int no_side_effects_scope_count_;
498 bool disallow_adding_new_values_;
500 DISALLOW_COPY_AND_ASSIGN(HGraph);
504 Zone* HBasicBlock::zone() const { return graph_->zone(); }
507 // Type of stack frame an environment might refer to.
518 class HEnvironment final : public ZoneObject {
520 HEnvironment(HEnvironment* outer,
522 Handle<JSFunction> closure,
525 HEnvironment(Zone* zone, int parameter_count);
527 HEnvironment* arguments_environment() {
528 return outer()->frame_type() == ARGUMENTS_ADAPTOR ? outer() : this;
532 Handle<JSFunction> closure() const { return closure_; }
533 const ZoneList<HValue*>* values() const { return &values_; }
534 const GrowableBitVector* assigned_variables() const {
535 return &assigned_variables_;
537 FrameType frame_type() const { return frame_type_; }
538 int parameter_count() const { return parameter_count_; }
539 int specials_count() const { return specials_count_; }
540 int local_count() const { return local_count_; }
541 HEnvironment* outer() const { return outer_; }
542 int pop_count() const { return pop_count_; }
543 int push_count() const { return push_count_; }
545 BailoutId ast_id() const { return ast_id_; }
546 void set_ast_id(BailoutId id) { ast_id_ = id; }
548 HEnterInlined* entry() const { return entry_; }
549 void set_entry(HEnterInlined* entry) { entry_ = entry; }
551 int length() const { return values_.length(); }
553 int first_expression_index() const {
554 return parameter_count() + specials_count() + local_count();
557 int first_local_index() const {
558 return parameter_count() + specials_count();
561 void Bind(Variable* variable, HValue* value) {
562 Bind(IndexFor(variable), value);
565 void Bind(int index, HValue* value);
567 void BindContext(HValue* value) {
568 Bind(parameter_count(), value);
571 HValue* Lookup(Variable* variable) const {
572 return Lookup(IndexFor(variable));
575 HValue* Lookup(int index) const {
576 HValue* result = values_[index];
577 DCHECK(result != NULL);
581 HValue* context() const {
582 // Return first special.
583 return Lookup(parameter_count());
586 void Push(HValue* value) {
587 DCHECK(value != NULL);
589 values_.Add(value, zone());
593 DCHECK(!ExpressionStackIsEmpty());
594 if (push_count_ > 0) {
599 return values_.RemoveLast();
602 void Drop(int count);
604 HValue* Top() const { return ExpressionStackAt(0); }
606 bool ExpressionStackIsEmpty() const;
608 HValue* ExpressionStackAt(int index_from_top) const {
609 int index = length() - index_from_top - 1;
610 DCHECK(HasExpressionAt(index));
611 return values_[index];
614 void SetExpressionStackAt(int index_from_top, HValue* value);
615 HValue* RemoveExpressionStackAt(int index_from_top);
617 HEnvironment* Copy() const;
618 HEnvironment* CopyWithoutHistory() const;
619 HEnvironment* CopyAsLoopHeader(HBasicBlock* block) const;
621 // Create an "inlined version" of this environment, where the original
622 // environment is the outer environment but the top expression stack
623 // elements are moved to an inner environment as parameters.
624 HEnvironment* CopyForInlining(Handle<JSFunction> target,
626 FunctionLiteral* function,
627 HConstant* undefined,
628 InliningKind inlining_kind) const;
630 HEnvironment* DiscardInlined(bool drop_extra) {
631 HEnvironment* outer = outer_;
632 while (outer->frame_type() != JS_FUNCTION) outer = outer->outer_;
633 if (drop_extra) outer->Drop(1);
637 void AddIncomingEdge(HBasicBlock* block, HEnvironment* other);
639 void ClearHistory() {
642 assigned_variables_.Clear();
645 void SetValueAt(int index, HValue* value) {
646 DCHECK(index < length());
647 values_[index] = value;
650 // Map a variable to an environment index. Parameter indices are shifted
651 // by 1 (receiver is parameter index -1 but environment index 0).
652 // Stack-allocated local indices are shifted by the number of parameters.
653 int IndexFor(Variable* variable) const {
654 DCHECK(variable->IsStackAllocated());
655 int shift = variable->IsParameter()
657 : parameter_count_ + specials_count_;
658 return variable->index() + shift;
661 bool is_local_index(int i) const {
662 return i >= first_local_index() && i < first_expression_index();
665 bool is_parameter_index(int i) const {
666 return i >= 0 && i < parameter_count();
669 bool is_special_index(int i) const {
670 return i >= parameter_count() && i < parameter_count() + specials_count();
673 Zone* zone() const { return zone_; }
676 HEnvironment(const HEnvironment* other, Zone* zone);
678 HEnvironment(HEnvironment* outer,
679 Handle<JSFunction> closure,
680 FrameType frame_type,
684 // Create an artificial stub environment (e.g. for argument adaptor or
685 // constructor stub).
686 HEnvironment* CreateStubEnvironment(HEnvironment* outer,
687 Handle<JSFunction> target,
688 FrameType frame_type,
689 int arguments) const;
691 // True if index is included in the expression stack part of the environment.
692 bool HasExpressionAt(int index) const;
694 void Initialize(int parameter_count, int local_count, int stack_height);
695 void Initialize(const HEnvironment* other);
697 Handle<JSFunction> closure_;
698 // Value array [parameters] [specials] [locals] [temporaries].
699 ZoneList<HValue*> values_;
700 GrowableBitVector assigned_variables_;
701 FrameType frame_type_;
702 int parameter_count_;
705 HEnvironment* outer_;
706 HEnterInlined* entry_;
714 std::ostream& operator<<(std::ostream& os, const HEnvironment& env);
717 class HOptimizedGraphBuilder;
719 enum ArgumentsAllowedFlag {
720 ARGUMENTS_NOT_ALLOWED,
726 class HIfContinuation;
728 // This class is not BASE_EMBEDDED because our inlining implementation uses
732 bool IsEffect() const { return kind_ == Expression::kEffect; }
733 bool IsValue() const { return kind_ == Expression::kValue; }
734 bool IsTest() const { return kind_ == Expression::kTest; }
736 // 'Fill' this context with a hydrogen value. The value is assumed to
737 // have already been inserted in the instruction stream (or not need to
738 // be, e.g., HPhi). Call this function in tail position in the Visit
739 // functions for expressions.
740 virtual void ReturnValue(HValue* value) = 0;
742 // Add a hydrogen instruction to the instruction stream (recording an
743 // environment simulation if necessary) and then fill this context with
744 // the instruction as value.
745 virtual void ReturnInstruction(HInstruction* instr, BailoutId ast_id) = 0;
747 // Finishes the current basic block and materialize a boolean for
748 // value context, nothing for effect, generate a branch for test context.
749 // Call this function in tail position in the Visit functions for
751 virtual void ReturnControl(HControlInstruction* instr, BailoutId ast_id) = 0;
753 // Finishes the current basic block and materialize a boolean for
754 // value context, nothing for effect, generate a branch for test context.
755 // Call this function in tail position in the Visit functions for
756 // expressions that use an IfBuilder.
757 virtual void ReturnContinuation(HIfContinuation* continuation,
758 BailoutId ast_id) = 0;
760 void set_for_typeof(bool for_typeof) { for_typeof_ = for_typeof; }
761 bool is_for_typeof() { return for_typeof_; }
764 AstContext(HOptimizedGraphBuilder* owner, Expression::Context kind);
765 virtual ~AstContext();
767 HOptimizedGraphBuilder* owner() const { return owner_; }
769 inline Zone* zone() const;
771 // We want to be able to assert, in a context-specific way, that the stack
772 // height makes sense when the context is filled.
774 int original_length_;
778 HOptimizedGraphBuilder* owner_;
779 Expression::Context kind_;
785 class EffectContext final : public AstContext {
787 explicit EffectContext(HOptimizedGraphBuilder* owner)
788 : AstContext(owner, Expression::kEffect) {
790 virtual ~EffectContext();
792 void ReturnValue(HValue* value) override;
793 virtual void ReturnInstruction(HInstruction* instr,
794 BailoutId ast_id) override;
795 virtual void ReturnControl(HControlInstruction* instr,
796 BailoutId ast_id) override;
797 virtual void ReturnContinuation(HIfContinuation* continuation,
798 BailoutId ast_id) override;
802 class ValueContext final : public AstContext {
804 ValueContext(HOptimizedGraphBuilder* owner, ArgumentsAllowedFlag flag)
805 : AstContext(owner, Expression::kValue), flag_(flag) {
807 virtual ~ValueContext();
809 void ReturnValue(HValue* value) override;
810 virtual void ReturnInstruction(HInstruction* instr,
811 BailoutId ast_id) override;
812 virtual void ReturnControl(HControlInstruction* instr,
813 BailoutId ast_id) override;
814 virtual void ReturnContinuation(HIfContinuation* continuation,
815 BailoutId ast_id) override;
817 bool arguments_allowed() { return flag_ == ARGUMENTS_ALLOWED; }
820 ArgumentsAllowedFlag flag_;
824 class TestContext final : public AstContext {
826 TestContext(HOptimizedGraphBuilder* owner,
827 Expression* condition,
828 HBasicBlock* if_true,
829 HBasicBlock* if_false)
830 : AstContext(owner, Expression::kTest),
831 condition_(condition),
833 if_false_(if_false) {
836 void ReturnValue(HValue* value) override;
837 virtual void ReturnInstruction(HInstruction* instr,
838 BailoutId ast_id) override;
839 virtual void ReturnControl(HControlInstruction* instr,
840 BailoutId ast_id) override;
841 virtual void ReturnContinuation(HIfContinuation* continuation,
842 BailoutId ast_id) override;
844 static TestContext* cast(AstContext* context) {
845 DCHECK(context->IsTest());
846 return reinterpret_cast<TestContext*>(context);
849 Expression* condition() const { return condition_; }
850 HBasicBlock* if_true() const { return if_true_; }
851 HBasicBlock* if_false() const { return if_false_; }
854 // Build the shared core part of the translation unpacking a value into
856 void BuildBranch(HValue* value);
858 Expression* condition_;
859 HBasicBlock* if_true_;
860 HBasicBlock* if_false_;
864 class FunctionState final {
866 FunctionState(HOptimizedGraphBuilder* owner,
867 CompilationInfo* info,
868 InliningKind inlining_kind,
872 CompilationInfo* compilation_info() { return compilation_info_; }
873 AstContext* call_context() { return call_context_; }
874 InliningKind inlining_kind() const { return inlining_kind_; }
875 HBasicBlock* function_return() { return function_return_; }
876 TestContext* test_context() { return test_context_; }
877 void ClearInlinedTestContext() {
878 delete test_context_;
879 test_context_ = NULL;
882 FunctionState* outer() { return outer_; }
884 HEnterInlined* entry() { return entry_; }
885 void set_entry(HEnterInlined* entry) { entry_ = entry; }
887 HArgumentsObject* arguments_object() { return arguments_object_; }
888 void set_arguments_object(HArgumentsObject* arguments_object) {
889 arguments_object_ = arguments_object;
892 HArgumentsElements* arguments_elements() { return arguments_elements_; }
893 void set_arguments_elements(HArgumentsElements* arguments_elements) {
894 arguments_elements_ = arguments_elements;
897 bool arguments_pushed() { return arguments_elements() != NULL; }
899 int inlining_id() const { return inlining_id_; }
902 HOptimizedGraphBuilder* owner_;
904 CompilationInfo* compilation_info_;
906 // During function inlining, expression context of the call being
907 // inlined. NULL when not inlining.
908 AstContext* call_context_;
910 // The kind of call which is currently being inlined.
911 InliningKind inlining_kind_;
913 // When inlining in an effect or value context, this is the return block.
914 // It is NULL otherwise. When inlining in a test context, there are a
915 // pair of return blocks in the context. When not inlining, there is no
916 // local return point.
917 HBasicBlock* function_return_;
919 // When inlining a call in a test context, a context containing a pair of
920 // return blocks. NULL in all other cases.
921 TestContext* test_context_;
923 // When inlining HEnterInlined instruction corresponding to the function
925 HEnterInlined* entry_;
927 HArgumentsObject* arguments_object_;
928 HArgumentsElements* arguments_elements_;
931 SourcePosition outer_source_position_;
933 FunctionState* outer_;
937 class HIfContinuation final {
940 : continuation_captured_(false),
942 false_branch_(NULL) {}
943 HIfContinuation(HBasicBlock* true_branch,
944 HBasicBlock* false_branch)
945 : continuation_captured_(true), true_branch_(true_branch),
946 false_branch_(false_branch) {}
947 ~HIfContinuation() { DCHECK(!continuation_captured_); }
949 void Capture(HBasicBlock* true_branch,
950 HBasicBlock* false_branch) {
951 DCHECK(!continuation_captured_);
952 true_branch_ = true_branch;
953 false_branch_ = false_branch;
954 continuation_captured_ = true;
957 void Continue(HBasicBlock** true_branch,
958 HBasicBlock** false_branch) {
959 DCHECK(continuation_captured_);
960 *true_branch = true_branch_;
961 *false_branch = false_branch_;
962 continuation_captured_ = false;
965 bool IsTrueReachable() { return true_branch_ != NULL; }
966 bool IsFalseReachable() { return false_branch_ != NULL; }
967 bool TrueAndFalseReachable() {
968 return IsTrueReachable() || IsFalseReachable();
971 HBasicBlock* true_branch() const { return true_branch_; }
972 HBasicBlock* false_branch() const { return false_branch_; }
975 bool continuation_captured_;
976 HBasicBlock* true_branch_;
977 HBasicBlock* false_branch_;
981 class HAllocationMode final BASE_EMBEDDED {
983 explicit HAllocationMode(Handle<AllocationSite> feedback_site)
984 : current_site_(NULL), feedback_site_(feedback_site),
985 pretenure_flag_(NOT_TENURED) {}
986 explicit HAllocationMode(HValue* current_site)
987 : current_site_(current_site), pretenure_flag_(NOT_TENURED) {}
988 explicit HAllocationMode(PretenureFlag pretenure_flag)
989 : current_site_(NULL), pretenure_flag_(pretenure_flag) {}
991 : current_site_(NULL), pretenure_flag_(NOT_TENURED) {}
993 HValue* current_site() const { return current_site_; }
994 Handle<AllocationSite> feedback_site() const { return feedback_site_; }
996 bool CreateAllocationMementos() const WARN_UNUSED_RESULT {
997 return current_site() != NULL;
1000 PretenureFlag GetPretenureMode() const WARN_UNUSED_RESULT {
1001 if (!feedback_site().is_null()) return feedback_site()->GetPretenureMode();
1002 return pretenure_flag_;
1006 HValue* current_site_;
1007 Handle<AllocationSite> feedback_site_;
1008 PretenureFlag pretenure_flag_;
1012 class HGraphBuilder {
1014 explicit HGraphBuilder(CompilationInfo* info)
1017 current_block_(NULL),
1018 scope_(info->scope()),
1019 position_(SourcePosition::Unknown()),
1020 start_position_(0) {}
1021 virtual ~HGraphBuilder() {}
1023 Scope* scope() const { return scope_; }
1024 void set_scope(Scope* scope) { scope_ = scope; }
1026 HBasicBlock* current_block() const { return current_block_; }
1027 void set_current_block(HBasicBlock* block) { current_block_ = block; }
1028 HEnvironment* environment() const {
1029 return current_block()->last_environment();
1031 Zone* zone() const { return info_->zone(); }
1032 HGraph* graph() const { return graph_; }
1033 Isolate* isolate() const { return graph_->isolate(); }
1034 CompilationInfo* top_info() { return info_; }
1036 HGraph* CreateGraph();
1038 // Bailout environment manipulation.
1039 void Push(HValue* value) { environment()->Push(value); }
1040 HValue* Pop() { return environment()->Pop(); }
1042 virtual HValue* context() = 0;
1044 // Adding instructions.
1045 HInstruction* AddInstruction(HInstruction* instr);
1046 void FinishCurrentBlock(HControlInstruction* last);
1047 void FinishExitCurrentBlock(HControlInstruction* instruction);
1049 void Goto(HBasicBlock* from,
1050 HBasicBlock* target,
1051 FunctionState* state = NULL,
1052 bool add_simulate = true) {
1053 from->Goto(target, source_position(), state, add_simulate);
1055 void Goto(HBasicBlock* target,
1056 FunctionState* state = NULL,
1057 bool add_simulate = true) {
1058 Goto(current_block(), target, state, add_simulate);
1060 void GotoNoSimulate(HBasicBlock* from, HBasicBlock* target) {
1061 Goto(from, target, NULL, false);
1063 void GotoNoSimulate(HBasicBlock* target) {
1064 Goto(target, NULL, false);
1066 void AddLeaveInlined(HBasicBlock* block,
1067 HValue* return_value,
1068 FunctionState* state) {
1069 block->AddLeaveInlined(return_value, state, source_position());
1071 void AddLeaveInlined(HValue* return_value, FunctionState* state) {
1072 return AddLeaveInlined(current_block(), return_value, state);
1076 HInstruction* NewUncasted() {
1077 return I::New(isolate(), zone(), context());
1082 return I::New(isolate(), zone(), context());
1086 HInstruction* AddUncasted() { return AddInstruction(NewUncasted<I>());}
1089 I* Add() { return AddInstructionTyped(New<I>());}
1091 template<class I, class P1>
1092 HInstruction* NewUncasted(P1 p1) {
1093 return I::New(isolate(), zone(), context(), p1);
1096 template <class I, class P1>
1098 return I::New(isolate(), zone(), context(), p1);
1101 template<class I, class P1>
1102 HInstruction* AddUncasted(P1 p1) {
1103 HInstruction* result = AddInstruction(NewUncasted<I>(p1));
1104 // Specializations must have their parameters properly casted
1105 // to avoid landing here.
1106 DCHECK(!result->IsReturn() && !result->IsSimulate() &&
1107 !result->IsDeoptimize());
1111 template<class I, class P1>
1113 I* result = AddInstructionTyped(New<I>(p1));
1114 // Specializations must have their parameters properly casted
1115 // to avoid landing here.
1116 DCHECK(!result->IsReturn() && !result->IsSimulate() &&
1117 !result->IsDeoptimize());
1121 template<class I, class P1, class P2>
1122 HInstruction* NewUncasted(P1 p1, P2 p2) {
1123 return I::New(isolate(), zone(), context(), p1, p2);
1126 template<class I, class P1, class P2>
1127 I* New(P1 p1, P2 p2) {
1128 return I::New(isolate(), zone(), context(), p1, p2);
1131 template<class I, class P1, class P2>
1132 HInstruction* AddUncasted(P1 p1, P2 p2) {
1133 HInstruction* result = AddInstruction(NewUncasted<I>(p1, p2));
1134 // Specializations must have their parameters properly casted
1135 // to avoid landing here.
1136 DCHECK(!result->IsSimulate());
1140 template<class I, class P1, class P2>
1141 I* Add(P1 p1, P2 p2) {
1142 I* result = AddInstructionTyped(New<I>(p1, p2));
1143 // Specializations must have their parameters properly casted
1144 // to avoid landing here.
1145 DCHECK(!result->IsSimulate());
1149 template<class I, class P1, class P2, class P3>
1150 HInstruction* NewUncasted(P1 p1, P2 p2, P3 p3) {
1151 return I::New(isolate(), zone(), context(), p1, p2, p3);
1154 template<class I, class P1, class P2, class P3>
1155 I* New(P1 p1, P2 p2, P3 p3) {
1156 return I::New(isolate(), zone(), context(), p1, p2, p3);
1159 template<class I, class P1, class P2, class P3>
1160 HInstruction* AddUncasted(P1 p1, P2 p2, P3 p3) {
1161 return AddInstruction(NewUncasted<I>(p1, p2, p3));
1164 template<class I, class P1, class P2, class P3>
1165 I* Add(P1 p1, P2 p2, P3 p3) {
1166 return AddInstructionTyped(New<I>(p1, p2, p3));
1169 template<class I, class P1, class P2, class P3, class P4>
1170 HInstruction* NewUncasted(P1 p1, P2 p2, P3 p3, P4 p4) {
1171 return I::New(isolate(), zone(), context(), p1, p2, p3, p4);
1174 template<class I, class P1, class P2, class P3, class P4>
1175 I* New(P1 p1, P2 p2, P3 p3, P4 p4) {
1176 return I::New(isolate(), zone(), context(), p1, p2, p3, p4);
1179 template<class I, class P1, class P2, class P3, class P4>
1180 HInstruction* AddUncasted(P1 p1, P2 p2, P3 p3, P4 p4) {
1181 return AddInstruction(NewUncasted<I>(p1, p2, p3, p4));
1184 template<class I, class P1, class P2, class P3, class P4>
1185 I* Add(P1 p1, P2 p2, P3 p3, P4 p4) {
1186 return AddInstructionTyped(New<I>(p1, p2, p3, p4));
1189 template<class I, class P1, class P2, class P3, class P4, class P5>
1190 HInstruction* NewUncasted(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
1191 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5);
1194 template<class I, class P1, class P2, class P3, class P4, class P5>
1195 I* New(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
1196 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5);
1199 template<class I, class P1, class P2, class P3, class P4, class P5>
1200 HInstruction* AddUncasted(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
1201 return AddInstruction(NewUncasted<I>(p1, p2, p3, p4, p5));
1204 template<class I, class P1, class P2, class P3, class P4, class P5>
1205 I* Add(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
1206 return AddInstructionTyped(New<I>(p1, p2, p3, p4, p5));
1209 template<class I, class P1, class P2, class P3, class P4, class P5, class P6>
1210 HInstruction* NewUncasted(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
1211 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5, p6);
1214 template<class I, class P1, class P2, class P3, class P4, class P5, class P6>
1215 I* New(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
1216 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5, p6);
1219 template<class I, class P1, class P2, class P3, class P4, class P5, class P6>
1220 HInstruction* AddUncasted(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
1221 return AddInstruction(NewUncasted<I>(p1, p2, p3, p4, p5, p6));
1224 template<class I, class P1, class P2, class P3, class P4, class P5, class P6>
1225 I* Add(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
1226 return AddInstructionTyped(New<I>(p1, p2, p3, p4, p5, p6));
1229 template<class I, class P1, class P2, class P3, class P4,
1230 class P5, class P6, class P7>
1231 HInstruction* NewUncasted(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
1232 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5, p6, p7);
1235 template<class I, class P1, class P2, class P3, class P4,
1236 class P5, class P6, class P7>
1237 I* New(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
1238 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5, p6, p7);
1241 template<class I, class P1, class P2, class P3,
1242 class P4, class P5, class P6, class P7>
1243 HInstruction* AddUncasted(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
1244 return AddInstruction(NewUncasted<I>(p1, p2, p3, p4, p5, p6, p7));
1247 template<class I, class P1, class P2, class P3,
1248 class P4, class P5, class P6, class P7>
1249 I* Add(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
1250 return AddInstructionTyped(New<I>(p1, p2, p3, p4, p5, p6, p7));
1253 template<class I, class P1, class P2, class P3, class P4,
1254 class P5, class P6, class P7, class P8>
1255 HInstruction* NewUncasted(P1 p1, P2 p2, P3 p3, P4 p4,
1256 P5 p5, P6 p6, P7 p7, P8 p8) {
1257 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5, p6, p7, p8);
1260 template<class I, class P1, class P2, class P3, class P4,
1261 class P5, class P6, class P7, class P8>
1262 I* New(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) {
1263 return I::New(isolate(), zone(), context(), p1, p2, p3, p4, p5, p6, p7, p8);
1266 template<class I, class P1, class P2, class P3, class P4,
1267 class P5, class P6, class P7, class P8>
1268 HInstruction* AddUncasted(P1 p1, P2 p2, P3 p3, P4 p4,
1269 P5 p5, P6 p6, P7 p7, P8 p8) {
1270 return AddInstruction(NewUncasted<I>(p1, p2, p3, p4, p5, p6, p7, p8));
1273 template<class I, class P1, class P2, class P3, class P4,
1274 class P5, class P6, class P7, class P8>
1275 I* Add(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) {
1276 return AddInstructionTyped(New<I>(p1, p2, p3, p4, p5, p6, p7, p8));
1279 void AddSimulate(BailoutId id, RemovableSimulate removable = FIXED_SIMULATE);
1281 // When initializing arrays, we'll unfold the loop if the number of elements
1282 // is known at compile time and is <= kElementLoopUnrollThreshold.
1283 static const int kElementLoopUnrollThreshold = 8;
1286 virtual bool BuildGraph() = 0;
1288 HBasicBlock* CreateBasicBlock(HEnvironment* env);
1289 HBasicBlock* CreateLoopHeaderBlock();
1291 template <class BitFieldClass>
1292 HValue* BuildDecodeField(HValue* encoded_field) {
1293 HValue* mask_value = Add<HConstant>(static_cast<int>(BitFieldClass::kMask));
1294 HValue* masked_field =
1295 AddUncasted<HBitwise>(Token::BIT_AND, encoded_field, mask_value);
1296 return AddUncasted<HShr>(masked_field,
1297 Add<HConstant>(static_cast<int>(BitFieldClass::kShift)));
1300 HValue* BuildGetElementsKind(HValue* object);
1302 HValue* BuildCheckHeapObject(HValue* object);
1303 HValue* BuildCheckString(HValue* string);
1304 HValue* BuildWrapReceiver(HValue* object, HValue* function);
1306 // Building common constructs
1307 HValue* BuildCheckForCapacityGrow(HValue* object,
1313 PropertyAccessType access_type);
1315 HValue* BuildCheckAndGrowElementsCapacity(HValue* object, HValue* elements,
1316 ElementsKind kind, HValue* length,
1317 HValue* capacity, HValue* key);
1319 HValue* BuildCopyElementsOnWrite(HValue* object,
1324 void BuildTransitionElementsKind(HValue* object,
1326 ElementsKind from_kind,
1327 ElementsKind to_kind,
1330 HValue* BuildNumberToString(HValue* object, Type* type);
1332 void BuildJSObjectCheck(HValue* receiver,
1333 int bit_field_mask);
1335 // Checks a key value that's being used for a keyed element access context. If
1336 // the key is a index, i.e. a smi or a number in a unique string with a cached
1337 // numeric value, the "true" of the continuation is joined. Otherwise,
1338 // if the key is a name or a unique string, the "false" of the continuation is
1339 // joined. Otherwise, a deoptimization is triggered. In both paths of the
1340 // continuation, the key is pushed on the top of the environment.
1341 void BuildKeyedIndexCheck(HValue* key,
1342 HIfContinuation* join_continuation);
1344 // Checks the properties of an object if they are in dictionary case, in which
1345 // case "true" of continuation is taken, otherwise the "false"
1346 void BuildTestForDictionaryProperties(HValue* object,
1347 HIfContinuation* continuation);
1349 void BuildNonGlobalObjectCheck(HValue* receiver);
1351 HValue* BuildKeyedLookupCacheHash(HValue* object,
1354 HValue* BuildUncheckedDictionaryElementLoad(HValue* receiver,
1359 HValue* BuildRegExpConstructResult(HValue* length,
1363 // Allocates a new object according with the given allocation properties.
1364 HAllocate* BuildAllocate(HValue* object_size,
1366 InstanceType instance_type,
1367 HAllocationMode allocation_mode);
1368 // Computes the sum of two string lengths, taking care of overflow handling.
1369 HValue* BuildAddStringLengths(HValue* left_length, HValue* right_length);
1370 // Creates a cons string using the two input strings.
1371 HValue* BuildCreateConsString(HValue* length,
1374 HAllocationMode allocation_mode);
1375 // Copies characters from one sequential string to another.
1376 void BuildCopySeqStringChars(HValue* src,
1378 String::Encoding src_encoding,
1381 String::Encoding dst_encoding,
1384 // Align an object size to object alignment boundary
1385 HValue* BuildObjectSizeAlignment(HValue* unaligned_size, int header_size);
1387 // Both operands are non-empty strings.
1388 HValue* BuildUncheckedStringAdd(HValue* left,
1390 HAllocationMode allocation_mode);
1391 // Add two strings using allocation mode, validating type feedback.
1392 HValue* BuildStringAdd(HValue* left,
1394 HAllocationMode allocation_mode);
1396 HInstruction* BuildUncheckedMonomorphicElementAccess(
1397 HValue* checked_object,
1401 ElementsKind elements_kind,
1402 PropertyAccessType access_type,
1403 LoadKeyedHoleMode load_mode,
1404 KeyedAccessStoreMode store_mode);
1406 HInstruction* AddElementAccess(
1408 HValue* checked_key,
1411 ElementsKind elements_kind,
1412 PropertyAccessType access_type,
1413 LoadKeyedHoleMode load_mode = NEVER_RETURN_HOLE);
1415 HInstruction* AddLoadStringInstanceType(HValue* string);
1416 HInstruction* AddLoadStringLength(HValue* string);
1417 HInstruction* BuildLoadStringLength(HValue* string);
1418 HStoreNamedField* AddStoreMapConstant(HValue* object, Handle<Map> map) {
1419 return Add<HStoreNamedField>(object, HObjectAccess::ForMap(),
1420 Add<HConstant>(map));
1422 HLoadNamedField* AddLoadMap(HValue* object,
1423 HValue* dependency = NULL);
1424 HLoadNamedField* AddLoadElements(HValue* object,
1425 HValue* dependency = NULL);
1427 bool MatchRotateRight(HValue* left,
1430 HValue** shift_amount);
1432 HValue* BuildBinaryOperation(Token::Value op, HValue* left, HValue* right,
1433 Type* left_type, Type* right_type,
1434 Type* result_type, Maybe<int> fixed_right_arg,
1435 HAllocationMode allocation_mode,
1438 HLoadNamedField* AddLoadFixedArrayLength(HValue *object,
1439 HValue *dependency = NULL);
1441 HLoadNamedField* AddLoadArrayLength(HValue *object,
1443 HValue *dependency = NULL);
1445 HValue* AddLoadJSBuiltin(Builtins::JavaScript builtin);
1447 HValue* EnforceNumberType(HValue* number, Type* expected);
1448 HValue* TruncateToNumber(HValue* value, Type** expected);
1450 void FinishExitWithHardDeoptimization(Deoptimizer::DeoptReason reason);
1452 void AddIncrementCounter(StatsCounter* counter);
1454 class IfBuilder final {
1456 // If using this constructor, Initialize() must be called explicitly!
1459 explicit IfBuilder(HGraphBuilder* builder);
1460 IfBuilder(HGraphBuilder* builder,
1461 HIfContinuation* continuation);
1464 if (!finished_) End();
1467 void Initialize(HGraphBuilder* builder);
1469 template<class Condition>
1470 Condition* If(HValue *p) {
1471 Condition* compare = builder()->New<Condition>(p);
1472 AddCompare(compare);
1476 template<class Condition, class P2>
1477 Condition* If(HValue* p1, P2 p2) {
1478 Condition* compare = builder()->New<Condition>(p1, p2);
1479 AddCompare(compare);
1483 template<class Condition, class P2, class P3>
1484 Condition* If(HValue* p1, P2 p2, P3 p3) {
1485 Condition* compare = builder()->New<Condition>(p1, p2, p3);
1486 AddCompare(compare);
1490 template<class Condition>
1491 Condition* IfNot(HValue* p) {
1492 Condition* compare = If<Condition>(p);
1497 template<class Condition, class P2>
1498 Condition* IfNot(HValue* p1, P2 p2) {
1499 Condition* compare = If<Condition>(p1, p2);
1504 template<class Condition, class P2, class P3>
1505 Condition* IfNot(HValue* p1, P2 p2, P3 p3) {
1506 Condition* compare = If<Condition>(p1, p2, p3);
1511 template<class Condition>
1512 Condition* OrIf(HValue *p) {
1514 return If<Condition>(p);
1517 template<class Condition, class P2>
1518 Condition* OrIf(HValue* p1, P2 p2) {
1520 return If<Condition>(p1, p2);
1523 template<class Condition, class P2, class P3>
1524 Condition* OrIf(HValue* p1, P2 p2, P3 p3) {
1526 return If<Condition>(p1, p2, p3);
1529 template<class Condition>
1530 Condition* AndIf(HValue *p) {
1532 return If<Condition>(p);
1535 template<class Condition, class P2>
1536 Condition* AndIf(HValue* p1, P2 p2) {
1538 return If<Condition>(p1, p2);
1541 template<class Condition, class P2, class P3>
1542 Condition* AndIf(HValue* p1, P2 p2, P3 p3) {
1544 return If<Condition>(p1, p2, p3);
1550 // Captures the current state of this IfBuilder in the specified
1551 // continuation and ends this IfBuilder.
1552 void CaptureContinuation(HIfContinuation* continuation);
1554 // Joins the specified continuation from this IfBuilder and ends this
1555 // IfBuilder. This appends a Goto instruction from the true branch of
1556 // this IfBuilder to the true branch of the continuation unless the
1557 // true branch of this IfBuilder is already finished. And vice versa
1558 // for the false branch.
1560 // The basic idea is as follows: You have several nested IfBuilder's
1561 // that you want to join based on two possible outcomes (i.e. success
1562 // and failure, or whatever). You can do this easily using this method
1563 // now, for example:
1565 // HIfContinuation cont(graph()->CreateBasicBlock(),
1566 // graph()->CreateBasicBlock());
1568 // IfBuilder if_whatever(this);
1569 // if_whatever.If<Condition>(arg);
1570 // if_whatever.Then();
1572 // if_whatever.Else();
1574 // if_whatever.JoinContinuation(&cont);
1576 // IfBuilder if_something(this);
1577 // if_something.If<Condition>(arg1, arg2);
1578 // if_something.Then();
1580 // if_something.Else();
1582 // if_something.JoinContinuation(&cont);
1584 // IfBuilder if_finally(this, &cont);
1585 // if_finally.Then();
1586 // // continues after then code of if_whatever or if_something.
1588 // if_finally.Else();
1589 // // continues after else code of if_whatever or if_something.
1591 // if_finally.End();
1592 void JoinContinuation(HIfContinuation* continuation);
1598 void Deopt(Deoptimizer::DeoptReason reason);
1599 void ThenDeopt(Deoptimizer::DeoptReason reason) {
1603 void ElseDeopt(Deoptimizer::DeoptReason reason) {
1608 void Return(HValue* value);
1611 void InitializeDontCreateBlocks(HGraphBuilder* builder);
1613 HControlInstruction* AddCompare(HControlInstruction* compare);
1615 HGraphBuilder* builder() const {
1616 DCHECK(builder_ != NULL); // Have you called "Initialize"?
1620 void AddMergeAtJoinBlock(bool deopt);
1623 void Finish(HBasicBlock** then_continuation,
1624 HBasicBlock** else_continuation);
1626 class MergeAtJoinBlock : public ZoneObject {
1628 MergeAtJoinBlock(HBasicBlock* block,
1630 MergeAtJoinBlock* next)
1634 HBasicBlock* block_;
1636 MergeAtJoinBlock* next_;
1639 HGraphBuilder* builder_;
1643 bool did_else_if_ : 1;
1647 bool needs_compare_ : 1;
1648 bool pending_merge_block_ : 1;
1649 HBasicBlock* first_true_block_;
1650 HBasicBlock* first_false_block_;
1651 HBasicBlock* split_edge_merge_block_;
1652 MergeAtJoinBlock* merge_at_join_blocks_;
1653 int normal_merge_at_join_block_count_;
1654 int deopt_merge_at_join_block_count_;
1657 class LoopBuilder final {
1667 explicit LoopBuilder(HGraphBuilder* builder); // while (true) {...}
1668 LoopBuilder(HGraphBuilder* builder,
1670 Direction direction);
1671 LoopBuilder(HGraphBuilder* builder,
1673 Direction direction,
1674 HValue* increment_amount);
1682 HValue* terminating,
1683 Token::Value token);
1685 void BeginBody(int drop_count);
1692 void Initialize(HGraphBuilder* builder, HValue* context,
1693 Direction direction, HValue* increment_amount);
1694 Zone* zone() { return builder_->zone(); }
1696 HGraphBuilder* builder_;
1698 HValue* increment_amount_;
1699 HInstruction* increment_;
1701 HBasicBlock* header_block_;
1702 HBasicBlock* body_block_;
1703 HBasicBlock* exit_block_;
1704 HBasicBlock* exit_trampoline_block_;
1705 Direction direction_;
1709 HValue* BuildNewElementsCapacity(HValue* old_capacity);
1711 class JSArrayBuilder final {
1713 JSArrayBuilder(HGraphBuilder* builder,
1715 HValue* allocation_site_payload,
1716 HValue* constructor_function,
1717 AllocationSiteOverrideMode override_mode);
1719 JSArrayBuilder(HGraphBuilder* builder,
1721 HValue* constructor_function = NULL);
1724 DONT_FILL_WITH_HOLE,
1728 ElementsKind kind() { return kind_; }
1729 HAllocate* elements_location() { return elements_location_; }
1731 HAllocate* AllocateEmptyArray();
1732 HAllocate* AllocateArray(HValue* capacity,
1733 HValue* length_field,
1734 FillMode fill_mode = FILL_WITH_HOLE);
1735 // Use these allocators when capacity could be unknown at compile time
1736 // but its limit is known. For constant |capacity| the value of
1737 // |capacity_upper_bound| is ignored and the actual |capacity|
1738 // value is used as an upper bound.
1739 HAllocate* AllocateArray(HValue* capacity,
1740 int capacity_upper_bound,
1741 HValue* length_field,
1742 FillMode fill_mode = FILL_WITH_HOLE);
1743 HAllocate* AllocateArray(HValue* capacity,
1744 HConstant* capacity_upper_bound,
1745 HValue* length_field,
1746 FillMode fill_mode = FILL_WITH_HOLE);
1747 HValue* GetElementsLocation() { return elements_location_; }
1748 HValue* EmitMapCode();
1751 Zone* zone() const { return builder_->zone(); }
1752 int elements_size() const {
1753 return IsFastDoubleElementsKind(kind_) ? kDoubleSize : kPointerSize;
1755 HGraphBuilder* builder() { return builder_; }
1756 HGraph* graph() { return builder_->graph(); }
1757 int initial_capacity() {
1758 STATIC_ASSERT(JSArray::kPreallocatedArrayElements > 0);
1759 return JSArray::kPreallocatedArrayElements;
1762 HValue* EmitInternalMapCode();
1764 HGraphBuilder* builder_;
1766 AllocationSiteMode mode_;
1767 HValue* allocation_site_payload_;
1768 HValue* constructor_function_;
1769 HAllocate* elements_location_;
1772 HValue* BuildAllocateArrayFromLength(JSArrayBuilder* array_builder,
1773 HValue* length_argument);
1774 HValue* BuildCalculateElementsSize(ElementsKind kind,
1776 HAllocate* AllocateJSArrayObject(AllocationSiteMode mode);
1777 HConstant* EstablishElementsAllocationSize(ElementsKind kind, int capacity);
1779 HAllocate* BuildAllocateElements(ElementsKind kind, HValue* size_in_bytes);
1781 void BuildInitializeElementsHeader(HValue* elements,
1785 // Build allocation and header initialization code for respective successor
1786 // of FixedArrayBase.
1787 HValue* BuildAllocateAndInitializeArray(ElementsKind kind, HValue* capacity);
1789 // |array| must have been allocated with enough room for
1790 // 1) the JSArray and 2) an AllocationMemento if mode requires it.
1791 // If the |elements| value provided is NULL then the array elements storage
1792 // is initialized with empty array.
1793 void BuildJSArrayHeader(HValue* array,
1796 AllocationSiteMode mode,
1797 ElementsKind elements_kind,
1798 HValue* allocation_site_payload,
1799 HValue* length_field);
1801 HValue* BuildGrowElementsCapacity(HValue* object,
1804 ElementsKind new_kind,
1806 HValue* new_capacity);
1808 void BuildFillElementsWithValue(HValue* elements,
1809 ElementsKind elements_kind,
1814 void BuildFillElementsWithHole(HValue* elements,
1815 ElementsKind elements_kind,
1819 void BuildCopyProperties(HValue* from_properties, HValue* to_properties,
1820 HValue* length, HValue* capacity);
1822 void BuildCopyElements(HValue* from_elements,
1823 ElementsKind from_elements_kind,
1824 HValue* to_elements,
1825 ElementsKind to_elements_kind,
1829 HValue* BuildCloneShallowArrayCow(HValue* boilerplate,
1830 HValue* allocation_site,
1831 AllocationSiteMode mode,
1834 HValue* BuildCloneShallowArrayEmpty(HValue* boilerplate,
1835 HValue* allocation_site,
1836 AllocationSiteMode mode);
1838 HValue* BuildCloneShallowArrayNonEmpty(HValue* boilerplate,
1839 HValue* allocation_site,
1840 AllocationSiteMode mode,
1843 HValue* BuildElementIndexHash(HValue* index);
1845 enum MapEmbedding { kEmbedMapsDirectly, kEmbedMapsViaWeakCells };
1847 void BuildCompareNil(HValue* value, Type* type, HIfContinuation* continuation,
1848 MapEmbedding map_embedding = kEmbedMapsDirectly);
1850 void BuildCreateAllocationMemento(HValue* previous_object,
1851 HValue* previous_object_size,
1854 HInstruction* BuildConstantMapCheck(Handle<JSObject> constant);
1855 HInstruction* BuildCheckPrototypeMaps(Handle<JSObject> prototype,
1856 Handle<JSObject> holder);
1858 HInstruction* BuildGetNativeContext(HValue* closure);
1859 HInstruction* BuildGetNativeContext();
1860 HInstruction* BuildGetScriptContext(int context_index);
1861 HInstruction* BuildGetArrayFunction();
1862 HValue* BuildArrayBufferViewFieldAccessor(HValue* object,
1863 HValue* checked_object,
1868 void SetSourcePosition(int position) {
1869 if (position != RelocInfo::kNoPosition) {
1870 position_.set_position(position - start_position_);
1872 // Otherwise position remains unknown.
1875 void EnterInlinedSource(int start_position, int id) {
1876 if (top_info()->is_tracking_positions()) {
1877 start_position_ = start_position;
1878 position_.set_inlining_id(id);
1882 // Convert the given absolute offset from the start of the script to
1883 // the SourcePosition assuming that this position corresponds to the
1884 // same function as current position_.
1885 SourcePosition ScriptPositionToSourcePosition(int position) {
1886 SourcePosition pos = position_;
1887 pos.set_position(position - start_position_);
1891 SourcePosition source_position() { return position_; }
1892 void set_source_position(SourcePosition position) { position_ = position; }
1894 HValue* BuildAllocateEmptyArrayBuffer(HValue* byte_length);
1895 template <typename ViewClass>
1896 void BuildArrayBufferViewInitialization(HValue* obj,
1898 HValue* byte_offset,
1899 HValue* byte_length);
1905 I* AddInstructionTyped(I* instr) {
1906 return I::cast(AddInstruction(instr));
1909 CompilationInfo* info_;
1911 HBasicBlock* current_block_;
1913 SourcePosition position_;
1914 int start_position_;
1919 inline HDeoptimize* HGraphBuilder::Add<HDeoptimize>(
1920 Deoptimizer::DeoptReason reason, Deoptimizer::BailoutType type) {
1921 if (type == Deoptimizer::SOFT) {
1922 isolate()->counters()->soft_deopts_requested()->Increment();
1923 if (FLAG_always_opt) return NULL;
1925 if (current_block()->IsDeoptimizing()) return NULL;
1926 HBasicBlock* after_deopt_block = CreateBasicBlock(
1927 current_block()->last_environment());
1928 HDeoptimize* instr = New<HDeoptimize>(reason, type, after_deopt_block);
1929 if (type == Deoptimizer::SOFT) {
1930 isolate()->counters()->soft_deopts_inserted()->Increment();
1932 FinishCurrentBlock(instr);
1933 set_current_block(after_deopt_block);
1939 inline HInstruction* HGraphBuilder::AddUncasted<HDeoptimize>(
1940 Deoptimizer::DeoptReason reason, Deoptimizer::BailoutType type) {
1941 return Add<HDeoptimize>(reason, type);
1946 inline HSimulate* HGraphBuilder::Add<HSimulate>(
1948 RemovableSimulate removable) {
1949 HSimulate* instr = current_block()->CreateSimulate(id, removable);
1950 AddInstruction(instr);
1956 inline HSimulate* HGraphBuilder::Add<HSimulate>(
1958 return Add<HSimulate>(id, FIXED_SIMULATE);
1963 inline HInstruction* HGraphBuilder::AddUncasted<HSimulate>(BailoutId id) {
1964 return Add<HSimulate>(id, FIXED_SIMULATE);
1969 inline HReturn* HGraphBuilder::Add<HReturn>(HValue* value) {
1970 int num_parameters = graph()->info()->num_parameters();
1971 HValue* params = AddUncasted<HConstant>(num_parameters);
1972 HReturn* return_instruction = New<HReturn>(value, params);
1973 FinishExitCurrentBlock(return_instruction);
1974 return return_instruction;
1979 inline HReturn* HGraphBuilder::Add<HReturn>(HConstant* value) {
1980 return Add<HReturn>(static_cast<HValue*>(value));
1984 inline HInstruction* HGraphBuilder::AddUncasted<HReturn>(HValue* value) {
1985 return Add<HReturn>(value);
1990 inline HInstruction* HGraphBuilder::AddUncasted<HReturn>(HConstant* value) {
1991 return Add<HReturn>(value);
1996 inline HCallRuntime* HGraphBuilder::Add<HCallRuntime>(
1997 Handle<String> name,
1998 const Runtime::Function* c_function,
1999 int argument_count) {
2000 HCallRuntime* instr = New<HCallRuntime>(name, c_function, argument_count);
2001 if (graph()->info()->IsStub()) {
2002 // When compiling code stubs, we don't want to save all double registers
2003 // upon entry to the stub, but instead have the call runtime instruction
2004 // save the double registers only on-demand (in the fallback case).
2005 instr->set_save_doubles(kSaveFPRegs);
2007 AddInstruction(instr);
2013 inline HInstruction* HGraphBuilder::AddUncasted<HCallRuntime>(
2014 Handle<String> name,
2015 const Runtime::Function* c_function,
2016 int argument_count) {
2017 return Add<HCallRuntime>(name, c_function, argument_count);
2022 inline HContext* HGraphBuilder::New<HContext>() {
2023 return HContext::New(zone());
2028 inline HInstruction* HGraphBuilder::NewUncasted<HContext>() {
2029 return New<HContext>();
2032 class HOptimizedGraphBuilder : public HGraphBuilder, public AstVisitor {
2034 // A class encapsulating (lazily-allocated) break and continue blocks for
2035 // a breakable statement. Separated from BreakAndContinueScope so that it
2036 // can have a separate lifetime.
2037 class BreakAndContinueInfo final BASE_EMBEDDED {
2039 explicit BreakAndContinueInfo(BreakableStatement* target,
2044 continue_block_(NULL),
2046 drop_extra_(drop_extra) {
2049 BreakableStatement* target() { return target_; }
2050 HBasicBlock* break_block() { return break_block_; }
2051 void set_break_block(HBasicBlock* block) { break_block_ = block; }
2052 HBasicBlock* continue_block() { return continue_block_; }
2053 void set_continue_block(HBasicBlock* block) { continue_block_ = block; }
2054 Scope* scope() { return scope_; }
2055 int drop_extra() { return drop_extra_; }
2058 BreakableStatement* target_;
2059 HBasicBlock* break_block_;
2060 HBasicBlock* continue_block_;
2065 // A helper class to maintain a stack of current BreakAndContinueInfo
2066 // structures mirroring BreakableStatement nesting.
2067 class BreakAndContinueScope final BASE_EMBEDDED {
2069 BreakAndContinueScope(BreakAndContinueInfo* info,
2070 HOptimizedGraphBuilder* owner)
2071 : info_(info), owner_(owner), next_(owner->break_scope()) {
2072 owner->set_break_scope(this);
2075 ~BreakAndContinueScope() { owner_->set_break_scope(next_); }
2077 BreakAndContinueInfo* info() { return info_; }
2078 HOptimizedGraphBuilder* owner() { return owner_; }
2079 BreakAndContinueScope* next() { return next_; }
2081 // Search the break stack for a break or continue target.
2082 enum BreakType { BREAK, CONTINUE };
2083 HBasicBlock* Get(BreakableStatement* stmt, BreakType type,
2084 Scope** scope, int* drop_extra);
2087 BreakAndContinueInfo* info_;
2088 HOptimizedGraphBuilder* owner_;
2089 BreakAndContinueScope* next_;
2092 explicit HOptimizedGraphBuilder(CompilationInfo* info);
2094 bool BuildGraph() override;
2096 // Simple accessors.
2097 BreakAndContinueScope* break_scope() const { return break_scope_; }
2098 void set_break_scope(BreakAndContinueScope* head) { break_scope_ = head; }
2100 HValue* context() override { return environment()->context(); }
2102 HOsrBuilder* osr() const { return osr_; }
2104 void Bailout(BailoutReason reason);
2106 HBasicBlock* CreateJoin(HBasicBlock* first,
2107 HBasicBlock* second,
2110 FunctionState* function_state() const { return function_state_; }
2112 void VisitDeclarations(ZoneList<Declaration*>* declarations) override;
2114 void* operator new(size_t size, Zone* zone) { return zone->New(size); }
2115 void operator delete(void* pointer, Zone* zone) { }
2116 void operator delete(void* pointer) { }
2118 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
2121 // Forward declarations for inner scope classes.
2122 class SubgraphScope;
2124 static const int kMaxCallPolymorphism = 4;
2125 static const int kMaxLoadPolymorphism = 4;
2126 static const int kMaxStorePolymorphism = 4;
2128 // Even in the 'unlimited' case we have to have some limit in order not to
2129 // overflow the stack.
2130 static const int kUnlimitedMaxInlinedSourceSize = 100000;
2131 static const int kUnlimitedMaxInlinedNodes = 10000;
2132 static const int kUnlimitedMaxInlinedNodesCumulative = 10000;
2134 // Maximum depth and total number of elements and properties for literal
2135 // graphs to be considered for fast deep-copying.
2136 static const int kMaxFastLiteralDepth = 3;
2137 static const int kMaxFastLiteralProperties = 8;
2139 // Simple accessors.
2140 void set_function_state(FunctionState* state) { function_state_ = state; }
2142 AstContext* ast_context() const { return ast_context_; }
2143 void set_ast_context(AstContext* context) { ast_context_ = context; }
2145 // Accessors forwarded to the function state.
2146 CompilationInfo* current_info() const {
2147 return function_state()->compilation_info();
2149 AstContext* call_context() const {
2150 return function_state()->call_context();
2152 HBasicBlock* function_return() const {
2153 return function_state()->function_return();
2155 TestContext* inlined_test_context() const {
2156 return function_state()->test_context();
2158 Handle<SharedFunctionInfo> current_shared_info() const {
2159 return current_info()->shared_info();
2161 TypeFeedbackVector* current_feedback_vector() const {
2162 return current_shared_info()->feedback_vector();
2164 void ClearInlinedTestContext() {
2165 function_state()->ClearInlinedTestContext();
2167 LanguageMode function_language_mode() {
2168 return function_state()->compilation_info()->language_mode();
2171 #define FOR_EACH_HYDROGEN_INTRINSIC(F) \
2176 F(IsConstructCall) \
2178 F(ArgumentsLength) \
2182 F(ThrowIfNotADate) \
2184 F(StringCharFromCode) \
2186 F(OneByteSeqStringSetChar) \
2187 F(TwoByteSeqStringSetChar) \
2191 F(IsUndetectableObject) \
2195 F(HasCachedArrayIndex) \
2196 F(GetCachedArrayIndex) \
2197 F(FastOneByteArrayJoin) \
2198 F(DebugBreakInOptimizedCode) \
2199 F(StringCharCodeAt) \
2204 F(RegExpConstructResult) \
2210 /* Typed Arrays */ \
2211 F(TypedArrayInitialize) \
2212 F(DataViewInitialize) \
2214 F(TypedArrayMaxSizeInHeap) \
2215 F(ArrayBufferViewGetByteLength) \
2216 F(ArrayBufferViewGetByteOffset) \
2217 F(TypedArrayGetLength) \
2219 F(ArrayBufferGetByteLength) \
2221 F(ConstructDouble) \
2228 /* ES6 Collections */ \
2235 F(JSCollectionGetTable) \
2236 F(StringGetRawHashField) \
2239 F(HasFastPackedElements) \
2242 F(StringGetLength) \
2246 #define GENERATOR_DECLARATION(Name) void Generate##Name(CallRuntime* call);
2247 FOR_EACH_HYDROGEN_INTRINSIC(GENERATOR_DECLARATION)
2248 #undef GENERATOR_DECLARATION
2250 void VisitDelete(UnaryOperation* expr);
2251 void VisitVoid(UnaryOperation* expr);
2252 void VisitTypeof(UnaryOperation* expr);
2253 void VisitNot(UnaryOperation* expr);
2255 void VisitComma(BinaryOperation* expr);
2256 void VisitLogicalExpression(BinaryOperation* expr);
2257 void VisitArithmeticExpression(BinaryOperation* expr);
2259 void VisitLoopBody(IterationStatement* stmt,
2260 HBasicBlock* loop_entry);
2262 void BuildForInBody(ForInStatement* stmt, Variable* each_var,
2263 HValue* enumerable);
2265 // Create a back edge in the flow graph. body_exit is the predecessor
2266 // block and loop_entry is the successor block. loop_successor is the
2267 // block where control flow exits the loop normally (e.g., via failure of
2268 // the condition) and break_block is the block where control flow breaks
2269 // from the loop. All blocks except loop_entry can be NULL. The return
2270 // value is the new successor block which is the join of loop_successor
2271 // and break_block, or NULL.
2272 HBasicBlock* CreateLoop(IterationStatement* statement,
2273 HBasicBlock* loop_entry,
2274 HBasicBlock* body_exit,
2275 HBasicBlock* loop_successor,
2276 HBasicBlock* break_block);
2278 // Build a loop entry
2279 HBasicBlock* BuildLoopEntry();
2281 // Builds a loop entry respectful of OSR requirements
2282 HBasicBlock* BuildLoopEntry(IterationStatement* statement);
2284 HBasicBlock* JoinContinue(IterationStatement* statement,
2285 HBasicBlock* exit_block,
2286 HBasicBlock* continue_block);
2288 HValue* Top() const { return environment()->Top(); }
2289 void Drop(int n) { environment()->Drop(n); }
2290 void Bind(Variable* var, HValue* value) { environment()->Bind(var, value); }
2291 bool IsEligibleForEnvironmentLivenessAnalysis(Variable* var,
2294 HEnvironment* env) {
2295 if (!FLAG_analyze_environment_liveness) return false;
2296 // |this| and |arguments| are always live; zapping parameters isn't
2297 // safe because function.arguments can inspect them at any time.
2298 return !var->is_this() &&
2299 !var->is_arguments() &&
2300 !value->IsArgumentsObject() &&
2301 env->is_local_index(index);
2303 void BindIfLive(Variable* var, HValue* value) {
2304 HEnvironment* env = environment();
2305 int index = env->IndexFor(var);
2306 env->Bind(index, value);
2307 if (IsEligibleForEnvironmentLivenessAnalysis(var, index, value, env)) {
2308 HEnvironmentMarker* bind =
2309 Add<HEnvironmentMarker>(HEnvironmentMarker::BIND, index);
2312 bind->set_closure(env->closure());
2316 HValue* LookupAndMakeLive(Variable* var) {
2317 HEnvironment* env = environment();
2318 int index = env->IndexFor(var);
2319 HValue* value = env->Lookup(index);
2320 if (IsEligibleForEnvironmentLivenessAnalysis(var, index, value, env)) {
2321 HEnvironmentMarker* lookup =
2322 Add<HEnvironmentMarker>(HEnvironmentMarker::LOOKUP, index);
2325 lookup->set_closure(env->closure());
2331 // The value of the arguments object is allowed in some but not most value
2332 // contexts. (It's allowed in all effect contexts and disallowed in all
2334 void VisitForValue(Expression* expr,
2335 ArgumentsAllowedFlag flag = ARGUMENTS_NOT_ALLOWED);
2336 void VisitForTypeOf(Expression* expr);
2337 void VisitForEffect(Expression* expr);
2338 void VisitForControl(Expression* expr,
2339 HBasicBlock* true_block,
2340 HBasicBlock* false_block);
2342 // Visit a list of expressions from left to right, each in a value context.
2343 void VisitExpressions(ZoneList<Expression*>* exprs) override;
2344 void VisitExpressions(ZoneList<Expression*>* exprs,
2345 ArgumentsAllowedFlag flag);
2347 // Remove the arguments from the bailout environment and emit instructions
2348 // to push them as outgoing parameters.
2349 template <class Instruction> HInstruction* PreProcessCall(Instruction* call);
2350 void PushArgumentsFromEnvironment(int count);
2352 void SetUpScope(Scope* scope);
2353 void VisitStatements(ZoneList<Statement*>* statements) override;
2355 #define DECLARE_VISIT(type) virtual void Visit##type(type* node) override;
2356 AST_NODE_LIST(DECLARE_VISIT)
2357 #undef DECLARE_VISIT
2360 // Helpers for flow graph construction.
2361 enum GlobalPropertyAccess {
2365 GlobalPropertyAccess LookupGlobalProperty(Variable* var, LookupIterator* it,
2366 PropertyAccessType access_type);
2368 void EnsureArgumentsArePushedForAccess();
2369 bool TryArgumentsAccess(Property* expr);
2371 // Shared code for .call and .apply optimizations.
2372 void HandleIndirectCall(Call* expr, HValue* function, int arguments_count);
2373 // Try to optimize indirect calls such as fun.apply(receiver, arguments)
2374 // or fun.call(...).
2375 bool TryIndirectCall(Call* expr);
2376 void BuildFunctionApply(Call* expr);
2377 void BuildFunctionCall(Call* expr);
2379 bool TryHandleArrayCall(Call* expr, HValue* function);
2380 bool TryHandleArrayCallNew(CallNew* expr, HValue* function);
2381 void BuildArrayCall(Expression* expr, int arguments_count, HValue* function,
2382 Handle<AllocationSite> cell);
2384 enum ArrayIndexOfMode { kFirstIndexOf, kLastIndexOf };
2385 HValue* BuildArrayIndexOf(HValue* receiver,
2386 HValue* search_element,
2388 ArrayIndexOfMode mode);
2390 HValue* ImplicitReceiverFor(HValue* function,
2391 Handle<JSFunction> target);
2393 int InliningAstSize(Handle<JSFunction> target);
2394 bool TryInline(Handle<JSFunction> target, int arguments_count,
2395 HValue* implicit_return_value, BailoutId ast_id,
2396 BailoutId return_id, InliningKind inlining_kind);
2398 bool TryInlineCall(Call* expr);
2399 bool TryInlineConstruct(CallNew* expr, HValue* implicit_return_value);
2400 bool TryInlineGetter(Handle<JSFunction> getter,
2401 Handle<Map> receiver_map,
2403 BailoutId return_id);
2404 bool TryInlineSetter(Handle<JSFunction> setter,
2405 Handle<Map> receiver_map,
2407 BailoutId assignment_id,
2408 HValue* implicit_return_value);
2409 bool TryInlineIndirectCall(Handle<JSFunction> function, Call* expr,
2410 int arguments_count);
2411 bool TryInlineBuiltinMethodCall(Call* expr, Handle<JSFunction> function,
2412 Handle<Map> receiver_map,
2413 int args_count_no_receiver);
2414 bool TryInlineBuiltinFunctionCall(Call* expr);
2421 bool TryInlineApiMethodCall(Call* expr,
2423 SmallMapList* receiver_types);
2424 bool TryInlineApiFunctionCall(Call* expr, HValue* receiver);
2425 bool TryInlineApiGetter(Handle<JSFunction> function,
2426 Handle<Map> receiver_map,
2428 bool TryInlineApiSetter(Handle<JSFunction> function,
2429 Handle<Map> receiver_map,
2431 bool TryInlineApiCall(Handle<JSFunction> function,
2433 SmallMapList* receiver_maps,
2436 ApiCallType call_type);
2437 static bool IsReadOnlyLengthDescriptor(Handle<Map> jsarray_map);
2438 static bool CanInlineArrayResizeOperation(Handle<Map> receiver_map);
2440 // If --trace-inlining, print a line of the inlining trace. Inlining
2441 // succeeded if the reason string is NULL and failed if there is a
2442 // non-NULL reason string.
2443 void TraceInline(Handle<JSFunction> target,
2444 Handle<JSFunction> caller,
2445 const char* failure_reason);
2447 void HandleGlobalVariableAssignment(Variable* var,
2451 void HandlePropertyAssignment(Assignment* expr);
2452 void HandleCompoundAssignment(Assignment* expr);
2453 void HandlePolymorphicNamedFieldAccess(PropertyAccessType access_type,
2456 BailoutId return_id,
2459 SmallMapList* types,
2460 Handle<String> name);
2462 HValue* BuildAllocateExternalElements(
2463 ExternalArrayType array_type,
2464 bool is_zero_byte_offset,
2465 HValue* buffer, HValue* byte_offset, HValue* length);
2466 HValue* BuildAllocateFixedTypedArray(ExternalArrayType array_type,
2467 size_t element_size,
2468 ElementsKind fixed_elements_kind,
2469 HValue* byte_length, HValue* length,
2472 // TODO(adamk): Move all OrderedHashTable functions to their own class.
2473 HValue* BuildOrderedHashTableHashToBucket(HValue* hash, HValue* num_buckets);
2474 template <typename CollectionType>
2475 HValue* BuildOrderedHashTableHashToEntry(HValue* table, HValue* hash,
2476 HValue* num_buckets);
2477 template <typename CollectionType>
2478 HValue* BuildOrderedHashTableEntryToIndex(HValue* entry, HValue* num_buckets);
2479 template <typename CollectionType>
2480 HValue* BuildOrderedHashTableFindEntry(HValue* table, HValue* key,
2482 template <typename CollectionType>
2483 HValue* BuildOrderedHashTableAddEntry(HValue* table, HValue* key,
2485 HIfContinuation* join_continuation);
2486 template <typename CollectionType>
2487 HValue* BuildAllocateOrderedHashTable();
2488 template <typename CollectionType>
2489 void BuildOrderedHashTableClear(HValue* receiver);
2490 template <typename CollectionType>
2491 void BuildJSCollectionDelete(CallRuntime* call,
2492 const Runtime::Function* c_function);
2493 template <typename CollectionType>
2494 void BuildJSCollectionHas(CallRuntime* call,
2495 const Runtime::Function* c_function);
2496 HValue* BuildStringHashLoadIfIsStringAndHashComputed(
2497 HValue* object, HIfContinuation* continuation);
2499 Handle<JSFunction> array_function() {
2500 return handle(isolate()->native_context()->array_function());
2503 bool IsCallArrayInlineable(int argument_count, Handle<AllocationSite> site);
2504 void BuildInlinedCallArray(Expression* expression, int argument_count,
2505 Handle<AllocationSite> site);
2507 class PropertyAccessInfo {
2509 PropertyAccessInfo(HOptimizedGraphBuilder* builder,
2510 PropertyAccessType access_type, Handle<Map> map,
2511 Handle<String> name)
2512 : builder_(builder),
2513 access_type_(access_type),
2516 field_type_(HType::Tagged()),
2517 access_(HObjectAccess::ForMap()),
2518 lookup_type_(NOT_FOUND),
2519 details_(NONE, DATA, Representation::None()) {}
2521 // Checkes whether this PropertyAccessInfo can be handled as a monomorphic
2522 // load named. It additionally fills in the fields necessary to generate the
2524 bool CanAccessMonomorphic();
2526 // Checks whether all types behave uniform when loading name. If all maps
2527 // behave the same, a single monomorphic load instruction can be emitted,
2528 // guarded by a single map-checks instruction that whether the receiver is
2529 // an instance of any of the types.
2530 // This method skips the first type in types, assuming that this
2531 // PropertyAccessInfo is built for types->first().
2532 bool CanAccessAsMonomorphic(SmallMapList* types);
2534 bool NeedsWrappingFor(Handle<JSFunction> target) const;
2537 Handle<String> name() const { return name_; }
2539 bool IsJSObjectFieldAccessor() {
2540 int offset; // unused
2541 return Accessors::IsJSObjectFieldAccessor(map_, name_, &offset);
2544 bool GetJSObjectFieldAccess(HObjectAccess* access) {
2546 if (Accessors::IsJSObjectFieldAccessor(map_, name_, &offset)) {
2547 if (IsStringType()) {
2548 DCHECK(String::Equals(isolate()->factory()->length_string(), name_));
2549 *access = HObjectAccess::ForStringLength();
2550 } else if (IsArrayType()) {
2551 DCHECK(String::Equals(isolate()->factory()->length_string(), name_));
2552 *access = HObjectAccess::ForArrayLength(map_->elements_kind());
2554 *access = HObjectAccess::ForMapAndOffset(map_, offset);
2561 bool IsJSArrayBufferViewFieldAccessor() {
2562 int offset; // unused
2563 return Accessors::IsJSArrayBufferViewFieldAccessor(map_, name_, &offset);
2566 bool GetJSArrayBufferViewFieldAccess(HObjectAccess* access) {
2568 if (Accessors::IsJSArrayBufferViewFieldAccessor(map_, name_, &offset)) {
2569 *access = HObjectAccess::ForMapAndOffset(map_, offset);
2575 bool has_holder() { return !holder_.is_null(); }
2576 bool IsLoad() const { return access_type_ == LOAD; }
2578 Isolate* isolate() const { return builder_->isolate(); }
2579 Handle<JSObject> holder() { return holder_; }
2580 Handle<JSFunction> accessor() { return accessor_; }
2581 Handle<Object> constant() { return constant_; }
2582 Handle<Map> transition() { return transition_; }
2583 SmallMapList* field_maps() { return &field_maps_; }
2584 HType field_type() const { return field_type_; }
2585 HObjectAccess access() { return access_; }
2587 bool IsFound() const { return lookup_type_ != NOT_FOUND; }
2588 bool IsProperty() const { return IsFound() && !IsTransition(); }
2589 bool IsTransition() const { return lookup_type_ == TRANSITION_TYPE; }
2590 bool IsData() const {
2591 return lookup_type_ == DESCRIPTOR_TYPE && details_.type() == DATA;
2593 bool IsDataConstant() const {
2594 return lookup_type_ == DESCRIPTOR_TYPE &&
2595 details_.type() == DATA_CONSTANT;
2597 bool IsAccessorConstant() const {
2598 return !IsTransition() && details_.type() == ACCESSOR_CONSTANT;
2600 bool IsConfigurable() const { return details_.IsConfigurable(); }
2601 bool IsReadOnly() const { return details_.IsReadOnly(); }
2603 bool IsStringType() { return map_->instance_type() < FIRST_NONSTRING_TYPE; }
2604 bool IsNumberType() { return map_->instance_type() == HEAP_NUMBER_TYPE; }
2605 bool IsValueWrapped() { return IsStringType() || IsNumberType(); }
2606 bool IsArrayType() { return map_->instance_type() == JS_ARRAY_TYPE; }
2609 Handle<Object> GetConstantFromMap(Handle<Map> map) const {
2610 DCHECK_EQ(DESCRIPTOR_TYPE, lookup_type_);
2611 DCHECK(number_ < map->NumberOfOwnDescriptors());
2612 return handle(map->instance_descriptors()->GetValue(number_), isolate());
2614 Handle<Object> GetAccessorsFromMap(Handle<Map> map) const {
2615 return GetConstantFromMap(map);
2617 Handle<HeapType> GetFieldTypeFromMap(Handle<Map> map) const {
2619 DCHECK(number_ < map->NumberOfOwnDescriptors());
2620 return handle(map->instance_descriptors()->GetFieldType(number_),
2623 Handle<Map> GetFieldOwnerFromMap(Handle<Map> map) const {
2625 DCHECK(number_ < map->NumberOfOwnDescriptors());
2626 return handle(map->FindFieldOwner(number_));
2628 int GetLocalFieldIndexFromMap(Handle<Map> map) const {
2629 DCHECK(lookup_type_ == DESCRIPTOR_TYPE ||
2630 lookup_type_ == TRANSITION_TYPE);
2631 DCHECK(number_ < map->NumberOfOwnDescriptors());
2632 int field_index = map->instance_descriptors()->GetFieldIndex(number_);
2633 return field_index - map->inobject_properties();
2636 void LookupDescriptor(Map* map, Name* name) {
2637 DescriptorArray* descriptors = map->instance_descriptors();
2638 int number = descriptors->SearchWithCache(name, map);
2639 if (number == DescriptorArray::kNotFound) return NotFound();
2640 lookup_type_ = DESCRIPTOR_TYPE;
2641 details_ = descriptors->GetDetails(number);
2644 void LookupTransition(Map* map, Name* name, PropertyAttributes attributes) {
2646 TransitionArray::SearchTransition(map, kData, name, attributes);
2647 if (target == NULL) return NotFound();
2648 lookup_type_ = TRANSITION_TYPE;
2649 transition_ = handle(target);
2650 number_ = transition_->LastAdded();
2651 details_ = transition_->instance_descriptors()->GetDetails(number_);
2654 lookup_type_ = NOT_FOUND;
2655 details_ = PropertyDetails::Empty();
2657 Representation representation() const {
2659 return details_.representation();
2661 bool IsTransitionToData() const {
2662 return IsTransition() && details_.type() == DATA;
2665 Zone* zone() { return builder_->zone(); }
2666 CompilationInfo* top_info() { return builder_->top_info(); }
2667 CompilationInfo* current_info() { return builder_->current_info(); }
2669 bool LoadResult(Handle<Map> map);
2670 bool LoadFieldMaps(Handle<Map> map);
2671 bool LookupDescriptor();
2672 bool LookupInPrototypes();
2673 bool IsIntegerIndexedExotic();
2674 bool IsCompatible(PropertyAccessInfo* other);
2676 void GeneralizeRepresentation(Representation r) {
2677 access_ = access_.WithRepresentation(
2678 access_.representation().generalize(r));
2681 HOptimizedGraphBuilder* builder_;
2682 PropertyAccessType access_type_;
2684 Handle<String> name_;
2685 Handle<JSObject> holder_;
2686 Handle<JSFunction> accessor_;
2687 Handle<JSObject> api_holder_;
2688 Handle<Object> constant_;
2689 SmallMapList field_maps_;
2691 HObjectAccess access_;
2693 enum { NOT_FOUND, DESCRIPTOR_TYPE, TRANSITION_TYPE } lookup_type_;
2694 Handle<Map> transition_;
2696 PropertyDetails details_;
2699 HValue* BuildMonomorphicAccess(PropertyAccessInfo* info, HValue* object,
2700 HValue* checked_object, HValue* value,
2701 BailoutId ast_id, BailoutId return_id,
2702 bool can_inline_accessor = true);
2704 HValue* BuildNamedAccess(PropertyAccessType access, BailoutId ast_id,
2705 BailoutId reutrn_id, Expression* expr,
2706 HValue* object, Handle<String> name, HValue* value,
2707 bool is_uninitialized = false);
2709 void HandlePolymorphicCallNamed(Call* expr,
2711 SmallMapList* types,
2712 Handle<String> name);
2713 void HandleLiteralCompareTypeof(CompareOperation* expr,
2714 Expression* sub_expr,
2715 Handle<String> check);
2716 void HandleLiteralCompareNil(CompareOperation* expr,
2717 Expression* sub_expr,
2720 enum PushBeforeSimulateBehavior {
2721 PUSH_BEFORE_SIMULATE,
2722 NO_PUSH_BEFORE_SIMULATE
2725 HControlInstruction* BuildCompareInstruction(
2726 Token::Value op, HValue* left, HValue* right, Type* left_type,
2727 Type* right_type, Type* combined_type, SourcePosition left_position,
2728 SourcePosition right_position, PushBeforeSimulateBehavior push_sim_result,
2729 BailoutId bailout_id);
2731 HInstruction* BuildStringCharCodeAt(HValue* string,
2734 HValue* BuildBinaryOperation(
2735 BinaryOperation* expr,
2738 PushBeforeSimulateBehavior push_sim_result);
2739 HInstruction* BuildIncrement(bool returns_original_input,
2740 CountOperation* expr);
2741 HInstruction* BuildKeyedGeneric(PropertyAccessType access_type,
2747 HInstruction* TryBuildConsolidatedElementLoad(HValue* object,
2750 SmallMapList* maps);
2752 LoadKeyedHoleMode BuildKeyedHoleMode(Handle<Map> map);
2754 HInstruction* BuildMonomorphicElementAccess(HValue* object,
2759 PropertyAccessType access_type,
2760 KeyedAccessStoreMode store_mode);
2762 HValue* HandlePolymorphicElementAccess(Expression* expr,
2767 PropertyAccessType access_type,
2768 KeyedAccessStoreMode store_mode,
2769 bool* has_side_effects);
2771 HValue* HandleKeyedElementAccess(HValue* obj, HValue* key, HValue* val,
2772 Expression* expr, BailoutId ast_id,
2773 BailoutId return_id,
2774 PropertyAccessType access_type,
2775 bool* has_side_effects);
2777 HInstruction* BuildNamedGeneric(PropertyAccessType access, Expression* expr,
2778 HValue* object, Handle<String> name,
2779 HValue* value, bool is_uninitialized = false);
2781 HCheckMaps* AddCheckMap(HValue* object, Handle<Map> map);
2783 void BuildLoad(Property* property,
2785 void PushLoad(Property* property,
2789 void BuildStoreForEffect(Expression* expression,
2792 BailoutId return_id,
2797 void BuildStore(Expression* expression,
2800 BailoutId return_id,
2801 bool is_uninitialized = false);
2803 HInstruction* BuildLoadNamedField(PropertyAccessInfo* info,
2804 HValue* checked_object);
2805 HInstruction* BuildStoreNamedField(PropertyAccessInfo* info,
2806 HValue* checked_object,
2809 HValue* BuildContextChainWalk(Variable* var);
2811 HInstruction* BuildThisFunction();
2813 HInstruction* BuildFastLiteral(Handle<JSObject> boilerplate_object,
2814 AllocationSiteUsageContext* site_context);
2816 void BuildEmitObjectHeader(Handle<JSObject> boilerplate_object,
2817 HInstruction* object);
2819 void BuildEmitInObjectProperties(Handle<JSObject> boilerplate_object,
2820 HInstruction* object,
2821 AllocationSiteUsageContext* site_context,
2822 PretenureFlag pretenure_flag);
2824 void BuildEmitElements(Handle<JSObject> boilerplate_object,
2825 Handle<FixedArrayBase> elements,
2826 HValue* object_elements,
2827 AllocationSiteUsageContext* site_context);
2829 void BuildEmitFixedDoubleArray(Handle<FixedArrayBase> elements,
2831 HValue* object_elements);
2833 void BuildEmitFixedArray(Handle<FixedArrayBase> elements,
2835 HValue* object_elements,
2836 AllocationSiteUsageContext* site_context);
2838 void AddCheckPrototypeMaps(Handle<JSObject> holder,
2839 Handle<Map> receiver_map);
2841 HInstruction* NewPlainFunctionCall(HValue* fun,
2843 bool pass_argument_count);
2845 HInstruction* NewArgumentAdaptorCall(HValue* fun, HValue* context,
2847 HValue* expected_param_count);
2849 HInstruction* BuildCallConstantFunction(Handle<JSFunction> target,
2850 int argument_count);
2852 bool CanBeFunctionApplyArguments(Call* expr);
2854 // The translation state of the currently-being-translated function.
2855 FunctionState* function_state_;
2857 // The base of the function state stack.
2858 FunctionState initial_function_state_;
2860 // Expression context of the currently visited subexpression. NULL when
2861 // visiting statements.
2862 AstContext* ast_context_;
2864 // A stack of breakable statements entered.
2865 BreakAndContinueScope* break_scope_;
2868 ZoneList<Handle<Object> > globals_;
2870 bool inline_bailout_;
2874 friend class FunctionState; // Pushes and pops the state stack.
2875 friend class AstContext; // Pushes and pops the AST context stack.
2876 friend class KeyedLoadFastElementStub;
2877 friend class HOsrBuilder;
2879 DISALLOW_COPY_AND_ASSIGN(HOptimizedGraphBuilder);
2883 Zone* AstContext::zone() const { return owner_->zone(); }
2886 class HStatistics final : public Malloced {
2895 void Initialize(CompilationInfo* info);
2897 void SaveTiming(const char* name, base::TimeDelta time, size_t size);
2899 void IncrementFullCodeGen(base::TimeDelta full_code_gen) {
2900 full_code_gen_ += full_code_gen;
2903 void IncrementCreateGraph(base::TimeDelta delta) { create_graph_ += delta; }
2905 void IncrementOptimizeGraph(base::TimeDelta delta) {
2906 optimize_graph_ += delta;
2909 void IncrementGenerateCode(base::TimeDelta delta) { generate_code_ += delta; }
2911 void IncrementSubtotals(base::TimeDelta create_graph,
2912 base::TimeDelta optimize_graph,
2913 base::TimeDelta generate_code) {
2914 IncrementCreateGraph(create_graph);
2915 IncrementOptimizeGraph(optimize_graph);
2916 IncrementGenerateCode(generate_code);
2920 List<base::TimeDelta> times_;
2921 List<const char*> names_;
2922 List<size_t> sizes_;
2923 base::TimeDelta create_graph_;
2924 base::TimeDelta optimize_graph_;
2925 base::TimeDelta generate_code_;
2927 base::TimeDelta full_code_gen_;
2928 double source_size_;
2932 class HPhase : public CompilationPhase {
2934 HPhase(const char* name, HGraph* graph)
2935 : CompilationPhase(name, graph->info()),
2940 HGraph* graph() const { return graph_; }
2945 DISALLOW_COPY_AND_ASSIGN(HPhase);
2949 class HTracer final : public Malloced {
2951 explicit HTracer(int isolate_id)
2952 : trace_(&string_allocator_), indent_(0) {
2953 if (FLAG_trace_hydrogen_file == NULL) {
2955 "hydrogen-%d-%d.cfg",
2956 base::OS::GetCurrentProcessId(),
2959 StrNCpy(filename_, FLAG_trace_hydrogen_file, filename_.length());
2961 WriteChars(filename_.start(), "", 0, false);
2964 void TraceCompilation(CompilationInfo* info);
2965 void TraceHydrogen(const char* name, HGraph* graph);
2966 void TraceLithium(const char* name, LChunk* chunk);
2967 void TraceLiveRanges(const char* name, LAllocator* allocator);
2970 class Tag final BASE_EMBEDDED {
2972 Tag(HTracer* tracer, const char* name) {
2975 tracer->PrintIndent();
2976 tracer->trace_.Add("begin_%s\n", name);
2982 tracer_->PrintIndent();
2983 tracer_->trace_.Add("end_%s\n", name_);
2984 DCHECK(tracer_->indent_ >= 0);
2985 tracer_->FlushToFile();
2993 void TraceLiveRange(LiveRange* range, const char* type, Zone* zone);
2994 void Trace(const char* name, HGraph* graph, LChunk* chunk);
2997 void PrintEmptyProperty(const char* name) {
2999 trace_.Add("%s\n", name);
3002 void PrintStringProperty(const char* name, const char* value) {
3004 trace_.Add("%s \"%s\"\n", name, value);
3007 void PrintLongProperty(const char* name, int64_t value) {
3009 trace_.Add("%s %d000\n", name, static_cast<int>(value / 1000));
3012 void PrintBlockProperty(const char* name, int block_id) {
3014 trace_.Add("%s \"B%d\"\n", name, block_id);
3017 void PrintIntProperty(const char* name, int value) {
3019 trace_.Add("%s %d\n", name, value);
3022 void PrintIndent() {
3023 for (int i = 0; i < indent_; i++) {
3028 EmbeddedVector<char, 64> filename_;
3029 HeapStringAllocator string_allocator_;
3030 StringStream trace_;
3035 class NoObservableSideEffectsScope final {
3037 explicit NoObservableSideEffectsScope(HGraphBuilder* builder) :
3039 builder_->graph()->IncrementInNoSideEffectsScope();
3041 ~NoObservableSideEffectsScope() {
3042 builder_->graph()->DecrementInNoSideEffectsScope();
3046 HGraphBuilder* builder_;
3050 } } // namespace v8::internal
3052 #endif // V8_HYDROGEN_H_