| 1 | /* |
| 2 | * Copyright (C) 2013-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(DFG_JIT) |
| 29 | |
| 30 | #include "CodeBlock.h" |
| 31 | #include "CompilationResult.h" |
| 32 | #include "DFGCommonData.h" |
| 33 | #include "DFGMinifiedGraph.h" |
| 34 | #include "DFGOSREntry.h" |
| 35 | #include "DFGOSRExit.h" |
| 36 | #include "DFGVariableEventStream.h" |
| 37 | #include "ExecutionCounter.h" |
| 38 | #include "JITCode.h" |
| 39 | #include <wtf/SegmentedVector.h> |
| 40 | |
| 41 | namespace JSC { |
| 42 | |
| 43 | class TrackedReferences; |
| 44 | |
| 45 | namespace DFG { |
| 46 | |
| 47 | class JITCompiler; |
| 48 | |
| 49 | class JITCode : public DirectJITCode { |
| 50 | public: |
| 51 | JITCode(); |
| 52 | virtual ~JITCode(); |
| 53 | |
| 54 | CommonData* dfgCommon() override; |
| 55 | JITCode* dfg() override; |
| 56 | |
| 57 | OSREntryData* appendOSREntryData(unsigned bytecodeIndex, CodeLocationLabel<OSREntryPtrTag> machineCode) |
| 58 | { |
| 59 | DFG::OSREntryData entry; |
| 60 | entry.m_bytecodeIndex = bytecodeIndex; |
| 61 | entry.m_machineCode = machineCode; |
| 62 | osrEntry.append(entry); |
| 63 | return &osrEntry.last(); |
| 64 | } |
| 65 | |
| 66 | OSREntryData* osrEntryDataForBytecodeIndex(unsigned bytecodeIndex) |
| 67 | { |
| 68 | return tryBinarySearch<OSREntryData, unsigned>( |
| 69 | osrEntry, osrEntry.size(), bytecodeIndex, |
| 70 | getOSREntryDataBytecodeIndex); |
| 71 | } |
| 72 | |
| 73 | void finalizeOSREntrypoints(); |
| 74 | |
| 75 | unsigned appendOSRExit(const OSRExit& exit) |
| 76 | { |
| 77 | unsigned result = osrExit.size(); |
| 78 | osrExit.append(exit); |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | OSRExit& lastOSRExit() |
| 83 | { |
| 84 | return osrExit.last(); |
| 85 | } |
| 86 | |
| 87 | unsigned appendSpeculationRecovery(const SpeculationRecovery& recovery) |
| 88 | { |
| 89 | unsigned result = speculationRecovery.size(); |
| 90 | speculationRecovery.append(recovery); |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | void reconstruct( |
| 95 | CodeBlock*, CodeOrigin, unsigned streamIndex, Operands<ValueRecovery>& result); |
| 96 | |
| 97 | // This is only applicable if we're at a point where all values are spilled to the |
| 98 | // stack. Currently, it also has the restriction that the values must be in their |
| 99 | // bytecode-designated stack slots. |
| 100 | void reconstruct( |
| 101 | ExecState*, CodeBlock*, CodeOrigin, unsigned streamIndex, Operands<Optional<JSValue>>& result); |
| 102 | |
| 103 | #if ENABLE(FTL_JIT) |
| 104 | // NB. All of these methods take CodeBlock* because they may want to use |
| 105 | // CodeBlock's logic about scaling thresholds. It should be a DFG CodeBlock. |
| 106 | |
| 107 | bool checkIfOptimizationThresholdReached(CodeBlock*); |
| 108 | void optimizeNextInvocation(CodeBlock*); |
| 109 | void dontOptimizeAnytimeSoon(CodeBlock*); |
| 110 | void optimizeAfterWarmUp(CodeBlock*); |
| 111 | void optimizeSoon(CodeBlock*); |
| 112 | void forceOptimizationSlowPathConcurrently(CodeBlock*); |
| 113 | void setOptimizationThresholdBasedOnCompilationResult(CodeBlock*, CompilationResult); |
| 114 | #endif // ENABLE(FTL_JIT) |
| 115 | |
| 116 | void validateReferences(const TrackedReferences&) override; |
| 117 | |
| 118 | void shrinkToFit(); |
| 119 | |
| 120 | RegisterSet liveRegistersToPreserveAtExceptionHandlingCallSite(CodeBlock*, CallSiteIndex) override; |
| 121 | #if ENABLE(FTL_JIT) |
| 122 | CodeBlock* osrEntryBlock() { return m_osrEntryBlock.get(); } |
| 123 | void setOSREntryBlock(VM&, const JSCell* owner, CodeBlock* osrEntryBlock); |
| 124 | void clearOSREntryBlockAndResetThresholds(CodeBlock* dfgCodeBlock); |
| 125 | #endif |
| 126 | |
| 127 | static ptrdiff_t commonDataOffset() { return OBJECT_OFFSETOF(JITCode, common); } |
| 128 | |
| 129 | Optional<CodeOrigin> findPC(CodeBlock*, void* pc) override; |
| 130 | |
| 131 | using DirectJITCode::initializeCodeRefForDFG; |
| 132 | |
| 133 | private: |
| 134 | friend class JITCompiler; // Allow JITCompiler to call setCodeRef(). |
| 135 | |
| 136 | public: |
| 137 | CommonData common; |
| 138 | Vector<DFG::OSREntryData> osrEntry; |
| 139 | SegmentedVector<DFG::OSRExit, 8> osrExit; |
| 140 | Vector<DFG::SpeculationRecovery> speculationRecovery; |
| 141 | DFG::VariableEventStream variableEventStream; |
| 142 | DFG::MinifiedGraph minifiedDFG; |
| 143 | |
| 144 | #if ENABLE(FTL_JIT) |
| 145 | uint8_t neverExecutedEntry { 1 }; |
| 146 | |
| 147 | UpperTierExecutionCounter tierUpCounter; |
| 148 | |
| 149 | // For osrEntryPoint that are in inner loop, this maps their bytecode to the bytecode |
| 150 | // of the outerloop entry points in order (from innermost to outermost). |
| 151 | // |
| 152 | // The key may not always be a target for OSR Entry but the list in the value is guaranteed |
| 153 | // to be usable for OSR Entry. |
| 154 | HashMap<unsigned, Vector<unsigned>> tierUpInLoopHierarchy; |
| 155 | |
| 156 | // Map each bytecode of CheckTierUpAndOSREnter to its stream index. |
| 157 | HashMap<unsigned, unsigned, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> bytecodeIndexToStreamIndex; |
| 158 | |
| 159 | enum class TriggerReason : uint8_t { |
| 160 | DontTrigger, |
| 161 | CompilationDone, |
| 162 | StartCompilation, |
| 163 | }; |
| 164 | |
| 165 | // Map each bytecode of CheckTierUpAndOSREnter to its trigger forcing OSR Entry. |
| 166 | // This can never be modified after it has been initialized since the addresses of the triggers |
| 167 | // are used by the JIT. |
| 168 | HashMap<unsigned, TriggerReason> tierUpEntryTriggers; |
| 169 | |
| 170 | // Set of bytecode that were the target of a TierUp operation. |
| 171 | HashSet<unsigned, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> tierUpEntrySeen; |
| 172 | |
| 173 | WriteBarrier<CodeBlock> m_osrEntryBlock; |
| 174 | unsigned osrEntryRetry; |
| 175 | bool abandonOSREntry; |
| 176 | #endif // ENABLE(FTL_JIT) |
| 177 | }; |
| 178 | |
| 179 | } } // namespace JSC::DFG |
| 180 | |
| 181 | #endif // ENABLE(DFG_JIT) |
| 182 | |