| 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 "AirRegLiveness.h" |
| 28 | |
| 29 | #if ENABLE(B3_JIT) |
| 30 | |
| 31 | #include "AirArgInlines.h" |
| 32 | #include "AirInstInlines.h" |
| 33 | |
| 34 | namespace JSC { namespace B3 { namespace Air { |
| 35 | |
| 36 | RegLiveness::RegLiveness(Code& code) |
| 37 | : m_liveAtHead(code.size()) |
| 38 | , m_liveAtTail(code.size()) |
| 39 | , m_actions(code.size()) |
| 40 | { |
| 41 | // Compute constraints. |
| 42 | for (BasicBlock* block : code) { |
| 43 | ActionsForBoundary& actionsForBoundary = m_actions[block]; |
| 44 | actionsForBoundary.resize(block->size() + 1); |
| 45 | |
| 46 | for (size_t instIndex = block->size(); instIndex--;) { |
| 47 | Inst& inst = block->at(instIndex); |
| 48 | inst.forEach<Reg>( |
| 49 | [&] (Reg& reg, Arg::Role role, Bank, Width) { |
| 50 | if (Arg::isEarlyUse(role)) |
| 51 | actionsForBoundary[instIndex].use.add(reg); |
| 52 | if (Arg::isEarlyDef(role)) |
| 53 | actionsForBoundary[instIndex].def.add(reg); |
| 54 | if (Arg::isLateUse(role)) |
| 55 | actionsForBoundary[instIndex + 1].use.add(reg); |
| 56 | if (Arg::isLateDef(role)) |
| 57 | actionsForBoundary[instIndex + 1].def.add(reg); |
| 58 | }); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // The liveAtTail of each block automatically contains the LateUse's of the terminal. |
| 63 | for (BasicBlock* block : code) { |
| 64 | RegisterSet& liveAtTail = m_liveAtTail[block]; |
| 65 | |
| 66 | block->last().forEach<Reg>( |
| 67 | [&] (Reg& reg, Arg::Role role, Bank, Width) { |
| 68 | if (Arg::isLateUse(role)) |
| 69 | liveAtTail.add(reg); |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | BitVector dirtyBlocks; |
| 74 | for (size_t blockIndex = code.size(); blockIndex--;) |
| 75 | dirtyBlocks.set(blockIndex); |
| 76 | |
| 77 | bool changed; |
| 78 | do { |
| 79 | changed = false; |
| 80 | |
| 81 | for (size_t blockIndex = code.size(); blockIndex--;) { |
| 82 | BasicBlock* block = code[blockIndex]; |
| 83 | if (!block) |
| 84 | continue; |
| 85 | |
| 86 | if (!dirtyBlocks.quickClear(blockIndex)) |
| 87 | continue; |
| 88 | |
| 89 | LocalCalc localCalc(*this, block); |
| 90 | for (size_t instIndex = block->size(); instIndex--;) |
| 91 | localCalc.execute(instIndex); |
| 92 | |
| 93 | // Handle the early def's of the first instruction. |
| 94 | block->at(0).forEach<Reg>( |
| 95 | [&] (Reg& reg, Arg::Role role, Bank, Width) { |
| 96 | if (Arg::isEarlyDef(role)) |
| 97 | localCalc.m_workset.remove(reg); |
| 98 | }); |
| 99 | |
| 100 | RegisterSet& liveAtHead = m_liveAtHead[block]; |
| 101 | if (liveAtHead.subsumes(localCalc.m_workset)) |
| 102 | continue; |
| 103 | |
| 104 | liveAtHead.merge(localCalc.m_workset); |
| 105 | |
| 106 | for (BasicBlock* predecessor : block->predecessors()) { |
| 107 | RegisterSet& liveAtTail = m_liveAtTail[predecessor]; |
| 108 | if (liveAtTail.subsumes(localCalc.m_workset)) |
| 109 | continue; |
| 110 | |
| 111 | liveAtTail.merge(localCalc.m_workset); |
| 112 | dirtyBlocks.quickSet(predecessor->index()); |
| 113 | changed = true; |
| 114 | } |
| 115 | } |
| 116 | } while (changed); |
| 117 | } |
| 118 | |
| 119 | RegLiveness::~RegLiveness() |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | RegLiveness::LocalCalcForUnifiedTmpLiveness::LocalCalcForUnifiedTmpLiveness(UnifiedTmpLiveness& liveness, BasicBlock* block) |
| 124 | : LocalCalcBase(block) |
| 125 | , m_code(liveness.code) |
| 126 | , m_actions(liveness.actions[block]) |
| 127 | { |
| 128 | for (Tmp tmp : liveness.liveAtTail(block)) { |
| 129 | if (tmp.isReg()) |
| 130 | m_workset.add(tmp.reg()); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void RegLiveness::LocalCalcForUnifiedTmpLiveness::execute(unsigned instIndex) |
| 135 | { |
| 136 | for (unsigned index : m_actions[instIndex + 1].def) { |
| 137 | Tmp tmp = Tmp::tmpForLinearIndex(m_code, index); |
| 138 | if (tmp.isReg()) |
| 139 | m_workset.remove(tmp.reg()); |
| 140 | } |
| 141 | for (unsigned index : m_actions[instIndex].use) { |
| 142 | Tmp tmp = Tmp::tmpForLinearIndex(m_code, index); |
| 143 | if (tmp.isReg()) |
| 144 | m_workset.add(tmp.reg()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } } } // namespace JSC::B3::Air |
| 149 | |
| 150 | #endif // ENABLE(B3_JIT) |
| 151 | |
| 152 | |