blob: 9836d3dc112c34ed7eabda52d5bea3a0f52769e8 [file] [log] [blame]
Juergen Ributzka310034e2013-12-14 06:52:56 +00001//===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Ributzka310034e2013-12-14 06:52:56 +00006//
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"
Matthias Braune1cd96b2015-07-01 17:17:17 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000018#include "llvm/CodeGen/MachineInstrBundle.h"
Matthias Braun332bb5c2016-07-06 21:31:27 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000020#include "llvm/Config/llvm-config.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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000026/// Remove all registers from the set that get clobbered by the register
Juergen Ributzka310034e2013-12-14 06:52:56 +000027/// 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,
Matthias Braunc6613872018-11-06 19:00:11 +000031 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers) {
32 RegisterSet::iterator LRI = LiveRegs.begin();
Juergen Ributzka310034e2013-12-14 06:52:56 +000033 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()) {
Matt Davis4b54e5f2018-03-19 16:06:40 +000047 if (!O->isDef() || O->isDebug())
Juergen Ributzka310034e2013-12-14 06:52:56 +000048 continue;
49 unsigned Reg = O->getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +000050 if (!Register::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) {
Matt Davis4b54e5f2018-03-19 16:06:40 +000061 if (!O->isReg() || !O->readsReg() || O->isDebug())
Juergen Ributzka310034e2013-12-14 06:52:56 +000062 continue;
63 unsigned Reg = O->getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +000064 if (!Register::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,
Matthias Braunc6613872018-11-06 19:00:11 +000085 SmallVectorImpl<std::pair<MCPhysReg, 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) {
Matt Davis4b54e5f2018-03-19 16:06:40 +000088 if (O->isReg() && !O->isDebug()) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000089 unsigned Reg = O->getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +000090 if (!Register::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) {
Krzysztof Parzyszek1cf329c2018-04-30 19:38:47 +0000108 // Skip dead defs and registers clobbered by regmasks. They shouldn't
109 // be added to the set.
Pete Cooper27483912015-05-06 22:51:04 +0000110 if (Reg.second->isReg() && Reg.second->isDead())
111 continue;
Krzysztof Parzyszek1cf329c2018-04-30 19:38:47 +0000112 if (Reg.second->isRegMask() &&
113 MachineOperand::clobbersPhysReg(Reg.second->getRegMask(), Reg.first))
114 continue;
Pete Cooper7605e372015-05-05 20:14:22 +0000115 addReg(Reg.first);
Pete Cooper27483912015-05-06 22:51:04 +0000116 }
Juergen Ributzka310034e2013-12-14 06:52:56 +0000117}
118
119/// Prin the currently live registers to OS.
120void LivePhysRegs::print(raw_ostream &OS) const {
121 OS << "Live Registers:";
122 if (!TRI) {
123 OS << " (uninitialized)\n";
124 return;
125 }
126
127 if (empty()) {
128 OS << " (empty)\n";
129 return;
130 }
131
132 for (const_iterator I = begin(), E = end(); I != E; ++I)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000133 OS << " " << printReg(*I, TRI);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000134 OS << "\n";
135}
136
Aaron Ballman615eb472017-10-15 14:32:27 +0000137#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000138LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000139 dbgs() << " " << *this;
Juergen Ributzka310034e2013-12-14 06:52:56 +0000140}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000141#endif
Matthias Braune1cd96b2015-07-01 17:17:17 +0000142
Matthias Braun332bb5c2016-07-06 21:31:27 +0000143bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
Matthias Braunc6613872018-11-06 19:00:11 +0000144 MCPhysReg Reg) const {
Matthias Braun332bb5c2016-07-06 21:31:27 +0000145 if (LiveRegs.count(Reg))
146 return false;
147 if (MRI.isReserved(Reg))
148 return false;
149 for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) {
150 if (LiveRegs.count(*R))
151 return false;
152 }
153 return true;
154}
155
Matthias Braune1cd96b2015-07-01 17:17:17 +0000156/// Add live-in registers of basic block \p MBB to \p LiveRegs.
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000157void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
158 for (const auto &LI : MBB.liveins()) {
Matthias Braunc6613872018-11-06 19:00:11 +0000159 MCPhysReg Reg = LI.PhysReg;
Matthias Brauneec1f362017-05-26 16:23:08 +0000160 LaneBitmask Mask = LI.LaneMask;
161 MCSubRegIndexIterator S(Reg, TRI);
162 assert(Mask.any() && "Invalid livein mask");
163 if (Mask.all() || !S.isValid()) {
164 addReg(Reg);
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000165 continue;
166 }
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000167 for (; S.isValid(); ++S) {
168 unsigned SI = S.getSubRegIndex();
Matthias Brauneec1f362017-05-26 16:23:08 +0000169 if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000170 addReg(S.getSubReg());
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000171 }
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000172 }
Matthias Braune1cd96b2015-07-01 17:17:17 +0000173}
174
Matthias Brauneec1f362017-05-26 16:23:08 +0000175/// Adds all callee saved registers to \p LiveRegs.
176static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
177 const MachineFunction &MF) {
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000178 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Brauneec1f362017-05-26 16:23:08 +0000179 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
Matthias Braune1cd96b2015-07-01 17:17:17 +0000180 LiveRegs.addReg(*CSR);
Matthias Brauneec1f362017-05-26 16:23:08 +0000181}
182
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000183void LivePhysRegs::addPristines(const MachineFunction &MF) {
Matthias Brauneec1f362017-05-26 16:23:08 +0000184 const MachineFrameInfo &MFI = MF.getFrameInfo();
185 if (!MFI.isCalleeSavedInfoValid())
186 return;
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000187 /// This function will usually be called on an empty object, handle this
188 /// as a special case.
189 if (empty()) {
190 /// Add all callee saved regs, then remove the ones that are saved and
191 /// restored.
192 addCalleeSavedRegs(*this, MF);
193 /// Remove the ones that are not saved/restored; they are pristine.
194 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
195 removeReg(Info.getReg());
196 return;
197 }
198 /// If a callee-saved register that is not pristine is already present
199 /// in the set, we should make sure that it stays in it. Precompute the
200 /// set of pristine registers in a separate object.
Matthias Brauneec1f362017-05-26 16:23:08 +0000201 /// Add all callee saved regs, then remove the ones that are saved+restored.
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000202 LivePhysRegs Pristine(*TRI);
203 addCalleeSavedRegs(Pristine, MF);
Matthias Brauneec1f362017-05-26 16:23:08 +0000204 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braune1cd96b2015-07-01 17:17:17 +0000205 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000206 Pristine.removeReg(Info.getReg());
207 for (MCPhysReg R : Pristine)
208 addReg(R);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000209}
210
Matthias Braund1aabb22016-05-03 00:24:32 +0000211void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
Eli Friedman98f8bba2018-02-06 23:00:17 +0000212 // To get the live-outs we simply merge the live-ins of all successors.
213 for (const MachineBasicBlock *Succ : MBB.successors())
214 addBlockLiveIns(*Succ);
215 if (MBB.isReturnBlock()) {
216 // Return blocks are a special case because we currently don't mark up
217 // return instructions completely: specifically, there is no explicit
218 // use for callee-saved registers. So we add all callee saved registers
219 // that are saved and restored (somewhere). This does not include
220 // callee saved registers that are unused and hence not saved and
221 // restored; they are called pristine.
222 // FIXME: PEI should add explicit markings to return instructions
223 // instead of implicitly handling them here.
Matthias Brauneec1f362017-05-26 16:23:08 +0000224 const MachineFunction &MF = *MBB.getParent();
225 const MachineFrameInfo &MFI = MF.getFrameInfo();
226 if (MFI.isCalleeSavedInfoValid()) {
227 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +0000228 if (Info.isRestored())
229 addReg(Info.getReg());
Matthias Brauneec1f362017-05-26 16:23:08 +0000230 }
231 }
Matthias Braun24f26e62016-05-03 00:08:46 +0000232}
233
Matthias Braund1aabb22016-05-03 00:24:32 +0000234void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
Matthias Braun4e8624d2017-06-03 00:26:35 +0000235 const MachineFunction &MF = *MBB.getParent();
Eli Friedman98f8bba2018-02-06 23:00:17 +0000236 addPristines(MF);
237 addLiveOutsNoPristines(MBB);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000238}
239
Matthias Braund1aabb22016-05-03 00:24:32 +0000240void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
241 const MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000242 addPristines(MF);
Krzysztof Parzyszekabc06622016-10-12 22:53:41 +0000243 addBlockLiveIns(MBB);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000244}
Matthias Braun18198302016-12-16 23:55:37 +0000245
Matthias Braune51c4352017-05-26 06:32:31 +0000246void llvm::computeLiveIns(LivePhysRegs &LiveRegs,
Matthias Braunc9056b82017-09-06 20:45:24 +0000247 const MachineBasicBlock &MBB) {
248 const MachineFunction &MF = *MBB.getParent();
249 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Braune51c4352017-05-26 06:32:31 +0000250 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Matthias Braun18198302016-12-16 23:55:37 +0000251 LiveRegs.init(TRI);
252 LiveRegs.addLiveOutsNoPristines(MBB);
Matthias Braunc9056b82017-09-06 20:45:24 +0000253 for (const MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
Matthias Braun18198302016-12-16 23:55:37 +0000254 LiveRegs.stepBackward(MI);
Matthias Braunc9056b82017-09-06 20:45:24 +0000255}
Matthias Braun18198302016-12-16 23:55:37 +0000256
Matthias Braunc9056b82017-09-06 20:45:24 +0000257void llvm::addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs) {
258 assert(MBB.livein_empty() && "Expected empty live-in list");
259 const MachineFunction &MF = *MBB.getParent();
260 const MachineRegisterInfo &MRI = MF.getRegInfo();
261 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
262 for (MCPhysReg Reg : LiveRegs) {
Matthias Braune51c4352017-05-26 06:32:31 +0000263 if (MRI.isReserved(Reg))
264 continue;
Matthias Braun18198302016-12-16 23:55:37 +0000265 // Skip the register if we are about to add one of its super registers.
266 bool ContainsSuperReg = false;
267 for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) {
Matthias Braune51c4352017-05-26 06:32:31 +0000268 if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) {
Matthias Braun18198302016-12-16 23:55:37 +0000269 ContainsSuperReg = true;
270 break;
271 }
272 }
273 if (ContainsSuperReg)
274 continue;
275 MBB.addLiveIn(Reg);
276 }
277}
Matthias Braunc9056b82017-09-06 20:45:24 +0000278
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +0000279void llvm::recomputeLivenessFlags(MachineBasicBlock &MBB) {
280 const MachineFunction &MF = *MBB.getParent();
281 const MachineRegisterInfo &MRI = MF.getRegInfo();
282 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
283
284 // We walk through the block backwards and start with the live outs.
285 LivePhysRegs LiveRegs;
286 LiveRegs.init(TRI);
287 LiveRegs.addLiveOutsNoPristines(MBB);
288
289 for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
290 // Recompute dead flags.
291 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
292 if (!MO->isReg() || !MO->isDef() || MO->isDebug())
293 continue;
294
295 unsigned Reg = MO->getReg();
296 if (Reg == 0)
297 continue;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000298 assert(Register::isPhysicalRegister(Reg));
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +0000299
300 bool IsNotLive = LiveRegs.available(MRI, Reg);
301 MO->setIsDead(IsNotLive);
302 }
303
304 // Step backward over defs.
305 LiveRegs.removeDefs(MI);
306
307 // Recompute kill flags.
308 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
309 if (!MO->isReg() || !MO->readsReg() || MO->isDebug())
310 continue;
311
312 unsigned Reg = MO->getReg();
313 if (Reg == 0)
314 continue;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000315 assert(Register::isPhysicalRegister(Reg));
Krzysztof Parzyszek6ca02b22017-09-14 15:53:11 +0000316
317 bool IsNotLive = LiveRegs.available(MRI, Reg);
318 MO->setIsKill(IsNotLive);
319 }
320
321 // Complete the stepbackward.
322 LiveRegs.addUses(MI);
323 }
324}
325
Matthias Braunc9056b82017-09-06 20:45:24 +0000326void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs,
327 MachineBasicBlock &MBB) {
328 computeLiveIns(LiveRegs, MBB);
329 addLiveIns(MBB, LiveRegs);
330}