1 | /* |
2 | * Copyright (C) 2017 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 "AirFixSpillsAfterTerminals.h" |
28 | |
29 | #if ENABLE(B3_JIT) |
30 | |
31 | #include "AirBlockInsertionSet.h" |
32 | #include "AirCode.h" |
33 | #include "AirInsertionSet.h" |
34 | #include "AirInstInlines.h" |
35 | #include "AirTmpInlines.h" |
36 | |
37 | namespace JSC { namespace B3 { namespace Air { |
38 | |
39 | void fixSpillsAfterTerminals(Code& code) |
40 | { |
41 | // Because there may be terminals that produce values, IRC may |
42 | // want to spill those terminals. It'll happen to spill it after |
43 | // the terminal. If we left the graph in this state, it'd be invalid |
44 | // because a terminal must be the last instruction in a block. |
45 | // We fix that here. |
46 | |
47 | BlockInsertionSet blockInsertionSet(code); |
48 | InsertionSet insertionSet(code); |
49 | |
50 | for (BasicBlock* block : code) { |
51 | unsigned terminalIndex = block->size(); |
52 | bool foundTerminal = false; |
53 | while (terminalIndex--) { |
54 | if (block->at(terminalIndex).isTerminal()) { |
55 | foundTerminal = true; |
56 | break; |
57 | } |
58 | } |
59 | ASSERT_UNUSED(foundTerminal, foundTerminal); |
60 | |
61 | if (terminalIndex == block->size() - 1) |
62 | continue; |
63 | |
64 | // There must be instructions after the terminal because it's not the last instruction. |
65 | ASSERT(terminalIndex < block->size() - 1); |
66 | Vector<Inst, 1> instsToMove; |
67 | for (unsigned i = terminalIndex + 1; i < block->size(); i++) |
68 | instsToMove.append(block->at(i)); |
69 | RELEASE_ASSERT(instsToMove.size()); |
70 | |
71 | for (FrequentedBlock& frequentedSuccessor : block->successors()) { |
72 | BasicBlock* successor = frequentedSuccessor.block(); |
73 | // If successor's only predecessor is block, we can plant the spill inside |
74 | // the successor. Otherwise, we must split the critical edge and create |
75 | // a new block for the spill. |
76 | if (successor->numPredecessors() == 1) { |
77 | insertionSet.insertInsts(0, instsToMove); |
78 | insertionSet.execute(successor); |
79 | } else { |
80 | BasicBlock* newBlock = blockInsertionSet.insertBefore(successor, successor->frequency()); |
81 | for (const Inst& inst : instsToMove) |
82 | newBlock->appendInst(inst); |
83 | newBlock->appendInst(Inst(Jump, instsToMove.last().origin)); |
84 | newBlock->successors().append(successor); |
85 | frequentedSuccessor.block() = newBlock; |
86 | } |
87 | } |
88 | |
89 | block->resize(terminalIndex + 1); |
90 | } |
91 | |
92 | if (blockInsertionSet.execute()) |
93 | code.resetReachability(); |
94 | } |
95 | |
96 | } } } // namespace JSC::B3::Air |
97 | |
98 | #endif // ENABLE(B3_JIT) |
99 | |
100 | |