|
|
|||
File indexing completed on 2026-05-10 08:43:29
0001 //===- llvm/CodeGen/LivePhysRegs.h - Live Physical Register Set -*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 /// \file 0010 /// This file implements the LivePhysRegs utility for tracking liveness of 0011 /// physical registers. This can be used for ad-hoc liveness tracking after 0012 /// register allocation. You can start with the live-ins/live-outs at the 0013 /// beginning/end of a block and update the information while walking the 0014 /// instructions inside the block. This implementation tracks the liveness on a 0015 /// sub-register granularity. 0016 /// 0017 /// We assume that the high bits of a physical super-register are not preserved 0018 /// unless the instruction has an implicit-use operand reading the super- 0019 /// register. 0020 /// 0021 /// X86 Example: 0022 /// %ymm0 = ... 0023 /// %xmm0 = ... (Kills %xmm0, all %xmm0s sub-registers, and %ymm0) 0024 /// 0025 /// %ymm0 = ... 0026 /// %xmm0 = ..., implicit %ymm0 (%ymm0 and all its sub-registers are alive) 0027 //===----------------------------------------------------------------------===// 0028 0029 #ifndef LLVM_CODEGEN_LIVEPHYSREGS_H 0030 #define LLVM_CODEGEN_LIVEPHYSREGS_H 0031 0032 #include "llvm/ADT/SparseSet.h" 0033 #include "llvm/CodeGen/MachineBasicBlock.h" 0034 #include "llvm/CodeGen/TargetRegisterInfo.h" 0035 #include "llvm/MC/MCRegister.h" 0036 #include "llvm/MC/MCRegisterInfo.h" 0037 #include <cassert> 0038 #include <utility> 0039 0040 namespace llvm { 0041 0042 template <typename T> class ArrayRef; 0043 0044 class MachineInstr; 0045 class MachineFunction; 0046 class MachineOperand; 0047 class MachineRegisterInfo; 0048 class raw_ostream; 0049 0050 /// A set of physical registers with utility functions to track liveness 0051 /// when walking backward/forward through a basic block. 0052 class LivePhysRegs { 0053 const TargetRegisterInfo *TRI = nullptr; 0054 using RegisterSet = SparseSet<MCPhysReg, identity<MCPhysReg>>; 0055 RegisterSet LiveRegs; 0056 0057 public: 0058 /// Constructs an unitialized set. init() needs to be called to initialize it. 0059 LivePhysRegs() = default; 0060 0061 /// Constructs and initializes an empty set. 0062 LivePhysRegs(const TargetRegisterInfo &TRI) : TRI(&TRI) { 0063 LiveRegs.setUniverse(TRI.getNumRegs()); 0064 } 0065 0066 LivePhysRegs(const LivePhysRegs&) = delete; 0067 LivePhysRegs &operator=(const LivePhysRegs&) = delete; 0068 0069 /// (re-)initializes and clears the set. 0070 void init(const TargetRegisterInfo &TRI) { 0071 this->TRI = &TRI; 0072 LiveRegs.clear(); 0073 LiveRegs.setUniverse(TRI.getNumRegs()); 0074 } 0075 0076 /// Clears the set. 0077 void clear() { LiveRegs.clear(); } 0078 0079 /// Returns true if the set is empty. 0080 bool empty() const { return LiveRegs.empty(); } 0081 0082 /// Adds a physical register and all its sub-registers to the set. 0083 void addReg(MCPhysReg Reg) { 0084 assert(TRI && "LivePhysRegs is not initialized."); 0085 assert(Reg <= TRI->getNumRegs() && "Expected a physical register."); 0086 for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg)) 0087 LiveRegs.insert(SubReg); 0088 } 0089 0090 /// Removes a physical register, all its sub-registers, and all its 0091 /// super-registers from the set. 0092 void removeReg(MCPhysReg Reg) { 0093 assert(TRI && "LivePhysRegs is not initialized."); 0094 assert(Reg <= TRI->getNumRegs() && "Expected a physical register."); 0095 for (MCRegAliasIterator R(Reg, TRI, true); R.isValid(); ++R) 0096 LiveRegs.erase((*R).id()); 0097 } 0098 0099 /// Removes physical registers clobbered by the regmask operand \p MO. 0100 void removeRegsInMask(const MachineOperand &MO, 0101 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers = 0102 nullptr); 0103 0104 /// Returns true if register \p Reg is contained in the set. This also 0105 /// works if only the super register of \p Reg has been defined, because 0106 /// addReg() always adds all sub-registers to the set as well. 0107 /// Note: Returns false if just some sub registers are live, use available() 0108 /// when searching a free register. 0109 bool contains(MCPhysReg Reg) const { return LiveRegs.count(Reg); } 0110 0111 /// Returns true if register \p Reg and no aliasing register is in the set. 0112 bool available(const MachineRegisterInfo &MRI, MCPhysReg Reg) const; 0113 0114 /// Remove defined registers and regmask kills from the set. 0115 void removeDefs(const MachineInstr &MI); 0116 0117 /// Add uses to the set. 0118 void addUses(const MachineInstr &MI); 0119 0120 /// Simulates liveness when stepping backwards over an instruction(bundle). 0121 /// Remove Defs, add uses. This is the recommended way of calculating 0122 /// liveness. 0123 void stepBackward(const MachineInstr &MI); 0124 0125 /// Simulates liveness when stepping forward over an instruction(bundle). 0126 /// Remove killed-uses, add defs. This is the not recommended way, because it 0127 /// depends on accurate kill flags. If possible use stepBackward() instead of 0128 /// this function. The clobbers set will be the list of registers either 0129 /// defined or clobbered by a regmask. The operand will identify whether this 0130 /// is a regmask or register operand. 0131 void stepForward(const MachineInstr &MI, 0132 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers); 0133 0134 /// Adds all live-in registers of basic block \p MBB. 0135 /// Live in registers are the registers in the blocks live-in list and the 0136 /// pristine registers. 0137 void addLiveIns(const MachineBasicBlock &MBB); 0138 0139 /// Adds all live-in registers of basic block \p MBB but skips pristine 0140 /// registers. 0141 void addLiveInsNoPristines(const MachineBasicBlock &MBB); 0142 0143 /// Adds all live-out registers of basic block \p MBB. 0144 /// Live out registers are the union of the live-in registers of the successor 0145 /// blocks and pristine registers. Live out registers of the end block are the 0146 /// callee saved registers. 0147 /// If a register is not added by this method, it is guaranteed to not be 0148 /// live out from MBB, although a sub-register may be. This is true 0149 /// both before and after regalloc. 0150 void addLiveOuts(const MachineBasicBlock &MBB); 0151 0152 /// Adds all live-out registers of basic block \p MBB but skips pristine 0153 /// registers. 0154 void addLiveOutsNoPristines(const MachineBasicBlock &MBB); 0155 0156 using const_iterator = RegisterSet::const_iterator; 0157 0158 const_iterator begin() const { return LiveRegs.begin(); } 0159 const_iterator end() const { return LiveRegs.end(); } 0160 0161 /// Prints the currently live registers to \p OS. 0162 void print(raw_ostream &OS) const; 0163 0164 /// Dumps the currently live registers to the debug output. 0165 void dump() const; 0166 0167 private: 0168 /// Adds live-in registers from basic block \p MBB, taking associated 0169 /// lane masks into consideration. 0170 void addBlockLiveIns(const MachineBasicBlock &MBB); 0171 0172 /// Adds pristine registers. Pristine registers are callee saved registers 0173 /// that are unused in the function. 0174 void addPristines(const MachineFunction &MF); 0175 }; 0176 0177 inline raw_ostream &operator<<(raw_ostream &OS, const LivePhysRegs& LR) { 0178 LR.print(OS); 0179 return OS; 0180 } 0181 0182 /// Computes registers live-in to \p MBB assuming all of its successors 0183 /// live-in lists are up-to-date. Puts the result into the given LivePhysReg 0184 /// instance \p LiveRegs. 0185 void computeLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB); 0186 0187 /// Recomputes dead and kill flags in \p MBB. 0188 void recomputeLivenessFlags(MachineBasicBlock &MBB); 0189 0190 /// Adds registers contained in \p LiveRegs to the block live-in list of \p MBB. 0191 /// Does not add reserved registers. 0192 void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs); 0193 0194 /// Convenience function combining computeLiveIns() and addLiveIns(). 0195 void computeAndAddLiveIns(LivePhysRegs &LiveRegs, 0196 MachineBasicBlock &MBB); 0197 0198 /// Convenience function for recomputing live-in's for a MBB. Returns true if 0199 /// any changes were made. 0200 static inline bool recomputeLiveIns(MachineBasicBlock &MBB) { 0201 LivePhysRegs LPR; 0202 std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIns; 0203 0204 MBB.clearLiveIns(OldLiveIns); 0205 computeAndAddLiveIns(LPR, MBB); 0206 MBB.sortUniqueLiveIns(); 0207 0208 const std::vector<MachineBasicBlock::RegisterMaskPair> &NewLiveIns = 0209 MBB.getLiveIns(); 0210 return OldLiveIns != NewLiveIns; 0211 } 0212 0213 /// Convenience function for recomputing live-in's for a set of MBBs until the 0214 /// computation converges. 0215 inline void fullyRecomputeLiveIns(ArrayRef<MachineBasicBlock *> MBBs) { 0216 MachineBasicBlock *const *Data = MBBs.data(); 0217 const size_t Len = MBBs.size(); 0218 while (true) { 0219 bool AnyChange = false; 0220 for (size_t I = 0; I < Len; ++I) 0221 if (recomputeLiveIns(*Data[I])) 0222 AnyChange = true; 0223 if (!AnyChange) 0224 return; 0225 } 0226 } 0227 0228 0229 } // end namespace llvm 0230 0231 #endif // LLVM_CODEGEN_LIVEPHYSREGS_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|