1*9880d681SAndroid Build Coastguard Worker //===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the LivePhysRegs utility for tracking liveness of
11*9880d681SAndroid Build Coastguard Worker // physical registers across machine instructions in forward or backward order.
12*9880d681SAndroid Build Coastguard Worker // A more detailed description can be found in the corresponding header file.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LivePhysRegs.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBundle.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker /// \brief Remove all registers from the set that get clobbered by the register
27*9880d681SAndroid Build Coastguard Worker /// mask.
28*9880d681SAndroid Build Coastguard Worker /// The clobbers set will be the list of live registers clobbered
29*9880d681SAndroid Build Coastguard Worker /// by the regmask.
removeRegsInMask(const MachineOperand & MO,SmallVectorImpl<std::pair<unsigned,const MachineOperand * >> * Clobbers)30*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
31*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
32*9880d681SAndroid Build Coastguard Worker SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
33*9880d681SAndroid Build Coastguard Worker while (LRI != LiveRegs.end()) {
34*9880d681SAndroid Build Coastguard Worker if (MO.clobbersPhysReg(*LRI)) {
35*9880d681SAndroid Build Coastguard Worker if (Clobbers)
36*9880d681SAndroid Build Coastguard Worker Clobbers->push_back(std::make_pair(*LRI, &MO));
37*9880d681SAndroid Build Coastguard Worker LRI = LiveRegs.erase(LRI);
38*9880d681SAndroid Build Coastguard Worker } else
39*9880d681SAndroid Build Coastguard Worker ++LRI;
40*9880d681SAndroid Build Coastguard Worker }
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker /// Simulates liveness when stepping backwards over an instruction(bundle):
44*9880d681SAndroid Build Coastguard Worker /// Remove Defs, add uses. This is the recommended way of calculating liveness.
stepBackward(const MachineInstr & MI)45*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::stepBackward(const MachineInstr &MI) {
46*9880d681SAndroid Build Coastguard Worker // Remove defined registers and regmask kills from the set.
47*9880d681SAndroid Build Coastguard Worker for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
48*9880d681SAndroid Build Coastguard Worker if (O->isReg()) {
49*9880d681SAndroid Build Coastguard Worker if (!O->isDef())
50*9880d681SAndroid Build Coastguard Worker continue;
51*9880d681SAndroid Build Coastguard Worker unsigned Reg = O->getReg();
52*9880d681SAndroid Build Coastguard Worker if (Reg == 0)
53*9880d681SAndroid Build Coastguard Worker continue;
54*9880d681SAndroid Build Coastguard Worker removeReg(Reg);
55*9880d681SAndroid Build Coastguard Worker } else if (O->isRegMask())
56*9880d681SAndroid Build Coastguard Worker removeRegsInMask(*O, nullptr);
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker // Add uses to the set.
60*9880d681SAndroid Build Coastguard Worker for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
61*9880d681SAndroid Build Coastguard Worker if (!O->isReg() || !O->readsReg())
62*9880d681SAndroid Build Coastguard Worker continue;
63*9880d681SAndroid Build Coastguard Worker unsigned Reg = O->getReg();
64*9880d681SAndroid Build Coastguard Worker if (Reg == 0)
65*9880d681SAndroid Build Coastguard Worker continue;
66*9880d681SAndroid Build Coastguard Worker addReg(Reg);
67*9880d681SAndroid Build Coastguard Worker }
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker /// Simulates liveness when stepping forward over an instruction(bundle): Remove
71*9880d681SAndroid Build Coastguard Worker /// killed-uses, add defs. This is the not recommended way, because it depends
72*9880d681SAndroid Build Coastguard Worker /// on accurate kill flags. If possible use stepBackward() instead of this
73*9880d681SAndroid Build Coastguard Worker /// function.
stepForward(const MachineInstr & MI,SmallVectorImpl<std::pair<unsigned,const MachineOperand * >> & Clobbers)74*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::stepForward(const MachineInstr &MI,
75*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
76*9880d681SAndroid Build Coastguard Worker // Remove killed registers from the set.
77*9880d681SAndroid Build Coastguard Worker for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
78*9880d681SAndroid Build Coastguard Worker if (O->isReg()) {
79*9880d681SAndroid Build Coastguard Worker unsigned Reg = O->getReg();
80*9880d681SAndroid Build Coastguard Worker if (Reg == 0)
81*9880d681SAndroid Build Coastguard Worker continue;
82*9880d681SAndroid Build Coastguard Worker if (O->isDef()) {
83*9880d681SAndroid Build Coastguard Worker // Note, dead defs are still recorded. The caller should decide how to
84*9880d681SAndroid Build Coastguard Worker // handle them.
85*9880d681SAndroid Build Coastguard Worker Clobbers.push_back(std::make_pair(Reg, &*O));
86*9880d681SAndroid Build Coastguard Worker } else {
87*9880d681SAndroid Build Coastguard Worker if (!O->isKill())
88*9880d681SAndroid Build Coastguard Worker continue;
89*9880d681SAndroid Build Coastguard Worker assert(O->isUse());
90*9880d681SAndroid Build Coastguard Worker removeReg(Reg);
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker } else if (O->isRegMask())
93*9880d681SAndroid Build Coastguard Worker removeRegsInMask(*O, &Clobbers);
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker // Add defs to the set.
97*9880d681SAndroid Build Coastguard Worker for (auto Reg : Clobbers) {
98*9880d681SAndroid Build Coastguard Worker // Skip dead defs. They shouldn't be added to the set.
99*9880d681SAndroid Build Coastguard Worker if (Reg.second->isReg() && Reg.second->isDead())
100*9880d681SAndroid Build Coastguard Worker continue;
101*9880d681SAndroid Build Coastguard Worker addReg(Reg.first);
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker /// Prin the currently live registers to OS.
print(raw_ostream & OS) const106*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::print(raw_ostream &OS) const {
107*9880d681SAndroid Build Coastguard Worker OS << "Live Registers:";
108*9880d681SAndroid Build Coastguard Worker if (!TRI) {
109*9880d681SAndroid Build Coastguard Worker OS << " (uninitialized)\n";
110*9880d681SAndroid Build Coastguard Worker return;
111*9880d681SAndroid Build Coastguard Worker }
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker if (empty()) {
114*9880d681SAndroid Build Coastguard Worker OS << " (empty)\n";
115*9880d681SAndroid Build Coastguard Worker return;
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker for (const_iterator I = begin(), E = end(); I != E; ++I)
119*9880d681SAndroid Build Coastguard Worker OS << " " << PrintReg(*I, TRI);
120*9880d681SAndroid Build Coastguard Worker OS << "\n";
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker /// Dumps the currently live registers to the debug output.
dump() const124*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void LivePhysRegs::dump() const {
125*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
126*9880d681SAndroid Build Coastguard Worker dbgs() << " " << *this;
127*9880d681SAndroid Build Coastguard Worker #endif
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker
available(const MachineRegisterInfo & MRI,unsigned Reg) const130*9880d681SAndroid Build Coastguard Worker bool LivePhysRegs::available(const MachineRegisterInfo &MRI,
131*9880d681SAndroid Build Coastguard Worker unsigned Reg) const {
132*9880d681SAndroid Build Coastguard Worker if (LiveRegs.count(Reg))
133*9880d681SAndroid Build Coastguard Worker return false;
134*9880d681SAndroid Build Coastguard Worker if (MRI.isReserved(Reg))
135*9880d681SAndroid Build Coastguard Worker return false;
136*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) {
137*9880d681SAndroid Build Coastguard Worker if (LiveRegs.count(*R))
138*9880d681SAndroid Build Coastguard Worker return false;
139*9880d681SAndroid Build Coastguard Worker }
140*9880d681SAndroid Build Coastguard Worker return true;
141*9880d681SAndroid Build Coastguard Worker }
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker /// Add live-in registers of basic block \p MBB to \p LiveRegs.
addLiveIns(LivePhysRegs & LiveRegs,const MachineBasicBlock & MBB)144*9880d681SAndroid Build Coastguard Worker static void addLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB) {
145*9880d681SAndroid Build Coastguard Worker for (const auto &LI : MBB.liveins())
146*9880d681SAndroid Build Coastguard Worker LiveRegs.addReg(LI.PhysReg);
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker /// Add pristine registers to the given \p LiveRegs. This function removes
150*9880d681SAndroid Build Coastguard Worker /// actually saved callee save registers when \p InPrologueEpilogue is false.
addPristines(LivePhysRegs & LiveRegs,const MachineFunction & MF,const MachineFrameInfo & MFI,const TargetRegisterInfo & TRI)151*9880d681SAndroid Build Coastguard Worker static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF,
152*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo &MFI,
153*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI) {
154*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
155*9880d681SAndroid Build Coastguard Worker LiveRegs.addReg(*CSR);
156*9880d681SAndroid Build Coastguard Worker for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
157*9880d681SAndroid Build Coastguard Worker LiveRegs.removeReg(Info.getReg());
158*9880d681SAndroid Build Coastguard Worker }
159*9880d681SAndroid Build Coastguard Worker
addLiveOutsNoPristines(const MachineBasicBlock & MBB)160*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock &MBB) {
161*9880d681SAndroid Build Coastguard Worker // To get the live-outs we simply merge the live-ins of all successors.
162*9880d681SAndroid Build Coastguard Worker for (const MachineBasicBlock *Succ : MBB.successors())
163*9880d681SAndroid Build Coastguard Worker ::addLiveIns(*this, *Succ);
164*9880d681SAndroid Build Coastguard Worker }
165*9880d681SAndroid Build Coastguard Worker
addLiveOuts(const MachineBasicBlock & MBB)166*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::addLiveOuts(const MachineBasicBlock &MBB) {
167*9880d681SAndroid Build Coastguard Worker const MachineFunction &MF = *MBB.getParent();
168*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo &MFI = *MF.getFrameInfo();
169*9880d681SAndroid Build Coastguard Worker if (MFI.isCalleeSavedInfoValid()) {
170*9880d681SAndroid Build Coastguard Worker if (MBB.isReturnBlock()) {
171*9880d681SAndroid Build Coastguard Worker // The return block has no successors whose live-ins we could merge
172*9880d681SAndroid Build Coastguard Worker // below. So instead we add the callee saved registers manually.
173*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I)
174*9880d681SAndroid Build Coastguard Worker addReg(*I);
175*9880d681SAndroid Build Coastguard Worker } else {
176*9880d681SAndroid Build Coastguard Worker addPristines(*this, MF, MFI, *TRI);
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker addLiveOutsNoPristines(MBB);
181*9880d681SAndroid Build Coastguard Worker }
182*9880d681SAndroid Build Coastguard Worker
addLiveIns(const MachineBasicBlock & MBB)183*9880d681SAndroid Build Coastguard Worker void LivePhysRegs::addLiveIns(const MachineBasicBlock &MBB) {
184*9880d681SAndroid Build Coastguard Worker const MachineFunction &MF = *MBB.getParent();
185*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo &MFI = *MF.getFrameInfo();
186*9880d681SAndroid Build Coastguard Worker if (MFI.isCalleeSavedInfoValid())
187*9880d681SAndroid Build Coastguard Worker addPristines(*this, MF, MFI, *TRI);
188*9880d681SAndroid Build Coastguard Worker ::addLiveIns(*this, MBB);
189*9880d681SAndroid Build Coastguard Worker }
190