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.
7 #include "src/double.h"
8 #include "src/factory.h"
9 #include "src/hydrogen-infer-representation.h"
10 #include "src/property-details-inl.h"
12 #if V8_TARGET_ARCH_IA32
13 #include "src/ia32/lithium-ia32.h" // NOLINT
14 #elif V8_TARGET_ARCH_X64
15 #include "src/x64/lithium-x64.h" // NOLINT
16 #elif V8_TARGET_ARCH_ARM64
17 #include "src/arm64/lithium-arm64.h" // NOLINT
18 #elif V8_TARGET_ARCH_ARM
19 #include "src/arm/lithium-arm.h" // NOLINT
20 #elif V8_TARGET_ARCH_MIPS
21 #include "src/mips/lithium-mips.h" // NOLINT
22 #elif V8_TARGET_ARCH_MIPS64
23 #include "src/mips64/lithium-mips64.h" // NOLINT
24 #elif V8_TARGET_ARCH_X87
25 #include "src/x87/lithium-x87.h" // NOLINT
27 #error Unsupported target architecture.
30 #include "src/base/safe_math.h"
35 #define DEFINE_COMPILE(type) \
36 LInstruction* H##type::CompileToLithium(LChunkBuilder* builder) { \
37 return builder->Do##type(this); \
39 HYDROGEN_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
43 Isolate* HValue::isolate() const {
44 DCHECK(block() != NULL);
45 return block()->isolate();
49 void HValue::AssumeRepresentation(Representation r) {
50 if (CheckFlag(kFlexibleRepresentation)) {
51 ChangeRepresentation(r);
52 // The representation of the value is dictated by type feedback and
53 // will not be changed later.
54 ClearFlag(kFlexibleRepresentation);
59 void HValue::InferRepresentation(HInferRepresentationPhase* h_infer) {
60 DCHECK(CheckFlag(kFlexibleRepresentation));
61 Representation new_rep = RepresentationFromInputs();
62 UpdateRepresentation(new_rep, h_infer, "inputs");
63 new_rep = RepresentationFromUses();
64 UpdateRepresentation(new_rep, h_infer, "uses");
65 if (representation().IsSmi() && HasNonSmiUse()) {
67 Representation::Integer32(), h_infer, "use requirements");
72 Representation HValue::RepresentationFromUses() {
73 if (HasNoUses()) return Representation::None();
75 // Array of use counts for each representation.
76 int use_count[Representation::kNumRepresentations] = { 0 };
78 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
79 HValue* use = it.value();
80 Representation rep = use->observed_input_representation(it.index());
81 if (rep.IsNone()) continue;
82 if (FLAG_trace_representation) {
83 PrintF("#%d %s is used by #%d %s as %s%s\n",
84 id(), Mnemonic(), use->id(), use->Mnemonic(), rep.Mnemonic(),
85 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
87 use_count[rep.kind()] += 1;
89 if (IsPhi()) HPhi::cast(this)->AddIndirectUsesTo(&use_count[0]);
90 int tagged_count = use_count[Representation::kTagged];
91 int double_count = use_count[Representation::kDouble];
92 int int32_count = use_count[Representation::kInteger32];
93 int smi_count = use_count[Representation::kSmi];
95 if (tagged_count > 0) return Representation::Tagged();
96 if (double_count > 0) return Representation::Double();
97 if (int32_count > 0) return Representation::Integer32();
98 if (smi_count > 0) return Representation::Smi();
100 return Representation::None();
104 void HValue::UpdateRepresentation(Representation new_rep,
105 HInferRepresentationPhase* h_infer,
106 const char* reason) {
107 Representation r = representation();
108 if (new_rep.is_more_general_than(r)) {
109 if (CheckFlag(kCannotBeTagged) && new_rep.IsTagged()) return;
110 if (FLAG_trace_representation) {
111 PrintF("Changing #%d %s representation %s -> %s based on %s\n",
112 id(), Mnemonic(), r.Mnemonic(), new_rep.Mnemonic(), reason);
114 ChangeRepresentation(new_rep);
115 AddDependantsToWorklist(h_infer);
120 void HValue::AddDependantsToWorklist(HInferRepresentationPhase* h_infer) {
121 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
122 h_infer->AddToWorklist(it.value());
124 for (int i = 0; i < OperandCount(); ++i) {
125 h_infer->AddToWorklist(OperandAt(i));
130 static int32_t ConvertAndSetOverflow(Representation r,
134 if (result > Smi::kMaxValue) {
136 return Smi::kMaxValue;
138 if (result < Smi::kMinValue) {
140 return Smi::kMinValue;
143 if (result > kMaxInt) {
147 if (result < kMinInt) {
152 return static_cast<int32_t>(result);
156 static int32_t AddWithoutOverflow(Representation r,
160 int64_t result = static_cast<int64_t>(a) + static_cast<int64_t>(b);
161 return ConvertAndSetOverflow(r, result, overflow);
165 static int32_t SubWithoutOverflow(Representation r,
169 int64_t result = static_cast<int64_t>(a) - static_cast<int64_t>(b);
170 return ConvertAndSetOverflow(r, result, overflow);
174 static int32_t MulWithoutOverflow(const Representation& r,
178 int64_t result = static_cast<int64_t>(a) * static_cast<int64_t>(b);
179 return ConvertAndSetOverflow(r, result, overflow);
183 int32_t Range::Mask() const {
184 if (lower_ == upper_) return lower_;
187 while (res < upper_) {
188 res = (res << 1) | 1;
196 void Range::AddConstant(int32_t value) {
197 if (value == 0) return;
198 bool may_overflow = false; // Overflow is ignored here.
199 Representation r = Representation::Integer32();
200 lower_ = AddWithoutOverflow(r, lower_, value, &may_overflow);
201 upper_ = AddWithoutOverflow(r, upper_, value, &may_overflow);
208 void Range::Intersect(Range* other) {
209 upper_ = Min(upper_, other->upper_);
210 lower_ = Max(lower_, other->lower_);
211 bool b = CanBeMinusZero() && other->CanBeMinusZero();
212 set_can_be_minus_zero(b);
216 void Range::Union(Range* other) {
217 upper_ = Max(upper_, other->upper_);
218 lower_ = Min(lower_, other->lower_);
219 bool b = CanBeMinusZero() || other->CanBeMinusZero();
220 set_can_be_minus_zero(b);
224 void Range::CombinedMax(Range* other) {
225 upper_ = Max(upper_, other->upper_);
226 lower_ = Max(lower_, other->lower_);
227 set_can_be_minus_zero(CanBeMinusZero() || other->CanBeMinusZero());
231 void Range::CombinedMin(Range* other) {
232 upper_ = Min(upper_, other->upper_);
233 lower_ = Min(lower_, other->lower_);
234 set_can_be_minus_zero(CanBeMinusZero() || other->CanBeMinusZero());
238 void Range::Sar(int32_t value) {
239 int32_t bits = value & 0x1F;
240 lower_ = lower_ >> bits;
241 upper_ = upper_ >> bits;
242 set_can_be_minus_zero(false);
246 void Range::Shl(int32_t value) {
247 int32_t bits = value & 0x1F;
248 int old_lower = lower_;
249 int old_upper = upper_;
250 lower_ = lower_ << bits;
251 upper_ = upper_ << bits;
252 if (old_lower != lower_ >> bits || old_upper != upper_ >> bits) {
256 set_can_be_minus_zero(false);
260 bool Range::AddAndCheckOverflow(const Representation& r, Range* other) {
261 bool may_overflow = false;
262 lower_ = AddWithoutOverflow(r, lower_, other->lower(), &may_overflow);
263 upper_ = AddWithoutOverflow(r, upper_, other->upper(), &may_overflow);
272 bool Range::SubAndCheckOverflow(const Representation& r, Range* other) {
273 bool may_overflow = false;
274 lower_ = SubWithoutOverflow(r, lower_, other->upper(), &may_overflow);
275 upper_ = SubWithoutOverflow(r, upper_, other->lower(), &may_overflow);
284 void Range::KeepOrder() {
285 if (lower_ > upper_) {
286 int32_t tmp = lower_;
294 void Range::Verify() const {
295 DCHECK(lower_ <= upper_);
300 bool Range::MulAndCheckOverflow(const Representation& r, Range* other) {
301 bool may_overflow = false;
302 int v1 = MulWithoutOverflow(r, lower_, other->lower(), &may_overflow);
303 int v2 = MulWithoutOverflow(r, lower_, other->upper(), &may_overflow);
304 int v3 = MulWithoutOverflow(r, upper_, other->lower(), &may_overflow);
305 int v4 = MulWithoutOverflow(r, upper_, other->upper(), &may_overflow);
306 lower_ = Min(Min(v1, v2), Min(v3, v4));
307 upper_ = Max(Max(v1, v2), Max(v3, v4));
315 bool HValue::IsDefinedAfter(HBasicBlock* other) const {
316 return block()->block_id() > other->block_id();
320 HUseListNode* HUseListNode::tail() {
321 // Skip and remove dead items in the use list.
322 while (tail_ != NULL && tail_->value()->CheckFlag(HValue::kIsDead)) {
323 tail_ = tail_->tail_;
329 bool HValue::CheckUsesForFlag(Flag f) const {
330 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
331 if (it.value()->IsSimulate()) continue;
332 if (!it.value()->CheckFlag(f)) return false;
338 bool HValue::CheckUsesForFlag(Flag f, HValue** value) const {
339 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
340 if (it.value()->IsSimulate()) continue;
341 if (!it.value()->CheckFlag(f)) {
350 bool HValue::HasAtLeastOneUseWithFlagAndNoneWithout(Flag f) const {
351 bool return_value = false;
352 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
353 if (it.value()->IsSimulate()) continue;
354 if (!it.value()->CheckFlag(f)) return false;
361 HUseIterator::HUseIterator(HUseListNode* head) : next_(head) {
366 void HUseIterator::Advance() {
368 if (current_ != NULL) {
369 next_ = current_->tail();
370 value_ = current_->value();
371 index_ = current_->index();
376 int HValue::UseCount() const {
378 for (HUseIterator it(uses()); !it.Done(); it.Advance()) ++count;
383 HUseListNode* HValue::RemoveUse(HValue* value, int index) {
384 HUseListNode* previous = NULL;
385 HUseListNode* current = use_list_;
386 while (current != NULL) {
387 if (current->value() == value && current->index() == index) {
388 if (previous == NULL) {
389 use_list_ = current->tail();
391 previous->set_tail(current->tail());
397 current = current->tail();
401 // Do not reuse use list nodes in debug mode, zap them.
402 if (current != NULL) {
405 HUseListNode(current->value(), current->index(), NULL);
414 bool HValue::Equals(HValue* other) {
415 if (other->opcode() != opcode()) return false;
416 if (!other->representation().Equals(representation())) return false;
417 if (!other->type_.Equals(type_)) return false;
418 if (other->flags() != flags()) return false;
419 if (OperandCount() != other->OperandCount()) return false;
420 for (int i = 0; i < OperandCount(); ++i) {
421 if (OperandAt(i)->id() != other->OperandAt(i)->id()) return false;
423 bool result = DataEquals(other);
424 DCHECK(!result || Hashcode() == other->Hashcode());
429 intptr_t HValue::Hashcode() {
430 intptr_t result = opcode();
431 int count = OperandCount();
432 for (int i = 0; i < count; ++i) {
433 result = result * 19 + OperandAt(i)->id() + (result >> 7);
439 const char* HValue::Mnemonic() const {
441 #define MAKE_CASE(type) case k##type: return #type;
442 HYDROGEN_CONCRETE_INSTRUCTION_LIST(MAKE_CASE)
444 case kPhi: return "Phi";
450 bool HValue::CanReplaceWithDummyUses() {
451 return FLAG_unreachable_code_elimination &&
452 !(block()->IsReachable() ||
454 IsControlInstruction() ||
455 IsArgumentsObject() ||
456 IsCapturedObject() ||
463 bool HValue::IsInteger32Constant() {
464 return IsConstant() && HConstant::cast(this)->HasInteger32Value();
468 int32_t HValue::GetInteger32Constant() {
469 return HConstant::cast(this)->Integer32Value();
473 bool HValue::EqualsInteger32Constant(int32_t value) {
474 return IsInteger32Constant() && GetInteger32Constant() == value;
478 void HValue::SetOperandAt(int index, HValue* value) {
479 RegisterUse(index, value);
480 InternalSetOperandAt(index, value);
484 void HValue::DeleteAndReplaceWith(HValue* other) {
485 // We replace all uses first, so Delete can assert that there are none.
486 if (other != NULL) ReplaceAllUsesWith(other);
492 void HValue::ReplaceAllUsesWith(HValue* other) {
493 while (use_list_ != NULL) {
494 HUseListNode* list_node = use_list_;
495 HValue* value = list_node->value();
496 DCHECK(!value->block()->IsStartBlock());
497 value->InternalSetOperandAt(list_node->index(), other);
498 use_list_ = list_node->tail();
499 list_node->set_tail(other->use_list_);
500 other->use_list_ = list_node;
505 void HValue::Kill() {
506 // Instead of going through the entire use list of each operand, we only
507 // check the first item in each use list and rely on the tail() method to
508 // skip dead items, removing them lazily next time we traverse the list.
510 for (int i = 0; i < OperandCount(); ++i) {
511 HValue* operand = OperandAt(i);
512 if (operand == NULL) continue;
513 HUseListNode* first = operand->use_list_;
514 if (first != NULL && first->value()->CheckFlag(kIsDead)) {
515 operand->use_list_ = first->tail();
521 void HValue::SetBlock(HBasicBlock* block) {
522 DCHECK(block_ == NULL || block == NULL);
524 if (id_ == kNoNumber && block != NULL) {
525 id_ = block->graph()->GetNextValueID(this);
530 OStream& operator<<(OStream& os, const HValue& v) { return v.PrintTo(os); }
533 OStream& operator<<(OStream& os, const TypeOf& t) {
534 if (t.value->representation().IsTagged() &&
535 !t.value->type().Equals(HType::Tagged()))
537 return os << " type:" << t.value->type();
541 OStream& operator<<(OStream& os, const ChangesOf& c) {
542 GVNFlagSet changes_flags = c.value->ChangesFlags();
543 if (changes_flags.IsEmpty()) return os;
545 if (changes_flags == c.value->AllSideEffectsFlagSet()) {
548 bool add_comma = false;
549 #define PRINT_DO(Type) \
550 if (changes_flags.Contains(k##Type)) { \
551 if (add_comma) os << ","; \
555 GVN_TRACKED_FLAG_LIST(PRINT_DO);
556 GVN_UNTRACKED_FLAG_LIST(PRINT_DO);
563 bool HValue::HasMonomorphicJSObjectType() {
564 return !GetMonomorphicJSObjectMap().is_null();
568 bool HValue::UpdateInferredType() {
569 HType type = CalculateInferredType();
570 bool result = (!type.Equals(type_));
576 void HValue::RegisterUse(int index, HValue* new_value) {
577 HValue* old_value = OperandAt(index);
578 if (old_value == new_value) return;
580 HUseListNode* removed = NULL;
581 if (old_value != NULL) {
582 removed = old_value->RemoveUse(this, index);
585 if (new_value != NULL) {
586 if (removed == NULL) {
587 new_value->use_list_ = new(new_value->block()->zone()) HUseListNode(
588 this, index, new_value->use_list_);
590 removed->set_tail(new_value->use_list_);
591 new_value->use_list_ = removed;
597 void HValue::AddNewRange(Range* r, Zone* zone) {
598 if (!HasRange()) ComputeInitialRange(zone);
599 if (!HasRange()) range_ = new(zone) Range();
601 r->StackUpon(range_);
606 void HValue::RemoveLastAddedRange() {
608 DCHECK(range_->next() != NULL);
609 range_ = range_->next();
613 void HValue::ComputeInitialRange(Zone* zone) {
615 range_ = InferRange(zone);
620 OStream& operator<<(OStream& os, const HSourcePosition& p) {
623 } else if (FLAG_hydrogen_track_positions) {
624 return os << "<" << p.inlining_id() << ":" << p.position() << ">";
626 return os << "<0:" << p.raw() << ">";
631 OStream& HInstruction::PrintTo(OStream& os) const { // NOLINT
632 os << Mnemonic() << " ";
633 PrintDataTo(os) << ChangesOf(this) << TypeOf(this);
634 if (CheckFlag(HValue::kHasNoObservableSideEffects)) os << " [noOSE]";
635 if (CheckFlag(HValue::kIsDead)) os << " [dead]";
640 OStream& HInstruction::PrintDataTo(OStream& os) const { // NOLINT
641 for (int i = 0; i < OperandCount(); ++i) {
642 if (i > 0) os << " ";
643 os << NameOf(OperandAt(i));
649 void HInstruction::Unlink() {
651 DCHECK(!IsControlInstruction()); // Must never move control instructions.
652 DCHECK(!IsBlockEntry()); // Doesn't make sense to delete these.
653 DCHECK(previous_ != NULL);
654 previous_->next_ = next_;
656 DCHECK(block()->last() == this);
657 block()->set_last(previous_);
659 next_->previous_ = previous_;
665 void HInstruction::InsertBefore(HInstruction* next) {
667 DCHECK(!next->IsBlockEntry());
668 DCHECK(!IsControlInstruction());
669 DCHECK(!next->block()->IsStartBlock());
670 DCHECK(next->previous_ != NULL);
671 HInstruction* prev = next->previous();
673 next->previous_ = this;
676 SetBlock(next->block());
677 if (!has_position() && next->has_position()) {
678 set_position(next->position());
683 void HInstruction::InsertAfter(HInstruction* previous) {
685 DCHECK(!previous->IsControlInstruction());
686 DCHECK(!IsControlInstruction() || previous->next_ == NULL);
687 HBasicBlock* block = previous->block();
688 // Never insert anything except constants into the start block after finishing
690 if (block->IsStartBlock() && block->IsFinished() && !IsConstant()) {
691 DCHECK(block->end()->SecondSuccessor() == NULL);
692 InsertAfter(block->end()->FirstSuccessor()->first());
696 // If we're inserting after an instruction with side-effects that is
697 // followed by a simulate instruction, we need to insert after the
698 // simulate instruction instead.
699 HInstruction* next = previous->next_;
700 if (previous->HasObservableSideEffects() && next != NULL) {
701 DCHECK(next->IsSimulate());
703 next = previous->next_;
706 previous_ = previous;
709 previous->next_ = this;
710 if (next != NULL) next->previous_ = this;
711 if (block->last() == previous) {
712 block->set_last(this);
714 if (!has_position() && previous->has_position()) {
715 set_position(previous->position());
720 bool HInstruction::Dominates(HInstruction* other) {
721 if (block() != other->block()) {
722 return block()->Dominates(other->block());
724 // Both instructions are in the same basic block. This instruction
725 // should precede the other one in order to dominate it.
726 for (HInstruction* instr = next(); instr != NULL; instr = instr->next()) {
727 if (instr == other) {
736 void HInstruction::Verify() {
737 // Verify that input operands are defined before use.
738 HBasicBlock* cur_block = block();
739 for (int i = 0; i < OperandCount(); ++i) {
740 HValue* other_operand = OperandAt(i);
741 if (other_operand == NULL) continue;
742 HBasicBlock* other_block = other_operand->block();
743 if (cur_block == other_block) {
744 if (!other_operand->IsPhi()) {
745 HInstruction* cur = this->previous();
746 while (cur != NULL) {
747 if (cur == other_operand) break;
748 cur = cur->previous();
750 // Must reach other operand in the same block!
751 DCHECK(cur == other_operand);
754 // If the following assert fires, you may have forgotten an
756 DCHECK(other_block->Dominates(cur_block));
760 // Verify that instructions that may have side-effects are followed
761 // by a simulate instruction.
762 if (HasObservableSideEffects() && !IsOsrEntry()) {
763 DCHECK(next()->IsSimulate());
766 // Verify that instructions that can be eliminated by GVN have overridden
767 // HValue::DataEquals. The default implementation is UNREACHABLE. We
768 // don't actually care whether DataEquals returns true or false here.
769 if (CheckFlag(kUseGVN)) DataEquals(this);
771 // Verify that all uses are in the graph.
772 for (HUseIterator use = uses(); !use.Done(); use.Advance()) {
773 if (use.value()->IsInstruction()) {
774 DCHECK(HInstruction::cast(use.value())->IsLinked());
781 bool HInstruction::CanDeoptimize() {
782 // TODO(titzer): make this a virtual method?
784 case HValue::kAbnormalExit:
785 case HValue::kAccessArgumentsAt:
786 case HValue::kAllocate:
787 case HValue::kArgumentsElements:
788 case HValue::kArgumentsLength:
789 case HValue::kArgumentsObject:
790 case HValue::kBlockEntry:
791 case HValue::kBoundsCheckBaseIndexInformation:
792 case HValue::kCallFunction:
793 case HValue::kCallNew:
794 case HValue::kCallNewArray:
795 case HValue::kCallStub:
796 case HValue::kCallWithDescriptor:
797 case HValue::kCapturedObject:
798 case HValue::kClassOfTestAndBranch:
799 case HValue::kCompareGeneric:
800 case HValue::kCompareHoleAndBranch:
801 case HValue::kCompareMap:
802 case HValue::kCompareMinusZeroAndBranch:
803 case HValue::kCompareNumericAndBranch:
804 case HValue::kCompareObjectEqAndBranch:
805 case HValue::kConstant:
806 case HValue::kConstructDouble:
807 case HValue::kContext:
808 case HValue::kDebugBreak:
809 case HValue::kDeclareGlobals:
810 case HValue::kDoubleBits:
811 case HValue::kDummyUse:
812 case HValue::kEnterInlined:
813 case HValue::kEnvironmentMarker:
814 case HValue::kForceRepresentation:
815 case HValue::kGetCachedArrayIndex:
817 case HValue::kHasCachedArrayIndexAndBranch:
818 case HValue::kHasInstanceTypeAndBranch:
819 case HValue::kInnerAllocatedObject:
820 case HValue::kInstanceOf:
821 case HValue::kInstanceOfKnownGlobal:
822 case HValue::kIsConstructCallAndBranch:
823 case HValue::kIsObjectAndBranch:
824 case HValue::kIsSmiAndBranch:
825 case HValue::kIsStringAndBranch:
826 case HValue::kIsUndetectableAndBranch:
827 case HValue::kLeaveInlined:
828 case HValue::kLoadFieldByIndex:
829 case HValue::kLoadGlobalGeneric:
830 case HValue::kLoadNamedField:
831 case HValue::kLoadNamedGeneric:
832 case HValue::kLoadRoot:
833 case HValue::kMapEnumLength:
834 case HValue::kMathMinMax:
835 case HValue::kParameter:
837 case HValue::kPushArguments:
838 case HValue::kRegExpLiteral:
839 case HValue::kReturn:
840 case HValue::kSeqStringGetChar:
841 case HValue::kStoreCodeEntry:
842 case HValue::kStoreFrameContext:
843 case HValue::kStoreNamedField:
844 case HValue::kStoreNamedGeneric:
845 case HValue::kStringCharCodeAt:
846 case HValue::kStringCharFromCode:
847 case HValue::kThisFunction:
848 case HValue::kTypeofIsAndBranch:
849 case HValue::kUnknownOSRValue:
850 case HValue::kUseConst:
851 case HValue::kNullarySIMDOperation:
854 case HValue::kStoreKeyed:
855 return !CpuFeatures::SupportsSIMD128InCrankshaft() &&
856 IsSIMD128ElementsKind(HStoreKeyed::cast(this)->elements_kind());
859 case HValue::kAllocateBlockContext:
860 case HValue::kApplyArguments:
861 case HValue::kBitwise:
862 case HValue::kBoundsCheck:
863 case HValue::kBranch:
864 case HValue::kCallJSFunction:
865 case HValue::kCallRuntime:
866 case HValue::kChange:
867 case HValue::kCheckHeapObject:
868 case HValue::kCheckInstanceType:
869 case HValue::kCheckMapValue:
870 case HValue::kCheckMaps:
871 case HValue::kCheckSmi:
872 case HValue::kCheckValue:
873 case HValue::kClampToUint8:
874 case HValue::kDateField:
875 case HValue::kDeoptimize:
877 case HValue::kForInCacheArray:
878 case HValue::kForInPrepareMap:
879 case HValue::kFunctionLiteral:
880 case HValue::kInvokeFunction:
881 case HValue::kLoadContextSlot:
882 case HValue::kLoadFunctionPrototype:
883 case HValue::kLoadGlobalCell:
884 case HValue::kLoadKeyed:
885 case HValue::kLoadKeyedGeneric:
886 case HValue::kMathFloorOfDiv:
889 case HValue::kOsrEntry:
893 case HValue::kSeqStringSetChar:
896 case HValue::kSimulate:
897 case HValue::kStackCheck:
898 case HValue::kStoreContextSlot:
899 case HValue::kStoreGlobalCell:
900 case HValue::kStoreKeyedGeneric:
901 case HValue::kStringAdd:
902 case HValue::kStringCompareAndBranch:
904 case HValue::kToFastProperties:
905 case HValue::kTransitionElementsKind:
906 case HValue::kTrapAllocationMemento:
907 case HValue::kTypeof:
908 case HValue::kUnaryMathOperation:
909 case HValue::kWrapReceiver:
910 case HValue::kUnarySIMDOperation:
911 case HValue::kBinarySIMDOperation:
912 case HValue::kTernarySIMDOperation:
913 case HValue::kQuarternarySIMDOperation:
921 OStream& operator<<(OStream& os, const NameOf& v) {
922 return os << v.value->representation().Mnemonic() << v.value->id();
925 OStream& HDummyUse::PrintDataTo(OStream& os) const { // NOLINT
926 return os << NameOf(value());
930 OStream& HEnvironmentMarker::PrintDataTo(OStream& os) const { // NOLINT
931 return os << (kind() == BIND ? "bind" : "lookup") << " var[" << index()
936 OStream& HUnaryCall::PrintDataTo(OStream& os) const { // NOLINT
937 return os << NameOf(value()) << " #" << argument_count();
941 OStream& HCallJSFunction::PrintDataTo(OStream& os) const { // NOLINT
942 return os << NameOf(function()) << " #" << argument_count();
946 HCallJSFunction* HCallJSFunction::New(
951 bool pass_argument_count) {
952 bool has_stack_check = false;
953 if (function->IsConstant()) {
954 HConstant* fun_const = HConstant::cast(function);
955 Handle<JSFunction> jsfun =
956 Handle<JSFunction>::cast(fun_const->handle(zone->isolate()));
957 has_stack_check = !jsfun.is_null() &&
958 (jsfun->code()->kind() == Code::FUNCTION ||
959 jsfun->code()->kind() == Code::OPTIMIZED_FUNCTION);
962 return new(zone) HCallJSFunction(
963 function, argument_count, pass_argument_count,
968 OStream& HBinaryCall::PrintDataTo(OStream& os) const { // NOLINT
969 return os << NameOf(first()) << " " << NameOf(second()) << " #"
974 void HBoundsCheck::ApplyIndexChange() {
975 if (skip_check()) return;
977 DecompositionResult decomposition;
978 bool index_is_decomposable = index()->TryDecompose(&decomposition);
979 if (index_is_decomposable) {
980 DCHECK(decomposition.base() == base());
981 if (decomposition.offset() == offset() &&
982 decomposition.scale() == scale()) return;
987 ReplaceAllUsesWith(index());
989 HValue* current_index = decomposition.base();
990 int actual_offset = decomposition.offset() + offset();
991 int actual_scale = decomposition.scale() + scale();
993 Zone* zone = block()->graph()->zone();
994 HValue* context = block()->graph()->GetInvalidContext();
995 if (actual_offset != 0) {
996 HConstant* add_offset = HConstant::New(zone, context, actual_offset);
997 add_offset->InsertBefore(this);
998 HInstruction* add = HAdd::New(zone, context,
999 current_index, add_offset);
1000 add->InsertBefore(this);
1001 add->AssumeRepresentation(index()->representation());
1002 add->ClearFlag(kCanOverflow);
1003 current_index = add;
1006 if (actual_scale != 0) {
1007 HConstant* sar_scale = HConstant::New(zone, context, actual_scale);
1008 sar_scale->InsertBefore(this);
1009 HInstruction* sar = HSar::New(zone, context,
1010 current_index, sar_scale);
1011 sar->InsertBefore(this);
1012 sar->AssumeRepresentation(index()->representation());
1013 current_index = sar;
1016 SetOperandAt(0, current_index);
1024 OStream& HBoundsCheck::PrintDataTo(OStream& os) const { // NOLINT
1025 os << NameOf(index()) << " " << NameOf(length());
1026 if (base() != NULL && (offset() != 0 || scale() != 0)) {
1028 if (base() != index()) {
1029 os << NameOf(index());
1033 os << " + " << offset() << ") >> " << scale() << ")";
1035 if (skip_check()) os << " [DISABLED]";
1040 void HBoundsCheck::InferRepresentation(HInferRepresentationPhase* h_infer) {
1041 DCHECK(CheckFlag(kFlexibleRepresentation));
1042 HValue* actual_index = index()->ActualValue();
1043 HValue* actual_length = length()->ActualValue();
1044 Representation index_rep = actual_index->representation();
1045 Representation length_rep = actual_length->representation();
1046 if (index_rep.IsTagged() && actual_index->type().IsSmi()) {
1047 index_rep = Representation::Smi();
1049 if (length_rep.IsTagged() && actual_length->type().IsSmi()) {
1050 length_rep = Representation::Smi();
1052 Representation r = index_rep.generalize(length_rep);
1053 if (r.is_more_general_than(Representation::Integer32())) {
1054 r = Representation::Integer32();
1056 UpdateRepresentation(r, h_infer, "boundscheck");
1060 Range* HBoundsCheck::InferRange(Zone* zone) {
1061 Representation r = representation();
1062 if (r.IsSmiOrInteger32() && length()->HasRange()) {
1063 int upper = length()->range()->upper() - (allow_equality() ? 0 : 1);
1066 Range* result = new(zone) Range(lower, upper);
1067 if (index()->HasRange()) {
1068 result->Intersect(index()->range());
1071 // In case of Smi representation, clamp result to Smi::kMaxValue.
1072 if (r.IsSmi()) result->ClampToSmi();
1075 return HValue::InferRange(zone);
1079 OStream& HBoundsCheckBaseIndexInformation::PrintDataTo(
1080 OStream& os) const { // NOLINT
1081 // TODO(svenpanne) This 2nd base_index() looks wrong...
1082 return os << "base: " << NameOf(base_index())
1083 << ", check: " << NameOf(base_index());
1087 OStream& HCallWithDescriptor::PrintDataTo(OStream& os) const { // NOLINT
1088 for (int i = 0; i < OperandCount(); i++) {
1089 os << NameOf(OperandAt(i)) << " ";
1091 return os << "#" << argument_count();
1095 OStream& HCallNewArray::PrintDataTo(OStream& os) const { // NOLINT
1096 os << ElementsKindToString(elements_kind()) << " ";
1097 return HBinaryCall::PrintDataTo(os);
1101 OStream& HCallRuntime::PrintDataTo(OStream& os) const { // NOLINT
1102 os << name()->ToCString().get() << " ";
1103 if (save_doubles() == kSaveFPRegs) os << "[save doubles] ";
1104 return os << "#" << argument_count();
1108 OStream& HClassOfTestAndBranch::PrintDataTo(OStream& os) const { // NOLINT
1109 return os << "class_of_test(" << NameOf(value()) << ", \""
1110 << class_name()->ToCString().get() << "\")";
1114 OStream& HWrapReceiver::PrintDataTo(OStream& os) const { // NOLINT
1115 return os << NameOf(receiver()) << " " << NameOf(function());
1119 OStream& HAccessArgumentsAt::PrintDataTo(OStream& os) const { // NOLINT
1120 return os << NameOf(arguments()) << "[" << NameOf(index()) << "], length "
1121 << NameOf(length());
1125 OStream& HAllocateBlockContext::PrintDataTo(OStream& os) const { // NOLINT
1126 return os << NameOf(context()) << " " << NameOf(function());
1130 OStream& HControlInstruction::PrintDataTo(OStream& os) const { // NOLINT
1132 bool first_block = true;
1133 for (HSuccessorIterator it(this); !it.Done(); it.Advance()) {
1134 if (!first_block) os << ", ";
1135 os << *it.Current();
1136 first_block = false;
1142 OStream& HUnaryControlInstruction::PrintDataTo(OStream& os) const { // NOLINT
1143 os << NameOf(value());
1144 return HControlInstruction::PrintDataTo(os);
1148 OStream& HReturn::PrintDataTo(OStream& os) const { // NOLINT
1149 return os << NameOf(value()) << " (pop " << NameOf(parameter_count())
1154 Representation HBranch::observed_input_representation(int index) {
1155 static const ToBooleanStub::Types tagged_types(
1156 ToBooleanStub::NULL_TYPE |
1157 ToBooleanStub::SPEC_OBJECT |
1158 ToBooleanStub::STRING |
1159 ToBooleanStub::SYMBOL);
1160 if (expected_input_types_.ContainsAnyOf(tagged_types)) {
1161 return Representation::Tagged();
1163 if (expected_input_types_.Contains(ToBooleanStub::UNDEFINED)) {
1164 if (expected_input_types_.Contains(ToBooleanStub::HEAP_NUMBER)) {
1165 return Representation::Double();
1167 return Representation::Tagged();
1169 if (expected_input_types_.Contains(ToBooleanStub::HEAP_NUMBER)) {
1170 return Representation::Double();
1172 if (expected_input_types_.Contains(ToBooleanStub::SMI)) {
1173 return Representation::Smi();
1175 return Representation::None();
1179 bool HBranch::KnownSuccessorBlock(HBasicBlock** block) {
1180 HValue* value = this->value();
1181 if (value->EmitAtUses()) {
1182 DCHECK(value->IsConstant());
1183 DCHECK(!value->representation().IsDouble());
1184 *block = HConstant::cast(value)->BooleanValue()
1186 : SecondSuccessor();
1194 OStream& HBranch::PrintDataTo(OStream& os) const { // NOLINT
1195 return HUnaryControlInstruction::PrintDataTo(os) << " "
1196 << expected_input_types();
1200 OStream& HCompareMap::PrintDataTo(OStream& os) const { // NOLINT
1201 os << NameOf(value()) << " (" << *map().handle() << ")";
1202 HControlInstruction::PrintDataTo(os);
1203 if (known_successor_index() == 0) {
1205 } else if (known_successor_index() == 1) {
1212 const char* HUnaryMathOperation::OpName() const {
1239 Range* HUnaryMathOperation::InferRange(Zone* zone) {
1240 Representation r = representation();
1241 if (op() == kMathClz32) return new(zone) Range(0, 32);
1242 if (r.IsSmiOrInteger32() && value()->HasRange()) {
1243 if (op() == kMathAbs) {
1244 int upper = value()->range()->upper();
1245 int lower = value()->range()->lower();
1246 bool spans_zero = value()->range()->CanBeZero();
1247 // Math.abs(kMinInt) overflows its representation, on which the
1248 // instruction deopts. Hence clamp it to kMaxInt.
1249 int abs_upper = upper == kMinInt ? kMaxInt : abs(upper);
1250 int abs_lower = lower == kMinInt ? kMaxInt : abs(lower);
1252 new(zone) Range(spans_zero ? 0 : Min(abs_lower, abs_upper),
1253 Max(abs_lower, abs_upper));
1254 // In case of Smi representation, clamp Math.abs(Smi::kMinValue) to
1256 if (r.IsSmi()) result->ClampToSmi();
1260 return HValue::InferRange(zone);
1264 OStream& HUnaryMathOperation::PrintDataTo(OStream& os) const { // NOLINT
1265 return os << OpName() << " " << NameOf(value());
1269 OStream& HUnaryOperation::PrintDataTo(OStream& os) const { // NOLINT
1270 return os << NameOf(value());
1274 OStream& HHasInstanceTypeAndBranch::PrintDataTo(OStream& os) const { // NOLINT
1275 os << NameOf(value());
1277 case FIRST_JS_RECEIVER_TYPE:
1278 if (to_ == LAST_TYPE) os << " spec_object";
1280 case JS_REGEXP_TYPE:
1281 if (to_ == JS_REGEXP_TYPE) os << " reg_exp";
1284 if (to_ == JS_ARRAY_TYPE) os << " array";
1286 case JS_FUNCTION_TYPE:
1287 if (to_ == JS_FUNCTION_TYPE) os << " function";
1296 OStream& HTypeofIsAndBranch::PrintDataTo(OStream& os) const { // NOLINT
1297 os << NameOf(value()) << " == " << type_literal()->ToCString().get();
1298 return HControlInstruction::PrintDataTo(os);
1302 static String* TypeOfString(HConstant* constant, Isolate* isolate) {
1303 Heap* heap = isolate->heap();
1304 if (constant->HasNumberValue()) return heap->number_string();
1305 if (constant->IsUndetectable()) return heap->undefined_string();
1306 if (constant->HasStringValue()) return heap->string_string();
1307 switch (constant->GetInstanceType()) {
1308 case ODDBALL_TYPE: {
1309 Unique<Object> unique = constant->GetUnique();
1310 if (unique.IsKnownGlobal(heap->true_value()) ||
1311 unique.IsKnownGlobal(heap->false_value())) {
1312 return heap->boolean_string();
1314 if (unique.IsKnownGlobal(heap->null_value())) {
1315 return heap->object_string();
1317 DCHECK(unique.IsKnownGlobal(heap->undefined_value()));
1318 return heap->undefined_string();
1321 return heap->symbol_string();
1322 case JS_FUNCTION_TYPE:
1323 case JS_FUNCTION_PROXY_TYPE:
1324 return heap->function_string();
1326 return heap->object_string();
1331 bool HTypeofIsAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
1332 if (FLAG_fold_constants && value()->IsConstant()) {
1333 HConstant* constant = HConstant::cast(value());
1334 String* type_string = TypeOfString(constant, isolate());
1335 bool same_type = type_literal_.IsKnownGlobal(type_string);
1336 *block = same_type ? FirstSuccessor() : SecondSuccessor();
1338 } else if (value()->representation().IsSpecialization()) {
1340 type_literal_.IsKnownGlobal(isolate()->heap()->number_string());
1341 *block = number_type ? FirstSuccessor() : SecondSuccessor();
1343 } else if (value()->representation().IsFloat32x4()) {
1344 bool float32x4_type =
1345 type_literal_.IsKnownGlobal(isolate()->heap()->float32x4_string());
1346 *block = float32x4_type ? FirstSuccessor() : SecondSuccessor();
1348 } else if (value()->representation().IsFloat64x2()) {
1349 bool float64x2_type =
1350 type_literal_.IsKnownGlobal(isolate()->heap()->float64x2_string());
1351 *block = float64x2_type ? FirstSuccessor() : SecondSuccessor();
1353 } else if (value()->representation().IsInt32x4()) {
1355 type_literal_.IsKnownGlobal(isolate()->heap()->int32x4_string());
1356 *block = int32x4_type ? FirstSuccessor() : SecondSuccessor();
1365 OStream& HCheckMapValue::PrintDataTo(OStream& os) const { // NOLINT
1366 return os << NameOf(value()) << " " << NameOf(map());
1370 HValue* HCheckMapValue::Canonicalize() {
1371 if (map()->IsConstant()) {
1372 HConstant* c_map = HConstant::cast(map());
1373 return HCheckMaps::CreateAndInsertAfter(
1374 block()->graph()->zone(), value(), c_map->MapValue(),
1375 c_map->HasStableMapValue(), this);
1381 OStream& HForInPrepareMap::PrintDataTo(OStream& os) const { // NOLINT
1382 return os << NameOf(enumerable());
1386 OStream& HForInCacheArray::PrintDataTo(OStream& os) const { // NOLINT
1387 return os << NameOf(enumerable()) << " " << NameOf(map()) << "[" << idx_
1392 OStream& HLoadFieldByIndex::PrintDataTo(OStream& os) const { // NOLINT
1393 return os << NameOf(object()) << " " << NameOf(index());
1397 static bool MatchLeftIsOnes(HValue* l, HValue* r, HValue** negated) {
1398 if (!l->EqualsInteger32Constant(~0)) return false;
1404 static bool MatchNegationViaXor(HValue* instr, HValue** negated) {
1405 if (!instr->IsBitwise()) return false;
1406 HBitwise* b = HBitwise::cast(instr);
1407 return (b->op() == Token::BIT_XOR) &&
1408 (MatchLeftIsOnes(b->left(), b->right(), negated) ||
1409 MatchLeftIsOnes(b->right(), b->left(), negated));
1413 static bool MatchDoubleNegation(HValue* instr, HValue** arg) {
1415 return MatchNegationViaXor(instr, &negated) &&
1416 MatchNegationViaXor(negated, arg);
1420 HValue* HBitwise::Canonicalize() {
1421 if (!representation().IsSmiOrInteger32()) return this;
1422 // If x is an int32, then x & -1 == x, x | 0 == x and x ^ 0 == x.
1423 int32_t nop_constant = (op() == Token::BIT_AND) ? -1 : 0;
1424 if (left()->EqualsInteger32Constant(nop_constant) &&
1425 !right()->CheckFlag(kUint32)) {
1428 if (right()->EqualsInteger32Constant(nop_constant) &&
1429 !left()->CheckFlag(kUint32)) {
1432 // Optimize double negation, a common pattern used for ToInt32(x).
1434 if (MatchDoubleNegation(this, &arg) && !arg->CheckFlag(kUint32)) {
1441 Representation HAdd::RepresentationFromInputs() {
1442 Representation left_rep = left()->representation();
1443 if (left_rep.IsExternal()) {
1444 return Representation::External();
1446 return HArithmeticBinaryOperation::RepresentationFromInputs();
1450 Representation HAdd::RequiredInputRepresentation(int index) {
1452 Representation left_rep = left()->representation();
1453 if (left_rep.IsExternal()) {
1454 return Representation::Integer32();
1457 return HArithmeticBinaryOperation::RequiredInputRepresentation(index);
1461 static bool IsIdentityOperation(HValue* arg1, HValue* arg2, int32_t identity) {
1462 return arg1->representation().IsSpecialization() &&
1463 arg2->EqualsInteger32Constant(identity);
1467 HValue* HAdd::Canonicalize() {
1468 // Adding 0 is an identity operation except in case of -0: -0 + 0 = +0
1469 if (IsIdentityOperation(left(), right(), 0) &&
1470 !left()->representation().IsDouble()) { // Left could be -0.
1473 if (IsIdentityOperation(right(), left(), 0) &&
1474 !left()->representation().IsDouble()) { // Right could be -0.
1481 HValue* HSub::Canonicalize() {
1482 if (IsIdentityOperation(left(), right(), 0)) return left();
1487 HValue* HMul::Canonicalize() {
1488 if (IsIdentityOperation(left(), right(), 1)) return left();
1489 if (IsIdentityOperation(right(), left(), 1)) return right();
1494 bool HMul::MulMinusOne() {
1495 if (left()->EqualsInteger32Constant(-1) ||
1496 right()->EqualsInteger32Constant(-1)) {
1504 HValue* HMod::Canonicalize() {
1509 HValue* HDiv::Canonicalize() {
1510 if (IsIdentityOperation(left(), right(), 1)) return left();
1515 HValue* HChange::Canonicalize() {
1516 return (from().Equals(to())) ? value() : this;
1520 HValue* HWrapReceiver::Canonicalize() {
1521 if (HasNoUses()) return NULL;
1522 if (receiver()->type().IsJSObject()) {
1529 OStream& HTypeof::PrintDataTo(OStream& os) const { // NOLINT
1530 return os << NameOf(value());
1534 HInstruction* HForceRepresentation::New(Zone* zone, HValue* context,
1535 HValue* value, Representation representation) {
1536 if (FLAG_fold_constants && value->IsConstant()) {
1537 HConstant* c = HConstant::cast(value);
1538 if (c->HasNumberValue()) {
1539 double double_res = c->DoubleValue();
1540 if (representation.IsDouble()) {
1541 return HConstant::New(zone, context, double_res);
1543 } else if (representation.CanContainDouble(double_res)) {
1544 return HConstant::New(zone, context,
1545 static_cast<int32_t>(double_res),
1550 return new(zone) HForceRepresentation(value, representation);
1554 OStream& HForceRepresentation::PrintDataTo(OStream& os) const { // NOLINT
1555 return os << representation().Mnemonic() << " " << NameOf(value());
1559 OStream& HChange::PrintDataTo(OStream& os) const { // NOLINT
1560 HUnaryOperation::PrintDataTo(os);
1561 os << " " << from().Mnemonic() << " to " << to().Mnemonic();
1563 if (CanTruncateToSmi()) os << " truncating-smi";
1564 if (CanTruncateToInt32()) os << " truncating-int32";
1565 if (CheckFlag(kBailoutOnMinusZero)) os << " -0?";
1566 if (CheckFlag(kAllowUndefinedAsNaN)) os << " allow-undefined-as-nan";
1571 HValue* HUnaryMathOperation::Canonicalize() {
1572 if (op() == kMathRound || op() == kMathFloor) {
1573 HValue* val = value();
1574 if (val->IsChange()) val = HChange::cast(val)->value();
1575 if (val->representation().IsSmiOrInteger32()) {
1576 if (val->representation().Equals(representation())) return val;
1577 return Prepend(new(block()->zone()) HChange(
1578 val, representation(), false, false));
1581 if (op() == kMathFloor && value()->IsDiv() && value()->HasOneUse()) {
1582 HDiv* hdiv = HDiv::cast(value());
1584 HValue* left = hdiv->left();
1585 if (left->representation().IsInteger32()) {
1586 // A value with an integer representation does not need to be transformed.
1587 } else if (left->IsChange() && HChange::cast(left)->from().IsInteger32()) {
1588 // A change from an integer32 can be replaced by the integer32 value.
1589 left = HChange::cast(left)->value();
1590 } else if (hdiv->observed_input_representation(1).IsSmiOrInteger32()) {
1591 left = Prepend(new(block()->zone()) HChange(
1592 left, Representation::Integer32(), false, false));
1597 HValue* right = hdiv->right();
1598 if (right->IsInteger32Constant()) {
1599 right = Prepend(HConstant::cast(right)->CopyToRepresentation(
1600 Representation::Integer32(), right->block()->zone()));
1601 } else if (right->representation().IsInteger32()) {
1602 // A value with an integer representation does not need to be transformed.
1603 } else if (right->IsChange() &&
1604 HChange::cast(right)->from().IsInteger32()) {
1605 // A change from an integer32 can be replaced by the integer32 value.
1606 right = HChange::cast(right)->value();
1607 } else if (hdiv->observed_input_representation(2).IsSmiOrInteger32()) {
1608 right = Prepend(new(block()->zone()) HChange(
1609 right, Representation::Integer32(), false, false));
1614 return Prepend(HMathFloorOfDiv::New(
1615 block()->zone(), context(), left, right));
1621 HValue* HCheckInstanceType::Canonicalize() {
1622 if ((check_ == IS_SPEC_OBJECT && value()->type().IsJSObject()) ||
1623 (check_ == IS_JS_ARRAY && value()->type().IsJSArray()) ||
1624 (check_ == IS_STRING && value()->type().IsString())) {
1628 if (check_ == IS_INTERNALIZED_STRING && value()->IsConstant()) {
1629 if (HConstant::cast(value())->HasInternalizedStringValue()) {
1637 void HCheckInstanceType::GetCheckInterval(InstanceType* first,
1638 InstanceType* last) {
1639 DCHECK(is_interval_check());
1641 case IS_SPEC_OBJECT:
1642 *first = FIRST_SPEC_OBJECT_TYPE;
1643 *last = LAST_SPEC_OBJECT_TYPE;
1646 *first = *last = JS_ARRAY_TYPE;
1654 void HCheckInstanceType::GetCheckMaskAndTag(uint8_t* mask, uint8_t* tag) {
1655 DCHECK(!is_interval_check());
1658 *mask = kIsNotStringMask;
1661 case IS_INTERNALIZED_STRING:
1662 *mask = kIsNotStringMask | kIsNotInternalizedMask;
1663 *tag = kInternalizedTag;
1671 OStream& HCheckMaps::PrintDataTo(OStream& os) const { // NOLINT
1672 os << NameOf(value()) << " [" << *maps()->at(0).handle();
1673 for (int i = 1; i < maps()->size(); ++i) {
1674 os << "," << *maps()->at(i).handle();
1677 if (IsStabilityCheck()) os << "(stability-check)";
1682 HValue* HCheckMaps::Canonicalize() {
1683 if (!IsStabilityCheck() && maps_are_stable() && value()->IsConstant()) {
1684 HConstant* c_value = HConstant::cast(value());
1685 if (c_value->HasObjectMap()) {
1686 for (int i = 0; i < maps()->size(); ++i) {
1687 if (c_value->ObjectMap() == maps()->at(i)) {
1688 if (maps()->size() > 1) {
1689 set_maps(new(block()->graph()->zone()) UniqueSet<Map>(
1690 maps()->at(i), block()->graph()->zone()));
1692 MarkAsStabilityCheck();
1702 OStream& HCheckValue::PrintDataTo(OStream& os) const { // NOLINT
1703 return os << NameOf(value()) << " " << Brief(*object().handle());
1707 HValue* HCheckValue::Canonicalize() {
1708 return (value()->IsConstant() &&
1709 HConstant::cast(value())->EqualsUnique(object_)) ? NULL : this;
1713 const char* HCheckInstanceType::GetCheckName() const {
1715 case IS_SPEC_OBJECT: return "object";
1716 case IS_JS_ARRAY: return "array";
1717 case IS_STRING: return "string";
1718 case IS_INTERNALIZED_STRING: return "internalized_string";
1725 OStream& HCheckInstanceType::PrintDataTo(OStream& os) const { // NOLINT
1726 os << GetCheckName() << " ";
1727 return HUnaryOperation::PrintDataTo(os);
1731 OStream& HCallStub::PrintDataTo(OStream& os) const { // NOLINT
1732 os << CodeStub::MajorName(major_key_, false) << " ";
1733 return HUnaryCall::PrintDataTo(os);
1737 OStream& HUnknownOSRValue::PrintDataTo(OStream& os) const { // NOLINT
1738 const char* type = "expression";
1739 if (environment_->is_local_index(index_)) type = "local";
1740 if (environment_->is_special_index(index_)) type = "special";
1741 if (environment_->is_parameter_index(index_)) type = "parameter";
1742 return os << type << " @ " << index_;
1746 OStream& HInstanceOf::PrintDataTo(OStream& os) const { // NOLINT
1747 return os << NameOf(left()) << " " << NameOf(right()) << " "
1748 << NameOf(context());
1752 Range* HValue::InferRange(Zone* zone) {
1754 if (representation().IsSmi() || type().IsSmi()) {
1755 result = new(zone) Range(Smi::kMinValue, Smi::kMaxValue);
1756 result->set_can_be_minus_zero(false);
1758 result = new(zone) Range();
1759 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32));
1760 // TODO(jkummerow): The range cannot be minus zero when the upper type
1761 // bound is Integer32.
1767 Range* HChange::InferRange(Zone* zone) {
1768 Range* input_range = value()->range();
1769 if (from().IsInteger32() && !value()->CheckFlag(HInstruction::kUint32) &&
1772 input_range != NULL &&
1773 input_range->IsInSmiRange()))) {
1774 set_type(HType::Smi());
1775 ClearChangesFlag(kNewSpacePromotion);
1777 if (to().IsSmiOrTagged() &&
1778 input_range != NULL &&
1779 input_range->IsInSmiRange() &&
1780 (!SmiValuesAre32Bits() ||
1781 !value()->CheckFlag(HValue::kUint32) ||
1782 input_range->upper() != kMaxInt)) {
1783 // The Range class can't express upper bounds in the (kMaxInt, kMaxUint32]
1784 // interval, so we treat kMaxInt as a sentinel for this entire interval.
1785 ClearFlag(kCanOverflow);
1787 Range* result = (input_range != NULL)
1788 ? input_range->Copy(zone)
1789 : HValue::InferRange(zone);
1790 result->set_can_be_minus_zero(!to().IsSmiOrInteger32() ||
1791 !(CheckFlag(kAllUsesTruncatingToInt32) ||
1792 CheckFlag(kAllUsesTruncatingToSmi)));
1793 if (to().IsSmi()) result->ClampToSmi();
1798 Range* HConstant::InferRange(Zone* zone) {
1799 if (has_int32_value_) {
1800 Range* result = new(zone) Range(int32_value_, int32_value_);
1801 result->set_can_be_minus_zero(false);
1804 return HValue::InferRange(zone);
1808 HSourcePosition HPhi::position() const {
1809 return block()->first()->position();
1813 Range* HPhi::InferRange(Zone* zone) {
1814 Representation r = representation();
1815 if (r.IsSmiOrInteger32()) {
1816 if (block()->IsLoopHeader()) {
1817 Range* range = r.IsSmi()
1818 ? new(zone) Range(Smi::kMinValue, Smi::kMaxValue)
1819 : new(zone) Range(kMinInt, kMaxInt);
1822 Range* range = OperandAt(0)->range()->Copy(zone);
1823 for (int i = 1; i < OperandCount(); ++i) {
1824 range->Union(OperandAt(i)->range());
1829 return HValue::InferRange(zone);
1834 Range* HAdd::InferRange(Zone* zone) {
1835 Representation r = representation();
1836 if (r.IsSmiOrInteger32()) {
1837 Range* a = left()->range();
1838 Range* b = right()->range();
1839 Range* res = a->Copy(zone);
1840 if (!res->AddAndCheckOverflow(r, b) ||
1841 (r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1842 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) {
1843 ClearFlag(kCanOverflow);
1845 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1846 !CheckFlag(kAllUsesTruncatingToInt32) &&
1847 a->CanBeMinusZero() && b->CanBeMinusZero());
1850 return HValue::InferRange(zone);
1855 Range* HSub::InferRange(Zone* zone) {
1856 Representation r = representation();
1857 if (r.IsSmiOrInteger32()) {
1858 Range* a = left()->range();
1859 Range* b = right()->range();
1860 Range* res = a->Copy(zone);
1861 if (!res->SubAndCheckOverflow(r, b) ||
1862 (r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1863 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) {
1864 ClearFlag(kCanOverflow);
1866 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1867 !CheckFlag(kAllUsesTruncatingToInt32) &&
1868 a->CanBeMinusZero() && b->CanBeZero());
1871 return HValue::InferRange(zone);
1876 Range* HMul::InferRange(Zone* zone) {
1877 Representation r = representation();
1878 if (r.IsSmiOrInteger32()) {
1879 Range* a = left()->range();
1880 Range* b = right()->range();
1881 Range* res = a->Copy(zone);
1882 if (!res->MulAndCheckOverflow(r, b) ||
1883 (((r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1884 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) &&
1886 // Truncated int multiplication is too precise and therefore not the
1887 // same as converting to Double and back.
1888 // Handle truncated integer multiplication by -1 special.
1889 ClearFlag(kCanOverflow);
1891 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1892 !CheckFlag(kAllUsesTruncatingToInt32) &&
1893 ((a->CanBeZero() && b->CanBeNegative()) ||
1894 (a->CanBeNegative() && b->CanBeZero())));
1897 return HValue::InferRange(zone);
1902 Range* HDiv::InferRange(Zone* zone) {
1903 if (representation().IsInteger32()) {
1904 Range* a = left()->range();
1905 Range* b = right()->range();
1906 Range* result = new(zone) Range();
1907 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1908 (a->CanBeMinusZero() ||
1909 (a->CanBeZero() && b->CanBeNegative())));
1910 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1911 ClearFlag(kCanOverflow);
1914 if (!b->CanBeZero()) {
1915 ClearFlag(kCanBeDivByZero);
1919 return HValue::InferRange(zone);
1924 Range* HMathFloorOfDiv::InferRange(Zone* zone) {
1925 if (representation().IsInteger32()) {
1926 Range* a = left()->range();
1927 Range* b = right()->range();
1928 Range* result = new(zone) Range();
1929 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1930 (a->CanBeMinusZero() ||
1931 (a->CanBeZero() && b->CanBeNegative())));
1932 if (!a->Includes(kMinInt)) {
1933 ClearFlag(kLeftCanBeMinInt);
1936 if (!a->CanBeNegative()) {
1937 ClearFlag(HValue::kLeftCanBeNegative);
1940 if (!a->CanBePositive()) {
1941 ClearFlag(HValue::kLeftCanBePositive);
1944 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1945 ClearFlag(kCanOverflow);
1948 if (!b->CanBeZero()) {
1949 ClearFlag(kCanBeDivByZero);
1953 return HValue::InferRange(zone);
1958 // Returns the absolute value of its argument minus one, avoiding undefined
1959 // behavior at kMinInt.
1960 static int32_t AbsMinus1(int32_t a) { return a < 0 ? -(a + 1) : (a - 1); }
1963 Range* HMod::InferRange(Zone* zone) {
1964 if (representation().IsInteger32()) {
1965 Range* a = left()->range();
1966 Range* b = right()->range();
1968 // The magnitude of the modulus is bounded by the right operand.
1969 int32_t positive_bound = Max(AbsMinus1(b->lower()), AbsMinus1(b->upper()));
1971 // The result of the modulo operation has the sign of its left operand.
1972 bool left_can_be_negative = a->CanBeMinusZero() || a->CanBeNegative();
1973 Range* result = new(zone) Range(left_can_be_negative ? -positive_bound : 0,
1974 a->CanBePositive() ? positive_bound : 0);
1976 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1977 left_can_be_negative);
1979 if (!a->CanBeNegative()) {
1980 ClearFlag(HValue::kLeftCanBeNegative);
1983 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1984 ClearFlag(HValue::kCanOverflow);
1987 if (!b->CanBeZero()) {
1988 ClearFlag(HValue::kCanBeDivByZero);
1992 return HValue::InferRange(zone);
1997 InductionVariableData* InductionVariableData::ExaminePhi(HPhi* phi) {
1998 if (phi->block()->loop_information() == NULL) return NULL;
1999 if (phi->OperandCount() != 2) return NULL;
2000 int32_t candidate_increment;
2002 candidate_increment = ComputeIncrement(phi, phi->OperandAt(0));
2003 if (candidate_increment != 0) {
2004 return new(phi->block()->graph()->zone())
2005 InductionVariableData(phi, phi->OperandAt(1), candidate_increment);
2008 candidate_increment = ComputeIncrement(phi, phi->OperandAt(1));
2009 if (candidate_increment != 0) {
2010 return new(phi->block()->graph()->zone())
2011 InductionVariableData(phi, phi->OperandAt(0), candidate_increment);
2019 * This function tries to match the following patterns (and all the relevant
2020 * variants related to |, & and + being commutative):
2021 * base | constant_or_mask
2022 * base & constant_and_mask
2023 * (base + constant_offset) & constant_and_mask
2024 * (base - constant_offset) & constant_and_mask
2026 void InductionVariableData::DecomposeBitwise(
2028 BitwiseDecompositionResult* result) {
2029 HValue* base = IgnoreOsrValue(value);
2030 result->base = value;
2032 if (!base->representation().IsInteger32()) return;
2034 if (base->IsBitwise()) {
2035 bool allow_offset = false;
2038 HBitwise* bitwise = HBitwise::cast(base);
2039 if (bitwise->right()->IsInteger32Constant()) {
2040 mask = bitwise->right()->GetInteger32Constant();
2041 base = bitwise->left();
2042 } else if (bitwise->left()->IsInteger32Constant()) {
2043 mask = bitwise->left()->GetInteger32Constant();
2044 base = bitwise->right();
2048 if (bitwise->op() == Token::BIT_AND) {
2049 result->and_mask = mask;
2050 allow_offset = true;
2051 } else if (bitwise->op() == Token::BIT_OR) {
2052 result->or_mask = mask;
2057 result->context = bitwise->context();
2060 if (base->IsAdd()) {
2061 HAdd* add = HAdd::cast(base);
2062 if (add->right()->IsInteger32Constant()) {
2064 } else if (add->left()->IsInteger32Constant()) {
2065 base = add->right();
2067 } else if (base->IsSub()) {
2068 HSub* sub = HSub::cast(base);
2069 if (sub->right()->IsInteger32Constant()) {
2075 result->base = base;
2080 void InductionVariableData::AddCheck(HBoundsCheck* check,
2081 int32_t upper_limit) {
2082 DCHECK(limit_validity() != NULL);
2083 if (limit_validity() != check->block() &&
2084 !limit_validity()->Dominates(check->block())) return;
2085 if (!phi()->block()->current_loop()->IsNestedInThisLoop(
2086 check->block()->current_loop())) return;
2088 ChecksRelatedToLength* length_checks = checks();
2089 while (length_checks != NULL) {
2090 if (length_checks->length() == check->length()) break;
2091 length_checks = length_checks->next();
2093 if (length_checks == NULL) {
2094 length_checks = new(check->block()->zone())
2095 ChecksRelatedToLength(check->length(), checks());
2096 checks_ = length_checks;
2099 length_checks->AddCheck(check, upper_limit);
2103 void InductionVariableData::ChecksRelatedToLength::CloseCurrentBlock() {
2104 if (checks() != NULL) {
2105 InductionVariableCheck* c = checks();
2106 HBasicBlock* current_block = c->check()->block();
2107 while (c != NULL && c->check()->block() == current_block) {
2108 c->set_upper_limit(current_upper_limit_);
2115 void InductionVariableData::ChecksRelatedToLength::UseNewIndexInCurrentBlock(
2120 DCHECK(first_check_in_block() != NULL);
2121 HValue* previous_index = first_check_in_block()->index();
2122 DCHECK(context != NULL);
2124 Zone* zone = index_base->block()->graph()->zone();
2125 set_added_constant(HConstant::New(zone, context, mask));
2126 if (added_index() != NULL) {
2127 added_constant()->InsertBefore(added_index());
2129 added_constant()->InsertBefore(first_check_in_block());
2132 if (added_index() == NULL) {
2133 first_check_in_block()->ReplaceAllUsesWith(first_check_in_block()->index());
2134 HInstruction* new_index = HBitwise::New(zone, context, token, index_base,
2136 DCHECK(new_index->IsBitwise());
2137 new_index->ClearAllSideEffects();
2138 new_index->AssumeRepresentation(Representation::Integer32());
2139 set_added_index(HBitwise::cast(new_index));
2140 added_index()->InsertBefore(first_check_in_block());
2142 DCHECK(added_index()->op() == token);
2144 added_index()->SetOperandAt(1, index_base);
2145 added_index()->SetOperandAt(2, added_constant());
2146 first_check_in_block()->SetOperandAt(0, added_index());
2147 if (previous_index->HasNoUses()) {
2148 previous_index->DeleteAndReplaceWith(NULL);
2152 void InductionVariableData::ChecksRelatedToLength::AddCheck(
2153 HBoundsCheck* check,
2154 int32_t upper_limit) {
2155 BitwiseDecompositionResult decomposition;
2156 InductionVariableData::DecomposeBitwise(check->index(), &decomposition);
2158 if (first_check_in_block() == NULL ||
2159 first_check_in_block()->block() != check->block()) {
2160 CloseCurrentBlock();
2162 first_check_in_block_ = check;
2163 set_added_index(NULL);
2164 set_added_constant(NULL);
2165 current_and_mask_in_block_ = decomposition.and_mask;
2166 current_or_mask_in_block_ = decomposition.or_mask;
2167 current_upper_limit_ = upper_limit;
2169 InductionVariableCheck* new_check = new(check->block()->graph()->zone())
2170 InductionVariableCheck(check, checks_, upper_limit);
2171 checks_ = new_check;
2175 if (upper_limit > current_upper_limit()) {
2176 current_upper_limit_ = upper_limit;
2179 if (decomposition.and_mask != 0 &&
2180 current_or_mask_in_block() == 0) {
2181 if (current_and_mask_in_block() == 0 ||
2182 decomposition.and_mask > current_and_mask_in_block()) {
2183 UseNewIndexInCurrentBlock(Token::BIT_AND,
2184 decomposition.and_mask,
2186 decomposition.context);
2187 current_and_mask_in_block_ = decomposition.and_mask;
2189 check->set_skip_check();
2191 if (current_and_mask_in_block() == 0) {
2192 if (decomposition.or_mask > current_or_mask_in_block()) {
2193 UseNewIndexInCurrentBlock(Token::BIT_OR,
2194 decomposition.or_mask,
2196 decomposition.context);
2197 current_or_mask_in_block_ = decomposition.or_mask;
2199 check->set_skip_check();
2202 if (!check->skip_check()) {
2203 InductionVariableCheck* new_check = new(check->block()->graph()->zone())
2204 InductionVariableCheck(check, checks_, upper_limit);
2205 checks_ = new_check;
2211 * This method detects if phi is an induction variable, with phi_operand as
2212 * its "incremented" value (the other operand would be the "base" value).
2214 * It cheks is phi_operand has the form "phi + constant".
2215 * If yes, the constant is the increment that the induction variable gets at
2216 * every loop iteration.
2217 * Otherwise it returns 0.
2219 int32_t InductionVariableData::ComputeIncrement(HPhi* phi,
2220 HValue* phi_operand) {
2221 if (!phi_operand->representation().IsInteger32()) return 0;
2223 if (phi_operand->IsAdd()) {
2224 HAdd* operation = HAdd::cast(phi_operand);
2225 if (operation->left() == phi &&
2226 operation->right()->IsInteger32Constant()) {
2227 return operation->right()->GetInteger32Constant();
2228 } else if (operation->right() == phi &&
2229 operation->left()->IsInteger32Constant()) {
2230 return operation->left()->GetInteger32Constant();
2232 } else if (phi_operand->IsSub()) {
2233 HSub* operation = HSub::cast(phi_operand);
2234 if (operation->left() == phi &&
2235 operation->right()->IsInteger32Constant()) {
2236 return -operation->right()->GetInteger32Constant();
2245 * Swaps the information in "update" with the one contained in "this".
2246 * The swapping is important because this method is used while doing a
2247 * dominator tree traversal, and "update" will retain the old data that
2248 * will be restored while backtracking.
2250 void InductionVariableData::UpdateAdditionalLimit(
2251 InductionVariableLimitUpdate* update) {
2252 DCHECK(update->updated_variable == this);
2253 if (update->limit_is_upper) {
2254 swap(&additional_upper_limit_, &update->limit);
2255 swap(&additional_upper_limit_is_included_, &update->limit_is_included);
2257 swap(&additional_lower_limit_, &update->limit);
2258 swap(&additional_lower_limit_is_included_, &update->limit_is_included);
2263 int32_t InductionVariableData::ComputeUpperLimit(int32_t and_mask,
2265 // Should be Smi::kMaxValue but it must fit 32 bits; lower is safe anyway.
2266 const int32_t MAX_LIMIT = 1 << 30;
2268 int32_t result = MAX_LIMIT;
2270 if (limit() != NULL &&
2271 limit()->IsInteger32Constant()) {
2272 int32_t limit_value = limit()->GetInteger32Constant();
2273 if (!limit_included()) {
2276 if (limit_value < result) result = limit_value;
2279 if (additional_upper_limit() != NULL &&
2280 additional_upper_limit()->IsInteger32Constant()) {
2281 int32_t limit_value = additional_upper_limit()->GetInteger32Constant();
2282 if (!additional_upper_limit_is_included()) {
2285 if (limit_value < result) result = limit_value;
2288 if (and_mask > 0 && and_mask < MAX_LIMIT) {
2289 if (and_mask < result) result = and_mask;
2293 // Add the effect of the or_mask.
2296 return result >= MAX_LIMIT ? kNoLimit : result;
2300 HValue* InductionVariableData::IgnoreOsrValue(HValue* v) {
2301 if (!v->IsPhi()) return v;
2302 HPhi* phi = HPhi::cast(v);
2303 if (phi->OperandCount() != 2) return v;
2304 if (phi->OperandAt(0)->block()->is_osr_entry()) {
2305 return phi->OperandAt(1);
2306 } else if (phi->OperandAt(1)->block()->is_osr_entry()) {
2307 return phi->OperandAt(0);
2314 InductionVariableData* InductionVariableData::GetInductionVariableData(
2316 v = IgnoreOsrValue(v);
2318 return HPhi::cast(v)->induction_variable_data();
2325 * Check if a conditional branch to "current_branch" with token "token" is
2326 * the branch that keeps the induction loop running (and, conversely, will
2327 * terminate it if the "other_branch" is taken).
2329 * Three conditions must be met:
2330 * - "current_branch" must be in the induction loop.
2331 * - "other_branch" must be out of the induction loop.
2332 * - "token" and the induction increment must be "compatible": the token should
2333 * be a condition that keeps the execution inside the loop until the limit is
2336 bool InductionVariableData::CheckIfBranchIsLoopGuard(
2338 HBasicBlock* current_branch,
2339 HBasicBlock* other_branch) {
2340 if (!phi()->block()->current_loop()->IsNestedInThisLoop(
2341 current_branch->current_loop())) {
2345 if (phi()->block()->current_loop()->IsNestedInThisLoop(
2346 other_branch->current_loop())) {
2350 if (increment() > 0 && (token == Token::LT || token == Token::LTE)) {
2353 if (increment() < 0 && (token == Token::GT || token == Token::GTE)) {
2356 if (Token::IsInequalityOp(token) && (increment() == 1 || increment() == -1)) {
2364 void InductionVariableData::ComputeLimitFromPredecessorBlock(
2366 LimitFromPredecessorBlock* result) {
2367 if (block->predecessors()->length() != 1) return;
2368 HBasicBlock* predecessor = block->predecessors()->at(0);
2369 HInstruction* end = predecessor->last();
2371 if (!end->IsCompareNumericAndBranch()) return;
2372 HCompareNumericAndBranch* branch = HCompareNumericAndBranch::cast(end);
2374 Token::Value token = branch->token();
2375 if (!Token::IsArithmeticCompareOp(token)) return;
2377 HBasicBlock* other_target;
2378 if (block == branch->SuccessorAt(0)) {
2379 other_target = branch->SuccessorAt(1);
2381 other_target = branch->SuccessorAt(0);
2382 token = Token::NegateCompareOp(token);
2383 DCHECK(block == branch->SuccessorAt(1));
2386 InductionVariableData* data;
2388 data = GetInductionVariableData(branch->left());
2389 HValue* limit = branch->right();
2391 data = GetInductionVariableData(branch->right());
2392 token = Token::ReverseCompareOp(token);
2393 limit = branch->left();
2397 result->variable = data;
2398 result->token = token;
2399 result->limit = limit;
2400 result->other_target = other_target;
2406 * Compute the limit that is imposed on an induction variable when entering
2408 * If the limit is the "proper" induction limit (the one that makes the loop
2409 * terminate when the induction variable reaches it) it is stored directly in
2410 * the induction variable data.
2411 * Otherwise the limit is written in "additional_limit" and the method
2414 bool InductionVariableData::ComputeInductionVariableLimit(
2416 InductionVariableLimitUpdate* additional_limit) {
2417 LimitFromPredecessorBlock limit;
2418 ComputeLimitFromPredecessorBlock(block, &limit);
2419 if (!limit.LimitIsValid()) return false;
2421 if (limit.variable->CheckIfBranchIsLoopGuard(limit.token,
2423 limit.other_target)) {
2424 limit.variable->limit_ = limit.limit;
2425 limit.variable->limit_included_ = limit.LimitIsIncluded();
2426 limit.variable->limit_validity_ = block;
2427 limit.variable->induction_exit_block_ = block->predecessors()->at(0);
2428 limit.variable->induction_exit_target_ = limit.other_target;
2431 additional_limit->updated_variable = limit.variable;
2432 additional_limit->limit = limit.limit;
2433 additional_limit->limit_is_upper = limit.LimitIsUpper();
2434 additional_limit->limit_is_included = limit.LimitIsIncluded();
2440 Range* HMathMinMax::InferRange(Zone* zone) {
2441 if (representation().IsSmiOrInteger32()) {
2442 Range* a = left()->range();
2443 Range* b = right()->range();
2444 Range* res = a->Copy(zone);
2445 if (operation_ == kMathMax) {
2446 res->CombinedMax(b);
2448 DCHECK(operation_ == kMathMin);
2449 res->CombinedMin(b);
2453 return HValue::InferRange(zone);
2458 void HPushArguments::AddInput(HValue* value) {
2459 inputs_.Add(NULL, value->block()->zone());
2460 SetOperandAt(OperandCount() - 1, value);
2464 OStream& HPhi::PrintTo(OStream& os) const { // NOLINT
2466 for (int i = 0; i < OperandCount(); ++i) {
2467 os << " " << NameOf(OperandAt(i)) << " ";
2469 return os << " uses:" << UseCount() << "_"
2470 << smi_non_phi_uses() + smi_indirect_uses() << "s_"
2471 << int32_non_phi_uses() + int32_indirect_uses() << "i_"
2472 << double_non_phi_uses() + double_indirect_uses() << "d_"
2473 << tagged_non_phi_uses() + tagged_indirect_uses() << "t"
2474 << TypeOf(this) << "]";
2478 void HPhi::AddInput(HValue* value) {
2479 inputs_.Add(NULL, value->block()->zone());
2480 SetOperandAt(OperandCount() - 1, value);
2481 // Mark phis that may have 'arguments' directly or indirectly as an operand.
2482 if (!CheckFlag(kIsArguments) && value->CheckFlag(kIsArguments)) {
2483 SetFlag(kIsArguments);
2488 bool HPhi::HasRealUses() {
2489 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
2490 if (!it.value()->IsPhi()) return true;
2496 HValue* HPhi::GetRedundantReplacement() {
2497 HValue* candidate = NULL;
2498 int count = OperandCount();
2500 while (position < count && candidate == NULL) {
2501 HValue* current = OperandAt(position++);
2502 if (current != this) candidate = current;
2504 while (position < count) {
2505 HValue* current = OperandAt(position++);
2506 if (current != this && current != candidate) return NULL;
2508 DCHECK(candidate != this);
2513 void HPhi::DeleteFromGraph() {
2514 DCHECK(block() != NULL);
2515 block()->RemovePhi(this);
2516 DCHECK(block() == NULL);
2520 void HPhi::InitRealUses(int phi_id) {
2521 // Initialize real uses.
2523 // Compute a conservative approximation of truncating uses before inferring
2524 // representations. The proper, exact computation will be done later, when
2525 // inserting representation changes.
2526 SetFlag(kTruncatingToSmi);
2527 SetFlag(kTruncatingToInt32);
2528 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
2529 HValue* value = it.value();
2530 if (!value->IsPhi()) {
2531 Representation rep = value->observed_input_representation(it.index());
2532 non_phi_uses_[rep.kind()] += 1;
2533 if (FLAG_trace_representation) {
2534 PrintF("#%d Phi is used by real #%d %s as %s\n",
2535 id(), value->id(), value->Mnemonic(), rep.Mnemonic());
2537 if (!value->IsSimulate()) {
2538 if (!value->CheckFlag(kTruncatingToSmi)) {
2539 ClearFlag(kTruncatingToSmi);
2541 if (!value->CheckFlag(kTruncatingToInt32)) {
2542 ClearFlag(kTruncatingToInt32);
2550 void HPhi::AddNonPhiUsesFrom(HPhi* other) {
2551 if (FLAG_trace_representation) {
2552 PrintF("adding to #%d Phi uses of #%d Phi: s%d i%d d%d t%d\n",
2554 other->non_phi_uses_[Representation::kSmi],
2555 other->non_phi_uses_[Representation::kInteger32],
2556 other->non_phi_uses_[Representation::kDouble],
2557 other->non_phi_uses_[Representation::kTagged]);
2560 for (int i = 0; i < Representation::kNumRepresentations; i++) {
2561 indirect_uses_[i] += other->non_phi_uses_[i];
2566 void HPhi::AddIndirectUsesTo(int* dest) {
2567 for (int i = 0; i < Representation::kNumRepresentations; i++) {
2568 dest[i] += indirect_uses_[i];
2573 void HSimulate::MergeWith(ZoneList<HSimulate*>* list) {
2574 while (!list->is_empty()) {
2575 HSimulate* from = list->RemoveLast();
2576 ZoneList<HValue*>* from_values = &from->values_;
2577 for (int i = 0; i < from_values->length(); ++i) {
2578 if (from->HasAssignedIndexAt(i)) {
2579 int index = from->GetAssignedIndexAt(i);
2580 if (HasValueForIndex(index)) continue;
2581 AddAssignedValue(index, from_values->at(i));
2583 if (pop_count_ > 0) {
2586 AddPushedValue(from_values->at(i));
2590 pop_count_ += from->pop_count_;
2591 from->DeleteAndReplaceWith(NULL);
2596 OStream& HSimulate::PrintDataTo(OStream& os) const { // NOLINT
2597 os << "id=" << ast_id().ToInt();
2598 if (pop_count_ > 0) os << " pop " << pop_count_;
2599 if (values_.length() > 0) {
2600 if (pop_count_ > 0) os << " /";
2601 for (int i = values_.length() - 1; i >= 0; --i) {
2602 if (HasAssignedIndexAt(i)) {
2603 os << " var[" << GetAssignedIndexAt(i) << "] = ";
2607 os << NameOf(values_[i]);
2608 if (i > 0) os << ",";
2615 void HSimulate::ReplayEnvironment(HEnvironment* env) {
2616 if (done_with_replay_) return;
2617 DCHECK(env != NULL);
2618 env->set_ast_id(ast_id());
2619 env->Drop(pop_count());
2620 for (int i = values()->length() - 1; i >= 0; --i) {
2621 HValue* value = values()->at(i);
2622 if (HasAssignedIndexAt(i)) {
2623 env->Bind(GetAssignedIndexAt(i), value);
2628 done_with_replay_ = true;
2632 static void ReplayEnvironmentNested(const ZoneList<HValue*>* values,
2633 HCapturedObject* other) {
2634 for (int i = 0; i < values->length(); ++i) {
2635 HValue* value = values->at(i);
2636 if (value->IsCapturedObject()) {
2637 if (HCapturedObject::cast(value)->capture_id() == other->capture_id()) {
2638 values->at(i) = other;
2640 ReplayEnvironmentNested(HCapturedObject::cast(value)->values(), other);
2647 // Replay captured objects by replacing all captured objects with the
2648 // same capture id in the current and all outer environments.
2649 void HCapturedObject::ReplayEnvironment(HEnvironment* env) {
2650 DCHECK(env != NULL);
2651 while (env != NULL) {
2652 ReplayEnvironmentNested(env->values(), this);
2658 OStream& HCapturedObject::PrintDataTo(OStream& os) const { // NOLINT
2659 os << "#" << capture_id() << " ";
2660 return HDematerializedObject::PrintDataTo(os);
2664 void HEnterInlined::RegisterReturnTarget(HBasicBlock* return_target,
2666 DCHECK(return_target->IsInlineReturnTarget());
2667 return_targets_.Add(return_target, zone);
2671 OStream& HEnterInlined::PrintDataTo(OStream& os) const { // NOLINT
2672 return os << function()->debug_name()->ToCString().get()
2673 << ", id=" << function()->id().ToInt();
2677 static bool IsInteger32(double value) {
2678 double roundtrip_value = static_cast<double>(static_cast<int32_t>(value));
2679 return BitCast<int64_t>(roundtrip_value) == BitCast<int64_t>(value);
2683 HConstant::HConstant(Handle<Object> object, Representation r)
2684 : HTemplateInstruction<0>(HType::FromValue(object)),
2685 object_(Unique<Object>::CreateUninitialized(object)),
2686 object_map_(Handle<Map>::null()),
2687 has_stable_map_value_(false),
2688 has_smi_value_(false),
2689 has_int32_value_(false),
2690 has_double_value_(false),
2691 has_external_reference_value_(false),
2692 is_not_in_new_space_(true),
2693 boolean_value_(object->BooleanValue()),
2694 is_undetectable_(false),
2695 instance_type_(kUnknownInstanceType) {
2696 if (object->IsHeapObject()) {
2697 Handle<HeapObject> heap_object = Handle<HeapObject>::cast(object);
2698 Isolate* isolate = heap_object->GetIsolate();
2699 Handle<Map> map(heap_object->map(), isolate);
2700 is_not_in_new_space_ = !isolate->heap()->InNewSpace(*object);
2701 instance_type_ = map->instance_type();
2702 is_undetectable_ = map->is_undetectable();
2703 if (map->is_stable()) object_map_ = Unique<Map>::CreateImmovable(map);
2704 has_stable_map_value_ = (instance_type_ == MAP_TYPE &&
2705 Handle<Map>::cast(heap_object)->is_stable());
2707 if (object->IsNumber()) {
2708 double n = object->Number();
2709 has_int32_value_ = IsInteger32(n);
2710 int32_value_ = DoubleToInt32(n);
2711 has_smi_value_ = has_int32_value_ && Smi::IsValid(int32_value_);
2713 has_double_value_ = true;
2714 // TODO(titzer): if this heap number is new space, tenure a new one.
2721 HConstant::HConstant(Unique<Object> object,
2722 Unique<Map> object_map,
2723 bool has_stable_map_value,
2726 bool is_not_in_new_space,
2728 bool is_undetectable,
2729 InstanceType instance_type)
2730 : HTemplateInstruction<0>(type),
2732 object_map_(object_map),
2733 has_stable_map_value_(has_stable_map_value),
2734 has_smi_value_(false),
2735 has_int32_value_(false),
2736 has_double_value_(false),
2737 has_external_reference_value_(false),
2738 is_not_in_new_space_(is_not_in_new_space),
2739 boolean_value_(boolean_value),
2740 is_undetectable_(is_undetectable),
2741 instance_type_(instance_type) {
2742 DCHECK(!object.handle().is_null());
2743 DCHECK(!type.IsTaggedNumber() || type.IsNone());
2748 HConstant::HConstant(int32_t integer_value,
2750 bool is_not_in_new_space,
2751 Unique<Object> object)
2753 object_map_(Handle<Map>::null()),
2754 has_stable_map_value_(false),
2755 has_smi_value_(Smi::IsValid(integer_value)),
2756 has_int32_value_(true),
2757 has_double_value_(true),
2758 has_external_reference_value_(false),
2759 is_not_in_new_space_(is_not_in_new_space),
2760 boolean_value_(integer_value != 0),
2761 is_undetectable_(false),
2762 int32_value_(integer_value),
2763 double_value_(FastI2D(integer_value)),
2764 instance_type_(kUnknownInstanceType) {
2765 // It's possible to create a constant with a value in Smi-range but stored
2766 // in a (pre-existing) HeapNumber. See crbug.com/349878.
2767 bool could_be_heapobject = r.IsTagged() && !object.handle().is_null();
2768 bool is_smi = has_smi_value_ && !could_be_heapobject;
2769 set_type(is_smi ? HType::Smi() : HType::TaggedNumber());
2774 HConstant::HConstant(double double_value,
2776 bool is_not_in_new_space,
2777 Unique<Object> object)
2779 object_map_(Handle<Map>::null()),
2780 has_stable_map_value_(false),
2781 has_int32_value_(IsInteger32(double_value)),
2782 has_double_value_(true),
2783 has_external_reference_value_(false),
2784 is_not_in_new_space_(is_not_in_new_space),
2785 boolean_value_(double_value != 0 && !std::isnan(double_value)),
2786 is_undetectable_(false),
2787 int32_value_(DoubleToInt32(double_value)),
2788 double_value_(double_value),
2789 instance_type_(kUnknownInstanceType) {
2790 has_smi_value_ = has_int32_value_ && Smi::IsValid(int32_value_);
2791 // It's possible to create a constant with a value in Smi-range but stored
2792 // in a (pre-existing) HeapNumber. See crbug.com/349878.
2793 bool could_be_heapobject = r.IsTagged() && !object.handle().is_null();
2794 bool is_smi = has_smi_value_ && !could_be_heapobject;
2795 set_type(is_smi ? HType::Smi() : HType::TaggedNumber());
2800 HConstant::HConstant(ExternalReference reference)
2801 : HTemplateInstruction<0>(HType::Any()),
2802 object_(Unique<Object>(Handle<Object>::null())),
2803 object_map_(Handle<Map>::null()),
2804 has_stable_map_value_(false),
2805 has_smi_value_(false),
2806 has_int32_value_(false),
2807 has_double_value_(false),
2808 has_external_reference_value_(true),
2809 is_not_in_new_space_(true),
2810 boolean_value_(true),
2811 is_undetectable_(false),
2812 external_reference_value_(reference),
2813 instance_type_(kUnknownInstanceType) {
2814 Initialize(Representation::External());
2818 void HConstant::Initialize(Representation r) {
2820 if (has_smi_value_ && SmiValuesAre31Bits()) {
2821 r = Representation::Smi();
2822 } else if (has_int32_value_) {
2823 r = Representation::Integer32();
2824 } else if (has_double_value_) {
2825 r = Representation::Double();
2826 } else if (has_external_reference_value_) {
2827 r = Representation::External();
2829 Handle<Object> object = object_.handle();
2830 if (object->IsJSObject()) {
2831 // Try to eagerly migrate JSObjects that have deprecated maps.
2832 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
2833 if (js_object->map()->is_deprecated()) {
2834 JSObject::TryMigrateInstance(js_object);
2837 r = Representation::Tagged();
2840 set_representation(r);
2845 bool HConstant::ImmortalImmovable() const {
2846 if (has_int32_value_) {
2849 if (has_double_value_) {
2850 if (IsSpecialDouble()) {
2855 if (has_external_reference_value_) {
2859 DCHECK(!object_.handle().is_null());
2860 Heap* heap = isolate()->heap();
2861 DCHECK(!object_.IsKnownGlobal(heap->minus_zero_value()));
2862 DCHECK(!object_.IsKnownGlobal(heap->nan_value()));
2864 #define IMMORTAL_IMMOVABLE_ROOT(name) \
2865 object_.IsKnownGlobal(heap->name()) ||
2866 IMMORTAL_IMMOVABLE_ROOT_LIST(IMMORTAL_IMMOVABLE_ROOT)
2867 #undef IMMORTAL_IMMOVABLE_ROOT
2868 #define INTERNALIZED_STRING(name, value) \
2869 object_.IsKnownGlobal(heap->name()) ||
2870 INTERNALIZED_STRING_LIST(INTERNALIZED_STRING)
2871 #undef INTERNALIZED_STRING
2872 #define STRING_TYPE(NAME, size, name, Name) \
2873 object_.IsKnownGlobal(heap->name##_map()) ||
2874 STRING_TYPE_LIST(STRING_TYPE)
2880 bool HConstant::EmitAtUses() {
2882 if (block()->graph()->has_osr() &&
2883 block()->graph()->IsStandardConstant(this)) {
2884 // TODO(titzer): this seems like a hack that should be fixed by custom OSR.
2887 if (HasNoUses()) return true;
2888 if (IsCell()) return false;
2889 if (representation().IsDouble()) return false;
2890 if (representation().IsExternal()) return false;
2895 HConstant* HConstant::CopyToRepresentation(Representation r, Zone* zone) const {
2896 if (r.IsSmi() && !has_smi_value_) return NULL;
2897 if (r.IsInteger32() && !has_int32_value_) return NULL;
2898 if (r.IsDouble() && !has_double_value_) return NULL;
2899 if (r.IsExternal() && !has_external_reference_value_) return NULL;
2900 if (has_int32_value_) {
2901 return new(zone) HConstant(int32_value_, r, is_not_in_new_space_, object_);
2903 if (has_double_value_) {
2904 return new(zone) HConstant(double_value_, r, is_not_in_new_space_, object_);
2906 if (has_external_reference_value_) {
2907 return new(zone) HConstant(external_reference_value_);
2909 DCHECK(!object_.handle().is_null());
2910 return new(zone) HConstant(object_,
2912 has_stable_map_value_,
2915 is_not_in_new_space_,
2922 Maybe<HConstant*> HConstant::CopyToTruncatedInt32(Zone* zone) {
2923 HConstant* res = NULL;
2924 if (has_int32_value_) {
2925 res = new(zone) HConstant(int32_value_,
2926 Representation::Integer32(),
2927 is_not_in_new_space_,
2929 } else if (has_double_value_) {
2930 res = new(zone) HConstant(DoubleToInt32(double_value_),
2931 Representation::Integer32(),
2932 is_not_in_new_space_,
2935 return Maybe<HConstant*>(res != NULL, res);
2939 Maybe<HConstant*> HConstant::CopyToTruncatedNumber(Zone* zone) {
2940 HConstant* res = NULL;
2941 Handle<Object> handle = this->handle(zone->isolate());
2942 if (handle->IsBoolean()) {
2943 res = handle->BooleanValue() ?
2944 new(zone) HConstant(1) : new(zone) HConstant(0);
2945 } else if (handle->IsUndefined()) {
2946 res = new(zone) HConstant(base::OS::nan_value());
2947 } else if (handle->IsNull()) {
2948 res = new(zone) HConstant(0);
2950 return Maybe<HConstant*>(res != NULL, res);
2954 OStream& HConstant::PrintDataTo(OStream& os) const { // NOLINT
2955 if (has_int32_value_) {
2956 os << int32_value_ << " ";
2957 } else if (has_double_value_) {
2958 os << double_value_ << " ";
2959 } else if (has_external_reference_value_) {
2960 os << reinterpret_cast<void*>(external_reference_value_.address()) << " ";
2962 // The handle() method is silently and lazily mutating the object.
2963 Handle<Object> h = const_cast<HConstant*>(this)->handle(Isolate::Current());
2964 os << Brief(*h) << " ";
2965 if (HasStableMapValue()) os << "[stable-map] ";
2966 if (HasObjectMap()) os << "[map " << *ObjectMap().handle() << "] ";
2968 if (!is_not_in_new_space_) os << "[new space] ";
2973 OStream& HBinaryOperation::PrintDataTo(OStream& os) const { // NOLINT
2974 os << NameOf(left()) << " " << NameOf(right());
2975 if (CheckFlag(kCanOverflow)) os << " !";
2976 if (CheckFlag(kBailoutOnMinusZero)) os << " -0?";
2981 void HBinaryOperation::InferRepresentation(HInferRepresentationPhase* h_infer) {
2982 DCHECK(CheckFlag(kFlexibleRepresentation));
2983 Representation new_rep = RepresentationFromInputs();
2984 UpdateRepresentation(new_rep, h_infer, "inputs");
2986 if (representation().IsSmi() && HasNonSmiUse()) {
2987 UpdateRepresentation(
2988 Representation::Integer32(), h_infer, "use requirements");
2991 if (observed_output_representation_.IsNone()) {
2992 new_rep = RepresentationFromUses();
2993 UpdateRepresentation(new_rep, h_infer, "uses");
2995 new_rep = RepresentationFromOutput();
2996 UpdateRepresentation(new_rep, h_infer, "output");
3001 Representation HBinaryOperation::RepresentationFromInputs() {
3002 // Determine the worst case of observed input representations and
3003 // the currently assumed output representation.
3004 Representation rep = representation();
3005 for (int i = 1; i <= 2; ++i) {
3006 rep = rep.generalize(observed_input_representation(i));
3008 // If any of the actual input representation is more general than what we
3009 // have so far but not Tagged, use that representation instead.
3010 Representation left_rep = left()->representation();
3011 Representation right_rep = right()->representation();
3012 if (!left_rep.IsTagged()) rep = rep.generalize(left_rep);
3013 if (!right_rep.IsTagged()) rep = rep.generalize(right_rep);
3019 bool HBinaryOperation::IgnoreObservedOutputRepresentation(
3020 Representation current_rep) {
3021 return ((current_rep.IsInteger32() && CheckUsesForFlag(kTruncatingToInt32)) ||
3022 (current_rep.IsSmi() && CheckUsesForFlag(kTruncatingToSmi))) &&
3023 // Mul in Integer32 mode would be too precise.
3024 (!this->IsMul() || HMul::cast(this)->MulMinusOne());
3028 Representation HBinaryOperation::RepresentationFromOutput() {
3029 Representation rep = representation();
3030 // Consider observed output representation, but ignore it if it's Double,
3031 // this instruction is not a division, and all its uses are truncating
3033 if (observed_output_representation_.is_more_general_than(rep) &&
3034 !IgnoreObservedOutputRepresentation(rep)) {
3035 return observed_output_representation_;
3037 return Representation::None();
3041 void HBinaryOperation::AssumeRepresentation(Representation r) {
3042 set_observed_input_representation(1, r);
3043 set_observed_input_representation(2, r);
3044 HValue::AssumeRepresentation(r);
3048 void HMathMinMax::InferRepresentation(HInferRepresentationPhase* h_infer) {
3049 DCHECK(CheckFlag(kFlexibleRepresentation));
3050 Representation new_rep = RepresentationFromInputs();
3051 UpdateRepresentation(new_rep, h_infer, "inputs");
3052 // Do not care about uses.
3056 Range* HBitwise::InferRange(Zone* zone) {
3057 if (op() == Token::BIT_XOR) {
3058 if (left()->HasRange() && right()->HasRange()) {
3059 // The maximum value has the high bit, and all bits below, set:
3061 // If the range can be negative, the minimum int is a negative number with
3062 // the high bit, and all bits below, unset:
3064 // If it cannot be negative, conservatively choose 0 as minimum int.
3065 int64_t left_upper = left()->range()->upper();
3066 int64_t left_lower = left()->range()->lower();
3067 int64_t right_upper = right()->range()->upper();
3068 int64_t right_lower = right()->range()->lower();
3070 if (left_upper < 0) left_upper = ~left_upper;
3071 if (left_lower < 0) left_lower = ~left_lower;
3072 if (right_upper < 0) right_upper = ~right_upper;
3073 if (right_lower < 0) right_lower = ~right_lower;
3075 int high = MostSignificantBit(
3076 static_cast<uint32_t>(
3077 left_upper | left_lower | right_upper | right_lower));
3081 int32_t min = (left()->range()->CanBeNegative() ||
3082 right()->range()->CanBeNegative())
3083 ? static_cast<int32_t>(-limit) : 0;
3084 return new(zone) Range(min, static_cast<int32_t>(limit - 1));
3086 Range* result = HValue::InferRange(zone);
3087 result->set_can_be_minus_zero(false);
3090 const int32_t kDefaultMask = static_cast<int32_t>(0xffffffff);
3091 int32_t left_mask = (left()->range() != NULL)
3092 ? left()->range()->Mask()
3094 int32_t right_mask = (right()->range() != NULL)
3095 ? right()->range()->Mask()
3097 int32_t result_mask = (op() == Token::BIT_AND)
3098 ? left_mask & right_mask
3099 : left_mask | right_mask;
3100 if (result_mask >= 0) return new(zone) Range(0, result_mask);
3102 Range* result = HValue::InferRange(zone);
3103 result->set_can_be_minus_zero(false);
3108 Range* HSar::InferRange(Zone* zone) {
3109 if (right()->IsConstant()) {
3110 HConstant* c = HConstant::cast(right());
3111 if (c->HasInteger32Value()) {
3112 Range* result = (left()->range() != NULL)
3113 ? left()->range()->Copy(zone)
3114 : new(zone) Range();
3115 result->Sar(c->Integer32Value());
3119 return HValue::InferRange(zone);
3123 Range* HShr::InferRange(Zone* zone) {
3124 if (right()->IsConstant()) {
3125 HConstant* c = HConstant::cast(right());
3126 if (c->HasInteger32Value()) {
3127 int shift_count = c->Integer32Value() & 0x1f;
3128 if (left()->range()->CanBeNegative()) {
3129 // Only compute bounds if the result always fits into an int32.
3130 return (shift_count >= 1)
3131 ? new(zone) Range(0,
3132 static_cast<uint32_t>(0xffffffff) >> shift_count)
3133 : new(zone) Range();
3135 // For positive inputs we can use the >> operator.
3136 Range* result = (left()->range() != NULL)
3137 ? left()->range()->Copy(zone)
3138 : new(zone) Range();
3139 result->Sar(c->Integer32Value());
3144 return HValue::InferRange(zone);
3148 Range* HShl::InferRange(Zone* zone) {
3149 if (right()->IsConstant()) {
3150 HConstant* c = HConstant::cast(right());
3151 if (c->HasInteger32Value()) {
3152 Range* result = (left()->range() != NULL)
3153 ? left()->range()->Copy(zone)
3154 : new(zone) Range();
3155 result->Shl(c->Integer32Value());
3159 return HValue::InferRange(zone);
3163 Range* HLoadNamedField::InferRange(Zone* zone) {
3164 if (access().representation().IsInteger8()) {
3165 return new(zone) Range(kMinInt8, kMaxInt8);
3167 if (access().representation().IsUInteger8()) {
3168 return new(zone) Range(kMinUInt8, kMaxUInt8);
3170 if (access().representation().IsInteger16()) {
3171 return new(zone) Range(kMinInt16, kMaxInt16);
3173 if (access().representation().IsUInteger16()) {
3174 return new(zone) Range(kMinUInt16, kMaxUInt16);
3176 if (access().IsStringLength()) {
3177 return new(zone) Range(0, String::kMaxLength);
3179 return HValue::InferRange(zone);
3183 Range* HLoadKeyed::InferRange(Zone* zone) {
3184 switch (elements_kind()) {
3185 case EXTERNAL_INT8_ELEMENTS:
3186 return new(zone) Range(kMinInt8, kMaxInt8);
3187 case EXTERNAL_UINT8_ELEMENTS:
3188 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3189 return new(zone) Range(kMinUInt8, kMaxUInt8);
3190 case EXTERNAL_INT16_ELEMENTS:
3191 return new(zone) Range(kMinInt16, kMaxInt16);
3192 case EXTERNAL_UINT16_ELEMENTS:
3193 return new(zone) Range(kMinUInt16, kMaxUInt16);
3195 return HValue::InferRange(zone);
3200 OStream& HCompareGeneric::PrintDataTo(OStream& os) const { // NOLINT
3201 os << Token::Name(token()) << " ";
3202 return HBinaryOperation::PrintDataTo(os);
3206 OStream& HStringCompareAndBranch::PrintDataTo(OStream& os) const { // NOLINT
3207 os << Token::Name(token()) << " ";
3208 return HControlInstruction::PrintDataTo(os);
3212 OStream& HCompareNumericAndBranch::PrintDataTo(OStream& os) const { // NOLINT
3213 os << Token::Name(token()) << " " << NameOf(left()) << " " << NameOf(right());
3214 return HControlInstruction::PrintDataTo(os);
3218 OStream& HCompareObjectEqAndBranch::PrintDataTo(OStream& os) const { // NOLINT
3219 os << NameOf(left()) << " " << NameOf(right());
3220 return HControlInstruction::PrintDataTo(os);
3224 bool HCompareObjectEqAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3225 if (known_successor_index() != kNoKnownSuccessorIndex) {
3226 *block = SuccessorAt(known_successor_index());
3229 if (FLAG_fold_constants && left()->IsConstant() && right()->IsConstant()) {
3230 *block = HConstant::cast(left())->DataEquals(HConstant::cast(right()))
3231 ? FirstSuccessor() : SecondSuccessor();
3239 bool ConstantIsObject(HConstant* constant, Isolate* isolate) {
3240 if (constant->HasNumberValue()) return false;
3241 if (constant->GetUnique().IsKnownGlobal(isolate->heap()->null_value())) {
3244 if (constant->IsUndetectable()) return false;
3245 InstanceType type = constant->GetInstanceType();
3246 return (FIRST_NONCALLABLE_SPEC_OBJECT_TYPE <= type) &&
3247 (type <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
3251 bool HIsObjectAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3252 if (FLAG_fold_constants && value()->IsConstant()) {
3253 *block = ConstantIsObject(HConstant::cast(value()), isolate())
3254 ? FirstSuccessor() : SecondSuccessor();
3262 bool HIsStringAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3263 if (known_successor_index() != kNoKnownSuccessorIndex) {
3264 *block = SuccessorAt(known_successor_index());
3267 if (FLAG_fold_constants && value()->IsConstant()) {
3268 *block = HConstant::cast(value())->HasStringValue()
3269 ? FirstSuccessor() : SecondSuccessor();
3272 if (value()->type().IsString()) {
3273 *block = FirstSuccessor();
3276 if (value()->type().IsSmi() ||
3277 value()->type().IsNull() ||
3278 value()->type().IsBoolean() ||
3279 value()->type().IsUndefined() ||
3280 value()->type().IsJSObject()) {
3281 *block = SecondSuccessor();
3289 bool HIsUndetectableAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3290 if (FLAG_fold_constants && value()->IsConstant()) {
3291 *block = HConstant::cast(value())->IsUndetectable()
3292 ? FirstSuccessor() : SecondSuccessor();
3300 bool HHasInstanceTypeAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3301 if (FLAG_fold_constants && value()->IsConstant()) {
3302 InstanceType type = HConstant::cast(value())->GetInstanceType();
3303 *block = (from_ <= type) && (type <= to_)
3304 ? FirstSuccessor() : SecondSuccessor();
3312 void HCompareHoleAndBranch::InferRepresentation(
3313 HInferRepresentationPhase* h_infer) {
3314 ChangeRepresentation(value()->representation());
3318 bool HCompareNumericAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3319 if (left() == right() &&
3320 left()->representation().IsSmiOrInteger32()) {
3321 *block = (token() == Token::EQ ||
3322 token() == Token::EQ_STRICT ||
3323 token() == Token::LTE ||
3324 token() == Token::GTE)
3325 ? FirstSuccessor() : SecondSuccessor();
3333 bool HCompareMinusZeroAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3334 if (FLAG_fold_constants && value()->IsConstant()) {
3335 HConstant* constant = HConstant::cast(value());
3336 if (constant->HasDoubleValue()) {
3337 *block = IsMinusZero(constant->DoubleValue())
3338 ? FirstSuccessor() : SecondSuccessor();
3342 if (value()->representation().IsSmiOrInteger32()) {
3343 // A Smi or Integer32 cannot contain minus zero.
3344 *block = SecondSuccessor();
3352 void HCompareMinusZeroAndBranch::InferRepresentation(
3353 HInferRepresentationPhase* h_infer) {
3354 ChangeRepresentation(value()->representation());
3358 OStream& HGoto::PrintDataTo(OStream& os) const { // NOLINT
3359 return os << *SuccessorAt(0);
3363 void HCompareNumericAndBranch::InferRepresentation(
3364 HInferRepresentationPhase* h_infer) {
3365 Representation left_rep = left()->representation();
3366 Representation right_rep = right()->representation();
3367 Representation observed_left = observed_input_representation(0);
3368 Representation observed_right = observed_input_representation(1);
3370 Representation rep = Representation::None();
3371 rep = rep.generalize(observed_left);
3372 rep = rep.generalize(observed_right);
3373 if (rep.IsNone() || rep.IsSmiOrInteger32()) {
3374 if (!left_rep.IsTagged()) rep = rep.generalize(left_rep);
3375 if (!right_rep.IsTagged()) rep = rep.generalize(right_rep);
3377 rep = Representation::Double();
3380 if (rep.IsDouble()) {
3381 // According to the ES5 spec (11.9.3, 11.8.5), Equality comparisons (==, ===
3382 // and !=) have special handling of undefined, e.g. undefined == undefined
3383 // is 'true'. Relational comparisons have a different semantic, first
3384 // calling ToPrimitive() on their arguments. The standard Crankshaft
3385 // tagged-to-double conversion to ensure the HCompareNumericAndBranch's
3386 // inputs are doubles caused 'undefined' to be converted to NaN. That's
3387 // compatible out-of-the box with ordered relational comparisons (<, >, <=,
3388 // >=). However, for equality comparisons (and for 'in' and 'instanceof'),
3389 // it is not consistent with the spec. For example, it would cause undefined
3390 // == undefined (should be true) to be evaluated as NaN == NaN
3391 // (false). Therefore, any comparisons other than ordered relational
3392 // comparisons must cause a deopt when one of their arguments is undefined.
3394 if (Token::IsOrderedRelationalCompareOp(token_)) {
3395 SetFlag(kAllowUndefinedAsNaN);
3398 ChangeRepresentation(rep);
3402 OStream& HParameter::PrintDataTo(OStream& os) const { // NOLINT
3403 return os << index();
3407 OStream& HLoadNamedField::PrintDataTo(OStream& os) const { // NOLINT
3408 os << NameOf(object()) << access_;
3410 if (maps() != NULL) {
3411 os << " [" << *maps()->at(0).handle();
3412 for (int i = 1; i < maps()->size(); ++i) {
3413 os << "," << *maps()->at(i).handle();
3418 if (HasDependency()) os << " " << NameOf(dependency());
3423 OStream& HLoadNamedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3424 Handle<String> n = Handle<String>::cast(name());
3425 return os << NameOf(object()) << "." << n->ToCString().get();
3429 OStream& HLoadKeyed::PrintDataTo(OStream& os) const { // NOLINT
3430 if (!is_external()) {
3431 os << NameOf(elements());
3433 DCHECK(elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
3434 elements_kind() <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND);
3435 os << NameOf(elements()) << "." << ElementsKindToString(elements_kind());
3438 os << "[" << NameOf(key());
3439 if (IsDehoisted()) os << " + " << base_offset();
3442 if (HasDependency()) os << " " << NameOf(dependency());
3443 if (RequiresHoleCheck()) os << " check_hole";
3448 bool HLoadKeyed::TryIncreaseBaseOffset(uint32_t increase_by_value) {
3449 // The base offset is usually simply the size of the array header, except
3450 // with dehoisting adds an addition offset due to a array index key
3451 // manipulation, in which case it becomes (array header size +
3452 // constant-offset-from-key * kPointerSize)
3453 uint32_t base_offset = BaseOffsetField::decode(bit_field_);
3454 v8::base::internal::CheckedNumeric<uint32_t> addition_result = base_offset;
3455 addition_result += increase_by_value;
3456 if (!addition_result.IsValid()) return false;
3457 base_offset = addition_result.ValueOrDie();
3458 if (!BaseOffsetField::is_valid(base_offset)) return false;
3459 bit_field_ = BaseOffsetField::update(bit_field_, base_offset);
3464 bool HLoadKeyed::UsesMustHandleHole() const {
3465 if (IsFastPackedElementsKind(elements_kind())) {
3469 if (IsExternalArrayElementsKind(elements_kind())) {
3473 if (hole_mode() == ALLOW_RETURN_HOLE) {
3474 if (IsFastDoubleElementsKind(elements_kind())) {
3475 return AllUsesCanTreatHoleAsNaN();
3480 if (IsFastDoubleElementsKind(elements_kind())) {
3484 // Holes are only returned as tagged values.
3485 if (!representation().IsTagged()) {
3489 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
3490 HValue* use = it.value();
3491 if (!use->IsChange()) return false;
3498 bool HLoadKeyed::AllUsesCanTreatHoleAsNaN() const {
3499 return IsFastDoubleElementsKind(elements_kind()) &&
3500 CheckUsesForFlag(HValue::kAllowUndefinedAsNaN);
3504 bool HLoadKeyed::RequiresHoleCheck() const {
3505 if (IsFastPackedElementsKind(elements_kind())) {
3509 if (IsExternalArrayElementsKind(elements_kind())) {
3513 return !UsesMustHandleHole();
3517 OStream& HLoadKeyedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3518 return os << NameOf(object()) << "[" << NameOf(key()) << "]";
3522 HValue* HLoadKeyedGeneric::Canonicalize() {
3523 // Recognize generic keyed loads that use property name generated
3524 // by for-in statement as a key and rewrite them into fast property load
3526 if (key()->IsLoadKeyed()) {
3527 HLoadKeyed* key_load = HLoadKeyed::cast(key());
3528 if (key_load->elements()->IsForInCacheArray()) {
3529 HForInCacheArray* names_cache =
3530 HForInCacheArray::cast(key_load->elements());
3532 if (names_cache->enumerable() == object()) {
3533 HForInCacheArray* index_cache =
3534 names_cache->index_cache();
3535 HCheckMapValue* map_check =
3536 HCheckMapValue::New(block()->graph()->zone(),
3537 block()->graph()->GetInvalidContext(),
3539 names_cache->map());
3540 HInstruction* index = HLoadKeyed::New(
3541 block()->graph()->zone(),
3542 block()->graph()->GetInvalidContext(),
3546 key_load->elements_kind());
3547 map_check->InsertBefore(this);
3548 index->InsertBefore(this);
3549 return Prepend(new(block()->zone()) HLoadFieldByIndex(
3559 OStream& HStoreNamedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3560 Handle<String> n = Handle<String>::cast(name());
3561 return os << NameOf(object()) << "." << n->ToCString().get() << " = "
3566 OStream& HStoreNamedField::PrintDataTo(OStream& os) const { // NOLINT
3567 os << NameOf(object()) << access_ << " = " << NameOf(value());
3568 if (NeedsWriteBarrier()) os << " (write-barrier)";
3569 if (has_transition()) os << " (transition map " << *transition_map() << ")";
3574 OStream& HStoreKeyed::PrintDataTo(OStream& os) const { // NOLINT
3575 if (!is_external()) {
3576 os << NameOf(elements());
3578 DCHECK(elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
3579 elements_kind() <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND);
3580 os << NameOf(elements()) << "." << ElementsKindToString(elements_kind());
3583 os << "[" << NameOf(key());
3584 if (IsDehoisted()) os << " + " << base_offset();
3585 return os << "] = " << NameOf(value());
3589 OStream& HStoreKeyedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3590 return os << NameOf(object()) << "[" << NameOf(key())
3591 << "] = " << NameOf(value());
3595 OStream& HTransitionElementsKind::PrintDataTo(OStream& os) const { // NOLINT
3596 os << NameOf(object());
3597 ElementsKind from_kind = original_map().handle()->elements_kind();
3598 ElementsKind to_kind = transitioned_map().handle()->elements_kind();
3599 os << " " << *original_map().handle() << " ["
3600 << ElementsAccessor::ForKind(from_kind)->name() << "] -> "
3601 << *transitioned_map().handle() << " ["
3602 << ElementsAccessor::ForKind(to_kind)->name() << "]";
3603 if (IsSimpleMapChangeTransition(from_kind, to_kind)) os << " (simple)";
3608 OStream& HLoadGlobalCell::PrintDataTo(OStream& os) const { // NOLINT
3609 os << "[" << *cell().handle() << "]";
3610 if (!details_.IsDontDelete()) os << " (deleteable)";
3611 if (details_.IsReadOnly()) os << " (read-only)";
3616 bool HLoadGlobalCell::RequiresHoleCheck() const {
3617 if (details_.IsDontDelete() && !details_.IsReadOnly()) return false;
3618 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
3619 HValue* use = it.value();
3620 if (!use->IsChange()) return true;
3626 OStream& HLoadGlobalGeneric::PrintDataTo(OStream& os) const { // NOLINT
3627 return os << name()->ToCString().get() << " ";
3631 OStream& HInnerAllocatedObject::PrintDataTo(OStream& os) const { // NOLINT
3632 os << NameOf(base_object()) << " offset ";
3633 return offset()->PrintTo(os);
3637 OStream& HStoreGlobalCell::PrintDataTo(OStream& os) const { // NOLINT
3638 os << "[" << *cell().handle() << "] = " << NameOf(value());
3639 if (!details_.IsDontDelete()) os << " (deleteable)";
3640 if (details_.IsReadOnly()) os << " (read-only)";
3645 OStream& HLoadContextSlot::PrintDataTo(OStream& os) const { // NOLINT
3646 return os << NameOf(value()) << "[" << slot_index() << "]";
3650 OStream& HStoreContextSlot::PrintDataTo(OStream& os) const { // NOLINT
3651 return os << NameOf(context()) << "[" << slot_index()
3652 << "] = " << NameOf(value());
3656 // Implementation of type inference and type conversions. Calculates
3657 // the inferred type of this instruction based on the input operands.
3659 HType HValue::CalculateInferredType() {
3664 HType HPhi::CalculateInferredType() {
3665 if (OperandCount() == 0) return HType::Tagged();
3666 HType result = OperandAt(0)->type();
3667 for (int i = 1; i < OperandCount(); ++i) {
3668 HType current = OperandAt(i)->type();
3669 result = result.Combine(current);
3675 HType HChange::CalculateInferredType() {
3676 if (from().IsDouble() && to().IsTagged()) return HType::HeapNumber();
3681 Representation HUnaryMathOperation::RepresentationFromInputs() {
3682 if (SupportsFlexibleFloorAndRound() &&
3683 (op_ == kMathFloor || op_ == kMathRound)) {
3684 // Floor and Round always take a double input. The integral result can be
3685 // used as an integer or a double. Infer the representation from the uses.
3686 return Representation::None();
3688 Representation rep = representation();
3689 // If any of the actual input representation is more general than what we
3690 // have so far but not Tagged, use that representation instead.
3691 Representation input_rep = value()->representation();
3692 if (!input_rep.IsTagged()) {
3693 rep = rep.generalize(input_rep);
3699 bool HAllocate::HandleSideEffectDominator(GVNFlag side_effect,
3700 HValue* dominator) {
3701 DCHECK(side_effect == kNewSpacePromotion);
3702 Zone* zone = block()->zone();
3703 if (!FLAG_use_allocation_folding) return false;
3705 // Try to fold allocations together with their dominating allocations.
3706 if (!dominator->IsAllocate()) {
3707 if (FLAG_trace_allocation_folding) {
3708 PrintF("#%d (%s) cannot fold into #%d (%s)\n",
3709 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3714 // Check whether we are folding within the same block for local folding.
3715 if (FLAG_use_local_allocation_folding && dominator->block() != block()) {
3716 if (FLAG_trace_allocation_folding) {
3717 PrintF("#%d (%s) cannot fold into #%d (%s), crosses basic blocks\n",
3718 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3723 HAllocate* dominator_allocate = HAllocate::cast(dominator);
3724 HValue* dominator_size = dominator_allocate->size();
3725 HValue* current_size = size();
3727 // TODO(hpayer): Add support for non-constant allocation in dominator.
3728 if (!dominator_size->IsInteger32Constant()) {
3729 if (FLAG_trace_allocation_folding) {
3730 PrintF("#%d (%s) cannot fold into #%d (%s), "
3731 "dynamic allocation size in dominator\n",
3732 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3737 dominator_allocate = GetFoldableDominator(dominator_allocate);
3738 if (dominator_allocate == NULL) {
3742 if (!has_size_upper_bound()) {
3743 if (FLAG_trace_allocation_folding) {
3744 PrintF("#%d (%s) cannot fold into #%d (%s), "
3745 "can't estimate total allocation size\n",
3746 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3751 if (!current_size->IsInteger32Constant()) {
3752 // If it's not constant then it is a size_in_bytes calculation graph
3753 // like this: (const_header_size + const_element_size * size).
3754 DCHECK(current_size->IsInstruction());
3756 HInstruction* current_instr = HInstruction::cast(current_size);
3757 if (!current_instr->Dominates(dominator_allocate)) {
3758 if (FLAG_trace_allocation_folding) {
3759 PrintF("#%d (%s) cannot fold into #%d (%s), dynamic size "
3760 "value does not dominate target allocation\n",
3761 id(), Mnemonic(), dominator_allocate->id(),
3762 dominator_allocate->Mnemonic());
3768 DCHECK((IsNewSpaceAllocation() &&
3769 dominator_allocate->IsNewSpaceAllocation()) ||
3770 (IsOldDataSpaceAllocation() &&
3771 dominator_allocate->IsOldDataSpaceAllocation()) ||
3772 (IsOldPointerSpaceAllocation() &&
3773 dominator_allocate->IsOldPointerSpaceAllocation()));
3775 // First update the size of the dominator allocate instruction.
3776 dominator_size = dominator_allocate->size();
3777 int32_t original_object_size =
3778 HConstant::cast(dominator_size)->GetInteger32Constant();
3779 int32_t dominator_size_constant = original_object_size;
3781 if (MustAllocateDoubleAligned()) {
3782 if ((dominator_size_constant & kDoubleAlignmentMask) != 0) {
3783 dominator_size_constant += kDoubleSize / 2;
3787 int32_t current_size_max_value = size_upper_bound()->GetInteger32Constant();
3788 int32_t new_dominator_size = dominator_size_constant + current_size_max_value;
3790 // Since we clear the first word after folded memory, we cannot use the
3791 // whole Page::kMaxRegularHeapObjectSize memory.
3792 if (new_dominator_size > Page::kMaxRegularHeapObjectSize - kPointerSize) {
3793 if (FLAG_trace_allocation_folding) {
3794 PrintF("#%d (%s) cannot fold into #%d (%s) due to size: %d\n",
3795 id(), Mnemonic(), dominator_allocate->id(),
3796 dominator_allocate->Mnemonic(), new_dominator_size);
3801 HInstruction* new_dominator_size_value;
3803 if (current_size->IsInteger32Constant()) {
3804 new_dominator_size_value =
3805 HConstant::CreateAndInsertBefore(zone,
3808 Representation::None(),
3809 dominator_allocate);
3811 HValue* new_dominator_size_constant =
3812 HConstant::CreateAndInsertBefore(zone,
3814 dominator_size_constant,
3815 Representation::Integer32(),
3816 dominator_allocate);
3818 // Add old and new size together and insert.
3819 current_size->ChangeRepresentation(Representation::Integer32());
3821 new_dominator_size_value = HAdd::New(zone, context(),
3822 new_dominator_size_constant, current_size);
3823 new_dominator_size_value->ClearFlag(HValue::kCanOverflow);
3824 new_dominator_size_value->ChangeRepresentation(Representation::Integer32());
3826 new_dominator_size_value->InsertBefore(dominator_allocate);
3829 dominator_allocate->UpdateSize(new_dominator_size_value);
3831 if (MustAllocateDoubleAligned()) {
3832 if (!dominator_allocate->MustAllocateDoubleAligned()) {
3833 dominator_allocate->MakeDoubleAligned();
3837 bool keep_new_space_iterable = FLAG_log_gc || FLAG_heap_stats;
3839 keep_new_space_iterable = keep_new_space_iterable || FLAG_verify_heap;
3842 if (keep_new_space_iterable && dominator_allocate->IsNewSpaceAllocation()) {
3843 dominator_allocate->MakePrefillWithFiller();
3845 // TODO(hpayer): This is a short-term hack to make allocation mementos
3846 // work again in new space.
3847 dominator_allocate->ClearNextMapWord(original_object_size);
3850 dominator_allocate->UpdateClearNextMapWord(MustClearNextMapWord());
3852 // After that replace the dominated allocate instruction.
3853 HInstruction* inner_offset = HConstant::CreateAndInsertBefore(
3856 dominator_size_constant,
3857 Representation::None(),
3860 HInstruction* dominated_allocate_instr =
3861 HInnerAllocatedObject::New(zone,
3866 dominated_allocate_instr->InsertBefore(this);
3867 DeleteAndReplaceWith(dominated_allocate_instr);
3868 if (FLAG_trace_allocation_folding) {
3869 PrintF("#%d (%s) folded into #%d (%s)\n",
3870 id(), Mnemonic(), dominator_allocate->id(),
3871 dominator_allocate->Mnemonic());
3877 HAllocate* HAllocate::GetFoldableDominator(HAllocate* dominator) {
3878 if (!IsFoldable(dominator)) {
3879 // We cannot hoist old space allocations over new space allocations.
3880 if (IsNewSpaceAllocation() || dominator->IsNewSpaceAllocation()) {
3881 if (FLAG_trace_allocation_folding) {
3882 PrintF("#%d (%s) cannot fold into #%d (%s), new space hoisting\n",
3883 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3888 HAllocate* dominator_dominator = dominator->dominating_allocate_;
3890 // We can hoist old data space allocations over an old pointer space
3891 // allocation and vice versa. For that we have to check the dominator
3892 // of the dominator allocate instruction.
3893 if (dominator_dominator == NULL) {
3894 dominating_allocate_ = dominator;
3895 if (FLAG_trace_allocation_folding) {
3896 PrintF("#%d (%s) cannot fold into #%d (%s), different spaces\n",
3897 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3902 // We can just fold old space allocations that are in the same basic block,
3903 // since it is not guaranteed that we fill up the whole allocated old
3905 // TODO(hpayer): Remove this limitation and add filler maps for each each
3906 // allocation as soon as we have store elimination.
3907 if (block()->block_id() != dominator_dominator->block()->block_id()) {
3908 if (FLAG_trace_allocation_folding) {
3909 PrintF("#%d (%s) cannot fold into #%d (%s), different basic blocks\n",
3910 id(), Mnemonic(), dominator_dominator->id(),
3911 dominator_dominator->Mnemonic());
3916 DCHECK((IsOldDataSpaceAllocation() &&
3917 dominator_dominator->IsOldDataSpaceAllocation()) ||
3918 (IsOldPointerSpaceAllocation() &&
3919 dominator_dominator->IsOldPointerSpaceAllocation()));
3921 int32_t current_size = HConstant::cast(size())->GetInteger32Constant();
3922 HStoreNamedField* dominator_free_space_size =
3923 dominator->filler_free_space_size_;
3924 if (dominator_free_space_size != NULL) {
3925 // We already hoisted one old space allocation, i.e., we already installed
3926 // a filler map. Hence, we just have to update the free space size.
3927 dominator->UpdateFreeSpaceFiller(current_size);
3929 // This is the first old space allocation that gets hoisted. We have to
3930 // install a filler map since the follwing allocation may cause a GC.
3931 dominator->CreateFreeSpaceFiller(current_size);
3934 // We can hoist the old space allocation over the actual dominator.
3935 return dominator_dominator;
3941 void HAllocate::UpdateFreeSpaceFiller(int32_t free_space_size) {
3942 DCHECK(filler_free_space_size_ != NULL);
3943 Zone* zone = block()->zone();
3944 // We must explicitly force Smi representation here because on x64 we
3945 // would otherwise automatically choose int32, but the actual store
3946 // requires a Smi-tagged value.
3947 HConstant* new_free_space_size = HConstant::CreateAndInsertBefore(
3950 filler_free_space_size_->value()->GetInteger32Constant() +
3952 Representation::Smi(),
3953 filler_free_space_size_);
3954 filler_free_space_size_->UpdateValue(new_free_space_size);
3958 void HAllocate::CreateFreeSpaceFiller(int32_t free_space_size) {
3959 DCHECK(filler_free_space_size_ == NULL);
3960 Zone* zone = block()->zone();
3961 HInstruction* free_space_instr =
3962 HInnerAllocatedObject::New(zone, context(), dominating_allocate_,
3963 dominating_allocate_->size(), type());
3964 free_space_instr->InsertBefore(this);
3965 HConstant* filler_map = HConstant::CreateAndInsertAfter(
3966 zone, Unique<Map>::CreateImmovable(
3967 isolate()->factory()->free_space_map()), true, free_space_instr);
3968 HInstruction* store_map = HStoreNamedField::New(zone, context(),
3969 free_space_instr, HObjectAccess::ForMap(), filler_map);
3970 store_map->SetFlag(HValue::kHasNoObservableSideEffects);
3971 store_map->InsertAfter(filler_map);
3973 // We must explicitly force Smi representation here because on x64 we
3974 // would otherwise automatically choose int32, but the actual store
3975 // requires a Smi-tagged value.
3976 HConstant* filler_size = HConstant::CreateAndInsertAfter(
3977 zone, context(), free_space_size, Representation::Smi(), store_map);
3978 // Must force Smi representation for x64 (see comment above).
3979 HObjectAccess access =
3980 HObjectAccess::ForMapAndOffset(isolate()->factory()->free_space_map(),
3981 FreeSpace::kSizeOffset,
3982 Representation::Smi());
3983 HStoreNamedField* store_size = HStoreNamedField::New(zone, context(),
3984 free_space_instr, access, filler_size);
3985 store_size->SetFlag(HValue::kHasNoObservableSideEffects);
3986 store_size->InsertAfter(filler_size);
3987 filler_free_space_size_ = store_size;
3991 void HAllocate::ClearNextMapWord(int offset) {
3992 if (MustClearNextMapWord()) {
3993 Zone* zone = block()->zone();
3994 HObjectAccess access =
3995 HObjectAccess::ForObservableJSObjectOffset(offset);
3996 HStoreNamedField* clear_next_map =
3997 HStoreNamedField::New(zone, context(), this, access,
3998 block()->graph()->GetConstant0());
3999 clear_next_map->ClearAllSideEffects();
4000 clear_next_map->InsertAfter(this);
4005 OStream& HAllocate::PrintDataTo(OStream& os) const { // NOLINT
4006 os << NameOf(size()) << " (";
4007 if (IsNewSpaceAllocation()) os << "N";
4008 if (IsOldPointerSpaceAllocation()) os << "P";
4009 if (IsOldDataSpaceAllocation()) os << "D";
4010 if (MustAllocateDoubleAligned()) os << "A";
4011 if (MustPrefillWithFiller()) os << "F";
4016 bool HStoreKeyed::TryIncreaseBaseOffset(uint32_t increase_by_value) {
4017 // The base offset is usually simply the size of the array header, except
4018 // with dehoisting adds an addition offset due to a array index key
4019 // manipulation, in which case it becomes (array header size +
4020 // constant-offset-from-key * kPointerSize)
4021 v8::base::internal::CheckedNumeric<uint32_t> addition_result = base_offset_;
4022 addition_result += increase_by_value;
4023 if (!addition_result.IsValid()) return false;
4024 base_offset_ = addition_result.ValueOrDie();
4029 bool HStoreKeyed::NeedsCanonicalization() {
4030 // If value is an integer or smi or comes from the result of a keyed load or
4031 // constant then it is either be a non-hole value or in the case of a constant
4032 // the hole is only being stored explicitly: no need for canonicalization.
4034 // The exception to that is keyed loads from external float or double arrays:
4035 // these can load arbitrary representation of NaN.
4037 if (value()->IsConstant()) {
4041 if (value()->IsLoadKeyed()) {
4042 return IsExternalFloatOrDoubleElementsKind(
4043 HLoadKeyed::cast(value())->elements_kind());
4046 if (value()->IsChange()) {
4047 if (HChange::cast(value())->from().IsSmiOrInteger32()) {
4050 if (HChange::cast(value())->value()->type().IsSmi()) {
4058 #define H_CONSTANT_INT(val) \
4059 HConstant::New(zone, context, static_cast<int32_t>(val))
4060 #define H_CONSTANT_DOUBLE(val) \
4061 HConstant::New(zone, context, static_cast<double>(val))
4063 #define DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HInstr, op) \
4064 HInstruction* HInstr::New( \
4065 Zone* zone, HValue* context, HValue* left, HValue* right) { \
4066 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) { \
4067 HConstant* c_left = HConstant::cast(left); \
4068 HConstant* c_right = HConstant::cast(right); \
4069 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) { \
4070 double double_res = c_left->DoubleValue() op c_right->DoubleValue(); \
4071 if (IsInt32Double(double_res)) { \
4072 return H_CONSTANT_INT(double_res); \
4074 return H_CONSTANT_DOUBLE(double_res); \
4077 return new(zone) HInstr(context, left, right); \
4081 DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HAdd, +)
4082 DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HMul, *)
4083 DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HSub, -)
4085 #undef DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR
4088 HInstruction* HStringAdd::New(Zone* zone,
4092 PretenureFlag pretenure_flag,
4093 StringAddFlags flags,
4094 Handle<AllocationSite> allocation_site) {
4095 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4096 HConstant* c_right = HConstant::cast(right);
4097 HConstant* c_left = HConstant::cast(left);
4098 if (c_left->HasStringValue() && c_right->HasStringValue()) {
4099 Handle<String> left_string = c_left->StringValue();
4100 Handle<String> right_string = c_right->StringValue();
4101 // Prevent possible exception by invalid string length.
4102 if (left_string->length() + right_string->length() < String::kMaxLength) {
4103 MaybeHandle<String> concat = zone->isolate()->factory()->NewConsString(
4104 c_left->StringValue(), c_right->StringValue());
4105 return HConstant::New(zone, context, concat.ToHandleChecked());
4109 return new(zone) HStringAdd(
4110 context, left, right, pretenure_flag, flags, allocation_site);
4114 OStream& HStringAdd::PrintDataTo(OStream& os) const { // NOLINT
4115 if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_BOTH) {
4117 } else if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_LEFT) {
4119 } else if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_RIGHT) {
4120 os << "_CheckRight";
4122 HBinaryOperation::PrintDataTo(os);
4124 if (pretenure_flag() == NOT_TENURED)
4126 else if (pretenure_flag() == TENURED)
4132 HInstruction* HStringCharFromCode::New(
4133 Zone* zone, HValue* context, HValue* char_code) {
4134 if (FLAG_fold_constants && char_code->IsConstant()) {
4135 HConstant* c_code = HConstant::cast(char_code);
4136 Isolate* isolate = zone->isolate();
4137 if (c_code->HasNumberValue()) {
4138 if (std::isfinite(c_code->DoubleValue())) {
4139 uint32_t code = c_code->NumberValueAsInteger32() & 0xffff;
4140 return HConstant::New(zone, context,
4141 isolate->factory()->LookupSingleCharacterStringFromCode(code));
4143 return HConstant::New(zone, context, isolate->factory()->empty_string());
4146 return new(zone) HStringCharFromCode(context, char_code);
4150 HInstruction* HUnaryMathOperation::New(
4151 Zone* zone, HValue* context, HValue* value, BuiltinFunctionId op) {
4153 if (!FLAG_fold_constants) break;
4154 if (!value->IsConstant()) break;
4155 HConstant* constant = HConstant::cast(value);
4156 if (!constant->HasNumberValue()) break;
4157 double d = constant->DoubleValue();
4158 if (std::isnan(d)) { // NaN poisons everything.
4159 return H_CONSTANT_DOUBLE(base::OS::nan_value());
4161 if (std::isinf(d)) { // +Infinity and -Infinity.
4164 return H_CONSTANT_DOUBLE((d > 0.0) ? d : 0.0);
4167 return H_CONSTANT_DOUBLE((d > 0.0) ? d : base::OS::nan_value());
4170 return H_CONSTANT_DOUBLE((d > 0.0) ? d : -d);
4174 return H_CONSTANT_DOUBLE(d);
4176 return H_CONSTANT_INT(32);
4184 return H_CONSTANT_DOUBLE(fast_exp(d));
4186 return H_CONSTANT_DOUBLE(std::log(d));
4188 return H_CONSTANT_DOUBLE(fast_sqrt(d));
4190 return H_CONSTANT_DOUBLE(power_double_double(d, 0.5));
4192 return H_CONSTANT_DOUBLE((d >= 0.0) ? d + 0.0 : -d);
4194 // -0.5 .. -0.0 round to -0.0.
4195 if ((d >= -0.5 && Double(d).Sign() < 0)) return H_CONSTANT_DOUBLE(-0.0);
4196 // Doubles are represented as Significant * 2 ^ Exponent. If the
4197 // Exponent is not negative, the double value is already an integer.
4198 if (Double(d).Exponent() >= 0) return H_CONSTANT_DOUBLE(d);
4199 return H_CONSTANT_DOUBLE(Floor(d + 0.5));
4201 return H_CONSTANT_DOUBLE(static_cast<double>(static_cast<float>(d)));
4203 return H_CONSTANT_DOUBLE(Floor(d));
4205 uint32_t i = DoubleToUint32(d);
4206 return H_CONSTANT_INT(
4207 (i == 0) ? 32 : CompilerIntrinsics::CountLeadingZeros(i));
4214 return new(zone) HUnaryMathOperation(context, value, op);
4218 Representation HUnaryMathOperation::RepresentationFromUses() {
4219 if (op_ != kMathFloor && op_ != kMathRound) {
4220 return HValue::RepresentationFromUses();
4223 // The instruction can have an int32 or double output. Prefer a double
4224 // representation if there are double uses.
4225 bool use_double = false;
4227 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4228 HValue* use = it.value();
4229 int use_index = it.index();
4230 Representation rep_observed = use->observed_input_representation(use_index);
4231 Representation rep_required = use->RequiredInputRepresentation(use_index);
4232 use_double |= (rep_observed.IsDouble() || rep_required.IsDouble());
4233 if (use_double && !FLAG_trace_representation) {
4234 // Having seen one double is enough.
4237 if (FLAG_trace_representation) {
4238 if (!rep_required.IsDouble() || rep_observed.IsDouble()) {
4239 PrintF("#%d %s is used by #%d %s as %s%s\n",
4240 id(), Mnemonic(), use->id(),
4241 use->Mnemonic(), rep_observed.Mnemonic(),
4242 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
4244 PrintF("#%d %s is required by #%d %s as %s%s\n",
4245 id(), Mnemonic(), use->id(),
4246 use->Mnemonic(), rep_required.Mnemonic(),
4247 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
4251 return use_double ? Representation::Double() : Representation::Integer32();
4255 HInstruction* HPower::New(Zone* zone,
4259 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4260 HConstant* c_left = HConstant::cast(left);
4261 HConstant* c_right = HConstant::cast(right);
4262 if (c_left->HasNumberValue() && c_right->HasNumberValue()) {
4263 double result = power_helper(c_left->DoubleValue(),
4264 c_right->DoubleValue());
4265 return H_CONSTANT_DOUBLE(std::isnan(result) ? base::OS::nan_value()
4269 return new(zone) HPower(left, right);
4273 HInstruction* HMathMinMax::New(
4274 Zone* zone, HValue* context, HValue* left, HValue* right, Operation op) {
4275 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4276 HConstant* c_left = HConstant::cast(left);
4277 HConstant* c_right = HConstant::cast(right);
4278 if (c_left->HasNumberValue() && c_right->HasNumberValue()) {
4279 double d_left = c_left->DoubleValue();
4280 double d_right = c_right->DoubleValue();
4281 if (op == kMathMin) {
4282 if (d_left > d_right) return H_CONSTANT_DOUBLE(d_right);
4283 if (d_left < d_right) return H_CONSTANT_DOUBLE(d_left);
4284 if (d_left == d_right) {
4285 // Handle +0 and -0.
4286 return H_CONSTANT_DOUBLE((Double(d_left).Sign() == -1) ? d_left
4290 if (d_left < d_right) return H_CONSTANT_DOUBLE(d_right);
4291 if (d_left > d_right) return H_CONSTANT_DOUBLE(d_left);
4292 if (d_left == d_right) {
4293 // Handle +0 and -0.
4294 return H_CONSTANT_DOUBLE((Double(d_left).Sign() == -1) ? d_right
4298 // All comparisons failed, must be NaN.
4299 return H_CONSTANT_DOUBLE(base::OS::nan_value());
4302 return new(zone) HMathMinMax(context, left, right, op);
4306 HInstruction* HMod::New(Zone* zone,
4310 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4311 HConstant* c_left = HConstant::cast(left);
4312 HConstant* c_right = HConstant::cast(right);
4313 if (c_left->HasInteger32Value() && c_right->HasInteger32Value()) {
4314 int32_t dividend = c_left->Integer32Value();
4315 int32_t divisor = c_right->Integer32Value();
4316 if (dividend == kMinInt && divisor == -1) {
4317 return H_CONSTANT_DOUBLE(-0.0);
4320 int32_t res = dividend % divisor;
4321 if ((res == 0) && (dividend < 0)) {
4322 return H_CONSTANT_DOUBLE(-0.0);
4324 return H_CONSTANT_INT(res);
4328 return new(zone) HMod(context, left, right);
4332 HInstruction* HDiv::New(
4333 Zone* zone, HValue* context, HValue* left, HValue* right) {
4334 // If left and right are constant values, try to return a constant value.
4335 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4336 HConstant* c_left = HConstant::cast(left);
4337 HConstant* c_right = HConstant::cast(right);
4338 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4339 if (c_right->DoubleValue() != 0) {
4340 double double_res = c_left->DoubleValue() / c_right->DoubleValue();
4341 if (IsInt32Double(double_res)) {
4342 return H_CONSTANT_INT(double_res);
4344 return H_CONSTANT_DOUBLE(double_res);
4346 int sign = Double(c_left->DoubleValue()).Sign() *
4347 Double(c_right->DoubleValue()).Sign(); // Right could be -0.
4348 return H_CONSTANT_DOUBLE(sign * V8_INFINITY);
4352 return new(zone) HDiv(context, left, right);
4356 HInstruction* HBitwise::New(
4357 Zone* zone, HValue* context, Token::Value op, HValue* left, HValue* right) {
4358 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4359 HConstant* c_left = HConstant::cast(left);
4360 HConstant* c_right = HConstant::cast(right);
4361 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4363 int32_t v_left = c_left->NumberValueAsInteger32();
4364 int32_t v_right = c_right->NumberValueAsInteger32();
4366 case Token::BIT_XOR:
4367 result = v_left ^ v_right;
4369 case Token::BIT_AND:
4370 result = v_left & v_right;
4373 result = v_left | v_right;
4376 result = 0; // Please the compiler.
4379 return H_CONSTANT_INT(result);
4382 return new(zone) HBitwise(context, op, left, right);
4386 #define DEFINE_NEW_H_BITWISE_INSTR(HInstr, result) \
4387 HInstruction* HInstr::New( \
4388 Zone* zone, HValue* context, HValue* left, HValue* right) { \
4389 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) { \
4390 HConstant* c_left = HConstant::cast(left); \
4391 HConstant* c_right = HConstant::cast(right); \
4392 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) { \
4393 return H_CONSTANT_INT(result); \
4396 return new(zone) HInstr(context, left, right); \
4400 DEFINE_NEW_H_BITWISE_INSTR(HSar,
4401 c_left->NumberValueAsInteger32() >> (c_right->NumberValueAsInteger32() & 0x1f))
4402 DEFINE_NEW_H_BITWISE_INSTR(HShl,
4403 c_left->NumberValueAsInteger32() << (c_right->NumberValueAsInteger32() & 0x1f))
4405 #undef DEFINE_NEW_H_BITWISE_INSTR
4408 HInstruction* HShr::New(
4409 Zone* zone, HValue* context, HValue* left, HValue* right) {
4410 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4411 HConstant* c_left = HConstant::cast(left);
4412 HConstant* c_right = HConstant::cast(right);
4413 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4414 int32_t left_val = c_left->NumberValueAsInteger32();
4415 int32_t right_val = c_right->NumberValueAsInteger32() & 0x1f;
4416 if ((right_val == 0) && (left_val < 0)) {
4417 return H_CONSTANT_DOUBLE(static_cast<uint32_t>(left_val));
4419 return H_CONSTANT_INT(static_cast<uint32_t>(left_val) >> right_val);
4422 return new(zone) HShr(context, left, right);
4426 HInstruction* HSeqStringGetChar::New(Zone* zone,
4428 String::Encoding encoding,
4431 if (FLAG_fold_constants && string->IsConstant() && index->IsConstant()) {
4432 HConstant* c_string = HConstant::cast(string);
4433 HConstant* c_index = HConstant::cast(index);
4434 if (c_string->HasStringValue() && c_index->HasInteger32Value()) {
4435 Handle<String> s = c_string->StringValue();
4436 int32_t i = c_index->Integer32Value();
4438 DCHECK_LT(i, s->length());
4439 return H_CONSTANT_INT(s->Get(i));
4442 return new(zone) HSeqStringGetChar(encoding, string, index);
4446 #undef H_CONSTANT_INT
4447 #undef H_CONSTANT_DOUBLE
4450 OStream& HBitwise::PrintDataTo(OStream& os) const { // NOLINT
4451 os << Token::Name(op_) << " ";
4452 return HBitwiseBinaryOperation::PrintDataTo(os);
4456 void HPhi::SimplifyConstantInputs() {
4457 // Convert constant inputs to integers when all uses are truncating.
4458 // This must happen before representation inference takes place.
4459 if (!CheckUsesForFlag(kTruncatingToInt32)) return;
4460 for (int i = 0; i < OperandCount(); ++i) {
4461 if (!OperandAt(i)->IsConstant()) return;
4463 HGraph* graph = block()->graph();
4464 for (int i = 0; i < OperandCount(); ++i) {
4465 HConstant* operand = HConstant::cast(OperandAt(i));
4466 if (operand->HasInteger32Value()) {
4468 } else if (operand->HasDoubleValue()) {
4469 HConstant* integer_input =
4470 HConstant::New(graph->zone(), graph->GetInvalidContext(),
4471 DoubleToInt32(operand->DoubleValue()));
4472 integer_input->InsertAfter(operand);
4473 SetOperandAt(i, integer_input);
4474 } else if (operand->HasBooleanValue()) {
4475 SetOperandAt(i, operand->BooleanValue() ? graph->GetConstant1()
4476 : graph->GetConstant0());
4477 } else if (operand->ImmortalImmovable()) {
4478 SetOperandAt(i, graph->GetConstant0());
4481 // Overwrite observed input representations because they are likely Tagged.
4482 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4483 HValue* use = it.value();
4484 if (use->IsBinaryOperation()) {
4485 HBinaryOperation::cast(use)->set_observed_input_representation(
4486 it.index(), Representation::Smi());
4492 void HPhi::InferRepresentation(HInferRepresentationPhase* h_infer) {
4493 DCHECK(CheckFlag(kFlexibleRepresentation));
4494 Representation new_rep = RepresentationFromInputs();
4495 UpdateRepresentation(new_rep, h_infer, "inputs");
4496 new_rep = RepresentationFromUses();
4497 UpdateRepresentation(new_rep, h_infer, "uses");
4498 new_rep = RepresentationFromUseRequirements();
4499 UpdateRepresentation(new_rep, h_infer, "use requirements");
4503 Representation HPhi::RepresentationFromInputs() {
4504 Representation r = Representation::None();
4505 for (int i = 0; i < OperandCount(); ++i) {
4506 r = r.generalize(OperandAt(i)->KnownOptimalRepresentation());
4512 // Returns a representation if all uses agree on the same representation.
4513 // Integer32 is also returned when some uses are Smi but others are Integer32.
4514 Representation HValue::RepresentationFromUseRequirements() {
4515 Representation rep = Representation::None();
4516 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4517 // Ignore the use requirement from never run code
4518 if (it.value()->block()->IsUnreachable()) continue;
4520 // We check for observed_input_representation elsewhere.
4521 Representation use_rep =
4522 it.value()->RequiredInputRepresentation(it.index());
4527 if (use_rep.IsNone() || rep.Equals(use_rep)) continue;
4528 if (rep.generalize(use_rep).IsInteger32()) {
4529 rep = Representation::Integer32();
4532 return Representation::None();
4538 bool HValue::HasNonSmiUse() {
4539 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4540 // We check for observed_input_representation elsewhere.
4541 Representation use_rep =
4542 it.value()->RequiredInputRepresentation(it.index());
4543 if (!use_rep.IsNone() &&
4545 !use_rep.IsTagged()) {
4553 // Node-specific verification code is only included in debug mode.
4556 void HPhi::Verify() {
4557 DCHECK(OperandCount() == block()->predecessors()->length());
4558 for (int i = 0; i < OperandCount(); ++i) {
4559 HValue* value = OperandAt(i);
4560 HBasicBlock* defining_block = value->block();
4561 HBasicBlock* predecessor_block = block()->predecessors()->at(i);
4562 DCHECK(defining_block == predecessor_block ||
4563 defining_block->Dominates(predecessor_block));
4568 void HSimulate::Verify() {
4569 HInstruction::Verify();
4570 DCHECK(HasAstId() || next()->IsEnterInlined());
4574 void HCheckHeapObject::Verify() {
4575 HInstruction::Verify();
4576 DCHECK(HasNoUses());
4580 void HCheckValue::Verify() {
4581 HInstruction::Verify();
4582 DCHECK(HasNoUses());
4588 HObjectAccess HObjectAccess::ForFixedArrayHeader(int offset) {
4589 DCHECK(offset >= 0);
4590 DCHECK(offset < FixedArray::kHeaderSize);
4591 if (offset == FixedArray::kLengthOffset) return ForFixedArrayLength();
4592 return HObjectAccess(kInobject, offset);
4596 HObjectAccess HObjectAccess::ForMapAndOffset(Handle<Map> map, int offset,
4597 Representation representation) {
4598 DCHECK(offset >= 0);
4599 Portion portion = kInobject;
4601 if (offset == JSObject::kElementsOffset) {
4602 portion = kElementsPointer;
4603 } else if (offset == JSObject::kMapOffset) {
4606 bool existing_inobject_property = true;
4607 if (!map.is_null()) {
4608 existing_inobject_property = (offset <
4609 map->instance_size() - map->unused_property_fields() * kPointerSize);
4611 return HObjectAccess(portion, offset, representation, Handle<String>::null(),
4612 false, existing_inobject_property);
4616 HObjectAccess HObjectAccess::ForAllocationSiteOffset(int offset) {
4618 case AllocationSite::kTransitionInfoOffset:
4619 return HObjectAccess(kInobject, offset, Representation::Tagged());
4620 case AllocationSite::kNestedSiteOffset:
4621 return HObjectAccess(kInobject, offset, Representation::Tagged());
4622 case AllocationSite::kPretenureDataOffset:
4623 return HObjectAccess(kInobject, offset, Representation::Smi());
4624 case AllocationSite::kPretenureCreateCountOffset:
4625 return HObjectAccess(kInobject, offset, Representation::Smi());
4626 case AllocationSite::kDependentCodeOffset:
4627 return HObjectAccess(kInobject, offset, Representation::Tagged());
4628 case AllocationSite::kWeakNextOffset:
4629 return HObjectAccess(kInobject, offset, Representation::Tagged());
4633 return HObjectAccess(kInobject, offset);
4637 HObjectAccess HObjectAccess::ForContextSlot(int index) {
4639 Portion portion = kInobject;
4640 int offset = Context::kHeaderSize + index * kPointerSize;
4641 DCHECK_EQ(offset, Context::SlotOffset(index) + kHeapObjectTag);
4642 return HObjectAccess(portion, offset, Representation::Tagged());
4646 HObjectAccess HObjectAccess::ForJSArrayOffset(int offset) {
4647 DCHECK(offset >= 0);
4648 Portion portion = kInobject;
4650 if (offset == JSObject::kElementsOffset) {
4651 portion = kElementsPointer;
4652 } else if (offset == JSArray::kLengthOffset) {
4653 portion = kArrayLengths;
4654 } else if (offset == JSObject::kMapOffset) {
4657 return HObjectAccess(portion, offset);
4661 HObjectAccess HObjectAccess::ForBackingStoreOffset(int offset,
4662 Representation representation) {
4663 DCHECK(offset >= 0);
4664 return HObjectAccess(kBackingStore, offset, representation,
4665 Handle<String>::null(), false, false);
4669 HObjectAccess HObjectAccess::ForField(Handle<Map> map,
4670 LookupResult* lookup,
4671 Handle<String> name) {
4672 DCHECK(lookup->IsField() || lookup->IsTransitionToField());
4674 Representation representation;
4675 if (lookup->IsField()) {
4676 index = lookup->GetLocalFieldIndexFromMap(*map);
4677 representation = lookup->representation();
4679 Map* transition = lookup->GetTransitionTarget();
4680 int descriptor = transition->LastAdded();
4681 index = transition->instance_descriptors()->GetFieldIndex(descriptor) -
4682 map->inobject_properties();
4683 PropertyDetails details =
4684 transition->instance_descriptors()->GetDetails(descriptor);
4685 representation = details.representation();
4688 // Negative property indices are in-object properties, indexed
4689 // from the end of the fixed part of the object.
4690 int offset = (index * kPointerSize) + map->instance_size();
4691 return HObjectAccess(kInobject, offset, representation, name, false, true);
4693 // Non-negative property indices are in the properties array.
4694 int offset = (index * kPointerSize) + FixedArray::kHeaderSize;
4695 return HObjectAccess(kBackingStore, offset, representation, name,
4701 HObjectAccess HObjectAccess::ForCellPayload(Isolate* isolate) {
4702 return HObjectAccess(
4703 kInobject, Cell::kValueOffset, Representation::Tagged(),
4704 Handle<String>(isolate->heap()->cell_value_string()));
4708 void HObjectAccess::SetGVNFlags(HValue *instr, PropertyAccessType access_type) {
4709 // set the appropriate GVN flags for a given load or store instruction
4710 if (access_type == STORE) {
4711 // track dominating allocations in order to eliminate write barriers
4712 instr->SetDependsOnFlag(::v8::internal::kNewSpacePromotion);
4713 instr->SetFlag(HValue::kTrackSideEffectDominators);
4715 // try to GVN loads, but don't hoist above map changes
4716 instr->SetFlag(HValue::kUseGVN);
4717 instr->SetDependsOnFlag(::v8::internal::kMaps);
4720 switch (portion()) {
4722 if (access_type == STORE) {
4723 instr->SetChangesFlag(::v8::internal::kArrayLengths);
4725 instr->SetDependsOnFlag(::v8::internal::kArrayLengths);
4728 case kStringLengths:
4729 if (access_type == STORE) {
4730 instr->SetChangesFlag(::v8::internal::kStringLengths);
4732 instr->SetDependsOnFlag(::v8::internal::kStringLengths);
4736 if (access_type == STORE) {
4737 instr->SetChangesFlag(::v8::internal::kInobjectFields);
4739 instr->SetDependsOnFlag(::v8::internal::kInobjectFields);
4743 if (access_type == STORE) {
4744 instr->SetChangesFlag(::v8::internal::kDoubleFields);
4746 instr->SetDependsOnFlag(::v8::internal::kDoubleFields);
4750 if (access_type == STORE) {
4751 instr->SetChangesFlag(::v8::internal::kBackingStoreFields);
4753 instr->SetDependsOnFlag(::v8::internal::kBackingStoreFields);
4756 case kElementsPointer:
4757 if (access_type == STORE) {
4758 instr->SetChangesFlag(::v8::internal::kElementsPointer);
4760 instr->SetDependsOnFlag(::v8::internal::kElementsPointer);
4764 if (access_type == STORE) {
4765 instr->SetChangesFlag(::v8::internal::kMaps);
4767 instr->SetDependsOnFlag(::v8::internal::kMaps);
4770 case kExternalMemory:
4771 if (access_type == STORE) {
4772 instr->SetChangesFlag(::v8::internal::kExternalMemory);
4774 instr->SetDependsOnFlag(::v8::internal::kExternalMemory);
4781 OStream& operator<<(OStream& os, const HObjectAccess& access) {
4784 switch (access.portion()) {
4785 case HObjectAccess::kArrayLengths:
4786 case HObjectAccess::kStringLengths:
4789 case HObjectAccess::kElementsPointer:
4792 case HObjectAccess::kMaps:
4795 case HObjectAccess::kDouble: // fall through
4796 case HObjectAccess::kInobject:
4797 if (!access.name().is_null()) {
4798 os << Handle<String>::cast(access.name())->ToCString().get();
4800 os << "[in-object]";
4802 case HObjectAccess::kBackingStore:
4803 if (!access.name().is_null()) {
4804 os << Handle<String>::cast(access.name())->ToCString().get();
4806 os << "[backing-store]";
4808 case HObjectAccess::kExternalMemory:
4809 os << "[external-memory]";
4813 return os << "@" << access.offset();
4817 HInstruction* HNullarySIMDOperation::New(
4818 Zone* zone, HValue* context, BuiltinFunctionId op) {
4819 return new(zone) HNullarySIMDOperation(context, op);
4823 HInstruction* HUnarySIMDOperation::New(
4824 Zone* zone, HValue* context, HValue* value, BuiltinFunctionId op,
4825 Representation to) {
4826 return new(zone) HUnarySIMDOperation(context, value, op, to);
4830 HInstruction* HBinarySIMDOperation::New(
4831 Zone* zone, HValue* context, HValue* left, HValue* right,
4832 BuiltinFunctionId op) {
4833 return new(zone) HBinarySIMDOperation(context, left, right, op);
4837 HInstruction* HTernarySIMDOperation::New(
4838 Zone* zone, HValue* context, HValue* mask, HValue* left, HValue* right,
4839 BuiltinFunctionId op) {
4840 return new(zone) HTernarySIMDOperation(context, mask, left, right, op);
4844 HInstruction* HQuarternarySIMDOperation::New(
4845 Zone* zone, HValue* context, HValue* x, HValue* y, HValue* z, HValue* w,
4846 BuiltinFunctionId op) {
4847 return new(zone) HQuarternarySIMDOperation(context, x, y, z, w, op);
4851 const char* HNullarySIMDOperation::OpName() const {
4853 #define SIMD_NULLARY_OPERATION_CASE_ITEM(module, function, name, p4) \
4855 return #module "." #function;
4856 SIMD_NULLARY_OPERATIONS(SIMD_NULLARY_OPERATION_CASE_ITEM)
4857 #undef SIMD_NULLARY_OPERATION_CASE_ITEM
4865 OStream& HNullarySIMDOperation::PrintDataTo(OStream& os) const {
4866 return os << OpName();
4870 const char* HUnarySIMDOperation::OpName() const {
4872 #define SIMD_UNARY_OPERATION_CASE_ITEM(module, function, name, p4, p5) \
4874 return #module "." #function;
4875 SIMD_UNARY_OPERATIONS(SIMD_UNARY_OPERATION_CASE_ITEM)
4876 SIMD_UNARY_OPERATIONS_FOR_PROPERTY_ACCESS(SIMD_UNARY_OPERATION_CASE_ITEM)
4877 #undef SIMD_UNARY_OPERATION_CASE_ITEM
4885 OStream& HUnarySIMDOperation::PrintDataTo(OStream& os) const {
4886 return os << OpName() << " " << NameOf(value());
4890 const char* HBinarySIMDOperation::OpName() const {
4892 #define SIMD_BINARY_OPERATION_CASE_ITEM(module, function, name, p4, p5, p6) \
4894 return #module "." #function;
4895 SIMD_BINARY_OPERATIONS(SIMD_BINARY_OPERATION_CASE_ITEM)
4896 #undef SIMD_BINARY_OPERATION_CASE_ITEM
4904 OStream& HBinarySIMDOperation::PrintDataTo(OStream& os) const {
4905 return os << OpName() << " " << NameOf(left()) << " "
4910 const char* HTernarySIMDOperation::OpName() const {
4912 #define SIMD_TERNARY_OPERATION_CASE_ITEM(module, function, name, p4, p5, p6, \
4915 return #module "." #function;
4916 SIMD_TERNARY_OPERATIONS(SIMD_TERNARY_OPERATION_CASE_ITEM)
4917 #undef SIMD_TERNARY_OPERATION_CASE_ITEM
4925 OStream& HTernarySIMDOperation::PrintDataTo(OStream& os) const {
4926 return os << OpName() << " " << NameOf(first()) << " "
4927 << NameOf(second()) << " " << NameOf(third());
4931 const char* HQuarternarySIMDOperation::OpName() const {
4933 #define SIMD_QUARTERNARY_OPERATION_CASE_ITEM(module, function, name, p4, p5, \
4936 return #module "." #function;
4937 SIMD_QUARTERNARY_OPERATIONS(SIMD_QUARTERNARY_OPERATION_CASE_ITEM)
4938 #undef SIMD_QUARTERNARY_OPERATION_CASE_ITEM
4946 OStream& HQuarternarySIMDOperation::PrintDataTo(OStream& os) const {
4947 return os << OpName() << " " << NameOf(x()) << " " << NameOf(y()) << " "
4948 << NameOf(z()) << " " << NameOf(w());
4952 } } // namespace v8::internal