blob: ea2075bc139dfd8f9c70452855b3bc09a738e6cd [file] [log] [blame]
Eugene Zelenko5db84df2017-02-17 21:43:25 +00001//===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
Matthias Braun710a4c12017-01-20 00:16:14 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Matthias Braun710a4c12017-01-20 00:16:14 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file This file imlements the LiveRegUnits set.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/LiveRegUnits.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000014#include "llvm/CodeGen/MachineBasicBlock.h"
Matthias Braun710a4c12017-01-20 00:16:14 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
16#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000017#include "llvm/CodeGen/MachineOperand.h"
Matthias Braun4e8624d2017-06-03 00:26:35 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000019
Matthias Braun710a4c12017-01-20 00:16:14 +000020using namespace llvm;
21
22void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
23 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
24 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
25 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
26 Units.reset(U);
27 }
28 }
29}
30
Matthias Braun28eae8f2017-01-21 02:21:04 +000031void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
32 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
33 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
34 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
35 Units.set(U);
36 }
37 }
38}
39
Matthias Braun710a4c12017-01-20 00:16:14 +000040void LiveRegUnits::stepBackward(const MachineInstr &MI) {
41 // Remove defined registers and regmask kills from the set.
Florian Hahn11f31182019-12-11 09:27:01 +000042 for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
43 if (MOP.isRegMask()) {
44 removeRegsNotPreserved(MOP.getRegMask());
45 continue;
46 }
47
48 if (MOP.isDef())
49 removeReg(MOP.getReg());
Matthias Braun710a4c12017-01-20 00:16:14 +000050 }
51
52 // Add uses to the set.
Florian Hahn11f31182019-12-11 09:27:01 +000053 for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
54 if (!MOP.isReg() || !MOP.readsReg())
Matthias Braun710a4c12017-01-20 00:16:14 +000055 continue;
Florian Hahn11f31182019-12-11 09:27:01 +000056 addReg(MOP.getReg());
Matthias Braun710a4c12017-01-20 00:16:14 +000057 }
58}
59
Matthias Braun1b54aa52017-07-07 03:02:17 +000060void LiveRegUnits::accumulate(const MachineInstr &MI) {
Matthias Braun28eae8f2017-01-21 02:21:04 +000061 // Add defs, uses and regmask clobbers to the set.
Florian Hahn11f31182019-12-11 09:27:01 +000062 for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
63 if (MOP.isRegMask()) {
64 addRegsInMask(MOP.getRegMask());
65 continue;
66 }
67 if (!MOP.isDef() && !MOP.readsReg())
68 continue;
69 addReg(MOP.getReg());
Matthias Braun28eae8f2017-01-21 02:21:04 +000070 }
71}
72
Matthias Braun710a4c12017-01-20 00:16:14 +000073/// Add live-in registers of basic block \p MBB to \p LiveUnits.
Matthias Braun4e8624d2017-06-03 00:26:35 +000074static void addBlockLiveIns(LiveRegUnits &LiveUnits,
75 const MachineBasicBlock &MBB) {
Matthias Braun710a4c12017-01-20 00:16:14 +000076 for (const auto &LI : MBB.liveins())
77 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
78}
79
Matthias Braun4e8624d2017-06-03 00:26:35 +000080/// Adds all callee saved registers to \p LiveUnits.
81static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
82 const MachineFunction &MF) {
83 const MachineRegisterInfo &MRI = MF.getRegInfo();
84 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
85 LiveUnits.addReg(*CSR);
Matthias Braun710a4c12017-01-20 00:16:14 +000086}
87
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +000088void LiveRegUnits::addPristines(const MachineFunction &MF) {
Matthias Braun4e8624d2017-06-03 00:26:35 +000089 const MachineFrameInfo &MFI = MF.getFrameInfo();
90 if (!MFI.isCalleeSavedInfoValid())
91 return;
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +000092 /// This function will usually be called on an empty object, handle this
93 /// as a special case.
94 if (empty()) {
95 /// Add all callee saved regs, then remove the ones that are saved and
96 /// restored.
97 addCalleeSavedRegs(*this, MF);
98 /// Remove the ones that are not saved/restored; they are pristine.
99 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
100 removeReg(Info.getReg());
101 return;
102 }
103 /// If a callee-saved register that is not pristine is already present
104 /// in the set, we should make sure that it stays in it. Precompute the
105 /// set of pristine registers in a separate object.
Matthias Braun4e8624d2017-06-03 00:26:35 +0000106 /// Add all callee saved regs, then remove the ones that are saved+restored.
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000107 LiveRegUnits Pristine(*TRI);
108 addCalleeSavedRegs(Pristine, MF);
Matthias Braun4e8624d2017-06-03 00:26:35 +0000109 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braun710a4c12017-01-20 00:16:14 +0000110 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000111 Pristine.removeReg(Info.getReg());
112 addUnits(Pristine.getBitVector());
Matthias Braun710a4c12017-01-20 00:16:14 +0000113}
114
115void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
116 const MachineFunction &MF = *MBB.getParent();
Oliver Stannardbac11512019-02-01 09:23:51 +0000117
118 addPristines(MF);
119
120 // To get the live-outs we simply merge the live-ins of all successors.
121 for (const MachineBasicBlock *Succ : MBB.successors())
122 addBlockLiveIns(*this, *Succ);
123
124 // For the return block: Add all callee saved registers.
125 if (MBB.isReturnBlock()) {
Matthias Braun4e8624d2017-06-03 00:26:35 +0000126 const MachineFrameInfo &MFI = MF.getFrameInfo();
127 if (MFI.isCalleeSavedInfoValid())
128 addCalleeSavedRegs(*this, MF);
Matthias Braun710a4c12017-01-20 00:16:14 +0000129 }
Matthias Braun710a4c12017-01-20 00:16:14 +0000130}
131
132void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
133 const MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000134 addPristines(MF);
Matthias Braun4e8624d2017-06-03 00:26:35 +0000135 addBlockLiveIns(*this, MBB);
Matthias Braun710a4c12017-01-20 00:16:14 +0000136}