blob: b2731aa0e7dbca82b296295e752a15eeb916bf94 [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"
Matthias Braun4e8624d2017-06-03 00:26:35 +000014
Eugene Zelenko5db84df2017-02-17 21:43:25 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
Matthias Braun710a4c12017-01-20 00:16:14 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstrBundle.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000019#include "llvm/CodeGen/MachineOperand.h"
Matthias Braun4e8624d2017-06-03 00:26:35 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000022#include "llvm/MC/MCRegisterInfo.h"
Eugene Zelenko5db84df2017-02-17 21:43:25 +000023
Matthias Braun710a4c12017-01-20 00:16:14 +000024using namespace llvm;
25
26void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
27 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
28 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
29 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
30 Units.reset(U);
31 }
32 }
33}
34
Matthias Braun28eae8f2017-01-21 02:21:04 +000035void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
36 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
37 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
38 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
39 Units.set(U);
40 }
41 }
42}
43
Matthias Braun710a4c12017-01-20 00:16:14 +000044void LiveRegUnits::stepBackward(const MachineInstr &MI) {
45 // Remove defined registers and regmask kills from the set.
Florian Hahn11f31182019-12-11 09:27:01 +000046 for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
47 if (MOP.isRegMask()) {
48 removeRegsNotPreserved(MOP.getRegMask());
49 continue;
50 }
51
52 if (MOP.isDef())
53 removeReg(MOP.getReg());
Matthias Braun710a4c12017-01-20 00:16:14 +000054 }
55
56 // Add uses to the set.
Florian Hahn11f31182019-12-11 09:27:01 +000057 for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
58 if (!MOP.isReg() || !MOP.readsReg())
Matthias Braun710a4c12017-01-20 00:16:14 +000059 continue;
Florian Hahn11f31182019-12-11 09:27:01 +000060 addReg(MOP.getReg());
Matthias Braun710a4c12017-01-20 00:16:14 +000061 }
62}
63
Matthias Braun1b54aa52017-07-07 03:02:17 +000064void LiveRegUnits::accumulate(const MachineInstr &MI) {
Matthias Braun28eae8f2017-01-21 02:21:04 +000065 // Add defs, uses and regmask clobbers to the set.
Florian Hahn11f31182019-12-11 09:27:01 +000066 for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
67 if (MOP.isRegMask()) {
68 addRegsInMask(MOP.getRegMask());
69 continue;
70 }
71 if (!MOP.isDef() && !MOP.readsReg())
72 continue;
73 addReg(MOP.getReg());
Matthias Braun28eae8f2017-01-21 02:21:04 +000074 }
75}
76
Matthias Braun710a4c12017-01-20 00:16:14 +000077/// Add live-in registers of basic block \p MBB to \p LiveUnits.
Matthias Braun4e8624d2017-06-03 00:26:35 +000078static void addBlockLiveIns(LiveRegUnits &LiveUnits,
79 const MachineBasicBlock &MBB) {
Matthias Braun710a4c12017-01-20 00:16:14 +000080 for (const auto &LI : MBB.liveins())
81 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
82}
83
Matthias Braun4e8624d2017-06-03 00:26:35 +000084/// Adds all callee saved registers to \p LiveUnits.
85static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
86 const MachineFunction &MF) {
87 const MachineRegisterInfo &MRI = MF.getRegInfo();
88 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
89 LiveUnits.addReg(*CSR);
Matthias Braun710a4c12017-01-20 00:16:14 +000090}
91
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +000092void LiveRegUnits::addPristines(const MachineFunction &MF) {
Matthias Braun4e8624d2017-06-03 00:26:35 +000093 const MachineFrameInfo &MFI = MF.getFrameInfo();
94 if (!MFI.isCalleeSavedInfoValid())
95 return;
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +000096 /// This function will usually be called on an empty object, handle this
97 /// as a special case.
98 if (empty()) {
99 /// Add all callee saved regs, then remove the ones that are saved and
100 /// restored.
101 addCalleeSavedRegs(*this, MF);
102 /// Remove the ones that are not saved/restored; they are pristine.
103 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
104 removeReg(Info.getReg());
105 return;
106 }
107 /// If a callee-saved register that is not pristine is already present
108 /// in the set, we should make sure that it stays in it. Precompute the
109 /// set of pristine registers in a separate object.
Matthias Braun4e8624d2017-06-03 00:26:35 +0000110 /// Add all callee saved regs, then remove the ones that are saved+restored.
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000111 LiveRegUnits Pristine(*TRI);
112 addCalleeSavedRegs(Pristine, MF);
Matthias Braun4e8624d2017-06-03 00:26:35 +0000113 /// Remove the ones that are not saved/restored; they are pristine.
Matthias Braun710a4c12017-01-20 00:16:14 +0000114 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000115 Pristine.removeReg(Info.getReg());
116 addUnits(Pristine.getBitVector());
Matthias Braun710a4c12017-01-20 00:16:14 +0000117}
118
119void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
120 const MachineFunction &MF = *MBB.getParent();
Oliver Stannardbac11512019-02-01 09:23:51 +0000121
122 addPristines(MF);
123
124 // To get the live-outs we simply merge the live-ins of all successors.
125 for (const MachineBasicBlock *Succ : MBB.successors())
126 addBlockLiveIns(*this, *Succ);
127
128 // For the return block: Add all callee saved registers.
129 if (MBB.isReturnBlock()) {
Matthias Braun4e8624d2017-06-03 00:26:35 +0000130 const MachineFrameInfo &MFI = MF.getFrameInfo();
131 if (MFI.isCalleeSavedInfoValid())
132 addCalleeSavedRegs(*this, MF);
Matthias Braun710a4c12017-01-20 00:16:14 +0000133 }
Matthias Braun710a4c12017-01-20 00:16:14 +0000134}
135
136void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
137 const MachineFunction &MF = *MBB.getParent();
Krzysztof Parzyszekf78eca82017-09-08 16:29:50 +0000138 addPristines(MF);
Matthias Braun4e8624d2017-06-03 00:26:35 +0000139 addBlockLiveIns(*this, MBB);
Matthias Braun710a4c12017-01-20 00:16:14 +0000140}