LLVM  mainline
ValueMapper.h
Go to the documentation of this file.
00001 //===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MapValue interface which is used by various parts of
00011 // the Transforms/Utils library to implement cloning and linking facilities.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
00016 #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
00017 
00018 #include "llvm/IR/ValueMap.h"
00019 
00020 namespace llvm {
00021   class Value;
00022   class Instruction;
00023   typedef ValueMap<const Value *, WeakVH> ValueToValueMapTy;
00024 
00025   /// ValueMapTypeRemapper - This is a class that can be implemented by clients
00026   /// to remap types when cloning constants and instructions.
00027   class ValueMapTypeRemapper {
00028     virtual void anchor();  // Out of line method.
00029   public:
00030     virtual ~ValueMapTypeRemapper() {}
00031 
00032     /// remapType - The client should implement this method if they want to
00033     /// remap types while mapping values.
00034     virtual Type *remapType(Type *SrcTy) = 0;
00035   };
00036 
00037   /// ValueMaterializer - This is a class that can be implemented by clients
00038   /// to materialize Values on demand.
00039   class ValueMaterializer {
00040     virtual void anchor(); // Out of line method.
00041 
00042   protected:
00043     ~ValueMaterializer() = default;
00044     ValueMaterializer() = default;
00045     ValueMaterializer(const ValueMaterializer&) = default;
00046     ValueMaterializer &operator=(const ValueMaterializer&) = default;
00047 
00048   public:
00049     /// The client should implement this method if they want to generate a
00050     /// mapped Value on demand. For example, if linking lazily.
00051     virtual Value *materializeDeclFor(Value *V) = 0;
00052 
00053     /// If the data being mapped is recursive, the above function can map
00054     /// just the declaration and this is called to compute the initializer.
00055     /// It is called after the mapping is recorded, so it doesn't need to worry
00056     /// about recursion.
00057     virtual void materializeInitFor(GlobalValue *New, GlobalValue *Old);
00058 
00059     /// If the client needs to handle temporary metadata it must implement
00060     /// these methods.
00061     virtual Metadata *mapTemporaryMetadata(Metadata *MD) { return nullptr; }
00062     virtual void replaceTemporaryMetadata(const Metadata *OrigMD,
00063                                           Metadata *NewMD) {}
00064 
00065     /// The client should implement this method if some metadata need
00066     /// not be mapped, for example DISubprogram metadata for functions not
00067     /// linked into the destination module.
00068     virtual bool isMetadataNeeded(Metadata *MD) { return true; }
00069   };
00070 
00071   /// RemapFlags - These are flags that the value mapping APIs allow.
00072   enum RemapFlags {
00073     RF_None = 0,
00074 
00075     /// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that
00076     /// only local values within a function (such as an instruction or argument)
00077     /// are mapped, not global values like functions and global metadata.
00078     RF_NoModuleLevelChanges = 1,
00079 
00080     /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
00081     /// entries that are not in the value map.  If it is unset, it aborts if an
00082     /// operand is asked to be remapped which doesn't exist in the mapping.
00083     RF_IgnoreMissingEntries = 2,
00084 
00085     /// Instruct the remapper to move distinct metadata instead of duplicating
00086     /// it when there are module-level changes.
00087     RF_MoveDistinctMDs = 4,
00088 
00089     /// Any global values not in value map are mapped to null instead of
00090     /// mapping to self. Illegal if RF_IgnoreMissingEntries is also set.
00091     RF_NullMapMissingGlobalValues = 8,
00092 
00093     /// Set when there is still temporary metadata that must be handled,
00094     /// such as when we are doing function importing and will materialize
00095     /// and link metadata as a postpass.
00096     RF_HaveUnmaterializedMetadata = 16,
00097   };
00098 
00099   static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
00100     return RemapFlags(unsigned(LHS)|unsigned(RHS));
00101   }
00102 
00103   Value *MapValue(const Value *V, ValueToValueMapTy &VM,
00104                   RemapFlags Flags = RF_None,
00105                   ValueMapTypeRemapper *TypeMapper = nullptr,
00106                   ValueMaterializer *Materializer = nullptr);
00107 
00108   Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
00109                         RemapFlags Flags = RF_None,
00110                         ValueMapTypeRemapper *TypeMapper = nullptr,
00111                         ValueMaterializer *Materializer = nullptr);
00112 
00113   /// MapMetadata - provide versions that preserve type safety for MDNodes.
00114   MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
00115                       RemapFlags Flags = RF_None,
00116                       ValueMapTypeRemapper *TypeMapper = nullptr,
00117                       ValueMaterializer *Materializer = nullptr);
00118 
00119   void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
00120                         RemapFlags Flags = RF_None,
00121                         ValueMapTypeRemapper *TypeMapper = nullptr,
00122                         ValueMaterializer *Materializer = nullptr);
00123 
00124   /// MapValue - provide versions that preserve type safety for Constants.
00125   inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
00126                             RemapFlags Flags = RF_None,
00127                             ValueMapTypeRemapper *TypeMapper = nullptr,
00128                             ValueMaterializer *Materializer = nullptr) {
00129     return cast<Constant>(MapValue((const Value*)V, VM, Flags, TypeMapper,
00130                                    Materializer));
00131   }
00132 
00133 } // End llvm namespace
00134 
00135 #endif