//===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file defines the MapValue interface which is used by various parts of // the Transforms/Utils library to implement cloning and linking facilities. // //===----------------------------------------------------------------------===// #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/simple_ilist.h" #include "llvm/IR/ValueHandle.h" #include "llvm/IR/ValueMap.h" namespace llvm { class Constant; class DIBuilder; class DbgRecord; class Function; class GlobalVariable; class Instruction; class MDNode; class Metadata; class Module; class Type; class Value; ValueToValueMapTy; DbgRecordIterator; /// This is a class that can be implemented by clients to remap types when /// cloning constants and instructions. class ValueMapTypeRemapper { … }; /// This is a class that can be implemented by clients to materialize Values on /// demand. class ValueMaterializer { … }; /// These are flags that the value mapping APIs allow. enum RemapFlags { … }; inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) { … } /// Context for (re-)mapping values (and metadata). /// /// A shared context used for mapping and remapping of Value and Metadata /// instances using \a ValueToValueMapTy, \a RemapFlags, \a /// ValueMapTypeRemapper, and \a ValueMaterializer. /// /// There are a number of top-level entry points: /// - \a mapValue() (and \a mapConstant()); /// - \a mapMetadata() (and \a mapMDNode()); /// - \a remapInstruction(); /// - \a remapFunction(); and /// - \a remapGlobalObjectMetadata(). /// /// The \a ValueMaterializer can be used as a callback, but cannot invoke any /// of these top-level functions recursively. Instead, callbacks should use /// one of the following to schedule work lazily in the \a ValueMapper /// instance: /// - \a scheduleMapGlobalInitializer() /// - \a scheduleMapAppendingVariable() /// - \a scheduleMapGlobalAlias() /// - \a scheduleMapGlobalIFunc() /// - \a scheduleRemapFunction() /// /// Sometimes a callback needs a different mapping context. Such a context can /// be registered using \a registerAlternateMappingContext(), which takes an /// alternate \a ValueToValueMapTy and \a ValueMaterializer and returns a ID to /// pass into the schedule*() functions. /// /// TODO: lib/Linker really doesn't need the \a ValueHandle in the \a /// ValueToValueMapTy. We should template \a ValueMapper (and its /// implementation classes), and explicitly instantiate on two concrete /// instances of \a ValueMap (one as \a ValueToValueMap, and one with raw \a /// Value pointers). It may be viable to do away with \a TrackingMDRef in the /// \a Metadata side map for the lib/Linker case as well, in which case we'll /// need a new template parameter on \a ValueMap. /// /// TODO: Update callers of \a RemapInstruction() and \a MapValue() (etc.) to /// use \a ValueMapper directly. class ValueMapper { … }; /// Look up or compute a value in the value map. /// /// Return a mapped value for a function-local value (Argument, Instruction, /// BasicBlock), or compute and memoize a value for a Constant. /// /// 1. If \c V is in VM, return the result. /// 2. Else if \c V can be materialized with \c Materializer, do so, memoize /// it in \c VM, and return it. /// 3. Else if \c V is a function-local value, return nullptr. /// 4. Else if \c V is a \a GlobalValue, return \c nullptr or \c V depending /// on \a RF_NullMapMissingGlobalValues. /// 5. Else if \c V is a \a MetadataAsValue wrapping a LocalAsMetadata, /// recurse on the local SSA value, and return nullptr or "metadata !{}" on /// missing depending on RF_IgnoreMissingValues. /// 6. Else if \c V is a \a MetadataAsValue, rewrap the return of \a /// MapMetadata(). /// 7. Else, compute the equivalent constant, and return it. inline Value *MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Lookup or compute a mapping for a piece of metadata. /// /// Compute and memoize a mapping for \c MD. /// /// 1. If \c MD is mapped, return it. /// 2. Else if \a RF_NoModuleLevelChanges or \c MD is an \a MDString, return /// \c MD. /// 3. Else if \c MD is a \a ConstantAsMetadata, call \a MapValue() and /// re-wrap its return (returning nullptr on nullptr). /// 4. Else, \c MD is an \a MDNode. These are remapped, along with their /// transitive operands. Distinct nodes are duplicated or moved depending /// on \a RF_MoveDistinctNodes. Uniqued nodes are remapped like constants. /// /// \note \a LocalAsMetadata is completely unsupported by \a MapMetadata. /// Instead, use \a MapValue() with its wrapping \a MetadataAsValue instance. inline Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Version of MapMetadata with type safety for MDNode. inline MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Convert the instruction operands from referencing the current values into /// those specified by VM. /// /// If \a RF_IgnoreMissingLocals is set and an operand can't be found via \a /// MapValue(), use the old value. Otherwise assert that this doesn't happen. /// /// Note that \a MapValue() only returns \c nullptr for SSA values missing from /// \c VM. inline void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Remap the Values used in the DbgRecord \a DR using the value map \a /// VM. inline void RemapDbgRecord(Module *M, DbgRecord *DR, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Remap the Values used in the DbgRecords \a Range using the value map \a /// VM. inline void RemapDbgRecordRange(Module *M, iterator_range<DbgRecordIterator> Range, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Remap the operands, metadata, arguments, and instructions of a function. /// /// Calls \a MapValue() on prefix data, prologue data, and personality /// function; calls \a MapMetadata() on each attached MDNode; remaps the /// argument types using the provided \c TypeMapper; and calls \a /// RemapInstruction() on every instruction. inline void RemapFunction(Function &F, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } /// Version of MapValue with type safety for Constant. inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM, RemapFlags Flags = RF_None, ValueMapTypeRemapper *TypeMapper = nullptr, ValueMaterializer *Materializer = nullptr) { … } } // end namespace llvm #endif // LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H