1/*
2 * Copyright (C) 2014-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(DFG_JIT)
29
30#include "DFGEdge.h"
31#include "DFGNodeOrigin.h"
32#include <wtf/HashTable.h>
33#include <wtf/PrintStream.h>
34
35namespace JSC { namespace DFG {
36
37struct Node;
38
39// Promoted locations are like heap locations but are meant to be more precise. A heap location is
40// applicable to CSE scenarios, where it makes sense to speak of a location very abstractly. A
41// promoted heap location is for cases where we speak of a specific object and the compiler knows
42// this object's identity - for example, the object allocation has been eliminated and we turned the
43// fields into local variables. Because these two cases have subtly different needs, we use subtly
44// different structures. One of the really significant differences is that promoted locations can be
45// spoken of using either a descriptor which does not refer to any Node*'s or with a heap location,
46// which is a descriptor with a Node* base.
47
48enum PromotedLocationKind {
49 InvalidPromotedLocationKind,
50
51 ActivationScopePLoc,
52 ActivationSymbolTablePLoc,
53 ArgumentCountPLoc,
54 ArgumentPLoc,
55 ArgumentsCalleePLoc,
56 ClosureVarPLoc,
57 PromiseInternalFieldPLoc,
58 FunctionActivationPLoc,
59 FunctionExecutablePLoc,
60 IndexedPropertyPLoc,
61 NamedPropertyPLoc,
62 PublicLengthPLoc,
63 StructurePLoc,
64 VectorLengthPLoc,
65 SpreadPLoc,
66 NewArrayWithSpreadArgumentPLoc,
67 NewArrayBufferPLoc,
68 RegExpObjectRegExpPLoc,
69 RegExpObjectLastIndexPLoc,
70};
71
72class PromotedLocationDescriptor {
73public:
74 PromotedLocationDescriptor(
75 PromotedLocationKind kind = InvalidPromotedLocationKind, unsigned info = 0)
76 : m_kind(kind)
77 , m_info(info)
78 {
79 }
80
81 PromotedLocationDescriptor(WTF::HashTableDeletedValueType)
82 : m_kind(InvalidPromotedLocationKind)
83 , m_info(1)
84 {
85 }
86
87 bool operator!() const { return m_kind == InvalidPromotedLocationKind; }
88
89 explicit operator bool() const { return !!*this; }
90
91 PromotedLocationKind kind() const { return m_kind; }
92 unsigned info() const { return m_info; }
93
94 unsigned imm1() const { return static_cast<uint32_t>(m_kind); }
95 unsigned imm2() const { return static_cast<uint32_t>(m_info); }
96
97 unsigned hash() const
98 {
99 return m_kind + m_info;
100 }
101
102 bool operator==(const PromotedLocationDescriptor& other) const
103 {
104 return m_kind == other.m_kind
105 && m_info == other.m_info;
106 }
107
108 bool operator!=(const PromotedLocationDescriptor& other) const
109 {
110 return !(*this == other);
111 }
112
113 bool isHashTableDeletedValue() const
114 {
115 return m_kind == InvalidPromotedLocationKind && m_info;
116 }
117
118 bool neededForMaterialization() const
119 {
120 switch (kind()) {
121 case NamedPropertyPLoc:
122 case ClosureVarPLoc:
123 case RegExpObjectLastIndexPLoc:
124 return false;
125
126 default:
127 return true;
128 }
129 }
130
131 void dump(PrintStream& out) const;
132
133private:
134 PromotedLocationKind m_kind;
135 unsigned m_info;
136};
137
138struct PromotedLocationDescriptorHash {
139 static unsigned hash(const PromotedLocationDescriptor& key) { return key.hash(); }
140 static bool equal(const PromotedLocationDescriptor& a, const PromotedLocationDescriptor& b) { return a == b; }
141 static constexpr bool safeToCompareToEmptyOrDeleted = true;
142};
143
144class PromotedHeapLocation {
145public:
146 PromotedHeapLocation(
147 PromotedLocationKind kind = InvalidPromotedLocationKind,
148 Node* base = nullptr, unsigned info = 0)
149 : m_base(base)
150 , m_meta(kind, info)
151 {
152 }
153
154 PromotedHeapLocation(
155 PromotedLocationKind kind, Edge base, unsigned info = 0)
156 : PromotedHeapLocation(kind, base.node(), info)
157 {
158 }
159
160 PromotedHeapLocation(Node* base, PromotedLocationDescriptor meta)
161 : m_base(base)
162 , m_meta(meta)
163 {
164 }
165
166 PromotedHeapLocation(WTF::HashTableDeletedValueType)
167 : m_base(nullptr)
168 , m_meta(InvalidPromotedLocationKind, 1)
169 {
170 }
171
172 Node* createHint(Graph&, NodeOrigin, Node* value);
173
174 bool operator!() const { return kind() == InvalidPromotedLocationKind; }
175
176 PromotedLocationKind kind() const { return m_meta.kind(); }
177 Node* base() const { return m_base; }
178 unsigned info() const { return m_meta.info(); }
179 PromotedLocationDescriptor descriptor() const { return m_meta; }
180
181 unsigned hash() const
182 {
183 return m_meta.hash() + WTF::PtrHash<Node*>::hash(m_base);
184 }
185
186 bool operator==(const PromotedHeapLocation& other) const
187 {
188 return m_base == other.m_base
189 && m_meta == other.m_meta;
190 }
191
192 bool isHashTableDeletedValue() const
193 {
194 return m_meta.isHashTableDeletedValue();
195 }
196
197 void dump(PrintStream& out) const;
198
199private:
200 Node* m_base;
201 PromotedLocationDescriptor m_meta;
202};
203
204struct PromotedHeapLocationHash {
205 static unsigned hash(const PromotedHeapLocation& key) { return key.hash(); }
206 static bool equal(const PromotedHeapLocation& a, const PromotedHeapLocation& b) { return a == b; }
207 static constexpr bool safeToCompareToEmptyOrDeleted = true;
208};
209
210} } // namespace JSC::DFG
211
212namespace WTF {
213
214void printInternal(PrintStream&, JSC::DFG::PromotedLocationKind);
215
216template<typename T> struct DefaultHash;
217template<> struct DefaultHash<JSC::DFG::PromotedHeapLocation> {
218 typedef JSC::DFG::PromotedHeapLocationHash Hash;
219};
220
221template<typename T> struct HashTraits;
222template<> struct HashTraits<JSC::DFG::PromotedHeapLocation> : SimpleClassHashTraits<JSC::DFG::PromotedHeapLocation> {
223 static constexpr bool emptyValueIsZero = false;
224};
225
226template<typename T> struct DefaultHash;
227template<> struct DefaultHash<JSC::DFG::PromotedLocationDescriptor> {
228 typedef JSC::DFG::PromotedLocationDescriptorHash Hash;
229};
230
231template<typename T> struct HashTraits;
232template<> struct HashTraits<JSC::DFG::PromotedLocationDescriptor> : SimpleClassHashTraits<JSC::DFG::PromotedLocationDescriptor> {
233 static constexpr bool emptyValueIsZero = false;
234};
235
236} // namespace WTF
237
238#endif // ENABLE(DFG_JIT)
239