15db84df7SEugene Zelenko //===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
2710a4c1fSMatthias Braun //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6710a4c1fSMatthias Braun //
7710a4c1fSMatthias Braun //===----------------------------------------------------------------------===//
8710a4c1fSMatthias Braun //
9710a4c1fSMatthias Braun /// \file This file imlements the LiveRegUnits set.
10710a4c1fSMatthias Braun //
11710a4c1fSMatthias Braun //===----------------------------------------------------------------------===//
12710a4c1fSMatthias Braun
13710a4c1fSMatthias Braun #include "llvm/CodeGen/LiveRegUnits.h"
145db84df7SEugene Zelenko #include "llvm/CodeGen/MachineBasicBlock.h"
15710a4c1fSMatthias Braun #include "llvm/CodeGen/MachineFrameInfo.h"
16710a4c1fSMatthias Braun #include "llvm/CodeGen/MachineFunction.h"
175db84df7SEugene Zelenko #include "llvm/CodeGen/MachineOperand.h"
184e8624d1SMatthias Braun #include "llvm/CodeGen/MachineRegisterInfo.h"
195db84df7SEugene Zelenko
20710a4c1fSMatthias Braun using namespace llvm;
21710a4c1fSMatthias Braun
removeRegsNotPreserved(const uint32_t * RegMask)22710a4c1fSMatthias Braun void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
23710a4c1fSMatthias Braun for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
24710a4c1fSMatthias Braun for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
25710a4c1fSMatthias Braun if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
26710a4c1fSMatthias Braun Units.reset(U);
27710a4c1fSMatthias Braun }
28710a4c1fSMatthias Braun }
29710a4c1fSMatthias Braun }
30710a4c1fSMatthias Braun
addRegsInMask(const uint32_t * RegMask)3128eae8f4SMatthias Braun void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
3228eae8f4SMatthias Braun for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
3328eae8f4SMatthias Braun for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
3428eae8f4SMatthias Braun if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
3528eae8f4SMatthias Braun Units.set(U);
3628eae8f4SMatthias Braun }
3728eae8f4SMatthias Braun }
3828eae8f4SMatthias Braun }
3928eae8f4SMatthias Braun
stepBackward(const MachineInstr & MI)40710a4c1fSMatthias Braun void LiveRegUnits::stepBackward(const MachineInstr &MI) {
41710a4c1fSMatthias Braun // Remove defined registers and regmask kills from the set.
4211f31187SFlorian Hahn for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
4311f31187SFlorian Hahn if (MOP.isRegMask()) {
4411f31187SFlorian Hahn removeRegsNotPreserved(MOP.getRegMask());
45710a4c1fSMatthias Braun continue;
4611f31187SFlorian Hahn }
4711f31187SFlorian Hahn
4811f31187SFlorian Hahn if (MOP.isDef())
4911f31187SFlorian Hahn removeReg(MOP.getReg());
50710a4c1fSMatthias Braun }
51710a4c1fSMatthias Braun
52710a4c1fSMatthias Braun // Add uses to the set.
5311f31187SFlorian Hahn for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
5411f31187SFlorian Hahn if (!MOP.isReg() || !MOP.readsReg())
55710a4c1fSMatthias Braun continue;
5611f31187SFlorian Hahn addReg(MOP.getReg());
57710a4c1fSMatthias Braun }
58710a4c1fSMatthias Braun }
59710a4c1fSMatthias Braun
accumulate(const MachineInstr & MI)601b54aa58SMatthias Braun void LiveRegUnits::accumulate(const MachineInstr &MI) {
6128eae8f4SMatthias Braun // Add defs, uses and regmask clobbers to the set.
6211f31187SFlorian Hahn for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
6311f31187SFlorian Hahn if (MOP.isRegMask()) {
6411f31187SFlorian Hahn addRegsInMask(MOP.getRegMask());
6528eae8f4SMatthias Braun continue;
6611f31187SFlorian Hahn }
6711f31187SFlorian Hahn if (!MOP.isDef() && !MOP.readsReg())
6828eae8f4SMatthias Braun continue;
6911f31187SFlorian Hahn addReg(MOP.getReg());
7028eae8f4SMatthias Braun }
7128eae8f4SMatthias Braun }
7228eae8f4SMatthias Braun
73710a4c1fSMatthias Braun /// Add live-in registers of basic block \p MBB to \p LiveUnits.
addBlockLiveIns(LiveRegUnits & LiveUnits,const MachineBasicBlock & MBB)744e8624d1SMatthias Braun static void addBlockLiveIns(LiveRegUnits &LiveUnits,
754e8624d1SMatthias Braun const MachineBasicBlock &MBB) {
76710a4c1fSMatthias Braun for (const auto &LI : MBB.liveins())
77710a4c1fSMatthias Braun LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
78710a4c1fSMatthias Braun }
79710a4c1fSMatthias Braun
804e8624d1SMatthias Braun /// Adds all callee saved registers to \p LiveUnits.
addCalleeSavedRegs(LiveRegUnits & LiveUnits,const MachineFunction & MF)814e8624d1SMatthias Braun static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
824e8624d1SMatthias Braun const MachineFunction &MF) {
834e8624d1SMatthias Braun const MachineRegisterInfo &MRI = MF.getRegInfo();
84*b9ed8ebeSTomas Matheson const MachineFrameInfo &MFI = MF.getFrameInfo();
85*b9ed8ebeSTomas Matheson for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) {
86*b9ed8ebeSTomas Matheson const unsigned N = *CSR;
87*b9ed8ebeSTomas Matheson
88*b9ed8ebeSTomas Matheson const auto &CSI = MFI.getCalleeSavedInfo();
89*b9ed8ebeSTomas Matheson auto Info =
90*b9ed8ebeSTomas Matheson llvm::find_if(CSI, [N](auto Info) { return Info.getReg() == N; });
91*b9ed8ebeSTomas Matheson // If we have no info for this callee-saved register, assume it is liveout
92*b9ed8ebeSTomas Matheson if (Info == CSI.end() || Info->isRestored())
93*b9ed8ebeSTomas Matheson LiveUnits.addReg(N);
94*b9ed8ebeSTomas Matheson }
95710a4c1fSMatthias Braun }
96710a4c1fSMatthias Braun
addPristines(const MachineFunction & MF)97f78eca8fSKrzysztof Parzyszek void LiveRegUnits::addPristines(const MachineFunction &MF) {
984e8624d1SMatthias Braun const MachineFrameInfo &MFI = MF.getFrameInfo();
994e8624d1SMatthias Braun if (!MFI.isCalleeSavedInfoValid())
1004e8624d1SMatthias Braun return;
101f78eca8fSKrzysztof Parzyszek /// This function will usually be called on an empty object, handle this
102f78eca8fSKrzysztof Parzyszek /// as a special case.
103f78eca8fSKrzysztof Parzyszek if (empty()) {
104f78eca8fSKrzysztof Parzyszek /// Add all callee saved regs, then remove the ones that are saved and
105f78eca8fSKrzysztof Parzyszek /// restored.
106f78eca8fSKrzysztof Parzyszek addCalleeSavedRegs(*this, MF);
1074e8624d1SMatthias Braun /// Remove the ones that are not saved/restored; they are pristine.
108710a4c1fSMatthias Braun for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
109f78eca8fSKrzysztof Parzyszek removeReg(Info.getReg());
110f78eca8fSKrzysztof Parzyszek return;
111f78eca8fSKrzysztof Parzyszek }
112f78eca8fSKrzysztof Parzyszek /// If a callee-saved register that is not pristine is already present
113f78eca8fSKrzysztof Parzyszek /// in the set, we should make sure that it stays in it. Precompute the
114f78eca8fSKrzysztof Parzyszek /// set of pristine registers in a separate object.
115f78eca8fSKrzysztof Parzyszek /// Add all callee saved regs, then remove the ones that are saved+restored.
116f78eca8fSKrzysztof Parzyszek LiveRegUnits Pristine(*TRI);
117f78eca8fSKrzysztof Parzyszek addCalleeSavedRegs(Pristine, MF);
118f78eca8fSKrzysztof Parzyszek /// Remove the ones that are not saved/restored; they are pristine.
119f78eca8fSKrzysztof Parzyszek for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
120f78eca8fSKrzysztof Parzyszek Pristine.removeReg(Info.getReg());
121f78eca8fSKrzysztof Parzyszek addUnits(Pristine.getBitVector());
122710a4c1fSMatthias Braun }
123710a4c1fSMatthias Braun
addLiveOuts(const MachineBasicBlock & MBB)124710a4c1fSMatthias Braun void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
125710a4c1fSMatthias Braun const MachineFunction &MF = *MBB.getParent();
126bac11518SOliver Stannard
127f78eca8fSKrzysztof Parzyszek addPristines(MF);
128bac11518SOliver Stannard
1294e8624d1SMatthias Braun // To get the live-outs we simply merge the live-ins of all successors.
1304e8624d1SMatthias Braun for (const MachineBasicBlock *Succ : MBB.successors())
1314e8624d1SMatthias Braun addBlockLiveIns(*this, *Succ);
132bac11518SOliver Stannard
1334e8624d1SMatthias Braun // For the return block: Add all callee saved registers.
134bac11518SOliver Stannard if (MBB.isReturnBlock()) {
135710a4c1fSMatthias Braun const MachineFrameInfo &MFI = MF.getFrameInfo();
1364e8624d1SMatthias Braun if (MFI.isCalleeSavedInfoValid())
1374e8624d1SMatthias Braun addCalleeSavedRegs(*this, MF);
138710a4c1fSMatthias Braun }
139710a4c1fSMatthias Braun }
140710a4c1fSMatthias Braun
addLiveIns(const MachineBasicBlock & MBB)141710a4c1fSMatthias Braun void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
142710a4c1fSMatthias Braun const MachineFunction &MF = *MBB.getParent();
143f78eca8fSKrzysztof Parzyszek addPristines(MF);
1444e8624d1SMatthias Braun addBlockLiveIns(*this, MBB);
145710a4c1fSMatthias Braun }
146