1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
* Copyright (C) 2013-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "BytecodeLivenessAnalysis.h"
#include "BytecodeLivenessAnalysisInlines.h"
#include "BytecodeUseDef.h"
#include "CodeBlock.h"
#include "FullBytecodeLiveness.h"
#include "JSCJSValueInlines.h"
#include <wtf/TZoneMallocInlines.h>
namespace JSC {
WTF_MAKE_TZONE_ALLOCATED_IMPL(BytecodeLivenessAnalysis);
WTF_MAKE_TZONE_ALLOCATED_IMPL(FullBytecodeLiveness);
BytecodeLivenessAnalysis::BytecodeLivenessAnalysis(CodeBlock* codeBlock)
: m_graph(codeBlock, codeBlock->instructions())
{
runLivenessFixpoint(codeBlock, codeBlock->instructions(), m_graph);
if (UNLIKELY(Options::dumpBytecodeLivenessResults()))
dumpResults(codeBlock);
}
std::unique_ptr<FullBytecodeLiveness> BytecodeLivenessAnalysis::computeFullLiveness(CodeBlock* codeBlock)
{
FastBitVector out;
size_t size = codeBlock->instructions().size();
auto result = makeUnique<FullBytecodeLiveness>(size);
for (auto& block : m_graph.basicBlocksInReverseOrder()) {
if (block.isEntryBlock() || block.isExitBlock())
continue;
out = block.out();
auto use = [&] (unsigned bitIndex) {
// This is the use functor, so we set the bit.
out[bitIndex] = true;
};
auto def = [&] (unsigned bitIndex) {
// This is the def functor, so we clear the bit.
out[bitIndex] = false;
};
auto& instructions = codeBlock->instructions();
unsigned cursor = block.totalLength();
for (unsigned i = block.delta().size(); i--;) {
cursor -= block.delta()[i];
BytecodeIndex bytecodeIndex = BytecodeIndex(block.leaderOffset() + cursor);
auto instruction = instructions.at(bytecodeIndex);
for (Checkpoint checkpoint = instruction->numberOfCheckpoints(); checkpoint--;) {
ASSERT(checkpoint < instruction->size());
bytecodeIndex = bytecodeIndex.withCheckpoint(checkpoint);
stepOverBytecodeIndexDef(codeBlock, instructions, m_graph, bytecodeIndex, def);
stepOverBytecodeIndexUseInExceptionHandler(codeBlock, instructions, m_graph, bytecodeIndex, use);
result->m_usesAfter[result->toIndex(bytecodeIndex)] = out; // AfterUse point.
stepOverBytecodeIndexUse(codeBlock, instructions, m_graph, bytecodeIndex, use);
result->m_usesBefore[result->toIndex(bytecodeIndex)] = out; // BeforeUse point.
}
}
}
return result;
}
void BytecodeLivenessAnalysis::dumpResults(CodeBlock* codeBlock)
{
dataLog("\nDumping bytecode liveness for ", *codeBlock, ":\n");
const auto& instructions = codeBlock->instructions();
unsigned i = 0;
unsigned numberOfBlocks = m_graph.size();
Vector<FastBitVector> predecessors(numberOfBlocks);
for (auto& block : m_graph)
predecessors[block.index()].resize(numberOfBlocks);
for (auto& block : m_graph) {
for (unsigned successorIndex : block.successors()) {
unsigned blockIndex = block.index();
predecessors[successorIndex][blockIndex] = true;
}
}
auto dumpBitVector = [] (FastBitVector& bits) {
for (unsigned j = 0; j < bits.numBits(); j++) {
if (bits[j])
dataLogF(" %u", j);
}
};
for (auto& block : m_graph) {
dataLogF("\nBytecode basic block %u: %p (offset: %u, length: %u)\n", i++, &block, block.leaderOffset(), block.totalLength());
dataLogF("Predecessors:");
dumpBitVector(predecessors[block.index()]);
dataLogF("\n");
dataLogF("Successors:");
FastBitVector successors;
successors.resize(numberOfBlocks);
for (unsigned successorIndex : block.successors())
successors[successorIndex] = true;
dumpBitVector(successors); // Dump in sorted order.
dataLogF("\n");
if (block.isEntryBlock()) {
dataLogF("Entry block %p\n", &block);
continue;
}
if (block.isExitBlock()) {
dataLogF("Exit block: %p\n", &block);
continue;
}
for (unsigned bytecodeOffset = block.leaderOffset(); bytecodeOffset < block.leaderOffset() + block.totalLength();) {
const auto currentInstruction = instructions.at(bytecodeOffset);
dataLogF("Live variables:");
FastBitVector liveBefore = getLivenessInfoAtInstruction(codeBlock, BytecodeIndex(bytecodeOffset));
dumpBitVector(liveBefore);
dataLogF("\n");
codeBlock->dumpBytecode(WTF::dataFile(), currentInstruction);
bytecodeOffset += currentInstruction->size();
}
dataLogF("Live variables:");
FastBitVector liveAfter = block.out();
dumpBitVector(liveAfter);
dataLogF("\n");
}
}
template<typename EnumType1, typename EnumType2>
constexpr bool enumValuesEqualAsIntegral(EnumType1 v1, EnumType2 v2)
{
using IntType1 = typename std::underlying_type<EnumType1>::type;
using IntType2 = typename std::underlying_type<EnumType2>::type;
if constexpr (sizeof(IntType1) > sizeof(IntType2))
return static_cast<IntType1>(v1) == static_cast<IntType1>(v2);
else
return static_cast<IntType2>(v1) == static_cast<IntType2>(v2);
}
WTF::BitSet<maxNumCheckpointTmps> tmpLivenessForCheckpoint(const CodeBlock& codeBlock, BytecodeIndex bytecodeIndex)
{
WTF::BitSet<maxNumCheckpointTmps> result;
Checkpoint checkpoint = bytecodeIndex.checkpoint();
if (!checkpoint)
return result;
switch (codeBlock.instructions().at(bytecodeIndex)->opcodeID()) {
case op_call_varargs:
case op_tail_call_varargs:
case op_construct_varargs:
case op_super_construct_varargs: {
static_assert(enumValuesEqualAsIntegral(OpCallVarargs::makeCall, OpTailCallVarargs::makeCall) && enumValuesEqualAsIntegral(OpCallVarargs::argCountIncludingThis, OpTailCallVarargs::argCountIncludingThis));
static_assert(enumValuesEqualAsIntegral(OpCallVarargs::makeCall, OpConstructVarargs::makeCall) && enumValuesEqualAsIntegral(OpCallVarargs::argCountIncludingThis, OpConstructVarargs::argCountIncludingThis));
static_assert(enumValuesEqualAsIntegral(OpCallVarargs::makeCall, OpSuperConstructVarargs::makeCall) && enumValuesEqualAsIntegral(OpCallVarargs::argCountIncludingThis, OpSuperConstructVarargs::argCountIncludingThis));
if (checkpoint == OpCallVarargs::makeCall)
result.set(OpCallVarargs::argCountIncludingThis);
return result;
}
case op_iterator_open: {
return result;
}
case op_iterator_next: {
result.set(OpIteratorNext::nextResult);
return result;
}
case op_instanceof: {
return result;
}
default:
break;
}
RELEASE_ASSERT_NOT_REACHED();
}
} // namespace JSC
|