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/base/bits.h"
8 #include "src/double.h"
9 #include "src/elements.h"
10 #include "src/factory.h"
11 #include "src/hydrogen-infer-representation.h"
13 #if V8_TARGET_ARCH_IA32
14 #include "src/ia32/lithium-ia32.h" // NOLINT
15 #elif V8_TARGET_ARCH_X64
16 #include "src/x64/lithium-x64.h" // NOLINT
17 #elif V8_TARGET_ARCH_ARM64
18 #include "src/arm64/lithium-arm64.h" // NOLINT
19 #elif V8_TARGET_ARCH_ARM
20 #include "src/arm/lithium-arm.h" // NOLINT
21 #elif V8_TARGET_ARCH_PPC
22 #include "src/ppc/lithium-ppc.h" // NOLINT
23 #elif V8_TARGET_ARCH_MIPS
24 #include "src/mips/lithium-mips.h" // NOLINT
25 #elif V8_TARGET_ARCH_MIPS64
26 #include "src/mips64/lithium-mips64.h" // NOLINT
27 #elif V8_TARGET_ARCH_X87
28 #include "src/x87/lithium-x87.h" // NOLINT
30 #error Unsupported target architecture.
33 #include "src/base/safe_math.h"
38 #define DEFINE_COMPILE(type) \
39 LInstruction* H##type::CompileToLithium(LChunkBuilder* builder) { \
40 return builder->Do##type(this); \
42 HYDROGEN_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
46 Isolate* HValue::isolate() const {
47 DCHECK(block() != NULL);
48 return block()->isolate();
52 void HValue::AssumeRepresentation(Representation r) {
53 if (CheckFlag(kFlexibleRepresentation)) {
54 ChangeRepresentation(r);
55 // The representation of the value is dictated by type feedback and
56 // will not be changed later.
57 ClearFlag(kFlexibleRepresentation);
62 void HValue::InferRepresentation(HInferRepresentationPhase* h_infer) {
63 DCHECK(CheckFlag(kFlexibleRepresentation));
64 Representation new_rep = RepresentationFromInputs();
65 UpdateRepresentation(new_rep, h_infer, "inputs");
66 new_rep = RepresentationFromUses();
67 UpdateRepresentation(new_rep, h_infer, "uses");
68 if (representation().IsSmi() && HasNonSmiUse()) {
70 Representation::Integer32(), h_infer, "use requirements");
75 Representation HValue::RepresentationFromUses() {
76 if (HasNoUses()) return Representation::None();
78 // Array of use counts for each representation.
79 int use_count[Representation::kNumRepresentations] = { 0 };
81 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
82 HValue* use = it.value();
83 Representation rep = use->observed_input_representation(it.index());
84 if (rep.IsNone()) continue;
85 if (FLAG_trace_representation) {
86 PrintF("#%d %s is used by #%d %s as %s%s\n",
87 id(), Mnemonic(), use->id(), use->Mnemonic(), rep.Mnemonic(),
88 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
90 use_count[rep.kind()] += 1;
92 if (IsPhi()) HPhi::cast(this)->AddIndirectUsesTo(&use_count[0]);
93 int tagged_count = use_count[Representation::kTagged];
94 int double_count = use_count[Representation::kDouble];
95 int int32_count = use_count[Representation::kInteger32];
96 int smi_count = use_count[Representation::kSmi];
98 if (tagged_count > 0) return Representation::Tagged();
99 if (double_count > 0) return Representation::Double();
100 if (int32_count > 0) return Representation::Integer32();
101 if (smi_count > 0) return Representation::Smi();
103 return Representation::None();
107 void HValue::UpdateRepresentation(Representation new_rep,
108 HInferRepresentationPhase* h_infer,
109 const char* reason) {
110 Representation r = representation();
111 if (new_rep.is_more_general_than(r)) {
112 if (CheckFlag(kCannotBeTagged) && new_rep.IsTagged()) return;
113 if (FLAG_trace_representation) {
114 PrintF("Changing #%d %s representation %s -> %s based on %s\n",
115 id(), Mnemonic(), r.Mnemonic(), new_rep.Mnemonic(), reason);
117 ChangeRepresentation(new_rep);
118 AddDependantsToWorklist(h_infer);
123 void HValue::AddDependantsToWorklist(HInferRepresentationPhase* h_infer) {
124 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
125 h_infer->AddToWorklist(it.value());
127 for (int i = 0; i < OperandCount(); ++i) {
128 h_infer->AddToWorklist(OperandAt(i));
133 static int32_t ConvertAndSetOverflow(Representation r,
137 if (result > Smi::kMaxValue) {
139 return Smi::kMaxValue;
141 if (result < Smi::kMinValue) {
143 return Smi::kMinValue;
146 if (result > kMaxInt) {
150 if (result < kMinInt) {
155 return static_cast<int32_t>(result);
159 static int32_t AddWithoutOverflow(Representation r,
163 int64_t result = static_cast<int64_t>(a) + static_cast<int64_t>(b);
164 return ConvertAndSetOverflow(r, result, overflow);
168 static int32_t SubWithoutOverflow(Representation r,
172 int64_t result = static_cast<int64_t>(a) - static_cast<int64_t>(b);
173 return ConvertAndSetOverflow(r, result, overflow);
177 static int32_t MulWithoutOverflow(const Representation& r,
181 int64_t result = static_cast<int64_t>(a) * static_cast<int64_t>(b);
182 return ConvertAndSetOverflow(r, result, overflow);
186 int32_t Range::Mask() const {
187 if (lower_ == upper_) return lower_;
190 while (res < upper_) {
191 res = (res << 1) | 1;
199 void Range::AddConstant(int32_t value) {
200 if (value == 0) return;
201 bool may_overflow = false; // Overflow is ignored here.
202 Representation r = Representation::Integer32();
203 lower_ = AddWithoutOverflow(r, lower_, value, &may_overflow);
204 upper_ = AddWithoutOverflow(r, upper_, value, &may_overflow);
211 void Range::Intersect(Range* other) {
212 upper_ = Min(upper_, other->upper_);
213 lower_ = Max(lower_, other->lower_);
214 bool b = CanBeMinusZero() && other->CanBeMinusZero();
215 set_can_be_minus_zero(b);
219 void Range::Union(Range* other) {
220 upper_ = Max(upper_, other->upper_);
221 lower_ = Min(lower_, other->lower_);
222 bool b = CanBeMinusZero() || other->CanBeMinusZero();
223 set_can_be_minus_zero(b);
227 void Range::CombinedMax(Range* other) {
228 upper_ = Max(upper_, other->upper_);
229 lower_ = Max(lower_, other->lower_);
230 set_can_be_minus_zero(CanBeMinusZero() || other->CanBeMinusZero());
234 void Range::CombinedMin(Range* other) {
235 upper_ = Min(upper_, other->upper_);
236 lower_ = Min(lower_, other->lower_);
237 set_can_be_minus_zero(CanBeMinusZero() || other->CanBeMinusZero());
241 void Range::Sar(int32_t value) {
242 int32_t bits = value & 0x1F;
243 lower_ = lower_ >> bits;
244 upper_ = upper_ >> bits;
245 set_can_be_minus_zero(false);
249 void Range::Shl(int32_t value) {
250 int32_t bits = value & 0x1F;
251 int old_lower = lower_;
252 int old_upper = upper_;
253 lower_ = lower_ << bits;
254 upper_ = upper_ << bits;
255 if (old_lower != lower_ >> bits || old_upper != upper_ >> bits) {
259 set_can_be_minus_zero(false);
263 bool Range::AddAndCheckOverflow(const Representation& r, Range* other) {
264 bool may_overflow = false;
265 lower_ = AddWithoutOverflow(r, lower_, other->lower(), &may_overflow);
266 upper_ = AddWithoutOverflow(r, upper_, other->upper(), &may_overflow);
275 bool Range::SubAndCheckOverflow(const Representation& r, Range* other) {
276 bool may_overflow = false;
277 lower_ = SubWithoutOverflow(r, lower_, other->upper(), &may_overflow);
278 upper_ = SubWithoutOverflow(r, upper_, other->lower(), &may_overflow);
287 void Range::KeepOrder() {
288 if (lower_ > upper_) {
289 int32_t tmp = lower_;
297 void Range::Verify() const {
298 DCHECK(lower_ <= upper_);
303 bool Range::MulAndCheckOverflow(const Representation& r, Range* other) {
304 bool may_overflow = false;
305 int v1 = MulWithoutOverflow(r, lower_, other->lower(), &may_overflow);
306 int v2 = MulWithoutOverflow(r, lower_, other->upper(), &may_overflow);
307 int v3 = MulWithoutOverflow(r, upper_, other->lower(), &may_overflow);
308 int v4 = MulWithoutOverflow(r, upper_, other->upper(), &may_overflow);
309 lower_ = Min(Min(v1, v2), Min(v3, v4));
310 upper_ = Max(Max(v1, v2), Max(v3, v4));
318 bool HValue::IsDefinedAfter(HBasicBlock* other) const {
319 return block()->block_id() > other->block_id();
323 HUseListNode* HUseListNode::tail() {
324 // Skip and remove dead items in the use list.
325 while (tail_ != NULL && tail_->value()->CheckFlag(HValue::kIsDead)) {
326 tail_ = tail_->tail_;
332 bool HValue::CheckUsesForFlag(Flag f) const {
333 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
334 if (it.value()->IsSimulate()) continue;
335 if (!it.value()->CheckFlag(f)) return false;
341 bool HValue::CheckUsesForFlag(Flag f, HValue** value) const {
342 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
343 if (it.value()->IsSimulate()) continue;
344 if (!it.value()->CheckFlag(f)) {
353 bool HValue::HasAtLeastOneUseWithFlagAndNoneWithout(Flag f) const {
354 bool return_value = false;
355 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
356 if (it.value()->IsSimulate()) continue;
357 if (!it.value()->CheckFlag(f)) return false;
364 HUseIterator::HUseIterator(HUseListNode* head) : next_(head) {
369 void HUseIterator::Advance() {
371 if (current_ != NULL) {
372 next_ = current_->tail();
373 value_ = current_->value();
374 index_ = current_->index();
379 int HValue::UseCount() const {
381 for (HUseIterator it(uses()); !it.Done(); it.Advance()) ++count;
386 HUseListNode* HValue::RemoveUse(HValue* value, int index) {
387 HUseListNode* previous = NULL;
388 HUseListNode* current = use_list_;
389 while (current != NULL) {
390 if (current->value() == value && current->index() == index) {
391 if (previous == NULL) {
392 use_list_ = current->tail();
394 previous->set_tail(current->tail());
400 current = current->tail();
404 // Do not reuse use list nodes in debug mode, zap them.
405 if (current != NULL) {
408 HUseListNode(current->value(), current->index(), NULL);
417 bool HValue::Equals(HValue* other) {
418 if (other->opcode() != opcode()) return false;
419 if (!other->representation().Equals(representation())) return false;
420 if (!other->type_.Equals(type_)) return false;
421 if (other->flags() != flags()) return false;
422 if (OperandCount() != other->OperandCount()) return false;
423 for (int i = 0; i < OperandCount(); ++i) {
424 if (OperandAt(i)->id() != other->OperandAt(i)->id()) return false;
426 bool result = DataEquals(other);
427 DCHECK(!result || Hashcode() == other->Hashcode());
432 intptr_t HValue::Hashcode() {
433 intptr_t result = opcode();
434 int count = OperandCount();
435 for (int i = 0; i < count; ++i) {
436 result = result * 19 + OperandAt(i)->id() + (result >> 7);
442 const char* HValue::Mnemonic() const {
444 #define MAKE_CASE(type) case k##type: return #type;
445 HYDROGEN_CONCRETE_INSTRUCTION_LIST(MAKE_CASE)
447 case kPhi: return "Phi";
453 bool HValue::CanReplaceWithDummyUses() {
454 return FLAG_unreachable_code_elimination &&
455 !(block()->IsReachable() ||
457 IsControlInstruction() ||
458 IsArgumentsObject() ||
459 IsCapturedObject() ||
466 bool HValue::IsInteger32Constant() {
467 return IsConstant() && HConstant::cast(this)->HasInteger32Value();
471 int32_t HValue::GetInteger32Constant() {
472 return HConstant::cast(this)->Integer32Value();
476 bool HValue::EqualsInteger32Constant(int32_t value) {
477 return IsInteger32Constant() && GetInteger32Constant() == value;
481 void HValue::SetOperandAt(int index, HValue* value) {
482 RegisterUse(index, value);
483 InternalSetOperandAt(index, value);
487 void HValue::DeleteAndReplaceWith(HValue* other) {
488 // We replace all uses first, so Delete can assert that there are none.
489 if (other != NULL) ReplaceAllUsesWith(other);
495 void HValue::ReplaceAllUsesWith(HValue* other) {
496 while (use_list_ != NULL) {
497 HUseListNode* list_node = use_list_;
498 HValue* value = list_node->value();
499 DCHECK(!value->block()->IsStartBlock());
500 value->InternalSetOperandAt(list_node->index(), other);
501 use_list_ = list_node->tail();
502 list_node->set_tail(other->use_list_);
503 other->use_list_ = list_node;
508 void HValue::Kill() {
509 // Instead of going through the entire use list of each operand, we only
510 // check the first item in each use list and rely on the tail() method to
511 // skip dead items, removing them lazily next time we traverse the list.
513 for (int i = 0; i < OperandCount(); ++i) {
514 HValue* operand = OperandAt(i);
515 if (operand == NULL) continue;
516 HUseListNode* first = operand->use_list_;
517 if (first != NULL && first->value()->CheckFlag(kIsDead)) {
518 operand->use_list_ = first->tail();
524 void HValue::SetBlock(HBasicBlock* block) {
525 DCHECK(block_ == NULL || block == NULL);
527 if (id_ == kNoNumber && block != NULL) {
528 id_ = block->graph()->GetNextValueID(this);
533 std::ostream& operator<<(std::ostream& os, const HValue& v) {
534 return v.PrintTo(os);
538 std::ostream& operator<<(std::ostream& os, const TypeOf& t) {
539 if (t.value->representation().IsTagged() &&
540 !t.value->type().Equals(HType::Tagged()))
542 return os << " type:" << t.value->type();
546 std::ostream& operator<<(std::ostream& os, const ChangesOf& c) {
547 GVNFlagSet changes_flags = c.value->ChangesFlags();
548 if (changes_flags.IsEmpty()) return os;
550 if (changes_flags == c.value->AllSideEffectsFlagSet()) {
553 bool add_comma = false;
554 #define PRINT_DO(Type) \
555 if (changes_flags.Contains(k##Type)) { \
556 if (add_comma) os << ","; \
560 GVN_TRACKED_FLAG_LIST(PRINT_DO);
561 GVN_UNTRACKED_FLAG_LIST(PRINT_DO);
568 bool HValue::HasMonomorphicJSObjectType() {
569 return !GetMonomorphicJSObjectMap().is_null();
573 bool HValue::UpdateInferredType() {
574 HType type = CalculateInferredType();
575 bool result = (!type.Equals(type_));
581 void HValue::RegisterUse(int index, HValue* new_value) {
582 HValue* old_value = OperandAt(index);
583 if (old_value == new_value) return;
585 HUseListNode* removed = NULL;
586 if (old_value != NULL) {
587 removed = old_value->RemoveUse(this, index);
590 if (new_value != NULL) {
591 if (removed == NULL) {
592 new_value->use_list_ = new(new_value->block()->zone()) HUseListNode(
593 this, index, new_value->use_list_);
595 removed->set_tail(new_value->use_list_);
596 new_value->use_list_ = removed;
602 void HValue::AddNewRange(Range* r, Zone* zone) {
603 if (!HasRange()) ComputeInitialRange(zone);
604 if (!HasRange()) range_ = new(zone) Range();
606 r->StackUpon(range_);
611 void HValue::RemoveLastAddedRange() {
613 DCHECK(range_->next() != NULL);
614 range_ = range_->next();
618 void HValue::ComputeInitialRange(Zone* zone) {
620 range_ = InferRange(zone);
625 std::ostream& HInstruction::PrintTo(std::ostream& os) const { // NOLINT
626 os << Mnemonic() << " ";
627 PrintDataTo(os) << ChangesOf(this) << TypeOf(this);
628 if (CheckFlag(HValue::kHasNoObservableSideEffects)) os << " [noOSE]";
629 if (CheckFlag(HValue::kIsDead)) os << " [dead]";
634 std::ostream& HInstruction::PrintDataTo(std::ostream& os) const { // NOLINT
635 for (int i = 0; i < OperandCount(); ++i) {
636 if (i > 0) os << " ";
637 os << NameOf(OperandAt(i));
643 void HInstruction::Unlink() {
645 DCHECK(!IsControlInstruction()); // Must never move control instructions.
646 DCHECK(!IsBlockEntry()); // Doesn't make sense to delete these.
647 DCHECK(previous_ != NULL);
648 previous_->next_ = next_;
650 DCHECK(block()->last() == this);
651 block()->set_last(previous_);
653 next_->previous_ = previous_;
659 void HInstruction::InsertBefore(HInstruction* next) {
661 DCHECK(!next->IsBlockEntry());
662 DCHECK(!IsControlInstruction());
663 DCHECK(!next->block()->IsStartBlock());
664 DCHECK(next->previous_ != NULL);
665 HInstruction* prev = next->previous();
667 next->previous_ = this;
670 SetBlock(next->block());
671 if (!has_position() && next->has_position()) {
672 set_position(next->position());
677 void HInstruction::InsertAfter(HInstruction* previous) {
679 DCHECK(!previous->IsControlInstruction());
680 DCHECK(!IsControlInstruction() || previous->next_ == NULL);
681 HBasicBlock* block = previous->block();
682 // Never insert anything except constants into the start block after finishing
684 if (block->IsStartBlock() && block->IsFinished() && !IsConstant()) {
685 DCHECK(block->end()->SecondSuccessor() == NULL);
686 InsertAfter(block->end()->FirstSuccessor()->first());
690 // If we're inserting after an instruction with side-effects that is
691 // followed by a simulate instruction, we need to insert after the
692 // simulate instruction instead.
693 HInstruction* next = previous->next_;
694 if (previous->HasObservableSideEffects() && next != NULL) {
695 DCHECK(next->IsSimulate());
697 next = previous->next_;
700 previous_ = previous;
703 previous->next_ = this;
704 if (next != NULL) next->previous_ = this;
705 if (block->last() == previous) {
706 block->set_last(this);
708 if (!has_position() && previous->has_position()) {
709 set_position(previous->position());
714 bool HInstruction::Dominates(HInstruction* other) {
715 if (block() != other->block()) {
716 return block()->Dominates(other->block());
718 // Both instructions are in the same basic block. This instruction
719 // should precede the other one in order to dominate it.
720 for (HInstruction* instr = next(); instr != NULL; instr = instr->next()) {
721 if (instr == other) {
730 void HInstruction::Verify() {
731 // Verify that input operands are defined before use.
732 HBasicBlock* cur_block = block();
733 for (int i = 0; i < OperandCount(); ++i) {
734 HValue* other_operand = OperandAt(i);
735 if (other_operand == NULL) continue;
736 HBasicBlock* other_block = other_operand->block();
737 if (cur_block == other_block) {
738 if (!other_operand->IsPhi()) {
739 HInstruction* cur = this->previous();
740 while (cur != NULL) {
741 if (cur == other_operand) break;
742 cur = cur->previous();
744 // Must reach other operand in the same block!
745 DCHECK(cur == other_operand);
748 // If the following assert fires, you may have forgotten an
750 DCHECK(other_block->Dominates(cur_block));
754 // Verify that instructions that may have side-effects are followed
755 // by a simulate instruction.
756 if (HasObservableSideEffects() && !IsOsrEntry()) {
757 DCHECK(next()->IsSimulate());
760 // Verify that instructions that can be eliminated by GVN have overridden
761 // HValue::DataEquals. The default implementation is UNREACHABLE. We
762 // don't actually care whether DataEquals returns true or false here.
763 if (CheckFlag(kUseGVN)) DataEquals(this);
765 // Verify that all uses are in the graph.
766 for (HUseIterator use = uses(); !use.Done(); use.Advance()) {
767 if (use.value()->IsInstruction()) {
768 DCHECK(HInstruction::cast(use.value())->IsLinked());
775 bool HInstruction::CanDeoptimize() {
776 // TODO(titzer): make this a virtual method?
778 case HValue::kAbnormalExit:
779 case HValue::kAccessArgumentsAt:
780 case HValue::kAllocate:
781 case HValue::kArgumentsElements:
782 case HValue::kArgumentsLength:
783 case HValue::kArgumentsObject:
784 case HValue::kBlockEntry:
785 case HValue::kBoundsCheckBaseIndexInformation:
786 case HValue::kCallFunction:
787 case HValue::kCallNew:
788 case HValue::kCallNewArray:
789 case HValue::kCallStub:
790 case HValue::kCapturedObject:
791 case HValue::kClassOfTestAndBranch:
792 case HValue::kCompareGeneric:
793 case HValue::kCompareHoleAndBranch:
794 case HValue::kCompareMap:
795 case HValue::kCompareMinusZeroAndBranch:
796 case HValue::kCompareNumericAndBranch:
797 case HValue::kCompareObjectEqAndBranch:
798 case HValue::kConstant:
799 case HValue::kConstructDouble:
800 case HValue::kContext:
801 case HValue::kDebugBreak:
802 case HValue::kDeclareGlobals:
803 case HValue::kDoubleBits:
804 case HValue::kDummyUse:
805 case HValue::kEnterInlined:
806 case HValue::kEnvironmentMarker:
807 case HValue::kForceRepresentation:
808 case HValue::kGetCachedArrayIndex:
810 case HValue::kHasCachedArrayIndexAndBranch:
811 case HValue::kHasInstanceTypeAndBranch:
812 case HValue::kInnerAllocatedObject:
813 case HValue::kInstanceOf:
814 case HValue::kInstanceOfKnownGlobal:
815 case HValue::kIsConstructCallAndBranch:
816 case HValue::kIsObjectAndBranch:
817 case HValue::kIsSmiAndBranch:
818 case HValue::kIsStringAndBranch:
819 case HValue::kIsUndetectableAndBranch:
820 case HValue::kLeaveInlined:
821 case HValue::kLoadFieldByIndex:
822 case HValue::kLoadGlobalGeneric:
823 case HValue::kLoadNamedField:
824 case HValue::kLoadNamedGeneric:
825 case HValue::kLoadRoot:
826 case HValue::kMapEnumLength:
827 case HValue::kMathMinMax:
828 case HValue::kParameter:
830 case HValue::kPushArguments:
831 case HValue::kRegExpLiteral:
832 case HValue::kReturn:
833 case HValue::kSeqStringGetChar:
834 case HValue::kStoreCodeEntry:
835 case HValue::kStoreFrameContext:
836 case HValue::kStoreKeyed:
837 case HValue::kStoreNamedField:
838 case HValue::kStoreNamedGeneric:
839 case HValue::kStringCharCodeAt:
840 case HValue::kStringCharFromCode:
841 case HValue::kThisFunction:
842 case HValue::kTypeofIsAndBranch:
843 case HValue::kUnknownOSRValue:
844 case HValue::kUseConst:
848 case HValue::kAllocateBlockContext:
849 case HValue::kApplyArguments:
850 case HValue::kBitwise:
851 case HValue::kBoundsCheck:
852 case HValue::kBranch:
853 case HValue::kCallJSFunction:
854 case HValue::kCallRuntime:
855 case HValue::kCallWithDescriptor:
856 case HValue::kChange:
857 case HValue::kCheckArrayBufferNotNeutered:
858 case HValue::kCheckHeapObject:
859 case HValue::kCheckInstanceType:
860 case HValue::kCheckMapValue:
861 case HValue::kCheckMaps:
862 case HValue::kCheckSmi:
863 case HValue::kCheckValue:
864 case HValue::kClampToUint8:
865 case HValue::kDateField:
866 case HValue::kDeoptimize:
868 case HValue::kForInCacheArray:
869 case HValue::kForInPrepareMap:
870 case HValue::kFunctionLiteral:
871 case HValue::kInvokeFunction:
872 case HValue::kLoadContextSlot:
873 case HValue::kLoadFunctionPrototype:
874 case HValue::kLoadKeyed:
875 case HValue::kLoadKeyedGeneric:
876 case HValue::kMathFloorOfDiv:
877 case HValue::kMaybeGrowElements:
880 case HValue::kOsrEntry:
884 case HValue::kSeqStringSetChar:
887 case HValue::kSimulate:
888 case HValue::kStackCheck:
889 case HValue::kStoreContextSlot:
890 case HValue::kStoreKeyedGeneric:
891 case HValue::kStringAdd:
892 case HValue::kStringCompareAndBranch:
894 case HValue::kToFastProperties:
895 case HValue::kTransitionElementsKind:
896 case HValue::kTrapAllocationMemento:
897 case HValue::kTypeof:
898 case HValue::kUnaryMathOperation:
899 case HValue::kWrapReceiver:
907 std::ostream& operator<<(std::ostream& os, const NameOf& v) {
908 return os << v.value->representation().Mnemonic() << v.value->id();
911 std::ostream& HDummyUse::PrintDataTo(std::ostream& os) const { // NOLINT
912 return os << NameOf(value());
916 std::ostream& HEnvironmentMarker::PrintDataTo(
917 std::ostream& os) const { // NOLINT
918 return os << (kind() == BIND ? "bind" : "lookup") << " var[" << index()
923 std::ostream& HUnaryCall::PrintDataTo(std::ostream& os) const { // NOLINT
924 return os << NameOf(value()) << " #" << argument_count();
928 std::ostream& HCallJSFunction::PrintDataTo(std::ostream& os) const { // NOLINT
929 return os << NameOf(function()) << " #" << argument_count();
933 HCallJSFunction* HCallJSFunction::New(Isolate* isolate, Zone* zone,
934 HValue* context, HValue* function,
936 bool pass_argument_count) {
937 bool has_stack_check = false;
938 if (function->IsConstant()) {
939 HConstant* fun_const = HConstant::cast(function);
940 Handle<JSFunction> jsfun =
941 Handle<JSFunction>::cast(fun_const->handle(isolate));
942 has_stack_check = !jsfun.is_null() &&
943 (jsfun->code()->kind() == Code::FUNCTION ||
944 jsfun->code()->kind() == Code::OPTIMIZED_FUNCTION);
947 return new(zone) HCallJSFunction(
948 function, argument_count, pass_argument_count,
953 std::ostream& HBinaryCall::PrintDataTo(std::ostream& os) const { // NOLINT
954 return os << NameOf(first()) << " " << NameOf(second()) << " #"
959 std::ostream& HCallFunction::PrintDataTo(std::ostream& os) const { // NOLINT
960 os << NameOf(context()) << " " << NameOf(function());
961 if (HasVectorAndSlot()) {
962 os << " (type-feedback-vector icslot " << slot().ToInt() << ")";
968 void HBoundsCheck::ApplyIndexChange() {
969 if (skip_check()) return;
971 DecompositionResult decomposition;
972 bool index_is_decomposable = index()->TryDecompose(&decomposition);
973 if (index_is_decomposable) {
974 DCHECK(decomposition.base() == base());
975 if (decomposition.offset() == offset() &&
976 decomposition.scale() == scale()) return;
981 ReplaceAllUsesWith(index());
983 HValue* current_index = decomposition.base();
984 int actual_offset = decomposition.offset() + offset();
985 int actual_scale = decomposition.scale() + scale();
987 HGraph* graph = block()->graph();
988 Isolate* isolate = graph->isolate();
989 Zone* zone = graph->zone();
990 HValue* context = graph->GetInvalidContext();
991 if (actual_offset != 0) {
992 HConstant* add_offset =
993 HConstant::New(isolate, zone, context, actual_offset);
994 add_offset->InsertBefore(this);
996 HAdd::New(isolate, zone, context, current_index, add_offset);
997 add->InsertBefore(this);
998 add->AssumeRepresentation(index()->representation());
999 add->ClearFlag(kCanOverflow);
1000 current_index = add;
1003 if (actual_scale != 0) {
1004 HConstant* sar_scale = HConstant::New(isolate, zone, context, actual_scale);
1005 sar_scale->InsertBefore(this);
1007 HSar::New(isolate, zone, context, current_index, sar_scale);
1008 sar->InsertBefore(this);
1009 sar->AssumeRepresentation(index()->representation());
1010 current_index = sar;
1013 SetOperandAt(0, current_index);
1021 std::ostream& HBoundsCheck::PrintDataTo(std::ostream& os) const { // NOLINT
1022 os << NameOf(index()) << " " << NameOf(length());
1023 if (base() != NULL && (offset() != 0 || scale() != 0)) {
1025 if (base() != index()) {
1026 os << NameOf(index());
1030 os << " + " << offset() << ") >> " << scale() << ")";
1032 if (skip_check()) os << " [DISABLED]";
1037 void HBoundsCheck::InferRepresentation(HInferRepresentationPhase* h_infer) {
1038 DCHECK(CheckFlag(kFlexibleRepresentation));
1039 HValue* actual_index = index()->ActualValue();
1040 HValue* actual_length = length()->ActualValue();
1041 Representation index_rep = actual_index->representation();
1042 Representation length_rep = actual_length->representation();
1043 if (index_rep.IsTagged() && actual_index->type().IsSmi()) {
1044 index_rep = Representation::Smi();
1046 if (length_rep.IsTagged() && actual_length->type().IsSmi()) {
1047 length_rep = Representation::Smi();
1049 Representation r = index_rep.generalize(length_rep);
1050 if (r.is_more_general_than(Representation::Integer32())) {
1051 r = Representation::Integer32();
1053 UpdateRepresentation(r, h_infer, "boundscheck");
1057 Range* HBoundsCheck::InferRange(Zone* zone) {
1058 Representation r = representation();
1059 if (r.IsSmiOrInteger32() && length()->HasRange()) {
1060 int upper = length()->range()->upper() - (allow_equality() ? 0 : 1);
1063 Range* result = new(zone) Range(lower, upper);
1064 if (index()->HasRange()) {
1065 result->Intersect(index()->range());
1068 // In case of Smi representation, clamp result to Smi::kMaxValue.
1069 if (r.IsSmi()) result->ClampToSmi();
1072 return HValue::InferRange(zone);
1076 std::ostream& HBoundsCheckBaseIndexInformation::PrintDataTo(
1077 std::ostream& os) const { // NOLINT
1078 // TODO(svenpanne) This 2nd base_index() looks wrong...
1079 return os << "base: " << NameOf(base_index())
1080 << ", check: " << NameOf(base_index());
1084 std::ostream& HCallWithDescriptor::PrintDataTo(
1085 std::ostream& os) const { // NOLINT
1086 for (int i = 0; i < OperandCount(); i++) {
1087 os << NameOf(OperandAt(i)) << " ";
1089 return os << "#" << argument_count();
1093 std::ostream& HCallNewArray::PrintDataTo(std::ostream& os) const { // NOLINT
1094 os << ElementsKindToString(elements_kind()) << " ";
1095 return HBinaryCall::PrintDataTo(os);
1099 std::ostream& HCallRuntime::PrintDataTo(std::ostream& os) const { // NOLINT
1100 os << name()->ToCString().get() << " ";
1101 if (save_doubles() == kSaveFPRegs) os << "[save doubles] ";
1102 return os << "#" << argument_count();
1106 std::ostream& HClassOfTestAndBranch::PrintDataTo(
1107 std::ostream& os) const { // NOLINT
1108 return os << "class_of_test(" << NameOf(value()) << ", \""
1109 << class_name()->ToCString().get() << "\")";
1113 std::ostream& HWrapReceiver::PrintDataTo(std::ostream& os) const { // NOLINT
1114 return os << NameOf(receiver()) << " " << NameOf(function());
1118 std::ostream& HAccessArgumentsAt::PrintDataTo(
1119 std::ostream& os) const { // NOLINT
1120 return os << NameOf(arguments()) << "[" << NameOf(index()) << "], length "
1121 << NameOf(length());
1125 std::ostream& HAllocateBlockContext::PrintDataTo(
1126 std::ostream& os) const { // NOLINT
1127 return os << NameOf(context()) << " " << NameOf(function());
1131 std::ostream& HControlInstruction::PrintDataTo(
1132 std::ostream& os) const { // NOLINT
1134 bool first_block = true;
1135 for (HSuccessorIterator it(this); !it.Done(); it.Advance()) {
1136 if (!first_block) os << ", ";
1137 os << *it.Current();
1138 first_block = false;
1144 std::ostream& HUnaryControlInstruction::PrintDataTo(
1145 std::ostream& os) const { // NOLINT
1146 os << NameOf(value());
1147 return HControlInstruction::PrintDataTo(os);
1151 std::ostream& HReturn::PrintDataTo(std::ostream& os) const { // NOLINT
1152 return os << NameOf(value()) << " (pop " << NameOf(parameter_count())
1157 Representation HBranch::observed_input_representation(int index) {
1158 if (expected_input_types_.Contains(ToBooleanStub::NULL_TYPE) ||
1159 expected_input_types_.Contains(ToBooleanStub::SPEC_OBJECT) ||
1160 expected_input_types_.Contains(ToBooleanStub::STRING) ||
1161 expected_input_types_.Contains(ToBooleanStub::SYMBOL) ||
1162 expected_input_types_.Contains(ToBooleanStub::SIMD_VALUE)) {
1163 return Representation::Tagged();
1165 if (expected_input_types_.Contains(ToBooleanStub::UNDEFINED)) {
1166 if (expected_input_types_.Contains(ToBooleanStub::HEAP_NUMBER)) {
1167 return Representation::Double();
1169 return Representation::Tagged();
1171 if (expected_input_types_.Contains(ToBooleanStub::HEAP_NUMBER)) {
1172 return Representation::Double();
1174 if (expected_input_types_.Contains(ToBooleanStub::SMI)) {
1175 return Representation::Smi();
1177 return Representation::None();
1181 bool HBranch::KnownSuccessorBlock(HBasicBlock** block) {
1182 HValue* value = this->value();
1183 if (value->EmitAtUses()) {
1184 DCHECK(value->IsConstant());
1185 DCHECK(!value->representation().IsDouble());
1186 *block = HConstant::cast(value)->BooleanValue()
1188 : SecondSuccessor();
1196 std::ostream& HBranch::PrintDataTo(std::ostream& os) const { // NOLINT
1197 return HUnaryControlInstruction::PrintDataTo(os) << " "
1198 << expected_input_types();
1202 std::ostream& HCompareMap::PrintDataTo(std::ostream& os) const { // NOLINT
1203 os << NameOf(value()) << " (" << *map().handle() << ")";
1204 HControlInstruction::PrintDataTo(os);
1205 if (known_successor_index() == 0) {
1207 } else if (known_successor_index() == 1) {
1214 const char* HUnaryMathOperation::OpName() const {
1241 Range* HUnaryMathOperation::InferRange(Zone* zone) {
1242 Representation r = representation();
1243 if (op() == kMathClz32) return new(zone) Range(0, 32);
1244 if (r.IsSmiOrInteger32() && value()->HasRange()) {
1245 if (op() == kMathAbs) {
1246 int upper = value()->range()->upper();
1247 int lower = value()->range()->lower();
1248 bool spans_zero = value()->range()->CanBeZero();
1249 // Math.abs(kMinInt) overflows its representation, on which the
1250 // instruction deopts. Hence clamp it to kMaxInt.
1251 int abs_upper = upper == kMinInt ? kMaxInt : abs(upper);
1252 int abs_lower = lower == kMinInt ? kMaxInt : abs(lower);
1254 new(zone) Range(spans_zero ? 0 : Min(abs_lower, abs_upper),
1255 Max(abs_lower, abs_upper));
1256 // In case of Smi representation, clamp Math.abs(Smi::kMinValue) to
1258 if (r.IsSmi()) result->ClampToSmi();
1262 return HValue::InferRange(zone);
1266 std::ostream& HUnaryMathOperation::PrintDataTo(
1267 std::ostream& os) const { // NOLINT
1268 return os << OpName() << " " << NameOf(value());
1272 std::ostream& HUnaryOperation::PrintDataTo(std::ostream& os) const { // NOLINT
1273 return os << NameOf(value());
1277 std::ostream& HHasInstanceTypeAndBranch::PrintDataTo(
1278 std::ostream& os) const { // NOLINT
1279 os << NameOf(value());
1281 case FIRST_JS_RECEIVER_TYPE:
1282 if (to_ == LAST_TYPE) os << " spec_object";
1284 case JS_REGEXP_TYPE:
1285 if (to_ == JS_REGEXP_TYPE) os << " reg_exp";
1288 if (to_ == JS_ARRAY_TYPE) os << " array";
1290 case JS_FUNCTION_TYPE:
1291 if (to_ == JS_FUNCTION_TYPE) os << " function";
1300 std::ostream& HTypeofIsAndBranch::PrintDataTo(
1301 std::ostream& os) const { // NOLINT
1302 os << NameOf(value()) << " == " << type_literal()->ToCString().get();
1303 return HControlInstruction::PrintDataTo(os);
1307 static String* TypeOfString(HConstant* constant, Isolate* isolate) {
1308 Heap* heap = isolate->heap();
1309 if (constant->HasNumberValue()) return heap->number_string();
1310 if (constant->IsUndetectable()) return heap->undefined_string();
1311 if (constant->HasStringValue()) return heap->string_string();
1312 switch (constant->GetInstanceType()) {
1313 case ODDBALL_TYPE: {
1314 Unique<Object> unique = constant->GetUnique();
1315 if (unique.IsKnownGlobal(heap->true_value()) ||
1316 unique.IsKnownGlobal(heap->false_value())) {
1317 return heap->boolean_string();
1319 if (unique.IsKnownGlobal(heap->null_value())) {
1320 return heap->object_string();
1322 DCHECK(unique.IsKnownGlobal(heap->undefined_value()));
1323 return heap->undefined_string();
1326 return heap->symbol_string();
1327 case FLOAT32X4_TYPE:
1328 return heap->float32x4_string();
1329 case JS_FUNCTION_TYPE:
1330 case JS_FUNCTION_PROXY_TYPE:
1331 return heap->function_string();
1333 return heap->object_string();
1338 bool HTypeofIsAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
1339 if (FLAG_fold_constants && value()->IsConstant()) {
1340 HConstant* constant = HConstant::cast(value());
1341 String* type_string = TypeOfString(constant, isolate());
1342 bool same_type = type_literal_.IsKnownGlobal(type_string);
1343 *block = same_type ? FirstSuccessor() : SecondSuccessor();
1345 } else if (value()->representation().IsSpecialization()) {
1347 type_literal_.IsKnownGlobal(isolate()->heap()->number_string());
1348 *block = number_type ? FirstSuccessor() : SecondSuccessor();
1356 std::ostream& HCheckMapValue::PrintDataTo(std::ostream& os) const { // NOLINT
1357 return os << NameOf(value()) << " " << NameOf(map());
1361 HValue* HCheckMapValue::Canonicalize() {
1362 if (map()->IsConstant()) {
1363 HConstant* c_map = HConstant::cast(map());
1364 return HCheckMaps::CreateAndInsertAfter(
1365 block()->graph()->zone(), value(), c_map->MapValue(),
1366 c_map->HasStableMapValue(), this);
1372 std::ostream& HForInPrepareMap::PrintDataTo(std::ostream& os) const { // NOLINT
1373 return os << NameOf(enumerable());
1377 std::ostream& HForInCacheArray::PrintDataTo(std::ostream& os) const { // NOLINT
1378 return os << NameOf(enumerable()) << " " << NameOf(map()) << "[" << idx_
1383 std::ostream& HLoadFieldByIndex::PrintDataTo(
1384 std::ostream& os) const { // NOLINT
1385 return os << NameOf(object()) << " " << NameOf(index());
1389 static bool MatchLeftIsOnes(HValue* l, HValue* r, HValue** negated) {
1390 if (!l->EqualsInteger32Constant(~0)) return false;
1396 static bool MatchNegationViaXor(HValue* instr, HValue** negated) {
1397 if (!instr->IsBitwise()) return false;
1398 HBitwise* b = HBitwise::cast(instr);
1399 return (b->op() == Token::BIT_XOR) &&
1400 (MatchLeftIsOnes(b->left(), b->right(), negated) ||
1401 MatchLeftIsOnes(b->right(), b->left(), negated));
1405 static bool MatchDoubleNegation(HValue* instr, HValue** arg) {
1407 return MatchNegationViaXor(instr, &negated) &&
1408 MatchNegationViaXor(negated, arg);
1412 HValue* HBitwise::Canonicalize() {
1413 if (!representation().IsSmiOrInteger32()) return this;
1414 // If x is an int32, then x & -1 == x, x | 0 == x and x ^ 0 == x.
1415 int32_t nop_constant = (op() == Token::BIT_AND) ? -1 : 0;
1416 if (left()->EqualsInteger32Constant(nop_constant) &&
1417 !right()->CheckFlag(kUint32)) {
1420 if (right()->EqualsInteger32Constant(nop_constant) &&
1421 !left()->CheckFlag(kUint32)) {
1424 // Optimize double negation, a common pattern used for ToInt32(x).
1426 if (MatchDoubleNegation(this, &arg) && !arg->CheckFlag(kUint32)) {
1433 Representation HAdd::RepresentationFromInputs() {
1434 Representation left_rep = left()->representation();
1435 if (left_rep.IsExternal()) {
1436 return Representation::External();
1438 return HArithmeticBinaryOperation::RepresentationFromInputs();
1442 Representation HAdd::RequiredInputRepresentation(int index) {
1444 Representation left_rep = left()->representation();
1445 if (left_rep.IsExternal()) {
1446 return Representation::Integer32();
1449 return HArithmeticBinaryOperation::RequiredInputRepresentation(index);
1453 static bool IsIdentityOperation(HValue* arg1, HValue* arg2, int32_t identity) {
1454 return arg1->representation().IsSpecialization() &&
1455 arg2->EqualsInteger32Constant(identity);
1459 HValue* HAdd::Canonicalize() {
1460 // Adding 0 is an identity operation except in case of -0: -0 + 0 = +0
1461 if (IsIdentityOperation(left(), right(), 0) &&
1462 !left()->representation().IsDouble()) { // Left could be -0.
1465 if (IsIdentityOperation(right(), left(), 0) &&
1466 !left()->representation().IsDouble()) { // Right could be -0.
1473 HValue* HSub::Canonicalize() {
1474 if (IsIdentityOperation(left(), right(), 0)) return left();
1479 HValue* HMul::Canonicalize() {
1480 if (IsIdentityOperation(left(), right(), 1)) return left();
1481 if (IsIdentityOperation(right(), left(), 1)) return right();
1486 bool HMul::MulMinusOne() {
1487 if (left()->EqualsInteger32Constant(-1) ||
1488 right()->EqualsInteger32Constant(-1)) {
1496 HValue* HMod::Canonicalize() {
1501 HValue* HDiv::Canonicalize() {
1502 if (IsIdentityOperation(left(), right(), 1)) return left();
1507 HValue* HChange::Canonicalize() {
1508 return (from().Equals(to())) ? value() : this;
1512 HValue* HWrapReceiver::Canonicalize() {
1513 if (HasNoUses()) return NULL;
1514 if (receiver()->type().IsJSObject()) {
1521 std::ostream& HTypeof::PrintDataTo(std::ostream& os) const { // NOLINT
1522 return os << NameOf(value());
1526 HInstruction* HForceRepresentation::New(Isolate* isolate, Zone* zone,
1527 HValue* context, HValue* value,
1528 Representation representation) {
1529 if (FLAG_fold_constants && value->IsConstant()) {
1530 HConstant* c = HConstant::cast(value);
1531 c = c->CopyToRepresentation(representation, zone);
1532 if (c != NULL) return c;
1534 return new(zone) HForceRepresentation(value, representation);
1538 std::ostream& HForceRepresentation::PrintDataTo(
1539 std::ostream& os) const { // NOLINT
1540 return os << representation().Mnemonic() << " " << NameOf(value());
1544 std::ostream& HChange::PrintDataTo(std::ostream& os) const { // NOLINT
1545 HUnaryOperation::PrintDataTo(os);
1546 os << " " << from().Mnemonic() << " to " << to().Mnemonic();
1548 if (CanTruncateToSmi()) os << " truncating-smi";
1549 if (CanTruncateToInt32()) os << " truncating-int32";
1550 if (CheckFlag(kBailoutOnMinusZero)) os << " -0?";
1551 if (CheckFlag(kAllowUndefinedAsNaN)) os << " allow-undefined-as-nan";
1556 HValue* HUnaryMathOperation::Canonicalize() {
1557 if (op() == kMathRound || op() == kMathFloor) {
1558 HValue* val = value();
1559 if (val->IsChange()) val = HChange::cast(val)->value();
1560 if (val->representation().IsSmiOrInteger32()) {
1561 if (val->representation().Equals(representation())) return val;
1562 return Prepend(new(block()->zone()) HChange(
1563 val, representation(), false, false));
1566 if (op() == kMathFloor && value()->IsDiv() && value()->HasOneUse()) {
1567 HDiv* hdiv = HDiv::cast(value());
1569 HValue* left = hdiv->left();
1570 if (left->representation().IsInteger32()) {
1571 // A value with an integer representation does not need to be transformed.
1572 } else if (left->IsChange() && HChange::cast(left)->from().IsInteger32()) {
1573 // A change from an integer32 can be replaced by the integer32 value.
1574 left = HChange::cast(left)->value();
1575 } else if (hdiv->observed_input_representation(1).IsSmiOrInteger32()) {
1576 left = Prepend(new(block()->zone()) HChange(
1577 left, Representation::Integer32(), false, false));
1582 HValue* right = hdiv->right();
1583 if (right->IsInteger32Constant()) {
1584 right = Prepend(HConstant::cast(right)->CopyToRepresentation(
1585 Representation::Integer32(), right->block()->zone()));
1586 } else if (right->representation().IsInteger32()) {
1587 // A value with an integer representation does not need to be transformed.
1588 } else if (right->IsChange() &&
1589 HChange::cast(right)->from().IsInteger32()) {
1590 // A change from an integer32 can be replaced by the integer32 value.
1591 right = HChange::cast(right)->value();
1592 } else if (hdiv->observed_input_representation(2).IsSmiOrInteger32()) {
1593 right = Prepend(new(block()->zone()) HChange(
1594 right, Representation::Integer32(), false, false));
1599 return Prepend(HMathFloorOfDiv::New(
1600 block()->graph()->isolate(), block()->zone(), context(), left, right));
1606 HValue* HCheckInstanceType::Canonicalize() {
1607 if ((check_ == IS_SPEC_OBJECT && value()->type().IsJSObject()) ||
1608 (check_ == IS_JS_ARRAY && value()->type().IsJSArray()) ||
1609 (check_ == IS_STRING && value()->type().IsString())) {
1613 if (check_ == IS_INTERNALIZED_STRING && value()->IsConstant()) {
1614 if (HConstant::cast(value())->HasInternalizedStringValue()) {
1622 void HCheckInstanceType::GetCheckInterval(InstanceType* first,
1623 InstanceType* last) {
1624 DCHECK(is_interval_check());
1626 case IS_SPEC_OBJECT:
1627 *first = FIRST_SPEC_OBJECT_TYPE;
1628 *last = LAST_SPEC_OBJECT_TYPE;
1631 *first = *last = JS_ARRAY_TYPE;
1634 *first = *last = JS_DATE_TYPE;
1642 void HCheckInstanceType::GetCheckMaskAndTag(uint8_t* mask, uint8_t* tag) {
1643 DCHECK(!is_interval_check());
1646 *mask = kIsNotStringMask;
1649 case IS_INTERNALIZED_STRING:
1650 *mask = kIsNotStringMask | kIsNotInternalizedMask;
1651 *tag = kInternalizedTag;
1659 std::ostream& HCheckMaps::PrintDataTo(std::ostream& os) const { // NOLINT
1660 os << NameOf(value()) << " [" << *maps()->at(0).handle();
1661 for (int i = 1; i < maps()->size(); ++i) {
1662 os << "," << *maps()->at(i).handle();
1665 if (IsStabilityCheck()) os << "(stability-check)";
1670 HValue* HCheckMaps::Canonicalize() {
1671 if (!IsStabilityCheck() && maps_are_stable() && value()->IsConstant()) {
1672 HConstant* c_value = HConstant::cast(value());
1673 if (c_value->HasObjectMap()) {
1674 for (int i = 0; i < maps()->size(); ++i) {
1675 if (c_value->ObjectMap() == maps()->at(i)) {
1676 if (maps()->size() > 1) {
1677 set_maps(new(block()->graph()->zone()) UniqueSet<Map>(
1678 maps()->at(i), block()->graph()->zone()));
1680 MarkAsStabilityCheck();
1690 std::ostream& HCheckValue::PrintDataTo(std::ostream& os) const { // NOLINT
1691 return os << NameOf(value()) << " " << Brief(*object().handle());
1695 HValue* HCheckValue::Canonicalize() {
1696 return (value()->IsConstant() &&
1697 HConstant::cast(value())->EqualsUnique(object_)) ? NULL : this;
1701 const char* HCheckInstanceType::GetCheckName() const {
1703 case IS_SPEC_OBJECT: return "object";
1704 case IS_JS_ARRAY: return "array";
1707 case IS_STRING: return "string";
1708 case IS_INTERNALIZED_STRING: return "internalized_string";
1715 std::ostream& HCheckInstanceType::PrintDataTo(
1716 std::ostream& os) const { // NOLINT
1717 os << GetCheckName() << " ";
1718 return HUnaryOperation::PrintDataTo(os);
1722 std::ostream& HCallStub::PrintDataTo(std::ostream& os) const { // NOLINT
1723 os << CodeStub::MajorName(major_key_, false) << " ";
1724 return HUnaryCall::PrintDataTo(os);
1728 std::ostream& HUnknownOSRValue::PrintDataTo(std::ostream& os) const { // NOLINT
1729 const char* type = "expression";
1730 if (environment_->is_local_index(index_)) type = "local";
1731 if (environment_->is_special_index(index_)) type = "special";
1732 if (environment_->is_parameter_index(index_)) type = "parameter";
1733 return os << type << " @ " << index_;
1737 std::ostream& HInstanceOf::PrintDataTo(std::ostream& os) const { // NOLINT
1738 return os << NameOf(left()) << " " << NameOf(right()) << " "
1739 << NameOf(context());
1743 Range* HValue::InferRange(Zone* zone) {
1745 if (representation().IsSmi() || type().IsSmi()) {
1746 result = new(zone) Range(Smi::kMinValue, Smi::kMaxValue);
1747 result->set_can_be_minus_zero(false);
1749 result = new(zone) Range();
1750 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32));
1751 // TODO(jkummerow): The range cannot be minus zero when the upper type
1752 // bound is Integer32.
1758 Range* HChange::InferRange(Zone* zone) {
1759 Range* input_range = value()->range();
1760 if (from().IsInteger32() && !value()->CheckFlag(HInstruction::kUint32) &&
1763 input_range != NULL &&
1764 input_range->IsInSmiRange()))) {
1765 set_type(HType::Smi());
1766 ClearChangesFlag(kNewSpacePromotion);
1768 if (to().IsSmiOrTagged() &&
1769 input_range != NULL &&
1770 input_range->IsInSmiRange() &&
1771 (!SmiValuesAre32Bits() ||
1772 !value()->CheckFlag(HValue::kUint32) ||
1773 input_range->upper() != kMaxInt)) {
1774 // The Range class can't express upper bounds in the (kMaxInt, kMaxUint32]
1775 // interval, so we treat kMaxInt as a sentinel for this entire interval.
1776 ClearFlag(kCanOverflow);
1778 Range* result = (input_range != NULL)
1779 ? input_range->Copy(zone)
1780 : HValue::InferRange(zone);
1781 result->set_can_be_minus_zero(!to().IsSmiOrInteger32() ||
1782 !(CheckFlag(kAllUsesTruncatingToInt32) ||
1783 CheckFlag(kAllUsesTruncatingToSmi)));
1784 if (to().IsSmi()) result->ClampToSmi();
1789 Range* HConstant::InferRange(Zone* zone) {
1790 if (HasInteger32Value()) {
1791 Range* result = new(zone) Range(int32_value_, int32_value_);
1792 result->set_can_be_minus_zero(false);
1795 return HValue::InferRange(zone);
1799 SourcePosition HPhi::position() const { return block()->first()->position(); }
1802 Range* HPhi::InferRange(Zone* zone) {
1803 Representation r = representation();
1804 if (r.IsSmiOrInteger32()) {
1805 if (block()->IsLoopHeader()) {
1806 Range* range = r.IsSmi()
1807 ? new(zone) Range(Smi::kMinValue, Smi::kMaxValue)
1808 : new(zone) Range(kMinInt, kMaxInt);
1811 Range* range = OperandAt(0)->range()->Copy(zone);
1812 for (int i = 1; i < OperandCount(); ++i) {
1813 range->Union(OperandAt(i)->range());
1818 return HValue::InferRange(zone);
1823 Range* HAdd::InferRange(Zone* zone) {
1824 Representation r = representation();
1825 if (r.IsSmiOrInteger32()) {
1826 Range* a = left()->range();
1827 Range* b = right()->range();
1828 Range* res = a->Copy(zone);
1829 if (!res->AddAndCheckOverflow(r, b) ||
1830 (r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1831 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) {
1832 ClearFlag(kCanOverflow);
1834 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1835 !CheckFlag(kAllUsesTruncatingToInt32) &&
1836 a->CanBeMinusZero() && b->CanBeMinusZero());
1839 return HValue::InferRange(zone);
1844 Range* HSub::InferRange(Zone* zone) {
1845 Representation r = representation();
1846 if (r.IsSmiOrInteger32()) {
1847 Range* a = left()->range();
1848 Range* b = right()->range();
1849 Range* res = a->Copy(zone);
1850 if (!res->SubAndCheckOverflow(r, b) ||
1851 (r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1852 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) {
1853 ClearFlag(kCanOverflow);
1855 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1856 !CheckFlag(kAllUsesTruncatingToInt32) &&
1857 a->CanBeMinusZero() && b->CanBeZero());
1860 return HValue::InferRange(zone);
1865 Range* HMul::InferRange(Zone* zone) {
1866 Representation r = representation();
1867 if (r.IsSmiOrInteger32()) {
1868 Range* a = left()->range();
1869 Range* b = right()->range();
1870 Range* res = a->Copy(zone);
1871 if (!res->MulAndCheckOverflow(r, b) ||
1872 (((r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1873 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) &&
1875 // Truncated int multiplication is too precise and therefore not the
1876 // same as converting to Double and back.
1877 // Handle truncated integer multiplication by -1 special.
1878 ClearFlag(kCanOverflow);
1880 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1881 !CheckFlag(kAllUsesTruncatingToInt32) &&
1882 ((a->CanBeZero() && b->CanBeNegative()) ||
1883 (a->CanBeNegative() && b->CanBeZero())));
1886 return HValue::InferRange(zone);
1891 Range* HDiv::InferRange(Zone* zone) {
1892 if (representation().IsInteger32()) {
1893 Range* a = left()->range();
1894 Range* b = right()->range();
1895 Range* result = new(zone) Range();
1896 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1897 (a->CanBeMinusZero() ||
1898 (a->CanBeZero() && b->CanBeNegative())));
1899 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1900 ClearFlag(kCanOverflow);
1903 if (!b->CanBeZero()) {
1904 ClearFlag(kCanBeDivByZero);
1908 return HValue::InferRange(zone);
1913 Range* HMathFloorOfDiv::InferRange(Zone* zone) {
1914 if (representation().IsInteger32()) {
1915 Range* a = left()->range();
1916 Range* b = right()->range();
1917 Range* result = new(zone) Range();
1918 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1919 (a->CanBeMinusZero() ||
1920 (a->CanBeZero() && b->CanBeNegative())));
1921 if (!a->Includes(kMinInt)) {
1922 ClearFlag(kLeftCanBeMinInt);
1925 if (!a->CanBeNegative()) {
1926 ClearFlag(HValue::kLeftCanBeNegative);
1929 if (!a->CanBePositive()) {
1930 ClearFlag(HValue::kLeftCanBePositive);
1933 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1934 ClearFlag(kCanOverflow);
1937 if (!b->CanBeZero()) {
1938 ClearFlag(kCanBeDivByZero);
1942 return HValue::InferRange(zone);
1947 // Returns the absolute value of its argument minus one, avoiding undefined
1948 // behavior at kMinInt.
1949 static int32_t AbsMinus1(int32_t a) { return a < 0 ? -(a + 1) : (a - 1); }
1952 Range* HMod::InferRange(Zone* zone) {
1953 if (representation().IsInteger32()) {
1954 Range* a = left()->range();
1955 Range* b = right()->range();
1957 // The magnitude of the modulus is bounded by the right operand.
1958 int32_t positive_bound = Max(AbsMinus1(b->lower()), AbsMinus1(b->upper()));
1960 // The result of the modulo operation has the sign of its left operand.
1961 bool left_can_be_negative = a->CanBeMinusZero() || a->CanBeNegative();
1962 Range* result = new(zone) Range(left_can_be_negative ? -positive_bound : 0,
1963 a->CanBePositive() ? positive_bound : 0);
1965 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1966 left_can_be_negative);
1968 if (!a->CanBeNegative()) {
1969 ClearFlag(HValue::kLeftCanBeNegative);
1972 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1973 ClearFlag(HValue::kCanOverflow);
1976 if (!b->CanBeZero()) {
1977 ClearFlag(HValue::kCanBeDivByZero);
1981 return HValue::InferRange(zone);
1986 InductionVariableData* InductionVariableData::ExaminePhi(HPhi* phi) {
1987 if (phi->block()->loop_information() == NULL) return NULL;
1988 if (phi->OperandCount() != 2) return NULL;
1989 int32_t candidate_increment;
1991 candidate_increment = ComputeIncrement(phi, phi->OperandAt(0));
1992 if (candidate_increment != 0) {
1993 return new(phi->block()->graph()->zone())
1994 InductionVariableData(phi, phi->OperandAt(1), candidate_increment);
1997 candidate_increment = ComputeIncrement(phi, phi->OperandAt(1));
1998 if (candidate_increment != 0) {
1999 return new(phi->block()->graph()->zone())
2000 InductionVariableData(phi, phi->OperandAt(0), candidate_increment);
2008 * This function tries to match the following patterns (and all the relevant
2009 * variants related to |, & and + being commutative):
2010 * base | constant_or_mask
2011 * base & constant_and_mask
2012 * (base + constant_offset) & constant_and_mask
2013 * (base - constant_offset) & constant_and_mask
2015 void InductionVariableData::DecomposeBitwise(
2017 BitwiseDecompositionResult* result) {
2018 HValue* base = IgnoreOsrValue(value);
2019 result->base = value;
2021 if (!base->representation().IsInteger32()) return;
2023 if (base->IsBitwise()) {
2024 bool allow_offset = false;
2027 HBitwise* bitwise = HBitwise::cast(base);
2028 if (bitwise->right()->IsInteger32Constant()) {
2029 mask = bitwise->right()->GetInteger32Constant();
2030 base = bitwise->left();
2031 } else if (bitwise->left()->IsInteger32Constant()) {
2032 mask = bitwise->left()->GetInteger32Constant();
2033 base = bitwise->right();
2037 if (bitwise->op() == Token::BIT_AND) {
2038 result->and_mask = mask;
2039 allow_offset = true;
2040 } else if (bitwise->op() == Token::BIT_OR) {
2041 result->or_mask = mask;
2046 result->context = bitwise->context();
2049 if (base->IsAdd()) {
2050 HAdd* add = HAdd::cast(base);
2051 if (add->right()->IsInteger32Constant()) {
2053 } else if (add->left()->IsInteger32Constant()) {
2054 base = add->right();
2056 } else if (base->IsSub()) {
2057 HSub* sub = HSub::cast(base);
2058 if (sub->right()->IsInteger32Constant()) {
2064 result->base = base;
2069 void InductionVariableData::AddCheck(HBoundsCheck* check,
2070 int32_t upper_limit) {
2071 DCHECK(limit_validity() != NULL);
2072 if (limit_validity() != check->block() &&
2073 !limit_validity()->Dominates(check->block())) return;
2074 if (!phi()->block()->current_loop()->IsNestedInThisLoop(
2075 check->block()->current_loop())) return;
2077 ChecksRelatedToLength* length_checks = checks();
2078 while (length_checks != NULL) {
2079 if (length_checks->length() == check->length()) break;
2080 length_checks = length_checks->next();
2082 if (length_checks == NULL) {
2083 length_checks = new(check->block()->zone())
2084 ChecksRelatedToLength(check->length(), checks());
2085 checks_ = length_checks;
2088 length_checks->AddCheck(check, upper_limit);
2092 void InductionVariableData::ChecksRelatedToLength::CloseCurrentBlock() {
2093 if (checks() != NULL) {
2094 InductionVariableCheck* c = checks();
2095 HBasicBlock* current_block = c->check()->block();
2096 while (c != NULL && c->check()->block() == current_block) {
2097 c->set_upper_limit(current_upper_limit_);
2104 void InductionVariableData::ChecksRelatedToLength::UseNewIndexInCurrentBlock(
2109 DCHECK(first_check_in_block() != NULL);
2110 HValue* previous_index = first_check_in_block()->index();
2111 DCHECK(context != NULL);
2113 Zone* zone = index_base->block()->graph()->zone();
2114 Isolate* isolate = index_base->block()->graph()->isolate();
2115 set_added_constant(HConstant::New(isolate, zone, context, mask));
2116 if (added_index() != NULL) {
2117 added_constant()->InsertBefore(added_index());
2119 added_constant()->InsertBefore(first_check_in_block());
2122 if (added_index() == NULL) {
2123 first_check_in_block()->ReplaceAllUsesWith(first_check_in_block()->index());
2124 HInstruction* new_index = HBitwise::New(isolate, zone, context, token,
2125 index_base, added_constant());
2126 DCHECK(new_index->IsBitwise());
2127 new_index->ClearAllSideEffects();
2128 new_index->AssumeRepresentation(Representation::Integer32());
2129 set_added_index(HBitwise::cast(new_index));
2130 added_index()->InsertBefore(first_check_in_block());
2132 DCHECK(added_index()->op() == token);
2134 added_index()->SetOperandAt(1, index_base);
2135 added_index()->SetOperandAt(2, added_constant());
2136 first_check_in_block()->SetOperandAt(0, added_index());
2137 if (previous_index->HasNoUses()) {
2138 previous_index->DeleteAndReplaceWith(NULL);
2142 void InductionVariableData::ChecksRelatedToLength::AddCheck(
2143 HBoundsCheck* check,
2144 int32_t upper_limit) {
2145 BitwiseDecompositionResult decomposition;
2146 InductionVariableData::DecomposeBitwise(check->index(), &decomposition);
2148 if (first_check_in_block() == NULL ||
2149 first_check_in_block()->block() != check->block()) {
2150 CloseCurrentBlock();
2152 first_check_in_block_ = check;
2153 set_added_index(NULL);
2154 set_added_constant(NULL);
2155 current_and_mask_in_block_ = decomposition.and_mask;
2156 current_or_mask_in_block_ = decomposition.or_mask;
2157 current_upper_limit_ = upper_limit;
2159 InductionVariableCheck* new_check = new(check->block()->graph()->zone())
2160 InductionVariableCheck(check, checks_, upper_limit);
2161 checks_ = new_check;
2165 if (upper_limit > current_upper_limit()) {
2166 current_upper_limit_ = upper_limit;
2169 if (decomposition.and_mask != 0 &&
2170 current_or_mask_in_block() == 0) {
2171 if (current_and_mask_in_block() == 0 ||
2172 decomposition.and_mask > current_and_mask_in_block()) {
2173 UseNewIndexInCurrentBlock(Token::BIT_AND,
2174 decomposition.and_mask,
2176 decomposition.context);
2177 current_and_mask_in_block_ = decomposition.and_mask;
2179 check->set_skip_check();
2181 if (current_and_mask_in_block() == 0) {
2182 if (decomposition.or_mask > current_or_mask_in_block()) {
2183 UseNewIndexInCurrentBlock(Token::BIT_OR,
2184 decomposition.or_mask,
2186 decomposition.context);
2187 current_or_mask_in_block_ = decomposition.or_mask;
2189 check->set_skip_check();
2192 if (!check->skip_check()) {
2193 InductionVariableCheck* new_check = new(check->block()->graph()->zone())
2194 InductionVariableCheck(check, checks_, upper_limit);
2195 checks_ = new_check;
2201 * This method detects if phi is an induction variable, with phi_operand as
2202 * its "incremented" value (the other operand would be the "base" value).
2204 * It cheks is phi_operand has the form "phi + constant".
2205 * If yes, the constant is the increment that the induction variable gets at
2206 * every loop iteration.
2207 * Otherwise it returns 0.
2209 int32_t InductionVariableData::ComputeIncrement(HPhi* phi,
2210 HValue* phi_operand) {
2211 if (!phi_operand->representation().IsSmiOrInteger32()) return 0;
2213 if (phi_operand->IsAdd()) {
2214 HAdd* operation = HAdd::cast(phi_operand);
2215 if (operation->left() == phi &&
2216 operation->right()->IsInteger32Constant()) {
2217 return operation->right()->GetInteger32Constant();
2218 } else if (operation->right() == phi &&
2219 operation->left()->IsInteger32Constant()) {
2220 return operation->left()->GetInteger32Constant();
2222 } else if (phi_operand->IsSub()) {
2223 HSub* operation = HSub::cast(phi_operand);
2224 if (operation->left() == phi &&
2225 operation->right()->IsInteger32Constant()) {
2226 int constant = operation->right()->GetInteger32Constant();
2227 if (constant == kMinInt) return 0;
2237 * Swaps the information in "update" with the one contained in "this".
2238 * The swapping is important because this method is used while doing a
2239 * dominator tree traversal, and "update" will retain the old data that
2240 * will be restored while backtracking.
2242 void InductionVariableData::UpdateAdditionalLimit(
2243 InductionVariableLimitUpdate* update) {
2244 DCHECK(update->updated_variable == this);
2245 if (update->limit_is_upper) {
2246 swap(&additional_upper_limit_, &update->limit);
2247 swap(&additional_upper_limit_is_included_, &update->limit_is_included);
2249 swap(&additional_lower_limit_, &update->limit);
2250 swap(&additional_lower_limit_is_included_, &update->limit_is_included);
2255 int32_t InductionVariableData::ComputeUpperLimit(int32_t and_mask,
2257 // Should be Smi::kMaxValue but it must fit 32 bits; lower is safe anyway.
2258 const int32_t MAX_LIMIT = 1 << 30;
2260 int32_t result = MAX_LIMIT;
2262 if (limit() != NULL &&
2263 limit()->IsInteger32Constant()) {
2264 int32_t limit_value = limit()->GetInteger32Constant();
2265 if (!limit_included()) {
2268 if (limit_value < result) result = limit_value;
2271 if (additional_upper_limit() != NULL &&
2272 additional_upper_limit()->IsInteger32Constant()) {
2273 int32_t limit_value = additional_upper_limit()->GetInteger32Constant();
2274 if (!additional_upper_limit_is_included()) {
2277 if (limit_value < result) result = limit_value;
2280 if (and_mask > 0 && and_mask < MAX_LIMIT) {
2281 if (and_mask < result) result = and_mask;
2285 // Add the effect of the or_mask.
2288 return result >= MAX_LIMIT ? kNoLimit : result;
2292 HValue* InductionVariableData::IgnoreOsrValue(HValue* v) {
2293 if (!v->IsPhi()) return v;
2294 HPhi* phi = HPhi::cast(v);
2295 if (phi->OperandCount() != 2) return v;
2296 if (phi->OperandAt(0)->block()->is_osr_entry()) {
2297 return phi->OperandAt(1);
2298 } else if (phi->OperandAt(1)->block()->is_osr_entry()) {
2299 return phi->OperandAt(0);
2306 InductionVariableData* InductionVariableData::GetInductionVariableData(
2308 v = IgnoreOsrValue(v);
2310 return HPhi::cast(v)->induction_variable_data();
2317 * Check if a conditional branch to "current_branch" with token "token" is
2318 * the branch that keeps the induction loop running (and, conversely, will
2319 * terminate it if the "other_branch" is taken).
2321 * Three conditions must be met:
2322 * - "current_branch" must be in the induction loop.
2323 * - "other_branch" must be out of the induction loop.
2324 * - "token" and the induction increment must be "compatible": the token should
2325 * be a condition that keeps the execution inside the loop until the limit is
2328 bool InductionVariableData::CheckIfBranchIsLoopGuard(
2330 HBasicBlock* current_branch,
2331 HBasicBlock* other_branch) {
2332 if (!phi()->block()->current_loop()->IsNestedInThisLoop(
2333 current_branch->current_loop())) {
2337 if (phi()->block()->current_loop()->IsNestedInThisLoop(
2338 other_branch->current_loop())) {
2342 if (increment() > 0 && (token == Token::LT || token == Token::LTE)) {
2345 if (increment() < 0 && (token == Token::GT || token == Token::GTE)) {
2348 if (Token::IsInequalityOp(token) && (increment() == 1 || increment() == -1)) {
2356 void InductionVariableData::ComputeLimitFromPredecessorBlock(
2358 LimitFromPredecessorBlock* result) {
2359 if (block->predecessors()->length() != 1) return;
2360 HBasicBlock* predecessor = block->predecessors()->at(0);
2361 HInstruction* end = predecessor->last();
2363 if (!end->IsCompareNumericAndBranch()) return;
2364 HCompareNumericAndBranch* branch = HCompareNumericAndBranch::cast(end);
2366 Token::Value token = branch->token();
2367 if (!Token::IsArithmeticCompareOp(token)) return;
2369 HBasicBlock* other_target;
2370 if (block == branch->SuccessorAt(0)) {
2371 other_target = branch->SuccessorAt(1);
2373 other_target = branch->SuccessorAt(0);
2374 token = Token::NegateCompareOp(token);
2375 DCHECK(block == branch->SuccessorAt(1));
2378 InductionVariableData* data;
2380 data = GetInductionVariableData(branch->left());
2381 HValue* limit = branch->right();
2383 data = GetInductionVariableData(branch->right());
2384 token = Token::ReverseCompareOp(token);
2385 limit = branch->left();
2389 result->variable = data;
2390 result->token = token;
2391 result->limit = limit;
2392 result->other_target = other_target;
2398 * Compute the limit that is imposed on an induction variable when entering
2400 * If the limit is the "proper" induction limit (the one that makes the loop
2401 * terminate when the induction variable reaches it) it is stored directly in
2402 * the induction variable data.
2403 * Otherwise the limit is written in "additional_limit" and the method
2406 bool InductionVariableData::ComputeInductionVariableLimit(
2408 InductionVariableLimitUpdate* additional_limit) {
2409 LimitFromPredecessorBlock limit;
2410 ComputeLimitFromPredecessorBlock(block, &limit);
2411 if (!limit.LimitIsValid()) return false;
2413 if (limit.variable->CheckIfBranchIsLoopGuard(limit.token,
2415 limit.other_target)) {
2416 limit.variable->limit_ = limit.limit;
2417 limit.variable->limit_included_ = limit.LimitIsIncluded();
2418 limit.variable->limit_validity_ = block;
2419 limit.variable->induction_exit_block_ = block->predecessors()->at(0);
2420 limit.variable->induction_exit_target_ = limit.other_target;
2423 additional_limit->updated_variable = limit.variable;
2424 additional_limit->limit = limit.limit;
2425 additional_limit->limit_is_upper = limit.LimitIsUpper();
2426 additional_limit->limit_is_included = limit.LimitIsIncluded();
2432 Range* HMathMinMax::InferRange(Zone* zone) {
2433 if (representation().IsSmiOrInteger32()) {
2434 Range* a = left()->range();
2435 Range* b = right()->range();
2436 Range* res = a->Copy(zone);
2437 if (operation_ == kMathMax) {
2438 res->CombinedMax(b);
2440 DCHECK(operation_ == kMathMin);
2441 res->CombinedMin(b);
2445 return HValue::InferRange(zone);
2450 void HPushArguments::AddInput(HValue* value) {
2451 inputs_.Add(NULL, value->block()->zone());
2452 SetOperandAt(OperandCount() - 1, value);
2456 std::ostream& HPhi::PrintTo(std::ostream& os) const { // NOLINT
2458 for (int i = 0; i < OperandCount(); ++i) {
2459 os << " " << NameOf(OperandAt(i)) << " ";
2461 return os << " uses:" << UseCount() << "_"
2462 << smi_non_phi_uses() + smi_indirect_uses() << "s_"
2463 << int32_non_phi_uses() + int32_indirect_uses() << "i_"
2464 << double_non_phi_uses() + double_indirect_uses() << "d_"
2465 << tagged_non_phi_uses() + tagged_indirect_uses() << "t"
2466 << TypeOf(this) << "]";
2470 void HPhi::AddInput(HValue* value) {
2471 inputs_.Add(NULL, value->block()->zone());
2472 SetOperandAt(OperandCount() - 1, value);
2473 // Mark phis that may have 'arguments' directly or indirectly as an operand.
2474 if (!CheckFlag(kIsArguments) && value->CheckFlag(kIsArguments)) {
2475 SetFlag(kIsArguments);
2480 bool HPhi::HasRealUses() {
2481 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
2482 if (!it.value()->IsPhi()) return true;
2488 HValue* HPhi::GetRedundantReplacement() {
2489 HValue* candidate = NULL;
2490 int count = OperandCount();
2492 while (position < count && candidate == NULL) {
2493 HValue* current = OperandAt(position++);
2494 if (current != this) candidate = current;
2496 while (position < count) {
2497 HValue* current = OperandAt(position++);
2498 if (current != this && current != candidate) return NULL;
2500 DCHECK(candidate != this);
2505 void HPhi::DeleteFromGraph() {
2506 DCHECK(block() != NULL);
2507 block()->RemovePhi(this);
2508 DCHECK(block() == NULL);
2512 void HPhi::InitRealUses(int phi_id) {
2513 // Initialize real uses.
2515 // Compute a conservative approximation of truncating uses before inferring
2516 // representations. The proper, exact computation will be done later, when
2517 // inserting representation changes.
2518 SetFlag(kTruncatingToSmi);
2519 SetFlag(kTruncatingToInt32);
2520 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
2521 HValue* value = it.value();
2522 if (!value->IsPhi()) {
2523 Representation rep = value->observed_input_representation(it.index());
2524 non_phi_uses_[rep.kind()] += 1;
2525 if (FLAG_trace_representation) {
2526 PrintF("#%d Phi is used by real #%d %s as %s\n",
2527 id(), value->id(), value->Mnemonic(), rep.Mnemonic());
2529 if (!value->IsSimulate()) {
2530 if (!value->CheckFlag(kTruncatingToSmi)) {
2531 ClearFlag(kTruncatingToSmi);
2533 if (!value->CheckFlag(kTruncatingToInt32)) {
2534 ClearFlag(kTruncatingToInt32);
2542 void HPhi::AddNonPhiUsesFrom(HPhi* other) {
2543 if (FLAG_trace_representation) {
2544 PrintF("adding to #%d Phi uses of #%d Phi: s%d i%d d%d t%d\n",
2546 other->non_phi_uses_[Representation::kSmi],
2547 other->non_phi_uses_[Representation::kInteger32],
2548 other->non_phi_uses_[Representation::kDouble],
2549 other->non_phi_uses_[Representation::kTagged]);
2552 for (int i = 0; i < Representation::kNumRepresentations; i++) {
2553 indirect_uses_[i] += other->non_phi_uses_[i];
2558 void HPhi::AddIndirectUsesTo(int* dest) {
2559 for (int i = 0; i < Representation::kNumRepresentations; i++) {
2560 dest[i] += indirect_uses_[i];
2565 void HSimulate::MergeWith(ZoneList<HSimulate*>* list) {
2566 while (!list->is_empty()) {
2567 HSimulate* from = list->RemoveLast();
2568 ZoneList<HValue*>* from_values = &from->values_;
2569 for (int i = 0; i < from_values->length(); ++i) {
2570 if (from->HasAssignedIndexAt(i)) {
2571 int index = from->GetAssignedIndexAt(i);
2572 if (HasValueForIndex(index)) continue;
2573 AddAssignedValue(index, from_values->at(i));
2575 if (pop_count_ > 0) {
2578 AddPushedValue(from_values->at(i));
2582 pop_count_ += from->pop_count_;
2583 from->DeleteAndReplaceWith(NULL);
2588 std::ostream& HSimulate::PrintDataTo(std::ostream& os) const { // NOLINT
2589 os << "id=" << ast_id().ToInt();
2590 if (pop_count_ > 0) os << " pop " << pop_count_;
2591 if (values_.length() > 0) {
2592 if (pop_count_ > 0) os << " /";
2593 for (int i = values_.length() - 1; i >= 0; --i) {
2594 if (HasAssignedIndexAt(i)) {
2595 os << " var[" << GetAssignedIndexAt(i) << "] = ";
2599 os << NameOf(values_[i]);
2600 if (i > 0) os << ",";
2607 void HSimulate::ReplayEnvironment(HEnvironment* env) {
2608 if (is_done_with_replay()) return;
2609 DCHECK(env != NULL);
2610 env->set_ast_id(ast_id());
2611 env->Drop(pop_count());
2612 for (int i = values()->length() - 1; i >= 0; --i) {
2613 HValue* value = values()->at(i);
2614 if (HasAssignedIndexAt(i)) {
2615 env->Bind(GetAssignedIndexAt(i), value);
2620 set_done_with_replay();
2624 static void ReplayEnvironmentNested(const ZoneList<HValue*>* values,
2625 HCapturedObject* other) {
2626 for (int i = 0; i < values->length(); ++i) {
2627 HValue* value = values->at(i);
2628 if (value->IsCapturedObject()) {
2629 if (HCapturedObject::cast(value)->capture_id() == other->capture_id()) {
2630 values->at(i) = other;
2632 ReplayEnvironmentNested(HCapturedObject::cast(value)->values(), other);
2639 // Replay captured objects by replacing all captured objects with the
2640 // same capture id in the current and all outer environments.
2641 void HCapturedObject::ReplayEnvironment(HEnvironment* env) {
2642 DCHECK(env != NULL);
2643 while (env != NULL) {
2644 ReplayEnvironmentNested(env->values(), this);
2650 std::ostream& HCapturedObject::PrintDataTo(std::ostream& os) const { // NOLINT
2651 os << "#" << capture_id() << " ";
2652 return HDematerializedObject::PrintDataTo(os);
2656 void HEnterInlined::RegisterReturnTarget(HBasicBlock* return_target,
2658 DCHECK(return_target->IsInlineReturnTarget());
2659 return_targets_.Add(return_target, zone);
2663 std::ostream& HEnterInlined::PrintDataTo(std::ostream& os) const { // NOLINT
2664 return os << function()->debug_name()->ToCString().get();
2668 static bool IsInteger32(double value) {
2669 if (value >= std::numeric_limits<int32_t>::min() &&
2670 value <= std::numeric_limits<int32_t>::max()) {
2671 double roundtrip_value = static_cast<double>(static_cast<int32_t>(value));
2672 return bit_cast<int64_t>(roundtrip_value) == bit_cast<int64_t>(value);
2678 HConstant::HConstant(Special special)
2679 : HTemplateInstruction<0>(HType::TaggedNumber()),
2680 object_(Handle<Object>::null()),
2681 object_map_(Handle<Map>::null()),
2682 bit_field_(HasDoubleValueField::encode(true) |
2683 InstanceTypeField::encode(kUnknownInstanceType)),
2685 DCHECK_EQ(kHoleNaN, special);
2686 std::memcpy(&double_value_, &kHoleNanInt64, sizeof(double_value_));
2687 Initialize(Representation::Double());
2691 HConstant::HConstant(Handle<Object> object, Representation r)
2692 : HTemplateInstruction<0>(HType::FromValue(object)),
2693 object_(Unique<Object>::CreateUninitialized(object)),
2694 object_map_(Handle<Map>::null()),
2695 bit_field_(HasStableMapValueField::encode(false) |
2696 HasSmiValueField::encode(false) |
2697 HasInt32ValueField::encode(false) |
2698 HasDoubleValueField::encode(false) |
2699 HasExternalReferenceValueField::encode(false) |
2700 IsNotInNewSpaceField::encode(true) |
2701 BooleanValueField::encode(object->BooleanValue()) |
2702 IsUndetectableField::encode(false) |
2703 InstanceTypeField::encode(kUnknownInstanceType)) {
2704 if (object->IsHeapObject()) {
2705 Handle<HeapObject> heap_object = Handle<HeapObject>::cast(object);
2706 Isolate* isolate = heap_object->GetIsolate();
2707 Handle<Map> map(heap_object->map(), isolate);
2708 bit_field_ = IsNotInNewSpaceField::update(
2709 bit_field_, !isolate->heap()->InNewSpace(*object));
2710 bit_field_ = InstanceTypeField::update(bit_field_, map->instance_type());
2712 IsUndetectableField::update(bit_field_, map->is_undetectable());
2713 if (map->is_stable()) object_map_ = Unique<Map>::CreateImmovable(map);
2714 bit_field_ = HasStableMapValueField::update(
2716 HasMapValue() && Handle<Map>::cast(heap_object)->is_stable());
2718 if (object->IsNumber()) {
2719 double n = object->Number();
2720 bool has_int32_value = IsInteger32(n);
2721 bit_field_ = HasInt32ValueField::update(bit_field_, has_int32_value);
2722 int32_value_ = DoubleToInt32(n);
2723 bit_field_ = HasSmiValueField::update(
2724 bit_field_, has_int32_value && Smi::IsValid(int32_value_));
2726 bit_field_ = HasDoubleValueField::update(bit_field_, true);
2727 // TODO(titzer): if this heap number is new space, tenure a new one.
2734 HConstant::HConstant(Unique<Object> object, Unique<Map> object_map,
2735 bool has_stable_map_value, Representation r, HType type,
2736 bool is_not_in_new_space, bool boolean_value,
2737 bool is_undetectable, InstanceType instance_type)
2738 : HTemplateInstruction<0>(type),
2740 object_map_(object_map),
2741 bit_field_(HasStableMapValueField::encode(has_stable_map_value) |
2742 HasSmiValueField::encode(false) |
2743 HasInt32ValueField::encode(false) |
2744 HasDoubleValueField::encode(false) |
2745 HasExternalReferenceValueField::encode(false) |
2746 IsNotInNewSpaceField::encode(is_not_in_new_space) |
2747 BooleanValueField::encode(boolean_value) |
2748 IsUndetectableField::encode(is_undetectable) |
2749 InstanceTypeField::encode(instance_type)) {
2750 DCHECK(!object.handle().is_null());
2751 DCHECK(!type.IsTaggedNumber() || type.IsNone());
2756 HConstant::HConstant(int32_t integer_value, Representation r,
2757 bool is_not_in_new_space, Unique<Object> object)
2759 object_map_(Handle<Map>::null()),
2760 bit_field_(HasStableMapValueField::encode(false) |
2761 HasSmiValueField::encode(Smi::IsValid(integer_value)) |
2762 HasInt32ValueField::encode(true) |
2763 HasDoubleValueField::encode(true) |
2764 HasExternalReferenceValueField::encode(false) |
2765 IsNotInNewSpaceField::encode(is_not_in_new_space) |
2766 BooleanValueField::encode(integer_value != 0) |
2767 IsUndetectableField::encode(false) |
2768 InstanceTypeField::encode(kUnknownInstanceType)),
2769 int32_value_(integer_value),
2770 double_value_(FastI2D(integer_value)) {
2771 // It's possible to create a constant with a value in Smi-range but stored
2772 // in a (pre-existing) HeapNumber. See crbug.com/349878.
2773 bool could_be_heapobject = r.IsTagged() && !object.handle().is_null();
2774 bool is_smi = HasSmiValue() && !could_be_heapobject;
2775 set_type(is_smi ? HType::Smi() : HType::TaggedNumber());
2780 HConstant::HConstant(double double_value, Representation r,
2781 bool is_not_in_new_space, Unique<Object> object)
2783 object_map_(Handle<Map>::null()),
2784 bit_field_(HasStableMapValueField::encode(false) |
2785 HasInt32ValueField::encode(IsInteger32(double_value)) |
2786 HasDoubleValueField::encode(true) |
2787 HasExternalReferenceValueField::encode(false) |
2788 IsNotInNewSpaceField::encode(is_not_in_new_space) |
2789 BooleanValueField::encode(double_value != 0 &&
2790 !std::isnan(double_value)) |
2791 IsUndetectableField::encode(false) |
2792 InstanceTypeField::encode(kUnknownInstanceType)),
2793 int32_value_(DoubleToInt32(double_value)),
2794 double_value_(double_value) {
2795 bit_field_ = HasSmiValueField::update(
2796 bit_field_, HasInteger32Value() && Smi::IsValid(int32_value_));
2797 // It's possible to create a constant with a value in Smi-range but stored
2798 // in a (pre-existing) HeapNumber. See crbug.com/349878.
2799 bool could_be_heapobject = r.IsTagged() && !object.handle().is_null();
2800 bool is_smi = HasSmiValue() && !could_be_heapobject;
2801 set_type(is_smi ? HType::Smi() : HType::TaggedNumber());
2806 HConstant::HConstant(ExternalReference reference)
2807 : HTemplateInstruction<0>(HType::Any()),
2808 object_(Unique<Object>(Handle<Object>::null())),
2809 object_map_(Handle<Map>::null()),
2811 HasStableMapValueField::encode(false) |
2812 HasSmiValueField::encode(false) | HasInt32ValueField::encode(false) |
2813 HasDoubleValueField::encode(false) |
2814 HasExternalReferenceValueField::encode(true) |
2815 IsNotInNewSpaceField::encode(true) | BooleanValueField::encode(true) |
2816 IsUndetectableField::encode(false) |
2817 InstanceTypeField::encode(kUnknownInstanceType)),
2818 external_reference_value_(reference) {
2819 Initialize(Representation::External());
2823 void HConstant::Initialize(Representation r) {
2825 if (HasSmiValue() && SmiValuesAre31Bits()) {
2826 r = Representation::Smi();
2827 } else if (HasInteger32Value()) {
2828 r = Representation::Integer32();
2829 } else if (HasDoubleValue()) {
2830 r = Representation::Double();
2831 } else if (HasExternalReferenceValue()) {
2832 r = Representation::External();
2834 Handle<Object> object = object_.handle();
2835 if (object->IsJSObject()) {
2836 // Try to eagerly migrate JSObjects that have deprecated maps.
2837 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
2838 if (js_object->map()->is_deprecated()) {
2839 JSObject::TryMigrateInstance(js_object);
2842 r = Representation::Tagged();
2846 // If we have an existing handle, zap it, because it might be a heap
2847 // number which we must not re-use when copying this HConstant to
2848 // Tagged representation later, because having Smi representation now
2849 // could cause heap object checks not to get emitted.
2850 object_ = Unique<Object>(Handle<Object>::null());
2852 if (r.IsSmiOrInteger32() && object_.handle().is_null()) {
2853 // If it's not a heap object, it can't be in new space.
2854 bit_field_ = IsNotInNewSpaceField::update(bit_field_, true);
2856 set_representation(r);
2861 bool HConstant::ImmortalImmovable() const {
2862 if (HasInteger32Value()) {
2865 if (HasDoubleValue()) {
2866 if (IsSpecialDouble()) {
2871 if (HasExternalReferenceValue()) {
2875 DCHECK(!object_.handle().is_null());
2876 Heap* heap = isolate()->heap();
2877 DCHECK(!object_.IsKnownGlobal(heap->minus_zero_value()));
2878 DCHECK(!object_.IsKnownGlobal(heap->nan_value()));
2880 #define IMMORTAL_IMMOVABLE_ROOT(name) \
2881 object_.IsKnownGlobal(heap->root(Heap::k##name##RootIndex)) ||
2882 IMMORTAL_IMMOVABLE_ROOT_LIST(IMMORTAL_IMMOVABLE_ROOT)
2883 #undef IMMORTAL_IMMOVABLE_ROOT
2884 #define INTERNALIZED_STRING(name, value) \
2885 object_.IsKnownGlobal(heap->name()) ||
2886 INTERNALIZED_STRING_LIST(INTERNALIZED_STRING)
2887 #undef INTERNALIZED_STRING
2888 #define STRING_TYPE(NAME, size, name, Name) \
2889 object_.IsKnownGlobal(heap->name##_map()) ||
2890 STRING_TYPE_LIST(STRING_TYPE)
2896 bool HConstant::EmitAtUses() {
2898 if (block()->graph()->has_osr() &&
2899 block()->graph()->IsStandardConstant(this)) {
2900 // TODO(titzer): this seems like a hack that should be fixed by custom OSR.
2903 if (HasNoUses()) return true;
2904 if (IsCell()) return false;
2905 if (representation().IsDouble()) return false;
2906 if (representation().IsExternal()) return false;
2911 HConstant* HConstant::CopyToRepresentation(Representation r, Zone* zone) const {
2912 if (r.IsSmi() && !HasSmiValue()) return NULL;
2913 if (r.IsInteger32() && !HasInteger32Value()) return NULL;
2914 if (r.IsDouble() && !HasDoubleValue()) return NULL;
2915 if (r.IsExternal() && !HasExternalReferenceValue()) return NULL;
2916 if (HasInteger32Value()) {
2917 return new (zone) HConstant(int32_value_, r, NotInNewSpace(), object_);
2919 if (HasDoubleValue()) {
2920 return new (zone) HConstant(double_value_, r, NotInNewSpace(), object_);
2922 if (HasExternalReferenceValue()) {
2923 return new(zone) HConstant(external_reference_value_);
2925 DCHECK(!object_.handle().is_null());
2926 return new (zone) HConstant(object_, object_map_, HasStableMapValue(), r,
2927 type_, NotInNewSpace(), BooleanValue(),
2928 IsUndetectable(), GetInstanceType());
2932 Maybe<HConstant*> HConstant::CopyToTruncatedInt32(Zone* zone) {
2933 HConstant* res = NULL;
2934 if (HasInteger32Value()) {
2935 res = new (zone) HConstant(int32_value_, Representation::Integer32(),
2936 NotInNewSpace(), object_);
2937 } else if (HasDoubleValue()) {
2939 HConstant(DoubleToInt32(double_value_), Representation::Integer32(),
2940 NotInNewSpace(), object_);
2942 return res != NULL ? Just(res) : Nothing<HConstant*>();
2946 Maybe<HConstant*> HConstant::CopyToTruncatedNumber(Isolate* isolate,
2948 HConstant* res = NULL;
2949 Handle<Object> handle = this->handle(isolate);
2950 if (handle->IsBoolean()) {
2951 res = handle->BooleanValue() ?
2952 new(zone) HConstant(1) : new(zone) HConstant(0);
2953 } else if (handle->IsUndefined()) {
2954 res = new (zone) HConstant(std::numeric_limits<double>::quiet_NaN());
2955 } else if (handle->IsNull()) {
2956 res = new(zone) HConstant(0);
2958 return res != NULL ? Just(res) : Nothing<HConstant*>();
2962 std::ostream& HConstant::PrintDataTo(std::ostream& os) const { // NOLINT
2963 if (HasInteger32Value()) {
2964 os << int32_value_ << " ";
2965 } else if (HasDoubleValue()) {
2966 os << double_value_ << " ";
2967 } else if (HasExternalReferenceValue()) {
2968 os << reinterpret_cast<void*>(external_reference_value_.address()) << " ";
2970 // The handle() method is silently and lazily mutating the object.
2971 Handle<Object> h = const_cast<HConstant*>(this)->handle(isolate());
2972 os << Brief(*h) << " ";
2973 if (HasStableMapValue()) os << "[stable-map] ";
2974 if (HasObjectMap()) os << "[map " << *ObjectMap().handle() << "] ";
2976 if (!NotInNewSpace()) os << "[new space] ";
2981 std::ostream& HBinaryOperation::PrintDataTo(std::ostream& os) const { // NOLINT
2982 os << NameOf(left()) << " " << NameOf(right());
2983 if (CheckFlag(kCanOverflow)) os << " !";
2984 if (CheckFlag(kBailoutOnMinusZero)) os << " -0?";
2989 void HBinaryOperation::InferRepresentation(HInferRepresentationPhase* h_infer) {
2990 DCHECK(CheckFlag(kFlexibleRepresentation));
2991 Representation new_rep = RepresentationFromInputs();
2992 UpdateRepresentation(new_rep, h_infer, "inputs");
2994 if (representation().IsSmi() && HasNonSmiUse()) {
2995 UpdateRepresentation(
2996 Representation::Integer32(), h_infer, "use requirements");
2999 if (observed_output_representation_.IsNone()) {
3000 new_rep = RepresentationFromUses();
3001 UpdateRepresentation(new_rep, h_infer, "uses");
3003 new_rep = RepresentationFromOutput();
3004 UpdateRepresentation(new_rep, h_infer, "output");
3009 Representation HBinaryOperation::RepresentationFromInputs() {
3010 // Determine the worst case of observed input representations and
3011 // the currently assumed output representation.
3012 Representation rep = representation();
3013 for (int i = 1; i <= 2; ++i) {
3014 rep = rep.generalize(observed_input_representation(i));
3016 // If any of the actual input representation is more general than what we
3017 // have so far but not Tagged, use that representation instead.
3018 Representation left_rep = left()->representation();
3019 Representation right_rep = right()->representation();
3020 if (!left_rep.IsTagged()) rep = rep.generalize(left_rep);
3021 if (!right_rep.IsTagged()) rep = rep.generalize(right_rep);
3027 bool HBinaryOperation::IgnoreObservedOutputRepresentation(
3028 Representation current_rep) {
3029 return ((current_rep.IsInteger32() && CheckUsesForFlag(kTruncatingToInt32)) ||
3030 (current_rep.IsSmi() && CheckUsesForFlag(kTruncatingToSmi))) &&
3031 // Mul in Integer32 mode would be too precise.
3032 (!this->IsMul() || HMul::cast(this)->MulMinusOne());
3036 Representation HBinaryOperation::RepresentationFromOutput() {
3037 Representation rep = representation();
3038 // Consider observed output representation, but ignore it if it's Double,
3039 // this instruction is not a division, and all its uses are truncating
3041 if (observed_output_representation_.is_more_general_than(rep) &&
3042 !IgnoreObservedOutputRepresentation(rep)) {
3043 return observed_output_representation_;
3045 return Representation::None();
3049 void HBinaryOperation::AssumeRepresentation(Representation r) {
3050 set_observed_input_representation(1, r);
3051 set_observed_input_representation(2, r);
3052 HValue::AssumeRepresentation(r);
3056 void HMathMinMax::InferRepresentation(HInferRepresentationPhase* h_infer) {
3057 DCHECK(CheckFlag(kFlexibleRepresentation));
3058 Representation new_rep = RepresentationFromInputs();
3059 UpdateRepresentation(new_rep, h_infer, "inputs");
3060 // Do not care about uses.
3064 Range* HBitwise::InferRange(Zone* zone) {
3065 if (op() == Token::BIT_XOR) {
3066 if (left()->HasRange() && right()->HasRange()) {
3067 // The maximum value has the high bit, and all bits below, set:
3069 // If the range can be negative, the minimum int is a negative number with
3070 // the high bit, and all bits below, unset:
3072 // If it cannot be negative, conservatively choose 0 as minimum int.
3073 int64_t left_upper = left()->range()->upper();
3074 int64_t left_lower = left()->range()->lower();
3075 int64_t right_upper = right()->range()->upper();
3076 int64_t right_lower = right()->range()->lower();
3078 if (left_upper < 0) left_upper = ~left_upper;
3079 if (left_lower < 0) left_lower = ~left_lower;
3080 if (right_upper < 0) right_upper = ~right_upper;
3081 if (right_lower < 0) right_lower = ~right_lower;
3083 int high = MostSignificantBit(
3084 static_cast<uint32_t>(
3085 left_upper | left_lower | right_upper | right_lower));
3089 int32_t min = (left()->range()->CanBeNegative() ||
3090 right()->range()->CanBeNegative())
3091 ? static_cast<int32_t>(-limit) : 0;
3092 return new(zone) Range(min, static_cast<int32_t>(limit - 1));
3094 Range* result = HValue::InferRange(zone);
3095 result->set_can_be_minus_zero(false);
3098 const int32_t kDefaultMask = static_cast<int32_t>(0xffffffff);
3099 int32_t left_mask = (left()->range() != NULL)
3100 ? left()->range()->Mask()
3102 int32_t right_mask = (right()->range() != NULL)
3103 ? right()->range()->Mask()
3105 int32_t result_mask = (op() == Token::BIT_AND)
3106 ? left_mask & right_mask
3107 : left_mask | right_mask;
3108 if (result_mask >= 0) return new(zone) Range(0, result_mask);
3110 Range* result = HValue::InferRange(zone);
3111 result->set_can_be_minus_zero(false);
3116 Range* HSar::InferRange(Zone* zone) {
3117 if (right()->IsConstant()) {
3118 HConstant* c = HConstant::cast(right());
3119 if (c->HasInteger32Value()) {
3120 Range* result = (left()->range() != NULL)
3121 ? left()->range()->Copy(zone)
3122 : new(zone) Range();
3123 result->Sar(c->Integer32Value());
3127 return HValue::InferRange(zone);
3131 Range* HShr::InferRange(Zone* zone) {
3132 if (right()->IsConstant()) {
3133 HConstant* c = HConstant::cast(right());
3134 if (c->HasInteger32Value()) {
3135 int shift_count = c->Integer32Value() & 0x1f;
3136 if (left()->range()->CanBeNegative()) {
3137 // Only compute bounds if the result always fits into an int32.
3138 return (shift_count >= 1)
3139 ? new(zone) Range(0,
3140 static_cast<uint32_t>(0xffffffff) >> shift_count)
3141 : new(zone) Range();
3143 // For positive inputs we can use the >> operator.
3144 Range* result = (left()->range() != NULL)
3145 ? left()->range()->Copy(zone)
3146 : new(zone) Range();
3147 result->Sar(c->Integer32Value());
3152 return HValue::InferRange(zone);
3156 Range* HShl::InferRange(Zone* zone) {
3157 if (right()->IsConstant()) {
3158 HConstant* c = HConstant::cast(right());
3159 if (c->HasInteger32Value()) {
3160 Range* result = (left()->range() != NULL)
3161 ? left()->range()->Copy(zone)
3162 : new(zone) Range();
3163 result->Shl(c->Integer32Value());
3167 return HValue::InferRange(zone);
3171 Range* HLoadNamedField::InferRange(Zone* zone) {
3172 if (access().representation().IsInteger8()) {
3173 return new(zone) Range(kMinInt8, kMaxInt8);
3175 if (access().representation().IsUInteger8()) {
3176 return new(zone) Range(kMinUInt8, kMaxUInt8);
3178 if (access().representation().IsInteger16()) {
3179 return new(zone) Range(kMinInt16, kMaxInt16);
3181 if (access().representation().IsUInteger16()) {
3182 return new(zone) Range(kMinUInt16, kMaxUInt16);
3184 if (access().IsStringLength()) {
3185 return new(zone) Range(0, String::kMaxLength);
3187 return HValue::InferRange(zone);
3191 Range* HLoadKeyed::InferRange(Zone* zone) {
3192 switch (elements_kind()) {
3193 case EXTERNAL_INT8_ELEMENTS:
3195 return new(zone) Range(kMinInt8, kMaxInt8);
3196 case EXTERNAL_UINT8_ELEMENTS:
3197 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
3198 case UINT8_ELEMENTS:
3199 case UINT8_CLAMPED_ELEMENTS:
3200 return new(zone) Range(kMinUInt8, kMaxUInt8);
3201 case EXTERNAL_INT16_ELEMENTS:
3202 case INT16_ELEMENTS:
3203 return new(zone) Range(kMinInt16, kMaxInt16);
3204 case EXTERNAL_UINT16_ELEMENTS:
3205 case UINT16_ELEMENTS:
3206 return new(zone) Range(kMinUInt16, kMaxUInt16);
3208 return HValue::InferRange(zone);
3213 std::ostream& HCompareGeneric::PrintDataTo(std::ostream& os) const { // NOLINT
3214 os << Token::Name(token()) << " ";
3215 return HBinaryOperation::PrintDataTo(os);
3219 std::ostream& HStringCompareAndBranch::PrintDataTo(
3220 std::ostream& os) const { // NOLINT
3221 os << Token::Name(token()) << " ";
3222 return HControlInstruction::PrintDataTo(os);
3226 std::ostream& HCompareNumericAndBranch::PrintDataTo(
3227 std::ostream& os) const { // NOLINT
3228 os << Token::Name(token()) << " " << NameOf(left()) << " " << NameOf(right());
3229 return HControlInstruction::PrintDataTo(os);
3233 std::ostream& HCompareObjectEqAndBranch::PrintDataTo(
3234 std::ostream& os) const { // NOLINT
3235 os << NameOf(left()) << " " << NameOf(right());
3236 return HControlInstruction::PrintDataTo(os);
3240 bool HCompareObjectEqAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3241 if (known_successor_index() != kNoKnownSuccessorIndex) {
3242 *block = SuccessorAt(known_successor_index());
3245 if (FLAG_fold_constants && left()->IsConstant() && right()->IsConstant()) {
3246 *block = HConstant::cast(left())->DataEquals(HConstant::cast(right()))
3247 ? FirstSuccessor() : SecondSuccessor();
3255 bool ConstantIsObject(HConstant* constant, Isolate* isolate) {
3256 if (constant->HasNumberValue()) return false;
3257 if (constant->GetUnique().IsKnownGlobal(isolate->heap()->null_value())) {
3260 if (constant->IsUndetectable()) return false;
3261 InstanceType type = constant->GetInstanceType();
3262 return (FIRST_NONCALLABLE_SPEC_OBJECT_TYPE <= type) &&
3263 (type <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
3267 bool HIsObjectAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3268 if (FLAG_fold_constants && value()->IsConstant()) {
3269 *block = ConstantIsObject(HConstant::cast(value()), isolate())
3270 ? FirstSuccessor() : SecondSuccessor();
3278 bool HIsStringAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3279 if (known_successor_index() != kNoKnownSuccessorIndex) {
3280 *block = SuccessorAt(known_successor_index());
3283 if (FLAG_fold_constants && value()->IsConstant()) {
3284 *block = HConstant::cast(value())->HasStringValue()
3285 ? FirstSuccessor() : SecondSuccessor();
3288 if (value()->type().IsString()) {
3289 *block = FirstSuccessor();
3292 if (value()->type().IsSmi() ||
3293 value()->type().IsNull() ||
3294 value()->type().IsBoolean() ||
3295 value()->type().IsUndefined() ||
3296 value()->type().IsJSObject()) {
3297 *block = SecondSuccessor();
3305 bool HIsUndetectableAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3306 if (FLAG_fold_constants && value()->IsConstant()) {
3307 *block = HConstant::cast(value())->IsUndetectable()
3308 ? FirstSuccessor() : SecondSuccessor();
3316 bool HHasInstanceTypeAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3317 if (FLAG_fold_constants && value()->IsConstant()) {
3318 InstanceType type = HConstant::cast(value())->GetInstanceType();
3319 *block = (from_ <= type) && (type <= to_)
3320 ? FirstSuccessor() : SecondSuccessor();
3328 void HCompareHoleAndBranch::InferRepresentation(
3329 HInferRepresentationPhase* h_infer) {
3330 ChangeRepresentation(value()->representation());
3334 bool HCompareNumericAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3335 if (left() == right() &&
3336 left()->representation().IsSmiOrInteger32()) {
3337 *block = (token() == Token::EQ ||
3338 token() == Token::EQ_STRICT ||
3339 token() == Token::LTE ||
3340 token() == Token::GTE)
3341 ? FirstSuccessor() : SecondSuccessor();
3349 bool HCompareMinusZeroAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3350 if (FLAG_fold_constants && value()->IsConstant()) {
3351 HConstant* constant = HConstant::cast(value());
3352 if (constant->HasDoubleValue()) {
3353 *block = IsMinusZero(constant->DoubleValue())
3354 ? FirstSuccessor() : SecondSuccessor();
3358 if (value()->representation().IsSmiOrInteger32()) {
3359 // A Smi or Integer32 cannot contain minus zero.
3360 *block = SecondSuccessor();
3368 void HCompareMinusZeroAndBranch::InferRepresentation(
3369 HInferRepresentationPhase* h_infer) {
3370 ChangeRepresentation(value()->representation());
3374 std::ostream& HGoto::PrintDataTo(std::ostream& os) const { // NOLINT
3375 return os << *SuccessorAt(0);
3379 void HCompareNumericAndBranch::InferRepresentation(
3380 HInferRepresentationPhase* h_infer) {
3381 Representation left_rep = left()->representation();
3382 Representation right_rep = right()->representation();
3383 Representation observed_left = observed_input_representation(0);
3384 Representation observed_right = observed_input_representation(1);
3386 Representation rep = Representation::None();
3387 rep = rep.generalize(observed_left);
3388 rep = rep.generalize(observed_right);
3389 if (rep.IsNone() || rep.IsSmiOrInteger32()) {
3390 if (!left_rep.IsTagged()) rep = rep.generalize(left_rep);
3391 if (!right_rep.IsTagged()) rep = rep.generalize(right_rep);
3393 rep = Representation::Double();
3396 if (rep.IsDouble()) {
3397 // According to the ES5 spec (11.9.3, 11.8.5), Equality comparisons (==, ===
3398 // and !=) have special handling of undefined, e.g. undefined == undefined
3399 // is 'true'. Relational comparisons have a different semantic, first
3400 // calling ToPrimitive() on their arguments. The standard Crankshaft
3401 // tagged-to-double conversion to ensure the HCompareNumericAndBranch's
3402 // inputs are doubles caused 'undefined' to be converted to NaN. That's
3403 // compatible out-of-the box with ordered relational comparisons (<, >, <=,
3404 // >=). However, for equality comparisons (and for 'in' and 'instanceof'),
3405 // it is not consistent with the spec. For example, it would cause undefined
3406 // == undefined (should be true) to be evaluated as NaN == NaN
3407 // (false). Therefore, any comparisons other than ordered relational
3408 // comparisons must cause a deopt when one of their arguments is undefined.
3410 if (Token::IsOrderedRelationalCompareOp(token_) && !is_strong(strength())) {
3411 SetFlag(kAllowUndefinedAsNaN);
3414 ChangeRepresentation(rep);
3418 std::ostream& HParameter::PrintDataTo(std::ostream& os) const { // NOLINT
3419 return os << index();
3423 std::ostream& HLoadNamedField::PrintDataTo(std::ostream& os) const { // NOLINT
3424 os << NameOf(object()) << access_;
3426 if (maps() != NULL) {
3427 os << " [" << *maps()->at(0).handle();
3428 for (int i = 1; i < maps()->size(); ++i) {
3429 os << "," << *maps()->at(i).handle();
3434 if (HasDependency()) os << " " << NameOf(dependency());
3439 std::ostream& HLoadNamedGeneric::PrintDataTo(
3440 std::ostream& os) const { // NOLINT
3441 Handle<String> n = Handle<String>::cast(name());
3442 return os << NameOf(object()) << "." << n->ToCString().get();
3446 std::ostream& HLoadKeyed::PrintDataTo(std::ostream& os) const { // NOLINT
3447 if (!is_external()) {
3448 os << NameOf(elements());
3450 DCHECK(elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
3451 elements_kind() <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND);
3452 os << NameOf(elements()) << "." << ElementsKindToString(elements_kind());
3455 os << "[" << NameOf(key());
3456 if (IsDehoisted()) os << " + " << base_offset();
3459 if (HasDependency()) os << " " << NameOf(dependency());
3460 if (RequiresHoleCheck()) os << " check_hole";
3465 bool HLoadKeyed::TryIncreaseBaseOffset(uint32_t increase_by_value) {
3466 // The base offset is usually simply the size of the array header, except
3467 // with dehoisting adds an addition offset due to a array index key
3468 // manipulation, in which case it becomes (array header size +
3469 // constant-offset-from-key * kPointerSize)
3470 uint32_t base_offset = BaseOffsetField::decode(bit_field_);
3471 v8::base::internal::CheckedNumeric<uint32_t> addition_result = base_offset;
3472 addition_result += increase_by_value;
3473 if (!addition_result.IsValid()) return false;
3474 base_offset = addition_result.ValueOrDie();
3475 if (!BaseOffsetField::is_valid(base_offset)) return false;
3476 bit_field_ = BaseOffsetField::update(bit_field_, base_offset);
3481 bool HLoadKeyed::UsesMustHandleHole() const {
3482 if (IsFastPackedElementsKind(elements_kind())) {
3486 if (IsExternalArrayElementsKind(elements_kind())) {
3490 if (hole_mode() == ALLOW_RETURN_HOLE) {
3491 if (IsFastDoubleElementsKind(elements_kind())) {
3492 return AllUsesCanTreatHoleAsNaN();
3497 if (IsFastDoubleElementsKind(elements_kind())) {
3501 // Holes are only returned as tagged values.
3502 if (!representation().IsTagged()) {
3506 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
3507 HValue* use = it.value();
3508 if (!use->IsChange()) return false;
3515 bool HLoadKeyed::AllUsesCanTreatHoleAsNaN() const {
3516 return IsFastDoubleElementsKind(elements_kind()) &&
3517 CheckUsesForFlag(HValue::kAllowUndefinedAsNaN);
3521 bool HLoadKeyed::RequiresHoleCheck() const {
3522 if (IsFastPackedElementsKind(elements_kind())) {
3526 if (IsExternalArrayElementsKind(elements_kind())) {
3530 if (hole_mode() == CONVERT_HOLE_TO_UNDEFINED) {
3534 return !UsesMustHandleHole();
3538 std::ostream& HLoadKeyedGeneric::PrintDataTo(
3539 std::ostream& os) const { // NOLINT
3540 return os << NameOf(object()) << "[" << NameOf(key()) << "]";
3544 HValue* HLoadKeyedGeneric::Canonicalize() {
3545 // Recognize generic keyed loads that use property name generated
3546 // by for-in statement as a key and rewrite them into fast property load
3548 if (key()->IsLoadKeyed()) {
3549 HLoadKeyed* key_load = HLoadKeyed::cast(key());
3550 if (key_load->elements()->IsForInCacheArray()) {
3551 HForInCacheArray* names_cache =
3552 HForInCacheArray::cast(key_load->elements());
3554 if (names_cache->enumerable() == object()) {
3555 HForInCacheArray* index_cache =
3556 names_cache->index_cache();
3557 HCheckMapValue* map_check = HCheckMapValue::New(
3558 block()->graph()->isolate(), block()->graph()->zone(),
3559 block()->graph()->GetInvalidContext(), object(),
3560 names_cache->map());
3561 HInstruction* index = HLoadKeyed::New(
3562 block()->graph()->isolate(), block()->graph()->zone(),
3563 block()->graph()->GetInvalidContext(), index_cache, key_load->key(),
3564 key_load->key(), key_load->elements_kind());
3565 map_check->InsertBefore(this);
3566 index->InsertBefore(this);
3567 return Prepend(new(block()->zone()) HLoadFieldByIndex(
3577 std::ostream& HStoreNamedGeneric::PrintDataTo(
3578 std::ostream& os) const { // NOLINT
3579 Handle<String> n = Handle<String>::cast(name());
3580 return os << NameOf(object()) << "." << n->ToCString().get() << " = "
3585 std::ostream& HStoreNamedField::PrintDataTo(std::ostream& os) const { // NOLINT
3586 os << NameOf(object()) << access_ << " = " << NameOf(value());
3587 if (NeedsWriteBarrier()) os << " (write-barrier)";
3588 if (has_transition()) os << " (transition map " << *transition_map() << ")";
3593 std::ostream& HStoreKeyed::PrintDataTo(std::ostream& os) const { // NOLINT
3594 if (!is_external()) {
3595 os << NameOf(elements());
3597 DCHECK(elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
3598 elements_kind() <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND);
3599 os << NameOf(elements()) << "." << ElementsKindToString(elements_kind());
3602 os << "[" << NameOf(key());
3603 if (IsDehoisted()) os << " + " << base_offset();
3604 return os << "] = " << NameOf(value());
3608 std::ostream& HStoreKeyedGeneric::PrintDataTo(
3609 std::ostream& os) const { // NOLINT
3610 return os << NameOf(object()) << "[" << NameOf(key())
3611 << "] = " << NameOf(value());
3615 std::ostream& HTransitionElementsKind::PrintDataTo(
3616 std::ostream& os) const { // NOLINT
3617 os << NameOf(object());
3618 ElementsKind from_kind = original_map().handle()->elements_kind();
3619 ElementsKind to_kind = transitioned_map().handle()->elements_kind();
3620 os << " " << *original_map().handle() << " ["
3621 << ElementsAccessor::ForKind(from_kind)->name() << "] -> "
3622 << *transitioned_map().handle() << " ["
3623 << ElementsAccessor::ForKind(to_kind)->name() << "]";
3624 if (IsSimpleMapChangeTransition(from_kind, to_kind)) os << " (simple)";
3629 std::ostream& HLoadGlobalGeneric::PrintDataTo(
3630 std::ostream& os) const { // NOLINT
3631 return os << name()->ToCString().get() << " ";
3635 std::ostream& HInnerAllocatedObject::PrintDataTo(
3636 std::ostream& os) const { // NOLINT
3637 os << NameOf(base_object()) << " offset ";
3638 return offset()->PrintTo(os);
3642 std::ostream& HLoadContextSlot::PrintDataTo(std::ostream& os) const { // NOLINT
3643 return os << NameOf(value()) << "[" << slot_index() << "]";
3647 std::ostream& HStoreContextSlot::PrintDataTo(
3648 std::ostream& os) const { // NOLINT
3649 return os << NameOf(context()) << "[" << slot_index()
3650 << "] = " << NameOf(value());
3654 // Implementation of type inference and type conversions. Calculates
3655 // the inferred type of this instruction based on the input operands.
3657 HType HValue::CalculateInferredType() {
3662 HType HPhi::CalculateInferredType() {
3663 if (OperandCount() == 0) return HType::Tagged();
3664 HType result = OperandAt(0)->type();
3665 for (int i = 1; i < OperandCount(); ++i) {
3666 HType current = OperandAt(i)->type();
3667 result = result.Combine(current);
3673 HType HChange::CalculateInferredType() {
3674 if (from().IsDouble() && to().IsTagged()) return HType::HeapNumber();
3679 Representation HUnaryMathOperation::RepresentationFromInputs() {
3680 if (SupportsFlexibleFloorAndRound() &&
3681 (op_ == kMathFloor || op_ == kMathRound)) {
3682 // Floor and Round always take a double input. The integral result can be
3683 // used as an integer or a double. Infer the representation from the uses.
3684 return Representation::None();
3686 Representation rep = representation();
3687 // If any of the actual input representation is more general than what we
3688 // have so far but not Tagged, use that representation instead.
3689 Representation input_rep = value()->representation();
3690 if (!input_rep.IsTagged()) {
3691 rep = rep.generalize(input_rep);
3697 bool HAllocate::HandleSideEffectDominator(GVNFlag side_effect,
3698 HValue* dominator) {
3699 DCHECK(side_effect == kNewSpacePromotion);
3700 Zone* zone = block()->zone();
3701 Isolate* isolate = block()->isolate();
3702 if (!FLAG_use_allocation_folding) return false;
3704 // Try to fold allocations together with their dominating allocations.
3705 if (!dominator->IsAllocate()) {
3706 if (FLAG_trace_allocation_folding) {
3707 PrintF("#%d (%s) cannot fold into #%d (%s)\n",
3708 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3713 // Check whether we are folding within the same block for local folding.
3714 if (FLAG_use_local_allocation_folding && dominator->block() != block()) {
3715 if (FLAG_trace_allocation_folding) {
3716 PrintF("#%d (%s) cannot fold into #%d (%s), crosses basic blocks\n",
3717 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3722 HAllocate* dominator_allocate = HAllocate::cast(dominator);
3723 HValue* dominator_size = dominator_allocate->size();
3724 HValue* current_size = size();
3726 // TODO(hpayer): Add support for non-constant allocation in dominator.
3727 if (!dominator_size->IsInteger32Constant()) {
3728 if (FLAG_trace_allocation_folding) {
3729 PrintF("#%d (%s) cannot fold into #%d (%s), "
3730 "dynamic allocation size in dominator\n",
3731 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3737 if (!IsFoldable(dominator_allocate)) {
3738 if (FLAG_trace_allocation_folding) {
3739 PrintF("#%d (%s) cannot fold into #%d (%s), different spaces\n", id(),
3740 Mnemonic(), dominator->id(), dominator->Mnemonic());
3745 if (!has_size_upper_bound()) {
3746 if (FLAG_trace_allocation_folding) {
3747 PrintF("#%d (%s) cannot fold into #%d (%s), "
3748 "can't estimate total allocation size\n",
3749 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3754 if (!current_size->IsInteger32Constant()) {
3755 // If it's not constant then it is a size_in_bytes calculation graph
3756 // like this: (const_header_size + const_element_size * size).
3757 DCHECK(current_size->IsInstruction());
3759 HInstruction* current_instr = HInstruction::cast(current_size);
3760 if (!current_instr->Dominates(dominator_allocate)) {
3761 if (FLAG_trace_allocation_folding) {
3762 PrintF("#%d (%s) cannot fold into #%d (%s), dynamic size "
3763 "value does not dominate target allocation\n",
3764 id(), Mnemonic(), dominator_allocate->id(),
3765 dominator_allocate->Mnemonic());
3772 (IsNewSpaceAllocation() && dominator_allocate->IsNewSpaceAllocation()) ||
3773 (IsOldSpaceAllocation() && dominator_allocate->IsOldSpaceAllocation()));
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 = HConstant::CreateAndInsertBefore(
3805 isolate, zone, context(), new_dominator_size, Representation::None(),
3806 dominator_allocate);
3808 HValue* new_dominator_size_constant = HConstant::CreateAndInsertBefore(
3809 isolate, zone, context(), dominator_size_constant,
3810 Representation::Integer32(), dominator_allocate);
3812 // Add old and new size together and insert.
3813 current_size->ChangeRepresentation(Representation::Integer32());
3815 new_dominator_size_value = HAdd::New(
3816 isolate, zone, context(), new_dominator_size_constant, current_size);
3817 new_dominator_size_value->ClearFlag(HValue::kCanOverflow);
3818 new_dominator_size_value->ChangeRepresentation(Representation::Integer32());
3820 new_dominator_size_value->InsertBefore(dominator_allocate);
3823 dominator_allocate->UpdateSize(new_dominator_size_value);
3825 if (MustAllocateDoubleAligned()) {
3826 if (!dominator_allocate->MustAllocateDoubleAligned()) {
3827 dominator_allocate->MakeDoubleAligned();
3831 bool keep_new_space_iterable = FLAG_log_gc || FLAG_heap_stats;
3833 keep_new_space_iterable = keep_new_space_iterable || FLAG_verify_heap;
3836 if (keep_new_space_iterable && dominator_allocate->IsNewSpaceAllocation()) {
3837 dominator_allocate->MakePrefillWithFiller();
3839 // TODO(hpayer): This is a short-term hack to make allocation mementos
3840 // work again in new space.
3841 dominator_allocate->ClearNextMapWord(original_object_size);
3844 dominator_allocate->UpdateClearNextMapWord(MustClearNextMapWord());
3846 // After that replace the dominated allocate instruction.
3847 HInstruction* inner_offset = HConstant::CreateAndInsertBefore(
3848 isolate, zone, context(), dominator_size_constant, Representation::None(),
3851 HInstruction* dominated_allocate_instr = HInnerAllocatedObject::New(
3852 isolate, zone, context(), dominator_allocate, inner_offset, type());
3853 dominated_allocate_instr->InsertBefore(this);
3854 DeleteAndReplaceWith(dominated_allocate_instr);
3855 if (FLAG_trace_allocation_folding) {
3856 PrintF("#%d (%s) folded into #%d (%s)\n",
3857 id(), Mnemonic(), dominator_allocate->id(),
3858 dominator_allocate->Mnemonic());
3864 void HAllocate::UpdateFreeSpaceFiller(int32_t free_space_size) {
3865 DCHECK(filler_free_space_size_ != NULL);
3866 Zone* zone = block()->zone();
3867 // We must explicitly force Smi representation here because on x64 we
3868 // would otherwise automatically choose int32, but the actual store
3869 // requires a Smi-tagged value.
3870 HConstant* new_free_space_size = HConstant::CreateAndInsertBefore(
3871 block()->isolate(), zone, context(),
3872 filler_free_space_size_->value()->GetInteger32Constant() +
3874 Representation::Smi(), filler_free_space_size_);
3875 filler_free_space_size_->UpdateValue(new_free_space_size);
3879 void HAllocate::CreateFreeSpaceFiller(int32_t free_space_size) {
3880 DCHECK(filler_free_space_size_ == NULL);
3881 Isolate* isolate = block()->isolate();
3882 Zone* zone = block()->zone();
3883 HInstruction* free_space_instr =
3884 HInnerAllocatedObject::New(isolate, zone, context(), dominating_allocate_,
3885 dominating_allocate_->size(), type());
3886 free_space_instr->InsertBefore(this);
3887 HConstant* filler_map = HConstant::CreateAndInsertAfter(
3888 zone, Unique<Map>::CreateImmovable(isolate->factory()->free_space_map()),
3889 true, free_space_instr);
3890 HInstruction* store_map =
3891 HStoreNamedField::New(isolate, zone, context(), free_space_instr,
3892 HObjectAccess::ForMap(), filler_map);
3893 store_map->SetFlag(HValue::kHasNoObservableSideEffects);
3894 store_map->InsertAfter(filler_map);
3896 // We must explicitly force Smi representation here because on x64 we
3897 // would otherwise automatically choose int32, but the actual store
3898 // requires a Smi-tagged value.
3899 HConstant* filler_size =
3900 HConstant::CreateAndInsertAfter(isolate, zone, context(), free_space_size,
3901 Representation::Smi(), store_map);
3902 // Must force Smi representation for x64 (see comment above).
3903 HObjectAccess access = HObjectAccess::ForMapAndOffset(
3904 isolate->factory()->free_space_map(), FreeSpace::kSizeOffset,
3905 Representation::Smi());
3906 HStoreNamedField* store_size = HStoreNamedField::New(
3907 isolate, zone, context(), free_space_instr, access, filler_size);
3908 store_size->SetFlag(HValue::kHasNoObservableSideEffects);
3909 store_size->InsertAfter(filler_size);
3910 filler_free_space_size_ = store_size;
3914 void HAllocate::ClearNextMapWord(int offset) {
3915 if (MustClearNextMapWord()) {
3916 Zone* zone = block()->zone();
3917 HObjectAccess access =
3918 HObjectAccess::ForObservableJSObjectOffset(offset);
3919 HStoreNamedField* clear_next_map =
3920 HStoreNamedField::New(block()->isolate(), zone, context(), this, access,
3921 block()->graph()->GetConstant0());
3922 clear_next_map->ClearAllSideEffects();
3923 clear_next_map->InsertAfter(this);
3928 std::ostream& HAllocate::PrintDataTo(std::ostream& os) const { // NOLINT
3929 os << NameOf(size()) << " (";
3930 if (IsNewSpaceAllocation()) os << "N";
3931 if (IsOldSpaceAllocation()) os << "P";
3932 if (MustAllocateDoubleAligned()) os << "A";
3933 if (MustPrefillWithFiller()) os << "F";
3938 bool HStoreKeyed::TryIncreaseBaseOffset(uint32_t increase_by_value) {
3939 // The base offset is usually simply the size of the array header, except
3940 // with dehoisting adds an addition offset due to a array index key
3941 // manipulation, in which case it becomes (array header size +
3942 // constant-offset-from-key * kPointerSize)
3943 v8::base::internal::CheckedNumeric<uint32_t> addition_result = base_offset_;
3944 addition_result += increase_by_value;
3945 if (!addition_result.IsValid()) return false;
3946 base_offset_ = addition_result.ValueOrDie();
3951 bool HStoreKeyed::NeedsCanonicalization() {
3952 switch (value()->opcode()) {
3954 ElementsKind load_kind = HLoadKeyed::cast(value())->elements_kind();
3955 return IsExternalFloatOrDoubleElementsKind(load_kind) ||
3956 IsFixedFloatElementsKind(load_kind);
3959 Representation from = HChange::cast(value())->from();
3960 return from.IsTagged() || from.IsHeapObject();
3962 case kLoadNamedField:
3964 // Better safe than sorry...
3973 #define H_CONSTANT_INT(val) \
3974 HConstant::New(isolate, zone, context, static_cast<int32_t>(val))
3975 #define H_CONSTANT_DOUBLE(val) \
3976 HConstant::New(isolate, zone, context, static_cast<double>(val))
3978 #define DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HInstr, op) \
3979 HInstruction* HInstr::New(Isolate* isolate, Zone* zone, HValue* context, \
3980 HValue* left, HValue* right, Strength strength) { \
3981 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) { \
3982 HConstant* c_left = HConstant::cast(left); \
3983 HConstant* c_right = HConstant::cast(right); \
3984 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) { \
3985 double double_res = c_left->DoubleValue() op c_right->DoubleValue(); \
3986 if (IsInt32Double(double_res)) { \
3987 return H_CONSTANT_INT(double_res); \
3989 return H_CONSTANT_DOUBLE(double_res); \
3992 return new (zone) HInstr(context, left, right, strength); \
3996 DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HAdd, +)
3997 DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HMul, *)
3998 DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HSub, -)
4000 #undef DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR
4003 HInstruction* HStringAdd::New(Isolate* isolate, Zone* zone, HValue* context,
4004 HValue* left, HValue* right, Strength strength,
4005 PretenureFlag pretenure_flag,
4006 StringAddFlags flags,
4007 Handle<AllocationSite> allocation_site) {
4008 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4009 HConstant* c_right = HConstant::cast(right);
4010 HConstant* c_left = HConstant::cast(left);
4011 if (c_left->HasStringValue() && c_right->HasStringValue()) {
4012 Handle<String> left_string = c_left->StringValue();
4013 Handle<String> right_string = c_right->StringValue();
4014 // Prevent possible exception by invalid string length.
4015 if (left_string->length() + right_string->length() < String::kMaxLength) {
4016 MaybeHandle<String> concat = isolate->factory()->NewConsString(
4017 c_left->StringValue(), c_right->StringValue());
4018 return HConstant::New(isolate, zone, context, concat.ToHandleChecked());
4022 return new (zone) HStringAdd(context, left, right, strength, pretenure_flag,
4023 flags, allocation_site);
4027 std::ostream& HStringAdd::PrintDataTo(std::ostream& os) const { // NOLINT
4028 if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_BOTH) {
4030 } else if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_LEFT) {
4032 } else if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_RIGHT) {
4033 os << "_CheckRight";
4035 HBinaryOperation::PrintDataTo(os);
4037 if (pretenure_flag() == NOT_TENURED)
4039 else if (pretenure_flag() == TENURED)
4045 HInstruction* HStringCharFromCode::New(Isolate* isolate, Zone* zone,
4046 HValue* context, HValue* char_code) {
4047 if (FLAG_fold_constants && char_code->IsConstant()) {
4048 HConstant* c_code = HConstant::cast(char_code);
4049 if (c_code->HasNumberValue()) {
4050 if (std::isfinite(c_code->DoubleValue())) {
4051 uint32_t code = c_code->NumberValueAsInteger32() & 0xffff;
4052 return HConstant::New(
4053 isolate, zone, context,
4054 isolate->factory()->LookupSingleCharacterStringFromCode(code));
4056 return HConstant::New(isolate, zone, context,
4057 isolate->factory()->empty_string());
4060 return new(zone) HStringCharFromCode(context, char_code);
4064 HInstruction* HUnaryMathOperation::New(Isolate* isolate, Zone* zone,
4065 HValue* context, HValue* value,
4066 BuiltinFunctionId op) {
4068 if (!FLAG_fold_constants) break;
4069 if (!value->IsConstant()) break;
4070 HConstant* constant = HConstant::cast(value);
4071 if (!constant->HasNumberValue()) break;
4072 double d = constant->DoubleValue();
4073 if (std::isnan(d)) { // NaN poisons everything.
4074 return H_CONSTANT_DOUBLE(std::numeric_limits<double>::quiet_NaN());
4076 if (std::isinf(d)) { // +Infinity and -Infinity.
4079 return H_CONSTANT_DOUBLE((d > 0.0) ? d : 0.0);
4082 return H_CONSTANT_DOUBLE(
4083 (d > 0.0) ? d : std::numeric_limits<double>::quiet_NaN());
4086 return H_CONSTANT_DOUBLE((d > 0.0) ? d : -d);
4090 return H_CONSTANT_DOUBLE(d);
4092 return H_CONSTANT_INT(32);
4100 return H_CONSTANT_DOUBLE(fast_exp(d));
4102 return H_CONSTANT_DOUBLE(std::log(d));
4104 return H_CONSTANT_DOUBLE(fast_sqrt(d));
4106 return H_CONSTANT_DOUBLE(power_double_double(d, 0.5));
4108 return H_CONSTANT_DOUBLE((d >= 0.0) ? d + 0.0 : -d);
4110 // -0.5 .. -0.0 round to -0.0.
4111 if ((d >= -0.5 && Double(d).Sign() < 0)) return H_CONSTANT_DOUBLE(-0.0);
4112 // Doubles are represented as Significant * 2 ^ Exponent. If the
4113 // Exponent is not negative, the double value is already an integer.
4114 if (Double(d).Exponent() >= 0) return H_CONSTANT_DOUBLE(d);
4115 return H_CONSTANT_DOUBLE(Floor(d + 0.5));
4117 return H_CONSTANT_DOUBLE(static_cast<double>(static_cast<float>(d)));
4119 return H_CONSTANT_DOUBLE(Floor(d));
4121 uint32_t i = DoubleToUint32(d);
4122 return H_CONSTANT_INT(base::bits::CountLeadingZeros32(i));
4129 return new(zone) HUnaryMathOperation(context, value, op);
4133 Representation HUnaryMathOperation::RepresentationFromUses() {
4134 if (op_ != kMathFloor && op_ != kMathRound) {
4135 return HValue::RepresentationFromUses();
4138 // The instruction can have an int32 or double output. Prefer a double
4139 // representation if there are double uses.
4140 bool use_double = false;
4142 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4143 HValue* use = it.value();
4144 int use_index = it.index();
4145 Representation rep_observed = use->observed_input_representation(use_index);
4146 Representation rep_required = use->RequiredInputRepresentation(use_index);
4147 use_double |= (rep_observed.IsDouble() || rep_required.IsDouble());
4148 if (use_double && !FLAG_trace_representation) {
4149 // Having seen one double is enough.
4152 if (FLAG_trace_representation) {
4153 if (!rep_required.IsDouble() || rep_observed.IsDouble()) {
4154 PrintF("#%d %s is used by #%d %s as %s%s\n",
4155 id(), Mnemonic(), use->id(),
4156 use->Mnemonic(), rep_observed.Mnemonic(),
4157 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
4159 PrintF("#%d %s is required by #%d %s as %s%s\n",
4160 id(), Mnemonic(), use->id(),
4161 use->Mnemonic(), rep_required.Mnemonic(),
4162 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
4166 return use_double ? Representation::Double() : Representation::Integer32();
4170 HInstruction* HPower::New(Isolate* isolate, Zone* zone, HValue* context,
4171 HValue* left, HValue* right) {
4172 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4173 HConstant* c_left = HConstant::cast(left);
4174 HConstant* c_right = HConstant::cast(right);
4175 if (c_left->HasNumberValue() && c_right->HasNumberValue()) {
4176 double result = power_helper(c_left->DoubleValue(),
4177 c_right->DoubleValue());
4178 return H_CONSTANT_DOUBLE(std::isnan(result)
4179 ? std::numeric_limits<double>::quiet_NaN()
4183 return new(zone) HPower(left, right);
4187 HInstruction* HMathMinMax::New(Isolate* isolate, Zone* zone, HValue* context,
4188 HValue* left, HValue* right, Operation op) {
4189 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4190 HConstant* c_left = HConstant::cast(left);
4191 HConstant* c_right = HConstant::cast(right);
4192 if (c_left->HasNumberValue() && c_right->HasNumberValue()) {
4193 double d_left = c_left->DoubleValue();
4194 double d_right = c_right->DoubleValue();
4195 if (op == kMathMin) {
4196 if (d_left > d_right) return H_CONSTANT_DOUBLE(d_right);
4197 if (d_left < d_right) return H_CONSTANT_DOUBLE(d_left);
4198 if (d_left == d_right) {
4199 // Handle +0 and -0.
4200 return H_CONSTANT_DOUBLE((Double(d_left).Sign() == -1) ? d_left
4204 if (d_left < d_right) return H_CONSTANT_DOUBLE(d_right);
4205 if (d_left > d_right) return H_CONSTANT_DOUBLE(d_left);
4206 if (d_left == d_right) {
4207 // Handle +0 and -0.
4208 return H_CONSTANT_DOUBLE((Double(d_left).Sign() == -1) ? d_right
4212 // All comparisons failed, must be NaN.
4213 return H_CONSTANT_DOUBLE(std::numeric_limits<double>::quiet_NaN());
4216 return new(zone) HMathMinMax(context, left, right, op);
4220 HInstruction* HMod::New(Isolate* isolate, Zone* zone, HValue* context,
4221 HValue* left, HValue* right, Strength strength) {
4222 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4223 HConstant* c_left = HConstant::cast(left);
4224 HConstant* c_right = HConstant::cast(right);
4225 if (c_left->HasInteger32Value() && c_right->HasInteger32Value()) {
4226 int32_t dividend = c_left->Integer32Value();
4227 int32_t divisor = c_right->Integer32Value();
4228 if (dividend == kMinInt && divisor == -1) {
4229 return H_CONSTANT_DOUBLE(-0.0);
4232 int32_t res = dividend % divisor;
4233 if ((res == 0) && (dividend < 0)) {
4234 return H_CONSTANT_DOUBLE(-0.0);
4236 return H_CONSTANT_INT(res);
4240 return new (zone) HMod(context, left, right, strength);
4244 HInstruction* HDiv::New(Isolate* isolate, Zone* zone, HValue* context,
4245 HValue* left, HValue* right, Strength strength) {
4246 // If left and right are constant values, try to return a constant value.
4247 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4248 HConstant* c_left = HConstant::cast(left);
4249 HConstant* c_right = HConstant::cast(right);
4250 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4251 if (c_right->DoubleValue() != 0) {
4252 double double_res = c_left->DoubleValue() / c_right->DoubleValue();
4253 if (IsInt32Double(double_res)) {
4254 return H_CONSTANT_INT(double_res);
4256 return H_CONSTANT_DOUBLE(double_res);
4258 int sign = Double(c_left->DoubleValue()).Sign() *
4259 Double(c_right->DoubleValue()).Sign(); // Right could be -0.
4260 return H_CONSTANT_DOUBLE(sign * V8_INFINITY);
4264 return new (zone) HDiv(context, left, right, strength);
4268 HInstruction* HBitwise::New(Isolate* isolate, Zone* zone, HValue* context,
4269 Token::Value op, HValue* left, HValue* right,
4270 Strength strength) {
4271 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4272 HConstant* c_left = HConstant::cast(left);
4273 HConstant* c_right = HConstant::cast(right);
4274 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4276 int32_t v_left = c_left->NumberValueAsInteger32();
4277 int32_t v_right = c_right->NumberValueAsInteger32();
4279 case Token::BIT_XOR:
4280 result = v_left ^ v_right;
4282 case Token::BIT_AND:
4283 result = v_left & v_right;
4286 result = v_left | v_right;
4289 result = 0; // Please the compiler.
4292 return H_CONSTANT_INT(result);
4295 return new (zone) HBitwise(context, op, left, right, strength);
4299 #define DEFINE_NEW_H_BITWISE_INSTR(HInstr, result) \
4300 HInstruction* HInstr::New(Isolate* isolate, Zone* zone, HValue* context, \
4301 HValue* left, HValue* right, Strength strength) { \
4302 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) { \
4303 HConstant* c_left = HConstant::cast(left); \
4304 HConstant* c_right = HConstant::cast(right); \
4305 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) { \
4306 return H_CONSTANT_INT(result); \
4309 return new (zone) HInstr(context, left, right, strength); \
4313 DEFINE_NEW_H_BITWISE_INSTR(HSar,
4314 c_left->NumberValueAsInteger32() >> (c_right->NumberValueAsInteger32() & 0x1f))
4315 DEFINE_NEW_H_BITWISE_INSTR(HShl,
4316 c_left->NumberValueAsInteger32() << (c_right->NumberValueAsInteger32() & 0x1f))
4318 #undef DEFINE_NEW_H_BITWISE_INSTR
4321 HInstruction* HShr::New(Isolate* isolate, Zone* zone, HValue* context,
4322 HValue* left, HValue* right, Strength strength) {
4323 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4324 HConstant* c_left = HConstant::cast(left);
4325 HConstant* c_right = HConstant::cast(right);
4326 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4327 int32_t left_val = c_left->NumberValueAsInteger32();
4328 int32_t right_val = c_right->NumberValueAsInteger32() & 0x1f;
4329 if ((right_val == 0) && (left_val < 0)) {
4330 return H_CONSTANT_DOUBLE(static_cast<uint32_t>(left_val));
4332 return H_CONSTANT_INT(static_cast<uint32_t>(left_val) >> right_val);
4335 return new (zone) HShr(context, left, right, strength);
4339 HInstruction* HSeqStringGetChar::New(Isolate* isolate, Zone* zone,
4340 HValue* context, String::Encoding encoding,
4341 HValue* string, HValue* index) {
4342 if (FLAG_fold_constants && string->IsConstant() && index->IsConstant()) {
4343 HConstant* c_string = HConstant::cast(string);
4344 HConstant* c_index = HConstant::cast(index);
4345 if (c_string->HasStringValue() && c_index->HasInteger32Value()) {
4346 Handle<String> s = c_string->StringValue();
4347 int32_t i = c_index->Integer32Value();
4349 DCHECK_LT(i, s->length());
4350 return H_CONSTANT_INT(s->Get(i));
4353 return new(zone) HSeqStringGetChar(encoding, string, index);
4357 #undef H_CONSTANT_INT
4358 #undef H_CONSTANT_DOUBLE
4361 std::ostream& HBitwise::PrintDataTo(std::ostream& os) const { // NOLINT
4362 os << Token::Name(op_) << " ";
4363 return HBitwiseBinaryOperation::PrintDataTo(os);
4367 void HPhi::SimplifyConstantInputs() {
4368 // Convert constant inputs to integers when all uses are truncating.
4369 // This must happen before representation inference takes place.
4370 if (!CheckUsesForFlag(kTruncatingToInt32)) return;
4371 for (int i = 0; i < OperandCount(); ++i) {
4372 if (!OperandAt(i)->IsConstant()) return;
4374 HGraph* graph = block()->graph();
4375 for (int i = 0; i < OperandCount(); ++i) {
4376 HConstant* operand = HConstant::cast(OperandAt(i));
4377 if (operand->HasInteger32Value()) {
4379 } else if (operand->HasDoubleValue()) {
4380 HConstant* integer_input = HConstant::New(
4381 graph->isolate(), graph->zone(), graph->GetInvalidContext(),
4382 DoubleToInt32(operand->DoubleValue()));
4383 integer_input->InsertAfter(operand);
4384 SetOperandAt(i, integer_input);
4385 } else if (operand->HasBooleanValue()) {
4386 SetOperandAt(i, operand->BooleanValue() ? graph->GetConstant1()
4387 : graph->GetConstant0());
4388 } else if (operand->ImmortalImmovable()) {
4389 SetOperandAt(i, graph->GetConstant0());
4392 // Overwrite observed input representations because they are likely Tagged.
4393 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4394 HValue* use = it.value();
4395 if (use->IsBinaryOperation()) {
4396 HBinaryOperation::cast(use)->set_observed_input_representation(
4397 it.index(), Representation::Smi());
4403 void HPhi::InferRepresentation(HInferRepresentationPhase* h_infer) {
4404 DCHECK(CheckFlag(kFlexibleRepresentation));
4405 Representation new_rep = RepresentationFromUses();
4406 UpdateRepresentation(new_rep, h_infer, "uses");
4407 new_rep = RepresentationFromInputs();
4408 UpdateRepresentation(new_rep, h_infer, "inputs");
4409 new_rep = RepresentationFromUseRequirements();
4410 UpdateRepresentation(new_rep, h_infer, "use requirements");
4414 Representation HPhi::RepresentationFromInputs() {
4415 bool has_type_feedback =
4416 smi_non_phi_uses() + int32_non_phi_uses() + double_non_phi_uses() > 0;
4417 Representation r = representation();
4418 for (int i = 0; i < OperandCount(); ++i) {
4419 // Ignore conservative Tagged assumption of parameters if we have
4420 // reason to believe that it's too conservative.
4421 if (has_type_feedback && OperandAt(i)->IsParameter()) continue;
4423 r = r.generalize(OperandAt(i)->KnownOptimalRepresentation());
4429 // Returns a representation if all uses agree on the same representation.
4430 // Integer32 is also returned when some uses are Smi but others are Integer32.
4431 Representation HValue::RepresentationFromUseRequirements() {
4432 Representation rep = Representation::None();
4433 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4434 // Ignore the use requirement from never run code
4435 if (it.value()->block()->IsUnreachable()) continue;
4437 // We check for observed_input_representation elsewhere.
4438 Representation use_rep =
4439 it.value()->RequiredInputRepresentation(it.index());
4444 if (use_rep.IsNone() || rep.Equals(use_rep)) continue;
4445 if (rep.generalize(use_rep).IsInteger32()) {
4446 rep = Representation::Integer32();
4449 return Representation::None();
4455 bool HValue::HasNonSmiUse() {
4456 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4457 // We check for observed_input_representation elsewhere.
4458 Representation use_rep =
4459 it.value()->RequiredInputRepresentation(it.index());
4460 if (!use_rep.IsNone() &&
4462 !use_rep.IsTagged()) {
4470 // Node-specific verification code is only included in debug mode.
4473 void HPhi::Verify() {
4474 DCHECK(OperandCount() == block()->predecessors()->length());
4475 for (int i = 0; i < OperandCount(); ++i) {
4476 HValue* value = OperandAt(i);
4477 HBasicBlock* defining_block = value->block();
4478 HBasicBlock* predecessor_block = block()->predecessors()->at(i);
4479 DCHECK(defining_block == predecessor_block ||
4480 defining_block->Dominates(predecessor_block));
4485 void HSimulate::Verify() {
4486 HInstruction::Verify();
4487 DCHECK(HasAstId() || next()->IsEnterInlined());
4491 void HCheckHeapObject::Verify() {
4492 HInstruction::Verify();
4493 DCHECK(HasNoUses());
4497 void HCheckValue::Verify() {
4498 HInstruction::Verify();
4499 DCHECK(HasNoUses());
4505 HObjectAccess HObjectAccess::ForFixedArrayHeader(int offset) {
4506 DCHECK(offset >= 0);
4507 DCHECK(offset < FixedArray::kHeaderSize);
4508 if (offset == FixedArray::kLengthOffset) return ForFixedArrayLength();
4509 return HObjectAccess(kInobject, offset);
4513 HObjectAccess HObjectAccess::ForMapAndOffset(Handle<Map> map, int offset,
4514 Representation representation) {
4515 DCHECK(offset >= 0);
4516 Portion portion = kInobject;
4518 if (offset == JSObject::kElementsOffset) {
4519 portion = kElementsPointer;
4520 } else if (offset == JSObject::kMapOffset) {
4523 bool existing_inobject_property = true;
4524 if (!map.is_null()) {
4525 existing_inobject_property = (offset <
4526 map->instance_size() - map->unused_property_fields() * kPointerSize);
4528 return HObjectAccess(portion, offset, representation, Handle<String>::null(),
4529 false, existing_inobject_property);
4533 HObjectAccess HObjectAccess::ForAllocationSiteOffset(int offset) {
4535 case AllocationSite::kTransitionInfoOffset:
4536 return HObjectAccess(kInobject, offset, Representation::Tagged());
4537 case AllocationSite::kNestedSiteOffset:
4538 return HObjectAccess(kInobject, offset, Representation::Tagged());
4539 case AllocationSite::kPretenureDataOffset:
4540 return HObjectAccess(kInobject, offset, Representation::Smi());
4541 case AllocationSite::kPretenureCreateCountOffset:
4542 return HObjectAccess(kInobject, offset, Representation::Smi());
4543 case AllocationSite::kDependentCodeOffset:
4544 return HObjectAccess(kInobject, offset, Representation::Tagged());
4545 case AllocationSite::kWeakNextOffset:
4546 return HObjectAccess(kInobject, offset, Representation::Tagged());
4550 return HObjectAccess(kInobject, offset);
4554 HObjectAccess HObjectAccess::ForContextSlot(int index) {
4556 Portion portion = kInobject;
4557 int offset = Context::kHeaderSize + index * kPointerSize;
4558 DCHECK_EQ(offset, Context::SlotOffset(index) + kHeapObjectTag);
4559 return HObjectAccess(portion, offset, Representation::Tagged());
4563 HObjectAccess HObjectAccess::ForScriptContext(int index) {
4565 Portion portion = kInobject;
4566 int offset = ScriptContextTable::GetContextOffset(index);
4567 return HObjectAccess(portion, offset, Representation::Tagged());
4571 HObjectAccess HObjectAccess::ForJSArrayOffset(int offset) {
4572 DCHECK(offset >= 0);
4573 Portion portion = kInobject;
4575 if (offset == JSObject::kElementsOffset) {
4576 portion = kElementsPointer;
4577 } else if (offset == JSArray::kLengthOffset) {
4578 portion = kArrayLengths;
4579 } else if (offset == JSObject::kMapOffset) {
4582 return HObjectAccess(portion, offset);
4586 HObjectAccess HObjectAccess::ForBackingStoreOffset(int offset,
4587 Representation representation) {
4588 DCHECK(offset >= 0);
4589 return HObjectAccess(kBackingStore, offset, representation,
4590 Handle<String>::null(), false, false);
4594 HObjectAccess HObjectAccess::ForField(Handle<Map> map, int index,
4595 Representation representation,
4596 Handle<String> name) {
4598 // Negative property indices are in-object properties, indexed
4599 // from the end of the fixed part of the object.
4600 int offset = (index * kPointerSize) + map->instance_size();
4601 return HObjectAccess(kInobject, offset, representation, name, false, true);
4603 // Non-negative property indices are in the properties array.
4604 int offset = (index * kPointerSize) + FixedArray::kHeaderSize;
4605 return HObjectAccess(kBackingStore, offset, representation, name,
4611 void HObjectAccess::SetGVNFlags(HValue *instr, PropertyAccessType access_type) {
4612 // set the appropriate GVN flags for a given load or store instruction
4613 if (access_type == STORE) {
4614 // track dominating allocations in order to eliminate write barriers
4615 instr->SetDependsOnFlag(::v8::internal::kNewSpacePromotion);
4616 instr->SetFlag(HValue::kTrackSideEffectDominators);
4618 // try to GVN loads, but don't hoist above map changes
4619 instr->SetFlag(HValue::kUseGVN);
4620 instr->SetDependsOnFlag(::v8::internal::kMaps);
4623 switch (portion()) {
4625 if (access_type == STORE) {
4626 instr->SetChangesFlag(::v8::internal::kArrayLengths);
4628 instr->SetDependsOnFlag(::v8::internal::kArrayLengths);
4631 case kStringLengths:
4632 if (access_type == STORE) {
4633 instr->SetChangesFlag(::v8::internal::kStringLengths);
4635 instr->SetDependsOnFlag(::v8::internal::kStringLengths);
4639 if (access_type == STORE) {
4640 instr->SetChangesFlag(::v8::internal::kInobjectFields);
4642 instr->SetDependsOnFlag(::v8::internal::kInobjectFields);
4646 if (access_type == STORE) {
4647 instr->SetChangesFlag(::v8::internal::kDoubleFields);
4649 instr->SetDependsOnFlag(::v8::internal::kDoubleFields);
4653 if (access_type == STORE) {
4654 instr->SetChangesFlag(::v8::internal::kBackingStoreFields);
4656 instr->SetDependsOnFlag(::v8::internal::kBackingStoreFields);
4659 case kElementsPointer:
4660 if (access_type == STORE) {
4661 instr->SetChangesFlag(::v8::internal::kElementsPointer);
4663 instr->SetDependsOnFlag(::v8::internal::kElementsPointer);
4667 if (access_type == STORE) {
4668 instr->SetChangesFlag(::v8::internal::kMaps);
4670 instr->SetDependsOnFlag(::v8::internal::kMaps);
4673 case kExternalMemory:
4674 if (access_type == STORE) {
4675 instr->SetChangesFlag(::v8::internal::kExternalMemory);
4677 instr->SetDependsOnFlag(::v8::internal::kExternalMemory);
4684 std::ostream& operator<<(std::ostream& os, const HObjectAccess& access) {
4687 switch (access.portion()) {
4688 case HObjectAccess::kArrayLengths:
4689 case HObjectAccess::kStringLengths:
4692 case HObjectAccess::kElementsPointer:
4695 case HObjectAccess::kMaps:
4698 case HObjectAccess::kDouble: // fall through
4699 case HObjectAccess::kInobject:
4700 if (!access.name().is_null()) {
4701 os << Handle<String>::cast(access.name())->ToCString().get();
4703 os << "[in-object]";
4705 case HObjectAccess::kBackingStore:
4706 if (!access.name().is_null()) {
4707 os << Handle<String>::cast(access.name())->ToCString().get();
4709 os << "[backing-store]";
4711 case HObjectAccess::kExternalMemory:
4712 os << "[external-memory]";
4716 return os << "@" << access.offset();
4719 } // namespace internal