Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- GCMetadata.h - Garbage collector metadata ----------------*- 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 declares the GCFunctionInfo and GCModuleInfo classes, which are
0010 // used as a communication channel from the target code generator to the target
0011 // garbage collectors. This interface allows code generators and garbage
0012 // collectors to be developed independently.
0013 //
0014 // The GCFunctionInfo class logs the data necessary to build a type accurate
0015 // stack map. The code generator outputs:
0016 //
0017 //   - Safe points as specified by the GCStrategy's NeededSafePoints.
0018 //   - Stack offsets for GC roots, as specified by calls to llvm.gcroot
0019 //
0020 // As a refinement, liveness analysis calculates the set of live roots at each
0021 // safe point. Liveness analysis is not presently performed by the code
0022 // generator, so all roots are assumed live.
0023 //
0024 // GCModuleInfo simply collects GCFunctionInfo instances for each Function as
0025 // they are compiled. This accretion is necessary for collectors which must emit
0026 // a stack map for the compilation unit as a whole. Therefore, GCFunctionInfo
0027 // outlives the MachineFunction from which it is derived and must not refer to
0028 // any code generator data structures.
0029 //
0030 //===----------------------------------------------------------------------===//
0031 
0032 #ifndef LLVM_CODEGEN_GCMETADATA_H
0033 #define LLVM_CODEGEN_GCMETADATA_H
0034 
0035 #include "llvm/ADT/DenseMap.h"
0036 #include "llvm/ADT/SmallVector.h"
0037 #include "llvm/ADT/StringMap.h"
0038 #include "llvm/ADT/StringRef.h"
0039 #include "llvm/IR/DebugLoc.h"
0040 #include "llvm/IR/GCStrategy.h"
0041 #include "llvm/IR/PassManager.h"
0042 #include "llvm/Pass.h"
0043 #include <algorithm>
0044 #include <cstddef>
0045 #include <cstdint>
0046 #include <memory>
0047 #include <vector>
0048 
0049 namespace llvm {
0050 
0051 class Constant;
0052 class Function;
0053 class MCSymbol;
0054 
0055 /// GCPoint - Metadata for a collector-safe point in machine code.
0056 ///
0057 struct GCPoint {
0058   MCSymbol *Label;    ///< A label.
0059   DebugLoc Loc;
0060 
0061   GCPoint(MCSymbol *L, DebugLoc DL)
0062       : Label(L), Loc(std::move(DL)) {}
0063 };
0064 
0065 /// GCRoot - Metadata for a pointer to an object managed by the garbage
0066 /// collector.
0067 struct GCRoot {
0068   int Num;                  ///< Usually a frame index.
0069   int StackOffset = -1;     ///< Offset from the stack pointer.
0070   const Constant *Metadata; ///< Metadata straight from the call
0071                             ///< to llvm.gcroot.
0072 
0073   GCRoot(int N, const Constant *MD) : Num(N), Metadata(MD) {}
0074 };
0075 
0076 /// Garbage collection metadata for a single function.  Currently, this
0077 /// information only applies to GCStrategies which use GCRoot.
0078 class GCFunctionInfo {
0079 public:
0080   using iterator = std::vector<GCPoint>::iterator;
0081   using roots_iterator = std::vector<GCRoot>::iterator;
0082   using live_iterator = std::vector<GCRoot>::const_iterator;
0083 
0084 private:
0085   const Function &F;
0086   GCStrategy &S;
0087   uint64_t FrameSize;
0088   std::vector<GCRoot> Roots;
0089   std::vector<GCPoint> SafePoints;
0090 
0091   // FIXME: Liveness. A 2D BitVector, perhaps?
0092   //
0093   //   BitVector Liveness;
0094   //
0095   //   bool islive(int point, int root) =
0096   //     Liveness[point * SafePoints.size() + root]
0097   //
0098   // The bit vector is the more compact representation where >3.2% of roots
0099   // are live per safe point (1.5% on 64-bit hosts).
0100 
0101 public:
0102   GCFunctionInfo(const Function &F, GCStrategy &S);
0103   ~GCFunctionInfo();
0104 
0105   /// Handle invalidation explicitly.
0106   bool invalidate(Function &F, const PreservedAnalyses &PA,
0107                   FunctionAnalysisManager::Invalidator &Inv);
0108 
0109   /// getFunction - Return the function to which this metadata applies.
0110   const Function &getFunction() const { return F; }
0111 
0112   /// getStrategy - Return the GC strategy for the function.
0113   GCStrategy &getStrategy() { return S; }
0114 
0115   /// addStackRoot - Registers a root that lives on the stack. Num is the
0116   ///                stack object ID for the alloca (if the code generator is
0117   //                 using  MachineFrameInfo).
0118   void addStackRoot(int Num, const Constant *Metadata) {
0119     Roots.push_back(GCRoot(Num, Metadata));
0120   }
0121 
0122   /// removeStackRoot - Removes a root.
0123   roots_iterator removeStackRoot(roots_iterator position) {
0124     return Roots.erase(position);
0125   }
0126 
0127   /// addSafePoint - Notes the existence of a safe point. Num is the ID of the
0128   /// label just prior to the safe point (if the code generator is using
0129   /// MachineModuleInfo).
0130   void addSafePoint(MCSymbol *Label, const DebugLoc &DL) {
0131     SafePoints.emplace_back(Label, DL);
0132   }
0133 
0134   /// getFrameSize/setFrameSize - Records the function's frame size.
0135   uint64_t getFrameSize() const { return FrameSize; }
0136   void setFrameSize(uint64_t S) { FrameSize = S; }
0137 
0138   /// begin/end - Iterators for safe points.
0139   iterator begin() { return SafePoints.begin(); }
0140   iterator end() { return SafePoints.end(); }
0141   size_t size() const { return SafePoints.size(); }
0142 
0143   /// roots_begin/roots_end - Iterators for all roots in the function.
0144   roots_iterator roots_begin() { return Roots.begin(); }
0145   roots_iterator roots_end() { return Roots.end(); }
0146   size_t roots_size() const { return Roots.size(); }
0147 
0148   /// live_begin/live_end - Iterators for live roots at a given safe point.
0149   live_iterator live_begin(const iterator &p) { return roots_begin(); }
0150   live_iterator live_end(const iterator &p) { return roots_end(); }
0151   size_t live_size(const iterator &p) const { return roots_size(); }
0152 };
0153 
0154 struct GCStrategyMap {
0155   StringMap<std::unique_ptr<GCStrategy>> StrategyMap;
0156 
0157   GCStrategyMap() = default;
0158   GCStrategyMap(GCStrategyMap &&) = default;
0159 
0160   /// Handle invalidation explicitly.
0161   bool invalidate(Module &M, const PreservedAnalyses &PA,
0162                   ModuleAnalysisManager::Invalidator &Inv);
0163 };
0164 
0165 /// An analysis pass which caches information about the entire Module.
0166 /// Records a cache of the 'active' gc strategy objects for the current Module.
0167 class CollectorMetadataAnalysis
0168     : public AnalysisInfoMixin<CollectorMetadataAnalysis> {
0169   friend struct AnalysisInfoMixin<CollectorMetadataAnalysis>;
0170   static AnalysisKey Key;
0171 
0172 public:
0173   using Result = GCStrategyMap;
0174   Result run(Module &M, ModuleAnalysisManager &MAM);
0175 };
0176 
0177 /// An analysis pass which caches information about the Function.
0178 /// Records the function level information used by GCRoots.
0179 /// This pass depends on `CollectorMetadataAnalysis`.
0180 class GCFunctionAnalysis : public AnalysisInfoMixin<GCFunctionAnalysis> {
0181   friend struct AnalysisInfoMixin<GCFunctionAnalysis>;
0182   static AnalysisKey Key;
0183 
0184 public:
0185   using Result = GCFunctionInfo;
0186   Result run(Function &F, FunctionAnalysisManager &FAM);
0187 };
0188 
0189 /// LowerIntrinsics - This pass rewrites calls to the llvm.gcread or
0190 /// llvm.gcwrite intrinsics, replacing them with simple loads and stores as
0191 /// directed by the GCStrategy. It also performs automatic root initialization
0192 /// and custom intrinsic lowering.
0193 ///
0194 /// This pass requires `CollectorMetadataAnalysis`.
0195 class GCLoweringPass : public PassInfoMixin<GCLoweringPass> {
0196 public:
0197   PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
0198 };
0199 
0200 /// An analysis pass which caches information about the entire Module.
0201 /// Records both the function level information used by GCRoots and a
0202 /// cache of the 'active' gc strategy objects for the current Module.
0203 class GCModuleInfo : public ImmutablePass {
0204   /// An owning list of all GCStrategies which have been created
0205   SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
0206   /// A helper map to speedup lookups into the above list
0207   StringMap<GCStrategy*> GCStrategyMap;
0208 
0209 public:
0210   /// Lookup the GCStrategy object associated with the given gc name.
0211   /// Objects are owned internally; No caller should attempt to delete the
0212   /// returned objects.
0213   GCStrategy *getGCStrategy(const StringRef Name);
0214 
0215   /// List of per function info objects.  In theory, Each of these
0216   /// may be associated with a different GC.
0217   using FuncInfoVec = std::vector<std::unique_ptr<GCFunctionInfo>>;
0218 
0219   FuncInfoVec::iterator funcinfo_begin() { return Functions.begin(); }
0220   FuncInfoVec::iterator funcinfo_end() { return Functions.end(); }
0221 
0222 private:
0223   /// Owning list of all GCFunctionInfos associated with this Module
0224   FuncInfoVec Functions;
0225 
0226   /// Non-owning map to bypass linear search when finding the GCFunctionInfo
0227   /// associated with a particular Function.
0228   using finfo_map_type = DenseMap<const Function *, GCFunctionInfo *>;
0229   finfo_map_type FInfoMap;
0230 
0231 public:
0232   using iterator = SmallVector<std::unique_ptr<GCStrategy>, 1>::const_iterator;
0233 
0234   static char ID;
0235 
0236   GCModuleInfo();
0237 
0238   /// clear - Resets the pass. Any pass, which uses GCModuleInfo, should
0239   /// call it in doFinalization().
0240   ///
0241   void clear();
0242 
0243   /// begin/end - Iterators for used strategies.
0244   ///
0245   iterator begin() const { return GCStrategyList.begin(); }
0246   iterator end() const { return GCStrategyList.end(); }
0247 
0248   /// get - Look up function metadata.  This is currently assumed
0249   /// have the side effect of initializing the associated GCStrategy.  That
0250   /// will soon change.
0251   GCFunctionInfo &getFunctionInfo(const Function &F);
0252 };
0253 
0254 } // end namespace llvm
0255 
0256 #endif // LLVM_CODEGEN_GCMETADATA_H