xref: /llvm-project/llvm/lib/CodeGen/LiveRegUnits.cpp (revision 91f95f0201cf2e0b893a1e0e24b7ff6c575cb5f8)
1 //===--- LiveRegUnits.cpp - Register Unit 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 /// \file This file imlements the LiveRegUnits set.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/LiveRegUnits.h"
15 #include "llvm/CodeGen/MachineFrameInfo.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineInstrBundle.h"
18 using namespace llvm;
19 
20 void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
21   for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
22     for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
23       if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
24         Units.reset(U);
25     }
26   }
27 }
28 
29 void LiveRegUnits::stepBackward(const MachineInstr &MI) {
30   // Remove defined registers and regmask kills from the set.
31   for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
32     if (O->isReg()) {
33       if (!O->isDef())
34         continue;
35       unsigned Reg = O->getReg();
36       if (!TargetRegisterInfo::isPhysicalRegister(Reg))
37         continue;
38       removeReg(Reg);
39     } else if (O->isRegMask())
40       removeRegsNotPreserved(O->getRegMask());
41   }
42 
43   // Add uses to the set.
44   for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
45     if (!O->isReg() || !O->readsReg())
46       continue;
47     unsigned Reg = O->getReg();
48     if (!TargetRegisterInfo::isPhysicalRegister(Reg))
49       continue;
50     addReg(Reg);
51   }
52 }
53 
54 /// Add live-in registers of basic block \p MBB to \p LiveUnits.
55 static void addLiveIns(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB) {
56   for (const auto &LI : MBB.liveins())
57     LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
58 }
59 
60 static void addLiveOuts(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB) {
61   // To get the live-outs we simply merge the live-ins of all successors.
62   for (const MachineBasicBlock *Succ : MBB.successors())
63     addLiveIns(LiveUnits, *Succ);
64 }
65 
66 /// Add pristine registers to the given \p LiveUnits. This function removes
67 /// actually saved callee save registers when \p InPrologueEpilogue is false.
68 static void removeSavedRegs(LiveRegUnits &LiveUnits, const MachineFunction &MF,
69                             const MachineFrameInfo &MFI,
70                             const TargetRegisterInfo &TRI) {
71   for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
72     LiveUnits.removeReg(Info.getReg());
73 }
74 
75 void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
76   const MachineFunction &MF = *MBB.getParent();
77   const MachineFrameInfo &MFI = MF.getFrameInfo();
78   if (MFI.isCalleeSavedInfoValid()) {
79     for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I)
80       addReg(*I);
81     if (!MBB.isReturnBlock())
82       removeSavedRegs(*this, MF, MFI, *TRI);
83   }
84   ::addLiveOuts(*this, MBB);
85 }
86 
87 void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
88   const MachineFunction &MF = *MBB.getParent();
89   const MachineFrameInfo &MFI = MF.getFrameInfo();
90   if (MFI.isCalleeSavedInfoValid()) {
91     for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I)
92       addReg(*I);
93     if (&MBB != &MF.front())
94       removeSavedRegs(*this, MF, MFI, *TRI);
95   }
96   ::addLiveIns(*this, MBB);
97 }
98