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
34namespace JSC {
35
36class VM;
37
38struct 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
62struct 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
70namespace WTF {
71
72template<typename T> struct DefaultHash;
73template<> struct DefaultHash<JSC::BasicBlockKey> {
74 typedef JSC::BasicBlockKeyHash Hash;
75};
76
77template<typename T> struct HashTraits;
78template<> struct HashTraits<JSC::BasicBlockKey> : SimpleClassHashTraits<JSC::BasicBlockKey> {
79 static const bool emptyValueIsZero = false;
80};
81
82} // namespace WTF
83
84namespace JSC {
85
86struct BasicBlockRange {
87 int m_startOffset;
88 int m_endOffset;
89 bool m_hasExecuted;
90 size_t m_executionCount;
91};
92
93class ControlFlowProfiler {
94 WTF_MAKE_FAST_ALLOCATED;
95public:
96 ControlFlowProfiler();
97 ~ControlFlowProfiler();
98 BasicBlockLocation* getBasicBlockLocation(intptr_t sourceID, int startOffset, int endOffset);
99 JS_EXPORT_PRIVATE void dumpData() const;
100 Vector<BasicBlockRange> getBasicBlocksForSourceID(intptr_t sourceID, VM&) const;
101 BasicBlockLocation* dummyBasicBlock() { return &m_dummyBasicBlock; }
102 JS_EXPORT_PRIVATE bool hasBasicBlockAtTextOffsetBeenExecuted(int, intptr_t, VM&); // This function exists for testing.
103 JS_EXPORT_PRIVATE size_t basicBlockExecutionCountAtTextOffset(int, intptr_t, VM&); // This function exists for testing.
104
105private:
106 typedef HashMap<BasicBlockKey, BasicBlockLocation*> BlockLocationCache;
107 typedef HashMap<intptr_t, BlockLocationCache> SourceIDBuckets;
108
109 SourceIDBuckets m_sourceIDBuckets;
110 BasicBlockLocation m_dummyBasicBlock;
111};
112
113} // namespace JSC
114
115#endif // ControlFlowProfiler_h
116