1/*
2 * Copyright (C) 2015-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#include "config.h"
27#include "PolymorphicCallStubRoutine.h"
28
29#if ENABLE(JIT)
30
31#include "CallLinkInfo.h"
32#include "CodeBlock.h"
33#include "FullCodeOrigin.h"
34#include "JSCInlines.h"
35#include "LinkBuffer.h"
36
37namespace JSC {
38
39PolymorphicCallNode::~PolymorphicCallNode()
40{
41 if (isOnList())
42 remove();
43}
44
45void PolymorphicCallNode::unlink(VM& vm)
46{
47 if (m_callLinkInfo) {
48 if (Options::dumpDisassembly())
49 dataLog("Unlinking polymorphic call at ", m_callLinkInfo->callReturnLocation(), ", ", m_callLinkInfo->codeOrigin(), "\n");
50
51 m_callLinkInfo->unlink(vm);
52 }
53
54 if (isOnList())
55 remove();
56}
57
58void PolymorphicCallNode::clearCallLinkInfo()
59{
60 if (Options::dumpDisassembly())
61 dataLog("Clearing call link info for polymorphic call at ", m_callLinkInfo->callReturnLocation(), ", ", m_callLinkInfo->codeOrigin(), "\n");
62
63 m_callLinkInfo = nullptr;
64}
65
66void PolymorphicCallCase::dump(PrintStream& out) const
67{
68 out.print("<variant = ", m_variant, ", codeBlock = ", pointerDump(m_codeBlock), ">");
69}
70
71PolymorphicCallStubRoutine::PolymorphicCallStubRoutine(
72 const MacroAssemblerCodeRef<JITStubRoutinePtrTag>& codeRef, VM& vm, const JSCell* owner, ExecState* callerFrame,
73 CallLinkInfo& info, const Vector<PolymorphicCallCase>& cases,
74 UniqueArray<uint32_t>&& fastCounts)
75 : GCAwareJITStubRoutine(codeRef, vm)
76 , m_fastCounts(WTFMove(fastCounts))
77{
78 for (PolymorphicCallCase callCase : cases) {
79 m_variants.append(WriteBarrier<JSCell>(vm, owner, callCase.variant().rawCalleeCell()));
80 if (shouldDumpDisassemblyFor(callerFrame->codeBlock()))
81 dataLog("Linking polymorphic call in ", FullCodeOrigin(callerFrame->codeBlock(), callerFrame->codeOrigin()), " to ", callCase.variant(), ", codeBlock = ", pointerDump(callCase.codeBlock()), "\n");
82 if (CodeBlock* codeBlock = callCase.codeBlock())
83 codeBlock->linkIncomingPolymorphicCall(callerFrame, m_callNodes.add(&info));
84 }
85 m_variants.shrinkToFit();
86 WTF::storeStoreFence();
87}
88
89PolymorphicCallStubRoutine::~PolymorphicCallStubRoutine() { }
90
91CallVariantList PolymorphicCallStubRoutine::variants() const
92{
93 CallVariantList result;
94 for (size_t i = 0; i < m_variants.size(); ++i)
95 result.append(CallVariant(m_variants[i].get()));
96 return result;
97}
98
99bool PolymorphicCallStubRoutine::hasEdges() const
100{
101 // The FTL does not count edges in its poly call stub routines. If the FTL went poly call, then
102 // it's not meaningful to keep profiling - we can just leave it at that. Remember, the FTL would
103 // have had full edge profiling from the DFG, and based on this information, it would have
104 // decided to go poly.
105 //
106 // There probably are very-difficult-to-imagine corner cases where the FTL not doing edge
107 // profiling is bad for polyvariant inlining. But polyvariant inlining is profitable sometimes
108 // while not having to increment counts is profitable always. So, we let the FTL run faster and
109 // not keep counts.
110 return !!m_fastCounts;
111}
112
113CallEdgeList PolymorphicCallStubRoutine::edges() const
114{
115 RELEASE_ASSERT(m_fastCounts);
116
117 CallEdgeList result;
118 for (size_t i = 0; i < m_variants.size(); ++i)
119 result.append(CallEdge(CallVariant(m_variants[i].get()), m_fastCounts[i]));
120 return result;
121}
122
123void PolymorphicCallStubRoutine::clearCallNodesFor(CallLinkInfo* info)
124{
125 for (Bag<PolymorphicCallNode>::iterator iter = m_callNodes.begin(); !!iter; ++iter) {
126 PolymorphicCallNode& node = **iter;
127 // All nodes should point to info, but okay to be a little paranoid.
128 if (node.hasCallLinkInfo(info))
129 node.clearCallLinkInfo();
130 }
131}
132
133bool PolymorphicCallStubRoutine::visitWeak(VM& vm)
134{
135 for (auto& variant : m_variants) {
136 if (!vm.heap.isMarked(variant.get()))
137 return false;
138 }
139 return true;
140}
141
142void PolymorphicCallStubRoutine::markRequiredObjectsInternal(SlotVisitor& visitor)
143{
144 for (auto& variant : m_variants)
145 visitor.append(variant);
146}
147
148} // namespace JSC
149
150#endif // ENABLE(JIT)
151