ValueMapper.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- ValueMapper.h - Remapping for constants and metadata -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the MapValue interface which is used by various parts of
  11. // the Transforms/Utils library to implement cloning and linking facilities.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
  15. #define LLVM_TRANSFORMS_UTILS_VALUEMAPPER_H
  16. #include "llvm/IR/ValueMap.h"
  17. namespace llvm {
  18. class Value;
  19. class Instruction;
  20. typedef ValueMap<const Value *, WeakVH> ValueToValueMapTy;
  21. /// ValueMapTypeRemapper - This is a class that can be implemented by clients
  22. /// to remap types when cloning constants and instructions.
  23. class ValueMapTypeRemapper {
  24. virtual void anchor(); // Out of line method.
  25. public:
  26. virtual ~ValueMapTypeRemapper() {}
  27. /// remapType - The client should implement this method if they want to
  28. /// remap types while mapping values.
  29. virtual Type *remapType(Type *SrcTy) = 0;
  30. };
  31. /// ValueMaterializer - This is a class that can be implemented by clients
  32. /// to materialize Values on demand.
  33. class ValueMaterializer {
  34. virtual void anchor(); // Out of line method.
  35. public:
  36. virtual ~ValueMaterializer() {}
  37. /// materializeValueFor - The client should implement this method if they
  38. /// want to generate a mapped Value on demand. For example, if linking
  39. /// lazily.
  40. virtual Value *materializeValueFor(Value *V) = 0;
  41. };
  42. /// RemapFlags - These are flags that the value mapping APIs allow.
  43. enum RemapFlags {
  44. RF_None = 0,
  45. /// RF_NoModuleLevelChanges - If this flag is set, the remapper knows that
  46. /// only local values within a function (such as an instruction or argument)
  47. /// are mapped, not global values like functions and global metadata.
  48. RF_NoModuleLevelChanges = 1,
  49. /// RF_IgnoreMissingEntries - If this flag is set, the remapper ignores
  50. /// entries that are not in the value map. If it is unset, it aborts if an
  51. /// operand is asked to be remapped which doesn't exist in the mapping.
  52. RF_IgnoreMissingEntries = 2
  53. };
  54. static inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
  55. return RemapFlags(unsigned(LHS)|unsigned(RHS));
  56. }
  57. Value *MapValue(const Value *V, ValueToValueMapTy &VM,
  58. RemapFlags Flags = RF_None,
  59. ValueMapTypeRemapper *TypeMapper = nullptr,
  60. ValueMaterializer *Materializer = nullptr);
  61. Metadata *MapMetadata(const Metadata *MD, ValueToValueMapTy &VM,
  62. RemapFlags Flags = RF_None,
  63. ValueMapTypeRemapper *TypeMapper = nullptr,
  64. ValueMaterializer *Materializer = nullptr);
  65. /// MapMetadata - provide versions that preserve type safety for MDNodes.
  66. MDNode *MapMetadata(const MDNode *MD, ValueToValueMapTy &VM,
  67. RemapFlags Flags = RF_None,
  68. ValueMapTypeRemapper *TypeMapper = nullptr,
  69. ValueMaterializer *Materializer = nullptr);
  70. void RemapInstruction(Instruction *I, ValueToValueMapTy &VM,
  71. RemapFlags Flags = RF_None,
  72. ValueMapTypeRemapper *TypeMapper = nullptr,
  73. ValueMaterializer *Materializer = nullptr);
  74. /// MapValue - provide versions that preserve type safety for Constants.
  75. inline Constant *MapValue(const Constant *V, ValueToValueMapTy &VM,
  76. RemapFlags Flags = RF_None,
  77. ValueMapTypeRemapper *TypeMapper = nullptr,
  78. ValueMaterializer *Materializer = nullptr) {
  79. return cast<Constant>(MapValue((const Value*)V, VM, Flags, TypeMapper,
  80. Materializer));
  81. }
  82. } // End llvm namespace
  83. #endif