1/*
2 * Copyright (C) 2013-2018 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 "DFGAbstractHeap.h"
31#include <wtf/HashMap.h>
32#include <wtf/HashSet.h>
33#include <wtf/PrintStream.h>
34
35namespace JSC { namespace DFG {
36
37class Graph;
38struct Node;
39
40// FIXME: If we ever want to compare if two nodes clobber each other, we should
41// have a SmallClobberSet, which just keeps an array of the AbstractHeaps and
42// satisfies overlaps() requests by looping over all of them. This will probably
43// be faster than a full HashMap in a lot of cases. Or, maybe, we could have
44// ClobberSet be smart and use a vector so long as it was small.
45
46class ClobberSet {
47public:
48 ClobberSet();
49 ~ClobberSet();
50
51 bool isEmpty() const { return m_clobbers.isEmpty(); }
52
53 void add(AbstractHeap);
54 void addAll(const ClobberSet&);
55 bool overlaps(AbstractHeap) const;
56 void clear();
57
58 // Calls useful for debugging the ClobberSet.
59 // Do not call for non debugging purpose. Otherwise, you must handle DOMState hierarchy carefully.
60
61 HashSet<AbstractHeap> direct() const;
62 HashSet<AbstractHeap> super() const;
63
64 void dump(PrintStream&) const;
65
66private:
67 bool contains(AbstractHeap) const;
68
69 HashSet<AbstractHeap> setOf(bool direct) const;
70
71 // Maps heap to:
72 // true --> it's a direct clobber
73 // false --> it's just a supertype of a direct clobber
74 HashMap<AbstractHeap, bool> m_clobbers;
75};
76
77class ClobberSetAdd {
78public:
79 ClobberSetAdd(ClobberSet& set)
80 : m_set(set)
81 {
82 }
83
84 void operator()(AbstractHeap heap) const
85 {
86 m_set.add(heap);
87 }
88private:
89 ClobberSet& m_set;
90};
91
92class ClobberSetOverlaps {
93public:
94 ClobberSetOverlaps(const ClobberSet& set)
95 : m_set(set)
96 , m_result(false)
97 {
98 }
99
100 void operator()(AbstractHeap heap) const
101 {
102 m_result |= m_set.overlaps(heap);
103 }
104
105 bool result() const { return m_result; }
106
107private:
108 const ClobberSet& m_set;
109 mutable bool m_result;
110};
111
112void addReads(Graph&, Node*, ClobberSet&);
113void addWrites(Graph&, Node*, ClobberSet&);
114void addReadsAndWrites(Graph&, Node*, ClobberSet& reads, ClobberSet& writes);
115
116ClobberSet writeSet(Graph&, Node*);
117
118bool readsOverlap(Graph&, Node*, ClobberSet&);
119bool writesOverlap(Graph&, Node*, ClobberSet&);
120
121} } // namespace JSC::DFG
122
123#endif // ENABLE(DFG_JIT)
124