1/*
2 * Copyright (C) 2016-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#pragma once
27
28#if ENABLE(JIT)
29
30#include "ArithProfile.h"
31#include "CCallHelpers.h"
32#include "JITAddGenerator.h"
33#include "JITMathICInlineResult.h"
34#include "JITMulGenerator.h"
35#include "JITNegGenerator.h"
36#include "JITSubGenerator.h"
37#include "LinkBuffer.h"
38#include "Repatch.h"
39
40namespace JSC {
41
42class LinkBuffer;
43
44struct MathICGenerationState {
45 MacroAssembler::Label fastPathStart;
46 MacroAssembler::Label fastPathEnd;
47 MacroAssembler::Label slowPathStart;
48 MacroAssembler::Call slowPathCall;
49 MacroAssembler::JumpList slowPathJumps;
50 bool shouldSlowPathRepatch;
51};
52
53#define ENABLE_MATH_IC_STATS 0
54
55template <typename GeneratorType, typename ArithProfileType>
56class JITMathIC {
57 WTF_MAKE_FAST_ALLOCATED;
58public:
59 JITMathIC(ArithProfileType* arithProfile)
60 : m_arithProfile(arithProfile)
61 {
62 }
63
64 CodeLocationLabel<JSInternalPtrTag> doneLocation() { return m_inlineEnd; }
65 CodeLocationCall<JSInternalPtrTag> slowPathCallLocation() { return m_slowPathCallLocation; }
66 CodeLocationLabel<JSInternalPtrTag> slowPathStartLocation() { return m_slowPathStartLocation; }
67
68 bool generateInline(CCallHelpers& jit, MathICGenerationState& state, bool shouldEmitProfiling = true)
69 {
70 state.fastPathStart = jit.label();
71 size_t startSize = jit.m_assembler.buffer().codeSize();
72
73 if (m_arithProfile && m_arithProfile->isObservedTypeEmpty()) {
74 // It looks like the MathIC has yet to execute. We don't want to emit code in this
75 // case for a couple reasons. First, the operation may never execute, so if we don't emit
76 // code, it's a win. Second, if the operation does execute, we can emit better code
77 // once we have an idea about the types.
78 state.slowPathJumps.append(jit.patchableJump());
79 size_t inlineSize = jit.m_assembler.buffer().codeSize() - startSize;
80 ASSERT_UNUSED(inlineSize, static_cast<ptrdiff_t>(inlineSize) <= MacroAssembler::patchableJumpSize());
81 state.shouldSlowPathRepatch = true;
82 state.fastPathEnd = jit.label();
83 ASSERT(!m_generateFastPathOnRepatch); // We should have gathered some observed type info about the types before trying to regenerate again.
84 m_generateFastPathOnRepatch = true;
85 return true;
86 }
87
88 JITMathICInlineResult result = m_generator.generateInline(jit, state, m_arithProfile);
89
90 switch (result) {
91 case JITMathICInlineResult::GeneratedFastPath: {
92 size_t inlineSize = jit.m_assembler.buffer().codeSize() - startSize;
93 if (static_cast<ptrdiff_t>(inlineSize) < MacroAssembler::patchableJumpSize()) {
94 size_t nopsToEmitInBytes = MacroAssembler::patchableJumpSize() - inlineSize;
95 jit.emitNops(nopsToEmitInBytes);
96 }
97 state.shouldSlowPathRepatch = true;
98 state.fastPathEnd = jit.label();
99 return true;
100 }
101 case JITMathICInlineResult::GenerateFullSnippet: {
102 MacroAssembler::JumpList endJumpList;
103 bool result = m_generator.generateFastPath(jit, endJumpList, state.slowPathJumps, m_arithProfile, shouldEmitProfiling);
104 if (result) {
105 state.fastPathEnd = jit.label();
106 state.shouldSlowPathRepatch = false;
107 endJumpList.link(&jit);
108 return true;
109 }
110 return false;
111 }
112 case JITMathICInlineResult::DontGenerate: {
113 return false;
114 }
115 default:
116 ASSERT_NOT_REACHED();
117 }
118
119 return false;
120 }
121
122 void generateOutOfLine(CodeBlock* codeBlock, FunctionPtr<CFunctionPtrTag> callReplacement)
123 {
124 auto linkJumpToOutOfLineSnippet = [&] () {
125 CCallHelpers jit(codeBlock);
126 auto jump = jit.jump();
127 // We don't need a nop sled here because nobody should be jumping into the middle of an IC.
128 bool needsBranchCompaction = false;
129 RELEASE_ASSERT(jit.m_assembler.buffer().codeSize() <= static_cast<size_t>(MacroAssembler::differenceBetweenCodePtr(m_inlineStart, m_inlineEnd)));
130 LinkBuffer linkBuffer(jit, m_inlineStart, jit.m_assembler.buffer().codeSize(), JITCompilationMustSucceed, needsBranchCompaction);
131 RELEASE_ASSERT(linkBuffer.isValid());
132 linkBuffer.link(jump, CodeLocationLabel<JITStubRoutinePtrTag>(m_code.code()));
133 FINALIZE_CODE(linkBuffer, NoPtrTag, "JITMathIC: linking constant jump to out of line stub");
134 };
135
136 auto replaceCall = [&] () {
137#if COMPILER(MSVC) && !COMPILER(CLANG)
138 ftlThunkAwareRepatchCall(codeBlock, slowPathCallLocation().retagged<JSInternalPtrTag>(), callReplacement);
139#else
140 ftlThunkAwareRepatchCall(codeBlock, slowPathCallLocation().template retagged<JSInternalPtrTag>(), callReplacement);
141#endif
142 };
143
144 bool shouldEmitProfiling = !JITCode::isOptimizingJIT(codeBlock->jitType());
145
146 if (m_generateFastPathOnRepatch) {
147
148 CCallHelpers jit(codeBlock);
149 MathICGenerationState generationState;
150 bool generatedInline = generateInline(jit, generationState, shouldEmitProfiling);
151
152 // We no longer want to try to regenerate the fast path.
153 m_generateFastPathOnRepatch = false;
154
155 if (generatedInline) {
156 auto jumpToDone = jit.jump();
157
158 LinkBuffer linkBuffer(jit, codeBlock, JITCompilationCanFail);
159 if (!linkBuffer.didFailToAllocate()) {
160 linkBuffer.link(generationState.slowPathJumps, slowPathStartLocation());
161 linkBuffer.link(jumpToDone, doneLocation());
162
163 m_code = FINALIZE_CODE_FOR(
164 codeBlock, linkBuffer, JITStubRoutinePtrTag, "JITMathIC: generating out of line fast IC snippet");
165
166 if (!generationState.shouldSlowPathRepatch) {
167 // We won't need to regenerate, so we can wire the slow path call
168 // to a non repatching variant.
169 replaceCall();
170 }
171
172 linkJumpToOutOfLineSnippet();
173
174 return;
175 }
176 }
177
178 // We weren't able to generate an out of line fast path.
179 // We just generate the snippet in its full generality.
180 }
181
182 // We rewire to the alternate regardless of whether or not we can allocate the out of line path
183 // because if we fail allocating the out of line path, we don't want to waste time trying to
184 // allocate it in the future.
185 replaceCall();
186
187 {
188 CCallHelpers jit(codeBlock);
189
190 MacroAssembler::JumpList endJumpList;
191 MacroAssembler::JumpList slowPathJumpList;
192
193 bool emittedFastPath = m_generator.generateFastPath(jit, endJumpList, slowPathJumpList, m_arithProfile, shouldEmitProfiling);
194 if (!emittedFastPath)
195 return;
196 endJumpList.append(jit.jump());
197
198 LinkBuffer linkBuffer(jit, codeBlock, JITCompilationCanFail);
199 if (linkBuffer.didFailToAllocate())
200 return;
201
202 linkBuffer.link(endJumpList, doneLocation());
203 linkBuffer.link(slowPathJumpList, slowPathStartLocation());
204
205 m_code = FINALIZE_CODE_FOR(
206 codeBlock, linkBuffer, JITStubRoutinePtrTag, "JITMathIC: generating out of line IC snippet");
207 }
208
209 linkJumpToOutOfLineSnippet();
210 }
211
212 void finalizeInlineCode(const MathICGenerationState& state, LinkBuffer& linkBuffer)
213 {
214 CodeLocationLabel<JSInternalPtrTag> start = linkBuffer.locationOf<JSInternalPtrTag>(state.fastPathStart);
215 m_inlineStart = start;
216
217 m_inlineEnd = linkBuffer.locationOf<JSInternalPtrTag>(state.fastPathEnd);
218 ASSERT(m_inlineEnd.untaggedExecutableAddress() > m_inlineStart.untaggedExecutableAddress());
219
220 m_slowPathCallLocation = linkBuffer.locationOf<JSInternalPtrTag>(state.slowPathCall);
221 m_slowPathStartLocation = linkBuffer.locationOf<JSInternalPtrTag>(state.slowPathStart);
222 }
223
224 ArithProfileType* arithProfile() const { return m_arithProfile; }
225
226#if ENABLE(MATH_IC_STATS)
227 size_t m_generatedCodeSize { 0 };
228 size_t codeSize() const
229 {
230 size_t result = m_generatedCodeSize;
231 if (m_code)
232 result += m_code.size();
233 return result;
234 }
235#endif
236
237 ArithProfileType* m_arithProfile;
238 MacroAssemblerCodeRef<JITStubRoutinePtrTag> m_code;
239 CodeLocationLabel<JSInternalPtrTag> m_inlineStart;
240 CodeLocationLabel<JSInternalPtrTag> m_inlineEnd;
241 CodeLocationLabel<JSInternalPtrTag> m_slowPathCallLocation;
242 CodeLocationLabel<JSInternalPtrTag> m_slowPathStartLocation;
243 bool m_generateFastPathOnRepatch { false };
244 GeneratorType m_generator;
245};
246
247template <typename GeneratorType>
248class JITBinaryMathIC : public JITMathIC<GeneratorType, BinaryArithProfile> {
249public:
250 JITBinaryMathIC(BinaryArithProfile* arithProfile)
251 : JITMathIC<GeneratorType, BinaryArithProfile>(arithProfile)
252 {
253 }
254};
255
256typedef JITBinaryMathIC<JITAddGenerator> JITAddIC;
257typedef JITBinaryMathIC<JITMulGenerator> JITMulIC;
258typedef JITBinaryMathIC<JITSubGenerator> JITSubIC;
259
260template <typename GeneratorType>
261class JITUnaryMathIC : public JITMathIC<GeneratorType, UnaryArithProfile> {
262public:
263 JITUnaryMathIC(UnaryArithProfile* arithProfile)
264 : JITMathIC<GeneratorType, UnaryArithProfile>(arithProfile)
265 {
266 }
267};
268
269typedef JITUnaryMathIC<JITNegGenerator> JITNegIC;
270
271} // namespace JSC
272
273#endif // ENABLE(JIT)
274