1/*
2 * Copyright (C) 2018 Yusuke Suzuki <[email protected]>.
3 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "CallLinkStatus.h"
30#include "CodeOrigin.h"
31#include "ConcurrentJSLock.h"
32#include "ICStatusMap.h"
33#include "InByIdVariant.h"
34#include "StubInfoSummary.h"
35
36namespace JSC {
37
38class AccessCase;
39class CodeBlock;
40class StructureStubInfo;
41
42class InByIdStatus {
43 WTF_MAKE_FAST_ALLOCATED;
44public:
45 enum State {
46 // It's uncached so we have no information.
47 NoInformation,
48 // It's cached for a simple access to a known object property with
49 // a possible structure chain and a possible specific value.
50 Simple,
51 // It's known to often take slow path.
52 TakesSlowPath,
53 };
54
55 InByIdStatus() = default;
56
57 InByIdStatus(State state, const InByIdVariant& variant = InByIdVariant())
58 : m_state(state)
59 {
60 ASSERT((state == Simple) == variant.isSet());
61 if (variant.isSet())
62 m_variants.append(variant);
63 }
64
65 explicit InByIdStatus(StubInfoSummary summary)
66 {
67 switch (summary) {
68 case StubInfoSummary::NoInformation:
69 m_state = NoInformation;
70 return;
71 case StubInfoSummary::Simple:
72 case StubInfoSummary::MakesCalls:
73 RELEASE_ASSERT_NOT_REACHED();
74 return;
75 case StubInfoSummary::TakesSlowPath:
76 case StubInfoSummary::TakesSlowPathAndMakesCalls:
77 m_state = TakesSlowPath;
78 return;
79 }
80 RELEASE_ASSERT_NOT_REACHED();
81 }
82
83 static InByIdStatus computeFor(CodeBlock*, ICStatusMap&, BytecodeIndex, UniquedStringImpl* uid);
84 static InByIdStatus computeFor(CodeBlock*, ICStatusMap&, BytecodeIndex, UniquedStringImpl* uid, ExitFlag);
85 static InByIdStatus computeFor(CodeBlock* baselineBlock, ICStatusMap& baselineMap, ICStatusContextStack& contextStack, CodeOrigin, UniquedStringImpl* uid);
86
87#if ENABLE(DFG_JIT)
88 static InByIdStatus computeForStubInfo(const ConcurrentJSLocker&, CodeBlock* baselineBlock, StructureStubInfo*, CodeOrigin, UniquedStringImpl* uid);
89#endif
90
91 State state() const { return m_state; }
92
93 bool isSet() const { return m_state != NoInformation; }
94 explicit operator bool() const { return isSet(); }
95 bool isSimple() const { return m_state == Simple; }
96
97 size_t numVariants() const { return m_variants.size(); }
98 const Vector<InByIdVariant, 1>& variants() const { return m_variants; }
99 const InByIdVariant& at(size_t index) const { return m_variants[index]; }
100 const InByIdVariant& operator[](size_t index) const { return at(index); }
101
102 bool takesSlowPath() const { return m_state == TakesSlowPath; }
103
104 void merge(const InByIdStatus&);
105
106 // Attempts to reduce the set of variants to fit the given structure set. This may be approximate.
107 void filter(const StructureSet&);
108
109 void markIfCheap(SlotVisitor&);
110 bool finalize(VM&);
111
112 void dump(PrintStream&) const;
113
114private:
115#if ENABLE(DFG_JIT)
116 static InByIdStatus computeForStubInfoWithoutExitSiteFeedback(const ConcurrentJSLocker&, StructureStubInfo*, UniquedStringImpl* uid);
117#endif
118 bool appendVariant(const InByIdVariant&);
119
120 State m_state { NoInformation };
121 Vector<InByIdVariant, 1> m_variants;
122};
123
124} // namespace JSC
125