]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ControlFlowProfiler.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / ControlFlowProfiler.h
1 /*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Saam Barati. <saambarati1@gmail.com>
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 #ifndef ControlFlowProfiler_h
28 #define ControlFlowProfiler_h
29
30 #include "BasicBlockLocation.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/HashMethod.h>
33
34 namespace JSC {
35
36 class VM;
37
38 struct BasicBlockKey {
39 BasicBlockKey()
40 : m_startOffset(-3)
41 , m_endOffset(-3)
42 { }
43
44 BasicBlockKey(int startOffset, int endOffset)
45 : m_startOffset(startOffset)
46 , m_endOffset(endOffset)
47 { }
48
49 BasicBlockKey(WTF::HashTableDeletedValueType)
50 : m_startOffset(-2)
51 , m_endOffset(-2)
52 { }
53
54 bool isHashTableDeletedValue() const { return m_startOffset == -2 && m_endOffset == -2; }
55 bool operator==(const BasicBlockKey& other) const { return m_startOffset == other.m_startOffset && m_endOffset == other.m_endOffset; }
56 unsigned hash() const { return m_startOffset + m_endOffset + 1; }
57
58 int m_startOffset;
59 int m_endOffset;
60 };
61
62 struct BasicBlockKeyHash {
63 static unsigned hash(const BasicBlockKey& key) { return key.hash(); }
64 static bool equal(const BasicBlockKey& a, const BasicBlockKey& b) { return a == b; }
65 static const bool safeToCompareToEmptyOrDeleted = true;
66 };
67
68 } // namespace JSC
69
70 namespace WTF {
71
72 template<typename T> struct DefaultHash;
73 template<> struct DefaultHash<JSC::BasicBlockKey> {
74 typedef JSC::BasicBlockKeyHash Hash;
75 };
76
77 template<typename T> struct HashTraits;
78 template<> struct HashTraits<JSC::BasicBlockKey> : SimpleClassHashTraits<JSC::BasicBlockKey> {
79 static const bool emptyValueIsZero = false;
80 };
81
82 } // namespace WTF
83
84 namespace JSC {
85
86 struct BasicBlockRange {
87 int m_startOffset;
88 int m_endOffset;
89 bool m_hasExecuted;
90 };
91
92 class ControlFlowProfiler {
93 public:
94 ControlFlowProfiler();
95 ~ControlFlowProfiler();
96 BasicBlockLocation* getBasicBlockLocation(intptr_t sourceID, int startOffset, int endOffset);
97 JS_EXPORT_PRIVATE void dumpData() const;
98 Vector<BasicBlockRange> getBasicBlocksForSourceID(intptr_t sourceID, VM&) const;
99 BasicBlockLocation* dummyBasicBlock() { return &m_dummyBasicBlock; }
100 JS_EXPORT_PRIVATE bool hasBasicBlockAtTextOffsetBeenExecuted(int, intptr_t, VM&); // This function exists for testing.
101
102 private:
103 typedef HashMap<BasicBlockKey, BasicBlockLocation*> BlockLocationCache;
104 typedef HashMap<intptr_t, BlockLocationCache> SourceIDBuckets;
105
106 SourceIDBuckets m_sourceIDBuckets;
107 BasicBlockLocation m_dummyBasicBlock;
108 };
109
110 } // namespace JSC
111
112 #endif // ControlFlowProfiler_h