ValueMapper.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
  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 function, which is shared by various parts of
  11. // the lib/Transforms/Utils library.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Utils/ValueMapper.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/InlineAsm.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/Metadata.h"
  20. using namespace llvm;
  21. // Out of line method to get vtable etc for class.
  22. void ValueMapTypeRemapper::anchor() {}
  23. void ValueMaterializer::anchor() {}
  24. Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
  25. ValueMapTypeRemapper *TypeMapper,
  26. ValueMaterializer *Materializer) {
  27. ValueToValueMapTy::iterator I = VM.find(V);
  28. // If the value already exists in the map, use it.
  29. if (I != VM.end() && I->second) return I->second;
  30. // If we have a materializer and it can materialize a value, use that.
  31. if (Materializer) {
  32. if (Value *NewV = Materializer->materializeValueFor(const_cast<Value*>(V)))
  33. return VM[V] = NewV;
  34. }
  35. // Global values do not need to be seeded into the VM if they
  36. // are using the identity mapping.
  37. if (isa<GlobalValue>(V))
  38. return VM[V] = const_cast<Value*>(V);
  39. if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
  40. // Inline asm may need *type* remapping.
  41. FunctionType *NewTy = IA->getFunctionType();
  42. if (TypeMapper) {
  43. NewTy = cast<FunctionType>(TypeMapper->remapType(NewTy));
  44. if (NewTy != IA->getFunctionType())
  45. V = InlineAsm::get(NewTy, IA->getAsmString(), IA->getConstraintString(),
  46. IA->hasSideEffects(), IA->isAlignStack());
  47. }
  48. return VM[V] = const_cast<Value*>(V);
  49. }
  50. if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
  51. const Metadata *MD = MDV->getMetadata();
  52. // If this is a module-level metadata and we know that nothing at the module
  53. // level is changing, then use an identity mapping.
  54. if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
  55. return VM[V] = const_cast<Value *>(V);
  56. auto *MappedMD = MapValue(MD, VM, Flags, TypeMapper, Materializer);
  57. if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
  58. return VM[V] = const_cast<Value *>(V);
  59. // FIXME: This assert crashes during bootstrap, but I think it should be
  60. // correct. For now, just match behaviour from before the metadata/value
  61. // split.
  62. //
  63. // assert(MappedMD && "Referenced metadata value not in value map");
  64. return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
  65. }
  66. // Okay, this either must be a constant (which may or may not be mappable) or
  67. // is something that is not in the mapping table.
  68. Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V));
  69. if (!C)
  70. return nullptr;
  71. if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
  72. Function *F =
  73. cast<Function>(MapValue(BA->getFunction(), VM, Flags, TypeMapper, Materializer));
  74. BasicBlock *BB = cast_or_null<BasicBlock>(MapValue(BA->getBasicBlock(), VM,
  75. Flags, TypeMapper, Materializer));
  76. return VM[V] = BlockAddress::get(F, BB ? BB : BA->getBasicBlock());
  77. }
  78. // Otherwise, we have some other constant to remap. Start by checking to see
  79. // if all operands have an identity remapping.
  80. unsigned OpNo = 0, NumOperands = C->getNumOperands();
  81. Value *Mapped = nullptr;
  82. for (; OpNo != NumOperands; ++OpNo) {
  83. Value *Op = C->getOperand(OpNo);
  84. Mapped = MapValue(Op, VM, Flags, TypeMapper, Materializer);
  85. if (Mapped != C) break;
  86. }
  87. // See if the type mapper wants to remap the type as well.
  88. Type *NewTy = C->getType();
  89. if (TypeMapper)
  90. NewTy = TypeMapper->remapType(NewTy);
  91. // If the result type and all operands match up, then just insert an identity
  92. // mapping.
  93. if (OpNo == NumOperands && NewTy == C->getType())
  94. return VM[V] = C;
  95. // Okay, we need to create a new constant. We've already processed some or
  96. // all of the operands, set them all up now.
  97. SmallVector<Constant*, 8> Ops;
  98. Ops.reserve(NumOperands);
  99. for (unsigned j = 0; j != OpNo; ++j)
  100. Ops.push_back(cast<Constant>(C->getOperand(j)));
  101. // If one of the operands mismatch, push it and the other mapped operands.
  102. if (OpNo != NumOperands) {
  103. Ops.push_back(cast<Constant>(Mapped));
  104. // Map the rest of the operands that aren't processed yet.
  105. for (++OpNo; OpNo != NumOperands; ++OpNo)
  106. Ops.push_back(MapValue(cast<Constant>(C->getOperand(OpNo)), VM,
  107. Flags, TypeMapper, Materializer));
  108. }
  109. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
  110. return VM[V] = CE->getWithOperands(Ops, NewTy);
  111. if (isa<ConstantArray>(C))
  112. return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
  113. if (isa<ConstantStruct>(C))
  114. return VM[V] = ConstantStruct::get(cast<StructType>(NewTy), Ops);
  115. if (isa<ConstantVector>(C))
  116. return VM[V] = ConstantVector::get(Ops);
  117. // If this is a no-operand constant, it must be because the type was remapped.
  118. if (isa<UndefValue>(C))
  119. return VM[V] = UndefValue::get(NewTy);
  120. if (isa<ConstantAggregateZero>(C))
  121. return VM[V] = ConstantAggregateZero::get(NewTy);
  122. assert(isa<ConstantPointerNull>(C));
  123. return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
  124. }
  125. static Metadata *map(ValueToValueMapTy &VM, const Metadata *Key,
  126. Metadata *Val) {
  127. VM.MD()[Key].reset(Val);
  128. return Val;
  129. }
  130. static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
  131. return map(VM, MD, const_cast<Metadata *>(MD));
  132. }
  133. static Metadata *MapValueImpl(const Metadata *MD, ValueToValueMapTy &VM,
  134. RemapFlags Flags,
  135. ValueMapTypeRemapper *TypeMapper,
  136. ValueMaterializer *Materializer) {
  137. // If the value already exists in the map, use it.
  138. if (Metadata *NewMD = VM.MD().lookup(MD).get())
  139. return NewMD;
  140. if (isa<MDString>(MD))
  141. return mapToSelf(VM, MD);
  142. if (isa<ConstantAsMetadata>(MD))
  143. if ((Flags & RF_NoModuleLevelChanges))
  144. return mapToSelf(VM, MD);
  145. if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
  146. Value *MappedV =
  147. MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
  148. if (VMD->getValue() == MappedV ||
  149. (!MappedV && (Flags & RF_IgnoreMissingEntries)))
  150. return mapToSelf(VM, MD);
  151. // FIXME: This assert crashes during bootstrap, but I think it should be
  152. // correct. For now, just match behaviour from before the metadata/value
  153. // split.
  154. //
  155. // assert(MappedV && "Referenced metadata not in value map!");
  156. if (MappedV)
  157. return map(VM, MD, ValueAsMetadata::get(MappedV));
  158. return nullptr;
  159. }
  160. const MDNode *Node = cast<MDNode>(MD);
  161. assert(Node->isResolved() && "Unexpected unresolved node");
  162. auto getMappedOp = [&](Metadata *Op) -> Metadata *{
  163. if (!Op)
  164. return nullptr;
  165. if (Metadata *MappedOp =
  166. MapValueImpl(Op, VM, Flags, TypeMapper, Materializer))
  167. return MappedOp;
  168. // Use identity map if MappedOp is null and we can ignore missing entries.
  169. if (Flags & RF_IgnoreMissingEntries)
  170. return Op;
  171. // FIXME: This assert crashes during bootstrap, but I think it should be
  172. // correct. For now, just match behaviour from before the metadata/value
  173. // split.
  174. //
  175. // llvm_unreachable("Referenced metadata not in value map!");
  176. return nullptr;
  177. };
  178. // If this is a module-level metadata and we know that nothing at the
  179. // module level is changing, then use an identity mapping.
  180. if (Flags & RF_NoModuleLevelChanges)
  181. return mapToSelf(VM, MD);
  182. // Create a dummy node in case we have a metadata cycle.
  183. MDNodeFwdDecl *Dummy = MDNode::getTemporary(Node->getContext(), None);
  184. map(VM, Node, Dummy);
  185. // Check all operands to see if any need to be remapped.
  186. for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
  187. Metadata *Op = Node->getOperand(I);
  188. Metadata *MappedOp = getMappedOp(Op);
  189. if (Op == MappedOp)
  190. continue;
  191. // Ok, at least one operand needs remapping.
  192. SmallVector<Metadata *, 4> Elts;
  193. Elts.reserve(Node->getNumOperands());
  194. for (I = 0; I != E; ++I)
  195. Elts.push_back(getMappedOp(Node->getOperand(I)));
  196. MDNode *NewMD = MDNode::get(Node->getContext(), Elts);
  197. Dummy->replaceAllUsesWith(NewMD);
  198. MDNode::deleteTemporary(Dummy);
  199. return map(VM, Node, NewMD);
  200. }
  201. // No operands needed remapping. Use an identity mapping.
  202. mapToSelf(VM, MD);
  203. MDNode::deleteTemporary(Dummy);
  204. return const_cast<Metadata *>(MD);
  205. }
  206. Metadata *llvm::MapValue(const Metadata *MD, ValueToValueMapTy &VM,
  207. RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
  208. ValueMaterializer *Materializer) {
  209. Metadata *NewMD = MapValueImpl(MD, VM, Flags, TypeMapper, Materializer);
  210. if (NewMD && NewMD != MD)
  211. if (auto *G = dyn_cast<GenericMDNode>(NewMD))
  212. G->resolveCycles();
  213. return NewMD;
  214. }
  215. MDNode *llvm::MapValue(const MDNode *MD, ValueToValueMapTy &VM,
  216. RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
  217. ValueMaterializer *Materializer) {
  218. return cast<MDNode>(MapValue(static_cast<const Metadata *>(MD), VM, Flags,
  219. TypeMapper, Materializer));
  220. }
  221. /// RemapInstruction - Convert the instruction operands from referencing the
  222. /// current values into those specified by VMap.
  223. ///
  224. void llvm::RemapInstruction(Instruction *I, ValueToValueMapTy &VMap,
  225. RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
  226. ValueMaterializer *Materializer){
  227. // Remap operands.
  228. for (User::op_iterator op = I->op_begin(), E = I->op_end(); op != E; ++op) {
  229. Value *V = MapValue(*op, VMap, Flags, TypeMapper, Materializer);
  230. // If we aren't ignoring missing entries, assert that something happened.
  231. if (V)
  232. *op = V;
  233. else
  234. assert((Flags & RF_IgnoreMissingEntries) &&
  235. "Referenced value not in value map!");
  236. }
  237. // Remap phi nodes' incoming blocks.
  238. if (PHINode *PN = dyn_cast<PHINode>(I)) {
  239. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  240. Value *V = MapValue(PN->getIncomingBlock(i), VMap, Flags);
  241. // If we aren't ignoring missing entries, assert that something happened.
  242. if (V)
  243. PN->setIncomingBlock(i, cast<BasicBlock>(V));
  244. else
  245. assert((Flags & RF_IgnoreMissingEntries) &&
  246. "Referenced block not in value map!");
  247. }
  248. }
  249. // Remap attached metadata.
  250. SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
  251. I->getAllMetadata(MDs);
  252. for (SmallVectorImpl<std::pair<unsigned, MDNode *>>::iterator
  253. MI = MDs.begin(),
  254. ME = MDs.end();
  255. MI != ME; ++MI) {
  256. MDNode *Old = MI->second;
  257. MDNode *New = MapValue(Old, VMap, Flags, TypeMapper, Materializer);
  258. if (New != Old)
  259. I->setMetadata(MI->first, New);
  260. }
  261. // If the instruction's type is being remapped, do so now.
  262. if (TypeMapper)
  263. I->mutateType(TypeMapper->remapType(I->getType()));
  264. }