Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- FunctionLoweringInfo.h - Lower functions from LLVM IR ---*- 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 implements routines for translating functions from LLVM IR into
0010 // Machine IR.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
0015 #define LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H
0016 
0017 #include "llvm/ADT/BitVector.h"
0018 #include "llvm/ADT/DenseMap.h"
0019 #include "llvm/ADT/IndexedMap.h"
0020 #include "llvm/ADT/SmallPtrSet.h"
0021 #include "llvm/ADT/SmallVector.h"
0022 #include "llvm/CodeGen/ISDOpcodes.h"
0023 #include "llvm/CodeGen/MachineBasicBlock.h"
0024 #include "llvm/CodeGen/TargetRegisterInfo.h"
0025 #include "llvm/IR/Instructions.h"
0026 #include "llvm/IR/Type.h"
0027 #include "llvm/IR/Value.h"
0028 #include "llvm/Support/KnownBits.h"
0029 #include <cassert>
0030 #include <utility>
0031 #include <vector>
0032 
0033 namespace llvm {
0034 
0035 class Argument;
0036 class BasicBlock;
0037 class BranchProbabilityInfo;
0038 class DbgDeclareInst;
0039 class Function;
0040 class Instruction;
0041 class MachineFunction;
0042 class MachineInstr;
0043 class MachineRegisterInfo;
0044 class MVT;
0045 class SelectionDAG;
0046 class TargetLowering;
0047 
0048 template <typename T> class GenericSSAContext;
0049 using SSAContext = GenericSSAContext<Function>;
0050 template <typename T> class GenericUniformityInfo;
0051 using UniformityInfo = GenericUniformityInfo<SSAContext>;
0052 
0053 //===--------------------------------------------------------------------===//
0054 /// FunctionLoweringInfo - This contains information that is global to a
0055 /// function that is used when lowering a region of the function.
0056 ///
0057 class FunctionLoweringInfo {
0058 public:
0059   const Function *Fn;
0060   MachineFunction *MF;
0061   const TargetLowering *TLI;
0062   MachineRegisterInfo *RegInfo;
0063   BranchProbabilityInfo *BPI;
0064   const UniformityInfo *UA;
0065   /// CanLowerReturn - true iff the function's return value can be lowered to
0066   /// registers.
0067   bool CanLowerReturn;
0068 
0069   /// True if part of the CSRs will be handled via explicit copies.
0070   bool SplitCSR;
0071 
0072   /// DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg
0073   /// allocated to hold a pointer to the hidden sret parameter.
0074   Register DemoteRegister;
0075 
0076   /// A mapping from LLVM basic block number to their machine block.
0077   SmallVector<MachineBasicBlock *> MBBMap;
0078 
0079   /// ValueMap - Since we emit code for the function a basic block at a time,
0080   /// we must remember which virtual registers hold the values for
0081   /// cross-basic-block values.
0082   DenseMap<const Value *, Register> ValueMap;
0083 
0084   /// VirtReg2Value map is needed by the Divergence Analysis driven
0085   /// instruction selection. It is reverted ValueMap. It is computed
0086   /// in lazy style - on demand. It is used to get the Value corresponding
0087   /// to the live in virtual register and is called from the
0088   /// TargetLowerinInfo::isSDNodeSourceOfDivergence.
0089   DenseMap<Register, const Value*> VirtReg2Value;
0090 
0091   /// This method is called from TargetLowerinInfo::isSDNodeSourceOfDivergence
0092   /// to get the Value corresponding to the live-in virtual register.
0093   const Value *getValueFromVirtualReg(Register Vreg);
0094 
0095   /// Track virtual registers created for exception pointers.
0096   DenseMap<const Value *, Register> CatchPadExceptionPointers;
0097 
0098   /// Helper object to track which of three possible relocation mechanisms are
0099   /// used for a particular value being relocated over a statepoint.
0100   struct StatepointRelocationRecord {
0101     enum RelocType {
0102       // Value did not need to be relocated and can be used directly.
0103       NoRelocate,
0104       // Value was spilled to stack and needs filled at the gc.relocate.
0105       Spill,
0106       // Value was lowered to tied def and gc.relocate should be replaced with
0107       // copy from vreg.
0108       VReg,
0109       // Value was lowered to tied def and gc.relocate should be replaced with
0110       // SDValue kept in StatepointLoweringInfo structure. This valid for local
0111       // relocates only.
0112       SDValueNode,
0113     } type = NoRelocate;
0114     // Payload contains either frame index of the stack slot in which the value
0115     // was spilled, or virtual register which contains the re-definition.
0116     union payload_t {
0117       payload_t() : FI(-1) {}
0118       int FI;
0119       Register Reg;
0120     } payload;
0121   };
0122 
0123   /// Keep track of each value which was relocated and the strategy used to
0124   /// relocate that value.  This information is required when visiting
0125   /// gc.relocates which may appear in following blocks.
0126   using StatepointSpillMapTy =
0127     DenseMap<const Value *, StatepointRelocationRecord>;
0128   DenseMap<const Instruction *, StatepointSpillMapTy> StatepointRelocationMaps;
0129 
0130   /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
0131   /// the entry block.  This allows the allocas to be efficiently referenced
0132   /// anywhere in the function.
0133   DenseMap<const AllocaInst*, int> StaticAllocaMap;
0134 
0135   /// ByValArgFrameIndexMap - Keep track of frame indices for byval arguments.
0136   DenseMap<const Argument*, int> ByValArgFrameIndexMap;
0137 
0138   /// ArgDbgValues - A list of DBG_VALUE instructions created during isel for
0139   /// function arguments that are inserted after scheduling is completed.
0140   SmallVector<MachineInstr*, 8> ArgDbgValues;
0141 
0142   /// Bitvector with a bit set if corresponding argument is described in
0143   /// ArgDbgValues. Using arg numbers according to Argument numbering.
0144   BitVector DescribedArgs;
0145 
0146   /// RegFixups - Registers which need to be replaced after isel is done.
0147   DenseMap<Register, Register> RegFixups;
0148 
0149   DenseSet<Register> RegsWithFixups;
0150 
0151   /// StatepointStackSlots - A list of temporary stack slots (frame indices)
0152   /// used to spill values at a statepoint.  We store them here to enable
0153   /// reuse of the same stack slots across different statepoints in different
0154   /// basic blocks.
0155   SmallVector<unsigned, 50> StatepointStackSlots;
0156 
0157   /// MBB - The current block.
0158   MachineBasicBlock *MBB;
0159 
0160   /// MBB - The current insert position inside the current block.
0161   MachineBasicBlock::iterator InsertPt;
0162 
0163   struct LiveOutInfo {
0164     unsigned NumSignBits : 31;
0165     unsigned IsValid : 1;
0166     KnownBits Known = 1;
0167 
0168     LiveOutInfo() : NumSignBits(0), IsValid(true) {}
0169   };
0170 
0171   /// Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND)
0172   /// for a value.
0173   DenseMap<const Value *, ISD::NodeType> PreferredExtendType;
0174 
0175   /// The set of basic blocks visited thus far by instruction selection. Indexed
0176   /// by basic block number.
0177   SmallVector<bool> VisitedBBs;
0178 
0179   /// PHINodesToUpdate - A list of phi instructions whose operand list will
0180   /// be updated after processing the current basic block.
0181   /// TODO: This isn't per-function state, it's per-basic-block state. But
0182   /// there's no other convenient place for it to live right now.
0183   std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
0184   unsigned OrigNumPHINodesToUpdate;
0185 
0186   /// If the current MBB is a landing pad, the exception pointer and exception
0187   /// selector registers are copied into these virtual registers by
0188   /// SelectionDAGISel::PrepareEHLandingPad().
0189   unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg;
0190 
0191   /// The current call site index being processed, if any. 0 if none.
0192   unsigned CurCallSite = 0;
0193 
0194   /// Collection of dbg.declare instructions handled after argument
0195   /// lowering and before ISel proper.
0196   SmallPtrSet<const DbgDeclareInst *, 8> PreprocessedDbgDeclares;
0197   SmallPtrSet<const DbgVariableRecord *, 8> PreprocessedDVRDeclares;
0198 
0199   /// set - Initialize this FunctionLoweringInfo with the given Function
0200   /// and its associated MachineFunction.
0201   ///
0202   void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
0203 
0204   /// clear - Clear out all the function-specific state. This returns this
0205   /// FunctionLoweringInfo to an empty state, ready to be used for a
0206   /// different function.
0207   void clear();
0208 
0209   /// isExportedInst - Return true if the specified value is an instruction
0210   /// exported from its block.
0211   bool isExportedInst(const Value *V) const {
0212     return ValueMap.count(V);
0213   }
0214 
0215   MachineBasicBlock *getMBB(const BasicBlock *BB) const {
0216     assert(BB->getNumber() < MBBMap.size() && "uninitialized MBBMap?");
0217     return MBBMap[BB->getNumber()];
0218   }
0219 
0220   Register CreateReg(MVT VT, bool isDivergent = false);
0221 
0222   Register CreateRegs(const Value *V);
0223 
0224   Register CreateRegs(Type *Ty, bool isDivergent = false);
0225 
0226   Register InitializeRegForValue(const Value *V);
0227 
0228   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
0229   /// register is a PHI destination and the PHI's LiveOutInfo is not valid.
0230   const LiveOutInfo *GetLiveOutRegInfo(Register Reg) {
0231     if (!LiveOutRegInfo.inBounds(Reg))
0232       return nullptr;
0233 
0234     const LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
0235     if (!LOI->IsValid)
0236       return nullptr;
0237 
0238     return LOI;
0239   }
0240 
0241   /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
0242   /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
0243   /// the register's LiveOutInfo is for a smaller bit width, it is extended to
0244   /// the larger bit width by zero extension. The bit width must be no smaller
0245   /// than the LiveOutInfo's existing bit width.
0246   const LiveOutInfo *GetLiveOutRegInfo(Register Reg, unsigned BitWidth);
0247 
0248   /// AddLiveOutRegInfo - Adds LiveOutInfo for a register.
0249   void AddLiveOutRegInfo(Register Reg, unsigned NumSignBits,
0250                          const KnownBits &Known) {
0251     // Only install this information if it tells us something.
0252     if (NumSignBits == 1 && Known.isUnknown())
0253       return;
0254 
0255     LiveOutRegInfo.grow(Reg);
0256     LiveOutInfo &LOI = LiveOutRegInfo[Reg];
0257     LOI.NumSignBits = NumSignBits;
0258     LOI.Known.One = Known.One;
0259     LOI.Known.Zero = Known.Zero;
0260   }
0261 
0262   /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
0263   /// register based on the LiveOutInfo of its operands.
0264   void ComputePHILiveOutRegInfo(const PHINode*);
0265 
0266   /// InvalidatePHILiveOutRegInfo - Invalidates a PHI's LiveOutInfo, to be
0267   /// called when a block is visited before all of its predecessors.
0268   void InvalidatePHILiveOutRegInfo(const PHINode *PN) {
0269     // PHIs with no uses have no ValueMap entry.
0270     DenseMap<const Value*, Register>::const_iterator It = ValueMap.find(PN);
0271     if (It == ValueMap.end())
0272       return;
0273 
0274     Register Reg = It->second;
0275     if (Reg == 0)
0276       return;
0277 
0278     LiveOutRegInfo.grow(Reg);
0279     LiveOutRegInfo[Reg].IsValid = false;
0280   }
0281 
0282   /// setArgumentFrameIndex - Record frame index for the byval
0283   /// argument.
0284   void setArgumentFrameIndex(const Argument *A, int FI);
0285 
0286   /// getArgumentFrameIndex - Get frame index for the byval argument.
0287   int getArgumentFrameIndex(const Argument *A);
0288 
0289   Register getCatchPadExceptionPointerVReg(const Value *CPI,
0290                                            const TargetRegisterClass *RC);
0291 
0292   /// Set the call site currently being processed.
0293   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
0294 
0295   /// Get the call site currently being processed, if any. Return zero if none.
0296   unsigned getCurrentCallSite() { return CurCallSite; }
0297 
0298 private:
0299   /// LiveOutRegInfo - Information about live out vregs.
0300   IndexedMap<LiveOutInfo, VirtReg2IndexFunctor> LiveOutRegInfo;
0301 };
0302 
0303 } // end namespace llvm
0304 
0305 #endif // LLVM_CODEGEN_FUNCTIONLOWERINGINFO_H