blob: cde6ccd29dfd8b5ac431854cc301212f1be92dc5 [file] [log] [blame]
Juergen Ributzka310034e2013-12-14 06:52:56 +00001//===--- 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 Braune1cd96b2015-07-01 17:17:17 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000019#include "llvm/CodeGen/MachineInstrBundle.h"
Matthias Braun332bb5c2016-07-06 21:31:27 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000021#include "llvm/Support/Debug.h"
Benjamin Kramerde9f0902015-03-23 18:23:08 +000022#include "llvm/Support/raw_ostream.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000023using namespace llvm;
24
25
26/// \brief Remove all registers from the set that get clobbered by the register
27/// mask.
Pete Cooper7605e372015-05-05 20:14:22 +000028/// The clobbers set will be the list of live registers clobbered
29/// by the regmask.
30void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
31 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000032 SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
33 while (LRI != LiveRegs.end()) {
Pete Cooper7605e372015-05-05 20:14:22 +000034 if (MO.clobbersPhysReg(*LRI)) {
35 if (Clobbers)
36 Clobbers->push_back(std::make_pair(*LRI, &MO));
Juergen Ributzka310034e2013-12-14 06:52:56 +000037 LRI = LiveRegs.erase(LRI);
Pete Cooper7605e372015-05-05 20:14:22 +000038 } else
Juergen Ributzka310034e2013-12-14 06:52:56 +000039 ++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.
45void LivePhysRegs::stepBackward(const MachineInstr &MI) {
46 // Remove defined registers and regmask kills from the set.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +000047 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000048 if (O->isReg()) {
49 if (!O->isDef())
50 continue;
51 unsigned Reg = O->getReg();
Krzysztof Parzyszeke513e172016-10-07 14:50:49 +000052 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka310034e2013-12-14 06:52:56 +000053 continue;
54 removeReg(Reg);
55 } else if (O->isRegMask())
Matthias Braun61cf1a92017-05-26 21:50:51 +000056 removeRegsInMask(*O);
Juergen Ributzka310034e2013-12-14 06:52:56 +000057 }
58
59 // Add uses to the set.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +000060 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Matthias Braun3bb0fcc2016-04-06 02:46:04 +000061 if (!O->isReg() || !O->readsReg())
Juergen Ributzka310034e2013-12-14 06:52:56 +000062 continue;
63 unsigned Reg = O->getReg();
Krzysztof Parzyszeke513e172016-10-07 14:50:49 +000064 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka310034e2013-12-14 06:52:56 +000065 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 Rosiera67b2d02015-09-04 12:34:55 +000072/// on accurate kill flags. If possible use stepBackward() instead of this
Juergen Ributzka310034e2013-12-14 06:52:56 +000073/// function.
Pete Cooper7605e372015-05-05 20:14:22 +000074void LivePhysRegs::stepForward(const MachineInstr &MI,
75 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000076 // Remove killed registers from the set.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +000077 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000078 if (O->isReg()) {
79 unsigned Reg = O->getReg();
Krzysztof Parzyszeke513e172016-10-07 14:50:49 +000080 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka310034e2013-12-14 06:52:56 +000081 continue;
82 if (O->isDef()) {
Pete Cooper27483912015-05-06 22:51:04 +000083 // 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 Ributzka310034e2013-12-14 06:52:56 +000086 } else {
87 if (!O->isKill())
88 continue;
89 assert(O->isUse());
90 removeReg(Reg);
91 }
92 } else if (O->isRegMask())
Pete Cooper7605e372015-05-05 20:14:22 +000093 removeRegsInMask(*O, &Clobbers);
Juergen Ributzka310034e2013-12-14 06:52:56 +000094 }
95
96 // Add defs to the set.
Pete Cooper27483912015-05-06 22:51:04 +000097 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 Cooper7605e372015-05-05 20:14:22 +0000101 addReg(Reg.first);
Pete Cooper27483912015-05-06 22:51:04 +0000102 }
Juergen Ributzka310034e2013-12-14 06:52:56 +0000103}
104
105/// Prin the currently live registers to OS.
106void 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 Ributzka310034e2013-12-14 06:52:56 +0000123#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000124LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000125 dbgs() << " " << *this;
Juergen Ributzka310034e2013-12-14 06:52:56 +0000126}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000127#endif
Matthias Braune1cd96b2015-07-01 17:17:17 +0000128
Matthias Braun332bb5c2016-07-06 21:31:27 +0000129bool 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 Braune1cd96b2015-07-01 17:17:17 +0000142/// Add live-in registers of basic block \p MBB to \p LiveRegs.
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000143void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
144 for (const auto &LI : MBB.liveins()) {
Matthias Brauneec1f362017-05-26 16:23:08 +0000145 unsigned Reg = LI.PhysReg;
146 LaneBitmask Mask = LI.LaneMask;
147 MCSubRegIndexIterator S(Reg, TRI);
148 assert(Mask.any() && "Invalid livein mask");
149 if (Mask.all() || !S.isValid()) {
150 addReg(Reg);
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000151 continue;
152 }
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000153 for (; S.isValid(); ++S) {
154 unsigned SI = S.getSubRegIndex();
Matthias Brauneec1f362017-05-26 16:23:08 +0000155 if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000156 addReg(S.getSubReg());
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000157 }
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000158 }
Matthias Braune1cd96b2015-07-01 17:17:17 +0000159}
160
Matthias Brauneec1f362017-05-26 16:23:08 +0000161/// Adds all callee saved registers to \p LiveRegs.
162static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
163 const MachineFunction &MF) {
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000164 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Brauneec1f362017-05-26 16:23:08 +0000165 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
Matthias Braune1cd96b2015-07-01 17:17:17 +0000166 LiveRegs.addReg(*CSR);
Matthias Brauneec1f362017-05-26 16:23:08 +0000167}
168
169/// Adds pristine registers to the given \p LiveRegs. Pristine registers are
170/// callee saved registers that are unused in the function.
171static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF) {
172 const MachineFrameInfo &MFI = MF.getFrameInfo();
173 if (!MFI.isCalleeSavedInfoValid())
174 return;
175 /// Add all callee saved regs, then remove the ones that are saved+restored.
176 addCalleeSavedRegs(LiveRegs, MF);
177 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braune1cd96b2015-07-01 17:17:17 +0000178 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
179 LiveRegs.removeReg(Info.getReg());
180}
181
Matthias Braund1aabb22016-05-03 00:24:32 +0000182void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
Matthias Brauneec1f362017-05-26 16:23:08 +0000183 if (!MBB.succ_empty()) {
184 // To get the live-outs we simply merge the live-ins of all successors.
185 for (const MachineBasicBlock *Succ : MBB.successors())
186 addBlockLiveIns(*Succ);
187 } else if (MBB.isReturnBlock()) {
188 // For the return block: Add all callee saved registers that are saved and
189 // restored (somewhere); This does not include callee saved registers that
190 // are unused and hence not saved and restored; they are called pristine.
191 const MachineFunction &MF = *MBB.getParent();
192 const MachineFrameInfo &MFI = MF.getFrameInfo();
193 if (MFI.isCalleeSavedInfoValid()) {
194 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
195 addReg(Info.getReg());
196 }
197 }
Matthias Braun24f26e62016-05-03 00:08:46 +0000198}
199
Matthias Braund1aabb22016-05-03 00:24:32 +0000200void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
Matthias Braun4e8624d2017-06-03 00:26:35 +0000201 const MachineFunction &MF = *MBB.getParent();
Matthias Brauneec1f362017-05-26 16:23:08 +0000202 if (!MBB.succ_empty()) {
Matthias Brauneec1f362017-05-26 16:23:08 +0000203 addPristines(*this, MF);
204 addLiveOutsNoPristines(MBB);
205 } else if (MBB.isReturnBlock()) {
206 // For the return block: Add all callee saved registers.
Matthias Brauneec1f362017-05-26 16:23:08 +0000207 const MachineFrameInfo &MFI = MF.getFrameInfo();
208 if (MFI.isCalleeSavedInfoValid())
209 addCalleeSavedRegs(*this, MF);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000210 }
Matthias Braune1cd96b2015-07-01 17:17:17 +0000211}
212
Matthias Braund1aabb22016-05-03 00:24:32 +0000213void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
214 const MachineFunction &MF = *MBB.getParent();
Matthias Brauneec1f362017-05-26 16:23:08 +0000215 addPristines(*this, MF);
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000216 addBlockLiveIns(MBB);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000217}
Matthias Braun18198302016-12-16 23:55:37 +0000218
Matthias Braune51c4352017-05-26 06:32:31 +0000219void llvm::computeLiveIns(LivePhysRegs &LiveRegs,
220 const MachineRegisterInfo &MRI,
Matthias Braun18198302016-12-16 23:55:37 +0000221 MachineBasicBlock &MBB) {
Matthias Braune51c4352017-05-26 06:32:31 +0000222 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Matthias Braun18198302016-12-16 23:55:37 +0000223 assert(MBB.livein_empty());
224 LiveRegs.init(TRI);
225 LiveRegs.addLiveOutsNoPristines(MBB);
226 for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
227 LiveRegs.stepBackward(MI);
228
229 for (unsigned Reg : LiveRegs) {
Matthias Braune51c4352017-05-26 06:32:31 +0000230 if (MRI.isReserved(Reg))
231 continue;
Matthias Braun18198302016-12-16 23:55:37 +0000232 // Skip the register if we are about to add one of its super registers.
233 bool ContainsSuperReg = false;
234 for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) {
Matthias Braune51c4352017-05-26 06:32:31 +0000235 if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) {
Matthias Braun18198302016-12-16 23:55:37 +0000236 ContainsSuperReg = true;
237 break;
238 }
239 }
240 if (ContainsSuperReg)
241 continue;
242 MBB.addLiveIn(Reg);
243 }
244}