blob: f9ba4ffa6527c23ace598e0d38ce9d04dc989b5d [file] [log] [blame]
Eugene Zelenko5db84df2017-02-17 21:43:25 +00001//===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
Matthias Braun710a4c12017-01-20 00:16:14 +00002//
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"
Matthias Braun4e8624d2017-06-03 00:26:35 +000015
Eugene Zelenko5db84df2017-02-17 21:43:25 +000016#include "llvm/CodeGen/MachineBasicBlock.h"
Matthias Braun710a4c12017-01-20 00:16:14 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBundle.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000020#include "llvm/CodeGen/MachineOperand.h"
Matthias Braun4e8624d2017-06-03 00:26:35 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000022#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/Target/TargetRegisterInfo.h"
24
Matthias Braun710a4c12017-01-20 00:16:14 +000025using namespace llvm;
26
27void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
28 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
29 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
30 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
31 Units.reset(U);
32 }
33 }
34}
35
Matthias Braun28eae8f2017-01-21 02:21:04 +000036void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
37 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
38 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
39 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
40 Units.set(U);
41 }
42 }
43}
44
Matthias Braun710a4c12017-01-20 00:16:14 +000045void LiveRegUnits::stepBackward(const MachineInstr &MI) {
46 // Remove defined registers and regmask kills from the set.
47 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
48 if (O->isReg()) {
49 if (!O->isDef())
50 continue;
51 unsigned Reg = O->getReg();
52 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
53 continue;
54 removeReg(Reg);
55 } else if (O->isRegMask())
56 removeRegsNotPreserved(O->getRegMask());
57 }
58
59 // Add uses to the set.
60 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
61 if (!O->isReg() || !O->readsReg())
62 continue;
63 unsigned Reg = O->getReg();
64 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
65 continue;
66 addReg(Reg);
67 }
68}
69
Matthias Braun1b54aa52017-07-07 03:02:17 +000070void LiveRegUnits::accumulate(const MachineInstr &MI) {
Matthias Braun28eae8f2017-01-21 02:21:04 +000071 // Add defs, uses and regmask clobbers to the set.
72 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
73 if (O->isReg()) {
74 unsigned Reg = O->getReg();
75 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
76 continue;
77 if (!O->isDef() && !O->readsReg())
78 continue;
79 addReg(Reg);
80 } else if (O->isRegMask())
81 addRegsInMask(O->getRegMask());
82 }
83}
84
Matthias Braun710a4c12017-01-20 00:16:14 +000085/// Add live-in registers of basic block \p MBB to \p LiveUnits.
Matthias Braun4e8624d2017-06-03 00:26:35 +000086static void addBlockLiveIns(LiveRegUnits &LiveUnits,
87 const MachineBasicBlock &MBB) {
Matthias Braun710a4c12017-01-20 00:16:14 +000088 for (const auto &LI : MBB.liveins())
89 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
90}
91
Matthias Braun4e8624d2017-06-03 00:26:35 +000092/// Adds all callee saved registers to \p LiveUnits.
93static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
94 const MachineFunction &MF) {
95 const MachineRegisterInfo &MRI = MF.getRegInfo();
96 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
97 LiveUnits.addReg(*CSR);
Matthias Braun710a4c12017-01-20 00:16:14 +000098}
99
Matthias Braun4e8624d2017-06-03 00:26:35 +0000100/// Adds pristine registers to the given \p LiveUnits. Pristine registers are
101/// callee saved registers that are unused in the function.
102static void addPristines(LiveRegUnits &LiveUnits, const MachineFunction &MF) {
103 const MachineFrameInfo &MFI = MF.getFrameInfo();
104 if (!MFI.isCalleeSavedInfoValid())
105 return;
106 /// Add all callee saved regs, then remove the ones that are saved+restored.
107 addCalleeSavedRegs(LiveUnits, MF);
108 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braun710a4c12017-01-20 00:16:14 +0000109 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
110 LiveUnits.removeReg(Info.getReg());
111}
112
113void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
114 const MachineFunction &MF = *MBB.getParent();
Matthias Braun4e8624d2017-06-03 00:26:35 +0000115 if (!MBB.succ_empty()) {
116 addPristines(*this, MF);
117 // To get the live-outs we simply merge the live-ins of all successors.
118 for (const MachineBasicBlock *Succ : MBB.successors())
119 addBlockLiveIns(*this, *Succ);
120 } else if (MBB.isReturnBlock()) {
121 // For the return block: Add all callee saved registers.
122 const MachineFrameInfo &MFI = MF.getFrameInfo();
123 if (MFI.isCalleeSavedInfoValid())
124 addCalleeSavedRegs(*this, MF);
Matthias Braun710a4c12017-01-20 00:16:14 +0000125 }
Matthias Braun710a4c12017-01-20 00:16:14 +0000126}
127
128void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
129 const MachineFunction &MF = *MBB.getParent();
Matthias Braun4e8624d2017-06-03 00:26:35 +0000130 addPristines(*this, MF);
131 addBlockLiveIns(*this, MBB);
Matthias Braun710a4c12017-01-20 00:16:14 +0000132}