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#pragma once
28
29#include "BasicBlockLocation.h"
30#include <wtf/HashMap.h>
31
32namespace JSC {
33
34class VM;
35
36struct BasicBlockKey {
37 BasicBlockKey()
38 : m_startOffset(-3)
39 , m_endOffset(-3)
40 { }
41
42 BasicBlockKey(int startOffset, int endOffset)
43 : m_startOffset(startOffset)
44 , m_endOffset(endOffset)
45 { }
46
47 BasicBlockKey(WTF::HashTableDeletedValueType)
48 : m_startOffset(-2)
49 , m_endOffset(-2)
50 { }
51
52 bool isHashTableDeletedValue() const { return m_startOffset == -2 && m_endOffset == -2; }
53 bool operator==(const BasicBlockKey& other) const { return m_startOffset == other.m_startOffset && m_endOffset == other.m_endOffset; }
54 unsigned hash() const { return m_startOffset + m_endOffset + 1; }
55
56 int m_startOffset;
57 int m_endOffset;
58};
59
60struct BasicBlockKeyHash {
61 static unsigned hash(const BasicBlockKey& key) { return key.hash(); }
62 static bool equal(const BasicBlockKey& a, const BasicBlockKey& b) { return a == b; }
63 static const bool safeToCompareToEmptyOrDeleted = true;
64};
65
66} // namespace JSC
67
68namespace WTF {
69
70template<typename T> struct DefaultHash;
71template<> struct DefaultHash<JSC::BasicBlockKey> {
72 typedef JSC::BasicBlockKeyHash Hash;
73};
74
75template<typename T> struct HashTraits;
76template<> struct HashTraits<JSC::BasicBlockKey> : SimpleClassHashTraits<JSC::BasicBlockKey> {
77 static const bool emptyValueIsZero = false;
78};
79
80} // namespace WTF
81
82namespace JSC {
83
84struct BasicBlockRange {
85 int m_startOffset;
86 int m_endOffset;
87 bool m_hasExecuted;
88 size_t m_executionCount;
89};
90
91class ControlFlowProfiler {
92 WTF_MAKE_FAST_ALLOCATED;
93public:
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 JS_EXPORT_PRIVATE size_t basicBlockExecutionCountAtTextOffset(int, intptr_t, VM&); // This function exists for testing.
102
103private:
104 typedef HashMap<BasicBlockKey, BasicBlockLocation*> BlockLocationCache;
105 typedef HashMap<intptr_t, BlockLocationCache> SourceIDBuckets;
106
107 SourceIDBuckets m_sourceIDBuckets;
108 BasicBlockLocation m_dummyBasicBlock;
109};
110
111} // namespace JSC
112