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" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineOperand.h" |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 19 | |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | void 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 Braun | 28eae8f | 2017-01-21 02:21:04 +0000 | [diff] [blame] | 31 | void 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 Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 40 | void LiveRegUnits::stepBackward(const MachineInstr &MI) { |
| 41 | // Remove defined registers and regmask kills from the set. |
Florian Hahn | 11f3118 | 2019-12-11 09:27:01 +0000 | [diff] [blame] | 42 | 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 Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Add uses to the set. |
Florian Hahn | 11f3118 | 2019-12-11 09:27:01 +0000 | [diff] [blame] | 53 | for (const MachineOperand &MOP : phys_regs_and_masks(MI)) { |
| 54 | if (!MOP.isReg() || !MOP.readsReg()) |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 55 | continue; |
Florian Hahn | 11f3118 | 2019-12-11 09:27:01 +0000 | [diff] [blame] | 56 | addReg(MOP.getReg()); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Matthias Braun | 1b54aa5 | 2017-07-07 03:02:17 +0000 | [diff] [blame] | 60 | void LiveRegUnits::accumulate(const MachineInstr &MI) { |
Matthias Braun | 28eae8f | 2017-01-21 02:21:04 +0000 | [diff] [blame] | 61 | // Add defs, uses and regmask clobbers to the set. |
Florian Hahn | 11f3118 | 2019-12-11 09:27:01 +0000 | [diff] [blame] | 62 | 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 Braun | 28eae8f | 2017-01-21 02:21:04 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 73 | /// Add live-in registers of basic block \p MBB to \p LiveUnits. |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 74 | static void addBlockLiveIns(LiveRegUnits &LiveUnits, |
| 75 | const MachineBasicBlock &MBB) { |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 76 | for (const auto &LI : MBB.liveins()) |
| 77 | LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask); |
| 78 | } |
| 79 | |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 80 | /// Adds all callee saved registers to \p LiveUnits. |
| 81 | static 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 Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 88 | void LiveRegUnits::addPristines(const MachineFunction &MF) { |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 89 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 90 | if (!MFI.isCalleeSavedInfoValid()) |
| 91 | return; |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 92 | /// 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 Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 106 | /// 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] | 107 | LiveRegUnits Pristine(*TRI); |
| 108 | addCalleeSavedRegs(Pristine, MF); |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 109 | /// Remove the ones that are not saved/restored; they are pristine. |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 110 | for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 111 | Pristine.removeReg(Info.getReg()); |
| 112 | addUnits(Pristine.getBitVector()); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) { |
| 116 | const MachineFunction &MF = *MBB.getParent(); |
Oliver Stannard | bac1151 | 2019-02-01 09:23:51 +0000 | [diff] [blame] | 117 | |
| 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 Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 126 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 127 | if (MFI.isCalleeSavedInfoValid()) |
| 128 | addCalleeSavedRegs(*this, MF); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 129 | } |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) { |
| 133 | const MachineFunction &MF = *MBB.getParent(); |
Krzysztof Parzyszek | f78eca8 | 2017-09-08 16:29:50 +0000 | [diff] [blame] | 134 | addPristines(MF); |
Matthias Braun | 4e8624d | 2017-06-03 00:26:35 +0000 | [diff] [blame] | 135 | addBlockLiveIns(*this, MBB); |
Matthias Braun | 710a4c1 | 2017-01-20 00:16:14 +0000 | [diff] [blame] | 136 | } |