blob: efbbcbe23e1501c548dd28765dac28ab23993c80 [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"
20#include "llvm/Support/Debug.h"
Benjamin Kramerde9f0902015-03-23 18:23:08 +000021#include "llvm/Support/raw_ostream.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000022using namespace llvm;
23
24
25/// \brief Remove all registers from the set that get clobbered by the register
26/// mask.
Pete Cooper7605e372015-05-05 20:14:22 +000027/// The clobbers set will be the list of live registers clobbered
28/// by the regmask.
29void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
30 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000031 SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
32 while (LRI != LiveRegs.end()) {
Pete Cooper7605e372015-05-05 20:14:22 +000033 if (MO.clobbersPhysReg(*LRI)) {
34 if (Clobbers)
35 Clobbers->push_back(std::make_pair(*LRI, &MO));
Juergen Ributzka310034e2013-12-14 06:52:56 +000036 LRI = LiveRegs.erase(LRI);
Pete Cooper7605e372015-05-05 20:14:22 +000037 } else
Juergen Ributzka310034e2013-12-14 06:52:56 +000038 ++LRI;
39 }
40}
41
42/// Simulates liveness when stepping backwards over an instruction(bundle):
43/// Remove Defs, add uses. This is the recommended way of calculating liveness.
44void LivePhysRegs::stepBackward(const MachineInstr &MI) {
45 // Remove defined registers and regmask kills from the set.
46 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
47 if (O->isReg()) {
48 if (!O->isDef())
49 continue;
50 unsigned Reg = O->getReg();
51 if (Reg == 0)
52 continue;
53 removeReg(Reg);
54 } else if (O->isRegMask())
Pete Cooper7605e372015-05-05 20:14:22 +000055 removeRegsInMask(*O, nullptr);
Juergen Ributzka310034e2013-12-14 06:52:56 +000056 }
57
58 // Add uses to the set.
59 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
60 if (!O->isReg() || !O->readsReg() || O->isUndef())
61 continue;
62 unsigned Reg = O->getReg();
63 if (Reg == 0)
64 continue;
65 addReg(Reg);
66 }
67}
68
69/// Simulates liveness when stepping forward over an instruction(bundle): Remove
70/// killed-uses, add defs. This is the not recommended way, because it depends
Chad Rosiera67b2d02015-09-04 12:34:55 +000071/// on accurate kill flags. If possible use stepBackward() instead of this
Juergen Ributzka310034e2013-12-14 06:52:56 +000072/// function.
Pete Cooper7605e372015-05-05 20:14:22 +000073void LivePhysRegs::stepForward(const MachineInstr &MI,
74 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
Juergen Ributzka310034e2013-12-14 06:52:56 +000075 // Remove killed registers from the set.
76 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
77 if (O->isReg()) {
78 unsigned Reg = O->getReg();
79 if (Reg == 0)
80 continue;
81 if (O->isDef()) {
Pete Cooper27483912015-05-06 22:51:04 +000082 // Note, dead defs are still recorded. The caller should decide how to
83 // handle them.
84 Clobbers.push_back(std::make_pair(Reg, &*O));
Juergen Ributzka310034e2013-12-14 06:52:56 +000085 } else {
86 if (!O->isKill())
87 continue;
88 assert(O->isUse());
89 removeReg(Reg);
90 }
91 } else if (O->isRegMask())
Pete Cooper7605e372015-05-05 20:14:22 +000092 removeRegsInMask(*O, &Clobbers);
Juergen Ributzka310034e2013-12-14 06:52:56 +000093 }
94
95 // Add defs to the set.
Pete Cooper27483912015-05-06 22:51:04 +000096 for (auto Reg : Clobbers) {
97 // Skip dead defs. They shouldn't be added to the set.
98 if (Reg.second->isReg() && Reg.second->isDead())
99 continue;
Pete Cooper7605e372015-05-05 20:14:22 +0000100 addReg(Reg.first);
Pete Cooper27483912015-05-06 22:51:04 +0000101 }
Juergen Ributzka310034e2013-12-14 06:52:56 +0000102}
103
104/// Prin the currently live registers to OS.
105void LivePhysRegs::print(raw_ostream &OS) const {
106 OS << "Live Registers:";
107 if (!TRI) {
108 OS << " (uninitialized)\n";
109 return;
110 }
111
112 if (empty()) {
113 OS << " (empty)\n";
114 return;
115 }
116
117 for (const_iterator I = begin(), E = end(); I != E; ++I)
118 OS << " " << PrintReg(*I, TRI);
119 OS << "\n";
120}
121
122/// Dumps the currently live registers to the debug output.
123void LivePhysRegs::dump() const {
124#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125 dbgs() << " " << *this;
126#endif
127}
Matthias Braune1cd96b2015-07-01 17:17:17 +0000128
129/// Add live-in registers of basic block \p MBB to \p LiveRegs.
130static void addLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB) {
Matthias Braund9da1622015-09-09 18:08:03 +0000131 for (const auto &LI : MBB.liveins())
132 LiveRegs.addReg(LI.PhysReg);
Matthias Braune1cd96b2015-07-01 17:17:17 +0000133}
134
135/// Add pristine registers to the given \p LiveRegs. This function removes
136/// actually saved callee save registers when \p InPrologueEpilogue is false.
137static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF,
138 const TargetRegisterInfo &TRI) {
139 const MachineFrameInfo &MFI = *MF.getFrameInfo();
140 if (!MFI.isCalleeSavedInfoValid())
141 return;
142
143 for (const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
144 LiveRegs.addReg(*CSR);
145 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
146 LiveRegs.removeReg(Info.getReg());
147}
148
149void LivePhysRegs::addLiveOuts(const MachineBasicBlock *MBB,
Matthias Braun93ab9422015-09-25 23:50:53 +0000150 bool AddPristinesAndCSRs) {
151 if (AddPristinesAndCSRs) {
Matthias Braune1cd96b2015-07-01 17:17:17 +0000152 const MachineFunction &MF = *MBB->getParent();
153 addPristines(*this, MF, *TRI);
Matthias Braun93ab9422015-09-25 23:50:53 +0000154 if (!MBB->isReturnBlock()) {
155 // The return block has no successors whose live-ins we could merge
156 // below. So instead we add the callee saved registers manually.
157 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I)
158 addReg(*I);
159 }
Matthias Braune1cd96b2015-07-01 17:17:17 +0000160 }
Matthias Braun93ab9422015-09-25 23:50:53 +0000161
162 // To get the live-outs we simply merge the live-ins of all successors.
Matthias Braune1cd96b2015-07-01 17:17:17 +0000163 for (const MachineBasicBlock *Succ : MBB->successors())
164 ::addLiveIns(*this, *Succ);
165}
166
167void LivePhysRegs::addLiveIns(const MachineBasicBlock *MBB,
168 bool AddPristines) {
169 if (AddPristines) {
170 const MachineFunction &MF = *MBB->getParent();
171 addPristines(*this, MF, *TRI);
172 }
173 ::addLiveIns(*this, *MBB);
174}