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