1 //===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This file imlements the LiveRegUnits set.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/LiveRegUnits.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBundle.h"
19 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
25 void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
26 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
27 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
28 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
34 void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
35 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
36 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
37 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
43 void LiveRegUnits::stepBackward(const MachineInstr &MI) {
44 // Remove defined registers and regmask kills from the set.
45 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
49 unsigned Reg = O->getReg();
50 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
53 } else if (O->isRegMask())
54 removeRegsNotPreserved(O->getRegMask());
57 // Add uses to the set.
58 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
59 if (!O->isReg() || !O->readsReg())
61 unsigned Reg = O->getReg();
62 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
68 void LiveRegUnits::accumulateBackward(const MachineInstr &MI) {
69 // Add defs, uses and regmask clobbers to the set.
70 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
72 unsigned Reg = O->getReg();
73 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
75 if (!O->isDef() && !O->readsReg())
78 } else if (O->isRegMask())
79 addRegsInMask(O->getRegMask());
83 /// Add live-in registers of basic block \p MBB to \p LiveUnits.
84 static void addLiveIns(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB) {
85 for (const auto &LI : MBB.liveins())
86 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
89 static void addLiveOuts(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB) {
90 // To get the live-outs we simply merge the live-ins of all successors.
91 for (const MachineBasicBlock *Succ : MBB.successors())
92 addLiveIns(LiveUnits, *Succ);
95 /// Add pristine registers to the given \p LiveUnits. This function removes
96 /// actually saved callee save registers when \p InPrologueEpilogue is false.
97 static void removeSavedRegs(LiveRegUnits &LiveUnits, const MachineFunction &MF,
98 const MachineFrameInfo &MFI,
99 const TargetRegisterInfo &TRI) {
100 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
101 LiveUnits.removeReg(Info.getReg());
104 void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
105 const MachineFunction &MF = *MBB.getParent();
106 const MachineFrameInfo &MFI = MF.getFrameInfo();
107 if (MFI.isCalleeSavedInfoValid()) {
108 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I)
110 if (!MBB.isReturnBlock())
111 removeSavedRegs(*this, MF, MFI, *TRI);
113 ::addLiveOuts(*this, MBB);
116 void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
117 const MachineFunction &MF = *MBB.getParent();
118 const MachineFrameInfo &MFI = MF.getFrameInfo();
119 if (MFI.isCalleeSavedInfoValid()) {
120 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I)
122 if (&MBB != &MF.front())
123 removeSavedRegs(*this, MF, MFI, *TRI);
125 ::addLiveIns(*this, MBB);