Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- RegisterClassInfo.h - Dynamic Register Class Info --------*- 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 // This file implements the RegisterClassInfo class which provides dynamic
0010 // information about target register classes. Callee saved and reserved
0011 // registers depends on calling conventions and other dynamic information, so
0012 // some things cannot be determined statically.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_CODEGEN_REGISTERCLASSINFO_H
0017 #define LLVM_CODEGEN_REGISTERCLASSINFO_H
0018 
0019 #include "llvm/ADT/ArrayRef.h"
0020 #include "llvm/ADT/BitVector.h"
0021 #include "llvm/ADT/SmallVector.h"
0022 #include "llvm/CodeGen/TargetRegisterInfo.h"
0023 #include "llvm/MC/MCRegister.h"
0024 #include <cstdint>
0025 #include <memory>
0026 
0027 namespace llvm {
0028 
0029 class RegisterClassInfo {
0030   struct RCInfo {
0031     unsigned Tag = 0;
0032     unsigned NumRegs = 0;
0033     bool ProperSubClass = false;
0034     uint8_t MinCost = 0;
0035     uint16_t LastCostChange = 0;
0036     std::unique_ptr<MCPhysReg[]> Order;
0037 
0038     RCInfo() = default;
0039 
0040     operator ArrayRef<MCPhysReg>() const {
0041       return ArrayRef(Order.get(), NumRegs);
0042     }
0043   };
0044 
0045   // Brief cached information for each register class.
0046   std::unique_ptr<RCInfo[]> RegClass;
0047 
0048   // Tag changes whenever cached information needs to be recomputed. An RCInfo
0049   // entry is valid when its tag matches.
0050   unsigned Tag = 0;
0051 
0052   const MachineFunction *MF = nullptr;
0053   const TargetRegisterInfo *TRI = nullptr;
0054 
0055   // Callee saved registers of last MF.
0056   // Used only to determine if an update for CalleeSavedAliases is necessary.
0057   SmallVector<MCPhysReg, 16> LastCalleeSavedRegs;
0058 
0059   // Map regunit to the callee saved Register.
0060   SmallVector<MCPhysReg> CalleeSavedAliases;
0061 
0062   // Indicate if a specified callee saved register be in the allocation order
0063   // exactly as written in the tablegen descriptions or listed later.
0064   BitVector IgnoreCSRForAllocOrder;
0065 
0066   // Reserved registers in the current MF.
0067   BitVector Reserved;
0068 
0069   std::unique_ptr<unsigned[]> PSetLimits;
0070 
0071   // The register cost values.
0072   ArrayRef<uint8_t> RegCosts;
0073 
0074   // Compute all information about RC.
0075   void compute(const TargetRegisterClass *RC) const;
0076 
0077   // Return an up-to-date RCInfo for RC.
0078   const RCInfo &get(const TargetRegisterClass *RC) const {
0079     const RCInfo &RCI = RegClass[RC->getID()];
0080     if (Tag != RCI.Tag)
0081       compute(RC);
0082     return RCI;
0083   }
0084 
0085 public:
0086   RegisterClassInfo();
0087 
0088   /// runOnFunction - Prepare to answer questions about MF. This must be called
0089   /// before any other methods are used.
0090   void runOnMachineFunction(const MachineFunction &MF);
0091 
0092   /// getNumAllocatableRegs - Returns the number of actually allocatable
0093   /// registers in RC in the current function.
0094   unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const {
0095     return get(RC).NumRegs;
0096   }
0097 
0098   /// getOrder - Returns the preferred allocation order for RC. The order
0099   /// contains no reserved registers, and registers that alias callee saved
0100   /// registers come last.
0101   ArrayRef<MCPhysReg> getOrder(const TargetRegisterClass *RC) const {
0102     return get(RC);
0103   }
0104 
0105   /// isProperSubClass - Returns true if RC has a legal super-class with more
0106   /// allocatable registers.
0107   ///
0108   /// Register classes like GR32_NOSP are not proper sub-classes because %esp
0109   /// is not allocatable.  Similarly, tGPR is not a proper sub-class in Thumb
0110   /// mode because the GPR super-class is not legal.
0111   bool isProperSubClass(const TargetRegisterClass *RC) const {
0112     return get(RC).ProperSubClass;
0113   }
0114 
0115   /// getLastCalleeSavedAlias - Returns the last callee saved register that
0116   /// overlaps PhysReg, or NoRegister if PhysReg doesn't overlap a
0117   /// CalleeSavedAliases.
0118   MCRegister getLastCalleeSavedAlias(MCRegister PhysReg) const {
0119     MCRegister CSR;
0120     for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
0121       CSR = CalleeSavedAliases[*UI];
0122       if (CSR)
0123         break;
0124     }
0125     return CSR;
0126   }
0127 
0128   /// Get the minimum register cost in RC's allocation order.
0129   /// This is the smallest value in RegCosts[Reg] for all
0130   /// the registers in getOrder(RC).
0131   uint8_t getMinCost(const TargetRegisterClass *RC) const {
0132     return get(RC).MinCost;
0133   }
0134 
0135   /// Get the position of the last cost change in getOrder(RC).
0136   ///
0137   /// All registers in getOrder(RC).slice(getLastCostChange(RC)) will have the
0138   /// same cost according to RegCosts[Reg].
0139   unsigned getLastCostChange(const TargetRegisterClass *RC) const {
0140     return get(RC).LastCostChange;
0141   }
0142 
0143   /// Get the register unit limit for the given pressure set index.
0144   ///
0145   /// RegisterClassInfo adjusts this limit for reserved registers.
0146   unsigned getRegPressureSetLimit(unsigned Idx) const {
0147     if (!PSetLimits[Idx])
0148       PSetLimits[Idx] = computePSetLimit(Idx);
0149     return PSetLimits[Idx];
0150   }
0151 
0152 protected:
0153   unsigned computePSetLimit(unsigned Idx) const;
0154 };
0155 
0156 } // end namespace llvm
0157 
0158 #endif // LLVM_CODEGEN_REGISTERCLASSINFO_H