blob: 277212cf7dac248bbe78882d5483f8e07c1ba51f [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
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +000043/// Remove defined registers and regmask kills from the set.
44void LivePhysRegs::removeDefs(const MachineInstr &MI) {
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +000045 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000046 if (O->isReg()) {
47 if (!O->isDef())
48 continue;
49 unsigned Reg = O->getReg();
Krzysztof Parzyszeke513e172016-10-07 14:50:49 +000050 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka310034e2013-12-14 06:52:56 +000051 continue;
52 removeReg(Reg);
53 } else if (O->isRegMask())
Matthias Braun61cf1a92017-05-26 21:50:51 +000054 removeRegsInMask(*O);
Juergen Ributzka310034e2013-12-14 06:52:56 +000055 }
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +000056}
Juergen Ributzka310034e2013-12-14 06:52:56 +000057
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +000058/// Add uses to the set.
59void LivePhysRegs::addUses(const MachineInstr &MI) {
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
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +000070/// Simulates liveness when stepping backwards over an instruction(bundle):
71/// Remove Defs, add uses. This is the recommended way of calculating liveness.
72void LivePhysRegs::stepBackward(const MachineInstr &MI) {
73 // Remove defined registers and regmask kills from the set.
74 removeDefs(MI);
75
76 // Add uses to the set.
77 addUses(MI);
78}
79
Juergen Ributzka310034e2013-12-14 06:52:56 +000080/// Simulates liveness when stepping forward over an instruction(bundle): Remove
81/// killed-uses, add defs. This is the not recommended way, because it depends
Chad Rosiera67b2d02015-09-04 12:34:55 +000082/// on accurate kill flags. If possible use stepBackward() instead of this
Juergen Ributzka310034e2013-12-14 06:52:56 +000083/// function.
Pete Cooper7605e372015-05-05 20:14:22 +000084void LivePhysRegs::stepForward(const MachineInstr &MI,
85 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000086 // Remove killed registers from the set.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +000087 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000088 if (O->isReg()) {
89 unsigned Reg = O->getReg();
Krzysztof Parzyszeke513e172016-10-07 14:50:49 +000090 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka310034e2013-12-14 06:52:56 +000091 continue;
92 if (O->isDef()) {
Pete Cooper27483912015-05-06 22:51:04 +000093 // Note, dead defs are still recorded. The caller should decide how to
94 // handle them.
95 Clobbers.push_back(std::make_pair(Reg, &*O));
Juergen Ributzka310034e2013-12-14 06:52:56 +000096 } else {
97 if (!O->isKill())
98 continue;
99 assert(O->isUse());
100 removeReg(Reg);
101 }
102 } else if (O->isRegMask())
Pete Cooper7605e372015-05-05 20:14:22 +0000103 removeRegsInMask(*O, &Clobbers);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000104 }
105
106 // Add defs to the set.
Pete Cooper27483912015-05-06 22:51:04 +0000107 for (auto Reg : Clobbers) {
108 // Skip dead defs. They shouldn't be added to the set.
109 if (Reg.second->isReg() && Reg.second->isDead())
110 continue;
Pete Cooper7605e372015-05-05 20:14:22 +0000111 addReg(Reg.first);
Pete Cooper27483912015-05-06 22:51:04 +0000112 }
Juergen Ributzka310034e2013-12-14 06:52:56 +0000113}
114
115/// Prin the currently live registers to OS.
116void 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 Mistrih9d419d32017-11-28 12:42:37 +0000129 OS << " " << printReg(*I, TRI);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000130 OS << "\n";
131}
132
Aaron Ballman615eb472017-10-15 14:32:27 +0000133#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000134LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000135 dbgs() << " " << *this;
Juergen Ributzka310034e2013-12-14 06:52:56 +0000136}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000137#endif
Matthias Braune1cd96b2015-07-01 17:17:17 +0000138
Matthias Braun332bb5c2016-07-06 21:31:27 +0000139bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
140 unsigned Reg) const {
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 Braune1cd96b2015-07-01 17:17:17 +0000152/// Add live-in registers of basic block \p MBB to \p LiveRegs.
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000153void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
154 for (const auto &LI : MBB.liveins()) {
Matthias Brauneec1f362017-05-26 16:23:08 +0000155 unsigned Reg = LI.PhysReg;
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 Parzyszekabc06622016-10-12 22:53:41 +0000161 continue;
162 }
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000163 for (; S.isValid(); ++S) {
164 unsigned SI = S.getSubRegIndex();
Matthias Brauneec1f362017-05-26 16:23:08 +0000165 if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000166 addReg(S.getSubReg());
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000167 }
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000168 }
Matthias Braune1cd96b2015-07-01 17:17:17 +0000169}
170
Matthias Brauneec1f362017-05-26 16:23:08 +0000171/// Adds all callee saved registers to \p LiveRegs.
172static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
173 const MachineFunction &MF) {
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000174 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Brauneec1f362017-05-26 16:23:08 +0000175 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
Matthias Braune1cd96b2015-07-01 17:17:17 +0000176 LiveRegs.addReg(*CSR);
Matthias Brauneec1f362017-05-26 16:23:08 +0000177}
178
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000179void LivePhysRegs::addPristines(const MachineFunction &MF) {
Matthias Brauneec1f362017-05-26 16:23:08 +0000180 const MachineFrameInfo &MFI = MF.getFrameInfo();
181 if (!MFI.isCalleeSavedInfoValid())
182 return;
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000183 /// 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 Brauneec1f362017-05-26 16:23:08 +0000197 /// Add all callee saved regs, then remove the ones that are saved+restored.
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000198 LivePhysRegs Pristine(*TRI);
199 addCalleeSavedRegs(Pristine, MF);
Matthias Brauneec1f362017-05-26 16:23:08 +0000200 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braune1cd96b2015-07-01 17:17:17 +0000201 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000202 Pristine.removeReg(Info.getReg());
203 for (MCPhysReg R : Pristine)
204 addReg(R);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000205}
206
Matthias Braund1aabb22016-05-03 00:24:32 +0000207void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
Eli Friedman98f8bba2018-02-06 23:00:17 +0000208 // 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 Brauneec1f362017-05-26 16:23:08 +0000220 const MachineFunction &MF = *MBB.getParent();
221 const MachineFrameInfo &MFI = MF.getFrameInfo();
222 if (MFI.isCalleeSavedInfoValid()) {
223 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +0000224 if (Info.isRestored())
225 addReg(Info.getReg());
Matthias Brauneec1f362017-05-26 16:23:08 +0000226 }
227 }
Matthias Braun24f26e62016-05-03 00:08:46 +0000228}
229
Matthias Braund1aabb22016-05-03 00:24:32 +0000230void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
Matthias Braun4e8624d2017-06-03 00:26:35 +0000231 const MachineFunction &MF = *MBB.getParent();
Eli Friedman98f8bba2018-02-06 23:00:17 +0000232 addPristines(MF);
233 addLiveOutsNoPristines(MBB);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000234}
235
Matthias Braund1aabb22016-05-03 00:24:32 +0000236void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
237 const MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000238 addPristines(MF);
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000239 addBlockLiveIns(MBB);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000240}
Matthias Braun18198302016-12-16 23:55:37 +0000241
Matthias Braune51c4352017-05-26 06:32:31 +0000242void llvm::computeLiveIns(LivePhysRegs &LiveRegs,
Matthias Braunc9056b82017-09-06 20:45:24 +0000243 const MachineBasicBlock &MBB) {
244 const MachineFunction &MF = *MBB.getParent();
245 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Braune51c4352017-05-26 06:32:31 +0000246 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Matthias Braun18198302016-12-16 23:55:37 +0000247 LiveRegs.init(TRI);
248 LiveRegs.addLiveOutsNoPristines(MBB);
Matthias Braunc9056b82017-09-06 20:45:24 +0000249 for (const MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
Matthias Braun18198302016-12-16 23:55:37 +0000250 LiveRegs.stepBackward(MI);
Matthias Braunc9056b82017-09-06 20:45:24 +0000251}
Matthias Braun18198302016-12-16 23:55:37 +0000252
Matthias Braunc9056b82017-09-06 20:45:24 +0000253void 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 Braune51c4352017-05-26 06:32:31 +0000259 if (MRI.isReserved(Reg))
260 continue;
Matthias Braun18198302016-12-16 23:55:37 +0000261 // 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 Braune51c4352017-05-26 06:32:31 +0000264 if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) {
Matthias Braun18198302016-12-16 23:55:37 +0000265 ContainsSuperReg = true;
266 break;
267 }
268 }
269 if (ContainsSuperReg)
270 continue;
271 MBB.addLiveIn(Reg);
272 }
273}
Matthias Braunc9056b82017-09-06 20:45:24 +0000274
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +0000275void llvm::recomputeLivenessFlags(MachineBasicBlock &MBB) {
276 const MachineFunction &MF = *MBB.getParent();
277 const MachineRegisterInfo &MRI = MF.getRegInfo();
278 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
279
280 // We walk through the block backwards and start with the live outs.
281 LivePhysRegs LiveRegs;
282 LiveRegs.init(TRI);
283 LiveRegs.addLiveOutsNoPristines(MBB);
284
285 for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
286 // Recompute dead flags.
287 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
288 if (!MO->isReg() || !MO->isDef() || MO->isDebug())
289 continue;
290
291 unsigned Reg = MO->getReg();
292 if (Reg == 0)
293 continue;
294 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
295
296 bool IsNotLive = LiveRegs.available(MRI, Reg);
297 MO->setIsDead(IsNotLive);
298 }
299
300 // Step backward over defs.
301 LiveRegs.removeDefs(MI);
302
303 // Recompute kill flags.
304 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
305 if (!MO->isReg() || !MO->readsReg() || MO->isDebug())
306 continue;
307
308 unsigned Reg = MO->getReg();
309 if (Reg == 0)
310 continue;
311 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
312
313 bool IsNotLive = LiveRegs.available(MRI, Reg);
314 MO->setIsKill(IsNotLive);
315 }
316
317 // Complete the stepbackward.
318 LiveRegs.addUses(MI);
319 }
320}
321
Matthias Braunc9056b82017-09-06 20:45:24 +0000322void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs,
323 MachineBasicBlock &MBB) {
324 computeLiveIns(LiveRegs, MBB);
325 addLiveIns(MBB, LiveRegs);
326}