2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Saam Barati. <saambarati1@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef ControlFlowProfiler_h
28 #define ControlFlowProfiler_h
30 #include "BasicBlockLocation.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/HashMethod.h>
38 struct BasicBlockKey
{
44 BasicBlockKey(int startOffset
, int endOffset
)
45 : m_startOffset(startOffset
)
46 , m_endOffset(endOffset
)
49 BasicBlockKey(WTF::HashTableDeletedValueType
)
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; }
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;
72 template<typename T
> struct DefaultHash
;
73 template<> struct DefaultHash
<JSC::BasicBlockKey
> {
74 typedef JSC::BasicBlockKeyHash Hash
;
77 template<typename T
> struct HashTraits
;
78 template<> struct HashTraits
<JSC::BasicBlockKey
> : SimpleClassHashTraits
<JSC::BasicBlockKey
> {
79 static const bool emptyValueIsZero
= false;
86 struct BasicBlockRange
{
92 class ControlFlowProfiler
{
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.
103 typedef HashMap
<BasicBlockKey
, BasicBlockLocation
*> BlockLocationCache
;
104 typedef HashMap
<intptr_t, BlockLocationCache
> SourceIDBuckets
;
106 SourceIDBuckets m_sourceIDBuckets
;
107 BasicBlockLocation m_dummyBasicBlock
;
112 #endif // ControlFlowProfiler_h