blob: 619643acb6d37853988ef386997b18045ee74520 [file] [log] [blame]
Juergen Ributzka57c38e32013-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 Braun08f65f92015-07-01 17:17:17 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
Juergen Ributzka57c38e32013-12-14 06:52:56 +000019#include "llvm/CodeGen/MachineInstrBundle.h"
Matthias Braun2a63bae2016-07-06 21:31:27 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Nico Weber0f38c602018-04-30 14:59:11 +000021#include "llvm/Config/llvm-config.h"
Juergen Ributzka57c38e32013-12-14 06:52:56 +000022#include "llvm/Support/Debug.h"
Benjamin Kramerebe87422015-03-23 18:23:08 +000023#include "llvm/Support/raw_ostream.h"
Juergen Ributzka57c38e32013-12-14 06:52:56 +000024using namespace llvm;
25
26
Adrian Prantl26b584c2018-05-01 15:54:18 +000027/// Remove all registers from the set that get clobbered by the register
Juergen Ributzka57c38e32013-12-14 06:52:56 +000028/// mask.
Pete Cooper2bce3aa2015-05-05 20:14:22 +000029/// The clobbers set will be the list of live registers clobbered
30/// by the regmask.
31void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
Matthias Braun27ad7c22018-11-06 19:00:11 +000032 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers) {
33 RegisterSet::iterator LRI = LiveRegs.begin();
Juergen Ributzka57c38e32013-12-14 06:52:56 +000034 while (LRI != LiveRegs.end()) {
Pete Cooper2bce3aa2015-05-05 20:14:22 +000035 if (MO.clobbersPhysReg(*LRI)) {
36 if (Clobbers)
37 Clobbers->push_back(std::make_pair(*LRI, &MO));
Juergen Ributzka57c38e32013-12-14 06:52:56 +000038 LRI = LiveRegs.erase(LRI);
Pete Cooper2bce3aa2015-05-05 20:14:22 +000039 } else
Juergen Ributzka57c38e32013-12-14 06:52:56 +000040 ++LRI;
41 }
42}
43
Krzysztof Parzyszek0ca0dda2017-09-14 15:53:11 +000044/// Remove defined registers and regmask kills from the set.
45void LivePhysRegs::removeDefs(const MachineInstr &MI) {
Duncan P. N. Exon Smith63ec7f02016-02-27 17:05:33 +000046 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Juergen Ributzka57c38e32013-12-14 06:52:56 +000047 if (O->isReg()) {
Matt Davisdb1b5f02018-03-19 16:06:40 +000048 if (!O->isDef() || O->isDebug())
Juergen Ributzka57c38e32013-12-14 06:52:56 +000049 continue;
50 unsigned Reg = O->getReg();
Krzysztof Parzyszek5cbeeed2016-10-07 14:50:49 +000051 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka57c38e32013-12-14 06:52:56 +000052 continue;
53 removeReg(Reg);
54 } else if (O->isRegMask())
Matthias Braun7c751142017-05-26 21:50:51 +000055 removeRegsInMask(*O);
Juergen Ributzka57c38e32013-12-14 06:52:56 +000056 }
Krzysztof Parzyszek0ca0dda2017-09-14 15:53:11 +000057}
Juergen Ributzka57c38e32013-12-14 06:52:56 +000058
Krzysztof Parzyszek0ca0dda2017-09-14 15:53:11 +000059/// Add uses to the set.
60void LivePhysRegs::addUses(const MachineInstr &MI) {
Duncan P. N. Exon Smith63ec7f02016-02-27 17:05:33 +000061 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Matt Davisdb1b5f02018-03-19 16:06:40 +000062 if (!O->isReg() || !O->readsReg() || O->isDebug())
Juergen Ributzka57c38e32013-12-14 06:52:56 +000063 continue;
64 unsigned Reg = O->getReg();
Krzysztof Parzyszek5cbeeed2016-10-07 14:50:49 +000065 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka57c38e32013-12-14 06:52:56 +000066 continue;
67 addReg(Reg);
68 }
69}
70
Krzysztof Parzyszek0ca0dda2017-09-14 15:53:11 +000071/// Simulates liveness when stepping backwards over an instruction(bundle):
72/// Remove Defs, add uses. This is the recommended way of calculating liveness.
73void LivePhysRegs::stepBackward(const MachineInstr &MI) {
74 // Remove defined registers and regmask kills from the set.
75 removeDefs(MI);
76
77 // Add uses to the set.
78 addUses(MI);
79}
80
Juergen Ributzka57c38e32013-12-14 06:52:56 +000081/// Simulates liveness when stepping forward over an instruction(bundle): Remove
82/// killed-uses, add defs. This is the not recommended way, because it depends
Chad Rosier7ab65d62015-09-04 12:34:55 +000083/// on accurate kill flags. If possible use stepBackward() instead of this
Juergen Ributzka57c38e32013-12-14 06:52:56 +000084/// function.
Pete Cooper2bce3aa2015-05-05 20:14:22 +000085void LivePhysRegs::stepForward(const MachineInstr &MI,
Matthias Braun27ad7c22018-11-06 19:00:11 +000086 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers) {
Juergen Ributzka57c38e32013-12-14 06:52:56 +000087 // Remove killed registers from the set.
Duncan P. N. Exon Smith63ec7f02016-02-27 17:05:33 +000088 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
Matt Davisdb1b5f02018-03-19 16:06:40 +000089 if (O->isReg() && !O->isDebug()) {
Juergen Ributzka57c38e32013-12-14 06:52:56 +000090 unsigned Reg = O->getReg();
Krzysztof Parzyszek5cbeeed2016-10-07 14:50:49 +000091 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Juergen Ributzka57c38e32013-12-14 06:52:56 +000092 continue;
93 if (O->isDef()) {
Pete Cooper28b0dda2015-05-06 22:51:04 +000094 // Note, dead defs are still recorded. The caller should decide how to
95 // handle them.
96 Clobbers.push_back(std::make_pair(Reg, &*O));
Juergen Ributzka57c38e32013-12-14 06:52:56 +000097 } else {
98 if (!O->isKill())
99 continue;
100 assert(O->isUse());
101 removeReg(Reg);
102 }
103 } else if (O->isRegMask())
Pete Cooper2bce3aa2015-05-05 20:14:22 +0000104 removeRegsInMask(*O, &Clobbers);
Juergen Ributzka57c38e32013-12-14 06:52:56 +0000105 }
106
107 // Add defs to the set.
Pete Cooper28b0dda2015-05-06 22:51:04 +0000108 for (auto Reg : Clobbers) {
Krzysztof Parzyszek7d1c3742018-04-30 19:38:47 +0000109 // Skip dead defs and registers clobbered by regmasks. They shouldn't
110 // be added to the set.
Pete Cooper28b0dda2015-05-06 22:51:04 +0000111 if (Reg.second->isReg() && Reg.second->isDead())
112 continue;
Krzysztof Parzyszek7d1c3742018-04-30 19:38:47 +0000113 if (Reg.second->isRegMask() &&
114 MachineOperand::clobbersPhysReg(Reg.second->getRegMask(), Reg.first))
115 continue;
Pete Cooper2bce3aa2015-05-05 20:14:22 +0000116 addReg(Reg.first);
Pete Cooper28b0dda2015-05-06 22:51:04 +0000117 }
Juergen Ributzka57c38e32013-12-14 06:52:56 +0000118}
119
120/// Prin the currently live registers to OS.
121void LivePhysRegs::print(raw_ostream &OS) const {
122 OS << "Live Registers:";
123 if (!TRI) {
124 OS << " (uninitialized)\n";
125 return;
126 }
127
128 if (empty()) {
129 OS << " (empty)\n";
130 return;
131 }
132
133 for (const_iterator I = begin(), E = end(); I != E; ++I)
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +0000134 OS << " " << printReg(*I, TRI);
Juergen Ributzka57c38e32013-12-14 06:52:56 +0000135 OS << "\n";
136}
137
Aaron Ballman1d03d382017-10-15 14:32:27 +0000138#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun88d20752017-01-28 02:02:38 +0000139LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
Juergen Ributzka57c38e32013-12-14 06:52:56 +0000140 dbgs() << " " << *this;
Juergen Ributzka57c38e32013-12-14 06:52:56 +0000141}
Matthias Braun88d20752017-01-28 02:02:38 +0000142#endif
Matthias Braun08f65f92015-07-01 17:17:17 +0000143
Matthias Braun2a63bae2016-07-06 21:31:27 +0000144bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
Matthias Braun27ad7c22018-11-06 19:00:11 +0000145 MCPhysReg Reg) const {
Matthias Braun2a63bae2016-07-06 21:31:27 +0000146 if (LiveRegs.count(Reg))
147 return false;
148 if (MRI.isReserved(Reg))
149 return false;
150 for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) {
151 if (LiveRegs.count(*R))
152 return false;
153 }
154 return true;
155}
156
Matthias Braun08f65f92015-07-01 17:17:17 +0000157/// Add live-in registers of basic block \p MBB to \p LiveRegs.
Krzysztof Parzyszek6dcccf42016-10-12 22:53:41 +0000158void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
159 for (const auto &LI : MBB.liveins()) {
Matthias Braun27ad7c22018-11-06 19:00:11 +0000160 MCPhysReg Reg = LI.PhysReg;
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000161 LaneBitmask Mask = LI.LaneMask;
162 MCSubRegIndexIterator S(Reg, TRI);
163 assert(Mask.any() && "Invalid livein mask");
164 if (Mask.all() || !S.isValid()) {
165 addReg(Reg);
Krzysztof Parzyszek6dcccf42016-10-12 22:53:41 +0000166 continue;
167 }
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000168 for (; S.isValid(); ++S) {
169 unsigned SI = S.getSubRegIndex();
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000170 if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
Krzysztof Parzyszek6dcccf42016-10-12 22:53:41 +0000171 addReg(S.getSubReg());
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000172 }
Krzysztof Parzyszek6dcccf42016-10-12 22:53:41 +0000173 }
Matthias Braun08f65f92015-07-01 17:17:17 +0000174}
175
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000176/// Adds all callee saved registers to \p LiveRegs.
177static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
178 const MachineFunction &MF) {
Oren Ben Simhon6095a792017-03-14 09:09:26 +0000179 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000180 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
Matthias Braun08f65f92015-07-01 17:17:17 +0000181 LiveRegs.addReg(*CSR);
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000182}
183
Krzysztof Parzyszek14574092017-09-08 16:29:50 +0000184void LivePhysRegs::addPristines(const MachineFunction &MF) {
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000185 const MachineFrameInfo &MFI = MF.getFrameInfo();
186 if (!MFI.isCalleeSavedInfoValid())
187 return;
Krzysztof Parzyszek14574092017-09-08 16:29:50 +0000188 /// This function will usually be called on an empty object, handle this
189 /// as a special case.
190 if (empty()) {
191 /// Add all callee saved regs, then remove the ones that are saved and
192 /// restored.
193 addCalleeSavedRegs(*this, MF);
194 /// Remove the ones that are not saved/restored; they are pristine.
195 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
196 removeReg(Info.getReg());
197 return;
198 }
199 /// If a callee-saved register that is not pristine is already present
200 /// in the set, we should make sure that it stays in it. Precompute the
201 /// set of pristine registers in a separate object.
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000202 /// Add all callee saved regs, then remove the ones that are saved+restored.
Krzysztof Parzyszek14574092017-09-08 16:29:50 +0000203 LivePhysRegs Pristine(*TRI);
204 addCalleeSavedRegs(Pristine, MF);
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000205 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braun08f65f92015-07-01 17:17:17 +0000206 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszek14574092017-09-08 16:29:50 +0000207 Pristine.removeReg(Info.getReg());
208 for (MCPhysReg R : Pristine)
209 addReg(R);
Matthias Braun08f65f92015-07-01 17:17:17 +0000210}
211
Matthias Braun02073cb2016-05-03 00:24:32 +0000212void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
Eli Friedmanc1d66ba2018-02-06 23:00:17 +0000213 // To get the live-outs we simply merge the live-ins of all successors.
214 for (const MachineBasicBlock *Succ : MBB.successors())
215 addBlockLiveIns(*Succ);
216 if (MBB.isReturnBlock()) {
217 // Return blocks are a special case because we currently don't mark up
218 // return instructions completely: specifically, there is no explicit
219 // use for callee-saved registers. So we add all callee saved registers
220 // that are saved and restored (somewhere). This does not include
221 // callee saved registers that are unused and hence not saved and
222 // restored; they are called pristine.
223 // FIXME: PEI should add explicit markings to return instructions
224 // instead of implicitly handling them here.
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000225 const MachineFunction &MF = *MBB.getParent();
226 const MachineFrameInfo &MFI = MF.getFrameInfo();
227 if (MFI.isCalleeSavedInfoValid()) {
228 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszek7b10f6e2017-08-10 16:17:32 +0000229 if (Info.isRestored())
230 addReg(Info.getReg());
Matthias Braunbfcbf6a2017-05-26 16:23:08 +0000231 }
232 }
Matthias Braunb4756d62016-05-03 00:08:46 +0000233}
234
Matthias Braun02073cb2016-05-03 00:24:32 +0000235void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
Matthias Braun70862df2017-06-03 00:26:35 +0000236 const MachineFunction &MF = *MBB.getParent();
Eli Friedmanc1d66ba2018-02-06 23:00:17 +0000237 addPristines(MF);
238 addLiveOutsNoPristines(MBB);
Matthias Braun08f65f92015-07-01 17:17:17 +0000239}
240
Matthias Braun02073cb2016-05-03 00:24:32 +0000241void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
242 const MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszek14574092017-09-08 16:29:50 +0000243 addPristines(MF);
Krzysztof Parzyszek6dcccf42016-10-12 22:53:41 +0000244 addBlockLiveIns(MBB);
Matthias Braun08f65f92015-07-01 17:17:17 +0000245}
Matthias Braunfe82f4a2016-12-16 23:55:37 +0000246
Matthias Braunb0e29ac2017-05-26 06:32:31 +0000247void llvm::computeLiveIns(LivePhysRegs &LiveRegs,
Matthias Braun109c6e02017-09-06 20:45:24 +0000248 const MachineBasicBlock &MBB) {
249 const MachineFunction &MF = *MBB.getParent();
250 const MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Braunb0e29ac2017-05-26 06:32:31 +0000251 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
Matthias Braunfe82f4a2016-12-16 23:55:37 +0000252 LiveRegs.init(TRI);
253 LiveRegs.addLiveOutsNoPristines(MBB);
Matthias Braun109c6e02017-09-06 20:45:24 +0000254 for (const MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
Matthias Braunfe82f4a2016-12-16 23:55:37 +0000255 LiveRegs.stepBackward(MI);
Matthias Braun109c6e02017-09-06 20:45:24 +0000256}
Matthias Braunfe82f4a2016-12-16 23:55:37 +0000257
Matthias Braun109c6e02017-09-06 20:45:24 +0000258void llvm::addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs) {
259 assert(MBB.livein_empty() && "Expected empty live-in list");
260 const MachineFunction &MF = *MBB.getParent();
261 const MachineRegisterInfo &MRI = MF.getRegInfo();
262 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
263 for (MCPhysReg Reg : LiveRegs) {
Matthias Braunb0e29ac2017-05-26 06:32:31 +0000264 if (MRI.isReserved(Reg))
265 continue;
Matthias Braunfe82f4a2016-12-16 23:55:37 +0000266 // Skip the register if we are about to add one of its super registers.
267 bool ContainsSuperReg = false;
268 for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) {
Matthias Braunb0e29ac2017-05-26 06:32:31 +0000269 if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) {
Matthias Braunfe82f4a2016-12-16 23:55:37 +0000270 ContainsSuperReg = true;
271 break;
272 }
273 }
274 if (ContainsSuperReg)
275 continue;
276 MBB.addLiveIn(Reg);
277 }
278}
Matthias Braun109c6e02017-09-06 20:45:24 +0000279
Krzysztof Parzyszek0ca0dda2017-09-14 15:53:11 +0000280void llvm::recomputeLivenessFlags(MachineBasicBlock &MBB) {
281 const MachineFunction &MF = *MBB.getParent();
282 const MachineRegisterInfo &MRI = MF.getRegInfo();
283 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
284
285 // We walk through the block backwards and start with the live outs.
286 LivePhysRegs LiveRegs;
287 LiveRegs.init(TRI);
288 LiveRegs.addLiveOutsNoPristines(MBB);
289
290 for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
291 // Recompute dead flags.
292 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
293 if (!MO->isReg() || !MO->isDef() || MO->isDebug())
294 continue;
295
296 unsigned Reg = MO->getReg();
297 if (Reg == 0)
298 continue;
299 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
300
301 bool IsNotLive = LiveRegs.available(MRI, Reg);
302 MO->setIsDead(IsNotLive);
303 }
304
305 // Step backward over defs.
306 LiveRegs.removeDefs(MI);
307
308 // Recompute kill flags.
309 for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
310 if (!MO->isReg() || !MO->readsReg() || MO->isDebug())
311 continue;
312
313 unsigned Reg = MO->getReg();
314 if (Reg == 0)
315 continue;
316 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
317
318 bool IsNotLive = LiveRegs.available(MRI, Reg);
319 MO->setIsKill(IsNotLive);
320 }
321
322 // Complete the stepbackward.
323 LiveRegs.addUses(MI);
324 }
325}
326
Matthias Braun109c6e02017-09-06 20:45:24 +0000327void llvm::computeAndAddLiveIns(LivePhysRegs &LiveRegs,
328 MachineBasicBlock &MBB) {
329 computeLiveIns(LiveRegs, MBB);
330 addLiveIns(MBB, LiveRegs);
331}