Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 1 | //===- LiveRegUnits.cpp - Register Unit Set -------------------------------===// |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file This file imlements the LiveRegUnits set. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/CodeGen/LiveRegUnits.h" |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 14 | |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineOperand.h" |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCRegisterInfo.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 23 | |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | void 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 Braun | 28eae8f | 2017-01-21 02:21:04 +0000 | [diff] [blame] | 35 | void 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 Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 44 | void LiveRegUnits::stepBackward(const MachineInstr &MI) { |
| 45 | // Remove defined registers and regmask kills from the set. |
| 46 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
| 47 | if (O->isReg()) { |
Krzysztof Parzyszek | 6a32293 | 2018-06-21 13:38:43 +0000 | [diff] [blame] | 48 | if (!O->isDef() || O->isDebug()) |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 49 | continue; |
| 50 | unsigned Reg = O->getReg(); |
| 51 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 52 | continue; |
| 53 | removeReg(Reg); |
| 54 | } else if (O->isRegMask()) |
| 55 | removeRegsNotPreserved(O->getRegMask()); |
| 56 | } |
| 57 | |
| 58 | // Add uses to the set. |
| 59 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
Krzysztof Parzyszek | 6a32293 | 2018-06-21 13:38:43 +0000 | [diff] [blame] | 60 | if (!O->isReg() || !O->readsReg() || O->isDebug()) |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 61 | continue; |
| 62 | unsigned Reg = O->getReg(); |
| 63 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 64 | continue; |
| 65 | addReg(Reg); |
| 66 | } |
| 67 | } |
| 68 | |
Matthias Braun | 1b54aa5 | 2017-07-07 03:02:17 +0000 | [diff] [blame] | 69 | void LiveRegUnits::accumulate(const MachineInstr &MI) { |
Matthias Braun | 28eae8f | 2017-01-21 02:21:04 +0000 | [diff] [blame] | 70 | // Add defs, uses and regmask clobbers to the set. |
| 71 | for (ConstMIBundleOperands O(MI); O.isValid(); ++O) { |
| 72 | if (O->isReg()) { |
| 73 | unsigned Reg = O->getReg(); |
| 74 | if (!TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 75 | continue; |
| 76 | if (!O->isDef() && !O->readsReg()) |
| 77 | continue; |
| 78 | addReg(Reg); |
| 79 | } else if (O->isRegMask()) |
| 80 | addRegsInMask(O->getRegMask()); |
| 81 | } |
| 82 | } |
| 83 | |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 84 | /// Add live-in registers of basic block \p MBB to \p LiveUnits. |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 85 | static void addBlockLiveIns(LiveRegUnits &LiveUnits, |
| 86 | const MachineBasicBlock &MBB) { |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 87 | for (const auto &LI : MBB.liveins()) |
| 88 | LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask); |
| 89 | } |
| 90 | |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 91 | /// Adds all callee saved registers to \p LiveUnits. |
| 92 | static void addCalleeSavedRegs(LiveRegUnits &LiveUnits, |
| 93 | const MachineFunction &MF) { |
| 94 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 95 | for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) |
| 96 | LiveUnits.addReg(*CSR); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 99 | void LiveRegUnits::addPristines(const MachineFunction &MF) { |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 100 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 101 | if (!MFI.isCalleeSavedInfoValid()) |
| 102 | return; |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 103 | /// This function will usually be called on an empty object, handle this |
| 104 | /// as a special case. |
| 105 | if (empty()) { |
| 106 | /// Add all callee saved regs, then remove the ones that are saved and |
| 107 | /// restored. |
| 108 | addCalleeSavedRegs(*this, MF); |
| 109 | /// Remove the ones that are not saved/restored; they are pristine. |
| 110 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
| 111 | removeReg(Info.getReg()); |
| 112 | return; |
| 113 | } |
| 114 | /// If a callee-saved register that is not pristine is already present |
| 115 | /// in the set, we should make sure that it stays in it. Precompute the |
| 116 | /// set of pristine registers in a separate object. |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 117 | /// Add all callee saved regs, then remove the ones that are saved+restored. |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 118 | LiveRegUnits Pristine(*TRI); |
| 119 | addCalleeSavedRegs(Pristine, MF); |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 120 | /// Remove the ones that are not saved/restored; they are pristine. |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 121 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 122 | Pristine.removeReg(Info.getReg()); |
| 123 | addUnits(Pristine.getBitVector()); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) { |
| 127 | const MachineFunction &MF = *MBB.getParent(); |
Oliver Stannard | bac1151 | 2019-02-01 09:23:51 +0000 | [diff] [blame] | 128 | |
| 129 | addPristines(MF); |
| 130 | |
| 131 | // To get the live-outs we simply merge the live-ins of all successors. |
| 132 | for (const MachineBasicBlock *Succ : MBB.successors()) |
| 133 | addBlockLiveIns(*this, *Succ); |
| 134 | |
| 135 | // For the return block: Add all callee saved registers. |
| 136 | if (MBB.isReturnBlock()) { |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 137 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 138 | if (MFI.isCalleeSavedInfoValid()) |
| 139 | addCalleeSavedRegs(*this, MF); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 140 | } |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) { |
| 144 | const MachineFunction &MF = *MBB.getParent(); |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 145 | addPristines(MF); |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 146 | addBlockLiveIns(*this, MBB); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 147 | } |