Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:34

0001 //===- RegisterScavenging.h - Machine register scavenging -------*- 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 declares the machine register scavenger class. It can provide
0011 /// information such as unused register at any point in a machine basic block.
0012 /// It also provides a mechanism to make registers available by evicting them
0013 /// to spill slots.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_CODEGEN_REGISTERSCAVENGING_H
0018 #define LLVM_CODEGEN_REGISTERSCAVENGING_H
0019 
0020 #include "llvm/ADT/BitVector.h"
0021 #include "llvm/ADT/SmallVector.h"
0022 #include "llvm/CodeGen/LiveRegUnits.h"
0023 #include "llvm/CodeGen/MachineBasicBlock.h"
0024 #include "llvm/CodeGen/MachineRegisterInfo.h"
0025 #include "llvm/MC/LaneBitmask.h"
0026 
0027 namespace llvm {
0028 
0029 class MachineInstr;
0030 class TargetInstrInfo;
0031 class TargetRegisterClass;
0032 class TargetRegisterInfo;
0033 
0034 class RegScavenger {
0035   const TargetRegisterInfo *TRI = nullptr;
0036   const TargetInstrInfo *TII = nullptr;
0037   MachineRegisterInfo *MRI = nullptr;
0038   MachineBasicBlock *MBB = nullptr;
0039   MachineBasicBlock::iterator MBBI;
0040 
0041   /// Information on scavenged registers (held in a spill slot).
0042   struct ScavengedInfo {
0043     ScavengedInfo(int FI = -1) : FrameIndex(FI) {}
0044 
0045     /// A spill slot used for scavenging a register post register allocation.
0046     int FrameIndex;
0047 
0048     /// If non-zero, the specific register is currently being
0049     /// scavenged. That is, it is spilled to this scavenging stack slot.
0050     Register Reg;
0051 
0052     /// The instruction that restores the scavenged register from stack.
0053     const MachineInstr *Restore = nullptr;
0054   };
0055 
0056   /// A vector of information on scavenged registers.
0057   SmallVector<ScavengedInfo, 2> Scavenged;
0058 
0059   LiveRegUnits LiveUnits;
0060 
0061 public:
0062   RegScavenger() = default;
0063 
0064   /// Record that \p Reg is in use at scavenging index \p FI. This is for
0065   /// targets which need to directly manage the spilling process, and need to
0066   /// update the scavenger's internal state.  It's expected this be called a
0067   /// second time with \p Restore set to a non-null value, so that the
0068   /// externally inserted restore instruction resets the scavenged slot
0069   /// liveness when encountered.
0070   void assignRegToScavengingIndex(int FI, Register Reg,
0071                                   MachineInstr *Restore = nullptr) {
0072     for (ScavengedInfo &Slot : Scavenged) {
0073       if (Slot.FrameIndex == FI) {
0074         assert(!Slot.Reg || Slot.Reg == Reg);
0075         Slot.Reg = Reg;
0076         Slot.Restore = Restore;
0077         return;
0078       }
0079     }
0080 
0081     llvm_unreachable("did not find scavenging index");
0082   }
0083 
0084   /// Start tracking liveness from the begin of basic block \p MBB.
0085   void enterBasicBlock(MachineBasicBlock &MBB);
0086 
0087   /// Start tracking liveness from the end of basic block \p MBB.
0088   /// Use backward() to move towards the beginning of the block.
0089   void enterBasicBlockEnd(MachineBasicBlock &MBB);
0090 
0091   /// Update internal register state and move MBB iterator backwards. This
0092   /// method gives precise results even in the absence of kill flags.
0093   void backward();
0094 
0095   /// Call backward() to update internal register state to just before \p *I.
0096   void backward(MachineBasicBlock::iterator I) {
0097     while (MBBI != I)
0098       backward();
0099   }
0100 
0101   /// Return if a specific register is currently used.
0102   bool isRegUsed(Register Reg, bool includeReserved = true) const;
0103 
0104   /// Return all available registers in the register class in Mask.
0105   BitVector getRegsAvailable(const TargetRegisterClass *RC);
0106 
0107   /// Find an unused register of the specified register class.
0108   /// Return 0 if none is found.
0109   Register FindUnusedReg(const TargetRegisterClass *RC) const;
0110 
0111   /// Add a scavenging frame index.
0112   void addScavengingFrameIndex(int FI) {
0113     Scavenged.push_back(ScavengedInfo(FI));
0114   }
0115 
0116   /// Query whether a frame index is a scavenging frame index.
0117   bool isScavengingFrameIndex(int FI) const {
0118     for (const ScavengedInfo &SI : Scavenged)
0119       if (SI.FrameIndex == FI)
0120         return true;
0121 
0122     return false;
0123   }
0124 
0125   /// Get an array of scavenging frame indices.
0126   void getScavengingFrameIndices(SmallVectorImpl<int> &A) const {
0127     for (const ScavengedInfo &I : Scavenged)
0128       if (I.FrameIndex >= 0)
0129         A.push_back(I.FrameIndex);
0130   }
0131 
0132   /// Make a register of the specific register class available from the current
0133   /// position backwards to the place before \p To. If \p RestoreAfter is true
0134   /// this includes the instruction following the current position.
0135   /// SPAdj is the stack adjustment due to call frame, it's passed along to
0136   /// eliminateFrameIndex().
0137   /// Returns the scavenged register.
0138   ///
0139   /// If \p AllowSpill is false, fail if a spill is required to make the
0140   /// register available, and return NoRegister.
0141   Register scavengeRegisterBackwards(const TargetRegisterClass &RC,
0142                                      MachineBasicBlock::iterator To,
0143                                      bool RestoreAfter, int SPAdj,
0144                                      bool AllowSpill = true);
0145 
0146   /// Tell the scavenger a register is used.
0147   void setRegUsed(Register Reg, LaneBitmask LaneMask = LaneBitmask::getAll());
0148 
0149 private:
0150   /// Returns true if a register is reserved. It is never "unused".
0151   bool isReserved(Register Reg) const { return MRI->isReserved(Reg); }
0152 
0153   /// Initialize RegisterScavenger.
0154   void init(MachineBasicBlock &MBB);
0155 
0156   /// Spill a register after position \p After and reload it before position
0157   /// \p UseMI.
0158   ScavengedInfo &spill(Register Reg, const TargetRegisterClass &RC, int SPAdj,
0159                        MachineBasicBlock::iterator Before,
0160                        MachineBasicBlock::iterator &UseMI);
0161 };
0162 
0163 /// Replaces all frame index virtual registers with physical registers. Uses the
0164 /// register scavenger to find an appropriate register to use.
0165 void scavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger &RS);
0166 
0167 } // end namespace llvm
0168 
0169 #endif // LLVM_CODEGEN_REGISTERSCAVENGING_H