Juergen Ributzka | 310034e | 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 | 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" |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | de9f090 | 2015-03-23 18:23:08 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | |
| 26 | /// \brief Remove all registers from the set that get clobbered by the register |
| 27 | /// mask. |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 28 | /// The clobbers set will be the list of live registers clobbered |
| 29 | /// by the regmask. |
| 30 | void LivePhysRegs::removeRegsInMask(const MachineOperand &MO, |
| 31 | SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) { |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 32 | SparseSet<unsigned>::iterator LRI = LiveRegs.begin(); |
| 33 | while (LRI != LiveRegs.end()) { |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 34 | if (MO.clobbersPhysReg(*LRI)) { |
| 35 | if (Clobbers) |
| 36 | Clobbers->push_back(std::make_pair(*LRI, &MO)); |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 37 | LRI = LiveRegs.erase(LRI); |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 38 | } else |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 39 | ++LRI; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Simulates liveness when stepping backwards over an instruction(bundle): |
| 44 | /// Remove Defs, add uses. This is the recommended way of calculating liveness. |
| 45 | void LivePhysRegs::stepBackward(const MachineInstr &MI) { |
| 46 | // Remove defined registers and regmask kills from the set. |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 47 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 48 | if (O->isReg()) { |
| 49 | if (!O->isDef()) |
| 50 | continue; |
| 51 | unsigned Reg = O->getReg(); |
Krzysztof Parzyszek | e513e17 | 2016-10-07 14:50:49 +0000 | [diff] [blame] | 52 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 53 | continue; |
| 54 | removeReg(Reg); |
| 55 | } else if (O->isRegMask()) |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 56 | removeRegsInMask(*O, nullptr); |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Add uses to the set. |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 60 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Matthias Braun | 3bb0fcc | 2016-04-06 02:46:04 +0000 | [diff] [blame] | 61 | if (!O->isReg() || !O->readsReg()) |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 62 | continue; |
| 63 | unsigned Reg = O->getReg(); |
Krzysztof Parzyszek | e513e17 | 2016-10-07 14:50:49 +0000 | [diff] [blame] | 64 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 65 | continue; |
| 66 | addReg(Reg); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Simulates liveness when stepping forward over an instruction(bundle): Remove |
| 71 | /// 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] | 72 | /// on accurate kill flags. If possible use stepBackward() instead of this |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 73 | /// function. |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 74 | void LivePhysRegs::stepForward(const MachineInstr &MI, |
| 75 | SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) { |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 76 | // Remove killed registers from the set. |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 77 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 78 | if (O->isReg()) { |
| 79 | unsigned Reg = O->getReg(); |
Krzysztof Parzyszek | e513e17 | 2016-10-07 14:50:49 +0000 | [diff] [blame] | 80 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 81 | continue; |
| 82 | if (O->isDef()) { |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 83 | // Note, dead defs are still recorded. The caller should decide how to |
| 84 | // handle them. |
| 85 | Clobbers.push_back(std::make_pair(Reg, &*O)); |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 86 | } else { |
| 87 | if (!O->isKill()) |
| 88 | continue; |
| 89 | assert(O->isUse()); |
| 90 | removeReg(Reg); |
| 91 | } |
| 92 | } else if (O->isRegMask()) |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 93 | removeRegsInMask(*O, &Clobbers); |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Add defs to the set. |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 97 | for (auto Reg : Clobbers) { |
| 98 | // Skip dead defs. They shouldn't be added to the set. |
| 99 | if (Reg.second->isReg() && Reg.second->isDead()) |
| 100 | continue; |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 101 | addReg(Reg.first); |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 102 | } |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /// Prin the currently live registers to OS. |
| 106 | void LivePhysRegs::print(raw_ostream &OS) const { |
| 107 | OS << "Live Registers:"; |
| 108 | if (!TRI) { |
| 109 | OS << " (uninitialized)\n"; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | if (empty()) { |
| 114 | OS << " (empty)\n"; |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 119 | OS << " " << PrintReg(*I, TRI); |
| 120 | OS << "\n"; |
| 121 | } |
| 122 | |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 123 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 124 | LLVM_DUMP_METHOD void LivePhysRegs::dump() const { |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 125 | dbgs() << " " << *this; |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 126 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 127 | #endif |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 128 | |
Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 129 | bool LivePhysRegs::available(const MachineRegisterInfo &MRI, |
| 130 | unsigned Reg) const { |
| 131 | if (LiveRegs.count(Reg)) |
| 132 | return false; |
| 133 | if (MRI.isReserved(Reg)) |
| 134 | return false; |
| 135 | for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) { |
| 136 | if (LiveRegs.count(*R)) |
| 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 142 | /// Add live-in registers of basic block \p MBB to \p LiveRegs. |
Krzysztof Parzyszek | abc0662 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 143 | void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) { |
| 144 | for (const auto &LI : MBB.liveins()) { |
Krzysztof Parzyszek | 2717175 | 2016-10-28 20:06:37 +0000 | [diff] [blame] | 145 | MCSubRegIndexIterator S(LI.PhysReg, TRI); |
Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 146 | if (LI.LaneMask.all() || (LI.LaneMask.any() && !S.isValid())) { |
Krzysztof Parzyszek | abc0662 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 147 | addReg(LI.PhysReg); |
| 148 | continue; |
| 149 | } |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 150 | for (; S.isValid(); ++S) { |
| 151 | unsigned SI = S.getSubRegIndex(); |
Krzysztof Parzyszek | ea9f8ce | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 152 | if ((LI.LaneMask & TRI->getSubRegIndexLaneMask(SI)).any()) |
Krzysztof Parzyszek | abc0662 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 153 | addReg(S.getSubReg()); |
Krzysztof Parzyszek | 91b5cf8 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 154 | } |
Krzysztof Parzyszek | abc0662 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 155 | } |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /// Add pristine registers to the given \p LiveRegs. This function removes |
| 159 | /// actually saved callee save registers when \p InPrologueEpilogue is false. |
| 160 | static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF, |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 161 | const MachineFrameInfo &MFI, |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 162 | const TargetRegisterInfo &TRI) { |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 163 | for (const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR) |
| 164 | LiveRegs.addReg(*CSR); |
| 165 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
| 166 | LiveRegs.removeReg(Info.getReg()); |
| 167 | } |
| 168 | |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 169 | void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) { |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 170 | // To get the live-outs we simply merge the live-ins of all successors. |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 171 | for (const MachineBasicBlock *Succ : MBB.successors()) |
Krzysztof Parzyszek | abc0662 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 172 | addBlockLiveIns(*Succ); |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 175 | void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) { |
| 176 | const MachineFunction &MF = *MBB.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 177 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 178 | if (MFI.isCalleeSavedInfoValid()) { |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 179 | if (MBB.isReturnBlock()) { |
Matthias Braun | 93ab942 | 2015-09-25 23:50:53 +0000 | [diff] [blame] | 180 | // The return block has no successors whose live-ins we could merge |
| 181 | // below. So instead we add the callee saved registers manually. |
| 182 | for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) |
| 183 | addReg(*I); |
Matthias Braun | 3f80043 | 2016-07-09 01:31:36 +0000 | [diff] [blame] | 184 | } else { |
| 185 | addPristines(*this, MF, MFI, *TRI); |
Matthias Braun | 93ab942 | 2015-09-25 23:50:53 +0000 | [diff] [blame] | 186 | } |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 187 | } |
Matthias Braun | 93ab942 | 2015-09-25 23:50:53 +0000 | [diff] [blame] | 188 | |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 189 | addLiveOutsNoPristines(MBB); |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 192 | void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) { |
| 193 | const MachineFunction &MF = *MBB.getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 194 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 195 | if (MFI.isCalleeSavedInfoValid()) |
| 196 | addPristines(*this, MF, MFI, *TRI); |
Krzysztof Parzyszek | abc0662 | 2016-10-12 22:53:41 +0000 | [diff] [blame] | 197 | addBlockLiveIns(MBB); |
Matthias Braun | e1cd96b | 2015-07-01 17:17:17 +0000 | [diff] [blame] | 198 | } |
Matthias Braun | 1819830 | 2016-12-16 23:55:37 +0000 | [diff] [blame] | 199 | |
| 200 | void llvm::computeLiveIns(LivePhysRegs &LiveRegs, const TargetRegisterInfo &TRI, |
| 201 | MachineBasicBlock &MBB) { |
| 202 | assert(MBB.livein_empty()); |
| 203 | LiveRegs.init(TRI); |
| 204 | LiveRegs.addLiveOutsNoPristines(MBB); |
| 205 | for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) |
| 206 | LiveRegs.stepBackward(MI); |
| 207 | |
| 208 | for (unsigned Reg : LiveRegs) { |
| 209 | // Skip the register if we are about to add one of its super registers. |
| 210 | bool ContainsSuperReg = false; |
| 211 | for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) { |
| 212 | if (LiveRegs.contains(*SReg)) { |
| 213 | ContainsSuperReg = true; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | if (ContainsSuperReg) |
| 218 | continue; |
| 219 | MBB.addLiveIn(Reg); |
| 220 | } |
| 221 | } |