Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:08

0001 //===-- llvm/IR/ModuleSlotTracker.h -----------------------------*- 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 #ifndef LLVM_IR_MODULESLOTTRACKER_H
0010 #define LLVM_IR_MODULESLOTTRACKER_H
0011 
0012 #include <functional>
0013 #include <memory>
0014 #include <utility>
0015 #include <vector>
0016 
0017 namespace llvm {
0018 
0019 class Module;
0020 class Function;
0021 class SlotTracker;
0022 class Value;
0023 class MDNode;
0024 
0025 /// Abstract interface of slot tracker storage.
0026 class AbstractSlotTrackerStorage {
0027 public:
0028   virtual ~AbstractSlotTrackerStorage();
0029 
0030   virtual unsigned getNextMetadataSlot() = 0;
0031 
0032   virtual void createMetadataSlot(const MDNode *) = 0;
0033   virtual int getMetadataSlot(const MDNode *) = 0;
0034 };
0035 
0036 /// Manage lifetime of a slot tracker for printing IR.
0037 ///
0038 /// Wrapper around the \a SlotTracker used internally by \a AsmWriter.  This
0039 /// class allows callers to share the cost of incorporating the metadata in a
0040 /// module or a function.
0041 ///
0042 /// If the IR changes from underneath \a ModuleSlotTracker, strings like
0043 /// "<badref>" will be printed, or, worse, the wrong slots entirely.
0044 class ModuleSlotTracker {
0045   /// Storage for a slot tracker.
0046   std::unique_ptr<SlotTracker> MachineStorage;
0047   bool ShouldCreateStorage = false;
0048   bool ShouldInitializeAllMetadata = false;
0049 
0050   const Module *M = nullptr;
0051   const Function *F = nullptr;
0052   SlotTracker *Machine = nullptr;
0053 
0054   std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
0055       ProcessModuleHookFn;
0056   std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
0057       ProcessFunctionHookFn;
0058 
0059 public:
0060   /// Wrap a preinitialized SlotTracker.
0061   ModuleSlotTracker(SlotTracker &Machine, const Module *M,
0062                     const Function *F = nullptr);
0063 
0064   /// Construct a slot tracker from a module.
0065   ///
0066   /// If \a M is \c nullptr, uses a null slot tracker.  Otherwise, initializes
0067   /// a slot tracker, and initializes all metadata slots.  \c
0068   /// ShouldInitializeAllMetadata defaults to true because this is expected to
0069   /// be shared between multiple callers, and otherwise MDNode references will
0070   /// not match up.
0071   explicit ModuleSlotTracker(const Module *M,
0072                              bool ShouldInitializeAllMetadata = true);
0073 
0074   /// Destructor to clean up storage.
0075   virtual ~ModuleSlotTracker();
0076 
0077   /// Lazily creates a slot tracker.
0078   SlotTracker *getMachine();
0079 
0080   const Module *getModule() const { return M; }
0081   const Function *getCurrentFunction() const { return F; }
0082 
0083   /// Incorporate the given function.
0084   ///
0085   /// Purge the currently incorporated function and incorporate \c F.  If \c F
0086   /// is currently incorporated, this is a no-op.
0087   void incorporateFunction(const Function &F);
0088 
0089   /// Return the slot number of the specified local value.
0090   ///
0091   /// A function that defines this value should be incorporated prior to calling
0092   /// this method.
0093   /// Return -1 if the value is not in the function's SlotTracker.
0094   int getLocalSlot(const Value *V);
0095 
0096   void setProcessHook(
0097       std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>);
0098   void setProcessHook(std::function<void(AbstractSlotTrackerStorage *,
0099                                          const Function *, bool)>);
0100 
0101   using MachineMDNodeListType =
0102       std::vector<std::pair<unsigned, const MDNode *>>;
0103 
0104   void collectMDNodes(MachineMDNodeListType &L, unsigned LB, unsigned UB) const;
0105 };
0106 
0107 } // end namespace llvm
0108 
0109 #endif