mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 9b5601a | 2017-06-16 21:02:37 +0000 | [diff] [blame] | 2 | * Copyright (C) 2013-2017 Apple Inc. All rights reserved. |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 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. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "BytecodeBasicBlock.h" |
| 28 | |
| 29 | #include "CodeBlock.h" |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 30 | #include "PreciseJumpTargets.h" |
ysuzuki@apple.com | 193bf7b | 2020-02-04 19:05:17 +0000 | [diff] [blame] | 31 | #include "UnlinkedCodeBlockGenerator.h" |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 32 | |
| 33 | namespace JSC { |
| 34 | |
ysuzuki@apple.com | 4642e11 | 2020-01-03 02:36:43 +0000 | [diff] [blame] | 35 | DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(BytecodeBasicBlock); |
| 36 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 37 | template<typename OpcodeTraits> |
| 38 | BytecodeBasicBlock<OpcodeTraits>::BytecodeBasicBlock(const typename InstructionStreamType::Ref& instruction, unsigned blockIndex) |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 39 | : m_leaderOffset(instruction.offset()) |
| 40 | , m_totalLength(0) |
| 41 | , m_index(blockIndex) |
| 42 | { |
| 43 | addLength(instruction->size()); |
| 44 | } |
| 45 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 46 | template<typename OpcodeTraits> |
| 47 | BytecodeBasicBlock<OpcodeTraits>::BytecodeBasicBlock(typename BytecodeBasicBlock<OpcodeTraits>::SpecialBlockType blockType, unsigned blockIndex) |
| 48 | : m_leaderOffset(blockType == BytecodeBasicBlock<OpcodeTraits>::EntryBlock ? 0 : UINT_MAX) |
| 49 | , m_totalLength(blockType == BytecodeBasicBlock<OpcodeTraits>::EntryBlock ? 0 : UINT_MAX) |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 50 | , m_index(blockIndex) |
| 51 | { |
| 52 | } |
| 53 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 54 | template<typename OpcodeTraits> |
| 55 | void BytecodeBasicBlock<OpcodeTraits>::addLength(unsigned bytecodeLength) |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 56 | { |
| 57 | m_delta.append(bytecodeLength); |
| 58 | m_totalLength += bytecodeLength; |
| 59 | } |
| 60 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 61 | template<typename OpcodeTraits> |
| 62 | void BytecodeBasicBlock<OpcodeTraits>::shrinkToFit() |
benjamin@webkit.org | 66220c7 | 2015-08-24 06:18:42 +0000 | [diff] [blame] | 63 | { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 64 | m_delta.shrinkToFit(); |
benjamin@webkit.org | 66220c7 | 2015-08-24 06:18:42 +0000 | [diff] [blame] | 65 | m_successors.shrinkToFit(); |
| 66 | } |
| 67 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 68 | template<typename OpcodeTraits> |
| 69 | static bool isJumpTarget(typename OpcodeTraits::OpcodeID opcodeID, const Vector<typename BytecodeBasicBlock<OpcodeTraits>::InstructionStreamType::Offset, 32>& jumpTargets, unsigned bytecodeOffset) |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 70 | { |
| 71 | if (opcodeID == op_catch) |
| 72 | return true; |
| 73 | |
benjamin@webkit.org | 66220c7 | 2015-08-24 06:18:42 +0000 | [diff] [blame] | 74 | return std::binary_search(jumpTargets.begin(), jumpTargets.end(), bytecodeOffset); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 75 | } |
| 76 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 77 | template<typename OpcodeTraits> |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 78 | template<typename Block> |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 79 | auto BytecodeBasicBlock<OpcodeTraits>::computeImpl(Block* codeBlock, const InstructionStreamType& instructions) -> BasicBlockVector |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 80 | { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 81 | BasicBlockVector basicBlocks; |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 82 | Vector<typename InstructionStreamType::Offset, 32> jumpTargets; |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 83 | computePreciseJumpTargets(codeBlock, instructions, jumpTargets); |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 84 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 85 | auto linkBlocks = [&] (BytecodeBasicBlock<OpcodeTraits>& from, BytecodeBasicBlock<OpcodeTraits>& to) { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 86 | from.addSuccessor(to); |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 89 | { |
| 90 | // Create the entry and exit basic blocks. |
| 91 | basicBlocks.reserveCapacity(jumpTargets.size() + 2); |
| 92 | { |
| 93 | // Entry block. |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 94 | basicBlocks.constructAndAppend(BytecodeBasicBlock<OpcodeTraits>::EntryBlock, basicBlocks.size()); |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 95 | // First block. |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 96 | basicBlocks.constructAndAppend(BytecodeBasicBlock<OpcodeTraits>::EntryBlock, basicBlocks.size()); |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 97 | linkBlocks(basicBlocks[0], basicBlocks[1]); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 98 | } |
| 99 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 100 | auto* current = &basicBlocks.last(); |
| 101 | auto appendBlock = [&] (const typename InstructionStreamType::Ref& instruction) -> BytecodeBasicBlock<OpcodeTraits>* { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 102 | basicBlocks.constructAndAppend(instruction, basicBlocks.size()); |
| 103 | return &basicBlocks.last(); |
| 104 | }; |
| 105 | bool nextInstructionIsLeader = false; |
| 106 | for (const auto& instruction : instructions) { |
| 107 | auto bytecodeOffset = instruction.offset(); |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 108 | auto opcodeID = instruction->opcodeID(); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 109 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 110 | bool createdBlock = false; |
| 111 | // If the current bytecode is a jump target, then it's the leader of its own basic block. |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 112 | if (nextInstructionIsLeader || isJumpTarget<OpcodeTraits>(opcodeID, jumpTargets, bytecodeOffset)) { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 113 | current = appendBlock(instruction); |
| 114 | createdBlock = true; |
| 115 | nextInstructionIsLeader = false; |
| 116 | } |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 117 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 118 | // If the current bytecode is a branch or a return, then the next instruction is the leader of its own basic block. |
| 119 | if (isBranch(opcodeID) || isTerminal(opcodeID) || isThrow(opcodeID)) |
| 120 | nextInstructionIsLeader = true; |
| 121 | |
| 122 | if (createdBlock) |
| 123 | continue; |
| 124 | |
| 125 | // Otherwise, just add to the length of the current block. |
| 126 | current->addLength(instruction->size()); |
| 127 | } |
| 128 | // Exit block. |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 129 | basicBlocks.constructAndAppend(BytecodeBasicBlock<OpcodeTraits>::ExitBlock, basicBlocks.size()); |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 130 | basicBlocks.shrinkToFit(); |
| 131 | ASSERT(basicBlocks.last().isExitBlock()); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 132 | } |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 133 | // After this point, we never change basicBlocks. |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 134 | |
| 135 | // Link basic blocks together. |
| 136 | for (unsigned i = 0; i < basicBlocks.size(); i++) { |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 137 | auto& block = basicBlocks[i]; |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 138 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 139 | if (block.isEntryBlock() || block.isExitBlock()) |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 140 | continue; |
| 141 | |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 142 | bool fallsThrough = true; |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 143 | for (unsigned visitedLength = 0; visitedLength < block.totalLength();) { |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 144 | auto instruction = instructions.at(block.leaderOffset() + visitedLength); |
| 145 | auto opcodeID = instruction->opcodeID(); |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 146 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 147 | visitedLength += instruction->size(); |
| 148 | |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 149 | // If we found a terminal bytecode, link to the exit block. |
| 150 | if (isTerminal(opcodeID)) { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 151 | ASSERT(instruction.offset() + instruction->size() == block.leaderOffset() + block.totalLength()); |
| 152 | linkBlocks(block, basicBlocks.last()); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 153 | fallsThrough = false; |
| 154 | break; |
| 155 | } |
| 156 | |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 157 | // If we found a throw, get the HandlerInfo for this instruction to see where we will jump. |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 158 | // If there isn't one, treat this throw as a terminal. This is true even if we have a finally |
| 159 | // block because the finally block will create its own catch, which will generate a HandlerInfo. |
| 160 | if (isThrow(opcodeID)) { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 161 | ASSERT(instruction.offset() + instruction->size() == block.leaderOffset() + block.totalLength()); |
keith_miller@apple.com | 0f985ec | 2019-10-23 00:55:38 +0000 | [diff] [blame] | 162 | auto* handler = codeBlock->handlerForBytecodeIndex(BytecodeIndex(instruction.offset())); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 163 | fallsThrough = false; |
| 164 | if (!handler) { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 165 | linkBlocks(block, basicBlocks.last()); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 166 | break; |
| 167 | } |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 168 | for (auto& otherBlock : basicBlocks) { |
| 169 | if (handler->target == otherBlock.leaderOffset()) { |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 170 | linkBlocks(block, otherBlock); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | // If we found a branch, link to the block(s) that we jump to. |
| 178 | if (isBranch(opcodeID)) { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 179 | ASSERT(instruction.offset() + instruction->size() == block.leaderOffset() + block.totalLength()); |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 180 | Vector<typename InstructionStreamType::Offset, 1> bytecodeOffsetsJumpedTo; |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 181 | findJumpTargetsForInstruction(codeBlock, instruction, bytecodeOffsetsJumpedTo); |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 182 | |
mark.lam@apple.com | a08e585 | 2016-12-14 19:58:27 +0000 | [diff] [blame] | 183 | size_t numberOfJumpTargets = bytecodeOffsetsJumpedTo.size(); |
| 184 | ASSERT(numberOfJumpTargets); |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 185 | for (auto& otherBlock : basicBlocks) { |
| 186 | if (bytecodeOffsetsJumpedTo.contains(otherBlock.leaderOffset())) { |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 187 | linkBlocks(block, otherBlock); |
mark.lam@apple.com | a08e585 | 2016-12-14 19:58:27 +0000 | [diff] [blame] | 188 | --numberOfJumpTargets; |
| 189 | if (!numberOfJumpTargets) |
| 190 | break; |
| 191 | } |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 192 | } |
mark.lam@apple.com | a08e585 | 2016-12-14 19:58:27 +0000 | [diff] [blame] | 193 | // numberOfJumpTargets may not be 0 here if there are multiple jumps targeting the same |
| 194 | // basic blocks (e.g. in a switch type opcode). Since we only decrement numberOfJumpTargets |
| 195 | // once per basic block, the duplicates are not accounted for. For our purpose here, |
| 196 | // that doesn't matter because we only need to link to the target block once regardless |
| 197 | // of how many ways this block can jump there. |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 198 | |
| 199 | if (isUnconditionalBranch(opcodeID)) |
| 200 | fallsThrough = false; |
| 201 | |
| 202 | break; |
| 203 | } |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // If we fall through then link to the next block in program order. |
| 207 | if (fallsThrough) { |
| 208 | ASSERT(i + 1 < basicBlocks.size()); |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 209 | auto& nextBlock = basicBlocks[i + 1]; |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 210 | linkBlocks(block, nextBlock); |
| 211 | } |
| 212 | } |
| 213 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 214 | unsigned index = 0; |
| 215 | for (auto& basicBlock : basicBlocks) { |
| 216 | basicBlock.shrinkToFit(); |
| 217 | ASSERT_UNUSED(index, basicBlock.index() == index++); |
| 218 | } |
tzagallo@apple.com | 3474dd0 | 2018-10-29 13:16:03 +0000 | [diff] [blame] | 219 | |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 220 | return basicBlocks; |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 221 | } |
| 222 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 223 | template<> |
| 224 | auto BytecodeBasicBlock<JSOpcodeTraits>::compute(CodeBlock* codeBlock, const JSInstructionStream& instructions) -> BasicBlockVector |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 225 | { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 226 | return computeImpl(codeBlock, instructions); |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 227 | } |
| 228 | |
justin_michaud@apple.com | c488df6 | 2022-03-03 06:49:38 +0000 | [diff] [blame] | 229 | template<> |
| 230 | auto BytecodeBasicBlock<JSOpcodeTraits>::compute(UnlinkedCodeBlockGenerator* codeBlock, const JSInstructionStream& instructions) -> BasicBlockVector |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 231 | { |
ysuzuki@apple.com | 3e7cae2 | 2020-01-30 22:32:43 +0000 | [diff] [blame] | 232 | return computeImpl(codeBlock, instructions); |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 233 | } |
| 234 | |
mhahnenberg@apple.com | 3811e21 | 2013-11-12 20:35:34 +0000 | [diff] [blame] | 235 | } // namespace JSC |