Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1 | //===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LivePhysRegs utility for tracking liveness of |
| 11 | // physical registers across machine instructions in forward or backward order. |
| 12 | // A more detailed description can be found in the corresponding header file. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/CodeGen/LivePhysRegs.h" |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Matthias Braun | 2a63bae | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 21 | #include "llvm/Config/llvm-config.h" |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | ebe8742 | 2015-03-23 18:23:08 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 27 | /// Remove all registers from the set that get clobbered by the register |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 28 | /// mask. |
Pete Cooper | 2bce3aa | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 29 | /// The clobbers set will be the list of live registers clobbered |
| 30 | /// by the regmask. |
| 31 | void LivePhysRegs::removeRegsInMask(const MachineOperand &MO, |
Matthias Braun | 27ad7c2 | 2018-11-06 19:00:11 +0000 | [diff] [blame] | 32 | SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers) { |
| 33 | RegisterSet::iterator LRI = LiveRegs.begin(); |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 34 | while (LRI != LiveRegs.end()) { |
Pete Cooper | 2bce3aa | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 35 | if (MO.clobbersPhysReg(*LRI)) { |
| 36 | if (Clobbers) |
| 37 | Clobbers->push_back(std::make_pair(*LRI, &MO)); |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 38 | LRI = LiveRegs.erase(LRI); |
Pete Cooper | 2bce3aa | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 39 | } else |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 40 | ++LRI; |
| 41 | } |
| 42 | } |
| 43 | |
Krzysztof Parzyszek | 0ca0dda | 2017-09-14 15:53:11 +0000 | [diff] [blame] | 44 | /// Remove defined registers and regmask kills from the set. |
| 45 | void LivePhysRegs::removeDefs(const MachineInstr &MI) { |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 46 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 47 | if (O->isReg()) { |
Matt Davis | db1b5f0 | 2018-03-19 16:06:40 +0000 | [diff] [blame] | 48 | if (!O->isDef() || O->isDebug()) |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 49 | continue; |
| 50 | unsigned Reg = O->getReg(); |
Krzysztof Parzyszek | 5cbeeed | 2016-10-07 14:50:49 +0000 | [diff] [blame] | 51 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 52 | continue; |
| 53 | removeReg(Reg); |
| 54 | } else if (O->isRegMask()) |
Matthias Braun | 7c75114 | 2017-05-26 21:50:51 +0000 | [diff] [blame] | 55 | removeRegsInMask(*O); |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 56 | } |
Krzysztof Parzyszek | 0ca0dda | 2017-09-14 15:53:11 +0000 | [diff] [blame] | 57 | } |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 58 | |
Krzysztof Parzyszek | 0ca0dda | 2017-09-14 15:53:11 +0000 | [diff] [blame] | 59 | /// Add uses to the set. |
| 60 | void LivePhysRegs::addUses(const MachineInstr &MI) { |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 61 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Matt Davis | db1b5f0 | 2018-03-19 16:06:40 +0000 | [diff] [blame] | 62 | if (!O->isReg() || !O->readsReg() || O->isDebug()) |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 63 | continue; |
| 64 | unsigned Reg = O->getReg(); |
Krzysztof Parzyszek | 5cbeeed | 2016-10-07 14:50:49 +0000 | [diff] [blame] | 65 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 66 | continue; |
| 67 | addReg(Reg); |
| 68 | } |
| 69 | } |
| 70 | |
Krzysztof Parzyszek | 0ca0dda | 2017-09-14 15:53:11 +0000 | [diff] [blame] | 71 | /// Simulates liveness when stepping backwards over an instruction(bundle): |
| 72 | /// Remove Defs, add uses. This is the recommended way of calculating liveness. |
| 73 | void LivePhysRegs::stepBackward(const MachineInstr &MI) { |
| 74 | // Remove defined registers and regmask kills from the set. |
| 75 | removeDefs(MI); |
| 76 | |
| 77 | // Add uses to the set. |
| 78 | addUses(MI); |
| 79 | } |
| 80 | |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 81 | /// Simulates liveness when stepping forward over an instruction(bundle): Remove |
| 82 | /// killed-uses, add defs. This is the not recommended way, because it depends |
Chad Rosier | 7ab65d6 | 2015-09-04 12:34:55 +0000 | [diff] [blame] | 83 | /// on accurate kill flags. If possible use stepBackward() instead of this |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 84 | /// function. |
Pete Cooper | 2bce3aa | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 85 | void LivePhysRegs::stepForward(const MachineInstr &MI, |
Matthias Braun | 27ad7c2 | 2018-11-06 19:00:11 +0000 | [diff] [blame] | 86 | SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers) { |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 87 | // Remove killed registers from the set. |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 88 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Matt Davis | db1b5f0 | 2018-03-19 16:06:40 +0000 | [diff] [blame] | 89 | if (O->isReg() && !O->isDebug()) { |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 90 | unsigned Reg = O->getReg(); |
Krzysztof Parzyszek | 5cbeeed | 2016-10-07 14:50:49 +0000 | [diff] [blame] | 91 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 92 | continue; |
| 93 | if (O->isDef()) { |
Pete Cooper | 28b0dda | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 94 | // Note, dead defs are still recorded. The caller should decide how to |
| 95 | // handle them. |
| 96 | Clobbers.push_back(std::make_pair(Reg, &*O)); |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 97 | } else { |
| 98 | if (!O->isKill()) |
| 99 | continue; |
| 100 | assert(O->isUse()); |
| 101 | removeReg(Reg); |
| 102 | } |
| 103 | } else if (O->isRegMask()) |
Pete Cooper | 2bce3aa | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 104 | removeRegsInMask(*O, &Clobbers); |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Add defs to the set. |
Pete Cooper | 28b0dda | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 108 | for (auto Reg : Clobbers) { |
Krzysztof Parzyszek | 7d1c374 | 2018-04-30 19:38:47 +0000 | [diff] [blame] | 109 | // Skip dead defs and registers clobbered by regmasks. They shouldn't |
| 110 | // be added to the set. |
Pete Cooper | 28b0dda | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 111 | if (Reg.second->isReg() && Reg.second->isDead()) |
| 112 | continue; |
Krzysztof Parzyszek | 7d1c374 | 2018-04-30 19:38:47 +0000 | [diff] [blame] | 113 | if (Reg.second->isRegMask() && |
| 114 | MachineOperand::clobbersPhysReg(Reg.second->getRegMask(), Reg.first)) |
| 115 | continue; |
Pete Cooper | 2bce3aa | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 116 | addReg(Reg.first); |
Pete Cooper | 28b0dda | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 117 | } |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /// Prin the currently live registers to OS. |
| 121 | void LivePhysRegs::print(raw_ostream &OS) const { |
| 122 | OS << "Live Registers:"; |
| 123 | if (!TRI) { |
| 124 | OS << " (uninitialized)\n"; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (empty()) { |
| 129 | OS << " (empty)\n"; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 134 | OS << " " << printReg(*I, TRI); |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 135 | OS << "\n"; |
| 136 | } |
| 137 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 138 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 139 | LLVM_DUMP_METHOD void LivePhysRegs::dump() const { |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 140 | dbgs() << " " << *this; |
Juergen Ributzka | 57c38e3 | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 141 | } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 142 | #endif |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 143 | |
Matthias Braun | 2a63bae | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 144 | bool LivePhysRegs::available(const MachineRegisterInfo &MRI, |
Matthias Braun | 27ad7c2 | 2018-11-06 19:00:11 +0000 | [diff] [blame] | 145 | MCPhysReg Reg) const { |
Matthias Braun | 2a63bae | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 146 | if (LiveRegs.count(Reg)) |
| 147 | return false; |
| 148 | if (MRI.isReserved(Reg)) |
| 149 | return false; |
| 150 | for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) { |
| 151 | if (LiveRegs.count(*R)) |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 157 | /// Add live-in registers of basic block \p MBB to \p LiveRegs. |
Krzysztof Parzyszek | 6dcccf4 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 158 | void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) { |
| 159 | for (const auto &LI : MBB.liveins()) { |
Matthias Braun | 27ad7c2 | 2018-11-06 19:00:11 +0000 | [diff] [blame] | 160 | MCPhysReg Reg = LI.PhysReg; |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 161 | LaneBitmask Mask = LI.LaneMask; |
| 162 | MCSubRegIndexIterator S(Reg, TRI); |
| 163 | assert(Mask.any() && "Invalid livein mask"); |
| 164 | if (Mask.all() || !S.isValid()) { |
| 165 | addReg(Reg); |
Krzysztof Parzyszek | 6dcccf4 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 166 | continue; |
| 167 | } |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 168 | for (; S.isValid(); ++S) { |
| 169 | unsigned SI = S.getSubRegIndex(); |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 170 | if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any()) |
Krzysztof Parzyszek | 6dcccf4 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 171 | addReg(S.getSubReg()); |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 172 | } |
Krzysztof Parzyszek | 6dcccf4 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 173 | } |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 176 | /// Adds all callee saved registers to \p LiveRegs. |
| 177 | static void addCalleeSavedRegs(LivePhysRegs &LiveRegs, |
| 178 | const MachineFunction &MF) { |
Oren Ben Simhon | 6095a79 | 2017-03-14 09:09:26 +0000 | [diff] [blame] | 179 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 180 | for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 181 | LiveRegs.addReg(*CSR); |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Krzysztof Parzyszek | 1457409 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 184 | void LivePhysRegs::addPristines(const MachineFunction &MF) { |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 185 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 186 | if (!MFI.isCalleeSavedInfoValid()) |
| 187 | return; |
Krzysztof Parzyszek | 1457409 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 188 | /// This function will usually be called on an empty object, handle this |
| 189 | /// as a special case. |
| 190 | if (empty()) { |
| 191 | /// Add all callee saved regs, then remove the ones that are saved and |
| 192 | /// restored. |
| 193 | addCalleeSavedRegs(*this, MF); |
| 194 | /// Remove the ones that are not saved/restored; they are pristine. |
| 195 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
| 196 | removeReg(Info.getReg()); |
| 197 | return; |
| 198 | } |
| 199 | /// If a callee-saved register that is not pristine is already present |
| 200 | /// in the set, we should make sure that it stays in it. Precompute the |
| 201 | /// set of pristine registers in a separate object. |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 202 | /// Add all callee saved regs, then remove the ones that are saved+restored. |
Krzysztof Parzyszek | 1457409 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 203 | LivePhysRegs Pristine(*TRI); |
| 204 | addCalleeSavedRegs(Pristine, MF); |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 205 | /// Remove the ones that are not saved/restored; they are pristine. |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 206 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
Krzysztof Parzyszek | 1457409 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 207 | Pristine.removeReg(Info.getReg()); |
| 208 | for (MCPhysReg R : Pristine) |
| 209 | addReg(R); |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Matthias Braun | 02073cb | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 212 | void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) { |
Eli Friedman | c1d66ba | 2018-02-06 23:00:17 +0000 | [diff] [blame] | 213 | // To get the live-outs we simply merge the live-ins of all successors. |
| 214 | for (const MachineBasicBlock *Succ : MBB.successors()) |
| 215 | addBlockLiveIns(*Succ); |
| 216 | if (MBB.isReturnBlock()) { |
| 217 | // Return blocks are a special case because we currently don't mark up |
| 218 | // return instructions completely: specifically, there is no explicit |
| 219 | // use for callee-saved registers. So we add all callee saved registers |
| 220 | // that are saved and restored (somewhere). This does not include |
| 221 | // callee saved registers that are unused and hence not saved and |
| 222 | // restored; they are called pristine. |
| 223 | // FIXME: PEI should add explicit markings to return instructions |
| 224 | // instead of implicitly handling them here. |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 225 | const MachineFunction &MF = *MBB.getParent(); |
| 226 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 227 | if (MFI.isCalleeSavedInfoValid()) { |
| 228 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
Krzysztof Parzyszek | 7b10f6e | 2017-08-10 16:17:32 +0000 | [diff] [blame] | 229 | if (Info.isRestored()) |
| 230 | addReg(Info.getReg()); |
Matthias Braun | bfcbf6a | 2017-05-26 16:23:08 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
Matthias Braun | b4756d6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Matthias Braun | 02073cb | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 235 | void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) { |
Matthias Braun | 70862df | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 236 | const MachineFunction &MF = *MBB.getParent(); |
Eli Friedman | c1d66ba | 2018-02-06 23:00:17 +0000 | [diff] [blame] | 237 | addPristines(MF); |
| 238 | addLiveOutsNoPristines(MBB); |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Matthias Braun | 02073cb | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 241 | void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) { |
| 242 | const MachineFunction &MF = *MBB.getParent(); |
Krzysztof Parzyszek | 1457409 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 243 | addPristines(MF); |
Krzysztof Parzyszek | 6dcccf4 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 244 | addBlockLiveIns(MBB); |
Matthias Braun | 08f65f9 | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 245 | } |
Matthias Braun | fe82f4a | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 246 | |
Matthias Braun | b0e29ac | 2017-05-26 06:32:31 +0000 | [diff] [blame] | 247 | void llvm::computeLiveIns(LivePhysRegs &LiveRegs, |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 248 | const MachineBasicBlock &MBB) { |
| 249 | const MachineFunction &MF = *MBB.getParent(); |
| 250 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matthias Braun | b0e29ac | 2017-05-26 06:32:31 +0000 | [diff] [blame] | 251 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
Matthias Braun | fe82f4a | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 252 | LiveRegs.init(TRI); |
| 253 | LiveRegs.addLiveOutsNoPristines(MBB); |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 254 | for (const MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) |
Matthias Braun | fe82f4a | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 255 | LiveRegs.stepBackward(MI); |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 256 | } |
Matthias Braun | fe82f4a | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 257 | |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 258 | void llvm::addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs) { |
| 259 | assert(MBB.livein_empty() && "Expected empty live-in list"); |
| 260 | const MachineFunction &MF = *MBB.getParent(); |
| 261 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 262 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
| 263 | for (MCPhysReg Reg : LiveRegs) { |
Matthias Braun | b0e29ac | 2017-05-26 06:32:31 +0000 | [diff] [blame] | 264 | if (MRI.isReserved(Reg)) |
| 265 | continue; |
Matthias Braun | fe82f4a | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 266 | // Skip the register if we are about to add one of its super registers. |
| 267 | bool ContainsSuperReg = false; |
| 268 | for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) { |
Matthias Braun | b0e29ac | 2017-05-26 06:32:31 +0000 | [diff] [blame] | 269 | if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) { |
Matthias Braun | fe82f4a | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 270 | ContainsSuperReg = true; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | if (ContainsSuperReg) |
| 275 | continue; |
| 276 | MBB.addLiveIn(Reg); |
| 277 | } |
| 278 | } |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 279 | |
Krzysztof Parzyszek | 0ca0dda | 2017-09-14 15:53:11 +0000 | [diff] [blame] | 280 | void llvm::recomputeLivenessFlags(MachineBasicBlock &MBB) { |
| 281 | const MachineFunction &MF = *MBB.getParent(); |
| 282 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 283 | const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); |
| 284 | |
| 285 | // We walk through the block backwards and start with the live outs. |
| 286 | LivePhysRegs LiveRegs; |
| 287 | LiveRegs.init(TRI); |
| 288 | LiveRegs.addLiveOutsNoPristines(MBB); |
| 289 | |
| 290 | for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) { |
| 291 | // Recompute dead flags. |
| 292 | for (MIBundleOperands MO(MI); MO.isValid(); ++MO) { |
| 293 | if (!MO->isReg() || !MO->isDef() || MO->isDebug()) |
| 294 | continue; |
| 295 | |
| 296 | unsigned Reg = MO->getReg(); |
| 297 | if (Reg == 0) |
| 298 | continue; |
| 299 | assert(TargetRegisterInfo::isPhysicalRegister(Reg)); |
| 300 | |
| 301 | bool IsNotLive = LiveRegs.available(MRI, Reg); |
| 302 | MO->setIsDead(IsNotLive); |
| 303 | } |
| 304 | |
| 305 | // Step backward over defs. |
| 306 | LiveRegs.removeDefs(MI); |
| 307 | |
| 308 | // Recompute kill flags. |
| 309 | for (MIBundleOperands MO(MI); MO.isValid(); ++MO) { |
| 310 | if (!MO->isReg() || !MO->readsReg() || MO->isDebug()) |
| 311 | continue; |
| 312 | |
| 313 | unsigned Reg = MO->getReg(); |
| 314 | if (Reg == 0) |
| 315 | continue; |
| 316 | assert(TargetRegisterInfo::isPhysicalRegister(Reg)); |
| 317 | |
| 318 | bool IsNotLive = LiveRegs.available(MRI, Reg); |
| 319 | MO->setIsKill(IsNotLive); |
| 320 | } |
| 321 | |
| 322 | // Complete the stepbackward. |
| 323 | LiveRegs.addUses(MI); |
| 324 | } |
| 325 | } |
| 326 | |
Matthias Braun | 109c6e0 | 2017-09-06 20:45:24 +0000 | [diff] [blame] | 327 | void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs, |
| 328 | MachineBasicBlock &MBB) { |
| 329 | computeLiveIns(LiveRegs, MBB); |
| 330 | addLiveIns(MBB, LiveRegs); |
| 331 | } |